bootefi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * EFI application loader
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <efi_loader.h>
  12. #include <errno.h>
  13. #include <libfdt.h>
  14. #include <libfdt_env.h>
  15. #include <memalign.h>
  16. #include <asm/global_data.h>
  17. #include <asm-generic/sections.h>
  18. #include <linux/linkage.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. static uint8_t efi_obj_list_initalized;
  21. static struct efi_device_path *bootefi_image_path;
  22. static struct efi_device_path *bootefi_device_path;
  23. /* Initialize and populate EFI object list */
  24. static void efi_init_obj_list(void)
  25. {
  26. efi_obj_list_initalized = 1;
  27. efi_console_register();
  28. #ifdef CONFIG_PARTITIONS
  29. efi_disk_register();
  30. #endif
  31. #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
  32. efi_gop_register();
  33. #endif
  34. #ifdef CONFIG_NET
  35. efi_net_register();
  36. #endif
  37. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  38. efi_smbios_register();
  39. #endif
  40. efi_watchdog_register();
  41. /* Initialize EFI runtime services */
  42. efi_reset_system_init();
  43. efi_get_time_init();
  44. }
  45. static void *copy_fdt(void *fdt)
  46. {
  47. u64 fdt_size = fdt_totalsize(fdt);
  48. unsigned long fdt_ram_start = -1L, fdt_pages;
  49. u64 new_fdt_addr;
  50. void *new_fdt;
  51. int i;
  52. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  53. u64 ram_start = gd->bd->bi_dram[i].start;
  54. u64 ram_size = gd->bd->bi_dram[i].size;
  55. if (!ram_size)
  56. continue;
  57. if (ram_start < fdt_ram_start)
  58. fdt_ram_start = ram_start;
  59. }
  60. /* Give us at least 4kb breathing room */
  61. fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
  62. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  63. /* Safe fdt location is at 128MB */
  64. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  65. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  66. &new_fdt_addr) != EFI_SUCCESS) {
  67. /* If we can't put it there, put it somewhere */
  68. new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
  69. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  70. &new_fdt_addr) != EFI_SUCCESS) {
  71. printf("ERROR: Failed to reserve space for FDT\n");
  72. return NULL;
  73. }
  74. }
  75. new_fdt = (void*)(ulong)new_fdt_addr;
  76. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  77. fdt_set_totalsize(new_fdt, fdt_size);
  78. return new_fdt;
  79. }
  80. static efi_status_t efi_do_enter(
  81. void *image_handle, struct efi_system_table *st,
  82. asmlinkage ulong (*entry)(void *image_handle,
  83. struct efi_system_table *st))
  84. {
  85. efi_status_t ret = EFI_LOAD_ERROR;
  86. if (entry)
  87. ret = entry(image_handle, st);
  88. st->boottime->exit(image_handle, ret, 0, NULL);
  89. return ret;
  90. }
  91. #ifdef CONFIG_ARM64
  92. static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)(
  93. void *image_handle, struct efi_system_table *st),
  94. void *image_handle, struct efi_system_table *st)
  95. {
  96. /* Enable caches again */
  97. dcache_enable();
  98. return efi_do_enter(image_handle, st, entry);
  99. }
  100. #endif
  101. /*
  102. * Load an EFI payload into a newly allocated piece of memory, register all
  103. * EFI objects it would want to access and jump to it.
  104. */
  105. static efi_status_t do_bootefi_exec(void *efi, void *fdt,
  106. struct efi_device_path *device_path,
  107. struct efi_device_path *image_path)
  108. {
  109. struct efi_loaded_image loaded_image_info = {};
  110. struct efi_object loaded_image_info_obj = {};
  111. struct efi_device_path *memdp = NULL;
  112. ulong ret;
  113. ulong (*entry)(void *image_handle, struct efi_system_table *st)
  114. asmlinkage;
  115. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  116. const efi_guid_t fdt_guid = EFI_FDT_GUID;
  117. bootm_headers_t img = { 0 };
  118. /*
  119. * Special case for efi payload not loaded from disk, such as
  120. * 'bootefi hello' or for example payload loaded directly into
  121. * memory via jtag/etc:
  122. */
  123. if (!device_path && !image_path) {
  124. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  125. /* actual addresses filled in after efi_load_pe() */
  126. memdp = efi_dp_from_mem(0, 0, 0);
  127. device_path = image_path = memdp;
  128. } else {
  129. assert(device_path && image_path);
  130. }
  131. /* Initialize and populate EFI object list */
  132. if (!efi_obj_list_initalized)
  133. efi_init_obj_list();
  134. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  135. device_path, image_path);
  136. /*
  137. * gd lives in a fixed register which may get clobbered while we execute
  138. * the payload. So save it here and restore it on every callback entry
  139. */
  140. efi_save_gd();
  141. if (fdt && !fdt_check_header(fdt)) {
  142. /* Prepare fdt for payload */
  143. fdt = copy_fdt(fdt);
  144. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  145. printf("ERROR: Failed to process device tree\n");
  146. return -EINVAL;
  147. }
  148. /* Link to it in the efi tables */
  149. efi_install_configuration_table(&fdt_guid, fdt);
  150. /* And reserve the space in the memory map */
  151. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  152. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  153. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  154. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  155. /* Give a bootloader the chance to modify the device tree */
  156. fdt_pages += 2;
  157. efi_add_memory_map(fdt_start, fdt_pages,
  158. EFI_BOOT_SERVICES_DATA, true);
  159. } else {
  160. printf("WARNING: Invalid device tree, expect boot to fail\n");
  161. efi_install_configuration_table(&fdt_guid, NULL);
  162. }
  163. /* Load the EFI payload */
  164. entry = efi_load_pe(efi, &loaded_image_info);
  165. if (!entry) {
  166. ret = -ENOENT;
  167. goto exit;
  168. }
  169. if (memdp) {
  170. struct efi_device_path_memory *mdp = (void *)memdp;
  171. mdp->memory_type = loaded_image_info.image_code_type;
  172. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  173. mdp->end_address = mdp->start_address +
  174. loaded_image_info.image_size;
  175. }
  176. /* we don't support much: */
  177. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  178. "{ro,boot}(blob)0000000000000000");
  179. /* Call our payload! */
  180. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  181. if (setjmp(&loaded_image_info.exit_jmp)) {
  182. ret = loaded_image_info.exit_status;
  183. goto exit;
  184. }
  185. #ifdef CONFIG_ARM64
  186. /* On AArch64 we need to make sure we call our payload in < EL3 */
  187. if (current_el() == 3) {
  188. smp_kick_all_cpus();
  189. dcache_disable(); /* flush cache before switch to EL2 */
  190. /* Move into EL2 and keep running there */
  191. armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
  192. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  193. ES_TO_AARCH64);
  194. /* Should never reach here, efi exits with longjmp */
  195. while (1) { }
  196. }
  197. #endif
  198. ret = efi_do_enter(&loaded_image_info, &systab, entry);
  199. exit:
  200. /* image has returned, loaded-image obj goes *poof*: */
  201. list_del(&loaded_image_info_obj.link);
  202. return ret;
  203. }
  204. static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
  205. {
  206. struct efi_device_path *device_path, *file_path;
  207. void *addr;
  208. efi_status_t r;
  209. /* Initialize and populate EFI object list */
  210. if (!efi_obj_list_initalized)
  211. efi_init_obj_list();
  212. /*
  213. * gd lives in a fixed register which may get clobbered while we execute
  214. * the payload. So save it here and restore it on every callback entry
  215. */
  216. efi_save_gd();
  217. addr = efi_bootmgr_load(&device_path, &file_path);
  218. if (!addr)
  219. return 1;
  220. printf("## Starting EFI application at %p ...\n", addr);
  221. r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
  222. printf("## Application terminated, r = %lu\n",
  223. r & ~EFI_ERROR_MASK);
  224. if (r != EFI_SUCCESS)
  225. return 1;
  226. return 0;
  227. }
  228. /* Interpreter command to boot an arbitrary EFI image from memory */
  229. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  230. {
  231. char *saddr, *sfdt;
  232. unsigned long addr, fdt_addr = 0;
  233. efi_status_t r;
  234. if (argc < 2)
  235. return CMD_RET_USAGE;
  236. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  237. if (!strcmp(argv[1], "hello")) {
  238. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  239. saddr = env_get("loadaddr");
  240. if (saddr)
  241. addr = simple_strtoul(saddr, NULL, 16);
  242. else
  243. addr = CONFIG_SYS_LOAD_ADDR;
  244. memcpy((char *)addr, __efi_helloworld_begin, size);
  245. } else
  246. #endif
  247. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  248. if (!strcmp(argv[1], "selftest")) {
  249. struct efi_loaded_image loaded_image_info = {};
  250. struct efi_object loaded_image_info_obj = {};
  251. efi_setup_loaded_image(&loaded_image_info,
  252. &loaded_image_info_obj,
  253. bootefi_device_path, bootefi_image_path);
  254. /*
  255. * gd lives in a fixed register which may get clobbered while we
  256. * execute the payload. So save it here and restore it on every
  257. * callback entry
  258. */
  259. efi_save_gd();
  260. /* Initialize and populate EFI object list */
  261. if (!efi_obj_list_initalized)
  262. efi_init_obj_list();
  263. return efi_selftest(&loaded_image_info, &systab);
  264. } else
  265. #endif
  266. if (!strcmp(argv[1], "bootmgr")) {
  267. unsigned long fdt_addr = 0;
  268. if (argc > 2)
  269. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  270. return do_bootefi_bootmgr_exec(fdt_addr);
  271. } else {
  272. saddr = argv[1];
  273. addr = simple_strtoul(saddr, NULL, 16);
  274. if (argc > 2) {
  275. sfdt = argv[2];
  276. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  277. }
  278. }
  279. printf("## Starting EFI application at %08lx ...\n", addr);
  280. r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
  281. bootefi_device_path, bootefi_image_path);
  282. printf("## Application terminated, r = %lu\n",
  283. r & ~EFI_ERROR_MASK);
  284. if (r != EFI_SUCCESS)
  285. return 1;
  286. else
  287. return 0;
  288. }
  289. #ifdef CONFIG_SYS_LONGHELP
  290. static char bootefi_help_text[] =
  291. "<image address> [fdt address]\n"
  292. " - boot EFI payload stored at address <image address>.\n"
  293. " If specified, the device tree located at <fdt address> gets\n"
  294. " exposed as EFI configuration table.\n"
  295. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  296. "bootefi hello\n"
  297. " - boot a sample Hello World application stored within U-Boot\n"
  298. #endif
  299. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  300. "bootefi selftest\n"
  301. " - boot an EFI selftest application stored within U-Boot\n"
  302. #endif
  303. "bootmgr [fdt addr]\n"
  304. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  305. "\n"
  306. " If specified, the device tree located at <fdt address> gets\n"
  307. " exposed as EFI configuration table.\n";
  308. #endif
  309. U_BOOT_CMD(
  310. bootefi, 3, 0, do_bootefi,
  311. "Boots an EFI payload from memory",
  312. bootefi_help_text
  313. );
  314. static int parse_partnum(const char *devnr)
  315. {
  316. const char *str = strchr(devnr, ':');
  317. if (str) {
  318. str++;
  319. return simple_strtoul(str, NULL, 16);
  320. }
  321. return 0;
  322. }
  323. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  324. {
  325. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  326. char *s;
  327. if (strcmp(dev, "Net")) {
  328. struct blk_desc *desc;
  329. int part;
  330. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  331. part = parse_partnum(devnr);
  332. bootefi_device_path = efi_dp_from_part(desc, part);
  333. } else {
  334. #ifdef CONFIG_NET
  335. bootefi_device_path = efi_dp_from_eth();
  336. #endif
  337. }
  338. if (!path)
  339. return;
  340. if (strcmp(dev, "Net")) {
  341. /* Add leading / to fs paths, because they're absolute */
  342. snprintf(filename, sizeof(filename), "/%s", path);
  343. } else {
  344. snprintf(filename, sizeof(filename), "%s", path);
  345. }
  346. /* DOS style file path: */
  347. s = filename;
  348. while ((s = strchr(s, '/')))
  349. *s++ = '\\';
  350. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  351. }