3 Copyright (c) 1999 - 2005 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
19 This contains all code necessary to build the PeiRebase.exe utility.
20 This utility relies heavily on the PeiRebase DLL. Definitions for both
21 can be found in the PEI Rebase Utility Specification, review draft.
29 #include <Common/UefiBaseTypes.h>
30 #include <Common/FirmwareVolumeImageFormat.h>
31 #include <Common/FirmwareFileSystem.h>
32 #include <Library/PeCoffLib.h>
34 #include "CommonLib.h"
37 #include "EfiUtilityMsgs.h"
38 #include "PeiRebaseExe.h"
44 OUT BOOLEAN
*ErasePolarity
56 This utility relocates PEI XIP PE32s in a FV.
60 argc - Number of command line arguments
62 BaseAddress The base address to use for rebasing the FV. The correct
63 format is a hex number preceded by 0x.
64 InputFileName The name of the input FV file.
65 OutputFileName The name of the output FV file.
67 Arguments come in pair in any order.
74 0 No error conditions detected.
75 1 One or more of the input parameters is invalid.
76 2 A resource required by the utility was unavailable.
77 Most commonly this will be memory allocation or file creation.
78 3 PeiRebase.dll could not be loaded.
79 4 Error executing the PEI rebase.
84 CHAR8 InputFileName
[_MAX_PATH
];
85 CHAR8 OutputFileName
[_MAX_PATH
];
86 EFI_PHYSICAL_ADDRESS BaseAddress
;
87 BOOLEAN BaseAddressSet
;
94 EFI_FIRMWARE_VOLUME_HEADER
*FvImage
;
96 EFI_FFS_FILE_HEADER
*CurrentFile
;
97 BOOLEAN ErasePolarity
;
98 EFI_PHYSICAL_ADDRESS CurrentFileBaseAddress
;
100 ErasePolarity
= FALSE
;
102 // Set utility name for error/warning reporting purposes.
104 SetUtilityName (UTILITY_NAME
);
106 // Verify the correct number of arguments
108 if (argc
!= MAX_ARGS
) {
113 // Initialize variables
115 InputFileName
[0] = 0;
116 OutputFileName
[0] = 0;
118 BaseAddressSet
= FALSE
;
121 ErasePolarity
= FALSE
;
126 // Parse the command line arguments
128 for (Index
= 1; Index
< MAX_ARGS
; Index
+= 2) {
130 // Make sure argument pair begin with - or /
132 if (argv
[Index
][0] != '-' && argv
[Index
][0] != '/') {
134 Error (NULL
, 0, 0, argv
[Index
], "unrecognized option");
138 // Make sure argument specifier is only one letter
140 if (argv
[Index
][2] != 0) {
142 Error (NULL
, 0, 0, argv
[Index
], "unrecognized option");
146 // Determine argument to read
148 switch (argv
[Index
][1]) {
151 if (strlen (InputFileName
) == 0) {
152 strcpy (InputFileName
, argv
[Index
+ 1]);
155 Error (NULL
, 0, 0, argv
[Index
+ 1], "only one -i InputFileName may be specified");
162 if (strlen (OutputFileName
) == 0) {
163 strcpy (OutputFileName
, argv
[Index
+ 1]);
166 Error (NULL
, 0, 0, argv
[Index
+ 1], "only one -o OutputFileName may be specified");
173 if (!BaseAddressSet
) {
174 Status
= AsciiStringToUint64 (argv
[Index
+ 1], FALSE
, &BaseAddress
);
175 if (EFI_ERROR (Status
)) {
177 Error (NULL
, 0, 0, argv
[Index
+ 1], "invalid hex digit given for the base address");
181 BaseAddressSet
= TRUE
;
184 Error (NULL
, 0, 0, argv
[Index
+ 1], "-b BaseAddress may only be specified once");
191 Error (NULL
, 0, 0, argv
[Index
], "unrecognized argument");
197 // Open the file containing the FV
199 InputFile
= fopen (InputFileName
, "rb");
200 if (InputFile
== NULL
) {
201 Error (NULL
, 0, 0, InputFileName
, "could not open input file for reading");
205 // Determine size of FV
207 Status
= ReadHeader (InputFile
, &FvSize
, &ErasePolarity
);
208 if (EFI_ERROR (Status
)) {
209 Error (NULL
, 0, 0, "could not parse the FV header", NULL
);
213 // Allocate a buffer for the FV image
215 FvImage
= malloc (FvSize
);
216 if (FvImage
== NULL
) {
217 Error (NULL
, 0, 0, "application error", "memory allocation failed");
221 // Read the entire FV to the buffer
223 BytesRead
= fread (FvImage
, 1, FvSize
, InputFile
);
226 if ((unsigned int) BytesRead
!= FvSize
) {
227 Error (NULL
, 0, 0, InputFileName
, "failed to read from file");
231 // Prepare to walk the FV image
233 InitializeFvLib (FvImage
, FvSize
);
235 // Get the first file
237 Status
= GetNextFile (NULL
, &CurrentFile
);
238 if (EFI_ERROR (Status
)) {
239 Error (NULL
, 0, 0, "cannot find the first file in the FV image", NULL
);
243 // Check if each file should be rebased
245 while (CurrentFile
!= NULL
) {
249 CurrentFileBaseAddress
= BaseAddress
+ ((UINTN
) CurrentFile
- (UINTN
) FvImage
);
250 Status
= FfsRebase (CurrentFile
, CurrentFileBaseAddress
);
252 if (EFI_ERROR (Status
)) {
255 case EFI_INVALID_PARAMETER
:
256 Error (NULL
, 0, 0, "invalid parameter passed to FfsRebase", NULL
);
260 Error (NULL
, 0, 0, "error detected while rebasing -- aborted", NULL
);
263 case EFI_OUT_OF_RESOURCES
:
264 Error (NULL
, 0, 0, "FfsRebase could not allocate required resources", NULL
);
268 Error (NULL
, 0, 0, "FfsRebase could not locate a PE32 section", NULL
);
272 Error (NULL
, 0, 0, "FfsRebase returned unknown status", "status=0x%08X", Status
);
281 Status
= GetNextFile (CurrentFile
, &CurrentFile
);
282 if (EFI_ERROR (Status
)) {
283 Error (NULL
, 0, 0, "cannot find the next file in the FV image", NULL
);
288 // Open the output file
290 OutputFile
= fopen (OutputFileName
, "wb");
291 if (OutputFile
== NULL
) {
292 Error (NULL
, 0, 0, OutputFileName
, "failed to open output file");
296 if (fwrite (FvImage
, 1, FvSize
, OutputFile
) != FvSize
) {
297 Error (NULL
, 0, 0, "failed to write to output file", 0);
302 if (InputFile
!= NULL
) {
306 // If we created an output file, and there was an error, remove it so
307 // subsequent builds will rebuild it.
309 if (OutputFile
!= NULL
) {
310 if (GetUtilityStatus () == STATUS_ERROR
) {
311 remove (OutputFileName
);
317 if (FvImage
!= NULL
) {
321 return GetUtilityStatus ();
328 OUT BOOLEAN
*ErasePolarity
334 This function determines the size of the FV and the erase polarity. The
335 erase polarity is the FALSE value for file state.
339 InputFile The file that contains the FV image.
340 FvSize The size of the FV.
341 ErasePolarity The FV erase polarity.
345 EFI_SUCCESS Function completed successfully.
346 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.
347 EFI_ABORTED The function encountered an error.
351 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader
;
352 EFI_FV_BLOCK_MAP_ENTRY BlockMap
;
360 // Check input parameters
362 if ((InputFile
== NULL
) || (FvSize
== NULL
) || (ErasePolarity
== NULL
)) {
363 Error (NULL
, 0, 0, "ReadHeader()", "invalid input parameter");
364 return EFI_INVALID_PARAMETER
;
369 fread (&VolumeHeader
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, InputFile
);
370 BytesRead
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
371 Signature
[0] = VolumeHeader
.Signature
;
375 // Get erase polarity
377 if (VolumeHeader
.Attributes
& EFI_FVB_ERASE_POLARITY
) {
378 *ErasePolarity
= TRUE
;
382 fread (&BlockMap
, sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, InputFile
);
383 BytesRead
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
385 if (BlockMap
.NumBlocks
!= 0) {
386 Size
+= BlockMap
.NumBlocks
* BlockMap
.BlockLength
;
389 } while (!(BlockMap
.NumBlocks
== 0 && BlockMap
.BlockLength
== 0));
391 if (VolumeHeader
.FvLength
!= Size
) {
392 Error (NULL
, 0, 0, "volume size not consistant with block maps", NULL
);
411 Displays the standard utility information to SDTOUT
424 "%s, PEI Rebase Utility. Version %i.%i, %s.\n\n",
426 UTILITY_MAJOR_VERSION
,
427 UTILITY_MINOR_VERSION
,
440 Displays the utility usage syntax to STDOUT
453 "Usage: %s -I InputFileName -O OutputFileName -B BaseAddress\n",
456 printf (" Where:\n");
457 printf (" InputFileName is the name of the EFI FV file to rebase.\n");
458 printf (" OutputFileName is the desired output file name.\n");
459 printf (" BaseAddress is the FV base address to rebase agains.\n");
460 printf (" Argument pair may be in any order.\n\n");
465 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
466 IN EFI_PHYSICAL_ADDRESS BaseAddress
472 This function determines if a file is XIP and should be rebased. It will
473 rebase any PE32 sections found in the file using the base address.
477 FfsFile A pointer to Ffs file image.
478 BaseAddress The base address to use for rebasing the file image.
482 EFI_SUCCESS The image was properly rebased.
483 EFI_INVALID_PARAMETER An input parameter is invalid.
484 EFI_ABORTED An error occurred while rebasing the input file image.
485 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
486 EFI_NOT_FOUND No compressed sections could be found.
491 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
492 UINTN MemoryImagePointer
;
493 UINTN MemoryImagePointerAligned
;
494 EFI_PHYSICAL_ADDRESS ImageAddress
;
496 EFI_PHYSICAL_ADDRESS EntryPoint
;
497 UINT32 Pe32ImageSize
;
498 UINT32 NewPe32BaseAddress
;
500 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
501 EFI_FFS_FILE_STATE SavedState
;
502 EFI_IMAGE_NT_HEADERS
*PeHdr
;
503 UINT32
*PeHdrSizeOfImage
;
504 UINT32
*PeHdrChecksum
;
506 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
508 EFI_IMAGE_DOS_HEADER
*DosHeader
;
509 UINT8 FileGuidString
[80];
511 EFI_FFS_FILE_TAIL TailValue
;
514 // Verify input parameters
516 if (FfsFile
== NULL
) {
517 return EFI_INVALID_PARAMETER
;
520 // Convert the GUID to a string so we can at least report which file
521 // if we find an error.
523 PrintGuidToBuffer (&FfsFile
->Name
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
524 if (FfsFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
525 TailSize
= sizeof (EFI_FFS_FILE_TAIL
);
530 // Do some cursory checks on the FFS file contents
532 Status
= VerifyFfsFile (FfsFile
);
533 if (EFI_ERROR (Status
)) {
534 Error (NULL
, 0, 0, "file does not appear to be a valid FFS file, cannot be rebased", FileGuidString
);
535 return EFI_INVALID_PARAMETER
;
538 // Check if XIP file type. If not XIP, don't rebase.
540 if (FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
541 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
542 FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
543 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
548 // Rebase each PE32 section
550 Status
= EFI_SUCCESS
;
552 for (Index
= 1;; Index
++) {
553 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
554 if (EFI_ERROR (Status
)) {
561 // Calculate the PE32 base address, the FFS file base plus the offset of the PE32 section
563 NewPe32BaseAddress
= ((UINT32
) BaseAddress
) + ((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
) - (UINTN
) FfsFile
);
566 // Initialize context
568 memset (&ImageContext
, 0, sizeof (ImageContext
));
569 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
));
570 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
572 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
574 if (EFI_ERROR (Status
)) {
575 Error (NULL
, 0, 0, "GetImageInfo() call failed on rebase", FileGuidString
);
579 // Allocate a buffer for the image to be loaded into.
581 Pe32ImageSize
= GetLength (CurrentPe32Section
.Pe32Section
->CommonHeader
.Size
) - sizeof (EFI_PE32_SECTION
);
582 MemoryImagePointer
= (UINTN
) (malloc (Pe32ImageSize
+ 0x1000));
583 if (MemoryImagePointer
== 0) {
584 Error (NULL
, 0, 0, "memory allocation failure", NULL
);
585 return EFI_OUT_OF_RESOURCES
;
587 memset ((void *) MemoryImagePointer
, 0, Pe32ImageSize
+ 0x1000);
588 MemoryImagePointerAligned
= (MemoryImagePointer
+ 0x0FFF) & (-1 << 12);
591 ImageContext
.ImageAddress
= MemoryImagePointerAligned
;
593 Status
= PeCoffLoaderLoadImage (&ImageContext
);
594 if (EFI_ERROR (Status
)) {
595 Error (NULL
, 0, 0, "LoadImage() call failed on rebase", FileGuidString
);
596 free ((VOID
*) MemoryImagePointer
);
600 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
601 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
602 if (EFI_ERROR (Status
)) {
603 Error (NULL
, 0, 0, "RelocateImage() call failed on rebase", FileGuidString
);
604 free ((VOID
*) MemoryImagePointer
);
608 ImageAddress
= ImageContext
.ImageAddress
;
609 ImageSize
= ImageContext
.ImageSize
;
610 EntryPoint
= ImageContext
.EntryPoint
;
612 if (ImageSize
> Pe32ImageSize
) {
617 "rebased image is larger than original PE32 image",
618 "0x%X > 0x%X, file %s",
623 free ((VOID
*) MemoryImagePointer
);
627 // Since we may have updated the Codeview RVA, we need to insure the PE
628 // header indicates the image is large enough to contain the Codeview data
629 // so it will be loaded properly later if the PEIM is reloaded into memory...
631 PeHdr
= (VOID
*) ((UINTN
) ImageAddress
+ ImageContext
.PeCoffHeaderOffset
);
632 if (PeHdr
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA32
) {
633 PeHdrSizeOfImage
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER32
*) &PeHdr
->OptionalHeader
).SizeOfImage
);
634 PeHdrChecksum
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER32
*) &PeHdr
->OptionalHeader
).CheckSum
);
635 } else if (PeHdr
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
) {
636 PeHdrSizeOfImage
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).SizeOfImage
);
637 PeHdrChecksum
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).CheckSum
);
638 } else if (PeHdr
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_X64
) {
639 PeHdrSizeOfImage
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).SizeOfImage
);
640 PeHdrChecksum
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).CheckSum
);
646 "unknown machine type in PE32 image",
647 "machine type=0x%X, file=%s",
648 (UINT32
) PeHdr
->FileHeader
.Machine
,
651 free ((VOID
*) MemoryImagePointer
);
655 if (*PeHdrSizeOfImage
!= ImageContext
.ImageSize
) {
656 *PeHdrSizeOfImage
= (UINT32
) ImageContext
.ImageSize
;
657 if (*PeHdrChecksum
) {
662 memcpy (CurrentPe32Section
.Pe32Section
+ 1, (VOID
*) MemoryImagePointerAligned
, (UINT32
) ImageSize
);
664 free ((VOID
*) MemoryImagePointer
);
667 // Now update file checksum
669 if (FfsFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
670 TailSize
= sizeof (EFI_FFS_FILE_TAIL
);
675 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
676 SavedState
= FfsFile
->State
;
677 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
679 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
680 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
682 GetLength (FfsFile
->Size
) - TailSize
685 FfsFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
688 FfsFile
->State
= SavedState
;
691 // Update tail if present
693 if (FfsFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
694 TailValue
= (EFI_FFS_FILE_TAIL
) (~(FfsFile
->IntegrityCheck
.TailReference
));
695 *(EFI_FFS_FILE_TAIL
*) (((UINTN
) FfsFile
+ GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_TAIL
))) = TailValue
;
699 // Now process TE sections
701 for (Index
= 1;; Index
++) {
702 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
703 if (EFI_ERROR (Status
)) {
710 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
713 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
));
715 NewPe32BaseAddress
= ((UINT32
) BaseAddress
) +
717 (UINTN
) CurrentPe32Section
.Pe32Section
+
718 sizeof (EFI_COMMON_SECTION_HEADER
) +
719 sizeof (EFI_TE_IMAGE_HEADER
) -
720 TEImageHeader
->StrippedSize
-
725 // Allocate a buffer to unshrink the image into.
727 Pe32ImageSize
= GetLength (CurrentPe32Section
.Pe32Section
->CommonHeader
.Size
) - sizeof (EFI_PE32_SECTION
) -
728 sizeof (EFI_TE_IMAGE_HEADER
);
729 Pe32ImageSize
+= TEImageHeader
->StrippedSize
;
730 TEBuffer
= (UINT8
*) malloc (Pe32ImageSize
);
731 if (TEBuffer
== NULL
) {
732 Error (NULL
, 0, 0, "failed to allocate memory", NULL
);
733 return EFI_OUT_OF_RESOURCES
;
736 // Expand the image into our buffer and fill in critical fields in the DOS header
737 // Fill in fields required by the loader.
738 // At offset 0x3C is the offset to the PE signature. We'll put it immediately following the offset value
741 memset (TEBuffer
, 0, Pe32ImageSize
);
742 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) TEBuffer
;
743 DosHeader
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
744 *(UINT32
*) (TEBuffer
+ 0x3C) = 0x40;
745 PeHdr
= (EFI_IMAGE_NT_HEADERS
*) (TEBuffer
+ 0x40);
746 PeHdr
->Signature
= EFI_IMAGE_NT_SIGNATURE
;
747 PeHdr
->FileHeader
.Machine
= TEImageHeader
->Machine
;
748 PeHdr
->FileHeader
.NumberOfSections
= TEImageHeader
->NumberOfSections
;
751 // Say the size of the optional header is the total we stripped off less the size of a PE file header and PE signature and
752 // the 0x40 bytes for our DOS header.
754 PeHdr
->FileHeader
.SizeOfOptionalHeader
= (UINT16
) (TEImageHeader
->StrippedSize
- 0x40 - sizeof (UINT32
) - sizeof (EFI_IMAGE_FILE_HEADER
));
755 PeHdr
->OptionalHeader
.ImageBase
= (UINTN
) (TEImageHeader
->ImageBase
- TEImageHeader
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
756 PeHdr
->OptionalHeader
.SizeOfImage
= Pe32ImageSize
;
757 PeHdr
->OptionalHeader
.Subsystem
= TEImageHeader
->Subsystem
;
758 PeHdr
->OptionalHeader
.SizeOfImage
= Pe32ImageSize
;
759 PeHdr
->OptionalHeader
.SizeOfHeaders
= TEImageHeader
->StrippedSize
+ TEImageHeader
->NumberOfSections
*
760 sizeof (EFI_IMAGE_SECTION_HEADER
) - 12;
763 // Set NumberOfRvaAndSizes in the optional header to what we had available in the original image
765 if ((TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
!= 0) ||
766 (TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
!= 0)
768 PeHdr
->OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
+ 1;
769 PeHdr
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
;
770 PeHdr
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
773 if ((TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) ||
774 (TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
!= 0)
776 PeHdr
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
777 PeHdr
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
778 if (PeHdr
->OptionalHeader
.NumberOfRvaAndSizes
< EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
+ 1) {
779 PeHdr
->OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
+ 1;
783 // NOTE: These values are defaults, and should be verified to be correct in the GenTE utility
785 PeHdr
->OptionalHeader
.SectionAlignment
= 0x10;
788 // Copy the rest of the image to its original offset
791 TEBuffer
+ TEImageHeader
->StrippedSize
,
792 (UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) + sizeof (EFI_TE_IMAGE_HEADER
),
793 GetLength (CurrentPe32Section
.Pe32Section
->CommonHeader
.Size
) - sizeof (EFI_PE32_SECTION
) -
794 sizeof (EFI_TE_IMAGE_HEADER
)
798 // Initialize context
800 memset (&ImageContext
, 0, sizeof (ImageContext
));
801 ImageContext
.Handle
= (VOID
*) TEBuffer
;
802 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
804 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
806 if (EFI_ERROR (Status
)) {
807 Error (NULL
, 0, 0, "GetImageInfo() call failed on rebase of TE image", FileGuidString
);
812 // Allocate a buffer for the image to be loaded into.
814 MemoryImagePointer
= (UINTN
) (malloc (Pe32ImageSize
+ 0x1000));
815 if (MemoryImagePointer
== 0) {
816 Error (NULL
, 0, 0, "memory allocation error on rebase of TE image", FileGuidString
);
818 return EFI_OUT_OF_RESOURCES
;
820 memset ((void *) MemoryImagePointer
, 0, Pe32ImageSize
+ 0x1000);
821 MemoryImagePointerAligned
= (MemoryImagePointer
+ 0x0FFF) & (-1 << 12);
824 ImageContext
.ImageAddress
= MemoryImagePointerAligned
;
825 Status
= PeCoffLoaderLoadImage (&ImageContext
);
826 if (EFI_ERROR (Status
)) {
827 Error (NULL
, 0, 0, "LoadImage() call failed on rebase of TE image", FileGuidString
);
829 free ((VOID
*) MemoryImagePointer
);
833 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
834 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
835 if (EFI_ERROR (Status
)) {
836 Error (NULL
, 0, 0, "RelocateImage() call failed on rebase of TE image", FileGuidString
);
837 free ((VOID
*) MemoryImagePointer
);
842 ImageAddress
= ImageContext
.ImageAddress
;
843 ImageSize
= ImageContext
.ImageSize
;
844 EntryPoint
= ImageContext
.EntryPoint
;
847 // Since we may have updated the Codeview RVA, we need to insure the PE
848 // header indicates the image is large enough to contain the Codeview data
849 // so it will be loaded properly later if the PEIM is reloaded into memory...
851 PeHdr
= (VOID
*) ((UINTN
) ImageAddress
+ ImageContext
.PeCoffHeaderOffset
);
852 if (PeHdr
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA32
) {
853 PeHdrSizeOfImage
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER32
*) &PeHdr
->OptionalHeader
).SizeOfImage
);
854 PeHdrChecksum
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER32
*) &PeHdr
->OptionalHeader
).CheckSum
);
855 } else if (PeHdr
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
) {
856 PeHdrSizeOfImage
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).SizeOfImage
);
857 PeHdrChecksum
= (UINT32
*) (&(*(EFI_IMAGE_OPTIONAL_HEADER64
*) &PeHdr
->OptionalHeader
).CheckSum
);
863 "unknown machine type in TE image",
864 "machine type=0x%X, file=%s",
865 (UINT32
) PeHdr
->FileHeader
.Machine
,
868 free ((VOID
*) MemoryImagePointer
);
873 if (*PeHdrSizeOfImage
!= ImageContext
.ImageSize
) {
874 *PeHdrSizeOfImage
= (UINT32
) ImageContext
.ImageSize
;
875 if (*PeHdrChecksum
) {
880 TEImageHeader
->ImageBase
= (UINT64
) (NewPe32BaseAddress
+ TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
882 (UINT8
*) (CurrentPe32Section
.Pe32Section
+ 1) + sizeof (EFI_TE_IMAGE_HEADER
),
883 (VOID
*) ((UINT8
*) MemoryImagePointerAligned
+ TEImageHeader
->StrippedSize
),
884 GetLength (CurrentPe32Section
.Pe32Section
->CommonHeader
.Size
) - sizeof (EFI_PE32_SECTION
) -
885 sizeof (EFI_TE_IMAGE_HEADER
)
887 free ((VOID
*) MemoryImagePointer
);
889 if (FfsFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
890 TailSize
= sizeof (EFI_FFS_FILE_TAIL
);
895 // Now update file checksum
897 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
898 SavedState
= FfsFile
->State
;
899 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
901 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
902 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
904 GetLength (FfsFile
->Size
) - TailSize
907 FfsFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
910 FfsFile
->State
= SavedState
;
913 // Update tail if present
915 if (FfsFile
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
916 TailValue
= (EFI_FFS_FILE_TAIL
) (~(FfsFile
->IntegrityCheck
.TailReference
));
917 *(EFI_FFS_FILE_TAIL
*) (((UINTN
) FfsFile
+ GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_TAIL
))) = TailValue
;
921 // If we found no files, then emit an error if no compressed sections either
923 if (FoundCount
== 0) {
924 Status
= GetSectionByType (FfsFile
, EFI_SECTION_COMPRESSION
, Index
, &CurrentPe32Section
);
925 if (EFI_ERROR (Status
)) {
926 Error (NULL
, 0, 0, "no PE32, TE, nor compressed section found in FV file", FileGuidString
);
927 return EFI_NOT_FOUND
;
938 IN OUT UINT32
*ReadSize
,
945 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
949 FileHandle - The handle to the PE/COFF file
951 FileOffset - The offset, in bytes, into the file to read
953 ReadSize - The number of bytes to read from the file starting at FileOffset
955 Buffer - A pointer to the buffer to read the data into.
959 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
967 Destination8
= Buffer
;
968 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
971 *(Destination8
++) = *(Source8
++);