[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / arch / i386 / image / nbi.c
blob7244e22a42cb6c062b3475b4fcf886dfce981d7b
1 #include <errno.h>
2 #include <assert.h>
3 #include <realmode.h>
4 #include <memsizes.h>
5 #include <basemem_packet.h>
6 #include <gpxe/uaccess.h>
7 #include <gpxe/segment.h>
8 #include <gpxe/init.h>
9 #include <gpxe/netdevice.h>
10 #include <gpxe/fakedhcp.h>
11 #include <gpxe/image.h>
12 #include <gpxe/features.h>
14 /** @file
16 * NBI image format.
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 );
33 /**
34 * An NBI image header
36 * Note that the length field uses a peculiar encoding; use the
37 * NBI_LENGTH() macro to decode the actual header length.
40 struct imgheader {
41 unsigned long magic; /**< Magic number (NBI_MAGIC) */
42 union {
43 unsigned char length; /**< Nibble-coded header length */
44 unsigned long flags; /**< Image flags */
46 segoff_t location; /**< 16-bit seg:off header location */
47 union {
48 segoff_t segoff; /**< 16-bit seg:off entry point */
49 unsigned long linear; /**< 32-bit entry point */
50 } execaddr;
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
68 /**
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.
75 struct segheader {
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 */
94 struct ebinfo {
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
108 * @v image 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 ){
117 int rc;
119 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
120 DBGC ( image, "NBI %p could not prepare segment: %s\n",
121 image, strerror ( rc ) );
122 return rc;
125 return 0;
129 * Load a segment for an NBI image
131 * @v image 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 );
142 return 0;
146 * Process segments of an NBI image
148 * @v image 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,
156 size_t offset,
157 userptr_t dest,
158 size_t filesz,
159 size_t memsz ) ) {
160 struct segheader sh;
161 size_t offset = 0;
162 size_t sh_off;
163 userptr_t dest;
164 size_t filesz;
165 size_t memsz;
166 int rc;
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 )
173 return rc;
174 offset += filesz;
176 /* Process segments in turn */
177 sh_off = NBI_LENGTH ( imgheader->length );
178 do {
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",
184 image );
185 return -ENOEXEC;
188 /* Calculate segment load address */
189 switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
190 case NBI_LOADADDR_ABS:
191 dest = phys_to_user ( sh.loadaddr );
192 break;
193 case NBI_LOADADDR_AFTER:
194 dest = userptr_add ( dest, memsz + sh.loadaddr );
195 break;
196 case NBI_LOADADDR_BEFORE:
197 dest = userptr_add ( dest, -sh.loadaddr );
198 break;
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
205 - sh.loadaddr );
206 break;
207 default:
208 /* Cannot be reached */
209 assert ( 0 );
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 );
217 return -ENOEXEC;
219 if ( ( rc = process ( image, offset, dest,
220 filesz, memsz ) ) != 0 ) {
221 return rc;
223 offset += filesz;
225 /* Next segheader */
226 sh_off += NBI_LENGTH ( sh.length );
227 if ( sh_off >= NBI_HEADER_LENGTH ) {
228 DBGC ( image, "NBI %p header overflow\n", image );
229 return -ENOEXEC;
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 );
237 return -ENOEXEC;
240 return 0;
244 * Load an NBI image into memory
246 * @v image NBI image
247 * @ret rc Return status code
249 static int nbi_load ( struct image *image ) {
250 struct imgheader imgheader;
251 int rc;
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 );
256 return -ENOEXEC;
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 );
263 return -ENOEXEC;
266 /* This is an NBI image, valid or otherwise */
267 if ( ! image->type )
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 )
282 return rc;
283 if ( ( rc = nbi_process_segments ( image, &imgheader,
284 nbi_load_segment ) ) != 0 )
285 return rc;
287 /* Record header address in image private data field */
288 image->priv.user = real_to_user ( imgheader.location.segment,
289 imgheader.location.offset );
291 return 0;
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;
302 int rc;
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 */
310 "pushw %%bx\n\t"
311 "pushl %%esi\n\t" /* location */
312 "pushw %%cs\n\t" /* lcall execaddr */
313 "call 1f\n\t"
314 "jmp 2f\n\t"
315 "\n1:\n\t"
316 "pushl %%edi\n\t"
317 "lret\n\t"
318 "\n2:\n\t"
319 "addw $8,%%sp\n\t" /* clean up stack */ )
320 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
321 "=b" ( discard_b )
322 : "D" ( imgheader->execaddr.segoff ),
323 "S" ( imgheader->location ),
324 "b" ( __from_data16 ( basemem_packet ) )
325 : "ecx", "edx", "ebp" );
327 return rc;
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;
338 int rc;
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 */
348 "call *%%edi\n\t"
349 "addl $12, %%esp\n\t" /* clean up stack */ )
350 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
351 "=b" ( discard_b )
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" );
359 return rc;
363 * Prepare DHCP parameter block for NBI image
365 * @v image NBI image
366 * @ret rc Return status code
368 static int nbi_prepare_dhcp ( struct image *image ) {
369 struct net_device *boot_netdev;
370 int rc;
372 boot_netdev = last_opened_netdev();
373 if ( ! boot_netdev ) {
374 DBGC ( image, "NBI %p could not identify a network device\n",
375 image );
376 return -ENODEV;
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 );
382 return rc;
385 return 0;
389 * Execute a loaded NBI image
391 * @v image NBI image
392 * @ret rc Return status code
394 static int nbi_exec ( struct image *image ) {
395 struct imgheader imgheader;
396 int may_return;
397 int rc;
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 )
404 return rc;
406 /* Shut down now if NBI image will not return */
407 may_return = NBI_PROGRAM_RETURNS ( imgheader.flags );
408 if ( ! may_return )
409 shutdown ( SHUTDOWN_BOOT );
411 /* Execute NBI image */
412 if ( NBI_LINEAR_EXEC_ADDR ( imgheader.flags ) ) {
413 rc = nbi_boot32 ( image, &imgheader );
414 } else {
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",
421 image, rc );
422 while ( 1 ) {}
425 DBGC ( image, "NBI %p returned %d\n", image, rc );
427 return rc;
430 /** NBI image type */
431 struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {
432 .name = "NBI",
433 .load = nbi_load,
434 .exec = nbi_exec,