bootefi.c 14 KB

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