bootefi.c 19 KB

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