bootefi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /*
  15. * When booting using the "bootefi" command, we don't know which
  16. * physical device the file came from. So we create a pseudo-device
  17. * called "bootefi" with the device path /bootefi.
  18. *
  19. * In addition to the originating device we also declare the file path
  20. * of "bootefi" based loads to be /bootefi.
  21. */
  22. static struct efi_device_path_file_path bootefi_image_path[] = {
  23. {
  24. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  25. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  26. .dp.length = sizeof(bootefi_image_path[0]),
  27. .str = { 'b','o','o','t','e','f','i' },
  28. }, {
  29. .dp.type = DEVICE_PATH_TYPE_END,
  30. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  31. .dp.length = sizeof(bootefi_image_path[0]),
  32. }
  33. };
  34. static struct efi_device_path_file_path bootefi_device_path[] = {
  35. {
  36. .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
  37. .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
  38. .dp.length = sizeof(bootefi_image_path[0]),
  39. .str = { 'b','o','o','t','e','f','i' },
  40. }, {
  41. .dp.type = DEVICE_PATH_TYPE_END,
  42. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  43. .dp.length = sizeof(bootefi_image_path[0]),
  44. }
  45. };
  46. static efi_status_t bootefi_open_dp(void *handle, efi_guid_t *protocol,
  47. void **protocol_interface, void *agent_handle,
  48. void *controller_handle, uint32_t attributes)
  49. {
  50. *protocol_interface = bootefi_device_path;
  51. return EFI_SUCCESS;
  52. }
  53. /* The EFI loaded_image interface for the image executed via "bootefi" */
  54. static struct efi_loaded_image loaded_image_info = {
  55. .device_handle = bootefi_device_path,
  56. .file_path = bootefi_image_path,
  57. };
  58. /* The EFI object struct for the image executed via "bootefi" */
  59. static struct efi_object loaded_image_info_obj = {
  60. .handle = &loaded_image_info,
  61. .protocols = {
  62. {
  63. /*
  64. * When asking for the loaded_image interface, just
  65. * return handle which points to loaded_image_info
  66. */
  67. .guid = &efi_guid_loaded_image,
  68. .open = &efi_return_handle,
  69. },
  70. {
  71. /*
  72. * When asking for the device path interface, return
  73. * bootefi_device_path
  74. */
  75. .guid = &efi_guid_device_path,
  76. .open = &bootefi_open_dp,
  77. },
  78. },
  79. };
  80. /* The EFI object struct for the device the "bootefi" image was loaded from */
  81. static struct efi_object bootefi_device_obj = {
  82. .handle = bootefi_device_path,
  83. .protocols = {
  84. {
  85. /* When asking for the device path interface, return
  86. * bootefi_device_path */
  87. .guid = &efi_guid_device_path,
  88. .open = &bootefi_open_dp,
  89. }
  90. },
  91. };
  92. /*
  93. * Load an EFI payload into a newly allocated piece of memory, register all
  94. * EFI objects it would want to access and jump to it.
  95. */
  96. static unsigned long do_bootefi_exec(void *efi)
  97. {
  98. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  99. ulong fdt_pages, fdt_size, fdt_start, fdt_end;
  100. bootm_headers_t img = { 0 };
  101. /*
  102. * gd lives in a fixed register which may get clobbered while we execute
  103. * the payload. So save it here and restore it on every callback entry
  104. */
  105. efi_save_gd();
  106. /* Update system table to point to our currently loaded FDT */
  107. if (working_fdt) {
  108. /* Prepare fdt for payload */
  109. if (image_setup_libfdt(&img, working_fdt, 0, NULL)) {
  110. printf("ERROR: Failed to process device tree\n");
  111. return -EINVAL;
  112. }
  113. /* Link to it in the efi tables */
  114. systab.tables[0].guid = EFI_FDT_GUID;
  115. systab.tables[0].table = working_fdt;
  116. systab.nr_tables = 1;
  117. /* And reserve the space in the memory map */
  118. fdt_start = ((ulong)working_fdt) & ~EFI_PAGE_MASK;
  119. fdt_end = ((ulong)working_fdt) + fdt_totalsize(working_fdt);
  120. fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
  121. fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
  122. /* Give a bootloader the chance to modify the device tree */
  123. fdt_pages += 2;
  124. efi_add_memory_map(fdt_start, fdt_pages,
  125. EFI_BOOT_SERVICES_DATA, true);
  126. } else {
  127. printf("WARNING: No device tree loaded, expect boot to fail\n");
  128. systab.nr_tables = 0;
  129. }
  130. /* Load the EFI payload */
  131. entry = efi_load_pe(efi, &loaded_image_info);
  132. if (!entry)
  133. return -ENOENT;
  134. /* Initialize and populate EFI object list */
  135. INIT_LIST_HEAD(&efi_obj_list);
  136. list_add_tail(&loaded_image_info_obj.link, &efi_obj_list);
  137. list_add_tail(&bootefi_device_obj.link, &efi_obj_list);
  138. #ifdef CONFIG_PARTITIONS
  139. efi_disk_register();
  140. #endif
  141. #ifdef CONFIG_LCD
  142. efi_gop_register();
  143. #endif
  144. /* Call our payload! */
  145. #ifdef DEBUG_EFI
  146. printf("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
  147. #endif
  148. return entry(&loaded_image_info, &systab);
  149. }
  150. /* Interpreter command to boot an arbitrary EFI image from memory */
  151. static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  152. {
  153. char *saddr;
  154. unsigned long addr;
  155. int r = 0;
  156. if (argc < 2)
  157. return 1;
  158. saddr = argv[1];
  159. addr = simple_strtoul(saddr, NULL, 16);
  160. printf("## Starting EFI application at 0x%08lx ...\n", addr);
  161. r = do_bootefi_exec((void *)addr);
  162. printf("## Application terminated, r = %d\n", r);
  163. if (r != 0)
  164. r = 1;
  165. return r;
  166. }
  167. #ifdef CONFIG_SYS_LONGHELP
  168. static char bootefi_help_text[] =
  169. "<image address>\n"
  170. " - boot EFI payload stored at address <image address>\n"
  171. "\n"
  172. "Since most EFI payloads want to have a device tree provided, please\n"
  173. "make sure you load a device tree using the fdt addr command before\n"
  174. "executing bootefi.\n";
  175. #endif
  176. U_BOOT_CMD(
  177. bootefi, 2, 0, do_bootefi,
  178. "Boots an EFI payload from memory\n",
  179. bootefi_help_text
  180. );
  181. void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
  182. {
  183. __maybe_unused struct blk_desc *desc;
  184. char devname[16] = { 0 }; /* dp->str is u16[16] long */
  185. char *colon;
  186. /* Assemble the condensed device name we use in efi_disk.c */
  187. snprintf(devname, sizeof(devname), "%s%s", dev, devnr);
  188. colon = strchr(devname, ':');
  189. #ifdef CONFIG_ISO_PARTITION
  190. /* For ISOs we create partition block devices */
  191. desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
  192. if (desc && (desc->type != DEV_TYPE_UNKNOWN) &&
  193. (desc->part_type == PART_TYPE_ISO)) {
  194. if (!colon)
  195. snprintf(devname, sizeof(devname), "%s%s:1", dev,
  196. devnr);
  197. colon = NULL;
  198. }
  199. #endif
  200. if (colon)
  201. *colon = '\0';
  202. /* Patch bootefi_device_path to the target device */
  203. memset(bootefi_device_path[0].str, 0, sizeof(bootefi_device_path[0].str));
  204. ascii2unicode(bootefi_device_path[0].str, devname);
  205. /* Patch bootefi_image_path to the target file path */
  206. memset(bootefi_image_path[0].str, 0, sizeof(bootefi_image_path[0].str));
  207. snprintf(devname, sizeof(devname), "%s", path);
  208. ascii2unicode(bootefi_image_path[0].str, devname);
  209. }