[U-Boot] [PATCH v2 03/11] cmd: efidebug: rework "boot dump" sub-command using GetNextVariableName()
AKASHI Takahiro
takahiro.akashi at linaro.org
Wed Apr 24 06:30:37 UTC 2019
Efidebug command should be implemented using well-defined EFI interfaces,
rather than using internal functions/data. This change will be needed in
a later patch where UEFI variables are re-implemented.
Signed-off-by: AKASHI Takahiro <takahiro.akashi at linaro.org>
---
cmd/efidebug.c | 92 ++++++++++++++++++++++++++++++++++++--------------
1 file changed, 66 insertions(+), 26 deletions(-)
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index a40c4f4be286..8890dd7268f1 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -509,7 +509,7 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
if (argc < 6 || argc > 7)
return CMD_RET_USAGE;
- id = (int)simple_strtoul(argv[1], &endp, 16);
+ id = simple_strtoul(argv[1], &endp, 16);
if (*endp != '\0' || id > 0xffff)
return CMD_RET_USAGE;
@@ -595,7 +595,7 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
guid = efi_global_variable_guid;
for (i = 1; i < argc; i++, argv++) {
- id = (int)simple_strtoul(argv[1], &endp, 16);
+ id = simple_strtoul(argv[1], &endp, 16);
if (*endp != '\0' || id > 0xffff)
return CMD_RET_FAILURE;
@@ -693,6 +693,27 @@ static void show_efi_boot_opt(int id)
free(data);
}
+static bool u16_isxdigit(u16 c)
+{
+ if (c & 0xff00)
+ return false;
+
+ return isxdigit((u8)c);
+}
+
+static int u16_tohex(u16 c)
+{
+ if (c >= '0' && c < '9')
+ return c - '0';
+ if (c >= 'A' && c < 'F')
+ return c - 'A' + 10;
+ if (c >= 'a' && c < 'f')
+ return c - 'a' + 10;
+
+ /* dummy */
+ return -1;
+}
+
/**
* show_efi_boot_dump() - dump all UEFI load options
*
@@ -709,38 +730,57 @@ static void show_efi_boot_opt(int id)
static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
int argc, char * const argv[])
{
- char regex[256];
- char * const regexlist[] = {regex};
- char *variables = NULL, *boot, *value;
- int len;
- int id;
+ u16 *var_name16, *p;
+ efi_uintn_t buf_size, size;
+ efi_guid_t guid;
+ int id, i;
+ efi_status_t ret;
if (argc > 1)
return CMD_RET_USAGE;
- snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+");
-
- /* TODO: use GetNextVariableName? */
- len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
- &variables, 0, 1, regexlist);
+ buf_size = 128;
+ var_name16 = malloc(buf_size);
+ if (!var_name16)
+ return CMD_RET_FAILURE;
- if (!len)
- return CMD_RET_SUCCESS;
+ var_name16[0] = 0;
+ for (;;) {
+ size = buf_size;
+ ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
+ &guid));
+ if (ret == EFI_NOT_FOUND)
+ break;
+ if (ret == EFI_BUFFER_TOO_SMALL) {
+ buf_size = size;
+ p = realloc(var_name16, buf_size);
+ if (!p) {
+ free(var_name16);
+ return CMD_RET_FAILURE;
+ }
+ var_name16 = p;
+ ret = EFI_CALL(efi_get_next_variable_name(&size,
+ var_name16,
+ &guid));
+ }
+ if (ret != EFI_SUCCESS) {
+ free(var_name16);
+ return CMD_RET_FAILURE;
+ }
- if (len < 0)
- return CMD_RET_FAILURE;
+ if (u16_strncmp(var_name16, L"Boot", 4) || var_name16[8] ||
+ !u16_isxdigit(var_name16[4]) ||
+ !u16_isxdigit(var_name16[5]) ||
+ !u16_isxdigit(var_name16[6]) ||
+ !u16_isxdigit(var_name16[7]))
+ continue;
- boot = variables;
- while (*boot) {
- value = strstr(boot, "Boot") + 4;
- id = (int)simple_strtoul(value, NULL, 16);
+ for (id = 0, i = 0; i < 4; i++)
+ id = (id << 4) + u16_tohex(var_name16[4 + i]);
show_efi_boot_opt(id);
- boot = strchr(boot, '\n');
- if (!*boot)
- break;
- boot++;
}
- free(variables);
+
+ free(var_name16);
return CMD_RET_SUCCESS;
}
@@ -914,7 +954,7 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
return CMD_RET_FAILURE;
for (i = 0; i < argc; i++) {
- id = (int)simple_strtoul(argv[i], &endp, 16);
+ id = simple_strtoul(argv[i], &endp, 16);
if (*endp != '\0' || id > 0xffff) {
printf("invalid value: %s\n", argv[i]);
ret = CMD_RET_FAILURE;
--
2.20.1
More information about the U-Boot
mailing list