123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #include "image.h"
- #include "memsizes.h"
- #include "realmode.h"
- #include "gateA20.h"
- #include "osloader.h"
- #include "etherboot.h"
- #include "errno.h"
-
-
-
-
- struct imgheader {
- unsigned long magic;
- union {
- unsigned char length;
- unsigned long flags;
- };
- segoff_t location;
- union {
- segoff_t segoff;
- unsigned long linear;
- } execaddr;
- } __attribute__ (( packed ));
-
-
- #define NBI_MAGIC 0x1B031336UL
-
-
- #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
- #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
- #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
-
-
- #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
- #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
-
-
- #define NBI_HEADER_LENGTH 512
-
-
- struct segheader {
- unsigned char length;
- unsigned char vendortag;
- unsigned char reserved;
- unsigned char flags;
- unsigned long loadaddr;
- unsigned long imglength;
- unsigned long memlength;
- };
-
-
- #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
- #define NBI_LOADADDR_ABS 0x00
- #define NBI_LOADADDR_AFTER 0x01
- #define NBI_LOADADDR_END 0x02
- #define NBI_LOADADDR_BEFORE 0x03
- #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
-
-
- static struct ebinfo loaderinfo = {
- VERSION_MAJOR, VERSION_MINOR,
- 0
- };
-
-
- 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;
- }
-
-
- DBG ( "NBI found valid image\n" );
- *context = &imgheader;
- return 1;
- }
-
-
- 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_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_process_segments ( physaddr_t start, off_t len,
- struct imgheader *imgheader,
- int ( * process ) ( physaddr_t dest,
- off_t imglen,
- off_t memlen,
- physaddr_t src ) ) {
- struct segheader sh;
- off_t offset = 0;
- off_t sh_off;
- physaddr_t dest;
- off_t dest_imglen, dest_memlen;
-
-
- 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;
-
-
- sh_off = NBI_LENGTH ( imgheader->length );
- do {
-
- copy_from_phys ( &sh, start + sh_off, sizeof ( sh ) );
- if ( sh.length == 0 ) {
-
- DBG ( "NBI invalid segheader length 0\n" );
- errno = ENOEXEC;
- return 0;
- }
-
-
- switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
- case NBI_LOADADDR_ABS:
- dest = sh.loadaddr;
- break;
- case NBI_LOADADDR_AFTER:
- dest = dest + dest_memlen + sh.loadaddr;
- break;
- case NBI_LOADADDR_BEFORE:
- dest = dest - sh.loadaddr;
- break;
- case NBI_LOADADDR_END:
-
-
- dest = ( meminfo.memsize * 1024 + 0x100000UL )
- - sh.loadaddr;
- break;
- default:
-
- DBG ( "NBI can't count up to three!\n" );
- }
-
-
- dest_imglen = sh.imglength;
- dest_memlen = sh.memlength;
- if ( ! process ( dest, dest_imglen, dest_memlen,
- start + offset ) )
- return 0;
- offset += dest_imglen;
-
-
- sh_off += NBI_LENGTH ( sh.length );
- if ( sh_off >= NBI_HEADER_LENGTH ) {
- DBG ( "NBI header overflow\n" );
- errno = ENOEXEC;
- return 0;
- }
-
- } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
-
- if ( offset != len ) {
- DBG ( "NBI length mismatch (file %d, metadata %d)\n",
- len, offset );
- errno = ENOEXEC;
- return 0;
- }
-
- return 1;
- }
-
-
- static int nbi_load ( physaddr_t start, off_t len, void *context ) {
- struct imgheader *imgheader = context;
-
-
- if ( len < NBI_HEADER_LENGTH ) {
- errno = ENOEXEC;
- return 0;
- }
-
- DBG ( "NBI placing header at %hx:%hx\n",
- imgheader->location.segment, imgheader->location.offset );
-
-
-
- if ( ! nbi_process_segments ( start, len, imgheader,
- nbi_prepare_segment ) )
- return 0;
- if ( ! nbi_process_segments ( start, len, imgheader,
- nbi_load_segment ) )
- return 0;
-
- return 1;
- }
-
-
- static int nbi_boot16 ( struct imgheader *imgheader ) {
- uint16_t basemem_bootp;
- 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 );
-
- gateA20_unset();
-
- basemem_bootp = BASEMEM_PARAMETER_INIT ( bootp_data );
- REAL_EXEC ( rm_xstart16,
- "pushw %%ds\n\t"
- "pushw %%bx\n\t"
- "pushl %%esi\n\t"
- "pushw %%cs\n\t"
- "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",
- 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 );
-
- errno = ECANCELED;
- return 0;
- }
-
-
- static int nbi_boot32 ( struct imgheader *imgheader ) {
- int rc = 0;
-
- DBG ( "NBI executing 32-bit image at %x\n",
- imgheader->execaddr.linear );
-
-
- errno = ENOERR;
- 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 );
- if ( ! NBI_PROGRAM_RETURNS ( imgheader->flags ) ) {
-
- errno = ECANCELED;
- rc = 0;
- }
-
- return rc;
- }
-
-
- static int nbi_boot ( void *context ) {
- struct imgheader *imgheader = context;
-
- if ( NBI_LINEAR_EXEC_ADDR ( imgheader->flags ) ) {
- return nbi_boot32 ( imgheader );
- } else {
- return nbi_boot16 ( imgheader );
- }
- }
-
-
- struct image nbi_image __image = {
- .name = "NBI",
- .probe = nbi_probe,
- .load = nbi_load,
- .boot = nbi_boot,
- };
|