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

« back to all changes in this revision

Viewing changes to kern/env.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
 
/* 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
 
}