3 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
4 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "StandaloneMmCore.h"
10 #include <Library/FvLib.h>
11 #include <Library/ExtractGuidedSectionLib.h>
14 // List of file types supported by dispatcher
16 EFI_FV_FILETYPE mMmFileTypes
[] = {
18 0xE, //EFI_FV_FILETYPE_MM_STANDALONE,
20 // Note: DXE core will process the FV image file, so skip it in MM core
21 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
27 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
29 IN UINTN Pe32DataSize
,
32 IN EFI_GUID
*DriverName
37 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
42 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
46 Given the pointer to the Firmware Volume Header find the
47 MM driver and return its PE32 image.
49 @param [in] FwVolHeader Pointer to memory mapped FV
51 @retval EFI_SUCCESS Success.
52 @retval EFI_INVALID_PARAMETER Invalid parameter.
53 @retval EFI_NOT_FOUND Could not find section data.
54 @retval EFI_OUT_OF_RESOURCES Out of resources.
55 @retval EFI_VOLUME_CORRUPTED Firmware volume is corrupted.
56 @retval EFI_UNSUPPORTED Operation not supported.
60 MmCoreFfsFindMmDriver (
61 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
65 EFI_STATUS DepexStatus
;
66 EFI_FFS_FILE_HEADER
*FileHeader
;
67 EFI_FV_FILETYPE FileType
;
73 EFI_COMMON_SECTION_HEADER
*Section
;
75 UINTN SectionDataSize
;
78 UINT32 ScratchBufferSize
;
80 UINT16 SectionAttribute
;
81 UINT32 AuthenticationStatus
;
82 EFI_FIRMWARE_VOLUME_HEADER
*InnerFvHeader
;
84 DEBUG ((DEBUG_INFO
, "MmCoreFfsFindMmDriver - 0x%x\n", FwVolHeader
));
86 if (FvHasBeenProcessed (FwVolHeader
)) {
90 FvIsBeingProcessed (FwVolHeader
);
93 // First check for encapsulated compressed firmware volumes
97 Status
= FfsFindNextFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
,
98 FwVolHeader
, &FileHeader
);
99 if (EFI_ERROR (Status
)) {
102 Status
= FfsFindSectionData (EFI_SECTION_GUID_DEFINED
, FileHeader
,
103 &SectionData
, &SectionDataSize
);
104 if (EFI_ERROR (Status
)) {
107 Section
= (EFI_COMMON_SECTION_HEADER
*)(FileHeader
+ 1);
108 Status
= ExtractGuidedSectionGetInfo (Section
, &DstBufferSize
,
109 &ScratchBufferSize
, &SectionAttribute
);
110 if (EFI_ERROR (Status
)) {
115 // Allocate scratch buffer
117 ScratchBuffer
= (VOID
*)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
118 if (ScratchBuffer
== NULL
) {
119 return EFI_OUT_OF_RESOURCES
;
123 // Allocate destination buffer, extra one page for adjustment
125 DstBuffer
= (VOID
*)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
));
126 if (DstBuffer
== NULL
) {
127 return EFI_OUT_OF_RESOURCES
;
131 // Call decompress function
133 Status
= ExtractGuidedSectionDecode (Section
, &DstBuffer
, ScratchBuffer
,
134 &AuthenticationStatus
);
135 FreePages (ScratchBuffer
, EFI_SIZE_TO_PAGES (ScratchBufferSize
));
136 if (EFI_ERROR (Status
)) {
141 "Processing compressed firmware volume (AuthenticationStatus == %x)\n",
142 AuthenticationStatus
));
144 Status
= FindFfsSectionInSections (DstBuffer
, DstBufferSize
,
145 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
, &Section
);
146 if (EFI_ERROR (Status
)) {
150 InnerFvHeader
= (VOID
*)(Section
+ 1);
151 Status
= MmCoreFfsFindMmDriver (InnerFvHeader
);
152 if (EFI_ERROR (Status
)) {
157 for (Index
= 0; Index
< sizeof (mMmFileTypes
) / sizeof (mMmFileTypes
[0]); Index
++) {
158 DEBUG ((DEBUG_INFO
, "Check MmFileTypes - 0x%x\n", mMmFileTypes
[Index
]));
159 FileType
= mMmFileTypes
[Index
];
162 Status
= FfsFindNextFile (FileType
, FwVolHeader
, &FileHeader
);
163 if (!EFI_ERROR (Status
)) {
164 Status
= FfsFindSectionData (EFI_SECTION_PE32
, FileHeader
, &Pe32Data
, &Pe32DataSize
);
165 DEBUG ((DEBUG_INFO
, "Find PE data - 0x%x\n", Pe32Data
));
166 DepexStatus
= FfsFindSectionData (EFI_SECTION_MM_DEPEX
, FileHeader
, &Depex
, &DepexSize
);
167 if (!EFI_ERROR (DepexStatus
)) {
168 MmAddToDriverList (FwVolHeader
, Pe32Data
, Pe32DataSize
, Depex
, DepexSize
, &FileHeader
->Name
);
171 } while (!EFI_ERROR (Status
));
177 FreePages (DstBuffer
, EFI_SIZE_TO_PAGES (DstBufferSize
));