[`edk2-devel][PATCH 6/8] UefiPayloadPkg: Add a common FVB SMM module


Guo Dong
 

From: Guo Dong <guo.dong@intel.com>

This FVB module is used to initialize NV variable region
and provide SMM FVB protocol to read/write SPI variable region.

This module consume HOB gNvVariableInfoGuid and depends on
FlashDeviceLib for the actual SPI device operate.

During FVB initialization, it will initialize the variable region
if the variable region is not valid. And it support to write initial
variable data from FFS file if it is found.

Signed-off-by: Guo Dong <guo.dong@intel.com>
---
UefiPayloadPkg/FvbRuntimeDxe/FvbInfo.c | 143 +++
UefiPayloadPkg/FvbRuntimeDxe/FvbService.c | 1085 +++++++++++++++++
UefiPayloadPkg/FvbRuntimeDxe/FvbService.h | 187 +++
UefiPayloadPkg/FvbRuntimeDxe/FvbServiceSmm.c | 139 +++
UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf | 71 ++
UefiPayloadPkg/FvbRuntimeDxe/FvbSmmCommon.h | 69 ++
.../Include/Guid/NvVariableInfoGuid.h | 30 +
UefiPayloadPkg/UefiPayloadPkg.dec | 6 +
8 files changed, 1730 insertions(+)
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbInfo.c
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbService.c
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbService.h
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbServiceSmm.c
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
create mode 100644 UefiPayloadPkg/FvbRuntimeDxe/FvbSmmCommon.h
create mode 100644 UefiPayloadPkg/Include/Guid/NvVariableInfoGuid.h

diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbInfo.c b/UefiPayloadPkg/FvbRun=
timeDxe/FvbInfo.c
new file mode 100644
index 0000000000..e4ce27b5b3
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbInfo.c
@@ -0,0 +1,143 @@
+/** @file=0D
+=0D
+ Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#include <PiDxe.h>=0D
+#include <Protocol/FirmwareVolumeBlock.h>=0D
+#include <Library/PcdLib.h>=0D
+#include <Library/DebugLib.h>=0D
+#include <Library/BaseLib.h>=0D
+#include <Guid/FirmwareFileSystem2.h>=0D
+#include <Guid/SystemNvDataGuid.h>=0D
+#include <Guid/NvVariableInfoGuid.h>=0D
+#include <Library/HobLib.h>=0D
+=0D
+#define FVB_MEDIA_BLOCK_SIZE 0x1000=0D
+=0D
+typedef struct {=0D
+ EFI_FIRMWARE_VOLUME_HEADER FvInfo;=0D
+ EFI_FV_BLOCK_MAP_ENTRY End[1];=0D
+} EFI_FVB2_MEDIA_INFO;=0D
+=0D
+//=0D
+// This data structure contains a template of FV header which is used to r=
estore=0D
+// Fv header if it's corrupted.=0D
+//=0D
+EFI_FVB2_MEDIA_INFO mFvbMediaInfo =3D {=0D
+ {=0D
+ {0,}, // ZeroVector[16]=0D
+ EFI_SYSTEM_NV_DATA_FV_GUID,=0D
+ 0,=0D
+ EFI_FVH_SIGNATURE,=0D
+ 0x0004feff, // check PiFirmwareVolume.h for details on EFI_FVB_ATT=
RIBUTES_2=0D
+ sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY),=
=0D
+ 0, // CheckSum which will be calucated dynamically.=0D
+ 0, // ExtHeaderOffset=0D
+ {0,},=0D
+ EFI_FVH_REVISION,=0D
+ {=0D
+ {=0D
+ 0,=0D
+ FVB_MEDIA_BLOCK_SIZE,=0D
+ }=0D
+ }=0D
+ },=0D
+ {=0D
+ {=0D
+ 0,=0D
+ 0=0D
+ }=0D
+ }=0D
+};=0D
+=0D
+=0D
+EFI_STATUS=0D
+InitVariableStore (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ UINT32 NvStorageBase;=0D
+ UINT32 NvStorageSize;=0D
+ UINT32 NvVariableSize;=0D
+ UINT32 FtwWorkingSize;=0D
+ UINT32 FtwSpareSize;=0D
+ EFI_HOB_GUID_TYPE *GuidHob;=0D
+ NV_VARIABLE_INFO *NvVariableInfo;=0D
+=0D
+ //=0D
+ // Find SPI flash variable hob=0D
+ //=0D
+ GuidHob =3D GetFirstGuidHob (&gNvVariableInfoGuid);=0D
+ if (GuidHob =3D=3D NULL) {=0D
+ ASSERT (FALSE);=0D
+ return EFI_NOT_FOUND;=0D
+ }=0D
+ NvVariableInfo =3D (NV_VARIABLE_INFO *) GET_GUID_HOB_DATA (GuidHob);=0D
+=0D
+ //=0D
+ // Get variable region base and size.=0D
+ //=0D
+ NvStorageSize =3D NvVariableInfo->VariableStoreSize;=0D
+ NvStorageBase =3D NvVariableInfo->VariableStoreBase;=0D
+=0D
+ //=0D
+ // NvStorageBase needs to be 4KB aligned, NvStorageSize needs to be 8KB =
* n=0D
+ //=0D
+ if (((NvStorageBase & (SIZE_4KB - 1)) !=3D 0) || ((NvStorageSize & (SIZE=
_8KB - 1)) !=3D 0)) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ FtwSpareSize =3D NvStorageSize / 2;=0D
+ FtwWorkingSize =3D 0x2000;=0D
+ NvVariableSize =3D NvStorageSize / 2 - FtwWorkingSize;=0D
+ DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorag=
eBase, NvStorageSize));=0D
+=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageVariableSize, NvVariableSize);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageVariableBase, NvStorageBase);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ Status =3D PcdSet64S(PcdFlashNvStorageVariableBase64, NvStorageBase);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageFtwWorkingBase, NvStorageBase + Nv=
VariableSize);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageFtwSpareSize, FtwSpareSize);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ Status =3D PcdSet32S(PcdFlashNvStorageFtwSpareBase, NvStorageBase + Ft=
wSpareSize);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Get a heathy FV header used for variable store recovery=0D
+=0D
+ @retval The FV header.=0D
+=0D
+**/=0D
+EFI_FIRMWARE_VOLUME_HEADER *=0D
+GetFvHeaderTemplate (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_FIRMWARE_VOLUME_HEADER *FvHeader;=0D
+ UINTN FvSize;=0D
+=0D
+ FvSize =3D PcdGet32(PcdFlashNvStorageFtwSpareSize) * 2;=0D
+ FvHeader =3D &mFvbMediaInfo.FvInfo;=0D
+ FvHeader->FvLength =3D FvSize;=0D
+ FvHeader->BlockMap[0].NumBlocks =3D (UINT32) (FvSize / FvHeader->BlockMa=
p[0].Length);=0D
+ FvHeader->Checksum =3D 0;=0D
+ FvHeader->Checksum =3D CalculateCheckSum16 ((UINT16 *) FvHeader, FvHeade=
r->HeaderLength);=0D
+=0D
+ return FvHeader;=0D
+}=0D
+=0D
diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbService.c b/UefiPayloadPkg/Fvb=
RuntimeDxe/FvbService.c
new file mode 100644
index 0000000000..dc213073df
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbService.c
@@ -0,0 +1,1085 @@
+/** @file=0D
+Firmware Volume Block Driver to provide FVB service.=0D
+=0D
+ Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#include "FvbService.h"=0D
+=0D
+//=0D
+// Global variable for this FVB driver which contains=0D
+// the private data of all firmware volume block instances=0D
+//=0D
+FWB_GLOBAL mFvbModuleGlobal;=0D
+=0D
+FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate =3D {=0D
+ {=0D
+ {=0D
+ HARDWARE_DEVICE_PATH,=0D
+ HW_MEMMAP_DP,=0D
+ {=0D
+ (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),=0D
+ (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)=0D
+ }=0D
+ },=0D
+ EfiMemoryMappedIO,=0D
+ (EFI_PHYSICAL_ADDRESS) 0,=0D
+ (EFI_PHYSICAL_ADDRESS) 0,=0D
+ },=0D
+ {=0D
+ END_DEVICE_PATH_TYPE,=0D
+ END_ENTIRE_DEVICE_PATH_SUBTYPE,=0D
+ {=0D
+ END_DEVICE_PATH_LENGTH,=0D
+ 0=0D
+ }=0D
+ }=0D
+};=0D
+=0D
+FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate =3D {=0D
+ {=0D
+ {=0D
+ MEDIA_DEVICE_PATH,=0D
+ MEDIA_PIWG_FW_VOL_DP,=0D
+ {=0D
+ (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),=0D
+ (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)=0D
+ }=0D
+ },=0D
+ { 0 }=0D
+ },=0D
+ {=0D
+ END_DEVICE_PATH_TYPE,=0D
+ END_ENTIRE_DEVICE_PATH_SUBTYPE,=0D
+ {=0D
+ END_DEVICE_PATH_LENGTH,=0D
+ 0=0D
+ }=0D
+ }=0D
+};=0D
+=0D
+=0D
+EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate =3D {=0D
+ FVB_DEVICE_SIGNATURE,=0D
+ NULL,=0D
+ 0, // Instance=0D
+ {=0D
+ FvbProtocolGetAttributes,=0D
+ FvbProtocolSetAttributes,=0D
+ FvbProtocolGetPhysicalAddress,=0D
+ FvbProtocolGetBlockSize,=0D
+ FvbProtocolRead,=0D
+ FvbProtocolWrite,=0D
+ FvbProtocolEraseBlocks,=0D
+ NULL=0D
+ } // FwVolBlockInstance=0D
+};=0D
+=0D
+=0D
+/**=0D
+ Get the pointer to EFI_FW_VOL_INSTANCE from the buffer pointed=0D
+ by mFvbModuleGlobal.FvInstance based on a index.=0D
+ Each EFI_FW_VOL_INSTANCE is with variable length as=0D
+ we have a block map at the end of the EFI_FIRMWARE_VOLUME_HEADER.=0D
+=0D
+ @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.=0D
+=0D
+ @return A pointer to EFI_FW_VOL_INSTANCE.=0D
+=0D
+**/=0D
+EFI_FW_VOL_INSTANCE *=0D
+GetFvbInstance (=0D
+ IN UINTN Instance=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_INSTANCE *FwhRecord;=0D
+=0D
+ if ( Instance >=3D mFvbModuleGlobal.NumFv ) {=0D
+ ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);=0D
+ return NULL;=0D
+ }=0D
+=0D
+ //=0D
+ // Find the right instance of the FVB private data=0D
+ //=0D
+ FwhRecord =3D mFvbModuleGlobal.FvInstance;=0D
+ while ( Instance > 0 ) {=0D
+ FwhRecord =3D (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhRecord) +=0D
+ FwhRecord->VolumeHeader.HeaderLength +=0D
+ (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUM=
E_HEADER)));=0D
+ Instance--;=0D
+ }=0D
+=0D
+ return FwhRecord;=0D
+=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Get the EFI_FVB_ATTRIBUTES_2 of a FV.=0D
+=0D
+ @param[in] The index of the EFI_FW_VOL_INSTANCE.=0D
+=0D
+ @return EFI_FVB_ATTRIBUTES_2 of the FV identified by Instance.=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_FVB_ATTRIBUTES_2=0D
+FvbGetVolumeAttributes (=0D
+ IN UINTN Instance=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_INSTANCE * FwInstance;=0D
+ FwInstance =3D GetFvbInstance(Instance);=0D
+ ASSERT (FwInstance !=3D NULL);=0D
+=0D
+ if (FwInstance =3D=3D NULL) {=0D
+ return 0;=0D
+ }=0D
+=0D
+ return FwInstance->VolumeHeader.Attributes;=0D
+}=0D
+=0D
+=0D
+=0D
+/**=0D
+ Retrieves the starting address of an LBA in an FV. It also=0D
+ return a few other attribut of the FV.=0D
+=0D
+ @param[in] Instance The index of the EFI_FW_VOL_INSTANCE.=0D
+ @param[in] Lba The logical block address=0D
+ @param[out] LbaAddress On output, contains the physical starting ad=
dress=0D
+ of the Lba=0D
+ @param[out] LbaLength On output, contains the length of the block=
=0D
+ @param[out] NumOfBlocks A pointer to a caller allocated UINTN in whi=
ch the=0D
+ number of consecutive blocks starting with L=
ba is=0D
+ returned. All blocks in this range have a si=
ze of=0D
+ BlockSize=0D
+=0D
+ @retval EFI_SUCCESS Successfully returns=0D
+ @retval EFI_INVALID_PARAMETER Instance not found=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+FvbGetLbaAddress (=0D
+ IN UINTN Instance,=0D
+ IN EFI_LBA Lba,=0D
+ OUT UINTN *LbaAddress,=0D
+ OUT UINTN *LbaLength,=0D
+ OUT UINTN *NumOfBlocks=0D
+ )=0D
+{=0D
+ UINT32 NumBlocks;=0D
+ UINT32 BlockLength;=0D
+ UINTN Offset;=0D
+ EFI_LBA StartLba;=0D
+ EFI_LBA NextLba;=0D
+ EFI_FW_VOL_INSTANCE *FwhInstance;=0D
+ EFI_FV_BLOCK_MAP_ENTRY *BlockMap;=0D
+=0D
+ //=0D
+ // Find the right instance of the FVB private data=0D
+ //=0D
+ FwhInstance =3D GetFvbInstance (Instance);=0D
+ if (FwhInstance =3D=3D NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ StartLba =3D 0;=0D
+ Offset =3D 0;=0D
+ BlockMap =3D &FwhInstance->VolumeHeader.BlockMap[0];=0D
+ ASSERT (BlockMap !=3D NULL);=0D
+=0D
+ //=0D
+ // Parse the blockmap of the FV to find which map entry the Lba belongs =
to=0D
+ //=0D
+ while (TRUE) {=0D
+ if ( BlockMap !=3D NULL) {=0D
+ NumBlocks =3D BlockMap->NumBlocks;=0D
+ BlockLength =3D BlockMap->Length;=0D
+ }=0D
+=0D
+ if ( NumBlocks =3D=3D 0 || BlockLength =3D=3D 0) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ NextLba =3D StartLba + NumBlocks;=0D
+=0D
+ //=0D
+ // The map entry found=0D
+ //=0D
+ if (Lba >=3D StartLba && Lba < NextLba) {=0D
+ Offset =3D Offset + (UINTN)MultU64x32((Lba - StartLba), BlockLength)=
;=0D
+ if (LbaAddress !=3D NULL) {=0D
+ *LbaAddress =3D FwhInstance->FvBase + Offset;=0D
+ }=0D
+=0D
+ if (LbaLength !=3D NULL) {=0D
+ *LbaLength =3D BlockLength;=0D
+ }=0D
+=0D
+ if (NumOfBlocks !=3D NULL) {=0D
+ *NumOfBlocks =3D (UINTN)(NextLba - Lba);=0D
+ }=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ StartLba =3D NextLba;=0D
+ Offset =3D Offset + NumBlocks * BlockLength;=0D
+ BlockMap++;=0D
+ }=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Reads specified number of bytes into a buffer from the specified block=0D
+=0D
+ @param[in] Instance The FV instance to be read from=0D
+ @param[in] Lba The logical block address to be read fro=
m=0D
+ @param[in] BlockOffset Offset into the block at which to begin =
reading=0D
+ @param[in] NumBytes Pointer that on input contains the total=
size of=0D
+ the buffer. On output, it contains the t=
otal number=0D
+ of bytes read=0D
+ @param[in] Buffer Pointer to a caller allocated buffer tha=
t will be=0D
+ used to hold the data read=0D
+=0D
+=0D
+ @retval EFI_SUCCESS The firmware volume was read success=
fully and=0D
+ contents are in Buffer=0D
+ @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary=
. On output,=0D
+ NumBytes contains the total number o=
f bytes returned=0D
+ in Buffer=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDi=
sabled state=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning =
correctly and=0D
+ could not be read=0D
+ @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, B=
uffer are NULL=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+FvbReadBlock (=0D
+ IN UINTN Instance,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN BlockOffset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ IN UINT8 *Buffer=0D
+ )=0D
+{=0D
+ EFI_FVB_ATTRIBUTES_2 Attributes;=0D
+ UINTN LbaAddress;=0D
+ UINTN LbaLength;=0D
+ EFI_STATUS Status;=0D
+=0D
+ if ( (NumBytes =3D=3D NULL) || (Buffer =3D=3D NULL)) {=0D
+ return (EFI_INVALID_PARAMETER);=0D
+ }=0D
+ if (*NumBytes =3D=3D 0) {=0D
+ return (EFI_INVALID_PARAMETER);=0D
+ }=0D
+=0D
+ Status =3D FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NUL=
L);=0D
+ if (EFI_ERROR(Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ Attributes =3D FvbGetVolumeAttributes (Instance);=0D
+=0D
+ if ( (Attributes & EFI_FVB2_READ_STATUS) =3D=3D 0) {=0D
+ return (EFI_ACCESS_DENIED);=0D
+ }=0D
+=0D
+ if (BlockOffset > LbaLength) {=0D
+ return (EFI_INVALID_PARAMETER);=0D
+ }=0D
+=0D
+ if (LbaLength < ( *NumBytes + BlockOffset ) ) {=0D
+ *NumBytes =3D (UINT32) (LbaLength - BlockOffset);=0D
+ Status =3D EFI_BAD_BUFFER_SIZE;=0D
+ }=0D
+=0D
+ Status =3D LibFvbFlashDeviceRead (LbaAddress + BlockOffset, NumBytes, Bu=
ffer);=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Writes specified number of bytes from the input buffer to the block=0D
+=0D
+ @param[in] Instance The FV instance to be written to=0D
+ @param[in] Lba The starting logical block index to write =
to=0D
+ @param[in] BlockOffset Offset into the block at which to begin wr=
iting=0D
+ @param[in] NumBytes Pointer that on input contains the total s=
ize of=0D
+ the buffer. On output, it contains the to=
tal number=0D
+ of bytes actually written=0D
+ @param[in] Buffer Pointer to a caller allocated buffer that =
contains=0D
+ the source for the write=0D
+ @retval EFI_SUCCESS The firmware volume was written successf=
ully=0D
+ @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. O=
n output,=0D
+ NumBytes contains the total number of by=
tes=0D
+ actually written=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisab=
led state=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning corr=
ectly and=0D
+ could not be written=0D
+ @retval EFI_INVALID_PARAMETER Instance not found, or NumBytes, Buffe=
r are NULL=0D
+=0D
+**/=0D
+=0D
+EFI_STATUS=0D
+FvbWriteBlock (=0D
+ IN UINTN Instance,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN BlockOffset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ IN UINT8 *Buffer=0D
+ )=0D
+{=0D
+ EFI_FVB_ATTRIBUTES_2 Attributes;=0D
+ UINTN LbaAddress;=0D
+ UINTN LbaLength;=0D
+ EFI_STATUS Status;=0D
+=0D
+ if ( (NumBytes =3D=3D NULL) || (Buffer =3D=3D NULL)) {=0D
+ return (EFI_INVALID_PARAMETER);=0D
+ }=0D
+ if (*NumBytes =3D=3D 0) {=0D
+ return (EFI_INVALID_PARAMETER);=0D
+ }=0D
+=0D
+ Status =3D FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NUL=
L);=0D
+ if (EFI_ERROR(Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ //=0D
+ // Check if the FV is write enabled=0D
+ //=0D
+ Attributes =3D FvbGetVolumeAttributes (Instance);=0D
+ if ( (Attributes & EFI_FVB2_WRITE_STATUS) =3D=3D 0) {=0D
+ return EFI_ACCESS_DENIED;=0D
+ }=0D
+=0D
+ //=0D
+ // Perform boundary checks and adjust NumBytes=0D
+ //=0D
+ if (BlockOffset > LbaLength) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ if ( LbaLength < ( *NumBytes + BlockOffset ) ) {=0D
+ DEBUG ((DEBUG_ERROR,=0D
+ "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",=0D
+ *NumBytes, (UINT32)(LbaLength - BlockOffset)));=0D
+ *NumBytes =3D (UINT32) (LbaLength - BlockOffset);=0D
+ return EFI_BAD_BUFFER_SIZE;=0D
+ }=0D
+=0D
+ LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);=0D
+ Status =3D LibFvbFlashDeviceWrite (LbaAddress + BlockOffset, NumBytes, B=
uffer);=0D
+=0D
+ LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);=0D
+ WriteBackInvalidateDataCacheRange ((VOID *) (LbaAddress + BlockOffset), =
*NumBytes);=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Erases and initializes a firmware volume block=0D
+=0D
+ @param[in] Instance The FV instance to be erased=0D
+ @param[in] Lba The logical block index to be erased=0D
+=0D
+ @retval EFI_SUCCESS The erase request was successfully completed=
=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled =
state=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning correctl=
y and=0D
+ could not be written. Firmware device may ha=
ve been=0D
+ partially erased=0D
+ @retval EFI_INVALID_PARAMETER Instance not found=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+FvbEraseBlock (=0D
+ IN UINTN Instance,=0D
+ IN EFI_LBA Lba=0D
+ )=0D
+{=0D
+=0D
+ EFI_FVB_ATTRIBUTES_2 Attributes;=0D
+ UINTN LbaAddress;=0D
+ UINTN LbaLength;=0D
+ EFI_STATUS Status;=0D
+=0D
+ //=0D
+ // Check if the FV is write enabled=0D
+ //=0D
+ Attributes =3D FvbGetVolumeAttributes (Instance);=0D
+=0D
+ if( (Attributes & EFI_FVB2_WRITE_STATUS) =3D=3D 0) {=0D
+ return (EFI_ACCESS_DENIED);=0D
+ }=0D
+=0D
+ //=0D
+ // Get the starting address of the block for erase.=0D
+ //=0D
+ Status =3D FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NUL=
L);=0D
+ if (EFI_ERROR(Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);=0D
+=0D
+ Status =3D LibFvbFlashDeviceBlockErase (LbaAddress, LbaLength);=0D
+=0D
+ LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);=0D
+=0D
+ WriteBackInvalidateDataCacheRange ((VOID *) LbaAddress, LbaLength);=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Modifies the current settings of the firmware volume according to the=0D
+ input parameter, and returns the new setting of the volume=0D
+=0D
+ @param[in] Instance The FV instance whose attributes is go=
ing to be=0D
+ modified=0D
+ @param[in] Attributes On input, it is a pointer to EFI_FVB_A=
TTRIBUTES_2=0D
+ containing the desired firmware volume=
settings.=0D
+ On successful return, it contains the =
new settings=0D
+ of the firmware volume=0D
+=0D
+ @retval EFI_SUCCESS Successfully returns=0D
+ @retval EFI_ACCESS_DENIED The volume setting is locked and canno=
t be modified=0D
+ @retval EFI_INVALID_PARAMETER Instance not found, or The attributes =
requested are=0D
+ in conflict with the capabilities as d=
eclared in the=0D
+ firmware volume header=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+FvbSetVolumeAttributes (=0D
+ IN UINTN Instance,=0D
+ IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_INSTANCE *FwhInstance;=0D
+ EFI_FVB_ATTRIBUTES_2 OldAttributes;=0D
+ EFI_FVB_ATTRIBUTES_2 *AttribPtr;=0D
+ EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;=0D
+ UINT32 Capabilities;=0D
+ UINT32 OldStatus, NewStatus;=0D
+=0D
+ //=0D
+ // Find the right instance of the FVB private data=0D
+ //=0D
+ FwhInstance =3D GetFvbInstance (Instance);=0D
+ if (FwhInstance =3D=3D NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ AttribPtr =3D (EFI_FVB_ATTRIBUTES_2 *) &(FwhInstance->VolumeHeader.Attri=
butes);=0D
+ ASSERT (AttribPtr !=3D NULL);=0D
+ if ( AttribPtr =3D=3D NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ OldAttributes =3D *AttribPtr;=0D
+ Capabilities =3D OldAttributes & EFI_FVB2_CAPABILITIES;=0D
+ OldStatus =3D OldAttributes & EFI_FVB2_STATUS;=0D
+ NewStatus =3D *Attributes & EFI_FVB2_STATUS;=0D
+=0D
+ UnchangedAttributes =3D EFI_FVB2_READ_DISABLED_CAP | \=0D
+ EFI_FVB2_READ_ENABLED_CAP | \=0D
+ EFI_FVB2_WRITE_DISABLED_CAP | \=0D
+ EFI_FVB2_WRITE_ENABLED_CAP | \=0D
+ EFI_FVB2_LOCK_CAP | \=0D
+ EFI_FVB2_STICKY_WRITE | \=0D
+ EFI_FVB2_MEMORY_MAPPED | \=0D
+ EFI_FVB2_ERASE_POLARITY | \=0D
+ EFI_FVB2_READ_LOCK_CAP | \=0D
+ EFI_FVB2_WRITE_LOCK_CAP | \=0D
+ EFI_FVB2_ALIGNMENT;=0D
+=0D
+ //=0D
+ // Some attributes of FV is read only can *not* be set=0D
+ //=0D
+ if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttr=
ibutes)) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ //=0D
+ // If firmware volume is locked, no status bit can be updated=0D
+ //=0D
+ if ( OldAttributes & EFI_FVB2_LOCK_STATUS ) {=0D
+ if ( OldStatus ^ NewStatus ) {=0D
+ return EFI_ACCESS_DENIED;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Test read disable=0D
+ //=0D
+ if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) =3D=3D 0) {=0D
+ if ((NewStatus & EFI_FVB2_READ_STATUS) =3D=3D 0) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Test read enable=0D
+ //=0D
+ if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) =3D=3D 0) {=0D
+ if (NewStatus & EFI_FVB2_READ_STATUS) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Test write disable=0D
+ //=0D
+ if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) =3D=3D 0) {=0D
+ if ((NewStatus & EFI_FVB2_WRITE_STATUS) =3D=3D 0) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Test write enable=0D
+ //=0D
+ if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) =3D=3D 0) {=0D
+ if (NewStatus & EFI_FVB2_WRITE_STATUS) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Test lock=0D
+ //=0D
+ if ((Capabilities & EFI_FVB2_LOCK_CAP) =3D=3D 0) {=0D
+ if (NewStatus & EFI_FVB2_LOCK_STATUS) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ }=0D
+=0D
+ *AttribPtr =3D (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));=0D
+ *AttribPtr =3D (*AttribPtr) | NewStatus;=0D
+ *Attributes =3D *AttribPtr;=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Retrieves the physical address of the device.=0D
+=0D
+ @param[in] This A pointer to EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL.=0D
+ @param[out] Address Output buffer containing the address.=0D
+=0D
+ @retval EFI_SUCCESS The function always return successfully.=0D
+ @retval EFI_INVALID_PARAMETER Instance not found.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetPhysicalAddress (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ OUT EFI_PHYSICAL_ADDRESS *Address=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+ EFI_FW_VOL_INSTANCE *FwhInstance;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ FwhInstance =3D GetFvbInstance(FvbDevice->Instance);=0D
+ if (FwhInstance =3D=3D NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ *Address =3D FwhInstance->FvBase;=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+=0D
+/**=0D
+ Retrieve the size of a logical block=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[in] Lba Indicates which block to return the size for.=0D
+ @param[out] BlockSize A pointer to a caller allocated UINTN in which=0D
+ the size of the block is returned=0D
+ @param[out] NumOfBlocks A pointer to a caller allocated UINTN in which t=
he=0D
+ number of consecutive blocks starting with Lba i=
s=0D
+ returned. All blocks in this range have a size o=
f=0D
+ BlockSize=0D
+=0D
+ @retval EFI_SUCCESS The function always return successfully.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetBlockSize (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ OUT UINTN *BlockSize,=0D
+ OUT UINTN *NumOfBlocks=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ return FvbGetLbaAddress (FvbDevice->Instance, Lba, NULL, BlockSize, NumO=
fBlocks);=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Retrieves Volume attributes. No polarity translations are done.=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[out] Attributes Output buffer which contains attributes=0D
+=0D
+ @retval EFI_SUCCESS The function always return successfully.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetAttributes (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ *Attributes =3D FvbGetVolumeAttributes (FvbDevice->Instance);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Sets Volume attributes. No polarity translations are done.=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[out] Attributes Output buffer which contains attributes=0D
+=0D
+ @retval EFI_SUCCESS The function always return successfully.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolSetAttributes (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ Status =3D FvbSetVolumeAttributes (FvbDevice->Instance, Attributes);=
=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+=0D
+/**=0D
+ This function erases one or more blocks as denoted by the=0D
+ variable argument list. The entire parameter list of blocks must be veri=
fied=0D
+ prior to erasing any blocks. If a block is requested that does not exis=
t=0D
+ within the associated firmware volume (it has a larger index than the la=
st=0D
+ block of the firmware volume), the EraseBlock() function must return=0D
+ EFI_INVALID_PARAMETER without modifying the contents of the firmware vol=
ume.=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[in] ... Starting LBA followed by Number of Lba to erase.=
=0D
+ a -1 to terminate the list.=0D
+=0D
+ @retval EFI_SUCCESS The erase request was successfully completed=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled st=
ate=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning correctly =
and=0D
+ could not be written. Firmware device may have=
been=0D
+ partially erased=0D
+=0D
+**/EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolEraseBlocks (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ ...=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+ EFI_FW_VOL_INSTANCE *FwhInstance;=0D
+ UINTN NumOfBlocks;=0D
+ VA_LIST args;=0D
+ EFI_LBA StartingLba;=0D
+ UINTN NumOfLba;=0D
+ EFI_STATUS Status;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ FwhInstance =3D GetFvbInstance (FvbDevice->Instance);=0D
+ if (FwhInstance =3D=3D NULL) {=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ NumOfBlocks =3D FwhInstance->NumOfBlocks;=0D
+ VA_START (args, This);=0D
+=0D
+ do {=0D
+ StartingLba =3D VA_ARG (args, EFI_LBA);=0D
+ if ( StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR ) {=0D
+ break;=0D
+ }=0D
+=0D
+ NumOfLba =3D VA_ARG (args, UINT32);=0D
+=0D
+ //=0D
+ // Check input parameters=0D
+ //=0D
+ if (NumOfLba =3D=3D 0) {=0D
+ VA_END (args);=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ if ( ( StartingLba + NumOfLba ) > NumOfBlocks ) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+ } while ( 1 );=0D
+=0D
+ VA_END (args);=0D
+=0D
+ VA_START (args, This);=0D
+ do {=0D
+ StartingLba =3D VA_ARG (args, EFI_LBA);=0D
+ if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) {=0D
+ break;=0D
+ }=0D
+=0D
+ NumOfLba =3D VA_ARG (args, UINT32);=0D
+=0D
+ while ( NumOfLba > 0 ) {=0D
+ Status =3D FvbEraseBlock (FvbDevice->Instance, StartingLba);=0D
+ if ( EFI_ERROR(Status)) {=0D
+ VA_END (args);=0D
+ return Status;=0D
+ }=0D
+ StartingLba++;=0D
+ NumOfLba--;=0D
+ }=0D
+ } while ( 1 );=0D
+=0D
+ VA_END (args);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+=0D
+/**=0D
+ Writes data beginning at Lba:Offset from FV. The write terminates either=
=0D
+ when *NumBytes of data have been written, or when a block boundary is=0D
+ reached. *NumBytes is updated to reflect the actual number of bytes=0D
+ written. The write opertion does not include erase. This routine will=0D
+ attempt to write only the specified bytes. If the writes do not stick,=0D
+ it will return an error.=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[in] Lba Block in which to begin write=0D
+ @param[in] Offset Offset in the block at which to begin write=0D
+ @param[in,out] NumBytes On input, indicates the requested write size. =
On=0D
+ output, indicates the actual number of bytes w=
ritten=0D
+ @param[in] Buffer Buffer containing source data for the write.=0D
+=0D
+ @retval EFI_SUCCESS The firmware volume was written successful=
ly=0D
+ @retval EFI_BAD_BUFFER_SIZE Write attempted across a LBA boundary. On =
output,=0D
+ NumBytes contains the total number of byte=
s=0D
+ actually written=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisable=
d state=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning correc=
tly and=0D
+ could not be written=0D
+ @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolWrite (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN Offset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ IN UINT8 *Buffer=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+ EFI_STATUS Status;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ Status =3D FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes,=
Buffer);=0D
+ DEBUG((DEBUG_VERBOSE,=0D
+ "FvbWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x Status=
:%r\n",=0D
+ Lba, Offset, *NumBytes, Buffer, Status));=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+=0D
+/**=0D
+ Reads data beginning at Lba:Offset from FV. The Read terminates either=0D
+ when *NumBytes of data have been read, or when a block boundary is=0D
+ reached. *NumBytes is updated to reflect the actual number of bytes=0D
+ written. The write opertion does not include erase. This routine will=0D
+ attempt to write only the specified bytes. If the writes do not stick,=0D
+ it will return an error.=0D
+=0D
+ @param[in] This Calling context=0D
+ @param[in] Lba Block in which to begin write=0D
+ @param[in] Offset Offset in the block at which to begin write=0D
+ @param[in,out] NumBytes On input, indicates the requested write size. =
On=0D
+ output, indicates the actual number of bytes w=
ritten=0D
+ @param[in] Buffer Buffer containing source data for the write.=0D
+=0D
+=0D
+Returns:=0D
+ @retval EFI_SUCCESS The firmware volume was read successfully =
and=0D
+ contents are in Buffer=0D
+ @retval EFI_BAD_BUFFER_SIZE Read attempted across a LBA boundary. On o=
utput,=0D
+ NumBytes contains the total number of byte=
s returned=0D
+ in Buffer=0D
+ @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled=
state=0D
+ @retval EFI_DEVICE_ERROR The block device is not functioning correc=
tly and=0D
+ could not be read=0D
+ @retval EFI_INVALID_PARAMETER NumBytes or Buffer are NULL=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolRead (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN Offset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ OUT UINT8 *Buffer=0D
+ )=0D
+{=0D
+=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+ EFI_STATUS Status;=0D
+=0D
+ FvbDevice =3D FVB_DEVICE_FROM_THIS (This);=0D
+ Status =3D FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, =
Buffer);=0D
+ DEBUG((DEBUG_VERBOSE,=0D
+ "FvbRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x, Status=
:%r\n",=0D
+ Lba, Offset, *NumBytes, Buffer, Status));=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Check the integrity of firmware volume header in FvBase=0D
+=0D
+ @param[in] FvBase A pointer to firmware volume base address.=0D
+=0D
+ @retval TRUE The firmware volume is consistent=0D
+ @retval FALSE The firmware volume has corrupted.=0D
+=0D
+**/=0D
+BOOLEAN=0D
+IsFvHeaderValid (=0D
+ IN EFI_PHYSICAL_ADDRESS FvBase=0D
+ )=0D
+{=0D
+ UINT16 Sum;=0D
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;=0D
+=0D
+ FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) FvBase;=0D
+ if (FvBase =3D=3D PcdGet32(PcdFlashNvStorageVariableBase)) {=0D
+ if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid,=
sizeof(EFI_GUID)) !=3D 0 ) {=0D
+ DEBUG((DEBUG_INFO, " --FileSystemGuid not match: %g\n", &FwVolHeade=
r->FileSystemGuid));=0D
+ return FALSE;=0D
+ }=0D
+ } else {=0D
+ if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2=
Guid, sizeof(EFI_GUID)) !=3D 0 ) {=0D
+ DEBUG((DEBUG_INFO, " --not expected guid.\n"));=0D
+ return FALSE;=0D
+ }=0D
+ }=0D
+=0D
+ if ( (FwVolHeader->Revision !=3D EFI_FVH_REVISION) ||=0D
+ (FwVolHeader->Signature !=3D EFI_FVH_SIGNATURE) ||=0D
+ (FwVolHeader->FvLength =3D=3D ((UINTN) -1)) ||=0D
+ ((FwVolHeader->HeaderLength & 0x01 ) !=3D0) ) {=0D
+ DEBUG((DEBUG_INFO, " -- >Revision =3D 0x%x, Signature =3D 0x%x\n", Fw=
VolHeader->Revision, FwVolHeader->Signature ));=0D
+ DEBUG((DEBUG_INFO, " -- >FvLength =3D 0x%lx, HeaderLength =3D 0x%x\n"=
, FwVolHeader->FvLength, FwVolHeader->HeaderLength ));=0D
+ return FALSE;=0D
+ }=0D
+=0D
+ Sum =3D CalculateSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLengt=
h);=0D
+ if (Sum !=3D 0) {=0D
+ DEBUG((DEBUG_INFO, "error: checksum: 0x%04X (expect 0x0)\n", Sum));=0D
+ return FALSE;=0D
+ }=0D
+=0D
+ return TRUE;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Get intial variable data.=0D
+=0D
+ @param[out] VarData Valid variable data.=0D
+ @param[out] VarSize Valid variable size.=0D
+=0D
+ @retval RETURN_SUCCESS Successfully found initial variable data.=
=0D
+ @retval RETURN_NOT_FOUND Failed to find the variable data file from=
FV.=0D
+ @retval EFI_INVALID_PARAMETER VarData or VarSize is null.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+GetInitialVariableData (=0D
+ OUT VOID **VarData,=0D
+ OUT UINTN *VarSize=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ VOID *ImageData;=0D
+ UINTN ImageSize;=0D
+ EFI_FIRMWARE_VOLUME_HEADER *FvHeader;=0D
+ VARIABLE_STORE_HEADER *VariableStore;=0D
+ AUTHENTICATED_VARIABLE_HEADER *Variable;=0D
+ UINTN VariableSize;=0D
+ UINTN VarEndAddr;=0D
+=0D
+ if ((VarData =3D=3D NULL) || (VarSize =3D=3D NULL)) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ Status =3D GetSectionFromAnyFv (PcdGetPtr(PcdNvsDataFile), EFI_SECTION_R=
AW, 0, &ImageData, &ImageSize);=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ FvHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *) ImageData;=0D
+ VariableStore =3D (VARIABLE_STORE_HEADER *) ((UINT8 *)ImageData + FvHead=
er->HeaderLength);=0D
+ VarEndAddr =3D (UINTN)VariableStore + VariableStore->Size;=0D
+ Variable =3D (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (Variab=
leStore + 1);=0D
+ *VarData =3D (VOID *)Variable;=0D
+ while (((UINTN)Variable < VarEndAddr)) {=0D
+ if (Variable->StartId !=3D VARIABLE_DATA) {=0D
+ break;=0D
+ }=0D
+ VariableSize =3D sizeof (AUTHENTICATED_VARIABLE_HEADER);=0D
+ VariableSize =3D sizeof (AUTHENTICATED_VARIABLE_HEADER) + Variable->Da=
taSize + Variable->NameSize;=0D
+ Variable =3D (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN ((UINT=
N) Variable + VariableSize);=0D
+ }=0D
+=0D
+ *VarSize =3D (UINTN)Variable - HEADER_ALIGN (VariableStore + 1);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+/**=0D
+ The function does the necessary initialization work for=0D
+ Firmware Volume Block Driver.=0D
+=0D
+ @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.=0D
+ It will ASSERT on errors.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+FvbInitialize (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_INSTANCE *FwVolInstance;=0D
+ EFI_FIRMWARE_VOLUME_HEADER *FvHeader;=0D
+ EFI_FV_BLOCK_MAP_ENTRY *BlockMap;=0D
+ EFI_PHYSICAL_ADDRESS BaseAddress;=0D
+ UINTN WriteAddr;=0D
+ EFI_STATUS Status;=0D
+ UINTN BufferSize;=0D
+ UINTN Length;=0D
+ VARIABLE_STORE_HEADER VariableStore;=0D
+ VOID *VarData;=0D
+=0D
+ InitVariableStore ();=0D
+ BaseAddress =3D PcdGet32(PcdFlashNvStorageVariableBase);=0D
+ FvHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;=0D
+=0D
+ //=0D
+ // Check FV header and variable store header=0D
+ //=0D
+ if (!IsFvHeaderValid (BaseAddress)) {=0D
+ //=0D
+ // Write back a healthy FV header=0D
+ //=0D
+ DEBUG ((DEBUG_ERROR, "Fvb: Writing back a healthy FV header: 0x%lx\n",=
BaseAddress));=0D
+ FvHeader =3D GetFvHeaderTemplate ();=0D
+ LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FvHeader->BlockMap->Le=
ngth, FALSE);=0D
+=0D
+ Status =3D LibFvbFlashDeviceBlockErase ((UINTN)BaseAddress, FvHeader->=
BlockMap->Length);=0D
+ ASSERT_EFI_ERROR(Status);=0D
+=0D
+ Length =3D FvHeader->HeaderLength;=0D
+ WriteAddr =3D (UINTN)BaseAddress;=0D
+ Status =3D LibFvbFlashDeviceWrite (WriteAddr, &Length, (UINT8 *) FvHea=
der);=0D
+ WriteAddr +=3D Length;=0D
+ ASSERT_EFI_ERROR(Status);=0D
+=0D
+ //=0D
+ // Write back variable store header=0D
+ //=0D
+ VariableStore.Size =3D PcdGet32(PcdFlashNvStorageVariableSize) - FvH=
eader->HeaderLength;=0D
+ VariableStore.Format =3D VARIABLE_STORE_FORMATTED;=0D
+ VariableStore.State =3D VARIABLE_STORE_HEALTHY;=0D
+ CopyGuid (&VariableStore.Signature, &gEfiAuthenticatedVariableGuid);=0D
+ BufferSize =3D sizeof (VARIABLE_STORE_HEADER);=0D
+ Status =3D LibFvbFlashDeviceWrite (WriteAddr, &BufferSize, (UINT8 *) &=
VariableStore);=0D
+ WriteAddr +=3D BufferSize;=0D
+ ASSERT_EFI_ERROR(Status);=0D
+=0D
+ //=0D
+ // Write initial variable data if found=0D
+ //=0D
+ Status =3D GetInitialVariableData (&VarData, &Length);=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D LibFvbFlashDeviceWrite (WriteAddr, &Length, (UINT8 *) Var=
Data);=0D
+ ASSERT_EFI_ERROR(Status);=0D
+ }=0D
+=0D
+ LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FvHeader->BlockMap->Le=
ngth, TRUE);=0D
+ WriteBackInvalidateDataCacheRange ((VOID *) (UINTN) BaseAddress, FvHea=
der->BlockMap->Length);=0D
+ }=0D
+=0D
+ //=0D
+ // Create a new FW volume instance for NVS variable=0D
+ //=0D
+ BufferSize =3D FvHeader->HeaderLength + sizeof (EFI_FW_VOL_INSTANCE) =
- sizeof (EFI_FIRMWARE_VOLUME_HEADER);=0D
+ FwVolInstance =3D (EFI_FW_VOL_INSTANCE *) AllocateRuntimeZeroPool (Buffe=
rSize);=0D
+ if (FwVolInstance =3D=3D NULL) {=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+ FwVolInstance->FvBase =3D (UINTN)BaseAddress;=0D
+ CopyMem (&FwVolInstance->VolumeHeader, FvHeader, FvHeader->HeaderLength)=
;=0D
+=0D
+ //=0D
+ // Process the block map for each FV. Assume it has same block size.=0D
+ //=0D
+ FwVolInstance->NumOfBlocks =3D 0;=0D
+ FvHeader =3D &FwVolInstance->VolumeHeader;=0D
+ for (BlockMap =3D FvHeader->BlockMap; BlockMap->NumBlocks !=3D 0; BlockM=
ap++) {=0D
+ FwVolInstance->NumOfBlocks +=3D BlockMap->NumBlocks;=0D
+ }=0D
+=0D
+ //=0D
+ // Add a FVB Protocol Instance=0D
+ //=0D
+ Status =3D InstallFvbProtocol (FwVolInstance, mFvbModuleGlobal.NumFv);=0D
+ mFvbModuleGlobal.NumFv++;=0D
+ mFvbModuleGlobal.FvInstance =3D FwVolInstance;=0D
+=0D
+ return Status;=0D
+}=0D
diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbService.h b/UefiPayloadPkg/Fvb=
RuntimeDxe/FvbService.h
new file mode 100644
index 0000000000..8eb5657b07
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbService.h
@@ -0,0 +1,187 @@
+/** @file=0D
+The header file for Firmware volume block driver.=0D
+=0D
+Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=0D
+SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#ifndef _FW_BLOCK_SERVICE_H_=0D
+#define _FW_BLOCK_SERVICE_H_=0D
+=0D
+#include <Guid/EventGroup.h>=0D
+#include <Guid/FirmwareFileSystem2.h>=0D
+#include <Guid/SystemNvDataGuid.h>=0D
+#include <Guid/VariableFormat.h>=0D
+#include <Protocol/DevicePath.h>=0D
+#include <Protocol/FirmwareVolumeBlock.h>=0D
+#include <Library/UefiDriverEntryPoint.h>=0D
+#include <Library/UefiBootServicesTableLib.h>=0D
+#include <Library/UefiLib.h>=0D
+#include <Library/BaseLib.h>=0D
+#include <Library/DebugLib.h>=0D
+#include <Library/BaseMemoryLib.h>=0D
+#include <Library/IoLib.h>=0D
+#include <Library/CacheMaintenanceLib.h>=0D
+#include <Library/MemoryAllocationLib.h>=0D
+#include <Library/PcdLib.h>=0D
+#include <Library/FlashDeviceLib.h>=0D
+#include <Library/DevicePathLib.h>=0D
+#include <Library/HobLib.h>=0D
+#include <Library/DxeServicesLib.h>=0D
+#include <Guid/NvVariableInfoGuid.h>=0D
+#include <Register/ArchitecturalMsr.h>=0D
+=0D
+//=0D
+// Define two helper macro to extract the Capability field or Status field=
in FVB=0D
+// bit fields=0D
+//=0D
+#define EFI_FVB2_CAPABILITIES (EFI_FVB2_READ_DISABLED_CAP | \=0D
+ EFI_FVB2_READ_ENABLED_CAP | \=0D
+ EFI_FVB2_WRITE_DISABLED_CAP | \=0D
+ EFI_FVB2_WRITE_ENABLED_CAP | \=0D
+ EFI_FVB2_LOCK_CAP \=0D
+ )=0D
+=0D
+#define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EF=
I_FVB2_LOCK_STATUS)=0D
+=0D
+=0D
+typedef struct {=0D
+ UINTN FvBase;=0D
+ UINTN NumOfBlocks;=0D
+ //=0D
+ // Note!!!: VolumeHeader must be the last element=0D
+ // of the structure.=0D
+ //=0D
+ EFI_FIRMWARE_VOLUME_HEADER VolumeHeader;=0D
+} EFI_FW_VOL_INSTANCE;=0D
+=0D
+=0D
+typedef struct {=0D
+ EFI_FW_VOL_INSTANCE *FvInstance;=0D
+ UINT32 NumFv;=0D
+ UINT32 Flags;=0D
+} FWB_GLOBAL;=0D
+=0D
+//=0D
+// Fvb Protocol instance data=0D
+//=0D
+#define FVB_DEVICE_FROM_THIS(a) CR(a, EFI_FW_VOL_BLOCK_DEVICE, FwVolBlockI=
nstance, FVB_DEVICE_SIGNATURE)=0D
+#define FVB_EXTEND_DEVICE_FROM_THIS(a) CR(a, EFI_FW_VOL_BLOCK_DEVICE, FvbE=
xtension, FVB_DEVICE_SIGNATURE)=0D
+#define FVB_DEVICE_SIGNATURE SIGNATURE_32('F','V','B','C')=0D
+=0D
+typedef struct {=0D
+ MEDIA_FW_VOL_DEVICE_PATH FvDevPath;=0D
+ EFI_DEVICE_PATH_PROTOCOL EndDevPath;=0D
+} FV_PIWG_DEVICE_PATH;=0D
+=0D
+typedef struct {=0D
+ MEMMAP_DEVICE_PATH MemMapDevPath;=0D
+ EFI_DEVICE_PATH_PROTOCOL EndDevPath;=0D
+} FV_MEMMAP_DEVICE_PATH;=0D
+=0D
+typedef struct {=0D
+ UINT32 Signature;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+ UINTN Instance;=0D
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL FwVolBlockInstance;=0D
+} EFI_FW_VOL_BLOCK_DEVICE;=0D
+=0D
+/**=0D
+ Get a heathy FV header used for variable store recovery=0D
+=0D
+ @retval The FV header.=0D
+=0D
+**/=0D
+EFI_FIRMWARE_VOLUME_HEADER *=0D
+GetFvHeaderTemplate (=0D
+ VOID=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+InitVariableStore (=0D
+ VOID=0D
+ );=0D
+=0D
+//=0D
+// Protocol APIs=0D
+//=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetAttributes (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolSetAttributes (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetPhysicalAddress (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ OUT EFI_PHYSICAL_ADDRESS *Address=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolGetBlockSize (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ OUT UINTN *BlockSize,=0D
+ OUT UINTN *NumOfBlocks=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolRead (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN Offset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ OUT UINT8 *Buffer=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolWrite (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ IN EFI_LBA Lba,=0D
+ IN UINTN Offset,=0D
+ IN OUT UINTN *NumBytes,=0D
+ IN UINT8 *Buffer=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbProtocolEraseBlocks (=0D
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,=0D
+ ...=0D
+ );=0D
+=0D
+EFI_FW_VOL_INSTANCE *=0D
+GetFvbInstance (=0D
+ IN UINTN Instance=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+InstallFvbProtocol (=0D
+ IN EFI_FW_VOL_INSTANCE *FwhInstance,=0D
+ IN UINTN InstanceNum=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+FvbInitialize (=0D
+ VOID=0D
+ );=0D
+=0D
+extern FWB_GLOBAL mFvbModuleGlobal;=0D
+extern EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate;=0D
+extern FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate;=0D
+extern FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate;=0D
+=0D
+#endif=0D
diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbServiceSmm.c b/UefiPayloadPkg/=
FvbRuntimeDxe/FvbServiceSmm.c
new file mode 100644
index 0000000000..0f1f4b369c
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbServiceSmm.c
@@ -0,0 +1,139 @@
+/** @file=0D
+ SMM Firmware Volume Block Driver.=0D
+=0D
+ Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#include <PiSmm.h>=0D
+#include <Library/SmmServicesTableLib.h>=0D
+#include "FvbSmmCommon.h"=0D
+#include "FvbService.h"=0D
+=0D
+/**=0D
+ The function installs EFI_SMM_FIRMWARE_VOLUME_BLOCK protocol=0D
+ for each FV in the system.=0D
+=0D
+ @param[in] FwhInstance The pointer to a FW volume instance structure,=
=0D
+ which contains the information about one FV.=0D
+ @param[in] InstanceNum The instance number which can be used as a ID=
=0D
+ to locate this FwhInstance in other functions.=
=0D
+=0D
+ @retval EFI_SUCESS Installed successfully.=0D
+ @retval Else Did not install successfully.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+InstallFvbProtocol (=0D
+ IN EFI_FW_VOL_INSTANCE *FwhInstance,=0D
+ IN UINTN InstanceNum=0D
+ )=0D
+{=0D
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;=0D
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;=0D
+ EFI_STATUS Status;=0D
+ EFI_HANDLE FvbHandle;=0D
+ FV_MEMMAP_DEVICE_PATH *FvDevicePath;=0D
+ VOID *TempPtr;=0D
+=0D
+ FvbDevice =3D (EFI_FW_VOL_BLOCK_DEVICE *) AllocateRuntimeCopyPool (=0D
+ sizeof (EFI_FW_VOL_BLOCK_DEVIC=
E),=0D
+ &mFvbDeviceTemplate=0D
+ );=0D
+ if (FvbDevice =3D=3D NULL) {=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ FvbDevice->Instance =3D InstanceNum;=0D
+ FwVolHeader =3D &FwhInstance->VolumeHeader;=0D
+=0D
+ //=0D
+ // Set up the devicepath=0D
+ //=0D
+ if (FwVolHeader->ExtHeaderOffset =3D=3D 0) {=0D
+ //=0D
+ // FV does not contains extension header, then produce MEMMAP_DEVICE_P=
ATH=0D
+ //=0D
+ TempPtr =3D AllocateRuntimeCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &=
mFvMemmapDevicePathTemplate);=0D
+ FvbDevice->DevicePath =3D (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;=0D
+ if (FvbDevice->DevicePath =3D=3D NULL) {=0D
+ ASSERT (FALSE);=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+ FvDevicePath =3D (FV_MEMMAP_DEVICE_PATH *) FvbDevice->DevicePath;=0D
+ FvDevicePath->MemMapDevPath.StartingAddress =3D FwhInstance->FvBase;=0D
+ FvDevicePath->MemMapDevPath.EndingAddress =3D FwhInstance->FvBase + =
FwVolHeader->FvLength - 1;=0D
+ } else {=0D
+ TempPtr =3D AllocateRuntimeCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mF=
vPIWGDevicePathTemplate);=0D
+ FvbDevice->DevicePath =3D (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;=0D
+ if (FvbDevice->DevicePath =3D=3D NULL) {=0D
+ ASSERT (FALSE);=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ CopyGuid (=0D
+ &((FV_PIWG_DEVICE_PATH *)FvbDevice->DevicePath)->FvDevPath.FvName,=0D
+ (GUID *)(UINTN)(FwhInstance->FvBase + FwVolHeader->ExtHeaderOffset)=
=0D
+ );=0D
+ }=0D
+=0D
+ //=0D
+ // Install the SMM Firmware Volume Block Protocol and Device Path Protoc=
ol=0D
+ //=0D
+ FvbHandle =3D NULL;=0D
+ Status =3D gSmst->SmmInstallProtocolInterface (=0D
+ &FvbHandle,=0D
+ &gEfiSmmFirmwareVolumeBlockProtocolGuid,=0D
+ EFI_NATIVE_INTERFACE,=0D
+ &FvbDevice->FwVolBlockInstance=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ Status =3D gSmst->SmmInstallProtocolInterface (=0D
+ &FvbHandle,=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ EFI_NATIVE_INTERFACE,=0D
+ FvbDevice->DevicePath=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ //=0D
+ // Notify the Fvb wrapper driver SMM fvb is ready=0D
+ //=0D
+ FvbHandle =3D NULL;=0D
+ Status =3D gBS->InstallProtocolInterface (=0D
+ &FvbHandle,=0D
+ &gEfiSmmFirmwareVolumeBlockProtocolGuid,=0D
+ EFI_NATIVE_INTERFACE,=0D
+ &FvbDevice->FwVolBlockInstance=0D
+ );=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ The driver entry point for SMM Firmware Volume Block Driver.=0D
+=0D
+ The function does the necessary initialization work=0D
+ Firmware Volume Block Driver.=0D
+=0D
+ @param[in] ImageHandle The firmware allocated handle for the UEFI=
image.=0D
+ @param[in] SystemTable A pointer to the EFI system table.=0D
+=0D
+ @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.=0D
+ It will ASSERT on errors.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+FvbSmmInitialize (=0D
+ IN EFI_HANDLE ImageHandle,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable=0D
+ )=0D
+{=0D
+ FvbInitialize ();=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf b/UefiPayloadPkg/FvbRu=
ntimeDxe/FvbSmm.inf
new file mode 100644
index 0000000000..2a262076d6
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
@@ -0,0 +1,71 @@
+## @file=0D
+# This driver installs the EFI_SMM_FIRMWARE_VOLUMEN_PROTOCOL.=0D
+#=0D
+#=0D
+# Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=
=0D
+#=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010005=0D
+ BASE_NAME =3D FvbSmm=0D
+ FILE_GUID =3D A4EC8ADB-B7A8-47d1-8E52-EC820D0ACF6F=
=0D
+ MODULE_TYPE =3D DXE_SMM_DRIVER=0D
+ VERSION_STRING =3D 1.0=0D
+ PI_SPECIFICATION_VERSION =3D 0x0001000A=0D
+ ENTRY_POINT =3D FvbSmmInitialize=0D
+=0D
+[Sources]=0D
+ FvbInfo.c=0D
+ FvbService.h=0D
+ FvbService.c=0D
+ FvbServiceSmm.c=0D
+ FvbSmmCommon.h=0D
+=0D
+[Packages]=0D
+ MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
+ UefiCpuPkg/UefiCpuPkg.dec=0D
+ UefiPayloadPkg/UefiPayloadPkg.dec=0D
+=0D
+[LibraryClasses]=0D
+ FlashDeviceLib=0D
+ PcdLib=0D
+ MemoryAllocationLib=0D
+ CacheMaintenanceLib=0D
+ IoLib=0D
+ BaseMemoryLib=0D
+ DebugLib=0D
+ BaseLib=0D
+ UefiLib=0D
+ SmmServicesTableLib=0D
+ UefiBootServicesTableLib=0D
+ UefiDriverEntryPoint=0D
+ HobLib=0D
+ DxeServicesLib=0D
+=0D
+[Guids]=0D
+ gEfiFirmwareFileSystem2Guid # ALWAYS_CONSUMED=0D
+ gEfiSystemNvDataFvGuid # ALWAYS_CONSUMED=0D
+ gEfiAuthenticatedVariableGuid=0D
+ gNvVariableInfoGuid=0D
+=0D
+ [Protocols]=0D
+ gEfiDevicePathProtocolGuid # PROTOCOL ALWAYS_PRODUCED=
=0D
+ gEfiSmmFirmwareVolumeBlockProtocolGuid # PROTOCOL ALWAYS_PRODUCED=
=0D
+=0D
+[Pcd]=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize=0D
+=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase=0D
+ gUefiPayloadPkgTokenSpaceGuid.PcdNvsDataFile=0D
+=0D
+[Depex]=0D
+ TRUE=0D
diff --git a/UefiPayloadPkg/FvbRuntimeDxe/FvbSmmCommon.h b/UefiPayloadPkg/F=
vbRuntimeDxe/FvbSmmCommon.h
new file mode 100644
index 0000000000..08577c78db
--- /dev/null
+++ b/UefiPayloadPkg/FvbRuntimeDxe/FvbSmmCommon.h
@@ -0,0 +1,69 @@
+/** @file=0D
+ The common header file for SMM FVB module.=0D
+=0D
+Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>=0D
+SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#ifndef _SMM_FVB_COMMON_H_=0D
+#define _SMM_FVB_COMMON_H_=0D
+=0D
+#include <Protocol/SmmFirmwareVolumeBlock.h>=0D
+=0D
+#define EFI_FUNCTION_GET_ATTRIBUTES 1=0D
+#define EFI_FUNCTION_SET_ATTRIBUTES 2=0D
+#define EFI_FUNCTION_GET_PHYSICAL_ADDRESS 3=0D
+#define EFI_FUNCTION_GET_BLOCK_SIZE 4=0D
+#define EFI_FUNCTION_READ 5=0D
+#define EFI_FUNCTION_WRITE 6=0D
+#define EFI_FUNCTION_ERASE_BLOCKS 7=0D
+=0D
+typedef struct {=0D
+ UINTN Function;=0D
+ EFI_STATUS ReturnStatus;=0D
+ UINT8 Data[1];=0D
+} SMM_FVB_COMMUNICATE_FUNCTION_HEADER;=0D
+=0D
+=0D
+///=0D
+/// Size of SMM communicate header, without including the payload.=0D
+///=0D
+#define SMM_COMMUNICATE_HEADER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADE=
R, Data))=0D
+=0D
+///=0D
+/// Size of SMM FVB communicate function header, without including the pay=
load.=0D
+///=0D
+#define SMM_FVB_COMMUNICATE_HEADER_SIZE (OFFSET_OF (SMM_FVB_COMMUNICATE_F=
UNCTION_HEADER, Data))=0D
+=0D
+typedef struct {=0D
+ EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *SmmFvb;=0D
+ EFI_FVB_ATTRIBUTES_2 Attributes;=0D
+} SMM_FVB_ATTRIBUTES_HEADER;=0D
+=0D
+typedef struct {=0D
+ EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *SmmFvb;=0D
+ EFI_PHYSICAL_ADDRESS Address;=0D
+} SMM_FVB_PHYSICAL_ADDRESS_HEADER;=0D
+=0D
+typedef struct {=0D
+ EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *SmmFvb;=0D
+ EFI_LBA Lba;=0D
+ UINTN BlockSize;=0D
+ UINTN NumOfBlocks;=0D
+} SMM_FVB_BLOCK_SIZE_HEADER;=0D
+=0D
+typedef struct {=0D
+ EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *SmmFvb;=0D
+ EFI_LBA Lba;=0D
+ UINTN Offset;=0D
+ UINTN NumBytes;=0D
+} SMM_FVB_READ_WRITE_HEADER;=0D
+=0D
+typedef struct {=0D
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *SmmFvb;=0D
+ EFI_LBA StartLba;=0D
+ UINTN NumOfLba;=0D
+} SMM_FVB_BLOCKS_HEADER;=0D
+=0D
+#endif=0D
diff --git a/UefiPayloadPkg/Include/Guid/NvVariableInfoGuid.h b/UefiPayload=
Pkg/Include/Guid/NvVariableInfoGuid.h
new file mode 100644
index 0000000000..a19ce9b287
--- /dev/null
+++ b/UefiPayloadPkg/Include/Guid/NvVariableInfoGuid.h
@@ -0,0 +1,30 @@
+/** @file=0D
+ This file defines the hob structure for the SPI flash variable info.=0D
+=0D
+ Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>=0D
+ This program and the accompanying materials=0D
+ are licensed and made available under the terms and conditions of the BS=
D License=0D
+ which accompanies this distribution. The full text of the license may b=
e found at=0D
+ http://opensource.org/licenses/bsd-license.php.=0D
+=0D
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,=0D
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP=
LIED.=0D
+=0D
+**/=0D
+=0D
+#ifndef __NV_VARIABLE_INFO_GUID_H__=0D
+#define __NV_VARIABLE_INFO_GUID_H__=0D
+=0D
+//=0D
+// NV variable hob info GUID=0D
+//=0D
+extern EFI_GUID gNvVariableInfoGuid;=0D
+=0D
+typedef struct {=0D
+ UINT8 Revision;=0D
+ UINT8 Reserved[3];=0D
+ UINT32 VariableStoreBase;=0D
+ UINT32 VariableStoreSize;=0D
+} NV_VARIABLE_INFO;=0D
+=0D
+#endif=0D
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec b/UefiPayloadPkg/UefiPayload=
Pkg.dec
index 2467dc76d6..024c4ed920 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dec
+++ b/UefiPayloadPkg/UefiPayloadPkg.dec
@@ -37,6 +37,8 @@
gUefiSerialPortInfoGuid =3D { 0x6c6872fe, 0x56a9, 0x4403, { 0xbb, 0x98,=
0x95, 0x8d, 0x62, 0xde, 0x87, 0xf1 } }=0D
gLoaderMemoryMapInfoGuid =3D { 0xa1ff7424, 0x7a1a, 0x478e, { 0xa9, 0xe4,=
0x92, 0xf3, 0x57, 0xd1, 0x28, 0x32 } }=0D
=0D
+ # SMM variable support=0D
+ gNvVariableInfoGuid =3D { 0x7a345dca, 0xc26, 0x4f2a, { 0xa8, 0x9a,=
0x57, 0xc0, 0x8d, 0xdd, 0x22, 0xee } }=0D
gSpiFlashInfoGuid =3D { 0x2d4aac1b, 0x91a5, 0x4cd5, { 0x9b, 0x5c,=
0xb4, 0x0f, 0x5d, 0x28, 0x51, 0xa1 } }=0D
gSmmRegisterInfoGuid =3D { 0xaa9bd7a7, 0xcafb, 0x4499, { 0xa4, 0xa9,=
0xb, 0x34, 0x6b, 0x40, 0xa6, 0x22 } }=0D
gS3CommunicationGuid =3D { 0x88e31ba1, 0x1856, 0x4b8b, { 0xbb, 0xdf,=
0xf8, 0x16, 0xdd, 0x94, 0xa, 0xef } }=0D
@@ -81,3 +83,7 @@ gUefiPayloadPkgTokenSpaceGuid.PcdMemoryTypeEfiRuntimeServ=
icesCode|0x80|UINT32|0x
gUefiPayloadPkgTokenSpaceGuid.PcdSystemMemoryUefiRegionSize|0x02000000|UIN=
T32|0x00000017=0D
=0D
gUefiPayloadPkgTokenSpaceGuid.PcdPcdDriverFile|{ 0x57, 0x72, 0xcf, 0x80, 0=
xab, 0x87, 0xf9, 0x47, 0xa3, 0xfe, 0xD5, 0x0B, 0x76, 0xd8, 0x95, 0x41 }|VOI=
D*|0x00000018=0D
+=0D
+## FFS filename to find the default variable initial data file.=0D
+# @Prompt FFS Name of variable initial data file=0D
+ gUefiPayloadPkgTokenSpaceGuid.PcdNvsDataFile |{ 0x1a, 0xf1, 0xb1, 0xae, 0=
x42, 0xcc, 0xcf, 0x4e, 0xac, 0x60, 0xdb, 0xab, 0xf6, 0xca, 0x69, 0xe6 }|VOI=
D*|0x00000025=0D
--=20
2.32.0.windows.2