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

« back to all changes in this revision

Viewing changes to grub-core/kern/env.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
/* env.c - Environment variables */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003,2005,2006,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/env.h>
 
21
#include <grub/env_private.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
 
 
25
/* The initial context.  */
 
26
static struct grub_env_context initial_context;
 
27
 
 
28
/* The current context.  */
 
29
struct grub_env_context *grub_current_context = &initial_context;
 
30
 
 
31
/* Return the hash representation of the string S.  */
 
32
static unsigned int
 
33
grub_env_hashval (const char *s)
 
34
{
 
35
  unsigned int i = 0;
 
36
 
 
37
  /* XXX: This can be done much more efficiently.  */
 
38
  while (*s)
 
39
    i += 5 * *(s++);
 
40
 
 
41
  return i % HASHSZ;
 
42
}
 
43
 
 
44
struct grub_env_var *
 
45
grub_env_find (const char *name)
 
46
{
 
47
  struct grub_env_var *var;
 
48
  int idx = grub_env_hashval (name);
 
49
 
 
50
  /* Look for the variable in the current context.  */
 
51
  for (var = grub_current_context->vars[idx]; var; var = var->next)
 
52
    if (grub_strcmp (var->name, name) == 0)
 
53
      return var;
 
54
 
 
55
  return 0;
 
56
}
 
57
 
 
58
static void
 
59
grub_env_insert (struct grub_env_context *context,
 
60
                 struct grub_env_var *var)
 
61
{
 
62
  int idx = grub_env_hashval (var->name);
 
63
 
 
64
  /* Insert the variable into the hashtable.  */
 
65
  var->prevp = &context->vars[idx];
 
66
  var->next = context->vars[idx];
 
67
  if (var->next)
 
68
    var->next->prevp = &(var->next);
 
69
  context->vars[idx] = var;
 
70
}
 
71
 
 
72
static void
 
73
grub_env_remove (struct grub_env_var *var)
 
74
{
 
75
  /* Remove the entry from the variable table.  */
 
76
  *var->prevp = var->next;
 
77
  if (var->next)
 
78
    var->next->prevp = var->prevp;
 
79
}
 
80
 
 
81
grub_err_t
 
82
grub_env_set (const char *name, const char *val)
 
83
{
 
84
  struct grub_env_var *var;
 
85
 
 
86
  /* If the variable does already exist, just update the variable.  */
 
87
  var = grub_env_find (name);
 
88
  if (var)
 
89
    {
 
90
      char *old = var->value;
 
91
 
 
92
      if (var->write_hook)
 
93
        var->value = var->write_hook (var, val);
 
94
      else
 
95
        var->value = grub_strdup (val);
 
96
 
 
97
      if (! var->value)
 
98
        {
 
99
          var->value = old;
 
100
          return grub_errno;
 
101
        }
 
102
 
 
103
      grub_free (old);
 
104
      return GRUB_ERR_NONE;
 
105
    }
 
106
 
 
107
  /* The variable does not exist, so create a new one.  */
 
108
  var = grub_zalloc (sizeof (*var));
 
109
  if (! var)
 
110
    return grub_errno;
 
111
 
 
112
  /* This is not necessary. But leave this for readability.  */
 
113
  var->global = 0;
 
114
 
 
115
  var->name = grub_strdup (name);
 
116
  if (! var->name)
 
117
    goto fail;
 
118
 
 
119
  var->value = grub_strdup (val);
 
120
  if (! var->value)
 
121
    goto fail;
 
122
 
 
123
  grub_env_insert (grub_current_context, var);
 
124
 
 
125
  return GRUB_ERR_NONE;
 
126
 
 
127
 fail:
 
128
  grub_free (var->name);
 
129
  grub_free (var->value);
 
130
  grub_free (var);
 
131
 
 
132
  return grub_errno;
 
133
}
 
134
 
 
135
char *
 
136
grub_env_get (const char *name)
 
137
{
 
138
  struct grub_env_var *var;
 
139
 
 
140
  var = grub_env_find (name);
 
141
  if (! var)
 
142
    return 0;
 
143
 
 
144
  if (var->read_hook)
 
145
    return var->read_hook (var, var->value);
 
146
 
 
147
  return var->value;
 
148
}
 
149
 
 
150
void
 
151
grub_env_unset (const char *name)
 
152
{
 
153
  struct grub_env_var *var;
 
154
 
 
155
  var = grub_env_find (name);
 
156
  if (! var)
 
157
    return;
 
158
 
 
159
  if (var->read_hook || var->write_hook)
 
160
    {
 
161
      grub_env_set (name, "");
 
162
      return;
 
163
    }
 
164
 
 
165
  grub_env_remove (var);
 
166
 
 
167
  grub_free (var->name);
 
168
  grub_free (var->value);
 
169
  grub_free (var);
 
170
}
 
171
 
 
172
void
 
173
grub_env_iterate (int (*func) (struct grub_env_var *var))
 
174
{
 
175
  struct grub_env_sorted_var *sorted_list = 0;
 
176
  struct grub_env_sorted_var *sorted_var;
 
177
  int i;
 
178
 
 
179
  /* Add variables associated with this context into a sorted list.  */
 
180
  for (i = 0; i < HASHSZ; i++)
 
181
    {
 
182
      struct grub_env_var *var;
 
183
 
 
184
      for (var = grub_current_context->vars[i]; var; var = var->next)
 
185
        {
 
186
          struct grub_env_sorted_var *p, **q;
 
187
 
 
188
          sorted_var = grub_malloc (sizeof (*sorted_var));
 
189
          if (! sorted_var)
 
190
            goto fail;
 
191
 
 
192
          sorted_var->var = var;
 
193
 
 
194
          for (q = &sorted_list, p = *q; p; q = &((*q)->next), p = *q)
 
195
            {
 
196
              if (grub_strcmp (p->var->name, var->name) > 0)
 
197
                break;
 
198
            }
 
199
 
 
200
          sorted_var->next = *q;
 
201
          *q = sorted_var;
 
202
        }
 
203
    }
 
204
 
 
205
  /* Iterate FUNC on the sorted list.  */
 
206
  for (sorted_var = sorted_list; sorted_var; sorted_var = sorted_var->next)
 
207
    if (func (sorted_var->var))
 
208
      break;
 
209
 
 
210
 fail:
 
211
 
 
212
  /* Free the sorted list.  */
 
213
  for (sorted_var = sorted_list; sorted_var; )
 
214
    {
 
215
      struct grub_env_sorted_var *tmp = sorted_var->next;
 
216
 
 
217
      grub_free (sorted_var);
 
218
      sorted_var = tmp;
 
219
    }
 
220
}
 
221
 
 
222
grub_err_t
 
223
grub_register_variable_hook (const char *name,
 
224
                             grub_env_read_hook_t read_hook,
 
225
                             grub_env_write_hook_t write_hook)
 
226
{
 
227
  struct grub_env_var *var = grub_env_find (name);
 
228
 
 
229
  if (! var)
 
230
    {
 
231
      if (grub_env_set (name, "") != GRUB_ERR_NONE)
 
232
        return grub_errno;
 
233
 
 
234
      var = grub_env_find (name);
 
235
      /* XXX Insert an assertion?  */
 
236
    }
 
237
 
 
238
  var->read_hook = read_hook;
 
239
  var->write_hook = write_hook;
 
240
 
 
241
  return GRUB_ERR_NONE;
 
242
}