bootefi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #define OBJ_LIST_NOT_INITIALIZED 1
  23. static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
  24. static struct efi_device_path *bootefi_image_path;
  25. static struct efi_device_path *bootefi_device_path;
  26. /* Initialize and populate EFI object list */
  27. efi_status_t efi_init_obj_list(void)
  28. {
  29. efi_status_t ret = EFI_SUCCESS;
  30. /* Initialize once only */
  31. if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
  32. return efi_obj_list_initialized;
  33. /* Initialize EFI driver uclass */
  34. ret = efi_driver_init();
  35. if (ret != EFI_SUCCESS)
  36. goto out;
  37. ret = efi_console_register();
  38. if (ret != EFI_SUCCESS)
  39. goto out;
  40. #ifdef CONFIG_PARTITIONS
  41. ret = efi_disk_register();
  42. if (ret != EFI_SUCCESS)
  43. goto out;
  44. #endif
  45. #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
  46. ret = efi_gop_register();
  47. if (ret != EFI_SUCCESS)
  48. goto out;
  49. #endif
  50. #ifdef CONFIG_CMD_NET
  51. ret = efi_net_register();
  52. if (ret != EFI_SUCCESS)
  53. goto out;
  54. #endif
  55. #ifdef CONFIG_GENERATE_SMBIOS_TABLE
  56. ret = efi_smbios_register();
  57. if (ret != EFI_SUCCESS)
  58. goto out;
  59. #endif
  60. ret = efi_watchdog_register();
  61. if (ret != EFI_SUCCESS)
  62. goto out;
  63. /* Initialize EFI runtime services */
  64. ret = efi_reset_system_init();
  65. if (ret != EFI_SUCCESS)
  66. goto out;
  67. ret = efi_get_time_init();
  68. if (ret != EFI_SUCCESS)
  69. goto out;
  70. out:
  71. efi_obj_list_initialized = ret;
  72. return ret;
  73. }
  74. /*
  75. * Set the load options of an image from an environment variable.
  76. *
  77. * @loaded_image_info: the image
  78. * @env_var: name of the environment variable
  79. */
  80. static void set_load_options(struct efi_loaded_image *loaded_image_info,
  81. const char *env_var)
  82. {
  83. size_t size;
  84. const char *env = env_get(env_var);
  85. loaded_image_info->load_options = NULL;
  86. loaded_image_info->load_options_size = 0;
  87. if (!env)
  88. return;
  89. size = strlen(env) + 1;
  90. loaded_image_info->load_options = calloc(size, sizeof(u16));
  91. if (!loaded_image_info->load_options) {
  92. printf("ERROR: Out of memory\n");
  93. return;
  94. }
  95. utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
  96. loaded_image_info->load_options_size = size * 2;
  97. }
  98. static void *copy_fdt(void *fdt)
  99. {
  100. u64 fdt_size = fdt_totalsize(fdt);
  101. unsigned long fdt_ram_start = -1L, fdt_pages;
  102. u64 new_fdt_addr;
  103. void *new_fdt;
  104. int i;
  105. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  106. u64 ram_start = gd->bd->bi_dram[i].start;
  107. u64 ram_size = gd->bd->bi_dram[i].size;
  108. if (!ram_size)
  109. continue;
  110. if (ram_start < fdt_ram_start)
  111. fdt_ram_start = ram_start;
  112. }
  113. /* Give us at least 4kb breathing room */
  114. fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
  115. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  116. /* Safe fdt location is at 128MB */
  117. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  118. if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  119. &new_fdt_addr) != EFI_SUCCESS) {
  120. /* If we can't put it there, put it somewhere */
  121. new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
  122. if (efi_allocate_pages(1, EFI_RUNTIME_SERVICES_DATA, fdt_pages,
  123. &new_fdt_addr) != EFI_SUCCESS) {
  124. printf("ERROR: Failed to reserve space for FDT\n");
  125. return NULL;
  126. }
  127. }
  128. new_fdt = (void*)(ulong)new_fdt_addr;
  129. memcpy(new_fdt, fdt, fdt_totalsize(fdt));
  130. fdt_set_totalsize(new_fdt, fdt_size);
  131. return new_fdt;
  132. }
  133. static efi_status_t efi_do_enter(
  134. efi_handle_t image_handle, struct efi_system_table *st,
  135. EFIAPI efi_status_t (*entry)(
  136. efi_handle_t 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 efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
  147. efi_handle_t image_handle, struct efi_system_table *st),
  148. efi_handle_t 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 efi_status_t do_bootefi_exec(void *efi, void *fdt,
  160. struct efi_device_path *device_path,
  161. struct efi_device_path *image_path)
  162. {
  163. struct efi_loaded_image loaded_image_info = {};
  164. struct efi_object loaded_image_info_obj = {};
  165. struct efi_device_path *memdp = NULL;
  166. efi_status_t ret;
  167. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  168. struct efi_system_table *st);
  169. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  170. const efi_guid_t fdt_guid = EFI_FDT_GUID;
  171. bootm_headers_t img = { 0 };
  172. /*
  173. * Special case for efi payload not loaded from disk, such as
  174. * 'bootefi hello' or for example payload loaded directly into
  175. * memory via jtag/etc:
  176. */
  177. if (!device_path && !image_path) {
  178. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  179. /* actual addresses filled in after efi_load_pe() */
  180. memdp = efi_dp_from_mem(0, 0, 0);
  181. device_path = image_path = memdp;
  182. } else {
  183. assert(device_path && image_path);
  184. }
  185. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  186. device_path, image_path);
  187. /*
  188. * gd lives in a fixed register which may get clobbered while we execute
  189. * the payload. So save it here and restore it on every callback entry
  190. */
  191. efi_save_gd();
  192. if (fdt && !fdt_check_header(fdt)) {
  193. /* Prepare fdt for payload */
  194. fdt = copy_fdt(fdt);
  195. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  196. printf("ERROR: Failed to process device tree\n");
  197. return -EINVAL;
  198. }
  199. /* Link to it in the efi tables */
  200. efi_install_configuration_table(&fdt_guid, fdt);
  201. /* And reserve the space in the memory map */
  202. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  203. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  204. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  205. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  206. /* Give a bootloader the chance to modify the device tree */
  207. fdt_pages += 2;
  208. efi_add_memory_map(fdt_start, fdt_pages,
  209. EFI_BOOT_SERVICES_DATA, true);
  210. } else {
  211. printf("WARNING: Invalid device tree, expect boot to fail\n");
  212. efi_install_configuration_table(&fdt_guid, NULL);
  213. }
  214. /* Transfer environment variable bootargs as load options */
  215. set_load_options(&loaded_image_info, "bootargs");
  216. /* Load the EFI payload */
  217. entry = efi_load_pe(efi, &loaded_image_info);
  218. if (!entry) {
  219. ret = EFI_LOAD_ERROR;
  220. goto exit;
  221. }
  222. if (memdp) {
  223. struct efi_device_path_memory *mdp = (void *)memdp;
  224. mdp->memory_type = loaded_image_info.image_code_type;
  225. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  226. mdp->end_address = mdp->start_address +
  227. loaded_image_info.image_size;
  228. }
  229. /* we don't support much: */
  230. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  231. "{ro,boot}(blob)0000000000000000");
  232. /* Call our payload! */
  233. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  234. if (setjmp(&loaded_image_info.exit_jmp)) {
  235. ret = loaded_image_info.exit_status;
  236. goto exit;
  237. }
  238. #ifdef CONFIG_ARM64
  239. /* On AArch64 we need to make sure we call our payload in < EL3 */
  240. if (current_el() == 3) {
  241. smp_kick_all_cpus();
  242. dcache_disable(); /* flush cache before switch to EL2 */
  243. /* Move into EL2 and keep running there */
  244. armv8_switch_to_el2((ulong)entry,
  245. (ulong)&loaded_image_info_obj.handle,
  246. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  247. ES_TO_AARCH64);
  248. /* Should never reach here, efi exits with longjmp */
  249. while (1) { }
  250. }
  251. #endif
  252. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  253. exit:
  254. /* image has returned, loaded-image obj goes *poof*: */
  255. list_del(&loaded_image_info_obj.link);
  256. return ret;
  257. }
  258. static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
  259. {
  260. struct efi_device_path *device_path, *file_path;
  261. void *addr;
  262. efi_status_t r;
  263. /*
  264. * gd lives in a fixed register which may get clobbered while we execute
  265. * the payload. So save it here and restore it on every callback entry
  266. */
  267. efi_save_gd();
  268. addr = efi_bootmgr_load(&device_path, &file_path);
  269. if (!addr)
  270. return 1;
  271. printf("## Starting EFI application at %p ...\n", addr);
  272. r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
  273. printf("## Application terminated, r = %lu\n",
  274. r & ~EFI_ERROR_MASK);
  275. if (r != EFI_SUCCESS)
  276. return 1;
  277. return 0;
  278. }
  279. /* Interpreter command to boot an arbitrary EFI image from memory */
  280. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  281. {
  282. char *saddr, *sfdt;
  283. unsigned long addr, fdt_addr = 0;
  284. efi_status_t r;
  285. /* Initialize EFI drivers */
  286. r = efi_init_obj_list();
  287. if (r != EFI_SUCCESS) {
  288. printf("Error: Cannot set up EFI drivers, r = %lu\n",
  289. r & ~EFI_ERROR_MASK);
  290. return CMD_RET_FAILURE;
  291. }
  292. if (argc < 2)
  293. return CMD_RET_USAGE;
  294. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  295. if (!strcmp(argv[1], "hello")) {
  296. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  297. saddr = env_get("loadaddr");
  298. if (saddr)
  299. addr = simple_strtoul(saddr, NULL, 16);
  300. else
  301. addr = CONFIG_SYS_LOAD_ADDR;
  302. memcpy((char *)addr, __efi_helloworld_begin, size);
  303. } else
  304. #endif
  305. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  306. if (!strcmp(argv[1], "selftest")) {
  307. struct efi_loaded_image loaded_image_info = {};
  308. struct efi_object loaded_image_info_obj = {};
  309. /* Construct a dummy device path. */
  310. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  311. (uintptr_t)&efi_selftest,
  312. (uintptr_t)&efi_selftest);
  313. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  314. efi_setup_loaded_image(&loaded_image_info,
  315. &loaded_image_info_obj,
  316. bootefi_device_path, bootefi_image_path);
  317. /*
  318. * gd lives in a fixed register which may get clobbered while we
  319. * execute the payload. So save it here and restore it on every
  320. * callback entry
  321. */
  322. efi_save_gd();
  323. /* Initialize and populate EFI object list */
  324. efi_init_obj_list();
  325. /* Transfer environment variable efi_selftest as load options */
  326. set_load_options(&loaded_image_info, "efi_selftest");
  327. /* Execute the test */
  328. r = efi_selftest(loaded_image_info_obj.handle, &systab);
  329. efi_restore_gd();
  330. free(loaded_image_info.load_options);
  331. list_del(&loaded_image_info_obj.link);
  332. return r != EFI_SUCCESS;
  333. } else
  334. #endif
  335. if (!strcmp(argv[1], "bootmgr")) {
  336. unsigned long fdt_addr = 0;
  337. if (argc > 2)
  338. fdt_addr = simple_strtoul(argv[2], NULL, 16);
  339. return do_bootefi_bootmgr_exec(fdt_addr);
  340. } else {
  341. saddr = argv[1];
  342. addr = simple_strtoul(saddr, NULL, 16);
  343. /* Check that a numeric value was passed */
  344. if (!addr && *saddr != '0')
  345. return CMD_RET_USAGE;
  346. if (argc > 2) {
  347. sfdt = argv[2];
  348. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  349. }
  350. }
  351. printf("## Starting EFI application at %08lx ...\n", addr);
  352. r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
  353. bootefi_device_path, bootefi_image_path);
  354. printf("## Application terminated, r = %lu\n",
  355. r & ~EFI_ERROR_MASK);
  356. if (r != EFI_SUCCESS)
  357. return 1;
  358. else
  359. return 0;
  360. }
  361. #ifdef CONFIG_SYS_LONGHELP
  362. static char bootefi_help_text[] =
  363. "<image address> [fdt address]\n"
  364. " - boot EFI payload stored at address <image address>.\n"
  365. " If specified, the device tree located at <fdt address> gets\n"
  366. " exposed as EFI configuration table.\n"
  367. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  368. "bootefi hello\n"
  369. " - boot a sample Hello World application stored within U-Boot\n"
  370. #endif
  371. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  372. "bootefi selftest\n"
  373. " - boot an EFI selftest application stored within U-Boot\n"
  374. " Use environment variable efi_selftest to select a single test.\n"
  375. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  376. #endif
  377. "bootefi bootmgr [fdt addr]\n"
  378. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  379. "\n"
  380. " If specified, the device tree located at <fdt address> gets\n"
  381. " exposed as EFI configuration table.\n";
  382. #endif
  383. U_BOOT_CMD(
  384. bootefi, 3, 0, do_bootefi,
  385. "Boots an EFI payload from memory",
  386. bootefi_help_text
  387. );
  388. static int parse_partnum(const char *devnr)
  389. {
  390. const char *str = strchr(devnr, ':');
  391. if (str) {
  392. str++;
  393. return simple_strtoul(str, NULL, 16);
  394. }
  395. return 0;
  396. }
  397. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  398. {
  399. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  400. char *s;
  401. if (strcmp(dev, "Net")) {
  402. struct blk_desc *desc;
  403. int part;
  404. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  405. if (!desc)
  406. return;
  407. part = parse_partnum(devnr);
  408. bootefi_device_path = efi_dp_from_part(desc, part);
  409. } else {
  410. #ifdef CONFIG_CMD_NET
  411. bootefi_device_path = efi_dp_from_eth();
  412. #endif
  413. }
  414. if (!path)
  415. return;
  416. if (strcmp(dev, "Net")) {
  417. /* Add leading / to fs paths, because they're absolute */
  418. snprintf(filename, sizeof(filename), "/%s", path);
  419. } else {
  420. snprintf(filename, sizeof(filename), "%s", path);
  421. }
  422. /* DOS style file path: */
  423. s = filename;
  424. while ((s = strchr(s, '/')))
  425. *s++ = '\\';
  426. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  427. }