1/*++
2
3Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4This program and the accompanying materials
5are licensed and made available under the terms and conditions of the BSD License
6which accompanies this distribution.  The full text of the license may be found at
7http://opensource.org/licenses/bsd-license.php
8
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13Module Name:
14
15  EdkIIGluePeCoffLib.h
16
17Abstract:
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//
58typedef
59RETURN_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//
70typedef 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**/
113RETURN_STATUS
114EFIAPI
115GluePeCoffLoaderGetImageInfo (
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**/
139RETURN_STATUS
140EFIAPI
141GluePeCoffLoaderRelocateImage (
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**/
168RETURN_STATUS
169EFIAPI
170GluePeCoffLoaderLoadImage (
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**/
187RETURN_STATUS
188EFIAPI
189PeCoffLoaderImageReadFromMemory (
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**/
211VOID
212EFIAPI
213PeCoffLoaderRelocateImageForRuntime (
214  IN  PHYSICAL_ADDRESS        ImageBase,
215  IN  PHYSICAL_ADDRESS        VirtImageBase,
216  IN  UINTN                   ImageSize,
217  IN  VOID                    *RelocationData
218  );
219
220
221#endif
222