bootefi.c 17 KB

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