bootefi.c 15 KB

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