5 #include <basemem_packet.h>
6 #include <ipxe/uaccess.h>
7 #include <ipxe/segment.h>
9 #include <ipxe/netdevice.h>
10 #include <ipxe/fakedhcp.h>
11 #include <ipxe/image.h>
12 #include <ipxe/features.h>
13 #include <ipxe/version.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 );
35 * Note that the length field uses a peculiar encoding; use the
36 * NBI_LENGTH() macro to decode the actual header length.
40 unsigned long magic; /**< Magic number (NBI_MAGIC) */
42 unsigned char length; /**< Nibble-coded header length */
43 unsigned long flags; /**< Image flags */
45 segoff_t location; /**< 16-bit seg:off header location */
47 segoff_t segoff; /**< 16-bit seg:off entry point */
48 unsigned long linear; /**< 32-bit entry point */
50 } __attribute__ (( packed ));
52 /** NBI magic number */
53 #define NBI_MAGIC 0x1B031336UL
55 /* Interpretation of the "length" fields */
56 #define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
57 #define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
58 #define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
60 /* Interpretation of the "flags" fields */
61 #define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
62 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
64 /** NBI header length */
65 #define NBI_HEADER_LENGTH 512
68 * An NBI segment header
70 * Note that the length field uses a peculiar encoding; use the
71 * NBI_LENGTH() macro to decode the actual header length.
75 unsigned char length; /**< Nibble-coded header length */
76 unsigned char vendortag; /**< Vendor-defined private tag */
77 unsigned char reserved;
78 unsigned char flags; /**< Segment flags */
79 unsigned long loadaddr; /**< Load address */
80 unsigned long imglength; /**< Segment length in NBI file */
81 unsigned long memlength; /**< Segment length in memory */
84 /* Interpretation of the "flags" fields */
85 #define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
86 #define NBI_LOADADDR_ABS 0x00
87 #define NBI_LOADADDR_AFTER 0x01
88 #define NBI_LOADADDR_END 0x02
89 #define NBI_LOADADDR_BEFORE 0x03
90 #define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
92 /* Define a type for passing info to a loaded program */
94 uint8_t major, minor; /* Version */
95 uint16_t flags; /* Bit flags */
99 * Prepare a segment for an NBI image
102 * @v offset Offset within NBI image
103 * @v filesz Length of initialised-data portion of the segment
104 * @v memsz Total length of the segment
105 * @v src Source for initialised data
106 * @ret rc Return status code
108 static int nbi_prepare_segment ( struct image *image, size_t offset __unused,
109 userptr_t dest, size_t filesz, size_t memsz ){
112 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
113 DBGC ( image, "NBI %p could not prepare segment: %s\n",
114 image, strerror ( rc ) );
122 * Load a segment for an NBI image
125 * @v offset Offset within NBI image
126 * @v filesz Length of initialised-data portion of the segment
127 * @v memsz Total length of the segment
128 * @v src Source for initialised data
129 * @ret rc Return status code
131 static int nbi_load_segment ( struct image *image, size_t offset,
132 userptr_t dest, size_t filesz,
133 size_t memsz __unused ) {
134 memcpy_user ( dest, 0, image->data, offset, filesz );
139 * Process segments of an NBI image
142 * @v imgheader Image header information
143 * @v process Function to call for each segment
144 * @ret rc Return status code
146 static int nbi_process_segments ( struct image *image,
147 struct imgheader *imgheader,
148 int ( * process ) ( struct image *image,
161 /* Copy image header to target location */
162 dest = real_to_user ( imgheader->location.segment,
163 imgheader->location.offset );
164 filesz = memsz = NBI_HEADER_LENGTH;
165 if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 )
169 /* Process segments in turn */
170 sh_off = NBI_LENGTH ( imgheader->length );
172 /* Read segment header */
173 copy_from_user ( &sh, image->data, sh_off, sizeof ( sh ) );
174 if ( sh.length == 0 ) {
175 /* Avoid infinite loop? */
176 DBGC ( image, "NBI %p invalid segheader length 0\n",
181 /* Calculate segment load address */
182 switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
183 case NBI_LOADADDR_ABS:
184 dest = phys_to_user ( sh.loadaddr );
186 case NBI_LOADADDR_AFTER:
187 dest = userptr_add ( dest, memsz + sh.loadaddr );
189 case NBI_LOADADDR_BEFORE:
190 dest = userptr_add ( dest, -sh.loadaddr );
192 case NBI_LOADADDR_END:
193 /* Not correct according to the spec, but
194 * maintains backwards compatibility with
195 * previous versions of Etherboot.
197 dest = phys_to_user ( ( extmemsize() + 1024 ) * 1024
201 /* Cannot be reached */
205 /* Process this segment */
206 filesz = sh.imglength;
207 memsz = sh.memlength;
208 if ( ( offset + filesz ) > image->len ) {
209 DBGC ( image, "NBI %p segment outside file\n", image );
212 if ( ( rc = process ( image, offset, dest,
213 filesz, memsz ) ) != 0 ) {
219 sh_off += NBI_LENGTH ( sh.length );
220 if ( sh_off >= NBI_HEADER_LENGTH ) {
221 DBGC ( image, "NBI %p header overflow\n", image );
225 } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
227 if ( offset != image->len ) {
228 DBGC ( image, "NBI %p length wrong (file %zd, metadata %zd)\n",
229 image, image->len, offset );
237 * Boot a 16-bit NBI image
239 * @v imgheader Image header information
240 * @ret rc Return status code, if image returns
242 static int nbi_boot16 ( struct image *image, struct imgheader *imgheader ) {
243 int discard_D, discard_S, discard_b;
246 DBGC ( image, "NBI %p executing 16-bit image at %04x:%04x\n", image,
247 imgheader->execaddr.segoff.segment,
248 imgheader->execaddr.segoff.offset );
250 __asm__ __volatile__ (
251 REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
252 "pushw %%ds\n\t" /* far pointer to bootp data */
254 "pushl %%esi\n\t" /* location */
255 "pushw %%cs\n\t" /* lcall execaddr */
262 "addw $8,%%sp\n\t" /* clean up stack */
263 "popl %%ebp\n\t" /* gcc bug */ )
264 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
266 : "D" ( imgheader->execaddr.segoff ),
267 "S" ( imgheader->location ),
268 "b" ( __from_data16 ( basemem_packet ) )
275 * Boot a 32-bit NBI image
277 * @v imgheader Image header information
278 * @ret rc Return status code, if image returns
280 static int nbi_boot32 ( struct image *image, struct imgheader *imgheader ) {
281 struct ebinfo loaderinfo = {
282 product_major_version, product_minor_version,
285 int discard_D, discard_S, discard_b;
288 DBGC ( image, "NBI %p executing 32-bit image at %lx\n",
289 image, imgheader->execaddr.linear );
291 /* Jump to OS with flat physical addressing */
292 __asm__ __volatile__ (
293 PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
294 "pushl %%ebx\n\t" /* bootp data */
295 "pushl %%esi\n\t" /* imgheader */
296 "pushl %%eax\n\t" /* loaderinfo */
298 "addl $12, %%esp\n\t" /* clean up stack */
299 "popl %%ebp\n\t" /* gcc bug */ )
300 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
302 : "D" ( imgheader->execaddr.linear ),
303 "S" ( ( imgheader->location.segment << 4 ) +
304 imgheader->location.offset ),
305 "b" ( virt_to_phys ( basemem_packet ) ),
306 "a" ( virt_to_phys ( &loaderinfo ) )
307 : "ecx", "edx", "memory" );
313 * Prepare DHCP parameter block for NBI image
316 * @ret rc Return status code
318 static int nbi_prepare_dhcp ( struct image *image ) {
319 struct net_device *boot_netdev;
322 boot_netdev = last_opened_netdev();
323 if ( ! boot_netdev ) {
324 DBGC ( image, "NBI %p could not identify a network device\n",
329 if ( ( rc = create_fakedhcpack ( boot_netdev, basemem_packet,
330 sizeof ( basemem_packet ) ) ) != 0 ) {
331 DBGC ( image, "NBI %p failed to build DHCP packet\n", image );
339 * Execute a loaded NBI image
342 * @ret rc Return status code
344 static int nbi_exec ( struct image *image ) {
345 struct imgheader imgheader;
349 /* Retrieve image header */
350 copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) );
352 DBGC ( image, "NBI %p placing header at %hx:%hx\n", image,
353 imgheader.location.segment, imgheader.location.offset );
355 /* NBI files can have overlaps between segments; the bss of
356 * one segment may overlap the initialised data of another. I
357 * assume this is a design flaw, but there are images out
358 * there that we need to work with. We therefore do two
359 * passes: first to initialise the segments, then to copy the
360 * data. This avoids zeroing out already-copied data.
362 if ( ( rc = nbi_process_segments ( image, &imgheader,
363 nbi_prepare_segment ) ) != 0 )
365 if ( ( rc = nbi_process_segments ( image, &imgheader,
366 nbi_load_segment ) ) != 0 )
369 /* Prepare DHCP option block */
370 if ( ( rc = nbi_prepare_dhcp ( image ) ) != 0 )
373 /* Shut down now if NBI image will not return */
374 may_return = NBI_PROGRAM_RETURNS ( imgheader.flags );
378 /* Execute NBI image */
379 if ( NBI_LINEAR_EXEC_ADDR ( imgheader.flags ) ) {
380 rc = nbi_boot32 ( image, &imgheader );
382 rc = nbi_boot16 ( image, &imgheader );
385 if ( ! may_return ) {
386 /* Cannot continue after shutdown() called */
387 DBGC ( image, "NBI %p returned %d from non-returnable image\n",
392 DBGC ( image, "NBI %p returned %d\n", image, rc );
401 * @ret rc Return status code
403 static int nbi_probe ( struct image *image ) {
404 struct imgheader imgheader;
406 /* If we don't have enough data give up */
407 if ( image->len < NBI_HEADER_LENGTH ) {
408 DBGC ( image, "NBI %p too short for an NBI image\n", image );
412 /* Check image header */
413 copy_from_user ( &imgheader, image->data, 0, sizeof ( imgheader ) );
414 if ( imgheader.magic != NBI_MAGIC ) {
415 DBGC ( image, "NBI %p has no NBI signature\n", image );
422 /** NBI image type */
423 struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {