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...
 
 COMMAND (goto, goto_exec)
 "goto" command More...
 
static int prompt_exec (int argc, char **argv)
 "prompt" command More...
 
 COMMAND (prompt, prompt_exec)
 "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...
 
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  const void *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 ( ( image->data + script_offset ), '\n',
85  ( image->len - script_offset ) );
86  if ( eol ) {
87  frag_len = ( ( eol - image->data ) - script_offset );
88  } else {
89  frag_len = ( image->len - script_offset );
90  }
91 
92  /* Allocate buffer for line */
93  tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
94  if ( ! tmp ) {
95  rc = -ENOMEM;
96  goto err_alloc;
97  }
98  line = tmp;
99 
100  /* Copy line */
101  memcpy ( ( line + len ), ( image->data + script_offset ),
102  frag_len );
103  len += frag_len;
104 
105  /* Move to next line in script */
106  script_offset += ( frag_len + 1 );
107 
108  /* Strip trailing CR, if present */
109  if ( len && ( line[ len - 1 ] == '\r' ) )
110  len--;
111 
112  /* Handle backslash continuations */
113  if ( len && ( line[ len - 1 ] == '\\' ) ) {
114  len--;
115  rc = -EINVAL;
116  continue;
117  }
118 
119  /* Terminate line */
120  line[len] = '\0';
121 
122  /* Split line into (optional) label and command */
123  command = line;
124  while ( isspace ( *command ) )
125  command++;
126  if ( *command == ':' ) {
127  label = ++command;
128  while ( *command && ! isspace ( *command ) )
129  command++;
130  if ( *command )
131  *(command++) = '\0';
132  } else {
133  label = NULL;
134  }
135 
136  /* Process line */
137  rc = process_line ( image, line_offset, label, command );
138  if ( terminate ( rc ) )
139  goto err_process;
140 
141  /* Free line */
142  free ( line );
143  line = NULL;
144  len = 0;
145 
146  /* Update line offset */
147  line_offset = script_offset;
148 
149  } while ( script_offset < image->len );
150 
151  err_process:
152  err_alloc:
153  free ( line );
154  return rc;
155 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A text label widget.
Definition: label.h:16
const void * data
Read-only data.
Definition: image.h:50
A command-line command.
Definition: command.h:9
An executable image.
Definition: image.h:23
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:135
unsigned long tmp
Definition: linux_pci.h:64
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t len
Length of raw file image.
Definition: image.h:55
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
static int command
Definition: epic100.c:68
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:606
static size_t script_offset
Offset within current script.
Definition: script.c:51
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References command, image::data, EINVAL, ENOMEM, free, isspace(), image::len, len, memchr(), memcpy(), 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 163 of file script.c.

163  {
164 
166  ( rc != 0 ) );
167 }
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 178 of file script.c.

180  {
181  int rc;
182 
183  DBGC ( image, "[%04zx] $ %s\n", offset, command );
184 
185  /* Execute command */
186  if ( ( rc = system ( command ) ) != 0 )
187  return rc;
188 
189  return 0;
190 }
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:23
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
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 198 of file script.c.

198  {
199  size_t saved_offset;
200  int rc;
201 
202  /* Preserve state of any currently-running script */
203  saved_offset = script_offset;
204 
205  /* Process script */
208 
209  /* Restore saved state */
210  script_offset = saved_offset;
211 
212  return rc;
213 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:23
static int script_exec_line(struct image *image, size_t offset, const char *label __unused, const char *command)
Execute script line.
Definition: script.c:178
static int terminate_on_exit_or_failure(int rc)
Terminate script processing on shell exit or command failure.
Definition: script.c:163
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, script_exec_line(), script_offset, and terminate_on_exit_or_failure().

◆ script_probe()

static int script_probe ( struct image image)
static

Probe script image.

Parameters
imageScript
Return values
rcReturn status code

Definition at line 221 of file script.c.

221  {
222  static const char ipxe_magic[] = "#!ipxe";
223  static const char gpxe_magic[] = "#!gpxe";
224  static_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ) );
225  const struct {
226  char magic[ sizeof ( ipxe_magic ) - 1 /* NUL */ ];
227  char space;
228  } __attribute__ (( packed )) *test;
229 
230  /* Sanity check */
231  if ( image->len < sizeof ( *test ) ) {
232  DBGC ( image, "Too short to be a script\n" );
233  return -ENOEXEC;
234  }
235 
236  /* Check for magic signature */
237  test = image->data;
238  if ( ! ( ( ( memcmp ( test->magic, ipxe_magic,
239  sizeof ( test->magic ) ) == 0 ) ||
240  ( memcmp ( test->magic, gpxe_magic,
241  sizeof ( test->magic ) ) == 0 ) ) &&
242  isspace ( test->space ) ) ) {
243  DBGC ( image, "Invalid magic signature\n" );
244  return -ENOEXEC;
245  }
246 
247  return 0;
248 }
#define __attribute__(x)
Definition: compiler.h:10
char space
Whitespace.
Definition: pnm.h:22
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
#define static_assert(x)
Assert a condition at build time.
Definition: assert.h:65
size_t len
Length of raw file image.
Definition: image.h:55
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
char magic
Magic byte ('P')
Definition: pnm.h:18
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
static int test
Definition: epic100.c:73

References __attribute__, image::data, DBGC, ENOEXEC, isspace(), image::len, magic, memcmp(), static_assert, 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 283 of file script.c.

284  {
285 
286  /* Check label exists */
287  if ( ! label )
288  return -ENOENT;
289 
290  /* Check label matches */
291  if ( strcmp ( goto_label, label ) != 0 )
292  return -ENOENT;
293 
294  /* Update script offset */
296  DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
297 
298  return 0;
299 }
A text label widget.
Definition: label.h:16
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
An executable image.
Definition: image.h:23
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
static size_t script_offset
Offset within current script.
Definition: script.c:51
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static const char * goto_label
Current "goto" label.
Definition: script.c:272

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 307 of file script.c.

307  {
308  return ( rc == 0 );
309 }
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 318 of file script.c.

318  {
319  struct image *image = current_image.image;
320  struct goto_options opts;
321  size_t saved_offset;
322  int rc;
323 
324  /* Parse options */
325  if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
326  return rc;
327 
328  /* Sanity check */
329  if ( ! image ) {
330  rc = -ENOTTY;
331  printf ( "Not in a script: %s\n", strerror ( rc ) );
332  return rc;
333  }
334 
335  /* Parse label */
336  goto_label = argv[optind];
337 
338  /* Find label */
339  saved_offset = script_offset;
341  terminate_on_label_found ) ) != 0 ) {
342  script_offset = saved_offset;
343  DBGC ( image, "[%04zx] No such label :%s\n",
345  return rc;
346  }
347 
348  /* Terminate processing of current command */
350 
351  return 0;
352 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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:258
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:23
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
struct image * image
Image (weak reference, nullified when image is freed)
Definition: image.h:176
Stop processing current command line.
Definition: shell.h:22
struct image_tag current_image
static int terminate_on_label_found(int rc)
Terminate script processing when label is found.
Definition: script.c:307
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 union @447 opts
"cert<xxx>" option list
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:283
static const char * goto_label
Current "goto" label.
Definition: script.c:272
static struct command_descriptor goto_cmd
"goto" command descriptor
Definition: script.c:264
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, image_tag::image, optind, opts, parse_options(), printf(), process_script(), rc, script_offset, shell_stop(), SHELL_STOP_COMMAND, strerror(), and terminate_on_label_found().

◆ COMMAND() [1/2]

COMMAND ( goto  ,
goto_exec   
)

"goto" command

◆ 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 385 of file script.c.

385  {
386  struct prompt_options opts;
387  char *text;
388  int rc;
389 
390  /* Parse options */
391  if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
392  goto err_parse;
393 
394  /* Parse prompt text */
395  text = concat_args ( &argv[optind] );
396  if ( ! text ) {
397  rc = -ENOMEM;
398  goto err_concat;
399  }
400 
401  /* Display prompt and wait for key */
402  if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
403  goto err_prompt;
404 
405  /* Free prompt text */
406  free ( text );
407 
408  return 0;
409 
410  err_prompt:
411  free ( text );
412  err_concat:
413  err_parse:
414  return rc;
415 }
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:484
#define ENOMEM
Not enough space.
Definition: errno.h:534
"prompt" options
Definition: script.c:358
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
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 union @447 opts
"cert<xxx>" option list
static struct command_descriptor prompt_cmd
"prompt" command descriptor
Definition: script.c:374

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

◆ COMMAND() [2/2]

COMMAND ( prompt  ,
prompt_exec   
)

"prompt" command

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 261 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:258
static struct option_descriptor goto_opts[]
"goto" option list
Definition: script.c:261
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

"goto" command descriptor

Definition at line 264 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 272 of file script.c.

Referenced by goto_exec(), and goto_find_label().

◆ 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:241
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
"prompt" options
Definition: script.c:358
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
void timeout(int)
union @391 key
Sense key.
Definition: scsi.h:17

"prompt" option list

Definition at line 366 of file script.c.

◆ prompt_cmd

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

"prompt" command descriptor

Definition at line 374 of file script.c.

Referenced by prompt_exec().