efi_image_loader.c 8.4 KB

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