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/dhcppkt.h>
13 #include <gpxe/image.h>
14 #include <gpxe/features.h>
20 * The Net Boot Image format is defined by the "Draft Net Boot Image
21 * Proposal 0.3" by Jamie Honan, Gero Kuhlmann and Ken Yap. It is now
22 * considered to be a legacy format, but it still included because a
23 * large amount of software (e.g. nymph, LTSP) makes use of NBI files.
25 * Etherboot does not implement the INT 78 callback interface
26 * described by the NBI specification. For a callback interface on
27 * x86 architecture, use PXE.
31 FEATURE ( FEATURE_IMAGE, "NBI", DHCP_EB_FEATURE_NBI, 1 );
33 struct image_type nbi_image_type __image_type ( PROBE_NORMAL );
38 * Note that the length field uses a peculiar encoding; use the
39 * NBI_LENGTH() macro to decode the actual header length.
43 unsigned long magic; /**< Magic number (NBI_MAGIC) */
45 unsigned char length; /**< Nibble-coded header length */
46 unsigned long flags; /**< Image flags */
48 segoff_t location; /**< 16-bit seg:off header location */
50 segoff_t segoff; /**< 16-bit seg:off entry point */
51 unsigned long linear; /**< 32-bit entry point */
53 } __attribute__ (( packed ));
55 /** NBI magic number */
56 #define NBI_MAGIC 0x1B031336UL
58 /* Interpretation of the "length" fields */
59 #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
60 #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
61 #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
63 /* Interpretation of the "flags" fields */
64 #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
65 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
67 /** NBI header length */
68 #define NBI_HEADER_LENGTH 512
71 * An NBI segment header
73 * Note that the length field uses a peculiar encoding; use the
74 * NBI_LENGTH() macro to decode the actual header length.
78 unsigned char length; /**< Nibble-coded header length */
79 unsigned char vendortag; /**< Vendor-defined private tag */
80 unsigned char reserved;
81 unsigned char flags; /**< Segment flags */
82 unsigned long loadaddr; /**< Load address */
83 unsigned long imglength; /**< Segment length in NBI file */
84 unsigned long memlength; /**< Segment length in memory */
87 /* Interpretation of the "flags" fields */
88 #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
89 #define NBI_LOADADDR_ABS 0x00
90 #define NBI_LOADADDR_AFTER 0x01
91 #define NBI_LOADADDR_END 0x02
92 #define NBI_LOADADDR_BEFORE 0x03
93 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
95 /* Define a type for passing info to a loaded program */
97 uint8_t major, minor; /* Version */
98 uint16_t flags; /* Bit flags */
101 /** Info passed to NBI image */
102 static struct ebinfo loaderinfo = {
103 VERSION_MAJOR, VERSION_MINOR,
108 * Prepare a segment for an NBI image
111 * @v offset Offset within NBI image
112 * @v filesz Length of initialised-data portion of the segment
113 * @v memsz Total length of the segment
114 * @v src Source for initialised data
115 * @ret rc Return status code
117 static int nbi_prepare_segment ( struct image *image, size_t offset __unused,
118 userptr_t dest, size_t filesz, size_t memsz ){
121 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
122 DBGC ( image, "NBI %p could not prepare segment: %s\n",
123 image, strerror ( rc ) );
131 * Load a segment for an NBI image
134 * @v offset Offset within NBI image
135 * @v filesz Length of initialised-data portion of the segment
136 * @v memsz Total length of the segment
137 * @v src Source for initialised data
138 * @ret rc Return status code
140 static int nbi_load_segment ( struct image *image, size_t offset,
141 userptr_t dest, size_t filesz,
142 size_t memsz __unused ) {
143 memcpy_user ( dest, 0, image->data, offset, filesz );
148 * Process segments of an NBI image
151 * @v imgheader Image header information
152 * @v process Function to call for each segment
153 * @ret rc Return status code
155 static int nbi_process_segments ( struct image *image,
156 struct imgheader *imgheader,
157 int ( * process ) ( struct image *image,
170 /* Copy image header to target location */
171 dest = real_to_user ( imgheader->location.segment,
172 imgheader->location.offset );
173 filesz = memsz = NBI_HEADER_LENGTH;
174 if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 )
178 /* Process segments in turn */
179 sh_off = NBI_LENGTH ( imgheader->length );
181 /* Read segment header */
182 copy_from_user ( &sh, image->data, sh_off, sizeof ( sh ) );
183 if ( sh.length == 0 ) {
184 /* Avoid infinite loop? */
185 DBGC ( image, "NBI %p invalid segheader length 0\n",
190 /* Calculate segment load address */
191 switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
192 case NBI_LOADADDR_ABS:
193 dest = phys_to_user ( sh.loadaddr );
195 case NBI_LOADADDR_AFTER:
196 dest = userptr_add ( dest, memsz + sh.loadaddr );
198 case NBI_LOADADDR_BEFORE:
199 dest = userptr_add ( dest, -sh.loadaddr );
201 case NBI_LOADADDR_END:
202 /* Not correct according to the spec, but
203 * maintains backwards compatibility with
204 * previous versions of Etherboot.
206 dest = phys_to_user ( ( extmemsize() + 1024 ) * 1024
210 /* Cannot be reached */
214 /* Process this segment */
215 filesz = sh.imglength;
216 memsz = sh.memlength;
217 if ( ( offset + filesz ) > image->len ) {
218 DBGC ( image, "NBI %p segment outside file\n", image );
221 if ( ( rc = process ( image, offset, dest,
222 filesz, memsz ) ) != 0 ) {
228 sh_off += NBI_LENGTH ( sh.length );
229 if ( sh_off >= NBI_HEADER_LENGTH ) {
230 DBGC ( image, "NBI %p header overflow\n", image );
234 } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
236 if ( offset != image->len ) {
237 DBGC ( image, "NBI %p length wrong (file %zd, metadata %zd)\n",
238 image, image->len, offset );
246 * Load an NBI image into memory
249 * @ret rc Return status code
251 static int nbi_load ( struct image *image ) {
252 struct imgheader imgheader;
255 /* If we don't have enough data give up */
256 if ( image->len < NBI_HEADER_LENGTH ) {
257 DBGC ( image, "NBI %p too short for an NBI image\n", image );
261 /* Check image header */
262 copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) );
263 if ( imgheader.magic != NBI_MAGIC ) {
264 DBGC ( image, "NBI %p has no NBI signature\n", image );
268 /* This is an NBI image, valid or otherwise */
270 image->type = &nbi_image_type;
272 DBGC ( image, "NBI %p placing header at %hx:%hx\n", image,
273 imgheader.location.segment, imgheader.location.offset );
275 /* NBI files can have overlaps between segments; the bss of
276 * one segment may overlap the initialised data of another. I
277 * assume this is a design flaw, but there are images out
278 * there that we need to work with. We therefore do two
279 * passes: first to initialise the segments, then to copy the
280 * data. This avoids zeroing out already-copied data.
282 if ( ( rc = nbi_process_segments ( image, &imgheader,
283 nbi_prepare_segment ) ) != 0 )
285 if ( ( rc = nbi_process_segments ( image, &imgheader,
286 nbi_load_segment ) ) != 0 )
289 /* Record header address in image private data field */
290 image->priv.user = real_to_user ( imgheader.location.segment,
291 imgheader.location.offset );
297 * Boot a 16-bit NBI image
299 * @v imgheader Image header information
300 * @ret rc Return status code, if image returns
302 static int nbi_boot16 ( struct image *image, struct imgheader *imgheader ) {
303 int discard_D, discard_S, discard_b;
306 DBGC ( image, "NBI %p executing 16-bit image at %04x:%04x\n", image,
307 imgheader->execaddr.segoff.segment,
308 imgheader->execaddr.segoff.offset );
312 __asm__ __volatile__ (
313 REAL_CODE ( "pushw %%ds\n\t" /* far pointer to bootp data */
315 "pushl %%esi\n\t" /* location */
316 "pushw %%cs\n\t" /* lcall execaddr */
323 "addw $8,%%sp\n\t" /* clean up stack */ )
324 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
326 : "D" ( imgheader->execaddr.segoff ),
327 "S" ( imgheader->location ),
328 "b" ( __from_data16 ( basemem_packet ) )
329 : "ecx", "edx", "ebp" );
337 * Boot a 32-bit NBI image
339 * @v imgheader Image header information
340 * @ret rc Return status code, if image returns
342 static int nbi_boot32 ( struct image *image, struct imgheader *imgheader ) {
343 int discard_D, discard_S, discard_b;
346 DBGC ( image, "NBI %p executing 32-bit image at %lx\n",
347 image, imgheader->execaddr.linear );
349 /* no gateA20_unset for PM call */
351 /* Jump to OS with flat physical addressing */
352 __asm__ __volatile__ (
353 PHYS_CODE ( "pushl %%ebx\n\t" /* bootp data */
354 "pushl %%esi\n\t" /* imgheader */
355 "pushl %%eax\n\t" /* loaderinfo */
357 "addl $12, %%esp\n\t" /* clean up stack */ )
358 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
360 : "D" ( imgheader->execaddr.linear ),
361 "S" ( ( imgheader->location.segment << 4 ) +
362 imgheader->location.offset ),
363 "b" ( virt_to_phys ( basemem_packet ) ),
364 "a" ( virt_to_phys ( &loaderinfo ) )
365 : "ecx", "edx", "ebp", "memory" );
371 * Guess boot network device
373 * @ret netdev Boot network device
375 static struct net_device * guess_boot_netdev ( void ) {
376 struct net_device *boot_netdev;
378 /* Just use the first network device */
379 for_each_netdev ( boot_netdev ) {
387 * Prepare DHCP parameter block for NBI image
390 * @ret rc Return status code
392 static int nbi_prepare_dhcp ( struct image *image ) {
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_dhcpack ( boot_netdev, basemem_packet,
404 sizeof ( basemem_packet ) ) ) != 0 ) {
405 DBGC ( image, "NBI %p failed to build DHCP packet\n", image );
413 * Execute a loaded NBI image
416 * @ret rc Return status code
418 static int nbi_exec ( struct image *image ) {
419 struct imgheader imgheader;
423 copy_from_user ( &imgheader, image->priv.user, 0,
424 sizeof ( imgheader ) );
426 /* Prepare DHCP option block */
427 if ( ( rc = nbi_prepare_dhcp ( image ) ) != 0 )
430 /* Shut down now if NBI image will not return */
431 may_return = NBI_PROGRAM_RETURNS ( imgheader.flags );
435 /* Execute NBI image */
436 if ( NBI_LINEAR_EXEC_ADDR ( imgheader.flags ) ) {
437 rc = nbi_boot32 ( image, &imgheader );
439 rc = nbi_boot16 ( image, &imgheader );
442 if ( ! may_return ) {
443 /* Cannot continue after shutdown() called */
444 DBGC ( image, "NBI %p returned %d from non-returnable image\n",
449 DBGC ( image, "NBI %p returned %d\n", image, rc );
454 /** NBI image type */
455 struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {