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 
26 /**
27  * @file
28  *
29  * iPXE scripts
30  *
31  */
32 
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <getopt.h>
39 #include <ipxe/command.h>
40 #include <ipxe/parseopt.h>
41 #include <ipxe/image.h>
42 #include <ipxe/shell.h>
43 #include <usr/prompt.h>
44 #include <ipxe/script.h>
45 
46 /** Offset within current script
47  *
48  * This is a global in order to allow goto_exec() to update the
49  * offset.
50  */
51 static size_t script_offset;
52 
53 /**
54  * Process script lines
55  *
56  * @v image Script
57  * @v process_line Line processor
58  * @v terminate Termination check
59  * @ret rc Return status code
60  */
61 static int process_script ( struct image *image,
62  int ( * process_line ) ( struct image *image,
63  size_t offset,
64  const char *label,
65  const char *command ),
66  int ( * terminate ) ( int rc ) ) {
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 }
154 
155 /**
156  * Terminate script processing on shell exit or command failure
157  *
158  * @v rc Line processing status
159  * @ret terminate Terminate script processing
160  */
161 static int terminate_on_exit_or_failure ( int rc ) {
162 
164  ( rc != 0 ) );
165 }
166 
167 /**
168  * Execute script line
169  *
170  * @v image Script
171  * @v offset Offset within script
172  * @v label Label, or NULL
173  * @v command Command
174  * @ret rc Return status code
175  */
176 static int script_exec_line ( struct image *image, size_t offset,
177  const char *label __unused,
178  const char *command ) {
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 }
189 
190 /**
191  * Execute script
192  *
193  * @v image Script
194  * @ret rc Return status code
195  */
196 static int script_exec ( struct image *image ) {
197  size_t saved_offset;
198  int rc;
199 
200  /* Preserve state of any currently-running script */
201  saved_offset = script_offset;
202 
203  /* Process script */
206 
207  /* Restore saved state */
208  script_offset = saved_offset;
209 
210  return rc;
211 }
212 
213 /**
214  * Probe script image
215  *
216  * @v image Script
217  * @ret rc Return status code
218  */
219 static int script_probe ( struct image *image ) {
220  static const char ipxe_magic[] = "#!ipxe";
221  static const char gpxe_magic[] = "#!gpxe";
222  linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
223  magic_size_mismatch );
224  char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
225  + 1 /* terminating space */];
226 
227  /* Sanity check */
228  if ( image->len < sizeof ( test ) ) {
229  DBGC ( image, "Too short to be a script\n" );
230  return -ENOEXEC;
231  }
232 
233  /* Check for magic signature */
234  copy_from_user ( test, image->data, 0, sizeof ( test ) );
235  if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
236  ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
237  isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
238  DBGC ( image, "Invalid magic signature\n" );
239  return -ENOEXEC;
240  }
241 
242  return 0;
243 }
244 
245 /** Script image type */
246 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
247  .name = "script",
248  .probe = script_probe,
249  .exec = script_exec,
250 };
251 
252 /** "goto" options */
253 struct goto_options {};
254 
255 /** "goto" option list */
256 static struct option_descriptor goto_opts[] = {};
257 
258 /** "goto" command descriptor */
260  COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
261 
262 /**
263  * Current "goto" label
264  *
265  * Valid only during goto_exec(). Consider this part of a closure.
266  */
267 static const char *goto_label;
268 
269 /**
270  * Check for presence of label
271  *
272  * @v image Script
273  * @v offset Offset within script
274  * @v label Label
275  * @v command Command
276  * @ret rc Return status code
277  */
278 static int goto_find_label ( struct image *image, size_t offset,
279  const char *label, const char *command __unused ) {
280 
281  /* Check label exists */
282  if ( ! label )
283  return -ENOENT;
284 
285  /* Check label matches */
286  if ( strcmp ( goto_label, label ) != 0 )
287  return -ENOENT;
288 
289  /* Update script offset */
291  DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
292 
293  return 0;
294 }
295 
296 /**
297  * Terminate script processing when label is found
298  *
299  * @v rc Line processing status
300  * @ret terminate Terminate script processing
301  */
302 static int terminate_on_label_found ( int rc ) {
303  return ( rc == 0 );
304 }
305 
306 /**
307  * "goto" command
308  *
309  * @v argc Argument count
310  * @v argv Argument list
311  * @ret rc Return status code
312  */
313 static int goto_exec ( int argc, char **argv ) {
314  struct image *image = current_image.image;
315  struct goto_options opts;
316  size_t saved_offset;
317  int rc;
318 
319  /* Parse options */
320  if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
321  return rc;
322 
323  /* Sanity check */
324  if ( ! image ) {
325  rc = -ENOTTY;
326  printf ( "Not in a script: %s\n", strerror ( rc ) );
327  return rc;
328  }
329 
330  /* Parse label */
331  goto_label = argv[optind];
332 
333  /* Find label */
334  saved_offset = script_offset;
336  terminate_on_label_found ) ) != 0 ) {
337  script_offset = saved_offset;
338  DBGC ( image, "[%04zx] No such label :%s\n",
340  return rc;
341  }
342 
343  /* Terminate processing of current command */
345 
346  return 0;
347 }
348 
349 /** "goto" command */
350 struct command goto_command __command = {
351  .name = "goto",
352  .exec = goto_exec,
353 };
354 
355 /** "prompt" options */
357  /** Key to wait for */
358  unsigned int key;
359  /** Timeout */
360  unsigned long timeout;
361 };
362 
363 /** "prompt" option list */
364 static struct option_descriptor prompt_opts[] = {
365  OPTION_DESC ( "key", 'k', required_argument,
366  struct prompt_options, key, parse_key ),
367  OPTION_DESC ( "timeout", 't', required_argument,
369 };
370 
371 /** "prompt" command descriptor */
374  "[<text>]" );
375 
376 /**
377  * "prompt" command
378  *
379  * @v argc Argument count
380  * @v argv Argument list
381  * @ret rc Return status code
382  */
383 static int prompt_exec ( int argc, char **argv ) {
384  struct prompt_options opts;
385  char *text;
386  int rc;
387 
388  /* Parse options */
389  if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
390  goto err_parse;
391 
392  /* Parse prompt text */
393  text = concat_args ( &argv[optind] );
394  if ( ! text ) {
395  rc = -ENOMEM;
396  goto err_concat;
397  }
398 
399  /* Display prompt and wait for key */
400  if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
401  goto err_prompt;
402 
403  /* Free prompt text */
404  free ( text );
405 
406  return 0;
407 
408  err_prompt:
409  free ( text );
410  err_concat:
411  err_parse:
412  return rc;
413 }
414 
415 /** "prompt" command */
416 struct command prompt_command __command = {
417  .name = "prompt",
418  .exec = prompt_exec,
419 };
#define EINVAL
Invalid argument.
Definition: errno.h:428
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
userptr_t data
Raw file image.
Definition: image.h:41
int parse_key(char *text, unsigned int *key)
Parse key.
Definition: parseopt.c:217
Minimal command shell.
int optind
Current option index.
Definition: getopt.c:51
static int script_exec(struct image *image)
Execute script.
Definition: script.c:196
Error codes.
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
#define ENOEXEC
Exec format error.
Definition: errno.h:519
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:113
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
struct image_type script_image_type __image_type(PROBE_NORMAL)
Script image type.
#define DBGC(...)
Definition: compiler.h:505
"goto" options
Definition: script.c:253
An executable image type.
Definition: image.h:76
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define PROBE_NORMAL
Normal image probe priority.
Definition: image.h:137
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:460
Prompt for keypress.
An executable image.
Definition: image.h:24
Character types.
A command descriptor.
Definition: parseopt.h:76
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
static struct option_descriptor goto_opts[]
"goto" option list
Definition: script.c:256
struct image * image
Image (weak reference, nullified when image is freed)
Definition: image.h:158
iPXE scripts
char * name
Name of this image type.
Definition: image.h:78
unsigned long tmp
Definition: linux_pci.h:53
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define ENOMEM
Not enough space.
Definition: errno.h:534
Stop processing current command line.
Definition: shell.h:22
struct image_tag current_image
Parse command-line options.
unsigned long timeout
Timeout.
Definition: script.c:360
Executable images.
"prompt" options
Definition: script.c:356
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static int terminate_on_label_found(int rc)
Terminate script processing when label is found.
Definition: script.c:302
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t len
Length of raw file image.
Definition: image.h:43
Command line option parsing.
int isspace(int character)
Check to see if character is a space.
Definition: ctype.c:41
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
int prompt(const char *text, unsigned long timeout, int key)
Prompt for keypress.
Definition: prompt.c:48
static int goto_exec(int argc, char **argv)
"goto" command
Definition: script.c:313
static struct option_descriptor prompt_opts[]
"prompt" option list
Definition: script.c:364
static int command
Definition: epic100.c:68
static int script_probe(struct image *image)
Probe script image.
Definition: script.c:219
const char * name
Name of the command.
Definition: command.h:11
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:227
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
signed long off_t
Definition: stdint.h:8
static union @432 opts
"cert<xxx>" option list
uint32_t len
Length.
Definition: ena.h:14
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#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
Stop processing commands.
Definition: shell.h:29
A command-line option descriptor.
Definition: parseopt.h:22
static int terminate_on_exit_or_failure(int rc)
Terminate script processing on shell exit or command failure.
Definition: script.c:161
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
static int prompt_exec(int argc, char **argv)
"prompt" command
Definition: script.c:383
static size_t script_offset
Offset within current script.
Definition: script.c:51
void timeout(int)
struct command goto_command __command
"goto" command
Definition: script.c:350
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:278
static const char * goto_label
Current "goto" label.
Definition: script.c:267
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define linker_assert(condition, error_symbol)
Assert a condition at link-time.
Definition: assert.h:68
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
union @382 key
Sense key.
Definition: scsi.h:18
static int test
Definition: epic100.c:73
unsigned int key
Key to wait for.
Definition: script.c:358
static struct command_descriptor prompt_cmd
"prompt" command descriptor
Definition: script.c:372
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:24
static struct command_descriptor goto_cmd
"goto" command descriptor
Definition: script.c:259
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