]> git.ipfire.org Git - thirdparty/u-boot.git/blobdiff - cmd/nvedit_efi.c
Init virtio before loading ENV from EXT4 or FAT
[thirdparty/u-boot.git] / cmd / nvedit_efi.c
index 837e39e021798876ddc9e05a230ca619642898c8..64ae2ad2ce24b926c21f728bf4b31b2dbbe4370a 100644 (file)
@@ -6,14 +6,16 @@
  */
 
 #include <charset.h>
-#include <common.h>
 #include <command.h>
 #include <efi_loader.h>
+#include <efi_variable.h>
 #include <env.h>
 #include <exports.h>
 #include <hexdump.h>
 #include <malloc.h>
 #include <mapmem.h>
+#include <rtc.h>
+#include <uuid.h>
 #include <linux/kernel.h>
 
 /*
@@ -33,46 +35,9 @@ static const struct {
        {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
        {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
        {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
+       {EFI_VARIABLE_READ_ONLY, "RO"},
 };
 
-static const struct {
-       efi_guid_t guid;
-       char *text;
-} efi_guid_text[] = {
-       /* signature database */
-       {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
-       {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
-       /* certificate type */
-       {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
-       {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
-       {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
-};
-
-/* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
-static char unknown_guid[37];
-
-/**
- * efi_guid_to_str() - convert guid to readable name
- *
- * @guid:      GUID
- * Return:     string for GUID
- *
- * convert guid to readable name
- */
-static const char *efi_guid_to_str(const efi_guid_t *guid)
-{
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
-               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;
-}
-
 /**
  * efi_dump_single_var() - show information about a UEFI variable
  *
@@ -86,20 +51,22 @@ static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
 {
        u32 attributes;
        u8 *data;
+       u64 time;
+       struct rtc_time tm;
        efi_uintn_t size;
        int count, i;
        efi_status_t ret;
 
        data = NULL;
        size = 0;
-       ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
+       ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time);
        if (ret == EFI_BUFFER_TOO_SMALL) {
                data = malloc(size);
                if (!data)
                        goto out;
 
-               ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
-                                               data));
+               ret = efi_get_variable_int(name, guid, &attributes, &size,
+                                          data, &time);
        }
        if (ret == EFI_NOT_FOUND) {
                printf("Error: \"%ls\" not defined\n", name);
@@ -108,13 +75,16 @@ static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
        if (ret != EFI_SUCCESS)
                goto out;
 
-       printf("%ls:\n    %s:", name, efi_guid_to_str(guid));
+       rtc_to_tm(time, &tm);
+       printf("%ls:\n    %pUl (%pUs)\n", name, guid, 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);
+       printf("    ");
        for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
                if (attributes & efi_var_attrs[i].mask) {
                        if (count)
                                putc('|');
-                       else
-                               putc(' ');
                        count++;
                        puts(efi_var_attrs[i].text);
                }
@@ -127,51 +97,7 @@ 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)
+static bool match_name(int argc, char *const argv[], u16 *var_name16)
 {
        char *buf, *p;
        size_t buflen;
@@ -209,17 +135,14 @@ out:
  *
  * Show information encoded in all the UEFI variables
  */
-static int efi_dump_var_all(int argc,  char * const argv[],
+static int efi_dump_var_all(int argc,  char *const argv[],
                            const efi_guid_t *guid_p, bool verbose)
 {
        u16 *var_name16, *p;
        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);
@@ -229,8 +152,8 @@ static int efi_dump_var_all(int argc,  char * const argv[],
        var_name16[0] = 0;
        for (;;) {
                size = buf_size;
-               ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
-                                                         &guid));
+               ret = efi_get_next_variable_name_int(&size, var_name16,
+                                                    &guid);
                if (ret == EFI_NOT_FOUND)
                        break;
                if (ret == EFI_BUFFER_TOO_SMALL) {
@@ -241,22 +164,28 @@ static int efi_dump_var_all(int argc,  char * const argv[],
                                return CMD_RET_FAILURE;
                        }
                        var_name16 = p;
-                       ret = EFI_CALL(efi_get_next_variable_name(&size,
-                                                                 var_name16,
-                                                                 &guid));
+                       ret = efi_get_next_variable_name_int(&size, var_name16,
+                                                            &guid);
                }
                if (ret != EFI_SUCCESS) {
                        free(var_name16);
                        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_FAILURE;
+       }
+
        return CMD_RET_SUCCESS;
 }
 
@@ -274,11 +203,12 @@ static int efi_dump_var_all(int argc,  char * const argv[],
  * If one or more variable names are specified, show information
  * named UEFI variables, otherwise show all the UEFI variables.
  */
-int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
+                    char *const argv[])
 {
+       const efi_guid_t *guid_p = NULL;
        efi_guid_t guid;
-       const efi_guid_t *guid_p;
-       bool default_guid, guid_any, verbose;
+       bool verbose = true;
        efi_status_t ret;
 
        /* Initialize EFI drivers */
@@ -289,31 +219,16 @@ int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                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;
-
-                       /* -a already specified */
-                       if (!default_guid & guid_any)
-                               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 {
@@ -321,13 +236,6 @@ int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                }
        }
 
-       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);
 }
@@ -353,7 +261,7 @@ static int append_value(char **bufp, size_t *sizep, char *data)
        char *tmp_buf = NULL, *new_buf = NULL, *value;
        unsigned long len = 0;
 
-       if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
+       if (!strncmp(data, "=0x", 3)) { /* hexadecimal number */
                union {
                        u8 u8;
                        u16 u16;
@@ -464,7 +372,8 @@ out:
  * Encode values specified and set given UEFI variable.
  * If no value is specified, delete the variable.
  */
-int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
+                  char *const argv[])
 {
        char *var_name, *value, *ep;
        ulong addr;
@@ -472,8 +381,7 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        efi_guid_t guid;
        u32 attributes;
        bool default_guid, verbose, value_on_memory;
-       u16 *var_name16 = NULL, *p;
-       size_t len;
+       u16 *var_name16;
        efi_status_t ret;
 
        if (argc == 1)
@@ -507,8 +415,7 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        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")) {
@@ -529,12 +436,12 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
                        argc--;
                        argv++;
-                       addr = simple_strtoul(argv[0], &ep, 16);
-                       if (*ep != ',')
+                       addr = hextoul(argv[0], &ep);
+                       if (*ep != ':')
                                return CMD_RET_USAGE;
 
                        /* 0 should be allowed for delete */
-                       size = simple_strtoul(++ep, NULL, 16);
+                       size = hextoul(++ep, NULL);
 
                        value_on_memory = true;
                } else if (!strcmp(argv[0], "-v")) {
@@ -556,8 +463,7 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        }
 
        if (verbose) {
-               printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
-                                                    &guid));
+               printf("GUID: %pUl (%pUs)\n", &guid, &guid);
                printf("Attributes: 0x%x\n", attributes);
        }
 
@@ -579,18 +485,15 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                               16, 1, value, size, true);
        }
 
-       len = utf8_utf16_strnlen(var_name, strlen(var_name));
-       var_name16 = malloc((len + 1) * 2);
+       var_name16 = efi_convert_string(var_name);
        if (!var_name16) {
                printf("## Out of memory\n");
                ret = CMD_RET_FAILURE;
                goto out;
        }
-       p = var_name16;
-       utf8_utf16_strncpy(&p, var_name, len + 1);
-
-       ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
-                                       size, value));
+       ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
+                                  true);
+       free(var_name16);
        unmap_sysmem(value);
        if (ret == EFI_SUCCESS) {
                ret = CMD_RET_SUCCESS;
@@ -625,7 +528,6 @@ out:
                unmap_sysmem(value);
        else
                free(value);
-       free(var_name16);
 
        return ret;
 }