3 Copyright (c) 2004 - 2010, 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
;
216 // Read the FV File System Guid
218 if (!FvInfo
->FvFileSystemGuidSet
) {
219 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILESYSTEMGUID_STRING
, 0, Value
);
220 if (Status
== EFI_SUCCESS
) {
222 // Get the guid value
224 Status
= StringToGuid (Value
, &GuidValue
);
225 if (EFI_ERROR (Status
)) {
226 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, Value
);
229 memcpy (&FvInfo
->FvFileSystemGuid
, &GuidValue
, sizeof (EFI_GUID
));
230 FvInfo
->FvFileSystemGuidSet
= TRUE
;
235 // Read the FV Extension Header File Name
237 Status
= FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, EFI_FV_EXT_HEADER_FILE_NAME
, 0, Value
);
238 if (Status
== EFI_SUCCESS
) {
239 strcpy (FvInfo
->FvExtHeaderFile
, Value
);
243 // Read the FV file name
245 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FV_FILE_NAME_STRING
, 0, Value
);
246 if (Status
== EFI_SUCCESS
) {
248 // copy the file name
250 strcpy (FvInfo
->FvName
, Value
);
256 for (Index
= 0; Index
< sizeof (mFvbAttributeName
)/sizeof (CHAR8
*); Index
++) {
257 if ((mFvbAttributeName
[Index
] != NULL
) && \
258 (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAttributeName
[Index
], 0, Value
) == EFI_SUCCESS
)) {
259 if ((strcmp (Value
, TRUE_STRING
) == 0) || (strcmp (Value
, ONE_STRING
) == 0)) {
260 FvInfo
->FvAttributes
|= 1 << Index
;
261 } else if ((strcmp (Value
, FALSE_STRING
) != 0) && (strcmp (Value
, ZERO_STRING
) != 0)) {
262 Error (NULL
, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName
[Index
], TRUE_STRING
, FALSE_STRING
);
271 for (Index
= 0; Index
< sizeof (mFvbAlignmentName
)/sizeof (CHAR8
*); Index
++) {
272 if (FindToken (InfFile
, ATTRIBUTES_SECTION_STRING
, mFvbAlignmentName
[Index
], 0, Value
) == EFI_SUCCESS
) {
273 if (strcmp (Value
, TRUE_STRING
) == 0) {
274 FvInfo
->FvAttributes
|= Index
<< 16;
275 DebugMsg (NULL
, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName
[Index
]);
284 for (Index
= 0; Index
< MAX_NUMBER_OF_FV_BLOCKS
; Index
++) {
285 if (FvInfo
->FvBlocks
[Index
].Length
== 0) {
289 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_BLOCK_SIZE_STRING
, Index
, Value
);
291 if (Status
== EFI_SUCCESS
) {
293 // Update the size of block
295 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
296 if (EFI_ERROR (Status
)) {
297 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
301 FvInfo
->FvBlocks
[Index
].Length
= (UINT32
) Value64
;
302 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING
, Value
);
305 // If there is no blocks size, but there is the number of block, then we have a mismatched pair
306 // and should return an error.
308 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
309 if (!EFI_ERROR (Status
)) {
310 Error (NULL
, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING
, EFI_BLOCK_SIZE_STRING
);
321 // Read blocks number
323 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_NUM_BLOCKS_STRING
, Index
, Value
);
325 if (Status
== EFI_SUCCESS
) {
327 // Update the number of blocks
329 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
330 if (EFI_ERROR (Status
)) {
331 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
335 FvInfo
->FvBlocks
[Index
].NumBlocks
= (UINT32
) Value64
;
336 DebugMsg (NULL
, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING
, Value
);
342 Error (NULL
, 0, 2001, "Missing required argument", "block size.");
350 for (Number
= 0; Number
< MAX_NUMBER_OF_FILES_IN_FV
; Number
++) {
351 if (FvInfo
->FvFiles
[Number
][0] == '\0') {
356 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_FV
; Index
++) {
358 // Read the FFS file list
360 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Index
, Value
);
362 if (Status
== EFI_SUCCESS
) {
366 strcpy (FvInfo
->FvFiles
[Number
+ Index
], Value
);
367 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
, Value
);
373 if ((Index
+ Number
) == 0) {
374 Warning (NULL
, 0, 0, "FV components are not specified.", NULL
);
382 IN EFI_FFS_FILE_HEADER
*FfsFile
,
383 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
389 This function changes the FFS file attributes based on the erase polarity
390 of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
403 if (FvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
404 FfsFile
->State
= (UINT8
)~(FfsFile
->State
);
405 // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
411 IN EFI_FFS_FILE_HEADER
*FfsFile
,
412 IN OUT UINT32
*Alignment
418 This function determines the alignment of the FFS input file from the file
423 FfsFile FFS file to parse
424 Alignment The minimum required alignment offset of the FFS file
428 EFI_SUCCESS The function completed successfully.
429 EFI_INVALID_PARAMETER One of the input parameters was invalid.
430 EFI_ABORTED An error occurred.
435 // Verify input parameters.
437 if (FfsFile
== NULL
|| Alignment
== NULL
) {
438 return EFI_INVALID_PARAMETER
;
441 switch ((FfsFile
->Attributes
>> 3) & 0x07) {
445 // 8 byte alignment, mini alignment requirement for FFS file.
459 // 128 byte alignment
466 // 512 byte alignment
487 // 32K byte alignment
494 // 64K byte alignment
508 IN OUT MEMORY_FILE
*FvImage
,
509 IN UINT32 DataAlignment
,
511 IN EFI_FIRMWARE_VOLUME_EXT_HEADER
*ExtHeader
517 This function adds a pad file to the FV image if it required to align the
518 data of the next file.
522 FvImage The memory image of the FV to add it to.
523 The current offset must be valid.
524 DataAlignment The data alignment of the next FFS file.
525 FvEnd End of the empty data in FvImage.
526 ExtHeader PI FvExtHeader Optional
530 EFI_SUCCESS The function completed successfully.
531 EFI_INVALID_PARAMETER One of the input parameters was invalid.
532 EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
537 EFI_FFS_FILE_HEADER
*PadFile
;
541 // Verify input parameters.
543 if (FvImage
== NULL
) {
544 return EFI_INVALID_PARAMETER
;
548 // Check if a pad file is necessary
550 if ((ExtHeader
== NULL
) && (((UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ sizeof (EFI_FFS_FILE_HEADER
)) % DataAlignment
== 0)) {
555 // Calculate the pad file size
558 // This is the earliest possible valid offset (current plus pad file header
559 // plus the next file header)
561 PadFileSize
= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
+ (sizeof (EFI_FFS_FILE_HEADER
) * 2);
564 // Add whatever it takes to get to the next aligned address
566 while ((PadFileSize
% DataAlignment
) != 0) {
570 // Subtract the next file header size
572 PadFileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
575 // Subtract the starting offset to get size
577 PadFileSize
-= (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
;
580 // Append extension header size
582 if (ExtHeader
!= NULL
) {
583 PadFileSize
= PadFileSize
+ ExtHeader
->ExtHeaderSize
;
587 // Verify that we have enough space for the file header
589 if (((UINTN
) FvImage
->CurrentFilePointer
+ PadFileSize
) > (UINTN
) FvEnd
) {
590 return EFI_OUT_OF_RESOURCES
;
594 // Write pad file header
596 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
599 // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
601 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
602 PadFile
->Attributes
= 0;
605 // Write pad file size (calculated size minus next file header size)
607 PadFile
->Size
[0] = (UINT8
) (PadFileSize
& 0xFF);
608 PadFile
->Size
[1] = (UINT8
) ((PadFileSize
>> 8) & 0xFF);
609 PadFile
->Size
[2] = (UINT8
) ((PadFileSize
>> 16) & 0xFF);
612 // Fill in checksums and state, they must be 0 for checksumming.
614 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
615 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
617 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
618 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
620 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
622 (EFI_FFS_FILE_HEADER
*) PadFile
,
623 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
627 // Update the current FV pointer
629 FvImage
->CurrentFilePointer
+= PadFileSize
;
631 if (ExtHeader
!= NULL
) {
633 // Copy Fv Extension Header and Set Fv Extension header offset
635 memcpy (PadFile
+ 1, ExtHeader
, ExtHeader
->ExtHeaderSize
);
636 ((EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
)->ExtHeaderOffset
= (UINT16
) ((UINTN
) (PadFile
+ 1) - (UINTN
) FvImage
->FileImage
);
638 // Make next file start at QWord Boundry
640 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
641 FvImage
->CurrentFilePointer
++;
650 IN EFI_FFS_FILE_HEADER
*FileBuffer
656 This function checks the header to validate if it is a VTF file
660 FileBuffer Buffer in which content of a file has been read.
664 TRUE If this is a VTF file
665 FALSE If this is not a VTF file
669 if (!memcmp (&FileBuffer
->Name
, &mEfiFirmwareVolumeTopFileGuid
, sizeof (EFI_GUID
))) {
678 IN OUT
FILE *FvMapFile
,
680 IN EFI_FFS_FILE_HEADER
*FfsFile
,
681 IN EFI_PHYSICAL_ADDRESS ImageBaseAddress
,
682 IN PE_COFF_LOADER_IMAGE_CONTEXT
*pImageContext
688 This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
689 from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
693 FvMapFile A pointer to FvMap File
694 FileName Ffs File PathName
695 FfsFile A pointer to Ffs file image.
696 ImageBaseAddress PeImage Base Address.
697 pImageContext Image Context Information.
701 EFI_SUCCESS Added required map information.
705 CHAR8 PeMapFileName
[_MAX_PATH
];
707 CHAR8 FileGuidName
[MAX_LINE_LEN
];
709 CHAR8 Line
[MAX_LINE_LEN
];
710 CHAR8 KeyWord
[MAX_LINE_LEN
];
711 CHAR8 FunctionName
[MAX_LINE_LEN
];
712 EFI_PHYSICAL_ADDRESS FunctionAddress
;
714 CHAR8 FunctionTypeName
[MAX_LINE_LEN
];
716 UINT32 AddressOfEntryPoint
;
718 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
719 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
720 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
721 unsigned long long TempLongAddress
;
722 UINT32 TextVirtualAddress
;
723 UINT32 DataVirtualAddress
;
724 EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress
;
727 // Init local variable
731 // Print FileGuid to string buffer.
733 PrintGuidToBuffer (&FfsFile
->Name
, (UINT8
*)FileGuidName
, MAX_LINE_LEN
, TRUE
);
736 // Construct Map file Name
738 strcpy (PeMapFileName
, FileName
);
741 // Change '\\' to '/', unified path format.
743 Cptr
= PeMapFileName
;
744 while (*Cptr
!= '\0') {
746 *Cptr
= FILE_SEP_CHAR
;
754 Cptr
= PeMapFileName
+ strlen (PeMapFileName
);
755 while ((*Cptr
!= '.') && (Cptr
>= PeMapFileName
)) {
758 if (Cptr
< PeMapFileName
) {
759 return EFI_NOT_FOUND
;
771 while ((*Cptr
!= FILE_SEP_CHAR
) && (Cptr
>= PeMapFileName
)) {
775 strcpy (KeyWord
, Cptr
+ 1);
779 // AddressOfEntryPoint and Offset in Image
781 if (!pImageContext
->IsTeImage
) {
782 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINT8
*) pImageContext
->Handle
+ pImageContext
->PeCoffHeaderOffset
);
783 AddressOfEntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
785 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
788 sizeof (EFI_IMAGE_FILE_HEADER
) +
789 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
791 Index
= ImgHdr
->Pe32
.FileHeader
.NumberOfSections
;
793 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) pImageContext
->Handle
;
794 AddressOfEntryPoint
= TEImageHeader
->AddressOfEntryPoint
;
795 Offset
= TEImageHeader
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
796 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
797 Index
= TEImageHeader
->NumberOfSections
;
801 // module information output
803 if (ImageBaseAddress
== 0) {
804 fprintf (FvMapFile
, "%s (dummy) (", KeyWord
);
805 fprintf (FvMapFile
, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress
);
807 fprintf (FvMapFile
, "%s (Fixed Flash Address, ", KeyWord
);
808 fprintf (FvMapFile
, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress
+ Offset
));
811 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&& pImageContext
->Machine
== EFI_IMAGE_MACHINE_IA64
) {
813 // Process IPF PLABEL to get the real address after the image has been rebased.
814 // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.
816 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64
*)((UINTN
) pImageContext
->Handle
+ (UINTN
) AddressOfEntryPoint
)));
818 fprintf (FvMapFile
, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress
+ AddressOfEntryPoint
));
820 fprintf (FvMapFile
, ")\n");
822 fprintf (FvMapFile
, "(GUID=%s", FileGuidName
);
823 TextVirtualAddress
= 0;
824 DataVirtualAddress
= 0;
825 for (; Index
> 0; Index
--, SectionHeader
++) {
826 if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".text") == 0) {
827 TextVirtualAddress
= SectionHeader
->VirtualAddress
;
828 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".data") == 0) {
829 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
830 } else if (stricmp ((CHAR8
*)SectionHeader
->Name
, ".sdata") == 0) {
831 DataVirtualAddress
= SectionHeader
->VirtualAddress
;
834 fprintf (FvMapFile
, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ TextVirtualAddress
));
835 fprintf (FvMapFile
, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress
+ DataVirtualAddress
));
836 fprintf (FvMapFile
, ")\n\n");
841 PeMapFile
= fopen (PeMapFileName
, "r");
842 if (PeMapFile
== NULL
) {
843 // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
846 VerboseMsg ("The map file is %s", PeMapFileName
);
849 // Output Functions information into Fv Map file
851 LinkTimeBaseAddress
= 0;
852 while (fgets (Line
, MAX_LINE_LEN
, PeMapFile
) != NULL
) {
856 if (Line
[0] == 0x0a) {
861 // By Address and Static keyword
863 if (FunctionType
== 0) {
864 sscanf (Line
, "%s", KeyWord
);
865 if (stricmp (KeyWord
, "Address") == 0) {
870 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
871 } else if (stricmp (KeyWord
, "Static") == 0) {
873 // static function list
876 fgets (Line
, MAX_LINE_LEN
, PeMapFile
);
877 } else if (stricmp (KeyWord
, "Preferred") ==0) {
878 sscanf (Line
+ strlen (" Preferred load address is"), "%llx", &TempLongAddress
);
879 LinkTimeBaseAddress
= (UINT64
) TempLongAddress
;
884 // Printf Function Information
886 if (FunctionType
== 1) {
887 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
888 FunctionAddress
= (UINT64
) TempLongAddress
;
889 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
890 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
891 fprintf (FvMapFile
, "%s\n", FunctionName
);
893 } else if (FunctionType
== 2) {
894 sscanf (Line
, "%s %s %llx %s", KeyWord
, FunctionName
, &TempLongAddress
, FunctionTypeName
);
895 FunctionAddress
= (UINT64
) TempLongAddress
;
896 if (FunctionTypeName
[1] == '\0' && (FunctionTypeName
[0] == 'f' || FunctionTypeName
[0] == 'F')) {
897 fprintf (FvMapFile
, " 0x%010llx ", (unsigned long long) (ImageBaseAddress
+ FunctionAddress
- LinkTimeBaseAddress
));
898 fprintf (FvMapFile
, "%s\n", FunctionName
);
905 fprintf (FvMapFile
, "\n\n");
913 IN OUT MEMORY_FILE
*FvImage
,
916 IN OUT EFI_FFS_FILE_HEADER
**VtfFileImage
,
918 IN
FILE *FvReportFile
924 This function adds a file to the FV image. The file will pad to the
925 appropriate alignment if required.
929 FvImage The memory image of the FV to add it to. The current offset
931 FvInfo Pointer to information about the FV.
932 Index The file in the FvInfo file list to add.
933 VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
934 to the end of the FvImage then no VTF previously found.
935 FvMapFile Pointer to FvMap File
936 FvReportFile Pointer to FvReport File
940 EFI_SUCCESS The function completed successfully.
941 EFI_INVALID_PARAMETER One of the input parameters was invalid.
942 EFI_ABORTED An error occurred.
943 EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
951 UINT32 CurrentFileAlignment
;
954 UINT8 FileGuidString
[PRINTED_GUID_BUFFER_SIZE
];
958 // Verify input parameters.
960 if (FvImage
== NULL
|| FvInfo
== NULL
|| FvInfo
->FvFiles
[Index
][0] == 0 || VtfFileImage
== NULL
) {
961 return EFI_INVALID_PARAMETER
;
965 // Read the file to add
967 NewFile
= fopen (FvInfo
->FvFiles
[Index
], "rb");
969 if (NewFile
== NULL
) {
970 Error (NULL
, 0, 0001, "Error opening file", FvInfo
->FvFiles
[Index
]);
977 FileSize
= _filelength (fileno (NewFile
));
980 // Read the file into a buffer
982 FileBuffer
= malloc (FileSize
);
983 if (FileBuffer
== NULL
) {
984 Error (NULL
, 0, 4001, "Resouce", "memory cannot be allocated!");
985 return EFI_OUT_OF_RESOURCES
;
988 NumBytesRead
= fread (FileBuffer
, sizeof (UINT8
), FileSize
, NewFile
);
991 // Done with the file, from this point on we will just use the buffer read.
996 // Verify read successful
998 if (NumBytesRead
!= sizeof (UINT8
) * FileSize
) {
1000 Error (NULL
, 0, 0004, "Error reading file", FvInfo
->FvFiles
[Index
]);
1005 // For None PI Ffs file, directly add them into FvImage.
1007 if (!FvInfo
->IsPiFvImage
) {
1008 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1009 if (FvInfo
->SizeofFvFiles
[Index
] > FileSize
) {
1010 FvImage
->CurrentFilePointer
+= FvInfo
->SizeofFvFiles
[Index
];
1012 FvImage
->CurrentFilePointer
+= FileSize
;
1020 Status
= VerifyFfsFile ((EFI_FFS_FILE_HEADER
*)FileBuffer
);
1021 if (EFI_ERROR (Status
)) {
1023 Error (NULL
, 0, 3000, "Invalid", "%s is a FFS file.", FvInfo
->FvFiles
[Index
]);
1024 return EFI_INVALID_PARAMETER
;
1028 // Verify space exists to add the file
1030 if (FileSize
> (UINTN
) ((UINTN
) *VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
)) {
1032 Error (NULL
, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo
->FvFiles
[Index
]);
1033 return EFI_OUT_OF_RESOURCES
;
1037 // Verify the input file is the duplicated file in this Fv image
1039 for (Index1
= 0; Index1
< Index
; Index1
++) {
1040 if (CompareGuid ((EFI_GUID
*) FileBuffer
, &mFileGuidArray
[Index1
]) == 0) {
1041 Error (NULL
, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1
+ 1, (unsigned) Index
+ 1);
1042 PrintGuid ((EFI_GUID
*) FileBuffer
);
1043 return EFI_INVALID_PARAMETER
;
1046 CopyMem (&mFileGuidArray
[Index
], FileBuffer
, sizeof (EFI_GUID
));
1049 // Update the file state based on polarity of the FV.
1051 UpdateFfsFileState (
1052 (EFI_FFS_FILE_HEADER
*) FileBuffer
,
1053 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1057 // Check if alignment is required
1059 ReadFfsAlignment ((EFI_FFS_FILE_HEADER
*) FileBuffer
, &CurrentFileAlignment
);
1062 // Find the largest alignment of all the FFS files in the FV
1064 if (CurrentFileAlignment
> MaxFfsAlignment
) {
1065 MaxFfsAlignment
= CurrentFileAlignment
;
1068 // If we have a VTF file, add it at the top.
1070 if (IsVtfFile ((EFI_FFS_FILE_HEADER
*) FileBuffer
)) {
1071 if ((UINTN
) *VtfFileImage
== (UINTN
) FvImage
->Eof
) {
1073 // No previous VTF, add this one.
1075 *VtfFileImage
= (EFI_FFS_FILE_HEADER
*) (UINTN
) ((UINTN
) FvImage
->FileImage
+ FvInfo
->Size
- FileSize
);
1077 // Sanity check. The file MUST align appropriately
1079 if (((UINTN
) *VtfFileImage
+ sizeof (EFI_FFS_FILE_HEADER
) - (UINTN
) FvImage
->FileImage
) % (1 << CurrentFileAlignment
)) {
1080 Error (NULL
, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment
));
1085 // Rebase the PE or TE image in FileBuffer of FFS file for XIP
1086 // Rebase for the debug genfvmap tool
1088 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) *VtfFileImage
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1089 if (EFI_ERROR (Status
)) {
1090 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1096 memcpy (*VtfFileImage
, FileBuffer
, FileSize
);
1098 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1099 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned)(UINTN
) (((UINT8
*)*VtfFileImage
) - (UINTN
)FvImage
->FileImage
), FileGuidString
);
1102 DebugMsg (NULL
, 0, 9, "Add VTF FFS file in FV image", NULL
);
1106 // Already found a VTF file.
1108 Error (NULL
, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
1115 // Add pad file if necessary
1117 Status
= AddPadFile (FvImage
, 1 << CurrentFileAlignment
, *VtfFileImage
, NULL
);
1118 if (EFI_ERROR (Status
)) {
1119 Error (NULL
, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
1126 if ((UINTN
) (FvImage
->CurrentFilePointer
+ FileSize
) <= (UINTN
) (*VtfFileImage
)) {
1128 // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
1129 // Rebase Bs and Rt drivers for the debug genfvmap tool.
1131 Status
= FfsRebase (FvInfo
, FvInfo
->FvFiles
[Index
], (EFI_FFS_FILE_HEADER
*) FileBuffer
, (UINTN
) FvImage
->CurrentFilePointer
- (UINTN
) FvImage
->FileImage
, FvMapFile
);
1132 if (EFI_ERROR (Status
)) {
1133 Error (NULL
, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo
->FvFiles
[Index
]);
1139 memcpy (FvImage
->CurrentFilePointer
, FileBuffer
, FileSize
);
1140 PrintGuidToBuffer ((EFI_GUID
*) FileBuffer
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
1141 fprintf (FvReportFile
, "0x%08X %s\n", (unsigned) (FvImage
->CurrentFilePointer
- FvImage
->FileImage
), FileGuidString
);
1142 FvImage
->CurrentFilePointer
+= FileSize
;
1144 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo
->FvFiles
[Index
]);
1149 // Make next file start at QWord Boundry
1151 while (((UINTN
) FvImage
->CurrentFilePointer
& (EFI_FFS_FILE_HEADER_ALIGNMENT
- 1)) != 0) {
1152 FvImage
->CurrentFilePointer
++;
1157 // Free allocated memory.
1166 IN MEMORY_FILE
*FvImage
,
1167 IN EFI_FFS_FILE_HEADER
*VtfFileImage
1171 Routine Description:
1173 This function places a pad file between the last file in the FV and the VTF
1174 file if the VTF file exists.
1178 FvImage Memory file for the FV memory image
1179 VtfFileImage The address of the VTF file. If this is the end of the FV
1180 image, no VTF exists and no pad file is needed.
1184 EFI_SUCCESS Completed successfully.
1185 EFI_INVALID_PARAMETER One of the input parameters was NULL.
1189 EFI_FFS_FILE_HEADER
*PadFile
;
1193 // If there is no VTF or the VTF naturally follows the previous file without a
1194 // pad file, then there's nothing to do
1196 if ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->Eof
|| \
1197 ((UINTN
) VtfFileImage
== (UINTN
) FvImage
->CurrentFilePointer
)) {
1201 if ((UINTN
) VtfFileImage
< (UINTN
) FvImage
->CurrentFilePointer
) {
1202 return EFI_INVALID_PARAMETER
;
1206 // Pad file starts at beginning of free space
1208 PadFile
= (EFI_FFS_FILE_HEADER
*) FvImage
->CurrentFilePointer
;
1211 // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
1213 PadFile
->Type
= EFI_FV_FILETYPE_FFS_PAD
;
1214 PadFile
->Attributes
= 0;
1217 // FileSize includes the EFI_FFS_FILE_HEADER
1219 FileSize
= (UINTN
) VtfFileImage
- (UINTN
) FvImage
->CurrentFilePointer
;
1220 PadFile
->Size
[0] = (UINT8
) (FileSize
& 0x000000FF);
1221 PadFile
->Size
[1] = (UINT8
) ((FileSize
& 0x0000FF00) >> 8);
1222 PadFile
->Size
[2] = (UINT8
) ((FileSize
& 0x00FF0000) >> 16);
1225 // Fill in checksums and state, must be zero during checksum calculation.
1227 PadFile
->IntegrityCheck
.Checksum
.Header
= 0;
1228 PadFile
->IntegrityCheck
.Checksum
.File
= 0;
1230 PadFile
->IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 ((UINT8
*) PadFile
, sizeof (EFI_FFS_FILE_HEADER
));
1231 PadFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1233 PadFile
->State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1235 UpdateFfsFileState (
1236 (EFI_FFS_FILE_HEADER
*) PadFile
,
1237 (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
->FileImage
1240 // Update the current FV pointer
1242 FvImage
->CurrentFilePointer
= FvImage
->Eof
;
1249 IN MEMORY_FILE
*FvImage
,
1251 IN EFI_FFS_FILE_HEADER
*VtfFile
1255 Routine Description:
1257 This parses the FV looking for the PEI core and then plugs the address into
1258 the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
1259 complete an IA32 Bootstrap FV.
1263 FvImage Memory file for the FV memory image
1264 FvInfo Information read from INF file.
1265 VtfFile Pointer to the VTF file in the FV image.
1269 EFI_SUCCESS Function Completed successfully.
1270 EFI_ABORTED Error encountered.
1271 EFI_INVALID_PARAMETER A required parameter was NULL.
1272 EFI_NOT_FOUND PEI Core file not found.
1276 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1277 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1279 EFI_FILE_SECTION_POINTER Pe32Section
;
1283 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1284 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1285 EFI_PHYSICAL_ADDRESS
*SecCoreEntryAddressPtr
;
1286 INT32 Ia32SecEntryOffset
;
1287 UINT32
*Ia32ResetAddressPtr
;
1289 UINT8
*BytePointer2
;
1290 UINT16
*WordPointer
;
1294 EFI_FFS_FILE_STATE SavedState
;
1296 FIT_TABLE
*FitTablePtr
;
1297 BOOLEAN Vtf0Detected
;
1300 // Verify input parameters
1302 if (FvImage
== NULL
|| FvInfo
== NULL
|| VtfFile
== NULL
) {
1303 return EFI_INVALID_PARAMETER
;
1306 // Initialize FV library
1308 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1313 Status
= VerifyFfsFile (VtfFile
);
1314 if (EFI_ERROR (Status
)) {
1315 return EFI_INVALID_PARAMETER
;
1319 (((UINTN
)FvImage
->Eof
- (UINTN
)FvImage
->FileImage
) >=
1320 IA32_X64_VTF_SIGNATURE_OFFSET
) &&
1321 (*(UINT32
*)(VOID
*)((UINTN
) FvImage
->Eof
-
1322 IA32_X64_VTF_SIGNATURE_OFFSET
) ==
1323 IA32_X64_VTF0_SIGNATURE
)
1325 Vtf0Detected
= TRUE
;
1327 Vtf0Detected
= FALSE
;
1331 // Find the Sec Core
1333 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1334 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1337 // If the SEC core file is not found, but the VTF-0 signature
1338 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1339 // This means no modifications are required to the VTF.
1344 Error (NULL
, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
1348 // Sec Core found, now find PE32 section
1350 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1351 if (Status
== EFI_NOT_FOUND
) {
1352 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1355 if (EFI_ERROR (Status
)) {
1356 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1360 Status
= GetPe32Info (
1361 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1367 if (EFI_ERROR (Status
)) {
1368 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1374 (MachineType
== EFI_IMAGE_MACHINE_IA32
||
1375 MachineType
== EFI_IMAGE_MACHINE_X64
)
1378 // If the SEC core code is IA32 or X64 and the VTF-0 signature
1379 // is found, we'll treat it as a VTF-0 'Volume Top File'.
1380 // This means no modifications are required to the VTF.
1386 // Physical address is FV base + offset of PE32 + offset of the entry point
1388 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1389 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1390 SecCorePhysicalAddress
+= EntryPoint
;
1391 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1394 // Find the PEI Core
1396 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1397 if (EFI_ERROR (Status
) || PeiCoreFile
== NULL
) {
1398 Error (NULL
, 0, 3000, "Invalid", "could not find the PEI core in the FV.");
1402 // PEI Core found, now find PE32 or TE section
1404 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1405 if (Status
== EFI_NOT_FOUND
) {
1406 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1409 if (EFI_ERROR (Status
)) {
1410 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
1414 Status
= GetPe32Info (
1415 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1421 if (EFI_ERROR (Status
)) {
1422 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
1426 // Physical address is FV base + offset of PE32 + offset of the entry point
1428 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1429 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1430 PeiCorePhysicalAddress
+= EntryPoint
;
1431 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1433 if (MachineType
== EFI_IMAGE_MACHINE_IA64
) {
1435 // Update PEI_CORE address
1438 // Set the uncached attribute bit in the physical address
1440 PeiCorePhysicalAddress
|= 0x8000000000000000ULL
;
1443 // Check if address is aligned on a 16 byte boundary
1445 if (PeiCorePhysicalAddress
& 0xF) {
1446 Error (NULL
, 0, 3000, "Invalid",
1447 "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1448 (unsigned long long) PeiCorePhysicalAddress
1453 // First Get the FIT table address
1455 FitAddress
= (*(UINT64
*) (FvImage
->Eof
- IPF_FIT_ADDRESS_OFFSET
)) & 0xFFFFFFFF;
1457 FitTablePtr
= (FIT_TABLE
*) (FvImage
->FileImage
+ (FitAddress
- FvInfo
->BaseAddress
));
1459 Status
= UpdatePeiCoreEntryInFit (FitTablePtr
, PeiCorePhysicalAddress
);
1461 if (!EFI_ERROR (Status
)) {
1462 UpdateFitCheckSum (FitTablePtr
);
1466 // Update SEC_CORE address
1469 // Set the uncached attribute bit in the physical address
1471 SecCorePhysicalAddress
|= 0x8000000000000000ULL
;
1473 // Check if address is aligned on a 16 byte boundary
1475 if (SecCorePhysicalAddress
& 0xF) {
1476 Error (NULL
, 0, 3000, "Invalid",
1477 "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",
1478 (unsigned long long) SecCorePhysicalAddress
1483 // Update the address
1485 SecCoreEntryAddressPtr
= (EFI_PHYSICAL_ADDRESS
*) ((UINTN
) FvImage
->Eof
- IPF_SALE_ENTRY_ADDRESS_OFFSET
);
1486 *SecCoreEntryAddressPtr
= SecCorePhysicalAddress
;
1488 } else if (MachineType
== EFI_IMAGE_MACHINE_IA32
|| MachineType
== EFI_IMAGE_MACHINE_X64
) {
1490 // Get the location to update
1492 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_PEI_CORE_ENTRY_OFFSET
);
1495 // Write lower 32 bits of physical address for Pei Core entry
1497 *Ia32ResetAddressPtr
= (UINT32
) PeiCorePhysicalAddress
;
1500 // Write SecCore Entry point relative address into the jmp instruction in reset vector.
1502 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- IA32_SEC_CORE_ENTRY_OFFSET
);
1504 Ia32SecEntryOffset
= (INT32
) (SecCorePhysicalAddress
- (FV_IMAGES_TOP_ADDRESS
- IA32_SEC_CORE_ENTRY_OFFSET
+ 2));
1505 if (Ia32SecEntryOffset
<= -65536) {
1506 Error (NULL
, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
1507 return STATUS_ERROR
;
1510 *(UINT16
*) Ia32ResetAddressPtr
= (UINT16
) Ia32SecEntryOffset
;
1513 // Update the BFV base address
1515 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 4);
1516 *Ia32ResetAddressPtr
= (UINT32
) (FvInfo
->BaseAddress
);
1517 DebugMsg (NULL
, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo
->BaseAddress
);
1520 // Update the Startup AP in the FVH header block ZeroVector region.
1522 BytePointer
= (UINT8
*) ((UINTN
) FvImage
->FileImage
);
1523 if (FvInfo
->Size
<= 0x10000) {
1524 BytePointer2
= m64kRecoveryStartupApDataArray
;
1525 } else if (FvInfo
->Size
<= 0x20000) {
1526 BytePointer2
= m128kRecoveryStartupApDataArray
;
1528 BytePointer2
= m128kRecoveryStartupApDataArray
;
1530 // Find the position to place Ap reset vector, the offset
1531 // between the position and the end of Fvrecovery.fv file
1532 // should not exceed 128kB to prevent Ap reset vector from
1533 // outside legacy E and F segment
1535 Status
= FindApResetVectorPosition (FvImage
, &BytePointer
);
1536 if (EFI_ERROR (Status
)) {
1537 Error (NULL
, 0, 3000, "Invalid", "Cannot find the appropriate location in FvImage to add Ap reset vector!");
1542 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
; Index
++) {
1543 BytePointer
[Index
] = BytePointer2
[Index
];
1546 // Calculate the checksum
1549 WordPointer
= (UINT16
*) (BytePointer
);
1550 for (Index
= 0; Index
< SIZEOF_STARTUP_DATA_ARRAY
/ 2; Index
++) {
1551 CheckSum
= (UINT16
) (CheckSum
+ ((UINT16
) *WordPointer
));
1555 // Update the checksum field
1557 WordPointer
= (UINT16
*) (BytePointer
+ SIZEOF_STARTUP_DATA_ARRAY
- 2);
1558 *WordPointer
= (UINT16
) (0x10000 - (UINT32
) CheckSum
);
1561 // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
1563 IpiVector
= (UINT32
) (FV_IMAGES_TOP_ADDRESS
- ((UINTN
) FvImage
->Eof
- (UINTN
) BytePointer
));
1564 DebugMsg (NULL
, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector
);
1565 if ((IpiVector
& 0xFFF) != 0) {
1566 Error (NULL
, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
1569 IpiVector
= IpiVector
>> 12;
1570 IpiVector
= IpiVector
& 0xFF;
1573 // Write IPI Vector at Offset FvrecoveryFileSize - 8
1575 Ia32ResetAddressPtr
= (UINT32
*) ((UINTN
) FvImage
->Eof
- 8);
1576 *Ia32ResetAddressPtr
= IpiVector
;
1577 } else if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1579 // Since the ARM reset vector is in the FV Header you really don't need a
1580 // Volume Top File, but if you have one for some reason don't crash...
1583 Error (NULL
, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType
);
1588 // Now update file checksum
1590 SavedState
= VtfFile
->State
;
1591 VtfFile
->IntegrityCheck
.Checksum
.File
= 0;
1593 if (VtfFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1594 VtfFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1595 (UINT8
*) (VtfFile
+ 1),
1596 GetLength (VtfFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
1599 VtfFile
->IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1602 VtfFile
->State
= SavedState
;
1609 UpdateArmResetVectorIfNeeded (
1610 IN MEMORY_FILE
*FvImage
,
1615 Routine Description:
1616 This parses the FV looking for SEC and patches that address into the
1617 beginning of the FV header.
1619 For ARM the reset vector is at 0x00000000 or 0xFFFF0000.
1620 This would commonly map to the first entry in the ROM.
1630 We support two schemes on ARM.
1631 1) Beginning of the FV is the reset vector
1632 2) Reset vector is data bytes FDF file and that code branches to reset vector
1633 in the beginning of the FV (fixed size offset).
1636 Need to have the jump for the reset vector at location zero.
1637 We also need to store the address or PEI (if it exists).
1638 We stub out a return from interrupt in case the debugger
1640 The optional entry to the common exception handler is
1641 to support full featured exception handling from ROM and is currently
1642 not support by this tool.
1645 FvImage Memory file for the FV memory image
1646 FvInfo Information read from INF file.
1650 EFI_SUCCESS Function Completed successfully.
1651 EFI_ABORTED Error encountered.
1652 EFI_INVALID_PARAMETER A required parameter was NULL.
1653 EFI_NOT_FOUND PEI Core file not found.
1657 EFI_FFS_FILE_HEADER
*PeiCoreFile
;
1658 EFI_FFS_FILE_HEADER
*SecCoreFile
;
1660 EFI_FILE_SECTION_POINTER Pe32Section
;
1664 EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress
;
1665 EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress
;
1666 INT32 ResetVector
[4]; // 0 - is branch relative to SEC entry point
1667 // 1 - PEI Entry Point
1668 // 2 - movs pc,lr for a SWI handler
1669 // 3 - Place holder for Common Exception Handler
1672 // Verify input parameters
1674 if (FvImage
== NULL
|| FvInfo
== NULL
) {
1675 return EFI_INVALID_PARAMETER
;
1678 // Initialize FV library
1680 InitializeFvLib (FvImage
->FileImage
, FvInfo
->Size
);
1683 // Find the Sec Core
1685 Status
= GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE
, 1, &SecCoreFile
);
1686 if (EFI_ERROR (Status
) || SecCoreFile
== NULL
) {
1688 // Maybe hardware does SEC job and we only have PEI Core?
1692 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1694 PeiCorePhysicalAddress
= 0;
1695 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1696 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1698 // PEI Core found, now find PE32 or TE section
1700 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1701 if (Status
== EFI_NOT_FOUND
) {
1702 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1705 if (EFI_ERROR (Status
)) {
1706 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1710 Status
= GetPe32Info (
1711 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1717 if (EFI_ERROR (Status
)) {
1718 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1722 // Physical address is FV base + offset of PE32 + offset of the entry point
1724 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1725 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1726 PeiCorePhysicalAddress
+= EntryPoint
;
1727 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1729 if (MachineType
== EFI_IMAGE_MACHINE_ARMT
) {
1730 memset (ResetVector
, 0, sizeof (ResetVector
));
1731 // Address of PEI Core, if we have one
1732 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1736 // Copy to the beginning of the FV
1738 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1746 // Sec Core found, now find PE32 section
1748 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1749 if (Status
== EFI_NOT_FOUND
) {
1750 Status
= GetSectionByType (SecCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1753 if (EFI_ERROR (Status
)) {
1754 Error (NULL
, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
1758 Status
= GetPe32Info (
1759 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1764 if (EFI_ERROR (Status
)) {
1765 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
1769 if (MachineType
!= EFI_IMAGE_MACHINE_ARMT
) {
1771 // If SEC is not ARM we have nothing to do
1777 // Physical address is FV base + offset of PE32 + offset of the entry point
1779 SecCorePhysicalAddress
= FvInfo
->BaseAddress
;
1780 SecCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1781 SecCorePhysicalAddress
+= EntryPoint
;
1782 DebugMsg (NULL
, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress
);
1785 // Find the PEI Core. It may not exist if SEC loads DXE core directly
1787 PeiCorePhysicalAddress
= 0;
1788 Status
= GetFileByType (EFI_FV_FILETYPE_PEI_CORE
, 1, &PeiCoreFile
);
1789 if (!EFI_ERROR (Status
) && PeiCoreFile
!= NULL
) {
1791 // PEI Core found, now find PE32 or TE section
1793 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_PE32
, 1, &Pe32Section
);
1794 if (Status
== EFI_NOT_FOUND
) {
1795 Status
= GetSectionByType (PeiCoreFile
, EFI_SECTION_TE
, 1, &Pe32Section
);
1798 if (EFI_ERROR (Status
)) {
1799 Error (NULL
, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");
1803 Status
= GetPe32Info (
1804 (VOID
*) ((UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
)),
1810 if (EFI_ERROR (Status
)) {
1811 Error (NULL
, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");
1815 // Physical address is FV base + offset of PE32 + offset of the entry point
1817 PeiCorePhysicalAddress
= FvInfo
->BaseAddress
;
1818 PeiCorePhysicalAddress
+= (UINTN
) Pe32Section
.Pe32Section
+ sizeof (EFI_SECTION_PE32
) - (UINTN
) FvImage
->FileImage
;
1819 PeiCorePhysicalAddress
+= EntryPoint
;
1820 DebugMsg (NULL
, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress
);
1824 // B SecEntryPoint - signed_immed_24 part +/-32MB offset
1825 // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
1826 ResetVector
[0] = (INT32
)(SecCorePhysicalAddress
- FvInfo
->BaseAddress
- 8) >> 2;
1828 if (ResetVector
[0] > 0x00FFFFFF) {
1829 Error (NULL
, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
1833 // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint
1834 ResetVector
[0] |= 0xEB000000;
1837 // Address of PEI Core, if we have one
1838 ResetVector
[1] = (UINT32
)PeiCorePhysicalAddress
;
1840 // SWI handler movs pc,lr. Just in case a debugger uses SWI
1841 ResetVector
[2] = 0xE1B0F07E;
1843 // Place holder to support a common interrupt handler from ROM.
1844 // Currently not suppprted. For this to be used the reset vector would not be in this FV
1845 // and the exception vectors would be hard coded in the ROM and just through this address
1846 // to find a common handler in the a module in the FV.
1850 // Copy to the beginning of the FV
1852 memcpy ((UINT8
*) ((UINTN
) FvImage
->FileImage
), ResetVector
, sizeof (ResetVector
));
1854 DebugMsg (NULL
, 0, 9, "Update Reset vector in FV Header", NULL
);
1862 OUT UINT32
*EntryPoint
,
1863 OUT UINT32
*BaseOfCode
,
1864 OUT UINT16
*MachineType
1868 Routine Description:
1870 Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
1871 See EfiImage.h for machine types. The entry point offset is from the beginning
1872 of the PE32 buffer passed in.
1876 Pe32 Beginning of the PE32.
1877 EntryPoint Offset from the beginning of the PE32 to the image entry point.
1878 BaseOfCode Base address of code.
1879 MachineType Magic number for the machine type.
1883 EFI_SUCCESS Function completed successfully.
1884 EFI_ABORTED Error encountered.
1885 EFI_INVALID_PARAMETER A required parameter was NULL.
1886 EFI_UNSUPPORTED The operation is unsupported.
1890 EFI_IMAGE_DOS_HEADER
*DosHeader
;
1891 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
1892 EFI_TE_IMAGE_HEADER
*TeHeader
;
1895 // Verify input parameters
1898 return EFI_INVALID_PARAMETER
;
1902 // First check whether it is one TE Image.
1904 TeHeader
= (EFI_TE_IMAGE_HEADER
*) Pe32
;
1905 if (TeHeader
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
1907 // By TeImage Header to get output
1909 *EntryPoint
= TeHeader
->AddressOfEntryPoint
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1910 *BaseOfCode
= TeHeader
->BaseOfCode
+ sizeof (EFI_TE_IMAGE_HEADER
) - TeHeader
->StrippedSize
;
1911 *MachineType
= TeHeader
->Machine
;
1915 // Then check whether
1916 // First is the DOS header
1918 DosHeader
= (EFI_IMAGE_DOS_HEADER
*) Pe32
;
1921 // Verify DOS header is expected
1923 if (DosHeader
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
1924 Error (NULL
, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader
->e_magic
);
1925 return EFI_UNSUPPORTED
;
1928 // Immediately following is the NT header.
1930 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*) ((UINTN
) Pe32
+ DosHeader
->e_lfanew
);
1933 // Verify NT header is expected
1935 if (ImgHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1936 Error (NULL
, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr
->Pe32
.Signature
);
1937 return EFI_UNSUPPORTED
;
1942 *EntryPoint
= ImgHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
;
1943 *BaseOfCode
= ImgHdr
->Pe32
.OptionalHeader
.BaseOfCode
;
1944 *MachineType
= ImgHdr
->Pe32
.FileHeader
.Machine
;
1948 // Verify machine type is supported
1950 if (*MachineType
!= EFI_IMAGE_MACHINE_IA32
&& *MachineType
!= EFI_IMAGE_MACHINE_IA64
&& *MachineType
!= EFI_IMAGE_MACHINE_X64
&& *MachineType
!= EFI_IMAGE_MACHINE_EBC
&&
1951 *MachineType
!= EFI_IMAGE_MACHINE_ARMT
) {
1952 Error (NULL
, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
1953 return EFI_UNSUPPORTED
;
1961 IN CHAR8
*InfFileImage
,
1962 IN UINTN InfFileSize
,
1963 IN CHAR8
*FvFileName
,
1964 IN CHAR8
*MapFileName
1968 Routine Description:
1970 This is the main function which will be called from application.
1974 InfFileImage Buffer containing the INF file contents.
1975 InfFileSize Size of the contents of the InfFileImage buffer.
1976 FvFileName Requested name for the FV file.
1977 MapFileName Fv map file to log fv driver information.
1981 EFI_SUCCESS Function completed successfully.
1982 EFI_OUT_OF_RESOURCES Could not allocate required resources.
1983 EFI_ABORTED Error encountered.
1984 EFI_INVALID_PARAMETER A required parameter was NULL.
1989 MEMORY_FILE InfMemoryFile
;
1990 MEMORY_FILE FvImageMemoryFile
;
1992 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
1993 EFI_FFS_FILE_HEADER
*VtfFileImage
;
1994 UINT8
*FvBufferHeader
; // to make sure fvimage header 8 type alignment.
1998 CHAR8 FvMapName
[_MAX_PATH
];
2000 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
2001 FILE *FvExtHeaderFile
;
2003 CHAR8 FvReportName
[_MAX_PATH
];
2006 FvBufferHeader
= NULL
;
2009 FvReportFile
= NULL
;
2011 if (InfFileImage
!= NULL
) {
2013 // Initialize file structures
2015 InfMemoryFile
.FileImage
= InfFileImage
;
2016 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
2017 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
2020 // Parse the FV inf file for header information
2022 Status
= ParseFvInf (&InfMemoryFile
, &mFvDataInfo
);
2023 if (EFI_ERROR (Status
)) {
2024 Error (NULL
, 0, 0003, "Error parsing file", "the input FV INF file.");
2030 // Update the file name return values
2032 if (FvFileName
== NULL
&& mFvDataInfo
.FvName
[0] != '\0') {
2033 FvFileName
= mFvDataInfo
.FvName
;
2036 if (FvFileName
== NULL
) {
2037 Error (NULL
, 0, 1001, "Missing option", "Output file name");
2041 if (mFvDataInfo
.FvBlocks
[0].Length
== 0) {
2042 Error (NULL
, 0, 1001, "Missing required argument", "Block Size");
2047 // Debug message Fv File System Guid
2049 if (mFvDataInfo
.FvFileSystemGuidSet
) {
2050 DebugMsg (NULL
, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2051 (unsigned) mFvDataInfo
.FvFileSystemGuid
.Data1
,
2052 mFvDataInfo
.FvFileSystemGuid
.Data2
,
2053 mFvDataInfo
.FvFileSystemGuid
.Data3
,
2054 mFvDataInfo
.FvFileSystemGuid
.Data4
[0],
2055 mFvDataInfo
.FvFileSystemGuid
.Data4
[1],
2056 mFvDataInfo
.FvFileSystemGuid
.Data4
[2],
2057 mFvDataInfo
.FvFileSystemGuid
.Data4
[3],
2058 mFvDataInfo
.FvFileSystemGuid
.Data4
[4],
2059 mFvDataInfo
.FvFileSystemGuid
.Data4
[5],
2060 mFvDataInfo
.FvFileSystemGuid
.Data4
[6],
2061 mFvDataInfo
.FvFileSystemGuid
.Data4
[7]);
2065 // Add PI FV extension header
2068 FvExtHeaderFile
= NULL
;
2069 if (mFvDataInfo
.FvExtHeaderFile
[0] != 0) {
2071 // Open the FV Extension Header file
2073 FvExtHeaderFile
= fopen (mFvDataInfo
.FvExtHeaderFile
, "rb");
2076 // Get the file size
2078 FileSize
= _filelength (fileno (FvExtHeaderFile
));
2081 // Allocate a buffer for the FV Extension Header
2083 FvExtHeader
= malloc(FileSize
);
2084 if (FvExtHeader
== NULL
) {
2085 fclose (FvExtHeaderFile
);
2086 return EFI_OUT_OF_RESOURCES
;
2090 // Read the FV Extension Header
2092 fread (FvExtHeader
, sizeof (UINT8
), FileSize
, FvExtHeaderFile
);
2093 fclose (FvExtHeaderFile
);
2096 // See if there is an override for the FV Name GUID
2098 if (mFvDataInfo
.FvNameGuidSet
) {
2099 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2101 memcpy (&mFvDataInfo
.FvNameGuid
, &FvExtHeader
->FvName
, sizeof (EFI_GUID
));
2102 mFvDataInfo
.FvNameGuidSet
= TRUE
;
2103 } else if (mFvDataInfo
.FvNameGuidSet
) {
2105 // Allocate a buffer for the FV Extension Header
2107 FvExtHeader
= malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
));
2108 if (FvExtHeader
== NULL
) {
2109 return EFI_OUT_OF_RESOURCES
;
2111 memcpy (&FvExtHeader
->FvName
, &mFvDataInfo
.FvNameGuid
, sizeof (EFI_GUID
));
2112 FvExtHeader
->ExtHeaderSize
= sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2116 // Debug message Fv Name Guid
2118 if (mFvDataInfo
.FvNameGuidSet
) {
2119 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
2120 (unsigned) mFvDataInfo
.FvNameGuid
.Data1
,
2121 mFvDataInfo
.FvNameGuid
.Data2
,
2122 mFvDataInfo
.FvNameGuid
.Data3
,
2123 mFvDataInfo
.FvNameGuid
.Data4
[0],
2124 mFvDataInfo
.FvNameGuid
.Data4
[1],
2125 mFvDataInfo
.FvNameGuid
.Data4
[2],
2126 mFvDataInfo
.FvNameGuid
.Data4
[3],
2127 mFvDataInfo
.FvNameGuid
.Data4
[4],
2128 mFvDataInfo
.FvNameGuid
.Data4
[5],
2129 mFvDataInfo
.FvNameGuid
.Data4
[6],
2130 mFvDataInfo
.FvNameGuid
.Data4
[7]);
2133 if (CompareGuid (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
) == 0) {
2134 mFvDataInfo
.IsPiFvImage
= TRUE
;
2138 // FvMap file to log the function address of all modules in one Fvimage
2140 if (MapFileName
!= NULL
) {
2141 strcpy (FvMapName
, MapFileName
);
2143 strcpy (FvMapName
, FvFileName
);
2144 strcat (FvMapName
, ".map");
2146 VerboseMsg ("FV Map file name is %s", FvMapName
);
2149 // FvReport file to log the FV information in one Fvimage
2151 strcpy (FvReportName
, FvFileName
);
2152 strcat (FvReportName
, ".txt");
2155 // Calculate the FV size and Update Fv Size based on the actual FFS files.
2156 // And Update mFvDataInfo data.
2158 Status
= CalculateFvSize (&mFvDataInfo
);
2159 if (EFI_ERROR (Status
)) {
2162 VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo
.Size
);
2165 // support fv image and empty fv image
2167 FvImageSize
= mFvDataInfo
.Size
;
2170 // Allocate the FV, assure FvImage Header 8 byte alignment
2172 FvBufferHeader
= malloc (FvImageSize
+ sizeof (UINT64
));
2173 if (FvBufferHeader
== NULL
) {
2174 return EFI_OUT_OF_RESOURCES
;
2176 FvImage
= (UINT8
*) (((UINTN
) FvBufferHeader
+ 7) & ~7);
2179 // Initialize the FV to the erase polarity
2181 if (mFvDataInfo
.FvAttributes
== 0) {
2183 // Set Default Fv Attribute
2185 mFvDataInfo
.FvAttributes
= FV_DEFAULT_ATTRIBUTE
;
2187 if (mFvDataInfo
.FvAttributes
& EFI_FVB2_ERASE_POLARITY
) {
2188 memset (FvImage
, -1, FvImageSize
);
2190 memset (FvImage
, 0, FvImageSize
);
2194 // Initialize FV header
2196 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvImage
;
2199 // Initialize the zero vector to all zeros.
2201 memset (FvHeader
->ZeroVector
, 0, 16);
2204 // Copy the Fv file system GUID
2206 memcpy (&FvHeader
->FileSystemGuid
, &mFvDataInfo
.FvFileSystemGuid
, sizeof (EFI_GUID
));
2208 FvHeader
->FvLength
= FvImageSize
;
2209 FvHeader
->Signature
= EFI_FVH_SIGNATURE
;
2210 FvHeader
->Attributes
= mFvDataInfo
.FvAttributes
;
2211 FvHeader
->Revision
= EFI_FVH_REVISION
;
2212 FvHeader
->ExtHeaderOffset
= 0;
2213 FvHeader
->Reserved
[0] = 0;
2216 // Copy firmware block map
2218 for (Index
= 0; mFvDataInfo
.FvBlocks
[Index
].Length
!= 0; Index
++) {
2219 FvHeader
->BlockMap
[Index
].NumBlocks
= mFvDataInfo
.FvBlocks
[Index
].NumBlocks
;
2220 FvHeader
->BlockMap
[Index
].Length
= mFvDataInfo
.FvBlocks
[Index
].Length
;
2224 // Add block map terminator
2226 FvHeader
->BlockMap
[Index
].NumBlocks
= 0;
2227 FvHeader
->BlockMap
[Index
].Length
= 0;
2230 // Complete the header
2232 FvHeader
->HeaderLength
= (UINT16
) (((UINTN
) &(FvHeader
->BlockMap
[Index
+ 1])) - (UINTN
) FvImage
);
2233 FvHeader
->Checksum
= 0;
2234 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2237 // If there is no FFS file, generate one empty FV
2239 if (mFvDataInfo
.FvFiles
[0][0] == 0 && !mFvDataInfo
.FvNameGuidSet
) {
2244 // Initialize our "file" view of the buffer
2246 FvImageMemoryFile
.FileImage
= (CHAR8
*)FvImage
;
2247 FvImageMemoryFile
.CurrentFilePointer
= (CHAR8
*)FvImage
+ FvHeader
->HeaderLength
;
2248 FvImageMemoryFile
.Eof
= (CHAR8
*)FvImage
+ FvImageSize
;
2251 // Initialize the FV library.
2253 InitializeFvLib (FvImageMemoryFile
.FileImage
, FvImageSize
);
2256 // Initialize the VTF file address.
2258 VtfFileImage
= (EFI_FFS_FILE_HEADER
*) FvImageMemoryFile
.Eof
;
2263 FvMapFile
= fopen (FvMapName
, "w");
2264 if (FvMapFile
== NULL
) {
2265 Error (NULL
, 0, 0001, "Error opening file", FvMapName
);
2270 // Open FvReport file
2272 FvReportFile
= fopen(FvReportName
, "w");
2273 if (FvReportFile
== NULL
) {
2274 Error (NULL
, 0, 0001, "Error opening file", FvReportName
);
2278 // record FV size information into FvMap file.
2280 if (mFvTotalSize
!= 0) {
2281 fprintf (FvMapFile
, EFI_FV_TOTAL_SIZE_STRING
);
2282 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTotalSize
);
2284 if (mFvTakenSize
!= 0) {
2285 fprintf (FvMapFile
, EFI_FV_TAKEN_SIZE_STRING
);
2286 fprintf (FvMapFile
, " = 0x%x\n", (unsigned) mFvTakenSize
);
2288 if (mFvTotalSize
!= 0 && mFvTakenSize
!= 0) {
2289 fprintf (FvMapFile
, EFI_FV_SPACE_SIZE_STRING
);
2290 fprintf (FvMapFile
, " = 0x%x\n\n", (unsigned) (mFvTotalSize
- mFvTakenSize
));
2294 // record FV size information to FvReportFile.
2296 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
2297 fprintf (FvReportFile
, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
2300 // Add PI FV extension header
2302 if (FvExtHeader
!= NULL
) {
2304 // Add FV Extended Header contents to the FV as a PAD file
2306 AddPadFile (&FvImageMemoryFile
, 4, VtfFileImage
, FvExtHeader
);
2309 // Fv Extension header change update Fv Header Check sum
2311 FvHeader
->Checksum
= 0;
2312 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2318 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != 0; Index
++) {
2322 Status
= AddFile (&FvImageMemoryFile
, &mFvDataInfo
, Index
, &VtfFileImage
, FvMapFile
, FvReportFile
);
2325 // Exit if error detected while adding the file
2327 if (EFI_ERROR (Status
)) {
2333 // If there is a VTF file, some special actions need to occur.
2335 if ((UINTN
) VtfFileImage
!= (UINTN
) FvImageMemoryFile
.Eof
) {
2337 // Pad from the end of the last file to the beginning of the VTF file.
2338 // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
2340 Status
= PadFvImage (&FvImageMemoryFile
, VtfFileImage
);
2341 if (EFI_ERROR (Status
)) {
2342 Error (NULL
, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
2347 // Update reset vector (SALE_ENTRY for IPF)
2348 // Now for IA32 and IA64 platform, the fv which has bsf file must have the
2349 // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the
2350 // reset vector. If the PEI Core is found, the VTF file will probably get
2351 // corrupted by updating the entry point.
2353 if ((mFvDataInfo
.BaseAddress
+ mFvDataInfo
.Size
) == FV_IMAGES_TOP_ADDRESS
) {
2354 Status
= UpdateResetVector (&FvImageMemoryFile
, &mFvDataInfo
, VtfFileImage
);
2355 if (EFI_ERROR(Status
)) {
2356 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2359 DebugMsg (NULL
, 0, 9, "Update Reset vector in VTF file", NULL
);
2365 Status
= UpdateArmResetVectorIfNeeded (&FvImageMemoryFile
, &mFvDataInfo
);
2366 if (EFI_ERROR (Status
)) {
2367 Error (NULL
, 0, 3000, "Invalid", "Could not update the reset vector.");
2372 // Update Checksum for FvHeader
2374 FvHeader
->Checksum
= 0;
2375 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2379 // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
2381 if ((((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16)) < MaxFfsAlignment
) {
2382 FvHeader
->Attributes
= ((MaxFfsAlignment
<< 16) | (FvHeader
->Attributes
& 0xFFFF));
2384 // Update Checksum for FvHeader
2386 FvHeader
->Checksum
= 0;
2387 FvHeader
->Checksum
= CalculateChecksum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
2394 FvFile
= fopen (FvFileName
, "wb");
2395 if (FvFile
== NULL
) {
2396 Error (NULL
, 0, 0001, "Error opening file", FvFileName
);
2397 Status
= EFI_ABORTED
;
2401 if (fwrite (FvImage
, 1, FvImageSize
, FvFile
) != FvImageSize
) {
2402 Error (NULL
, 0, 0002, "Error writing file", FvFileName
);
2403 Status
= EFI_ABORTED
;
2408 if (FvBufferHeader
!= NULL
) {
2409 free (FvBufferHeader
);
2412 if (FvExtHeader
!= NULL
) {
2416 if (FvFile
!= NULL
) {
2421 if (FvMapFile
!= NULL
) {
2426 if (FvReportFile
!= NULL
) {
2427 fflush (FvReportFile
);
2428 fclose (FvReportFile
);
2434 UpdatePeiCoreEntryInFit (
2435 IN FIT_TABLE
*FitTablePtr
,
2436 IN UINT64 PeiCorePhysicalAddress
2440 Routine Description:
2442 This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
2447 FitTablePtr - The pointer of FIT_TABLE.
2448 PeiCorePhysicalAddress - The address of Pei Core entry.
2452 EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
2453 EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
2457 FIT_TABLE
*TmpFitPtr
;
2459 UINTN NumFitComponents
;
2461 TmpFitPtr
= FitTablePtr
;
2462 NumFitComponents
= TmpFitPtr
->CompSize
;
2464 for (Index
= 0; Index
< NumFitComponents
; Index
++) {
2465 if ((TmpFitPtr
->CvAndType
& FIT_TYPE_MASK
) == COMP_TYPE_FIT_PEICORE
) {
2466 TmpFitPtr
->CompAddress
= PeiCorePhysicalAddress
;
2473 return EFI_NOT_FOUND
;
2478 IN FIT_TABLE
*FitTablePtr
2482 Routine Description:
2484 This function is used to update the checksum for FIT.
2489 FitTablePtr - The pointer of FIT_TABLE.
2497 if ((FitTablePtr
->CvAndType
& CHECKSUM_BIT_MASK
) >> 7) {
2498 FitTablePtr
->CheckSum
= 0;
2499 FitTablePtr
->CheckSum
= CalculateChecksum8 ((UINT8
*) FitTablePtr
, FitTablePtr
->CompSize
* 16);
2508 Routine Description:
2509 Calculate the FV size and Update Fv Size based on the actual FFS files.
2510 And Update FvInfo data.
2513 FvInfoPtr - The pointer to FV_INFO structure.
2516 EFI_ABORTED - Ffs Image Error
2517 EFI_SUCCESS - Successfully update FvSize
2520 UINTN CurrentOffset
;
2524 UINTN FvExtendHeaderSize
;
2525 UINT32 FfsAlignment
;
2526 EFI_FFS_FILE_HEADER FfsHeader
;
2527 BOOLEAN VtfFileFlag
;
2530 FvExtendHeaderSize
= 0;
2532 VtfFileFlag
= FALSE
;
2537 // Compute size for easy access later
2539 FvInfoPtr
->Size
= 0;
2540 for (Index
= 0; FvInfoPtr
->FvBlocks
[Index
].NumBlocks
> 0 && FvInfoPtr
->FvBlocks
[Index
].Length
> 0; Index
++) {
2541 FvInfoPtr
->Size
+= FvInfoPtr
->FvBlocks
[Index
].NumBlocks
* FvInfoPtr
->FvBlocks
[Index
].Length
;
2545 // Caculate the required sizes for all FFS files.
2547 CurrentOffset
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
);
2549 for (Index
= 1;; Index
++) {
2550 CurrentOffset
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
2551 if (FvInfoPtr
->FvBlocks
[Index
].NumBlocks
== 0 || FvInfoPtr
->FvBlocks
[Index
].Length
== 0) {
2557 // Calculate PI extension header
2559 if (mFvDataInfo
.FvExtHeaderFile
[0] != '\0') {
2560 fpin
= fopen (mFvDataInfo
.FvExtHeaderFile
, "rb");
2562 Error (NULL
, 0, 0001, "Error opening file", mFvDataInfo
.FvExtHeaderFile
);
2565 FvExtendHeaderSize
= _filelength (fileno (fpin
));
2567 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + FvExtendHeaderSize
;
2568 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2569 } else if (mFvDataInfo
.FvNameGuidSet
) {
2570 CurrentOffset
+= sizeof (EFI_FFS_FILE_HEADER
) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
);
2571 CurrentOffset
= (CurrentOffset
+ 7) & (~7);
2575 // Accumlate every FFS file size.
2577 for (Index
= 0; FvInfoPtr
->FvFiles
[Index
][0] != 0; Index
++) {
2582 fpin
= fopen (FvInfoPtr
->FvFiles
[Index
], "rb");
2584 Error (NULL
, 0, 0001, "Error opening file", FvInfoPtr
->FvFiles
[Index
]);
2588 // Get the file size
2590 FfsFileSize
= _filelength (fileno (fpin
));
2592 // Read Ffs File header
2594 fread (&FfsHeader
, sizeof (UINT8
), sizeof (EFI_FFS_FILE_HEADER
), fpin
);
2600 if (FvInfoPtr
->IsPiFvImage
) {
2602 // Check whether this ffs file is vtf file
2604 if (IsVtfFile (&FfsHeader
)) {
2607 // One Fv image can't have two vtf files.
2612 VtfFileSize
= FfsFileSize
;
2617 // Get the alignment of FFS file
2619 ReadFfsAlignment (&FfsHeader
, &FfsAlignment
);
2620 FfsAlignment
= 1 << FfsAlignment
;
2624 if (((CurrentOffset
+ sizeof (EFI_FFS_FILE_HEADER
)) % FfsAlignment
) != 0) {
2625 CurrentOffset
= (CurrentOffset
+ sizeof (EFI_FFS_FILE_HEADER
) * 2 + FfsAlignment
- 1) & ~(FfsAlignment
- 1);
2626 CurrentOffset
-= sizeof (EFI_FFS_FILE_HEADER
);
2631 // Add ffs file size
2633 if (FvInfoPtr
->SizeofFvFiles
[Index
] > FfsFileSize
) {
2634 CurrentOffset
+= FvInfoPtr
->SizeofFvFiles
[Index
];
2636 CurrentOffset
+= FfsFileSize
;
2640 // Make next ffs file start at QWord Boundry
2642 if (FvInfoPtr
->IsPiFvImage
) {
2643 CurrentOffset
= (CurrentOffset
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
2646 CurrentOffset
+= VtfFileSize
;
2647 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
);
2649 if (FvInfoPtr
->Size
== 0) {
2651 // Update FvInfo data
2653 FvInfoPtr
->FvBlocks
[0].NumBlocks
= CurrentOffset
/ FvInfoPtr
->FvBlocks
[0].Length
+ ((CurrentOffset
% FvInfoPtr
->FvBlocks
[0].Length
)?1:0);
2654 FvInfoPtr
->Size
= FvInfoPtr
->FvBlocks
[0].NumBlocks
* FvInfoPtr
->FvBlocks
[0].Length
;
2655 FvInfoPtr
->FvBlocks
[1].NumBlocks
= 0;
2656 FvInfoPtr
->FvBlocks
[1].Length
= 0;
2657 } else if (FvInfoPtr
->Size
< CurrentOffset
) {
2661 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
);
2662 return EFI_INVALID_PARAMETER
;
2666 // Set Fv Size Information
2668 mFvTotalSize
= FvInfoPtr
->Size
;
2669 mFvTakenSize
= CurrentOffset
;
2675 FfsRebaseImageRead (
2676 IN VOID
*FileHandle
,
2677 IN UINTN FileOffset
,
2678 IN OUT UINT32
*ReadSize
,
2683 Routine Description:
2685 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
2689 FileHandle - The handle to the PE/COFF file
2691 FileOffset - The offset, in bytes, into the file to read
2693 ReadSize - The number of bytes to read from the file starting at FileOffset
2695 Buffer - A pointer to the buffer to read the data into.
2699 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
2703 CHAR8
*Destination8
;
2707 Destination8
= Buffer
;
2708 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
2711 *(Destination8
++) = *(Source8
++);
2720 IN EFI_FFS_FILE_HEADER
*FfsFile
,
2725 Routine Description:
2727 This function gets all child FvImages in the input FfsFile, and records
2728 their base address to the parent image.
2731 FvInfo A pointer to FV_INFO struture.
2732 FfsFile A pointer to Ffs file image that may contain FvImage.
2733 XipOffset The offset address to the parent FvImage base.
2737 EFI_SUCCESS Base address of child Fv image is recorded.
2742 EFI_FILE_SECTION_POINTER SubFvSection
;
2743 EFI_FIRMWARE_VOLUME_HEADER
*SubFvImageHeader
;
2744 EFI_PHYSICAL_ADDRESS SubFvBaseAddress
;
2746 for (Index
= 1;; Index
++) {
2750 Status
= GetSectionByType (FfsFile
, EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, Index
, &SubFvSection
);
2751 if (EFI_ERROR (Status
)) {
2754 SubFvImageHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINT8
*) SubFvSection
.FVImageSection
+ sizeof (EFI_FIRMWARE_VOLUME_IMAGE_SECTION
));
2758 SubFvBaseAddress
= FvInfo
->BaseAddress
+ (UINTN
) SubFvImageHeader
- (UINTN
) FfsFile
+ XipOffset
;
2759 mFvBaseAddress
[mFvBaseAddressNumber
++ ] = SubFvBaseAddress
;
2767 IN OUT FV_INFO
*FvInfo
,
2769 IN OUT EFI_FFS_FILE_HEADER
*FfsFile
,
2775 Routine Description:
2777 This function determines if a file is XIP and should be rebased. It will
2778 rebase any PE32 sections found in the file using the base address.
2782 FvInfo A pointer to FV_INFO struture.
2783 FileName Ffs File PathName
2784 FfsFile A pointer to Ffs file image.
2785 XipOffset The offset address to use for rebasing the XIP file image.
2786 FvMapFile FvMapFile to record the function address in one Fvimage
2790 EFI_SUCCESS The image was properly rebased.
2791 EFI_INVALID_PARAMETER An input parameter is invalid.
2792 EFI_ABORTED An error occurred while rebasing the input file image.
2793 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
2794 EFI_NOT_FOUND No compressed sections could be found.
2799 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
2800 PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext
;
2801 EFI_PHYSICAL_ADDRESS XipBase
;
2802 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress
;
2803 EFI_PHYSICAL_ADDRESS
*BaseToUpdate
;
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 BaseToUpdate
= NULL
;
2821 TEImageHeader
= NULL
;
2823 SectionHeader
= NULL
;
2826 PeFileBuffer
= NULL
;
2829 // Don't need to relocate image when BaseAddress is not set.
2831 if (FvInfo
->BaseAddress
== 0) {
2834 XipBase
= FvInfo
->BaseAddress
+ XipOffset
;
2837 // We only process files potentially containing PE32 sections.
2839 switch (FfsFile
->Type
) {
2840 case EFI_FV_FILETYPE_SECURITY_CORE
:
2841 case EFI_FV_FILETYPE_PEI_CORE
:
2842 case EFI_FV_FILETYPE_PEIM
:
2843 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2844 case EFI_FV_FILETYPE_DRIVER
:
2845 case EFI_FV_FILETYPE_DXE_CORE
:
2847 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
2849 // Rebase the inside FvImage.
2851 GetChildFvFromFfs (FvInfo
, FfsFile
, XipOffset
);
2854 // Search PE/TE section in FV sectin.
2861 // Rebase each PE32 section
2863 Status
= EFI_SUCCESS
;
2864 for (Index
= 1;; Index
++) {
2868 NewPe32BaseAddress
= 0;
2873 Status
= GetSectionByType (FfsFile
, EFI_SECTION_PE32
, Index
, &CurrentPe32Section
);
2874 if (EFI_ERROR (Status
)) {
2879 // Initialize context
2881 memset (&ImageContext
, 0, sizeof (ImageContext
));
2882 ImageContext
.Handle
= (VOID
*) ((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
));
2883 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
2884 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
2885 if (EFI_ERROR (Status
)) {
2886 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
2890 if (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) {
2895 // Keep Image Context for PE image in FV
2897 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
2900 // Get File PdbPointer
2902 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
2905 // Get PeHeader pointer
2907 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)((UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) + ImageContext
.PeCoffHeaderOffset
);
2910 // Calculate the PE32 base address, based on file type
2912 switch (FfsFile
->Type
) {
2913 case EFI_FV_FILETYPE_SECURITY_CORE
:
2914 case EFI_FV_FILETYPE_PEI_CORE
:
2915 case EFI_FV_FILETYPE_PEIM
:
2916 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
2918 // Check if section-alignment and file-alignment match or not
2920 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
2922 // Xip module has the same section alignment and file alignment.
2924 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
2928 // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
2930 if (ImageContext
.RelocationsStripped
) {
2932 // Construct the original efi file Name
2934 strcpy (PeFileName
, FileName
);
2935 Cptr
= PeFileName
+ strlen (PeFileName
);
2936 while (*Cptr
!= '.') {
2940 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
2948 PeFile
= fopen (PeFileName
, "rb");
2949 if (PeFile
== NULL
) {
2950 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
2951 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
2952 //return EFI_ABORTED;
2956 // Get the file size
2958 PeFileSize
= _filelength (fileno (PeFile
));
2959 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
2960 if (PeFileBuffer
== NULL
) {
2961 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
2962 return EFI_OUT_OF_RESOURCES
;
2967 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
2973 // Handle pointer to the original efi image.
2975 ImageContext
.Handle
= PeFileBuffer
;
2976 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
2977 if (EFI_ERROR (Status
)) {
2978 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
2981 ImageContext
.RelocationsStripped
= FALSE
;
2984 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) - (UINTN
)FfsFile
;
2985 BaseToUpdate
= &XipBase
;
2988 case EFI_FV_FILETYPE_DRIVER
:
2989 case EFI_FV_FILETYPE_DXE_CORE
:
2991 // Check if section-alignment and file-alignment match or not
2993 if ((ImgHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= ImgHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
2995 // Xip module has the same section alignment and file alignment.
2997 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName
);
3000 NewPe32BaseAddress
= XipBase
+ (UINTN
) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_PE32_SECTION
) - (UINTN
)FfsFile
;
3001 BaseToUpdate
= &XipBase
;
3006 // Not supported file type
3012 // Relocation doesn't exist
3014 if (ImageContext
.RelocationsStripped
) {
3015 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3020 // Relocation exist and rebase
3023 // Load and Relocate Image Data
3025 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3026 if (MemoryImagePointer
== NULL
) {
3027 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3028 return EFI_OUT_OF_RESOURCES
;
3030 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3031 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3033 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3034 if (EFI_ERROR (Status
)) {
3035 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3036 free ((VOID
*) MemoryImagePointer
);
3040 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3041 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3042 if (EFI_ERROR (Status
)) {
3043 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
3044 free ((VOID
*) MemoryImagePointer
);
3049 // Copy Relocated data to raw image file.
3051 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
3054 sizeof (EFI_IMAGE_FILE_HEADER
) +
3055 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
3058 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
3060 (UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
) + SectionHeader
->PointerToRawData
,
3061 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3062 SectionHeader
->SizeOfRawData
3066 free ((VOID
*) MemoryImagePointer
);
3067 MemoryImagePointer
= NULL
;
3068 if (PeFileBuffer
!= NULL
) {
3069 free (PeFileBuffer
);
3070 PeFileBuffer
= NULL
;
3074 // Update Image Base Address
3076 if (ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
3077 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
3078 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
3079 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
3081 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
3082 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
3089 // Now update file checksum
3091 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3092 SavedState
= FfsFile
->State
;
3093 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3095 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3096 (UINT8
*) (FfsFile
+ 1),
3097 GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
3099 FfsFile
->State
= SavedState
;
3103 // Get this module function address from ModulePeMapFile and add them into FvMap file
3107 // Default use FileName as map file path
3109 if (PdbPointer
== NULL
) {
3110 PdbPointer
= FileName
;
3113 WriteMapFile (FvMapFile
, PdbPointer
, FfsFile
, NewPe32BaseAddress
, &OrigImageContext
);
3116 if (FfsFile
->Type
!= EFI_FV_FILETYPE_SECURITY_CORE
&&
3117 FfsFile
->Type
!= EFI_FV_FILETYPE_PEI_CORE
&&
3118 FfsFile
->Type
!= EFI_FV_FILETYPE_PEIM
&&
3119 FfsFile
->Type
!= EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
&&
3120 FfsFile
->Type
!= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
3123 // Only Peim code may have a TE section
3129 // Now process TE sections
3131 for (Index
= 1;; Index
++) {
3132 NewPe32BaseAddress
= 0;
3137 Status
= GetSectionByType (FfsFile
, EFI_SECTION_TE
, Index
, &CurrentPe32Section
);
3138 if (EFI_ERROR (Status
)) {
3143 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
3146 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) ((UINT8
*) CurrentPe32Section
.Pe32Section
+ sizeof (EFI_COMMON_SECTION_HEADER
));
3149 // Initialize context, load image info.
3151 memset (&ImageContext
, 0, sizeof (ImageContext
));
3152 ImageContext
.Handle
= (VOID
*) TEImageHeader
;
3153 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) FfsRebaseImageRead
;
3154 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3155 if (EFI_ERROR (Status
)) {
3156 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3160 if (ImageContext
.Machine
== EFI_IMAGE_MACHINE_ARMT
) {
3165 // Keep Image Context for TE image in FV
3167 memcpy (&OrigImageContext
, &ImageContext
, sizeof (ImageContext
));
3170 // Get File PdbPointer
3172 PdbPointer
= PeCoffLoaderGetPdbPointer (ImageContext
.Handle
);
3175 // Set new rebased address.
3177 NewPe32BaseAddress
= XipBase
+ (UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) \
3178 - TEImageHeader
->StrippedSize
- (UINTN
) FfsFile
;
3181 // if reloc is stripped, try to get the original efi image to get reloc info.
3183 if (ImageContext
.RelocationsStripped
) {
3185 // Construct the original efi file name
3187 strcpy (PeFileName
, FileName
);
3188 Cptr
= PeFileName
+ strlen (PeFileName
);
3189 while (*Cptr
!= '.') {
3194 Error (NULL
, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName
);
3203 PeFile
= fopen (PeFileName
, "rb");
3204 if (PeFile
== NULL
) {
3205 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3206 //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
3207 //return EFI_ABORTED;
3210 // Get the file size
3212 PeFileSize
= _filelength (fileno (PeFile
));
3213 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
3214 if (PeFileBuffer
== NULL
) {
3215 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3216 return EFI_OUT_OF_RESOURCES
;
3221 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, PeFile
);
3227 // Append reloc section into TeImage
3229 ImageContext
.Handle
= PeFileBuffer
;
3230 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
3231 if (EFI_ERROR (Status
)) {
3232 Error (NULL
, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName
, (int) Status
);
3235 ImageContext
.RelocationsStripped
= FALSE
;
3239 // Relocation doesn't exist
3241 if (ImageContext
.RelocationsStripped
) {
3242 Warning (NULL
, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName
);
3247 // Relocation exist and rebase
3250 // Load and Relocate Image Data
3252 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3253 if (MemoryImagePointer
== NULL
) {
3254 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
3255 return EFI_OUT_OF_RESOURCES
;
3257 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
3258 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((UINTN
) ImageContext
.SectionAlignment
- 1));
3260 Status
= PeCoffLoaderLoadImage (&ImageContext
);
3261 if (EFI_ERROR (Status
)) {
3262 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
3263 free ((VOID
*) MemoryImagePointer
);
3267 // Reloacate TeImage
3269 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
3270 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
3271 if (EFI_ERROR (Status
)) {
3272 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName
);
3273 free ((VOID
*) MemoryImagePointer
);
3278 // Copy the relocated image into raw image file.
3280 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TEImageHeader
+ 1);
3281 for (Index
= 0; Index
< TEImageHeader
->NumberOfSections
; Index
++, SectionHeader
++) {
3282 if (!ImageContext
.IsTeImage
) {
3284 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3285 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
3286 SectionHeader
->SizeOfRawData
3290 (UINT8
*) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->PointerToRawData
,
3291 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
+ SectionHeader
->VirtualAddress
),
3292 SectionHeader
->SizeOfRawData
3298 // Free the allocated memory resource
3300 free ((VOID
*) MemoryImagePointer
);
3301 MemoryImagePointer
= NULL
;
3302 if (PeFileBuffer
!= NULL
) {
3303 free (PeFileBuffer
);
3304 PeFileBuffer
= NULL
;
3308 // Update Image Base Address
3310 TEImageHeader
->ImageBase
= NewPe32BaseAddress
;
3313 // Now update file checksum
3315 if (FfsFile
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
3316 SavedState
= FfsFile
->State
;
3317 FfsFile
->IntegrityCheck
.Checksum
.File
= 0;
3319 FfsFile
->IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
3320 (UINT8
*)(FfsFile
+ 1),
3321 GetLength (FfsFile
->Size
) - sizeof (EFI_FFS_FILE_HEADER
)
3323 FfsFile
->State
= SavedState
;
3326 // Get this module function address from ModulePeMapFile and add them into FvMap file
3330 // Default use FileName as map file path
3332 if (PdbPointer
== NULL
) {
3333 PdbPointer
= FileName
;
3349 FindApResetVectorPosition (
3350 IN MEMORY_FILE
*FvImage
,
3355 Routine Description:
3357 Find the position in this FvImage to place Ap reset vector.
3361 FvImage Memory file for the FV memory image.
3362 Pointer Pointer to pointer to position.
3366 EFI_NOT_FOUND - No satisfied position is found.
3367 EFI_SUCCESS - The suitable position is return.
3371 EFI_FFS_FILE_HEADER
*PadFile
;
3377 for (Index
= 1; ;Index
++) {
3379 // Find Pad File to add ApResetVector info
3381 Status
= GetFileByType (EFI_FV_FILETYPE_FFS_PAD
, Index
, &PadFile
);
3382 if (EFI_ERROR (Status
) || (PadFile
== NULL
)) {
3384 // No Pad file to be found.
3389 // Get Pad file size.
3391 FileLength
= (*(UINT32
*)(PadFile
->Size
)) & 0x00FFFFFF;
3392 FileLength
= (FileLength
+ EFI_FFS_FILE_HEADER_ALIGNMENT
- 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT
- 1);
3394 // FixPoint must be align on 0x1000 relative to FvImage Header
3396 FixPoint
= (UINT8
*) PadFile
+ sizeof (EFI_FFS_FILE_HEADER
);
3397 FixPoint
= FixPoint
+ 0x1000 - (((UINTN
) FixPoint
- (UINTN
) FvImage
->FileImage
) & 0xFFF);
3399 // FixPoint be larger at the last place of one fv image.
3401 while (((UINTN
) FixPoint
+ SIZEOF_STARTUP_DATA_ARRAY
- (UINTN
) PadFile
) <= FileLength
) {
3406 if ((UINTN
) FixPoint
< ((UINTN
) PadFile
+ sizeof (EFI_FFS_FILE_HEADER
))) {
3408 // No alignment FixPoint in this Pad File.
3413 if ((UINTN
) FvImage
->Eof
- (UINTN
)FixPoint
<= 0x20000) {
3415 // Find the position to place ApResetVector
3417 *Pointer
= FixPoint
;
3422 return EFI_NOT_FOUND
;
3427 IN MEMORY_FILE
*InfFile
,
3428 OUT CAP_INFO
*CapInfo
3432 Routine Description:
3434 This function parses a Cap.INF file and copies info into a CAP_INFO structure.
3438 InfFile Memory file image.
3439 CapInfo Information read from INF file.
3443 EFI_SUCCESS INF file information successfully retrieved.
3444 EFI_ABORTED INF file has an invalid format.
3445 EFI_NOT_FOUND A required string was not found in the INF file.
3448 CHAR8 Value
[_MAX_PATH
];
3450 UINTN Index
, Number
;
3454 // Initialize Cap info
3456 // memset (CapInfo, 0, sizeof (CAP_INFO));
3460 // Read the Capsule Guid
3462 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_GUID_STRING
, 0, Value
);
3463 if (Status
== EFI_SUCCESS
) {
3465 // Get the Capsule Guid
3467 Status
= StringToGuid (Value
, &CapInfo
->CapGuid
);
3468 if (EFI_ERROR (Status
)) {
3469 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3472 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, Value
);
3476 // Read the Capsule Header Size
3478 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_HEADER_SIZE_STRING
, 0, Value
);
3479 if (Status
== EFI_SUCCESS
) {
3480 Status
= AsciiStringToUint64 (Value
, FALSE
, &Value64
);
3481 if (EFI_ERROR (Status
)) {
3482 Error (NULL
, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3485 CapInfo
->HeaderSize
= (UINT32
) Value64
;
3486 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING
, Value
);
3490 // Read the Capsule Flag
3492 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_CAPSULE_FLAGS_STRING
, 0, Value
);
3493 if (Status
== EFI_SUCCESS
) {
3494 if (strstr (Value
, "PopulateSystemTable") != NULL
) {
3495 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
3496 if (strstr (Value
, "InitiateReset") != NULL
) {
3497 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3499 } else if (strstr (Value
, "PersistAcrossReset") != NULL
) {
3500 CapInfo
->Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
3501 if (strstr (Value
, "InitiateReset") != NULL
) {
3502 CapInfo
->Flags
|= CAPSULE_FLAGS_INITIATE_RESET
;
3505 Error (NULL
, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING
);
3508 DebugMsg (NULL
, 0, 9, "Capsule Flag", Value
);
3512 // Read Capsule File name
3514 Status
= FindToken (InfFile
, OPTIONS_SECTION_STRING
, EFI_FILE_NAME_STRING
, 0, Value
);
3515 if (Status
== EFI_SUCCESS
) {
3517 // Get output file name
3519 strcpy (CapInfo
->CapName
, Value
);
3523 // Read the Capsule FileImage
3526 for (Index
= 0; Index
< MAX_NUMBER_OF_FILES_IN_CAP
; Index
++) {
3527 if (CapInfo
->CapFiles
[Index
][0] != '\0') {
3531 // Read the capsule file name
3533 Status
= FindToken (InfFile
, FILES_SECTION_STRING
, EFI_FILE_NAME_STRING
, Number
++, Value
);
3535 if (Status
== EFI_SUCCESS
) {
3539 strcpy (CapInfo
->CapFiles
[Index
], Value
);
3540 DebugMsg (NULL
, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index
, CapInfo
->CapFiles
[Index
]);
3547 Warning (NULL
, 0, 0, "Capsule components are not specified.", NULL
);
3555 IN CHAR8
*InfFileImage
,
3556 IN UINTN InfFileSize
,
3557 IN CHAR8
*CapFileName
3561 Routine Description:
3563 This is the main function which will be called from application to create UEFI Capsule image.
3567 InfFileImage Buffer containing the INF file contents.
3568 InfFileSize Size of the contents of the InfFileImage buffer.
3569 CapFileName Requested name for the Cap file.
3573 EFI_SUCCESS Function completed successfully.
3574 EFI_OUT_OF_RESOURCES Could not allocate required resources.
3575 EFI_ABORTED Error encountered.
3576 EFI_INVALID_PARAMETER A required parameter was NULL.
3582 EFI_CAPSULE_HEADER
*CapsuleHeader
;
3583 MEMORY_FILE InfMemoryFile
;
3589 if (InfFileImage
!= NULL
) {
3591 // Initialize file structures
3593 InfMemoryFile
.FileImage
= InfFileImage
;
3594 InfMemoryFile
.CurrentFilePointer
= InfFileImage
;
3595 InfMemoryFile
.Eof
= InfFileImage
+ InfFileSize
;
3598 // Parse the Cap inf file for header information
3600 Status
= ParseCapInf (&InfMemoryFile
, &mCapDataInfo
);
3601 if (Status
!= EFI_SUCCESS
) {
3606 if (mCapDataInfo
.HeaderSize
== 0) {
3608 // make header size align 16 bytes.
3610 mCapDataInfo
.HeaderSize
= sizeof (EFI_CAPSULE_HEADER
);
3611 mCapDataInfo
.HeaderSize
= (mCapDataInfo
.HeaderSize
+ 0xF) & ~0xF;
3614 if (mCapDataInfo
.HeaderSize
< sizeof (EFI_CAPSULE_HEADER
)) {
3615 Error (NULL
, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
3616 return EFI_INVALID_PARAMETER
;
3619 if (CapFileName
== NULL
&& mCapDataInfo
.CapName
[0] != '\0') {
3620 CapFileName
= mCapDataInfo
.CapName
;
3623 if (CapFileName
== NULL
) {
3624 Error (NULL
, 0, 2001, "Missing required argument", "Output Capsule file name");
3625 return EFI_INVALID_PARAMETER
;
3629 // Set Default Capsule Guid value
3631 if (CompareGuid (&mCapDataInfo
.CapGuid
, &mZeroGuid
) == 0) {
3632 memcpy (&mCapDataInfo
.CapGuid
, &mDefaultCapsuleGuid
, sizeof (EFI_GUID
));
3635 // Calculate the size of capsule image.
3639 CapSize
= mCapDataInfo
.HeaderSize
;
3640 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3641 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3643 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3646 FileSize
= _filelength (fileno (fpin
));
3647 CapSize
+= FileSize
;
3653 // Allocate buffer for capsule image.
3655 CapBuffer
= (UINT8
*) malloc (CapSize
);
3656 if (CapBuffer
== NULL
) {
3657 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
3658 return EFI_OUT_OF_RESOURCES
;
3662 // Initialize the capsule header to zero
3664 memset (CapBuffer
, 0, mCapDataInfo
.HeaderSize
);
3667 // create capsule header and get capsule body
3669 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapBuffer
;
3670 memcpy (&CapsuleHeader
->CapsuleGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
3671 CapsuleHeader
->HeaderSize
= mCapDataInfo
.HeaderSize
;
3672 CapsuleHeader
->Flags
= mCapDataInfo
.Flags
;
3673 CapsuleHeader
->CapsuleImageSize
= CapSize
;
3677 CapSize
= CapsuleHeader
->HeaderSize
;
3678 while (mCapDataInfo
.CapFiles
[Index
][0] != '\0') {
3679 fpin
= fopen (mCapDataInfo
.CapFiles
[Index
], "rb");
3681 Error (NULL
, 0, 0001, "Error opening file", mCapDataInfo
.CapFiles
[Index
]);
3685 FileSize
= _filelength (fileno (fpin
));
3686 fread (CapBuffer
+ CapSize
, 1, FileSize
, fpin
);
3689 CapSize
+= FileSize
;
3693 // write capsule data into the output file
3695 fpout
= fopen (CapFileName
, "wb");
3696 if (fpout
== NULL
) {
3697 Error (NULL
, 0, 0001, "Error opening file", CapFileName
);
3702 fwrite (CapBuffer
, 1, CapSize
, fpout
);
3705 VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize
);