dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0091-Add-support-for-linuxefi.patch

4fe85b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4fe85b
From: Matthew Garrett <mjg@redhat.com>
4fe85b
Date: Tue, 10 Jul 2012 11:58:52 -0400
4fe85b
Subject: [PATCH] Add support for linuxefi
4fe85b
4fe85b
---
4fe85b
 grub-core/Makefile.core.def       |   8 +
4fe85b
 grub-core/kern/efi/mm.c           |  32 ++++
4fe85b
 grub-core/loader/i386/efi/linux.c | 371 ++++++++++++++++++++++++++++++++++++++
4fe85b
 include/grub/efi/efi.h            |   3 +
4fe85b
 include/grub/i386/linux.h         |   1 +
4fe85b
 5 files changed, 415 insertions(+)
4fe85b
 create mode 100644 grub-core/loader/i386/efi/linux.c
4fe85b
4fe85b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
4fe85b
index 42443bc00b9..ec46506e39e 100644
4fe85b
--- a/grub-core/Makefile.core.def
4fe85b
+++ b/grub-core/Makefile.core.def
4fe85b
@@ -1705,6 +1705,14 @@ module = {
4fe85b
   enable = x86_64_efi;
4fe85b
 };
4fe85b
 
4fe85b
+module = {
4fe85b
+  name = linuxefi;
4fe85b
+  efi = loader/i386/efi/linux.c;
4fe85b
+  efi = lib/cmdline.c;
4fe85b
+  enable = i386_efi;
4fe85b
+  enable = x86_64_efi;
4fe85b
+};
4fe85b
+
4fe85b
 module = {
4fe85b
   name = chain;
4fe85b
   efi = loader/efi/chainloader.c;
4fe85b
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
4fe85b
index be37afd9dc4..ddeca6073f6 100644
4fe85b
--- a/grub-core/kern/efi/mm.c
4fe85b
+++ b/grub-core/kern/efi/mm.c
4fe85b
@@ -49,6 +49,38 @@ static grub_efi_uintn_t finish_desc_size;
4fe85b
 static grub_efi_uint32_t finish_desc_version;
4fe85b
 int grub_efi_is_finished = 0;
4fe85b
 
4fe85b
+/* Allocate pages below a specified address */
4fe85b
+void *
4fe85b
+grub_efi_allocate_pages_max (grub_efi_physical_address_t max,
4fe85b
+			     grub_efi_uintn_t pages)
4fe85b
+{
4fe85b
+  grub_efi_status_t status;
4fe85b
+  grub_efi_boot_services_t *b;
4fe85b
+  grub_efi_physical_address_t address = max;
4fe85b
+
4fe85b
+  if (max > 0xffffffff)
4fe85b
+    return 0;
4fe85b
+
4fe85b
+  b = grub_efi_system_table->boot_services;
4fe85b
+  status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
4fe85b
+
4fe85b
+  if (status != GRUB_EFI_SUCCESS)
4fe85b
+    return 0;
4fe85b
+
4fe85b
+  if (address == 0)
4fe85b
+    {
4fe85b
+      /* Uggh, the address 0 was allocated... This is too annoying,
4fe85b
+	 so reallocate another one.  */
4fe85b
+      address = max;
4fe85b
+      status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA, pages, &address);
4fe85b
+      grub_efi_free_pages (0, pages);
4fe85b
+      if (status != GRUB_EFI_SUCCESS)
4fe85b
+	return 0;
4fe85b
+    }
4fe85b
+
4fe85b
+  return (void *) ((grub_addr_t) address);
4fe85b
+}
4fe85b
+
4fe85b
 /* Allocate pages. Return the pointer to the first of allocated pages.  */
4fe85b
 void *
4fe85b
 grub_efi_allocate_pages (grub_efi_physical_address_t address,
4fe85b
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
4fe85b
new file mode 100644
4fe85b
index 00000000000..b79e6320ba9
4fe85b
--- /dev/null
4fe85b
+++ b/grub-core/loader/i386/efi/linux.c
4fe85b
@@ -0,0 +1,371 @@
4fe85b
+/*
4fe85b
+ *  GRUB  --  GRand Unified Bootloader
4fe85b
+ *  Copyright (C) 2012  Free Software Foundation, Inc.
4fe85b
+ *
4fe85b
+ *  GRUB is free software: you can redistribute it and/or modify
4fe85b
+ *  it under the terms of the GNU General Public License as published by
4fe85b
+ *  the Free Software Foundation, either version 3 of the License, or
4fe85b
+ *  (at your option) any later version.
4fe85b
+ *
4fe85b
+ *  GRUB is distributed in the hope that it will be useful,
4fe85b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4fe85b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4fe85b
+ *  GNU General Public License for more details.
4fe85b
+ *
4fe85b
+ *  You should have received a copy of the GNU General Public License
4fe85b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
4fe85b
+ */
4fe85b
+
4fe85b
+#include <grub/loader.h>
4fe85b
+#include <grub/file.h>
4fe85b
+#include <grub/err.h>
4fe85b
+#include <grub/types.h>
4fe85b
+#include <grub/mm.h>
4fe85b
+#include <grub/cpu/linux.h>
4fe85b
+#include <grub/command.h>
4fe85b
+#include <grub/i18n.h>
4fe85b
+#include <grub/lib/cmdline.h>
4fe85b
+#include <grub/efi/efi.h>
4fe85b
+
4fe85b
+GRUB_MOD_LICENSE ("GPLv3+");
4fe85b
+
4fe85b
+static grub_dl_t my_mod;
4fe85b
+static int loaded;
4fe85b
+static void *kernel_mem;
4fe85b
+static grub_uint64_t kernel_size;
4fe85b
+static grub_uint8_t *initrd_mem;
4fe85b
+static grub_uint32_t handover_offset;
4fe85b
+struct linux_kernel_params *params;
4fe85b
+static char *linux_cmdline;
4fe85b
+
4fe85b
+#define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
4fe85b
+
4fe85b
+#define SHIM_LOCK_GUID \
4fe85b
+  { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }
4fe85b
+
4fe85b
+struct grub_efi_shim_lock
4fe85b
+{
4fe85b
+  grub_efi_status_t (*verify) (void *buffer, grub_uint32_t size);
4fe85b
+};
4fe85b
+typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
4fe85b
+
4fe85b
+static grub_efi_boolean_t
4fe85b
+grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
4fe85b
+{
4fe85b
+  grub_efi_guid_t guid = SHIM_LOCK_GUID;
4fe85b
+  grub_efi_shim_lock_t *shim_lock;
4fe85b
+
4fe85b
+  shim_lock = grub_efi_locate_protocol(&guid, NULL);
4fe85b
+
4fe85b
+  if (!shim_lock)
4fe85b
+    return 1;
4fe85b
+
4fe85b
+  if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
4fe85b
+    return 1;
4fe85b
+
4fe85b
+  return 0;
4fe85b
+}
4fe85b
+
4fe85b
+typedef void(*handover_func)(void *, grub_efi_system_table_t *, struct linux_kernel_params *);
4fe85b
+
4fe85b
+static grub_err_t
4fe85b
+grub_linuxefi_boot (void)
4fe85b
+{
4fe85b
+  handover_func hf;
4fe85b
+  int offset = 0;
4fe85b
+
4fe85b
+#ifdef __x86_64__
4fe85b
+  offset = 512;
4fe85b
+#endif
4fe85b
+
4fe85b
+  hf = (handover_func)((char *)kernel_mem + handover_offset + offset);
4fe85b
+
4fe85b
+  asm volatile ("cli");
4fe85b
+
4fe85b
+  hf (grub_efi_image_handle, grub_efi_system_table, params);
4fe85b
+
4fe85b
+  /* Not reached */
4fe85b
+  return GRUB_ERR_NONE;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_err_t
4fe85b
+grub_linuxefi_unload (void)
4fe85b
+{
4fe85b
+  grub_dl_unref (my_mod);
4fe85b
+  loaded = 0;
4fe85b
+  if (initrd_mem)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(params->ramdisk_size));
4fe85b
+  if (linux_cmdline)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(params->cmdline_size + 1));
4fe85b
+  if (kernel_mem)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
4fe85b
+  if (params)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
4fe85b
+  return GRUB_ERR_NONE;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_err_t
4fe85b
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
4fe85b
+                 int argc, char *argv[])
4fe85b
+{
4fe85b
+  grub_file_t *files = 0;
4fe85b
+  int i, nfiles = 0;
4fe85b
+  grub_size_t size = 0;
4fe85b
+  grub_uint8_t *ptr;
4fe85b
+
4fe85b
+  if (argc == 0)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (!loaded)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  files = grub_zalloc (argc * sizeof (files[0]));
4fe85b
+  if (!files)
4fe85b
+    goto fail;
4fe85b
+
4fe85b
+  for (i = 0; i < argc; i++)
4fe85b
+    {
4fe85b
+      grub_file_filter_disable_compression ();
4fe85b
+      files[i] = grub_file_open (argv[i]);
4fe85b
+      if (! files[i])
4fe85b
+        goto fail;
4fe85b
+      nfiles++;
4fe85b
+      size += ALIGN_UP (grub_file_size (files[i]), 4);
4fe85b
+    }
4fe85b
+
4fe85b
+  initrd_mem = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(size));
4fe85b
+
4fe85b
+  if (!initrd_mem)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  params->ramdisk_size = size;
4fe85b
+  params->ramdisk_image = (grub_uint32_t)(grub_uint64_t) initrd_mem;
4fe85b
+
4fe85b
+  ptr = initrd_mem;
4fe85b
+
4fe85b
+  for (i = 0; i < nfiles; i++)
4fe85b
+    {
4fe85b
+      grub_ssize_t cursize = grub_file_size (files[i]);
4fe85b
+      if (grub_file_read (files[i], ptr, cursize) != cursize)
4fe85b
+        {
4fe85b
+          if (!grub_errno)
4fe85b
+            grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
4fe85b
+                        argv[i]);
4fe85b
+          goto fail;
4fe85b
+        }
4fe85b
+      ptr += cursize;
4fe85b
+      grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
4fe85b
+      ptr += ALIGN_UP_OVERHEAD (cursize, 4);
4fe85b
+    }
4fe85b
+
4fe85b
+  params->ramdisk_size = size;
4fe85b
+
4fe85b
+ fail:
4fe85b
+  for (i = 0; i < nfiles; i++)
4fe85b
+    grub_file_close (files[i]);
4fe85b
+  grub_free (files);
4fe85b
+
4fe85b
+  if (initrd_mem && grub_errno)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)initrd_mem, BYTES_TO_PAGES(size));
4fe85b
+
4fe85b
+  return grub_errno;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_err_t
4fe85b
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
4fe85b
+		int argc, char *argv[])
4fe85b
+{
4fe85b
+  grub_file_t file = 0;
4fe85b
+  struct linux_kernel_header lh;
4fe85b
+  grub_ssize_t len, start, filelen;
4fe85b
+  void *kernel;
4fe85b
+
4fe85b
+  grub_dl_ref (my_mod);
4fe85b
+
4fe85b
+  if (argc == 0)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  file = grub_file_open (argv[0]);
4fe85b
+  if (! file)
4fe85b
+    goto fail;
4fe85b
+
4fe85b
+  filelen = grub_file_size (file);
4fe85b
+
4fe85b
+  kernel = grub_malloc(filelen);
4fe85b
+
4fe85b
+  if (!kernel)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (grub_file_read (file, kernel, filelen) != filelen)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (! grub_linuxefi_secure_validate (kernel, filelen))
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
4fe85b
+      grub_free (kernel);
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  grub_file_seek (file, 0);
4fe85b
+
4fe85b
+  grub_free(kernel);
4fe85b
+
4fe85b
+  params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
4fe85b
+
4fe85b
+  if (! params)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  memset (params, 0, 16384);
4fe85b
+
4fe85b
+  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
4fe85b
+    {
4fe85b
+      if (!grub_errno)
4fe85b
+	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
+		    argv[0]);
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (lh.version < grub_cpu_to_le16 (0x020b))
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (!lh.handover_offset)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI handover"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
4fe85b
+					 BYTES_TO_PAGES(lh.cmdline_size + 1));
4fe85b
+
4fe85b
+  if (!linux_cmdline)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
4fe85b
+  grub_create_loader_cmdline (argc, argv,
4fe85b
+                              linux_cmdline + sizeof (LINUX_IMAGE) - 1,
4fe85b
+			      lh.cmdline_size - (sizeof (LINUX_IMAGE) - 1));
4fe85b
+
4fe85b
+  lh.cmd_line_ptr = (grub_uint32_t)(grub_uint64_t)linux_cmdline;
4fe85b
+
4fe85b
+  handover_offset = lh.handover_offset;
4fe85b
+
4fe85b
+  start = (lh.setup_sects + 1) * 512;
4fe85b
+  len = grub_file_size(file) - start;
4fe85b
+
4fe85b
+  kernel_mem = grub_efi_allocate_pages(lh.pref_address,
4fe85b
+				       BYTES_TO_PAGES(lh.init_size));
4fe85b
+
4fe85b
+  if (!kernel_mem)
4fe85b
+    kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
4fe85b
+					     BYTES_TO_PAGES(lh.init_size));
4fe85b
+
4fe85b
+  if (!kernel_mem)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (grub_file_seek (file, start) == (grub_off_t) -1)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
+		  argv[0]);
4fe85b
+      goto fail;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (grub_file_read (file, kernel_mem, len) != len && !grub_errno)
4fe85b
+    {
4fe85b
+      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
4fe85b
+		  argv[0]);
4fe85b
+    }
4fe85b
+
4fe85b
+  if (grub_errno == GRUB_ERR_NONE)
4fe85b
+    {
4fe85b
+      grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
4fe85b
+      loaded = 1;
4fe85b
+      lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
4fe85b
+    }
4fe85b
+
4fe85b
+  memcpy(params, &lh, 2 * 512);
4fe85b
+
4fe85b
+  params->type_of_loader = 0x21;
4fe85b
+
4fe85b
+ fail:
4fe85b
+
4fe85b
+  if (file)
4fe85b
+    grub_file_close (file);
4fe85b
+
4fe85b
+  if (grub_errno != GRUB_ERR_NONE)
4fe85b
+    {
4fe85b
+      grub_dl_unref (my_mod);
4fe85b
+      loaded = 0;
4fe85b
+    }
4fe85b
+
4fe85b
+  if (linux_cmdline && !loaded)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)linux_cmdline, BYTES_TO_PAGES(lh.cmdline_size + 1));
4fe85b
+
4fe85b
+  if (kernel_mem && !loaded)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)kernel_mem, BYTES_TO_PAGES(kernel_size));
4fe85b
+
4fe85b
+  if (params && !loaded)
4fe85b
+    grub_efi_free_pages((grub_efi_physical_address_t)params, BYTES_TO_PAGES(16384));
4fe85b
+
4fe85b
+  return grub_errno;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_command_t cmd_linux, cmd_initrd;
4fe85b
+
4fe85b
+GRUB_MOD_INIT(linuxefi)
4fe85b
+{
4fe85b
+  cmd_linux =
4fe85b
+    grub_register_command ("linuxefi", grub_cmd_linux,
4fe85b
+                           0, N_("Load Linux."));
4fe85b
+  cmd_initrd =
4fe85b
+    grub_register_command ("initrdefi", grub_cmd_initrd,
4fe85b
+                           0, N_("Load initrd."));
4fe85b
+  my_mod = mod;
4fe85b
+}
4fe85b
+
4fe85b
+GRUB_MOD_FINI(linuxefi)
4fe85b
+{
4fe85b
+  grub_unregister_command (cmd_linux);
4fe85b
+  grub_unregister_command (cmd_initrd);
4fe85b
+}
4fe85b
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
4fe85b
index 489cf9e6da7..9370fd53096 100644
4fe85b
--- a/include/grub/efi/efi.h
4fe85b
+++ b/include/grub/efi/efi.h
4fe85b
@@ -40,6 +40,9 @@ void EXPORT_FUNC(grub_efi_stall) (grub_efi_uintn_t microseconds);
4fe85b
 void *
4fe85b
 EXPORT_FUNC(grub_efi_allocate_pages) (grub_efi_physical_address_t address,
4fe85b
 				      grub_efi_uintn_t pages);
4fe85b
+void *
4fe85b
+EXPORT_FUNC(grub_efi_allocate_pages_max) (grub_efi_physical_address_t max,
4fe85b
+					  grub_efi_uintn_t pages);
4fe85b
 void EXPORT_FUNC(grub_efi_free_pages) (grub_efi_physical_address_t address,
4fe85b
 				       grub_efi_uintn_t pages);
4fe85b
 int
4fe85b
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
4fe85b
index da0ca3b83cd..fc36bdaf367 100644
4fe85b
--- a/include/grub/i386/linux.h
4fe85b
+++ b/include/grub/i386/linux.h
4fe85b
@@ -139,6 +139,7 @@ struct linux_kernel_header
4fe85b
   grub_uint64_t setup_data;
4fe85b
   grub_uint64_t pref_address;
4fe85b
   grub_uint32_t init_size;
4fe85b
+  grub_uint32_t handover_offset;
4fe85b
 } GRUB_PACKED;
4fe85b
 
4fe85b
 /* Boot parameters for Linux based on 2.6.12. This is used by the setup