bootefi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. };
  79. /* The EFI object struct for the device the "bootefi" image was loaded from */
  80. static struct efi_object bootefi_device_obj = {
  81. .handle = bootefi_device_path,
  82. .protocols = {
  83. {
  84. /* When asking for the device path interface, return
  85. * bootefi_device_path */
  86. .guid = &efi_guid_device_path,
  87. .protocol_interface = bootefi_device_path
  88. }
  89. },
  90. };
  91. static void *copy_fdt(void *fdt)
  92. {
  93. u64 fdt_size = fdt_totalsize(fdt);
  94. unsigned long fdt_ram_start = -1L, fdt_pages;
  95. u64 new_fdt_addr;
  96. void *new_fdt;
  97. int i;
  98. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  99. u64 ram_start = gd->bd->bi_dram[i].start;
  100. u64 ram_size = gd->bd->bi_dram[i].size;
  101. if (!ram_size)
  102. continue;
  103. if (ram_start < fdt_ram_start)
  104. fdt_ram_start = ram_start;
  105. }
  106. /* Give us at least 4kb breathing room */
  107. fdt_size = ALIGN(fdt_size + 4096, 4096);
  108. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  109. /* Safe fdt location is at 128MB */
  110. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  111. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  112. &new_fdt_addr) != EFI_SUCCESS) {
  113. /* If we can't put it there, put it somewhere */
  114. new_fdt_addr = (ulong)memalign(4096, fdt_size);
  115. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  116. &new_fdt_addr) != EFI_SUCCESS) {
  117. printf("ERROR: Failed to reserve space for FDT\n");
  118. return NULL;
  119. }
  120. }
  121. new_fdt = (void*)(ulong)new_fdt_addr;
  122. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  123. fdt_set_totalsize(new_fdt, fdt_size);
  124. return new_fdt;
  125. }
  126. #ifdef CONFIG_ARM64
  127. static unsigned long efi_run_in_el2(ulong (*entry)(void *image_handle,
  128. struct efi_system_table *st), void *image_handle,
  129. struct efi_system_table *st)
  130. {
  131. /* Enable caches again */
  132. dcache_enable();
  133. return entry(image_handle, st);
  134. }
  135. #endif
  136. /*
  137. * Load an EFI payload into a newly allocated piece of memory, register all
  138. * EFI objects it would want to access and jump to it.
  139. */
  140. static unsigned long do_bootefi_exec(void *efi, void *fdt)
  141. {
  142. ulong (*entry)(void *image_handle, struct efi_system_table *st)
  143. asmlinkage;
  144. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  145. bootm_headers_t img = { 0 };
  146. /*
  147. * gd lives in a fixed register which may get clobbered while we execute
  148. * the payload. So save it here and restore it on every callback entry
  149. */
  150. efi_save_gd();
  151. if (fdt && !fdt_check_header(fdt)) {
  152. /* Prepare fdt for payload */
  153. fdt = copy_fdt(fdt);
  154. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  155. printf("ERROR: Failed to process device tree\n");
  156. return -EINVAL;
  157. }
  158. /* Link to it in the efi tables */
  159. systab.tables[0].guid = EFI_FDT_GUID;
  160. systab.tables[0].table = fdt;
  161. systab.nr_tables = 1;
  162. /* And reserve the space in the memory map */
  163. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  164. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  165. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  166. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  167. /* Give a bootloader the chance to modify the device tree */
  168. fdt_pages += 2;
  169. efi_add_memory_map(fdt_start, fdt_pages,
  170. EFI_BOOT_SERVICES_DATA, true);
  171. } else {
  172. printf("WARNING: Invalid device tree, expect boot to fail\n");
  173. systab.nr_tables = 0;
  174. }
  175. /* Load the EFI payload */
  176. entry = efi_load_pe(efi, &loaded_image_info);
  177. if (!entry)
  178. return -ENOENT;
  179. /* Initialize and populate EFI object list */
  180. INIT_LIST_HEAD(&efi_obj_list);
  181. list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
  182. list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
  183. #ifdef CONFIG_PARTITIONS
  184. efi_disk_register();
  185. #endif
  186. #ifdef CONFIG_LCD
  187. efi_gop_register();
  188. #endif
  189. #ifdef CONFIG_NET
  190. void *nethandle = loaded_image_info.device_handle;
  191. efi_net_register(&nethandle);
  192. if (!memcmp(bootefi_device_path[0].str, "N\0e\0t", 6))
  193. loaded_image_info.device_handle = nethandle;
  194. else
  195. loaded_image_info.device_handle = bootefi_device_path;
  196. #endif
  197. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  198. efi_smbios_register();
  199. #endif
  200. /* Initialize EFI runtime services */
  201. efi_reset_system_init();
  202. efi_get_time_init();
  203. /* Call our payload! */
  204. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  205. if (setjmp(&loaded_image_info.exit_jmp)) {
  206. efi_status_t status = loaded_image_info.exit_status;
  207. return status == EFI_SUCCESS ? 0 : -EINVAL;
  208. }
  209. #ifdef CONFIG_ARM64
  210. /* On AArch64 we need to make sure we call our payload in < EL3 */
  211. if (current_el() == 3) {
  212. smp_kick_all_cpus();
  213. dcache_disable(); /* flush cache before switch to EL2 */
  214. /* Move into EL2 and keep running there */
  215. armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
  216. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  217. ES_TO_AARCH64);
  218. /* Should never reach here, efi exits with longjmp */
  219. while (1) { }
  220. }
  221. #endif
  222. return entry(&loaded_image_info, &systab);
  223. }
  224. /* Interpreter command to boot an arbitrary EFI image from memory */
  225. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  226. {
  227. char *saddr, *sfdt;
  228. unsigned long addr, fdt_addr = 0;
  229. int r = 0;
  230. if (argc < 2)
  231. return CMD_RET_USAGE;
  232. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  233. if (!strcmp(argv[1], "hello")) {
  234. ulong size = __efi_hello_world_end - __efi_hello_world_begin;
  235. addr = CONFIG_SYS_LOAD_ADDR;
  236. memcpy((char *)addr, __efi_hello_world_begin, size);
  237. } else
  238. #endif
  239. {
  240. saddr = argv[1];
  241. addr = simple_strtoul(saddr, NULL, 16);
  242. if (argc > 2) {
  243. sfdt = argv[2];
  244. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  245. }
  246. }
  247. printf("## Starting EFI application at %08lx ...\n", addr);
  248. r = do_bootefi_exec((void *)addr, (void*)fdt_addr);
  249. printf("## Application terminated, r = %d\n", r);
  250. if (r != 0)
  251. r = 1;
  252. return r;
  253. }
  254. #ifdef CONFIG_SYS_LONGHELP
  255. static char bootefi_help_text[] =
  256. "<image address> [fdt address]\n"
  257. " - boot EFI payload stored at address <image address>.\n"
  258. " If specified, the device tree located at <fdt address> gets\n"
  259. " exposed as EFI configuration table.\n"
  260. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  261. "hello\n"
  262. " - boot a sample Hello World application stored within U-Boot"
  263. #endif
  264. ;
  265. #endif
  266. U_BOOT_CMD(
  267. bootefi, 3, 0, do_bootefi,
  268. "Boots an EFI payload from memory",
  269. bootefi_help_text
  270. );
  271. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  272. {
  273. __maybe_unused struct blk_desc *desc;
  274. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  275. char *colon;
  276. #if defined(CONFIG_BLK) || CONFIG_IS_ENABLED(ISO_PARTITION)
  277. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  278. #endif
  279. #ifdef CONFIG_BLK
  280. if (desc) {
  281. snprintf(devname, sizeof(devname), "%s", desc->bdev->name);
  282. } else
  283. #endif
  284. {
  285. /* Assemble the condensed device name we use in efi_disk.c */
  286. snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
  287. }
  288. colon = strchr(devname, ':');
  289. #if CONFIG_IS_ENABLED(ISO_PARTITION)
  290. /* For ISOs we create partition block devices */
  291. if (desc && (desc->type != DEV_TYPE_UNKNOWN) &&
  292. (desc->part_type == PART_TYPE_ISO)) {
  293. if (!colon)
  294. snprintf(devname, sizeof(devname), "%s:1", devname);
  295. colon = NULL;
  296. }
  297. #endif
  298. if (colon)
  299. *colon = '\0';
  300. /* Patch bootefi_device_path to the target device */
  301. memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str));
  302. ascii2unicode(bootefi_device_path[0].str, devname);
  303. /* Patch bootefi_image_path to the target file path */
  304. memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
  305. if (strcmp(dev, "Net")) {
  306. /* Add leading / to fs paths, because they're absolute */
  307. snprintf(devname, sizeof(devname), "/%s", path);
  308. } else {
  309. snprintf(devname, sizeof(devname), "%s", path);
  310. }
  311. ascii2unicode(bootefi_image_path[0].str, devname);
  312. }