nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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