~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to grub-core/kern/parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* parser.c - the part of the parser that can return partial tokens */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005,2007,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/parser.h>
 
21
#include <grub/env.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
 
 
25
 
 
26
/* All the possible state transitions on the command line.  If a
 
27
   transition can not be found, it is assumed that there is no
 
28
   transition and keep_value is assumed to be 1.  */
 
29
static struct grub_parser_state_transition state_transitions[] = {
 
30
  {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
 
31
  {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
 
32
  {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
 
33
  {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
 
34
 
 
35
  {GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
 
36
 
 
37
  {GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
 
38
 
 
39
  {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
 
40
  {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
 
41
 
 
42
  {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
 
43
  {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
 
44
  {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
 
45
  {GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
 
46
 
 
47
  {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
 
48
  {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
 
49
  {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
 
50
  {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
 
51
  {GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
 
52
 
 
53
  {0, 0, 0, 0}
 
54
};
 
55
 
 
56
 
 
57
/* Determines the state following STATE, determined by C.  */
 
58
grub_parser_state_t
 
59
grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
 
60
{
 
61
  struct grub_parser_state_transition *transition;
 
62
  struct grub_parser_state_transition default_transition;
 
63
 
 
64
  default_transition.to_state = state;
 
65
  default_transition.keep_value = 1;
 
66
 
 
67
  /* Look for a good translation.  */
 
68
  for (transition = state_transitions; transition->from_state; transition++)
 
69
    {
 
70
      if (transition->from_state != state)
 
71
        continue;
 
72
      /* An exact match was found, use it.  */
 
73
      if (transition->input == c)
 
74
        break;
 
75
 
 
76
      if (transition->input == ' ' && !grub_isalpha (c)
 
77
          && !grub_isdigit (c) && c != '_')
 
78
        break;
 
79
 
 
80
      /* A less perfect match was found, use this one if no exact
 
81
         match can be found.  */
 
82
      if (transition->input == 0)
 
83
        break;
 
84
    }
 
85
 
 
86
  if (!transition->from_state)
 
87
    transition = &default_transition;
 
88
 
 
89
  if (transition->keep_value)
 
90
    *result = c;
 
91
  else
 
92
    *result = 0;
 
93
  return transition->to_state;
 
94
}
 
95
 
 
96
 
 
97
grub_err_t
 
98
grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
 
99
                           int *argc, char ***argv)
 
100
{
 
101
  grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
 
102
  /* XXX: Fixed size buffer, perhaps this buffer should be dynamically
 
103
     allocated.  */
 
104
  char buffer[1024];
 
105
  char *bp = buffer;
 
106
  char *rd = (char *) cmdline;
 
107
  char varname[200];
 
108
  char *vp = varname;
 
109
  char *args;
 
110
  int i;
 
111
 
 
112
  auto int check_varstate (grub_parser_state_t s);
 
113
 
 
114
  int check_varstate (grub_parser_state_t s)
 
115
  {
 
116
    return (s == GRUB_PARSER_STATE_VARNAME
 
117
            || s == GRUB_PARSER_STATE_VARNAME2
 
118
            || s == GRUB_PARSER_STATE_QVARNAME
 
119
            || s == GRUB_PARSER_STATE_QVARNAME2);
 
120
  }
 
121
 
 
122
  auto void add_var (grub_parser_state_t newstate);
 
123
 
 
124
  void add_var (grub_parser_state_t newstate)
 
125
  {
 
126
    char *val;
 
127
 
 
128
    /* Check if a variable was being read in and the end of the name
 
129
       was reached.  */
 
130
    if (!(check_varstate (state) && !check_varstate (newstate)))
 
131
      return;
 
132
 
 
133
    *(vp++) = '\0';
 
134
    val = grub_env_get (varname);
 
135
    vp = varname;
 
136
    if (!val)
 
137
      return;
 
138
 
 
139
    /* Insert the contents of the variable in the buffer.  */
 
140
    for (; *val; val++)
 
141
      *(bp++) = *val;
 
142
  }
 
143
 
 
144
  *argc = 0;
 
145
  do
 
146
    {
 
147
      if (!rd || !*rd)
 
148
        {
 
149
          if (getline)
 
150
            getline (&rd, 1);
 
151
          else
 
152
            break;
 
153
        }
 
154
 
 
155
      if (!rd)
 
156
        break;
 
157
 
 
158
      for (; *rd; rd++)
 
159
        {
 
160
          grub_parser_state_t newstate;
 
161
          char use;
 
162
 
 
163
          newstate = grub_parser_cmdline_state (state, *rd, &use);
 
164
 
 
165
          /* If a variable was being processed and this character does
 
166
             not describe the variable anymore, write the variable to
 
167
             the buffer.  */
 
168
          add_var (newstate);
 
169
 
 
170
          if (check_varstate (newstate))
 
171
            {
 
172
              if (use)
 
173
                *(vp++) = use;
 
174
            }
 
175
          else
 
176
            {
 
177
              if (newstate == GRUB_PARSER_STATE_TEXT
 
178
                  && state != GRUB_PARSER_STATE_ESC && use == ' ')
 
179
                {
 
180
                  /* Don't add more than one argument if multiple
 
181
                     spaces are used.  */
 
182
                  if (bp != buffer && *(bp - 1))
 
183
                    {
 
184
                      *(bp++) = '\0';
 
185
                      (*argc)++;
 
186
                    }
 
187
                }
 
188
              else if (use)
 
189
                *(bp++) = use;
 
190
            }
 
191
          state = newstate;
 
192
        }
 
193
    }
 
194
  while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
 
195
 
 
196
  /* A special case for when the last character was part of a
 
197
     variable.  */
 
198
  add_var (GRUB_PARSER_STATE_TEXT);
 
199
 
 
200
  if (bp != buffer && *(bp - 1))
 
201
    {
 
202
      *(bp++) = '\0';
 
203
      (*argc)++;
 
204
    }
 
205
 
 
206
  /* Reserve memory for the return values.  */
 
207
  args = grub_malloc (bp - buffer);
 
208
  if (!args)
 
209
    return grub_errno;
 
210
  grub_memcpy (args, buffer, bp - buffer);
 
211
 
 
212
  *argv = grub_malloc (sizeof (char *) * (*argc + 1));
 
213
  if (!*argv)
 
214
    {
 
215
      grub_free (args);
 
216
      return grub_errno;
 
217
    }
 
218
 
 
219
  /* The arguments are separated with 0's, setup argv so it points to
 
220
     the right values.  */
 
221
  bp = args;
 
222
  for (i = 0; i < *argc; i++)
 
223
    {
 
224
      (*argv)[i] = bp;
 
225
      while (*bp)
 
226
        bp++;
 
227
      bp++;
 
228
    }
 
229
 
 
230
  return 0;
 
231
}
 
232
 
 
233
grub_err_t
 
234
grub_parser_execute (char *source)
 
235
{
 
236
  auto grub_err_t getline (char **line, int cont);
 
237
  grub_err_t getline (char **line, int cont __attribute__ ((unused)))
 
238
  {
 
239
    char *p;
 
240
 
 
241
    if (!source)
 
242
      {
 
243
        *line = 0;
 
244
        return 0;
 
245
      }
 
246
 
 
247
    p = grub_strchr (source, '\n');
 
248
 
 
249
    if (p)
 
250
      *line = grub_strndup (source, p - source);
 
251
    else
 
252
      *line = grub_strdup (source);
 
253
    source = p ? p + 1 : 0;
 
254
    return 0;
 
255
  }
 
256
 
 
257
  while (source)
 
258
    {
 
259
      char *line;
 
260
 
 
261
      getline (&line, 0);
 
262
      grub_rescue_parse_line (line, getline);
 
263
      grub_free (line);
 
264
    }
 
265
 
 
266
  return grub_errno;
 
267
}