2007-01-12 10:00:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2007-01-12 10:00:48 +01:00
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-01-12 10:00:48 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
2010-04-19 21:16:01 +02:00
|
|
|
* iPXE scripts
|
2007-01-12 10:00:48 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-11-22 05:19:24 +01:00
|
|
|
#include <stdio.h>
|
2009-06-03 11:13:29 +02:00
|
|
|
#include <ctype.h>
|
2007-01-12 10:00:48 +01:00
|
|
|
#include <errno.h>
|
2010-11-22 05:19:24 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <ipxe/command.h>
|
|
|
|
#include <ipxe/parseopt.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/image.h>
|
2010-11-29 15:19:59 +01:00
|
|
|
#include <ipxe/shell.h>
|
2011-03-07 19:32:30 +01:00
|
|
|
#include <usr/prompt.h>
|
2011-03-07 23:02:07 +01:00
|
|
|
#include <ipxe/script.h>
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
/** Offset within current script
|
|
|
|
*
|
|
|
|
* This is a global in order to allow goto_exec() to update the
|
|
|
|
* offset.
|
|
|
|
*/
|
|
|
|
static size_t script_offset;
|
|
|
|
|
2007-01-12 10:00:48 +01:00
|
|
|
/**
|
2010-11-22 05:19:24 +01:00
|
|
|
* Process script lines
|
2007-01-12 10:00:48 +01:00
|
|
|
*
|
2011-03-09 14:38:56 +01:00
|
|
|
* @v image Script
|
2010-11-22 05:19:24 +01:00
|
|
|
* @v process_line Line processor
|
|
|
|
* @v terminate Termination check
|
2007-01-12 10:00:48 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2011-03-09 14:38:56 +01:00
|
|
|
static int process_script ( struct image *image,
|
|
|
|
int ( * process_line ) ( const char *line ),
|
2010-11-22 05:19:24 +01:00
|
|
|
int ( * terminate ) ( int rc ) ) {
|
2013-07-15 11:13:33 +02:00
|
|
|
size_t len = 0;
|
|
|
|
char *line = NULL;
|
2008-06-18 01:43:58 +02:00
|
|
|
off_t eol;
|
2013-07-15 11:13:33 +02:00
|
|
|
size_t frag_len;
|
|
|
|
char *tmp;
|
2007-01-12 10:00:48 +01:00
|
|
|
int rc;
|
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
script_offset = 0;
|
2007-06-09 20:00:34 +02:00
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
do {
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2008-06-18 01:43:58 +02:00
|
|
|
/* Find length of next line, excluding any terminating '\n' */
|
2011-03-09 14:38:56 +01:00
|
|
|
eol = memchr_user ( image->data, script_offset, '\n',
|
|
|
|
( image->len - script_offset ) );
|
2008-06-18 01:43:58 +02:00
|
|
|
if ( eol < 0 )
|
2011-03-09 14:38:56 +01:00
|
|
|
eol = image->len;
|
2013-07-15 11:13:33 +02:00
|
|
|
frag_len = ( eol - script_offset );
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2012-04-23 23:42:10 +02:00
|
|
|
/* Allocate buffer for line */
|
2013-07-15 11:13:33 +02:00
|
|
|
tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
|
|
|
|
if ( ! tmp ) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
|
|
|
line = tmp;
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2012-04-23 23:42:10 +02:00
|
|
|
/* Copy line */
|
2013-07-15 11:13:33 +02:00
|
|
|
copy_from_user ( ( line + len ), image->data, script_offset,
|
|
|
|
frag_len );
|
|
|
|
len += frag_len;
|
|
|
|
|
|
|
|
/* Move to next line in script */
|
|
|
|
script_offset += ( frag_len + 1 );
|
|
|
|
|
|
|
|
/* Strip trailing CR, if present */
|
2013-07-15 18:06:41 +02:00
|
|
|
if ( len && ( line[ len - 1 ] == '\r' ) )
|
2013-07-15 11:13:33 +02:00
|
|
|
len--;
|
|
|
|
|
|
|
|
/* Handle backslash continuations */
|
2013-07-15 18:06:41 +02:00
|
|
|
if ( len && ( line[ len - 1 ] == '\\' ) ) {
|
2013-07-15 11:13:33 +02:00
|
|
|
len--;
|
|
|
|
rc = -EINVAL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate line */
|
|
|
|
line[len] = '\0';
|
2012-04-23 23:42:10 +02:00
|
|
|
DBG ( "$ %s\n", line );
|
2010-11-22 05:19:24 +01:00
|
|
|
|
2013-07-15 11:13:33 +02:00
|
|
|
/* Process line */
|
2012-04-23 23:42:10 +02:00
|
|
|
rc = process_line ( line );
|
|
|
|
if ( terminate ( rc ) )
|
2013-07-15 11:13:33 +02:00
|
|
|
goto err_process;
|
|
|
|
|
|
|
|
/* Free line */
|
|
|
|
free ( line );
|
|
|
|
line = NULL;
|
|
|
|
len = 0;
|
2010-11-22 05:19:24 +01:00
|
|
|
|
2011-03-09 14:38:56 +01:00
|
|
|
} while ( script_offset < image->len );
|
2010-11-22 05:19:24 +01:00
|
|
|
|
2013-07-15 11:13:33 +02:00
|
|
|
err_process:
|
|
|
|
err_alloc:
|
|
|
|
free ( line );
|
2010-11-22 05:19:24 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-22 21:20:33 +01:00
|
|
|
* Terminate script processing on shell exit or command failure
|
2010-11-22 05:19:24 +01:00
|
|
|
*
|
|
|
|
* @v rc Line processing status
|
|
|
|
* @ret terminate Terminate script processing
|
|
|
|
*/
|
2010-11-22 21:20:33 +01:00
|
|
|
static int terminate_on_exit_or_failure ( int rc ) {
|
2010-11-22 05:19:24 +01:00
|
|
|
|
2010-11-29 15:19:59 +01:00
|
|
|
return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
|
|
|
|
( rc != 0 ) );
|
2010-11-22 05:19:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute script line
|
|
|
|
*
|
|
|
|
* @v line Line of script
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int script_exec_line ( const char *line ) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Skip label lines */
|
|
|
|
if ( line[0] == ':' )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Execute command */
|
2010-11-29 15:38:03 +01:00
|
|
|
if ( ( rc = system ( line ) ) != 0 )
|
2010-11-22 05:19:24 +01:00
|
|
|
return rc;
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute script
|
|
|
|
*
|
|
|
|
* @v image Script
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int script_exec ( struct image *image ) {
|
|
|
|
size_t saved_offset;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Temporarily de-register image, so that a "boot" command
|
|
|
|
* doesn't throw us into an execution loop.
|
|
|
|
*/
|
|
|
|
unregister_image ( image );
|
|
|
|
|
|
|
|
/* Preserve state of any currently-running script */
|
|
|
|
saved_offset = script_offset;
|
|
|
|
|
|
|
|
/* Process script */
|
2011-03-09 14:38:56 +01:00
|
|
|
rc = process_script ( image, script_exec_line,
|
|
|
|
terminate_on_exit_or_failure );
|
2010-11-22 05:19:24 +01:00
|
|
|
|
2011-03-09 18:32:32 +01:00
|
|
|
/* Restore saved state */
|
2010-11-22 05:19:24 +01:00
|
|
|
script_offset = saved_offset;
|
2011-03-09 18:32:32 +01:00
|
|
|
|
|
|
|
/* Re-register image (unless we have been replaced) */
|
|
|
|
if ( ! image->replacement )
|
|
|
|
register_image ( image );
|
|
|
|
|
2007-06-09 20:00:34 +02:00
|
|
|
return rc;
|
2007-01-12 10:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-07 01:37:50 +01:00
|
|
|
* Probe script image
|
2007-01-12 10:00:48 +01:00
|
|
|
*
|
|
|
|
* @v image Script
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2011-03-07 01:37:50 +01:00
|
|
|
static int script_probe ( struct image *image ) {
|
2010-05-22 01:27:10 +02:00
|
|
|
static const char ipxe_magic[] = "#!ipxe";
|
|
|
|
static const char gpxe_magic[] = "#!gpxe";
|
|
|
|
linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
|
|
|
|
magic_size_mismatch );
|
|
|
|
char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
|
|
|
|
+ 1 /* terminating space */];
|
2007-01-12 10:00:48 +01:00
|
|
|
|
2009-02-16 01:30:48 +01:00
|
|
|
/* Sanity check */
|
|
|
|
if ( image->len < sizeof ( test ) ) {
|
|
|
|
DBG ( "Too short to be a script\n" );
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
2007-01-12 10:00:48 +01:00
|
|
|
/* Check for magic signature */
|
|
|
|
copy_from_user ( test, image->data, 0, sizeof ( test ) );
|
2010-05-22 01:27:10 +02:00
|
|
|
if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
|
|
|
|
( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
|
|
|
|
isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
|
2007-01-12 10:00:48 +01:00
|
|
|
DBG ( "Invalid magic signature\n" );
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Script image type */
|
|
|
|
struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
|
|
|
|
.name = "script",
|
2011-03-07 01:37:50 +01:00
|
|
|
.probe = script_probe,
|
2007-01-12 10:00:48 +01:00
|
|
|
.exec = script_exec,
|
|
|
|
};
|
2010-11-22 05:19:24 +01:00
|
|
|
|
|
|
|
/** "goto" options */
|
|
|
|
struct goto_options {};
|
|
|
|
|
|
|
|
/** "goto" option list */
|
|
|
|
static struct option_descriptor goto_opts[] = {};
|
|
|
|
|
|
|
|
/** "goto" command descriptor */
|
|
|
|
static struct command_descriptor goto_cmd =
|
2011-03-04 13:14:51 +01:00
|
|
|
COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
|
2010-11-22 05:19:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current "goto" label
|
|
|
|
*
|
|
|
|
* Valid only during goto_exec(). Consider this part of a closure.
|
|
|
|
*/
|
|
|
|
static const char *goto_label;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for presence of label
|
|
|
|
*
|
|
|
|
* @v line Script line
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int goto_find_label ( const char *line ) {
|
2011-08-08 17:35:30 +02:00
|
|
|
size_t len = strlen ( goto_label );
|
2010-11-22 05:19:24 +01:00
|
|
|
|
|
|
|
if ( line[0] != ':' )
|
|
|
|
return -ENOENT;
|
2011-08-08 17:35:30 +02:00
|
|
|
|
|
|
|
if ( strncmp ( goto_label, &line[1], len ) != 0 )
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
|
2010-11-22 05:19:24 +01:00
|
|
|
return -ENOENT;
|
2011-08-08 17:35:30 +02:00
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-22 21:20:33 +01:00
|
|
|
/**
|
|
|
|
* Terminate script processing when label is found
|
|
|
|
*
|
|
|
|
* @v rc Line processing status
|
|
|
|
* @ret terminate Terminate script processing
|
|
|
|
*/
|
|
|
|
static int terminate_on_label_found ( int rc ) {
|
|
|
|
return ( rc == 0 );
|
|
|
|
}
|
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
/**
|
|
|
|
* "goto" command
|
|
|
|
*
|
|
|
|
* @v argc Argument count
|
|
|
|
* @v argv Argument list
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int goto_exec ( int argc, char **argv ) {
|
|
|
|
struct goto_options opts;
|
|
|
|
size_t saved_offset;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Parse options */
|
|
|
|
if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2011-03-09 14:38:56 +01:00
|
|
|
if ( ! current_image ) {
|
2011-03-07 04:11:14 +01:00
|
|
|
rc = -ENOTTY;
|
|
|
|
printf ( "Not in a script: %s\n", strerror ( rc ) );
|
|
|
|
return rc;
|
2010-11-22 05:19:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse label */
|
|
|
|
goto_label = argv[optind];
|
|
|
|
|
|
|
|
/* Find label */
|
|
|
|
saved_offset = script_offset;
|
2011-03-09 14:38:56 +01:00
|
|
|
if ( ( rc = process_script ( current_image, goto_find_label,
|
2010-11-22 21:20:33 +01:00
|
|
|
terminate_on_label_found ) ) != 0 ) {
|
2010-11-22 05:19:24 +01:00
|
|
|
script_offset = saved_offset;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-11-29 15:19:59 +01:00
|
|
|
/* Terminate processing of current command */
|
|
|
|
shell_stop ( SHELL_STOP_COMMAND );
|
|
|
|
|
2010-11-22 05:19:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** "goto" command */
|
|
|
|
struct command goto_command __command = {
|
|
|
|
.name = "goto",
|
|
|
|
.exec = goto_exec,
|
|
|
|
};
|
2011-03-07 19:32:30 +01:00
|
|
|
|
|
|
|
/** "prompt" options */
|
|
|
|
struct prompt_options {
|
|
|
|
/** Key to wait for */
|
|
|
|
unsigned int key;
|
|
|
|
/** Timeout */
|
|
|
|
unsigned int timeout;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** "prompt" option list */
|
|
|
|
static struct option_descriptor prompt_opts[] = {
|
|
|
|
OPTION_DESC ( "key", 'k', required_argument,
|
2012-03-27 20:49:12 +02:00
|
|
|
struct prompt_options, key, parse_key ),
|
2011-03-07 19:32:30 +01:00
|
|
|
OPTION_DESC ( "timeout", 't', required_argument,
|
|
|
|
struct prompt_options, timeout, parse_integer ),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** "prompt" command descriptor */
|
|
|
|
static struct command_descriptor prompt_cmd =
|
|
|
|
COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
|
|
|
|
"[--key <key>] [--timeout <timeout>] [<text>]" );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "prompt" command
|
|
|
|
*
|
|
|
|
* @v argc Argument count
|
|
|
|
* @v argv Argument list
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int prompt_exec ( int argc, char **argv ) {
|
|
|
|
struct prompt_options opts;
|
|
|
|
char *text;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Parse options */
|
|
|
|
if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
|
|
|
|
goto err_parse;
|
|
|
|
|
|
|
|
/* Parse prompt text */
|
|
|
|
text = concat_args ( &argv[optind] );
|
|
|
|
if ( ! text ) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err_concat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display prompt and wait for key */
|
|
|
|
if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
|
|
|
|
goto err_prompt;
|
|
|
|
|
|
|
|
/* Free prompt text */
|
|
|
|
free ( text );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_prompt:
|
|
|
|
free ( text );
|
|
|
|
err_concat:
|
|
|
|
err_parse:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** "prompt" command */
|
|
|
|
struct command prompt_command __command = {
|
|
|
|
.name = "prompt",
|
|
|
|
.exec = prompt_exec,
|
|
|
|
};
|