bootefi.c 16 KB

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