nalika / rpms / grub2

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