]>
Commit | Line | Data |
---|---|---|
b9939336 AG |
1 | /* |
2 | * EFI application loader | |
3 | * | |
4 | * Copyright (c) 2016 Alexander Graf | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0+ | |
7 | */ | |
8 | ||
d78e40d6 | 9 | #include <charset.h> |
b9939336 AG |
10 | #include <common.h> |
11 | #include <command.h> | |
9d922450 | 12 | #include <dm.h> |
b9939336 | 13 | #include <efi_loader.h> |
d78e40d6 | 14 | #include <efi_selftest.h> |
b9939336 AG |
15 | #include <errno.h> |
16 | #include <libfdt.h> | |
17 | #include <libfdt_env.h> | |
ad0c1a3d | 18 | #include <memalign.h> |
0d9d501f | 19 | #include <asm/global_data.h> |
e275458c SG |
20 | #include <asm-generic/sections.h> |
21 | #include <linux/linkage.h> | |
0d9d501f AG |
22 | |
23 | DECLARE_GLOBAL_DATA_PTR; | |
b9939336 | 24 | |
7cbc1241 HS |
25 | static uint8_t efi_obj_list_initalized; |
26 | ||
95c5553e RC |
27 | static struct efi_device_path *bootefi_image_path; |
28 | static struct efi_device_path *bootefi_device_path; | |
b9939336 | 29 | |
7cbc1241 HS |
30 | /* Initialize and populate EFI object list */ |
31 | static void efi_init_obj_list(void) | |
32 | { | |
33 | efi_obj_list_initalized = 1; | |
34 | ||
05ef48a2 HS |
35 | /* Initialize EFI driver uclass */ |
36 | efi_driver_init(); | |
37 | ||
7cbc1241 HS |
38 | efi_console_register(); |
39 | #ifdef CONFIG_PARTITIONS | |
40 | efi_disk_register(); | |
41 | #endif | |
42 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) | |
43 | efi_gop_register(); | |
44 | #endif | |
45 | #ifdef CONFIG_NET | |
95c5553e | 46 | efi_net_register(); |
7cbc1241 HS |
47 | #endif |
48 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE | |
49 | efi_smbios_register(); | |
50 | #endif | |
b3d60900 | 51 | efi_watchdog_register(); |
7cbc1241 HS |
52 | |
53 | /* Initialize EFI runtime services */ | |
54 | efi_reset_system_init(); | |
55 | efi_get_time_init(); | |
56 | } | |
57 | ||
d78e40d6 HS |
58 | /* |
59 | * Set the load options of an image from an environment variable. | |
60 | * | |
61 | * @loaded_image_info: the image | |
62 | * @env_var: name of the environment variable | |
63 | */ | |
64 | static void set_load_options(struct efi_loaded_image *loaded_image_info, | |
65 | const char *env_var) | |
66 | { | |
67 | size_t size; | |
68 | const char *env = env_get(env_var); | |
69 | ||
70 | loaded_image_info->load_options = NULL; | |
71 | loaded_image_info->load_options_size = 0; | |
72 | if (!env) | |
73 | return; | |
74 | size = strlen(env) + 1; | |
75 | loaded_image_info->load_options = calloc(size, sizeof(u16)); | |
76 | if (!loaded_image_info->load_options) { | |
77 | printf("ERROR: Out of memory\n"); | |
78 | return; | |
79 | } | |
80 | utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size); | |
81 | loaded_image_info->load_options_size = size * 2; | |
82 | } | |
83 | ||
0d9d501f AG |
84 | static void *copy_fdt(void *fdt) |
85 | { | |
86 | u64 fdt_size = fdt_totalsize(fdt); | |
ad0c1a3d AG |
87 | unsigned long fdt_ram_start = -1L, fdt_pages; |
88 | u64 new_fdt_addr; | |
0d9d501f | 89 | void *new_fdt; |
ad0c1a3d | 90 | int i; |
0d9d501f | 91 | |
ad0c1a3d AG |
92 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
93 | u64 ram_start = gd->bd->bi_dram[i].start; | |
94 | u64 ram_size = gd->bd->bi_dram[i].size; | |
0d9d501f | 95 | |
ad0c1a3d AG |
96 | if (!ram_size) |
97 | continue; | |
98 | ||
99 | if (ram_start < fdt_ram_start) | |
100 | fdt_ram_start = ram_start; | |
101 | } | |
102 | ||
103 | /* Give us at least 4kb breathing room */ | |
a44bffcc | 104 | fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE); |
ad0c1a3d AG |
105 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
106 | ||
107 | /* Safe fdt location is at 128MB */ | |
108 | new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size; | |
17ff6f02 | 109 | if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
ad0c1a3d AG |
110 | &new_fdt_addr) != EFI_SUCCESS) { |
111 | /* If we can't put it there, put it somewhere */ | |
a44bffcc | 112 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
17ff6f02 | 113 | if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
85a6e9b3 AG |
114 | &new_fdt_addr) != EFI_SUCCESS) { |
115 | printf("ERROR: Failed to reserve space for FDT\n"); | |
116 | return NULL; | |
117 | } | |
ad0c1a3d | 118 | } |
85a6e9b3 | 119 | |
ad0c1a3d | 120 | new_fdt = (void*)(ulong)new_fdt_addr; |
0d9d501f AG |
121 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
122 | fdt_set_totalsize(new_fdt, fdt_size); | |
123 | ||
124 | return new_fdt; | |
125 | } | |
126 | ||
3eb0841b | 127 | static efi_status_t efi_do_enter( |
2074f700 HS |
128 | efi_handle_t image_handle, struct efi_system_table *st, |
129 | asmlinkage ulong (*entry)(efi_handle_t image_handle, | |
3eb0841b | 130 | struct efi_system_table *st)) |
b06d8ac3 HS |
131 | { |
132 | efi_status_t ret = EFI_LOAD_ERROR; | |
133 | ||
134 | if (entry) | |
135 | ret = entry(image_handle, st); | |
136 | st->boottime->exit(image_handle, ret, 0, NULL); | |
137 | return ret; | |
138 | } | |
139 | ||
ec6617c3 | 140 | #ifdef CONFIG_ARM64 |
3eb0841b | 141 | static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)( |
2074f700 HS |
142 | efi_handle_t image_handle, struct efi_system_table *st), |
143 | efi_handle_t image_handle, struct efi_system_table *st) | |
ec6617c3 AW |
144 | { |
145 | /* Enable caches again */ | |
146 | dcache_enable(); | |
147 | ||
b06d8ac3 | 148 | return efi_do_enter(image_handle, st, entry); |
ec6617c3 AW |
149 | } |
150 | #endif | |
151 | ||
b9939336 AG |
152 | /* |
153 | * Load an EFI payload into a newly allocated piece of memory, register all | |
154 | * EFI objects it would want to access and jump to it. | |
155 | */ | |
3eb0841b HS |
156 | static efi_status_t do_bootefi_exec(void *efi, void *fdt, |
157 | struct efi_device_path *device_path, | |
158 | struct efi_device_path *image_path) | |
b9939336 | 159 | { |
95c5553e RC |
160 | struct efi_loaded_image loaded_image_info = {}; |
161 | struct efi_object loaded_image_info_obj = {}; | |
bf19273e | 162 | struct efi_device_path *memdp = NULL; |
95c5553e RC |
163 | ulong ret; |
164 | ||
2074f700 | 165 | ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st) |
e275458c | 166 | asmlinkage; |
b9939336 | 167 | ulong fdt_pages, fdt_size, fdt_start, fdt_end; |
f4f9993f | 168 | const efi_guid_t fdt_guid = EFI_FDT_GUID; |
dea2174d | 169 | bootm_headers_t img = { 0 }; |
b9939336 | 170 | |
bf19273e RC |
171 | /* |
172 | * Special case for efi payload not loaded from disk, such as | |
173 | * 'bootefi hello' or for example payload loaded directly into | |
174 | * memory via jtag/etc: | |
175 | */ | |
176 | if (!device_path && !image_path) { | |
177 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); | |
178 | /* actual addresses filled in after efi_load_pe() */ | |
179 | memdp = efi_dp_from_mem(0, 0, 0); | |
180 | device_path = image_path = memdp; | |
181 | } else { | |
182 | assert(device_path && image_path); | |
183 | } | |
184 | ||
95c5553e RC |
185 | /* Initialize and populate EFI object list */ |
186 | if (!efi_obj_list_initalized) | |
187 | efi_init_obj_list(); | |
188 | ||
189 | efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj, | |
190 | device_path, image_path); | |
191 | ||
b9939336 AG |
192 | /* |
193 | * gd lives in a fixed register which may get clobbered while we execute | |
194 | * the payload. So save it here and restore it on every callback entry | |
195 | */ | |
196 | efi_save_gd(); | |
197 | ||
1c39809b | 198 | if (fdt && !fdt_check_header(fdt)) { |
dea2174d | 199 | /* Prepare fdt for payload */ |
0d9d501f AG |
200 | fdt = copy_fdt(fdt); |
201 | ||
202 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { | |
dea2174d AG |
203 | printf("ERROR: Failed to process device tree\n"); |
204 | return -EINVAL; | |
205 | } | |
206 | ||
207 | /* Link to it in the efi tables */ | |
f4f9993f | 208 | efi_install_configuration_table(&fdt_guid, fdt); |
b9939336 AG |
209 | |
210 | /* And reserve the space in the memory map */ | |
0d9d501f AG |
211 | fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK; |
212 | fdt_end = ((ulong)fdt) + fdt_totalsize(fdt); | |
b9939336 AG |
213 | fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK; |
214 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; | |
215 | /* Give a bootloader the chance to modify the device tree */ | |
216 | fdt_pages += 2; | |
217 | efi_add_memory_map(fdt_start, fdt_pages, | |
218 | EFI_BOOT_SERVICES_DATA, true); | |
b9939336 | 219 | } else { |
1c39809b | 220 | printf("WARNING: Invalid device tree, expect boot to fail\n"); |
f4f9993f | 221 | efi_install_configuration_table(&fdt_guid, NULL); |
b9939336 AG |
222 | } |
223 | ||
b57f48a8 HS |
224 | /* Transfer environment variable bootargs as load options */ |
225 | set_load_options(&loaded_image_info, "bootargs"); | |
b9939336 AG |
226 | /* Load the EFI payload */ |
227 | entry = efi_load_pe(efi, &loaded_image_info); | |
95c5553e RC |
228 | if (!entry) { |
229 | ret = -ENOENT; | |
230 | goto exit; | |
231 | } | |
80a4800e | 232 | |
bf19273e RC |
233 | if (memdp) { |
234 | struct efi_device_path_memory *mdp = (void *)memdp; | |
235 | mdp->memory_type = loaded_image_info.image_code_type; | |
236 | mdp->start_address = (uintptr_t)loaded_image_info.image_base; | |
237 | mdp->end_address = mdp->start_address + | |
238 | loaded_image_info.image_size; | |
239 | } | |
240 | ||
ad644e7c RC |
241 | /* we don't support much: */ |
242 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", | |
243 | "{ro,boot}(blob)0000000000000000"); | |
244 | ||
b9939336 | 245 | /* Call our payload! */ |
edcef3ba | 246 | debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); |
a86aeaf2 AG |
247 | |
248 | if (setjmp(&loaded_image_info.exit_jmp)) { | |
95c5553e | 249 | ret = loaded_image_info.exit_status; |
95c5553e | 250 | goto exit; |
a86aeaf2 AG |
251 | } |
252 | ||
69bd459d AG |
253 | #ifdef CONFIG_ARM64 |
254 | /* On AArch64 we need to make sure we call our payload in < EL3 */ | |
255 | if (current_el() == 3) { | |
256 | smp_kick_all_cpus(); | |
257 | dcache_disable(); /* flush cache before switch to EL2 */ | |
ec6617c3 AW |
258 | |
259 | /* Move into EL2 and keep running there */ | |
ea54ad59 HS |
260 | armv8_switch_to_el2((ulong)entry, |
261 | (ulong)&loaded_image_info_obj.handle, | |
7c5e1feb | 262 | (ulong)&systab, 0, (ulong)efi_run_in_el2, |
ec6617c3 AW |
263 | ES_TO_AARCH64); |
264 | ||
265 | /* Should never reach here, efi exits with longjmp */ | |
266 | while (1) { } | |
69bd459d AG |
267 | } |
268 | #endif | |
269 | ||
ea54ad59 | 270 | ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry); |
95c5553e RC |
271 | |
272 | exit: | |
273 | /* image has returned, loaded-image obj goes *poof*: */ | |
274 | list_del(&loaded_image_info_obj.link); | |
275 | ||
276 | return ret; | |
b9939336 AG |
277 | } |
278 | ||
9975fe96 RC |
279 | static int do_bootefi_bootmgr_exec(unsigned long fdt_addr) |
280 | { | |
281 | struct efi_device_path *device_path, *file_path; | |
282 | void *addr; | |
283 | efi_status_t r; | |
284 | ||
285 | /* Initialize and populate EFI object list */ | |
286 | if (!efi_obj_list_initalized) | |
287 | efi_init_obj_list(); | |
288 | ||
289 | /* | |
290 | * gd lives in a fixed register which may get clobbered while we execute | |
291 | * the payload. So save it here and restore it on every callback entry | |
292 | */ | |
293 | efi_save_gd(); | |
294 | ||
295 | addr = efi_bootmgr_load(&device_path, &file_path); | |
296 | if (!addr) | |
297 | return 1; | |
298 | ||
299 | printf("## Starting EFI application at %p ...\n", addr); | |
300 | r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path); | |
301 | printf("## Application terminated, r = %lu\n", | |
302 | r & ~EFI_ERROR_MASK); | |
303 | ||
304 | if (r != EFI_SUCCESS) | |
305 | return 1; | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
b9939336 AG |
310 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
311 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
312 | { | |
1c39809b AG |
313 | char *saddr, *sfdt; |
314 | unsigned long addr, fdt_addr = 0; | |
3eb0841b | 315 | efi_status_t r; |
b9939336 AG |
316 | |
317 | if (argc < 2) | |
3c1dcef6 | 318 | return CMD_RET_USAGE; |
c7ae3dfd SG |
319 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
320 | if (!strcmp(argv[1], "hello")) { | |
5e44489b | 321 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
b9939336 | 322 | |
51c533fd HS |
323 | saddr = env_get("loadaddr"); |
324 | if (saddr) | |
325 | addr = simple_strtoul(saddr, NULL, 16); | |
326 | else | |
327 | addr = CONFIG_SYS_LOAD_ADDR; | |
5e44489b | 328 | memcpy((char *)addr, __efi_helloworld_begin, size); |
c7ae3dfd | 329 | } else |
623b3a57 HS |
330 | #endif |
331 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST | |
332 | if (!strcmp(argv[1], "selftest")) { | |
7aca68ca HS |
333 | struct efi_loaded_image loaded_image_info = {}; |
334 | struct efi_object loaded_image_info_obj = {}; | |
335 | ||
f972dc14 HS |
336 | /* Construct a dummy device path. */ |
337 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, | |
338 | (uintptr_t)&efi_selftest, | |
339 | (uintptr_t)&efi_selftest); | |
340 | bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest"); | |
341 | ||
7aca68ca HS |
342 | efi_setup_loaded_image(&loaded_image_info, |
343 | &loaded_image_info_obj, | |
344 | bootefi_device_path, bootefi_image_path); | |
623b3a57 HS |
345 | /* |
346 | * gd lives in a fixed register which may get clobbered while we | |
347 | * execute the payload. So save it here and restore it on every | |
348 | * callback entry | |
349 | */ | |
350 | efi_save_gd(); | |
351 | /* Initialize and populate EFI object list */ | |
352 | if (!efi_obj_list_initalized) | |
353 | efi_init_obj_list(); | |
d78e40d6 HS |
354 | /* Transfer environment variable efi_selftest as load options */ |
355 | set_load_options(&loaded_image_info, "efi_selftest"); | |
356 | /* Execute the test */ | |
ea54ad59 | 357 | r = efi_selftest(loaded_image_info_obj.handle, &systab); |
c2b53902 | 358 | efi_restore_gd(); |
d78e40d6 | 359 | free(loaded_image_info.load_options); |
c2b53902 HS |
360 | list_del(&loaded_image_info_obj.link); |
361 | return r != EFI_SUCCESS; | |
623b3a57 | 362 | } else |
c7ae3dfd | 363 | #endif |
9975fe96 RC |
364 | if (!strcmp(argv[1], "bootmgr")) { |
365 | unsigned long fdt_addr = 0; | |
366 | ||
367 | if (argc > 2) | |
368 | fdt_addr = simple_strtoul(argv[2], NULL, 16); | |
369 | ||
370 | return do_bootefi_bootmgr_exec(fdt_addr); | |
371 | } else { | |
c7ae3dfd | 372 | saddr = argv[1]; |
b9939336 | 373 | |
c7ae3dfd SG |
374 | addr = simple_strtoul(saddr, NULL, 16); |
375 | ||
376 | if (argc > 2) { | |
377 | sfdt = argv[2]; | |
378 | fdt_addr = simple_strtoul(sfdt, NULL, 16); | |
379 | } | |
1c39809b AG |
380 | } |
381 | ||
5ee31baf | 382 | printf("## Starting EFI application at %08lx ...\n", addr); |
95c5553e RC |
383 | r = do_bootefi_exec((void *)addr, (void *)fdt_addr, |
384 | bootefi_device_path, bootefi_image_path); | |
1da1bac4 HS |
385 | printf("## Application terminated, r = %lu\n", |
386 | r & ~EFI_ERROR_MASK); | |
b9939336 | 387 | |
1da1bac4 HS |
388 | if (r != EFI_SUCCESS) |
389 | return 1; | |
390 | else | |
391 | return 0; | |
b9939336 AG |
392 | } |
393 | ||
394 | #ifdef CONFIG_SYS_LONGHELP | |
395 | static char bootefi_help_text[] = | |
1c39809b AG |
396 | "<image address> [fdt address]\n" |
397 | " - boot EFI payload stored at address <image address>.\n" | |
398 | " If specified, the device tree located at <fdt address> gets\n" | |
c7ae3dfd SG |
399 | " exposed as EFI configuration table.\n" |
400 | #ifdef CONFIG_CMD_BOOTEFI_HELLO | |
623b3a57 HS |
401 | "bootefi hello\n" |
402 | " - boot a sample Hello World application stored within U-Boot\n" | |
403 | #endif | |
404 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST | |
405 | "bootefi selftest\n" | |
406 | " - boot an EFI selftest application stored within U-Boot\n" | |
d78e40d6 HS |
407 | " Use environment variable efi_selftest to select a single test.\n" |
408 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" | |
c7ae3dfd | 409 | #endif |
9975fe96 RC |
410 | "bootmgr [fdt addr]\n" |
411 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" | |
412 | "\n" | |
413 | " If specified, the device tree located at <fdt address> gets\n" | |
414 | " exposed as EFI configuration table.\n"; | |
b9939336 AG |
415 | #endif |
416 | ||
417 | U_BOOT_CMD( | |
1c39809b | 418 | bootefi, 3, 0, do_bootefi, |
92dfd922 | 419 | "Boots an EFI payload from memory", |
b9939336 AG |
420 | bootefi_help_text |
421 | ); | |
0f4060eb | 422 | |
95c5553e | 423 | static int parse_partnum(const char *devnr) |
0f4060eb | 424 | { |
95c5553e RC |
425 | const char *str = strchr(devnr, ':'); |
426 | if (str) { | |
427 | str++; | |
428 | return simple_strtoul(str, NULL, 16); | |
f9d334bd | 429 | } |
95c5553e RC |
430 | return 0; |
431 | } | |
f9d334bd | 432 | |
95c5553e RC |
433 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
434 | { | |
435 | char filename[32] = { 0 }; /* dp->str is u16[32] long */ | |
436 | char *s; | |
f9d334bd | 437 | |
95c5553e RC |
438 | if (strcmp(dev, "Net")) { |
439 | struct blk_desc *desc; | |
440 | int part; | |
8c3df0bf | 441 | |
95c5553e | 442 | desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10)); |
8300be61 SR |
443 | if (!desc) |
444 | return; | |
95c5553e | 445 | part = parse_partnum(devnr); |
0f4060eb | 446 | |
95c5553e RC |
447 | bootefi_device_path = efi_dp_from_part(desc, part); |
448 | } else { | |
449 | #ifdef CONFIG_NET | |
450 | bootefi_device_path = efi_dp_from_eth(); | |
451 | #endif | |
452 | } | |
c07ad7c0 | 453 | |
9975fe96 RC |
454 | if (!path) |
455 | return; | |
456 | ||
49271666 AG |
457 | if (strcmp(dev, "Net")) { |
458 | /* Add leading / to fs paths, because they're absolute */ | |
95c5553e | 459 | snprintf(filename, sizeof(filename), "/%s", path); |
49271666 | 460 | } else { |
95c5553e | 461 | snprintf(filename, sizeof(filename), "%s", path); |
49271666 | 462 | } |
3e433e96 | 463 | /* DOS style file path: */ |
95c5553e | 464 | s = filename; |
3e433e96 RC |
465 | while ((s = strchr(s, '/'))) |
466 | *s++ = '\\'; | |
95c5553e | 467 | bootefi_image_path = efi_dp_from_file(NULL, 0, filename); |
0f4060eb | 468 | } |