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