]>
Commit | Line | Data |
---|---|---|
d071fb19 | 1 | /** @file\r |
2bfb6009 | 2 | Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but\r |
030cd1a2 | 3 | only supports relocating IA32, x64, IPF, and EBC images.\r |
d071fb19 | 4 | \r |
dc204d5a JY |
5 | Caution: This file requires additional review when modified.\r |
6 | This library will have external input - PE/COFF image.\r | |
7 | This external input must be validated carefully to avoid security issue like\r | |
8 | buffer overflow, integer overflow.\r | |
9 | \r | |
10 | The basic guideline is that caller need provide ImageContext->ImageRead () with the\r | |
11 | necessary data range check, to make sure when this library reads PE/COFF image, the\r | |
12 | PE image buffer is always in valid range.\r | |
13 | This library will also do some additional check for PE header fields.\r | |
14 | \r | |
15 | PeCoffLoaderGetPeHeader() routine will do basic check for PE/COFF header.\r | |
16 | PeCoffLoaderGetImageInfo() routine will do basic check for whole PE/COFF image.\r | |
17 | \r | |
be30ddf7 | 18 | Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r |
acf57dec HT |
19 | Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r |
20 | This program and the accompanying materials\r | |
d071fb19 | 21 | are licensed and made available under the terms and conditions of the BSD License\r |
22 | which accompanies this distribution. The full text of the license may be found at\r | |
2fc59a00 | 23 | http://opensource.org/licenses/bsd-license.php.\r |
d071fb19 | 24 | \r |
25 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
26 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
27 | \r | |
d071fb19 | 28 | **/\r |
29 | \r | |
d071fb19 | 30 | #include "BasePeCoffLibInternals.h"\r |
31 | \r | |
32 | /**\r | |
33 | Retrieves the magic value from the PE/COFF header.\r | |
34 | \r | |
35 | @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r | |
36 | \r | |
37 | @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32\r | |
38 | @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+\r | |
39 | \r | |
40 | **/\r | |
41 | UINT16\r | |
42 | PeCoffLoaderGetPeHeaderMagicValue (\r | |
43 | IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r | |
44 | )\r | |
45 | {\r | |
46 | //\r | |
47 | // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r | |
48 | // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the \r | |
49 | // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r | |
50 | // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r | |
51 | //\r | |
4ab0dff3 | 52 | if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r |
d071fb19 | 53 | return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r |
54 | }\r | |
55 | //\r | |
56 | // Return the magic value from the PC/COFF Optional Header\r | |
57 | //\r | |
58 | return Hdr.Pe32->OptionalHeader.Magic;\r | |
59 | }\r | |
60 | \r | |
61 | \r | |
62 | /**\r | |
28186d45 | 63 | Retrieves the PE or TE Header from a PE/COFF or TE image. \r |
dc204d5a JY |
64 | \r |
65 | Caution: This function may receive untrusted input.\r | |
66 | PE/COFF image is external input, so this routine will \r | |
67 | also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, \r | |
28186d45 | 68 | SizeOfHeader, Section Data Region and Security Data Region be in PE image range. \r |
d071fb19 | 69 | \r |
70 | @param ImageContext The context of the image being loaded.\r | |
71 | @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r | |
72 | \r | |
73 | @retval RETURN_SUCCESS The PE or TE Header is read.\r | |
74 | @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.\r | |
75 | \r | |
76 | **/\r | |
77 | RETURN_STATUS\r | |
78 | PeCoffLoaderGetPeHeader (\r | |
79 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
80 | OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r | |
81 | )\r | |
82 | {\r | |
83 | RETURN_STATUS Status;\r | |
84 | EFI_IMAGE_DOS_HEADER DosHdr;\r | |
85 | UINTN Size;\r | |
3acb1985 | 86 | UINTN ReadSize;\r |
d071fb19 | 87 | UINT16 Magic;\r |
28186d45 ED |
88 | UINT32 SectionHeaderOffset;\r |
89 | UINT32 Index;\r | |
9ca9935a | 90 | UINT32 HeaderWithoutDataDir;\r |
28186d45 | 91 | CHAR8 BufferData;\r |
00b7e3e3 | 92 | UINTN NumberOfSections;\r |
28186d45 | 93 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r |
d071fb19 | 94 | \r |
95 | //\r | |
50cd68df | 96 | // Read the DOS image header to check for its existence\r |
d071fb19 | 97 | //\r |
98 | Size = sizeof (EFI_IMAGE_DOS_HEADER);\r | |
3acb1985 | 99 | ReadSize = Size;\r |
d071fb19 | 100 | Status = ImageContext->ImageRead (\r |
101 | ImageContext->Handle,\r | |
102 | 0,\r | |
103 | &Size,\r | |
104 | &DosHdr\r | |
105 | );\r | |
3acb1985 | 106 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 107 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
108 | if (Size != ReadSize) {\r |
109 | Status = RETURN_UNSUPPORTED;\r | |
110 | }\r | |
d071fb19 | 111 | return Status;\r |
112 | }\r | |
113 | \r | |
114 | ImageContext->PeCoffHeaderOffset = 0;\r | |
115 | if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
116 | //\r | |
117 | // DOS image header is present, so read the PE header after the DOS image\r | |
118 | // header\r | |
119 | //\r | |
120 | ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew;\r | |
121 | }\r | |
122 | \r | |
123 | //\r | |
124 | // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much\r | |
b4500f6e | 125 | // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic\r |
126 | // determines if this is a PE32 or PE32+ image. The magic is in the same\r | |
d071fb19 | 127 | // location in both images.\r |
128 | //\r | |
129 | Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r | |
3acb1985 | 130 | ReadSize = Size;\r |
d071fb19 | 131 | Status = ImageContext->ImageRead (\r |
132 | ImageContext->Handle,\r | |
133 | ImageContext->PeCoffHeaderOffset,\r | |
134 | &Size,\r | |
135 | Hdr.Pe32\r | |
136 | );\r | |
3acb1985 | 137 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 138 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
139 | if (Size != ReadSize) {\r |
140 | Status = RETURN_UNSUPPORTED;\r | |
141 | }\r | |
d071fb19 | 142 | return Status;\r |
143 | }\r | |
144 | \r | |
145 | //\r | |
146 | // Use Signature to figure out if we understand the image format\r | |
147 | //\r | |
148 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
149 | ImageContext->IsTeImage = TRUE;\r | |
150 | ImageContext->Machine = Hdr.Te->Machine;\r | |
151 | ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem);\r | |
2bfb6009 LG |
152 | //\r |
153 | // For TeImage, SectionAlignment is undefined to be set to Zero\r | |
154 | // ImageSize can be calculated.\r | |
155 | //\r | |
d071fb19 | 156 | ImageContext->ImageSize = 0;\r |
2bfb6009 | 157 | ImageContext->SectionAlignment = 0;\r |
d071fb19 | 158 | ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;\r |
159 | \r | |
160 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
161 | ImageContext->IsTeImage = FALSE;\r | |
162 | ImageContext->Machine = Hdr.Pe32->FileHeader.Machine;\r | |
163 | \r | |
164 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
165 | \r | |
166 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
28186d45 | 167 | //\r |
9ca9935a | 168 | // 1. Check OptionalHeader.NumberOfRvaAndSizes filed.\r |
28186d45 ED |
169 | //\r |
170 | if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES < Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes) {\r | |
0054ce56 | 171 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
28186d45 ED |
172 | return RETURN_UNSUPPORTED;\r |
173 | }\r | |
174 | \r | |
28186d45 | 175 | //\r |
9ca9935a ED |
176 | // 2. Check the FileHeader.SizeOfOptionalHeader field.\r |
177 | // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so \r | |
178 | // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.\r | |
28186d45 | 179 | //\r |
9ca9935a ED |
180 | HeaderWithoutDataDir = sizeof (EFI_IMAGE_OPTIONAL_HEADER32) - sizeof (EFI_IMAGE_DATA_DIRECTORY) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;\r |
181 | if (((UINT32)Hdr.Pe32->FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=\r | |
182 | Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY)) {\r | |
183 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
184 | return RETURN_UNSUPPORTED;\r | |
185 | }\r | |
186 | \r | |
187 | SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + Hdr.Pe32->FileHeader.SizeOfOptionalHeader;\r | |
188 | //\r | |
189 | // 3. Check the FileHeader.NumberOfSections field.\r | |
190 | //\r | |
191 | if ((Hdr.Pe32->OptionalHeader.SizeOfImage - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <= Hdr.Pe32->FileHeader.NumberOfSections) {\r | |
192 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
193 | return RETURN_UNSUPPORTED;\r | |
28186d45 ED |
194 | }\r |
195 | \r | |
196 | //\r | |
9ca9935a ED |
197 | // 4. Check the OptionalHeader.SizeOfHeaders field.\r |
198 | //\r | |
199 | if ((Hdr.Pe32->OptionalHeader.SizeOfHeaders - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER < (UINT32)Hdr.Pe32->FileHeader.NumberOfSections) {\r | |
200 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
201 | return RETURN_UNSUPPORTED;\r | |
202 | }\r | |
203 | \r | |
204 | //\r | |
205 | // 4.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the file.\r | |
28186d45 ED |
206 | //\r |
207 | Size = 1;\r | |
3acb1985 | 208 | ReadSize = Size;\r |
28186d45 ED |
209 | Status = ImageContext->ImageRead (\r |
210 | ImageContext->Handle,\r | |
211 | Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,\r | |
212 | &Size,\r | |
213 | &BufferData\r | |
214 | );\r | |
3acb1985 | 215 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
216 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
217 | if (Size != ReadSize) {\r | |
218 | Status = RETURN_UNSUPPORTED;\r | |
219 | }\r | |
28186d45 ED |
220 | return Status;\r |
221 | }\r | |
222 | \r | |
223 | //\r | |
224 | // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.\r | |
225 | // Read the last byte to make sure the data is in the image region.\r | |
226 | // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.\r | |
227 | //\r | |
228 | if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY < Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes) {\r | |
229 | if (Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size != 0) {\r | |
230 | //\r | |
231 | // Check the member data to avoid overflow.\r | |
232 | //\r | |
233 | if ((UINT32) (~0) - Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress <\r | |
234 | Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size) {\r | |
0054ce56 ED |
235 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
236 | return RETURN_UNSUPPORTED;\r | |
28186d45 ED |
237 | }\r |
238 | \r | |
239 | //\r | |
3acb1985 | 240 | // Read last byte of section header from file\r |
28186d45 ED |
241 | //\r |
242 | Size = 1;\r | |
3acb1985 | 243 | ReadSize = Size;\r |
28186d45 ED |
244 | Status = ImageContext->ImageRead (\r |
245 | ImageContext->Handle,\r | |
246 | Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r | |
247 | Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size - 1,\r | |
248 | &Size,\r | |
249 | &BufferData\r | |
250 | );\r | |
3acb1985 | 251 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
252 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
253 | if (Size != ReadSize) {\r | |
254 | Status = RETURN_UNSUPPORTED;\r | |
255 | }\r | |
28186d45 ED |
256 | return Status;\r |
257 | }\r | |
258 | }\r | |
259 | }\r | |
260 | \r | |
d071fb19 | 261 | //\r |
262 | // Use PE32 offset\r | |
263 | //\r | |
264 | ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem;\r | |
265 | ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage;\r | |
266 | ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment;\r | |
267 | ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r | |
268 | \r | |
269 | } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r | |
28186d45 | 270 | //\r |
9ca9935a | 271 | // 1. Check FileHeader.NumberOfRvaAndSizes filed.\r |
28186d45 ED |
272 | //\r |
273 | if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES < Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes) {\r | |
0054ce56 | 274 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
28186d45 ED |
275 | return RETURN_UNSUPPORTED;\r |
276 | }\r | |
9ca9935a ED |
277 | //\r |
278 | // 2. Check the FileHeader.SizeOfOptionalHeader field.\r | |
279 | // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so \r | |
280 | // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.\r | |
281 | //\r | |
282 | HeaderWithoutDataDir = sizeof (EFI_IMAGE_OPTIONAL_HEADER64) - sizeof (EFI_IMAGE_DATA_DIRECTORY) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;\r | |
283 | if (((UINT32)Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=\r | |
284 | Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY)) {\r | |
285 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
286 | return RETURN_UNSUPPORTED;\r | |
287 | }\r | |
28186d45 | 288 | \r |
9ca9935a | 289 | SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;\r |
28186d45 | 290 | //\r |
9ca9935a | 291 | // 3. Check the FileHeader.NumberOfSections field.\r |
28186d45 | 292 | //\r |
9ca9935a ED |
293 | if ((Hdr.Pe32Plus->OptionalHeader.SizeOfImage - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <= Hdr.Pe32Plus->FileHeader.NumberOfSections) {\r |
294 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
295 | return RETURN_UNSUPPORTED;\r | |
296 | }\r | |
297 | \r | |
298 | //\r | |
299 | // 4. Check the OptionalHeader.SizeOfHeaders field.\r | |
300 | //\r | |
301 | if ((Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER < (UINT32)Hdr.Pe32Plus->FileHeader.NumberOfSections) {\r | |
302 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
303 | return RETURN_UNSUPPORTED;\r | |
28186d45 ED |
304 | }\r |
305 | \r | |
306 | //\r | |
9ca9935a | 307 | // 4.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from the file.\r |
28186d45 ED |
308 | //\r |
309 | Size = 1;\r | |
3acb1985 | 310 | ReadSize = Size;\r |
28186d45 ED |
311 | Status = ImageContext->ImageRead (\r |
312 | ImageContext->Handle,\r | |
313 | Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,\r | |
314 | &Size,\r | |
315 | &BufferData\r | |
316 | );\r | |
3acb1985 | 317 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
318 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
319 | if (Size != ReadSize) {\r | |
320 | Status = RETURN_UNSUPPORTED;\r | |
321 | }\r | |
28186d45 ED |
322 | return Status;\r |
323 | }\r | |
324 | \r | |
325 | //\r | |
326 | // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.\r | |
327 | // Read the last byte to make sure the data is in the image region.\r | |
328 | // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.\r | |
329 | //\r | |
330 | if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY < Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes) {\r | |
331 | if (Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size != 0) {\r | |
332 | //\r | |
333 | // Check the member data to avoid overflow.\r | |
334 | //\r | |
335 | if ((UINT32) (~0) - Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress <\r | |
336 | Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size) {\r | |
0054ce56 ED |
337 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
338 | return RETURN_UNSUPPORTED;\r | |
28186d45 ED |
339 | }\r |
340 | \r | |
341 | //\r | |
3acb1985 | 342 | // Read last byte of section header from file\r |
28186d45 ED |
343 | //\r |
344 | Size = 1;\r | |
3acb1985 | 345 | ReadSize = Size;\r |
28186d45 ED |
346 | Status = ImageContext->ImageRead (\r |
347 | ImageContext->Handle,\r | |
348 | Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +\r | |
349 | Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size - 1,\r | |
350 | &Size,\r | |
351 | &BufferData\r | |
352 | );\r | |
3acb1985 | 353 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
354 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
355 | if (Size != ReadSize) {\r | |
356 | Status = RETURN_UNSUPPORTED;\r | |
357 | }\r | |
28186d45 ED |
358 | return Status;\r |
359 | }\r | |
360 | }\r | |
361 | }\r | |
362 | \r | |
d071fb19 | 363 | //\r |
364 | // Use PE32+ offset\r | |
365 | //\r | |
366 | ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem;\r | |
367 | ImageContext->ImageSize = (UINT64) Hdr.Pe32Plus->OptionalHeader.SizeOfImage;\r | |
368 | ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;\r | |
369 | ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;\r | |
370 | } else {\r | |
371 | ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r | |
372 | return RETURN_UNSUPPORTED;\r | |
373 | }\r | |
374 | } else {\r | |
375 | ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r | |
376 | return RETURN_UNSUPPORTED;\r | |
377 | }\r | |
378 | \r | |
379 | if (!PeCoffLoaderImageFormatSupported (ImageContext->Machine)) {\r | |
380 | //\r | |
381 | // If the PE/COFF loader does not support the image type return\r | |
b4500f6e | 382 | // unsupported. This library can support lots of types of images\r |
d071fb19 | 383 | // this does not mean the user of this library can call the entry\r |
384 | // point of the image.\r | |
385 | //\r | |
386 | return RETURN_UNSUPPORTED;\r | |
387 | }\r | |
388 | \r | |
28186d45 ED |
389 | //\r |
390 | // Check each section field.\r | |
391 | //\r | |
00b7e3e3 ED |
392 | if (ImageContext->IsTeImage) {\r |
393 | SectionHeaderOffset = sizeof(EFI_TE_IMAGE_HEADER);\r | |
394 | NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);\r | |
395 | } else {\r | |
396 | SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + Hdr.Pe32->FileHeader.SizeOfOptionalHeader;\r | |
397 | NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);\r | |
398 | }\r | |
399 | \r | |
400 | for (Index = 0; Index < NumberOfSections; Index++) {\r | |
28186d45 ED |
401 | //\r |
402 | // Read section header from file\r | |
403 | //\r | |
404 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
3acb1985 | 405 | ReadSize = Size;\r |
28186d45 ED |
406 | Status = ImageContext->ImageRead (\r |
407 | ImageContext->Handle,\r | |
408 | SectionHeaderOffset,\r | |
409 | &Size,\r | |
410 | &SectionHeader\r | |
411 | );\r | |
3acb1985 | 412 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
413 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
414 | if (Size != ReadSize) {\r | |
415 | Status = RETURN_UNSUPPORTED;\r | |
416 | }\r | |
28186d45 ED |
417 | return Status;\r |
418 | }\r | |
419 | \r | |
420 | if (SectionHeader.SizeOfRawData > 0) {\r | |
9ca9935a ED |
421 | //\r |
422 | // Section data should bigger than the Pe header.\r | |
423 | //\r | |
424 | if (SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders || \r | |
425 | SectionHeader.PointerToRawData < ImageContext->SizeOfHeaders) {\r | |
426 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
427 | return RETURN_UNSUPPORTED;\r | |
428 | }\r | |
429 | \r | |
28186d45 ED |
430 | //\r |
431 | // Check the member data to avoid overflow.\r | |
432 | //\r | |
433 | if ((UINT32) (~0) - SectionHeader.PointerToRawData < SectionHeader.SizeOfRawData) {\r | |
0054ce56 ED |
434 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
435 | return RETURN_UNSUPPORTED;\r | |
28186d45 ED |
436 | }\r |
437 | \r | |
438 | //\r | |
439 | // Base on the ImageRead function to check the section data field.\r | |
440 | // Read the last byte to make sure the data is in the image region.\r | |
441 | //\r | |
442 | Size = 1;\r | |
3acb1985 | 443 | ReadSize = Size;\r |
28186d45 ED |
444 | Status = ImageContext->ImageRead (\r |
445 | ImageContext->Handle,\r | |
446 | SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,\r | |
447 | &Size,\r | |
448 | &BufferData\r | |
449 | );\r | |
3acb1985 | 450 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
0054ce56 ED |
451 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
452 | if (Size != ReadSize) {\r | |
453 | Status = RETURN_UNSUPPORTED;\r | |
454 | }\r | |
28186d45 ED |
455 | return Status;\r |
456 | }\r | |
457 | }\r | |
458 | \r | |
459 | //\r | |
460 | // Check next section.\r | |
461 | //\r | |
462 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
463 | }\r | |
464 | \r | |
d071fb19 | 465 | return RETURN_SUCCESS;\r |
466 | }\r | |
467 | \r | |
468 | \r | |
469 | /**\r | |
470 | Retrieves information about a PE/COFF image.\r | |
471 | \r | |
3ecdcd11 LG |
472 | Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, \r |
473 | DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and \r | |
474 | DebugDirectoryEntryRva fields of the ImageContext structure. \r | |
475 | If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. \r | |
476 | If the PE/COFF image accessed through the ImageRead service in the ImageContext \r | |
477 | structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. \r | |
478 | If any errors occur while computing the fields of ImageContext, \r | |
479 | then the error status is returned in the ImageError field of ImageContext. \r | |
480 | If the image is a TE image, then SectionAlignment is set to 0.\r | |
122e2191 | 481 | The ImageRead and Handle fields of ImageContext structure must be valid prior \r |
482 | to invoking this service.\r | |
d071fb19 | 483 | \r |
dc204d5a JY |
484 | Caution: This function may receive untrusted input.\r |
485 | PE/COFF image is external input, so this routine will \r | |
486 | also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, \r | |
28186d45 ED |
487 | SizeOfHeader, Section Data Region and Security Data Region be in PE image range. \r |
488 | \r | |
2fc59a00 | 489 | @param ImageContext The pointer to the image context structure that describes the PE/COFF\r |
d071fb19 | 490 | image that needs to be examined by this function.\r |
491 | \r | |
492 | @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r | |
493 | @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r | |
494 | @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r | |
495 | \r | |
496 | **/\r | |
497 | RETURN_STATUS\r | |
498 | EFIAPI\r | |
499 | PeCoffLoaderGetImageInfo (\r | |
efb23117 | 500 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r |
d071fb19 | 501 | )\r |
502 | {\r | |
503 | RETURN_STATUS Status;\r | |
504 | EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r | |
505 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
506 | EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;\r | |
507 | UINTN Size;\r | |
3acb1985 | 508 | UINTN ReadSize;\r |
d071fb19 | 509 | UINTN Index;\r |
510 | UINTN DebugDirectoryEntryRva;\r | |
511 | UINTN DebugDirectoryEntryFileOffset;\r | |
512 | UINTN SectionHeaderOffset;\r | |
513 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r | |
514 | EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry;\r | |
515 | UINT32 NumberOfRvaAndSizes;\r | |
516 | UINT16 Magic;\r | |
517 | \r | |
2bfb6009 | 518 | if (ImageContext == NULL) {\r |
d071fb19 | 519 | return RETURN_INVALID_PARAMETER;\r |
520 | }\r | |
521 | //\r | |
522 | // Assume success\r | |
523 | //\r | |
524 | ImageContext->ImageError = IMAGE_ERROR_SUCCESS;\r | |
525 | \r | |
526 | Hdr.Union = &HdrData;\r | |
527 | Status = PeCoffLoaderGetPeHeader (ImageContext, Hdr);\r | |
528 | if (RETURN_ERROR (Status)) {\r | |
529 | return Status;\r | |
530 | }\r | |
531 | \r | |
532 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
533 | \r | |
534 | //\r | |
535 | // Retrieve the base address of the image\r | |
536 | //\r | |
537 | if (!(ImageContext->IsTeImage)) {\r | |
538 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
539 | //\r | |
540 | // Use PE32 offset\r | |
541 | //\r | |
542 | ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.ImageBase;\r | |
543 | } else {\r | |
544 | //\r | |
545 | // Use PE32+ offset\r | |
546 | //\r | |
547 | ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase;\r | |
548 | }\r | |
549 | } else {\r | |
550 | ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));\r | |
551 | }\r | |
552 | \r | |
553 | //\r | |
554 | // Initialize the alternate destination address to 0 indicating that it\r | |
555 | // should not be used.\r | |
556 | //\r | |
557 | ImageContext->DestinationAddress = 0;\r | |
558 | \r | |
559 | //\r | |
1a82ed3d | 560 | // Initialize the debug codeview pointer.\r |
d071fb19 | 561 | //\r |
1a82ed3d LG |
562 | ImageContext->DebugDirectoryEntryRva = 0;\r |
563 | ImageContext->CodeView = NULL;\r | |
564 | ImageContext->PdbPointer = NULL;\r | |
d071fb19 | 565 | \r |
566 | //\r | |
567 | // Three cases with regards to relocations:\r | |
568 | // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r | |
569 | // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r | |
570 | // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r | |
571 | // has no base relocs to apply\r | |
572 | // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r | |
573 | //\r | |
574 | // Look at the file header to determine if relocations have been stripped, and\r | |
58380e9c | 575 | // save this information in the image context for later use.\r |
d071fb19 | 576 | //\r |
577 | if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {\r | |
578 | ImageContext->RelocationsStripped = TRUE;\r | |
9626a87e LG |
579 | } else if ((ImageContext->IsTeImage) && (Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r |
580 | ImageContext->RelocationsStripped = TRUE;\r | |
d071fb19 | 581 | } else {\r |
582 | ImageContext->RelocationsStripped = FALSE;\r | |
583 | }\r | |
6beb225a LG |
584 | \r |
585 | //\r | |
586 | // TE Image Relocation Data Directory Entry size is non-zero, but the Relocation Data Directory Virtual Address is zero.\r | |
587 | // This case is not a valid TE image. \r | |
588 | //\r | |
589 | if ((ImageContext->IsTeImage) && (Hdr.Te->DataDirectory[0].Size != 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r | |
0054ce56 ED |
590 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
591 | return RETURN_UNSUPPORTED;\r | |
6beb225a | 592 | }\r |
d071fb19 | 593 | \r |
594 | if (!(ImageContext->IsTeImage)) {\r | |
595 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
596 | //\r | |
597 | // Use PE32 offset\r | |
598 | //\r | |
599 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
600 | DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
601 | } else {\r | |
602 | //\r | |
603 | // Use PE32+ offset\r | |
604 | //\r | |
605 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
606 | DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
607 | }\r | |
608 | \r | |
609 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r | |
610 | \r | |
611 | DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;\r | |
612 | \r | |
613 | //\r | |
614 | // Determine the file offset of the debug directory... This means we walk\r | |
615 | // the sections to find which section contains the RVA of the debug\r | |
616 | // directory\r | |
617 | //\r | |
618 | DebugDirectoryEntryFileOffset = 0;\r | |
619 | \r | |
620 | SectionHeaderOffset = (UINTN)(\r | |
621 | ImageContext->PeCoffHeaderOffset +\r | |
622 | sizeof (UINT32) +\r | |
623 | sizeof (EFI_IMAGE_FILE_HEADER) +\r | |
624 | Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r | |
625 | );\r | |
626 | \r | |
627 | for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r | |
628 | //\r | |
629 | // Read section header from file\r | |
630 | //\r | |
631 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
3acb1985 | 632 | ReadSize = Size;\r |
d071fb19 | 633 | Status = ImageContext->ImageRead (\r |
634 | ImageContext->Handle,\r | |
635 | SectionHeaderOffset,\r | |
636 | &Size,\r | |
637 | &SectionHeader\r | |
638 | );\r | |
3acb1985 | 639 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 640 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
641 | if (Size != ReadSize) {\r |
642 | Status = RETURN_UNSUPPORTED;\r | |
643 | }\r | |
d071fb19 | 644 | return Status;\r |
645 | }\r | |
646 | \r | |
647 | if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&\r | |
648 | DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {\r | |
649 | \r | |
650 | DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData;\r | |
651 | break;\r | |
652 | }\r | |
653 | \r | |
654 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
655 | }\r | |
656 | \r | |
657 | if (DebugDirectoryEntryFileOffset != 0) {\r | |
658 | for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r | |
659 | //\r | |
660 | // Read next debug directory entry\r | |
661 | //\r | |
662 | Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r | |
3acb1985 | 663 | ReadSize = Size;\r |
d071fb19 | 664 | Status = ImageContext->ImageRead (\r |
665 | ImageContext->Handle,\r | |
be30ddf7 | 666 | DebugDirectoryEntryFileOffset + Index,\r |
d071fb19 | 667 | &Size,\r |
668 | &DebugEntry\r | |
669 | );\r | |
3acb1985 | 670 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 671 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
672 | if (Size != ReadSize) {\r |
673 | Status = RETURN_UNSUPPORTED;\r | |
674 | }\r | |
d071fb19 | 675 | return Status;\r |
676 | }\r | |
36b48bf6 ED |
677 | \r |
678 | //\r | |
679 | // From PeCoff spec, when DebugEntry.RVA == 0 means this debug info will not load into memory.\r | |
680 | // Here we will always load EFI_IMAGE_DEBUG_TYPE_CODEVIEW type debug info. so need adjust the\r | |
681 | // ImageContext->ImageSize when DebugEntry.RVA == 0.\r | |
682 | //\r | |
d071fb19 | 683 | if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r |
684 | ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);\r | |
685 | if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) {\r | |
686 | ImageContext->ImageSize += DebugEntry.SizeOfData;\r | |
687 | }\r | |
688 | \r | |
689 | return RETURN_SUCCESS;\r | |
690 | }\r | |
691 | }\r | |
692 | }\r | |
693 | }\r | |
694 | } else {\r | |
695 | \r | |
696 | DebugDirectoryEntry = &Hdr.Te->DataDirectory[1];\r | |
697 | DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;\r | |
698 | SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER));\r | |
699 | \r | |
700 | DebugDirectoryEntryFileOffset = 0;\r | |
701 | \r | |
702 | for (Index = 0; Index < Hdr.Te->NumberOfSections;) {\r | |
703 | //\r | |
704 | // Read section header from file\r | |
705 | //\r | |
706 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
3acb1985 | 707 | ReadSize = Size;\r |
d071fb19 | 708 | Status = ImageContext->ImageRead (\r |
709 | ImageContext->Handle,\r | |
710 | SectionHeaderOffset,\r | |
711 | &Size,\r | |
712 | &SectionHeader\r | |
713 | );\r | |
3acb1985 | 714 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 715 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
716 | if (Size != ReadSize) {\r |
717 | Status = RETURN_UNSUPPORTED;\r | |
718 | }\r | |
d071fb19 | 719 | return Status;\r |
720 | }\r | |
721 | \r | |
722 | if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&\r | |
723 | DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {\r | |
724 | DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva -\r | |
725 | SectionHeader.VirtualAddress +\r | |
726 | SectionHeader.PointerToRawData +\r | |
727 | sizeof (EFI_TE_IMAGE_HEADER) -\r | |
728 | Hdr.Te->StrippedSize;\r | |
729 | \r | |
730 | //\r | |
731 | // File offset of the debug directory was found, if this is not the last\r | |
732 | // section, then skip to the last section for calculating the image size.\r | |
733 | //\r | |
734 | if (Index < (UINTN) Hdr.Te->NumberOfSections - 1) {\r | |
735 | SectionHeaderOffset += (Hdr.Te->NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
736 | Index = Hdr.Te->NumberOfSections - 1;\r | |
737 | continue;\r | |
738 | }\r | |
739 | }\r | |
740 | \r | |
741 | //\r | |
742 | // In Te image header there is not a field to describe the ImageSize.\r | |
743 | // Actually, the ImageSize equals the RVA plus the VirtualSize of\r | |
744 | // the last section mapped into memory (Must be rounded up to\r | |
b4500f6e | 745 | // a multiple of Section Alignment). Per the PE/COFF specification, the\r |
d071fb19 | 746 | // section headers in the Section Table must appear in order of the RVA\r |
747 | // values for the corresponding sections. So the ImageSize can be determined\r | |
748 | // by the RVA and the VirtualSize of the last section header in the\r | |
2bfb6009 | 749 | // Section Table. \r |
d071fb19 | 750 | //\r |
751 | if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) {\r | |
2bfb6009 | 752 | ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize);\r |
d071fb19 | 753 | }\r |
754 | \r | |
755 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
756 | }\r | |
757 | \r | |
758 | if (DebugDirectoryEntryFileOffset != 0) {\r | |
759 | for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r | |
760 | //\r | |
761 | // Read next debug directory entry\r | |
762 | //\r | |
763 | Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r | |
3acb1985 | 764 | ReadSize = Size;\r |
d071fb19 | 765 | Status = ImageContext->ImageRead (\r |
766 | ImageContext->Handle,\r | |
be30ddf7 | 767 | DebugDirectoryEntryFileOffset + Index,\r |
d071fb19 | 768 | &Size,\r |
769 | &DebugEntry\r | |
770 | );\r | |
3acb1985 | 771 | if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r |
d071fb19 | 772 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r |
0054ce56 ED |
773 | if (Size != ReadSize) {\r |
774 | Status = RETURN_UNSUPPORTED;\r | |
775 | }\r | |
d071fb19 | 776 | return Status;\r |
777 | }\r | |
778 | \r | |
779 | if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r | |
780 | ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);\r | |
781 | return RETURN_SUCCESS;\r | |
782 | }\r | |
783 | }\r | |
784 | }\r | |
785 | }\r | |
786 | \r | |
787 | return RETURN_SUCCESS;\r | |
788 | }\r | |
789 | \r | |
790 | \r | |
791 | /**\r | |
792 | Converts an image address to the loaded address.\r | |
793 | \r | |
794 | @param ImageContext The context of the image being loaded.\r | |
9833a9bb | 795 | @param Address The relative virtual address to be converted to the loaded address.\r |
d071fb19 | 796 | \r |
797 | @return The converted address or NULL if the address can not be converted.\r | |
798 | \r | |
799 | **/\r | |
800 | VOID *\r | |
801 | PeCoffLoaderImageAddress (\r | |
802 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
803 | IN UINTN Address\r | |
804 | )\r | |
805 | {\r | |
806 | //\r | |
9833a9bb | 807 | // Make sure that Address and ImageSize is correct for the loaded image.\r |
d071fb19 | 808 | //\r |
809 | if (Address >= ImageContext->ImageSize) {\r | |
810 | ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;\r | |
811 | return NULL;\r | |
812 | }\r | |
813 | \r | |
814 | return (CHAR8 *)((UINTN) ImageContext->ImageAddress + Address);\r | |
815 | }\r | |
816 | \r | |
817 | /**\r | |
818 | Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r | |
819 | \r | |
820 | If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r | |
821 | ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r | |
822 | of ImageContext as the relocation base address. The caller must allocate the relocation\r | |
823 | fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.\r | |
efb23117 | 824 | \r |
825 | The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, \r | |
826 | ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, \r | |
827 | DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of \r | |
828 | the ImageContext structure must be valid prior to invoking this service.\r | |
829 | \r | |
d071fb19 | 830 | If ImageContext is NULL, then ASSERT().\r |
831 | \r | |
8d579453 | 832 | Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r |
833 | cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r | |
834 | prior to transferring control to a PE/COFF image that is loaded using this library.\r | |
835 | \r | |
2fc59a00 | 836 | @param ImageContext The pointer to the image context structure that describes the PE/COFF\r |
d071fb19 | 837 | image that is being relocated.\r |
838 | \r | |
839 | @retval RETURN_SUCCESS The PE/COFF image was relocated.\r | |
840 | Extended status information is in the ImageError field of ImageContext.\r | |
841 | @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r | |
842 | Extended status information is in the ImageError field of ImageContext.\r | |
843 | @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r | |
844 | Extended status information is in the ImageError field of ImageContext.\r | |
845 | \r | |
846 | **/\r | |
847 | RETURN_STATUS\r | |
848 | EFIAPI\r | |
849 | PeCoffLoaderRelocateImage (\r | |
850 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
851 | )\r | |
852 | {\r | |
853 | RETURN_STATUS Status;\r | |
854 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
855 | EFI_IMAGE_DATA_DIRECTORY *RelocDir;\r | |
856 | UINT64 Adjust;\r | |
857 | EFI_IMAGE_BASE_RELOCATION *RelocBase;\r | |
858 | EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;\r | |
859 | UINT16 *Reloc;\r | |
860 | UINT16 *RelocEnd;\r | |
861 | CHAR8 *Fixup;\r | |
862 | CHAR8 *FixupBase;\r | |
1fa524e9 | 863 | UINT16 *Fixup16;\r |
864 | UINT32 *Fixup32;\r | |
865 | UINT64 *Fixup64;\r | |
d071fb19 | 866 | CHAR8 *FixupData;\r |
867 | PHYSICAL_ADDRESS BaseAddress;\r | |
868 | UINT32 NumberOfRvaAndSizes;\r | |
869 | UINT16 Magic;\r | |
870 | \r | |
871 | ASSERT (ImageContext != NULL);\r | |
872 | \r | |
873 | //\r | |
874 | // Assume success\r | |
875 | //\r | |
876 | ImageContext->ImageError = IMAGE_ERROR_SUCCESS;\r | |
877 | \r | |
878 | //\r | |
879 | // If there are no relocation entries, then we are done\r | |
880 | //\r | |
881 | if (ImageContext->RelocationsStripped) {\r | |
b4500f6e | 882 | // Applies additional environment specific actions to relocate fixups \r |
883 | // to a PE/COFF image if needed\r | |
01a54966 | 884 | PeCoffLoaderRelocateImageExtraAction (ImageContext); \r |
d071fb19 | 885 | return RETURN_SUCCESS;\r |
886 | }\r | |
887 | \r | |
888 | //\r | |
889 | // If the destination address is not 0, use that rather than the\r | |
890 | // image address as the relocation target.\r | |
891 | //\r | |
892 | if (ImageContext->DestinationAddress != 0) {\r | |
893 | BaseAddress = ImageContext->DestinationAddress;\r | |
d071fb19 | 894 | } else {\r |
19bee90c | 895 | BaseAddress = ImageContext->ImageAddress;\r |
d071fb19 | 896 | }\r |
897 | \r | |
898 | if (!(ImageContext->IsTeImage)) {\r | |
899 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);\r | |
900 | \r | |
901 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
902 | \r | |
903 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
904 | //\r | |
905 | // Use PE32 offset\r | |
906 | //\r | |
907 | Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;\r | |
2816e216 | 908 | if (Adjust != 0) {\r |
909 | Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;\r | |
910 | }\r | |
d071fb19 | 911 | \r |
912 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
913 | RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
914 | } else {\r | |
915 | //\r | |
916 | // Use PE32+ offset\r | |
917 | //\r | |
918 | Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase;\r | |
2816e216 | 919 | if (Adjust != 0) {\r |
920 | Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;\r | |
921 | }\r | |
d071fb19 | 922 | \r |
923 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
924 | RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
925 | }\r | |
926 | \r | |
927 | //\r | |
928 | // Find the relocation block\r | |
929 | // Per the PE/COFF spec, you can't assume that a given data directory\r | |
930 | // is present in the image. You have to check the NumberOfRvaAndSizes in\r | |
931 | // the optional header to verify a desired directory entry is there.\r | |
932 | //\r | |
933 | \r | |
9833a9bb | 934 | if ((NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) && (RelocDir->Size > 0)) {\r |
d071fb19 | 935 | RelocBase = PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress);\r |
936 | RelocBaseEnd = PeCoffLoaderImageAddress (\r | |
937 | ImageContext,\r | |
938 | RelocDir->VirtualAddress + RelocDir->Size - 1\r | |
939 | );\r | |
9833a9bb LG |
940 | if (RelocBase == NULL || RelocBaseEnd == NULL) {\r |
941 | return RETURN_LOAD_ERROR;\r | |
942 | }\r | |
d071fb19 | 943 | } else {\r |
944 | //\r | |
945 | // Set base and end to bypass processing below.\r | |
946 | //\r | |
9833a9bb | 947 | RelocBase = RelocBaseEnd = NULL;\r |
d071fb19 | 948 | }\r |
949 | } else {\r | |
950 | Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\r | |
19bee90c | 951 | Adjust = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->ImageBase);\r |
2816e216 | 952 | if (Adjust != 0) {\r |
953 | Hdr.Te->ImageBase = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER));\r | |
954 | }\r | |
d071fb19 | 955 | \r |
956 | //\r | |
957 | // Find the relocation block\r | |
958 | //\r | |
959 | RelocDir = &Hdr.Te->DataDirectory[0];\r | |
9833a9bb LG |
960 | if (RelocDir->Size > 0) {\r |
961 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(\r | |
962 | ImageContext->ImageAddress +\r | |
963 | RelocDir->VirtualAddress +\r | |
964 | sizeof(EFI_TE_IMAGE_HEADER) -\r | |
965 | Hdr.Te->StrippedSize\r | |
966 | );\r | |
967 | RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1);\r | |
968 | } else {\r | |
969 | //\r | |
970 | // Set base and end to bypass processing below.\r | |
971 | //\r | |
972 | RelocBase = RelocBaseEnd = NULL; \r | |
973 | }\r | |
d071fb19 | 974 | }\r |
975 | \r | |
976 | //\r | |
01e1171e | 977 | // If Adjust is not zero, then apply fix ups to the image\r |
d071fb19 | 978 | //\r |
01e1171e | 979 | if (Adjust != 0) {\r |
9833a9bb | 980 | //\r |
01e1171e | 981 | // Run the relocation information and apply the fixups\r |
9833a9bb | 982 | //\r |
01e1171e | 983 | FixupData = ImageContext->FixupData;\r |
984 | while (RelocBase < RelocBaseEnd) {\r | |
9833a9bb | 985 | \r |
01e1171e | 986 | Reloc = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r |
987 | RelocEnd = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);\r | |
988 | \r | |
989 | //\r | |
990 | // Make sure RelocEnd is in the Image range.\r | |
991 | //\r | |
992 | if ((CHAR8 *) RelocEnd < (CHAR8 *)((UINTN) ImageContext->ImageAddress) ||\r | |
993 | (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress + (UINTN)ImageContext->ImageSize)) {\r | |
994 | ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r | |
9833a9bb LG |
995 | return RETURN_LOAD_ERROR;\r |
996 | }\r | |
d071fb19 | 997 | \r |
01e1171e | 998 | if (!(ImageContext->IsTeImage)) {\r |
999 | FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);\r | |
1000 | if (FixupBase == NULL) {\r | |
1001 | return RETURN_LOAD_ERROR;\r | |
d071fb19 | 1002 | }\r |
01e1171e | 1003 | } else {\r |
1004 | FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +\r | |
1005 | RelocBase->VirtualAddress +\r | |
1006 | sizeof(EFI_TE_IMAGE_HEADER) -\r | |
1007 | Hdr.Te->StrippedSize\r | |
1008 | );\r | |
1009 | } \r | |
1010 | \r | |
1011 | //\r | |
1012 | // Run this relocation record\r | |
1013 | //\r | |
1014 | while (Reloc < RelocEnd) {\r | |
1015 | \r | |
1016 | Fixup = FixupBase + (*Reloc & 0xFFF);\r | |
1017 | switch ((*Reloc) >> 12) {\r | |
1018 | case EFI_IMAGE_REL_BASED_ABSOLUTE:\r | |
1019 | break;\r | |
1020 | \r | |
1021 | case EFI_IMAGE_REL_BASED_HIGH:\r | |
1022 | Fixup16 = (UINT16 *) Fixup;\r | |
1023 | *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r | |
1024 | if (FixupData != NULL) {\r | |
1025 | *(UINT16 *) FixupData = *Fixup16;\r | |
1026 | FixupData = FixupData + sizeof (UINT16);\r | |
1027 | }\r | |
1028 | break;\r | |
1029 | \r | |
1030 | case EFI_IMAGE_REL_BASED_LOW:\r | |
1031 | Fixup16 = (UINT16 *) Fixup;\r | |
1032 | *Fixup16 = (UINT16) (*Fixup16 + (UINT16) Adjust);\r | |
1033 | if (FixupData != NULL) {\r | |
1034 | *(UINT16 *) FixupData = *Fixup16;\r | |
1035 | FixupData = FixupData + sizeof (UINT16);\r | |
1036 | }\r | |
1037 | break;\r | |
1038 | \r | |
1039 | case EFI_IMAGE_REL_BASED_HIGHLOW:\r | |
1040 | Fixup32 = (UINT32 *) Fixup;\r | |
1041 | *Fixup32 = *Fixup32 + (UINT32) Adjust;\r | |
1042 | if (FixupData != NULL) {\r | |
1043 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r | |
1044 | *(UINT32 *)FixupData = *Fixup32;\r | |
1045 | FixupData = FixupData + sizeof (UINT32);\r | |
1046 | }\r | |
1047 | break;\r | |
1048 | \r | |
1049 | case EFI_IMAGE_REL_BASED_DIR64:\r | |
1050 | Fixup64 = (UINT64 *) Fixup;\r | |
1051 | *Fixup64 = *Fixup64 + (UINT64) Adjust;\r | |
1052 | if (FixupData != NULL) {\r | |
1053 | FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));\r | |
1054 | *(UINT64 *)(FixupData) = *Fixup64;\r | |
1055 | FixupData = FixupData + sizeof(UINT64);\r | |
1056 | }\r | |
1057 | break;\r | |
1058 | \r | |
1059 | default:\r | |
1060 | //\r | |
1061 | // The common code does not handle some of the stranger IPF relocations\r | |
1062 | // PeCoffLoaderRelocateImageEx () adds support for these complex fixups\r | |
1063 | // on IPF and is a No-Op on other architectures.\r | |
1064 | //\r | |
1065 | Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r | |
1066 | if (RETURN_ERROR (Status)) {\r | |
1067 | ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r | |
1068 | return Status;\r | |
1069 | }\r | |
d071fb19 | 1070 | }\r |
d071fb19 | 1071 | \r |
d071fb19 | 1072 | //\r |
01e1171e | 1073 | // Next relocation record\r |
d071fb19 | 1074 | //\r |
01e1171e | 1075 | Reloc += 1;\r |
d071fb19 | 1076 | }\r |
1077 | \r | |
1078 | //\r | |
01e1171e | 1079 | // Next reloc block\r |
d071fb19 | 1080 | //\r |
01e1171e | 1081 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r |
d071fb19 | 1082 | }\r |
1083 | \r | |
1084 | //\r | |
01e1171e | 1085 | // Adjust the EntryPoint to match the linked-to address\r |
d071fb19 | 1086 | //\r |
01e1171e | 1087 | if (ImageContext->DestinationAddress != 0) {\r |
1088 | ImageContext->EntryPoint -= (UINT64) ImageContext->ImageAddress;\r | |
1089 | ImageContext->EntryPoint += (UINT64) ImageContext->DestinationAddress;\r | |
1090 | }\r | |
19bee90c | 1091 | }\r |
27b2d249 | 1092 | \r |
1093 | // Applies additional environment specific actions to relocate fixups \r | |
1094 | // to a PE/COFF image if needed\r | |
1095 | PeCoffLoaderRelocateImageExtraAction (ImageContext);\r | |
1096 | \r | |
d071fb19 | 1097 | return RETURN_SUCCESS;\r |
1098 | }\r | |
1099 | \r | |
1100 | /**\r | |
1101 | Loads a PE/COFF image into memory.\r | |
1102 | \r | |
1103 | Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r | |
1104 | specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r | |
1105 | the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r | |
b4500f6e | 1106 | The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.\r |
efb23117 | 1107 | The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize, \r |
1108 | DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva \r | |
1109 | fields of the ImageContext structure must be valid prior to invoking this service.\r | |
1110 | \r | |
d071fb19 | 1111 | If ImageContext is NULL, then ASSERT().\r |
1112 | \r | |
8d579453 | 1113 | Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r |
1114 | cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r | |
1115 | prior to transferring control to a PE/COFF image that is loaded using this library.\r | |
1116 | \r | |
2fc59a00 | 1117 | @param ImageContext The pointer to the image context structure that describes the PE/COFF\r |
d071fb19 | 1118 | image that is being loaded.\r |
1119 | \r | |
1120 | @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r | |
1121 | the ImageAddress and ImageSize fields of ImageContext.\r | |
1122 | Extended status information is in the ImageError field of ImageContext.\r | |
1123 | @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r | |
1124 | Extended status information is in the ImageError field of ImageContext.\r | |
1125 | @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r | |
1126 | Extended status information is in the ImageError field of ImageContext.\r | |
1127 | @retval RETURN_INVALID_PARAMETER The image address is invalid.\r | |
1128 | Extended status information is in the ImageError field of ImageContext.\r | |
1129 | \r | |
1130 | **/\r | |
1131 | RETURN_STATUS\r | |
1132 | EFIAPI\r | |
1133 | PeCoffLoaderLoadImage (\r | |
1134 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
1135 | )\r | |
1136 | {\r | |
1137 | RETURN_STATUS Status;\r | |
1138 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
1139 | PE_COFF_LOADER_IMAGE_CONTEXT CheckContext;\r | |
1140 | EFI_IMAGE_SECTION_HEADER *FirstSection;\r | |
1141 | EFI_IMAGE_SECTION_HEADER *Section;\r | |
1142 | UINTN NumberOfSections;\r | |
1143 | UINTN Index;\r | |
1144 | CHAR8 *Base;\r | |
1145 | CHAR8 *End;\r | |
d071fb19 | 1146 | EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;\r |
1147 | EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;\r | |
1148 | UINTN Size;\r | |
1149 | UINT32 TempDebugEntryRva;\r | |
1150 | UINT32 NumberOfRvaAndSizes;\r | |
1151 | UINT16 Magic;\r | |
b4500f6e | 1152 | EFI_IMAGE_RESOURCE_DIRECTORY *ResourceDirectory;\r |
1153 | EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *ResourceDirectoryEntry;\r | |
1154 | EFI_IMAGE_RESOURCE_DIRECTORY_STRING *ResourceDirectoryString;\r | |
1155 | EFI_IMAGE_RESOURCE_DATA_ENTRY *ResourceDataEntry;\r | |
bab427db | 1156 | CHAR16 *String;\r |
713ffa20 | 1157 | UINT32 Offset;\r |
b4500f6e | 1158 | \r |
d071fb19 | 1159 | \r |
1160 | ASSERT (ImageContext != NULL);\r | |
1161 | \r | |
1162 | //\r | |
1163 | // Assume success\r | |
1164 | //\r | |
1165 | ImageContext->ImageError = IMAGE_ERROR_SUCCESS;\r | |
1166 | \r | |
1167 | //\r | |
58380e9c | 1168 | // Copy the provided context information into our local version, get what we\r |
d071fb19 | 1169 | // can from the original image, and then use that to make sure everything\r |
1170 | // is legit.\r | |
1171 | //\r | |
1172 | CopyMem (&CheckContext, ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));\r | |
1173 | \r | |
1174 | Status = PeCoffLoaderGetImageInfo (&CheckContext);\r | |
1175 | if (RETURN_ERROR (Status)) {\r | |
1176 | return Status;\r | |
1177 | }\r | |
1178 | \r | |
1179 | //\r | |
1180 | // Make sure there is enough allocated space for the image being loaded\r | |
1181 | //\r | |
1182 | if (ImageContext->ImageSize < CheckContext.ImageSize) {\r | |
1183 | ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;\r | |
1184 | return RETURN_BUFFER_TOO_SMALL;\r | |
1185 | }\r | |
1186 | if (ImageContext->ImageAddress == 0) {\r | |
1187 | //\r | |
1188 | // Image cannot be loaded into 0 address.\r | |
1189 | //\r | |
1190 | ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;\r | |
1191 | return RETURN_INVALID_PARAMETER;\r | |
1192 | }\r | |
1193 | //\r | |
1194 | // If there's no relocations, then make sure it's not a runtime driver,\r | |
1195 | // and that it's being loaded at the linked address.\r | |
1196 | //\r | |
1197 | if (CheckContext.RelocationsStripped) {\r | |
1198 | //\r | |
1199 | // If the image does not contain relocations and it is a runtime driver\r | |
1200 | // then return an error.\r | |
1201 | //\r | |
1202 | if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {\r | |
1203 | ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r | |
1204 | return RETURN_LOAD_ERROR;\r | |
1205 | }\r | |
1206 | //\r | |
1207 | // If the image does not contain relocations, and the requested load address\r | |
1208 | // is not the linked address, then return an error.\r | |
1209 | //\r | |
1210 | if (CheckContext.ImageAddress != ImageContext->ImageAddress) {\r | |
1211 | ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;\r | |
1212 | return RETURN_INVALID_PARAMETER;\r | |
1213 | }\r | |
1214 | }\r | |
1215 | //\r | |
1216 | // Make sure the allocated space has the proper section alignment\r | |
1217 | //\r | |
1218 | if (!(ImageContext->IsTeImage)) {\r | |
1219 | if ((ImageContext->ImageAddress & (CheckContext.SectionAlignment - 1)) != 0) {\r | |
1220 | ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;\r | |
1221 | return RETURN_INVALID_PARAMETER;\r | |
1222 | }\r | |
1223 | }\r | |
1224 | //\r | |
1225 | // Read the entire PE/COFF or TE header into memory\r | |
1226 | //\r | |
1227 | if (!(ImageContext->IsTeImage)) {\r | |
1228 | Status = ImageContext->ImageRead (\r | |
1229 | ImageContext->Handle,\r | |
1230 | 0,\r | |
1231 | &ImageContext->SizeOfHeaders,\r | |
1232 | (VOID *) (UINTN) ImageContext->ImageAddress\r | |
1233 | );\r | |
1234 | \r | |
1235 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);\r | |
1236 | \r | |
1237 | FirstSection = (EFI_IMAGE_SECTION_HEADER *) (\r | |
1238 | (UINTN)ImageContext->ImageAddress +\r | |
1239 | ImageContext->PeCoffHeaderOffset +\r | |
1240 | sizeof(UINT32) +\r | |
1241 | sizeof(EFI_IMAGE_FILE_HEADER) +\r | |
1242 | Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r | |
1243 | );\r | |
1244 | NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);\r | |
1245 | } else {\r | |
1246 | Status = ImageContext->ImageRead (\r | |
1247 | ImageContext->Handle,\r | |
1248 | 0,\r | |
1249 | &ImageContext->SizeOfHeaders,\r | |
1250 | (void *)(UINTN)ImageContext->ImageAddress\r | |
1251 | );\r | |
1252 | \r | |
1253 | Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\r | |
1254 | \r | |
1255 | FirstSection = (EFI_IMAGE_SECTION_HEADER *) (\r | |
1256 | (UINTN)ImageContext->ImageAddress +\r | |
1257 | sizeof(EFI_TE_IMAGE_HEADER)\r | |
1258 | );\r | |
1259 | NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);\r | |
1260 | \r | |
1261 | }\r | |
1262 | \r | |
1263 | if (RETURN_ERROR (Status)) {\r | |
1264 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
1265 | return RETURN_LOAD_ERROR;\r | |
1266 | }\r | |
1267 | \r | |
1268 | //\r | |
1269 | // Load each section of the image\r | |
1270 | //\r | |
1271 | Section = FirstSection;\r | |
f8ffa6d7 | 1272 | for (Index = 0; Index < NumberOfSections; Index++) {\r |
5cfafa07 | 1273 | //\r |
1274 | // Read the section\r | |
1275 | //\r | |
1276 | Size = (UINTN) Section->Misc.VirtualSize;\r | |
1277 | if ((Size == 0) || (Size > Section->SizeOfRawData)) {\r | |
1278 | Size = (UINTN) Section->SizeOfRawData;\r | |
1279 | }\r | |
1280 | \r | |
d071fb19 | 1281 | //\r |
1282 | // Compute sections address\r | |
1283 | //\r | |
1284 | Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);\r | |
1285 | End = PeCoffLoaderImageAddress (\r | |
1286 | ImageContext,\r | |
1287 | Section->VirtualAddress + Section->Misc.VirtualSize - 1\r | |
1288 | );\r | |
d071fb19 | 1289 | \r |
d071fb19 | 1290 | //\r |
5cfafa07 | 1291 | // If the size of the section is non-zero and the base address or end address resolved to 0, then fail.\r |
d071fb19 | 1292 | //\r |
5cfafa07 | 1293 | if ((Size > 0) && ((Base == NULL) || (End == NULL))) {\r |
d071fb19 | 1294 | ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;\r |
1295 | return RETURN_LOAD_ERROR;\r | |
1296 | }\r | |
1297 | \r | |
9833a9bb LG |
1298 | if (ImageContext->IsTeImage) {\r |
1299 | Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);\r | |
1300 | End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);\r | |
1301 | }\r | |
9833a9bb | 1302 | \r |
2bfb6009 | 1303 | if (Section->SizeOfRawData > 0) {\r |
d071fb19 | 1304 | if (!(ImageContext->IsTeImage)) {\r |
1305 | Status = ImageContext->ImageRead (\r | |
1306 | ImageContext->Handle,\r | |
1307 | Section->PointerToRawData,\r | |
1308 | &Size,\r | |
1309 | Base\r | |
1310 | );\r | |
1311 | } else {\r | |
1312 | Status = ImageContext->ImageRead (\r | |
1313 | ImageContext->Handle,\r | |
1314 | Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize,\r | |
1315 | &Size,\r | |
1316 | Base\r | |
1317 | );\r | |
1318 | }\r | |
1319 | \r | |
1320 | if (RETURN_ERROR (Status)) {\r | |
1321 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
1322 | return Status;\r | |
1323 | }\r | |
1324 | }\r | |
1325 | \r | |
1326 | //\r | |
b4500f6e | 1327 | // If raw size is less then virtual size, zero fill the remaining\r |
d071fb19 | 1328 | //\r |
1329 | \r | |
1330 | if (Size < Section->Misc.VirtualSize) {\r | |
1331 | ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);\r | |
1332 | }\r | |
1333 | \r | |
1334 | //\r | |
1335 | // Next Section\r | |
1336 | //\r | |
1337 | Section += 1;\r | |
1338 | }\r | |
1339 | \r | |
1340 | //\r | |
1341 | // Get image's entry point\r | |
1342 | //\r | |
1343 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
1344 | if (!(ImageContext->IsTeImage)) {\r | |
1345 | //\r | |
1346 | // Sizes of AddressOfEntryPoint are different so we need to do this safely\r | |
1347 | //\r | |
1348 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
1349 | //\r | |
1350 | // Use PE32 offset\r | |
1351 | //\r | |
1352 | ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (\r | |
1353 | ImageContext,\r | |
1354 | (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint\r | |
1355 | );\r | |
1356 | } else {\r | |
1357 | //\r | |
1358 | // Use PE32+ offset\r | |
1359 | //\r | |
1360 | ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (\r | |
1361 | ImageContext,\r | |
1362 | (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint\r | |
1363 | );\r | |
1364 | }\r | |
1365 | } else {\r | |
1366 | ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (\r | |
1367 | (UINTN)ImageContext->ImageAddress +\r | |
1368 | (UINTN)Hdr.Te->AddressOfEntryPoint +\r | |
1369 | (UINTN)sizeof(EFI_TE_IMAGE_HEADER) -\r | |
1370 | (UINTN)Hdr.Te->StrippedSize\r | |
1371 | );\r | |
1372 | }\r | |
1373 | \r | |
1374 | //\r | |
1375 | // Determine the size of the fixup data\r | |
1376 | //\r | |
1377 | // Per the PE/COFF spec, you can't assume that a given data directory\r | |
1378 | // is present in the image. You have to check the NumberOfRvaAndSizes in\r | |
1379 | // the optional header to verify a desired directory entry is there.\r | |
1380 | //\r | |
1381 | if (!(ImageContext->IsTeImage)) {\r | |
1382 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
1383 | //\r | |
1384 | // Use PE32 offset\r | |
1385 | //\r | |
1386 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
1387 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
1388 | } else {\r | |
1389 | //\r | |
1390 | // Use PE32+ offset\r | |
1391 | //\r | |
1392 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
1393 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
1394 | }\r | |
1395 | \r | |
1396 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r | |
1397 | ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);\r | |
1398 | } else {\r | |
1399 | ImageContext->FixupDataSize = 0;\r | |
1400 | }\r | |
1401 | } else {\r | |
1402 | DirectoryEntry = &Hdr.Te->DataDirectory[0];\r | |
1403 | ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);\r | |
1404 | }\r | |
1405 | //\r | |
1406 | // Consumer must allocate a buffer for the relocation fixup log.\r | |
1407 | // Only used for runtime drivers.\r | |
1408 | //\r | |
1409 | ImageContext->FixupData = NULL;\r | |
1410 | \r | |
1411 | //\r | |
58380e9c | 1412 | // Load the Codeview information if present\r |
d071fb19 | 1413 | //\r |
1414 | if (ImageContext->DebugDirectoryEntryRva != 0) {\r | |
1415 | if (!(ImageContext->IsTeImage)) {\r | |
1416 | DebugEntry = PeCoffLoaderImageAddress (\r | |
1417 | ImageContext,\r | |
1418 | ImageContext->DebugDirectoryEntryRva\r | |
1419 | );\r | |
1420 | } else {\r | |
1421 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)(UINTN)(\r | |
1422 | ImageContext->ImageAddress +\r | |
1423 | ImageContext->DebugDirectoryEntryRva +\r | |
1424 | sizeof(EFI_TE_IMAGE_HEADER) -\r | |
1425 | Hdr.Te->StrippedSize\r | |
1426 | );\r | |
1427 | }\r | |
1428 | \r | |
1429 | if (DebugEntry != NULL) {\r | |
1430 | TempDebugEntryRva = DebugEntry->RVA;\r | |
1431 | if (DebugEntry->RVA == 0 && DebugEntry->FileOffset != 0) {\r | |
1432 | Section--;\r | |
1433 | if ((UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) {\r | |
1434 | TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize;\r | |
1435 | } else {\r | |
1436 | TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData;\r | |
1437 | }\r | |
1438 | }\r | |
1439 | \r | |
1440 | if (TempDebugEntryRva != 0) {\r | |
1441 | if (!(ImageContext->IsTeImage)) {\r | |
1442 | ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva);\r | |
1443 | } else {\r | |
1444 | ImageContext->CodeView = (VOID *)(\r | |
1445 | (UINTN)ImageContext->ImageAddress +\r | |
1446 | (UINTN)TempDebugEntryRva +\r | |
1447 | (UINTN)sizeof (EFI_TE_IMAGE_HEADER) -\r | |
1448 | (UINTN) Hdr.Te->StrippedSize\r | |
1449 | );\r | |
1450 | }\r | |
1451 | \r | |
1452 | if (ImageContext->CodeView == NULL) {\r | |
1453 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
1454 | return RETURN_LOAD_ERROR;\r | |
1455 | }\r | |
1456 | \r | |
1457 | if (DebugEntry->RVA == 0) {\r | |
1458 | Size = DebugEntry->SizeOfData;\r | |
1459 | if (!(ImageContext->IsTeImage)) {\r | |
1460 | Status = ImageContext->ImageRead (\r | |
1461 | ImageContext->Handle,\r | |
1462 | DebugEntry->FileOffset,\r | |
1463 | &Size,\r | |
1464 | ImageContext->CodeView\r | |
1465 | );\r | |
1466 | } else {\r | |
1467 | Status = ImageContext->ImageRead (\r | |
1468 | ImageContext->Handle,\r | |
1469 | DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize,\r | |
1470 | &Size,\r | |
1471 | ImageContext->CodeView\r | |
1472 | );\r | |
1473 | //\r | |
1474 | // Should we apply fix up to this field according to the size difference between PE and TE?\r | |
1475 | // Because now we maintain TE header fields unfixed, this field will also remain as they are\r | |
1476 | // in original PE image.\r | |
1477 | //\r | |
1478 | }\r | |
1479 | \r | |
1480 | if (RETURN_ERROR (Status)) {\r | |
1481 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
1482 | return RETURN_LOAD_ERROR;\r | |
1483 | }\r | |
1484 | \r | |
1485 | DebugEntry->RVA = TempDebugEntryRva;\r | |
1486 | }\r | |
1487 | \r | |
1488 | switch (*(UINT32 *) ImageContext->CodeView) {\r | |
1489 | case CODEVIEW_SIGNATURE_NB10:\r | |
36b48bf6 ED |
1490 | if (DebugEntry->SizeOfData < sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY)) {\r |
1491 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1492 | return RETURN_UNSUPPORTED;\r | |
1493 | }\r | |
d071fb19 | 1494 | ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);\r |
1495 | break;\r | |
1496 | \r | |
1497 | case CODEVIEW_SIGNATURE_RSDS:\r | |
36b48bf6 ED |
1498 | if (DebugEntry->SizeOfData < sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY)) {\r |
1499 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1500 | return RETURN_UNSUPPORTED;\r | |
1501 | }\r | |
d071fb19 | 1502 | ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);\r |
1503 | break;\r | |
1504 | \r | |
ebd04fc2 | 1505 | case CODEVIEW_SIGNATURE_MTOC:\r |
36b48bf6 ED |
1506 | if (DebugEntry->SizeOfData < sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY)) {\r |
1507 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1508 | return RETURN_UNSUPPORTED;\r | |
1509 | }\r | |
9101c2e8 | 1510 | ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY);\r |
ebd04fc2 | 1511 | break;\r |
1512 | \r | |
d071fb19 | 1513 | default:\r |
1514 | break;\r | |
1515 | }\r | |
1516 | }\r | |
1517 | }\r | |
1518 | }\r | |
1519 | \r | |
b4500f6e | 1520 | //\r |
1521 | // Get Image's HII resource section\r | |
1522 | //\r | |
1523 | ImageContext->HiiResourceData = 0;\r | |
1524 | if (!(ImageContext->IsTeImage)) {\r | |
1525 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
1526 | //\r | |
1527 | // Use PE32 offset\r | |
1528 | //\r | |
713ffa20 | 1529 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r |
b4500f6e | 1530 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE];\r |
1531 | } else {\r | |
1532 | //\r | |
1533 | // Use PE32+ offset\r | |
1534 | //\r | |
713ffa20 | 1535 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r |
b4500f6e | 1536 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE];\r |
1537 | }\r | |
1538 | \r | |
713ffa20 | 1539 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE && DirectoryEntry->Size != 0) {\r |
b4500f6e | 1540 | Base = PeCoffLoaderImageAddress (ImageContext, DirectoryEntry->VirtualAddress);\r |
50cd68df | 1541 | if (Base != NULL) {\r |
1542 | ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) Base;\r | |
713ffa20 ED |
1543 | Offset = sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * \r |
1544 | (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r | |
1545 | if (Offset > DirectoryEntry->Size) {\r | |
1546 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1547 | return RETURN_UNSUPPORTED;\r | |
1548 | }\r | |
50cd68df | 1549 | ResourceDirectoryEntry = (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *) (ResourceDirectory + 1);\r |
1550 | \r | |
1551 | for (Index = 0; Index < ResourceDirectory->NumberOfNamedEntries; Index++) {\r | |
1552 | if (ResourceDirectoryEntry->u1.s.NameIsString) {\r | |
60893cc3 ED |
1553 | //\r |
1554 | // Check the ResourceDirectoryEntry->u1.s.NameOffset before use it.\r | |
1555 | //\r | |
1556 | if (ResourceDirectoryEntry->u1.s.NameOffset >= DirectoryEntry->Size) {\r | |
713ffa20 ED |
1557 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r |
1558 | return RETURN_UNSUPPORTED;\r | |
60893cc3 | 1559 | }\r |
50cd68df | 1560 | ResourceDirectoryString = (EFI_IMAGE_RESOURCE_DIRECTORY_STRING *) (Base + ResourceDirectoryEntry->u1.s.NameOffset);\r |
bab427db | 1561 | String = &ResourceDirectoryString->String[0];\r |
50cd68df | 1562 | \r |
1563 | if (ResourceDirectoryString->Length == 3 &&\r | |
bab427db | 1564 | String[0] == L'H' &&\r |
1565 | String[1] == L'I' &&\r | |
1566 | String[2] == L'I') {\r | |
b4500f6e | 1567 | //\r |
50cd68df | 1568 | // Resource Type "HII" found\r |
b4500f6e | 1569 | //\r |
b4500f6e | 1570 | if (ResourceDirectoryEntry->u2.s.DataIsDirectory) {\r |
1571 | //\r | |
50cd68df | 1572 | // Move to next level - resource Name\r |
b4500f6e | 1573 | //\r |
713ffa20 ED |
1574 | if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >= DirectoryEntry->Size) {\r |
1575 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1576 | return RETURN_UNSUPPORTED;\r | |
1577 | }\r | |
b4500f6e | 1578 | ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory);\r |
713ffa20 ED |
1579 | Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + \r |
1580 | sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r | |
1581 | if (Offset > DirectoryEntry->Size) {\r | |
1582 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1583 | return RETURN_UNSUPPORTED;\r | |
1584 | }\r | |
b4500f6e | 1585 | ResourceDirectoryEntry = (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *) (ResourceDirectory + 1);\r |
50cd68df | 1586 | \r |
1587 | if (ResourceDirectoryEntry->u2.s.DataIsDirectory) {\r | |
1588 | //\r | |
1589 | // Move to next level - resource Language\r | |
1590 | //\r | |
713ffa20 ED |
1591 | if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >= DirectoryEntry->Size) {\r |
1592 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1593 | return RETURN_UNSUPPORTED;\r | |
1594 | }\r | |
50cd68df | 1595 | ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory);\r |
713ffa20 ED |
1596 | Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + \r |
1597 | sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r | |
1598 | if (Offset > DirectoryEntry->Size) {\r | |
1599 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1600 | return RETURN_UNSUPPORTED;\r | |
1601 | }\r | |
50cd68df | 1602 | ResourceDirectoryEntry = (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *) (ResourceDirectory + 1);\r |
1603 | }\r | |
b4500f6e | 1604 | }\r |
b4500f6e | 1605 | \r |
50cd68df | 1606 | //\r |
1607 | // Now it ought to be resource Data\r | |
1608 | //\r | |
1609 | if (!ResourceDirectoryEntry->u2.s.DataIsDirectory) {\r | |
713ffa20 ED |
1610 | if (ResourceDirectoryEntry->u2.OffsetToData >= DirectoryEntry->Size) {\r |
1611 | ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r | |
1612 | return RETURN_UNSUPPORTED;\r | |
1613 | }\r | |
50cd68df | 1614 | ResourceDataEntry = (EFI_IMAGE_RESOURCE_DATA_ENTRY *) (Base + ResourceDirectoryEntry->u2.OffsetToData);\r |
1615 | ImageContext->HiiResourceData = (PHYSICAL_ADDRESS) (UINTN) PeCoffLoaderImageAddress (ImageContext, ResourceDataEntry->OffsetToData);\r | |
1616 | break;\r | |
1617 | }\r | |
b4500f6e | 1618 | }\r |
1619 | }\r | |
50cd68df | 1620 | ResourceDirectoryEntry++;\r |
b4500f6e | 1621 | }\r |
b4500f6e | 1622 | }\r |
1623 | }\r | |
1624 | }\r | |
1625 | \r | |
d071fb19 | 1626 | return Status;\r |
1627 | }\r | |
1628 | \r | |
1629 | \r | |
1630 | /**\r | |
1631 | Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r | |
efb23117 | 1632 | runtime. \r |
1633 | \r | |
9833a9bb LG |
1634 | This function reapplies relocation fixups to the PE/COFF image specified by ImageBase \r |
1635 | and ImageSize so the image will execute correctly when the PE/COFF image is mapped \r | |
1636 | to the address specified by VirtualImageBase. RelocationData must be identical \r | |
1637 | to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure \r | |
1638 | after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().\r | |
d071fb19 | 1639 | \r |
8d579453 | 1640 | Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r |
1641 | cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r | |
1642 | prior to transferring control to a PE/COFF image that is loaded using this library.\r | |
1643 | \r | |
2fc59a00 | 1644 | @param ImageBase The base address of a PE/COFF image that has been loaded \r |
efb23117 | 1645 | and relocated into system memory.\r |
1646 | @param VirtImageBase The request virtual address that the PE/COFF image is to\r | |
1647 | be fixed up for.\r | |
1648 | @param ImageSize The size, in bytes, of the PE/COFF image.\r | |
1649 | @param RelocationData A pointer to the relocation data that was collected when the PE/COFF \r | |
1650 | image was relocated using PeCoffLoaderRelocateImage().\r | |
1651 | \r | |
d071fb19 | 1652 | **/\r |
1653 | VOID\r | |
1654 | EFIAPI\r | |
1655 | PeCoffLoaderRelocateImageForRuntime (\r | |
1656 | IN PHYSICAL_ADDRESS ImageBase,\r | |
1657 | IN PHYSICAL_ADDRESS VirtImageBase,\r | |
1658 | IN UINTN ImageSize,\r | |
1659 | IN VOID *RelocationData\r | |
1660 | )\r | |
1661 | {\r | |
1662 | CHAR8 *OldBase;\r | |
1663 | CHAR8 *NewBase;\r | |
1664 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
1665 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
1666 | UINT32 NumberOfRvaAndSizes;\r | |
1667 | EFI_IMAGE_DATA_DIRECTORY *DataDirectory;\r | |
1668 | EFI_IMAGE_DATA_DIRECTORY *RelocDir;\r | |
1669 | EFI_IMAGE_BASE_RELOCATION *RelocBase;\r | |
1670 | EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;\r | |
1671 | UINT16 *Reloc;\r | |
1672 | UINT16 *RelocEnd;\r | |
1673 | CHAR8 *Fixup;\r | |
1674 | CHAR8 *FixupBase;\r | |
1fa524e9 | 1675 | UINT16 *Fixup16;\r |
1676 | UINT32 *Fixup32;\r | |
1677 | UINT64 *Fixup64;\r | |
d071fb19 | 1678 | CHAR8 *FixupData;\r |
1679 | UINTN Adjust;\r | |
1680 | RETURN_STATUS Status;\r | |
1681 | UINT16 Magic;\r | |
1682 | \r | |
1683 | OldBase = (CHAR8 *)((UINTN)ImageBase);\r | |
1684 | NewBase = (CHAR8 *)((UINTN)VirtImageBase);\r | |
1685 | Adjust = (UINTN) NewBase - (UINTN) OldBase;\r | |
1686 | \r | |
1687 | //\r | |
1688 | // Find the image's relocate dir info\r | |
1689 | //\r | |
1690 | DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase;\r | |
1691 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
1692 | //\r | |
1693 | // Valid DOS header so get address of PE header\r | |
1694 | //\r | |
1695 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew);\r | |
1696 | } else {\r | |
1697 | //\r | |
1698 | // No Dos header so assume image starts with PE header.\r | |
1699 | //\r | |
1700 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase;\r | |
1701 | }\r | |
1702 | \r | |
1703 | if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r | |
1704 | //\r | |
1705 | // Not a valid PE image so Exit\r | |
1706 | //\r | |
1707 | return ;\r | |
1708 | }\r | |
1709 | \r | |
1710 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
1711 | \r | |
1712 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
1713 | //\r | |
1714 | // Use PE32 offset\r | |
1715 | //\r | |
1716 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
1717 | DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);\r | |
1718 | } else {\r | |
1719 | //\r | |
1720 | // Use PE32+ offset\r | |
1721 | //\r | |
1722 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
1723 | DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);\r | |
1724 | }\r | |
1725 | \r | |
1726 | //\r | |
1727 | // Find the relocation block\r | |
1728 | //\r | |
1729 | // Per the PE/COFF spec, you can't assume that a given data directory\r | |
1730 | // is present in the image. You have to check the NumberOfRvaAndSizes in\r | |
1731 | // the optional header to verify a desired directory entry is there.\r | |
1732 | //\r | |
1733 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r | |
1734 | RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;\r | |
1735 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);\r | |
1736 | RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);\r | |
1737 | } else {\r | |
1738 | //\r | |
2bfb6009 | 1739 | // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.\r |
d071fb19 | 1740 | //\r |
1741 | ASSERT (FALSE);\r | |
1742 | return ;\r | |
1743 | }\r | |
2bfb6009 LG |
1744 | \r |
1745 | //\r | |
1746 | // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.\r | |
1747 | //\r | |
d071fb19 | 1748 | ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);\r |
1749 | \r | |
1750 | //\r | |
1751 | // Run the whole relocation block. And re-fixup data that has not been\r | |
1752 | // modified. The FixupData is used to see if the image has been modified\r | |
1753 | // since it was relocated. This is so data sections that have been updated\r | |
1754 | // by code will not be fixed up, since that would set them back to\r | |
1755 | // defaults.\r | |
1756 | //\r | |
1757 | FixupData = RelocationData;\r | |
1758 | while (RelocBase < RelocBaseEnd) {\r | |
60893cc3 ED |
1759 | //\r |
1760 | // Add check for RelocBase->SizeOfBlock field.\r | |
1761 | //\r | |
1762 | if ((RelocBase->SizeOfBlock == 0) || (RelocBase->SizeOfBlock > RelocDir->Size)) {\r | |
1763 | //\r | |
1764 | // Data invalid, cannot continue to relocate the image, just return.\r | |
1765 | //\r | |
1766 | return;\r | |
1767 | }\r | |
d071fb19 | 1768 | \r |
1769 | Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r | |
1770 | RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);\r | |
1771 | FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;\r | |
1772 | \r | |
1773 | //\r | |
1774 | // Run this relocation record\r | |
1775 | //\r | |
1776 | while (Reloc < RelocEnd) {\r | |
1777 | \r | |
1778 | Fixup = FixupBase + (*Reloc & 0xFFF);\r | |
1779 | switch ((*Reloc) >> 12) {\r | |
1780 | \r | |
1781 | case EFI_IMAGE_REL_BASED_ABSOLUTE:\r | |
1782 | break;\r | |
1783 | \r | |
1784 | case EFI_IMAGE_REL_BASED_HIGH:\r | |
1fa524e9 | 1785 | Fixup16 = (UINT16 *) Fixup;\r |
1786 | if (*(UINT16 *) FixupData == *Fixup16) {\r | |
1787 | *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r | |
d071fb19 | 1788 | }\r |
1789 | \r | |
1790 | FixupData = FixupData + sizeof (UINT16);\r | |
1791 | break;\r | |
1792 | \r | |
1793 | case EFI_IMAGE_REL_BASED_LOW:\r | |
1fa524e9 | 1794 | Fixup16 = (UINT16 *) Fixup;\r |
1795 | if (*(UINT16 *) FixupData == *Fixup16) {\r | |
1796 | *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) Adjust & 0xffff));\r | |
d071fb19 | 1797 | }\r |
1798 | \r | |
1799 | FixupData = FixupData + sizeof (UINT16);\r | |
1800 | break;\r | |
1801 | \r | |
1802 | case EFI_IMAGE_REL_BASED_HIGHLOW:\r | |
1fa524e9 | 1803 | Fixup32 = (UINT32 *) Fixup;\r |
d071fb19 | 1804 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r |
1fa524e9 | 1805 | if (*(UINT32 *) FixupData == *Fixup32) {\r |
1806 | *Fixup32 = *Fixup32 + (UINT32) Adjust;\r | |
d071fb19 | 1807 | }\r |
1808 | \r | |
1809 | FixupData = FixupData + sizeof (UINT32);\r | |
1810 | break;\r | |
1811 | \r | |
1812 | case EFI_IMAGE_REL_BASED_DIR64:\r | |
1fa524e9 | 1813 | Fixup64 = (UINT64 *)Fixup;\r |
d071fb19 | 1814 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));\r |
1fa524e9 | 1815 | if (*(UINT64 *) FixupData == *Fixup64) {\r |
1816 | *Fixup64 = *Fixup64 + (UINT64)Adjust;\r | |
d071fb19 | 1817 | }\r |
1818 | \r | |
1819 | FixupData = FixupData + sizeof (UINT64);\r | |
1820 | break;\r | |
1821 | \r | |
1822 | case EFI_IMAGE_REL_BASED_HIGHADJ:\r | |
1823 | //\r | |
2bfb6009 | 1824 | // Not valid Relocation type for UEFI image, ASSERT\r |
d071fb19 | 1825 | //\r |
1826 | ASSERT (FALSE);\r | |
1827 | break;\r | |
1828 | \r | |
1829 | default:\r | |
1830 | //\r | |
1831 | // Only Itanium requires ConvertPeImage_Ex\r | |
1832 | //\r | |
1833 | Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r | |
1834 | if (RETURN_ERROR (Status)) {\r | |
1835 | return ;\r | |
1836 | }\r | |
1837 | }\r | |
1838 | //\r | |
1839 | // Next relocation record\r | |
1840 | //\r | |
1841 | Reloc += 1;\r | |
1842 | }\r | |
1843 | //\r | |
1844 | // next reloc block\r | |
1845 | //\r | |
1846 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r | |
1847 | }\r | |
1848 | }\r | |
1849 | \r | |
1850 | \r | |
1851 | /**\r | |
657073df | 1852 | Reads contents of a PE/COFF image from a buffer in system memory.\r |
1853 | \r | |
1854 | This is the default implementation of a PE_COFF_LOADER_READ_FILE function \r | |
1855 | that assumes FileHandle pointer to the beginning of a PE/COFF image. \r | |
1856 | This function reads contents of the PE/COFF image that starts at the system memory \r | |
1857 | address specified by FileHandle. The read operation copies ReadSize bytes from the \r | |
1858 | PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. \r | |
1859 | The size of the buffer actually read is returned in ReadSize.\r | |
1860 | \r | |
158e788f ED |
1861 | The caller must make sure the FileOffset and ReadSize within the file scope.\r |
1862 | \r | |
657073df | 1863 | If FileHandle is NULL, then ASSERT().\r |
1864 | If ReadSize is NULL, then ASSERT().\r | |
1865 | If Buffer is NULL, then ASSERT().\r | |
d071fb19 | 1866 | \r |
2fc59a00 | 1867 | @param FileHandle The pointer to base of the input stream\r |
657073df | 1868 | @param FileOffset Offset into the PE/COFF image to begin the read operation.\r |
1869 | @param ReadSize On input, the size in bytes of the requested read operation. \r | |
1870 | On output, the number of bytes actually read.\r | |
1871 | @param Buffer Output buffer that contains the data read from the PE/COFF image.\r | |
d071fb19 | 1872 | \r |
657073df | 1873 | @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r |
d071fb19 | 1874 | the buffer.\r |
1875 | **/\r | |
1876 | RETURN_STATUS\r | |
1877 | EFIAPI\r | |
1878 | PeCoffLoaderImageReadFromMemory (\r | |
1879 | IN VOID *FileHandle,\r | |
1880 | IN UINTN FileOffset,\r | |
1881 | IN OUT UINTN *ReadSize,\r | |
1882 | OUT VOID *Buffer\r | |
1883 | )\r | |
1884 | {\r | |
657073df | 1885 | ASSERT (ReadSize != NULL);\r |
1886 | ASSERT (FileHandle != NULL);\r | |
1887 | ASSERT (Buffer != NULL);\r | |
1888 | \r | |
d071fb19 | 1889 | CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);\r |
1890 | return RETURN_SUCCESS;\r | |
1891 | }\r | |
1892 | \r | |
3d7b0992 LG |
1893 | /**\r |
1894 | Unloads a loaded PE/COFF image from memory and releases its taken resource.\r | |
efb23117 | 1895 | Releases any environment specific resources that were allocated when the image \r |
1896 | specified by ImageContext was loaded using PeCoffLoaderLoadImage(). \r | |
1897 | \r | |
3d7b0992 LG |
1898 | For NT32 emulator, the PE/COFF image loaded by system needs to release.\r |
1899 | For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r | |
1900 | this function can simply return RETURN_SUCCESS.\r | |
efb23117 | 1901 | \r |
1902 | If ImageContext is NULL, then ASSERT().\r | |
1903 | \r | |
2fc59a00 | 1904 | @param ImageContext The pointer to the image context structure that describes the PE/COFF\r |
3d7b0992 LG |
1905 | image to be unloaded.\r |
1906 | \r | |
1907 | @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r | |
1908 | **/\r | |
1909 | RETURN_STATUS\r | |
1910 | EFIAPI\r | |
1911 | PeCoffLoaderUnloadImage (\r | |
0465a73e | 1912 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r |
3d7b0992 LG |
1913 | )\r |
1914 | {\r | |
b4500f6e | 1915 | //\r |
27b2d249 | 1916 | // Applies additional environment specific actions to unload a \r |
1917 | // PE/COFF image if needed\r | |
b4500f6e | 1918 | //\r |
27b2d249 | 1919 | PeCoffLoaderUnloadImageExtraAction (ImageContext);\r |
3d7b0992 LG |
1920 | return RETURN_SUCCESS;\r |
1921 | }\r |