bootefi.c 8.7 KB

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