bootefi.c 18 KB

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