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