bootefi.c 11 KB

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