6b2dd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
bc092b
From: Peter Jones <pjones@redhat.com>
bc092b
Date: Tue, 14 Feb 2017 16:18:54 -0500
31cddd
Subject: [PATCH] Handle multi-arch (64-on-32) boot in linuxefi loader.
bc092b
bc092b
Allow booting 64-bit kernels on 32-bit EFI on x86.
bc092b
bc092b
Signed-off-by: Peter Jones <pjones@redhat.com>
bc092b
---
ec4acb
 grub-core/loader/efi/linux.c      |   9 +++-
15a207
 grub-core/loader/i386/efi/linux.c | 110 ++++++++++++++++++++++++++------------
bc092b
 include/grub/i386/linux.h         |   7 ++-
15a207
 3 files changed, 89 insertions(+), 37 deletions(-)
bc092b
bc092b
diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c
dbfd2e
index c8ecce6dfd0..0622dfa48d4 100644
bc092b
--- a/grub-core/loader/efi/linux.c
bc092b
+++ b/grub-core/loader/efi/linux.c
dbfd2e
@@ -69,12 +69,17 @@ grub_linuxefi_secure_validate (void *data, grub_uint32_t size)
bc092b
 typedef void (*handover_func) (void *, grub_efi_system_table_t *, void *);
bc092b
 
bc092b
 grub_err_t
bc092b
-grub_efi_linux_boot (void *kernel_addr, grub_off_t offset,
bc092b
+grub_efi_linux_boot (void *kernel_addr, grub_off_t handover_offset,
bc092b
 		     void *kernel_params)
bc092b
 {
bc092b
   handover_func hf;
bc092b
+  int offset = 0;
bc092b
 
bc092b
-  hf = (handover_func)((char *)kernel_addr + offset);
bc092b
+#ifdef __x86_64__
bc092b
+  offset = 512;
bc092b
+#endif
bc092b
+
bc092b
+  hf = (handover_func)((char *)kernel_addr + handover_offset + offset);
bc092b
   hf (grub_efi_image_handle, grub_efi_system_table, kernel_params);
bc092b
 
bc092b
   return GRUB_ERR_BUG;
bc092b
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
15a207
index 8db228c5bf5..800c3e54022 100644
bc092b
--- a/grub-core/loader/i386/efi/linux.c
bc092b
+++ b/grub-core/loader/i386/efi/linux.c
bc092b
@@ -44,14 +44,10 @@ static char *linux_cmdline;
bc092b
 static grub_err_t
bc092b
 grub_linuxefi_boot (void)
bc092b
 {
bc092b
-  int offset = 0;
bc092b
-
bc092b
-#ifdef __x86_64__
bc092b
-  offset = 512;
bc092b
-#endif
bc092b
   asm volatile ("cli");
bc092b
 
bc092b
-  return grub_efi_linux_boot ((char *)kernel_mem, handover_offset + offset,
bc092b
+  return grub_efi_linux_boot ((char *)kernel_mem,
bc092b
+			      handover_offset,
bc092b
 			      params);
bc092b
 }
bc092b
 
ad4aff
@@ -154,14 +150,20 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
bc092b
   return grub_errno;
bc092b
 }
bc092b
 
bc092b
+#define MIN(a, b) \
bc092b
+  ({ typeof (a) _a = (a); \
bc092b
+     typeof (b) _b = (b); \
bc092b
+     _a < _b ? _a : _b; })
bc092b
+
bc092b
 static grub_err_t
bc092b
 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
bc092b
 		int argc, char *argv[])
bc092b
 {
bc092b
   grub_file_t file = 0;
bc092b
-  struct linux_kernel_header lh;
ad4aff
-  grub_ssize_t len, start, filelen;
ad4aff
+  struct linux_i386_kernel_header *lh = NULL;
ad4aff
+  grub_ssize_t start, filelen;
bc092b
   void *kernel = NULL;
bc092b
+  int setup_header_end_offset;
bc092b
   int rc;
bc092b
 
bc092b
   grub_dl_ref (my_mod);
ad4aff
@@ -201,48 +203,79 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
-  params = grub_efi_allocate_pages_max (0x3fffffff, BYTES_TO_PAGES(16384));
bc092b
-
bc092b
+  params = grub_efi_allocate_pages_max (0x3fffffff,
bc092b
+					BYTES_TO_PAGES(sizeof(*params)));
bc092b
   if (! params)
bc092b
     {
bc092b
       grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
bc092b
       goto fail;
bc092b
     }
bc092b
 
ad4aff
-  grub_dprintf ("linux", "params = %lx\n", (unsigned long) params);
ad4aff
+  grub_dprintf ("linux", "params = %p\n", params);
ec4acb
 
ad4aff
-  grub_memset (params, 0, 16384);
bc092b
+  grub_memset (params, 0, sizeof(*params));
ec4acb
 
ad4aff
-  grub_memcpy (&lh, kernel, sizeof (lh));
ad4aff
-
ec4acb
-  if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
bc092b
+  setup_header_end_offset = *((grub_uint8_t *)kernel + 0x201);
15a207
+  grub_dprintf ("linux", "copying %lu bytes from %p to %p\n",
ad4aff
+		MIN((grub_size_t)0x202+setup_header_end_offset,
ad4aff
+		    sizeof (*params)) - 0x1f1,
bc092b
+		(grub_uint8_t *)kernel + 0x1f1,
bc092b
+		(grub_uint8_t *)params + 0x1f1);
bc092b
+  grub_memcpy ((grub_uint8_t *)params + 0x1f1,
bc092b
+	       (grub_uint8_t *)kernel + 0x1f1,
ad4aff
+		MIN((grub_size_t)0x202+setup_header_end_offset,sizeof (*params)) - 0x1f1);
ad4aff
+  lh = (struct linux_i386_kernel_header *)params;
ad4aff
+  grub_dprintf ("linux", "lh is at %p\n", lh);
ad4aff
+  grub_dprintf ("linux", "checking lh->boot_flag\n");
bc092b
+  if (lh->boot_flag != grub_cpu_to_le16 (0xaa55))
bc092b
     {
bc092b
       grub_error (GRUB_ERR_BAD_OS, N_("invalid magic number"));
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
-  if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
ad4aff
+  grub_dprintf ("linux", "checking lh->setup_sects\n");
bc092b
+  if (lh->setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
bc092b
     {
bc092b
       grub_error (GRUB_ERR_BAD_OS, N_("too many setup sectors"));
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
-  if (lh.version < grub_cpu_to_le16 (0x020b))
ad4aff
+  grub_dprintf ("linux", "checking lh->version\n");
bc092b
+  if (lh->version < grub_cpu_to_le16 (0x020b))
bc092b
     {
bc092b
       grub_error (GRUB_ERR_BAD_OS, N_("kernel too old"));
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
-  if (!lh.handover_offset)
ad4aff
+  grub_dprintf ("linux", "checking lh->handover_offset\n");
bc092b
+  if (!lh->handover_offset)
bc092b
     {
bc092b
       grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support EFI handover"));
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
+#if defined(__x86_64__) || defined(__aarch64__)
ad4aff
+  grub_dprintf ("linux", "checking lh->xloadflags\n");
bc092b
+  if (!(lh->xloadflags & LINUX_XLF_KERNEL_64))
bc092b
+    {
bc092b
+      grub_error (GRUB_ERR_BAD_OS, N_("kernel doesn't support 64-bit CPUs"));
bc092b
+      goto fail;
bc092b
+    }
bc092b
+#endif
bc092b
+
bc092b
+#if defined(__i386__)
bc092b
+  if ((lh->xloadflags & LINUX_XLF_KERNEL_64) &&
bc092b
+      !(lh->xloadflags & LINUX_XLF_EFI_HANDOVER_32))
bc092b
+    {
bc092b
+      grub_error (GRUB_ERR_BAD_OS,
bc092b
+		  N_("kernel doesn't support 32-bit handover"));
bc092b
+      goto fail;
bc092b
+    }
bc092b
+#endif
ec4acb
+
ad4aff
   grub_dprintf ("linux", "setting up cmdline\n");
ec4acb
   linux_cmdline = grub_efi_allocate_pages_max(0x3fffffff,
ec4acb
-					 BYTES_TO_PAGES(lh.cmdline_size + 1));
ec4acb
-
bc092b
+					 BYTES_TO_PAGES(lh->cmdline_size + 1));
bc092b
   if (!linux_cmdline)
bc092b
     {
bc092b
       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
15a207
@@ -255,21 +288,23 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
bc092b
   grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
bc092b
   grub_create_loader_cmdline (argc, argv,
bc092b
                               linux_cmdline + sizeof (LINUX_IMAGE) - 1,
bc092b
-			      lh.cmdline_size - (sizeof (LINUX_IMAGE) - 1));
bc092b
+			      lh->cmdline_size - (sizeof (LINUX_IMAGE) - 1));
bc092b
 
bc092b
-  lh.cmd_line_ptr = (grub_uint32_t)(grub_addr_t)linux_cmdline;
15a207
+  grub_dprintf ("linux", "cmdline:%s\n", linux_cmdline);
ad4aff
+  grub_dprintf ("linux", "setting lh->cmd_line_ptr\n");
bc092b
+  lh->cmd_line_ptr = (grub_uint32_t)(grub_addr_t)linux_cmdline;
bc092b
 
bc092b
-  handover_offset = lh.handover_offset;
ad4aff
+  grub_dprintf ("linux", "computing handover offset\n");
bc092b
+  handover_offset = lh->handover_offset;
bc092b
 
bc092b
-  start = (lh.setup_sects + 1) * 512;
ad4aff
-  len = grub_file_size(file) - start;
bc092b
+  start = (lh->setup_sects + 1) * 512;
bc092b
 
ad4aff
-  kernel_mem = grub_efi_allocate_pages_max(lh.pref_address,
ad4aff
-					   BYTES_TO_PAGES(lh.init_size));
bbc6a8
+  kernel_mem = grub_efi_allocate_pages_max(lh->pref_address,
bbc6a8
+					   BYTES_TO_PAGES(lh->init_size));
bc092b
 
bc092b
   if (!kernel_mem)
bc092b
     kernel_mem = grub_efi_allocate_pages_max(0x3fffffff,
bc092b
-					     BYTES_TO_PAGES(lh.init_size));
bc092b
+					     BYTES_TO_PAGES(lh->init_size));
bc092b
 
bc092b
   if (!kernel_mem)
bc092b
     {
15a207
@@ -277,14 +312,21 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
bc092b
       goto fail;
bc092b
     }
bc092b
 
bc092b
-  grub_memcpy (kernel_mem, (char *)kernel + start, len);
ad4aff
+  grub_dprintf ("linux", "kernel_mem = %lx\n", (unsigned long) kernel_mem);
bc092b
+
bc092b
   grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
bc092b
   loaded=1;
ad4aff
+  grub_dprintf ("linux", "setting lh->code32_start to %p\n", kernel_mem);
bc092b
+  lh->code32_start = (grub_uint32_t)(grub_addr_t) kernel_mem;
bc092b
 
bc092b
-  lh.code32_start = (grub_uint32_t)(grub_uint64_t) kernel_mem;
bc092b
-  grub_memcpy (params, &lh, 2 * 512);
bc092b
+  grub_memcpy (kernel_mem, (char *)kernel + start, filelen - start);
bc092b
 
bc092b
-  params->type_of_loader = 0x21;
ad4aff
+  grub_dprintf ("linux", "setting lh->type_of_loader\n");
bc092b
+  lh->type_of_loader = 0x6;
ec4acb
+
ad4aff
+  grub_dprintf ("linux", "setting lh->ext_loader_{type,ver}\n");
bc092b
+  params->ext_loader_type = 0;
bc092b
+  params->ext_loader_ver = 2;
ad4aff
   grub_dprintf("linux", "kernel_mem: %p handover_offset: %08x\n",
ad4aff
 	       kernel_mem, handover_offset);
bc092b
 
15a207
@@ -301,10 +343,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
ad4aff
       loaded = 0;
ad4aff
     }
bc092b
 
ad4aff
-  if (linux_cmdline && !loaded)
ad4aff
+  if (linux_cmdline && lh && !loaded)
bc092b
     grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)
bc092b
 			 linux_cmdline,
bc092b
-			 BYTES_TO_PAGES(lh.cmdline_size + 1));
bc092b
+			 BYTES_TO_PAGES(lh->cmdline_size + 1));
bc092b
 
bc092b
   if (kernel_mem && !loaded)
bc092b
     grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)kernel_mem,
bc092b
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
15a207
index bb19dbd5a77..8474a857ed2 100644
bc092b
--- a/include/grub/i386/linux.h
bc092b
+++ b/include/grub/i386/linux.h
15a207
@@ -133,7 +133,12 @@ struct linux_i386_kernel_header
bc092b
   grub_uint32_t kernel_alignment;
bc092b
   grub_uint8_t relocatable;
bc092b
   grub_uint8_t min_alignment;
bc092b
-  grub_uint8_t pad[2];
bc092b
+#define LINUX_XLF_KERNEL_64                   (1<<0)
bc092b
+#define LINUX_XLF_CAN_BE_LOADED_ABOVE_4G      (1<<1)
bc092b
+#define LINUX_XLF_EFI_HANDOVER_32             (1<<2)
bc092b
+#define LINUX_XLF_EFI_HANDOVER_64             (1<<3)
bc092b
+#define LINUX_XLF_EFI_KEXEC                   (1<<4)
bc092b
+  grub_uint16_t xloadflags;
bc092b
   grub_uint32_t cmdline_size;
bc092b
   grub_uint32_t hardware_subarch;
bc092b
   grub_uint64_t hardware_subarch_data;