efi_image_loader.c 5.2 KB

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