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

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