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