Skip to contentSkip to conversation

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

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

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 commands/efi: add 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 v3:
- Command arguments, maybe this command is better if it doesn't have any command argument, since we are printing only. Command options is just unneccessary. And this is how many grub commands does, so it should align.
- The size required for the buffer UTF-16, maybe the firmware already did the formula so the bytes are as needed to store UTF-16. However not sure so i implemented it. But if the firmware did already we allocated large memory unneccesarily. We need to make sure.
- Other issues i fixed, as being suggested.

---
 grub-core/Makefile.core.def       |   4 +-
 grub-core/commands/efi/lsefivar.c | 129 ++++++++++++++++++++++++++++++
 2 files changed, 131 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 +129 -0

129 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 9 lines

Changes in v3:
- Command arguments, maybe this command is better if it doesn't have any command argument, since we are printing only. Command options is just unneccessary. And this is how many grub commands does, so it should align.

Options are good. Just use extcmd to parse them, not manual parsing.

+static void

Quoted 1 line

Better to return grub_err_t here and properly propagate it

Quoted 63 lines
grub-core/commands/efi/lsefivar.c new file +8 -0
+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_IO, "failed to retrieve variable data 0x%"

PRIxGRUB_EFI_UINTN_T, status);

grub-core/commands/efi/lsefivar.c new file +29 -0
+        return;
+    }
+
+    grub_printf(_("Attributes:\n"));
+    if (attributes & GRUB_EFI_VARIABLE_NON_VOLATILE)
+        grub_printf(_("\tNon-Volatile\n"));
+    if (attributes & GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS)
+        grub_printf(_("\tBoot Service Access\n"));
+    if (attributes & GRUB_EFI_VARIABLE_RUNTIME_ACCESS)
+        grub_printf(_("\tRuntime Service Access\n"));
+
+    grub_printf(_("Value:\n"));
+    hexdump(0, data, data_size);
+    grub_free(data);
+}
+
+static grub_err_t
+dump_efi_variables(void)
+{
+       grub_efi_status_t status;
+       grub_efi_runtime_services_t *r;
+       grub_efi_uintn_t variable_name_size = 512;
+       grub_efi_char16_t *variable_name;
+       grub_uint8_t *variable_name_string = NULL;
+       grub_guid_t vendor_guid;
+
+       r = grub_efi_system_table->runtime_services;
+
+       variable_name = grub_calloc(variable_name_size,

sizeof(grub_efi_char16_t));

grub-core/commands/efi/lsefivar.c new file +7 -0
+       if (variable_name == NULL)
+               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 +5 -0
+               if (status == GRUB_EFI_NOT_FOUND)
+                       break;
+               else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+               {
+                       grub_dprintf("lsefivar", "buffer isn't enough,

enlarging up to %ld bytes\n",variable_name_size *
sizeof(grub_efi_char16_t));
+ variable_name = grub_realloc(variable_name,
variable_name_size * sizeof(grub_efi_char16_t));

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

Quoted 3 lines
grub-core/commands/efi/lsefivar.c new file +3 -0
+                       if (variable_name == NULL)
+                       {
+                               grub_free(variable_name);

This free of NULL does absolutely nothing. Please fix it.

Quoted 6 lines
grub-core/commands/efi/lsefivar.c new file +4 -0
+                               return grub_errno;
+                       }
+               }
+               variable_name_string = grub_calloc(variable_name_size,

GRUB_MAX_UTF8_PER_UTF16);

This is not enough. You need 1 byte more.

Quoted 11 lines
grub-core/commands/efi/lsefivar.c new file +6 -0
+               if (variable_name_string == NULL)
+               {
+                       grub_free(variable_name);
+                       return grub_errno;
+               }
+               *grub_utf16_to_utf8(variable_name_string, variable_name,

variable_name_size) = '\0';

(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 line 107

Quoted 5 lines
grub-core/commands/efi/lsefivar.c new file +5 -0
+       }
+
+       grub_free(variable_name);
+       if (variable_name_string)
+               grub_free(variable_name_string);

grub_free already checks for null.

+ return GRUB_ERR_NONE;

Review

Quoted 9 lines
grub-core/commands/efi/lsefivar.c new file +5 -0
+}
+
+static grub_err_t
+grub_cmd_lsefivar (grub_command_t cmd __attribute__((unused)),
+                int argc __attribute__((unused)), char **argv

__attribute__((unused)))

(quoted diff) +2 -0
+{
+       return dump_efi_variables();

As I said: options and extcmd. Like normal ls command

Quoted 19 lines
grub-core/commands/efi/lsefivar.c new file +7 -0
+}
+
+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 +7 -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

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