efi_image_loader.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <cpu_func.h>
  11. #include <efi_loader.h>
  12. #include <pe.h>
  13. const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
  14. const efi_guid_t efi_guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
  15. const efi_guid_t efi_guid_loaded_image = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  16. const efi_guid_t efi_guid_loaded_image_device_path =
  17. EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_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(__aarch64__)
  23. IMAGE_FILE_MACHINE_ARM64,
  24. #elif defined(__arm__)
  25. IMAGE_FILE_MACHINE_ARM,
  26. IMAGE_FILE_MACHINE_THUMB,
  27. IMAGE_FILE_MACHINE_ARMNT,
  28. #endif
  29. #if defined(__x86_64__)
  30. IMAGE_FILE_MACHINE_AMD64,
  31. #elif defined(__i386__)
  32. IMAGE_FILE_MACHINE_I386,
  33. #endif
  34. #if defined(__riscv) && (__riscv_xlen == 32)
  35. IMAGE_FILE_MACHINE_RISCV32,
  36. #endif
  37. #if defined(__riscv) && (__riscv_xlen == 64)
  38. IMAGE_FILE_MACHINE_RISCV64,
  39. #endif
  40. 0 };
  41. /**
  42. * efi_print_image_info() - 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. * @obj: EFI object
  48. * @image: loaded image
  49. * @pc: program counter (use NULL to suppress offset output)
  50. * Return: status code
  51. */
  52. static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj,
  53. struct efi_loaded_image *image,
  54. void *pc)
  55. {
  56. printf("UEFI image");
  57. printf(" [0x%p:0x%p]",
  58. image->image_base, image->image_base + image->image_size - 1);
  59. if (pc && pc >= image->image_base &&
  60. pc < image->image_base + image->image_size)
  61. printf(" pc=0x%zx", pc - image->image_base);
  62. if (image->file_path)
  63. printf(" '%pD'", image->file_path);
  64. printf("\n");
  65. return EFI_SUCCESS;
  66. }
  67. /**
  68. * efi_print_image_infos() - print information about all loaded images
  69. *
  70. * @pc: program counter (use NULL to suppress offset output)
  71. */
  72. void efi_print_image_infos(void *pc)
  73. {
  74. struct efi_object *efiobj;
  75. struct efi_handler *handler;
  76. list_for_each_entry(efiobj, &efi_obj_list, link) {
  77. list_for_each_entry(handler, &efiobj->protocols, link) {
  78. if (!guidcmp(handler->guid, &efi_guid_loaded_image)) {
  79. efi_print_image_info(
  80. (struct efi_loaded_image_obj *)efiobj,
  81. handler->protocol_interface, pc);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * efi_loader_relocate() - relocate UEFI binary
  88. *
  89. * @rel: pointer to the relocation table
  90. * @rel_size: size of the relocation table in bytes
  91. * @efi_reloc: actual load address of the image
  92. * @pref_address: preferred load address of the image
  93. * Return: status code
  94. */
  95. static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  96. unsigned long rel_size, void *efi_reloc,
  97. unsigned long pref_address)
  98. {
  99. unsigned long delta = (unsigned long)efi_reloc - pref_address;
  100. const IMAGE_BASE_RELOCATION *end;
  101. int i;
  102. if (delta == 0)
  103. return EFI_SUCCESS;
  104. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  105. while (rel < end && rel->SizeOfBlock) {
  106. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  107. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  108. while (i--) {
  109. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  110. rel->VirtualAddress;
  111. int type = *relocs >> EFI_PAGE_SHIFT;
  112. uint64_t *x64 = efi_reloc + offset;
  113. uint32_t *x32 = efi_reloc + offset;
  114. uint16_t *x16 = efi_reloc + offset;
  115. switch (type) {
  116. case IMAGE_REL_BASED_ABSOLUTE:
  117. break;
  118. case IMAGE_REL_BASED_HIGH:
  119. *x16 += ((uint32_t)delta) >> 16;
  120. break;
  121. case IMAGE_REL_BASED_LOW:
  122. *x16 += (uint16_t)delta;
  123. break;
  124. case IMAGE_REL_BASED_HIGHLOW:
  125. *x32 += (uint32_t)delta;
  126. break;
  127. case IMAGE_REL_BASED_DIR64:
  128. *x64 += (uint64_t)delta;
  129. break;
  130. #ifdef __riscv
  131. case IMAGE_REL_BASED_RISCV_HI20:
  132. *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) |
  133. (*x32 & 0x00000fff);
  134. break;
  135. case IMAGE_REL_BASED_RISCV_LOW12I:
  136. case IMAGE_REL_BASED_RISCV_LOW12S:
  137. /* We know that we're 4k aligned */
  138. if (delta & 0xfff) {
  139. printf("Unsupported reloc offset\n");
  140. return EFI_LOAD_ERROR;
  141. }
  142. break;
  143. #endif
  144. default:
  145. printf("Unknown Relocation off %x type %x\n",
  146. offset, type);
  147. return EFI_LOAD_ERROR;
  148. }
  149. relocs++;
  150. }
  151. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  152. }
  153. return EFI_SUCCESS;
  154. }
  155. void __weak invalidate_icache_all(void)
  156. {
  157. /* If the system doesn't support icache_all flush, cross our fingers */
  158. }
  159. /**
  160. * efi_set_code_and_data_type() - determine the memory types to be used for code
  161. * and data.
  162. *
  163. * @loaded_image_info: image descriptor
  164. * @image_type: field Subsystem of the optional header for
  165. * Windows specific field
  166. */
  167. static void efi_set_code_and_data_type(
  168. struct efi_loaded_image *loaded_image_info,
  169. uint16_t image_type)
  170. {
  171. switch (image_type) {
  172. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  173. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  174. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  175. break;
  176. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  177. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  178. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  179. break;
  180. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  181. case IMAGE_SUBSYSTEM_EFI_ROM:
  182. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  183. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  184. break;
  185. default:
  186. printf("%s: invalid image type: %u\n", __func__, image_type);
  187. /* Let's assume it is an application */
  188. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  189. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  190. break;
  191. }
  192. }
  193. /**
  194. * efi_load_pe() - relocate EFI binary
  195. *
  196. * This function loads all sections from a PE binary into a newly reserved
  197. * piece of memory. On success the entry point is returned as handle->entry.
  198. *
  199. * @handle: loaded image handle
  200. * @efi: pointer to the EFI binary
  201. * @loaded_image_info: loaded image protocol
  202. * Return: status code
  203. */
  204. efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi,
  205. struct efi_loaded_image *loaded_image_info)
  206. {
  207. IMAGE_NT_HEADERS32 *nt;
  208. IMAGE_DOS_HEADER *dos;
  209. IMAGE_SECTION_HEADER *sections;
  210. int num_sections;
  211. void *efi_reloc;
  212. int i;
  213. const IMAGE_BASE_RELOCATION *rel;
  214. unsigned long rel_size;
  215. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  216. uint64_t image_base;
  217. unsigned long virt_size = 0;
  218. int supported = 0;
  219. dos = efi;
  220. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  221. printf("%s: Invalid DOS Signature\n", __func__);
  222. return EFI_LOAD_ERROR;
  223. }
  224. nt = (void *) ((char *)efi + dos->e_lfanew);
  225. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  226. printf("%s: Invalid NT Signature\n", __func__);
  227. return EFI_LOAD_ERROR;
  228. }
  229. for (i = 0; machines[i]; i++)
  230. if (machines[i] == nt->FileHeader.Machine) {
  231. supported = 1;
  232. break;
  233. }
  234. if (!supported) {
  235. printf("%s: Machine type 0x%04x is not supported\n",
  236. __func__, nt->FileHeader.Machine);
  237. return EFI_LOAD_ERROR;
  238. }
  239. /* Calculate upper virtual address boundary */
  240. num_sections = nt->FileHeader.NumberOfSections;
  241. sections = (void *)&nt->OptionalHeader +
  242. nt->FileHeader.SizeOfOptionalHeader;
  243. for (i = num_sections - 1; i >= 0; i--) {
  244. IMAGE_SECTION_HEADER *sec = &sections[i];
  245. virt_size = max_t(unsigned long, virt_size,
  246. sec->VirtualAddress + sec->Misc.VirtualSize);
  247. }
  248. /* Read 32/64bit specific header bits */
  249. if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  250. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  251. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  252. image_base = opt->ImageBase;
  253. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  254. handle->image_type = opt->Subsystem;
  255. efi_reloc = efi_alloc(virt_size,
  256. loaded_image_info->image_code_type);
  257. if (!efi_reloc) {
  258. printf("%s: Could not allocate %lu bytes\n",
  259. __func__, virt_size);
  260. return EFI_OUT_OF_RESOURCES;
  261. }
  262. handle->entry = efi_reloc + opt->AddressOfEntryPoint;
  263. rel_size = opt->DataDirectory[rel_idx].Size;
  264. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  265. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  266. } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  267. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  268. image_base = opt->ImageBase;
  269. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  270. handle->image_type = opt->Subsystem;
  271. efi_reloc = efi_alloc(virt_size,
  272. loaded_image_info->image_code_type);
  273. if (!efi_reloc) {
  274. printf("%s: Could not allocate %lu bytes\n",
  275. __func__, virt_size);
  276. return EFI_OUT_OF_RESOURCES;
  277. }
  278. handle->entry = efi_reloc + opt->AddressOfEntryPoint;
  279. rel_size = opt->DataDirectory[rel_idx].Size;
  280. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  281. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  282. } else {
  283. printf("%s: Invalid optional header magic %x\n", __func__,
  284. nt->OptionalHeader.Magic);
  285. return EFI_LOAD_ERROR;
  286. }
  287. /* Copy PE headers */
  288. memcpy(efi_reloc, efi, sizeof(*dos) + sizeof(*nt)
  289. + nt->FileHeader.SizeOfOptionalHeader
  290. + num_sections * sizeof(IMAGE_SECTION_HEADER));
  291. /* Load sections into RAM */
  292. for (i = num_sections - 1; i >= 0; i--) {
  293. IMAGE_SECTION_HEADER *sec = &sections[i];
  294. memset(efi_reloc + sec->VirtualAddress, 0,
  295. sec->Misc.VirtualSize);
  296. memcpy(efi_reloc + sec->VirtualAddress,
  297. efi + sec->PointerToRawData,
  298. sec->SizeOfRawData);
  299. }
  300. /* Run through relocations */
  301. if (efi_loader_relocate(rel, rel_size, efi_reloc,
  302. (unsigned long)image_base) != EFI_SUCCESS) {
  303. efi_free_pages((uintptr_t) efi_reloc,
  304. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  305. return EFI_LOAD_ERROR;
  306. }
  307. /* Flush cache */
  308. flush_cache((ulong)efi_reloc,
  309. ALIGN(virt_size, EFI_CACHELINE_SIZE));
  310. invalidate_icache_all();
  311. /* Populate the loaded image interface bits */
  312. loaded_image_info->image_base = efi_reloc;
  313. loaded_image_info->image_size = virt_size;
  314. return EFI_SUCCESS;
  315. }