All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] Improve logic to check for fwsetup support
@ 2022-08-17 19:50 Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 1/4] commands/efi/efifwsetup: Add missing grub_free()s Robbie Harwood
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Robbie Harwood @ 2022-08-17 19:50 UTC (permalink / raw)
  To: grub-devel; +Cc: dkiper, pmenzel, javierm, Robbie Harwood

Javier's original message for this series:

> 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.
>
> 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(-)

-- 
2.35.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/4] commands/efi/efifwsetup: Add missing grub_free()s
  2022-08-17 19:50 [PATCH v2 0/4] Improve logic to check for fwsetup support Robbie Harwood
@ 2022-08-17 19:50 ` Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 2/4] Make all grub_efi_guid_t variables static Robbie Harwood
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Robbie Harwood @ 2022-08-17 19:50 UTC (permalink / raw)
  To: grub-devel; +Cc: dkiper, pmenzel, javierm, Robbie Harwood

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

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 grub-core/commands/efi/efifwsetup.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
index eaca032838..b5b1f106c6 100644
--- a/grub-core/commands/efi/efifwsetup.c
+++ b/grub-core/commands/efi/efifwsetup.c
@@ -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)
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/4] Make all grub_efi_guid_t variables static
  2022-08-17 19:50 [PATCH v2 0/4] Improve logic to check for fwsetup support Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 1/4] commands/efi/efifwsetup: Add missing grub_free()s Robbie Harwood
@ 2022-08-17 19:50 ` Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 4/4] efi: Print an error if boot to firmware setup is not supported Robbie Harwood
  3 siblings, 0 replies; 6+ messages in thread
From: Robbie Harwood @ 2022-08-17 19:50 UTC (permalink / raw)
  To: grub-devel; +Cc: dkiper, pmenzel, javierm, Robbie Harwood

This is believed to result in smaller code.

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 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(-)

diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
index b5b1f106c6..51bb2ace3b 100644
--- a/grub-core/commands/efi/efifwsetup.c
+++ b/grub-core/commands/efi/efifwsetup.c
@@ -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,
diff --git a/grub-core/efiemu/i386/pc/cfgtables.c b/grub-core/efiemu/i386/pc/cfgtables.c
index e5fffb7d4a..1098f0b79f 100644
--- a/grub-core/efiemu/i386/pc/cfgtables.c
+++ b/grub-core/efiemu/i386/pc/cfgtables.c
@@ -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)
diff --git a/grub-core/kern/efi/fdt.c b/grub-core/kern/efi/fdt.c
index 30100c61c1..24f955289f 100644
--- a/grub-core/kern/efi/fdt.c
+++ b/grub-core/kern/efi/fdt.c
@@ -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;
 
diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c
index c86f283d75..80d7088747 100644
--- a/grub-core/loader/efi/fdt.c
+++ b/grub-core/loader/efi/fdt.c
@@ -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;
diff --git a/grub-core/term/efi/console.c b/grub-core/term/efi/console.c
index a3622e4fe5..532948a8e1 100644
--- a/grub-core/term/efi/console.c
+++ b/grub-core/term/efi/console.c
@@ -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)
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time
  2022-08-17 19:50 [PATCH v2 0/4] Improve logic to check for fwsetup support Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 1/4] commands/efi/efifwsetup: Add missing grub_free()s Robbie Harwood
  2022-08-17 19:50 ` [PATCH v2 2/4] Make all grub_efi_guid_t variables static Robbie Harwood
@ 2022-08-17 19:50 ` Robbie Harwood
  2022-08-17 22:45   ` Glenn Washburn
  2022-08-17 19:50 ` [PATCH v2 4/4] efi: Print an error if boot to firmware setup is not supported Robbie Harwood
  3 siblings, 1 reply; 6+ messages in thread
From: Robbie Harwood @ 2022-08-17 19:50 UTC (permalink / raw)
  To: grub-devel; +Cc: dkiper, pmenzel, javierm, Robbie Harwood

From: Javier Martinez Canillas <javierm@redhat.com>

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.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 util/grub.d/30_uefi-firmware.in | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/util/grub.d/30_uefi-firmware.in b/util/grub.d/30_uefi-firmware.in
index d344d3883d..b6041b55e2 100644
--- a/util/grub.d/30_uefi-firmware.in
+++ b/util/grub.d/30_uefi-firmware.in
@@ -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
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/4] efi: Print an error if boot to firmware setup is not supported
  2022-08-17 19:50 [PATCH v2 0/4] Improve logic to check for fwsetup support Robbie Harwood
                   ` (2 preceding siblings ...)
  2022-08-17 19:50 ` [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time Robbie Harwood
@ 2022-08-17 19:50 ` Robbie Harwood
  3 siblings, 0 replies; 6+ messages in thread
From: Robbie Harwood @ 2022-08-17 19:50 UTC (permalink / raw)
  To: grub-devel; +Cc: dkiper, pmenzel, javierm, Robbie Harwood

From: Javier Martinez Canillas <javierm@redhat.com>

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.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 grub-core/commands/efi/efifwsetup.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/grub-core/commands/efi/efifwsetup.c b/grub-core/commands/efi/efifwsetup.c
index 51bb2ace3b..75f4af7e07 100644
--- a/grub-core/commands/efi/efifwsetup.c
+++ b/grub-core/commands/efi/efifwsetup.c
@@ -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)
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time
  2022-08-17 19:50 ` [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time Robbie Harwood
@ 2022-08-17 22:45   ` Glenn Washburn
  0 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2022-08-17 22:45 UTC (permalink / raw)
  To: Robbie Harwood; +Cc: The development of GNU GRUB, dkiper, pmenzel, javierm

On Wed, 17 Aug 2022 15:50:33 -0400
Robbie Harwood <rharwood@redhat.com> wrote:

> From: Javier Martinez Canillas <javierm@redhat.com>
> 
> 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.
> 
> Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
> Signed-off-by: Robbie Harwood <rharwood@redhat.com>
> ---
>  util/grub.d/30_uefi-firmware.in | 21 ++++++++-------------
>  1 file changed, 8 insertions(+), 13 deletions(-)
> 
> diff --git a/util/grub.d/30_uefi-firmware.in b/util/grub.d/30_uefi-firmware.in
> index d344d3883d..b6041b55e2 100644
> --- a/util/grub.d/30_uefi-firmware.in
> +++ b/util/grub.d/30_uefi-firmware.in
> @@ -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

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

> +	menuentry '$LABEL' \$menuentry_id_option 'uefi-firmware' {
> +		fwsetup
> +	}
>  fi
> +EOF


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-08-17 22:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-17 19:50 [PATCH v2 0/4] Improve logic to check for fwsetup support Robbie Harwood
2022-08-17 19:50 ` [PATCH v2 1/4] commands/efi/efifwsetup: Add missing grub_free()s Robbie Harwood
2022-08-17 19:50 ` [PATCH v2 2/4] Make all grub_efi_guid_t variables static Robbie Harwood
2022-08-17 19:50 ` [PATCH v2 3/4] templates: Check for EFI at runtime instead of config generation time Robbie Harwood
2022-08-17 22:45   ` Glenn Washburn
2022-08-17 19:50 ` [PATCH v2 4/4] efi: Print an error if boot to firmware setup is not supported Robbie Harwood

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.