2 This file contains the internal functions required to generate a Firmware Volume.
4 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2016 HP Development Company, L.P.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #if defined(__FreeBSD__)
23 #elif defined(__GNUC__)
24 #include <uuid/uuid.h>
35 #include <Guid/FfsSectionAlignmentPadding.h>
37 #include "GenFvInternalLib.h"
39 #include "PeCoffLib.h"
40 #include "WinNtInclude.h"
42 #define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION 0xEB000000
43 #define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION 0x14000000
46 STATIC UINT32 MaxFfsAlignment
= 0;
48 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
49 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
50 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
51 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
52 EFI_GUID mEfiFfsSectionAlignmentPaddingGuid
= EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID
;
54 CHAR8
*mFvbAttributeName
[] = {
55 EFI_FVB2_READ_DISABLED_CAP_STRING
,
56 EFI_FVB2_READ_ENABLED_CAP_STRING
,
57 EFI_FVB2_READ_STATUS_STRING
,
58 EFI_FVB2_WRITE_DISABLED_CAP_STRING
,
59 EFI_FVB2_WRITE_ENABLED_CAP_STRING
,
60 EFI_FVB2_WRITE_STATUS_STRING
,
61 EFI_FVB2_LOCK_CAP_STRING
,
62 EFI_FVB2_LOCK_STATUS_STRING
,
64 EFI_FVB2_STICKY_WRITE_STRING
,
65 EFI_FVB2_MEMORY_MAPPED_STRING
,
66 EFI_FVB2_ERASE_POLARITY_STRING
,
67 EFI_FVB2_READ_LOCK_CAP_STRING
,
68 EFI_FVB2_READ_LOCK_STATUS_STRING
,
69 EFI_FVB2_WRITE_LOCK_CAP_STRING
,
70 EFI_FVB2_WRITE_LOCK_STATUS_STRING
73 CHAR8
*mFvbAlignmentName
[] = {
74 EFI_FVB2_ALIGNMENT_1_STRING
,
75 EFI_FVB2_ALIGNMENT_2_STRING
,
76 EFI_FVB2_ALIGNMENT_4_STRING
,
77 EFI_FVB2_ALIGNMENT_8_STRING
,
78 EFI_FVB2_ALIGNMENT_16_STRING
,
79 EFI_FVB2_ALIGNMENT_32_STRING
,
80 EFI_FVB2_ALIGNMENT_64_STRING
,
81 EFI_FVB2_ALIGNMENT_128_STRING
,
82 EFI_FVB2_ALIGNMENT_256_STRING
,
83 EFI_FVB2_ALIGNMENT_512_STRING
,
84 EFI_FVB2_ALIGNMENT_1K_STRING
,
85 EFI_FVB2_ALIGNMENT_2K_STRING
,
86 EFI_FVB2_ALIGNMENT_4K_STRING
,
87 EFI_FVB2_ALIGNMENT_8K_STRING
,
88 EFI_FVB2_ALIGNMENT_16K_STRING
,
89 EFI_FVB2_ALIGNMENT_32K_STRING
,
90 EFI_FVB2_ALIGNMENT_64K_STRING
,
91 EFI_FVB2_ALIGNMENT_128K_STRING
,
92 EFI_FVB2_ALIGNMENT_256K_STRING
,
93 EFI_FVB2_ALIGNMENT_512K_STRING
,
94 EFI_FVB2_ALIGNMENT_1M_STRING
,
95 EFI_FVB2_ALIGNMENT_2M_STRING
,
96 EFI_FVB2_ALIGNMENT_4M_STRING
,
97 EFI_FVB2_ALIGNMENT_8M_STRING
,
98 EFI_FVB2_ALIGNMENT_16M_STRING
,
99 EFI_FVB2_ALIGNMENT_32M_STRING
,
100 EFI_FVB2_ALIGNMENT_64M_STRING
,
101 EFI_FVB2_ALIGNMENT_128M_STRING
,
102 EFI_FVB2_ALIGNMENT_256M_STRING
,
103 EFI_FVB2_ALIGNMENT_512M_STRING
,
104 EFI_FVB2_ALIGNMENT_1G_STRING
,
105 EFI_FVB2_ALIGNMENT_2G_STRING
109 // This data array will be located at the base of the Firmware Volume Header (FVH)
110 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
111 // will be used to keep the FVH checksum consistent.
112 // This code will be run in response to a starutp IPI for HT-enabled systems.
114 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
116 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
118 // EA D0 FF 00 F0 ; far jmp F000:FFD0
119 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
120 // 0, 0 ; Checksum Padding
140 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
142 // EB CE ; jmp short ($-0x30)
143 // ; (from offset 0x0 to offset 0xFFD0)
144 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
145 // 0, 0 ; Checksum Padding
166 CAP_INFO mCapDataInfo
;
167 BOOLEAN mIsLargeFfs
= FALSE
;
169 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
170 UINT32 mFvBaseAddressNumber
= 0;
174 IN MEMORY_FILE
*InfFile
,
181 This function parses a FV.INF file and copies info into a FV_INFO structure.
185 InfFile Memory file image.
186 FvInfo Information read from INF file.
190 EFI_SUCCESS INF file information successfully retrieved.
191 EFI_ABORTED INF file has an invalid format.
192 EFI_NOT_FOUND A required string was not found in the INF file.
195 CHAR8 Value
[MAX_LONG_FILE_PATH
];
203 // Read the FV base address
205 if (!mFvDataInfo
.BaseAddressSet
) {
206 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
207 if (Status
== EFI_SUCCESS
) {
209 // Get the base address
211 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
212 if (EFI_ERROR (Status
)) {
213 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
216 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
218 FvInfo
->BaseAddress
= Value64
;
219 FvInfo
->BaseAddressSet
= TRUE
;
224 // Read the FV File System Guid
226 if (!FvInfo
->FvFileSystemGuidSet
) {
227 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
228 if (Status
== EFI_SUCCESS
) {
230 // Get the guid value
232 Status
= StringToGuid (Value
, &GuidValue
);
233 if (EFI_ERROR (Status
)) {
234 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
237 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
238 FvInfo
->FvFileSystemGuidSet
= TRUE
;
243 // Read the FV Extension Header File Name
245 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
246 if (Status
== EFI_SUCCESS
) {
247 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
251 // Read the FV file name
253 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
254 if (Status
== EFI_SUCCESS
) {
256 // copy the file name
258 strcpy (FvInfo
->FvName
, Value
);
264 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
265 if ((mFvbAttributeName
[Index
] != NULL
) && \
266 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
267 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
268 FvInfo
->FvAttributes
|= 1 << Index
;
269 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
270 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
279 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
280 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
281 if (strcmp (Value
, TRUE_STRING
) == 0) {
282 FvInfo
->FvAttributes
|= Index
<< 16;
283 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
290 // Read weak alignment flag
292 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_WEAK_ALIGNMENT_STRING
, 0, Value
);
293 if (Status
== EFI_SUCCESS
) {
294 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
295 FvInfo
->FvAttributes
|= EFI_FVB2_WEAK_ALIGNMENT
;
296 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
297 Error (NULL
, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
305 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
306 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
310 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
312 if (Status
== EFI_SUCCESS
) {
314 // Update the size of block
316 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
317 if (EFI_ERROR (Status
)) {
318 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
322 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
323 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
326 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
327 // and should return an error.
329 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
330 if (!EFI_ERROR (Status
)) {
331 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
342 // Read blocks number
344 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
346 if (Status
== EFI_SUCCESS
) {
348 // Update the number of blocks
350 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
351 if (EFI_ERROR (Status
)) {
352 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
356 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
357 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
363 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
371 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
372 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
377 for (Index
= 0; Number
+ Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
379 // Read the FFS file list
381 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
383 if (Status
== EFI_SUCCESS
) {
387 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
388 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
394 if ((Index
+ Number
) == 0) {
395 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
403 IN EFI_FFS_FILE_HEADER
*FfsFile
,
404 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
410 This function changes the FFS file attributes based on the erase polarity
411 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
424 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
425 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
426 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
432 IN EFI_FFS_FILE_HEADER
*FfsFile
,
433 IN OUT UINT32
*Alignment
439 This function determines the alignment of the FFS input file from the file
444 FfsFile FFS file to parse
445 Alignment The minimum required alignment offset of the FFS file
449 EFI_SUCCESS The function completed successfully.
450 EFI_INVALID_PARAMETER One of the input parameters was invalid.
451 EFI_ABORTED An error occurred.
456 // Verify input parameters.
458 if (FfsFile
== NULL
|| Alignment
== NULL
) {
459 return EFI_INVALID_PARAMETER
;
462 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
480 // 128 byte alignment
487 // 512 byte alignment
508 // 32K byte alignment
515 // 64K byte alignment
529 IN OUT MEMORY_FILE
*FvImage
,
530 IN UINT32 DataAlignment
,
532 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
,
533 IN UINT32 NextFfsSize
539 This function adds a pad file to the FV image if it required to align the
540 data of the next file.
544 FvImage The memory image of the FV to add it to.
545 The current offset must be valid.
546 DataAlignment The data alignment of the next FFS file.
547 FvEnd End of the empty data in FvImage.
548 ExtHeader PI FvExtHeader Optional
552 EFI_SUCCESS The function completed successfully.
553 EFI_INVALID_PARAMETER One of the input parameters was invalid.
554 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
559 EFI_FFS_FILE_HEADER
*PadFile
;
561 UINT32 NextFfsHeaderSize
;
562 UINT32 CurFfsHeaderSize
;
564 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
566 // Verify input parameters.
568 if (FvImage
== NULL
) {
569 return EFI_INVALID_PARAMETER
;
573 // Calculate the pad file size
577 // Append extension header size
579 if (ExtHeader
!= NULL
) {
580 PadFileSize
= ExtHeader
->ExtHeaderSize
;
581 if (PadFileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
582 CurFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
584 PadFileSize
+= CurFfsHeaderSize
;
586 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
587 if (NextFfsSize
>= MAX_FFS_SIZE
) {
588 NextFfsHeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
591 // Check if a pad file is necessary
593 if (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ NextFfsHeaderSize
) % DataAlignment
== 0) {
596 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
) + NextFfsHeaderSize
;
598 // Add whatever it takes to get to the next aligned address
600 while ((PadFileSize
% DataAlignment
) != 0) {
604 // Subtract the next file header size
606 PadFileSize
-= NextFfsHeaderSize
;
608 // Subtract the starting offset to get size
610 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
614 // Verify that we have enough space for the file header
616 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
617 return EFI_OUT_OF_RESOURCES
;
621 // Write pad file header
623 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
626 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
628 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
629 PadFile
->Attributes
= 0;
632 // Write pad file size (calculated size minus next file header size)
634 if (PadFileSize
>= MAX_FFS_SIZE
) {
635 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
636 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= PadFileSize
;
637 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
639 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
640 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
641 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
645 // Fill in checksums and state, they must be 0 for checksumming.
647 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
648 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
650 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, CurFfsHeaderSize
);
651 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
653 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
655 (EFI_FFS_FILE_HEADER
*) PadFile
,
656 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
660 // Update the current FV pointer
662 FvImage
->CurrentFilePointer
+= PadFileSize
;
664 if (ExtHeader
!= NULL
) {
666 // Copy Fv Extension Header and Set Fv Extension header offset
668 memcpy ((UINT8
*)PadFile
+ CurFfsHeaderSize
, ExtHeader
, ExtHeader
->ExtHeaderSize
);
669 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) ((UINT8
*)PadFile
+ CurFfsHeaderSize
) - (UINTN
) FvImage
->FileImage
);
671 // Make next file start at QWord Boundry
673 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
674 FvImage
->CurrentFilePointer
++;
683 IN EFI_FFS_FILE_HEADER
*FileBuffer
689 This function checks the header to validate if it is a VTF file
693 FileBuffer Buffer in which content of a file has been read.
697 TRUE If this is a VTF file
698 FALSE If this is not a VTF file
702 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
711 IN OUT
FILE *FvMapFile
,
713 IN EFI_FFS_FILE_HEADER
*FfsFile
,
714 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
715 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
721 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
722 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
726 FvMapFile A pointer to FvMap File
727 FileName Ffs File PathName
728 FfsFile A pointer to Ffs file image.
729 ImageBaseAddress PeImage Base Address.
730 pImageContext Image Context Information.
734 EFI_SUCCESS Added required map information.
738 CHAR8 PeMapFileName
[MAX_LONG_FILE_PATH
];
740 CHAR8 FileGuidName
[MAX_LINE_LEN
];
742 CHAR8 Line
[MAX_LINE_LEN
];
743 CHAR8 KeyWord
[MAX_LINE_LEN
];
744 CHAR8 FunctionName
[MAX_LINE_LEN
];
745 EFI_PHYSICAL_ADDRESS FunctionAddress
;
747 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
749 UINT32 AddressOfEntryPoint
;
751 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
752 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
753 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
754 long long TempLongAddress
;
755 UINT32 TextVirtualAddress
;
756 UINT32 DataVirtualAddress
;
757 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
760 // Init local variable
764 // Print FileGuid to string buffer.
766 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
769 // Construct Map file Name
771 strcpy (PeMapFileName
, FileName
);
774 // Change '\\' to '/', unified path format.
776 Cptr
= PeMapFileName
;
777 while (*Cptr
!= '\0') {
779 *Cptr
= FILE_SEP_CHAR
;
787 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
788 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
791 if (Cptr
< PeMapFileName
) {
792 return EFI_NOT_FOUND
;
804 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
808 strcpy (KeyWord
, Cptr
+ 1);
812 // AddressOfEntryPoint and Offset in Image
814 if (!pImageContext
->IsTeImage
) {
815 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
816 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
818 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
821 sizeof (EFI_IMAGE_FILE_HEADER
) +
822 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
824 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
826 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
827 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
828 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
829 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
830 Index
= TEImageHeader
->NumberOfSections
;
834 // module information output
836 if (ImageBaseAddress
== 0) {
837 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
838 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
840 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
841 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
844 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&& pImageContext
->Machine
== EFI_IMAGE_MACHINE_IA64
) {
846 // Process IPF PLABEL to get the real address after the image has been rebased.
847 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
849 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64
*)((UINTN
) pImageContext
->Handle
+ (UINTN
) AddressOfEntryPoint
)));
851 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
853 fprintf (FvMapFile
, ")\n");
855 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
856 TextVirtualAddress
= 0;
857 DataVirtualAddress
= 0;
858 for (; Index
> 0; Index
--, SectionHeader
++) {
859 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
860 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
861 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
862 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
863 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
864 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
867 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
868 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
869 fprintf (FvMapFile
, ")\n\n");
874 PeMapFile
= fopen (LongFilePath (PeMapFileName
), "r");
875 if (PeMapFile
== NULL
) {
876 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
879 VerboseMsg ("The map file is %s", PeMapFileName
);
882 // Output Functions information into Fv Map file
884 LinkTimeBaseAddress
= 0;
885 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
889 if (Line
[0] == 0x0a) {
894 // By Address and Static keyword
896 if (FunctionType
== 0) {
897 sscanf (Line
, "%s", KeyWord
);
898 if (stricmp (KeyWord
, "Address") == 0) {
903 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
904 } else if (stricmp (KeyWord
, "Static") == 0) {
906 // static function list
909 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
910 } else if (stricmp (KeyWord
, "Preferred") ==0) {
911 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
912 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
917 // Printf Function Information
919 if (FunctionType
== 1) {
920 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
921 FunctionAddress
= (UINT64
) TempLongAddress
;
922 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
923 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
924 fprintf (FvMapFile
, "%s\n", FunctionName
);
926 } else if (FunctionType
== 2) {
927 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
928 FunctionAddress
= (UINT64
) TempLongAddress
;
929 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
930 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
931 fprintf (FvMapFile
, "%s\n", FunctionName
);
938 fprintf (FvMapFile
, "\n\n");
946 AdjustInternalFfsPadding (
947 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
948 IN OUT MEMORY_FILE
*FvImage
,
950 IN OUT UINTN
*FileSize
956 This function looks for a dedicated alignment padding section in the FFS, and
957 shrinks it to the size required to line up subsequent sections correctly.
961 FfsFile A pointer to Ffs file image.
962 FvImage The memory image of the FV to adjust it to.
963 Alignment Current file alignment
964 FileSize Reference to a variable holding the size of the FFS file
968 TRUE Padding section was found and updated successfully
973 EFI_FILE_SECTION_POINTER PadSection
;
976 UINT32 FfsHeaderLength
;
977 UINT32 FfsFileLength
;
980 EFI_FFS_INTEGRITY_CHECK
*IntegrityCheck
;
983 // Figure out the misalignment: all FFS sections are aligned relative to the
984 // start of the FFS payload, so use that as the base of the misalignment
987 FfsHeaderLength
= GetFfsHeaderLength(FfsFile
);
988 Misalignment
= (UINTN
) FvImage
->CurrentFilePointer
-
989 (UINTN
) FvImage
->FileImage
+ FfsHeaderLength
;
990 Misalignment
&= Alignment
- 1;
991 if (Misalignment
== 0) {
992 // Nothing to do, return success
997 // We only apply this optimization to FFS files with the FIXED attribute set,
998 // since the FFS will not be loadable at arbitrary offsets anymore after
999 // we adjust the size of the padding section.
1001 if ((FfsFile
->Attributes
& FFS_ATTRIB_FIXED
) == 0) {
1006 // Look for a dedicated padding section that we can adjust to compensate
1007 // for the misalignment. If such a padding section exists, it precedes all
1008 // sections with alignment requirements, and so the adjustment will correct
1011 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FREEFORM_SUBTYPE_GUID
, 1,
1013 if (EFI_ERROR (Status
) ||
1014 CompareGuid (&PadSection
.FreeformSubtypeSection
->SubTypeGuid
,
1015 &mEfiFfsSectionAlignmentPaddingGuid
) != 0) {
1020 // Find out if the size of the padding section is sufficient to compensate
1021 // for the misalignment.
1023 PadSize
= GetSectionFileLength (PadSection
.CommonHeader
);
1024 if (Misalignment
> PadSize
- sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION
)) {
1029 // Move the remainder of the FFS file towards the front, and adjust the
1030 // file size output parameter.
1032 Remainder
= (UINT8
*) PadSection
.CommonHeader
+ PadSize
;
1033 memmove (Remainder
- Misalignment
, Remainder
,
1034 *FileSize
- (UINTN
) (Remainder
- (UINTN
) FfsFile
));
1035 *FileSize
-= Misalignment
;
1038 // Update the padding section's length with the new values. Note that the
1039 // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2
1042 PadSize
-= Misalignment
;
1043 PadSection
.CommonHeader
->Size
[0] = (UINT8
) (PadSize
& 0xff);
1044 PadSection
.CommonHeader
->Size
[1] = (UINT8
) ((PadSize
& 0xff00) >> 8);
1045 PadSection
.CommonHeader
->Size
[2] = (UINT8
) ((PadSize
& 0xff0000) >> 16);
1048 // Update the FFS header with the new overall length
1050 FfsFileLength
= GetFfsFileLength (FfsFile
) - Misalignment
;
1051 if (FfsHeaderLength
> sizeof(EFI_FFS_FILE_HEADER
)) {
1052 ((EFI_FFS_FILE_HEADER2
*)FfsFile
)->ExtendedSize
= FfsFileLength
;
1054 FfsFile
->Size
[0] = (UINT8
) (FfsFileLength
& 0x000000FF);
1055 FfsFile
->Size
[1] = (UINT8
) ((FfsFileLength
& 0x0000FF00) >> 8);
1056 FfsFile
->Size
[2] = (UINT8
) ((FfsFileLength
& 0x00FF0000) >> 16);
1060 // Clear the alignment bits: these have become meaningless now that we have
1061 // adjusted the padding section.
1063 FfsFile
->Attributes
&= ~FFS_ATTRIB_DATA_ALIGNMENT
;
1066 // Recalculate the FFS header checksum. Instead of setting Header and State
1067 // both to zero, set Header to (UINT8)(-State) so State preserves its original
1070 IntegrityCheck
= &FfsFile
->IntegrityCheck
;
1071 IntegrityCheck
->Checksum
.Header
= (UINT8
) (0x100 - FfsFile
->State
);
1072 IntegrityCheck
->Checksum
.File
= 0;
1074 IntegrityCheck
->Checksum
.Header
= CalculateChecksum8 (
1075 (UINT8
*) FfsFile
, FfsHeaderLength
);
1077 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1079 // Ffs header checksum = zero, so only need to calculate ffs body.
1081 IntegrityCheck
->Checksum
.File
= CalculateChecksum8 (
1082 (UINT8
*) FfsFile
+ FfsHeaderLength
,
1083 FfsFileLength
- FfsHeaderLength
);
1085 IntegrityCheck
->Checksum
.File
= FFS_FIXED_CHECKSUM
;
1093 IN OUT MEMORY_FILE
*FvImage
,
1096 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
1098 IN
FILE *FvReportFile
1102 Routine Description:
1104 This function adds a file to the FV image. The file will pad to the
1105 appropriate alignment if required.
1109 FvImage The memory image of the FV to add it to. The current offset
1111 FvInfo Pointer to information about the FV.
1112 Index The file in the FvInfo file list to add.
1113 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
1114 to the end of the FvImage then no VTF previously found.
1115 FvMapFile Pointer to FvMap File
1116 FvReportFile Pointer to FvReport File
1120 EFI_SUCCESS The function completed successfully.
1121 EFI_INVALID_PARAMETER One of the input parameters was invalid.
1122 EFI_ABORTED An error occurred.
1123 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
1131 UINT32 CurrentFileAlignment
;
1134 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
1138 // Verify input parameters.
1140 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
1141 return EFI_INVALID_PARAMETER
;
1145 // Read the file to add
1147 NewFile
= fopen (LongFilePath (FvInfo
->FvFiles
[Index
]), "rb");
1149 if (NewFile
== NULL
) {
1150 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
1155 // Get the file size
1157 FileSize
= _filelength (fileno (NewFile
));
1160 // Read the file into a buffer
1162 FileBuffer
= malloc (FileSize
);
1163 if (FileBuffer
== NULL
) {
1164 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
1165 return EFI_OUT_OF_RESOURCES
;
1168 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
1171 // Done with the file, from this point on we will just use the buffer read.
1176 // Verify read successful
1178 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1180 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1185 // For None PI Ffs file, directly add them into FvImage.
1187 if (!FvInfo
->IsPiFvImage
) {
1188 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1189 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1190 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1192 FvImage
->CurrentFilePointer
+= FileSize
;
1200 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1201 if (EFI_ERROR (Status
)) {
1203 Error (NULL
, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo
->FvFiles
[Index
]);
1204 return EFI_INVALID_PARAMETER
;
1208 // Verify space exists to add the file
1210 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1212 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1213 return EFI_OUT_OF_RESOURCES
;
1217 // Verify the input file is the duplicated file in this Fv image
1219 for (Index1
= 0; Index1
< Index
; Index1
++) {
1220 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1221 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1222 PrintGuid ((EFI_GUID
*) FileBuffer
);
1224 return EFI_INVALID_PARAMETER
;
1227 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1230 // Update the file state based on polarity of the FV.
1232 UpdateFfsFileState (
1233 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1234 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1238 // Check if alignment is required
1240 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1243 // Find the largest alignment of all the FFS files in the FV
1245 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1246 MaxFfsAlignment
= CurrentFileAlignment
;
1249 // If we have a VTF file, add it at the top.
1251 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1252 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1254 // No previous VTF, add this one.
1256 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1258 // Sanity check. The file MUST align appropriately
1260 if (((UINTN
) *VtfFileImage
+ GetFfsHeaderLength((EFI_FFS_FILE_HEADER
*)FileBuffer
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1261 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1266 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1267 // Rebase for the debug genfvmap tool
1269 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1270 if (EFI_ERROR (Status
)) {
1271 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1277 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1279 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1280 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1283 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1287 // Already found a VTF file.
1289 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1296 // Add pad file if necessary
1298 if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER
*) FileBuffer
, FvImage
,
1299 1 << CurrentFileAlignment
, &FileSize
)) {
1300 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
, FileSize
);
1301 if (EFI_ERROR (Status
)) {
1302 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1310 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1312 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1313 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1315 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1316 if (EFI_ERROR (Status
)) {
1317 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1323 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1324 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1325 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1326 FvImage
->CurrentFilePointer
+= FileSize
;
1328 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1333 // Make next file start at QWord Boundry
1335 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1336 FvImage
->CurrentFilePointer
++;
1341 // Free allocated memory.
1350 IN MEMORY_FILE
*FvImage
,
1351 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1355 Routine Description:
1357 This function places a pad file between the last file in the FV and the VTF
1358 file if the VTF file exists.
1362 FvImage Memory file for the FV memory image
1363 VtfFileImage The address of the VTF file. If this is the end of the FV
1364 image, no VTF exists and no pad file is needed.
1368 EFI_SUCCESS Completed successfully.
1369 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1373 EFI_FFS_FILE_HEADER
*PadFile
;
1375 UINT32 FfsHeaderSize
;
1378 // If there is no VTF or the VTF naturally follows the previous file without a
1379 // pad file, then there's nothing to do
1381 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1382 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1386 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1387 return EFI_INVALID_PARAMETER
;
1391 // Pad file starts at beginning of free space
1393 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1396 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1398 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1399 PadFile
->Attributes
= 0;
1402 // FileSize includes the EFI_FFS_FILE_HEADER
1404 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1405 if (FileSize
>= MAX_FFS_SIZE
) {
1406 PadFile
->Attributes
|= FFS_ATTRIB_LARGE_FILE
;
1407 memset(PadFile
->Size
, 0, sizeof(UINT8
) * 3);
1408 ((EFI_FFS_FILE_HEADER2
*)PadFile
)->ExtendedSize
= FileSize
;
1409 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
1412 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1413 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1414 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1415 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
1419 // Fill in checksums and state, must be zero during checksum calculation.
1421 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1422 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1424 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, FfsHeaderSize
);
1425 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1427 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1429 UpdateFfsFileState (
1430 (EFI_FFS_FILE_HEADER
*) PadFile
,
1431 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1434 // Update the current FV pointer
1436 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1443 IN MEMORY_FILE
*FvImage
,
1445 IN EFI_FFS_FILE_HEADER
*VtfFile
1449 Routine Description:
1451 This parses the FV looking for the PEI core and then plugs the address into
1452 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1453 complete an IA32 Bootstrap FV.
1457 FvImage Memory file for the FV memory image
1458 FvInfo Information read from INF file.
1459 VtfFile Pointer to the VTF file in the FV image.
1463 EFI_SUCCESS Function Completed successfully.
1464 EFI_ABORTED Error encountered.
1465 EFI_INVALID_PARAMETER A required parameter was NULL.
1466 EFI_NOT_FOUND PEI Core file not found.
1470 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1471 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1473 EFI_FILE_SECTION_POINTER Pe32Section
;
1477 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1478 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1479 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
1480 INT32 Ia32SecEntryOffset
;
1481 UINT32
*Ia32ResetAddressPtr
;
1483 UINT8
*BytePointer2
;
1484 UINT16
*WordPointer
;
1488 EFI_FFS_FILE_STATE SavedState
;
1490 FIT_TABLE
*FitTablePtr
;
1491 BOOLEAN Vtf0Detected
;
1492 UINT32 FfsHeaderSize
;
1493 UINT32 SecHeaderSize
;
1496 // Verify input parameters
1498 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1499 return EFI_INVALID_PARAMETER
;
1502 // Initialize FV library
1504 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1509 Status
= VerifyFfsFile (VtfFile
);
1510 if (EFI_ERROR (Status
)) {
1511 return EFI_INVALID_PARAMETER
;
1515 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1516 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1517 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1518 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1519 IA32_X64_VTF0_SIGNATURE
)
1521 Vtf0Detected
= TRUE
;
1523 Vtf0Detected
= FALSE
;
1527 // Find the Sec Core
1529 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1530 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1533 // If the SEC core file is not found, but the VTF-0 signature
1534 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1535 // This means no modifications are required to the VTF.
1540 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1544 // Sec Core found, now find PE32 section
1546 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1547 if (Status
== EFI_NOT_FOUND
) {
1548 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1551 if (EFI_ERROR (Status
)) {
1552 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1556 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1557 Status
= GetPe32Info (
1558 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1564 if (EFI_ERROR (Status
)) {
1565 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1571 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1572 MachineType
== EFI_IMAGE_MACHINE_X64
)
1575 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1576 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1577 // This means no modifications are required to the VTF.
1583 // Physical address is FV base + offset of PE32 + offset of the entry point
1585 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1586 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1587 SecCorePhysicalAddress
+= EntryPoint
;
1588 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1591 // Find the PEI Core
1593 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1594 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
1595 Error (NULL
, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1599 // PEI Core found, now find PE32 or TE section
1601 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1602 if (Status
== EFI_NOT_FOUND
) {
1603 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1606 if (EFI_ERROR (Status
)) {
1607 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1611 SecHeaderSize
= GetSectionHeaderLength(Pe32Section
.CommonHeader
);
1612 Status
= GetPe32Info (
1613 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
),
1619 if (EFI_ERROR (Status
)) {
1620 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1624 // Physical address is FV base + offset of PE32 + offset of the entry point
1626 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1627 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ SecHeaderSize
- (UINTN
) FvImage
->FileImage
;
1628 PeiCorePhysicalAddress
+= EntryPoint
;
1629 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1631 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
1633 // Update PEI_CORE address
1636 // Set the uncached attribute bit in the physical address
1638 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
1641 // Check if address is aligned on a 16 byte boundary
1643 if (PeiCorePhysicalAddress
& 0xF) {
1644 Error (NULL
, 0, 3000, "Invalid",
1645 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1646 (unsigned long long) PeiCorePhysicalAddress
1651 // First Get the FIT table address
1653 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
1655 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
1657 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
1659 if (!EFI_ERROR (Status
)) {
1660 UpdateFitCheckSum (FitTablePtr
);
1664 // Update SEC_CORE address
1667 // Set the uncached attribute bit in the physical address
1669 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
1671 // Check if address is aligned on a 16 byte boundary
1673 if (SecCorePhysicalAddress
& 0xF) {
1674 Error (NULL
, 0, 3000, "Invalid",
1675 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1676 (unsigned long long) SecCorePhysicalAddress
1681 // Update the address
1683 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
1684 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
1686 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1688 // Get the location to update
1690 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1693 // Write lower 32 bits of physical address for Pei Core entry
1695 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1698 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1700 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1702 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1703 if (Ia32SecEntryOffset
<= -65536) {
1704 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1705 return STATUS_ERROR
;
1708 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1711 // Update the BFV base address
1713 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1714 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1715 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1718 // Update the Startup AP in the FVH header block ZeroVector region.
1720 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1721 if (FvInfo
->Size
<= 0x10000) {
1722 BytePointer2
= m64kRecoveryStartupApDataArray
;
1723 } else if (FvInfo
->Size
<= 0x20000) {
1724 BytePointer2
= m128kRecoveryStartupApDataArray
;
1726 BytePointer2
= m128kRecoveryStartupApDataArray
;
1728 // Find the position to place Ap reset vector, the offset
1729 // between the position and the end of Fvrecovery.fv file
1730 // should not exceed 128kB to prevent Ap reset vector from
1731 // outside legacy E and F segment
1733 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1734 if (EFI_ERROR (Status
)) {
1735 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.");
1740 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1741 BytePointer
[Index
] = BytePointer2
[Index
];
1744 // Calculate the checksum
1747 WordPointer
= (UINT16
*) (BytePointer
);
1748 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1749 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1753 // Update the checksum field
1755 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1756 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1759 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1761 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1762 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1763 if ((IpiVector
& 0xFFF) != 0) {
1764 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1767 IpiVector
= IpiVector
>> 12;
1768 IpiVector
= IpiVector
& 0xFF;
1771 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1773 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1774 *Ia32ResetAddressPtr
= IpiVector
;
1775 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1777 // Since the ARM 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...
1780 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
1782 // Since the AArch64 reset vector is in the FV Header you really don't need a
1783 // Volume Top File, but if you have one for some reason don't crash...
1786 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1791 // Now update file checksum
1793 SavedState
= VtfFile
->State
;
1794 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1796 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1797 FfsHeaderSize
= GetFfsHeaderLength(VtfFile
);
1798 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1799 (UINT8
*) ((UINT8
*)VtfFile
+ FfsHeaderSize
),
1800 GetFfsFileLength (VtfFile
) - FfsHeaderSize
1803 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1806 VtfFile
->State
= SavedState
;
1813 IN VOID
*FvImageBuffer
,
1815 IN EFI_FV_FILETYPE FileType
,
1816 OUT EFI_FILE_SECTION_POINTER
*Pe32Section
1820 Routine Description:
1822 Recursively searches the FV for the FFS file of specified type (typically
1823 SEC or PEI core) and extracts the PE32 section for further processing.
1827 FvImageBuffer Buffer containing FV data
1828 FvSize Size of the FV
1829 FileType Type of FFS file to search for
1830 Pe32Section PE32 section pointer when FFS file is found.
1834 EFI_SUCCESS Function Completed successfully.
1835 EFI_ABORTED Error encountered.
1836 EFI_INVALID_PARAMETER A required parameter was NULL.
1837 EFI_NOT_FOUND Core file not found.
1842 EFI_FIRMWARE_VOLUME_HEADER
*OrigFvHeader
;
1843 UINT32 OrigFvLength
;
1844 EFI_FFS_FILE_HEADER
*CoreFfsFile
;
1845 UINTN FvImageFileCount
;
1846 EFI_FFS_FILE_HEADER
*FvImageFile
;
1847 UINTN EncapFvSectionCount
;
1848 EFI_FILE_SECTION_POINTER EncapFvSection
;
1849 EFI_FIRMWARE_VOLUME_HEADER
*EncapsulatedFvHeader
;
1851 if (Pe32Section
== NULL
) {
1852 return EFI_INVALID_PARAMETER
;
1856 // Initialize FV library, saving previous values
1858 OrigFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)NULL
;
1859 GetFvHeader (&OrigFvHeader
, &OrigFvLength
);
1860 InitializeFvLib(FvImageBuffer
, (UINT32
)FvSize
);
1863 // First see if we can obtain the file directly in outer FV
1865 Status
= GetFileByType(FileType
, 1, &CoreFfsFile
);
1866 if (!EFI_ERROR(Status
) && (CoreFfsFile
!= NULL
) ) {
1869 // Core found, now find PE32 or TE section
1871 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_PE32
, 1, Pe32Section
);
1872 if (EFI_ERROR(Status
)) {
1873 Status
= GetSectionByType(CoreFfsFile
, EFI_SECTION_TE
, 1, Pe32Section
);
1876 if (EFI_ERROR(Status
)) {
1877 Error(NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the core file.");
1882 // Core PE/TE section, found, return
1884 Status
= EFI_SUCCESS
;
1889 // File was not found, look for FV Image file
1892 // iterate through all FV image files in outer FV
1893 for (FvImageFileCount
= 1;; FvImageFileCount
++) {
1895 Status
= GetFileByType(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
, FvImageFileCount
, &FvImageFile
);
1897 if (EFI_ERROR(Status
) || (FvImageFile
== NULL
) ) {
1898 // exit FV image file loop, no more found
1902 // Found an fv image file, look for an FV image section. The PI spec does not
1903 // preclude multiple FV image sections so we loop accordingly.
1904 for (EncapFvSectionCount
= 1;; EncapFvSectionCount
++) {
1906 // Look for the next FV image section. The section search code will
1907 // iterate into encapsulation sections. For example, it will iterate
1908 // into an EFI_SECTION_GUID_DEFINED encapsulation section to find the
1909 // EFI_SECTION_FIRMWARE_VOLUME_IMAGE sections contained therein.
1910 Status
= GetSectionByType(FvImageFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, EncapFvSectionCount
, &EncapFvSection
);
1912 if (EFI_ERROR(Status
)) {
1913 // exit section inner loop, no more found
1917 EncapsulatedFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)((UINT8
*)EncapFvSection
.FVImageSection
+ GetSectionHeaderLength(EncapFvSection
.FVImageSection
));
1919 // recurse to search the encapsulated FV for this core file type
1920 Status
= FindCorePeSection(EncapsulatedFvHeader
, EncapsulatedFvHeader
->FvLength
, FileType
, Pe32Section
);
1922 if (!EFI_ERROR(Status
)) {
1923 // we found the core in the capsulated image, success
1927 } // end encapsulated fv image section loop
1928 } // end fv image file loop
1930 // core was not found
1931 Status
= EFI_NOT_FOUND
;
1935 // restore FV lib values
1936 if(OrigFvHeader
!= NULL
) {
1937 InitializeFvLib(OrigFvHeader
, OrigFvLength
);
1945 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1946 OUT UINT16
*CoreMachineType
1950 Routine Description:
1952 Returns the machine type of a P32 image, typically SEC or PEI core.
1956 Pe32Section PE32 section data
1957 CoreMachineType The extracted machine type
1961 EFI_SUCCESS Function Completed successfully.
1962 EFI_ABORTED Error encountered.
1963 EFI_INVALID_PARAMETER A required parameter was NULL.
1971 if (CoreMachineType
== NULL
) {
1972 return EFI_INVALID_PARAMETER
;
1975 Status
= GetPe32Info(
1976 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
1981 if (EFI_ERROR(Status
)) {
1982 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 machine type for the core.");
1990 GetCoreEntryPointAddress(
1991 IN VOID
*FvImageBuffer
,
1993 IN EFI_FILE_SECTION_POINTER Pe32Section
,
1994 OUT EFI_PHYSICAL_ADDRESS
*CoreEntryAddress
1998 Routine Description:
2000 Returns the physical address of the core (SEC or PEI) entry point.
2004 FvImageBuffer Pointer to buffer containing FV data
2005 FvInfo Info for the parent FV
2006 Pe32Section PE32 section data
2007 CoreEntryAddress The extracted core entry physical address
2011 EFI_SUCCESS Function Completed successfully.
2012 EFI_ABORTED Error encountered.
2013 EFI_INVALID_PARAMETER A required parameter was NULL.
2021 EFI_PHYSICAL_ADDRESS EntryPhysicalAddress
;
2023 if (CoreEntryAddress
== NULL
) {
2024 return EFI_INVALID_PARAMETER
;
2027 Status
= GetPe32Info(
2028 (VOID
*)((UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
)),
2033 if (EFI_ERROR(Status
)) {
2034 Error(NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the core.");
2039 // Physical address is FV base + offset of PE32 + offset of the entry point
2041 EntryPhysicalAddress
= FvInfo
->BaseAddress
;
2042 EntryPhysicalAddress
+= (UINTN
)Pe32Section
.Pe32Section
+ GetSectionHeaderLength(Pe32Section
.CommonHeader
) - (UINTN
)FvImageBuffer
;
2043 EntryPhysicalAddress
+= EntryPoint
;
2045 *CoreEntryAddress
= EntryPhysicalAddress
;
2051 UpdateArmResetVectorIfNeeded (
2052 IN MEMORY_FILE
*FvImage
,
2057 Routine Description:
2058 This parses the FV looking for SEC and patches that address into the
2059 beginning of the FV header.
2061 For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
2062 For AArch64 the reset vector is at 0x00000000.
2064 This would commonly map to the first entry in the ROM.
2074 We support two schemes on ARM.
2075 1) Beginning of the FV is the reset vector
2076 2) Reset vector is data bytes FDF file and that code branches to reset vector
2077 in the beginning of the FV (fixed size offset).
2079 Need to have the jump for the reset vector at location zero.
2080 We also need to store the address or PEI (if it exists).
2081 We stub out a return from interrupt in case the debugger
2082 is using SWI (not done for AArch64, not enough space in struct).
2083 The optional entry to the common exception handler is
2084 to support full featured exception handling from ROM and is currently
2085 not support by this tool.
2088 FvImage Memory file for the FV memory image
2089 FvInfo Information read from INF file.
2093 EFI_SUCCESS Function Completed successfully.
2094 EFI_ABORTED Error encountered.
2095 EFI_INVALID_PARAMETER A required parameter was NULL.
2096 EFI_NOT_FOUND PEI Core file not found.
2101 EFI_FILE_SECTION_POINTER SecPe32
;
2102 EFI_FILE_SECTION_POINTER PeiPe32
;
2103 BOOLEAN UpdateVectorSec
= FALSE
;
2104 BOOLEAN UpdateVectorPei
= FALSE
;
2105 UINT16 MachineType
= 0;
2106 EFI_PHYSICAL_ADDRESS SecCoreEntryAddress
= 0;
2107 UINT16 PeiMachineType
= 0;
2108 EFI_PHYSICAL_ADDRESS PeiCoreEntryAddress
= 0;
2111 // Verify input parameters
2113 if (FvImage
== NULL
|| FvInfo
== NULL
) {
2114 return EFI_INVALID_PARAMETER
;
2118 // Locate an SEC Core instance and if found extract the machine type and entry point address
2120 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_SECURITY_CORE
, &SecPe32
);
2121 if (!EFI_ERROR(Status
)) {
2123 Status
= GetCoreMachineType(SecPe32
, &MachineType
);
2124 if (EFI_ERROR(Status
)) {
2125 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC Core.");
2129 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, SecPe32
, &SecCoreEntryAddress
);
2130 if (EFI_ERROR(Status
)) {
2131 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for SEC Core.");
2135 VerboseMsg("UpdateArmResetVectorIfNeeded found SEC core entry at 0x%llx", (unsigned long long)SecCoreEntryAddress
);
2136 UpdateVectorSec
= TRUE
;
2140 // Locate a PEI Core instance and if found extract the machine type and entry point address
2142 Status
= FindCorePeSection(FvImage
->FileImage
, FvInfo
->Size
, EFI_FV_FILETYPE_PEI_CORE
, &PeiPe32
);
2143 if (!EFI_ERROR(Status
)) {
2145 Status
= GetCoreMachineType(PeiPe32
, &PeiMachineType
);
2146 if (EFI_ERROR(Status
)) {
2147 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for PEI Core.");
2151 Status
= GetCoreEntryPointAddress(FvImage
->FileImage
, FvInfo
, PeiPe32
, &PeiCoreEntryAddress
);
2152 if (EFI_ERROR(Status
)) {
2153 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 entry point address for PEI Core.");
2157 VerboseMsg("UpdateArmResetVectorIfNeeded found PEI core entry at 0x%llx", (unsigned long long)PeiCoreEntryAddress
);
2159 // if we previously found an SEC Core make sure machine types match
2160 if (UpdateVectorSec
&& (MachineType
!= PeiMachineType
)) {
2161 Error(NULL
, 0, 3000, "Invalid", "SEC and PEI machine types do not match, can't update reset vector");
2165 MachineType
= PeiMachineType
;
2168 UpdateVectorPei
= TRUE
;
2171 if (!UpdateVectorSec
&& !UpdateVectorPei
) {
2175 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
2176 // ARM: Array of 4 UINT32s:
2177 // 0 - is branch relative to SEC entry point
2178 // 1 - PEI Entry Point
2179 // 2 - movs pc,lr for a SWI handler
2180 // 3 - Place holder for Common Exception Handler
2181 UINT32 ResetVector
[4];
2183 memset(ResetVector
, 0, sizeof (ResetVector
));
2185 // if we found an SEC core entry point then generate a branch instruction
2186 // to it and populate a debugger SWI entry as well
2187 if (UpdateVectorSec
) {
2189 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector");
2191 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
2192 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
2193 ResetVector
[0] = (INT32
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
- 8) >> 2;
2195 if (ResetVector
[0] > 0x00FFFFFF) {
2196 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
2200 // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"
2201 ResetVector
[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION
;
2203 // SWI handler movs pc,lr. Just in case a debugger uses SWI
2204 ResetVector
[2] = 0xE1B0F07E;
2206 // Place holder to support a common interrupt handler from ROM.
2207 // Currently not suppprted. For this to be used the reset vector would not be in this FV
2208 // and the exception vectors would be hard coded in the ROM and just through this address
2209 // to find a common handler in the a module in the FV.
2213 // if a PEI core entry was found place its address in the vector area
2214 if (UpdateVectorPei
) {
2216 VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM PEI address");
2218 // Address of PEI Core, if we have one
2219 ResetVector
[1] = (UINT32
)PeiCoreEntryAddress
;
2223 // Copy to the beginning of the FV
2225 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2227 } else if (MachineType
== EFI_IMAGE_MACHINE_AARCH64
) {
2228 // AArch64: Used as UINT64 ResetVector[2]
2229 // 0 - is branch relative to SEC entry point
2230 // 1 - PEI Entry Point
2231 UINT64 ResetVector
[2];
2233 memset(ResetVector
, 0, sizeof (ResetVector
));
2236 ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
2237 array at the moment, for AArch64, does not allow us space for this as the header only
2238 allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
2239 within the first 4GB of addressable RAM we could potensioally adopt the same ResetVector
2240 layout as above. But for the moment we replace the four 32bit vectors with two 64bit
2241 vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
2245 // if we found an SEC core entry point then generate a branch instruction to it
2246 if (UpdateVectorSec
) {
2248 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 SEC vector");
2250 ResetVector
[0] = (UINT64
)(SecCoreEntryAddress
- FvInfo
->BaseAddress
) >> 2;
2252 // B SecEntryPoint - signed_immed_26 part +/-128MB offset
2253 if (ResetVector
[0] > 0x03FFFFFF) {
2254 Error(NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
2257 // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"
2258 ResetVector
[0] |= ARM64_UNCONDITIONAL_JUMP_INSTRUCTION
;
2261 // if a PEI core entry was found place its address in the vector area
2262 if (UpdateVectorPei
) {
2264 VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 PEI address");
2266 // Address of PEI Core, if we have one
2267 ResetVector
[1] = (UINT64
)PeiCoreEntryAddress
;
2271 // Copy to the beginning of the FV
2273 memcpy(FvImage
->FileImage
, ResetVector
, sizeof (ResetVector
));
2276 Error(NULL
, 0, 3000, "Invalid", "Unknown machine type");
2286 OUT UINT32
*EntryPoint
,
2287 OUT UINT32
*BaseOfCode
,
2288 OUT UINT16
*MachineType
2292 Routine Description:
2294 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
2295 See EfiImage.h for machine types. The entry point offset is from the beginning
2296 of the PE32 buffer passed in.
2300 Pe32 Beginning of the PE32.
2301 EntryPoint Offset from the beginning of the PE32 to the image entry point.
2302 BaseOfCode Base address of code.
2303 MachineType Magic number for the machine type.
2307 EFI_SUCCESS Function completed successfully.
2308 EFI_ABORTED Error encountered.
2309 EFI_INVALID_PARAMETER A required parameter was NULL.
2310 EFI_UNSUPPORTED The operation is unsupported.
2314 EFI_IMAGE_DOS_HEADER
*DosHeader
;
2315 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2316 EFI_TE_IMAGE_HEADER
*TeHeader
;
2319 // Verify input parameters
2322 return EFI_INVALID_PARAMETER
;
2326 // First check whether it is one TE Image.
2328 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
2329 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
2331 // By TeImage Header to get output
2333 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2334 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
2335 *MachineType
= TeHeader
->Machine
;
2339 // Then check whether
2340 // First is the DOS header
2342 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
2345 // Verify DOS header is expected
2347 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2348 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
2349 return EFI_UNSUPPORTED
;
2352 // Immediately following is the NT header.
2354 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
2357 // Verify NT header is expected
2359 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2360 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
2361 return EFI_UNSUPPORTED
;
2366 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
2367 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
2368 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
2372 // Verify machine type is supported
2374 if ((*MachineType
!= EFI_IMAGE_MACHINE_IA32
) && (*MachineType
!= EFI_IMAGE_MACHINE_IA64
) && (*MachineType
!= EFI_IMAGE_MACHINE_X64
) && (*MachineType
!= EFI_IMAGE_MACHINE_EBC
) &&
2375 (*MachineType
!= EFI_IMAGE_MACHINE_ARMT
) && (*MachineType
!= EFI_IMAGE_MACHINE_AARCH64
)) {
2376 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
2377 return EFI_UNSUPPORTED
;
2385 IN CHAR8
*InfFileImage
,
2386 IN UINTN InfFileSize
,
2387 IN CHAR8
*FvFileName
,
2388 IN CHAR8
*MapFileName
2392 Routine Description:
2394 This is the main function which will be called from application.
2398 InfFileImage Buffer containing the INF file contents.
2399 InfFileSize Size of the contents of the InfFileImage buffer.
2400 FvFileName Requested name for the FV file.
2401 MapFileName Fv map file to log fv driver information.
2405 EFI_SUCCESS Function completed successfully.
2406 EFI_OUT_OF_RESOURCES Could not allocate required resources.
2407 EFI_ABORTED Error encountered.
2408 EFI_INVALID_PARAMETER A required parameter was NULL.
2413 MEMORY_FILE InfMemoryFile
;
2414 MEMORY_FILE FvImageMemoryFile
;
2416 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
2417 EFI_FFS_FILE_HEADER
*VtfFileImage
;
2418 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
2424 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2425 FILE *FvExtHeaderFile
;
2427 CHAR8
*FvReportName
;
2430 FvBufferHeader
= NULL
;
2434 FvReportName
= NULL
;
2435 FvReportFile
= NULL
;
2437 if (InfFileImage
!= NULL
) {
2439 // Initialize file structures
2441 InfMemoryFile
.FileImage
= InfFileImage
;
2442 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2443 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2446 // Parse the FV inf file for header information
2448 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2449 if (EFI_ERROR (Status
)) {
2450 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2456 // Update the file name return values
2458 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2459 FvFileName
= mFvDataInfo
.FvName
;
2462 if (FvFileName
== NULL
) {
2463 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2467 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2468 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2473 // Debug message Fv File System Guid
2475 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2476 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2477 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2478 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2479 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2480 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2481 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2482 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2483 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2484 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2485 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2486 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2487 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2491 // Add PI FV extension header
2494 FvExtHeaderFile
= NULL
;
2495 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2497 // Open the FV Extension Header file
2499 FvExtHeaderFile
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
2500 if (FvExtHeaderFile
== NULL
) {
2501 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2506 // Get the file size
2508 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2511 // Allocate a buffer for the FV Extension Header
2513 FvExtHeader
= malloc(FileSize
);
2514 if (FvExtHeader
== NULL
) {
2515 fclose (FvExtHeaderFile
);
2516 return EFI_OUT_OF_RESOURCES
;
2520 // Read the FV Extension Header
2522 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2523 fclose (FvExtHeaderFile
);
2526 // See if there is an override for the FV Name GUID
2528 if (mFvDataInfo
.FvNameGuidSet
) {
2529 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2531 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2532 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2533 } else if (mFvDataInfo
.FvNameGuidSet
) {
2535 // Allocate a buffer for the FV Extension Header
2537 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2538 if (FvExtHeader
== NULL
) {
2539 return EFI_OUT_OF_RESOURCES
;
2541 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2542 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2546 // Debug message Fv Name Guid
2548 if (mFvDataInfo
.FvNameGuidSet
) {
2549 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2550 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2551 mFvDataInfo
.FvNameGuid
.Data2
,
2552 mFvDataInfo
.FvNameGuid
.Data3
,
2553 mFvDataInfo
.FvNameGuid
.Data4
[0],
2554 mFvDataInfo
.FvNameGuid
.Data4
[1],
2555 mFvDataInfo
.FvNameGuid
.Data4
[2],
2556 mFvDataInfo
.FvNameGuid
.Data4
[3],
2557 mFvDataInfo
.FvNameGuid
.Data4
[4],
2558 mFvDataInfo
.FvNameGuid
.Data4
[5],
2559 mFvDataInfo
.FvNameGuid
.Data4
[6],
2560 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2563 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0 ||
2564 CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem3Guid
) == 0) {
2565 mFvDataInfo
.IsPiFvImage
= TRUE
;
2569 // FvMap file to log the function address of all modules in one Fvimage
2571 if (MapFileName
!= NULL
) {
2572 if (strlen (MapFileName
) > MAX_LONG_FILE_PATH
- 1) {
2573 Error (NULL
, 0, 1003, "Invalid option value", "MapFileName %s is too long!", MapFileName
);
2574 Status
= EFI_ABORTED
;
2578 FvMapName
= malloc (strlen (MapFileName
) + 1);
2579 if (FvMapName
== NULL
) {
2580 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2581 Status
= EFI_OUT_OF_RESOURCES
;
2585 strcpy (FvMapName
, MapFileName
);
2587 if (strlen (FvFileName
) + strlen (".map") > MAX_LONG_FILE_PATH
- 1) {
2588 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2589 Status
= EFI_ABORTED
;
2593 FvMapName
= malloc (strlen (FvFileName
) + strlen (".map") + 1);
2594 if (FvMapName
== NULL
) {
2595 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2596 Status
= EFI_OUT_OF_RESOURCES
;
2600 strcpy (FvMapName
, FvFileName
);
2601 strcat (FvMapName
, ".map");
2603 VerboseMsg ("FV Map file name is %s", FvMapName
);
2606 // FvReport file to log the FV information in one Fvimage
2608 if (strlen (FvFileName
) + strlen (".txt") > MAX_LONG_FILE_PATH
- 1) {
2609 Error (NULL
, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName
);
2610 Status
= EFI_ABORTED
;
2614 FvReportName
= malloc (strlen (FvFileName
) + strlen (".txt") + 1);
2615 if (FvReportName
== NULL
) {
2616 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
2617 Status
= EFI_OUT_OF_RESOURCES
;
2621 strcpy (FvReportName
, FvFileName
);
2622 strcat (FvReportName
, ".txt");
2625 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2626 // And Update mFvDataInfo data.
2628 Status
= CalculateFvSize (&mFvDataInfo
);
2629 if (EFI_ERROR (Status
)) {
2632 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2635 // support fv image and empty fv image
2637 FvImageSize
= mFvDataInfo
.Size
;
2640 // Allocate the FV, assure FvImage Header 8 byte alignment
2642 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2643 if (FvBufferHeader
== NULL
) {
2644 Status
= EFI_OUT_OF_RESOURCES
;
2647 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2650 // Initialize the FV to the erase polarity
2652 if (mFvDataInfo
.FvAttributes
== 0) {
2654 // Set Default Fv Attribute
2656 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2658 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2659 memset (FvImage
, -1, FvImageSize
);
2661 memset (FvImage
, 0, FvImageSize
);
2665 // Initialize FV header
2667 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2670 // Initialize the zero vector to all zeros.
2672 memset (FvHeader
->ZeroVector
, 0, 16);
2675 // Copy the Fv file system GUID
2677 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2679 FvHeader
->FvLength
= FvImageSize
;
2680 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2681 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2682 FvHeader
->Revision
= EFI_FVH_REVISION
;
2683 FvHeader
->ExtHeaderOffset
= 0;
2684 FvHeader
->Reserved
[0] = 0;
2687 // Copy firmware block map
2689 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2690 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2691 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2695 // Add block map terminator
2697 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2698 FvHeader
->BlockMap
[Index
].Length
= 0;
2701 // Complete the header
2703 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2704 FvHeader
->Checksum
= 0;
2705 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2708 // If there is no FFS file, generate one empty FV
2710 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2715 // Initialize our "file" view of the buffer
2717 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2718 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2719 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2722 // Initialize the FV library.
2724 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2727 // Initialize the VTF file address.
2729 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2734 FvMapFile
= fopen (LongFilePath (FvMapName
), "w");
2735 if (FvMapFile
== NULL
) {
2736 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2737 Status
= EFI_ABORTED
;
2742 // Open FvReport file
2744 FvReportFile
= fopen (LongFilePath (FvReportName
), "w");
2745 if (FvReportFile
== NULL
) {
2746 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2747 Status
= EFI_ABORTED
;
2751 // record FV size information into FvMap file.
2753 if (mFvTotalSize
!= 0) {
2754 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2755 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2757 if (mFvTakenSize
!= 0) {
2758 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2759 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2761 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2762 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2763 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2767 // record FV size information to FvReportFile.
2769 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2770 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2773 // Add PI FV extension header
2775 if (FvExtHeader
!= NULL
) {
2777 // Add FV Extended Header contents to the FV as a PAD file
2779 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
, 0);
2782 // Fv Extension header change update Fv Header Check sum
2784 FvHeader
->Checksum
= 0;
2785 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2791 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2795 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2798 // Exit if error detected while adding the file
2800 if (EFI_ERROR (Status
)) {
2806 // If there is a VTF file, some special actions need to occur.
2808 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2810 // Pad from the end of the last file to the beginning of the VTF file.
2811 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2813 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2814 if (EFI_ERROR (Status
)) {
2815 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2820 // Update reset vector (SALE_ENTRY for IPF)
2821 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2822 // EndAddress of 0xFFFFFFFF (unless the section was rebased).
2823 // Thus, only this type fv needs to update the reset vector.
2824 // If the PEI Core is found, the VTF file will probably get
2825 // corrupted by updating the entry point.
2827 if (mFvDataInfo
.ForceRebase
== 1 ||
2828 (mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2829 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2830 if (EFI_ERROR(Status
)) {
2831 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2834 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2840 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2841 if (EFI_ERROR (Status
)) {
2842 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2847 // Update Checksum for FvHeader
2849 FvHeader
->Checksum
= 0;
2850 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2854 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2856 if (((FvHeader
->Attributes
& EFI_FVB2_WEAK_ALIGNMENT
) != EFI_FVB2_WEAK_ALIGNMENT
) &&
2857 (((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2858 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2860 // Update Checksum for FvHeader
2862 FvHeader
->Checksum
= 0;
2863 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2867 // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
2869 if (mIsLargeFfs
&& CompareGuid (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2870 memcpy (&FvHeader
->FileSystemGuid
, &mEfiFirmwareFileSystem3Guid
, sizeof (EFI_GUID
));
2871 FvHeader
->Checksum
= 0;
2872 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2879 FvFile
= fopen (LongFilePath (FvFileName
), "wb");
2880 if (FvFile
== NULL
) {
2881 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2882 Status
= EFI_ABORTED
;
2886 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2887 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2888 Status
= EFI_ABORTED
;
2893 if (FvBufferHeader
!= NULL
) {
2894 free (FvBufferHeader
);
2897 if (FvExtHeader
!= NULL
) {
2901 if (FvMapName
!= NULL
) {
2905 if (FvReportName
!= NULL
) {
2906 free (FvReportName
);
2909 if (FvFile
!= NULL
) {
2914 if (FvMapFile
!= NULL
) {
2919 if (FvReportFile
!= NULL
) {
2920 fflush (FvReportFile
);
2921 fclose (FvReportFile
);
2927 UpdatePeiCoreEntryInFit (
2928 IN FIT_TABLE
*FitTablePtr
,
2929 IN UINT64 PeiCorePhysicalAddress
2933 Routine Description:
2935 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2940 FitTablePtr - The pointer of FIT_TABLE.
2941 PeiCorePhysicalAddress - The address of Pei Core entry.
2945 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2946 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2950 FIT_TABLE
*TmpFitPtr
;
2952 UINTN NumFitComponents
;
2954 TmpFitPtr
= FitTablePtr
;
2955 NumFitComponents
= TmpFitPtr
->CompSize
;
2957 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2958 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2959 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2966 return EFI_NOT_FOUND
;
2971 IN FIT_TABLE
*FitTablePtr
2975 Routine Description:
2977 This function is used to update the checksum for FIT.
2982 FitTablePtr - The pointer of FIT_TABLE.
2990 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2991 FitTablePtr
->CheckSum
= 0;
2992 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
3001 Routine Description:
3002 Calculate the FV size and Update Fv Size based on the actual FFS files.
3003 And Update FvInfo data.
3006 FvInfoPtr - The pointer to FV_INFO structure.
3009 EFI_ABORTED - Ffs Image Error
3010 EFI_SUCCESS - Successfully update FvSize
3013 UINTN CurrentOffset
;
3017 UINTN FvExtendHeaderSize
;
3018 UINT32 FfsAlignment
;
3019 UINT32 FfsHeaderSize
;
3020 EFI_FFS_FILE_HEADER FfsHeader
;
3021 BOOLEAN VtfFileFlag
;
3024 FvExtendHeaderSize
= 0;
3026 VtfFileFlag
= FALSE
;
3031 // Compute size for easy access later
3033 FvInfoPtr
->Size
= 0;
3034 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
3035 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
3039 // Calculate the required sizes for all FFS files.
3041 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
3043 for (Index
= 1;; Index
++) {
3044 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
3045 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
3051 // Calculate PI extension header
3053 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
3054 fpin
= fopen (LongFilePath (mFvDataInfo
.FvExtHeaderFile
), "rb");
3056 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
3059 FvExtendHeaderSize
= _filelength (fileno (fpin
));
3061 if (sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
>= MAX_FFS_SIZE
) {
3062 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER2
) + FvExtendHeaderSize
;
3065 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
3067 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3068 } else if (mFvDataInfo
.FvNameGuidSet
) {
3069 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
3070 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
3074 // Accumlate every FFS file size.
3076 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
3081 fpin
= fopen (LongFilePath (FvInfoPtr
->FvFiles
[Index
]), "rb");
3083 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
3087 // Get the file size
3089 FfsFileSize
= _filelength (fileno (fpin
));
3090 if (FfsFileSize
>= MAX_FFS_SIZE
) {
3091 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER2
);
3094 FfsHeaderSize
= sizeof(EFI_FFS_FILE_HEADER
);
3097 // Read Ffs File header
3099 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
3105 if (FvInfoPtr
->IsPiFvImage
) {
3107 // Check whether this ffs file is vtf file
3109 if (IsVtfFile (&FfsHeader
)) {
3112 // One Fv image can't have two vtf files.
3114 Error (NULL
, 0, 3000,"Invalid", "One Fv image can't have two vtf files.");
3118 VtfFileSize
= FfsFileSize
;
3123 // Get the alignment of FFS file
3125 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
3126 FfsAlignment
= 1 << FfsAlignment
;
3130 if (((CurrentOffset
+ FfsHeaderSize
) % FfsAlignment
) != 0) {
3132 // Only EFI_FFS_FILE_HEADER is needed for a pad section.
3134 CurrentOffset
= (CurrentOffset
+ FfsHeaderSize
+ sizeof(EFI_FFS_FILE_HEADER
) + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
3135 CurrentOffset
-= FfsHeaderSize
;
3140 // Add ffs file size
3142 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
3143 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
3145 CurrentOffset
+= FfsFileSize
;
3149 // Make next ffs file start at QWord Boundry
3151 if (FvInfoPtr
->IsPiFvImage
) {
3152 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3155 CurrentOffset
+= VtfFileSize
;
3156 DebugMsg (NULL
, 0, 9, "FvImage size", "The calculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
3158 if (FvInfoPtr
->Size
== 0) {
3160 // Update FvInfo data
3162 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
3163 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
3164 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
3165 FvInfoPtr
->FvBlocks
[1].Length
= 0;
3166 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
3170 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
);
3171 return EFI_INVALID_PARAMETER
;
3175 // Set Fv Size Information
3177 mFvTotalSize
= FvInfoPtr
->Size
;
3178 mFvTakenSize
= CurrentOffset
;
3184 FfsRebaseImageRead (
3185 IN VOID
*FileHandle
,
3186 IN UINTN FileOffset
,
3187 IN OUT UINT32
*ReadSize
,
3192 Routine Description:
3194 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
3198 FileHandle - The handle to the PE/COFF file
3200 FileOffset - The offset, in bytes, into the file to read
3202 ReadSize - The number of bytes to read from the file starting at FileOffset
3204 Buffer - A pointer to the buffer to read the data into.
3208 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
3212 CHAR8
*Destination8
;
3216 Destination8
= Buffer
;
3217 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
3220 *(Destination8
++) = *(Source8
++);
3229 IN EFI_FFS_FILE_HEADER
*FfsFile
,
3234 Routine Description:
3236 This function gets all child FvImages in the input FfsFile, and records
3237 their base address to the parent image.
3240 FvInfo A pointer to FV_INFO struture.
3241 FfsFile A pointer to Ffs file image that may contain FvImage.
3242 XipOffset The offset address to the parent FvImage base.
3246 EFI_SUCCESS Base address of child Fv image is recorded.
3251 EFI_FILE_SECTION_POINTER SubFvSection
;
3252 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
3253 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
3254 EFI_FILE_SECTION_POINTER CorePe32
;
3257 for (Index
= 1;; Index
++) {
3261 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
3262 if (EFI_ERROR (Status
)) {
3265 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ GetSectionHeaderLength(SubFvSection
.FVImageSection
));
3268 // See if there's an SEC core in the child FV
3269 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_SECURITY_CORE
, &CorePe32
);
3271 // if we couldn't find the SEC core, look for a PEI core
3272 if (EFI_ERROR(Status
)) {
3273 Status
= FindCorePeSection(SubFvImageHeader
, SubFvImageHeader
->FvLength
, EFI_FV_FILETYPE_PEI_CORE
, &CorePe32
);
3276 if (!EFI_ERROR(Status
)) {
3277 Status
= GetCoreMachineType(CorePe32
, &MachineType
);
3278 if (EFI_ERROR(Status
)) {
3279 Error(NULL
, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC/PEI Core.");
3283 // machine type is ARM, set a flag so ARM reset vector procesing occurs
3284 if ((MachineType
== EFI_IMAGE_MACHINE_ARMT
) || (MachineType
== EFI_IMAGE_MACHINE_AARCH64
)) {
3285 VerboseMsg("Located ARM/AArch64 SEC/PEI core in child FV");
3293 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
3294 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
3302 IN OUT FV_INFO
*FvInfo
,
3304 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
3310 Routine Description:
3312 This function determines if a file is XIP and should be rebased. It will
3313 rebase any PE32 sections found in the file using the base address.
3317 FvInfo A pointer to FV_INFO struture.
3318 FileName Ffs File PathName
3319 FfsFile A pointer to Ffs file image.
3320 XipOffset The offset address to use for rebasing the XIP file image.
3321 FvMapFile FvMapFile to record the function address in one Fvimage
3325 EFI_SUCCESS The image was properly rebased.
3326 EFI_INVALID_PARAMETER An input parameter is invalid.
3327 EFI_ABORTED An error occurred while rebasing the input file image.
3328 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
3329 EFI_NOT_FOUND No compressed sections could be found.
3334 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
3335 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
3336 EFI_PHYSICAL_ADDRESS XipBase
;
3337 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
3339 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
3340 EFI_FFS_FILE_STATE SavedState
;
3341 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
3342 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
3343 UINT8
*MemoryImagePointer
;
3344 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
3345 CHAR8 PeFileName
[MAX_LONG_FILE_PATH
];
3348 UINT8
*PeFileBuffer
;
3351 UINT32 FfsHeaderSize
;
3352 UINT32 CurSecHdrSize
;
3355 MemoryImagePointer
= NULL
;
3356 TEImageHeader
= NULL
;
3358 SectionHeader
= NULL
;
3361 PeFileBuffer
= NULL
;
3364 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
3366 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
3371 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
3373 if (FvInfo
->ForceRebase
== 0) {
3378 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
3381 // We only process files potentially containing PE32 sections.
3383 switch (FfsFile
->Type
) {
3384 case EFI_FV_FILETYPE_SECURITY_CORE
:
3385 case EFI_FV_FILETYPE_PEI_CORE
:
3386 case EFI_FV_FILETYPE_PEIM
:
3387 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3388 case EFI_FV_FILETYPE_DRIVER
:
3389 case EFI_FV_FILETYPE_DXE_CORE
:
3391 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
3393 // Rebase the inside FvImage.
3395 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
3398 // Search PE/TE section in FV sectin.
3405 FfsHeaderSize
= GetFfsHeaderLength(FfsFile
);
3407 // Rebase each PE32 section
3409 Status
= EFI_SUCCESS
;
3410 for (Index
= 1;; Index
++) {
3414 NewPe32BaseAddress
= 0;
3419 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
3420 if (EFI_ERROR (Status
)) {
3423 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3426 // Initialize context
3428 memset (&ImageContext
, 0, sizeof (ImageContext
));
3429 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3430 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3431 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3432 if (EFI_ERROR (Status
)) {
3433 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3437 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3438 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3443 // Keep Image Context for PE image in FV
3445 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3448 // Get File PdbPointer
3450 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3453 // Get PeHeader pointer
3455 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ ImageContext
.PeCoffHeaderOffset
);
3458 // Calculate the PE32 base address, based on file type
3460 switch (FfsFile
->Type
) {
3461 case EFI_FV_FILETYPE_SECURITY_CORE
:
3462 case EFI_FV_FILETYPE_PEI_CORE
:
3463 case EFI_FV_FILETYPE_PEIM
:
3464 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
3466 // Check if section-alignment and file-alignment match or not
3468 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3470 // Xip module has the same section alignment and file alignment.
3472 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3476 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
3478 if (ImageContext
.RelocationsStripped
) {
3480 // Construct the original efi file Name
3482 strcpy (PeFileName
, FileName
);
3483 Cptr
= PeFileName
+ strlen (PeFileName
);
3484 while (*Cptr
!= '.') {
3488 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3496 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3497 if (PeFile
== NULL
) {
3498 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3499 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3500 //return EFI_ABORTED;
3504 // Get the file size
3506 PeFileSize
= _filelength (fileno (PeFile
));
3507 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3508 if (PeFileBuffer
== NULL
) {
3509 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3510 return EFI_OUT_OF_RESOURCES
;
3515 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3521 // Handle pointer to the original efi image.
3523 ImageContext
.Handle
= PeFileBuffer
;
3524 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3525 if (EFI_ERROR (Status
)) {
3526 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3529 ImageContext
.RelocationsStripped
= FALSE
;
3532 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3535 case EFI_FV_FILETYPE_DRIVER
:
3536 case EFI_FV_FILETYPE_DXE_CORE
:
3538 // Check if section-alignment and file-alignment match or not
3540 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3542 // Xip module has the same section alignment and file alignment.
3544 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3547 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
- (UINTN
)FfsFile
;
3552 // Not supported file type
3558 // Relocation doesn't exist
3560 if (ImageContext
.RelocationsStripped
) {
3561 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3566 // Relocation exist and rebase
3569 // Load and Relocate Image Data
3571 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3572 if (MemoryImagePointer
== NULL
) {
3573 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3574 return EFI_OUT_OF_RESOURCES
;
3576 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3577 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3579 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3580 if (EFI_ERROR (Status
)) {
3581 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3582 free ((VOID
*) MemoryImagePointer
);
3586 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3587 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3588 if (EFI_ERROR (Status
)) {
3589 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3590 free ((VOID
*) MemoryImagePointer
);
3595 // Copy Relocated data to raw image file.
3597 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3600 sizeof (EFI_IMAGE_FILE_HEADER
) +
3601 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3604 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3606 (UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
+ SectionHeader
->PointerToRawData
,
3607 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3608 SectionHeader
->SizeOfRawData
3612 free ((VOID
*) MemoryImagePointer
);
3613 MemoryImagePointer
= NULL
;
3614 if (PeFileBuffer
!= NULL
) {
3615 free (PeFileBuffer
);
3616 PeFileBuffer
= NULL
;
3620 // Update Image Base Address
3622 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3623 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3624 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3625 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3627 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3628 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3635 // Now update file checksum
3637 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3638 SavedState
= FfsFile
->State
;
3639 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3641 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3642 (UINT8
*) ((UINT8
*)FfsFile
+ FfsHeaderSize
),
3643 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3645 FfsFile
->State
= SavedState
;
3649 // Get this module function address from ModulePeMapFile and add them into FvMap file
3653 // Default use FileName as map file path
3655 if (PdbPointer
== NULL
) {
3656 PdbPointer
= FileName
;
3659 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3662 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3663 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3664 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3665 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3666 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3669 // Only Peim code may have a TE section
3675 // Now process TE sections
3677 for (Index
= 1;; Index
++) {
3678 NewPe32BaseAddress
= 0;
3683 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3684 if (EFI_ERROR (Status
)) {
3688 CurSecHdrSize
= GetSectionHeaderLength(CurrentPe32Section
.CommonHeader
);
3691 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3694 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ CurSecHdrSize
);
3697 // Initialize context, load image info.
3699 memset (&ImageContext
, 0, sizeof (ImageContext
));
3700 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3701 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3702 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3703 if (EFI_ERROR (Status
)) {
3704 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3708 if ( (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) ||
3709 (ImageContext
.Machine
== EFI_IMAGE_MACHINE_AARCH64
) ) {
3714 // Keep Image Context for TE image in FV
3716 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3719 // Get File PdbPointer
3721 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3724 // Set new rebased address.
3726 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3727 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3730 // if reloc is stripped, try to get the original efi image to get reloc info.
3732 if (ImageContext
.RelocationsStripped
) {
3734 // Construct the original efi file name
3736 strcpy (PeFileName
, FileName
);
3737 Cptr
= PeFileName
+ strlen (PeFileName
);
3738 while (*Cptr
!= '.') {
3743 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3752 PeFile
= fopen (LongFilePath (PeFileName
), "rb");
3753 if (PeFile
== NULL
) {
3754 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3755 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3756 //return EFI_ABORTED;
3759 // Get the file size
3761 PeFileSize
= _filelength (fileno (PeFile
));
3762 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3763 if (PeFileBuffer
== NULL
) {
3764 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3765 return EFI_OUT_OF_RESOURCES
;
3770 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3776 // Append reloc section into TeImage
3778 ImageContext
.Handle
= PeFileBuffer
;
3779 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3780 if (EFI_ERROR (Status
)) {
3781 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3784 ImageContext
.RelocationsStripped
= FALSE
;
3788 // Relocation doesn't exist
3790 if (ImageContext
.RelocationsStripped
) {
3791 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3796 // Relocation exist and rebase
3799 // Load and Relocate Image Data
3801 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3802 if (MemoryImagePointer
== NULL
) {
3803 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3804 return EFI_OUT_OF_RESOURCES
;
3806 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3807 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3809 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3810 if (EFI_ERROR (Status
)) {
3811 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3812 free ((VOID
*) MemoryImagePointer
);
3816 // Reloacate TeImage
3818 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3819 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3820 if (EFI_ERROR (Status
)) {
3821 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3822 free ((VOID
*) MemoryImagePointer
);
3827 // Copy the relocated image into raw image file.
3829 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3830 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3831 if (!ImageContext
.IsTeImage
) {
3833 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3834 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3835 SectionHeader
->SizeOfRawData
3839 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3840 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3841 SectionHeader
->SizeOfRawData
3847 // Free the allocated memory resource
3849 free ((VOID
*) MemoryImagePointer
);
3850 MemoryImagePointer
= NULL
;
3851 if (PeFileBuffer
!= NULL
) {
3852 free (PeFileBuffer
);
3853 PeFileBuffer
= NULL
;
3857 // Update Image Base Address
3859 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3862 // Now update file checksum
3864 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3865 SavedState
= FfsFile
->State
;
3866 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3868 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3869 (UINT8
*)((UINT8
*)FfsFile
+ FfsHeaderSize
),
3870 GetFfsFileLength (FfsFile
) - FfsHeaderSize
3872 FfsFile
->State
= SavedState
;
3875 // Get this module function address from ModulePeMapFile and add them into FvMap file
3879 // Default use FileName as map file path
3881 if (PdbPointer
== NULL
) {
3882 PdbPointer
= FileName
;
3898 FindApResetVectorPosition (
3899 IN MEMORY_FILE
*FvImage
,
3904 Routine Description:
3906 Find the position in this FvImage to place Ap reset vector.
3910 FvImage Memory file for the FV memory image.
3911 Pointer Pointer to pointer to position.
3915 EFI_NOT_FOUND - No satisfied position is found.
3916 EFI_SUCCESS - The suitable position is return.
3920 EFI_FFS_FILE_HEADER
*PadFile
;
3926 for (Index
= 1; ;Index
++) {
3928 // Find Pad File to add ApResetVector info
3930 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3931 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3933 // No Pad file to be found.
3938 // Get Pad file size.
3940 FileLength
= GetFfsFileLength(PadFile
);
3941 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3943 // FixPoint must be align on 0x1000 relative to FvImage Header
3945 FixPoint
= (UINT8
*) PadFile
+ GetFfsHeaderLength(PadFile
);
3946 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3948 // FixPoint be larger at the last place of one fv image.
3950 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3955 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ GetFfsHeaderLength(PadFile
))) {
3957 // No alignment FixPoint in this Pad File.
3962 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3964 // Find the position to place ApResetVector
3966 *Pointer
= FixPoint
;
3971 return EFI_NOT_FOUND
;
3976 IN MEMORY_FILE
*InfFile
,
3977 OUT CAP_INFO
*CapInfo
3981 Routine Description:
3983 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3987 InfFile Memory file image.
3988 CapInfo Information read from INF file.
3992 EFI_SUCCESS INF file information successfully retrieved.
3993 EFI_ABORTED INF file has an invalid format.
3994 EFI_NOT_FOUND A required string was not found in the INF file.
3997 CHAR8 Value
[MAX_LONG_FILE_PATH
];
3999 UINTN Index
, Number
;
4003 // Initialize Cap info
4005 // memset (CapInfo, 0, sizeof (CAP_INFO));
4009 // Read the Capsule Guid
4011 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
4012 if (Status
== EFI_SUCCESS
) {
4014 // Get the Capsule Guid
4016 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
4017 if (EFI_ERROR (Status
)) {
4018 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4021 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
4025 // Read the Capsule Header Size
4027 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
4028 if (Status
== EFI_SUCCESS
) {
4029 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4030 if (EFI_ERROR (Status
)) {
4031 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4034 CapInfo
->HeaderSize
= (UINT32
) Value64
;
4035 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
4039 // Read the Capsule Flag
4041 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
4042 if (Status
== EFI_SUCCESS
) {
4043 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
4044 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
4045 if (strstr (Value
, "InitiateReset") != NULL
) {
4046 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4048 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
4049 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
4050 if (strstr (Value
, "InitiateReset") != NULL
) {
4051 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
4054 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
4057 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
4060 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_OEM_CAPSULE_FLAGS_STRING
, 0, Value
);
4061 if (Status
== EFI_SUCCESS
) {
4062 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
4063 if (EFI_ERROR (Status
) || Value64
> 0xffff) {
4064 Error (NULL
, 0, 2000, "Invalid parameter",
4065 "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
4066 EFI_OEM_CAPSULE_FLAGS_STRING
);
4069 CapInfo
->Flags
|= Value64
;
4070 DebugMsg (NULL
, 0, 9, "Capsule Extend Flag", Value
);
4074 // Read Capsule File name
4076 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
4077 if (Status
== EFI_SUCCESS
) {
4079 // Get output file name
4081 strcpy (CapInfo
->CapName
, Value
);
4085 // Read the Capsule FileImage
4088 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
4089 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
4093 // Read the capsule file name
4095 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
4097 if (Status
== EFI_SUCCESS
) {
4101 strcpy (CapInfo
->CapFiles
[Index
], Value
);
4102 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
4109 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
4117 IN CHAR8
*InfFileImage
,
4118 IN UINTN InfFileSize
,
4119 IN CHAR8
*CapFileName
4123 Routine Description:
4125 This is the main function which will be called from application to create UEFI Capsule image.
4129 InfFileImage Buffer containing the INF file contents.
4130 InfFileSize Size of the contents of the InfFileImage buffer.
4131 CapFileName Requested name for the Cap file.
4135 EFI_SUCCESS Function completed successfully.
4136 EFI_OUT_OF_RESOURCES Could not allocate required resources.
4137 EFI_ABORTED Error encountered.
4138 EFI_INVALID_PARAMETER A required parameter was NULL.
4144 EFI_CAPSULE_HEADER
*CapsuleHeader
;
4145 MEMORY_FILE InfMemoryFile
;
4151 if (InfFileImage
!= NULL
) {
4153 // Initialize file structures
4155 InfMemoryFile
.FileImage
= InfFileImage
;
4156 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
4157 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
4160 // Parse the Cap inf file for header information
4162 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
4163 if (Status
!= EFI_SUCCESS
) {
4168 if (mCapDataInfo
.HeaderSize
== 0) {
4170 // make header size align 16 bytes.
4172 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
4173 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
4176 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
4177 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
4178 return EFI_INVALID_PARAMETER
;
4181 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
4182 CapFileName
= mCapDataInfo
.CapName
;
4185 if (CapFileName
== NULL
) {
4186 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
4187 return EFI_INVALID_PARAMETER
;
4191 // Set Default Capsule Guid value
4193 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
4194 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
4197 // Calculate the size of capsule image.
4201 CapSize
= mCapDataInfo
.HeaderSize
;
4202 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4203 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4205 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4208 FileSize
= _filelength (fileno (fpin
));
4209 CapSize
+= FileSize
;
4215 // Allocate buffer for capsule image.
4217 CapBuffer
= (UINT8
*) malloc (CapSize
);
4218 if (CapBuffer
== NULL
) {
4219 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
4220 return EFI_OUT_OF_RESOURCES
;
4224 // Initialize the capsule header to zero
4226 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
4229 // create capsule header and get capsule body
4231 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
4232 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
4233 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
4234 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
4235 CapsuleHeader
->CapsuleImageSize
= CapSize
;
4239 CapSize
= CapsuleHeader
->HeaderSize
;
4240 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
4241 fpin
= fopen (LongFilePath (mCapDataInfo
.CapFiles
[Index
]), "rb");
4243 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
4247 FileSize
= _filelength (fileno (fpin
));
4248 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
4251 CapSize
+= FileSize
;
4255 // write capsule data into the output file
4257 fpout
= fopen (LongFilePath (CapFileName
), "wb");
4258 if (fpout
== NULL
) {
4259 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
4264 fwrite (CapBuffer
, 1, CapSize
, fpout
);
4268 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);