grub/loader/powerpc/ieee1275/linux.c

345 lines
8.8 KiB
C
Raw Normal View History

/* linux.c - boot Linux */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <grub/elf.h>
#include <grub/elfload.h>
#include <grub/loader.h>
#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/rescue.h>
#include <grub/misc.h>
#include <grub/ieee1275/ieee1275.h>
#include <grub/machine/loader.h>
#define ELF32_LOADMASK (0xc0000000UL)
#define ELF64_LOADMASK (0xc000000000000000ULL)
static grub_dl_t my_mod;
static int loaded;
static grub_addr_t initrd_addr;
static grub_size_t initrd_size;
static grub_addr_t linux_addr;
static grub_size_t linux_size;
static char *linux_args;
typedef void (*kernel_entry_t) (void *, unsigned long, int (void *),
unsigned long, unsigned long);
static grub_err_t
grub_linux_boot (void)
{
kernel_entry_t linuxmain;
grub_ssize_t actual;
/* Set the command line arguments. */
grub_ieee1275_set_property (grub_ieee1275_chosen, "bootargs", linux_args,
grub_strlen (linux_args) + 1, &actual);
grub_dprintf ("loader", "Entry point: 0x%x\n", linux_addr);
grub_dprintf ("loader", "Initrd at: 0x%x, size 0x%x\n", initrd_addr,
initrd_size);
grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
grub_dprintf ("loader", "Jumping to Linux...\n");
/* Boot the kernel. */
linuxmain = (kernel_entry_t) linux_addr;
linuxmain ((void *) initrd_addr, initrd_size, grub_ieee1275_entry_fn, 0, 0);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_release_mem (void)
{
grub_free (linux_args);
linux_args = 0;
if (linux_addr && grub_ieee1275_release (linux_addr, linux_size))
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Can not release memory");
if (initrd_addr && grub_ieee1275_release (initrd_addr, initrd_size))
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Can not release memory");
linux_addr = 0;
initrd_addr = 0;
return GRUB_ERR_NONE;
}
static grub_err_t
grub_linux_unload (void)
{
grub_err_t err;
err = grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
return err;
}
static grub_err_t
grub_linux_load32 (grub_elf_t elf)
{
Elf32_Addr entry;
int found_addr = 0;
/* Linux's entry point incorrectly contains a virtual address. */
entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK;
if (entry == 0)
entry = 0x01400000;
linux_size = grub_elf32_size (elf);
if (linux_size == 0)
return grub_errno;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
/* On some systems, firmware occupies the memory we're trying to use.
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
* until we find an open area. */
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
linux_addr, linux_size);
found_addr = grub_claimmap (linux_addr, linux_size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory.");
/* Now load the segments into the area we claimed. */
auto int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr);
int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr)
{
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr;
return 0;
}
return grub_elf32_load (elf, offset_phdr, 0, 0);
}
static grub_err_t
grub_linux_load64 (grub_elf_t elf)
{
Elf64_Addr entry;
int found_addr = 0;
/* Linux's entry point incorrectly contains a virtual address. */
entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK;
if (entry == 0)
entry = 0x01400000;
linux_size = grub_elf64_size (elf);
if (linux_size == 0)
return grub_errno;
/* Pad it; the kernel scribbles over memory beyond its load address. */
linux_size += 0x100000;
/* On some systems, firmware occupies the memory we're trying to use.
* Happily, Linux can be loaded anywhere (it relocates itself). Iterate
* until we find an open area. */
for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
linux_addr, linux_size);
found_addr = grub_claimmap (linux_addr, linux_size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory.");
/* Now load the segments into the area we claimed. */
auto int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr);
int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr)
{
/* Linux's program headers incorrectly contain virtual addresses.
* Translate those to physical, and offset to the area we claimed. */
*addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr;
return 0;
}
return grub_elf64_load (elf, offset_phdr, 0, 0);
}
void
grub_rescue_cmd_linux (int argc, char *argv[])
{
grub_elf_t elf = 0;
int i;
int size;
char *dest;
grub_dl_ref (my_mod);
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
goto out;
}
elf = grub_elf_open (argv[0]);
if (! elf)
goto out;
if (elf->ehdr.ehdr32.e_type != ET_EXEC)
{
grub_error (GRUB_ERR_UNKNOWN_OS,
"This ELF file is not of the right type\n");
goto out;
}
/* Release the previously used memory. */
grub_loader_unset ();
if (grub_elf_is_elf32 (elf))
grub_linux_load32 (elf);
else
if (grub_elf_is_elf64 (elf))
grub_linux_load64 (elf);
else
{
grub_error (GRUB_ERR_BAD_FILE_TYPE, "Unknown ELF class");
goto out;
}
size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
for (i = 0; i < argc; i++)
size += grub_strlen (argv[i]) + 1;
linux_args = grub_malloc (size);
if (! linux_args)
goto out;
/* Specify the boot file. */
dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
dest = grub_stpcpy (dest, argv[0]);
for (i = 1; i < argc; i++)
{
*dest++ = ' ';
dest = grub_stpcpy (dest, argv[i]);
}
out:
if (elf)
grub_elf_close (elf);
if (grub_errno != GRUB_ERR_NONE)
{
grub_linux_release_mem ();
grub_dl_unref (my_mod);
loaded = 0;
}
else
{
2006-04-30 Yoshinori K. Okuji <okuji@enbug.org> Extend the loader so that GRUB can accept a loader which comes back to GRUB when a loaded image exits. Also, this change adds support for a chainloader on EFI. * term/efi/console.c: Include grub/misc.h. (grub_console_checkkey): Display a scan code on the top for debugging. This will be removed once the EFI port gets stable. Correct the scan code mapping. * kern/efi/mm.c (sort_memory_map): Sort in a descending order to allocate memory from larger regions, in order to reduce the number of allocated regions. Otherwise, the MacOSX loader panics. (filter_memory_map): Avoid less than 1MB for compatibility with other loaders. (add_memory_regions): Allocate from the tail of a region, if possible, to avoid allocating a region near to 1MB, for the MacOSX loader. * kern/efi/init.c (grub_efi_set_prefix): Specify GRUB_EFI_IMAGE_HANDLE to grub_efi_get_loaded_image. * kern/efi/efi.c (grub_efi_get_loaded_image): Accept a new argument IMAGE_HANDLE and specify it to get a loaded image. (grub_arch_modules_addr): Specify GRUB_EFI_IMAGE_HANDLE to grub_efi_get_loaded_image. (grub_efi_get_filename): Divide the legnth by the size of grub_efi_char16_t. (grub_efi_get_device_path): New function. (grub_efi_print_device_path): Print End Device Path nodes. Divide the length by the size of grub_efi_char16_t for a file path device path node. * kern/loader.c (grub_loader_noreturn): New variable. (grub_loader_set): Accept a new argument NORETURN. Set GRUB_LOADER_NORETURN to NORETURN. All callers changed. (grub_loader_boot): If GRUB_LOADER_NORETURN is false, do not call grub_machine_fini. * include/grub/efi/efi.h (grub_efi_get_device_path): New prototype. (grub_efi_get_loaded_image): Take an argument to specify an image handle. * include/grub/loader.h (grub_loader_set): Added one more argument NORETURN. * disk/efi/efidisk.c (make_devices): Use grub_efi_get_device_path instead of grub_efi_open_protocol. (grub_efidisk_get_device_name): Likewise. (grub_efidisk_close): Print a newline. (grub_efidisk_get_device_handle): Fixed to use GRUB_EFI_DEVICE_PATH_SUBTYPE instead of GRUB_EFI_DEVICE_PATH_TYPE. * disk/efi/efidisk.c (device_path_guid): Moved to ... * kern/efi/efi.c (device_path_guid): ... here. * conf/i386-efi.rmk (pkgdata_MODULES): Added _chain.mod and chain.mod. (kernel_mod_HEADERS): Added efi/disk.h. (_chain_mod_SOURCES): New variable. (_chain_mod_CFLAGS): Likewise. (_chain_mod_LDFLAGS): Likewise. (chain_mod_SOURCES): Likewise. (chain_mod_CFLAGS): Likewise. (chain_mod_LDFLAGS): Likewise. * DISTLIST: Added include/grub/efi/chainloader.h, loader/efi/chainloader.c and loader/efi/chainloader_normal.c. * include/grub/efi/chainloader.h: New file. * loader/efi/chainloader.c: Likewise. * loader/efi/chainloader_normal.c: Likewise.
2006-04-30 21:09:37 +00:00
grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
initrd_addr = 0;
loaded = 1;
}
}
void
grub_rescue_cmd_initrd (int argc, char *argv[])
{
grub_file_t file = 0;
grub_ssize_t size;
grub_addr_t first_addr;
grub_addr_t addr;
int found_addr = 0;
if (argc == 0)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
goto fail;
}
if (!loaded)
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "You need to load the kernel first.");
goto fail;
}
file = grub_file_open (argv[0]);
if (! file)
goto fail;
first_addr = linux_addr + linux_size;
size = grub_file_size (file);
/* Attempt to claim at a series of addresses until successful in
the same way that grub_rescue_cmd_linux does. */
for (addr = first_addr; addr < first_addr + 200 * 0x100000; addr += 0x100000)
{
grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
addr, size);
found_addr = grub_claimmap (addr, size);
if (found_addr != -1)
break;
}
if (found_addr == -1)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, "Can not claim memory");
goto fail;
}
grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
if (grub_file_read (file, (void *) addr, size) != size)
{
grub_ieee1275_release (addr, size);
grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
goto fail;
}
initrd_addr = addr;
initrd_size = size;
fail:
if (file)
grub_file_close (file);
}
2005-11-13 Marco Gerards <mgerards@xs4all.nl> * geninit.sh: New file. * geninitheader.sh: Likewise. * commands/boot.c (grub_boot_init, grub_boot_fini): Removed. * commands/cat.c (grub_cat_init, grub_cat_fini): Likewise. * commands/cmp.c (grub_cmp_init, grub_cmp_fini): Likewise. * commands/configfile.c (grub_configfile_init) (grub_configfile_fini): Likewise. * commands/default.c (grub_default_init, grub_default_fini): Likewise. * commands/help.c (grub_help_init, grub_help_fini): Likewise. * commands/ls.c (grub_ls_init, grub_ls_fini): Likewise. * commands/search.c (grub_search_init, grub_search_fini): Likewise. * commands/terminal.c (grub_terminal_init, grub_terminal_fini): Likewise. * commands/test.c (grub_test_init, grub_test_fini): Likewise. * commands/timeout.c (grub_timeout_init, grub_timeout_fini): Likewise. * commands/i386/pc/halt.c (grub_halt_init, grub_halt_fini): Likewise. * commands/iee1275/halt.c (grub_halt_init, grub_halt_fini): Likewise. * commands/i386/pc/reboot.c (grub_reboot_init, grub_reboot_fini): Likewise. * commands/iee1275/reboot.c (grub_reboot_init, grub_reboot_fini): Likewise. * disk/loopback.c (grub_loop_init, grub_loop_fini): Likewise. * fs/affs.c (grub_affs_init, grub_affs_fini): Likewise. * fs/ext2.c (grub_ext2_init, grub_ext2_fini): Likewise. * fs/fat.c (grub_fat_init, grub_fat_fini): Likewise. * fs/hfs.c (grub_hfs_init, grub_hfs_fini): Likewise. * fs/iso9660.c (grub_iso9660_init, grub_iso9660_fini): Likewise. * fs/jfs.c (grub_jfs_init, grub_jfs_fini): Likewise. * fs/minix.c (grub_minix_init, grub_minix_fini): Likewise. * fs/sfs.c (grub_sfs_init, grub_sfs_fini): Likewise. * fs/ufs.c (grub_ufs_init, grub_ufs_fini): Likewise. * fs/xfs.c (grub_xfs_init, grub_xfs_fini): Likewise. * normal/main.c (grub_normal_init, grub_normal_fini): Likewise. * partmap/amiga.c (grub_amiga_partition_map_init) (grub_amiga_partition_map_fini): Likewise. * partmap/apple.c (grub_apple_partition_map_init) (grub_apple_partition_map_fini): Likewise. * partmap/pc.c (grub_pc_partition_map_init) (grub_pc_partition_map_fini): Likewise. * partmap/sun.c (grub_sun_partition_map_init, grub_sun_partition_map_fini): Likewise. * term/terminfo.c (grub_terminal_init, grub_terminal_fini): Likewise. * util/grub-emu.c: Include <grub_modules_init.h>. (main): Don't initialize and de-initialize any modules directly, use `grub_init_all' and `grub_fini_all' instead. * term/i386/pc/vesafb.c (grub_vesafb_init): Renamed to `grub_vesafb_mod_init'. (grub_vesafb_fini): Renamed to `grub_vesafb_mod_fini'. Updated all users. * term/i386/pc/vga.c (grub_vga_init): Renamed to `grub_vga_mod_init'. Updated all users. (grub_vga_fini): Renamed to `grub_vga_mod_fini'. * conf/i386-pc.rmk (grub_emu_SOURCES): Add `grub_emu_init.c'. (grub_modules_init.lst, grub_modules_init.h, grub_emu_init.c): New rules. * include/grub/dl.h (GRUB_MOD_INIT): Add argument `name'. Generate a function to initialize the module in utilities. Updated all callers. (GRUB_MOD_FINI): Add argument `name'. Generate a function to initialize the module in utilities. Updated all callers.
2005-11-13 15:47:09 +00:00
GRUB_MOD_INIT(linux)
{
grub_rescue_register_command ("linux", grub_rescue_cmd_linux,
"load a linux kernel");
grub_rescue_register_command ("initrd", grub_rescue_cmd_initrd,
"load an initrd");
my_mod = mod;
}
2005-11-13 Marco Gerards <mgerards@xs4all.nl> * geninit.sh: New file. * geninitheader.sh: Likewise. * commands/boot.c (grub_boot_init, grub_boot_fini): Removed. * commands/cat.c (grub_cat_init, grub_cat_fini): Likewise. * commands/cmp.c (grub_cmp_init, grub_cmp_fini): Likewise. * commands/configfile.c (grub_configfile_init) (grub_configfile_fini): Likewise. * commands/default.c (grub_default_init, grub_default_fini): Likewise. * commands/help.c (grub_help_init, grub_help_fini): Likewise. * commands/ls.c (grub_ls_init, grub_ls_fini): Likewise. * commands/search.c (grub_search_init, grub_search_fini): Likewise. * commands/terminal.c (grub_terminal_init, grub_terminal_fini): Likewise. * commands/test.c (grub_test_init, grub_test_fini): Likewise. * commands/timeout.c (grub_timeout_init, grub_timeout_fini): Likewise. * commands/i386/pc/halt.c (grub_halt_init, grub_halt_fini): Likewise. * commands/iee1275/halt.c (grub_halt_init, grub_halt_fini): Likewise. * commands/i386/pc/reboot.c (grub_reboot_init, grub_reboot_fini): Likewise. * commands/iee1275/reboot.c (grub_reboot_init, grub_reboot_fini): Likewise. * disk/loopback.c (grub_loop_init, grub_loop_fini): Likewise. * fs/affs.c (grub_affs_init, grub_affs_fini): Likewise. * fs/ext2.c (grub_ext2_init, grub_ext2_fini): Likewise. * fs/fat.c (grub_fat_init, grub_fat_fini): Likewise. * fs/hfs.c (grub_hfs_init, grub_hfs_fini): Likewise. * fs/iso9660.c (grub_iso9660_init, grub_iso9660_fini): Likewise. * fs/jfs.c (grub_jfs_init, grub_jfs_fini): Likewise. * fs/minix.c (grub_minix_init, grub_minix_fini): Likewise. * fs/sfs.c (grub_sfs_init, grub_sfs_fini): Likewise. * fs/ufs.c (grub_ufs_init, grub_ufs_fini): Likewise. * fs/xfs.c (grub_xfs_init, grub_xfs_fini): Likewise. * normal/main.c (grub_normal_init, grub_normal_fini): Likewise. * partmap/amiga.c (grub_amiga_partition_map_init) (grub_amiga_partition_map_fini): Likewise. * partmap/apple.c (grub_apple_partition_map_init) (grub_apple_partition_map_fini): Likewise. * partmap/pc.c (grub_pc_partition_map_init) (grub_pc_partition_map_fini): Likewise. * partmap/sun.c (grub_sun_partition_map_init, grub_sun_partition_map_fini): Likewise. * term/terminfo.c (grub_terminal_init, grub_terminal_fini): Likewise. * util/grub-emu.c: Include <grub_modules_init.h>. (main): Don't initialize and de-initialize any modules directly, use `grub_init_all' and `grub_fini_all' instead. * term/i386/pc/vesafb.c (grub_vesafb_init): Renamed to `grub_vesafb_mod_init'. (grub_vesafb_fini): Renamed to `grub_vesafb_mod_fini'. Updated all users. * term/i386/pc/vga.c (grub_vga_init): Renamed to `grub_vga_mod_init'. Updated all users. (grub_vga_fini): Renamed to `grub_vga_mod_fini'. * conf/i386-pc.rmk (grub_emu_SOURCES): Add `grub_emu_init.c'. (grub_modules_init.lst, grub_modules_init.h, grub_emu_init.c): New rules. * include/grub/dl.h (GRUB_MOD_INIT): Add argument `name'. Generate a function to initialize the module in utilities. Updated all callers. (GRUB_MOD_FINI): Add argument `name'. Generate a function to initialize the module in utilities. Updated all callers.
2005-11-13 15:47:09 +00:00
GRUB_MOD_FINI(linux)
{
grub_rescue_unregister_command ("linux");
grub_rescue_unregister_command ("initrd");
}