2N/A/* linux.c - boot Linux */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003, 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/elf.h>
2N/A#include <grub/elfload.h>
2N/A#include <grub/loader.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/ieee1275/ieee1275.h>
2N/A#include <grub/command.h>
2N/A#include <grub/i18n.h>
2N/A#include <grub/memory.h>
2N/A#include <grub/lib/cmdline.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/Astatic int loaded;
2N/A
2N/A/* /virtual-memory/translations property layout */
2N/Astruct grub_ieee1275_translation {
2N/A grub_uint64_t vaddr;
2N/A grub_uint64_t size;
2N/A grub_uint64_t data;
2N/A};
2N/A
2N/Astatic struct grub_ieee1275_translation *of_trans;
2N/Astatic int of_num_trans;
2N/A
2N/Astatic grub_addr_t phys_base;
2N/Astatic grub_addr_t grub_phys_start;
2N/Astatic grub_addr_t grub_phys_end;
2N/A
2N/Astatic grub_addr_t initrd_addr;
2N/Astatic grub_addr_t initrd_paddr;
2N/Astatic grub_size_t initrd_size;
2N/A
2N/Astatic Elf64_Addr linux_entry;
2N/Astatic grub_addr_t linux_addr;
2N/Astatic grub_addr_t linux_paddr;
2N/Astatic grub_size_t linux_size;
2N/A
2N/Astatic char *linux_args;
2N/A
2N/Astruct linux_bootstr_info {
2N/A int len, valid;
2N/A char buf[];
2N/A};
2N/A
2N/Astruct linux_hdrs {
2N/A /* All HdrS versions support these fields. */
2N/A unsigned int start_insns[2];
2N/A char magic[4]; /* "HdrS" */
2N/A unsigned int linux_kernel_version; /* LINUX_VERSION_CODE */
2N/A unsigned short hdrs_version;
2N/A unsigned short root_flags;
2N/A unsigned short root_dev;
2N/A unsigned short ram_flags;
2N/A unsigned int __deprecated_ramdisk_image;
2N/A unsigned int ramdisk_size;
2N/A
2N/A /* HdrS versions 0x0201 and higher only */
2N/A char *reboot_command;
2N/A
2N/A /* HdrS versions 0x0202 and higher only */
2N/A struct linux_bootstr_info *bootstr_info;
2N/A
2N/A /* HdrS versions 0x0301 and higher only */
2N/A unsigned long ramdisk_image;
2N/A};
2N/A
2N/Astatic grub_err_t
2N/Agrub_linux_boot (void)
2N/A{
2N/A struct linux_bootstr_info *bp;
2N/A struct linux_hdrs *hp;
2N/A grub_addr_t addr;
2N/A
2N/A hp = (struct linux_hdrs *) linux_addr;
2N/A
2N/A /* Any pointer we dereference in the kernel image must be relocated
2N/A to where we actually loaded the kernel. */
2N/A addr = (grub_addr_t) hp->bootstr_info;
2N/A addr += (linux_addr - linux_entry);
2N/A bp = (struct linux_bootstr_info *) addr;
2N/A
2N/A /* Set the command line arguments, unless the kernel has been
2N/A built with a fixed CONFIG_CMDLINE. */
2N/A if (!bp->valid)
2N/A {
2N/A int len = grub_strlen (linux_args) + 1;
2N/A if (bp->len < len)
2N/A len = bp->len;
2N/A memcpy(bp->buf, linux_args, len);
2N/A bp->buf[len-1] = '\0';
2N/A bp->valid = 1;
2N/A }
2N/A
2N/A if (initrd_addr)
2N/A {
2N/A /* The kernel expects the physical address, adjusted relative
2N/A to the lowest address advertised in "/memory"'s available
2N/A property.
2N/A
2N/A The history of this is that back when the kernel only supported
2N/A specifying a 32-bit ramdisk address, this was the way to still
2N/A be able to specify the ramdisk physical address even if memory
2N/A started at some place above 4GB.
2N/A
2N/A The magic 0x400000 is KERNBASE, I have no idea why SILO adds
2N/A that term into the address, but it does and thus we have to do
2N/A it too as this is what the kernel expects. */
2N/A hp->ramdisk_image = initrd_paddr - phys_base + 0x400000;
2N/A hp->ramdisk_size = initrd_size;
2N/A }
2N/A
2N/A grub_dprintf ("loader", "Entry point: 0x%lx\n", linux_addr);
2N/A grub_dprintf ("loader", "Initrd at: 0x%lx, size 0x%lx\n", initrd_addr,
2N/A initrd_size);
2N/A grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
2N/A grub_dprintf ("loader", "Jumping to Linux...\n");
2N/A
2N/A /* Boot the kernel. */
2N/A asm volatile ("sethi %hi(grub_ieee1275_entry_fn), %o1\n"
2N/A "ldx [%o1 + %lo(grub_ieee1275_entry_fn)], %o4\n"
2N/A "sethi %hi(grub_ieee1275_original_stack), %o1\n"
2N/A "ldx [%o1 + %lo(grub_ieee1275_original_stack)], %o6\n"
2N/A "sethi %hi(linux_addr), %o1\n"
2N/A "ldx [%o1 + %lo(linux_addr)], %o5\n"
2N/A "mov %g0, %o0\n"
2N/A "mov %g0, %o2\n"
2N/A "mov %g0, %o3\n"
2N/A "jmp %o5\n"
2N/A "mov %g0, %o1\n");
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_linux_release_mem (void)
2N/A{
2N/A grub_free (linux_args);
2N/A linux_args = 0;
2N/A linux_addr = 0;
2N/A initrd_addr = 0;
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_linux_unload (void)
2N/A{
2N/A grub_err_t err;
2N/A
2N/A err = grub_linux_release_mem ();
2N/A grub_dl_unref (my_mod);
2N/A
2N/A loaded = 0;
2N/A
2N/A return err;
2N/A}
2N/A
2N/A#define FOUR_MB (4 * 1024 * 1024)
2N/A
2N/Astatic grub_addr_t
2N/Aalloc_phys (grub_addr_t size)
2N/A{
2N/A grub_addr_t ret = (grub_addr_t) -1;
2N/A
2N/A auto int NESTED_FUNC_ATTR choose (grub_uint64_t addr, grub_uint64_t len,
2N/A grub_memory_type_t type);
2N/A int NESTED_FUNC_ATTR choose (grub_uint64_t addr, grub_uint64_t len,
2N/A grub_memory_type_t type)
2N/A {
2N/A grub_addr_t end = addr + len;
2N/A
2N/A if (type != 1)
2N/A return 0;
2N/A
2N/A addr = ALIGN_UP (addr, FOUR_MB);
2N/A if (addr + size >= end)
2N/A return 0;
2N/A
2N/A if (addr >= grub_phys_start && addr < grub_phys_end)
2N/A {
2N/A addr = ALIGN_UP (grub_phys_end, FOUR_MB);
2N/A if (addr + size >= end)
2N/A return 0;
2N/A }
2N/A if ((addr + size) >= grub_phys_start
2N/A && (addr + size) < grub_phys_end)
2N/A {
2N/A addr = ALIGN_UP (grub_phys_end, FOUR_MB);
2N/A if (addr + size >= end)
2N/A return 0;
2N/A }
2N/A
2N/A if (loaded)
2N/A {
2N/A grub_addr_t linux_end = ALIGN_UP (linux_paddr + linux_size, FOUR_MB);
2N/A
2N/A if (addr >= linux_paddr && addr < linux_end)
2N/A {
2N/A addr = linux_end;
2N/A if (addr + size >= end)
2N/A return 0;
2N/A }
2N/A if ((addr + size) >= linux_paddr
2N/A && (addr + size) < linux_end)
2N/A {
2N/A addr = linux_end;
2N/A if (addr + size >= end)
2N/A return 0;
2N/A }
2N/A }
2N/A
2N/A ret = addr;
2N/A return 1;
2N/A }
2N/A
2N/A grub_machine_mmap_iterate (choose);
2N/A
2N/A return ret;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_linux_load64 (grub_elf_t elf)
2N/A{
2N/A grub_addr_t off, paddr, base;
2N/A int ret;
2N/A
2N/A linux_entry = elf->ehdr.ehdr64.e_entry;
2N/A linux_addr = 0x40004000;
2N/A off = 0x4000;
2N/A linux_size = grub_elf64_size (elf, 0, 0);
2N/A if (linux_size == 0)
2N/A return grub_errno;
2N/A
2N/A grub_dprintf ("loader", "Attempting to claim at 0x%lx, size 0x%lx.\n",
2N/A linux_addr, linux_size);
2N/A
2N/A paddr = alloc_phys (linux_size + off);
2N/A if (paddr == (grub_addr_t) -1)
2N/A return grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "couldn't allocate physical memory");
2N/A ret = grub_ieee1275_map (paddr, linux_addr - off,
2N/A linux_size + off, IEEE1275_MAP_DEFAULT);
2N/A if (ret)
2N/A return grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "couldn't map physical memory");
2N/A
2N/A grub_dprintf ("loader", "Loading Linux at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
2N/A linux_addr, paddr, linux_size);
2N/A
2N/A linux_paddr = paddr;
2N/A
2N/A base = linux_entry - off;
2N/A
2N/A /* Now load the segments into the area we claimed. */
2N/A auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
2N/A grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
2N/A {
2N/A if (phdr->p_type != PT_LOAD)
2N/A {
2N/A *do_load = 0;
2N/A return 0;
2N/A }
2N/A *do_load = 1;
2N/A
2N/A /* Adjust the program load address to linux_addr. */
2N/A *addr = (phdr->p_paddr - base) + (linux_addr - off);
2N/A return 0;
2N/A }
2N/A return grub_elf64_load (elf, offset_phdr, 0, 0);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
2N/A int argc, char *argv[])
2N/A{
2N/A grub_file_t file = 0;
2N/A grub_elf_t elf = 0;
2N/A int size;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A if (argc == 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
2N/A goto out;
2N/A }
2N/A
2N/A file = grub_file_open (argv[0]);
2N/A if (!file)
2N/A goto out;
2N/A
2N/A elf = grub_elf_file (file);
2N/A if (! elf)
2N/A goto out;
2N/A
2N/A if (elf->ehdr.ehdr32.e_type != ET_EXEC)
2N/A {
2N/A grub_error (GRUB_ERR_UNKNOWN_OS,
2N/A "this ELF file is not of the right type");
2N/A goto out;
2N/A }
2N/A
2N/A /* Release the previously used memory. */
2N/A grub_loader_unset ();
2N/A
2N/A if (grub_elf_is_elf64 (elf))
2N/A grub_linux_load64 (elf);
2N/A else
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
2N/A goto out;
2N/A }
2N/A
2N/A size = grub_loader_cmdline_size(argc, argv);
2N/A
2N/A linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
2N/A if (! linux_args)
2N/A goto out;
2N/A
2N/A /* Create kernel command line. */
2N/A grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
2N/A grub_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1,
2N/A size);
2N/A
2N/Aout:
2N/A if (elf)
2N/A grub_elf_close (elf);
2N/A else if (file)
2N/A grub_file_close (file);
2N/A
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A {
2N/A grub_linux_release_mem ();
2N/A grub_dl_unref (my_mod);
2N/A loaded = 0;
2N/A }
2N/A else
2N/A {
2N/A grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
2N/A initrd_addr = 0;
2N/A loaded = 1;
2N/A }
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
2N/A int argc, char *argv[])
2N/A{
2N/A grub_file_t *files = 0;
2N/A grub_size_t size = 0;
2N/A grub_addr_t paddr;
2N/A grub_addr_t addr;
2N/A int ret;
2N/A int i;
2N/A int nfiles = 0;
2N/A grub_uint8_t *ptr;
2N/A
2N/A if (argc == 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
2N/A goto fail;
2N/A }
2N/A
2N/A if (!loaded)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
2N/A goto fail;
2N/A }
2N/A
2N/A files = grub_zalloc (argc * sizeof (files[0]));
2N/A if (!files)
2N/A goto fail;
2N/A
2N/A for (i = 0; i < argc; i++)
2N/A {
2N/A grub_file_filter_disable_compression ();
2N/A files[i] = grub_file_open (argv[i]);
2N/A if (! files[i])
2N/A goto fail;
2N/A nfiles++;
2N/A size += grub_file_size (files[i]);
2N/A }
2N/A
2N/A addr = 0x60000000;
2N/A
2N/A paddr = alloc_phys (size);
2N/A if (paddr == (grub_addr_t) -1)
2N/A {
2N/A grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "couldn't allocate physical memory");
2N/A goto fail;
2N/A }
2N/A ret = grub_ieee1275_map (paddr, addr, size, IEEE1275_MAP_DEFAULT);
2N/A if (ret)
2N/A {
2N/A grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "couldn't map physical memory");
2N/A goto fail;
2N/A }
2N/A
2N/A grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
2N/A addr, paddr, size);
2N/A
2N/A ptr = (void *) addr;
2N/A for (i = 0; i < nfiles; i++)
2N/A {
2N/A grub_ssize_t cursize = grub_file_size (files[i]);
2N/A if (grub_file_read (files[i], ptr, cursize) != cursize)
2N/A {
2N/A if (!grub_errno)
2N/A grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
2N/A argv[i]);
2N/A goto fail;
2N/A }
2N/A ptr += cursize;
2N/A }
2N/A
2N/A initrd_addr = addr;
2N/A initrd_paddr = paddr;
2N/A initrd_size = size;
2N/A
2N/A fail:
2N/A for (i = 0; i < nfiles; i++)
2N/A grub_file_close (files[i]);
2N/A grub_free (files);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic void
2N/Adetermine_phys_base (void)
2N/A{
2N/A auto int NESTED_FUNC_ATTR get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type);
2N/A int NESTED_FUNC_ATTR get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__((unused)), grub_uint32_t type)
2N/A {
2N/A if (type != 1)
2N/A return 0;
2N/A if (addr < phys_base)
2N/A phys_base = addr;
2N/A return 0;
2N/A }
2N/A
2N/A phys_base = ~(grub_uint64_t) 0;
2N/A grub_machine_mmap_iterate (get_physbase);
2N/A}
2N/A
2N/Astatic void
2N/Afetch_translations (void)
2N/A{
2N/A grub_ieee1275_phandle_t node;
2N/A grub_ssize_t actual;
2N/A int i;
2N/A
2N/A if (grub_ieee1275_finddevice ("/virtual-memory", &node))
2N/A {
2N/A grub_printf ("Cannot find /virtual-memory node.\n");
2N/A return;
2N/A }
2N/A
2N/A if (grub_ieee1275_get_property_length (node, "translations", &actual))
2N/A {
2N/A grub_printf ("Cannot find /virtual-memory/translations size.\n");
2N/A return;
2N/A }
2N/A
2N/A of_trans = grub_malloc (actual);
2N/A if (!of_trans)
2N/A {
2N/A grub_printf ("Cannot allocate translations buffer.\n");
2N/A return;
2N/A }
2N/A
2N/A if (grub_ieee1275_get_property (node, "translations", of_trans, actual, &actual))
2N/A {
2N/A grub_printf ("Cannot fetch /virtual-memory/translations property.\n");
2N/A return;
2N/A }
2N/A
2N/A of_num_trans = actual / sizeof(struct grub_ieee1275_translation);
2N/A
2N/A for (i = 0; i < of_num_trans; i++)
2N/A {
2N/A struct grub_ieee1275_translation *p = &of_trans[i];
2N/A
2N/A if (p->vaddr == 0x2000)
2N/A {
2N/A grub_addr_t phys, tte = p->data;
2N/A
2N/A phys = tte & ~(0xff00000000001fffULL);
2N/A
2N/A grub_phys_start = phys;
2N/A grub_phys_end = grub_phys_start + p->size;
2N/A grub_dprintf ("loader", "Grub lives at phys_start[%lx] phys_end[%lx]\n",
2N/A (unsigned long) grub_phys_start,
2N/A (unsigned long) grub_phys_end);
2N/A break;
2N/A }
2N/A }
2N/A}
2N/A
2N/A
2N/Astatic grub_command_t cmd_linux, cmd_initrd;
2N/A
2N/AGRUB_MOD_INIT(linux)
2N/A{
2N/A determine_phys_base ();
2N/A fetch_translations ();
2N/A
2N/A cmd_linux = grub_register_command ("linux", grub_cmd_linux,
2N/A 0, N_("Load Linux."));
2N/A cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
2N/A 0, N_("Load initrd."));
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(linux)
2N/A{
2N/A grub_unregister_command (cmd_linux);
2N/A grub_unregister_command (cmd_initrd);
2N/A}