2003-11-16 16:36:39 +00:00
|
|
|
/* multiboot.c - boot a multiboot OS image. */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-14 21:08:31 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2003-11-16 16:36:39 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-11-16 16:36:39 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-11-16 16:36:39 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-11-16 16:36:39 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-16 16:36:39 +00:00
|
|
|
*/
|
|
|
|
|
2008-02-19 16:40:45 +00:00
|
|
|
/*
|
2003-11-16 16:36:39 +00:00
|
|
|
* FIXME: The following features from the Multiboot specification still
|
|
|
|
* need to be implemented:
|
|
|
|
* - drives table
|
|
|
|
* - ROM configuration table
|
2010-09-21 06:37:50 +00:00
|
|
|
* - SMBIOS tables
|
|
|
|
* - Networking information
|
2003-11-16 16:36:39 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/loader.h>
|
2010-03-08 14:40:57 +00:00
|
|
|
#include <grub/command.h>
|
2017-08-14 10:51:26 +00:00
|
|
|
#ifdef GRUB_USE_MULTIBOOT2
|
|
|
|
#include <grub/multiboot2.h>
|
|
|
|
#define GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER GRUB_MULTIBOOT2_CONSOLE_FRAMEBUFFER
|
|
|
|
#define GRUB_MULTIBOOT_CONSOLE_EGA_TEXT GRUB_MULTIBOOT2_CONSOLE_EGA_TEXT
|
|
|
|
#define GRUB_MULTIBOOT(x) grub_multiboot2_ ## x
|
|
|
|
#else
|
2007-07-25 00:44:03 +00:00
|
|
|
#include <grub/multiboot.h>
|
2017-08-14 10:51:26 +00:00
|
|
|
#define GRUB_MULTIBOOT(x) grub_multiboot_ ## x
|
|
|
|
#endif
|
2009-03-22 12:28:00 +00:00
|
|
|
#include <grub/cpu/multiboot.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/elf.h>
|
2008-02-19 16:40:45 +00:00
|
|
|
#include <grub/aout.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
2008-03-05 05:09:35 +00:00
|
|
|
#include <grub/env.h>
|
2010-04-03 12:14:48 +00:00
|
|
|
#include <grub/cpu/relocator.h>
|
2010-01-14 21:08:31 +00:00
|
|
|
#include <grub/video.h>
|
2010-01-16 15:25:43 +00:00
|
|
|
#include <grub/memory.h>
|
2010-03-08 14:40:57 +00:00
|
|
|
#include <grub/i18n.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2010-01-15 20:11:51 +00:00
|
|
|
#ifdef GRUB_MACHINE_EFI
|
|
|
|
#include <grub/efi/efi.h>
|
|
|
|
#endif
|
2004-04-04 13:46:03 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
struct grub_relocator *GRUB_MULTIBOOT (relocator) = NULL;
|
|
|
|
grub_uint32_t GRUB_MULTIBOOT (payload_eip);
|
2010-05-01 12:06:53 +00:00
|
|
|
#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
|
2010-01-16 15:25:43 +00:00
|
|
|
#define DEFAULT_VIDEO_MODE "text"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_VIDEO_MODE "auto"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int accepts_video;
|
2010-03-10 10:40:20 +00:00
|
|
|
static int accepts_ega_text;
|
|
|
|
static int console_required;
|
2010-03-08 14:40:57 +00:00
|
|
|
static grub_dl_t my_mod;
|
|
|
|
|
2010-01-16 15:25:43 +00:00
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
/* Helper for grub_get_multiboot_mmap_count. */
|
|
|
|
static int
|
|
|
|
count_hook (grub_uint64_t addr __attribute__ ((unused)),
|
|
|
|
grub_uint64_t size __attribute__ ((unused)),
|
|
|
|
grub_memory_type_t type __attribute__ ((unused)), void *data)
|
|
|
|
{
|
|
|
|
grub_size_t *count = data;
|
|
|
|
|
|
|
|
(*count)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-16 15:25:43 +00:00
|
|
|
/* Return the length of the Multiboot mmap that will be needed to allocate
|
|
|
|
our platform's map. */
|
|
|
|
grub_uint32_t
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (get_mmap_count) (void)
|
2010-01-16 15:25:43 +00:00
|
|
|
{
|
|
|
|
grub_size_t count = 0;
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
grub_mmap_iterate (count_hook, &count);
|
2010-01-16 15:25:43 +00:00
|
|
|
|
2010-03-07 13:59:15 +00:00
|
|
|
return count;
|
2010-01-16 15:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (set_video_mode) (void)
|
2010-01-16 15:25:43 +00:00
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
const char *modevar;
|
|
|
|
|
2012-02-09 22:48:34 +00:00
|
|
|
#if GRUB_MACHINE_HAS_VGA_TEXT
|
|
|
|
if (accepts_video)
|
|
|
|
#endif
|
2010-01-16 15:25:43 +00:00
|
|
|
{
|
|
|
|
modevar = grub_env_get ("gfxpayload");
|
|
|
|
if (! modevar || *modevar == 0)
|
2010-03-07 12:01:43 +00:00
|
|
|
err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
|
2010-01-16 15:25:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *tmp;
|
2010-03-07 12:01:43 +00:00
|
|
|
tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
|
2010-01-16 15:25:43 +00:00
|
|
|
if (! tmp)
|
|
|
|
return grub_errno;
|
2010-03-07 12:01:43 +00:00
|
|
|
err = grub_video_set_mode (tmp, 0, 0);
|
2010-01-16 15:25:43 +00:00
|
|
|
grub_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 22:48:34 +00:00
|
|
|
#if GRUB_MACHINE_HAS_VGA_TEXT
|
2010-01-16 15:25:43 +00:00
|
|
|
else
|
2010-03-07 12:01:43 +00:00
|
|
|
err = grub_video_set_mode ("text", 0, 0);
|
2012-02-09 22:48:34 +00:00
|
|
|
#endif
|
2010-01-16 15:25:43 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2009-11-25 22:39:59 +00:00
|
|
|
|
2015-07-17 17:43:42 +00:00
|
|
|
#ifdef GRUB_MACHINE_EFI
|
|
|
|
#ifdef __x86_64__
|
|
|
|
#define grub_relocator_efi_boot grub_relocator64_efi_boot
|
|
|
|
#define grub_relocator_efi_state grub_relocator64_efi_state
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef grub_relocator_efi_boot
|
|
|
|
static void
|
|
|
|
efi_boot (struct grub_relocator *rel,
|
|
|
|
grub_uint32_t target)
|
|
|
|
{
|
2017-08-14 14:24:05 +00:00
|
|
|
#ifdef GRUB_USE_MULTIBOOT2
|
|
|
|
struct grub_relocator_efi_state state_efi = MULTIBOOT2_EFI_INITIAL_STATE;
|
|
|
|
#else
|
2015-07-17 17:43:42 +00:00
|
|
|
struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
|
2017-08-14 14:24:05 +00:00
|
|
|
#endif
|
2017-09-07 11:55:22 +00:00
|
|
|
state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
|
2015-07-17 17:43:42 +00:00
|
|
|
state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
|
|
|
|
|
|
|
|
grub_relocator_efi_boot (rel, state_efi);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define grub_efi_is_finished 1
|
|
|
|
static void
|
|
|
|
efi_boot (struct grub_relocator *rel __attribute__ ((unused)),
|
|
|
|
grub_uint32_t target __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (__i386__) || defined (__x86_64__)
|
|
|
|
static void
|
|
|
|
normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
|
|
|
|
{
|
|
|
|
grub_relocator32_boot (rel, state, 0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void
|
|
|
|
normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
|
|
|
|
{
|
|
|
|
grub_relocator32_boot (rel, state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_multiboot_boot (void)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_err_t err;
|
2010-04-03 12:14:48 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
#ifdef GRUB_USE_MULTIBOOT2
|
|
|
|
struct grub_relocator32_state state = MULTIBOOT2_INITIAL_STATE;
|
|
|
|
#else
|
|
|
|
struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
|
|
|
|
#endif
|
|
|
|
state.MULTIBOOT_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
|
2009-11-25 22:39:59 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
err = GRUB_MULTIBOOT (make_mbi) (&state.MULTIBOOT_MBI_REGISTER);
|
2010-01-10 17:58:18 +00:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2015-07-17 17:43:42 +00:00
|
|
|
if (grub_efi_is_finished)
|
2017-08-14 10:51:26 +00:00
|
|
|
normal_boot (GRUB_MULTIBOOT (relocator), state);
|
2015-07-17 17:43:42 +00:00
|
|
|
else
|
2017-08-14 10:51:26 +00:00
|
|
|
efi_boot (GRUB_MULTIBOOT (relocator), state.MULTIBOOT_MBI_REGISTER);
|
2003-11-16 16:36:39 +00:00
|
|
|
|
|
|
|
/* Not reached. */
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_multiboot_unload (void)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (free_mbi) ();
|
2010-01-10 17:58:18 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
grub_relocator_unload (GRUB_MULTIBOOT (relocator));
|
|
|
|
GRUB_MULTIBOOT (relocator) = NULL;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_dl_unref (my_mod);
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 14:23:46 +00:00
|
|
|
static grub_uint64_t highest_load;
|
|
|
|
|
2009-02-27 23:04:46 +00:00
|
|
|
#define MULTIBOOT_LOAD_ELF64
|
|
|
|
#include "multiboot_elfxx.c"
|
|
|
|
#undef MULTIBOOT_LOAD_ELF64
|
2005-07-31 16:12:29 +00:00
|
|
|
|
2009-02-27 23:04:46 +00:00
|
|
|
#define MULTIBOOT_LOAD_ELF32
|
|
|
|
#include "multiboot_elfxx.c"
|
|
|
|
#undef MULTIBOOT_LOAD_ELF32
|
2005-07-31 16:12:29 +00:00
|
|
|
|
|
|
|
/* Load ELF32 or ELF64. */
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_err_t
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (load_elf) (mbi_load_data_t *mld)
|
2005-07-31 16:12:29 +00:00
|
|
|
{
|
multiboot2: Add support for relocatable images
Currently multiboot2 protocol loads image exactly at address specified in
ELF or multiboot2 header. This solution works quite well on legacy BIOS
platforms. It is possible because memory regions are placed at predictable
addresses (though I was not able to find any spec which says that it is
strong requirement, so, it looks that it is just a goodwill of hardware
designers). However, EFI platforms are more volatile. Even if required
memory regions live at specific addresses then they are sometimes simply
not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and
OVMF). This means that you are not able to just set up final image
destination on build time. You have to provide method to relocate image
contents to real load address which is usually different than load address
specified in ELF and multiboot2 headers.
This patch provides all needed machinery to do self relocation in image code.
First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load addr),
align (required image alignment), preference (it says which memory regions are
preferred by image, e.g. none, low, high) from multiboot_header_tag_relocatable
header tag contained in binary (at this stage load addresses from multiboot2
and/or ELF headers are ignored). Later loader tries to fulfill request (not only
that one) and if it succeeds then it informs image about real load address via
multiboot_tag_load_base_addr tag. At this stage GRUB2 role is finished. Starting
from now executable must cope with relocations itself using whole static and
dynamic knowledge provided by boot loader.
This patch does not provide functionality which could do relocations using
ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and Vladimir
'phcoder' Serbinenko to investigate that thing. It looks that relevant machinery
could be added to existing code (including this patch) without huge effort.
Additionally, ELF relocation could live in parallel with self relocation provided
by this patch. However, during research I realized that first of all we should
establish the details how ELF relocatable image should look like and how it should
be build. At least to build proper test/example files.
So, this patch just provides support for self relocatable images. If ELF file
with relocs is loaded then GRUB2 complains loudly and ignores it. Support for
such files will be added later.
This patch was tested with Xen image which uses that functionality. However, this Xen
feature is still under development and new patchset will be released in about 2-3 weeks.
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
2015-07-17 19:02:09 +00:00
|
|
|
if (grub_multiboot_is_elf32 (mld->buffer))
|
|
|
|
return grub_multiboot_load_elf32 (mld);
|
|
|
|
else if (grub_multiboot_is_elf64 (mld->buffer))
|
|
|
|
return grub_multiboot_load_elf64 (mld);
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2012-02-26 16:28:05 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
|
2005-07-31 16:12:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_err_t
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (set_console) (int console_type, int accepted_consoles,
|
|
|
|
int width, int height, int depth,
|
|
|
|
int console_req)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2010-03-10 10:40:20 +00:00
|
|
|
console_required = console_req;
|
|
|
|
if (!(accepted_consoles
|
|
|
|
& (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
|
|
|
|
| (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2010-03-10 10:40:20 +00:00
|
|
|
if (console_required)
|
|
|
|
return grub_error (GRUB_ERR_BAD_OS,
|
|
|
|
"OS requires a console but none is available");
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_puts_ (N_("WARNING: no console will be available to OS"));
|
2010-03-10 10:40:20 +00:00
|
|
|
accepts_video = 0;
|
|
|
|
accepts_ega_text = 0;
|
|
|
|
return GRUB_ERR_NONE;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2010-03-08 14:40:57 +00:00
|
|
|
char *buf;
|
|
|
|
if (depth && width && height)
|
|
|
|
buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
|
|
|
|
height, depth, width, height);
|
|
|
|
else if (width && height)
|
|
|
|
buf = grub_xasprintf ("%dx%d,auto", width, height);
|
|
|
|
else
|
|
|
|
buf = grub_strdup ("auto");
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
if (!buf)
|
|
|
|
return grub_errno;
|
|
|
|
grub_env_set ("gfxpayload", buf);
|
|
|
|
grub_free (buf);
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2013-09-23 11:35:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#if GRUB_MACHINE_HAS_VGA_TEXT
|
|
|
|
grub_env_set ("gfxpayload", "text");
|
|
|
|
#else
|
|
|
|
/* Always use video if no VGA text is available. */
|
|
|
|
grub_env_set ("gfxpayload", "auto");
|
|
|
|
#endif
|
|
|
|
}
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
|
2010-03-10 10:40:20 +00:00
|
|
|
accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
|
2010-03-08 14:40:57 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_file_t file = 0;
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_err_t err;
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2005-07-31 16:12:29 +00:00
|
|
|
grub_loader_unset ();
|
2005-02-14 18:41:33 +00:00
|
|
|
|
2013-10-28 14:23:46 +00:00
|
|
|
highest_load = 0;
|
|
|
|
|
|
|
|
#ifndef GRUB_USE_MULTIBOOT2
|
|
|
|
grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
|
2015-11-12 10:54:38 +00:00
|
|
|
int option_found = 0;
|
2013-10-28 14:23:46 +00:00
|
|
|
|
2015-11-12 10:54:38 +00:00
|
|
|
do
|
2013-10-28 14:23:46 +00:00
|
|
|
{
|
2015-11-12 10:54:38 +00:00
|
|
|
option_found = 0;
|
|
|
|
if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
|
|
|
|
{
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
option_found = 1;
|
|
|
|
grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
|
|
|
|
}
|
2013-10-28 14:23:46 +00:00
|
|
|
|
2015-11-12 10:54:38 +00:00
|
|
|
if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
|
|
|
|
{
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
option_found = 1;
|
|
|
|
grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
|
|
|
|
}
|
|
|
|
} while (option_found);
|
2013-10-28 14:23:46 +00:00
|
|
|
#endif
|
|
|
|
|
2003-11-16 16:36:39 +00:00
|
|
|
if (argc == 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (argv[0], GRUB_FILE_TYPE_MULTIBOOT_KERNEL);
|
2005-07-31 16:12:29 +00:00
|
|
|
if (! file)
|
2011-12-26 11:58:08 +00:00
|
|
|
return grub_errno;
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_dl_ref (my_mod);
|
2008-08-12 15:40:26 +00:00
|
|
|
|
2009-08-14 15:19:24 +00:00
|
|
|
/* Skip filename. */
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (init_mbi) (argc - 1, argv + 1);
|
2008-08-17 16:32:18 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
grub_relocator_unload (GRUB_MULTIBOOT (relocator));
|
|
|
|
GRUB_MULTIBOOT (relocator) = grub_relocator_new ();
|
2010-01-10 14:06:17 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
if (!GRUB_MULTIBOOT (relocator))
|
2010-01-10 14:06:17 +00:00
|
|
|
goto fail;
|
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
err = GRUB_MULTIBOOT (load) (file, argv[0]);
|
2010-03-08 14:40:57 +00:00
|
|
|
if (err)
|
2005-07-31 16:12:29 +00:00
|
|
|
goto fail;
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
GRUB_MULTIBOOT (set_bootdev) ();
|
2008-03-05 05:09:35 +00:00
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
|
2003-11-16 16:36:39 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (file)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_file_close (file);
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2017-08-14 10:51:26 +00:00
|
|
|
grub_relocator_unload (GRUB_MULTIBOOT (relocator));
|
|
|
|
GRUB_MULTIBOOT (relocator) = NULL;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_dl_unref (my_mod);
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2010-03-08 14:40:57 +00:00
|
|
|
|
|
|
|
return grub_errno;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_file_t file = 0;
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_ssize_t size;
|
2010-01-11 10:29:52 +00:00
|
|
|
void *module = NULL;
|
|
|
|
grub_addr_t target;
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_err_t err;
|
2010-08-20 22:57:12 +00:00
|
|
|
int nounzip = 0;
|
2013-10-28 14:23:46 +00:00
|
|
|
grub_uint64_t lowest_addr = 0;
|
2010-08-20 22:57:12 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
|
2010-08-20 22:57:12 +00:00
|
|
|
|
|
|
|
if (grub_strcmp (argv[0], "--nounzip") == 0)
|
|
|
|
{
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
nounzip = 1;
|
|
|
|
}
|
2003-11-16 16:36:39 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
if (!GRUB_MULTIBOOT (relocator))
|
2010-03-08 14:40:57 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
2012-02-08 18:26:01 +00:00
|
|
|
N_("you need to load the kernel first"));
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (argv[0], GRUB_FILE_TYPE_MULTIBOOT_MODULE
|
|
|
|
| (nounzip ? GRUB_FILE_TYPE_NO_DECOMPRESS : GRUB_FILE_TYPE_NONE));
|
2005-07-31 16:12:29 +00:00
|
|
|
if (! file)
|
2010-03-08 14:40:57 +00:00
|
|
|
return grub_errno;
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2013-10-28 14:23:46 +00:00
|
|
|
#ifndef GRUB_USE_MULTIBOOT2
|
2015-05-27 06:37:55 +00:00
|
|
|
lowest_addr = 0x100000;
|
2013-10-28 14:23:46 +00:00
|
|
|
if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
|
|
|
|
lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
|
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
size = grub_file_size (file);
|
2013-10-28 13:27:19 +00:00
|
|
|
if (size)
|
2010-04-21 17:13:45 +00:00
|
|
|
{
|
|
|
|
grub_relocator_chunk_t ch;
|
2017-08-14 10:51:26 +00:00
|
|
|
err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
|
2020-07-08 01:44:38 +00:00
|
|
|
lowest_addr, UP_TO_TOP32 (size),
|
2010-04-21 17:13:45 +00:00
|
|
|
size, MULTIBOOT_MOD_ALIGN,
|
2013-12-13 11:56:14 +00:00
|
|
|
GRUB_RELOCATOR_PREFERENCE_NONE, 1);
|
2010-04-21 17:13:45 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_file_close (file);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
module = get_virtual_current_address (ch);
|
2011-12-15 19:05:27 +00:00
|
|
|
target = get_physical_target_address (ch);
|
2010-04-21 17:13:45 +00:00
|
|
|
}
|
2013-10-28 13:27:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
module = 0;
|
|
|
|
target = 0;
|
|
|
|
}
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2017-08-14 10:51:26 +00:00
|
|
|
err = GRUB_MULTIBOOT (add_module) (target, size, argc - 1, argv + 1);
|
2010-01-10 17:58:18 +00:00
|
|
|
if (err)
|
2010-03-08 14:40:57 +00:00
|
|
|
{
|
|
|
|
grub_file_close (file);
|
|
|
|
return err;
|
|
|
|
}
|
2003-11-16 16:36:39 +00:00
|
|
|
|
2013-10-28 13:27:19 +00:00
|
|
|
if (size && grub_file_read (file, module, size) != size)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_file_close (file);
|
2011-12-26 11:58:08 +00:00
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
|
argv[0]);
|
|
|
|
return grub_errno;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
grub_file_close (file);
|
2013-10-28 13:27:19 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2010-01-10 17:58:18 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
static grub_command_t cmd_multiboot, cmd_module;
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(multiboot)
|
|
|
|
{
|
|
|
|
cmd_multiboot =
|
|
|
|
#ifdef GRUB_USE_MULTIBOOT2
|
|
|
|
grub_register_command ("multiboot2", grub_cmd_multiboot,
|
|
|
|
0, N_("Load a multiboot 2 kernel."));
|
2010-04-10 01:05:03 +00:00
|
|
|
cmd_module =
|
|
|
|
grub_register_command ("module2", grub_cmd_module,
|
|
|
|
0, N_("Load a multiboot 2 module."));
|
2010-03-08 14:40:57 +00:00
|
|
|
#else
|
|
|
|
grub_register_command ("multiboot", grub_cmd_multiboot,
|
|
|
|
0, N_("Load a multiboot kernel."));
|
|
|
|
cmd_module =
|
|
|
|
grub_register_command ("module", grub_cmd_module,
|
|
|
|
0, N_("Load a multiboot module."));
|
2010-04-10 01:05:03 +00:00
|
|
|
#endif
|
2010-03-08 14:40:57 +00:00
|
|
|
|
|
|
|
my_mod = mod;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2010-01-10 17:58:18 +00:00
|
|
|
|
2010-03-08 14:40:57 +00:00
|
|
|
GRUB_MOD_FINI(multiboot)
|
|
|
|
{
|
|
|
|
grub_unregister_command (cmd_multiboot);
|
|
|
|
grub_unregister_command (cmd_module);
|
|
|
|
}
|