2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 #include <ipxe/image.h>
29 #include <ipxe/uaccess.h>
30 #include <ipxe/init.h>
31 #include <ipxe/memblock.h>
35 * Initial ramdisk (initrd) reshuffling
39 /** Maximum address available for initrd */
42 /** Minimum address available for initrd */
43 userptr_t initrd_bottom;
46 * Squash initrds as high as possible in memory
48 * @v top Highest possible address
49 * @ret used Lowest address used by initrds
51 static userptr_t initrd_squash_high ( userptr_t top ) {
52 userptr_t current = top;
54 struct image *highest;
57 /* Squash up any initrds already within or below the region */
60 /* Find the highest image not yet in its final position */
62 for_each_image ( initrd ) {
63 if ( ( userptr_sub ( initrd->data, current ) < 0 ) &&
64 ( ( highest == NULL ) ||
65 ( userptr_sub ( initrd->data,
66 highest->data ) > 0 ) ) ) {
73 /* Move this image to its final position */
74 len = ( ( highest->len + INITRD_ALIGN - 1 ) &
75 ~( INITRD_ALIGN - 1 ) );
76 current = userptr_sub ( current, len );
77 DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
78 "[%#08lx,%#08lx)\n", highest->name,
79 user_to_phys ( highest->data, 0 ),
80 user_to_phys ( highest->data, highest->len ),
81 user_to_phys ( current, 0 ),
82 user_to_phys ( current, highest->len ) );
83 memmove_user ( current, 0, highest->data, 0, highest->len );
84 highest->data = current;
87 /* Copy any remaining initrds (e.g. embedded images) to the region */
88 for_each_image ( initrd ) {
89 if ( userptr_sub ( initrd->data, top ) >= 0 ) {
90 len = ( ( initrd->len + INITRD_ALIGN - 1 ) &
91 ~( INITRD_ALIGN - 1 ) );
92 current = userptr_sub ( current, len );
93 DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
94 "[%#08lx,%#08lx)\n", initrd->name,
95 user_to_phys ( initrd->data, 0 ),
96 user_to_phys ( initrd->data, initrd->len ),
97 user_to_phys ( current, 0 ),
98 user_to_phys ( current, initrd->len ) );
99 memcpy_user ( current, 0, initrd->data, 0,
101 initrd->data = current;
109 * Swap position of two adjacent initrds
111 * @v low Lower initrd
112 * @v high Higher initrd
114 * @v free_len Length of free space
116 static void initrd_swap ( struct image *low, struct image *high,
117 userptr_t free, size_t free_len ) {
122 DBGC ( &images, "INITRD swapping %s [%#08lx,%#08lx)<->[%#08lx,%#08lx) "
123 "%s\n", low->name, user_to_phys ( low->data, 0 ),
124 user_to_phys ( low->data, low->len ),
125 user_to_phys ( high->data, 0 ),
126 user_to_phys ( high->data, high->len ), high->name );
128 /* Round down length of free space */
129 free_len &= ~( INITRD_ALIGN - 1 );
130 assert ( free_len > 0 );
132 /* Swap image data */
133 while ( len < high->len ) {
135 /* Calculate maximum fragment length */
136 frag_len = ( high->len - len );
137 if ( frag_len > free_len )
139 new_len = ( ( len + frag_len + INITRD_ALIGN - 1 ) &
140 ~( INITRD_ALIGN - 1 ) );
143 memcpy_user ( free, 0, high->data, len, frag_len );
144 memmove_user ( low->data, new_len, low->data, len, low->len );
145 memcpy_user ( low->data, len, free, 0, frag_len );
149 /* Adjust data pointers */
150 high->data = low->data;
151 low->data = userptr_add ( low->data, len );
155 * Swap position of any two adjacent initrds not currently in the correct order
158 * @v free_len Length of free space
159 * @ret swapped A pair of initrds was swapped
161 static int initrd_swap_any ( userptr_t free, size_t free_len ) {
167 /* Find any pair of initrds that can be swapped */
168 for_each_image ( low ) {
170 /* Calculate location of adjacent image (if any) */
171 padded_len = ( ( low->len + INITRD_ALIGN - 1 ) &
172 ~( INITRD_ALIGN - 1 ) );
173 adjacent = userptr_add ( low->data, padded_len );
175 /* Search for adjacent image */
176 for_each_image ( high ) {
178 /* If we have found the adjacent image, swap and exit */
179 if ( high->data == adjacent ) {
180 initrd_swap ( low, high, free, free_len );
184 /* Stop search if all remaining potential
185 * adjacent images are already in the correct
193 /* Nothing swapped */
198 * Dump initrd locations (for debug)
201 static void initrd_dump ( void ) {
202 struct image *initrd;
204 /* Do nothing unless debugging is enabled */
208 /* Dump initrd locations */
209 for_each_image ( initrd ) {
210 DBGC ( &images, "INITRD %s at [%#08lx,%#08lx)\n",
211 initrd->name, user_to_phys ( initrd->data, 0 ),
212 user_to_phys ( initrd->data, initrd->len ) );
213 DBGC2_MD5A ( &images, user_to_phys ( initrd->data, 0 ),
214 user_to_virt ( initrd->data, 0 ), initrd->len );
219 * Reshuffle initrds into desired order at top of memory
221 * @v bottom Lowest address available for initrds
223 * After this function returns, the initrds have been rearranged in
224 * memory and the external heap structures will have been corrupted.
225 * Reshuffling must therefore take place immediately prior to jumping
226 * to the loaded OS kernel; no further execution within iPXE is
229 void initrd_reshuffle ( userptr_t bottom ) {
235 /* Calculate limits of available space for initrds */
237 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
238 bottom = initrd_bottom;
241 DBGC ( &images, "INITRD region [%#08lx,%#08lx)\n",
242 user_to_phys ( bottom, 0 ), user_to_phys ( top, 0 ) );
245 /* Squash initrds as high as possible in memory */
246 used = initrd_squash_high ( top );
248 /* Calculate available free space */
250 free_len = userptr_sub ( used, free );
252 /* Bubble-sort initrds into desired order */
253 while ( initrd_swap_any ( free, free_len ) ) {}
260 * Check that there is enough space to reshuffle initrds
262 * @v len Total length of initrds (including padding)
263 * @v bottom Lowest address available for initrds
264 * @ret rc Return status code
266 int initrd_reshuffle_check ( size_t len, userptr_t bottom ) {
270 /* Calculate limits of available space for initrds */
272 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
273 bottom = initrd_bottom;
274 available = userptr_sub ( top, bottom );
276 /* Allow for a sensible minimum amount of free space */
277 len += INITRD_MIN_FREE_LEN;
279 /* Check for available space */
280 return ( ( len < available ) ? 0 : -ENOBUFS );
284 * initrd startup function
287 static void initrd_startup ( void ) {
290 /* Record largest memory block available. Do this after any
291 * allocations made during driver startup (e.g. large host
292 * memory blocks for Infiniband devices, which may still be in
293 * use at the time of rearranging if a SAN device is hooked)
294 * but before any allocations for downloaded images (which we
295 * can safely reuse when rearranging).
297 len = largest_memblock ( &initrd_bottom );
298 initrd_top = userptr_add ( initrd_bottom, len );
301 /** initrd startup function */
302 struct startup_fn startup_initrd __startup_fn ( STARTUP_LATE ) = {
303 .startup = initrd_startup,