efi_image_loader.c 8.4 KB

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