nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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