bootefi.c 18 KB

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