cmd: efidebug: add memmap command
authorAKASHI Takahiro <takahiro.akashi@linaro.org>
Mon, 25 Feb 2019 06:54:43 +0000 (15:54 +0900)
committerHeinrich Schuchardt <xypron.glpk@gmx.de>
Mon, 25 Feb 2019 11:47:13 +0000 (12:47 +0100)
"memmap" command prints uefi-specific memory map information.
=> efi memmap
Type             Start            End              Attributes
================ ================ ================ ==========
CONVENTIONAL     0000000040000000-000000007de27000 WB
RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
RESERVED         000000007de28000-000000007de2a000 WB
RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
RESERVED         000000007de2b000-000000007de2c000 WB
RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
LOADER DATA      000000007de2d000-000000007ff37000 WB
RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
LOADER DATA      000000007ff38000-0000000080000000 WB

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
cmd/efidebug.c

index e3a2d09..5072a7b 100644 (file)
@@ -335,6 +335,127 @@ 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
  *
@@ -845,6 +966,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,
+                        "", ""),
 };
 
 /**
@@ -911,7 +1034,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(