iPXE
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)
 FILE_SECBOOT (PERMITTED)
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.
static int terminate_on_exit_or_failure (int rc)
 Terminate script processing on shell exit or command failure.
static int script_exec_line (struct image *image, size_t offset, const char *label __unused, const char *command)
 Execute script line.
static int script_exec (struct image *image)
 Execute script.
static int script_probe (struct image *image)
 Probe script image.
struct image_type script_image_type __image_type (PROBE_NORMAL)
 Script image type.
static int goto_find_label (struct image *image, size_t offset, const char *label, const char *command __unused)
 Check for presence of label.
static int terminate_on_label_found (int rc)
 Terminate script processing when label is found.
static int goto_exec (int argc, char **argv)
 "goto" command
 COMMAND (goto, goto_exec)
 "goto" command
static int prompt_exec (int argc, char **argv)
 "prompt" command
 COMMAND (prompt, prompt_exec)
 "prompt" command

Variables

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

Detailed Description

iPXE scripts

Definition in file script.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ process_script()

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) )
static

Process script lines.

Parameters
imageScript
process_lineLine processor
terminateTermination check
Return values
rcReturn status code

Definition at line 62 of file script.c.

67 {
68 size_t len = 0;
69 char *line = NULL;
70 size_t line_offset;
71 char *label;
72 char *command;
73 const void *eol;
74 size_t frag_len;
75 char *tmp;
76 int rc;
77
78 /* Initialise script and line offsets */
79 script_offset = 0;
80 line_offset = 0;
81
82 do {
83
84 /* Find length of next line, excluding any terminating '\n' */
85 eol = memchr ( ( image->data + script_offset ), '\n',
86 ( image->len - script_offset ) );
87 if ( eol ) {
88 frag_len = ( ( eol - image->data ) - script_offset );
89 } else {
90 frag_len = ( image->len - script_offset );
91 }
92
93 /* Allocate buffer for line */
94 tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
95 if ( ! tmp ) {
96 rc = -ENOMEM;
97 goto err_alloc;
98 }
99 line = tmp;
100
101 /* Copy line */
102 memcpy ( ( line + len ), ( image->data + script_offset ),
103 frag_len );
104 len += frag_len;
105
106 /* Move to next line in script */
107 script_offset += ( frag_len + 1 );
108
109 /* Strip trailing CR, if present */
110 if ( len && ( line[ len - 1 ] == '\r' ) )
111 len--;
112
113 /* Handle backslash continuations */
114 if ( len && ( line[ len - 1 ] == '\\' ) ) {
115 len--;
116 rc = -EINVAL;
117 continue;
118 }
119
120 /* Terminate line */
121 line[len] = '\0';
122
123 /* Split line into (optional) label and command */
124 command = line;
125 while ( isspace ( *command ) )
126 command++;
127 if ( *command == ':' ) {
128 label = ++command;
129 while ( *command && ! isspace ( *command ) )
130 command++;
131 if ( *command )
132 *(command++) = '\0';
133 } else {
134 label = NULL;
135 }
136
137 /* Process line */
138 rc = process_line ( image, line_offset, label, command );
139 if ( terminate ( rc ) )
140 goto err_process;
141
142 /* Free line */
143 free ( line );
144 line = NULL;
145 len = 0;
146
147 /* Update line offset */
148 line_offset = script_offset;
149
150 } while ( script_offset < image->len );
151
152 err_process:
153 err_alloc:
154 free ( line );
155 return rc;
156}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int isspace(int character)
Check to see if character is a space.
Definition ctype.c:42
ring len
Length.
Definition dwmac.h:226
#define EINVAL
Invalid argument.
Definition errno.h:429
#define ENOMEM
Not enough space.
Definition errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
unsigned long tmp
Definition linux_pci.h:65
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:607
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
static size_t script_offset
Offset within current script.
Definition script.c:52
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition string.c:136
A command-line command.
Definition command.h:10
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
size_t len
Length of raw file image.
Definition image.h:56
A text label widget.
Definition label.h:16

References image::data, EINVAL, ENOMEM, free, isspace(), image::len, len, memchr(), memcpy(), NULL, offset, rc, realloc(), script_offset, and tmp.

Referenced by goto_exec(), and script_exec().

◆ terminate_on_exit_or_failure()

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

164 {
165
167 ( rc != 0 ) );
168}
int shell_stopped(int stop)
Test and consume shell stop state.
Definition exec.c:228
@ SHELL_STOP_COMMAND_SEQUENCE
Stop processing commands.
Definition shell.h:30

References rc, SHELL_STOP_COMMAND_SEQUENCE, and shell_stopped().

Referenced by script_exec().

◆ script_exec_line()

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

181 {
182 int rc;
183
184 DBGC ( image, "[%04zx] $ %s\n", offset, command );
185
186 /* Execute command */
187 if ( ( rc = system ( command ) ) != 0 )
188 return rc;
189
190 return 0;
191}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
uint8_t system[ETH_ALEN]
System identifier.
Definition eth_slow.h:13
#define DBGC(...)
Definition compiler.h:505

References __unused, DBGC, offset, rc, and system.

Referenced by script_exec().

◆ script_exec()

int script_exec ( struct image * image)
static

Execute script.

Parameters
imageScript
Return values
rcReturn status code

Definition at line 199 of file script.c.

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

References process_script(), rc, script_exec_line(), script_offset, and terminate_on_exit_or_failure().

Referenced by __image_type().

◆ script_probe()

int script_probe ( struct image * image)
static

Probe script image.

Parameters
imageScript
Return values
rcReturn status code

Definition at line 222 of file script.c.

222 {
223 static const char ipxe_magic[] = "#!ipxe";
224 static const char gpxe_magic[] = "#!gpxe";
225 static_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ) );
226 const struct {
227 char magic[ sizeof ( ipxe_magic ) - 1 /* NUL */ ];
228 char space;
229 } __attribute__ (( packed )) *test;
230
231 /* Sanity check */
232 if ( image->len < sizeof ( *test ) ) {
233 DBGC ( image, "Too short to be a script\n" );
234 return -ENOEXEC;
235 }
236
237 /* Check for magic signature */
238 test = image->data;
239 if ( ! ( ( ( memcmp ( test->magic, ipxe_magic,
240 sizeof ( test->magic ) ) == 0 ) ||
241 ( memcmp ( test->magic, gpxe_magic,
242 sizeof ( test->magic ) ) == 0 ) ) &&
243 isspace ( test->space ) ) ) {
244 DBGC ( image, "Invalid magic signature\n" );
245 return -ENOEXEC;
246 }
247
248 return 0;
249}
uint16_t magic
Magic signature.
Definition bzimage.h:1
static int test
Definition epic100.c:73
#define ENOEXEC
Exec format error.
Definition errno.h:520
#define __attribute__(x)
Definition compiler.h:10
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115

References __attribute__, image::data, DBGC, ENOEXEC, isspace(), image::len, magic, memcmp(), and test.

Referenced by __image_type().

◆ __image_type()

struct image_type script_image_type __image_type ( PROBE_NORMAL )

Script image type.

References __image_type, PROBE_NORMAL, script_exec(), and script_probe().

◆ goto_find_label()

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

285 {
286
287 /* Check label exists */
288 if ( ! label )
289 return -ENOENT;
290
291 /* Check label matches */
292 if ( strcmp ( goto_label, label ) != 0 )
293 return -ENOENT;
294
295 /* Update script offset */
297 DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
298
299 return 0;
300}
#define ENOENT
No such file or directory.
Definition errno.h:515
static const char * goto_label
Current "goto" label.
Definition script.c:273
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174

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

Referenced by goto_exec().

◆ terminate_on_label_found()

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

308 {
309 return ( rc == 0 );
310}

References rc.

Referenced by goto_exec().

◆ goto_exec()

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

"goto" command

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 319 of file script.c.

319 {
320 struct image *image = current_image.image;
321 struct goto_options opts;
322 size_t saved_offset;
323 int rc;
324
325 /* Parse options */
326 if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
327 return rc;
328
329 /* Sanity check */
330 if ( ! image ) {
331 rc = -ENOTTY;
332 printf ( "Not in a script: %s\n", strerror ( rc ) );
333 return rc;
334 }
335
336 /* Parse label */
337 goto_label = argv[optind];
338
339 /* Find label */
340 saved_offset = script_offset;
342 terminate_on_label_found ) ) != 0 ) {
343 script_offset = saved_offset;
344 DBGC ( image, "[%04zx] No such label :%s\n",
346 return rc;
347 }
348
349 /* Terminate processing of current command */
351
352 return 0;
353}
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
void shell_stop(int stop)
Set shell stop state.
Definition exec.c:218
int optind
Current option index.
Definition getopt.c:52
#define ENOTTY
Inappropriate I/O control operation.
Definition errno.h:595
struct image_tag current_image
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
static int terminate_on_label_found(int rc)
Terminate script processing when label is found.
Definition script.c:308
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:284
static struct command_descriptor goto_cmd
"goto" command descriptor
Definition script.c:265
@ SHELL_STOP_COMMAND
Stop processing current command line.
Definition shell.h:23
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
"goto" options
Definition script.c:259
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465

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().

Referenced by COMMAND().

◆ COMMAND() [1/2]

COMMAND ( goto ,
goto_exec  )

"goto" command

References goto_exec().

◆ prompt_exec()

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

"prompt" command

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 386 of file script.c.

386 {
387 struct prompt_options opts;
388 char *text;
389 int rc;
390
391 /* Parse options */
392 if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
393 goto err_parse;
394
395 /* Parse prompt text */
396 text = concat_args ( &argv[optind] );
397 if ( ! text ) {
398 rc = -ENOMEM;
399 goto err_concat;
400 }
401
402 /* Display prompt and wait for key */
403 if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
404 goto err_prompt;
405
406 /* Free prompt text */
407 free ( text );
408
409 return 0;
410
411 err_prompt:
412 free ( text );
413 err_concat:
414 err_parse:
415 return rc;
416}
char * concat_args(char **args)
Concatenate arguments.
Definition exec.c:359
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition prompt.c:49
static struct command_descriptor prompt_cmd
"prompt" command descriptor
Definition script.c:375
"prompt" options
Definition script.c:359

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

Referenced by COMMAND().

◆ COMMAND() [2/2]

COMMAND ( prompt ,
prompt_exec  )

"prompt" command

References prompt(), and prompt_exec().

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

262{};

◆ goto_cmd

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

"goto" command descriptor

Definition at line 265 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 273 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,
}
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
void timeout(int)
@ required_argument
Option requires an argument.
Definition getopt.h:19
int parse_key(char *text, unsigned int *key)
Parse key.
Definition parseopt.c:242
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition parseopt.c:115
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68

"prompt" option list

Definition at line 367 of file script.c.

367 {
368 OPTION_DESC ( "key", 'k', required_argument,
369 struct prompt_options, key, parse_key ),
370 OPTION_DESC ( "timeout", 't', required_argument,
372};

◆ prompt_cmd

struct command_descriptor prompt_cmd
static
Initial value:
=
"[<text>]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition parseopt.h:98
static struct option_descriptor prompt_opts[]
"prompt" option list
Definition script.c:367

"prompt" command descriptor

Definition at line 375 of file script.c.

Referenced by prompt_exec().