2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
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>
42 /** Offset within current script
44 * This is a global in order to allow goto_exec() to update the
47 static size_t script_offset;
50 * Process script lines
53 * @v process_line Line processor
54 * @v terminate Termination check
55 * @ret rc Return status code
57 static int process_script ( struct image *image,
58 int ( * process_line ) ( struct image *image,
61 const char *command ),
62 int ( * terminate ) ( int rc ) ) {
73 /* Initialise script and line offsets */
79 /* Find length of next line, excluding any terminating '\n' */
80 eol = memchr_user ( image->data, script_offset, '\n',
81 ( image->len - script_offset ) );
84 frag_len = ( eol - script_offset );
86 /* Allocate buffer for line */
87 tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
95 copy_from_user ( ( line + len ), image->data, script_offset,
99 /* Move to next line in script */
100 script_offset += ( frag_len + 1 );
102 /* Strip trailing CR, if present */
103 if ( len && ( line[ len - 1 ] == '\r' ) )
106 /* Handle backslash continuations */
107 if ( len && ( line[ len - 1 ] == '\\' ) ) {
116 /* Split line into (optional) label and command */
118 while ( isspace ( *command ) )
120 if ( *command == ':' ) {
122 while ( *command && ! isspace ( *command ) )
131 rc = process_line ( image, line_offset, label, command );
132 if ( terminate ( rc ) )
140 /* Update line offset */
141 line_offset = script_offset;
143 } while ( script_offset < image->len );
152 * Terminate script processing on shell exit or command failure
154 * @v rc Line processing status
155 * @ret terminate Terminate script processing
157 static int terminate_on_exit_or_failure ( int rc ) {
159 return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
164 * Execute script line
167 * @v offset Offset within script
168 * @v label Label, or NULL
170 * @ret rc Return status code
172 static int script_exec_line ( struct image *image, size_t offset,
173 const char *label __unused,
174 const char *command ) {
177 DBGC ( image, "[%04zx] $ %s\n", offset, command );
179 /* Execute command */
180 if ( ( rc = system ( command ) ) != 0 )
190 * @ret rc Return status code
192 static int script_exec ( struct image *image ) {
196 /* Temporarily de-register image, so that a "boot" command
197 * doesn't throw us into an execution loop.
199 unregister_image ( image );
201 /* Preserve state of any currently-running script */
202 saved_offset = script_offset;
205 rc = process_script ( image, script_exec_line,
206 terminate_on_exit_or_failure );
208 /* Restore saved state */
209 script_offset = saved_offset;
211 /* Re-register image (unless we have been replaced) */
212 if ( ! image->replacement )
213 register_image ( image );
222 * @ret rc Return status code
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 */];
233 if ( image->len < sizeof ( test ) ) {
234 DBGC ( image, "Too short to be a script\n" );
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" );
250 /** Script image type */
251 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
253 .probe = script_probe,
257 /** "goto" options */
258 struct goto_options {};
260 /** "goto" option list */
261 static struct option_descriptor goto_opts[] = {};
263 /** "goto" command descriptor */
264 static struct command_descriptor goto_cmd =
265 COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
268 * Current "goto" label
270 * Valid only during goto_exec(). Consider this part of a closure.
272 static const char *goto_label;
275 * Check for presence of label
278 * @v offset Offset within script
281 * @ret rc Return status code
283 static int goto_find_label ( struct image *image, size_t offset,
284 const char *label, const char *command __unused ) {
286 /* Check label exists */
290 /* Check label matches */
291 if ( strcmp ( goto_label, label ) != 0 )
294 /* Update script offset */
295 script_offset = offset;
296 DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
302 * Terminate script processing when label is found
304 * @v rc Line processing status
305 * @ret terminate Terminate script processing
307 static int terminate_on_label_found ( int rc ) {
314 * @v argc Argument count
315 * @v argv Argument list
316 * @ret rc Return status code
318 static int goto_exec ( int argc, char **argv ) {
319 struct goto_options opts;
324 if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
328 if ( ! current_image ) {
330 printf ( "Not in a script: %s\n", strerror ( rc ) );
335 goto_label = argv[optind];
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 );
347 /* Terminate processing of current command */
348 shell_stop ( SHELL_STOP_COMMAND );
353 /** "goto" command */
354 struct command goto_command __command = {
359 /** "prompt" options */
360 struct prompt_options {
361 /** Key to wait for */
364 unsigned long timeout;
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 ),
375 /** "prompt" command descriptor */
376 static struct command_descriptor prompt_cmd =
377 COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
383 * @v argc Argument count
384 * @v argv Argument list
385 * @ret rc Return status code
387 static int prompt_exec ( int argc, char **argv ) {
388 struct prompt_options opts;
393 if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
396 /* Parse prompt text */
397 text = concat_args ( &argv[optind] );
403 /* Display prompt and wait for key */
404 if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
407 /* Free prompt text */
419 /** "prompt" command */
420 struct command prompt_command __command = {