bootefi.c 15 KB

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