2N/A/* chainloader.c - boot another boot loader */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2004,2006,2007,2008 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/* TODO: support load options. */
2N/A
2N/A#include <grub/loader.h>
2N/A#include <grub/file.h>
2N/A#include <grub/err.h>
2N/A#include <grub/device.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/charset.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/types.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/efi/api.h>
2N/A#include <grub/efi/efi.h>
2N/A#include <grub/efi/disk.h>
2N/A#include <grub/command.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/Astatic grub_efi_physical_address_t address;
2N/Astatic grub_efi_uintn_t pages;
2N/Astatic grub_efi_device_path_t *file_path;
2N/Astatic grub_efi_handle_t image_handle;
2N/Astatic grub_efi_char16_t *cmdline;
2N/A
2N/Astatic grub_err_t
2N/Agrub_chainloader_unload (void)
2N/A{
2N/A grub_efi_boot_services_t *b;
2N/A
2N/A b = grub_efi_system_table->boot_services;
2N/A efi_call_1 (b->unload_image, image_handle);
2N/A efi_call_2 (b->free_pages, address, pages);
2N/A
2N/A grub_free (file_path);
2N/A grub_free (cmdline);
2N/A cmdline = 0;
2N/A file_path = 0;
2N/A
2N/A grub_dl_unref (my_mod);
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_chainloader_boot (void)
2N/A{
2N/A grub_efi_boot_services_t *b;
2N/A grub_efi_status_t status;
2N/A grub_efi_uintn_t exit_data_size;
2N/A grub_efi_char16_t *exit_data = NULL;
2N/A
2N/A b = grub_efi_system_table->boot_services;
2N/A status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
2N/A if (status != GRUB_EFI_SUCCESS)
2N/A {
2N/A if (exit_data)
2N/A {
2N/A char *buf;
2N/A
2N/A buf = grub_malloc (exit_data_size * 4 + 1);
2N/A if (buf)
2N/A {
2N/A *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
2N/A exit_data, exit_data_size) = 0;
2N/A
2N/A grub_error (GRUB_ERR_BAD_OS, buf);
2N/A grub_free (buf);
2N/A }
2N/A else
2N/A grub_error (GRUB_ERR_BAD_OS, "unknown error");
2N/A }
2N/A }
2N/A
2N/A if (exit_data)
2N/A efi_call_1 (b->free_pool, exit_data);
2N/A
2N/A grub_loader_unset ();
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic void
2N/Acopy_file_path (grub_efi_file_path_device_path_t *fp,
2N/A const char *str, grub_efi_uint16_t len)
2N/A{
2N/A grub_efi_char16_t *p;
2N/A grub_efi_uint16_t size;
2N/A
2N/A fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
2N/A fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
2N/A size = len * sizeof (grub_efi_char16_t) + sizeof (*fp);
2N/A fp->header.length[0] = (grub_efi_uint8_t) (size & 0xff);
2N/A fp->header.length[1] = (grub_efi_uint8_t) (size >> 8);
2N/A for (p = fp->path_name; len > 0; len--, p++, str++)
2N/A {
2N/A /* FIXME: this assumes that the path is in ASCII. */
2N/A *p = (grub_efi_char16_t) (*str == '/' ? '\\' : *str);
2N/A }
2N/A}
2N/A
2N/Astatic grub_efi_device_path_t *
2N/Amake_file_path (grub_efi_device_path_t *dp, const char *filename)
2N/A{
2N/A char *dir_start;
2N/A char *dir_end;
2N/A grub_size_t size;
2N/A grub_efi_device_path_t *d;
2N/A
2N/A dir_start = grub_strchr (filename, ')');
2N/A if (! dir_start)
2N/A dir_start = (char *) filename;
2N/A else
2N/A dir_start++;
2N/A
2N/A dir_end = grub_strrchr (dir_start, '/');
2N/A if (! dir_end)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
2N/A return 0;
2N/A }
2N/A
2N/A size = 0;
2N/A d = dp;
2N/A while (1)
2N/A {
2N/A size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
2N/A if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
2N/A break;
2N/A d = GRUB_EFI_NEXT_DEVICE_PATH (d);
2N/A }
2N/A
2N/A file_path = grub_malloc (size
2N/A + ((grub_strlen (dir_start) + 1)
2N/A * sizeof (grub_efi_char16_t))
2N/A + sizeof (grub_efi_file_path_device_path_t) * 2);
2N/A if (! file_path)
2N/A return 0;
2N/A
2N/A grub_memcpy (file_path, dp, size);
2N/A
2N/A /* Fill the file path for the directory. */
2N/A d = (grub_efi_device_path_t *) ((char *) file_path
2N/A + ((char *) d - (char *) dp));
2N/A grub_efi_print_device_path (d);
2N/A copy_file_path ((grub_efi_file_path_device_path_t *) d,
2N/A dir_start, dir_end - dir_start);
2N/A
2N/A /* Fill the file path for the file. */
2N/A d = GRUB_EFI_NEXT_DEVICE_PATH (d);
2N/A copy_file_path ((grub_efi_file_path_device_path_t *) d,
2N/A dir_end + 1, grub_strlen (dir_end + 1));
2N/A
2N/A /* Fill the end of device path nodes. */
2N/A d = GRUB_EFI_NEXT_DEVICE_PATH (d);
2N/A d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
2N/A d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
2N/A d->length[0] = sizeof (*d);
2N/A d->length[1] = 0;
2N/A
2N/A return file_path;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
2N/A int argc, char *argv[])
2N/A{
2N/A grub_file_t file = 0;
2N/A grub_ssize_t size;
2N/A grub_efi_status_t status;
2N/A grub_efi_boot_services_t *b;
2N/A grub_efi_handle_t dev_handle = 0;
2N/A grub_device_t dev = 0;
2N/A grub_efi_device_path_t *dp = 0;
2N/A grub_efi_loaded_image_t *loaded_image;
2N/A char *filename;
2N/A
2N/A if (argc == 0)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
2N/A filename = argv[0];
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A /* Initialize some global variables. */
2N/A address = 0;
2N/A image_handle = 0;
2N/A file_path = 0;
2N/A
2N/A b = grub_efi_system_table->boot_services;
2N/A
2N/A file = grub_file_open (filename);
2N/A if (! file)
2N/A goto fail;
2N/A
2N/A /* Get the root device's device path. */
2N/A dev = grub_device_open (0);
2N/A if (! dev)
2N/A goto fail;
2N/A
2N/A if (dev->disk)
2N/A {
2N/A dev_handle = grub_efidisk_get_device_handle (dev->disk);
2N/A if (dev_handle)
2N/A dp = grub_efi_get_device_path (dev_handle);
2N/A }
2N/A
2N/A if (! dev->disk || ! dev_handle || ! dp)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
2N/A goto fail;
2N/A }
2N/A
2N/A file_path = make_file_path (dp, filename);
2N/A if (! file_path)
2N/A goto fail;
2N/A
2N/A grub_printf ("file path: ");
2N/A grub_efi_print_device_path (file_path);
2N/A
2N/A size = grub_file_size (file);
2N/A if (!size)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_OS, "file is empty");
2N/A goto fail;
2N/A }
2N/A pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
2N/A
2N/A status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
2N/A GRUB_EFI_LOADER_CODE,
2N/A pages, &address);
2N/A if (status != GRUB_EFI_SUCCESS)
2N/A {
2N/A grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate %u pages", pages);
2N/A goto fail;
2N/A }
2N/A
2N/A if (grub_file_read (file, (void *) ((grub_addr_t) address), size) != size)
2N/A {
2N/A if (grub_errno == GRUB_ERR_NONE)
2N/A grub_error (GRUB_ERR_BAD_OS, "too small");
2N/A
2N/A goto fail;
2N/A }
2N/A
2N/A status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
2N/A (void *) ((grub_addr_t) address), size,
2N/A &image_handle);
2N/A if (status != GRUB_EFI_SUCCESS)
2N/A {
2N/A if (status == GRUB_EFI_OUT_OF_RESOURCES)
2N/A grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
2N/A else
2N/A grub_error (GRUB_ERR_BAD_OS, "cannot load image");
2N/A
2N/A goto fail;
2N/A }
2N/A
2N/A /* LoadImage does not set a device handler when the image is
2N/A loaded from memory, so it is necessary to set it explicitly here.
2N/A This is a mess. */
2N/A loaded_image = grub_efi_get_loaded_image (image_handle);
2N/A if (! loaded_image)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
2N/A goto fail;
2N/A }
2N/A loaded_image->device_handle = dev_handle;
2N/A
2N/A grub_file_close (file);
2N/A
2N/A if (argc > 1)
2N/A {
2N/A int i, len;
2N/A grub_efi_char16_t *p16;
2N/A
2N/A for (i = 1, len = 0; i < argc; i++)
2N/A len += grub_strlen (argv[i]) + 1;
2N/A
2N/A len *= sizeof (grub_efi_char16_t);
2N/A cmdline = p16 = grub_malloc (len);
2N/A if (! cmdline)
2N/A goto fail;
2N/A
2N/A for (i = 1; i < argc; i++)
2N/A {
2N/A char *p8;
2N/A
2N/A p8 = argv[i];
2N/A while (*p8)
2N/A *(p16++) = *(p8++);
2N/A
2N/A *(p16++) = ' ';
2N/A }
2N/A *(--p16) = 0;
2N/A
2N/A loaded_image->load_options = cmdline;
2N/A loaded_image->load_options_size = len;
2N/A }
2N/A
2N/A grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
2N/A return 0;
2N/A
2N/A fail:
2N/A
2N/A if (dev)
2N/A grub_device_close (dev);
2N/A
2N/A if (file)
2N/A grub_file_close (file);
2N/A
2N/A grub_free (file_path);
2N/A
2N/A if (address)
2N/A efi_call_2 (b->free_pages, address, pages);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_command_t cmd;
2N/A
2N/AGRUB_MOD_INIT(chainloader)
2N/A{
2N/A cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
2N/A 0, N_("Load another boot loader."));
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(chainloader)
2N/A{
2N/A grub_unregister_command (cmd);
2N/A}