bootefi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/device.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. DECLARE_GLOBAL_DATA_PTR;
  18. /*
  19. * When booting using the "bootefi" command, we don't know which
  20. * physical device the file came from. So we create a pseudo-device
  21. * called "bootefi" with the device path /bootefi.
  22. *
  23. * In addition to the originating device we also declare the file path
  24. * of "bootefi" based loads to be /bootefi.
  25. */
  26. static struct efi_device_path_file_path bootefi_image_path[] = {
  27. {
  28. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  29. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  30. .dp.length = sizeof(bootefi_image_path[0]),
  31. .str = { 'b','o','o','t','e','f','i' },
  32. }, {
  33. .dp.type = DEVICE_PATH_TYPE_END,
  34. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  35. .dp.length = sizeof(bootefi_image_path[0]),
  36. }
  37. };
  38. static struct efi_device_path_file_path bootefi_device_path[] = {
  39. {
  40. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  41. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  42. .dp.length = sizeof(bootefi_image_path[0]),
  43. .str = { 'b','o','o','t','e','f','i' },
  44. }, {
  45. .dp.type = DEVICE_PATH_TYPE_END,
  46. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  47. .dp.length = sizeof(bootefi_image_path[0]),
  48. }
  49. };
  50. static efi_status_t bootefi_open_dp(void *handle, efi_guid_t *protocol,
  51. void **protocol_interface, void *agent_handle,
  52. void *controller_handle, uint32_t attributes)
  53. {
  54. *protocol_interface = bootefi_device_path;
  55. return EFI_SUCCESS;
  56. }
  57. /* The EFI loaded_image interface for the image executed via "bootefi" */
  58. static struct efi_loaded_image loaded_image_info = {
  59. .device_handle = bootefi_device_path,
  60. .file_path = bootefi_image_path,
  61. };
  62. /* The EFI object struct for the image executed via "bootefi" */
  63. static struct efi_object loaded_image_info_obj = {
  64. .handle = &loaded_image_info,
  65. .protocols = {
  66. {
  67. /*
  68. * When asking for the loaded_image interface, just
  69. * return handle which points to loaded_image_info
  70. */
  71. .guid = &efi_guid_loaded_image,
  72. .open = &efi_return_handle,
  73. },
  74. {
  75. /*
  76. * When asking for the device path interface, return
  77. * bootefi_device_path
  78. */
  79. .guid = &efi_guid_device_path,
  80. .open = &bootefi_open_dp,
  81. },
  82. },
  83. };
  84. /* The EFI object struct for the device the "bootefi" image was loaded from */
  85. static struct efi_object bootefi_device_obj = {
  86. .handle = bootefi_device_path,
  87. .protocols = {
  88. {
  89. /* When asking for the device path interface, return
  90. * bootefi_device_path */
  91. .guid = &efi_guid_device_path,
  92. .open = &bootefi_open_dp,
  93. }
  94. },
  95. };
  96. static void *copy_fdt(void *fdt)
  97. {
  98. u64 fdt_size = fdt_totalsize(fdt);
  99. unsigned long fdt_ram_start = -1L, fdt_pages;
  100. u64 new_fdt_addr;
  101. void *new_fdt;
  102. int i;
  103. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  104. u64 ram_start = gd->bd->bi_dram[i].start;
  105. u64 ram_size = gd->bd->bi_dram[i].size;
  106. if (!ram_size)
  107. continue;
  108. if (ram_start < fdt_ram_start)
  109. fdt_ram_start = ram_start;
  110. }
  111. /* Give us at least 4kb breathing room */
  112. fdt_size = ALIGN(fdt_size + 4096, 4096);
  113. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  114. /* Safe fdt location is at 128MB */
  115. new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
  116. if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
  117. &new_fdt_addr) != EFI_SUCCESS) {
  118. /* If we can't put it there, put it somewhere */
  119. new_fdt_addr = (ulong)memalign(4096, fdt_size);
  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. /*
  127. * Load an EFI payload into a newly allocated piece of memory, register all
  128. * EFI objects it would want to access and jump to it.
  129. */
  130. static unsigned long do_bootefi_exec(void *efi, void *fdt)
  131. {
  132. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  133. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  134. bootm_headers_t img = { 0 };
  135. /*
  136. * gd lives in a fixed register which may get clobbered while we execute
  137. * the payload. So save it here and restore it on every callback entry
  138. */
  139. efi_save_gd();
  140. if (fdt && !fdt_check_header(fdt)) {
  141. /* Prepare fdt for payload */
  142. fdt = copy_fdt(fdt);
  143. if (image_setup_libfdt(&img, fdt, 0, NULL)) {
  144. printf("ERROR: Failed to process device tree\n");
  145. return -EINVAL;
  146. }
  147. /* Link to it in the efi tables */
  148. systab.tables[0].guid = EFI_FDT_GUID;
  149. systab.tables[0].table = fdt;
  150. systab.nr_tables = 1;
  151. /* And reserve the space in the memory map */
  152. fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
  153. fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
  154. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  155. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  156. /* Give a bootloader the chance to modify the device tree */
  157. fdt_pages += 2;
  158. efi_add_memory_map(fdt_start, fdt_pages,
  159. EFI_BOOT_SERVICES_DATA, true);
  160. } else {
  161. printf("WARNING: Invalid device tree, expect boot to fail\n");
  162. systab.nr_tables = 0;
  163. }
  164. /* Load the EFI payload */
  165. entry = efi_load_pe(efi, &loaded_image_info);
  166. if (!entry)
  167. return -ENOENT;
  168. /* Initialize and populate EFI object list */
  169. INIT_LIST_HEAD(&efi_obj_list);
  170. list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
  171. list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
  172. #ifdef CONFIG_PARTITIONS
  173. efi_disk_register();
  174. #endif
  175. #ifdef CONFIG_LCD
  176. efi_gop_register();
  177. #endif
  178. #ifdef CONFIG_NET
  179. void *nethandle = loaded_image_info.device_handle;
  180. efi_net_register(&nethandle);
  181. if (!memcmp(bootefi_device_path[0].str, "N\0e\0t", 6))
  182. loaded_image_info.device_handle = nethandle;
  183. #endif
  184. /* Call our payload! */
  185. debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  186. if (setjmp(&loaded_image_info.exit_jmp)) {
  187. efi_status_t status = loaded_image_info.exit_status;
  188. return status == EFI_SUCCESS ? 0 : -EINVAL;
  189. }
  190. return entry(&loaded_image_info, &systab);
  191. }
  192. /* Interpreter command to boot an arbitrary EFI image from memory */
  193. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  194. {
  195. char *saddr, *sfdt;
  196. unsigned long addr, fdt_addr = 0;
  197. int r = 0;
  198. if (argc < 2)
  199. return CMD_RET_USAGE;
  200. saddr = argv[1];
  201. addr = simple_strtoul(saddr, NULL, 16);
  202. if (argc > 2) {
  203. sfdt = argv[2];
  204. fdt_addr = simple_strtoul(sfdt, NULL, 16);
  205. }
  206. printf("## Starting EFI application at 0x%08lx ...\n", addr);
  207. r = do_bootefi_exec((void *)addr, (void*)fdt_addr);
  208. printf("## Application terminated, r = %d\n", r);
  209. if (r != 0)
  210. r = 1;
  211. return r;
  212. }
  213. #ifdef CONFIG_SYS_LONGHELP
  214. static char bootefi_help_text[] =
  215. "<image address> [fdt address]\n"
  216. " - boot EFI payload stored at address <image address>.\n"
  217. " If specified, the device tree located at <fdt address> gets\n"
  218. " exposed as EFI configuration table.\n";
  219. #endif
  220. U_BOOT_CMD(
  221. bootefi, 3, 0, do_bootefi,
  222. "Boots an EFI payload from memory",
  223. bootefi_help_text
  224. );
  225. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  226. {
  227. __maybe_unused struct blk_desc *desc;
  228. char devname[32] = { 0 }; /* dp->str is u16[32] long */
  229. char *colon;
  230. #if defined(CONFIG_BLK) || defined(CONFIG_ISO_PARTITION)
  231. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  232. #endif
  233. #ifdef CONFIG_BLK
  234. if (desc) {
  235. snprintf(devname, sizeof(devname), "%s", desc->bdev->name);
  236. } else
  237. #endif
  238. {
  239. /* Assemble the condensed device name we use in efi_disk.c */
  240. snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
  241. }
  242. colon = strchr(devname, ':');
  243. #ifdef CONFIG_ISO_PARTITION
  244. /* For ISOs we create partition block devices */
  245. if (desc && (desc->type != DEV_TYPE_UNKNOWN) &&
  246. (desc->part_type == PART_TYPE_ISO)) {
  247. if (!colon)
  248. snprintf(devname, sizeof(devname), "%s:1", devname);
  249. colon = NULL;
  250. }
  251. #endif
  252. if (colon)
  253. *colon = '\0';
  254. /* Patch bootefi_device_path to the target device */
  255. memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str));
  256. ascii2unicode(bootefi_device_path[0].str, devname);
  257. /* Patch bootefi_image_path to the target file path */
  258. memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
  259. if (strcmp(dev, "Net")) {
  260. /* Add leading / to fs paths, because they're absolute */
  261. snprintf(devname, sizeof(devname), "/%s", path);
  262. } else {
  263. snprintf(devname, sizeof(devname), "%s", path);
  264. }
  265. ascii2unicode(bootefi_image_path[0].str, devname);
  266. }