Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / image / script.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /**
23  * @file
24  *
25  * iPXE scripts
26  *
27  */
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <ipxe/command.h>
36 #include <ipxe/parseopt.h>
37 #include <ipxe/image.h>
38 #include <ipxe/shell.h>
39 #include <usr/prompt.h>
40 #include <ipxe/script.h>
41
42 /** Offset within current script
43  *
44  * This is a global in order to allow goto_exec() to update the
45  * offset.
46  */
47 static size_t script_offset;
48
49 /**
50  * Process script lines
51  *
52  * @v image             Script
53  * @v process_line      Line processor
54  * @v terminate         Termination check
55  * @ret rc              Return status code
56  */
57 static int process_script ( struct image *image,
58                             int ( * process_line ) ( struct image *image,
59                                                      size_t offset,
60                                                      const char *label,
61                                                      const char *command ),
62                             int ( * terminate ) ( int rc ) ) {
63         size_t len = 0;
64         char *line = NULL;
65         size_t line_offset;
66         char *label;
67         char *command;
68         off_t eol;
69         size_t frag_len;
70         char *tmp;
71         int rc;
72
73         /* Initialise script and line offsets */
74         script_offset = 0;
75         line_offset = 0;
76
77         do {
78
79                 /* Find length of next line, excluding any terminating '\n' */
80                 eol = memchr_user ( image->data, script_offset, '\n',
81                                     ( image->len - script_offset ) );
82                 if ( eol < 0 )
83                         eol = image->len;
84                 frag_len = ( eol - script_offset );
85
86                 /* Allocate buffer for line */
87                 tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
88                 if ( ! tmp ) {
89                         rc = -ENOMEM;
90                         goto err_alloc;
91                 }
92                 line = tmp;
93
94                 /* Copy line */
95                 copy_from_user ( ( line + len ), image->data, script_offset,
96                                  frag_len );
97                 len += frag_len;
98
99                 /* Move to next line in script */
100                 script_offset += ( frag_len + 1 );
101
102                 /* Strip trailing CR, if present */
103                 if ( len && ( line[ len - 1 ] == '\r' ) )
104                         len--;
105
106                 /* Handle backslash continuations */
107                 if ( len && ( line[ len - 1 ] == '\\' ) ) {
108                         len--;
109                         rc = -EINVAL;
110                         continue;
111                 }
112
113                 /* Terminate line */
114                 line[len] = '\0';
115
116                 /* Split line into (optional) label and command */
117                 command = line;
118                 while ( isspace ( *command ) )
119                         command++;
120                 if ( *command == ':' ) {
121                         label = ++command;
122                         while ( *command && ! isspace ( *command ) )
123                                 command++;
124                         if ( *command )
125                                 *(command++) = '\0';
126                 } else {
127                         label = NULL;
128                 }
129
130                 /* Process line */
131                 rc = process_line ( image, line_offset, label, command );
132                 if ( terminate ( rc ) )
133                         goto err_process;
134
135                 /* Free line */
136                 free ( line );
137                 line = NULL;
138                 len = 0;
139
140                 /* Update line offset */
141                 line_offset = script_offset;
142
143         } while ( script_offset < image->len );
144
145  err_process:
146  err_alloc:
147         free ( line );
148         return rc;
149 }
150
151 /**
152  * Terminate script processing on shell exit or command failure
153  *
154  * @v rc                Line processing status
155  * @ret terminate       Terminate script processing
156  */
157 static int terminate_on_exit_or_failure ( int rc ) {
158
159         return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
160                  ( rc != 0 ) );
161 }
162
163 /**
164  * Execute script line
165  *
166  * @v image             Script
167  * @v offset            Offset within script
168  * @v label             Label, or NULL
169  * @v command           Command
170  * @ret rc              Return status code
171  */
172 static int script_exec_line ( struct image *image, size_t offset,
173                               const char *label __unused,
174                               const char *command ) {
175         int rc;
176
177         DBGC ( image, "[%04zx] $ %s\n", offset, command );
178
179         /* Execute command */
180         if ( ( rc = system ( command ) ) != 0 )
181                 return rc;
182
183         return 0;
184 }
185
186 /**
187  * Execute script
188  *
189  * @v image             Script
190  * @ret rc              Return status code
191  */
192 static int script_exec ( struct image *image ) {
193         size_t saved_offset;
194         int rc;
195
196         /* Temporarily de-register image, so that a "boot" command
197          * doesn't throw us into an execution loop.
198          */
199         unregister_image ( image );
200
201         /* Preserve state of any currently-running script */
202         saved_offset = script_offset;
203
204         /* Process script */
205         rc = process_script ( image, script_exec_line,
206                               terminate_on_exit_or_failure );
207
208         /* Restore saved state */
209         script_offset = saved_offset;
210
211         /* Re-register image (unless we have been replaced) */
212         if ( ! image->replacement )
213                 register_image ( image );
214
215         return rc;
216 }
217
218 /**
219  * Probe script image
220  *
221  * @v image             Script
222  * @ret rc              Return status code
223  */
224 static int script_probe ( struct image *image ) {
225         static const char ipxe_magic[] = "#!ipxe";
226         static const char gpxe_magic[] = "#!gpxe";
227         linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
228                         magic_size_mismatch );
229         char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
230                    + 1 /* terminating space */];
231
232         /* Sanity check */
233         if ( image->len < sizeof ( test ) ) {
234                 DBGC ( image, "Too short to be a script\n" );
235                 return -ENOEXEC;
236         }
237
238         /* Check for magic signature */
239         copy_from_user ( test, image->data, 0, sizeof ( test ) );
240         if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
241                    ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
242                  isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
243                 DBGC ( image, "Invalid magic signature\n" );
244                 return -ENOEXEC;
245         }
246
247         return 0;
248 }
249
250 /** Script image type */
251 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
252         .name = "script",
253         .probe = script_probe,
254         .exec = script_exec,
255 };
256
257 /** "goto" options */
258 struct goto_options {};
259
260 /** "goto" option list */
261 static struct option_descriptor goto_opts[] = {};
262
263 /** "goto" command descriptor */
264 static struct command_descriptor goto_cmd =
265         COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
266
267 /**
268  * Current "goto" label
269  *
270  * Valid only during goto_exec().  Consider this part of a closure.
271  */
272 static const char *goto_label;
273
274 /**
275  * Check for presence of label
276  *
277  * @v image             Script
278  * @v offset            Offset within script
279  * @v label             Label
280  * @v command           Command
281  * @ret rc              Return status code
282  */
283 static int goto_find_label ( struct image *image, size_t offset,
284                              const char *label, const char *command __unused ) {
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 */
295         script_offset = offset;
296         DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
297
298         return 0;
299 }
300
301 /**
302  * Terminate script processing when label is found
303  *
304  * @v rc                Line processing status
305  * @ret terminate       Terminate script processing
306  */
307 static int terminate_on_label_found ( int rc ) {
308         return ( rc == 0 );
309 }
310
311 /**
312  * "goto" command
313  *
314  * @v argc              Argument count
315  * @v argv              Argument list
316  * @ret rc              Return status code
317  */
318 static int goto_exec ( int argc, char **argv ) {
319         struct goto_options opts;
320         size_t saved_offset;
321         int rc;
322
323         /* Parse options */
324         if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
325                 return rc;
326
327         /* Sanity check */
328         if ( ! current_image ) {
329                 rc = -ENOTTY;
330                 printf ( "Not in a script: %s\n", strerror ( rc ) );
331                 return rc;
332         }
333
334         /* Parse label */
335         goto_label = argv[optind];
336
337         /* Find label */
338         saved_offset = script_offset;
339         if ( ( rc = process_script ( current_image, goto_find_label,
340                                      terminate_on_label_found ) ) != 0 ) {
341                 script_offset = saved_offset;
342                 DBGC ( current_image, "[%04zx] No such label :%s\n",
343                        script_offset, goto_label );
344                 return rc;
345         }
346
347         /* Terminate processing of current command */
348         shell_stop ( SHELL_STOP_COMMAND );
349
350         return 0;
351 }
352
353 /** "goto" command */
354 struct command goto_command __command = {
355         .name = "goto",
356         .exec = goto_exec,
357 };
358
359 /** "prompt" options */
360 struct prompt_options {
361         /** Key to wait for */
362         unsigned int key;
363         /** Timeout */
364         unsigned long timeout;
365 };
366
367 /** "prompt" option list */
368 static struct option_descriptor prompt_opts[] = {
369         OPTION_DESC ( "key", 'k', required_argument,
370                       struct prompt_options, key, parse_key ),
371         OPTION_DESC ( "timeout", 't', required_argument,
372                       struct prompt_options, timeout, parse_timeout ),
373 };
374
375 /** "prompt" command descriptor */
376 static struct command_descriptor prompt_cmd =
377         COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
378                        "[<text>]" );
379
380 /**
381  * "prompt" command
382  *
383  * @v argc              Argument count
384  * @v argv              Argument list
385  * @ret rc              Return status code
386  */
387 static int prompt_exec ( int argc, char **argv ) {
388         struct prompt_options opts;
389         char *text;
390         int rc;
391
392         /* Parse options */
393         if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
394                 goto err_parse;
395
396         /* Parse prompt text */
397         text = concat_args ( &argv[optind] );
398         if ( ! text ) {
399                 rc = -ENOMEM;
400                 goto err_concat;
401         }
402
403         /* Display prompt and wait for key */
404         if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
405                 goto err_prompt;
406
407         /* Free prompt text */
408         free ( text );
409
410         return 0;
411
412  err_prompt:
413         free ( text );
414  err_concat:
415  err_parse:
416         return rc;
417 }
418
419 /** "prompt" command */
420 struct command prompt_command __command = {
421         .name = "prompt",
422         .exec = prompt_exec,
423 };