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