2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>.
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. */
25
#include <grub/file.h>
27
#include <grub/normal.h>
30
#include <grub/misc.h>
31
#include <grub/efiemu/efiemu.h>
32
#include <grub/machine/efiemu.h>
33
#include <grub/command.h>
34
#include <grub/i18n.h>
36
/* System table. Two version depending on mode */
37
grub_efi_system_table32_t *grub_efiemu_system_table32 = 0;
38
grub_efi_system_table64_t *grub_efiemu_system_table64 = 0;
39
/* Modules may need to execute some actions after memory allocation happens */
40
static struct grub_efiemu_prepare_hook *efiemu_prepare_hooks = 0;
41
/* Linked list of configuration tables */
42
static struct grub_efiemu_configuration_table *efiemu_config_tables = 0;
43
static int prepared = 0;
45
/* Free all allocated space */
47
grub_efiemu_unload (void)
49
struct grub_efiemu_configuration_table *cur, *d;
50
struct grub_efiemu_prepare_hook *curhook, *d2;
51
grub_efiemu_loadcore_unload ();
53
grub_efiemu_mm_unload ();
55
for (cur = efiemu_config_tables; cur;)
59
cur->unload (cur->data);
63
efiemu_config_tables = 0;
65
for (curhook = efiemu_prepare_hooks; curhook;)
69
curhook->unload (curhook->data);
73
efiemu_prepare_hooks = 0;
80
/* Remove previously registered table from the list */
82
grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid)
84
struct grub_efiemu_configuration_table *cur, *prev;
86
/* Special treating if head is to remove */
87
while (efiemu_config_tables
88
&& !grub_memcmp (&(efiemu_config_tables->guid), &guid, sizeof (guid)))
90
if (efiemu_config_tables->unload)
91
efiemu_config_tables->unload (efiemu_config_tables->data);
92
cur = efiemu_config_tables->next;
93
grub_free (efiemu_config_tables);
94
efiemu_config_tables = cur;
96
if (!efiemu_config_tables)
99
/* Remove from chain */
100
for (prev = efiemu_config_tables, cur = prev->next; cur;)
101
if (grub_memcmp (&(cur->guid), &guid, sizeof (guid)) == 0)
104
cur->unload (cur->data);
105
prev->next = cur->next;
114
return GRUB_ERR_NONE;
118
grub_efiemu_register_prepare_hook (grub_err_t (*hook) (void *data),
119
void (*unload) (void *data),
122
struct grub_efiemu_prepare_hook *nhook;
124
return grub_error (GRUB_ERR_BAD_ARGUMENT, "you must supply the hook");
125
nhook = (struct grub_efiemu_prepare_hook *) grub_malloc (sizeof (*nhook));
127
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't prepare hook");
129
nhook->unload = unload;
131
nhook->next = efiemu_prepare_hooks;
132
efiemu_prepare_hooks = nhook;
133
return GRUB_ERR_NONE;
136
/* Register a configuration table either supplying the address directly
140
grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
141
void * (*get_table) (void *data),
142
void (*unload) (void *data),
145
struct grub_efiemu_configuration_table *tbl;
148
if (! get_table && ! data)
149
return grub_error (GRUB_ERR_BAD_ARGUMENT,
150
"you must set at least get_table or data");
151
if ((err = grub_efiemu_unregister_configuration_table (guid)))
154
tbl = (struct grub_efiemu_configuration_table *) grub_malloc (sizeof (*tbl));
156
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't register table");
159
tbl->get_table = get_table;
160
tbl->unload = unload;
162
tbl->next = efiemu_config_tables;
163
efiemu_config_tables = tbl;
165
return GRUB_ERR_NONE;
169
grub_cmd_efiemu_unload (grub_command_t cmd __attribute__ ((unused)),
170
int argc __attribute__ ((unused)),
171
char *args[] __attribute__ ((unused)))
173
return grub_efiemu_unload ();
177
grub_cmd_efiemu_prepare (grub_command_t cmd __attribute__ ((unused)),
178
int argc __attribute__ ((unused)),
179
char *args[] __attribute__ ((unused)))
181
return grub_efiemu_prepare ();
188
grub_efiemu_exit_boot_services (grub_efi_uintn_t map_key
189
__attribute__ ((unused)))
191
/* Nothing to do here yet */
196
grub_efiemu_finish_boot_services (void)
198
/* Nothing to do here yet */
202
/* Load the runtime from the file FILENAME. */
204
grub_efiemu_load_file (const char *filename)
209
file = grub_file_open (filename);
213
err = grub_efiemu_mm_init ();
216
grub_file_close (file);
217
grub_efiemu_unload ();
218
return grub_error (grub_errno, "couldn't init memory management");
221
grub_dprintf ("efiemu", "mm initialized\n");
223
err = grub_efiemu_loadcore_init (file);
226
grub_file_close (file);
227
grub_efiemu_unload ();
231
grub_file_close (file);
233
/* For configuration tables entry in system table. */
234
grub_efiemu_request_symbols (1);
236
return GRUB_ERR_NONE;
240
grub_efiemu_autocore (void)
247
if (grub_efiemu_sizeof_uintn_t () != 0)
248
return GRUB_ERR_NONE;
250
prefix = grub_env_get ("prefix");
253
return grub_error (GRUB_ERR_FILE_NOT_FOUND,
254
"couldn't find efiemu core because prefix "
257
suffix = grub_efiemu_get_default_core_name ();
259
filename = grub_xasprintf ("%s/%s", prefix, suffix);
261
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
262
"couldn't allocate temporary space");
265
err = grub_efiemu_load_file (filename);
266
grub_free (filename);
269
#ifndef GRUB_MACHINE_EMU
270
err = grub_machine_efiemu_init_tables ();
275
return GRUB_ERR_NONE;
279
grub_efiemu_prepare (void)
284
return GRUB_ERR_NONE;
286
grub_dprintf ("efiemu", "Preparing %d-bit efiemu\n",
287
8 * grub_efiemu_sizeof_uintn_t ());
289
err = grub_efiemu_autocore ();
292
grub_efiemu_pnvram ();
296
if (grub_efiemu_sizeof_uintn_t () == 4)
297
return grub_efiemu_prepare32 (efiemu_prepare_hooks, efiemu_config_tables);
299
return grub_efiemu_prepare64 (efiemu_prepare_hooks, efiemu_config_tables);
304
grub_cmd_efiemu_load (grub_command_t cmd __attribute__ ((unused)),
305
int argc, char *args[])
309
grub_efiemu_unload ();
312
return grub_error (GRUB_ERR_BAD_ARGUMENT, "filename required");
314
err = grub_efiemu_load_file (args[0]);
317
#ifndef GRUB_MACHINE_EMU
318
err = grub_machine_efiemu_init_tables ();
322
return GRUB_ERR_NONE;
325
static grub_command_t cmd_loadcore, cmd_prepare, cmd_unload;
327
GRUB_MOD_INIT(efiemu)
329
cmd_loadcore = grub_register_command ("efiemu_loadcore",
330
grub_cmd_efiemu_load,
332
N_("Load and initialize EFI emulator."));
333
cmd_prepare = grub_register_command ("efiemu_prepare",
334
grub_cmd_efiemu_prepare,
336
N_("Finalize loading of EFI emulator."));
337
cmd_unload = grub_register_command ("efiemu_unload", grub_cmd_efiemu_unload,
339
N_("Unload EFI emulator."));
342
GRUB_MOD_FINI(efiemu)
344
grub_unregister_command (cmd_loadcore);
345
grub_unregister_command (cmd_prepare);
346
grub_unregister_command (cmd_unload);