67f07b7
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
67f07b7
From: Javier Martinez Canillas <javierm@redhat.com>
67f07b7
Date: Tue, 6 Jul 2021 01:10:18 +0200
67f07b7
Subject: [PATCH] efi: Print an error if boot to firmware setup is not
67f07b7
 supported
67f07b7
67f07b7
The "fwsetup" command is only registered if the firmware supports booting
67f07b7
to the firmware setup UI. But it could be possible that the GRUB config
67f07b7
already contains a "fwsetup" entry, because it was generated in a machine
67f07b7
that has support for this feature.
67f07b7
67f07b7
To prevent users getting a "can't find command `fwsetup`" error if it is
67f07b7
not supported by the firmware, let's just always register the command but
67f07b7
print a more accurate message if the firmware doesn't support this option.
67f07b7
67f07b7
Resolves: rhbz#1823864
67f07b7
67f07b7
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
67f07b7
---
67f07b7
 grub-core/commands/efi/efifwsetup.c | 43 ++++++++++++++++++++-----------------
67f07b7
 1 file changed, 23 insertions(+), 20 deletions(-)
67f07b7
67f07b7
diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
e622855
index eaca032838..328c45e82e 100644
67f07b7
--- a/grub-core/commands/efi/efifwsetup.c
67f07b7
+++ b/grub-core/commands/efi/efifwsetup.c
67f07b7
@@ -27,6 +27,25 @@
67f07b7
 
67f07b7
 GRUB_MOD_LICENSE ("GPLv3+");
67f07b7
 
67f07b7
+static grub_efi_boolean_t
67f07b7
+efifwsetup_is_supported (void)
67f07b7
+{
67f07b7
+  grub_efi_uint64_t *os_indications_supported = NULL;
67f07b7
+  grub_size_t oi_size = 0;
67f07b7
+  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
67f07b7
+
67f07b7
+  grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
67f07b7
+			 (void **) &os_indications_supported);
67f07b7
+
67f07b7
+  if (!os_indications_supported)
67f07b7
+    return 0;
67f07b7
+
67f07b7
+  if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
67f07b7
+    return 1;
67f07b7
+
67f07b7
+  return 0;
67f07b7
+}
67f07b7
+
67f07b7
 static grub_err_t
67f07b7
 grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
67f07b7
 		  int argc __attribute__ ((unused)),
67f07b7
@@ -38,6 +57,10 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
67f07b7
   grub_size_t oi_size;
67f07b7
   grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
67f07b7
 
67f07b7
+  if (!efifwsetup_is_supported ())
67f07b7
+	  return grub_error (GRUB_ERR_INVALID_COMMAND,
67f07b7
+			     N_("Reboot to firmware setup is not supported"));
67f07b7
+
67f07b7
   grub_efi_get_variable ("OsIndications", &global, &oi_size,
67f07b7
 			 (void **) &old_os_indications);
67f07b7
 
67f07b7
@@ -56,28 +79,8 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
67f07b7
 
67f07b7
 static grub_command_t cmd = NULL;
67f07b7
 
67f07b7
-static grub_efi_boolean_t
67f07b7
-efifwsetup_is_supported (void)
67f07b7
-{
67f07b7
-  grub_efi_uint64_t *os_indications_supported = NULL;
67f07b7
-  grub_size_t oi_size = 0;
67f07b7
-  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
67f07b7
-
67f07b7
-  grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
67f07b7
-			 (void **) &os_indications_supported);
67f07b7
-
67f07b7
-  if (!os_indications_supported)
67f07b7
-    return 0;
67f07b7
-
67f07b7
-  if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
67f07b7
-    return 1;
67f07b7
-
67f07b7
-  return 0;
67f07b7
-}
67f07b7
-
67f07b7
 GRUB_MOD_INIT (efifwsetup)
67f07b7
 {
67f07b7
-  if (efifwsetup_is_supported ())
67f07b7
     cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
67f07b7
 				 N_("Reboot into firmware setup menu."));
67f07b7