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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /**
00022  * @file
00023  *
00024  * Linux bzImage image format
00025  *
00026  */
00027 
00028 #include <stdint.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 #include <assert.h>
00033 #include <realmode.h>
00034 #include <bzimage.h>
00035 #include <gpxe/uaccess.h>
00036 #include <gpxe/image.h>
00037 #include <gpxe/segment.h>
00038 #include <gpxe/init.h>
00039 #include <gpxe/cpio.h>
00040 #include <gpxe/features.h>
00041 
00042 FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
00043 
00044 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL );
00045 
00046 /**
00047  * bzImage context
00048  */
00049 struct bzimage_context {
00050         /** Boot protocol version */
00051         unsigned int version;
00052         /** Real-mode kernel portion load segment address */
00053         unsigned int rm_kernel_seg;
00054         /** Real-mode kernel portion load address */
00055         userptr_t rm_kernel;
00056         /** Real-mode kernel portion file size */
00057         size_t rm_filesz;
00058         /** Real-mode heap top (offset from rm_kernel) */
00059         size_t rm_heap;
00060         /** Command line (offset from rm_kernel) */
00061         size_t rm_cmdline;
00062         /** Command line maximum length */
00063         size_t cmdline_size;
00064         /** Real-mode kernel portion total memory size */
00065         size_t rm_memsz;
00066         /** Non-real-mode kernel portion load address */
00067         userptr_t pm_kernel;
00068         /** Non-real-mode kernel portion file and memory size */
00069         size_t pm_sz;
00070         /** Video mode */
00071         unsigned int vid_mode;
00072         /** Memory limit */
00073         uint64_t mem_limit;
00074         /** Initrd address */
00075         physaddr_t ramdisk_image;
00076         /** Initrd size */
00077         physaddr_t ramdisk_size;
00078 
00079         /** Command line magic block */
00080         struct bzimage_cmdline cmdline_magic;
00081         /** bzImage header */
00082         struct bzimage_header bzhdr;
00083 };
00084 
00085 /**
00086  * Parse bzImage header
00087  *
00088  * @v image             bzImage file
00089  * @v bzimg             bzImage context
00090  * @v src               bzImage to parse
00091  * @ret rc              Return status code
00092  */
00093 static int bzimage_parse_header ( struct image *image,
00094                                   struct bzimage_context *bzimg,
00095                                   userptr_t src ) {
00096         unsigned int syssize;
00097         int is_bzimage;
00098 
00099         /* Sanity check */
00100         if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
00101                 DBGC ( image, "bzImage %p too short for kernel header\n",
00102                        image );
00103                 return -ENOEXEC;
00104         }
00105 
00106         /* Read in header structures */
00107         memset ( bzimg, 0, sizeof ( *bzimg ) );
00108         copy_from_user ( &bzimg->cmdline_magic, src, BZI_CMDLINE_OFFSET,
00109                          sizeof ( bzimg->cmdline_magic ) );
00110         copy_from_user ( &bzimg->bzhdr, src, BZI_HDR_OFFSET,
00111                          sizeof ( bzimg->bzhdr ) );
00112 
00113         /* Calculate size of real-mode portion */
00114         bzimg->rm_filesz =
00115                 ( ( bzimg->bzhdr.setup_sects ? bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9;
00116         if ( bzimg->rm_filesz > image->len ) {
00117                 DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
00118                        image, bzimg->rm_filesz );
00119                 return -ENOEXEC;
00120         }
00121         bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
00122 
00123         /* Calculate size of protected-mode portion */
00124         bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
00125         syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
00126 
00127         /* Check for signatures and determine version */
00128         if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
00129                 DBGC ( image, "bzImage %p missing 55AA signature\n", image );
00130                 return -ENOEXEC;
00131         }
00132         if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
00133                 /* 2.00+ */
00134                 bzimg->version = bzimg->bzhdr.version;
00135         } else {
00136                 /* Pre-2.00.  Check that the syssize field is correct,
00137                  * as a guard against accepting arbitrary binary data,
00138                  * since the 55AA check is pretty lax.  Note that the
00139                  * syssize field is unreliable for protocols between
00140                  * 2.00 and 2.03 inclusive, so we should not always
00141                  * check this field.
00142                  */
00143                 bzimg->version = 0x0100;
00144                 if ( bzimg->bzhdr.syssize != syssize ) {
00145                         DBGC ( image, "bzImage %p bad syssize %x (expected "
00146                                "%x)\n", image, bzimg->bzhdr.syssize, syssize );
00147                         return -ENOEXEC;
00148                 }
00149         }
00150 
00151         /* Determine image type */
00152         is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
00153                        ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
00154 
00155         /* Calculate load address of real-mode portion */
00156         bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
00157         bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
00158 
00159         /* Allow space for the stack and heap */
00160         bzimg->rm_memsz += BZI_STACK_SIZE;
00161         bzimg->rm_heap = bzimg->rm_memsz;
00162 
00163         /* Allow space for the command line */
00164         bzimg->rm_cmdline = bzimg->rm_memsz;
00165         bzimg->rm_memsz += BZI_CMDLINE_SIZE;
00166 
00167         /* Calculate load address of protected-mode portion */
00168         bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
00169                                         : BZI_LOAD_LOW_ADDR );
00170 
00171         /* Extract video mode */
00172         bzimg->vid_mode = bzimg->bzhdr.vid_mode;
00173 
00174         /* Extract memory limit */
00175         bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
00176                              bzimg->bzhdr.initrd_addr_max : BZI_INITRD_MAX );
00177 
00178         /* Extract command line size */
00179         bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
00180                                 bzimg->bzhdr.cmdline_size : BZI_CMDLINE_SIZE );
00181 
00182         DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
00183                "cmdlen %zd\n", image, bzimg->version,
00184                user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
00185                user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
00186                bzimg->cmdline_size );
00187 
00188         return 0;
00189 }
00190 
00191 /**
00192  * Update bzImage header in loaded kernel
00193  *
00194  * @v image             bzImage file
00195  * @v bzimg             bzImage context
00196  * @v dst               bzImage to update
00197  */
00198 static void bzimage_update_header ( struct image *image,
00199                                     struct bzimage_context *bzimg,
00200                                     userptr_t dst ) {
00201 
00202         /* Set loader type */
00203         if ( bzimg->version >= 0x0200 )
00204                 bzimg->bzhdr.type_of_loader = BZI_LOADER_TYPE_GPXE;
00205 
00206         /* Set heap end pointer */
00207         if ( bzimg->version >= 0x0201 ) {
00208                 bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
00209                 bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
00210         }
00211 
00212         /* Set command line */
00213         if ( bzimg->version >= 0x0202 ) {
00214                 bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
00215                                                            bzimg->rm_cmdline );
00216         } else {
00217                 bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
00218                 bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
00219                 bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
00220         }
00221 
00222         /* Set video mode */
00223         bzimg->bzhdr.vid_mode = bzimg->vid_mode;
00224 
00225         /* Set initrd address */
00226         if ( bzimg->version >= 0x0200 ) {
00227                 bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
00228                 bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
00229         }
00230 
00231         /* Write out header structures */
00232         copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
00233                        sizeof ( bzimg->cmdline_magic ) );
00234         copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
00235                        sizeof ( bzimg->bzhdr ) );
00236 
00237         DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
00238 }
00239 
00240 /**
00241  * Parse kernel command line for bootloader parameters
00242  *
00243  * @v image             bzImage file
00244  * @v bzimg             bzImage context
00245  * @v cmdline           Kernel command line
00246  * @ret rc              Return status code
00247  */
00248 static int bzimage_parse_cmdline ( struct image *image,
00249                                    struct bzimage_context *bzimg,
00250                                    const char *cmdline ) {
00251         char *vga;
00252         char *mem;
00253 
00254         /* Look for "vga=" */
00255         if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
00256                 vga += 4;
00257                 if ( strcmp ( vga, "normal" ) == 0 ) {
00258                         bzimg->vid_mode = BZI_VID_MODE_NORMAL;
00259                 } else if ( strcmp ( vga, "ext" ) == 0 ) {
00260                         bzimg->vid_mode = BZI_VID_MODE_EXT;
00261                 } else if ( strcmp ( vga, "ask" ) == 0 ) {
00262                         bzimg->vid_mode = BZI_VID_MODE_ASK;
00263                 } else {
00264                         bzimg->vid_mode = strtoul ( vga, &vga, 0 );
00265                         if ( *vga && ( *vga != ' ' ) ) {
00266                                 DBGC ( image, "bzImage %p strange \"vga=\""
00267                                        "terminator '%c'\n", image, *vga );
00268                         }
00269                 }
00270         }
00271 
00272         /* Look for "mem=" */
00273         if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
00274                 mem += 4;
00275                 bzimg->mem_limit = strtoul ( mem, &mem, 0 );
00276                 switch ( *mem ) {
00277                 case 'G':
00278                 case 'g':
00279                         bzimg->mem_limit <<= 10;
00280                 case 'M':
00281                 case 'm':
00282                         bzimg->mem_limit <<= 10;
00283                 case 'K':
00284                 case 'k':
00285                         bzimg->mem_limit <<= 10;
00286                         break;
00287                 case '\0':
00288                 case ' ':
00289                         break;
00290                 default:
00291                         DBGC ( image, "bzImage %p strange \"mem=\" "
00292                                "terminator '%c'\n", image, *mem );
00293                         break;
00294                 }
00295                 bzimg->mem_limit -= 1;
00296         }
00297 
00298         return 0;
00299 }
00300 
00301 /**
00302  * Set command line
00303  *
00304  * @v image             bzImage image
00305  * @v bzimg             bzImage context
00306  * @v cmdline           Kernel command line
00307  * @ret rc              Return status code
00308  */
00309 static int bzimage_set_cmdline ( struct image *image,
00310                                  struct bzimage_context *bzimg,
00311                                  const char *cmdline ) {
00312         size_t cmdline_len;
00313 
00314         /* Copy command line down to real-mode portion */
00315         cmdline_len = ( strlen ( cmdline ) + 1 );
00316         if ( cmdline_len > bzimg->cmdline_size )
00317                 cmdline_len = bzimg->cmdline_size;
00318         copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
00319                        cmdline, cmdline_len );
00320         DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
00321 
00322         return 0;
00323 }
00324 
00325 /**
00326  * Load initrd
00327  *
00328  * @v image             bzImage image
00329  * @v initrd            initrd image
00330  * @v address           Address at which to load, or UNULL
00331  * @ret len             Length of loaded image, rounded up to 4 bytes
00332  */
00333 static size_t bzimage_load_initrd ( struct image *image,
00334                                     struct image *initrd,
00335                                     userptr_t address ) {
00336         char *filename = initrd->cmdline;
00337         struct cpio_header cpio;
00338         size_t offset = 0;
00339 
00340         /* Do not include kernel image itself as an initrd */
00341         if ( initrd == image )
00342                 return 0;
00343 
00344         /* Create cpio header before non-prebuilt images */
00345         if ( filename && filename[0] ) {
00346                 size_t name_len = ( strlen ( filename ) + 1 );
00347 
00348                 DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
00349                        image, initrd, filename );
00350                 memset ( &cpio, '0', sizeof ( cpio ) );
00351                 memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
00352                 cpio_set_field ( cpio.c_mode, 0100644 );
00353                 cpio_set_field ( cpio.c_nlink, 1 );
00354                 cpio_set_field ( cpio.c_filesize, initrd->len );
00355                 cpio_set_field ( cpio.c_namesize, name_len );
00356                 if ( address ) {
00357                         copy_to_user ( address, offset, &cpio,
00358                                        sizeof ( cpio ) );
00359                 }
00360                 offset += sizeof ( cpio );
00361                 if ( address ) {
00362                         copy_to_user ( address, offset, filename,
00363                                        name_len );
00364                 }
00365                 offset += name_len;
00366                 offset = ( ( offset + 0x03 ) & ~0x03 );
00367         }
00368 
00369         /* Copy in initrd image body */
00370         if ( address )
00371                 memcpy_user ( address, offset, initrd->data, 0, initrd->len );
00372         offset += initrd->len;
00373         if ( address ) {
00374                 DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
00375                        image, initrd, user_to_phys ( address, 0 ),
00376                        user_to_phys ( address, offset ) );
00377         }
00378 
00379         /* Round up to 4-byte boundary */
00380         offset = ( ( offset + 0x03 ) & ~0x03 );
00381         return offset;
00382 }
00383 
00384 /**
00385  * Load initrds, if any
00386  *
00387  * @v image             bzImage image
00388  * @v bzimg             bzImage context
00389  * @ret rc              Return status code
00390  */
00391 static int bzimage_load_initrds ( struct image *image,
00392                                   struct bzimage_context *bzimg ) {
00393         struct image *initrd;
00394         size_t total_len = 0;
00395         physaddr_t address;
00396         int rc;
00397 
00398         /* Add up length of all initrd images */
00399         for_each_image ( initrd )
00400                 total_len += bzimage_load_initrd ( image, initrd, UNULL );
00401 
00402         /* Give up if no initrd images found */
00403         if ( ! total_len )
00404                 return 0;
00405 
00406         /* Find a suitable start address.  Try 1MB boundaries,
00407          * starting from the downloaded kernel image itself and
00408          * working downwards until we hit an available region.
00409          */
00410         for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
00411               address -= 0x100000 ) {
00412                 /* Check that we're not going to overwrite the
00413                  * kernel itself.  This check isn't totally
00414                  * accurate, but errs on the side of caution.
00415                  */
00416                 if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
00417                         DBGC ( image, "bzImage %p could not find a location "
00418                                "for initrd\n", image );
00419                         return -ENOBUFS;
00420                 }
00421                 /* Check that we are within the kernel's range */
00422                 if ( ( address + total_len - 1 ) > bzimg->mem_limit )
00423                         continue;
00424                 /* Prepare and verify segment */
00425                 if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
00426                                            total_len ) ) != 0 )
00427                         continue;
00428                 /* Use this address */
00429                 break;
00430         }
00431 
00432         /* Record initrd location */
00433         bzimg->ramdisk_image = address;
00434         bzimg->ramdisk_size = total_len;
00435 
00436         /* Construct initrd */
00437         DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
00438                image, address, ( address + total_len ) );
00439         for_each_image ( initrd ) {
00440                 address += bzimage_load_initrd ( image, initrd,
00441                                                  phys_to_user ( address ) );
00442         }
00443 
00444         return 0;
00445 }
00446 
00447 /**
00448  * Execute bzImage image
00449  *
00450  * @v image             bzImage image
00451  * @ret rc              Return status code
00452  */
00453 static int bzimage_exec ( struct image *image ) {
00454         struct bzimage_context bzimg;
00455         const char *cmdline = ( image->cmdline ? image->cmdline : "" );
00456         int rc;
00457 
00458         /* Read and parse header from loaded kernel */
00459         if ( ( rc = bzimage_parse_header ( image, &bzimg,
00460                                            image->priv.user ) ) != 0 )
00461                 return rc;
00462         assert ( bzimg.rm_kernel == image->priv.user );
00463 
00464         /* Parse command line for bootloader parameters */
00465         if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
00466                 return rc;
00467 
00468         /* Store command line */
00469         if ( ( rc = bzimage_set_cmdline ( image, &bzimg, cmdline ) ) != 0 )
00470                 return rc;
00471 
00472         /* Load any initrds */
00473         if ( ( rc = bzimage_load_initrds ( image, &bzimg ) ) != 0 )
00474                 return rc;
00475 
00476         /* Update kernel header */
00477         bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
00478 
00479         /* Prepare for exiting */
00480         shutdown ( SHUTDOWN_BOOT );
00481 
00482         DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
00483                "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
00484                bzimg.rm_kernel_seg, bzimg.rm_heap );
00485 
00486         /* Jump to the kernel */
00487         __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
00488                                            "movw %w0, %%es\n\t"
00489                                            "movw %w0, %%fs\n\t"
00490                                            "movw %w0, %%gs\n\t"
00491                                            "movw %w0, %%ss\n\t"
00492                                            "movw %w1, %%sp\n\t"
00493                                            "pushw %w2\n\t"
00494                                            "pushw $0\n\t"
00495                                            "lret\n\t" )
00496                                : : "r" ( bzimg.rm_kernel_seg ),
00497                                    "r" ( bzimg.rm_heap ),
00498                                    "r" ( bzimg.rm_kernel_seg + 0x20 ) );
00499 
00500         /* There is no way for the image to return, since we provide
00501          * no return address.
00502          */
00503         assert ( 0 );
00504 
00505         return -ECANCELED; /* -EIMPOSSIBLE */
00506 }
00507 
00508 /**
00509  * Load bzImage image into memory
00510  *
00511  * @v image             bzImage file
00512  * @ret rc              Return status code
00513  */
00514 int bzimage_load ( struct image *image ) {
00515         struct bzimage_context bzimg;
00516         int rc;
00517 
00518         /* Read and parse header from image */
00519         if ( ( rc = bzimage_parse_header ( image, &bzimg,
00520                                            image->data ) ) != 0 )
00521                 return rc;
00522 
00523         /* This is a bzImage image, valid or otherwise */
00524         if ( ! image->type )
00525                 image->type = &bzimage_image_type;
00526 
00527         /* Prepare segments */
00528         if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
00529                                    bzimg.rm_memsz ) ) != 0 ) {
00530                 DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
00531                        image, strerror ( rc ) );
00532                 return rc;
00533         }
00534         if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
00535                                    bzimg.pm_sz ) ) != 0 ) {
00536                 DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
00537                        image, strerror ( rc ) );
00538                 return rc;
00539         }
00540 
00541         /* Load segments */
00542         memcpy_user ( bzimg.rm_kernel, 0, image->data,
00543                       0, bzimg.rm_filesz );
00544         memcpy_user ( bzimg.pm_kernel, 0, image->data,
00545                       bzimg.rm_filesz, bzimg.pm_sz );
00546 
00547         /* Update and write out header */
00548         bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
00549 
00550         /* Record real-mode segment in image private data field */
00551         image->priv.user = bzimg.rm_kernel;
00552 
00553         return 0;
00554 }
00555 
00556 /** Linux bzImage image type */
00557 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
00558         .name = "bzImage",
00559         .load = bzimage_load,
00560         .exec = bzimage_exec,
00561 };

Generated on Tue Apr 6 20:00:50 2010 for gPXE by  doxygen 1.5.7.1