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