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

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