dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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