Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f1a0baf | 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 | f1a0baf | 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> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 8bef79b | 2019-11-14 12:57:19 -0700 | [diff] [blame] | 13 | #include <sort.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 15 | |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 18 | static const char *const type_name[] = { |
| 19 | "reserved", |
| 20 | "loader_code", |
| 21 | "loader_data", |
| 22 | "bs_code", |
| 23 | "bs_data", |
| 24 | "rt_code", |
| 25 | "rt_data", |
| 26 | "conv", |
| 27 | "unusable", |
| 28 | "acpi_reclaim", |
| 29 | "acpi_nvs", |
| 30 | "io", |
| 31 | "io_port", |
| 32 | "pal_code", |
| 33 | }; |
| 34 | |
| 35 | static struct attr_info { |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 36 | u64 val; |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 37 | const char *name; |
| 38 | } mem_attr[] = { |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 39 | { EFI_MEMORY_UC, "uncached" }, |
| 40 | { EFI_MEMORY_WC, "write-coalescing" }, |
| 41 | { EFI_MEMORY_WT, "write-through" }, |
| 42 | { EFI_MEMORY_WB, "write-back" }, |
| 43 | { EFI_MEMORY_UCE, "uncached & exported" }, |
| 44 | { EFI_MEMORY_WP, "write-protect" }, |
| 45 | { EFI_MEMORY_RP, "read-protect" }, |
| 46 | { EFI_MEMORY_XP, "execute-protect" }, |
Eugeniu Rosca | c3a40cc | 2018-07-14 22:53:31 +0200 | [diff] [blame] | 47 | { EFI_MEMORY_NV, "non-volatile" }, |
| 48 | { EFI_MEMORY_MORE_RELIABLE, "higher reliability" }, |
| 49 | { EFI_MEMORY_RO, "read-only" }, |
Heinrich Schuchardt | 255a473 | 2020-05-19 07:20:46 +0200 | [diff] [blame] | 50 | { EFI_MEMORY_SP, "specific purpose" }, |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 51 | { EFI_MEMORY_RUNTIME, "needs runtime mapping" } |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /* Maximum different attribute values we can track */ |
| 55 | #define ATTR_SEEN_MAX 30 |
| 56 | |
| 57 | static inline bool is_boot_services(int type) |
| 58 | { |
| 59 | return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA || |
| 60 | type == EFI_BOOT_SERVICES_CODE || |
| 61 | type == EFI_BOOT_SERVICES_DATA; |
| 62 | } |
| 63 | |
| 64 | static int h_cmp_entry(const void *v1, const void *v2) |
| 65 | { |
| 66 | const struct efi_mem_desc *desc1 = v1; |
| 67 | const struct efi_mem_desc *desc2 = v2; |
| 68 | int64_t diff = desc1->physical_start - desc2->physical_start; |
| 69 | |
| 70 | /* |
| 71 | * Manually calculate the difference to avoid sign loss in the 64-bit |
| 72 | * to 32-bit conversion |
| 73 | */ |
| 74 | return diff < 0 ? -1 : diff > 0 ? 1 : 0; |
| 75 | } |
| 76 | |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 77 | /** |
| 78 | * efi_build_mem_table() - make a sorted copy of the memory table |
| 79 | * |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 80 | * @desc_base: Pointer to EFI memory map table |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 81 | * @size: Size of table in bytes |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 82 | * @desc_size: Size of each @desc_base record |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 83 | * @skip_bs: True to skip boot-time memory and merge it with conventional |
| 84 | * memory. This will significantly reduce the number of table |
| 85 | * entries. |
| 86 | * Return: pointer to the new table. It should be freed with free() by the |
| 87 | * caller. |
| 88 | */ |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 89 | static void *efi_build_mem_table(struct efi_mem_desc *desc_base, int size, |
| 90 | int desc_size, bool skip_bs) |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 91 | { |
| 92 | struct efi_mem_desc *desc, *end, *base, *dest, *prev; |
| 93 | int count; |
| 94 | u64 addr; |
| 95 | |
| 96 | base = malloc(size + sizeof(*desc)); |
| 97 | if (!base) { |
| 98 | debug("%s: Cannot allocate %#x bytes\n", __func__, size); |
| 99 | return NULL; |
| 100 | } |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 101 | end = (void *)desc_base + size; |
| 102 | count = ((ulong)end - (ulong)desc_base) / desc_size; |
| 103 | memcpy(base, desc_base, (ulong)end - (ulong)desc_base); |
| 104 | qsort(base, count, desc_size, h_cmp_entry); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 105 | prev = NULL; |
| 106 | addr = 0; |
| 107 | dest = base; |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 108 | end = (struct efi_mem_desc *)((ulong)base + count * desc_size); |
| 109 | for (desc = base; desc < end; |
| 110 | desc = efi_get_next_mem_desc(desc, desc_size)) { |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 111 | bool merge = true; |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 112 | u32 type = desc->type; |
| 113 | |
| 114 | if (type >= EFI_MAX_MEMORY_TYPE) { |
| 115 | printf("Memory map contains invalid entry type %u\n", |
| 116 | type); |
| 117 | continue; |
| 118 | } |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 119 | |
| 120 | if (skip_bs && is_boot_services(desc->type)) |
| 121 | type = EFI_CONVENTIONAL_MEMORY; |
| 122 | |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 123 | memcpy(dest, desc, desc_size); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 124 | dest->type = type; |
| 125 | if (!skip_bs || !prev) |
| 126 | merge = false; |
| 127 | else if (desc->physical_start != addr) |
| 128 | merge = false; |
| 129 | else if (type != EFI_CONVENTIONAL_MEMORY) |
| 130 | merge = false; |
| 131 | else if (prev->type != EFI_CONVENTIONAL_MEMORY) |
| 132 | merge = false; |
| 133 | |
| 134 | if (merge) { |
| 135 | prev->num_pages += desc->num_pages; |
| 136 | } else { |
| 137 | prev = dest; |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 138 | dest = efi_get_next_mem_desc(dest, desc_size); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 139 | } |
| 140 | addr = desc->physical_start + (desc->num_pages << |
| 141 | EFI_PAGE_SHIFT); |
| 142 | } |
| 143 | |
| 144 | /* Mark the end */ |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 145 | dest->type = EFI_MAX_MEMORY_TYPE; |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 146 | |
| 147 | return base; |
| 148 | } |
| 149 | |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 150 | static void efi_print_mem_table(struct efi_mem_desc *desc, int desc_size, |
| 151 | bool skip_bs) |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 152 | { |
| 153 | u64 attr_seen[ATTR_SEEN_MAX]; |
| 154 | int attr_seen_count; |
| 155 | int upto, i; |
| 156 | u64 addr; |
| 157 | |
| 158 | printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical", |
| 159 | "Virtual", "Size", "Attributes"); |
| 160 | |
| 161 | /* Keep track of all the different attributes we have seen */ |
| 162 | attr_seen_count = 0; |
| 163 | addr = 0; |
Heinrich Schuchardt | 5b94e26 | 2020-08-27 12:39:03 +0200 | [diff] [blame] | 164 | for (upto = 0; desc->type != EFI_MAX_MEMORY_TYPE; |
Simon Glass | ce1dc0c | 2022-01-04 03:51:11 -0700 | [diff] [blame] | 165 | upto++, desc = efi_get_next_mem_desc(desc, desc_size)) { |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 166 | const char *name; |
| 167 | u64 size; |
| 168 | |
| 169 | if (skip_bs && is_boot_services(desc->type)) |
| 170 | continue; |
| 171 | if (desc->physical_start != addr) { |
| 172 | printf(" %-14s %010llx %10s %010llx\n", "<gap>", |
| 173 | addr, "", desc->physical_start - addr); |
| 174 | } |
| 175 | size = desc->num_pages << EFI_PAGE_SHIFT; |
| 176 | |
| 177 | name = desc->type < ARRAY_SIZE(type_name) ? |
| 178 | type_name[desc->type] : "<invalid>"; |
| 179 | printf("%2d %x:%-12s %010llx %010llx %010llx ", upto, |
| 180 | desc->type, name, desc->physical_start, |
| 181 | desc->virtual_start, size); |
| 182 | if (desc->attribute & EFI_MEMORY_RUNTIME) |
| 183 | putc('r'); |
| 184 | printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME); |
| 185 | putc('\n'); |
| 186 | |
| 187 | for (i = 0; i < attr_seen_count; i++) { |
| 188 | if (attr_seen[i] == desc->attribute) |
| 189 | break; |
| 190 | } |
| 191 | if (i == attr_seen_count && i < ATTR_SEEN_MAX) |
| 192 | attr_seen[attr_seen_count++] = desc->attribute; |
| 193 | addr = desc->physical_start + size; |
| 194 | } |
| 195 | |
| 196 | printf("\nAttributes key:\n"); |
| 197 | for (i = 0; i < attr_seen_count; i++) { |
| 198 | u64 attr = attr_seen[i]; |
| 199 | bool first; |
| 200 | int j; |
| 201 | |
Eugeniu Rosca | dbb148b | 2018-07-14 22:53:32 +0200 | [diff] [blame] | 202 | printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ', |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 203 | attr & ~EFI_MEMORY_RUNTIME); |
| 204 | for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) { |
Eugeniu Rosca | 9b89183 | 2018-07-14 22:53:30 +0200 | [diff] [blame] | 205 | if (attr & mem_attr[j].val) { |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 206 | if (first) |
| 207 | first = false; |
| 208 | else |
| 209 | printf(", "); |
| 210 | printf("%s", mem_attr[j].name); |
| 211 | } |
| 212 | } |
| 213 | putc('\n'); |
| 214 | } |
| 215 | if (skip_bs) |
| 216 | printf("*Some areas are merged (use 'all' to see)\n"); |
| 217 | } |
| 218 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 219 | static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc, |
| 220 | char *const argv[]) |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 221 | { |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 222 | struct efi_mem_desc *orig, *desc; |
| 223 | uint version, key; |
| 224 | int desc_size; |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 225 | int size, ret; |
| 226 | bool skip_bs; |
| 227 | |
| 228 | skip_bs = !argc || *argv[0] != 'a'; |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 229 | if (IS_ENABLED(CONFIG_EFI_APP)) { |
| 230 | ret = efi_get_mmap(&orig, &size, &key, &desc_size, &version); |
| 231 | if (ret) { |
| 232 | printf("Cannot read memory map (err=%d)\n", ret); |
| 233 | return CMD_RET_FAILURE; |
| 234 | } |
| 235 | } else { |
| 236 | struct efi_entry_memmap *map; |
| 237 | |
| 238 | ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); |
| 239 | switch (ret) { |
| 240 | case -ENOENT: |
| 241 | printf("No EFI table available\n"); |
| 242 | goto done; |
| 243 | case -EPROTONOSUPPORT: |
| 244 | printf("Incorrect EFI table version\n"); |
| 245 | goto done; |
| 246 | } |
| 247 | orig = map->desc; |
| 248 | desc_size = map->desc_size; |
| 249 | version = map->version; |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 250 | } |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 251 | printf("EFI table at %lx, memory map %p, size %x, key %x, version %x, descr. size %#x\n", |
| 252 | gd->arch.table, orig, size, key, version, desc_size); |
| 253 | if (version != EFI_MEM_DESC_VERSION) { |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 254 | printf("Incorrect memory map version\n"); |
| 255 | ret = -EPROTONOSUPPORT; |
| 256 | goto done; |
| 257 | } |
| 258 | |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 259 | desc = efi_build_mem_table(orig, size, desc_size, skip_bs); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 260 | if (!desc) { |
| 261 | ret = -ENOMEM; |
| 262 | goto done; |
| 263 | } |
| 264 | |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 265 | efi_print_mem_table(desc, desc_size, skip_bs); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 266 | free(desc); |
Simon Glass | 25a326b | 2022-01-04 03:51:12 -0700 | [diff] [blame] | 267 | if (IS_ENABLED(CONFIG_EFI_APP)) |
| 268 | free(orig); |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 269 | done: |
| 270 | if (ret) |
| 271 | printf("Error: %d\n", ret); |
| 272 | |
| 273 | return ret ? CMD_RET_FAILURE : 0; |
| 274 | } |
| 275 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 276 | static struct cmd_tbl efi_commands[] = { |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 277 | U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""), |
| 278 | }; |
| 279 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 280 | static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 281 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 282 | struct cmd_tbl *efi_cmd; |
Simon Glass | f1a0baf | 2015-08-04 12:33:59 -0600 | [diff] [blame] | 283 | int ret; |
| 284 | |
| 285 | if (argc < 2) |
| 286 | return CMD_RET_USAGE; |
| 287 | efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands)); |
| 288 | argc -= 2; |
| 289 | argv += 2; |
| 290 | if (!efi_cmd || argc > efi_cmd->maxargs) |
| 291 | return CMD_RET_USAGE; |
| 292 | |
| 293 | ret = efi_cmd->cmd(efi_cmd, flag, argc, argv); |
| 294 | |
| 295 | return cmd_process_error(efi_cmd, ret); |
| 296 | } |
| 297 | |
| 298 | U_BOOT_CMD( |
| 299 | efi, 3, 1, do_efi, |
| 300 | "EFI access", |
| 301 | "mem [all] Dump memory information [include boot services]" |
| 302 | ); |