bootefi.c 12 KB

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