58bc8bd5b1
* boot/i386/pc/boot.S: Update copyright year. * boot/i386/pc/cdboot.S: Likewise. * boot/i386/pc/diskboot.S: Likewise. * boot/i386/pc/lnxboot.S: Likewise. * boot/i386/pc/pxeboot.S: Likewise. * bus/pci.c: Likewise. * commands/cmp.c: Likewise. * commands/help.c: Likewise. * commands/hexdump.c: Likewise. * commands/i386/pc/halt.c: Likewise. * commands/i386/pc/play.c: Likewise. * commands/i386/pc/vbeinfo.c: Likewise. * commands/ls.c: Likewise. * commands/test.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/i386/pc/biosdisk.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/lvm.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * fs/affs.c: Likewise. * fs/cpio.c: Likewise. * fs/ext2.c: Likewise. * fs/hfs.c: Likewise. * fs/iso9660.c: Likewise. * fs/ntfs.c: Likewise. * fs/sfs.c: Likewise. * fs/udf.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gencmdlist.sh: Likewise. * genmk.rb: Likewise. * include/grub/disk.h: Likewise. * include/grub/efi/api.h: Likewise. * include/grub/efi/efi.h: Likewise. * include/grub/efi/pe32.h: Likewise. * include/grub/elf.h: Likewise. * include/grub/fs.h: Likewise. * include/grub/i386/at_keyboard.h: Likewise. * include/grub/i386/pc/memory.h: Likewise. * include/grub/i386/pc/vbe.h: Likewise. * include/grub/i386/pci.h: Likewise. * include/grub/i386/tsc.h: Likewise. * include/grub/ieee1275/ieee1275.h: Likewise. * include/grub/ntfs.h: Likewise. * include/grub/sparc64/ieee1275/ieee1275.h: Likewise. * include/grub/sparc64/libgcc.h: Likewise. * include/grub/symbol.h: Likewise. * include/grub/types.h: Likewise. * include/multiboot2.h: Likewise. * io/gzio.c: Likewise. * kern/device.c: Likewise. * kern/disk.c: Likewise. * kern/efi/efi.c: Likewise. * kern/efi/mm.c: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/i386/dl.c: Likewise. * kern/i386/pc/init.c: Likewise. * kern/i386/pc/startup.S: Likewise. * kern/ieee1275/ieee1275.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/main.c: Likewise. * kern/mm.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * kern/x86_64/dl.c: Likewise. * lib/hexdump.c: Likewise. * loader/efi/appleloader.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/pc/chainloader.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * normal/completion.c: Likewise. * normal/menu_entry.c: Likewise. * partmap/apple.c: Likewise. * util/grub.d/10_hurd.in: Likewise. * util/hostfs.c: Likewise. * video/readers/png.c: Likewise.
461 lines
10 KiB
C
461 lines
10 KiB
C
/* multiboot2.c - boot a multiboot 2 OS image. */
|
|
/*
|
|
* GRUB -- GRand Unified Bootloader
|
|
* Copyright (C) 2007,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/>.
|
|
*/
|
|
|
|
#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 ();
|
|
}
|