Skip to contentSkip to conversation

Improve logic to check for fwsetup support

PATCH Superseded Robbie Harwood 2 participants 6 messages Aug 17, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Improve logic to check for fwsetup support

Javier's original message for this series:

Quoted 21 lines

The fwsetup command can be used for rebooting to the firmware setup UI but this can only be used on machines that have support for this feature.

Curently, this is achieved by checking in the 30_uefi-firmware template if OsIndicationsSupported is present and has EFI_OS_INDICATIONS_BOOT_TO_FW_UI set. This is again checked in the efifwsetup module init function and the fwsetup command is only registered if that condition is meet.

But this logic has a comple of problems, since it assumes that the config file is always generated in the same machine than the one that's booted.

If that's not the case, any of the following situations could happen:

* A config file generated on a non-EFI machine, will not have the fwsetup menu entry even when booting with EFI.
* A config file generated on an EFI machine, will have a fwsetup even when is not booted with EFI.
* A config file generated on an EFI machine that supports rebooting to the firmware setup, will have the fwsetup even if booting on an EFI machine that does not support that feature.

Quoted 4 lines

To prevent these, let's change when the check is done. Instead of doing it at config generation time, do it at runtime with grub_platform == "efi" and when the command is executed. That way, the 30_uefi-firmware template won't make any assumption about the firmware support of the machine that's booted.

In this version, I've made changes to address dkiper's and pmenzel's feedback. This includes the addition of two new commits. I have also modified the error printing commit to hopefully produce a diff that's easier to read.

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 (2):
commands/efi/efifwsetup: Add missing grub_free()s Make all grub_efi_guid_t variables static

 grub-core/commands/efi/efifwsetup.c  | 27 ++++++++++++++++++---------
 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, 32 insertions(+), 28 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/4 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 v2 2/4 Make all grub_efi_guid_t variables static

In reply to Robbie Harwood

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 v2 3/4 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

Re: [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time

Quoted 43 lines · On Wed, 17 Aug 2022 15:50:33 -0400 Robbie Harwood <[email protected]> wrote:

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 +2 -6
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

util/grub.d/30_uefi-firmware.in export TEXTDOMAINDIR="@localedir@" · line 34

Quoted 10 lines
util/grub.d/30_uefi-firmware.in +2 -7
 
-  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

I think it would be even better if this was not displayed if efifwsetup_is_supported () returns false. This could be done with another condition on the output of "fwsetup --is-supported" and in the following patch or an additional patch add support for the "--is-supported" argument which should just return success if efifwsetup_is_supported () is true and false otherwise. This addition would preserve the current behavior which this patch otherwise does not completely preserve.

Glenn

Quoted 5 lines
util/grub.d/30_uefi-firmware.in +4 -0
+	menuentry '$LABEL' \$menuentry_id_option 'uefi-firmware' {
+		fwsetup
+	}
 fi
+EOF
#5 lore raw

PATCH v2 4/4 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..75f4af7e07 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"));
+
   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