1 /*
2 * EFI application loader
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9 #include <common.h>
10 #include <command.h>
11 #include <dm.h>
12 #include <efi_loader.h>
13 #include <errno.h>
14 #include <linux/libfdt.h>
15 #include <linux/libfdt_env.h>
16 #include <memalign.h>
17 #include <asm/global_data.h>
18 #include <asm-generic/sections.h>
19 #include <linux/linkage.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 static uint8_t efi_obj_list_initalized;
24
25 static struct efi_device_path *bootefi_image_path;
26 static struct efi_device_path *bootefi_device_path;
27
28 /* Initialize and populate EFI object list */
efi_init_obj_list(void)29 static void efi_init_obj_list(void)
30 {
31 efi_obj_list_initalized = 1;
32
33 efi_console_register();
34 #ifdef CONFIG_PARTITIONS
35 efi_disk_register();
36 #endif
37 #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
38 efi_gop_register();
39 #endif
40 #ifdef CONFIG_NET
41 efi_net_register();
42 #endif
43 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
44 efi_smbios_register();
45 #endif
46
47 /* Initialize EFI runtime services */
48 efi_reset_system_init();
49 efi_get_time_init();
50 }
51
copy_fdt(void * fdt)52 static void *copy_fdt(void *fdt)
53 {
54 u64 fdt_size = fdt_totalsize(fdt);
55 unsigned long fdt_ram_start = -1L, fdt_pages;
56 u64 new_fdt_addr;
57 void *new_fdt;
58 int i;
59
60 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
61 u64 ram_start = gd->bd->bi_dram[i].start;
62 u64 ram_size = gd->bd->bi_dram[i].size;
63
64 if (!ram_size)
65 continue;
66
67 if (ram_start < fdt_ram_start)
68 fdt_ram_start = ram_start;
69 }
70
71 /* Give us at least 4kb breathing room */
72 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
73 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
74
75 /* Safe fdt location is at 128MB */
76 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
77 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
78 &new_fdt_addr) != EFI_SUCCESS) {
79 /* If we can't put it there, put it somewhere */
80 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
81 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
82 &new_fdt_addr) != EFI_SUCCESS) {
83 printf("ERROR: Failed to reserve space for FDT\n");
84 return NULL;
85 }
86 }
87
88 new_fdt = (void*)(ulong)new_fdt_addr;
89 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
90 fdt_set_totalsize(new_fdt, fdt_size);
91
92 return new_fdt;
93 }
94
efi_do_enter(void * image_handle,struct efi_system_table * st,asmlinkage ulong (* entry)(void * image_handle,struct efi_system_table * st))95 static ulong efi_do_enter(void *image_handle,
96 struct efi_system_table *st,
97 asmlinkage ulong (*entry)(void *image_handle,
98 struct efi_system_table *st))
99 {
100 efi_status_t ret = EFI_LOAD_ERROR;
101
102 if (entry)
103 ret = entry(image_handle, st);
104 st->boottime->exit(image_handle, ret, 0, NULL);
105 return ret;
106 }
107
108 #ifdef CONFIG_ARM64
efi_run_in_el2(asmlinkage ulong (* entry)(void * image_handle,struct efi_system_table * st),void * image_handle,struct efi_system_table * st)109 static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)(
110 void *image_handle, struct efi_system_table *st),
111 void *image_handle, struct efi_system_table *st)
112 {
113 /* Enable caches again */
114 dcache_enable();
115
116 return efi_do_enter(image_handle, st, entry);
117 }
118 #endif
119
120 /*
121 * Load an EFI payload into a newly allocated piece of memory, register all
122 * EFI objects it would want to access and jump to it.
123 */
do_bootefi_exec(void * efi,void * fdt,struct efi_device_path * device_path,struct efi_device_path * image_path)124 static unsigned long do_bootefi_exec(void *efi, void *fdt,
125 struct efi_device_path *device_path,
126 struct efi_device_path *image_path)
127 {
128 struct efi_loaded_image loaded_image_info = {};
129 struct efi_object loaded_image_info_obj = {};
130 ulong ret;
131
132 ulong (*entry)(void *image_handle, struct efi_system_table *st)
133 asmlinkage;
134 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
135 const efi_guid_t fdt_guid = EFI_FDT_GUID;
136 bootm_headers_t img = { 0 };
137
138 /* Initialize and populate EFI object list */
139 if (!efi_obj_list_initalized)
140 efi_init_obj_list();
141
142 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
143 device_path, image_path);
144
145 /*
146 * gd lives in a fixed register which may get clobbered while we execute
147 * the payload. So save it here and restore it on every callback entry
148 */
149 efi_save_gd();
150
151 if (fdt && !fdt_check_header(fdt)) {
152 /* Prepare fdt for payload */
153 fdt = copy_fdt(fdt);
154
155 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
156 printf("ERROR: Failed to process device tree\n");
157 return -EINVAL;
158 }
159
160 /* Link to it in the efi tables */
161 efi_install_configuration_table(&fdt_guid, fdt);
162
163 /* And reserve the space in the memory map */
164 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
165 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
166 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
167 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
168 /* Give a bootloader the chance to modify the device tree */
169 fdt_pages += 2;
170 efi_add_memory_map(fdt_start, fdt_pages,
171 EFI_BOOT_SERVICES_DATA, true);
172 } else {
173 printf("WARNING: Invalid device tree, expect boot to fail\n");
174 efi_install_configuration_table(&fdt_guid, NULL);
175 }
176
177 /* Load the EFI payload */
178 entry = efi_load_pe(efi, &loaded_image_info);
179 if (!entry) {
180 ret = -ENOENT;
181 goto exit;
182 }
183
184 /* Call our payload! */
185 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
186
187 if (setjmp(&loaded_image_info.exit_jmp)) {
188 ret = loaded_image_info.exit_status;
189 EFI_EXIT(ret);
190 goto exit;
191 }
192
193 #ifdef CONFIG_ARM64
194 /* On AArch64 we need to make sure we call our payload in < EL3 */
195 if (current_el() == 3) {
196 smp_kick_all_cpus();
197 dcache_disable(); /* flush cache before switch to EL2 */
198
199 /* Move into EL2 and keep running there */
200 armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
201 (ulong)&systab, 0, (ulong)efi_run_in_el2,
202 ES_TO_AARCH64);
203
204 /* Should never reach here, efi exits with longjmp */
205 while (1) { }
206 }
207 #endif
208
209 ret = efi_do_enter(&loaded_image_info, &systab, entry);
210
211 exit:
212 /* image has returned, loaded-image obj goes *poof*: */
213 list_del(&loaded_image_info_obj.link);
214
215 return ret;
216 }
217
218
219 /* Interpreter command to boot an arbitrary EFI image from memory */
do_bootefi(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])220 static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
221 {
222 char *saddr, *sfdt;
223 unsigned long addr, fdt_addr = 0;
224 unsigned long r;
225
226 if (argc < 2)
227 return CMD_RET_USAGE;
228 #ifdef CONFIG_CMD_BOOTEFI_HELLO
229 if (!strcmp(argv[1], "hello")) {
230 ulong size = __efi_hello_world_end - __efi_hello_world_begin;
231
232 saddr = env_get("loadaddr");
233 if (saddr)
234 addr = simple_strtoul(saddr, NULL, 16);
235 else
236 addr = CONFIG_SYS_LOAD_ADDR;
237 memcpy((char *)addr, __efi_hello_world_begin, size);
238 } else
239 #endif
240 {
241 saddr = argv[1];
242
243 addr = simple_strtoul(saddr, NULL, 16);
244
245 if (argc > 2) {
246 sfdt = argv[2];
247 fdt_addr = simple_strtoul(sfdt, NULL, 16);
248 }
249 }
250
251 printf("## Starting EFI application at %08lx ...\n", addr);
252 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
253 bootefi_device_path, bootefi_image_path);
254 printf("## Application terminated, r = %lu\n",
255 r & ~EFI_ERROR_MASK);
256
257 if (r != EFI_SUCCESS)
258 return 1;
259 else
260 return 0;
261 }
262
263 #ifdef CONFIG_SYS_LONGHELP
264 static char bootefi_help_text[] =
265 "<image address> [fdt address]\n"
266 " - boot EFI payload stored at address <image address>.\n"
267 " If specified, the device tree located at <fdt address> gets\n"
268 " exposed as EFI configuration table.\n"
269 #ifdef CONFIG_CMD_BOOTEFI_HELLO
270 "hello\n"
271 " - boot a sample Hello World application stored within U-Boot"
272 #endif
273 ;
274 #endif
275
276 U_BOOT_CMD(
277 bootefi, 3, 0, do_bootefi,
278 "Boots an EFI payload from memory",
279 bootefi_help_text
280 );
281
parse_partnum(const char * devnr)282 static int parse_partnum(const char *devnr)
283 {
284 const char *str = strchr(devnr, ':');
285 if (str) {
286 str++;
287 return simple_strtoul(str, NULL, 16);
288 }
289 return 0;
290 }
291
efi_set_bootdev(const char * dev,const char * devnr,const char * path)292 void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
293 {
294 char filename[32] = { 0 }; /* dp->str is u16[32] long */
295 char *s;
296
297 if (strcmp(dev, "Net")) {
298 struct blk_desc *desc;
299 int part;
300
301 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
302 part = parse_partnum(devnr);
303
304 bootefi_device_path = efi_dp_from_part(desc, part);
305 } else {
306 #ifdef CONFIG_NET
307 bootefi_device_path = efi_dp_from_eth();
308 #endif
309 }
310
311 if (strcmp(dev, "Net")) {
312 /* Add leading / to fs paths, because they're absolute */
313 snprintf(filename, sizeof(filename), "/%s", path);
314 } else {
315 snprintf(filename, sizeof(filename), "%s", path);
316 }
317 /* DOS style file path: */
318 s = filename;
319 while ((s = strchr(s, '/')))
320 *s++ = '\\';
321 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
322 }
323