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:
|
|
|
|
* - VBE support
|
|
|
|
* - symbol table
|
|
|
|
* - drives table
|
|
|
|
* - ROM configuration table
|
|
|
|
* - APM table
|
|
|
|
*/
|
|
|
|
|
2010-01-07 19:55:16 +00:00
|
|
|
/* The bits in the required part of flags field we don't support. */
|
2010-01-15 15:30:57 +00:00
|
|
|
#define UNSUPPORTED_FLAGS 0x0000fff8
|
2010-01-07 19:55:16 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/loader.h>
|
2007-07-25 00:44:03 +00:00
|
|
|
#include <grub/multiboot.h>
|
2007-10-31 22:14:50 +00:00
|
|
|
#include <grub/machine/memory.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>
|
2005-08-22 17:28:59 +00:00
|
|
|
#include <grub/gzio.h>
|
2008-03-05 05:09:35 +00:00
|
|
|
#include <grub/env.h>
|
2009-11-25 22:39:59 +00:00
|
|
|
#include <grub/i386/relocator.h>
|
2010-01-14 21:08:31 +00:00
|
|
|
#include <grub/video.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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
|
|
|
|
2007-07-25 00:44:03 +00:00
|
|
|
extern grub_dl_t my_mod;
|
2010-01-10 14:06:17 +00:00
|
|
|
struct grub_relocator *grub_multiboot_relocator = NULL;
|
2010-01-13 19:56:57 +00:00
|
|
|
static grub_uint32_t grub_multiboot_payload_eip;
|
2009-11-25 22:39:59 +00:00
|
|
|
|
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;
|
2009-11-25 22:39:59 +00:00
|
|
|
struct grub_relocator32_state state =
|
|
|
|
{
|
2009-12-24 14:09:06 +00:00
|
|
|
.eax = MULTIBOOT_BOOTLOADER_MAGIC,
|
2009-11-25 22:39:59 +00:00
|
|
|
.ecx = 0,
|
|
|
|
.edx = 0,
|
|
|
|
.eip = grub_multiboot_payload_eip,
|
2009-12-13 17:10:53 +00:00
|
|
|
/* Set esp to some random location in low memory to avoid breaking
|
|
|
|
non-compliant kernels. */
|
|
|
|
.esp = 0x7ff00
|
2009-11-25 22:39:59 +00:00
|
|
|
};
|
|
|
|
|
2010-01-11 10:29:52 +00:00
|
|
|
err = grub_multiboot_make_mbi (&state.ebx);
|
2010-01-10 17:58:18 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2010-01-15 20:11:51 +00:00
|
|
|
#ifdef GRUB_MACHINE_EFI
|
|
|
|
if (! grub_efi_finish_boot_services ())
|
|
|
|
grub_fatal ("cannot exit boot services");
|
|
|
|
#endif
|
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_relocator32_boot (grub_multiboot_relocator, state);
|
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
|
|
|
{
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_multiboot_free_mbi ();
|
|
|
|
|
2010-01-10 14:06:17 +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
|
|
|
}
|
|
|
|
|
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. */
|
|
|
|
static grub_err_t
|
|
|
|
grub_multiboot_load_elf (grub_file_t file, void *buffer)
|
|
|
|
{
|
|
|
|
if (grub_multiboot_is_elf32 (buffer))
|
|
|
|
return grub_multiboot_load_elf32 (file, buffer);
|
|
|
|
else if (grub_multiboot_is_elf64 (buffer))
|
|
|
|
return grub_multiboot_load_elf64 (file, buffer);
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2005-07-31 16:12:29 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
|
|
|
|
}
|
|
|
|
|
2003-11-16 16:36:39 +00:00
|
|
|
void
|
2007-07-25 00:44:03 +00:00
|
|
|
grub_multiboot (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
|
|
|
char buffer[MULTIBOOT_SEARCH];
|
2009-11-13 13:34:51 +00:00
|
|
|
struct multiboot_header *header;
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_ssize_t len;
|
2010-01-10 14:06:17 +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 ();
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2003-11-16 16:36:39 +00:00
|
|
|
if (argc == 0)
|
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2005-08-22 17:28:59 +00:00
|
|
|
file = grub_gzfile_open (argv[0], 1);
|
2005-07-31 16:12:29 +00:00
|
|
|
if (! file)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2007-07-25 00:44:03 +00:00
|
|
|
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
|
2003-11-16 16:36:39 +00:00
|
|
|
if (len < 32)
|
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_OS, "file too small");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for the multiboot header in the buffer. The header should
|
|
|
|
be at least 12 bytes and aligned on a 4-byte boundary. */
|
2009-11-13 13:34:51 +00:00
|
|
|
for (header = (struct multiboot_header *) buffer;
|
2003-11-16 16:36:39 +00:00
|
|
|
((char *) header <= buffer + len - 12) || (header = 0);
|
2009-11-13 13:34:51 +00:00
|
|
|
header = (struct multiboot_header *) ((char *) header + 4))
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2009-12-24 14:09:06 +00:00
|
|
|
if (header->magic == MULTIBOOT_HEADER_MAGIC
|
2003-11-16 16:36:39 +00:00
|
|
|
&& !(header->magic + header->flags + header->checksum))
|
2005-07-31 16:12:29 +00:00
|
|
|
break;
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2003-11-16 16:36:39 +00:00
|
|
|
if (header == 0)
|
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-01-07 19:55:16 +00:00
|
|
|
if (header->flags & UNSUPPORTED_FLAGS)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2005-07-31 16:12:29 +00:00
|
|
|
grub_error (GRUB_ERR_UNKNOWN_OS,
|
2009-12-24 22:53:05 +00:00
|
|
|
"unsupported flag: 0x%x", header->flags);
|
2005-02-14 18:41:33 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_relocator_unload (grub_multiboot_relocator);
|
|
|
|
grub_multiboot_relocator = NULL;
|
2008-08-12 15:40:26 +00:00
|
|
|
|
2009-08-14 15:19:24 +00:00
|
|
|
/* Skip filename. */
|
2010-01-10 17:58:18 +00:00
|
|
|
grub_multiboot_init_mbi (argc - 1, argv + 1);
|
2008-08-17 16:32:18 +00:00
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_multiboot_relocator = grub_relocator_new ();
|
|
|
|
|
|
|
|
if (!grub_multiboot_relocator)
|
|
|
|
goto fail;
|
|
|
|
|
2008-02-19 16:40:45 +00:00
|
|
|
if (header->flags & MULTIBOOT_AOUT_KLUDGE)
|
|
|
|
{
|
2008-08-12 15:40:26 +00:00
|
|
|
int offset = ((char *) header - buffer -
|
|
|
|
(header->header_addr - header->load_addr));
|
|
|
|
int load_size = ((header->load_end_addr == 0) ? file->size - offset :
|
|
|
|
header->load_end_addr - header->load_addr);
|
2010-01-11 10:32:41 +00:00
|
|
|
grub_size_t code_size;
|
2010-01-10 14:06:17 +00:00
|
|
|
void *source;
|
2008-08-12 15:40:26 +00:00
|
|
|
|
|
|
|
if (header->bss_end_addr)
|
2009-03-10 23:47:51 +00:00
|
|
|
code_size = (header->bss_end_addr - header->load_addr);
|
2008-08-12 15:40:26 +00:00
|
|
|
else
|
2009-03-10 23:47:51 +00:00
|
|
|
code_size = load_size;
|
2008-08-12 15:40:26 +00:00
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
|
|
|
|
&source, header->load_addr,
|
|
|
|
code_size);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-08-12 15:40:26 +00:00
|
|
|
|
2009-12-13 17:10:53 +00:00
|
|
|
if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
|
2008-08-12 15:40:26 +00:00
|
|
|
goto fail;
|
|
|
|
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_file_read (file, source, load_size);
|
2008-08-12 15:40:26 +00:00
|
|
|
if (grub_errno)
|
|
|
|
goto fail;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-08-12 15:40:26 +00:00
|
|
|
if (header->bss_end_addr)
|
2010-01-10 14:06:17 +00:00
|
|
|
grub_memset ((grub_uint32_t *) source + load_size, 0,
|
2008-08-12 15:40:26 +00:00
|
|
|
header->bss_end_addr - header->load_addr - load_size);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-11-25 22:39:59 +00:00
|
|
|
grub_multiboot_payload_eip = header->entry_addr;
|
2008-02-19 16:40:45 +00:00
|
|
|
}
|
|
|
|
else if (grub_multiboot_load_elf (file, buffer) != GRUB_ERR_NONE)
|
2005-07-31 16:12:29 +00:00
|
|
|
goto fail;
|
2008-02-19 16:40:45 +00:00
|
|
|
|
2010-01-15 15:30:57 +00:00
|
|
|
if (header->flags & MULTIBOOT_VIDEO_MODE)
|
|
|
|
{
|
|
|
|
switch (header->mode_type)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
grub_env_set ("gfxpayload", "text");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
char buf[sizeof ("XXXXXXXXXXxXXXXXXXXXXxXXXXXXXXXX,XXXXXXXXXXxXXXXXXXXXX,auto")];
|
|
|
|
if (header->depth && header->width && header->height)
|
|
|
|
grub_sprintf (buf, "%dx%dx%d,%dx%d,auto", header->width,
|
|
|
|
header->height, header->depth, header->width,
|
|
|
|
header->height);
|
|
|
|
else if (header->width && header->height)
|
|
|
|
grub_sprintf (buf, "%dx%d,auto", header->width, header->height);
|
|
|
|
else
|
|
|
|
grub_sprintf (buf, "auto");
|
|
|
|
|
|
|
|
grub_env_set ("gfxpayload", buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_multiboot_set_accepts_video (!!(header->flags & MULTIBOOT_VIDEO_MODE));
|
|
|
|
|
2010-01-10 17:58:18 +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
|
|
|
{
|
2010-01-11 10:29:52 +00:00
|
|
|
grub_relocator_unload (grub_multiboot_relocator);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_dl_unref (my_mod);
|
2003-11-16 16:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-25 00:44:03 +00:00
|
|
|
grub_module (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;
|
2003-11-16 16:36:39 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-01-11 10:29:52 +00:00
|
|
|
if (!grub_multiboot_relocator)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2008-02-19 16:40:45 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT,
|
2009-12-24 22:53:05 +00:00
|
|
|
"you need to load the multiboot kernel first");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2005-08-22 17:28:59 +00:00
|
|
|
file = grub_gzfile_open (argv[0], 1);
|
2005-07-31 16:12:29 +00:00
|
|
|
if (! file)
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
size = grub_file_size (file);
|
2010-01-11 10:29:52 +00:00
|
|
|
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &module,
|
|
|
|
&target,
|
|
|
|
0, (0xffffffff - size) + 1,
|
2010-01-12 21:14:26 +00:00
|
|
|
size, MULTIBOOT_MOD_ALIGN,
|
|
|
|
GRUB_RELOCATOR_PREFERENCE_NONE);
|
2010-01-11 10:29:52 +00:00
|
|
|
if (err)
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
|
2010-01-11 10:29:52 +00:00
|
|
|
err = grub_multiboot_add_module (target, size, argc - 1, argv + 1);
|
2010-01-10 17:58:18 +00:00
|
|
|
if (err)
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_file_read (file, module, size) != size)
|
2003-11-16 16:36:39 +00:00
|
|
|
{
|
2009-12-24 22:53:05 +00:00
|
|
|
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
|
2003-11-16 16:36:39 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-02-19 16:40:45 +00:00
|
|
|
|
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
|
|
|
}
|
2010-01-10 17:58:18 +00:00
|
|
|
|