Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <efi.h> |
| 10 | #include <errno.h> |
| 11 | #include <malloc.h> |
Simon Glass | 16ff570 | 2019-11-14 12:57:19 -0700 | [diff] [blame] | 12 | #include <sort.h> |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 13 | |
| 14 | static const char *const type_name[] = { |
| 15 | "reserved", |
| 16 | "loader_code", |
| 17 | "loader_data", |
| 18 | "bs_code", |
| 19 | "bs_data", |
| 20 | "rt_code", |
| 21 | "rt_data", |
| 22 | "conv", |
| 23 | "unusable", |
| 24 | "acpi_reclaim", |
| 25 | "acpi_nvs", |
| 26 | "io", |
| 27 | "io_port", |
| 28 | "pal_code", |
| 29 | }; |
| 30 | |
| 31 | static struct attr_info { |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 32 | u64 val; |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 33 | const char *name; |
| 34 | } mem_attr[] = { |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 35 | { EFI_MEMORY_UC, "uncached" }, |
| 36 | { EFI_MEMORY_WC, "write-coalescing" }, |
| 37 | { EFI_MEMORY_WT, "write-through" }, |
| 38 | { EFI_MEMORY_WB, "write-back" }, |
| 39 | { EFI_MEMORY_UCE, "uncached & exported" }, |
| 40 | { EFI_MEMORY_WP, "write-protect" }, |
| 41 | { EFI_MEMORY_RP, "read-protect" }, |
| 42 | { EFI_MEMORY_XP, "execute-protect" }, |
Eugeniu Rosca | 4e44233 | 2018-07-14 22:53:31 +0200 | [diff] [blame] | 43 | { EFI_MEMORY_NV, "non-volatile" }, |
| 44 | { EFI_MEMORY_MORE_RELIABLE, "higher reliability" }, |
| 45 | { EFI_MEMORY_RO, "read-only" }, |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 46 | { EFI_MEMORY_RUNTIME, "needs runtime mapping" } |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* Maximum different attribute values we can track */ |
| 50 | #define ATTR_SEEN_MAX 30 |
| 51 | |
| 52 | static inline bool is_boot_services(int type) |
| 53 | { |
| 54 | return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA || |
| 55 | type == EFI_BOOT_SERVICES_CODE || |
| 56 | type == EFI_BOOT_SERVICES_DATA; |
| 57 | } |
| 58 | |
| 59 | static int h_cmp_entry(const void *v1, const void *v2) |
| 60 | { |
| 61 | const struct efi_mem_desc *desc1 = v1; |
| 62 | const struct efi_mem_desc *desc2 = v2; |
| 63 | int64_t diff = desc1->physical_start - desc2->physical_start; |
| 64 | |
| 65 | /* |
| 66 | * Manually calculate the difference to avoid sign loss in the 64-bit |
| 67 | * to 32-bit conversion |
| 68 | */ |
| 69 | return diff < 0 ? -1 : diff > 0 ? 1 : 0; |
| 70 | } |
| 71 | |
| 72 | void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs) |
| 73 | { |
| 74 | struct efi_mem_desc *desc, *end, *base, *dest, *prev; |
| 75 | int count; |
| 76 | u64 addr; |
| 77 | |
| 78 | base = malloc(size + sizeof(*desc)); |
| 79 | if (!base) { |
| 80 | debug("%s: Cannot allocate %#x bytes\n", __func__, size); |
| 81 | return NULL; |
| 82 | } |
| 83 | end = (struct efi_mem_desc *)((ulong)map + size); |
| 84 | count = ((ulong)end - (ulong)map->desc) / map->desc_size; |
| 85 | memcpy(base, map->desc, (ulong)end - (ulong)map->desc); |
| 86 | qsort(base, count, map->desc_size, h_cmp_entry); |
| 87 | prev = NULL; |
| 88 | addr = 0; |
| 89 | dest = base; |
Bin Meng | 8db8790 | 2018-06-22 01:38:31 -0700 | [diff] [blame] | 90 | end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size); |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 91 | for (desc = base; desc < end; desc = efi_get_next_mem_desc(map, desc)) { |
| 92 | bool merge = true; |
| 93 | int type = desc->type; |
| 94 | |
| 95 | if (skip_bs && is_boot_services(desc->type)) |
| 96 | type = EFI_CONVENTIONAL_MEMORY; |
| 97 | |
| 98 | memcpy(dest, desc, map->desc_size); |
| 99 | dest->type = type; |
| 100 | if (!skip_bs || !prev) |
| 101 | merge = false; |
| 102 | else if (desc->physical_start != addr) |
| 103 | merge = false; |
| 104 | else if (type != EFI_CONVENTIONAL_MEMORY) |
| 105 | merge = false; |
| 106 | else if (prev->type != EFI_CONVENTIONAL_MEMORY) |
| 107 | merge = false; |
| 108 | |
| 109 | if (merge) { |
| 110 | prev->num_pages += desc->num_pages; |
| 111 | } else { |
| 112 | prev = dest; |
| 113 | dest = efi_get_next_mem_desc(map, dest); |
| 114 | } |
| 115 | addr = desc->physical_start + (desc->num_pages << |
| 116 | EFI_PAGE_SHIFT); |
| 117 | } |
| 118 | |
| 119 | /* Mark the end */ |
| 120 | dest->type = EFI_TABLE_END; |
| 121 | |
| 122 | return base; |
| 123 | } |
| 124 | |
| 125 | static void efi_print_mem_table(struct efi_entry_memmap *map, |
| 126 | struct efi_mem_desc *desc, bool skip_bs) |
| 127 | { |
| 128 | u64 attr_seen[ATTR_SEEN_MAX]; |
| 129 | int attr_seen_count; |
| 130 | int upto, i; |
| 131 | u64 addr; |
| 132 | |
| 133 | printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical", |
| 134 | "Virtual", "Size", "Attributes"); |
| 135 | |
| 136 | /* Keep track of all the different attributes we have seen */ |
| 137 | attr_seen_count = 0; |
| 138 | addr = 0; |
| 139 | for (upto = 0; desc->type != EFI_TABLE_END; |
| 140 | upto++, desc = efi_get_next_mem_desc(map, desc)) { |
| 141 | const char *name; |
| 142 | u64 size; |
| 143 | |
| 144 | if (skip_bs && is_boot_services(desc->type)) |
| 145 | continue; |
| 146 | if (desc->physical_start != addr) { |
| 147 | printf(" %-14s %010llx %10s %010llx\n", "<gap>", |
| 148 | addr, "", desc->physical_start - addr); |
| 149 | } |
| 150 | size = desc->num_pages << EFI_PAGE_SHIFT; |
| 151 | |
| 152 | name = desc->type < ARRAY_SIZE(type_name) ? |
| 153 | type_name[desc->type] : "<invalid>"; |
| 154 | printf("%2d %x:%-12s %010llx %010llx %010llx ", upto, |
| 155 | desc->type, name, desc->physical_start, |
| 156 | desc->virtual_start, size); |
| 157 | if (desc->attribute & EFI_MEMORY_RUNTIME) |
| 158 | putc('r'); |
| 159 | printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME); |
| 160 | putc('\n'); |
| 161 | |
| 162 | for (i = 0; i < attr_seen_count; i++) { |
| 163 | if (attr_seen[i] == desc->attribute) |
| 164 | break; |
| 165 | } |
| 166 | if (i == attr_seen_count && i < ATTR_SEEN_MAX) |
| 167 | attr_seen[attr_seen_count++] = desc->attribute; |
| 168 | addr = desc->physical_start + size; |
| 169 | } |
| 170 | |
| 171 | printf("\nAttributes key:\n"); |
| 172 | for (i = 0; i < attr_seen_count; i++) { |
| 173 | u64 attr = attr_seen[i]; |
| 174 | bool first; |
| 175 | int j; |
| 176 | |
Eugeniu Rosca | f0426f6 | 2018-07-14 22:53:32 +0200 | [diff] [blame] | 177 | printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ', |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 178 | attr & ~EFI_MEMORY_RUNTIME); |
| 179 | for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) { |
Eugeniu Rosca | c86c081 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 180 | if (attr & mem_attr[j].val) { |
Simon Glass | 2997bb4 | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 181 | if (first) |
| 182 | first = false; |
| 183 | else |
| 184 | printf(", "); |
| 185 | printf("%s", mem_attr[j].name); |
| 186 | } |
| 187 | } |
| 188 | putc('\n'); |
| 189 | } |
| 190 | if (skip_bs) |
| 191 | printf("*Some areas are merged (use 'all' to see)\n"); |
| 192 | } |
| 193 | |
| 194 | static int do_efi_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 195 | { |
| 196 | struct efi_mem_desc *desc; |
| 197 | struct efi_entry_memmap *map; |
| 198 | int size, ret; |
| 199 | bool skip_bs; |
| 200 | |
| 201 | skip_bs = !argc || *argv[0] != 'a'; |
| 202 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 203 | switch (ret) { |
| 204 | case -ENOENT: |
| 205 | printf("No EFI table available\n"); |
| 206 | goto done; |
| 207 | case -EPROTONOSUPPORT: |
| 208 | printf("Incorrect EFI table version\n"); |
| 209 | goto done; |
| 210 | } |
| 211 | printf("EFI table at %lx, memory map %p, size %x, version %x, descr. size %#x\n", |
| 212 | gd->arch.table, map, size, map->version, map->desc_size); |
| 213 | if (map->version != EFI_MEM_DESC_VERSION) { |
| 214 | printf("Incorrect memory map version\n"); |
| 215 | ret = -EPROTONOSUPPORT; |
| 216 | goto done; |
| 217 | } |
| 218 | |
| 219 | desc = efi_build_mem_table(map, size, skip_bs); |
| 220 | if (!desc) { |
| 221 | ret = -ENOMEM; |
| 222 | goto done; |
| 223 | } |
| 224 | |
| 225 | efi_print_mem_table(map, desc, skip_bs); |
| 226 | free(desc); |
| 227 | done: |
| 228 | if (ret) |
| 229 | printf("Error: %d\n", ret); |
| 230 | |
| 231 | return ret ? CMD_RET_FAILURE : 0; |
| 232 | } |
| 233 | |
| 234 | static cmd_tbl_t efi_commands[] = { |
| 235 | U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""), |
| 236 | }; |
| 237 | |
| 238 | static int do_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 239 | { |
| 240 | cmd_tbl_t *efi_cmd; |
| 241 | int ret; |
| 242 | |
| 243 | if (argc < 2) |
| 244 | return CMD_RET_USAGE; |
| 245 | efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands)); |
| 246 | argc -= 2; |
| 247 | argv += 2; |
| 248 | if (!efi_cmd || argc > efi_cmd->maxargs) |
| 249 | return CMD_RET_USAGE; |
| 250 | |
| 251 | ret = efi_cmd->cmd(efi_cmd, flag, argc, argv); |
| 252 | |
| 253 | return cmd_process_error(efi_cmd, ret); |
| 254 | } |
| 255 | |
| 256 | U_BOOT_CMD( |
| 257 | efi, 3, 1, do_efi, |
| 258 | "EFI access", |
| 259 | "mem [all] Dump memory information [include boot services]" |
| 260 | ); |