blob: 9aeb913ff37e8447c778d216351fa137be0becb6 [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" },
Eugeniu Rosca9b891832018-07-14 22:53:30 +020047 { EFI_MEMORY_RUNTIME, "needs runtime mapping" }
Simon Glassf1a0baf2015-08-04 12:33:59 -060048};
49
50/* Maximum different attribute values we can track */
51#define ATTR_SEEN_MAX 30
52
53static 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
60static 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
73void *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 Mengdd099ec2018-06-22 01:38:31 -070091 end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size);
Simon Glassf1a0baf2015-08-04 12:33:59 -060092 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
126static 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 Roscadbb148b2018-07-14 22:53:32 +0200178 printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ',
Simon Glassf1a0baf2015-08-04 12:33:59 -0600179 attr & ~EFI_MEMORY_RUNTIME);
180 for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
Eugeniu Rosca9b891832018-07-14 22:53:30 +0200181 if (attr & mem_attr[j].val) {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600182 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 Glass09140112020-05-10 11:40:03 -0600195static int do_efi_mem(struct cmd_tbl *cmdtp, int flag, int argc,
196 char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600197{
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);
229done:
230 if (ret)
231 printf("Error: %d\n", ret);
232
233 return ret ? CMD_RET_FAILURE : 0;
234}
235
Simon Glass09140112020-05-10 11:40:03 -0600236static struct cmd_tbl efi_commands[] = {
Simon Glassf1a0baf2015-08-04 12:33:59 -0600237 U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""),
238};
239
Simon Glass09140112020-05-10 11:40:03 -0600240static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassf1a0baf2015-08-04 12:33:59 -0600241{
Simon Glass09140112020-05-10 11:40:03 -0600242 struct cmd_tbl *efi_cmd;
Simon Glassf1a0baf2015-08-04 12:33:59 -0600243 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
258U_BOOT_CMD(
259 efi, 3, 1, do_efi,
260 "EFI access",
261 "mem [all] Dump memory information [include boot services]"
262);