bootefi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 <malloc.h>
  16. #include <linux/libfdt.h>
  17. #include <linux/libfdt_env.h>
  18. #include <mapmem.h>
  19. #include <memalign.h>
  20. #include <asm-generic/sections.h>
  21. #include <linux/linkage.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static struct efi_device_path *bootefi_image_path;
  24. static struct efi_device_path *bootefi_device_path;
  25. /**
  26. * Set the load options of an image from an environment variable.
  27. *
  28. * @handle: the image handle
  29. * @env_var: name of the environment variable
  30. * @load_options: pointer to load options (output)
  31. * Return: status code
  32. */
  33. static efi_status_t set_load_options(efi_handle_t handle, const char *env_var,
  34. u16 **load_options)
  35. {
  36. struct efi_loaded_image *loaded_image_info;
  37. size_t size;
  38. const char *env = env_get(env_var);
  39. u16 *pos;
  40. efi_status_t ret;
  41. *load_options = NULL;
  42. ret = EFI_CALL(systab.boottime->open_protocol(
  43. handle,
  44. &efi_guid_loaded_image,
  45. (void **)&loaded_image_info,
  46. efi_root, NULL,
  47. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
  48. if (ret != EFI_SUCCESS)
  49. return EFI_INVALID_PARAMETER;
  50. loaded_image_info->load_options = NULL;
  51. loaded_image_info->load_options_size = 0;
  52. if (!env)
  53. goto out;
  54. size = utf8_utf16_strlen(env) + 1;
  55. loaded_image_info->load_options = calloc(size, sizeof(u16));
  56. if (!loaded_image_info->load_options) {
  57. printf("ERROR: Out of memory\n");
  58. EFI_CALL(systab.boottime->close_protocol(handle,
  59. &efi_guid_loaded_image,
  60. efi_root, NULL));
  61. return EFI_OUT_OF_RESOURCES;
  62. }
  63. pos = loaded_image_info->load_options;
  64. *load_options = pos;
  65. utf8_utf16_strcpy(&pos, env);
  66. loaded_image_info->load_options_size = size * 2;
  67. out:
  68. return EFI_CALL(systab.boottime->close_protocol(handle,
  69. &efi_guid_loaded_image,
  70. efi_root, NULL));
  71. }
  72. #if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
  73. /**
  74. * copy_fdt() - Copy the device tree to a new location available to EFI
  75. *
  76. * The FDT is copied to a suitable location within the EFI memory map.
  77. * Additional 12 KiB are added to the space in case the device tree needs to be
  78. * expanded later with fdt_open_into().
  79. *
  80. * @fdtp: On entry a pointer to the flattened device tree.
  81. * On exit a pointer to the copy of the flattened device tree.
  82. * FDT start
  83. * Return: status code
  84. */
  85. static efi_status_t copy_fdt(void **fdtp)
  86. {
  87. unsigned long fdt_ram_start = -1L, fdt_pages;
  88. efi_status_t ret = 0;
  89. void *fdt, *new_fdt;
  90. u64 new_fdt_addr;
  91. uint fdt_size;
  92. int i;
  93. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  94. u64 ram_start = gd->bd->bi_dram[i].start;
  95. u64 ram_size = gd->bd->bi_dram[i].size;
  96. if (!ram_size)
  97. continue;
  98. if (ram_start < fdt_ram_start)
  99. fdt_ram_start = ram_start;
  100. }
  101. /*
  102. * Give us at least 12 KiB of breathing room in case the device tree
  103. * needs to be expanded later.
  104. */
  105. fdt = *fdtp;
  106. fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
  107. fdt_size = fdt_pages << EFI_PAGE_SHIFT;
  108. /*
  109. * Safe fdt location is at 127 MiB.
  110. * On the sandbox convert from the sandbox address space.
  111. */
  112. new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
  113. fdt_size, 0);
  114. ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  115. EFI_BOOT_SERVICES_DATA, fdt_pages,
  116. &new_fdt_addr);
  117. if (ret != EFI_SUCCESS) {
  118. /* If we can't put it there, put it somewhere */
  119. new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
  120. ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  121. EFI_BOOT_SERVICES_DATA, fdt_pages,
  122. &new_fdt_addr);
  123. if (ret != EFI_SUCCESS) {
  124. printf("ERROR: Failed to reserve space for FDT\n");
  125. goto done;
  126. }
  127. }
  128. new_fdt = (void *)(uintptr_t)new_fdt_addr;
  129. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  130. fdt_set_totalsize(new_fdt, fdt_size);
  131. *fdtp = (void *)(uintptr_t)new_fdt_addr;
  132. done:
  133. return ret;
  134. }
  135. static void efi_reserve_memory(u64 addr, u64 size)
  136. {
  137. u64 pages;
  138. /* Convert from sandbox address space. */
  139. addr = (uintptr_t)map_sysmem(addr, 0);
  140. pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
  141. addr &= ~EFI_PAGE_MASK;
  142. if (efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
  143. false) != EFI_SUCCESS)
  144. printf("Reserved memory mapping failed addr %llx size %llx\n",
  145. addr, size);
  146. }
  147. /**
  148. * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
  149. *
  150. * The mem_rsv entries of the FDT are added to the memory map. Any failures are
  151. * ignored because this is not critical and we would rather continue to try to
  152. * boot.
  153. *
  154. * @fdt: Pointer to device tree
  155. */
  156. static void efi_carve_out_dt_rsv(void *fdt)
  157. {
  158. int nr_rsv, i;
  159. u64 addr, size;
  160. int nodeoffset, subnode;
  161. nr_rsv = fdt_num_mem_rsv(fdt);
  162. /* Look for an existing entry and add it to the efi mem map. */
  163. for (i = 0; i < nr_rsv; i++) {
  164. if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
  165. continue;
  166. efi_reserve_memory(addr, size);
  167. }
  168. /* process reserved-memory */
  169. nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
  170. if (nodeoffset >= 0) {
  171. subnode = fdt_first_subnode(fdt, nodeoffset);
  172. while (subnode >= 0) {
  173. /* check if this subnode has a reg property */
  174. addr = fdtdec_get_addr_size(fdt, subnode, "reg",
  175. (fdt_size_t *)&size);
  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(addr, 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(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  527. {
  528. efi_status_t ret;
  529. void *fdt;
  530. if (argc < 2)
  531. return CMD_RET_USAGE;
  532. /* Initialize EFI drivers */
  533. ret = efi_init_obj_list();
  534. if (ret != EFI_SUCCESS) {
  535. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  536. ret & ~EFI_ERROR_MASK);
  537. return CMD_RET_FAILURE;
  538. }
  539. if (argc > 2) {
  540. uintptr_t fdt_addr;
  541. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  542. fdt = map_sysmem(fdt_addr, 0);
  543. } else {
  544. fdt = EFI_FDT_USE_INTERNAL;
  545. }
  546. ret = efi_install_fdt(fdt);
  547. if (ret == EFI_INVALID_PARAMETER)
  548. return CMD_RET_USAGE;
  549. else if (ret != EFI_SUCCESS)
  550. return CMD_RET_FAILURE;
  551. if (!strcmp(argv[1], "bootmgr"))
  552. return do_efibootmgr();
  553. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  554. else if (!strcmp(argv[1], "selftest"))
  555. return do_efi_selftest();
  556. #endif
  557. return do_bootefi_image(argv[1]);
  558. }
  559. #ifdef CONFIG_SYS_LONGHELP
  560. static char bootefi_help_text[] =
  561. "<image address> [fdt address]\n"
  562. " - boot EFI payload stored at address <image address>.\n"
  563. " If specified, the device tree located at <fdt address> gets\n"
  564. " exposed as EFI configuration table.\n"
  565. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  566. "bootefi hello\n"
  567. " - boot a sample Hello World application stored within U-Boot\n"
  568. #endif
  569. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  570. "bootefi selftest [fdt address]\n"
  571. " - boot an EFI selftest application stored within U-Boot\n"
  572. " Use environment variable efi_selftest to select a single test.\n"
  573. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  574. #endif
  575. "bootefi bootmgr [fdt address]\n"
  576. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  577. "\n"
  578. " If specified, the device tree located at <fdt address> gets\n"
  579. " exposed as EFI configuration table.\n";
  580. #endif
  581. U_BOOT_CMD(
  582. bootefi, 3, 0, do_bootefi,
  583. "Boots an EFI payload from memory",
  584. bootefi_help_text
  585. );
  586. /**
  587. * efi_set_bootdev() - set boot device
  588. *
  589. * This function is called when a file is loaded, e.g. via the 'load' command.
  590. * We use the path to this file to inform the UEFI binary about the boot device.
  591. *
  592. * @dev: device, e.g. "MMC"
  593. * @devnr: number of the device, e.g. "1:2"
  594. * @path: path to file loaded
  595. */
  596. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  597. {
  598. struct efi_device_path *device, *image;
  599. efi_status_t ret;
  600. /* efi_set_bootdev is typically called repeatedly, recover memory */
  601. efi_free_pool(bootefi_device_path);
  602. efi_free_pool(bootefi_image_path);
  603. ret = efi_dp_from_name(dev, devnr, path, &device, &image);
  604. if (ret == EFI_SUCCESS) {
  605. bootefi_device_path = device;
  606. if (image) {
  607. /* FIXME: image should not contain device */
  608. struct efi_device_path *image_tmp = image;
  609. efi_dp_split_file_path(image, &device, &image);
  610. efi_free_pool(image_tmp);
  611. }
  612. bootefi_image_path = image;
  613. } else {
  614. bootefi_device_path = NULL;
  615. bootefi_image_path = NULL;
  616. }
  617. }