d558e6b5ac
* conf/common.rmk (grub_script.tab.c): Change normal/parser.y to script/sh/parser.y. (pkglib_MODULES): Add normal.mod and sh.mod. (normal_SOURCES): New variable. (normal_mod_CFLAGS): Likewise. (normal_mod_LDFLAGS): Likewise. (sh_mod_SOURCES): Likewise. (sh_mod_CFLAGS): Likewise. (sh_mod_LDFLAGS): Likewise. * conf/i386-pc.rmk (normal/lexer.c_DEPENDENCIES): Changed to script/sh/lexer.c_DEPENDENCIES. (kernel_img_SOURCES): Remove kern/rescue.c, and kern/reader.c, kern/rescue_reader.c and kern/rescue_parser.c. (kernel_img_HEADERS): Remove rescue.h, add reader.h. (grub_emu_SOURCES): Change source files. (pkglib_MODULES): Remove normal.mod. (normal_SOURCES): Removed. (normal_mod_CFLAGS): Likewise. (normal_mod_LDFLAGS): Likewise. * conf/i386-coreboot.rmk: Likewise. * conf/i386-efi.rmk: Likewise. * conf/i386-ieee1276.rmk: Likewise. * conf/powerpc-ieee1275.rmk: Likewise. * conf/sparc64-ieee1275.rmk: Likewise. * conf/x86_64-efi.rmk: Likewise. * include/grub/command.h (grub_command_execute): New inline function. * include/grub/menu.h (grub_menu_entry): Removed commands field. * include/grub/normal.h: Remove <grub/setjmp.h>. (grub_fs_module_list): Moved to normal/autofs.c. (grub_exit_env): Removed. (grub_command_execute): Likewise. (grub_normal_menu_addentry): Renamed to grub_menu_addentry, removed parameter script. (read_command_list): New function declaration. (read_fs_list): Likewise. * include/parser.h: Include <grub/reader.h>. (grub_parser_split_cmdline): Change type of getline parameter. (grub_parser): New structure. (grub_parser_class): New variable. (grub_parser_execute): New function declaration. (grub_register_rescue_parser): Likewise. (grub_parser_register): New inline function. (grub_parser_unregister): Likewise. (grub_parser_get_current): Likewise. (grub_parser_set_current): Likewise. * include/grub/reader.h: New file. * kern/reader.c: Likewise. * kern/rescue_parser.c: Likewise. * kern/rescue_reader.c: Likewise. * normal/autofs.c: Likewise. * normal/dyncmd.c: Likewise. * include/grub/rescue.h: Removed. * normal/command.h: Likewise. * include/grub/script.h: Moved to ... * include/grub/script_sh.h: ... Moved here. * normal/execute.c: Moved to ... * script/sh/execute.c: ... Moved here. * normal/function.c: Moved to ... * script/sh/function.c: ... Moved here. * normal/lexer.c: Moved to ... * script/sh/lexer.c: ... Moved here. * normal/parser.y: Moved to ... * script/sh/parser.y: ... Moved here. * normal/script.c: Moved to ... * script/sh/script.c: ... Moved here. * normal/main.c: Remove <grub/rescue.h> and <grub/script.h>, include <grub/reader.h>. (grub_exit_env): Removed. (fs_module_list): Moved to normal/autofs.c. (grub_file_getline): Don't handle comment here. (free_menu): Skip removed field entry->commands. (grub_normal_menu_addentry): Removed as grub_menu_entry, removed script parameter. (read_config_file): Removed nested parameter, change getline function. (grub_enter_normal_mode): Removed. (grub_dyncmd_dispatcher): Moved to normal/dyncmd.c. (read_command_list): Likewise. (autoload_fs_module): Moved to normal/autofs.c. (read_fs_list): Likewise. (reader_nested): New variable. (grub_normal_execute): Run parser.sh to switch to sh parser. (grub_cmd_rescue): Removed. (cmd_normal): Removed. (grub_cmd_normal): Unregister itself at the beginning. Don't register rescue command. (grub_cmdline_run): New function. (grub_normal_reader_init): Likewise. (grub_normal_read_line): Likewise. (grub_env_write_pager): Likewise. (cmdline): New variable. (grub_normal_reader): Likewise. (GRUB_MOD_INIT): Register normal reader and set as current, register pager hook, register normal command with grub_register_command_prio, so that it won't show up in command.lst. (GRUB_MOD_FINI): Unregister normal reader, unhook pager, clear grub_fs_autoload_hook. * normal/menu.c: Remove <grub/script.h>, add <grub/command.h>. (grub_menu_execute_entry): Replace grub_script_execute with grub_parser_execute, change parameter to grub_command_execute. * normal/menu_text.c: Remove <grub/script.h>. * normal/menu_entry.c: Remove <grub/script.h>, add <grub/command.h> and <grub/parser.h>. (run): Change editor_getline to use new parser interface. Change parameter to grub_command_execute. * kern/main.c: Remove <grub/rescue.h>, include <grub/command.h>, <grub/reader.h> and <grub/parser.h>. (grub_load_normal_mode): Execute normal command. (grub_main): Call grub_register_core_commands, grub_register_rescue_parser and grub_register_rescue_reader, use grub_reader_loop to enter input loop. * kern/parser.c (grub_parser_spli_cmdline): Change type of getline parameter. (grub_parser_class): New variable. (grub_parser_execute): New function. * loader/i386/multiboot.c: Remove <grub/rescue.h>. * loader/multiboot2.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * util/grub-emu.c (read_command_list): New dummy function.
460 lines
10 KiB
C
460 lines
10 KiB
C
/* multiboot2.c - boot a multiboot 2 OS image. */
|
|
/*
|
|
* GRUB -- GRand Unified Bootloader
|
|
* Copyright (C) 2007 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/>.
|
|
*/
|
|
|
|
#include <multiboot2.h>
|
|
#include <grub/loader.h>
|
|
#include <grub/machine/loader.h>
|
|
#include <grub/multiboot2.h>
|
|
#include <grub/elfload.h>
|
|
#include <grub/file.h>
|
|
#include <grub/err.h>
|
|
#include <grub/dl.h>
|
|
#include <grub/mm.h>
|
|
#include <grub/misc.h>
|
|
#include <grub/gzio.h>
|
|
|
|
static grub_addr_t entry;
|
|
extern grub_dl_t my_mod;
|
|
|
|
static char *grub_mb2_tags;
|
|
static char *grub_mb2_tags_pos;
|
|
static grub_size_t grub_mb2_tags_len;
|
|
static int grub_mb2_tags_count;
|
|
|
|
static void
|
|
grub_mb2_tags_free (void)
|
|
{
|
|
grub_dprintf ("loader", "Freeing all tags...\n");
|
|
grub_free (grub_mb2_tags);
|
|
grub_mb2_tags = 0;
|
|
grub_mb2_tags_pos = 0;
|
|
grub_mb2_tags_len = 0;
|
|
grub_mb2_tags_count = 0;
|
|
}
|
|
|
|
grub_err_t
|
|
grub_mb2_tag_alloc (grub_addr_t *addr, int key, grub_size_t len)
|
|
{
|
|
struct multiboot_tag_header *tag;
|
|
grub_size_t used;
|
|
grub_size_t needed;
|
|
|
|
grub_dprintf ("loader", "Allocating tag: key 0x%x, size 0x%lx.\n",
|
|
key, (unsigned long) len);
|
|
|
|
used = grub_mb2_tags_pos - grub_mb2_tags;
|
|
len = ALIGN_UP (len, sizeof (multiboot_word));
|
|
|
|
needed = used + len;
|
|
|
|
if (needed > grub_mb2_tags_len)
|
|
{
|
|
/* Allocate new buffer. */
|
|
grub_size_t newsize = needed * 2;
|
|
char *newarea;
|
|
|
|
grub_dprintf ("loader", "Reallocating tag buffer (new size 0x%lx).\n",
|
|
(unsigned long) newsize);
|
|
|
|
newarea = grub_malloc (newsize);
|
|
if (! newarea)
|
|
return grub_errno;
|
|
grub_memcpy (newarea, grub_mb2_tags, grub_mb2_tags_len);
|
|
grub_free (grub_mb2_tags);
|
|
|
|
grub_mb2_tags_len = newsize;
|
|
grub_mb2_tags = newarea;
|
|
grub_mb2_tags_pos = newarea + used;
|
|
}
|
|
|
|
tag = (struct multiboot_tag_header *) grub_mb2_tags_pos;
|
|
grub_mb2_tags_pos += len;
|
|
|
|
tag->key = key;
|
|
tag->len = len;
|
|
|
|
if (addr)
|
|
*addr = (grub_addr_t) tag;
|
|
|
|
grub_mb2_tags_count++;
|
|
|
|
grub_dprintf ("loader", "Allocated tag %u at %p.\n", grub_mb2_tags_count, tag);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_tag_start_create (void)
|
|
{
|
|
return grub_mb2_tag_alloc (0, MULTIBOOT2_TAG_START,
|
|
sizeof (struct multiboot_tag_start));
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_tag_name_create (void)
|
|
{
|
|
struct multiboot_tag_name *name;
|
|
grub_addr_t name_addr;
|
|
grub_err_t err;
|
|
const char *grub_version = PACKAGE_STRING;
|
|
|
|
err = grub_mb2_tag_alloc (&name_addr, MULTIBOOT2_TAG_NAME,
|
|
sizeof (struct multiboot_tag_name) +
|
|
sizeof (grub_version) + 1);
|
|
if (err)
|
|
return err;
|
|
|
|
name = (struct multiboot_tag_name *) name_addr;
|
|
grub_strcpy (name->name, grub_version);
|
|
|
|
return GRUB_ERR_NONE;
|
|
}
|
|
|
|
typedef grub_err_t (*tag_create_t) (void);
|
|
static tag_create_t grub_mb2_tag_creators[] = {
|
|
grub_mb2_tag_start_create,
|
|
grub_mb2_tag_name_create,
|
|
grub_mb2_tags_arch_create,
|
|
0,
|
|
};
|
|
|
|
static grub_err_t
|
|
grub_mb2_tags_create (void)
|
|
{
|
|
tag_create_t *creator;
|
|
grub_err_t err;
|
|
|
|
for (creator = grub_mb2_tag_creators; *creator != 0; creator++)
|
|
{
|
|
err = (*creator) ();
|
|
if (err)
|
|
goto error;
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
error:
|
|
grub_error_push ();
|
|
grub_mb2_tags_free ();
|
|
grub_error_pop ();
|
|
return err;
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_tags_finish (void)
|
|
{
|
|
struct multiboot_tag_start *start;
|
|
grub_err_t err;
|
|
|
|
/* Create the `end' tag. */
|
|
err = grub_mb2_tag_alloc (0, MULTIBOOT2_TAG_END,
|
|
sizeof (struct multiboot_tag_end));
|
|
if (err)
|
|
goto error;
|
|
|
|
/* We created the `start' tag first. Update it now. */
|
|
start = (struct multiboot_tag_start *) grub_mb2_tags;
|
|
start->size = grub_mb2_tags_pos - grub_mb2_tags;
|
|
return GRUB_ERR_NONE;
|
|
|
|
error:
|
|
grub_error_push ();
|
|
grub_mb2_tags_free ();
|
|
grub_error_pop ();
|
|
return err;
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_boot (void)
|
|
{
|
|
grub_mb2_tags_finish ();
|
|
|
|
grub_dprintf ("loader", "Tags at %p\n", grub_mb2_tags);
|
|
grub_mb2_arch_boot (entry, grub_mb2_tags);
|
|
|
|
/* Not reached. */
|
|
return GRUB_ERR_NONE;
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_unload (void)
|
|
{
|
|
struct multiboot_tag_header *tag;
|
|
struct multiboot_tag_header *tags =
|
|
(struct multiboot_tag_header *) grub_mb2_tags;
|
|
|
|
/* Free all module memory in the tag list. */
|
|
for_each_tag (tag, tags)
|
|
{
|
|
if (tag->key == MULTIBOOT2_TAG_MODULE)
|
|
{
|
|
struct multiboot_tag_module *module =
|
|
(struct multiboot_tag_module *) tag;
|
|
grub_free ((void *) module->addr);
|
|
}
|
|
}
|
|
|
|
/* Allow architecture to un-reserve memory. */
|
|
grub_mb2_arch_unload (tags);
|
|
|
|
/* Free the tags themselves. */
|
|
grub_mb2_tags_free ();
|
|
|
|
grub_dl_unref (my_mod);
|
|
|
|
return GRUB_ERR_NONE;
|
|
}
|
|
|
|
static grub_err_t
|
|
grub_mb2_load_other (UNUSED grub_file_t file, UNUSED void *buffer)
|
|
{
|
|
/* XXX Create module tag here. */
|
|
return grub_error (GRUB_ERR_UNKNOWN_OS, "currently only ELF is supported");
|
|
}
|
|
|
|
/* Create the tag containing the cmdline and the address of the module data. */
|
|
static grub_err_t
|
|
grub_mb2_tag_module_create (grub_addr_t modaddr, grub_size_t modsize,
|
|
char *type, int key, int argc, char *argv[])
|
|
{
|
|
struct multiboot_tag_module *module;
|
|
grub_ssize_t argslen = 0;
|
|
grub_err_t err;
|
|
char *p;
|
|
grub_addr_t module_addr;
|
|
int i;
|
|
|
|
/* Allocate enough space for the arguments and spaces between them. */
|
|
for (i = 0; i < argc; i++)
|
|
argslen += grub_strlen (argv[i]) + 1;
|
|
|
|
/* Note: includes implicit 1-byte cmdline. */
|
|
err = grub_mb2_tag_alloc (&module_addr, key,
|
|
sizeof (struct multiboot_tag_module) + argslen);
|
|
if (err)
|
|
return grub_errno;
|
|
|
|
module = (struct multiboot_tag_module *) module_addr;
|
|
module->addr = modaddr;
|
|
module->size = modsize;
|
|
grub_strcpy(module->type, type);
|
|
|
|
/* Fill in the command line. */
|
|
p = module->cmdline;
|
|
for (i = 0; i < argc; i++)
|
|
{
|
|
p = grub_stpcpy (p, argv[i]);
|
|
*p++ = ' ';
|
|
}
|
|
module->cmdline[argslen] = '\0';
|
|
|
|
return GRUB_ERR_NONE;
|
|
}
|
|
|
|
/* Load ELF32 or ELF64. */
|
|
static grub_err_t
|
|
grub_mb2_load_elf (grub_elf_t elf, int argc, char *argv[])
|
|
{
|
|
grub_addr_t kern_base;
|
|
grub_size_t kern_size;
|
|
grub_err_t err;
|
|
|
|
if (grub_elf_is_elf32 (elf))
|
|
{
|
|
entry = elf->ehdr.ehdr32.e_entry;
|
|
err = grub_elf32_load (elf, grub_mb2_arch_elf32_hook, &kern_base,
|
|
&kern_size);
|
|
}
|
|
else if (grub_elf_is_elf64 (elf))
|
|
{
|
|
entry = elf->ehdr.ehdr64.e_entry;
|
|
err = grub_elf64_load (elf, grub_mb2_arch_elf64_hook, &kern_base,
|
|
&kern_size);
|
|
}
|
|
else
|
|
err = grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
|
|
|
|
if (err)
|
|
goto fail;
|
|
|
|
grub_dprintf ("loader", "Entry point is 0x%lx.\n", (unsigned long) entry);
|
|
|
|
grub_mb2_tag_module_create (kern_base, kern_size, "kernel",
|
|
MULTIBOOT2_TAG_MODULE, argc, argv);
|
|
|
|
fail:
|
|
return err;
|
|
}
|
|
|
|
void
|
|
grub_multiboot2 (int argc, char *argv[])
|
|
{
|
|
char *buffer;
|
|
grub_file_t file = 0;
|
|
grub_elf_t elf = 0;
|
|
struct multiboot_header *header = 0;
|
|
char *p;
|
|
grub_ssize_t len;
|
|
grub_err_t err;
|
|
int header_found = 0;
|
|
|
|
grub_loader_unset ();
|
|
|
|
if (argc == 0)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
|
|
goto fail;
|
|
}
|
|
|
|
file = grub_gzfile_open (argv[0], 1);
|
|
if (! file)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
|
|
goto fail;
|
|
}
|
|
|
|
buffer = grub_malloc (MULTIBOOT2_HEADER_SEARCH);
|
|
if (! buffer)
|
|
return;
|
|
|
|
len = grub_file_read (file, buffer, MULTIBOOT2_HEADER_SEARCH);
|
|
if (len < 32)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_OS, "File too small");
|
|
goto fail;
|
|
}
|
|
|
|
/* Look for the multiboot header in the buffer. The header should
|
|
be at least 8 bytes and aligned on a 8-byte boundary. */
|
|
for (p = buffer; p <= buffer + len - 8; p += 8)
|
|
{
|
|
header = (struct multiboot_header *) p;
|
|
if (header->magic == MULTIBOOT2_HEADER_MAGIC)
|
|
{
|
|
header_found = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (! header_found)
|
|
grub_dprintf ("loader", "No multiboot 2 header found.\n");
|
|
|
|
|
|
/* Create the basic tags. */
|
|
grub_dprintf ("loader", "Creating multiboot 2 tags\n");
|
|
grub_mb2_tags_create ();
|
|
|
|
/* Load the kernel and create its tag. */
|
|
elf = grub_elf_file (file);
|
|
if (elf)
|
|
{
|
|
grub_dprintf ("loader", "Loading ELF multiboot 2 file.\n");
|
|
err = grub_mb2_load_elf (elf, argc-1, &argv[1]);
|
|
grub_elf_close (elf);
|
|
}
|
|
else
|
|
{
|
|
grub_errno = 0;
|
|
grub_dprintf ("loader", "Loading non-ELF multiboot 2 file.\n");
|
|
|
|
if (header)
|
|
err = grub_mb2_load_other (file, header);
|
|
else
|
|
err = grub_error (GRUB_ERR_BAD_OS,
|
|
"Need multiboot 2 header to load non-ELF files.");
|
|
grub_file_close (file);
|
|
}
|
|
|
|
grub_free (buffer);
|
|
|
|
if (err)
|
|
goto fail;
|
|
|
|
/* Good to go. */
|
|
grub_loader_set (grub_mb2_boot, grub_mb2_unload, 1);
|
|
return;
|
|
|
|
fail:
|
|
grub_mb2_tags_free ();
|
|
grub_dl_unref (my_mod);
|
|
}
|
|
|
|
void
|
|
grub_module2 (int argc, char *argv[])
|
|
{
|
|
grub_file_t file;
|
|
grub_addr_t modaddr = 0;
|
|
grub_ssize_t modsize = 0;
|
|
grub_err_t err;
|
|
|
|
if (argc == 0)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "No module specified");
|
|
return;
|
|
}
|
|
|
|
if (argc == 1)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "No module type specified");
|
|
return;
|
|
}
|
|
|
|
if (entry == 0)
|
|
{
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
"You need to load the multiboot kernel first");
|
|
return;
|
|
}
|
|
|
|
/* Load module data. */
|
|
file = grub_gzfile_open (argv[0], 1);
|
|
if (! file)
|
|
goto out;
|
|
|
|
modsize = grub_file_size (file);
|
|
err = grub_mb2_arch_module_alloc (modsize, &modaddr);
|
|
if (err)
|
|
goto out;
|
|
|
|
grub_dprintf ("loader", "Loading module at 0x%x - 0x%x\n", modaddr,
|
|
modaddr + modsize);
|
|
if (grub_file_read (file, (char *) modaddr, modsize) != modsize)
|
|
{
|
|
grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
|
|
goto out;
|
|
}
|
|
|
|
/* Create the module tag. */
|
|
err = grub_mb2_tag_module_create (modaddr, modsize,
|
|
argv[1], MULTIBOOT2_TAG_MODULE,
|
|
argc-2, &argv[2]);
|
|
if (err)
|
|
goto out;
|
|
|
|
out:
|
|
grub_error_push ();
|
|
|
|
if (file)
|
|
grub_file_close (file);
|
|
|
|
if (modaddr)
|
|
grub_mb2_arch_module_free (modaddr, modsize);
|
|
|
|
grub_error_pop ();
|
|
}
|