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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 #include <ipxe/image.h>
25 #include <ipxe/uaccess.h>
26 #include <ipxe/init.h>
27 #include <ipxe/memblock.h>
31 * Initial ramdisk (initrd) reshuffling
35 /** Maximum address available for initrd */
38 /** Minimum address available for initrd */
39 userptr_t initrd_bottom;
42 * Squash initrds as high as possible in memory
44 * @v top Highest possible address
45 * @ret used Lowest address used by initrds
47 static userptr_t initrd_squash_high ( userptr_t top ) {
48 userptr_t current = top;
50 struct image *highest;
53 /* Squash up any initrds already within or below the region */
56 /* Find the highest image not yet in its final position */
58 for_each_image ( initrd ) {
59 if ( ( userptr_sub ( initrd->data, current ) < 0 ) &&
60 ( ( highest == NULL ) ||
61 ( userptr_sub ( initrd->data,
62 highest->data ) > 0 ) ) ) {
69 /* Move this image to its final position */
70 len = ( ( highest->len + INITRD_ALIGN - 1 ) &
71 ~( INITRD_ALIGN - 1 ) );
72 current = userptr_sub ( current, len );
73 DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
74 "[%#08lx,%#08lx)\n", highest->name,
75 user_to_phys ( highest->data, 0 ),
76 user_to_phys ( highest->data, highest->len ),
77 user_to_phys ( current, 0 ),
78 user_to_phys ( current, highest->len ) );
79 memmove_user ( current, 0, highest->data, 0, highest->len );
80 highest->data = current;
83 /* Copy any remaining initrds (e.g. embedded images) to the region */
84 for_each_image ( initrd ) {
85 if ( userptr_sub ( initrd->data, top ) >= 0 ) {
86 len = ( ( initrd->len + INITRD_ALIGN - 1 ) &
87 ~( INITRD_ALIGN - 1 ) );
88 current = userptr_sub ( current, len );
89 DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
90 "[%#08lx,%#08lx)\n", initrd->name,
91 user_to_phys ( initrd->data, 0 ),
92 user_to_phys ( initrd->data, initrd->len ),
93 user_to_phys ( current, 0 ),
94 user_to_phys ( current, initrd->len ) );
95 memcpy_user ( current, 0, initrd->data, 0,
97 initrd->data = current;
105 * Swap position of two adjacent initrds
107 * @v low Lower initrd
108 * @v high Higher initrd
110 * @v free_len Length of free space
112 static void initrd_swap ( struct image *low, struct image *high,
113 userptr_t free, size_t free_len ) {
118 DBGC ( &images, "INITRD swapping %s [%#08lx,%#08lx)<->[%#08lx,%#08lx) "
119 "%s\n", low->name, user_to_phys ( low->data, 0 ),
120 user_to_phys ( low->data, low->len ),
121 user_to_phys ( high->data, 0 ),
122 user_to_phys ( high->data, high->len ), high->name );
124 /* Round down length of free space */
125 free_len &= ~( INITRD_ALIGN - 1 );
126 assert ( free_len > 0 );
128 /* Swap image data */
129 while ( len < high->len ) {
131 /* Calculate maximum fragment length */
132 frag_len = ( high->len - len );
133 if ( frag_len > free_len )
135 new_len = ( ( len + frag_len + INITRD_ALIGN - 1 ) &
136 ~( INITRD_ALIGN - 1 ) );
139 memcpy_user ( free, 0, high->data, len, frag_len );
140 memmove_user ( low->data, new_len, low->data, len, low->len );
141 memcpy_user ( low->data, len, free, 0, frag_len );
145 /* Adjust data pointers */
146 high->data = low->data;
147 low->data = userptr_add ( low->data, len );
151 * Swap position of any two adjacent initrds not currently in the correct order
154 * @v free_len Length of free space
155 * @ret swapped A pair of initrds was swapped
157 static int initrd_swap_any ( userptr_t free, size_t free_len ) {
163 /* Find any pair of initrds that can be swapped */
164 for_each_image ( low ) {
166 /* Calculate location of adjacent image (if any) */
167 padded_len = ( ( low->len + INITRD_ALIGN - 1 ) &
168 ~( INITRD_ALIGN - 1 ) );
169 adjacent = userptr_add ( low->data, padded_len );
171 /* Search for adjacent image */
172 for_each_image ( high ) {
174 /* If we have found the adjacent image, swap and exit */
175 if ( high->data == adjacent ) {
176 initrd_swap ( low, high, free, free_len );
180 /* Stop search if all remaining potential
181 * adjacent images are already in the correct
189 /* Nothing swapped */
194 * Dump initrd locations (for debug)
197 static void initrd_dump ( void ) {
198 struct image *initrd;
200 /* Do nothing unless debugging is enabled */
204 /* Dump initrd locations */
205 for_each_image ( initrd ) {
206 DBGC ( &images, "INITRD %s at [%#08lx,%#08lx)\n",
207 initrd->name, user_to_phys ( initrd->data, 0 ),
208 user_to_phys ( initrd->data, initrd->len ) );
209 DBGC2_MD5A ( &images, user_to_phys ( initrd->data, 0 ),
210 user_to_virt ( initrd->data, 0 ), initrd->len );
215 * Reshuffle initrds into desired order at top of memory
217 * @v bottom Lowest address available for initrds
219 * After this function returns, the initrds have been rearranged in
220 * memory and the external heap structures will have been corrupted.
221 * Reshuffling must therefore take place immediately prior to jumping
222 * to the loaded OS kernel; no further execution within iPXE is
225 void initrd_reshuffle ( userptr_t bottom ) {
231 /* Calculate limits of available space for initrds */
233 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
234 bottom = initrd_bottom;
237 DBGC ( &images, "INITRD region [%#08lx,%#08lx)\n",
238 user_to_phys ( bottom, 0 ), user_to_phys ( top, 0 ) );
241 /* Squash initrds as high as possible in memory */
242 used = initrd_squash_high ( top );
244 /* Calculate available free space */
246 free_len = userptr_sub ( used, free );
248 /* Bubble-sort initrds into desired order */
249 while ( initrd_swap_any ( free, free_len ) ) {}
256 * Check that there is enough space to reshuffle initrds
258 * @v len Total length of initrds (including padding)
259 * @v bottom Lowest address available for initrds
260 * @ret rc Return status code
262 int initrd_reshuffle_check ( size_t len, userptr_t bottom ) {
266 /* Calculate limits of available space for initrds */
268 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
269 bottom = initrd_bottom;
270 available = userptr_sub ( top, bottom );
272 /* Allow for a sensible minimum amount of free space */
273 len += INITRD_MIN_FREE_LEN;
275 /* Check for available space */
276 return ( ( len < available ) ? 0 : -ENOBUFS );
280 * initrd startup function
283 static void initrd_startup ( void ) {
286 /* Record largest memory block available. Do this after any
287 * allocations made during driver startup (e.g. large host
288 * memory blocks for Infiniband devices, which may still be in
289 * use at the time of rearranging if a SAN device is hooked)
290 * but before any allocations for downloaded images (which we
291 * can safely reuse when rearranging).
293 len = largest_memblock ( &initrd_bottom );
294 initrd_top = userptr_add ( initrd_bottom, len );
297 /** initrd startup function */
298 struct startup_fn startup_initrd __startup_fn ( STARTUP_LATE ) = {
299 .startup = initrd_startup,