iPXE
Data Structures | Functions
bzimage.c File Reference

Linux bzImage image format. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <bzimage.h>
#include <initrd.h>
#include <ipxe/uaccess.h>
#include <ipxe/image.h>
#include <ipxe/segment.h>
#include <ipxe/init.h>
#include <ipxe/cpio.h>
#include <ipxe/features.h>

Go to the source code of this file.

Data Structures

struct  bzimage_context
 bzImage context More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FEATURE (FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1)
 
static int bzimage_parse_header (struct image *image, struct bzimage_context *bzimg, userptr_t src)
 Parse bzImage header. More...
 
static void bzimage_update_header (struct image *image, struct bzimage_context *bzimg, userptr_t dst)
 Update bzImage header in loaded kernel. More...
 
static int bzimage_parse_cmdline (struct image *image, struct bzimage_context *bzimg, const char *cmdline)
 Parse kernel command line for bootloader parameters. More...
 
static void bzimage_set_cmdline (struct image *image, struct bzimage_context *bzimg, const char *cmdline)
 Set command line. More...
 
static void bzimage_parse_cpio_cmdline (struct image *image, struct cpio_header *cpio, const char *cmdline)
 Parse standalone image command line for cpio parameters. More...
 
static size_t bzimage_align (size_t len)
 Align initrd length. More...
 
static size_t bzimage_load_initrd (struct image *image, struct image *initrd, userptr_t address)
 Load initrd. More...
 
static int bzimage_check_initrds (struct image *image, struct bzimage_context *bzimg)
 Check that initrds can be loaded. More...
 
static void bzimage_load_initrds (struct image *image, struct bzimage_context *bzimg)
 Load initrds, if any. More...
 
static int bzimage_exec (struct image *image)
 Execute bzImage image. More...
 
int bzimage_probe (struct image *image)
 Probe bzImage image. More...
 
struct image_type bzimage_image_type __image_type (PROBE_NORMAL)
 Linux bzImage image type. More...
 

Detailed Description

Linux bzImage image format.

Definition in file bzimage.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FEATURE()

FEATURE ( FEATURE_IMAGE  ,
"bzImage"  ,
DHCP_EB_FEATURE_BZIMAGE  ,
 
)

◆ bzimage_parse_header()

static int bzimage_parse_header ( struct image image,
struct bzimage_context bzimg,
userptr_t  src 
)
static

Parse bzImage header.

Parameters
imagebzImage file
bzimgbzImage context
srcbzImage to parse
Return values
rcReturn status code

Definition at line 97 of file bzimage.c.

99  {
100  unsigned int syssize;
101  int is_bzimage;
102 
103  /* Sanity check */
104  if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
105  DBGC ( image, "bzImage %p too short for kernel header\n",
106  image );
107  return -ENOEXEC;
108  }
109 
110  /* Read in header structures */
111  memset ( bzimg, 0, sizeof ( *bzimg ) );
113  sizeof ( bzimg->cmdline_magic ) );
115  sizeof ( bzimg->bzhdr ) );
116 
117  /* Calculate size of real-mode portion */
118  bzimg->rm_filesz = ( ( ( bzimg->bzhdr.setup_sects ?
119  bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9 );
120  if ( bzimg->rm_filesz > image->len ) {
121  DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
122  image, bzimg->rm_filesz );
123  return -ENOEXEC;
124  }
125  bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
126 
127  /* Calculate size of protected-mode portion */
128  bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
129  syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
130 
131  /* Check for signatures and determine version */
132  if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
133  DBGC ( image, "bzImage %p missing 55AA signature\n", image );
134  return -ENOEXEC;
135  }
136  if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
137  /* 2.00+ */
138  bzimg->version = bzimg->bzhdr.version;
139  } else {
140  /* Pre-2.00. Check that the syssize field is correct,
141  * as a guard against accepting arbitrary binary data,
142  * since the 55AA check is pretty lax. Note that the
143  * syssize field is unreliable for protocols between
144  * 2.00 and 2.03 inclusive, so we should not always
145  * check this field.
146  */
147  bzimg->version = 0x0100;
148  if ( bzimg->bzhdr.syssize != syssize ) {
149  DBGC ( image, "bzImage %p bad syssize %x (expected "
150  "%x)\n", image, bzimg->bzhdr.syssize, syssize );
151  return -ENOEXEC;
152  }
153  }
154 
155  /* Determine image type */
156  is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
157  ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
158 
159  /* Calculate load address of real-mode portion */
160  bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
161  bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
162 
163  /* Allow space for the stack and heap */
164  bzimg->rm_memsz += BZI_STACK_SIZE;
165  bzimg->rm_heap = bzimg->rm_memsz;
166 
167  /* Allow space for the command line */
168  bzimg->rm_cmdline = bzimg->rm_memsz;
169  bzimg->rm_memsz += BZI_CMDLINE_SIZE;
170 
171  /* Calculate load address of protected-mode portion */
172  bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
173  : BZI_LOAD_LOW_ADDR );
174 
175  /* Extract video mode */
176  bzimg->vid_mode = bzimg->bzhdr.vid_mode;
177 
178  /* Extract memory limit */
179  bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
181 
182  /* Extract command line size */
183  bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
185 
186  DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
187  "cmdlen %zd\n", image, bzimg->version,
188  user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
189  user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
190  bzimg->cmdline_size );
191 
192  return 0;
193 }
size_t rm_heap
Real-mode heap top (offset from rm_kernel)
Definition: bzimage.c:63
#define BZI_CMDLINE_SIZE
Maximum size of command line.
Definition: bzimage.h:140
#define ENOEXEC
Exec format error.
Definition: errno.h:519
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:65
size_t rm_filesz
Real-mode kernel portion file size.
Definition: bzimage.c:61
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
size_t rm_memsz
Real-mode kernel portion total memory size.
Definition: bzimage.c:69
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
An executable image.
Definition: image.h:24
#define BZI_CMDLINE_OFFSET
Offset of bzImage command-line structure within kernel image.
Definition: bzimage.h:128
uint16_t version
Boot protocol version supported.
Definition: bzimage.h:38
#define BZI_SIGNATURE
bzImage magic signature value
Definition: bzimage.h:84
#define BZI_LOAD_HIGH_ADDR
Load address for high-loaded kernels.
Definition: bzimage.h:99
#define BZI_LOAD_LOW_ADDR
Load address for low-loaded kernels.
Definition: bzimage.h:102
uint32_t initrd_addr_max
Highest legal initrd address.
Definition: bzimage.h:66
userptr_t pm_kernel
Non-real-mode kernel portion load address.
Definition: bzimage.c:71
unsigned int rm_kernel_seg
Real-mode kernel portion load segment address.
Definition: bzimage.c:57
#define BZI_STACK_SIZE
Amount of stack space to provide.
Definition: bzimage.h:137
size_t cmdline_size
Command line maximum length.
Definition: bzimage.c:67
#define BZI_HDR_OFFSET
Offset of bzImage header within kernel image.
Definition: bzimage.h:78
struct bzimage_header bzhdr
bzImage header
Definition: bzimage.c:86
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
#define BZI_BOOT_FLAG
bzImage boot flag value
Definition: bzimage.h:81
uint16_t syssize
DO NOT USE - for bootsect.S use only.
Definition: bzimage.h:22
size_t len
Length of raw file image.
Definition: image.h:43
uint32_t cmdline_size
Maximum size of the kernel command line.
Definition: bzimage.h:74
#define BZI_INITRD_MAX
bzImage maximum initrd address for versions < 2.03
Definition: bzimage.h:117
uint32_t header
Magic signature "HdrS".
Definition: bzimage.h:36
unsigned int vid_mode
Video mode.
Definition: bzimage.c:75
userptr_t rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:59
uint16_t syssize
DO NOT USE - for bootsect.S use only.
Definition: bzimage.h:13
size_t pm_sz
Non-real-mode kernel portion file and memory size.
Definition: bzimage.c:73
#define BZI_ASSUMED_RM_SIZE
Assumed size of real-mode portion (including .bss)
Definition: bzimage.h:134
static __always_inline userptr_t real_to_user(unsigned int segment, unsigned int offset)
Convert segment:offset address to user buffer.
Definition: realmode.h:75
struct bzimage_cmdline cmdline_magic
Command line magic block.
Definition: bzimage.c:84
#define BZI_LOAD_HIGH
bzImage "load high" flag
Definition: bzimage.h:96
unsigned int version
Boot protocol version.
Definition: bzimage.c:55
uint16_t vid_mode
Video mode control.
Definition: bzimage.h:28
uint8_t loadflags
Boot protocol option flags.
Definition: bzimage.h:48
uint8_t setup_sects
The size of the setup in sectors.
Definition: bzimage.h:18
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:77
uint16_t boot_flag
0xAA55 magic number
Definition: bzimage.h:32
void * memset(void *dest, int character, size_t len) __nonnull

References bzimage_header::boot_flag, bzimage_context::bzhdr, BZI_ASSUMED_RM_SIZE, BZI_BOOT_FLAG, BZI_CMDLINE_OFFSET, BZI_CMDLINE_SIZE, BZI_HDR_OFFSET, BZI_INITRD_MAX, BZI_LOAD_HIGH, BZI_LOAD_HIGH_ADDR, BZI_LOAD_LOW_ADDR, BZI_SIGNATURE, BZI_STACK_SIZE, bzimage_context::cmdline_magic, bzimage_context::cmdline_size, bzimage_header::cmdline_size, copy_from_user(), DBGC, ENOEXEC, bzimage_header::header, bzimage_header::initrd_addr_max, image::len, bzimage_header::loadflags, bzimage_context::mem_limit, memset(), phys_to_user(), bzimage_context::pm_kernel, bzimage_context::pm_sz, real_to_user(), bzimage_context::rm_cmdline, bzimage_context::rm_filesz, bzimage_context::rm_heap, bzimage_context::rm_kernel, bzimage_context::rm_kernel_seg, bzimage_context::rm_memsz, bzimage_header::setup_sects, src, syssize, bzimage_header::syssize, user_to_phys(), bzimage_header::version, bzimage_context::version, bzimage_header::vid_mode, and bzimage_context::vid_mode.

Referenced by bzimage_exec(), and bzimage_probe().

◆ bzimage_update_header()

static void bzimage_update_header ( struct image image,
struct bzimage_context bzimg,
userptr_t  dst 
)
static

Update bzImage header in loaded kernel.

Parameters
imagebzImage file
bzimgbzImage context
dstbzImage to update

Definition at line 202 of file bzimage.c.

204  {
205 
206  /* Set loader type */
207  if ( bzimg->version >= 0x0200 )
209 
210  /* Set heap end pointer */
211  if ( bzimg->version >= 0x0201 ) {
212  bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
213  bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
214  }
215 
216  /* Set command line */
217  if ( bzimg->version >= 0x0202 ) {
218  bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
219  bzimg->rm_cmdline );
220  } else {
222  bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
223  if ( bzimg->version >= 0x0200 )
224  bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
225  }
226 
227  /* Set video mode */
228  bzimg->bzhdr.vid_mode = bzimg->vid_mode;
229 
230  /* Set initrd address */
231  if ( bzimg->version >= 0x0200 ) {
232  bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
233  bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
234  }
235 
236  /* Write out header structures */
238  sizeof ( bzimg->cmdline_magic ) );
239  copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
240  sizeof ( bzimg->bzhdr ) );
241 
242  DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
243 }
#define BZI_CMDLINE_MAGIC
bzImage command line present magic marker value
Definition: bzimage.h:131
size_t rm_heap
Real-mode heap top (offset from rm_kernel)
Definition: bzimage.c:63
physaddr_t ramdisk_size
Initrd size.
Definition: bzimage.c:81
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:65
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
size_t rm_memsz
Real-mode kernel portion total memory size.
Definition: bzimage.c:69
An executable image.
Definition: image.h:24
#define BZI_CMDLINE_OFFSET
Offset of bzImage command-line structure within kernel image.
Definition: bzimage.h:128
uint32_t ramdisk_size
initrd size (set by boot loader)
Definition: bzimage.h:56
#define BZI_CAN_USE_HEAP
bzImage "kernel can use heap" flag
Definition: bzimage.h:105
physaddr_t ramdisk_image
Initrd address.
Definition: bzimage.c:79
uint32_t ramdisk_image
initrd load address (set by boot loader)
Definition: bzimage.h:54
#define BZI_LOADER_TYPE_IPXE
bzImage boot loader identifier for iPXE
Definition: bzimage.h:93
#define BZI_HDR_OFFSET
Offset of bzImage header within kernel image.
Definition: bzimage.h:78
struct bzimage_header bzhdr
bzImage header
Definition: bzimage.c:86
uint16_t heap_end_ptr
Free memory after setup end.
Definition: bzimage.h:60
uint8_t type_of_loader
Boot loader identifier.
Definition: bzimage.h:46
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
uint16_t setup_move_size
Move to high memory size (used with hooks)
Definition: bzimage.h:50
uint16_t magic
Magic signature.
Definition: bzimage.h:122
uint32_t cmd_line_ptr
32-bit pointer to the kernel command line
Definition: bzimage.h:64
unsigned int vid_mode
Video mode.
Definition: bzimage.c:75
uint16_t offset
Offset to command line.
Definition: bzimage.h:124
userptr_t rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:59
struct bzimage_cmdline cmdline_magic
Command line magic block.
Definition: bzimage.c:84
unsigned int version
Boot protocol version.
Definition: bzimage.c:55
uint16_t vid_mode
Video mode control.
Definition: bzimage.h:28
uint8_t loadflags
Boot protocol option flags.
Definition: bzimage.h:48

References bzimage_context::bzhdr, BZI_CAN_USE_HEAP, BZI_CMDLINE_MAGIC, BZI_CMDLINE_OFFSET, BZI_HDR_OFFSET, BZI_LOADER_TYPE_IPXE, bzimage_header::cmd_line_ptr, bzimage_context::cmdline_magic, copy_to_user(), DBGC, bzimage_header::heap_end_ptr, bzimage_header::loadflags, bzimage_cmdline::magic, bzimage_cmdline::offset, bzimage_header::ramdisk_image, bzimage_context::ramdisk_image, bzimage_header::ramdisk_size, bzimage_context::ramdisk_size, bzimage_context::rm_cmdline, bzimage_context::rm_heap, bzimage_context::rm_kernel, bzimage_context::rm_memsz, bzimage_header::setup_move_size, bzimage_header::type_of_loader, user_to_phys(), bzimage_context::version, bzimage_header::vid_mode, and bzimage_context::vid_mode.

Referenced by bzimage_exec().

◆ bzimage_parse_cmdline()

static int bzimage_parse_cmdline ( struct image image,
struct bzimage_context bzimg,
const char *  cmdline 
)
static

Parse kernel command line for bootloader parameters.

Parameters
imagebzImage file
bzimgbzImage context
cmdlineKernel command line
Return values
rcReturn status code

Definition at line 253 of file bzimage.c.

255  {
256  char *vga;
257  char *mem;
258 
259  /* Look for "vga=" */
260  if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
261  vga += 4;
262  if ( strcmp ( vga, "normal" ) == 0 ) {
263  bzimg->vid_mode = BZI_VID_MODE_NORMAL;
264  } else if ( strcmp ( vga, "ext" ) == 0 ) {
265  bzimg->vid_mode = BZI_VID_MODE_EXT;
266  } else if ( strcmp ( vga, "ask" ) == 0 ) {
267  bzimg->vid_mode = BZI_VID_MODE_ASK;
268  } else {
269  bzimg->vid_mode = strtoul ( vga, &vga, 0 );
270  if ( *vga && ( *vga != ' ' ) ) {
271  DBGC ( image, "bzImage %p strange \"vga=\""
272  "terminator '%c'\n", image, *vga );
273  }
274  }
275  }
276 
277  /* Look for "mem=" */
278  if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
279  mem += 4;
280  bzimg->mem_limit = strtoul ( mem, &mem, 0 );
281  switch ( *mem ) {
282  case 'G':
283  case 'g':
284  bzimg->mem_limit <<= 10;
285  /* Fall through */
286  case 'M':
287  case 'm':
288  bzimg->mem_limit <<= 10;
289  /* Fall through */
290  case 'K':
291  case 'k':
292  bzimg->mem_limit <<= 10;
293  break;
294  case '\0':
295  case ' ':
296  break;
297  default:
298  DBGC ( image, "bzImage %p strange \"mem=\" "
299  "terminator '%c'\n", image, *mem );
300  break;
301  }
302  bzimg->mem_limit -= 1;
303  }
304 
305  return 0;
306 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define BZI_VID_MODE_ASK
bzImage special video mode "ask"
Definition: bzimage.h:114
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:279
#define BZI_VID_MODE_EXT
bzImage special video mode "ext"
Definition: bzimage.h:111
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
unsigned int vid_mode
Video mode.
Definition: bzimage.c:75
#define BZI_VID_MODE_NORMAL
bzImage special video mode "normal"
Definition: bzimage.h:108
uint32_t cmdline
Definition: multiboot.h:16
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:77
static unsigned int unsigned int mem
Definition: intel.h:271

References BZI_VID_MODE_ASK, BZI_VID_MODE_EXT, BZI_VID_MODE_NORMAL, cmdline, DBGC, mem, bzimage_context::mem_limit, strcmp(), strstr(), strtoul(), and bzimage_context::vid_mode.

Referenced by bzimage_exec().

◆ bzimage_set_cmdline()

static void bzimage_set_cmdline ( struct image image,
struct bzimage_context bzimg,
const char *  cmdline 
)
static

Set command line.

Parameters
imagebzImage image
bzimgbzImage context
cmdlineKernel command line

Definition at line 315 of file bzimage.c.

317  {
318  size_t cmdline_len;
319 
320  /* Copy command line down to real-mode portion */
321  cmdline_len = ( strlen ( cmdline ) + 1 );
322  if ( cmdline_len > bzimg->cmdline_size )
323  cmdline_len = bzimg->cmdline_size;
324  copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
325  cmdline, cmdline_len );
326  DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
327 }
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:65
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
size_t cmdline_size
Command line maximum length.
Definition: bzimage.c:67
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
userptr_t rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:59
uint32_t cmdline
Definition: multiboot.h:16

References cmdline, bzimage_context::cmdline_size, copy_to_user(), DBGC, bzimage_context::rm_cmdline, bzimage_context::rm_kernel, and strlen().

Referenced by bzimage_exec().

◆ bzimage_parse_cpio_cmdline()

static void bzimage_parse_cpio_cmdline ( struct image image,
struct cpio_header cpio,
const char *  cmdline 
)
static

Parse standalone image command line for cpio parameters.

Parameters
imagebzImage file
cpioCPIO header
cmdlineCommand line

Definition at line 336 of file bzimage.c.

338  {
339  char *arg;
340  char *end;
341  unsigned int mode;
342 
343  /* Look for "mode=" */
344  if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
345  arg += 5;
346  mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
347  if ( *end && ( *end != ' ' ) ) {
348  DBGC ( image, "bzImage %p strange \"mode=\""
349  "terminator '%c'\n", image, *end );
350  }
351  cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
352  }
353 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
void cpio_set_field(char *field, unsigned long value)
Set field within a CPIO header.
Definition: cpio.c:42
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:279
char c_mode[8]
File mode and permissions.
Definition: cpio.h:23
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint32_t cmdline
Definition: multiboot.h:16

References cpio_header::c_mode, cmdline, cpio_set_field(), DBGC, end, strstr(), and strtoul().

Referenced by bzimage_load_initrd().

◆ bzimage_align()

static size_t bzimage_align ( size_t  len)
inlinestatic

Align initrd length.

Parameters
lenLength
Return values
lenLength rounded up to INITRD_ALIGN

Definition at line 361 of file bzimage.c.

361  {
362 
363  return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
364 }
#define INITRD_ALIGN
Minimum alignment for initrds.
Definition: initrd.h:19
uint32_t len
Length.
Definition: ena.h:14

References INITRD_ALIGN, and len.

Referenced by bzimage_check_initrds(), and bzimage_load_initrds().

◆ bzimage_load_initrd()

static size_t bzimage_load_initrd ( struct image image,
struct image initrd,
userptr_t  address 
)
static

Load initrd.

Parameters
imagebzImage image
initrdinitrd image
addressAddress at which to load, or UNULL
Return values
lenLength of loaded image, excluding zero-padding

Definition at line 374 of file bzimage.c.

376  {
377  char *filename = initrd->cmdline;
378  char *cmdline;
379  struct cpio_header cpio;
380  size_t offset;
381  size_t name_len;
382  size_t pad_len;
383 
384  /* Do not include kernel image itself as an initrd */
385  if ( initrd == image )
386  return 0;
387 
388  /* Create cpio header for non-prebuilt images */
389  if ( filename && filename[0] ) {
390  cmdline = strchr ( filename, ' ' );
391  name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
392  : strlen ( filename ) ) + 1 /* NUL */ );
393  memset ( &cpio, '0', sizeof ( cpio ) );
394  memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
395  cpio_set_field ( cpio.c_mode, 0100644 );
396  cpio_set_field ( cpio.c_nlink, 1 );
397  cpio_set_field ( cpio.c_filesize, initrd->len );
398  cpio_set_field ( cpio.c_namesize, name_len );
399  if ( cmdline ) {
401  ( cmdline + 1 /* ' ' */ ));
402  }
403  offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
404  } else {
405  offset = 0;
406  name_len = 0;
407  }
408 
409  /* Copy in initrd image body (and cpio header if applicable) */
410  if ( address ) {
411  memmove_user ( address, offset, initrd->data, 0, initrd->len );
412  if ( offset ) {
413  memset_user ( address, 0, 0, offset );
414  copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
415  copy_to_user ( address, sizeof ( cpio ), filename,
416  ( name_len - 1 /* NUL (or space) */ ) );
417  }
418  DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
419  "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
421  user_to_phys ( address, ( offset + initrd->len ) ),
422  ( filename ? " " : "" ), ( filename ? filename : "" ) );
424  user_to_virt ( address, offset ), initrd->len );
425  }
426  offset += initrd->len;
427 
428  /* Zero-pad to next INITRD_ALIGN boundary */
429  pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
430  if ( address )
431  memset_user ( address, offset, 0, pad_len );
432 
433  return offset;
434 }
userptr_t data
Raw file image.
Definition: image.h:41
A CPIO archive header.
Definition: cpio.h:17
__SIZE_TYPE__ size_t
Definition: stdint.h:6
static void bzimage_parse_cpio_cmdline(struct image *image, struct cpio_header *cpio, const char *cmdline)
Parse standalone image command line for cpio parameters.
Definition: bzimage.c:336
uint64_t address
Base address.
Definition: ena.h:24
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define DBGC2_MD5A(...)
Definition: compiler.h:525
char * cmdline
Command line to pass to image.
Definition: image.h:39
#define INITRD_ALIGN
Minimum alignment for initrds.
Definition: initrd.h:19
void cpio_set_field(char *field, unsigned long value)
Set field within a CPIO header.
Definition: cpio.c:42
void memset_user(userptr_t userptr, off_t offset, int c, size_t len)
Fill user buffer with a constant byte.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t len
Length of raw file image.
Definition: image.h:43
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:241
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
#define CPIO_MAGIC
CPIO magic.
Definition: cpio.h:49
void * user_to_virt(userptr_t userptr, off_t offset)
Convert user pointer to virtual address.
void memmove_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers, allowing for overlap.
uint32_t cmdline
Definition: multiboot.h:16
void * memset(void *dest, int character, size_t len) __nonnull

References address, bzimage_parse_cpio_cmdline(), cpio_header::c_filesize, cpio_header::c_magic, cpio_header::c_mode, cpio_header::c_namesize, cpio_header::c_nlink, cmdline, image::cmdline, copy_to_user(), CPIO_MAGIC, cpio_set_field(), image::data, DBGC, DBGC2_MD5A, INITRD_ALIGN, image::len, memcpy(), memmove_user(), memset(), memset_user(), offset, strchr(), strlen(), user_to_phys(), and user_to_virt().

Referenced by bzimage_check_initrds(), and bzimage_load_initrds().

◆ bzimage_check_initrds()

static int bzimage_check_initrds ( struct image image,
struct bzimage_context bzimg 
)
static

Check that initrds can be loaded.

Parameters
imagebzImage image
bzimgbzImage context
Return values
rcReturn status code

Definition at line 443 of file bzimage.c.

444  {
445  struct image *initrd;
447  size_t len = 0;
448  int rc;
449 
450  /* Calculate total loaded length of initrds */
451  for_each_image ( initrd ) {
452 
453  /* Skip kernel */
454  if ( initrd == image )
455  continue;
456 
457  /* Calculate length */
458  len += bzimage_load_initrd ( image, initrd, UNULL );
459  len = bzimage_align ( len );
460 
461  DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
462  image, initrd, user_to_phys ( initrd->data, 0 ),
463  user_to_phys ( initrd->data, initrd->len ),
464  ( initrd->cmdline ? " " : "" ),
465  ( initrd->cmdline ? initrd->cmdline : "" ) );
466  DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
467  user_to_virt ( initrd->data, 0 ), initrd->len );
468  }
469 
470  /* Calculate lowest usable address */
471  bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
472 
473  /* Check that total length fits within space available for
474  * reshuffling. This is a conservative check, since CPIO
475  * headers are not present during reshuffling, but this
476  * doesn't hurt and keeps the code simple.
477  */
478  if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
479  DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
480  image, strerror ( rc ) );
481  return rc;
482  }
483 
484  /* Check that total length fits within kernel's memory limit */
485  if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
486  DBGC ( image, "bzImage %p not enough space for initrds\n",
487  image );
488  return -ENOBUFS;
489  }
490 
491  return 0;
492 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
static size_t bzimage_load_initrd(struct image *image, struct image *initrd, userptr_t address)
Load initrd.
Definition: bzimage.c:374
static userptr_t bottom
Bottom of heap (current lowest allocated block)
An executable image.
Definition: image.h:24
#define DBGC2_MD5A(...)
Definition: compiler.h:525
char * cmdline
Command line to pass to image.
Definition: image.h:39
userptr_t userptr_add(userptr_t userptr, off_t offset)
Add offset to user pointer.
userptr_t pm_kernel
Non-real-mode kernel portion load address.
Definition: bzimage.c:71
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:149
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:43
static size_t bzimage_align(size_t len)
Align initrd length.
Definition: bzimage.c:361
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
uint32_t len
Length.
Definition: ena.h:14
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
int initrd_reshuffle_check(size_t len, userptr_t bottom)
Check that there is enough space to reshuffle initrds.
Definition: initrd.c:266
void * user_to_virt(userptr_t userptr, off_t offset)
Convert user pointer to virtual address.
size_t pm_sz
Non-real-mode kernel portion file and memory size.
Definition: bzimage.c:73
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:77
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References bottom, bzimage_align(), bzimage_load_initrd(), image::cmdline, image::data, DBGC, DBGC2_MD5A, ENOBUFS, for_each_image, initrd_reshuffle_check(), len, image::len, bzimage_context::mem_limit, bzimage_context::pm_kernel, bzimage_context::pm_sz, rc, strerror(), UNULL, user_to_phys(), user_to_virt(), and userptr_add().

Referenced by bzimage_exec().

◆ bzimage_load_initrds()

static void bzimage_load_initrds ( struct image image,
struct bzimage_context bzimg 
)
static

Load initrds, if any.

Parameters
imagebzImage image
bzimgbzImage context

Definition at line 500 of file bzimage.c.

501  {
502  struct image *initrd;
503  struct image *highest = NULL;
504  struct image *other;
505  userptr_t top;
506  userptr_t dest;
507  size_t offset;
508  size_t len;
509 
510  /* Reshuffle initrds into desired order */
511  initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
512 
513  /* Find highest initrd */
514  for_each_image ( initrd ) {
515  if ( ( highest == NULL ) ||
516  ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
517  highest = initrd;
518  }
519  }
520 
521  /* Do nothing if there are no initrds */
522  if ( ! highest )
523  return;
524 
525  /* Find highest usable address */
526  top = userptr_add ( highest->data, bzimage_align ( highest->len ) );
527  if ( user_to_phys ( top, -1 ) > bzimg->mem_limit ) {
528  top = phys_to_user ( ( bzimg->mem_limit + 1 ) &
529  ~( INITRD_ALIGN - 1 ) );
530  }
531  DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
532  image, user_to_phys ( top, -1 ) );
533 
534  /* Load initrds in order */
535  for_each_image ( initrd ) {
536 
537  /* Calculate cumulative length of following
538  * initrds (including padding).
539  */
540  offset = 0;
541  for_each_image ( other ) {
542  if ( other == initrd )
543  offset = 0;
544  offset += bzimage_load_initrd ( image, other, UNULL );
546  }
547 
548  /* Load initrd at this address */
549  dest = userptr_add ( top, -offset );
550  len = bzimage_load_initrd ( image, initrd, dest );
551 
552  /* Record initrd location */
553  if ( ! bzimg->ramdisk_image )
554  bzimg->ramdisk_image = user_to_phys ( dest, 0 );
555  bzimg->ramdisk_size = ( user_to_phys ( dest, len ) -
556  bzimg->ramdisk_image );
557  }
558  DBGC ( image, "bzImage %p initrds at [%#08lx,%#08lx)\n",
559  image, bzimg->ramdisk_image,
560  ( bzimg->ramdisk_image + bzimg->ramdisk_size ) );
561 }
userptr_t data
Raw file image.
Definition: image.h:41
physaddr_t ramdisk_size
Initrd size.
Definition: bzimage.c:81
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
void initrd_reshuffle(userptr_t bottom)
Reshuffle initrds into desired order at top of memory.
Definition: initrd.c:229
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
static size_t bzimage_load_initrd(struct image *image, struct image *initrd, userptr_t address)
Load initrd.
Definition: bzimage.c:374
An executable image.
Definition: image.h:24
#define INITRD_ALIGN
Minimum alignment for initrds.
Definition: initrd.h:19
off_t userptr_sub(userptr_t userptr, userptr_t subtrahend)
Subtract user pointers.
userptr_t userptr_add(userptr_t userptr, off_t offset)
Add offset to user pointer.
physaddr_t ramdisk_image
Initrd address.
Definition: bzimage.c:79
userptr_t pm_kernel
Non-real-mode kernel portion load address.
Definition: bzimage.c:71
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void * dest
Definition: strings.h:176
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:149
size_t len
Length of raw file image.
Definition: image.h:43
static userptr_t top
Top of heap.
static size_t bzimage_align(size_t len)
Align initrd length.
Definition: bzimage.c:361
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
uint32_t len
Length.
Definition: ena.h:14
size_t pm_sz
Non-real-mode kernel portion file and memory size.
Definition: bzimage.c:73
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:77
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References bzimage_align(), bzimage_load_initrd(), image::data, DBGC, dest, for_each_image, INITRD_ALIGN, initrd_reshuffle(), len, image::len, bzimage_context::mem_limit, NULL, offset, phys_to_user(), bzimage_context::pm_kernel, bzimage_context::pm_sz, bzimage_context::ramdisk_image, bzimage_context::ramdisk_size, top, UNULL, user_to_phys(), userptr_add(), and userptr_sub().

Referenced by bzimage_exec().

◆ bzimage_exec()

static int bzimage_exec ( struct image image)
static

Execute bzImage image.

Parameters
imagebzImage image
Return values
rcReturn status code

Definition at line 569 of file bzimage.c.

569  {
570  struct bzimage_context bzimg;
571  const char *cmdline = ( image->cmdline ? image->cmdline : "" );
572  int rc;
573 
574  /* Read and parse header from image */
575  if ( ( rc = bzimage_parse_header ( image, &bzimg,
576  image->data ) ) != 0 )
577  return rc;
578 
579  /* Prepare segments */
580  if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
581  bzimg.rm_memsz ) ) != 0 ) {
582  DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
583  image, strerror ( rc ) );
584  return rc;
585  }
586  if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
587  bzimg.pm_sz ) ) != 0 ) {
588  DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
589  image, strerror ( rc ) );
590  return rc;
591  }
592 
593  /* Parse command line for bootloader parameters */
594  if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
595  return rc;
596 
597  /* Check that initrds can be loaded */
598  if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
599  return rc;
600 
601  /* Remove kernel from image list (without invalidating image pointer) */
603 
604  /* Load segments */
605  memcpy_user ( bzimg.rm_kernel, 0, image->data,
606  0, bzimg.rm_filesz );
607  memcpy_user ( bzimg.pm_kernel, 0, image->data,
608  bzimg.rm_filesz, bzimg.pm_sz );
609 
610  /* Store command line */
611  bzimage_set_cmdline ( image, &bzimg, cmdline );
612 
613  /* Prepare for exiting. Must do this before loading initrds,
614  * since loading the initrds will corrupt the external heap.
615  */
616  shutdown_boot();
617 
618  /* Load any initrds */
619  bzimage_load_initrds ( image, &bzimg );
620 
621  /* Update kernel header */
622  bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
623 
624  DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
625  "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
626  bzimg.rm_kernel_seg, bzimg.rm_heap );
627 
628  /* Jump to the kernel */
629  __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
630  "movw %w0, %%es\n\t"
631  "movw %w0, %%fs\n\t"
632  "movw %w0, %%gs\n\t"
633  "movw %w0, %%ss\n\t"
634  "movw %w1, %%sp\n\t"
635  "pushw %w2\n\t"
636  "pushw $0\n\t"
637  "lret\n\t" )
638  : : "R" ( bzimg.rm_kernel_seg ),
639  "R" ( bzimg.rm_heap ),
640  "R" ( bzimg.rm_kernel_seg + 0x20 ) );
641 
642  /* There is no way for the image to return, since we provide
643  * no return address.
644  */
645  assert ( 0 );
646 
647  return -ECANCELED; /* -EIMPOSSIBLE */
648 }
static void bzimage_load_initrds(struct image *image, struct bzimage_context *bzimg)
Load initrds, if any.
Definition: bzimage.c:500
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
static void bzimage_set_cmdline(struct image *image, struct bzimage_context *bzimg, const char *cmdline)
Set command line.
Definition: bzimage.c:315
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:196
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define ECANCELED
Operation canceled.
Definition: errno.h:343
char * cmdline
Command line to pass to image.
Definition: image.h:39
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int prep_segment(userptr_t segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:60
static int bzimage_parse_header(struct image *image, struct bzimage_context *bzimg, userptr_t src)
Parse bzImage header.
Definition: bzimage.c:97
static int bzimage_check_initrds(struct image *image, struct bzimage_context *bzimg)
Check that initrds can be loaded.
Definition: bzimage.c:443
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void bzimage_update_header(struct image *image, struct bzimage_context *bzimg, userptr_t dst)
Update bzImage header in loaded kernel.
Definition: bzimage.c:202
bzImage context
Definition: bzimage.c:53
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:252
__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 int bzimage_parse_cmdline(struct image *image, struct bzimage_context *bzimg, const char *cmdline)
Parse kernel command line for bootloader parameters.
Definition: bzimage.c:253
uint32_t cmdline
Definition: multiboot.h:16
static void shutdown_boot(void)
Shut down system for OS boot.
Definition: init.h:76
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
void memcpy_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers.

References __asm__(), __volatile__(), assert(), bzimage_check_initrds(), bzimage_load_initrds(), bzimage_parse_cmdline(), bzimage_parse_header(), bzimage_set_cmdline(), bzimage_update_header(), cmdline, image::cmdline, image::data, DBGC, ECANCELED, image_get(), memcpy_user(), bzimage_context::pm_kernel, bzimage_context::pm_sz, prep_segment(), rc, REAL_CODE, bzimage_context::rm_filesz, bzimage_context::rm_heap, bzimage_context::rm_kernel, bzimage_context::rm_kernel_seg, bzimage_context::rm_memsz, shutdown_boot(), strerror(), and unregister_image().

◆ bzimage_probe()

int bzimage_probe ( struct image image)

Probe bzImage image.

Parameters
imagebzImage file
Return values
rcReturn status code

Definition at line 656 of file bzimage.c.

656  {
657  struct bzimage_context bzimg;
658  int rc;
659 
660  /* Read and parse header from image */
661  if ( ( rc = bzimage_parse_header ( image, &bzimg,
662  image->data ) ) != 0 )
663  return rc;
664 
665  return 0;
666 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
An executable image.
Definition: image.h:24
static int bzimage_parse_header(struct image *image, struct bzimage_context *bzimg, userptr_t src)
Parse bzImage header.
Definition: bzimage.c:97
bzImage context
Definition: bzimage.c:53

References bzimage_parse_header(), image::data, and rc.

◆ __image_type()

struct image_type bzimage_image_type __image_type ( PROBE_NORMAL  )

Linux bzImage image type.