f6e916
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f6e916
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
f6e916
Date: Tue, 15 Feb 2022 14:05:22 +0100
f6e916
Subject: [PATCH] efi: new 'connectefi' command
f6e916
MIME-Version: 1.0
f6e916
Content-Type: text/plain; charset=UTF-8
f6e916
Content-Transfer-Encoding: 8bit
f6e916
f6e916
When efi.quickboot is enabled on VMWare (which is the default for
f6e916
hardware release 16 and later), it may happen that not all EFI devices
f6e916
are connected. Due to this, browsing the devices in make_devices() just
f6e916
fails to find devices, in particular disks or partitions for a given
f6e916
disk.
f6e916
This typically happens when network booting, then trying to chainload to
f6e916
local disk (this is used in deployment tools such as Red Hat Satellite),
f6e916
which is done through using the following grub.cfg snippet:
f6e916
-------- 8< ---------------- 8< ---------------- 8< --------
f6e916
unset prefix
f6e916
search --file --set=prefix /EFI/redhat/grubx64.efi
f6e916
if [ -n "$prefix" ]; then
f6e916
  chainloader ($prefix)/EFI/redhat/grubx64/efi
f6e916
...
f6e916
-------- 8< ---------------- 8< ---------------- 8< --------
f6e916
f6e916
With efi.quickboot, none of the devices are connected, causing "search"
f6e916
to fail. Sometimes devices are connected but not the partition of the
f6e916
disk matching $prefix, causing partition to not be found by
f6e916
"chainloader".
f6e916
f6e916
This patch introduces a new "connectefi pciroot|scsi" command which
f6e916
recursively connects all EFI devices starting from a given controller
f6e916
type:
f6e916
- if 'pciroot' is specified, recursion is performed for all PCI root
f6e916
  handles
f6e916
- if 'scsi' is specified, recursion is performed for all SCSI I/O
f6e916
  handles (recommended usage to avoid connecting unwanted handles which
f6e916
  may impact Grub performances)
f6e916
f6e916
Typical grub.cfg snippet would then be:
f6e916
-------- 8< ---------------- 8< ---------------- 8< --------
f6e916
connectefi scsi
f6e916
unset prefix
f6e916
search --file --set=prefix /EFI/redhat/grubx64.efi
f6e916
if [ -n "$prefix" ]; then
f6e916
  chainloader ($prefix)/EFI/redhat/grubx64/efi
f6e916
...
f6e916
-------- 8< ---------------- 8< ---------------- 8< --------
f6e916
f6e916
The code is easily extensible to handle other arguments in the future if
f6e916
needed.
f6e916
f6e916
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
f6e916
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
f6e916
(cherry picked from commit cc972c27314c841f80ab0fe8318fae06f078c680)
f6e916
(cherry picked from commit 84b0c3f965a3918be64ca850139bea7c32d23ae9)
f6e916
---
f6e916
 grub-core/Makefile.core.def         |   6 ++
f6e916
 grub-core/commands/efi/connectefi.c | 205 ++++++++++++++++++++++++++++++++++++
f6e916
 grub-core/commands/efi/lsefi.c      |   1 +
f6e916
 grub-core/disk/efi/efidisk.c        |  13 +++
f6e916
 grub-core/kern/efi/efi.c            |  13 +++
f6e916
 include/grub/efi/disk.h             |   2 +
f6e916
 include/grub/efi/efi.h              |   5 +
f6e916
 NEWS                                |   2 +-
f6e916
 8 files changed, 246 insertions(+), 1 deletion(-)
f6e916
 create mode 100644 grub-core/commands/efi/connectefi.c
f6e916
f6e916
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
f6e916
index 612df2e9c..ef06f8c95 100644
f6e916
--- a/grub-core/Makefile.core.def
f6e916
+++ b/grub-core/Makefile.core.def
f6e916
@@ -779,6 +779,12 @@ module = {
f6e916
   enable = efi;
f6e916
 };
f6e916
 
f6e916
+module = {
f6e916
+  name = connectefi;
f6e916
+  common = commands/efi/connectefi.c;
f6e916
+  enable = efi;
f6e916
+};
f6e916
+
f6e916
 module = {
f6e916
   name = blocklist;
f6e916
   common = commands/blocklist.c;
f6e916
diff --git a/grub-core/commands/efi/connectefi.c b/grub-core/commands/efi/connectefi.c
f6e916
new file mode 100644
f6e916
index 000000000..8ab75bd51
f6e916
--- /dev/null
f6e916
+++ b/grub-core/commands/efi/connectefi.c
f6e916
@@ -0,0 +1,205 @@
f6e916
+/*
f6e916
+ *  GRUB  --  GRand Unified Bootloader
f6e916
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
f6e916
+ *
f6e916
+ *  GRUB is free software: you can redistribute it and/or modify
f6e916
+ *  it under the terms of the GNU General Public License as published by
f6e916
+ *  the Free Software Foundation, either version 3 of the License, or
f6e916
+ *  (at your option) any later version.
f6e916
+ *
f6e916
+ *  GRUB is distributed in the hope that it will be useful,
f6e916
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
f6e916
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f6e916
+ *  GNU General Public License for more details.
f6e916
+ *
f6e916
+ *  You should have received a copy of the GNU General Public License
f6e916
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
f6e916
+ */
f6e916
+#include <grub/types.h>
f6e916
+#include <grub/mm.h>
f6e916
+#include <grub/misc.h>
f6e916
+#include <grub/efi/api.h>
f6e916
+#include <grub/efi/pci.h>
f6e916
+#include <grub/efi/efi.h>
f6e916
+#include <grub/command.h>
f6e916
+#include <grub/err.h>
f6e916
+#include <grub/i18n.h>
f6e916
+
f6e916
+GRUB_MOD_LICENSE ("GPLv3+");
f6e916
+
f6e916
+typedef struct handle_list
f6e916
+{
f6e916
+  grub_efi_handle_t handle;
f6e916
+  struct handle_list *next;
f6e916
+} handle_list_t;
f6e916
+
f6e916
+static handle_list_t *already_handled = NULL;
f6e916
+
f6e916
+static grub_err_t
f6e916
+add_handle (grub_efi_handle_t handle)
f6e916
+{
f6e916
+  handle_list_t *e;
f6e916
+  e = grub_malloc (sizeof (*e));
f6e916
+  if (! e)
f6e916
+    return grub_errno;
f6e916
+  e->handle = handle;
f6e916
+  e->next = already_handled;
f6e916
+  already_handled = e;
f6e916
+  return GRUB_ERR_NONE;
f6e916
+}
f6e916
+
f6e916
+static int
f6e916
+is_in_list (grub_efi_handle_t handle)
f6e916
+{
f6e916
+  handle_list_t *e;
f6e916
+  for (e = already_handled; e != NULL; e = e->next)
f6e916
+    if (e->handle == handle)
f6e916
+      return 1;
f6e916
+  return 0;
f6e916
+}
f6e916
+
f6e916
+static void
f6e916
+free_handle_list (void)
f6e916
+{
f6e916
+  handle_list_t *e;
f6e916
+  while ((e = already_handled) != NULL)
f6e916
+    {
f6e916
+      already_handled = already_handled->next;
f6e916
+      grub_free (e);
f6e916
+    }
f6e916
+}
f6e916
+
f6e916
+typedef enum searched_item_flag
f6e916
+{
f6e916
+  SEARCHED_ITEM_FLAG_LOOP = 1,
f6e916
+  SEARCHED_ITEM_FLAG_RECURSIVE = 2
f6e916
+} searched_item_flags;
f6e916
+
f6e916
+typedef struct searched_item
f6e916
+{
f6e916
+  grub_efi_guid_t guid;
f6e916
+  const char *name;
f6e916
+  searched_item_flags flags;
f6e916
+} searched_items;
f6e916
+
f6e916
+static grub_err_t
f6e916
+grub_cmd_connectefi (grub_command_t cmd __attribute__ ((unused)),
f6e916
+		     int argc, char **args)
f6e916
+{
f6e916
+  unsigned s;
f6e916
+  searched_items pciroot_items[] =
f6e916
+    {
f6e916
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", SEARCHED_ITEM_FLAG_RECURSIVE }
f6e916
+    };
f6e916
+  searched_items scsi_items[] =
f6e916
+    {
f6e916
+      { GRUB_EFI_PCI_ROOT_IO_GUID, "PCI root", 0 },
f6e916
+      { GRUB_EFI_PCI_IO_GUID, "PCI", SEARCHED_ITEM_FLAG_LOOP },
f6e916
+      { GRUB_EFI_SCSI_IO_PROTOCOL_GUID, "SCSI I/O", SEARCHED_ITEM_FLAG_RECURSIVE }
f6e916
+    };
f6e916
+  searched_items *items = NULL;
f6e916
+  unsigned nitems = 0;
f6e916
+  grub_err_t grub_err = GRUB_ERR_NONE;
f6e916
+  unsigned total_connected = 0;
f6e916
+
f6e916
+  if (argc != 1)
f6e916
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
f6e916
+
f6e916
+  if (grub_strcmp(args[0], N_("pciroot")) == 0)
f6e916
+    {
f6e916
+      items = pciroot_items;
f6e916
+      nitems = ARRAY_SIZE (pciroot_items);
f6e916
+    }
f6e916
+  else if (grub_strcmp(args[0], N_("scsi")) == 0)
f6e916
+    {
f6e916
+      items = scsi_items;
f6e916
+      nitems = ARRAY_SIZE (scsi_items);
f6e916
+    }
f6e916
+  else
f6e916
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
f6e916
+		       N_("unexpected argument `%s'"), args[0]);
f6e916
+
f6e916
+  for (s = 0; s < nitems; s++)
f6e916
+    {
f6e916
+      grub_efi_handle_t *handles;
f6e916
+      grub_efi_uintn_t num_handles;
f6e916
+      unsigned i, connected = 0, loop = 0;
f6e916
+
f6e916
+loop:
f6e916
+      loop++;
f6e916
+      grub_dprintf ("efi", "step '%s' loop %d:\n", items[s].name, loop);
f6e916
+
f6e916
+      handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
f6e916
+					&items[s].guid, 0, &num_handles);
f6e916
+
f6e916
+      if (!handles)
f6e916
+	continue;
f6e916
+
f6e916
+      for (i = 0; i < num_handles; i++)
f6e916
+	{
f6e916
+	  grub_efi_handle_t handle = handles[i];
f6e916
+	  grub_efi_status_t status;
f6e916
+	  unsigned j;
f6e916
+
f6e916
+	  /* Skip already handled handles  */
f6e916
+	  if (is_in_list (handle))
f6e916
+	    {
f6e916
+	      grub_dprintf ("efi", "  handle %p: already processed\n",
f6e916
+				   handle);
f6e916
+	      continue;
f6e916
+	    }
f6e916
+
f6e916
+	  status = grub_efi_connect_controller(handle, NULL, NULL,
f6e916
+			items[s].flags & SEARCHED_ITEM_FLAG_RECURSIVE ? 1 : 0);
f6e916
+	  if (status == GRUB_EFI_SUCCESS)
f6e916
+	    {
f6e916
+	      connected++;
f6e916
+	      total_connected++;
f6e916
+	      grub_dprintf ("efi", "  handle %p: connected\n", handle);
f6e916
+	    }
f6e916
+	  else
f6e916
+	    grub_dprintf ("efi", "  handle %p: failed to connect (%d)\n",
f6e916
+				 handle, (grub_efi_int8_t) status);
f6e916
+
f6e916
+	  if ((grub_err = add_handle (handle)) != GRUB_ERR_NONE)
f6e916
+	    break; /* fatal  */
f6e916
+	}
f6e916
+
f6e916
+      grub_free (handles);
f6e916
+      if (grub_err != GRUB_ERR_NONE)
f6e916
+	break; /* fatal  */
f6e916
+
f6e916
+      if (items[s].flags & SEARCHED_ITEM_FLAG_LOOP && connected)
f6e916
+	{
f6e916
+	  connected = 0;
f6e916
+	  goto loop;
f6e916
+	}
f6e916
+
f6e916
+      free_handle_list ();
f6e916
+    }
f6e916
+
f6e916
+  free_handle_list ();
f6e916
+
f6e916
+  if (total_connected)
f6e916
+    grub_efidisk_reenumerate_disks ();
f6e916
+
f6e916
+  return grub_err;
f6e916
+}
f6e916
+
f6e916
+static grub_command_t cmd;
f6e916
+
f6e916
+GRUB_MOD_INIT(connectefi)
f6e916
+{
f6e916
+  cmd = grub_register_command ("connectefi", grub_cmd_connectefi,
f6e916
+			       N_("pciroot|scsi"),
f6e916
+			       N_("Connect EFI handles."
f6e916
+				  " If 'pciroot' is specified, connect PCI"
f6e916
+				  " root EFI handles recursively."
f6e916
+				  " If 'scsi' is specified, connect SCSI"
f6e916
+				  " I/O EFI handles recursively."));
f6e916
+}
f6e916
+
f6e916
+GRUB_MOD_FINI(connectefi)
f6e916
+{
f6e916
+  grub_unregister_command (cmd);
f6e916
+}
f6e916
diff --git a/grub-core/commands/efi/lsefi.c b/grub-core/commands/efi/lsefi.c
f6e916
index d1ce99af4..f2d2430e6 100644
f6e916
--- a/grub-core/commands/efi/lsefi.c
f6e916
+++ b/grub-core/commands/efi/lsefi.c
f6e916
@@ -19,6 +19,7 @@
f6e916
 #include <grub/mm.h>
f6e916
 #include <grub/misc.h>
f6e916
 #include <grub/efi/api.h>
f6e916
+#include <grub/efi/disk.h>
f6e916
 #include <grub/efi/edid.h>
f6e916
 #include <grub/efi/pci.h>
f6e916
 #include <grub/efi/efi.h>
f6e916
diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c
f6e916
index 5d2400f66..720a23fcc 100644
f6e916
--- a/grub-core/disk/efi/efidisk.c
f6e916
+++ b/grub-core/disk/efi/efidisk.c
f6e916
@@ -390,6 +390,19 @@ enumerate_disks (void)
f6e916
   free_devices (devices);
f6e916
 }
f6e916
 
f6e916
+void
f6e916
+grub_efidisk_reenumerate_disks (void)
f6e916
+{
f6e916
+  free_devices (fd_devices);
f6e916
+  free_devices (hd_devices);
f6e916
+  free_devices (cd_devices);
f6e916
+  fd_devices = 0;
f6e916
+  hd_devices = 0;
f6e916
+  cd_devices = 0;
f6e916
+
f6e916
+  enumerate_disks ();
f6e916
+}
f6e916
+
f6e916
 static int
f6e916
 grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
f6e916
 		      grub_disk_pull_t pull)
f6e916
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
f6e916
index 4b95a4004..286395645 100644
f6e916
--- a/grub-core/kern/efi/efi.c
f6e916
+++ b/grub-core/kern/efi/efi.c
f6e916
@@ -95,6 +95,19 @@ grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
f6e916
   return buffer;
f6e916
 }
f6e916
 
f6e916
+grub_efi_status_t
f6e916
+grub_efi_connect_controller (grub_efi_handle_t controller_handle,
f6e916
+			     grub_efi_handle_t *driver_image_handle,
f6e916
+			     grub_efi_device_path_protocol_t *remaining_device_path,
f6e916
+			     grub_efi_boolean_t recursive)
f6e916
+{
f6e916
+  grub_efi_boot_services_t *b;
f6e916
+
f6e916
+  b = grub_efi_system_table->boot_services;
f6e916
+  return efi_call_4 (b->connect_controller, controller_handle,
f6e916
+		     driver_image_handle, remaining_device_path, recursive);
f6e916
+}
f6e916
+
f6e916
 void *
f6e916
 grub_efi_open_protocol (grub_efi_handle_t handle,
f6e916
 			grub_efi_guid_t *protocol,
f6e916
diff --git a/include/grub/efi/disk.h b/include/grub/efi/disk.h
f6e916
index 254475c84..6845c2f1f 100644
f6e916
--- a/include/grub/efi/disk.h
f6e916
+++ b/include/grub/efi/disk.h
f6e916
@@ -27,6 +27,8 @@ grub_efi_handle_t
f6e916
 EXPORT_FUNC(grub_efidisk_get_device_handle) (grub_disk_t disk);
f6e916
 char *EXPORT_FUNC(grub_efidisk_get_device_name) (grub_efi_handle_t *handle);
f6e916
 
f6e916
+void EXPORT_FUNC(grub_efidisk_reenumerate_disks) (void);
f6e916
+
f6e916
 void grub_efidisk_init (void);
f6e916
 void grub_efidisk_fini (void);
f6e916
 
f6e916
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
f6e916
index 570a69361..4411ffa16 100644
f6e916
--- a/include/grub/efi/efi.h
f6e916
+++ b/include/grub/efi/efi.h
f6e916
@@ -36,6 +36,11 @@ EXPORT_FUNC(grub_efi_locate_handle) (grub_efi_locate_search_type_t search_type,
f6e916
 				     grub_efi_guid_t *protocol,
f6e916
 				     void *search_key,
f6e916
 				     grub_efi_uintn_t *num_handles);
f6e916
+grub_efi_status_t
f6e916
+EXPORT_FUNC(grub_efi_connect_controller) (grub_efi_handle_t controller_handle,
f6e916
+					  grub_efi_handle_t *driver_image_handle,
f6e916
+					  grub_efi_device_path_protocol_t *remaining_device_path,
f6e916
+					  grub_efi_boolean_t recursive);
f6e916
 void *EXPORT_FUNC(grub_efi_open_protocol) (grub_efi_handle_t handle,
f6e916
 					   grub_efi_guid_t *protocol,
f6e916
 					   grub_efi_uint32_t attributes);
f6e916
diff --git a/NEWS b/NEWS
f6e916
index 2ebd54e78..b04041507 100644
f6e916
--- a/NEWS
f6e916
+++ b/NEWS
f6e916
@@ -66,7 +66,7 @@ New in 2.02:
f6e916
   * Prefer pmtimer for TSC calibration.
f6e916
 
f6e916
 * New/improved platform support:
f6e916
-  * New `efifwsetup' and `lsefi' commands on EFI platforms.
f6e916
+  * New `efifwsetup', `lsefi' and `connectefi` commands on EFI platforms.
f6e916
   * New `cmosdump' and `cmosset' commands on platforms with CMOS support.
f6e916
   * New command `pcidump' for PCI platforms.
f6e916
   * Improve opcode parsing in ACPI halt implementation.