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