efi_image_loader.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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(__aarch64__)
  20. IMAGE_FILE_MACHINE_ARM64,
  21. #elif defined(__arm__)
  22. IMAGE_FILE_MACHINE_ARM,
  23. IMAGE_FILE_MACHINE_THUMB,
  24. IMAGE_FILE_MACHINE_ARMNT,
  25. #endif
  26. #if defined(__x86_64__)
  27. IMAGE_FILE_MACHINE_AMD64,
  28. #elif defined(__i386__)
  29. IMAGE_FILE_MACHINE_I386,
  30. #endif
  31. #if defined(__riscv) && (__riscv_xlen == 32)
  32. IMAGE_FILE_MACHINE_RISCV32,
  33. #endif
  34. #if defined(__riscv) && (__riscv_xlen == 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. #ifdef __riscv
  117. case IMAGE_REL_BASED_RISCV_HI20:
  118. *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
  119. (*x32 & 0x00000fff);
  120. break;
  121. case IMAGE_REL_BASED_RISCV_LOW12I:
  122. case IMAGE_REL_BASED_RISCV_LOW12S:
  123. /* We know that we're 4k aligned */
  124. if (delta & 0xfff) {
  125. printf("Unsupported reloc offset\n");
  126. return EFI_LOAD_ERROR;
  127. }
  128. break;
  129. #endif
  130. default:
  131. printf("Unknown Relocation off %x type %x\n",
  132. offset, type);
  133. return EFI_LOAD_ERROR;
  134. }
  135. relocs++;
  136. }
  137. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  138. }
  139. return EFI_SUCCESS;
  140. }
  141. void __weak invalidate_icache_all(void)
  142. {
  143. /* If the system doesn't support icache_all flush, cross our fingers */
  144. }
  145. /*
  146. * Determine the memory types to be used for code and data.
  147. *
  148. * @loaded_image_info image descriptor
  149. * @image_type field Subsystem of the optional header for
  150. * Windows specific field
  151. */
  152. static void efi_set_code_and_data_type(
  153. struct efi_loaded_image *loaded_image_info,
  154. uint16_t image_type)
  155. {
  156. switch (image_type) {
  157. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  158. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  159. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  160. break;
  161. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  162. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  163. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  164. break;
  165. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  166. case IMAGE_SUBSYSTEM_EFI_ROM:
  167. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  168. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  169. break;
  170. default:
  171. printf("%s: invalid image type: %u\n", __func__, image_type);
  172. /* Let's assume it is an application */
  173. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  174. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  175. break;
  176. }
  177. }
  178. /*
  179. * This function loads all sections from a PE binary into a newly reserved
  180. * piece of memory. On successful load it then returns the entry point for
  181. * the binary. Otherwise NULL.
  182. */
  183. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  184. {
  185. IMAGE_NT_HEADERS32 *nt;
  186. IMAGE_DOS_HEADER *dos;
  187. IMAGE_SECTION_HEADER *sections;
  188. int num_sections;
  189. void *efi_reloc;
  190. int i;
  191. const IMAGE_BASE_RELOCATION *rel;
  192. unsigned long rel_size;
  193. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  194. void *entry;
  195. uint64_t image_base;
  196. uint64_t image_size;
  197. unsigned long virt_size = 0;
  198. int supported = 0;
  199. dos = efi;
  200. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  201. printf("%s: Invalid DOS Signature\n", __func__);
  202. return NULL;
  203. }
  204. nt = (void *) ((char *)efi + dos->e_lfanew);
  205. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  206. printf("%s: Invalid NT Signature\n", __func__);
  207. return NULL;
  208. }
  209. for (i = 0; machines[i]; i++)
  210. if (machines[i] == nt->FileHeader.Machine) {
  211. supported = 1;
  212. break;
  213. }
  214. if (!supported) {
  215. printf("%s: Machine type 0x%04x is not supported\n",
  216. __func__, nt->FileHeader.Machine);
  217. return NULL;
  218. }
  219. /* Calculate upper virtual address boundary */
  220. num_sections = nt->FileHeader.NumberOfSections;
  221. sections = (void *)&nt->OptionalHeader +
  222. nt->FileHeader.SizeOfOptionalHeader;
  223. for (i = num_sections - 1; i >= 0; i--) {
  224. IMAGE_SECTION_HEADER *sec = &sections[i];
  225. virt_size = max_t(unsigned long, virt_size,
  226. sec->VirtualAddress + sec->Misc.VirtualSize);
  227. }
  228. /* Read 32/64bit specific header bits */
  229. if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  230. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  231. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  232. image_base = opt->ImageBase;
  233. image_size = opt->SizeOfImage;
  234. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  235. efi_reloc = efi_alloc(virt_size,
  236. loaded_image_info->image_code_type);
  237. if (!efi_reloc) {
  238. printf("%s: Could not allocate %lu bytes\n",
  239. __func__, virt_size);
  240. return NULL;
  241. }
  242. entry = efi_reloc + opt->AddressOfEntryPoint;
  243. rel_size = opt->DataDirectory[rel_idx].Size;
  244. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  245. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  246. } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  247. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  248. image_base = opt->ImageBase;
  249. image_size = opt->SizeOfImage;
  250. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  251. efi_reloc = efi_alloc(virt_size,
  252. loaded_image_info->image_code_type);
  253. if (!efi_reloc) {
  254. printf("%s: Could not allocate %lu bytes\n",
  255. __func__, virt_size);
  256. return NULL;
  257. }
  258. entry = efi_reloc + opt->AddressOfEntryPoint;
  259. rel_size = opt->DataDirectory[rel_idx].Size;
  260. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  261. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  262. } else {
  263. printf("%s: Invalid optional header magic %x\n", __func__,
  264. nt->OptionalHeader.Magic);
  265. return NULL;
  266. }
  267. /* Load sections into RAM */
  268. for (i = num_sections - 1; i >= 0; i--) {
  269. IMAGE_SECTION_HEADER *sec = &sections[i];
  270. memset(efi_reloc + sec->VirtualAddress, 0,
  271. sec->Misc.VirtualSize);
  272. memcpy(efi_reloc + sec->VirtualAddress,
  273. efi + sec->PointerToRawData,
  274. sec->SizeOfRawData);
  275. }
  276. /* Run through relocations */
  277. if (efi_loader_relocate(rel, rel_size, efi_reloc,
  278. (unsigned long)image_base) != EFI_SUCCESS) {
  279. efi_free_pages((uintptr_t) efi_reloc,
  280. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  281. return NULL;
  282. }
  283. /* Flush cache */
  284. flush_cache((ulong)efi_reloc,
  285. ALIGN(virt_size, EFI_CACHELINE_SIZE));
  286. invalidate_icache_all();
  287. /* Populate the loaded image interface bits */
  288. loaded_image_info->image_base = efi;
  289. loaded_image_info->image_size = image_size;
  290. loaded_image_info->reloc_base = efi_reloc;
  291. loaded_image_info->reloc_size = virt_size;
  292. return entry;
  293. }