iPXE
Data Structures | Functions
bzimage.c File Reference

Linux bzImage image format. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <bzimage.h>
#include <ipxe/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)
 Parse bzImage header. More...
 
static void bzimage_update_header (struct image *image, struct bzimage_context *bzimg)
 Update bzImage header in loaded kernel. More...
 
static int bzimage_parse_cmdline (struct image *image, struct bzimage_context *bzimg)
 Parse kernel command line for bootloader parameters. More...
 
static void bzimage_set_cmdline (struct image *image, struct bzimage_context *bzimg)
 Set command line. 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 
)
static

Parse bzImage header.

Parameters
imagebzImage file
bzimgbzImage context
Return values
rcReturn status code

Definition at line 92 of file bzimage.c.

93  {
94  const struct bzimage_header *bzhdr;
95  unsigned int syssize;
96  int is_bzimage;
97 
98  /* Initialise context */
99  memset ( bzimg, 0, sizeof ( *bzimg ) );
100 
101  /* Sanity check */
102  if ( image->len < ( BZI_HDR_OFFSET + sizeof ( *bzhdr ) ) ) {
103  DBGC ( image, "bzImage %s too short for kernel header\n",
104  image->name );
105  return -ENOEXEC;
106  }
107  bzhdr = ( image->data + BZI_HDR_OFFSET );
108 
109  /* Calculate size of real-mode portion */
110  bzimg->rm_filesz = ( ( ( bzhdr->setup_sects ?
111  bzhdr->setup_sects : 4 ) + 1 ) << 9 );
112  if ( bzimg->rm_filesz > image->len ) {
113  DBGC ( image, "bzImage %s too short for %zd byte of setup\n",
114  image->name, bzimg->rm_filesz );
115  return -ENOEXEC;
116  }
117  bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
118 
119  /* Calculate size of protected-mode portion */
120  bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
121  syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
122 
123  /* Check for signatures and determine version */
124  if ( bzhdr->boot_flag != BZI_BOOT_FLAG ) {
125  DBGC ( image, "bzImage %s missing 55AA signature\n",
126  image->name );
127  return -ENOEXEC;
128  }
129  if ( bzhdr->header == BZI_SIGNATURE ) {
130  /* 2.00+ */
131  bzimg->version = bzhdr->version;
132  } else {
133  /* Pre-2.00. Check that the syssize field is correct,
134  * as a guard against accepting arbitrary binary data,
135  * since the 55AA check is pretty lax. Note that the
136  * syssize field is unreliable for protocols between
137  * 2.00 and 2.03 inclusive, so we should not always
138  * check this field.
139  */
140  bzimg->version = 0x0100;
141  if ( bzhdr->syssize != syssize ) {
142  DBGC ( image, "bzImage %s bad syssize %x (expected "
143  "%x)\n", image->name, bzhdr->syssize,
144  syssize );
145  return -ENOEXEC;
146  }
147  }
148 
149  /* Determine image type */
150  is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
151  ( bzhdr->loadflags & BZI_LOAD_HIGH ) : 0 );
152 
153  /* Calculate load address of real-mode portion */
154  bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
155  bzimg->rm_kernel = real_to_virt ( bzimg->rm_kernel_seg, 0 );
156 
157  /* Allow space for the stack and heap */
158  bzimg->rm_memsz += BZI_STACK_SIZE;
159  bzimg->rm_heap = bzimg->rm_memsz;
160 
161  /* Allow space for the command line */
162  bzimg->rm_cmdline = bzimg->rm_memsz;
163  bzimg->rm_memsz += BZI_CMDLINE_SIZE;
164 
165  /* Calculate load address of protected-mode portion */
166  bzimg->pm_kernel = phys_to_virt ( is_bzimage ? BZI_LOAD_HIGH_ADDR
167  : BZI_LOAD_LOW_ADDR );
168 
169  /* Extract video mode */
170  bzimg->vid_mode = bzhdr->vid_mode;
171 
172  /* Extract memory limit */
173  bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
174  bzhdr->initrd_addr_max : BZI_INITRD_MAX );
175 
176  /* Extract command line size */
177  bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
178  bzhdr->cmdline_size : BZI_CMDLINE_SIZE );
179 
180  DBGC ( image, "bzImage %s version %04x RM %#lx+%#zx PM %#lx+%#zx "
181  "cmdlen %zd\n", image->name, bzimg->version,
182  virt_to_phys ( bzimg->rm_kernel ), bzimg->rm_filesz,
183  virt_to_phys ( bzimg->pm_kernel ), bzimg->pm_sz,
184  bzimg->cmdline_size );
185 
186  return 0;
187 }
size_t rm_heap
Real-mode heap top (offset from rm_kernel)
Definition: bzimage.c:64
#define BZI_CMDLINE_SIZE
Maximum size of command line.
Definition: bzimage.h:140
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:66
size_t rm_filesz
Real-mode kernel portion file size.
Definition: bzimage.c:62
#define DBGC(...)
Definition: compiler.h:505
size_t rm_memsz
Real-mode kernel portion total memory size.
Definition: bzimage.c:70
An executable image.
Definition: image.h:23
void * rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:60
static __always_inline void * real_to_virt(unsigned int segment, unsigned int offset)
Convert segment:offset address to virtual address.
Definition: realmode.h:77
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
unsigned int rm_kernel_seg
Real-mode kernel portion load segment address.
Definition: bzimage.c:58
#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:68
#define BZI_HDR_OFFSET
Offset of bzImage header within kernel image.
Definition: bzimage.h:78
#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:55
void * pm_kernel
Non-real-mode kernel portion load address.
Definition: bzimage.c:72
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:76
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:74
#define BZI_ASSUMED_RM_SIZE
Assumed size of real-mode portion (including .bss)
Definition: bzimage.h:134
#define BZI_LOAD_HIGH
bzImage "load high" flag
Definition: bzimage.h:96
unsigned int version
Boot protocol version.
Definition: bzimage.c:56
char * name
Name.
Definition: image.h:37
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
A bzImage header.
Definition: bzimage.h:13
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:78
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, BZI_ASSUMED_RM_SIZE, BZI_BOOT_FLAG, 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_size, bzimage_header::cmdline_size, image::data, DBGC, ENOEXEC, bzimage_header::header, bzimage_header::initrd_addr_max, image::len, bzimage_header::loadflags, bzimage_context::mem_limit, memset(), image::name, bzimage_context::pm_kernel, bzimage_context::pm_sz, real_to_virt(), 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, syssize, bzimage_header::syssize, 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 
)
static

Update bzImage header in loaded kernel.

Parameters
imagebzImage file
bzimgbzImage context

Definition at line 195 of file bzimage.c.

196  {
197  struct bzimage_header *bzhdr = ( bzimg->rm_kernel + BZI_HDR_OFFSET );
198  struct bzimage_cmdline *cmdline;
199 
200  /* Set loader type */
201  if ( bzimg->version >= 0x0200 )
203 
204  /* Set heap end pointer */
205  if ( bzimg->version >= 0x0201 ) {
206  bzhdr->heap_end_ptr = ( bzimg->rm_heap - 0x200 );
207  bzhdr->loadflags |= BZI_CAN_USE_HEAP;
208  }
209 
210  /* Set command line */
211  if ( bzimg->version >= 0x0202 ) {
212  bzhdr->cmd_line_ptr = ( virt_to_phys ( bzimg->rm_kernel )
213  + bzimg->rm_cmdline );
214  } else {
215  cmdline = ( bzimg->rm_kernel + BZI_CMDLINE_OFFSET );
216  cmdline->magic = BZI_CMDLINE_MAGIC;
217  cmdline->offset = bzimg->rm_cmdline;
218  if ( bzimg->version >= 0x0200 )
219  bzhdr->setup_move_size = bzimg->rm_memsz;
220  }
221 
222  /* Set video mode */
223  bzhdr->vid_mode = bzimg->vid_mode;
224  DBGC ( image, "bzImage %s vidmode %d\n",
225  image->name, bzhdr->vid_mode );
226 
227  /* Set initrd address */
228  if ( bzimg->version >= 0x0200 ) {
229  bzhdr->ramdisk_image = virt_to_phys ( bzimg->initrd );
230  bzhdr->ramdisk_size = bzimg->initrd_size;
231  }
232 }
#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:64
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:66
#define DBGC(...)
Definition: compiler.h:505
size_t rm_memsz
Real-mode kernel portion total memory size.
Definition: bzimage.c:70
An executable image.
Definition: image.h:23
#define BZI_CMDLINE_OFFSET
Offset of bzImage command-line structure within kernel image.
Definition: bzimage.h:128
void * rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:60
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
void * initrd
Initrd address.
Definition: bzimage.c:80
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
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
uint16_t setup_move_size
Move to high memory size (used with hooks)
Definition: bzimage.h:50
uint32_t cmd_line_ptr
32-bit pointer to the kernel command line
Definition: bzimage.h:64
physaddr_t initrd_size
Initrd size.
Definition: bzimage.c:82
unsigned int vid_mode
Video mode.
Definition: bzimage.c:76
bzImage command-line structure used by older kernels
Definition: bzimage.h:120
uint32_t cmdline
Definition: multiboot.h:16
unsigned int version
Boot protocol version.
Definition: bzimage.c:56
char * name
Name.
Definition: image.h:37
uint16_t vid_mode
Video mode control.
Definition: bzimage.h:28
uint8_t loadflags
Boot protocol option flags.
Definition: bzimage.h:48
A bzImage header.
Definition: bzimage.h:13

References BZI_CAN_USE_HEAP, BZI_CMDLINE_MAGIC, BZI_CMDLINE_OFFSET, BZI_HDR_OFFSET, BZI_LOADER_TYPE_IPXE, bzimage_header::cmd_line_ptr, cmdline, DBGC, bzimage_header::heap_end_ptr, bzimage_context::initrd, bzimage_context::initrd_size, bzimage_header::loadflags, image::name, bzimage_header::ramdisk_image, bzimage_header::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, 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 
)
static

Parse kernel command line for bootloader parameters.

Parameters
imagebzImage file
bzimgbzImage context
Return values
rcReturn status code

Definition at line 241 of file bzimage.c.

242  {
243  const char *vga;
244  const char *mem;
245  char *sep;
246  char *end;
247 
248  /* Look for "vga=" */
249  if ( ( vga = image_argument ( image, "vga=" ) ) ) {
250  sep = strchr ( vga, ' ' );
251  if ( sep )
252  *sep = '\0';
253  if ( strcmp ( vga, "normal" ) == 0 ) {
254  bzimg->vid_mode = BZI_VID_MODE_NORMAL;
255  } else if ( strcmp ( vga, "ext" ) == 0 ) {
256  bzimg->vid_mode = BZI_VID_MODE_EXT;
257  } else if ( strcmp ( vga, "ask" ) == 0 ) {
258  bzimg->vid_mode = BZI_VID_MODE_ASK;
259  } else {
260  bzimg->vid_mode = strtoul ( vga, &end, 0 );
261  if ( *end ) {
262  DBGC ( image, "bzImage %s strange \"vga=\" "
263  "terminator '%c'\n",
264  image->name, *end );
265  }
266  }
267  if ( sep )
268  *sep = ' ';
269  }
270 
271  /* Look for "mem=" */
272  if ( ( mem = image_argument ( image, "mem=" ) ) ) {
273  bzimg->mem_limit = strtoul ( mem, &end, 0 );
274  switch ( *end ) {
275  case 'G':
276  case 'g':
277  bzimg->mem_limit <<= 10;
278  /* Fall through */
279  case 'M':
280  case 'm':
281  bzimg->mem_limit <<= 10;
282  /* Fall through */
283  case 'K':
284  case 'k':
285  bzimg->mem_limit <<= 10;
286  break;
287  case '\0':
288  case ' ':
289  break;
290  default:
291  DBGC ( image, "bzImage %s strange \"mem=\" "
292  "terminator '%c'\n", image->name, *end );
293  break;
294  }
295  bzimg->mem_limit -= 1;
296  }
297 
298  return 0;
299 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:484
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
#define BZI_VID_MODE_ASK
bzImage special video mode "ask"
Definition: bzimage.h:114
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
#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:173
unsigned int vid_mode
Video mode.
Definition: bzimage.c:76
const char * image_argument(struct image *image, const char *key)
Find argument within image command line.
Definition: image.c:652
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define BZI_VID_MODE_NORMAL
bzImage special video mode "normal"
Definition: bzimage.h:108
char * name
Name.
Definition: image.h:37
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:78

References BZI_VID_MODE_ASK, BZI_VID_MODE_EXT, BZI_VID_MODE_NORMAL, DBGC, end, image_argument(), bzimage_context::mem_limit, image::name, strchr(), strcmp(), 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 
)
static

Set command line.

Parameters
imagebzImage image
bzimgbzImage context

Definition at line 307 of file bzimage.c.

308  {
309  const char *cmdline = ( image->cmdline ? image->cmdline : "" );
310  char *rm_cmdline;
311 
312  /* Copy command line down to real-mode portion */
313  rm_cmdline = ( bzimg->rm_kernel + bzimg->rm_cmdline );
314  snprintf ( rm_cmdline, bzimg->cmdline_size, "%s", cmdline );
315  DBGC ( image, "bzImage %s command line \"%s\"\n",
316  image->name, rm_cmdline );
317 }
size_t rm_cmdline
Command line (offset from rm_kernel)
Definition: bzimage.c:66
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
void * rm_kernel
Real-mode kernel portion load address.
Definition: bzimage.c:60
char * cmdline
Command line to pass to image.
Definition: image.h:42
size_t cmdline_size
Command line maximum length.
Definition: bzimage.c:68
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
uint32_t cmdline
Definition: multiboot.h:16
char * name
Name.
Definition: image.h:37

References cmdline, image::cmdline, bzimage_context::cmdline_size, DBGC, image::name, bzimage_context::rm_cmdline, bzimage_context::rm_kernel, and snprintf().

Referenced by bzimage_exec().

◆ 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 326 of file bzimage.c.

327  {
328  struct memmap_region region;
329  physaddr_t min;
330  physaddr_t max;
332  int rc;
333 
334  /* Calculate total loaded length of initrds */
335  bzimg->initrd_size = initrd_len();
336 
337  /* Succeed if there are no initrds */
338  if ( ! bzimg->initrd_size )
339  return 0;
340 
341  /* Calculate available load region after reshuffling */
342  if ( ( rc = initrd_region ( bzimg->initrd_size, &region ) ) != 0 ) {
343  DBGC ( image, "bzImage %s no region for initrds: %s\n",
344  image->name, strerror ( rc ) );
345  return rc;
346  }
347 
348  /* Limit region to avoiding kernel itself */
349  min = virt_to_phys ( bzimg->pm_kernel + bzimg->pm_sz );
350  if ( min < region.min )
351  min = region.min;
352 
353  /* Limit region to kernel's memory limit */
354  max = region.max;
355  if ( max > bzimg->mem_limit )
356  max = bzimg->mem_limit;
357 
358  /* Calculate installation address */
359  if ( max < ( bzimg->initrd_size - 1 ) ) {
360  DBGC ( image, "bzImage %s not enough space for initrds\n",
361  image->name );
362  return -ENOBUFS;
363  }
364  dest = ( ( max + 1 - bzimg->initrd_size ) & ~( INITRD_ALIGN - 1 ) );
365  if ( dest < min ) {
366  DBGC ( image, "bzImage %s not enough space for initrds\n",
367  image->name );
368  return -ENOBUFS;
369  }
370  bzimg->initrd = phys_to_virt ( dest );
371 
372  DBGC ( image, "bzImage %s loading initrds from %#08lx downwards\n",
373  image->name, max );
374  return 0;
375 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define max(x, y)
Definition: ath.h:40
#define DBGC(...)
Definition: compiler.h:505
#define min(x, y)
Definition: ath.h:35
int initrd_region(size_t len, struct memmap_region *region)
Calculate post-reshuffle initrd load region.
Definition: initrd.c:354
An executable image.
Definition: image.h:23
#define INITRD_ALIGN
Initial ramdisk chunk alignment.
Definition: initrd.h:16
void * initrd
Initrd address.
Definition: bzimage.c:80
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * pm_kernel
Non-real-mode kernel portion load address.
Definition: bzimage.c:72
unsigned long physaddr_t
Definition: stdint.h:20
physaddr_t initrd_size
Initrd size.
Definition: bzimage.c:82
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
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
size_t pm_sz
Non-real-mode kernel portion file and memory size.
Definition: bzimage.c:74
A memory region descriptor.
Definition: memmap.h:48
char * name
Name.
Definition: image.h:37
uint64_t mem_limit
Memory limit.
Definition: bzimage.c:78
#define initrd_len
Definition: runtime.c:63

References DBGC, dest, ENOBUFS, bzimage_context::initrd, INITRD_ALIGN, initrd_len, initrd_region(), bzimage_context::initrd_size, max, memmap_region::max, bzimage_context::mem_limit, min, memmap_region::min, image::name, bzimage_context::pm_kernel, bzimage_context::pm_sz, rc, and strerror().

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 383 of file bzimage.c.

384  {
385  size_t len;
386 
387  /* Do nothing if there are no initrds */
388  if ( ! bzimg->initrd )
389  return;
390 
391  /* Reshuffle initrds into desired order */
393 
394  /* Load initrds */
395  DBGC ( image, "bzImage %s initrds at [%#08lx,%#08lx)\n",
396  image->name, virt_to_phys ( bzimg->initrd ),
397  ( virt_to_phys ( bzimg->initrd ) + bzimg->initrd_size ) );
398  len = initrd_load_all ( bzimg->initrd );
399  assert ( len == bzimg->initrd_size );
400 }
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
void initrd_reshuffle(void)
Reshuffle initrds into desired order at top of memory.
Definition: initrd.c:229
void * initrd
Initrd address.
Definition: bzimage.c:80
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
size_t initrd_load_all(void *address)
Load all initrds.
Definition: initrd.c:317
ring len
Length.
Definition: dwmac.h:231
physaddr_t initrd_size
Initrd size.
Definition: bzimage.c:82
char * name
Name.
Definition: image.h:37

References assert(), DBGC, bzimage_context::initrd, initrd_load_all(), initrd_reshuffle(), bzimage_context::initrd_size, len, and image::name.

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 408 of file bzimage.c.

408  {
409  struct bzimage_context bzimg;
410  int rc;
411 
412  /* Read and parse header from image */
413  if ( ( rc = bzimage_parse_header ( image, &bzimg ) ) != 0 )
414  return rc;
415 
416  /* Prepare segments */
417  if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
418  bzimg.rm_memsz ) ) != 0 ) {
419  DBGC ( image, "bzImage %s could not prepare RM segment: %s\n",
420  image->name, strerror ( rc ) );
421  return rc;
422  }
423  if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
424  bzimg.pm_sz ) ) != 0 ) {
425  DBGC ( image, "bzImage %s could not prepare PM segment: %s\n",
426  image->name, strerror ( rc ) );
427  return rc;
428  }
429 
430  /* Parse command line for bootloader parameters */
431  if ( ( rc = bzimage_parse_cmdline ( image, &bzimg ) ) != 0)
432  return rc;
433 
434  /* Check that initrds can be loaded */
435  if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
436  return rc;
437 
438  /* Remove kernel from image list (without invalidating image pointer) */
440 
441  /* Load segments */
442  memcpy ( bzimg.rm_kernel, image->data, bzimg.rm_filesz );
443  memcpy ( bzimg.pm_kernel, ( image->data + bzimg.rm_filesz ),
444  bzimg.pm_sz );
445 
446  /* Store command line */
447  bzimage_set_cmdline ( image, &bzimg );
448 
449  /* Prepare for exiting. Must do this before loading initrds,
450  * since loading the initrds will corrupt the external heap.
451  */
452  shutdown_boot();
453 
454  /* Load any initrds */
455  bzimage_load_initrds ( image, &bzimg );
456 
457  /* Update kernel header */
458  bzimage_update_header ( image, &bzimg );
459 
460  DBGC ( image, "bzImage %s jumping to RM kernel at %04x:0000 (stack "
461  "%04x:%04zx)\n", image->name, ( bzimg.rm_kernel_seg + 0x20 ),
462  bzimg.rm_kernel_seg, bzimg.rm_heap );
463 
464  /* Jump to the kernel */
465  __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
466  "movw %w0, %%es\n\t"
467  "movw %w0, %%fs\n\t"
468  "movw %w0, %%gs\n\t"
469  "movw %w0, %%ss\n\t"
470  "movw %w1, %%sp\n\t"
471  "pushw %w2\n\t"
472  "pushw $0\n\t"
473  "lret\n\t" )
474  : : "R" ( bzimg.rm_kernel_seg ),
475  "R" ( bzimg.rm_heap ),
476  "R" ( bzimg.rm_kernel_seg + 0x20 ) );
477 
478  /* There is no way for the image to return, since we provide
479  * no return address.
480  */
481  assert ( 0 );
482 
483  return -ECANCELED; /* -EIMPOSSIBLE */
484 }
static void bzimage_load_initrds(struct image *image, struct bzimage_context *bzimg)
Load initrds, if any.
Definition: bzimage.c:383
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Read-only data.
Definition: image.h:50
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:239
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
#define ECANCELED
Operation canceled.
Definition: errno.h:343
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int bzimage_parse_header(struct image *image, struct bzimage_context *bzimg)
Parse bzImage header.
Definition: bzimage.c:92
static int bzimage_parse_cmdline(struct image *image, struct bzimage_context *bzimg)
Parse kernel command line for bootloader parameters.
Definition: bzimage.c:241
static void bzimage_set_cmdline(struct image *image, struct bzimage_context *bzimg)
Set command line.
Definition: bzimage.c:307
static int bzimage_check_initrds(struct image *image, struct bzimage_context *bzimg)
Check that initrds can be loaded.
Definition: bzimage.c:326
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
__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))
bzImage context
Definition: bzimage.c:54
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357
__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 void bzimage_update_header(struct image *image, struct bzimage_context *bzimg)
Update bzImage header in loaded kernel.
Definition: bzimage.c:195
int prep_segment(void *segment, size_t filesz, size_t memsz)
Prepare segment for loading.
Definition: segment.c:61
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

References __asm__(), __volatile__(), assert(), bzimage_check_initrds(), bzimage_load_initrds(), bzimage_parse_cmdline(), bzimage_parse_header(), bzimage_set_cmdline(), bzimage_update_header(), image::data, DBGC, ECANCELED, image_get(), memcpy(), image::name, 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 492 of file bzimage.c.

492  {
493  struct bzimage_context bzimg;
494  int rc;
495 
496  /* Read and parse header from image */
497  if ( ( rc = bzimage_parse_header ( image, &bzimg ) ) != 0 )
498  return rc;
499 
500  return 0;
501 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:23
static int bzimage_parse_header(struct image *image, struct bzimage_context *bzimg)
Parse bzImage header.
Definition: bzimage.c:92
bzImage context
Definition: bzimage.c:54

References bzimage_parse_header(), and rc.

◆ __image_type()

struct image_type bzimage_image_type __image_type ( PROBE_NORMAL  )

Linux bzImage image type.