]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b9939336 AG |
2 | /* |
3 | * EFI application loader | |
4 | * | |
5 | * Copyright (c) 2016 Alexander Graf | |
b9939336 AG |
6 | */ |
7 | ||
d78e40d6 | 8 | #include <charset.h> |
b9939336 AG |
9 | #include <common.h> |
10 | #include <command.h> | |
9d922450 | 11 | #include <dm.h> |
b9939336 | 12 | #include <efi_loader.h> |
d78e40d6 | 13 | #include <efi_selftest.h> |
b9939336 | 14 | #include <errno.h> |
b08c8c48 MY |
15 | #include <linux/libfdt.h> |
16 | #include <linux/libfdt_env.h> | |
354264b3 | 17 | #include <mapmem.h> |
ad0c1a3d | 18 | #include <memalign.h> |
0d9d501f | 19 | #include <asm/global_data.h> |
e275458c | 20 | #include <asm-generic/sections.h> |
c3b11dea | 21 | #include <asm-generic/unaligned.h> |
e275458c | 22 | #include <linux/linkage.h> |
0d9d501f | 23 | |
dc500c36 MK |
24 | #ifdef CONFIG_ARMV7_NONSEC |
25 | #include <asm/armv7.h> | |
26 | #include <asm/secure.h> | |
27 | #endif | |
28 | ||
0d9d501f | 29 | DECLARE_GLOBAL_DATA_PTR; |
b9939336 | 30 | |
fc225e60 HS |
31 | #define OBJ_LIST_NOT_INITIALIZED 1 |
32 | ||
33 | static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED; | |
7cbc1241 | 34 | |
95c5553e RC |
35 | static struct efi_device_path *bootefi_image_path; |
36 | static struct efi_device_path *bootefi_device_path; | |
b9939336 | 37 | |
7cbc1241 | 38 | /* Initialize and populate EFI object list */ |
fc225e60 | 39 | efi_status_t efi_init_obj_list(void) |
7cbc1241 | 40 | { |
fc225e60 HS |
41 | efi_status_t ret = EFI_SUCCESS; |
42 | ||
1e1e1c27 HS |
43 | /* |
44 | * On the ARM architecture gd is mapped to a fixed register (r9 or x18). | |
45 | * As this register may be overwritten by an EFI payload we save it here | |
46 | * and restore it on every callback entered. | |
47 | */ | |
48 | efi_save_gd(); | |
49 | ||
098a6cdd | 50 | /* Initialize once only */ |
fc225e60 HS |
51 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
52 | return efi_obj_list_initialized; | |
7cbc1241 | 53 | |
640adadf HS |
54 | /* Initialize system table */ |
55 | ret = efi_initialize_system_table(); | |
56 | if (ret != EFI_SUCCESS) | |
57 | goto out; | |
58 | ||
4e6b5d65 HS |
59 | /* Initialize root node */ |
60 | ret = efi_root_node_register(); | |
61 | if (ret != EFI_SUCCESS) | |
62 | goto out; | |
63 | ||
05ef48a2 | 64 | /* Initialize EFI driver uclass */ |
fc225e60 HS |
65 | ret = efi_driver_init(); |
66 | if (ret != EFI_SUCCESS) | |
67 | goto out; | |
05ef48a2 | 68 | |
fc225e60 HS |
69 | ret = efi_console_register(); |
70 | if (ret != EFI_SUCCESS) | |
71 | goto out; | |
7cbc1241 | 72 | #ifdef CONFIG_PARTITIONS |
fc225e60 HS |
73 | ret = efi_disk_register(); |
74 | if (ret != EFI_SUCCESS) | |
75 | goto out; | |
7cbc1241 HS |
76 | #endif |
77 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) | |
fc225e60 HS |
78 | ret = efi_gop_register(); |
79 | if (ret != EFI_SUCCESS) | |
80 | goto out; | |
7cbc1241 | 81 | #endif |
092f2f35 | 82 | #ifdef CONFIG_NET |
fc225e60 HS |
83 | ret = efi_net_register(); |
84 | if (ret != EFI_SUCCESS) | |
85 | goto out; | |
7cbc1241 | 86 | #endif |
86df34d4 BM |
87 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
88 | ret = efi_acpi_register(); | |
89 | if (ret != EFI_SUCCESS) | |
90 | goto out; | |
91 | #endif | |
7cbc1241 | 92 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
fc225e60 HS |
93 | ret = efi_smbios_register(); |
94 | if (ret != EFI_SUCCESS) | |
95 | goto out; | |
7cbc1241 | 96 | #endif |
fc225e60 HS |
97 | ret = efi_watchdog_register(); |
98 | if (ret != EFI_SUCCESS) | |
99 | goto out; | |
7cbc1241 HS |
100 | |
101 | /* Initialize EFI runtime services */ | |
fc225e60 HS |
102 | ret = efi_reset_system_init(); |
103 | if (ret != EFI_SUCCESS) | |
104 | goto out; | |
fc225e60 HS |
105 | |
106 | out: | |
107 | efi_obj_list_initialized = ret; | |
108 | return ret; | |
7cbc1241 HS |
109 | } |
110 | ||
c3b11dea HS |
111 | /* |
112 | * Allow unaligned memory access. | |
113 | * | |
114 | * This routine is overridden by architectures providing this feature. | |
115 | */ | |
116 | void __weak allow_unaligned(void) | |
117 | { | |
118 | } | |
119 | ||
d78e40d6 HS |
120 | /* |
121 | * Set the load options of an image from an environment variable. | |
122 | * | |
123 | * @loaded_image_info: the image | |
124 | * @env_var: name of the environment variable | |
125 | */ | |
126 | static void set_load_options(struct efi_loaded_image *loaded_image_info, | |
127 | const char *env_var) | |
128 | { | |
129 | size_t size; | |
130 | const char *env = env_get(env_var); | |
7086a71a | 131 | u16 *pos; |
d78e40d6 HS |
132 | |
133 | loaded_image_info->load_options = NULL; | |
134 | loaded_image_info->load_options_size = 0; | |
135 | if (!env) | |
136 | return; | |
7086a71a | 137 | size = utf8_utf16_strlen(env) + 1; |
d78e40d6 HS |
138 | loaded_image_info->load_options = calloc(size, sizeof(u16)); |
139 | if (!loaded_image_info->load_options) { | |
140 | printf("ERROR: Out of memory\n"); | |
141 | return; | |
142 | } | |
7086a71a HS |
143 | pos = loaded_image_info->load_options; |
144 | utf8_utf16_strcpy(&pos, env); | |
d78e40d6 HS |
145 | loaded_image_info->load_options_size = size * 2; |
146 | } | |
147 | ||
9dff4900 SG |
148 | /** |
149 | * copy_fdt() - Copy the device tree to a new location available to EFI | |
150 | * | |
151 | * The FDT is relocated into a suitable location within the EFI memory map. | |
c3772ca1 | 152 | * Additional 12 KiB are added to the space in case the device tree needs to be |
9dff4900 SG |
153 | * expanded later with fdt_open_into(). |
154 | * | |
4574d1b3 HS |
155 | * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated |
156 | * FDT start | |
157 | * Return: status code | |
9dff4900 | 158 | */ |
4574d1b3 | 159 | static efi_status_t copy_fdt(ulong *fdt_addrp) |
0d9d501f | 160 | { |
ad0c1a3d | 161 | unsigned long fdt_ram_start = -1L, fdt_pages; |
9dff4900 SG |
162 | efi_status_t ret = 0; |
163 | void *fdt, *new_fdt; | |
ad0c1a3d | 164 | u64 new_fdt_addr; |
9dff4900 | 165 | uint fdt_size; |
ad0c1a3d | 166 | int i; |
0d9d501f | 167 | |
9dff4900 SG |
168 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
169 | u64 ram_start = gd->bd->bi_dram[i].start; | |
170 | u64 ram_size = gd->bd->bi_dram[i].size; | |
0d9d501f | 171 | |
ad0c1a3d AG |
172 | if (!ram_size) |
173 | continue; | |
174 | ||
175 | if (ram_start < fdt_ram_start) | |
176 | fdt_ram_start = ram_start; | |
177 | } | |
178 | ||
bc9a638a | 179 | /* |
c3772ca1 HS |
180 | * Give us at least 12 KiB of breathing room in case the device tree |
181 | * needs to be expanded later. | |
bc9a638a | 182 | */ |
9dff4900 | 183 | fdt = map_sysmem(*fdt_addrp, 0); |
c3772ca1 HS |
184 | fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000); |
185 | fdt_size = fdt_pages << EFI_PAGE_SHIFT; | |
ad0c1a3d | 186 | |
baf70c02 SG |
187 | /* Safe fdt location is at 127MB */ |
188 | new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size; | |
9dff4900 SG |
189 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
190 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, | |
191 | &new_fdt_addr); | |
192 | if (ret != EFI_SUCCESS) { | |
ad0c1a3d | 193 | /* If we can't put it there, put it somewhere */ |
a44bffcc | 194 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
9dff4900 SG |
195 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
196 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, | |
197 | &new_fdt_addr); | |
198 | if (ret != EFI_SUCCESS) { | |
85a6e9b3 | 199 | printf("ERROR: Failed to reserve space for FDT\n"); |
9dff4900 | 200 | goto done; |
85a6e9b3 | 201 | } |
ad0c1a3d | 202 | } |
85a6e9b3 | 203 | |
9dff4900 | 204 | new_fdt = map_sysmem(new_fdt_addr, fdt_size); |
0d9d501f AG |
205 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
206 | fdt_set_totalsize(new_fdt, fdt_size); | |
207 | ||
9dff4900 | 208 | *fdt_addrp = new_fdt_addr; |
9dff4900 SG |
209 | done: |
210 | return ret; | |
0d9d501f AG |
211 | } |
212 | ||
3eb0841b | 213 | static efi_status_t efi_do_enter( |
2074f700 | 214 | efi_handle_t image_handle, struct efi_system_table *st, |
c6fa5df6 AG |
215 | EFIAPI efi_status_t (*entry)( |
216 | efi_handle_t image_handle, | |
217 | struct efi_system_table *st)) | |
b06d8ac3 HS |
218 | { |
219 | efi_status_t ret = EFI_LOAD_ERROR; | |
220 | ||
221 | if (entry) | |
222 | ret = entry(image_handle, st); | |
223 | st->boottime->exit(image_handle, ret, 0, NULL); | |
224 | return ret; | |
225 | } | |
226 | ||
ec6617c3 | 227 | #ifdef CONFIG_ARM64 |
c6fa5df6 | 228 | static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)( |
2074f700 HS |
229 | efi_handle_t image_handle, struct efi_system_table *st), |
230 | efi_handle_t image_handle, struct efi_system_table *st) | |
ec6617c3 AW |
231 | { |
232 | /* Enable caches again */ | |
233 | dcache_enable(); | |
234 | ||
b06d8ac3 | 235 | return efi_do_enter(image_handle, st, entry); |
ec6617c3 AW |
236 | } |
237 | #endif | |
238 | ||
dc500c36 | 239 | #ifdef CONFIG_ARMV7_NONSEC |
f17f2001 MK |
240 | static bool is_nonsec; |
241 | ||
dc500c36 MK |
242 | static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)( |
243 | efi_handle_t image_handle, struct efi_system_table *st), | |
244 | efi_handle_t image_handle, struct efi_system_table *st) | |
245 | { | |
246 | /* Enable caches again */ | |
247 | dcache_enable(); | |
248 | ||
f17f2001 MK |
249 | is_nonsec = true; |
250 | ||
dc500c36 MK |
251 | return efi_do_enter(image_handle, st, entry); |
252 | } | |
253 | #endif | |
254 | ||
416e07e2 SG |
255 | /* |
256 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges | |
257 | * | |
258 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are | |
259 | * ignored because this is not critical and we would rather continue to try to | |
260 | * boot. | |
261 | * | |
262 | * @fdt: Pointer to device tree | |
263 | */ | |
264 | static void efi_carve_out_dt_rsv(void *fdt) | |
806d2fa8 AG |
265 | { |
266 | int nr_rsv, i; | |
267 | uint64_t addr, size, pages; | |
268 | ||
269 | nr_rsv = fdt_num_mem_rsv(fdt); | |
270 | ||
271 | /* Look for an existing entry and add it to the efi mem map. */ | |
272 | for (i = 0; i < nr_rsv; i++) { | |
273 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) | |
274 | continue; | |
275 | ||
23fd84b3 HS |
276 | /* |
277 | * Do not carve out the device tree. It is already marked as | |
278 | * EFI_RUNTIME_SERVICES_DATA | |
279 | */ | |
280 | if (addr == (uintptr_t)fdt) | |
281 | continue; | |
282 | ||
c3772ca1 | 283 | pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK)); |
23fd84b3 | 284 | addr &= ~EFI_PAGE_MASK; |
416e07e2 SG |
285 | if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, |
286 | false)) | |
287 | printf("FDT memrsv map %d: Failed to add to map\n", i); | |
806d2fa8 | 288 | } |
806d2fa8 AG |
289 | } |
290 | ||
9dff4900 | 291 | static efi_status_t efi_install_fdt(ulong fdt_addr) |
bc4f9133 HS |
292 | { |
293 | bootm_headers_t img = { 0 }; | |
bc4f9133 | 294 | efi_status_t ret; |
9dff4900 | 295 | void *fdt; |
bc4f9133 | 296 | |
9dff4900 | 297 | fdt = map_sysmem(fdt_addr, 0); |
bc4f9133 HS |
298 | if (fdt_check_header(fdt)) { |
299 | printf("ERROR: invalid device tree\n"); | |
300 | return EFI_INVALID_PARAMETER; | |
301 | } | |
302 | ||
303 | /* Prepare fdt for payload */ | |
4574d1b3 | 304 | ret = copy_fdt(&fdt_addr); |
9dff4900 SG |
305 | if (ret) |
306 | return ret; | |
bc4f9133 | 307 | |
9dff4900 SG |
308 | unmap_sysmem(fdt); |
309 | fdt = map_sysmem(fdt_addr, 0); | |
bc4f9133 HS |
310 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
311 | printf("ERROR: failed to process device tree\n"); | |
312 | return EFI_LOAD_ERROR; | |
313 | } | |
314 | ||
416e07e2 | 315 | efi_carve_out_dt_rsv(fdt); |
806d2fa8 | 316 | |
bc4f9133 HS |
317 | /* Link to it in the efi tables */ |
318 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); | |
319 | if (ret != EFI_SUCCESS) | |
320 | return EFI_OUT_OF_RESOURCES; | |
321 | ||
bc4f9133 HS |
322 | return ret; |
323 | } | |
324 | ||
f4f0f7cb SG |
325 | static efi_status_t bootefi_run_prepare(const char *load_options_path, |
326 | struct efi_device_path *device_path, | |
327 | struct efi_device_path *image_path, | |
328 | struct efi_loaded_image_obj **image_objp, | |
329 | struct efi_loaded_image **loaded_image_infop) | |
330 | { | |
331 | efi_status_t ret; | |
332 | ||
333 | ret = efi_setup_loaded_image(device_path, image_path, image_objp, | |
334 | loaded_image_infop); | |
335 | if (ret != EFI_SUCCESS) | |
336 | return ret; | |
337 | ||
338 | /* Transfer environment variable as load options */ | |
339 | set_load_options(*loaded_image_infop, load_options_path); | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
5e2f0391 SG |
344 | /** |
345 | * bootefi_run_finish() - finish up after running an EFI test | |
346 | * | |
347 | * @loaded_image_info: Pointer to a struct which holds the loaded image info | |
348 | * @image_objj: Pointer to a struct which holds the loaded image object | |
349 | */ | |
350 | static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj, | |
351 | struct efi_loaded_image *loaded_image_info) | |
352 | { | |
353 | efi_restore_gd(); | |
354 | free(loaded_image_info->load_options); | |
355 | efi_delete_handle(&image_obj->header); | |
356 | } | |
357 | ||
c982874e HS |
358 | /** |
359 | * do_bootefi_exec() - execute EFI binary | |
360 | * | |
361 | * @efi: address of the binary | |
362 | * @device_path: path of the device from which the binary was loaded | |
363 | * @image_path: device path of the binary | |
364 | * Return: status code | |
365 | * | |
366 | * Load the EFI binary into a newly assigned memory unwinding the relocation | |
367 | * information, install the loaded image protocol, and call the binary. | |
b9939336 | 368 | */ |
bc4f9133 | 369 | static efi_status_t do_bootefi_exec(void *efi, |
3eb0841b HS |
370 | struct efi_device_path *device_path, |
371 | struct efi_device_path *image_path) | |
b9939336 | 372 | { |
8887acc6 | 373 | efi_handle_t mem_handle = NULL; |
bf19273e | 374 | struct efi_device_path *memdp = NULL; |
45204b10 | 375 | efi_status_t ret; |
faea1041 | 376 | struct efi_loaded_image_obj *image_obj = NULL; |
c982874e | 377 | struct efi_loaded_image *loaded_image_info = NULL; |
95c5553e | 378 | |
c6fa5df6 AG |
379 | EFIAPI efi_status_t (*entry)(efi_handle_t image_handle, |
380 | struct efi_system_table *st); | |
b9939336 | 381 | |
bf19273e RC |
382 | /* |
383 | * Special case for efi payload not loaded from disk, such as | |
384 | * 'bootefi hello' or for example payload loaded directly into | |
e1fec152 | 385 | * memory via JTAG, etc: |
bf19273e RC |
386 | */ |
387 | if (!device_path && !image_path) { | |
388 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); | |
389 | /* actual addresses filled in after efi_load_pe() */ | |
390 | memdp = efi_dp_from_mem(0, 0, 0); | |
391 | device_path = image_path = memdp; | |
8887acc6 HS |
392 | /* |
393 | * Grub expects that the device path of the loaded image is | |
394 | * installed on a handle. | |
395 | */ | |
396 | ret = efi_create_handle(&mem_handle); | |
397 | if (ret != EFI_SUCCESS) | |
5e2f0391 | 398 | return ret; /* TODO: leaks device_path */ |
8887acc6 | 399 | ret = efi_add_protocol(mem_handle, &efi_guid_device_path, |
58bc69d2 AG |
400 | device_path); |
401 | if (ret != EFI_SUCCESS) | |
5e2f0391 | 402 | goto err_add_protocol; |
bf19273e RC |
403 | } else { |
404 | assert(device_path && image_path); | |
405 | } | |
406 | ||
f4f0f7cb SG |
407 | ret = bootefi_run_prepare("bootargs", device_path, image_path, |
408 | &image_obj, &loaded_image_info); | |
409 | if (ret) | |
5e2f0391 | 410 | goto err_prepare; |
95c5553e | 411 | |
b9939336 | 412 | /* Load the EFI payload */ |
faea1041 | 413 | entry = efi_load_pe(image_obj, efi, loaded_image_info); |
95c5553e | 414 | if (!entry) { |
45204b10 | 415 | ret = EFI_LOAD_ERROR; |
5e2f0391 | 416 | goto err_prepare; |
95c5553e | 417 | } |
80a4800e | 418 | |
bf19273e RC |
419 | if (memdp) { |
420 | struct efi_device_path_memory *mdp = (void *)memdp; | |
c982874e HS |
421 | mdp->memory_type = loaded_image_info->image_code_type; |
422 | mdp->start_address = (uintptr_t)loaded_image_info->image_base; | |
bf19273e | 423 | mdp->end_address = mdp->start_address + |
c982874e | 424 | loaded_image_info->image_size; |
bf19273e RC |
425 | } |
426 | ||
ad644e7c RC |
427 | /* we don't support much: */ |
428 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", | |
429 | "{ro,boot}(blob)0000000000000000"); | |
430 | ||
b9939336 | 431 | /* Call our payload! */ |
edcef3ba | 432 | debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); |
a86aeaf2 | 433 | |
faea1041 HS |
434 | if (setjmp(&image_obj->exit_jmp)) { |
435 | ret = image_obj->exit_status; | |
5e2f0391 | 436 | goto err_prepare; |
a86aeaf2 AG |
437 | } |
438 | ||
69bd459d AG |
439 | #ifdef CONFIG_ARM64 |
440 | /* On AArch64 we need to make sure we call our payload in < EL3 */ | |
441 | if (current_el() == 3) { | |
442 | smp_kick_all_cpus(); | |
443 | dcache_disable(); /* flush cache before switch to EL2 */ | |
ec6617c3 AW |
444 | |
445 | /* Move into EL2 and keep running there */ | |
ea54ad59 | 446 | armv8_switch_to_el2((ulong)entry, |
d39646a3 | 447 | (ulong)&image_obj->header, |
7c5e1feb | 448 | (ulong)&systab, 0, (ulong)efi_run_in_el2, |
ec6617c3 AW |
449 | ES_TO_AARCH64); |
450 | ||
451 | /* Should never reach here, efi exits with longjmp */ | |
452 | while (1) { } | |
69bd459d AG |
453 | } |
454 | #endif | |
455 | ||
dc500c36 | 456 | #ifdef CONFIG_ARMV7_NONSEC |
f17f2001 | 457 | if (armv7_boot_nonsec() && !is_nonsec) { |
dc500c36 MK |
458 | dcache_disable(); /* flush cache before switch to HYP */ |
459 | ||
460 | armv7_init_nonsec(); | |
461 | secure_ram_addr(_do_nonsec_entry)( | |
462 | efi_run_in_hyp, | |
463 | (uintptr_t)entry, | |
d39646a3 | 464 | (uintptr_t)&image_obj->header, |
dc500c36 MK |
465 | (uintptr_t)&systab); |
466 | ||
467 | /* Should never reach here, efi exits with longjmp */ | |
468 | while (1) { } | |
469 | } | |
470 | #endif | |
471 | ||
d39646a3 | 472 | ret = efi_do_enter(&image_obj->header, &systab, entry); |
95c5553e | 473 | |
5e2f0391 | 474 | err_prepare: |
95c5553e | 475 | /* image has returned, loaded-image obj goes *poof*: */ |
5e2f0391 SG |
476 | bootefi_run_finish(image_obj, loaded_image_info); |
477 | ||
478 | err_add_protocol: | |
8887acc6 HS |
479 | if (mem_handle) |
480 | efi_delete_handle(mem_handle); | |
95c5553e RC |
481 | |
482 | return ret; | |
b9939336 AG |
483 | } |
484 | ||
d9717eae SG |
485 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
486 | /** | |
487 | * bootefi_test_prepare() - prepare to run an EFI test | |
488 | * | |
489 | * This sets things up so we can call EFI functions. This involves preparing | |
490 | * the 'gd' pointer and setting up the load ed image data structures. | |
491 | * | |
492 | * @image_objp: loaded_image_infop: Pointer to a struct which will hold the | |
493 | * loaded image object. This struct will be inited by this function before | |
494 | * use. | |
495 | * @loaded_image_infop: Pointer to a struct which will hold the loaded image | |
496 | * info. This struct will be inited by this function before use. | |
497 | * @path: File path to the test being run (often just the test name with a | |
498 | * backslash before it | |
499 | * @test_func: Address of the test function that is being run | |
f4f0f7cb | 500 | * @load_options_path: U-Boot environment variable to use as load options |
d9717eae SG |
501 | * @return 0 if OK, -ve on error |
502 | */ | |
503 | static efi_status_t bootefi_test_prepare | |
504 | (struct efi_loaded_image_obj **image_objp, | |
f4f0f7cb SG |
505 | struct efi_loaded_image **loaded_image_infop, const char *path, |
506 | ulong test_func, const char *load_options_path) | |
d9717eae | 507 | { |
d9717eae SG |
508 | /* Construct a dummy device path */ |
509 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, | |
510 | (uintptr_t)test_func, | |
511 | (uintptr_t)test_func); | |
512 | if (!bootefi_device_path) | |
513 | return EFI_OUT_OF_RESOURCES; | |
514 | bootefi_image_path = efi_dp_from_file(NULL, 0, path); | |
515 | if (!bootefi_image_path) | |
516 | return EFI_OUT_OF_RESOURCES; | |
d9717eae | 517 | |
f4f0f7cb SG |
518 | return bootefi_run_prepare(load_options_path, bootefi_device_path, |
519 | bootefi_image_path, image_objp, | |
520 | loaded_image_infop); | |
d9717eae SG |
521 | } |
522 | ||
d9717eae SG |
523 | #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */ |
524 | ||
bc4f9133 | 525 | static int do_bootefi_bootmgr_exec(void) |
9975fe96 RC |
526 | { |
527 | struct efi_device_path *device_path, *file_path; | |
528 | void *addr; | |
529 | efi_status_t r; | |
530 | ||
9975fe96 RC |
531 | addr = efi_bootmgr_load(&device_path, &file_path); |
532 | if (!addr) | |
533 | return 1; | |
534 | ||
535 | printf("## Starting EFI application at %p ...\n", addr); | |
bc4f9133 | 536 | r = do_bootefi_exec(addr, device_path, file_path); |
9975fe96 RC |
537 | printf("## Application terminated, r = %lu\n", |
538 | r & ~EFI_ERROR_MASK); | |
539 | ||
540 | if (r != EFI_SUCCESS) | |
541 | return 1; | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
b9939336 AG |
546 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
547 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
548 | { | |
bc4f9133 HS |
549 | unsigned long addr; |
550 | char *saddr; | |
3eb0841b | 551 | efi_status_t r; |
354264b3 | 552 | unsigned long fdt_addr; |
b9939336 | 553 | |
c3b11dea HS |
554 | /* Allow unaligned memory access */ |
555 | allow_unaligned(); | |
556 | ||
fc225e60 HS |
557 | /* Initialize EFI drivers */ |
558 | r = efi_init_obj_list(); | |
559 | if (r != EFI_SUCCESS) { | |
560 | printf("Error: Cannot set up EFI drivers, r = %lu\n", | |
561 | r & ~EFI_ERROR_MASK); | |
562 | return CMD_RET_FAILURE; | |
563 | } | |
564 | ||
b9939336 | 565 | if (argc < 2) |
3c1dcef6 | 566 | return CMD_RET_USAGE; |
bc4f9133 HS |
567 | |
568 | if (argc > 2) { | |
354264b3 | 569 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
bc4f9133 HS |
570 | if (!fdt_addr && *argv[2] != '0') |
571 | return CMD_RET_USAGE; | |
572 | /* Install device tree */ | |
9dff4900 | 573 | r = efi_install_fdt(fdt_addr); |
bc4f9133 HS |
574 | if (r != EFI_SUCCESS) { |
575 | printf("ERROR: failed to install device tree\n"); | |
576 | return CMD_RET_FAILURE; | |
577 | } | |
578 | } else { | |
579 | /* Remove device tree. EFI_NOT_FOUND can be ignored here */ | |
580 | efi_install_configuration_table(&efi_guid_fdt, NULL); | |
581 | printf("WARNING: booting without device tree\n"); | |
582 | } | |
c7ae3dfd SG |
583 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
584 | if (!strcmp(argv[1], "hello")) { | |
5e44489b | 585 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
b9939336 | 586 | |
51c533fd HS |
587 | saddr = env_get("loadaddr"); |
588 | if (saddr) | |
589 | addr = simple_strtoul(saddr, NULL, 16); | |
590 | else | |
591 | addr = CONFIG_SYS_LOAD_ADDR; | |
354264b3 | 592 | memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size); |
c7ae3dfd | 593 | } else |
623b3a57 HS |
594 | #endif |
595 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST | |
596 | if (!strcmp(argv[1], "selftest")) { | |
faea1041 | 597 | struct efi_loaded_image_obj *image_obj; |
c982874e | 598 | struct efi_loaded_image *loaded_image_info; |
7aca68ca | 599 | |
d9717eae | 600 | if (bootefi_test_prepare(&image_obj, &loaded_image_info, |
f4f0f7cb SG |
601 | "\\selftest", (uintptr_t)&efi_selftest, |
602 | "efi_selftest")) | |
2ab7ef74 | 603 | return CMD_RET_FAILURE; |
f972dc14 | 604 | |
d78e40d6 | 605 | /* Execute the test */ |
d39646a3 | 606 | r = efi_selftest(&image_obj->header, &systab); |
5e2f0391 | 607 | bootefi_run_finish(image_obj, loaded_image_info); |
c2b53902 | 608 | return r != EFI_SUCCESS; |
623b3a57 | 609 | } else |
c7ae3dfd | 610 | #endif |
9975fe96 | 611 | if (!strcmp(argv[1], "bootmgr")) { |
bc4f9133 | 612 | return do_bootefi_bootmgr_exec(); |
9975fe96 | 613 | } else { |
c7ae3dfd | 614 | saddr = argv[1]; |
b9939336 | 615 | |
c7ae3dfd | 616 | addr = simple_strtoul(saddr, NULL, 16); |
49db1cb8 HS |
617 | /* Check that a numeric value was passed */ |
618 | if (!addr && *saddr != '0') | |
619 | return CMD_RET_USAGE; | |
c7ae3dfd | 620 | |
1c39809b AG |
621 | } |
622 | ||
5ee31baf | 623 | printf("## Starting EFI application at %08lx ...\n", addr); |
354264b3 | 624 | r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path, |
bc4f9133 | 625 | bootefi_image_path); |
1da1bac4 HS |
626 | printf("## Application terminated, r = %lu\n", |
627 | r & ~EFI_ERROR_MASK); | |
b9939336 | 628 | |
1da1bac4 HS |
629 | if (r != EFI_SUCCESS) |
630 | return 1; | |
631 | else | |
632 | return 0; | |
b9939336 AG |
633 | } |
634 | ||
635 | #ifdef CONFIG_SYS_LONGHELP | |
636 | static char bootefi_help_text[] = | |
1c39809b AG |
637 | "<image address> [fdt address]\n" |
638 | " - boot EFI payload stored at address <image address>.\n" | |
639 | " If specified, the device tree located at <fdt address> gets\n" | |
c7ae3dfd SG |
640 | " exposed as EFI configuration table.\n" |
641 | #ifdef CONFIG_CMD_BOOTEFI_HELLO | |
623b3a57 HS |
642 | "bootefi hello\n" |
643 | " - boot a sample Hello World application stored within U-Boot\n" | |
644 | #endif | |
645 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST | |
bc4f9133 | 646 | "bootefi selftest [fdt address]\n" |
623b3a57 | 647 | " - boot an EFI selftest application stored within U-Boot\n" |
d78e40d6 HS |
648 | " Use environment variable efi_selftest to select a single test.\n" |
649 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" | |
c7ae3dfd | 650 | #endif |
f623e07f | 651 | "bootefi bootmgr [fdt addr]\n" |
9975fe96 RC |
652 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
653 | "\n" | |
654 | " If specified, the device tree located at <fdt address> gets\n" | |
655 | " exposed as EFI configuration table.\n"; | |
b9939336 AG |
656 | #endif |
657 | ||
658 | U_BOOT_CMD( | |
1c39809b | 659 | bootefi, 3, 0, do_bootefi, |
92dfd922 | 660 | "Boots an EFI payload from memory", |
b9939336 AG |
661 | bootefi_help_text |
662 | ); | |
0f4060eb | 663 | |
95c5553e RC |
664 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
665 | { | |
f1589ffb AT |
666 | struct efi_device_path *device, *image; |
667 | efi_status_t ret; | |
f9d334bd | 668 | |
79276eb2 HS |
669 | /* efi_set_bootdev is typically called repeatedly, recover memory */ |
670 | efi_free_pool(bootefi_device_path); | |
671 | efi_free_pool(bootefi_image_path); | |
9975fe96 | 672 | |
f1589ffb AT |
673 | ret = efi_dp_from_name(dev, devnr, path, &device, &image); |
674 | if (ret == EFI_SUCCESS) { | |
675 | bootefi_device_path = device; | |
676 | bootefi_image_path = image; | |
49271666 | 677 | } else { |
f1589ffb AT |
678 | bootefi_device_path = NULL; |
679 | bootefi_image_path = NULL; | |
49271666 | 680 | } |
0f4060eb | 681 | } |