Merge mainline into mbivid

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-09-16 00:22:49 +02:00
commit 9ba27423f5
931 changed files with 122070 additions and 40781 deletions

60
grub-core/loader/aout.c Normal file
View file

@ -0,0 +1,60 @@
/*
* Copyright (C) 2008 Free Software Foundation, Inc.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/file.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/aout.h>
int
grub_aout_get_type (union grub_aout_header *header)
{
int magic;
magic = AOUT_GETMAGIC (header->aout32);
if ((magic == AOUT32_OMAGIC) || (magic == AOUT32_NMAGIC) ||
(magic == AOUT32_ZMAGIC) || (magic == AOUT32_QMAGIC))
return AOUT_TYPE_AOUT32;
else if ((magic == AOUT64_OMAGIC) || (magic == AOUT64_NMAGIC) ||
(magic == AOUT64_ZMAGIC))
return AOUT_TYPE_AOUT64;
else
return AOUT_TYPE_NONE;
}
grub_err_t
grub_aout_load (grub_file_t file, int offset,
void *load_addr,
int load_size, grub_size_t bss_size)
{
if ((grub_file_seek (file, offset)) == (grub_off_t) - 1)
return grub_errno;
if (!load_size)
load_size = file->size - offset;
grub_file_read (file, load_addr, load_size);
if (grub_errno)
return grub_errno;
if (bss_size)
grub_memset ((char *) load_addr + load_size, 0, bss_size);
return GRUB_ERR_NONE;
}

View file

@ -0,0 +1,332 @@
/* appleloader.c - apple legacy boot loader. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008,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/err.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/command.h>
#include <grub/i18n.h>
static grub_dl_t my_mod;
static grub_efi_handle_t image_handle;
static grub_efi_char16_t *cmdline;
static grub_err_t
grub_appleloader_unload (void)
{
grub_efi_boot_services_t *b;
b = grub_efi_system_table->boot_services;
efi_call_1 (b->unload_image, image_handle);
grub_free (cmdline);
cmdline = 0;
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_appleloader_boot (void)
{
grub_efi_boot_services_t *b;
b = grub_efi_system_table->boot_services;
efi_call_3 (b->start_image, image_handle, 0, 0);
grub_appleloader_unload ();
return grub_errno;
}
struct piwg_full_device_path
{
struct grub_efi_memory_mapped_device_path comp1;
struct grub_efi_piwg_device_path comp2;
struct grub_efi_device_path end;
};
/* early 2006 Core Duo / Core Solo models */
static struct piwg_full_device_path devpath_1 =
{
.comp1 =
{
.header = {
.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_memory_mapped_device_path), 0}
},
.memory_type = GRUB_EFI_MEMORY_MAPPED_IO,
.start_address = 0xffe00000,
.end_address = 0xfff9ffff
},
.comp2 =
{
.header = {
.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_PIWG_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_piwg_device_path), 0}
},
.guid = {0x2B0585EB, 0xD8B8, 0x49A9, {0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7}}
},
.end =
{
.type = GRUB_EFI_END_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_device_path), 0}
}
};
/* mid-2006 Mac Pro (and probably other Core 2 models) */
static struct piwg_full_device_path devpath_2 =
{
.comp1 =
{
.header = {
.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_memory_mapped_device_path), 0}
},
.memory_type = GRUB_EFI_MEMORY_MAPPED_IO,
.start_address = 0xffe00000,
.end_address = 0xfff7ffff
},
.comp2 =
{
.header = {
.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_PIWG_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_piwg_device_path), 0}
},
.guid = {0x2B0585EB, 0xD8B8, 0x49A9, {0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7}}
},
.end =
{
.type = GRUB_EFI_END_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_device_path), 0}
}
};
/* mid-2007 MBP ("Santa Rosa" based models) */
static struct piwg_full_device_path devpath_3 =
{
.comp1 =
{
.header = {
.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_memory_mapped_device_path), 0}
},
.memory_type = GRUB_EFI_MEMORY_MAPPED_IO,
.start_address = 0xffe00000,
.end_address = 0xfff8ffff
},
.comp2 =
{
.header = {
.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_PIWG_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_piwg_device_path), 0}
},
.guid = {0x2B0585EB, 0xD8B8, 0x49A9, {0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7}}
},
.end =
{
.type = GRUB_EFI_END_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_device_path), 0}
}
};
/* early-2008 MBA */
static struct piwg_full_device_path devpath_4 =
{
.comp1 =
{
.header = {
.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_memory_mapped_device_path), 0}
},
.memory_type = GRUB_EFI_MEMORY_MAPPED_IO,
.start_address = 0xffc00000,
.end_address = 0xfff8ffff
},
.comp2 =
{
.header = {
.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_PIWG_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_piwg_device_path), 0}
},
.guid = {0x2B0585EB, 0xD8B8, 0x49A9, {0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7}}
},
.end =
{
.type = GRUB_EFI_END_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_device_path), 0}
}
};
/* late-2008 MB/MBP (NVidia chipset) */
static struct piwg_full_device_path devpath_5 =
{
.comp1 =
{
.header = {
.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_memory_mapped_device_path), 0}
},
.memory_type = GRUB_EFI_MEMORY_MAPPED_IO,
.start_address = 0xffcb4000,
.end_address = 0xffffbfff
},
.comp2 =
{
.header = {
.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_PIWG_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_piwg_device_path), 0}
},
.guid = {0x2B0585EB, 0xD8B8, 0x49A9, {0x8B, 0x8C, 0xE2, 0x1B,
0x01, 0xAE, 0xF2, 0xB7}}
},
.end =
{
.type = GRUB_EFI_END_DEVICE_PATH_TYPE,
.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE,
.length = {sizeof (struct grub_efi_device_path), 0}
}
};
struct devdata
{
char *model;
grub_efi_device_path_t *devpath;
};
struct devdata devs[] =
{
{"Core Duo/Solo", (grub_efi_device_path_t *) &devpath_1},
{"Mac Pro", (grub_efi_device_path_t *) &devpath_2},
{"MBP", (grub_efi_device_path_t *) &devpath_3},
{"MBA", (grub_efi_device_path_t *) &devpath_4},
{"MB NV", (grub_efi_device_path_t *) &devpath_5},
{NULL, NULL},
};
static grub_err_t
grub_cmd_appleloader (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_efi_boot_services_t *b;
grub_efi_loaded_image_t *loaded_image;
struct devdata *pdev;
grub_dl_ref (my_mod);
/* Initialize some global variables. */
image_handle = 0;
b = grub_efi_system_table->boot_services;
for (pdev = devs ; pdev->devpath ; pdev++)
if (efi_call_6 (b->load_image, 0, grub_efi_image_handle, pdev->devpath,
NULL, 0, &image_handle) == GRUB_EFI_SUCCESS)
break;
if (! pdev->devpath)
{
grub_error (GRUB_ERR_BAD_OS, "can't find model");
goto fail;
}
grub_printf ("Model : %s\n", pdev->model);
loaded_image = grub_efi_get_loaded_image (image_handle);
if (! loaded_image)
{
grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
goto fail;
}
if (argc > 0)
{
int i, len;
grub_efi_char16_t *p16;
for (i = 0, len = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
len *= sizeof (grub_efi_char16_t);
cmdline = p16 = grub_malloc (len);
if (! cmdline)
goto fail;
for (i = 0; i < argc; i++)
{
char *p8;
p8 = argv[i];
while (*p8)
*(p16++) = *(p8++);
*(p16++) = ' ';
}
*(--p16) = 0;
loaded_image->load_options = cmdline;
loaded_image->load_options_size = len;
}
grub_loader_set (grub_appleloader_boot, grub_appleloader_unload, 0);
return 0;
fail:
grub_dl_unref (my_mod);
return grub_errno;
}
static grub_command_t cmd;
GRUB_MOD_INIT(appleloader)
{
cmd = grub_register_command ("appleloader", grub_cmd_appleloader,
"[OPTS]", N_("Boot legacy system."));
my_mod = mod;
}
GRUB_MOD_FINI(appleloader)
{
grub_unregister_command (cmd);
}

View file

@ -0,0 +1,347 @@
/* chainloader.c - boot another boot loader */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2004,2006,2007,2008 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/>.
*/
/* TODO: support load options. */
#include <grub/loader.h>
#include <grub/file.h>
#include <grub/err.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/charset.h>
#include <grub/mm.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/efi/disk.h>
#include <grub/command.h>
#include <grub/i18n.h>
static grub_dl_t my_mod;
static grub_efi_physical_address_t address;
static grub_efi_uintn_t pages;
static grub_efi_device_path_t *file_path;
static grub_efi_handle_t image_handle;
static grub_efi_char16_t *cmdline;
static grub_err_t
grub_chainloader_unload (void)
{
grub_efi_boot_services_t *b;
b = grub_efi_system_table->boot_services;
efi_call_1 (b->unload_image, image_handle);
efi_call_2 (b->free_pages, address, pages);
grub_free (file_path);
grub_free (cmdline);
cmdline = 0;
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_chainloader_boot (void)
{
grub_efi_boot_services_t *b;
grub_efi_status_t status;
grub_efi_uintn_t exit_data_size;
grub_efi_char16_t *exit_data;
b = grub_efi_system_table->boot_services;
status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
if (status != GRUB_EFI_SUCCESS)
{
if (exit_data)
{
char *buf;
buf = grub_malloc (exit_data_size * 4 + 1);
if (buf)
{
*grub_utf16_to_utf8 ((grub_uint8_t *) buf,
exit_data, exit_data_size) = 0;
grub_error (GRUB_ERR_BAD_OS, buf);
grub_free (buf);
}
else
grub_error (GRUB_ERR_BAD_OS, "unknown error");
}
}
if (exit_data)
efi_call_1 (b->free_pool, exit_data);
grub_chainloader_unload ();
return grub_errno;
}
static void
copy_file_path (grub_efi_file_path_device_path_t *fp,
const char *str, grub_efi_uint16_t len)
{
grub_efi_char16_t *p;
grub_efi_uint16_t size;
fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
size = len * sizeof (grub_efi_char16_t) + sizeof (*fp);
fp->header.length[0] = (grub_efi_uint8_t) (size & 0xff);
fp->header.length[1] = (grub_efi_uint8_t) (size >> 8);
for (p = fp->path_name; len > 0; len--, p++, str++)
{
/* FIXME: this assumes that the path is in ASCII. */
*p = (grub_efi_char16_t) (*str == '/' ? '\\' : *str);
}
}
static grub_efi_device_path_t *
make_file_path (grub_efi_device_path_t *dp, const char *filename)
{
char *dir_start;
char *dir_end;
grub_size_t size;
grub_efi_device_path_t *d;
dir_start = grub_strchr (filename, ')');
if (! dir_start)
dir_start = (char *) filename;
else
dir_start++;
dir_end = grub_strrchr (dir_start, '/');
if (! dir_end)
{
grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
return 0;
}
size = 0;
d = dp;
while (1)
{
size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
break;
d = GRUB_EFI_NEXT_DEVICE_PATH (d);
}
file_path = grub_malloc (size
+ ((grub_strlen (dir_start) + 1)
* sizeof (grub_efi_char16_t))
+ sizeof (grub_efi_file_path_device_path_t) * 2);
if (! file_path)
return 0;
grub_memcpy (file_path, dp, size);
/* Fill the file path for the directory. */
d = (grub_efi_device_path_t *) ((char *) file_path
+ ((char *) d - (char *) dp));
grub_efi_print_device_path (d);
copy_file_path ((grub_efi_file_path_device_path_t *) d,
dir_start, dir_end - dir_start);
/* Fill the file path for the file. */
d = GRUB_EFI_NEXT_DEVICE_PATH (d);
copy_file_path ((grub_efi_file_path_device_path_t *) d,
dir_end + 1, grub_strlen (dir_end + 1));
/* Fill the end of device path nodes. */
d = GRUB_EFI_NEXT_DEVICE_PATH (d);
d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
d->length[0] = sizeof (*d);
d->length[1] = 0;
return file_path;
}
static grub_err_t
grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
grub_efi_status_t status;
grub_efi_boot_services_t *b;
grub_efi_handle_t dev_handle = 0;
grub_device_t dev = 0;
grub_efi_device_path_t *dp = 0;
grub_efi_loaded_image_t *loaded_image;
char *filename;
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
filename = argv[0];
grub_dl_ref (my_mod);
/* Initialize some global variables. */
address = 0;
image_handle = 0;
file_path = 0;
b = grub_efi_system_table->boot_services;
file = grub_file_open (filename);
if (! file)
goto fail;
/* Get the root device's device path. */
dev = grub_device_open (0);
if (! dev)
goto fail;
if (dev->disk)
{
dev_handle = grub_efidisk_get_device_handle (dev->disk);
if (dev_handle)
dp = grub_efi_get_device_path (dev_handle);
}
if (! dev->disk || ! dev_handle || ! dp)
{
grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
goto fail;
}
file_path = make_file_path (dp, filename);
if (! file_path)
goto fail;
grub_printf ("file path: ");
grub_efi_print_device_path (file_path);
size = grub_file_size (file);
pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
GRUB_EFI_LOADER_CODE,
pages, &address);
if (status != GRUB_EFI_SUCCESS)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate %u pages", pages);
goto fail;
}
if (grub_file_read (file, (void *) ((grub_addr_t) address), size) != size)
{
if (grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_OS, "too small");
goto fail;
}
status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
(void *) ((grub_addr_t) address), size,
&image_handle);
if (status != GRUB_EFI_SUCCESS)
{
if (status == GRUB_EFI_OUT_OF_RESOURCES)
grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
else
grub_error (GRUB_ERR_BAD_OS, "cannot load image");
goto fail;
}
/* LoadImage does not set a device handler when the image is
loaded from memory, so it is necessary to set it explicitly here.
This is a mess. */
loaded_image = grub_efi_get_loaded_image (image_handle);
if (! loaded_image)
{
grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
goto fail;
}
loaded_image->device_handle = dev_handle;
grub_file_close (file);
if (argc > 1)
{
int i, len;
grub_efi_char16_t *p16;
for (i = 1, len = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
len *= sizeof (grub_efi_char16_t);
cmdline = p16 = grub_malloc (len);
if (! cmdline)
goto fail;
for (i = 1; i < argc; i++)
{
char *p8;
p8 = argv[i];
while (*p8)
*(p16++) = *(p8++);
*(p16++) = ' ';
}
*(--p16) = 0;
loaded_image->load_options = cmdline;
loaded_image->load_options_size = len;
}
grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
return 0;
fail:
if (dev)
grub_device_close (dev);
if (file)
grub_file_close (file);
if (file_path)
grub_free (file_path);
if (address)
efi_call_2 (b->free_pages, address, pages);
grub_dl_unref (my_mod);
return grub_errno;
}
static grub_command_t cmd;
GRUB_MOD_INIT(chainloader)
{
cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
0, N_("Load another boot loader."));
my_mod = mod;
}
GRUB_MOD_FINI(chainloader)
{
grub_unregister_command (cmd);
}

1991
grub-core/loader/i386/bsd.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
#define SUFFIX(x) x ## 32
#define GRUB_TARGET_WORDSIZE 32
#define OBJSYM 0
#include <grub/types.h>
typedef grub_uint32_t grub_freebsd_addr_t;
#include "bsdXX.c"

View file

@ -0,0 +1,6 @@
#define SUFFIX(x) x ## 64
#define GRUB_TARGET_WORDSIZE 64
#define OBJSYM 1
#include <grub/types.h>
typedef grub_uint64_t grub_freebsd_addr_t;
#include "bsdXX.c"

View file

@ -0,0 +1,620 @@
#include <grub/loader.h>
#include <grub/i386/bsd.h>
#include <grub/mm.h>
#include <grub/elf.h>
#include <grub/misc.h>
#include <grub/i386/relocator.h>
#define ALIGN_PAGE(a) ALIGN_UP (a, 4096)
static inline grub_err_t
load (grub_file_t file, void *where, grub_off_t off, grub_size_t size)
{
if (grub_file_seek (file, off) == (grub_off_t) -1)
return grub_errno;
if (grub_file_read (file, where, size) != (grub_ssize_t) size)
{
if (grub_errno)
return grub_errno;
else
return grub_error (GRUB_ERR_BAD_OS, "file is truncated");
}
return GRUB_ERR_NONE;
}
static inline grub_err_t
read_headers (grub_file_t file, Elf_Ehdr *e, char **shdr)
{
if (grub_file_seek (file, 0) == (grub_off_t) -1)
return grub_errno;
if (grub_file_read (file, (char *) e, sizeof (*e)) != sizeof (*e))
{
if (grub_errno)
return grub_errno;
else
return grub_error (GRUB_ERR_BAD_OS, "file is too short");
}
if (e->e_ident[EI_MAG0] != ELFMAG0
|| e->e_ident[EI_MAG1] != ELFMAG1
|| e->e_ident[EI_MAG2] != ELFMAG2
|| e->e_ident[EI_MAG3] != ELFMAG3
|| e->e_ident[EI_VERSION] != EV_CURRENT
|| e->e_version != EV_CURRENT)
return grub_error (GRUB_ERR_BAD_OS, "invalid arch independent ELF magic");
if (e->e_ident[EI_CLASS] != SUFFIX (ELFCLASS))
return grub_error (GRUB_ERR_BAD_OS, "invalid arch dependent ELF magic");
*shdr = grub_malloc (e->e_shnum * e->e_shentsize);
if (! *shdr)
return grub_errno;
if (grub_file_seek (file, e->e_shoff) == (grub_off_t) -1)
return grub_errno;
if (grub_file_read (file, *shdr, e->e_shnum * e->e_shentsize)
!= e->e_shnum * e->e_shentsize)
{
if (grub_errno)
return grub_errno;
else
return grub_error (GRUB_ERR_BAD_OS, "file is truncated");
}
return GRUB_ERR_NONE;
}
/* On i386 FreeBSD uses "elf module" approarch for 32-bit variant
and "elf obj module" for 64-bit variant. However it may differ on other
platforms. So I keep both versions. */
#if OBJSYM
grub_err_t
SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
grub_file_t file, int argc,
char *argv[], grub_addr_t *kern_end)
{
Elf_Ehdr e;
Elf_Shdr *s;
char *shdr = 0;
grub_addr_t curload, module;
grub_err_t err;
grub_size_t chunk_size = 0;
void *chunk_src;
err = read_headers (file, &e, &shdr);
if (err)
return err;
curload = module = ALIGN_PAGE (*kern_end);
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
{
if (s->sh_size == 0)
continue;
if (s->sh_addralign)
chunk_size = ALIGN_UP (chunk_size + *kern_end, s->sh_addralign)
- *kern_end;
chunk_size += s->sh_size;
}
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
module, chunk_size);
if (err)
return err;
chunk_src = get_virtual_current_address (ch);
}
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
{
if (s->sh_size == 0)
continue;
if (s->sh_addralign)
curload = ALIGN_UP (curload, s->sh_addralign);
s->sh_addr = curload;
grub_dprintf ("bsd", "loading section to %x, size %d, align %d\n",
(unsigned) curload, (int) s->sh_size,
(int) s->sh_addralign);
switch (s->sh_type)
{
default:
case SHT_PROGBITS:
err = load (file, (grub_uint8_t *) chunk_src + curload - *kern_end,
s->sh_offset, s->sh_size);
if (err)
return err;
break;
case SHT_NOBITS:
grub_memset ((grub_uint8_t *) chunk_src + curload - *kern_end, 0,
s->sh_size);
break;
}
curload += s->sh_size;
}
*kern_end = ALIGN_PAGE (curload);
err = grub_freebsd_add_meta_module (argv[0], FREEBSD_MODTYPE_ELF_MODULE_OBJ,
argc - 1, argv + 1, module,
curload - module);
if (! err)
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA
| FREEBSD_MODINFOMD_ELFHDR,
&e, sizeof (e));
if (! err)
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA
| FREEBSD_MODINFOMD_SHDR,
shdr, e.e_shnum * e.e_shentsize);
return err;
}
#else
grub_err_t
SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator,
grub_file_t file, int argc, char *argv[],
grub_addr_t *kern_end)
{
Elf_Ehdr e;
Elf_Shdr *s;
char *shdr = 0;
grub_addr_t curload, module;
grub_err_t err;
grub_size_t chunk_size = 0;
void *chunk_src;
err = read_headers (file, &e, &shdr);
if (err)
return err;
curload = module = ALIGN_PAGE (*kern_end);
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
{
if (s->sh_size == 0)
continue;
if (! (s->sh_flags & SHF_ALLOC))
continue;
if (chunk_size < s->sh_addr + s->sh_size)
chunk_size = s->sh_addr + s->sh_size;
}
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
module, chunk_size);
if (err)
return err;
chunk_src = get_virtual_current_address (ch);
}
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
{
if (s->sh_size == 0)
continue;
if (! (s->sh_flags & SHF_ALLOC))
continue;
grub_dprintf ("bsd", "loading section to %x, size %d, align %d\n",
(unsigned) curload, (int) s->sh_size,
(int) s->sh_addralign);
switch (s->sh_type)
{
default:
case SHT_PROGBITS:
err = load (file, (grub_uint8_t *) chunk_src + module
+ s->sh_addr - *kern_end,
s->sh_offset, s->sh_size);
if (err)
return err;
break;
case SHT_NOBITS:
grub_memset ((grub_uint8_t *) chunk_src + module
+ s->sh_addr - *kern_end, 0, s->sh_size);
break;
}
if (curload < module + s->sh_addr + s->sh_size)
curload = module + s->sh_addr + s->sh_size;
}
load (file, UINT_TO_PTR (module), 0, sizeof (e));
if (curload < module + sizeof (e))
curload = module + sizeof (e);
load (file, UINT_TO_PTR (curload), e.e_shoff,
e.e_shnum * e.e_shentsize);
e.e_shoff = curload - module;
curload += e.e_shnum * e.e_shentsize;
load (file, UINT_TO_PTR (curload), e.e_phoff,
e.e_phnum * e.e_phentsize);
e.e_phoff = curload - module;
curload += e.e_phnum * e.e_phentsize;
*kern_end = curload;
grub_freebsd_add_meta_module (argv[0], FREEBSD_MODTYPE_ELF_MODULE,
argc - 1, argv + 1, module,
curload - module);
return SUFFIX (grub_freebsd_load_elf_meta) (relocator, file, kern_end);
}
#endif
grub_err_t
SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator,
grub_file_t file, grub_addr_t *kern_end)
{
grub_err_t err;
Elf_Ehdr e;
Elf_Shdr *s;
char *shdr = 0;
unsigned symoff, stroff, symsize, strsize;
grub_freebsd_addr_t symstart, symend, symentsize, dynamic;
Elf_Sym *sym;
void *sym_chunk;
grub_uint8_t *curload;
grub_freebsd_addr_t symtarget;
const char *str;
unsigned i;
grub_size_t chunk_size;
err = read_headers (file, &e, &shdr);
if (err)
return err;
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
FREEBSD_MODINFOMD_ELFHDR, &e,
sizeof (e));
if (err)
return err;
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
if (s->sh_type == SHT_SYMTAB)
break;
if (s >= (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize))
return grub_error (GRUB_ERR_BAD_OS, "no symbol table");
symoff = s->sh_offset;
symsize = s->sh_size;
symentsize = s->sh_entsize;
s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link);
stroff = s->sh_offset;
strsize = s->sh_size;
chunk_size = ALIGN_UP (symsize + strsize, sizeof (grub_freebsd_addr_t))
+ 2 * sizeof (grub_freebsd_addr_t);
symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t));
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
symtarget, chunk_size);
if (err)
return err;
sym_chunk = get_virtual_current_address (ch);
}
symstart = symtarget;
symend = symstart + chunk_size;
curload = sym_chunk;
*((grub_freebsd_addr_t *) curload) = symsize;
curload += sizeof (grub_freebsd_addr_t);
if (grub_file_seek (file, symoff) == (grub_off_t) -1)
return grub_errno;
sym = (Elf_Sym *) curload;
if (grub_file_read (file, curload, symsize) != (grub_ssize_t) symsize)
{
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
curload += symsize;
*((grub_freebsd_addr_t *) curload) = strsize;
curload += sizeof (grub_freebsd_addr_t);
if (grub_file_seek (file, stroff) == (grub_off_t) -1)
return grub_errno;
str = (char *) curload;
if (grub_file_read (file, curload, strsize) != (grub_ssize_t) strsize)
{
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
for (i = 0;
i * symentsize < symsize;
i++, sym = (Elf_Sym *) ((char *) sym + symentsize))
{
const char *name = str + sym->st_name;
if (grub_strcmp (name, "_DYNAMIC") == 0)
break;
}
if (i * symentsize < symsize)
{
dynamic = sym->st_value;
grub_dprintf ("bsd", "dynamic = %llx\n", (unsigned long long) dynamic);
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
FREEBSD_MODINFOMD_DYNAMIC, &dynamic,
sizeof (dynamic));
if (err)
return err;
}
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
FREEBSD_MODINFOMD_SSYM, &symstart,
sizeof (symstart));
if (err)
return err;
err = grub_bsd_add_meta (FREEBSD_MODINFO_METADATA |
FREEBSD_MODINFOMD_ESYM, &symend,
sizeof (symend));
if (err)
return err;
*kern_end = ALIGN_PAGE (symend);
return GRUB_ERR_NONE;
}
grub_err_t
SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator,
grub_file_t file, grub_addr_t *kern_end)
{
grub_err_t err;
Elf_Ehdr e;
Elf_Shdr *s, *symsh, *strsh;
char *shdr;
unsigned symsize, strsize;
Elf_Sym *sym;
void *sym_chunk;
grub_uint8_t *curload;
const char *str;
grub_size_t chunk_size;
Elf_Ehdr *e2;
struct grub_netbsd_btinfo_symtab symtab;
grub_addr_t symtarget;
err = read_headers (file, &e, &shdr);
if (err)
return err;
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
if (s->sh_type == SHT_SYMTAB)
break;
if (s >= (Elf_Shdr *) ((char *) shdr
+ e.e_shnum * e.e_shentsize))
return GRUB_ERR_NONE;
symsize = s->sh_size;
symsh = s;
s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link);
strsize = s->sh_size;
strsh = s;
chunk_size = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t))
+ ALIGN_UP (strsize, sizeof (grub_freebsd_addr_t))
+ sizeof (e) + e.e_shnum * e.e_shentsize;
symtarget = ALIGN_UP (*kern_end, sizeof (grub_freebsd_addr_t));
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
symtarget, chunk_size);
if (err)
return err;
sym_chunk = get_virtual_current_address (ch);
}
symtab.nsyms = 1;
symtab.ssyms = symtarget;
symtab.esyms = symtarget + chunk_size;
curload = sym_chunk;
e2 = (Elf_Ehdr *) curload;
grub_memcpy (curload, &e, sizeof (e));
e2->e_phoff = 0;
e2->e_phnum = 0;
e2->e_phentsize = 0;
e2->e_shstrndx = 0;
e2->e_shoff = sizeof (e);
curload += sizeof (e);
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
{
Elf_Shdr *s2;
s2 = (Elf_Shdr *) curload;
grub_memcpy (curload, s, e.e_shentsize);
if (s == symsh)
s2->sh_offset = sizeof (e) + e.e_shnum * e.e_shentsize;
else if (s == strsh)
s2->sh_offset = ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t))
+ sizeof (e) + e.e_shnum * e.e_shentsize;
else
s2->sh_offset = 0;
s2->sh_addr = s2->sh_offset;
curload += e.e_shentsize;
}
if (grub_file_seek (file, symsh->sh_offset) == (grub_off_t) -1)
return grub_errno;
sym = (Elf_Sym *) curload;
if (grub_file_read (file, curload, symsize) != (grub_ssize_t) symsize)
{
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
curload += ALIGN_UP (symsize, sizeof (grub_freebsd_addr_t));
if (grub_file_seek (file, strsh->sh_offset) == (grub_off_t) -1)
return grub_errno;
str = (char *) curload;
if (grub_file_read (file, curload, strsize) != (grub_ssize_t) strsize)
{
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
err = grub_bsd_add_meta (NETBSD_BTINFO_SYMTAB,
&symtab,
sizeof (symtab));
if (err)
return err;
*kern_end = ALIGN_PAGE (symtarget + chunk_size);
return GRUB_ERR_NONE;
}
grub_err_t
SUFFIX(grub_openbsd_find_ramdisk) (grub_file_t file,
grub_addr_t kern_start,
void *kern_chunk_src,
struct grub_openbsd_ramdisk_descriptor *desc)
{
unsigned symoff, stroff, symsize, strsize, symentsize;
{
grub_err_t err;
Elf_Ehdr e;
Elf_Shdr *s;
char *shdr;
err = read_headers (file, &e, &shdr);
if (err)
return err;
for (s = (Elf_Shdr *) shdr; s < (Elf_Shdr *) (shdr
+ e.e_shnum * e.e_shentsize);
s = (Elf_Shdr *) ((char *) s + e.e_shentsize))
if (s->sh_type == SHT_SYMTAB)
break;
if (s >= (Elf_Shdr *) ((char *) shdr + e.e_shnum * e.e_shentsize))
{
grub_free (shdr);
return GRUB_ERR_NONE;
}
symsize = s->sh_size;
symentsize = s->sh_entsize;
symoff = s->sh_offset;
s = (Elf_Shdr *) (shdr + e.e_shentsize * s->sh_link);
stroff = s->sh_offset;
strsize = s->sh_size;
grub_free (shdr);
}
{
Elf_Sym *syms, *sym, *imagesym = NULL, *sizesym = NULL;
unsigned i;
char *strs;
syms = grub_malloc (symsize);
if (!syms)
return grub_errno;
if (grub_file_seek (file, symoff) == (grub_off_t) -1)
{
grub_free (syms);
return grub_errno;
}
if (grub_file_read (file, syms, symsize) != (grub_ssize_t) symsize)
{
grub_free (syms);
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
strs = grub_malloc (strsize);
if (!strs)
{
grub_free (syms);
return grub_errno;
}
if (grub_file_seek (file, stroff) == (grub_off_t) -1)
return grub_errno;
if (grub_file_read (file, strs, strsize) != (grub_ssize_t) strsize)
{
grub_free (syms);
grub_free (strs);
if (! grub_errno)
return grub_error (GRUB_ERR_BAD_OS, "invalid ELF");
return grub_errno;
}
for (i = 0, sym = syms; i < symsize / symentsize;
i++, sym = (Elf_Sym *) ((char *) sym + symentsize))
{
if (ELF_ST_TYPE (sym->st_info) != STT_OBJECT)
continue;
if (!sym->st_name)
continue;
if (grub_strcmp (strs + sym->st_name, "rd_root_image") == 0)
imagesym = sym;
if (grub_strcmp (strs + sym->st_name, "rd_root_size") == 0)
sizesym = sym;
if (imagesym && sizesym)
break;
}
if (!imagesym || !sizesym)
{
grub_free (syms);
grub_free (strs);
return GRUB_ERR_NONE;
}
if (sizeof (*desc->size) != sizesym->st_size)
{
grub_free (syms);
grub_free (strs);
return grub_error (GRUB_ERR_BAD_OS, "unexpected size of rd_root_size");
}
desc->max_size = imagesym->st_size;
desc->target = (imagesym->st_value & 0xFFFFFF) - kern_start
+ (grub_uint8_t *) kern_chunk_src;
desc->size = (grub_uint32_t *) ((sizesym->st_value & 0xFFFFFF) - kern_start
+ (grub_uint8_t *) kern_chunk_src);
grub_free (syms);
grub_free (strs);
return GRUB_ERR_NONE;
}
}

View file

@ -0,0 +1,45 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008, 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/symbol.h>
.p2align 2
.code32
/*
* Use cdecl calling convention for *BSD kernels.
*/
FUNCTION(grub_unix_real_boot)
/* Interrupts should be disabled. */
cli
/* Discard `grub_unix_real_boot' return address. */
popl %eax
/* Fetch `entry' address ... */
popl %eax
/*
* ... and put our return address in its place. The kernel will
* ignore it, but it expects %esp to point to it.
*/
call *%eax

View file

@ -0,0 +1,93 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
* Copyright (C) 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/>.
*/
/* Based on the code from FreeBSD originally distributed under the
following terms: */
/*-
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
static void
fill_bsd64_pagetable (grub_uint8_t *src, grub_addr_t target)
{
grub_uint64_t *pt2, *pt3, *pt4;
grub_addr_t pt2t, pt3t, pt4t;
int i;
#define PG_V 0x001
#define PG_RW 0x002
#define PG_U 0x004
#define PG_PS 0x080
pt4 = (grub_uint64_t *) src;
pt3 = (grub_uint64_t *) (src + 4096);
pt2 = (grub_uint64_t *) (src + 8192);
pt4t = target;
pt3t = target + 4096;
pt2t = target + 8192;
grub_memset (src, 0, 4096 * 3);
/*
* This is kinda brutal, but every single 1GB VM memory segment points to
* the same first 1GB of physical memory. But it is how BSD expects
* it to be.
*/
for (i = 0; i < 512; i++)
{
/* Each slot of the level 4 pages points to the same level 3 page */
pt4[i] = (grub_addr_t) pt3t;
pt4[i] |= PG_V | PG_RW | PG_U;
/* Each slot of the level 3 pages points to the same level 2 page */
pt3[i] = (grub_addr_t) pt2t;
pt3[i] |= PG_V | PG_RW | PG_U;
/* The level 2 page slots are mapped with 2MB pages for 1GB. */
pt2[i] = i * (2 * 1024 * 1024);
pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
}
}

View file

@ -0,0 +1,124 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (c) 2003 Peter Wemm <peter@FreeBSD.org>
* Copyright (C) 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/>.
*/
/* Based on the code from FreeBSD originally distributed under the
following terms: */
/*-
* Copyright (c) 2003 Peter Wemm <peter@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#define MSR_EFER 0xc0000080
#define EFER_LME 0x00000100
#define CR4_PAE 0x00000020
#define CR4_PSE 0x00000010
#define CR0_PG 0x80000000
#include <grub/symbol.h>
.p2align 2
.code32
VARIABLE(grub_bsd64_trampoline_start)
/* Discard `grub_unix_real_boot' return address. */
popl %eax
/* entry */
popl %edi
/* entry_hi */
popl %esi
cli
/* Turn on EFER.LME. */
movl $MSR_EFER, %ecx
rdmsr
orl $EFER_LME, %eax
wrmsr
/* Turn on PAE. */
movl %cr4, %eax
orl $(CR4_PAE | CR4_PSE), %eax
movl %eax, %cr4
/* Set %cr3 for PT4. */
popl %eax
movl %eax, %cr3
/* Push a dummy return address. */
pushl %eax
/* Turn on paging (implicitly sets EFER.LMA). */
movl %cr0, %eax
orl $CR0_PG, %eax
movl %eax, %cr0
/* Now we're in compatibility mode. set %cs for long mode. */
/* lgdt */
.byte 0x0f
.byte 0x01
.byte 0x15
VARIABLE (grub_bsd64_trampoline_gdt)
.long 0x0
/* ljmp */
.byte 0xea
VARIABLE (grub_bsd64_trampoline_selfjump)
.long 0x0
.word 0x08
.code64
bsd64_longmode:
/* We're still running V=P, jump to entry point. */
movl %esi, %eax
salq $32, %rax
orq %rdi, %rax
pushq %rax
ret
VARIABLE(grub_bsd64_trampoline_end)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,178 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/env.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/efi/uga_draw.h>
#include <grub/pci.h>
#include <grub/misc.h>
/* Setup video for xnu. Big parts are copied from linux.c. */
static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
#define RGB_MASK 0xffffff
#define RGB_MAGIC 0x121314
#define LINE_MIN 800
#define LINE_MAX 4096
#define FBTEST_STEP (0x10000 >> 2)
#define FBTEST_COUNT 8
static int
find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len)
{
grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base;
int i;
for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
{
if ((*base & RGB_MASK) == RGB_MAGIC)
{
int j;
for (j = LINE_MIN; j <= LINE_MAX; j++)
{
if ((base[j] & RGB_MASK) == RGB_MAGIC)
{
*fb_base = (grub_uint32_t) (grub_target_addr_t) base;
*line_len = j << 2;
return 1;
}
}
break;
}
}
return 0;
}
static int
find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len)
{
int found = 0;
auto int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev,
grub_pci_id_t pciid);
int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev,
grub_pci_id_t pciid)
{
grub_pci_address_t addr;
addr = grub_pci_make_address (dev, 2);
if (grub_pci_read (addr) >> 24 == 0x3)
{
int i;
grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n",
grub_pci_get_bus (dev), grub_pci_get_device (dev),
grub_pci_get_function (dev), pciid);
addr += 8;
for (i = 0; i < 6; i++, addr += 4)
{
grub_uint32_t old_bar1, old_bar2, type;
grub_uint64_t base64;
old_bar1 = grub_pci_read (addr);
if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
continue;
type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
{
if (i == 5)
break;
old_bar2 = grub_pci_read (addr + 4);
}
else
old_bar2 = 0;
base64 = old_bar2;
base64 <<= 32;
base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
grub_printf ("%s(%d): 0x%llx\n",
((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
"VMEM" : "MMIO"), i,
(unsigned long long) base64);
if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! found))
{
*fb_base = base64;
if (find_line_len (fb_base, line_len))
found++;
}
if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
{
i++;
addr += 4;
}
}
}
return found;
}
grub_pci_iterate (find_card);
return found;
}
grub_err_t
grub_xnu_set_video (struct grub_xnu_boot_params *params)
{
grub_efi_uga_draw_protocol_t *c;
grub_uint32_t width, height, depth, rate, pixel, fb_base, line_len;
int ret;
c = grub_efi_locate_protocol (&uga_draw_guid, 0);
if (! c)
return grub_error (GRUB_ERR_IO, "couldn't find UGADraw");
if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate))
return grub_error (GRUB_ERR_IO, "couldn't retrieve video mode");
grub_printf ("Video mode: %ux%u-%u@%u\n", width, height, depth, rate);
grub_efi_set_text_mode (0);
pixel = RGB_MAGIC;
efi_call_10 (c->blt, c, (struct grub_efi_uga_pixel *) &pixel,
GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
ret = find_framebuf (&fb_base, &line_len);
grub_efi_set_text_mode (1);
if (! ret)
return grub_error (GRUB_ERR_IO, "can\'t find frame buffer address");
grub_printf ("Frame buffer base: 0x%x\n", fb_base);
grub_printf ("Video line length: %d\n", line_len);
params->lfb_width = width;
params->lfb_height = height;
params->lfb_depth = depth;
params->lfb_line_len = line_len;
params->lfb_mode = GRUB_XNU_VIDEO_TEXT_IN_VIDEO;
params->lfb_base = fb_base;
return GRUB_ERR_NONE;
}

View file

@ -0,0 +1,311 @@
/* linux.c - boot Linux zImage or bzImage */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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/machine/memory.h>
#include <grub/file.h>
#include <grub/err.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/env.h>
#include <grub/term.h>
#include <grub/cpu/linux.h>
#include <grub/ieee1275/ieee1275.h>
#include <grub/command.h>
#include <grub/i18n.h>
#define GRUB_OFW_LINUX_PARAMS_ADDR 0x90000
#define GRUB_OFW_LINUX_KERNEL_ADDR 0x100000
#define GRUB_OFW_LINUX_INITRD_ADDR 0x800000
#define GRUB_OFW_LINUX_CL_OFFSET 0x1e00
#define GRUB_OFW_LINUX_CL_LENGTH 0x100
static grub_dl_t my_mod;
static grub_size_t kernel_size;
static char *kernel_addr, *kernel_cmdline;
static grub_size_t initrd_size;
static grub_err_t
grub_linux_unload (void)
{
grub_free (kernel_cmdline);
grub_free (kernel_addr);
kernel_cmdline = 0;
kernel_addr = 0;
initrd_size = 0;
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
/*
static int
grub_ieee1275_debug (void)
{
struct enter_args
{
struct grub_ieee1275_common_hdr common;
}
args;
INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0);
if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
return -1;
return 0;
}
*/
static grub_err_t
grub_linux_boot (void)
{
struct linux_kernel_params *params;
struct linux_kernel_header *lh;
char *prot_code;
char *bootpath;
grub_ssize_t len;
bootpath = grub_env_get ("root");
if (bootpath)
grub_ieee1275_set_property (grub_ieee1275_chosen,
"bootpath", bootpath,
grub_strlen (bootpath) + 1,
&len);
params = (struct linux_kernel_params *) GRUB_OFW_LINUX_PARAMS_ADDR;
lh = (struct linux_kernel_header *) params;
grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
params->alt_mem = grub_mmap_get_upper () >> 10;
params->ext_mem = params->alt_mem;
lh->cmd_line_ptr = (char *)
(GRUB_OFW_LINUX_PARAMS_ADDR + GRUB_OFW_LINUX_CL_OFFSET);
params->cl_magic = GRUB_LINUX_CL_MAGIC;
params->cl_offset = GRUB_OFW_LINUX_CL_OFFSET;
{
grub_term_output_t term;
int found = 0;
FOR_ACTIVE_TERM_OUTPUTS(term)
if (grub_strcmp (term->name, "ofconsole") == 0)
{
grub_uint16_t pos = grub_term_getxy (term);
params->video_cursor_x = pos >> 8;
params->video_cursor_y = pos & 0xff;
params->video_width = grub_term_width (term);
params->video_height = grub_term_height (term);
found = 1;
break;
}
if (!found)
{
params->video_cursor_x = 0;
params->video_cursor_y = 0;
params->video_width = 80;
params->video_height = 25;
}
}
params->font_size = 16;
params->ofw_signature = GRUB_LINUX_OFW_SIGNATURE;
params->ofw_num_items = 1;
params->ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn;
params->ofw_idt = 0;
if (initrd_size)
{
lh->type_of_loader = 1;
lh->ramdisk_image = GRUB_OFW_LINUX_INITRD_ADDR;
lh->ramdisk_size = initrd_size;
}
if (kernel_cmdline)
grub_strcpy (lh->cmd_line_ptr, kernel_cmdline);
prot_code = (char *) GRUB_OFW_LINUX_KERNEL_ADDR;
grub_memcpy (prot_code, kernel_addr, kernel_size);
asm volatile ("movl %0, %%esi" : : "m" (params));
asm volatile ("movl %%esi, %%esp" : : );
asm volatile ("movl %0, %%ecx" : : "m" (prot_code));
asm volatile ("xorl %%ebx, %%ebx" : : );
asm volatile ("jmp *%%ecx" : : );
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
struct linux_kernel_header lh;
grub_uint8_t setup_sects;
grub_size_t real_size, prot_size;
int i;
char *dest;
grub_dl_ref (my_mod);
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
goto fail;
}
file = grub_file_open (argv[0]);
if (! file)
goto fail;
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
{
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
goto fail;
}
if ((lh.boot_flag != grub_cpu_to_le16 (0xaa55)) ||
(lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)))
{
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
goto fail;
}
setup_sects = lh.setup_sects;
if (! setup_sects)
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
"bzImage", real_size, prot_size);
grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
if (grub_errno)
goto fail;
kernel_cmdline = grub_malloc (GRUB_OFW_LINUX_CL_LENGTH);
if (! kernel_cmdline)
goto fail;
dest = kernel_cmdline;
for (i = 1;
i < argc
&& dest + grub_strlen (argv[i]) + 1 < (kernel_cmdline
+ GRUB_OFW_LINUX_CL_LENGTH);
i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
kernel_addr = grub_malloc (prot_size);
if (! kernel_addr)
goto fail;
kernel_size = prot_size;
if (grub_file_read (file, kernel_addr, prot_size) != (int) prot_size)
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
if (grub_errno == GRUB_ERR_NONE)
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
fail:
if (file)
grub_file_close (file);
if (grub_errno != GRUB_ERR_NONE)
{
grub_free (kernel_cmdline);
grub_free (kernel_addr);
kernel_cmdline = 0;
kernel_addr = 0;
grub_dl_unref (my_mod);
}
return grub_errno;
}
static grub_err_t
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
goto fail;
}
if (! kernel_addr)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
goto fail;
}
file = grub_file_open (argv[0]);
if (! file)
goto fail;
initrd_size = grub_file_size (file);
if (grub_file_read (file, (void *) GRUB_OFW_LINUX_INITRD_ADDR,
initrd_size) != (int) initrd_size)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
fail:
if (file)
grub_file_close (file);
return grub_errno;
}
static grub_command_t cmd_linux, cmd_initrd;
GRUB_MOD_INIT(linux)
{
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
0, N_("Load Linux."));
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
GRUB_MOD_FINI(linux)
{
grub_unregister_command (cmd_linux);
grub_unregister_command (cmd_initrd);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,129 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/symbol.h>
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_linux_trampoline_start)
cli
/* %rdi contains protected memory start and %rsi
contains real memory start. */
mov %rsi, %rbx
call base
base:
pop %rsi
#ifdef APPLE_CC
lea (cont1 - base) (%esi, 1), %rax
mov %eax, (jump_vector - base) (%esi, 1)
lea (gdt - base) (%esi, 1), %rax
mov %rax, (gdtaddr - base) (%esi, 1)
/* Switch to compatibility mode. */
lidt (idtdesc - base) (%esi, 1)
lgdt (gdtdesc - base) (%esi, 1)
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
ljmp *(jump_vector - base) (%esi, 1)
#else
lea (cont1 - base) (%rsi, 1), %rax
mov %eax, (jump_vector - base) (%rsi, 1)
lea (gdt - base) (%rsi, 1), %rax
mov %rax, (gdtaddr - base) (%rsi, 1)
/* Switch to compatibility mode. */
lidt (idtdesc - base) (%rsi, 1)
lgdt (gdtdesc - base) (%rsi, 1)
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
ljmp *(jump_vector - base) (%rsi, 1)
#endif
cont1:
.code32
/* Update other registers. */
mov $0x18, %eax
mov %eax, %ds
mov %eax, %es
mov %eax, %fs
mov %eax, %gs
mov %eax, %ss
/* Disable paging. */
mov %cr0, %eax
and $0x7fffffff, %eax
mov %eax, %cr0
/* Disable amd64. */
mov $0xc0000080, %ecx
rdmsr
and $0xfffffeff, %eax
wrmsr
/* Turn off PAE. */
movl %cr4, %eax
and $0xffffffcf, %eax
mov %eax, %cr4
jmp cont2
cont2:
.code32
mov %ebx, %esi
jmp *%edi
/* GDT. */
.p2align 4
gdt:
/* NULL. */
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
/* Reserved. */
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
/* Code segment. */
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
/* Data segment. */
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
gdtdesc:
.word 31
gdtaddr:
.quad gdt
idtdesc:
.word 0
idtaddr:
.quad 0
.p2align 4
jump_vector:
/* Jump location. Is filled by the code */
.long 0
.long 0x10
VARIABLE(grub_linux_trampoline_end)

View file

@ -0,0 +1,674 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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/machine/memory.h>
#include <grub/memory.h>
#ifdef GRUB_MACHINE_PCBIOS
#include <grub/machine/biosnum.h>
#endif
#include <grub/multiboot.h>
#include <grub/cpu/relocator.h>
#include <grub/disk.h>
#include <grub/device.h>
#include <grub/partition.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/env.h>
#include <grub/relocator.h>
#include <grub/video.h>
#include <grub/file.h>
/* The bits in the required part of flags field we don't support. */
#define UNSUPPORTED_FLAGS 0x0000fff8
struct module
{
struct module *next;
grub_addr_t start;
grub_size_t size;
char *cmdline;
int cmdline_size;
};
struct module *modules, *modules_last;
static grub_size_t cmdline_size;
static grub_size_t total_modcmd;
static unsigned modcnt;
static char *cmdline = NULL;
static grub_uint32_t bootdev;
static int bootdev_set;
static grub_size_t elf_sec_num, elf_sec_entsize;
static unsigned elf_sec_shstrndx;
static void *elf_sections;
grub_err_t
grub_multiboot_load (grub_file_t file)
{
char *buffer;
grub_ssize_t len;
struct multiboot_header *header;
grub_err_t err;
buffer = grub_malloc (MULTIBOOT_SEARCH);
if (!buffer)
return grub_errno;
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
if (len < 32)
{
grub_free (buffer);
return grub_error (GRUB_ERR_BAD_OS, "file too small");
}
/* 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 multiboot_header *) buffer;
((char *) header <= buffer + len - 12) || (header = 0);
header = (struct multiboot_header *) ((char *) header + MULTIBOOT_HEADER_ALIGN))
{
if (header->magic == MULTIBOOT_HEADER_MAGIC
&& !(header->magic + header->flags + header->checksum))
break;
}
if (header == 0)
{
grub_free (buffer);
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
}
if (header->flags & UNSUPPORTED_FLAGS)
{
grub_free (buffer);
return grub_error (GRUB_ERR_UNKNOWN_OS,
"unsupported flag: 0x%x", header->flags);
}
if (header->flags & MULTIBOOT_AOUT_KLUDGE)
{
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);
grub_size_t code_size;
void *source;
grub_relocator_chunk_t ch;
if (header->bss_end_addr)
code_size = (header->bss_end_addr - header->load_addr);
else
code_size = load_size;
err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
&ch, header->load_addr,
code_size);
if (err)
{
grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
grub_free (buffer);
return err;
}
source = get_virtual_current_address (ch);
if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
{
grub_free (buffer);
return grub_errno;
}
grub_file_read (file, source, load_size);
if (grub_errno)
{
grub_free (buffer);
return grub_errno;
}
if (header->bss_end_addr)
grub_memset ((grub_uint32_t *) source + load_size, 0,
header->bss_end_addr - header->load_addr - load_size);
grub_multiboot_payload_eip = header->entry_addr;
}
else
{
err = grub_multiboot_load_elf (file, buffer);
if (err)
{
grub_free (buffer);
return err;
}
}
if (header->flags & MULTIBOOT_VIDEO_MODE)
{
switch (header->mode_type)
{
case 1:
err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_EGA_TEXT,
GRUB_MULTIBOOT_CONSOLE_EGA_TEXT
| GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
0, 0, 0, 0);
break;
case 0:
err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
GRUB_MULTIBOOT_CONSOLE_EGA_TEXT
| GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
header->width, header->height,
header->depth, 0);
break;
default:
err = grub_error (GRUB_ERR_BAD_OS,
"unsupported graphical mode type %d",
header->mode_type);
break;
}
}
else
err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_EGA_TEXT,
GRUB_MULTIBOOT_CONSOLE_EGA_TEXT,
0, 0, 0, 0);
return err;
}
static grub_size_t
grub_multiboot_get_mbi_size (void)
{
return sizeof (struct multiboot_info) + ALIGN_UP (cmdline_size, 4)
+ modcnt * sizeof (struct multiboot_mod_list) + total_modcmd
+ ALIGN_UP (sizeof(PACKAGE_STRING), 4)
+ grub_get_multiboot_mmap_count () * sizeof (struct multiboot_mmap_entry)
+ elf_sec_entsize * elf_sec_num
#if HAS_VBE
+ sizeof (struct grub_vbe_info_block)
+ sizeof (struct grub_vbe_mode_info_block)
#endif
+ 256 * sizeof (struct multiboot_color);
}
/* Fill previously allocated Multiboot mmap. */
static void
grub_fill_multiboot_mmap (struct multiboot_mmap_entry *first_entry)
{
struct multiboot_mmap_entry *mmap_entry = (struct multiboot_mmap_entry *) first_entry;
auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type)
{
mmap_entry->addr = addr;
mmap_entry->len = size;
switch (type)
{
case GRUB_MACHINE_MEMORY_AVAILABLE:
mmap_entry->type = MULTIBOOT_MEMORY_AVAILABLE;
break;
#ifdef GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE
case GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE:
mmap_entry->type = MULTIBOOT_MEMORY_ACPI_RECLAIMABLE;
break;
#endif
#ifdef GRUB_MACHINE_MEMORY_NVS
case GRUB_MACHINE_MEMORY_NVS:
mmap_entry->type = MULTIBOOT_MEMORY_NVS;
break;
#endif
default:
mmap_entry->type = MULTIBOOT_MEMORY_RESERVED;
break;
}
mmap_entry->size = sizeof (struct multiboot_mmap_entry) - sizeof (mmap_entry->size);
mmap_entry++;
return 0;
}
grub_mmap_iterate (hook);
}
#if HAS_VBE
static grub_err_t
fill_vbe_info (struct multiboot_info *mbi, grub_uint8_t *ptrorig,
grub_uint32_t ptrdest, int fill_generic)
{
grub_vbe_status_t status;
grub_uint32_t vbe_mode;
void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
struct grub_vbe_mode_info_block *mode_info;
status = grub_vbe_bios_get_controller_info (scratch);
if (status != GRUB_VBE_STATUS_OK)
return grub_error (GRUB_ERR_IO, "Can't get controller info.");
mbi->vbe_control_info = ptrdest;
grub_memcpy (ptrorig, scratch, sizeof (struct grub_vbe_info_block));
ptrorig += sizeof (struct grub_vbe_info_block);
ptrdest += sizeof (struct grub_vbe_info_block);
status = grub_vbe_bios_get_mode (scratch);
vbe_mode = *(grub_uint32_t *) scratch;
if (status != GRUB_VBE_STATUS_OK)
return grub_error (GRUB_ERR_IO, "can't get VBE mode");
mbi->vbe_mode = vbe_mode;
mode_info = (struct grub_vbe_mode_info_block *) ptrorig;
mbi->vbe_mode_info = ptrdest;
/* get_mode_info isn't available for mode 3. */
if (vbe_mode == 3)
{
grub_memset (mode_info, 0, sizeof (struct grub_vbe_mode_info_block));
mode_info->memory_model = GRUB_VBE_MEMORY_MODEL_TEXT;
mode_info->x_resolution = 80;
mode_info->y_resolution = 25;
}
else
{
status = grub_vbe_bios_get_mode_info (vbe_mode, scratch);
if (status != GRUB_VBE_STATUS_OK)
return grub_error (GRUB_ERR_IO, "can't get mode info");
grub_memcpy (mode_info, scratch,
sizeof (struct grub_vbe_mode_info_block));
}
ptrorig += sizeof (struct grub_vbe_mode_info_block);
ptrdest += sizeof (struct grub_vbe_mode_info_block);
/* FIXME: retrieve those. */
mbi->vbe_interface_seg = 0;
mbi->vbe_interface_off = 0;
mbi->vbe_interface_len = 0;
mbi->flags |= MULTIBOOT_INFO_VBE_INFO;
if (fill_generic && mode_info->memory_model == GRUB_VBE_MEMORY_MODEL_TEXT)
{
mbi->framebuffer_addr = 0xb8000;
mbi->framebuffer_pitch = 2 * mode_info->x_resolution;
mbi->framebuffer_width = mode_info->x_resolution;
mbi->framebuffer_height = mode_info->y_resolution;
mbi->framebuffer_bpp = 16;
mbi->framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
mbi->flags |= MULTIBOOT_INFO_FRAMEBUFFER_INFO;
}
return GRUB_ERR_NONE;
}
#endif
static grub_err_t
retrieve_video_parameters (struct multiboot_info *mbi,
grub_uint8_t *ptrorig, grub_uint32_t ptrdest)
{
grub_err_t err;
struct grub_video_mode_info mode_info;
void *framebuffer;
grub_video_driver_id_t driv_id;
struct grub_video_palette_data palette[256];
err = grub_multiboot_set_video_mode ();
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
grub_video_get_palette (0, ARRAY_SIZE (palette), palette);
driv_id = grub_video_get_driver_id ();
#if GRUB_MACHINE_HAS_VGA_TEXT
if (driv_id == GRUB_VIDEO_DRIVER_NONE)
return fill_vbe_info (mbi, ptrorig, ptrdest, 1);
#else
if (driv_id == GRUB_VIDEO_DRIVER_NONE)
return GRUB_ERR_NONE;
#endif
err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
if (err)
return err;
mbi->framebuffer_addr = (grub_addr_t) framebuffer;
mbi->framebuffer_pitch = mode_info.pitch;
mbi->framebuffer_width = mode_info.width;
mbi->framebuffer_height = mode_info.height;
mbi->framebuffer_bpp = mode_info.bpp;
if (mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
{
struct multiboot_color *mb_palette;
unsigned i;
mbi->framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED;
mbi->framebuffer_palette_addr = ptrdest;
mbi->framebuffer_palette_num_colors = mode_info.number_of_colors;
if (mbi->framebuffer_palette_num_colors > ARRAY_SIZE (palette))
mbi->framebuffer_palette_num_colors = ARRAY_SIZE (palette);
mb_palette = (struct multiboot_color *) ptrorig;
for (i = 0; i < mbi->framebuffer_palette_num_colors; i++)
{
mb_palette[i].red = palette[i].r;
mb_palette[i].green = palette[i].g;
mb_palette[i].blue = palette[i].b;
}
ptrorig += mbi->framebuffer_palette_num_colors
* sizeof (struct multiboot_color);
ptrdest += mbi->framebuffer_palette_num_colors
* sizeof (struct multiboot_color);
}
else
{
mbi->framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
mbi->framebuffer_red_field_position = mode_info.red_field_pos;
mbi->framebuffer_red_mask_size = mode_info.red_mask_size;
mbi->framebuffer_green_field_position = mode_info.green_field_pos;
mbi->framebuffer_green_mask_size = mode_info.green_mask_size;
mbi->framebuffer_blue_field_position = mode_info.blue_field_pos;
mbi->framebuffer_blue_mask_size = mode_info.blue_mask_size;
}
mbi->flags |= MULTIBOOT_INFO_FRAMEBUFFER_INFO;
#if GRUB_MACHINE_HAS_VBE
if (driv_id == GRUB_VIDEO_DRIVER_VBE)
return fill_vbe_info (mbi, ptrorig, ptrdest, 0);
#endif
return GRUB_ERR_NONE;
}
grub_err_t
grub_multiboot_make_mbi (grub_uint32_t *target)
{
struct multiboot_info *mbi;
struct multiboot_mod_list *modlist;
unsigned i;
struct module *cur;
grub_size_t mmap_size;
grub_uint8_t *ptrorig;
grub_addr_t ptrdest;
grub_err_t err;
grub_size_t bufsize;
grub_relocator_chunk_t ch;
bufsize = grub_multiboot_get_mbi_size ();
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
0, 0xffffffff - bufsize,
bufsize, 4,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
ptrorig = get_virtual_current_address (ch);
ptrdest = (grub_addr_t) get_virtual_current_address (ch);
*target = ptrdest;
mbi = (struct multiboot_info *) ptrorig;
ptrorig += sizeof (*mbi);
ptrdest += sizeof (*mbi);
grub_memset (mbi, 0, sizeof (*mbi));
grub_memcpy (ptrorig, cmdline, cmdline_size);
mbi->flags |= MULTIBOOT_INFO_CMDLINE;
mbi->cmdline = ptrdest;
ptrorig += ALIGN_UP (cmdline_size, 4);
ptrdest += ALIGN_UP (cmdline_size, 4);
grub_memcpy (ptrorig, PACKAGE_STRING, sizeof(PACKAGE_STRING));
mbi->flags |= MULTIBOOT_INFO_BOOT_LOADER_NAME;
mbi->boot_loader_name = ptrdest;
ptrorig += ALIGN_UP (sizeof(PACKAGE_STRING), 4);
ptrdest += ALIGN_UP (sizeof(PACKAGE_STRING), 4);
if (modcnt)
{
mbi->flags |= MULTIBOOT_INFO_MODS;
mbi->mods_addr = ptrdest;
mbi->mods_count = modcnt;
modlist = (struct multiboot_mod_list *) ptrorig;
ptrorig += modcnt * sizeof (struct multiboot_mod_list);
ptrdest += modcnt * sizeof (struct multiboot_mod_list);
for (i = 0, cur = modules; i < modcnt; i++, cur = cur->next)
{
modlist[i].mod_start = cur->start;
modlist[i].mod_end = modlist[i].mod_start + cur->size;
modlist[i].cmdline = ptrdest;
grub_memcpy (ptrorig, cur->cmdline, cur->cmdline_size);
ptrorig += ALIGN_UP (cur->cmdline_size, 4);
ptrdest += ALIGN_UP (cur->cmdline_size, 4);
}
}
else
{
mbi->mods_addr = 0;
mbi->mods_count = 0;
}
mmap_size = grub_get_multiboot_mmap_count ()
* sizeof (struct multiboot_mmap_entry);
grub_fill_multiboot_mmap ((struct multiboot_mmap_entry *) ptrorig);
mbi->mmap_length = mmap_size;
mbi->mmap_addr = ptrdest;
mbi->flags |= MULTIBOOT_INFO_MEM_MAP;
ptrorig += mmap_size;
ptrdest += mmap_size;
/* Convert from bytes to kilobytes. */
mbi->mem_lower = grub_mmap_get_lower () / 1024;
mbi->mem_upper = grub_mmap_get_upper () / 1024;
mbi->flags |= MULTIBOOT_INFO_MEMORY;
if (bootdev_set)
{
mbi->boot_device = bootdev;
mbi->flags |= MULTIBOOT_INFO_BOOTDEV;
}
if (elf_sec_num)
{
mbi->u.elf_sec.addr = ptrdest;
grub_memcpy (ptrorig, elf_sections, elf_sec_entsize * elf_sec_num);
mbi->u.elf_sec.num = elf_sec_num;
mbi->u.elf_sec.size = elf_sec_entsize;
mbi->u.elf_sec.shndx = elf_sec_shstrndx;
mbi->flags |= MULTIBOOT_INFO_ELF_SHDR;
}
err = retrieve_video_parameters (mbi, ptrorig, ptrdest);
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
#if GRUB_MACHINE_HAS_VBE
ptrorig += sizeof (struct grub_vbe_info_block);
ptrdest += sizeof (struct grub_vbe_info_block);
ptrorig += sizeof (struct grub_vbe_mode_info_block);
ptrdest += sizeof (struct grub_vbe_mode_info_block);
#endif
return GRUB_ERR_NONE;
}
void
grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
unsigned shndx, void *data)
{
elf_sec_num = num;
elf_sec_shstrndx = shndx;
elf_sec_entsize = entsize;
elf_sections = data;
}
void
grub_multiboot_free_mbi (void)
{
struct module *cur, *next;
cmdline_size = 0;
total_modcmd = 0;
modcnt = 0;
grub_free (cmdline);
cmdline = NULL;
bootdev_set = 0;
for (cur = modules; cur; cur = next)
{
next = cur->next;
grub_free (cur->cmdline);
grub_free (cur);
}
modules = NULL;
modules_last = NULL;
grub_free (elf_sections);
elf_sections = NULL;
elf_sec_entsize = 0;
elf_sec_num = 0;
}
grub_err_t
grub_multiboot_init_mbi (int argc, char *argv[])
{
grub_ssize_t len = 0;
char *p;
int i;
grub_multiboot_free_mbi ();
for (i = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
if (len == 0)
len = 1;
cmdline = p = grub_malloc (len);
if (! cmdline)
return grub_errno;
cmdline_size = len;
for (i = 0; i < argc; i++)
{
p = grub_stpcpy (p, argv[i]);
*(p++) = ' ';
}
/* Remove the space after the last word. */
if (p != cmdline)
p--;
*p = '\0';
return GRUB_ERR_NONE;
}
grub_err_t
grub_multiboot_add_module (grub_addr_t start, grub_size_t size,
int argc, char *argv[])
{
struct module *newmod;
char *p;
grub_ssize_t len = 0;
int i;
newmod = grub_malloc (sizeof (*newmod));
if (!newmod)
return grub_errno;
newmod->start = start;
newmod->size = size;
for (i = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
if (len == 0)
len = 1;
newmod->cmdline = p = grub_malloc (len);
if (! newmod->cmdline)
{
grub_free (newmod);
return grub_errno;
}
newmod->cmdline_size = len;
total_modcmd += ALIGN_UP (len, 4);
for (i = 0; i < argc; i++)
{
p = grub_stpcpy (p, argv[i]);
*(p++) = ' ';
}
/* Remove the space after the last word. */
if (p != newmod->cmdline)
p--;
*p = '\0';
if (modules_last)
modules_last->next = newmod;
else
{
modules = newmod;
modules_last->next = NULL;
}
modules_last = newmod;
modcnt++;
return GRUB_ERR_NONE;
}
void
grub_multiboot_set_bootdev (void)
{
grub_uint32_t biosdev, slice = ~0, part = ~0;
grub_device_t dev;
#ifdef GRUB_MACHINE_PCBIOS
biosdev = grub_get_root_biosnumber ();
#else
biosdev = 0xffffffff;
#endif
if (biosdev == 0xffffffff)
return;
dev = grub_device_open (0);
if (dev && dev->disk && dev->disk->partition)
{
if (dev->disk->partition->parent)
{
part = dev->disk->partition->number;
slice = dev->disk->partition->parent->number;
}
else
slice = dev->disk->partition->number;
}
if (dev)
grub_device_close (dev);
bootdev = ((biosdev & 0xff) << 24) | ((slice & 0xff) << 16)
| ((part & 0xff) << 8) | 0xff;
bootdev_set = 1;
}

View file

@ -0,0 +1,174 @@
/* chainloader.c - boot another boot loader */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2004,2007,2009,2010 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/machine/chainloader.h>
#include <grub/file.h>
#include <grub/err.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/partition.h>
#include <grub/machine/memory.h>
#include <grub/dl.h>
#include <grub/command.h>
#include <grub/msdos_partition.h>
#include <grub/machine/biosnum.h>
#include <grub/cpu/floppy.h>
#include <grub/i18n.h>
#include <grub/video.h>
#include <grub/mm.h>
static grub_dl_t my_mod;
static int boot_drive;
static void *boot_part_addr;
static grub_err_t
grub_chainloader_boot (void)
{
grub_video_set_mode ("text", 0, 0);
grub_chainloader_real_boot (boot_drive, boot_part_addr);
/* Never reach here. */
return GRUB_ERR_NONE;
}
static grub_err_t
grub_chainloader_unload (void)
{
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
static void
grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
{
grub_file_t file = 0;
grub_uint16_t signature;
grub_device_t dev;
int drive = -1;
void *part_addr = 0;
grub_dl_ref (my_mod);
grub_file_filter_disable_compression ();
file = grub_file_open (filename);
if (! file)
goto fail;
/* Read the first block. */
if (grub_file_read (file, (void *) 0x7C00, GRUB_DISK_SECTOR_SIZE)
!= GRUB_DISK_SECTOR_SIZE)
{
if (grub_errno == GRUB_ERR_NONE)
grub_error (GRUB_ERR_BAD_OS, "too small");
goto fail;
}
/* Check the signature. */
signature = *((grub_uint16_t *) (0x7C00 + GRUB_DISK_SECTOR_SIZE - 2));
if (signature != grub_le_to_cpu16 (0xaa55)
&& ! (flags & GRUB_CHAINLOADER_FORCE))
{
grub_error (GRUB_ERR_BAD_OS, "invalid signature");
goto fail;
}
grub_file_close (file);
/* Obtain the partition table from the root device. */
drive = grub_get_root_biosnumber ();
dev = grub_device_open (0);
if (dev && dev->disk && dev->disk->partition)
{
grub_disk_t disk = dev->disk;
if (disk)
{
grub_partition_t p = disk->partition;
if (p && grub_strcmp (p->partmap->name, "msdos") == 0)
{
disk->partition = p->parent;
grub_disk_read (disk, p->offset, 446, 64,
(void *) GRUB_MEMORY_MACHINE_PART_TABLE_ADDR);
part_addr = (void *) (GRUB_MEMORY_MACHINE_PART_TABLE_ADDR
+ (p->index << 4));
disk->partition = p;
}
}
}
if (dev)
grub_device_close (dev);
/* Ignore errors. Perhaps it's not fatal. */
grub_errno = GRUB_ERR_NONE;
boot_drive = drive;
boot_part_addr = part_addr;
grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 1);
return;
fail:
if (file)
grub_file_close (file);
grub_dl_unref (my_mod);
}
static grub_err_t
grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_chainloader_flags_t flags = 0;
if (argc > 0 && grub_strcmp (argv[0], "--force") == 0)
{
flags |= GRUB_CHAINLOADER_FORCE;
argc--;
argv++;
}
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
else
grub_chainloader_cmd (argv[0], flags);
return grub_errno;
}
static grub_command_t cmd;
GRUB_MOD_INIT(chainloader)
{
cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
0, N_("Load another boot loader."));
my_mod = mod;
}
GRUB_MOD_FINI(chainloader)
{
grub_unregister_command (cmd);
}

View file

@ -0,0 +1,455 @@
/* linux.c - boot Linux zImage or bzImage */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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/file.h>
#include <grub/err.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/machine/memory.h>
#include <grub/dl.h>
#include <grub/cpu/linux.h>
#include <grub/command.h>
#include <grub/i18n.h>
#include <grub/mm.h>
#include <grub/cpu/relocator.h>
#include <grub/video.h>
#include <grub/i386/floppy.h>
#define GRUB_LINUX_CL_OFFSET 0x9000
#define GRUB_LINUX_CL_END_OFFSET 0x90FF
static grub_dl_t my_mod;
static grub_size_t linux_mem_size;
static int loaded;
static struct grub_relocator *relocator = NULL;
static grub_addr_t grub_linux_real_target;
static char *grub_linux_real_chunk;
static grub_size_t grub_linux16_prot_size;
static grub_err_t
grub_linux16_boot (void)
{
grub_uint16_t segment;
struct grub_relocator16_state state;
segment = grub_linux_real_target >> 4;
state.gs = state.fs = state.es = state.ds = state.ss = segment;
state.sp = GRUB_LINUX_SETUP_STACK;
state.cs = segment + 0x20;
state.ip = 0;
grub_video_set_mode ("text", 0, 0);
grub_stop_floppy ();
return grub_relocator16_boot (relocator, state);
}
static grub_err_t
grub_linux_unload (void)
{
grub_dl_unref (my_mod);
loaded = 0;
grub_relocator_unload (relocator);
relocator = NULL;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
struct linux_kernel_header lh;
grub_uint8_t setup_sects;
grub_size_t real_size;
grub_ssize_t len;
int i;
char *dest;
char *grub_linux_prot_chunk;
int grub_linux_is_bzimage;
grub_addr_t grub_linux_prot_target;
grub_err_t err;
grub_dl_ref (my_mod);
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
goto fail;
}
file = grub_file_open (argv[0]);
if (! file)
goto fail;
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
{
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
goto fail;
}
if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
{
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
goto fail;
}
if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
{
grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
goto fail;
}
grub_linux_is_bzimage = 0;
setup_sects = lh.setup_sects;
linux_mem_size = 0;
if (lh.header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
&& grub_le_to_cpu16 (lh.version) >= 0x0200)
{
grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
/* Put the real mode part at as a high location as possible. */
grub_linux_real_target = grub_mmap_get_lower ()
- GRUB_LINUX_SETUP_MOVE_SIZE;
/* But it must not exceed the traditional area. */
if (grub_linux_real_target > GRUB_LINUX_OLD_REAL_MODE_ADDR)
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
if (grub_le_to_cpu16 (lh.version) >= 0x0201)
{
lh.heap_end_ptr = grub_cpu_to_le16 (GRUB_LINUX_HEAP_END_OFFSET);
lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
}
if (grub_le_to_cpu16 (lh.version) >= 0x0202)
lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
else
{
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_SETUP_MOVE_SIZE);
}
}
else
{
/* Your kernel is quite old... */
lh.cl_magic = grub_cpu_to_le16 (GRUB_LINUX_CL_MAGIC);
lh.cl_offset = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET);
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
}
/* If SETUP_SECTS is not set, set it to the default (4). */
if (! setup_sects)
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
grub_linux16_prot_size = grub_file_size (file)
- real_size - GRUB_DISK_SECTOR_SIZE;
if (! grub_linux_is_bzimage
&& GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
> grub_linux_real_target)
{
grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
(char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
(grub_size_t) grub_linux_real_target);
goto fail;
}
if (grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE
> grub_mmap_get_lower ())
{
grub_error (GRUB_ERR_OUT_OF_RANGE,
"too small lower memory (0x%x > 0x%x)",
grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE,
(int) grub_mmap_get_lower ());
goto fail;
}
grub_printf (" [Linux-%s, setup=0x%x, size=0x%x]\n",
grub_linux_is_bzimage ? "bzImage" : "zImage", real_size,
grub_linux16_prot_size);
relocator = grub_relocator_new ();
if (!relocator)
goto fail;
for (i = 1; i < argc; i++)
if (grub_memcmp (argv[i], "vga=", 4) == 0)
{
/* Video mode selection support. */
grub_uint16_t vid_mode;
char *val = argv[i] + 4;
if (grub_strcmp (val, "normal") == 0)
vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
else if (grub_strcmp (val, "ext") == 0)
vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
else if (grub_strcmp (val, "ask") == 0)
vid_mode = GRUB_LINUX_VID_MODE_ASK;
else
vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
if (grub_errno)
goto fail;
lh.vid_mode = grub_cpu_to_le16 (vid_mode);
}
else if (grub_memcmp (argv[i], "mem=", 4) == 0)
{
char *val = argv[i] + 4;
linux_mem_size = grub_strtoul (val, &val, 0);
if (grub_errno)
{
grub_errno = GRUB_ERR_NONE;
linux_mem_size = 0;
}
else
{
int shift = 0;
switch (grub_tolower (val[0]))
{
case 'g':
shift += 10;
case 'm':
shift += 10;
case 'k':
shift += 10;
default:
break;
}
/* Check an overflow. */
if (linux_mem_size > (~0UL >> shift))
linux_mem_size = 0;
else
linux_mem_size <<= shift;
}
}
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
grub_linux_real_target,
GRUB_LINUX_SETUP_MOVE_SIZE);
if (err)
return err;
grub_linux_real_chunk = get_virtual_current_address (ch);
}
/* Put the real mode code at the temporary address. */
grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
|| grub_le_to_cpu16 (lh.version) < 0x0200)
/* Clear the heap space. */
grub_memset (grub_linux_real_chunk
+ ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
0,
((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
<< GRUB_DISK_SECTOR_BITS));
/* Specify the boot file. */
dest = grub_stpcpy (grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
"BOOT_IMAGE=");
dest = grub_stpcpy (dest, argv[0]);
/* Copy kernel parameters. */
for (i = 1;
i < argc
&& dest + grub_strlen (argv[i]) + 1 < (grub_linux_real_chunk
+ GRUB_LINUX_CL_END_OFFSET);
i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
if (grub_linux_is_bzimage)
grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
else
grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
grub_linux_prot_target,
grub_linux16_prot_size);
if (err)
return err;
grub_linux_prot_chunk = get_virtual_current_address (ch);
}
len = grub_linux16_prot_size;
if (grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)
!= (grub_ssize_t) grub_linux16_prot_size)
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
if (grub_errno == GRUB_ERR_NONE)
{
grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
loaded = 1;
}
fail:
if (file)
grub_file_close (file);
if (grub_errno != GRUB_ERR_NONE)
{
grub_dl_unref (my_mod);
loaded = 0;
grub_relocator_unload (relocator);
}
return grub_errno;
}
static grub_err_t
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
grub_addr_t addr_max, addr_min;
struct linux_kernel_header *lh;
grub_uint8_t *initrd_chunk;
grub_addr_t initrd_addr;
grub_err_t err;
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
goto fail;
}
if (!loaded)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
goto fail;
}
lh = (struct linux_kernel_header *) grub_linux_real_chunk;
if (!(lh->header == grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
&& grub_le_to_cpu16 (lh->version) >= 0x0200))
{
grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
goto fail;
}
/* Get the highest address available for the initrd. */
if (grub_le_to_cpu16 (lh->version) >= 0x0203)
{
addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
/* XXX in reality, Linux specifies a bogus value, so
it is necessary to make sure that ADDR_MAX does not exceed
0x3fffffff. */
if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
}
else
addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
if (linux_mem_size != 0 && linux_mem_size < addr_max)
addr_max = linux_mem_size;
/* Linux 2.3.xx has a bug in the memory range check, so avoid
the last page.
Linux 2.2.xx has a bug in the memory range check, which is
worse than that of Linux 2.3.xx, so avoid the last 64kb. */
addr_max -= 0x10000;
addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
grub_file_filter_disable_compression ();
file = grub_file_open (argv[0]);
if (!file)
goto fail;
size = grub_file_size (file);
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (relocator, &ch,
addr_min, addr_max - size,
size, 0x1000,
GRUB_RELOCATOR_PREFERENCE_HIGH);
if (err)
return err;
initrd_chunk = get_virtual_current_address (ch);
initrd_addr = get_physical_target_address (ch);
}
if (grub_file_read (file, initrd_chunk, size) != size)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
lh->ramdisk_image = initrd_addr;
lh->ramdisk_size = size;
fail:
if (file)
grub_file_close (file);
return grub_errno;
}
static grub_command_t cmd_linux, cmd_initrd;
GRUB_MOD_INIT(linux16)
{
cmd_linux =
grub_register_command ("linux16", grub_cmd_linux,
0, N_("Load Linux."));
cmd_initrd =
grub_register_command ("initrd16", grub_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
GRUB_MOD_FINI(linux16)
{
grub_unregister_command (cmd_linux);
grub_unregister_command (cmd_initrd);
}

View file

@ -0,0 +1,157 @@
/* chainloader.c - boot another boot loader */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2004,2007,2009,2010 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/file.h>
#include <grub/err.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/partition.h>
#include <grub/dl.h>
#include <grub/command.h>
#include <grub/machine/biosnum.h>
#include <grub/i18n.h>
#include <grub/video.h>
#include <grub/mm.h>
#include <grub/cpu/relocator.h>
static grub_dl_t my_mod;
static struct grub_relocator *rel;
static grub_uint32_t edx = 0xffffffff;
#define GRUB_NTLDR_SEGMENT 0x2000
static grub_err_t
grub_ntldr_boot (void)
{
struct grub_relocator16_state state = {
.cs = GRUB_NTLDR_SEGMENT,
.ip = 0,
.ds = 0,
.es = 0,
.fs = 0,
.gs = 0,
.ss = 0,
.sp = 0x7c00,
.edx = edx
};
grub_video_set_mode ("text", 0, 0);
return grub_relocator16_boot (rel, state);
}
static grub_err_t
grub_ntldr_unload (void)
{
grub_relocator_unload (rel);
rel = NULL;
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_err_t err;
void *bs, *ntldr;
grub_size_t ntldrsize;
grub_device_t dev;
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
grub_dl_ref (my_mod);
rel = grub_relocator_new ();
if (!rel)
goto fail;
file = grub_file_open (argv[0]);
if (! file)
goto fail;
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
GRUB_DISK_SECTOR_SIZE);
if (err)
goto fail;
bs = get_virtual_current_address (ch);
}
edx = grub_get_root_biosnumber ();
dev = grub_device_open (0);
if (dev && dev->disk)
{
err = grub_disk_read (dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, bs);
if (err)
{
grub_device_close (dev);
goto fail;
}
}
if (dev)
grub_device_close (dev);
ntldrsize = grub_file_size (file);
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_NTLDR_SEGMENT << 4,
ntldrsize);
if (err)
goto fail;
ntldr = get_virtual_current_address (ch);
}
if (grub_file_read (file, ntldr, ntldrsize)
!= (grub_ssize_t) ntldrsize)
goto fail;
grub_loader_set (grub_ntldr_boot, grub_ntldr_unload, 1);
return GRUB_ERR_NONE;
fail:
if (file)
grub_file_close (file);
grub_ntldr_unload ();
return grub_errno;
}
static grub_command_t cmd;
GRUB_MOD_INIT(ntldr)
{
cmd = grub_register_command ("ntldr", grub_cmd_ntldr,
0, N_("Load NTLDR or BootMGR."));
my_mod = mod;
}
GRUB_MOD_FINI(ntldr)
{
grub_unregister_command (cmd);
}

View file

@ -0,0 +1,110 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/env.h>
#include <grub/misc.h>
#include <grub/xnu.h>
#include <grub/mm.h>
#include <grub/cpu/xnu.h>
#include <grub/video_fb.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define DEFAULT_VIDEO_MODE "1024x768x32,800x600x32,640x480x32"
static int NESTED_FUNC_ATTR video_hook (grub_video_adapter_t p __attribute__ ((unused)),
struct grub_video_mode_info *info)
{
if (info->mode_type & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
return 0;
return 1;
}
/* Setup video for xnu. */
grub_err_t
grub_xnu_set_video (struct grub_xnu_boot_params *params)
{
struct grub_video_mode_info mode_info;
int ret;
char *tmp, *modevar;
void *framebuffer;
grub_err_t err;
modevar = grub_env_get ("gfxpayload");
if (! modevar || *modevar == 0)
err = grub_video_set_mode (DEFAULT_VIDEO_MODE, video_hook);
else
{
tmp = grub_malloc (grub_strlen (modevar)
+ sizeof (DEFAULT_VIDEO_MODE) + 1);
if (! tmp)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't allocate temporary storag");
grub_sprintf (tmp, "%s;" DEFAULT_VIDEO_MODE, modevar);
err = grub_video_set_mode (tmp, video_hook);
grub_free (tmp);
}
if (err)
return err;
if (grub_xnu_bitmap)
{
int x, y;
x = mode_info.width - grub_xnu_bitmap->mode_info.width;
x /= 2;
y = mode_info.height - grub_xnu_bitmap->mode_info.height;
y /= 2;
err = grub_video_blit_bitmap (grub_xnu_bitmap,
GRUB_VIDEO_BLIT_REPLACE,
x > 0 ? x : 0,
y > 0 ? y : 0,
x < 0 ? -x : 0,
y < 0 ? -y : 0,
min (grub_xnu_bitmap->mode_info.width,
mode_info.width),
min (grub_xnu_bitmap->mode_info.height,
mode_info.height));
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
grub_xnu_bitmap = 0;
}
err = GRUB_ERR_NONE;
}
ret = grub_video_get_info_and_fini (&mode_info, &framebuffer);
if (ret)
return grub_error (GRUB_ERR_IO, "couldn't retrieve video parameters");
params->lfb_width = mode_info.width;
params->lfb_height = mode_info.height;
params->lfb_depth = mode_info.bpp;
params->lfb_line_len = mode_info.pitch;
params->lfb_base = PTR_TO_UINT32 (framebuffer);
params->lfb_mode = grub_xnu_bitmap
? GRUB_XNU_VIDEO_SPLASH : GRUB_XNU_VIDEO_TEXT_IN_VIDEO;
return GRUB_ERR_NONE;
}

1135
grub-core/loader/i386/xnu.c Normal file

File diff suppressed because it is too large Load diff

160
grub-core/loader/macho.c Normal file
View file

@ -0,0 +1,160 @@
/* macho.c - load Mach-O files. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/>.
*/
/* This Mach-O loader is incomplete and can load only non-relocatable segments.
This is however enough to boot xnu (otool -l and Mach-O specs for more info).
*/
#include <grub/err.h>
#include <grub/macho.h>
#include <grub/cpu/macho.h>
#include <grub/machoload.h>
#include <grub/file.h>
#include <grub/misc.h>
#include <grub/mm.h>
grub_err_t
grub_macho_close (grub_macho_t macho)
{
grub_file_t file = macho->file;
grub_free (macho->cmds32);
grub_free (macho->cmds64);
grub_free (macho);
if (file)
grub_file_close (file);
return grub_errno;
}
grub_macho_t
grub_macho_file (grub_file_t file)
{
grub_macho_t macho;
union grub_macho_filestart filestart;
macho = grub_malloc (sizeof (*macho));
if (! macho)
return 0;
macho->file = file;
macho->offset32 = -1;
macho->offset64 = -1;
macho->end32 = -1;
macho->end64 = -1;
macho->cmds32 = 0;
macho->cmds64 = 0;
if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
goto fail;
if (grub_file_read (macho->file, &filestart, sizeof (filestart))
!= sizeof (filestart))
{
grub_error_push ();
grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
goto fail;
}
/* Is it a fat file? */
if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
{
struct grub_macho_fat_arch *archs;
int i, narchs;
/* Load architecture description. */
narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
== (grub_off_t) -1)
goto fail;
archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
if (!archs)
goto fail;
if (grub_file_read (macho->file, archs,
sizeof (struct grub_macho_fat_arch) * narchs)
!= (grub_ssize_t)sizeof(struct grub_macho_fat_arch) * narchs)
{
grub_free (archs);
grub_error_push ();
grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
goto fail;
}
for (i = 0; i < narchs; i++)
{
if (GRUB_MACHO_CPUTYPE_IS_HOST32
(grub_be_to_cpu32 (archs[i].cputype)))
{
macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
macho->end32 = grub_be_to_cpu32 (archs[i].offset)
+ grub_be_to_cpu32 (archs[i].size);
}
if (GRUB_MACHO_CPUTYPE_IS_HOST64
(grub_be_to_cpu32 (archs[i].cputype)))
{
macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
macho->end64 = grub_be_to_cpu32 (archs[i].offset)
+ grub_be_to_cpu32 (archs[i].size);
}
}
grub_free (archs);
}
/* Is it a thin 32-bit file? */
if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
{
macho->offset32 = 0;
macho->end32 = grub_file_size (file);
}
/* Is it a thin 64-bit file? */
if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
{
macho->offset64 = 0;
macho->end64 = grub_file_size (file);
}
grub_macho_parse32 (macho);
grub_macho_parse64 (macho);
return macho;
fail:
grub_macho_close (macho);
return 0;
}
grub_macho_t
grub_macho_open (const char *name)
{
grub_file_t file;
grub_macho_t macho;
file = grub_file_open (name);
if (! file)
return 0;
macho = grub_macho_file (file);
if (! macho)
grub_file_close (file);
return macho;
}

View file

@ -0,0 +1,18 @@
#include <grub/cpu/macho.h>
#include <grub/machoload.h>
#define SUFFIX(x) x ## 32
typedef struct grub_macho_header32 grub_macho_header_t;
typedef struct grub_macho_segment32 grub_macho_segment_t;
typedef grub_uint32_t grub_macho_addr_t;
typedef struct grub_macho_thread32 grub_macho_thread_t;
#define offsetXX offset32
#define ncmdsXX ncmds32
#define cmdsizeXX cmdsize32
#define cmdsXX cmds32
#define endXX end32
#define XX "32"
#define GRUB_MACHO_MAGIC GRUB_MACHO_MAGIC32
#define GRUB_MACHO_CMD_SEGMENT GRUB_MACHO_CMD_SEGMENT32
#include "machoXX.c"

View file

@ -0,0 +1,18 @@
#include <grub/cpu/macho.h>
#include <grub/machoload.h>
#define SUFFIX(x) x ## 64
typedef struct grub_macho_header64 grub_macho_header_t;
typedef struct grub_macho_segment64 grub_macho_segment_t;
typedef grub_uint64_t grub_macho_addr_t;
typedef struct grub_macho_thread64 grub_macho_thread_t;
#define offsetXX offset64
#define ncmdsXX ncmds64
#define cmdsizeXX cmdsize64
#define cmdsXX cmds64
#define endXX end64
#define XX "64"
#define GRUB_MACHO_MAGIC GRUB_MACHO_MAGIC64
#define GRUB_MACHO_CMD_SEGMENT GRUB_MACHO_CMD_SEGMENT64
#include "machoXX.c"

239
grub-core/loader/machoXX.c Normal file
View file

@ -0,0 +1,239 @@
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
int
SUFFIX (grub_macho_contains_macho) (grub_macho_t macho)
{
return macho->offsetXX != -1;
}
void
SUFFIX (grub_macho_parse) (grub_macho_t macho)
{
grub_macho_header_t head;
/* Is there any candidate at all? */
if (macho->offsetXX == -1)
return;
/* Read header and check magic*/
if (grub_file_seek (macho->file, macho->offsetXX) == (grub_off_t) -1
|| grub_file_read (macho->file, &head, sizeof (head))
!= sizeof(head))
{
grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
macho->offsetXX = -1;
return;
}
if (head.magic != GRUB_MACHO_MAGIC)
{
grub_error (GRUB_ERR_BAD_OS, "invalid Mach-O " XX "-bit header");
macho->offsetXX = -1;
return;
}
/* Read commands. */
macho->ncmdsXX = head.ncmds;
macho->cmdsizeXX = head.sizeofcmds;
macho->cmdsXX = grub_malloc(macho->cmdsizeXX);
if (! macho->cmdsXX)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, "not enough memory to read commands");
return;
}
if (grub_file_read (macho->file, macho->cmdsXX,
(grub_size_t) macho->cmdsizeXX)
!= (grub_ssize_t) macho->cmdsizeXX)
{
grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
macho->offsetXX = -1;
}
}
typedef int NESTED_FUNC_ATTR (*grub_macho_iter_hook_t)
(grub_macho_t , struct grub_macho_cmd *,
void *);
static grub_err_t
grub_macho_cmds_iterate (grub_macho_t macho,
grub_macho_iter_hook_t hook,
void *hook_arg)
{
grub_uint8_t *hdrs = macho->cmdsXX;
int i;
if (! macho->cmdsXX)
return grub_error (GRUB_ERR_BAD_OS, "couldn't find " XX "-bit Mach-O");
for (i = 0; i < macho->ncmdsXX; i++)
{
struct grub_macho_cmd *hdr = (struct grub_macho_cmd *) hdrs;
if (hook (macho, hdr, hook_arg))
break;
hdrs += hdr->cmdsize;
}
return grub_errno;
}
grub_size_t
SUFFIX (grub_macho_filesize) (grub_macho_t macho)
{
if (SUFFIX (grub_macho_contains_macho) (macho))
return macho->endXX - macho->offsetXX;
return 0;
}
grub_err_t
SUFFIX (grub_macho_readfile) (grub_macho_t macho, void *dest)
{
grub_ssize_t read;
if (! SUFFIX (grub_macho_contains_macho) (macho))
return grub_error (GRUB_ERR_BAD_OS,
"couldn't read architecture-specific part");
if (grub_file_seek (macho->file, macho->offsetXX) == (grub_off_t) -1)
{
grub_error_push ();
return grub_error (GRUB_ERR_BAD_OS,
"invalid offset in program header");
}
read = grub_file_read (macho->file, dest,
macho->endXX - macho->offsetXX);
if (read != (grub_ssize_t) (macho->endXX - macho->offsetXX))
{
grub_error_push ();
return grub_error (GRUB_ERR_BAD_OS,
"couldn't read architecture-specific part");
}
return GRUB_ERR_NONE;
}
/* Calculate the amount of memory spanned by the segments. */
grub_err_t
SUFFIX (grub_macho_size) (grub_macho_t macho, grub_macho_addr_t *segments_start,
grub_macho_addr_t *segments_end, int flags)
{
int nr_phdrs = 0;
/* Run through the program headers to calculate the total memory size we
should claim. */
auto int NESTED_FUNC_ATTR calcsize (grub_macho_t _macho,
struct grub_macho_cmd *phdr, void *_arg);
int NESTED_FUNC_ATTR calcsize (grub_macho_t _macho __attribute__ ((unused)),
struct grub_macho_cmd *hdr0,
void *_arg __attribute__ ((unused)))
{
grub_macho_segment_t *hdr = (grub_macho_segment_t *) hdr0;
if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT)
return 0;
if (! hdr->vmsize)
return 0;
if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
return 0;
nr_phdrs++;
if (hdr->vmaddr < *segments_start)
*segments_start = hdr->vmaddr;
if (hdr->vmaddr + hdr->vmsize > *segments_end)
*segments_end = hdr->vmaddr + hdr->vmsize;
return 0;
}
*segments_start = (grub_macho_addr_t) -1;
*segments_end = 0;
grub_macho_cmds_iterate (macho, calcsize, 0);
if (nr_phdrs == 0)
return grub_error (GRUB_ERR_BAD_OS, "no program headers present");
if (*segments_end < *segments_start)
/* Very bad addresses. */
return grub_error (GRUB_ERR_BAD_OS, "bad program header load addresses");
return GRUB_ERR_NONE;
}
/* Load every loadable segment into memory specified by `_load_hook'. */
grub_err_t
SUFFIX (grub_macho_load) (grub_macho_t macho, char *offset, int flags)
{
grub_err_t err = 0;
auto int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
struct grub_macho_cmd *hdr0,
void *_arg __attribute__ ((unused)));
int NESTED_FUNC_ATTR do_load(grub_macho_t _macho,
struct grub_macho_cmd *hdr0,
void *_arg __attribute__ ((unused)))
{
grub_macho_segment_t *hdr = (grub_macho_segment_t *) hdr0;
if (hdr->cmd != GRUB_MACHO_CMD_SEGMENT)
return 0;
if (! hdr->filesize && (flags & GRUB_MACHO_NOBSS))
return 0;
if (! hdr->vmsize)
return 0;
if (grub_file_seek (_macho->file, hdr->fileoff
+ _macho->offsetXX) == (grub_off_t) -1)
{
grub_error_push ();
grub_error (GRUB_ERR_BAD_OS,
"invalid offset in program header");
return 1;
}
if (hdr->filesize)
{
grub_ssize_t read;
read = grub_file_read (_macho->file, offset + hdr->vmaddr,
min (hdr->filesize, hdr->vmsize));
if (read != (grub_ssize_t) min (hdr->filesize, hdr->vmsize))
{
/* XXX How can we free memory from `load_hook'? */
grub_error_push ();
err=grub_error (GRUB_ERR_BAD_OS,
"couldn't read segment from file: "
"wanted 0x%lx bytes; read 0x%lx bytes",
hdr->filesize, read);
return 1;
}
}
if (hdr->filesize < hdr->vmsize)
grub_memset (offset + hdr->vmaddr + hdr->filesize,
0, hdr->vmsize - hdr->filesize);
return 0;
}
grub_macho_cmds_iterate (macho, do_load, 0);
return err;
}
grub_macho_addr_t
SUFFIX (grub_macho_get_entry_point) (grub_macho_t macho)
{
grub_macho_addr_t entry_point = 0;
auto int NESTED_FUNC_ATTR hook(grub_macho_t _macho,
struct grub_macho_cmd *hdr,
void *_arg __attribute__ ((unused)));
int NESTED_FUNC_ATTR hook(grub_macho_t _macho __attribute__ ((unused)),
struct grub_macho_cmd *hdr,
void *_arg __attribute__ ((unused)))
{
if (hdr->cmd == GRUB_MACHO_CMD_THREAD)
entry_point = ((grub_macho_thread_t *) hdr)->entry_point;
return 0;
}
grub_macho_cmds_iterate (macho, hook, 0);
return entry_point;
}

View file

@ -0,0 +1,416 @@
/* linux.c - boot Linux */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2007,2009,2010 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/elf.h>
#include <grub/elfload.h>
#include <grub/loader.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/command.h>
#include <grub/mips/relocator.h>
#include <grub/machine/memory.h>
#include <grub/i18n.h>
/* For frequencies. */
#include <grub/pci.h>
#include <grub/machine/time.h>
static grub_dl_t my_mod;
static int loaded;
static grub_size_t linux_size;
static struct grub_relocator *relocator;
static grub_uint8_t *playground;
static grub_addr_t target_addr, entry_addr;
static int linux_argc;
static grub_off_t argv_off, envp_off;
static grub_off_t rd_addr_arg_off, rd_size_arg_off;
static int initrd_loaded = 0;
static grub_err_t
grub_linux_boot (void)
{
struct grub_relocator32_state state;
/* Boot the kernel. */
state.gpr[1] = entry_addr;
state.gpr[4] = linux_argc;
state.gpr[5] = target_addr + argv_off;
state.gpr[6] = target_addr + envp_off;
state.jumpreg = 1;
grub_relocator32_boot (relocator, state);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_unload (void)
{
grub_relocator_unload (relocator);
grub_dl_unref (my_mod);
loaded = 0;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
{
Elf32_Addr base;
int extraoff;
grub_err_t err;
/* Linux's entry point incorrectly contains a virtual address. */
entry_addr = elf->ehdr.ehdr32.e_entry;
linux_size = grub_elf32_size (elf, &base);
if (linux_size == 0)
return grub_errno;
target_addr = base;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
linux_size = ALIGN_UP (base + linux_size, 4) - base;
extraoff = linux_size;
linux_size += extra_size;
relocator = grub_relocator_new ();
if (!relocator)
return grub_errno;
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
target_addr & 0x1fffffff,
linux_size);
if (err)
return err;
playground = get_virtual_current_address (ch);
}
*extra_mem = playground + extraoff;
/* Now load the segments into the area we claimed. */
auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load);
grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load)
{
if (phdr->p_type != PT_LOAD)
{
*do_load = 0;
return 0;
}
*do_load = 1;
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (grub_addr_t) (phdr->p_paddr - base + playground);
return 0;
}
return grub_elf32_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)
{
Elf64_Addr base;
int extraoff;
grub_err_t err;
/* Linux's entry point incorrectly contains a virtual address. */
entry_addr = elf->ehdr.ehdr64.e_entry;
linux_size = grub_elf64_size (elf, &base);
if (linux_size == 0)
return grub_errno;
target_addr = base;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
linux_size = ALIGN_UP (base + linux_size, 4) - base;
extraoff = linux_size;
linux_size += extra_size;
relocator = grub_relocator_new ();
if (!relocator)
return grub_errno;
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
target_addr & 0x1fffffff,
linux_size);
if (err)
return err;
playground = get_virtual_current_address (ch);
}
*extra_mem = playground + extraoff;
/* Now load the segments into the area we claimed. */
auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
{
if (phdr->p_type != PT_LOAD)
{
*do_load = 0;
return 0;
}
*do_load = 1;
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (grub_addr_t) (phdr->p_paddr - base + playground);
return 0;
}
return grub_elf64_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_elf_t elf = 0;
int i;
int size;
void *extra = NULL;
grub_uint32_t *linux_argv, *linux_envp;
char *linux_args, *linux_envs;
grub_err_t err;
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
elf = grub_elf_open (argv[0]);
if (! elf)
return grub_errno;
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
{
grub_elf_close (elf);
return grub_error (GRUB_ERR_UNKNOWN_OS,
"this ELF file is not of the right type\n");
}
/* Release the previously used memory. */
grub_loader_unset ();
loaded = 0;
/* For arguments. */
linux_argc = argc;
/* Main arguments. */
size = (linux_argc) * sizeof (grub_uint32_t);
/* Initrd address and size. */
size += 2 * sizeof (grub_uint32_t);
/* NULL terminator. */
size += sizeof (grub_uint32_t);
/* First argument is always "a0". */
size += ALIGN_UP (sizeof ("a0"), 4);
/* Normal arguments. */
for (i = 1; i < argc; i++)
size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4);
/* rd arguments. */
size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4);
size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4);
/* For the environment. */
size += sizeof (grub_uint32_t);
size += 4 * sizeof (grub_uint32_t);
size += ALIGN_UP (sizeof ("memsize=XXXXXXXXXXXXXXXXXXXX"), 4)
+ ALIGN_UP (sizeof ("highmemsize=XXXXXXXXXXXXXXXXXXXX"), 4)
+ ALIGN_UP (sizeof ("busclock=XXXXXXXXXX"), 4)
+ ALIGN_UP (sizeof ("cpuclock=XXXXXXXXXX"), 4);
if (grub_elf_is_elf32 (elf))
err = grub_linux_load32 (elf, &extra, size);
else
if (grub_elf_is_elf64 (elf))
err = grub_linux_load64 (elf, &extra, size);
else
err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
grub_elf_close (elf);
if (err)
return err;
linux_argv = extra;
argv_off = (grub_uint8_t *) linux_argv - (grub_uint8_t *) playground;
extra = linux_argv + (linux_argc + 1 + 2);
linux_args = extra;
grub_memcpy (linux_args, "a0", sizeof ("a0"));
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
+ target_addr;
linux_argv++;
linux_args += ALIGN_UP (sizeof ("a0"), 4);
for (i = 1; i < argc; i++)
{
grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1);
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
+ target_addr;
linux_argv++;
linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4);
}
/* Reserve space for rd arguments. */
rd_addr_arg_off = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground;
linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4);
*linux_argv = 0;
linux_argv++;
rd_size_arg_off = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground;
linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4);
*linux_argv = 0;
linux_argv++;
*linux_argv = 0;
extra = linux_args;
linux_envp = extra;
envp_off = (grub_uint8_t *) linux_envp - (grub_uint8_t *) playground;
linux_envs = (char *) (linux_envp + 5);
grub_snprintf (linux_envs, sizeof ("memsize=XXXXXXXXXXXXXXXXXXXX"),
"memsize=%lld",
(unsigned long long) grub_mmap_get_lower () >> 20);
linux_envp[0] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
+ target_addr;
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
grub_snprintf (linux_envs, sizeof ("highmemsize=XXXXXXXXXXXXXXXXXXXX"),
"highmemsize=%lld",
(unsigned long long) grub_mmap_get_upper () >> 20);
linux_envp[1] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
+ target_addr;
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
grub_snprintf (linux_envs, sizeof ("busclock=XXXXXXXXXX"),
"busclock=%d", grub_arch_busclock);
linux_envp[2] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
+ target_addr;
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
grub_snprintf (linux_envs, sizeof ("cpuclock=XXXXXXXXXX"),
"cpuclock=%d", grub_arch_cpuclock);
linux_envp[3] = (grub_uint8_t *) linux_envs - (grub_uint8_t *) playground
+ target_addr;
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
linux_envp[4] = 0;
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
initrd_loaded = 0;
loaded = 1;
grub_dl_ref (my_mod);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
void *initrd_src;
grub_addr_t initrd_dest;
grub_err_t err;
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
if (!loaded)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load Linux first.");
if (initrd_loaded)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd can be loaded.");
grub_file_filter_disable_compression ();
file = grub_file_open (argv[0]);
if (! file)
return grub_errno;
size = grub_file_size (file);
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (relocator, &ch,
target_addr + linux_size + 0x10000,
(0xffffffff - size) + 1,
size, 0x10000,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
{
grub_file_close (file);
return err;
}
initrd_src = get_virtual_current_address (ch);
initrd_dest = get_physical_target_address (ch) | 0x80000000;
}
if (grub_file_read (file, initrd_src, size) != size)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
grub_file_close (file);
return grub_errno;
}
grub_snprintf ((char *) playground + rd_addr_arg_off,
sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%llx",
(unsigned long long) initrd_dest);
((grub_uint32_t *) (playground + argv_off))[linux_argc]
= target_addr + rd_addr_arg_off;
linux_argc++;
grub_snprintf ((char *) playground + rd_size_arg_off,
sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%llx",
(unsigned long long) size);
((grub_uint32_t *) (playground + argv_off))[linux_argc]
= target_addr + rd_size_arg_off;
linux_argc++;
initrd_loaded = 1;
grub_file_close (file);
return GRUB_ERR_NONE;
}
static grub_command_t cmd_linux, cmd_initrd;
GRUB_MOD_INIT(linux)
{
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
0, N_("Load Linux."));
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
GRUB_MOD_FINI(linux)
{
grub_unregister_command (cmd_linux);
grub_unregister_command (cmd_initrd);
}

View file

@ -0,0 +1,356 @@
/* multiboot.c - boot a multiboot OS image. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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/>.
*/
/*
* FIXME: The following features from the Multiboot specification still
* need to be implemented:
* - drives table
* - ROM configuration table
* - APM table
*/
#include <grub/loader.h>
#include <grub/command.h>
#include <grub/multiboot.h>
#include <grub/cpu/multiboot.h>
#include <grub/machine/memory.h>
#include <grub/elf.h>
#include <grub/aout.h>
#include <grub/file.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/env.h>
#include <grub/cpu/relocator.h>
#include <grub/video.h>
#include <grub/memory.h>
#include <grub/i18n.h>
#ifdef GRUB_MACHINE_EFI
#include <grub/efi/efi.h>
#endif
struct grub_relocator *grub_multiboot_relocator = NULL;
grub_uint32_t grub_multiboot_payload_eip;
#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
#define DEFAULT_VIDEO_MODE "text"
#else
#define DEFAULT_VIDEO_MODE "auto"
#endif
static int accepts_video;
static int accepts_ega_text;
static int console_required;
static grub_dl_t my_mod;
/* Return the length of the Multiboot mmap that will be needed to allocate
our platform's map. */
grub_uint32_t
grub_get_multiboot_mmap_count (void)
{
grub_size_t count = 0;
auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
int NESTED_FUNC_ATTR hook (grub_uint64_t addr __attribute__ ((unused)),
grub_uint64_t size __attribute__ ((unused)),
grub_uint32_t type __attribute__ ((unused)))
{
count++;
return 0;
}
grub_mmap_iterate (hook);
return count;
}
grub_err_t
grub_multiboot_set_video_mode (void)
{
grub_err_t err;
const char *modevar;
if (accepts_video || !GRUB_MACHINE_HAS_VGA_TEXT)
{
modevar = grub_env_get ("gfxpayload");
if (! modevar || *modevar == 0)
err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
else
{
char *tmp;
tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
if (! tmp)
return grub_errno;
err = grub_video_set_mode (tmp, 0, 0);
grub_free (tmp);
}
}
else
err = grub_video_set_mode ("text", 0, 0);
return err;
}
static grub_err_t
grub_multiboot_boot (void)
{
grub_err_t err;
struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
if (err)
return err;
#ifdef GRUB_MACHINE_EFI
err = grub_efi_finish_boot_services (NULL, NULL, NULL, NULL, NULL);
if (err)
return err;
#endif
grub_relocator32_boot (grub_multiboot_relocator, state);
/* Not reached. */
return GRUB_ERR_NONE;
}
static grub_err_t
grub_multiboot_unload (void)
{
grub_multiboot_free_mbi ();
grub_relocator_unload (grub_multiboot_relocator);
grub_multiboot_relocator = NULL;
grub_dl_unref (my_mod);
return GRUB_ERR_NONE;
}
#define MULTIBOOT_LOAD_ELF64
#include "multiboot_elfxx.c"
#undef MULTIBOOT_LOAD_ELF64
#define MULTIBOOT_LOAD_ELF32
#include "multiboot_elfxx.c"
#undef MULTIBOOT_LOAD_ELF32
/* Load ELF32 or ELF64. */
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);
return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
}
grub_err_t
grub_multiboot_set_console (int console_type, int accepted_consoles,
int width, int height, int depth,
int console_req)
{
console_required = console_req;
if (!(accepted_consoles
& (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
| (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
{
if (console_required)
return grub_error (GRUB_ERR_BAD_OS,
"OS requires a console but none is available");
grub_printf ("WARNING: no console will be available to OS");
accepts_video = 0;
accepts_ega_text = 0;
return GRUB_ERR_NONE;
}
if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
{
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");
if (!buf)
return grub_errno;
grub_env_set ("gfxpayload", buf);
grub_free (buf);
}
else
grub_env_set ("gfxpayload", "text");
accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_err_t err;
grub_loader_unset ();
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
file = grub_file_open (argv[0]);
if (! file)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
grub_dl_ref (my_mod);
/* Skip filename. */
grub_multiboot_init_mbi (argc - 1, argv + 1);
grub_relocator_unload (grub_multiboot_relocator);
grub_multiboot_relocator = grub_relocator_new ();
if (!grub_multiboot_relocator)
goto fail;
err = grub_multiboot_load (file);
if (err)
goto fail;
grub_multiboot_set_bootdev ();
grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
fail:
if (file)
grub_file_close (file);
if (grub_errno != GRUB_ERR_NONE)
{
grub_relocator_unload (grub_multiboot_relocator);
grub_multiboot_relocator = NULL;
grub_dl_unref (my_mod);
}
return grub_errno;
}
static grub_err_t
grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
void *module = NULL;
grub_addr_t target;
grub_err_t err;
int nounzip = 0;
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
if (grub_strcmp (argv[0], "--nounzip") == 0)
{
argv++;
argc--;
nounzip = 1;
}
if (argc == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
if (!grub_multiboot_relocator)
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"you need to load the multiboot kernel first");
if (nounzip)
grub_file_filter_disable_compression ();
file = grub_file_open (argv[0]);
if (! file)
return grub_errno;
size = grub_file_size (file);
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
0, (0xffffffff - size) + 1,
size, MULTIBOOT_MOD_ALIGN,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
{
grub_file_close (file);
return err;
}
module = get_virtual_current_address (ch);
target = (grub_addr_t) get_virtual_current_address (ch);
}
err = grub_multiboot_add_module (target, size, argc - 1, argv + 1);
if (err)
{
grub_file_close (file);
return err;
}
if (grub_file_read (file, module, size) != size)
{
grub_file_close (file);
return grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
}
grub_file_close (file);
return GRUB_ERR_NONE;;
}
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."));
cmd_module =
grub_register_command ("module2", grub_cmd_module,
0, N_("Load a multiboot 2 module."));
#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."));
#endif
my_mod = mod;
}
GRUB_MOD_FINI(multiboot)
{
grub_unregister_command (cmd_multiboot);
grub_unregister_command (cmd_module);
}

View file

@ -0,0 +1,225 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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/>.
*/
#if defined(MULTIBOOT_LOAD_ELF32)
# define XX 32
# define E_MACHINE MULTIBOOT_ELF32_MACHINE
# define ELFCLASSXX ELFCLASS32
# define Elf_Ehdr Elf32_Ehdr
# define Elf_Phdr Elf32_Phdr
#elif defined(MULTIBOOT_LOAD_ELF64)
# define XX 64
# define E_MACHINE MULTIBOOT_ELF64_MACHINE
# define ELFCLASSXX ELFCLASS64
# define Elf_Ehdr Elf64_Ehdr
# define Elf_Phdr Elf64_Phdr
#else
#error "I'm confused"
#endif
#include <grub/i386/relocator.h>
#define CONCAT(a,b) CONCAT_(a, b)
#define CONCAT_(a,b) a ## b
/* Check if BUFFER contains ELF32 (or ELF64). */
static int
CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
{
Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
}
static grub_err_t
CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
{
Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
char *phdr_base;
int i;
if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX)
return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
if (ehdr->e_ident[EI_MAG0] != ELFMAG0
|| ehdr->e_ident[EI_MAG1] != ELFMAG1
|| ehdr->e_ident[EI_MAG2] != ELFMAG2
|| ehdr->e_ident[EI_MAG3] != ELFMAG3
|| ehdr->e_version != EV_CURRENT
|| ehdr->e_ident[EI_DATA] != ELFDATA2LSB
|| ehdr->e_machine != E_MACHINE)
return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
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 > MULTIBOOT_SEARCH)
return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
#if defined (MULTIBOOT_LOAD_ELF64) && defined (__mips)
/* We still in 32-bit mode. */
if (ehdr->e_entry < 0xffffffff80000000ULL)
return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
#else
/* We still in 32-bit mode. */
if (ehdr->e_entry > 0xffffffff)
return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
#endif
phdr_base = (char *) buffer + ehdr->e_phoff;
#define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
/* Load every loadable segment in memory. */
for (i = 0; i < ehdr->e_phnum; i++)
{
if (phdr(i)->p_type == PT_LOAD)
{
grub_err_t err;
void *source;
grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
&ch, phdr(i)->p_paddr,
phdr(i)->p_memsz);
if (err)
{
grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
return err;
}
source = get_virtual_current_address (ch);
}
if (phdr(i)->p_filesz != 0)
{
if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
== (grub_off_t) -1)
return grub_error (GRUB_ERR_BAD_OS,
"invalid offset in program header");
if (grub_file_read (file, source, phdr(i)->p_filesz)
!= (grub_ssize_t) phdr(i)->p_filesz)
return grub_error (GRUB_ERR_BAD_OS,
"couldn't read segment from file");
}
if (phdr(i)->p_filesz < phdr(i)->p_memsz)
grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0,
phdr(i)->p_memsz - phdr(i)->p_filesz);
}
}
for (i = 0; i < ehdr->e_phnum; i++)
if (phdr(i)->p_vaddr <= ehdr->e_entry
&& phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
{
grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr)
+ phdr(i)->p_paddr;
break;
}
if (i == ehdr->e_phnum)
return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
#if defined (__i386__) || defined (__x86_64__)
#elif defined (__mips)
grub_multiboot_payload_eip |= 0x80000000;
#else
#error Please complete this
#endif
if (ehdr->e_shnum)
{
grub_uint8_t *shdr, *shdrptr;
shdr = grub_malloc (ehdr->e_shnum * ehdr->e_shentsize);
if (!shdr)
return grub_errno;
if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1)
return grub_error (GRUB_ERR_BAD_OS,
"invalid offset to section headers");
if (grub_file_read (file, shdr, ehdr->e_shnum * ehdr->e_shentsize)
!= (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
return grub_error (GRUB_ERR_BAD_OS,
"couldn't read sections headers from file");
for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
shdrptr += ehdr->e_shentsize, i++)
{
Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
void *src;
grub_addr_t target;
grub_err_t err;
/* This section is a loaded section,
so we don't care. */
if (sh->sh_addr != 0)
continue;
/* This section is empty, so we don't care. */
if (sh->sh_size == 0)
continue;
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
&ch, 0,
(0xffffffff - sh->sh_size)
+ 1, sh->sh_size,
sh->sh_addralign,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
{
grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
return err;
}
src = get_virtual_current_address (ch);
target = get_physical_target_address (ch);
}
if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1)
return grub_error (GRUB_ERR_BAD_OS,
"invalid offset in section header");
if (grub_file_read (file, src, sh->sh_size)
!= (grub_ssize_t) sh->sh_size)
return grub_error (GRUB_ERR_BAD_OS,
"couldn't read segment from file");
sh->sh_addr = target;
}
grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize,
ehdr->e_shstrndx, shdr);
}
#undef phdr
return grub_errno;
}
#undef XX
#undef E_MACHINE
#undef ELFCLASSXX
#undef Elf_Ehdr
#undef Elf_Phdr

View file

@ -0,0 +1,150 @@
/* multiboot_loader.c - boot multiboot kernel image */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2010 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/multiboot.h>
#include <grub/elf.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>
#include <grub/command.h>
#include <grub/i18n.h>
grub_dl_t my_mod;
static int
find_multi_boot1_header (grub_file_t file)
{
struct 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 multiboot_header *) buffer;
((char *) header <= buffer + len - 12) || (header = 0);
header = (struct multiboot_header *) ((char *) header + 4))
{
if (header->magic == MULTIBOOT_HEADER_MAGIC
&& !(header->magic + header->flags + header->checksum))
{
found_status = 1;
break;
}
}
return found_status;
}
static grub_err_t
grub_cmd_multiboot_loader (grub_command_t cmd __attribute__ ((unused)),
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
{
grub_error (GRUB_ERR_BAD_OS, "multiboot header not found");
goto fail;
}
/* close file before calling functions */
if (file)
grub_file_close (file);
/* Launch multi boot with header */
grub_dprintf ("multiboot_loader",
"Launching multiboot 1 grub_multiboot() function\n");
grub_multiboot (argc, argv);
return grub_errno;
fail:
if (file)
grub_file_close (file);
grub_dl_unref (my_mod);
return grub_errno;
}
static grub_err_t
grub_cmd_module_loader (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_dprintf("multiboot_loader",
"Launching multiboot 1 grub_module() function\n");
grub_module (argc, argv);
return grub_errno;
}
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_loader,
0, N_("Load a multiboot 2 kernel."));
#else
grub_register_command ("multiboot", grub_cmd_multiboot_loader,
0, N_("Load a multiboot kernel."));
#endif
cmd_module =
grub_register_command ("module", grub_cmd_module_loader,
0, N_("Load a multiboot module."));
my_mod = mod;
}
GRUB_MOD_FINI(multiboot)
{
grub_unregister_command (cmd_multiboot);
grub_unregister_command (cmd_module);
}

View file

@ -0,0 +1,743 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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/machine/memory.h>
#include <grub/memory.h>
#ifdef GRUB_MACHINE_PCBIOS
#include <grub/machine/biosnum.h>
#endif
#include <grub/multiboot.h>
#include <grub/cpu/multiboot.h>
#include <grub/cpu/relocator.h>
#include <grub/disk.h>
#include <grub/device.h>
#include <grub/partition.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/env.h>
#include <grub/video.h>
#if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_QEMU)
#include <grub/i386/pc/vbe.h>
#define HAS_VGA_TEXT 1
#else
#define HAS_VGA_TEXT 0
#endif
struct module
{
struct module *next;
grub_addr_t start;
grub_size_t size;
char *cmdline;
int cmdline_size;
};
struct module *modules, *modules_last;
static grub_size_t cmdline_size;
static grub_size_t total_modcmd;
static unsigned modcnt;
static char *cmdline = NULL;
static int bootdev_set;
static grub_uint32_t biosdev, slice, part;
static grub_size_t elf_sec_num, elf_sec_entsize;
static unsigned elf_sec_shstrndx;
static void *elf_sections;
void
grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
unsigned shndx, void *data)
{
elf_sec_num = num;
elf_sec_shstrndx = shndx;
elf_sec_entsize = entsize;
elf_sections = data;
}
grub_err_t
grub_multiboot_load (grub_file_t file)
{
char *buffer;
grub_ssize_t len;
struct multiboot_header *header;
grub_err_t err;
struct multiboot_header_tag *tag;
struct multiboot_header_tag_address *addr_tag = NULL;
int entry_specified = 0;
grub_addr_t entry = 0;
grub_uint32_t console_required = 0;
struct multiboot_header_tag_framebuffer *fbtag = NULL;
int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
buffer = grub_malloc (MULTIBOOT_SEARCH);
if (!buffer)
return grub_errno;
len = grub_file_read (file, buffer, MULTIBOOT_SEARCH);
if (len < 32)
{
grub_free (buffer);
return grub_error (GRUB_ERR_BAD_OS, "file too small");
}
/* 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 multiboot_header *) buffer;
((char *) header <= buffer + len - 12) || (header = 0);
header = (struct multiboot_header *) ((char *) header + MULTIBOOT_HEADER_ALIGN))
{
if (header->magic == MULTIBOOT_HEADER_MAGIC
&& !(header->magic + header->architecture
+ header->header_length + header->checksum)
&& header->architecture == MULTIBOOT_ARCHITECTURE_CURRENT)
break;
}
if (header == 0)
{
grub_free (buffer);
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
}
for (tag = (struct multiboot_header_tag *) (header + 1);
tag->type != MULTIBOOT_TAG_TYPE_END;
tag = (struct multiboot_header_tag *) ((char *) tag + tag->size))
switch (tag->type)
{
case MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST:
{
unsigned i;
struct multiboot_header_tag_information_request *request_tag
= (struct multiboot_header_tag_information_request *) tag;
if (request_tag->flags & MULTIBOOT_HEADER_TAG_OPTIONAL)
break;
for (i = 0; i < (request_tag->size - sizeof (request_tag))
/ sizeof (request_tag->requests[0]); i++)
switch (request_tag->requests[i])
{
case MULTIBOOT_TAG_TYPE_END:
case MULTIBOOT_TAG_TYPE_CMDLINE:
case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
case MULTIBOOT_TAG_TYPE_MODULE:
case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
case MULTIBOOT_TAG_TYPE_BOOTDEV:
case MULTIBOOT_TAG_TYPE_MMAP:
case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
break;
case MULTIBOOT_TAG_TYPE_VBE:
case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
case MULTIBOOT_TAG_TYPE_APM:
default:
grub_free (buffer);
return grub_error (GRUB_ERR_UNKNOWN_OS,
"unsupported information tag: 0x%x",
request_tag->requests[i]);
}
break;
}
case MULTIBOOT_HEADER_TAG_ADDRESS:
addr_tag = (struct multiboot_header_tag_address *) tag;
break;
case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS:
entry_specified = 1;
entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
break;
case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
& MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
accepted_consoles &= ~GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
if (((struct multiboot_header_tag_console_flags *) tag)->console_flags
& MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED)
console_required = 1;
break;
case MULTIBOOT_HEADER_TAG_FRAMEBUFFER:
fbtag = (struct multiboot_header_tag_framebuffer *) tag;
accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
break;
/* GRUB always page-aligns modules. */
case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
break;
default:
if (! (tag->flags & MULTIBOOT_HEADER_TAG_OPTIONAL))
{
grub_free (buffer);
return grub_error (GRUB_ERR_UNKNOWN_OS,
"unsupported tag: 0x%x", tag->type);
}
break;
}
if (addr_tag && !entry_specified)
{
grub_free (buffer);
return grub_error (GRUB_ERR_UNKNOWN_OS,
"load address tag without entry address tag");
}
if (addr_tag)
{
int offset = ((char *) header - buffer -
(addr_tag->header_addr - addr_tag->load_addr));
int load_size = ((addr_tag->load_end_addr == 0) ? file->size - offset :
addr_tag->load_end_addr - addr_tag->load_addr);
grub_size_t code_size;
void *source;
grub_relocator_chunk_t ch;
if (addr_tag->bss_end_addr)
code_size = (addr_tag->bss_end_addr - addr_tag->load_addr);
else
code_size = load_size;
err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
&ch, addr_tag->load_addr,
code_size);
if (err)
{
grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
grub_free (buffer);
return err;
}
source = get_virtual_current_address (ch);
if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
{
grub_free (buffer);
return grub_errno;
}
grub_file_read (file, source, load_size);
if (grub_errno)
{
grub_free (buffer);
return grub_errno;
}
if (addr_tag->bss_end_addr)
grub_memset ((grub_uint32_t *) source + load_size, 0,
addr_tag->bss_end_addr - addr_tag->load_addr - load_size);
}
else
{
err = grub_multiboot_load_elf (file, buffer);
if (err)
{
grub_free (buffer);
return err;
}
}
if (entry_specified)
grub_multiboot_payload_eip = entry;
if (fbtag)
err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
accepted_consoles,
fbtag->width, fbtag->height,
fbtag->depth, console_required);
else
err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_EGA_TEXT,
accepted_consoles,
0, 0, 0, console_required);
return err;
}
static grub_size_t
grub_multiboot_get_mbi_size (void)
{
return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
+ (sizeof (struct multiboot_tag_string)
+ ALIGN_UP (cmdline_size, MULTIBOOT_TAG_ALIGN))
+ (sizeof (struct multiboot_tag_string)
+ ALIGN_UP (sizeof (PACKAGE_STRING), MULTIBOOT_TAG_ALIGN))
+ (modcnt * sizeof (struct multiboot_tag_module) + total_modcmd)
+ sizeof (struct multiboot_tag_basic_meminfo)
+ ALIGN_UP (sizeof (struct multiboot_tag_bootdev), MULTIBOOT_TAG_ALIGN)
+ sizeof (struct multiboot_tag_elf_sections)
+ elf_sec_entsize * elf_sec_num
+ (sizeof (struct multiboot_tag_mmap) + grub_get_multiboot_mmap_count ()
* sizeof (struct multiboot_mmap_entry))
+ sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1;
}
/* Fill previously allocated Multiboot mmap. */
static void
grub_fill_multiboot_mmap (struct multiboot_tag_mmap *tag)
{
struct multiboot_mmap_entry *mmap_entry = tag->entries;
auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type)
{
mmap_entry->addr = addr;
mmap_entry->len = size;
switch (type)
{
case GRUB_MACHINE_MEMORY_AVAILABLE:
mmap_entry->type = MULTIBOOT_MEMORY_AVAILABLE;
break;
#ifdef GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE
case GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE:
mmap_entry->type = MULTIBOOT_MEMORY_ACPI_RECLAIMABLE;
break;
#endif
#ifdef GRUB_MACHINE_MEMORY_NVS
case GRUB_MACHINE_MEMORY_NVS:
mmap_entry->type = MULTIBOOT_MEMORY_NVS;
break;
#endif
default:
mmap_entry->type = MULTIBOOT_MEMORY_RESERVED;
break;
}
mmap_entry++;
return 0;
}
tag->type = MULTIBOOT_TAG_TYPE_MMAP;
tag->size = sizeof (struct multiboot_tag_mmap)
+ sizeof (struct multiboot_mmap_entry) * grub_get_multiboot_mmap_count ();
tag->entry_size = sizeof (struct multiboot_mmap_entry);
tag->entry_version = 0;
grub_mmap_iterate (hook);
}
static grub_err_t
retrieve_video_parameters (grub_uint8_t **ptrorig)
{
grub_err_t err;
struct grub_video_mode_info mode_info;
void *framebuffer;
grub_video_driver_id_t driv_id;
struct grub_video_palette_data palette[256];
struct multiboot_tag_framebuffer *tag
= (struct multiboot_tag_framebuffer *) *ptrorig;
err = grub_multiboot_set_video_mode ();
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
grub_video_get_palette (0, ARRAY_SIZE (palette), palette);
driv_id = grub_video_get_driver_id ();
#if HAS_VGA_TEXT
if (driv_id == GRUB_VIDEO_DRIVER_NONE)
{
struct grub_vbe_mode_info_block vbe_mode_info;
grub_uint32_t vbe_mode;
#if defined (GRUB_MACHINE_PCBIOS)
{
grub_vbe_status_t status;
void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
status = grub_vbe_bios_get_mode (scratch);
vbe_mode = *(grub_uint32_t *) scratch;
if (status != GRUB_VBE_STATUS_OK)
return GRUB_ERR_NONE;
}
#else
vbe_mode = 3;
#endif
/* get_mode_info isn't available for mode 3. */
if (vbe_mode == 3)
{
grub_memset (&vbe_mode_info, 0,
sizeof (struct grub_vbe_mode_info_block));
vbe_mode_info.memory_model = GRUB_VBE_MEMORY_MODEL_TEXT;
vbe_mode_info.x_resolution = 80;
vbe_mode_info.y_resolution = 25;
}
#if defined (GRUB_MACHINE_PCBIOS)
else
{
grub_vbe_status_t status;
void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
status = grub_vbe_bios_get_mode_info (vbe_mode, scratch);
if (status != GRUB_VBE_STATUS_OK)
return GRUB_ERR_NONE;
grub_memcpy (&vbe_mode_info, scratch,
sizeof (struct grub_vbe_mode_info_block));
}
#endif
if (vbe_mode_info.memory_model == GRUB_VBE_MEMORY_MODEL_TEXT)
{
tag = (struct multiboot_tag_framebuffer *) *ptrorig;
tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
tag->common.size = 0;
tag->common.framebuffer_addr = 0xb8000;
tag->common.framebuffer_pitch = 2 * vbe_mode_info.x_resolution;
tag->common.framebuffer_width = vbe_mode_info.x_resolution;
tag->common.framebuffer_height = vbe_mode_info.y_resolution;
tag->common.framebuffer_bpp = 16;
tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
tag->common.size = sizeof (tag->common);
tag->common.reserved = 0;
*ptrorig += ALIGN_UP (tag->common.size, MULTIBOOT_TAG_ALIGN);
}
return GRUB_ERR_NONE;
}
#else
if (driv_id == GRUB_VIDEO_DRIVER_NONE)
return GRUB_ERR_NONE;
#endif
err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
if (err)
return err;
tag = (struct multiboot_tag_framebuffer *) *ptrorig;
tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
tag->common.size = 0;
tag->common.framebuffer_addr = (grub_addr_t) framebuffer;
tag->common.framebuffer_pitch = mode_info.pitch;
tag->common.framebuffer_width = mode_info.width;
tag->common.framebuffer_height = mode_info.height;
tag->common.framebuffer_bpp = mode_info.bpp;
tag->common.reserved = 0;
if (mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
{
unsigned i;
tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED;
tag->framebuffer_palette_num_colors = mode_info.number_of_colors;
if (tag->framebuffer_palette_num_colors > ARRAY_SIZE (palette))
tag->framebuffer_palette_num_colors = ARRAY_SIZE (palette);
tag->common.size = sizeof (struct multiboot_tag_framebuffer_common)
+ sizeof (multiboot_uint16_t) + tag->framebuffer_palette_num_colors
* sizeof (struct multiboot_color);
for (i = 0; i < tag->framebuffer_palette_num_colors; i++)
{
tag->framebuffer_palette[i].red = palette[i].r;
tag->framebuffer_palette[i].green = palette[i].g;
tag->framebuffer_palette[i].blue = palette[i].b;
}
}
else
{
tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
tag->framebuffer_red_field_position = mode_info.red_field_pos;
tag->framebuffer_red_mask_size = mode_info.red_mask_size;
tag->framebuffer_green_field_position = mode_info.green_field_pos;
tag->framebuffer_green_mask_size = mode_info.green_mask_size;
tag->framebuffer_blue_field_position = mode_info.blue_field_pos;
tag->framebuffer_blue_mask_size = mode_info.blue_mask_size;
tag->common.size = sizeof (struct multiboot_tag_framebuffer_common) + 6;
}
*ptrorig += ALIGN_UP (tag->common.size, MULTIBOOT_TAG_ALIGN);
return GRUB_ERR_NONE;
}
grub_err_t
grub_multiboot_make_mbi (grub_uint32_t *target)
{
grub_uint8_t *ptrorig;
grub_uint8_t *mbistart;
grub_err_t err;
grub_size_t bufsize;
grub_relocator_chunk_t ch;
bufsize = grub_multiboot_get_mbi_size ();
err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
0, 0xffffffff - bufsize,
bufsize, 4,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
ptrorig = get_virtual_current_address (ch);
#if defined (__i386__) || defined (__x86_64__)
*target = get_physical_target_address (ch);
#elif defined (__mips)
*target = get_physical_target_address (ch) | 0x80000000;
#else
#error Please complete this
#endif
mbistart = ptrorig;
ptrorig += 2 * sizeof (grub_uint32_t);
{
struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
tag->size = sizeof (struct multiboot_tag_string) + cmdline_size;
grub_memcpy (tag->string, cmdline, cmdline_size);
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
{
struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME;
tag->size = sizeof (struct multiboot_tag_string) + sizeof (PACKAGE_STRING);
grub_memcpy (tag->string, PACKAGE_STRING, sizeof (PACKAGE_STRING));
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
{
unsigned i;
struct module *cur;
for (i = 0, cur = modules; i < modcnt; i++, cur = cur->next)
{
struct multiboot_tag_module *tag
= (struct multiboot_tag_module *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_MODULE;
tag->size = sizeof (struct multiboot_tag_module) + cur->cmdline_size;
tag->mod_start = cur->start;
tag->mod_end = tag->mod_start + cur->size;
grub_memcpy (tag->cmdline, cur->cmdline, cur->cmdline_size);
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
}
{
struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
grub_fill_multiboot_mmap (tag);
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
{
struct multiboot_tag_elf_sections *tag
= (struct multiboot_tag_elf_sections *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS;
tag->size = sizeof (struct multiboot_tag_elf_sections)
+ elf_sec_entsize * elf_sec_num;
grub_memcpy (tag->sections, elf_sections, elf_sec_entsize * elf_sec_num);
tag->num = elf_sec_num;
tag->entsize = elf_sec_entsize;
tag->shndx = elf_sec_shstrndx;
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
{
struct multiboot_tag_basic_meminfo *tag
= (struct multiboot_tag_basic_meminfo *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
tag->size = sizeof (struct multiboot_tag_basic_meminfo);
/* Convert from bytes to kilobytes. */
tag->mem_lower = grub_mmap_get_lower () / 1024;
tag->mem_upper = grub_mmap_get_upper () / 1024;
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
if (bootdev_set)
{
struct multiboot_tag_bootdev *tag
= (struct multiboot_tag_bootdev *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_BOOTDEV;
tag->size = sizeof (struct multiboot_tag_bootdev);
tag->biosdev = biosdev;
tag->slice = slice;
tag->part = part;
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
{
err = retrieve_video_parameters (&ptrorig);
if (err)
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
}
{
struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
tag->type = MULTIBOOT_TAG_TYPE_END;
tag->size = sizeof (struct multiboot_tag);
ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN);
}
((grub_uint32_t *) mbistart)[0] = ptrorig - mbistart;
((grub_uint32_t *) mbistart)[1] = 0;
return GRUB_ERR_NONE;
}
void
grub_multiboot_free_mbi (void)
{
struct module *cur, *next;
cmdline_size = 0;
total_modcmd = 0;
modcnt = 0;
grub_free (cmdline);
cmdline = NULL;
bootdev_set = 0;
for (cur = modules; cur; cur = next)
{
next = cur->next;
grub_free (cur->cmdline);
grub_free (cur);
}
modules = NULL;
modules_last = NULL;
}
grub_err_t
grub_multiboot_init_mbi (int argc, char *argv[])
{
grub_ssize_t len = 0;
char *p;
int i;
grub_multiboot_free_mbi ();
for (i = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
if (len == 0)
len = 1;
cmdline = p = grub_malloc (len);
if (! cmdline)
return grub_errno;
cmdline_size = len;
for (i = 0; i < argc; i++)
{
p = grub_stpcpy (p, argv[i]);
*(p++) = ' ';
}
/* Remove the space after the last word. */
if (p != cmdline)
p--;
*p = '\0';
return GRUB_ERR_NONE;
}
grub_err_t
grub_multiboot_add_module (grub_addr_t start, grub_size_t size,
int argc, char *argv[])
{
struct module *newmod;
char *p;
grub_ssize_t len = 0;
int i;
newmod = grub_malloc (sizeof (*newmod));
if (!newmod)
return grub_errno;
newmod->start = start;
newmod->size = size;
for (i = 0; i < argc; i++)
len += grub_strlen (argv[i]) + 1;
if (len == 0)
len = 1;
newmod->cmdline = p = grub_malloc (len);
if (! newmod->cmdline)
{
grub_free (newmod);
return grub_errno;
}
newmod->cmdline_size = len;
total_modcmd += ALIGN_UP (len, MULTIBOOT_TAG_ALIGN);
for (i = 0; i < argc; i++)
{
p = grub_stpcpy (p, argv[i]);
*(p++) = ' ';
}
/* Remove the space after the last word. */
if (p != newmod->cmdline)
p--;
*p = '\0';
if (modules_last)
modules_last->next = newmod;
else
{
modules = newmod;
modules_last->next = NULL;
}
modules_last = newmod;
modcnt++;
return GRUB_ERR_NONE;
}
void
grub_multiboot_set_bootdev (void)
{
grub_device_t dev;
slice = ~0;
part = ~0;
#ifdef GRUB_MACHINE_PCBIOS
biosdev = grub_get_root_biosnumber ();
#else
biosdev = 0xffffffff;
#endif
if (biosdev == 0xffffffff)
return;
dev = grub_device_open (0);
if (dev && dev->disk && dev->disk->partition)
{
if (dev->disk->partition->parent)
{
part = dev->disk->partition->number;
slice = dev->disk->partition->parent->number;
}
else
slice = dev->disk->partition->number;
}
if (dev)
grub_device_close (dev);
bootdev_set = 1;
}

View file

@ -0,0 +1,363 @@
/* linux.c - boot Linux */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,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/elf.h>
#include <grub/elfload.h>
#include <grub/loader.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/ieee1275/ieee1275.h>
#include <grub/command.h>
#include <grub/i18n.h>
#define ELF32_LOADMASK (0xc0000000UL)
#define ELF64_LOADMASK (0xc000000000000000ULL)
static grub_dl_t my_mod;
static int loaded;
static grub_addr_t initrd_addr;
static grub_size_t initrd_size;
static grub_addr_t linux_addr;
static grub_size_t linux_size;
static char *linux_args;
typedef void (*kernel_entry_t) (void *, unsigned long, int (void *),
unsigned long, unsigned long);
static grub_err_t
grub_linux_boot (void)
{
kernel_entry_t linuxmain;
grub_ssize_t actual;
/* Set the command line arguments. */
grub_ieee1275_set_property (grub_ieee1275_chosen, "bootargs", linux_args,
grub_strlen (linux_args) + 1, &actual);
grub_dprintf ("loader", "Entry point: 0x%x\n", linux_addr);
grub_dprintf ("loader", "Initrd at: 0x%x, size 0x%x\n", initrd_addr,
initrd_size);
grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
grub_dprintf ("loader", "Jumping to Linux...\n");
/* Boot the kernel. */
linuxmain = (kernel_entry_t) linux_addr;
linuxmain ((void *) initrd_addr, initrd_size, grub_ieee1275_entry_fn, 0, 0);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_release_mem (void)
{
grub_free (linux_args);
linux_args = 0;
if (linux_addr && grub_ieee1275_release (linux_addr, linux_size))
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
if (initrd_addr && grub_ieee1275_release (initrd_addr, initrd_size))
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
linux_addr = 0;
initrd_addr = 0;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_unload (void)
{
grub_err_t err;
err = grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
return err;
}
static grub_err_t
grub_linux_load32 (grub_elf_t elf)
{
Elf32_Addr entry;
int found_addr = 0;
/* Linux's entry point incorrectly contains a virtual address. */
entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK;
if (entry == 0)
entry = 0x01400000;
linux_size = grub_elf32_size (elf, 0);
if (linux_size == 0)
return grub_errno;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
/* On some systems, firmware occupies the memory we're trying to use.
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
* until we find an open area. */
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
linux_addr, linux_size);
found_addr = grub_claimmap (linux_addr, linux_size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
/* Now load the segments into the area we claimed. */
auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load);
grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load)
{
if (phdr->p_type != PT_LOAD)
{
*do_load = 0;
return 0;
}
*do_load = 1;
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr;
return 0;
}
return grub_elf32_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_linux_load64 (grub_elf_t elf)
{
Elf64_Addr entry;
int found_addr = 0;
/* Linux's entry point incorrectly contains a virtual address. */
entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK;
if (entry == 0)
entry = 0x01400000;
linux_size = grub_elf64_size (elf, 0);
if (linux_size == 0)
return grub_errno;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
/* On some systems, firmware occupies the memory we're trying to use.
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
* until we find an open area. */
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
linux_addr, linux_size);
found_addr = grub_claimmap (linux_addr, linux_size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
/* Now load the segments into the area we claimed. */
auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
{
if (phdr->p_type != PT_LOAD)
{
*do_load = 0;
return 0;
}
*do_load = 1;
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr;
return 0;
}
return grub_elf64_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_elf_t elf = 0;
int i;
int size;
char *dest;
grub_dl_ref (my_mod);
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
goto out;
}
elf = grub_elf_open (argv[0]);
if (! elf)
goto out;
if (elf->ehdr.ehdr32.e_type != ET_EXEC && elf->ehdr.ehdr32.e_type != ET_DYN)
{
grub_error (GRUB_ERR_UNKNOWN_OS,
"this ELF file is not of the right type");
goto out;
}
/* Release the previously used memory. */
grub_loader_unset ();
if (grub_elf_is_elf32 (elf))
grub_linux_load32 (elf);
else
if (grub_elf_is_elf64 (elf))
grub_linux_load64 (elf);
else
{
grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
goto out;
}
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
for (i = 0; i < argc; i++)
size += grub_strlen (argv[i]) + 1;
linux_args = grub_malloc (size);
if (! linux_args)
goto out;
/* Specify the boot file. */
dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
dest = grub_stpcpy (dest, argv[0]);
for (i = 1; i < argc; i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
out:
if (elf)
grub_elf_close (elf);
if (grub_errno != GRUB_ERR_NONE)
{
grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
}
else
{
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
initrd_addr = 0;
loaded = 1;
}
return grub_errno;
}
static grub_err_t
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
grub_addr_t first_addr;
grub_addr_t addr;
int found_addr = 0;
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
goto fail;
}
if (!loaded)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
goto fail;
}
grub_file_filter_disable_compression ();
file = grub_file_open (argv[0]);
if (! file)
goto fail;
first_addr = linux_addr + linux_size;
size = grub_file_size (file);
/* Attempt to claim at a series of addresses until successful in
the same way that grub_rescue_cmd_linux does. */
for (addr = first_addr; addr < first_addr + 200 * 0x100000; addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
addr, size);
found_addr = grub_claimmap (addr, size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot claim memory");
goto fail;
}
grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
if (grub_file_read (file, (void *) addr, size) != size)
{
grub_ieee1275_release (addr, size);
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
initrd_addr = addr;
initrd_size = size;
fail:
if (file)
grub_file_close (file);
return grub_errno;
}
static grub_command_t cmd_linux, cmd_initrd;
GRUB_MOD_INIT(linux)
{
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
0, N_("Load Linux."));
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
GRUB_MOD_FINI(linux)
{
grub_unregister_command (cmd_linux);
grub_unregister_command (cmd_initrd);
}

View file

@ -0,0 +1,528 @@
/* linux.c - boot Linux */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003, 2004, 2005, 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/elf.h>
#include <grub/elfload.h>
#include <grub/loader.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/ieee1275/ieee1275.h>
#include <grub/command.h>
#include <grub/i18n.h>
static grub_dl_t my_mod;
static int loaded;
/* /virtual-memory/translations property layout */
struct grub_ieee1275_translation {
grub_uint64_t vaddr;
grub_uint64_t size;
grub_uint64_t data;
};
static struct grub_ieee1275_translation *of_trans;
static int of_num_trans;
static grub_addr_t phys_base;
static grub_addr_t grub_phys_start;
static grub_addr_t grub_phys_end;
static grub_addr_t initrd_addr;
static grub_addr_t initrd_paddr;
static grub_size_t initrd_size;
static Elf64_Addr linux_entry;
static grub_addr_t linux_addr;
static grub_addr_t linux_paddr;
static grub_size_t linux_size;
static char *linux_args;
struct linux_bootstr_info {
int len, valid;
char buf[];
};
struct linux_hdrs {
/* All HdrS versions support these fields. */
unsigned int start_insns[2];
char magic[4]; /* "HdrS" */
unsigned int linux_kernel_version; /* LINUX_VERSION_CODE */
unsigned short hdrs_version;
unsigned short root_flags;
unsigned short root_dev;
unsigned short ram_flags;
unsigned int __deprecated_ramdisk_image;
unsigned int ramdisk_size;
/* HdrS versions 0x0201 and higher only */
char *reboot_command;
/* HdrS versions 0x0202 and higher only */
struct linux_bootstr_info *bootstr_info;
/* HdrS versions 0x0301 and higher only */
unsigned long ramdisk_image;
};
static grub_err_t
grub_linux_boot (void)
{
struct linux_bootstr_info *bp;
struct linux_hdrs *hp;
grub_addr_t addr;
hp = (struct linux_hdrs *) linux_addr;
/* Any pointer we dereference in the kernel image must be relocated
to where we actually loaded the kernel. */
addr = (grub_addr_t) hp->bootstr_info;
addr += (linux_addr - linux_entry);
bp = (struct linux_bootstr_info *) addr;
/* Set the command line arguments, unless the kernel has been
built with a fixed CONFIG_CMDLINE. */
if (!bp->valid)
{
int len = grub_strlen (linux_args) + 1;
if (bp->len < len)
len = bp->len;
memcpy(bp->buf, linux_args, len);
bp->buf[len-1] = '\0';
bp->valid = 1;
}
if (initrd_addr)
{
/* The kernel expects the physical address, adjusted relative
to the lowest address advertised in "/memory"'s available
property.
The history of this is that back when the kernel only supported
specifying a 32-bit ramdisk address, this was the way to still
be able to specify the ramdisk physical address even if memory
started at some place above 4GB.
The magic 0x400000 is KERNBASE, I have no idea why SILO adds
that term into the address, but it does and thus we have to do
it too as this is what the kernel expects. */
hp->ramdisk_image = initrd_paddr - phys_base + 0x400000;
hp->ramdisk_size = initrd_size;
}
grub_dprintf ("loader", "Entry point: 0x%lx\n", linux_addr);
grub_dprintf ("loader", "Initrd at: 0x%lx, size 0x%lx\n", initrd_addr,
initrd_size);
grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
grub_dprintf ("loader", "Jumping to Linux...\n");
/* Boot the kernel. */
asm volatile ("sethi %hi(grub_ieee1275_entry_fn), %o1\n"
"ldx [%o1 + %lo(grub_ieee1275_entry_fn)], %o4\n"
"sethi %hi(grub_ieee1275_original_stack), %o1\n"
"ldx [%o1 + %lo(grub_ieee1275_original_stack)], %o6\n"
"sethi %hi(linux_addr), %o1\n"
"ldx [%o1 + %lo(linux_addr)], %o5\n"
"mov %g0, %o0\n"
"mov %g0, %o2\n"
"mov %g0, %o3\n"
"jmp %o5\n"
"mov %g0, %o1\n");
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_release_mem (void)
{
grub_free (linux_args);
linux_args = 0;
linux_addr = 0;
initrd_addr = 0;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_unload (void)
{
grub_err_t err;
err = grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
return err;
}
#define FOUR_MB (4 * 1024 * 1024)
static grub_addr_t
alloc_phys (grub_addr_t size)
{
grub_addr_t ret = (grub_addr_t) -1;
auto int NESTED_FUNC_ATTR choose (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type);
int NESTED_FUNC_ATTR choose (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type)
{
grub_addr_t end = addr + len;
if (type != 1)
return 0;
addr = ALIGN_UP (addr, FOUR_MB);
if (addr + size >= end)
return 0;
if (addr >= grub_phys_start && addr < grub_phys_end)
{
addr = ALIGN_UP (grub_phys_end, FOUR_MB);
if (addr + size >= end)
return 0;
}
if ((addr + size) >= grub_phys_start
&& (addr + size) < grub_phys_end)
{
addr = ALIGN_UP (grub_phys_end, FOUR_MB);
if (addr + size >= end)
return 0;
}
if (loaded)
{
grub_addr_t linux_end = ALIGN_UP (linux_paddr + linux_size, FOUR_MB);
if (addr >= linux_paddr && addr < linux_end)
{
addr = linux_end;
if (addr + size >= end)
return 0;
}
if ((addr + size) >= linux_paddr
&& (addr + size) < linux_end)
{
addr = linux_end;
if (addr + size >= end)
return 0;
}
}
ret = addr;
return 1;
}
grub_machine_mmap_iterate (choose);
return ret;
}
static grub_err_t
grub_linux_load64 (grub_elf_t elf)
{
grub_addr_t off, paddr, base;
int ret;
linux_entry = elf->ehdr.ehdr64.e_entry;
linux_addr = 0x40004000;
off = 0x4000;
linux_size = grub_elf64_size (elf, 0);
if (linux_size == 0)
return grub_errno;
grub_dprintf ("loader", "Attempting to claim at 0x%lx, size 0x%lx.\n",
linux_addr, linux_size);
paddr = alloc_phys (linux_size + off);
if (paddr == (grub_addr_t) -1)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't allocate physical memory");
ret = grub_ieee1275_map (paddr, linux_addr - off,
linux_size + off, IEEE1275_MAP_DEFAULT);
if (ret)
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't map physical memory");
grub_dprintf ("loader", "Loading Linux at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
linux_addr, paddr, linux_size);
linux_paddr = paddr;
base = linux_entry - off;
/* Now load the segments into the area we claimed. */
auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
{
if (phdr->p_type != PT_LOAD)
{
*do_load = 0;
return 0;
}
*do_load = 1;
/* Adjust the program load address to linux_addr. */
*addr = (phdr->p_paddr - base) + (linux_addr - off);
return 0;
}
return grub_elf64_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_elf_t elf = 0;
int i;
int size;
char *dest;
grub_dl_ref (my_mod);
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
goto out;
}
file = grub_file_open (argv[0]);
if (!file)
goto out;
elf = grub_elf_file (file);
if (! elf)
goto out;
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
{
grub_error (GRUB_ERR_UNKNOWN_OS,
"this ELF file is not of the right type");
goto out;
}
/* Release the previously used memory. */
grub_loader_unset ();
if (grub_elf_is_elf64 (elf))
grub_linux_load64 (elf);
else
{
grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
goto out;
}
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
for (i = 0; i < argc; i++)
size += grub_strlen (argv[i]) + 1;
linux_args = grub_malloc (size);
if (! linux_args)
goto out;
/* Specify the boot file. */
dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
dest = grub_stpcpy (dest, argv[0]);
for (i = 1; i < argc; i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
out:
if (elf)
grub_elf_close (elf);
else if (file)
grub_file_close (file);
if (grub_errno != GRUB_ERR_NONE)
{
grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
}
else
{
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
initrd_addr = 0;
loaded = 1;
}
return grub_errno;
}
static grub_err_t
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
grub_addr_t paddr;
grub_addr_t addr;
int ret;
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
goto fail;
}
if (!loaded)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
goto fail;
}
grub_file_filter_disable_compression ();
file = grub_file_open (argv[0]);
if (! file)
goto fail;
addr = 0x60000000;
size = grub_file_size (file);
paddr = alloc_phys (size);
if (paddr == (grub_addr_t) -1)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't allocate physical memory");
goto fail;
}
ret = grub_ieee1275_map (paddr, addr, size, IEEE1275_MAP_DEFAULT);
if (ret)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't map physical memory");
goto fail;
}
grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
addr, paddr, size);
if (grub_file_read (file, (void *) addr, size) != size)
{
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
goto fail;
}
initrd_addr = addr;
initrd_paddr = paddr;
initrd_size = size;
fail:
if (file)
grub_file_close (file);
return grub_errno;
}
static void
determine_phys_base (void)
{
auto int NESTED_FUNC_ATTR get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type);
int NESTED_FUNC_ATTR get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type)
{
if (type != 1)
return 0;
if (addr < phys_base)
phys_base = addr;
return 0;
}
phys_base = ~(grub_uint64_t) 0;
grub_machine_mmap_iterate (get_physbase);
}
static void
fetch_translations (void)
{
grub_ieee1275_phandle_t node;
grub_ssize_t actual;
int i;
if (grub_ieee1275_finddevice ("/virtual-memory", &node))
{
grub_printf ("Cannot find /virtual-memory node.\n");
return;
}
if (grub_ieee1275_get_property_length (node, "translations", &actual))
{
grub_printf ("Cannot find /virtual-memory/translations size.\n");
return;
}
of_trans = grub_malloc (actual);
if (!of_trans)
{
grub_printf ("Cannot allocate translations buffer.\n");
return;
}
if (grub_ieee1275_get_property (node, "translations", of_trans, actual, &actual))
{
grub_printf ("Cannot fetch /virtual-memory/translations property.\n");
return;
}
of_num_trans = actual / sizeof(struct grub_ieee1275_translation);
for (i = 0; i < of_num_trans; i++)
{
struct grub_ieee1275_translation *p = &of_trans[i];
if (p->vaddr == 0x2000)
{
grub_addr_t phys, tte = p->data;
phys = tte & ~(0xff00000000001fffULL);
grub_phys_start = phys;
grub_phys_end = grub_phys_start + p->size;
grub_dprintf ("loader", "Grub lives at phys_start[%lx] phys_end[%lx]\n",
(unsigned long) grub_phys_start,
(unsigned long) grub_phys_end);
break;
}
}
}
static grub_command_t cmd_linux, cmd_initrd;
GRUB_MOD_INIT(linux)
{
determine_phys_base ();
fetch_translations ();
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
0, N_("Load Linux."));
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
0, N_("Load initrd."));
my_mod = mod;
}
GRUB_MOD_FINI(linux)
{
grub_unregister_command (cmd_linux);
grub_unregister_command (cmd_initrd);
}

1465
grub-core/loader/xnu.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,172 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/normal.h>
#include <grub/dl.h>
#include <grub/file.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/xnu.h>
#include <grub/cpu/xnu.h>
#include <grub/mm.h>
#include <grub/loader.h>
static void *grub_xnu_hibernate_image;
static grub_err_t
grub_xnu_resume_unload (void)
{
/* Free loaded image */
if (grub_xnu_hibernate_image)
grub_free (grub_xnu_hibernate_image);
grub_xnu_hibernate_image = 0;
grub_xnu_unlock ();
return GRUB_ERR_NONE;
}
grub_err_t
grub_xnu_resume (char *imagename)
{
grub_file_t file;
grub_size_t total_header_size;
struct grub_xnu_hibernate_header hibhead;
void *code;
void *image;
grub_uint32_t codedest;
grub_uint32_t codesize;
grub_addr_t target_image;
grub_err_t err;
grub_file_filter_disable_compression ();
file = grub_file_open (imagename);
if (! file)
return 0;
/* Read the header. */
if (grub_file_read (file, &hibhead, sizeof (hibhead))
!=sizeof (hibhead))
{
grub_file_close (file);
return grub_error (GRUB_ERR_READ_ERROR,
"cannot read the hibernate header");
}
/* Check the header. */
if (hibhead.magic != GRUB_XNU_HIBERNATE_MAGIC)
{
grub_file_close (file);
return grub_error (GRUB_ERR_BAD_OS,
"hibernate header has incorrect magic number");
}
if (hibhead.encoffset)
{
grub_file_close (file);
return grub_error (GRUB_ERR_BAD_OS,
"encrypted images aren't supported yet");
}
codedest = hibhead.launchcode_target_page;
codedest *= GRUB_XNU_PAGESIZE;
codesize = hibhead.launchcode_numpages;
codesize *= GRUB_XNU_PAGESIZE;
/* FIXME: check that codedest..codedest+codesize is available. */
/* Calculate total size before pages to copy. */
total_header_size = hibhead.extmapsize + sizeof (hibhead);
/* Unload image if any. */
if (grub_xnu_hibernate_image)
grub_free (grub_xnu_hibernate_image);
/* Try to allocate necessary space.
FIXME: mm isn't good enough yet to handle huge allocations.
*/
grub_xnu_relocator = grub_relocator_new ();
if (!grub_xnu_relocator)
{
grub_file_close (file);
return grub_errno;
}
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, codedest,
codesize + GRUB_XNU_PAGESIZE);
if (err)
{
grub_file_close (file);
return err;
}
code = get_virtual_current_address (ch);
}
{
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (grub_xnu_relocator, &ch, 0,
(0xffffffff - hibhead.image_size) + 1,
hibhead.image_size,
GRUB_XNU_PAGESIZE,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
{
grub_file_close (file);
return err;
}
image = get_virtual_current_address (ch);
target_image = get_physical_target_address (ch);
}
/* Read code part. */
if (grub_file_seek (file, total_header_size) == (grub_off_t) -1
|| grub_file_read (file, code, codesize)
!= (grub_ssize_t) codesize)
{
grub_file_close (file);
return grub_error (GRUB_ERR_READ_ERROR, "cannot read resume image");
}
/* Read image. */
if (grub_file_seek (file, 0) == (grub_off_t) -1
|| grub_file_read (file, image, hibhead.image_size)
!= (grub_ssize_t) hibhead.image_size)
{
grub_file_close (file);
return grub_error (GRUB_ERR_READ_ERROR, "cannot read resume image");
}
grub_file_close (file);
/* Setup variables needed by asm helper. */
grub_xnu_heap_target_start = codedest;
grub_xnu_heap_size = target_image + hibhead.image_size - codedest;
grub_xnu_stack = (codedest + hibhead.stack);
grub_xnu_entry_point = (codedest + hibhead.entry_point);
grub_xnu_arg1 = target_image;
grub_dprintf ("xnu", "entry point 0x%x\n", codedest + hibhead.entry_point);
grub_dprintf ("xnu", "image at 0x%x\n",
codedest + codesize + GRUB_XNU_PAGESIZE);
/* We're ready now. */
grub_loader_set (grub_xnu_boot_resume,
grub_xnu_resume_unload, 0);
/* Prevent module from unloading. */
grub_xnu_lock ();
return GRUB_ERR_NONE;
}