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