2010-01-18 14:17:47 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-18 17:36:16 +00:00
|
|
|
* Copyright (C) 2008,2010 Free Software Foundation, Inc.
|
2010-01-18 14:17:47 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2010-01-18 17:36:16 +00:00
|
|
|
#include <grub/command.h>
|
2010-01-18 14:17:47 +00:00
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/cache.h>
|
2010-01-18 14:28:03 +00:00
|
|
|
#include <grub/kernel.h>
|
2010-01-18 14:17:47 +00:00
|
|
|
#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
|
|
|
|
|
2010-01-18 14:24:17 +00:00
|
|
|
struct ia64_boot_param
|
|
|
|
{
|
2010-01-18 14:17:47 +00:00
|
|
|
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 */
|
2010-01-18 14:24:17 +00:00
|
|
|
struct
|
|
|
|
{
|
2010-01-18 14:17:47 +00:00
|
|
|
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 */
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_uint64_t payloads_chain;
|
|
|
|
grub_uint64_t payloads_nbr;
|
2010-01-18 14:17:47 +00:00
|
|
|
};
|
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
struct ia64_boot_payload
|
2010-01-18 14:24:17 +00:00
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_uint64_t start;
|
|
|
|
grub_uint64_t length;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
/* Payload command line */
|
2010-01-18 14:17:47 +00:00
|
|
|
grub_uint64_t cmdline;
|
|
|
|
|
|
|
|
grub_uint64_t next;
|
|
|
|
};
|
|
|
|
|
2010-01-18 14:24:17 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2010-01-18 14:17:47 +00:00
|
|
|
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;
|
2010-01-18 14:28:03 +00:00
|
|
|
static struct ia64_boot_payload *last_payload = NULL;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
|
|
|
/* 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,
|
2010-01-18 14:28:03 +00:00
|
|
|
(void *)&fpswa_protocol,
|
2010-01-18 14:17:47 +00:00
|
|
|
NULL, &size, &fpswa_image);
|
|
|
|
if (status != GRUB_EFI_SUCCESS)
|
|
|
|
{
|
|
|
|
grub_printf("Could not locate FPSWA driver\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-01-18 14:28:03 +00:00
|
|
|
status = bs->handle_protocol (fpswa_image,
|
|
|
|
(void *)&fpswa_protocol, (void *)&fpswa);
|
2010-01-18 14:17:47 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages ((grub_addr_t) kernel_mem, kernel_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
kernel_mem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initrd_mem)
|
|
|
|
{
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
initrd_mem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot_param)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
struct ia64_boot_payload *payload;
|
|
|
|
struct ia64_boot_payload *next_payload;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
/* Free payloads. */
|
|
|
|
payload = (struct ia64_boot_payload *)boot_param->payloads_chain;
|
|
|
|
while (payload != 0)
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
next_payload = (struct ia64_boot_payload *)payload->next;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages
|
2010-01-18 14:28:03 +00:00
|
|
|
(payload->start, page_align (payload->length) >> 12);
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages ((grub_efi_physical_address_t)payload, 1);
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
payload = next_payload;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free bootparam. */
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages ((grub_efi_physical_address_t)boot_param,
|
|
|
|
boot_param_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
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;
|
2010-09-01 01:05:36 +00:00
|
|
|
grub_err_t err;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
|
|
|
/* 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 ();
|
2010-09-01 07:43:36 +00:00
|
|
|
mmap_buf = grub_efi_allocate_pages (0, page_align (mmap_size) >> 12);
|
2010-01-18 14:17:47 +00:00
|
|
|
if (! mmap_buf)
|
|
|
|
grub_fatal ("cannot allocate memory map");
|
2010-09-01 01:05:36 +00:00
|
|
|
err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, &map_key,
|
|
|
|
&desc_size, &desc_version);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-09-01 07:43:36 +00:00
|
|
|
kernel_mem = grub_efi_allocate_pages (low_addr, kernel_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
reloc_offset = 0;
|
|
|
|
}
|
|
|
|
/* Try to relocate. */
|
|
|
|
if (! kernel_mem && relocate != RELOCATE_OFF)
|
|
|
|
{
|
|
|
|
kernel_mem = allocate_pages (align, kernel_pages, low_addr);
|
|
|
|
if (kernel_mem)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
reloc_offset = (grub_uint64_t)kernel_mem - low_addr;
|
|
|
|
grub_printf (" Relocated at %p (offset=%016lx)\n",
|
2010-01-18 14:17:47 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_printf (" [paddr=%lx load=%lx memsz=%08lx "
|
2010-01-18 14:17:47 +00:00
|
|
|
"off=%lx flags=%x]\n",
|
|
|
|
phdr->p_paddr, phdr->p_paddr + reloc_offset,
|
|
|
|
phdr->p_memsz, phdr->p_offset, phdr->p_flags);
|
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
if (grub_file_seek (file, phdr->p_offset) == (grub_off_t)-1)
|
2010-01-18 14:17:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-03 01:28:14 +00:00
|
|
|
file = grub_file_open (argv[0]);
|
2010-01-18 14:17:47 +00:00
|
|
|
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;
|
2010-09-01 07:43:36 +00:00
|
|
|
boot_param = grub_efi_allocate_pages (0, boot_param_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
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] = '=';
|
|
|
|
|
2010-09-01 07:43:36 +00:00
|
|
|
boot_param->command_line = (grub_uint64_t) cmdline;
|
|
|
|
boot_param->efi_systab = (grub_uint64_t) grub_efi_system_table;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-09-01 07:43:36 +00:00
|
|
|
grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
|
|
|
|
boot_param_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
grub_dl_unref (my_mod);
|
|
|
|
}
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_errno;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
grub_file_t file = 0;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "No filename specified");
|
2010-01-18 14:17:47 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! loaded)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, "You need to load the kernel first.");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-01-03 01:28:14 +00:00
|
|
|
file = grub_file_open (argv[0]);
|
2010-01-18 14:17:47 +00:00
|
|
|
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);
|
2010-09-01 07:43:36 +00:00
|
|
|
initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
|
2010-01-18 14:17:47 +00:00
|
|
|
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);
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_errno;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_payload (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
grub_file_t file = 0;
|
|
|
|
grub_ssize_t size, len = 0;
|
2010-01-18 14:28:03 +00:00
|
|
|
char *base = 0, *cmdline = 0, *p;
|
|
|
|
struct ia64_boot_payload *payload = NULL;
|
2010-01-18 14:17:47 +00:00
|
|
|
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,
|
2010-01-18 14:28:03 +00:00
|
|
|
"You need to load the kernel first");
|
2010-01-18 14:17:47 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-01-03 01:28:14 +00:00
|
|
|
file = grub_file_open (argv[0]);
|
2010-01-18 14:17:47 +00:00
|
|
|
if (! file)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
size = grub_file_size (file);
|
2010-09-01 07:43:36 +00:00
|
|
|
base = grub_efi_allocate_pages (0, page_align (size) >> 12);
|
2010-01-18 14:28:03 +00:00
|
|
|
if (! base)
|
2010-01-18 14:17:47 +00:00
|
|
|
goto fail;
|
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_printf ("Payload %s [addr=%lx + %lx]\n",
|
|
|
|
argv[0], (grub_uint64_t)base, size);
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
if (grub_file_read (file, base, size) != size)
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
len = sizeof (struct ia64_boot_payload);
|
2010-01-18 14:17:47 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
len += grub_strlen (argv[i]) + 1;
|
|
|
|
|
|
|
|
if (len > 4096)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_error (GRUB_ERR_OUT_OF_RANGE, "payload command line too long");
|
2010-01-18 14:17:47 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2010-09-01 07:43:36 +00:00
|
|
|
payload = grub_efi_allocate_pages (0, 1);
|
2010-01-18 14:28:03 +00:00
|
|
|
if (! payload)
|
2010-01-18 14:17:47 +00:00
|
|
|
goto fail;
|
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
p = (char *)(payload + 1);
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
payload->start = (grub_uint64_t)base;
|
|
|
|
payload->length = size;
|
|
|
|
payload->cmdline = (grub_uint64_t)p;
|
|
|
|
payload->next = 0;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
2010-01-18 14:28:03 +00:00
|
|
|
if (last_payload)
|
|
|
|
last_payload->next = (grub_uint64_t)payload;
|
2010-01-18 14:17:47 +00:00
|
|
|
else
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
last_payload = payload;
|
|
|
|
boot_param->payloads_chain = (grub_uint64_t)payload;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
2010-01-18 14:28:03 +00:00
|
|
|
boot_param->payloads_nbr++;
|
2010-01-18 14:17:47 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2010-01-18 14:28:03 +00:00
|
|
|
grub_free (base);
|
2010-01-18 14:17:47 +00:00
|
|
|
grub_free (cmdline);
|
|
|
|
}
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_errno;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_relocate (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[])
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
static const char * const vals[] = { "off", "on", "force"};
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
|
|
|
grub_printf ("relocate is %s\n", vals[relocate]);
|
2010-09-01 01:05:36 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
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;
|
2010-09-01 01:05:36 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown relocate value");
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "accept 0 or 1 argument");
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
2010-09-01 01:05:36 +00:00
|
|
|
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_fpswa (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char *argv[] __attribute__((unused)))
|
2010-01-18 14:17:47 +00:00
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
{
|
2010-09-01 01:05:36 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Arguments not expected");
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
query_fpswa ();
|
|
|
|
if (fpswa == NULL)
|
|
|
|
grub_printf ("No FPSWA loaded\n");
|
|
|
|
else
|
|
|
|
grub_printf ("FPSWA revision: %x\n", fpswa->revision);
|
2010-09-01 01:05:36 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
static grub_command_t cmd_linux, cmd_initrd, cmd_payload, cmd_relocate, cmd_fpswa;
|
|
|
|
|
2010-01-18 14:17:47 +00:00
|
|
|
GRUB_MOD_INIT(linux)
|
|
|
|
{
|
2010-09-01 01:05:36 +00:00
|
|
|
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
|
|
|
|
"FILE [ARGS...]", "Load Linux.");
|
2010-01-18 17:36:16 +00:00
|
|
|
|
2010-09-01 01:05:36 +00:00
|
|
|
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.");
|
2010-01-18 17:36:16 +00:00
|
|
|
|
2010-01-18 14:17:47 +00:00
|
|
|
my_mod = mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(linux)
|
|
|
|
{
|
2010-09-01 01:05:36 +00:00
|
|
|
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);
|
2010-01-18 14:17:47 +00:00
|
|
|
}
|