bootefi.c 18 KB

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