iPXE
script.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 /**
28  * @file
29  *
30  * iPXE scripts
31  *
32  */
33 
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <getopt.h>
40 #include <ipxe/command.h>
41 #include <ipxe/parseopt.h>
42 #include <ipxe/image.h>
43 #include <ipxe/shell.h>
44 #include <usr/prompt.h>
45 #include <ipxe/script.h>
46 
47 /** Offset within current script
48  *
49  * This is a global in order to allow goto_exec() to update the
50  * offset.
51  */
52 static size_t script_offset;
53 
54 /**
55  * Process script lines
56  *
57  * @v image Script
58  * @v process_line Line processor
59  * @v terminate Termination check
60  * @ret rc Return status code
61  */
62 static int process_script ( struct image *image,
63  int ( * process_line ) ( struct image *image,
64  size_t offset,
65  const char *label,
66  const char *command ),
67  int ( * terminate ) ( int rc ) ) {
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 }
157 
158 /**
159  * Terminate script processing on shell exit or command failure
160  *
161  * @v rc Line processing status
162  * @ret terminate Terminate script processing
163  */
164 static int terminate_on_exit_or_failure ( int rc ) {
165 
167  ( rc != 0 ) );
168 }
169 
170 /**
171  * Execute script line
172  *
173  * @v image Script
174  * @v offset Offset within script
175  * @v label Label, or NULL
176  * @v command Command
177  * @ret rc Return status code
178  */
179 static int script_exec_line ( struct image *image, size_t offset,
180  const char *label __unused,
181  const char *command ) {
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 }
192 
193 /**
194  * Execute script
195  *
196  * @v image Script
197  * @ret rc Return status code
198  */
199 static int script_exec ( struct image *image ) {
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 }
215 
216 /**
217  * Probe script image
218  *
219  * @v image Script
220  * @ret rc Return status code
221  */
222 static int script_probe ( struct image *image ) {
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 }
250 
251 /** Script image type */
252 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
253  .name = "script",
254  .probe = script_probe,
255  .exec = script_exec,
256 };
257 
258 /** "goto" options */
259 struct goto_options {};
260 
261 /** "goto" option list */
262 static struct option_descriptor goto_opts[] = {};
263 
264 /** "goto" command descriptor */
266  COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
267 
268 /**
269  * Current "goto" label
270  *
271  * Valid only during goto_exec(). Consider this part of a closure.
272  */
273 static const char *goto_label;
274 
275 /**
276  * Check for presence of label
277  *
278  * @v image Script
279  * @v offset Offset within script
280  * @v label Label
281  * @v command Command
282  * @ret rc Return status code
283  */
284 static int goto_find_label ( struct image *image, size_t offset,
285  const char *label, const char *command __unused ) {
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 }
301 
302 /**
303  * Terminate script processing when label is found
304  *
305  * @v rc Line processing status
306  * @ret terminate Terminate script processing
307  */
308 static int terminate_on_label_found ( int rc ) {
309  return ( rc == 0 );
310 }
311 
312 /**
313  * "goto" command
314  *
315  * @v argc Argument count
316  * @v argv Argument list
317  * @ret rc Return status code
318  */
319 static int goto_exec ( int argc, char **argv ) {
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 }
354 
355 /** "goto" command */
356 COMMAND ( goto, goto_exec );
357 
358 /** "prompt" options */
360  /** Key to wait for */
361  unsigned int key;
362  /** Timeout */
363  unsigned long timeout;
364 };
365 
366 /** "prompt" option list */
367 static struct option_descriptor prompt_opts[] = {
368  OPTION_DESC ( "key", 'k', required_argument,
369  struct prompt_options, key, parse_key ),
370  OPTION_DESC ( "timeout", 't', required_argument,
372 };
373 
374 /** "prompt" command descriptor */
377  "[<text>]" );
378 
379 /**
380  * "prompt" command
381  *
382  * @v argc Argument count
383  * @v argv Argument list
384  * @ret rc Return status code
385  */
386 static int prompt_exec ( int argc, char **argv ) {
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 }
417 
418 /** "prompt" command */
#define __attribute__(x)
Definition: compiler.h:10
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A text label widget.
Definition: label.h:16
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:242
Minimal command shell.
int optind
Current option index.
Definition: getopt.c:52
static int script_exec(struct image *image)
Execute script.
Definition: script.c:199
Error codes.
const void * data
Read-only data.
Definition: image.h:51
uint16_t magic
Magic signature.
Definition: bzimage.h:6
A command-line command.
Definition: command.h:10
#define ENOEXEC
Exec format error.
Definition: errno.h:520
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:115
struct image_type script_image_type __image_type(PROBE_NORMAL)
Script image type.
#define DBGC(...)
Definition: compiler.h:505
"goto" options
Definition: script.c:259
An executable image type.
Definition: image.h:95
#define ENOENT
No such file or directory.
Definition: errno.h:515
#define PROBE_NORMAL
Normal image probe priority.
Definition: image.h:156
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:485
Prompt for keypress.
COMMAND(goto, goto_exec)
"goto" command
An executable image.
Definition: image.h:24
Character types.
#define static_assert(x)
Assert a condition at build time.
Definition: assert.h:66
A command descriptor.
Definition: parseopt.h:78
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:218
static struct option_descriptor goto_opts[]
"goto" option list
Definition: script.c:262
struct image * image
Image (weak reference, nullified when image is freed)
Definition: image.h:177
iPXE scripts
char * name
Name of this image type.
Definition: image.h:97
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:136
unsigned long tmp
Definition: linux_pci.h:65
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define ENOMEM
Not enough space.
Definition: errno.h:535
Stop processing current command line.
Definition: shell.h:23
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct image_tag current_image
Parse command-line options.
unsigned long timeout
Timeout.
Definition: script.c:363
Executable images.
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
ring len
Length.
Definition: dwmac.h:231
"prompt" options
Definition: script.c:359
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:98
static int terminate_on_label_found(int rc)
Terminate script processing when label is found.
Definition: script.c:308
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:359
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
size_t len
Length of raw file image.
Definition: image.h:56
Command line option parsing.
FILE_SECBOOT(PERMITTED)
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:42
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
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition: prompt.c:49
static int goto_exec(int argc, char **argv)
"goto" command
Definition: script.c:319
static struct option_descriptor prompt_opts[]
"prompt" option list
Definition: script.c:367
static int command
Definition: epic100.c:68
static int script_probe(struct image *image)
Probe script image.
Definition: script.c:222
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:228
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:595
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:174
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
Option requires an argument.
Definition: getopt.h:19
Stop processing commands.
Definition: shell.h:30
A command-line option descriptor.
Definition: parseopt.h:24
static int terminate_on_exit_or_failure(int rc)
Terminate script processing on shell exit or command failure.
Definition: script.c:164
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:607
static int prompt_exec(int argc, char **argv)
"prompt" command
Definition: script.c:386
static size_t script_offset
Offset within current script.
Definition: script.c:52
void timeout(int)
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
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static const char * goto_label
Current "goto" label.
Definition: script.c:273
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:115
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
union @391 key
Sense key.
Definition: scsi.h:18
static int test
Definition: epic100.c:73
unsigned int key
Key to wait for.
Definition: script.c:361
static struct command_descriptor prompt_cmd
"prompt" command descriptor
Definition: script.c:375
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:25
static struct command_descriptor goto_cmd
"goto" command descriptor
Definition: script.c:265
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