5b44e10
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5b44e10
From: Peter Jones <pjones@redhat.com>
5b44e10
Date: Mon, 1 Aug 2022 14:06:30 -0400
5b44e10
Subject: [PATCH] efi: use enumerated array positions for our allocation
5b44e10
 choices
5b44e10
5b44e10
In our kernel allocator on EFI systems, we currently have a growing
5b44e10
amount of code that references the various allocation policies by
5b44e10
position in the array, and of course maintenance of this code scales
5b44e10
very poorly.
5b44e10
5b44e10
This patch changes them to be enumerated, so they're easier to refer to
5b44e10
farther along in the code without confusion.
5b44e10
5b44e10
Signed-off-by: Peter Jones <pjones@redhat.com>
5b44e10
---
5b44e10
 grub-core/loader/i386/efi/linux.c | 31 ++++++++++++++++++++-----------
5b44e10
 1 file changed, 20 insertions(+), 11 deletions(-)
5b44e10
5b44e10
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
5b44e10
index 91ae274299..8daa070132 100644
5b44e10
--- a/grub-core/loader/i386/efi/linux.c
5b44e10
+++ b/grub-core/loader/i386/efi/linux.c
5b44e10
@@ -60,17 +60,26 @@ struct allocation_choice {
5b44e10
     grub_efi_allocate_type_t alloc_type;
5b44e10
 };
5b44e10
 
5b44e10
-static struct allocation_choice max_addresses[4] =
5b44e10
+enum {
5b44e10
+    KERNEL_PREF_ADDRESS,
5b44e10
+    KERNEL_4G_LIMIT,
5b44e10
+    KERNEL_NO_LIMIT,
5b44e10
+};
5b44e10
+
5b44e10
+static struct allocation_choice max_addresses[] =
5b44e10
   {
5b44e10
     /* the kernel overrides this one with pref_address and
5b44e10
      * GRUB_EFI_ALLOCATE_ADDRESS */
5b44e10
-    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
+    [KERNEL_PREF_ADDRESS] =
5b44e10
+      { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
+    /* If the flag in params is set, this one gets changed to be above 4GB. */
5b44e10
+    [KERNEL_4G_LIMIT] =
5b44e10
+      { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
     /* this one is always below 4GB, which we still *prefer* even if the flag
5b44e10
      * is set. */
5b44e10
-    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
-    /* If the flag in params is set, this one gets changed to be above 4GB. */
5b44e10
-    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
-    { 0, 0 }
5b44e10
+    [KERNEL_NO_LIMIT] =
5b44e10
+      { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
5b44e10
+    { NO_MEM, 0, 0 }
5b44e10
   };
5b44e10
 static struct allocation_choice saved_addresses[4];
5b44e10
 
5b44e10
@@ -405,7 +414,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5b44e10
   if (lh->xloadflags & LINUX_XLF_CAN_BE_LOADED_ABOVE_4G)
5b44e10
     {
5b44e10
       grub_dprintf ("linux", "Loading kernel above 4GB is supported; enabling.\n");
5b44e10
-      max_addresses[2].addr = GRUB_EFI_MAX_USABLE_ADDRESS;
5b44e10
+      max_addresses[KERNEL_NO_LIMIT].addr = GRUB_EFI_MAX_USABLE_ADDRESS;
5b44e10
     }
5b44e10
   else
5b44e10
     {
5b44e10
@@ -478,11 +487,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
5b44e10
   grub_dprintf ("linux", "lh->pref_address: %p\n", (void *)(grub_addr_t)lh->pref_address);
5b44e10
   if (lh->pref_address < (grub_uint64_t)GRUB_EFI_MAX_ALLOCATION_ADDRESS)
5b44e10
     {
5b44e10
-      max_addresses[0].addr = lh->pref_address;
5b44e10
-      max_addresses[0].alloc_type = GRUB_EFI_ALLOCATE_ADDRESS;
5b44e10
+      max_addresses[KERNEL_PREF_ADDRESS].addr = lh->pref_address;
5b44e10
+      max_addresses[KERNEL_PREF_ADDRESS].alloc_type = GRUB_EFI_ALLOCATE_ADDRESS;
5b44e10
     }
5b44e10
-  max_addresses[1].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
5b44e10
-  max_addresses[2].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
5b44e10
+  max_addresses[KERNEL_4G_LIMIT].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
5b44e10
+  max_addresses[KERNEL_NO_LIMIT].addr = GRUB_EFI_MAX_ALLOCATION_ADDRESS;
5b44e10
   kernel_size = lh->init_size;
5b44e10
   kernel_mem = kernel_alloc (kernel_size, GRUB_EFI_RUNTIME_SERVICES_CODE,
5b44e10
 			     N_("can't allocate kernel"));