efi_image_loader.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  22. unsigned long rel_size, void *efi_reloc)
  23. {
  24. const IMAGE_BASE_RELOCATION *end;
  25. int i;
  26. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  27. while (rel < end - 1 && rel->SizeOfBlock) {
  28. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  29. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  30. while (i--) {
  31. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  32. rel->VirtualAddress;
  33. int type = *relocs >> EFI_PAGE_SHIFT;
  34. unsigned long delta = (unsigned long)efi_reloc;
  35. uint64_t *x64 = efi_reloc + offset;
  36. uint32_t *x32 = efi_reloc + offset;
  37. uint16_t *x16 = efi_reloc + offset;
  38. switch (type) {
  39. case IMAGE_REL_BASED_ABSOLUTE:
  40. break;
  41. case IMAGE_REL_BASED_HIGH:
  42. *x16 += ((uint32_t)delta) >> 16;
  43. break;
  44. case IMAGE_REL_BASED_LOW:
  45. *x16 += (uint16_t)delta;
  46. break;
  47. case IMAGE_REL_BASED_HIGHLOW:
  48. *x32 += (uint32_t)delta;
  49. break;
  50. case IMAGE_REL_BASED_DIR64:
  51. *x64 += (uint64_t)delta;
  52. break;
  53. default:
  54. printf("Unknown Relocation off %x type %x\n",
  55. offset, type);
  56. return EFI_LOAD_ERROR;
  57. }
  58. relocs++;
  59. }
  60. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  61. }
  62. return EFI_SUCCESS;
  63. }
  64. void __weak invalidate_icache_all(void)
  65. {
  66. /* If the system doesn't support icache_all flush, cross our fingers */
  67. }
  68. /*
  69. * Determine the memory types to be used for code and data.
  70. *
  71. * @loaded_image_info image descriptor
  72. * @image_type field Subsystem of the optional header for
  73. * Windows specific field
  74. */
  75. static void efi_set_code_and_data_type(
  76. struct efi_loaded_image *loaded_image_info,
  77. uint16_t image_type)
  78. {
  79. switch (image_type) {
  80. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  81. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  82. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  83. break;
  84. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  85. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  86. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  87. break;
  88. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  89. case IMAGE_SUBSYSTEM_EFI_ROM:
  90. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  91. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  92. break;
  93. default:
  94. printf("%s: invalid image type: %u\n", __func__, image_type);
  95. /* Let's assume it is an application */
  96. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  97. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  98. break;
  99. }
  100. }
  101. /*
  102. * This function loads all sections from a PE binary into a newly reserved
  103. * piece of memory. On successful load it then returns the entry point for
  104. * the binary. Otherwise NULL.
  105. */
  106. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  107. {
  108. IMAGE_NT_HEADERS32 *nt;
  109. IMAGE_DOS_HEADER *dos;
  110. IMAGE_SECTION_HEADER *sections;
  111. int num_sections;
  112. void *efi_reloc;
  113. int i;
  114. const IMAGE_BASE_RELOCATION *rel;
  115. unsigned long rel_size;
  116. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  117. void *entry;
  118. uint64_t image_size;
  119. unsigned long virt_size = 0;
  120. bool can_run_nt64 = true;
  121. bool can_run_nt32 = true;
  122. #if defined(CONFIG_ARM64)
  123. can_run_nt32 = false;
  124. #elif defined(CONFIG_ARM)
  125. can_run_nt64 = false;
  126. #endif
  127. dos = efi;
  128. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  129. printf("%s: Invalid DOS Signature\n", __func__);
  130. return NULL;
  131. }
  132. nt = (void *) ((char *)efi + dos->e_lfanew);
  133. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  134. printf("%s: Invalid NT Signature\n", __func__);
  135. return NULL;
  136. }
  137. /* Calculate upper virtual address boundary */
  138. num_sections = nt->FileHeader.NumberOfSections;
  139. sections = (void *)&nt->OptionalHeader +
  140. nt->FileHeader.SizeOfOptionalHeader;
  141. for (i = num_sections - 1; i >= 0; i--) {
  142. IMAGE_SECTION_HEADER *sec = &sections[i];
  143. virt_size = max_t(unsigned long, virt_size,
  144. sec->VirtualAddress + sec->Misc.VirtualSize);
  145. }
  146. /* Read 32/64bit specific header bits */
  147. if (can_run_nt64 &&
  148. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
  149. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  150. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  151. image_size = opt->SizeOfImage;
  152. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  153. efi_reloc = efi_alloc(virt_size,
  154. loaded_image_info->image_code_type);
  155. if (!efi_reloc) {
  156. printf("%s: Could not allocate %lu bytes\n",
  157. __func__, virt_size);
  158. return NULL;
  159. }
  160. entry = efi_reloc + opt->AddressOfEntryPoint;
  161. rel_size = opt->DataDirectory[rel_idx].Size;
  162. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  163. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  164. } else if (can_run_nt32 &&
  165. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
  166. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  167. image_size = opt->SizeOfImage;
  168. efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  169. efi_reloc = efi_alloc(virt_size,
  170. loaded_image_info->image_code_type);
  171. if (!efi_reloc) {
  172. printf("%s: Could not allocate %lu bytes\n",
  173. __func__, virt_size);
  174. return NULL;
  175. }
  176. entry = efi_reloc + opt->AddressOfEntryPoint;
  177. rel_size = opt->DataDirectory[rel_idx].Size;
  178. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  179. virt_size = ALIGN(virt_size, opt->SectionAlignment);
  180. } else {
  181. printf("%s: Invalid optional header magic %x\n", __func__,
  182. nt->OptionalHeader.Magic);
  183. return NULL;
  184. }
  185. /* Load sections into RAM */
  186. for (i = num_sections - 1; i >= 0; i--) {
  187. IMAGE_SECTION_HEADER *sec = &sections[i];
  188. memset(efi_reloc + sec->VirtualAddress, 0,
  189. sec->Misc.VirtualSize);
  190. memcpy(efi_reloc + sec->VirtualAddress,
  191. efi + sec->PointerToRawData,
  192. sec->SizeOfRawData);
  193. }
  194. /* Run through relocations */
  195. if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
  196. efi_free_pages((uintptr_t) efi_reloc,
  197. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  198. return NULL;
  199. }
  200. /* Flush cache */
  201. flush_cache((ulong)efi_reloc,
  202. ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
  203. invalidate_icache_all();
  204. /* Populate the loaded image interface bits */
  205. loaded_image_info->image_base = efi;
  206. loaded_image_info->image_size = image_size;
  207. loaded_image_info->reloc_base = efi_reloc;
  208. loaded_image_info->reloc_size = virt_size;
  209. return entry;
  210. }