blob: 1558cb17eb0a58c6f7da4ab8b028c4285a3d8a65 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf1a0baf2015-08-04 12:33:59 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf1a0baf2015-08-04 12:33:59 -06005 */
6
7#include <common.h>
8#include <command.h>
9#include <efi.h>
10#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glassf1a0baf2015-08-04 12:33:59 -060012#include <malloc.h>
Simon Glass8bef79b2019-11-14 12:57:19 -070013#include <sort.h>
Simon Glassf1a0baf2015-08-04 12:33:59 -060014
15static 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
32static struct attr_info {
Eugeniu Rosca9b891832018-07-14 22:53:30 +020033 u64 val;
Simon Glassf1a0baf2015-08-04 12:33:59 -060034 const char *name;
35} mem_attr[] = {
Eugeniu Rosca9b891832018-07-14 22:53:30 +020036 { 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 Roscac3a40cc2018-07-14 22:53:31 +020044 { EFI_MEMORY_NV, "non-volatile" },
45 { EFI_MEMORY_MORE_RELIABLE, "higher reliability" },
46 { EFI_MEMORY_RO, "read-only" },
Heinrich Schuchardt255a4732020-05-19 07:20:46 +020047 { EFI_MEMORY_SP, "specific purpose" },
Eugeniu Rosca9b891832018-07-14 22:53:30 +020048 { EFI_MEMORY_RUNTIME, "needs runtime mapping" }
Simon Glassf1a0baf2015-08-04 12:33:59 -060049};
50
51/* Maximum different attribute values we can track */
52#define ATTR_SEEN_MAX 30
53
54static inline bool is_boot_services(int type)
55{
56 return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA ||
57 type == EFI_BOOT_SERVICES_CODE ||
58 type == EFI_BOOT_SERVICES_DATA;
59}
60
61static int h_cmp_entry(const void *v1, const void *v2)
62{
63 const struct efi_mem_desc *desc1 = v1;
64 const struct efi_mem_desc *desc2 = v2;
65 int64_t diff = desc1->physical_start - desc2->physical_start;
66
67 /*
68 * Manually calculate the difference to avoid sign loss in the 64-bit
69 * to 32-bit conversion
70 */
71 return diff < 0 ? -1 : diff > 0 ? 1 : 0;
72}
73
Heinrich Schuchardt5b94e262020-08-27 12:39:03 +020074/**
75 * efi_build_mem_table() - make a sorted copy of the memory table
76 *
77 * @map: Pointer to EFI memory map table
78 * @size: Size of table in bytes
79 * @skip_bs: True to skip boot-time memory and merge it with conventional
80 * memory. This will significantly reduce the number of table
81 * entries.
82 * Return: pointer to the new table. It should be freed with free() by the
83 * caller.
84 */
85static void *efi_build_mem_table(struct efi_entry_memmap *map, int size,
86 bool skip_bs)
Simon Glassf1a0baf2015-08-04 12:33:59 -060087{
88 struct efi_mem_desc *desc, *end, *base, *dest, *prev;
89 int count;
90 u64 addr;
91
92 base = malloc(size + sizeof(*desc));
93 if (!base) {
94 debug("%s: Cannot allocate %#x bytes\n", __func__, size);
95 return NULL;
96 }
97 end = (struct efi_mem_desc *)((ulong)map + size);
98 count = ((ulong)end - (ulong)map->desc) / map->desc_size;
99 memcpy(base, map->desc, (ulong)end - (ulong)map->desc);
100 qsort(base, count, map->desc_size, h_cmp_entry);
101 prev = NULL;
102 addr = 0;
103 dest = base;
Bin Mengdd099ec2018-06-22 01:38:31 -0700104 end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size);
Simon Glassf1a0baf2015-08-04 12:33:59 -0600105 for (desc = base; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
106 bool merge = true;
Heinrich Schuchardt5b94e262020-08-27 12:39:03 +0200107 u32 type = desc->type;
108
109 if (type >= EFI_MAX_MEMORY_TYPE) {
110 printf("Memory map contains invalid entry type %u\n",
111 type);
112 continue;
113 }
Simon Glassf1a0baf2015-08-04 12:33:59 -0600114
115 if (skip_bs && is_boot_services(desc->type))
116 type = EFI_CONVENTIONAL_MEMORY;
117
118 memcpy(dest, desc, map->desc_size);
119 dest->type = type;
120 if (!skip_bs || !prev)
121 merge = false;
122 else if (desc->physical_start != addr)
123 merge = false;
124 else if (type != EFI_CONVENTIONAL_MEMORY)
125 merge = false;
126 else if (prev->type != EFI_CONVENTIONAL_MEMORY)
127 merge = false;
128
129 if (merge) {
130 prev->num_pages += desc->num_pages;
131 } else {
132 prev = dest;
133 dest = efi_get_next_mem_desc(map, dest);
134 }
135 addr = desc->physical_start + (desc->num_pages <<
136 EFI_PAGE_SHIFT);
137 }
138
139 /* Mark the end */
Heinrich Schuchardt5b94e262020-08-27 12:39:03 +0200140 dest->type = EFI_MAX_MEMORY_TYPE;
Simon Glassf1a0baf2015-08-04 12:33:59 -0600141
142 return base;
143}
144
145static void efi_print_mem_table(struct efi_entry_memmap *map,
146 struct efi_mem_desc *desc, bool skip_bs)
147{
148 u64 attr_seen[ATTR_SEEN_MAX];
149 int attr_seen_count;
150 int upto, i;
151 u64 addr;
152
153 printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical",
154 "Virtual", "Size", "Attributes");
155
156 /* Keep track of all the different attributes we have seen */
157 attr_seen_count = 0;
158 addr = 0;
Heinrich Schuchardt5b94e262020-08-27 12:39:03 +0200159 for (upto = 0; desc->type != EFI_MAX_MEMORY_TYPE;
Simon Glassf1a0baf2015-08-04 12:33:59 -0600160 upto++, desc = efi_get_next_mem_desc(map, desc)) {
161 const char *name;
162 u64 size;
163
164 if (skip_bs && is_boot_services(desc->type))
165 continue;
166 if (desc->physical_start != addr) {
167 printf(" %-14s %010llx %10s %010llx\n", "<gap>",
168 addr, "", desc->physical_start - addr);
169 }
170 size = desc->num_pages << EFI_PAGE_SHIFT;
171
172 name = desc->type < ARRAY_SIZE(type_name) ?
173 type_name[desc->type] : "<invalid>";
174 printf("%2d %x:%-12s %010llx %010llx %010llx ", upto,
175 desc->type, name, desc->physical_start,
176 desc->virtual_start, size);
177 if (desc->attribute & EFI_MEMORY_RUNTIME)
178 putc('r');
179 printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME);
180 putc('\n');
181
182 for (i = 0; i < attr_seen_count; i++) {
183 if (attr_seen[i] == desc->attribute)
184 break;
185 }
186 if (i == attr_seen_count && i < ATTR_SEEN_MAX)
187 attr_seen[attr_seen_count++] = desc->attribute;
188 addr = desc->physical_start + size;
189 }
190
191 printf("\nAttributes key:\n");
192 for (i = 0; i < attr_seen_count; i++) {
193 u64 attr = attr_seen[i];
194 bool first;
195 int j;
196
Eugeniu Roscadbb148b2018-07-14 22:53:32 +0200197 printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ',
Simon Glassf1a0baf2015-08-04 12:33:59 -0600198 attr & ~EFI_MEMORY_RUNTIME);
199 for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200200 if (attr & mem_attr[j].val) {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600201 if (first)
202 first = false;
203 else
204 printf(", ");
205 printf("%s", mem_attr[j].name);
206 }
207 }
208 putc('\n');
209 }
210 if (skip_bs)
211 printf("*Some areas are merged (use 'all' to see)\n");
212}
213
Simon Glass09140112020-05-10 11:40:03 -0600214static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc,
215 char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600216{
217 struct efi_mem_desc *desc;
218 struct efi_entry_memmap *map;
219 int size, ret;
220 bool skip_bs;
221
222 skip_bs = !argc || *argv[0] != 'a';
223 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
224 switch (ret) {
225 case -ENOENT:
226 printf("No EFI table available\n");
227 goto done;
228 case -EPROTONOSUPPORT:
229 printf("Incorrect EFI table version\n");
230 goto done;
231 }
232 printf("EFI table at %lx, memory map %p, size %x, version %x, descr. size %#x\n",
233 gd->arch.table, map, size, map->version, map->desc_size);
234 if (map->version != EFI_MEM_DESC_VERSION) {
235 printf("Incorrect memory map version\n");
236 ret = -EPROTONOSUPPORT;
237 goto done;
238 }
239
240 desc = efi_build_mem_table(map, size, skip_bs);
241 if (!desc) {
242 ret = -ENOMEM;
243 goto done;
244 }
245
246 efi_print_mem_table(map, desc, skip_bs);
247 free(desc);
248done:
249 if (ret)
250 printf("Error: %d\n", ret);
251
252 return ret ? CMD_RET_FAILURE : 0;
253}
254
Simon Glass09140112020-05-10 11:40:03 -0600255static struct cmd_tbl efi_commands[] = {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600256 U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""),
257};
258
Simon Glass09140112020-05-10 11:40:03 -0600259static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600260{
Simon Glass09140112020-05-10 11:40:03 -0600261 struct cmd_tbl *efi_cmd;
Simon Glassf1a0baf2015-08-04 12:33:59 -0600262 int ret;
263
264 if (argc < 2)
265 return CMD_RET_USAGE;
266 efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands));
267 argc -= 2;
268 argv += 2;
269 if (!efi_cmd || argc > efi_cmd->maxargs)
270 return CMD_RET_USAGE;
271
272 ret = efi_cmd->cmd(efi_cmd, flag, argc, argv);
273
274 return cmd_process_error(efi_cmd, ret);
275}
276
277U_BOOT_CMD(
278 efi, 3, 1, do_efi,
279 "EFI access",
280 "mem [all] Dump memory information [include boot services]"
281);