bootefi.c 12 KB

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