1 /*++ 2 3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR> 4 This program and the accompanying materials 5 are licensed and made available under the terms and conditions of the BSD License 6 which accompanies this distribution. The full text of the license may be found at 7 http://opensource.org/licenses/bsd-license.php 8 9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 12 13 Module Name: 14 15 EdkIIGluePeCoffLib.h 16 17 Abstract: 18 19 Public header file for PeCoff Lib 20 21 --*/ 22 23 #ifndef __EDKII_GLUE_BASE_PE_COFF_LIB_H__ 24 #define __EDKII_GLUE_BASE_PE_COFF_LIB_H__ 25 26 27 #define PeCoffLoaderGetImageInfo(_IMAGECONTEXT) GluePeCoffLoaderGetImageInfo(_IMAGECONTEXT) 28 #define PeCoffLoaderRelocateImage(_IMAGECONTEXT) GluePeCoffLoaderRelocateImage(_IMAGECONTEXT) 29 #define PeCoffLoaderLoadImage(_IMAGECONTEXT) GluePeCoffLoaderLoadImage(_IMAGECONTEXT) 30 #define PeCoffLoaderGetPeHeader(_IMAGECONTEXT, _HDR) GluePeCoffLoaderGetPeHeader(_IMAGECONTEXT, _HDR) 31 #define PeCoffLoaderImageAddress(_IMAGECONTEXT, _ADR) GluePeCoffLoaderImageAddress(_IMAGECONTEXT, _ADR) 32 #define PeCoffLoaderRelocateImage(_IMAGECONTEXT) GluePeCoffLoaderRelocateImage(_IMAGECONTEXT) 33 #define PeCoffLoaderRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) \ 34 GluePeCoffLoaderRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) 35 #define PeHotRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) \ 36 GluePeHotRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) 37 38 39 // 40 // Return status codes from the PE/COFF Loader services 41 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes 42 // 43 #define IMAGE_ERROR_SUCCESS 0 44 #define IMAGE_ERROR_IMAGE_READ 1 45 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2 46 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3 47 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4 48 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5 49 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6 50 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7 51 #define IMAGE_ERROR_SECTION_NOT_LOADED 8 52 #define IMAGE_ERROR_FAILED_RELOCATION 9 53 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10 54 55 // 56 // PE/COFF Loader Read Function passed in by caller 57 // 58 typedef 59 RETURN_STATUS 60 (EFIAPI *PE_COFF_LOADER_READ_FILE) ( 61 IN VOID *FileHandle, 62 IN UINTN FileOffset, 63 IN OUT UINTN *ReadSize, 64 OUT VOID *Buffer 65 ); 66 67 // 68 // Context structure used while PE/COFF image is being loaded and relocated 69 // 70 typedef struct { 71 PHYSICAL_ADDRESS ImageAddress; 72 UINT64 ImageSize; 73 PHYSICAL_ADDRESS DestinationAddress; 74 PHYSICAL_ADDRESS EntryPoint; 75 PE_COFF_LOADER_READ_FILE ImageRead; 76 VOID *Handle; 77 VOID *FixupData; 78 UINT32 SectionAlignment; 79 UINT32 PeCoffHeaderOffset; 80 UINT32 DebugDirectoryEntryRva; 81 VOID *CodeView; 82 CHAR8 *PdbPointer; 83 UINTN SizeOfHeaders; 84 UINT32 ImageCodeMemoryType; 85 UINT32 ImageDataMemoryType; 86 UINT32 ImageError; 87 UINTN FixupDataSize; 88 UINT16 Machine; 89 UINT16 ImageType; 90 BOOLEAN RelocationsStripped; 91 BOOLEAN IsTeImage; 92 } PE_COFF_LOADER_IMAGE_CONTEXT; 93 94 /** 95 Retrieves information about a PE/COFF image. 96 97 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView, 98 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva 99 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. 100 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not 101 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while 102 computing the fields of ImageContext, then the error status is returned in the ImageError field of 103 ImageContext. 104 105 @param ImageContext Pointer to the image context structure that describes the PE/COFF 106 image that needs to be examined by this function. 107 108 @retval RETURN_SUCCESS The information on the PE/COFF image was collected. 109 @retval RETURN_INVALID_PARAMETER ImageContext is NULL. 110 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported. 111 112 **/ 113 RETURN_STATUS 114 EFIAPI 115 GluePeCoffLoaderGetImageInfo ( 116 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext 117 ); 118 119 /** 120 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage(). 121 122 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of 123 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field 124 of ImageContext as the relocation base address. The caller must allocate the relocation 125 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. 126 If ImageContext is NULL, then ASSERT(). 127 128 @param ImageContext Pointer to the image context structure that describes the PE/COFF 129 image that is being relocated. 130 131 @retval RETURN_SUCCESS The PE/COFF image was relocated. 132 Extended status information is in the ImageError field of ImageContext. 133 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image. 134 Extended status information is in the ImageError field of ImageContext. 135 @retval RETURN_UNSUPPORTED A relocation record type is not supported. 136 Extended status information is in the ImageError field of ImageContext. 137 138 **/ 139 RETURN_STATUS 140 EFIAPI 141 GluePeCoffLoaderRelocateImage ( 142 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext 143 ); 144 145 /** 146 Loads a PE/COFF image into memory. 147 148 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer 149 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate 150 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function. 151 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed. 152 If ImageContext is NULL, then ASSERT(). 153 154 @param ImageContext Pointer to the image context structure that describes the PE/COFF 155 image that is being loaded. 156 157 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by 158 the ImageAddress and ImageSize fields of ImageContext. 159 Extended status information is in the ImageError field of ImageContext. 160 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer. 161 Extended status information is in the ImageError field of ImageContext. 162 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations. 163 Extended status information is in the ImageError field of ImageContext. 164 @retval RETURN_INVALID_PARAMETER The image address is invalid. 165 Extended status information is in the ImageError field of ImageContext. 166 167 **/ 168 RETURN_STATUS 169 EFIAPI 170 GluePeCoffLoaderLoadImage ( 171 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext 172 ); 173 174 175 /** 176 ImageRead function that operates on a memory buffer whos base is passed into 177 FileHandle. 178 179 @param FileHandle Ponter to baes of the input stream 180 @param FileOffset Offset to the start of the buffer 181 @param ReadSize Number of bytes to copy into the buffer 182 @param Buffer Location to place results of read 183 184 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into 185 the buffer. 186 **/ 187 RETURN_STATUS 188 EFIAPI 189 PeCoffLoaderImageReadFromMemory ( 190 IN VOID *FileHandle, 191 IN UINTN FileOffset, 192 IN OUT UINTN *ReadSize, 193 OUT VOID *Buffer 194 ); 195 196 197 /** 198 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI 199 runtime. 200 201 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply 202 the fixups with a virtual mapping. 203 204 205 @param ImageBase Base address of relocated image 206 @param VirtImageBase Virtual mapping for ImageBase 207 @param ImageSize Size of the image to relocate 208 @param RelocationData Location to place results of read 209 210 **/ 211 VOID 212 EFIAPI 213 PeCoffLoaderRelocateImageForRuntime ( 214 IN PHYSICAL_ADDRESS ImageBase, 215 IN PHYSICAL_ADDRESS VirtImageBase, 216 IN UINTN ImageSize, 217 IN VOID *RelocationData 218 ); 219 220 221 #endif 222