2009-04-13 07:08:10 +00:00
|
|
|
|
/* 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>
|
2009-04-19 08:51:04 +00:00
|
|
|
|
#include <grub/command.h>
|
2010-01-08 Carles Pina i Estany <carles@pina.cat>
* loader/efi/appleloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/efi/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/efi/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/xnu.c: Include `<grub/i18n.h>'.
(grub_cpu_xnu_init): Gettextizze.
* loader/multiboot_loader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/powerpc/ieee1275/linux.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/sparc64/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_linux_load64): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/xnu.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* po/POTFILES: Add `loader/efi/appleloader.c',
`loader/efi/chainloader.c', `loader/i386/efi/linux.c',
`loader/i386/ieee1275/linux.c', `loader/i386/linux.c',
`loader/i386/pc/chainloader.c', `loader/i386/pc/linux.c',
`loader/i386/xnu.c', `loader/multiboot_loader.c',
`loader/powerpc/ieee1275/linux.c', `loader/sparc64/ieee1275/linux.c'
and `loader/xnu.c'.
2010-01-08 21:35:38 +00:00
|
|
|
|
#include <grub/i18n.h>
|
2010-09-04 15:10:10 +00:00
|
|
|
|
#include <grub/memory.h>
|
2011-01-07 16:09:39 +00:00
|
|
|
|
#include <grub/lib/cmdline.h>
|
2013-03-22 20:01:28 +00:00
|
|
|
|
#include <grub/linux.h>
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
|
2009-04-13 07:08:10 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2009-05-04 20:06:05 +00:00
|
|
|
|
/* Any pointer we dereference in the kernel image must be relocated
|
2009-04-13 07:08:10 +00:00
|
|
|
|
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. */
|
2010-02-14 13:32:21 +00:00
|
|
|
|
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");
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2013-01-21 14:41:06 +00:00
|
|
|
|
/* Context for alloc_phys. */
|
|
|
|
|
struct alloc_phys_ctx
|
|
|
|
|
{
|
|
|
|
|
grub_addr_t size;
|
|
|
|
|
grub_addr_t ret;
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
/* Helper for alloc_phys. */
|
|
|
|
|
static int
|
|
|
|
|
alloc_phys_choose (grub_uint64_t addr, grub_uint64_t len,
|
|
|
|
|
grub_memory_type_t type, void *data)
|
2009-04-13 07:08:10 +00:00
|
|
|
|
{
|
2013-01-21 14:41:06 +00:00
|
|
|
|
struct alloc_phys_ctx *ctx = data;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_addr_t end = addr + len;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (type != 1)
|
|
|
|
|
return 0;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
addr = ALIGN_UP (addr, FOUR_MB);
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if (addr + ctx->size >= end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 0;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (addr >= grub_phys_start && addr < grub_phys_end)
|
|
|
|
|
{
|
|
|
|
|
addr = ALIGN_UP (grub_phys_end, FOUR_MB);
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if (addr + ctx->size >= end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if ((addr + ctx->size) >= grub_phys_start
|
|
|
|
|
&& (addr + ctx->size) < grub_phys_end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
{
|
|
|
|
|
addr = ALIGN_UP (grub_phys_end, FOUR_MB);
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if (addr + ctx->size >= end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
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;
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if (addr + ctx->size >= end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if ((addr + ctx->size) >= linux_paddr
|
|
|
|
|
&& (addr + ctx->size) < linux_end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
{
|
|
|
|
|
addr = linux_end;
|
2013-01-21 14:41:06 +00:00
|
|
|
|
if (addr + ctx->size >= end)
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-21 14:41:06 +00:00
|
|
|
|
ctx->ret = addr;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static grub_addr_t
|
|
|
|
|
alloc_phys (grub_addr_t size)
|
|
|
|
|
{
|
2013-01-21 14:41:06 +00:00
|
|
|
|
struct alloc_phys_ctx ctx = {
|
|
|
|
|
.size = size,
|
|
|
|
|
.ret = (grub_addr_t) -1
|
|
|
|
|
};
|
2013-01-15 12:02:35 +00:00
|
|
|
|
|
2013-01-21 14:41:06 +00:00
|
|
|
|
grub_machine_mmap_iterate (alloc_phys_choose, &ctx);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-21 14:41:06 +00:00
|
|
|
|
return ctx.ret;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static grub_err_t
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_linux_load64 (grub_elf_t elf, const char *filename)
|
2009-04-13 07:08:10 +00:00
|
|
|
|
{
|
|
|
|
|
grub_addr_t off, paddr, base;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
linux_entry = elf->ehdr.ehdr64.e_entry;
|
|
|
|
|
linux_addr = 0x40004000;
|
|
|
|
|
off = 0x4000;
|
2013-03-02 15:45:57 +00:00
|
|
|
|
linux_size = grub_elf64_size (elf, 0, 0);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
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,
|
2009-12-25 00:04:51 +00:00
|
|
|
|
"couldn't allocate physical memory");
|
2010-02-13 15:48:22 +00:00
|
|
|
|
ret = grub_ieee1275_map (paddr, linux_addr - off,
|
|
|
|
|
linux_size + off, IEEE1275_MAP_DEFAULT);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
if (ret)
|
|
|
|
|
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
2009-12-25 00:04:51 +00:00
|
|
|
|
"couldn't map physical memory");
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2010-01-08 Carles Pina i Estany <carles@pina.cat>
* loader/efi/appleloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/efi/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/efi/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/xnu.c: Include `<grub/i18n.h>'.
(grub_cpu_xnu_init): Gettextizze.
* loader/multiboot_loader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/powerpc/ieee1275/linux.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/sparc64/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_linux_load64): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/xnu.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* po/POTFILES: Add `loader/efi/appleloader.c',
`loader/efi/chainloader.c', `loader/i386/efi/linux.c',
`loader/i386/ieee1275/linux.c', `loader/i386/linux.c',
`loader/i386/pc/chainloader.c', `loader/i386/pc/linux.c',
`loader/i386/xnu.c', `loader/multiboot_loader.c',
`loader/powerpc/ieee1275/linux.c', `loader/sparc64/ieee1275/linux.c'
and `loader/xnu.c'.
2010-01-08 21:35:38 +00:00
|
|
|
|
grub_dprintf ("loader", "Loading Linux at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
|
2009-04-13 07:08:10 +00:00
|
|
|
|
linux_addr, paddr, linux_size);
|
|
|
|
|
|
|
|
|
|
linux_paddr = paddr;
|
|
|
|
|
|
|
|
|
|
base = linux_entry - off;
|
|
|
|
|
|
|
|
|
|
/* Now load the segments into the area we claimed. */
|
2013-03-02 15:45:57 +00:00
|
|
|
|
return grub_elf64_load (elf, filename, (void *) (linux_addr - off - base), GRUB_ELF_LOAD_FLAGS_NONE, 0, 0);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-19 08:51:04 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
|
int argc, char *argv[])
|
2009-04-13 07:08:10 +00:00
|
|
|
|
{
|
|
|
|
|
grub_file_t file = 0;
|
|
|
|
|
grub_elf_t elf = 0;
|
|
|
|
|
int size;
|
|
|
|
|
|
|
|
|
|
grub_dl_ref (my_mod);
|
|
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-05 11:05:36 +00:00
|
|
|
|
file = grub_file_open (argv[0]);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
if (!file)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2012-02-08 18:26:01 +00:00
|
|
|
|
elf = grub_elf_file (file, argv[0]);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
if (! elf)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
|
|
|
|
|
{
|
|
|
|
|
grub_error (GRUB_ERR_UNKNOWN_OS,
|
2012-02-08 18:26:01 +00:00
|
|
|
|
N_("this ELF file is not of the right type"));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Release the previously used memory. */
|
|
|
|
|
grub_loader_unset ();
|
|
|
|
|
|
|
|
|
|
if (grub_elf_is_elf64 (elf))
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_linux_load64 (elf, argv[0]);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2012-02-26 16:28:05 +00:00
|
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid arch-dependent ELF magic"));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-07 16:09:39 +00:00
|
|
|
|
size = grub_loader_cmdline_size(argc, argv);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2011-01-07 16:09:39 +00:00
|
|
|
|
linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
if (! linux_args)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2011-01-07 16:09:39 +00:00
|
|
|
|
/* Create kernel command line. */
|
|
|
|
|
grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
|
|
|
|
|
grub_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1,
|
|
|
|
|
size);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2009-04-19 08:51:04 +00:00
|
|
|
|
|
|
|
|
|
return grub_errno;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-19 08:51:04 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
|
int argc, char *argv[])
|
2009-04-13 07:08:10 +00:00
|
|
|
|
{
|
2012-01-13 12:54:24 +00:00
|
|
|
|
grub_size_t size = 0;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
grub_addr_t paddr;
|
|
|
|
|
grub_addr_t addr;
|
|
|
|
|
int ret;
|
2013-03-22 20:01:28 +00:00
|
|
|
|
struct grub_linux_initrd_context initrd_ctx;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loaded)
|
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
|
grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 20:01:28 +00:00
|
|
|
|
if (grub_initrd_init (argc, argv, &initrd_ctx))
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
|
2013-03-22 20:01:28 +00:00
|
|
|
|
size = grub_get_initrd_size (&initrd_ctx);
|
2012-01-13 12:54:24 +00:00
|
|
|
|
|
2009-04-13 07:08:10 +00:00
|
|
|
|
addr = 0x60000000;
|
|
|
|
|
|
|
|
|
|
paddr = alloc_phys (size);
|
|
|
|
|
if (paddr == (grub_addr_t) -1)
|
|
|
|
|
{
|
|
|
|
|
grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
2009-12-25 00:04:51 +00:00
|
|
|
|
"couldn't allocate physical memory");
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2010-02-13 15:48:22 +00:00
|
|
|
|
ret = grub_ieee1275_map (paddr, addr, size, IEEE1275_MAP_DEFAULT);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
if (ret)
|
|
|
|
|
{
|
|
|
|
|
grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
2009-12-25 00:04:51 +00:00
|
|
|
|
"couldn't map physical memory");
|
2009-04-13 07:08:10 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
|
|
|
|
|
addr, paddr, size);
|
|
|
|
|
|
2013-03-22 20:01:28 +00:00
|
|
|
|
if (grub_initrd_load (&initrd_ctx, argv, (void *) addr))
|
|
|
|
|
goto fail;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
|
|
|
|
initrd_addr = addr;
|
|
|
|
|
initrd_paddr = paddr;
|
|
|
|
|
initrd_size = size;
|
|
|
|
|
|
|
|
|
|
fail:
|
2013-03-22 20:01:28 +00:00
|
|
|
|
grub_initrd_close (&initrd_ctx);
|
2009-04-19 08:51:04 +00:00
|
|
|
|
|
|
|
|
|
return grub_errno;
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
/* Helper for determine_phys_base. */
|
|
|
|
|
static int
|
|
|
|
|
get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__ ((unused)),
|
|
|
|
|
grub_uint32_t type, void *data __attribute__ ((unused)))
|
2009-04-13 07:08:10 +00:00
|
|
|
|
{
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (type != 1)
|
2009-04-13 07:08:10 +00:00
|
|
|
|
return 0;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
if (addr < phys_base)
|
|
|
|
|
phys_base = addr;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-04-13 07:08:10 +00:00
|
|
|
|
|
2013-01-15 12:02:35 +00:00
|
|
|
|
static void
|
|
|
|
|
determine_phys_base (void)
|
|
|
|
|
{
|
2009-04-13 07:08:10 +00:00
|
|
|
|
phys_base = ~(grub_uint64_t) 0;
|
2013-01-15 12:02:35 +00:00
|
|
|
|
grub_machine_mmap_iterate (get_physbase, NULL);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2009-05-04 20:06:05 +00:00
|
|
|
|
grub_printf ("Cannot fetch /virtual-memory/translations property.\n");
|
2009-04-13 07:08:10 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-19 08:51:04 +00:00
|
|
|
|
static grub_command_t cmd_linux, cmd_initrd;
|
|
|
|
|
|
2009-04-13 07:08:10 +00:00
|
|
|
|
GRUB_MOD_INIT(linux)
|
|
|
|
|
{
|
|
|
|
|
determine_phys_base ();
|
|
|
|
|
fetch_translations ();
|
|
|
|
|
|
2009-04-19 08:51:04 +00:00
|
|
|
|
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
|
2010-01-08 Carles Pina i Estany <carles@pina.cat>
* loader/efi/appleloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/efi/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/efi/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/xnu.c: Include `<grub/i18n.h>'.
(grub_cpu_xnu_init): Gettextizze.
* loader/multiboot_loader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/powerpc/ieee1275/linux.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/sparc64/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_linux_load64): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/xnu.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* po/POTFILES: Add `loader/efi/appleloader.c',
`loader/efi/chainloader.c', `loader/i386/efi/linux.c',
`loader/i386/ieee1275/linux.c', `loader/i386/linux.c',
`loader/i386/pc/chainloader.c', `loader/i386/pc/linux.c',
`loader/i386/xnu.c', `loader/multiboot_loader.c',
`loader/powerpc/ieee1275/linux.c', `loader/sparc64/ieee1275/linux.c'
and `loader/xnu.c'.
2010-01-08 21:35:38 +00:00
|
|
|
|
0, N_("Load Linux."));
|
2009-04-19 08:51:04 +00:00
|
|
|
|
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
|
2010-01-08 Carles Pina i Estany <carles@pina.cat>
* loader/efi/appleloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/efi/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/efi/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/chainloader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/pc/linux.c: Include `<grub/i18n.h>'.
(grub_cmd_linux): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/i386/xnu.c: Include `<grub/i18n.h>'.
(grub_cpu_xnu_init): Gettextizze.
* loader/multiboot_loader.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/powerpc/ieee1275/linux.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* loader/sparc64/ieee1275/linux.c: Include `<grub/i18n.h>'.
(grub_linux_load64): Capitalise Linux.
(GRUB_MOD_INIT): Gettextizze.
* loader/xnu.c: Include `<grub/i18n.h>'.
(GRUB_MOD_INIT): Gettextizze.
* po/POTFILES: Add `loader/efi/appleloader.c',
`loader/efi/chainloader.c', `loader/i386/efi/linux.c',
`loader/i386/ieee1275/linux.c', `loader/i386/linux.c',
`loader/i386/pc/chainloader.c', `loader/i386/pc/linux.c',
`loader/i386/xnu.c', `loader/multiboot_loader.c',
`loader/powerpc/ieee1275/linux.c', `loader/sparc64/ieee1275/linux.c'
and `loader/xnu.c'.
2010-01-08 21:35:38 +00:00
|
|
|
|
0, N_("Load initrd."));
|
2009-04-13 07:08:10 +00:00
|
|
|
|
my_mod = mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(linux)
|
|
|
|
|
{
|
2009-04-19 08:51:04 +00:00
|
|
|
|
grub_unregister_command (cmd_linux);
|
|
|
|
|
grub_unregister_command (cmd_initrd);
|
2009-04-13 07:08:10 +00:00
|
|
|
|
}
|