efi_image_loader.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_guid_device_path = DEVICE_PATH_GUID;
  16. const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
  17. efi_status_t EFIAPI efi_return_handle(void *handle, efi_guid_t *protocol,
  18. void **protocol_interface, void *agent_handle,
  19. void *controller_handle, uint32_t attributes)
  20. {
  21. *protocol_interface = handle;
  22. return EFI_SUCCESS;
  23. }
  24. static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
  25. unsigned long rel_size, void *efi_reloc)
  26. {
  27. const IMAGE_BASE_RELOCATION *end;
  28. int i;
  29. end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
  30. while (rel < end - 1 && rel->SizeOfBlock) {
  31. const uint16_t *relocs = (const uint16_t *)(rel + 1);
  32. i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
  33. while (i--) {
  34. uint32_t offset = (uint32_t)(*relocs & 0xfff) +
  35. rel->VirtualAddress;
  36. int type = *relocs >> EFI_PAGE_SHIFT;
  37. unsigned long delta = (unsigned long)efi_reloc;
  38. uint64_t *x64 = efi_reloc + offset;
  39. uint32_t *x32 = efi_reloc + offset;
  40. uint16_t *x16 = efi_reloc + offset;
  41. switch (type) {
  42. case IMAGE_REL_BASED_ABSOLUTE:
  43. break;
  44. case IMAGE_REL_BASED_HIGH:
  45. *x16 += ((uint32_t)delta) >> 16;
  46. break;
  47. case IMAGE_REL_BASED_LOW:
  48. *x16 += (uint16_t)delta;
  49. break;
  50. case IMAGE_REL_BASED_HIGHLOW:
  51. *x32 += (uint32_t)delta;
  52. break;
  53. case IMAGE_REL_BASED_DIR64:
  54. *x64 += (uint64_t)delta;
  55. break;
  56. default:
  57. printf("Unknown Relocation off %x type %x\n",
  58. offset, type);
  59. return EFI_LOAD_ERROR;
  60. }
  61. relocs++;
  62. }
  63. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  64. }
  65. return EFI_SUCCESS;
  66. }
  67. void __weak invalidate_icache_all(void)
  68. {
  69. /* If the system doesn't support icache_all flush, cross our fingers */
  70. }
  71. /*
  72. * This function loads all sections from a PE binary into a newly reserved
  73. * piece of memory. On successful load it then returns the entry point for
  74. * the binary. Otherwise NULL.
  75. */
  76. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  77. {
  78. IMAGE_NT_HEADERS32 *nt;
  79. IMAGE_DOS_HEADER *dos;
  80. IMAGE_SECTION_HEADER *sections;
  81. int num_sections;
  82. void *efi_reloc;
  83. int i;
  84. const IMAGE_BASE_RELOCATION *rel;
  85. unsigned long rel_size;
  86. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  87. void *entry;
  88. uint64_t image_size;
  89. unsigned long virt_size = 0;
  90. bool can_run_nt64 = true;
  91. bool can_run_nt32 = true;
  92. #if defined(CONFIG_ARM64)
  93. can_run_nt32 = false;
  94. #elif defined(CONFIG_ARM)
  95. can_run_nt64 = false;
  96. #endif
  97. dos = efi;
  98. if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
  99. printf("%s: Invalid DOS Signature\n", __func__);
  100. return NULL;
  101. }
  102. nt = (void *) ((char *)efi + dos->e_lfanew);
  103. if (nt->Signature != IMAGE_NT_SIGNATURE) {
  104. printf("%s: Invalid NT Signature\n", __func__);
  105. return NULL;
  106. }
  107. /* Calculate upper virtual address boundary */
  108. num_sections = nt->FileHeader.NumberOfSections;
  109. sections = (void *)&nt->OptionalHeader +
  110. nt->FileHeader.SizeOfOptionalHeader;
  111. for (i = num_sections - 1; i >= 0; i--) {
  112. IMAGE_SECTION_HEADER *sec = &sections[i];
  113. virt_size = max_t(unsigned long, virt_size,
  114. sec->VirtualAddress + sec->Misc.VirtualSize);
  115. }
  116. /* Read 32/64bit specific header bits */
  117. if (can_run_nt64 &&
  118. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
  119. IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
  120. IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
  121. image_size = opt->SizeOfImage;
  122. efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  123. if (!efi_reloc) {
  124. printf("%s: Could not allocate %ld bytes\n",
  125. __func__, virt_size);
  126. return NULL;
  127. }
  128. entry = efi_reloc + opt->AddressOfEntryPoint;
  129. rel_size = opt->DataDirectory[rel_idx].Size;
  130. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  131. } else if (can_run_nt32 &&
  132. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
  133. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  134. image_size = opt->SizeOfImage;
  135. efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  136. if (!efi_reloc) {
  137. printf("%s: Could not allocate %ld bytes\n",
  138. __func__, virt_size);
  139. return NULL;
  140. }
  141. entry = efi_reloc + opt->AddressOfEntryPoint;
  142. rel_size = opt->DataDirectory[rel_idx].Size;
  143. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  144. } else {
  145. printf("%s: Invalid optional header magic %x\n", __func__,
  146. nt->OptionalHeader.Magic);
  147. return NULL;
  148. }
  149. /* Load sections into RAM */
  150. for (i = num_sections - 1; i >= 0; i--) {
  151. IMAGE_SECTION_HEADER *sec = &sections[i];
  152. memset(efi_reloc + sec->VirtualAddress, 0,
  153. sec->Misc.VirtualSize);
  154. memcpy(efi_reloc + sec->VirtualAddress,
  155. efi + sec->PointerToRawData,
  156. sec->SizeOfRawData);
  157. }
  158. /* Run through relocations */
  159. if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
  160. efi_free_pages((uintptr_t) efi_reloc,
  161. (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
  162. return NULL;
  163. }
  164. /* Flush cache */
  165. flush_cache((ulong)efi_reloc,
  166. ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
  167. invalidate_icache_all();
  168. /* Populate the loaded image interface bits */
  169. loaded_image_info->image_base = efi;
  170. loaded_image_info->image_size = image_size;
  171. return entry;
  172. }