~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to script/execute.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* execute.c -- Execute a GRUB script.  */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2005,2007,2008,2009  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB is free software: you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation, either version 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/misc.h>
21
 
#include <grub/mm.h>
22
 
#include <grub/env.h>
23
 
#include <grub/script_sh.h>
24
 
#include <grub/command.h>
25
 
#include <grub/menu.h>
26
 
#include <grub/lib/arg.h>
27
 
#include <grub/normal.h>
28
 
 
29
 
static grub_err_t
30
 
grub_script_execute_cmd (struct grub_script_cmd *cmd)
31
 
{
32
 
  if (cmd == 0)
33
 
    return 0;
34
 
 
35
 
  return cmd->exec (cmd);
36
 
}
37
 
 
38
 
/* Parse ARG and return the textual representation.  Add strings are
39
 
   concatenated and all values of the variables are filled in.  */
40
 
char *
41
 
grub_script_execute_argument_to_string (struct grub_script_arg *arg)
42
 
{
43
 
  int size = 0;
44
 
  char *val;
45
 
  char *chararg;
46
 
  struct grub_script_arg *argi;
47
 
 
48
 
  /* First determine the size of the argument.  */
49
 
  for (argi = arg; argi; argi = argi->next)
50
 
    {
51
 
      if (argi->type == 1)
52
 
        {
53
 
          val = grub_env_get (argi->str);
54
 
          if (val)
55
 
            size += grub_strlen (val);
56
 
        }
57
 
      else
58
 
        size += grub_strlen (argi->str);
59
 
    }
60
 
 
61
 
  /* Create the argument.  */
62
 
  chararg = grub_malloc (size + 1);
63
 
  if (! chararg)
64
 
    return 0;
65
 
 
66
 
  *chararg = '\0';
67
 
  /* First determine the size of the argument.  */
68
 
  for (argi = arg; argi; argi = argi->next)
69
 
    {
70
 
      if (argi->type == 1)
71
 
        {
72
 
          val = grub_env_get (argi->str);
73
 
          if (val)
74
 
            grub_strcat (chararg, val);
75
 
        }
76
 
      else
77
 
        grub_strcat (chararg, argi->str);
78
 
    }
79
 
 
80
 
  return chararg;
81
 
}
82
 
 
83
 
/* Execute a single command line.  */
84
 
grub_err_t
85
 
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
86
 
{
87
 
  struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
88
 
  struct grub_script_arglist *arglist;
89
 
  char **args = 0;
90
 
  int i = 0;
91
 
  grub_command_t grubcmd;
92
 
  grub_err_t ret = 0;
93
 
  int argcount = 0;
94
 
  grub_script_function_t func = 0;
95
 
  char errnobuf[18];
96
 
  char *cmdname;
97
 
 
98
 
  /* Lookup the command.  */
99
 
  cmdname = grub_script_execute_argument_to_string (cmdline->arglist->arg);
100
 
  grubcmd = grub_command_find (cmdname);
101
 
  if (! grubcmd)
102
 
    {
103
 
      /* Ignore errors.  */
104
 
      grub_errno = GRUB_ERR_NONE;
105
 
 
106
 
      /* It's not a GRUB command, try all functions.  */
107
 
      func = grub_script_function_find (cmdname);
108
 
      if (! func)
109
 
        {
110
 
          /* As a last resort, try if it is an assignment.  */
111
 
          char *assign = grub_strdup (cmdname);
112
 
          char *eq = grub_strchr (assign, '=');
113
 
 
114
 
          if (eq)
115
 
            {
116
 
              /* This was set because the command was not found.  */
117
 
              grub_errno = GRUB_ERR_NONE;
118
 
 
119
 
              /* Create two strings and set the variable.  */
120
 
              *eq = '\0';
121
 
              eq++;
122
 
              grub_env_set (assign, eq);
123
 
            }
124
 
          grub_free (assign);
125
 
 
126
 
          grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
127
 
          grub_env_set ("?", errnobuf);
128
 
 
129
 
          return 0;
130
 
        }
131
 
    }
132
 
  grub_free (cmdname);
133
 
 
134
 
  if (cmdline->arglist->next)
135
 
    {
136
 
      argcount = cmdline->arglist->argcount - 1;
137
 
 
138
 
      /* Create argv from the arguments.  */
139
 
      args = grub_malloc (sizeof (char *) * argcount);
140
 
      for (arglist = cmdline->arglist->next; arglist; arglist = arglist->next)
141
 
        {
142
 
          char *str;
143
 
          str = grub_script_execute_argument_to_string (arglist->arg);
144
 
          args[i++] = str;
145
 
        }
146
 
    }
147
 
 
148
 
  /* Execute the GRUB command or function.  */
149
 
  if (grubcmd)
150
 
    ret = (grubcmd->func) (grubcmd, argcount, args);
151
 
  else
152
 
    ret = grub_script_function_call (func, argcount, args);
153
 
 
154
 
  /* Free arguments.  */
155
 
  for (i = 0; i < argcount; i++)
156
 
    grub_free (args[i]);
157
 
  grub_free (args);
158
 
 
159
 
  grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
160
 
  grub_env_set ("?", errnobuf);
161
 
 
162
 
  return ret;
163
 
}
164
 
 
165
 
/* Execute a block of one or more commands.  */
166
 
grub_err_t
167
 
grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
168
 
{
169
 
  struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
170
 
 
171
 
  /* Loop over every command and execute it.  */
172
 
  for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
173
 
    grub_script_execute_cmd (cmd);
174
 
 
175
 
  return 0;
176
 
}
177
 
 
178
 
/* Execute an if statement.  */
179
 
grub_err_t
180
 
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
181
 
{
182
 
  struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
183
 
  char *result;
184
 
 
185
 
  /* Check if the commands results in a true or a false.  The value is
186
 
     read from the env variable `?'.  */
187
 
  grub_script_execute_cmd (cmdif->exec_to_evaluate);
188
 
  result = grub_env_get ("?");
189
 
 
190
 
  grub_errno = GRUB_ERR_NONE;
191
 
 
192
 
  /* Execute the `if' or the `else' part depending on the value of
193
 
     `?'.  */
194
 
  if (result && ! grub_strcmp (result, "0"))
195
 
    return grub_script_execute_cmd (cmdif->exec_on_true);
196
 
  else
197
 
    return grub_script_execute_cmd (cmdif->exec_on_false);
198
 
}
199
 
 
200
 
/* Execute the menu entry generate statement.  */
201
 
grub_err_t
202
 
grub_script_execute_menuentry (struct grub_script_cmd *cmd)
203
 
{
204
 
  struct grub_script_cmd_menuentry *cmd_menuentry;
205
 
  struct grub_script_arglist *arglist;
206
 
  char **args = 0;
207
 
  int argcount = 0;
208
 
  int i = 0;
209
 
 
210
 
  cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
211
 
 
212
 
  if (cmd_menuentry->arglist)
213
 
    {
214
 
      argcount = cmd_menuentry->arglist->argcount;
215
 
 
216
 
      /* Create argv from the arguments.  */
217
 
      args = grub_malloc (sizeof (char *) * argcount);
218
 
 
219
 
      if (! args)
220
 
        {
221
 
          return grub_errno;
222
 
        }
223
 
 
224
 
      for (arglist = cmd_menuentry->arglist; arglist; arglist = arglist->next)
225
 
        {
226
 
          char *str;
227
 
          str = grub_script_execute_argument_to_string (arglist->arg);
228
 
          args[i++] = str;
229
 
        }
230
 
    }
231
 
 
232
 
  grub_normal_add_menu_entry (argcount, (const char **) args,
233
 
                              cmd_menuentry->sourcecode);
234
 
 
235
 
  /* Free arguments.  */
236
 
  for (i = 0; i < argcount; i++)
237
 
    grub_free (args[i]);
238
 
  grub_free (args);
239
 
 
240
 
  return grub_errno;
241
 
}
242
 
 
243
 
 
244
 
 
245
 
/* Execute any GRUB pre-parsed command or script.  */
246
 
grub_err_t
247
 
grub_script_execute (struct grub_script *script)
248
 
{
249
 
  if (script == 0)
250
 
    return 0;
251
 
 
252
 
  return grub_script_execute_cmd (script->cmd);
253
 
}
254