Merge branch '2019-10-28-azure-ci-support'
[oweals/u-boot.git] / cmd / efidebug.c
index e3a2d093fb2ab38614a8d794024777d1591ad7d9..ef97e19d0735851f9fbdb93ee4910a53041b33b1 100644 (file)
@@ -9,8 +9,8 @@
 #include <common.h>
 #include <command.h>
 #include <efi_loader.h>
-#include <environment.h>
 #include <exports.h>
+#include <hexdump.h>
 #include <malloc.h>
 #include <search.h>
 #include <linux/ctype.h>
@@ -185,7 +185,7 @@ static const struct {
 } guid_list[] = {
        {
                "Device Path",
-               DEVICE_PATH_GUID,
+               EFI_DEVICE_PATH_PROTOCOL_GUID,
        },
        {
                "Device Path To Text",
@@ -217,7 +217,7 @@ static const struct {
        },
        {
                "Block IO",
-               BLOCK_IO_GUID,
+               EFI_BLOCK_IO_PROTOCOL_GUID,
        },
        {
                "Simple File System",
@@ -225,11 +225,31 @@ static const struct {
        },
        {
                "Loaded Image",
-               LOADED_IMAGE_PROTOCOL_GUID,
+               EFI_LOADED_IMAGE_PROTOCOL_GUID,
        },
        {
-               "GOP",
-               EFI_GOP_GUID,
+               "Graphics Output",
+               EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
+       },
+       {
+               "HII String",
+               EFI_HII_STRING_PROTOCOL_GUID,
+       },
+       {
+               "HII Database",
+               EFI_HII_DATABASE_PROTOCOL_GUID,
+       },
+       {
+               "HII Config Routing",
+               EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
+       },
+       {
+               "Simple Network",
+               EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
+       },
+       {
+               "PXE Base Code",
+               EFI_PXE_BASE_CODE_PROTOCOL_GUID,
        },
 };
 
@@ -335,6 +355,128 @@ static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
        return CMD_RET_SUCCESS;
 }
 
+static const char * const efi_mem_type_string[] = {
+       [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
+       [EFI_LOADER_CODE] = "LOADER CODE",
+       [EFI_LOADER_DATA] = "LOADER DATA",
+       [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
+       [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
+       [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
+       [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
+       [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
+       [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
+       [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
+       [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
+       [EFI_MMAP_IO] = "IO",
+       [EFI_MMAP_IO_PORT] = "IO PORT",
+       [EFI_PAL_CODE] = "PAL",
+};
+
+static const struct efi_mem_attrs {
+       const u64 bit;
+       const char *text;
+} efi_mem_attrs[] = {
+       {EFI_MEMORY_UC, "UC"},
+       {EFI_MEMORY_UC, "UC"},
+       {EFI_MEMORY_WC, "WC"},
+       {EFI_MEMORY_WT, "WT"},
+       {EFI_MEMORY_WB, "WB"},
+       {EFI_MEMORY_UCE, "UCE"},
+       {EFI_MEMORY_WP, "WP"},
+       {EFI_MEMORY_RP, "RP"},
+       {EFI_MEMORY_XP, "WP"},
+       {EFI_MEMORY_NV, "NV"},
+       {EFI_MEMORY_MORE_RELIABLE, "REL"},
+       {EFI_MEMORY_RO, "RO"},
+       {EFI_MEMORY_RUNTIME, "RT"},
+};
+
+/**
+ * print_memory_attributes() - print memory map attributes
+ *
+ * @attributes:        Attribute value
+ *
+ * Print memory map attributes
+ */
+static void print_memory_attributes(u64 attributes)
+{
+       int sep, i;
+
+       for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
+               if (attributes & efi_mem_attrs[i].bit) {
+                       if (sep) {
+                               putc('|');
+                       } else {
+                               putc(' ');
+                               sep = 1;
+                       }
+                       puts(efi_mem_attrs[i].text);
+               }
+}
+
+#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
+
+/**
+ * do_efi_show_memmap() - show UEFI memory map
+ *
+ * @cmdtp:     Command table
+ * @flag:      Command flag
+ * @argc:      Number of arguments
+ * @argv:      Argument array
+ * Return:     CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "memmap" sub-command.
+ * Show UEFI memory map.
+ */
+static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
+                             int argc, char * const argv[])
+{
+       struct efi_mem_desc *memmap = NULL, *map;
+       efi_uintn_t map_size = 0;
+       const char *type;
+       int i;
+       efi_status_t ret;
+
+       ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
+       if (ret == EFI_BUFFER_TOO_SMALL) {
+               map_size += sizeof(struct efi_mem_desc); /* for my own */
+               ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
+                                                map_size, (void *)&memmap));
+               if (ret != EFI_SUCCESS)
+                       return CMD_RET_FAILURE;
+               ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
+                                                 NULL, NULL, NULL));
+       }
+       if (ret != EFI_SUCCESS) {
+               EFI_CALL(BS->free_pool(memmap));
+               return CMD_RET_FAILURE;
+       }
+
+       printf("Type             Start%.*s End%.*s Attributes\n",
+              EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
+       printf("================ %.*s %.*s ==========\n",
+              EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
+       for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
+               if (map->type < EFI_MAX_MEMORY_TYPE)
+                       type = efi_mem_type_string[map->type];
+               else
+                       type = "(unknown)";
+
+               printf("%-16s %.*llx-%.*llx", type,
+                      EFI_PHYS_ADDR_WIDTH,
+                      map->physical_start,
+                      EFI_PHYS_ADDR_WIDTH,
+                      map->physical_start + map->num_pages * EFI_PAGE_SIZE);
+
+               print_memory_attributes(map->attribute);
+               putc('\n');
+       }
+
+       EFI_CALL(BS->free_pool(memmap));
+
+       return CMD_RET_SUCCESS;
+}
+
 /**
  * do_efi_boot_add() - set UEFI load option
  *
@@ -345,9 +487,9 @@ static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
  * Return:     CMD_RET_SUCCESS on success,
  *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  *
- * Implement efidebug "boot add" sub-command.
- * Create or change UEFI load option.
- *   - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
+ * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
+ *
+ *     efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
  */
 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
                           int argc, char * const argv[])
@@ -363,14 +505,15 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
        struct efi_load_option lo;
        void *data = NULL;
        efi_uintn_t size;
-       int ret;
+       efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc < 6 || argc > 7)
                return CMD_RET_USAGE;
 
        id = (int)simple_strtoul(argv[1], &endp, 16);
        if (*endp != '\0' || id > 0xffff)
-               return CMD_RET_FAILURE;
+               return CMD_RET_USAGE;
 
        sprintf(var_name, "Boot%04X", id);
        p = var_name16;
@@ -396,7 +539,7 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
        if (ret != EFI_SUCCESS) {
                printf("Cannot create device path for \"%s %s\"\n",
                       argv[3], argv[4]);
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
        lo.file_path = file_path;
@@ -404,26 +547,33 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
                                + sizeof(struct efi_device_path); /* for END */
 
        /* optional data */
-       lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
+       if (argc < 6)
+               lo.optional_data = NULL;
+       else
+               lo.optional_data = (const u8 *)argv[6];
 
        size = efi_serialize_load_option(&lo, (u8 **)&data);
        if (!size) {
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
 
        ret = EFI_CALL(RT->set_variable(var_name16, &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, data));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set %ls\n", var_name16);
+               r = CMD_RET_FAILURE;
+       }
 out:
        free(data);
        efi_free_pool(device_path);
        efi_free_pool(file_path);
        free(lo.label);
 
-       return ret;
+       return r;
 }
 
 /**
@@ -437,7 +587,8 @@ out:
  *
  * Implement efidebug "boot rm" sub-command.
  * Delete UEFI load options.
- *   - boot rm <id> ...
+ *
+ *     efidebug boot rm <id> ...
  */
 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
                          int argc, char * const argv[])
@@ -463,7 +614,7 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
 
                ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
                if (ret) {
-                       printf("cannot remove Boot%04X", id);
+                       printf("Cannot remove Boot%04X", id);
                        return CMD_RET_FAILURE;
                }
        }
@@ -474,12 +625,13 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
 /**
  * show_efi_boot_opt_data() - dump UEFI load option
  *
- * @id:                Load option number
- * @data:      Value of UEFI load option variable
+ * @id:                load option number
+ * @data:      value of UEFI load option variable
+ * @size:      size of the boot option
  *
  * Decode the value of UEFI load option variable and print information.
  */
-static void show_efi_boot_opt_data(int id, void *data)
+static void show_efi_boot_opt_data(int id, void *data, size_t size)
 {
        struct efi_load_option lo;
        char *label, *p;
@@ -497,7 +649,7 @@ static void show_efi_boot_opt_data(int id, void *data)
        utf16_utf8_strncpy(&p, lo.label, label_len16);
 
        printf("Boot%04X:\n", id);
-       printf("\tattributes: %c%c%c (0x%08x)\n",
+       printf("  attributes: %c%c%c (0x%08x)\n",
               /* ACTIVE */
               lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
               /* FORCE RECONNECT */
@@ -505,14 +657,16 @@ static void show_efi_boot_opt_data(int id, void *data)
               /* HIDDEN */
               lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
               lo.attributes);
-       printf("\tlabel: %s\n", label);
+       printf("  label: %s\n", label);
 
        dp_str = efi_dp_str(lo.file_path);
-       printf("\tfile_path: %ls\n", dp_str);
+       printf("  file_path: %ls\n", dp_str);
        efi_free_pool(dp_str);
 
-       printf("\tdata: %s\n", lo.optional_data);
-
+       printf("  data:\n");
+       print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1,
+                      lo.optional_data, size + (u8 *)data -
+                      (u8 *)lo.optional_data, true);
        free(label);
 }
 
@@ -545,13 +699,24 @@ static void show_efi_boot_opt(int id)
                                                data));
        }
        if (ret == EFI_SUCCESS)
-               show_efi_boot_opt_data(id, data);
+               show_efi_boot_opt_data(id, data, size);
        else if (ret == EFI_NOT_FOUND)
                printf("Boot%04X: not found\n", id);
 
        free(data);
 }
 
+static int u16_tohex(u16 c)
+{
+       if (c >= '0' && c <= '9')
+               return c - '0';
+       if (c >= 'A' && c <= 'F')
+               return c - 'A' + 10;
+
+       /* not hexadecimal */
+       return -1;
+}
+
 /**
  * show_efi_boot_dump() - dump all UEFI load options
  *
@@ -563,43 +728,64 @@ static void show_efi_boot_opt(int id)
  *
  * Implement efidebug "boot dump" sub-command.
  * Dump information of all UEFI load options defined.
- *   - boot dump
+ *
+ *     efidebug boot dump
  */
 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, digit;
+       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);
-
-       if (!len)
-               return CMD_RET_SUCCESS;
-
-       if (len < 0)
+       buf_size = 128;
+       var_name16 = malloc(buf_size);
+       if (!var_name16)
                return CMD_RET_FAILURE;
 
-       boot = variables;
-       while (*boot) {
-               value = strstr(boot, "Boot") + 4;
-               id = (int)simple_strtoul(value, NULL, 16);
-               show_efi_boot_opt(id);
-               boot = strchr(boot, '\n');
-               if (!*boot)
+       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;
-               boot++;
+               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 (memcmp(var_name16, L"Boot", 8))
+                       continue;
+
+               for (id = 0, i = 0; i < 4; i++) {
+                       digit = u16_tohex(var_name16[4 + i]);
+                       if (digit < 0)
+                               break;
+                       id = (id << 4) + digit;
+               }
+               if (i == 4 && !var_name16[8])
+                       show_efi_boot_opt(id);
        }
-       free(variables);
+
+       free(var_name16);
 
        return CMD_RET_SUCCESS;
 }
@@ -706,7 +892,8 @@ out:
  *
  * Implement efidebug "boot next" sub-command.
  * Set BootNext variable.
- *   - boot next <id>
+ *
+ *     efidebug boot next <id>
  */
 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
                            int argc, char * const argv[])
@@ -716,6 +903,7 @@ static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
        char *endp;
        efi_guid_t guid;
        efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc != 2)
                return CMD_RET_USAGE;
@@ -723,19 +911,23 @@ static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
        bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
        if (*endp != '\0' || bootnext > 0xffff) {
                printf("invalid value: %s\n", argv[1]);
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
 
        guid = efi_global_variable_guid;
        size = sizeof(u16);
        ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, &bootnext));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set BootNext\n");
+               r = CMD_RET_FAILURE;
+       }
 out:
-       return ret;
+       return r;
 }
 
 /**
@@ -749,7 +941,8 @@ out:
  *
  * Implement efidebug "boot order" sub-command.
  * Show order of UEFI load options, or change it in BootOrder variable.
- *   - boot order [<id> ...]
+ *
+ *     efidebug boot order [<id> ...]
  */
 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
                             int argc, char * const argv[])
@@ -760,6 +953,7 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
        char *endp;
        efi_guid_t guid;
        efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc == 1)
                return show_efi_boot_order();
@@ -776,7 +970,7 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
                id = (int)simple_strtoul(argv[i], &endp, 16);
                if (*endp != '\0' || id > 0xffff) {
                        printf("invalid value: %s\n", argv[i]);
-                       ret = CMD_RET_FAILURE;
+                       r = CMD_RET_FAILURE;
                        goto out;
                }
 
@@ -785,14 +979,18 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
 
        guid = efi_global_variable_guid;
        ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, bootorder));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set BootOrder\n");
+               r = CMD_RET_FAILURE;
+       }
 out:
        free(bootorder);
 
-       return ret;
+       return r;
 }
 
 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
@@ -815,7 +1013,6 @@ static cmd_tbl_t cmd_efidebug_boot_sub[] = {
  *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  *
  * Implement efidebug "boot" sub-command.
- * See above for details of sub-commands.
  */
 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
                           int argc, char * const argv[])
@@ -845,6 +1042,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
                         "", ""),
        U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
                         "", ""),
+       U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
+                        "", ""),
 };
 
 /**
@@ -859,7 +1058,6 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
  *
  * Implement efidebug command which allows us to display and
  * configure UEFI environment.
- * See above for details of sub-commands.
  */
 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
                       int argc, char * const argv[])
@@ -911,7 +1109,9 @@ static char efidebug_help_text[] =
        "efidebug dh\n"
        "  - show uefi handles\n"
        "efidebug images\n"
-       "  - show loaded images\n";
+       "  - show loaded images\n"
+       "efidebug memmap\n"
+       "  - show uefi memory map\n";
 #endif
 
 U_BOOT_CMD(