efi_image_loader.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * This function loads all sections from a PE binary into a newly reserved
  70. * piece of memory. On successful load it then returns the entry point for
  71. * the binary. Otherwise NULL.
  72. */
  73. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  74. {
  75. IMAGE_NT_HEADERS32 *nt;
  76. IMAGE_DOS_HEADER *dos;
  77. IMAGE_SECTION_HEADER *sections;
  78. int num_sections;
  79. void *efi_reloc;
  80. int i;
  81. const IMAGE_BASE_RELOCATION *rel;
  82. unsigned long rel_size;
  83. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  84. void *entry;
  85. uint64_t image_size;
  86. unsigned long virt_size = 0;
  87. bool can_run_nt64 = true;
  88. bool can_run_nt32 = true;
  89. uint16_t image_type;
  90. #if defined(CONFIG_ARM64)
  91. can_run_nt32 = false;
  92. #elif defined(CONFIG_ARM)
  93. can_run_nt64 = false;
  94. #endif
  95. dos = efi;
  96. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  97. printf("%s: Invalid DOS Signature\n", __func__);
  98. return NULL;
  99. }
  100. nt = (void *) ((char *)efi + dos->e_lfanew);
  101. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  102. printf("%s: Invalid NT Signature\n", __func__);
  103. return NULL;
  104. }
  105. /* Calculate upper virtual address boundary */
  106. num_sections = nt->FileHeader.NumberOfSections;
  107. sections = (void *)&nt->OptionalHeader +
  108. nt->FileHeader.SizeOfOptionalHeader;
  109. for (i = num_sections - 1; i >= 0; i--) {
  110. IMAGE_SECTION_HEADER *sec = &sections[i];
  111. virt_size = max_t(unsigned long, virt_size,
  112. sec->VirtualAddress + sec->Misc.VirtualSize);
  113. }
  114. /* Read 32/64bit specific header bits */
  115. if (can_run_nt64 &&
  116. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
  117. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  118. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  119. image_size = opt->SizeOfImage;
  120. efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  121. if (!efi_reloc) {
  122. printf("%s: Could not allocate %ld bytes\n",
  123. __func__, virt_size);
  124. return NULL;
  125. }
  126. entry = efi_reloc + opt->AddressOfEntryPoint;
  127. rel_size = opt->DataDirectory[rel_idx].Size;
  128. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  129. image_type = opt->Subsystem;
  130. } else if (can_run_nt32 &&
  131. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
  132. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  133. image_size = opt->SizeOfImage;
  134. efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  135. if (!efi_reloc) {
  136. printf("%s: Could not allocate %ld bytes\n",
  137. __func__, virt_size);
  138. return NULL;
  139. }
  140. entry = efi_reloc + opt->AddressOfEntryPoint;
  141. rel_size = opt->DataDirectory[rel_idx].Size;
  142. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  143. image_type = opt->Subsystem;
  144. } else {
  145. printf("%s: Invalid optional header magic %x\n", __func__,
  146. nt->OptionalHeader.Magic);
  147. return NULL;
  148. }
  149. switch (image_type) {
  150. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  151. loaded_image_info->image_code_type = EFI_LOADER_CODE;
  152. loaded_image_info->image_data_type = EFI_LOADER_DATA;
  153. break;
  154. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  155. loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  156. loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  157. break;
  158. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  159. case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
  160. loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  161. loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  162. break;
  163. default:
  164. printf("%s: invalid image type: %u\n", __func__, image_type);
  165. break;
  166. }
  167. /* Load sections into RAM */
  168. for (i = num_sections - 1; i >= 0; i--) {
  169. IMAGE_SECTION_HEADER *sec = &sections[i];
  170. memset(efi_reloc + sec->VirtualAddress, 0,
  171. sec->Misc.VirtualSize);
  172. memcpy(efi_reloc + sec->VirtualAddress,
  173. efi + sec->PointerToRawData,
  174. sec->SizeOfRawData);
  175. }
  176. /* Run through relocations */
  177. if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
  178. efi_free_pages((uintptr_t) efi_reloc,
  179. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  180. return NULL;
  181. }
  182. /* Flush cache */
  183. flush_cache((ulong)efi_reloc,
  184. ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
  185. invalidate_icache_all();
  186. /* Populate the loaded image interface bits */
  187. loaded_image_info->image_base = efi;
  188. loaded_image_info->image_size = image_size;
  189. return entry;
  190. }