From c4b2028c94cd5922461ca562f3a07eb13b6b04dd Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phco...@gmail.com>
Date: Sun, 13 Aug 2023 09:15:02 +0200
Subject: [PATCH 1/4] efi: Deduplicate configuration table search function

Signed-off-by: Vladimir Serbinenko <phco...@gmail.com>
---
 grub-core/commands/efi/loadbios.c | 37 ++++++++-----------------------
 grub-core/commands/efi/lssal.c    | 18 +++++----------
 grub-core/commands/efi/smbios.c   | 30 ++-----------------------
 grub-core/kern/efi/acpi.c         | 28 ++---------------------
 grub-core/kern/efi/efi.c          | 18 +++++++++++++++
 grub-core/kern/efi/fdt.c          | 21 +++++-------------
 include/grub/efi/efi.h            |  3 +++
 7 files changed, 46 insertions(+), 109 deletions(-)

diff --git a/grub-core/commands/efi/loadbios.c b/grub-core/commands/efi/loadbios.c
index 8f6b0ecfc..916633631 100644
--- a/grub-core/commands/efi/loadbios.c
+++ b/grub-core/commands/efi/loadbios.c
@@ -92,7 +92,6 @@ lock_rom_area (void)
 static void
 fake_bios_data (int use_rom)
 {
-  unsigned i;
   void *acpi, *smbios;
   grub_uint16_t *ebda_seg_ptr, *low_mem_ptr;
 
@@ -101,33 +100,15 @@ fake_bios_data (int use_rom)
   if ((*ebda_seg_ptr) || (*low_mem_ptr))
     return;
 
-  acpi = 0;
-  smbios = 0;
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    {
-      grub_guid_t *guid =
-	&grub_efi_system_table->configuration_table[i].vendor_guid;
-
-      if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_guid_t)))
-	{
-	  acpi = grub_efi_system_table->configuration_table[i].vendor_table;
-	  grub_dprintf ("efi", "ACPI2: %p\n", acpi);
-	}
-      else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_guid_t)))
-	{
-	  void *t;
-
-	  t = grub_efi_system_table->configuration_table[i].vendor_table;
-	  if (! acpi)
-	    acpi = t;
-	  grub_dprintf ("efi", "ACPI: %p\n", t);
-	}
-      else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
-	{
-	  smbios = grub_efi_system_table->configuration_table[i].vendor_table;
-	  grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
-	}
-    }
+  acpi = grub_efi_find_configuration_table(&acpi2_guid);
+  grub_dprintf ("efi", "ACPI2: %p\n", acpi);
+  if (!acpi) {
+    acpi = grub_efi_find_configuration_table(&acpi_guid);
+    grub_dprintf ("efi", "ACPI1: %p\n", acpi);
+  }
+
+  smbios = grub_efi_find_configuration_table(&smbios_guid);
+  grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
 
   *ebda_seg_ptr = FAKE_EBDA_SEG;
   *low_mem_ptr = (FAKE_EBDA_SEG >> 6);
diff --git a/grub-core/commands/efi/lssal.c b/grub-core/commands/efi/lssal.c
index fd6085f1b..0e36bfcdc 100644
--- a/grub-core/commands/efi/lssal.c
+++ b/grub-core/commands/efi/lssal.c
@@ -136,22 +136,16 @@ grub_cmd_lssal (struct grub_command *cmd __attribute__ ((unused)),
 		int argc __attribute__ ((unused)),
 		char **args __attribute__ ((unused)))
 {
-  const grub_efi_system_table_t *st = grub_efi_system_table;
-  grub_efi_configuration_table_t *t = st->configuration_table;
-  unsigned int i;
   static grub_guid_t guid = GRUB_EFI_SAL_TABLE_GUID;
+  void *table = grub_efi_find_configuration_table(&guid);
 
-  for (i = 0; i < st->num_table_entries; i++)
+  if (!table)
     {
-      if (grub_memcmp (&guid, &t->vendor_guid,
-		       sizeof (grub_guid_t)) == 0)
-	{
-	  disp_sal (t->vendor_table);
-	  return GRUB_ERR_NONE;
-	}
-      t++;
+        grub_printf ("SAL not found\n");
+	return GRUB_ERR_NONE;
     }
-  grub_printf ("SAL not found\n");
+
+  disp_sal (table);
   return GRUB_ERR_NONE;
 }
 
diff --git a/grub-core/commands/efi/smbios.c b/grub-core/commands/efi/smbios.c
index d77239732..296bedf7b 100644
--- a/grub-core/commands/efi/smbios.c
+++ b/grub-core/commands/efi/smbios.c
@@ -18,44 +18,18 @@
  */
 
 #include <grub/smbios.h>
-#include <grub/misc.h>
 #include <grub/efi/efi.h>
-#include <grub/efi/api.h>
 
 struct grub_smbios_eps *
 grub_machine_smbios_get_eps (void)
 {
-  unsigned i;
   static grub_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
-
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    {
-      grub_guid_t *guid =
-	&grub_efi_system_table->configuration_table[i].vendor_guid;
-
-      if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_guid_t)))
-	return (struct grub_smbios_eps *)
-	  grub_efi_system_table->configuration_table[i].vendor_table;
-    }
-
-  return 0;
+  return (struct grub_smbios_eps *) grub_efi_find_configuration_table(&smbios_guid);
 }
 
 struct grub_smbios_eps3 *
 grub_machine_smbios_get_eps3 (void)
 {
-  unsigned i;
   static grub_guid_t smbios3_guid = GRUB_EFI_SMBIOS3_TABLE_GUID;
-
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    {
-      grub_guid_t *guid =
-	&grub_efi_system_table->configuration_table[i].vendor_guid;
-
-      if (! grub_memcmp (guid, &smbios3_guid, sizeof (grub_guid_t)))
-	return (struct grub_smbios_eps3 *)
-	  grub_efi_system_table->configuration_table[i].vendor_table;
-    }
-
-  return 0;
+  return (struct grub_smbios_eps3 *) grub_efi_find_configuration_table(&smbios3_guid);
 }
diff --git a/grub-core/kern/efi/acpi.c b/grub-core/kern/efi/acpi.c
index 461c77c33..640ba172f 100644
--- a/grub-core/kern/efi/acpi.c
+++ b/grub-core/kern/efi/acpi.c
@@ -18,42 +18,18 @@
  */
 
 #include <grub/acpi.h>
-#include <grub/misc.h>
 #include <grub/efi/efi.h>
-#include <grub/efi/api.h>
 
 struct grub_acpi_rsdp_v10 *
 grub_machine_acpi_get_rsdpv1 (void)
 {
-  unsigned i;
   static grub_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
-
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    {
-      grub_guid_t *guid =
-	&grub_efi_system_table->configuration_table[i].vendor_guid;
-
-      if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_guid_t)))
-	return (struct grub_acpi_rsdp_v10 *)
-	  grub_efi_system_table->configuration_table[i].vendor_table;
-    }
-  return 0;
+  return (struct grub_acpi_rsdp_v10 *) grub_efi_find_configuration_table(&acpi_guid);
 }
 
 struct grub_acpi_rsdp_v20 *
 grub_machine_acpi_get_rsdpv2 (void)
 {
-  unsigned i;
   static grub_guid_t acpi20_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
-
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    {
-      grub_guid_t *guid =
-	&grub_efi_system_table->configuration_table[i].vendor_guid;
-
-      if (! grub_memcmp (guid, &acpi20_guid, sizeof (grub_guid_t)))
-	return (struct grub_acpi_rsdp_v20 *)
-	  grub_efi_system_table->configuration_table[i].vendor_table;
-    }
-  return 0;
+  return (struct grub_acpi_rsdp_v20 *) grub_efi_find_configuration_table(&acpi20_guid);
 }
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index a2afd8de9..2a9817a80 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -1031,3 +1031,21 @@ grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
 
   return 0;
 }
+
+void *
+grub_efi_find_configuration_table(const grub_guid_t *target_guid)
+{
+  unsigned i;
+
+  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
+    {
+      void *guid =
+	&grub_efi_system_table->configuration_table[i].vendor_guid;
+
+      if (! grub_memcmp (guid, target_guid, sizeof (grub_guid_t)))
+	return (void *)
+	  grub_efi_system_table->configuration_table[i].vendor_table;
+    }
+
+  return 0;
+}
diff --git a/grub-core/kern/efi/fdt.c b/grub-core/kern/efi/fdt.c
index 8fcf43f1b..99e95d34e 100644
--- a/grub-core/kern/efi/fdt.c
+++ b/grub-core/kern/efi/fdt.c
@@ -23,21 +23,12 @@
 void *
 grub_efi_get_firmware_fdt (void)
 {
-  grub_efi_configuration_table_t *tables;
   static grub_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
-  void *firmware_fdt = NULL;
-  unsigned int i;
-
-  /* Look for FDT in UEFI config tables. */
-  tables = grub_efi_system_table->configuration_table;
-
-  for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
-    if (grub_memcmp (&tables[i].vendor_guid, &fdt_guid, sizeof (fdt_guid)) == 0)
-      {
-	firmware_fdt = tables[i].vendor_table;
-	grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt);
-	break;
-      }
-
+  void *firmware_fdt = grub_efi_find_configuration_table(&fdt_guid);
+  if (firmware_fdt) {
+    grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt);
+  } else {
+    grub_dprintf ("linux", "not found registered FDT\n");
+  }
   return firmware_fdt;
 }
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 572f7135f..a5cd99e5a 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -119,6 +119,9 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
 						char **device,
 						char **path);
 
+void *
+EXPORT_FUNC (grub_efi_find_configuration_table) (const grub_guid_t *target_guid);
+
 #if defined(__arm__) || defined(__aarch64__) || defined(__riscv) || defined(__loongarch__)
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
-- 
2.39.2

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to