Merge Mainline.
This commit is contained in:
commit
85fd555441
191 changed files with 9311 additions and 1208 deletions
|
@ -57,6 +57,7 @@ grub_chainloader_unload (void)
|
|||
grub_free (file_path);
|
||||
grub_free (cmdline);
|
||||
cmdline = 0;
|
||||
file_path = 0;
|
||||
|
||||
grub_dl_unref (my_mod);
|
||||
return GRUB_ERR_NONE;
|
||||
|
@ -68,7 +69,7 @@ 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;
|
||||
grub_efi_char16_t *exit_data = NULL;
|
||||
|
||||
b = grub_efi_system_table->boot_services;
|
||||
status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
|
||||
|
@ -95,7 +96,7 @@ grub_chainloader_boot (void)
|
|||
if (exit_data)
|
||||
efi_call_1 (b->free_pool, exit_data);
|
||||
|
||||
grub_chainloader_unload ();
|
||||
grub_loader_unset ();
|
||||
|
||||
return grub_errno;
|
||||
}
|
||||
|
@ -238,6 +239,11 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_efi_print_device_path (file_path);
|
||||
|
||||
size = grub_file_size (file);
|
||||
if (!size)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_OS, "file is empty");
|
||||
goto fail;
|
||||
}
|
||||
pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
|
||||
|
||||
status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
|
||||
|
|
|
@ -57,7 +57,6 @@ GRUB_MOD_LICENSE ("GPLv3+");
|
|||
#endif
|
||||
|
||||
#define GRUB_LINUX_CL_OFFSET 0x1000
|
||||
#define GRUB_LINUX_CL_END_OFFSET 0x2000
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
|
||||
|
@ -74,6 +73,7 @@ static grub_uint32_t prot_mode_pages;
|
|||
static grub_uint32_t initrd_pages;
|
||||
static struct grub_relocator *relocator = NULL;
|
||||
static void *efi_mmap_buf;
|
||||
static grub_size_t maximal_cmdline_size;
|
||||
#ifdef GRUB_MACHINE_EFI
|
||||
static grub_efi_uintn_t efi_mmap_size;
|
||||
#else
|
||||
|
@ -189,7 +189,7 @@ allocate_pages (grub_size_t prot_size)
|
|||
grub_err_t err;
|
||||
|
||||
/* Make sure that each size is aligned to a page boundary. */
|
||||
real_size = GRUB_LINUX_CL_END_OFFSET;
|
||||
real_size = GRUB_LINUX_CL_OFFSET + maximal_cmdline_size;
|
||||
prot_size = page_align (prot_size);
|
||||
mmap_size = find_mmap_size ();
|
||||
|
||||
|
@ -367,6 +367,7 @@ grub_linux_setup_video (struct linux_kernel_params *params)
|
|||
|
||||
/* FIXME: check if better id is available. */
|
||||
case GRUB_VIDEO_DRIVER_SM712:
|
||||
case GRUB_VIDEO_DRIVER_SIS315PRO:
|
||||
case GRUB_VIDEO_DRIVER_VGA:
|
||||
case GRUB_VIDEO_DRIVER_CIRRUS:
|
||||
case GRUB_VIDEO_DRIVER_BOCHS:
|
||||
|
@ -662,6 +663,14 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
goto fail;
|
||||
}
|
||||
|
||||
if (grub_le_to_cpu16 (lh.version) >= 0x0206)
|
||||
maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
|
||||
else
|
||||
maximal_cmdline_size = 256;
|
||||
|
||||
if (maximal_cmdline_size < 128)
|
||||
maximal_cmdline_size = 128;
|
||||
|
||||
setup_sects = lh.setup_sects;
|
||||
|
||||
/* If SETUP_SECTS is not set, set it to the default (4). */
|
||||
|
@ -675,7 +684,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
goto fail;
|
||||
|
||||
params = (struct linux_kernel_params *) real_mode_mem;
|
||||
grub_memset (params, 0, GRUB_LINUX_CL_END_OFFSET);
|
||||
grub_memset (params, 0, GRUB_LINUX_CL_OFFSET + maximal_cmdline_size);
|
||||
grub_memcpy (¶ms->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
|
||||
|
||||
params->ps_mouse = params->padding10 = 0;
|
||||
|
@ -870,7 +879,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_create_loader_cmdline (argc, argv,
|
||||
(char *)real_mode_mem + GRUB_LINUX_CL_OFFSET
|
||||
+ sizeof (LINUX_IMAGE) - 1,
|
||||
GRUB_LINUX_CL_END_OFFSET - GRUB_LINUX_CL_OFFSET
|
||||
maximal_cmdline_size
|
||||
- (sizeof (LINUX_IMAGE) - 1));
|
||||
|
||||
len = prot_size;
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
#include <grub/i18n.h>
|
||||
#include <grub/video.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/fat.h>
|
||||
#include <grub/ntfs.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
|
@ -44,6 +46,12 @@ static grub_dl_t my_mod;
|
|||
static int boot_drive;
|
||||
static void *boot_part_addr;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GRUB_CHAINLOADER_FORCE = 0x1,
|
||||
GRUB_CHAINLOADER_BPB = 0x2,
|
||||
} grub_chainloader_flags_t;
|
||||
|
||||
static grub_err_t
|
||||
grub_chainloader_boot (void)
|
||||
{
|
||||
|
@ -61,6 +69,63 @@ grub_chainloader_unload (void)
|
|||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
void
|
||||
grub_chainloader_patch_bpb (void *bs, grub_device_t dev, grub_uint8_t dl)
|
||||
{
|
||||
grub_uint32_t part_start = 0;
|
||||
if (dev && dev->disk)
|
||||
part_start = grub_partition_get_start (dev->disk->partition);
|
||||
if (grub_memcmp ((char *) &((struct grub_ntfs_bpb *) bs)->oem_name,
|
||||
"NTFS", 4) == 0)
|
||||
{
|
||||
struct grub_ntfs_bpb *bpb = (struct grub_ntfs_bpb *) bs;
|
||||
bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
|
||||
bpb->bios_drive = dl;
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
struct grub_fat_bpb *bpb = (struct grub_fat_bpb *) bs;
|
||||
if (grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT12", 5)
|
||||
&& grub_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT16", 5)
|
||||
&& grub_strncmp((const char *) bpb->version_specific.fat32.fstype, "FAT32", 5))
|
||||
break;
|
||||
|
||||
if (grub_le_to_cpu16 (bpb->bytes_per_sector) < 512
|
||||
|| (grub_le_to_cpu16 (bpb->bytes_per_sector)
|
||||
& (grub_le_to_cpu16 (bpb->bytes_per_sector) - 1)))
|
||||
break;
|
||||
|
||||
if (bpb->sectors_per_cluster == 0
|
||||
|| (bpb->sectors_per_cluster & (bpb->sectors_per_cluster - 1)))
|
||||
break;
|
||||
|
||||
if (bpb->num_reserved_sectors == 0)
|
||||
break;
|
||||
if (bpb->num_total_sectors_16 == 0 || bpb->num_total_sectors_32 == 0)
|
||||
break;
|
||||
|
||||
if (bpb->num_fats == 0)
|
||||
break;
|
||||
|
||||
if (bpb->sectors_per_fat_16)
|
||||
{
|
||||
bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
|
||||
bpb->version_specific.fat12_or_fat16.num_ph_drive = dl;
|
||||
return;
|
||||
}
|
||||
if (bpb->version_specific.fat32.sectors_per_fat_32)
|
||||
{
|
||||
bpb->num_hidden_sectors = grub_cpu_to_le32 (part_start);
|
||||
bpb->version_specific.fat32.num_ph_drive = dl;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (0);
|
||||
}
|
||||
|
||||
static void
|
||||
grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
|
||||
{
|
||||
|
@ -121,6 +186,9 @@ grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)
|
|||
}
|
||||
}
|
||||
|
||||
if (flags & GRUB_CHAINLOADER_BPB)
|
||||
grub_chainloader_patch_bpb ((void *) 0x7C00, dev, drive);
|
||||
|
||||
if (dev)
|
||||
grub_device_close (dev);
|
||||
|
||||
|
@ -147,11 +215,23 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
|
|||
{
|
||||
grub_chainloader_flags_t flags = 0;
|
||||
|
||||
if (argc > 0 && grub_strcmp (argv[0], "--force") == 0)
|
||||
while (argc > 0)
|
||||
{
|
||||
flags |= GRUB_CHAINLOADER_FORCE;
|
||||
argc--;
|
||||
argv++;
|
||||
if (grub_strcmp (argv[0], "--force") == 0)
|
||||
{
|
||||
flags |= GRUB_CHAINLOADER_FORCE;
|
||||
argc--;
|
||||
argv++;
|
||||
continue;
|
||||
}
|
||||
if (grub_strcmp (argv[0], "--bpb") == 0)
|
||||
{
|
||||
flags |= GRUB_CHAINLOADER_BPB;
|
||||
argc--;
|
||||
argv++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (argc == 0)
|
||||
|
@ -167,7 +247,8 @@ static grub_command_t cmd;
|
|||
GRUB_MOD_INIT(chainloader)
|
||||
{
|
||||
cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
|
||||
0, N_("Load another boot loader."));
|
||||
"[--force|--bpb] FILE",
|
||||
N_("Load another boot loader."));
|
||||
my_mod = mod;
|
||||
}
|
||||
|
||||
|
|
136
grub-core/loader/i386/pc/freedos.c
Normal file
136
grub-core/loader/i386/pc/freedos.c
Normal file
|
@ -0,0 +1,136 @@
|
|||
/* 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 ebx = 0xffffffff;
|
||||
|
||||
#define GRUB_FREEDOS_SEGMENT 0x60
|
||||
#define GRUB_FREEDOS_STACK_SEGMENT 0x1fe0
|
||||
#define GRUB_FREEDOS_STACK_POINTER 0x8000
|
||||
|
||||
static grub_err_t
|
||||
grub_freedos_boot (void)
|
||||
{
|
||||
struct grub_relocator16_state state = {
|
||||
.cs = GRUB_FREEDOS_SEGMENT,
|
||||
.ip = 0,
|
||||
.ds = 0,
|
||||
.es = 0,
|
||||
.fs = 0,
|
||||
.gs = 0,
|
||||
.ss = GRUB_FREEDOS_STACK_SEGMENT,
|
||||
.sp = GRUB_FREEDOS_STACK_POINTER,
|
||||
.ebx = ebx,
|
||||
.edx = 0
|
||||
};
|
||||
grub_video_set_mode ("text", 0, 0);
|
||||
|
||||
return grub_relocator16_boot (rel, state);
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_freedos_unload (void)
|
||||
{
|
||||
grub_relocator_unload (rel);
|
||||
rel = NULL;
|
||||
grub_dl_unref (my_mod);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_freedos (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
grub_err_t err;
|
||||
void *kernelsys;
|
||||
grub_size_t kernelsyssize;
|
||||
|
||||
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;
|
||||
|
||||
ebx = grub_get_root_biosnumber ();
|
||||
|
||||
kernelsyssize = grub_file_size (file);
|
||||
{
|
||||
grub_relocator_chunk_t ch;
|
||||
err = grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_FREEDOS_SEGMENT << 4,
|
||||
kernelsyssize);
|
||||
if (err)
|
||||
goto fail;
|
||||
kernelsys = get_virtual_current_address (ch);
|
||||
}
|
||||
|
||||
if (grub_file_read (file, kernelsys, kernelsyssize)
|
||||
!= (grub_ssize_t) kernelsyssize)
|
||||
goto fail;
|
||||
|
||||
grub_loader_set (grub_freedos_boot, grub_freedos_unload, 1);
|
||||
return GRUB_ERR_NONE;
|
||||
|
||||
fail:
|
||||
|
||||
if (file)
|
||||
grub_file_close (file);
|
||||
|
||||
grub_freedos_unload ();
|
||||
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
static grub_command_t cmd;
|
||||
|
||||
GRUB_MOD_INIT(freedos)
|
||||
{
|
||||
cmd = grub_register_command ("freedos", grub_cmd_freedos,
|
||||
0, N_("Load FreeDOS kernel.sys."));
|
||||
my_mod = mod;
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(freedos)
|
||||
{
|
||||
grub_unregister_command (cmd);
|
||||
}
|
|
@ -39,7 +39,6 @@
|
|||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
#define GRUB_LINUX_CL_OFFSET 0x9000
|
||||
#define GRUB_LINUX_CL_END_OFFSET 0x90FF
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
|
||||
|
@ -49,6 +48,7 @@ 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_size_t maximal_cmdline_size;
|
||||
|
||||
static grub_err_t
|
||||
grub_linux16_boot (void)
|
||||
|
@ -128,15 +128,20 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
setup_sects = lh.setup_sects;
|
||||
linux_mem_size = 0;
|
||||
|
||||
maximal_cmdline_size = 256;
|
||||
|
||||
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;
|
||||
|
||||
if (grub_le_to_cpu16 (lh.version) >= 0x0206)
|
||||
maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
|
||||
|
||||
/* 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;
|
||||
- (GRUB_LINUX_CL_OFFSET + maximal_cmdline_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;
|
||||
|
@ -153,7 +158,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
{
|
||||
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);
|
||||
lh.setup_move_size = grub_cpu_to_le16 (GRUB_LINUX_CL_OFFSET
|
||||
+ maximal_cmdline_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -185,12 +191,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
goto fail;
|
||||
}
|
||||
|
||||
if (grub_linux_real_target + GRUB_LINUX_SETUP_MOVE_SIZE
|
||||
if (grub_linux_real_target + GRUB_LINUX_CL_OFFSET + maximal_cmdline_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,
|
||||
grub_linux_real_target + GRUB_LINUX_CL_OFFSET
|
||||
+ maximal_cmdline_size,
|
||||
(int) grub_mmap_get_lower ());
|
||||
goto fail;
|
||||
}
|
||||
|
@ -263,7 +270,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_relocator_chunk_t ch;
|
||||
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
|
||||
grub_linux_real_target,
|
||||
GRUB_LINUX_SETUP_MOVE_SIZE);
|
||||
GRUB_LINUX_CL_OFFSET
|
||||
+ maximal_cmdline_size);
|
||||
if (err)
|
||||
return err;
|
||||
grub_linux_real_chunk = get_virtual_current_address (ch);
|
||||
|
@ -294,7 +302,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_create_loader_cmdline (argc, argv,
|
||||
(char *)grub_linux_real_chunk
|
||||
+ GRUB_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
|
||||
GRUB_LINUX_CL_END_OFFSET - GRUB_LINUX_CL_OFFSET
|
||||
maximal_cmdline_size
|
||||
- (sizeof (LINUX_IMAGE) - 1));
|
||||
|
||||
if (grub_linux_is_bzimage)
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <grub/video.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/cpu/relocator.h>
|
||||
#include <grub/machine/chainloader.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
|
@ -112,6 +113,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_device_close (dev);
|
||||
goto fail;
|
||||
}
|
||||
grub_chainloader_patch_bpb (bs, dev, edx);
|
||||
}
|
||||
|
||||
if (dev)
|
||||
|
|
795
grub-core/loader/ia64/efi/linux.c
Normal file
795
grub-core/loader/ia64/efi/linux.c
Normal file
|
@ -0,0 +1,795 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2008,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/disk.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/command.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/cache.h>
|
||||
#include <grub/kernel.h>
|
||||
#include <grub/efi/api.h>
|
||||
#include <grub/efi/efi.h>
|
||||
#include <grub/elf.h>
|
||||
|
||||
#define ALIGN_MIN (256*1024*1024)
|
||||
|
||||
#define GRUB_ELF_SEARCH 1024
|
||||
|
||||
#define BOOT_PARAM_SIZE 16384
|
||||
|
||||
struct ia64_boot_param
|
||||
{
|
||||
grub_uint64_t command_line; /* physical address of command line. */
|
||||
grub_uint64_t efi_systab; /* physical address of EFI system table */
|
||||
grub_uint64_t efi_memmap; /* physical address of EFI memory map */
|
||||
grub_uint64_t efi_memmap_size; /* size of EFI memory map */
|
||||
grub_uint64_t efi_memdesc_size; /* size of an EFI memory map descriptor */
|
||||
grub_uint32_t efi_memdesc_version; /* memory descriptor version */
|
||||
struct
|
||||
{
|
||||
grub_uint16_t num_cols; /* number of columns on console output dev */
|
||||
grub_uint16_t num_rows; /* number of rows on console output device */
|
||||
grub_uint16_t orig_x; /* cursor's x position */
|
||||
grub_uint16_t orig_y; /* cursor's y position */
|
||||
} console_info;
|
||||
grub_uint64_t fpswa; /* physical address of the fpswa interface */
|
||||
grub_uint64_t initrd_start;
|
||||
grub_uint64_t initrd_size;
|
||||
grub_uint64_t domain_start; /* boot domain address. */
|
||||
grub_uint64_t domain_size; /* how big is the boot domain */
|
||||
grub_uint64_t payloads_chain;
|
||||
grub_uint64_t payloads_nbr;
|
||||
};
|
||||
|
||||
struct ia64_boot_payload
|
||||
{
|
||||
grub_uint64_t start;
|
||||
grub_uint64_t length;
|
||||
|
||||
/* Payload command line */
|
||||
grub_uint64_t cmdline;
|
||||
|
||||
grub_uint64_t next;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
grub_uint32_t revision;
|
||||
grub_uint32_t reserved;
|
||||
void *fpswa;
|
||||
} fpswa_interface_t;
|
||||
static fpswa_interface_t *fpswa;
|
||||
|
||||
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
|
||||
((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
|
||||
static int loaded;
|
||||
|
||||
/* Kernel base and size. */
|
||||
static void *kernel_mem;
|
||||
static grub_efi_uintn_t kernel_pages;
|
||||
static grub_uint64_t entry;
|
||||
|
||||
/* Initrd base and size. */
|
||||
static void *initrd_mem;
|
||||
static grub_efi_uintn_t initrd_pages;
|
||||
static grub_efi_uintn_t initrd_size;
|
||||
|
||||
static struct ia64_boot_param *boot_param;
|
||||
static grub_efi_uintn_t boot_param_pages;
|
||||
static struct ia64_boot_payload *last_payload = NULL;
|
||||
|
||||
/* Can linux kernel be relocated ? */
|
||||
#define RELOCATE_OFF 0 /* No. */
|
||||
#define RELOCATE_ON 1 /* Yes. */
|
||||
#define RELOCATE_FORCE 2 /* Always - used to debug. */
|
||||
static int relocate = RELOCATE_OFF;
|
||||
|
||||
static inline grub_size_t
|
||||
page_align (grub_size_t size)
|
||||
{
|
||||
return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
|
||||
}
|
||||
|
||||
static void
|
||||
query_fpswa (void)
|
||||
{
|
||||
grub_efi_handle_t fpswa_image;
|
||||
grub_efi_boot_services_t *bs;
|
||||
grub_efi_status_t status;
|
||||
grub_efi_uintn_t size;
|
||||
static const grub_efi_guid_t fpswa_protocol =
|
||||
{ 0xc41b6531, 0x97b9, 0x11d3,
|
||||
{0x9a, 0x29, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d} };
|
||||
|
||||
if (fpswa != NULL)
|
||||
return;
|
||||
|
||||
size = sizeof(grub_efi_handle_t);
|
||||
|
||||
bs = grub_efi_system_table->boot_services;
|
||||
status = bs->locate_handle (GRUB_EFI_BY_PROTOCOL,
|
||||
(void *)&fpswa_protocol,
|
||||
NULL, &size, &fpswa_image);
|
||||
if (status != GRUB_EFI_SUCCESS)
|
||||
{
|
||||
grub_printf("Could not locate FPSWA driver\n");
|
||||
return;
|
||||
}
|
||||
status = bs->handle_protocol (fpswa_image,
|
||||
(void *)&fpswa_protocol, (void *)&fpswa);
|
||||
if (status != GRUB_EFI_SUCCESS)
|
||||
{
|
||||
grub_printf ("Fpswa protocol not able find the interface\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the optimal number of pages for the memory map. Is it better to
|
||||
move this code to efi/mm.c? */
|
||||
static grub_efi_uintn_t
|
||||
find_mmap_size (void)
|
||||
{
|
||||
static grub_efi_uintn_t mmap_size = 0;
|
||||
|
||||
if (mmap_size != 0)
|
||||
return mmap_size;
|
||||
|
||||
mmap_size = (1 << 12);
|
||||
while (1)
|
||||
{
|
||||
int ret;
|
||||
grub_efi_memory_descriptor_t *mmap;
|
||||
grub_efi_uintn_t desc_size;
|
||||
|
||||
mmap = grub_malloc (mmap_size);
|
||||
if (! mmap)
|
||||
return 0;
|
||||
|
||||
ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0);
|
||||
grub_free (mmap);
|
||||
|
||||
if (ret < 0)
|
||||
grub_fatal ("cannot get memory map");
|
||||
else if (ret > 0)
|
||||
break;
|
||||
|
||||
mmap_size += (1 << 12);
|
||||
}
|
||||
|
||||
/* Increase the size a bit for safety, because GRUB allocates more on
|
||||
later, and EFI itself may allocate more. */
|
||||
mmap_size += (1 << 12);
|
||||
|
||||
return page_align (mmap_size);
|
||||
}
|
||||
|
||||
static void
|
||||
free_pages (void)
|
||||
{
|
||||
if (kernel_mem)
|
||||
{
|
||||
grub_efi_free_pages ((grub_addr_t) kernel_mem, kernel_pages);
|
||||
kernel_mem = 0;
|
||||
}
|
||||
|
||||
if (initrd_mem)
|
||||
{
|
||||
grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
|
||||
initrd_mem = 0;
|
||||
}
|
||||
|
||||
if (boot_param)
|
||||
{
|
||||
struct ia64_boot_payload *payload;
|
||||
struct ia64_boot_payload *next_payload;
|
||||
|
||||
/* Free payloads. */
|
||||
payload = (struct ia64_boot_payload *)boot_param->payloads_chain;
|
||||
while (payload != 0)
|
||||
{
|
||||
next_payload = (struct ia64_boot_payload *)payload->next;
|
||||
|
||||
grub_efi_free_pages
|
||||
(payload->start, page_align (payload->length) >> 12);
|
||||
grub_efi_free_pages ((grub_efi_physical_address_t)payload, 1);
|
||||
|
||||
payload = next_payload;
|
||||
}
|
||||
|
||||
/* Free bootparam. */
|
||||
grub_efi_free_pages ((grub_efi_physical_address_t)boot_param,
|
||||
boot_param_pages);
|
||||
boot_param = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void *
|
||||
allocate_pages (grub_uint64_t align, grub_uint64_t size_pages,
|
||||
grub_uint64_t nobase)
|
||||
{
|
||||
grub_uint64_t size;
|
||||
grub_efi_uintn_t desc_size;
|
||||
grub_efi_memory_descriptor_t *mmap, *mmap_end;
|
||||
grub_efi_uintn_t mmap_size, tmp_mmap_size;
|
||||
grub_efi_memory_descriptor_t *desc;
|
||||
void *mem = NULL;
|
||||
|
||||
size = size_pages << 12;
|
||||
|
||||
mmap_size = find_mmap_size ();
|
||||
|
||||
/* Read the memory map temporarily, to find free space. */
|
||||
mmap = grub_malloc (mmap_size);
|
||||
if (! mmap)
|
||||
return 0;
|
||||
|
||||
tmp_mmap_size = mmap_size;
|
||||
if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0)
|
||||
grub_fatal ("cannot get memory map");
|
||||
|
||||
mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size);
|
||||
|
||||
/* First, find free pages for the real mode code
|
||||
and the memory map buffer. */
|
||||
for (desc = mmap;
|
||||
desc < mmap_end;
|
||||
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
|
||||
{
|
||||
grub_uint64_t start, end;
|
||||
grub_uint64_t aligned_start;
|
||||
|
||||
if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
|
||||
continue;
|
||||
|
||||
start = desc->physical_start;
|
||||
end = start + (desc->num_pages << 12);
|
||||
/* Align is a power of 2. */
|
||||
aligned_start = (start + align - 1) & ~(align - 1);
|
||||
if (aligned_start + size > end)
|
||||
continue;
|
||||
if (aligned_start == nobase)
|
||||
aligned_start += align;
|
||||
if (aligned_start + size > end)
|
||||
continue;
|
||||
mem = grub_efi_allocate_pages (aligned_start, size_pages);
|
||||
if (! mem)
|
||||
grub_fatal ("cannot allocate pages");
|
||||
break;
|
||||
}
|
||||
|
||||
if (! mem)
|
||||
{
|
||||
grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
grub_free (mmap);
|
||||
return mem;
|
||||
|
||||
fail:
|
||||
grub_free (mmap);
|
||||
free_pages ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
set_boot_param_console (void)
|
||||
{
|
||||
grub_efi_simple_text_output_interface_t *conout;
|
||||
grub_efi_uintn_t cols, rows;
|
||||
|
||||
conout = grub_efi_system_table->con_out;
|
||||
if (conout->query_mode (conout, conout->mode->mode, &cols, &rows)
|
||||
!= GRUB_EFI_SUCCESS)
|
||||
return;
|
||||
|
||||
grub_dprintf("linux",
|
||||
"Console info: cols=%lu rows=%lu x=%u y=%u\n",
|
||||
cols, rows,
|
||||
conout->mode->cursor_column, conout->mode->cursor_row);
|
||||
|
||||
boot_param->console_info.num_cols = cols;
|
||||
boot_param->console_info.num_rows = rows;
|
||||
boot_param->console_info.orig_x = conout->mode->cursor_column;
|
||||
boot_param->console_info.orig_y = conout->mode->cursor_row;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_linux_boot (void)
|
||||
{
|
||||
grub_efi_uintn_t mmap_size;
|
||||
grub_efi_uintn_t map_key;
|
||||
grub_efi_uintn_t desc_size;
|
||||
grub_efi_uint32_t desc_version;
|
||||
grub_efi_memory_descriptor_t *mmap_buf;
|
||||
grub_err_t err;
|
||||
|
||||
/* FPSWA. */
|
||||
query_fpswa ();
|
||||
boot_param->fpswa = (grub_uint64_t)fpswa;
|
||||
|
||||
/* Initrd. */
|
||||
boot_param->initrd_start = (grub_uint64_t)initrd_mem;
|
||||
boot_param->initrd_size = (grub_uint64_t)initrd_size;
|
||||
|
||||
set_boot_param_console ();
|
||||
|
||||
grub_printf ("Jump to %016lx\n", entry);
|
||||
|
||||
grub_machine_fini ();
|
||||
|
||||
/* MDT.
|
||||
Must be done after grub_machine_fini because map_key is used by
|
||||
exit_boot_services. */
|
||||
mmap_size = find_mmap_size ();
|
||||
mmap_buf = grub_efi_allocate_pages (0, page_align (mmap_size) >> 12);
|
||||
if (! mmap_buf)
|
||||
grub_fatal ("cannot allocate memory map");
|
||||
err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, &map_key,
|
||||
&desc_size, &desc_version);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
boot_param->efi_memmap = (grub_uint64_t)mmap_buf;
|
||||
boot_param->efi_memmap_size = mmap_size;
|
||||
boot_param->efi_memdesc_size = desc_size;
|
||||
boot_param->efi_memdesc_version = desc_version;
|
||||
|
||||
/* See you next boot. */
|
||||
asm volatile ("mov r28=%1; br.sptk.few %0" :: "b"(entry),"r"(boot_param));
|
||||
|
||||
/* Never reach here. */
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_linux_unload (void)
|
||||
{
|
||||
free_pages ();
|
||||
grub_dl_unref (my_mod);
|
||||
loaded = 0;
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_load_elf64 (grub_file_t file, void *buffer)
|
||||
{
|
||||
Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
|
||||
Elf64_Phdr *phdr;
|
||||
int i;
|
||||
grub_uint64_t low_addr;
|
||||
grub_uint64_t high_addr;
|
||||
grub_uint64_t align;
|
||||
grub_uint64_t reloc_offset;
|
||||
|
||||
if (ehdr->e_ident[EI_CLASS] != ELFCLASS64)
|
||||
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 != EM_IA_64)
|
||||
return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
|
||||
|
||||
if (ehdr->e_type != ET_EXEC)
|
||||
return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
|
||||
|
||||
/* FIXME: Should we support program headers at strange locations? */
|
||||
if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_ELF_SEARCH)
|
||||
return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
|
||||
|
||||
entry = ehdr->e_entry;
|
||||
|
||||
/* Compute low, high and align addresses. */
|
||||
low_addr = ~0UL;
|
||||
high_addr = 0;
|
||||
align = 0;
|
||||
for (i = 0; i < ehdr->e_phnum; i++)
|
||||
{
|
||||
phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
|
||||
+ i * ehdr->e_phentsize);
|
||||
if (phdr->p_type == PT_LOAD)
|
||||
{
|
||||
if (phdr->p_paddr < low_addr)
|
||||
low_addr = phdr->p_paddr;
|
||||
if (phdr->p_paddr + phdr->p_memsz > high_addr)
|
||||
high_addr = phdr->p_paddr + phdr->p_memsz;
|
||||
if (phdr->p_align > align)
|
||||
align = phdr->p_align;
|
||||
}
|
||||
}
|
||||
|
||||
if (align < ALIGN_MIN)
|
||||
align = ALIGN_MIN;
|
||||
|
||||
if (high_addr == 0)
|
||||
return grub_error (GRUB_ERR_BAD_OS, "no program entries");
|
||||
|
||||
kernel_pages = page_align (high_addr - low_addr) >> 12;
|
||||
|
||||
if (relocate != RELOCATE_FORCE)
|
||||
{
|
||||
kernel_mem = grub_efi_allocate_pages (low_addr, kernel_pages);
|
||||
reloc_offset = 0;
|
||||
}
|
||||
/* Try to relocate. */
|
||||
if (! kernel_mem && relocate != RELOCATE_OFF)
|
||||
{
|
||||
kernel_mem = allocate_pages (align, kernel_pages, low_addr);
|
||||
if (kernel_mem)
|
||||
{
|
||||
reloc_offset = (grub_uint64_t)kernel_mem - low_addr;
|
||||
grub_printf (" Relocated at %p (offset=%016lx)\n",
|
||||
kernel_mem, reloc_offset);
|
||||
entry += reloc_offset;
|
||||
}
|
||||
}
|
||||
if (! kernel_mem)
|
||||
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
||||
"cannot allocate memory for OS");
|
||||
|
||||
/* Load every loadable segment in memory. */
|
||||
for (i = 0; i < ehdr->e_phnum; i++)
|
||||
{
|
||||
phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
|
||||
+ i * ehdr->e_phentsize);
|
||||
if (phdr->p_type == PT_LOAD)
|
||||
{
|
||||
grub_printf (" [paddr=%lx load=%lx memsz=%08lx "
|
||||
"off=%lx flags=%x]\n",
|
||||
phdr->p_paddr, phdr->p_paddr + reloc_offset,
|
||||
phdr->p_memsz, phdr->p_offset, phdr->p_flags);
|
||||
|
||||
if (grub_file_seek (file, phdr->p_offset) == (grub_off_t)-1)
|
||||
return grub_error (GRUB_ERR_BAD_OS,
|
||||
"invalid offset in program header");
|
||||
|
||||
if (grub_file_read (file, (void *)(phdr->p_paddr + reloc_offset),
|
||||
phdr->p_filesz)
|
||||
!= (grub_ssize_t) phdr->p_filesz)
|
||||
return grub_error (GRUB_ERR_BAD_OS,
|
||||
"couldn't read segment from file");
|
||||
|
||||
if (phdr->p_filesz < phdr->p_memsz)
|
||||
grub_memset
|
||||
((char *)(phdr->p_paddr + reloc_offset + phdr->p_filesz),
|
||||
0, phdr->p_memsz - phdr->p_filesz);
|
||||
|
||||
/* Sync caches if necessary. */
|
||||
if (phdr->p_flags & PF_X)
|
||||
grub_arch_sync_caches
|
||||
((void *)(phdr->p_paddr + reloc_offset), phdr->p_memsz);
|
||||
}
|
||||
}
|
||||
loaded = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
char buffer[GRUB_ELF_SEARCH];
|
||||
char *cmdline, *p;
|
||||
grub_ssize_t len;
|
||||
int i;
|
||||
|
||||
grub_dl_ref (my_mod);
|
||||
|
||||
grub_loader_unset ();
|
||||
|
||||
if (argc == 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
file = grub_file_open (argv[0]);
|
||||
if (! file)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
len = grub_file_read (file, buffer, sizeof (buffer));
|
||||
if (len < (grub_ssize_t)sizeof (Elf64_Ehdr))
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_OS, "File too small");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
grub_printf ("Loading linux: %s\n", argv[0]);
|
||||
|
||||
if (grub_load_elf64 (file, buffer))
|
||||
goto fail;
|
||||
|
||||
len = sizeof("BOOT_IMAGE=") + 8;
|
||||
for (i = 0; i < argc; i++)
|
||||
len += grub_strlen (argv[i]) + 1;
|
||||
len += sizeof (struct ia64_boot_param) + 256; /* Room for extensions. */
|
||||
boot_param_pages = page_align (len) >> 12;
|
||||
boot_param = grub_efi_allocate_pages (0, boot_param_pages);
|
||||
if (boot_param == 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
||||
"cannot allocate memory for bootparams");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
grub_memset (boot_param, 0, len);
|
||||
cmdline = ((char *)(boot_param + 1)) + 256;
|
||||
|
||||
/* Build cmdline. */
|
||||
p = grub_stpcpy (cmdline, "BOOT_IMAGE");
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
*p++ = ' ';
|
||||
p = grub_stpcpy (p, argv[i]);
|
||||
}
|
||||
cmdline[10] = '=';
|
||||
|
||||
boot_param->command_line = (grub_uint64_t) cmdline;
|
||||
boot_param->efi_systab = (grub_uint64_t) grub_efi_system_table;
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
||||
grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
|
||||
|
||||
fail:
|
||||
if (file)
|
||||
grub_file_close (file);
|
||||
|
||||
if (grub_errno != GRUB_ERR_NONE)
|
||||
{
|
||||
grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
|
||||
boot_param_pages);
|
||||
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 filename specified");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (! loaded)
|
||||
{
|
||||
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;
|
||||
|
||||
grub_printf ("Loading initrd: %s\n",argv[0]);
|
||||
|
||||
initrd_size = grub_file_size (file);
|
||||
initrd_pages = (page_align (initrd_size) >> 12);
|
||||
initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
|
||||
if (! initrd_mem)
|
||||
grub_fatal ("cannot allocate pages");
|
||||
|
||||
grub_printf (" [addr=0x%lx, size=0x%lx]\n",
|
||||
(grub_uint64_t)initrd_mem, initrd_size);
|
||||
|
||||
if (grub_file_read (file, initrd_mem, initrd_size)
|
||||
!= (grub_ssize_t)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_err_t
|
||||
grub_cmd_payload (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char *argv[])
|
||||
{
|
||||
grub_file_t file = 0;
|
||||
grub_ssize_t size, len = 0;
|
||||
char *base = 0, *cmdline = 0, *p;
|
||||
struct ia64_boot_payload *payload = NULL;
|
||||
int i;
|
||||
|
||||
if (argc == 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "No module specified");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!boot_param)
|
||||
{
|
||||
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;
|
||||
|
||||
size = grub_file_size (file);
|
||||
base = grub_efi_allocate_pages (0, page_align (size) >> 12);
|
||||
if (! base)
|
||||
goto fail;
|
||||
|
||||
grub_printf ("Payload %s [addr=%lx + %lx]\n",
|
||||
argv[0], (grub_uint64_t)base, size);
|
||||
|
||||
if (grub_file_read (file, base, size) != size)
|
||||
{
|
||||
grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
len = sizeof (struct ia64_boot_payload);
|
||||
for (i = 0; i < argc; i++)
|
||||
len += grub_strlen (argv[i]) + 1;
|
||||
|
||||
if (len > 4096)
|
||||
{
|
||||
grub_error (GRUB_ERR_OUT_OF_RANGE, "payload command line too long");
|
||||
goto fail;
|
||||
}
|
||||
payload = grub_efi_allocate_pages (0, 1);
|
||||
if (! payload)
|
||||
goto fail;
|
||||
|
||||
p = (char *)(payload + 1);
|
||||
|
||||
payload->start = (grub_uint64_t)base;
|
||||
payload->length = size;
|
||||
payload->cmdline = (grub_uint64_t)p;
|
||||
payload->next = 0;
|
||||
|
||||
if (last_payload)
|
||||
last_payload->next = (grub_uint64_t)payload;
|
||||
else
|
||||
{
|
||||
last_payload = payload;
|
||||
boot_param->payloads_chain = (grub_uint64_t)payload;
|
||||
}
|
||||
boot_param->payloads_nbr++;
|
||||
|
||||
/* Copy command line. */
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
p = grub_stpcpy (p, argv[i]);
|
||||
*(p++) = ' ';
|
||||
}
|
||||
|
||||
/* Remove the space after the last word. */
|
||||
*(--p) = '\0';
|
||||
|
||||
|
||||
fail:
|
||||
if (file)
|
||||
grub_file_close (file);
|
||||
|
||||
if (grub_errno != GRUB_ERR_NONE)
|
||||
{
|
||||
grub_free (base);
|
||||
grub_free (cmdline);
|
||||
}
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_relocate (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char *argv[])
|
||||
{
|
||||
static const char * const vals[] = { "off", "on", "force"};
|
||||
unsigned int i;
|
||||
|
||||
if (argc == 0)
|
||||
{
|
||||
grub_printf ("relocate is %s\n", vals[relocate]);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
else if (argc == 1)
|
||||
{
|
||||
if (kernel_mem != NULL)
|
||||
grub_printf ("Warning: kernel already loaded!\n");
|
||||
for (i = 0; i < sizeof (vals)/sizeof(vals[0]); i++)
|
||||
if (grub_strcmp (argv[0], vals[i]) == 0)
|
||||
{
|
||||
relocate = i;
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown relocate value");
|
||||
}
|
||||
else
|
||||
{
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "accept 0 or 1 argument");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_fpswa (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char *argv[] __attribute__((unused)))
|
||||
{
|
||||
if (argc != 0)
|
||||
{
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Arguments not expected");
|
||||
}
|
||||
query_fpswa ();
|
||||
if (fpswa == NULL)
|
||||
grub_printf ("No FPSWA loaded\n");
|
||||
else
|
||||
grub_printf ("FPSWA revision: %x\n", fpswa->revision);
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_command_t cmd_linux, cmd_initrd, cmd_payload, cmd_relocate, cmd_fpswa;
|
||||
|
||||
GRUB_MOD_INIT(linux)
|
||||
{
|
||||
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
|
||||
"FILE [ARGS...]", "Load Linux.");
|
||||
|
||||
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
|
||||
"FILE", "Load initrd.");
|
||||
|
||||
cmd_payload = grub_register_command ("payload", grub_cmd_payload,
|
||||
"FILE [ARGS...]",
|
||||
"Load an additional file.");
|
||||
|
||||
cmd_relocate = grub_register_command ("relocate", grub_cmd_relocate,
|
||||
"[on|off|force]",
|
||||
"Set relocate feature.");
|
||||
|
||||
cmd_fpswa = grub_register_command ("fpswa", grub_cmd_fpswa,
|
||||
"", "Display FPSWA version.");
|
||||
|
||||
my_mod = mod;
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(linux)
|
||||
{
|
||||
grub_unregister_command (cmd_linux);
|
||||
grub_unregister_command (cmd_initrd);
|
||||
grub_unregister_command (cmd_payload);
|
||||
grub_unregister_command (cmd_relocate);
|
||||
grub_unregister_command (cmd_fpswa);
|
||||
}
|
|
@ -27,19 +27,22 @@
|
|||
#include <grub/mips/relocator.h>
|
||||
#include <grub/memory.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/lib/cmdline.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
/* For frequencies. */
|
||||
#include <grub/pci.h>
|
||||
#include <grub/machine/time.h>
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
/* This can be detected on runtime from PMON, but:
|
||||
a) it wouldn't work when GRUB is the firmware
|
||||
and
|
||||
b) for now we only support Yeeloong anyway. */
|
||||
#define LOONGSON_MACHTYPE "machtype=lemote-yeeloong-2f-8.9inches"
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
#include <grub/pci.h>
|
||||
#include <grub/machine/kernel.h>
|
||||
|
||||
const char loongson_machtypes[][60] =
|
||||
{
|
||||
[GRUB_ARCH_MACHINE_YEELOONG] = "machtype=lemote-yeeloong-2f-8.9inches",
|
||||
[GRUB_ARCH_MACHINE_FULOONG] = "machtype=lemote-fuloong-2f-unknown"
|
||||
};
|
||||
#endif
|
||||
|
||||
static grub_dl_t my_mod;
|
||||
|
@ -51,9 +54,16 @@ static grub_size_t linux_size;
|
|||
static struct grub_relocator *relocator;
|
||||
static grub_uint8_t *playground;
|
||||
static grub_addr_t target_addr, entry_addr;
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
static char *params;
|
||||
#else
|
||||
static int linux_argc;
|
||||
static grub_off_t argv_off, envp_off;
|
||||
static grub_off_t argv_off;
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
static grub_off_t envp_off;
|
||||
#endif
|
||||
static grub_off_t rd_addr_arg_off, rd_size_arg_off;
|
||||
#endif
|
||||
static int initrd_loaded = 0;
|
||||
|
||||
static grub_err_t
|
||||
|
@ -61,11 +71,43 @@ grub_linux_boot (void)
|
|||
{
|
||||
struct grub_relocator32_state state;
|
||||
|
||||
grub_memset (&state, 0, sizeof (state));
|
||||
|
||||
/* Boot the kernel. */
|
||||
state.gpr[1] = entry_addr;
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
{
|
||||
grub_err_t err;
|
||||
grub_relocator_chunk_t ch;
|
||||
grub_uint32_t *memsize;
|
||||
grub_uint32_t *magic;
|
||||
char *str;
|
||||
|
||||
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
|
||||
((16 << 20) - 264),
|
||||
grub_strlen (params) + 1 + 8);
|
||||
if (err)
|
||||
return err;
|
||||
memsize = get_virtual_current_address (ch);
|
||||
magic = memsize + 1;
|
||||
*memsize = grub_mmap_get_lower ();
|
||||
*magic = 0x12345678;
|
||||
str = (char *) (magic + 1);
|
||||
grub_strcpy (str, params);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
state.gpr[4] = linux_argc;
|
||||
state.gpr[5] = target_addr + argv_off;
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
state.gpr[6] = target_addr + envp_off;
|
||||
#else
|
||||
state.gpr[6] = 0;
|
||||
#endif
|
||||
state.gpr[7] = 0;
|
||||
#endif
|
||||
state.jumpreg = 1;
|
||||
grub_relocator32_boot (relocator, state);
|
||||
|
||||
|
@ -78,6 +120,11 @@ grub_linux_unload (void)
|
|||
grub_relocator_unload (relocator);
|
||||
grub_dl_unref (my_mod);
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
grub_free (params);
|
||||
params = 0;
|
||||
#endif
|
||||
|
||||
loaded = 0;
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
|
@ -197,12 +244,18 @@ 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;
|
||||
#ifndef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
int i;
|
||||
grub_uint32_t *linux_argv;
|
||||
char *linux_args;
|
||||
#endif
|
||||
grub_err_t err;
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
char *linux_envs;
|
||||
grub_uint32_t *linux_envp;
|
||||
#endif
|
||||
|
||||
if (argc == 0)
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
|
||||
|
@ -222,9 +275,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
grub_loader_unset ();
|
||||
loaded = 0;
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
size = 0;
|
||||
#else
|
||||
/* For arguments. */
|
||||
linux_argc = argc;
|
||||
#ifdef LOONGSON_MACHTYPE
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
linux_argc++;
|
||||
#endif
|
||||
/* Main arguments. */
|
||||
|
@ -239,8 +295,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
/* Normal arguments. */
|
||||
for (i = 1; i < argc; i++)
|
||||
size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4);
|
||||
#ifdef LOONGSON_MACHTYPE
|
||||
size += ALIGN_UP (sizeof (LOONGSON_MACHTYPE), 4);
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
size += ALIGN_UP (sizeof (loongson_machtypes[0]), 4);
|
||||
#endif
|
||||
|
||||
/* rd arguments. */
|
||||
|
@ -254,6 +310,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
+ ALIGN_UP (sizeof ("highmemsize=XXXXXXXXXXXXXXXXXXXX"), 4)
|
||||
+ ALIGN_UP (sizeof ("busclock=XXXXXXXXXX"), 4)
|
||||
+ ALIGN_UP (sizeof ("cpuclock=XXXXXXXXXX"), 4);
|
||||
#endif
|
||||
|
||||
if (grub_elf_is_elf32 (elf))
|
||||
err = grub_linux_load32 (elf, &extra, size);
|
||||
|
@ -268,6 +325,20 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
if (err)
|
||||
return err;
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
/* Create kernel command line. */
|
||||
size = grub_loader_cmdline_size(argc, argv);
|
||||
params = grub_malloc (size + sizeof (LINUX_IMAGE));
|
||||
if (! params)
|
||||
{
|
||||
grub_linux_unload ();
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
grub_memcpy (params, LINUX_IMAGE, sizeof (LINUX_IMAGE));
|
||||
grub_create_loader_cmdline (argc, argv, params + sizeof (LINUX_IMAGE) - 1,
|
||||
size);
|
||||
#else
|
||||
linux_argv = extra;
|
||||
argv_off = (grub_uint8_t *) linux_argv - (grub_uint8_t *) playground;
|
||||
extra = linux_argv + (linux_argc + 1 + 2);
|
||||
|
@ -279,14 +350,20 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
linux_argv++;
|
||||
linux_args += ALIGN_UP (sizeof ("a0"), 4);
|
||||
|
||||
#ifdef LOONGSON_MACHTYPE
|
||||
/* In Loongson platform, it is the responsibility of the bootloader/firmware
|
||||
to supply the OS kernel with machine type information. */
|
||||
grub_memcpy (linux_args, LOONGSON_MACHTYPE, sizeof (LOONGSON_MACHTYPE));
|
||||
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
|
||||
+ target_addr;
|
||||
linux_argv++;
|
||||
linux_args += ALIGN_UP (sizeof (LOONGSON_MACHTYPE), 4);
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
{
|
||||
unsigned mtype = grub_arch_machine;
|
||||
if (mtype >= ARRAY_SIZE (loongson_machtypes))
|
||||
mtype = 0;
|
||||
/* In Loongson platform, it is the responsibility of the bootloader/firmware
|
||||
to supply the OS kernel with machine type information. */
|
||||
grub_memcpy (linux_args, loongson_machtypes[mtype],
|
||||
sizeof (loongson_machtypes[mtype]));
|
||||
*linux_argv = (grub_uint8_t *) linux_args - (grub_uint8_t *) playground
|
||||
+ target_addr;
|
||||
linux_argv++;
|
||||
linux_args += ALIGN_UP (sizeof (loongson_machtypes[mtype]), 4);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i = 1; i < argc; i++)
|
||||
|
@ -313,6 +390,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
|
||||
extra = linux_args;
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_LOONGSON
|
||||
linux_envp = extra;
|
||||
envp_off = (grub_uint8_t *) linux_envp - (grub_uint8_t *) playground;
|
||||
linux_envs = (char *) (linux_envp + 5);
|
||||
|
@ -340,8 +418,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|||
+ target_addr;
|
||||
linux_envs += ALIGN_UP (grub_strlen (linux_envs) + 1, 4);
|
||||
|
||||
|
||||
linux_envp[4] = 0;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
|
||||
initrd_loaded = 0;
|
||||
|
@ -404,6 +483,21 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
|
|||
return grub_errno;
|
||||
}
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
|
||||
{
|
||||
char *tmp;
|
||||
tmp = grub_xasprintf ("%s rd_start=0x%" PRIxGRUB_ADDR
|
||||
" rd_size=0x%" PRIxGRUB_ADDR, params,
|
||||
initrd_dest, size);
|
||||
if (!tmp)
|
||||
{
|
||||
grub_file_close (file);
|
||||
return grub_errno;
|
||||
}
|
||||
grub_free (params);
|
||||
params = tmp;
|
||||
}
|
||||
#else
|
||||
grub_snprintf ((char *) playground + rd_addr_arg_off,
|
||||
sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%llx",
|
||||
(unsigned long long) initrd_dest);
|
||||
|
@ -417,6 +511,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
|
|||
((grub_uint32_t *) (playground + argv_off))[linux_argc]
|
||||
= target_addr + rd_size_arg_off;
|
||||
linux_argc++;
|
||||
#endif
|
||||
|
||||
initrd_loaded = 1;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue