bootefi.c 14 KB

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