iPXE
nbi.c
Go to the documentation of this file.
1#include <string.h>
2#include <errno.h>
3#include <assert.h>
4#include <realmode.h>
5#include <memsizes.h>
6#include <basemem_packet.h>
7#include <ipxe/uaccess.h>
8#include <ipxe/segment.h>
9#include <ipxe/init.h>
10#include <ipxe/netdevice.h>
11#include <ipxe/fakedhcp.h>
12#include <ipxe/image.h>
13#include <ipxe/features.h>
14#include <ipxe/version.h>
15
16/** @file
17 *
18 * NBI image format.
19 *
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.
24 *
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.
28 *
29 */
30
32
33/**
34 * An NBI image header
35 *
36 * Note that the length field uses a peculiar encoding; use the
37 * NBI_LENGTH() macro to decode the actual header length.
38 *
39 */
40struct 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 */
45 };
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 */
51} __attribute__ (( packed ));
52
53/** NBI magic number */
54#define NBI_MAGIC 0x1B031336UL
55
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) )
60
61/* Interpretation of the "flags" fields */
62#define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1U << 8 ) )
63#define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1U << 31 ) )
64
65/** NBI header length */
66#define NBI_HEADER_LENGTH 512
67
68/**
69 * An NBI segment header
70 *
71 * Note that the length field uses a peculiar encoding; use the
72 * NBI_LENGTH() macro to decode the actual header length.
73 *
74 */
75struct 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 */
83};
84
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) & ( 1U << 2 ) )
92
93/* Define a type for passing info to a loaded program */
94struct ebinfo {
95 uint8_t major, minor; /* Version */
96 uint16_t flags; /* Bit flags */
97};
98
99/**
100 * Prepare a segment for an NBI image
101 *
102 * @v image NBI image
103 * @v offset Offset within NBI image
104 * @v filesz Length of initialised-data portion of the segment
105 * @v memsz Total length of the segment
106 * @v src Source for initialised data
107 * @ret rc Return status code
108 */
109static int nbi_prepare_segment ( struct image *image, size_t offset __unused,
110 void *dest, size_t filesz, size_t memsz ) {
111 int rc;
112
113 if ( ( rc = prep_segment ( dest, filesz, memsz ) ) != 0 ) {
114 DBGC ( image, "NBI %s could not prepare segment: %s\n",
115 image->name, strerror ( rc ) );
116 return rc;
117 }
118
119 return 0;
120}
121
122/**
123 * Load a segment for an NBI image
124 *
125 * @v image NBI image
126 * @v offset Offset within NBI image
127 * @v filesz Length of initialised-data portion of the segment
128 * @v memsz Total length of the segment
129 * @v src Source for initialised data
130 * @ret rc Return status code
131 */
132static int nbi_load_segment ( struct image *image, size_t offset,
133 void *dest, size_t filesz,
134 size_t memsz __unused ) {
135 memcpy ( dest, ( image->data + offset ), filesz );
136 return 0;
137}
138
139/**
140 * Process segments of an NBI image
141 *
142 * @v image NBI image
143 * @v imgheader Image header information
144 * @v process Function to call for each segment
145 * @ret rc Return status code
146 */
147static int nbi_process_segments ( struct image *image,
148 const struct imgheader *imgheader,
149 int ( * process ) ( struct image *image,
150 size_t offset,
151 void *dest,
152 size_t filesz,
153 size_t memsz ) ) {
154 const struct segheader *sh;
155 size_t offset = 0;
156 size_t sh_off;
157 void *dest;
158 size_t filesz;
159 size_t memsz;
160 int rc;
161
162 /* Copy image header to target location */
165 filesz = memsz = NBI_HEADER_LENGTH;
166 if ( ( rc = process ( image, offset, dest, filesz, memsz ) ) != 0 )
167 return rc;
168 offset += filesz;
169
170 /* Process segments in turn */
171 sh_off = NBI_LENGTH ( imgheader->length );
172 do {
173 /* Read segment header */
174 if ( ( sh_off + sizeof ( *sh ) ) > image->len ) {
175 DBGC ( image, "NBI %s segheader outside file\n",
176 image->name );
177 return -ENOEXEC;
178 }
179 sh = ( image->data + sh_off );
180 if ( sh->length == 0 ) {
181 /* Avoid infinite loop? */
182 DBGC ( image, "NBI %s invalid segheader length 0\n",
183 image->name );
184 return -ENOEXEC;
185 }
186
187 /* Calculate segment load address */
188 switch ( NBI_LOADADDR_FLAGS ( sh->flags ) ) {
189 case NBI_LOADADDR_ABS:
190 dest = phys_to_virt ( sh->loadaddr );
191 break;
193 dest = ( dest + memsz + sh->loadaddr );
194 break;
196 dest = ( dest - sh->loadaddr );
197 break;
198 case NBI_LOADADDR_END:
199 /* Not correct according to the spec, but
200 * maintains backwards compatibility with
201 * previous versions of Etherboot.
202 */
203 dest = phys_to_virt ( ( extmemsize() + 1024 ) * 1024
204 - sh->loadaddr );
205 break;
206 default:
207 /* Cannot be reached */
208 assert ( 0 );
209 }
210
211 /* Process this segment */
212 filesz = sh->imglength;
213 memsz = sh->memlength;
214 if ( ( offset > image->len ) ||
215 ( filesz > ( image->len - offset ) ) ) {
216 DBGC ( image, "NBI %s segment outside file\n",
217 image->name );
218 return -ENOEXEC;
219 }
220 if ( ( rc = process ( image, offset, dest,
221 filesz, memsz ) ) != 0 ) {
222 return rc;
223 }
224 offset += filesz;
225
226 /* Next segheader */
227 sh_off += NBI_LENGTH ( sh->length );
228 if ( sh_off >= NBI_HEADER_LENGTH ) {
229 DBGC ( image, "NBI %s header overflow\n",
230 image->name );
231 return -ENOEXEC;
232 }
233
234 } while ( ! NBI_LAST_SEGHEADER ( sh->flags ) );
235
236 if ( offset != image->len ) {
237 DBGC ( image, "NBI %s length wrong (file %zd, metadata %zd)\n",
238 image->name, image->len, offset );
239 return -ENOEXEC;
240 }
241
242 return 0;
243}
244
245/**
246 * Boot a 16-bit NBI image
247 *
248 * @v imgheader Image header information
249 * @ret rc Return status code, if image returns
250 */
251static int nbi_boot16 ( struct image *image,
252 const struct imgheader *imgheader ) {
253 int discard_D, discard_S, discard_b;
254 int32_t rc;
255
256 DBGC ( image, "NBI %s executing 16-bit image at %04x:%04x\n",
259
261 REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
262 "pushw %%ds\n\t" /* far pointer to bootp data */
263 "pushw %%bx\n\t"
264 "pushl %%esi\n\t" /* location */
265 "pushw %%cs\n\t" /* lcall execaddr */
266 "call 1f\n\t"
267 "jmp 2f\n\t"
268 "\n1:\n\t"
269 "pushl %%edi\n\t"
270 "lret\n\t"
271 "\n2:\n\t"
272 "addw $8,%%sp\n\t" /* clean up stack */
273 "popl %%ebp\n\t" /* gcc bug */ )
274 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
275 "=b" ( discard_b )
276 : "D" ( imgheader->execaddr.segoff ),
277 "S" ( imgheader->location ),
278 "b" ( __from_data16 ( basemem_packet ) )
279 : "ecx", "edx" );
280
281 return rc;
282}
283
284/**
285 * Boot a 32-bit NBI image
286 *
287 * @v imgheader Image header information
288 * @ret rc Return status code, if image returns
289 */
290static int nbi_boot32 ( struct image *image,
291 const struct imgheader *imgheader ) {
292 struct ebinfo loaderinfo = {
294 0
295 };
296 int discard_D, discard_S, discard_b;
297 int32_t rc;
298
299 DBGC ( image, "NBI %s executing 32-bit image at %lx\n",
301
302 /* Jump to OS with flat physical addressing */
304 PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
305 "pushl %%ebx\n\t" /* bootp data */
306 "pushl %%esi\n\t" /* imgheader */
307 "pushl %%eax\n\t" /* loaderinfo */
308 "call *%%edi\n\t"
309 "addl $12, %%esp\n\t" /* clean up stack */
310 "popl %%ebp\n\t" /* gcc bug */ )
311 : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
312 "=b" ( discard_b )
313 : "D" ( imgheader->execaddr.linear ),
314 "S" ( ( imgheader->location.segment << 4 ) +
316 "b" ( virt_to_phys ( basemem_packet ) ),
317 "a" ( virt_to_phys ( &loaderinfo ) )
318 : "ecx", "edx", "memory" );
319
320 return rc;
321}
322
323/**
324 * Prepare DHCP parameter block for NBI image
325 *
326 * @v image NBI image
327 * @ret rc Return status code
328 */
329static int nbi_prepare_dhcp ( struct image *image ) {
330 struct net_device *boot_netdev;
331 int rc;
332
333 boot_netdev = last_opened_netdev();
334 if ( ! boot_netdev ) {
335 DBGC ( image, "NBI %s could not identify a network device\n",
336 image->name );
337 return -ENODEV;
338 }
339
340 if ( ( rc = create_fakedhcpack ( boot_netdev, basemem_packet,
341 sizeof ( basemem_packet ) ) ) != 0 ) {
342 DBGC ( image, "NBI %s failed to build DHCP packet\n",
343 image->name );
344 return rc;
345 }
346
347 return 0;
348}
349
350/**
351 * Execute a loaded NBI image
352 *
353 * @v image NBI image
354 * @ret rc Return status code
355 */
356static int nbi_exec ( struct image *image ) {
357 const struct imgheader *imgheader;
358 int may_return;
359 int rc;
360
361 /* Retrieve image header */
363
364 DBGC ( image, "NBI %s placing header at %hx:%hx\n", image->name,
366
367 /* NBI files can have overlaps between segments; the bss of
368 * one segment may overlap the initialised data of another. I
369 * assume this is a design flaw, but there are images out
370 * there that we need to work with. We therefore do two
371 * passes: first to initialise the segments, then to copy the
372 * data. This avoids zeroing out already-copied data.
373 */
375 nbi_prepare_segment ) ) != 0 )
376 return rc;
378 nbi_load_segment ) ) != 0 )
379 return rc;
380
381 /* Prepare DHCP option block */
382 if ( ( rc = nbi_prepare_dhcp ( image ) ) != 0 )
383 return rc;
384
385 /* Shut down now if NBI image will not return */
386 may_return = NBI_PROGRAM_RETURNS ( imgheader->flags );
387 if ( ! may_return )
389
390 /* Execute NBI image */
393 } else {
395 }
396
397 if ( ! may_return ) {
398 /* Cannot continue after shutdown() called */
399 DBGC ( image, "NBI %s returned %d from non-returnable image\n",
400 image->name, rc );
401 while ( 1 ) {}
402 }
403
404 DBGC ( image, "NBI %s returned %d\n", image->name, rc );
405
406 return rc;
407}
408
409/**
410 * Probe NBI image
411 *
412 * @v image NBI image
413 * @ret rc Return status code
414 */
415static int nbi_probe ( struct image *image ) {
416 const struct imgheader *imgheader;
417
418 /* If we don't have enough data give up */
419 if ( image->len < NBI_HEADER_LENGTH ) {
420 DBGC ( image, "NBI %s too short for an NBI image\n",
421 image->name );
422 return -ENOEXEC;
423 }
425
426 /* Check image header */
427 if ( imgheader->magic != NBI_MAGIC ) {
428 DBGC ( image, "NBI %s has no NBI signature\n", image->name );
429 return -ENOEXEC;
430 }
431
432 return 0;
433}
434
435/** NBI image type */
436struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {
437 .name = "NBI",
438 .probe = nbi_probe,
439 .exec = nbi_exec,
440};
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
unsigned short uint16_t
Definition stdint.h:11
signed int int32_t
Definition stdint.h:17
unsigned char uint8_t
Definition stdint.h:10
void * discard_D
Definition bigint.h:146
void * discard_S
Definition bigint.h:31
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" retur dest)
Definition string.h:151
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
#define basemem_packet
uint16_t offset
Offset to command line.
Definition bzimage.h:3
Error codes.
int create_fakedhcpack(struct net_device *netdev, void *data, size_t max_len)
Create fake DHCPACK packet.
Definition fakedhcp.c:137
Fake DHCP packets.
static size_t memsz
Definition fdtmem.c:51
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define DBGC(...)
Definition compiler.h:530
#define DHCP_EB_FEATURE_NBI
NBI format.
Definition features.h:49
#define FEATURE_IMAGE
Image formats.
Definition features.h:23
#define ENOEXEC
Exec format error.
Definition errno.h:563
#define ENODEV
No such device.
Definition errno.h:553
Executable images.
#define PROBE_NORMAL
Normal image probe priority.
Definition image.h:163
#define __image_type(probe_order)
An executable image type.
Definition image.h:177
#define __attribute__(x)
Definition compiler.h:10
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void shutdown_boot(void)
Shut down system for OS boot.
Definition init.h:78
unsigned int extmemsize(void)
Get size of extended memory.
Definition int15.c:159
Feature list.
#define FEATURE(category, text, feature_opt, version)
Declare a feature.
Definition features.h:101
Access to external ("user") memory.
Version number.
#define REAL_CODE(asm_code_str)
Definition libkir.h:226
#define __from_data16(pointer)
Definition libkir.h:22
#define PHYS_CODE(asm_code_str)
Definition librm.h:167
#define NBI_LAST_SEGHEADER(flags)
Definition nbi.c:91
static int nbi_boot32(struct image *image, const struct imgheader *imgheader)
Boot a 32-bit NBI image.
Definition nbi.c:290
#define NBI_HEADER_LENGTH
NBI header length.
Definition nbi.c:66
static int nbi_prepare_dhcp(struct image *image)
Prepare DHCP parameter block for NBI image.
Definition nbi.c:329
#define NBI_LOADADDR_BEFORE
Definition nbi.c:90
#define NBI_LOADADDR_ABS
Definition nbi.c:87
static int nbi_exec(struct image *image)
Execute a loaded NBI image.
Definition nbi.c:356
#define NBI_MAGIC
NBI magic number.
Definition nbi.c:54
#define NBI_LOADADDR_FLAGS(flags)
Definition nbi.c:86
#define NBI_LINEAR_EXEC_ADDR(flags)
Definition nbi.c:63
static int nbi_prepare_segment(struct image *image, size_t offset __unused, void *dest, size_t filesz, size_t memsz)
Prepare a segment for an NBI image.
Definition nbi.c:109
#define NBI_LOADADDR_AFTER
Definition nbi.c:88
static int nbi_boot16(struct image *image, const struct imgheader *imgheader)
Boot a 16-bit NBI image.
Definition nbi.c:251
static int nbi_process_segments(struct image *image, const struct imgheader *imgheader, int(*process)(struct image *image, size_t offset, void *dest, size_t filesz, size_t memsz))
Process segments of an NBI image.
Definition nbi.c:147
#define NBI_LENGTH(len)
Definition nbi.c:59
#define NBI_PROGRAM_RETURNS(flags)
Definition nbi.c:62
static int nbi_probe(struct image *image)
Probe NBI image.
Definition nbi.c:415
static int nbi_load_segment(struct image *image, size_t offset, void *dest, size_t filesz, size_t memsz __unused)
Load a segment for an NBI image.
Definition nbi.c:132
#define NBI_LOADADDR_END
Definition nbi.c:89
struct net_device * last_opened_netdev(void)
Get most recently opened network device.
Definition netdevice.c:1052
Network device management.
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
static __always_inline void * real_to_virt(unsigned int segment, unsigned int offset)
Convert segment:offset address to virtual address.
Definition realmode.h:77
struct segoff segoff_t
Definition registers.h:196
int prep_segment(void *segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition segment.c:61
Executable image segments.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
Definition nbi.c:94
uint16_t flags
Definition nbi.c:96
uint8_t major
Definition nbi.c:95
uint8_t minor
Definition nbi.c:95
An executable image type.
Definition image.h:102
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:63
An NBI image header.
Definition nbi.c:40
unsigned char length
Nibble-coded header length.
Definition nbi.c:43
unsigned long linear
32-bit entry point
Definition nbi.c:49
unsigned long flags
Image flags.
Definition nbi.c:44
unsigned long magic
Magic number (NBI_MAGIC).
Definition nbi.c:41
union imgheader::@012245212055350361152346201066323020337132126275 execaddr
segoff_t location
16-bit seg:off header location
Definition nbi.c:46
segoff_t segoff
16-bit seg:off entry point
Definition nbi.c:48
A network device.
Definition netdevice.h:353
A process.
Definition process.h:18
An NBI segment header.
Definition nbi.c:75
unsigned char flags
Segment flags.
Definition nbi.c:79
unsigned char vendortag
Vendor-defined private tag.
Definition nbi.c:77
unsigned char reserved
Definition nbi.c:78
unsigned long loadaddr
Load address.
Definition nbi.c:80
unsigned long memlength
Segment length in memory.
Definition nbi.c:82
unsigned long imglength
Segment length in NBI file.
Definition nbi.c:81
unsigned char length
Nibble-coded header length.
Definition nbi.c:76
uint16_t segment
Definition registers.h:193
uint16_t offset
Definition registers.h:192
const int product_major_version
Product major version.
Definition version.c:66
const int product_minor_version
Product minor version.
Definition version.c:69