]>
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 | ||
8 | #include <common.h> | |
f6c6df7e | 9 | #include <charset.h> |
b9939336 | 10 | #include <command.h> |
9d922450 | 11 | #include <dm.h> |
b9939336 | 12 | #include <efi_loader.h> |
d78e40d6 | 13 | #include <efi_selftest.h> |
7b51b576 | 14 | #include <env.h> |
b9939336 | 15 | #include <errno.h> |
336d4615 | 16 | #include <malloc.h> |
b08c8c48 MY |
17 | #include <linux/libfdt.h> |
18 | #include <linux/libfdt_env.h> | |
354264b3 | 19 | #include <mapmem.h> |
ad0c1a3d | 20 | #include <memalign.h> |
e275458c SG |
21 | #include <asm-generic/sections.h> |
22 | #include <linux/linkage.h> | |
0d9d501f AG |
23 | |
24 | DECLARE_GLOBAL_DATA_PTR; | |
b9939336 | 25 | |
95c5553e RC |
26 | static struct efi_device_path *bootefi_image_path; |
27 | static struct efi_device_path *bootefi_device_path; | |
b9939336 | 28 | |
810371a0 | 29 | /** |
d78e40d6 HS |
30 | * Set the load options of an image from an environment variable. |
31 | * | |
a3850e40 HS |
32 | * @handle: the image handle |
33 | * @env_var: name of the environment variable | |
34 | * @load_options: pointer to load options (output) | |
35 | * Return: status code | |
d78e40d6 | 36 | */ |
a3850e40 HS |
37 | static efi_status_t set_load_options(efi_handle_t handle, const char *env_var, |
38 | u16 **load_options) | |
d78e40d6 | 39 | { |
defa7b8e | 40 | struct efi_loaded_image *loaded_image_info; |
d78e40d6 HS |
41 | size_t size; |
42 | const char *env = env_get(env_var); | |
7086a71a | 43 | u16 *pos; |
defa7b8e AT |
44 | efi_status_t ret; |
45 | ||
a3850e40 | 46 | *load_options = NULL; |
defa7b8e AT |
47 | ret = EFI_CALL(systab.boottime->open_protocol( |
48 | handle, | |
49 | &efi_guid_loaded_image, | |
50 | (void **)&loaded_image_info, | |
51 | efi_root, NULL, | |
52 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)); | |
53 | if (ret != EFI_SUCCESS) | |
54 | return EFI_INVALID_PARAMETER; | |
d78e40d6 HS |
55 | |
56 | loaded_image_info->load_options = NULL; | |
57 | loaded_image_info->load_options_size = 0; | |
58 | if (!env) | |
defa7b8e AT |
59 | goto out; |
60 | ||
7086a71a | 61 | size = utf8_utf16_strlen(env) + 1; |
d78e40d6 HS |
62 | loaded_image_info->load_options = calloc(size, sizeof(u16)); |
63 | if (!loaded_image_info->load_options) { | |
64 | printf("ERROR: Out of memory\n"); | |
defa7b8e AT |
65 | EFI_CALL(systab.boottime->close_protocol(handle, |
66 | &efi_guid_loaded_image, | |
67 | efi_root, NULL)); | |
68 | return EFI_OUT_OF_RESOURCES; | |
d78e40d6 | 69 | } |
7086a71a | 70 | pos = loaded_image_info->load_options; |
a3850e40 | 71 | *load_options = pos; |
7086a71a | 72 | utf8_utf16_strcpy(&pos, env); |
d78e40d6 | 73 | loaded_image_info->load_options_size = size * 2; |
defa7b8e AT |
74 | |
75 | out: | |
76 | return EFI_CALL(systab.boottime->close_protocol(handle, | |
77 | &efi_guid_loaded_image, | |
78 | efi_root, NULL)); | |
d78e40d6 HS |
79 | } |
80 | ||
6182495e HS |
81 | #if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) |
82 | ||
9dff4900 SG |
83 | /** |
84 | * copy_fdt() - Copy the device tree to a new location available to EFI | |
85 | * | |
16b615d9 | 86 | * The FDT is copied to a suitable location within the EFI memory map. |
c3772ca1 | 87 | * Additional 12 KiB are added to the space in case the device tree needs to be |
9dff4900 SG |
88 | * expanded later with fdt_open_into(). |
89 | * | |
16b615d9 HS |
90 | * @fdtp: On entry a pointer to the flattened device tree. |
91 | * On exit a pointer to the copy of the flattened device tree. | |
4574d1b3 HS |
92 | * FDT start |
93 | * Return: status code | |
9dff4900 | 94 | */ |
16b615d9 | 95 | static efi_status_t copy_fdt(void **fdtp) |
0d9d501f | 96 | { |
ad0c1a3d | 97 | unsigned long fdt_ram_start = -1L, fdt_pages; |
9dff4900 SG |
98 | efi_status_t ret = 0; |
99 | void *fdt, *new_fdt; | |
ad0c1a3d | 100 | u64 new_fdt_addr; |
9dff4900 | 101 | uint fdt_size; |
ad0c1a3d | 102 | int i; |
0d9d501f | 103 | |
9dff4900 SG |
104 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
105 | u64 ram_start = gd->bd->bi_dram[i].start; | |
106 | u64 ram_size = gd->bd->bi_dram[i].size; | |
0d9d501f | 107 | |
ad0c1a3d AG |
108 | if (!ram_size) |
109 | continue; | |
110 | ||
111 | if (ram_start < fdt_ram_start) | |
112 | fdt_ram_start = ram_start; | |
113 | } | |
114 | ||
bc9a638a | 115 | /* |
c3772ca1 HS |
116 | * Give us at least 12 KiB of breathing room in case the device tree |
117 | * needs to be expanded later. | |
bc9a638a | 118 | */ |
16b615d9 | 119 | fdt = *fdtp; |
c3772ca1 HS |
120 | fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000); |
121 | fdt_size = fdt_pages << EFI_PAGE_SHIFT; | |
ad0c1a3d | 122 | |
16b615d9 HS |
123 | /* |
124 | * Safe fdt location is at 127 MiB. | |
125 | * On the sandbox convert from the sandbox address space. | |
126 | */ | |
127 | new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 + | |
128 | fdt_size, 0); | |
9dff4900 | 129 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
29361ec4 | 130 | EFI_BOOT_SERVICES_DATA, fdt_pages, |
9dff4900 SG |
131 | &new_fdt_addr); |
132 | if (ret != EFI_SUCCESS) { | |
ad0c1a3d | 133 | /* If we can't put it there, put it somewhere */ |
a44bffcc | 134 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
9dff4900 | 135 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
29361ec4 | 136 | EFI_BOOT_SERVICES_DATA, fdt_pages, |
9dff4900 SG |
137 | &new_fdt_addr); |
138 | if (ret != EFI_SUCCESS) { | |
85a6e9b3 | 139 | printf("ERROR: Failed to reserve space for FDT\n"); |
9dff4900 | 140 | goto done; |
85a6e9b3 | 141 | } |
ad0c1a3d | 142 | } |
16b615d9 | 143 | new_fdt = (void *)(uintptr_t)new_fdt_addr; |
0d9d501f AG |
144 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
145 | fdt_set_totalsize(new_fdt, fdt_size); | |
146 | ||
16b615d9 | 147 | *fdtp = (void *)(uintptr_t)new_fdt_addr; |
9dff4900 SG |
148 | done: |
149 | return ret; | |
0d9d501f AG |
150 | } |
151 | ||
7be64b88 AP |
152 | static void efi_reserve_memory(u64 addr, u64 size) |
153 | { | |
154 | u64 pages; | |
155 | ||
156 | /* Convert from sandbox address space. */ | |
157 | addr = (uintptr_t)map_sysmem(addr, 0); | |
158 | pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK)); | |
159 | addr &= ~EFI_PAGE_MASK; | |
160 | if (efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, | |
161 | false) != EFI_SUCCESS) | |
162 | printf("Reserved memory mapping failed addr %llx size %llx\n", | |
163 | addr, size); | |
164 | } | |
165 | ||
810371a0 | 166 | /** |
416e07e2 SG |
167 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
168 | * | |
169 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are | |
170 | * ignored because this is not critical and we would rather continue to try to | |
171 | * boot. | |
172 | * | |
173 | * @fdt: Pointer to device tree | |
174 | */ | |
175 | static void efi_carve_out_dt_rsv(void *fdt) | |
806d2fa8 AG |
176 | { |
177 | int nr_rsv, i; | |
7be64b88 AP |
178 | u64 addr, size; |
179 | int nodeoffset, subnode; | |
806d2fa8 AG |
180 | |
181 | nr_rsv = fdt_num_mem_rsv(fdt); | |
182 | ||
183 | /* Look for an existing entry and add it to the efi mem map. */ | |
184 | for (i = 0; i < nr_rsv; i++) { | |
185 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) | |
186 | continue; | |
7be64b88 AP |
187 | efi_reserve_memory(addr, size); |
188 | } | |
806d2fa8 | 189 | |
7be64b88 AP |
190 | /* process reserved-memory */ |
191 | nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory"); | |
192 | if (nodeoffset >= 0) { | |
193 | subnode = fdt_first_subnode(fdt, nodeoffset); | |
194 | while (subnode >= 0) { | |
195 | /* check if this subnode has a reg property */ | |
196 | addr = fdtdec_get_addr_size(fdt, subnode, "reg", | |
197 | (fdt_size_t *)&size); | |
198 | /* | |
199 | * The /reserved-memory node may have children with | |
200 | * a size instead of a reg property. | |
201 | */ | |
4ef2b0d5 HS |
202 | if (addr != FDT_ADDR_T_NONE && |
203 | fdtdec_get_is_enabled(fdt, subnode)) | |
7be64b88 AP |
204 | efi_reserve_memory(addr, size); |
205 | subnode = fdt_next_subnode(fdt, subnode); | |
206 | } | |
806d2fa8 | 207 | } |
806d2fa8 AG |
208 | } |
209 | ||
6182495e HS |
210 | /** |
211 | * get_config_table() - get configuration table | |
212 | * | |
213 | * @guid: GUID of the configuration table | |
214 | * Return: pointer to configuration table or NULL | |
215 | */ | |
216 | static void *get_config_table(const efi_guid_t *guid) | |
217 | { | |
218 | size_t i; | |
219 | ||
220 | for (i = 0; i < systab.nr_tables; i++) { | |
221 | if (!guidcmp(guid, &systab.tables[i].guid)) | |
222 | return systab.tables[i].table; | |
223 | } | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | #endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */ | |
228 | ||
e878e6a7 | 229 | /** |
7a597259 | 230 | * efi_install_fdt() - install device tree |
e2d82f8b | 231 | * |
7d4d551e HS |
232 | * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory |
233 | * address will will be installed as configuration table, otherwise the device | |
234 | * tree located at the address indicated by environment variable fdt_addr or as | |
235 | * fallback fdtcontroladdr will be used. | |
e2d82f8b | 236 | * |
7a597259 HS |
237 | * On architectures using ACPI tables device trees shall not be installed as |
238 | * configuration table. | |
e2d82f8b | 239 | * |
7d4d551e | 240 | * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the |
753aa18f HS |
241 | * the hardware device tree as indicated by environment variable |
242 | * fdt_addr or as fallback the internal device tree as indicated by | |
243 | * the environment variable fdtcontroladdr | |
e878e6a7 | 244 | * Return: status code |
e878e6a7 | 245 | */ |
f64f2232 | 246 | efi_status_t efi_install_fdt(void *fdt) |
e878e6a7 | 247 | { |
6182495e HS |
248 | /* |
249 | * The EBBR spec requires that we have either an FDT or an ACPI table | |
250 | * but not both. | |
251 | */ | |
252 | #if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) | |
f64f2232 | 253 | if (fdt) { |
6182495e HS |
254 | printf("ERROR: can't have ACPI table and device tree.\n"); |
255 | return EFI_LOAD_ERROR; | |
256 | } | |
257 | #else | |
3ffc52fd | 258 | bootm_headers_t img = { 0 }; |
e878e6a7 AT |
259 | efi_status_t ret; |
260 | ||
f64f2232 | 261 | if (fdt == EFI_FDT_USE_INTERNAL) { |
7a597259 | 262 | const char *fdt_opt; |
f64f2232 | 263 | uintptr_t fdt_addr; |
7a597259 | 264 | |
6182495e HS |
265 | /* Look for device tree that is already installed */ |
266 | if (get_config_table(&efi_guid_fdt)) | |
267 | return EFI_SUCCESS; | |
753aa18f HS |
268 | /* Check if there is a hardware device tree */ |
269 | fdt_opt = env_get("fdt_addr"); | |
270 | /* Use our own device tree as fallback */ | |
6182495e | 271 | if (!fdt_opt) { |
753aa18f HS |
272 | fdt_opt = env_get("fdtcontroladdr"); |
273 | if (!fdt_opt) { | |
274 | printf("ERROR: need device tree\n"); | |
275 | return EFI_NOT_FOUND; | |
276 | } | |
6182495e HS |
277 | } |
278 | fdt_addr = simple_strtoul(fdt_opt, NULL, 16); | |
279 | if (!fdt_addr) { | |
753aa18f | 280 | printf("ERROR: invalid $fdt_addr or $fdtcontroladdr\n"); |
6182495e | 281 | return EFI_LOAD_ERROR; |
3ffc52fd | 282 | } |
f64f2232 | 283 | fdt = map_sysmem(fdt_addr, 0); |
6182495e | 284 | } |
3ffc52fd | 285 | |
6182495e | 286 | /* Install device tree */ |
6182495e HS |
287 | if (fdt_check_header(fdt)) { |
288 | printf("ERROR: invalid device tree\n"); | |
289 | return EFI_LOAD_ERROR; | |
290 | } | |
3ffc52fd | 291 | |
6182495e HS |
292 | /* Prepare device tree for payload */ |
293 | ret = copy_fdt(&fdt); | |
294 | if (ret) { | |
295 | printf("ERROR: out of memory\n"); | |
296 | return EFI_OUT_OF_RESOURCES; | |
297 | } | |
3ffc52fd | 298 | |
6182495e HS |
299 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
300 | printf("ERROR: failed to process device tree\n"); | |
301 | return EFI_LOAD_ERROR; | |
302 | } | |
303 | ||
fef907b2 HS |
304 | /* Create memory reservations as indicated by the device tree */ |
305 | efi_carve_out_dt_rsv(fdt); | |
306 | ||
6182495e HS |
307 | /* Install device tree as UEFI table */ |
308 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); | |
309 | if (ret != EFI_SUCCESS) { | |
310 | printf("ERROR: failed to install device tree\n"); | |
311 | return ret; | |
e878e6a7 | 312 | } |
6182495e | 313 | #endif /* GENERATE_ACPI_TABLE */ |
e878e6a7 AT |
314 | |
315 | return EFI_SUCCESS; | |
316 | } | |
317 | ||
c982874e HS |
318 | /** |
319 | * do_bootefi_exec() - execute EFI binary | |
320 | * | |
6b95b38c | 321 | * @handle: handle of loaded image |
c982874e HS |
322 | * Return: status code |
323 | * | |
324 | * Load the EFI binary into a newly assigned memory unwinding the relocation | |
325 | * information, install the loaded image protocol, and call the binary. | |
b9939336 | 326 | */ |
6b95b38c | 327 | static efi_status_t do_bootefi_exec(efi_handle_t handle) |
b9939336 | 328 | { |
45204b10 | 329 | efi_status_t ret; |
556d8dc9 HS |
330 | efi_uintn_t exit_data_size = 0; |
331 | u16 *exit_data = NULL; | |
a3850e40 | 332 | u16 *load_options; |
95c5553e | 333 | |
3fc2b163 | 334 | /* Transfer environment variable as load options */ |
a3850e40 | 335 | ret = set_load_options(handle, "bootargs", &load_options); |
8f7e2b29 | 336 | if (ret != EFI_SUCCESS) |
6b95b38c | 337 | return ret; |
bf19273e | 338 | |
b9939336 | 339 | /* Call our payload! */ |
556d8dc9 HS |
340 | ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data)); |
341 | printf("## Application terminated, r = %lu\n", ret & ~EFI_ERROR_MASK); | |
342 | if (ret && exit_data) { | |
343 | printf("## %ls\n", exit_data); | |
344 | efi_free_pool(exit_data); | |
345 | } | |
95c5553e | 346 | |
3fc2b163 | 347 | efi_restore_gd(); |
5e2f0391 | 348 | |
a3850e40 | 349 | free(load_options); |
95c5553e RC |
350 | |
351 | return ret; | |
b9939336 AG |
352 | } |
353 | ||
d6b21894 | 354 | /** |
7e92db81 | 355 | * do_efibootmgr() - execute EFI boot manager |
d6b21894 | 356 | * |
d6b21894 | 357 | * Return: status code |
d6b21894 | 358 | */ |
7e92db81 | 359 | static int do_efibootmgr(void) |
cc999d58 | 360 | { |
6b95b38c | 361 | efi_handle_t handle; |
d6b21894 AT |
362 | efi_status_t ret; |
363 | ||
6b95b38c AT |
364 | ret = efi_bootmgr_load(&handle); |
365 | if (ret != EFI_SUCCESS) { | |
366 | printf("EFI boot manager: Cannot load any image\n"); | |
367 | return CMD_RET_FAILURE; | |
368 | } | |
cc999d58 | 369 | |
6b95b38c | 370 | ret = do_bootefi_exec(handle); |
cc999d58 | 371 | |
d6b21894 | 372 | if (ret != EFI_SUCCESS) |
6b95b38c | 373 | return CMD_RET_FAILURE; |
cc999d58 | 374 | |
6b95b38c | 375 | return CMD_RET_SUCCESS; |
cc999d58 AT |
376 | } |
377 | ||
810371a0 | 378 | /** |
7e92db81 HS |
379 | * do_bootefi_image() - execute EFI binary |
380 | * | |
381 | * Set up memory image for the binary to be loaded, prepare device path, and | |
382 | * then call do_bootefi_exec() to execute it. | |
e2e4098e AT |
383 | * |
384 | * @image_opt: string of image start address | |
e2e4098e | 385 | * Return: status code |
e2e4098e | 386 | */ |
7e92db81 | 387 | static int do_bootefi_image(const char *image_opt) |
e2e4098e | 388 | { |
6b95b38c | 389 | void *image_buf; |
6b95b38c AT |
390 | unsigned long addr, size; |
391 | const char *size_str; | |
e2e4098e AT |
392 | efi_status_t ret; |
393 | ||
e2e4098e AT |
394 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
395 | if (!strcmp(image_opt, "hello")) { | |
396 | char *saddr; | |
e2e4098e AT |
397 | |
398 | saddr = env_get("loadaddr"); | |
6b95b38c AT |
399 | size = __efi_helloworld_end - __efi_helloworld_begin; |
400 | ||
e2e4098e AT |
401 | if (saddr) |
402 | addr = simple_strtoul(saddr, NULL, 16); | |
403 | else | |
404 | addr = CONFIG_SYS_LOAD_ADDR; | |
6b95b38c AT |
405 | |
406 | image_buf = map_sysmem(addr, size); | |
407 | memcpy(image_buf, __efi_helloworld_begin, size); | |
408 | ||
f9ceb6ac HS |
409 | efi_free_pool(bootefi_device_path); |
410 | efi_free_pool(bootefi_image_path); | |
411 | bootefi_device_path = NULL; | |
412 | bootefi_image_path = NULL; | |
e2e4098e AT |
413 | } else |
414 | #endif | |
415 | { | |
6b95b38c AT |
416 | size_str = env_get("filesize"); |
417 | if (size_str) | |
418 | size = simple_strtoul(size_str, NULL, 16); | |
419 | else | |
420 | size = 0; | |
421 | ||
e2e4098e AT |
422 | addr = simple_strtoul(image_opt, NULL, 16); |
423 | /* Check that a numeric value was passed */ | |
424 | if (!addr && *image_opt != '0') | |
425 | return CMD_RET_USAGE; | |
6b95b38c AT |
426 | |
427 | image_buf = map_sysmem(addr, size); | |
e2e4098e | 428 | } |
f9ceb6ac HS |
429 | ret = efi_run_image(image_buf, size); |
430 | ||
431 | if (ret != EFI_SUCCESS) | |
432 | return CMD_RET_FAILURE; | |
433 | ||
434 | return CMD_RET_SUCCESS; | |
435 | } | |
436 | ||
437 | /** | |
438 | * efi_run_image() - run loaded UEFI image | |
439 | * | |
440 | * @source_buffer: memory address of the UEFI image | |
441 | * @source_size: size of the UEFI image | |
442 | * Return: status code | |
443 | */ | |
444 | efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size) | |
445 | { | |
446 | efi_handle_t mem_handle = NULL, handle; | |
447 | struct efi_device_path *file_path = NULL; | |
448 | efi_status_t ret; | |
e2e4098e | 449 | |
f9ceb6ac | 450 | if (!bootefi_device_path || !bootefi_image_path) { |
6b95b38c AT |
451 | /* |
452 | * Special case for efi payload not loaded from disk, | |
453 | * such as 'bootefi hello' or for example payload | |
454 | * loaded directly into memory via JTAG, etc: | |
455 | */ | |
456 | file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, | |
f9ceb6ac HS |
457 | (uintptr_t)source_buffer, |
458 | source_size); | |
6b95b38c AT |
459 | /* |
460 | * Make sure that device for device_path exist | |
461 | * in load_image(). Otherwise, shell and grub will fail. | |
462 | */ | |
463 | ret = efi_create_handle(&mem_handle); | |
464 | if (ret != EFI_SUCCESS) | |
465 | goto out; | |
466 | ||
467 | ret = efi_add_protocol(mem_handle, &efi_guid_device_path, | |
468 | file_path); | |
469 | if (ret != EFI_SUCCESS) | |
470 | goto out; | |
471 | } else { | |
f9ceb6ac HS |
472 | file_path = efi_dp_append(bootefi_device_path, |
473 | bootefi_image_path); | |
6b95b38c AT |
474 | } |
475 | ||
f9ceb6ac HS |
476 | ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer, |
477 | source_size, &handle)); | |
6b95b38c AT |
478 | if (ret != EFI_SUCCESS) |
479 | goto out; | |
480 | ||
481 | ret = do_bootefi_exec(handle); | |
6b95b38c AT |
482 | |
483 | out: | |
484 | if (mem_handle) | |
485 | efi_delete_handle(mem_handle); | |
486 | if (file_path) | |
487 | efi_free_pool(file_path); | |
f9ceb6ac | 488 | return ret; |
e2e4098e AT |
489 | } |
490 | ||
d9717eae | 491 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
3fc2b163 AT |
492 | static efi_status_t bootefi_run_prepare(const char *load_options_path, |
493 | struct efi_device_path *device_path, | |
494 | struct efi_device_path *image_path, | |
495 | struct efi_loaded_image_obj **image_objp, | |
496 | struct efi_loaded_image **loaded_image_infop) | |
497 | { | |
498 | efi_status_t ret; | |
a3850e40 | 499 | u16 *load_options; |
3fc2b163 AT |
500 | |
501 | ret = efi_setup_loaded_image(device_path, image_path, image_objp, | |
502 | loaded_image_infop); | |
503 | if (ret != EFI_SUCCESS) | |
504 | return ret; | |
505 | ||
506 | /* Transfer environment variable as load options */ | |
a3850e40 HS |
507 | return set_load_options((efi_handle_t)*image_objp, load_options_path, |
508 | &load_options); | |
3fc2b163 AT |
509 | } |
510 | ||
d9717eae SG |
511 | /** |
512 | * bootefi_test_prepare() - prepare to run an EFI test | |
513 | * | |
1504bb0d | 514 | * Prepare to run a test as if it were provided by a loaded image. |
d9717eae | 515 | * |
1504bb0d HS |
516 | * @image_objp: pointer to be set to the loaded image handle |
517 | * @loaded_image_infop: pointer to be set to the loaded image protocol | |
518 | * @path: dummy file path used to construct the device path | |
519 | * set in the loaded image protocol | |
520 | * @load_options_path: name of a U-Boot environment variable. Its value is | |
521 | * set as load options in the loaded image protocol. | |
522 | * Return: status code | |
d9717eae SG |
523 | */ |
524 | static efi_status_t bootefi_test_prepare | |
525 | (struct efi_loaded_image_obj **image_objp, | |
1504bb0d HS |
526 | struct efi_loaded_image **loaded_image_infop, const char *path, |
527 | const char *load_options_path) | |
d9717eae | 528 | { |
1504bb0d HS |
529 | efi_status_t ret; |
530 | ||
d9717eae | 531 | /* Construct a dummy device path */ |
1504bb0d | 532 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0); |
d9717eae SG |
533 | if (!bootefi_device_path) |
534 | return EFI_OUT_OF_RESOURCES; | |
1504bb0d | 535 | |
d9717eae | 536 | bootefi_image_path = efi_dp_from_file(NULL, 0, path); |
1504bb0d HS |
537 | if (!bootefi_image_path) { |
538 | ret = EFI_OUT_OF_RESOURCES; | |
539 | goto failure; | |
540 | } | |
541 | ||
542 | ret = bootefi_run_prepare(load_options_path, bootefi_device_path, | |
543 | bootefi_image_path, image_objp, | |
544 | loaded_image_infop); | |
545 | if (ret == EFI_SUCCESS) | |
546 | return ret; | |
d9717eae | 547 | |
1504bb0d HS |
548 | efi_free_pool(bootefi_image_path); |
549 | bootefi_image_path = NULL; | |
550 | failure: | |
551 | efi_free_pool(bootefi_device_path); | |
552 | bootefi_device_path = NULL; | |
553 | return ret; | |
d9717eae SG |
554 | } |
555 | ||
3fc2b163 AT |
556 | /** |
557 | * bootefi_run_finish() - finish up after running an EFI test | |
558 | * | |
559 | * @loaded_image_info: Pointer to a struct which holds the loaded image info | |
560 | * @image_obj: Pointer to a struct which holds the loaded image object | |
561 | */ | |
562 | static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj, | |
563 | struct efi_loaded_image *loaded_image_info) | |
564 | { | |
565 | efi_restore_gd(); | |
566 | free(loaded_image_info->load_options); | |
567 | efi_delete_handle(&image_obj->header); | |
568 | } | |
569 | ||
570 | /** | |
7e92db81 | 571 | * do_efi_selftest() - execute EFI selftest |
3fc2b163 | 572 | * |
3fc2b163 | 573 | * Return: status code |
3fc2b163 | 574 | */ |
7e92db81 | 575 | static int do_efi_selftest(void) |
3fc2b163 AT |
576 | { |
577 | struct efi_loaded_image_obj *image_obj; | |
578 | struct efi_loaded_image *loaded_image_info; | |
579 | efi_status_t ret; | |
580 | ||
3fc2b163 AT |
581 | ret = bootefi_test_prepare(&image_obj, &loaded_image_info, |
582 | "\\selftest", "efi_selftest"); | |
583 | if (ret != EFI_SUCCESS) | |
584 | return CMD_RET_FAILURE; | |
585 | ||
586 | /* Execute the test */ | |
587 | ret = EFI_CALL(efi_selftest(&image_obj->header, &systab)); | |
588 | bootefi_run_finish(image_obj, loaded_image_info); | |
589 | ||
590 | return ret != EFI_SUCCESS; | |
591 | } | |
d9717eae SG |
592 | #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */ |
593 | ||
7e92db81 HS |
594 | /** |
595 | * do_bootefi() - execute `bootefi` command | |
596 | * | |
597 | * @cmdtp: table entry describing command | |
598 | * @flag: bitmap indicating how the command was invoked | |
599 | * @argc: number of arguments | |
600 | * @argv: command line arguments | |
601 | * Return: status code | |
602 | */ | |
b9939336 AG |
603 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
604 | { | |
7e92db81 | 605 | efi_status_t ret; |
f64f2232 | 606 | void *fdt; |
7e92db81 | 607 | |
3fc2b163 AT |
608 | if (argc < 2) |
609 | return CMD_RET_USAGE; | |
d6b21894 | 610 | |
7e92db81 HS |
611 | /* Initialize EFI drivers */ |
612 | ret = efi_init_obj_list(); | |
613 | if (ret != EFI_SUCCESS) { | |
614 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", | |
615 | ret & ~EFI_ERROR_MASK); | |
616 | return CMD_RET_FAILURE; | |
617 | } | |
618 | ||
f64f2232 HS |
619 | if (argc > 2) { |
620 | uintptr_t fdt_addr; | |
621 | ||
7a597259 | 622 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
f64f2232 HS |
623 | fdt = map_sysmem(fdt_addr, 0); |
624 | } else { | |
625 | fdt = EFI_FDT_USE_INTERNAL; | |
626 | } | |
627 | ret = efi_install_fdt(fdt); | |
7e92db81 HS |
628 | if (ret == EFI_INVALID_PARAMETER) |
629 | return CMD_RET_USAGE; | |
630 | else if (ret != EFI_SUCCESS) | |
631 | return CMD_RET_FAILURE; | |
632 | ||
d6b21894 | 633 | if (!strcmp(argv[1], "bootmgr")) |
7e92db81 | 634 | return do_efibootmgr(); |
3fc2b163 AT |
635 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
636 | else if (!strcmp(argv[1], "selftest")) | |
7e92db81 | 637 | return do_efi_selftest(); |
3fc2b163 AT |
638 | #endif |
639 | ||
7e92db81 | 640 | return do_bootefi_image(argv[1]); |
b9939336 AG |
641 | } |
642 | ||
643 | #ifdef CONFIG_SYS_LONGHELP | |
644 | static char bootefi_help_text[] = | |
1c39809b AG |
645 | "<image address> [fdt address]\n" |
646 | " - boot EFI payload stored at address <image address>.\n" | |
647 | " If specified, the device tree located at <fdt address> gets\n" | |
c7ae3dfd SG |
648 | " exposed as EFI configuration table.\n" |
649 | #ifdef CONFIG_CMD_BOOTEFI_HELLO | |
623b3a57 HS |
650 | "bootefi hello\n" |
651 | " - boot a sample Hello World application stored within U-Boot\n" | |
652 | #endif | |
653 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST | |
bc4f9133 | 654 | "bootefi selftest [fdt address]\n" |
623b3a57 | 655 | " - boot an EFI selftest application stored within U-Boot\n" |
d78e40d6 HS |
656 | " Use environment variable efi_selftest to select a single test.\n" |
657 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" | |
c7ae3dfd | 658 | #endif |
6b95b38c | 659 | "bootefi bootmgr [fdt address]\n" |
9975fe96 RC |
660 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
661 | "\n" | |
662 | " If specified, the device tree located at <fdt address> gets\n" | |
663 | " exposed as EFI configuration table.\n"; | |
b9939336 AG |
664 | #endif |
665 | ||
666 | U_BOOT_CMD( | |
1c39809b | 667 | bootefi, 3, 0, do_bootefi, |
92dfd922 | 668 | "Boots an EFI payload from memory", |
b9939336 AG |
669 | bootefi_help_text |
670 | ); | |
0f4060eb | 671 | |
810371a0 HS |
672 | /** |
673 | * efi_set_bootdev() - set boot device | |
674 | * | |
675 | * This function is called when a file is loaded, e.g. via the 'load' command. | |
676 | * We use the path to this file to inform the UEFI binary about the boot device. | |
677 | * | |
678 | * @dev: device, e.g. "MMC" | |
679 | * @devnr: number of the device, e.g. "1:2" | |
680 | * @path: path to file loaded | |
681 | */ | |
95c5553e RC |
682 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
683 | { | |
f1589ffb AT |
684 | struct efi_device_path *device, *image; |
685 | efi_status_t ret; | |
f9d334bd | 686 | |
79276eb2 HS |
687 | /* efi_set_bootdev is typically called repeatedly, recover memory */ |
688 | efi_free_pool(bootefi_device_path); | |
689 | efi_free_pool(bootefi_image_path); | |
9975fe96 | 690 | |
f1589ffb AT |
691 | ret = efi_dp_from_name(dev, devnr, path, &device, &image); |
692 | if (ret == EFI_SUCCESS) { | |
693 | bootefi_device_path = device; | |
6b95b38c AT |
694 | if (image) { |
695 | /* FIXME: image should not contain device */ | |
696 | struct efi_device_path *image_tmp = image; | |
697 | ||
698 | efi_dp_split_file_path(image, &device, &image); | |
699 | efi_free_pool(image_tmp); | |
700 | } | |
f1589ffb | 701 | bootefi_image_path = image; |
49271666 | 702 | } else { |
f1589ffb AT |
703 | bootefi_device_path = NULL; |
704 | bootefi_image_path = NULL; | |
49271666 | 705 | } |
0f4060eb | 706 | } |