commands/efi: dump all uefi runtime variables
PATCH v5 1/1 commands/efi: dump all uefi runtime variables
From: khaalid <[email protected]>
Quoted 2 lines
Vladimir 'phcoder' Serbinenko wrote:
Please have a look at how to handle errors with realloc. You need to keep a copy of old pointer in case you have to free it. Some places in our codebase have similar memory leaks but it'> s not a reason to introduce more.
Well, i followed style used on codebase mostly. And now fine.
--- grub-core/Makefile.core.def | 6 ++ grub-core/commands/efi/lsefivar.c | 139 ++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 grub-core/commands/efi/lsefivar.c
grub-core/Makefile.core.def +6 -0
@@ -840,6 +840,12 @@ module = { enable = efi; }; +module = { + name = lsefivar; + efi = commands/efi/lsefivar.c; + enable = efi; +}; + module = { name = blocklist; common = commands/blocklist.c;
grub-core/commands/efi/lsefivar.c new file +139 -0
139 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
Quoted 93 lines · khaalid cali
From: khaalid <[email protected]>
Quoted 2 lines
Vladimir 'phcoder' Serbinenko wrote:
Please have a look at how to handle errors with realloc. You need tokeep a copy of old pointer in case you have to free it. Some places in our codebase have similar memory leaks but it'> s not a reason to introduce more.
Well, i followed style used on codebase mostly. And now fine.
Signed-off-by Khalid Ali <[email protected]>--- grub-core/Makefile.core.def | 6 ++ grub-core/commands/efi/lsefivar.c | 139 ++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 grub-core/commands/efi/lsefivar.cgrub-core/Makefile.core.def +6
-0@@ -840,6 +840,12 @@ module = { enable = efi; }; +module = { + name = lsefivar; + efi = commands/efi/lsefivar.c; + enable = efi; +}; + module = { name = blocklist; common = commands/blocklist.c;grub-core/commands/efi/lsefivar.c new file +46
-0@@ -0,0 +1,139 @@ +/* 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);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%"
Quoted 3 lines
PRIxGRUB_EFI_UINTN_T, status);
+grub_errno=GRUB_ERR_NONE;
Do not call grub_error here.
grub-core/commands/efi/lsefivar.c line 55
Quoted 5 lines
grub-core/commands/efi/lsefivar.c new file +4
-0+return ; + } + +grub_puts_ ("Attributes:\n" );
Puts_(N_(...))
Having to put N_ is unfortunate and I might fix it separately but for now, is necessary.
grub-core/commands/efi/lsefivar.c line 57
Quoted 3 lines
grub-core/commands/efi/lsefivar.c new file +2
-0+if (attributes &GRUB_EFI_VARIABLE_NON_VOLATILE ) +grub_printf (_ ("\tNon-Volatile\n" ));
puts_
Quoted 73 lines
grub-core/commands/efi/lsefivar.c new file +23
-0+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 +6
-0+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));grub-core/commands/efi/lsefivar.c new file +8
-0+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);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';grub-core/commands/efi/lsefivar.c new file +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 +10
-0+ } + +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)))
grub-core/commands/efi/lsefivar.c line 123
Quoted 3 lines
grub-core/commands/efi/lsefivar.c new file +3
-0+{ +if (ctxt->state[0 ].set) +return dump_efi_variables ();
Without -l it should print just the list of variables without their contents, not throw an error
+
Quoted 20 lines
grub-core/commands/efi/lsefivar.c new file +8
-0+return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); +} + +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-core/commands/efi/lsefivar.c new file +7
-1+} +GRUB_MOD_FINI (lsefivar) +{ +grub_unregister_extcmd (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