nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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