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