2 Var Check Hii generation from FV.
4 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5 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.
15 #include "VarCheckHiiGen.h"
17 // {d0bc7cb4-6a47-495f-aa11-710746da06a2}
18 #define EFI_VFR_ATTRACT_GUID \
19 { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } }
21 EFI_GUID gVfrArrayAttractGuid
= EFI_VFR_ATTRACT_GUID
;
24 { 0xFFFFFFFF, 0xFFFF, 0xFFFF, { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } }
26 EFI_GUID mAllFfGuid
= ALL_FF_GUID
;
28 #define VAR_CHECK_VFR_DRIVER_INFO_SIGNATURE SIGNATURE_32 ('V', 'D', 'R', 'I')
34 } VAR_CHECK_VFR_DRIVER_INFO
;
36 LIST_ENTRY mVfrDriverList
= INITIALIZE_LIST_HEAD_VARIABLE (mVfrDriverList
);
38 #define VAR_CHECK_VFR_DRIVER_INFO_FROM_LINK(a) CR (a, VAR_CHECK_VFR_DRIVER_INFO, Link, VAR_CHECK_VFR_DRIVER_INFO_SIGNATURE)
40 #define MAX_MATCH_GUID_NUM 100
43 Get the address by Guid.
45 Parse the FFS and find the GUID address.
46 There may be multiple Guids matching the searched Guid.
48 @param Ffs Pointer to the FFS.
49 @param Guid Guid to find.
50 @param Length The length of FFS.
51 @param Offset Pointer to pointer to the offset.
52 @param NumOfMatchingGuid The number of matching Guid.
54 @retval EFI_SUCCESS One or multiple Guids matching the searched Guid.
55 @retval EFI_NOT_FOUND No Guid matching the searched Guid.
64 OUT UINT8
*NumOfMatchingGuid
70 if((Ffs
== NULL
) || (Guid
== NULL
) || (Length
== 0)){
74 if (NumOfMatchingGuid
!= NULL
) {
75 *NumOfMatchingGuid
= 0;
79 for (LoopControl
= 0; LoopControl
< Length
; LoopControl
++) {
80 if (CompareGuid (Guid
, (EFI_GUID
*) ((UINT8
*) Ffs
+ LoopControl
))) {
83 // If NumOfMatchGuid or Offset are NULL, means user only want
84 // to check whether current FFS includes this Guid or not.
86 if ((NumOfMatchingGuid
!= NULL
) && (Offset
!= NULL
)) {
87 if (*NumOfMatchingGuid
== 0) {
88 *Offset
= InternalVarCheckAllocateZeroPool (sizeof (UINTN
) * MAX_MATCH_GUID_NUM
);
89 ASSERT (*Offset
!= NULL
);
91 *(*Offset
+ *NumOfMatchingGuid
) = LoopControl
+ sizeof (EFI_GUID
);
92 (*NumOfMatchingGuid
)++;
99 return (Found
? EFI_SUCCESS
: EFI_NOT_FOUND
);
103 Search the VfrBin Base address.
105 According to the known GUID gVfrArrayAttractGuid to get the base address from FFS.
107 @param Ffs Pointer to the FFS.
108 @param EfiAddr Pointer to the EFI in FFS
109 @param Length The length of FFS.
110 @param Offset Pointer to pointer to the Addr (Offset).
111 @param NumOfMatchingOffset The number of Addr (Offset).
113 @retval EFI_SUCCESS Get the address successfully.
114 @retval EFI_NOT_FOUND No VfrBin found.
123 OUT UINT8
*NumOfMatchingOffset
130 if ((Ffs
== NULL
) || (Offset
== NULL
)) {
131 return EFI_NOT_FOUND
;
133 Status
= GetAddressByGuid (
135 &gVfrArrayAttractGuid
,
140 if (Status
!= EFI_SUCCESS
) {
144 for (Index
= 0; Index
< *NumOfMatchingOffset
; Index
++) {
146 // Got the virOffset after the GUID
148 VirOffValue
= *(UINTN
*) ((UINTN
) Ffs
+ *(*Offset
+ Index
));
150 // Transfer the offset to the VA address. One modules may own multiple VfrBin address.
152 *(*Offset
+ Index
) = (UINTN
) EfiAddr
+ VirOffValue
;
161 @param[in] Fv2 Pointer to Fv2 protocol.
162 @param[in] DriverGuid Pointer to driver GUID.
164 @return Found the driver in the FV or not.
169 IN EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv2
,
170 IN EFI_GUID
*DriverGuid
174 EFI_FV_FILETYPE FoundType
;
175 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
176 UINT32 AuthenticationStatus
;
182 UINT8 NumberofMatchingVfrBin
;
183 UINTN
*VfrBinBaseAddress
;
185 Status
= Fv2
->ReadFile (
192 &AuthenticationStatus
194 if (EFI_ERROR (Status
)) {
199 Status
= Fv2
->ReadSection (
206 &AuthenticationStatus
208 if (!EFI_ERROR (Status
)) {
209 Status
= SearchVfrBinInFfs (Buffer
, 0, Size
, &VfrBinBaseAddress
, &NumberofMatchingVfrBin
);
210 if (!EFI_ERROR (Status
)) {
211 SectionBuffer
= NULL
;
212 Status
= Fv2
->ReadSection (
219 &AuthenticationStatus
221 if (!EFI_ERROR (Status
)) {
222 DEBUG ((EFI_D_INFO
, "FfsNameGuid - %g\n", DriverGuid
));
223 DEBUG ((EFI_D_INFO
, "NumberofMatchingVfrBin - 0x%02x\n", NumberofMatchingVfrBin
));
225 for (VfrBinIndex
= 0; VfrBinIndex
< NumberofMatchingVfrBin
; VfrBinIndex
++) {
228 DumpHiiPackage ((UINT8
*) (UINTN
) SectionBuffer
+ VfrBinBaseAddress
[VfrBinIndex
] + sizeof (UINT32
));
231 VarCheckParseHiiPackage ((UINT8
*) (UINTN
) SectionBuffer
+ VfrBinBaseAddress
[VfrBinIndex
] + sizeof (UINT32
), TRUE
);
234 FreePool (SectionBuffer
);
237 InternalVarCheckFreePool (VfrBinBaseAddress
);
249 @param[in] ScanAll Scan all modules in all FVs or not.
258 EFI_HANDLE
*HandleBuffer
;
261 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv2
;
263 EFI_FV_FILETYPE FileType
;
265 EFI_FV_FILE_ATTRIBUTES FileAttributes
;
268 VAR_CHECK_VFR_DRIVER_INFO
*VfrDriverInfo
;
269 LIST_ENTRY
*VfrDriverLink
;
272 Status
= gBS
->LocateHandleBuffer (
274 &gEfiFirmwareVolume2ProtocolGuid
,
279 if (EFI_ERROR (Status
)) {
286 for (Index
= 0; Index
< HandleCount
; Index
++) {
287 DEBUG ((EFI_D_INFO
, "FvIndex - %x\n", Index
));
288 Status
= gBS
->HandleProtocol (
290 &gEfiFirmwareVolume2ProtocolGuid
,
293 ASSERT_EFI_ERROR (Status
);
296 EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
*Fvb2
;
297 EFI_PHYSICAL_ADDRESS FvAddress
;
300 Status
= gBS
->HandleProtocol (
302 &gEfiFirmwareVolumeBlock2ProtocolGuid
,
305 ASSERT_EFI_ERROR (Status
);
306 Status
= Fvb2
->GetPhysicalAddress (Fvb2
, &FvAddress
);
307 if (!EFI_ERROR (Status
)) {
308 DEBUG ((EFI_D_INFO
, "FvAddress - 0x%08x\n", FvAddress
));
309 FvSize
= ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) FvAddress
)->FvLength
;
310 DEBUG ((EFI_D_INFO
, "FvSize - 0x%08x\n", FvSize
));
316 // Need to parse all modules in all FVs.
318 Key
= InternalVarCheckAllocateZeroPool (Fv2
->KeySize
);
319 ASSERT (Key
!= NULL
);
321 for (FfsIndex
= 0; ; FfsIndex
++) {
322 FileType
= EFI_FV_FILETYPE_ALL
;
323 Status
= Fv2
->GetNextFile (
331 if (EFI_ERROR (Status
)) {
335 ParseFfs (Fv2
, &NameGuid
);
338 InternalVarCheckFreePool (Key
);
341 // Only parse drivers in the VFR drivers list.
343 VfrDriverLink
= mVfrDriverList
.ForwardLink
;
344 while (VfrDriverLink
!= &mVfrDriverList
) {
345 VfrDriverInfo
= VAR_CHECK_VFR_DRIVER_INFO_FROM_LINK (VfrDriverLink
);
346 VfrDriverLink
= VfrDriverLink
->ForwardLink
;
347 if (ParseFfs (Fv2
, VfrDriverInfo
->DriverGuid
)) {
349 // Found the driver in the FV.
351 RemoveEntryList (&VfrDriverInfo
->Link
);
352 InternalVarCheckFreePool (VfrDriverInfo
);
358 FreePool (HandleBuffer
);
362 Create Vfr Driver List.
364 @param[in] DriverGuidArray Driver Guid Array
368 CreateVfrDriverList (
369 IN EFI_GUID
*DriverGuidArray
373 VAR_CHECK_VFR_DRIVER_INFO
*VfrDriverInfo
;
375 for (Index
= 0; !IsZeroGuid (&DriverGuidArray
[Index
]); Index
++) {
376 DEBUG ((EFI_D_INFO
, "CreateVfrDriverList: %g\n", &DriverGuidArray
[Index
]));
377 VfrDriverInfo
= InternalVarCheckAllocateZeroPool (sizeof (*VfrDriverInfo
));
378 ASSERT (VfrDriverInfo
!= NULL
);
379 VfrDriverInfo
->Signature
= VAR_CHECK_VFR_DRIVER_INFO_SIGNATURE
;
380 VfrDriverInfo
->DriverGuid
= &DriverGuidArray
[Index
];
381 InsertTailList (&mVfrDriverList
, &VfrDriverInfo
->Link
);
386 Destroy Vfr Driver List.
390 DestroyVfrDriverList (
394 VAR_CHECK_VFR_DRIVER_INFO
*VfrDriverInfo
;
395 LIST_ENTRY
*VfrDriverLink
;
397 while (mVfrDriverList
.ForwardLink
!= &mVfrDriverList
) {
398 VfrDriverLink
= mVfrDriverList
.ForwardLink
;
399 VfrDriverInfo
= VAR_CHECK_VFR_DRIVER_INFO_FROM_LINK (VfrDriverLink
);
400 RemoveEntryList (&VfrDriverInfo
->Link
);
401 InternalVarCheckFreePool (VfrDriverInfo
);
410 VarCheckHiiGenFromFv (
414 EFI_GUID
*DriverGuidArray
;
417 DEBUG ((EFI_D_INFO
, "VarCheckHiiGenDxeFromFv\n"));
420 // Get vfr driver guid array from PCD.
422 DriverGuidArray
= (EFI_GUID
*) PcdGetPtr (PcdVarCheckVfrDriverGuidArray
);
424 if (IsZeroGuid (&DriverGuidArray
[0])) {
426 // No VFR driver will be parsed from FVs.
431 if (CompareGuid (&DriverGuidArray
[0], &mAllFfGuid
)) {
435 CreateVfrDriverList (DriverGuidArray
);
441 DestroyVfrDriverList ();