iPXE
Data Structures | Functions | Variables
script.c File Reference

iPXE scripts More...

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/image.h>
#include <ipxe/shell.h>
#include <usr/prompt.h>
#include <ipxe/script.h>

Go to the source code of this file.

Data Structures

struct  goto_options
 "goto" options More...
 
struct  prompt_options
 "prompt" options More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int process_script (struct image *image, int(*process_line)(struct image *image, size_t offset, const char *label, const char *command), int(*terminate)(int rc))
 Process script lines. More...
 
static int terminate_on_exit_or_failure (int rc)
 Terminate script processing on shell exit or command failure. More...
 
static int script_exec_line (struct image *image, size_t offset, const char *label __unused, const char *command)
 Execute script line. More...
 
static int script_exec (struct image *image)
 Execute script. More...
 
static int script_probe (struct image *image)
 Probe script image. More...
 
struct image_type script_image_type __image_type (PROBE_NORMAL)
 Script image type. More...
 
static int goto_find_label (struct image *image, size_t offset, const char *label, const char *command __unused)
 Check for presence of label. More...
 
static int terminate_on_label_found (int rc)
 Terminate script processing when label is found. More...
 
static int goto_exec (int argc, char **argv)
 "goto" command More...
 
static int prompt_exec (int argc, char **argv)
 "prompt" command More...
 

Variables

static size_t script_offset
 Offset within current script. More...
 
static struct option_descriptor goto_opts [] = {}
 "goto" option list More...
 
static struct command_descriptor goto_cmd
 "goto" command descriptor More...
 
static const char * goto_label
 Current "goto" label. More...
 
struct command goto_command __command
 "goto" command More...
 
static struct option_descriptor prompt_opts []
 "prompt" option list More...
 
static struct command_descriptor prompt_cmd
 "prompt" command descriptor More...
 

Detailed Description

iPXE scripts

Definition in file script.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ process_script()

static int process_script ( struct image image,
int(*)(struct image *image, size_t offset, const char *label, const char *command process_line,
int(*)(int rc terminate 
)
static

Process script lines.

Parameters
imageScript
process_lineLine processor
terminateTermination check
Return values
rcReturn status code

Definition at line 61 of file script.c.

66  {
67  size_t len = 0;
68  char *line = NULL;
69  size_t line_offset;
70  char *label;
71  char *command;
72  off_t eol;
73  size_t frag_len;
74  char *tmp;
75  int rc;
76 
77  /* Initialise script and line offsets */
78  script_offset = 0;
79  line_offset = 0;
80 
81  do {
82 
83  /* Find length of next line, excluding any terminating '\n' */
84  eol = memchr_user ( image->data, script_offset, '\n',
85  ( image->len - script_offset ) );
86  if ( eol < 0 )
87  eol = image->len;
88  frag_len = ( eol - script_offset );
89 
90  /* Allocate buffer for line */
91  tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
92  if ( ! tmp ) {
93  rc = -ENOMEM;
94  goto err_alloc;
95  }
96  line = tmp;
97 
98  /* Copy line */
99  copy_from_user ( ( line + len ), image->data, script_offset,
100  frag_len );
101  len += frag_len;
102 
103  /* Move to next line in script */
104  script_offset += ( frag_len + 1 );
105 
106  /* Strip trailing CR, if present */
107  if ( len && ( line[ len - 1 ] == '\r' ) )
108  len--;
109 
110  /* Handle backslash continuations */
111  if ( len && ( line[ len - 1 ] == '\\' ) ) {
112  len--;
113  rc = -EINVAL;
114  continue;
115  }
116 
117  /* Terminate line */
118  line[len] = '\0';
119 
120  /* Split line into (optional) label and command */
121  command = line;
122  while ( isspace ( *command ) )
123  command++;
124  if ( *command == ':' ) {
125  label = ++command;
126  while ( *command && ! isspace ( *command ) )
127  command++;
128  if ( *command )
129  *(command++) = '\0';
130  } else {
131  label = NULL;
132  }
133 
134  /* Process line */
135  rc = process_line ( image, line_offset, label, command );
136  if ( terminate ( rc ) )
137  goto err_process;
138 
139  /* Free line */
140  free ( line );
141  line = NULL;
142  len = 0;
143 
144  /* Update line offset */
145  line_offset = script_offset;
146 
147  } while ( script_offset < image->len );
148 
149  err_process:
150  err_alloc:
151  free ( line );
152  return rc;
153 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
off_t memchr_user(userptr_t userptr, off_t offset, int c, size_t len)
Find character in user buffer.
A command-line command.
Definition: command.h:9
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
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t len
Length of raw file image.
Definition: image.h:43
uint8_t * tmp
Definition: entropy.h:156
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
signed long off_t
Definition: stdint.h:8
uint32_t len
Length.
Definition: ena.h:14
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
uint8_t command
Command.
Definition: intel.h:18
static size_t script_offset
Offset within current script.
Definition: script.c:51
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References command, copy_from_user(), image::data, EINVAL, ENOMEM, free, isspace(), len, image::len, memchr_user(), NULL, rc, realloc(), script_offset, and tmp.

Referenced by goto_exec(), and script_exec().

◆ terminate_on_exit_or_failure()

static int terminate_on_exit_or_failure ( int  rc)
static

Terminate script processing on shell exit or command failure.

Parameters
rcLine processing status
Return values
terminateTerminate script processing

Definition at line 161 of file script.c.

161  {
162 
164  ( rc != 0 ) );
165 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:227
Stop processing commands.
Definition: shell.h:29

References rc, SHELL_STOP_COMMAND_SEQUENCE, and shell_stopped().

Referenced by script_exec().

◆ script_exec_line()

static int script_exec_line ( struct image image,
size_t  offset,
const char *label  __unused,
const char *  command 
)
static

Execute script line.

Parameters
imageScript
offsetOffset within script
labelLabel, or NULL
commandCommand
Return values
rcReturn status code

Definition at line 176 of file script.c.

178  {
179  int rc;
180 
181  DBGC ( image, "[%04zx] $ %s\n", offset, command );
182 
183  /* Execute command */
184  if ( ( rc = system ( command ) ) != 0 )
185  return rc;
186 
187  return 0;
188 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A command-line command.
Definition: command.h:9
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:24

References DBGC, offset, rc, and system.

Referenced by script_exec().

◆ script_exec()

static int script_exec ( struct image image)
static

Execute script.

Parameters
imageScript
Return values
rcReturn status code

Definition at line 196 of file script.c.

196  {
197  size_t saved_offset;
198  int rc;
199 
200  /* Temporarily de-register image, so that a "boot" command
201  * doesn't throw us into an execution loop.
202  */
204 
205  /* Preserve state of any currently-running script */
206  saved_offset = script_offset;
207 
208  /* Process script */
211 
212  /* Restore saved state */
213  script_offset = saved_offset;
214 
215  /* Re-register image (unless we have been replaced) */
216  if ( ! image->replacement )
217  register_image ( image );
218 
219  return rc;
220 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:24
int register_image(struct image *image)
Register executable image.
Definition: image.c:210
static int script_exec_line(struct image *image, size_t offset, const char *label __unused, const char *command)
Execute script line.
Definition: script.c:176
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:252
static int terminate_on_exit_or_failure(int rc)
Terminate script processing on shell exit or command failure.
Definition: script.c:161
struct image * replacement
Replacement image.
Definition: image.h:60
static size_t script_offset
Offset within current script.
Definition: script.c:51
static int process_script(struct image *image, int(*process_line)(struct image *image, size_t offset, const char *label, const char *command), int(*terminate)(int rc))
Process script lines.
Definition: script.c:61

References process_script(), rc, register_image(), image::replacement, script_exec_line(), script_offset, terminate_on_exit_or_failure(), and unregister_image().

◆ script_probe()

static int script_probe ( struct image image)
static

Probe script image.

Parameters
imageScript
Return values
rcReturn status code

Definition at line 228 of file script.c.

228  {
229  static const char ipxe_magic[] = "#!ipxe";
230  static const char gpxe_magic[] = "#!gpxe";
231  linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
232  magic_size_mismatch );
233  char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
234  + 1 /* terminating space */];
235 
236  /* Sanity check */
237  if ( image->len < sizeof ( test ) ) {
238  DBGC ( image, "Too short to be a script\n" );
239  return -ENOEXEC;
240  }
241 
242  /* Check for magic signature */
243  copy_from_user ( test, image->data, 0, sizeof ( test ) );
244  if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
245  ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
246  isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
247  DBGC ( image, "Invalid magic signature\n" );
248  return -ENOEXEC;
249  }
250 
251  return 0;
252 }
userptr_t data
Raw file image.
Definition: image.h:41
#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
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
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
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define linker_assert(condition, error_symbol)
Assert a condition at link-time.
Definition: assert.h:68
static int test
Definition: epic100.c:73

References copy_from_user(), image::data, DBGC, ENOEXEC, isspace(), image::len, linker_assert, memcmp(), and test.

◆ __image_type()

struct image_type script_image_type __image_type ( PROBE_NORMAL  )

Script image type.

◆ goto_find_label()

static int goto_find_label ( struct image image,
size_t  offset,
const char *  label,
const char *command  __unused 
)
static

Check for presence of label.

Parameters
imageScript
offsetOffset within script
labelLabel
commandCommand
Return values
rcReturn status code

Definition at line 287 of file script.c.

288  {
289 
290  /* Check label exists */
291  if ( ! label )
292  return -ENOENT;
293 
294  /* Check label matches */
295  if ( strcmp ( goto_label, label ) != 0 )
296  return -ENOENT;
297 
298  /* Update script offset */
300  DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
301 
302  return 0;
303 }
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
An executable image.
Definition: image.h:24
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
static size_t script_offset
Offset within current script.
Definition: script.c:51
static const char * goto_label
Current "goto" label.
Definition: script.c:276

References DBGC, ENOENT, goto_label, offset, script_offset, and strcmp().

Referenced by goto_exec().

◆ terminate_on_label_found()

static int terminate_on_label_found ( int  rc)
static

Terminate script processing when label is found.

Parameters
rcLine processing status
Return values
terminateTerminate script processing

Definition at line 311 of file script.c.

311  {
312  return ( rc == 0 );
313 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14

References rc.

Referenced by goto_exec().

◆ goto_exec()

static int goto_exec ( int  argc,
char **  argv 
)
static

"goto" command

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 322 of file script.c.

322  {
323  struct goto_options opts;
324  size_t saved_offset;
325  int rc;
326 
327  /* Parse options */
328  if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
329  return rc;
330 
331  /* Sanity check */
332  if ( ! current_image ) {
333  rc = -ENOTTY;
334  printf ( "Not in a script: %s\n", strerror ( rc ) );
335  return rc;
336  }
337 
338  /* Parse label */
339  goto_label = argv[optind];
340 
341  /* Find label */
342  saved_offset = script_offset;
344  terminate_on_label_found ) ) != 0 ) {
345  script_offset = saved_offset;
346  DBGC ( current_image, "[%04zx] No such label :%s\n",
348  return rc;
349  }
350 
351  /* Terminate processing of current command */
353 
354  return 0;
355 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct image * current_image
Currently-executing image.
Definition: image.c:59
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optind
Current option index.
Definition: getopt.c:51
#define DBGC(...)
Definition: compiler.h:505
"goto" options
Definition: script.c:262
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
Stop processing current command line.
Definition: shell.h:22
static int terminate_on_label_found(int rc)
Terminate script processing when label is found.
Definition: script.c:311
static union @423 opts
"cert<xxx>" option list
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
static size_t script_offset
Offset within current script.
Definition: script.c:51
static int goto_find_label(struct image *image, size_t offset, const char *label, const char *command __unused)
Check for presence of label.
Definition: script.c:287
static const char * goto_label
Current "goto" label.
Definition: script.c:276
static struct command_descriptor goto_cmd
"goto" command descriptor
Definition: script.c:268
static int process_script(struct image *image, int(*process_line)(struct image *image, size_t offset, const char *label, const char *command), int(*terminate)(int rc))
Process script lines.
Definition: script.c:61

References current_image, DBGC, ENOTTY, goto_cmd, goto_find_label(), goto_label, optind, opts, parse_options(), printf(), process_script(), rc, script_offset, shell_stop(), SHELL_STOP_COMMAND, strerror(), and terminate_on_label_found().

◆ prompt_exec()

static int prompt_exec ( int  argc,
char **  argv 
)
static

"prompt" command

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 391 of file script.c.

391  {
392  struct prompt_options opts;
393  char *text;
394  int rc;
395 
396  /* Parse options */
397  if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
398  goto err_parse;
399 
400  /* Parse prompt text */
401  text = concat_args ( &argv[optind] );
402  if ( ! text ) {
403  rc = -ENOMEM;
404  goto err_concat;
405  }
406 
407  /* Display prompt and wait for key */
408  if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
409  goto err_prompt;
410 
411  /* Free prompt text */
412  free ( text );
413 
414  return 0;
415 
416  err_prompt:
417  free ( text );
418  err_concat:
419  err_parse:
420  return rc;
421 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int optind
Current option index.
Definition: getopt.c:51
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
#define ENOMEM
Not enough space.
Definition: errno.h:534
"prompt" options
Definition: script.c:364
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
static union @423 opts
"cert<xxx>" option list
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition: prompt.c:48
static struct command_descriptor prompt_cmd
"prompt" command descriptor
Definition: script.c:380

References concat_args(), ENOMEM, free, optind, opts, parse_options(), prompt(), prompt_cmd, and rc.

Variable Documentation

◆ script_offset

size_t script_offset
static

Offset within current script.

This is a global in order to allow goto_exec() to update the offset.

Definition at line 51 of file script.c.

Referenced by goto_exec(), goto_find_label(), process_script(), and script_exec().

◆ goto_opts

struct option_descriptor goto_opts[] = {}
static

"goto" option list

Definition at line 265 of file script.c.

◆ goto_cmd

struct command_descriptor goto_cmd
static
Initial value:
=
COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" )
"goto" options
Definition: script.c:262
static struct option_descriptor goto_opts[]
"goto" option list
Definition: script.c:265
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107

"goto" command descriptor

Definition at line 268 of file script.c.

Referenced by goto_exec().

◆ goto_label

const char* goto_label
static

Current "goto" label.

Valid only during goto_exec(). Consider this part of a closure.

Definition at line 276 of file script.c.

Referenced by goto_exec(), and goto_find_label().

◆ __command

struct command prompt_command __command
Initial value:
= {
.name = "goto",
.exec = goto_exec,
}
static int goto_exec(int argc, char **argv)
"goto" command
Definition: script.c:322

"goto" command

"prompt" command

Definition at line 358 of file script.c.

◆ prompt_opts

struct option_descriptor prompt_opts[]
static
Initial value:
= {
OPTION_DESC ( "timeout", 't', required_argument,
}
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:215
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:111
"prompt" options
Definition: script.c:364
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:66
Option requires an argument.
Definition: getopt.h:18
void timeout(int)
union @375 key
Sense key.
Definition: scsi.h:18

"prompt" option list

Definition at line 372 of file script.c.

◆ prompt_cmd

struct command_descriptor prompt_cmd
static
Initial value:
=
"[<text>]" )
"prompt" options
Definition: script.c:364
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
static struct option_descriptor prompt_opts[]
"prompt" option list
Definition: script.c:372
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107

"prompt" command descriptor

Definition at line 380 of file script.c.

Referenced by prompt_exec().