2005-11-06 22:19:59 +00:00
|
|
|
/* execute.c -- Execute a GRUB script. */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2009-01-31 09:15:43 +00:00
|
|
|
* Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
|
2005-11-06 22:19:59 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2005-11-06 22:19:59 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2005-11-06 22:19:59 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2005-11-06 22:19:59 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2005-11-06 22:19:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/normal.h>
|
|
|
|
#include <grub/arg.h>
|
|
|
|
#include <grub/env.h>
|
|
|
|
#include <grub/script.h>
|
|
|
|
|
2006-04-30 20:29:51 +00:00
|
|
|
static grub_err_t
|
2005-11-06 22:19:59 +00:00
|
|
|
grub_script_execute_cmd (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
if (cmd == 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-04-30 20:29:51 +00:00
|
|
|
return cmd->exec (cmd);
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse ARG and return the textual representation. Add strings are
|
|
|
|
concatenated and all values of the variables are filled in. */
|
|
|
|
static char *
|
|
|
|
grub_script_execute_argument_to_string (struct grub_script_arg *arg)
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
char *val;
|
|
|
|
char *chararg;
|
|
|
|
struct grub_script_arg *argi;
|
|
|
|
|
|
|
|
/* First determine the size of the argument. */
|
|
|
|
for (argi = arg; argi; argi = argi->next)
|
|
|
|
{
|
|
|
|
if (argi->type == 1)
|
|
|
|
{
|
|
|
|
val = grub_env_get (argi->str);
|
2008-01-15 15:32:17 +00:00
|
|
|
if (val)
|
|
|
|
size += grub_strlen (val);
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
size += grub_strlen (argi->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the argument. */
|
|
|
|
chararg = grub_malloc (size + 1);
|
|
|
|
if (! chararg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*chararg = '\0';
|
|
|
|
/* First determine the size of the argument. */
|
|
|
|
for (argi = arg; argi; argi = argi->next)
|
|
|
|
{
|
|
|
|
if (argi->type == 1)
|
|
|
|
{
|
|
|
|
val = grub_env_get (argi->str);
|
2008-01-15 15:32:17 +00:00
|
|
|
if (val)
|
|
|
|
grub_strcat (chararg, val);
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
grub_strcat (chararg, argi->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chararg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute a single command line. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
|
|
|
|
struct grub_script_arglist *arglist;
|
|
|
|
char **args = 0;
|
|
|
|
int i = 0;
|
|
|
|
grub_command_t grubcmd;
|
|
|
|
struct grub_arg_list *state;
|
|
|
|
struct grub_arg_option *parser;
|
|
|
|
int maxargs = 0;
|
|
|
|
char **parsed_arglist;
|
|
|
|
int numargs;
|
|
|
|
grub_err_t ret = 0;
|
|
|
|
int argcount = 0;
|
|
|
|
grub_script_function_t func = 0;
|
|
|
|
char errnobuf[6];
|
|
|
|
|
|
|
|
/* Lookup the command. */
|
|
|
|
grubcmd = grub_command_find (cmdline->cmdname);
|
|
|
|
if (! grubcmd)
|
|
|
|
{
|
2008-01-15 15:32:17 +00:00
|
|
|
/* Ignore errors. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* It's not a GRUB command, try all functions. */
|
|
|
|
func = grub_script_function_find (cmdline->cmdname);
|
|
|
|
if (! func)
|
2006-04-30 20:29:51 +00:00
|
|
|
{
|
|
|
|
/* As a last resort, try if it is an assignment. */
|
|
|
|
char *assign = grub_strdup (cmdline->cmdname);
|
|
|
|
char *eq = grub_strchr (assign, '=');
|
|
|
|
|
|
|
|
if (eq)
|
|
|
|
{
|
|
|
|
/* Create two strings and set the variable. */
|
|
|
|
*eq = '\0';
|
|
|
|
eq++;
|
|
|
|
grub_env_set (assign, eq);
|
|
|
|
|
|
|
|
/* This was set because the command was not found. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
grub_free (assign);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdline->arglist)
|
|
|
|
{
|
|
|
|
argcount = cmdline->arglist->argcount;
|
|
|
|
|
|
|
|
/* Create argv from the arguments. */
|
|
|
|
args = grub_malloc (sizeof (char *) * argcount);
|
|
|
|
for (arglist = cmdline->arglist; arglist; arglist = arglist->next)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
str = grub_script_execute_argument_to_string (arglist->arg);
|
|
|
|
args[i++] = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the GRUB command or function. */
|
|
|
|
if (grubcmd)
|
|
|
|
{
|
|
|
|
/* Count the amount of options the command has. */
|
|
|
|
parser = (struct grub_arg_option *) grubcmd->options;
|
|
|
|
while (parser && (parser++)->doc)
|
|
|
|
maxargs++;
|
2009-01-31 09:15:43 +00:00
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* Set up the option state. */
|
|
|
|
state = grub_malloc (sizeof (struct grub_arg_list) * maxargs);
|
|
|
|
grub_memset (state, 0, sizeof (struct grub_arg_list) * maxargs);
|
2009-01-31 09:15:43 +00:00
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* Start the command. */
|
|
|
|
if (! (grubcmd->flags & GRUB_COMMAND_FLAG_NO_ARG_PARSE))
|
|
|
|
{
|
|
|
|
if (grub_arg_parse (grubcmd, argcount, args, state, &parsed_arglist, &numargs))
|
|
|
|
ret = (grubcmd->func) (state, numargs, parsed_arglist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = (grubcmd->func) (state, argcount, args);
|
2009-01-31 09:15:43 +00:00
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
grub_free (state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = grub_script_function_call (func, argcount, args);
|
|
|
|
|
|
|
|
/* Free arguments. */
|
|
|
|
for (i = 0; i < argcount; i++)
|
|
|
|
grub_free (args[i]);
|
|
|
|
grub_free (args);
|
|
|
|
|
|
|
|
grub_sprintf (errnobuf, "%d", ret);
|
|
|
|
grub_env_set ("?", errnobuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
/* Execute a block of one or more commands. */
|
2005-11-06 22:19:59 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
|
|
|
|
|
|
|
|
/* Loop over every command and execute it. */
|
|
|
|
for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
|
|
|
|
grub_script_execute_cmd (cmd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute an if statement. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
|
2006-05-28 21:58:35 +00:00
|
|
|
char *result;
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Check if the commands results in a true or a false. The value is
|
2006-05-28 21:58:35 +00:00
|
|
|
read from the env variable `?'. */
|
|
|
|
grub_script_execute_cmd (cmdif->exec_to_evaluate);
|
|
|
|
result = grub_env_get ("?");
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Execute the `if' or the `else' part depending on the value of
|
2006-05-28 21:58:35 +00:00
|
|
|
`?'. */
|
|
|
|
if (result && ! grub_strcmp (result, "0"))
|
|
|
|
return grub_script_execute_cmd (cmdif->exec_on_true);
|
2005-11-06 22:19:59 +00:00
|
|
|
else
|
2006-05-28 21:58:35 +00:00
|
|
|
return grub_script_execute_cmd (cmdif->exec_on_false);
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-17 09:50:47 +00:00
|
|
|
/* Execute the menu entry generate statement. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_menuentry (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct grub_script_cmd_menuentry *cmd_menuentry;
|
2009-01-31 09:15:43 +00:00
|
|
|
struct grub_script_arglist *arglist;
|
2006-01-17 09:50:47 +00:00
|
|
|
struct grub_script *script;
|
2009-01-31 09:15:43 +00:00
|
|
|
char **args = 0;
|
|
|
|
int argcount = 0;
|
|
|
|
int i = 0;
|
2006-01-17 09:50:47 +00:00
|
|
|
|
|
|
|
cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
if (cmd_menuentry->arglist)
|
|
|
|
{
|
|
|
|
argcount = cmd_menuentry->arglist->argcount;
|
|
|
|
|
|
|
|
/* Create argv from the arguments. */
|
|
|
|
args = grub_malloc (sizeof (char *) * argcount);
|
|
|
|
|
|
|
|
if (! args)
|
|
|
|
{
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (arglist = cmd_menuentry->arglist; arglist; arglist = arglist->next)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
str = grub_script_execute_argument_to_string (arglist->arg);
|
|
|
|
args[i++] = str;
|
|
|
|
}
|
|
|
|
}
|
2006-01-17 09:50:47 +00:00
|
|
|
|
|
|
|
/* Parse the menu entry *again*. */
|
|
|
|
script = grub_script_parse ((char *) cmd_menuentry->sourcecode, 0);
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
/* Add new menu entry. */
|
|
|
|
if (script)
|
2006-01-17 09:50:47 +00:00
|
|
|
{
|
2009-01-31 09:15:43 +00:00
|
|
|
grub_normal_menu_addentry (argcount, (const char **)args,
|
|
|
|
script, cmd_menuentry->sourcecode);
|
2006-01-17 09:50:47 +00:00
|
|
|
}
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
/* Free arguments. */
|
|
|
|
for (i = 0; i < argcount; i++)
|
|
|
|
grub_free (args[i]);
|
|
|
|
grub_free (args);
|
|
|
|
|
|
|
|
return grub_errno;
|
2006-01-17 09:50:47 +00:00
|
|
|
}
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Execute any GRUB pre-parsed command or script. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute (struct grub_script *script)
|
|
|
|
{
|
|
|
|
if (script == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return grub_script_execute_cmd (script->cmd);
|
|
|
|
}
|