3 Copyright (c) 2004 - 2013, 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_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) {
461 // 8 byte alignment, mini alignment requirement for FFS file.
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_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 (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 (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_PATH
];
2082 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2083 FILE *FvExtHeaderFile
;
2085 CHAR8 FvReportName
[_MAX_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 (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 mFvDataInfo
.IsPiFvImage
= TRUE
;
2220 // FvMap file to log the function address of all modules in one Fvimage
2222 if (MapFileName
!= NULL
) {
2223 strcpy (FvMapName
, MapFileName
);
2225 strcpy (FvMapName
, FvFileName
);
2226 strcat (FvMapName
, ".map");
2228 VerboseMsg ("FV Map file name is %s", FvMapName
);
2231 // FvReport file to log the FV information in one Fvimage
2233 strcpy (FvReportName
, FvFileName
);
2234 strcat (FvReportName
, ".txt");
2237 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2238 // And Update mFvDataInfo data.
2240 Status
= CalculateFvSize (&mFvDataInfo
);
2241 if (EFI_ERROR (Status
)) {
2244 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2247 // support fv image and empty fv image
2249 FvImageSize
= mFvDataInfo
.Size
;
2252 // Allocate the FV, assure FvImage Header 8 byte alignment
2254 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2255 if (FvBufferHeader
== NULL
) {
2256 return EFI_OUT_OF_RESOURCES
;
2258 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2261 // Initialize the FV to the erase polarity
2263 if (mFvDataInfo
.FvAttributes
== 0) {
2265 // Set Default Fv Attribute
2267 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2269 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2270 memset (FvImage
, -1, FvImageSize
);
2272 memset (FvImage
, 0, FvImageSize
);
2276 // Initialize FV header
2278 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2281 // Initialize the zero vector to all zeros.
2283 memset (FvHeader
->ZeroVector
, 0, 16);
2286 // Copy the Fv file system GUID
2288 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2290 FvHeader
->FvLength
= FvImageSize
;
2291 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2292 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2293 FvHeader
->Revision
= EFI_FVH_REVISION
;
2294 FvHeader
->ExtHeaderOffset
= 0;
2295 FvHeader
->Reserved
[0] = 0;
2298 // Copy firmware block map
2300 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2301 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2302 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2306 // Add block map terminator
2308 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2309 FvHeader
->BlockMap
[Index
].Length
= 0;
2312 // Complete the header
2314 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2315 FvHeader
->Checksum
= 0;
2316 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2319 // If there is no FFS file, generate one empty FV
2321 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2326 // Initialize our "file" view of the buffer
2328 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2329 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2330 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2333 // Initialize the FV library.
2335 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2338 // Initialize the VTF file address.
2340 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2345 FvMapFile
= fopen (FvMapName
, "w");
2346 if (FvMapFile
== NULL
) {
2347 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2352 // Open FvReport file
2354 FvReportFile
= fopen(FvReportName
, "w");
2355 if (FvReportFile
== NULL
) {
2356 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2360 // record FV size information into FvMap file.
2362 if (mFvTotalSize
!= 0) {
2363 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2364 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2366 if (mFvTakenSize
!= 0) {
2367 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2368 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2370 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2371 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2372 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2376 // record FV size information to FvReportFile.
2378 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2379 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2382 // Add PI FV extension header
2384 if (FvExtHeader
!= NULL
) {
2386 // Add FV Extended Header contents to the FV as a PAD file
2388 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2391 // Fv Extension header change update Fv Header Check sum
2393 FvHeader
->Checksum
= 0;
2394 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2400 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2404 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2407 // Exit if error detected while adding the file
2409 if (EFI_ERROR (Status
)) {
2415 // If there is a VTF file, some special actions need to occur.
2417 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2419 // Pad from the end of the last file to the beginning of the VTF file.
2420 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2422 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2423 if (EFI_ERROR (Status
)) {
2424 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2429 // Update reset vector (SALE_ENTRY for IPF)
2430 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2431 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2432 // reset vector. If the PEI Core is found, the VTF file will probably get
2433 // corrupted by updating the entry point.
2435 if ((mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2436 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2437 if (EFI_ERROR(Status
)) {
2438 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2441 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2447 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2448 if (EFI_ERROR (Status
)) {
2449 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2454 // Update Checksum for FvHeader
2456 FvHeader
->Checksum
= 0;
2457 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2461 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2463 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2464 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2465 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2467 // Update Checksum for FvHeader
2469 FvHeader
->Checksum
= 0;
2470 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2474 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2476 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2477 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2478 FvHeader
->Checksum
= 0;
2479 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2486 FvFile
= fopen (FvFileName
, "wb");
2487 if (FvFile
== NULL
) {
2488 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2489 Status
= EFI_ABORTED
;
2493 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2494 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2495 Status
= EFI_ABORTED
;
2500 if (FvBufferHeader
!= NULL
) {
2501 free (FvBufferHeader
);
2504 if (FvExtHeader
!= NULL
) {
2508 if (FvFile
!= NULL
) {
2513 if (FvMapFile
!= NULL
) {
2518 if (FvReportFile
!= NULL
) {
2519 fflush (FvReportFile
);
2520 fclose (FvReportFile
);
2526 UpdatePeiCoreEntryInFit (
2527 IN FIT_TABLE
*FitTablePtr
,
2528 IN UINT64 PeiCorePhysicalAddress
2532 Routine Description:
2534 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2539 FitTablePtr - The pointer of FIT_TABLE.
2540 PeiCorePhysicalAddress - The address of Pei Core entry.
2544 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2545 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2549 FIT_TABLE
*TmpFitPtr
;
2551 UINTN NumFitComponents
;
2553 TmpFitPtr
= FitTablePtr
;
2554 NumFitComponents
= TmpFitPtr
->CompSize
;
2556 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2557 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2558 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2565 return EFI_NOT_FOUND
;
2570 IN FIT_TABLE
*FitTablePtr
2574 Routine Description:
2576 This function is used to update the checksum for FIT.
2581 FitTablePtr - The pointer of FIT_TABLE.
2589 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2590 FitTablePtr
->CheckSum
= 0;
2591 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2600 Routine Description:
2601 Calculate the FV size and Update Fv Size based on the actual FFS files.
2602 And Update FvInfo data.
2605 FvInfoPtr - The pointer to FV_INFO structure.
2608 EFI_ABORTED - Ffs Image Error
2609 EFI_SUCCESS - Successfully update FvSize
2612 UINTN CurrentOffset
;
2616 UINTN FvExtendHeaderSize
;
2617 UINT32 FfsAlignment
;
2618 UINT32 FfsHeaderSize
;
2619 EFI_FFS_FILE_HEADER FfsHeader
;
2620 BOOLEAN VtfFileFlag
;
2623 FvExtendHeaderSize
= 0;
2625 VtfFileFlag
= FALSE
;
2630 // Compute size for easy access later
2632 FvInfoPtr
->Size
= 0;
2633 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
2634 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
2638 // Caculate the required sizes for all FFS files.
2640 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
2642 for (Index
= 1;; Index
++) {
2643 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
2644 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
2650 // Calculate PI extension header
2652 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
2653 fpin
= fopen (mFvDataInfo
.FvExtHeaderFile
, "rb");
2655 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2658 FvExtendHeaderSize
= _filelength (fileno (fpin
));
2660 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
2661 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
2664 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
2666 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2667 } else if (mFvDataInfo
.FvNameGuidSet
) {
2668 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2669 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2673 // Accumlate every FFS file size.
2675 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
2680 fpin
= fopen (FvInfoPtr
->FvFiles
[Index
], "rb");
2682 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
2686 // Get the file size
2688 FfsFileSize
= _filelength (fileno (fpin
));
2689 if (FfsFileSize
>= MAX_FFS_SIZE
) {
2690 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
2693 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
2696 // Read Ffs File header
2698 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
2704 if (FvInfoPtr
->IsPiFvImage
) {
2706 // Check whether this ffs file is vtf file
2708 if (IsVtfFile (&FfsHeader
)) {
2711 // One Fv image can't have two vtf files.
2716 VtfFileSize
= FfsFileSize
;
2721 // Get the alignment of FFS file
2723 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
2724 FfsAlignment
= 1 << FfsAlignment
;
2728 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
2730 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
2732 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
2733 CurrentOffset
-= FfsHeaderSize
;
2738 // Add ffs file size
2740 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
2741 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
2743 CurrentOffset
+= FfsFileSize
;
2747 // Make next ffs file start at QWord Boundry
2749 if (FvInfoPtr
->IsPiFvImage
) {
2750 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
2753 CurrentOffset
+= VtfFileSize
;
2754 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
);
2756 if (FvInfoPtr
->Size
== 0) {
2758 // Update FvInfo data
2760 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
2761 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
2762 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
2763 FvInfoPtr
->FvBlocks
[1].Length
= 0;
2764 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
2768 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
);
2769 return EFI_INVALID_PARAMETER
;
2773 // Set Fv Size Information
2775 mFvTotalSize
= FvInfoPtr
->Size
;
2776 mFvTakenSize
= CurrentOffset
;
2782 FfsRebaseImageRead (
2783 IN VOID
*FileHandle
,
2784 IN UINTN FileOffset
,
2785 IN OUT UINT32
*ReadSize
,
2790 Routine Description:
2792 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2796 FileHandle - The handle to the PE/COFF file
2798 FileOffset - The offset, in bytes, into the file to read
2800 ReadSize - The number of bytes to read from the file starting at FileOffset
2802 Buffer - A pointer to the buffer to read the data into.
2806 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2810 CHAR8
*Destination8
;
2814 Destination8
= Buffer
;
2815 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
2818 *(Destination8
++) = *(Source8
++);
2827 IN EFI_FFS_FILE_HEADER
*FfsFile
,
2832 Routine Description:
2834 This function gets all child FvImages in the input FfsFile, and records
2835 their base address to the parent image.
2838 FvInfo A pointer to FV_INFO struture.
2839 FfsFile A pointer to Ffs file image that may contain FvImage.
2840 XipOffset The offset address to the parent FvImage base.
2844 EFI_SUCCESS Base address of child Fv image is recorded.
2849 EFI_FILE_SECTION_POINTER SubFvSection
;
2850 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
2851 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
2853 for (Index
= 1;; Index
++) {
2857 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
2858 if (EFI_ERROR (Status
)) {
2861 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
2865 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
2866 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
2874 IN OUT FV_INFO
*FvInfo
,
2876 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
2882 Routine Description:
2884 This function determines if a file is XIP and should be rebased. It will
2885 rebase any PE32 sections found in the file using the base address.
2889 FvInfo A pointer to FV_INFO struture.
2890 FileName Ffs File PathName
2891 FfsFile A pointer to Ffs file image.
2892 XipOffset The offset address to use for rebasing the XIP file image.
2893 FvMapFile FvMapFile to record the function address in one Fvimage
2897 EFI_SUCCESS The image was properly rebased.
2898 EFI_INVALID_PARAMETER An input parameter is invalid.
2899 EFI_ABORTED An error occurred while rebasing the input file image.
2900 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2901 EFI_NOT_FOUND No compressed sections could be found.
2906 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
2907 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
2908 EFI_PHYSICAL_ADDRESS XipBase
;
2909 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
2911 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
2912 EFI_FFS_FILE_STATE SavedState
;
2913 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2914 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
2915 UINT8
*MemoryImagePointer
;
2916 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
2917 CHAR8 PeFileName
[_MAX_PATH
];
2920 UINT8
*PeFileBuffer
;
2923 UINT32 FfsHeaderSize
;
2924 UINT32 CurSecHdrSize
;
2927 MemoryImagePointer
= NULL
;
2928 TEImageHeader
= NULL
;
2930 SectionHeader
= NULL
;
2933 PeFileBuffer
= NULL
;
2936 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
2938 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
2943 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
2945 if (FvInfo
->ForceRebase
== 0) {
2950 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
2953 // We only process files potentially containing PE32 sections.
2955 switch (FfsFile
->Type
) {
2956 case EFI_FV_FILETYPE_SECURITY_CORE
:
2957 case EFI_FV_FILETYPE_PEI_CORE
:
2958 case EFI_FV_FILETYPE_PEIM
:
2959 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2960 case EFI_FV_FILETYPE_DRIVER
:
2961 case EFI_FV_FILETYPE_DXE_CORE
:
2963 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
2965 // Rebase the inside FvImage.
2967 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
2970 // Search PE/TE section in FV sectin.
2977 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
2979 // Rebase each PE32 section
2981 Status
= EFI_SUCCESS
;
2982 for (Index
= 1;; Index
++) {
2986 NewPe32BaseAddress
= 0;
2991 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
2992 if (EFI_ERROR (Status
)) {
2995 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
2998 // Initialize context
3000 memset (&ImageContext
, 0, sizeof (ImageContext
));
3001 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3002 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3003 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3004 if (EFI_ERROR (Status
)) {
3005 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3009 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3010 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3015 // Keep Image Context for PE image in FV
3017 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3020 // Get File PdbPointer
3022 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3025 // Get PeHeader pointer
3027 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3030 // Calculate the PE32 base address, based on file type
3032 switch (FfsFile
->Type
) {
3033 case EFI_FV_FILETYPE_SECURITY_CORE
:
3034 case EFI_FV_FILETYPE_PEI_CORE
:
3035 case EFI_FV_FILETYPE_PEIM
:
3036 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3038 // Check if section-alignment and file-alignment match or not
3040 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3042 // Xip module has the same section alignment and file alignment.
3044 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3048 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3050 if (ImageContext
.RelocationsStripped
) {
3052 // Construct the original efi file Name
3054 strcpy (PeFileName
, FileName
);
3055 Cptr
= PeFileName
+ strlen (PeFileName
);
3056 while (*Cptr
!= '.') {
3060 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3068 PeFile
= fopen (PeFileName
, "rb");
3069 if (PeFile
== NULL
) {
3070 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3071 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3072 //return EFI_ABORTED;
3076 // Get the file size
3078 PeFileSize
= _filelength (fileno (PeFile
));
3079 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3080 if (PeFileBuffer
== NULL
) {
3081 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3082 return EFI_OUT_OF_RESOURCES
;
3087 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3093 // Handle pointer to the original efi image.
3095 ImageContext
.Handle
= PeFileBuffer
;
3096 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3097 if (EFI_ERROR (Status
)) {
3098 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3101 ImageContext
.RelocationsStripped
= FALSE
;
3104 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3107 case EFI_FV_FILETYPE_DRIVER
:
3108 case EFI_FV_FILETYPE_DXE_CORE
:
3110 // Check if section-alignment and file-alignment match or not
3112 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3114 // Xip module has the same section alignment and file alignment.
3116 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3119 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3124 // Not supported file type
3130 // Relocation doesn't exist
3132 if (ImageContext
.RelocationsStripped
) {
3133 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3138 // Relocation exist and rebase
3141 // Load and Relocate Image Data
3143 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3144 if (MemoryImagePointer
== NULL
) {
3145 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3146 return EFI_OUT_OF_RESOURCES
;
3148 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3149 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3151 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3152 if (EFI_ERROR (Status
)) {
3153 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3154 free ((VOID
*) MemoryImagePointer
);
3158 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3159 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3160 if (EFI_ERROR (Status
)) {
3161 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3162 free ((VOID
*) MemoryImagePointer
);
3167 // Copy Relocated data to raw image file.
3169 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3172 sizeof (EFI_IMAGE_FILE_HEADER
) +
3173 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3176 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3178 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3179 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3180 SectionHeader
->SizeOfRawData
3184 free ((VOID
*) MemoryImagePointer
);
3185 MemoryImagePointer
= NULL
;
3186 if (PeFileBuffer
!= NULL
) {
3187 free (PeFileBuffer
);
3188 PeFileBuffer
= NULL
;
3192 // Update Image Base Address
3194 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3195 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3196 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3197 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3199 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3200 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3207 // Now update file checksum
3209 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3210 SavedState
= FfsFile
->State
;
3211 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3213 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3214 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3215 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3217 FfsFile
->State
= SavedState
;
3221 // Get this module function address from ModulePeMapFile and add them into FvMap file
3225 // Default use FileName as map file path
3227 if (PdbPointer
== NULL
) {
3228 PdbPointer
= FileName
;
3231 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3234 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3235 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3236 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3237 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3238 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3241 // Only Peim code may have a TE section
3247 // Now process TE sections
3249 for (Index
= 1;; Index
++) {
3250 NewPe32BaseAddress
= 0;
3255 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3256 if (EFI_ERROR (Status
)) {
3260 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3263 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3266 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3269 // Initialize context, load image info.
3271 memset (&ImageContext
, 0, sizeof (ImageContext
));
3272 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3273 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3274 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3275 if (EFI_ERROR (Status
)) {
3276 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3280 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3281 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3286 // Keep Image Context for TE image in FV
3288 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3291 // Get File PdbPointer
3293 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3296 // Set new rebased address.
3298 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3299 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3302 // if reloc is stripped, try to get the original efi image to get reloc info.
3304 if (ImageContext
.RelocationsStripped
) {
3306 // Construct the original efi file name
3308 strcpy (PeFileName
, FileName
);
3309 Cptr
= PeFileName
+ strlen (PeFileName
);
3310 while (*Cptr
!= '.') {
3315 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3324 PeFile
= fopen (PeFileName
, "rb");
3325 if (PeFile
== NULL
) {
3326 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3327 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3328 //return EFI_ABORTED;
3331 // Get the file size
3333 PeFileSize
= _filelength (fileno (PeFile
));
3334 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3335 if (PeFileBuffer
== NULL
) {
3336 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3337 return EFI_OUT_OF_RESOURCES
;
3342 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3348 // Append reloc section into TeImage
3350 ImageContext
.Handle
= PeFileBuffer
;
3351 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3352 if (EFI_ERROR (Status
)) {
3353 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3356 ImageContext
.RelocationsStripped
= FALSE
;
3360 // Relocation doesn't exist
3362 if (ImageContext
.RelocationsStripped
) {
3363 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3368 // Relocation exist and rebase
3371 // Load and Relocate Image Data
3373 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3374 if (MemoryImagePointer
== NULL
) {
3375 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3376 return EFI_OUT_OF_RESOURCES
;
3378 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3379 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3381 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3382 if (EFI_ERROR (Status
)) {
3383 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3384 free ((VOID
*) MemoryImagePointer
);
3388 // Reloacate TeImage
3390 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3391 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3392 if (EFI_ERROR (Status
)) {
3393 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3394 free ((VOID
*) MemoryImagePointer
);
3399 // Copy the relocated image into raw image file.
3401 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3402 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3403 if (!ImageContext
.IsTeImage
) {
3405 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3406 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3407 SectionHeader
->SizeOfRawData
3411 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3412 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3413 SectionHeader
->SizeOfRawData
3419 // Free the allocated memory resource
3421 free ((VOID
*) MemoryImagePointer
);
3422 MemoryImagePointer
= NULL
;
3423 if (PeFileBuffer
!= NULL
) {
3424 free (PeFileBuffer
);
3425 PeFileBuffer
= NULL
;
3429 // Update Image Base Address
3431 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3434 // Now update file checksum
3436 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3437 SavedState
= FfsFile
->State
;
3438 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3440 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3441 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3442 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3444 FfsFile
->State
= SavedState
;
3447 // Get this module function address from ModulePeMapFile and add them into FvMap file
3451 // Default use FileName as map file path
3453 if (PdbPointer
== NULL
) {
3454 PdbPointer
= FileName
;
3470 FindApResetVectorPosition (
3471 IN MEMORY_FILE
*FvImage
,
3476 Routine Description:
3478 Find the position in this FvImage to place Ap reset vector.
3482 FvImage Memory file for the FV memory image.
3483 Pointer Pointer to pointer to position.
3487 EFI_NOT_FOUND - No satisfied position is found.
3488 EFI_SUCCESS - The suitable position is return.
3492 EFI_FFS_FILE_HEADER
*PadFile
;
3498 for (Index
= 1; ;Index
++) {
3500 // Find Pad File to add ApResetVector info
3502 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3503 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3505 // No Pad file to be found.
3510 // Get Pad file size.
3512 FileLength
= GetFfsFileLength(PadFile
);
3513 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3515 // FixPoint must be align on 0x1000 relative to FvImage Header
3517 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3518 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3520 // FixPoint be larger at the last place of one fv image.
3522 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3527 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3529 // No alignment FixPoint in this Pad File.
3534 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3536 // Find the position to place ApResetVector
3538 *Pointer
= FixPoint
;
3543 return EFI_NOT_FOUND
;
3548 IN MEMORY_FILE
*InfFile
,
3549 OUT CAP_INFO
*CapInfo
3553 Routine Description:
3555 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3559 InfFile Memory file image.
3560 CapInfo Information read from INF file.
3564 EFI_SUCCESS INF file information successfully retrieved.
3565 EFI_ABORTED INF file has an invalid format.
3566 EFI_NOT_FOUND A required string was not found in the INF file.
3569 CHAR8 Value
[_MAX_PATH
];
3571 UINTN Index
, Number
;
3575 // Initialize Cap info
3577 // memset (CapInfo, 0, sizeof (CAP_INFO));
3581 // Read the Capsule Guid
3583 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
3584 if (Status
== EFI_SUCCESS
) {
3586 // Get the Capsule Guid
3588 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
3589 if (EFI_ERROR (Status
)) {
3590 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3593 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3597 // Read the Capsule Header Size
3599 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
3600 if (Status
== EFI_SUCCESS
) {
3601 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3602 if (EFI_ERROR (Status
)) {
3603 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3606 CapInfo
->HeaderSize
= (UINT32
) Value64
;
3607 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3611 // Read the Capsule Flag
3613 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
3614 if (Status
== EFI_SUCCESS
) {
3615 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
3616 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
3617 if (strstr (Value
, "InitiateReset") != NULL
) {
3618 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3620 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
3621 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
3622 if (strstr (Value
, "InitiateReset") != NULL
) {
3623 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3626 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
3629 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
3632 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
3633 if (Status
== EFI_SUCCESS
) {
3634 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3635 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
3636 Error (NULL
, 0, 2000, "Invalid parameter",
3637 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
3638 EFI_OEM_CAPSULE_FLAGS_STRING
);
3641 CapInfo
->Flags
|= Value64
;
3642 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
3646 // Read Capsule File name
3648 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
3649 if (Status
== EFI_SUCCESS
) {
3651 // Get output file name
3653 strcpy (CapInfo
->CapName
, Value
);
3657 // Read the Capsule FileImage
3660 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
3661 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
3665 // Read the capsule file name
3667 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
3669 if (Status
== EFI_SUCCESS
) {
3673 strcpy (CapInfo
->CapFiles
[Index
], Value
);
3674 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
3681 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
3689 IN CHAR8
*InfFileImage
,
3690 IN UINTN InfFileSize
,
3691 IN CHAR8
*CapFileName
3695 Routine Description:
3697 This is the main function which will be called from application to create UEFI Capsule image.
3701 InfFileImage Buffer containing the INF file contents.
3702 InfFileSize Size of the contents of the InfFileImage buffer.
3703 CapFileName Requested name for the Cap file.
3707 EFI_SUCCESS Function completed successfully.
3708 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3709 EFI_ABORTED Error encountered.
3710 EFI_INVALID_PARAMETER A required parameter was NULL.
3716 EFI_CAPSULE_HEADER
*CapsuleHeader
;
3717 MEMORY_FILE InfMemoryFile
;
3723 if (InfFileImage
!= NULL
) {
3725 // Initialize file structures
3727 InfMemoryFile
.FileImage
= InfFileImage
;
3728 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
3729 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
3732 // Parse the Cap inf file for header information
3734 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
3735 if (Status
!= EFI_SUCCESS
) {
3740 if (mCapDataInfo
.HeaderSize
== 0) {
3742 // make header size align 16 bytes.
3744 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
3745 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
3748 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
3749 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3750 return EFI_INVALID_PARAMETER
;
3753 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
3754 CapFileName
= mCapDataInfo
.CapName
;
3757 if (CapFileName
== NULL
) {
3758 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
3759 return EFI_INVALID_PARAMETER
;
3763 // Set Default Capsule Guid value
3765 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
3766 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
3769 // Calculate the size of capsule image.
3773 CapSize
= mCapDataInfo
.HeaderSize
;
3774 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3775 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3777 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3780 FileSize
= _filelength (fileno (fpin
));
3781 CapSize
+= FileSize
;
3787 // Allocate buffer for capsule image.
3789 CapBuffer
= (UINT8
*) malloc (CapSize
);
3790 if (CapBuffer
== NULL
) {
3791 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3792 return EFI_OUT_OF_RESOURCES
;
3796 // Initialize the capsule header to zero
3798 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
3801 // create capsule header and get capsule body
3803 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
3804 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
3805 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
3806 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
3807 CapsuleHeader
->CapsuleImageSize
= CapSize
;
3811 CapSize
= CapsuleHeader
->HeaderSize
;
3812 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3813 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3815 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3819 FileSize
= _filelength (fileno (fpin
));
3820 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
3823 CapSize
+= FileSize
;
3827 // write capsule data into the output file
3829 fpout
= fopen (CapFileName
, "wb");
3830 if (fpout
== NULL
) {
3831 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
3836 fwrite (CapBuffer
, 1, CapSize
, fpout
);
3839 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);