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

« back to all changes in this revision

Viewing changes to grub-core/efiemu/main.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
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2009  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software: you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation, either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  GRUB is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
/* This is an emulation of EFI runtime services.
 
20
   This allows a more uniform boot on i386 machines.
 
21
   As it emulates only runtime service it isn't able
 
22
   to chainload EFI bootloader on non-EFI system. */
 
23
 
 
24
 
 
25
#include <grub/file.h>
 
26
#include <grub/err.h>
 
27
#include <grub/normal.h>
 
28
#include <grub/mm.h>
 
29
#include <grub/dl.h>
 
30
#include <grub/misc.h>
 
31
#include <grub/efiemu/efiemu.h>
 
32
#include <grub/command.h>
 
33
#include <grub/i18n.h>
 
34
 
 
35
/* System table. Two version depending on mode */
 
36
grub_efi_system_table32_t *grub_efiemu_system_table32 = 0;
 
37
grub_efi_system_table64_t *grub_efiemu_system_table64 = 0;
 
38
/* Modules may need to execute some actions after memory allocation happens */
 
39
static struct grub_efiemu_prepare_hook *efiemu_prepare_hooks = 0;
 
40
/* Linked list of configuration tables */
 
41
static struct grub_efiemu_configuration_table *efiemu_config_tables = 0;
 
42
static int prepared = 0;
 
43
 
 
44
/* Free all allocated space */
 
45
grub_err_t
 
46
grub_efiemu_unload (void)
 
47
{
 
48
  struct grub_efiemu_configuration_table *cur, *d;
 
49
  struct grub_efiemu_prepare_hook *curhook, *d2;
 
50
  grub_efiemu_loadcore_unload ();
 
51
 
 
52
  grub_efiemu_mm_unload ();
 
53
 
 
54
  for (cur = efiemu_config_tables; cur;)
 
55
    {
 
56
      d = cur->next;
 
57
      if (cur->unload)
 
58
        cur->unload (cur->data);
 
59
      grub_free (cur);
 
60
      cur = d;
 
61
    }
 
62
  efiemu_config_tables = 0;
 
63
 
 
64
  for (curhook = efiemu_prepare_hooks; curhook;)
 
65
    {
 
66
      d2 = curhook->next;
 
67
      if (curhook->unload)
 
68
        curhook->unload (curhook->data);
 
69
      grub_free (curhook);
 
70
      curhook = d2;
 
71
    }
 
72
  efiemu_prepare_hooks = 0;
 
73
 
 
74
  prepared = 0;
 
75
 
 
76
  return GRUB_ERR_NONE;
 
77
}
 
78
 
 
79
/* Remove previously registered table from the list */
 
80
grub_err_t
 
81
grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid)
 
82
{
 
83
  struct grub_efiemu_configuration_table *cur, *prev;
 
84
 
 
85
  /* Special treating if head is to remove */
 
86
  while (efiemu_config_tables
 
87
         && !grub_memcmp (&(efiemu_config_tables->guid), &guid, sizeof (guid)))
 
88
    {
 
89
      if (efiemu_config_tables->unload)
 
90
          efiemu_config_tables->unload (efiemu_config_tables->data);
 
91
        cur = efiemu_config_tables->next;
 
92
        grub_free (efiemu_config_tables);
 
93
        efiemu_config_tables = cur;
 
94
    }
 
95
  if (!efiemu_config_tables)
 
96
    return GRUB_ERR_NONE;
 
97
 
 
98
  /* Remove from chain */
 
99
  for (prev = efiemu_config_tables, cur = prev->next; cur;)
 
100
    if (grub_memcmp (&(cur->guid), &guid, sizeof (guid)) == 0)
 
101
      {
 
102
        if (cur->unload)
 
103
          cur->unload (cur->data);
 
104
        prev->next = cur->next;
 
105
        grub_free (cur);
 
106
        cur = prev->next;
 
107
      }
 
108
    else
 
109
      {
 
110
        prev = cur;
 
111
        cur = cur->next;
 
112
      }
 
113
  return GRUB_ERR_NONE;
 
114
}
 
115
 
 
116
grub_err_t
 
117
grub_efiemu_register_prepare_hook (grub_err_t (*hook) (void *data),
 
118
                                   void (*unload) (void *data),
 
119
                                   void *data)
 
120
{
 
121
  struct grub_efiemu_prepare_hook *nhook;
 
122
  if (! hook)
 
123
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "you must supply the hook");
 
124
  nhook = (struct grub_efiemu_prepare_hook *) grub_malloc (sizeof (*nhook));
 
125
  if (! nhook)
 
126
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't prepare hook");
 
127
  nhook->hook = hook;
 
128
  nhook->unload = unload;
 
129
  nhook->data = data;
 
130
  nhook->next = efiemu_prepare_hooks;
 
131
  efiemu_prepare_hooks = nhook;
 
132
  return GRUB_ERR_NONE;
 
133
}
 
134
 
 
135
/* Register a configuration table either supplying the address directly
 
136
   or with a hook
 
137
*/
 
138
grub_err_t
 
139
grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
 
140
                                          void * (*get_table) (void *data),
 
141
                                          void (*unload) (void *data),
 
142
                                          void *data)
 
143
{
 
144
  struct grub_efiemu_configuration_table *tbl;
 
145
  grub_err_t err;
 
146
 
 
147
 if (! get_table && ! data)
 
148
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
 
149
                       "you must set at least get_table or data");
 
150
  if ((err = grub_efiemu_unregister_configuration_table (guid)))
 
151
    return err;
 
152
 
 
153
  tbl = (struct grub_efiemu_configuration_table *) grub_malloc (sizeof (*tbl));
 
154
  if (! tbl)
 
155
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't register table");
 
156
 
 
157
  tbl->guid = guid;
 
158
  tbl->get_table = get_table;
 
159
  tbl->unload = unload;
 
160
  tbl->data = data;
 
161
  tbl->next = efiemu_config_tables;
 
162
  efiemu_config_tables = tbl;
 
163
 
 
164
  return GRUB_ERR_NONE;
 
165
}
 
166
 
 
167
static grub_err_t
 
168
grub_cmd_efiemu_unload (grub_command_t cmd __attribute__ ((unused)),
 
169
                        int argc __attribute__ ((unused)),
 
170
                        char *args[] __attribute__ ((unused)))
 
171
{
 
172
  return grub_efiemu_unload ();
 
173
}
 
174
 
 
175
static grub_err_t
 
176
grub_cmd_efiemu_prepare (grub_command_t cmd __attribute__ ((unused)),
 
177
                         int argc __attribute__ ((unused)),
 
178
                         char *args[] __attribute__ ((unused)))
 
179
{
 
180
  return grub_efiemu_prepare ();
 
181
}
 
182
 
 
183
 
 
184
 
 
185
/* Load the runtime from the file FILENAME.  */
 
186
static grub_err_t
 
187
grub_efiemu_load_file (const char *filename)
 
188
{
 
189
  grub_file_t file;
 
190
  grub_err_t err;
 
191
 
 
192
  file = grub_file_open (filename);
 
193
  if (! file)
 
194
    return 0;
 
195
 
 
196
  err = grub_efiemu_mm_init ();
 
197
  if (err)
 
198
    {
 
199
      grub_file_close (file);
 
200
      grub_efiemu_unload ();
 
201
      return grub_error (grub_errno, "couldn't init memory management");
 
202
    }
 
203
 
 
204
  grub_dprintf ("efiemu", "mm initialized\n");
 
205
 
 
206
  err = grub_efiemu_loadcore_init (file);
 
207
  if (err)
 
208
    {
 
209
      grub_file_close (file);
 
210
      grub_efiemu_unload ();
 
211
      return err;
 
212
    }
 
213
 
 
214
  grub_file_close (file);
 
215
 
 
216
  /* For configuration tables entry in system table. */
 
217
  grub_efiemu_request_symbols (1);
 
218
 
 
219
  return GRUB_ERR_NONE;
 
220
}
 
221
 
 
222
grub_err_t
 
223
grub_efiemu_autocore (void)
 
224
{
 
225
  const char *prefix;
 
226
  char *filename;
 
227
  char *suffix;
 
228
  grub_err_t err;
 
229
 
 
230
  if (grub_efiemu_sizeof_uintn_t () != 0)
 
231
    return GRUB_ERR_NONE;
 
232
 
 
233
  prefix = grub_env_get ("prefix");
 
234
 
 
235
  if (! prefix)
 
236
    return grub_error (GRUB_ERR_FILE_NOT_FOUND,
 
237
                       "couldn't find efiemu core because prefix "
 
238
                       "isn't set");
 
239
 
 
240
  suffix = grub_efiemu_get_default_core_name ();
 
241
 
 
242
  filename = grub_xasprintf ("%s/%s", prefix, suffix);
 
243
  if (! filename)
 
244
    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
 
245
                       "couldn't allocate temporary space");
 
246
 
 
247
 
 
248
  err = grub_efiemu_load_file (filename);
 
249
  grub_free (filename);
 
250
  if (err)
 
251
    return err;
 
252
#ifndef GRUB_MACHINE_EMU
 
253
  err = grub_machine_efiemu_init_tables ();
 
254
  if (err)
 
255
    return err;
 
256
#endif
 
257
 
 
258
  return GRUB_ERR_NONE;
 
259
}
 
260
 
 
261
grub_err_t
 
262
grub_efiemu_prepare (void)
 
263
{
 
264
  grub_err_t err;
 
265
 
 
266
  if (prepared)
 
267
    return GRUB_ERR_NONE;
 
268
 
 
269
  err = grub_efiemu_autocore ();
 
270
  if (err)
 
271
    return err;
 
272
 
 
273
  grub_dprintf ("efiemu", "Preparing %d-bit efiemu\n",
 
274
                8 * grub_efiemu_sizeof_uintn_t ());
 
275
 
 
276
  /* Create NVRAM. */
 
277
  grub_efiemu_pnvram ();
 
278
 
 
279
  prepared = 1;
 
280
 
 
281
  if (grub_efiemu_sizeof_uintn_t () == 4)
 
282
    return grub_efiemu_prepare32 (efiemu_prepare_hooks, efiemu_config_tables);
 
283
  else
 
284
    return grub_efiemu_prepare64 (efiemu_prepare_hooks, efiemu_config_tables);
 
285
}
 
286
 
 
287
 
 
288
static grub_err_t
 
289
grub_cmd_efiemu_load (grub_command_t cmd __attribute__ ((unused)),
 
290
                      int argc, char *args[])
 
291
{
 
292
  grub_err_t err;
 
293
 
 
294
  grub_efiemu_unload ();
 
295
 
 
296
  if (argc != 1)
 
297
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "filename required");
 
298
 
 
299
  err = grub_efiemu_load_file (args[0]);
 
300
  if (err)
 
301
    return err;
 
302
#ifndef GRUB_MACHINE_EMU
 
303
  err = grub_machine_efiemu_init_tables ();
 
304
  if (err)
 
305
    return err;
 
306
#endif
 
307
  return GRUB_ERR_NONE;
 
308
}
 
309
 
 
310
static grub_command_t cmd_loadcore, cmd_prepare, cmd_unload;
 
311
 
 
312
GRUB_MOD_INIT(efiemu)
 
313
{
 
314
  cmd_loadcore = grub_register_command ("efiemu_loadcore",
 
315
                                        grub_cmd_efiemu_load,
 
316
                                        N_("FILE"),
 
317
                                        N_("Load and initialize EFI emulator."));
 
318
  cmd_prepare = grub_register_command ("efiemu_prepare",
 
319
                                       grub_cmd_efiemu_prepare,
 
320
                                       0,
 
321
                                       N_("Finalize loading of EFI emulator."));
 
322
  cmd_unload = grub_register_command ("efiemu_unload", grub_cmd_efiemu_unload,
 
323
                                      0,
 
324
                                      N_("Unload EFI emulator."));
 
325
}
 
326
 
 
327
GRUB_MOD_FINI(efiemu)
 
328
{
 
329
  grub_unregister_command (cmd_loadcore);
 
330
  grub_unregister_command (cmd_prepare);
 
331
  grub_unregister_command (cmd_unload);
 
332
}