dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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