bootefi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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_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. /* Carve out DT reserved memory ranges */
  156. static efi_status_t efi_carve_out_dt_rsv(void *fdt)
  157. {
  158. int nr_rsv, i;
  159. uint64_t addr, size, pages;
  160. nr_rsv = fdt_num_mem_rsv(fdt);
  161. /* Look for an existing entry and add it to the efi mem map. */
  162. for (i = 0; i < nr_rsv; i++) {
  163. if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
  164. continue;
  165. pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT;
  166. efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
  167. false);
  168. }
  169. return EFI_SUCCESS;
  170. }
  171. static efi_status_t efi_install_fdt(void *fdt)
  172. {
  173. bootm_headers_t img = { 0 };
  174. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  175. efi_status_t ret;
  176. if (fdt_check_header(fdt)) {
  177. printf("ERROR: invalid device tree\n");
  178. return EFI_INVALID_PARAMETER;
  179. }
  180. /* Prepare fdt for payload */
  181. fdt = copy_fdt(fdt);
  182. if (!fdt)
  183. return EFI_OUT_OF_RESOURCES;
  184. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  185. printf("ERROR: failed to process device tree\n");
  186. return EFI_LOAD_ERROR;
  187. }
  188. if (efi_carve_out_dt_rsv(fdt) != EFI_SUCCESS) {
  189. printf("ERROR: failed to carve out memory\n");
  190. return EFI_LOAD_ERROR;
  191. }
  192. /* Link to it in the efi tables */
  193. ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
  194. if (ret != EFI_SUCCESS)
  195. return EFI_OUT_OF_RESOURCES;
  196. /* And reserve the space in the memory map */
  197. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  198. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  199. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  200. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  201. /* Give a bootloader the chance to modify the device tree */
  202. fdt_pages += 2;
  203. ret = efi_add_memory_map(fdt_start, fdt_pages,
  204. EFI_BOOT_SERVICES_DATA, true);
  205. return ret;
  206. }
  207. /*
  208. * Load an EFI payload into a newly allocated piece of memory, register all
  209. * EFI objects it would want to access and jump to it.
  210. */
  211. static efi_status_t do_bootefi_exec(void *efi,
  212. struct efi_device_path *device_path,
  213. struct efi_device_path *image_path)
  214. {
  215. struct efi_loaded_image loaded_image_info = {};
  216. struct efi_object loaded_image_info_obj = {};
  217. struct efi_device_path *memdp = NULL;
  218. efi_status_t ret;
  219. EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
  220. struct efi_system_table *st);
  221. /*
  222. * Special case for efi payload not loaded from disk, such as
  223. * 'bootefi hello' or for example payload loaded directly into
  224. * memory via jtag/etc:
  225. */
  226. if (!device_path && !image_path) {
  227. printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
  228. /* actual addresses filled in after efi_load_pe() */
  229. memdp = efi_dp_from_mem(0, 0, 0);
  230. device_path = image_path = memdp;
  231. } else {
  232. assert(device_path && image_path);
  233. }
  234. efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
  235. device_path, image_path);
  236. /*
  237. * gd lives in a fixed register which may get clobbered while we execute
  238. * the payload. So save it here and restore it on every callback entry
  239. */
  240. efi_save_gd();
  241. /* Transfer environment variable bootargs as load options */
  242. set_load_options(&loaded_image_info, "bootargs");
  243. /* Load the EFI payload */
  244. entry = efi_load_pe(efi, &loaded_image_info);
  245. if (!entry) {
  246. ret = EFI_LOAD_ERROR;
  247. goto exit;
  248. }
  249. if (memdp) {
  250. struct efi_device_path_memory *mdp = (void *)memdp;
  251. mdp->memory_type = loaded_image_info.image_code_type;
  252. mdp->start_address = (uintptr_t)loaded_image_info.image_base;
  253. mdp->end_address = mdp->start_address +
  254. loaded_image_info.image_size;
  255. }
  256. /* we don't support much: */
  257. env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
  258. "{ro,boot}(blob)0000000000000000");
  259. /* Call our payload! */
  260. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  261. if (setjmp(&loaded_image_info.exit_jmp)) {
  262. ret = loaded_image_info.exit_status;
  263. goto exit;
  264. }
  265. #ifdef CONFIG_ARM64
  266. /* On AArch64 we need to make sure we call our payload in < EL3 */
  267. if (current_el() == 3) {
  268. smp_kick_all_cpus();
  269. dcache_disable(); /* flush cache before switch to EL2 */
  270. /* Move into EL2 and keep running there */
  271. armv8_switch_to_el2((ulong)entry,
  272. (ulong)&loaded_image_info_obj.handle,
  273. (ulong)&systab, 0, (ulong)efi_run_in_el2,
  274. ES_TO_AARCH64);
  275. /* Should never reach here, efi exits with longjmp */
  276. while (1) { }
  277. }
  278. #endif
  279. ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
  280. exit:
  281. /* image has returned, loaded-image obj goes *poof*: */
  282. list_del(&loaded_image_info_obj.link);
  283. return ret;
  284. }
  285. static int do_bootefi_bootmgr_exec(void)
  286. {
  287. struct efi_device_path *device_path, *file_path;
  288. void *addr;
  289. efi_status_t r;
  290. /*
  291. * gd lives in a fixed register which may get clobbered while we execute
  292. * the payload. So save it here and restore it on every callback entry
  293. */
  294. efi_save_gd();
  295. addr = efi_bootmgr_load(&device_path, &file_path);
  296. if (!addr)
  297. return 1;
  298. printf("## Starting EFI application at %p ...\n", addr);
  299. r = do_bootefi_exec(addr, device_path, file_path);
  300. printf("## Application terminated, r = %lu\n",
  301. r & ~EFI_ERROR_MASK);
  302. if (r != EFI_SUCCESS)
  303. return 1;
  304. return 0;
  305. }
  306. /* Interpreter command to boot an arbitrary EFI image from memory */
  307. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  308. {
  309. unsigned long addr;
  310. char *saddr;
  311. efi_status_t r;
  312. void *fdt_addr;
  313. /* Initialize EFI drivers */
  314. r = efi_init_obj_list();
  315. if (r != EFI_SUCCESS) {
  316. printf("Error: Cannot set up EFI drivers, r = %lu\n",
  317. r & ~EFI_ERROR_MASK);
  318. return CMD_RET_FAILURE;
  319. }
  320. if (argc < 2)
  321. return CMD_RET_USAGE;
  322. if (argc > 2) {
  323. fdt_addr = (void *)simple_strtoul(argv[2], NULL, 16);
  324. if (!fdt_addr && *argv[2] != '0')
  325. return CMD_RET_USAGE;
  326. /* Install device tree */
  327. r = efi_install_fdt(fdt_addr);
  328. if (r != EFI_SUCCESS) {
  329. printf("ERROR: failed to install device tree\n");
  330. return CMD_RET_FAILURE;
  331. }
  332. } else {
  333. /* Remove device tree. EFI_NOT_FOUND can be ignored here */
  334. efi_install_configuration_table(&efi_guid_fdt, NULL);
  335. printf("WARNING: booting without device tree\n");
  336. }
  337. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  338. if (!strcmp(argv[1], "hello")) {
  339. ulong size = __efi_helloworld_end - __efi_helloworld_begin;
  340. saddr = env_get("loadaddr");
  341. if (saddr)
  342. addr = simple_strtoul(saddr, NULL, 16);
  343. else
  344. addr = CONFIG_SYS_LOAD_ADDR;
  345. memcpy((char *)addr, __efi_helloworld_begin, size);
  346. } else
  347. #endif
  348. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  349. if (!strcmp(argv[1], "selftest")) {
  350. struct efi_loaded_image loaded_image_info = {};
  351. struct efi_object loaded_image_info_obj = {};
  352. /* Construct a dummy device path. */
  353. bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
  354. (uintptr_t)&efi_selftest,
  355. (uintptr_t)&efi_selftest);
  356. bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
  357. efi_setup_loaded_image(&loaded_image_info,
  358. &loaded_image_info_obj,
  359. bootefi_device_path, bootefi_image_path);
  360. /*
  361. * gd lives in a fixed register which may get clobbered while we
  362. * execute the payload. So save it here and restore it on every
  363. * callback entry
  364. */
  365. efi_save_gd();
  366. /* Initialize and populate EFI object list */
  367. efi_init_obj_list();
  368. /* Transfer environment variable efi_selftest as load options */
  369. set_load_options(&loaded_image_info, "efi_selftest");
  370. /* Execute the test */
  371. r = efi_selftest(loaded_image_info_obj.handle, &systab);
  372. efi_restore_gd();
  373. free(loaded_image_info.load_options);
  374. list_del(&loaded_image_info_obj.link);
  375. return r != EFI_SUCCESS;
  376. } else
  377. #endif
  378. if (!strcmp(argv[1], "bootmgr")) {
  379. return do_bootefi_bootmgr_exec();
  380. } else {
  381. saddr = argv[1];
  382. addr = simple_strtoul(saddr, NULL, 16);
  383. /* Check that a numeric value was passed */
  384. if (!addr && *saddr != '0')
  385. return CMD_RET_USAGE;
  386. }
  387. printf("## Starting EFI application at %08lx ...\n", addr);
  388. r = do_bootefi_exec((void *)addr, bootefi_device_path,
  389. bootefi_image_path);
  390. printf("## Application terminated, r = %lu\n",
  391. r & ~EFI_ERROR_MASK);
  392. if (r != EFI_SUCCESS)
  393. return 1;
  394. else
  395. return 0;
  396. }
  397. #ifdef CONFIG_SYS_LONGHELP
  398. static char bootefi_help_text[] =
  399. "<image address> [fdt address]\n"
  400. " - boot EFI payload stored at address <image address>.\n"
  401. " If specified, the device tree located at <fdt address> gets\n"
  402. " exposed as EFI configuration table.\n"
  403. #ifdef CONFIG_CMD_BOOTEFI_HELLO
  404. "bootefi hello\n"
  405. " - boot a sample Hello World application stored within U-Boot\n"
  406. #endif
  407. #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
  408. "bootefi selftest [fdt address]\n"
  409. " - boot an EFI selftest application stored within U-Boot\n"
  410. " Use environment variable efi_selftest to select a single test.\n"
  411. " Use 'setenv efi_selftest list' to enumerate all tests.\n"
  412. #endif
  413. "bootefi bootmgr [fdt addr]\n"
  414. " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
  415. "\n"
  416. " If specified, the device tree located at <fdt address> gets\n"
  417. " exposed as EFI configuration table.\n";
  418. #endif
  419. U_BOOT_CMD(
  420. bootefi, 3, 0, do_bootefi,
  421. "Boots an EFI payload from memory",
  422. bootefi_help_text
  423. );
  424. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  425. {
  426. char filename[32] = { 0 }; /* dp->str is u16[32] long */
  427. char *s;
  428. if (strcmp(dev, "Net")) {
  429. struct blk_desc *desc;
  430. disk_partition_t fs_partition;
  431. int part;
  432. part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
  433. 1);
  434. if (part < 0)
  435. return;
  436. bootefi_device_path = efi_dp_from_part(desc, part);
  437. } else {
  438. #ifdef CONFIG_NET
  439. bootefi_device_path = efi_dp_from_eth();
  440. #endif
  441. }
  442. if (!path)
  443. return;
  444. if (strcmp(dev, "Net")) {
  445. /* Add leading / to fs paths, because they're absolute */
  446. snprintf(filename, sizeof(filename), "/%s", path);
  447. } else {
  448. snprintf(filename, sizeof(filename), "%s", path);
  449. }
  450. /* DOS style file path: */
  451. s = filename;
  452. while ((s = strchr(s, '/')))
  453. *s++ = '\\';
  454. bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
  455. }