Skip to contentSkip to conversation

commands/efi: add a command to dump all uefi runtime variables

PATCH Superseded khaalid cali 2 participants 2 messages May 10, 2025

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 commands/efi: add a command to dump all uefi runtime variables

From: khaalid <[email protected]>

This command is intended to print or dump all UEFI runtime services. The structure will look like efivar tool, since visually most people are familiar with it. If the variable content is string then dump it as string, otherwise for non string variables print them as raw hex; just the way efivar command does.

Changes in v2:
- Dump all variables as hex using hexdump() and let it handle it. Identifying string type and encoding method is alot complicated, error prone.
- Fix license and legal issue
- Make internalization of some messages being printed
- Strong error handling.
- About error handling i changed a place being suggested to change GRUB_ERR_BAD_MODULE, i changed to GRUB_ERR_BUG. I couldn't get most appriciate error type so the reason i chose that code is, if the retrieval fails, i considered it as a BUG that needs intervention. That is a bug present on this code, firmware or grub abstractions, because variable retrieval shouldn't fail.

---
 grub-core/Makefile.core.def       |   4 +-
 grub-core/commands/efi/lsefivar.c | 121 ++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/efi/lsefivar.c
grub-core/Makefile.core.def +2 -2
index ef3dc2dd0..8b9cf2a68 100644
@@ -841,8 +841,8 @@ module = {
 };
 
 module = {
-  name = efivar;
-  efi = commands/efi/efivar.c;
+  name = lsefivar;
+  efi = commands/efi/lsefivar.c;
   enable = efi;
 };
 
grub-core/commands/efi/lsefivar.c new file +121 -0

121 lines not on this page · raw message

Signature · 7 lines
2.49.0


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

- About error handling i changed a place being suggested to change GRUB_ERR_BAD_MODULE, i changed to GRUB_ERR_BUG

ERR_BUG is for an internal bugs aka should never happen. ERR_IO is a better possibility.

Quoted 17 lines
grub-core/commands/efi/lsefivar.c new file +11 -0
+
+
+static void
+dump_variable_data(const char *variable_name, grub_guid_t *variable_guid)
+{
+    grub_efi_status_t status;
+    grub_efi_uint32_t attributes;
+    grub_size_t data_size;
+    void *data;
+
+    status = grub_efi_get_variable_with_attributes(variable_name,

variable_guid,
+ &data_size, &data,
&attributes);

grub-core/commands/efi/lsefivar.c new file +3 -0
+    if (status != GRUB_EFI_SUCCESS)
+    {
+        grub_error(GRUB_ERR_BUG, "Failed to retrieve variable data 0x%"
Quoted 2 lines

PRIxGRUB_EFI_UINTN_T, status);

Error messages start with lowercase.

+ return;

grub-core/commands/efi/lsefivar.c line 49

Quoted 4 lines
grub-core/commands/efi/lsefivar.c new file +3 -0
+    }
+
+    grub_printf("Attributes:\n");

Needs translation

grub-core/commands/efi/lsefivar.c line 51

Quoted 3 lines
grub-core/commands/efi/lsefivar.c new file +2 -0
+    if (attributes & GRUB_EFI_VARIABLE_NON_VOLATILE)
+        grub_printf(N_("\tNon-Volatile\n"));

N_ doesn't translate, only marks for translation. You need to use printf_ or simple _. Here and following.

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+       variable_name = grub_malloc(variable_name_size);

Here and in following: buffer size is in char16s, not bytes, so you need to use calloc with size argument equal to size of(char16s)

grub-core/commands/efi/lsefivar.c line 83

Quoted 11 lines
grub-core/commands/efi/lsefivar.c new file +7 -0
+       if (!variable_name)
+               return grub_errno;
+       *variable_name = 0; /* Start with empty string */
+
+       while (1)
+       {
+               status = r->get_next_variable_name(&variable_name_size,

variable_name, &vendor_guid);

grub-core/commands/efi/lsefivar.c new file +2 -0
+               if (status == GRUB_EFI_NOT_FOUND)
+                       break;

You don't handle EFI_BUFFER_TOO_SMALL

Quoted 2 lines

+ variable_name_string = grub_realloc(variable_name_string,
variable_name_size);

You need variable_name_size * GRUB_MAX_UTF8_PER_UTF16 + 1 bytes to hold UTF-8 string.

grub-core/commands/efi/lsefivar.c line 86

Quoted 3 lines
grub-core/commands/efi/lsefivar.c new file +2 -0
+               if (variable_name_string == NULL)
+                       return grub_errno;

Here you have a memory leak

Quoted 3 lines

+ grub_utf16_to_utf8(variable_name_string, variable_name,
variable_name_size);

You need to set last char to \0. Look at other users

Quoted 10 lines
(quoted diff) +2 -0
+               grub_printf("%pG-%s\n",&vendor_guid, variable_name_string);
+               dump_variable_data((char*)variable_name_string,

&vendor_guid);

grub-core/commands/efi/lsefivar.c new file +6 -0
+       }
+
+       grub_free(variable_name);
+       grub_free(variable_name_string);
+       return GRUB_ERR_NONE;
+}

grub-core/commands/efi/lsefivar.c line 105

Quoted 10 lines
grub-core/commands/efi/lsefivar.c new file +10 -0
+
+static grub_err_t
+grub_cmd_lsefivar (grub_command_t cmd __attribute__((unused)),
+                int argc , char **argv)
+{
+       if (argc == 0)
+       {
+               grub_printf("Usage: lsefivar [-l]\n");
+               return GRUB_ERR_BAD_ARGUMENT;
+       }

Please use extcmd for argument parsing and documentation

Quoted 21 lines
grub-core/commands/efi/lsefivar.c new file +11 -0
+       if (!grub_strcmp(argv[0], "-l"))
+               return dump_efi_variables();
+
+       return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(lsefivar)
+{
+       cmd = grub_register_command("lsefivar", grub_cmd_lsefivar, NULL,

N_("Display UEFI variables."));

grub-core/commands/efi/lsefivar.c new file +5 -1
+}
+GRUB_MOD_FINI(lsefivar)
+{
+       grub_unregister_command(cmd);
+}
--

2.49.0

Quoted 5 lines

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

Regards
Vladimir 'phcoder' Serbinenko

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