6 #include <basemem_packet.h>
7 #include <gpxe/uaccess.h>
8 #include <gpxe/segment.h>
10 #include <gpxe/netdevice.h>
11 #include <gpxe/dhcp.h>
12 #include <gpxe/image.h>
13 #include <gpxe/features.h>
19 * The Net Boot Image format is defined by the "Draft Net Boot Image
20 * Proposal 0.3" by Jamie Honan, Gero Kuhlmann and Ken Yap. It is now
21 * considered to be a legacy format, but it still included because a
22 * large amount of software (e.g. nymph, LTSP) makes use of NBI files.
24 * Etherboot does not implement the INT 78 callback interface
25 * described by the NBI specification. For a callback interface on
26 * x86 architecture, use PXE.
30 FEATURE ( FEATURE_IMAGE, "NBI", DHCP_EB_FEATURE_NBI, 1 );
32 struct image_type nbi_image_type __image_type ( PROBE_NORMAL );
37 * Note that the length field uses a peculiar encoding; use the
38 * NBI_LENGTH() macro to decode the actual header length.
42 unsigned long magic; /**< Magic number (NBI_MAGIC) */
44 unsigned char length; /**< Nibble-coded header length */
45 unsigned long flags; /**< Image flags */
47 segoff_t location; /**< 16-bit seg:off header location */
49 segoff_t segoff; /**< 16-bit seg:off entry point */
50 unsigned long linear; /**< 32-bit entry point */
52 } __attribute__ (( packed ));
54 /** NBI magic number */
55 #define NBI_MAGIC 0x1B031336UL
57 /* Interpretation of the "length" fields */
58 #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
59 #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
60 #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
62 /* Interpretation of the "flags" fields */
63 #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
64 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
66 /** NBI header length */
67 #define NBI_HEADER_LENGTH 512
70 * An NBI segment header
72 * Note that the length field uses a peculiar encoding; use the
73 * NBI_LENGTH() macro to decode the actual header length.
77 unsigned char length; /**< Nibble-coded header length */
78 unsigned char vendortag; /**< Vendor-defined private tag */
79 unsigned char reserved;
80 unsigned char flags; /**< Segment flags */
81 unsigned long loadaddr; /**< Load address */
82 unsigned long imglength; /**< Segment length in NBI file */
83 unsigned long memlength; /**< Segment length in memory */
86 /* Interpretation of the "flags" fields */
87 #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
88 #define NBI_LOADADDR_ABS 0x00
89 #define NBI_LOADADDR_AFTER 0x01
90 #define NBI_LOADADDR_END 0x02
91 #define NBI_LOADADDR_BEFORE 0x03
92 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
94 /* Define a type for passing info to a loaded program */
96 uint8_t major, minor; /* Version */
97 uint16_t flags; /* Bit flags */
100 /** Info passed to NBI image */
101 static struct ebinfo loaderinfo = {
102 VERSION_MAJOR, VERSION_MINOR,
107 * Prepare a segment for an NBI image
110 * @v offset Offset within NBI image
111 * @v filesz Length of initialised-data portion of the segment
112 * @v memsz Total length of the segment
113 * @v src Source for initialised data
114 * @ret rc Return status code
116 static int nbi_prepare_segment ( struct image *image, size_t offset __unused,
117 userptr_t dest, size_t filesz, size_t memsz ){
120 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
121 DBGC ( image, "NBI %p could not prepare segment: %s\n",
122 image, strerror ( rc ) );
130 * Load a segment for an NBI image
133 * @v offset Offset within NBI image
134 * @v filesz Length of initialised-data portion of the segment
135 * @v memsz Total length of the segment
136 * @v src Source for initialised data
137 * @ret rc Return status code
139 static int nbi_load_segment ( struct image *image, size_t offset,
140 userptr_t dest, size_t filesz,
141 size_t memsz __unused ) {
142 memcpy_user ( dest, 0, image->data, offset, filesz );
147 * Process segments of an NBI image
150 * @v imgheader Image header information
151 * @v process Function to call for each segment
152 * @ret rc Return status code
154 static int nbi_process_segments ( struct image *image,
155 struct imgheader *imgheader,
156 int ( * process ) ( struct image *image,
169 /* Copy image header to target location */
170 dest = real_to_user ( imgheader->location.segment,
171 imgheader->location.offset );
172 filesz = memsz = NBI_HEADER_LENGTH;
173 if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 )
177 /* Process segments in turn */
178 sh_off = NBI_LENGTH ( imgheader->length );
180 /* Read segment header */
181 copy_from_user ( &sh, image->data, sh_off, sizeof ( sh ) );
182 if ( sh.length == 0 ) {
183 /* Avoid infinite loop? */
184 DBGC ( image, "NBI %p invalid segheader length 0\n",
189 /* Calculate segment load address */
190 switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
191 case NBI_LOADADDR_ABS:
192 dest = phys_to_user ( sh.loadaddr );
194 case NBI_LOADADDR_AFTER:
195 dest = userptr_add ( dest, memsz + sh.loadaddr );
197 case NBI_LOADADDR_BEFORE:
198 dest = userptr_add ( dest, -sh.loadaddr );
200 case NBI_LOADADDR_END:
201 /* Not correct according to the spec, but
202 * maintains backwards compatibility with
203 * previous versions of Etherboot.
205 dest = phys_to_user ( ( extmemsize() + 1024 ) * 1024
209 /* Cannot be reached */
213 /* Process this segment */
214 filesz = sh.imglength;
215 memsz = sh.memlength;
216 if ( ( offset + filesz ) > image->len ) {
217 DBGC ( image, "NBI %p segment outside file\n", image );
220 if ( ( rc = process ( image, offset, dest,
221 filesz, memsz ) ) != 0 ) {
227 sh_off += NBI_LENGTH ( sh.length );
228 if ( sh_off >= NBI_HEADER_LENGTH ) {
229 DBGC ( image, "NBI %p header overflow\n", image );
233 } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
235 if ( offset != image->len ) {
236 DBGC ( image, "NBI %p length wrong (file %d, metadata %d)\n",
237 image, image->len, offset );
245 * Load an NBI image into memory
248 * @ret rc Return status code
250 static int nbi_load ( struct image *image ) {
251 struct imgheader imgheader;
254 /* If we don't have enough data give up */
255 if ( image->len < NBI_HEADER_LENGTH ) {
256 DBGC ( image, "NBI %p too short for an NBI image\n", image );
260 /* Check image header */
261 copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) );
262 if ( imgheader.magic != NBI_MAGIC ) {
263 DBGC ( image, "NBI %p has no NBI signature\n", image );
267 /* This is an NBI image, valid or otherwise */
269 image->type = &nbi_image_type;
271 DBGC ( image, "NBI %p placing header at %hx:%hx\n", image,
272 imgheader.location.segment, imgheader.location.offset );
274 /* NBI files can have overlaps between segments; the bss of
275 * one segment may overlap the initialised data of another. I
276 * assume this is a design flaw, but there are images out
277 * there that we need to work with. We therefore do two
278 * passes: first to initialise the segments, then to copy the
279 * data. This avoids zeroing out already-copied data.
281 if ( ( rc = nbi_process_segments ( image, &imgheader,
282 nbi_prepare_segment ) ) != 0 )
284 if ( ( rc = nbi_process_segments ( image, &imgheader,
285 nbi_load_segment ) ) != 0 )
288 /* Record header address in image private data field */
289 image->priv.user = real_to_user ( imgheader.location.segment,
290 imgheader.location.offset );
296 * Boot a 16-bit NBI image
298 * @v imgheader Image header information
299 * @ret rc Return status code, if image returns
301 static int nbi_boot16 ( struct image *image, struct imgheader *imgheader ) {
302 int discard_D, discard_S, discard_b;
305 DBGC ( image, "NBI %p executing 16-bit image at %04x:%04x\n", image,
306 imgheader->execaddr.segoff.segment,
307 imgheader->execaddr.segoff.offset );
311 __asm__ __volatile__ (
312 REAL_CODE ( "pushw %%ds\n\t" /* far pointer to bootp data */
314 "pushl %%esi\n\t" /* location */
315 "pushw %%cs\n\t" /* lcall execaddr */
322 "addw $8,%%sp\n\t" /* clean up stack */ )
323 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
325 : "D" ( imgheader->execaddr.segoff ),
326 "S" ( imgheader->location ),
327 "b" ( __from_data16 ( basemem_packet ) )
328 : "ecx", "edx", "ebp" );
336 * Boot a 32-bit NBI image
338 * @v imgheader Image header information
339 * @ret rc Return status code, if image returns
341 static int nbi_boot32 ( struct image *image, struct imgheader *imgheader ) {
342 int discard_D, discard_S, discard_b;
345 DBGC ( image, "NBI %p executing 32-bit image at %lx\n",
346 image, imgheader->execaddr.linear );
348 /* no gateA20_unset for PM call */
350 /* Jump to OS with flat physical addressing */
351 __asm__ __volatile__ (
352 PHYS_CODE ( "pushl %%ebx\n\t" /* bootp data */
353 "pushl %%esi\n\t" /* imgheader */
354 "pushl %%eax\n\t" /* loaderinfo */
356 "addl $12, %%esp\n\t" /* clean up stack */ )
357 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
359 : "D" ( imgheader->execaddr.linear ),
360 "S" ( ( imgheader->location.segment << 4 ) +
361 imgheader->location.offset ),
362 "b" ( virt_to_phys ( basemem_packet ) ),
363 "a" ( virt_to_phys ( &loaderinfo ) )
364 : "ecx", "edx", "ebp", "memory" );
370 * Guess boot network device
372 * @ret netdev Boot network device
374 static struct net_device * guess_boot_netdev ( void ) {
375 struct net_device *boot_netdev;
377 /* Just use the first network device */
378 for_each_netdev ( boot_netdev ) {
386 * Prepare DHCP parameter block for NBI image
389 * @ret rc Return status code
391 static int nbi_prepare_dhcp ( struct image *image ) {
392 struct dhcp_packet dhcppkt;
393 struct net_device *boot_netdev;
396 boot_netdev = guess_boot_netdev();
397 if ( ! boot_netdev ) {
398 DBGC ( image, "NBI %p could not identify a network device\n",
403 if ( ( rc = create_dhcp_response ( boot_netdev, DHCPACK, NULL,
405 sizeof ( basemem_packet ),
406 &dhcppkt ) ) != 0 ) {
407 DBGC ( image, "NBI %p failed to build DHCP packet\n", image );
415 * Execute a loaded NBI image
418 * @ret rc Return status code
420 static int nbi_exec ( struct image *image ) {
421 struct imgheader imgheader;
425 copy_from_user ( &imgheader, image->priv.user, 0,
426 sizeof ( imgheader ) );
428 /* Prepare DHCP option block */
429 if ( ( rc = nbi_prepare_dhcp ( image ) ) != 0 )
432 /* Shut down now if NBI image will not return */
433 may_return = NBI_PROGRAM_RETURNS ( imgheader.flags );
437 /* Execute NBI image */
438 if ( NBI_LINEAR_EXEC_ADDR ( imgheader.flags ) ) {
439 rc = nbi_boot32 ( image, &imgheader );
441 rc = nbi_boot16 ( image, &imgheader );
444 if ( ! may_return ) {
445 /* Cannot continue after shutdown() called */
446 DBGC ( image, "NBI %p returned %d from non-returnable image\n",
451 DBGC ( image, "NBI %p returned %d\n", image, rc );
456 /** NBI image type */
457 struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {