EmbeddedPkg/PrePiLib: Add FFS_CHECK_SECTION_HOOK when finding section

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4152

EmbeddedPkg/PrePiLib provides the service of finding sections based on
the input SectionType. But sometimes there maybe multiple sections
with the same SectionType. FFS_CHECK_SECTION_HOOK is a hook which can
be called to do additional check.

Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Abner Chang <abner.chang@amd.com>
Cc: Daniel Schaefer <git@danielschaefer.me>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Acked-by: Ard Biesheuvel <ardb+tianocore@kernel.org>
Signed-off-by: Min Xu <min.m.xu@intel.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Min M Xu 2023-01-17 07:31:55 +08:00 committed by mergify[bot]
parent 6c1988af76
commit c673216f53
3 changed files with 50 additions and 19 deletions

View File

@ -53,10 +53,22 @@ FfsFindNextFile (
);
/**
This service enables discovery sections of a given type within a valid FFS file.
* This is a hook which is used to check if the section is the target one.
*
*/
typedef
EFI_STATUS
(EFIAPI *FFS_CHECK_SECTION_HOOK)(
IN EFI_COMMON_SECTION_HEADER *Section
);
@param SearchType The value of the section type to find.
@param FfsFileHeader A pointer to the file header that contains the set of sections to
/**
This service enables discovery sections of a given type within a valid FFS file.
Caller also can provide a SectionCheckHook to do additional checking.
@param SectionType The value of the section type to find.
@param SectionCheckHook A hook which can check if the section is the target one.
@param FileHeader A pointer to the file header that contains the set of sections to
be searched.
@param SectionData A pointer to the discovered section, if successful.
@ -68,6 +80,7 @@ EFI_STATUS
EFIAPI
FfsFindSectionData (
IN EFI_SECTION_TYPE SectionType,
IN FFS_CHECK_SECTION_HOOK SectionCheckHook,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT VOID **SectionData
);

View File

@ -265,6 +265,7 @@ FindFileEx (
when meeting an encapsuled section.
@param SectionType - Filter to find only section of this type.
@param SectionCheckHook - A hook which can check if the section is the target one.
@param Section - From where to search.
@param SectionSize - The file size to search.
@param OutputBuffer - Pointer to the section to search.
@ -274,6 +275,7 @@ FindFileEx (
EFI_STATUS
FfsProcessSection (
IN EFI_SECTION_TYPE SectionType,
IN FFS_CHECK_SECTION_HOOK SectionCheckHook,
IN EFI_COMMON_SECTION_HEADER *Section,
IN UINTN SectionSize,
OUT VOID **OutputBuffer
@ -292,7 +294,9 @@ FfsProcessSection (
UINT32 AuthenticationStatus;
CHAR8 *CompressedData;
UINT32 CompressedDataLength;
BOOLEAN Found;
Found = FALSE;
*OutputBuffer = NULL;
ParsedLength = 0;
Status = EFI_NOT_FOUND;
@ -302,6 +306,13 @@ FfsProcessSection (
}
if (Section->Type == SectionType) {
if (SectionCheckHook != NULL) {
Found = SectionCheckHook (Section) == EFI_SUCCESS;
} else {
Found = TRUE;
}
if (Found) {
if (IS_SECTION2 (Section)) {
*OutputBuffer = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER2));
} else {
@ -309,6 +320,9 @@ FfsProcessSection (
}
return EFI_SUCCESS;
} else {
goto CheckNextSection;
}
} else if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) {
if (Section->Type == EFI_SECTION_COMPRESSION) {
if (IS_SECTION2 (Section)) {
@ -415,6 +429,7 @@ FfsProcessSection (
} else {
return FfsProcessSection (
SectionType,
SectionCheckHook,
DstBuffer,
DstBufferSize,
OutputBuffer
@ -422,6 +437,7 @@ FfsProcessSection (
}
}
CheckNextSection:
if (IS_SECTION2 (Section)) {
SectionLength = SECTION2_SIZE (Section);
} else {
@ -457,6 +473,7 @@ EFI_STATUS
EFIAPI
FfsFindSectionData (
IN EFI_SECTION_TYPE SectionType,
IN FFS_CHECK_SECTION_HOOK SectionCheckHook,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT VOID **SectionData
)
@ -478,6 +495,7 @@ FfsFindSectionData (
return FfsProcessSection (
SectionType,
SectionCheckHook,
Section,
FileSize,
SectionData
@ -799,7 +817,7 @@ FfsProcessFvFile (
//
// Find FvImage in FvFile
//
Status = FfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, FvFileHandle, (VOID **)&FvImageHandle);
Status = FfsFindSectionData (EFI_SECTION_FIRMWARE_VOLUME_IMAGE, NULL, FvFileHandle, (VOID **)&FvImageHandle);
if (EFI_ERROR (Status)) {
return Status;
}

View File

@ -131,7 +131,7 @@ LoadDxeCoreFromFfsFile (
VOID *Hob;
EFI_FV_FILE_INFO FvFileInfo;
Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);
Status = FfsFindSectionData (EFI_SECTION_PE32, NULL, FileHandle, &PeCoffImage);
if (EFI_ERROR (Status)) {
return Status;
}