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

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