Skip to contentSkip to conversation

lsefi: fixed memory leaks

PATCH In review Renaud Métrich via Grub-devel 3 participants 10 messages Mar 19, 2025 – Jun 1, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/2 lsefi: fixed memory leaks

---
 grub-core/commands/efi/lsefi.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/commands/efi/lsefi.c +4 -0
index 7b8316d41..bda25a3a9 100644
@@ -127,8 +127,12 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
 	    grub_printf ("  %pG\n", protocols[j]);
 	}
 
+      if (protocols)
+	grub_efi_free_pool (protocols);
     }
 
+  grub_free (handles);
+
   return 0;
 }
 
Signature · 7 lines
2.48.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v6 2/2 efi: new 'connectefi' command

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
 NEWS                                |   5 +-
 grub-core/Makefile.core.def         |   6 +
 grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
 grub-core/commands/efi/lsefi.c      |   3 +-
 grub-core/disk/efi/efidisk.c        |  13 ++
 grub-core/kern/efi/efi.c            |  13 ++
 include/grub/efi/api.h              |   2 +-
 include/grub/efi/disk.h             |   2 +
 include/grub/efi/efi.h              |   5 +
 9 files changed, 258 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/commands/efi/connectefi.c
NEWS +4 -1
index 310130962..48d9dd4dd 100644
@@ -32,6 +32,9 @@ New in 2.06:
 * BootHole and BootHole2 fixes.
 * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
 New in 2.04:
 
 * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
   * Prefer pmtimer for TSC calibration.
 
 * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
   * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
   * New command `pcidump' for PCI platforms.
   * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +6 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
 module = {
   name = blocklist;
   common = commands/blocklist.c;
grub-core/commands/efi/connectefi.c new file +212 -0

212 lines not on this page · raw message

grub-core/commands/efi/lsefi.c +2 -1
index bda25a3a9..8a82ababf 100644
@@ -19,6 +19,7 @@
 #include <grub/mm.h>
 #include <grub/misc.h>
 #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
 #include <grub/efi/edid.h>
 #include <grub/efi/pci.h>
 #include <grub/efi/efi.h>
@@ -35,7 +36,7 @@ static struct known_protocol
   const char *name;
 } known_protocols[] =
   {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },
     { GRUB_EFI_BLOCK_IO_GUID, "block" },
     { GRUB_EFI_SERIAL_IO_GUID, "serial" },
     { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +13 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
   free_devices (devices);
 }
 
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;
+
+  enumerate_disks ();
+}
+
 static int
 grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
 		      grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +13 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
   return buffer;
 }
 
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
+			     grub_efi_handle_t *driver_image_handle,
+			     grub_efi_device_path_protocol_t *remaining_device_path,
+			     grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+		     driver_image_handle, remaining_device_path, recursive);
+}
+
 void *
 grub_efi_open_protocol (grub_efi_handle_t handle,
 			grub_guid_t *protocol,
include/grub/efi/api.h +1 -1
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
     { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }
 
-#define GRUB_EFI_DISK_IO_GUID	\
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID	\
   { 0xce345171, 0xba0b, 0x11d2, \
     { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }
include/grub/efi/disk.h +2 -0
index 254475c84..6845c2f1f 100644
@@ -27,6 +27,8 @@ grub_efi_handle_t
 EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk);
 char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t *handle);
 
+void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void);
+
 void grub_efidisk_init (void);
 void grub_efidisk_fini (void);
 
include/grub/efi/efi.h +5 -0
index a5cd99e5a..60d0b2bf8 100644
@@ -44,6 +44,11 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
 				     grub_guid_t *protocol,
 				     void *search_key,
 				     grub_efi_uintn_t *num_handles);
+grub_efi_status_t
+EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t controller_handle,
+					  grub_efi_handle_t *driver_image_handle,
+					  grub_efi_device_path_protocol_t *remaining_device_path,
+					  grub_efi_boolean_t recursive);
 void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
 					   grub_guid_t *protocol,
 					   grub_efi_uint32_t attributes);
Signature · 7 lines
2.48.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

Re: [PATCH v6 2/2] efi: new 'connectefi' command

Hello,

I just had one question about the use of the 'goto' and the 'loop' label in the below code... see my question below the 'loop' label. Let me know if I just misunderstand or am wrong.

Thank you!
Andrew

Quoted 221 lines · Renaud Métrich via Grub-devel

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
 NEWS                                |   5 +-
 grub-core/Makefile.core.def         |   6 +
 grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
 grub-core/commands/efi/lsefi.c      |   3 +-
 grub-core/disk/efi/efidisk.c        |  13 ++
 grub-core/kern/efi/efi.c            |  13 ++
 include/grub/efi/api.h              |   2 +-
 include/grub/efi/disk.h             |   2 +
 include/grub/efi/efi.h              |   5 +
 9 files changed, 258 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/commands/efi/connectefi.c
NEWS +4 -1
index 310130962..48d9dd4dd 100644
@@ -32,6 +32,9 @@ New in 2.06:
 * BootHole and BootHole2 fixes.
 * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
 New in 2.04:
 
 * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
   * Prefer pmtimer for TSC calibration.
 
 * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
   * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
   * New command `pcidump' for PCI platforms.
   * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +6 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
 module = {
   name = blocklist;
   common = commands/blocklist.c;
grub-core/commands/efi/connectefi.c new file +114 -0
new file mode 100644
index 000000000..5049c2abe
@@ -0,0 +1,212 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_efi_already_handled
+{
+  struct grub_efi_already_handled *next;
+  struct grub_efi_already_handled **prev;
+  grub_efi_handle_t handle;
+};
+
+static struct grub_efi_already_handled *already_handled;
+
+static struct grub_efi_already_handled *
+is_in_list (grub_efi_handle_t handle)
+{
+  struct grub_efi_already_handled *e;
+
+  FOR_LIST_ELEMENTS (e, already_handled)
+    if (e->handle == handle)
+      return e;
+
+  return NULL;
+}
+
+static void
+free_handle_list (void)
+{
+  struct grub_efi_already_handled *e;
+  while ((e = already_handled) != NULL)
+    {
+      already_handled = already_handled->next;
+      grub_free (e);
+    }
+}
+
+typedef enum searched_item_flag
+{
+  SEARCHED_ITEM_FLAG_LOOP = 1,
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
+} searched_item_flags;
+
+typedef struct searched_item
+{
+  grub_efi_guid_t guid;
+  const char *name;
+  searched_item_flags flags;
+} searched_items;
+
+static grub_err_t
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
+                    int argc, char **args)
+{
+  unsigned s;
+  searched_items pciroot_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items disk_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O", SEARCHED_ITEM_FLAG_RECURSIVE },
+      { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "DISK I/O", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items *items = NULL;
+  unsigned nitems = 0;
+  grub_err_t grub_err = GRUB_ERR_NONE;
+  unsigned total_connected = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+  if (grub_strcmp(args[0], "pciroot") == 0)
+    {
+      items = pciroot_items;
+      nitems = ARRAY_SIZE (pciroot_items);
+    }
+  else if (grub_strcmp(args[0], "disk") == 0)
+    {
+      items = disk_items;
+      nitems = ARRAY_SIZE (disk_items);
+    }
+  else if (grub_strcmp(args[0], N_("all")) == 0)
+    {
+      items = NULL;
+      nitems = 1;
+    }
+  else

grub-core/commands/efi/connectefi.c line 124

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                      N_("unexpected argument `%s'"), args[0]);
+
+  for (s = 0; s < nitems; s++)
+    {
+      grub_efi_handle_t *handles;
+      grub_efi_uintn_t num_handles;
+      unsigned i, connected = 0, loop = 0;
+
+loop:

In the section later that uses 'goto' to get back here, is there a potential this code will enter an infinite loop since I assume the increment of 's' is being skipped by the 'goto'? Any reason the 'goto' later in the code can't use a 'continue'?

Quoted 203 lines
grub-core/commands/efi/connectefi.c new file +88 -0
+      loop++;
+      if (items != NULL)
+       {
+         grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
+         handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
+                                           &items[s].guid, 0, &num_handles);
+       }
+      else
+       handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+                                         NULL, NULL, &num_handles);
+
+      if (!handles)
+       continue;
+
+      for (i = 0; i < num_handles; i++)
+       {
+         grub_efi_handle_t handle = handles[i];
+         grub_efi_status_t status;
+         unsigned j;
+
+         /* Skip already handled handles  */
+         if (is_in_list (handle) != NULL)
+           {
+             grub_dprintf ("efi", "  handle %p: already processed\n",
+                                  handle);
+             continue;
+           }
+
+         status = grub_efi_connect_controller(handle, NULL, NULL,
+                       !items || items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
+         if (status == GRUB_EFI_SUCCESS)
+           {
+             connected++;
+             total_connected++;
+             grub_dprintf ("efi", "  handle %p: connected\n", handle);
+           }
+         else
+           grub_dprintf ("efi", "  handle %p: failed to connect (%d)\n",
+                                handle, (grub_efi_int8_t) status);
+
+         struct grub_efi_already_handled *item = grub_malloc(sizeof (*item));
+         if (!item)
+           break; /* fatal  */
+         grub_list_push (GRUB_AS_LIST_P (&already_handled), GRUB_AS_LIST (item));
+         item->handle = handle;
+       }
+
+      grub_free (handles);
+      if (grub_err != GRUB_ERR_NONE)
+       break; /* fatal  */
+
+      if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
+       {
+         connected = 0;
+         goto loop;
+       }
+
+      free_handle_list ();
+    }
+
+  free_handle_list ();
+
+  if (total_connected)
+    grub_efidisk_reenumerate_disks ();
+
+  return grub_err;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(connectefi)
+{
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
+                              /* TRANSLATORS: only translate 'all' keyword */
+                              N_("pciroot|disk|all"),
+                              N_("Connect EFI handles."
+                                 " If 'pciroot' is specified, connect PCI"
+                                 " root EFI handles recursively."
+                                 " If 'disk' is specified, connect SCSI"
+                                 " and DISK I/O EFI handles recursively."
+                                 " If 'all' is specified, connect all"
+                                 " EFI handles recursively."));
+}
+
+GRUB_MOD_FINI(connectefi)
+{
+  grub_unregister_command (cmd);
+}
grub-core/commands/efi/lsefi.c +2 -1
index bda25a3a9..8a82ababf 100644
@@ -19,6 +19,7 @@
 #include <grub/mm.h>
 #include <grub/misc.h>
 #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
 #include <grub/efi/edid.h>
 #include <grub/efi/pci.h>
 #include <grub/efi/efi.h>
@@ -35,7 +36,7 @@ static struct known_protocol
   const char *name;
 } known_protocols[] =
   {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },
     { GRUB_EFI_BLOCK_IO_GUID, "block" },
     { GRUB_EFI_SERIAL_IO_GUID, "serial" },
     { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +13 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
   free_devices (devices);
 }
 
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;
+
+  enumerate_disks ();
+}
+
 static int
 grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
                      grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +13 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
   return buffer;
 }
 
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
+                            grub_efi_handle_t *driver_image_handle,
+                            grub_efi_device_path_protocol_t *remaining_device_path,
+                            grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+                    driver_image_handle, remaining_device_path, recursive);
+}
+
 void *
 grub_efi_open_protocol (grub_efi_handle_t handle,
                        grub_guid_t *protocol,
include/grub/efi/api.h +1 -1
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
     { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }
 
-#define GRUB_EFI_DISK_IO_GUID  \
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID \
   { 0xce345171, 0xba0b, 0x11d2, \
     { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }
include/grub/efi/disk.h +2 -0
index 254475c84..6845c2f1f 100644
@@ -27,6 +27,8 @@ grub_efi_handle_t
 EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk);
 char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t *handle);
 
+void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void);
+
 void grub_efidisk_init (void);
 void grub_efidisk_fini (void);
 
include/grub/efi/efi.h +5 -1
index a5cd99e5a..60d0b2bf8 100644
@@ -44,6 +44,11 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
                                     grub_guid_t *protocol,
                                     void *search_key,
                                     grub_efi_uintn_t *num_handles);
+grub_efi_status_t
+EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t controller_handle,
+                                         grub_efi_handle_t *driver_image_handle,
+                                         grub_efi_device_path_protocol_t *remaining_device_path,
+                                         grub_efi_boolean_t recursive);
 void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
                                           grub_guid_t *protocol,
                                           grub_efi_uint32_t attributes);
--

2.48.1

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Re: [PATCH v6 2/2] efi: new 'connectefi' command

Hi Andrew,

Sorry I missed your email.

IMHO the "goto" is useful here to be able to loop again at same "s" level: we actually want to redo the operation without going one step further.

Let me explain with an example.

We want to connect PCIROOT, then PCI level, then SCSI I/O level.

There is only one PCIROOT, then you can have multiple levels of PCI.

Whenever you enable a PCI handle, you may have new PCI handles showing up, so you want to enable them through listing all the handles again and processing them.

Only once we didn't enable some new PCI handle in the current loop, we are ready to enable the next level, which is SCSI I/O devices.

For these devices, however, all we do is let the UEFI function enable them recursively.

If we were letting the UEFI function enable recursively the PCI devices, then almost everything would be enabled and this could potentially take a long time.

Renaud.

Le 3/20/25 à 1:28 AM, Andrew Hamilton a écrit :

Quoted 449 lines

Hello,

I just had one question about the use of the 'goto' and the 'loop' label in the below code... see my question below the 'loop' label. Let me know if I just misunderstand or am wrong.

Thank you!
Andrew

Quoted 220 lines · Renaud Métrich via Grub-devel

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
  NEWS                                |   5 +-
  grub-core/Makefile.core.def         |   6 +
  grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
  grub-core/commands/efi/lsefi.c      |   3 +-
  grub-core/disk/efi/efidisk.c        |  13 ++
  grub-core/kern/efi/efi.c            |  13 ++
  include/grub/efi/api.h              |   2 +-
  include/grub/efi/disk.h             |   2 +
  include/grub/efi/efi.h              |   5 +
  9 files changed, 258 insertions(+), 3 deletions(-)

create mode 100644 grub-core/commands/efi/connectefi.c

NEWS +4 -1
index 310130962..48d9dd4dd 100644
@@ -32,6 +32,9 @@ New in 2.06:
  * BootHole and BootHole2 fixes.
  * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
  New in 2.04:
 
  * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
    * Prefer pmtimer for TSC calibration.
 
  * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
    * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
    * New command `pcidump' for PCI platforms.
    * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +6 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
    enable = efi;
  };
 
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
  module = {
    name = blocklist;
    common = commands/blocklist.c;
grub-core/commands/efi/connectefi.c new file +114 -0
new file mode 100644
index 000000000..5049c2abe
@@ -0,0 +1,212 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_efi_already_handled
+{
+  struct grub_efi_already_handled *next;
+  struct grub_efi_already_handled **prev;
+  grub_efi_handle_t handle;
+};
+
+static struct grub_efi_already_handled *already_handled;
+
+static struct grub_efi_already_handled *
+is_in_list (grub_efi_handle_t handle)
+{
+  struct grub_efi_already_handled *e;
+
+  FOR_LIST_ELEMENTS (e, already_handled)
+    if (e->handle == handle)
+      return e;
+
+  return NULL;
+}
+
+static void
+free_handle_list (void)
+{
+  struct grub_efi_already_handled *e;
+  while ((e = already_handled) != NULL)
+    {
+      already_handled = already_handled->next;
+      grub_free (e);
+    }
+}
+
+typedef enum searched_item_flag
+{
+  SEARCHED_ITEM_FLAG_LOOP = 1,
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
+} searched_item_flags;
+
+typedef struct searched_item
+{
+  grub_efi_guid_t guid;
+  const char *name;
+  searched_item_flags flags;
+} searched_items;
+
+static grub_err_t
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
+                    int argc, char **args)
+{
+  unsigned s;
+  searched_items pciroot_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items disk_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O", SEARCHED_ITEM_FLAG_RECURSIVE },
+      { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "DISK I/O", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items *items = NULL;
+  unsigned nitems = 0;
+  grub_err_t grub_err = GRUB_ERR_NONE;
+  unsigned total_connected = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+  if (grub_strcmp(args[0], "pciroot") == 0)
+    {
+      items = pciroot_items;
+      nitems = ARRAY_SIZE (pciroot_items);
+    }
+  else if (grub_strcmp(args[0], "disk") == 0)
+    {
+      items = disk_items;
+      nitems = ARRAY_SIZE (disk_items);
+    }
+  else if (grub_strcmp(args[0], N_("all")) == 0)
+    {
+      items = NULL;
+      nitems = 1;
+    }
+  else

grub-core/commands/efi/connectefi.c line 124

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                      N_("unexpected argument `%s'"), args[0]);
+
+  for (s = 0; s < nitems; s++)
+    {
+      grub_efi_handle_t *handles;
+      grub_efi_uintn_t num_handles;
+      unsigned i, connected = 0, loop = 0;
+
+loop:

In the section later that uses 'goto' to get back here, is there a potential this code will enter an infinite loop since I assume the increment of 's' is being skipped by the 'goto'? Any reason the 'goto' later in the code can't use a 'continue'?

Quoted 203 lines
grub-core/commands/efi/connectefi.c new file +88 -0
+      loop++;
+      if (items != NULL)
+       {
+         grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
+         handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
+                                           &items[s].guid, 0, &num_handles);
+       }
+      else
+       handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+                                         NULL, NULL, &num_handles);
+
+      if (!handles)
+       continue;
+
+      for (i = 0; i < num_handles; i++)
+       {
+         grub_efi_handle_t handle = handles[i];
+         grub_efi_status_t status;
+         unsigned j;
+
+         /* Skip already handled handles  */
+         if (is_in_list (handle) != NULL)
+           {
+             grub_dprintf ("efi", "  handle %p: already processed\n",
+                                  handle);
+             continue;
+           }
+
+         status = grub_efi_connect_controller(handle, NULL, NULL,
+                       !items || items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
+         if (status == GRUB_EFI_SUCCESS)
+           {
+             connected++;
+             total_connected++;
+             grub_dprintf ("efi", "  handle %p: connected\n", handle);
+           }
+         else
+           grub_dprintf ("efi", "  handle %p: failed to connect (%d)\n",
+                                handle, (grub_efi_int8_t) status);
+
+         struct grub_efi_already_handled *item = grub_malloc(sizeof (*item));
+         if (!item)
+           break; /* fatal  */
+         grub_list_push (GRUB_AS_LIST_P (&already_handled), GRUB_AS_LIST (item));
+         item->handle = handle;
+       }
+
+      grub_free (handles);
+      if (grub_err != GRUB_ERR_NONE)
+       break; /* fatal  */
+
+      if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
+       {
+         connected = 0;
+         goto loop;
+       }
+
+      free_handle_list ();
+    }
+
+  free_handle_list ();
+
+  if (total_connected)
+    grub_efidisk_reenumerate_disks ();
+
+  return grub_err;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(connectefi)
+{
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
+                              /* TRANSLATORS: only translate 'all' keyword */
+                              N_("pciroot|disk|all"),
+                              N_("Connect EFI handles."
+                                 " If 'pciroot' is specified, connect PCI"
+                                 " root EFI handles recursively."
+                                 " If 'disk' is specified, connect SCSI"
+                                 " and DISK I/O EFI handles recursively."
+                                 " If 'all' is specified, connect all"
+                                 " EFI handles recursively."));
+}
+
+GRUB_MOD_FINI(connectefi)
+{
+  grub_unregister_command (cmd);
+}
grub-core/commands/efi/lsefi.c +2 -1
index bda25a3a9..8a82ababf 100644
@@ -19,6 +19,7 @@
  #include <grub/mm.h>
  #include <grub/misc.h>
  #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
  #include <grub/efi/edid.h>
  #include <grub/efi/pci.h>
  #include <grub/efi/efi.h>
@@ -35,7 +36,7 @@ static struct known_protocol
    const char *name;
  } known_protocols[] =
    {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },
      { GRUB_EFI_BLOCK_IO_GUID, "block" },
      { GRUB_EFI_SERIAL_IO_GUID, "serial" },
      { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +13 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
    free_devices (devices);
  }
 
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;
+
+  enumerate_disks ();
+}
+
  static int
  grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
                       grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +13 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
    return buffer;
  }
 
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
+                            grub_efi_handle_t *driver_image_handle,
+                            grub_efi_device_path_protocol_t *remaining_device_path,
+                            grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+                    driver_image_handle, remaining_device_path, recursive);
+}
+
  void *
  grub_efi_open_protocol (grub_efi_handle_t handle,
                         grub_guid_t *protocol,
include/grub/efi/api.h +1 -1
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
      { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }
 
-#define GRUB_EFI_DISK_IO_GUID  \
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID \
    { 0xce345171, 0xba0b, 0x11d2, \
      { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }
include/grub/efi/disk.h +2 -0
index 254475c84..6845c2f1f 100644
@@ -27,6 +27,8 @@ grub_efi_handle_t
  EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk);
  char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t *handle);
 
+void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void);
+
  void grub_efidisk_init (void);
  void grub_efidisk_fini (void);
 
include/grub/efi/efi.h +5 -1
index a5cd99e5a..60d0b2bf8 100644
@@ -44,6 +44,11 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
                                      grub_guid_t *protocol,
                                      void *search_key,
                                      grub_efi_uintn_t *num_handles);
+grub_efi_status_t
+EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t controller_handle,
+                                         grub_efi_handle_t *driver_image_handle,
+                                         grub_efi_device_path_protocol_t *remaining_device_path,
+                                         grub_efi_boolean_t recursive);
  void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
                                            grub_guid_t *protocol,
                                            grub_efi_uint32_t attributes);
--

2.48.1

Quoted 4 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH v6 2/2] efi: new 'connectefi' command

Thank you for explaining. I would suggest adding some maximum number of iterations to prevent an infinite loop if perhaps the EFI is misbehaving inside the "loop:" section. Not sure the right limit, but there must be some maximum allowed assuming worst case conditions or maximum allow based on protocols.

Thanks,
Andrew

Quoted 497 lines · Renaud Métrich

Hi Andrew,

Sorry I missed your email.

IMHO the "goto" is useful here to be able to loop again at same "s"
level: we actually want to redo the operation without going one step further.

Let me explain with an example.

We want to connect PCIROOT, then PCI level, then SCSI I/O level.

There is only one PCIROOT, then you can have multiple levels of PCI.

Whenever you enable a PCI handle, you may have new PCI handles showing up, so you want to enable them through listing all the handles again and processing them.

Only once we didn't enable some new PCI handle in the current loop, we are ready to enable the next level, which is SCSI I/O devices.

For these devices, however, all we do is let the UEFI function enable them recursively.

If we were letting the UEFI function enable recursively the PCI devices, then almost everything would be enabled and this could potentially take a long time.

Renaud.

Quoted 84 lines · Andrew Hamilton

Hello,

I just had one question about the use of the 'goto' and the 'loop' label in the below code... see my question below the 'loop' label. Let me know if I just misunderstand or am wrong.

Thank you!
Andrew

Quoted 73 lines · Renaud Métrich via Grub-devel

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
  NEWS                                |   5 +-
  grub-core/Makefile.core.def         |   6 +
  grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
  grub-core/commands/efi/lsefi.c      |   3 +-
  grub-core/disk/efi/efidisk.c        |  13 ++
  grub-core/kern/efi/efi.c            |  13 ++
  include/grub/efi/api.h              |   2 +-
  include/grub/efi/disk.h             |   2 +
  include/grub/efi/efi.h              |   5 +
  9 files changed, 258 insertions(+), 3 deletions(-)

create mode 100644 grub-core/commands/efi/connectefi.c

NEWS +3 -0
index 310130962..48d9dd4dd 100644
@@ -32,6 +32,9 @@ New in 2.06:
  * BootHole and BootHole2 fixes.
  * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
  New in 2.04:
 
Quoted 8 lines

NEWS New in 2.02: · line 125

Quoted 8 lines
NEWS +1 -1
  * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
    * Prefer pmtimer for TSC calibration.
 
  * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
    * New `cmosdump' and `cmosset' commands on platforms with CMOS

support.

Quoted 10 lines
Quoted 10 lines
NEWS +0 -0
    * New command `pcidump' for PCI platforms.
    * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +0 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
    enable = efi;
  };
 
Quoted 10 lines

Review

Quoted 10 lines
grub-core/Makefile.core.def +6 -0
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
  module = {
    name = blocklist;
    common = commands/blocklist.c;
(quoted diff) +0 -0

b/grub-core/commands/efi/connectefi.c

Quoted 11 lines

grub-core/commands/efi/connectefi.c line 6

Quoted 11 lines

new file mode 100644
index 000000000..5049c2abe

grub-core/commands/efi/connectefi.c new file +6 -0
@@ -0,0 +1,212 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published

by

Quoted 68 lines
Quoted 68 lines
grub-core/commands/efi/connectefi.c new file +68 -0
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_efi_already_handled
+{
+  struct grub_efi_already_handled *next;
+  struct grub_efi_already_handled **prev;
+  grub_efi_handle_t handle;
+};
+
+static struct grub_efi_already_handled *already_handled;
+
+static struct grub_efi_already_handled *
+is_in_list (grub_efi_handle_t handle)
+{
+  struct grub_efi_already_handled *e;
+
+  FOR_LIST_ELEMENTS (e, already_handled)
+    if (e->handle == handle)
+      return e;
+
+  return NULL;
+}
+
+static void
+free_handle_list (void)
+{
+  struct grub_efi_already_handled *e;
+  while ((e = already_handled) != NULL)
+    {
+      already_handled = already_handled->next;
+      grub_free (e);
+    }
+}
+
+typedef enum searched_item_flag
+{
+  SEARCHED_ITEM_FLAG_LOOP = 1,
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
+} searched_item_flags;
+
+typedef struct searched_item
+{
+  grub_efi_guid_t guid;
+  const char *name;
+  searched_item_flags flags;
+} searched_items;
+
Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+static grub_err_t
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
+                    int argc, char **args)
+{
+  unsigned s;
+  searched_items pciroot_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root",

SEARCHED_ITEM_FLAG_RECURSIVE }

Quoted 6 lines

grub-core/commands/efi/connectefi.c

Quoted 6 lines
grub-core/commands/efi/connectefi.c new file +6 -0
+    };
+  searched_items disk_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O",

SEARCHED_ITEM_FLAG_RECURSIVE },

Quoted 1 line

grub-core/commands/efi/connectefi.c line 89

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+      { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "DISK I/O",

SEARCHED_ITEM_FLAG_RECURSIVE }

Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+    };
+  searched_items *items = NULL;
+  unsigned nitems = 0;
+  grub_err_t grub_err = GRUB_ERR_NONE;
+  unsigned total_connected = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument

expected"));

Quoted 23 lines
Quoted 23 lines
grub-core/commands/efi/connectefi.c new file +23 -0
+
+  if (grub_strcmp(args[0], "pciroot") == 0)
+    {
+      items = pciroot_items;
+      nitems = ARRAY_SIZE (pciroot_items);
+    }
+  else if (grub_strcmp(args[0], "disk") == 0)
+    {
+      items = disk_items;
+      nitems = ARRAY_SIZE (disk_items);
+    }
+  else if (grub_strcmp(args[0], N_("all")) == 0)
+    {
+      items = NULL;
+      nitems = 1;
+    }
+  else
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                      N_("unexpected argument `%s'"), args[0]);
+
+  for (s = 0; s < nitems; s++)
+    {
+      grub_efi_handle_t *handles;
Quoted 13 lines

grub-core/commands/efi/connectefi.c line 124

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+      grub_efi_uintn_t num_handles;
+      unsigned i, connected = 0, loop = 0;
+
+loop:

In the section later that uses 'goto' to get back here, is there a potential this code will enter an infinite loop since I assume the increment of 's' is being skipped by the 'goto'? Any reason the 'goto' later in the code can't use a 'continue'?

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+      loop++;
+      if (items != NULL)
+       {
+         grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name,

loop);

Quoted 2 lines

grub-core/commands/efi/connectefi.c line 130

Quoted 2 lines
grub-core/commands/efi/connectefi.c new file +2 -0
+         handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
+                                           &items[s].guid, 0,

&num_handles);

Quoted 16 lines
Quoted 16 lines
grub-core/commands/efi/connectefi.c new file +16 -0
+       }
+      else
+       handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+                                         NULL, NULL, &num_handles);
+
+      if (!handles)
+       continue;
+
+      for (i = 0; i < num_handles; i++)
+       {
+         grub_efi_handle_t handle = handles[i];
+         grub_efi_status_t status;
+         unsigned j;
+
+         /* Skip already handled handles  */
+         if (is_in_list (handle) != NULL)
Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+           {
+             grub_dprintf ("efi", "  handle %p: already processed\n",
+                                  handle);
+             continue;
+           }
+
+         status = grub_efi_connect_controller(handle, NULL, NULL,
+                       !items || items[s].flags &

SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);

Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+         if (status == GRUB_EFI_SUCCESS)
+           {
+             connected++;
+             total_connected++;
+             grub_dprintf ("efi", "  handle %p: connected\n", handle);
+           }
+         else
+           grub_dprintf ("efi", "  handle %p: failed to connect

(%d)\n",

Quoted 3 lines

grub-core/commands/efi/connectefi.c

Quoted 3 lines
grub-core/commands/efi/connectefi.c new file +3 -0
+                                handle, (grub_efi_int8_t) status);
+
+         struct grub_efi_already_handled *item = grub_malloc(sizeof

(*item));

Quoted 3 lines

grub-core/commands/efi/connectefi.c

Quoted 3 lines
grub-core/commands/efi/connectefi.c new file +3 -0
+         if (!item)
+           break; /* fatal  */
+         grub_list_push (GRUB_AS_LIST_P (&already_handled),

GRUB_AS_LIST (item));

Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+         item->handle = handle;
+       }
+
+      grub_free (handles);
+      if (grub_err != GRUB_ERR_NONE)
+       break; /* fatal  */
+
+      if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP &&

connected)

Quoted 14 lines
Quoted 14 lines
grub-core/commands/efi/connectefi.c new file +14 -0
+       {
+         connected = 0;
+         goto loop;
+       }
+
+      free_handle_list ();
+    }
+
+  free_handle_list ();
+
+  if (total_connected)
+    grub_efidisk_reenumerate_disks ();
+
+  return grub_err;
Quoted 8 lines

grub-core/commands/efi/connectefi.c

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(connectefi)
+{
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
+                              /* TRANSLATORS: only translate 'all'

keyword */

Quoted 3 lines

grub-core/commands/efi/connectefi.c

Quoted 3 lines
grub-core/commands/efi/connectefi.c new file +3 -0
+                              N_("pciroot|disk|all"),
+                              N_("Connect EFI handles."
+                                 " If 'pciroot' is specified, connect

PCI"

Quoted 2 lines

grub-core/commands/efi/connectefi.c line 203

Quoted 2 lines
grub-core/commands/efi/connectefi.c new file +2 -0
+                                 " root EFI handles recursively."
+                                 " If 'disk' is specified, connect

SCSI"

Quoted 1 line

grub-core/commands/efi/connectefi.c line 204

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+                                 " and DISK I/O EFI handles

recursively."

Quoted 9 lines

Review

Quoted 9 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+                                 " If 'all' is specified, connect all"
+                                 " EFI handles recursively."));
+}
+
+GRUB_MOD_FINI(connectefi)
+{
+  grub_unregister_command (cmd);
+}
(quoted diff) +0 -0

b/grub-core/commands/efi/lsefi.c

Quoted 28 lines
Quoted 28 lines

index bda25a3a9..8a82ababf 100644

grub-core/commands/efi/lsefi.c +2 -1
@@ -19,6 +19,7 @@
  #include <grub/mm.h>
  #include <grub/misc.h>
  #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
  #include <grub/efi/edid.h>
  #include <grub/efi/pci.h>
  #include <grub/efi/efi.h>
@@ -35,7 +36,7 @@ static struct known_protocol
    const char *name;
  } known_protocols[] =
    {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },
      { GRUB_EFI_BLOCK_IO_GUID, "block" },
      { GRUB_EFI_SERIAL_IO_GUID, "serial" },
      { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +0 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
    free_devices (devices);
  }
 
Quoted 15 lines
Quoted 5 lines
grub-core/disk/efi/efidisk.c +5 -0
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);

grub-core/disk/efi/efidisk.c enumerate_disks (void)

Quoted 10 lines
grub-core/disk/efi/efidisk.c +8 -0
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;
+
+  enumerate_disks ();
+}
+
  static int
  grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void

*hook_data,

Quoted 6 lines

grub-core/kern/efi/efi.c grub_efi_locate_handle

Quoted 6 lines
grub-core/disk/efi/efidisk.c +0 -0
                       grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +0 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle

(grub_efi_locate_search_type_t search_type,

Quoted 7 lines

grub-core/kern/efi/efi.c grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,

Quoted 7 lines
grub-core/kern/efi/efi.c +4 -0
    return buffer;
  }
 
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
+                            grub_efi_handle_t *driver_image_handle,
+                            grub_efi_device_path_protocol_t

*remaining_device_path,

Quoted 7 lines

grub-core/kern/efi/efi.c grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,

Quoted 7 lines
grub-core/kern/efi/efi.c +7 -0
+                            grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+                    driver_image_handle, remaining_device_path,

recursive);

Quoted 13 lines
Quoted 13 lines
grub-core/kern/efi/efi.c +2 -0
+}
+
  void *
  grub_efi_open_protocol (grub_efi_handle_t handle,
                         grub_guid_t *protocol,
include/grub/efi/api.h +0 -0
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
      { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }
 
Quoted 12 lines

include/grub/efi/disk.h grub_efi_handle_t · line 28

Quoted 12 lines
include/grub/efi/api.h +1 -1
-#define GRUB_EFI_DISK_IO_GUID  \
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID \
    { 0xce345171, 0xba0b, 0x11d2, \
      { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }
include/grub/efi/disk.h +0 -0
index 254475c84..6845c2f1f 100644
@@ -27,6 +27,8 @@ grub_efi_handle_t
  EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk);
  char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t

*handle);

Quoted 11 lines

include/grub/efi/efi.h EXPORT_FUNC(grub_efi_locate_handle)

Quoted 11 lines
include/grub/efi/disk.h +2 -0
+void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void);
+
  void grub_efidisk_init (void);
  void grub_efidisk_fini (void);
 
include/grub/efi/efi.h +0 -0
index a5cd99e5a..60d0b2bf8 100644
@@ -44,6 +44,11 @@ EXPORT_FUNC(grub_efi_locate_handle)

(grub_efi_locate_search_type_t search_type,

Quoted 5 lines

include/grub/efi/efi.h EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,

Quoted 5 lines
include/grub/efi/efi.h +2 -0
                                      grub_guid_t *protocol,
                                      void *search_key,
                                      grub_efi_uintn_t *num_handles);
+grub_efi_status_t
+EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t

controller_handle,

Quoted 1 line

include/grub/efi/efi.h EXPORT_FUNC(grub_efi_locate_handle) · line 49

Quoted 1 line
include/grub/efi/efi.h +1 -0
+                                         grub_efi_handle_t
Quoted 17 lines

*driver_image_handle,

Quoted 1 line

include/grub/efi/efi.h EXPORT_FUNC(grub_efi_locate_handle) · line 50

Quoted 1 line
include/grub/efi/efi.h +1 -0
+

grub_efi_device_path_protocol_t *remaining_device_path,

Quoted 4 lines

include/grub/efi/efi.h EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,

Quoted 4 lines
include/grub/efi/efi.h +1 -0
+                                         grub_efi_boolean_t recursive);
  void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
                                            grub_guid_t *protocol,
                                            grub_efi_uint32_t

attributes);

Quoted 8 lines
Quoted 8 lines

--
2.48.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

Re: [PATCH v6 2/2] efi: new 'connectefi' command

In reply to Renaud Métrich via Grub-devel
Quoted 22 lines · Renaud Métrich via Grub-devel

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

Quoted 10 lines

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

This has to be documented in the GRUB docs.

NEWS

Quoted 28 lines

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
 NEWS                                |   5 +-
 grub-core/Makefile.core.def         |   6 +
 grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
 grub-core/commands/efi/lsefi.c      |   3 +-
 grub-core/disk/efi/efidisk.c        |  13 ++
 grub-core/kern/efi/efi.c            |  13 ++
 include/grub/efi/api.h              |   2 +-
 include/grub/efi/disk.h             |   2 +
 include/grub/efi/efi.h              |   5 +
 9 files changed, 258 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/commands/efi/connectefi.c
NEWS +0 -0

Please drop NEWS file change.

Quoted 45 lines

index 310130962..48d9dd4dd 100644

NEWS +4 -1
@@ -32,6 +32,9 @@ New in 2.06:
 * BootHole and BootHole2 fixes.
 * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
 New in 2.04:
 
 * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
   * Prefer pmtimer for TSC calibration.
 
 * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
   * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
   * New command `pcidump' for PCI platforms.
   * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +6 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
 module = {
   name = blocklist;
   common = commands/blocklist.c;
grub-core/commands/efi/connectefi.c new file +0 -0
new file mode 100644
index 000000000..5049c2abe

grub-core/commands/efi/connectefi.c line 3

Quoted 3 lines
grub-core/commands/efi/connectefi.c new file +3 -0
@@ -0,0 +1,212 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.

s/2022/2025/

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or

grub-core/commands/efi/connectefi.c line 17

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */

Please add an empty line here.

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+

grub-core/commands/efi/connectefi.c line 37

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_efi_already_handled
+{
+  struct grub_efi_already_handled *next;
+  struct grub_efi_already_handled **prev;
+  grub_efi_handle_t handle;
+};
+
+static struct grub_efi_already_handled *already_handled;

This probably does not need to be global.

Quoted 6 lines
grub-core/commands/efi/connectefi.c new file +6 -0
+static struct grub_efi_already_handled *
+is_in_list (grub_efi_handle_t handle)
+{
+  struct grub_efi_already_handled *e;
+
+  FOR_LIST_ELEMENTS (e, already_handled)

grub-core/commands/efi/connectefi.c line 54

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    if (e->handle == handle)
+      return e;
+
+  return NULL;
+}
+
+static void
+free_handle_list (void)
+{
+  struct grub_efi_already_handled *e;

Please add an empty line here.

grub-core/commands/efi/connectefi.c line 62

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+  while ((e = already_handled) != NULL)
+    {
+      already_handled = already_handled->next;
+      grub_free (e);
+    }
+}
+
+typedef enum searched_item_flag

enum searched_item_flag

grub-core/commands/efi/connectefi.c line 66

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+{
+  SEARCHED_ITEM_FLAG_LOOP = 1,
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
+} searched_item_flags;

typedef enum searched_item_flag searched_item_flag_t;

Please fix similar issues everywhere.

grub-core/commands/efi/connectefi.c line 79

Quoted 12 lines
grub-core/commands/efi/connectefi.c new file +12 -0
+typedef struct searched_item
+{
+  grub_efi_guid_t guid;
+  const char *name;
+  searched_item_flags flags;
+} searched_items;
+
+static grub_err_t
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
+		     int argc, char **args)
+{
+  unsigned s;

s/unsigned/unsigned int/

And below please...

grub-core/commands/efi/connectefi.c line 92

Quoted 13 lines
grub-core/commands/efi/connectefi.c new file +13 -0
+  searched_items pciroot_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items disk_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O", SEARCHED_ITEM_FLAG_RECURSIVE },
+      { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "DISK I/O", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items *items = NULL;
+  unsigned nitems = 0;

Probably an int is good enough here.

grub-core/commands/efi/connectefi.c line 94

Quoted 2 lines
grub-core/commands/efi/connectefi.c new file +2 -0
+  grub_err_t grub_err = GRUB_ERR_NONE;
+  unsigned total_connected = 0;

Ditto.

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+  if (grub_strcmp(args[0], "pciroot") == 0)

grub-core/commands/efi/connectefi.c line 109

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    {
+      items = pciroot_items;
+      nitems = ARRAY_SIZE (pciroot_items);
+    }
+  else if (grub_strcmp(args[0], "disk") == 0)
+    {
+      items = disk_items;
+      nitems = ARRAY_SIZE (disk_items);
+    }
+  else if (grub_strcmp(args[0], N_("all")) == 0)

Please drop N_()...

grub-core/commands/efi/connectefi.c line 116

Quoted 7 lines
grub-core/commands/efi/connectefi.c new file +7 -0
+    {
+      items = NULL;
+      nitems = 1;
+    }
+  else
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+		       N_("unexpected argument `%s'"), args[0]);

unexpected argument: %s

Quoted 27 lines
grub-core/commands/efi/connectefi.c new file +27 -0
+
+  for (s = 0; s < nitems; s++)
+    {
+      grub_efi_handle_t *handles;
+      grub_efi_uintn_t num_handles;
+      unsigned i, connected = 0, loop = 0;
+
+loop:
+      loop++;
+      if (items != NULL)
+	{
+	  grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
+	  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
+					    &items[s].guid, 0, &num_handles);
+	}
+      else
+	handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+					  NULL, NULL, &num_handles);
+
+      if (!handles)
+	continue;
+
+      for (i = 0; i < num_handles; i++)
+	{
+	  grub_efi_handle_t handle = handles[i];
+	  grub_efi_status_t status;
+	  unsigned j;

grub-core/commands/efi/connectefi.c line 153

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+
+	  /* Skip already handled handles  */
+	  if (is_in_list (handle) != NULL)
+	    {
+	      grub_dprintf ("efi", "  handle %p: already processed\n",
+				   handle);
+	      continue;
+	    }
+
+	  status = grub_efi_connect_controller(handle, NULL, NULL,

Wrong coding style. Missing space before "(".
I can see similar issues in other places too.

grub-core/commands/efi/connectefi.c line 158

Quoted 5 lines
grub-core/commands/efi/connectefi.c new file +5 -0
+			!items || items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
+	  if (status == GRUB_EFI_SUCCESS)
+	    {
+	      connected++;
+	      total_connected++;

What is a difference between connected and total_connected? I think both could be bool. And then probably one could be dropped...

grub-core/commands/efi/connectefi.c line 162

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+	      grub_dprintf ("efi", "  handle %p: connected\n", handle);
+	    }
+	  else
+	    grub_dprintf ("efi", "  handle %p: failed to connect (%d)\n",

Please use PRIuGRUB_EFI_UINTN_T instead of %d and...

grub-core/commands/efi/connectefi.c line 163

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+				 handle, (grub_efi_int8_t) status);

... drop grub_efi_int8_t cast...

grub-core/commands/efi/connectefi.c line 165

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+	  struct grub_efi_already_handled *item = grub_malloc(sizeof (*item));

Please do not mix code with variable definitions.

grub-core/commands/efi/connectefi.c line 166

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+	  if (!item)

if (item == NULL)

Please check for NULL explicitly.

grub-core/commands/efi/connectefi.c line 179

Quoted 13 lines
grub-core/commands/efi/connectefi.c new file +13 -0
+	    break; /* fatal  */
+	  grub_list_push (GRUB_AS_LIST_P (&already_handled), GRUB_AS_LIST (item));
+	  item->handle = handle;
+	}
+
+      grub_free (handles);
+      if (grub_err != GRUB_ERR_NONE)
+	break; /* fatal  */
+
+      if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
+	{
+	  connected = 0;
+	  goto loop;

I think it is possible to not use goto for the loop here.

Quoted 9 lines
grub-core/commands/efi/connectefi.c new file +9 -0
+	}
+
+      free_handle_list ();
+    }
+
+  free_handle_list ();
+
+  if (total_connected)
+    grub_efidisk_reenumerate_disks ();

grub-core/commands/efi/connectefi.c line 198

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+
+  return grub_err;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(connectefi)
+{
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
+			       /* TRANSLATORS: only translate 'all' keyword */

Do not translate arguments for commands. It will only make confusion...

Quoted 27 lines
grub-core/commands/efi/connectefi.c new file +14 -0
+			       N_("pciroot|disk|all"),
+			       N_("Connect EFI handles."
+				  " If 'pciroot' is specified, connect PCI"
+				  " root EFI handles recursively."
+				  " If 'disk' is specified, connect SCSI"
+				  " and DISK I/O EFI handles recursively."
+				  " If 'all' is specified, connect all"
+				  " EFI handles recursively."));
+}
+
+GRUB_MOD_FINI(connectefi)
+{
+  grub_unregister_command (cmd);
+}
grub-core/commands/efi/lsefi.c +1 -0
index bda25a3a9..8a82ababf 100644
@@ -19,6 +19,7 @@
 #include <grub/mm.h>
 #include <grub/misc.h>
 #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
 #include <grub/efi/edid.h>
 #include <grub/efi/pci.h>
 #include <grub/efi/efi.h>

grub-core/commands/efi/lsefi.c static struct known_protocol · line 39

Quoted 5 lines
grub-core/commands/efi/lsefi.c +1 -1
@@ -35,7 +36,7 @@ static struct known_protocol
   const char *name;
 } known_protocols[] =
   {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },

Why? This change begs for separate patch with good explanation why the change is needed.

Quoted 10 lines
grub-core/commands/efi/lsefi.c +0 -0
     { GRUB_EFI_BLOCK_IO_GUID, "block" },
     { GRUB_EFI_SERIAL_IO_GUID, "serial" },
     { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +0 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
   free_devices (devices);
 }

grub-core/disk/efi/efidisk.c enumerate_disks (void) · line 407

Quoted 10 lines
grub-core/disk/efi/efidisk.c +9 -0
 
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;

Please use NULL instead of 0.

Quoted 16 lines
grub-core/disk/efi/efidisk.c +3 -0
+  enumerate_disks ();
+}
+
 static int
 grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
 		      grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +2 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
   return buffer;
 }
 
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,

grub-core/kern/efi/efi.c grub_efi_locate_handle (grub_efi_locate_search_type_t search_type, · line 110

Quoted 10 lines
grub-core/kern/efi/efi.c +10 -0
+			     grub_efi_handle_t *driver_image_handle,
+			     grub_efi_device_path_protocol_t *remaining_device_path,
+			     grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+		     driver_image_handle, remaining_device_path, recursive);
+}

Please make this function part of the connectefi module.

include/grub/efi/api.h line 82

Quoted 13 lines
grub-core/kern/efi/efi.c +0 -0
 void *
 grub_efi_open_protocol (grub_efi_handle_t handle,
 			grub_guid_t *protocol,
include/grub/efi/api.h +1 -1
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
     { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }
 
-#define GRUB_EFI_DISK_IO_GUID	\
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID	\

This change together with lsefi changes should go to separate patch... But first of all, are they really needed?

include/grub/efi/api.h line 85

Quoted 3 lines
include/grub/efi/api.h +0 -0
   { 0xce345171, 0xba0b, 0x11d2, \
     { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
   }

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw

Re: [PATCH v6 2/2] efi: new 'connectefi' command

Hi Daniel,

Sorry I missed your email, please see inline.

Le 3/21/25 à 3:51 PM, Daniel Kiper a écrit :

Quoted 33 lines · Renaud Métrich via Grub-devel
Quoted 22 lines

When network booting is used, trying to chainload to the local disk (which is used in deployment tools such as Red Hat Satellite) may fail when searching for the boot loader, e.g. /EFI/redhat/shimx64.efi:
the boot loader file is listed, but not readable, because UEFI DISK I/O and/or SCSI DISK I/O devices are not connected.
Sometimes devices are connected but not the partition of the disk matching $prefix, causing partition to not be found by"chainloader".

This issue was seen already on:
- VMWare systems with efi.quickboot enabled (which is the default)
- QEMU/KVM when network booting and not enabling any disk
- Physical hardware such as Cisco and HPE systems

Typical chainload configuration used:
-------- 8< ---------------- 8< ---------------- 8< -------- unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

Quoted 10 lines

This patch introduces a new "connectefi pciroot|disk|all" command which recursively connects all EFI devices starting from a given controller type:
- if 'pciroot' is specified, recursion is performed for all PCI root handles
- if 'disk' is specified, recursion is performed for all SCSI and DISK I/O handles (recommended usage to avoid connecting unwanted handles which may impact Grub performances)
- if 'all' is specified, recursion is performed on all handles (not recommended since it may heavily impact Grub performances)

Quoted 1 line

This has to be documented in the GRUB docs.

OK will do.

Quoted 254 lines

NEWS

Quoted 28 lines

Typical grub.cfg snippet would then be:
-------- 8< ---------------- 8< ---------------- 8< -------- connectefi disk
unset prefix
search --file --set=prefix /EFI/redhat/shimx64.efi if [ -n "$prefix" ]; then
chainloader ($prefix)/EFI/redhat/shimx64.efi
...
-------- 8< ---------------- 8< ---------------- 8< --------

The code is easily extensible to handle other arguments in the future if needed.

---
  NEWS                                |   5 +-
  grub-core/Makefile.core.def         |   6 +
  grub-core/commands/efi/connectefi.c | 212 ++++++++++++++++++++++++++++
  grub-core/commands/efi/lsefi.c      |   3 +-
  grub-core/disk/efi/efidisk.c        |  13 ++
  grub-core/kern/efi/efi.c            |  13 ++
  include/grub/efi/api.h              |   2 +-
  include/grub/efi/disk.h             |   2 +
  include/grub/efi/efi.h              |   5 +
  9 files changed, 258 insertions(+), 3 deletions(-)

create mode 100644 grub-core/commands/efi/connectefi.c

NEWS +0 -0

Please drop NEWS file change.

Quoted 45 lines

index 310130962..48d9dd4dd 100644

NEWS +4 -1
@@ -32,6 +32,9 @@ New in 2.06:
  * BootHole and BootHole2 fixes.
  * ...and tons of other fixes and cleanups...
 
+* New/improved platform support:
+  * New `connectefi' command on EFI platforms.
+
  New in 2.04:
 
  * GCC 8 and 9 support.
@@ -118,7 +121,7 @@ New in 2.02:
    * Prefer pmtimer for TSC calibration.
 
  * New/improved platform support:
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
+  * New `efifwsetup', `lsefi' commands on EFI platforms.
    * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
    * New command `pcidump' for PCI platforms.
    * Improve opcode parsing in ACPI halt implementation.
grub-core/Makefile.core.def +6 -0
index f70e02e69..55e43ea88 100644
@@ -840,6 +840,12 @@ module = {
    enable = efi;
  };
 
+module = {
+  name = connectefi;
+  efi = commands/efi/connectefi.c;
+  enable = efi;
+};
+
  module = {
    name = blocklist;
    common = commands/blocklist.c;
grub-core/commands/efi/connectefi.c new file +0 -0
new file mode 100644
index 000000000..5049c2abe

grub-core/commands/efi/connectefi.c line 3

Quoted 3 lines
grub-core/commands/efi/connectefi.c new file +3 -0
@@ -0,0 +1,212 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.

s/2022/2025/

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or

grub-core/commands/efi/connectefi.c line 17

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */

Please add an empty line here.

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+#include <grub/types.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/efi/api.h>
+#include <grub/efi/pci.h>
+#include <grub/efi/efi.h>
+#include <grub/command.h>
+#include <grub/err.h>
+#include <grub/i18n.h>
+

grub-core/commands/efi/connectefi.c line 37

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+GRUB_MOD_LICENSE ("GPLv3+");
+
+struct grub_efi_already_handled
+{
+  struct grub_efi_already_handled *next;
+  struct grub_efi_already_handled **prev;
+  grub_efi_handle_t handle;
+};
+
+static struct grub_efi_already_handled *already_handled;

This probably does not need to be global.

Quoted 6 lines
grub-core/commands/efi/connectefi.c new file +6 -0
+static struct grub_efi_already_handled *
+is_in_list (grub_efi_handle_t handle)
+{
+  struct grub_efi_already_handled *e;
+
+  FOR_LIST_ELEMENTS (e, already_handled)

grub-core/commands/efi/connectefi.c line 54

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    if (e->handle == handle)
+      return e;
+
+  return NULL;
+}
+
+static void
+free_handle_list (void)
+{
+  struct grub_efi_already_handled *e;

Please add an empty line here.

grub-core/commands/efi/connectefi.c line 62

Quoted 8 lines
grub-core/commands/efi/connectefi.c new file +8 -0
+  while ((e = already_handled) != NULL)
+    {
+      already_handled = already_handled->next;
+      grub_free (e);
+    }
+}
+
+typedef enum searched_item_flag

enum searched_item_flag

grub-core/commands/efi/connectefi.c line 66

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+{
+  SEARCHED_ITEM_FLAG_LOOP = 1,
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
+} searched_item_flags;

typedef enum searched_item_flag searched_item_flag_t;

Please fix similar issues everywhere.

grub-core/commands/efi/connectefi.c line 79

Quoted 12 lines
grub-core/commands/efi/connectefi.c new file +12 -0
+typedef struct searched_item
+{
+  grub_efi_guid_t guid;
+  const char *name;
+  searched_item_flags flags;
+} searched_items;
+
+static grub_err_t
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
+		     int argc, char **args)
+{
+  unsigned s;

s/unsigned/unsigned int/

And below please...

grub-core/commands/efi/connectefi.c line 92

Quoted 13 lines
grub-core/commands/efi/connectefi.c new file +13 -0
+  searched_items pciroot_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items disk_items[] =
+    {
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O", SEARCHED_ITEM_FLAG_RECURSIVE },
+      { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "DISK I/O", SEARCHED_ITEM_FLAG_RECURSIVE }
+    };
+  searched_items *items = NULL;
+  unsigned nitems = 0;

Probably an int is good enough here.

grub-core/commands/efi/connectefi.c line 94

Quoted 2 lines
grub-core/commands/efi/connectefi.c new file +2 -0
+  grub_err_t grub_err = GRUB_ERR_NONE;
+  unsigned total_connected = 0;

Ditto.

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+  if (grub_strcmp(args[0], "pciroot") == 0)

grub-core/commands/efi/connectefi.c line 109

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+    {
+      items = pciroot_items;
+      nitems = ARRAY_SIZE (pciroot_items);
+    }
+  else if (grub_strcmp(args[0], "disk") == 0)
+    {
+      items = disk_items;
+      nitems = ARRAY_SIZE (disk_items);
+    }
+  else if (grub_strcmp(args[0], N_("all")) == 0)

Please drop N_()...

grub-core/commands/efi/connectefi.c line 116

Quoted 7 lines
grub-core/commands/efi/connectefi.c new file +7 -0
+    {
+      items = NULL;
+      nitems = 1;
+    }
+  else
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+		       N_("unexpected argument `%s'"), args[0]);

unexpected argument: %s

Quoted 27 lines
grub-core/commands/efi/connectefi.c new file +27 -0
+
+  for (s = 0; s < nitems; s++)
+    {
+      grub_efi_handle_t *handles;
+      grub_efi_uintn_t num_handles;
+      unsigned i, connected = 0, loop = 0;
+
+loop:
+      loop++;
+      if (items != NULL)
+	{
+	  grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
+	  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
+					    &items[s].guid, 0, &num_handles);
+	}
+      else
+	handles = grub_efi_locate_handle (GRUB_EFI_ALL_HANDLES,
+					  NULL, NULL, &num_handles);
+
+      if (!handles)
+	continue;
+
+      for (i = 0; i < num_handles; i++)
+	{
+	  grub_efi_handle_t handle = handles[i];
+	  grub_efi_status_t status;
+	  unsigned j;

grub-core/commands/efi/connectefi.c line 153

Quoted 10 lines
grub-core/commands/efi/connectefi.c new file +10 -0
+
+	  /* Skip already handled handles  */
+	  if (is_in_list (handle) != NULL)
+	    {
+	      grub_dprintf ("efi", "  handle %p: already processed\n",
+				   handle);
+	      continue;
+	    }
+
+	  status = grub_efi_connect_controller(handle, NULL, NULL,

Wrong coding style. Missing space before "(".
I can see similar issues in other places too.

Quoted 5 lines

grub-core/commands/efi/connectefi.c line 158

Quoted 5 lines
grub-core/commands/efi/connectefi.c new file +5 -0
+			!items || items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
+	  if (status == GRUB_EFI_SUCCESS)
+	    {
+	      connected++;
+	      total_connected++;

All above OK.

Quoted 2 lines

What is a difference between connected and total_connected? I think both could be bool. And then probably one could be dropped...

It's possible to change to bool, but not drop one of the variable.

"total_connected" (will be renamed as "connected_devices") tracks if connection of devices has been done in the end, and if so, tell Grub to reevaluate its devices since new devices showed up probably.

"connected" tracks devices that were connected for a given iteration loop. If there was some device connected, then it may be required to redo the loop, for example when connecting PCI devices. If there was no device connected, then we can move to next level of iteration (e.g. after PCI devices comes SCSI I/O ones).

See also my answer to Andrew (my previous email from today).

Quoted 17 lines

grub-core/commands/efi/connectefi.c line 162

Quoted 4 lines
grub-core/commands/efi/connectefi.c new file +4 -0
+	      grub_dprintf ("efi", "  handle %p: connected\n", handle);
+	    }
+	  else
+	    grub_dprintf ("efi", "  handle %p: failed to connect (%d)\n",

Please use PRIuGRUB_EFI_UINTN_T instead of %d and...

grub-core/commands/efi/connectefi.c line 163

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+				 handle, (grub_efi_int8_t) status);

... drop grub_efi_int8_t cast...

grub-core/commands/efi/connectefi.c line 165

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+	  struct grub_efi_already_handled *item = grub_malloc(sizeof (*item));

Please do not mix code with variable definitions.

grub-core/commands/efi/connectefi.c line 166

Quoted 1 line
grub-core/commands/efi/connectefi.c new file +1 -0
+	  if (!item)

if (item == NULL)

Please check for NULL explicitly.

Quoted 14 lines

grub-core/commands/efi/connectefi.c line 179

Quoted 13 lines
grub-core/commands/efi/connectefi.c new file +13 -0
+	    break; /* fatal  */
+	  grub_list_push (GRUB_AS_LIST_P (&already_handled), GRUB_AS_LIST (item));
+	  item->handle = handle;
+	}
+
+      grub_free (handles);
+      if (grub_err != GRUB_ERR_NONE)
+	break; /* fatal  */
+
+      if (items && items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
+	{
+	  connected = 0;
+	  goto loop;

I think it is possible to not use goto for the loop here.

See above, IMHO it's required and easier to read with the "goto loop".

Quoted 12 lines
Quoted 12 lines
grub-core/commands/efi/connectefi.c new file +12 -0
+	}
+
+      free_handle_list ();
+    }
+
+  free_handle_list ();
+
+  if (total_connected)
+    grub_efidisk_reenumerate_disks ();
+
+  return grub_err;
+}
Quoted 8 lines

grub-core/commands/efi/connectefi.c line 198

Quoted 7 lines
grub-core/commands/efi/connectefi.c new file +7 -0
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(connectefi)
+{
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
+			       /* TRANSLATORS: only translate 'all' keyword */

Do not translate arguments for commands. It will only make confusion...

OK

Quoted 26 lines
Quoted 26 lines
grub-core/commands/efi/connectefi.c new file +14 -0
+			       N_("pciroot|disk|all"),
+			       N_("Connect EFI handles."
+				  " If 'pciroot' is specified, connect PCI"
+				  " root EFI handles recursively."
+				  " If 'disk' is specified, connect SCSI"
+				  " and DISK I/O EFI handles recursively."
+				  " If 'all' is specified, connect all"
+				  " EFI handles recursively."));
+}
+
+GRUB_MOD_FINI(connectefi)
+{
+  grub_unregister_command (cmd);
+}
grub-core/commands/efi/lsefi.c +1 -0
index bda25a3a9..8a82ababf 100644
@@ -19,6 +19,7 @@
  #include <grub/mm.h>
  #include <grub/misc.h>
  #include <grub/efi/api.h>
+#include <grub/efi/disk.h>
  #include <grub/efi/edid.h>
  #include <grub/efi/pci.h>
  #include <grub/efi/efi.h>
Quoted 8 lines

grub-core/commands/efi/lsefi.c static struct known_protocol · line 39

Quoted 6 lines
grub-core/commands/efi/lsefi.c +1 -1
@@ -35,7 +36,7 @@ static struct known_protocol
    const char *name;
  } known_protocols[] =
    {
-    { GRUB_EFI_DISK_IO_GUID, "disk" },
+    { GRUB_EFI_DISK_IO_PROTOCOL_GUID, "disk" },

Why? This change begs for separate patch with good explanation why the change is needed.

This change is not required, but was made to follow the naming of the UEFI spec version 2.6 (Page 587):
#define EFI_DISK_IO_PROTOCOL_GUID \
{0xCE345171,0xBA0B,0x11d2,\
{0x8e,0x4F,0x00,0xa0,0xc9,0x69,0x72,0x3b}}

I can revert this if needed.

Quoted 11 lines
Quoted 11 lines
grub-core/commands/efi/lsefi.c +0 -0
      { GRUB_EFI_BLOCK_IO_GUID, "block" },
      { GRUB_EFI_SERIAL_IO_GUID, "serial" },
      { GRUB_EFI_SIMPLE_NETWORK_GUID, "network" },
grub-core/disk/efi/efidisk.c +0 -0
index 3b5ed5691..525d59838 100644
@@ -396,6 +396,19 @@ enumerate_disks (void)
    free_devices (devices);
  }
 
Quoted 10 lines

grub-core/disk/efi/efidisk.c enumerate_disks (void) · line 407

Quoted 9 lines
grub-core/disk/efi/efidisk.c +9 -0
+void
+grub_efidisk_reenumerate_disks (void)
+{
+  free_devices (fd_devices);
+  free_devices (hd_devices);
+  free_devices (cd_devices);
+  fd_devices = 0;
+  hd_devices = 0;
+  cd_devices = 0;

Please use NULL instead of 0.

OK

Quoted 14 lines
Quoted 14 lines
grub-core/disk/efi/efidisk.c +3 -0
+  enumerate_disks ();
+}
+
  static int
  grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  		      grub_disk_pull_t pull)
grub-core/kern/efi/efi.c +0 -0
index b93ae3aba..b7c861b1e 100644
@@ -96,6 +96,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
    return buffer;
  }
 
Quoted 13 lines

grub-core/kern/efi/efi.c grub_efi_locate_handle (grub_efi_locate_search_type_t search_type, · line 110

Quoted 12 lines
grub-core/kern/efi/efi.c +12 -0
+grub_efi_status_t
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
+			     grub_efi_handle_t *driver_image_handle,
+			     grub_efi_device_path_protocol_t *remaining_device_path,
+			     grub_efi_boolean_t recursive)
+{
+  grub_efi_boot_services_t *b;
+
+  b = grub_efi_system_table->boot_services;
+  return efi_call_4 (b->connect_controller, controller_handle,
+		     driver_image_handle, remaining_device_path, recursive);
+}

Please make this function part of the connectefi module.

OK I thought that since it was a low-level function wrapper, it would have been better to add it there.

Quoted 11 lines
Quoted 11 lines
grub-core/kern/efi/efi.c +0 -0
  void *
  grub_efi_open_protocol (grub_efi_handle_t handle,
  			grub_guid_t *protocol,
include/grub/efi/api.h +0 -0
index b686e8afe..d73a10c28 100644
@@ -79,7 +79,7 @@
      { 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }
 
Quoted 4 lines

include/grub/efi/api.h line 82

Quoted 2 lines
include/grub/efi/api.h +1 -1
-#define GRUB_EFI_DISK_IO_GUID	\
+#define GRUB_EFI_DISK_IO_PROTOCOL_GUID	\

This change together with lsefi changes should go to separate patch... But first of all, are they really needed?

Same as above: it's to follow the UEFI Spec version 2.6

Quoted 5 lines

include/grub/efi/api.h line 85

Quoted 3 lines
include/grub/efi/api.h +0 -0
    { 0xce345171, 0xba0b, 0x11d2, \
      { 0x8e, 0x4f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
    }

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by on v6 1/2
#7 lore raw
In reply to Renaud Métrich via Grub-devel

Hello,

Looks good to me, thanks!

- Andrew

Quoted 27 lines · Renaud Métrich via Grub-devel
---
 grub-core/commands/efi/lsefi.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/commands/efi/lsefi.c +4 -1
index 7b8316d41..bda25a3a9 100644
@@ -127,8 +127,12 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
            grub_printf ("  %pG\n", protocols[j]);
        }
 
+      if (protocols)
+       grub_efi_free_pool (protocols);
     }
 
+  grub_free (handles);
+
   return 0;
 }
 
--

2.48.1

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#9 lore raw
In reply to Renaud Métrich via Grub-devel
Quoted 10 lines · Renaud Métrich via Grub-devel
---
 grub-core/commands/efi/lsefi.c | 4 ++++
 1 file changed, 4 insertions(+)
grub-core/commands/efi/lsefi.c +0 -0
index 7b8316d41..bda25a3a9 100644

grub-core/commands/efi/lsefi.c grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)), · line 131

Quoted 5 lines
grub-core/commands/efi/lsefi.c +2 -0
@@ -127,8 +127,12 @@ grub_cmd_lsefi (grub_command_t cmd __attribute__ ((unused)),
 	    grub_printf ("  %pG\n", protocols[j]);
 	}
 
+      if (protocols)
+	grub_efi_free_pool (protocols);

Again, the grub_efi_free_pool() does not exist in upstream. I pointed out this issue in v3 and it is still not fixed... :-(

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel