1 /** @file
2 	Function prototypes and defines on Memory Only PE COFF loader
3 
4 	Copyright (c) 2006 - 2014, Intel Corporation. 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 __BASE_PE_COFF_LIB_H__
16 #define __BASE_PE_COFF_LIB_H__
17 
18 //
19 // Return status codes from the PE/COFF Loader services
20 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
21 //
22 #define IMAGE_ERROR_SUCCESS                      0
23 #define IMAGE_ERROR_IMAGE_READ                   1
24 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE  2
25 #define IMAGE_ERROR_INVALID_MACHINE_TYPE         3
26 #define IMAGE_ERROR_INVALID_SUBSYSTEM            4
27 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS        5
28 #define IMAGE_ERROR_INVALID_IMAGE_SIZE           6
29 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT    7
30 #define IMAGE_ERROR_SECTION_NOT_LOADED           8
31 #define IMAGE_ERROR_FAILED_RELOCATION            9
32 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH          10
33 
34 
35 //
36 // PE/COFF Loader Read Function passed in by caller
37 //
38 typedef
39 RETURN_STATUS
40 (EFIAPI *PE_COFF_LOADER_READ_FILE) (
41   IN     VOID   *FileHandle,
42   IN     UINTN  FileOffset,
43   IN OUT UINTN  *ReadSize,
44   OUT    VOID   *Buffer
45   );
46 
47 //
48 // Context structure used while PE/COFF image is being loaded and relocated
49 //
50 typedef struct {
51   PHYSICAL_ADDRESS                  ImageAddress;
52   UINT64                            ImageSize;
53   PHYSICAL_ADDRESS                  DestinationAddress;
54   PHYSICAL_ADDRESS                  EntryPoint;
55   PE_COFF_LOADER_READ_FILE          ImageRead;
56   VOID                              *Handle;
57   VOID                              *FixupData;
58   UINT32                            SectionAlignment;
59   UINT32                            PeCoffHeaderOffset;
60   UINT32                            DebugDirectoryEntryRva;
61   VOID                              *CodeView;
62   CHAR8                             *PdbPointer;
63   UINTN                             SizeOfHeaders;
64   UINT32                            ImageCodeMemoryType;
65   UINT32                            ImageDataMemoryType;
66   UINT32                            ImageError;
67   UINTN                             FixupDataSize;
68   UINT16                            Machine;
69   UINT16                            ImageType;
70   BOOLEAN                           RelocationsStripped;
71   BOOLEAN                           IsTeImage;
72 } PE_COFF_LOADER_IMAGE_CONTEXT;
73 
74 
75 /**
76 	Retrieves information on a PE/COFF image
77 
78 	@param	ImageContext The context of the image being loaded
79 
80 	@retval	EFI_SUCCESS The information on the PE/COFF image was collected.
81 	@retval	EFI_INVALID_PARAMETER ImageContext is NULL.
82 	@retval	EFI_UNSUPPORTED The PE/COFF image is not supported.
83 	@retval	Otherwise The error status from reading the PE/COFF image using the
84 	ImageContext->ImageRead() function
85 
86 **/
87 RETURN_STATUS
88 EFIAPI
89 PeCoffLoaderGetImageInfo (
90   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
91   )
92 ;
93 
94 /**
95 	Relocates a PE/COFF image in memory
96 
97 	@param	ImageContext Contains information on the loaded image to relocate
98 
99 	@retval EFI_SUCCESS      if the PE/COFF image was relocated
100 	@retval EFI_LOAD_ERROR   if the image is not a valid PE/COFF image
101 	@retval EFI_UNSUPPORTED  not support
102 
103 **/
104 RETURN_STATUS
105 EFIAPI
106 PeCoffLoaderRelocateImage (
107   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
108   )
109 ;
110 
111 /**
112 	Loads a PE/COFF image into memory
113 
114 	@param	ImageContext Contains information on image to load into memory
115 
116 	@retval EFI_SUCCESS            if the PE/COFF image was loaded
117 	@retval EFI_BUFFER_TOO_SMALL   if the caller did not provide a large enough buffer
118 	@retval EFI_LOAD_ERROR         if the image is a runtime driver with no relocations
119 	@retval EFI_INVALID_PARAMETER  if the image address is invalid
120 
121 **/
122 RETURN_STATUS
123 EFIAPI
124 PeCoffLoaderLoadImage (
125   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
126   )
127 ;
128 
129 VOID *
130 EFIAPI
131 PeCoffLoaderGetPdbPointer (
132   IN VOID  *Pe32Data
133   )
134 ;
135 
136 RETURN_STATUS
137 EFIAPI
138 PeCoffLoaderGetEntryPoint (
139   IN  VOID  *Pe32Data,
140   OUT VOID  **EntryPoint,
141   OUT VOID  **BaseOfImage
142   )
143 ;
144 
145 //
146 // These functions are used by the ARM PE/COFF relocation code and by
147 // the ELF to PE/COFF converter so that is why they are public
148 //
149 
150 /**
151   Pass in a pointer to an ARM MOVT or MOVW immediate instruciton and
152   return the immediate data encoded in the instruction
153 
154   @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction
155 
156   @return Immediate address encoded in the instruction
157 
158 **/
159 UINT16
160 EFIAPI
161 ThumbMovtImmediateAddress (
162   IN UINT16 *Instruction
163   );
164 
165 /**
166   Update an ARM MOVT or MOVW immediate instruction immediate data.
167 
168   @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction
169   @param  Address       New addres to patch into the instruction
170 
171 **/
172 VOID
173 EFIAPI
174 ThumbMovtImmediatePatch (
175   IN OUT UINT16 *Instruction,
176   IN     UINT16 Address
177   );
178 
179 
180 /**
181   Pass in a pointer to an ARM MOVW/MOVT instruciton pair and
182   return the immediate data encoded in the two` instruction
183 
184   @param  Instructions  Pointer to ARM MOVW/MOVT insturction pair
185 
186   @return Immediate address encoded in the instructions
187 
188 **/
189 UINT32
190 EFIAPI
191 ThumbMovwMovtImmediateAddress (
192   IN UINT16 *Instructions
193   );
194 
195 /**
196   Update an ARM MOVW/MOVT immediate instruction instruction pair.
197 
198   @param  Instructions  Pointer to ARM MOVW/MOVT instruction pair
199   @param  Address       New addres to patch into the instructions
200 **/
201 VOID
202 EFIAPI
203 ThumbMovwMovtImmediatePatch (
204   IN OUT UINT16 *Instructions,
205   IN     UINT32 Address
206   );
207 
208 
209 
210 #endif
211