nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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