iPXE
initrd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <errno.h>
00027 #include <initrd.h>
00028 #include <ipxe/image.h>
00029 #include <ipxe/uaccess.h>
00030 #include <ipxe/init.h>
00031 #include <ipxe/memblock.h>
00032 
00033 /** @file
00034  *
00035  * Initial ramdisk (initrd) reshuffling
00036  *
00037  */
00038 
00039 /** Maximum address available for initrd */
00040 userptr_t initrd_top;
00041 
00042 /** Minimum address available for initrd */
00043 userptr_t initrd_bottom;
00044 
00045 /**
00046  * Squash initrds as high as possible in memory
00047  *
00048  * @v top               Highest possible address
00049  * @ret used            Lowest address used by initrds
00050  */
00051 static userptr_t initrd_squash_high ( userptr_t top ) {
00052         userptr_t current = top;
00053         struct image *initrd;
00054         struct image *highest;
00055         size_t len;
00056 
00057         /* Squash up any initrds already within or below the region */
00058         while ( 1 ) {
00059 
00060                 /* Find the highest image not yet in its final position */
00061                 highest = NULL;
00062                 for_each_image ( initrd ) {
00063                         if ( ( userptr_sub ( initrd->data, current ) < 0 ) &&
00064                              ( ( highest == NULL ) ||
00065                                ( userptr_sub ( initrd->data,
00066                                                highest->data ) > 0 ) ) ) {
00067                                 highest = initrd;
00068                         }
00069                 }
00070                 if ( ! highest )
00071                         break;
00072 
00073                 /* Move this image to its final position */
00074                 len = ( ( highest->len + INITRD_ALIGN - 1 ) &
00075                         ~( INITRD_ALIGN - 1 ) );
00076                 current = userptr_sub ( current, len );
00077                 DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
00078                        "[%#08lx,%#08lx)\n", highest->name,
00079                        user_to_phys ( highest->data, 0 ),
00080                        user_to_phys ( highest->data, highest->len ),
00081                        user_to_phys ( current, 0 ),
00082                        user_to_phys ( current, highest->len ) );
00083                 memmove_user ( current, 0, highest->data, 0, highest->len );
00084                 highest->data = current;
00085         }
00086 
00087         /* Copy any remaining initrds (e.g. embedded images) to the region */
00088         for_each_image ( initrd ) {
00089                 if ( userptr_sub ( initrd->data, top ) >= 0 ) {
00090                         len = ( ( initrd->len + INITRD_ALIGN - 1 ) &
00091                                 ~( INITRD_ALIGN - 1 ) );
00092                         current = userptr_sub ( current, len );
00093                         DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
00094                                "[%#08lx,%#08lx)\n", initrd->name,
00095                                user_to_phys ( initrd->data, 0 ),
00096                                user_to_phys ( initrd->data, initrd->len ),
00097                                user_to_phys ( current, 0 ),
00098                                user_to_phys ( current, initrd->len ) );
00099                         memcpy_user ( current, 0, initrd->data, 0,
00100                                       initrd->len );
00101                         initrd->data = current;
00102                 }
00103         }
00104 
00105         return current;
00106 }
00107 
00108 /**
00109  * Swap position of two adjacent initrds
00110  *
00111  * @v low               Lower initrd
00112  * @v high              Higher initrd
00113  * @v free              Free space
00114  * @v free_len          Length of free space
00115  */
00116 static void initrd_swap ( struct image *low, struct image *high,
00117                           userptr_t free, size_t free_len ) {
00118         size_t len = 0;
00119         size_t frag_len;
00120         size_t new_len;
00121 
00122         DBGC ( &images, "INITRD swapping %s [%#08lx,%#08lx)<->[%#08lx,%#08lx) "
00123                "%s\n", low->name, user_to_phys ( low->data, 0 ),
00124                user_to_phys ( low->data, low->len ),
00125                user_to_phys ( high->data, 0 ),
00126                user_to_phys ( high->data, high->len ), high->name );
00127 
00128         /* Round down length of free space */
00129         free_len &= ~( INITRD_ALIGN - 1 );
00130         assert ( free_len > 0 );
00131 
00132         /* Swap image data */
00133         while ( len < high->len ) {
00134 
00135                 /* Calculate maximum fragment length */
00136                 frag_len = ( high->len - len );
00137                 if ( frag_len > free_len )
00138                         frag_len = free_len;
00139                 new_len = ( ( len + frag_len + INITRD_ALIGN - 1 ) &
00140                             ~( INITRD_ALIGN - 1 ) );
00141 
00142                 /* Swap fragments */
00143                 memcpy_user ( free, 0, high->data, len, frag_len );
00144                 memmove_user ( low->data, new_len, low->data, len, low->len );
00145                 memcpy_user ( low->data, len, free, 0, frag_len );
00146                 len = new_len;
00147         }
00148 
00149         /* Adjust data pointers */
00150         high->data = low->data;
00151         low->data = userptr_add ( low->data, len );
00152 }
00153 
00154 /**
00155  * Swap position of any two adjacent initrds not currently in the correct order
00156  *
00157  * @v free              Free space
00158  * @v free_len          Length of free space
00159  * @ret swapped         A pair of initrds was swapped
00160  */
00161 static int initrd_swap_any ( userptr_t free, size_t free_len ) {
00162         struct image *low;
00163         struct image *high;
00164         size_t padded_len;
00165         userptr_t adjacent;
00166 
00167         /* Find any pair of initrds that can be swapped */
00168         for_each_image ( low ) {
00169 
00170                 /* Calculate location of adjacent image (if any) */
00171                 padded_len = ( ( low->len + INITRD_ALIGN - 1 ) &
00172                                ~( INITRD_ALIGN - 1 ) );
00173                 adjacent = userptr_add ( low->data, padded_len );
00174 
00175                 /* Search for adjacent image */
00176                 for_each_image ( high ) {
00177 
00178                         /* If we have found the adjacent image, swap and exit */
00179                         if ( high->data == adjacent ) {
00180                                 initrd_swap ( low, high, free, free_len );
00181                                 return 1;
00182                         }
00183 
00184                         /* Stop search if all remaining potential
00185                          * adjacent images are already in the correct
00186                          * order.
00187                          */
00188                         if ( high == low )
00189                                 break;
00190                 }
00191         }
00192 
00193         /* Nothing swapped */
00194         return 0;
00195 }
00196 
00197 /**
00198  * Dump initrd locations (for debug)
00199  *
00200  */
00201 static void initrd_dump ( void ) {
00202         struct image *initrd;
00203 
00204         /* Do nothing unless debugging is enabled */
00205         if ( ! DBG_LOG )
00206                 return;
00207 
00208         /* Dump initrd locations */
00209         for_each_image ( initrd ) {
00210                 DBGC ( &images, "INITRD %s at [%#08lx,%#08lx)\n",
00211                        initrd->name, user_to_phys ( initrd->data, 0 ),
00212                        user_to_phys ( initrd->data, initrd->len ) );
00213                 DBGC2_MD5A ( &images, user_to_phys ( initrd->data, 0 ),
00214                              user_to_virt ( initrd->data, 0 ), initrd->len );
00215         }
00216 }
00217 
00218 /**
00219  * Reshuffle initrds into desired order at top of memory
00220  *
00221  * @v bottom            Lowest address available for initrds
00222  *
00223  * After this function returns, the initrds have been rearranged in
00224  * memory and the external heap structures will have been corrupted.
00225  * Reshuffling must therefore take place immediately prior to jumping
00226  * to the loaded OS kernel; no further execution within iPXE is
00227  * permitted.
00228  */
00229 void initrd_reshuffle ( userptr_t bottom ) {
00230         userptr_t top;
00231         userptr_t used;
00232         userptr_t free;
00233         size_t free_len;
00234 
00235         /* Calculate limits of available space for initrds */
00236         top = initrd_top;
00237         if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
00238                 bottom = initrd_bottom;
00239 
00240         /* Debug */
00241         DBGC ( &images, "INITRD region [%#08lx,%#08lx)\n",
00242                user_to_phys ( bottom, 0 ), user_to_phys ( top, 0 ) );
00243         initrd_dump();
00244 
00245         /* Squash initrds as high as possible in memory */
00246         used = initrd_squash_high ( top );
00247 
00248         /* Calculate available free space */
00249         free = bottom;
00250         free_len = userptr_sub ( used, free );
00251 
00252         /* Bubble-sort initrds into desired order */
00253         while ( initrd_swap_any ( free, free_len ) ) {}
00254 
00255         /* Debug */
00256         initrd_dump();
00257 }
00258 
00259 /**
00260  * Check that there is enough space to reshuffle initrds
00261  *
00262  * @v len               Total length of initrds (including padding)
00263  * @v bottom            Lowest address available for initrds
00264  * @ret rc              Return status code
00265  */
00266 int initrd_reshuffle_check ( size_t len, userptr_t bottom ) {
00267         userptr_t top;
00268         size_t available;
00269 
00270         /* Calculate limits of available space for initrds */
00271         top = initrd_top;
00272         if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
00273                 bottom = initrd_bottom;
00274         available = userptr_sub ( top, bottom );
00275 
00276         /* Allow for a sensible minimum amount of free space */
00277         len += INITRD_MIN_FREE_LEN;
00278 
00279         /* Check for available space */
00280         return ( ( len < available ) ? 0 : -ENOBUFS );
00281 }
00282 
00283 /**
00284  * initrd startup function
00285  *
00286  */
00287 static void initrd_startup ( void ) {
00288         size_t len;
00289 
00290         /* Record largest memory block available.  Do this after any
00291          * allocations made during driver startup (e.g. large host
00292          * memory blocks for Infiniband devices, which may still be in
00293          * use at the time of rearranging if a SAN device is hooked)
00294          * but before any allocations for downloaded images (which we
00295          * can safely reuse when rearranging).
00296          */
00297         len = largest_memblock ( &initrd_bottom );
00298         initrd_top = userptr_add ( initrd_bottom, len );
00299 }
00300 
00301 /** initrd startup function */
00302 struct startup_fn startup_initrd __startup_fn ( STARTUP_LATE ) = {
00303         .startup = initrd_startup,
00304 };