Blame SOURCES/0493-search-new-efidisk-only-option-on-EFI-systems.patch

b71686
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b71686
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
b71686
Date: Tue, 8 Feb 2022 08:39:11 +0100
b71686
Subject: [PATCH] search: new --efidisk-only option on EFI systems
b71686
MIME-Version: 1.0
b71686
Content-Type: text/plain; charset=UTF-8
b71686
Content-Transfer-Encoding: 8bit
b71686
b71686
When using 'search' on EFI systems, we sometimes want to exclude devices
b71686
that are not EFI disks (e.g. md, lvm).
b71686
This is typically used when wanting to chainload when having a software
b71686
raid (md) for EFI partition:
b71686
with no option, 'search --file /EFI/redhat/shimx64.efi' sets root envvar
b71686
to 'md/boot_efi' which cannot be used for chainloading since there is no
b71686
effective EFI device behind.
b71686
b71686
This commit also refactors handling of --no-floppy option.
b71686
b71686
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
b71686
[rharwood: apply rmetrich's flags initialization fix]
b71686
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
b71686
(cherry picked from commit fdd8396f4fa750bbbabd4298f2593942f2b84710)
b71686
(cherry picked from commit bea473b58726705bb83a3db88f52d46fdcc6150e)
b71686
---
b71686
 grub-core/commands/search.c      | 27 +++++++++++++++++++++++----
b71686
 grub-core/commands/search_wrap.c | 18 ++++++++++++------
b71686
 include/grub/search.h            | 15 ++++++++++++---
b71686
 3 files changed, 47 insertions(+), 13 deletions(-)
b71686
b71686
diff --git a/grub-core/commands/search.c b/grub-core/commands/search.c
b71686
index d3180bf66..2036a756b 100644
b71686
--- a/grub-core/commands/search.c
b71686
+++ b/grub-core/commands/search.c
b71686
@@ -47,7 +47,7 @@ struct search_ctx
b71686
 {
b71686
   const char *key;
b71686
   const char *var;
b71686
-  int no_floppy;
b71686
+  enum search_flags flags;
b71686
   char **hints;
b71686
   unsigned nhints;
b71686
   int count;
b71686
@@ -62,10 +62,29 @@ iterate_device (const char *name, void *data)
b71686
   int found = 0;
b71686
 
b71686
   /* Skip floppy drives when requested.  */
b71686
-  if (ctx->no_floppy &&
b71686
+  if (ctx->flags & SEARCH_FLAGS_NO_FLOPPY &&
b71686
       name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
b71686
     return 0;
b71686
 
b71686
+  /* Limit to EFI disks when requested.  */
b71686
+  if (ctx->flags & SEARCH_FLAGS_EFIDISK_ONLY)
b71686
+    {
b71686
+      grub_device_t dev;
b71686
+      dev = grub_device_open (name);
b71686
+      if (! dev)
b71686
+	{
b71686
+	  grub_errno = GRUB_ERR_NONE;
b71686
+	  return 0;
b71686
+	}
b71686
+      if (! dev->disk || dev->disk->dev->id != GRUB_DISK_DEVICE_EFIDISK_ID)
b71686
+	{
b71686
+	  grub_device_close (dev);
b71686
+	  grub_errno = GRUB_ERR_NONE;
b71686
+	  return 0;
b71686
+	}
b71686
+      grub_device_close (dev);
b71686
+    }
b71686
+
b71686
 #ifdef DO_SEARCH_FS_UUID
b71686
 #define compare_fn grub_strcasecmp
b71686
 #else
b71686
@@ -261,13 +280,13 @@ try (struct search_ctx *ctx)
b71686
 }
b71686
 
b71686
 void
b71686
-FUNC_NAME (const char *key, const char *var, int no_floppy,
b71686
+FUNC_NAME (const char *key, const char *var, enum search_flags flags,
b71686
 	   char **hints, unsigned nhints)
b71686
 {
b71686
   struct search_ctx ctx = {
b71686
     .key = key,
b71686
     .var = var,
b71686
-    .no_floppy = no_floppy,
b71686
+    .flags = flags,
b71686
     .hints = hints,
b71686
     .nhints = nhints,
b71686
     .count = 0,
b71686
diff --git a/grub-core/commands/search_wrap.c b/grub-core/commands/search_wrap.c
b71686
index 47fc8eb99..0b62acf85 100644
b71686
--- a/grub-core/commands/search_wrap.c
b71686
+++ b/grub-core/commands/search_wrap.c
b71686
@@ -40,6 +40,7 @@ static const struct grub_arg_option options[] =
b71686
      N_("Set a variable to the first device found."), N_("VARNAME"),
b71686
      ARG_TYPE_STRING},
b71686
     {"no-floppy",	'n', 0, N_("Do not probe any floppy drive."), 0, 0},
b71686
+    {"efidisk-only",	0, 0, N_("Only probe EFI disks."), 0, 0},
b71686
     {"hint",	        'h', GRUB_ARG_OPTION_REPEATABLE,
b71686
      N_("First try the device HINT. If HINT ends in comma, "
b71686
 	"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
b71686
@@ -73,6 +74,7 @@ enum options
b71686
     SEARCH_FS_UUID,
b71686
     SEARCH_SET,
b71686
     SEARCH_NO_FLOPPY,
b71686
+    SEARCH_EFIDISK_ONLY,
b71686
     SEARCH_HINT,
b71686
     SEARCH_HINT_IEEE1275,
b71686
     SEARCH_HINT_BIOS,
b71686
@@ -89,6 +91,7 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
b71686
   const char *id = 0;
b71686
   int i = 0, j = 0, nhints = 0;
b71686
   char **hints = NULL;
b71686
+  enum search_flags flags = 0;
b71686
 
b71686
   if (state[SEARCH_HINT].set)
b71686
     for (i = 0; state[SEARCH_HINT].args[i]; i++)
b71686
@@ -180,15 +183,18 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
b71686
       goto out;
b71686
     }
b71686
 
b71686
+  if (state[SEARCH_NO_FLOPPY].set)
b71686
+    flags |= SEARCH_FLAGS_NO_FLOPPY;
b71686
+
b71686
+  if (state[SEARCH_EFIDISK_ONLY].set)
b71686
+    flags |= SEARCH_FLAGS_EFIDISK_ONLY;
b71686
+
b71686
   if (state[SEARCH_LABEL].set)
b71686
-    grub_search_label (id, var, state[SEARCH_NO_FLOPPY].set, 
b71686
-		       hints, nhints);
b71686
+    grub_search_label (id, var, flags, hints, nhints);
b71686
   else if (state[SEARCH_FS_UUID].set)
b71686
-    grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set,
b71686
-			 hints, nhints);
b71686
+    grub_search_fs_uuid (id, var, flags, hints, nhints);
b71686
   else if (state[SEARCH_FILE].set)
b71686
-    grub_search_fs_file (id, var, state[SEARCH_NO_FLOPPY].set, 
b71686
-			 hints, nhints);
b71686
+    grub_search_fs_file (id, var, flags, hints, nhints);
b71686
   else
b71686
     grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
b71686
 
b71686
diff --git a/include/grub/search.h b/include/grub/search.h
b71686
index d80347df3..4190aeb2c 100644
b71686
--- a/include/grub/search.h
b71686
+++ b/include/grub/search.h
b71686
@@ -19,11 +19,20 @@
b71686
 #ifndef GRUB_SEARCH_HEADER
b71686
 #define GRUB_SEARCH_HEADER 1
b71686
 
b71686
-void grub_search_fs_file (const char *key, const char *var, int no_floppy,
b71686
+enum search_flags
b71686
+  {
b71686
+    SEARCH_FLAGS_NO_FLOPPY	= 1,
b71686
+    SEARCH_FLAGS_EFIDISK_ONLY	= 2
b71686
+  };
b71686
+
b71686
+void grub_search_fs_file (const char *key, const char *var,
b71686
+			  enum search_flags flags,
b71686
 			  char **hints, unsigned nhints);
b71686
-void grub_search_fs_uuid (const char *key, const char *var, int no_floppy,
b71686
+void grub_search_fs_uuid (const char *key, const char *var,
b71686
+			  enum search_flags flags,
b71686
 			  char **hints, unsigned nhints);
b71686
-void grub_search_label (const char *key, const char *var, int no_floppy,
b71686
+void grub_search_label (const char *key, const char *var,
b71686
+			enum search_flags flags,
b71686
 			char **hints, unsigned nhints);
b71686
 
b71686
 #endif