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 <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 48 of file runtime.c.

◆ initrd_phys

#define initrd_phys   __use_data16 ( initrd_phys )

Definition at line 55 of file runtime.c.

◆ initrd_len

#define initrd_len   __use_data16 ( initrd_len )

Definition at line 62 of file runtime.c.

◆ colour

#define colour   &cmdline_image

Colour for debug messages.

Definition at line 83 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 68 of file runtime.c.

68  {
69  struct image *image = container_of ( refcnt, struct image, refcnt );
70 
71  DBGC ( image, "RUNTIME freeing command line\n" );
72  free ( cmdline_copy );
73 }
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
A reference counter.
Definition: refcnt.h:26
#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:65

References cmdline_copy, container_of, DBGC, and free.

◆ 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 91 of file runtime.c.

91  {
92  char *strip;
93  char *strip_end;
94 
95  /* Find unwanted cruft, if present */
96  if ( ! ( strip = strstr ( cmdline, cruft ) ) )
97  return;
98 
99  /* Strip unwanted cruft */
100  strip_end = strchr ( strip, ' ' );
101  if ( strip_end ) {
102  *strip_end = '\0';
103  DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
104  strcpy ( strip, ( strip_end + 1 ) );
105  } else {
106  DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
107  *strip = '\0';
108  }
109 }
#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:326
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:83

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 116 of file runtime.c.

116  {
117  userptr_t cmdline_user;
118  char *cmdline;
119  size_t len;
120  int rc;
121 
122  /* Do nothing if no command line was specified */
123  if ( ! cmdline_phys ) {
124  DBGC ( colour, "RUNTIME found no command line\n" );
125  return 0;
126  }
127  cmdline_user = phys_to_user ( cmdline_phys );
128  len = ( strlen_user ( cmdline_user, 0 ) + 1 /* NUL */ );
129 
130  /* Allocate and copy command line */
131  cmdline_copy = malloc ( len );
132  if ( ! cmdline_copy ) {
133  DBGC ( colour, "RUNTIME could not allocate %zd bytes for "
134  "command line\n", len );
135  rc = -ENOMEM;
136  goto err_alloc_cmdline_copy;
137  }
139  copy_from_user ( cmdline, cmdline_user, 0, len );
140  DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
142 
143  /* Mark command line as consumed */
144  cmdline_phys = 0;
145 
146  /* Strip unwanted cruft from the command line */
147  cmdline_strip ( cmdline, "BOOT_IMAGE=" );
148  cmdline_strip ( cmdline, "initrd=" );
149  while ( isspace ( *cmdline ) )
150  cmdline++;
151  DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );
152 
153  /* Prepare and register image */
156  if ( cmdline_image.len ) {
157  if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
158  DBGC ( colour, "RUNTIME could not register command "
159  "line: %s\n", strerror ( rc ) );
160  goto err_register_image;
161  }
162  }
163 
164  /* Drop our reference to the image */
166 
167  return 0;
168 
169  err_register_image:
171  err_alloc_cmdline_copy:
172  return rc;
173 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
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
#define DBGC(...)
Definition: compiler.h:505
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
#define cmdline_phys
Definition: runtime.c:48
#define ENOMEM
Not enough space.
Definition: errno.h:534
int register_image(struct image *image)
Register executable image.
Definition: image.c:264
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
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:228
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
static struct image cmdline_image
Embedded script representing the command line.
Definition: runtime.c:76
static void cmdline_strip(char *cmdline, const char *cruft)
Strip unwanted cruft from command line.
Definition: runtime.c:91
size_t strlen_user(userptr_t userptr, off_t offset)
Find length of NUL-terminated string in user buffer.
uint32_t len
Length.
Definition: ena.h:14
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
uint32_t cmdline
Definition: multiboot.h:16
static char * cmdline_copy
Internal copy of the command line.
Definition: runtime.c:65
#define colour
Colour for debug messages.
Definition: runtime.c:83
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

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

Referenced by runtime_init().

◆ initrd_init()

static int initrd_init ( void  )
static

Initialise initrd.

Return values
rcReturn status code

Definition at line 180 of file runtime.c.

180  {
181  struct image *image;
182 
183  /* Do nothing if no initrd was specified */
184  if ( ! initrd_phys ) {
185  DBGC ( colour, "RUNTIME found no initrd\n" );
186  return 0;
187  }
188  if ( ! initrd_len ) {
189  DBGC ( colour, "RUNTIME found empty initrd\n" );
190  return 0;
191  }
192  DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
194 
195  /* Create initrd image */
196  image = image_memory ( "<INITRD>", phys_to_user ( initrd_phys ),
197  initrd_len );
198  if ( ! image ) {
199  DBGC ( colour, "RUNTIME could not create initrd image\n" );
200  return -ENOMEM;
201  }
202 
203  /* Mark initrd as consumed */
204  initrd_phys = 0;
205 
206  return 0;
207 }
#define DBGC(...)
Definition: compiler.h:505
struct image * image_memory(const char *name, userptr_t data, size_t len)
Create registered image from block of memory.
Definition: image.c:547
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define initrd_phys
Definition: runtime.c:55
#define colour
Colour for debug messages.
Definition: runtime.c:83
#define initrd_len
Definition: runtime.c:62

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

Referenced by runtime_init().

◆ runtime_init()

static void runtime_init ( void  )
static

Initialise command line and initrd.

Definition at line 213 of file runtime.c.

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

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 65 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,
}
#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:68

Embedded script representing the command line.

Definition at line 76 of file runtime.c.

Referenced by cmdline_init().