3 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
4 Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 This file contains the internal functions required to generate a Firmware Volume.
27 #include <uuid/uuid.h>
36 #include "GenFvInternalLib.h"
38 #include "PeCoffLib.h"
39 #include "WinNtInclude.h"
42 STATIC UINT32 MaxFfsAlignment
= 0;
44 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
45 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
46 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
47 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
49 CHAR8
*mFvbAttributeName
[] = {
50 EFI_FVB2_READ_DISABLED_CAP_STRING
,
51 EFI_FVB2_READ_ENABLED_CAP_STRING
,
52 EFI_FVB2_READ_STATUS_STRING
,
53 EFI_FVB2_WRITE_DISABLED_CAP_STRING
,
54 EFI_FVB2_WRITE_ENABLED_CAP_STRING
,
55 EFI_FVB2_WRITE_STATUS_STRING
,
56 EFI_FVB2_LOCK_CAP_STRING
,
57 EFI_FVB2_LOCK_STATUS_STRING
,
59 EFI_FVB2_STICKY_WRITE_STRING
,
60 EFI_FVB2_MEMORY_MAPPED_STRING
,
61 EFI_FVB2_ERASE_POLARITY_STRING
,
62 EFI_FVB2_READ_LOCK_CAP_STRING
,
63 EFI_FVB2_READ_LOCK_STATUS_STRING
,
64 EFI_FVB2_WRITE_LOCK_CAP_STRING
,
65 EFI_FVB2_WRITE_LOCK_STATUS_STRING
68 CHAR8
*mFvbAlignmentName
[] = {
69 EFI_FVB2_ALIGNMENT_1_STRING
,
70 EFI_FVB2_ALIGNMENT_2_STRING
,
71 EFI_FVB2_ALIGNMENT_4_STRING
,
72 EFI_FVB2_ALIGNMENT_8_STRING
,
73 EFI_FVB2_ALIGNMENT_16_STRING
,
74 EFI_FVB2_ALIGNMENT_32_STRING
,
75 EFI_FVB2_ALIGNMENT_64_STRING
,
76 EFI_FVB2_ALIGNMENT_128_STRING
,
77 EFI_FVB2_ALIGNMENT_256_STRING
,
78 EFI_FVB2_ALIGNMENT_512_STRING
,
79 EFI_FVB2_ALIGNMENT_1K_STRING
,
80 EFI_FVB2_ALIGNMENT_2K_STRING
,
81 EFI_FVB2_ALIGNMENT_4K_STRING
,
82 EFI_FVB2_ALIGNMENT_8K_STRING
,
83 EFI_FVB2_ALIGNMENT_16K_STRING
,
84 EFI_FVB2_ALIGNMENT_32K_STRING
,
85 EFI_FVB2_ALIGNMENT_64K_STRING
,
86 EFI_FVB2_ALIGNMENT_128K_STRING
,
87 EFI_FVB2_ALIGNMENT_256K_STRING
,
88 EFI_FVB2_ALIGNMENT_512K_STRING
,
89 EFI_FVB2_ALIGNMENT_1M_STRING
,
90 EFI_FVB2_ALIGNMENT_2M_STRING
,
91 EFI_FVB2_ALIGNMENT_4M_STRING
,
92 EFI_FVB2_ALIGNMENT_8M_STRING
,
93 EFI_FVB2_ALIGNMENT_16M_STRING
,
94 EFI_FVB2_ALIGNMENT_32M_STRING
,
95 EFI_FVB2_ALIGNMENT_64M_STRING
,
96 EFI_FVB2_ALIGNMENT_128M_STRING
,
97 EFI_FVB2_ALIGNMENT_256M_STRING
,
98 EFI_FVB2_ALIGNMENT_512M_STRING
,
99 EFI_FVB2_ALIGNMENT_1G_STRING
,
100 EFI_FVB2_ALIGNMENT_2G_STRING
104 // This data array will be located at the base of the Firmware Volume Header (FVH)
105 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
106 // will be used to keep the FVH checksum consistent.
107 // This code will be run in response to a starutp IPI for HT-enabled systems.
109 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
111 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
113 // EA D0 FF 00 F0 ; far jmp F000:FFD0
114 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
115 // 0, 0 ; Checksum Padding
135 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
137 // EB CE ; jmp short ($-0x30)
138 // ; (from offset 0x0 to offset 0xFFD0)
139 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
140 // 0, 0 ; Checksum Padding
161 CAP_INFO mCapDataInfo
;
162 BOOLEAN mIsLargeFfs
= FALSE
;
164 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
165 UINT32 mFvBaseAddressNumber
= 0;
169 IN MEMORY_FILE
*InfFile
,
176 This function parses a FV.INF file and copies info into a FV_INFO structure.
180 InfFile Memory file image.
181 FvInfo Information read from INF file.
185 EFI_SUCCESS INF file information successfully retrieved.
186 EFI_ABORTED INF file has an invalid format.
187 EFI_NOT_FOUND A required string was not found in the INF file.
190 CHAR8 Value
[MAX_LONG_FILE_PATH
];
198 // Read the FV base address
200 if (!mFvDataInfo
.BaseAddressSet
) {
201 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
202 if (Status
== EFI_SUCCESS
) {
204 // Get the base address
206 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
207 if (EFI_ERROR (Status
)) {
208 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
211 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
213 FvInfo
->BaseAddress
= Value64
;
214 FvInfo
->BaseAddressSet
= TRUE
;
219 // Read the FV File System Guid
221 if (!FvInfo
->FvFileSystemGuidSet
) {
222 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
223 if (Status
== EFI_SUCCESS
) {
225 // Get the guid value
227 Status
= StringToGuid (Value
, &GuidValue
);
228 if (EFI_ERROR (Status
)) {
229 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
232 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
233 FvInfo
->FvFileSystemGuidSet
= TRUE
;
238 // Read the FV Extension Header File Name
240 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
241 if (Status
== EFI_SUCCESS
) {
242 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
246 // Read the FV file name
248 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
249 if (Status
== EFI_SUCCESS
) {
251 // copy the file name
253 strcpy (FvInfo
->FvName
, Value
);
259 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
260 if ((mFvbAttributeName
[Index
] != NULL
) && \
261 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
262 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
263 FvInfo
->FvAttributes
|= 1 << Index
;
264 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
265 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
274 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
275 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
276 if (strcmp (Value
, TRUE_STRING
) == 0) {
277 FvInfo
->FvAttributes
|= Index
<< 16;
278 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
285 // Read weak alignment flag
287 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_WEAK_ALIGNMENT_STRING
, 0, Value
);
288 if (Status
== EFI_SUCCESS
) {
289 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
290 FvInfo
->FvAttributes
|= EFI_FVB2_WEAK_ALIGNMENT
;
291 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
292 Error (NULL
, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
300 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
301 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
305 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
307 if (Status
== EFI_SUCCESS
) {
309 // Update the size of block
311 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
312 if (EFI_ERROR (Status
)) {
313 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
317 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
318 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
321 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
322 // and should return an error.
324 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
325 if (!EFI_ERROR (Status
)) {
326 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
337 // Read blocks number
339 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
341 if (Status
== EFI_SUCCESS
) {
343 // Update the number of blocks
345 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
346 if (EFI_ERROR (Status
)) {
347 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
351 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
352 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
358 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
366 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
367 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
372 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
374 // Read the FFS file list
376 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
378 if (Status
== EFI_SUCCESS
) {
382 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
383 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
389 if ((Index
+ Number
) == 0) {
390 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
398 IN EFI_FFS_FILE_HEADER
*FfsFile
,
399 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
405 This function changes the FFS file attributes based on the erase polarity
406 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
419 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
420 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
421 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
427 IN EFI_FFS_FILE_HEADER
*FfsFile
,
428 IN OUT UINT32
*Alignment
434 This function determines the alignment of the FFS input file from the file
439 FfsFile FFS file to parse
440 Alignment The minimum required alignment offset of the FFS file
444 EFI_SUCCESS The function completed successfully.
445 EFI_INVALID_PARAMETER One of the input parameters was invalid.
446 EFI_ABORTED An error occurred.
451 // Verify input parameters.
453 if (FfsFile
== NULL
|| Alignment
== NULL
) {
454 return EFI_INVALID_PARAMETER
;
457 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
475 // 128 byte alignment
482 // 512 byte alignment
503 // 32K byte alignment
510 // 64K byte alignment
524 IN OUT MEMORY_FILE
*FvImage
,
525 IN UINT32 DataAlignment
,
527 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
,
528 IN UINT32 NextFfsSize
534 This function adds a pad file to the FV image if it required to align the
535 data of the next file.
539 FvImage The memory image of the FV to add it to.
540 The current offset must be valid.
541 DataAlignment The data alignment of the next FFS file.
542 FvEnd End of the empty data in FvImage.
543 ExtHeader PI FvExtHeader Optional
547 EFI_SUCCESS The function completed successfully.
548 EFI_INVALID_PARAMETER One of the input parameters was invalid.
549 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
554 EFI_FFS_FILE_HEADER
*PadFile
;
556 UINT32 NextFfsHeaderSize
;
557 UINT32 CurFfsHeaderSize
;
559 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
561 // Verify input parameters.
563 if (FvImage
== NULL
) {
564 return EFI_INVALID_PARAMETER
;
568 // Calculate the pad file size
572 // Append extension header size
574 if (ExtHeader
!= NULL
) {
575 PadFileSize
= ExtHeader
->ExtHeaderSize
;
576 if (PadFileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
577 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
579 PadFileSize
+= CurFfsHeaderSize
;
581 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
582 if (NextFfsSize
>= MAX_FFS_SIZE
) {
583 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
586 // Check if a pad file is necessary
588 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ NextFfsHeaderSize
) % DataAlignment
== 0) {
591 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
) + NextFfsHeaderSize
;
593 // Add whatever it takes to get to the next aligned address
595 while ((PadFileSize
% DataAlignment
) != 0) {
599 // Subtract the next file header size
601 PadFileSize
-= NextFfsHeaderSize
;
603 // Subtract the starting offset to get size
605 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
609 // Verify that we have enough space for the file header
611 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
612 return EFI_OUT_OF_RESOURCES
;
616 // Write pad file header
618 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
621 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
623 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
624 PadFile
->Attributes
= 0;
627 // Write pad file size (calculated size minus next file header size)
629 if (PadFileSize
>= MAX_FFS_SIZE
) {
630 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
631 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= PadFileSize
;
632 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
634 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
635 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
636 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
640 // Fill in checksums and state, they must be 0 for checksumming.
642 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
643 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
645 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, CurFfsHeaderSize
);
646 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
648 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
650 (EFI_FFS_FILE_HEADER
*) PadFile
,
651 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
655 // Update the current FV pointer
657 FvImage
->CurrentFilePointer
+= PadFileSize
;
659 if (ExtHeader
!= NULL
) {
661 // Copy Fv Extension Header and Set Fv Extension header offset
663 memcpy ((UINT8
*)PadFile
+ CurFfsHeaderSize
, ExtHeader
, ExtHeader
->ExtHeaderSize
);
664 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) ((UINT8
*)PadFile
+ CurFfsHeaderSize
) - (UINTN
) FvImage
->FileImage
);
666 // Make next file start at QWord Boundry
668 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
669 FvImage
->CurrentFilePointer
++;
678 IN EFI_FFS_FILE_HEADER
*FileBuffer
684 This function checks the header to validate if it is a VTF file
688 FileBuffer Buffer in which content of a file has been read.
692 TRUE If this is a VTF file
693 FALSE If this is not a VTF file
697 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
706 IN OUT
FILE *FvMapFile
,
708 IN EFI_FFS_FILE_HEADER
*FfsFile
,
709 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
710 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
716 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
717 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
721 FvMapFile A pointer to FvMap File
722 FileName Ffs File PathName
723 FfsFile A pointer to Ffs file image.
724 ImageBaseAddress PeImage Base Address.
725 pImageContext Image Context Information.
729 EFI_SUCCESS Added required map information.
733 CHAR8 PeMapFileName
[MAX_LONG_FILE_PATH
];
735 CHAR8 FileGuidName
[MAX_LINE_LEN
];
737 CHAR8 Line
[MAX_LINE_LEN
];
738 CHAR8 KeyWord
[MAX_LINE_LEN
];
739 CHAR8 FunctionName
[MAX_LINE_LEN
];
740 EFI_PHYSICAL_ADDRESS FunctionAddress
;
742 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
744 UINT32 AddressOfEntryPoint
;
746 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
747 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
748 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
749 unsigned long long TempLongAddress
;
750 UINT32 TextVirtualAddress
;
751 UINT32 DataVirtualAddress
;
752 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
755 // Init local variable
759 // Print FileGuid to string buffer.
761 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
764 // Construct Map file Name
766 strcpy (PeMapFileName
, FileName
);
769 // Change '\\' to '/', unified path format.
771 Cptr
= PeMapFileName
;
772 while (*Cptr
!= '\0') {
774 *Cptr
= FILE_SEP_CHAR
;
782 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
783 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
786 if (Cptr
< PeMapFileName
) {
787 return EFI_NOT_FOUND
;
799 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
803 strcpy (KeyWord
, Cptr
+ 1);
807 // AddressOfEntryPoint and Offset in Image
809 if (!pImageContext
->IsTeImage
) {
810 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
811 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
813 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
816 sizeof (EFI_IMAGE_FILE_HEADER
) +
817 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
819 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
821 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
822 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
823 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
824 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
825 Index
= TEImageHeader
->NumberOfSections
;
829 // module information output
831 if (ImageBaseAddress
== 0) {
832 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
833 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
835 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
836 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
839 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&& pImageContext
->Machine
== EFI_IMAGE_MACHINE_IA64
) {
841 // Process IPF PLABEL to get the real address after the image has been rebased.
842 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
844 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64
*)((UINTN
) pImageContext
->Handle
+ (UINTN
) AddressOfEntryPoint
)));
846 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
848 fprintf (FvMapFile
, ")\n");
850 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
851 TextVirtualAddress
= 0;
852 DataVirtualAddress
= 0;
853 for (; Index
> 0; Index
--, SectionHeader
++) {
854 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
855 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
856 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
857 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
858 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
859 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
862 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
863 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
864 fprintf (FvMapFile
, ")\n\n");
869 PeMapFile
= fopen (LongFilePath (PeMapFileName
), "r");
870 if (PeMapFile
== NULL
) {
871 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
874 VerboseMsg ("The map file is %s", PeMapFileName
);
877 // Output Functions information into Fv Map file
879 LinkTimeBaseAddress
= 0;
880 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
884 if (Line
[0] == 0x0a) {
889 // By Address and Static keyword
891 if (FunctionType
== 0) {
892 sscanf (Line
, "%s", KeyWord
);
893 if (stricmp (KeyWord
, "Address") == 0) {
898 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
899 } else if (stricmp (KeyWord
, "Static") == 0) {
901 // static function list
904 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
905 } else if (stricmp (KeyWord
, "Preferred") ==0) {
906 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
907 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
912 // Printf Function Information
914 if (FunctionType
== 1) {
915 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
916 FunctionAddress
= (UINT64
) TempLongAddress
;
917 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
918 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
919 fprintf (FvMapFile
, "%s\n", FunctionName
);
921 } else if (FunctionType
== 2) {
922 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
923 FunctionAddress
= (UINT64
) TempLongAddress
;
924 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
925 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
926 fprintf (FvMapFile
, "%s\n", FunctionName
);
933 fprintf (FvMapFile
, "\n\n");
941 IN OUT MEMORY_FILE
*FvImage
,
944 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
946 IN
FILE *FvReportFile
952 This function adds a file to the FV image. The file will pad to the
953 appropriate alignment if required.
957 FvImage The memory image of the FV to add it to. The current offset
959 FvInfo Pointer to information about the FV.
960 Index The file in the FvInfo file list to add.
961 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
962 to the end of the FvImage then no VTF previously found.
963 FvMapFile Pointer to FvMap File
964 FvReportFile Pointer to FvReport File
968 EFI_SUCCESS The function completed successfully.
969 EFI_INVALID_PARAMETER One of the input parameters was invalid.
970 EFI_ABORTED An error occurred.
971 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
979 UINT32 CurrentFileAlignment
;
982 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
986 // Verify input parameters.
988 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
989 return EFI_INVALID_PARAMETER
;
993 // Read the file to add
995 NewFile
= fopen (LongFilePath (FvInfo
->FvFiles
[Index
]), "rb");
997 if (NewFile
== NULL
) {
998 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
1003 // Get the file size
1005 FileSize
= _filelength (fileno (NewFile
));
1008 // Read the file into a buffer
1010 FileBuffer
= malloc (FileSize
);
1011 if (FileBuffer
== NULL
) {
1012 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
1013 return EFI_OUT_OF_RESOURCES
;
1016 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1019 // Done with the file, from this point on we will just use the buffer read.
1024 // Verify read successful
1026 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1028 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1033 // For None PI Ffs file, directly add them into FvImage.
1035 if (!FvInfo
->IsPiFvImage
) {
1036 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1037 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1038 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1040 FvImage
->CurrentFilePointer
+= FileSize
;
1048 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1049 if (EFI_ERROR (Status
)) {
1051 Error (NULL
, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo
->FvFiles
[Index
]);
1052 return EFI_INVALID_PARAMETER
;
1056 // Verify space exists to add the file
1058 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1060 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1061 return EFI_OUT_OF_RESOURCES
;
1065 // Verify the input file is the duplicated file in this Fv image
1067 for (Index1
= 0; Index1
< Index
; Index1
++) {
1068 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1069 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1070 PrintGuid ((EFI_GUID
*) FileBuffer
);
1071 return EFI_INVALID_PARAMETER
;
1074 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1077 // Update the file state based on polarity of the FV.
1079 UpdateFfsFileState (
1080 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1081 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1085 // Check if alignment is required
1087 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1090 // Find the largest alignment of all the FFS files in the FV
1092 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1093 MaxFfsAlignment
= CurrentFileAlignment
;
1096 // If we have a VTF file, add it at the top.
1098 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1099 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1101 // No previous VTF, add this one.
1103 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1105 // Sanity check. The file MUST align appropriately
1107 if (((UINTN
) *VtfFileImage
+ GetFfsHeaderLength((EFI_FFS_FILE_HEADER
*)FileBuffer
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1108 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1113 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1114 // Rebase for the debug genfvmap tool
1116 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1117 if (EFI_ERROR (Status
)) {
1118 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1124 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1126 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1127 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1130 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1134 // Already found a VTF file.
1136 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1143 // Add pad file if necessary
1145 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
, FileSize
);
1146 if (EFI_ERROR (Status
)) {
1147 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1154 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1156 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1157 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1159 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1160 if (EFI_ERROR (Status
)) {
1161 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1167 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1168 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1169 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1170 FvImage
->CurrentFilePointer
+= FileSize
;
1172 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1177 // Make next file start at QWord Boundry
1179 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1180 FvImage
->CurrentFilePointer
++;
1185 // Free allocated memory.
1194 IN MEMORY_FILE
*FvImage
,
1195 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1199 Routine Description:
1201 This function places a pad file between the last file in the FV and the VTF
1202 file if the VTF file exists.
1206 FvImage Memory file for the FV memory image
1207 VtfFileImage The address of the VTF file. If this is the end of the FV
1208 image, no VTF exists and no pad file is needed.
1212 EFI_SUCCESS Completed successfully.
1213 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1217 EFI_FFS_FILE_HEADER
*PadFile
;
1219 UINT32 FfsHeaderSize
;
1222 // If there is no VTF or the VTF naturally follows the previous file without a
1223 // pad file, then there's nothing to do
1225 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1226 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1230 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1231 return EFI_INVALID_PARAMETER
;
1235 // Pad file starts at beginning of free space
1237 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1240 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1242 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1243 PadFile
->Attributes
= 0;
1246 // FileSize includes the EFI_FFS_FILE_HEADER
1248 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1249 if (FileSize
>= MAX_FFS_SIZE
) {
1250 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
1251 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
1252 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= FileSize
;
1253 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
1256 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1257 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1258 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1259 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
1263 // Fill in checksums and state, must be zero during checksum calculation.
1265 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1266 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1268 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, FfsHeaderSize
);
1269 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1271 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1273 UpdateFfsFileState (
1274 (EFI_FFS_FILE_HEADER
*) PadFile
,
1275 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1278 // Update the current FV pointer
1280 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1287 IN MEMORY_FILE
*FvImage
,
1289 IN EFI_FFS_FILE_HEADER
*VtfFile
1293 Routine Description:
1295 This parses the FV looking for the PEI core and then plugs the address into
1296 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1297 complete an IA32 Bootstrap FV.
1301 FvImage Memory file for the FV memory image
1302 FvInfo Information read from INF file.
1303 VtfFile Pointer to the VTF file in the FV image.
1307 EFI_SUCCESS Function Completed successfully.
1308 EFI_ABORTED Error encountered.
1309 EFI_INVALID_PARAMETER A required parameter was NULL.
1310 EFI_NOT_FOUND PEI Core file not found.
1314 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1315 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1317 EFI_FILE_SECTION_POINTER Pe32Section
;
1321 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1322 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1323 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
1324 INT32 Ia32SecEntryOffset
;
1325 UINT32
*Ia32ResetAddressPtr
;
1327 UINT8
*BytePointer2
;
1328 UINT16
*WordPointer
;
1332 EFI_FFS_FILE_STATE SavedState
;
1334 FIT_TABLE
*FitTablePtr
;
1335 BOOLEAN Vtf0Detected
;
1336 UINT32 FfsHeaderSize
;
1337 UINT32 SecHeaderSize
;
1340 // Verify input parameters
1342 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1343 return EFI_INVALID_PARAMETER
;
1346 // Initialize FV library
1348 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1353 Status
= VerifyFfsFile (VtfFile
);
1354 if (EFI_ERROR (Status
)) {
1355 return EFI_INVALID_PARAMETER
;
1359 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1360 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1361 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1362 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1363 IA32_X64_VTF0_SIGNATURE
)
1365 Vtf0Detected
= TRUE
;
1367 Vtf0Detected
= FALSE
;
1371 // Find the Sec Core
1373 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1374 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1377 // If the SEC core file is not found, but the VTF-0 signature
1378 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1379 // This means no modifications are required to the VTF.
1384 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1388 // Sec Core found, now find PE32 section
1390 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1391 if (Status
== EFI_NOT_FOUND
) {
1392 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1395 if (EFI_ERROR (Status
)) {
1396 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1400 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1401 Status
= GetPe32Info (
1402 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1408 if (EFI_ERROR (Status
)) {
1409 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1415 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1416 MachineType
== EFI_IMAGE_MACHINE_X64
)
1419 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1420 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1421 // This means no modifications are required to the VTF.
1427 // Physical address is FV base + offset of PE32 + offset of the entry point
1429 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1430 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1431 SecCorePhysicalAddress
+= EntryPoint
;
1432 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1435 // Find the PEI Core
1437 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1438 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
1439 Error (NULL
, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1443 // PEI Core found, now find PE32 or TE section
1445 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1446 if (Status
== EFI_NOT_FOUND
) {
1447 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1450 if (EFI_ERROR (Status
)) {
1451 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1455 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1456 Status
= GetPe32Info (
1457 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1463 if (EFI_ERROR (Status
)) {
1464 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1468 // Physical address is FV base + offset of PE32 + offset of the entry point
1470 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1471 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1472 PeiCorePhysicalAddress
+= EntryPoint
;
1473 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1475 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
1477 // Update PEI_CORE address
1480 // Set the uncached attribute bit in the physical address
1482 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
1485 // Check if address is aligned on a 16 byte boundary
1487 if (PeiCorePhysicalAddress
& 0xF) {
1488 Error (NULL
, 0, 3000, "Invalid",
1489 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1490 (unsigned long long) PeiCorePhysicalAddress
1495 // First Get the FIT table address
1497 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
1499 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
1501 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
1503 if (!EFI_ERROR (Status
)) {
1504 UpdateFitCheckSum (FitTablePtr
);
1508 // Update SEC_CORE address
1511 // Set the uncached attribute bit in the physical address
1513 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
1515 // Check if address is aligned on a 16 byte boundary
1517 if (SecCorePhysicalAddress
& 0xF) {
1518 Error (NULL
, 0, 3000, "Invalid",
1519 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1520 (unsigned long long) SecCorePhysicalAddress
1525 // Update the address
1527 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
1528 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
1530 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1532 // Get the location to update
1534 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1537 // Write lower 32 bits of physical address for Pei Core entry
1539 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1542 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1544 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1546 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1547 if (Ia32SecEntryOffset
<= -65536) {
1548 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1549 return STATUS_ERROR
;
1552 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1555 // Update the BFV base address
1557 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1558 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1559 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1562 // Update the Startup AP in the FVH header block ZeroVector region.
1564 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1565 if (FvInfo
->Size
<= 0x10000) {
1566 BytePointer2
= m64kRecoveryStartupApDataArray
;
1567 } else if (FvInfo
->Size
<= 0x20000) {
1568 BytePointer2
= m128kRecoveryStartupApDataArray
;
1570 BytePointer2
= m128kRecoveryStartupApDataArray
;
1572 // Find the position to place Ap reset vector, the offset
1573 // between the position and the end of Fvrecovery.fv file
1574 // should not exceed 128kB to prevent Ap reset vector from
1575 // outside legacy E and F segment
1577 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1578 if (EFI_ERROR (Status
)) {
1579 Error (NULL
, 0, 3000, "Invalid", "FV image does not have enough space to place AP reset vector. The FV image needs to reserve at least 4KB of unused space.");
1584 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1585 BytePointer
[Index
] = BytePointer2
[Index
];
1588 // Calculate the checksum
1591 WordPointer
= (UINT16
*) (BytePointer
);
1592 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1593 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1597 // Update the checksum field
1599 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1600 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1603 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1605 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1606 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1607 if ((IpiVector
& 0xFFF) != 0) {
1608 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1611 IpiVector
= IpiVector
>> 12;
1612 IpiVector
= IpiVector
& 0xFF;
1615 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1617 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1618 *Ia32ResetAddressPtr
= IpiVector
;
1619 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1621 // Since the ARM reset vector is in the FV Header you really don't need a
1622 // Volume Top File, but if you have one for some reason don't crash...
1624 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1626 // Since the AArch64 reset vector is in the FV Header you really don't need a
1627 // Volume Top File, but if you have one for some reason don't crash...
1630 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1635 // Now update file checksum
1637 SavedState
= VtfFile
->State
;
1638 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1640 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1641 FfsHeaderSize
= GetFfsHeaderLength(VtfFile
);
1642 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1643 (UINT8
*) ((UINT8
*)VtfFile
+ FfsHeaderSize
),
1644 GetFfsFileLength (VtfFile
) - FfsHeaderSize
1647 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1650 VtfFile
->State
= SavedState
;
1657 UpdateArmResetVectorIfNeeded (
1658 IN MEMORY_FILE
*FvImage
,
1663 Routine Description:
1664 This parses the FV looking for SEC and patches that address into the
1665 beginning of the FV header.
1667 For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
1668 For AArch64 the reset vector is at 0x00000000.
1670 This would commonly map to the first entry in the ROM.
1680 We support two schemes on ARM.
1681 1) Beginning of the FV is the reset vector
1682 2) Reset vector is data bytes FDF file and that code branches to reset vector
1683 in the beginning of the FV (fixed size offset).
1685 Need to have the jump for the reset vector at location zero.
1686 We also need to store the address or PEI (if it exists).
1687 We stub out a return from interrupt in case the debugger
1688 is using SWI (not done for AArch64, not enough space in struct).
1689 The optional entry to the common exception handler is
1690 to support full featured exception handling from ROM and is currently
1691 not support by this tool.
1694 FvImage Memory file for the FV memory image
1695 FvInfo Information read from INF file.
1699 EFI_SUCCESS Function Completed successfully.
1700 EFI_ABORTED Error encountered.
1701 EFI_INVALID_PARAMETER A required parameter was NULL.
1702 EFI_NOT_FOUND PEI Core file not found.
1706 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1707 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1709 EFI_FILE_SECTION_POINTER Pe32Section
;
1713 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1714 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1715 INT32 ResetVector
[4]; // ARM32:
1716 // 0 - is branch relative to SEC entry point
1717 // 1 - PEI Entry Point
1718 // 2 - movs pc,lr for a SWI handler
1719 // 3 - Place holder for Common Exception Handler
1720 // AArch64: Used as UINT64 ResetVector[2]
1721 // 0 - is branch relative to SEC entry point
1722 // 1 - PEI Entry Point
1725 // Verify input parameters
1727 if (FvImage
== NULL
|| FvInfo
== NULL
) {
1728 return EFI_INVALID_PARAMETER
;
1731 // Initialize FV library
1733 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1736 // Find the Sec Core
1738 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1739 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1741 // Maybe hardware does SEC job and we only have PEI Core?
1745 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1747 PeiCorePhysicalAddress
= 0;
1748 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1749 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1751 // PEI Core found, now find PE32 or TE section
1753 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1754 if (Status
== EFI_NOT_FOUND
) {
1755 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1758 if (EFI_ERROR (Status
)) {
1759 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1763 Status
= GetPe32Info (
1764 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1770 if (EFI_ERROR (Status
)) {
1771 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1775 // Physical address is FV base + offset of PE32 + offset of the entry point
1777 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1778 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
1779 PeiCorePhysicalAddress
+= EntryPoint
;
1780 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1782 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
|| MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1783 memset (ResetVector
, 0, sizeof (ResetVector
));
1784 // Address of PEI Core, if we have one
1785 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1789 // Copy to the beginning of the FV
1791 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1799 // Sec Core found, now find PE32 section
1801 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1802 if (Status
== EFI_NOT_FOUND
) {
1803 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1806 if (EFI_ERROR (Status
)) {
1807 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1811 Status
= GetPe32Info (
1812 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1817 if (EFI_ERROR (Status
)) {
1818 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1822 if ((MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
1824 // If SEC is not ARM we have nothing to do
1830 // Physical address is FV base + offset of PE32 + offset of the entry point
1832 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1833 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
1834 SecCorePhysicalAddress
+= EntryPoint
;
1835 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1838 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1840 PeiCorePhysicalAddress
= 0;
1841 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1842 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1844 // PEI Core found, now find PE32 or TE section
1846 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1847 if (Status
== EFI_NOT_FOUND
) {
1848 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1851 if (EFI_ERROR (Status
)) {
1852 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1856 Status
= GetPe32Info (
1857 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1863 if (EFI_ERROR (Status
)) {
1864 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1868 // Physical address is FV base + offset of PE32 + offset of the entry point
1870 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1871 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
1872 PeiCorePhysicalAddress
+= EntryPoint
;
1873 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1876 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1877 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
1878 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
1879 ResetVector
[0] = (INT32
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
- 8) >> 2;
1881 if (ResetVector
[0] > 0x00FFFFFF) {
1882 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
1886 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
1887 ResetVector
[0] |= 0xEB000000;
1890 // Address of PEI Core, if we have one
1891 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1893 // SWI handler movs pc,lr. Just in case a debugger uses SWI
1894 ResetVector
[2] = 0xE1B0F07E;
1896 // Place holder to support a common interrupt handler from ROM.
1897 // Currently not suppprted. For this to be used the reset vector would not be in this FV
1898 // and the exception vectors would be hard coded in the ROM and just through this address
1899 // to find a common handler in the a module in the FV.
1901 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1904 ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
1905 array at the moment, for AArch64, does not allow us space for this as the header only
1906 allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
1907 within the first 4GB of addressable RAM we could potensioally adopt the same ResetVector
1908 layout as above. But for the moment we replace the four 32bit vectors with two 64bit
1909 vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
1913 ((UINT64
*)ResetVector
)[0] = (UINT64
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
) >> 2;
1915 // B SecEntryPoint - signed_immed_26 part +/-128MB offset
1916 if ( ((UINT64
*)ResetVector
)[0] > 0x03FFFFFF) {
1917 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
1920 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
1921 ((UINT64
*)ResetVector
)[0] |= 0x14000000;
1923 // Address of PEI Core, if we have one
1924 ((UINT64
*)ResetVector
)[1] = (UINT64
)PeiCorePhysicalAddress
;
1927 Error (NULL
, 0, 3000, "Invalid", "Unknown ARM machine type");
1932 // Copy to the beginning of the FV
1934 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1936 DebugMsg (NULL
, 0, 9, "Update Reset vector in FV Header", NULL
);
1944 OUT UINT32
*EntryPoint
,
1945 OUT UINT32
*BaseOfCode
,
1946 OUT UINT16
*MachineType
1950 Routine Description:
1952 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
1953 See EfiImage.h for machine types. The entry point offset is from the beginning
1954 of the PE32 buffer passed in.
1958 Pe32 Beginning of the PE32.
1959 EntryPoint Offset from the beginning of the PE32 to the image entry point.
1960 BaseOfCode Base address of code.
1961 MachineType Magic number for the machine type.
1965 EFI_SUCCESS Function completed successfully.
1966 EFI_ABORTED Error encountered.
1967 EFI_INVALID_PARAMETER A required parameter was NULL.
1968 EFI_UNSUPPORTED The operation is unsupported.
1972 EFI_IMAGE_DOS_HEADER
*DosHeader
;
1973 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
1974 EFI_TE_IMAGE_HEADER
*TeHeader
;
1977 // Verify input parameters
1980 return EFI_INVALID_PARAMETER
;
1984 // First check whether it is one TE Image.
1986 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
1987 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
1989 // By TeImage Header to get output
1991 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1992 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1993 *MachineType
= TeHeader
->Machine
;
1997 // Then check whether
1998 // First is the DOS header
2000 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2003 // Verify DOS header is expected
2005 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2006 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
2007 return EFI_UNSUPPORTED
;
2010 // Immediately following is the NT header.
2012 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2015 // Verify NT header is expected
2017 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2018 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
2019 return EFI_UNSUPPORTED
;
2024 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
2025 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
2026 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
2030 // Verify machine type is supported
2032 if ((*MachineType
!= EFI_IMAGE_MACHINE_IA32
) && (*MachineType
!= EFI_IMAGE_MACHINE_IA64
) && (*MachineType
!= EFI_IMAGE_MACHINE_X64
) && (*MachineType
!= EFI_IMAGE_MACHINE_EBC
) &&
2033 (*MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (*MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
2034 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
2035 return EFI_UNSUPPORTED
;
2043 IN CHAR8
*InfFileImage
,
2044 IN UINTN InfFileSize
,
2045 IN CHAR8
*FvFileName
,
2046 IN CHAR8
*MapFileName
2050 Routine Description:
2052 This is the main function which will be called from application.
2056 InfFileImage Buffer containing the INF file contents.
2057 InfFileSize Size of the contents of the InfFileImage buffer.
2058 FvFileName Requested name for the FV file.
2059 MapFileName Fv map file to log fv driver information.
2063 EFI_SUCCESS Function completed successfully.
2064 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2065 EFI_ABORTED Error encountered.
2066 EFI_INVALID_PARAMETER A required parameter was NULL.
2071 MEMORY_FILE InfMemoryFile
;
2072 MEMORY_FILE FvImageMemoryFile
;
2074 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2075 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2076 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
2080 CHAR8 FvMapName
[MAX_LONG_FILE_PATH
];
2082 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2083 FILE *FvExtHeaderFile
;
2085 CHAR8 FvReportName
[MAX_LONG_FILE_PATH
];
2088 FvBufferHeader
= NULL
;
2091 FvReportFile
= NULL
;
2093 if (InfFileImage
!= NULL
) {
2095 // Initialize file structures
2097 InfMemoryFile
.FileImage
= InfFileImage
;
2098 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2099 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2102 // Parse the FV inf file for header information
2104 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2105 if (EFI_ERROR (Status
)) {
2106 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2112 // Update the file name return values
2114 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2115 FvFileName
= mFvDataInfo
.FvName
;
2118 if (FvFileName
== NULL
) {
2119 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2123 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2124 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2129 // Debug message Fv File System Guid
2131 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2132 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2133 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2134 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2135 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2136 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2137 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2138 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2139 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2140 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2141 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2142 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2143 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2147 // Add PI FV extension header
2150 FvExtHeaderFile
= NULL
;
2151 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2153 // Open the FV Extension Header file
2155 FvExtHeaderFile
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2158 // Get the file size
2160 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2163 // Allocate a buffer for the FV Extension Header
2165 FvExtHeader
= malloc(FileSize
);
2166 if (FvExtHeader
== NULL
) {
2167 fclose (FvExtHeaderFile
);
2168 return EFI_OUT_OF_RESOURCES
;
2172 // Read the FV Extension Header
2174 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2175 fclose (FvExtHeaderFile
);
2178 // See if there is an override for the FV Name GUID
2180 if (mFvDataInfo
.FvNameGuidSet
) {
2181 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2183 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2184 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2185 } else if (mFvDataInfo
.FvNameGuidSet
) {
2187 // Allocate a buffer for the FV Extension Header
2189 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2190 if (FvExtHeader
== NULL
) {
2191 return EFI_OUT_OF_RESOURCES
;
2193 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2194 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2198 // Debug message Fv Name Guid
2200 if (mFvDataInfo
.FvNameGuidSet
) {
2201 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2202 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2203 mFvDataInfo
.FvNameGuid
.Data2
,
2204 mFvDataInfo
.FvNameGuid
.Data3
,
2205 mFvDataInfo
.FvNameGuid
.Data4
[0],
2206 mFvDataInfo
.FvNameGuid
.Data4
[1],
2207 mFvDataInfo
.FvNameGuid
.Data4
[2],
2208 mFvDataInfo
.FvNameGuid
.Data4
[3],
2209 mFvDataInfo
.FvNameGuid
.Data4
[4],
2210 mFvDataInfo
.FvNameGuid
.Data4
[5],
2211 mFvDataInfo
.FvNameGuid
.Data4
[6],
2212 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2215 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0 ||
2216 CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem3Guid
) == 0) {
2217 mFvDataInfo
.IsPiFvImage
= TRUE
;
2221 // FvMap file to log the function address of all modules in one Fvimage
2223 if (MapFileName
!= NULL
) {
2224 strcpy (FvMapName
, MapFileName
);
2226 strcpy (FvMapName
, FvFileName
);
2227 strcat (FvMapName
, ".map");
2229 VerboseMsg ("FV Map file name is %s", FvMapName
);
2232 // FvReport file to log the FV information in one Fvimage
2234 strcpy (FvReportName
, FvFileName
);
2235 strcat (FvReportName
, ".txt");
2238 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2239 // And Update mFvDataInfo data.
2241 Status
= CalculateFvSize (&mFvDataInfo
);
2242 if (EFI_ERROR (Status
)) {
2245 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2248 // support fv image and empty fv image
2250 FvImageSize
= mFvDataInfo
.Size
;
2253 // Allocate the FV, assure FvImage Header 8 byte alignment
2255 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2256 if (FvBufferHeader
== NULL
) {
2257 return EFI_OUT_OF_RESOURCES
;
2259 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2262 // Initialize the FV to the erase polarity
2264 if (mFvDataInfo
.FvAttributes
== 0) {
2266 // Set Default Fv Attribute
2268 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2270 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2271 memset (FvImage
, -1, FvImageSize
);
2273 memset (FvImage
, 0, FvImageSize
);
2277 // Initialize FV header
2279 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2282 // Initialize the zero vector to all zeros.
2284 memset (FvHeader
->ZeroVector
, 0, 16);
2287 // Copy the Fv file system GUID
2289 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2291 FvHeader
->FvLength
= FvImageSize
;
2292 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2293 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2294 FvHeader
->Revision
= EFI_FVH_REVISION
;
2295 FvHeader
->ExtHeaderOffset
= 0;
2296 FvHeader
->Reserved
[0] = 0;
2299 // Copy firmware block map
2301 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2302 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2303 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2307 // Add block map terminator
2309 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2310 FvHeader
->BlockMap
[Index
].Length
= 0;
2313 // Complete the header
2315 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2316 FvHeader
->Checksum
= 0;
2317 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2320 // If there is no FFS file, generate one empty FV
2322 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2327 // Initialize our "file" view of the buffer
2329 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2330 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2331 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2334 // Initialize the FV library.
2336 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2339 // Initialize the VTF file address.
2341 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2346 FvMapFile
= fopen (LongFilePath (FvMapName
), "w");
2347 if (FvMapFile
== NULL
) {
2348 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2353 // Open FvReport file
2355 FvReportFile
= fopen (LongFilePath (FvReportName
), "w");
2356 if (FvReportFile
== NULL
) {
2357 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2361 // record FV size information into FvMap file.
2363 if (mFvTotalSize
!= 0) {
2364 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2365 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2367 if (mFvTakenSize
!= 0) {
2368 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2369 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2371 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2372 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2373 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2377 // record FV size information to FvReportFile.
2379 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2380 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2383 // Add PI FV extension header
2385 if (FvExtHeader
!= NULL
) {
2387 // Add FV Extended Header contents to the FV as a PAD file
2389 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2392 // Fv Extension header change update Fv Header Check sum
2394 FvHeader
->Checksum
= 0;
2395 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2401 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2405 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2408 // Exit if error detected while adding the file
2410 if (EFI_ERROR (Status
)) {
2416 // If there is a VTF file, some special actions need to occur.
2418 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2420 // Pad from the end of the last file to the beginning of the VTF file.
2421 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2423 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2424 if (EFI_ERROR (Status
)) {
2425 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2430 // Update reset vector (SALE_ENTRY for IPF)
2431 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2432 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2433 // reset vector. If the PEI Core is found, the VTF file will probably get
2434 // corrupted by updating the entry point.
2436 if ((mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2437 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2438 if (EFI_ERROR(Status
)) {
2439 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2442 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2448 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2449 if (EFI_ERROR (Status
)) {
2450 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2455 // Update Checksum for FvHeader
2457 FvHeader
->Checksum
= 0;
2458 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2462 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2464 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2465 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2466 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2468 // Update Checksum for FvHeader
2470 FvHeader
->Checksum
= 0;
2471 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2475 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2477 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2478 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2479 FvHeader
->Checksum
= 0;
2480 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2487 FvFile
= fopen (LongFilePath (FvFileName
), "wb");
2488 if (FvFile
== NULL
) {
2489 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2490 Status
= EFI_ABORTED
;
2494 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2495 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2496 Status
= EFI_ABORTED
;
2501 if (FvBufferHeader
!= NULL
) {
2502 free (FvBufferHeader
);
2505 if (FvExtHeader
!= NULL
) {
2509 if (FvFile
!= NULL
) {
2514 if (FvMapFile
!= NULL
) {
2519 if (FvReportFile
!= NULL
) {
2520 fflush (FvReportFile
);
2521 fclose (FvReportFile
);
2527 UpdatePeiCoreEntryInFit (
2528 IN FIT_TABLE
*FitTablePtr
,
2529 IN UINT64 PeiCorePhysicalAddress
2533 Routine Description:
2535 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2540 FitTablePtr - The pointer of FIT_TABLE.
2541 PeiCorePhysicalAddress - The address of Pei Core entry.
2545 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2546 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2550 FIT_TABLE
*TmpFitPtr
;
2552 UINTN NumFitComponents
;
2554 TmpFitPtr
= FitTablePtr
;
2555 NumFitComponents
= TmpFitPtr
->CompSize
;
2557 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2558 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2559 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2566 return EFI_NOT_FOUND
;
2571 IN FIT_TABLE
*FitTablePtr
2575 Routine Description:
2577 This function is used to update the checksum for FIT.
2582 FitTablePtr - The pointer of FIT_TABLE.
2590 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2591 FitTablePtr
->CheckSum
= 0;
2592 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2601 Routine Description:
2602 Calculate the FV size and Update Fv Size based on the actual FFS files.
2603 And Update FvInfo data.
2606 FvInfoPtr - The pointer to FV_INFO structure.
2609 EFI_ABORTED - Ffs Image Error
2610 EFI_SUCCESS - Successfully update FvSize
2613 UINTN CurrentOffset
;
2617 UINTN FvExtendHeaderSize
;
2618 UINT32 FfsAlignment
;
2619 UINT32 FfsHeaderSize
;
2620 EFI_FFS_FILE_HEADER FfsHeader
;
2621 BOOLEAN VtfFileFlag
;
2624 FvExtendHeaderSize
= 0;
2626 VtfFileFlag
= FALSE
;
2631 // Compute size for easy access later
2633 FvInfoPtr
->Size
= 0;
2634 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
2635 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
2639 // Caculate the required sizes for all FFS files.
2641 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
2643 for (Index
= 1;; Index
++) {
2644 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
2645 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
2651 // Calculate PI extension header
2653 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
2654 fpin
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2656 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2659 FvExtendHeaderSize
= _filelength (fileno (fpin
));
2661 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
2662 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
2665 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
2667 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2668 } else if (mFvDataInfo
.FvNameGuidSet
) {
2669 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2670 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2674 // Accumlate every FFS file size.
2676 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
2681 fpin
= fopen (LongFilePath (FvInfoPtr
->FvFiles
[Index
]), "rb");
2683 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
2687 // Get the file size
2689 FfsFileSize
= _filelength (fileno (fpin
));
2690 if (FfsFileSize
>= MAX_FFS_SIZE
) {
2691 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
2694 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
2697 // Read Ffs File header
2699 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
2705 if (FvInfoPtr
->IsPiFvImage
) {
2707 // Check whether this ffs file is vtf file
2709 if (IsVtfFile (&FfsHeader
)) {
2712 // One Fv image can't have two vtf files.
2717 VtfFileSize
= FfsFileSize
;
2722 // Get the alignment of FFS file
2724 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
2725 FfsAlignment
= 1 << FfsAlignment
;
2729 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
2731 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
2733 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
2734 CurrentOffset
-= FfsHeaderSize
;
2739 // Add ffs file size
2741 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
2742 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
2744 CurrentOffset
+= FfsFileSize
;
2748 // Make next ffs file start at QWord Boundry
2750 if (FvInfoPtr
->IsPiFvImage
) {
2751 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
2754 CurrentOffset
+= VtfFileSize
;
2755 DebugMsg (NULL
, 0, 9, "FvImage size", "The caculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
2757 if (FvInfoPtr
->Size
== 0) {
2759 // Update FvInfo data
2761 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
2762 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
2763 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
2764 FvInfoPtr
->FvBlocks
[1].Length
= 0;
2765 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
2769 Error (NULL
, 0, 3000, "Invalid", "the required fv image size 0x%x exceeds the set fv image size 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
2770 return EFI_INVALID_PARAMETER
;
2774 // Set Fv Size Information
2776 mFvTotalSize
= FvInfoPtr
->Size
;
2777 mFvTakenSize
= CurrentOffset
;
2783 FfsRebaseImageRead (
2784 IN VOID
*FileHandle
,
2785 IN UINTN FileOffset
,
2786 IN OUT UINT32
*ReadSize
,
2791 Routine Description:
2793 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2797 FileHandle - The handle to the PE/COFF file
2799 FileOffset - The offset, in bytes, into the file to read
2801 ReadSize - The number of bytes to read from the file starting at FileOffset
2803 Buffer - A pointer to the buffer to read the data into.
2807 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2811 CHAR8
*Destination8
;
2815 Destination8
= Buffer
;
2816 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
2819 *(Destination8
++) = *(Source8
++);
2828 IN EFI_FFS_FILE_HEADER
*FfsFile
,
2833 Routine Description:
2835 This function gets all child FvImages in the input FfsFile, and records
2836 their base address to the parent image.
2839 FvInfo A pointer to FV_INFO struture.
2840 FfsFile A pointer to Ffs file image that may contain FvImage.
2841 XipOffset The offset address to the parent FvImage base.
2845 EFI_SUCCESS Base address of child Fv image is recorded.
2850 EFI_FILE_SECTION_POINTER SubFvSection
;
2851 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
2852 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
2854 for (Index
= 1;; Index
++) {
2858 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
2859 if (EFI_ERROR (Status
)) {
2862 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
2866 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
2867 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
2875 IN OUT FV_INFO
*FvInfo
,
2877 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
2883 Routine Description:
2885 This function determines if a file is XIP and should be rebased. It will
2886 rebase any PE32 sections found in the file using the base address.
2890 FvInfo A pointer to FV_INFO struture.
2891 FileName Ffs File PathName
2892 FfsFile A pointer to Ffs file image.
2893 XipOffset The offset address to use for rebasing the XIP file image.
2894 FvMapFile FvMapFile to record the function address in one Fvimage
2898 EFI_SUCCESS The image was properly rebased.
2899 EFI_INVALID_PARAMETER An input parameter is invalid.
2900 EFI_ABORTED An error occurred while rebasing the input file image.
2901 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2902 EFI_NOT_FOUND No compressed sections could be found.
2907 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
2908 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
2909 EFI_PHYSICAL_ADDRESS XipBase
;
2910 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
2912 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
2913 EFI_FFS_FILE_STATE SavedState
;
2914 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2915 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
2916 UINT8
*MemoryImagePointer
;
2917 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
2918 CHAR8 PeFileName
[MAX_LONG_FILE_PATH
];
2921 UINT8
*PeFileBuffer
;
2924 UINT32 FfsHeaderSize
;
2925 UINT32 CurSecHdrSize
;
2928 MemoryImagePointer
= NULL
;
2929 TEImageHeader
= NULL
;
2931 SectionHeader
= NULL
;
2934 PeFileBuffer
= NULL
;
2937 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
2939 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
2944 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
2946 if (FvInfo
->ForceRebase
== 0) {
2951 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
2954 // We only process files potentially containing PE32 sections.
2956 switch (FfsFile
->Type
) {
2957 case EFI_FV_FILETYPE_SECURITY_CORE
:
2958 case EFI_FV_FILETYPE_PEI_CORE
:
2959 case EFI_FV_FILETYPE_PEIM
:
2960 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2961 case EFI_FV_FILETYPE_DRIVER
:
2962 case EFI_FV_FILETYPE_DXE_CORE
:
2964 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
2966 // Rebase the inside FvImage.
2968 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
2971 // Search PE/TE section in FV sectin.
2978 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
2980 // Rebase each PE32 section
2982 Status
= EFI_SUCCESS
;
2983 for (Index
= 1;; Index
++) {
2987 NewPe32BaseAddress
= 0;
2992 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
2993 if (EFI_ERROR (Status
)) {
2996 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
2999 // Initialize context
3001 memset (&ImageContext
, 0, sizeof (ImageContext
));
3002 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3003 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3004 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3005 if (EFI_ERROR (Status
)) {
3006 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3010 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3011 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3016 // Keep Image Context for PE image in FV
3018 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3021 // Get File PdbPointer
3023 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3026 // Get PeHeader pointer
3028 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3031 // Calculate the PE32 base address, based on file type
3033 switch (FfsFile
->Type
) {
3034 case EFI_FV_FILETYPE_SECURITY_CORE
:
3035 case EFI_FV_FILETYPE_PEI_CORE
:
3036 case EFI_FV_FILETYPE_PEIM
:
3037 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3039 // Check if section-alignment and file-alignment match or not
3041 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3043 // Xip module has the same section alignment and file alignment.
3045 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3049 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3051 if (ImageContext
.RelocationsStripped
) {
3053 // Construct the original efi file Name
3055 strcpy (PeFileName
, FileName
);
3056 Cptr
= PeFileName
+ strlen (PeFileName
);
3057 while (*Cptr
!= '.') {
3061 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3069 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3070 if (PeFile
== NULL
) {
3071 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3072 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3073 //return EFI_ABORTED;
3077 // Get the file size
3079 PeFileSize
= _filelength (fileno (PeFile
));
3080 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3081 if (PeFileBuffer
== NULL
) {
3082 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3083 return EFI_OUT_OF_RESOURCES
;
3088 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3094 // Handle pointer to the original efi image.
3096 ImageContext
.Handle
= PeFileBuffer
;
3097 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3098 if (EFI_ERROR (Status
)) {
3099 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3102 ImageContext
.RelocationsStripped
= FALSE
;
3105 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3108 case EFI_FV_FILETYPE_DRIVER
:
3109 case EFI_FV_FILETYPE_DXE_CORE
:
3111 // Check if section-alignment and file-alignment match or not
3113 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3115 // Xip module has the same section alignment and file alignment.
3117 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3120 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3125 // Not supported file type
3131 // Relocation doesn't exist
3133 if (ImageContext
.RelocationsStripped
) {
3134 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3139 // Relocation exist and rebase
3142 // Load and Relocate Image Data
3144 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3145 if (MemoryImagePointer
== NULL
) {
3146 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3147 return EFI_OUT_OF_RESOURCES
;
3149 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3150 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3152 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3153 if (EFI_ERROR (Status
)) {
3154 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3155 free ((VOID
*) MemoryImagePointer
);
3159 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3160 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3161 if (EFI_ERROR (Status
)) {
3162 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3163 free ((VOID
*) MemoryImagePointer
);
3168 // Copy Relocated data to raw image file.
3170 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3173 sizeof (EFI_IMAGE_FILE_HEADER
) +
3174 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3177 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3179 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3180 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3181 SectionHeader
->SizeOfRawData
3185 free ((VOID
*) MemoryImagePointer
);
3186 MemoryImagePointer
= NULL
;
3187 if (PeFileBuffer
!= NULL
) {
3188 free (PeFileBuffer
);
3189 PeFileBuffer
= NULL
;
3193 // Update Image Base Address
3195 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3196 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3197 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3198 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3200 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3201 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3208 // Now update file checksum
3210 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3211 SavedState
= FfsFile
->State
;
3212 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3214 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3215 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3216 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3218 FfsFile
->State
= SavedState
;
3222 // Get this module function address from ModulePeMapFile and add them into FvMap file
3226 // Default use FileName as map file path
3228 if (PdbPointer
== NULL
) {
3229 PdbPointer
= FileName
;
3232 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3235 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3236 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3237 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3238 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3239 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3242 // Only Peim code may have a TE section
3248 // Now process TE sections
3250 for (Index
= 1;; Index
++) {
3251 NewPe32BaseAddress
= 0;
3256 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3257 if (EFI_ERROR (Status
)) {
3261 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3264 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3267 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3270 // Initialize context, load image info.
3272 memset (&ImageContext
, 0, sizeof (ImageContext
));
3273 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3274 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3275 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3276 if (EFI_ERROR (Status
)) {
3277 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3281 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3282 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3287 // Keep Image Context for TE image in FV
3289 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3292 // Get File PdbPointer
3294 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3297 // Set new rebased address.
3299 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3300 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3303 // if reloc is stripped, try to get the original efi image to get reloc info.
3305 if (ImageContext
.RelocationsStripped
) {
3307 // Construct the original efi file name
3309 strcpy (PeFileName
, FileName
);
3310 Cptr
= PeFileName
+ strlen (PeFileName
);
3311 while (*Cptr
!= '.') {
3316 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3325 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3326 if (PeFile
== NULL
) {
3327 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3328 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3329 //return EFI_ABORTED;
3332 // Get the file size
3334 PeFileSize
= _filelength (fileno (PeFile
));
3335 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3336 if (PeFileBuffer
== NULL
) {
3337 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3338 return EFI_OUT_OF_RESOURCES
;
3343 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3349 // Append reloc section into TeImage
3351 ImageContext
.Handle
= PeFileBuffer
;
3352 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3353 if (EFI_ERROR (Status
)) {
3354 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3357 ImageContext
.RelocationsStripped
= FALSE
;
3361 // Relocation doesn't exist
3363 if (ImageContext
.RelocationsStripped
) {
3364 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3369 // Relocation exist and rebase
3372 // Load and Relocate Image Data
3374 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3375 if (MemoryImagePointer
== NULL
) {
3376 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3377 return EFI_OUT_OF_RESOURCES
;
3379 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3380 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3382 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3383 if (EFI_ERROR (Status
)) {
3384 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3385 free ((VOID
*) MemoryImagePointer
);
3389 // Reloacate TeImage
3391 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3392 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3393 if (EFI_ERROR (Status
)) {
3394 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3395 free ((VOID
*) MemoryImagePointer
);
3400 // Copy the relocated image into raw image file.
3402 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3403 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3404 if (!ImageContext
.IsTeImage
) {
3406 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3407 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3408 SectionHeader
->SizeOfRawData
3412 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3413 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3414 SectionHeader
->SizeOfRawData
3420 // Free the allocated memory resource
3422 free ((VOID
*) MemoryImagePointer
);
3423 MemoryImagePointer
= NULL
;
3424 if (PeFileBuffer
!= NULL
) {
3425 free (PeFileBuffer
);
3426 PeFileBuffer
= NULL
;
3430 // Update Image Base Address
3432 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3435 // Now update file checksum
3437 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3438 SavedState
= FfsFile
->State
;
3439 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3441 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3442 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3443 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3445 FfsFile
->State
= SavedState
;
3448 // Get this module function address from ModulePeMapFile and add them into FvMap file
3452 // Default use FileName as map file path
3454 if (PdbPointer
== NULL
) {
3455 PdbPointer
= FileName
;
3471 FindApResetVectorPosition (
3472 IN MEMORY_FILE
*FvImage
,
3477 Routine Description:
3479 Find the position in this FvImage to place Ap reset vector.
3483 FvImage Memory file for the FV memory image.
3484 Pointer Pointer to pointer to position.
3488 EFI_NOT_FOUND - No satisfied position is found.
3489 EFI_SUCCESS - The suitable position is return.
3493 EFI_FFS_FILE_HEADER
*PadFile
;
3499 for (Index
= 1; ;Index
++) {
3501 // Find Pad File to add ApResetVector info
3503 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3504 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3506 // No Pad file to be found.
3511 // Get Pad file size.
3513 FileLength
= GetFfsFileLength(PadFile
);
3514 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3516 // FixPoint must be align on 0x1000 relative to FvImage Header
3518 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3519 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3521 // FixPoint be larger at the last place of one fv image.
3523 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3528 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3530 // No alignment FixPoint in this Pad File.
3535 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3537 // Find the position to place ApResetVector
3539 *Pointer
= FixPoint
;
3544 return EFI_NOT_FOUND
;
3549 IN MEMORY_FILE
*InfFile
,
3550 OUT CAP_INFO
*CapInfo
3554 Routine Description:
3556 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3560 InfFile Memory file image.
3561 CapInfo Information read from INF file.
3565 EFI_SUCCESS INF file information successfully retrieved.
3566 EFI_ABORTED INF file has an invalid format.
3567 EFI_NOT_FOUND A required string was not found in the INF file.
3570 CHAR8 Value
[MAX_LONG_FILE_PATH
];
3572 UINTN Index
, Number
;
3576 // Initialize Cap info
3578 // memset (CapInfo, 0, sizeof (CAP_INFO));
3582 // Read the Capsule Guid
3584 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
3585 if (Status
== EFI_SUCCESS
) {
3587 // Get the Capsule Guid
3589 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
3590 if (EFI_ERROR (Status
)) {
3591 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3594 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3598 // Read the Capsule Header Size
3600 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
3601 if (Status
== EFI_SUCCESS
) {
3602 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3603 if (EFI_ERROR (Status
)) {
3604 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3607 CapInfo
->HeaderSize
= (UINT32
) Value64
;
3608 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3612 // Read the Capsule Flag
3614 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
3615 if (Status
== EFI_SUCCESS
) {
3616 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
3617 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
3618 if (strstr (Value
, "InitiateReset") != NULL
) {
3619 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3621 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
3622 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
3623 if (strstr (Value
, "InitiateReset") != NULL
) {
3624 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3627 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
3630 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
3633 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
3634 if (Status
== EFI_SUCCESS
) {
3635 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3636 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
3637 Error (NULL
, 0, 2000, "Invalid parameter",
3638 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
3639 EFI_OEM_CAPSULE_FLAGS_STRING
);
3642 CapInfo
->Flags
|= Value64
;
3643 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
3647 // Read Capsule File name
3649 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
3650 if (Status
== EFI_SUCCESS
) {
3652 // Get output file name
3654 strcpy (CapInfo
->CapName
, Value
);
3658 // Read the Capsule FileImage
3661 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
3662 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
3666 // Read the capsule file name
3668 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
3670 if (Status
== EFI_SUCCESS
) {
3674 strcpy (CapInfo
->CapFiles
[Index
], Value
);
3675 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
3682 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
3690 IN CHAR8
*InfFileImage
,
3691 IN UINTN InfFileSize
,
3692 IN CHAR8
*CapFileName
3696 Routine Description:
3698 This is the main function which will be called from application to create UEFI Capsule image.
3702 InfFileImage Buffer containing the INF file contents.
3703 InfFileSize Size of the contents of the InfFileImage buffer.
3704 CapFileName Requested name for the Cap file.
3708 EFI_SUCCESS Function completed successfully.
3709 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3710 EFI_ABORTED Error encountered.
3711 EFI_INVALID_PARAMETER A required parameter was NULL.
3717 EFI_CAPSULE_HEADER
*CapsuleHeader
;
3718 MEMORY_FILE InfMemoryFile
;
3724 if (InfFileImage
!= NULL
) {
3726 // Initialize file structures
3728 InfMemoryFile
.FileImage
= InfFileImage
;
3729 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
3730 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
3733 // Parse the Cap inf file for header information
3735 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
3736 if (Status
!= EFI_SUCCESS
) {
3741 if (mCapDataInfo
.HeaderSize
== 0) {
3743 // make header size align 16 bytes.
3745 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
3746 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
3749 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
3750 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3751 return EFI_INVALID_PARAMETER
;
3754 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
3755 CapFileName
= mCapDataInfo
.CapName
;
3758 if (CapFileName
== NULL
) {
3759 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
3760 return EFI_INVALID_PARAMETER
;
3764 // Set Default Capsule Guid value
3766 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
3767 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
3770 // Calculate the size of capsule image.
3774 CapSize
= mCapDataInfo
.HeaderSize
;
3775 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3776 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
3778 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3781 FileSize
= _filelength (fileno (fpin
));
3782 CapSize
+= FileSize
;
3788 // Allocate buffer for capsule image.
3790 CapBuffer
= (UINT8
*) malloc (CapSize
);
3791 if (CapBuffer
== NULL
) {
3792 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3793 return EFI_OUT_OF_RESOURCES
;
3797 // Initialize the capsule header to zero
3799 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
3802 // create capsule header and get capsule body
3804 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
3805 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
3806 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
3807 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
3808 CapsuleHeader
->CapsuleImageSize
= CapSize
;
3812 CapSize
= CapsuleHeader
->HeaderSize
;
3813 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3814 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
3816 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3820 FileSize
= _filelength (fileno (fpin
));
3821 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
3824 CapSize
+= FileSize
;
3828 // write capsule data into the output file
3830 fpout
= fopen (LongFilePath (CapFileName
), "wb");
3831 if (fpout
== NULL
) {
3832 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
3837 fwrite (CapBuffer
, 1, CapSize
, fpout
);
3840 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);