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  */
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 */
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 */
50  } execaddr;
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) & ( 1 << 8 ) )
63 #define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 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  */
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 */
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) & ( 1 << 2 ) )
92 
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 */
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  */
109 static 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  */
132 static 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  */
147 static 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  sh = ( image->data + sh_off );
175  if ( sh->length == 0 ) {
176  /* Avoid infinite loop? */
177  DBGC ( image, "NBI %s invalid segheader length 0\n",
178  image->name );
179  return -ENOEXEC;
180  }
181 
182  /* Calculate segment load address */
183  switch ( NBI_LOADADDR_FLAGS ( sh->flags ) ) {
184  case NBI_LOADADDR_ABS:
185  dest = phys_to_virt ( sh->loadaddr );
186  break;
187  case NBI_LOADADDR_AFTER:
188  dest = ( dest + memsz + sh->loadaddr );
189  break;
190  case NBI_LOADADDR_BEFORE:
191  dest = ( dest - sh->loadaddr );
192  break;
193  case NBI_LOADADDR_END:
194  /* Not correct according to the spec, but
195  * maintains backwards compatibility with
196  * previous versions of Etherboot.
197  */
198  dest = phys_to_virt ( ( extmemsize() + 1024 ) * 1024
199  - sh->loadaddr );
200  break;
201  default:
202  /* Cannot be reached */
203  assert ( 0 );
204  }
205 
206  /* Process this segment */
207  filesz = sh->imglength;
208  memsz = sh->memlength;
209  if ( ( offset + filesz ) > image->len ) {
210  DBGC ( image, "NBI %s segment outside file\n",
211  image->name );
212  return -ENOEXEC;
213  }
214  if ( ( rc = process ( image, offset, dest,
215  filesz, memsz ) ) != 0 ) {
216  return rc;
217  }
218  offset += filesz;
219 
220  /* Next segheader */
221  sh_off += NBI_LENGTH ( sh->length );
222  if ( sh_off >= NBI_HEADER_LENGTH ) {
223  DBGC ( image, "NBI %s header overflow\n",
224  image->name );
225  return -ENOEXEC;
226  }
227 
228  } while ( ! NBI_LAST_SEGHEADER ( sh->flags ) );
229 
230  if ( offset != image->len ) {
231  DBGC ( image, "NBI %s length wrong (file %zd, metadata %zd)\n",
232  image->name, image->len, offset );
233  return -ENOEXEC;
234  }
235 
236  return 0;
237 }
238 
239 /**
240  * Boot a 16-bit NBI image
241  *
242  * @v imgheader Image header information
243  * @ret rc Return status code, if image returns
244  */
245 static int nbi_boot16 ( struct image *image,
246  const struct imgheader *imgheader ) {
247  int discard_D, discard_S, discard_b;
248  int32_t rc;
249 
250  DBGC ( image, "NBI %s executing 16-bit image at %04x:%04x\n",
253 
255  REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
256  "pushw %%ds\n\t" /* far pointer to bootp data */
257  "pushw %%bx\n\t"
258  "pushl %%esi\n\t" /* location */
259  "pushw %%cs\n\t" /* lcall execaddr */
260  "call 1f\n\t"
261  "jmp 2f\n\t"
262  "\n1:\n\t"
263  "pushl %%edi\n\t"
264  "lret\n\t"
265  "\n2:\n\t"
266  "addw $8,%%sp\n\t" /* clean up stack */
267  "popl %%ebp\n\t" /* gcc bug */ )
268  : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
269  "=b" ( discard_b )
270  : "D" ( imgheader->execaddr.segoff ),
271  "S" ( imgheader->location ),
272  "b" ( __from_data16 ( basemem_packet ) )
273  : "ecx", "edx" );
274 
275  return rc;
276 }
277 
278 /**
279  * Boot a 32-bit NBI image
280  *
281  * @v imgheader Image header information
282  * @ret rc Return status code, if image returns
283  */
284 static int nbi_boot32 ( struct image *image,
285  const struct imgheader *imgheader ) {
286  struct ebinfo loaderinfo = {
288  0
289  };
290  int discard_D, discard_S, discard_b;
291  int32_t rc;
292 
293  DBGC ( image, "NBI %s executing 32-bit image at %lx\n",
295 
296  /* Jump to OS with flat physical addressing */
298  PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
299  "pushl %%ebx\n\t" /* bootp data */
300  "pushl %%esi\n\t" /* imgheader */
301  "pushl %%eax\n\t" /* loaderinfo */
302  "call *%%edi\n\t"
303  "addl $12, %%esp\n\t" /* clean up stack */
304  "popl %%ebp\n\t" /* gcc bug */ )
305  : "=a" ( rc ), "=D" ( discard_D ), "=S" ( discard_S ),
306  "=b" ( discard_b )
307  : "D" ( imgheader->execaddr.linear ),
308  "S" ( ( imgheader->location.segment << 4 ) +
310  "b" ( virt_to_phys ( basemem_packet ) ),
311  "a" ( virt_to_phys ( &loaderinfo ) )
312  : "ecx", "edx", "memory" );
313 
314  return rc;
315 }
316 
317 /**
318  * Prepare DHCP parameter block for NBI image
319  *
320  * @v image NBI image
321  * @ret rc Return status code
322  */
323 static int nbi_prepare_dhcp ( struct image *image ) {
324  struct net_device *boot_netdev;
325  int rc;
326 
327  boot_netdev = last_opened_netdev();
328  if ( ! boot_netdev ) {
329  DBGC ( image, "NBI %s could not identify a network device\n",
330  image->name );
331  return -ENODEV;
332  }
333 
334  if ( ( rc = create_fakedhcpack ( boot_netdev, basemem_packet,
335  sizeof ( basemem_packet ) ) ) != 0 ) {
336  DBGC ( image, "NBI %s failed to build DHCP packet\n",
337  image->name );
338  return rc;
339  }
340 
341  return 0;
342 }
343 
344 /**
345  * Execute a loaded NBI image
346  *
347  * @v image NBI image
348  * @ret rc Return status code
349  */
350 static int nbi_exec ( struct image *image ) {
351  const struct imgheader *imgheader;
352  int may_return;
353  int rc;
354 
355  /* Retrieve image header */
356  imgheader = image->data;
357 
358  DBGC ( image, "NBI %s placing header at %hx:%hx\n", image->name,
360 
361  /* NBI files can have overlaps between segments; the bss of
362  * one segment may overlap the initialised data of another. I
363  * assume this is a design flaw, but there are images out
364  * there that we need to work with. We therefore do two
365  * passes: first to initialise the segments, then to copy the
366  * data. This avoids zeroing out already-copied data.
367  */
369  nbi_prepare_segment ) ) != 0 )
370  return rc;
372  nbi_load_segment ) ) != 0 )
373  return rc;
374 
375  /* Prepare DHCP option block */
376  if ( ( rc = nbi_prepare_dhcp ( image ) ) != 0 )
377  return rc;
378 
379  /* Shut down now if NBI image will not return */
380  may_return = NBI_PROGRAM_RETURNS ( imgheader->flags );
381  if ( ! may_return )
382  shutdown_boot();
383 
384  /* Execute NBI image */
385  if ( NBI_LINEAR_EXEC_ADDR ( imgheader->flags ) ) {
386  rc = nbi_boot32 ( image, imgheader );
387  } else {
388  rc = nbi_boot16 ( image, imgheader );
389  }
390 
391  if ( ! may_return ) {
392  /* Cannot continue after shutdown() called */
393  DBGC ( image, "NBI %s returned %d from non-returnable image\n",
394  image->name, rc );
395  while ( 1 ) {}
396  }
397 
398  DBGC ( image, "NBI %s returned %d\n", image->name, rc );
399 
400  return rc;
401 }
402 
403 /**
404  * Probe NBI image
405  *
406  * @v image NBI image
407  * @ret rc Return status code
408  */
409 static int nbi_probe ( struct image *image ) {
410  const struct imgheader *imgheader;
411 
412  /* If we don't have enough data give up */
413  if ( image->len < NBI_HEADER_LENGTH ) {
414  DBGC ( image, "NBI %s too short for an NBI image\n",
415  image->name );
416  return -ENOEXEC;
417  }
418  imgheader = image->data;
419 
420  /* Check image header */
421  if ( imgheader->magic != NBI_MAGIC ) {
422  DBGC ( image, "NBI %s has no NBI signature\n", image->name );
423  return -ENOEXEC;
424  }
425 
426  return 0;
427 }
428 
429 /** NBI image type */
430 struct image_type nbi_image_type __image_type ( PROBE_NORMAL ) = {
431  .name = "NBI",
432  .probe = nbi_probe,
433  .exec = nbi_exec,
434 };
uint16_t segment
Definition: registers.h:193
A process.
Definition: process.h:17
#define __attribute__(x)
Definition: compiler.h:10
#define PHYS_CODE(asm_code_str)
Definition: librm.h:167
unsigned long magic
Magic number (NBI_MAGIC)
Definition: nbi.c:41
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
static int nbi_boot16(struct image *image, const struct imgheader *imgheader)
Boot a 16-bit NBI image.
Definition: nbi.c:245
#define NBI_LOADADDR_AFTER
Definition: nbi.c:88
An NBI image header.
Definition: nbi.c:40
#define NBI_LOADADDR_ABS
Definition: nbi.c:87
uint8_t major
Definition: nbi.c:95
Error codes.
const void * data
Read-only data.
Definition: image.h:50
void * discard_S
Definition: bigint.h:63
#define ENOEXEC
Exec format error.
Definition: errno.h:519
unsigned long memlength
Segment length in memory.
Definition: nbi.c:82
Definition: nbi.c:94
int create_fakedhcpack(struct net_device *netdev, void *data, size_t max_len)
Create fake DHCPACK packet.
Definition: fakedhcp.c:136
#define DBGC(...)
Definition: compiler.h:505
An executable image type.
Definition: image.h:94
segoff_t segoff
16-bit seg:off entry point
Definition: nbi.c:48
unsigned int extmemsize(void)
Get size of extended memory.
Definition: int15.c:159
#define PROBE_NORMAL
Normal image probe priority.
Definition: image.h:155
#define DHCP_EB_FEATURE_NBI
NBI format.
Definition: features.h:48
uint8_t minor
Definition: nbi.c:95
An executable image.
Definition: image.h:23
#define FEATURE_IMAGE
Image formats.
Definition: features.h:22
unsigned char reserved
Definition: nbi.c:78
#define NBI_LOADADDR_FLAGS(flags)
Definition: nbi.c:86
unsigned long loadaddr
Load address.
Definition: nbi.c:80
#define NBI_LAST_SEGHEADER(flags)
Definition: nbi.c:91
#define NBI_LOADADDR_BEFORE
Definition: nbi.c:90
char * name
Name of this image type.
Definition: image.h:96
static __always_inline void * real_to_virt(unsigned int segment, unsigned int offset)
Convert segment:offset address to virtual address.
Definition: realmode.h:77
unsigned char vendortag
Vendor-defined private tag.
Definition: nbi.c:77
Executable image segments.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define NBI_HEADER_LENGTH
NBI header length.
Definition: nbi.c:66
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
Access to external ("user") memory.
Executable images.
#define NBI_PROGRAM_RETURNS(flags)
Definition: nbi.c:62
static int nbi_prepare_dhcp(struct image *image)
Prepare DHCP parameter block for NBI image.
Definition: nbi.c:323
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define NBI_LINEAR_EXEC_ADDR(flags)
Definition: nbi.c:63
static int nbi_probe(struct image *image)
Probe NBI image.
Definition: nbi.c:409
struct net_device * last_opened_netdev(void)
Get most recently opened network device.
Definition: netdevice.c:1047
Feature list.
uint16_t offset
Definition: registers.h:192
unsigned char flags
Segment flags.
Definition: nbi.c:79
unsigned long imglength
Segment length in NBI file.
Definition: nbi.c:81
An NBI segment header.
Definition: nbi.c:75
#define basemem_packet
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:55
segoff_t location
16-bit seg:off header location
Definition: nbi.c:46
#define NBI_LENGTH(len)
Definition: nbi.c:59
A network device.
Definition: netdevice.h:352
__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))
#define ENODEV
No such device.
Definition: errno.h:509
unsigned char uint8_t
Definition: stdint.h:10
void * discard_D
Definition: bigint.h:31
Version number.
union imgheader::@453 execaddr
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
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_LOADADDR_END
Definition: nbi.c:89
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
Network device management.
const int product_major_version
Product major version.
Definition: version.c:64
static int nbi_boot32(struct image *image, const struct imgheader *imgheader)
Boot a 32-bit NBI image.
Definition: nbi.c:284
__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")
signed int int32_t
Definition: stdint.h:17
unsigned long flags
Image flags.
Definition: nbi.c:44
unsigned long linear
32-bit entry point
Definition: nbi.c:49
unsigned char length
Nibble-coded header length.
Definition: nbi.c:43
#define __from_data16(pointer)
Definition: libkir.h:22
static int nbi_exec(struct image *image)
Execute a loaded NBI image.
Definition: nbi.c:350
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" return dest
Definition: string.h:150
static size_t memsz
Definition: fdtmem.c:51
FEATURE(FEATURE_IMAGE, "NBI", DHCP_EB_FEATURE_NBI, 1)
unsigned char length
Nibble-coded header length.
Definition: nbi.c:76
int prep_segment(void *segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:61
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
uint16_t flags
Definition: nbi.c:96
static void shutdown_boot(void)
Shut down system for OS boot.
Definition: init.h:77
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
char * name
Name.
Definition: image.h:37
String functions.
const int product_minor_version
Product minor version.
Definition: version.c:67
struct image_type nbi_image_type __image_type(PROBE_NORMAL)
NBI image type.
Fake DHCP packets.
#define NBI_MAGIC
NBI magic number.
Definition: nbi.c:54