dbb475a441
* conf/i386-ieee1275.rmk (kernel_elf_SOURCES): Add `machine/loader.h' and `machine/memory.h'. (pkglib_MODULES): Add `multiboot.mod' and `_multiboot.mod'. (_multiboot_mod_SOURCES): New variable. (_multiboot_mod_CFLAGS): Likewise. (_multiboot_mod_LDFLAGS): Likewise. (multiboot_mod_SOURCES): Likewise. (multiboot_mod_CFLAGS): Likewise. (multiboot_mod_LDFLAGS): Likewise. * include/grub/i386/ieee1275/loader.h: New file. * include/grub/i386/ieee1275/machine.h: Likewise. * include/grub/i386/ieee1275/memory.h: Likewise. * include/grub/i386/pc/init.h (grub_os_area_addr): Remove (redundant) variable declaration. (grub_os_area_size): Likewise. * kern/i386/ieee1275/init.c (grub_os_area_addr, grub_os_area_size) (grub_lower_mem, grub_upper_mem): New variables. (grub_stop_floppy): New function (just to make grub_multiboot2_real_boot() happy). * kern/i386/ieee1275/startup.S: Include `<grub/machine/memory.h>', `<grub/cpu/linux.h>', `<multiboot.h>' and `<multiboot2.h>'. (grub_stop): New function. Include `"../realmode.S"' and `"../loader.S"'. * loader/multiboot_loader.c: Include `<grub/machine/machine.h>'. Replace `__i386__' #ifdefs with `GRUB_MACHINE_PCBIOS'. * loader/powerpc/ieee1275/multiboot2.c (grub_mb2_arch_boot): On i386, rely on grub_multiboot2_real_boot() for final boot.
177 lines
4.6 KiB
C
177 lines
4.6 KiB
C
/* multiboot_loader.c - boot multiboot 1 or 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/machine/machine.h>
|
|
#include <grub/multiboot_loader.h>
|
|
#include <grub/multiboot.h>
|
|
#include <grub/multiboot2.h>
|
|
#include <grub/elf.h>
|
|
#include <grub/file.h>
|
|
#include <grub/err.h>
|
|
#include <grub/rescue.h>
|
|
#include <grub/dl.h>
|
|
#include <grub/mm.h>
|
|
#include <grub/misc.h>
|
|
#include <grub/gzio.h>
|
|
|
|
grub_dl_t my_mod;
|
|
|
|
/* This tracks which version of multiboot to use when using
|
|
* the module command. By default use multiboot version 1.
|
|
* values:
|
|
* 1 - Multiboot version 1
|
|
* 2 - Multiboot version 2
|
|
*/
|
|
|
|
static unsigned int module_version_status = 1;
|
|
|
|
static int
|
|
find_multi_boot1_header (grub_file_t file)
|
|
{
|
|
struct grub_multiboot_header *header;
|
|
char buffer[MULTIBOOT_SEARCH];
|
|
int found_status = 0;
|
|
grub_ssize_t len;
|
|
|
|
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
|
|
if (len < 32)
|
|
return found_status;
|
|
|
|
/* Look for the multiboot header in the buffer. The header should
|
|
be at least 12 bytes and aligned on a 4-byte boundary. */
|
|
for (header = (struct grub_multiboot_header *) buffer;
|
|
((char *) header <= buffer + len - 12) || (header = 0);
|
|
header = (struct grub_multiboot_header *) ((char *) header + 4))
|
|
{
|
|
if (header->magic == MULTIBOOT_MAGIC
|
|
&& !(header->magic + header->flags + header->checksum))
|
|
{
|
|
found_status = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return found_status;
|
|
}
|
|
|
|
void
|
|
grub_rescue_cmd_multiboot_loader (int argc, char *argv[])
|
|
{
|
|
|
|
grub_file_t file = 0;
|
|
int header_multi_ver_found = 0;
|
|
|
|
grub_dl_ref (my_mod);
|
|
|
|
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;
|
|
}
|
|
|
|
/* find which header is in the file */
|
|
if (find_multi_boot1_header(file))
|
|
header_multi_ver_found = 1;
|
|
else
|
|
{
|
|
/* The behavior is that if you don't find a multiboot 1 header
|
|
use multiboot 2 loader (as you do not have to have a header
|
|
to use multiboot 2 */
|
|
grub_dprintf ("multiboot_loader", "No multiboot 1 header found. \n \
|
|
Using multiboot 2 loader\n");
|
|
header_multi_ver_found = 0;
|
|
}
|
|
|
|
/* close file before calling functions */
|
|
if (file)
|
|
grub_file_close (file);
|
|
|
|
/* Launch multi boot with header */
|
|
|
|
/* XXX Find a better way to identify this.
|
|
This is for i386-pc */
|
|
#ifdef GRUB_MACHINE_PCBIOS
|
|
if (header_multi_ver_found == 1)
|
|
{
|
|
grub_dprintf ("multiboot_loader",
|
|
"Launching multiboot 1 grub_multiboot() function\n");
|
|
grub_multiboot (argc, argv);
|
|
module_version_status = 1;
|
|
}
|
|
#endif
|
|
if (header_multi_ver_found == 0 || header_multi_ver_found == 2)
|
|
{
|
|
grub_dprintf ("multiboot_loader",
|
|
"Launching multiboot 2 grub_multiboot2() function\n");
|
|
grub_multiboot2 (argc, argv);
|
|
module_version_status = 2;
|
|
}
|
|
|
|
return;
|
|
|
|
fail:
|
|
if (file)
|
|
grub_file_close (file);
|
|
|
|
grub_dl_unref (my_mod);
|
|
}
|
|
|
|
void
|
|
grub_rescue_cmd_module_loader (int argc, char *argv[])
|
|
{
|
|
|
|
#ifdef GRUB_MACHINE_PCBIOS
|
|
if (module_version_status == 1)
|
|
{
|
|
grub_dprintf("multiboot_loader",
|
|
"Launching multiboot 1 grub_module() function\n");
|
|
grub_module (argc, argv);
|
|
}
|
|
#endif
|
|
if (module_version_status == 2)
|
|
{
|
|
grub_dprintf("multiboot_loader",
|
|
"Launching multiboot 2 grub_module2() function\n");
|
|
grub_module2 (argc, argv);
|
|
}
|
|
}
|
|
|
|
GRUB_MOD_INIT(multiboot)
|
|
{
|
|
grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot_loader,
|
|
"load a multiboot kernel");
|
|
grub_rescue_register_command ("module", grub_rescue_cmd_module_loader,
|
|
"load a multiboot module");
|
|
|
|
my_mod = mod;
|
|
}
|
|
|
|
GRUB_MOD_FINI(multiboot)
|
|
{
|
|
grub_rescue_unregister_command ("multiboot");
|
|
grub_rescue_unregister_command ("module");
|
|
}
|