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 <linux/libfdt.h>
  16. #include <linux/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 u8 efi_obj_list_initialized;
  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. /* Initialize once only */
  29. if (efi_obj_list_initialized)
  30. return;
  31. efi_obj_list_initialized = 1;
  32. /* Initialize EFI driver uclass */
  33. efi_driver_init();
  34. efi_console_register();
  35. #ifdef CONFIG_PARTITIONS
  36. efi_disk_register();
  37. #endif
  38. #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
  39. efi_gop_register();
  40. #endif
  41. #ifdef CONFIG_CMD_NET
  42. efi_net_register();
  43. #endif
  44. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  45. efi_smbios_register();
  46. #endif
  47. efi_watchdog_register();
  48. /* Initialize EFI runtime services */
  49. efi_reset_system_init();
  50. efi_get_time_init();
  51. }
  52. /*
  53. * Set the load options of an image from an environment variable.
  54. *
  55. * @loaded_image_info: the image
  56. * @env_var: name of the environment variable
  57. */
  58. static void set_load_options(struct efi_loaded_image *loaded_image_info,
  59. const char *env_var)
  60. {
  61. size_t size;
  62. const char *env = env_get(env_var);
  63. loaded_image_info->load_options = NULL;
  64. loaded_image_info->load_options_size = 0;
  65. if (!env)
  66. return;
  67. size = strlen(env) + 1;
  68. loaded_image_info->load_options = calloc(size, sizeof(u16));
  69. if (!loaded_image_info->load_options) {
  70. printf("ERROR: Out of memory\n");
  71. return;
  72. }
  73. utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
  74. loaded_image_info->load_options_size = size * 2;
  75. }
  76. static void *copy_fdt(void *fdt)
  77. {
  78. u64 fdt_size = fdt_totalsize(fdt);
  79. unsigned long fdt_ram_start = -1L, fdt_pages;
  80. u64 new_fdt_addr;
  81. void *new_fdt;
  82. int i;
  83. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  84. u64 ram_start = gd->bd->bi_dram[i].start;
  85. u64 ram_size = gd->bd->bi_dram[i].size;
  86. if (!ram_size)
  87. continue;
  88. if (ram_start < fdt_ram_start)
  89. fdt_ram_start = ram_start;
  90. }
  91. /* Give us at least 4kb breathing room */
  92. fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
  93. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  94. /* Safe fdt location is at 128MB */
  95. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  96. if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  97. &new_fdt_addr) != EFI_SUCCESS) {
  98. /* If we can't put it there, put it somewhere */
  99. new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
  100. if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  101. &new_fdt_addr) != EFI_SUCCESS) {
  102. printf("ERROR: Failed to reserve space for FDT\n");
  103. return NULL;
  104. }
  105. }
  106. new_fdt = (void*)(ulong)new_fdt_addr;
  107. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  108. fdt_set_totalsize(new_fdt, fdt_size);
  109. return new_fdt;
  110. }
  111. static efi_status_t efi_do_enter(
  112. efi_handle_t image_handle, struct efi_system_table *st,
  113. EFIAPI efi_status_t (*entry)(
  114. efi_handle_t image_handle,
  115. struct efi_system_table *st))
  116. {
  117. efi_status_t ret = EFI_LOAD_ERROR;
  118. if (entry)
  119. ret = entry(image_handle, st);
  120. st->boottime->exit(image_handle, ret, 0, NULL);
  121. return ret;
  122. }
  123. #ifdef CONFIG_ARM64
  124. static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
  125. efi_handle_t image_handle, struct efi_system_table *st),
  126. efi_handle_t image_handle, struct efi_system_table *st)
  127. {
  128. /* Enable caches again */
  129. dcache_enable();
  130. return efi_do_enter(image_handle, st, entry);
  131. }
  132. #endif
  133. /*
  134. * Load an EFI payload into a newly allocated piece of memory, register all
  135. * EFI objects it would want to access and jump to it.
  136. */
  137. static efi_status_t do_bootefi_exec(void *efi, void *fdt,
  138. struct efi_device_path *device_path,
  139. struct efi_device_path *image_path)
  140. {
  141. struct efi_loaded_image loaded_image_info = {};
  142. struct efi_object loaded_image_info_obj = {};
  143. struct efi_device_path *memdp = NULL;
  144. ulong ret;
  145. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  146. struct efi_system_table *st);
  147. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  148. const efi_guid_t fdt_guid = EFI_FDT_GUID;
  149. bootm_headers_t img = { 0 };
  150. /*
  151. * Special case for efi payload not loaded from disk, such as
  152. * 'bootefi hello' or for example payload loaded directly into
  153. * memory via jtag/etc:
  154. */
  155. if (!device_path && !image_path) {
  156. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  157. /* actual addresses filled in after efi_load_pe() */
  158. memdp = efi_dp_from_mem(0, 0, 0);
  159. device_path = image_path = memdp;
  160. } else {
  161. assert(device_path && image_path);
  162. }
  163. /* Initialize and populate EFI object list */
  164. efi_init_obj_list();
  165. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  166. device_path, image_path);
  167. /*
  168. * gd lives in a fixed register which may get clobbered while we execute
  169. * the payload. So save it here and restore it on every callback entry
  170. */
  171. efi_save_gd();
  172. if (fdt && !fdt_check_header(fdt)) {
  173. /* Prepare fdt for payload */
  174. fdt = copy_fdt(fdt);
  175. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  176. printf("ERROR: Failed to process device tree\n");
  177. return -EINVAL;
  178. }
  179. /* Link to it in the efi tables */
  180. efi_install_configuration_table(&fdt_guid, fdt);
  181. /* And reserve the space in the memory map */
  182. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  183. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  184. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  185. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  186. /* Give a bootloader the chance to modify the device tree */
  187. fdt_pages += 2;
  188. efi_add_memory_map(fdt_start, fdt_pages,
  189. EFI_BOOT_SERVICES_DATA, true);
  190. } else {
  191. printf("WARNING: Invalid device tree, expect boot to fail\n");
  192. efi_install_configuration_table(&fdt_guid, NULL);
  193. }
  194. /* Transfer environment variable bootargs as load options */
  195. set_load_options(&loaded_image_info, "bootargs");
  196. /* Load the EFI payload */
  197. entry = efi_load_pe(efi, &loaded_image_info);
  198. if (!entry) {
  199. ret = -ENOENT;
  200. goto exit;
  201. }
  202. if (memdp) {
  203. struct efi_device_path_memory *mdp = (void *)memdp;
  204. mdp->memory_type = loaded_image_info.image_code_type;
  205. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  206. mdp->end_address = mdp->start_address +
  207. loaded_image_info.image_size;
  208. }
  209. /* we don't support much: */
  210. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  211. "{ro,boot}(blob)0000000000000000");
  212. /* Call our payload! */
  213. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  214. if (setjmp(&loaded_image_info.exit_jmp)) {
  215. ret = loaded_image_info.exit_status;
  216. goto exit;
  217. }
  218. #ifdef CONFIG_ARM64
  219. /* On AArch64 we need to make sure we call our payload in < EL3 */
  220. if (current_el() == 3) {
  221. smp_kick_all_cpus();
  222. dcache_disable(); /* flush cache before switch to EL2 */
  223. /* Move into EL2 and keep running there */
  224. armv8_switch_to_el2((ulong)entry,
  225. (ulong)&loaded_image_info_obj.handle,
  226. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  227. ES_TO_AARCH64);
  228. /* Should never reach here, efi exits with longjmp */
  229. while (1) { }
  230. }
  231. #endif
  232. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  233. exit:
  234. /* image has returned, loaded-image obj goes *poof*: */
  235. list_del(&loaded_image_info_obj.link);
  236. return ret;
  237. }
  238. static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
  239. {
  240. struct efi_device_path *device_path, *file_path;
  241. void *addr;
  242. efi_status_t r;
  243. /* Initialize and populate EFI object list */
  244. efi_init_obj_list();
  245. /*
  246. * gd lives in a fixed register which may get clobbered while we execute
  247. * the payload. So save it here and restore it on every callback entry
  248. */
  249. efi_save_gd();
  250. addr = efi_bootmgr_load(&device_path, &file_path);
  251. if (!addr)
  252. return 1;
  253. printf("## Starting EFI application at %p ...\n", addr);
  254. r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
  255. printf("## Application terminated, r = %lu\n",
  256. r & ~EFI_ERROR_MASK);
  257. if (r != EFI_SUCCESS)
  258. return 1;
  259. return 0;
  260. }
  261. /* Interpreter command to boot an arbitrary EFI image from memory */
  262. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  263. {
  264. char *saddr, *sfdt;
  265. unsigned long addr, fdt_addr = 0;
  266. efi_status_t r;
  267. if (argc < 2)
  268. return CMD_RET_USAGE;
  269. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  270. if (!strcmp(argv[1], "hello")) {
  271. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  272. saddr = env_get("loadaddr");
  273. if (saddr)
  274. addr = simple_strtoul(saddr, NULL, 16);
  275. else
  276. addr = CONFIG_SYS_LOAD_ADDR;
  277. memcpy((char *)addr, __efi_helloworld_begin, size);
  278. } else
  279. #endif
  280. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  281. if (!strcmp(argv[1], "selftest")) {
  282. struct efi_loaded_image loaded_image_info = {};
  283. struct efi_object loaded_image_info_obj = {};
  284. /* Construct a dummy device path. */
  285. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  286. (uintptr_t)&efi_selftest,
  287. (uintptr_t)&efi_selftest);
  288. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  289. efi_setup_loaded_image(&loaded_image_info,
  290. &loaded_image_info_obj,
  291. bootefi_device_path, bootefi_image_path);
  292. /*
  293. * gd lives in a fixed register which may get clobbered while we
  294. * execute the payload. So save it here and restore it on every
  295. * callback entry
  296. */
  297. efi_save_gd();
  298. /* Initialize and populate EFI object list */
  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_CMD_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. }