bootefi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /* Transfer environment variable bootargs as load options */
  190. set_load_options(&loaded_image_info, "bootargs");
  191. /* Load the EFI payload */
  192. entry = efi_load_pe(efi, &loaded_image_info);
  193. if (!entry) {
  194. ret = -ENOENT;
  195. goto exit;
  196. }
  197. if (memdp) {
  198. struct efi_device_path_memory *mdp = (void *)memdp;
  199. mdp->memory_type = loaded_image_info.image_code_type;
  200. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  201. mdp->end_address = mdp->start_address +
  202. loaded_image_info.image_size;
  203. }
  204. /* we don't support much: */
  205. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  206. "{ro,boot}(blob)0000000000000000");
  207. /* Call our payload! */
  208. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  209. if (setjmp(&loaded_image_info.exit_jmp)) {
  210. ret = loaded_image_info.exit_status;
  211. goto exit;
  212. }
  213. #ifdef CONFIG_ARM64
  214. /* On AArch64 we need to make sure we call our payload in < EL3 */
  215. if (current_el() == 3) {
  216. smp_kick_all_cpus();
  217. dcache_disable(); /* flush cache before switch to EL2 */
  218. /* Move into EL2 and keep running there */
  219. armv8_switch_to_el2((ulong)entry,
  220. (ulong)&loaded_image_info_obj.handle,
  221. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  222. ES_TO_AARCH64);
  223. /* Should never reach here, efi exits with longjmp */
  224. while (1) { }
  225. }
  226. #endif
  227. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  228. exit:
  229. /* image has returned, loaded-image obj goes *poof*: */
  230. list_del(&loaded_image_info_obj.link);
  231. return ret;
  232. }
  233. static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
  234. {
  235. struct efi_device_path *device_path, *file_path;
  236. void *addr;
  237. efi_status_t r;
  238. /* Initialize and populate EFI object list */
  239. if (!efi_obj_list_initalized)
  240. efi_init_obj_list();
  241. /*
  242. * gd lives in a fixed register which may get clobbered while we execute
  243. * the payload. So save it here and restore it on every callback entry
  244. */
  245. efi_save_gd();
  246. addr = efi_bootmgr_load(&device_path, &file_path);
  247. if (!addr)
  248. return 1;
  249. printf("## Starting EFI application at %p ...\n", addr);
  250. r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
  251. printf("## Application terminated, r = %lu\n",
  252. r & ~EFI_ERROR_MASK);
  253. if (r != EFI_SUCCESS)
  254. return 1;
  255. return 0;
  256. }
  257. /* Interpreter command to boot an arbitrary EFI image from memory */
  258. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  259. {
  260. char *saddr, *sfdt;
  261. unsigned long addr, fdt_addr = 0;
  262. efi_status_t r;
  263. if (argc < 2)
  264. return CMD_RET_USAGE;
  265. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  266. if (!strcmp(argv[1], "hello")) {
  267. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  268. saddr = env_get("loadaddr");
  269. if (saddr)
  270. addr = simple_strtoul(saddr, NULL, 16);
  271. else
  272. addr = CONFIG_SYS_LOAD_ADDR;
  273. memcpy((char *)addr, __efi_helloworld_begin, size);
  274. } else
  275. #endif
  276. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  277. if (!strcmp(argv[1], "selftest")) {
  278. struct efi_loaded_image loaded_image_info = {};
  279. struct efi_object loaded_image_info_obj = {};
  280. /* Construct a dummy device path. */
  281. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  282. (uintptr_t)&efi_selftest,
  283. (uintptr_t)&efi_selftest);
  284. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  285. efi_setup_loaded_image(&loaded_image_info,
  286. &loaded_image_info_obj,
  287. bootefi_device_path, bootefi_image_path);
  288. /*
  289. * gd lives in a fixed register which may get clobbered while we
  290. * execute the payload. So save it here and restore it on every
  291. * callback entry
  292. */
  293. efi_save_gd();
  294. /* Initialize and populate EFI object list */
  295. if (!efi_obj_list_initalized)
  296. efi_init_obj_list();
  297. /* Transfer environment variable efi_selftest as load options */
  298. set_load_options(&loaded_image_info, "efi_selftest");
  299. /* Execute the test */
  300. r = efi_selftest(loaded_image_info_obj.handle, &systab);
  301. efi_restore_gd();
  302. free(loaded_image_info.load_options);
  303. list_del(&loaded_image_info_obj.link);
  304. return r != EFI_SUCCESS;
  305. } else
  306. #endif
  307. if (!strcmp(argv[1], "bootmgr")) {
  308. unsigned long fdt_addr = 0;
  309. if (argc > 2)
  310. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  311. return do_bootefi_bootmgr_exec(fdt_addr);
  312. } else {
  313. saddr = argv[1];
  314. addr = simple_strtoul(saddr, NULL, 16);
  315. if (argc > 2) {
  316. sfdt = argv[2];
  317. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  318. }
  319. }
  320. printf("## Starting EFI application at %08lx ...\n", addr);
  321. r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
  322. bootefi_device_path, bootefi_image_path);
  323. printf("## Application terminated, r = %lu\n",
  324. r & ~EFI_ERROR_MASK);
  325. if (r != EFI_SUCCESS)
  326. return 1;
  327. else
  328. return 0;
  329. }
  330. #ifdef CONFIG_SYS_LONGHELP
  331. static char bootefi_help_text[] =
  332. "<image address> [fdt address]\n"
  333. " - boot EFI payload stored at address <image address>.\n"
  334. " If specified, the device tree located at <fdt address> gets\n"
  335. " exposed as EFI configuration table.\n"
  336. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  337. "bootefi hello\n"
  338. " - boot a sample Hello World application stored within U-Boot\n"
  339. #endif
  340. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  341. "bootefi selftest\n"
  342. " - boot an EFI selftest application stored within U-Boot\n"
  343. " Use environment variable efi_selftest to select a single test.\n"
  344. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  345. #endif
  346. "bootmgr [fdt addr]\n"
  347. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  348. "\n"
  349. " If specified, the device tree located at <fdt address> gets\n"
  350. " exposed as EFI configuration table.\n";
  351. #endif
  352. U_BOOT_CMD(
  353. bootefi, 3, 0, do_bootefi,
  354. "Boots an EFI payload from memory",
  355. bootefi_help_text
  356. );
  357. static int parse_partnum(const char *devnr)
  358. {
  359. const char *str = strchr(devnr, ':');
  360. if (str) {
  361. str++;
  362. return simple_strtoul(str, NULL, 16);
  363. }
  364. return 0;
  365. }
  366. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  367. {
  368. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  369. char *s;
  370. if (strcmp(dev, "Net")) {
  371. struct blk_desc *desc;
  372. int part;
  373. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  374. if (!desc)
  375. return;
  376. part = parse_partnum(devnr);
  377. bootefi_device_path = efi_dp_from_part(desc, part);
  378. } else {
  379. #ifdef CONFIG_NET
  380. bootefi_device_path = efi_dp_from_eth();
  381. #endif
  382. }
  383. if (!path)
  384. return;
  385. if (strcmp(dev, "Net")) {
  386. /* Add leading / to fs paths, because they're absolute */
  387. snprintf(filename, sizeof(filename), "/%s", path);
  388. } else {
  389. snprintf(filename, sizeof(filename), "%s", path);
  390. }
  391. /* DOS style file path: */
  392. s = filename;
  393. while ((s = strchr(s, '/')))
  394. *s++ = '\\';
  395. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  396. }