bootefi.c 13 KB

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