Blame SOURCES/0007-Add-secureboot-support-on-efi-chainloader.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Raymund Will <rw@suse.com>
5593c8
Date: Mon, 8 Jul 2019 11:55:18 +0200
5593c8
Subject: [PATCH] Add secureboot support on efi chainloader
5593c8
5593c8
Expand the chainloader to be able to verify the image by means of shim
5593c8
lock protocol. The PE/COFF image is loaded and relocated by the
5593c8
chainloader instead of calling LoadImage and StartImage UEFI boot
5593c8
Service as they require positive verification result from keys enrolled
5593c8
in KEK or DB. The shim will use MOK in addition to firmware enrolled
5593c8
keys to verify the image.
5593c8
5593c8
The chainloader module could be used to load other UEFI bootloaders,
5593c8
such as xen.efi, and could be signed by any of MOK, KEK or DB.
5593c8
5593c8
Based on https://build.opensuse.org/package/view_file/openSUSE:Factory/grub2/grub2-secureboot-chainloader.patch
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
5593c8
Also:
5593c8
5593c8
commit cd7a8984d4fda905877b5bfe466339100156b3bc
5593c8
Author: Raymund Will <rw@suse.com>
5593c8
Date:   Fri Apr 10 01:45:02 2015 -0400
5593c8
5593c8
Use device part of chainloader target, if present.
5593c8
5593c8
Otherwise chainloading is restricted to '$root', which might not even
5593c8
be readable by EFI!
5593c8
5593c8
v1. use grub_file_get_device_name() to get device name
5593c8
5593c8
Signed-off-by: Michael Chang <mchang@suse.com>
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
5593c8
Also:
5593c8
5593c8
commit 0872a2310a0eeac4ecfe9e1b49dd2d72ab373039
5593c8
Author: Peter Jones <pjones@redhat.com>
5593c8
Date:   Fri Jun 10 14:06:15 2016 -0400
5593c8
5593c8
Rework even more of efi chainload so non-sb cases work right.
5593c8
5593c8
This ensures that if shim protocol is not loaded, or is loaded but shim
5593c8
is disabled, we will fall back to a correct load method for the efi
5593c8
chain loader.
5593c8
5593c8
Here's what I tested with this version:
5593c8
5593c8
results                             expected    actual
5593c8
------------------------------------------------------------
5593c8
sb + enabled + shim + fedora        success     success
5593c8
sb + enabled + shim + win           success     success
5593c8
sb + enabled + grub + fedora        fail        fail
5593c8
sb + enabled + grub + win           fail        fail
5593c8
5593c8
sb + mokdisabled + shim + fedora    success     success
5593c8
sb + mokdisabled + shim + win       success     success
5593c8
sb + mokdisabled + grub + fedora    fail        fail
5593c8
sb + mokdisabled + grub + win       fail        fail
5593c8
5593c8
sb disabled + shim + fedora         success     success*
5593c8
sb disabled + shim + win            success     success*
5593c8
sb disabled + grub + fedora         success     success
5593c8
sb disabled + grub + win            success     success
5593c8
5593c8
nosb + shim + fedora                success     success*
5593c8
nosb + shim + win                   success     success*
5593c8
nosb + grub + fedora                success     success
5593c8
nosb + grub + win                   success     success
5593c8
5593c8
* for some reason shim protocol is being installed in these cases, and I
5593c8
  can't see why, but I think it may be this firmware build returning an
5593c8
  erroneous value.  But this effectively falls back to the mokdisabled
5593c8
  behavior, which works correctly, and the presence of the "grub" (i.e.
5593c8
  no shim) tests effectively tests the desired behavior here.
5593c8
5593c8
Resolves: rhbz#1344512
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
5593c8
Also:
5593c8
5593c8
commit ff7b1cb7f69487870211aeb69ff4f54470fbcb58
5593c8
Author: Laszlo Ersek <lersek@redhat.com>
5593c8
Date:   Mon Nov 21 15:34:00 2016 +0100
5593c8
5593c8
efi/chainloader: fix wrong sanity check in relocate_coff()
5593c8
5593c8
In relocate_coff(), the relocation entries are parsed from the original
5593c8
image (not the section-wise copied image). The original image is
5593c8
pointed-to by the "orig" pointer. The current check
5593c8
5593c8
  (void *)reloc_end < data
5593c8
5593c8
compares the addresses of independent memory allocations. "data" is a typo
5593c8
here, it should be "orig".
5593c8
5593c8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1347291
5593c8
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
5593c8
Tested-by: Bogdan Costescu <bcostescu@gmail.com>
5593c8
Tested-by: Juan Orti <j.orti.alcaine@gmail.com>
5593c8
5593c8
Also:
5593c8
5593c8
commit ab4ba9997ad4832449e54d930fa2aac6a160d0e9
5593c8
Author: Laszlo Ersek <lersek@redhat.com>
5593c8
Date:   Wed Nov 23 06:27:09 2016 +0100
5593c8
5593c8
efi/chainloader: truncate overlong relocation section
5593c8
5593c8
The UEFI Windows 7 boot loader ("EFI/Microsoft/Boot/bootmgfw.efi", SHA1
5593c8
31b410e029bba87d2068c65a80b88882f9f8ea25) has inconsistent headers.
5593c8
5593c8
Compare:
5593c8
5593c8
> The Data Directory
5593c8
> ...
5593c8
> Entry 5 00000000000d9000 00000574 Base Relocation Directory [.reloc]
5593c8
5593c8
Versus:
5593c8
5593c8
> Sections:
5593c8
> Idx Name      Size      VMA               LMA               File off ...
5593c8
> ...
5593c8
>  10 .reloc    00000e22  00000000100d9000  00000000100d9000  000a1800 ...
5593c8
5593c8
That is, the size reported by the RelocDir entry (0x574) is smaller than
5593c8
the virtual size of the .reloc section (0xe22).
5593c8
5593c8
Quoting the grub2 debug log for the same:
5593c8
5593c8
> chainloader.c:595: reloc_dir: 0xd9000 reloc_size: 0x00000574
5593c8
> chainloader.c:603: reloc_base: 0x7d208000 reloc_base_end: 0x7d208573
5593c8
> ...
5593c8
> chainloader.c:620: Section 10 ".reloc" at 0x7d208000..0x7d208e21
5593c8
> chainloader.c:661:  section is not reloc section?
5593c8
> chainloader.c:663:  rds: 0x00001000, vs: 00000e22
5593c8
> chainloader.c:664:  base: 0x7d208000 end: 0x7d208e21
5593c8
> chainloader.c:666:  reloc_base: 0x7d208000 reloc_base_end: 0x7d208573
5593c8
> chainloader.c:671:  Section characteristics are 42000040
5593c8
> chainloader.c:673:  Section virtual size: 00000e22
5593c8
> chainloader.c:675:  Section raw_data size: 00001000
5593c8
> chainloader.c:678:  Discarding section
5593c8
5593c8
After hexdumping "bootmgfw.efi" and manually walking its relocation blocks
5593c8
(yes, really), I determined that the (smaller) RelocDir value is correct.
5593c8
The remaining area that extends up to the .reloc section size (== 0xe22 -
5593c8
0x574 == 0x8ae bytes) exists as zero padding in the file.
5593c8
5593c8
This zero padding shouldn't be passed to relocate_coff() for parsing. In
5593c8
order to cope with it, split the handling of .reloc sections into the
5593c8
following branches:
5593c8
5593c8
- original case (equal size): original behavior (--> relocation
5593c8
  attempted),
5593c8
5593c8
- overlong .reloc section (longer than reported by RelocDir): truncate the
5593c8
  section to the RelocDir size for the purposes of relocate_coff(), and
5593c8
  attempt relocation,
5593c8
5593c8
- .reloc section is too short, or other checks fail: original behavior
5593c8
  (--> relocation not attempted).
5593c8
5593c8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1347291
5593c8
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
5593c8
---
5593c8
 grub-core/kern/efi/efi.c           |  14 +-
5593c8
 grub-core/loader/arm64/linux.c     |   4 +-
5593c8
 grub-core/loader/efi/chainloader.c | 816 +++++++++++++++++++++++++++++++++----
5593c8
 grub-core/loader/efi/linux.c       |  25 +-
5593c8
 grub-core/loader/i386/efi/linux.c  |  17 +-
5593c8
 include/grub/efi/linux.h           |   2 +-
5593c8
 include/grub/efi/pe32.h            |  52 ++-
5593c8
 7 files changed, 840 insertions(+), 90 deletions(-)
5593c8
5593c8
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
5593c8
index 35b8f670602..4a2259aa1c7 100644
5593c8
--- a/grub-core/kern/efi/efi.c
5593c8
+++ b/grub-core/kern/efi/efi.c
5593c8
@@ -296,14 +296,20 @@ grub_efi_secure_boot (void)
5593c8
   grub_efi_boolean_t ret = 0;
5593c8
 
5593c8
   secure_boot = grub_efi_get_variable("SecureBoot", &efi_var_guid, &datasize);
5593c8
-
5593c8
   if (datasize != 1 || !secure_boot)
5593c8
-    goto out;
5593c8
+    {
5593c8
+      grub_dprintf ("secureboot", "No SecureBoot variable\n");
5593c8
+      goto out;
5593c8
+    }
5593c8
+  grub_dprintf ("secureboot", "SecureBoot: %d\n", *secure_boot);
5593c8
 
5593c8
   setup_mode = grub_efi_get_variable("SetupMode", &efi_var_guid, &datasize);
5593c8
-
5593c8
   if (datasize != 1 || !setup_mode)
5593c8
-    goto out;
5593c8
+    {
5593c8
+      grub_dprintf ("secureboot", "No SetupMode variable\n");
5593c8
+      goto out;
5593c8
+    }
5593c8
+  grub_dprintf ("secureboot", "SetupMode: %d\n", *setup_mode);
5593c8
 
5593c8
   if (*secure_boot && !*setup_mode)
5593c8
     ret = 1;
5593c8
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c
5593c8
index a312c668685..04994d5c67d 100644
5593c8
--- a/grub-core/loader/arm64/linux.c
5593c8
+++ b/grub-core/loader/arm64/linux.c
5593c8
@@ -284,6 +284,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
   struct linux_arch_kernel_header lh;
5593c8
   struct grub_armxx_linux_pe_header *pe;
5593c8
   grub_err_t err;
5593c8
+  int rc;
5593c8
 
5593c8
   grub_dl_ref (my_mod);
5593c8
 
5593c8
@@ -328,7 +329,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
 
5593c8
   grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
5593c8
 
5593c8
-  if (!grub_linuxefi_secure_validate (kernel_addr, kernel_size))
5593c8
+  rc = grub_linuxefi_secure_validate (kernel_addr, kernel_size);
5593c8
+  if (rc < 0)
5593c8
     {
5593c8
       grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"), argv[0]);
5593c8
       goto fail;
5593c8
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
5593c8
index 2bd80f4db3d..b54cf6986fc 100644
5593c8
--- a/grub-core/loader/efi/chainloader.c
5593c8
+++ b/grub-core/loader/efi/chainloader.c
5593c8
@@ -32,6 +32,8 @@
5593c8
 #include <grub/efi/api.h>
5593c8
 #include <grub/efi/efi.h>
5593c8
 #include <grub/efi/disk.h>
5593c8
+#include <grub/efi/pe32.h>
5593c8
+#include <grub/efi/linux.h>
5593c8
 #include <grub/command.h>
5593c8
 #include <grub/i18n.h>
5593c8
 #include <grub/net.h>
5593c8
@@ -46,9 +48,14 @@ static grub_dl_t my_mod;
5593c8
 
5593c8
 static grub_efi_physical_address_t address;
5593c8
 static grub_efi_uintn_t pages;
5593c8
+static grub_ssize_t fsize;
5593c8
 static grub_efi_device_path_t *file_path;
5593c8
 static grub_efi_handle_t image_handle;
5593c8
 static grub_efi_char16_t *cmdline;
5593c8
+static grub_ssize_t cmdline_len;
5593c8
+static grub_efi_handle_t dev_handle;
5593c8
+
5593c8
+static grub_efi_status_t (*entry_point) (grub_efi_handle_t image_handle, grub_efi_system_table_t *system_table);
5593c8
 
5593c8
 static grub_err_t
5593c8
 grub_chainloader_unload (void)
5593c8
@@ -63,6 +70,7 @@ grub_chainloader_unload (void)
5593c8
   grub_free (cmdline);
5593c8
   cmdline = 0;
5593c8
   file_path = 0;
5593c8
+  dev_handle = 0;
5593c8
 
5593c8
   grub_dl_unref (my_mod);
5593c8
   return GRUB_ERR_NONE;
5593c8
@@ -213,20 +221,690 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
5593c8
   return file_path;
5593c8
 }
5593c8
 
5593c8
+#define SHIM_LOCK_GUID \
5593c8
+  { 0x605dab50, 0xe046, 0x4300, { 0xab,0xb6,0x3d,0xd8,0x10,0xdd,0x8b,0x23 } }
5593c8
+
5593c8
+typedef union
5593c8
+{
5593c8
+  struct grub_pe32_header_32 pe32;
5593c8
+  struct grub_pe32_header_64 pe32plus;
5593c8
+} grub_pe_header_t;
5593c8
+
5593c8
+struct pe_coff_loader_image_context
5593c8
+{
5593c8
+  grub_efi_uint64_t image_address;
5593c8
+  grub_efi_uint64_t image_size;
5593c8
+  grub_efi_uint64_t entry_point;
5593c8
+  grub_efi_uintn_t size_of_headers;
5593c8
+  grub_efi_uint16_t image_type;
5593c8
+  grub_efi_uint16_t number_of_sections;
5593c8
+  grub_efi_uint32_t section_alignment;
5593c8
+  struct grub_pe32_section_table *first_section;
5593c8
+  struct grub_pe32_data_directory *reloc_dir;
5593c8
+  struct grub_pe32_data_directory *sec_dir;
5593c8
+  grub_efi_uint64_t number_of_rva_and_sizes;
5593c8
+  grub_pe_header_t *pe_hdr;
5593c8
+};
5593c8
+
5593c8
+typedef struct pe_coff_loader_image_context pe_coff_loader_image_context_t;
5593c8
+
5593c8
+struct grub_efi_shim_lock
5593c8
+{
5593c8
+  grub_efi_status_t (*verify)(void *buffer,
5593c8
+                              grub_efi_uint32_t size);
5593c8
+  grub_efi_status_t (*hash)(void *data,
5593c8
+                            grub_efi_int32_t datasize,
5593c8
+                            pe_coff_loader_image_context_t *context,
5593c8
+                            grub_efi_uint8_t *sha256hash,
5593c8
+                            grub_efi_uint8_t *sha1hash);
5593c8
+  grub_efi_status_t (*context)(void *data,
5593c8
+                               grub_efi_uint32_t size,
5593c8
+                               pe_coff_loader_image_context_t *context);
5593c8
+};
5593c8
+
5593c8
+typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
5593c8
+
5593c8
+static grub_efi_boolean_t
5593c8
+read_header (void *data, grub_efi_uint32_t size,
5593c8
+	     pe_coff_loader_image_context_t *context)
5593c8
+{
5593c8
+  grub_efi_guid_t guid = SHIM_LOCK_GUID;
5593c8
+  grub_efi_shim_lock_t *shim_lock;
5593c8
+  grub_efi_status_t status;
5593c8
+
5593c8
+  shim_lock = grub_efi_locate_protocol (&guid, NULL);
5593c8
+  if (!shim_lock)
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "no shim lock protocol");
5593c8
+      return 0;
5593c8
+    }
5593c8
+
5593c8
+  status = shim_lock->context (data, size, context);
5593c8
+
5593c8
+  if (status == GRUB_EFI_SUCCESS)
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "context success\n");
5593c8
+      return 1;
5593c8
+    }
5593c8
+
5593c8
+  switch (status)
5593c8
+    {
5593c8
+      case GRUB_EFI_UNSUPPORTED:
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error unsupported");
5593c8
+      break;
5593c8
+      case GRUB_EFI_INVALID_PARAMETER:
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error invalid parameter");
5593c8
+      break;
5593c8
+      default:
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "context error code");
5593c8
+      break;
5593c8
+    }
5593c8
+
5593c8
+  return -1;
5593c8
+}
5593c8
+
5593c8
+static void*
5593c8
+image_address (void *image, grub_efi_uint64_t sz, grub_efi_uint64_t adr)
5593c8
+{
5593c8
+  if (adr > sz)
5593c8
+    return NULL;
5593c8
+
5593c8
+  return ((grub_uint8_t*)image + adr);
5593c8
+}
5593c8
+
5593c8
+static int
5593c8
+image_is_64_bit (grub_pe_header_t *pe_hdr)
5593c8
+{
5593c8
+  /* .Magic is the same offset in all cases */
5593c8
+  if (pe_hdr->pe32plus.optional_header.magic == GRUB_PE32_PE64_MAGIC)
5593c8
+    return 1;
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static const grub_uint16_t machine_type __attribute__((__unused__)) =
5593c8
+#if defined(__x86_64__)
5593c8
+  GRUB_PE32_MACHINE_X86_64;
5593c8
+#elif defined(__aarch64__)
5593c8
+  GRUB_PE32_MACHINE_ARM64;
5593c8
+#elif defined(__arm__)
5593c8
+  GRUB_PE32_MACHINE_ARMTHUMB_MIXED;
5593c8
+#elif defined(__i386__) || defined(__i486__) || defined(__i686__)
5593c8
+  GRUB_PE32_MACHINE_I386;
5593c8
+#elif defined(__ia64__)
5593c8
+  GRUB_PE32_MACHINE_IA64;
5593c8
+#else
5593c8
+#error this architecture is not supported by grub2
5593c8
+#endif
5593c8
+
5593c8
+static grub_efi_status_t
5593c8
+relocate_coff (pe_coff_loader_image_context_t *context,
5593c8
+	       struct grub_pe32_section_table *section,
5593c8
+	       void *orig, void *data)
5593c8
+{
5593c8
+  struct grub_pe32_data_directory *reloc_base, *reloc_base_end;
5593c8
+  grub_efi_uint64_t adjust;
5593c8
+  struct grub_pe32_fixup_block *reloc, *reloc_end;
5593c8
+  char *fixup, *fixup_base, *fixup_data = NULL;
5593c8
+  grub_efi_uint16_t *fixup_16;
5593c8
+  grub_efi_uint32_t *fixup_32;
5593c8
+  grub_efi_uint64_t *fixup_64;
5593c8
+  grub_efi_uint64_t size = context->image_size;
5593c8
+  void *image_end = (char *)orig + size;
5593c8
+  int n = 0;
5593c8
+
5593c8
+  if (image_is_64_bit (context->pe_hdr))
5593c8
+    context->pe_hdr->pe32plus.optional_header.image_base =
5593c8
+      (grub_uint64_t)(unsigned long)data;
5593c8
+  else
5593c8
+    context->pe_hdr->pe32.optional_header.image_base =
5593c8
+      (grub_uint32_t)(unsigned long)data;
5593c8
+
5593c8
+  /* Alright, so here's how this works:
5593c8
+   *
5593c8
+   * context->reloc_dir gives us two things:
5593c8
+   * - the VA the table of base relocation blocks are (maybe) to be
5593c8
+   *   mapped at (reloc_dir->rva)
5593c8
+   * - the virtual size (reloc_dir->size)
5593c8
+   *
5593c8
+   * The .reloc section (section here) gives us some other things:
5593c8
+   * - the name! kind of. (section->name)
5593c8
+   * - the virtual size (section->virtual_size), which should be the same
5593c8
+   *   as RelocDir->Size
5593c8
+   * - the virtual address (section->virtual_address)
5593c8
+   * - the file section size (section->raw_data_size), which is
5593c8
+   *   a multiple of optional_header->file_alignment.  Only useful for image
5593c8
+   *   validation, not really useful for iteration bounds.
5593c8
+   * - the file address (section->raw_data_offset)
5593c8
+   * - a bunch of stuff we don't use that's 0 in our binaries usually
5593c8
+   * - Flags (section->characteristics)
5593c8
+   *
5593c8
+   * and then the thing that's actually at the file address is an array
5593c8
+   * of struct grub_pe32_fixup_block structs with some values packed behind
5593c8
+   * them.  The block_size field of this structure includes the
5593c8
+   * structure itself, and adding it to that structure's address will
5593c8
+   * yield the next entry in the array.
5593c8
+   */
5593c8
+
5593c8
+  reloc_base = image_address (orig, size, section->raw_data_offset);
5593c8
+  reloc_base_end = image_address (orig, size, section->raw_data_offset
5593c8
+				  + section->virtual_size);
5593c8
+
5593c8
+  grub_dprintf ("chain", "relocate_coff(): reloc_base %p reloc_base_end %p\n",
5593c8
+		reloc_base, reloc_base_end);
5593c8
+
5593c8
+  if (!reloc_base && !reloc_base_end)
5593c8
+    return GRUB_EFI_SUCCESS;
5593c8
+
5593c8
+  if (!reloc_base || !reloc_base_end)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc table overflows binary");
5593c8
+      return GRUB_EFI_UNSUPPORTED;
5593c8
+    }
5593c8
+
5593c8
+  adjust = (grub_uint64_t)(grub_efi_uintn_t)data - context->image_address;
5593c8
+  if (adjust == 0)
5593c8
+    return GRUB_EFI_SUCCESS;
5593c8
+
5593c8
+  while (reloc_base < reloc_base_end)
5593c8
+    {
5593c8
+      grub_uint16_t *entry;
5593c8
+      reloc = (struct grub_pe32_fixup_block *)((char*)reloc_base);
5593c8
+
5593c8
+      if ((reloc_base->size == 0) ||
5593c8
+	  (reloc_base->size > context->reloc_dir->size))
5593c8
+	{
5593c8
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
5593c8
+		      "Reloc %d block size %d is invalid\n", n,
5593c8
+		      reloc_base->size);
5593c8
+	  return GRUB_EFI_UNSUPPORTED;
5593c8
+	}
5593c8
+
5593c8
+      entry = &reloc->entries[0];
5593c8
+      reloc_end = (struct grub_pe32_fixup_block *)
5593c8
+	((char *)reloc_base + reloc_base->size);
5593c8
+
5593c8
+      if ((void *)reloc_end < orig || (void *)reloc_end > image_end)
5593c8
+        {
5593c8
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc entry %d overflows binary",
5593c8
+		      n);
5593c8
+          return GRUB_EFI_UNSUPPORTED;
5593c8
+        }
5593c8
+
5593c8
+      fixup_base = image_address(data, size, reloc_base->rva);
5593c8
+
5593c8
+      if (!fixup_base)
5593c8
+        {
5593c8
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Reloc %d Invalid fixupbase", n);
5593c8
+          return GRUB_EFI_UNSUPPORTED;
5593c8
+        }
5593c8
+
5593c8
+      while ((void *)entry < (void *)reloc_end)
5593c8
+        {
5593c8
+          fixup = fixup_base + (*entry & 0xFFF);
5593c8
+          switch ((*entry) >> 12)
5593c8
+            {
5593c8
+              case GRUB_PE32_REL_BASED_ABSOLUTE:
5593c8
+                break;
5593c8
+              case GRUB_PE32_REL_BASED_HIGH:
5593c8
+                fixup_16 = (grub_uint16_t *)fixup;
5593c8
+                *fixup_16 = (grub_uint16_t)
5593c8
+		  (*fixup_16 + ((grub_uint16_t)((grub_uint32_t)adjust >> 16)));
5593c8
+                if (fixup_data != NULL)
5593c8
+                  {
5593c8
+                    *(grub_uint16_t *) fixup_data = *fixup_16;
5593c8
+                    fixup_data = fixup_data + sizeof (grub_uint16_t);
5593c8
+                  }
5593c8
+                break;
5593c8
+              case GRUB_PE32_REL_BASED_LOW:
5593c8
+                fixup_16 = (grub_uint16_t *)fixup;
5593c8
+                *fixup_16 = (grub_uint16_t) (*fixup_16 + (grub_uint16_t)adjust);
5593c8
+                if (fixup_data != NULL)
5593c8
+                  {
5593c8
+                    *(grub_uint16_t *) fixup_data = *fixup_16;
5593c8
+                    fixup_data = fixup_data + sizeof (grub_uint16_t);
5593c8
+                  }
5593c8
+                break;
5593c8
+              case GRUB_PE32_REL_BASED_HIGHLOW:
5593c8
+                fixup_32 = (grub_uint32_t *)fixup;
5593c8
+                *fixup_32 = *fixup_32 + (grub_uint32_t)adjust;
5593c8
+                if (fixup_data != NULL)
5593c8
+                  {
5593c8
+                    fixup_data = (char *)ALIGN_UP ((grub_addr_t)fixup_data, sizeof (grub_uint32_t));
5593c8
+                    *(grub_uint32_t *) fixup_data = *fixup_32;
5593c8
+                    fixup_data += sizeof (grub_uint32_t);
5593c8
+                  }
5593c8
+                break;
5593c8
+              case GRUB_PE32_REL_BASED_DIR64:
5593c8
+                fixup_64 = (grub_uint64_t *)fixup;
5593c8
+                *fixup_64 = *fixup_64 + (grub_uint64_t)adjust;
5593c8
+                if (fixup_data != NULL)
5593c8
+                  {
5593c8
+                    fixup_data = (char *)ALIGN_UP ((grub_addr_t)fixup_data, sizeof (grub_uint64_t));
5593c8
+                    *(grub_uint64_t *) fixup_data = *fixup_64;
5593c8
+                    fixup_data += sizeof (grub_uint64_t);
5593c8
+                  }
5593c8
+                break;
5593c8
+              default:
5593c8
+                grub_error (GRUB_ERR_BAD_ARGUMENT,
5593c8
+			    "Reloc %d unknown relocation type %d",
5593c8
+			    n, (*entry) >> 12);
5593c8
+                return GRUB_EFI_UNSUPPORTED;
5593c8
+            }
5593c8
+          entry += 1;
5593c8
+        }
5593c8
+      reloc_base = (struct grub_pe32_data_directory *)reloc_end;
5593c8
+      n++;
5593c8
+    }
5593c8
+
5593c8
+  return GRUB_EFI_SUCCESS;
5593c8
+}
5593c8
+
5593c8
+static grub_efi_device_path_t *
5593c8
+grub_efi_get_media_file_path (grub_efi_device_path_t *dp)
5593c8
+{
5593c8
+  while (1)
5593c8
+    {
5593c8
+      grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
5593c8
+      grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
5593c8
+
5593c8
+      if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
5593c8
+        break;
5593c8
+      else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
5593c8
+            && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
5593c8
+      return dp;
5593c8
+
5593c8
+      dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
5593c8
+    }
5593c8
+
5593c8
+    return NULL;
5593c8
+}
5593c8
+
5593c8
+static grub_efi_boolean_t
5593c8
+handle_image (void *data, grub_efi_uint32_t datasize)
5593c8
+{
5593c8
+  grub_efi_boot_services_t *b;
5593c8
+  grub_efi_loaded_image_t *li, li_bak;
5593c8
+  grub_efi_status_t efi_status;
5593c8
+  char *buffer = NULL;
5593c8
+  char *buffer_aligned = NULL;
5593c8
+  grub_efi_uint32_t i;
5593c8
+  struct grub_pe32_section_table *section;
5593c8
+  char *base, *end;
5593c8
+  pe_coff_loader_image_context_t context;
5593c8
+  grub_uint32_t section_alignment;
5593c8
+  grub_uint32_t buffer_size;
5593c8
+  int found_entry_point = 0;
5593c8
+  int rc;
5593c8
+
5593c8
+  b = grub_efi_system_table->boot_services;
5593c8
+
5593c8
+  rc = read_header (data, datasize, &context);
5593c8
+  if (rc < 0)
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "Failed to read header\n");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+  else if (rc == 0)
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "Secure Boot is not enabled\n");
5593c8
+      return 0;
5593c8
+    }
5593c8
+  else
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "Header read without error\n");
5593c8
+    }
5593c8
+
5593c8
+  /*
5593c8
+   * The spec says, uselessly, of SectionAlignment:
5593c8
+   * =====
5593c8
+   * The alignment (in bytes) of sections when they are loaded into
5593c8
+   * memory. It must be greater than or equal to FileAlignment. The
5593c8
+   * default is the page size for the architecture.
5593c8
+   * =====
5593c8
+   * Which doesn't tell you whose responsibility it is to enforce the
5593c8
+   * "default", or when.  It implies that the value in the field must
5593c8
+   * be > FileAlignment (also poorly defined), but it appears visual
5593c8
+   * studio will happily write 512 for FileAlignment (its default) and
5593c8
+   * 0 for SectionAlignment, intending to imply PAGE_SIZE.
5593c8
+   *
5593c8
+   * We only support one page size, so if it's zero, nerf it to 4096.
5593c8
+   */
5593c8
+  section_alignment = context.section_alignment;
5593c8
+  if (section_alignment == 0)
5593c8
+    section_alignment = 4096;
5593c8
+
5593c8
+  buffer_size = context.image_size + section_alignment;
5593c8
+  grub_dprintf ("chain", "image size is %08"PRIxGRUB_UINT64_T", datasize is %08x\n",
5593c8
+	       context.image_size, datasize);
5593c8
+
5593c8
+  efi_status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA,
5593c8
+			   buffer_size, &buffer);
5593c8
+
5593c8
+  if (efi_status != GRUB_EFI_SUCCESS)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  buffer_aligned = (char *)ALIGN_UP ((grub_addr_t)buffer, section_alignment);
5593c8
+  if (!buffer_aligned)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  grub_memcpy (buffer_aligned, data, context.size_of_headers);
5593c8
+
5593c8
+  entry_point = image_address (buffer_aligned, context.image_size,
5593c8
+			       context.entry_point);
5593c8
+
5593c8
+  grub_dprintf ("chain", "entry_point: %p\n", entry_point);
5593c8
+  if (!entry_point)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid entry point");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  char *reloc_base, *reloc_base_end;
5593c8
+  grub_dprintf ("chain", "reloc_dir: %p reloc_size: 0x%08x\n",
5593c8
+		(void *)(unsigned long)context.reloc_dir->rva,
5593c8
+		context.reloc_dir->size);
5593c8
+  reloc_base = image_address (buffer_aligned, context.image_size,
5593c8
+			      context.reloc_dir->rva);
5593c8
+  /* RelocBaseEnd here is the address of the last byte of the table */
5593c8
+  reloc_base_end = image_address (buffer_aligned, context.image_size,
5593c8
+				  context.reloc_dir->rva
5593c8
+				  + context.reloc_dir->size - 1);
5593c8
+  grub_dprintf ("chain", "reloc_base: %p reloc_base_end: %p\n",
5593c8
+		reloc_base, reloc_base_end);
5593c8
+
5593c8
+  struct grub_pe32_section_table *reloc_section = NULL, fake_reloc_section;
5593c8
+
5593c8
+  section = context.first_section;
5593c8
+  for (i = 0; i < context.number_of_sections; i++, section++)
5593c8
+    {
5593c8
+      char name[9];
5593c8
+
5593c8
+      base = image_address (buffer_aligned, context.image_size,
5593c8
+			    section->virtual_address);
5593c8
+      end = image_address (buffer_aligned, context.image_size,
5593c8
+			   section->virtual_address + section->virtual_size -1);
5593c8
+
5593c8
+      grub_strncpy(name, section->name, 9);
5593c8
+      name[8] = '\0';
5593c8
+      grub_dprintf ("chain", "Section %d \"%s\" at %p..%p\n", i,
5593c8
+		   name, base, end);
5593c8
+
5593c8
+      if (end < base)
5593c8
+	{
5593c8
+	  grub_dprintf ("chain", " base is %p but end is %p... bad.\n",
5593c8
+		       base, end);
5593c8
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
5593c8
+		      "Image has invalid negative size");
5593c8
+	  goto error_exit;
5593c8
+	}
5593c8
+
5593c8
+      if (section->virtual_address <= context.entry_point &&
5593c8
+	  (section->virtual_address + section->raw_data_size - 1)
5593c8
+	  > context.entry_point)
5593c8
+	{
5593c8
+	  found_entry_point++;
5593c8
+	  grub_dprintf ("chain", " section contains entry point\n");
5593c8
+	}
5593c8
+
5593c8
+      /* We do want to process .reloc, but it's often marked
5593c8
+       * discardable, so we don't want to memcpy it. */
5593c8
+      if (grub_memcmp (section->name, ".reloc\0\0", 8) == 0)
5593c8
+	{
5593c8
+	  if (reloc_section)
5593c8
+	    {
5593c8
+	      grub_error (GRUB_ERR_BAD_ARGUMENT,
5593c8
+			  "Image has multiple relocation sections");
5593c8
+	      goto error_exit;
5593c8
+	    }
5593c8
+
5593c8
+	  /* If it has nonzero sizes, and our bounds check
5593c8
+	   * made sense, and the VA and size match RelocDir's
5593c8
+	   * versions, then we believe in this section table. */
5593c8
+	  if (section->raw_data_size && section->virtual_size &&
5593c8
+	      base && end && reloc_base == base)
5593c8
+	    {
5593c8
+	      if (reloc_base_end == end)
5593c8
+		{
5593c8
+		  grub_dprintf ("chain", " section is relocation section\n");
5593c8
+		  reloc_section = section;
5593c8
+		}
5593c8
+	      else if (reloc_base_end && reloc_base_end < end)
5593c8
+	        {
5593c8
+		  /* Bogus virtual size in the reloc section -- RelocDir
5593c8
+		   * reported a smaller Base Relocation Directory. Decrease
5593c8
+		   * the section's virtual size so that it equal RelocDir's
5593c8
+		   * idea, but only for the purposes of relocate_coff(). */
5593c8
+		  grub_dprintf ("chain",
5593c8
+				" section is (overlong) relocation section\n");
5593c8
+		  grub_memcpy (&fake_reloc_section, section, sizeof *section);
5593c8
+		  fake_reloc_section.virtual_size -= (end - reloc_base_end);
5593c8
+		  reloc_section = &fake_reloc_section;
5593c8
+		}
5593c8
+	    }
5593c8
+
5593c8
+	  if (!reloc_section)
5593c8
+	    {
5593c8
+	      grub_dprintf ("chain", " section is not reloc section?\n");
5593c8
+	      grub_dprintf ("chain", " rds: 0x%08x, vs: %08x\n",
5593c8
+			    section->raw_data_size, section->virtual_size);
5593c8
+	      grub_dprintf ("chain", " base: %p end: %p\n", base, end);
5593c8
+	      grub_dprintf ("chain", " reloc_base: %p reloc_base_end: %p\n",
5593c8
+			    reloc_base, reloc_base_end);
5593c8
+	    }
5593c8
+	}
5593c8
+
5593c8
+      grub_dprintf ("chain", " Section characteristics are %08x\n",
5593c8
+		   section->characteristics);
5593c8
+      grub_dprintf ("chain", " Section virtual size: %08x\n",
5593c8
+		   section->virtual_size);
5593c8
+      grub_dprintf ("chain", " Section raw_data size: %08x\n",
5593c8
+		   section->raw_data_size);
5593c8
+      if (section->characteristics & GRUB_PE32_SCN_MEM_DISCARDABLE)
5593c8
+	{
5593c8
+	  grub_dprintf ("chain", " Discarding section\n");
5593c8
+	  continue;
5593c8
+	}
5593c8
+
5593c8
+      if (!base || !end)
5593c8
+        {
5593c8
+	  grub_dprintf ("chain", " section is invalid\n");
5593c8
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid section size");
5593c8
+          goto error_exit;
5593c8
+        }
5593c8
+
5593c8
+      if (section->characteristics & GRUB_PE32_SCN_CNT_UNINITIALIZED_DATA)
5593c8
+	{
5593c8
+	  if (section->raw_data_size != 0)
5593c8
+	    grub_dprintf ("chain", " UNINITIALIZED_DATA section has data?\n");
5593c8
+	}
5593c8
+      else if (section->virtual_address < context.size_of_headers ||
5593c8
+	       section->raw_data_offset < context.size_of_headers)
5593c8
+	{
5593c8
+	  grub_error (GRUB_ERR_BAD_ARGUMENT,
5593c8
+		      "Section %d is inside image headers", i);
5593c8
+	  goto error_exit;
5593c8
+	}
5593c8
+
5593c8
+      if (section->raw_data_size > 0)
5593c8
+	{
5593c8
+	  grub_dprintf ("chain", " copying 0x%08x bytes to %p\n",
5593c8
+			section->raw_data_size, base);
5593c8
+	  grub_memcpy (base,
5593c8
+		       (grub_efi_uint8_t*)data + section->raw_data_offset,
5593c8
+		       section->raw_data_size);
5593c8
+	}
5593c8
+
5593c8
+      if (section->raw_data_size < section->virtual_size)
5593c8
+	{
5593c8
+	  grub_dprintf ("chain", " padding with 0x%08x bytes at %p\n",
5593c8
+			section->virtual_size - section->raw_data_size,
5593c8
+			base + section->raw_data_size);
5593c8
+	  grub_memset (base + section->raw_data_size, 0,
5593c8
+		       section->virtual_size - section->raw_data_size);
5593c8
+	}
5593c8
+
5593c8
+      grub_dprintf ("chain", " finished section %s\n", name);
5593c8
+    }
5593c8
+
5593c8
+  /* 5 == EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC */
5593c8
+  if (context.number_of_rva_and_sizes <= 5)
5593c8
+    {
5593c8
+      grub_dprintf ("chain", "image has no relocation entry\n");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  if (context.reloc_dir->size && reloc_section)
5593c8
+    {
5593c8
+      /* run the relocation fixups */
5593c8
+      efi_status = relocate_coff (&context, reloc_section, data,
5593c8
+				  buffer_aligned);
5593c8
+
5593c8
+      if (efi_status != GRUB_EFI_SUCCESS)
5593c8
+	{
5593c8
+	  grub_error (GRUB_ERR_BAD_ARGUMENT, "relocation failed");
5593c8
+	  goto error_exit;
5593c8
+	}
5593c8
+    }
5593c8
+
5593c8
+  if (!found_entry_point)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "entry point is not within sections");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+  if (found_entry_point > 1)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "%d sections contain entry point",
5593c8
+		  found_entry_point);
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  li = grub_efi_get_loaded_image (grub_efi_image_handle);
5593c8
+  if (!li)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_ARGUMENT, "no loaded image available");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  grub_memcpy (&li_bak, li, sizeof (grub_efi_loaded_image_t));
5593c8
+  li->image_base = buffer_aligned;
5593c8
+  li->image_size = context.image_size;
5593c8
+  li->load_options = cmdline;
5593c8
+  li->load_options_size = cmdline_len;
5593c8
+  li->file_path = grub_efi_get_media_file_path (file_path);
5593c8
+  li->device_handle = dev_handle;
5593c8
+  if (!li->file_path)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching file path found");
5593c8
+      goto error_exit;
5593c8
+    }
5593c8
+
5593c8
+  grub_dprintf ("chain", "booting via entry point\n");
5593c8
+  efi_status = efi_call_2 (entry_point, grub_efi_image_handle,
5593c8
+			   grub_efi_system_table);
5593c8
+
5593c8
+  grub_dprintf ("chain", "entry_point returned %ld\n", efi_status);
5593c8
+  grub_memcpy (li, &li_bak, sizeof (grub_efi_loaded_image_t));
5593c8
+  efi_status = efi_call_1 (b->free_pool, buffer);
5593c8
+
5593c8
+  return 1;
5593c8
+
5593c8
+error_exit:
5593c8
+  grub_dprintf ("chain", "error_exit: grub_errno: %d\n", grub_errno);
5593c8
+  if (buffer)
5593c8
+      efi_call_1 (b->free_pool, buffer);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_secureboot_chainloader_unload (void)
5593c8
+{
5593c8
+  grub_efi_boot_services_t *b;
5593c8
+
5593c8
+  b = grub_efi_system_table->boot_services;
5593c8
+  efi_call_2 (b->free_pages, address, pages);
5593c8
+  grub_free (file_path);
5593c8
+  grub_free (cmdline);
5593c8
+  cmdline = 0;
5593c8
+  file_path = 0;
5593c8
+  dev_handle = 0;
5593c8
+
5593c8
+  grub_dl_unref (my_mod);
5593c8
+  return GRUB_ERR_NONE;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_load_and_start_image(void *boot_image)
5593c8
+{
5593c8
+  grub_efi_boot_services_t *b;
5593c8
+  grub_efi_status_t status;
5593c8
+  grub_efi_loaded_image_t *loaded_image;
5593c8
+
5593c8
+  b = grub_efi_system_table->boot_services;
5593c8
+
5593c8
+  status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
5593c8
+		       boot_image, fsize, &image_handle);
5593c8
+  if (status != GRUB_EFI_SUCCESS)
5593c8
+    {
5593c8
+      if (status == GRUB_EFI_OUT_OF_RESOURCES)
5593c8
+	grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
5593c8
+      else
5593c8
+	grub_error (GRUB_ERR_BAD_OS, "cannot load image");
5593c8
+      return -1;
5593c8
+    }
5593c8
+
5593c8
+  /* LoadImage does not set a device handler when the image is
5593c8
+     loaded from memory, so it is necessary to set it explicitly here.
5593c8
+     This is a mess.  */
5593c8
+  loaded_image = grub_efi_get_loaded_image (image_handle);
5593c8
+  if (! loaded_image)
5593c8
+    {
5593c8
+      grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
5593c8
+      return -1;
5593c8
+    }
5593c8
+  loaded_image->device_handle = dev_handle;
5593c8
+
5593c8
+  if (cmdline)
5593c8
+    {
5593c8
+      loaded_image->load_options = cmdline;
5593c8
+      loaded_image->load_options_size = cmdline_len;
5593c8
+    }
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_secureboot_chainloader_boot (void)
5593c8
+{
5593c8
+  int rc;
5593c8
+  rc = handle_image ((void *)(unsigned long)address, fsize);
5593c8
+  if (rc == 0)
5593c8
+    {
5593c8
+      grub_load_and_start_image((void *)(unsigned long)address);
5593c8
+    }
5593c8
+
5593c8
+  grub_loader_unset ();
5593c8
+  return grub_errno;
5593c8
+}
5593c8
+
5593c8
 static grub_err_t
5593c8
 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
 		      int argc, char *argv[])
5593c8
 {
5593c8
   grub_file_t file = 0;
5593c8
-  grub_ssize_t size;
5593c8
   grub_efi_status_t status;
5593c8
   grub_efi_boot_services_t *b;
5593c8
   grub_device_t dev = 0;
5593c8
   grub_efi_device_path_t *dp = 0;
5593c8
-  grub_efi_loaded_image_t *loaded_image;
5593c8
   char *filename;
5593c8
   void *boot_image = 0;
5593c8
-  grub_efi_handle_t dev_handle = 0;
5593c8
+  int rc;
5593c8
 
5593c8
   if (argc == 0)
5593c8
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
5593c8
@@ -238,15 +916,45 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   address = 0;
5593c8
   image_handle = 0;
5593c8
   file_path = 0;
5593c8
+  dev_handle = 0;
5593c8
 
5593c8
   b = grub_efi_system_table->boot_services;
5593c8
 
5593c8
+  if (argc > 1)
5593c8
+    {
5593c8
+      int i;
5593c8
+      grub_efi_char16_t *p16;
5593c8
+
5593c8
+      for (i = 1, cmdline_len = 0; i < argc; i++)
5593c8
+        cmdline_len += grub_strlen (argv[i]) + 1;
5593c8
+
5593c8
+      cmdline_len *= sizeof (grub_efi_char16_t);
5593c8
+      cmdline = p16 = grub_malloc (cmdline_len);
5593c8
+      if (! cmdline)
5593c8
+        goto fail;
5593c8
+
5593c8
+      for (i = 1; i < argc; i++)
5593c8
+        {
5593c8
+          char *p8;
5593c8
+
5593c8
+          p8 = argv[i];
5593c8
+          while (*p8)
5593c8
+            *(p16++) = *(p8++);
5593c8
+
5593c8
+          *(p16++) = ' ';
5593c8
+        }
5593c8
+      *(--p16) = 0;
5593c8
+    }
5593c8
+
5593c8
   file = grub_file_open (filename, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);
5593c8
   if (! file)
5593c8
     goto fail;
5593c8
 
5593c8
-  /* Get the root device's device path.  */
5593c8
-  dev = grub_device_open (0);
5593c8
+  /* Get the device path from filename. */
5593c8
+  char *devname = grub_file_get_device_name (filename);
5593c8
+  dev = grub_device_open (devname);
5593c8
+  if (devname)
5593c8
+    grub_free (devname);
5593c8
   if (! dev)
5593c8
     goto fail;
5593c8
 
5593c8
@@ -283,17 +991,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   if (! file_path)
5593c8
     goto fail;
5593c8
 
5593c8
-  grub_printf ("file path: ");
5593c8
-  grub_efi_print_device_path (file_path);
5593c8
-
5593c8
-  size = grub_file_size (file);
5593c8
-  if (!size)
5593c8
+  fsize = grub_file_size (file);
5593c8
+  if (!fsize)
5593c8
     {
5593c8
       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
5593c8
 		  filename);
5593c8
       goto fail;
5593c8
     }
5593c8
-  pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
5593c8
+  pages = (((grub_efi_uintn_t) fsize + ((1 << 12) - 1)) >> 12);
5593c8
 
5593c8
   status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
5593c8
 			      GRUB_EFI_LOADER_CODE,
5593c8
@@ -307,7 +1012,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
     }
5593c8
 
5593c8
   boot_image = (void *) ((grub_addr_t) address);
5593c8
-  if (grub_file_read (file, boot_image, size) != size)
5593c8
+  if (grub_file_read (file, boot_image, fsize) != fsize)
5593c8
     {
5593c8
       if (grub_errno == GRUB_ERR_NONE)
5593c8
 	grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
5593c8
@@ -317,7 +1022,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
     }
5593c8
 
5593c8
 #if defined (__i386__) || defined (__x86_64__)
5593c8
-  if (size >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
5593c8
+  if (fsize >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
5593c8
     {
5593c8
       struct grub_macho_fat_header *head = boot_image;
5593c8
       if (head->magic
5593c8
@@ -326,6 +1031,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
 	  grub_uint32_t i;
5593c8
 	  struct grub_macho_fat_arch *archs
5593c8
 	    = (struct grub_macho_fat_arch *) (head + 1);
5593c8
+
5593c8
+	  if (grub_efi_get_secureboot () == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
5593c8
+	    {
5593c8
+	      grub_error (GRUB_ERR_BAD_OS,
5593c8
+			  "MACHO binaries are forbidden with Secure Boot");
5593c8
+	      goto fail;
5593c8
+	    }
5593c8
+
5593c8
 	  for (i = 0; i < grub_cpu_to_le32 (head->nfat_arch); i++)
5593c8
 	    {
5593c8
 	      if (GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT (archs[i].cputype))
5593c8
@@ -340,79 +1053,39 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
 	      > ~grub_cpu_to_le32 (archs[i].size)
5593c8
 	      || grub_cpu_to_le32 (archs[i].offset)
5593c8
 	      + grub_cpu_to_le32 (archs[i].size)
5593c8
-	      > (grub_size_t) size)
5593c8
+	      > (grub_size_t) fsize)
5593c8
 	    {
5593c8
 	      grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
5593c8
 			  filename);
5593c8
 	      goto fail;
5593c8
 	    }
5593c8
 	  boot_image = (char *) boot_image + grub_cpu_to_le32 (archs[i].offset);
5593c8
-	  size = grub_cpu_to_le32 (archs[i].size);
5593c8
+	  fsize = grub_cpu_to_le32 (archs[i].size);
5593c8
 	}
5593c8
     }
5593c8
 #endif
5593c8
 
5593c8
-  status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
5593c8
-		       boot_image, size,
5593c8
-		       &image_handle);
5593c8
-  if (status != GRUB_EFI_SUCCESS)
5593c8
+  rc = grub_linuxefi_secure_validate((void *)(unsigned long)address, fsize);
5593c8
+  grub_dprintf ("chain", "linuxefi_secure_validate: %d\n", rc);
5593c8
+  if (rc > 0)
5593c8
     {
5593c8
-      if (status == GRUB_EFI_OUT_OF_RESOURCES)
5593c8
-	grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
5593c8
-      else
5593c8
-	grub_error (GRUB_ERR_BAD_OS, "cannot load image");
5593c8
-
5593c8
-      goto fail;
5593c8
+      grub_file_close (file);
5593c8
+      grub_device_close (dev);
5593c8
+      grub_loader_set (grub_secureboot_chainloader_boot,
5593c8
+		       grub_secureboot_chainloader_unload, 0);
5593c8
+      return 0;
5593c8
     }
5593c8
-
5593c8
-  /* LoadImage does not set a device handler when the image is
5593c8
-     loaded from memory, so it is necessary to set it explicitly here.
5593c8
-     This is a mess.  */
5593c8
-  loaded_image = grub_efi_get_loaded_image (image_handle);
5593c8
-  if (! loaded_image)
5593c8
+  else if (rc == 0)
5593c8
     {
5593c8
-      grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
5593c8
-      goto fail;
5593c8
-    }
5593c8
-  loaded_image->device_handle = dev_handle;
5593c8
-
5593c8
-  if (argc > 1)
5593c8
-    {
5593c8
-      int i, len;
5593c8
-      grub_efi_char16_t *p16;
5593c8
-
5593c8
-      for (i = 1, len = 0; i < argc; i++)
5593c8
-        len += grub_strlen (argv[i]) + 1;
5593c8
-
5593c8
-      len *= sizeof (grub_efi_char16_t);
5593c8
-      cmdline = p16 = grub_malloc (len);
5593c8
-      if (! cmdline)
5593c8
-        goto fail;
5593c8
+      grub_load_and_start_image(boot_image);
5593c8
+      grub_file_close (file);
5593c8
+      grub_device_close (dev);
5593c8
+      grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
5593c8
 
5593c8
-      for (i = 1; i < argc; i++)
5593c8
-        {
5593c8
-          char *p8;
5593c8
-
5593c8
-          p8 = argv[i];
5593c8
-          while (*p8)
5593c8
-            *(p16++) = *(p8++);
5593c8
-
5593c8
-          *(p16++) = ' ';
5593c8
-        }
5593c8
-      *(--p16) = 0;
5593c8
-
5593c8
-      loaded_image->load_options = cmdline;
5593c8
-      loaded_image->load_options_size = len;
5593c8
+      return 0;
5593c8
     }
5593c8
 
5593c8
-  grub_file_close (file);
5593c8
-  grub_device_close (dev);
5593c8
-
5593c8
-  grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
5593c8
-  return 0;
5593c8
-
5593c8
- fail:
5593c8
-
5593c8
+fail:
5593c8
   if (dev)
5593c8
     grub_device_close (dev);
5593c8
 
5593c8
@@ -424,6 +1097,9 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   if (address)
5593c8
     efi_call_2 (b->free_pages, address, pages);
5593c8
 
5593c8
+  if (cmdline)
5593c8
+    grub_free (cmdline);
5593c8
+
5593c8
   grub_dl_unref (my_mod);
5593c8
 
5593c8
   return grub_errno;
5593c8
diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c
5593c8
index c24202a5dd1..c8ecce6dfd0 100644
5593c8
--- a/grub-core/loader/efi/linux.c
5593c8
+++ b/grub-core/loader/efi/linux.c
5593c8
@@ -33,21 +33,34 @@ struct grub_efi_shim_lock
5593c8
 };
5593c8
 typedef struct grub_efi_shim_lock grub_efi_shim_lock_t;
5593c8
 
5593c8
-grub_efi_boolean_t
5593c8
+int
5593c8
 grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
5593c8
 {
5593c8
   grub_efi_guid_t guid = SHIM_LOCK_GUID;
5593c8
   grub_efi_shim_lock_t *shim_lock;
5593c8
+  grub_efi_status_t status;
5593c8
 
5593c8
   shim_lock = grub_efi_locate_protocol(&guid, NULL);
5593c8
-
5593c8
+  grub_dprintf ("secureboot", "shim_lock: %p\n", shim_lock);
5593c8
   if (!shim_lock)
5593c8
-    return 1;
5593c8
+    {
5593c8
+      grub_dprintf ("secureboot", "shim not available\n");
5593c8
+      return 0;
5593c8
+    }
5593c8
 
5593c8
-  if (shim_lock->verify(data, size) == GRUB_EFI_SUCCESS)
5593c8
-    return 1;
5593c8
+  grub_dprintf ("secureboot", "Asking shim to verify kernel signature\n");
5593c8
+  status = shim_lock->verify (data, size);
5593c8
+  grub_dprintf ("secureboot", "shim_lock->verify(): %ld\n", (long int)status);
5593c8
+  if (status == GRUB_EFI_SUCCESS)
5593c8
+    {
5593c8
+      grub_dprintf ("secureboot", "Kernel signature verification passed\n");
5593c8
+      return 1;
5593c8
+    }
5593c8
 
5593c8
-  return 0;
5593c8
+  grub_dprintf ("secureboot", "Kernel signature verification failed (0x%lx)\n",
5593c8
+		(unsigned long) status);
5593c8
+
5593c8
+  return -1;
5593c8
 }
5593c8
 
5593c8
 #pragma GCC diagnostic push
5593c8
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
5593c8
index bb2616a8092..6b24cbb9483 100644
5593c8
--- a/grub-core/loader/i386/efi/linux.c
5593c8
+++ b/grub-core/loader/i386/efi/linux.c
5593c8
@@ -117,6 +117,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
5593c8
       goto fail;
5593c8
     }
5593c8
 
5593c8
+  grub_dprintf ("linux", "initrd_mem = %lx\n", (unsigned long) initrd_mem);
5593c8
+
5593c8
   params->ramdisk_size = size;
5593c8
   params->ramdisk_image = (grub_uint32_t)(grub_addr_t) initrd_mem;
5593c8
 
5593c8
@@ -159,6 +161,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
   struct linux_i386_kernel_header lh;
5593c8
   grub_ssize_t len, start, filelen;
5593c8
   void *kernel = NULL;
5593c8
+  int rc;
5593c8
 
5593c8
   grub_dl_ref (my_mod);
5593c8
 
5593c8
@@ -184,11 +187,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
 
5593c8
   if (grub_file_read (file, kernel, filelen) != filelen)
5593c8
     {
5593c8
-      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"), argv[0]);
5593c8
+      grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Can't read kernel %s"),
5593c8
+		  argv[0]);
5593c8
       goto fail;
5593c8
     }
5593c8
 
5593c8
-  if (! grub_linuxefi_secure_validate (kernel, filelen))
5593c8
+  rc = grub_linuxefi_secure_validate (kernel, filelen);
5593c8
+  if (rc < 0)
5593c8
     {
5593c8
       grub_error (GRUB_ERR_INVALID_COMMAND, N_("%s has invalid signature"),
5593c8
 		  argv[0]);
5593c8
@@ -203,6 +208,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
       goto fail;
5593c8
     }
5593c8
 
5593c8
+  grub_dprintf ("linux", "params = %lx\n", (unsigned long) params);
5593c8
+
5593c8
   grub_memset (params, 0, 16384);
5593c8
 
5593c8
   grub_memcpy (&lh, kernel, sizeof (lh));
5593c8
@@ -241,6 +248,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
       goto fail;
5593c8
     }
5593c8
 
5593c8
+  grub_dprintf ("linux", "linux_cmdline = %lx\n",
5593c8
+		(unsigned long)linux_cmdline);
5593c8
+
5593c8
   grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
5593c8
   grub_create_loader_cmdline (argc, argv,
5593c8
                               linux_cmdline + sizeof (LINUX_IMAGE) - 1,
5593c8
@@ -275,9 +285,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5593c8
   grub_memcpy (params, &lh, 2 * 512);
5593c8
 
5593c8
   params->type_of_loader = 0x21;
5593c8
+  grub_dprintf("linux", "kernel_mem: %p handover_offset: %08x\n",
5593c8
+	       kernel_mem, handover_offset);
5593c8
 
5593c8
  fail:
5593c8
-
5593c8
   if (file)
5593c8
     grub_file_close (file);
5593c8
 
5593c8
diff --git a/include/grub/efi/linux.h b/include/grub/efi/linux.h
5593c8
index d9ede36773b..0033d9305a9 100644
5593c8
--- a/include/grub/efi/linux.h
5593c8
+++ b/include/grub/efi/linux.h
5593c8
@@ -22,7 +22,7 @@
5593c8
 #include <grub/err.h>
5593c8
 #include <grub/symbol.h>
5593c8
 
5593c8
-grub_efi_boolean_t
5593c8
+int
5593c8
 EXPORT_FUNC(grub_linuxefi_secure_validate) (void *data, grub_uint32_t size);
5593c8
 grub_err_t
5593c8
 EXPORT_FUNC(grub_efi_linux_boot) (void *kernel_address, grub_off_t offset,
5593c8
diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
5593c8
index 0ed8781f037..a43adf27464 100644
5593c8
--- a/include/grub/efi/pe32.h
5593c8
+++ b/include/grub/efi/pe32.h
5593c8
@@ -223,7 +223,11 @@ struct grub_pe64_optional_header
5593c8
 struct grub_pe32_section_table
5593c8
 {
5593c8
   char name[8];
5593c8
-  grub_uint32_t virtual_size;
5593c8
+  union
5593c8
+    {
5593c8
+      grub_uint32_t physical_address;
5593c8
+      grub_uint32_t virtual_size;
5593c8
+    };
5593c8
   grub_uint32_t virtual_address;
5593c8
   grub_uint32_t raw_data_size;
5593c8
   grub_uint32_t raw_data_offset;
5593c8
@@ -234,12 +238,18 @@ struct grub_pe32_section_table
5593c8
   grub_uint32_t characteristics;
5593c8
 };
5593c8
 
5593c8
+#define GRUB_PE32_SCN_TYPE_NO_PAD		0x00000008
5593c8
 #define GRUB_PE32_SCN_CNT_CODE			0x00000020
5593c8
 #define GRUB_PE32_SCN_CNT_INITIALIZED_DATA	0x00000040
5593c8
-#define GRUB_PE32_SCN_MEM_DISCARDABLE		0x02000000
5593c8
-#define GRUB_PE32_SCN_MEM_EXECUTE		0x20000000
5593c8
-#define GRUB_PE32_SCN_MEM_READ			0x40000000
5593c8
-#define GRUB_PE32_SCN_MEM_WRITE			0x80000000
5593c8
+#define GRUB_PE32_SCN_CNT_UNINITIALIZED_DATA	0x00000080
5593c8
+#define GRUB_PE32_SCN_LNK_OTHER			0x00000100
5593c8
+#define GRUB_PE32_SCN_LNK_INFO			0x00000200
5593c8
+#define GRUB_PE32_SCN_LNK_REMOVE		0x00000800
5593c8
+#define GRUB_PE32_SCN_LNK_COMDAT		0x00001000
5593c8
+#define GRUB_PE32_SCN_GPREL			0x00008000
5593c8
+#define GRUB_PE32_SCN_MEM_16BIT			0x00020000
5593c8
+#define GRUB_PE32_SCN_MEM_LOCKED		0x00040000
5593c8
+#define GRUB_PE32_SCN_MEM_PRELOAD		0x00080000
5593c8
 
5593c8
 #define GRUB_PE32_SCN_ALIGN_1BYTES		0x00100000
5593c8
 #define GRUB_PE32_SCN_ALIGN_2BYTES		0x00200000
5593c8
@@ -248,10 +258,28 @@ struct grub_pe32_section_table
5593c8
 #define GRUB_PE32_SCN_ALIGN_16BYTES		0x00500000
5593c8
 #define GRUB_PE32_SCN_ALIGN_32BYTES		0x00600000
5593c8
 #define GRUB_PE32_SCN_ALIGN_64BYTES		0x00700000
5593c8
+#define GRUB_PE32_SCN_ALIGN_128BYTES		0x00800000
5593c8
+#define GRUB_PE32_SCN_ALIGN_256BYTES		0x00900000
5593c8
+#define GRUB_PE32_SCN_ALIGN_512BYTES		0x00A00000
5593c8
+#define GRUB_PE32_SCN_ALIGN_1024BYTES		0x00B00000
5593c8
+#define GRUB_PE32_SCN_ALIGN_2048BYTES		0x00C00000
5593c8
+#define GRUB_PE32_SCN_ALIGN_4096BYTES		0x00D00000
5593c8
+#define GRUB_PE32_SCN_ALIGN_8192BYTES		0x00E00000
5593c8
 
5593c8
 #define GRUB_PE32_SCN_ALIGN_SHIFT		20
5593c8
 #define GRUB_PE32_SCN_ALIGN_MASK		7
5593c8
 
5593c8
+#define GRUB_PE32_SCN_LNK_NRELOC_OVFL		0x01000000
5593c8
+#define GRUB_PE32_SCN_MEM_DISCARDABLE		0x02000000
5593c8
+#define GRUB_PE32_SCN_MEM_NOT_CACHED		0x04000000
5593c8
+#define GRUB_PE32_SCN_MEM_NOT_PAGED		0x08000000
5593c8
+#define GRUB_PE32_SCN_MEM_SHARED		0x10000000
5593c8
+#define GRUB_PE32_SCN_MEM_EXECUTE		0x20000000
5593c8
+#define GRUB_PE32_SCN_MEM_READ			0x40000000
5593c8
+#define GRUB_PE32_SCN_MEM_WRITE			0x80000000
5593c8
+
5593c8
+
5593c8
+
5593c8
 #define GRUB_PE32_SIGNATURE_SIZE 4
5593c8
 
5593c8
 struct grub_pe32_header
5593c8
@@ -274,6 +302,20 @@ struct grub_pe32_header
5593c8
 #endif
5593c8
 };
5593c8
 
5593c8
+struct grub_pe32_header_32
5593c8
+{
5593c8
+  char signature[GRUB_PE32_SIGNATURE_SIZE];
5593c8
+  struct grub_pe32_coff_header coff_header;
5593c8
+  struct grub_pe32_optional_header optional_header;
5593c8
+};
5593c8
+
5593c8
+struct grub_pe32_header_64
5593c8
+{
5593c8
+  char signature[GRUB_PE32_SIGNATURE_SIZE];
5593c8
+  struct grub_pe32_coff_header coff_header;
5593c8
+  struct grub_pe64_optional_header optional_header;
5593c8
+};
5593c8
+
5593c8
 struct grub_pe32_fixup_block
5593c8
 {
5593c8
   grub_uint32_t page_rva;