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