bootefi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. if (mem_handle)
  424. efi_delete_handle(mem_handle);
  425. if (file_path)
  426. efi_free_pool(file_path);
  427. return ret;
  428. }
  429. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  430. static efi_status_t bootefi_run_prepare(const char *load_options_path,
  431. struct efi_device_path *device_path,
  432. struct efi_device_path *image_path,
  433. struct efi_loaded_image_obj **image_objp,
  434. struct efi_loaded_image **loaded_image_infop)
  435. {
  436. efi_status_t ret;
  437. u16 *load_options;
  438. ret = efi_setup_loaded_image(device_path, image_path, image_objp,
  439. loaded_image_infop);
  440. if (ret != EFI_SUCCESS)
  441. return ret;
  442. /* Transfer environment variable as load options */
  443. return set_load_options((efi_handle_t)*image_objp, load_options_path,
  444. &load_options);
  445. }
  446. /**
  447. * bootefi_test_prepare() - prepare to run an EFI test
  448. *
  449. * Prepare to run a test as if it were provided by a loaded image.
  450. *
  451. * @image_objp: pointer to be set to the loaded image handle
  452. * @loaded_image_infop: pointer to be set to the loaded image protocol
  453. * @path: dummy file path used to construct the device path
  454. * set in the loaded image protocol
  455. * @load_options_path: name of a U-Boot environment variable. Its value is
  456. * set as load options in the loaded image protocol.
  457. * Return: status code
  458. */
  459. static efi_status_t bootefi_test_prepare
  460. (struct efi_loaded_image_obj **image_objp,
  461. struct efi_loaded_image **loaded_image_infop, const char *path,
  462. const char *load_options_path)
  463. {
  464. efi_status_t ret;
  465. /* Construct a dummy device path */
  466. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
  467. if (!bootefi_device_path)
  468. return EFI_OUT_OF_RESOURCES;
  469. bootefi_image_path = efi_dp_from_file(NULL, 0, path);
  470. if (!bootefi_image_path) {
  471. ret = EFI_OUT_OF_RESOURCES;
  472. goto failure;
  473. }
  474. ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
  475. bootefi_image_path, image_objp,
  476. loaded_image_infop);
  477. if (ret == EFI_SUCCESS)
  478. return ret;
  479. efi_free_pool(bootefi_image_path);
  480. bootefi_image_path = NULL;
  481. failure:
  482. efi_free_pool(bootefi_device_path);
  483. bootefi_device_path = NULL;
  484. return ret;
  485. }
  486. /**
  487. * bootefi_run_finish() - finish up after running an EFI test
  488. *
  489. * @loaded_image_info: Pointer to a struct which holds the loaded image info
  490. * @image_obj: Pointer to a struct which holds the loaded image object
  491. */
  492. static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
  493. struct efi_loaded_image *loaded_image_info)
  494. {
  495. efi_restore_gd();
  496. free(loaded_image_info->load_options);
  497. efi_delete_handle(&image_obj->header);
  498. }
  499. /**
  500. * do_efi_selftest() - execute EFI selftest
  501. *
  502. * Return: status code
  503. */
  504. static int do_efi_selftest(void)
  505. {
  506. struct efi_loaded_image_obj *image_obj;
  507. struct efi_loaded_image *loaded_image_info;
  508. efi_status_t ret;
  509. ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
  510. "\\selftest", "efi_selftest");
  511. if (ret != EFI_SUCCESS)
  512. return CMD_RET_FAILURE;
  513. /* Execute the test */
  514. ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
  515. bootefi_run_finish(image_obj, loaded_image_info);
  516. return ret != EFI_SUCCESS;
  517. }
  518. #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
  519. /**
  520. * do_bootefi() - execute `bootefi` command
  521. *
  522. * @cmdtp: table entry describing command
  523. * @flag: bitmap indicating how the command was invoked
  524. * @argc: number of arguments
  525. * @argv: command line arguments
  526. * Return: status code
  527. */
  528. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  529. {
  530. efi_status_t ret;
  531. void *fdt;
  532. if (argc < 2)
  533. return CMD_RET_USAGE;
  534. /* Initialize EFI drivers */
  535. ret = efi_init_obj_list();
  536. if (ret != EFI_SUCCESS) {
  537. printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
  538. ret & ~EFI_ERROR_MASK);
  539. return CMD_RET_FAILURE;
  540. }
  541. if (argc > 2) {
  542. uintptr_t fdt_addr;
  543. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  544. fdt = map_sysmem(fdt_addr, 0);
  545. } else {
  546. fdt = EFI_FDT_USE_INTERNAL;
  547. }
  548. ret = efi_install_fdt(fdt);
  549. if (ret == EFI_INVALID_PARAMETER)
  550. return CMD_RET_USAGE;
  551. else if (ret != EFI_SUCCESS)
  552. return CMD_RET_FAILURE;
  553. if (!strcmp(argv[1], "bootmgr"))
  554. return do_efibootmgr();
  555. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  556. else if (!strcmp(argv[1], "selftest"))
  557. return do_efi_selftest();
  558. #endif
  559. return do_bootefi_image(argv[1]);
  560. }
  561. #ifdef CONFIG_SYS_LONGHELP
  562. static char bootefi_help_text[] =
  563. "<image address> [fdt address]\n"
  564. " - boot EFI payload stored at address <image address>.\n"
  565. " If specified, the device tree located at <fdt address> gets\n"
  566. " exposed as EFI configuration table.\n"
  567. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  568. "bootefi hello\n"
  569. " - boot a sample Hello World application stored within U-Boot\n"
  570. #endif
  571. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  572. "bootefi selftest [fdt address]\n"
  573. " - boot an EFI selftest application stored within U-Boot\n"
  574. " Use environment variable efi_selftest to select a single test.\n"
  575. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  576. #endif
  577. "bootefi bootmgr [fdt address]\n"
  578. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  579. "\n"
  580. " If specified, the device tree located at <fdt address> gets\n"
  581. " exposed as EFI configuration table.\n";
  582. #endif
  583. U_BOOT_CMD(
  584. bootefi, 3, 0, do_bootefi,
  585. "Boots an EFI payload from memory",
  586. bootefi_help_text
  587. );
  588. /**
  589. * efi_set_bootdev() - set boot device
  590. *
  591. * This function is called when a file is loaded, e.g. via the 'load' command.
  592. * We use the path to this file to inform the UEFI binary about the boot device.
  593. *
  594. * @dev: device, e.g. "MMC"
  595. * @devnr: number of the device, e.g. "1:2"
  596. * @path: path to file loaded
  597. */
  598. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  599. {
  600. struct efi_device_path *device, *image;
  601. efi_status_t ret;
  602. /* efi_set_bootdev is typically called repeatedly, recover memory */
  603. efi_free_pool(bootefi_device_path);
  604. efi_free_pool(bootefi_image_path);
  605. ret = efi_dp_from_name(dev, devnr, path, &device, &image);
  606. if (ret == EFI_SUCCESS) {
  607. bootefi_device_path = device;
  608. if (image) {
  609. /* FIXME: image should not contain device */
  610. struct efi_device_path *image_tmp = image;
  611. efi_dp_split_file_path(image, &device, &image);
  612. efi_free_pool(image_tmp);
  613. }
  614. bootefi_image_path = image;
  615. } else {
  616. bootefi_device_path = NULL;
  617. bootefi_image_path = NULL;
  618. }
  619. }