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

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