Skip to contentSkip to conversation

EFI envblk

PATCH In review Glenn Washburn 1 participant 6 messages Sep 23, 2024 – Mar 2, 2025 Kbuild

lore
Share & tools
lore
#0 lore raw

v2 cover EFI envblk

Update v2:
* Rebase onto current master. This really did not change the contents of the patch though

This patch series (for me) was motivated by the "gdb: Add more support for debugging on EFI platforms" patch series, which allowed printing in early EFI init of the GDB command for properly loading symbols. That approach of having the functionality be included at compile time was ultimately rejected. During the discussion of that series, Robbie suggested[1] using patches by Peter and in the Redhat downstream repo which uses an EFI variable to store a GRUB env block. Using this, a user could store a variable in the env block stored in the EFI variable and then have GRUB load that env block in early init as a way to enable the printing of the GDB command.

I've taken the original patches by Peter, made more suitable for including in GRUB, fixed some bugs, and added a minor feature. The first patch, adds env block loading in early EFI init from the GRUB_ENV EFI variable. The second patch is included to provide tools for GRUB to set this EFI env block itself, as opposed to needing to use the method suggested by Robbie[1], which requires GNU/Linux and the user-space grub2-editenv utility. Of course, if GRUB is crashing before one can run the efi-export-env command, then other ways of creating and setting the EFI envblk variable are necessary. The third patch adds a test which checks the usability of the commands and the early init loading. And the last patch, whichvmotivated this series, prints the GDB command string if the GRUB variable named "enable_earlyinit_gdbinfo" is present and is set to "1", after the env block is loaded from the GRUB_ENV EFI variable. We might want a different name for the GRUB variable, I don't really have a preference.

Glenn

[1] https://mail.gnu.org/archive/html/grub-devel/2023-03/msg00072.html

Glenn Washburn (2):
tests: Add efienv_test for testing efi-export-env and efi-load-env
efi: Print GDB command for loading symbols if variable exists

Peter Jones (2):
efi: Load env block from GRUB_ENV EFI variable in early init
efi: Add efi-export-env and efi-load-env commands

 Makefile.util.def            |   6 ++
 docs/grub.texi               |  24 +++++
 grub-core/Makefile.core.def  |   7 ++
 grub-core/commands/efi/env.c | 182 +++++++++++++++++++++++++++++++++++
 grub-core/kern/efi/efi.c     |   3 +
 grub-core/kern/efi/init.c    |  37 +++++++
 grub-core/lib/envblk.c       |  43 +++++++++
 include/grub/efi/efi.h       |   5 +
 include/grub/lib/envblk.h    |   3 +
 tests/efienv_test.in         |  57 +++++++++++
 10 files changed, 367 insertions(+)
 create mode 100644 grub-core/commands/efi/env.c
 create mode 100644 tests/efienv_test.in

Range-diff against v1:
1: 15a9c7112eae ! 1: d2ad090d7486 efi: Load env block from GRUB_ENV EFI variable in early init

    @@ grub-core/kern/efi/init.c
      #ifdef GRUB_STACK_PROTECTOR
      
      static grub_efi_char16_t stack_chk_fail_msg[] =
    -@@ grub-core/kern/efi/init.c: stack_protector_init (void)
    +@@ grub-core/kern/efi/init.c: grub_stack_protector_init (void)
      
      grub_addr_t grub_modbase;
2:  d000cdbd0468 = 2:  456f4245ef76 efi: Add efi-export-env and efi-load-env commands
3:  3966eba4a32b = 3:  04072bccc2c7 tests: Add efienv_test for testing efi-export-env and efi-load-env
4:  b65e7a8bac40 = 4:  a23667d25f40 efi: Print GDB command for loading symbols if variable exists
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 1/4 efi: Load env block from GRUB_ENV EFI variable in early init

From: Peter Jones <[email protected]>

This allows setting GRUB variables before any user interaction or GRUB script can run. It is primarily intended to be used to turn on early debugging.

Replace grub_efi_guid_t -> grub_guid_t

---
 grub-core/Makefile.core.def |  1 +
 grub-core/kern/efi/init.c   | 31 +++++++++++++++++++++++++++++++
 include/grub/efi/efi.h      |  5 +++++
 3 files changed, 37 insertions(+)
grub-core/Makefile.core.def +1 -0
index 063ef5dd7801..5699736dad20 100644
@@ -219,6 +219,7 @@ kernel = {
   efi = kern/efi/acpi.c;
   efi = kern/efi/sb.c;
   efi = kern/lockdown.c;
+  efi = lib/envblk.c;
   i386_coreboot = kern/i386/pc/acpi.c;
   i386_multiboot = kern/i386/pc/acpi.c;
   i386_coreboot = kern/acpi.c;
grub-core/kern/efi/init.c +31 -0
index 1637077e1e96..2da6036129a1 100644
@@ -28,8 +28,11 @@
 #include <grub/env.h>
 #include <grub/mm.h>
 #include <grub/kernel.h>
+
 #include <grub/stack_protector.h>
 
+#include <grub/lib/envblk.h>
+
 #ifdef GRUB_STACK_PROTECTOR
 
 static grub_efi_char16_t stack_chk_fail_msg[] =
@@ -94,6 +97,31 @@ grub_stack_protector_init (void)
 
 grub_addr_t grub_modbase;
 
+/* Helper for grub_efi_env_init */
+static int
+set_var (const char *name, const char *value,
+	 void *whitelist __attribute__((__unused__)))
+{
+  grub_env_set (name, value);
+  return 0;
+}
+
+static void
+grub_efi_env_init (void)
+{
+  grub_guid_t efi_grub_guid = GRUB_EFI_GRUB_VARIABLE_GUID;
+  struct grub_envblk envblk_s = { NULL, 0 };
+  grub_envblk_t envblk = &envblk_s;
+
+  grub_efi_get_variable ("GRUB_ENV", &efi_grub_guid, &envblk_s.size,
+                         (void **) &envblk_s.buf);
+  if (envblk_s.buf == NULL || envblk_s.size < 1)
+    return;
+
+  grub_envblk_iterate (envblk, NULL, set_var);
+  grub_free (envblk_s.buf);
+}
+
 void
 grub_efi_init (void)
 {
@@ -105,6 +133,9 @@ grub_efi_init (void)
   /* Initialize the memory management system.  */
   grub_efi_mm_init ();
 
+  /* Populate environment with variables from EFI envblk, if it exists.  */
+  grub_efi_env_init ();
+
   /*
    * Lockdown the GRUB and register the shim_lock verifier
    * if the UEFI Secure Boot is enabled.
include/grub/efi/efi.h +5 -0
index a5cd99e5afa9..3edf44dbda49 100644
@@ -25,6 +25,11 @@
 #include <grub/efi/api.h>
 #include <grub/efi/pe32.h>
 
+#define GRUB_EFI_GRUB_VARIABLE_GUID \
+  { 0x91376aff, 0xcba6, 0x42be, \
+    { 0x94, 0x9d, 0x06, 0xfd, 0xe8, 0x11, 0x28, 0xe8 } \
+  }
+
 #define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818
 
 struct linux_arch_kernel_header {
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v2 2/4 efi: Add efi-export-env and efi-load-env commands

In reply to Glenn Washburn

From: Peter Jones <[email protected]>

This adds "efi-export-env VARIABLES" and "efi-load-env", which manipulate the environment block stored in the EFI variable GRUB_ENV-91376aff-cba6-42be-949d-06fde81128e8.

Fix rebase, cherry-pick issues, grub_efi_guid_t -> grub_guid_t,
module name bug, missing return bug, memory leaks, formatting
Add support for specifying multiple variables at once for efi-export-env.

---
 docs/grub.texi               |  24 +++++
 grub-core/Makefile.core.def  |   6 ++
 grub-core/commands/efi/env.c | 182 +++++++++++++++++++++++++++++++++++
 grub-core/kern/efi/efi.c     |   3 +
 grub-core/lib/envblk.c       |  43 +++++++++
 include/grub/lib/envblk.h    |   3 +
 6 files changed, 261 insertions(+)
 create mode 100644 grub-core/commands/efi/env.c
docs/grub.texi +24 -0
index 2ea6c56d100e..4d53777dce14 100644
@@ -6391,6 +6391,8 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efi-export-env::              Export environment variable to UEFI
+* efi-load-env::                Load the grub environment from UEFI
 * efitextmode::                 Set/Get text output mode resolution
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
@@ -6860,6 +6862,28 @@ character will print that character.
 @end deffn
 
 
+@node efi-export-env
+@subsection efi-export-env
+
+@deffn Command efi-export-env varname [varname @dots{}]
+Export the given GRUB variables to the GRUB_ENV UEFI variable as a GRUB
+environment block. The contents of an existing environment block in GRUB_ENV
+will be merged. So variables existing in GRUB_ENV but not given as arguements
+will be unaffected by this command.
+@end deffn
+
+
+@node efi-load-env
+@subsection efi-load-env
+
+@deffn Command efi-load-env
+Load the grub environment from the GRUB_ENV UEFI variable. This will overwrite
+existing variables if they exist in the environment block present in the UEFI
+variable. Other existing GRUB variables will be unchanged. This can allow for
+the persistence of state across boots.
+@end deffn
+
+
 @node efitextmode
 @subsection efitextmode
 
grub-core/Makefile.core.def +6 -0
index 5699736dad20..640f9176d306 100644
@@ -829,6 +829,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = efienv;
+  common = commands/efi/env.c;
+  enable = efi;
+};
+
 module = {
   name = efifwsetup;
   efi = commands/efi/efifwsetup.c;
grub-core/commands/efi/env.c new file +182 -0

182 lines not on this page · raw message

grub-core/kern/efi/efi.c +3 -0
index b93ae3aba12d..38e508ff6c28 100644
@@ -222,6 +222,9 @@ grub_efi_set_variable_with_attributes (const char *var, const grub_guid_t *guid,
   if (status == GRUB_EFI_SUCCESS)
     return GRUB_ERR_NONE;
 
+  if (status == GRUB_EFI_NOT_FOUND && datasize == 0)
+    return GRUB_ERR_NONE;
+
   return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
 }
 
grub-core/lib/envblk.c +43 -0

49 lines not on this page · raw message

include/grub/lib/envblk.h +3 -0
index c3e655921709..ab969af24612 100644
@@ -22,6 +22,8 @@
 #define GRUB_ENVBLK_SIGNATURE	"# GRUB Environment Block\n"
 #define GRUB_ENVBLK_DEFCFG	"grubenv"
 
+#define DEFAULT_ENVBLK_SIZE	1024
+
 #ifndef ASM_FILE
 
 struct grub_envblk
@@ -33,6 +35,7 @@ typedef struct grub_envblk *grub_envblk_t;
 
 grub_envblk_t grub_envblk_open (char *buf, grub_size_t size);
 int grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value);
+grub_err_t grub_envblk_get (grub_envblk_t envblk, const char * const name, char ** const value);
 void grub_envblk_delete (grub_envblk_t envblk, const char *name);
 void grub_envblk_iterate (grub_envblk_t envblk,
                           void *hook_data,
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v2 3/4 tests: Add efienv_test for testing efi-export-env and efi-load-env

In reply to Glenn Washburn
---
 Makefile.util.def    |  6 +++++
 tests/efienv_test.in | 57 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 tests/efienv_test.in
Makefile.util.def +6 -0
index 0f74a1680f13..539f8c4edd9a 100644
@@ -1185,6 +1185,12 @@ script = {
   common = tests/help_test.in;
 };
 
+script = {
+  testcase = nonnative;
+  name = efienv_test;
+  common = tests/efienv_test.in;
+};
+
 script = {
   testcase = nonnative;
   name = grub_script_gettext;
tests/efienv_test.in new file +57 -0

57 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v2 4/4 efi: Print GDB command for loading symbols if variable exists

In reply to Glenn Washburn

If the variable "enable_earlyinit_gdbinfo" exists, print the GDB command needed to properly load symbols in an attached GDB session. This variable must come from the environment block stored in the GRUB_ENV EFI variable, which allows this to get printed as early as possible.

---
 grub-core/kern/efi/init.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/kern/efi/init.c +6 -0
index 2da6036129a1..bdc65fb72ef3 100644
@@ -125,6 +125,8 @@ grub_efi_env_init (void)
 void
 grub_efi_init (void)
 {
+  const char *value;
+
   grub_modbase = grub_efi_section_addr ("mods");
   /* First of all, initialize the console so that GRUB can display
      messages.  */
@@ -136,6 +138,10 @@ grub_efi_init (void)
   /* Populate environment with variables from EFI envblk, if it exists.  */
   grub_efi_env_init ();
 
+  value = grub_env_get ("enable_earlyinit_gdbinfo");
+  if (value != NULL && !grub_strcmp (value, "1"))
+    grub_efi_print_gdb_info ();
+
   /*
    * Lockdown the GRUB and register the shim_lock verifier
    * if the UEFI Secure Boot is enabled.
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw
In reply to Glenn Washburn

Daniel,

Is this still in the to be reviewed queue? Despite how long its been, I believe this patch series still applies cleanly to master.

Glenn

Quoted 67 lines · On Mon, 23 Sep 2024 01:04:54 -0500 Glenn Washburn…

Update v2:
* Rebase onto current master. This really did not change the contents of the patch though

This patch series (for me) was motivated by the "gdb: Add more support for debugging on EFI platforms" patch series, which allowed printing in early EFI init of the GDB command for properly loading symbols. That approach of having the functionality be included at compile time was ultimately rejected. During the discussion of that series, Robbie suggested[1] using patches by Peter and in the Redhat downstream repo which uses an EFI variable to store a GRUB env block. Using this, a user could store a variable in the env block stored in the EFI variable and then have GRUB load that env block in early init as a way to enable the printing of the GDB command.

I've taken the original patches by Peter, made more suitable for including in GRUB, fixed some bugs, and added a minor feature. The first patch, adds env block loading in early EFI init from the GRUB_ENV EFI variable. The second patch is included to provide tools for GRUB to set this EFI env block itself, as opposed to needing to use the method suggested by Robbie[1], which requires GNU/Linux and the user-space grub2-editenv utility. Of course, if GRUB is crashing before one can run the efi-export-env command, then other ways of creating and setting the EFI envblk variable are necessary. The third patch adds a test which checks the usability of the commands and the early init loading. And the last patch, whichvmotivated this series, prints the GDB command string if the GRUB variable named "enable_earlyinit_gdbinfo" is present and is set to "1", after the env block is loaded from the GRUB_ENV EFI variable. We might want a different name for the GRUB variable, I don't really have a preference.

Glenn

[1] https://mail.gnu.org/archive/html/grub-devel/2023-03/msg00072.html

Glenn Washburn (2):
tests: Add efienv_test for testing efi-export-env and efi-load-env
efi: Print GDB command for loading symbols if variable exists

Peter Jones (2):
efi: Load env block from GRUB_ENV EFI variable in early init
efi: Add efi-export-env and efi-load-env commands

 Makefile.util.def            |   6 ++
 docs/grub.texi               |  24 +++++
 grub-core/Makefile.core.def  |   7 ++
 grub-core/commands/efi/env.c | 182 +++++++++++++++++++++++++++++++++++
 grub-core/kern/efi/efi.c     |   3 +
 grub-core/kern/efi/init.c    |  37 +++++++
 grub-core/lib/envblk.c       |  43 +++++++++
 include/grub/efi/efi.h       |   5 +
 include/grub/lib/envblk.h    |   3 +
 tests/efienv_test.in         |  57 +++++++++++
 10 files changed, 367 insertions(+)
 create mode 100644 grub-core/commands/efi/env.c
 create mode 100644 tests/efienv_test.in

Range-diff against v1:
1: 15a9c7112eae ! 1: d2ad090d7486 efi: Load env block from GRUB_ENV EFI variable in early init

    @@ grub-core/kern/efi/init.c
      #ifdef GRUB_STACK_PROTECTOR
      
      static grub_efi_char16_t stack_chk_fail_msg[] =
    -@@ grub-core/kern/efi/init.c: stack_protector_init (void)
    +@@ grub-core/kern/efi/init.c: grub_stack_protector_init (void)
      
      grub_addr_t grub_modbase;
Quoted 3 lines
2:  d000cdbd0468 = 2:  456f4245ef76 efi: Add efi-export-env and efi-load-env commands
3:  3966eba4a32b = 3:  04072bccc2c7 tests: Add efienv_test for testing efi-export-env and efi-load-env
4:  b65e7a8bac40 = 4:  a23667d25f40 efi: Print GDB command for loading symbols if variable exists

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel