bootefi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * EFI application loader
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <efi_loader.h>
  12. #include <errno.h>
  13. #include <libfdt.h>
  14. #include <libfdt_env.h>
  15. #include <memalign.h>
  16. #include <asm/global_data.h>
  17. #include <asm-generic/sections.h>
  18. #include <linux/linkage.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * When booting using the "bootefi" command, we don't know which
  22. * physical device the file came from. So we create a pseudo-device
  23. * called "bootefi" with the device path /bootefi.
  24. *
  25. * In addition to the originating device we also declare the file path
  26. * of "bootefi" based loads to be /bootefi.
  27. */
  28. static struct efi_device_path_file_path bootefi_image_path[] = {
  29. {
  30. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  31. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  32. .dp.length = sizeof(bootefi_image_path[0]),
  33. .str = { 'b','o','o','t','e','f','i' },
  34. }, {
  35. .dp.type = DEVICE_PATH_TYPE_END,
  36. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  37. .dp.length = sizeof(bootefi_image_path[0]),
  38. }
  39. };
  40. static struct efi_device_path_file_path bootefi_device_path[] = {
  41. {
  42. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  43. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  44. .dp.length = sizeof(bootefi_image_path[0]),
  45. .str = { 'b','o','o','t','e','f','i' },
  46. }, {
  47. .dp.type = DEVICE_PATH_TYPE_END,
  48. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  49. .dp.length = sizeof(bootefi_image_path[0]),
  50. }
  51. };
  52. /* The EFI loaded_image interface for the image executed via "bootefi" */
  53. static struct efi_loaded_image loaded_image_info = {
  54. .device_handle = bootefi_device_path,
  55. .file_path = bootefi_image_path,
  56. };
  57. /* The EFI object struct for the image executed via "bootefi" */
  58. static struct efi_object loaded_image_info_obj = {
  59. .handle = &loaded_image_info,
  60. .protocols = {
  61. {
  62. /*
  63. * When asking for the loaded_image interface, just
  64. * return handle which points to loaded_image_info
  65. */
  66. .guid = &efi_guid_loaded_image,
  67. .protocol_interface = &loaded_image_info,
  68. },
  69. {
  70. /*
  71. * When asking for the device path interface, return
  72. * bootefi_device_path
  73. */
  74. .guid = &efi_guid_device_path,
  75. .protocol_interface = bootefi_device_path,
  76. },
  77. {
  78. .guid = &efi_guid_console_control,
  79. .protocol_interface = (void *) &efi_console_control
  80. },
  81. {
  82. .guid = &efi_guid_device_path_to_text_protocol,
  83. .protocol_interface = (void *) &efi_device_path_to_text
  84. },
  85. },
  86. };
  87. /* The EFI object struct for the device the "bootefi" image was loaded from */
  88. static struct efi_object bootefi_device_obj = {
  89. .handle = bootefi_device_path,
  90. .protocols = {
  91. {
  92. /* When asking for the device path interface, return
  93. * bootefi_device_path */
  94. .guid = &efi_guid_device_path,
  95. .protocol_interface = bootefi_device_path
  96. }
  97. },
  98. };
  99. static void *copy_fdt(void *fdt)
  100. {
  101. u64 fdt_size = fdt_totalsize(fdt);
  102. unsigned long fdt_ram_start = -1L, fdt_pages;
  103. u64 new_fdt_addr;
  104. void *new_fdt;
  105. int i;
  106. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  107. u64 ram_start = gd->bd->bi_dram[i].start;
  108. u64 ram_size = gd->bd->bi_dram[i].size;
  109. if (!ram_size)
  110. continue;
  111. if (ram_start < fdt_ram_start)
  112. fdt_ram_start = ram_start;
  113. }
  114. /* Give us at least 4kb breathing room */
  115. fdt_size = ALIGN(fdt_size + 4096, 4096);
  116. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  117. /* Safe fdt location is at 128MB */
  118. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  119. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  120. &new_fdt_addr) != EFI_SUCCESS) {
  121. /* If we can't put it there, put it somewhere */
  122. new_fdt_addr = (ulong)memalign(4096, fdt_size);
  123. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  124. &new_fdt_addr) != EFI_SUCCESS) {
  125. printf("ERROR: Failed to reserve space for FDT\n");
  126. return NULL;
  127. }
  128. }
  129. new_fdt = (void*)(ulong)new_fdt_addr;
  130. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  131. fdt_set_totalsize(new_fdt, fdt_size);
  132. return new_fdt;
  133. }
  134. static ulong efi_do_enter(void *image_handle,
  135. struct efi_system_table *st,
  136. asmlinkage ulong (*entry)(void *image_handle,
  137. struct efi_system_table *st))
  138. {
  139. efi_status_t ret = EFI_LOAD_ERROR;
  140. if (entry)
  141. ret = entry(image_handle, st);
  142. st->boottime->exit(image_handle, ret, 0, NULL);
  143. return ret;
  144. }
  145. #ifdef CONFIG_ARM64
  146. static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)(
  147. void *image_handle, struct efi_system_table *st),
  148. void *image_handle, struct efi_system_table *st)
  149. {
  150. /* Enable caches again */
  151. dcache_enable();
  152. return efi_do_enter(image_handle, st, entry);
  153. }
  154. #endif
  155. /*
  156. * Load an EFI payload into a newly allocated piece of memory, register all
  157. * EFI objects it would want to access and jump to it.
  158. */
  159. static unsigned long do_bootefi_exec(void *efi, void *fdt)
  160. {
  161. ulong (*entry)(void *image_handle, struct efi_system_table *st)
  162. asmlinkage;
  163. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  164. bootm_headers_t img = { 0 };
  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. systab.tables[0].guid = EFI_FDT_GUID;
  179. systab.tables[0].table = fdt;
  180. systab.nr_tables = 1;
  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. systab.nr_tables = 0;
  193. }
  194. /* Load the EFI payload */
  195. entry = efi_load_pe(efi, &loaded_image_info);
  196. if (!entry)
  197. return -ENOENT;
  198. /* Initialize and populate EFI object list */
  199. INIT_LIST_HEAD(&efi_obj_list);
  200. list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
  201. list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
  202. #ifdef CONFIG_PARTITIONS
  203. efi_disk_register();
  204. #endif
  205. #ifdef CONFIG_LCD
  206. efi_gop_register();
  207. #endif
  208. #ifdef CONFIG_NET
  209. void *nethandle = loaded_image_info.device_handle;
  210. efi_net_register(&nethandle);
  211. if (!memcmp(bootefi_device_path[0].str, "N\0e\0t", 6))
  212. loaded_image_info.device_handle = nethandle;
  213. else
  214. loaded_image_info.device_handle = bootefi_device_path;
  215. #endif
  216. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  217. efi_smbios_register();
  218. #endif
  219. /* Initialize EFI runtime services */
  220. efi_reset_system_init();
  221. efi_get_time_init();
  222. /* Call our payload! */
  223. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  224. if (setjmp(&loaded_image_info.exit_jmp)) {
  225. return loaded_image_info.exit_status;
  226. }
  227. #ifdef CONFIG_ARM64
  228. /* On AArch64 we need to make sure we call our payload in < EL3 */
  229. if (current_el() == 3) {
  230. smp_kick_all_cpus();
  231. dcache_disable(); /* flush cache before switch to EL2 */
  232. /* Move into EL2 and keep running there */
  233. armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
  234. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  235. ES_TO_AARCH64);
  236. /* Should never reach here, efi exits with longjmp */
  237. while (1) { }
  238. }
  239. #endif
  240. return efi_do_enter(&loaded_image_info, &systab, entry);
  241. }
  242. /* Interpreter command to boot an arbitrary EFI image from memory */
  243. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  244. {
  245. char *saddr, *sfdt;
  246. unsigned long addr, fdt_addr = 0;
  247. unsigned long r;
  248. if (argc < 2)
  249. return CMD_RET_USAGE;
  250. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  251. if (!strcmp(argv[1], "hello")) {
  252. ulong size = __efi_hello_world_end - __efi_hello_world_begin;
  253. addr = CONFIG_SYS_LOAD_ADDR;
  254. memcpy((char *)addr, __efi_hello_world_begin, size);
  255. } else
  256. #endif
  257. {
  258. saddr = argv[1];
  259. addr = simple_strtoul(saddr, NULL, 16);
  260. if (argc > 2) {
  261. sfdt = argv[2];
  262. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  263. }
  264. }
  265. printf("## Starting EFI application at %08lx ...\n", addr);
  266. r = do_bootefi_exec((void *)addr, (void*)fdt_addr);
  267. printf("## Application terminated, r = %lu\n",
  268. r & ~EFI_ERROR_MASK);
  269. if (r != EFI_SUCCESS)
  270. return 1;
  271. else
  272. return 0;
  273. }
  274. #ifdef CONFIG_SYS_LONGHELP
  275. static char bootefi_help_text[] =
  276. "<image address> [fdt address]\n"
  277. " - boot EFI payload stored at address <image address>.\n"
  278. " If specified, the device tree located at <fdt address> gets\n"
  279. " exposed as EFI configuration table.\n"
  280. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  281. "hello\n"
  282. " - boot a sample Hello World application stored within U-Boot"
  283. #endif
  284. ;
  285. #endif
  286. U_BOOT_CMD(
  287. bootefi, 3, 0, do_bootefi,
  288. "Boots an EFI payload from memory",
  289. bootefi_help_text
  290. );
  291. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  292. {
  293. __maybe_unused struct blk_desc *desc;
  294. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  295. char *colon;
  296. #if defined(CONFIG_BLK) || CONFIG_IS_ENABLED(ISO_PARTITION)
  297. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  298. #endif
  299. #ifdef CONFIG_BLK
  300. if (desc) {
  301. snprintf(devname, sizeof(devname), "%s", desc->bdev->name);
  302. } else
  303. #endif
  304. {
  305. /* Assemble the condensed device name we use in efi_disk.c */
  306. snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
  307. }
  308. colon = strchr(devname, ':');
  309. #if CONFIG_IS_ENABLED(ISO_PARTITION)
  310. /* For ISOs we create partition block devices */
  311. if (desc && (desc->type != DEV_TYPE_UNKNOWN) &&
  312. (desc->part_type == PART_TYPE_ISO)) {
  313. if (!colon)
  314. snprintf(devname, sizeof(devname), "%s:1", devname);
  315. colon = NULL;
  316. }
  317. #endif
  318. if (colon)
  319. *colon = '\0';
  320. /* Patch bootefi_device_path to the target device */
  321. memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str));
  322. ascii2unicode(bootefi_device_path[0].str, devname);
  323. /* Patch bootefi_image_path to the target file path */
  324. memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
  325. if (strcmp(dev, "Net")) {
  326. /* Add leading / to fs paths, because they're absolute */
  327. snprintf(devname, sizeof(devname), "/%s", path);
  328. } else {
  329. snprintf(devname, sizeof(devname), "%s", path);
  330. }
  331. ascii2unicode(bootefi_image_path[0].str, devname);
  332. }