efi_image_loader.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * EFI image loader
  3. *
  4. * based partly on wine code
  5. *
  6. * Copyright (c) 2016 Alexander Graf
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <efi_loader.h>
  12. #include <pe.h>
  13. #include <asm/global_data.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  16. const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
  17. const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
  18. const efi_guid_t efi_simple_file_system_protocol_guid =
  19. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  20. const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
  21. /*
  22. * Print information about a loaded image.
  23. *
  24. * If the program counter is located within the image the offset to the base
  25. * address is shown.
  26. *
  27. * @image: loaded image
  28. * @pc: program counter (use NULL to suppress offset output)
  29. * @return: status code
  30. */
  31. efi_status_t efi_print_image_info(struct efi_loaded_image *image, void *pc)
  32. {
  33. if (!image)
  34. return EFI_INVALID_PARAMETER;
  35. printf("UEFI image");
  36. printf(" [0x%p:0x%p]",
  37. image->reloc_base, image->reloc_base + image->reloc_size - 1);
  38. if (pc && pc >= image->reloc_base &&
  39. pc < image->reloc_base + image->reloc_size)
  40. printf(" pc=0x%zx", pc - image->reloc_base);
  41. if (image->file_path)
  42. printf(" '%pD'", image->file_path);
  43. printf("\n");
  44. return EFI_SUCCESS;
  45. }
  46. /*
  47. * Print information about all loaded images.
  48. *
  49. * @pc: program counter (use NULL to suppress offset output)
  50. */
  51. void efi_print_image_infos(void *pc)
  52. {
  53. struct efi_object *efiobj;
  54. struct efi_handler *handler;
  55. list_for_each_entry(efiobj, &efi_obj_list, link) {
  56. list_for_each_entry(handler, &efiobj->protocols, link) {
  57. if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
  58. efi_print_image_info(
  59. handler->protocol_interface, pc);
  60. }
  61. }
  62. }
  63. }
  64. static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  65. unsigned long rel_size, void *efi_reloc)
  66. {
  67. const IMAGE_BASE_RELOCATION *end;
  68. int i;
  69. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  70. while (rel < end - 1 && rel->SizeOfBlock) {
  71. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  72. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  73. while (i--) {
  74. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  75. rel->VirtualAddress;
  76. int type = *relocs >> EFI_PAGE_SHIFT;
  77. unsigned long delta = (unsigned long)efi_reloc;
  78. uint64_t *x64 = efi_reloc + offset;
  79. uint32_t *x32 = efi_reloc + offset;
  80. uint16_t *x16 = efi_reloc + offset;
  81. switch (type) {
  82. case IMAGE_REL_BASED_ABSOLUTE:
  83. break;
  84. case IMAGE_REL_BASED_HIGH:
  85. *x16 += ((uint32_t)delta) >> 16;
  86. break;
  87. case IMAGE_REL_BASED_LOW:
  88. *x16 += (uint16_t)delta;
  89. break;
  90. case IMAGE_REL_BASED_HIGHLOW:
  91. *x32 += (uint32_t)delta;
  92. break;
  93. case IMAGE_REL_BASED_DIR64:
  94. *x64 += (uint64_t)delta;
  95. break;
  96. default:
  97. printf("Unknown Relocation off %x type %x\n",
  98. offset, type);
  99. return EFI_LOAD_ERROR;
  100. }
  101. relocs++;
  102. }
  103. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  104. }
  105. return EFI_SUCCESS;
  106. }
  107. void __weak invalidate_icache_all(void)
  108. {
  109. /* If the system doesn't support icache_all flush, cross our fingers */
  110. }
  111. /*
  112. * Determine the memory types to be used for code and data.
  113. *
  114. * @loaded_image_info image descriptor
  115. * @image_type field Subsystem of the optional header for
  116. * Windows specific field
  117. */
  118. static void efi_set_code_and_data_type(
  119. struct efi_loaded_image *loaded_image_info,
  120. uint16_t image_type)
  121. {
  122. switch (image_type) {
  123. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  124. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  125. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  126. break;
  127. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  128. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  129. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  130. break;
  131. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  132. case IMAGE_SUBSYSTEM_EFI_ROM:
  133. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  134. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  135. break;
  136. default:
  137. printf("%s: invalid image type: %u\n", __func__, image_type);
  138. /* Let's assume it is an application */
  139. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  140. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  141. break;
  142. }
  143. }
  144. /*
  145. * This function loads all sections from a PE binary into a newly reserved
  146. * piece of memory. On successful load it then returns the entry point for
  147. * the binary. Otherwise NULL.
  148. */
  149. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  150. {
  151. IMAGE_NT_HEADERS32 *nt;
  152. IMAGE_DOS_HEADER *dos;
  153. IMAGE_SECTION_HEADER *sections;
  154. int num_sections;
  155. void *efi_reloc;
  156. int i;
  157. const IMAGE_BASE_RELOCATION *rel;
  158. unsigned long rel_size;
  159. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  160. void *entry;
  161. uint64_t image_size;
  162. unsigned long virt_size = 0;
  163. bool can_run_nt64 = true;
  164. bool can_run_nt32 = true;
  165. #if defined(CONFIG_ARM64)
  166. can_run_nt32 = false;
  167. #elif defined(CONFIG_ARM)
  168. can_run_nt64 = false;
  169. #endif
  170. dos = efi;
  171. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  172. printf("%s: Invalid DOS Signature\n", __func__);
  173. return NULL;
  174. }
  175. nt = (void *) ((char *)efi + dos->e_lfanew);
  176. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  177. printf("%s: Invalid NT Signature\n", __func__);
  178. return NULL;
  179. }
  180. /* Calculate upper virtual address boundary */
  181. num_sections = nt->FileHeader.NumberOfSections;
  182. sections = (void *)&nt->OptionalHeader +
  183. nt->FileHeader.SizeOfOptionalHeader;
  184. for (i = num_sections - 1; i >= 0; i--) {
  185. IMAGE_SECTION_HEADER *sec = &sections[i];
  186. virt_size = max_t(unsigned long, virt_size,
  187. sec->VirtualAddress + sec->Misc.VirtualSize);
  188. }
  189. /* Read 32/64bit specific header bits */
  190. if (can_run_nt64 &&
  191. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
  192. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  193. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  194. image_size = opt->SizeOfImage;
  195. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  196. efi_reloc = efi_alloc(virt_size,
  197. loaded_image_info->image_code_type);
  198. if (!efi_reloc) {
  199. printf("%s: Could not allocate %lu bytes\n",
  200. __func__, virt_size);
  201. return NULL;
  202. }
  203. entry = efi_reloc + opt->AddressOfEntryPoint;
  204. rel_size = opt->DataDirectory[rel_idx].Size;
  205. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  206. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  207. } else if (can_run_nt32 &&
  208. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
  209. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  210. image_size = opt->SizeOfImage;
  211. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  212. efi_reloc = efi_alloc(virt_size,
  213. loaded_image_info->image_code_type);
  214. if (!efi_reloc) {
  215. printf("%s: Could not allocate %lu bytes\n",
  216. __func__, virt_size);
  217. return NULL;
  218. }
  219. entry = efi_reloc + opt->AddressOfEntryPoint;
  220. rel_size = opt->DataDirectory[rel_idx].Size;
  221. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  222. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  223. } else {
  224. printf("%s: Invalid optional header magic %x\n", __func__,
  225. nt->OptionalHeader.Magic);
  226. return NULL;
  227. }
  228. /* Load sections into RAM */
  229. for (i = num_sections - 1; i >= 0; i--) {
  230. IMAGE_SECTION_HEADER *sec = &sections[i];
  231. memset(efi_reloc + sec->VirtualAddress, 0,
  232. sec->Misc.VirtualSize);
  233. memcpy(efi_reloc + sec->VirtualAddress,
  234. efi + sec->PointerToRawData,
  235. sec->SizeOfRawData);
  236. }
  237. /* Run through relocations */
  238. if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
  239. efi_free_pages((uintptr_t) efi_reloc,
  240. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  241. return NULL;
  242. }
  243. /* Flush cache */
  244. flush_cache((ulong)efi_reloc,
  245. ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
  246. invalidate_icache_all();
  247. /* Populate the loaded image interface bits */
  248. loaded_image_info->image_base = efi;
  249. loaded_image_info->image_size = image_size;
  250. loaded_image_info->reloc_base = efi_reloc;
  251. loaded_image_info->reloc_size = virt_size;
  252. return entry;
  253. }