nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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