bootefi.c 7.9 KB

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