6 #include <dhcp_basemem.h>
7 #include <gpxe/uaccess.h>
8 #include <gpxe/segment.h>
9 #include <gpxe/shutdown.h>
10 #include <gpxe/netdevice.h>
11 #include <gpxe/dhcp.h>
12 #include <gpxe/image.h>
18 * The Net Boot Image format is defined by the "Draft Net Boot Image
19 * Proposal 0.3" by Jamie Honan, Gero Kuhlmann and Ken Yap. It is now
20 * considered to be a legacy format, but it still included because a
21 * large amount of software (e.g. nymph, LTSP) makes use of NBI files.
23 * Etherboot does not implement the INT 78 callback interface
24 * described by the NBI specification. For a callback interface on
25 * x86 architecture, use PXE.
29 struct image_type nbi_image_type __image_type ( PROBE_NORMAL );
34 * Note that the length field uses a peculiar encoding; use the
35 * NBI_LENGTH() macro to decode the actual header length.
39 unsigned long magic; /**< Magic number (NBI_MAGIC) */
41 unsigned char length; /**< Nibble-coded header length */
42 unsigned long flags; /**< Image flags */
44 segoff_t location; /**< 16-bit seg:off header location */
46 segoff_t segoff; /**< 16-bit seg:off entry point */
47 unsigned long linear; /**< 32-bit entry point */
49 } __attribute__ (( packed ));
51 /** NBI magic number */
52 #define NBI_MAGIC 0x1B031336UL
54 /* Interpretation of the "length" fields */
55 #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
56 #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
57 #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
59 /* Interpretation of the "flags" fields */
60 #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
61 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
63 /** NBI header length */
64 #define NBI_HEADER_LENGTH 512
67 * An NBI segment header
69 * Note that the length field uses a peculiar encoding; use the
70 * NBI_LENGTH() macro to decode the actual header length.
74 unsigned char length; /**< Nibble-coded header length */
75 unsigned char vendortag; /**< Vendor-defined private tag */
76 unsigned char reserved;
77 unsigned char flags; /**< Segment flags */
78 unsigned long loadaddr; /**< Load address */
79 unsigned long imglength; /**< Segment length in NBI file */
80 unsigned long memlength; /**< Segment length in memory */
83 /* Interpretation of the "flags" fields */
84 #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
85 #define NBI_LOADADDR_ABS 0x00
86 #define NBI_LOADADDR_AFTER 0x01
87 #define NBI_LOADADDR_END 0x02
88 #define NBI_LOADADDR_BEFORE 0x03
89 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
91 /* Define a type for passing info to a loaded program */
93 uint8_t major, minor; /* Version */
94 uint16_t flags; /* Bit flags */
97 /** Info passed to NBI image */
98 static struct ebinfo loaderinfo = {
99 VERSION_MAJOR, VERSION_MINOR,
104 * Prepare a segment for an NBI image
107 * @v offset Offset within NBI image
108 * @v filesz Length of initialised-data portion of the segment
109 * @v memsz Total length of the segment
110 * @v src Source for initialised data
111 * @ret rc Return status code
113 static int nbi_prepare_segment ( struct image *image, size_t offset __unused,
114 userptr_t dest, size_t filesz, size_t memsz ){
117 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
118 DBGC ( image, "NBI %p could not prepare segment: %s\n",
119 image, strerror ( rc ) );
127 * Load a segment for an NBI image
130 * @v offset Offset within NBI image
131 * @v filesz Length of initialised-data portion of the segment
132 * @v memsz Total length of the segment
133 * @v src Source for initialised data
134 * @ret rc Return status code
136 static int nbi_load_segment ( struct image *image, size_t offset,
137 userptr_t dest, size_t filesz,
138 size_t memsz __unused ) {
139 memcpy_user ( dest, 0, image->data, offset, filesz );
144 * Process segments of an NBI image
147 * @v imgheader Image header information
148 * @v process Function to call for each segment
149 * @ret rc Return status code
151 static int nbi_process_segments ( struct image *image,
152 struct imgheader *imgheader,
153 int ( * process ) ( struct image *image,
166 /* Copy image header to target location */
167 dest = real_to_user ( imgheader->location.segment,
168 imgheader->location.offset );
169 filesz = memsz = NBI_HEADER_LENGTH;
170 if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 )
174 /* Process segments in turn */
175 sh_off = NBI_LENGTH ( imgheader->length );
177 /* Read segment header */
178 copy_from_user ( &sh, image->data, sh_off, sizeof ( sh ) );
179 if ( sh.length == 0 ) {
180 /* Avoid infinite loop? */
181 DBGC ( image, "NBI %p invalid segheader length 0\n",
186 /* Calculate segment load address */
187 switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
188 case NBI_LOADADDR_ABS:
189 dest = phys_to_user ( sh.loadaddr );
191 case NBI_LOADADDR_AFTER:
192 dest = userptr_add ( dest, memsz + sh.loadaddr );
194 case NBI_LOADADDR_BEFORE:
195 dest = userptr_add ( dest, -sh.loadaddr );
197 case NBI_LOADADDR_END:
198 /* Not correct according to the spec, but
199 * maintains backwards compatibility with
200 * previous versions of Etherboot.
202 dest = phys_to_user ( ( extmemsize() + 1024 ) * 1024
206 /* Cannot be reached */
210 /* Process this segment */
211 filesz = sh.imglength;
212 memsz = sh.memlength;
213 if ( ( offset + filesz ) > image->len ) {
214 DBGC ( image, "NBI %p segment outside file\n", image );
217 if ( ( rc = process ( image, offset, dest,
218 filesz, memsz ) ) != 0 ) {
224 sh_off += NBI_LENGTH ( sh.length );
225 if ( sh_off >= NBI_HEADER_LENGTH ) {
226 DBGC ( image, "NBI %p header overflow\n", image );
230 } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
232 if ( offset != image->len ) {
233 DBGC ( image, "NBI %p length wrong (file %d, metadata %d)\n",
234 image, image->len, offset );
242 * Load an NBI image into memory
245 * @ret rc Return status code
247 int nbi_load ( struct image *image ) {
248 struct imgheader imgheader;
251 /* If we don't have enough data give up */
252 if ( image->len < NBI_HEADER_LENGTH ) {
253 DBGC ( image, "NBI %p too short for an NBI image\n", image );
257 /* Check image header */
258 copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) );
259 if ( imgheader.magic != NBI_MAGIC ) {
260 DBGC ( image, "NBI %p has no NBI signature\n", image );
264 /* This is an NBI image, valid or otherwise */
266 image->type = &nbi_image_type;
268 DBGC ( image, "NBI %p placing header at %hx:%hx\n", image,
269 imgheader.location.segment, imgheader.location.offset );
271 /* NBI files can have overlaps between segments; the bss of
272 * one segment may overlap the initialised data of another. I
273 * assume this is a design flaw, but there are images out
274 * there that we need to work with. We therefore do two
275 * passes: first to initialise the segments, then to copy the
276 * data. This avoids zeroing out already-copied data.
278 if ( ( rc = nbi_process_segments ( image, &imgheader,
279 nbi_prepare_segment ) ) != 0 )
281 if ( ( rc = nbi_process_segments ( image, &imgheader,
282 nbi_load_segment ) ) != 0 )
285 /* Record header address in image private data field */
286 image->priv.user = real_to_user ( imgheader.location.segment,
287 imgheader.location.offset );
293 * Boot a 16-bit NBI image
295 * @v imgheader Image header information
296 * @ret rc Return status code, if image returns
298 static int nbi_boot16 ( struct image *image, struct imgheader *imgheader ) {
299 int discard_D, discard_S, discard_b;
302 DBGC ( image, "NBI %p executing 16-bit image at %04x:%04x\n", image,
303 imgheader->execaddr.segoff.segment,
304 imgheader->execaddr.segoff.offset );
308 __asm__ __volatile__ (
309 REAL_CODE ( "pushw %%ds\n\t" /* far pointer to bootp data */
311 "pushl %%esi\n\t" /* location */
312 "pushw %%cs\n\t" /* lcall execaddr */
319 "addw $8,%%sp\n\t" /* clean up stack */ )
320 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
322 : "D" ( imgheader->execaddr.segoff ),
323 "S" ( imgheader->location ),
324 "b" ( __from_data16 ( dhcp_basemem ) )
325 : "ecx", "edx", "ebp" );
333 * Boot a 32-bit NBI image
335 * @v imgheader Image header information
336 * @ret rc Return status code, if image returns
338 static int nbi_boot32 ( struct image *image, struct imgheader *imgheader ) {
339 int discard_D, discard_S, discard_b;
342 DBGC ( image, "NBI %p executing 32-bit image at %lx\n",
343 image, imgheader->execaddr.linear );
345 /* no gateA20_unset for PM call */
347 /* Jump to OS with flat physical addressing */
348 __asm__ __volatile__ (
349 PHYS_CODE ( "pushl %%ebx\n\t" /* bootp data */
350 "pushl %%esi\n\t" /* imgheader */
351 "pushl %%eax\n\t" /* loaderinfo */
353 "addl $12, %%esp\n\t" /* clean up stack */ )
354 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
356 : "D" ( imgheader->execaddr.linear ),
357 "S" ( ( imgheader->location.segment << 4 ) +
358 imgheader->location.offset ),
359 "b" ( virt_to_phys ( dhcp_basemem ) ),
360 "a" ( virt_to_phys ( &loaderinfo ) )
361 : "ecx", "edx", "ebp", "memory" );
367 * Guess boot network device
369 * @ret netdev Boot network device
371 static struct net_device * guess_boot_netdev ( void ) {
372 struct net_device *boot_netdev;
374 /* Just use the first network device */
375 for_each_netdev ( boot_netdev ) {
383 * Prepare DHCP parameter block for NBI image
386 * @ret rc Return status code
388 static int nbi_prepare_dhcp ( struct image *image ) {
389 struct dhcp_packet dhcppkt;
390 struct net_device *boot_netdev;
393 boot_netdev = guess_boot_netdev();
394 if ( ! boot_netdev ) {
395 DBGC ( image, "NBI %p could not identify a network device\n",
400 if ( ( rc = create_dhcp_packet ( boot_netdev, DHCPACK,
401 dhcp_basemem, sizeof ( dhcp_basemem ),
402 &dhcppkt ) ) != 0 ) {
403 DBGC ( image, "NBI %p failed to build DHCP packet\n", image );
406 if ( ( rc = copy_dhcp_packet_options ( &dhcppkt, NULL ) ) != 0 ) {
407 DBGC ( image, "NBI %p failed to copy DHCP options\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 ) = {