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