grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Khalid Ali <khaliidcaliy@gmail.com>
To: grub-devel@gnu.org
Cc: Khalid Ali <khaliidcaly@gmail.com>
Subject: [PATCH v7] commands/efi: Add a command to dump all UEFI runtime variables
Date: Mon, 21 Jul 2025 07:55:44 +0000	[thread overview]
Message-ID: <20250721075607.674-1-khaliidcaliy@gmail.com> (raw)

From: Khalid Ali <khaliidcaly@gmail.com>

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. All uefi variables are dumped as hex no matter what
data type is.

Signed-off-by: Khalid Ali <khaliidcaly@gmail.com>
---
 grub-core/Makefile.core.def       |   6 ++
 grub-core/commands/efi/lsefivar.c | 137 ++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+)
 create mode 100644 grub-core/commands/efi/lsefivar.c

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index b3f71196a..71f562a28 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -828,6 +828,12 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = lsefivar;
+  common = commands/efi/lsefivar.c;
+  enable = efi;
+};
+
 module = {
   name = efifwsetup;
   efi = commands/efi/efifwsetup.c;
diff --git a/grub-core/commands/efi/lsefivar.c b/grub-core/commands/efi/lsefivar.c
new file mode 100644
index 000000000..8bbafdc44
--- /dev/null
+++ b/grub-core/commands/efi/lsefivar.c
@@ -0,0 +1,137 @@
+/* efivar.c - dump runtime uefi variables. */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2025  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/charset.h>
+#include <grub/extcmd.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/api.h>
+#include <grub/lib/hexdump.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static const struct grub_arg_option options[] = 
+{
+       {0, 'l', 0, N_("Dump variable contents"), 0, 0},
+       {0, 0, 0, 0, 0, 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);
+    if (status != GRUB_EFI_SUCCESS)
+    {
+        grub_errno = GRUB_ERR_NONE;
+        return;
+    }
+
+    grub_puts_ (N_("Attributes:"));
+    if (attributes & GRUB_EFI_VARIABLE_NON_VOLATILE)
+        grub_puts_ (N_("\tNon-Volatile"));
+    if (attributes & GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS)
+        grub_puts_ (N_("\tBoot Service Access"));
+    if (attributes & GRUB_EFI_VARIABLE_RUNTIME_ACCESS)
+        grub_puts_ (N_("\tRuntime Service Access"));
+
+    grub_puts_ (N_("Value:"));
+    hexdump (0, data, data_size);
+    grub_free (data);
+}
+
+static grub_err_t 
+dump_efi_variables(grub_efi_boolean_t dump_variable_content)
+{
+       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));
+       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);
+               if (status == GRUB_EFI_NOT_FOUND)
+                       break;
+               else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
+               {
+                       grub_efi_char16_t *tmp;
+                       grub_dprintf ("lsefivar", "buffer isn't enough, enlarging up to %ld bytes\n",variable_name_size * sizeof(grub_efi_char16_t));
+                       tmp = grub_realloc (variable_name, variable_name_size * sizeof(grub_efi_char16_t));
+                       if (tmp == NULL)
+                       {
+                               grub_free (variable_name);
+                               return grub_errno;
+                       }
+                       variable_name = tmp;
+               }
+               variable_name_string = grub_calloc (variable_name_size + 1, GRUB_MAX_UTF8_PER_UTF16);
+               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';
+               grub_printf ("%pG-%s\n",&vendor_guid, variable_name_string);
+               if (dump_variable_content)
+                       dump_variable_data ((char*)variable_name_string, &vendor_guid);
+       }
+
+       grub_free (variable_name);
+       grub_free (variable_name_string);
+       return GRUB_ERR_NONE;
+}      
+
+static grub_err_t
+grub_cmd_lsefivar (grub_extcmd_context_t ctxt,
+                int argc __attribute__((unused)), char **argv __attribute__((unused)))
+{
+       if (ctxt->state[0].set)
+               return dump_efi_variables (true); /* true: dump variables and contents*/
+        
+       return dump_efi_variables (false); /* false: do not dump variable contents*/
+}
+
+static grub_extcmd_t cmd;
+
+GRUB_MOD_INIT(lsefivar)
+{
+       cmd = grub_register_extcmd ("lsefivar", grub_cmd_lsefivar, 0, N_("[-l]"), N_("Display UEFI variables."), options);
+}
+GRUB_MOD_FINI(lsefivar)
+{
+       grub_unregister_extcmd (cmd);
+}
-- 
2.49.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

                 reply	other threads:[~2025-07-21  7:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250721075607.674-1-khaliidcaliy@gmail.com \
    --to=khaliidcaliy@gmail.com \
    --cc=grub-devel@gnu.org \
    --cc=khaliidcaly@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).