2 * Copyright (C) 2007 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Linux bzImage image format
33 #include <gpxe/uaccess.h>
34 #include <gpxe/image.h>
35 #include <gpxe/segment.h>
36 #include <gpxe/init.h>
37 #include <gpxe/initrd.h>
38 #include <gpxe/cpio.h>
39 #include <gpxe/features.h>
41 FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
43 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL );
46 * bzImage load context
48 struct bzimage_load_context {
49 /** Real-mode kernel portion load segment address */
50 unsigned int rm_kernel_seg;
51 /** Real-mode kernel portion load address */
53 /** Real-mode kernel portion file size */
55 /** Real-mode heap top (offset from rm_kernel) */
57 /** Command line (offset from rm_kernel) */
59 /** Real-mode kernel portion total memory size */
61 /** Non-real-mode kernel portion load address */
63 /** Non-real-mode kernel portion file and memory size */
68 * bzImage execution context
70 struct bzimage_exec_context {
71 /** Real-mode kernel portion load segment address */
72 unsigned int rm_kernel_seg;
73 /** Real-mode kernel portion load address */
75 /** Real-mode heap top (offset from rm_kernel) */
77 /** Command line (offset from rm_kernel) */
80 unsigned int vid_mode;
84 physaddr_t ramdisk_image;
86 physaddr_t ramdisk_size;
90 * Parse kernel command line for bootloader parameters
92 * @v image bzImage file
93 * @v exec_ctx Execution context
94 * @v cmdline Kernel command line
95 * @ret rc Return status code
97 static int bzimage_parse_cmdline ( struct image *image,
98 struct bzimage_exec_context *exec_ctx,
99 const char *cmdline ) {
103 /* Look for "vga=" */
104 if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
106 if ( strcmp ( vga, "normal" ) == 0 ) {
107 exec_ctx->vid_mode = BZI_VID_MODE_NORMAL;
108 } else if ( strcmp ( vga, "ext" ) == 0 ) {
109 exec_ctx->vid_mode = BZI_VID_MODE_EXT;
110 } else if ( strcmp ( vga, "ask" ) == 0 ) {
111 exec_ctx->vid_mode = BZI_VID_MODE_ASK;
113 exec_ctx->vid_mode = strtoul ( vga, &vga, 0 );
114 if ( *vga && ( *vga != ' ' ) ) {
115 DBGC ( image, "bzImage %p strange \"vga=\""
116 "terminator '%c'\n", image, *vga );
121 /* Look for "mem=" */
122 if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
124 exec_ctx->mem_limit = strtoul ( mem, &mem, 0 );
128 exec_ctx->mem_limit <<= 10;
131 exec_ctx->mem_limit <<= 10;
134 exec_ctx->mem_limit <<= 10;
140 DBGC ( image, "bzImage %p strange \"mem=\" "
141 "terminator '%c'\n", image, *mem );
144 exec_ctx->mem_limit -= 1;
153 * @v image bzImage image
154 * @v exec_ctx Execution context
155 * @v cmdline Kernel command line
156 * @ret rc Return status code
158 static int bzimage_set_cmdline ( struct image *image,
159 struct bzimage_exec_context *exec_ctx,
160 const char *cmdline ) {
163 /* Copy command line down to real-mode portion */
164 cmdline_len = ( strlen ( cmdline ) + 1 );
165 if ( cmdline_len > BZI_CMDLINE_SIZE )
166 cmdline_len = BZI_CMDLINE_SIZE;
167 copy_to_user ( exec_ctx->rm_kernel, exec_ctx->rm_cmdline,
168 cmdline, cmdline_len );
169 DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
177 * @v image bzImage image
178 * @v initrd initrd image
179 * @v address Address at which to load, or UNULL
180 * @ret len Length of loaded image, rounded up to 4 bytes
182 static size_t bzimage_load_initrd ( struct image *image,
183 struct image *initrd,
184 userptr_t address ) {
185 char *filename = initrd->cmdline;
186 struct cpio_header cpio;
189 /* Ignore images which aren't initrds */
190 if ( initrd->type != &initrd_image_type )
193 /* Create cpio header before non-prebuilt images */
194 if ( filename && filename[0] ) {
195 size_t name_len = ( strlen ( filename ) + 1 );
197 DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
198 image, initrd, filename );
199 memset ( &cpio, '0', sizeof ( cpio ) );
200 memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
201 cpio_set_field ( cpio.c_mode, 0100644 );
202 cpio_set_field ( cpio.c_nlink, 1 );
203 cpio_set_field ( cpio.c_filesize, initrd->len );
204 cpio_set_field ( cpio.c_namesize, name_len );
206 copy_to_user ( address, offset, &cpio,
209 offset += sizeof ( cpio );
211 copy_to_user ( address, offset, filename,
215 offset = ( ( offset + 0x03 ) & ~0x03 );
218 /* Copy in initrd image body */
220 DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
221 image, initrd, address, ( address + offset ) );
222 memcpy_user ( address, offset, initrd->data, 0,
225 offset += initrd->len;
227 offset = ( ( offset + 0x03 ) & ~0x03 );
232 * Load initrds, if any
234 * @v image bzImage image
235 * @v exec_ctx Execution context
236 * @ret rc Return status code
238 static int bzimage_load_initrds ( struct image *image,
239 struct bzimage_exec_context *exec_ctx ) {
240 struct image *initrd;
241 size_t total_len = 0;
245 /* Add up length of all initrd images */
246 for_each_image ( initrd ) {
247 total_len += bzimage_load_initrd ( image, initrd, UNULL );
250 /* Give up if no initrd images found */
254 /* Find a suitable start address. Try 1MB boundaries,
255 * starting from the downloaded kernel image itself and
256 * working downwards until we hit an available region.
258 for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
259 address -= 0x100000 ) {
260 /* Check that we're not going to overwrite the
261 * kernel itself. This check isn't totally
262 * accurate, but errs on the side of caution.
264 if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
265 DBGC ( image, "bzImage %p could not find a location "
266 "for initrd\n", image );
269 /* Check that we are within the kernel's range */
270 if ( ( address + total_len - 1 ) > exec_ctx->mem_limit )
272 /* Prepare and verify segment */
273 if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
276 /* Use this address */
280 /* Record initrd location */
281 exec_ctx->ramdisk_image = address;
282 exec_ctx->ramdisk_size = total_len;
284 /* Construct initrd */
285 DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
286 image, address, ( address + total_len ) );
287 for_each_image ( initrd ) {
288 address += bzimage_load_initrd ( image, initrd,
289 phys_to_user ( address ) );
296 * Execute bzImage image
298 * @v image bzImage image
299 * @ret rc Return status code
301 static int bzimage_exec ( struct image *image ) {
302 struct bzimage_exec_context exec_ctx;
303 struct bzimage_header bzhdr;
304 const char *cmdline = ( image->cmdline ? image->cmdline : "" );
307 /* Initialise context */
308 memset ( &exec_ctx, 0, sizeof ( exec_ctx ) );
310 /* Retrieve kernel header */
311 exec_ctx.rm_kernel_seg = image->priv.ul;
312 exec_ctx.rm_kernel = real_to_user ( exec_ctx.rm_kernel_seg, 0 );
313 copy_from_user ( &bzhdr, exec_ctx.rm_kernel, BZI_HDR_OFFSET,
315 exec_ctx.rm_cmdline = exec_ctx.rm_heap =
316 ( bzhdr.heap_end_ptr + 0x200 );
317 exec_ctx.vid_mode = bzhdr.vid_mode;
318 if ( bzhdr.version >= 0x0203 ) {
319 exec_ctx.mem_limit = bzhdr.initrd_addr_max;
321 exec_ctx.mem_limit = BZI_INITRD_MAX;
324 /* Parse command line for bootloader parameters */
325 if ( ( rc = bzimage_parse_cmdline ( image, &exec_ctx, cmdline ) ) != 0)
328 /* Store command line */
329 if ( ( rc = bzimage_set_cmdline ( image, &exec_ctx, cmdline ) ) != 0 )
332 /* Load any initrds */
333 if ( ( rc = bzimage_load_initrds ( image, &exec_ctx ) ) != 0 )
336 /* Update and store kernel header */
337 bzhdr.vid_mode = exec_ctx.vid_mode;
338 bzhdr.ramdisk_image = exec_ctx.ramdisk_image;
339 bzhdr.ramdisk_size = exec_ctx.ramdisk_size;
340 copy_to_user ( exec_ctx.rm_kernel, BZI_HDR_OFFSET, &bzhdr,
343 /* Prepare for exiting */
346 /* Jump to the kernel */
347 __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
356 : : "r" ( exec_ctx.rm_kernel_seg ),
357 "r" ( exec_ctx.rm_heap ),
358 "r" ( exec_ctx.rm_kernel_seg + 0x20 ) );
360 /* There is no way for the image to return, since we provide
365 return -ECANCELED; /* -EIMPOSSIBLE */
369 * Load and parse bzImage header
371 * @v image bzImage file
372 * @v load_ctx Load context
373 * @v bzhdr Buffer for bzImage header
374 * @ret rc Return status code
376 static int bzimage_load_header ( struct image *image,
377 struct bzimage_load_context *load_ctx,
378 struct bzimage_header *bzhdr ) {
381 if ( image->len < ( BZI_HDR_OFFSET + sizeof ( *bzhdr ) ) ) {
382 DBGC ( image, "bzImage %p too short for kernel header\n",
387 /* Read and verify header */
388 copy_from_user ( bzhdr, image->data, BZI_HDR_OFFSET,
390 if ( bzhdr->header != BZI_SIGNATURE ) {
391 DBGC ( image, "bzImage %p bad signature %08lx\n",
392 image, bzhdr->header );
396 /* We don't support ancient kernels */
397 if ( bzhdr->version < 0x0200 ) {
398 DBGC ( image, "bzImage %p version %04x not supported\n",
399 image, bzhdr->version );
403 /* Calculate load address and size of real-mode portion */
404 load_ctx->rm_kernel_seg = 0x1000; /* place RM kernel at 1000:0000 */
405 load_ctx->rm_kernel = real_to_user ( load_ctx->rm_kernel_seg, 0 );
406 load_ctx->rm_filesz = load_ctx->rm_memsz =
407 ( ( bzhdr->setup_sects ? bzhdr->setup_sects : 4 ) + 1 ) << 9;
408 if ( load_ctx->rm_filesz > image->len ) {
409 DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
410 image, load_ctx->rm_filesz );
414 /* Calculate load address and size of non-real-mode portion */
415 load_ctx->pm_kernel = ( ( bzhdr->loadflags & BZI_LOAD_HIGH ) ?
416 phys_to_user ( BZI_LOAD_HIGH_ADDR ) :
417 phys_to_user ( BZI_LOAD_LOW_ADDR ) );
418 load_ctx->pm_sz = ( image->len - load_ctx->rm_filesz );
420 DBGC ( image, "bzImage %p version %04x RM %#zx bytes PM %#zx bytes\n",
421 image, bzhdr->version, load_ctx->rm_filesz, load_ctx->pm_sz );
426 * Load real-mode portion of bzImage
428 * @v image bzImage file
429 * @v load_ctx Load context
430 * @ret rc Return status code
432 static int bzimage_load_real ( struct image *image,
433 struct bzimage_load_context *load_ctx ) {
436 /* Allow space for the stack and heap */
437 load_ctx->rm_memsz += BZI_STACK_SIZE;
438 load_ctx->rm_heap = load_ctx->rm_memsz;
440 /* Allow space for the command line */
441 load_ctx->rm_cmdline = load_ctx->rm_memsz;
442 load_ctx->rm_memsz += BZI_CMDLINE_SIZE;
444 /* Prepare, verify, and load the real-mode segment */
445 if ( ( rc = prep_segment ( load_ctx->rm_kernel, load_ctx->rm_filesz,
446 load_ctx->rm_memsz ) ) != 0 ) {
447 DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
448 image, strerror ( rc ) );
451 memcpy_user ( load_ctx->rm_kernel, 0, image->data, 0,
452 load_ctx->rm_filesz );
458 * Load non-real-mode portion of bzImage
460 * @v image bzImage file
461 * @v load_ctx Load context
462 * @ret rc Return status code
464 static int bzimage_load_non_real ( struct image *image,
465 struct bzimage_load_context *load_ctx ) {
468 /* Prepare, verify and load the non-real-mode segment */
469 if ( ( rc = prep_segment ( load_ctx->pm_kernel, load_ctx->pm_sz,
470 load_ctx->pm_sz ) ) != 0 ) {
471 DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
472 image, strerror ( rc ) );
475 memcpy_user ( load_ctx->pm_kernel, 0, image->data, load_ctx->rm_filesz,
482 * Update and store bzImage header
484 * @v image bzImage file
485 * @v load_ctx Load context
486 * @v bzhdr Original bzImage header
487 * @ret rc Return status code
489 static int bzimage_write_header ( struct image *image __unused,
490 struct bzimage_load_context *load_ctx,
491 struct bzimage_header *bzhdr ) {
492 struct bzimage_cmdline cmdline;
494 /* Update the header and copy it into the loaded kernel */
495 bzhdr->type_of_loader = BZI_LOADER_TYPE_GPXE;
496 if ( bzhdr->version >= 0x0201 ) {
497 bzhdr->heap_end_ptr = ( load_ctx->rm_heap - 0x200 );
498 bzhdr->loadflags |= BZI_CAN_USE_HEAP;
500 if ( bzhdr->version >= 0x0202 ) {
501 bzhdr->cmd_line_ptr = user_to_phys ( load_ctx->rm_kernel,
502 load_ctx->rm_cmdline );
504 cmdline.magic = BZI_CMDLINE_MAGIC;
505 cmdline.offset = load_ctx->rm_cmdline;
506 copy_to_user ( load_ctx->rm_kernel, BZI_CMDLINE_OFFSET,
507 &cmdline, sizeof ( cmdline ) );
508 bzhdr->setup_move_size = load_ctx->rm_memsz;
510 copy_to_user ( load_ctx->rm_kernel, BZI_HDR_OFFSET,
511 bzhdr, sizeof ( *bzhdr ) );
517 * Load bzImage image into memory
519 * @v image bzImage file
520 * @ret rc Return status code
522 int bzimage_load ( struct image *image ) {
523 struct bzimage_load_context load_ctx;
524 struct bzimage_header bzhdr;
527 /* Initialise context */
528 memset ( &load_ctx, 0, sizeof ( load_ctx ) );
530 /* Load and verify header */
531 if ( ( rc = bzimage_load_header ( image, &load_ctx, &bzhdr ) ) != 0 )
534 /* This is a bzImage image, valid or otherwise */
536 image->type = &bzimage_image_type;
538 /* Load real-mode portion */
539 if ( ( rc = bzimage_load_real ( image, &load_ctx ) ) != 0 )
542 /* Load non-real-mode portion */
543 if ( ( rc = bzimage_load_non_real ( image, &load_ctx ) ) != 0 )
546 /* Update and write out header */
547 if ( ( rc = bzimage_write_header ( image, &load_ctx, &bzhdr ) ) != 0 )
550 /* Record real-mode segment in image private data field */
551 image->priv.ul = load_ctx.rm_kernel_seg;
556 /** Linux bzImage image type */
557 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
559 .load = bzimage_load,
560 .exec = bzimage_exec,