efi_loader: simplify 'printenv -e'
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Wed, 15 Jul 2020 16:00:56 +0000 (18:00 +0200)
committerHeinrich Schuchardt <xypron.glpk@gmx.de>
Thu, 16 Jul 2020 10:37:03 +0000 (12:37 +0200)
Currently default output of 'printenv -e' is restricted to variables with
GUID EFI_GLOBAL_VARIABLE. This excludes db and dbx. As the number of
variables is small there is no need for this restriction.

If no GUID is provided, print all matching variables irrespective of GUID.

Always show the numeric value of the GUID.

If the GUID provided to 'setenv -e' is invalid, return CMD_RET_USAGE.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
cmd/nvedit.c
cmd/nvedit_efi.c

index 49338b4..ca0be92 100644 (file)
@@ -1410,7 +1410,7 @@ static char env_help_text[] =
 #endif
        "env print [-a | name ...] - print environment\n"
 #if defined(CONFIG_CMD_NVEDIT_EFI)
-       "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
+       "env print -e [-guid guid[-n] [name ...] - print UEFI environment\n"
 #endif
 #if defined(CONFIG_CMD_RUN)
        "env run var [...] - run commands in an environment variable\n"
@@ -1452,8 +1452,9 @@ U_BOOT_CMD_COMPLETE(
        "print environment variables",
        "[-a]\n    - print [all] values of all environment variables\n"
 #if defined(CONFIG_CMD_NVEDIT_EFI)
-       "printenv -e [-guid guid|-all][-n] [name ...]\n"
+       "printenv -e [-guid guid][-n] [name ...]\n"
        "    - print UEFI variable 'name' or all the variables\n"
+       "      \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
        "      \"-n\": suppress dumping variable's value\n"
 #endif
        "printenv name ...\n"
@@ -1487,7 +1488,7 @@ U_BOOT_CMD_COMPLETE(
        "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
        "        [-i addr,size name], or [name [value ...]]\n"
        "    - set UEFI variable 'name' to 'value' ...'\n"
-       "      \"-guid\": set vendor guid\n"
+       "      \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
        "      \"-nv\": set non-volatile attribute\n"
        "      \"-bs\": set boot-service attribute\n"
        "      \"-rt\": set runtime attribute\n"
index 3f61d5d..8e31f43 100644 (file)
@@ -52,8 +52,7 @@ static const struct {
        {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
 };
 
-/* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
-static char unknown_guid[37];
+static const char unknown_guid[] = "";
 
 /**
  * efi_guid_to_str() - convert guid to readable name
@@ -71,9 +70,6 @@ static const char *efi_guid_to_str(const efi_guid_t *guid)
                if (!guidcmp(guid, &efi_guid_text[i].guid))
                        return efi_guid_text[i].text;
 
-       uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
-                       UUID_STR_FORMAT_GUID);
-
        return unknown_guid;
 }
 
@@ -115,7 +111,7 @@ static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
                goto out;
 
        rtc_to_tm(time, &tm);
-       printf("%ls:\n    %s:\n", name, efi_guid_to_str(guid));
+       printf("%ls:\n    %pUl %s\n", name, guid, efi_guid_to_str(guid));
        if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
                printf("    %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year,
                       tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
@@ -136,50 +132,6 @@ out:
        free(data);
 }
 
-/**
- * efi_dump_vars() - show information about named UEFI variables
- *
- * @argc:      Number of arguments (variables)
- * @argv:      Argument (variable name) array
- * @verbose:   if true, dump data
- * Return:     CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
- *
- * Show information encoded in named UEFI variables
- */
-static int efi_dump_vars(int argc,  char *const argv[],
-                        const efi_guid_t *guid, bool verbose)
-{
-       u16 *var_name16, *p;
-       efi_uintn_t buf_size, size;
-
-       buf_size = 128;
-       var_name16 = malloc(buf_size);
-       if (!var_name16)
-               return CMD_RET_FAILURE;
-
-       for (; argc > 0; argc--, argv++) {
-               size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
-               if (buf_size < size) {
-                       buf_size = size;
-                       p = realloc(var_name16, buf_size);
-                       if (!p) {
-                               free(var_name16);
-                               return CMD_RET_FAILURE;
-                       }
-                       var_name16 = p;
-               }
-
-               p = var_name16;
-               utf8_utf16_strcpy(&p, argv[0]);
-
-               efi_dump_single_var(var_name16, guid, verbose);
-       }
-
-       free(var_name16);
-
-       return CMD_RET_SUCCESS;
-}
-
 static bool match_name(int argc, char *const argv[], u16 *var_name16)
 {
        char *buf, *p;
@@ -225,10 +177,7 @@ static int efi_dump_var_all(int argc,  char *const argv[],
        efi_uintn_t buf_size, size;
        efi_guid_t guid;
        efi_status_t ret;
-
-       if (argc && guid_p)
-               /* simplified case */
-               return efi_dump_vars(argc, argv, guid_p, verbose);
+       bool match = false;
 
        buf_size = 128;
        var_name16 = malloc(buf_size);
@@ -259,13 +208,18 @@ static int efi_dump_var_all(int argc,  char *const argv[],
                        return CMD_RET_FAILURE;
                }
 
-               if ((!guid_p || !guidcmp(guid_p, &guid)) &&
-                   (!argc || match_name(argc, argv, var_name16)))
+               if (guid_p && guidcmp(guid_p, &guid))
+                       continue;
+               if (!argc || match_name(argc, argv, var_name16)) {
+                       match = true;
                        efi_dump_single_var(var_name16, &guid, verbose);
+               }
        }
-
        free(var_name16);
 
+       if (!match && argc == 1)
+               printf("Error: \"%s\" not defined\n", argv[0]);
+
        return CMD_RET_SUCCESS;
 }
 
@@ -286,9 +240,8 @@ static int efi_dump_var_all(int argc,  char *const argv[],
 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
                     char *const argv[])
 {
-       efi_guid_t guid;
-       const efi_guid_t *guid_p;
-       bool default_guid, guid_any, verbose;
+       const efi_guid_t *guid_p = NULL;
+       bool verbose = true;
        efi_status_t ret;
 
        /* Initialize EFI drivers */
@@ -299,31 +252,18 @@ int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
                return CMD_RET_FAILURE;
        }
 
-       default_guid = true;
-       guid_any = false;
-       verbose = true;
        for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
                if (!strcmp(argv[0], "-guid")) {
-                       if (argc == 1)
-                               return CMD_RET_USAGE;
+                       efi_guid_t guid;
 
-                       /* -a already specified */
-                       if (!default_guid && guid_any)
+                       if (argc == 1)
                                return CMD_RET_USAGE;
-
                        argc--;
                        argv++;
                        if (uuid_str_to_bin(argv[0], guid.b,
                                            UUID_STR_FORMAT_GUID))
                                return CMD_RET_USAGE;
-                       default_guid = false;
-               } else if (!strcmp(argv[0], "-all")) {
-                       /* -guid already specified */
-                       if (!default_guid && !guid_any)
-                               return CMD_RET_USAGE;
-
-                       guid_any = true;
-                       default_guid = false;
+                       guid_p = (const efi_guid_t *)guid.b;
                } else if (!strcmp(argv[0], "-n")) {
                        verbose = false;
                } else {
@@ -331,13 +271,6 @@ int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
                }
        }
 
-       if (guid_any)
-               guid_p = NULL;
-       else if (default_guid)
-               guid_p = &efi_global_variable_guid;
-       else
-               guid_p = (const efi_guid_t *)guid.b;
-
        /* enumerate and show all UEFI variables */
        return efi_dump_var_all(argc, argv, guid_p, verbose);
 }
@@ -518,8 +451,7 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
                        argv++;
                        if (uuid_str_to_bin(argv[0], guid.b,
                                            UUID_STR_FORMAT_GUID)) {
-                               printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
-                               return CMD_RET_FAILURE;
+                               return CMD_RET_USAGE;
                        }
                        default_guid = false;
                } else if (!strcmp(argv[0], "-bs")) {
@@ -567,8 +499,8 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
        }
 
        if (verbose) {
-               printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
-                                                    &guid));
+               printf("GUID: %pUl %s\n", &guid,
+                      efi_guid_to_str((const efi_guid_t *)&guid));
                printf("Attributes: 0x%x\n", attributes);
        }