1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
14 #include <asm/global_data.h>
16 static const char *const type_name[] = {
33 static struct attr_info {
37 { EFI_MEMORY_UC, "uncached" },
38 { EFI_MEMORY_WC, "write-coalescing" },
39 { EFI_MEMORY_WT, "write-through" },
40 { EFI_MEMORY_WB, "write-back" },
41 { EFI_MEMORY_UCE, "uncached & exported" },
42 { EFI_MEMORY_WP, "write-protect" },
43 { EFI_MEMORY_RP, "read-protect" },
44 { EFI_MEMORY_XP, "execute-protect" },
45 { EFI_MEMORY_NV, "non-volatile" },
46 { EFI_MEMORY_MORE_RELIABLE, "higher reliability" },
47 { EFI_MEMORY_RO, "read-only" },
48 { EFI_MEMORY_SP, "specific purpose" },
49 { EFI_MEMORY_RUNTIME, "needs runtime mapping" }
52 /* Maximum different attribute values we can track */
53 #define ATTR_SEEN_MAX 30
55 static inline bool is_boot_services(int type)
57 return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA ||
58 type == EFI_BOOT_SERVICES_CODE ||
59 type == EFI_BOOT_SERVICES_DATA;
62 static int h_cmp_entry(const void *v1, const void *v2)
64 const struct efi_mem_desc *desc1 = v1;
65 const struct efi_mem_desc *desc2 = v2;
66 int64_t diff = desc1->physical_start - desc2->physical_start;
69 * Manually calculate the difference to avoid sign loss in the 64-bit
70 * to 32-bit conversion
72 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
76 * efi_build_mem_table() - make a sorted copy of the memory table
78 * @map: Pointer to EFI memory map table
79 * @size: Size of table in bytes
80 * @skip_bs: True to skip boot-time memory and merge it with conventional
81 * memory. This will significantly reduce the number of table
83 * Return: pointer to the new table. It should be freed with free() by the
86 static void *efi_build_mem_table(struct efi_entry_memmap *map, int size,
89 struct efi_mem_desc *desc, *end, *base, *dest, *prev;
93 base = malloc(size + sizeof(*desc));
95 debug("%s: Cannot allocate %#x bytes\n", __func__, size);
98 end = (struct efi_mem_desc *)((ulong)map + size);
99 count = ((ulong)end - (ulong)map->desc) / map->desc_size;
100 memcpy(base, map->desc, (ulong)end - (ulong)map->desc);
101 qsort(base, count, map->desc_size, h_cmp_entry);
105 end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size);
106 for (desc = base; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
108 u32 type = desc->type;
110 if (type >= EFI_MAX_MEMORY_TYPE) {
111 printf("Memory map contains invalid entry type %u\n",
116 if (skip_bs && is_boot_services(desc->type))
117 type = EFI_CONVENTIONAL_MEMORY;
119 memcpy(dest, desc, map->desc_size);
121 if (!skip_bs || !prev)
123 else if (desc->physical_start != addr)
125 else if (type != EFI_CONVENTIONAL_MEMORY)
127 else if (prev->type != EFI_CONVENTIONAL_MEMORY)
131 prev->num_pages += desc->num_pages;
134 dest = efi_get_next_mem_desc(map, dest);
136 addr = desc->physical_start + (desc->num_pages <<
141 dest->type = EFI_MAX_MEMORY_TYPE;
146 static void efi_print_mem_table(struct efi_entry_memmap *map,
147 struct efi_mem_desc *desc, bool skip_bs)
149 u64 attr_seen[ATTR_SEEN_MAX];
154 printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical",
155 "Virtual", "Size", "Attributes");
157 /* Keep track of all the different attributes we have seen */
160 for (upto = 0; desc->type != EFI_MAX_MEMORY_TYPE;
161 upto++, desc = efi_get_next_mem_desc(map, desc)) {
165 if (skip_bs && is_boot_services(desc->type))
167 if (desc->physical_start != addr) {
168 printf(" %-14s %010llx %10s %010llx\n", "<gap>",
169 addr, "", desc->physical_start - addr);
171 size = desc->num_pages << EFI_PAGE_SHIFT;
173 name = desc->type < ARRAY_SIZE(type_name) ?
174 type_name[desc->type] : "<invalid>";
175 printf("%2d %x:%-12s %010llx %010llx %010llx ", upto,
176 desc->type, name, desc->physical_start,
177 desc->virtual_start, size);
178 if (desc->attribute & EFI_MEMORY_RUNTIME)
180 printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME);
183 for (i = 0; i < attr_seen_count; i++) {
184 if (attr_seen[i] == desc->attribute)
187 if (i == attr_seen_count && i < ATTR_SEEN_MAX)
188 attr_seen[attr_seen_count++] = desc->attribute;
189 addr = desc->physical_start + size;
192 printf("\nAttributes key:\n");
193 for (i = 0; i < attr_seen_count; i++) {
194 u64 attr = attr_seen[i];
198 printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ',
199 attr & ~EFI_MEMORY_RUNTIME);
200 for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
201 if (attr & mem_attr[j].val) {
206 printf("%s", mem_attr[j].name);
212 printf("*Some areas are merged (use 'all' to see)\n");
215 static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc,
218 struct efi_mem_desc *desc;
219 struct efi_entry_memmap *map;
223 skip_bs = !argc || *argv[0] != 'a';
224 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
227 printf("No EFI table available\n");
229 case -EPROTONOSUPPORT:
230 printf("Incorrect EFI table version\n");
233 printf("EFI table at %lx, memory map %p, size %x, version %x, descr. size %#x\n",
234 gd->arch.table, map, size, map->version, map->desc_size);
235 if (map->version != EFI_MEM_DESC_VERSION) {
236 printf("Incorrect memory map version\n");
237 ret = -EPROTONOSUPPORT;
241 desc = efi_build_mem_table(map, size, skip_bs);
247 efi_print_mem_table(map, desc, skip_bs);
251 printf("Error: %d\n", ret);
253 return ret ? CMD_RET_FAILURE : 0;
256 static struct cmd_tbl efi_commands[] = {
257 U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""),
260 static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
262 struct cmd_tbl *efi_cmd;
266 return CMD_RET_USAGE;
267 efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands));
270 if (!efi_cmd || argc > efi_cmd->maxargs)
271 return CMD_RET_USAGE;
273 ret = efi_cmd->cmd(efi_cmd, flag, argc, argv);
275 return cmd_process_error(efi_cmd, ret);
281 "mem [all] Dump memory information [include boot services]"