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