bootefi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application loader
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #include <charset.h>
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <efi_loader.h>
  12. #include <efi_selftest.h>
  13. #include <errno.h>
  14. #include <linux/libfdt.h>
  15. #include <linux/libfdt_env.h>
  16. #include <mapmem.h>
  17. #include <memalign.h>
  18. #include <asm/global_data.h>
  19. #include <asm-generic/sections.h>
  20. #include <asm-generic/unaligned.h>
  21. #include <linux/linkage.h>
  22. #ifdef CONFIG_ARMV7_NONSEC
  23. #include <asm/armv7.h>
  24. #include <asm/secure.h>
  25. #endif
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #define OBJ_LIST_NOT_INITIALIZED 1
  28. static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
  29. static struct efi_device_path *bootefi_image_path;
  30. static struct efi_device_path *bootefi_device_path;
  31. /* Initialize and populate EFI object list */
  32. efi_status_t efi_init_obj_list(void)
  33. {
  34. efi_status_t ret = EFI_SUCCESS;
  35. /* Initialize once only */
  36. if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
  37. return efi_obj_list_initialized;
  38. /* Initialize system table */
  39. ret = efi_initialize_system_table();
  40. if (ret != EFI_SUCCESS)
  41. goto out;
  42. /* Initialize EFI driver uclass */
  43. ret = efi_driver_init();
  44. if (ret != EFI_SUCCESS)
  45. goto out;
  46. ret = efi_console_register();
  47. if (ret != EFI_SUCCESS)
  48. goto out;
  49. #ifdef CONFIG_PARTITIONS
  50. ret = efi_disk_register();
  51. if (ret != EFI_SUCCESS)
  52. goto out;
  53. #endif
  54. #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
  55. ret = efi_gop_register();
  56. if (ret != EFI_SUCCESS)
  57. goto out;
  58. #endif
  59. #ifdef CONFIG_NET
  60. ret = efi_net_register();
  61. if (ret != EFI_SUCCESS)
  62. goto out;
  63. #endif
  64. #ifdef CONFIG_GENERATE_ACPI_TABLE
  65. ret = efi_acpi_register();
  66. if (ret != EFI_SUCCESS)
  67. goto out;
  68. #endif
  69. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  70. ret = efi_smbios_register();
  71. if (ret != EFI_SUCCESS)
  72. goto out;
  73. #endif
  74. ret = efi_watchdog_register();
  75. if (ret != EFI_SUCCESS)
  76. goto out;
  77. /* Initialize EFI runtime services */
  78. ret = efi_reset_system_init();
  79. if (ret != EFI_SUCCESS)
  80. goto out;
  81. ret = efi_get_time_init();
  82. if (ret != EFI_SUCCESS)
  83. goto out;
  84. out:
  85. efi_obj_list_initialized = ret;
  86. return ret;
  87. }
  88. /*
  89. * Allow unaligned memory access.
  90. *
  91. * This routine is overridden by architectures providing this feature.
  92. */
  93. void __weak allow_unaligned(void)
  94. {
  95. }
  96. /*
  97. * Set the load options of an image from an environment variable.
  98. *
  99. * @loaded_image_info: the image
  100. * @env_var: name of the environment variable
  101. */
  102. static void set_load_options(struct efi_loaded_image *loaded_image_info,
  103. const char *env_var)
  104. {
  105. size_t size;
  106. const char *env = env_get(env_var);
  107. loaded_image_info->load_options = NULL;
  108. loaded_image_info->load_options_size = 0;
  109. if (!env)
  110. return;
  111. size = strlen(env) + 1;
  112. loaded_image_info->load_options = calloc(size, sizeof(u16));
  113. if (!loaded_image_info->load_options) {
  114. printf("ERROR: Out of memory\n");
  115. return;
  116. }
  117. utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
  118. loaded_image_info->load_options_size = size * 2;
  119. }
  120. static void *copy_fdt(void *fdt)
  121. {
  122. u64 fdt_size = fdt_totalsize(fdt);
  123. unsigned long fdt_ram_start = -1L, fdt_pages;
  124. u64 new_fdt_addr;
  125. void *new_fdt;
  126. int i;
  127. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  128. u64 ram_start = gd->bd->bi_dram[i].start;
  129. u64 ram_size = gd->bd->bi_dram[i].size;
  130. if (!ram_size)
  131. continue;
  132. if (ram_start < fdt_ram_start)
  133. fdt_ram_start = ram_start;
  134. }
  135. /*
  136. * Give us at least 4KB of breathing room in case the device tree needs
  137. * to be expanded later. Round up to the nearest EFI page boundary.
  138. */
  139. fdt_size += 4096;
  140. fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE);
  141. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  142. /* Safe fdt location is at 128MB */
  143. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  144. if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  145. EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  146. &new_fdt_addr) != EFI_SUCCESS) {
  147. /* If we can't put it there, put it somewhere */
  148. new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
  149. if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  150. EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  151. &new_fdt_addr) != EFI_SUCCESS) {
  152. printf("ERROR: Failed to reserve space for FDT\n");
  153. return NULL;
  154. }
  155. }
  156. new_fdt = (void*)(ulong)new_fdt_addr;
  157. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  158. fdt_set_totalsize(new_fdt, fdt_size);
  159. return new_fdt;
  160. }
  161. static efi_status_t efi_do_enter(
  162. efi_handle_t image_handle, struct efi_system_table *st,
  163. EFIAPI efi_status_t (*entry)(
  164. efi_handle_t image_handle,
  165. struct efi_system_table *st))
  166. {
  167. efi_status_t ret = EFI_LOAD_ERROR;
  168. if (entry)
  169. ret = entry(image_handle, st);
  170. st->boottime->exit(image_handle, ret, 0, NULL);
  171. return ret;
  172. }
  173. #ifdef CONFIG_ARM64
  174. static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
  175. efi_handle_t image_handle, struct efi_system_table *st),
  176. efi_handle_t image_handle, struct efi_system_table *st)
  177. {
  178. /* Enable caches again */
  179. dcache_enable();
  180. return efi_do_enter(image_handle, st, entry);
  181. }
  182. #endif
  183. #ifdef CONFIG_ARMV7_NONSEC
  184. static bool is_nonsec;
  185. static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
  186. efi_handle_t image_handle, struct efi_system_table *st),
  187. efi_handle_t image_handle, struct efi_system_table *st)
  188. {
  189. /* Enable caches again */
  190. dcache_enable();
  191. is_nonsec = true;
  192. return efi_do_enter(image_handle, st, entry);
  193. }
  194. #endif
  195. /*
  196. * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
  197. *
  198. * The mem_rsv entries of the FDT are added to the memory map. Any failures are
  199. * ignored because this is not critical and we would rather continue to try to
  200. * boot.
  201. *
  202. * @fdt: Pointer to device tree
  203. */
  204. static void efi_carve_out_dt_rsv(void *fdt)
  205. {
  206. int nr_rsv, i;
  207. uint64_t addr, size, pages;
  208. nr_rsv = fdt_num_mem_rsv(fdt);
  209. /* Look for an existing entry and add it to the efi mem map. */
  210. for (i = 0; i < nr_rsv; i++) {
  211. if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
  212. continue;
  213. pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT;
  214. if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
  215. false))
  216. printf("FDT memrsv map %d: Failed to add to map\n", i);
  217. }
  218. }
  219. static efi_status_t efi_install_fdt(void *fdt)
  220. {
  221. bootm_headers_t img = { 0 };
  222. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  223. efi_status_t ret;
  224. if (fdt_check_header(fdt)) {
  225. printf("ERROR: invalid device tree\n");
  226. return EFI_INVALID_PARAMETER;
  227. }
  228. /* Prepare fdt for payload */
  229. fdt = copy_fdt(fdt);
  230. if (!fdt)
  231. return EFI_OUT_OF_RESOURCES;
  232. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  233. printf("ERROR: failed to process device tree\n");
  234. return EFI_LOAD_ERROR;
  235. }
  236. efi_carve_out_dt_rsv(fdt);
  237. /* Link to it in the efi tables */
  238. ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
  239. if (ret != EFI_SUCCESS)
  240. return EFI_OUT_OF_RESOURCES;
  241. /* And reserve the space in the memory map */
  242. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  243. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  244. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  245. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  246. /* Give a bootloader the chance to modify the device tree */
  247. fdt_pages += 2;
  248. ret = efi_add_memory_map(fdt_start, fdt_pages,
  249. EFI_BOOT_SERVICES_DATA, true);
  250. return ret;
  251. }
  252. /*
  253. * Load an EFI payload into a newly allocated piece of memory, register all
  254. * EFI objects it would want to access and jump to it.
  255. */
  256. static efi_status_t do_bootefi_exec(void *efi,
  257. struct efi_device_path *device_path,
  258. struct efi_device_path *image_path)
  259. {
  260. struct efi_loaded_image loaded_image_info = {};
  261. struct efi_object loaded_image_info_obj = {};
  262. struct efi_object mem_obj = {};
  263. struct efi_device_path *memdp = NULL;
  264. efi_status_t ret;
  265. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  266. struct efi_system_table *st);
  267. /*
  268. * Special case for efi payload not loaded from disk, such as
  269. * 'bootefi hello' or for example payload loaded directly into
  270. * memory via jtag/etc:
  271. */
  272. if (!device_path && !image_path) {
  273. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  274. /* actual addresses filled in after efi_load_pe() */
  275. memdp = efi_dp_from_mem(0, 0, 0);
  276. device_path = image_path = memdp;
  277. efi_add_handle(&mem_obj);
  278. ret = efi_add_protocol(mem_obj.handle, &efi_guid_device_path,
  279. device_path);
  280. if (ret != EFI_SUCCESS)
  281. goto exit;
  282. } else {
  283. assert(device_path && image_path);
  284. }
  285. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  286. device_path, image_path);
  287. /*
  288. * gd lives in a fixed register which may get clobbered while we execute
  289. * the payload. So save it here and restore it on every callback entry
  290. */
  291. efi_save_gd();
  292. /* Transfer environment variable bootargs as load options */
  293. set_load_options(&loaded_image_info, "bootargs");
  294. /* Load the EFI payload */
  295. entry = efi_load_pe(efi, &loaded_image_info);
  296. if (!entry) {
  297. ret = EFI_LOAD_ERROR;
  298. goto exit;
  299. }
  300. if (memdp) {
  301. struct efi_device_path_memory *mdp = (void *)memdp;
  302. mdp->memory_type = loaded_image_info.image_code_type;
  303. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  304. mdp->end_address = mdp->start_address +
  305. loaded_image_info.image_size;
  306. }
  307. /* we don't support much: */
  308. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  309. "{ro,boot}(blob)0000000000000000");
  310. /* Call our payload! */
  311. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  312. if (setjmp(&loaded_image_info.exit_jmp)) {
  313. ret = loaded_image_info.exit_status;
  314. goto exit;
  315. }
  316. #ifdef CONFIG_ARM64
  317. /* On AArch64 we need to make sure we call our payload in < EL3 */
  318. if (current_el() == 3) {
  319. smp_kick_all_cpus();
  320. dcache_disable(); /* flush cache before switch to EL2 */
  321. /* Move into EL2 and keep running there */
  322. armv8_switch_to_el2((ulong)entry,
  323. (ulong)&loaded_image_info_obj.handle,
  324. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  325. ES_TO_AARCH64);
  326. /* Should never reach here, efi exits with longjmp */
  327. while (1) { }
  328. }
  329. #endif
  330. #ifdef CONFIG_ARMV7_NONSEC
  331. if (armv7_boot_nonsec() && !is_nonsec) {
  332. dcache_disable(); /* flush cache before switch to HYP */
  333. armv7_init_nonsec();
  334. secure_ram_addr(_do_nonsec_entry)(
  335. efi_run_in_hyp,
  336. (uintptr_t)entry,
  337. (uintptr_t)loaded_image_info_obj.handle,
  338. (uintptr_t)&systab);
  339. /* Should never reach here, efi exits with longjmp */
  340. while (1) { }
  341. }
  342. #endif
  343. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  344. exit:
  345. /* image has returned, loaded-image obj goes *poof*: */
  346. list_del(&loaded_image_info_obj.link);
  347. if (mem_obj.handle)
  348. list_del(&mem_obj.link);
  349. return ret;
  350. }
  351. static int do_bootefi_bootmgr_exec(void)
  352. {
  353. struct efi_device_path *device_path, *file_path;
  354. void *addr;
  355. efi_status_t r;
  356. /*
  357. * gd lives in a fixed register which may get clobbered while we execute
  358. * the payload. So save it here and restore it on every callback entry
  359. */
  360. efi_save_gd();
  361. addr = efi_bootmgr_load(&device_path, &file_path);
  362. if (!addr)
  363. return 1;
  364. printf("## Starting EFI application at %p ...\n", addr);
  365. r = do_bootefi_exec(addr, device_path, file_path);
  366. printf("## Application terminated, r = %lu\n",
  367. r & ~EFI_ERROR_MASK);
  368. if (r != EFI_SUCCESS)
  369. return 1;
  370. return 0;
  371. }
  372. /* Interpreter command to boot an arbitrary EFI image from memory */
  373. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  374. {
  375. unsigned long addr;
  376. char *saddr;
  377. efi_status_t r;
  378. unsigned long fdt_addr;
  379. void *fdt;
  380. /* Allow unaligned memory access */
  381. allow_unaligned();
  382. /* Initialize EFI drivers */
  383. r = efi_init_obj_list();
  384. if (r != EFI_SUCCESS) {
  385. printf("Error: Cannot set up EFI drivers, r = %lu\n",
  386. r & ~EFI_ERROR_MASK);
  387. return CMD_RET_FAILURE;
  388. }
  389. if (argc < 2)
  390. return CMD_RET_USAGE;
  391. if (argc > 2) {
  392. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  393. if (!fdt_addr && *argv[2] != '0')
  394. return CMD_RET_USAGE;
  395. /* Install device tree */
  396. fdt = map_sysmem(fdt_addr, 0);
  397. r = efi_install_fdt(fdt);
  398. if (r != EFI_SUCCESS) {
  399. printf("ERROR: failed to install device tree\n");
  400. return CMD_RET_FAILURE;
  401. }
  402. } else {
  403. /* Remove device tree. EFI_NOT_FOUND can be ignored here */
  404. efi_install_configuration_table(&efi_guid_fdt, NULL);
  405. printf("WARNING: booting without device tree\n");
  406. }
  407. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  408. if (!strcmp(argv[1], "hello")) {
  409. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  410. saddr = env_get("loadaddr");
  411. if (saddr)
  412. addr = simple_strtoul(saddr, NULL, 16);
  413. else
  414. addr = CONFIG_SYS_LOAD_ADDR;
  415. memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
  416. } else
  417. #endif
  418. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  419. if (!strcmp(argv[1], "selftest")) {
  420. struct efi_loaded_image loaded_image_info = {};
  421. struct efi_object loaded_image_info_obj = {};
  422. /* Construct a dummy device path. */
  423. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  424. (uintptr_t)&efi_selftest,
  425. (uintptr_t)&efi_selftest);
  426. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  427. efi_setup_loaded_image(&loaded_image_info,
  428. &loaded_image_info_obj,
  429. bootefi_device_path, bootefi_image_path);
  430. /*
  431. * gd lives in a fixed register which may get clobbered while we
  432. * execute the payload. So save it here and restore it on every
  433. * callback entry
  434. */
  435. efi_save_gd();
  436. /* Transfer environment variable efi_selftest as load options */
  437. set_load_options(&loaded_image_info, "efi_selftest");
  438. /* Execute the test */
  439. r = efi_selftest(loaded_image_info_obj.handle, &systab);
  440. efi_restore_gd();
  441. free(loaded_image_info.load_options);
  442. list_del(&loaded_image_info_obj.link);
  443. return r != EFI_SUCCESS;
  444. } else
  445. #endif
  446. if (!strcmp(argv[1], "bootmgr")) {
  447. return do_bootefi_bootmgr_exec();
  448. } else {
  449. saddr = argv[1];
  450. addr = simple_strtoul(saddr, NULL, 16);
  451. /* Check that a numeric value was passed */
  452. if (!addr && *saddr != '0')
  453. return CMD_RET_USAGE;
  454. }
  455. printf("## Starting EFI application at %08lx ...\n", addr);
  456. r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
  457. bootefi_image_path);
  458. printf("## Application terminated, r = %lu\n",
  459. r & ~EFI_ERROR_MASK);
  460. if (r != EFI_SUCCESS)
  461. return 1;
  462. else
  463. return 0;
  464. }
  465. #ifdef CONFIG_SYS_LONGHELP
  466. static char bootefi_help_text[] =
  467. "<image address> [fdt address]\n"
  468. " - boot EFI payload stored at address <image address>.\n"
  469. " If specified, the device tree located at <fdt address> gets\n"
  470. " exposed as EFI configuration table.\n"
  471. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  472. "bootefi hello\n"
  473. " - boot a sample Hello World application stored within U-Boot\n"
  474. #endif
  475. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  476. "bootefi selftest [fdt address]\n"
  477. " - boot an EFI selftest application stored within U-Boot\n"
  478. " Use environment variable efi_selftest to select a single test.\n"
  479. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  480. #endif
  481. "bootefi bootmgr [fdt addr]\n"
  482. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  483. "\n"
  484. " If specified, the device tree located at <fdt address> gets\n"
  485. " exposed as EFI configuration table.\n";
  486. #endif
  487. U_BOOT_CMD(
  488. bootefi, 3, 0, do_bootefi,
  489. "Boots an EFI payload from memory",
  490. bootefi_help_text
  491. );
  492. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  493. {
  494. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  495. char *s;
  496. if (strcmp(dev, "Net")) {
  497. struct blk_desc *desc;
  498. disk_partition_t fs_partition;
  499. int part;
  500. part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
  501. 1);
  502. if (part < 0)
  503. return;
  504. bootefi_device_path = efi_dp_from_part(desc, part);
  505. } else {
  506. #ifdef CONFIG_NET
  507. bootefi_device_path = efi_dp_from_eth();
  508. #endif
  509. }
  510. if (!path)
  511. return;
  512. if (strcmp(dev, "Net")) {
  513. /* Add leading / to fs paths, because they're absolute */
  514. snprintf(filename, sizeof(filename), "/%s", path);
  515. } else {
  516. snprintf(filename, sizeof(filename), "%s", path);
  517. }
  518. /* DOS style file path: */
  519. s = filename;
  520. while ((s = strchr(s, '/')))
  521. *s++ = '\\';
  522. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  523. }