2 Pei Core Firmware File System service routines.
4 Copyright (c) 2006 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList
= {
18 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
19 &gEfiPeiFirmwareVolumeInfoPpiGuid
,
20 FirmwareVolmeInfoPpiNotifyCallback
24 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
25 ((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1)))
28 Returns the file state set by the highest zero bit in the State field
30 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
31 in the Attributes field.
32 @param FfsHeader Pointer to FFS File Header.
34 @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit
35 in the header State field.
39 IN UINT8 ErasePolarity
,
40 IN EFI_FFS_FILE_HEADER
*FfsHeader
43 EFI_FFS_FILE_STATE FileState
;
44 EFI_FFS_FILE_STATE HighestBit
;
46 FileState
= FfsHeader
->State
;
48 if (ErasePolarity
!= 0) {
49 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
53 // Get file state set by its highest none zero bit.
56 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
64 Calculates the checksum of the header of a file.
66 @param FileHeader Pointer to FFS File Header.
68 @return Checksum of the header.
69 Zero means the header is good.
70 Non-zero means the header is bad.
73 CalculateHeaderChecksum (
74 IN EFI_FFS_FILE_HEADER
*FileHeader
77 EFI_FFS_FILE_HEADER TestFileHeader
;
79 CopyMem (&TestFileHeader
, FileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
81 // Ingore State and File field in FFS header.
83 TestFileHeader
.State
= 0;
84 TestFileHeader
.IntegrityCheck
.Checksum
.File
= 0;
86 return CalculateSum8 ((CONST UINT8
*) &TestFileHeader
, sizeof (EFI_FFS_FILE_HEADER
));
90 Find FV handler according some FileHandle in that FV.
92 @param FileHandle Handle of file image
93 @param VolumeHandle Handle of the found FV, if not found, NULL will be set.
95 @retval TRUE The corresponding FV handler is found.
96 @retval FALSE The corresponding FV handler is not found.
101 PeiFileHandleToVolume (
102 IN EFI_PEI_FILE_HANDLE FileHandle
,
103 OUT EFI_PEI_FV_HANDLE
*VolumeHandle
107 PEI_CORE_INSTANCE
*PrivateData
;
108 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
110 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
111 for (Index
= 0; Index
< PrivateData
->FvCount
; Index
++) {
112 FwVolHeader
= PrivateData
->Fv
[Index
].FvHeader
;
113 if (((UINT64
) (UINTN
) FileHandle
> (UINT64
) (UINTN
) FwVolHeader
) && \
114 ((UINT64
) (UINTN
) FileHandle
<= ((UINT64
) (UINTN
) FwVolHeader
+ FwVolHeader
->FvLength
- 1))) {
115 *VolumeHandle
= (EFI_PEI_FV_HANDLE
)FwVolHeader
;
119 *VolumeHandle
= NULL
;
124 Given the input file pointer, search for the first matching file in the
125 FFS volume as defined by SearchType. The search starts from FileHeader inside
126 the Firmware Volume defined by FwVolHeader.
127 If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.
128 If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE,
129 the first PEIM, or COMBINED PEIM or FV file type FFS file will return.
131 @param FvHandle Pointer to the FV header of the volume to search
132 @param FileName File name
133 @param SearchType Filter to find only files of this type.
134 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
135 @param FileHandle This parameter must point to a valid FFS volume.
136 @param AprioriFile Pointer to AprioriFile image in this FV if has
138 @return EFI_NOT_FOUND No files matching the search criteria were found
139 @retval EFI_SUCCESS Success to search given file
144 IN CONST EFI_PEI_FV_HANDLE FvHandle
,
145 IN CONST EFI_GUID
*FileName
, OPTIONAL
146 IN EFI_FV_FILETYPE SearchType
,
147 IN OUT EFI_PEI_FILE_HANDLE
*FileHandle
,
148 IN OUT EFI_PEI_FV_HANDLE
*AprioriFile OPTIONAL
151 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
152 EFI_FFS_FILE_HEADER
**FileHeader
;
153 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
155 UINT32 FileOccupiedSize
;
161 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)FvHandle
;
162 FileHeader
= (EFI_FFS_FILE_HEADER
**)FileHandle
;
164 FvLength
= FwVolHeader
->FvLength
;
165 if ((FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) != 0) {
172 // If FileHeader is not specified (NULL) or FileName is not NULL,
173 // start with the first file in the firmware volume. Otherwise,
174 // start from the FileHeader.
176 if ((*FileHeader
== NULL
) || (FileName
!= NULL
)) {
177 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FwVolHeader
+ FwVolHeader
->HeaderLength
);
180 // Length is 24 bits wide so mask upper 8 bits
181 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
183 FileLength
= *(UINT32
*)(*FileHeader
)->Size
& 0x00FFFFFF;
184 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
185 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)*FileHeader
+ FileOccupiedSize
);
188 FileOffset
= (UINT32
) ((UINT8
*)FfsFileHeader
- (UINT8
*)FwVolHeader
);
189 ASSERT (FileOffset
<= 0xFFFFFFFF);
191 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
193 // Get FileState which is the highest bit of the State
195 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
198 case EFI_FILE_HEADER_INVALID
:
199 FileOffset
+= sizeof(EFI_FFS_FILE_HEADER
);
200 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ sizeof(EFI_FFS_FILE_HEADER
));
203 case EFI_FILE_DATA_VALID
:
204 case EFI_FILE_MARKED_FOR_UPDATE
:
205 if (CalculateHeaderChecksum (FfsFileHeader
) != 0) {
208 return EFI_NOT_FOUND
;
211 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
212 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
214 if (FileName
!= NULL
) {
215 if (CompareGuid (&FfsFileHeader
->Name
, (EFI_GUID
*)FileName
)) {
216 *FileHeader
= FfsFileHeader
;
219 } else if (SearchType
== PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE
) {
220 if ((FfsFileHeader
->Type
== EFI_FV_FILETYPE_PEIM
) ||
221 (FfsFileHeader
->Type
== EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
) ||
222 (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
)) {
224 *FileHeader
= FfsFileHeader
;
226 } else if (AprioriFile
!= NULL
) {
227 if (FfsFileHeader
->Type
== EFI_FV_FILETYPE_FREEFORM
) {
228 if (CompareGuid (&FfsFileHeader
->Name
, &gPeiAprioriFileNameGuid
)) {
229 *AprioriFile
= FfsFileHeader
;
233 } else if (((SearchType
== FfsFileHeader
->Type
) || (SearchType
== EFI_FV_FILETYPE_ALL
)) &&
234 (FfsFileHeader
->Type
!= EFI_FV_FILETYPE_FFS_PAD
)) {
235 *FileHeader
= FfsFileHeader
;
239 FileOffset
+= FileOccupiedSize
;
240 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
243 case EFI_FILE_DELETED
:
244 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
245 FileOccupiedSize
= GET_OCCUPIED_SIZE(FileLength
, 8);
246 FileOffset
+= FileOccupiedSize
;
247 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
252 return EFI_NOT_FOUND
;
257 return EFI_NOT_FOUND
;
261 Initialize PeiCore Fv List.
263 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
264 @param SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.
268 IN PEI_CORE_INSTANCE
*PrivateData
,
269 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
274 // The BFV must be the first entry. The Core FV support is stateless
275 // The AllFV list has a single entry per FV in PEI.
276 // The Fv list only includes FV that PEIMs will be dispatched from and
277 // its File System Format is PI 1.0 definition.
279 PrivateData
->FvCount
= 1;
280 PrivateData
->Fv
[0].FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)SecCoreData
->BootFirmwareVolumeBase
;
282 PrivateData
->AllFvCount
= 1;
283 PrivateData
->AllFv
[0] = (EFI_PEI_FV_HANDLE
)PrivateData
->Fv
[0].FvHeader
;
287 // Post a call-back for the FvInfoPPI services to expose
288 // additional Fvs to PeiCore.
290 Status
= PeiServicesNotifyPpi (&mNotifyOnFvInfoList
);
291 ASSERT_EFI_ERROR (Status
);
296 Process Firmware Volum Information once FvInfoPPI install.
297 The FV Info will be registered into PeiCore private data structure.
298 And search the inside FV image, if found, the new FV INFO PPI will be installed.
300 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
301 @param NotifyDescriptor Address of the notification descriptor data structure.
302 @param Ppi Address of the PPI that was installed.
304 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.
305 @return if not EFI_SUCESS, fail to verify FV.
310 FirmwareVolmeInfoPpiNotifyCallback (
311 IN EFI_PEI_SERVICES
**PeiServices
,
312 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
317 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*Fv
;
318 PEI_CORE_INSTANCE
*PrivateData
;
319 EFI_PEI_FILE_HANDLE FileHandle
;
321 UINT32 AuthenticationStatus
;
326 Status
= EFI_SUCCESS
;
327 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
329 if (PrivateData
->FvCount
>= FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
330 DEBUG ((EFI_D_ERROR
, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData
->FvCount
+ 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)));
331 DEBUG ((EFI_D_ERROR
, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));
335 Fv
= (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*)Ppi
;
338 // Only add FileSystem2 Fv to Fv list
340 if (CompareGuid (&Fv
->FvFormat
, &gEfiFirmwareFileSystem2Guid
)) {
341 for (FvCount
= 0; FvCount
< PrivateData
->FvCount
; FvCount
++) {
342 if ((UINTN
)PrivateData
->Fv
[FvCount
].FvHeader
== (UINTN
)Fv
->FvInfo
) {
347 Status
= VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER
*)Fv
->FvInfo
);
348 if (EFI_ERROR(Status
)) {
349 DEBUG ((EFI_D_ERROR
, "Fail to verify FV which address is 0x%11p", (VOID
*) Fv
->FvInfo
));
353 PrivateData
->Fv
[PrivateData
->FvCount
++].FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)Fv
->FvInfo
;
355 PrivateData
->AllFv
[PrivateData
->AllFvCount
++] = (EFI_PEI_FV_HANDLE
)Fv
->FvInfo
;
357 DEBUG ((EFI_D_INFO
, "The %dth FvImage start address is 0x%11p and size is 0x%08x\n", (UINT32
)PrivateData
->AllFvCount
, (VOID
*) Fv
->FvInfo
, Fv
->FvInfoSize
));
359 // Preprocess all FV type files in this new FileSystem2 Fv image
362 Status
= PeiFindFileEx (
363 (EFI_PEI_FV_HANDLE
)Fv
->FvInfo
,
365 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
,
369 if (!EFI_ERROR (Status
)) {
370 Status
= PeiFfsFindSectionData (
371 (CONST EFI_PEI_SERVICES
**) PeiServices
,
372 EFI_SECTION_PEI_DEPEX
,
376 if (!EFI_ERROR (Status
)) {
377 if (!PeimDispatchReadiness (PeiServices
, DepexData
)) {
379 // Dependency is not satisfied.
385 // Process FvFile to install FvInfo ppi and build FvHob
387 ProcessFvFile ((CONST EFI_PEI_SERVICES
**) PeiServices
, (EFI_PEI_FV_HANDLE
)Fv
->FvInfo
, FileHandle
, &AuthenticationStatus
);
389 } while (FileHandle
!= NULL
);
396 Go through the file to search SectionType section.
397 Search within encapsulation sections (compression and GUIDed) recursively,
398 until the match section is found.
400 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
401 @param SectionType - Filter to find only section of this type.
402 @param Section - From where to search.
403 @param SectionSize - The file size to search.
404 @param OutputBuffer - A pointer to the discovered section, if successful.
405 NULL if section not found
407 @return EFI_NOT_FOUND The match section is not found.
408 @return EFI_SUCCESS The match section is found.
412 PeiFfsProcessSection (
413 IN CONST EFI_PEI_SERVICES
**PeiServices
,
414 IN EFI_SECTION_TYPE SectionType
,
415 IN EFI_COMMON_SECTION_HEADER
*Section
,
416 IN UINTN SectionSize
,
417 OUT VOID
**OutputBuffer
421 UINT32 SectionLength
;
423 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*GuidSectionPpi
;
424 EFI_PEI_DECOMPRESS_PPI
*DecompressPpi
;
428 UINT32 Authentication
;
429 PEI_CORE_INSTANCE
*PrivateData
;
431 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
432 *OutputBuffer
= NULL
;
435 Status
= EFI_NOT_FOUND
;
438 while (ParsedLength
< SectionSize
) {
439 if (Section
->Type
== SectionType
) {
440 *OutputBuffer
= (VOID
*)(Section
+ 1);
442 } else if ((Section
->Type
== EFI_SECTION_GUID_DEFINED
) || (Section
->Type
== EFI_SECTION_COMPRESSION
)) {
444 // Check the encapsulated section is extracted into the cache data.
446 for (Index
= 0; Index
< PrivateData
->CacheSection
.AllSectionCount
; Index
++) {
447 if (Section
== PrivateData
->CacheSection
.Section
[Index
]) {
448 PpiOutput
= PrivateData
->CacheSection
.SectionData
[Index
];
449 PpiOutputSize
= PrivateData
->CacheSection
.SectionSize
[Index
];
451 // Search section directly from the cache data.
453 return PeiFfsProcessSection (
463 Status
= EFI_NOT_FOUND
;
464 if (Section
->Type
== EFI_SECTION_GUID_DEFINED
) {
465 Status
= PeiServicesLocatePpi (
466 &((EFI_GUID_DEFINED_SECTION
*)Section
)->SectionDefinitionGuid
,
469 (VOID
**) &GuidSectionPpi
471 if (!EFI_ERROR (Status
)) {
472 Status
= GuidSectionPpi
->ExtractSection (
480 } else if (Section
->Type
== EFI_SECTION_COMPRESSION
) {
481 Status
= PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid
, 0, NULL
, (VOID
**) &DecompressPpi
);
482 if (!EFI_ERROR (Status
)) {
483 Status
= DecompressPpi
->Decompress (
485 (CONST EFI_COMPRESSION_SECTION
*) Section
,
492 if (!EFI_ERROR (Status
)) {
494 // Update cache section data.
496 if (PrivateData
->CacheSection
.AllSectionCount
< CACHE_SETION_MAX_NUMBER
) {
497 PrivateData
->CacheSection
.AllSectionCount
++;
499 PrivateData
->CacheSection
.Section
[PrivateData
->CacheSection
.SectionIndex
] = Section
;
500 PrivateData
->CacheSection
.SectionData
[PrivateData
->CacheSection
.SectionIndex
] = PpiOutput
;
501 PrivateData
->CacheSection
.SectionSize
[PrivateData
->CacheSection
.SectionIndex
] = PpiOutputSize
;
502 PrivateData
->CacheSection
.SectionIndex
= (PrivateData
->CacheSection
.SectionIndex
+ 1)%CACHE_SETION_MAX_NUMBER
;
504 return PeiFfsProcessSection (
515 // Size is 24 bits wide so mask upper 8 bits.
516 // SectionLength is adjusted it is 4 byte aligned.
517 // Go to the next section
519 SectionLength
= *(UINT32
*)Section
->Size
& 0x00FFFFFF;
520 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
521 ASSERT (SectionLength
!= 0);
522 ParsedLength
+= SectionLength
;
523 Section
= (EFI_COMMON_SECTION_HEADER
*)((UINT8
*)Section
+ SectionLength
);
526 return EFI_NOT_FOUND
;
531 Given the input file pointer, search for the first matching section in the
534 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
535 @param SectionType Filter to find only sections of this type.
536 @param FileHandle Pointer to the current file to search.
537 @param SectionData A pointer to the discovered section, if successful.
538 NULL if section not found
540 @retval EFI_NOT_FOUND No files matching the search criteria were found
541 @retval EFI_SUCCESS Success to find section data in given file
546 PeiFfsFindSectionData (
547 IN CONST EFI_PEI_SERVICES
**PeiServices
,
548 IN EFI_SECTION_TYPE SectionType
,
549 IN EFI_PEI_FILE_HANDLE FileHandle
,
550 IN OUT VOID
**SectionData
553 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
555 EFI_COMMON_SECTION_HEADER
*Section
;
557 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)(FileHandle
);
560 // Size is 24 bits wide so mask upper 8 bits.
561 // Does not include FfsFileHeader header size
562 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
564 Section
= (EFI_COMMON_SECTION_HEADER
*)(FfsFileHeader
+ 1);
565 FileSize
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
566 FileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
568 return PeiFfsProcessSection (
578 Given the input file pointer, search for the next matching file in the
579 FFS volume as defined by SearchType. The search starts from FileHeader inside
580 the Firmware Volume defined by FwVolHeader.
583 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
584 @param SearchType Filter to find only files of this type.
585 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
586 @param VolumeHandle Pointer to the FV header of the volume to search.
587 @param FileHandle Pointer to the current file from which to begin searching.
588 This pointer will be updated upon return to reflect the file found.
589 @retval EFI_NOT_FOUND No files matching the search criteria were found
590 @retval EFI_SUCCESS Success to find next file in given volume
596 IN CONST EFI_PEI_SERVICES
**PeiServices
,
598 IN EFI_PEI_FV_HANDLE VolumeHandle
,
599 IN OUT EFI_PEI_FILE_HANDLE
*FileHandle
602 return PeiFindFileEx (VolumeHandle
, NULL
, SearchType
, FileHandle
, NULL
);
607 Search the firmware volumes by index
609 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
610 @param Instance Instance of FV to find
611 @param VolumeHandle Pointer to found Volume.
613 @retval EFI_INVALID_PARAMETER FwVolHeader is NULL
614 @retval EFI_SUCCESS Firmware volume instance successfully found.
619 PeiFvFindNextVolume (
620 IN CONST EFI_PEI_SERVICES
**PeiServices
,
622 IN OUT EFI_PEI_FV_HANDLE
*VolumeHandle
625 PEI_CORE_INSTANCE
*Private
;
628 EFI_HOB_FIRMWARE_VOLUME
*FvHob
;
630 Private
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
631 if (VolumeHandle
== NULL
) {
632 return EFI_INVALID_PARAMETER
;
636 // Handle Framework FvHob and Install FvInfo Ppi for it.
638 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport
)) {
640 // Loop to search the wanted FirmwareVolume which supports FFS
642 FvHob
= (EFI_HOB_FIRMWARE_VOLUME
*)GetFirstHob (EFI_HOB_TYPE_FV
);
643 while (FvHob
!= NULL
) {
644 for (Index
= 0, Match
= FALSE
; Index
< Private
->AllFvCount
; Index
++) {
645 if ((EFI_PEI_FV_HANDLE
)(UINTN
)FvHob
->BaseAddress
== Private
->AllFv
[Index
]) {
651 // If Not Found, Install FvInfo Ppi for it.
654 PeiServicesInstallFvInfoPpi (
656 (VOID
*)(UINTN
)FvHob
->BaseAddress
,
657 (UINT32
)FvHob
->Length
,
662 FvHob
= (EFI_HOB_FIRMWARE_VOLUME
*)GetNextHob (EFI_HOB_TYPE_FV
, (VOID
*)((UINTN
)FvHob
+ FvHob
->Header
.HobLength
));
666 if (Instance
>= Private
->AllFvCount
) {
668 return EFI_NOT_FOUND
;
671 *VolumeHandle
= Private
->AllFv
[Instance
];
677 Find a file within a volume by its name.
679 @param FileName A pointer to the name of the file to find within the firmware volume.
680 @param VolumeHandle The firmware volume to search
681 @param FileHandle Upon exit, points to the found file's handle
682 or NULL if it could not be found.
684 @retval EFI_SUCCESS File was found.
685 @retval EFI_NOT_FOUND File was not found.
686 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.
691 PeiFfsFindFileByName (
692 IN CONST EFI_GUID
*FileName
,
693 IN EFI_PEI_FV_HANDLE VolumeHandle
,
694 OUT EFI_PEI_FILE_HANDLE
*FileHandle
698 if ((VolumeHandle
== NULL
) || (FileName
== NULL
) || (FileHandle
== NULL
)) {
699 return EFI_INVALID_PARAMETER
;
701 Status
= PeiFindFileEx (VolumeHandle
, FileName
, 0, FileHandle
, NULL
);
702 if (Status
== EFI_NOT_FOUND
) {
710 Returns information about a specific file.
713 @param FileHandle - The handle to file.
714 @param FileInfo - Pointer to the file information.
716 @retval EFI_INVALID_PARAMETER Invalid FileHandle or FileInfo.
717 @retval EFI_SUCCESS Success to collect file info.
723 IN EFI_PEI_FILE_HANDLE FileHandle
,
724 OUT EFI_FV_FILE_INFO
*FileInfo
729 EFI_FFS_FILE_HEADER
*FileHeader
;
730 EFI_PEI_FV_HANDLE VolumeHandle
;
732 if ((FileHandle
== NULL
) || (FileInfo
== NULL
)) {
733 return EFI_INVALID_PARAMETER
;
738 // Retrieve the FirmwareVolume which the file resides in.
740 if (!PeiFileHandleToVolume(FileHandle
, &VolumeHandle
)) {
741 return EFI_INVALID_PARAMETER
;
744 if (((EFI_FIRMWARE_VOLUME_HEADER
*)VolumeHandle
)->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
751 // Get FileState which is the highest bit of the State
753 FileState
= GetFileState (ErasePolarity
, (EFI_FFS_FILE_HEADER
*)FileHandle
);
756 case EFI_FILE_DATA_VALID
:
757 case EFI_FILE_MARKED_FOR_UPDATE
:
760 return EFI_INVALID_PARAMETER
;
763 FileHeader
= (EFI_FFS_FILE_HEADER
*)FileHandle
;
764 CopyMem (&FileInfo
->FileName
, &FileHeader
->Name
, sizeof(EFI_GUID
));
765 FileInfo
->FileType
= FileHeader
->Type
;
766 FileInfo
->FileAttributes
= FileHeader
->Attributes
;
767 FileInfo
->BufferSize
= ((*(UINT32
*)FileHeader
->Size
) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER
);
768 FileInfo
->Buffer
= (FileHeader
+ 1);
775 Collect information of given Fv Volume.
777 @param VolumeHandle - The handle to Fv Volume.
778 @param VolumeInfo - The pointer to volume information.
780 @retval EFI_INVALID_PARAMETER VolumeInfo is NULL
781 @retval EFI_SUCCESS Success to collect fv info.
785 PeiFfsGetVolumeInfo (
786 IN EFI_PEI_FV_HANDLE VolumeHandle
,
787 OUT EFI_FV_INFO
*VolumeInfo
790 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader
;
791 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FwVolExHeaderInfo
;
793 if (VolumeInfo
== NULL
) {
794 return EFI_INVALID_PARAMETER
;
798 // VolumeHandle may not align at 8 byte,
799 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
800 // So, Copy FvHeader into the local FvHeader structure.
802 CopyMem (&FwVolHeader
, VolumeHandle
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
));
804 // Check Fv Image Signature
806 if (FwVolHeader
.Signature
!= EFI_FVH_SIGNATURE
) {
807 return EFI_INVALID_PARAMETER
;
809 ZeroMem (VolumeInfo
, sizeof (EFI_FV_INFO
));
810 VolumeInfo
->FvAttributes
= FwVolHeader
.Attributes
;
811 VolumeInfo
->FvStart
= (VOID
*) VolumeHandle
;
812 VolumeInfo
->FvSize
= FwVolHeader
.FvLength
;
813 CopyMem (&VolumeInfo
->FvFormat
, &FwVolHeader
.FileSystemGuid
, sizeof(EFI_GUID
));
815 if (FwVolHeader
.ExtHeaderOffset
!= 0) {
816 FwVolExHeaderInfo
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*)(((UINT8
*)VolumeHandle
) + FwVolHeader
.ExtHeaderOffset
);
817 CopyMem (&VolumeInfo
->FvName
, &FwVolExHeaderInfo
->FvName
, sizeof(EFI_GUID
));
823 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.
825 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
826 @param ParentFvHandle Fv handle to parent Fv image that contain this Fv image.
827 @param ParentFvFileHandle File handle of a Fv type file that contain this Fv image.
828 @param AuthenticationState Pointer to attestation authentication state of image.
831 @retval EFI_NOT_FOUND FV image can't be found.
832 @retval EFI_SUCCESS Successfully to process it.
833 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image
834 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section
839 IN CONST EFI_PEI_SERVICES
**PeiServices
,
840 IN EFI_PEI_FV_HANDLE ParentFvHandle
,
841 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle
,
842 OUT UINT32
*AuthenticationState
846 EFI_PEI_FV_HANDLE FvImageHandle
;
847 EFI_FV_INFO FvImageInfo
;
848 EFI_FV_INFO ParentFvImageInfo
;
851 EFI_PEI_HOB_POINTERS HobPtr
;
854 *AuthenticationState
= 0;
857 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
860 HobPtr
.Raw
= GetHobList ();
861 while ((HobPtr
.Raw
= GetNextHob (EFI_HOB_TYPE_FV2
, HobPtr
.Raw
)) != NULL
) {
862 if (CompareGuid (&(((EFI_FFS_FILE_HEADER
*)ParentFvFileHandle
)->Name
), &HobPtr
.FirmwareVolume2
->FileName
)) {
864 // this FILE has been dispatched, it will not be dispatched again.
868 HobPtr
.Raw
= GET_NEXT_HOB (HobPtr
);
872 // Find FvImage in FvFile
874 Status
= PeiFfsFindSectionData (
876 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
,
878 (VOID
**)&FvImageHandle
881 if (EFI_ERROR (Status
)) {
886 // Collect Parent FvImage Info.
888 Status
= PeiFfsGetVolumeInfo (ParentFvHandle
, &ParentFvImageInfo
);
889 ASSERT_EFI_ERROR (Status
);
892 // Collect FvImage Info.
894 Status
= PeiFfsGetVolumeInfo (FvImageHandle
, &FvImageInfo
);
895 ASSERT_EFI_ERROR (Status
);
898 // FvAlignment must be more than 8 bytes required by FvHeader structure.
900 FvAlignment
= 1 << ((FvImageInfo
.FvAttributes
& EFI_FVB2_ALIGNMENT
) >> 16);
901 if (FvAlignment
< 8) {
908 if ((UINTN
) FvImageInfo
.FvStart
% FvAlignment
!= 0) {
909 FvBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32
) FvImageInfo
.FvSize
), FvAlignment
);
910 if (FvBuffer
== NULL
) {
911 return EFI_OUT_OF_RESOURCES
;
913 CopyMem (FvBuffer
, FvImageInfo
.FvStart
, (UINTN
) FvImageInfo
.FvSize
);
915 // Update FvImageInfo after reload FvImage to new aligned memory
917 Status
= PeiFfsGetVolumeInfo ((EFI_PEI_FV_HANDLE
) FvBuffer
, &FvImageInfo
);
918 ASSERT_EFI_ERROR (Status
);
922 // Install FvPpi and Build FvHob
924 PeiServicesInstallFvInfoPpi (
927 (UINT32
) FvImageInfo
.FvSize
,
928 &ParentFvImageInfo
.FvName
,
929 &(((EFI_FFS_FILE_HEADER
*)ParentFvFileHandle
)->Name
)
933 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase
936 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvImageInfo
.FvStart
,
941 // Makes the encapsulated volume show up in DXE phase to skip processing of
942 // encapsulated file again.
945 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvImageInfo
.FvStart
,
947 &ParentFvImageInfo
.FvName
,
948 &(((EFI_FFS_FILE_HEADER
*)ParentFvFileHandle
)->Name
)