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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #if defined(__FreeBSD__)
23 #elif defined(__GNUC__)
24 #include <uuid/uuid.h>
35 #include <Guid/FfsSectionAlignmentPadding.h>
37 #include "WinNtInclude.h"
38 #include "GenFvInternalLib.h"
40 #include "PeCoffLib.h"
42 #define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION 0xEB000000
43 #define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION 0x14000000
46 STATIC UINT32 MaxFfsAlignment
= 0;
47 BOOLEAN VtfFileFlag
= FALSE
;
49 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
50 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
51 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
52 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
53 EFI_GUID mEfiFfsSectionAlignmentPaddingGuid
= EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID
;
55 CHAR8
*mFvbAttributeName
[] = {
56 EFI_FVB2_READ_DISABLED_CAP_STRING
,
57 EFI_FVB2_READ_ENABLED_CAP_STRING
,
58 EFI_FVB2_READ_STATUS_STRING
,
59 EFI_FVB2_WRITE_DISABLED_CAP_STRING
,
60 EFI_FVB2_WRITE_ENABLED_CAP_STRING
,
61 EFI_FVB2_WRITE_STATUS_STRING
,
62 EFI_FVB2_LOCK_CAP_STRING
,
63 EFI_FVB2_LOCK_STATUS_STRING
,
65 EFI_FVB2_STICKY_WRITE_STRING
,
66 EFI_FVB2_MEMORY_MAPPED_STRING
,
67 EFI_FVB2_ERASE_POLARITY_STRING
,
68 EFI_FVB2_READ_LOCK_CAP_STRING
,
69 EFI_FVB2_READ_LOCK_STATUS_STRING
,
70 EFI_FVB2_WRITE_LOCK_CAP_STRING
,
71 EFI_FVB2_WRITE_LOCK_STATUS_STRING
74 CHAR8
*mFvbAlignmentName
[] = {
75 EFI_FVB2_ALIGNMENT_1_STRING
,
76 EFI_FVB2_ALIGNMENT_2_STRING
,
77 EFI_FVB2_ALIGNMENT_4_STRING
,
78 EFI_FVB2_ALIGNMENT_8_STRING
,
79 EFI_FVB2_ALIGNMENT_16_STRING
,
80 EFI_FVB2_ALIGNMENT_32_STRING
,
81 EFI_FVB2_ALIGNMENT_64_STRING
,
82 EFI_FVB2_ALIGNMENT_128_STRING
,
83 EFI_FVB2_ALIGNMENT_256_STRING
,
84 EFI_FVB2_ALIGNMENT_512_STRING
,
85 EFI_FVB2_ALIGNMENT_1K_STRING
,
86 EFI_FVB2_ALIGNMENT_2K_STRING
,
87 EFI_FVB2_ALIGNMENT_4K_STRING
,
88 EFI_FVB2_ALIGNMENT_8K_STRING
,
89 EFI_FVB2_ALIGNMENT_16K_STRING
,
90 EFI_FVB2_ALIGNMENT_32K_STRING
,
91 EFI_FVB2_ALIGNMENT_64K_STRING
,
92 EFI_FVB2_ALIGNMENT_128K_STRING
,
93 EFI_FVB2_ALIGNMENT_256K_STRING
,
94 EFI_FVB2_ALIGNMENT_512K_STRING
,
95 EFI_FVB2_ALIGNMENT_1M_STRING
,
96 EFI_FVB2_ALIGNMENT_2M_STRING
,
97 EFI_FVB2_ALIGNMENT_4M_STRING
,
98 EFI_FVB2_ALIGNMENT_8M_STRING
,
99 EFI_FVB2_ALIGNMENT_16M_STRING
,
100 EFI_FVB2_ALIGNMENT_32M_STRING
,
101 EFI_FVB2_ALIGNMENT_64M_STRING
,
102 EFI_FVB2_ALIGNMENT_128M_STRING
,
103 EFI_FVB2_ALIGNMENT_256M_STRING
,
104 EFI_FVB2_ALIGNMENT_512M_STRING
,
105 EFI_FVB2_ALIGNMENT_1G_STRING
,
106 EFI_FVB2_ALIGNMENT_2G_STRING
110 // This data array will be located at the base of the Firmware Volume Header (FVH)
111 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
112 // will be used to keep the FVH checksum consistent.
113 // This code will be run in response to a starutp IPI for HT-enabled systems.
115 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
117 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
119 // EA D0 FF 00 F0 ; far jmp F000:FFD0
120 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
121 // 0, 0 ; Checksum Padding
141 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
143 // EB CE ; jmp short ($-0x30)
144 // ; (from offset 0x0 to offset 0xFFD0)
145 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
146 // 0, 0 ; Checksum Padding
167 CAP_INFO mCapDataInfo
;
168 BOOLEAN mIsLargeFfs
= FALSE
;
170 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
171 UINT32 mFvBaseAddressNumber
= 0;
175 IN MEMORY_FILE
*InfFile
,
182 This function parses a FV.INF file and copies info into a FV_INFO structure.
186 InfFile Memory file image.
187 FvInfo Information read from INF file.
191 EFI_SUCCESS INF file information successfully retrieved.
192 EFI_ABORTED INF file has an invalid format.
193 EFI_NOT_FOUND A required string was not found in the INF file.
196 CHAR8 Value
[MAX_LONG_FILE_PATH
];
204 // Read the FV base address
206 if (!mFvDataInfo
.BaseAddressSet
) {
207 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
208 if (Status
== EFI_SUCCESS
) {
210 // Get the base address
212 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
213 if (EFI_ERROR (Status
)) {
214 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
217 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
219 FvInfo
->BaseAddress
= Value64
;
220 FvInfo
->BaseAddressSet
= TRUE
;
225 // Read the FV File System Guid
227 if (!FvInfo
->FvFileSystemGuidSet
) {
228 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
229 if (Status
== EFI_SUCCESS
) {
231 // Get the guid value
233 Status
= StringToGuid (Value
, &GuidValue
);
234 if (EFI_ERROR (Status
)) {
235 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
238 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
239 FvInfo
->FvFileSystemGuidSet
= TRUE
;
244 // Read the FV Extension Header File Name
246 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
247 if (Status
== EFI_SUCCESS
) {
248 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
252 // Read the FV file name
254 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
255 if (Status
== EFI_SUCCESS
) {
257 // copy the file name
259 strcpy (FvInfo
->FvName
, Value
);
265 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
266 if ((mFvbAttributeName
[Index
] != NULL
) && \
267 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
268 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
269 FvInfo
->FvAttributes
|= 1 << Index
;
270 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
271 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
280 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
281 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
282 if (strcmp (Value
, TRUE_STRING
) == 0) {
283 FvInfo
->FvAttributes
|= Index
<< 16;
284 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
291 // Read weak alignment flag
293 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_WEAK_ALIGNMENT_STRING
, 0, Value
);
294 if (Status
== EFI_SUCCESS
) {
295 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
296 FvInfo
->FvAttributes
|= EFI_FVB2_WEAK_ALIGNMENT
;
297 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
298 Error (NULL
, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
306 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
307 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
311 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
313 if (Status
== EFI_SUCCESS
) {
315 // Update the size of block
317 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
318 if (EFI_ERROR (Status
)) {
319 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
323 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
324 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
327 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
328 // and should return an error.
330 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
331 if (!EFI_ERROR (Status
)) {
332 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
343 // Read blocks number
345 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
347 if (Status
== EFI_SUCCESS
) {
349 // Update the number of blocks
351 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
352 if (EFI_ERROR (Status
)) {
353 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
357 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
358 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
364 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
372 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
373 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
378 for (Index
= 0; Number
+ Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
380 // Read the FFS file list
382 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
384 if (Status
== EFI_SUCCESS
) {
388 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
389 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
395 if ((Index
+ Number
) == 0) {
396 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
404 IN EFI_FFS_FILE_HEADER
*FfsFile
,
405 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
411 This function changes the FFS file attributes based on the erase polarity
412 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
425 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
426 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
427 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
433 IN EFI_FFS_FILE_HEADER
*FfsFile
,
434 IN OUT UINT32
*Alignment
440 This function determines the alignment of the FFS input file from the file
445 FfsFile FFS file to parse
446 Alignment The minimum required alignment offset of the FFS file
450 EFI_SUCCESS The function completed successfully.
451 EFI_INVALID_PARAMETER One of the input parameters was invalid.
452 EFI_ABORTED An error occurred.
457 // Verify input parameters.
459 if (FfsFile
== NULL
|| Alignment
== NULL
) {
460 return EFI_INVALID_PARAMETER
;
463 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
468 //if bit 1 have set, 128K byte alignmnet
470 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
480 //if bit 1 have set, 256K byte alignment
482 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
491 // 128 byte alignment
492 //if bit 1 have set, 512K byte alignment
494 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
503 // 512 byte alignment
504 //if bit 1 have set, 1M byte alignment
506 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
516 //if bit 1 have set, 2M byte alignment
518 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
528 //if bit 1 have set, 4M byte alignment
530 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
539 // 32K byte alignment
540 //if bit 1 have set , 8M byte alignment
542 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
551 // 64K byte alignment
552 //if bit 1 have set, 16M alignment
554 if (FfsFile
->Attributes
& FFS_ATTRIB_DATA_ALIGNMENT2
) {
570 IN OUT MEMORY_FILE
*FvImage
,
571 IN UINT32 DataAlignment
,
573 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
,
574 IN UINT32 NextFfsSize
580 This function adds a pad file to the FV image if it required to align the
581 data of the next file.
585 FvImage The memory image of the FV to add it to.
586 The current offset must be valid.
587 DataAlignment The data alignment of the next FFS file.
588 FvEnd End of the empty data in FvImage.
589 ExtHeader PI FvExtHeader Optional
593 EFI_SUCCESS The function completed successfully.
594 EFI_INVALID_PARAMETER One of the input parameters was invalid.
595 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
600 EFI_FFS_FILE_HEADER
*PadFile
;
602 UINT32 NextFfsHeaderSize
;
603 UINT32 CurFfsHeaderSize
;
607 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
609 // Verify input parameters.
611 if (FvImage
== NULL
) {
612 return EFI_INVALID_PARAMETER
;
616 // Calculate the pad file size
620 // Append extension header size
622 if (ExtHeader
!= NULL
) {
623 PadFileSize
= ExtHeader
->ExtHeaderSize
;
624 if (PadFileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
625 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
627 PadFileSize
+= CurFfsHeaderSize
;
629 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
630 if (NextFfsSize
>= MAX_FFS_SIZE
) {
631 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
634 // Check if a pad file is necessary
636 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ NextFfsHeaderSize
) % DataAlignment
== 0) {
639 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
) + NextFfsHeaderSize
;
641 // Add whatever it takes to get to the next aligned address
643 while ((PadFileSize
% DataAlignment
) != 0) {
647 // Subtract the next file header size
649 PadFileSize
-= NextFfsHeaderSize
;
651 // Subtract the starting offset to get size
653 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
657 // Verify that we have enough space for the file header
659 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
660 return EFI_OUT_OF_RESOURCES
;
664 // Write pad file header
666 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
669 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
671 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
672 PadFile
->Attributes
= 0;
675 // Write pad file size (calculated size minus next file header size)
677 if (PadFileSize
>= MAX_FFS_SIZE
) {
678 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
679 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= PadFileSize
;
680 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
682 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
683 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
684 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
688 // Fill in checksums and state, they must be 0 for checksumming.
690 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
691 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
693 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, CurFfsHeaderSize
);
694 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
696 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
698 (EFI_FFS_FILE_HEADER
*) PadFile
,
699 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
703 // Update the current FV pointer
705 FvImage
->CurrentFilePointer
+= PadFileSize
;
707 if (ExtHeader
!= NULL
) {
709 // Copy Fv Extension Header and Set Fv Extension header offset
711 if (ExtHeader
->ExtHeaderSize
> sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
)) {
712 for (Index
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
); Index
< ExtHeader
->ExtHeaderSize
;) {
713 if (((EFI_FIRMWARE_VOLUME_EXT_ENTRY
*)((UINT8
*)ExtHeader
+ Index
))-> ExtEntryType
== EFI_FV_EXT_TYPE_USED_SIZE_TYPE
) {
715 ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE
*)((UINT8
*)ExtHeader
+ Index
))->UsedSize
= mFvTotalSize
;
717 ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE
*)((UINT8
*)ExtHeader
+ Index
))->UsedSize
= mFvTakenSize
;
721 Index
+= ((EFI_FIRMWARE_VOLUME_EXT_ENTRY
*)((UINT8
*)ExtHeader
+ Index
))-> ExtEntrySize
;
724 memcpy ((UINT8
*)PadFile
+ CurFfsHeaderSize
, ExtHeader
, ExtHeader
->ExtHeaderSize
);
725 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) ((UINT8
*)PadFile
+ CurFfsHeaderSize
) - (UINTN
) FvImage
->FileImage
);
727 // Make next file start at QWord Boundry
729 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
730 FvImage
->CurrentFilePointer
++;
739 IN EFI_FFS_FILE_HEADER
*FileBuffer
745 This function checks the header to validate if it is a VTF file
749 FileBuffer Buffer in which content of a file has been read.
753 TRUE If this is a VTF file
754 FALSE If this is not a VTF file
758 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
767 IN OUT
FILE *FvMapFile
,
769 IN EFI_FFS_FILE_HEADER
*FfsFile
,
770 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
771 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
777 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
778 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
782 FvMapFile A pointer to FvMap File
783 FileName Ffs File PathName
784 FfsFile A pointer to Ffs file image.
785 ImageBaseAddress PeImage Base Address.
786 pImageContext Image Context Information.
790 EFI_SUCCESS Added required map information.
794 CHAR8 PeMapFileName
[MAX_LONG_FILE_PATH
];
796 CHAR8 FileGuidName
[MAX_LINE_LEN
];
798 CHAR8 Line
[MAX_LINE_LEN
];
799 CHAR8 KeyWord
[MAX_LINE_LEN
];
800 CHAR8 FunctionName
[MAX_LINE_LEN
];
801 EFI_PHYSICAL_ADDRESS FunctionAddress
;
803 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
805 UINT32 AddressOfEntryPoint
;
807 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
808 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
809 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
810 long long TempLongAddress
;
811 UINT32 TextVirtualAddress
;
812 UINT32 DataVirtualAddress
;
813 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
816 // Init local variable
820 // Print FileGuid to string buffer.
822 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
825 // Construct Map file Name
827 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
830 strncpy (PeMapFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
831 PeMapFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
834 // Change '\\' to '/', unified path format.
836 Cptr
= PeMapFileName
;
837 while (*Cptr
!= '\0') {
839 *Cptr
= FILE_SEP_CHAR
;
847 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
848 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
851 if (Cptr
< PeMapFileName
) {
852 return EFI_NOT_FOUND
;
864 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
868 if (strlen (Cptr
+ 1) >= MAX_LINE_LEN
) {
871 strncpy (KeyWord
, Cptr
+ 1, MAX_LINE_LEN
- 1);
872 KeyWord
[MAX_LINE_LEN
- 1] = 0;
876 // AddressOfEntryPoint and Offset in Image
878 if (!pImageContext
->IsTeImage
) {
879 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
880 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
882 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
885 sizeof (EFI_IMAGE_FILE_HEADER
) +
886 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
888 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
890 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
891 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
892 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
893 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
894 Index
= TEImageHeader
->NumberOfSections
;
898 // module information output
900 if (ImageBaseAddress
== 0) {
901 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
902 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
904 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
905 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
908 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
909 fprintf (FvMapFile
, ")\n");
911 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
912 TextVirtualAddress
= 0;
913 DataVirtualAddress
= 0;
914 for (; Index
> 0; Index
--, SectionHeader
++) {
915 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
916 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
917 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
918 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
919 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
920 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
923 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
924 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
925 fprintf (FvMapFile
, ")\n\n");
930 PeMapFile
= fopen (LongFilePath (PeMapFileName
), "r");
931 if (PeMapFile
== NULL
) {
932 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
935 VerboseMsg ("The map file is %s", PeMapFileName
);
938 // Output Functions information into Fv Map file
940 LinkTimeBaseAddress
= 0;
941 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
945 if (Line
[0] == 0x0a) {
950 // By Address and Static keyword
952 if (FunctionType
== 0) {
953 sscanf (Line
, "%s", KeyWord
);
954 if (stricmp (KeyWord
, "Address") == 0) {
959 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
960 } else if (stricmp (KeyWord
, "Static") == 0) {
962 // static function list
965 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
966 } else if (stricmp (KeyWord
, "Preferred") ==0) {
967 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
968 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
973 // Printf Function Information
975 if (FunctionType
== 1) {
976 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
977 FunctionAddress
= (UINT64
) TempLongAddress
;
978 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
979 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
980 fprintf (FvMapFile
, "%s\n", FunctionName
);
982 } else if (FunctionType
== 2) {
983 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
984 FunctionAddress
= (UINT64
) TempLongAddress
;
985 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
986 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
987 fprintf (FvMapFile
, "%s\n", FunctionName
);
994 fprintf (FvMapFile
, "\n\n");
1002 AdjustInternalFfsPadding (
1003 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
1004 IN OUT MEMORY_FILE
*FvImage
,
1006 IN OUT UINTN
*FileSize
1010 Routine Description:
1012 This function looks for a dedicated alignment padding section in the FFS, and
1013 shrinks it to the size required to line up subsequent sections correctly.
1017 FfsFile A pointer to Ffs file image.
1018 FvImage The memory image of the FV to adjust it to.
1019 Alignment Current file alignment
1020 FileSize Reference to a variable holding the size of the FFS file
1024 TRUE Padding section was found and updated successfully
1029 EFI_FILE_SECTION_POINTER PadSection
;
1032 UINT32 FfsHeaderLength
;
1033 UINT32 FfsFileLength
;
1036 EFI_FFS_INTEGRITY_CHECK
*IntegrityCheck
;
1039 // Figure out the misalignment: all FFS sections are aligned relative to the
1040 // start of the FFS payload, so use that as the base of the misalignment
1043 FfsHeaderLength
= GetFfsHeaderLength(FfsFile
);
1044 Misalignment
= (UINTN
) FvImage
->CurrentFilePointer
-
1045 (UINTN
) FvImage
->FileImage
+ FfsHeaderLength
;
1046 Misalignment
&= Alignment
- 1;
1047 if (Misalignment
== 0) {
1048 // Nothing to do, return success
1053 // We only apply this optimization to FFS files with the FIXED attribute set,
1054 // since the FFS will not be loadable at arbitrary offsets anymore after
1055 // we adjust the size of the padding section.
1057 if ((FfsFile
->Attributes
& FFS_ATTRIB_FIXED
) == 0) {
1062 // Look for a dedicated padding section that we can adjust to compensate
1063 // for the misalignment. If such a padding section exists, it precedes all
1064 // sections with alignment requirements, and so the adjustment will correct
1067 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FREEFORM_SUBTYPE_GUID
, 1,
1069 if (EFI_ERROR (Status
) ||
1070 CompareGuid (&PadSection
.FreeformSubtypeSection
->SubTypeGuid
,
1071 &mEfiFfsSectionAlignmentPaddingGuid
) != 0) {
1076 // Find out if the size of the padding section is sufficient to compensate
1077 // for the misalignment.
1079 PadSize
= GetSectionFileLength (PadSection
.CommonHeader
);
1080 if (Misalignment
> PadSize
- sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION
)) {
1085 // Move the remainder of the FFS file towards the front, and adjust the
1086 // file size output parameter.
1088 Remainder
= (UINT8
*) PadSection
.CommonHeader
+ PadSize
;
1089 memmove (Remainder
- Misalignment
, Remainder
,
1090 *FileSize
- (UINTN
) (Remainder
- (UINTN
) FfsFile
));
1091 *FileSize
-= Misalignment
;
1094 // Update the padding section's length with the new values. Note that the
1095 // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2
1098 PadSize
-= Misalignment
;
1099 PadSection
.CommonHeader
->Size
[0] = (UINT8
) (PadSize
& 0xff);
1100 PadSection
.CommonHeader
->Size
[1] = (UINT8
) ((PadSize
& 0xff00) >> 8);
1101 PadSection
.CommonHeader
->Size
[2] = (UINT8
) ((PadSize
& 0xff0000) >> 16);
1104 // Update the FFS header with the new overall length
1106 FfsFileLength
= GetFfsFileLength (FfsFile
) - Misalignment
;
1107 if (FfsHeaderLength
> sizeof(EFI_FFS_FILE_HEADER
)) {
1108 ((EFI_FFS_FILE_HEADER2
*)FfsFile
)->ExtendedSize
= FfsFileLength
;
1110 FfsFile
->Size
[0] = (UINT8
) (FfsFileLength
& 0x000000FF);
1111 FfsFile
->Size
[1] = (UINT8
) ((FfsFileLength
& 0x0000FF00) >> 8);
1112 FfsFile
->Size
[2] = (UINT8
) ((FfsFileLength
& 0x00FF0000) >> 16);
1116 // Clear the alignment bits: these have become meaningless now that we have
1117 // adjusted the padding section.
1119 FfsFile
->Attributes
&= ~(FFS_ATTRIB_DATA_ALIGNMENT
| FFS_ATTRIB_DATA_ALIGNMENT2
);
1122 // Recalculate the FFS header checksum. Instead of setting Header and State
1123 // both to zero, set Header to (UINT8)(-State) so State preserves its original
1126 IntegrityCheck
= &FfsFile
->IntegrityCheck
;
1127 IntegrityCheck
->Checksum
.Header
= (UINT8
) (0x100 - FfsFile
->State
);
1128 IntegrityCheck
->Checksum
.File
= 0;
1130 IntegrityCheck
->Checksum
.Header
= CalculateChecksum8 (
1131 (UINT8
*) FfsFile
, FfsHeaderLength
);
1133 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1135 // Ffs header checksum = zero, so only need to calculate ffs body.
1137 IntegrityCheck
->Checksum
.File
= CalculateChecksum8 (
1138 (UINT8
*) FfsFile
+ FfsHeaderLength
,
1139 FfsFileLength
- FfsHeaderLength
);
1141 IntegrityCheck
->Checksum
.File
= FFS_FIXED_CHECKSUM
;
1149 IN OUT MEMORY_FILE
*FvImage
,
1152 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
1154 IN
FILE *FvReportFile
1158 Routine Description:
1160 This function adds a file to the FV image. The file will pad to the
1161 appropriate alignment if required.
1165 FvImage The memory image of the FV to add it to. The current offset
1167 FvInfo Pointer to information about the FV.
1168 Index The file in the FvInfo file list to add.
1169 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
1170 to the end of the FvImage then no VTF previously found.
1171 FvMapFile Pointer to FvMap File
1172 FvReportFile Pointer to FvReport File
1176 EFI_SUCCESS The function completed successfully.
1177 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1178 EFI_ABORTED An error occurred.
1179 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1187 UINT32 CurrentFileAlignment
;
1190 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
1194 // Verify input parameters.
1196 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
1197 return EFI_INVALID_PARAMETER
;
1201 // Read the file to add
1203 NewFile
= fopen (LongFilePath (FvInfo
->FvFiles
[Index
]), "rb");
1205 if (NewFile
== NULL
) {
1206 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
1211 // Get the file size
1213 FileSize
= _filelength (fileno (NewFile
));
1216 // Read the file into a buffer
1218 FileBuffer
= malloc (FileSize
);
1219 if (FileBuffer
== NULL
) {
1221 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
1222 return EFI_OUT_OF_RESOURCES
;
1225 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1228 // Done with the file, from this point on we will just use the buffer read.
1233 // Verify read successful
1235 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1237 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1242 // For None PI Ffs file, directly add them into FvImage.
1244 if (!FvInfo
->IsPiFvImage
) {
1245 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1246 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1247 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1249 FvImage
->CurrentFilePointer
+= FileSize
;
1257 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1258 if (EFI_ERROR (Status
)) {
1260 Error (NULL
, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo
->FvFiles
[Index
]);
1261 return EFI_INVALID_PARAMETER
;
1265 // Verify space exists to add the file
1267 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1269 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1270 return EFI_OUT_OF_RESOURCES
;
1274 // Verify the input file is the duplicated file in this Fv image
1276 for (Index1
= 0; Index1
< Index
; Index1
++) {
1277 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1278 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1279 PrintGuid ((EFI_GUID
*) FileBuffer
);
1281 return EFI_INVALID_PARAMETER
;
1284 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1287 // Update the file state based on polarity of the FV.
1289 UpdateFfsFileState (
1290 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1291 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1295 // Check if alignment is required
1297 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1300 // Find the largest alignment of all the FFS files in the FV
1302 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1303 MaxFfsAlignment
= CurrentFileAlignment
;
1306 // If we have a VTF file, add it at the top.
1308 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1309 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1311 // No previous VTF, add this one.
1313 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1315 // Sanity check. The file MUST align appropriately
1317 if (((UINTN
) *VtfFileImage
+ GetFfsHeaderLength((EFI_FFS_FILE_HEADER
*)FileBuffer
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1318 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1323 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1324 // Rebase for the debug genfvmap tool
1326 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1327 if (EFI_ERROR (Status
)) {
1328 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1334 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1336 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1337 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1340 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1344 // Already found a VTF file.
1346 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1353 // Add pad file if necessary
1355 if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER
*) FileBuffer
, FvImage
,
1356 1 << CurrentFileAlignment
, &FileSize
)) {
1357 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
, FileSize
);
1358 if (EFI_ERROR (Status
)) {
1359 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1367 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1369 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1370 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1372 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1373 if (EFI_ERROR (Status
)) {
1374 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1380 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1381 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1382 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1383 FvImage
->CurrentFilePointer
+= FileSize
;
1385 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1390 // Make next file start at QWord Boundry
1392 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1393 FvImage
->CurrentFilePointer
++;
1398 // Free allocated memory.
1407 IN MEMORY_FILE
*FvImage
,
1408 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1412 Routine Description:
1414 This function places a pad file between the last file in the FV and the VTF
1415 file if the VTF file exists.
1419 FvImage Memory file for the FV memory image
1420 VtfFileImage The address of the VTF file. If this is the end of the FV
1421 image, no VTF exists and no pad file is needed.
1425 EFI_SUCCESS Completed successfully.
1426 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1430 EFI_FFS_FILE_HEADER
*PadFile
;
1432 UINT32 FfsHeaderSize
;
1435 // If there is no VTF or the VTF naturally follows the previous file without a
1436 // pad file, then there's nothing to do
1438 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1439 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1443 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1444 return EFI_INVALID_PARAMETER
;
1448 // Pad file starts at beginning of free space
1450 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1453 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1455 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1456 PadFile
->Attributes
= 0;
1459 // FileSize includes the EFI_FFS_FILE_HEADER
1461 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1462 if (FileSize
>= MAX_FFS_SIZE
) {
1463 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
1464 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
1465 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= FileSize
;
1466 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
1469 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1470 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1471 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1472 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
1476 // Fill in checksums and state, must be zero during checksum calculation.
1478 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1479 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1481 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, FfsHeaderSize
);
1482 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1484 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1486 UpdateFfsFileState (
1487 (EFI_FFS_FILE_HEADER
*) PadFile
,
1488 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1491 // Update the current FV pointer
1493 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1500 IN MEMORY_FILE
*FvImage
,
1502 IN EFI_FFS_FILE_HEADER
*VtfFile
1506 Routine Description:
1508 This parses the FV looking for the PEI core and then plugs the address into
1509 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1510 complete an IA32 Bootstrap FV.
1514 FvImage Memory file for the FV memory image
1515 FvInfo Information read from INF file.
1516 VtfFile Pointer to the VTF file in the FV image.
1520 EFI_SUCCESS Function Completed successfully.
1521 EFI_ABORTED Error encountered.
1522 EFI_INVALID_PARAMETER A required parameter was NULL.
1523 EFI_NOT_FOUND PEI Core file not found.
1527 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1528 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1530 EFI_FILE_SECTION_POINTER Pe32Section
;
1534 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1535 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1536 INT32 Ia32SecEntryOffset
;
1537 UINT32
*Ia32ResetAddressPtr
;
1539 UINT8
*BytePointer2
;
1540 UINT16
*WordPointer
;
1544 EFI_FFS_FILE_STATE SavedState
;
1545 BOOLEAN Vtf0Detected
;
1546 UINT32 FfsHeaderSize
;
1547 UINT32 SecHeaderSize
;
1550 // Verify input parameters
1552 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1553 return EFI_INVALID_PARAMETER
;
1556 // Initialize FV library
1558 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1563 Status
= VerifyFfsFile (VtfFile
);
1564 if (EFI_ERROR (Status
)) {
1565 return EFI_INVALID_PARAMETER
;
1569 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1570 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1571 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1572 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1573 IA32_X64_VTF0_SIGNATURE
)
1575 Vtf0Detected
= TRUE
;
1577 Vtf0Detected
= FALSE
;
1581 // Find the Sec Core
1583 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1584 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1587 // If the SEC core file is not found, but the VTF-0 signature
1588 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1589 // This means no modifications are required to the VTF.
1594 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1598 // Sec Core found, now find PE32 section
1600 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1601 if (Status
== EFI_NOT_FOUND
) {
1602 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1605 if (EFI_ERROR (Status
)) {
1606 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1610 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1611 Status
= GetPe32Info (
1612 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1618 if (EFI_ERROR (Status
)) {
1619 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1625 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1626 MachineType
== EFI_IMAGE_MACHINE_X64
)
1629 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1630 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1631 // This means no modifications are required to the VTF.
1637 // Physical address is FV base + offset of PE32 + offset of the entry point
1639 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1640 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1641 SecCorePhysicalAddress
+= EntryPoint
;
1642 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1645 // Find the PEI Core
1647 PeiCorePhysicalAddress
= 0;
1648 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1649 if (!EFI_ERROR (Status
) && (PeiCoreFile
!= NULL
)) {
1651 // PEI Core found, now find PE32 or TE section
1653 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1654 if (Status
== EFI_NOT_FOUND
) {
1655 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1658 if (EFI_ERROR (Status
)) {
1659 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1663 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1664 Status
= GetPe32Info (
1665 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1671 if (EFI_ERROR (Status
)) {
1672 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1676 // Physical address is FV base + offset of PE32 + offset of the entry point
1678 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1679 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1680 PeiCorePhysicalAddress
+= EntryPoint
;
1681 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1684 if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1685 if (PeiCorePhysicalAddress
!= 0) {
1687 // Get the location to update
1689 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1692 // Write lower 32 bits of physical address for Pei Core entry
1694 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1697 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1699 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1701 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1702 if (Ia32SecEntryOffset
<= -65536) {
1703 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1704 return STATUS_ERROR
;
1707 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1710 // Update the BFV base address
1712 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1713 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1714 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1717 // Update the Startup AP in the FVH header block ZeroVector region.
1719 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1720 if (FvInfo
->Size
<= 0x10000) {
1721 BytePointer2
= m64kRecoveryStartupApDataArray
;
1722 } else if (FvInfo
->Size
<= 0x20000) {
1723 BytePointer2
= m128kRecoveryStartupApDataArray
;
1725 BytePointer2
= m128kRecoveryStartupApDataArray
;
1727 // Find the position to place Ap reset vector, the offset
1728 // between the position and the end of Fvrecovery.fv file
1729 // should not exceed 128kB to prevent Ap reset vector from
1730 // outside legacy E and F segment
1732 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1733 if (EFI_ERROR (Status
)) {
1734 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.");
1739 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1740 BytePointer
[Index
] = BytePointer2
[Index
];
1743 // Calculate the checksum
1746 WordPointer
= (UINT16
*) (BytePointer
);
1747 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1748 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1752 // Update the checksum field
1754 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1755 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1758 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1760 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1761 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1762 if ((IpiVector
& 0xFFF) != 0) {
1763 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1766 IpiVector
= IpiVector
>> 12;
1767 IpiVector
= IpiVector
& 0xFF;
1770 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1772 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1773 *Ia32ResetAddressPtr
= IpiVector
;
1774 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1776 // Since the ARM reset vector is in the FV Header you really don't need a
1777 // Volume Top File, but if you have one for some reason don't crash...
1779 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1781 // Since the AArch64 reset vector is in the FV Header you really don't need a
1782 // Volume Top File, but if you have one for some reason don't crash...
1785 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1790 // Now update file checksum
1792 SavedState
= VtfFile
->State
;
1793 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1795 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1796 FfsHeaderSize
= GetFfsHeaderLength(VtfFile
);
1797 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1798 (UINT8
*) ((UINT8
*)VtfFile
+ FfsHeaderSize
),
1799 GetFfsFileLength (VtfFile
) - FfsHeaderSize
1802 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1805 VtfFile
->State
= SavedState
;
1812 IN VOID
*FvImageBuffer
,
1814 IN EFI_FV_FILETYPE FileType
,
1815 OUT EFI_FILE_SECTION_POINTER
*Pe32Section
1819 Routine Description:
1821 Recursively searches the FV for the FFS file of specified type (typically
1822 SEC or PEI core) and extracts the PE32 section for further processing.
1826 FvImageBuffer Buffer containing FV data
1827 FvSize Size of the FV
1828 FileType Type of FFS file to search for
1829 Pe32Section PE32 section pointer when FFS file is found.
1833 EFI_SUCCESS Function Completed successfully.
1834 EFI_ABORTED Error encountered.
1835 EFI_INVALID_PARAMETER A required parameter was NULL.
1836 EFI_NOT_FOUND Core file not found.
1841 EFI_FIRMWARE_VOLUME_HEADER
*OrigFvHeader
;
1842 UINT32 OrigFvLength
;
1843 EFI_FFS_FILE_HEADER
*CoreFfsFile
;
1844 UINTN FvImageFileCount
;
1845 EFI_FFS_FILE_HEADER
*FvImageFile
;
1846 UINTN EncapFvSectionCount
;
1847 EFI_FILE_SECTION_POINTER EncapFvSection
;
1848 EFI_FIRMWARE_VOLUME_HEADER
*EncapsulatedFvHeader
;
1850 if (Pe32Section
== NULL
) {
1851 return EFI_INVALID_PARAMETER
;
1855 // Initialize FV library, saving previous values
1857 OrigFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)NULL
;
1858 GetFvHeader (&OrigFvHeader
, &OrigFvLength
);
1859 InitializeFvLib(FvImageBuffer
, (UINT32
)FvSize
);
1862 // First see if we can obtain the file directly in outer FV
1864 Status
= GetFileByType(FileType
, 1, &CoreFfsFile
);
1865 if (!EFI_ERROR(Status
) && (CoreFfsFile
!= NULL
) ) {
1868 // Core found, now find PE32 or TE section
1870 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_PE32
, 1, Pe32Section
);
1871 if (EFI_ERROR(Status
)) {
1872 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_TE
, 1, Pe32Section
);
1875 if (EFI_ERROR(Status
)) {
1876 Error(NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the core file.");
1881 // Core PE/TE section, found, return
1883 Status
= EFI_SUCCESS
;
1888 // File was not found, look for FV Image file
1891 // iterate through all FV image files in outer FV
1892 for (FvImageFileCount
= 1;; FvImageFileCount
++) {
1894 Status
= GetFileByType(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
, FvImageFileCount
, &FvImageFile
);
1896 if (EFI_ERROR(Status
) || (FvImageFile
== NULL
) ) {
1897 // exit FV image file loop, no more found
1901 // Found an fv image file, look for an FV image section. The PI spec does not
1902 // preclude multiple FV image sections so we loop accordingly.
1903 for (EncapFvSectionCount
= 1;; EncapFvSectionCount
++) {
1905 // Look for the next FV image section. The section search code will
1906 // iterate into encapsulation sections. For example, it will iterate
1907 // into an EFI_SECTION_GUID_DEFINED encapsulation section to find the
1908 // EFI_SECTION_FIRMWARE_VOLUME_IMAGE sections contained therein.
1909 Status
= GetSectionByType(FvImageFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, EncapFvSectionCount
, &EncapFvSection
);
1911 if (EFI_ERROR(Status
)) {
1912 // exit section inner loop, no more found
1916 EncapsulatedFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINT8
*)EncapFvSection
.FVImageSection
+ GetSectionHeaderLength(EncapFvSection
.FVImageSection
));
1918 // recurse to search the encapsulated FV for this core file type
1919 Status
= FindCorePeSection(EncapsulatedFvHeader
, EncapsulatedFvHeader
->FvLength
, FileType
, Pe32Section
);
1921 if (!EFI_ERROR(Status
)) {
1922 // we found the core in the capsulated image, success
1926 } // end encapsulated fv image section loop
1927 } // end fv image file loop
1929 // core was not found
1930 Status
= EFI_NOT_FOUND
;
1934 // restore FV lib values
1935 if(OrigFvHeader
!= NULL
) {
1936 InitializeFvLib(OrigFvHeader
, OrigFvLength
);
1944 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1945 OUT UINT16
*CoreMachineType
1949 Routine Description:
1951 Returns the machine type of a P32 image, typically SEC or PEI core.
1955 Pe32Section PE32 section data
1956 CoreMachineType The extracted machine type
1960 EFI_SUCCESS Function Completed successfully.
1961 EFI_ABORTED Error encountered.
1962 EFI_INVALID_PARAMETER A required parameter was NULL.
1970 if (CoreMachineType
== NULL
) {
1971 return EFI_INVALID_PARAMETER
;
1974 Status
= GetPe32Info(
1975 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1980 if (EFI_ERROR(Status
)) {
1981 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 machine type for the core.");
1989 GetCoreEntryPointAddress(
1990 IN VOID
*FvImageBuffer
,
1992 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1993 OUT EFI_PHYSICAL_ADDRESS
*CoreEntryAddress
1997 Routine Description:
1999 Returns the physical address of the core (SEC or PEI) entry point.
2003 FvImageBuffer Pointer to buffer containing FV data
2004 FvInfo Info for the parent FV
2005 Pe32Section PE32 section data
2006 CoreEntryAddress The extracted core entry physical address
2010 EFI_SUCCESS Function Completed successfully.
2011 EFI_ABORTED Error encountered.
2012 EFI_INVALID_PARAMETER A required parameter was NULL.
2020 EFI_PHYSICAL_ADDRESS EntryPhysicalAddress
;
2022 if (CoreEntryAddress
== NULL
) {
2023 return EFI_INVALID_PARAMETER
;
2026 Status
= GetPe32Info(
2027 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
2032 if (EFI_ERROR(Status
)) {
2033 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the core.");
2038 // Physical address is FV base + offset of PE32 + offset of the entry point
2040 EntryPhysicalAddress
= FvInfo
->BaseAddress
;
2041 EntryPhysicalAddress
+= (UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
)FvImageBuffer
;
2042 EntryPhysicalAddress
+= EntryPoint
;
2044 *CoreEntryAddress
= EntryPhysicalAddress
;
2050 UpdateArmResetVectorIfNeeded (
2051 IN MEMORY_FILE
*FvImage
,
2056 Routine Description:
2057 This parses the FV looking for SEC and patches that address into the
2058 beginning of the FV header.
2060 For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
2061 For AArch64 the reset vector is at 0x00000000.
2063 This would commonly map to the first entry in the ROM.
2073 We support two schemes on ARM.
2074 1) Beginning of the FV is the reset vector
2075 2) Reset vector is data bytes FDF file and that code branches to reset vector
2076 in the beginning of the FV (fixed size offset).
2078 Need to have the jump for the reset vector at location zero.
2079 We also need to store the address or PEI (if it exists).
2080 We stub out a return from interrupt in case the debugger
2081 is using SWI (not done for AArch64, not enough space in struct).
2082 The optional entry to the common exception handler is
2083 to support full featured exception handling from ROM and is currently
2084 not support by this tool.
2087 FvImage Memory file for the FV memory image
2088 FvInfo Information read from INF file.
2092 EFI_SUCCESS Function Completed successfully.
2093 EFI_ABORTED Error encountered.
2094 EFI_INVALID_PARAMETER A required parameter was NULL.
2095 EFI_NOT_FOUND PEI Core file not found.
2100 EFI_FILE_SECTION_POINTER SecPe32
;
2101 EFI_FILE_SECTION_POINTER PeiPe32
;
2102 BOOLEAN UpdateVectorSec
= FALSE
;
2103 BOOLEAN UpdateVectorPei
= FALSE
;
2104 UINT16 MachineType
= 0;
2105 EFI_PHYSICAL_ADDRESS SecCoreEntryAddress
= 0;
2106 UINT16 PeiMachineType
= 0;
2107 EFI_PHYSICAL_ADDRESS PeiCoreEntryAddress
= 0;
2110 // Verify input parameters
2112 if (FvImage
== NULL
|| FvInfo
== NULL
) {
2113 return EFI_INVALID_PARAMETER
;
2117 // Locate an SEC Core instance and if found extract the machine type and entry point address
2119 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_SECURITY_CORE
, &SecPe32
);
2120 if (!EFI_ERROR(Status
)) {
2122 Status
= GetCoreMachineType(SecPe32
, &MachineType
);
2123 if (EFI_ERROR(Status
)) {
2124 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC Core.");
2128 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, SecPe32
, &SecCoreEntryAddress
);
2129 if (EFI_ERROR(Status
)) {
2130 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for SEC Core.");
2134 VerboseMsg("UpdateArmResetVectorIfNeeded found SEC core entry at 0x%llx", (unsigned long long)SecCoreEntryAddress
);
2135 UpdateVectorSec
= TRUE
;
2139 // Locate a PEI Core instance and if found extract the machine type and entry point address
2141 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_PEI_CORE
, &PeiPe32
);
2142 if (!EFI_ERROR(Status
)) {
2144 Status
= GetCoreMachineType(PeiPe32
, &PeiMachineType
);
2145 if (EFI_ERROR(Status
)) {
2146 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for PEI Core.");
2150 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, PeiPe32
, &PeiCoreEntryAddress
);
2151 if (EFI_ERROR(Status
)) {
2152 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for PEI Core.");
2156 VerboseMsg("UpdateArmResetVectorIfNeeded found PEI core entry at 0x%llx", (unsigned long long)PeiCoreEntryAddress
);
2158 // if we previously found an SEC Core make sure machine types match
2159 if (UpdateVectorSec
&& (MachineType
!= PeiMachineType
)) {
2160 Error(NULL
, 0, 3000, "Invalid", "SEC and PEI machine types do not match, can't update reset vector");
2164 MachineType
= PeiMachineType
;
2167 UpdateVectorPei
= TRUE
;
2170 if (!UpdateVectorSec
&& !UpdateVectorPei
) {
2174 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
2175 // ARM: Array of 4 UINT32s:
2176 // 0 - is branch relative to SEC entry point
2177 // 1 - PEI Entry Point
2178 // 2 - movs pc,lr for a SWI handler
2179 // 3 - Place holder for Common Exception Handler
2180 UINT32 ResetVector
[4];
2182 memset(ResetVector
, 0, sizeof (ResetVector
));
2184 // if we found an SEC core entry point then generate a branch instruction
2185 // to it and populate a debugger SWI entry as well
2186 if (UpdateVectorSec
) {
2188 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector");
2190 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
2191 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
2192 ResetVector
[0] = (INT32
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
- 8) >> 2;
2194 if (ResetVector
[0] > 0x00FFFFFF) {
2195 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
2199 // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"
2200 ResetVector
[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION
;
2202 // SWI handler movs pc,lr. Just in case a debugger uses SWI
2203 ResetVector
[2] = 0xE1B0F07E;
2205 // Place holder to support a common interrupt handler from ROM.
2206 // Currently not suppprted. For this to be used the reset vector would not be in this FV
2207 // and the exception vectors would be hard coded in the ROM and just through this address
2208 // to find a common handler in the a module in the FV.
2212 // if a PEI core entry was found place its address in the vector area
2213 if (UpdateVectorPei
) {
2215 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM PEI address");
2217 // Address of PEI Core, if we have one
2218 ResetVector
[1] = (UINT32
)PeiCoreEntryAddress
;
2222 // Copy to the beginning of the FV
2224 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2226 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
2227 // AArch64: Used as UINT64 ResetVector[2]
2228 // 0 - is branch relative to SEC entry point
2229 // 1 - PEI Entry Point
2230 UINT64 ResetVector
[2];
2232 memset(ResetVector
, 0, sizeof (ResetVector
));
2235 ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
2236 array at the moment, for AArch64, does not allow us space for this as the header only
2237 allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
2238 within the first 4GB of addressable RAM we could potensioally adopt the same ResetVector
2239 layout as above. But for the moment we replace the four 32bit vectors with two 64bit
2240 vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
2244 // if we found an SEC core entry point then generate a branch instruction to it
2245 if (UpdateVectorSec
) {
2247 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 SEC vector");
2249 ResetVector
[0] = (UINT64
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
) >> 2;
2251 // B SecEntryPoint - signed_immed_26 part +/-128MB offset
2252 if (ResetVector
[0] > 0x03FFFFFF) {
2253 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
2256 // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"
2257 ResetVector
[0] |= ARM64_UNCONDITIONAL_JUMP_INSTRUCTION
;
2260 // if a PEI core entry was found place its address in the vector area
2261 if (UpdateVectorPei
) {
2263 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 PEI address");
2265 // Address of PEI Core, if we have one
2266 ResetVector
[1] = (UINT64
)PeiCoreEntryAddress
;
2270 // Copy to the beginning of the FV
2272 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2275 Error(NULL
, 0, 3000, "Invalid", "Unknown machine type");
2285 OUT UINT32
*EntryPoint
,
2286 OUT UINT32
*BaseOfCode
,
2287 OUT UINT16
*MachineType
2291 Routine Description:
2293 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
2294 See EfiImage.h for machine types. The entry point offset is from the beginning
2295 of the PE32 buffer passed in.
2299 Pe32 Beginning of the PE32.
2300 EntryPoint Offset from the beginning of the PE32 to the image entry point.
2301 BaseOfCode Base address of code.
2302 MachineType Magic number for the machine type.
2306 EFI_SUCCESS Function completed successfully.
2307 EFI_ABORTED Error encountered.
2308 EFI_INVALID_PARAMETER A required parameter was NULL.
2309 EFI_UNSUPPORTED The operation is unsupported.
2313 EFI_IMAGE_DOS_HEADER
*DosHeader
;
2314 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2315 EFI_TE_IMAGE_HEADER
*TeHeader
;
2318 // Verify input parameters
2321 return EFI_INVALID_PARAMETER
;
2325 // First check whether it is one TE Image.
2327 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
2328 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
2330 // By TeImage Header to get output
2332 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2333 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2334 *MachineType
= TeHeader
->Machine
;
2338 // Then check whether
2339 // First is the DOS header
2341 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2344 // Verify DOS header is expected
2346 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2347 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
2348 return EFI_UNSUPPORTED
;
2351 // Immediately following is the NT header.
2353 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2356 // Verify NT header is expected
2358 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2359 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
2360 return EFI_UNSUPPORTED
;
2365 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
2366 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
2367 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
2371 // Verify machine type is supported
2373 if ((*MachineType
!= EFI_IMAGE_MACHINE_IA32
) && (*MachineType
!= EFI_IMAGE_MACHINE_X64
) && (*MachineType
!= EFI_IMAGE_MACHINE_EBC
) &&
2374 (*MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (*MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
2375 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
2376 return EFI_UNSUPPORTED
;
2384 IN CHAR8
*InfFileImage
,
2385 IN UINTN InfFileSize
,
2386 IN CHAR8
*FvFileName
,
2387 IN CHAR8
*MapFileName
2391 Routine Description:
2393 This is the main function which will be called from application.
2397 InfFileImage Buffer containing the INF file contents.
2398 InfFileSize Size of the contents of the InfFileImage buffer.
2399 FvFileName Requested name for the FV file.
2400 MapFileName Fv map file to log fv driver information.
2404 EFI_SUCCESS Function completed successfully.
2405 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2406 EFI_ABORTED Error encountered.
2407 EFI_INVALID_PARAMETER A required parameter was NULL.
2412 MEMORY_FILE InfMemoryFile
;
2413 MEMORY_FILE FvImageMemoryFile
;
2415 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2416 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2417 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
2423 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2424 FILE *FvExtHeaderFile
;
2426 CHAR8
*FvReportName
;
2429 FvBufferHeader
= NULL
;
2433 FvReportName
= NULL
;
2434 FvReportFile
= NULL
;
2436 if (InfFileImage
!= NULL
) {
2438 // Initialize file structures
2440 InfMemoryFile
.FileImage
= InfFileImage
;
2441 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2442 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2445 // Parse the FV inf file for header information
2447 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2448 if (EFI_ERROR (Status
)) {
2449 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2455 // Update the file name return values
2457 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2458 FvFileName
= mFvDataInfo
.FvName
;
2461 if (FvFileName
== NULL
) {
2462 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2466 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2467 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2472 // Debug message Fv File System Guid
2474 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2475 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2476 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2477 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2478 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2479 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2480 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2481 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2482 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2483 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2484 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2485 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2486 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2490 // Add PI FV extension header
2493 FvExtHeaderFile
= NULL
;
2494 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2496 // Open the FV Extension Header file
2498 FvExtHeaderFile
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2499 if (FvExtHeaderFile
== NULL
) {
2500 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2505 // Get the file size
2507 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2510 // Allocate a buffer for the FV Extension Header
2512 FvExtHeader
= malloc(FileSize
);
2513 if (FvExtHeader
== NULL
) {
2514 fclose (FvExtHeaderFile
);
2515 return EFI_OUT_OF_RESOURCES
;
2519 // Read the FV Extension Header
2521 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2522 fclose (FvExtHeaderFile
);
2525 // See if there is an override for the FV Name GUID
2527 if (mFvDataInfo
.FvNameGuidSet
) {
2528 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2530 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2531 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2532 } else if (mFvDataInfo
.FvNameGuidSet
) {
2534 // Allocate a buffer for the FV Extension Header
2536 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2537 if (FvExtHeader
== NULL
) {
2538 return EFI_OUT_OF_RESOURCES
;
2540 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2541 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2545 // Debug message Fv Name Guid
2547 if (mFvDataInfo
.FvNameGuidSet
) {
2548 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2549 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2550 mFvDataInfo
.FvNameGuid
.Data2
,
2551 mFvDataInfo
.FvNameGuid
.Data3
,
2552 mFvDataInfo
.FvNameGuid
.Data4
[0],
2553 mFvDataInfo
.FvNameGuid
.Data4
[1],
2554 mFvDataInfo
.FvNameGuid
.Data4
[2],
2555 mFvDataInfo
.FvNameGuid
.Data4
[3],
2556 mFvDataInfo
.FvNameGuid
.Data4
[4],
2557 mFvDataInfo
.FvNameGuid
.Data4
[5],
2558 mFvDataInfo
.FvNameGuid
.Data4
[6],
2559 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2562 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0 ||
2563 CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem3Guid
) == 0) {
2564 mFvDataInfo
.IsPiFvImage
= TRUE
;
2568 // FvMap file to log the function address of all modules in one Fvimage
2570 if (MapFileName
!= NULL
) {
2571 if (strlen (MapFileName
) > MAX_LONG_FILE_PATH
- 1) {
2572 Error (NULL
, 0, 1003, "Invalid option value", "MapFileName %s is too long!", MapFileName
);
2573 Status
= EFI_ABORTED
;
2577 FvMapName
= malloc (strlen (MapFileName
) + 1);
2578 if (FvMapName
== NULL
) {
2579 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2580 Status
= EFI_OUT_OF_RESOURCES
;
2584 strcpy (FvMapName
, MapFileName
);
2586 if (strlen (FvFileName
) + strlen (".map") > MAX_LONG_FILE_PATH
- 1) {
2587 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2588 Status
= EFI_ABORTED
;
2592 FvMapName
= malloc (strlen (FvFileName
) + strlen (".map") + 1);
2593 if (FvMapName
== NULL
) {
2594 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2595 Status
= EFI_OUT_OF_RESOURCES
;
2599 strcpy (FvMapName
, FvFileName
);
2600 strcat (FvMapName
, ".map");
2602 VerboseMsg ("FV Map file name is %s", FvMapName
);
2605 // FvReport file to log the FV information in one Fvimage
2607 if (strlen (FvFileName
) + strlen (".txt") > MAX_LONG_FILE_PATH
- 1) {
2608 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2609 Status
= EFI_ABORTED
;
2613 FvReportName
= malloc (strlen (FvFileName
) + strlen (".txt") + 1);
2614 if (FvReportName
== NULL
) {
2615 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2616 Status
= EFI_OUT_OF_RESOURCES
;
2620 strcpy (FvReportName
, FvFileName
);
2621 strcat (FvReportName
, ".txt");
2624 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2625 // And Update mFvDataInfo data.
2627 Status
= CalculateFvSize (&mFvDataInfo
);
2628 if (EFI_ERROR (Status
)) {
2631 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2634 // support fv image and empty fv image
2636 FvImageSize
= mFvDataInfo
.Size
;
2639 // Allocate the FV, assure FvImage Header 8 byte alignment
2641 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2642 if (FvBufferHeader
== NULL
) {
2643 Status
= EFI_OUT_OF_RESOURCES
;
2646 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2649 // Initialize the FV to the erase polarity
2651 if (mFvDataInfo
.FvAttributes
== 0) {
2653 // Set Default Fv Attribute
2655 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2657 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2658 memset (FvImage
, -1, FvImageSize
);
2660 memset (FvImage
, 0, FvImageSize
);
2664 // Initialize FV header
2666 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2669 // Initialize the zero vector to all zeros.
2671 memset (FvHeader
->ZeroVector
, 0, 16);
2674 // Copy the Fv file system GUID
2676 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2678 FvHeader
->FvLength
= FvImageSize
;
2679 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2680 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2681 FvHeader
->Revision
= EFI_FVH_REVISION
;
2682 FvHeader
->ExtHeaderOffset
= 0;
2683 FvHeader
->Reserved
[0] = 0;
2686 // Copy firmware block map
2688 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2689 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2690 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2694 // Add block map terminator
2696 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2697 FvHeader
->BlockMap
[Index
].Length
= 0;
2700 // Complete the header
2702 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2703 FvHeader
->Checksum
= 0;
2704 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2707 // If there is no FFS file, generate one empty FV
2709 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2714 // Initialize our "file" view of the buffer
2716 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2717 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2718 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2721 // Initialize the FV library.
2723 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2726 // Initialize the VTF file address.
2728 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2733 FvMapFile
= fopen (LongFilePath (FvMapName
), "w");
2734 if (FvMapFile
== NULL
) {
2735 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2736 Status
= EFI_ABORTED
;
2741 // Open FvReport file
2743 FvReportFile
= fopen (LongFilePath (FvReportName
), "w");
2744 if (FvReportFile
== NULL
) {
2745 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2746 Status
= EFI_ABORTED
;
2750 // record FV size information into FvMap file.
2752 if (mFvTotalSize
!= 0) {
2753 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2754 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2756 if (mFvTakenSize
!= 0) {
2757 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2758 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2760 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2761 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2762 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2766 // record FV size information to FvReportFile.
2768 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2769 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2772 // Add PI FV extension header
2774 if (FvExtHeader
!= NULL
) {
2776 // Add FV Extended Header contents to the FV as a PAD file
2778 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2781 // Fv Extension header change update Fv Header Check sum
2783 FvHeader
->Checksum
= 0;
2784 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2790 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2794 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2797 // Exit if error detected while adding the file
2799 if (EFI_ERROR (Status
)) {
2805 // If there is a VTF file, some special actions need to occur.
2807 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2809 // Pad from the end of the last file to the beginning of the VTF file.
2810 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2812 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2813 if (EFI_ERROR (Status
)) {
2814 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2819 // Update reset vector (SALE_ENTRY for IPF)
2820 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2821 // EndAddress of 0xFFFFFFFF (unless the section was rebased).
2822 // Thus, only this type fv needs to update the reset vector.
2823 // If the PEI Core is found, the VTF file will probably get
2824 // corrupted by updating the entry point.
2826 if (mFvDataInfo
.ForceRebase
== 1 ||
2827 (mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2828 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2829 if (EFI_ERROR(Status
)) {
2830 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2833 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2839 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2840 if (EFI_ERROR (Status
)) {
2841 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2846 // Update Checksum for FvHeader
2848 FvHeader
->Checksum
= 0;
2849 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2853 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2855 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2856 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2857 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2859 // Update Checksum for FvHeader
2861 FvHeader
->Checksum
= 0;
2862 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2866 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2868 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2869 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2870 FvHeader
->Checksum
= 0;
2871 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2878 FvFile
= fopen (LongFilePath (FvFileName
), "wb");
2879 if (FvFile
== NULL
) {
2880 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2881 Status
= EFI_ABORTED
;
2885 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2886 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2887 Status
= EFI_ABORTED
;
2892 if (FvBufferHeader
!= NULL
) {
2893 free (FvBufferHeader
);
2896 if (FvExtHeader
!= NULL
) {
2900 if (FvMapName
!= NULL
) {
2904 if (FvReportName
!= NULL
) {
2905 free (FvReportName
);
2908 if (FvFile
!= NULL
) {
2913 if (FvMapFile
!= NULL
) {
2918 if (FvReportFile
!= NULL
) {
2919 fflush (FvReportFile
);
2920 fclose (FvReportFile
);
2926 UpdatePeiCoreEntryInFit (
2927 IN FIT_TABLE
*FitTablePtr
,
2928 IN UINT64 PeiCorePhysicalAddress
2932 Routine Description:
2934 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2939 FitTablePtr - The pointer of FIT_TABLE.
2940 PeiCorePhysicalAddress - The address of Pei Core entry.
2944 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2945 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2949 FIT_TABLE
*TmpFitPtr
;
2951 UINTN NumFitComponents
;
2953 TmpFitPtr
= FitTablePtr
;
2954 NumFitComponents
= TmpFitPtr
->CompSize
;
2956 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2957 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2958 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2965 return EFI_NOT_FOUND
;
2970 IN FIT_TABLE
*FitTablePtr
2974 Routine Description:
2976 This function is used to update the checksum for FIT.
2981 FitTablePtr - The pointer of FIT_TABLE.
2989 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2990 FitTablePtr
->CheckSum
= 0;
2991 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
3000 Routine Description:
3001 Calculate the FV size and Update Fv Size based on the actual FFS files.
3002 And Update FvInfo data.
3005 FvInfoPtr - The pointer to FV_INFO structure.
3008 EFI_ABORTED - Ffs Image Error
3009 EFI_SUCCESS - Successfully update FvSize
3012 UINTN CurrentOffset
;
3016 UINTN FvExtendHeaderSize
;
3017 UINT32 FfsAlignment
;
3018 UINT32 FfsHeaderSize
;
3019 EFI_FFS_FILE_HEADER FfsHeader
;
3022 FvExtendHeaderSize
= 0;
3028 // Compute size for easy access later
3030 FvInfoPtr
->Size
= 0;
3031 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
3032 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
3036 // Calculate the required sizes for all FFS files.
3038 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
3040 for (Index
= 1;; Index
++) {
3041 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
3042 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
3048 // Calculate PI extension header
3050 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
3051 fpin
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
3053 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
3056 FvExtendHeaderSize
= _filelength (fileno (fpin
));
3058 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
3059 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
3062 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
3064 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3065 } else if (mFvDataInfo
.FvNameGuidSet
) {
3066 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
3067 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3071 // Accumlate every FFS file size.
3073 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
3078 fpin
= fopen (LongFilePath (FvInfoPtr
->FvFiles
[Index
]), "rb");
3080 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
3084 // Get the file size
3086 FfsFileSize
= _filelength (fileno (fpin
));
3087 if (FfsFileSize
>= MAX_FFS_SIZE
) {
3088 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
3091 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
3094 // Read Ffs File header
3096 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
3102 if (FvInfoPtr
->IsPiFvImage
) {
3104 // Check whether this ffs file is vtf file
3106 if (IsVtfFile (&FfsHeader
)) {
3109 // One Fv image can't have two vtf files.
3111 Error (NULL
, 0, 3000,"Invalid", "One Fv image can't have two vtf files.");
3115 VtfFileSize
= FfsFileSize
;
3120 // Get the alignment of FFS file
3122 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
3123 FfsAlignment
= 1 << FfsAlignment
;
3127 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
3129 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
3131 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
3132 CurrentOffset
-= FfsHeaderSize
;
3137 // Add ffs file size
3139 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
3140 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
3142 CurrentOffset
+= FfsFileSize
;
3146 // Make next ffs file start at QWord Boundry
3148 if (FvInfoPtr
->IsPiFvImage
) {
3149 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3152 CurrentOffset
+= VtfFileSize
;
3153 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
);
3155 if (FvInfoPtr
->Size
== 0) {
3157 // Update FvInfo data
3159 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
3160 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
3161 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
3162 FvInfoPtr
->FvBlocks
[1].Length
= 0;
3163 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
3167 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
);
3168 return EFI_INVALID_PARAMETER
;
3172 // Set Fv Size Information
3174 mFvTotalSize
= FvInfoPtr
->Size
;
3175 mFvTakenSize
= CurrentOffset
;
3181 FfsRebaseImageRead (
3182 IN VOID
*FileHandle
,
3183 IN UINTN FileOffset
,
3184 IN OUT UINT32
*ReadSize
,
3189 Routine Description:
3191 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
3195 FileHandle - The handle to the PE/COFF file
3197 FileOffset - The offset, in bytes, into the file to read
3199 ReadSize - The number of bytes to read from the file starting at FileOffset
3201 Buffer - A pointer to the buffer to read the data into.
3205 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
3209 CHAR8
*Destination8
;
3213 Destination8
= Buffer
;
3214 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
3217 *(Destination8
++) = *(Source8
++);
3226 IN EFI_FFS_FILE_HEADER
*FfsFile
,
3231 Routine Description:
3233 This function gets all child FvImages in the input FfsFile, and records
3234 their base address to the parent image.
3237 FvInfo A pointer to FV_INFO struture.
3238 FfsFile A pointer to Ffs file image that may contain FvImage.
3239 XipOffset The offset address to the parent FvImage base.
3243 EFI_SUCCESS Base address of child Fv image is recorded.
3248 EFI_FILE_SECTION_POINTER SubFvSection
;
3249 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
3250 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
3251 EFI_FILE_SECTION_POINTER CorePe32
;
3254 for (Index
= 1;; Index
++) {
3258 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
3259 if (EFI_ERROR (Status
)) {
3262 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
3265 // See if there's an SEC core in the child FV
3266 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_SECURITY_CORE
, &CorePe32
);
3268 // if we couldn't find the SEC core, look for a PEI core
3269 if (EFI_ERROR(Status
)) {
3270 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_PEI_CORE
, &CorePe32
);
3273 if (!EFI_ERROR(Status
)) {
3274 Status
= GetCoreMachineType(CorePe32
, &MachineType
);
3275 if (EFI_ERROR(Status
)) {
3276 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC/PEI Core.");
3280 // machine type is ARM, set a flag so ARM reset vector procesing occurs
3281 if ((MachineType
== EFI_IMAGE_MACHINE_ARMT
) || (MachineType
== EFI_IMAGE_MACHINE_AARCH64
)) {
3282 VerboseMsg("Located ARM/AArch64 SEC/PEI core in child FV");
3290 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
3291 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
3299 IN OUT FV_INFO
*FvInfo
,
3301 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
3307 Routine Description:
3309 This function determines if a file is XIP and should be rebased. It will
3310 rebase any PE32 sections found in the file using the base address.
3314 FvInfo A pointer to FV_INFO struture.
3315 FileName Ffs File PathName
3316 FfsFile A pointer to Ffs file image.
3317 XipOffset The offset address to use for rebasing the XIP file image.
3318 FvMapFile FvMapFile to record the function address in one Fvimage
3322 EFI_SUCCESS The image was properly rebased.
3323 EFI_INVALID_PARAMETER An input parameter is invalid.
3324 EFI_ABORTED An error occurred while rebasing the input file image.
3325 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
3326 EFI_NOT_FOUND No compressed sections could be found.
3331 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
3332 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
3333 EFI_PHYSICAL_ADDRESS XipBase
;
3334 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
3336 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
3337 EFI_FFS_FILE_STATE SavedState
;
3338 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
3339 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
3340 UINT8
*MemoryImagePointer
;
3341 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
3342 CHAR8 PeFileName
[MAX_LONG_FILE_PATH
];
3345 UINT8
*PeFileBuffer
;
3348 UINT32 FfsHeaderSize
;
3349 UINT32 CurSecHdrSize
;
3352 MemoryImagePointer
= NULL
;
3353 TEImageHeader
= NULL
;
3355 SectionHeader
= NULL
;
3358 PeFileBuffer
= NULL
;
3361 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
3363 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
3368 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
3370 if (FvInfo
->ForceRebase
== 0) {
3375 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
3378 // We only process files potentially containing PE32 sections.
3380 switch (FfsFile
->Type
) {
3381 case EFI_FV_FILETYPE_SECURITY_CORE
:
3382 case EFI_FV_FILETYPE_PEI_CORE
:
3383 case EFI_FV_FILETYPE_PEIM
:
3384 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3385 case EFI_FV_FILETYPE_DRIVER
:
3386 case EFI_FV_FILETYPE_DXE_CORE
:
3388 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
3390 // Rebase the inside FvImage.
3392 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
3395 // Search PE/TE section in FV sectin.
3402 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
3404 // Rebase each PE32 section
3406 Status
= EFI_SUCCESS
;
3407 for (Index
= 1;; Index
++) {
3411 NewPe32BaseAddress
= 0;
3416 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
3417 if (EFI_ERROR (Status
)) {
3420 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3423 // Initialize context
3425 memset (&ImageContext
, 0, sizeof (ImageContext
));
3426 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3427 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3428 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3429 if (EFI_ERROR (Status
)) {
3430 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3434 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3435 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3440 // Keep Image Context for PE image in FV
3442 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3445 // Get File PdbPointer
3447 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3450 // Get PeHeader pointer
3452 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3455 // Calculate the PE32 base address, based on file type
3457 switch (FfsFile
->Type
) {
3458 case EFI_FV_FILETYPE_SECURITY_CORE
:
3459 case EFI_FV_FILETYPE_PEI_CORE
:
3460 case EFI_FV_FILETYPE_PEIM
:
3461 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3463 // Check if section-alignment and file-alignment match or not
3465 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3467 // Xip module has the same section alignment and file alignment.
3469 Error (NULL
, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName
);
3473 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3475 if (ImageContext
.RelocationsStripped
) {
3477 // Construct the original efi file Name
3479 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
3480 Error (NULL
, 0, 2000, "Invalid", "The file name %s is too long.", FileName
);
3483 strncpy (PeFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
3484 PeFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
3485 Cptr
= PeFileName
+ strlen (PeFileName
);
3486 while (*Cptr
!= '.') {
3490 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3498 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3499 if (PeFile
== NULL
) {
3500 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3501 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3502 //return EFI_ABORTED;
3506 // Get the file size
3508 PeFileSize
= _filelength (fileno (PeFile
));
3509 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3510 if (PeFileBuffer
== NULL
) {
3512 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3513 return EFI_OUT_OF_RESOURCES
;
3518 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3524 // Handle pointer to the original efi image.
3526 ImageContext
.Handle
= PeFileBuffer
;
3527 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3528 if (EFI_ERROR (Status
)) {
3529 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3532 ImageContext
.RelocationsStripped
= FALSE
;
3535 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3538 case EFI_FV_FILETYPE_DRIVER
:
3539 case EFI_FV_FILETYPE_DXE_CORE
:
3541 // Check if section-alignment and file-alignment match or not
3543 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3545 // Xip module has the same section alignment and file alignment.
3547 Error (NULL
, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName
);
3550 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3555 // Not supported file type
3561 // Relocation doesn't exist
3563 if (ImageContext
.RelocationsStripped
) {
3564 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3569 // Relocation exist and rebase
3572 // Load and Relocate Image Data
3574 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3575 if (MemoryImagePointer
== NULL
) {
3576 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3577 return EFI_OUT_OF_RESOURCES
;
3579 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3580 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3582 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3583 if (EFI_ERROR (Status
)) {
3584 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3585 free ((VOID
*) MemoryImagePointer
);
3589 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3590 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3591 if (EFI_ERROR (Status
)) {
3592 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3593 free ((VOID
*) MemoryImagePointer
);
3598 // Copy Relocated data to raw image file.
3600 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3603 sizeof (EFI_IMAGE_FILE_HEADER
) +
3604 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3607 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3609 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3610 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3611 SectionHeader
->SizeOfRawData
3615 free ((VOID
*) MemoryImagePointer
);
3616 MemoryImagePointer
= NULL
;
3617 if (PeFileBuffer
!= NULL
) {
3618 free (PeFileBuffer
);
3619 PeFileBuffer
= NULL
;
3623 // Update Image Base Address
3625 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3626 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3627 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3628 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3630 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3631 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3638 // Now update file checksum
3640 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3641 SavedState
= FfsFile
->State
;
3642 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3644 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3645 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3646 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3648 FfsFile
->State
= SavedState
;
3652 // Get this module function address from ModulePeMapFile and add them into FvMap file
3656 // Default use FileName as map file path
3658 if (PdbPointer
== NULL
) {
3659 PdbPointer
= FileName
;
3662 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3665 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3666 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3667 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3668 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3669 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3672 // Only Peim code may have a TE section
3678 // Now process TE sections
3680 for (Index
= 1;; Index
++) {
3681 NewPe32BaseAddress
= 0;
3686 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3687 if (EFI_ERROR (Status
)) {
3691 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3694 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3697 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3700 // Initialize context, load image info.
3702 memset (&ImageContext
, 0, sizeof (ImageContext
));
3703 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3704 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3705 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3706 if (EFI_ERROR (Status
)) {
3707 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3711 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3712 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3717 // Keep Image Context for TE image in FV
3719 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3722 // Get File PdbPointer
3724 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3727 // Set new rebased address.
3729 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3730 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3733 // if reloc is stripped, try to get the original efi image to get reloc info.
3735 if (ImageContext
.RelocationsStripped
) {
3737 // Construct the original efi file name
3739 if (strlen (FileName
) >= MAX_LONG_FILE_PATH
) {
3740 Error (NULL
, 0, 2000, "Invalid", "The file name %s is too long.", FileName
);
3743 strncpy (PeFileName
, FileName
, MAX_LONG_FILE_PATH
- 1);
3744 PeFileName
[MAX_LONG_FILE_PATH
- 1] = 0;
3745 Cptr
= PeFileName
+ strlen (PeFileName
);
3746 while (*Cptr
!= '.') {
3751 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3760 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3761 if (PeFile
== NULL
) {
3762 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3763 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3764 //return EFI_ABORTED;
3767 // Get the file size
3769 PeFileSize
= _filelength (fileno (PeFile
));
3770 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3771 if (PeFileBuffer
== NULL
) {
3773 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3774 return EFI_OUT_OF_RESOURCES
;
3779 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3785 // Append reloc section into TeImage
3787 ImageContext
.Handle
= PeFileBuffer
;
3788 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3789 if (EFI_ERROR (Status
)) {
3790 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3793 ImageContext
.RelocationsStripped
= FALSE
;
3797 // Relocation doesn't exist
3799 if (ImageContext
.RelocationsStripped
) {
3800 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3805 // Relocation exist and rebase
3808 // Load and Relocate Image Data
3810 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3811 if (MemoryImagePointer
== NULL
) {
3812 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3813 return EFI_OUT_OF_RESOURCES
;
3815 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3816 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3818 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3819 if (EFI_ERROR (Status
)) {
3820 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3821 free ((VOID
*) MemoryImagePointer
);
3825 // Reloacate TeImage
3827 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3828 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3829 if (EFI_ERROR (Status
)) {
3830 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3831 free ((VOID
*) MemoryImagePointer
);
3836 // Copy the relocated image into raw image file.
3838 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3839 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3840 if (!ImageContext
.IsTeImage
) {
3842 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3843 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3844 SectionHeader
->SizeOfRawData
3848 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3849 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3850 SectionHeader
->SizeOfRawData
3856 // Free the allocated memory resource
3858 free ((VOID
*) MemoryImagePointer
);
3859 MemoryImagePointer
= NULL
;
3860 if (PeFileBuffer
!= NULL
) {
3861 free (PeFileBuffer
);
3862 PeFileBuffer
= NULL
;
3866 // Update Image Base Address
3868 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3871 // Now update file checksum
3873 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3874 SavedState
= FfsFile
->State
;
3875 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3877 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3878 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3879 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3881 FfsFile
->State
= SavedState
;
3884 // Get this module function address from ModulePeMapFile and add them into FvMap file
3888 // Default use FileName as map file path
3890 if (PdbPointer
== NULL
) {
3891 PdbPointer
= FileName
;
3907 FindApResetVectorPosition (
3908 IN MEMORY_FILE
*FvImage
,
3913 Routine Description:
3915 Find the position in this FvImage to place Ap reset vector.
3919 FvImage Memory file for the FV memory image.
3920 Pointer Pointer to pointer to position.
3924 EFI_NOT_FOUND - No satisfied position is found.
3925 EFI_SUCCESS - The suitable position is return.
3929 EFI_FFS_FILE_HEADER
*PadFile
;
3935 for (Index
= 1; ;Index
++) {
3937 // Find Pad File to add ApResetVector info
3939 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3940 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3942 // No Pad file to be found.
3947 // Get Pad file size.
3949 FileLength
= GetFfsFileLength(PadFile
);
3950 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3952 // FixPoint must be align on 0x1000 relative to FvImage Header
3954 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3955 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3957 // FixPoint be larger at the last place of one fv image.
3959 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3964 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3966 // No alignment FixPoint in this Pad File.
3971 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3973 // Find the position to place ApResetVector
3975 *Pointer
= FixPoint
;
3980 return EFI_NOT_FOUND
;
3985 IN MEMORY_FILE
*InfFile
,
3986 OUT CAP_INFO
*CapInfo
3990 Routine Description:
3992 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3996 InfFile Memory file image.
3997 CapInfo Information read from INF file.
4001 EFI_SUCCESS INF file information successfully retrieved.
4002 EFI_ABORTED INF file has an invalid format.
4003 EFI_NOT_FOUND A required string was not found in the INF file.
4006 CHAR8 Value
[MAX_LONG_FILE_PATH
];
4008 UINTN Index
, Number
;
4012 // Initialize Cap info
4014 // memset (CapInfo, 0, sizeof (CAP_INFO));
4018 // Read the Capsule Guid
4020 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
4021 if (Status
== EFI_SUCCESS
) {
4023 // Get the Capsule Guid
4025 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
4026 if (EFI_ERROR (Status
)) {
4027 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4030 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4034 // Read the Capsule Header Size
4036 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
4037 if (Status
== EFI_SUCCESS
) {
4038 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4039 if (EFI_ERROR (Status
)) {
4040 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4043 CapInfo
->HeaderSize
= (UINT32
) Value64
;
4044 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4048 // Read the Capsule Flag
4050 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
4051 if (Status
== EFI_SUCCESS
) {
4052 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
4053 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
4054 if (strstr (Value
, "InitiateReset") != NULL
) {
4055 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4057 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
4058 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
4059 if (strstr (Value
, "InitiateReset") != NULL
) {
4060 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4063 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
4066 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
4069 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
4070 if (Status
== EFI_SUCCESS
) {
4071 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4072 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
4073 Error (NULL
, 0, 2000, "Invalid parameter",
4074 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
4075 EFI_OEM_CAPSULE_FLAGS_STRING
);
4078 CapInfo
->Flags
|= Value64
;
4079 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
4083 // Read Capsule File name
4085 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
4086 if (Status
== EFI_SUCCESS
) {
4088 // Get output file name
4090 strcpy (CapInfo
->CapName
, Value
);
4094 // Read the Capsule FileImage
4097 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
4098 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
4102 // Read the capsule file name
4104 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
4106 if (Status
== EFI_SUCCESS
) {
4110 strcpy (CapInfo
->CapFiles
[Index
], Value
);
4111 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
4118 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
4126 IN CHAR8
*InfFileImage
,
4127 IN UINTN InfFileSize
,
4128 IN CHAR8
*CapFileName
4132 Routine Description:
4134 This is the main function which will be called from application to create UEFI Capsule image.
4138 InfFileImage Buffer containing the INF file contents.
4139 InfFileSize Size of the contents of the InfFileImage buffer.
4140 CapFileName Requested name for the Cap file.
4144 EFI_SUCCESS Function completed successfully.
4145 EFI_OUT_OF_RESOURCES Could not allocate required resources.
4146 EFI_ABORTED Error encountered.
4147 EFI_INVALID_PARAMETER A required parameter was NULL.
4153 EFI_CAPSULE_HEADER
*CapsuleHeader
;
4154 MEMORY_FILE InfMemoryFile
;
4160 if (InfFileImage
!= NULL
) {
4162 // Initialize file structures
4164 InfMemoryFile
.FileImage
= InfFileImage
;
4165 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
4166 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
4169 // Parse the Cap inf file for header information
4171 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
4172 if (Status
!= EFI_SUCCESS
) {
4177 if (mCapDataInfo
.HeaderSize
== 0) {
4179 // make header size align 16 bytes.
4181 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
4182 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
4185 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
4186 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
4187 return EFI_INVALID_PARAMETER
;
4190 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
4191 CapFileName
= mCapDataInfo
.CapName
;
4194 if (CapFileName
== NULL
) {
4195 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
4196 return EFI_INVALID_PARAMETER
;
4200 // Set Default Capsule Guid value
4202 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
4203 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
4206 // Calculate the size of capsule image.
4210 CapSize
= mCapDataInfo
.HeaderSize
;
4211 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4212 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4214 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4217 FileSize
= _filelength (fileno (fpin
));
4218 CapSize
+= FileSize
;
4224 // Allocate buffer for capsule image.
4226 CapBuffer
= (UINT8
*) malloc (CapSize
);
4227 if (CapBuffer
== NULL
) {
4228 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
4229 return EFI_OUT_OF_RESOURCES
;
4233 // Initialize the capsule header to zero
4235 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
4238 // create capsule header and get capsule body
4240 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
4241 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
4242 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
4243 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
4244 CapsuleHeader
->CapsuleImageSize
= CapSize
;
4248 CapSize
= CapsuleHeader
->HeaderSize
;
4249 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4250 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4252 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4256 FileSize
= _filelength (fileno (fpin
));
4257 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
4260 CapSize
+= FileSize
;
4264 // write capsule data into the output file
4266 fpout
= fopen (LongFilePath (CapFileName
), "wb");
4267 if (fpout
== NULL
) {
4268 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
4273 fwrite (CapBuffer
, 1, CapSize
, fpout
);
4277 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);