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

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