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 <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;
  172. fdt_size_t fdt_size;
  173. /* check if this subnode has a reg property */
  174. fdt_addr = fdtdec_get_addr_size_auto_parent(
  175. fdt, nodeoffset, subnode,
  176. "reg", 0, &fdt_size, false);
  177. /*
  178. * The /reserved-memory node may have children with
  179. * a size instead of a reg property.
  180. */
  181. if (fdt_addr != FDT_ADDR_T_NONE &&
  182. fdtdec_get_is_enabled(fdt, subnode))
  183. efi_reserve_memory(fdt_addr, fdt_size);
  184. subnode = fdt_next_subnode(fdt, subnode);
  185. }
  186. }
  187. }
  188. /**
  189. * get_config_table() - get configuration table
  190. *
  191. * @guid: GUID of the configuration table
  192. * Return: pointer to configuration table or NULL
  193. */
  194. static void *get_config_table(const efi_guid_t *guid)
  195. {
  196. size_t i;
  197. for (i = 0; i < systab.nr_tables; i++) {
  198. if (!guidcmp(guid, &systab.tables[i].guid))
  199. return systab.tables[i].table;
  200. }
  201. return NULL;
  202. }
  203. #endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
  204. /**
  205. * efi_install_fdt() - install device tree
  206. *
  207. * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
  208. * address will will be installed as configuration table, otherwise the device
  209. * tree located at the address indicated by environment variable fdt_addr or as
  210. * fallback fdtcontroladdr will be used.
  211. *
  212. * On architectures using ACPI tables device trees shall not be installed as
  213. * configuration table.
  214. *
  215. * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
  216. * the hardware device tree as indicated by environment variable
  217. * fdt_addr or as fallback the internal device tree as indicated by
  218. * the environment variable fdtcontroladdr
  219. * Return: status code
  220. */
  221. efi_status_t efi_install_fdt(void *fdt)
  222. {
  223. /*
  224. * The EBBR spec requires that we have either an FDT or an ACPI table
  225. * but not both.
  226. */
  227. #if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
  228. if (fdt) {
  229. printf("ERROR: can't have ACPI table and device tree.\n");
  230. return EFI_LOAD_ERROR;
  231. }
  232. #else
  233. bootm_headers_t img = { 0 };
  234. efi_status_t ret;
  235. if (fdt == EFI_FDT_USE_INTERNAL) {
  236. const char *fdt_opt;
  237. uintptr_t fdt_addr;
  238. /* Look for device tree that is already installed */
  239. if (get_config_table(&efi_guid_fdt))
  240. return EFI_SUCCESS;
  241. /* Check if there is a hardware device tree */
  242. fdt_opt = env_get("fdt_addr");
  243. /* Use our own device tree as fallback */
  244. if (!fdt_opt) {
  245. fdt_opt = env_get("fdtcontroladdr");
  246. if (!fdt_opt) {
  247. printf("ERROR: need device tree\n");
  248. return EFI_NOT_FOUND;
  249. }
  250. }
  251. fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
  252. if (!fdt_addr) {
  253. printf("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
  254. return EFI_LOAD_ERROR;
  255. }
  256. fdt = map_sysmem(fdt_addr, 0);
  257. }
  258. /* Install device tree */
  259. if (fdt_check_header(fdt)) {
  260. printf("ERROR: invalid device tree\n");
  261. return EFI_LOAD_ERROR;
  262. }
  263. /* Prepare device tree for payload */
  264. ret = copy_fdt(&fdt);
  265. if (ret) {
  266. printf("ERROR: out of memory\n");
  267. return EFI_OUT_OF_RESOURCES;
  268. }
  269. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  270. printf("ERROR: failed to process device tree\n");
  271. return EFI_LOAD_ERROR;
  272. }
  273. /* Create memory reservations as indicated by the device tree */
  274. efi_carve_out_dt_rsv(fdt);
  275. /* Install device tree as UEFI table */
  276. ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
  277. if (ret != EFI_SUCCESS) {
  278. printf("ERROR: failed to install device tree\n");
  279. return ret;
  280. }
  281. #endif /* GENERATE_ACPI_TABLE */
  282. return EFI_SUCCESS;
  283. }
  284. /**
  285. * do_bootefi_exec() - execute EFI binary
  286. *
  287. * @handle: handle of loaded image
  288. * Return: status code
  289. *
  290. * Load the EFI binary into a newly assigned memory unwinding the relocation
  291. * information, install the loaded image protocol, and call the binary.
  292. */
  293. static efi_status_t do_bootefi_exec(efi_handle_t handle)
  294. {
  295. efi_status_t ret;
  296. efi_uintn_t exit_data_size = 0;
  297. u16 *exit_data = NULL;
  298. u16 *load_options;
  299. /* Transfer environment variable as load options */
  300. ret = set_load_options(handle, "bootargs", &load_options);
  301. if (ret != EFI_SUCCESS)
  302. return ret;
  303. /* Call our payload! */
  304. ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data));
  305. printf("## Application terminated, r = %lu\n", ret & ~EFI_ERROR_MASK);
  306. if (ret && exit_data) {
  307. printf("## %ls\n", exit_data);
  308. efi_free_pool(exit_data);
  309. }
  310. efi_restore_gd();
  311. free(load_options);
  312. return ret;
  313. }
  314. /**
  315. * do_efibootmgr() - execute EFI boot manager
  316. *
  317. * Return: status code
  318. */
  319. static int do_efibootmgr(void)
  320. {
  321. efi_handle_t handle;
  322. efi_status_t ret;
  323. ret = efi_bootmgr_load(&handle);
  324. if (ret != EFI_SUCCESS) {
  325. printf("EFI boot manager: Cannot load any image\n");
  326. return CMD_RET_FAILURE;
  327. }
  328. ret = do_bootefi_exec(handle);
  329. if (ret != EFI_SUCCESS)
  330. return CMD_RET_FAILURE;
  331. return CMD_RET_SUCCESS;
  332. }
  333. /**
  334. * do_bootefi_image() - execute EFI binary
  335. *
  336. * Set up memory image for the binary to be loaded, prepare device path, and
  337. * then call do_bootefi_exec() to execute it.
  338. *
  339. * @image_opt: string of image start address
  340. * Return: status code
  341. */
  342. static int do_bootefi_image(const char *image_opt)
  343. {
  344. void *image_buf;
  345. unsigned long addr, size;
  346. const char *size_str;
  347. efi_status_t ret;
  348. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  349. if (!strcmp(image_opt, "hello")) {
  350. char *saddr;
  351. saddr = env_get("loadaddr");
  352. size = __efi_helloworld_end - __efi_helloworld_begin;
  353. if (saddr)
  354. addr = simple_strtoul(saddr, NULL, 16);
  355. else
  356. addr = CONFIG_SYS_LOAD_ADDR;
  357. image_buf = map_sysmem(addr, size);
  358. memcpy(image_buf, __efi_helloworld_begin, size);
  359. efi_free_pool(bootefi_device_path);
  360. efi_free_pool(bootefi_image_path);
  361. bootefi_device_path = NULL;
  362. bootefi_image_path = NULL;
  363. } else
  364. #endif
  365. {
  366. size_str = env_get("filesize");
  367. if (size_str)
  368. size = simple_strtoul(size_str, NULL, 16);
  369. else
  370. size = 0;
  371. addr = simple_strtoul(image_opt, NULL, 16);
  372. /* Check that a numeric value was passed */
  373. if (!addr && *image_opt != '0')
  374. return CMD_RET_USAGE;
  375. image_buf = map_sysmem(addr, size);
  376. }
  377. ret = efi_run_image(image_buf, size);
  378. if (ret != EFI_SUCCESS)
  379. return CMD_RET_FAILURE;
  380. return CMD_RET_SUCCESS;
  381. }
  382. /**
  383. * efi_run_image() - run loaded UEFI image
  384. *
  385. * @source_buffer: memory address of the UEFI image
  386. * @source_size: size of the UEFI image
  387. * Return: status code
  388. */
  389. efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
  390. {
  391. efi_handle_t mem_handle = NULL, handle;
  392. struct efi_device_path *file_path = NULL;
  393. efi_status_t ret;
  394. if (!bootefi_device_path || !bootefi_image_path) {
  395. /*
  396. * Special case for efi payload not loaded from disk,
  397. * such as 'bootefi hello' or for example payload
  398. * loaded directly into memory via JTAG, etc:
  399. */
  400. file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  401. (uintptr_t)source_buffer,
  402. source_size);
  403. /*
  404. * Make sure that device for device_path exist
  405. * in load_image(). Otherwise, shell and grub will fail.
  406. */
  407. ret = efi_create_handle(&mem_handle);
  408. if (ret != EFI_SUCCESS)
  409. goto out;
  410. ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
  411. file_path);
  412. if (ret != EFI_SUCCESS)
  413. goto out;
  414. } else {
  415. file_path = efi_dp_append(bootefi_device_path,
  416. bootefi_image_path);
  417. }
  418. ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
  419. source_size, &handle));
  420. if (ret != EFI_SUCCESS)
  421. goto out;
  422. ret = do_bootefi_exec(handle);
  423. out:
  424. efi_delete_handle(mem_handle);
  425. efi_free_pool(file_path);
  426. return ret;
  427. }
  428. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  429. static efi_status_t bootefi_run_prepare(const char *load_options_path,
  430. struct efi_device_path *device_path,
  431. struct efi_device_path *image_path,
  432. struct efi_loaded_image_obj **image_objp,
  433. struct efi_loaded_image **loaded_image_infop)
  434. {
  435. efi_status_t ret;
  436. u16 *load_options;
  437. ret = efi_setup_loaded_image(device_path, image_path, image_objp,
  438. loaded_image_infop);
  439. if (ret != EFI_SUCCESS)
  440. return ret;
  441. /* Transfer environment variable as load options */
  442. return set_load_options((efi_handle_t)*image_objp, load_options_path,
  443. &load_options);
  444. }
  445. /**
  446. * bootefi_test_prepare() - prepare to run an EFI test
  447. *
  448. * Prepare to run a test as if it were provided by a loaded image.
  449. *
  450. * @image_objp: pointer to be set to the loaded image handle
  451. * @loaded_image_infop: pointer to be set to the loaded image protocol
  452. * @path: dummy file path used to construct the device path
  453. * set in the loaded image protocol
  454. * @load_options_path: name of a U-Boot environment variable. Its value is
  455. * set as load options in the loaded image protocol.
  456. * Return: status code
  457. */
  458. static efi_status_t bootefi_test_prepare
  459. (struct efi_loaded_image_obj **image_objp,
  460. struct efi_loaded_image **loaded_image_infop, const char *path,
  461. const char *load_options_path)
  462. {
  463. efi_status_t ret;
  464. /* Construct a dummy device path */
  465. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
  466. if (!bootefi_device_path)
  467. return EFI_OUT_OF_RESOURCES;
  468. bootefi_image_path = efi_dp_from_file(NULL, 0, path);
  469. if (!bootefi_image_path) {
  470. ret = EFI_OUT_OF_RESOURCES;
  471. goto failure;
  472. }
  473. ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
  474. bootefi_image_path, image_objp,
  475. loaded_image_infop);
  476. if (ret == EFI_SUCCESS)
  477. return ret;
  478. efi_free_pool(bootefi_image_path);
  479. bootefi_image_path = NULL;
  480. failure:
  481. efi_free_pool(bootefi_device_path);
  482. bootefi_device_path = NULL;
  483. return ret;
  484. }
  485. /**
  486. * bootefi_run_finish() - finish up after running an EFI test
  487. *
  488. * @loaded_image_info: Pointer to a struct which holds the loaded image info
  489. * @image_obj: Pointer to a struct which holds the loaded image object
  490. */
  491. static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
  492. struct efi_loaded_image *loaded_image_info)
  493. {
  494. efi_restore_gd();
  495. free(loaded_image_info->load_options);
  496. efi_delete_handle(&image_obj->header);
  497. }
  498. /**
  499. * do_efi_selftest() - execute EFI selftest
  500. *
  501. * Return: status code
  502. */
  503. static int do_efi_selftest(void)
  504. {
  505. struct efi_loaded_image_obj *image_obj;
  506. struct efi_loaded_image *loaded_image_info;
  507. efi_status_t ret;
  508. ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
  509. "\\selftest", "efi_selftest");
  510. if (ret != EFI_SUCCESS)
  511. return CMD_RET_FAILURE;
  512. /* Execute the test */
  513. ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
  514. bootefi_run_finish(image_obj, loaded_image_info);
  515. return ret != EFI_SUCCESS;
  516. }
  517. #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
  518. /**
  519. * do_bootefi() - execute `bootefi` command
  520. *
  521. * @cmdtp: table entry describing command
  522. * @flag: bitmap indicating how the command was invoked
  523. * @argc: number of arguments
  524. * @argv: command line arguments
  525. * Return: status code
  526. */
  527. static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
  528. 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. }