blob: b3a3bf828215e82a9dd0bcb384f4bada6cb3bfd0 [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
74void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs)
75{
76 struct efi_mem_desc *desc, *end, *base, *dest, *prev;
77 int count;
78 u64 addr;
79
80 base = malloc(size + sizeof(*desc));
81 if (!base) {
82 debug("%s: Cannot allocate %#x bytes\n", __func__, size);
83 return NULL;
84 }
85 end = (struct efi_mem_desc *)((ulong)map + size);
86 count = ((ulong)end - (ulong)map->desc) / map->desc_size;
87 memcpy(base, map->desc, (ulong)end - (ulong)map->desc);
88 qsort(base, count, map->desc_size, h_cmp_entry);
89 prev = NULL;
90 addr = 0;
91 dest = base;
Bin Mengdd099ec2018-06-22 01:38:31 -070092 end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size);
Simon Glassf1a0baf2015-08-04 12:33:59 -060093 for (desc = base; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
94 bool merge = true;
95 int type = desc->type;
96
97 if (skip_bs && is_boot_services(desc->type))
98 type = EFI_CONVENTIONAL_MEMORY;
99
100 memcpy(dest, desc, map->desc_size);
101 dest->type = type;
102 if (!skip_bs || !prev)
103 merge = false;
104 else if (desc->physical_start != addr)
105 merge = false;
106 else if (type != EFI_CONVENTIONAL_MEMORY)
107 merge = false;
108 else if (prev->type != EFI_CONVENTIONAL_MEMORY)
109 merge = false;
110
111 if (merge) {
112 prev->num_pages += desc->num_pages;
113 } else {
114 prev = dest;
115 dest = efi_get_next_mem_desc(map, dest);
116 }
117 addr = desc->physical_start + (desc->num_pages <<
118 EFI_PAGE_SHIFT);
119 }
120
121 /* Mark the end */
122 dest->type = EFI_TABLE_END;
123
124 return base;
125}
126
127static void efi_print_mem_table(struct efi_entry_memmap *map,
128 struct efi_mem_desc *desc, bool skip_bs)
129{
130 u64 attr_seen[ATTR_SEEN_MAX];
131 int attr_seen_count;
132 int upto, i;
133 u64 addr;
134
135 printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical",
136 "Virtual", "Size", "Attributes");
137
138 /* Keep track of all the different attributes we have seen */
139 attr_seen_count = 0;
140 addr = 0;
141 for (upto = 0; desc->type != EFI_TABLE_END;
142 upto++, desc = efi_get_next_mem_desc(map, desc)) {
143 const char *name;
144 u64 size;
145
146 if (skip_bs && is_boot_services(desc->type))
147 continue;
148 if (desc->physical_start != addr) {
149 printf(" %-14s %010llx %10s %010llx\n", "<gap>",
150 addr, "", desc->physical_start - addr);
151 }
152 size = desc->num_pages << EFI_PAGE_SHIFT;
153
154 name = desc->type < ARRAY_SIZE(type_name) ?
155 type_name[desc->type] : "<invalid>";
156 printf("%2d %x:%-12s %010llx %010llx %010llx ", upto,
157 desc->type, name, desc->physical_start,
158 desc->virtual_start, size);
159 if (desc->attribute & EFI_MEMORY_RUNTIME)
160 putc('r');
161 printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME);
162 putc('\n');
163
164 for (i = 0; i < attr_seen_count; i++) {
165 if (attr_seen[i] == desc->attribute)
166 break;
167 }
168 if (i == attr_seen_count && i < ATTR_SEEN_MAX)
169 attr_seen[attr_seen_count++] = desc->attribute;
170 addr = desc->physical_start + size;
171 }
172
173 printf("\nAttributes key:\n");
174 for (i = 0; i < attr_seen_count; i++) {
175 u64 attr = attr_seen[i];
176 bool first;
177 int j;
178
Eugeniu Roscadbb148b2018-07-14 22:53:32 +0200179 printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ',
Simon Glassf1a0baf2015-08-04 12:33:59 -0600180 attr & ~EFI_MEMORY_RUNTIME);
181 for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200182 if (attr & mem_attr[j].val) {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600183 if (first)
184 first = false;
185 else
186 printf(", ");
187 printf("%s", mem_attr[j].name);
188 }
189 }
190 putc('\n');
191 }
192 if (skip_bs)
193 printf("*Some areas are merged (use 'all' to see)\n");
194}
195
Simon Glass09140112020-05-10 11:40:03 -0600196static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc,
197 char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600198{
199 struct efi_mem_desc *desc;
200 struct efi_entry_memmap *map;
201 int size, ret;
202 bool skip_bs;
203
204 skip_bs = !argc || *argv[0] != 'a';
205 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
206 switch (ret) {
207 case -ENOENT:
208 printf("No EFI table available\n");
209 goto done;
210 case -EPROTONOSUPPORT:
211 printf("Incorrect EFI table version\n");
212 goto done;
213 }
214 printf("EFI table at %lx, memory map %p, size %x, version %x, descr. size %#x\n",
215 gd->arch.table, map, size, map->version, map->desc_size);
216 if (map->version != EFI_MEM_DESC_VERSION) {
217 printf("Incorrect memory map version\n");
218 ret = -EPROTONOSUPPORT;
219 goto done;
220 }
221
222 desc = efi_build_mem_table(map, size, skip_bs);
223 if (!desc) {
224 ret = -ENOMEM;
225 goto done;
226 }
227
228 efi_print_mem_table(map, desc, skip_bs);
229 free(desc);
230done:
231 if (ret)
232 printf("Error: %d\n", ret);
233
234 return ret ? CMD_RET_FAILURE : 0;
235}
236
Simon Glass09140112020-05-10 11:40:03 -0600237static struct cmd_tbl efi_commands[] = {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600238 U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""),
239};
240
Simon Glass09140112020-05-10 11:40:03 -0600241static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600242{
Simon Glass09140112020-05-10 11:40:03 -0600243 struct cmd_tbl *efi_cmd;
Simon Glassf1a0baf2015-08-04 12:33:59 -0600244 int ret;
245
246 if (argc < 2)
247 return CMD_RET_USAGE;
248 efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands));
249 argc -= 2;
250 argv += 2;
251 if (!efi_cmd || argc > efi_cmd->maxargs)
252 return CMD_RET_USAGE;
253
254 ret = efi_cmd->cmd(efi_cmd, flag, argc, argv);
255
256 return cmd_process_error(efi_cmd, ret);
257}
258
259U_BOOT_CMD(
260 efi, 3, 1, do_efi,
261 "EFI access",
262 "mem [all] Dump memory information [include boot services]"
263);