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

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