summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2007-01-14 14:36:33 +0100
committerMichael Brown2007-01-14 14:36:33 +0100
commit3ccd7335f0ae284835ab3ac6aab14eacd890b0e6 (patch)
tree1200d9a57ffb4a32cb100f31c94411a79b2a759c
parentCan start a Linux kernel directly (albeit with no initrd support) (diff)
downloadipxe-3ccd7335f0ae284835ab3ac6aab14eacd890b0e6.tar.gz
ipxe-3ccd7335f0ae284835ab3ac6aab14eacd890b0e6.tar.xz
ipxe-3ccd7335f0ae284835ab3ac6aab14eacd890b0e6.zip
Split bzimage_load into separate functions
-rw-r--r--src/arch/i386/image/bzimage.c264
-rw-r--r--src/arch/i386/include/bzimage.h25
2 files changed, 202 insertions, 87 deletions
diff --git a/src/arch/i386/image/bzimage.c b/src/arch/i386/image/bzimage.c
index 48b69046..4a9a45ff 100644
--- a/src/arch/i386/image/bzimage.c
+++ b/src/arch/i386/image/bzimage.c
@@ -36,17 +36,35 @@
struct image_type bzimage_image_type __image_type ( PROBE_NORMAL );
/**
+ * bzImage load context
+ */
+struct bzimage_load_context {
+ /** Real-mode kernel portion load segment address */
+ unsigned int rm_kernel_seg;
+ /** Real-mode kernel portion load address */
+ userptr_t rm_kernel;
+ /** Real-mode kernel portion file size */
+ size_t rm_filesz;
+ /** Real-mode heap top (offset from rm_kernel) */
+ size_t rm_heap;
+ /** Command line (offset from rm_kernel) */
+ size_t rm_cmdline;
+ /** Real-mode kernel portion total memory size */
+ size_t rm_memsz;
+ /** Non-real-mode kernel portion load address */
+ userptr_t pm_kernel;
+ /** Non-real-mode kernel portion file and memory size */
+ size_t pm_sz;
+};
+
+/**
* bzImage execution context
*/
-union bzimage_exec_context {
- /** Real-mode parameters */
- struct {
- /** Kernel real-mode data segment */
- uint16_t kernel_seg;
- /** Kernel real-mode stack pointer */
- uint16_t stack;
- } rm;
- unsigned long ul;
+struct bzimage_exec_context {
+ /** Kernel real-mode data segment */
+ uint16_t kernel_seg;
+ /** Kernel real-mode stack pointer */
+ uint16_t stack;
};
/**
@@ -56,10 +74,13 @@ union bzimage_exec_context {
* @ret rc Return status code
*/
static int bzimage_exec ( struct image *image ) {
- union bzimage_exec_context context;
+ union {
+ struct bzimage_exec_context bz;
+ unsigned long ul;
+ } exec_ctx;
/* Retrieve stored execution context */
- context.ul = image->priv.ul;
+ exec_ctx.ul = image->priv.ul;
/* Prepare for exiting */
shutdown();
@@ -74,9 +95,9 @@ static int bzimage_exec ( struct image *image ) {
"pushw %w2\n\t"
"pushw $0\n\t"
"lret\n\t" )
- : : "r" ( context.rm.kernel_seg ),
- "r" ( context.rm.stack ),
- "r" ( context.rm.kernel_seg + 0x20 ) );
+ : : "r" ( exec_ctx.bz.kernel_seg ),
+ "r" ( exec_ctx.bz.stack ),
+ "r" ( exec_ctx.bz.kernel_seg + 0x20 ) );
/* There is no way for the image to return, since we provide
* no return address.
@@ -87,119 +108,200 @@ static int bzimage_exec ( struct image *image ) {
}
/**
- * Load bzImage image into memory
+ * Load and parse bzImage header
*
* @v image bzImage file
+ * @v load_ctx Load context
+ * @v bzhdr Buffer for bzImage header
* @ret rc Return status code
*/
-int bzimage_load ( struct image *image ) {
- struct bzimage_header bzhdr;
- union bzimage_exec_context context;
- unsigned int rm_kernel_seg = 0x1000; /* place RM kernel at 1000:0000 */
- userptr_t rm_kernel = real_to_user ( rm_kernel_seg, 0 );
- userptr_t pm_kernel;
- size_t rm_filesz;
- size_t rm_memsz;
- size_t pm_filesz;
- size_t pm_memsz;
- size_t rm_heap_end;
- size_t rm_cmdline;
- int rc;
+static int bzimage_load_header ( struct image *image,
+ struct bzimage_load_context *load_ctx,
+ struct bzimage_header *bzhdr ) {
/* Sanity check */
- if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzhdr ) ) ) {
+ if ( image->len < ( BZI_HDR_OFFSET + sizeof ( *bzhdr ) ) ) {
DBGC ( image, "bzImage %p too short for kernel header\n",
image );
return -ENOEXEC;
}
/* Read and verify header */
- copy_from_user ( &bzhdr, image->data, BZI_HDR_OFFSET,
- sizeof ( bzhdr ) );
- if ( bzhdr.header != BZI_SIGNATURE ) {
- DBGC ( image, "bzImage %p not a bzImage\n", image );
+ copy_from_user ( bzhdr, image->data, BZI_HDR_OFFSET,
+ sizeof ( *bzhdr ) );
+ if ( bzhdr->header != BZI_SIGNATURE ) {
+ DBGC ( image, "bzImage %p bad signature\n", image );
return -ENOEXEC;
}
- /* This is a bzImage image, valid or otherwise */
- if ( ! image->type )
- image->type = &bzimage_image_type;
-
/* We don't support ancient kernels */
- if ( bzhdr.version < 0x0200 ) {
+ if ( bzhdr->version < 0x0200 ) {
DBGC ( image, "bzImage %p version %04x not supported\n",
- image, bzhdr.version );
+ image, bzhdr->version );
return -ENOTSUP;
}
- DBGC ( image, "bzImage %p version %04x\n", image, bzhdr.version );
- /* Check size of base memory portions */
- rm_filesz = ( ( bzhdr.setup_sects ? bzhdr.setup_sects : 4 ) + 1 ) << 9;
- if ( rm_filesz > image->len ) {
+ /* Calculate load address and size of real-mode portion */
+ load_ctx->rm_kernel_seg = 0x1000; /* place RM kernel at 1000:0000 */
+ load_ctx->rm_kernel = real_to_user ( load_ctx->rm_kernel_seg, 0 );
+ load_ctx->rm_filesz = load_ctx->rm_memsz =
+ ( ( bzhdr->setup_sects ? bzhdr->setup_sects : 4 ) + 1 ) << 9;
+ if ( load_ctx->rm_filesz > image->len ) {
DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
- image, rm_filesz );
+ image, load_ctx->rm_filesz );
return -ENOEXEC;
}
- rm_memsz = rm_filesz;
+
+ /* Calculate load address and size of non-real-mode portion */
+ load_ctx->pm_kernel = ( ( bzhdr->loadflags & BZI_LOAD_HIGH ) ?
+ phys_to_user ( BZI_LOAD_HIGH_ADDR ) :
+ phys_to_user ( BZI_LOAD_LOW_ADDR ) );
+ load_ctx->pm_sz = ( image->len - load_ctx->rm_filesz );
+
+ DBGC ( image, "bzImage %p version %04x RM %#zx bytes PM %#zx bytes\n",
+ image, bzhdr->version, load_ctx->rm_filesz, load_ctx->pm_sz );
+ return 0;
+}
+
+/**
+ * Load real-mode portion of bzImage
+ *
+ * @v image bzImage file
+ * @v load_ctx Load context
+ * @v cmdline Kernel command line
+ * @ret rc Return status code
+ */
+static int bzimage_load_real ( struct image *image,
+ struct bzimage_load_context *load_ctx,
+ const char *cmdline ) {
+ size_t cmdline_len = ( strlen ( cmdline ) + 1 );
+ int rc;
/* Allow space for the stack and heap */
- rm_memsz += BZI_STACK_SIZE;
- rm_heap_end = rm_memsz;
+ load_ctx->rm_memsz += BZI_STACK_SIZE;
+ load_ctx->rm_heap = load_ctx->rm_memsz;
/* Allow space for the command line, if one exists */
- rm_cmdline = rm_memsz;
- if ( image->cmdline )
- rm_memsz += ( strlen ( image->cmdline ) + 1 );
+ load_ctx->rm_cmdline = load_ctx->rm_memsz;
+ load_ctx->rm_memsz += cmdline_len;
/* Prepare, verify, and load the real-mode segment */
- if ( ( rc = prep_segment ( rm_kernel, rm_filesz, rm_memsz ) ) != 0 ) {
+ if ( ( rc = prep_segment ( load_ctx->rm_kernel, load_ctx->rm_filesz,
+ load_ctx->rm_memsz ) ) != 0 ) {
DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
image, strerror ( rc ) );
return rc;
}
- memcpy_user ( rm_kernel, 0, image->data, 0, rm_filesz );
+ memcpy_user ( load_ctx->rm_kernel, 0, image->data, 0,
+ load_ctx->rm_filesz );
+
+ /* Copy command line */
+ copy_to_user ( load_ctx->rm_kernel, load_ctx->rm_cmdline,
+ cmdline, cmdline_len );
+
+ return 0;
+}
- /* Prepare, verify and load the rest of the kernel */
- pm_kernel = ( ( bzhdr.loadflags & BZI_LOAD_HIGH ) ?
- phys_to_user ( 0x100000 ) : phys_to_user ( 0x10000 ) );
- pm_filesz = pm_memsz = ( image->len - rm_filesz );
- if ( ( rc = prep_segment ( pm_kernel, pm_filesz, pm_memsz ) ) != 0 ) {
+/**
+ * Load non-real-mode portion of bzImage
+ *
+ * @v image bzImage file
+ * @v load_ctx Load context
+ * @ret rc Return status code
+ */
+static int bzimage_load_non_real ( struct image *image,
+ struct bzimage_load_context *load_ctx ) {
+ int rc;
+
+ /* Prepare, verify and load the non-real-mode segment */
+ if ( ( rc = prep_segment ( load_ctx->pm_kernel, load_ctx->pm_sz,
+ load_ctx->pm_sz ) ) != 0 ) {
DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
image, strerror ( rc ) );
return rc;
}
- memcpy_user ( pm_kernel, 0, image->data, rm_filesz, pm_filesz );
+ memcpy_user ( load_ctx->pm_kernel, 0, image->data, load_ctx->rm_filesz,
+ load_ctx->pm_sz );
+
+ return 0;
+}
- /* Copy down the command line, if it exists */
- if ( image->cmdline ) {
- copy_to_user ( rm_kernel, rm_cmdline, image->cmdline,
- strlen ( image->cmdline ) + 1 );
- }
+
+/**
+ * Update and store bzImage header
+ *
+ * @v image bzImage file
+ * @v load_ctx Load context
+ * @v bzhdr Original bzImage header
+ * @ret rc Return status code
+ */
+static int bzimage_write_header ( struct image *image __unused,
+ struct bzimage_load_context *load_ctx,
+ struct bzimage_header *bzhdr ) {
+ struct bzimage_cmdline cmdline;
/* Update the header and copy it into the loaded kernel */
- bzhdr.type_of_loader = BZI_LOADER_TYPE_ETHERBOOT;
- if ( bzhdr.version >= 0x0201 ) {
- bzhdr.heap_end_ptr = ( rm_heap_end - 0x200 );
- bzhdr.loadflags |= BZI_CAN_USE_HEAP;
+ bzhdr->type_of_loader = BZI_LOADER_TYPE_ETHERBOOT;
+ if ( bzhdr->version >= 0x0201 ) {
+ bzhdr->heap_end_ptr = ( load_ctx->rm_heap - 0x200 );
+ bzhdr->loadflags |= BZI_CAN_USE_HEAP;
}
- if ( bzhdr.version >= 0x0202 ) {
- bzhdr.cmd_line_ptr = user_to_phys ( rm_kernel, rm_cmdline );
+ if ( bzhdr->version >= 0x0202 ) {
+ bzhdr->cmd_line_ptr = user_to_phys ( load_ctx->rm_kernel,
+ load_ctx->rm_cmdline );
} else {
- uint16_t cmd_line_magic = BZI_CMD_LINE_MAGIC;
- uint16_t cmd_line_offset = rm_cmdline;
-
- put_real ( cmd_line_magic, rm_kernel_seg,
- BZI_CMD_LINE_MAGIC_OFFSET );
- put_real ( cmd_line_offset, rm_kernel_seg,
- BZI_CMD_LINE_OFFSET_OFFSET );
- bzhdr.setup_move_size = rm_memsz;
+ cmdline.magic = BZI_CMDLINE_MAGIC;
+ cmdline.offset = load_ctx->rm_cmdline;
+ copy_to_user ( load_ctx->rm_kernel, BZI_CMDLINE_OFFSET,
+ &cmdline, sizeof ( cmdline ) );
+ bzhdr->setup_move_size = load_ctx->rm_memsz;
}
- copy_to_user ( rm_kernel, BZI_HDR_OFFSET, &bzhdr, sizeof ( bzhdr ) );
+ copy_to_user ( load_ctx->rm_kernel, BZI_HDR_OFFSET,
+ bzhdr, sizeof ( *bzhdr ) );
+
+ return 0;
+}
+
+/**
+ * Load bzImage image into memory
+ *
+ * @v image bzImage file
+ * @ret rc Return status code
+ */
+int bzimage_load ( struct image *image ) {
+ struct bzimage_header bzhdr;
+ struct bzimage_load_context load_ctx;
+ union {
+ struct bzimage_exec_context bz;
+ unsigned long ul;
+ } exec_ctx;
+ const char *cmdline = ( image->cmdline ? image->cmdline : "" );
+ int rc;
+
+ /* Load and verify header */
+ if ( ( rc = bzimage_load_header ( image, &load_ctx, &bzhdr ) ) != 0 )
+ return rc;
+
+ /* This is a bzImage image, valid or otherwise */
+ if ( ! image->type )
+ image->type = &bzimage_image_type;
+
+ /* Load real-mode portion */
+ if ( ( rc = bzimage_load_real ( image, &load_ctx, cmdline ) ) != 0 )
+ return rc;
+
+ /* Load non-real-mode portion */
+ if ( ( rc = bzimage_load_non_real ( image, &load_ctx ) ) != 0 )
+ return rc;
+
+ /* Update and write out header */
+ if ( ( rc = bzimage_write_header ( image, &load_ctx, &bzhdr ) ) != 0 )
+ return rc;
/* Record execution context in image private data field */
- context.rm.kernel_seg = rm_kernel_seg;
- context.rm.stack = rm_heap_end;
- image->priv.ul = context.ul;
+ exec_ctx.bz.kernel_seg = load_ctx.rm_kernel_seg;
+ exec_ctx.bz.stack = load_ctx.rm_heap;
+ image->priv.ul = exec_ctx.ul;
return 0;
}
diff --git a/src/arch/i386/include/bzimage.h b/src/arch/i386/include/bzimage.h
index 9b338034..96df180d 100644
--- a/src/arch/i386/include/bzimage.h
+++ b/src/arch/i386/include/bzimage.h
@@ -76,17 +76,30 @@ struct bzimage_header {
/** bzImage "load high" flag */
#define BZI_LOAD_HIGH 0x01
+/** Load address for high-loaded kernels */
+#define BZI_LOAD_HIGH_ADDR 0x100000
+
+/** Load address for low-loaded kernels */
+#define BZI_LOAD_LOW_ADDR 0x10000
+
/** bzImage "kernel can use heap" flag */
#define BZI_CAN_USE_HEAP 0x80
-/** bzImage command line present magic marker value */
-#define BZI_CMD_LINE_MAGIC 0xa33f
-/** bzImage command line present magic marker offset */
-#define BZI_CMD_LINE_MAGIC_OFFSET 0x20
+/** bzImage command-line structure used by older kernels */
+struct bzimage_cmdline {
+ /** Magic signature */
+ uint16_t magic;
+ /** Offset to command line */
+ uint16_t offset;
+} __attribute__ (( packed ));
+
+/** Offset of bzImage command-line structure within kernel image */
+#define BZI_CMDLINE_OFFSET 0x20
+
+/** bzImage command line present magic marker value */
+#define BZI_CMDLINE_MAGIC 0xa33f
-/** bzImage command line offset offset */
-#define BZI_CMD_LINE_OFFSET_OFFSET 0x22
/** Amount of stack space to provide */
#define BZI_STACK_SIZE 0x1000