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