efi_image_loader.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  13. const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
  14. const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
  15. const efi_guid_t efi_simple_file_system_protocol_guid =
  16. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  17. const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
  18. static int machines[] = {
  19. #if defined(CONFIG_ARM64)
  20. IMAGE_FILE_MACHINE_ARM64,
  21. #elif defined(CONFIG_ARM)
  22. IMAGE_FILE_MACHINE_ARM,
  23. IMAGE_FILE_MACHINE_THUMB,
  24. IMAGE_FILE_MACHINE_ARMNT,
  25. #endif
  26. #if defined(CONFIG_X86_64)
  27. IMAGE_FILE_MACHINE_AMD64,
  28. #elif defined(CONFIG_X86)
  29. IMAGE_FILE_MACHINE_I386,
  30. #endif
  31. #if defined(CONFIG_CPU_RISCV_32)
  32. IMAGE_FILE_MACHINE_RISCV32,
  33. #endif
  34. #if defined(CONFIG_CPU_RISCV_64)
  35. IMAGE_FILE_MACHINE_RISCV64,
  36. #endif
  37. 0 };
  38. /*
  39. * Print information about a loaded image.
  40. *
  41. * If the program counter is located within the image the offset to the base
  42. * address is shown.
  43. *
  44. * @image: loaded image
  45. * @pc: program counter (use NULL to suppress offset output)
  46. * @return: status code
  47. */
  48. efi_status_t efi_print_image_info(struct efi_loaded_image *image, void *pc)
  49. {
  50. if (!image)
  51. return EFI_INVALID_PARAMETER;
  52. printf("UEFI image");
  53. printf(" [0x%p:0x%p]",
  54. image->reloc_base, image->reloc_base + image->reloc_size - 1);
  55. if (pc && pc >= image->reloc_base &&
  56. pc < image->reloc_base + image->reloc_size)
  57. printf(" pc=0x%zx", pc - image->reloc_base);
  58. if (image->file_path)
  59. printf(" '%pD'", image->file_path);
  60. printf("\n");
  61. return EFI_SUCCESS;
  62. }
  63. /*
  64. * Print information about all loaded images.
  65. *
  66. * @pc: program counter (use NULL to suppress offset output)
  67. */
  68. void efi_print_image_infos(void *pc)
  69. {
  70. struct efi_object *efiobj;
  71. struct efi_handler *handler;
  72. list_for_each_entry(efiobj, &efi_obj_list, link) {
  73. list_for_each_entry(handler, &efiobj->protocols, link) {
  74. if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
  75. efi_print_image_info(
  76. handler->protocol_interface, pc);
  77. }
  78. }
  79. }
  80. }
  81. static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  82. unsigned long rel_size, void *efi_reloc,
  83. unsigned long pref_address)
  84. {
  85. unsigned long delta = (unsigned long)efi_reloc - pref_address;
  86. const IMAGE_BASE_RELOCATION *end;
  87. int i;
  88. if (delta == 0)
  89. return EFI_SUCCESS;
  90. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  91. while (rel < end - 1 && rel->SizeOfBlock) {
  92. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  93. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  94. while (i--) {
  95. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  96. rel->VirtualAddress;
  97. int type = *relocs >> EFI_PAGE_SHIFT;
  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_base;
  182. uint64_t image_size;
  183. unsigned long virt_size = 0;
  184. int supported = 0;
  185. dos = efi;
  186. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  187. printf("%s: Invalid DOS Signature\n", __func__);
  188. return NULL;
  189. }
  190. nt = (void *) ((char *)efi + dos->e_lfanew);
  191. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  192. printf("%s: Invalid NT Signature\n", __func__);
  193. return NULL;
  194. }
  195. for (i = 0; machines[i]; i++)
  196. if (machines[i] == nt->FileHeader.Machine) {
  197. supported = 1;
  198. break;
  199. }
  200. if (!supported) {
  201. printf("%s: Machine type 0x%04x is not supported\n",
  202. __func__, nt->FileHeader.Machine);
  203. return NULL;
  204. }
  205. /* Calculate upper virtual address boundary */
  206. num_sections = nt->FileHeader.NumberOfSections;
  207. sections = (void *)&nt->OptionalHeader +
  208. nt->FileHeader.SizeOfOptionalHeader;
  209. for (i = num_sections - 1; i >= 0; i--) {
  210. IMAGE_SECTION_HEADER *sec = &sections[i];
  211. virt_size = max_t(unsigned long, virt_size,
  212. sec->VirtualAddress + sec->Misc.VirtualSize);
  213. }
  214. /* Read 32/64bit specific header bits */
  215. if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  216. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  217. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  218. image_base = opt->ImageBase;
  219. image_size = opt->SizeOfImage;
  220. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  221. efi_reloc = efi_alloc(virt_size,
  222. loaded_image_info->image_code_type);
  223. if (!efi_reloc) {
  224. printf("%s: Could not allocate %lu bytes\n",
  225. __func__, virt_size);
  226. return NULL;
  227. }
  228. entry = efi_reloc + opt->AddressOfEntryPoint;
  229. rel_size = opt->DataDirectory[rel_idx].Size;
  230. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  231. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  232. } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  233. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  234. image_base = opt->ImageBase;
  235. image_size = opt->SizeOfImage;
  236. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  237. efi_reloc = efi_alloc(virt_size,
  238. loaded_image_info->image_code_type);
  239. if (!efi_reloc) {
  240. printf("%s: Could not allocate %lu bytes\n",
  241. __func__, virt_size);
  242. return NULL;
  243. }
  244. entry = efi_reloc + opt->AddressOfEntryPoint;
  245. rel_size = opt->DataDirectory[rel_idx].Size;
  246. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  247. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  248. } else {
  249. printf("%s: Invalid optional header magic %x\n", __func__,
  250. nt->OptionalHeader.Magic);
  251. return NULL;
  252. }
  253. /* Load sections into RAM */
  254. for (i = num_sections - 1; i >= 0; i--) {
  255. IMAGE_SECTION_HEADER *sec = &sections[i];
  256. memset(efi_reloc + sec->VirtualAddress, 0,
  257. sec->Misc.VirtualSize);
  258. memcpy(efi_reloc + sec->VirtualAddress,
  259. efi + sec->PointerToRawData,
  260. sec->SizeOfRawData);
  261. }
  262. /* Run through relocations */
  263. if (efi_loader_relocate(rel, rel_size, efi_reloc,
  264. (unsigned long)image_base) != EFI_SUCCESS) {
  265. efi_free_pages((uintptr_t) efi_reloc,
  266. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  267. return NULL;
  268. }
  269. /* Flush cache */
  270. flush_cache((ulong)efi_reloc,
  271. ALIGN(virt_size, EFI_CACHELINE_SIZE));
  272. invalidate_icache_all();
  273. /* Populate the loaded image interface bits */
  274. loaded_image_info->image_base = efi;
  275. loaded_image_info->image_size = image_size;
  276. loaded_image_info->reloc_base = efi_reloc;
  277. loaded_image_info->reloc_size = virt_size;
  278. return entry;
  279. }