iPXE
Macros | Functions | Variables
runtime.c File Reference

Command line and initrd passed to iPXE at runtime. More...

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/init.h>
#include <ipxe/image.h>
#include <ipxe/script.h>
#include <realmode.h>

Go to the source code of this file.

Macros

#define cmdline_phys   __use_data16 ( cmdline_phys )
 
#define initrd_phys   __use_data16 ( initrd_phys )
 
#define initrd_len   __use_data16 ( initrd_len )
 
#define colour   &cmdline_image
 Colour for debug messages. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
uint32_t __bss16 (cmdline_phys)
 Command line physical address. More...
 
uint32_t __bss16 (initrd_phys)
 initrd physical address More...
 
uint32_t __bss16 (initrd_len)
 initrd length More...
 
static void cmdline_image_free (struct refcnt *refcnt)
 Free command line image. More...
 
static void cmdline_strip (char *cmdline, const char *cruft)
 Strip unwanted cruft from command line. More...
 
static int cmdline_init (void)
 Initialise command line. More...
 
static int initrd_init (void)
 Initialise initrd. More...
 
static void runtime_init (void)
 Initialise command line and initrd. More...
 
struct startup_fn runtime_startup_fn __startup_fn (STARTUP_NORMAL)
 Command line and initrd initialisation function. More...
 

Variables

static char * cmdline_copy
 Internal copy of the command line. More...
 
static struct image cmdline_image
 Embedded script representing the command line. More...
 

Detailed Description

Command line and initrd passed to iPXE at runtime.

Definition in file runtime.c.

Macro Definition Documentation

◆ cmdline_phys

#define cmdline_phys   __use_data16 ( cmdline_phys )

Definition at line 49 of file runtime.c.

◆ initrd_phys

#define initrd_phys   __use_data16 ( initrd_phys )

Definition at line 56 of file runtime.c.

◆ initrd_len

#define initrd_len   __use_data16 ( initrd_len )

Definition at line 63 of file runtime.c.

◆ colour

#define colour   &cmdline_image

Colour for debug messages.

Definition at line 86 of file runtime.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __bss16() [1/3]

uint32_t __bss16 ( cmdline_phys  )

Command line physical address.

This can be set by the prefix.

◆ __bss16() [2/3]

uint32_t __bss16 ( initrd_phys  )

initrd physical address

This can be set by the prefix.

◆ __bss16() [3/3]

uint32_t __bss16 ( initrd_len  )

initrd length

This can be set by the prefix.

◆ cmdline_image_free()

static void cmdline_image_free ( struct refcnt refcnt)
static

Free command line image.

Definition at line 69 of file runtime.c.

69  {
70  struct image *image = container_of ( refcnt, struct image, refcnt );
71 
72  DBGC ( image, "RUNTIME freeing command line\n" );
73  free_image ( refcnt );
74  free ( cmdline_copy );
75 }
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
A reference counter.
Definition: refcnt.h:26
void free_image(struct refcnt *refcnt)
Free executable image.
Definition: image.c:85
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static char * cmdline_copy
Internal copy of the command line.
Definition: runtime.c:66

References cmdline_copy, container_of, DBGC, free, and free_image().

◆ cmdline_strip()

static void cmdline_strip ( char *  cmdline,
const char *  cruft 
)
static

Strip unwanted cruft from command line.

Parameters
cmdlineCommand line
cruftInitial substring of cruft to strip

Definition at line 94 of file runtime.c.

94  {
95  char *strip;
96  char *strip_end;
97 
98  /* Find unwanted cruft, if present */
99  if ( ! ( strip = strstr ( cmdline, cruft ) ) )
100  return;
101 
102  /* Strip unwanted cruft */
103  strip_end = strchr ( strip, ' ' );
104  if ( strip_end ) {
105  *strip_end = '\0';
106  DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
107  strcpy ( strip, ( strip_end + 1 ) );
108  } else {
109  DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
110  *strip = '\0';
111  }
112 }
#define DBGC(...)
Definition: compiler.h:505
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:309
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
uint32_t cmdline
Definition: multiboot.h:16
#define colour
Colour for debug messages.
Definition: runtime.c:86

References cmdline, colour, DBGC, strchr(), strcpy(), and strstr().

Referenced by cmdline_init().

◆ cmdline_init()

static int cmdline_init ( void  )
static

Initialise command line.

Return values
rcReturn status code

Definition at line 119 of file runtime.c.

119  {
120  char *cmdline;
121  int rc;
122 
123  /* Do nothing if no command line was specified */
124  if ( ! cmdline_phys ) {
125  DBGC ( colour, "RUNTIME found no command line\n" );
126  return 0;
127  }
128 
129  /* Allocate and copy command line */
130  cmdline_copy = strdup ( phys_to_virt ( cmdline_phys ) );
131  if ( ! cmdline_copy ) {
132  DBGC ( colour, "RUNTIME could not allocate command line\n" );
133  rc = -ENOMEM;
134  goto err_alloc_cmdline_copy;
135  }
137  DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
139 
140  /* Mark command line as consumed */
141  cmdline_phys = 0;
142 
143  /* Strip unwanted cruft from the command line */
144  cmdline_strip ( cmdline, "BOOT_IMAGE=" );
145  cmdline_strip ( cmdline, "initrd=" );
146  while ( isspace ( *cmdline ) )
147  cmdline++;
148  DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );
149 
150  /* Prepare and register image */
153  if ( cmdline_image.len ) {
154  if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
155  DBGC ( colour, "RUNTIME could not register command "
156  "line: %s\n", strerror ( rc ) );
157  goto err_register_image;
158  }
159  }
160 
161  /* Drop our reference to the image */
163 
164  return 0;
165 
166  err_register_image:
168  err_alloc_cmdline_copy:
169  return rc;
170 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Read-only data.
Definition: image.h:50
#define DBGC(...)
Definition: compiler.h:505
#define cmdline_phys
Definition: runtime.c:49
#define ENOMEM
Not enough space.
Definition: errno.h:534
int register_image(struct image *image)
Register executable image.
Definition: image.c:314
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
char * strdup(const char *src)
Duplicate string.
Definition: string.c:393
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:249
static struct image cmdline_image
Embedded script representing the command line.
Definition: runtime.c:78
static void cmdline_strip(char *cmdline, const char *cruft)
Strip unwanted cruft from command line.
Definition: runtime.c:94
uint32_t cmdline
Definition: multiboot.h:16
static char * cmdline_copy
Internal copy of the command line.
Definition: runtime.c:66
#define colour
Colour for debug messages.
Definition: runtime.c:86

References cmdline, cmdline_copy, cmdline_image, cmdline_phys, cmdline_strip(), colour, image::data, DBGC, ENOMEM, image_put(), isspace(), image::len, rc, register_image(), strdup(), strerror(), and strlen().

Referenced by runtime_init().

◆ initrd_init()

static int initrd_init ( void  )
static

Initialise initrd.

Return values
rcReturn status code

Definition at line 177 of file runtime.c.

177  {
178  struct image *image;
179 
180  /* Do nothing if no initrd was specified */
181  if ( ! initrd_phys ) {
182  DBGC ( colour, "RUNTIME found no initrd\n" );
183  return 0;
184  }
185  if ( ! initrd_len ) {
186  DBGC ( colour, "RUNTIME found empty initrd\n" );
187  return 0;
188  }
189  DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
191 
192  /* Create initrd image */
193  image = image_memory ( "<INITRD>", phys_to_virt ( initrd_phys ),
194  initrd_len );
195  if ( ! image ) {
196  DBGC ( colour, "RUNTIME could not create initrd image\n" );
197  return -ENOMEM;
198  }
199 
200  /* Mark initrd as consumed */
201  initrd_phys = 0;
202 
203  return 0;
204 }
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
#define ENOMEM
Not enough space.
Definition: errno.h:534
struct image * image_memory(const char *name, const void *data, size_t len)
Create registered image from block of memory.
Definition: image.c:608
#define initrd_phys
Definition: runtime.c:56
#define colour
Colour for debug messages.
Definition: runtime.c:86
#define initrd_len
Definition: runtime.c:63

References colour, DBGC, ENOMEM, image_memory(), initrd_len, and initrd_phys.

Referenced by runtime_init().

◆ runtime_init()

static void runtime_init ( void  )
static

Initialise command line and initrd.

Definition at line 210 of file runtime.c.

210  {
211  int rc;
212 
213  /* Initialise command line */
214  if ( ( rc = cmdline_init() ) != 0 ) {
215  /* No way to report failure */
216  return;
217  }
218 
219  /* Initialise initrd */
220  if ( ( rc = initrd_init() ) != 0 ) {
221  /* No way to report failure */
222  return;
223  }
224 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int cmdline_init(void)
Initialise command line.
Definition: runtime.c:119
static int initrd_init(void)
Initialise initrd.
Definition: runtime.c:177

References cmdline_init(), initrd_init(), and rc.

◆ __startup_fn()

struct startup_fn runtime_startup_fn __startup_fn ( STARTUP_NORMAL  )

Command line and initrd initialisation function.

Variable Documentation

◆ cmdline_copy

char* cmdline_copy
static

Internal copy of the command line.

Definition at line 66 of file runtime.c.

Referenced by cmdline_image_free(), and cmdline_init().

◆ cmdline_image

struct image cmdline_image
static
Initial value:
= {
.name = "<CMDLINE>",
.type = &script_image_type,
}
uint32_t type
Operating system type.
Definition: ena.h:12
#define IMAGE_STATIC
Image is statically allocated.
Definition: image.h:88
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition: image.h:91
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
static void cmdline_image_free(struct refcnt *refcnt)
Free command line image.
Definition: runtime.c:69

Embedded script representing the command line.

Definition at line 78 of file runtime.c.

Referenced by cmdline_init().