2 This file contains the internal functions required to generate a Firmware Volume.
4 Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
5 Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #if defined(__FreeBSD__)
22 #elif defined(__GNUC__)
23 #include <uuid/uuid.h>
34 #include <Guid/FfsSectionAlignmentPadding.h>
36 #include "GenFvInternalLib.h"
38 #include "PeCoffLib.h"
39 #include "WinNtInclude.h"
42 STATIC UINT32 MaxFfsAlignment
= 0;
44 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
45 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
46 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
47 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
48 EFI_GUID mEfiFfsSectionAlignmentPaddingGuid
= EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID
;
50 CHAR8
*mFvbAttributeName
[] = {
51 EFI_FVB2_READ_DISABLED_CAP_STRING
,
52 EFI_FVB2_READ_ENABLED_CAP_STRING
,
53 EFI_FVB2_READ_STATUS_STRING
,
54 EFI_FVB2_WRITE_DISABLED_CAP_STRING
,
55 EFI_FVB2_WRITE_ENABLED_CAP_STRING
,
56 EFI_FVB2_WRITE_STATUS_STRING
,
57 EFI_FVB2_LOCK_CAP_STRING
,
58 EFI_FVB2_LOCK_STATUS_STRING
,
60 EFI_FVB2_STICKY_WRITE_STRING
,
61 EFI_FVB2_MEMORY_MAPPED_STRING
,
62 EFI_FVB2_ERASE_POLARITY_STRING
,
63 EFI_FVB2_READ_LOCK_CAP_STRING
,
64 EFI_FVB2_READ_LOCK_STATUS_STRING
,
65 EFI_FVB2_WRITE_LOCK_CAP_STRING
,
66 EFI_FVB2_WRITE_LOCK_STATUS_STRING
69 CHAR8
*mFvbAlignmentName
[] = {
70 EFI_FVB2_ALIGNMENT_1_STRING
,
71 EFI_FVB2_ALIGNMENT_2_STRING
,
72 EFI_FVB2_ALIGNMENT_4_STRING
,
73 EFI_FVB2_ALIGNMENT_8_STRING
,
74 EFI_FVB2_ALIGNMENT_16_STRING
,
75 EFI_FVB2_ALIGNMENT_32_STRING
,
76 EFI_FVB2_ALIGNMENT_64_STRING
,
77 EFI_FVB2_ALIGNMENT_128_STRING
,
78 EFI_FVB2_ALIGNMENT_256_STRING
,
79 EFI_FVB2_ALIGNMENT_512_STRING
,
80 EFI_FVB2_ALIGNMENT_1K_STRING
,
81 EFI_FVB2_ALIGNMENT_2K_STRING
,
82 EFI_FVB2_ALIGNMENT_4K_STRING
,
83 EFI_FVB2_ALIGNMENT_8K_STRING
,
84 EFI_FVB2_ALIGNMENT_16K_STRING
,
85 EFI_FVB2_ALIGNMENT_32K_STRING
,
86 EFI_FVB2_ALIGNMENT_64K_STRING
,
87 EFI_FVB2_ALIGNMENT_128K_STRING
,
88 EFI_FVB2_ALIGNMENT_256K_STRING
,
89 EFI_FVB2_ALIGNMENT_512K_STRING
,
90 EFI_FVB2_ALIGNMENT_1M_STRING
,
91 EFI_FVB2_ALIGNMENT_2M_STRING
,
92 EFI_FVB2_ALIGNMENT_4M_STRING
,
93 EFI_FVB2_ALIGNMENT_8M_STRING
,
94 EFI_FVB2_ALIGNMENT_16M_STRING
,
95 EFI_FVB2_ALIGNMENT_32M_STRING
,
96 EFI_FVB2_ALIGNMENT_64M_STRING
,
97 EFI_FVB2_ALIGNMENT_128M_STRING
,
98 EFI_FVB2_ALIGNMENT_256M_STRING
,
99 EFI_FVB2_ALIGNMENT_512M_STRING
,
100 EFI_FVB2_ALIGNMENT_1G_STRING
,
101 EFI_FVB2_ALIGNMENT_2G_STRING
105 // This data array will be located at the base of the Firmware Volume Header (FVH)
106 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
107 // will be used to keep the FVH checksum consistent.
108 // This code will be run in response to a starutp IPI for HT-enabled systems.
110 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
112 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
114 // EA D0 FF 00 F0 ; far jmp F000:FFD0
115 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
116 // 0, 0 ; Checksum Padding
136 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
138 // EB CE ; jmp short ($-0x30)
139 // ; (from offset 0x0 to offset 0xFFD0)
140 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
141 // 0, 0 ; Checksum Padding
162 CAP_INFO mCapDataInfo
;
163 BOOLEAN mIsLargeFfs
= FALSE
;
165 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
166 UINT32 mFvBaseAddressNumber
= 0;
170 IN MEMORY_FILE
*InfFile
,
177 This function parses a FV.INF file and copies info into a FV_INFO structure.
181 InfFile Memory file image.
182 FvInfo Information read from INF file.
186 EFI_SUCCESS INF file information successfully retrieved.
187 EFI_ABORTED INF file has an invalid format.
188 EFI_NOT_FOUND A required string was not found in the INF file.
191 CHAR8 Value
[MAX_LONG_FILE_PATH
];
199 // Read the FV base address
201 if (!mFvDataInfo
.BaseAddressSet
) {
202 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
203 if (Status
== EFI_SUCCESS
) {
205 // Get the base address
207 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
208 if (EFI_ERROR (Status
)) {
209 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
212 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
214 FvInfo
->BaseAddress
= Value64
;
215 FvInfo
->BaseAddressSet
= TRUE
;
220 // Read the FV File System Guid
222 if (!FvInfo
->FvFileSystemGuidSet
) {
223 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
224 if (Status
== EFI_SUCCESS
) {
226 // Get the guid value
228 Status
= StringToGuid (Value
, &GuidValue
);
229 if (EFI_ERROR (Status
)) {
230 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
233 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
234 FvInfo
->FvFileSystemGuidSet
= TRUE
;
239 // Read the FV Extension Header File Name
241 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
242 if (Status
== EFI_SUCCESS
) {
243 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
247 // Read the FV file name
249 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
250 if (Status
== EFI_SUCCESS
) {
252 // copy the file name
254 strcpy (FvInfo
->FvName
, Value
);
260 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
261 if ((mFvbAttributeName
[Index
] != NULL
) && \
262 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
263 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
264 FvInfo
->FvAttributes
|= 1 << Index
;
265 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
266 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
275 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
276 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
277 if (strcmp (Value
, TRUE_STRING
) == 0) {
278 FvInfo
->FvAttributes
|= Index
<< 16;
279 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
286 // Read weak alignment flag
288 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_WEAK_ALIGNMENT_STRING
, 0, Value
);
289 if (Status
== EFI_SUCCESS
) {
290 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
291 FvInfo
->FvAttributes
|= EFI_FVB2_WEAK_ALIGNMENT
;
292 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
293 Error (NULL
, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
301 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
302 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
306 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
308 if (Status
== EFI_SUCCESS
) {
310 // Update the size of block
312 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
313 if (EFI_ERROR (Status
)) {
314 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
318 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
319 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
322 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
323 // and should return an error.
325 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
326 if (!EFI_ERROR (Status
)) {
327 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
338 // Read blocks number
340 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
342 if (Status
== EFI_SUCCESS
) {
344 // Update the number of blocks
346 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
347 if (EFI_ERROR (Status
)) {
348 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
352 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
353 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
359 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
367 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
368 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
373 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
375 // Read the FFS file list
377 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
379 if (Status
== EFI_SUCCESS
) {
383 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
384 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
390 if ((Index
+ Number
) == 0) {
391 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
399 IN EFI_FFS_FILE_HEADER
*FfsFile
,
400 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
406 This function changes the FFS file attributes based on the erase polarity
407 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
420 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
421 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
422 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
428 IN EFI_FFS_FILE_HEADER
*FfsFile
,
429 IN OUT UINT32
*Alignment
435 This function determines the alignment of the FFS input file from the file
440 FfsFile FFS file to parse
441 Alignment The minimum required alignment offset of the FFS file
445 EFI_SUCCESS The function completed successfully.
446 EFI_INVALID_PARAMETER One of the input parameters was invalid.
447 EFI_ABORTED An error occurred.
452 // Verify input parameters.
454 if (FfsFile
== NULL
|| Alignment
== NULL
) {
455 return EFI_INVALID_PARAMETER
;
458 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
476 // 128 byte alignment
483 // 512 byte alignment
504 // 32K byte alignment
511 // 64K byte alignment
525 IN OUT MEMORY_FILE
*FvImage
,
526 IN UINT32 DataAlignment
,
528 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
,
529 IN UINT32 NextFfsSize
535 This function adds a pad file to the FV image if it required to align the
536 data of the next file.
540 FvImage The memory image of the FV to add it to.
541 The current offset must be valid.
542 DataAlignment The data alignment of the next FFS file.
543 FvEnd End of the empty data in FvImage.
544 ExtHeader PI FvExtHeader Optional
548 EFI_SUCCESS The function completed successfully.
549 EFI_INVALID_PARAMETER One of the input parameters was invalid.
550 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
555 EFI_FFS_FILE_HEADER
*PadFile
;
557 UINT32 NextFfsHeaderSize
;
558 UINT32 CurFfsHeaderSize
;
560 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
562 // Verify input parameters.
564 if (FvImage
== NULL
) {
565 return EFI_INVALID_PARAMETER
;
569 // Calculate the pad file size
573 // Append extension header size
575 if (ExtHeader
!= NULL
) {
576 PadFileSize
= ExtHeader
->ExtHeaderSize
;
577 if (PadFileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
578 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
580 PadFileSize
+= CurFfsHeaderSize
;
582 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
583 if (NextFfsSize
>= MAX_FFS_SIZE
) {
584 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
587 // Check if a pad file is necessary
589 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ NextFfsHeaderSize
) % DataAlignment
== 0) {
592 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
) + NextFfsHeaderSize
;
594 // Add whatever it takes to get to the next aligned address
596 while ((PadFileSize
% DataAlignment
) != 0) {
600 // Subtract the next file header size
602 PadFileSize
-= NextFfsHeaderSize
;
604 // Subtract the starting offset to get size
606 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
610 // Verify that we have enough space for the file header
612 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
613 return EFI_OUT_OF_RESOURCES
;
617 // Write pad file header
619 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
622 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
624 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
625 PadFile
->Attributes
= 0;
628 // Write pad file size (calculated size minus next file header size)
630 if (PadFileSize
>= MAX_FFS_SIZE
) {
631 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
632 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= PadFileSize
;
633 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
635 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
636 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
637 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
641 // Fill in checksums and state, they must be 0 for checksumming.
643 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
644 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
646 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, CurFfsHeaderSize
);
647 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
649 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
651 (EFI_FFS_FILE_HEADER
*) PadFile
,
652 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
656 // Update the current FV pointer
658 FvImage
->CurrentFilePointer
+= PadFileSize
;
660 if (ExtHeader
!= NULL
) {
662 // Copy Fv Extension Header and Set Fv Extension header offset
664 memcpy ((UINT8
*)PadFile
+ CurFfsHeaderSize
, ExtHeader
, ExtHeader
->ExtHeaderSize
);
665 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) ((UINT8
*)PadFile
+ CurFfsHeaderSize
) - (UINTN
) FvImage
->FileImage
);
667 // Make next file start at QWord Boundry
669 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
670 FvImage
->CurrentFilePointer
++;
679 IN EFI_FFS_FILE_HEADER
*FileBuffer
685 This function checks the header to validate if it is a VTF file
689 FileBuffer Buffer in which content of a file has been read.
693 TRUE If this is a VTF file
694 FALSE If this is not a VTF file
698 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
707 IN OUT
FILE *FvMapFile
,
709 IN EFI_FFS_FILE_HEADER
*FfsFile
,
710 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
711 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
717 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
718 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
722 FvMapFile A pointer to FvMap File
723 FileName Ffs File PathName
724 FfsFile A pointer to Ffs file image.
725 ImageBaseAddress PeImage Base Address.
726 pImageContext Image Context Information.
730 EFI_SUCCESS Added required map information.
734 CHAR8 PeMapFileName
[MAX_LONG_FILE_PATH
];
736 CHAR8 FileGuidName
[MAX_LINE_LEN
];
738 CHAR8 Line
[MAX_LINE_LEN
];
739 CHAR8 KeyWord
[MAX_LINE_LEN
];
740 CHAR8 FunctionName
[MAX_LINE_LEN
];
741 EFI_PHYSICAL_ADDRESS FunctionAddress
;
743 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
745 UINT32 AddressOfEntryPoint
;
747 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
748 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
749 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
750 unsigned long long TempLongAddress
;
751 UINT32 TextVirtualAddress
;
752 UINT32 DataVirtualAddress
;
753 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
756 // Init local variable
760 // Print FileGuid to string buffer.
762 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
765 // Construct Map file Name
767 strcpy (PeMapFileName
, FileName
);
770 // Change '\\' to '/', unified path format.
772 Cptr
= PeMapFileName
;
773 while (*Cptr
!= '\0') {
775 *Cptr
= FILE_SEP_CHAR
;
783 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
784 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
787 if (Cptr
< PeMapFileName
) {
788 return EFI_NOT_FOUND
;
800 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
804 strcpy (KeyWord
, Cptr
+ 1);
808 // AddressOfEntryPoint and Offset in Image
810 if (!pImageContext
->IsTeImage
) {
811 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
812 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
814 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
817 sizeof (EFI_IMAGE_FILE_HEADER
) +
818 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
820 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
822 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
823 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
824 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
825 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
826 Index
= TEImageHeader
->NumberOfSections
;
830 // module information output
832 if (ImageBaseAddress
== 0) {
833 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
834 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
836 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
837 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
840 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&& pImageContext
->Machine
== EFI_IMAGE_MACHINE_IA64
) {
842 // Process IPF PLABEL to get the real address after the image has been rebased.
843 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
845 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64
*)((UINTN
) pImageContext
->Handle
+ (UINTN
) AddressOfEntryPoint
)));
847 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
849 fprintf (FvMapFile
, ")\n");
851 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
852 TextVirtualAddress
= 0;
853 DataVirtualAddress
= 0;
854 for (; Index
> 0; Index
--, SectionHeader
++) {
855 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
856 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
857 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
858 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
859 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
860 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
863 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
864 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
865 fprintf (FvMapFile
, ")\n\n");
870 PeMapFile
= fopen (LongFilePath (PeMapFileName
), "r");
871 if (PeMapFile
== NULL
) {
872 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
875 VerboseMsg ("The map file is %s", PeMapFileName
);
878 // Output Functions information into Fv Map file
880 LinkTimeBaseAddress
= 0;
881 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
885 if (Line
[0] == 0x0a) {
890 // By Address and Static keyword
892 if (FunctionType
== 0) {
893 sscanf (Line
, "%s", KeyWord
);
894 if (stricmp (KeyWord
, "Address") == 0) {
899 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
900 } else if (stricmp (KeyWord
, "Static") == 0) {
902 // static function list
905 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
906 } else if (stricmp (KeyWord
, "Preferred") ==0) {
907 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
908 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
913 // Printf Function Information
915 if (FunctionType
== 1) {
916 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
917 FunctionAddress
= (UINT64
) TempLongAddress
;
918 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
919 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
920 fprintf (FvMapFile
, "%s\n", FunctionName
);
922 } else if (FunctionType
== 2) {
923 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
924 FunctionAddress
= (UINT64
) TempLongAddress
;
925 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
926 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
927 fprintf (FvMapFile
, "%s\n", FunctionName
);
934 fprintf (FvMapFile
, "\n\n");
942 AdjustInternalFfsPadding (
943 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
944 IN OUT MEMORY_FILE
*FvImage
,
946 IN OUT UINTN
*FileSize
952 This function looks for a dedicated alignment padding section in the FFS, and
953 shrinks it to the size required to line up subsequent sections correctly.
957 FfsFile A pointer to Ffs file image.
958 FvImage The memory image of the FV to adjust it to.
959 Alignment Current file alignment
960 FileSize Reference to a variable holding the size of the FFS file
964 TRUE Padding section was found and updated successfully
969 EFI_FILE_SECTION_POINTER PadSection
;
972 UINT32 FfsHeaderLength
;
973 UINT32 FfsFileLength
;
976 EFI_FFS_INTEGRITY_CHECK
*IntegrityCheck
;
979 // Figure out the misalignment: all FFS sections are aligned relative to the
980 // start of the FFS payload, so use that as the base of the misalignment
983 FfsHeaderLength
= GetFfsHeaderLength(FfsFile
);
984 Misalignment
= (UINTN
) FvImage
->CurrentFilePointer
-
985 (UINTN
) FvImage
->FileImage
+ FfsHeaderLength
;
986 Misalignment
&= Alignment
- 1;
987 if (Misalignment
== 0) {
988 // Nothing to do, return success
993 // We only apply this optimization to FFS files with the FIXED attribute set,
994 // since the FFS will not be loadable at arbitrary offsets anymore after
995 // we adjust the size of the padding section.
997 if ((FfsFile
->Attributes
& FFS_ATTRIB_FIXED
) == 0) {
1002 // Look for a dedicated padding section that we can adjust to compensate
1003 // for the misalignment. If such a padding section exists, it precedes all
1004 // sections with alignment requirements, and so the adjustment will correct
1007 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FREEFORM_SUBTYPE_GUID
, 1,
1009 if (EFI_ERROR (Status
) ||
1010 CompareGuid (&PadSection
.FreeformSubtypeSection
->SubTypeGuid
,
1011 &mEfiFfsSectionAlignmentPaddingGuid
) != 0) {
1016 // Find out if the size of the padding section is sufficient to compensate
1017 // for the misalignment.
1019 PadSize
= GetSectionFileLength (PadSection
.CommonHeader
);
1020 if (Misalignment
> PadSize
- sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION
)) {
1025 // Move the remainder of the FFS file towards the front, and adjust the
1026 // file size output parameter.
1028 Remainder
= (UINT8
*) PadSection
.CommonHeader
+ PadSize
;
1029 memmove (Remainder
- Misalignment
, Remainder
,
1030 *FileSize
- (UINTN
) (Remainder
- (UINTN
) FfsFile
));
1031 *FileSize
-= Misalignment
;
1034 // Update the padding section's length with the new values. Note that the
1035 // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2
1038 PadSize
-= Misalignment
;
1039 PadSection
.CommonHeader
->Size
[0] = (UINT8
) (PadSize
& 0xff);
1040 PadSection
.CommonHeader
->Size
[1] = (UINT8
) ((PadSize
& 0xff00) >> 8);
1041 PadSection
.CommonHeader
->Size
[2] = (UINT8
) ((PadSize
& 0xff0000) >> 16);
1044 // Update the FFS header with the new overall length
1046 FfsFileLength
= GetFfsFileLength (FfsFile
) - Misalignment
;
1047 if (FfsHeaderLength
> sizeof(EFI_FFS_FILE_HEADER
)) {
1048 ((EFI_FFS_FILE_HEADER2
*)FfsFile
)->ExtendedSize
= FfsFileLength
;
1050 FfsFile
->Size
[0] = (UINT8
) (FfsFileLength
& 0x000000FF);
1051 FfsFile
->Size
[1] = (UINT8
) ((FfsFileLength
& 0x0000FF00) >> 8);
1052 FfsFile
->Size
[2] = (UINT8
) ((FfsFileLength
& 0x00FF0000) >> 16);
1056 // Clear the alignment bits: these have become meaningless now that we have
1057 // adjusted the padding section.
1059 FfsFile
->Attributes
&= ~FFS_ATTRIB_DATA_ALIGNMENT
;
1062 // Recalculate the FFS header checksum. Instead of setting Header and State
1063 // both to zero, set Header to (UINT8)(-State) so State preserves its original
1066 IntegrityCheck
= &FfsFile
->IntegrityCheck
;
1067 IntegrityCheck
->Checksum
.Header
= (UINT8
) (0x100 - FfsFile
->State
);
1068 IntegrityCheck
->Checksum
.File
= 0;
1070 IntegrityCheck
->Checksum
.Header
= CalculateChecksum8 (
1071 (UINT8
*) FfsFile
, FfsHeaderLength
);
1073 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1075 // Ffs header checksum = zero, so only need to calculate ffs body.
1077 IntegrityCheck
->Checksum
.File
= CalculateChecksum8 (
1078 (UINT8
*) FfsFile
+ FfsHeaderLength
,
1079 FfsFileLength
- FfsHeaderLength
);
1081 IntegrityCheck
->Checksum
.File
= FFS_FIXED_CHECKSUM
;
1089 IN OUT MEMORY_FILE
*FvImage
,
1092 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
1094 IN
FILE *FvReportFile
1098 Routine Description:
1100 This function adds a file to the FV image. The file will pad to the
1101 appropriate alignment if required.
1105 FvImage The memory image of the FV to add it to. The current offset
1107 FvInfo Pointer to information about the FV.
1108 Index The file in the FvInfo file list to add.
1109 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
1110 to the end of the FvImage then no VTF previously found.
1111 FvMapFile Pointer to FvMap File
1112 FvReportFile Pointer to FvReport File
1116 EFI_SUCCESS The function completed successfully.
1117 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1118 EFI_ABORTED An error occurred.
1119 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1127 UINT32 CurrentFileAlignment
;
1130 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
1134 // Verify input parameters.
1136 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
1137 return EFI_INVALID_PARAMETER
;
1141 // Read the file to add
1143 NewFile
= fopen (LongFilePath (FvInfo
->FvFiles
[Index
]), "rb");
1145 if (NewFile
== NULL
) {
1146 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
1151 // Get the file size
1153 FileSize
= _filelength (fileno (NewFile
));
1156 // Read the file into a buffer
1158 FileBuffer
= malloc (FileSize
);
1159 if (FileBuffer
== NULL
) {
1160 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
1161 return EFI_OUT_OF_RESOURCES
;
1164 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1167 // Done with the file, from this point on we will just use the buffer read.
1172 // Verify read successful
1174 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1176 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1181 // For None PI Ffs file, directly add them into FvImage.
1183 if (!FvInfo
->IsPiFvImage
) {
1184 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1185 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1186 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1188 FvImage
->CurrentFilePointer
+= FileSize
;
1196 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1197 if (EFI_ERROR (Status
)) {
1199 Error (NULL
, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo
->FvFiles
[Index
]);
1200 return EFI_INVALID_PARAMETER
;
1204 // Verify space exists to add the file
1206 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1208 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1209 return EFI_OUT_OF_RESOURCES
;
1213 // Verify the input file is the duplicated file in this Fv image
1215 for (Index1
= 0; Index1
< Index
; Index1
++) {
1216 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1217 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1218 PrintGuid ((EFI_GUID
*) FileBuffer
);
1219 return EFI_INVALID_PARAMETER
;
1222 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1225 // Update the file state based on polarity of the FV.
1227 UpdateFfsFileState (
1228 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1229 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1233 // Check if alignment is required
1235 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1238 // Find the largest alignment of all the FFS files in the FV
1240 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1241 MaxFfsAlignment
= CurrentFileAlignment
;
1244 // If we have a VTF file, add it at the top.
1246 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1247 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1249 // No previous VTF, add this one.
1251 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1253 // Sanity check. The file MUST align appropriately
1255 if (((UINTN
) *VtfFileImage
+ GetFfsHeaderLength((EFI_FFS_FILE_HEADER
*)FileBuffer
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1256 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1261 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1262 // Rebase for the debug genfvmap tool
1264 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1265 if (EFI_ERROR (Status
)) {
1266 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1272 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1274 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1275 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1278 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1282 // Already found a VTF file.
1284 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1291 // Add pad file if necessary
1293 if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER
*) FileBuffer
, FvImage
,
1294 1 << CurrentFileAlignment
, &FileSize
)) {
1295 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
, FileSize
);
1296 if (EFI_ERROR (Status
)) {
1297 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1305 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1307 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1308 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1310 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1311 if (EFI_ERROR (Status
)) {
1312 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1318 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1319 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1320 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1321 FvImage
->CurrentFilePointer
+= FileSize
;
1323 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1328 // Make next file start at QWord Boundry
1330 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1331 FvImage
->CurrentFilePointer
++;
1336 // Free allocated memory.
1345 IN MEMORY_FILE
*FvImage
,
1346 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1350 Routine Description:
1352 This function places a pad file between the last file in the FV and the VTF
1353 file if the VTF file exists.
1357 FvImage Memory file for the FV memory image
1358 VtfFileImage The address of the VTF file. If this is the end of the FV
1359 image, no VTF exists and no pad file is needed.
1363 EFI_SUCCESS Completed successfully.
1364 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1368 EFI_FFS_FILE_HEADER
*PadFile
;
1370 UINT32 FfsHeaderSize
;
1373 // If there is no VTF or the VTF naturally follows the previous file without a
1374 // pad file, then there's nothing to do
1376 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1377 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1381 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1382 return EFI_INVALID_PARAMETER
;
1386 // Pad file starts at beginning of free space
1388 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1391 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1393 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1394 PadFile
->Attributes
= 0;
1397 // FileSize includes the EFI_FFS_FILE_HEADER
1399 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1400 if (FileSize
>= MAX_FFS_SIZE
) {
1401 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
1402 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
1403 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= FileSize
;
1404 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
1407 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1408 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1409 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1410 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
1414 // Fill in checksums and state, must be zero during checksum calculation.
1416 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1417 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1419 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, FfsHeaderSize
);
1420 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1422 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1424 UpdateFfsFileState (
1425 (EFI_FFS_FILE_HEADER
*) PadFile
,
1426 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1429 // Update the current FV pointer
1431 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1438 IN MEMORY_FILE
*FvImage
,
1440 IN EFI_FFS_FILE_HEADER
*VtfFile
1444 Routine Description:
1446 This parses the FV looking for the PEI core and then plugs the address into
1447 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1448 complete an IA32 Bootstrap FV.
1452 FvImage Memory file for the FV memory image
1453 FvInfo Information read from INF file.
1454 VtfFile Pointer to the VTF file in the FV image.
1458 EFI_SUCCESS Function Completed successfully.
1459 EFI_ABORTED Error encountered.
1460 EFI_INVALID_PARAMETER A required parameter was NULL.
1461 EFI_NOT_FOUND PEI Core file not found.
1465 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1466 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1468 EFI_FILE_SECTION_POINTER Pe32Section
;
1472 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1473 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1474 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
1475 INT32 Ia32SecEntryOffset
;
1476 UINT32
*Ia32ResetAddressPtr
;
1478 UINT8
*BytePointer2
;
1479 UINT16
*WordPointer
;
1483 EFI_FFS_FILE_STATE SavedState
;
1485 FIT_TABLE
*FitTablePtr
;
1486 BOOLEAN Vtf0Detected
;
1487 UINT32 FfsHeaderSize
;
1488 UINT32 SecHeaderSize
;
1491 // Verify input parameters
1493 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1494 return EFI_INVALID_PARAMETER
;
1497 // Initialize FV library
1499 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1504 Status
= VerifyFfsFile (VtfFile
);
1505 if (EFI_ERROR (Status
)) {
1506 return EFI_INVALID_PARAMETER
;
1510 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1511 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1512 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1513 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1514 IA32_X64_VTF0_SIGNATURE
)
1516 Vtf0Detected
= TRUE
;
1518 Vtf0Detected
= FALSE
;
1522 // Find the Sec Core
1524 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1525 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1528 // If the SEC core file is not found, but the VTF-0 signature
1529 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1530 // This means no modifications are required to the VTF.
1535 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1539 // Sec Core found, now find PE32 section
1541 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1542 if (Status
== EFI_NOT_FOUND
) {
1543 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1546 if (EFI_ERROR (Status
)) {
1547 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1551 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1552 Status
= GetPe32Info (
1553 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1559 if (EFI_ERROR (Status
)) {
1560 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1566 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1567 MachineType
== EFI_IMAGE_MACHINE_X64
)
1570 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1571 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1572 // This means no modifications are required to the VTF.
1578 // Physical address is FV base + offset of PE32 + offset of the entry point
1580 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1581 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1582 SecCorePhysicalAddress
+= EntryPoint
;
1583 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1586 // Find the PEI Core
1588 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1589 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
1590 Error (NULL
, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1594 // PEI Core found, now find PE32 or TE section
1596 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1597 if (Status
== EFI_NOT_FOUND
) {
1598 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1601 if (EFI_ERROR (Status
)) {
1602 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1606 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1607 Status
= GetPe32Info (
1608 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1614 if (EFI_ERROR (Status
)) {
1615 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1619 // Physical address is FV base + offset of PE32 + offset of the entry point
1621 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1622 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1623 PeiCorePhysicalAddress
+= EntryPoint
;
1624 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1626 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
1628 // Update PEI_CORE address
1631 // Set the uncached attribute bit in the physical address
1633 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
1636 // Check if address is aligned on a 16 byte boundary
1638 if (PeiCorePhysicalAddress
& 0xF) {
1639 Error (NULL
, 0, 3000, "Invalid",
1640 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1641 (unsigned long long) PeiCorePhysicalAddress
1646 // First Get the FIT table address
1648 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
1650 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
1652 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
1654 if (!EFI_ERROR (Status
)) {
1655 UpdateFitCheckSum (FitTablePtr
);
1659 // Update SEC_CORE address
1662 // Set the uncached attribute bit in the physical address
1664 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
1666 // Check if address is aligned on a 16 byte boundary
1668 if (SecCorePhysicalAddress
& 0xF) {
1669 Error (NULL
, 0, 3000, "Invalid",
1670 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1671 (unsigned long long) SecCorePhysicalAddress
1676 // Update the address
1678 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
1679 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
1681 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1683 // Get the location to update
1685 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1688 // Write lower 32 bits of physical address for Pei Core entry
1690 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1693 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1695 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1697 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1698 if (Ia32SecEntryOffset
<= -65536) {
1699 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1700 return STATUS_ERROR
;
1703 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1706 // Update the BFV base address
1708 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1709 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1710 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1713 // Update the Startup AP in the FVH header block ZeroVector region.
1715 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1716 if (FvInfo
->Size
<= 0x10000) {
1717 BytePointer2
= m64kRecoveryStartupApDataArray
;
1718 } else if (FvInfo
->Size
<= 0x20000) {
1719 BytePointer2
= m128kRecoveryStartupApDataArray
;
1721 BytePointer2
= m128kRecoveryStartupApDataArray
;
1723 // Find the position to place Ap reset vector, the offset
1724 // between the position and the end of Fvrecovery.fv file
1725 // should not exceed 128kB to prevent Ap reset vector from
1726 // outside legacy E and F segment
1728 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1729 if (EFI_ERROR (Status
)) {
1730 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.");
1735 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1736 BytePointer
[Index
] = BytePointer2
[Index
];
1739 // Calculate the checksum
1742 WordPointer
= (UINT16
*) (BytePointer
);
1743 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1744 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1748 // Update the checksum field
1750 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1751 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1754 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1756 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1757 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1758 if ((IpiVector
& 0xFFF) != 0) {
1759 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1762 IpiVector
= IpiVector
>> 12;
1763 IpiVector
= IpiVector
& 0xFF;
1766 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1768 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1769 *Ia32ResetAddressPtr
= IpiVector
;
1770 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1772 // Since the ARM reset vector is in the FV Header you really don't need a
1773 // Volume Top File, but if you have one for some reason don't crash...
1775 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1777 // Since the AArch64 reset vector is in the FV Header you really don't need a
1778 // Volume Top File, but if you have one for some reason don't crash...
1781 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1786 // Now update file checksum
1788 SavedState
= VtfFile
->State
;
1789 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1791 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1792 FfsHeaderSize
= GetFfsHeaderLength(VtfFile
);
1793 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1794 (UINT8
*) ((UINT8
*)VtfFile
+ FfsHeaderSize
),
1795 GetFfsFileLength (VtfFile
) - FfsHeaderSize
1798 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1801 VtfFile
->State
= SavedState
;
1808 UpdateArmResetVectorIfNeeded (
1809 IN MEMORY_FILE
*FvImage
,
1814 Routine Description:
1815 This parses the FV looking for SEC and patches that address into the
1816 beginning of the FV header.
1818 For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
1819 For AArch64 the reset vector is at 0x00000000.
1821 This would commonly map to the first entry in the ROM.
1831 We support two schemes on ARM.
1832 1) Beginning of the FV is the reset vector
1833 2) Reset vector is data bytes FDF file and that code branches to reset vector
1834 in the beginning of the FV (fixed size offset).
1836 Need to have the jump for the reset vector at location zero.
1837 We also need to store the address or PEI (if it exists).
1838 We stub out a return from interrupt in case the debugger
1839 is using SWI (not done for AArch64, not enough space in struct).
1840 The optional entry to the common exception handler is
1841 to support full featured exception handling from ROM and is currently
1842 not support by this tool.
1845 FvImage Memory file for the FV memory image
1846 FvInfo Information read from INF file.
1850 EFI_SUCCESS Function Completed successfully.
1851 EFI_ABORTED Error encountered.
1852 EFI_INVALID_PARAMETER A required parameter was NULL.
1853 EFI_NOT_FOUND PEI Core file not found.
1857 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1858 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1860 EFI_FILE_SECTION_POINTER Pe32Section
;
1864 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1865 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1866 INT32 ResetVector
[4]; // ARM32:
1867 // 0 - is branch relative to SEC entry point
1868 // 1 - PEI Entry Point
1869 // 2 - movs pc,lr for a SWI handler
1870 // 3 - Place holder for Common Exception Handler
1871 // AArch64: Used as UINT64 ResetVector[2]
1872 // 0 - is branch relative to SEC entry point
1873 // 1 - PEI Entry Point
1876 // Verify input parameters
1878 if (FvImage
== NULL
|| FvInfo
== NULL
) {
1879 return EFI_INVALID_PARAMETER
;
1882 // Initialize FV library
1884 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1887 // Find the Sec Core
1889 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1890 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1892 // Maybe hardware does SEC job and we only have PEI Core?
1896 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1898 PeiCorePhysicalAddress
= 0;
1899 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1900 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1902 // PEI Core found, now find PE32 or TE section
1904 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1905 if (Status
== EFI_NOT_FOUND
) {
1906 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1909 if (EFI_ERROR (Status
)) {
1910 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1914 Status
= GetPe32Info (
1915 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1921 if (EFI_ERROR (Status
)) {
1922 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1926 // Physical address is FV base + offset of PE32 + offset of the entry point
1928 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1929 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
1930 PeiCorePhysicalAddress
+= EntryPoint
;
1931 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1933 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
|| MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1934 memset (ResetVector
, 0, sizeof (ResetVector
));
1935 // Address of PEI Core, if we have one
1936 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1940 // Copy to the beginning of the FV
1942 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1950 // Sec Core found, now find PE32 section
1952 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1953 if (Status
== EFI_NOT_FOUND
) {
1954 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1957 if (EFI_ERROR (Status
)) {
1958 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1962 Status
= GetPe32Info (
1963 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1968 if (EFI_ERROR (Status
)) {
1969 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1973 if ((MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
1975 // If SEC is not ARM we have nothing to do
1981 // Physical address is FV base + offset of PE32 + offset of the entry point
1983 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1984 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
1985 SecCorePhysicalAddress
+= EntryPoint
;
1986 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1989 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1991 PeiCorePhysicalAddress
= 0;
1992 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1993 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1995 // PEI Core found, now find PE32 or TE section
1997 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1998 if (Status
== EFI_NOT_FOUND
) {
1999 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
2002 if (EFI_ERROR (Status
)) {
2003 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
2007 Status
= GetPe32Info (
2008 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
2014 if (EFI_ERROR (Status
)) {
2015 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
2019 // Physical address is FV base + offset of PE32 + offset of the entry point
2021 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
2022 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
) FvImage
->FileImage
;
2023 PeiCorePhysicalAddress
+= EntryPoint
;
2024 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
2027 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
2028 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
2029 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
2030 ResetVector
[0] = (INT32
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
- 8) >> 2;
2032 if (ResetVector
[0] > 0x00FFFFFF) {
2033 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
2037 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
2038 ResetVector
[0] |= 0xEB000000;
2041 // Address of PEI Core, if we have one
2042 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
2044 // SWI handler movs pc,lr. Just in case a debugger uses SWI
2045 ResetVector
[2] = 0xE1B0F07E;
2047 // Place holder to support a common interrupt handler from ROM.
2048 // Currently not suppprted. For this to be used the reset vector would not be in this FV
2049 // and the exception vectors would be hard coded in the ROM and just through this address
2050 // to find a common handler in the a module in the FV.
2052 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
2055 ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
2056 array at the moment, for AArch64, does not allow us space for this as the header only
2057 allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
2058 within the first 4GB of addressable RAM we could potensioally adopt the same ResetVector
2059 layout as above. But for the moment we replace the four 32bit vectors with two 64bit
2060 vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
2064 ((UINT64
*)ResetVector
)[0] = (UINT64
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
) >> 2;
2066 // B SecEntryPoint - signed_immed_26 part +/-128MB offset
2067 if ( ((UINT64
*)ResetVector
)[0] > 0x03FFFFFF) {
2068 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
2071 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
2072 ((UINT64
*)ResetVector
)[0] |= 0x14000000;
2074 // Address of PEI Core, if we have one
2075 ((UINT64
*)ResetVector
)[1] = (UINT64
)PeiCorePhysicalAddress
;
2078 Error (NULL
, 0, 3000, "Invalid", "Unknown ARM machine type");
2083 // Copy to the beginning of the FV
2085 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
2087 DebugMsg (NULL
, 0, 9, "Update Reset vector in FV Header", NULL
);
2095 OUT UINT32
*EntryPoint
,
2096 OUT UINT32
*BaseOfCode
,
2097 OUT UINT16
*MachineType
2101 Routine Description:
2103 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
2104 See EfiImage.h for machine types. The entry point offset is from the beginning
2105 of the PE32 buffer passed in.
2109 Pe32 Beginning of the PE32.
2110 EntryPoint Offset from the beginning of the PE32 to the image entry point.
2111 BaseOfCode Base address of code.
2112 MachineType Magic number for the machine type.
2116 EFI_SUCCESS Function completed successfully.
2117 EFI_ABORTED Error encountered.
2118 EFI_INVALID_PARAMETER A required parameter was NULL.
2119 EFI_UNSUPPORTED The operation is unsupported.
2123 EFI_IMAGE_DOS_HEADER
*DosHeader
;
2124 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2125 EFI_TE_IMAGE_HEADER
*TeHeader
;
2128 // Verify input parameters
2131 return EFI_INVALID_PARAMETER
;
2135 // First check whether it is one TE Image.
2137 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
2138 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
2140 // By TeImage Header to get output
2142 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2143 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2144 *MachineType
= TeHeader
->Machine
;
2148 // Then check whether
2149 // First is the DOS header
2151 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2154 // Verify DOS header is expected
2156 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2157 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
2158 return EFI_UNSUPPORTED
;
2161 // Immediately following is the NT header.
2163 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2166 // Verify NT header is expected
2168 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2169 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
2170 return EFI_UNSUPPORTED
;
2175 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
2176 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
2177 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
2181 // Verify machine type is supported
2183 if ((*MachineType
!= EFI_IMAGE_MACHINE_IA32
) && (*MachineType
!= EFI_IMAGE_MACHINE_IA64
) && (*MachineType
!= EFI_IMAGE_MACHINE_X64
) && (*MachineType
!= EFI_IMAGE_MACHINE_EBC
) &&
2184 (*MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (*MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
2185 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
2186 return EFI_UNSUPPORTED
;
2194 IN CHAR8
*InfFileImage
,
2195 IN UINTN InfFileSize
,
2196 IN CHAR8
*FvFileName
,
2197 IN CHAR8
*MapFileName
2201 Routine Description:
2203 This is the main function which will be called from application.
2207 InfFileImage Buffer containing the INF file contents.
2208 InfFileSize Size of the contents of the InfFileImage buffer.
2209 FvFileName Requested name for the FV file.
2210 MapFileName Fv map file to log fv driver information.
2214 EFI_SUCCESS Function completed successfully.
2215 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2216 EFI_ABORTED Error encountered.
2217 EFI_INVALID_PARAMETER A required parameter was NULL.
2222 MEMORY_FILE InfMemoryFile
;
2223 MEMORY_FILE FvImageMemoryFile
;
2225 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2226 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2227 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
2231 CHAR8 FvMapName
[MAX_LONG_FILE_PATH
];
2233 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2234 FILE *FvExtHeaderFile
;
2236 CHAR8 FvReportName
[MAX_LONG_FILE_PATH
];
2239 FvBufferHeader
= NULL
;
2242 FvReportFile
= NULL
;
2244 if (InfFileImage
!= NULL
) {
2246 // Initialize file structures
2248 InfMemoryFile
.FileImage
= InfFileImage
;
2249 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2250 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2253 // Parse the FV inf file for header information
2255 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2256 if (EFI_ERROR (Status
)) {
2257 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2263 // Update the file name return values
2265 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2266 FvFileName
= mFvDataInfo
.FvName
;
2269 if (FvFileName
== NULL
) {
2270 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2274 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2275 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2280 // Debug message Fv File System Guid
2282 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2283 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2284 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2285 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2286 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2287 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2288 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2289 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2290 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2291 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2292 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2293 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2294 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2298 // Add PI FV extension header
2301 FvExtHeaderFile
= NULL
;
2302 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2304 // Open the FV Extension Header file
2306 FvExtHeaderFile
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2309 // Get the file size
2311 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2314 // Allocate a buffer for the FV Extension Header
2316 FvExtHeader
= malloc(FileSize
);
2317 if (FvExtHeader
== NULL
) {
2318 fclose (FvExtHeaderFile
);
2319 return EFI_OUT_OF_RESOURCES
;
2323 // Read the FV Extension Header
2325 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2326 fclose (FvExtHeaderFile
);
2329 // See if there is an override for the FV Name GUID
2331 if (mFvDataInfo
.FvNameGuidSet
) {
2332 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2334 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2335 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2336 } else if (mFvDataInfo
.FvNameGuidSet
) {
2338 // Allocate a buffer for the FV Extension Header
2340 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2341 if (FvExtHeader
== NULL
) {
2342 return EFI_OUT_OF_RESOURCES
;
2344 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2345 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2349 // Debug message Fv Name Guid
2351 if (mFvDataInfo
.FvNameGuidSet
) {
2352 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2353 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2354 mFvDataInfo
.FvNameGuid
.Data2
,
2355 mFvDataInfo
.FvNameGuid
.Data3
,
2356 mFvDataInfo
.FvNameGuid
.Data4
[0],
2357 mFvDataInfo
.FvNameGuid
.Data4
[1],
2358 mFvDataInfo
.FvNameGuid
.Data4
[2],
2359 mFvDataInfo
.FvNameGuid
.Data4
[3],
2360 mFvDataInfo
.FvNameGuid
.Data4
[4],
2361 mFvDataInfo
.FvNameGuid
.Data4
[5],
2362 mFvDataInfo
.FvNameGuid
.Data4
[6],
2363 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2366 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0 ||
2367 CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem3Guid
) == 0) {
2368 mFvDataInfo
.IsPiFvImage
= TRUE
;
2372 // FvMap file to log the function address of all modules in one Fvimage
2374 if (MapFileName
!= NULL
) {
2375 strcpy (FvMapName
, MapFileName
);
2377 strcpy (FvMapName
, FvFileName
);
2378 strcat (FvMapName
, ".map");
2380 VerboseMsg ("FV Map file name is %s", FvMapName
);
2383 // FvReport file to log the FV information in one Fvimage
2385 strcpy (FvReportName
, FvFileName
);
2386 strcat (FvReportName
, ".txt");
2389 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2390 // And Update mFvDataInfo data.
2392 Status
= CalculateFvSize (&mFvDataInfo
);
2393 if (EFI_ERROR (Status
)) {
2396 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2399 // support fv image and empty fv image
2401 FvImageSize
= mFvDataInfo
.Size
;
2404 // Allocate the FV, assure FvImage Header 8 byte alignment
2406 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2407 if (FvBufferHeader
== NULL
) {
2408 return EFI_OUT_OF_RESOURCES
;
2410 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2413 // Initialize the FV to the erase polarity
2415 if (mFvDataInfo
.FvAttributes
== 0) {
2417 // Set Default Fv Attribute
2419 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2421 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2422 memset (FvImage
, -1, FvImageSize
);
2424 memset (FvImage
, 0, FvImageSize
);
2428 // Initialize FV header
2430 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2433 // Initialize the zero vector to all zeros.
2435 memset (FvHeader
->ZeroVector
, 0, 16);
2438 // Copy the Fv file system GUID
2440 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2442 FvHeader
->FvLength
= FvImageSize
;
2443 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2444 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2445 FvHeader
->Revision
= EFI_FVH_REVISION
;
2446 FvHeader
->ExtHeaderOffset
= 0;
2447 FvHeader
->Reserved
[0] = 0;
2450 // Copy firmware block map
2452 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2453 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2454 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2458 // Add block map terminator
2460 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2461 FvHeader
->BlockMap
[Index
].Length
= 0;
2464 // Complete the header
2466 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2467 FvHeader
->Checksum
= 0;
2468 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2471 // If there is no FFS file, generate one empty FV
2473 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2478 // Initialize our "file" view of the buffer
2480 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2481 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2482 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2485 // Initialize the FV library.
2487 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2490 // Initialize the VTF file address.
2492 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2497 FvMapFile
= fopen (LongFilePath (FvMapName
), "w");
2498 if (FvMapFile
== NULL
) {
2499 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2504 // Open FvReport file
2506 FvReportFile
= fopen (LongFilePath (FvReportName
), "w");
2507 if (FvReportFile
== NULL
) {
2508 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2512 // record FV size information into FvMap file.
2514 if (mFvTotalSize
!= 0) {
2515 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2516 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2518 if (mFvTakenSize
!= 0) {
2519 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2520 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2522 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2523 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2524 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2528 // record FV size information to FvReportFile.
2530 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2531 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2534 // Add PI FV extension header
2536 if (FvExtHeader
!= NULL
) {
2538 // Add FV Extended Header contents to the FV as a PAD file
2540 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2543 // Fv Extension header change update Fv Header Check sum
2545 FvHeader
->Checksum
= 0;
2546 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2552 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2556 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2559 // Exit if error detected while adding the file
2561 if (EFI_ERROR (Status
)) {
2567 // If there is a VTF file, some special actions need to occur.
2569 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2571 // Pad from the end of the last file to the beginning of the VTF file.
2572 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2574 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2575 if (EFI_ERROR (Status
)) {
2576 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2581 // Update reset vector (SALE_ENTRY for IPF)
2582 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2583 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2584 // reset vector. If the PEI Core is found, the VTF file will probably get
2585 // corrupted by updating the entry point.
2587 if ((mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2588 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2589 if (EFI_ERROR(Status
)) {
2590 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2593 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2599 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2600 if (EFI_ERROR (Status
)) {
2601 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2606 // Update Checksum for FvHeader
2608 FvHeader
->Checksum
= 0;
2609 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2613 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2615 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2616 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2617 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2619 // Update Checksum for FvHeader
2621 FvHeader
->Checksum
= 0;
2622 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2626 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2628 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2629 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2630 FvHeader
->Checksum
= 0;
2631 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2638 FvFile
= fopen (LongFilePath (FvFileName
), "wb");
2639 if (FvFile
== NULL
) {
2640 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2641 Status
= EFI_ABORTED
;
2645 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2646 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2647 Status
= EFI_ABORTED
;
2652 if (FvBufferHeader
!= NULL
) {
2653 free (FvBufferHeader
);
2656 if (FvExtHeader
!= NULL
) {
2660 if (FvFile
!= NULL
) {
2665 if (FvMapFile
!= NULL
) {
2670 if (FvReportFile
!= NULL
) {
2671 fflush (FvReportFile
);
2672 fclose (FvReportFile
);
2678 UpdatePeiCoreEntryInFit (
2679 IN FIT_TABLE
*FitTablePtr
,
2680 IN UINT64 PeiCorePhysicalAddress
2684 Routine Description:
2686 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2691 FitTablePtr - The pointer of FIT_TABLE.
2692 PeiCorePhysicalAddress - The address of Pei Core entry.
2696 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2697 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2701 FIT_TABLE
*TmpFitPtr
;
2703 UINTN NumFitComponents
;
2705 TmpFitPtr
= FitTablePtr
;
2706 NumFitComponents
= TmpFitPtr
->CompSize
;
2708 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2709 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2710 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2717 return EFI_NOT_FOUND
;
2722 IN FIT_TABLE
*FitTablePtr
2726 Routine Description:
2728 This function is used to update the checksum for FIT.
2733 FitTablePtr - The pointer of FIT_TABLE.
2741 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2742 FitTablePtr
->CheckSum
= 0;
2743 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2752 Routine Description:
2753 Calculate the FV size and Update Fv Size based on the actual FFS files.
2754 And Update FvInfo data.
2757 FvInfoPtr - The pointer to FV_INFO structure.
2760 EFI_ABORTED - Ffs Image Error
2761 EFI_SUCCESS - Successfully update FvSize
2764 UINTN CurrentOffset
;
2768 UINTN FvExtendHeaderSize
;
2769 UINT32 FfsAlignment
;
2770 UINT32 FfsHeaderSize
;
2771 EFI_FFS_FILE_HEADER FfsHeader
;
2772 BOOLEAN VtfFileFlag
;
2775 FvExtendHeaderSize
= 0;
2777 VtfFileFlag
= FALSE
;
2782 // Compute size for easy access later
2784 FvInfoPtr
->Size
= 0;
2785 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
2786 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
2790 // Caculate the required sizes for all FFS files.
2792 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
2794 for (Index
= 1;; Index
++) {
2795 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
2796 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
2802 // Calculate PI extension header
2804 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
2805 fpin
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2807 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2810 FvExtendHeaderSize
= _filelength (fileno (fpin
));
2812 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
2813 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
2816 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
2818 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2819 } else if (mFvDataInfo
.FvNameGuidSet
) {
2820 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2821 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2825 // Accumlate every FFS file size.
2827 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
2832 fpin
= fopen (LongFilePath (FvInfoPtr
->FvFiles
[Index
]), "rb");
2834 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
2838 // Get the file size
2840 FfsFileSize
= _filelength (fileno (fpin
));
2841 if (FfsFileSize
>= MAX_FFS_SIZE
) {
2842 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
2845 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
2848 // Read Ffs File header
2850 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
2856 if (FvInfoPtr
->IsPiFvImage
) {
2858 // Check whether this ffs file is vtf file
2860 if (IsVtfFile (&FfsHeader
)) {
2863 // One Fv image can't have two vtf files.
2868 VtfFileSize
= FfsFileSize
;
2873 // Get the alignment of FFS file
2875 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
2876 FfsAlignment
= 1 << FfsAlignment
;
2880 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
2882 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
2884 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
2885 CurrentOffset
-= FfsHeaderSize
;
2890 // Add ffs file size
2892 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
2893 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
2895 CurrentOffset
+= FfsFileSize
;
2899 // Make next ffs file start at QWord Boundry
2901 if (FvInfoPtr
->IsPiFvImage
) {
2902 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
2905 CurrentOffset
+= VtfFileSize
;
2906 DebugMsg (NULL
, 0, 9, "FvImage size", "The caculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
2908 if (FvInfoPtr
->Size
== 0) {
2910 // Update FvInfo data
2912 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
2913 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
2914 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
2915 FvInfoPtr
->FvBlocks
[1].Length
= 0;
2916 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
2920 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
);
2921 return EFI_INVALID_PARAMETER
;
2925 // Set Fv Size Information
2927 mFvTotalSize
= FvInfoPtr
->Size
;
2928 mFvTakenSize
= CurrentOffset
;
2934 FfsRebaseImageRead (
2935 IN VOID
*FileHandle
,
2936 IN UINTN FileOffset
,
2937 IN OUT UINT32
*ReadSize
,
2942 Routine Description:
2944 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2948 FileHandle - The handle to the PE/COFF file
2950 FileOffset - The offset, in bytes, into the file to read
2952 ReadSize - The number of bytes to read from the file starting at FileOffset
2954 Buffer - A pointer to the buffer to read the data into.
2958 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2962 CHAR8
*Destination8
;
2966 Destination8
= Buffer
;
2967 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
2970 *(Destination8
++) = *(Source8
++);
2979 IN EFI_FFS_FILE_HEADER
*FfsFile
,
2984 Routine Description:
2986 This function gets all child FvImages in the input FfsFile, and records
2987 their base address to the parent image.
2990 FvInfo A pointer to FV_INFO struture.
2991 FfsFile A pointer to Ffs file image that may contain FvImage.
2992 XipOffset The offset address to the parent FvImage base.
2996 EFI_SUCCESS Base address of child Fv image is recorded.
3001 EFI_FILE_SECTION_POINTER SubFvSection
;
3002 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
3003 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
3005 for (Index
= 1;; Index
++) {
3009 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
3010 if (EFI_ERROR (Status
)) {
3013 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
3017 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
3018 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
3026 IN OUT FV_INFO
*FvInfo
,
3028 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
3034 Routine Description:
3036 This function determines if a file is XIP and should be rebased. It will
3037 rebase any PE32 sections found in the file using the base address.
3041 FvInfo A pointer to FV_INFO struture.
3042 FileName Ffs File PathName
3043 FfsFile A pointer to Ffs file image.
3044 XipOffset The offset address to use for rebasing the XIP file image.
3045 FvMapFile FvMapFile to record the function address in one Fvimage
3049 EFI_SUCCESS The image was properly rebased.
3050 EFI_INVALID_PARAMETER An input parameter is invalid.
3051 EFI_ABORTED An error occurred while rebasing the input file image.
3052 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
3053 EFI_NOT_FOUND No compressed sections could be found.
3058 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
3059 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
3060 EFI_PHYSICAL_ADDRESS XipBase
;
3061 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
3063 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
3064 EFI_FFS_FILE_STATE SavedState
;
3065 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
3066 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
3067 UINT8
*MemoryImagePointer
;
3068 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
3069 CHAR8 PeFileName
[MAX_LONG_FILE_PATH
];
3072 UINT8
*PeFileBuffer
;
3075 UINT32 FfsHeaderSize
;
3076 UINT32 CurSecHdrSize
;
3079 MemoryImagePointer
= NULL
;
3080 TEImageHeader
= NULL
;
3082 SectionHeader
= NULL
;
3085 PeFileBuffer
= NULL
;
3088 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
3090 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
3095 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
3097 if (FvInfo
->ForceRebase
== 0) {
3102 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
3105 // We only process files potentially containing PE32 sections.
3107 switch (FfsFile
->Type
) {
3108 case EFI_FV_FILETYPE_SECURITY_CORE
:
3109 case EFI_FV_FILETYPE_PEI_CORE
:
3110 case EFI_FV_FILETYPE_PEIM
:
3111 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3112 case EFI_FV_FILETYPE_DRIVER
:
3113 case EFI_FV_FILETYPE_DXE_CORE
:
3115 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
3117 // Rebase the inside FvImage.
3119 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
3122 // Search PE/TE section in FV sectin.
3129 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
3131 // Rebase each PE32 section
3133 Status
= EFI_SUCCESS
;
3134 for (Index
= 1;; Index
++) {
3138 NewPe32BaseAddress
= 0;
3143 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
3144 if (EFI_ERROR (Status
)) {
3147 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3150 // Initialize context
3152 memset (&ImageContext
, 0, sizeof (ImageContext
));
3153 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3154 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3155 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3156 if (EFI_ERROR (Status
)) {
3157 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3161 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3162 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3167 // Keep Image Context for PE image in FV
3169 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3172 // Get File PdbPointer
3174 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3177 // Get PeHeader pointer
3179 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3182 // Calculate the PE32 base address, based on file type
3184 switch (FfsFile
->Type
) {
3185 case EFI_FV_FILETYPE_SECURITY_CORE
:
3186 case EFI_FV_FILETYPE_PEI_CORE
:
3187 case EFI_FV_FILETYPE_PEIM
:
3188 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3190 // Check if section-alignment and file-alignment match or not
3192 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3194 // Xip module has the same section alignment and file alignment.
3196 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3200 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3202 if (ImageContext
.RelocationsStripped
) {
3204 // Construct the original efi file Name
3206 strcpy (PeFileName
, FileName
);
3207 Cptr
= PeFileName
+ strlen (PeFileName
);
3208 while (*Cptr
!= '.') {
3212 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3220 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3221 if (PeFile
== NULL
) {
3222 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3223 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3224 //return EFI_ABORTED;
3228 // Get the file size
3230 PeFileSize
= _filelength (fileno (PeFile
));
3231 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3232 if (PeFileBuffer
== NULL
) {
3233 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3234 return EFI_OUT_OF_RESOURCES
;
3239 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3245 // Handle pointer to the original efi image.
3247 ImageContext
.Handle
= PeFileBuffer
;
3248 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3249 if (EFI_ERROR (Status
)) {
3250 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3253 ImageContext
.RelocationsStripped
= FALSE
;
3256 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3259 case EFI_FV_FILETYPE_DRIVER
:
3260 case EFI_FV_FILETYPE_DXE_CORE
:
3262 // Check if section-alignment and file-alignment match or not
3264 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3266 // Xip module has the same section alignment and file alignment.
3268 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3271 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3276 // Not supported file type
3282 // Relocation doesn't exist
3284 if (ImageContext
.RelocationsStripped
) {
3285 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3290 // Relocation exist and rebase
3293 // Load and Relocate Image Data
3295 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3296 if (MemoryImagePointer
== NULL
) {
3297 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3298 return EFI_OUT_OF_RESOURCES
;
3300 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3301 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3303 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3304 if (EFI_ERROR (Status
)) {
3305 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3306 free ((VOID
*) MemoryImagePointer
);
3310 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3311 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3312 if (EFI_ERROR (Status
)) {
3313 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3314 free ((VOID
*) MemoryImagePointer
);
3319 // Copy Relocated data to raw image file.
3321 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3324 sizeof (EFI_IMAGE_FILE_HEADER
) +
3325 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3328 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3330 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3331 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3332 SectionHeader
->SizeOfRawData
3336 free ((VOID
*) MemoryImagePointer
);
3337 MemoryImagePointer
= NULL
;
3338 if (PeFileBuffer
!= NULL
) {
3339 free (PeFileBuffer
);
3340 PeFileBuffer
= NULL
;
3344 // Update Image Base Address
3346 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3347 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3348 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3349 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3351 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3352 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3359 // Now update file checksum
3361 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3362 SavedState
= FfsFile
->State
;
3363 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3365 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3366 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3367 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3369 FfsFile
->State
= SavedState
;
3373 // Get this module function address from ModulePeMapFile and add them into FvMap file
3377 // Default use FileName as map file path
3379 if (PdbPointer
== NULL
) {
3380 PdbPointer
= FileName
;
3383 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3386 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3387 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3388 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3389 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3390 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3393 // Only Peim code may have a TE section
3399 // Now process TE sections
3401 for (Index
= 1;; Index
++) {
3402 NewPe32BaseAddress
= 0;
3407 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3408 if (EFI_ERROR (Status
)) {
3412 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3415 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3418 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3421 // Initialize context, load image info.
3423 memset (&ImageContext
, 0, sizeof (ImageContext
));
3424 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3425 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3426 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3427 if (EFI_ERROR (Status
)) {
3428 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3432 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3433 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3438 // Keep Image Context for TE image in FV
3440 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3443 // Get File PdbPointer
3445 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3448 // Set new rebased address.
3450 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3451 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3454 // if reloc is stripped, try to get the original efi image to get reloc info.
3456 if (ImageContext
.RelocationsStripped
) {
3458 // Construct the original efi file name
3460 strcpy (PeFileName
, FileName
);
3461 Cptr
= PeFileName
+ strlen (PeFileName
);
3462 while (*Cptr
!= '.') {
3467 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3476 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3477 if (PeFile
== NULL
) {
3478 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3479 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3480 //return EFI_ABORTED;
3483 // Get the file size
3485 PeFileSize
= _filelength (fileno (PeFile
));
3486 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3487 if (PeFileBuffer
== NULL
) {
3488 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3489 return EFI_OUT_OF_RESOURCES
;
3494 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3500 // Append reloc section into TeImage
3502 ImageContext
.Handle
= PeFileBuffer
;
3503 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3504 if (EFI_ERROR (Status
)) {
3505 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3508 ImageContext
.RelocationsStripped
= FALSE
;
3512 // Relocation doesn't exist
3514 if (ImageContext
.RelocationsStripped
) {
3515 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3520 // Relocation exist and rebase
3523 // Load and Relocate Image Data
3525 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3526 if (MemoryImagePointer
== NULL
) {
3527 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3528 return EFI_OUT_OF_RESOURCES
;
3530 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3531 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3533 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3534 if (EFI_ERROR (Status
)) {
3535 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3536 free ((VOID
*) MemoryImagePointer
);
3540 // Reloacate TeImage
3542 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3543 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3544 if (EFI_ERROR (Status
)) {
3545 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3546 free ((VOID
*) MemoryImagePointer
);
3551 // Copy the relocated image into raw image file.
3553 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3554 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3555 if (!ImageContext
.IsTeImage
) {
3557 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3558 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3559 SectionHeader
->SizeOfRawData
3563 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3564 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3565 SectionHeader
->SizeOfRawData
3571 // Free the allocated memory resource
3573 free ((VOID
*) MemoryImagePointer
);
3574 MemoryImagePointer
= NULL
;
3575 if (PeFileBuffer
!= NULL
) {
3576 free (PeFileBuffer
);
3577 PeFileBuffer
= NULL
;
3581 // Update Image Base Address
3583 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3586 // Now update file checksum
3588 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3589 SavedState
= FfsFile
->State
;
3590 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3592 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3593 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3594 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3596 FfsFile
->State
= SavedState
;
3599 // Get this module function address from ModulePeMapFile and add them into FvMap file
3603 // Default use FileName as map file path
3605 if (PdbPointer
== NULL
) {
3606 PdbPointer
= FileName
;
3622 FindApResetVectorPosition (
3623 IN MEMORY_FILE
*FvImage
,
3628 Routine Description:
3630 Find the position in this FvImage to place Ap reset vector.
3634 FvImage Memory file for the FV memory image.
3635 Pointer Pointer to pointer to position.
3639 EFI_NOT_FOUND - No satisfied position is found.
3640 EFI_SUCCESS - The suitable position is return.
3644 EFI_FFS_FILE_HEADER
*PadFile
;
3650 for (Index
= 1; ;Index
++) {
3652 // Find Pad File to add ApResetVector info
3654 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3655 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3657 // No Pad file to be found.
3662 // Get Pad file size.
3664 FileLength
= GetFfsFileLength(PadFile
);
3665 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3667 // FixPoint must be align on 0x1000 relative to FvImage Header
3669 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3670 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3672 // FixPoint be larger at the last place of one fv image.
3674 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3679 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3681 // No alignment FixPoint in this Pad File.
3686 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3688 // Find the position to place ApResetVector
3690 *Pointer
= FixPoint
;
3695 return EFI_NOT_FOUND
;
3700 IN MEMORY_FILE
*InfFile
,
3701 OUT CAP_INFO
*CapInfo
3705 Routine Description:
3707 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3711 InfFile Memory file image.
3712 CapInfo Information read from INF file.
3716 EFI_SUCCESS INF file information successfully retrieved.
3717 EFI_ABORTED INF file has an invalid format.
3718 EFI_NOT_FOUND A required string was not found in the INF file.
3721 CHAR8 Value
[MAX_LONG_FILE_PATH
];
3723 UINTN Index
, Number
;
3727 // Initialize Cap info
3729 // memset (CapInfo, 0, sizeof (CAP_INFO));
3733 // Read the Capsule Guid
3735 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
3736 if (Status
== EFI_SUCCESS
) {
3738 // Get the Capsule Guid
3740 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
3741 if (EFI_ERROR (Status
)) {
3742 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3745 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3749 // Read the Capsule Header Size
3751 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
3752 if (Status
== EFI_SUCCESS
) {
3753 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3754 if (EFI_ERROR (Status
)) {
3755 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3758 CapInfo
->HeaderSize
= (UINT32
) Value64
;
3759 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3763 // Read the Capsule Flag
3765 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
3766 if (Status
== EFI_SUCCESS
) {
3767 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
3768 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
3769 if (strstr (Value
, "InitiateReset") != NULL
) {
3770 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3772 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
3773 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
3774 if (strstr (Value
, "InitiateReset") != NULL
) {
3775 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3778 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
3781 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
3784 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
3785 if (Status
== EFI_SUCCESS
) {
3786 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3787 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
3788 Error (NULL
, 0, 2000, "Invalid parameter",
3789 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
3790 EFI_OEM_CAPSULE_FLAGS_STRING
);
3793 CapInfo
->Flags
|= Value64
;
3794 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
3798 // Read Capsule File name
3800 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
3801 if (Status
== EFI_SUCCESS
) {
3803 // Get output file name
3805 strcpy (CapInfo
->CapName
, Value
);
3809 // Read the Capsule FileImage
3812 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
3813 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
3817 // Read the capsule file name
3819 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
3821 if (Status
== EFI_SUCCESS
) {
3825 strcpy (CapInfo
->CapFiles
[Index
], Value
);
3826 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
3833 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
3841 IN CHAR8
*InfFileImage
,
3842 IN UINTN InfFileSize
,
3843 IN CHAR8
*CapFileName
3847 Routine Description:
3849 This is the main function which will be called from application to create UEFI Capsule image.
3853 InfFileImage Buffer containing the INF file contents.
3854 InfFileSize Size of the contents of the InfFileImage buffer.
3855 CapFileName Requested name for the Cap file.
3859 EFI_SUCCESS Function completed successfully.
3860 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3861 EFI_ABORTED Error encountered.
3862 EFI_INVALID_PARAMETER A required parameter was NULL.
3868 EFI_CAPSULE_HEADER
*CapsuleHeader
;
3869 MEMORY_FILE InfMemoryFile
;
3875 if (InfFileImage
!= NULL
) {
3877 // Initialize file structures
3879 InfMemoryFile
.FileImage
= InfFileImage
;
3880 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
3881 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
3884 // Parse the Cap inf file for header information
3886 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
3887 if (Status
!= EFI_SUCCESS
) {
3892 if (mCapDataInfo
.HeaderSize
== 0) {
3894 // make header size align 16 bytes.
3896 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
3897 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
3900 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
3901 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3902 return EFI_INVALID_PARAMETER
;
3905 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
3906 CapFileName
= mCapDataInfo
.CapName
;
3909 if (CapFileName
== NULL
) {
3910 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
3911 return EFI_INVALID_PARAMETER
;
3915 // Set Default Capsule Guid value
3917 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
3918 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
3921 // Calculate the size of capsule image.
3925 CapSize
= mCapDataInfo
.HeaderSize
;
3926 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3927 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
3929 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3932 FileSize
= _filelength (fileno (fpin
));
3933 CapSize
+= FileSize
;
3939 // Allocate buffer for capsule image.
3941 CapBuffer
= (UINT8
*) malloc (CapSize
);
3942 if (CapBuffer
== NULL
) {
3943 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3944 return EFI_OUT_OF_RESOURCES
;
3948 // Initialize the capsule header to zero
3950 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
3953 // create capsule header and get capsule body
3955 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
3956 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
3957 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
3958 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
3959 CapsuleHeader
->CapsuleImageSize
= CapSize
;
3963 CapSize
= CapsuleHeader
->HeaderSize
;
3964 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3965 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
3967 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3971 FileSize
= _filelength (fileno (fpin
));
3972 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
3975 CapSize
+= FileSize
;
3979 // write capsule data into the output file
3981 fpout
= fopen (LongFilePath (CapFileName
), "wb");
3982 if (fpout
== NULL
) {
3983 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
3988 fwrite (CapBuffer
, 1, CapSize
, fpout
);
3991 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);