2 This file contains the internal functions required to generate a Firmware Volume.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2016 HP Development Company, L.P.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #if defined(__FreeBSD__)
17 #elif defined(__GNUC__)
18 #include <uuid/uuid.h>
29 #include <Guid/FfsSectionAlignmentPadding.h>
31 #include "WinNtInclude.h"
32 #include "GenFvInternalLib.h"
34 #include "PeCoffLib.h"
36 #define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION 0xEB000000
37 #define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION 0x14000000
40 STATIC UINT32 MaxFfsAlignment
= 0;
41 BOOLEAN VtfFileFlag
= FALSE
;
43 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
44 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
45 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
46 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
47 EFI_GUID mEfiFfsSectionAlignmentPaddingGuid
= EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID
;
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 startup IPI for HT-enabled systems.
109 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
111 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
113 // EA D0 FF 00 F0 ; far jmp F000:FFD0
114 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
115 // 0, 0 ; Checksum Padding
135 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
137 // EB CE ; jmp short ($-0x30)
138 // ; (from offset 0x0 to offset 0xFFD0)
139 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
140 // 0, 0 ; Checksum Padding
161 CAP_INFO mCapDataInfo
;
162 BOOLEAN mIsLargeFfs
= FALSE
;
164 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
165 UINT32 mFvBaseAddressNumber
= 0;
169 IN MEMORY_FILE
*InfFile
,
176 This function parses a FV.INF file and copies info into a FV_INFO structure.
180 InfFile Memory file image.
181 FvInfo Information read from INF file.
185 EFI_SUCCESS INF file information successfully retrieved.
186 EFI_ABORTED INF file has an invalid format.
187 EFI_NOT_FOUND A required string was not found in the INF file.
190 CHAR8 Value
[MAX_LONG_FILE_PATH
];
198 // Read the FV base address
200 if (!mFvDataInfo
.BaseAddressSet
) {
201 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
202 if (Status
== EFI_SUCCESS
) {
204 // Get the base address
206 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
207 if (EFI_ERROR (Status
)) {
208 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
211 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
213 FvInfo
->BaseAddress
= Value64
;
214 FvInfo
->BaseAddressSet
= TRUE
;
219 // Read the FV File System Guid
221 if (!FvInfo
->FvFileSystemGuidSet
) {
222 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
223 if (Status
== EFI_SUCCESS
) {
225 // Get the guid value
227 Status
= StringToGuid (Value
, &GuidValue
);
228 if (EFI_ERROR (Status
)) {
229 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
232 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
233 FvInfo
->FvFileSystemGuidSet
= TRUE
;
238 // Read the FV Extension Header File Name
240 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
241 if (Status
== EFI_SUCCESS
) {
242 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
246 // Read the FV file name
248 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
249 if (Status
== EFI_SUCCESS
) {
251 // copy the file name
253 strcpy (FvInfo
->FvName
, Value
);
259 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
260 if ((mFvbAttributeName
[Index
] != NULL
) && \
261 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
262 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
263 FvInfo
->FvAttributes
|= 1 << Index
;
264 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
265 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
274 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
275 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
276 if (strcmp (Value
, TRUE_STRING
) == 0) {
277 FvInfo
->FvAttributes
|= Index
<< 16;
278 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
285 // Read weak alignment flag
287 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_WEAK_ALIGNMENT_STRING
, 0, Value
);
288 if (Status
== EFI_SUCCESS
) {
289 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
290 FvInfo
->FvAttributes
|= EFI_FVB2_WEAK_ALIGNMENT
;
291 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
292 Error (NULL
, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
300 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
301 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
305 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
307 if (Status
== EFI_SUCCESS
) {
309 // Update the size of block
311 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
312 if (EFI_ERROR (Status
)) {
313 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
317 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
318 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
321 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
322 // and should return an error.
324 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
325 if (!EFI_ERROR (Status
)) {
326 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
337 // Read blocks number
339 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
341 if (Status
== EFI_SUCCESS
) {
343 // Update the number of blocks
345 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
346 if (EFI_ERROR (Status
)) {
347 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
351 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
352 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
358 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
366 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
367 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
372 for (Index
= 0; Number
+ 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) {
462 //if bit 1 have set, 128K byte alignment
464 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
474 //if bit 1 have set, 256K byte alignment
476 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
485 // 128 byte alignment
486 //if bit 1 have set, 512K byte alignment
488 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
497 // 512 byte alignment
498 //if bit 1 have set, 1M byte alignment
500 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
510 //if bit 1 have set, 2M byte alignment
512 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
522 //if bit 1 have set, 4M byte alignment
524 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
533 // 32K byte alignment
534 //if bit 1 have set , 8M byte alignment
536 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
545 // 64K byte alignment
546 //if bit 1 have set, 16M alignment
548 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
564 IN OUT MEMORY_FILE
*FvImage
,
565 IN UINT32 DataAlignment
,
567 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
,
568 IN UINT32 NextFfsSize
574 This function adds a pad file to the FV image if it required to align the
575 data of the next file.
579 FvImage The memory image of the FV to add it to.
580 The current offset must be valid.
581 DataAlignment The data alignment of the next FFS file.
582 FvEnd End of the empty data in FvImage.
583 ExtHeader PI FvExtHeader Optional
587 EFI_SUCCESS The function completed successfully.
588 EFI_INVALID_PARAMETER One of the input parameters was invalid.
589 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
594 EFI_FFS_FILE_HEADER
*PadFile
;
596 UINT32 NextFfsHeaderSize
;
597 UINT32 CurFfsHeaderSize
;
601 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
603 // Verify input parameters.
605 if (FvImage
== NULL
) {
606 return EFI_INVALID_PARAMETER
;
610 // Calculate the pad file size
614 // Append extension header size
616 if (ExtHeader
!= NULL
) {
617 PadFileSize
= ExtHeader
->ExtHeaderSize
;
618 if (PadFileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
619 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
621 PadFileSize
+= CurFfsHeaderSize
;
623 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
624 if (NextFfsSize
>= MAX_FFS_SIZE
) {
625 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
628 // Check if a pad file is necessary
630 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ NextFfsHeaderSize
) % DataAlignment
== 0) {
633 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
) + NextFfsHeaderSize
;
635 // Add whatever it takes to get to the next aligned address
637 while ((PadFileSize
% DataAlignment
) != 0) {
641 // Subtract the next file header size
643 PadFileSize
-= NextFfsHeaderSize
;
645 // Subtract the starting offset to get size
647 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
651 // Verify that we have enough space for the file header
653 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
654 return EFI_OUT_OF_RESOURCES
;
658 // Write pad file header
660 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
663 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
665 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
666 PadFile
->Attributes
= 0;
669 // Write pad file size (calculated size minus next file header size)
671 if (PadFileSize
>= MAX_FFS_SIZE
) {
672 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
673 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= PadFileSize
;
674 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
676 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
677 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
678 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
682 // Fill in checksums and state, they must be 0 for checksumming.
684 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
685 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
687 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, CurFfsHeaderSize
);
688 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
690 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
692 (EFI_FFS_FILE_HEADER
*) PadFile
,
693 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
697 // Update the current FV pointer
699 FvImage
->CurrentFilePointer
+= PadFileSize
;
701 if (ExtHeader
!= NULL
) {
703 // Copy Fv Extension Header and Set Fv Extension header offset
705 if (ExtHeader
->ExtHeaderSize
> sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
)) {
706 for (Index
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
); Index
< ExtHeader
->ExtHeaderSize
;) {
707 if (((EFI_FIRMWARE_VOLUME_EXT_ENTRY
*)((UINT8
*)ExtHeader
+ Index
))-> ExtEntryType
== EFI_FV_EXT_TYPE_USED_SIZE_TYPE
) {
709 ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE
*)((UINT8
*)ExtHeader
+ Index
))->UsedSize
= mFvTotalSize
;
711 ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE
*)((UINT8
*)ExtHeader
+ Index
))->UsedSize
= mFvTakenSize
;
715 Index
+= ((EFI_FIRMWARE_VOLUME_EXT_ENTRY
*)((UINT8
*)ExtHeader
+ Index
))-> ExtEntrySize
;
718 memcpy ((UINT8
*)PadFile
+ CurFfsHeaderSize
, ExtHeader
, ExtHeader
->ExtHeaderSize
);
719 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) ((UINT8
*)PadFile
+ CurFfsHeaderSize
) - (UINTN
) FvImage
->FileImage
);
721 // Make next file start at QWord Boundary
723 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
724 FvImage
->CurrentFilePointer
++;
733 IN EFI_FFS_FILE_HEADER
*FileBuffer
739 This function checks the header to validate if it is a VTF file
743 FileBuffer Buffer in which content of a file has been read.
747 TRUE If this is a VTF file
748 FALSE If this is not a VTF file
752 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
761 IN OUT
FILE *FvMapFile
,
763 IN EFI_FFS_FILE_HEADER
*FfsFile
,
764 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
765 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
771 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
772 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
776 FvMapFile A pointer to FvMap File
777 FileName Ffs File PathName
778 FfsFile A pointer to Ffs file image.
779 ImageBaseAddress PeImage Base Address.
780 pImageContext Image Context Information.
784 EFI_SUCCESS Added required map information.
788 CHAR8 PeMapFileName
[MAX_LONG_FILE_PATH
];
790 CHAR8 FileGuidName
[MAX_LINE_LEN
];
792 CHAR8 Line
[MAX_LINE_LEN
];
793 CHAR8 KeyWord
[MAX_LINE_LEN
];
794 CHAR8 FunctionName
[MAX_LINE_LEN
];
795 EFI_PHYSICAL_ADDRESS FunctionAddress
;
797 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
799 UINT32 AddressOfEntryPoint
;
801 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
802 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
803 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
804 long long TempLongAddress
;
805 UINT32 TextVirtualAddress
;
806 UINT32 DataVirtualAddress
;
807 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
810 // Init local variable
814 // Print FileGuid to string buffer.
816 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
819 // Construct Map file Name
821 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
824 strncpy (PeMapFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
825 PeMapFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
828 // Change '\\' to '/', unified path format.
830 Cptr
= PeMapFileName
;
831 while (*Cptr
!= '\0') {
833 *Cptr
= FILE_SEP_CHAR
;
841 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
842 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
845 if (Cptr
< PeMapFileName
) {
846 return EFI_NOT_FOUND
;
858 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
862 if (strlen (Cptr
+ 1) >= MAX_LINE_LEN
) {
865 strncpy (KeyWord
, Cptr
+ 1, MAX_LINE_LEN
- 1);
866 KeyWord
[MAX_LINE_LEN
- 1] = 0;
870 // AddressOfEntryPoint and Offset in Image
872 if (!pImageContext
->IsTeImage
) {
873 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
874 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
876 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
879 sizeof (EFI_IMAGE_FILE_HEADER
) +
880 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
882 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
884 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
885 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
886 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
887 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
888 Index
= TEImageHeader
->NumberOfSections
;
892 // module information output
894 if (ImageBaseAddress
== 0) {
895 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
896 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
898 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
899 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
902 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
903 fprintf (FvMapFile
, ")\n");
905 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
906 TextVirtualAddress
= 0;
907 DataVirtualAddress
= 0;
908 for (; Index
> 0; Index
--, SectionHeader
++) {
909 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
910 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
911 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
912 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
913 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
914 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
917 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
918 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
919 fprintf (FvMapFile
, ")\n\n");
924 PeMapFile
= fopen (LongFilePath (PeMapFileName
), "r");
925 if (PeMapFile
== NULL
) {
926 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
929 VerboseMsg ("The map file is %s", PeMapFileName
);
932 // Output Functions information into Fv Map file
934 LinkTimeBaseAddress
= 0;
935 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
939 if (Line
[0] == 0x0a) {
944 // By Address and Static keyword
946 if (FunctionType
== 0) {
947 sscanf (Line
, "%s", KeyWord
);
948 if (stricmp (KeyWord
, "Address") == 0) {
953 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
954 } else if (stricmp (KeyWord
, "Static") == 0) {
956 // static function list
959 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
960 } else if (stricmp (KeyWord
, "Preferred") ==0) {
961 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
962 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
967 // Printf Function Information
969 if (FunctionType
== 1) {
970 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
971 FunctionAddress
= (UINT64
) TempLongAddress
;
972 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
973 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
974 fprintf (FvMapFile
, "%s\n", FunctionName
);
976 } else if (FunctionType
== 2) {
977 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
978 FunctionAddress
= (UINT64
) TempLongAddress
;
979 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
980 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
981 fprintf (FvMapFile
, "%s\n", FunctionName
);
988 fprintf (FvMapFile
, "\n\n");
996 AdjustInternalFfsPadding (
997 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
998 IN OUT MEMORY_FILE
*FvImage
,
1000 IN OUT UINTN
*FileSize
1004 Routine Description:
1006 This function looks for a dedicated alignment padding section in the FFS, and
1007 shrinks it to the size required to line up subsequent sections correctly.
1011 FfsFile A pointer to Ffs file image.
1012 FvImage The memory image of the FV to adjust it to.
1013 Alignment Current file alignment
1014 FileSize Reference to a variable holding the size of the FFS file
1018 TRUE Padding section was found and updated successfully
1023 EFI_FILE_SECTION_POINTER PadSection
;
1026 UINT32 FfsHeaderLength
;
1027 UINT32 FfsFileLength
;
1030 EFI_FFS_INTEGRITY_CHECK
*IntegrityCheck
;
1033 // Figure out the misalignment: all FFS sections are aligned relative to the
1034 // start of the FFS payload, so use that as the base of the misalignment
1037 FfsHeaderLength
= GetFfsHeaderLength(FfsFile
);
1038 Misalignment
= (UINTN
) FvImage
->CurrentFilePointer
-
1039 (UINTN
) FvImage
->FileImage
+ FfsHeaderLength
;
1040 Misalignment
&= Alignment
- 1;
1041 if (Misalignment
== 0) {
1042 // Nothing to do, return success
1047 // We only apply this optimization to FFS files with the FIXED attribute set,
1048 // since the FFS will not be loadable at arbitrary offsets anymore after
1049 // we adjust the size of the padding section.
1051 if ((FfsFile
->Attributes
& FFS_ATTRIB_FIXED
) == 0) {
1056 // Look for a dedicated padding section that we can adjust to compensate
1057 // for the misalignment. If such a padding section exists, it precedes all
1058 // sections with alignment requirements, and so the adjustment will correct
1061 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FREEFORM_SUBTYPE_GUID
, 1,
1063 if (EFI_ERROR (Status
) ||
1064 CompareGuid (&PadSection
.FreeformSubtypeSection
->SubTypeGuid
,
1065 &mEfiFfsSectionAlignmentPaddingGuid
) != 0) {
1070 // Find out if the size of the padding section is sufficient to compensate
1071 // for the misalignment.
1073 PadSize
= GetSectionFileLength (PadSection
.CommonHeader
);
1074 if (Misalignment
> PadSize
- sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION
)) {
1079 // Move the remainder of the FFS file towards the front, and adjust the
1080 // file size output parameter.
1082 Remainder
= (UINT8
*) PadSection
.CommonHeader
+ PadSize
;
1083 memmove (Remainder
- Misalignment
, Remainder
,
1084 *FileSize
- (UINTN
) (Remainder
- (UINTN
) FfsFile
));
1085 *FileSize
-= Misalignment
;
1088 // Update the padding section's length with the new values. Note that the
1089 // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2
1092 PadSize
-= Misalignment
;
1093 PadSection
.CommonHeader
->Size
[0] = (UINT8
) (PadSize
& 0xff);
1094 PadSection
.CommonHeader
->Size
[1] = (UINT8
) ((PadSize
& 0xff00) >> 8);
1095 PadSection
.CommonHeader
->Size
[2] = (UINT8
) ((PadSize
& 0xff0000) >> 16);
1098 // Update the FFS header with the new overall length
1100 FfsFileLength
= GetFfsFileLength (FfsFile
) - Misalignment
;
1101 if (FfsHeaderLength
> sizeof(EFI_FFS_FILE_HEADER
)) {
1102 ((EFI_FFS_FILE_HEADER2
*)FfsFile
)->ExtendedSize
= FfsFileLength
;
1104 FfsFile
->Size
[0] = (UINT8
) (FfsFileLength
& 0x000000FF);
1105 FfsFile
->Size
[1] = (UINT8
) ((FfsFileLength
& 0x0000FF00) >> 8);
1106 FfsFile
->Size
[2] = (UINT8
) ((FfsFileLength
& 0x00FF0000) >> 16);
1110 // Clear the alignment bits: these have become meaningless now that we have
1111 // adjusted the padding section.
1113 FfsFile
->Attributes
&= ~(FFS_ATTRIB_DATA_ALIGNMENT
| FFS_ATTRIB_DATA_ALIGNMENT2
);
1116 // Recalculate the FFS header checksum. Instead of setting Header and State
1117 // both to zero, set Header to (UINT8)(-State) so State preserves its original
1120 IntegrityCheck
= &FfsFile
->IntegrityCheck
;
1121 IntegrityCheck
->Checksum
.Header
= (UINT8
) (0x100 - FfsFile
->State
);
1122 IntegrityCheck
->Checksum
.File
= 0;
1124 IntegrityCheck
->Checksum
.Header
= CalculateChecksum8 (
1125 (UINT8
*) FfsFile
, FfsHeaderLength
);
1127 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1129 // Ffs header checksum = zero, so only need to calculate ffs body.
1131 IntegrityCheck
->Checksum
.File
= CalculateChecksum8 (
1132 (UINT8
*) FfsFile
+ FfsHeaderLength
,
1133 FfsFileLength
- FfsHeaderLength
);
1135 IntegrityCheck
->Checksum
.File
= FFS_FIXED_CHECKSUM
;
1143 IN OUT MEMORY_FILE
*FvImage
,
1146 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
1148 IN
FILE *FvReportFile
1152 Routine Description:
1154 This function adds a file to the FV image. The file will pad to the
1155 appropriate alignment if required.
1159 FvImage The memory image of the FV to add it to. The current offset
1161 FvInfo Pointer to information about the FV.
1162 Index The file in the FvInfo file list to add.
1163 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
1164 to the end of the FvImage then no VTF previously found.
1165 FvMapFile Pointer to FvMap File
1166 FvReportFile Pointer to FvReport File
1170 EFI_SUCCESS The function completed successfully.
1171 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1172 EFI_ABORTED An error occurred.
1173 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1181 UINT32 CurrentFileAlignment
;
1184 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
1188 // Verify input parameters.
1190 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
1191 return EFI_INVALID_PARAMETER
;
1195 // Read the file to add
1197 NewFile
= fopen (LongFilePath (FvInfo
->FvFiles
[Index
]), "rb");
1199 if (NewFile
== NULL
) {
1200 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
1205 // Get the file size
1207 FileSize
= _filelength (fileno (NewFile
));
1210 // Read the file into a buffer
1212 FileBuffer
= malloc (FileSize
);
1213 if (FileBuffer
== NULL
) {
1215 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1216 return EFI_OUT_OF_RESOURCES
;
1219 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1222 // Done with the file, from this point on we will just use the buffer read.
1227 // Verify read successful
1229 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1231 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1236 // For None PI Ffs file, directly add them into FvImage.
1238 if (!FvInfo
->IsPiFvImage
) {
1239 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1240 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1241 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1243 FvImage
->CurrentFilePointer
+= FileSize
;
1251 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1252 if (EFI_ERROR (Status
)) {
1254 Error (NULL
, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo
->FvFiles
[Index
]);
1255 return EFI_INVALID_PARAMETER
;
1259 // Verify space exists to add the file
1261 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1263 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1264 return EFI_OUT_OF_RESOURCES
;
1268 // Verify the input file is the duplicated file in this Fv image
1270 for (Index1
= 0; Index1
< Index
; Index1
++) {
1271 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1272 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1273 PrintGuid ((EFI_GUID
*) FileBuffer
);
1275 return EFI_INVALID_PARAMETER
;
1278 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1281 // Update the file state based on polarity of the FV.
1283 UpdateFfsFileState (
1284 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1285 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1289 // Check if alignment is required
1291 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1294 // Find the largest alignment of all the FFS files in the FV
1296 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1297 MaxFfsAlignment
= CurrentFileAlignment
;
1300 // If we have a VTF file, add it at the top.
1302 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1303 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1305 // No previous VTF, add this one.
1307 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1309 // Sanity check. The file MUST align appropriately
1311 if (((UINTN
) *VtfFileImage
+ GetFfsHeaderLength((EFI_FFS_FILE_HEADER
*)FileBuffer
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1312 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1317 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1318 // Rebase for the debug genfvmap tool
1320 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1321 if (EFI_ERROR (Status
)) {
1322 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1328 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1330 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1331 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1334 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1338 // Already found a VTF file.
1340 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1347 // Add pad file if necessary
1349 if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER
*) FileBuffer
, FvImage
,
1350 1 << CurrentFileAlignment
, &FileSize
)) {
1351 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
, FileSize
);
1352 if (EFI_ERROR (Status
)) {
1353 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1361 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1363 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1364 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1366 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1367 if (EFI_ERROR (Status
)) {
1368 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1374 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1375 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1376 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1377 FvImage
->CurrentFilePointer
+= FileSize
;
1379 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1384 // Make next file start at QWord Boundary
1386 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1387 FvImage
->CurrentFilePointer
++;
1392 // Free allocated memory.
1401 IN MEMORY_FILE
*FvImage
,
1402 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1406 Routine Description:
1408 This function places a pad file between the last file in the FV and the VTF
1409 file if the VTF file exists.
1413 FvImage Memory file for the FV memory image
1414 VtfFileImage The address of the VTF file. If this is the end of the FV
1415 image, no VTF exists and no pad file is needed.
1419 EFI_SUCCESS Completed successfully.
1420 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1424 EFI_FFS_FILE_HEADER
*PadFile
;
1426 UINT32 FfsHeaderSize
;
1429 // If there is no VTF or the VTF naturally follows the previous file without a
1430 // pad file, then there's nothing to do
1432 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1433 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1437 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1438 return EFI_INVALID_PARAMETER
;
1442 // Pad file starts at beginning of free space
1444 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1447 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1449 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1450 PadFile
->Attributes
= 0;
1453 // FileSize includes the EFI_FFS_FILE_HEADER
1455 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1456 if (FileSize
>= MAX_FFS_SIZE
) {
1457 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
1458 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
1459 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= FileSize
;
1460 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
1463 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1464 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1465 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1466 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
1470 // Fill in checksums and state, must be zero during checksum calculation.
1472 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1473 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1475 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, FfsHeaderSize
);
1476 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1478 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1480 UpdateFfsFileState (
1481 (EFI_FFS_FILE_HEADER
*) PadFile
,
1482 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1485 // Update the current FV pointer
1487 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1494 IN MEMORY_FILE
*FvImage
,
1496 IN EFI_FFS_FILE_HEADER
*VtfFile
1500 Routine Description:
1502 This parses the FV looking for the PEI core and then plugs the address into
1503 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1504 complete an IA32 Bootstrap FV.
1508 FvImage Memory file for the FV memory image
1509 FvInfo Information read from INF file.
1510 VtfFile Pointer to the VTF file in the FV image.
1514 EFI_SUCCESS Function Completed successfully.
1515 EFI_ABORTED Error encountered.
1516 EFI_INVALID_PARAMETER A required parameter was NULL.
1517 EFI_NOT_FOUND PEI Core file not found.
1521 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1522 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1524 EFI_FILE_SECTION_POINTER Pe32Section
;
1528 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1529 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1530 INT32 Ia32SecEntryOffset
;
1531 UINT32
*Ia32ResetAddressPtr
;
1533 UINT8
*BytePointer2
;
1534 UINT16
*WordPointer
;
1538 EFI_FFS_FILE_STATE SavedState
;
1539 BOOLEAN Vtf0Detected
;
1540 UINT32 FfsHeaderSize
;
1541 UINT32 SecHeaderSize
;
1544 // Verify input parameters
1546 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1547 return EFI_INVALID_PARAMETER
;
1550 // Initialize FV library
1552 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1557 Status
= VerifyFfsFile (VtfFile
);
1558 if (EFI_ERROR (Status
)) {
1559 return EFI_INVALID_PARAMETER
;
1563 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1564 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1565 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1566 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1567 IA32_X64_VTF0_SIGNATURE
)
1569 Vtf0Detected
= TRUE
;
1571 Vtf0Detected
= FALSE
;
1575 // Find the Sec Core
1577 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1578 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1581 // If the SEC core file is not found, but the VTF-0 signature
1582 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1583 // This means no modifications are required to the VTF.
1588 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1592 // Sec Core found, now find PE32 section
1594 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1595 if (Status
== EFI_NOT_FOUND
) {
1596 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1599 if (EFI_ERROR (Status
)) {
1600 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1604 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1605 Status
= GetPe32Info (
1606 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1612 if (EFI_ERROR (Status
)) {
1613 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1619 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1620 MachineType
== EFI_IMAGE_MACHINE_X64
)
1623 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1624 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1625 // This means no modifications are required to the VTF.
1631 // Physical address is FV base + offset of PE32 + offset of the entry point
1633 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1634 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1635 SecCorePhysicalAddress
+= EntryPoint
;
1636 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1639 // Find the PEI Core
1641 PeiCorePhysicalAddress
= 0;
1642 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1643 if (!EFI_ERROR (Status
) && (PeiCoreFile
!= NULL
)) {
1645 // PEI Core found, now find PE32 or TE section
1647 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1648 if (Status
== EFI_NOT_FOUND
) {
1649 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1652 if (EFI_ERROR (Status
)) {
1653 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1657 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1658 Status
= GetPe32Info (
1659 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1665 if (EFI_ERROR (Status
)) {
1666 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1670 // Physical address is FV base + offset of PE32 + offset of the entry point
1672 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1673 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1674 PeiCorePhysicalAddress
+= EntryPoint
;
1675 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1678 if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1679 if (PeiCorePhysicalAddress
!= 0) {
1681 // Get the location to update
1683 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1686 // Write lower 32 bits of physical address for Pei Core entry
1688 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1691 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1693 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1695 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1696 if (Ia32SecEntryOffset
<= -65536) {
1697 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1698 return STATUS_ERROR
;
1701 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1704 // Update the BFV base address
1706 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1707 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1708 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1711 // Update the Startup AP in the FVH header block ZeroVector region.
1713 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1714 if (FvInfo
->Size
<= 0x10000) {
1715 BytePointer2
= m64kRecoveryStartupApDataArray
;
1716 } else if (FvInfo
->Size
<= 0x20000) {
1717 BytePointer2
= m128kRecoveryStartupApDataArray
;
1719 BytePointer2
= m128kRecoveryStartupApDataArray
;
1721 // Find the position to place Ap reset vector, the offset
1722 // between the position and the end of Fvrecovery.fv file
1723 // should not exceed 128kB to prevent Ap reset vector from
1724 // outside legacy E and F segment
1726 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1727 if (EFI_ERROR (Status
)) {
1728 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.");
1733 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1734 BytePointer
[Index
] = BytePointer2
[Index
];
1737 // Calculate the checksum
1740 WordPointer
= (UINT16
*) (BytePointer
);
1741 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1742 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1746 // Update the checksum field
1748 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1749 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1752 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1754 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1755 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1756 if ((IpiVector
& 0xFFF) != 0) {
1757 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1760 IpiVector
= IpiVector
>> 12;
1761 IpiVector
= IpiVector
& 0xFF;
1764 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1766 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1767 *Ia32ResetAddressPtr
= IpiVector
;
1768 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1770 // Since the ARM reset vector is in the FV Header you really don't need a
1771 // Volume Top File, but if you have one for some reason don't crash...
1773 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1775 // Since the AArch64 reset vector is in the FV Header you really don't need a
1776 // Volume Top File, but if you have one for some reason don't crash...
1779 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1784 // Now update file checksum
1786 SavedState
= VtfFile
->State
;
1787 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1789 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1790 FfsHeaderSize
= GetFfsHeaderLength(VtfFile
);
1791 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1792 (UINT8
*) ((UINT8
*)VtfFile
+ FfsHeaderSize
),
1793 GetFfsFileLength (VtfFile
) - FfsHeaderSize
1796 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1799 VtfFile
->State
= SavedState
;
1806 IN VOID
*FvImageBuffer
,
1808 IN EFI_FV_FILETYPE FileType
,
1809 OUT EFI_FILE_SECTION_POINTER
*Pe32Section
1813 Routine Description:
1815 Recursively searches the FV for the FFS file of specified type (typically
1816 SEC or PEI core) and extracts the PE32 section for further processing.
1820 FvImageBuffer Buffer containing FV data
1821 FvSize Size of the FV
1822 FileType Type of FFS file to search for
1823 Pe32Section PE32 section pointer when FFS file is found.
1827 EFI_SUCCESS Function Completed successfully.
1828 EFI_ABORTED Error encountered.
1829 EFI_INVALID_PARAMETER A required parameter was NULL.
1830 EFI_NOT_FOUND Core file not found.
1835 EFI_FIRMWARE_VOLUME_HEADER
*OrigFvHeader
;
1836 UINT32 OrigFvLength
;
1837 EFI_FFS_FILE_HEADER
*CoreFfsFile
;
1838 UINTN FvImageFileCount
;
1839 EFI_FFS_FILE_HEADER
*FvImageFile
;
1840 UINTN EncapFvSectionCount
;
1841 EFI_FILE_SECTION_POINTER EncapFvSection
;
1842 EFI_FIRMWARE_VOLUME_HEADER
*EncapsulatedFvHeader
;
1844 if (Pe32Section
== NULL
) {
1845 return EFI_INVALID_PARAMETER
;
1849 // Initialize FV library, saving previous values
1851 OrigFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)NULL
;
1852 GetFvHeader (&OrigFvHeader
, &OrigFvLength
);
1853 InitializeFvLib(FvImageBuffer
, (UINT32
)FvSize
);
1856 // First see if we can obtain the file directly in outer FV
1858 Status
= GetFileByType(FileType
, 1, &CoreFfsFile
);
1859 if (!EFI_ERROR(Status
) && (CoreFfsFile
!= NULL
) ) {
1862 // Core found, now find PE32 or TE section
1864 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_PE32
, 1, Pe32Section
);
1865 if (EFI_ERROR(Status
)) {
1866 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_TE
, 1, Pe32Section
);
1869 if (EFI_ERROR(Status
)) {
1870 Error(NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the core file.");
1875 // Core PE/TE section, found, return
1877 Status
= EFI_SUCCESS
;
1882 // File was not found, look for FV Image file
1885 // iterate through all FV image files in outer FV
1886 for (FvImageFileCount
= 1;; FvImageFileCount
++) {
1888 Status
= GetFileByType(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
, FvImageFileCount
, &FvImageFile
);
1890 if (EFI_ERROR(Status
) || (FvImageFile
== NULL
) ) {
1891 // exit FV image file loop, no more found
1895 // Found an fv image file, look for an FV image section. The PI spec does not
1896 // preclude multiple FV image sections so we loop accordingly.
1897 for (EncapFvSectionCount
= 1;; EncapFvSectionCount
++) {
1899 // Look for the next FV image section. The section search code will
1900 // iterate into encapsulation sections. For example, it will iterate
1901 // into an EFI_SECTION_GUID_DEFINED encapsulation section to find the
1902 // EFI_SECTION_FIRMWARE_VOLUME_IMAGE sections contained therein.
1903 Status
= GetSectionByType(FvImageFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, EncapFvSectionCount
, &EncapFvSection
);
1905 if (EFI_ERROR(Status
)) {
1906 // exit section inner loop, no more found
1910 EncapsulatedFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINT8
*)EncapFvSection
.FVImageSection
+ GetSectionHeaderLength(EncapFvSection
.FVImageSection
));
1912 // recurse to search the encapsulated FV for this core file type
1913 Status
= FindCorePeSection(EncapsulatedFvHeader
, EncapsulatedFvHeader
->FvLength
, FileType
, Pe32Section
);
1915 if (!EFI_ERROR(Status
)) {
1916 // we found the core in the capsulated image, success
1920 } // end encapsulated fv image section loop
1921 } // end fv image file loop
1923 // core was not found
1924 Status
= EFI_NOT_FOUND
;
1928 // restore FV lib values
1929 if(OrigFvHeader
!= NULL
) {
1930 InitializeFvLib(OrigFvHeader
, OrigFvLength
);
1938 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1939 OUT UINT16
*CoreMachineType
1943 Routine Description:
1945 Returns the machine type of a P32 image, typically SEC or PEI core.
1949 Pe32Section PE32 section data
1950 CoreMachineType The extracted machine type
1954 EFI_SUCCESS Function Completed successfully.
1955 EFI_ABORTED Error encountered.
1956 EFI_INVALID_PARAMETER A required parameter was NULL.
1964 if (CoreMachineType
== NULL
) {
1965 return EFI_INVALID_PARAMETER
;
1968 Status
= GetPe32Info(
1969 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1974 if (EFI_ERROR(Status
)) {
1975 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 machine type for the core.");
1983 GetCoreEntryPointAddress(
1984 IN VOID
*FvImageBuffer
,
1986 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1987 OUT EFI_PHYSICAL_ADDRESS
*CoreEntryAddress
1991 Routine Description:
1993 Returns the physical address of the core (SEC or PEI) entry point.
1997 FvImageBuffer Pointer to buffer containing FV data
1998 FvInfo Info for the parent FV
1999 Pe32Section PE32 section data
2000 CoreEntryAddress The extracted core entry physical address
2004 EFI_SUCCESS Function Completed successfully.
2005 EFI_ABORTED Error encountered.
2006 EFI_INVALID_PARAMETER A required parameter was NULL.
2014 EFI_PHYSICAL_ADDRESS EntryPhysicalAddress
;
2016 if (CoreEntryAddress
== NULL
) {
2017 return EFI_INVALID_PARAMETER
;
2020 Status
= GetPe32Info(
2021 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
2026 if (EFI_ERROR(Status
)) {
2027 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the core.");
2032 // Physical address is FV base + offset of PE32 + offset of the entry point
2034 EntryPhysicalAddress
= FvInfo
->BaseAddress
;
2035 EntryPhysicalAddress
+= (UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
)FvImageBuffer
;
2036 EntryPhysicalAddress
+= EntryPoint
;
2038 *CoreEntryAddress
= EntryPhysicalAddress
;
2044 UpdateArmResetVectorIfNeeded (
2045 IN MEMORY_FILE
*FvImage
,
2050 Routine Description:
2051 This parses the FV looking for SEC and patches that address into the
2052 beginning of the FV header.
2054 For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
2055 For AArch64 the reset vector is at 0x00000000.
2057 This would commonly map to the first entry in the ROM.
2067 We support two schemes on ARM.
2068 1) Beginning of the FV is the reset vector
2069 2) Reset vector is data bytes FDF file and that code branches to reset vector
2070 in the beginning of the FV (fixed size offset).
2072 Need to have the jump for the reset vector at location zero.
2073 We also need to store the address or PEI (if it exists).
2074 We stub out a return from interrupt in case the debugger
2075 is using SWI (not done for AArch64, not enough space in struct).
2076 The optional entry to the common exception handler is
2077 to support full featured exception handling from ROM and is currently
2078 not support by this tool.
2081 FvImage Memory file for the FV memory image
2082 FvInfo Information read from INF file.
2086 EFI_SUCCESS Function Completed successfully.
2087 EFI_ABORTED Error encountered.
2088 EFI_INVALID_PARAMETER A required parameter was NULL.
2089 EFI_NOT_FOUND PEI Core file not found.
2094 EFI_FILE_SECTION_POINTER SecPe32
;
2095 EFI_FILE_SECTION_POINTER PeiPe32
;
2096 BOOLEAN UpdateVectorSec
= FALSE
;
2097 BOOLEAN UpdateVectorPei
= FALSE
;
2098 UINT16 MachineType
= 0;
2099 EFI_PHYSICAL_ADDRESS SecCoreEntryAddress
= 0;
2100 UINT16 PeiMachineType
= 0;
2101 EFI_PHYSICAL_ADDRESS PeiCoreEntryAddress
= 0;
2104 // Verify input parameters
2106 if (FvImage
== NULL
|| FvInfo
== NULL
) {
2107 return EFI_INVALID_PARAMETER
;
2111 // Locate an SEC Core instance and if found extract the machine type and entry point address
2113 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_SECURITY_CORE
, &SecPe32
);
2114 if (!EFI_ERROR(Status
)) {
2116 Status
= GetCoreMachineType(SecPe32
, &MachineType
);
2117 if (EFI_ERROR(Status
)) {
2118 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC Core.");
2122 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, SecPe32
, &SecCoreEntryAddress
);
2123 if (EFI_ERROR(Status
)) {
2124 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for SEC Core.");
2128 VerboseMsg("UpdateArmResetVectorIfNeeded found SEC core entry at 0x%llx", (unsigned long long)SecCoreEntryAddress
);
2129 UpdateVectorSec
= TRUE
;
2133 // Locate a PEI Core instance and if found extract the machine type and entry point address
2135 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_PEI_CORE
, &PeiPe32
);
2136 if (!EFI_ERROR(Status
)) {
2138 Status
= GetCoreMachineType(PeiPe32
, &PeiMachineType
);
2139 if (EFI_ERROR(Status
)) {
2140 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for PEI Core.");
2144 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, PeiPe32
, &PeiCoreEntryAddress
);
2145 if (EFI_ERROR(Status
)) {
2146 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for PEI Core.");
2150 VerboseMsg("UpdateArmResetVectorIfNeeded found PEI core entry at 0x%llx", (unsigned long long)PeiCoreEntryAddress
);
2152 // if we previously found an SEC Core make sure machine types match
2153 if (UpdateVectorSec
&& (MachineType
!= PeiMachineType
)) {
2154 Error(NULL
, 0, 3000, "Invalid", "SEC and PEI machine types do not match, can't update reset vector");
2158 MachineType
= PeiMachineType
;
2161 UpdateVectorPei
= TRUE
;
2164 if (!UpdateVectorSec
&& !UpdateVectorPei
) {
2168 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
2169 // ARM: Array of 4 UINT32s:
2170 // 0 - is branch relative to SEC entry point
2171 // 1 - PEI Entry Point
2172 // 2 - movs pc,lr for a SWI handler
2173 // 3 - Place holder for Common Exception Handler
2174 UINT32 ResetVector
[4];
2176 memset(ResetVector
, 0, sizeof (ResetVector
));
2178 // if we found an SEC core entry point then generate a branch instruction
2179 // to it and populate a debugger SWI entry as well
2180 if (UpdateVectorSec
) {
2182 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector");
2184 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
2185 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
2186 ResetVector
[0] = (INT32
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
- 8) >> 2;
2188 if (ResetVector
[0] > 0x00FFFFFF) {
2189 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
2193 // Add opcode for an unconditional branch with no link. i.e.: " B SecEntryPoint"
2194 ResetVector
[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION
;
2196 // SWI handler movs pc,lr. Just in case a debugger uses SWI
2197 ResetVector
[2] = 0xE1B0F07E;
2199 // Place holder to support a common interrupt handler from ROM.
2200 // Currently not supported. For this to be used the reset vector would not be in this FV
2201 // and the exception vectors would be hard coded in the ROM and just through this address
2202 // to find a common handler in the a module in the FV.
2206 // if a PEI core entry was found place its address in the vector area
2207 if (UpdateVectorPei
) {
2209 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM PEI address");
2211 // Address of PEI Core, if we have one
2212 ResetVector
[1] = (UINT32
)PeiCoreEntryAddress
;
2216 // Copy to the beginning of the FV
2218 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2220 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
2221 // AArch64: Used as UINT64 ResetVector[2]
2222 // 0 - is branch relative to SEC entry point
2223 // 1 - PEI Entry Point
2224 UINT64 ResetVector
[2];
2226 memset(ResetVector
, 0, sizeof (ResetVector
));
2229 ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
2230 array at the moment, for AArch64, does not allow us space for this as the header only
2231 allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
2232 within the first 4GB of addressable RAM we could potentially adopt the same ResetVector
2233 layout as above. But for the moment we replace the four 32bit vectors with two 64bit
2234 vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
2238 // if we found an SEC core entry point then generate a branch instruction to it
2239 if (UpdateVectorSec
) {
2241 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 SEC vector");
2243 ResetVector
[0] = (UINT64
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
) >> 2;
2245 // B SecEntryPoint - signed_immed_26 part +/-128MB offset
2246 if (ResetVector
[0] > 0x03FFFFFF) {
2247 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
2250 // Add opcode for an unconditional branch with no link. i.e.: " B SecEntryPoint"
2251 ResetVector
[0] |= ARM64_UNCONDITIONAL_JUMP_INSTRUCTION
;
2254 // if a PEI core entry was found place its address in the vector area
2255 if (UpdateVectorPei
) {
2257 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 PEI address");
2259 // Address of PEI Core, if we have one
2260 ResetVector
[1] = (UINT64
)PeiCoreEntryAddress
;
2264 // Copy to the beginning of the FV
2266 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2269 Error(NULL
, 0, 3000, "Invalid", "Unknown machine type");
2279 OUT UINT32
*EntryPoint
,
2280 OUT UINT32
*BaseOfCode
,
2281 OUT UINT16
*MachineType
2285 Routine Description:
2287 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
2288 See EfiImage.h for machine types. The entry point offset is from the beginning
2289 of the PE32 buffer passed in.
2293 Pe32 Beginning of the PE32.
2294 EntryPoint Offset from the beginning of the PE32 to the image entry point.
2295 BaseOfCode Base address of code.
2296 MachineType Magic number for the machine type.
2300 EFI_SUCCESS Function completed successfully.
2301 EFI_ABORTED Error encountered.
2302 EFI_INVALID_PARAMETER A required parameter was NULL.
2303 EFI_UNSUPPORTED The operation is unsupported.
2307 EFI_IMAGE_DOS_HEADER
*DosHeader
;
2308 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2309 EFI_TE_IMAGE_HEADER
*TeHeader
;
2312 // Verify input parameters
2315 return EFI_INVALID_PARAMETER
;
2319 // First check whether it is one TE Image.
2321 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
2322 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
2324 // By TeImage Header to get output
2326 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2327 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2328 *MachineType
= TeHeader
->Machine
;
2332 // Then check whether
2333 // First is the DOS header
2335 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2338 // Verify DOS header is expected
2340 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2341 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
2342 return EFI_UNSUPPORTED
;
2345 // Immediately following is the NT header.
2347 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2350 // Verify NT header is expected
2352 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2353 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
2354 return EFI_UNSUPPORTED
;
2359 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
2360 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
2361 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
2365 // Verify machine type is supported
2367 if ((*MachineType
!= EFI_IMAGE_MACHINE_IA32
) && (*MachineType
!= EFI_IMAGE_MACHINE_X64
) && (*MachineType
!= EFI_IMAGE_MACHINE_EBC
) &&
2368 (*MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (*MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
2369 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
2370 return EFI_UNSUPPORTED
;
2378 IN CHAR8
*InfFileImage
,
2379 IN UINTN InfFileSize
,
2380 IN CHAR8
*FvFileName
,
2381 IN CHAR8
*MapFileName
2385 Routine Description:
2387 This is the main function which will be called from application.
2391 InfFileImage Buffer containing the INF file contents.
2392 InfFileSize Size of the contents of the InfFileImage buffer.
2393 FvFileName Requested name for the FV file.
2394 MapFileName Fv map file to log fv driver information.
2398 EFI_SUCCESS Function completed successfully.
2399 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2400 EFI_ABORTED Error encountered.
2401 EFI_INVALID_PARAMETER A required parameter was NULL.
2406 MEMORY_FILE InfMemoryFile
;
2407 MEMORY_FILE FvImageMemoryFile
;
2409 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2410 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2411 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
2417 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2418 FILE *FvExtHeaderFile
;
2420 CHAR8
*FvReportName
;
2423 FvBufferHeader
= NULL
;
2427 FvReportName
= NULL
;
2428 FvReportFile
= NULL
;
2430 if (InfFileImage
!= NULL
) {
2432 // Initialize file structures
2434 InfMemoryFile
.FileImage
= InfFileImage
;
2435 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2436 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2439 // Parse the FV inf file for header information
2441 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2442 if (EFI_ERROR (Status
)) {
2443 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2449 // Update the file name return values
2451 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2452 FvFileName
= mFvDataInfo
.FvName
;
2455 if (FvFileName
== NULL
) {
2456 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2460 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2461 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2466 // Debug message Fv File System Guid
2468 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2469 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2470 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2471 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2472 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2473 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2474 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2475 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2476 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2477 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2478 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2479 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2480 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2484 // Add PI FV extension header
2487 FvExtHeaderFile
= NULL
;
2488 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2490 // Open the FV Extension Header file
2492 FvExtHeaderFile
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2493 if (FvExtHeaderFile
== NULL
) {
2494 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2499 // Get the file size
2501 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2504 // Allocate a buffer for the FV Extension Header
2506 FvExtHeader
= malloc(FileSize
);
2507 if (FvExtHeader
== NULL
) {
2508 fclose (FvExtHeaderFile
);
2509 return EFI_OUT_OF_RESOURCES
;
2513 // Read the FV Extension Header
2515 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2516 fclose (FvExtHeaderFile
);
2519 // See if there is an override for the FV Name GUID
2521 if (mFvDataInfo
.FvNameGuidSet
) {
2522 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2524 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2525 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2526 } else if (mFvDataInfo
.FvNameGuidSet
) {
2528 // Allocate a buffer for the FV Extension Header
2530 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2531 if (FvExtHeader
== NULL
) {
2532 return EFI_OUT_OF_RESOURCES
;
2534 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2535 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2539 // Debug message Fv Name Guid
2541 if (mFvDataInfo
.FvNameGuidSet
) {
2542 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2543 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2544 mFvDataInfo
.FvNameGuid
.Data2
,
2545 mFvDataInfo
.FvNameGuid
.Data3
,
2546 mFvDataInfo
.FvNameGuid
.Data4
[0],
2547 mFvDataInfo
.FvNameGuid
.Data4
[1],
2548 mFvDataInfo
.FvNameGuid
.Data4
[2],
2549 mFvDataInfo
.FvNameGuid
.Data4
[3],
2550 mFvDataInfo
.FvNameGuid
.Data4
[4],
2551 mFvDataInfo
.FvNameGuid
.Data4
[5],
2552 mFvDataInfo
.FvNameGuid
.Data4
[6],
2553 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2556 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0 ||
2557 CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem3Guid
) == 0) {
2558 mFvDataInfo
.IsPiFvImage
= TRUE
;
2562 // FvMap file to log the function address of all modules in one Fvimage
2564 if (MapFileName
!= NULL
) {
2565 if (strlen (MapFileName
) > MAX_LONG_FILE_PATH
- 1) {
2566 Error (NULL
, 0, 1003, "Invalid option value", "MapFileName %s is too long!", MapFileName
);
2567 Status
= EFI_ABORTED
;
2571 FvMapName
= malloc (strlen (MapFileName
) + 1);
2572 if (FvMapName
== NULL
) {
2573 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2574 Status
= EFI_OUT_OF_RESOURCES
;
2578 strcpy (FvMapName
, MapFileName
);
2580 if (strlen (FvFileName
) + strlen (".map") > MAX_LONG_FILE_PATH
- 1) {
2581 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2582 Status
= EFI_ABORTED
;
2586 FvMapName
= malloc (strlen (FvFileName
) + strlen (".map") + 1);
2587 if (FvMapName
== NULL
) {
2588 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2589 Status
= EFI_OUT_OF_RESOURCES
;
2593 strcpy (FvMapName
, FvFileName
);
2594 strcat (FvMapName
, ".map");
2596 VerboseMsg ("FV Map file name is %s", FvMapName
);
2599 // FvReport file to log the FV information in one Fvimage
2601 if (strlen (FvFileName
) + strlen (".txt") > MAX_LONG_FILE_PATH
- 1) {
2602 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2603 Status
= EFI_ABORTED
;
2607 FvReportName
= malloc (strlen (FvFileName
) + strlen (".txt") + 1);
2608 if (FvReportName
== NULL
) {
2609 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2610 Status
= EFI_OUT_OF_RESOURCES
;
2614 strcpy (FvReportName
, FvFileName
);
2615 strcat (FvReportName
, ".txt");
2618 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2619 // And Update mFvDataInfo data.
2621 Status
= CalculateFvSize (&mFvDataInfo
);
2622 if (EFI_ERROR (Status
)) {
2625 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2628 // support fv image and empty fv image
2630 FvImageSize
= mFvDataInfo
.Size
;
2633 // Allocate the FV, assure FvImage Header 8 byte alignment
2635 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2636 if (FvBufferHeader
== NULL
) {
2637 Status
= EFI_OUT_OF_RESOURCES
;
2640 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2643 // Initialize the FV to the erase polarity
2645 if (mFvDataInfo
.FvAttributes
== 0) {
2647 // Set Default Fv Attribute
2649 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2651 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2652 memset (FvImage
, -1, FvImageSize
);
2654 memset (FvImage
, 0, FvImageSize
);
2658 // Initialize FV header
2660 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2663 // Initialize the zero vector to all zeros.
2665 memset (FvHeader
->ZeroVector
, 0, 16);
2668 // Copy the Fv file system GUID
2670 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2672 FvHeader
->FvLength
= FvImageSize
;
2673 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2674 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2675 FvHeader
->Revision
= EFI_FVH_REVISION
;
2676 FvHeader
->ExtHeaderOffset
= 0;
2677 FvHeader
->Reserved
[0] = 0;
2680 // Copy firmware block map
2682 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2683 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2684 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2688 // Add block map terminator
2690 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2691 FvHeader
->BlockMap
[Index
].Length
= 0;
2694 // Complete the header
2696 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2697 FvHeader
->Checksum
= 0;
2698 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2701 // If there is no FFS file, generate one empty FV
2703 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2708 // Initialize our "file" view of the buffer
2710 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2711 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2712 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2715 // Initialize the FV library.
2717 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2720 // Initialize the VTF file address.
2722 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2727 FvMapFile
= fopen (LongFilePath (FvMapName
), "w");
2728 if (FvMapFile
== NULL
) {
2729 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2730 Status
= EFI_ABORTED
;
2735 // Open FvReport file
2737 FvReportFile
= fopen (LongFilePath (FvReportName
), "w");
2738 if (FvReportFile
== NULL
) {
2739 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2740 Status
= EFI_ABORTED
;
2744 // record FV size information into FvMap file.
2746 if (mFvTotalSize
!= 0) {
2747 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2748 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2750 if (mFvTakenSize
!= 0) {
2751 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2752 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2754 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2755 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2756 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2760 // record FV size information to FvReportFile.
2762 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2763 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2766 // Add PI FV extension header
2768 if (FvExtHeader
!= NULL
) {
2770 // Add FV Extended Header contents to the FV as a PAD file
2772 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2775 // Fv Extension header change update Fv Header Check sum
2777 FvHeader
->Checksum
= 0;
2778 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2784 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2788 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2791 // Exit if error detected while adding the file
2793 if (EFI_ERROR (Status
)) {
2799 // If there is a VTF file, some special actions need to occur.
2801 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2803 // Pad from the end of the last file to the beginning of the VTF file.
2804 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2806 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2807 if (EFI_ERROR (Status
)) {
2808 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2813 // Update reset vector (SALE_ENTRY for IPF)
2814 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2815 // EndAddress of 0xFFFFFFFF (unless the section was rebased).
2816 // Thus, only this type fv needs to update the reset vector.
2817 // If the PEI Core is found, the VTF file will probably get
2818 // corrupted by updating the entry point.
2820 if (mFvDataInfo
.ForceRebase
== 1 ||
2821 (mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2822 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2823 if (EFI_ERROR(Status
)) {
2824 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2827 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2833 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2834 if (EFI_ERROR (Status
)) {
2835 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2840 // Update Checksum for FvHeader
2842 FvHeader
->Checksum
= 0;
2843 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2847 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2849 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2850 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2851 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2853 // Update Checksum for FvHeader
2855 FvHeader
->Checksum
= 0;
2856 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2860 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2862 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2863 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2864 FvHeader
->Checksum
= 0;
2865 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2872 FvFile
= fopen (LongFilePath (FvFileName
), "wb");
2873 if (FvFile
== NULL
) {
2874 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2875 Status
= EFI_ABORTED
;
2879 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2880 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2881 Status
= EFI_ABORTED
;
2886 if (FvBufferHeader
!= NULL
) {
2887 free (FvBufferHeader
);
2890 if (FvExtHeader
!= NULL
) {
2894 if (FvMapName
!= NULL
) {
2898 if (FvReportName
!= NULL
) {
2899 free (FvReportName
);
2902 if (FvFile
!= NULL
) {
2907 if (FvMapFile
!= NULL
) {
2912 if (FvReportFile
!= NULL
) {
2913 fflush (FvReportFile
);
2914 fclose (FvReportFile
);
2920 UpdatePeiCoreEntryInFit (
2921 IN FIT_TABLE
*FitTablePtr
,
2922 IN UINT64 PeiCorePhysicalAddress
2926 Routine Description:
2928 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2933 FitTablePtr - The pointer of FIT_TABLE.
2934 PeiCorePhysicalAddress - The address of Pei Core entry.
2938 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2939 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2943 FIT_TABLE
*TmpFitPtr
;
2945 UINTN NumFitComponents
;
2947 TmpFitPtr
= FitTablePtr
;
2948 NumFitComponents
= TmpFitPtr
->CompSize
;
2950 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2951 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2952 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2959 return EFI_NOT_FOUND
;
2964 IN FIT_TABLE
*FitTablePtr
2968 Routine Description:
2970 This function is used to update the checksum for FIT.
2975 FitTablePtr - The pointer of FIT_TABLE.
2983 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2984 FitTablePtr
->CheckSum
= 0;
2985 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2994 Routine Description:
2995 Calculate the FV size and Update Fv Size based on the actual FFS files.
2996 And Update FvInfo data.
2999 FvInfoPtr - The pointer to FV_INFO structure.
3002 EFI_ABORTED - Ffs Image Error
3003 EFI_SUCCESS - Successfully update FvSize
3006 UINTN CurrentOffset
;
3010 UINTN FvExtendHeaderSize
;
3011 UINT32 FfsAlignment
;
3012 UINT32 FfsHeaderSize
;
3013 EFI_FFS_FILE_HEADER FfsHeader
;
3016 FvExtendHeaderSize
= 0;
3022 // Compute size for easy access later
3024 FvInfoPtr
->Size
= 0;
3025 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
3026 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
3030 // Calculate the required sizes for all FFS files.
3032 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
3034 for (Index
= 1;; Index
++) {
3035 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
3036 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
3042 // Calculate PI extension header
3044 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
3045 fpin
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
3047 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
3050 FvExtendHeaderSize
= _filelength (fileno (fpin
));
3052 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
3053 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
3056 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
3058 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3059 } else if (mFvDataInfo
.FvNameGuidSet
) {
3060 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
3061 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3065 // Accumulate every FFS file size.
3067 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
3072 fpin
= fopen (LongFilePath (FvInfoPtr
->FvFiles
[Index
]), "rb");
3074 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
3078 // Get the file size
3080 FfsFileSize
= _filelength (fileno (fpin
));
3081 if (FfsFileSize
>= MAX_FFS_SIZE
) {
3082 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
3085 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
3088 // Read Ffs File header
3090 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
3096 if (FvInfoPtr
->IsPiFvImage
) {
3098 // Check whether this ffs file is vtf file
3100 if (IsVtfFile (&FfsHeader
)) {
3103 // One Fv image can't have two vtf files.
3105 Error (NULL
, 0, 3000,"Invalid", "One Fv image can't have two vtf files.");
3109 VtfFileSize
= FfsFileSize
;
3114 // Get the alignment of FFS file
3116 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
3117 FfsAlignment
= 1 << FfsAlignment
;
3121 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
3123 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
3125 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
3126 CurrentOffset
-= FfsHeaderSize
;
3131 // Add ffs file size
3133 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
3134 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
3136 CurrentOffset
+= FfsFileSize
;
3140 // Make next ffs file start at QWord Boundary
3142 if (FvInfoPtr
->IsPiFvImage
) {
3143 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3146 CurrentOffset
+= VtfFileSize
;
3147 DebugMsg (NULL
, 0, 9, "FvImage size", "The calculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
3149 if (FvInfoPtr
->Size
== 0) {
3151 // Update FvInfo data
3153 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
3154 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
3155 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
3156 FvInfoPtr
->FvBlocks
[1].Length
= 0;
3157 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
3161 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
);
3162 return EFI_INVALID_PARAMETER
;
3166 // Set Fv Size Information
3168 mFvTotalSize
= FvInfoPtr
->Size
;
3169 mFvTakenSize
= CurrentOffset
;
3175 FfsRebaseImageRead (
3176 IN VOID
*FileHandle
,
3177 IN UINTN FileOffset
,
3178 IN OUT UINT32
*ReadSize
,
3183 Routine Description:
3185 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
3189 FileHandle - The handle to the PE/COFF file
3191 FileOffset - The offset, in bytes, into the file to read
3193 ReadSize - The number of bytes to read from the file starting at FileOffset
3195 Buffer - A pointer to the buffer to read the data into.
3199 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
3203 CHAR8
*Destination8
;
3207 Destination8
= Buffer
;
3208 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
3211 *(Destination8
++) = *(Source8
++);
3220 IN EFI_FFS_FILE_HEADER
*FfsFile
,
3225 Routine Description:
3227 This function gets all child FvImages in the input FfsFile, and records
3228 their base address to the parent image.
3231 FvInfo A pointer to FV_INFO structure.
3232 FfsFile A pointer to Ffs file image that may contain FvImage.
3233 XipOffset The offset address to the parent FvImage base.
3237 EFI_SUCCESS Base address of child Fv image is recorded.
3242 EFI_FILE_SECTION_POINTER SubFvSection
;
3243 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
3244 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
3245 EFI_FILE_SECTION_POINTER CorePe32
;
3248 for (Index
= 1;; Index
++) {
3252 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
3253 if (EFI_ERROR (Status
)) {
3256 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
3259 // See if there's an SEC core in the child FV
3260 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_SECURITY_CORE
, &CorePe32
);
3262 // if we couldn't find the SEC core, look for a PEI core
3263 if (EFI_ERROR(Status
)) {
3264 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_PEI_CORE
, &CorePe32
);
3267 if (!EFI_ERROR(Status
)) {
3268 Status
= GetCoreMachineType(CorePe32
, &MachineType
);
3269 if (EFI_ERROR(Status
)) {
3270 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC/PEI Core.");
3274 // machine type is ARM, set a flag so ARM reset vector processing occurs
3275 if ((MachineType
== EFI_IMAGE_MACHINE_ARMT
) || (MachineType
== EFI_IMAGE_MACHINE_AARCH64
)) {
3276 VerboseMsg("Located ARM/AArch64 SEC/PEI core in child FV");
3284 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
3285 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
3293 IN OUT FV_INFO
*FvInfo
,
3295 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
3301 Routine Description:
3303 This function determines if a file is XIP and should be rebased. It will
3304 rebase any PE32 sections found in the file using the base address.
3308 FvInfo A pointer to FV_INFO structure.
3309 FileName Ffs File PathName
3310 FfsFile A pointer to Ffs file image.
3311 XipOffset The offset address to use for rebasing the XIP file image.
3312 FvMapFile FvMapFile to record the function address in one Fvimage
3316 EFI_SUCCESS The image was properly rebased.
3317 EFI_INVALID_PARAMETER An input parameter is invalid.
3318 EFI_ABORTED An error occurred while rebasing the input file image.
3319 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
3320 EFI_NOT_FOUND No compressed sections could be found.
3325 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
3326 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
3327 EFI_PHYSICAL_ADDRESS XipBase
;
3328 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
3330 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
3331 EFI_FFS_FILE_STATE SavedState
;
3332 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
3333 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
3334 UINT8
*MemoryImagePointer
;
3335 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
3336 CHAR8 PeFileName
[MAX_LONG_FILE_PATH
];
3339 UINT8
*PeFileBuffer
;
3342 UINT32 FfsHeaderSize
;
3343 UINT32 CurSecHdrSize
;
3346 MemoryImagePointer
= NULL
;
3347 TEImageHeader
= NULL
;
3349 SectionHeader
= NULL
;
3352 PeFileBuffer
= NULL
;
3355 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
3357 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
3362 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
3364 if (FvInfo
->ForceRebase
== 0) {
3369 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
3372 // We only process files potentially containing PE32 sections.
3374 switch (FfsFile
->Type
) {
3375 case EFI_FV_FILETYPE_SECURITY_CORE
:
3376 case EFI_FV_FILETYPE_PEI_CORE
:
3377 case EFI_FV_FILETYPE_PEIM
:
3378 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3379 case EFI_FV_FILETYPE_DRIVER
:
3380 case EFI_FV_FILETYPE_DXE_CORE
:
3382 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
3384 // Rebase the inside FvImage.
3386 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
3389 // Search PE/TE section in FV sectin.
3396 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
3398 // Rebase each PE32 section
3400 Status
= EFI_SUCCESS
;
3401 for (Index
= 1;; Index
++) {
3405 NewPe32BaseAddress
= 0;
3410 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
3411 if (EFI_ERROR (Status
)) {
3414 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3417 // Initialize context
3419 memset (&ImageContext
, 0, sizeof (ImageContext
));
3420 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3421 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3422 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3423 if (EFI_ERROR (Status
)) {
3424 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3428 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3429 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3434 // Keep Image Context for PE image in FV
3436 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3439 // Get File PdbPointer
3441 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3444 // Get PeHeader pointer
3446 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3449 // Calculate the PE32 base address, based on file type
3451 switch (FfsFile
->Type
) {
3452 case EFI_FV_FILETYPE_SECURITY_CORE
:
3453 case EFI_FV_FILETYPE_PEI_CORE
:
3454 case EFI_FV_FILETYPE_PEIM
:
3455 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3457 // Check if section-alignment and file-alignment match or not
3459 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3461 // Xip module has the same section alignment and file alignment.
3463 Error (NULL
, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName
);
3467 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3469 if (ImageContext
.RelocationsStripped
) {
3471 // Construct the original efi file Name
3473 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
3474 Error (NULL
, 0, 2000, "Invalid", "The file name %s is too long.", FileName
);
3477 strncpy (PeFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
3478 PeFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
3479 Cptr
= PeFileName
+ strlen (PeFileName
);
3480 while (*Cptr
!= '.') {
3484 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3492 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3493 if (PeFile
== NULL
) {
3494 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3495 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3496 //return EFI_ABORTED;
3500 // Get the file size
3502 PeFileSize
= _filelength (fileno (PeFile
));
3503 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3504 if (PeFileBuffer
== NULL
) {
3506 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3507 return EFI_OUT_OF_RESOURCES
;
3512 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3518 // Handle pointer to the original efi image.
3520 ImageContext
.Handle
= PeFileBuffer
;
3521 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3522 if (EFI_ERROR (Status
)) {
3523 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3526 ImageContext
.RelocationsStripped
= FALSE
;
3529 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3532 case EFI_FV_FILETYPE_DRIVER
:
3533 case EFI_FV_FILETYPE_DXE_CORE
:
3535 // Check if section-alignment and file-alignment match or not
3537 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3539 // Xip module has the same section alignment and file alignment.
3541 Error (NULL
, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName
);
3544 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3549 // Not supported file type
3555 // Relocation doesn't exist
3557 if (ImageContext
.RelocationsStripped
) {
3558 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3563 // Relocation exist and rebase
3566 // Load and Relocate Image Data
3568 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3569 if (MemoryImagePointer
== NULL
) {
3570 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3571 return EFI_OUT_OF_RESOURCES
;
3573 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3574 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3576 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3577 if (EFI_ERROR (Status
)) {
3578 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3579 free ((VOID
*) MemoryImagePointer
);
3583 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3584 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3585 if (EFI_ERROR (Status
)) {
3586 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3587 free ((VOID
*) MemoryImagePointer
);
3592 // Copy Relocated data to raw image file.
3594 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3597 sizeof (EFI_IMAGE_FILE_HEADER
) +
3598 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3601 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3603 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3604 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3605 SectionHeader
->SizeOfRawData
3609 free ((VOID
*) MemoryImagePointer
);
3610 MemoryImagePointer
= NULL
;
3611 if (PeFileBuffer
!= NULL
) {
3612 free (PeFileBuffer
);
3613 PeFileBuffer
= NULL
;
3617 // Update Image Base Address
3619 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3620 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3621 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3622 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3624 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3625 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3632 // Now update file checksum
3634 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3635 SavedState
= FfsFile
->State
;
3636 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3638 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3639 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3640 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3642 FfsFile
->State
= SavedState
;
3646 // Get this module function address from ModulePeMapFile and add them into FvMap file
3650 // Default use FileName as map file path
3652 if (PdbPointer
== NULL
) {
3653 PdbPointer
= FileName
;
3656 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3659 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3660 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3661 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3662 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3663 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3666 // Only Peim code may have a TE section
3672 // Now process TE sections
3674 for (Index
= 1;; Index
++) {
3675 NewPe32BaseAddress
= 0;
3680 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3681 if (EFI_ERROR (Status
)) {
3685 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3688 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3691 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3694 // Initialize context, load image info.
3696 memset (&ImageContext
, 0, sizeof (ImageContext
));
3697 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3698 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3699 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3700 if (EFI_ERROR (Status
)) {
3701 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3705 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3706 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3711 // Keep Image Context for TE image in FV
3713 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3716 // Get File PdbPointer
3718 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3721 // Set new rebased address.
3723 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3724 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3727 // if reloc is stripped, try to get the original efi image to get reloc info.
3729 if (ImageContext
.RelocationsStripped
) {
3731 // Construct the original efi file name
3733 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
3734 Error (NULL
, 0, 2000, "Invalid", "The file name %s is too long.", FileName
);
3737 strncpy (PeFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
3738 PeFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
3739 Cptr
= PeFileName
+ strlen (PeFileName
);
3740 while (*Cptr
!= '.') {
3745 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3754 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3755 if (PeFile
== NULL
) {
3756 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3757 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3758 //return EFI_ABORTED;
3761 // Get the file size
3763 PeFileSize
= _filelength (fileno (PeFile
));
3764 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3765 if (PeFileBuffer
== NULL
) {
3767 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3768 return EFI_OUT_OF_RESOURCES
;
3773 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3779 // Append reloc section into TeImage
3781 ImageContext
.Handle
= PeFileBuffer
;
3782 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3783 if (EFI_ERROR (Status
)) {
3784 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3787 ImageContext
.RelocationsStripped
= FALSE
;
3791 // Relocation doesn't exist
3793 if (ImageContext
.RelocationsStripped
) {
3794 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3799 // Relocation exist and rebase
3802 // Load and Relocate Image Data
3804 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3805 if (MemoryImagePointer
== NULL
) {
3806 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3807 return EFI_OUT_OF_RESOURCES
;
3809 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3810 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3812 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3813 if (EFI_ERROR (Status
)) {
3814 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3815 free ((VOID
*) MemoryImagePointer
);
3819 // Reloacate TeImage
3821 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3822 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3823 if (EFI_ERROR (Status
)) {
3824 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3825 free ((VOID
*) MemoryImagePointer
);
3830 // Copy the relocated image into raw image file.
3832 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3833 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3834 if (!ImageContext
.IsTeImage
) {
3836 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3837 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3838 SectionHeader
->SizeOfRawData
3842 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3843 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3844 SectionHeader
->SizeOfRawData
3850 // Free the allocated memory resource
3852 free ((VOID
*) MemoryImagePointer
);
3853 MemoryImagePointer
= NULL
;
3854 if (PeFileBuffer
!= NULL
) {
3855 free (PeFileBuffer
);
3856 PeFileBuffer
= NULL
;
3860 // Update Image Base Address
3862 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3865 // Now update file checksum
3867 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3868 SavedState
= FfsFile
->State
;
3869 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3871 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3872 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3873 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3875 FfsFile
->State
= SavedState
;
3878 // Get this module function address from ModulePeMapFile and add them into FvMap file
3882 // Default use FileName as map file path
3884 if (PdbPointer
== NULL
) {
3885 PdbPointer
= FileName
;
3901 FindApResetVectorPosition (
3902 IN MEMORY_FILE
*FvImage
,
3907 Routine Description:
3909 Find the position in this FvImage to place Ap reset vector.
3913 FvImage Memory file for the FV memory image.
3914 Pointer Pointer to pointer to position.
3918 EFI_NOT_FOUND - No satisfied position is found.
3919 EFI_SUCCESS - The suitable position is return.
3923 EFI_FFS_FILE_HEADER
*PadFile
;
3929 for (Index
= 1; ;Index
++) {
3931 // Find Pad File to add ApResetVector info
3933 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3934 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3936 // No Pad file to be found.
3941 // Get Pad file size.
3943 FileLength
= GetFfsFileLength(PadFile
);
3944 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3946 // FixPoint must be align on 0x1000 relative to FvImage Header
3948 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3949 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3951 // FixPoint be larger at the last place of one fv image.
3953 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3958 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3960 // No alignment FixPoint in this Pad File.
3965 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3967 // Find the position to place ApResetVector
3969 *Pointer
= FixPoint
;
3974 return EFI_NOT_FOUND
;
3979 IN MEMORY_FILE
*InfFile
,
3980 OUT CAP_INFO
*CapInfo
3984 Routine Description:
3986 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3990 InfFile Memory file image.
3991 CapInfo Information read from INF file.
3995 EFI_SUCCESS INF file information successfully retrieved.
3996 EFI_ABORTED INF file has an invalid format.
3997 EFI_NOT_FOUND A required string was not found in the INF file.
4000 CHAR8 Value
[MAX_LONG_FILE_PATH
];
4002 UINTN Index
, Number
;
4006 // Initialize Cap info
4008 // memset (CapInfo, 0, sizeof (CAP_INFO));
4012 // Read the Capsule Guid
4014 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
4015 if (Status
== EFI_SUCCESS
) {
4017 // Get the Capsule Guid
4019 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
4020 if (EFI_ERROR (Status
)) {
4021 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4024 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4028 // Read the Capsule Header Size
4030 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
4031 if (Status
== EFI_SUCCESS
) {
4032 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4033 if (EFI_ERROR (Status
)) {
4034 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4037 CapInfo
->HeaderSize
= (UINT32
) Value64
;
4038 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4042 // Read the Capsule Flag
4044 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
4045 if (Status
== EFI_SUCCESS
) {
4046 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
4047 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
4048 if (strstr (Value
, "InitiateReset") != NULL
) {
4049 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4051 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
4052 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
4053 if (strstr (Value
, "InitiateReset") != NULL
) {
4054 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4057 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
4060 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
4063 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
4064 if (Status
== EFI_SUCCESS
) {
4065 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4066 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
4067 Error (NULL
, 0, 2000, "Invalid parameter",
4068 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
4069 EFI_OEM_CAPSULE_FLAGS_STRING
);
4072 CapInfo
->Flags
|= Value64
;
4073 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
4077 // Read Capsule File name
4079 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
4080 if (Status
== EFI_SUCCESS
) {
4082 // Get output file name
4084 strcpy (CapInfo
->CapName
, Value
);
4088 // Read the Capsule FileImage
4091 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
4092 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
4096 // Read the capsule file name
4098 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
4100 if (Status
== EFI_SUCCESS
) {
4104 strcpy (CapInfo
->CapFiles
[Index
], Value
);
4105 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
4112 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
4120 IN CHAR8
*InfFileImage
,
4121 IN UINTN InfFileSize
,
4122 IN CHAR8
*CapFileName
4126 Routine Description:
4128 This is the main function which will be called from application to create UEFI Capsule image.
4132 InfFileImage Buffer containing the INF file contents.
4133 InfFileSize Size of the contents of the InfFileImage buffer.
4134 CapFileName Requested name for the Cap file.
4138 EFI_SUCCESS Function completed successfully.
4139 EFI_OUT_OF_RESOURCES Could not allocate required resources.
4140 EFI_ABORTED Error encountered.
4141 EFI_INVALID_PARAMETER A required parameter was NULL.
4147 EFI_CAPSULE_HEADER
*CapsuleHeader
;
4148 MEMORY_FILE InfMemoryFile
;
4154 if (InfFileImage
!= NULL
) {
4156 // Initialize file structures
4158 InfMemoryFile
.FileImage
= InfFileImage
;
4159 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
4160 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
4163 // Parse the Cap inf file for header information
4165 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
4166 if (Status
!= EFI_SUCCESS
) {
4171 if (mCapDataInfo
.HeaderSize
== 0) {
4173 // make header size align 16 bytes.
4175 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
4176 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
4179 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
4180 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
4181 return EFI_INVALID_PARAMETER
;
4184 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
4185 CapFileName
= mCapDataInfo
.CapName
;
4188 if (CapFileName
== NULL
) {
4189 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
4190 return EFI_INVALID_PARAMETER
;
4194 // Set Default Capsule Guid value
4196 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
4197 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
4200 // Calculate the size of capsule image.
4204 CapSize
= mCapDataInfo
.HeaderSize
;
4205 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4206 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4208 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4211 FileSize
= _filelength (fileno (fpin
));
4212 CapSize
+= FileSize
;
4218 // Allocate buffer for capsule image.
4220 CapBuffer
= (UINT8
*) malloc (CapSize
);
4221 if (CapBuffer
== NULL
) {
4222 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
4223 return EFI_OUT_OF_RESOURCES
;
4227 // Initialize the capsule header to zero
4229 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
4232 // create capsule header and get capsule body
4234 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
4235 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
4236 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
4237 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
4238 CapsuleHeader
->CapsuleImageSize
= CapSize
;
4242 CapSize
= CapsuleHeader
->HeaderSize
;
4243 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4244 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4246 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4250 FileSize
= _filelength (fileno (fpin
));
4251 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
4254 CapSize
+= FileSize
;
4258 // write capsule data into the output file
4260 fpout
= fopen (LongFilePath (CapFileName
), "wb");
4261 if (fpout
== NULL
) {
4262 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
4267 fwrite (CapBuffer
, 1, CapSize
, fpout
);
4271 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);