PeCoffLoader.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /** @file
  2. Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2010, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef __PE_COFF_LOADER_H__
  7. #define __PE_COFF_LOADER_H__
  8. // Needed for PE_COFF_LOADER_IMAGE_CONTEXT
  9. #include <Library/PeCoffLib.h>
  10. // B323179B-97FB-477E-B0FE-D88591FA11AB
  11. #define PE_COFF_LOADER_PROTOCOL_GUID \
  12. { 0xB323179B, 0x97FB, 0x477E, { 0xB0, 0xFE, 0xD8, 0x85, 0x91, 0xFA, 0x11, 0xAB } }
  13. typedef struct _PE_COFF_LOADER_PROTOCOL PE_COFF_LOADER_PROTOCOL;
  14. /**
  15. Retrieves information about a PE/COFF image.
  16. Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
  17. DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
  18. DebugDirectoryEntryRva fields of the ImageContext structure.
  19. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
  20. If the PE/COFF image accessed through the ImageRead service in the ImageContext
  21. structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
  22. If any errors occur while computing the fields of ImageContext,
  23. then the error status is returned in the ImageError field of ImageContext.
  24. If the image is a TE image, then SectionAlignment is set to 0.
  25. The ImageRead and Handle fields of ImageContext structure must be valid prior
  26. to invoking this service.
  27. @param ImageContext Pointer to the image context structure that describes the PE/COFF
  28. image that needs to be examined by this function.
  29. @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
  30. @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
  31. @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
  32. **/
  33. typedef
  34. RETURN_STATUS
  35. (EFIAPI *PE_COFF_LOADER_GET_IMAGE_INFO)(
  36. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  37. );
  38. /**
  39. Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
  40. If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
  41. ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
  42. of ImageContext as the relocation base address. The caller must allocate the relocation
  43. fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
  44. The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
  45. ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
  46. DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
  47. the ImageContext structure must be valid prior to invoking this service.
  48. If ImageContext is NULL, then ASSERT().
  49. Note that if the platform does not maintain coherency between the instruction cache(s) and the data
  50. cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
  51. prior to transferring control to a PE/COFF image that is loaded using this library.
  52. @param ImageContext Pointer to the image context structure that describes the PE/COFF
  53. image that is being relocated.
  54. @retval RETURN_SUCCESS The PE/COFF image was relocated.
  55. Extended status information is in the ImageError field of ImageContext.
  56. @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
  57. Extended status information is in the ImageError field of ImageContext.
  58. @retval RETURN_UNSUPPORTED A relocation record type is not supported.
  59. Extended status information is in the ImageError field of ImageContext.
  60. **/
  61. typedef
  62. RETURN_STATUS
  63. (EFIAPI *PE_COFF_LOADER_RELOCATE_IMAGE)(
  64. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  65. );
  66. /**
  67. Loads a PE/COFF image into memory.
  68. Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
  69. specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
  70. the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
  71. The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
  72. The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
  73. DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
  74. fields of the ImageContext structure must be valid prior to invoking this service.
  75. If ImageContext is NULL, then ASSERT().
  76. Note that if the platform does not maintain coherency between the instruction cache(s) and the data
  77. cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
  78. prior to transferring control to a PE/COFF image that is loaded using this library.
  79. @param ImageContext Pointer to the image context structure that describes the PE/COFF
  80. image that is being loaded.
  81. @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
  82. the ImageAddress and ImageSize fields of ImageContext.
  83. Extended status information is in the ImageError field of ImageContext.
  84. @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
  85. Extended status information is in the ImageError field of ImageContext.
  86. @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
  87. Extended status information is in the ImageError field of ImageContext.
  88. @retval RETURN_INVALID_PARAMETER The image address is invalid.
  89. Extended status information is in the ImageError field of ImageContext.
  90. **/
  91. typedef
  92. RETURN_STATUS
  93. (EFIAPI *PE_COFF_LOADER_LOAD_IMAGE)(
  94. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  95. );
  96. /**
  97. Reads contents of a PE/COFF image from a buffer in system memory.
  98. This is the default implementation of a PE_COFF_LOADER_READ_FILE function
  99. that assumes FileHandle pointer to the beginning of a PE/COFF image.
  100. This function reads contents of the PE/COFF image that starts at the system memory
  101. address specified by FileHandle. The read operation copies ReadSize bytes from the
  102. PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
  103. The size of the buffer actually read is returned in ReadSize.
  104. If FileHandle is NULL, then ASSERT().
  105. If ReadSize is NULL, then ASSERT().
  106. If Buffer is NULL, then ASSERT().
  107. @param FileHandle Pointer to base of the input stream
  108. @param FileOffset Offset into the PE/COFF image to begin the read operation.
  109. @param ReadSize On input, the size in bytes of the requested read operation.
  110. On output, the number of bytes actually read.
  111. @param Buffer Output buffer that contains the data read from the PE/COFF image.
  112. @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
  113. the buffer.
  114. **/
  115. typedef
  116. RETURN_STATUS
  117. (EFIAPI *PE_COFF_LOADER_READ_FROM_MEMORY)(
  118. IN VOID *FileHandle,
  119. IN UINTN FileOffset,
  120. IN OUT UINTN *ReadSize,
  121. OUT VOID *Buffer
  122. );
  123. /**
  124. Reapply fixups on a fixed up PE32/PE32+ image to allow virtual calling at EFI
  125. runtime.
  126. This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
  127. and ImageSize so the image will execute correctly when the PE/COFF image is mapped
  128. to the address specified by VirtualImageBase. RelocationData must be identical
  129. to the FixupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
  130. after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
  131. Note that if the platform does not maintain coherency between the instruction cache(s) and the data
  132. cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
  133. prior to transferring control to a PE/COFF image that is loaded using this library.
  134. @param ImageBase Base address of a PE/COFF image that has been loaded
  135. and relocated into system memory.
  136. @param VirtImageBase The request virtual address that the PE/COFF image is to
  137. be fixed up for.
  138. @param ImageSize The size, in bytes, of the PE/COFF image.
  139. @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
  140. image was relocated using PeCoffLoaderRelocateImage().
  141. **/
  142. typedef
  143. VOID
  144. (EFIAPI *PE_COFF_LOADER_RELOCATE_IMAGE_FOR_RUNTIME)(
  145. IN PHYSICAL_ADDRESS ImageBase,
  146. IN PHYSICAL_ADDRESS VirtImageBase,
  147. IN UINTN ImageSize,
  148. IN VOID *RelocationData
  149. );
  150. /**
  151. Unloads a loaded PE/COFF image from memory and releases its taken resource.
  152. Releases any environment specific resources that were allocated when the image
  153. specified by ImageContext was loaded using PeCoffLoaderLoadImage().
  154. For NT32 emulator, the PE/COFF image loaded by system needs to release.
  155. For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
  156. this function can simply return RETURN_SUCCESS.
  157. If ImageContext is NULL, then ASSERT().
  158. @param ImageContext Pointer to the image context structure that describes the PE/COFF
  159. image to be unloaded.
  160. @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
  161. **/
  162. typedef
  163. RETURN_STATUS
  164. (EFIAPI *PE_COFF_LOADER_UNLOAD_IMAGE)(
  165. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  166. );
  167. struct _PE_COFF_LOADER_PROTOCOL {
  168. PE_COFF_LOADER_GET_IMAGE_INFO GetImageInfo;
  169. PE_COFF_LOADER_LOAD_IMAGE LoadImage;
  170. PE_COFF_LOADER_RELOCATE_IMAGE RelocateImage;
  171. PE_COFF_LOADER_READ_FROM_MEMORY ReadFromMemory;
  172. PE_COFF_LOADER_RELOCATE_IMAGE_FOR_RUNTIME RelocateImageForRuntime;
  173. PE_COFF_LOADER_UNLOAD_IMAGE UnloadImage;
  174. };
  175. extern EFI_GUID gPeCoffLoaderProtocolGuid;
  176. #endif