grub/loader/multiboot2.c
carles 7fd0baee0f 2009-12-24 Carles Pina i Estany <carles@pina.cat>
* bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in
	grub_errno calls.
	* commands/acpi.c: Likewise.
	* commands/blocklist.c: Likewise.
	* commands/efi/loadbios.c: Likewise.
	* commands/i386/pc/drivemap.c: Likewise.
	* commands/loadenv.c: Likewise.
	* commands/memrw.c: Likewise.
	* commands/password.c: Likewise.
	* commands/videotest.c: Likewise.
	* disk/ata.c: Likewise.
	* disk/ata_pthru.c: Likewise.
	* disk/dmraid_nvidia.c: Likewise.
	* disk/ieee1275/nand.c: Likewise.
	* disk/ieee1275/ofdisk.c: Likewise.
	* disk/loopback.c: Likewise.
	* disk/lvm.c: Likewise.
	* disk/mdraid_linux.c: Likewise.
	* disk/raid.c: Likewise.
	* disk/raid6_recover.c: Likewise.
	* disk/scsi.c: Likewise.
	* efiemu/main.c: Likewise.
	* efiemu/mm.c: Likewise.
	* efiemu/pnvram.c: Likewise.
	* efiemu/symbols.c: Likewise.
	* font/font.c: Likewise.
	* fs/cpio.c: Likewise.
	* fs/hfsplus.c: Likewise.
	* fs/iso9660.c: Likewise.
	* fs/jfs.c: Likewise.
	* fs/minix.c: Likewise.
	* fs/ntfs.c: Likewise.
	* fs/ntfscomp.c: Likewise.
	* fs/reiserfs.c: Likewise.
	* fs/ufs.c: Likewise.
	* fs/xfs.c: Likewise.
	* gettext/gettext.c: Likewise.
	* include/grub/auth.h: Likewise.
	* kern/elf.c: Likewise.
	* kern/file.c: Likewise.
	* kern/ieee1275/init.c: Likewise.
	* kern/ieee1275/mmap.c: Likewise.
	* kern/ieee1275/openfw.c: Likewise.
	* kern/powerpc/dl.c: Likewise.
	* kern/sparc64/dl.c: Likewise.
	* lib/arg.c: Likewise.
	* loader/i386/bsd.c: Likewise.
	* loader/i386/bsdXX.c: Likewise.
	* loader/i386/efi/linux.c: Likewise.
	* loader/i386/efi/xnu.c: Likewise.
	* loader/i386/ieee1275/linux.c: Likewise.
	* loader/i386/linux.c: Likewise.
	* loader/i386/multiboot.c: Likewise.
	* loader/i386/pc/linux.c: Likewise.
	* loader/i386/pc/multiboot2.c: Likewise.
	* loader/i386/xnu.c: Likewise.
	* loader/ieee1275/multiboot2.c: Likewise.
	* loader/macho.c: Likewise.
	* loader/machoXX.c: Likewise.
	* loader/multiboot2.c: Likewise.
	* loader/multiboot_loader.c: Likewise.
	* loader/powerpc/ieee1275/linux.c: Likewise.
	* loader/sparc64/ieee1275/linux.c: Likewise.
	* loader/xnu.c: Likewise.
	* loader/xnu_resume.c: Likewise.
	* mmap/i386/pc/mmap.c: Likewise.
	* normal/menu_viewer.c: Likewise.
	* partmap/acorn.c: Likewise.
	* partmap/amiga.c: Likewise.
	* partmap/apple.c: Likewise.
	* script/lexer.c: Likewise.
	* term/gfxterm.c: Likewise.
	* term/i386/pc/serial.c: Likewise.
	* term/i386/pc/vga.c: Likewise.
	* term/ieee1275/ofconsole.c: Likewise.
	* term/terminfo.c: Likewise.
	* video/bitmap.c: Likewise.
	* video/efi_gop.c: Likewise.
	* video/efi_uga.c: Likewise.
	* video/fb/video_fb.c: Likewise.
	* video/i386/pc/vbe.c: Likewise.
	* video/readers/tga.c: Likewise.
	* video/video.c: Likewise.
2009-12-24 23:53:05 +01:00

461 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 <grub/loader.h>
#include <grub/machine/loader.h>
#include <grub/multiboot2.h>
#include <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 multiboot2_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 (multiboot2_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 multiboot2_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 multiboot2_tag_start));
}
static grub_err_t
grub_mb2_tag_name_create (void)
{
struct multiboot2_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 multiboot2_tag_name) +
sizeof (grub_version) + 1);
if (err)
return err;
name = (struct multiboot2_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 multiboot2_tag_start *start;
grub_err_t err;
/* Create the `end' tag. */
err = grub_mb2_tag_alloc (0, MULTIBOOT2_TAG_END,
sizeof (struct multiboot2_tag_end));
if (err)
goto error;
/* We created the `start' tag first. Update it now. */
start = (struct multiboot2_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 multiboot2_tag_header *tag;
struct multiboot2_tag_header *tags =
(struct multiboot2_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 multiboot2_tag_module *module =
(struct multiboot2_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 (grub_file_t file __attribute__ ((unused)),
void *buffer __attribute__ ((unused)))
{
/* 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 multiboot2_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 multiboot2_tag_module) + argslen);
if (err)
return grub_errno;
module = (struct multiboot2_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 multiboot2_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 multiboot2_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, (void *) 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 ();
}