efi_image_loader.c 5.1 KB

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