5 #include <basemem_packet.h>
6 #include <gpxe/uaccess.h>
7 #include <gpxe/segment.h>
9 #include <gpxe/netdevice.h>
10 #include <gpxe/fakedhcp.h>
11 #include <gpxe/image.h>
12 #include <gpxe/features.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 FEATURE ( FEATURE_IMAGE
, "NBI", DHCP_EB_FEATURE_NBI
, 1 );
31 struct image_type nbi_image_type
__image_type ( PROBE_NORMAL
);
36 * Note that the length field uses a peculiar encoding; use the
37 * NBI_LENGTH() macro to decode the actual header length.
41 unsigned long magic
; /**< Magic number (NBI_MAGIC) */
43 unsigned char length
; /**< Nibble-coded header length */
44 unsigned long flags
; /**< Image flags */
46 segoff_t location
; /**< 16-bit seg:off header location */
48 segoff_t segoff
; /**< 16-bit seg:off entry point */
49 unsigned long linear
; /**< 32-bit entry point */
51 } __attribute__ (( packed
));
53 /** NBI magic number */
54 #define NBI_MAGIC 0x1B031336UL
56 /* Interpretation of the "length" fields */
57 #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
58 #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
59 #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
61 /* Interpretation of the "flags" fields */
62 #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
63 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
65 /** NBI header length */
66 #define NBI_HEADER_LENGTH 512
69 * An NBI segment header
71 * Note that the length field uses a peculiar encoding; use the
72 * NBI_LENGTH() macro to decode the actual header length.
76 unsigned char length
; /**< Nibble-coded header length */
77 unsigned char vendortag
; /**< Vendor-defined private tag */
78 unsigned char reserved
;
79 unsigned char flags
; /**< Segment flags */
80 unsigned long loadaddr
; /**< Load address */
81 unsigned long imglength
; /**< Segment length in NBI file */
82 unsigned long memlength
; /**< Segment length in memory */
85 /* Interpretation of the "flags" fields */
86 #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
87 #define NBI_LOADADDR_ABS 0x00
88 #define NBI_LOADADDR_AFTER 0x01
89 #define NBI_LOADADDR_END 0x02
90 #define NBI_LOADADDR_BEFORE 0x03
91 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
93 /* Define a type for passing info to a loaded program */
95 uint8_t major
, minor
; /* Version */
96 uint16_t flags
; /* Bit flags */
99 /** Info passed to NBI image */
100 static struct ebinfo loaderinfo
= {
101 VERSION_MAJOR
, VERSION_MINOR
,
106 * Prepare a segment for an NBI image
109 * @v offset Offset within NBI image
110 * @v filesz Length of initialised-data portion of the segment
111 * @v memsz Total length of the segment
112 * @v src Source for initialised data
113 * @ret rc Return status code
115 static int nbi_prepare_segment ( struct image
*image
, size_t offset __unused
,
116 userptr_t dest
, size_t filesz
, size_t memsz
){
119 if ( ( rc
= prep_segment ( dest
, filesz
, memsz
) ) != 0 ) {
120 DBGC ( image
, "NBI %p could not prepare segment: %s\n",
121 image
, strerror ( rc
) );
129 * Load a segment for an NBI image
132 * @v offset Offset within NBI image
133 * @v filesz Length of initialised-data portion of the segment
134 * @v memsz Total length of the segment
135 * @v src Source for initialised data
136 * @ret rc Return status code
138 static int nbi_load_segment ( struct image
*image
, size_t offset
,
139 userptr_t dest
, size_t filesz
,
140 size_t memsz __unused
) {
141 memcpy_user ( dest
, 0, image
->data
, offset
, filesz
);
146 * Process segments of an NBI image
149 * @v imgheader Image header information
150 * @v process Function to call for each segment
151 * @ret rc Return status code
153 static int nbi_process_segments ( struct image
*image
,
154 struct imgheader
*imgheader
,
155 int ( * process
) ( struct image
*image
,
168 /* Copy image header to target location */
169 dest
= real_to_user ( imgheader
->location
.segment
,
170 imgheader
->location
.offset
);
171 filesz
= memsz
= NBI_HEADER_LENGTH
;
172 if ( ( rc
= process ( image
, offset
, dest
, filesz
, memsz
) ) != 0 )
176 /* Process segments in turn */
177 sh_off
= NBI_LENGTH ( imgheader
->length
);
179 /* Read segment header */
180 copy_from_user ( &sh
, image
->data
, sh_off
, sizeof ( sh
) );
181 if ( sh
.length
== 0 ) {
182 /* Avoid infinite loop? */
183 DBGC ( image
, "NBI %p invalid segheader length 0\n",
188 /* Calculate segment load address */
189 switch ( NBI_LOADADDR_FLAGS ( sh
.flags
) ) {
190 case NBI_LOADADDR_ABS
:
191 dest
= phys_to_user ( sh
.loadaddr
);
193 case NBI_LOADADDR_AFTER
:
194 dest
= userptr_add ( dest
, memsz
+ sh
.loadaddr
);
196 case NBI_LOADADDR_BEFORE
:
197 dest
= userptr_add ( dest
, -sh
.loadaddr
);
199 case NBI_LOADADDR_END
:
200 /* Not correct according to the spec, but
201 * maintains backwards compatibility with
202 * previous versions of Etherboot.
204 dest
= phys_to_user ( ( extmemsize() + 1024 ) * 1024
208 /* Cannot be reached */
212 /* Process this segment */
213 filesz
= sh
.imglength
;
214 memsz
= sh
.memlength
;
215 if ( ( offset
+ filesz
) > image
->len
) {
216 DBGC ( image
, "NBI %p segment outside file\n", image
);
219 if ( ( rc
= process ( image
, offset
, dest
,
220 filesz
, memsz
) ) != 0 ) {
226 sh_off
+= NBI_LENGTH ( sh
.length
);
227 if ( sh_off
>= NBI_HEADER_LENGTH
) {
228 DBGC ( image
, "NBI %p header overflow\n", image
);
232 } while ( ! NBI_LAST_SEGHEADER ( sh
.flags
) );
234 if ( offset
!= image
->len
) {
235 DBGC ( image
, "NBI %p length wrong (file %zd, metadata %zd)\n",
236 image
, image
->len
, offset
);
244 * Load an NBI image into memory
247 * @ret rc Return status code
249 static int nbi_load ( struct image
*image
) {
250 struct imgheader imgheader
;
253 /* If we don't have enough data give up */
254 if ( image
->len
< NBI_HEADER_LENGTH
) {
255 DBGC ( image
, "NBI %p too short for an NBI image\n", image
);
259 /* Check image header */
260 copy_from_user ( &imgheader
, image
->data
, 0, sizeof ( imgheader
) );
261 if ( imgheader
.magic
!= NBI_MAGIC
) {
262 DBGC ( image
, "NBI %p has no NBI signature\n", image
);
266 /* This is an NBI image, valid or otherwise */
268 image
->type
= &nbi_image_type
;
270 DBGC ( image
, "NBI %p placing header at %hx:%hx\n", image
,
271 imgheader
.location
.segment
, imgheader
.location
.offset
);
273 /* NBI files can have overlaps between segments; the bss of
274 * one segment may overlap the initialised data of another. I
275 * assume this is a design flaw, but there are images out
276 * there that we need to work with. We therefore do two
277 * passes: first to initialise the segments, then to copy the
278 * data. This avoids zeroing out already-copied data.
280 if ( ( rc
= nbi_process_segments ( image
, &imgheader
,
281 nbi_prepare_segment
) ) != 0 )
283 if ( ( rc
= nbi_process_segments ( image
, &imgheader
,
284 nbi_load_segment
) ) != 0 )
287 /* Record header address in image private data field */
288 image
->priv
.user
= real_to_user ( imgheader
.location
.segment
,
289 imgheader
.location
.offset
);
295 * Boot a 16-bit NBI image
297 * @v imgheader Image header information
298 * @ret rc Return status code, if image returns
300 static int nbi_boot16 ( struct image
*image
, struct imgheader
*imgheader
) {
301 int discard_D
, discard_S
, discard_b
;
304 DBGC ( image
, "NBI %p executing 16-bit image at %04x:%04x\n", image
,
305 imgheader
->execaddr
.segoff
.segment
,
306 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 ( basemem_packet
) )
325 : "ecx", "edx", "ebp" );
331 * Boot a 32-bit NBI image
333 * @v imgheader Image header information
334 * @ret rc Return status code, if image returns
336 static int nbi_boot32 ( struct image
*image
, struct imgheader
*imgheader
) {
337 int discard_D
, discard_S
, discard_b
;
340 DBGC ( image
, "NBI %p executing 32-bit image at %lx\n",
341 image
, imgheader
->execaddr
.linear
);
343 /* Jump to OS with flat physical addressing */
344 __asm__
__volatile__ (
345 PHYS_CODE ( "pushl %%ebx\n\t" /* bootp data */
346 "pushl %%esi\n\t" /* imgheader */
347 "pushl %%eax\n\t" /* loaderinfo */
349 "addl $12, %%esp\n\t" /* clean up stack */ )
350 : "=a" ( rc
), "=D" ( discard_D
), "=S" ( discard_S
),
352 : "D" ( imgheader
->execaddr
.linear
),
353 "S" ( ( imgheader
->location
.segment
<< 4 ) +
354 imgheader
->location
.offset
),
355 "b" ( virt_to_phys ( basemem_packet
) ),
356 "a" ( virt_to_phys ( &loaderinfo
) )
357 : "ecx", "edx", "ebp", "memory" );
363 * Prepare DHCP parameter block for NBI image
366 * @ret rc Return status code
368 static int nbi_prepare_dhcp ( struct image
*image
) {
369 struct net_device
*boot_netdev
;
372 boot_netdev
= last_opened_netdev();
373 if ( ! boot_netdev
) {
374 DBGC ( image
, "NBI %p could not identify a network device\n",
379 if ( ( rc
= create_fakedhcpack ( boot_netdev
, basemem_packet
,
380 sizeof ( basemem_packet
) ) ) != 0 ) {
381 DBGC ( image
, "NBI %p failed to build DHCP packet\n", image
);
389 * Execute a loaded NBI image
392 * @ret rc Return status code
394 static int nbi_exec ( struct image
*image
) {
395 struct imgheader imgheader
;
399 copy_from_user ( &imgheader
, image
->priv
.user
, 0,
400 sizeof ( imgheader
) );
402 /* Prepare DHCP option block */
403 if ( ( rc
= nbi_prepare_dhcp ( image
) ) != 0 )
406 /* Shut down now if NBI image will not return */
407 may_return
= NBI_PROGRAM_RETURNS ( imgheader
.flags
);
409 shutdown ( SHUTDOWN_BOOT
);
411 /* Execute NBI image */
412 if ( NBI_LINEAR_EXEC_ADDR ( imgheader
.flags
) ) {
413 rc
= nbi_boot32 ( image
, &imgheader
);
415 rc
= nbi_boot16 ( image
, &imgheader
);
418 if ( ! may_return
) {
419 /* Cannot continue after shutdown() called */
420 DBGC ( image
, "NBI %p returned %d from non-returnable image\n",
425 DBGC ( image
, "NBI %p returned %d\n", image
, rc
);
430 /** NBI image type */
431 struct image_type nbi_image_type
__image_type ( PROBE_NORMAL
) = {