iPXE
bzimage.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 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 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 /**
00027  * @file
00028  *
00029  * Linux bzImage image format
00030  *
00031  */
00032 
00033 #include <stdint.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <errno.h>
00037 #include <assert.h>
00038 #include <realmode.h>
00039 #include <bzimage.h>
00040 #include <initrd.h>
00041 #include <ipxe/uaccess.h>
00042 #include <ipxe/image.h>
00043 #include <ipxe/segment.h>
00044 #include <ipxe/init.h>
00045 #include <ipxe/cpio.h>
00046 #include <ipxe/features.h>
00047 
00048 FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
00049 
00050 /**
00051  * bzImage context
00052  */
00053 struct bzimage_context {
00054         /** Boot protocol version */
00055         unsigned int version;
00056         /** Real-mode kernel portion load segment address */
00057         unsigned int rm_kernel_seg;
00058         /** Real-mode kernel portion load address */
00059         userptr_t rm_kernel;
00060         /** Real-mode kernel portion file size */
00061         size_t rm_filesz;
00062         /** Real-mode heap top (offset from rm_kernel) */
00063         size_t rm_heap;
00064         /** Command line (offset from rm_kernel) */
00065         size_t rm_cmdline;
00066         /** Command line maximum length */
00067         size_t cmdline_size;
00068         /** Real-mode kernel portion total memory size */
00069         size_t rm_memsz;
00070         /** Non-real-mode kernel portion load address */
00071         userptr_t pm_kernel;
00072         /** Non-real-mode kernel portion file and memory size */
00073         size_t pm_sz;
00074         /** Video mode */
00075         unsigned int vid_mode;
00076         /** Memory limit */
00077         uint64_t mem_limit;
00078         /** Initrd address */
00079         physaddr_t ramdisk_image;
00080         /** Initrd size */
00081         physaddr_t ramdisk_size;
00082 
00083         /** Command line magic block */
00084         struct bzimage_cmdline cmdline_magic;
00085         /** bzImage header */
00086         struct bzimage_header bzhdr;
00087 };
00088 
00089 /**
00090  * Parse bzImage header
00091  *
00092  * @v image             bzImage file
00093  * @v bzimg             bzImage context
00094  * @v src               bzImage to parse
00095  * @ret rc              Return status code
00096  */
00097 static int bzimage_parse_header ( struct image *image,
00098                                   struct bzimage_context *bzimg,
00099                                   userptr_t src ) {
00100         unsigned int syssize;
00101         int is_bzimage;
00102 
00103         /* Sanity check */
00104         if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
00105                 DBGC ( image, "bzImage %p too short for kernel header\n",
00106                        image );
00107                 return -ENOEXEC;
00108         }
00109 
00110         /* Read in header structures */
00111         memset ( bzimg, 0, sizeof ( *bzimg ) );
00112         copy_from_user ( &bzimg->cmdline_magic, src, BZI_CMDLINE_OFFSET,
00113                          sizeof ( bzimg->cmdline_magic ) );
00114         copy_from_user ( &bzimg->bzhdr, src, BZI_HDR_OFFSET,
00115                          sizeof ( bzimg->bzhdr ) );
00116 
00117         /* Calculate size of real-mode portion */
00118         bzimg->rm_filesz = ( ( ( bzimg->bzhdr.setup_sects ?
00119                                  bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9 );
00120         if ( bzimg->rm_filesz > image->len ) {
00121                 DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
00122                        image, bzimg->rm_filesz );
00123                 return -ENOEXEC;
00124         }
00125         bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
00126 
00127         /* Calculate size of protected-mode portion */
00128         bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
00129         syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
00130 
00131         /* Check for signatures and determine version */
00132         if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
00133                 DBGC ( image, "bzImage %p missing 55AA signature\n", image );
00134                 return -ENOEXEC;
00135         }
00136         if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
00137                 /* 2.00+ */
00138                 bzimg->version = bzimg->bzhdr.version;
00139         } else {
00140                 /* Pre-2.00.  Check that the syssize field is correct,
00141                  * as a guard against accepting arbitrary binary data,
00142                  * since the 55AA check is pretty lax.  Note that the
00143                  * syssize field is unreliable for protocols between
00144                  * 2.00 and 2.03 inclusive, so we should not always
00145                  * check this field.
00146                  */
00147                 bzimg->version = 0x0100;
00148                 if ( bzimg->bzhdr.syssize != syssize ) {
00149                         DBGC ( image, "bzImage %p bad syssize %x (expected "
00150                                "%x)\n", image, bzimg->bzhdr.syssize, syssize );
00151                         return -ENOEXEC;
00152                 }
00153         }
00154 
00155         /* Determine image type */
00156         is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
00157                        ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
00158 
00159         /* Calculate load address of real-mode portion */
00160         bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
00161         bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
00162 
00163         /* Allow space for the stack and heap */
00164         bzimg->rm_memsz += BZI_STACK_SIZE;
00165         bzimg->rm_heap = bzimg->rm_memsz;
00166 
00167         /* Allow space for the command line */
00168         bzimg->rm_cmdline = bzimg->rm_memsz;
00169         bzimg->rm_memsz += BZI_CMDLINE_SIZE;
00170 
00171         /* Calculate load address of protected-mode portion */
00172         bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
00173                                         : BZI_LOAD_LOW_ADDR );
00174 
00175         /* Extract video mode */
00176         bzimg->vid_mode = bzimg->bzhdr.vid_mode;
00177 
00178         /* Extract memory limit */
00179         bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
00180                              bzimg->bzhdr.initrd_addr_max : BZI_INITRD_MAX );
00181 
00182         /* Extract command line size */
00183         bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
00184                                 bzimg->bzhdr.cmdline_size : BZI_CMDLINE_SIZE );
00185 
00186         DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
00187                "cmdlen %zd\n", image, bzimg->version,
00188                user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
00189                user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
00190                bzimg->cmdline_size );
00191 
00192         return 0;
00193 }
00194 
00195 /**
00196  * Update bzImage header in loaded kernel
00197  *
00198  * @v image             bzImage file
00199  * @v bzimg             bzImage context
00200  * @v dst               bzImage to update
00201  */
00202 static void bzimage_update_header ( struct image *image,
00203                                     struct bzimage_context *bzimg,
00204                                     userptr_t dst ) {
00205 
00206         /* Set loader type */
00207         if ( bzimg->version >= 0x0200 )
00208                 bzimg->bzhdr.type_of_loader = BZI_LOADER_TYPE_IPXE;
00209 
00210         /* Set heap end pointer */
00211         if ( bzimg->version >= 0x0201 ) {
00212                 bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
00213                 bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
00214         }
00215 
00216         /* Set command line */
00217         if ( bzimg->version >= 0x0202 ) {
00218                 bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
00219                                                            bzimg->rm_cmdline );
00220         } else {
00221                 bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
00222                 bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
00223                 if ( bzimg->version >= 0x0200 )
00224                         bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
00225         }
00226 
00227         /* Set video mode */
00228         bzimg->bzhdr.vid_mode = bzimg->vid_mode;
00229 
00230         /* Set initrd address */
00231         if ( bzimg->version >= 0x0200 ) {
00232                 bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
00233                 bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
00234         }
00235 
00236         /* Write out header structures */
00237         copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
00238                        sizeof ( bzimg->cmdline_magic ) );
00239         copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
00240                        sizeof ( bzimg->bzhdr ) );
00241 
00242         DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
00243 }
00244 
00245 /**
00246  * Parse kernel command line for bootloader parameters
00247  *
00248  * @v image             bzImage file
00249  * @v bzimg             bzImage context
00250  * @v cmdline           Kernel command line
00251  * @ret rc              Return status code
00252  */
00253 static int bzimage_parse_cmdline ( struct image *image,
00254                                    struct bzimage_context *bzimg,
00255                                    const char *cmdline ) {
00256         char *vga;
00257         char *mem;
00258 
00259         /* Look for "vga=" */
00260         if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
00261                 vga += 4;
00262                 if ( strcmp ( vga, "normal" ) == 0 ) {
00263                         bzimg->vid_mode = BZI_VID_MODE_NORMAL;
00264                 } else if ( strcmp ( vga, "ext" ) == 0 ) {
00265                         bzimg->vid_mode = BZI_VID_MODE_EXT;
00266                 } else if ( strcmp ( vga, "ask" ) == 0 ) {
00267                         bzimg->vid_mode = BZI_VID_MODE_ASK;
00268                 } else {
00269                         bzimg->vid_mode = strtoul ( vga, &vga, 0 );
00270                         if ( *vga && ( *vga != ' ' ) ) {
00271                                 DBGC ( image, "bzImage %p strange \"vga=\""
00272                                        "terminator '%c'\n", image, *vga );
00273                         }
00274                 }
00275         }
00276 
00277         /* Look for "mem=" */
00278         if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
00279                 mem += 4;
00280                 bzimg->mem_limit = strtoul ( mem, &mem, 0 );
00281                 switch ( *mem ) {
00282                 case 'G':
00283                 case 'g':
00284                         bzimg->mem_limit <<= 10;
00285                         /* Fall through */
00286                 case 'M':
00287                 case 'm':
00288                         bzimg->mem_limit <<= 10;
00289                         /* Fall through */
00290                 case 'K':
00291                 case 'k':
00292                         bzimg->mem_limit <<= 10;
00293                         break;
00294                 case '\0':
00295                 case ' ':
00296                         break;
00297                 default:
00298                         DBGC ( image, "bzImage %p strange \"mem=\" "
00299                                "terminator '%c'\n", image, *mem );
00300                         break;
00301                 }
00302                 bzimg->mem_limit -= 1;
00303         }
00304 
00305         return 0;
00306 }
00307 
00308 /**
00309  * Set command line
00310  *
00311  * @v image             bzImage image
00312  * @v bzimg             bzImage context
00313  * @v cmdline           Kernel command line
00314  */
00315 static void bzimage_set_cmdline ( struct image *image,
00316                                   struct bzimage_context *bzimg,
00317                                   const char *cmdline ) {
00318         size_t cmdline_len;
00319 
00320         /* Copy command line down to real-mode portion */
00321         cmdline_len = ( strlen ( cmdline ) + 1 );
00322         if ( cmdline_len > bzimg->cmdline_size )
00323                 cmdline_len = bzimg->cmdline_size;
00324         copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
00325                        cmdline, cmdline_len );
00326         DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
00327 }
00328 
00329 /**
00330  * Parse standalone image command line for cpio parameters
00331  *
00332  * @v image             bzImage file
00333  * @v cpio              CPIO header
00334  * @v cmdline           Command line
00335  */
00336 static void bzimage_parse_cpio_cmdline ( struct image *image,
00337                                          struct cpio_header *cpio,
00338                                          const char *cmdline ) {
00339         char *arg;
00340         char *end;
00341         unsigned int mode;
00342 
00343         /* Look for "mode=" */
00344         if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
00345                 arg += 5;
00346                 mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
00347                 if ( *end && ( *end != ' ' ) ) {
00348                         DBGC ( image, "bzImage %p strange \"mode=\""
00349                                "terminator '%c'\n", image, *end );
00350                 }
00351                 cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
00352         }
00353 }
00354 
00355 /**
00356  * Align initrd length
00357  *
00358  * @v len               Length
00359  * @ret len             Length rounded up to INITRD_ALIGN
00360  */
00361 static inline size_t bzimage_align ( size_t len ) {
00362 
00363         return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
00364 }
00365 
00366 /**
00367  * Load initrd
00368  *
00369  * @v image             bzImage image
00370  * @v initrd            initrd image
00371  * @v address           Address at which to load, or UNULL
00372  * @ret len             Length of loaded image, excluding zero-padding
00373  */
00374 static size_t bzimage_load_initrd ( struct image *image,
00375                                     struct image *initrd,
00376                                     userptr_t address ) {
00377         char *filename = initrd->cmdline;
00378         char *cmdline;
00379         struct cpio_header cpio;
00380         size_t offset;
00381         size_t name_len;
00382         size_t pad_len;
00383 
00384         /* Do not include kernel image itself as an initrd */
00385         if ( initrd == image )
00386                 return 0;
00387 
00388         /* Create cpio header for non-prebuilt images */
00389         if ( filename && filename[0] ) {
00390                 cmdline = strchr ( filename, ' ' );
00391                 name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
00392                                : strlen ( filename ) ) + 1 /* NUL */ );
00393                 memset ( &cpio, '0', sizeof ( cpio ) );
00394                 memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
00395                 cpio_set_field ( cpio.c_mode, 0100644 );
00396                 cpio_set_field ( cpio.c_nlink, 1 );
00397                 cpio_set_field ( cpio.c_filesize, initrd->len );
00398                 cpio_set_field ( cpio.c_namesize, name_len );
00399                 if ( cmdline ) {
00400                         bzimage_parse_cpio_cmdline ( image, &cpio,
00401                                                      ( cmdline + 1 /* ' ' */ ));
00402                 }
00403                 offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
00404         } else {
00405                 offset = 0;
00406                 name_len = 0;
00407         }
00408 
00409         /* Copy in initrd image body (and cpio header if applicable) */
00410         if ( address ) {
00411                 memmove_user ( address, offset, initrd->data, 0, initrd->len );
00412                 if ( offset ) {
00413                         memset_user ( address, 0, 0, offset );
00414                         copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
00415                         copy_to_user ( address, sizeof ( cpio ), filename,
00416                                        ( name_len - 1 /* NUL (or space) */ ) );
00417                 }
00418                 DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
00419                        "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
00420                        user_to_phys ( address, offset ),
00421                        user_to_phys ( address, ( offset + initrd->len ) ),
00422                        ( filename ? " " : "" ), ( filename ? filename : "" ) );
00423                 DBGC2_MD5A ( image, user_to_phys ( address, offset ),
00424                              user_to_virt ( address, offset ), initrd->len );
00425         }
00426         offset += initrd->len;
00427 
00428         /* Zero-pad to next INITRD_ALIGN boundary */
00429         pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
00430         if ( address )
00431                 memset_user ( address, offset, 0, pad_len );
00432 
00433         return offset;
00434 }
00435 
00436 /**
00437  * Check that initrds can be loaded
00438  *
00439  * @v image             bzImage image
00440  * @v bzimg             bzImage context
00441  * @ret rc              Return status code
00442  */
00443 static int bzimage_check_initrds ( struct image *image,
00444                                    struct bzimage_context *bzimg ) {
00445         struct image *initrd;
00446         userptr_t bottom;
00447         size_t len = 0;
00448         int rc;
00449 
00450         /* Calculate total loaded length of initrds */
00451         for_each_image ( initrd ) {
00452 
00453                 /* Skip kernel */
00454                 if ( initrd == image )
00455                         continue;
00456 
00457                 /* Calculate length */
00458                 len += bzimage_load_initrd ( image, initrd, UNULL );
00459                 len = bzimage_align ( len );
00460 
00461                 DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
00462                        image, initrd, user_to_phys ( initrd->data, 0 ),
00463                        user_to_phys ( initrd->data, initrd->len ),
00464                        ( initrd->cmdline ? " " : "" ),
00465                        ( initrd->cmdline ? initrd->cmdline : "" ) );
00466                 DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
00467                              user_to_virt ( initrd->data, 0 ), initrd->len );
00468         }
00469 
00470         /* Calculate lowest usable address */
00471         bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
00472 
00473         /* Check that total length fits within space available for
00474          * reshuffling.  This is a conservative check, since CPIO
00475          * headers are not present during reshuffling, but this
00476          * doesn't hurt and keeps the code simple.
00477          */
00478         if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
00479                 DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
00480                        image, strerror ( rc ) );
00481                 return rc;
00482         }
00483 
00484         /* Check that total length fits within kernel's memory limit */
00485         if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
00486                 DBGC ( image, "bzImage %p not enough space for initrds\n",
00487                        image );
00488                 return -ENOBUFS;
00489         }
00490 
00491         return 0;
00492 }
00493 
00494 /**
00495  * Load initrds, if any
00496  *
00497  * @v image             bzImage image
00498  * @v bzimg             bzImage context
00499  */
00500 static void bzimage_load_initrds ( struct image *image,
00501                                    struct bzimage_context *bzimg ) {
00502         struct image *initrd;
00503         struct image *highest = NULL;
00504         struct image *other;
00505         userptr_t top;
00506         userptr_t dest;
00507         size_t offset;
00508         size_t len;
00509 
00510         /* Reshuffle initrds into desired order */
00511         initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
00512 
00513         /* Find highest initrd */
00514         for_each_image ( initrd ) {
00515                 if ( ( highest == NULL ) ||
00516                      ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
00517                         highest = initrd;
00518                 }
00519         }
00520 
00521         /* Do nothing if there are no initrds */
00522         if ( ! highest )
00523                 return;
00524 
00525         /* Find highest usable address */
00526         top = userptr_add ( highest->data, bzimage_align ( highest->len ) );
00527         if ( user_to_phys ( top, -1 ) > bzimg->mem_limit ) {
00528                 top = phys_to_user ( ( bzimg->mem_limit + 1 ) &
00529                                      ~( INITRD_ALIGN - 1 ) );
00530         }
00531         DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
00532                image, user_to_phys ( top, -1 ) );
00533 
00534         /* Load initrds in order */
00535         for_each_image ( initrd ) {
00536 
00537                 /* Calculate cumulative length of following
00538                  * initrds (including padding).
00539                  */
00540                 offset = 0;
00541                 for_each_image ( other ) {
00542                         if ( other == initrd )
00543                                 offset = 0;
00544                         offset += bzimage_load_initrd ( image, other, UNULL );
00545                         offset = bzimage_align ( offset );
00546                 }
00547 
00548                 /* Load initrd at this address */
00549                 dest = userptr_add ( top, -offset );
00550                 len = bzimage_load_initrd ( image, initrd, dest );
00551 
00552                 /* Record initrd location */
00553                 if ( ! bzimg->ramdisk_image )
00554                         bzimg->ramdisk_image = user_to_phys ( dest, 0 );
00555                 bzimg->ramdisk_size = ( user_to_phys ( dest, len ) -
00556                                         bzimg->ramdisk_image );
00557         }
00558         DBGC ( image, "bzImage %p initrds at [%#08lx,%#08lx)\n",
00559                image, bzimg->ramdisk_image,
00560                ( bzimg->ramdisk_image + bzimg->ramdisk_size ) );
00561 }
00562 
00563 /**
00564  * Execute bzImage image
00565  *
00566  * @v image             bzImage image
00567  * @ret rc              Return status code
00568  */
00569 static int bzimage_exec ( struct image *image ) {
00570         struct bzimage_context bzimg;
00571         const char *cmdline = ( image->cmdline ? image->cmdline : "" );
00572         int rc;
00573 
00574         /* Read and parse header from image */
00575         if ( ( rc = bzimage_parse_header ( image, &bzimg,
00576                                            image->data ) ) != 0 )
00577                 return rc;
00578 
00579         /* Prepare segments */
00580         if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
00581                                    bzimg.rm_memsz ) ) != 0 ) {
00582                 DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
00583                        image, strerror ( rc ) );
00584                 return rc;
00585         }
00586         if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
00587                                    bzimg.pm_sz ) ) != 0 ) {
00588                 DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
00589                        image, strerror ( rc ) );
00590                 return rc;
00591         }
00592 
00593         /* Parse command line for bootloader parameters */
00594         if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
00595                 return rc;
00596 
00597         /* Check that initrds can be loaded */
00598         if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
00599                 return rc;
00600 
00601         /* Remove kernel from image list (without invalidating image pointer) */
00602         unregister_image ( image_get ( image ) );
00603 
00604         /* Load segments */
00605         memcpy_user ( bzimg.rm_kernel, 0, image->data,
00606                       0, bzimg.rm_filesz );
00607         memcpy_user ( bzimg.pm_kernel, 0, image->data,
00608                       bzimg.rm_filesz, bzimg.pm_sz );
00609 
00610         /* Store command line */
00611         bzimage_set_cmdline ( image, &bzimg, cmdline );
00612 
00613         /* Prepare for exiting.  Must do this before loading initrds,
00614          * since loading the initrds will corrupt the external heap.
00615          */
00616         shutdown_boot();
00617 
00618         /* Load any initrds */
00619         bzimage_load_initrds ( image, &bzimg );
00620 
00621         /* Update kernel header */
00622         bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
00623 
00624         DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
00625                "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
00626                bzimg.rm_kernel_seg, bzimg.rm_heap );
00627 
00628         /* Jump to the kernel */
00629         __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
00630                                            "movw %w0, %%es\n\t"
00631                                            "movw %w0, %%fs\n\t"
00632                                            "movw %w0, %%gs\n\t"
00633                                            "movw %w0, %%ss\n\t"
00634                                            "movw %w1, %%sp\n\t"
00635                                            "pushw %w2\n\t"
00636                                            "pushw $0\n\t"
00637                                            "lret\n\t" )
00638                                : : "R" ( bzimg.rm_kernel_seg ),
00639                                    "R" ( bzimg.rm_heap ),
00640                                    "R" ( bzimg.rm_kernel_seg + 0x20 ) );
00641 
00642         /* There is no way for the image to return, since we provide
00643          * no return address.
00644          */
00645         assert ( 0 );
00646 
00647         return -ECANCELED; /* -EIMPOSSIBLE */
00648 }
00649 
00650 /**
00651  * Probe bzImage image
00652  *
00653  * @v image             bzImage file
00654  * @ret rc              Return status code
00655  */
00656 int bzimage_probe ( struct image *image ) {
00657         struct bzimage_context bzimg;
00658         int rc;
00659 
00660         /* Read and parse header from image */
00661         if ( ( rc = bzimage_parse_header ( image, &bzimg,
00662                                            image->data ) ) != 0 )
00663                 return rc;
00664 
00665         return 0;
00666 }
00667 
00668 /** Linux bzImage image type */
00669 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
00670         .name = "bzImage",
00671         .probe = bzimage_probe,
00672         .exec = bzimage_exec,
00673 };