efi_image_loader.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. * @obj: EFI object
  45. * @image: loaded image
  46. * @pc: program counter (use NULL to suppress offset output)
  47. * @return: status code
  48. */
  49. static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
  50. struct efi_loaded_image *image,
  51. void *pc)
  52. {
  53. printf("UEFI image");
  54. printf(" [0x%p:0x%p]",
  55. obj->reloc_base, obj->reloc_base + obj->reloc_size - 1);
  56. if (pc && pc >= obj->reloc_base &&
  57. pc < obj->reloc_base + obj->reloc_size)
  58. printf(" pc=0x%zx", pc - obj->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. (struct efi_loaded_image_obj *)efiobj,
  78. handler->protocol_interface, pc);
  79. }
  80. }
  81. }
  82. }
  83. static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  84. unsigned long rel_size, void *efi_reloc,
  85. unsigned long pref_address)
  86. {
  87. unsigned long delta = (unsigned long)efi_reloc - pref_address;
  88. const IMAGE_BASE_RELOCATION *end;
  89. int i;
  90. if (delta == 0)
  91. return EFI_SUCCESS;
  92. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  93. while (rel < end - 1 && rel->SizeOfBlock) {
  94. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  95. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  96. while (i--) {
  97. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  98. rel->VirtualAddress;
  99. int type = *relocs >> EFI_PAGE_SHIFT;
  100. uint64_t *x64 = efi_reloc + offset;
  101. uint32_t *x32 = efi_reloc + offset;
  102. uint16_t *x16 = efi_reloc + offset;
  103. switch (type) {
  104. case IMAGE_REL_BASED_ABSOLUTE:
  105. break;
  106. case IMAGE_REL_BASED_HIGH:
  107. *x16 += ((uint32_t)delta) >> 16;
  108. break;
  109. case IMAGE_REL_BASED_LOW:
  110. *x16 += (uint16_t)delta;
  111. break;
  112. case IMAGE_REL_BASED_HIGHLOW:
  113. *x32 += (uint32_t)delta;
  114. break;
  115. case IMAGE_REL_BASED_DIR64:
  116. *x64 += (uint64_t)delta;
  117. break;
  118. #ifdef __riscv
  119. case IMAGE_REL_BASED_RISCV_HI20:
  120. *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
  121. (*x32 & 0x00000fff);
  122. break;
  123. case IMAGE_REL_BASED_RISCV_LOW12I:
  124. case IMAGE_REL_BASED_RISCV_LOW12S:
  125. /* We know that we're 4k aligned */
  126. if (delta & 0xfff) {
  127. printf("Unsupported reloc offset\n");
  128. return EFI_LOAD_ERROR;
  129. }
  130. break;
  131. #endif
  132. default:
  133. printf("Unknown Relocation off %x type %x\n",
  134. offset, type);
  135. return EFI_LOAD_ERROR;
  136. }
  137. relocs++;
  138. }
  139. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  140. }
  141. return EFI_SUCCESS;
  142. }
  143. void __weak invalidate_icache_all(void)
  144. {
  145. /* If the system doesn't support icache_all flush, cross our fingers */
  146. }
  147. /*
  148. * Determine the memory types to be used for code and data.
  149. *
  150. * @loaded_image_info image descriptor
  151. * @image_type field Subsystem of the optional header for
  152. * Windows specific field
  153. */
  154. static void efi_set_code_and_data_type(
  155. struct efi_loaded_image *loaded_image_info,
  156. uint16_t image_type)
  157. {
  158. switch (image_type) {
  159. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  160. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  161. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  162. break;
  163. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  164. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  165. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  166. break;
  167. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  168. case IMAGE_SUBSYSTEM_EFI_ROM:
  169. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  170. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  171. break;
  172. default:
  173. printf("%s: invalid image type: %u\n", __func__, image_type);
  174. /* Let's assume it is an application */
  175. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  176. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  177. break;
  178. }
  179. }
  180. /*
  181. * This function loads all sections from a PE binary into a newly reserved
  182. * piece of memory. On successful load it then returns the entry point for
  183. * the binary. Otherwise NULL.
  184. */
  185. void *efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,
  186. struct efi_loaded_image *loaded_image_info)
  187. {
  188. IMAGE_NT_HEADERS32 *nt;
  189. IMAGE_DOS_HEADER *dos;
  190. IMAGE_SECTION_HEADER *sections;
  191. int num_sections;
  192. void *efi_reloc;
  193. int i;
  194. const IMAGE_BASE_RELOCATION *rel;
  195. unsigned long rel_size;
  196. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  197. void *entry;
  198. uint64_t image_base;
  199. uint64_t image_size;
  200. unsigned long virt_size = 0;
  201. int supported = 0;
  202. dos = efi;
  203. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  204. printf("%s: Invalid DOS Signature\n", __func__);
  205. return NULL;
  206. }
  207. nt = (void *) ((char *)efi + dos->e_lfanew);
  208. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  209. printf("%s: Invalid NT Signature\n", __func__);
  210. return NULL;
  211. }
  212. for (i = 0; machines[i]; i++)
  213. if (machines[i] == nt->FileHeader.Machine) {
  214. supported = 1;
  215. break;
  216. }
  217. if (!supported) {
  218. printf("%s: Machine type 0x%04x is not supported\n",
  219. __func__, nt->FileHeader.Machine);
  220. return NULL;
  221. }
  222. /* Calculate upper virtual address boundary */
  223. num_sections = nt->FileHeader.NumberOfSections;
  224. sections = (void *)&nt->OptionalHeader +
  225. nt->FileHeader.SizeOfOptionalHeader;
  226. for (i = num_sections - 1; i >= 0; i--) {
  227. IMAGE_SECTION_HEADER *sec = &sections[i];
  228. virt_size = max_t(unsigned long, virt_size,
  229. sec->VirtualAddress + sec->Misc.VirtualSize);
  230. }
  231. /* Read 32/64bit specific header bits */
  232. if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  233. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  234. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  235. image_base = opt->ImageBase;
  236. image_size = opt->SizeOfImage;
  237. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  238. efi_reloc = efi_alloc(virt_size,
  239. loaded_image_info->image_code_type);
  240. if (!efi_reloc) {
  241. printf("%s: Could not allocate %lu bytes\n",
  242. __func__, virt_size);
  243. return NULL;
  244. }
  245. entry = efi_reloc + opt->AddressOfEntryPoint;
  246. rel_size = opt->DataDirectory[rel_idx].Size;
  247. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  248. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  249. } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  250. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  251. image_base = opt->ImageBase;
  252. image_size = opt->SizeOfImage;
  253. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  254. efi_reloc = efi_alloc(virt_size,
  255. loaded_image_info->image_code_type);
  256. if (!efi_reloc) {
  257. printf("%s: Could not allocate %lu bytes\n",
  258. __func__, virt_size);
  259. return NULL;
  260. }
  261. entry = efi_reloc + opt->AddressOfEntryPoint;
  262. rel_size = opt->DataDirectory[rel_idx].Size;
  263. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  264. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  265. } else {
  266. printf("%s: Invalid optional header magic %x\n", __func__,
  267. nt->OptionalHeader.Magic);
  268. return NULL;
  269. }
  270. /* Load sections into RAM */
  271. for (i = num_sections - 1; i >= 0; i--) {
  272. IMAGE_SECTION_HEADER *sec = &sections[i];
  273. memset(efi_reloc + sec->VirtualAddress, 0,
  274. sec->Misc.VirtualSize);
  275. memcpy(efi_reloc + sec->VirtualAddress,
  276. efi + sec->PointerToRawData,
  277. sec->SizeOfRawData);
  278. }
  279. /* Run through relocations */
  280. if (efi_loader_relocate(rel, rel_size, efi_reloc,
  281. (unsigned long)image_base) != EFI_SUCCESS) {
  282. efi_free_pages((uintptr_t) efi_reloc,
  283. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  284. return NULL;
  285. }
  286. /* Flush cache */
  287. flush_cache((ulong)efi_reloc,
  288. ALIGN(virt_size, EFI_CACHELINE_SIZE));
  289. invalidate_icache_all();
  290. /* Populate the loaded image interface bits */
  291. loaded_image_info->image_base = efi;
  292. loaded_image_info->image_size = image_size;
  293. handle->reloc_base = efi_reloc;
  294. handle->reloc_size = virt_size;
  295. return entry;
  296. }