Blame SOURCES/0006-Handle-multi-arch-64-on-32-boot-in-linuxefi-loader.patch

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