Skip to contentSkip to conversation

Improve logic to check for fwsetup support

PATCH Superseded Robbie Harwood 1 participant 6 messages Aug 18, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Improve logic to check for fwsetup support

In this version: add and check `fwsetup --is-supported, and include the changes for Paul's review that were left out accidentally in the previous version.

Be well,
--Robbie

Javier Martinez Canillas (2):
templates: Check for EFI at runtime instead of config generation time
efi: Print an error if boot to firmware setup is not supported

Robbie Harwood (3):
commands/efi/efifwsetup: Add missing grub_free()s Make all grub_efi_guid_t variables static
Don't display a uefi-firmware entry if it's not supported

 grub-core/commands/efi/efifwsetup.c  | 30 +++++++++++++++++++---------
 grub-core/efiemu/i386/pc/cfgtables.c |  6 +++---
 grub-core/kern/efi/fdt.c             |  2 +-
 grub-core/loader/efi/fdt.c           |  2 +-
 grub-core/term/efi/console.c         |  2 +-
 util/grub.d/30_uefi-firmware.in      | 21 ++++++++-----------
 6 files changed, 35 insertions(+), 28 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/5 commands/efi/efifwsetup: Add missing grub_free()s

Each call of grub_efi_get_variable() needs a grub_free().

---
 grub-core/commands/efi/efifwsetup.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
grub-core/commands/efi/efifwsetup.c +8 -3
index eaca032838..b5b1f106c6 100644
@@ -44,6 +44,8 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
   if (old_os_indications != NULL && oi_size == sizeof (os_indications))
     os_indications |= *old_os_indications;
 
+  grub_free (old_os_indications);
+
   status = grub_efi_set_variable ("OsIndications", &global, &os_indications,
 				  sizeof (os_indications));
   if (status != GRUB_ERR_NONE)
@@ -62,17 +64,20 @@ efifwsetup_is_supported (void)
   grub_efi_uint64_t *os_indications_supported = NULL;
   grub_size_t oi_size = 0;
   grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  grub_boolean_t ret = 0;
 
   grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
 			 (void **) &os_indications_supported);
 
   if (!os_indications_supported)
-    return 0;
+    goto done;
 
   if (*os_indications_supported & GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
-    return 1;
+    ret = 1;
 
-  return 0;
+ done:
+  grub_free (os_indications_supported);
+  return ret;
 }
 
 GRUB_MOD_INIT (efifwsetup)
Signature · 1 line
2.35.1
#2 lore raw

PATCH v3 2/5 Make all grub_efi_guid_t variables static

This is believed to result in smaller code.

---
 grub-core/commands/efi/efifwsetup.c  | 4 ++--
 grub-core/efiemu/i386/pc/cfgtables.c | 6 +++---
 grub-core/kern/efi/fdt.c             | 2 +-
 grub-core/loader/efi/fdt.c           | 2 +-
 grub-core/term/efi/console.c         | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)
grub-core/commands/efi/efifwsetup.c +2 -2
index b5b1f106c6..51bb2ace3b 100644
@@ -36,7 +36,7 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
   grub_efi_uint64_t os_indications = GRUB_EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
   grub_err_t status;
   grub_size_t oi_size;
-  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
 
   grub_efi_get_variable ("OsIndications", &global, &oi_size,
 			 (void **) &old_os_indications);
@@ -63,7 +63,7 @@ efifwsetup_is_supported (void)
 {
   grub_efi_uint64_t *os_indications_supported = NULL;
   grub_size_t oi_size = 0;
-  grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
+  static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
   grub_boolean_t ret = 0;
 
   grub_efi_get_variable ("OsIndicationsSupported", &global, &oi_size,
grub-core/efiemu/i386/pc/cfgtables.c +3 -3
index e5fffb7d4a..1098f0b79f 100644
@@ -29,9 +29,9 @@ grub_machine_efiemu_init_tables (void)
 {
   void *table;
   grub_err_t err;
-  grub_efi_guid_t smbios = GRUB_EFI_SMBIOS_TABLE_GUID;
-  grub_efi_guid_t acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
-  grub_efi_guid_t acpi = GRUB_EFI_ACPI_TABLE_GUID;
+  static grub_efi_guid_t smbios = GRUB_EFI_SMBIOS_TABLE_GUID;
+  static grub_efi_guid_t acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
+  static grub_efi_guid_t acpi = GRUB_EFI_ACPI_TABLE_GUID;
 
   err = grub_efiemu_unregister_configuration_table (smbios);
   if (err)
grub-core/kern/efi/fdt.c +1 -1
index 30100c61c1..24f955289f 100644
@@ -24,7 +24,7 @@ void *
 grub_efi_get_firmware_fdt (void)
 {
   grub_efi_configuration_table_t *tables;
-  grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
+  static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
   void *firmware_fdt = NULL;
   unsigned int i;
 
grub-core/loader/efi/fdt.c +1 -1
index c86f283d75..80d7088747 100644
@@ -86,7 +86,7 @@ grub_err_t
 grub_fdt_install (void)
 {
   grub_efi_boot_services_t *b;
-  grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
+  static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
   grub_efi_status_t status;
 
   b = grub_efi_system_table->boot_services;
grub-core/term/efi/console.c +1 -1
index a3622e4fe5..532948a8e1 100644
@@ -353,7 +353,7 @@ grub_console_getkeystatus (struct grub_term_input *term)
 static grub_err_t
 grub_efi_console_input_init (struct grub_term_input *term)
 {
-  grub_efi_guid_t text_input_ex_guid =
+  static grub_efi_guid_t text_input_ex_guid =
     GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
 
   if (grub_efi_is_finished)
Signature · 1 line
2.35.1
#3 lore raw

PATCH v3 3/5 templates: Check for EFI at runtime instead of config generation time

In reply to Robbie Harwood

From: Javier Martinez Canillas <[email protected]>

The 30_uefi-firmware template checks if an OsIndicationsSupported UEFI var exists and EFI_OS_INDICATIONS_BOOT_TO_FW_UI bit is set, to decide whether a "fwsetup" menu entry would be added or not to the GRUB menu.

But this has the problem that it will only work if the configuration file was created on an UEFI machine that supports booting to a firmware UI.

This for example doesn't support creating GRUB config files when executing on systems that support both UEFI and legacy BIOS booting. Since creating the config file from legacy BIOS wouldn't allow to access the firmware UI.

To prevent this, make the template to unconditionally create the grub.cfg snippet but check at runtime if was booted through UEFI to decide if this entry should be added. That way it won't be added when booting with BIOS.

There's no need to check if EFI_OS_INDICATIONS_BOOT_TO_FW_UI bit is set, since that's already done by the "fwsetup" command when is executed.

---
 util/grub.d/30_uefi-firmware.in | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
util/grub.d/30_uefi-firmware.in +8 -13
index d344d3883d..b6041b55e2 100644
@@ -26,19 +26,14 @@ export TEXTDOMAINDIR="@localedir@"
 
 . "$pkgdatadir/grub-mkconfig_lib"
 
-EFI_VARS_DIR=/sys/firmware/efi/efivars
-EFI_GLOBAL_VARIABLE=8be4df61-93ca-11d2-aa0d-00e098032b8c
-OS_INDICATIONS="$EFI_VARS_DIR/OsIndicationsSupported-$EFI_GLOBAL_VARIABLE"
+LABEL="UEFI Firmware Settings"
 
-if [ -e "$OS_INDICATIONS" ] && \
-   [ "$(( $(printf 0x%x \'"$(cat $OS_INDICATIONS | cut -b5)"\') & 1 ))" = 1 ]; then
-  LABEL="UEFI Firmware Settings"
+gettext_printf "Adding boot menu entry for UEFI Firmware Settings ...\n" >&2
 
-  gettext_printf "Adding boot menu entry for UEFI Firmware Settings ...\n" >&2
-
-  cat << EOF
-menuentry '$LABEL' \$menuentry_id_option 'uefi-firmware' {
-	fwsetup
-}
-EOF
+cat << EOF
+if [ "\$grub_platform" = "efi" ]; then
+	menuentry '$LABEL' \$menuentry_id_option 'uefi-firmware' {
+		fwsetup
+	}
 fi
+EOF
Signature · 1 line
2.35.1
#4 lore raw

PATCH v3 4/5 efi: Print an error if boot to firmware setup is not supported

In reply to Robbie Harwood

From: Javier Martinez Canillas <[email protected]>

The "fwsetup" command is only registered if the firmware supports booting to the firmware setup UI. But it could be possible that the GRUB config already contains a "fwsetup" entry, because it was generated in a machine that has support for this feature.

To prevent users getting an error like:

    error: ../../grub-core/script/function.c:109:can't find command `fwsetup'.

if it is not supported by the firmware, let's just always register the command but print a more accurate message if the firmware doesn't support this option.

---
 grub-core/commands/efi/efifwsetup.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
grub-core/commands/efi/efifwsetup.c +8 -4
index 51bb2ace3b..cb4b6ff18c 100644
@@ -27,6 +27,8 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
+static grub_efi_boolean_t efifwsetup_is_supported (void);
+
 static grub_err_t
 grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
 		  int argc __attribute__ ((unused)),
@@ -38,6 +40,10 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
   grub_size_t oi_size;
   static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
 
+  if (!efifwsetup_is_supported ())
+	  return grub_error (GRUB_ERR_INVALID_COMMAND,
+			     N_("reboot to firmware setup is not supported by the current firmware"));
+
   grub_efi_get_variable ("OsIndications", &global, &oi_size,
 			 (void **) &old_os_indications);
 
@@ -82,10 +88,8 @@ efifwsetup_is_supported (void)
 
 GRUB_MOD_INIT (efifwsetup)
 {
-  if (efifwsetup_is_supported ())
-    cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
-				 N_("Reboot into firmware setup menu."));
-
+  cmd = grub_register_command ("fwsetup", grub_cmd_fwsetup, NULL,
+                               N_("Reboot into firmware setup menu."));
 }
 
 GRUB_MOD_FINI (efifwsetup)
Signature · 1 line
2.35.1
#5 lore raw

PATCH v3 5/5 Don't display a uefi-firmware entry if it's not supported

In reply to Robbie Harwood

Add a new --is-supported option to commands/efi/efifwsetup and conditionalize display on it.

---
 grub-core/commands/efi/efifwsetup.c | 3 +++
 util/grub.d/30_uefi-firmware.in     | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
grub-core/commands/efi/efifwsetup.c +3 -0
index cb4b6ff18c..52e47102ba 100644
@@ -40,6 +40,9 @@ grub_cmd_fwsetup (grub_command_t cmd __attribute__ ((unused)),
   grub_size_t oi_size;
   static grub_efi_guid_t global = GRUB_EFI_GLOBAL_VARIABLE_GUID;
 
+  if (argc >= 1 && strcmp(args[1], "--is-supported"))
+    return !efifwsetup_is_supported ();
+
   if (!efifwsetup_is_supported ())
 	  return grub_error (GRUB_ERR_INVALID_COMMAND,
 			     N_("reboot to firmware setup is not supported by the current firmware"));
util/grub.d/30_uefi-firmware.in +1 -1
index b6041b55e2..78aef67d78 100644
@@ -31,7 +31,7 @@ LABEL="UEFI Firmware Settings"
 gettext_printf "Adding boot menu entry for UEFI Firmware Settings ...\n" >&2
 
 cat << EOF
-if [ "\$grub_platform" = "efi" ]; then
+if [ "\$grub_platform" = "efi" ] && fwsetup --is-supported; then
 	menuentry '$LABEL' \$menuentry_id_option 'uefi-firmware' {
 		fwsetup
 	}
Signature · 1 line
2.35.1