From 3bdbfe1f003b398ae6775906747d8b5c955960ee Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 14 Jan 2007 03:49:07 +0000 Subject: [PATCH] Mostly updated. Won't work yet. --- src/arch/i386/image/nbi.c | 369 ++++++++++++++++++-------------------- 1 file changed, 171 insertions(+), 198 deletions(-) diff --git a/src/arch/i386/image/nbi.c b/src/arch/i386/image/nbi.c index 6b00f5fb..974b1b05 100644 --- a/src/arch/i386/image/nbi.c +++ b/src/arch/i386/image/nbi.c @@ -1,11 +1,10 @@ -#warning "This file is obsolete" -#if 0 - -#include "memsizes.h" -#include "realmode.h" -#include "gateA20.h" -#include "etherboot.h" -#include "errno.h" +#include +#include +#include +#include +#include +#include +#include /** @file * @@ -22,6 +21,8 @@ * */ +struct image_type nbi_image_type __image_type ( PROBE_NORMAL ); + /** * An NBI image header * @@ -82,206 +83,185 @@ struct segheader { #define NBI_LOADADDR_BEFORE 0x03 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) ) +/* Define a type for passing info to a loaded program */ +struct ebinfo { + uint8_t major, minor; /* Version */ + uint16_t flags; /* Bit flags */ +}; + /** Info passed to NBI image */ static struct ebinfo loaderinfo = { VERSION_MAJOR, VERSION_MINOR, 0 }; -/** - * Determine whether or not this is a valid NBI image - * - * @v start Address of the image - * @v len Length of the image - * @v context NBI image context - * @ret True Image is a valid NBI image - * @ret False Image is not a valid NBI image - * @err ENOEXEC Image is not a valid NBI image - * - * "context" is filled in with a context pointer suitable for passing to - * nbi_load() and nbi_boot(). - * - */ -static int nbi_probe ( physaddr_t start, off_t len, void **context ) { - static struct imgheader imgheader; - - if ( (unsigned)len < sizeof ( imgheader ) ) { - DBG ( "NBI image too small\n" ); - errno = ENOEXEC; - return 0; - } - - copy_from_phys ( &imgheader, start, sizeof ( imgheader ) ); - - if ( imgheader.magic != NBI_MAGIC ) { - errno = ENOEXEC; - return 0; - } - - /* Record image context */ - DBG ( "NBI found valid image\n" ); - *context = &imgheader; - return 1; -} - /** * Prepare a segment for an NBI image * - * @v dest Address of segment - * @v imglen Length of initialised-data portion of the segment - * @v memlen Total length of the segment + * @v image NBI image + * @v offset Offset within NBI image + * @v filesz Length of initialised-data portion of the segment + * @v memsz Total length of the segment * @v src Source for initialised data - * @ret True Segment can be used - * @ret False Segment cannot be used - * @err other As returned by prep_segment() - * + * @ret rc Return status code */ -static int nbi_prepare_segment ( physaddr_t dest, off_t imglen, off_t memlen, - physaddr_t src __unused ) { - DBG ( "NBI preparing segment [%x,%x) (imglen %d memlen %d)\n", - dest, dest + memlen, imglen, memlen ); - return prep_segment ( dest, dest + imglen, dest + memlen ); +static int nbi_prepare_segment ( struct image *image, size_t offset __unused, + userptr_t dest, size_t filesz, size_t memsz ){ + int rc; + + if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) { + DBGC ( image, "NBI %p could not prepare segment: %s\n", + image, strerror ( rc ) ); + return rc; + } + + return 0; } /** * Load a segment for an NBI image * - * @v dest Address of segment - * @v imglen Length of initialised-data portion of the segment - * @v memlen Total length of the segment + * @v image NBI image + * @v offset Offset within NBI image + * @v filesz Length of initialised-data portion of the segment + * @v memsz Total length of the segment * @v src Source for initialised data - * @ret True Always - * + * @ret rc Return status code */ -static int nbi_load_segment ( physaddr_t dest, off_t imglen, - off_t memlen __unused, physaddr_t src ) { - DBG ( "NBI loading segment [%x,%x)\n", dest, dest + imglen ); - copy_phys_to_phys ( dest, src, imglen ); - return 1; +static int nbi_load_segment ( struct image *image, size_t offset, + userptr_t dest, size_t filesz, + size_t memsz __unused ) { + memcpy_user ( dest, 0, image->data, offset, filesz ); + return 0; } /** * Process segments of an NBI image * - * @v start Address of the image - * @v len Length of the image + * @v image NBI image * @v imgheader Image header information * @v process Function to call for each segment - * @ret True All segments were processed successfully - * @ret False An error occurred processing a segment - * @err ENOEXEC Image is not a valid NBI image - * @err other As returned by the "process" function - * + * @ret rc Return status code */ -static int nbi_process_segments ( physaddr_t start, off_t len, +static int nbi_process_segments ( struct image *image, struct imgheader *imgheader, - int ( * process ) ( physaddr_t dest, - off_t imglen, - off_t memlen, - physaddr_t src ) ) { + int ( * process ) ( struct image *image, + size_t offset, + userptr_t dest, + size_t filesz, + size_t memsz ) ) { struct segheader sh; - off_t offset = 0; - off_t sh_off; - physaddr_t dest; - off_t dest_imglen, dest_memlen; + size_t offset = 0; + size_t sh_off; + userptr_t dest; + size_t filesz; + size_t memsz; + int rc; - /* Copy header to target location */ - dest = ( ( imgheader->location.segment << 4 ) + - imgheader->location.offset ); - dest_imglen = dest_memlen = NBI_HEADER_LENGTH; - if ( ! process ( dest, dest_imglen, dest_memlen, start + offset ) ) - return 0; - offset += dest_imglen; + /* Copy image header to target location */ + dest = real_to_user ( imgheader->location.segment, + imgheader->location.offset ); + filesz = memsz = NBI_HEADER_LENGTH; + if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 ) + return rc; + offset += filesz; /* Process segments in turn */ sh_off = NBI_LENGTH ( imgheader->length ); do { /* Read segment header */ - copy_from_phys ( &sh, start + sh_off, sizeof ( sh ) ); + copy_from_user ( &sh, image->data, sh_off, sizeof ( sh ) ); if ( sh.length == 0 ) { /* Avoid infinite loop? */ - DBG ( "NBI invalid segheader length 0\n" ); - errno = ENOEXEC; - return 0; + DBGC ( image, "NBI %p invalid segheader length 0\n", + image ); + return -ENOEXEC; } /* Calculate segment load address */ switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) { case NBI_LOADADDR_ABS: - dest = sh.loadaddr; + dest = phys_to_user ( sh.loadaddr ); break; case NBI_LOADADDR_AFTER: - dest = dest + dest_memlen + sh.loadaddr; + dest = userptr_add ( dest, memsz + sh.loadaddr ); break; case NBI_LOADADDR_BEFORE: - dest = dest - sh.loadaddr; + dest = userptr_add ( dest, -sh.loadaddr ); break; case NBI_LOADADDR_END: /* Not correct according to the spec, but * maintains backwards compatibility with * previous versions of Etherboot. */ - dest = ( meminfo.memsize * 1024 + 0x100000UL ) - - sh.loadaddr; + dest = phys_to_user ( ( extmemsize() + 1024 ) * 1024 + - sh.loadaddr ); break; default: /* Cannot be reached */ - DBG ( "NBI can't count up to three!\n" ); + assert ( 0 ); } /* Process this segment */ - dest_imglen = sh.imglength; - dest_memlen = sh.memlength; - if ( ! process ( dest, dest_imglen, dest_memlen, - start + offset ) ) - return 0; - offset += dest_imglen; + filesz = sh.imglength; + memsz = sh.memlength; + if ( ( offset + filesz ) > image->len ) { + DBGC ( image, "NBI %p segment outside file\n", image ); + return -ENOEXEC; + } + if ( ( rc = process ( image, offset, dest, + filesz, memsz ) ) != 0 ) { + return rc; + } + offset += filesz; /* Next segheader */ sh_off += NBI_LENGTH ( sh.length ); if ( sh_off >= NBI_HEADER_LENGTH ) { - DBG ( "NBI header overflow\n" ); - errno = ENOEXEC; - return 0; + DBGC ( image, "NBI %p header overflow\n", image ); + return -ENOEXEC; } } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) ); - if ( offset != len ) { - DBG ( "NBI length mismatch (file %d, metadata %d)\n", - len, offset ); - errno = ENOEXEC; - return 0; + if ( offset != image->len ) { + DBGC ( image, "NBI %p length wrong (file %d, metadata %d)\n", + image, image->len, offset ); + return -ENOEXEC; } - return 1; + return 0; } /** * Load an NBI image into memory * - * @v start Address of image - * @v len Length of image - * @v context NBI context (as returned by nbi_probe()) - * @ret True Image loaded into memory - * @ret False Image not loaded into memory - * @err ENOEXEC Image is not a valid NBI image - * @err other As returned by nbi_process_segments() - * @err other As returned by nbi_prepare_segment() - * @err other As returned by nbi_load_segment() - * + * @v image NBI image + * @ret rc Return status code */ -static int nbi_load ( physaddr_t start, off_t len, void *context ) { - struct imgheader *imgheader = context; +int nbi_load ( struct image *image ) { + struct imgheader imgheader; + int rc; /* If we don't have enough data give up */ - if ( len < NBI_HEADER_LENGTH ) { - errno = ENOEXEC; - return 0; + if ( image->len < NBI_HEADER_LENGTH ) { + DBGC ( image, "NBI %p too short for an NBI image\n", image ); + return -ENOEXEC; } - - DBG ( "NBI placing header at %hx:%hx\n", - imgheader->location.segment, imgheader->location.offset ); + + /* Check image header */ + copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) ); + if ( imgheader.magic != NBI_MAGIC ) { + DBGC ( image, "NBI %p has no NBI signature\n", image ); + return -ENOEXEC; + } + + /* This is an NBI image, valid or otherwise */ + if ( ! image->type ) + image->type = &nbi_image_type; + + DBGC ( image, "NBI %p placing header at %hx:%hx\n", image, + imgheader.location.segment, imgheader.location.offset ); /* NBI files can have overlaps between segments; the bss of * one segment may overlap the initialised data of another. I @@ -290,14 +270,14 @@ static int nbi_load ( physaddr_t start, off_t len, void *context ) { * passes: first to initialise the segments, then to copy the * data. This avoids zeroing out already-copied data. */ - if ( ! nbi_process_segments ( start, len, imgheader, - nbi_prepare_segment ) ) - return 0; - if ( ! nbi_process_segments ( start, len, imgheader, - nbi_load_segment ) ) - return 0; + if ( ( rc = nbi_process_segments ( image, &imgheader, + nbi_prepare_segment ) ) != 0 ) + return rc; + if ( ( rc = nbi_process_segments ( image, &imgheader, + nbi_load_segment ) ) != 0 ) + return rc; - return 1; + return 0; } /** @@ -309,40 +289,34 @@ static int nbi_load ( physaddr_t start, off_t len, void *context ) { * @err ECANCELED NBI program returned * */ -static int nbi_boot16 ( struct imgheader *imgheader ) { - uint16_t basemem_bootp; +static int nbi_boot16 ( struct image *image, struct imgheader *imgheader ) { int discard_D, discard_S, discard_b; - DBG ( "NBI executing 16-bit image at %hx:%hx\n", - imgheader->execaddr.segoff.segment, - imgheader->execaddr.segoff.offset ); + DBGC ( image, "NBI %p executing 16-bit image at %04x:%04x\n", image, + imgheader->execaddr.segoff.segment, + imgheader->execaddr.segoff.offset ); gateA20_unset(); - basemem_bootp = BASEMEM_PARAMETER_INIT ( bootp_data ); - REAL_EXEC ( rm_xstart16, - "pushw %%ds\n\t" /* far pointer to bootp data copy */ - "pushw %%bx\n\t" - "pushl %%esi\n\t" /* location */ - "pushw %%cs\n\t" /* lcall execaddr */ - "call 1f\n\t" - "jmp 2f\n\t" - "\n1:\n\t" - "pushl %%edi\n\t" - "lret\n\t" - "\n2:\n\t" - "addw $8,%%sp\n\t", /* pop location and bootp ptr */ - 3, - OUT_CONSTRAINTS ( "=D" ( discard_D ), "=S" ( discard_S ), - "=b" ( discard_b ) ), - IN_CONSTRAINTS ( "D" ( imgheader->execaddr.segoff ), - "S" ( imgheader->location ), - "b" ( basemem_bootp ) ), - CLOBBER ( "eax", "ecx", "edx", "ebp" ) ); - BASEMEM_PARAMETER_DONE ( bootp_data ); +#warning "Should be providing bootp data" + __asm__ __volatile__ ( + REAL_CODE ( "pushw %%ds\n\t" /* far pointer to bootp data */ + "pushw %%bx\n\t" + "pushl %%esi\n\t" /* location */ + "pushw %%cs\n\t" /* lcall execaddr */ + "call 1f\n\t" + "jmp 2f\n\t" + "\n1:\n\t" + "pushl %%edi\n\t" + "lret\n\t" + "\n2:\n\t" + "addw $8,%%sp\n\t" /* clean up stack */ ) + : "=D" ( discard_D ), "=S" ( discard_S ), "=b" ( discard_b ) + : "D" ( imgheader->execaddr.segoff ), + "S" ( imgheader->location ), "b" ( 0 /* bootp data */ ) + : "eax", "ecx", "edx", "ebp" ); - errno = ECANCELED; - return 0; + return -ECANCELED; } /** @@ -359,57 +333,56 @@ static int nbi_boot16 ( struct imgheader *imgheader ) { * have returned. * */ -static int nbi_boot32 ( struct imgheader *imgheader ) { - int rc = 0; +static int nbi_boot32 ( struct image *image, struct imgheader *imgheader ) { + int rc; - DBG ( "NBI executing 32-bit image at %x\n", - imgheader->execaddr.linear ); + DBGC ( image, "NBI %p executing 32-bit image at %lx\n", + image, imgheader->execaddr.linear ); /* no gateA20_unset for PM call */ - errno = ENOERR; + +#warning "Should be providing bootp data" +#warning "xstart32 no longer exists" +#if 0 rc = xstart32 ( imgheader->execaddr.linear, virt_to_phys ( &loaderinfo ), ( ( imgheader->location.segment << 4 ) + imgheader->location.offset ), - virt_to_phys ( &bootp_data ) ); - printf ( "Secondary program returned %d\n", rc ); + 0 /* bootp data */ ); +#endif + + DBGC ( image, "NBI %p returned %d\n", image, rc ); + if ( ! NBI_PROGRAM_RETURNS ( imgheader->flags ) ) { /* We shouldn't have returned */ - errno = ECANCELED; - rc = 0; + rc = -ECANCELED; } return rc; } /** - * Boot a loaded NBI image - * - * @v context NBI context (as returned by nbi_probe()) - * @ret Never NBI program booted successfully - * @ret False NBI program should not have returned - * @ret other As returned by NBI program - * @err ECANCELED NBI program should not have returned - * - * See also nbi_boot16() and nbi_boot32(). + * Execute a loaded NBI image * + * @v image NBI image + * @ret rc Return status code */ -static int nbi_boot ( void *context ) { - struct imgheader *imgheader = context; +static int nbi_exec ( struct image *image ) { + struct imgheader imgheader; - if ( NBI_LINEAR_EXEC_ADDR ( imgheader->flags ) ) { - return nbi_boot32 ( imgheader ); + copy_from_user ( &imgheader, phys_to_user ( image->entry ), 0, + sizeof ( imgheader ) ); + + if ( NBI_LINEAR_EXEC_ADDR ( imgheader.flags ) ) { + return nbi_boot32 ( image, &imgheader ); } else { - return nbi_boot16 ( imgheader ); + return nbi_boot16 ( image, &imgheader ); } } -/** Declaration of the NBI image format */ -struct image nbi_image __image = { - .name = "NBI", - .probe = nbi_probe, - .load = nbi_load, - .boot = nbi_boot, +/** NBI image type */ +struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = { + .name = "NBI", + .load = nbi_load, + .exec = nbi_exec, }; - -#endif