3 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This file contains the internal functions required to generate a Firmware Volume.
26 #include <uuid/uuid.h>
35 #include "GenFvInternalLib.h"
37 #include "PeCoffLib.h"
38 #include "WinNtInclude.h"
41 STATIC UINT32 MaxFfsAlignment
= 0;
43 EFI_GUID mEfiFirmwareVolumeTopFileGuid
= EFI_FFS_VOLUME_TOP_FILE_GUID
;
44 EFI_GUID mFileGuidArray
[MAX_NUMBER_OF_FILES_IN_FV
];
45 EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
46 EFI_GUID mDefaultCapsuleGuid
= {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
48 CHAR8
*mFvbAttributeName
[] = {
49 EFI_FVB2_READ_DISABLED_CAP_STRING
,
50 EFI_FVB2_READ_ENABLED_CAP_STRING
,
51 EFI_FVB2_READ_STATUS_STRING
,
52 EFI_FVB2_WRITE_DISABLED_CAP_STRING
,
53 EFI_FVB2_WRITE_ENABLED_CAP_STRING
,
54 EFI_FVB2_WRITE_STATUS_STRING
,
55 EFI_FVB2_LOCK_CAP_STRING
,
56 EFI_FVB2_LOCK_STATUS_STRING
,
58 EFI_FVB2_STICKY_WRITE_STRING
,
59 EFI_FVB2_MEMORY_MAPPED_STRING
,
60 EFI_FVB2_ERASE_POLARITY_STRING
,
61 EFI_FVB2_READ_LOCK_CAP_STRING
,
62 EFI_FVB2_READ_LOCK_STATUS_STRING
,
63 EFI_FVB2_WRITE_LOCK_CAP_STRING
,
64 EFI_FVB2_WRITE_LOCK_STATUS_STRING
67 CHAR8
*mFvbAlignmentName
[] = {
68 EFI_FVB2_ALIGNMENT_1_STRING
,
69 EFI_FVB2_ALIGNMENT_2_STRING
,
70 EFI_FVB2_ALIGNMENT_4_STRING
,
71 EFI_FVB2_ALIGNMENT_8_STRING
,
72 EFI_FVB2_ALIGNMENT_16_STRING
,
73 EFI_FVB2_ALIGNMENT_32_STRING
,
74 EFI_FVB2_ALIGNMENT_64_STRING
,
75 EFI_FVB2_ALIGNMENT_128_STRING
,
76 EFI_FVB2_ALIGNMENT_256_STRING
,
77 EFI_FVB2_ALIGNMENT_512_STRING
,
78 EFI_FVB2_ALIGNMENT_1K_STRING
,
79 EFI_FVB2_ALIGNMENT_2K_STRING
,
80 EFI_FVB2_ALIGNMENT_4K_STRING
,
81 EFI_FVB2_ALIGNMENT_8K_STRING
,
82 EFI_FVB2_ALIGNMENT_16K_STRING
,
83 EFI_FVB2_ALIGNMENT_32K_STRING
,
84 EFI_FVB2_ALIGNMENT_64K_STRING
,
85 EFI_FVB2_ALIGNMENT_128K_STRING
,
86 EFI_FVB2_ALIGNMENT_256K_STRING
,
87 EFI_FVB2_ALIGNMNET_512K_STRING
,
88 EFI_FVB2_ALIGNMENT_1M_STRING
,
89 EFI_FVB2_ALIGNMENT_2M_STRING
,
90 EFI_FVB2_ALIGNMENT_4M_STRING
,
91 EFI_FVB2_ALIGNMENT_8M_STRING
,
92 EFI_FVB2_ALIGNMENT_16M_STRING
,
93 EFI_FVB2_ALIGNMENT_32M_STRING
,
94 EFI_FVB2_ALIGNMENT_64M_STRING
,
95 EFI_FVB2_ALIGNMENT_128M_STRING
,
96 EFI_FVB2_ALIGNMENT_256M_STRING
,
97 EFI_FVB2_ALIGNMENT_512M_STRING
,
98 EFI_FVB2_ALIGNMENT_1G_STRING
,
99 EFI_FVB2_ALIGNMENT_2G_STRING
103 // This data array will be located at the base of the Firmware Volume Header (FVH)
104 // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
105 // will be used to keep the FVH checksum consistent.
106 // This code will be run in response to a starutp IPI for HT-enabled systems.
108 #define SIZEOF_STARTUP_DATA_ARRAY 0x10
110 UINT8 m128kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
112 // EA D0 FF 00 F0 ; far jmp F000:FFD0
113 // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
114 // 0, 0 ; Checksum Padding
134 UINT8 m64kRecoveryStartupApDataArray
[SIZEOF_STARTUP_DATA_ARRAY
] = {
136 // EB CE ; jmp short ($-0x30)
137 // ; (from offset 0x0 to offset 0xFFD0)
138 // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
139 // 0, 0 ; Checksum Padding
160 CAP_INFO mCapDataInfo
;
162 EFI_PHYSICAL_ADDRESS mFvBaseAddress
[0x10];
163 UINT32 mFvBaseAddressNumber
= 0;
167 IN MEMORY_FILE
*InfFile
,
174 This function parses a FV.INF file and copies info into a FV_INFO structure.
178 InfFile Memory file image.
179 FvInfo Information read from INF file.
183 EFI_SUCCESS INF file information successfully retrieved.
184 EFI_ABORTED INF file has an invalid format.
185 EFI_NOT_FOUND A required string was not found in the INF file.
188 CHAR8 Value
[_MAX_PATH
];
196 // Read the FV base address
198 if (!mFvDataInfo
.BaseAddressSet
) {
199 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_BASE_ADDRESS_STRING
, 0, Value
);
200 if (Status
== EFI_SUCCESS
) {
202 // Get the base address
204 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
205 if (EFI_ERROR (Status
)) {
206 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
209 DebugMsg (NULL
, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING
, Value
);
211 FvInfo
->BaseAddress
= Value64
;
212 FvInfo
->BaseAddressSet
= TRUE
;
217 // Read the FV File System Guid
219 if (!FvInfo
->FvFileSystemGuidSet
) {
220 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
221 if (Status
== EFI_SUCCESS
) {
223 // Get the guid value
225 Status
= StringToGuid (Value
, &GuidValue
);
226 if (EFI_ERROR (Status
)) {
227 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
230 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
231 FvInfo
->FvFileSystemGuidSet
= TRUE
;
236 // Read the FV Extension Header File Name
238 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
239 if (Status
== EFI_SUCCESS
) {
240 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
244 // Read the FV file name
246 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
247 if (Status
== EFI_SUCCESS
) {
249 // copy the file name
251 strcpy (FvInfo
->FvName
, Value
);
257 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
258 if ((mFvbAttributeName
[Index
] != NULL
) && \
259 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
260 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
261 FvInfo
->FvAttributes
|= 1 << Index
;
262 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
263 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
272 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
273 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
274 if (strcmp (Value
, TRUE_STRING
) == 0) {
275 FvInfo
->FvAttributes
|= Index
<< 16;
276 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
285 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
286 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
290 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
292 if (Status
== EFI_SUCCESS
) {
294 // Update the size of block
296 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
297 if (EFI_ERROR (Status
)) {
298 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
302 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
303 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
306 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
307 // and should return an error.
309 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
310 if (!EFI_ERROR (Status
)) {
311 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
322 // Read blocks number
324 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
326 if (Status
== EFI_SUCCESS
) {
328 // Update the number of blocks
330 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
331 if (EFI_ERROR (Status
)) {
332 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
336 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
337 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
343 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
351 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
352 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
357 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
359 // Read the FFS file list
361 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
363 if (Status
== EFI_SUCCESS
) {
367 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
368 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
374 if ((Index
+ Number
) == 0) {
375 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
383 IN EFI_FFS_FILE_HEADER
*FfsFile
,
384 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
390 This function changes the FFS file attributes based on the erase polarity
391 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
404 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
405 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
406 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
412 IN EFI_FFS_FILE_HEADER
*FfsFile
,
413 IN OUT UINT32
*Alignment
419 This function determines the alignment of the FFS input file from the file
424 FfsFile FFS file to parse
425 Alignment The minimum required alignment offset of the FFS file
429 EFI_SUCCESS The function completed successfully.
430 EFI_INVALID_PARAMETER One of the input parameters was invalid.
431 EFI_ABORTED An error occurred.
436 // Verify input parameters.
438 if (FfsFile
== NULL
|| Alignment
== NULL
) {
439 return EFI_INVALID_PARAMETER
;
442 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
446 // 8 byte alignment, mini alignment requirement for FFS file.
460 // 128 byte alignment
467 // 512 byte alignment
488 // 32K byte alignment
495 // 64K byte alignment
509 IN OUT MEMORY_FILE
*FvImage
,
510 IN UINT32 DataAlignment
,
512 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
518 This function adds a pad file to the FV image if it required to align the
519 data of the next file.
523 FvImage The memory image of the FV to add it to.
524 The current offset must be valid.
525 DataAlignment The data alignment of the next FFS file.
526 FvEnd End of the empty data in FvImage.
527 ExtHeader PI FvExtHeader Optional
531 EFI_SUCCESS The function completed successfully.
532 EFI_INVALID_PARAMETER One of the input parameters was invalid.
533 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
538 EFI_FFS_FILE_HEADER
*PadFile
;
542 // Verify input parameters.
544 if (FvImage
== NULL
) {
545 return EFI_INVALID_PARAMETER
;
549 // Check if a pad file is necessary
551 if ((ExtHeader
== NULL
) && (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
)) % DataAlignment
== 0)) {
556 // Calculate the pad file size
559 // This is the earliest possible valid offset (current plus pad file header
560 // plus the next file header)
562 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ (sizeof (EFI_FFS_FILE_HEADER
) * 2);
565 // Add whatever it takes to get to the next aligned address
567 while ((PadFileSize
% DataAlignment
) != 0) {
571 // Subtract the next file header size
573 PadFileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
576 // Subtract the starting offset to get size
578 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
581 // Append extension header size
583 if (ExtHeader
!= NULL
) {
584 PadFileSize
= PadFileSize
+ ExtHeader
->ExtHeaderSize
;
588 // Verify that we have enough space for the file header
590 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
591 return EFI_OUT_OF_RESOURCES
;
595 // Write pad file header
597 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
600 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
602 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
603 PadFile
->Attributes
= 0;
606 // Write pad file size (calculated size minus next file header size)
608 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
609 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
610 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
613 // Fill in checksums and state, they must be 0 for checksumming.
615 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
616 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
618 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
619 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
621 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
623 (EFI_FFS_FILE_HEADER
*) PadFile
,
624 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
628 // Update the current FV pointer
630 FvImage
->CurrentFilePointer
+= PadFileSize
;
632 if (ExtHeader
!= NULL
) {
634 // Copy Fv Extension Header and Set Fv Extension header offset
636 memcpy (PadFile
+ 1, ExtHeader
, ExtHeader
->ExtHeaderSize
);
637 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) (PadFile
+ 1) - (UINTN
) FvImage
->FileImage
);
639 // Make next file start at QWord Boundry
641 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
642 FvImage
->CurrentFilePointer
++;
651 IN EFI_FFS_FILE_HEADER
*FileBuffer
657 This function checks the header to validate if it is a VTF file
661 FileBuffer Buffer in which content of a file has been read.
665 TRUE If this is a VTF file
666 FALSE If this is not a VTF file
670 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
679 IN OUT
FILE *FvMapFile
,
681 IN EFI_FFS_FILE_HEADER
*FfsFile
,
682 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
683 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
689 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
690 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
694 FvMapFile A pointer to FvMap File
695 FileName Ffs File PathName
696 FfsFile A pointer to Ffs file image.
697 ImageBaseAddress PeImage Base Address.
698 pImageContext Image Context Information.
702 EFI_SUCCESS Added required map information.
706 CHAR8 PeMapFileName
[_MAX_PATH
];
708 CHAR8 FileGuidName
[MAX_LINE_LEN
];
710 CHAR8 Line
[MAX_LINE_LEN
];
711 CHAR8 KeyWord
[MAX_LINE_LEN
];
712 CHAR8 FunctionName
[MAX_LINE_LEN
];
713 EFI_PHYSICAL_ADDRESS FunctionAddress
;
715 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
717 UINT32 AddressOfEntryPoint
;
719 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
720 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
721 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
722 unsigned long long TempLongAddress
;
723 UINT32 TextVirtualAddress
;
724 UINT32 DataVirtualAddress
;
725 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
728 // Init local variable
732 // Print FileGuid to string buffer.
734 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
737 // Construct Map file Name
739 strcpy (PeMapFileName
, FileName
);
742 // Change '\\' to '/', unified path format.
744 Cptr
= PeMapFileName
;
745 while (*Cptr
!= '\0') {
747 *Cptr
= FILE_SEP_CHAR
;
755 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
756 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
759 if (Cptr
< PeMapFileName
) {
760 return EFI_NOT_FOUND
;
772 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
776 strcpy (KeyWord
, Cptr
+ 1);
780 // AddressOfEntryPoint and Offset in Image
782 if (!pImageContext
->IsTeImage
) {
783 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
784 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
786 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
789 sizeof (EFI_IMAGE_FILE_HEADER
) +
790 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
792 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
794 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
795 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
796 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
797 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
798 Index
= TEImageHeader
->NumberOfSections
;
802 // module information output
804 if (ImageBaseAddress
== 0) {
805 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
806 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
808 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
809 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
812 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&& pImageContext
->Machine
== EFI_IMAGE_MACHINE_IA64
) {
814 // Process IPF PLABEL to get the real address after the image has been rebased.
815 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
817 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64
*)((UINTN
) pImageContext
->Handle
+ (UINTN
) AddressOfEntryPoint
)));
819 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
821 fprintf (FvMapFile
, ")\n");
823 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
824 TextVirtualAddress
= 0;
825 DataVirtualAddress
= 0;
826 for (; Index
> 0; Index
--, SectionHeader
++) {
827 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
828 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
829 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
830 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
831 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
832 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
835 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
836 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
837 fprintf (FvMapFile
, ")\n\n");
842 PeMapFile
= fopen (PeMapFileName
, "r");
843 if (PeMapFile
== NULL
) {
844 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
847 VerboseMsg ("The map file is %s", PeMapFileName
);
850 // Output Functions information into Fv Map file
852 LinkTimeBaseAddress
= 0;
853 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
857 if (Line
[0] == 0x0a) {
862 // By Address and Static keyword
864 if (FunctionType
== 0) {
865 sscanf (Line
, "%s", KeyWord
);
866 if (stricmp (KeyWord
, "Address") == 0) {
871 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
872 } else if (stricmp (KeyWord
, "Static") == 0) {
874 // static function list
877 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
878 } else if (stricmp (KeyWord
, "Preferred") ==0) {
879 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
880 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
885 // Printf Function Information
887 if (FunctionType
== 1) {
888 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
889 FunctionAddress
= (UINT64
) TempLongAddress
;
890 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
891 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
892 fprintf (FvMapFile
, "%s\n", FunctionName
);
894 } else if (FunctionType
== 2) {
895 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
896 FunctionAddress
= (UINT64
) TempLongAddress
;
897 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
898 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
899 fprintf (FvMapFile
, "%s\n", FunctionName
);
906 fprintf (FvMapFile
, "\n\n");
914 IN OUT MEMORY_FILE
*FvImage
,
917 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
919 IN
FILE *FvReportFile
925 This function adds a file to the FV image. The file will pad to the
926 appropriate alignment if required.
930 FvImage The memory image of the FV to add it to. The current offset
932 FvInfo Pointer to information about the FV.
933 Index The file in the FvInfo file list to add.
934 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
935 to the end of the FvImage then no VTF previously found.
936 FvMapFile Pointer to FvMap File
937 FvReportFile Pointer to FvReport File
941 EFI_SUCCESS The function completed successfully.
942 EFI_INVALID_PARAMETER One of the input parameters was invalid.
943 EFI_ABORTED An error occurred.
944 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
952 UINT32 CurrentFileAlignment
;
955 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
959 // Verify input parameters.
961 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
962 return EFI_INVALID_PARAMETER
;
966 // Read the file to add
968 NewFile
= fopen (FvInfo
->FvFiles
[Index
], "rb");
970 if (NewFile
== NULL
) {
971 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
978 FileSize
= _filelength (fileno (NewFile
));
981 // Read the file into a buffer
983 FileBuffer
= malloc (FileSize
);
984 if (FileBuffer
== NULL
) {
985 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
986 return EFI_OUT_OF_RESOURCES
;
989 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
992 // Done with the file, from this point on we will just use the buffer read.
997 // Verify read successful
999 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1001 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1006 // For None PI Ffs file, directly add them into FvImage.
1008 if (!FvInfo
->IsPiFvImage
) {
1009 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1010 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1011 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1013 FvImage
->CurrentFilePointer
+= FileSize
;
1021 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1022 if (EFI_ERROR (Status
)) {
1024 Error (NULL
, 0, 3000, "Invalid", "%s is a FFS file.", FvInfo
->FvFiles
[Index
]);
1025 return EFI_INVALID_PARAMETER
;
1029 // Verify space exists to add the file
1031 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1033 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1034 return EFI_OUT_OF_RESOURCES
;
1038 // Verify the input file is the duplicated file in this Fv image
1040 for (Index1
= 0; Index1
< Index
; Index1
++) {
1041 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1042 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1043 PrintGuid ((EFI_GUID
*) FileBuffer
);
1044 return EFI_INVALID_PARAMETER
;
1047 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1050 // Update the file state based on polarity of the FV.
1052 UpdateFfsFileState (
1053 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1054 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1058 // Check if alignment is required
1060 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1063 // Find the largest alignment of all the FFS files in the FV
1065 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1066 MaxFfsAlignment
= CurrentFileAlignment
;
1069 // If we have a VTF file, add it at the top.
1071 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1072 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1074 // No previous VTF, add this one.
1076 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1078 // Sanity check. The file MUST align appropriately
1080 if (((UINTN
) *VtfFileImage
+ sizeof (EFI_FFS_FILE_HEADER
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1081 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1086 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1087 // Rebase for the debug genfvmap tool
1089 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1090 if (EFI_ERROR (Status
)) {
1091 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1097 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1099 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1100 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1103 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1107 // Already found a VTF file.
1109 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1116 // Add pad file if necessary
1118 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
);
1119 if (EFI_ERROR (Status
)) {
1120 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1127 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1129 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1130 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1132 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1133 if (EFI_ERROR (Status
)) {
1134 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1140 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1141 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1142 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1143 FvImage
->CurrentFilePointer
+= FileSize
;
1145 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1150 // Make next file start at QWord Boundry
1152 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1153 FvImage
->CurrentFilePointer
++;
1158 // Free allocated memory.
1167 IN MEMORY_FILE
*FvImage
,
1168 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1172 Routine Description:
1174 This function places a pad file between the last file in the FV and the VTF
1175 file if the VTF file exists.
1179 FvImage Memory file for the FV memory image
1180 VtfFileImage The address of the VTF file. If this is the end of the FV
1181 image, no VTF exists and no pad file is needed.
1185 EFI_SUCCESS Completed successfully.
1186 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1190 EFI_FFS_FILE_HEADER
*PadFile
;
1194 // If there is no VTF or the VTF naturally follows the previous file without a
1195 // pad file, then there's nothing to do
1197 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1198 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1202 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1203 return EFI_INVALID_PARAMETER
;
1207 // Pad file starts at beginning of free space
1209 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1212 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1214 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1215 PadFile
->Attributes
= 0;
1218 // FileSize includes the EFI_FFS_FILE_HEADER
1220 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1221 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1222 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1223 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1226 // Fill in checksums and state, must be zero during checksum calculation.
1228 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1229 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1231 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
1232 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1234 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1236 UpdateFfsFileState (
1237 (EFI_FFS_FILE_HEADER
*) PadFile
,
1238 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1241 // Update the current FV pointer
1243 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1250 IN MEMORY_FILE
*FvImage
,
1252 IN EFI_FFS_FILE_HEADER
*VtfFile
1256 Routine Description:
1258 This parses the FV looking for the PEI core and then plugs the address into
1259 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1260 complete an IA32 Bootstrap FV.
1264 FvImage Memory file for the FV memory image
1265 FvInfo Information read from INF file.
1266 VtfFile Pointer to the VTF file in the FV image.
1270 EFI_SUCCESS Function Completed successfully.
1271 EFI_ABORTED Error encountered.
1272 EFI_INVALID_PARAMETER A required parameter was NULL.
1273 EFI_NOT_FOUND PEI Core file not found.
1277 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1278 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1280 EFI_FILE_SECTION_POINTER Pe32Section
;
1284 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1285 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1286 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
1287 INT32 Ia32SecEntryOffset
;
1288 UINT32
*Ia32ResetAddressPtr
;
1290 UINT8
*BytePointer2
;
1291 UINT16
*WordPointer
;
1295 EFI_FFS_FILE_STATE SavedState
;
1297 FIT_TABLE
*FitTablePtr
;
1298 BOOLEAN Vtf0Detected
;
1301 // Verify input parameters
1303 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1304 return EFI_INVALID_PARAMETER
;
1307 // Initialize FV library
1309 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1314 Status
= VerifyFfsFile (VtfFile
);
1315 if (EFI_ERROR (Status
)) {
1316 return EFI_INVALID_PARAMETER
;
1320 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1321 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1322 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1323 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1324 IA32_X64_VTF0_SIGNATURE
)
1326 Vtf0Detected
= TRUE
;
1328 Vtf0Detected
= FALSE
;
1332 // Find the Sec Core
1334 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1335 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1338 // If the SEC core file is not found, but the VTF-0 signature
1339 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1340 // This means no modifications are required to the VTF.
1345 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1349 // Sec Core found, now find PE32 section
1351 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1352 if (Status
== EFI_NOT_FOUND
) {
1353 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1356 if (EFI_ERROR (Status
)) {
1357 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1361 Status
= GetPe32Info (
1362 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1368 if (EFI_ERROR (Status
)) {
1369 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1375 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1376 MachineType
== EFI_IMAGE_MACHINE_X64
)
1379 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1380 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1381 // This means no modifications are required to the VTF.
1387 // Physical address is FV base + offset of PE32 + offset of the entry point
1389 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1390 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1391 SecCorePhysicalAddress
+= EntryPoint
;
1392 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1395 // Find the PEI Core
1397 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1398 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
1399 Error (NULL
, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1403 // PEI Core found, now find PE32 or TE section
1405 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1406 if (Status
== EFI_NOT_FOUND
) {
1407 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1410 if (EFI_ERROR (Status
)) {
1411 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1415 Status
= GetPe32Info (
1416 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1422 if (EFI_ERROR (Status
)) {
1423 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1427 // Physical address is FV base + offset of PE32 + offset of the entry point
1429 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1430 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1431 PeiCorePhysicalAddress
+= EntryPoint
;
1432 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1434 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
1436 // Update PEI_CORE address
1439 // Set the uncached attribute bit in the physical address
1441 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
1444 // Check if address is aligned on a 16 byte boundary
1446 if (PeiCorePhysicalAddress
& 0xF) {
1447 Error (NULL
, 0, 3000, "Invalid",
1448 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1449 (unsigned long long) PeiCorePhysicalAddress
1454 // First Get the FIT table address
1456 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
1458 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
1460 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
1462 if (!EFI_ERROR (Status
)) {
1463 UpdateFitCheckSum (FitTablePtr
);
1467 // Update SEC_CORE address
1470 // Set the uncached attribute bit in the physical address
1472 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
1474 // Check if address is aligned on a 16 byte boundary
1476 if (SecCorePhysicalAddress
& 0xF) {
1477 Error (NULL
, 0, 3000, "Invalid",
1478 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1479 (unsigned long long) SecCorePhysicalAddress
1484 // Update the address
1486 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
1487 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
1489 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1491 // Get the location to update
1493 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1496 // Write lower 32 bits of physical address for Pei Core entry
1498 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1501 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1503 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1505 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1506 if (Ia32SecEntryOffset
<= -65536) {
1507 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1508 return STATUS_ERROR
;
1511 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1514 // Update the BFV base address
1516 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1517 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1518 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1521 // Update the Startup AP in the FVH header block ZeroVector region.
1523 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1524 if (FvInfo
->Size
<= 0x10000) {
1525 BytePointer2
= m64kRecoveryStartupApDataArray
;
1526 } else if (FvInfo
->Size
<= 0x20000) {
1527 BytePointer2
= m128kRecoveryStartupApDataArray
;
1529 BytePointer2
= m128kRecoveryStartupApDataArray
;
1531 // Find the position to place Ap reset vector, the offset
1532 // between the position and the end of Fvrecovery.fv file
1533 // should not exceed 128kB to prevent Ap reset vector from
1534 // outside legacy E and F segment
1536 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1537 if (EFI_ERROR (Status
)) {
1538 Error (NULL
, 0, 3000, "Invalid", "Cannot find the appropriate location in FvImage to add Ap reset vector!");
1543 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1544 BytePointer
[Index
] = BytePointer2
[Index
];
1547 // Calculate the checksum
1550 WordPointer
= (UINT16
*) (BytePointer
);
1551 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1552 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1556 // Update the checksum field
1558 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1559 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1562 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1564 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1565 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1566 if ((IpiVector
& 0xFFF) != 0) {
1567 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1570 IpiVector
= IpiVector
>> 12;
1571 IpiVector
= IpiVector
& 0xFF;
1574 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1576 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1577 *Ia32ResetAddressPtr
= IpiVector
;
1578 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1580 // Since the ARM reset vector is in the FV Header you really don't need a
1581 // Volume Top File, but if you have one for some reason don't crash...
1584 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1589 // Now update file checksum
1591 SavedState
= VtfFile
->State
;
1592 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1594 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1595 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1596 (UINT8
*) (VtfFile
+ 1),
1597 GetLength (VtfFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
1600 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1603 VtfFile
->State
= SavedState
;
1610 UpdateArmResetVectorIfNeeded (
1611 IN MEMORY_FILE
*FvImage
,
1616 Routine Description:
1617 This parses the FV looking for SEC and patches that address into the
1618 beginning of the FV header.
1620 For ARM the reset vector is at 0x00000000 or 0xFFFF0000.
1621 This would commonly map to the first entry in the ROM.
1631 We support two schemes on ARM.
1632 1) Beginning of the FV is the reset vector
1633 2) Reset vector is data bytes FDF file and that code branches to reset vector
1634 in the beginning of the FV (fixed size offset).
1637 Need to have the jump for the reset vector at location zero.
1638 We also need to store the address or PEI (if it exists).
1639 We stub out a return from interrupt in case the debugger
1641 The optional entry to the common exception handler is
1642 to support full featured exception handling from ROM and is currently
1643 not support by this tool.
1646 FvImage Memory file for the FV memory image
1647 FvInfo Information read from INF file.
1651 EFI_SUCCESS Function Completed successfully.
1652 EFI_ABORTED Error encountered.
1653 EFI_INVALID_PARAMETER A required parameter was NULL.
1654 EFI_NOT_FOUND PEI Core file not found.
1658 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1659 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1661 EFI_FILE_SECTION_POINTER Pe32Section
;
1665 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1666 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1667 INT32 ResetVector
[4]; // 0 - is branch relative to SEC entry point
1668 // 1 - PEI Entry Point
1669 // 2 - movs pc,lr for a SWI handler
1670 // 3 - Place holder for Common Exception Handler
1673 // Verify input parameters
1675 if (FvImage
== NULL
|| FvInfo
== NULL
) {
1676 return EFI_INVALID_PARAMETER
;
1679 // Initialize FV library
1681 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1684 // Find the Sec Core
1686 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1687 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1689 // Maybe hardware does SEC job and we only have PEI Core?
1693 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1695 PeiCorePhysicalAddress
= 0;
1696 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1697 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1699 // PEI Core found, now find PE32 or TE section
1701 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1702 if (Status
== EFI_NOT_FOUND
) {
1703 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1706 if (EFI_ERROR (Status
)) {
1707 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1711 Status
= GetPe32Info (
1712 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1718 if (EFI_ERROR (Status
)) {
1719 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1723 // Physical address is FV base + offset of PE32 + offset of the entry point
1725 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1726 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1727 PeiCorePhysicalAddress
+= EntryPoint
;
1728 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1730 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1731 memset (ResetVector
, 0, sizeof (ResetVector
));
1732 // Address of PEI Core, if we have one
1733 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1737 // Copy to the beginning of the FV
1739 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1747 // Sec Core found, now find PE32 section
1749 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1750 if (Status
== EFI_NOT_FOUND
) {
1751 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1754 if (EFI_ERROR (Status
)) {
1755 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1759 Status
= GetPe32Info (
1760 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1765 if (EFI_ERROR (Status
)) {
1766 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1770 if (MachineType
!= EFI_IMAGE_MACHINE_ARMT
) {
1772 // If SEC is not ARM we have nothing to do
1778 // Physical address is FV base + offset of PE32 + offset of the entry point
1780 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1781 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1782 SecCorePhysicalAddress
+= EntryPoint
;
1783 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1786 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1788 PeiCorePhysicalAddress
= 0;
1789 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1790 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1792 // PEI Core found, now find PE32 or TE section
1794 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1795 if (Status
== EFI_NOT_FOUND
) {
1796 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1799 if (EFI_ERROR (Status
)) {
1800 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1804 Status
= GetPe32Info (
1805 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1811 if (EFI_ERROR (Status
)) {
1812 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1816 // Physical address is FV base + offset of PE32 + offset of the entry point
1818 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1819 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1820 PeiCorePhysicalAddress
+= EntryPoint
;
1821 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1825 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
1826 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
1827 ResetVector
[0] = (INT32
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
- 8) >> 2;
1829 if (ResetVector
[0] > 0x00FFFFFF) {
1830 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
1834 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
1835 ResetVector
[0] |= 0xEB000000;
1838 // Address of PEI Core, if we have one
1839 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1841 // SWI handler movs pc,lr. Just in case a debugger uses SWI
1842 ResetVector
[2] = 0xE1B0F07E;
1844 // Place holder to support a common interrupt handler from ROM.
1845 // Currently not suppprted. For this to be used the reset vector would not be in this FV
1846 // and the exception vectors would be hard coded in the ROM and just through this address
1847 // to find a common handler in the a module in the FV.
1851 // Copy to the beginning of the FV
1853 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1855 DebugMsg (NULL
, 0, 9, "Update Reset vector in FV Header", NULL
);
1863 OUT UINT32
*EntryPoint
,
1864 OUT UINT32
*BaseOfCode
,
1865 OUT UINT16
*MachineType
1869 Routine Description:
1871 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
1872 See EfiImage.h for machine types. The entry point offset is from the beginning
1873 of the PE32 buffer passed in.
1877 Pe32 Beginning of the PE32.
1878 EntryPoint Offset from the beginning of the PE32 to the image entry point.
1879 BaseOfCode Base address of code.
1880 MachineType Magic number for the machine type.
1884 EFI_SUCCESS Function completed successfully.
1885 EFI_ABORTED Error encountered.
1886 EFI_INVALID_PARAMETER A required parameter was NULL.
1887 EFI_UNSUPPORTED The operation is unsupported.
1891 EFI_IMAGE_DOS_HEADER
*DosHeader
;
1892 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
1893 EFI_TE_IMAGE_HEADER
*TeHeader
;
1896 // Verify input parameters
1899 return EFI_INVALID_PARAMETER
;
1903 // First check whether it is one TE Image.
1905 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
1906 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
1908 // By TeImage Header to get output
1910 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1911 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1912 *MachineType
= TeHeader
->Machine
;
1916 // Then check whether
1917 // First is the DOS header
1919 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
1922 // Verify DOS header is expected
1924 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1925 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
1926 return EFI_UNSUPPORTED
;
1929 // Immediately following is the NT header.
1931 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
1934 // Verify NT header is expected
1936 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1937 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
1938 return EFI_UNSUPPORTED
;
1943 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
1944 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
1945 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
1949 // Verify machine type is supported
1951 if (*MachineType
!= EFI_IMAGE_MACHINE_IA32
&& *MachineType
!= EFI_IMAGE_MACHINE_IA64
&& *MachineType
!= EFI_IMAGE_MACHINE_X64
&& *MachineType
!= EFI_IMAGE_MACHINE_EBC
&&
1952 *MachineType
!= EFI_IMAGE_MACHINE_ARMT
) {
1953 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
1954 return EFI_UNSUPPORTED
;
1962 IN CHAR8
*InfFileImage
,
1963 IN UINTN InfFileSize
,
1964 IN CHAR8
*FvFileName
,
1965 IN CHAR8
*MapFileName
1969 Routine Description:
1971 This is the main function which will be called from application.
1975 InfFileImage Buffer containing the INF file contents.
1976 InfFileSize Size of the contents of the InfFileImage buffer.
1977 FvFileName Requested name for the FV file.
1978 MapFileName Fv map file to log fv driver information.
1982 EFI_SUCCESS Function completed successfully.
1983 EFI_OUT_OF_RESOURCES Could not allocate required resources.
1984 EFI_ABORTED Error encountered.
1985 EFI_INVALID_PARAMETER A required parameter was NULL.
1990 MEMORY_FILE InfMemoryFile
;
1991 MEMORY_FILE FvImageMemoryFile
;
1993 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1994 EFI_FFS_FILE_HEADER
*VtfFileImage
;
1995 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
1999 CHAR8 FvMapName
[_MAX_PATH
];
2001 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2002 FILE *FvExtHeaderFile
;
2004 CHAR8 FvReportName
[_MAX_PATH
];
2007 FvBufferHeader
= NULL
;
2010 FvReportFile
= NULL
;
2012 if (InfFileImage
!= NULL
) {
2014 // Initialize file structures
2016 InfMemoryFile
.FileImage
= InfFileImage
;
2017 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2018 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2021 // Parse the FV inf file for header information
2023 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2024 if (EFI_ERROR (Status
)) {
2025 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2031 // Update the file name return values
2033 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2034 FvFileName
= mFvDataInfo
.FvName
;
2037 if (FvFileName
== NULL
) {
2038 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2042 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2043 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2048 // Debug message Fv File System Guid
2050 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2051 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2052 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2053 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2054 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2055 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2056 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2057 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2058 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2059 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2060 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2061 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2062 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2066 // Add PI FV extension header
2069 FvExtHeaderFile
= NULL
;
2070 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2072 // Open the FV Extension Header file
2074 FvExtHeaderFile
= fopen (mFvDataInfo
.FvExtHeaderFile
, "rb");
2077 // Get the file size
2079 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2082 // Allocate a buffer for the FV Extension Header
2084 FvExtHeader
= malloc(FileSize
);
2085 if (FvExtHeader
== NULL
) {
2086 fclose (FvExtHeaderFile
);
2087 return EFI_OUT_OF_RESOURCES
;
2091 // Read the FV Extension Header
2093 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2094 fclose (FvExtHeaderFile
);
2097 // See if there is an override for the FV Name GUID
2099 if (mFvDataInfo
.FvNameGuidSet
) {
2100 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2102 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2103 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2104 } else if (mFvDataInfo
.FvNameGuidSet
) {
2106 // Allocate a buffer for the FV Extension Header
2108 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2109 if (FvExtHeader
== NULL
) {
2110 return EFI_OUT_OF_RESOURCES
;
2112 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2113 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2117 // Debug message Fv Name Guid
2119 if (mFvDataInfo
.FvNameGuidSet
) {
2120 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2121 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2122 mFvDataInfo
.FvNameGuid
.Data2
,
2123 mFvDataInfo
.FvNameGuid
.Data3
,
2124 mFvDataInfo
.FvNameGuid
.Data4
[0],
2125 mFvDataInfo
.FvNameGuid
.Data4
[1],
2126 mFvDataInfo
.FvNameGuid
.Data4
[2],
2127 mFvDataInfo
.FvNameGuid
.Data4
[3],
2128 mFvDataInfo
.FvNameGuid
.Data4
[4],
2129 mFvDataInfo
.FvNameGuid
.Data4
[5],
2130 mFvDataInfo
.FvNameGuid
.Data4
[6],
2131 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2134 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2135 mFvDataInfo
.IsPiFvImage
= TRUE
;
2139 // FvMap file to log the function address of all modules in one Fvimage
2141 if (MapFileName
!= NULL
) {
2142 strcpy (FvMapName
, MapFileName
);
2144 strcpy (FvMapName
, FvFileName
);
2145 strcat (FvMapName
, ".map");
2147 VerboseMsg ("FV Map file name is %s", FvMapName
);
2150 // FvReport file to log the FV information in one Fvimage
2152 strcpy (FvReportName
, FvFileName
);
2153 strcat (FvReportName
, ".txt");
2156 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2157 // And Update mFvDataInfo data.
2159 Status
= CalculateFvSize (&mFvDataInfo
);
2160 if (EFI_ERROR (Status
)) {
2163 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2166 // support fv image and empty fv image
2168 FvImageSize
= mFvDataInfo
.Size
;
2171 // Allocate the FV, assure FvImage Header 8 byte alignment
2173 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2174 if (FvBufferHeader
== NULL
) {
2175 return EFI_OUT_OF_RESOURCES
;
2177 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2180 // Initialize the FV to the erase polarity
2182 if (mFvDataInfo
.FvAttributes
== 0) {
2184 // Set Default Fv Attribute
2186 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2188 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2189 memset (FvImage
, -1, FvImageSize
);
2191 memset (FvImage
, 0, FvImageSize
);
2195 // Initialize FV header
2197 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2200 // Initialize the zero vector to all zeros.
2202 memset (FvHeader
->ZeroVector
, 0, 16);
2205 // Copy the Fv file system GUID
2207 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2209 FvHeader
->FvLength
= FvImageSize
;
2210 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2211 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2212 FvHeader
->Revision
= EFI_FVH_REVISION
;
2213 FvHeader
->ExtHeaderOffset
= 0;
2214 FvHeader
->Reserved
[0] = 0;
2217 // Copy firmware block map
2219 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2220 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2221 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2225 // Add block map terminator
2227 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2228 FvHeader
->BlockMap
[Index
].Length
= 0;
2231 // Complete the header
2233 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2234 FvHeader
->Checksum
= 0;
2235 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2238 // If there is no FFS file, generate one empty FV
2240 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2245 // Initialize our "file" view of the buffer
2247 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2248 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2249 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2252 // Initialize the FV library.
2254 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2257 // Initialize the VTF file address.
2259 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2264 FvMapFile
= fopen (FvMapName
, "w");
2265 if (FvMapFile
== NULL
) {
2266 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2271 // Open FvReport file
2273 FvReportFile
= fopen(FvReportName
, "w");
2274 if (FvReportFile
== NULL
) {
2275 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2279 // record FV size information into FvMap file.
2281 if (mFvTotalSize
!= 0) {
2282 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2283 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2285 if (mFvTakenSize
!= 0) {
2286 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2287 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2289 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2290 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2291 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2295 // record FV size information to FvReportFile.
2297 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2298 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2301 // Add PI FV extension header
2303 if (FvExtHeader
!= NULL
) {
2305 // Add FV Extended Header contents to the FV as a PAD file
2307 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
);
2310 // Fv Extension header change update Fv Header Check sum
2312 FvHeader
->Checksum
= 0;
2313 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2319 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2323 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2326 // Exit if error detected while adding the file
2328 if (EFI_ERROR (Status
)) {
2334 // If there is a VTF file, some special actions need to occur.
2336 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2338 // Pad from the end of the last file to the beginning of the VTF file.
2339 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2341 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2342 if (EFI_ERROR (Status
)) {
2343 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2348 // Update reset vector (SALE_ENTRY for IPF)
2349 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2350 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2351 // reset vector. If the PEI Core is found, the VTF file will probably get
2352 // corrupted by updating the entry point.
2354 if ((mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2355 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2356 if (EFI_ERROR(Status
)) {
2357 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2360 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2366 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2367 if (EFI_ERROR (Status
)) {
2368 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2373 // Update Checksum for FvHeader
2375 FvHeader
->Checksum
= 0;
2376 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2380 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2382 if ((((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2383 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2385 // Update Checksum for FvHeader
2387 FvHeader
->Checksum
= 0;
2388 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2395 FvFile
= fopen (FvFileName
, "wb");
2396 if (FvFile
== NULL
) {
2397 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2398 Status
= EFI_ABORTED
;
2402 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2403 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2404 Status
= EFI_ABORTED
;
2409 if (FvBufferHeader
!= NULL
) {
2410 free (FvBufferHeader
);
2413 if (FvExtHeader
!= NULL
) {
2417 if (FvFile
!= NULL
) {
2422 if (FvMapFile
!= NULL
) {
2427 if (FvReportFile
!= NULL
) {
2428 fflush (FvReportFile
);
2429 fclose (FvReportFile
);
2435 UpdatePeiCoreEntryInFit (
2436 IN FIT_TABLE
*FitTablePtr
,
2437 IN UINT64 PeiCorePhysicalAddress
2441 Routine Description:
2443 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2448 FitTablePtr - The pointer of FIT_TABLE.
2449 PeiCorePhysicalAddress - The address of Pei Core entry.
2453 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2454 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2458 FIT_TABLE
*TmpFitPtr
;
2460 UINTN NumFitComponents
;
2462 TmpFitPtr
= FitTablePtr
;
2463 NumFitComponents
= TmpFitPtr
->CompSize
;
2465 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2466 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2467 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2474 return EFI_NOT_FOUND
;
2479 IN FIT_TABLE
*FitTablePtr
2483 Routine Description:
2485 This function is used to update the checksum for FIT.
2490 FitTablePtr - The pointer of FIT_TABLE.
2498 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2499 FitTablePtr
->CheckSum
= 0;
2500 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2509 Routine Description:
2510 Calculate the FV size and Update Fv Size based on the actual FFS files.
2511 And Update FvInfo data.
2514 FvInfoPtr - The pointer to FV_INFO structure.
2517 EFI_ABORTED - Ffs Image Error
2518 EFI_SUCCESS - Successfully update FvSize
2521 UINTN CurrentOffset
;
2525 UINTN FvExtendHeaderSize
;
2526 UINT32 FfsAlignment
;
2527 EFI_FFS_FILE_HEADER FfsHeader
;
2528 BOOLEAN VtfFileFlag
;
2531 FvExtendHeaderSize
= 0;
2533 VtfFileFlag
= FALSE
;
2538 // Compute size for easy access later
2540 FvInfoPtr
->Size
= 0;
2541 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
2542 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
2546 // Caculate the required sizes for all FFS files.
2548 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
2550 for (Index
= 1;; Index
++) {
2551 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
2552 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
2558 // Calculate PI extension header
2560 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
2561 fpin
= fopen (mFvDataInfo
.FvExtHeaderFile
, "rb");
2563 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2566 FvExtendHeaderSize
= _filelength (fileno (fpin
));
2568 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
2569 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2570 } else if (mFvDataInfo
.FvNameGuidSet
) {
2571 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2572 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2576 // Accumlate every FFS file size.
2578 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
2583 fpin
= fopen (FvInfoPtr
->FvFiles
[Index
], "rb");
2585 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
2589 // Get the file size
2591 FfsFileSize
= _filelength (fileno (fpin
));
2593 // Read Ffs File header
2595 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
2601 if (FvInfoPtr
->IsPiFvImage
) {
2603 // Check whether this ffs file is vtf file
2605 if (IsVtfFile (&FfsHeader
)) {
2608 // One Fv image can't have two vtf files.
2613 VtfFileSize
= FfsFileSize
;
2618 // Get the alignment of FFS file
2620 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
2621 FfsAlignment
= 1 << FfsAlignment
;
2625 if (((CurrentOffset
+ sizeof (EFI_FFS_FILE_HEADER
)) % FfsAlignment
) != 0) {
2626 CurrentOffset
= (CurrentOffset
+ sizeof (EFI_FFS_FILE_HEADER
) * 2 + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
2627 CurrentOffset
-= sizeof (EFI_FFS_FILE_HEADER
);
2632 // Add ffs file size
2634 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
2635 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
2637 CurrentOffset
+= FfsFileSize
;
2641 // Make next ffs file start at QWord Boundry
2643 if (FvInfoPtr
->IsPiFvImage
) {
2644 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
2647 CurrentOffset
+= VtfFileSize
;
2648 DebugMsg (NULL
, 0, 9, "FvImage size", "The caculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset
, (unsigned) FvInfoPtr
->Size
);
2650 if (FvInfoPtr
->Size
== 0) {
2652 // Update FvInfo data
2654 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
2655 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
2656 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
2657 FvInfoPtr
->FvBlocks
[1].Length
= 0;
2658 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
2662 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
);
2663 return EFI_INVALID_PARAMETER
;
2667 // Set Fv Size Information
2669 mFvTotalSize
= FvInfoPtr
->Size
;
2670 mFvTakenSize
= CurrentOffset
;
2676 FfsRebaseImageRead (
2677 IN VOID
*FileHandle
,
2678 IN UINTN FileOffset
,
2679 IN OUT UINT32
*ReadSize
,
2684 Routine Description:
2686 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2690 FileHandle - The handle to the PE/COFF file
2692 FileOffset - The offset, in bytes, into the file to read
2694 ReadSize - The number of bytes to read from the file starting at FileOffset
2696 Buffer - A pointer to the buffer to read the data into.
2700 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2704 CHAR8
*Destination8
;
2708 Destination8
= Buffer
;
2709 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
2712 *(Destination8
++) = *(Source8
++);
2721 IN EFI_FFS_FILE_HEADER
*FfsFile
,
2726 Routine Description:
2728 This function gets all child FvImages in the input FfsFile, and records
2729 their base address to the parent image.
2732 FvInfo A pointer to FV_INFO struture.
2733 FfsFile A pointer to Ffs file image that may contain FvImage.
2734 XipOffset The offset address to the parent FvImage base.
2738 EFI_SUCCESS Base address of child Fv image is recorded.
2743 EFI_FILE_SECTION_POINTER SubFvSection
;
2744 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
2745 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
2747 for (Index
= 1;; Index
++) {
2751 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
2752 if (EFI_ERROR (Status
)) {
2755 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ sizeof (EFI_FIRMWARE_VOLUME_IMAGE_SECTION
));
2759 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
2760 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
2768 IN OUT FV_INFO
*FvInfo
,
2770 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
2776 Routine Description:
2778 This function determines if a file is XIP and should be rebased. It will
2779 rebase any PE32 sections found in the file using the base address.
2783 FvInfo A pointer to FV_INFO struture.
2784 FileName Ffs File PathName
2785 FfsFile A pointer to Ffs file image.
2786 XipOffset The offset address to use for rebasing the XIP file image.
2787 FvMapFile FvMapFile to record the function address in one Fvimage
2791 EFI_SUCCESS The image was properly rebased.
2792 EFI_INVALID_PARAMETER An input parameter is invalid.
2793 EFI_ABORTED An error occurred while rebasing the input file image.
2794 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2795 EFI_NOT_FOUND No compressed sections could be found.
2800 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
2801 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
2802 EFI_PHYSICAL_ADDRESS XipBase
;
2803 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
2805 EFI_FILE_SECTION_POINTER CurrentPe32Section
;
2806 EFI_FFS_FILE_STATE SavedState
;
2807 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
2808 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
2809 UINT8
*MemoryImagePointer
;
2810 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
2811 CHAR8 PeFileName
[_MAX_PATH
];
2814 UINT8
*PeFileBuffer
;
2819 MemoryImagePointer
= NULL
;
2820 TEImageHeader
= NULL
;
2822 SectionHeader
= NULL
;
2825 PeFileBuffer
= NULL
;
2828 // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
2830 if ((FvInfo
->BaseAddress
== 0) && (FvInfo
->ForceRebase
== -1)) {
2835 // If ForceRebase Flag specified to FALSE, will always not take rebase action.
2837 if (FvInfo
->ForceRebase
== 0) {
2842 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
2845 // We only process files potentially containing PE32 sections.
2847 switch (FfsFile
->Type
) {
2848 case EFI_FV_FILETYPE_SECURITY_CORE
:
2849 case EFI_FV_FILETYPE_PEI_CORE
:
2850 case EFI_FV_FILETYPE_PEIM
:
2851 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2852 case EFI_FV_FILETYPE_DRIVER
:
2853 case EFI_FV_FILETYPE_DXE_CORE
:
2855 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
2857 // Rebase the inside FvImage.
2859 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
2862 // Search PE/TE section in FV sectin.
2869 // Rebase each PE32 section
2871 Status
= EFI_SUCCESS
;
2872 for (Index
= 1;; Index
++) {
2876 NewPe32BaseAddress
= 0;
2881 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
2882 if (EFI_ERROR (Status
)) {
2887 // Initialize context
2889 memset (&ImageContext
, 0, sizeof (ImageContext
));
2890 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
));
2891 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
2892 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
2893 if (EFI_ERROR (Status
)) {
2894 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
2898 if (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) {
2903 // Keep Image Context for PE image in FV
2905 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
2908 // Get File PdbPointer
2910 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
2913 // Get PeHeader pointer
2915 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) + ImageContext
.PeCoffHeaderOffset
);
2918 // Calculate the PE32 base address, based on file type
2920 switch (FfsFile
->Type
) {
2921 case EFI_FV_FILETYPE_SECURITY_CORE
:
2922 case EFI_FV_FILETYPE_PEI_CORE
:
2923 case EFI_FV_FILETYPE_PEIM
:
2924 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2926 // Check if section-alignment and file-alignment match or not
2928 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
2930 // Xip module has the same section alignment and file alignment.
2932 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
2936 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
2938 if (ImageContext
.RelocationsStripped
) {
2940 // Construct the original efi file Name
2942 strcpy (PeFileName
, FileName
);
2943 Cptr
= PeFileName
+ strlen (PeFileName
);
2944 while (*Cptr
!= '.') {
2948 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
2956 PeFile
= fopen (PeFileName
, "rb");
2957 if (PeFile
== NULL
) {
2958 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
2959 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
2960 //return EFI_ABORTED;
2964 // Get the file size
2966 PeFileSize
= _filelength (fileno (PeFile
));
2967 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
2968 if (PeFileBuffer
== NULL
) {
2969 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
2970 return EFI_OUT_OF_RESOURCES
;
2975 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
2981 // Handle pointer to the original efi image.
2983 ImageContext
.Handle
= PeFileBuffer
;
2984 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
2985 if (EFI_ERROR (Status
)) {
2986 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
2989 ImageContext
.RelocationsStripped
= FALSE
;
2992 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) - (UINTN
)FfsFile
;
2995 case EFI_FV_FILETYPE_DRIVER
:
2996 case EFI_FV_FILETYPE_DXE_CORE
:
2998 // Check if section-alignment and file-alignment match or not
3000 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
3002 // Xip module has the same section alignment and file alignment.
3004 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3007 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) - (UINTN
)FfsFile
;
3012 // Not supported file type
3018 // Relocation doesn't exist
3020 if (ImageContext
.RelocationsStripped
) {
3021 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3026 // Relocation exist and rebase
3029 // Load and Relocate Image Data
3031 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3032 if (MemoryImagePointer
== NULL
) {
3033 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3034 return EFI_OUT_OF_RESOURCES
;
3036 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3037 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3039 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3040 if (EFI_ERROR (Status
)) {
3041 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3042 free ((VOID
*) MemoryImagePointer
);
3046 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3047 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3048 if (EFI_ERROR (Status
)) {
3049 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3050 free ((VOID
*) MemoryImagePointer
);
3055 // Copy Relocated data to raw image file.
3057 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3060 sizeof (EFI_IMAGE_FILE_HEADER
) +
3061 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3064 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3066 (UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
) + SectionHeader
->PointerToRawData
,
3067 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3068 SectionHeader
->SizeOfRawData
3072 free ((VOID
*) MemoryImagePointer
);
3073 MemoryImagePointer
= NULL
;
3074 if (PeFileBuffer
!= NULL
) {
3075 free (PeFileBuffer
);
3076 PeFileBuffer
= NULL
;
3080 // Update Image Base Address
3082 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3083 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3084 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3085 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3087 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3088 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3095 // Now update file checksum
3097 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3098 SavedState
= FfsFile
->State
;
3099 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3101 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3102 (UINT8
*) (FfsFile
+ 1),
3103 GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
3105 FfsFile
->State
= SavedState
;
3109 // Get this module function address from ModulePeMapFile and add them into FvMap file
3113 // Default use FileName as map file path
3115 if (PdbPointer
== NULL
) {
3116 PdbPointer
= FileName
;
3119 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3122 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3123 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3124 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3125 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3126 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3129 // Only Peim code may have a TE section
3135 // Now process TE sections
3137 for (Index
= 1;; Index
++) {
3138 NewPe32BaseAddress
= 0;
3143 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3144 if (EFI_ERROR (Status
)) {
3149 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3152 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
));
3155 // Initialize context, load image info.
3157 memset (&ImageContext
, 0, sizeof (ImageContext
));
3158 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3159 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3160 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3161 if (EFI_ERROR (Status
)) {
3162 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3166 if (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) {
3171 // Keep Image Context for TE image in FV
3173 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3176 // Get File PdbPointer
3178 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3181 // Set new rebased address.
3183 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3184 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3187 // if reloc is stripped, try to get the original efi image to get reloc info.
3189 if (ImageContext
.RelocationsStripped
) {
3191 // Construct the original efi file name
3193 strcpy (PeFileName
, FileName
);
3194 Cptr
= PeFileName
+ strlen (PeFileName
);
3195 while (*Cptr
!= '.') {
3200 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3209 PeFile
= fopen (PeFileName
, "rb");
3210 if (PeFile
== NULL
) {
3211 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3212 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3213 //return EFI_ABORTED;
3216 // Get the file size
3218 PeFileSize
= _filelength (fileno (PeFile
));
3219 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3220 if (PeFileBuffer
== NULL
) {
3221 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3222 return EFI_OUT_OF_RESOURCES
;
3227 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3233 // Append reloc section into TeImage
3235 ImageContext
.Handle
= PeFileBuffer
;
3236 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3237 if (EFI_ERROR (Status
)) {
3238 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3241 ImageContext
.RelocationsStripped
= FALSE
;
3245 // Relocation doesn't exist
3247 if (ImageContext
.RelocationsStripped
) {
3248 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3253 // Relocation exist and rebase
3256 // Load and Relocate Image Data
3258 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3259 if (MemoryImagePointer
== NULL
) {
3260 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3261 return EFI_OUT_OF_RESOURCES
;
3263 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3264 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3266 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3267 if (EFI_ERROR (Status
)) {
3268 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3269 free ((VOID
*) MemoryImagePointer
);
3273 // Reloacate TeImage
3275 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3276 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3277 if (EFI_ERROR (Status
)) {
3278 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3279 free ((VOID
*) MemoryImagePointer
);
3284 // Copy the relocated image into raw image file.
3286 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3287 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3288 if (!ImageContext
.IsTeImage
) {
3290 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3291 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3292 SectionHeader
->SizeOfRawData
3296 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3297 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3298 SectionHeader
->SizeOfRawData
3304 // Free the allocated memory resource
3306 free ((VOID
*) MemoryImagePointer
);
3307 MemoryImagePointer
= NULL
;
3308 if (PeFileBuffer
!= NULL
) {
3309 free (PeFileBuffer
);
3310 PeFileBuffer
= NULL
;
3314 // Update Image Base Address
3316 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3319 // Now update file checksum
3321 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3322 SavedState
= FfsFile
->State
;
3323 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3325 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3326 (UINT8
*)(FfsFile
+ 1),
3327 GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
3329 FfsFile
->State
= SavedState
;
3332 // Get this module function address from ModulePeMapFile and add them into FvMap file
3336 // Default use FileName as map file path
3338 if (PdbPointer
== NULL
) {
3339 PdbPointer
= FileName
;
3355 FindApResetVectorPosition (
3356 IN MEMORY_FILE
*FvImage
,
3361 Routine Description:
3363 Find the position in this FvImage to place Ap reset vector.
3367 FvImage Memory file for the FV memory image.
3368 Pointer Pointer to pointer to position.
3372 EFI_NOT_FOUND - No satisfied position is found.
3373 EFI_SUCCESS - The suitable position is return.
3377 EFI_FFS_FILE_HEADER
*PadFile
;
3383 for (Index
= 1; ;Index
++) {
3385 // Find Pad File to add ApResetVector info
3387 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3388 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3390 // No Pad file to be found.
3395 // Get Pad file size.
3397 FileLength
= (*(UINT32
*)(PadFile
->Size
)) & 0x00FFFFFF;
3398 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3400 // FixPoint must be align on 0x1000 relative to FvImage Header
3402 FixPoint
= (UINT8
*) PadFile
+ sizeof (EFI_FFS_FILE_HEADER
);
3403 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3405 // FixPoint be larger at the last place of one fv image.
3407 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3412 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ sizeof (EFI_FFS_FILE_HEADER
))) {
3414 // No alignment FixPoint in this Pad File.
3419 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3421 // Find the position to place ApResetVector
3423 *Pointer
= FixPoint
;
3428 return EFI_NOT_FOUND
;
3433 IN MEMORY_FILE
*InfFile
,
3434 OUT CAP_INFO
*CapInfo
3438 Routine Description:
3440 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3444 InfFile Memory file image.
3445 CapInfo Information read from INF file.
3449 EFI_SUCCESS INF file information successfully retrieved.
3450 EFI_ABORTED INF file has an invalid format.
3451 EFI_NOT_FOUND A required string was not found in the INF file.
3454 CHAR8 Value
[_MAX_PATH
];
3456 UINTN Index
, Number
;
3460 // Initialize Cap info
3462 // memset (CapInfo, 0, sizeof (CAP_INFO));
3466 // Read the Capsule Guid
3468 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
3469 if (Status
== EFI_SUCCESS
) {
3471 // Get the Capsule Guid
3473 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
3474 if (EFI_ERROR (Status
)) {
3475 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3478 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3482 // Read the Capsule Header Size
3484 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
3485 if (Status
== EFI_SUCCESS
) {
3486 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3487 if (EFI_ERROR (Status
)) {
3488 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3491 CapInfo
->HeaderSize
= (UINT32
) Value64
;
3492 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3496 // Read the Capsule Flag
3498 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
3499 if (Status
== EFI_SUCCESS
) {
3500 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
3501 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
3502 if (strstr (Value
, "InitiateReset") != NULL
) {
3503 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3505 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
3506 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
3507 if (strstr (Value
, "InitiateReset") != NULL
) {
3508 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3511 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
3514 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
3518 // Read Capsule File name
3520 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
3521 if (Status
== EFI_SUCCESS
) {
3523 // Get output file name
3525 strcpy (CapInfo
->CapName
, Value
);
3529 // Read the Capsule FileImage
3532 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
3533 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
3537 // Read the capsule file name
3539 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
3541 if (Status
== EFI_SUCCESS
) {
3545 strcpy (CapInfo
->CapFiles
[Index
], Value
);
3546 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
3553 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
3561 IN CHAR8
*InfFileImage
,
3562 IN UINTN InfFileSize
,
3563 IN CHAR8
*CapFileName
3567 Routine Description:
3569 This is the main function which will be called from application to create UEFI Capsule image.
3573 InfFileImage Buffer containing the INF file contents.
3574 InfFileSize Size of the contents of the InfFileImage buffer.
3575 CapFileName Requested name for the Cap file.
3579 EFI_SUCCESS Function completed successfully.
3580 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3581 EFI_ABORTED Error encountered.
3582 EFI_INVALID_PARAMETER A required parameter was NULL.
3588 EFI_CAPSULE_HEADER
*CapsuleHeader
;
3589 MEMORY_FILE InfMemoryFile
;
3595 if (InfFileImage
!= NULL
) {
3597 // Initialize file structures
3599 InfMemoryFile
.FileImage
= InfFileImage
;
3600 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
3601 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
3604 // Parse the Cap inf file for header information
3606 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
3607 if (Status
!= EFI_SUCCESS
) {
3612 if (mCapDataInfo
.HeaderSize
== 0) {
3614 // make header size align 16 bytes.
3616 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
3617 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
3620 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
3621 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3622 return EFI_INVALID_PARAMETER
;
3625 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
3626 CapFileName
= mCapDataInfo
.CapName
;
3629 if (CapFileName
== NULL
) {
3630 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
3631 return EFI_INVALID_PARAMETER
;
3635 // Set Default Capsule Guid value
3637 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
3638 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
3641 // Calculate the size of capsule image.
3645 CapSize
= mCapDataInfo
.HeaderSize
;
3646 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3647 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3649 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3652 FileSize
= _filelength (fileno (fpin
));
3653 CapSize
+= FileSize
;
3659 // Allocate buffer for capsule image.
3661 CapBuffer
= (UINT8
*) malloc (CapSize
);
3662 if (CapBuffer
== NULL
) {
3663 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3664 return EFI_OUT_OF_RESOURCES
;
3668 // Initialize the capsule header to zero
3670 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
3673 // create capsule header and get capsule body
3675 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
3676 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
3677 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
3678 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
3679 CapsuleHeader
->CapsuleImageSize
= CapSize
;
3683 CapSize
= CapsuleHeader
->HeaderSize
;
3684 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3685 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3687 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3691 FileSize
= _filelength (fileno (fpin
));
3692 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
3695 CapSize
+= FileSize
;
3699 // write capsule data into the output file
3701 fpout
= fopen (CapFileName
, "wb");
3702 if (fpout
== NULL
) {
3703 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
3708 fwrite (CapBuffer
, 1, CapSize
, fpout
);
3711 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);