efi_image_loader.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 void 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. uint16_t offset = (*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. }
  60. relocs++;
  61. }
  62. rel = (const IMAGE_BASE_RELOCATION *)relocs;
  63. }
  64. }
  65. void __weak invalidate_icache_all(void)
  66. {
  67. /* If the system doesn't support icache_all flush, cross our fingers */
  68. }
  69. /*
  70. * This function loads all sections from a PE binary into a newly reserved
  71. * piece of memory. On successful load it then returns the entry point for
  72. * the binary. Otherwise NULL.
  73. */
  74. void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
  75. {
  76. IMAGE_NT_HEADERS32 *nt;
  77. IMAGE_DOS_HEADER *dos;
  78. IMAGE_SECTION_HEADER *sections;
  79. int num_sections;
  80. void *efi_reloc;
  81. int i;
  82. const IMAGE_BASE_RELOCATION *rel;
  83. unsigned long rel_size;
  84. int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
  85. void *entry;
  86. uint64_t image_size;
  87. unsigned long virt_size = 0;
  88. bool can_run_nt64 = true;
  89. bool can_run_nt32 = true;
  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. } else if (can_run_nt32 &&
  130. (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
  131. IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
  132. image_size = opt->SizeOfImage;
  133. efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  134. if (!efi_reloc) {
  135. printf("%s: Could not allocate %ld bytes\n",
  136. __func__, virt_size);
  137. return NULL;
  138. }
  139. entry = efi_reloc + opt->AddressOfEntryPoint;
  140. rel_size = opt->DataDirectory[rel_idx].Size;
  141. rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
  142. } else {
  143. printf("%s: Invalid optional header magic %x\n", __func__,
  144. nt->OptionalHeader.Magic);
  145. return NULL;
  146. }
  147. /* Load sections into RAM */
  148. for (i = num_sections - 1; i >= 0; i--) {
  149. IMAGE_SECTION_HEADER *sec = &sections[i];
  150. memset(efi_reloc + sec->VirtualAddress, 0,
  151. sec->Misc.VirtualSize);
  152. memcpy(efi_reloc + sec->VirtualAddress,
  153. efi + sec->PointerToRawData,
  154. sec->SizeOfRawData);
  155. }
  156. /* Run through relocations */
  157. efi_loader_relocate(rel, rel_size, efi_reloc);
  158. /* Flush cache */
  159. flush_cache((ulong)efi_reloc, virt_size);
  160. invalidate_icache_all();
  161. /* Populate the loaded image interface bits */
  162. loaded_image_info->image_base = efi;
  163. loaded_image_info->image_size = image_size;
  164. return entry;
  165. }