grub/grub-core/efiemu/main.c

329 lines
7.9 KiB
C
Raw Normal View History

/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/* This is an emulation of EFI runtime services.
2009-06-10 21:04:23 +00:00
This allows a more uniform boot on i386 machines.
As it emulates only runtime service it isn't able
to chainload EFI bootloader on non-EFI system. */
#include <grub/file.h>
#include <grub/err.h>
#include <grub/normal.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/efiemu/efiemu.h>
#include <grub/command.h>
#include <grub/i18n.h>
GRUB_MOD_LICENSE ("GPLv3+");
/* System table. Two version depending on mode */
grub_efi_system_table32_t *grub_efiemu_system_table32 = 0;
grub_efi_system_table64_t *grub_efiemu_system_table64 = 0;
/* Modules may need to execute some actions after memory allocation happens */
static struct grub_efiemu_prepare_hook *efiemu_prepare_hooks = 0;
/* Linked list of configuration tables */
static struct grub_efiemu_configuration_table *efiemu_config_tables = 0;
2009-11-09 17:43:53 +00:00
static int prepared = 0;
/* Free all allocated space */
grub_err_t
grub_efiemu_unload (void)
{
struct grub_efiemu_configuration_table *cur, *d;
struct grub_efiemu_prepare_hook *curhook, *d2;
grub_efiemu_loadcore_unload ();
grub_efiemu_mm_unload ();
for (cur = efiemu_config_tables; cur;)
{
d = cur->next;
if (cur->unload)
cur->unload (cur->data);
grub_free (cur);
cur = d;
}
efiemu_config_tables = 0;
for (curhook = efiemu_prepare_hooks; curhook;)
{
d2 = curhook->next;
if (curhook->unload)
curhook->unload (curhook->data);
grub_free (curhook);
curhook = d2;
}
efiemu_prepare_hooks = 0;
2009-11-09 17:43:53 +00:00
prepared = 0;
return GRUB_ERR_NONE;
}
/* Remove previously registered table from the list */
grub_err_t
grub_efiemu_unregister_configuration_table (grub_efi_guid_t guid)
{
struct grub_efiemu_configuration_table *cur, *prev;
/* Special treating if head is to remove */
while (efiemu_config_tables
&& !grub_memcmp (&(efiemu_config_tables->guid), &guid, sizeof (guid)))
{
if (efiemu_config_tables->unload)
efiemu_config_tables->unload (efiemu_config_tables->data);
cur = efiemu_config_tables->next;
grub_free (efiemu_config_tables);
efiemu_config_tables = cur;
}
if (!efiemu_config_tables)
return GRUB_ERR_NONE;
/* Remove from chain */
for (prev = efiemu_config_tables, cur = prev->next; cur;)
if (grub_memcmp (&(cur->guid), &guid, sizeof (guid)) == 0)
{
if (cur->unload)
cur->unload (cur->data);
prev->next = cur->next;
grub_free (cur);
cur = prev->next;
}
else
{
prev = cur;
cur = cur->next;
}
return GRUB_ERR_NONE;
}
grub_err_t
grub_efiemu_register_prepare_hook (grub_err_t (*hook) (void *data),
2009-06-10 21:04:23 +00:00
void (*unload) (void *data),
void *data)
{
struct grub_efiemu_prepare_hook *nhook;
nhook = (struct grub_efiemu_prepare_hook *) grub_malloc (sizeof (*nhook));
if (! nhook)
return grub_errno;
nhook->hook = hook;
nhook->unload = unload;
nhook->data = data;
nhook->next = efiemu_prepare_hooks;
efiemu_prepare_hooks = nhook;
return GRUB_ERR_NONE;
}
2009-06-10 21:04:23 +00:00
/* Register a configuration table either supplying the address directly
or with a hook
*/
grub_err_t
2009-06-10 21:04:23 +00:00
grub_efiemu_register_configuration_table (grub_efi_guid_t guid,
void * (*get_table) (void *data),
2009-06-10 21:04:23 +00:00
void (*unload) (void *data),
void *data)
{
struct grub_efiemu_configuration_table *tbl;
grub_err_t err;
2009-06-10 21:04:23 +00:00
err = grub_efiemu_unregister_configuration_table (guid);
if (err)
return err;
tbl = (struct grub_efiemu_configuration_table *) grub_malloc (sizeof (*tbl));
if (! tbl)
return grub_errno;
tbl->guid = guid;
tbl->get_table = get_table;
tbl->unload = unload;
tbl->data = data;
tbl->next = efiemu_config_tables;
efiemu_config_tables = tbl;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_efiemu_unload (grub_command_t cmd __attribute__ ((unused)),
2009-06-10 21:04:23 +00:00
int argc __attribute__ ((unused)),
char *args[] __attribute__ ((unused)))
{
return grub_efiemu_unload ();
}
static grub_err_t
grub_cmd_efiemu_prepare (grub_command_t cmd __attribute__ ((unused)),
2009-06-10 21:04:23 +00:00
int argc __attribute__ ((unused)),
char *args[] __attribute__ ((unused)))
{
return grub_efiemu_prepare ();
}
/* Load the runtime from the file FILENAME. */
static grub_err_t
grub_efiemu_load_file (const char *filename)
{
grub_file_t file;
grub_err_t err;
2009-06-10 21:04:23 +00:00
file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE);
if (! file)
return grub_errno;
2009-06-10 21:04:23 +00:00
err = grub_efiemu_mm_init ();
if (err)
{
grub_file_close (file);
grub_efiemu_unload ();
return err;
}
grub_dprintf ("efiemu", "mm initialized\n");
err = grub_efiemu_loadcore_init (file, filename);
if (err)
{
grub_file_close (file);
grub_efiemu_unload ();
return err;
}
grub_file_close (file);
/* For configuration tables entry in system table. */
grub_efiemu_request_symbols (1);
return GRUB_ERR_NONE;
}
grub_err_t
grub_efiemu_autocore (void)
{
const char *prefix;
char *filename;
Add missing const qualifiers. * grub-core/commands/i386/pc/sendkey.c (keysym): Add missing const. * grub-core/commands/lspci.c (grub_pci_classname): Likewise. * grub-core/commands/menuentry.c (hotkey_aliases): Likewise. * grub-core/disk/lvm.c (grub_lvm_getvalue): Likewise. (grub_lvm_check_flag): Likewise. * grub-core/efiemu/i386/coredetect.c (grub_efiemu_get_default_core_name): Likewise * grub-core/efiemu/main.c (grub_efiemu_autocore): Likewise. * grub-core/fs/hfsplus.c (grub_hfsplus_catkey_internal): Likewise. * grub-core/fs/ntfs.c (fixup): Likewise. * grub-core/fs/xfs.c (grub_xfs_iterate_dir): Likewise. * grub-core/fs/zfs/zfs.c (decomp_entry): Likewise. (fzap_lookup): Likewise. (zap_lookup): Likewise. * grub-core/gnulib/regcomp.c (init_dfa): Likewise. * grub-core/lib/legacy_parse.c (check_option): Likewise. * grub-core/lib/posix_wrap/langinfo.h (nl_langinfo): Likewise. * grub-core/loader/i386/bsd.c (grub_bsd_add_meta): Likewise. (grub_freebsd_add_meta_module): Likewise. (grub_cmd_freebsd_module): Likewise. * grub-core/loader/i386/xnu.c (tbl_alias): Likewise. * grub-core/loader/xnu.c (grub_xnu_register_memory): Likewise. (grub_xnu_writetree_get_size): Likewise. (grub_xnu_writetree_toheap_real): Likewise. (grub_xnu_find_key): Likewise. (grub_xnu_create_key): Likewise. (grub_xnu_create_value): Likewise. (grub_xnu_register_memory): Likewise. (grub_xnu_check_os_bundle_required): Likewise. (grub_xnu_scan_dir_for_kexts): Likewise. (grub_xnu_load_kext_from_dir): Likewise. * grub-core/normal/color.c (color_list): Likewise. * grub-core/normal/completion.c (current_word): Likewise. * grub-core/normal/menu_entry.c (insert_string): Likewise. * grub-core/term/serial.c (grub_serial_find): Likewise. * grub-core/term/tparm.c (grub_terminfo_tparm): Likewise. * include/grub/efiemu/efiemu.h (grub_efiemu_get_default_core_name): Likewise. * include/grub/i386/bsd.h (grub_bsd_add_meta): Likewise. (grub_freebsd_add_meta_module): Likewise. * include/grub/lib/arg.h (grub_arg_option): Likewise. * include/grub/net.h (grub_net_card_driver): Likewise. (grub_net_card): Likewise. (grub_net_app_protocol): Likewise. * include/grub/parttool.h (grub_parttool_argdesc): Likewise. * include/grub/serial.h (grub_serial_find): Likewise. * include/grub/tparm.h (grub_terminfo_tparm): Likewise. * include/grub/xnu.h (grub_xnu_create_key): Likewise. (grub_xnu_create_value): Likewise. (grub_xnu_find_key): Likewise. (grub_xnu_scan_dir_for_kexts): Likewise. (grub_xnu_load_kext_from_dir): Likewise. * include/grub/zfs/zio_checksum.h (zio_checksum_t): Moved from here ... * grub-core/fs/zfs/zfs.c (zio_checksum_t): ...here. * include/grub/zfs/zio_checksum.h (zio_checksum_info): Moved from here ... * grub-core/fs/zfs/zfs.c (zio_checksum_info): ... here. Added missing const.
2011-11-30 15:20:13 +00:00
const char *suffix;
grub_err_t err;
if (grub_efiemu_sizeof_uintn_t () != 0)
return GRUB_ERR_NONE;
prefix = grub_env_get ("prefix");
2009-06-10 21:04:23 +00:00
if (! prefix)
2009-06-10 21:04:23 +00:00
return grub_error (GRUB_ERR_FILE_NOT_FOUND,
N_("variable `%s' isn't set"), "prefix");
2009-06-10 21:04:23 +00:00
suffix = grub_efiemu_get_default_core_name ();
2009-06-10 21:04:23 +00:00
filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s",
prefix, suffix);
if (! filename)
return grub_errno;
2009-06-10 21:04:23 +00:00
err = grub_efiemu_load_file (filename);
grub_free (filename);
if (err)
return err;
#ifndef GRUB_MACHINE_EMU
err = grub_machine_efiemu_init_tables ();
if (err)
return err;
#endif
return GRUB_ERR_NONE;
}
grub_err_t
grub_efiemu_prepare (void)
{
grub_err_t err;
2009-11-09 17:43:53 +00:00
if (prepared)
return GRUB_ERR_NONE;
err = grub_efiemu_autocore ();
if (err)
return err;
2009-06-10 21:04:23 +00:00
grub_dprintf ("efiemu", "Preparing %d-bit efiemu\n",
8 * grub_efiemu_sizeof_uintn_t ());
2009-11-09 17:43:53 +00:00
/* Create NVRAM. */
grub_efiemu_pnvram ();
2009-11-09 17:43:53 +00:00
prepared = 1;
if (grub_efiemu_sizeof_uintn_t () == 4)
return grub_efiemu_prepare32 (efiemu_prepare_hooks, efiemu_config_tables);
else
return grub_efiemu_prepare64 (efiemu_prepare_hooks, efiemu_config_tables);
}
static grub_err_t
grub_cmd_efiemu_load (grub_command_t cmd __attribute__ ((unused)),
int argc, char *args[])
{
grub_err_t err;
grub_efiemu_unload ();
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
err = grub_efiemu_load_file (args[0]);
if (err)
return err;
#ifndef GRUB_MACHINE_EMU
err = grub_machine_efiemu_init_tables ();
if (err)
return err;
#endif
return GRUB_ERR_NONE;
}
static grub_command_t cmd_loadcore, cmd_prepare, cmd_unload;
GRUB_MOD_INIT(efiemu)
{
2009-06-10 21:04:23 +00:00
cmd_loadcore = grub_register_command ("efiemu_loadcore",
grub_cmd_efiemu_load,
N_("FILE"),
N_("Load and initialize EFI emulator."));
2009-06-10 21:04:23 +00:00
cmd_prepare = grub_register_command ("efiemu_prepare",
grub_cmd_efiemu_prepare,
2009-12-26 Carles Pina i Estany <carles@pina.cat> * commands/help.c (grub_cmd_help): Print the command name before the summary. (GRUB_MOD_INIT): Remove command name from the summary. * kern/command.c (GRUB_MOD_INIT): If summary is null assign an empty strig as summary. * lib/arg.c (find_long): Print the command name before the summary. * commands/acpi.c (GRUB_MOD_INIT): Remove command name from the summary. * commands/blocklist.c (GRUB_MOD_INIT): Likewise. * commands/cat.c (GRUB_MOD_INIT): Likewise. * commands/cmp.c (GRUB_MOD_INIT): Likewise. * commands/configfile.c (GRUB_MOD_INIT): Likewise. * commands/crc.c (GRUB_MOD_INIT): Likewise. * commands/date.c (GRUB_MOD_INIT): Likewise. * commands/echo.c (GRUB_MOD_INIT): Likewise. * commands/efi/loadbios.c (GRUB_MOD_INIT): Likewise. * commands/gptsync.c (GRUB_MOD_INIT): Likewise. * commands/handler.c (GRUB_MOD_INIT): Likewise. * commands/hdparm.c (GRUB_MOD_INIT): Likewise. * commands/hexdump.c (GRUB_MOD_INIT): Likewise. * commands/i386/cpuid.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/halt.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/play.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/pxecmd.c (GRUB_MOD_INIT): Likewise. * commands/keystatus.c (GRUB_MOD_INIT): Likewise. * commands/loadenv.c (GRUB_MOD_INIT): Likewise. * commands/ls.c (GRUB_MOD_INIT): Likewise. * commands/lspci.c (GRUB_MOD_INIT): Likewise. * commands/memrw.c (GRUB_MOD_INIT): Likewise. * commands/minicmd.c (GRUB_MOD_INIT): Likewise. * commands/parttool.c (GRUB_MOD_INIT): Likewise. * commands/password.c (GRUB_MOD_INIT): Likewise. * commands/probe.c (GRUB_MOD_INIT): Likewise. * commands/read.c (GRUB_MOD_INIT): Likewise. * commands/search.c (GRUB_MOD_INIT): Likewise. * commands/sleep.c (GRUB_MOD_INIT): Likewise. * commands/test.c (GRUB_MOD_INIT): Likewise. * commands/xnu_uuid.c (GRUB_MOD_INIT): Likewise. * efiemu/main.c (GRUB_MOD_INIT): Likewise. * font/font_cmd.c (GRUB_MOD_INIT): Likewise. * gettext/gettext.c (GRUB_MOD_INIT): Likewise. * kern/corecmd.c (GRUB_MOD_INIT): Likewise. * lib/arg.c (GRUB_MOD_INIT): Likewise. * loader/efi/appleloader.c (GRUB_MOD_INIT): Likewise. * loader/i386/bsd.c (GRUB_MOD_INIT): Likewise. * loader/xnu.c (GRUB_MOD_INIT): Likewise. * mmap/mmap.c (GRUB_MOD_INIT): Likewise. * term/terminfo.c (GRUB_MOD_INIT): Likewise. * video/readers/jpeg.c (GRUB_MOD_INIT): Likewise. * video/readers/png.c (GRUB_MOD_INIT): Likewise. * video/readers/tga.c (GRUB_MOD_INIT): Likewise.
2009-12-25 23:50:59 +00:00
0,
N_("Finalize loading of EFI emulator."));
2009-06-10 21:04:23 +00:00
cmd_unload = grub_register_command ("efiemu_unload", grub_cmd_efiemu_unload,
2009-12-26 Carles Pina i Estany <carles@pina.cat> * commands/help.c (grub_cmd_help): Print the command name before the summary. (GRUB_MOD_INIT): Remove command name from the summary. * kern/command.c (GRUB_MOD_INIT): If summary is null assign an empty strig as summary. * lib/arg.c (find_long): Print the command name before the summary. * commands/acpi.c (GRUB_MOD_INIT): Remove command name from the summary. * commands/blocklist.c (GRUB_MOD_INIT): Likewise. * commands/cat.c (GRUB_MOD_INIT): Likewise. * commands/cmp.c (GRUB_MOD_INIT): Likewise. * commands/configfile.c (GRUB_MOD_INIT): Likewise. * commands/crc.c (GRUB_MOD_INIT): Likewise. * commands/date.c (GRUB_MOD_INIT): Likewise. * commands/echo.c (GRUB_MOD_INIT): Likewise. * commands/efi/loadbios.c (GRUB_MOD_INIT): Likewise. * commands/gptsync.c (GRUB_MOD_INIT): Likewise. * commands/handler.c (GRUB_MOD_INIT): Likewise. * commands/hdparm.c (GRUB_MOD_INIT): Likewise. * commands/hexdump.c (GRUB_MOD_INIT): Likewise. * commands/i386/cpuid.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/halt.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/play.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/pxecmd.c (GRUB_MOD_INIT): Likewise. * commands/keystatus.c (GRUB_MOD_INIT): Likewise. * commands/loadenv.c (GRUB_MOD_INIT): Likewise. * commands/ls.c (GRUB_MOD_INIT): Likewise. * commands/lspci.c (GRUB_MOD_INIT): Likewise. * commands/memrw.c (GRUB_MOD_INIT): Likewise. * commands/minicmd.c (GRUB_MOD_INIT): Likewise. * commands/parttool.c (GRUB_MOD_INIT): Likewise. * commands/password.c (GRUB_MOD_INIT): Likewise. * commands/probe.c (GRUB_MOD_INIT): Likewise. * commands/read.c (GRUB_MOD_INIT): Likewise. * commands/search.c (GRUB_MOD_INIT): Likewise. * commands/sleep.c (GRUB_MOD_INIT): Likewise. * commands/test.c (GRUB_MOD_INIT): Likewise. * commands/xnu_uuid.c (GRUB_MOD_INIT): Likewise. * efiemu/main.c (GRUB_MOD_INIT): Likewise. * font/font_cmd.c (GRUB_MOD_INIT): Likewise. * gettext/gettext.c (GRUB_MOD_INIT): Likewise. * kern/corecmd.c (GRUB_MOD_INIT): Likewise. * lib/arg.c (GRUB_MOD_INIT): Likewise. * loader/efi/appleloader.c (GRUB_MOD_INIT): Likewise. * loader/i386/bsd.c (GRUB_MOD_INIT): Likewise. * loader/xnu.c (GRUB_MOD_INIT): Likewise. * mmap/mmap.c (GRUB_MOD_INIT): Likewise. * term/terminfo.c (GRUB_MOD_INIT): Likewise. * video/readers/jpeg.c (GRUB_MOD_INIT): Likewise. * video/readers/png.c (GRUB_MOD_INIT): Likewise. * video/readers/tga.c (GRUB_MOD_INIT): Likewise.
2009-12-25 23:50:59 +00:00
0,
N_("Unload EFI emulator."));
}
GRUB_MOD_FINI(efiemu)
{
grub_unregister_command (cmd_loadcore);
grub_unregister_command (cmd_prepare);
grub_unregister_command (cmd_unload);
}