nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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