bootefi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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_object mem_obj = {};
  228. struct efi_device_path *memdp = NULL;
  229. efi_status_t ret;
  230. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  231. struct efi_system_table *st);
  232. /*
  233. * Special case for efi payload not loaded from disk, such as
  234. * 'bootefi hello' or for example payload loaded directly into
  235. * memory via jtag/etc:
  236. */
  237. if (!device_path && !image_path) {
  238. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  239. /* actual addresses filled in after efi_load_pe() */
  240. memdp = efi_dp_from_mem(0, 0, 0);
  241. device_path = image_path = memdp;
  242. efi_add_handle(&mem_obj);
  243. ret = efi_add_protocol(mem_obj.handle, &efi_guid_device_path,
  244. device_path);
  245. if (ret != EFI_SUCCESS)
  246. goto exit;
  247. } else {
  248. assert(device_path && image_path);
  249. }
  250. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  251. device_path, image_path);
  252. /*
  253. * gd lives in a fixed register which may get clobbered while we execute
  254. * the payload. So save it here and restore it on every callback entry
  255. */
  256. efi_save_gd();
  257. /* Transfer environment variable bootargs as load options */
  258. set_load_options(&loaded_image_info, "bootargs");
  259. /* Load the EFI payload */
  260. entry = efi_load_pe(efi, &loaded_image_info);
  261. if (!entry) {
  262. ret = EFI_LOAD_ERROR;
  263. goto exit;
  264. }
  265. if (memdp) {
  266. struct efi_device_path_memory *mdp = (void *)memdp;
  267. mdp->memory_type = loaded_image_info.image_code_type;
  268. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  269. mdp->end_address = mdp->start_address +
  270. loaded_image_info.image_size;
  271. }
  272. /* we don't support much: */
  273. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  274. "{ro,boot}(blob)0000000000000000");
  275. /* Call our payload! */
  276. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  277. if (setjmp(&loaded_image_info.exit_jmp)) {
  278. ret = loaded_image_info.exit_status;
  279. goto exit;
  280. }
  281. #ifdef CONFIG_ARM64
  282. /* On AArch64 we need to make sure we call our payload in < EL3 */
  283. if (current_el() == 3) {
  284. smp_kick_all_cpus();
  285. dcache_disable(); /* flush cache before switch to EL2 */
  286. /* Move into EL2 and keep running there */
  287. armv8_switch_to_el2((ulong)entry,
  288. (ulong)&loaded_image_info_obj.handle,
  289. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  290. ES_TO_AARCH64);
  291. /* Should never reach here, efi exits with longjmp */
  292. while (1) { }
  293. }
  294. #endif
  295. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  296. exit:
  297. /* image has returned, loaded-image obj goes *poof*: */
  298. list_del(&loaded_image_info_obj.link);
  299. if (mem_obj.handle)
  300. list_del(&mem_obj.link);
  301. return ret;
  302. }
  303. static int do_bootefi_bootmgr_exec(void)
  304. {
  305. struct efi_device_path *device_path, *file_path;
  306. void *addr;
  307. efi_status_t r;
  308. /*
  309. * gd lives in a fixed register which may get clobbered while we execute
  310. * the payload. So save it here and restore it on every callback entry
  311. */
  312. efi_save_gd();
  313. addr = efi_bootmgr_load(&device_path, &file_path);
  314. if (!addr)
  315. return 1;
  316. printf("## Starting EFI application at %p ...\n", addr);
  317. r = do_bootefi_exec(addr, device_path, file_path);
  318. printf("## Application terminated, r = %lu\n",
  319. r & ~EFI_ERROR_MASK);
  320. if (r != EFI_SUCCESS)
  321. return 1;
  322. return 0;
  323. }
  324. /* Interpreter command to boot an arbitrary EFI image from memory */
  325. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  326. {
  327. unsigned long addr;
  328. char *saddr;
  329. efi_status_t r;
  330. void *fdt_addr;
  331. /* Allow unaligned memory access */
  332. allow_unaligned();
  333. /* Initialize EFI drivers */
  334. r = efi_init_obj_list();
  335. if (r != EFI_SUCCESS) {
  336. printf("Error: Cannot set up EFI drivers, r = %lu\n",
  337. r & ~EFI_ERROR_MASK);
  338. return CMD_RET_FAILURE;
  339. }
  340. if (argc < 2)
  341. return CMD_RET_USAGE;
  342. if (argc > 2) {
  343. fdt_addr = (void *)simple_strtoul(argv[2], NULL, 16);
  344. if (!fdt_addr && *argv[2] != '0')
  345. return CMD_RET_USAGE;
  346. /* Install device tree */
  347. r = efi_install_fdt(fdt_addr);
  348. if (r != EFI_SUCCESS) {
  349. printf("ERROR: failed to install device tree\n");
  350. return CMD_RET_FAILURE;
  351. }
  352. } else {
  353. /* Remove device tree. EFI_NOT_FOUND can be ignored here */
  354. efi_install_configuration_table(&efi_guid_fdt, NULL);
  355. printf("WARNING: booting without device tree\n");
  356. }
  357. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  358. if (!strcmp(argv[1], "hello")) {
  359. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  360. saddr = env_get("loadaddr");
  361. if (saddr)
  362. addr = simple_strtoul(saddr, NULL, 16);
  363. else
  364. addr = CONFIG_SYS_LOAD_ADDR;
  365. memcpy((char *)addr, __efi_helloworld_begin, size);
  366. } else
  367. #endif
  368. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  369. if (!strcmp(argv[1], "selftest")) {
  370. struct efi_loaded_image loaded_image_info = {};
  371. struct efi_object loaded_image_info_obj = {};
  372. /* Construct a dummy device path. */
  373. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  374. (uintptr_t)&efi_selftest,
  375. (uintptr_t)&efi_selftest);
  376. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  377. efi_setup_loaded_image(&loaded_image_info,
  378. &loaded_image_info_obj,
  379. bootefi_device_path, bootefi_image_path);
  380. /*
  381. * gd lives in a fixed register which may get clobbered while we
  382. * execute the payload. So save it here and restore it on every
  383. * callback entry
  384. */
  385. efi_save_gd();
  386. /* Transfer environment variable efi_selftest as load options */
  387. set_load_options(&loaded_image_info, "efi_selftest");
  388. /* Execute the test */
  389. r = efi_selftest(loaded_image_info_obj.handle, &systab);
  390. efi_restore_gd();
  391. free(loaded_image_info.load_options);
  392. list_del(&loaded_image_info_obj.link);
  393. return r != EFI_SUCCESS;
  394. } else
  395. #endif
  396. if (!strcmp(argv[1], "bootmgr")) {
  397. return do_bootefi_bootmgr_exec();
  398. } else {
  399. saddr = argv[1];
  400. addr = simple_strtoul(saddr, NULL, 16);
  401. /* Check that a numeric value was passed */
  402. if (!addr && *saddr != '0')
  403. return CMD_RET_USAGE;
  404. }
  405. printf("## Starting EFI application at %08lx ...\n", addr);
  406. r = do_bootefi_exec((void *)addr, bootefi_device_path,
  407. bootefi_image_path);
  408. printf("## Application terminated, r = %lu\n",
  409. r & ~EFI_ERROR_MASK);
  410. if (r != EFI_SUCCESS)
  411. return 1;
  412. else
  413. return 0;
  414. }
  415. #ifdef CONFIG_SYS_LONGHELP
  416. static char bootefi_help_text[] =
  417. "<image address> [fdt address]\n"
  418. " - boot EFI payload stored at address <image address>.\n"
  419. " If specified, the device tree located at <fdt address> gets\n"
  420. " exposed as EFI configuration table.\n"
  421. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  422. "bootefi hello\n"
  423. " - boot a sample Hello World application stored within U-Boot\n"
  424. #endif
  425. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  426. "bootefi selftest [fdt address]\n"
  427. " - boot an EFI selftest application stored within U-Boot\n"
  428. " Use environment variable efi_selftest to select a single test.\n"
  429. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  430. #endif
  431. "bootefi bootmgr [fdt addr]\n"
  432. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  433. "\n"
  434. " If specified, the device tree located at <fdt address> gets\n"
  435. " exposed as EFI configuration table.\n";
  436. #endif
  437. U_BOOT_CMD(
  438. bootefi, 3, 0, do_bootefi,
  439. "Boots an EFI payload from memory",
  440. bootefi_help_text
  441. );
  442. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  443. {
  444. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  445. char *s;
  446. if (strcmp(dev, "Net")) {
  447. struct blk_desc *desc;
  448. disk_partition_t fs_partition;
  449. int part;
  450. part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
  451. 1);
  452. if (part < 0)
  453. return;
  454. bootefi_device_path = efi_dp_from_part(desc, part);
  455. } else {
  456. #ifdef CONFIG_NET
  457. bootefi_device_path = efi_dp_from_eth();
  458. #endif
  459. }
  460. if (!path)
  461. return;
  462. if (strcmp(dev, "Net")) {
  463. /* Add leading / to fs paths, because they're absolute */
  464. snprintf(filename, sizeof(filename), "/%s", path);
  465. } else {
  466. snprintf(filename, sizeof(filename), "%s", path);
  467. }
  468. /* DOS style file path: */
  469. s = filename;
  470. while ((s = strchr(s, '/')))
  471. *s++ = '\\';
  472. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  473. }