This patch is to introduce multiboot 2 loading capabilities to grub2
for powerpc & i386-pc. This patch was more so started by Hollis Blanchard getting multiboot 2 working for powerpc and I added to it and cleaned it up. One of the ideas with this patch is to keep everything under one command for the user. So instead of having a "multiboot2" & "module2" command, I created a proxy like mechanism so that you have only one command for both multiboot 1 & 2 ... "multiboot". This is where "loader/multiboot_loader.c" comes from. I could have integrated things more but I figure the current approach will less likely break anything. So if your OS is multiboot 2 capable, the user would do the following to load it up from a grub prompt: grub> multiboot <location of kernel> <kernel args> grub> module <some image> <multiboot tag> <image arguments> grub> module <isome mage> <multiboot tag> <image arguments> grub ..... The other thing that this patch does is it begins to make the multiboot 1 code a bit more architecture agnostic so IF someone wanted to implement it on another architecture they can. A bit of file moving around and definition renaming is also apart of this patch. I have also taken the time to make sure that it does not break multiboot 1 loading on i386-pc. But mulitboot 2 may still need a little more testing and work for i386-pc. Powerpc multiboot 2 has been heavily tested and does work.
This commit is contained in:
parent
daf0f0ba3e
commit
e5dfe7775a
17 changed files with 1315 additions and 300 deletions
|
@ -32,7 +32,7 @@
|
|||
|
||||
#include <grub/loader.h>
|
||||
#include <grub/machine/loader.h>
|
||||
#include <grub/machine/multiboot.h>
|
||||
#include <grub/multiboot.h>
|
||||
#include <grub/machine/init.h>
|
||||
#include <grub/elf.h>
|
||||
#include <grub/file.h>
|
||||
|
@ -43,7 +43,7 @@
|
|||
#include <grub/misc.h>
|
||||
#include <grub/gzio.h>
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
extern grub_dl_t my_mod;
|
||||
static struct grub_multiboot_info *mbi;
|
||||
static grub_addr_t entry;
|
||||
|
||||
|
@ -107,7 +107,7 @@ grub_multiboot_load_elf32 (grub_file_t file, void *buffer)
|
|||
return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
|
||||
|
||||
/* FIXME: Should we support program headers at strange locations? */
|
||||
if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH)
|
||||
if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
|
||||
return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
|
||||
|
||||
entry = ehdr->e_entry;
|
||||
|
@ -177,7 +177,7 @@ grub_multiboot_load_elf64 (grub_file_t file, void *buffer)
|
|||
return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
|
||||
|
||||
/* FIXME: Should we support program headers at strange locations? */
|
||||
if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH)
|
||||
if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
|
||||
return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
|
||||
|
||||
/* We still in 32-bit mode */
|
||||
|
@ -236,16 +236,14 @@ grub_multiboot_load_elf (grub_file_t file, void *buffer)
|
|||
}
|
||||
|
||||
void
|
||||
grub_rescue_cmd_multiboot (int argc, char *argv[])
|
||||
grub_multiboot (int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
char buffer[GRUB_MB_SEARCH], *cmdline = 0, *p;
|
||||
char buffer[MULTIBOOT_SEARCH], *cmdline = 0, *p;
|
||||
struct grub_multiboot_header *header;
|
||||
grub_ssize_t len;
|
||||
int i;
|
||||
|
||||
grub_dl_ref (my_mod);
|
||||
|
||||
grub_loader_unset ();
|
||||
|
||||
if (argc == 0)
|
||||
|
@ -261,7 +259,7 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
goto fail;
|
||||
}
|
||||
|
||||
len = grub_file_read (file, buffer, GRUB_MB_SEARCH);
|
||||
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
|
||||
if (len < 32)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_OS, "File too small");
|
||||
|
@ -274,7 +272,7 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
((char *) header <= buffer + len - 12) || (header = 0);
|
||||
header = (struct grub_multiboot_header *) ((char *) header + 4))
|
||||
{
|
||||
if (header->magic == GRUB_MB_MAGIC
|
||||
if (header->magic == MULTIBOOT_MAGIC
|
||||
&& !(header->magic + header->flags + header->checksum))
|
||||
break;
|
||||
}
|
||||
|
@ -285,7 +283,7 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
goto fail;
|
||||
}
|
||||
|
||||
if (header->flags & GRUB_MB_UNSUPPORTED)
|
||||
if (header->flags & MULTIBOOT_UNSUPPORTED)
|
||||
{
|
||||
grub_error (GRUB_ERR_UNKNOWN_OS,
|
||||
"Unsupported flag: 0x%x", header->flags);
|
||||
|
@ -299,7 +297,7 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
if (! mbi)
|
||||
goto fail;
|
||||
|
||||
mbi->flags = GRUB_MB_INFO_MEMORY;
|
||||
mbi->flags = MULTIBOOT_INFO_MEMORY;
|
||||
|
||||
/* Convert from bytes to kilobytes. */
|
||||
mbi->mem_lower = grub_lower_mem / 1024;
|
||||
|
@ -321,10 +319,10 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
/* Remove the space after the last word. */
|
||||
*(--p) = '\0';
|
||||
|
||||
mbi->flags |= GRUB_MB_INFO_CMDLINE;
|
||||
mbi->flags |= MULTIBOOT_INFO_CMDLINE;
|
||||
mbi->cmdline = (grub_uint32_t) cmdline;
|
||||
|
||||
mbi->flags |= GRUB_MB_INFO_BOOT_LOADER_NAME;
|
||||
mbi->flags |= MULTIBOOT_INFO_BOOT_LOADER_NAME;
|
||||
mbi->boot_loader_name = (grub_uint32_t) grub_strdup (PACKAGE_STRING);
|
||||
|
||||
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 1);
|
||||
|
@ -343,7 +341,7 @@ grub_rescue_cmd_multiboot (int argc, char *argv[])
|
|||
|
||||
|
||||
void
|
||||
grub_rescue_cmd_module (int argc, char *argv[])
|
||||
grub_module (int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
grub_ssize_t size, len = 0;
|
||||
|
@ -368,7 +366,7 @@ grub_rescue_cmd_module (int argc, char *argv[])
|
|||
goto fail;
|
||||
|
||||
size = grub_file_size (file);
|
||||
module = grub_memalign (GRUB_MB_MOD_ALIGN, size);
|
||||
module = grub_memalign (MULTIBOOT_MOD_ALIGN, size);
|
||||
if (! module)
|
||||
goto fail;
|
||||
|
||||
|
@ -394,7 +392,7 @@ grub_rescue_cmd_module (int argc, char *argv[])
|
|||
/* Remove the space after the last word. */
|
||||
*(--p) = '\0';
|
||||
|
||||
if (mbi->flags & GRUB_MB_INFO_MODS)
|
||||
if (mbi->flags & MULTIBOOT_INFO_MODS)
|
||||
{
|
||||
struct grub_mod_list *modlist = (struct grub_mod_list *) mbi->mods_addr;
|
||||
|
||||
|
@ -421,7 +419,7 @@ grub_rescue_cmd_module (int argc, char *argv[])
|
|||
modlist->pad = 0;
|
||||
mbi->mods_count = 1;
|
||||
mbi->mods_addr = (grub_uint32_t) modlist;
|
||||
mbi->flags |= GRUB_MB_INFO_MODS;
|
||||
mbi->flags |= MULTIBOOT_INFO_MODS;
|
||||
}
|
||||
|
||||
fail:
|
||||
|
@ -434,19 +432,3 @@ grub_rescue_cmd_module (int argc, char *argv[])
|
|||
grub_free (cmdline);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GRUB_MOD_INIT(multiboot)
|
||||
{
|
||||
grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot,
|
||||
"load a multiboot kernel");
|
||||
grub_rescue_register_command ("module", grub_rescue_cmd_module,
|
||||
"load a multiboot module");
|
||||
my_mod = mod;
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(multiboot)
|
||||
{
|
||||
grub_rescue_unregister_command ("multiboot");
|
||||
grub_rescue_unregister_command ("module");
|
||||
}
|
||||
|
|
101
loader/i386/pc/multiboot2.c
Normal file
101
loader/i386/pc/multiboot2.c
Normal file
|
@ -0,0 +1,101 @@
|
|||
/* 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/multiboot2.h>
|
||||
#include <grub/elf.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/machine/loader.h>
|
||||
#include <grub/mm.h>
|
||||
|
||||
grub_err_t
|
||||
grub_mb2_arch_elf32_hook (Elf32_Phdr *phdr, UNUSED grub_addr_t *addr)
|
||||
{
|
||||
Elf32_Addr paddr = phdr->p_paddr;
|
||||
|
||||
if ((paddr < grub_os_area_addr)
|
||||
|| (paddr + phdr->p_memsz > grub_os_area_addr + grub_os_area_size))
|
||||
return grub_error(GRUB_ERR_OUT_OF_RANGE,"Address 0x%x is out of range",
|
||||
paddr);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_mb2_arch_elf64_hook (Elf64_Phdr *phdr, UNUSED grub_addr_t *addr)
|
||||
{
|
||||
Elf64_Addr paddr = phdr->p_paddr;
|
||||
|
||||
if ((paddr < grub_os_area_addr)
|
||||
|| (paddr + phdr->p_memsz > grub_os_area_addr + grub_os_area_size))
|
||||
return (GRUB_ERR_OUT_OF_RANGE,"Address 0x%x is out of range",
|
||||
paddr);
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_mb2_arch_module_alloc (grub_size_t size, grub_addr_t *addr)
|
||||
{
|
||||
grub_addr_t modaddr;
|
||||
|
||||
modaddr = grub_memalign (MULTIBOOT2_MOD_ALIGN, size);
|
||||
if (! modaddr)
|
||||
return grub_errno;
|
||||
|
||||
*addr = modaddr;
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_mb2_arch_module_free (grub_addr_t addr, UNUSED grub_size_t size)
|
||||
{
|
||||
grub_free((void *) addr);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
void
|
||||
grub_mb2_arch_boot (grub_addr_t entry, void *tags)
|
||||
{
|
||||
grub_multiboot2_real_boot (entry, tags);
|
||||
}
|
||||
|
||||
void
|
||||
grub_mb2_arch_unload (struct multiboot_tag_header *tags)
|
||||
{
|
||||
struct multiboot_tag_header *tag;
|
||||
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_mb2_tags_arch_create (void)
|
||||
{
|
||||
/* XXX Create boot device et al. */
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue