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