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

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