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 DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
347 "(stack %04x:%04x)\n", image,
348 ( exec_ctx.rm_kernel_seg + 0x20 ),
349 exec_ctx.rm_kernel_seg, exec_ctx.rm_heap );
351 /* Jump to the kernel */
352 __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
361 : : "r" ( exec_ctx.rm_kernel_seg ),
362 "r" ( exec_ctx.rm_heap ),
363 "r" ( exec_ctx.rm_kernel_seg + 0x20 ) );
365 /* There is no way for the image to return, since we provide
370 return -ECANCELED; /* -EIMPOSSIBLE */
374 * Load and parse bzImage header
376 * @v image bzImage file
377 * @v load_ctx Load context
378 * @v bzhdr Buffer for bzImage header
379 * @ret rc Return status code
381 static int bzimage_load_header ( struct image *image,
382 struct bzimage_load_context *load_ctx,
383 struct bzimage_header *bzhdr ) {
386 if ( image->len < ( BZI_HDR_OFFSET + sizeof ( *bzhdr ) ) ) {
387 DBGC ( image, "bzImage %p too short for kernel header\n",
392 /* Read and verify header */
393 copy_from_user ( bzhdr, image->data, BZI_HDR_OFFSET,
395 if ( bzhdr->header != BZI_SIGNATURE ) {
396 DBGC ( image, "bzImage %p bad signature %08lx\n",
397 image, bzhdr->header );
401 /* We don't support ancient kernels */
402 if ( bzhdr->version < 0x0200 ) {
403 DBGC ( image, "bzImage %p version %04x not supported\n",
404 image, bzhdr->version );
408 /* Calculate load address and size of real-mode portion */
409 load_ctx->rm_kernel_seg = 0x1000; /* place RM kernel at 1000:0000 */
410 load_ctx->rm_kernel = real_to_user ( load_ctx->rm_kernel_seg, 0 );
411 load_ctx->rm_filesz =
412 ( ( bzhdr->setup_sects ? bzhdr->setup_sects : 4 ) + 1 ) << 9;
413 load_ctx->rm_memsz = BZI_ASSUMED_RM_SIZE;
414 if ( load_ctx->rm_filesz > image->len ) {
415 DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
416 image, load_ctx->rm_filesz );
420 /* Calculate load address and size of non-real-mode portion */
421 load_ctx->pm_kernel = ( ( bzhdr->loadflags & BZI_LOAD_HIGH ) ?
422 phys_to_user ( BZI_LOAD_HIGH_ADDR ) :
423 phys_to_user ( BZI_LOAD_LOW_ADDR ) );
424 load_ctx->pm_sz = ( image->len - load_ctx->rm_filesz );
426 DBGC ( image, "bzImage %p version %04x RM %#zx bytes PM %#zx bytes\n",
427 image, bzhdr->version, load_ctx->rm_filesz, load_ctx->pm_sz );
432 * Load real-mode portion of bzImage
434 * @v image bzImage file
435 * @v load_ctx Load context
436 * @ret rc Return status code
438 static int bzimage_load_real ( struct image *image,
439 struct bzimage_load_context *load_ctx ) {
442 /* Allow space for the stack and heap */
443 load_ctx->rm_memsz += BZI_STACK_SIZE;
444 load_ctx->rm_heap = load_ctx->rm_memsz;
446 /* Allow space for the command line */
447 load_ctx->rm_cmdline = load_ctx->rm_memsz;
448 load_ctx->rm_memsz += BZI_CMDLINE_SIZE;
450 /* Prepare, verify, and load the real-mode segment */
451 if ( ( rc = prep_segment ( load_ctx->rm_kernel, load_ctx->rm_filesz,
452 load_ctx->rm_memsz ) ) != 0 ) {
453 DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
454 image, strerror ( rc ) );
457 memcpy_user ( load_ctx->rm_kernel, 0, image->data, 0,
458 load_ctx->rm_filesz );
464 * Load non-real-mode portion of bzImage
466 * @v image bzImage file
467 * @v load_ctx Load context
468 * @ret rc Return status code
470 static int bzimage_load_non_real ( struct image *image,
471 struct bzimage_load_context *load_ctx ) {
474 /* Prepare, verify and load the non-real-mode segment */
475 if ( ( rc = prep_segment ( load_ctx->pm_kernel, load_ctx->pm_sz,
476 load_ctx->pm_sz ) ) != 0 ) {
477 DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
478 image, strerror ( rc ) );
481 memcpy_user ( load_ctx->pm_kernel, 0, image->data, load_ctx->rm_filesz,
488 * Update and store bzImage header
490 * @v image bzImage file
491 * @v load_ctx Load context
492 * @v bzhdr Original bzImage header
493 * @ret rc Return status code
495 static int bzimage_write_header ( struct image *image __unused,
496 struct bzimage_load_context *load_ctx,
497 struct bzimage_header *bzhdr ) {
498 struct bzimage_cmdline cmdline;
500 /* Update the header and copy it into the loaded kernel */
501 bzhdr->type_of_loader = BZI_LOADER_TYPE_GPXE;
502 if ( bzhdr->version >= 0x0201 ) {
503 bzhdr->heap_end_ptr = ( load_ctx->rm_heap - 0x200 );
504 bzhdr->loadflags |= BZI_CAN_USE_HEAP;
506 if ( bzhdr->version >= 0x0202 ) {
507 bzhdr->cmd_line_ptr = user_to_phys ( load_ctx->rm_kernel,
508 load_ctx->rm_cmdline );
510 cmdline.magic = BZI_CMDLINE_MAGIC;
511 cmdline.offset = load_ctx->rm_cmdline;
512 copy_to_user ( load_ctx->rm_kernel, BZI_CMDLINE_OFFSET,
513 &cmdline, sizeof ( cmdline ) );
514 bzhdr->setup_move_size = load_ctx->rm_memsz;
516 copy_to_user ( load_ctx->rm_kernel, BZI_HDR_OFFSET,
517 bzhdr, sizeof ( *bzhdr ) );
523 * Load bzImage image into memory
525 * @v image bzImage file
526 * @ret rc Return status code
528 int bzimage_load ( struct image *image ) {
529 struct bzimage_load_context load_ctx;
530 struct bzimage_header bzhdr;
533 /* Initialise context */
534 memset ( &load_ctx, 0, sizeof ( load_ctx ) );
536 /* Load and verify header */
537 if ( ( rc = bzimage_load_header ( image, &load_ctx, &bzhdr ) ) != 0 )
540 /* This is a bzImage image, valid or otherwise */
542 image->type = &bzimage_image_type;
544 /* Load real-mode portion */
545 if ( ( rc = bzimage_load_real ( image, &load_ctx ) ) != 0 )
548 /* Load non-real-mode portion */
549 if ( ( rc = bzimage_load_non_real ( image, &load_ctx ) ) != 0 )
552 /* Update and write out header */
553 if ( ( rc = bzimage_write_header ( image, &load_ctx, &bzhdr ) ) != 0 )
556 /* Record real-mode segment in image private data field */
557 image->priv.ul = load_ctx.rm_kernel_seg;
562 /** Linux bzImage image type */
563 struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
565 .load = bzimage_load,
566 .exec = bzimage_exec,