bootefi.c 15 KB

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