bootefi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /* Call our payload! */
  155. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  156. if (setjmp(&loaded_image_info.exit_jmp)) {
  157. ret = loaded_image_info.exit_status;
  158. EFI_EXIT(ret);
  159. goto exit;
  160. }
  161. #ifdef CONFIG_ARM64
  162. /* On AArch64 we need to make sure we call our payload in < EL3 */
  163. if (current_el() == 3) {
  164. smp_kick_all_cpus();
  165. dcache_disable(); /* flush cache before switch to EL2 */
  166. /* Move into EL2 and keep running there */
  167. armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
  168. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  169. ES_TO_AARCH64);
  170. /* Should never reach here, efi exits with longjmp */
  171. while (1) { }
  172. }
  173. #endif
  174. ret = efi_do_enter(&loaded_image_info, &systab, entry);
  175. exit:
  176. /* image has returned, loaded-image obj goes *poof*: */
  177. list_del(&loaded_image_info_obj.link);
  178. return ret;
  179. }
  180. /* Interpreter command to boot an arbitrary EFI image from memory */
  181. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  182. {
  183. char *saddr, *sfdt;
  184. unsigned long addr, fdt_addr = 0;
  185. unsigned long r;
  186. if (argc < 2)
  187. return CMD_RET_USAGE;
  188. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  189. if (!strcmp(argv[1], "hello")) {
  190. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  191. saddr = env_get("loadaddr");
  192. if (saddr)
  193. addr = simple_strtoul(saddr, NULL, 16);
  194. else
  195. addr = CONFIG_SYS_LOAD_ADDR;
  196. memcpy((char *)addr, __efi_helloworld_begin, size);
  197. } else
  198. #endif
  199. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  200. if (!strcmp(argv[1], "selftest")) {
  201. /*
  202. * gd lives in a fixed register which may get clobbered while we
  203. * execute the payload. So save it here and restore it on every
  204. * callback entry
  205. */
  206. efi_save_gd();
  207. /* Initialize and populate EFI object list */
  208. if (!efi_obj_list_initalized)
  209. efi_init_obj_list();
  210. loaded_image_info.device_handle = bootefi_device_path;
  211. loaded_image_info.file_path = bootefi_image_path;
  212. return efi_selftest(&loaded_image_info, &systab);
  213. } else
  214. #endif
  215. {
  216. saddr = argv[1];
  217. addr = simple_strtoul(saddr, NULL, 16);
  218. if (argc > 2) {
  219. sfdt = argv[2];
  220. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  221. }
  222. }
  223. printf("## Starting EFI application at %08lx ...\n", addr);
  224. r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
  225. bootefi_device_path, bootefi_image_path);
  226. printf("## Application terminated, r = %lu\n",
  227. r & ~EFI_ERROR_MASK);
  228. if (r != EFI_SUCCESS)
  229. return 1;
  230. else
  231. return 0;
  232. }
  233. #ifdef CONFIG_SYS_LONGHELP
  234. static char bootefi_help_text[] =
  235. "<image address> [fdt address]\n"
  236. " - boot EFI payload stored at address <image address>.\n"
  237. " If specified, the device tree located at <fdt address> gets\n"
  238. " exposed as EFI configuration table.\n"
  239. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  240. "bootefi hello\n"
  241. " - boot a sample Hello World application stored within U-Boot\n"
  242. #endif
  243. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  244. "bootefi selftest\n"
  245. " - boot an EFI selftest application stored within U-Boot\n"
  246. #endif
  247. ;
  248. #endif
  249. U_BOOT_CMD(
  250. bootefi, 3, 0, do_bootefi,
  251. "Boots an EFI payload from memory",
  252. bootefi_help_text
  253. );
  254. static int parse_partnum(const char *devnr)
  255. {
  256. const char *str = strchr(devnr, ':');
  257. if (str) {
  258. str++;
  259. return simple_strtoul(str, NULL, 16);
  260. }
  261. return 0;
  262. }
  263. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  264. {
  265. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  266. char *s;
  267. if (strcmp(dev, "Net")) {
  268. struct blk_desc *desc;
  269. int part;
  270. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  271. part = parse_partnum(devnr);
  272. bootefi_device_path = efi_dp_from_part(desc, part);
  273. } else {
  274. #ifdef CONFIG_NET
  275. bootefi_device_path = efi_dp_from_eth();
  276. #endif
  277. }
  278. if (strcmp(dev, "Net")) {
  279. /* Add leading / to fs paths, because they're absolute */
  280. snprintf(filename, sizeof(filename), "/%s", path);
  281. } else {
  282. snprintf(filename, sizeof(filename), "%s", path);
  283. }
  284. /* DOS style file path: */
  285. s = filename;
  286. while ((s = strchr(s, '/')))
  287. *s++ = '\\';
  288. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  289. }