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

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