public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry
@ 2022-03-21  9:10 Sean Rhodes
  2022-03-21  9:10 ` [PATCH 2/5] UefiPayloadPkg/Include/Coreboot: Add headers for SMMSTOREv2 table Sean Rhodes
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Sean Rhodes @ 2022-03-21  9:10 UTC (permalink / raw)
  To: devel
  Cc: Patrick Rudolph, Guo Dong, Ray Ni, Maurice Ma, Benjamin You,
	Sean Rhodes

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Remove a duplicated entry to fix a CI error.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/UefiPayloadPkg.ci.yaml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
index 93f4d3603d..909379eaac 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
+++ b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
@@ -20,7 +20,6 @@
         "IgnoreFiles": [
             "Include/Coreboot.h",
             "Library/CbParseLib/CbParseLib.c",
-            "Library/CbParseLib/CbParseLib.c",
             "PayloadLoaderPeim/ElfLib/ElfCommon.h",
             "PayloadLoaderPeim/ElfLib/Elf32.h",
             "PayloadLoaderPeim/ElfLib/Elf64.h"
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 2/5] UefiPayloadPkg/Include/Coreboot: Add headers for SMMSTOREv2 table
  2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
@ 2022-03-21  9:10 ` Sean Rhodes
  2022-03-21  9:10 ` [PATCH 3/5] UefiPayloadPkg: Add SmmStoreInfoGuid Sean Rhodes
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Sean Rhodes @ 2022-03-21  9:10 UTC (permalink / raw)
  To: devel
  Cc: Patrick Rudolph, Guo Dong, Ray Ni, Maurice Ma, Benjamin You,
	Sean Rhodes

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
the SMMSTORE v2 feature. It implements a SMI handler that is able to
write, read and erase pages in the boot media (SPI flash).
The existence of this optional feature is advertised by a coreboot table.

Add the tag and headers to parse the table.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/Include/Coreboot.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Coreboot.h
index a3e1109fe8..617e5e9bc0 100644
--- a/UefiPayloadPkg/Include/Coreboot.h
+++ b/UefiPayloadPkg/Include/Coreboot.h
@@ -236,6 +236,19 @@ struct cb_cbmem_tab {
   UINT64    cbmem_tab;
 };
 
+#define CB_TAG_SMMSTOREV2  0x0039
+struct cb_smmstorev2 {
+  UINT32    tag;
+  UINT32    size;
+  UINT32    num_blocks;      /* Number of writeable blocks in Smm */
+  UINT32    block_size;      /* Size of a block in byte. Default: 64 KiB */
+  UINT32    mmap_addr;       /* MMIO address of the store for read only access */
+  UINT32    com_buffer;      /* Physical address of the communication buffer */
+  UINT32    com_buffer_size; /* Size of the communication buffer in byte */
+  UINT8     apm_cmd;         /* The command byte to write to the APM I/O port */
+  UINT8     unused[3];       /* Set to zero */
+};
+
 /* Helpful macros */
 
 #define MEM_RANGE_COUNT(_rec) \
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 3/5] UefiPayloadPkg: Add SmmStoreInfoGuid
  2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
  2022-03-21  9:10 ` [PATCH 2/5] UefiPayloadPkg/Include/Coreboot: Add headers for SMMSTOREv2 table Sean Rhodes
@ 2022-03-21  9:10 ` Sean Rhodes
  2022-03-21  9:10 ` [PATCH 4/5] UefiPayloadPkg: Add SmmStoreLib Sean Rhodes
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Sean Rhodes @ 2022-03-21  9:10 UTC (permalink / raw)
  To: devel
  Cc: Patrick Rudolph, Guo Dong, Ray Ni, Maurice Ma, Benjamin You,
	Sean Rhodes

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Add a new InfoHob that contains the SmmStore information passed from
coreboot tables when the SMMSTOREV2 feature is enabled.

This will be used to implement the FVB in top of the MM installed by
coreboot.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 .../Include/Guid/SmmStoreInfoGuid.h           | 27 ++++++++++++
 .../Include/Library/SmmStoreParseLib.h        | 29 +++++++++++++
 .../Library/CbParseLib/CbParseLib.c           | 43 +++++++++++++++++++
 .../Library/SblParseLib/SblParseLib.c         | 18 ++++++++
 .../UefiPayloadEntry/UefiPayloadEntry.c       | 13 ++++++
 .../UefiPayloadEntry/UefiPayloadEntry.h       |  2 +
 .../UefiPayloadEntry/UefiPayloadEntry.inf     |  1 +
 UefiPayloadPkg/UefiPayloadPkg.dec             |  2 +
 8 files changed, 135 insertions(+)
 create mode 100644 UefiPayloadPkg/Include/Guid/SmmStoreInfoGuid.h
 create mode 100644 UefiPayloadPkg/Include/Library/SmmStoreParseLib.h

diff --git a/UefiPayloadPkg/Include/Guid/SmmStoreInfoGuid.h b/UefiPayloadPkg/Include/Guid/SmmStoreInfoGuid.h
new file mode 100644
index 0000000000..d5dcb8218f
--- /dev/null
+++ b/UefiPayloadPkg/Include/Guid/SmmStoreInfoGuid.h
@@ -0,0 +1,27 @@
+/** @file
+  This file defines the hob structure for coreboot's SmmStore.
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef SMMSTORE_GUID_H_
+#define SMMSTORE_GUID_H_
+
+///
+/// System Table Information GUID
+///
+extern EFI_GUID  gEfiSmmStoreInfoHobGuid;
+
+typedef struct {
+  UINT64    ComBuffer;
+  UINT32    ComBufferSize;
+  UINT32    NumBlocks;
+  UINT32    BlockSize;
+  UINT64    MmioAddress;
+  UINT8     ApmCmd;
+  UINT8     Reserved0[3];
+} SMMSTORE_INFO;
+
+#endif // SMMSTORE_GUID_H_
diff --git a/UefiPayloadPkg/Include/Library/SmmStoreParseLib.h b/UefiPayloadPkg/Include/Library/SmmStoreParseLib.h
new file mode 100644
index 0000000000..0b5b8f8aba
--- /dev/null
+++ b/UefiPayloadPkg/Include/Library/SmmStoreParseLib.h
@@ -0,0 +1,29 @@
+/** @file
+  This library will parse the coreboot table in memory and extract those required
+  information.
+
+  Copyright (c) 2021, Star Labs Systems. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef SMM_STORE_PARSE_LIB_H_
+#define SMM_STORE_PARSE_LIB_H_
+
+#include <Guid/SmmStoreInfoGuid.h>
+
+/**
+  Find the SmmStore HOB.
+
+  @param  SmmStoreInfo       Pointer to the SMMSTORE_INFO structure
+
+  @retval RETURN_SUCCESS     Successfully find the Smm store buffer information.
+  @retval RETURN_NOT_FOUND   Failed to find the Smm store buffer information .
+**/
+RETURN_STATUS
+EFIAPI
+ParseSmmStoreInfo (
+  OUT SMMSTORE_INFO  *SmmStoreInfo
+  );
+
+#endif // SMM_STORE_PARSE_LIB_H_
diff --git a/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c b/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c
index 8a353f77f6..5c7efec3ec 100644
--- a/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c
+++ b/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c
@@ -14,6 +14,7 @@
 #include <Library/PcdLib.h>
 #include <Library/IoLib.h>
 #include <Library/BlParseLib.h>
+#include <Library/SmmStoreParseLib.h>
 #include <IndustryStandard/Acpi.h>
 #include <Coreboot.h>
 
@@ -604,3 +605,45 @@ ParseMiscInfo (
 {
   return RETURN_SUCCESS;
 }
+
+/**
+  Find the SmmStore HOB.
+
+  @param  SmmStoreInfo       Pointer to the SMMSTORE_INFO structure
+
+  @retval RETURN_SUCCESS     Successfully find the Smm store buffer information.
+  @retval RETURN_NOT_FOUND   Failed to find the Smm store buffer information .
+**/
+RETURN_STATUS
+EFIAPI
+ParseSmmStoreInfo (
+  OUT SMMSTORE_INFO  *SmmStoreInfo
+  )
+{
+  struct cb_smmstorev2  *CbSSRec;
+
+  if (SmmStoreInfo == NULL) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  CbSSRec = FindCbTag (CB_TAG_SMMSTOREV2);
+  if (CbSSRec == NULL) {
+    return RETURN_NOT_FOUND;
+  }
+
+  DEBUG ((DEBUG_INFO, "Found Smm Store information\n"));
+  DEBUG ((DEBUG_INFO, "block size: 0x%x\n", CbSSRec->block_size));
+  DEBUG ((DEBUG_INFO, "number of blocks: 0x%x\n", CbSSRec->num_blocks));
+  DEBUG ((DEBUG_INFO, "communication buffer: 0x%x\n", CbSSRec->com_buffer));
+  DEBUG ((DEBUG_INFO, "communication buffer size: 0x%x\n", CbSSRec->com_buffer_size));
+  DEBUG ((DEBUG_INFO, "MMIO address of store: 0x%x\n", CbSSRec->mmap_addr));
+
+  SmmStoreInfo->ComBuffer     = CbSSRec->com_buffer;
+  SmmStoreInfo->ComBufferSize = CbSSRec->com_buffer_size;
+  SmmStoreInfo->BlockSize     = CbSSRec->block_size;
+  SmmStoreInfo->NumBlocks     = CbSSRec->num_blocks;
+  SmmStoreInfo->MmioAddress   = CbSSRec->mmap_addr;
+  SmmStoreInfo->ApmCmd        = CbSSRec->apm_cmd;
+
+  return RETURN_SUCCESS;
+}
diff --git a/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c b/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c
index d88238bfdc..6875fa9934 100644
--- a/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c
+++ b/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c
@@ -14,6 +14,7 @@
 #include <Library/IoLib.h>
 #include <Library/HobLib.h>
 #include <Library/BlParseLib.h>
+#include <Library/SmmStoreParseLib.h>
 #include <IndustryStandard/Acpi.h>
 #include <UniversalPayload/PciRootBridges.h>
 
@@ -289,3 +290,20 @@ ParseMiscInfo (
 
   return Status;
 }
+
+/**
+  Find the SmmStore HOB.
+
+  @param  SmmStoreInfo       Pointer to the SMMSTORE_INFO structure
+
+  @retval RETURN_SUCCESS     Successfully find the Smm store buffer information.
+  @retval RETURN_NOT_FOUND   Failed to find the Smm store buffer information .
+**/
+RETURN_STATUS
+EFIAPI
+ParseSmmStoreInfo (
+  OUT SMMSTORE_INFO  *SmmStoreInfo
+  )
+{
+  return RETURN_NOT_FOUND;
+}
diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
index 780348eadf..7f37283515 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
@@ -259,6 +259,8 @@ BuildHobFromBl (
 {
   EFI_STATUS                        Status;
   ACPI_BOARD_INFO                   *AcpiBoardInfo;
+  SMMSTORE_INFO                     SmmStoreInfo;
+  SMMSTORE_INFO                     *NewSmmStoreInfo;
   EFI_PEI_GRAPHICS_INFO_HOB         GfxInfo;
   EFI_PEI_GRAPHICS_INFO_HOB         *NewGfxInfo;
   EFI_PEI_GRAPHICS_DEVICE_INFO_HOB  GfxDeviceInfo;
@@ -305,6 +307,17 @@ BuildHobFromBl (
     DEBUG ((DEBUG_INFO, "Created graphics device info hob\n"));
   }
 
+  //
+  // Create guid hob for SmmStore
+  //
+  Status = ParseSmmStoreInfo (&SmmStoreInfo);
+  if (!EFI_ERROR (Status)) {
+    NewSmmStoreInfo = BuildGuidHob (&gEfiSmmStoreInfoHobGuid, sizeof (SmmStoreInfo));
+    ASSERT (NewSmmStoreInfo != NULL);
+    CopyMem (NewSmmStoreInfo, &SmmStoreInfo, sizeof (SmmStoreInfo));
+    DEBUG ((DEBUG_INFO, "Created SmmStore info hob\n"));
+  }
+
   //
   // Creat SmBios table Hob
   //
diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index 56e0a4c639..037ebbda36 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -22,6 +22,7 @@
 #include <Library/IoLib.h>
 #include <Library/PeCoffLib.h>
 #include <Library/BlParseLib.h>
+#include <Library/SmmStoreParseLib.h>
 #include <Library/PlatformSupportLib.h>
 #include <Library/UefiCpuLib.h>
 #include <IndustryStandard/Acpi.h>
@@ -36,6 +37,7 @@
 #include <UniversalPayload/ExtraData.h>
 #include <UniversalPayload/SerialPortInfo.h>
 #include <Guid/PcdDataBaseSignatureGuid.h>
+#include <Guid/SmmStoreInfoGuid.h>
 
 #define LEGACY_8259_MASK_REGISTER_MASTER  0x21
 #define LEGACY_8259_MASK_REGISTER_SLAVE   0xA1
diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf
index c4e4339ede..6dcd6d956d 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf
@@ -65,6 +65,7 @@
   gUniversalPayloadSmbiosTableGuid
   gUniversalPayloadAcpiTableGuid
   gUniversalPayloadSerialPortInfoGuid
+  gEfiSmmStoreInfoHobGuid
 
 [FeaturePcd.IA32]
   gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode      ## CONSUMES
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec b/UefiPayloadPkg/UefiPayloadPkg.dec
index 4051172caf..fcf62dd8c8 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dec
+++ b/UefiPayloadPkg/UefiPayloadPkg.dec
@@ -42,6 +42,8 @@
   gSmmRegisterInfoGuid     = { 0xaa9bd7a7, 0xcafb, 0x4499, { 0xa4, 0xa9, 0xb, 0x34, 0x6b, 0x40, 0xa6, 0x22 } }
   gS3CommunicationGuid     = { 0x88e31ba1, 0x1856, 0x4b8b, { 0xbb, 0xdf, 0xf8, 0x16, 0xdd, 0x94, 0xa, 0xef } }
 
+  gEfiSmmStoreInfoHobGuid  = { 0xf585ca19, 0x881b, 0x44fb, { 0x3f, 0x3d, 0x81, 0x89, 0x7c, 0x57, 0xbb, 0x01 } }
+
 [Ppis]
   gEfiPayLoadHobBasePpiGuid = { 0xdbe23aa1, 0xa342, 0x4b97, {0x85, 0xb6, 0xb2, 0x26, 0xf1, 0x61, 0x73, 0x89} }
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 4/5] UefiPayloadPkg: Add SmmStoreLib
  2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
  2022-03-21  9:10 ` [PATCH 2/5] UefiPayloadPkg/Include/Coreboot: Add headers for SMMSTOREv2 table Sean Rhodes
  2022-03-21  9:10 ` [PATCH 3/5] UefiPayloadPkg: Add SmmStoreInfoGuid Sean Rhodes
@ 2022-03-21  9:10 ` Sean Rhodes
  2022-03-21  9:10 ` [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Sean Rhodes
  2022-03-23 17:58 ` [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Guo Dong
  4 siblings, 0 replies; 14+ messages in thread
From: Sean Rhodes @ 2022-03-21  9:10 UTC (permalink / raw)
  To: devel
  Cc: Patrick Rudolph, Guo Dong, Ray Ni, Maurice Ma, Benjamin You,
	Sean Rhodes

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Implement all of the FVB protocol functions on top of the SmmStore
as a library. The library consumes the introduced gEfiSmmStoreInfoHobGuid.

The SMI handler uses a fixed communication buffer in reserved DRAM.
To initiate a transaction you must write to the I/O APM_CNT port.

Tests on Intel(R) Xeon(R) E-2288G CPU @ 3.70G showed that the SMI isn't
triggered with a probability of 1:40 of all cases when called in a tight
loop. The CPU continues running and the SMI is triggeres asynchronously
a few clock cycles later. coreboot only handels synchronous APM request
and does nothing on asynchronous APM triggers.

As there's no livesign from SMM it's impossible to tell if the handler
has run. Just wait a bit and try again to trigger a synchronous SMI.

Tests confirmed that out of 5 million tries the SMI is now always handled.

When a synchronous SMI happens with the correct write to the APM_CNT port,
the ebx register is checked first that it doesn't point to SMRAM.
If it doesn't it's used to read in the arguments that define a SmmStore
transaction.

The SMI handler will only operate on a predefined and memory mapped region in
the boot media.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/Include/Library/SmmStoreLib.h  | 120 +++++
 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c | 473 ++++++++++++++++++
 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h |  81 +++
 .../Library/SmmStoreLib/SmmStoreLib.inf       |  41 ++
 .../Library/SmmStoreLib/X64/SmmStore.nasm     |  48 ++
 UefiPayloadPkg/UefiPayloadPkg.dsc             |   1 +
 6 files changed, 764 insertions(+)
 create mode 100644 UefiPayloadPkg/Include/Library/SmmStoreLib.h
 create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c
 create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h
 create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
 create mode 100644 UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm

diff --git a/UefiPayloadPkg/Include/Library/SmmStoreLib.h b/UefiPayloadPkg/Include/Library/SmmStoreLib.h
new file mode 100644
index 0000000000..6ade375b1d
--- /dev/null
+++ b/UefiPayloadPkg/Include/Library/SmmStoreLib.h
@@ -0,0 +1,120 @@
+/** @file  SmmStoreLib.h
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef SMM_STORE_LIB_H_
+#define SMM_STORE_LIB_H_
+
+#include <Base.h>
+#include <Uefi/UefiBaseType.h>
+#include <Guid/SmmStoreInfoGuid.h>
+
+#define SMMSTORE_COMBUF_SIZE  16
+
+/**
+  Get the SmmStore block size
+
+  @param BlockSize    The pointer to store the block size in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetBlockSize (
+  OUT UINTN  *BlockSize
+  );
+
+/**
+  Get the SmmStore number of blocks
+
+  @param NumBlocks    The pointer to store the number of blocks in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetNumBlocks (
+  OUT UINTN  *NumBlocks
+  );
+
+/**
+  Get the SmmStore MMIO address
+
+  @param MmioAddress    The pointer to store the address in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetMmioAddress (
+  OUT EFI_PHYSICAL_ADDRESS  *MmioAddress
+  );
+
+/**
+  Read from SmmStore
+
+  @param[in] Lba      The starting logical block index to read from.
+  @param[in] Offset   Offset into the block at which to begin reading.
+  @param[in] NumBytes On input, indicates the requested read size. On
+                      output, indicates the actual number of bytes read
+  @param[in] Buffer   Pointer to the buffer to read into.
+
+**/
+EFI_STATUS
+SmmStoreLibRead (
+  IN        EFI_LBA  Lba,
+  IN        UINTN    Offset,
+  IN        UINTN    *NumBytes,
+  IN        UINT8    *Buffer
+  );
+
+/**
+  Write to SmmStore
+
+  @param[in] Lba      The starting logical block index to write to.
+  @param[in] Offset   Offset into the block at which to begin writing.
+  @param[in] NumBytes On input, indicates the requested write size. On
+                      output, indicates the actual number of bytes written
+  @param[in] Buffer   Pointer to the data to write.
+
+**/
+EFI_STATUS
+SmmStoreLibWrite (
+  IN        EFI_LBA  Lba,
+  IN        UINTN    Offset,
+  IN        UINTN    *NumBytes,
+  IN        UINT8    *Buffer
+  );
+
+/**
+  Erase a block using the SmmStore
+
+  @param Lba    The logical block index to erase.
+
+**/
+EFI_STATUS
+SmmStoreLibEraseBlock (
+  IN         EFI_LBA  Lba
+  );
+
+/**
+  Initializes SmmStore support
+
+  @retval EFI_WRITE_PROTECTED   The SmmStore is not present.
+  @retval EFI_UNSUPPORTED       The SmmStoreInfo HOB wasn't found.
+  @retval EFI_SUCCESS           The SmmStore is supported.
+
+**/
+EFI_STATUS
+SmmStoreLibInitialize (
+  VOID
+  );
+
+/**
+  Denitializes SmmStore support
+**/
+VOID
+EFIAPI
+SmmStoreLibDeinitialize (
+  VOID
+  );
+
+#endif /* SMM_STORE_LIB_H_ */
diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c
new file mode 100644
index 0000000000..25426a4d1a
--- /dev/null
+++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.c
@@ -0,0 +1,473 @@
+/** @file  SmmStore.c
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+#include <PiDxe.h>
+
+#include <Library/DebugLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Library/HobLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiRuntimeLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/SmmStoreLib.h>
+#include "SmmStore.h"
+
+/*
+ * A memory buffer to place arguments in.
+ */
+STATIC SMM_STORE_COM_BUF     *mArgComBuf;
+STATIC EFI_PHYSICAL_ADDRESS  mArgComBufPhys;
+
+/*
+ * Metadata provided by the first stage bootloader.
+ */
+STATIC SMMSTORE_INFO  *mSmmStoreInfo;
+
+STATIC EFI_EVENT  mSmmStoreLibVirtualAddrChangeEvent;
+
+/**
+  Calls into SMM to use the SMMSTOREv2 implementation for persistent storage.
+
+  @param Cmd     The command to write into the APM port. This allows to enter the
+                 Smi special command handler.
+  @param SubCmd  The subcommand to execute in the Smi handler.
+  @param Arg     Optional argument to pass to the Smi handler. Typically a pointer
+                 in 'flat' memory mode, which points to read only memory.
+
+  @retval EFI_NO_RESPONSE       The SmmStore is not present or didn't response.
+  @retval EFI_UNSUPPORTED       The request isn't suppored.
+  @retval EFI_DEVICE_ERROR      An error occured while executing the request.
+  @retval EFI_SUCCESS           The operation was executed successfully.
+**/
+STATIC
+EFI_STATUS
+CallSmm (
+  UINT8  Cmd,
+  UINT8  SubCmd,
+  UINTN  Arg
+  )
+{
+  CONST UINTN  Rax = ((SubCmd << 8) | Cmd);
+  CONST UINTN  Rbx = Arg;
+  UINTN        Result;
+
+  Result = TriggerSmi (Rax, Rbx, 5);
+  if (Result == Rax) {
+    return EFI_NO_RESPONSE;
+  } else if (Result == SMMSTORE_RET_SUCCESS) {
+    return EFI_SUCCESS;
+  } else if (Result == SMMSTORE_RET_UNSUPPORTED) {
+    return EFI_UNSUPPORTED;
+  }
+
+  return EFI_DEVICE_ERROR;
+}
+
+/**
+  Get the SmmStore block size
+
+  @param BlockSize    The pointer to store the block size in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetBlockSize (
+  OUT UINTN  *BlockSize
+  )
+{
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (BlockSize == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *BlockSize = mSmmStoreInfo->BlockSize;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Get the SmmStore number of blocks
+
+  @param NumBlocks    The pointer to store the number of blocks in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetNumBlocks (
+  OUT UINTN  *NumBlocks
+  )
+{
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (NumBlocks == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *NumBlocks = mSmmStoreInfo->NumBlocks;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Get the SmmStore MMIO address
+
+  @param MmioAddress    The pointer to store the address in.
+
+**/
+EFI_STATUS
+SmmStoreLibGetMmioAddress (
+  OUT EFI_PHYSICAL_ADDRESS  *MmioAddress
+  )
+{
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (MmioAddress == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *MmioAddress = mSmmStoreInfo->MmioAddress;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Read from SmmStore
+
+  @param[in] Lba      The starting logical block index to read from.
+  @param[in] Offset   Offset into the block at which to begin reading.
+  @param[in] NumBytes On input, indicates the requested read size. On
+                      output, indicates the actual number of bytes read
+  @param[in] Buffer   Pointer to the buffer to read into.
+
+**/
+EFI_STATUS
+SmmStoreLibRead (
+  IN        EFI_LBA  Lba,
+  IN        UINTN    Offset,
+  IN        UINTN    *NumBytes,
+  IN        UINT8    *Buffer
+  )
+{
+  EFI_STATUS  Status;
+
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (Lba >= mSmmStoreInfo->NumBlocks) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) ||
+      ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize))
+  {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  mArgComBuf->Read.BufSize   = *NumBytes;
+  mArgComBuf->Read.BufOffset = Offset;
+  mArgComBuf->Read.BlockId   = Lba;
+
+  Status = CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_READ, mArgComBufPhys);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  CopyMem (Buffer, (VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), *NumBytes);
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Write to SmmStore
+
+  @param[in] Lba      The starting logical block index to write to.
+  @param[in] Offset   Offset into the block at which to begin writing.
+  @param[in] NumBytes On input, indicates the requested write size. On
+                      output, indicates the actual number of bytes written
+  @param[in] Buffer   Pointer to the data to write.
+
+**/
+EFI_STATUS
+SmmStoreLibWrite (
+  IN        EFI_LBA  Lba,
+  IN        UINTN    Offset,
+  IN        UINTN    *NumBytes,
+  IN        UINT8    *Buffer
+  )
+{
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (Lba >= mSmmStoreInfo->NumBlocks) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) ||
+      ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize))
+  {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  mArgComBuf->Write.BufSize   = *NumBytes;
+  mArgComBuf->Write.BufOffset = Offset;
+  mArgComBuf->Write.BlockId   = Lba;
+
+  CopyMem ((VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), Buffer, *NumBytes);
+
+  return CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_WRITE, mArgComBufPhys);
+}
+
+/**
+  Erase a SmmStore block
+
+  @param Lba    The logical block index to erase.
+
+**/
+EFI_STATUS
+SmmStoreLibEraseBlock (
+  IN   EFI_LBA  Lba
+  )
+{
+  if (mSmmStoreInfo == NULL) {
+    return EFI_NO_MEDIA;
+  }
+
+  if (Lba >= mSmmStoreInfo->NumBlocks) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  mArgComBuf->Clear.BlockId = Lba;
+
+  return CallSmm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_CLEAR, mArgComBufPhys);
+}
+
+/**
+  Fixup internal data so that EFI can be call in virtual mode.
+  Call the passed in Child Notify event and convert any pointers in
+  lib to virtual mode.
+
+  @param[in]    Event   The Event that is being processed
+  @param[in]    Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+SmmStoreLibVirtualNotifyEvent (
+  IN EFI_EVENT  Event,
+  IN VOID       *Context
+  )
+{
+  EfiConvertPointer (0x0, (VOID **)&mArgComBuf);
+  if (mSmmStoreInfo != NULL) {
+    EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo->ComBuffer);
+    EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo);
+  }
+
+  return;
+}
+
+/**
+  Initializes SmmStore support
+
+  @retval EFI_WRITE_PROTECTED   The SmmStore is not present.
+  @retval EFI_OUT_OF_RESOURCES  Run out of memory.
+  @retval EFI_SUCCESS           The SmmStore is supported.
+
+**/
+EFI_STATUS
+SmmStoreLibInitialize (
+  VOID
+  )
+{
+  EFI_STATUS                       Status;
+  VOID                             *GuidHob;
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR  GcdDescriptor;
+
+  //
+  // Find the SmmStore information guid hob
+  //
+  GuidHob = GetFirstGuidHob (&gEfiSmmStoreInfoHobGuid);
+  if (GuidHob == NULL) {
+    DEBUG ((DEBUG_WARN, "SmmStore not supported! Skipping driver init.\n"));
+    return EFI_UNSUPPORTED;
+  }
+
+  //
+  // Place SmmStore information hob in a runtime buffer
+  //
+  mSmmStoreInfo = AllocateRuntimePool (GET_GUID_HOB_DATA_SIZE (GuidHob));
+  if (mSmmStoreInfo == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  CopyMem (mSmmStoreInfo, GET_GUID_HOB_DATA (GuidHob), GET_GUID_HOB_DATA_SIZE (GuidHob));
+
+  //
+  // Validate input
+  //
+  if ((mSmmStoreInfo->MmioAddress == 0) ||
+      (mSmmStoreInfo->ComBuffer == 0) ||
+      (mSmmStoreInfo->BlockSize == 0) ||
+      (mSmmStoreInfo->NumBlocks == 0))
+  {
+    DEBUG ((DEBUG_ERROR, "%a: Invalid data in SmmStore Info hob\n", __FUNCTION__));
+    FreePool (mSmmStoreInfo);
+    mSmmStoreInfo = NULL;
+    return EFI_WRITE_PROTECTED;
+  }
+
+  //
+  // Allocate Communication Buffer for arguments to pass to SMM.
+  // The argument com buffer is only read by SMM, but never written.
+  // The FVB data send/retrieved will be placed in a separate bootloader
+  // pre-allocated memory region, the ComBuffer.
+  //
+  if (mSmmStoreInfo->ComBuffer < BASE_4GB) {
+    //
+    // Assume that SMM handler is running in 32-bit mode when ComBuffer is
+    // is placed below BASE_4GB.
+    //
+    mArgComBufPhys = BASE_4GB - 1;
+  } else {
+    mArgComBufPhys = BASE_8EB - 1;
+  }
+
+  Status = gBS->AllocatePages (
+                  AllocateMaxAddress,
+                  EfiRuntimeServicesData,
+                  EFI_SIZE_TO_PAGES (sizeof (SMM_STORE_COM_BUF)),
+                  &mArgComBufPhys
+                  );
+
+  if (EFI_ERROR (Status)) {
+    FreePool (mSmmStoreInfo);
+    mSmmStoreInfo = NULL;
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  mArgComBuf = (VOID *)mArgComBufPhys;
+
+  //
+  // Register for the virtual address change event
+  //
+  Status = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_NOTIFY,
+                  SmmStoreLibVirtualNotifyEvent,
+                  NULL,
+                  &gEfiEventVirtualAddressChangeGuid,
+                  &mSmmStoreLibVirtualAddrChangeEvent
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Finally mark the SMM communication buffer provided by CB or SBL as runtime memory
+  //
+  Status = gDS->GetMemorySpaceDescriptor (mSmmStoreInfo->ComBuffer, &GcdDescriptor);
+  if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType != EfiGcdMemoryTypeReserved)) {
+    DEBUG (
+      (
+       DEBUG_INFO,
+       "%a: No memory space descriptor for com buffer found\n",
+       __FUNCTION__
+      )
+      );
+
+    //
+    // Add a new entry if not covered by existing mapping
+    //
+    Status = gDS->AddMemorySpace (
+                    EfiGcdMemoryTypeReserved,
+                    mSmmStoreInfo->ComBuffer,
+                    mSmmStoreInfo->ComBufferSize,
+                    EFI_MEMORY_WB | EFI_MEMORY_RUNTIME
+                    );
+    ASSERT_EFI_ERROR (Status);
+  }
+
+  //
+  // Mark as runtime service
+  //
+  Status = gDS->SetMemorySpaceAttributes (
+                  mSmmStoreInfo->ComBuffer,
+                  mSmmStoreInfo->ComBufferSize,
+                  EFI_MEMORY_RUNTIME
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Mark the memory mapped store as MMIO memory
+  //
+  Status = gDS->GetMemorySpaceDescriptor (mSmmStoreInfo->MmioAddress, &GcdDescriptor);
+  if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo)) {
+    DEBUG (
+      (
+       DEBUG_INFO,
+       "%a: No memory space descriptor for com buffer found\n",
+       __FUNCTION__
+      )
+      );
+
+    //
+    // Add a new entry if not covered by existing mapping
+    //
+    Status = gDS->AddMemorySpace (
+                    EfiGcdMemoryTypeMemoryMappedIo,
+                    mSmmStoreInfo->MmioAddress,
+                    mSmmStoreInfo->NumBlocks * mSmmStoreInfo->BlockSize,
+                    EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
+                    );
+    ASSERT_EFI_ERROR (Status);
+  }
+
+  //
+  // Mark as runtime service
+  //
+  Status = gDS->SetMemorySpaceAttributes (
+                  mSmmStoreInfo->MmioAddress,
+                  mSmmStoreInfo->NumBlocks * mSmmStoreInfo->BlockSize,
+                  EFI_MEMORY_RUNTIME
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Denitializes SmmStore support by freeing allocated memory and unregistering
+  the virtual address change event.
+**/
+VOID
+EFIAPI
+SmmStoreLibDeinitialize (
+  VOID
+  )
+{
+  if (mArgComBuf != NULL) {
+    gBS->FreePages (mArgComBufPhys, EFI_SIZE_TO_PAGES (sizeof (SMM_STORE_COM_BUF)));
+    mArgComBuf = NULL;
+  }
+
+  if (mSmmStoreInfo != NULL) {
+    FreePool (mSmmStoreInfo);
+    mSmmStoreInfo = NULL;
+  }
+
+  if (mSmmStoreLibVirtualAddrChangeEvent != NULL) {
+    gBS->CloseEvent (mSmmStoreLibVirtualAddrChangeEvent);
+    mSmmStoreLibVirtualAddrChangeEvent = NULL;
+  }
+}
diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h
new file mode 100644
index 0000000000..619b00fa30
--- /dev/null
+++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStore.h
@@ -0,0 +1,81 @@
+/** @file  SmmStore.h
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef COREBOOT_SMMSTORE_H_
+#define COREBOOT_SMMSTORE_H_
+
+#define SMMSTORE_RET_SUCCESS      0
+#define SMMSTORE_RET_FAILURE      1
+#define SMMSTORE_RET_UNSUPPORTED  2
+
+/* Version 2 only */
+#define SMMSTORE_CMD_INIT       4
+#define SMMSTORE_CMD_RAW_READ   5
+#define SMMSTORE_CMD_RAW_WRITE  6
+#define SMMSTORE_CMD_RAW_CLEAR  7
+
+/*
+ * This allows the payload to store raw data in the flash regions.
+ * This can be used by a FaultTolerantWrite implementation, that uses at least
+ * two regions in an A/B update scheme.
+ */
+
+#pragma pack(1)
+
+/*
+ * Reads a chunk of raw data with size BufSize from the block specified by
+ * block_id starting at BufOffset.
+ * The read data is placed in buf.
+ *
+ * block_id must be less than num_blocks
+ * BufOffset + BufSize must be less than block_size
+ */
+typedef struct {
+  UINT32    BufSize;
+  UINT32    BufOffset;
+  UINT32    BlockId;
+} SMM_STORE_PARAMS_WRITE;
+
+/*
+ * Writes a chunk of raw data with size BufSize to the block specified by
+ * block_id starting at BufOffset.
+ *
+ * block_id must be less than num_blocks
+ * BufOffset + BufSize must be less than block_size
+ */
+typedef struct {
+  UINT32    BufSize;
+  UINT32    BufOffset;
+  UINT32    BlockId;
+} SMM_STORE_PARAMS_READ;
+
+/*
+ * Erases the specified block.
+ *
+ * block_id must be less than num_blocks
+ */
+typedef struct {
+  UINT32    BlockId;
+} SMM_STORE_PARAMS_CLEAR;
+
+typedef union {
+  SMM_STORE_PARAMS_WRITE    Write;
+  SMM_STORE_PARAMS_READ     Read;
+  SMM_STORE_PARAMS_CLEAR    Clear;
+} SMM_STORE_COM_BUF;
+#pragma pack(0)
+
+UINTN
+EFIAPI
+TriggerSmi (
+  IN UINTN  Cmd,
+  IN UINTN  Arg,
+  IN UINTN  Retry
+  );
+
+#endif // COREBOOT_SMMSTORE_H_
diff --git a/UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf b/UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
new file mode 100644
index 0000000000..13a520f358
--- /dev/null
+++ b/UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
@@ -0,0 +1,41 @@
+## @file
+#  SmmStore library for coreboot
+#
+#  Copyright (c) 2022 9elements GmbH.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = SmmStoreLib
+  FILE_GUID                      = 40A2CBC6-CFB8-447b-A90E-298E88FD345E
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = SmmStoreLib
+
+[Sources]
+  SmmStore.c
+  SmmStore.h
+
+[Sources.X64]
+  X64/SmmStore.nasm
+
+[LibraryClasses]
+  BaseMemoryLib
+  DebugLib
+  DxeServicesTableLib
+  HobLib
+  MemoryAllocationLib
+  UefiBootServicesTableLib
+  UefiRuntimeLib
+
+[Guids]
+  gEfiSmmStoreInfoHobGuid           ## CONSUMES
+  gEfiEventVirtualAddressChangeGuid ## CONSUMES
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  UefiPayloadPkg/UefiPayloadPkg.dec
diff --git a/UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm b/UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm
new file mode 100644
index 0000000000..1a8b5b64c8
--- /dev/null
+++ b/UefiPayloadPkg/Library/SmmStoreLib/X64/SmmStore.nasm
@@ -0,0 +1,48 @@
+;------------------------------------------------------------------------------ ;
+; Copyright (c) 2022, 9elements GmbH. All rights reserved.<BR>
+; SPDX-License-Identifier: BSD-2-Clause-Patent
+;
+;-------------------------------------------------------------------------------
+
+%include "Nasm.inc"
+
+DEFAULT REL
+SECTION .text
+
+;UINTN
+;EFIAPI
+;TriggerSmi (
+;  UINTN   Cmd,
+;  UINTN   Arg,
+;  UINTN   Retry
+;  )
+
+global ASM_PFX(TriggerSmi)
+ASM_PFX(TriggerSmi):
+    push    rbx
+    mov     rax, rcx                    ; Smi handler expect Cmd in RAX
+    mov     rbx, rdx                    ; Smi handler expect Argument in RBX
+@Trigger:
+    out     0b2h, al                    ; write to APM port to trigger SMI
+
+; There might ba a delay between writing the Smi trigger register and
+; entering SMM, in which case the Smi handler will do nothing as only
+; synchronous Smis are handled. In addition when there's no Smi handler
+; or the SmmStore feature isn't compiled in, no register will be modified.
+
+; As there's no livesign from SMM, just wait a bit for the handler to fire,
+; and then try again.
+
+    cmp     rax, rcx                    ; Check if rax was modified by SMM
+    jne     @Return                     ; SMM modified rax, return now
+    push    rcx                         ; save rcx to stack
+    mov     rcx, 10000
+    rep     pause                       ; add a small delay
+    pop     rcx                         ; restore rcx
+    cmp     r8, 0
+    je      @Return
+    dec     r8
+    jmp     @Trigger
+@Return:
+    pop     rbx
+    ret
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 14a8d157a2..cf440fee58 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -261,6 +261,7 @@
   LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
   FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf
   AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf
+  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
 !if $(VARIABLE_SUPPORT) == "EMU"
   TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
 !elseif $(VARIABLE_SUPPORT) == "SPI"
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
                   ` (2 preceding siblings ...)
  2022-03-21  9:10 ` [PATCH 4/5] UefiPayloadPkg: Add SmmStoreLib Sean Rhodes
@ 2022-03-21  9:10 ` Sean Rhodes
  2022-03-23 18:12   ` Guo Dong
  2022-03-23 17:58 ` [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Guo Dong
  4 siblings, 1 reply; 14+ messages in thread
From: Sean Rhodes @ 2022-03-21  9:10 UTC (permalink / raw)
  To: devel
  Cc: Sean Rhodes, Guo Dong, Ray Ni, Maurice Ma, Benjamin You,
	Patrick Rudolph

This adds support for FVB in order to support a platform independent
and non-volatile variable store on UefiPayloadPkg. The variable store
makes use the SmmStoreLib to provide an unauthenticed variable store.

Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
the SMMSTORE v2 feature. It implements a SMI handler that is able to
write, read and erase pages in the boot media (SPI flash).
The communication is done using a fixed communication buffer that is
allocated in CBMEM. The existence of this optional feature is advertised
by a coreboot table.
When the SMMSTORE feature is not available the variable emulation is used
by setting PcdEmuVariableNvModeEnable to TRUE.

The DXE component provides runtime services and takes care of virtual to
physical mapping the communication buffers between SMM and OS.

The contents of the variable store can be accessed and modified by any
priviledged application. As authentication is done by runtime services
only the store shouldn't be used to store authenticated variables.

Tested on Linux and Windows 10 on real hardware.
Currently this cannot be tested on coreboot and qemu as it doesn't support
the SMMSTORE on qemu.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
 .../SmmStoreFvb/SmmStoreFvbRuntime.c          | 282 ++++++
 .../SmmStoreFvb/SmmStoreFvbRuntime.h          | 111 +++
 .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.c       | 849 ++++++++++++++++++
 .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf     |  66 ++
 UefiPayloadPkg/UefiPayloadPkg.dsc             |  24 +-
 UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
 6 files changed, 1330 insertions(+), 6 deletions(-)
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf

diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
new file mode 100644
index 0000000000..08d5c408fb
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
@@ -0,0 +1,282 @@
+/** @file  SmmStoreFvbRuntime.c
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/UefiLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/PcdLib.h>
+#include <Library/SmmStoreLib.h>
+
+#include "SmmStoreFvbRuntime.h"
+
+STATIC EFI_EVENT  mSmmStoreVirtualAddrChangeEvent;
+
+//
+// Global variable declarations
+//
+SMMSTORE_INSTANCE  *mSmmStoreInstance;
+
+SMMSTORE_INSTANCE  mSmmStoreInstanceTemplate = {
+  SMMSTORE_SIGNATURE, // Signature
+  NULL,               // Handle ... NEED TO BE FILLED
+  {
+    FvbGetAttributes,      // GetAttributes
+    FvbSetAttributes,      // SetAttributes
+    FvbGetPhysicalAddress, // GetPhysicalAddress
+    FvbGetBlockSize,       // GetBlockSize
+    FvbRead,               // Read
+    FvbWrite,              // Write
+    FvbEraseBlocks,        // EraseBlocks
+    NULL,                  // ParentHandle
+  }, //  FvbProtoccol
+  0, // BlockSize ... NEED TO BE FILLED
+  0, // LastBlock ... NEED TO BE FILLED
+  0, // MmioAddress ... NEED TO BE FILLED
+  {
+    {
+      {
+        HARDWARE_DEVICE_PATH,
+        HW_MEMMAP_DP,
+        {
+          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
+          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
+        }
+      },
+      EfiMemoryMappedIO,
+      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
+      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
+    },
+    {
+      END_DEVICE_PATH_TYPE,
+      END_ENTIRE_DEVICE_PATH_SUBTYPE,
+      {
+        END_DEVICE_PATH_LENGTH,
+        0
+      }
+    }
+  } // DevicePath
+};
+
+/**
+  Initialize the SmmStore instance.
+
+
+  @param[in]      FvBase         The physical MMIO base address of the FV containing
+                                 the variable store.
+
+  @param[in]      NumberofBlocks Number of blocks within the FV.
+  @param[in]      BlockSize      The size in bytes of one block within the FV.
+  @param[in, out] Instance       The SmmStore instace to initialize
+
+**/
+STATIC
+EFI_STATUS
+SmmStoreInitInstance (
+  IN EFI_PHYSICAL_ADDRESS   FvBase,
+  IN UINTN                  NumberofBlocks,
+  IN UINTN                  BlockSize,
+  IN OUT SMMSTORE_INSTANCE  *Instance
+  )
+{
+  EFI_STATUS             Status;
+  FV_MEMMAP_DEVICE_PATH  *FvDevicePath;
+
+  ASSERT (Instance != NULL);
+
+  Instance->BlockSize   = BlockSize;
+  Instance->LastBlock   = NumberofBlocks - 1;
+  Instance->MmioAddress = FvBase;
+
+  FvDevicePath                                = &Instance->DevicePath;
+  FvDevicePath->MemMapDevPath.StartingAddress = FvBase;
+  FvDevicePath->MemMapDevPath.EndingAddress   = FvBase + BlockSize * NumberofBlocks - 1;
+
+  Status = FvbInitialize (Instance);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = gBS->InstallMultipleProtocolInterfaces (
+                  &Instance->Handle,
+                  &gEfiDevicePathProtocolGuid,
+                  &Instance->DevicePath,
+                  &gEfiFirmwareVolumeBlockProtocolGuid,
+                  &Instance->FvbProtocol,
+                  NULL
+                  );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__));
+
+  return Status;
+}
+
+/**
+  Fixup internal data so that EFI can be call in virtual mode.
+  Call the passed in Child Notify event and convert any pointers in
+  lib to virtual mode.
+
+  @param[in]    Event   The Event that is being processed
+  @param[in]    Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+SmmStoreVirtualNotifyEvent (
+  IN EFI_EVENT  Event,
+  IN VOID       *Context
+  )
+{
+  // Convert Fvb
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.EraseBlocks);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetAttributes);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetBlockSize);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetPhysicalAddress);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Read);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.SetAttributes);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Write);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->MmioAddress);
+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance);
+
+  return;
+}
+
+/**
+  The user Entry Point for module SmmStoreFvbRuntimeDxe. The user code starts with this function.
+
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in] SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+  @retval other             Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmStoreInitialize (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS            Status;
+  EFI_PHYSICAL_ADDRESS  MmioAddress;
+  UINTN                 BlockSize;
+  UINTN                 BlockCount;
+  UINT32                NvStorageBase;
+  UINT32                NvStorageSize;
+  UINT32                NvVariableSize;
+  UINT32                FtwWorkingSize;
+  UINT32                FtwSpareSize;
+
+  Status = SmmStoreLibInitialize ();
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: Failed to initialize SmmStoreLib\n", __FUNCTION__));
+    return Status;
+  }
+
+  Status = SmmStoreLibGetMmioAddress (&MmioAddress);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore MMIO address\n", __FUNCTION__));
+    SmmStoreLibDeinitialize ();
+    return Status;
+  }
+
+  Status = SmmStoreLibGetNumBlocks (&BlockCount);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore No. blocks\n", __FUNCTION__));
+    SmmStoreLibDeinitialize ();
+    return Status;
+  }
+
+  Status = SmmStoreLibGetBlockSize (&BlockSize);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore block size\n", __FUNCTION__));
+    SmmStoreLibDeinitialize ();
+    return Status;
+  }
+
+  NvStorageSize = BlockCount * BlockSize;
+  NvStorageBase = MmioAddress;
+
+  FtwSpareSize   = (BlockCount / 2) * BlockSize;
+  FtwWorkingSize = BlockSize;
+  NvVariableSize = NvStorageSize - FtwSpareSize - FtwWorkingSize;
+  DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase, NvStorageSize));
+
+  if (NvVariableSize >= 0x80000000) {
+    SmmStoreLibDeinitialize ();
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = PcdSet32S (PcdFlashNvStorageVariableSize, NvVariableSize);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet32S (PcdFlashNvStorageVariableBase, NvStorageBase);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet64S (PcdFlashNvStorageVariableBase64, NvStorageBase);
+  ASSERT_EFI_ERROR (Status);
+
+  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64, NvStorageBase + NvVariableSize);
+  ASSERT_EFI_ERROR (Status);
+
+  Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + NvVariableSize + FtwWorkingSize);
+  ASSERT_EFI_ERROR (Status);
+  Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64, NvStorageBase + NvVariableSize + FtwWorkingSize);
+  ASSERT_EFI_ERROR (Status);
+
+  mSmmStoreInstance = AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSmmStoreInstanceTemplate);
+  if (mSmmStoreInstance == NULL) {
+    SmmStoreLibDeinitialize ();
+    DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__));
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  Status = SmmStoreInitInstance (
+             MmioAddress,
+             BlockCount,
+             BlockSize,
+             mSmmStoreInstance
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG (
+      (
+       DEBUG_ERROR,
+       "%a: Fail to create instance for SmmStore\n",
+       __FUNCTION__
+      )
+      );
+    FreePool (mSmmStoreInstance);
+    SmmStoreLibDeinitialize ();
+    return Status;
+  }
+
+  //
+  // Register for the virtual address change event
+  //
+  Status = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_NOTIFY,
+                  SmmStoreVirtualNotifyEvent,
+                  NULL,
+                  &gEfiEventVirtualAddressChangeGuid,
+                  &mSmmStoreVirtualAddrChangeEvent
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  return Status;
+}
diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
new file mode 100644
index 0000000000..8015d12d13
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
@@ -0,0 +1,111 @@
+/** @file  SmmStoreFvbRuntime.h
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef SMM_STORE_DXE_H_
+#define SMM_STORE_DXE_H_
+
+#include <Base.h>
+#include <PiDxe.h>
+
+#include <Guid/EventGroup.h>
+
+#include <Protocol/FirmwareVolumeBlock.h>
+
+#include <Library/DebugLib.h>
+#include <Library/IoLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiRuntimeLib.h>
+
+#define SMMSTORE_SIGNATURE  SIGNATURE_32('S', 'M', 'M', 'S')
+#define INSTANCE_FROM_FVB_THIS(a)  CR(a, SMMSTORE_INSTANCE, FvbProtocol, SMMSTORE_SIGNATURE)
+
+typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE;
+
+typedef struct {
+  MEMMAP_DEVICE_PATH          MemMapDevPath;
+  EFI_DEVICE_PATH_PROTOCOL    EndDevPath;
+} FV_MEMMAP_DEVICE_PATH;
+
+struct _SMMSTORE_INSTANCE {
+  UINT32                                 Signature;
+  EFI_HANDLE                             Handle;
+  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL    FvbProtocol;
+  UINTN                                  BlockSize;
+  UINTN                                  LastBlock;
+  EFI_PHYSICAL_ADDRESS                   MmioAddress;
+  FV_MEMMAP_DEVICE_PATH                  DevicePath;
+};
+
+//
+// SmmStoreFvbRuntimeDxe.c
+//
+
+EFI_STATUS
+EFIAPI
+FvbInitialize (
+  IN SMMSTORE_INSTANCE  *Instance
+  );
+
+EFI_STATUS
+EFIAPI
+FvbGetAttributes (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
+  );
+
+EFI_STATUS
+EFIAPI
+FvbSetAttributes (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
+  );
+
+EFI_STATUS
+EFIAPI
+FvbGetPhysicalAddress (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  OUT       EFI_PHYSICAL_ADDRESS                 *Address
+  );
+
+EFI_STATUS
+EFIAPI
+FvbGetBlockSize (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  OUT       UINTN                                *BlockSize,
+  OUT       UINTN                                *NumberOfBlocks
+  );
+
+EFI_STATUS
+EFIAPI
+FvbRead (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  IN        UINTN                                Offset,
+  IN OUT    UINTN                                *NumBytes,
+  IN OUT    UINT8                                *Buffer
+  );
+
+EFI_STATUS
+EFIAPI
+FvbWrite (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  IN        UINTN                                Offset,
+  IN OUT    UINTN                                *NumBytes,
+  IN        UINT8                                *Buffer
+  );
+
+EFI_STATUS
+EFIAPI
+FvbEraseBlocks (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  ...
+  );
+
+#endif // SMM_STORE_DXE_H_
diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
new file mode 100644
index 0000000000..14d4ec324d
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
@@ -0,0 +1,849 @@
+/** @file  SmmStoreFvbRuntimeDxe.c
+
+  Copyright (c) 2022, 9elements GmbH<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiDxe.h>
+
+#include <Library/PcdLib.h>
+#include <Library/BaseLib.h>
+#include <Library/HobLib.h>
+#include <Library/UefiLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DxeServicesTableLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/SmmStoreLib.h>
+
+#include <Guid/VariableFormat.h>
+#include <Guid/SystemNvDataGuid.h>
+#include <Guid/NvVarStoreFormatted.h>
+
+#include "SmmStoreFvbRuntime.h"
+
+///
+/// The Firmware Volume Block Protocol is the low-level interface
+/// to a firmware volume. File-level access to a firmware volume
+/// should not be done using the Firmware Volume Block Protocol.
+/// Normal access to a firmware volume must use the Firmware
+/// Volume Protocol. Typically, only the file system driver that
+/// produces the Firmware Volume Protocol will bind to the
+/// Firmware Volume Block Protocol.
+///
+
+/**
+  Initialises the FV Header and Variable Store Header
+  to support variable operations.
+
+  @param[in]  Instance - Pointer to SmmStore instance
+
+**/
+EFI_STATUS
+InitializeFvAndVariableStoreHeaders (
+  IN SMMSTORE_INSTANCE  *Instance
+  )
+{
+  EFI_STATUS                  Status;
+  VOID                        *Headers;
+  UINTN                       HeadersLength;
+  EFI_FIRMWARE_VOLUME_HEADER  *FirmwareVolumeHeader;
+  VARIABLE_STORE_HEADER       *VariableStoreHeader;
+  UINT32                      NvStorageFtwSpareSize;
+  UINT32                      NvStorageFtwWorkingSize;
+  UINT32                      NvStorageVariableSize;
+  UINT64                      NvStorageFtwSpareBase;
+  UINT64                      NvStorageFtwWorkingBase;
+  UINT64                      NvStorageVariableBase;
+
+  HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);
+  Headers       = AllocateZeroPool (HeadersLength);
+
+  NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
+  NvStorageFtwSpareSize   = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  NvStorageVariableSize   = PcdGet32 (PcdFlashNvStorageVariableSize);
+
+  NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?
+                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);
+  NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?
+                            PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
+  NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
+                          PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
+
+  // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
+  if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
+      __FUNCTION__
+      ));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
+      __FUNCTION__
+      ));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Check if the size of the area is at least one block size
+  if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
+      __FUNCTION__,
+      NvStorageVariableSize
+      ));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
+      __FUNCTION__,
+      NvStorageFtwWorkingSize
+      ));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
+      __FUNCTION__,
+      NvStorageFtwSpareSize
+      ));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Ensure the Variable area Base Addresses are aligned on a block size boundaries
+  if ((NvStorageVariableBase % Instance->BlockSize != 0) ||
+      (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||
+      (NvStorageFtwSpareBase % Instance->BlockSize != 0))
+  {
+    DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // EFI_FIRMWARE_VOLUME_HEADER
+  //
+  FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;
+  CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
+  FirmwareVolumeHeader->FvLength =
+    PcdGet32 (PcdFlashNvStorageVariableSize) +
+    PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
+    PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  FirmwareVolumeHeader->Signature  = EFI_FVH_SIGNATURE;
+  FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(
+                                                            EFI_FVB2_READ_ENABLED_CAP   | // Reads may be enabled
+                                                            EFI_FVB2_READ_STATUS        | // Reads are currently enabled
+                                                            EFI_FVB2_STICKY_WRITE       | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
+                                                            EFI_FVB2_MEMORY_MAPPED      | // It is memory mapped
+                                                            EFI_FVB2_ERASE_POLARITY     | // After erasure all bits take this value (i.e. '1')
+                                                            EFI_FVB2_WRITE_STATUS       | // Writes are currently enabled
+                                                            EFI_FVB2_WRITE_ENABLED_CAP    // Writes may be enabled
+                                                            );
+  FirmwareVolumeHeader->HeaderLength          = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);
+  FirmwareVolumeHeader->Revision              = EFI_FVH_REVISION;
+  FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;
+  FirmwareVolumeHeader->BlockMap[0].Length    = Instance->BlockSize;
+  FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
+  FirmwareVolumeHeader->BlockMap[1].Length    = 0;
+  FirmwareVolumeHeader->Checksum              = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
+
+  //
+  // VARIABLE_STORE_HEADER
+  //
+  VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
+  CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);
+  VariableStoreHeader->Size   = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
+  VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
+  VariableStoreHeader->State  = VARIABLE_STORE_HEALTHY;
+
+  // Install the combined super-header in the NorFlash
+  Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
+
+  FreePool (Headers);
+  return Status;
+}
+
+/**
+  Check the integrity of firmware volume header.
+
+  @retval  EFI_SUCCESS   - The firmware volume is consistent
+  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.
+
+**/
+EFI_STATUS
+ValidateFvHeader (
+  VOID
+  )
+{
+  UINT16                      Checksum;
+  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;
+  VARIABLE_STORE_HEADER       *VariableStoreHeader;
+  UINTN                       VariableStoreLength;
+  UINTN                       FvLength;
+  EFI_STATUS                  TempStatus;
+  UINTN                       BufferSize;
+  UINTN                       BufferSizeReqested;
+
+  BufferSizeReqested = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
+  FwVolHeader        = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
+  if (!FwVolHeader) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  BufferSize = BufferSizeReqested;
+  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
+    FreePool (FwVolHeader);
+    return EFI_DEVICE_ERROR;
+  }
+
+  FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
+             PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+
+  //
+  // Verify the header revision, header signature, length
+  // Length of FvBlock cannot be 2**64-1
+  // HeaderLength cannot be an odd number
+  //
+  if (  (FwVolHeader->Revision  != EFI_FVH_REVISION)
+     || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
+     || (FwVolHeader->FvLength  != FvLength)
+        )
+  {
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: No Firmware Volume header present\n",
+      __FUNCTION__
+      ));
+    FreePool (FwVolHeader);
+    return EFI_NOT_FOUND;
+  }
+
+  // Check the Firmware Volume Guid
+  if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Firmware Volume Guid non-compatible\n",
+      __FUNCTION__
+      ));
+    FreePool (FwVolHeader);
+    return EFI_NOT_FOUND;
+  }
+
+  BufferSizeReqested = FwVolHeader->HeaderLength;
+  FreePool (FwVolHeader);
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
+  if (!FwVolHeader) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  BufferSize = BufferSizeReqested;
+  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
+    FreePool (FwVolHeader);
+    return EFI_DEVICE_ERROR;
+  }
+
+  // Verify the header checksum
+  Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
+  if (Checksum != 0) {
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: FV checksum is invalid (Checksum:0x%X)\n",
+      __FUNCTION__,
+      Checksum
+      ));
+    FreePool (FwVolHeader);
+    return EFI_NOT_FOUND;
+  }
+
+  BufferSizeReqested  = sizeof (VARIABLE_STORE_HEADER);
+  VariableStoreHeader = (VARIABLE_STORE_HEADER *)AllocatePool (BufferSizeReqested);
+  if (!VariableStoreHeader) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  BufferSize = BufferSizeReqested;
+  TempStatus = SmmStoreLibRead (0, FwVolHeader->HeaderLength, &BufferSize, (UINT8 *)VariableStoreHeader);
+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
+    FreePool (VariableStoreHeader);
+    FreePool (FwVolHeader);
+    return EFI_DEVICE_ERROR;
+  }
+
+  // Check the Variable Store Guid
+  if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
+      !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))
+  {
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Variable Store Guid non-compatible\n",
+      __FUNCTION__
+      ));
+    FreePool (FwVolHeader);
+    FreePool (VariableStoreHeader);
+    return EFI_NOT_FOUND;
+  }
+
+  VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
+  if (VariableStoreHeader->Size != VariableStoreLength) {
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Variable Store Length does not match\n",
+      __FUNCTION__
+      ));
+    FreePool (FwVolHeader);
+    FreePool (VariableStoreHeader);
+    return EFI_NOT_FOUND;
+  }
+
+  FreePool (FwVolHeader);
+  FreePool (VariableStoreHeader);
+
+  return EFI_SUCCESS;
+}
+
+/**
+ The GetAttributes() function retrieves the attributes and
+ current settings of the block.
+
+ @param This         Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Attributes   Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
+                     current settings are returned.
+                     Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
+
+ @retval EFI_SUCCESS The firmware volume attributes were returned.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbGetAttributes (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
+  )
+{
+  EFI_FVB_ATTRIBUTES_2  FlashFvbAttributes;
+
+  FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
+                                              EFI_FVB2_READ_STATUS      | // Reads are currently enabled
+                                              EFI_FVB2_WRITE_STATUS     | // Writes are enabled
+                                              EFI_FVB2_STICKY_WRITE     | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
+                                              EFI_FVB2_MEMORY_MAPPED    | // It is memory mapped
+                                              EFI_FVB2_ERASE_POLARITY     // After erasure all bits take this value (i.e. '1')
+                                              );
+
+  *Attributes = FlashFvbAttributes;
+
+  DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
+
+  return EFI_SUCCESS;
+}
+
+/**
+ The SetAttributes() function sets configurable firmware volume attributes
+ and returns the new settings of the firmware volume.
+
+
+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Attributes               On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
+                                 that contains the desired firmware volume settings.
+                                 On successful return, it contains the new settings of
+                                 the firmware volume.
+                                 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
+
+ @retval EFI_SUCCESS             The firmware volume attributes were returned.
+
+ @retval EFI_INVALID_PARAMETER   The attributes requested are in conflict with the capabilities
+                                 as declared in the firmware volume header.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbSetAttributes (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
+  )
+{
+  DEBUG ((DEBUG_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
+  return EFI_UNSUPPORTED;
+}
+
+/**
+ The GetPhysicalAddress() function retrieves the base address of
+ a memory-mapped firmware volume. This function should be called
+ only for memory-mapped firmware volumes.
+
+ @param This               Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Address            Pointer to a caller-allocated
+                           EFI_PHYSICAL_ADDRESS that, on successful
+                           return from GetPhysicalAddress(), contains the
+                           base address of the firmware volume.
+
+ @retval EFI_SUCCESS       The firmware volume base address was returned.
+
+ @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbGetPhysicalAddress (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  OUT       EFI_PHYSICAL_ADDRESS                 *Address
+  )
+{
+  SMMSTORE_INSTANCE  *Instance;
+
+  Instance = INSTANCE_FROM_FVB_THIS (This);
+
+  ASSERT (Address != NULL);
+  *Address = Instance->MmioAddress;
+
+  return EFI_SUCCESS;
+}
+
+/**
+ The GetBlockSize() function retrieves the size of the requested
+ block. It also returns the number of additional blocks with
+ the identical size. The GetBlockSize() function is used to
+ retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
+
+
+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Lba                      Indicates the block for which to return the size.
+
+ @param BlockSize                Pointer to a caller-allocated UINTN in which
+                                 the size of the block is returned.
+
+ @param NumberOfBlocks           Pointer to a caller-allocated UINTN in
+                                 which the number of consecutive blocks,
+                                 starting with Lba, is returned. All
+                                 blocks in this range have a size of
+                                 BlockSize.
+
+
+ @retval EFI_SUCCESS             The firmware volume base address was returned.
+
+ @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbGetBlockSize (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  OUT       UINTN                                *BlockSize,
+  OUT       UINTN                                *NumberOfBlocks
+  )
+{
+  EFI_STATUS         Status;
+  SMMSTORE_INSTANCE  *Instance;
+
+  Instance = INSTANCE_FROM_FVB_THIS (This);
+
+  DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));
+
+  if (Lba > Instance->LastBlock) {
+    DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));
+    Status = EFI_INVALID_PARAMETER;
+  } else {
+    *BlockSize      = (UINTN)Instance->BlockSize;
+    *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);
+
+    DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
+
+    Status = EFI_SUCCESS;
+  }
+
+  return Status;
+}
+
+/**
+ Reads the specified number of bytes into a buffer from the specified block.
+
+ The Read() function reads the requested number of bytes from the
+ requested block and stores them in the provided buffer.
+ Implementations should be mindful that the firmware volume
+ might be in the ReadDisabled state. If it is in this state,
+ the Read() function must return the status code
+ EFI_ACCESS_DENIED without modifying the contents of the
+ buffer. The Read() function must also prevent spanning block
+ boundaries. If a read is requested that would span a block
+ boundary, the read must read up to the boundary but not
+ beyond. The output parameter NumBytes must be set to correctly
+ indicate the number of bytes actually read. The caller must be
+ aware that a read may be partially completed.
+
+ @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Lba                  The starting logical block index from which to read.
+
+ @param Offset               Offset into the block at which to begin reading.
+
+ @param NumBytes             Pointer to a UINTN.
+                             At entry, *NumBytes contains the total size of the buffer.
+                             At exit, *NumBytes contains the total number of bytes read.
+
+ @param Buffer               Pointer to a caller-allocated buffer that will be used
+                             to hold the data that is read.
+
+ @retval EFI_SUCCESS         The firmware volume was read successfully,  and contents are
+                             in Buffer.
+
+ @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
+                             On output, NumBytes contains the total number of bytes
+                             returned in Buffer.
+
+ @retval EFI_ACCESS_DENIED   The firmware volume is in the ReadDisabled state.
+
+ @retval EFI_DEVICE_ERROR    The block device is not functioning correctly and could not be read.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbRead (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  IN        UINTN                                Offset,
+  IN OUT    UINTN                                *NumBytes,
+  IN OUT    UINT8                                *Buffer
+  )
+{
+  UINTN              BlockSize;
+  SMMSTORE_INSTANCE  *Instance;
+
+  Instance = INSTANCE_FROM_FVB_THIS (This);
+
+  DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
+
+  // Cache the block size to avoid de-referencing pointers all the time
+  BlockSize = Instance->BlockSize;
+
+  // The read must not span block boundaries.
+  // We need to check each variable individually because adding two large values together overflows.
+  if ((Offset               >= BlockSize) ||
+      (*NumBytes            >  BlockSize) ||
+      ((Offset + *NumBytes) >  BlockSize))
+  {
+    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
+    return EFI_BAD_BUFFER_SIZE;
+  }
+
+  // We must have some bytes to read
+  if (*NumBytes == 0) {
+    return EFI_BAD_BUFFER_SIZE;
+  }
+
+  return SmmStoreLibRead (Lba, Offset, NumBytes, Buffer);
+}
+
+/**
+ Writes the specified number of bytes from the input buffer to the block.
+
+ The Write() function writes the specified number of bytes from
+ the provided buffer to the specified block and offset. If the
+ firmware volume is sticky write, the caller must ensure that
+ all the bits of the specified range to write are in the
+ EFI_FVB_ERASE_POLARITY state before calling the Write()
+ function, or else the result will be unpredictable. This
+ unpredictability arises because, for a sticky-write firmware
+ volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
+ state but cannot flip it back again.  Before calling the
+ Write() function,  it is recommended for the caller to first call
+ the EraseBlocks() function to erase the specified block to
+ write. A block erase cycle will transition bits from the
+ (NOT)EFI_FVB_ERASE_POLARITY state back to the
+ EFI_FVB_ERASE_POLARITY state. Implementations should be
+ mindful that the firmware volume might be in the WriteDisabled
+ state. If it is in this state, the Write() function must
+ return the status code EFI_ACCESS_DENIED without modifying the
+ contents of the firmware volume. The Write() function must
+ also prevent spanning block boundaries. If a write is
+ requested that spans a block boundary, the write must store up
+ to the boundary but not beyond. The output parameter NumBytes
+ must be set to correctly indicate the number of bytes actually
+ written. The caller must be aware that a write may be
+ partially completed. All writes, partial or otherwise, must be
+ fully flushed to the hardware before the Write() service
+ returns.
+
+ @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
+
+ @param Lba                  The starting logical block index to write to.
+
+ @param Offset               Offset into the block at which to begin writing.
+
+ @param NumBytes             The pointer to a UINTN.
+                             At entry, *NumBytes contains the total size of the buffer.
+                             At exit, *NumBytes contains the total number of bytes actually written.
+
+ @param Buffer               The pointer to a caller-allocated buffer that contains the source for the write.
+
+ @retval EFI_SUCCESS         The firmware volume was written successfully.
+
+ @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
+                             On output, NumBytes contains the total number of bytes
+                             actually written.
+
+ @retval EFI_ACCESS_DENIED   The firmware volume is in the WriteDisabled state.
+
+ @retval EFI_DEVICE_ERROR    The block device is malfunctioning and could not be written.
+
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbWrite (
+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  IN        EFI_LBA                              Lba,
+  IN        UINTN                                Offset,
+  IN OUT    UINTN                                *NumBytes,
+  IN        UINT8                                *Buffer
+  )
+{
+  UINTN              BlockSize;
+  SMMSTORE_INSTANCE  *Instance;
+
+  Instance = INSTANCE_FROM_FVB_THIS (This);
+
+  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
+
+  // Cache the block size to avoid de-referencing pointers all the time
+  BlockSize = Instance->BlockSize;
+
+  // The read must not span block boundaries.
+  // We need to check each variable individually because adding two large values together overflows.
+  if ((Offset               >= BlockSize) ||
+      (*NumBytes            >  BlockSize) ||
+      ((Offset + *NumBytes) >  BlockSize))
+  {
+    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
+    return EFI_BAD_BUFFER_SIZE;
+  }
+
+  // We must have some bytes to read
+  if (*NumBytes == 0) {
+    return EFI_BAD_BUFFER_SIZE;
+  }
+
+  return SmmStoreLibWrite (Lba, Offset, NumBytes, Buffer);
+}
+
+/**
+ Erases and initialises a firmware volume block.
+
+ The EraseBlocks() function erases one or more blocks as denoted
+ by the variable argument list. The entire parameter list of
+ blocks must be verified before erasing any blocks. If a block is
+ requested that does not exist within the associated firmware
+ volume (it has a larger index than the last block of the
+ firmware volume), the EraseBlocks() function must return the
+ status code EFI_INVALID_PARAMETER without modifying the contents
+ of the firmware volume. Implementations should be mindful that
+ the firmware volume might be in the WriteDisabled state. If it
+ is in this state, the EraseBlocks() function must return the
+ status code EFI_ACCESS_DENIED without modifying the contents of
+ the firmware volume. All calls to EraseBlocks() must be fully
+ flushed to the hardware before the EraseBlocks() service
+ returns.
+
+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
+ instance.
+
+ @param ...                      The variable argument list is a list of tuples.
+                                 Each tuple describes a range of LBAs to erase
+                                 and consists of the following:
+                                 - An EFI_LBA that indicates the starting LBA
+                                 - A UINTN that indicates the number of blocks to erase.
+
+                                 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
+                                 For example, the following indicates that two ranges of blocks
+                                 (5-7 and 10-11) are to be erased:
+                                 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
+
+ @retval EFI_SUCCESS             The erase request successfully completed.
+
+ @retval EFI_ACCESS_DENIED       The firmware volume is in the WriteDisabled state.
+
+ @retval EFI_DEVICE_ERROR        The block device is not functioning correctly and could not be written.
+                                 The firmware device may have been partially erased.
+
+ @retval EFI_INVALID_PARAMETER   One or more of the LBAs listed in the variable argument list do
+                                 not exist in the firmware volume.
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbEraseBlocks (
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
+  ...
+  )
+{
+  EFI_STATUS         Status;
+  VA_LIST            Args;
+  EFI_LBA            StartingLba; // Lba from which we start erasing
+  UINTN              NumOfLba;    // Number of Lba blocks to erase
+  SMMSTORE_INSTANCE  *Instance;
+
+  Instance = INSTANCE_FROM_FVB_THIS (This);
+
+  Status = EFI_SUCCESS;
+
+  // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
+
+  VA_START (Args, This);
+  do {
+    // Get the Lba from which we start erasing
+    StartingLba = VA_ARG (Args, EFI_LBA);
+
+    // Have we reached the end of the list?
+    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
+      // Exit the while loop
+      break;
+    }
+
+    // How many Lba blocks are we requested to erase?
+    NumOfLba = VA_ARG (Args, UINTN);
+
+    // All blocks must be within range
+    DEBUG ((
+      DEBUG_BLKIO,
+      "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
+      StartingLba,
+      (UINT64)NumOfLba,
+      Instance->LastBlock
+      ));
+    if ((NumOfLba == 0) || ((StartingLba + NumOfLba - 1) > Instance->LastBlock)) {
+      VA_END (Args);
+      DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
+      Status = EFI_INVALID_PARAMETER;
+      goto EXIT;
+    }
+  } while (TRUE);
+
+  VA_END (Args);
+
+  //
+  // To get here, all must be ok, so start erasing
+  //
+  VA_START (Args, This);
+  do {
+    // Get the Lba from which we start erasing
+    StartingLba = VA_ARG (Args, EFI_LBA);
+
+    // Have we reached the end of the list?
+    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
+      // Exit the while loop
+      break;
+    }
+
+    // How many Lba blocks are we requested to erase?
+    NumOfLba = VA_ARG (Args, UINTN);
+
+    // Go through each one and erase it
+    while (NumOfLba > 0) {
+      // Erase it
+      DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld\n", StartingLba));
+      Status = SmmStoreLibEraseBlock (StartingLba);
+      if (EFI_ERROR (Status)) {
+        VA_END (Args);
+        Status = EFI_DEVICE_ERROR;
+        goto EXIT;
+      }
+
+      // Move to the next Lba
+      StartingLba++;
+      NumOfLba--;
+    }
+  } while (TRUE);
+
+  VA_END (Args);
+
+EXIT:
+  return Status;
+}
+
+/**
+  Initialized the Firmware Volume if necessary and installs the
+  gEdkiiNvVarStoreFormattedGuid protocol.
+
+  @param Instance                    Pointer to SmmStore instance
+
+ **/
+EFI_STATUS
+EFIAPI
+FvbInitialize (
+  IN SMMSTORE_INSTANCE  *Instance
+  )
+{
+  EFI_STATUS     Status;
+  UINT32         FvbNumLba;
+  EFI_BOOT_MODE  BootMode;
+
+  ASSERT ((Instance != NULL));
+
+  BootMode = GetBootModeHob ();
+  if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
+    Status = EFI_INVALID_PARAMETER;
+  } else {
+    // Determine if there is a valid header at the beginning of the NorFlash
+    Status = ValidateFvHeader ();
+  }
+
+  // Install the Default FVB header if required
+  if (EFI_ERROR (Status)) {
+    // There is no valid header, so time to install one.
+    DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Installing a correct one for this volume.\n",
+      __FUNCTION__
+      ));
+
+    // Erase all the NorFlash that is reserved for variable storage
+    FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) +
+                 PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
+                 PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
+
+    Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    // Install all appropriate headers
+    Status = InitializeFvAndVariableStoreHeaders (Instance);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  } else {
+    DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__));
+  }
+
+  //
+  // The driver implementing the variable read service can now be dispatched;
+  // the varstore headers are in place.
+  //
+  Status = gBS->InstallProtocolInterface (
+                  &gImageHandle,
+                  &gEdkiiNvVarStoreFormattedGuid,
+                  EFI_NATIVE_INTERFACE,
+                  NULL
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  return Status;
+}
diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
new file mode 100644
index 0000000000..2bd8886bcc
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
@@ -0,0 +1,66 @@
+## @file
+#  Component description file for SmmStore module
+#
+#  This module produces the FirmwareVolumeBlock protocol on top of the
+#  SmmStore provided by bootloader and installs the NvVarStoreFormatted protocol.
+#
+#  Copyright (c) 2022, 9elements GmbH<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = SmmStoreFvbRuntimeDxe
+  FILE_GUID                      = A0402FCA-6B25-4CEA-B7DD-C08F99714B29
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = SmmStoreInitialize
+
+[Sources.common]
+  SmmStoreFvbRuntimeDxe.c
+  SmmStoreFvbRuntime.h
+  SmmStoreFvbRuntime.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+  UefiPayloadPkg/UefiPayloadPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  HobLib
+  SmmStoreLib
+  UefiLib
+  UefiDriverEntryPoint
+  UefiBootServicesTableLib
+  UefiRuntimeLib
+  DxeServicesTableLib
+
+[Guids]
+  gEfiSystemNvDataFvGuid
+  gEfiVariableGuid                  ## PRODUCES ## PROTOCOL
+  gEfiAuthenticatedVariableGuid
+  gEfiEventVirtualAddressChangeGuid
+  gEdkiiNvVarStoreFormattedGuid     ## PRODUCES ## PROTOCOL
+
+[Protocols]
+  gEfiDevicePathProtocolGuid          ## BY_START
+  gEfiFirmwareVolumeBlockProtocolGuid ## BY_START
+
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
+
+[Depex]
+  TRUE
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index cf440fee58..0f6421612c 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -97,6 +97,7 @@
   #
   # EMU:      UEFI payload with EMU variable
   # SPI:      UEFI payload with SPI NV variable support
+  # SMMSTORE: UEFI payload with coreboot SMM NV variable support
   # NONE:     UEFI payload with no variable modules
   #
   DEFINE VARIABLE_SUPPORT      = EMU
@@ -261,9 +262,11 @@
   LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
   FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf
   AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf
-  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
 !if $(VARIABLE_SUPPORT) == "EMU"
   TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
+  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
+  TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
 !elseif $(VARIABLE_SUPPORT) == "SPI"
   PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf
   TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
@@ -379,15 +382,13 @@
   gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE
 
 [PcdsFixedAtBuild]
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000
 !if $(VARIABLE_SUPPORT) == "EMU"
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |TRUE
-!else
+!elseif $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |FALSE
 !endif
-
   gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE
   gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE
@@ -445,7 +446,6 @@
   gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|$(MAX_LOGICAL_PROCESSORS)
   gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0
   gUefiPayloadPkgTokenSpaceGuid.PcdBootloaderParameter|0
-
 ################################################################################
 #
 # Pcd DynamicEx Section - list of all EDK II PCD Entries defined by this Platform
@@ -473,15 +473,19 @@
 !endif
   gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FALSE
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0
   gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3
-!if $(VARIABLE_SUPPORT) == "SPI"
+!if $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize  |0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize  |0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase  |0
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0
 !endif
   # Disable SMM S3 script
   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE
@@ -697,6 +701,14 @@
 
 !if $(VARIABLE_SUPPORT) == "EMU"
   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
+  UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
+  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
+  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
+    <LibraryClasses>
+      NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
+      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
+  }
 !elseif $(VARIABLE_SUPPORT) == "SPI"
   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf {
     <LibraryClasses>
diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
index c7b04978ad..ca44f48f6d 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -140,6 +140,10 @@ INF PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
 
 !if $(VARIABLE_SUPPORT) == "EMU"
   INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
+  INF UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
+  INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
+  INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
 !elseif $(VARIABLE_SUPPORT) == "SPI"
   INF UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
   INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry
  2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
                   ` (3 preceding siblings ...)
  2022-03-21  9:10 ` [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Sean Rhodes
@ 2022-03-23 17:58 ` Guo Dong
  2022-03-24  0:37   ` Ni, Ray
  4 siblings, 1 reply; 14+ messages in thread
From: Guo Dong @ 2022-03-23 17:58 UTC (permalink / raw)
  To: Rhodes, Sean, devel@edk2.groups.io
  Cc: Patrick Rudolph, Ni, Ray, Ma, Maurice, You, Benjamin,
	Rhodes, Sean


Reviewed-by: Guo Dong <guo.dong@intel.com>

-----Original Message-----
From: Sean Rhodes <sean@starlabs.systems> 
Sent: Monday, March 21, 2022 2:10 AM
To: devel@edk2.groups.io
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Remove a duplicated entry to fix a CI error.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/UefiPayloadPkg.ci.yaml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
index 93f4d3603d..909379eaac 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
+++ b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
@@ -20,7 +20,6 @@
         "IgnoreFiles": [

             "Include/Coreboot.h",

             "Library/CbParseLib/CbParseLib.c",

-            "Library/CbParseLib/CbParseLib.c",

             "PayloadLoaderPeim/ElfLib/ElfCommon.h",

             "PayloadLoaderPeim/ElfLib/Elf32.h",

             "PayloadLoaderPeim/ElfLib/Elf64.h"

-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-21  9:10 ` [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Sean Rhodes
@ 2022-03-23 18:12   ` Guo Dong
  2022-03-23 18:24     ` Patrick Rudolph
  0 siblings, 1 reply; 14+ messages in thread
From: Guo Dong @ 2022-03-23 18:12 UTC (permalink / raw)
  To: Rhodes, Sean, devel@edk2.groups.io
  Cc: Rhodes, Sean, Ni, Ray, Ma, Maurice, You, Benjamin,
	Patrick Rudolph


Hi Sean,
SMMSTORE and this FVB module are specific for coreboot while UefiPayloadPkg in Edk2 is targeting platform/bootloader independent.
I just submitted an "add UEFI payload platform FV" patch to edk2-platforms repo, in this patch it moves PCH specific modules from
Edk2 UefiPayloadPkg to edk2-platforms Platform/Intel/PayloadPlatformPkg. Maybe these coreboot specific modules could also target
to edk2-platforms repo?

Thanks,
Guo
-----Original Message-----
From: Sean Rhodes <sean@starlabs.systems> 
Sent: Monday, March 21, 2022 2:10 AM
To: devel@edk2.groups.io
Cc: Rhodes, Sean <sean@starlabs.systems>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Patrick Rudolph <patrick.rudolph@9elements.com>
Subject: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol

This adds support for FVB in order to support a platform independent
and non-volatile variable store on UefiPayloadPkg. The variable store
makes use the SmmStoreLib to provide an unauthenticed variable store.

Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
the SMMSTORE v2 feature. It implements a SMI handler that is able to
write, read and erase pages in the boot media (SPI flash).
The communication is done using a fixed communication buffer that is
allocated in CBMEM. The existence of this optional feature is advertised
by a coreboot table.
When the SMMSTORE feature is not available the variable emulation is used
by setting PcdEmuVariableNvModeEnable to TRUE.

The DXE component provides runtime services and takes care of virtual to
physical mapping the communication buffers between SMM and OS.

The contents of the variable store can be accessed and modified by any
priviledged application. As authentication is done by runtime services
only the store shouldn't be used to store authenticated variables.

Tested on Linux and Windows 10 on real hardware.
Currently this cannot be tested on coreboot and qemu as it doesn't support
the SMMSTORE on qemu.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
 .../SmmStoreFvb/SmmStoreFvbRuntime.c          | 282 ++++++
 .../SmmStoreFvb/SmmStoreFvbRuntime.h          | 111 +++
 .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.c       | 849 ++++++++++++++++++
 .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf     |  66 ++
 UefiPayloadPkg/UefiPayloadPkg.dsc             |  24 +-
 UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
 6 files changed, 1330 insertions(+), 6 deletions(-)
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
 create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf

diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
new file mode 100644
index 0000000000..08d5c408fb
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
@@ -0,0 +1,282 @@
+/** @file  SmmStoreFvbRuntime.c

+

+  Copyright (c) 2022, 9elements GmbH<BR>

+

+  SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <Library/UefiLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/DxeServicesTableLib.h>

+#include <Library/DevicePathLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/PcdLib.h>

+#include <Library/SmmStoreLib.h>

+

+#include "SmmStoreFvbRuntime.h"

+

+STATIC EFI_EVENT  mSmmStoreVirtualAddrChangeEvent;

+

+//

+// Global variable declarations

+//

+SMMSTORE_INSTANCE  *mSmmStoreInstance;

+

+SMMSTORE_INSTANCE  mSmmStoreInstanceTemplate = {

+  SMMSTORE_SIGNATURE, // Signature

+  NULL,               // Handle ... NEED TO BE FILLED

+  {

+    FvbGetAttributes,      // GetAttributes

+    FvbSetAttributes,      // SetAttributes

+    FvbGetPhysicalAddress, // GetPhysicalAddress

+    FvbGetBlockSize,       // GetBlockSize

+    FvbRead,               // Read

+    FvbWrite,              // Write

+    FvbEraseBlocks,        // EraseBlocks

+    NULL,                  // ParentHandle

+  }, //  FvbProtoccol

+  0, // BlockSize ... NEED TO BE FILLED

+  0, // LastBlock ... NEED TO BE FILLED

+  0, // MmioAddress ... NEED TO BE FILLED

+  {

+    {

+      {

+        HARDWARE_DEVICE_PATH,

+        HW_MEMMAP_DP,

+        {

+          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),

+          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)

+        }

+      },

+      EfiMemoryMappedIO,

+      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED

+      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED

+    },

+    {

+      END_DEVICE_PATH_TYPE,

+      END_ENTIRE_DEVICE_PATH_SUBTYPE,

+      {

+        END_DEVICE_PATH_LENGTH,

+        0

+      }

+    }

+  } // DevicePath

+};

+

+/**

+  Initialize the SmmStore instance.

+

+

+  @param[in]      FvBase         The physical MMIO base address of the FV containing

+                                 the variable store.

+

+  @param[in]      NumberofBlocks Number of blocks within the FV.

+  @param[in]      BlockSize      The size in bytes of one block within the FV.

+  @param[in, out] Instance       The SmmStore instace to initialize

+

+**/

+STATIC

+EFI_STATUS

+SmmStoreInitInstance (

+  IN EFI_PHYSICAL_ADDRESS   FvBase,

+  IN UINTN                  NumberofBlocks,

+  IN UINTN                  BlockSize,

+  IN OUT SMMSTORE_INSTANCE  *Instance

+  )

+{

+  EFI_STATUS             Status;

+  FV_MEMMAP_DEVICE_PATH  *FvDevicePath;

+

+  ASSERT (Instance != NULL);

+

+  Instance->BlockSize   = BlockSize;

+  Instance->LastBlock   = NumberofBlocks - 1;

+  Instance->MmioAddress = FvBase;

+

+  FvDevicePath                                = &Instance->DevicePath;

+  FvDevicePath->MemMapDevPath.StartingAddress = FvBase;

+  FvDevicePath->MemMapDevPath.EndingAddress   = FvBase + BlockSize * NumberofBlocks - 1;

+

+  Status = FvbInitialize (Instance);

+  if (EFI_ERROR (Status)) {

+    return Status;

+  }

+

+  Status = gBS->InstallMultipleProtocolInterfaces (

+                  &Instance->Handle,

+                  &gEfiDevicePathProtocolGuid,

+                  &Instance->DevicePath,

+                  &gEfiFirmwareVolumeBlockProtocolGuid,

+                  &Instance->FvbProtocol,

+                  NULL

+                  );

+  if (EFI_ERROR (Status)) {

+    return Status;

+  }

+

+  DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__));

+

+  return Status;

+}

+

+/**

+  Fixup internal data so that EFI can be call in virtual mode.

+  Call the passed in Child Notify event and convert any pointers in

+  lib to virtual mode.

+

+  @param[in]    Event   The Event that is being processed

+  @param[in]    Context Event Context

+**/

+STATIC

+VOID

+EFIAPI

+SmmStoreVirtualNotifyEvent (

+  IN EFI_EVENT  Event,

+  IN VOID       *Context

+  )

+{

+  // Convert Fvb

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.EraseBlocks);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetAttributes);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetBlockSize);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetPhysicalAddress);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Read);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.SetAttributes);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Write);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->MmioAddress);

+  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance);

+

+  return;

+}

+

+/**

+  The user Entry Point for module SmmStoreFvbRuntimeDxe. The user code starts with this function.

+

+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.

+  @param[in] SystemTable    A pointer to the EFI System Table.

+

+  @retval EFI_SUCCESS       The entry point is executed successfully.

+  @retval other             Some error occurs when executing this entry point.

+

+**/

+EFI_STATUS

+EFIAPI

+SmmStoreInitialize (

+  IN EFI_HANDLE        ImageHandle,

+  IN EFI_SYSTEM_TABLE  *SystemTable

+  )

+{

+  EFI_STATUS            Status;

+  EFI_PHYSICAL_ADDRESS  MmioAddress;

+  UINTN                 BlockSize;

+  UINTN                 BlockCount;

+  UINT32                NvStorageBase;

+  UINT32                NvStorageSize;

+  UINT32                NvVariableSize;

+  UINT32                FtwWorkingSize;

+  UINT32                FtwSpareSize;

+

+  Status = SmmStoreLibInitialize ();

+  if (EFI_ERROR (Status)) {

+    DEBUG ((DEBUG_ERROR, "%a: Failed to initialize SmmStoreLib\n", __FUNCTION__));

+    return Status;

+  }

+

+  Status = SmmStoreLibGetMmioAddress (&MmioAddress);

+  if (EFI_ERROR (Status)) {

+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore MMIO address\n", __FUNCTION__));

+    SmmStoreLibDeinitialize ();

+    return Status;

+  }

+

+  Status = SmmStoreLibGetNumBlocks (&BlockCount);

+  if (EFI_ERROR (Status)) {

+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore No. blocks\n", __FUNCTION__));

+    SmmStoreLibDeinitialize ();

+    return Status;

+  }

+

+  Status = SmmStoreLibGetBlockSize (&BlockSize);

+  if (EFI_ERROR (Status)) {

+    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore block size\n", __FUNCTION__));

+    SmmStoreLibDeinitialize ();

+    return Status;

+  }

+

+  NvStorageSize = BlockCount * BlockSize;

+  NvStorageBase = MmioAddress;

+

+  FtwSpareSize   = (BlockCount / 2) * BlockSize;

+  FtwWorkingSize = BlockSize;

+  NvVariableSize = NvStorageSize - FtwSpareSize - FtwWorkingSize;

+  DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase, NvStorageSize));

+

+  if (NvVariableSize >= 0x80000000) {

+    SmmStoreLibDeinitialize ();

+    return EFI_INVALID_PARAMETER;

+  }

+

+  Status = PcdSet32S (PcdFlashNvStorageVariableSize, NvVariableSize);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet32S (PcdFlashNvStorageVariableBase, NvStorageBase);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet64S (PcdFlashNvStorageVariableBase64, NvStorageBase);

+  ASSERT_EFI_ERROR (Status);

+

+  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64, NvStorageBase + NvVariableSize);

+  ASSERT_EFI_ERROR (Status);

+

+  Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + NvVariableSize + FtwWorkingSize);

+  ASSERT_EFI_ERROR (Status);

+  Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64, NvStorageBase + NvVariableSize + FtwWorkingSize);

+  ASSERT_EFI_ERROR (Status);

+

+  mSmmStoreInstance = AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSmmStoreInstanceTemplate);

+  if (mSmmStoreInstance == NULL) {

+    SmmStoreLibDeinitialize ();

+    DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__));

+    return EFI_OUT_OF_RESOURCES;

+  }

+

+  Status = SmmStoreInitInstance (

+             MmioAddress,

+             BlockCount,

+             BlockSize,

+             mSmmStoreInstance

+             );

+  if (EFI_ERROR (Status)) {

+    DEBUG (

+      (

+       DEBUG_ERROR,

+       "%a: Fail to create instance for SmmStore\n",

+       __FUNCTION__

+      )

+      );

+    FreePool (mSmmStoreInstance);

+    SmmStoreLibDeinitialize ();

+    return Status;

+  }

+

+  //

+  // Register for the virtual address change event

+  //

+  Status = gBS->CreateEventEx (

+                  EVT_NOTIFY_SIGNAL,

+                  TPL_NOTIFY,

+                  SmmStoreVirtualNotifyEvent,

+                  NULL,

+                  &gEfiEventVirtualAddressChangeGuid,

+                  &mSmmStoreVirtualAddrChangeEvent

+                  );

+  ASSERT_EFI_ERROR (Status);

+

+  return Status;

+}

diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
new file mode 100644
index 0000000000..8015d12d13
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
@@ -0,0 +1,111 @@
+/** @file  SmmStoreFvbRuntime.h

+

+  Copyright (c) 2022, 9elements GmbH<BR>

+

+  SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef SMM_STORE_DXE_H_

+#define SMM_STORE_DXE_H_

+

+#include <Base.h>

+#include <PiDxe.h>

+

+#include <Guid/EventGroup.h>

+

+#include <Protocol/FirmwareVolumeBlock.h>

+

+#include <Library/DebugLib.h>

+#include <Library/IoLib.h>

+#include <Library/UefiLib.h>

+#include <Library/UefiRuntimeLib.h>

+

+#define SMMSTORE_SIGNATURE  SIGNATURE_32('S', 'M', 'M', 'S')

+#define INSTANCE_FROM_FVB_THIS(a)  CR(a, SMMSTORE_INSTANCE, FvbProtocol, SMMSTORE_SIGNATURE)

+

+typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE;

+

+typedef struct {

+  MEMMAP_DEVICE_PATH          MemMapDevPath;

+  EFI_DEVICE_PATH_PROTOCOL    EndDevPath;

+} FV_MEMMAP_DEVICE_PATH;

+

+struct _SMMSTORE_INSTANCE {

+  UINT32                                 Signature;

+  EFI_HANDLE                             Handle;

+  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL    FvbProtocol;

+  UINTN                                  BlockSize;

+  UINTN                                  LastBlock;

+  EFI_PHYSICAL_ADDRESS                   MmioAddress;

+  FV_MEMMAP_DEVICE_PATH                  DevicePath;

+};

+

+//

+// SmmStoreFvbRuntimeDxe.c

+//

+

+EFI_STATUS

+EFIAPI

+FvbInitialize (

+  IN SMMSTORE_INSTANCE  *Instance

+  );

+

+EFI_STATUS

+EFIAPI

+FvbGetAttributes (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes

+  );

+

+EFI_STATUS

+EFIAPI

+FvbSetAttributes (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes

+  );

+

+EFI_STATUS

+EFIAPI

+FvbGetPhysicalAddress (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  OUT       EFI_PHYSICAL_ADDRESS                 *Address

+  );

+

+EFI_STATUS

+EFIAPI

+FvbGetBlockSize (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  OUT       UINTN                                *BlockSize,

+  OUT       UINTN                                *NumberOfBlocks

+  );

+

+EFI_STATUS

+EFIAPI

+FvbRead (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  IN        UINTN                                Offset,

+  IN OUT    UINTN                                *NumBytes,

+  IN OUT    UINT8                                *Buffer

+  );

+

+EFI_STATUS

+EFIAPI

+FvbWrite (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  IN        UINTN                                Offset,

+  IN OUT    UINTN                                *NumBytes,

+  IN        UINT8                                *Buffer

+  );

+

+EFI_STATUS

+EFIAPI

+FvbEraseBlocks (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  ...

+  );

+

+#endif // SMM_STORE_DXE_H_

diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
new file mode 100644
index 0000000000..14d4ec324d
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
@@ -0,0 +1,849 @@
+/** @file  SmmStoreFvbRuntimeDxe.c

+

+  Copyright (c) 2022, 9elements GmbH<BR>

+

+  SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <PiDxe.h>

+

+#include <Library/PcdLib.h>

+#include <Library/BaseLib.h>

+#include <Library/HobLib.h>

+#include <Library/UefiLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/DxeServicesTableLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/SmmStoreLib.h>

+

+#include <Guid/VariableFormat.h>

+#include <Guid/SystemNvDataGuid.h>

+#include <Guid/NvVarStoreFormatted.h>

+

+#include "SmmStoreFvbRuntime.h"

+

+///

+/// The Firmware Volume Block Protocol is the low-level interface

+/// to a firmware volume. File-level access to a firmware volume

+/// should not be done using the Firmware Volume Block Protocol.

+/// Normal access to a firmware volume must use the Firmware

+/// Volume Protocol. Typically, only the file system driver that

+/// produces the Firmware Volume Protocol will bind to the

+/// Firmware Volume Block Protocol.

+///

+

+/**

+  Initialises the FV Header and Variable Store Header

+  to support variable operations.

+

+  @param[in]  Instance - Pointer to SmmStore instance

+

+**/

+EFI_STATUS

+InitializeFvAndVariableStoreHeaders (

+  IN SMMSTORE_INSTANCE  *Instance

+  )

+{

+  EFI_STATUS                  Status;

+  VOID                        *Headers;

+  UINTN                       HeadersLength;

+  EFI_FIRMWARE_VOLUME_HEADER  *FirmwareVolumeHeader;

+  VARIABLE_STORE_HEADER       *VariableStoreHeader;

+  UINT32                      NvStorageFtwSpareSize;

+  UINT32                      NvStorageFtwWorkingSize;

+  UINT32                      NvStorageVariableSize;

+  UINT64                      NvStorageFtwSpareBase;

+  UINT64                      NvStorageFtwWorkingBase;

+  UINT64                      NvStorageVariableBase;

+

+  HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);

+  Headers       = AllocateZeroPool (HeadersLength);

+

+  NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);

+  NvStorageFtwSpareSize   = PcdGet32 (PcdFlashNvStorageFtwSpareSize);

+  NvStorageVariableSize   = PcdGet32 (PcdFlashNvStorageVariableSize);

+

+  NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?

+                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);

+  NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?

+                            PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);

+  NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?

+                          PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);

+

+  // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.

+  if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",

+      __FUNCTION__

+      ));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",

+      __FUNCTION__

+      ));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  // Check if the size of the area is at least one block size

+  if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",

+      __FUNCTION__,

+      NvStorageVariableSize

+      ));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",

+      __FUNCTION__,

+      NvStorageFtwWorkingSize

+      ));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",

+      __FUNCTION__,

+      NvStorageFtwSpareSize

+      ));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  // Ensure the Variable area Base Addresses are aligned on a block size boundaries

+  if ((NvStorageVariableBase % Instance->BlockSize != 0) ||

+      (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||

+      (NvStorageFtwSpareBase % Instance->BlockSize != 0))

+  {

+    DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));

+    return EFI_INVALID_PARAMETER;

+  }

+

+  //

+  // EFI_FIRMWARE_VOLUME_HEADER

+  //

+  FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;

+  CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);

+  FirmwareVolumeHeader->FvLength =

+    PcdGet32 (PcdFlashNvStorageVariableSize) +

+    PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +

+    PcdGet32 (PcdFlashNvStorageFtwSpareSize);

+  FirmwareVolumeHeader->Signature  = EFI_FVH_SIGNATURE;

+  FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(

+                                                            EFI_FVB2_READ_ENABLED_CAP   | // Reads may be enabled

+                                                            EFI_FVB2_READ_STATUS        | // Reads are currently enabled

+                                                            EFI_FVB2_STICKY_WRITE       | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY

+                                                            EFI_FVB2_MEMORY_MAPPED      | // It is memory mapped

+                                                            EFI_FVB2_ERASE_POLARITY     | // After erasure all bits take this value (i.e. '1')

+                                                            EFI_FVB2_WRITE_STATUS       | // Writes are currently enabled

+                                                            EFI_FVB2_WRITE_ENABLED_CAP    // Writes may be enabled

+                                                            );

+  FirmwareVolumeHeader->HeaderLength          = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);

+  FirmwareVolumeHeader->Revision              = EFI_FVH_REVISION;

+  FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;

+  FirmwareVolumeHeader->BlockMap[0].Length    = Instance->BlockSize;

+  FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;

+  FirmwareVolumeHeader->BlockMap[1].Length    = 0;

+  FirmwareVolumeHeader->Checksum              = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);

+

+  //

+  // VARIABLE_STORE_HEADER

+  //

+  VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);

+  CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);

+  VariableStoreHeader->Size   = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;

+  VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;

+  VariableStoreHeader->State  = VARIABLE_STORE_HEALTHY;

+

+  // Install the combined super-header in the NorFlash

+  Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);

+

+  FreePool (Headers);

+  return Status;

+}

+

+/**

+  Check the integrity of firmware volume header.

+

+  @retval  EFI_SUCCESS   - The firmware volume is consistent

+  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.

+

+**/

+EFI_STATUS

+ValidateFvHeader (

+  VOID

+  )

+{

+  UINT16                      Checksum;

+  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;

+  VARIABLE_STORE_HEADER       *VariableStoreHeader;

+  UINTN                       VariableStoreLength;

+  UINTN                       FvLength;

+  EFI_STATUS                  TempStatus;

+  UINTN                       BufferSize;

+  UINTN                       BufferSizeReqested;

+

+  BufferSizeReqested = sizeof (EFI_FIRMWARE_VOLUME_HEADER);

+  FwVolHeader        = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);

+  if (!FwVolHeader) {

+    return EFI_OUT_OF_RESOURCES;

+  }

+

+  BufferSize = BufferSizeReqested;

+  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);

+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {

+    FreePool (FwVolHeader);

+    return EFI_DEVICE_ERROR;

+  }

+

+  FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +

+             PcdGet32 (PcdFlashNvStorageFtwSpareSize);

+

+  //

+  // Verify the header revision, header signature, length

+  // Length of FvBlock cannot be 2**64-1

+  // HeaderLength cannot be an odd number

+  //

+  if (  (FwVolHeader->Revision  != EFI_FVH_REVISION)

+     || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)

+     || (FwVolHeader->FvLength  != FvLength)

+        )

+  {

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: No Firmware Volume header present\n",

+      __FUNCTION__

+      ));

+    FreePool (FwVolHeader);

+    return EFI_NOT_FOUND;

+  }

+

+  // Check the Firmware Volume Guid

+  if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: Firmware Volume Guid non-compatible\n",

+      __FUNCTION__

+      ));

+    FreePool (FwVolHeader);

+    return EFI_NOT_FOUND;

+  }

+

+  BufferSizeReqested = FwVolHeader->HeaderLength;

+  FreePool (FwVolHeader);

+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);

+  if (!FwVolHeader) {

+    return EFI_OUT_OF_RESOURCES;

+  }

+

+  BufferSize = BufferSizeReqested;

+  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);

+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {

+    FreePool (FwVolHeader);

+    return EFI_DEVICE_ERROR;

+  }

+

+  // Verify the header checksum

+  Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);

+  if (Checksum != 0) {

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: FV checksum is invalid (Checksum:0x%X)\n",

+      __FUNCTION__,

+      Checksum

+      ));

+    FreePool (FwVolHeader);

+    return EFI_NOT_FOUND;

+  }

+

+  BufferSizeReqested  = sizeof (VARIABLE_STORE_HEADER);

+  VariableStoreHeader = (VARIABLE_STORE_HEADER *)AllocatePool (BufferSizeReqested);

+  if (!VariableStoreHeader) {

+    return EFI_OUT_OF_RESOURCES;

+  }

+

+  BufferSize = BufferSizeReqested;

+  TempStatus = SmmStoreLibRead (0, FwVolHeader->HeaderLength, &BufferSize, (UINT8 *)VariableStoreHeader);

+  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {

+    FreePool (VariableStoreHeader);

+    FreePool (FwVolHeader);

+    return EFI_DEVICE_ERROR;

+  }

+

+  // Check the Variable Store Guid

+  if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&

+      !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))

+  {

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: Variable Store Guid non-compatible\n",

+      __FUNCTION__

+      ));

+    FreePool (FwVolHeader);

+    FreePool (VariableStoreHeader);

+    return EFI_NOT_FOUND;

+  }

+

+  VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;

+  if (VariableStoreHeader->Size != VariableStoreLength) {

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: Variable Store Length does not match\n",

+      __FUNCTION__

+      ));

+    FreePool (FwVolHeader);

+    FreePool (VariableStoreHeader);

+    return EFI_NOT_FOUND;

+  }

+

+  FreePool (FwVolHeader);

+  FreePool (VariableStoreHeader);

+

+  return EFI_SUCCESS;

+}

+

+/**

+ The GetAttributes() function retrieves the attributes and

+ current settings of the block.

+

+ @param This         Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Attributes   Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and

+                     current settings are returned.

+                     Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.

+

+ @retval EFI_SUCCESS The firmware volume attributes were returned.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbGetAttributes (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes

+  )

+{

+  EFI_FVB_ATTRIBUTES_2  FlashFvbAttributes;

+

+  FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(

+                                              EFI_FVB2_READ_STATUS      | // Reads are currently enabled

+                                              EFI_FVB2_WRITE_STATUS     | // Writes are enabled

+                                              EFI_FVB2_STICKY_WRITE     | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY

+                                              EFI_FVB2_MEMORY_MAPPED    | // It is memory mapped

+                                              EFI_FVB2_ERASE_POLARITY     // After erasure all bits take this value (i.e. '1')

+                                              );

+

+  *Attributes = FlashFvbAttributes;

+

+  DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));

+

+  return EFI_SUCCESS;

+}

+

+/**

+ The SetAttributes() function sets configurable firmware volume attributes

+ and returns the new settings of the firmware volume.

+

+

+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Attributes               On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2

+                                 that contains the desired firmware volume settings.

+                                 On successful return, it contains the new settings of

+                                 the firmware volume.

+                                 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.

+

+ @retval EFI_SUCCESS             The firmware volume attributes were returned.

+

+ @retval EFI_INVALID_PARAMETER   The attributes requested are in conflict with the capabilities

+                                 as declared in the firmware volume header.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbSetAttributes (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes

+  )

+{

+  DEBUG ((DEBUG_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));

+  return EFI_UNSUPPORTED;

+}

+

+/**

+ The GetPhysicalAddress() function retrieves the base address of

+ a memory-mapped firmware volume. This function should be called

+ only for memory-mapped firmware volumes.

+

+ @param This               Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Address            Pointer to a caller-allocated

+                           EFI_PHYSICAL_ADDRESS that, on successful

+                           return from GetPhysicalAddress(), contains the

+                           base address of the firmware volume.

+

+ @retval EFI_SUCCESS       The firmware volume base address was returned.

+

+ @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbGetPhysicalAddress (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  OUT       EFI_PHYSICAL_ADDRESS                 *Address

+  )

+{

+  SMMSTORE_INSTANCE  *Instance;

+

+  Instance = INSTANCE_FROM_FVB_THIS (This);

+

+  ASSERT (Address != NULL);

+  *Address = Instance->MmioAddress;

+

+  return EFI_SUCCESS;

+}

+

+/**

+ The GetBlockSize() function retrieves the size of the requested

+ block. It also returns the number of additional blocks with

+ the identical size. The GetBlockSize() function is used to

+ retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).

+

+

+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Lba                      Indicates the block for which to return the size.

+

+ @param BlockSize                Pointer to a caller-allocated UINTN in which

+                                 the size of the block is returned.

+

+ @param NumberOfBlocks           Pointer to a caller-allocated UINTN in

+                                 which the number of consecutive blocks,

+                                 starting with Lba, is returned. All

+                                 blocks in this range have a size of

+                                 BlockSize.

+

+

+ @retval EFI_SUCCESS             The firmware volume base address was returned.

+

+ @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbGetBlockSize (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  OUT       UINTN                                *BlockSize,

+  OUT       UINTN                                *NumberOfBlocks

+  )

+{

+  EFI_STATUS         Status;

+  SMMSTORE_INSTANCE  *Instance;

+

+  Instance = INSTANCE_FROM_FVB_THIS (This);

+

+  DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));

+

+  if (Lba > Instance->LastBlock) {

+    DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));

+    Status = EFI_INVALID_PARAMETER;

+  } else {

+    *BlockSize      = (UINTN)Instance->BlockSize;

+    *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);

+

+    DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));

+

+    Status = EFI_SUCCESS;

+  }

+

+  return Status;

+}

+

+/**

+ Reads the specified number of bytes into a buffer from the specified block.

+

+ The Read() function reads the requested number of bytes from the

+ requested block and stores them in the provided buffer.

+ Implementations should be mindful that the firmware volume

+ might be in the ReadDisabled state. If it is in this state,

+ the Read() function must return the status code

+ EFI_ACCESS_DENIED without modifying the contents of the

+ buffer. The Read() function must also prevent spanning block

+ boundaries. If a read is requested that would span a block

+ boundary, the read must read up to the boundary but not

+ beyond. The output parameter NumBytes must be set to correctly

+ indicate the number of bytes actually read. The caller must be

+ aware that a read may be partially completed.

+

+ @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Lba                  The starting logical block index from which to read.

+

+ @param Offset               Offset into the block at which to begin reading.

+

+ @param NumBytes             Pointer to a UINTN.

+                             At entry, *NumBytes contains the total size of the buffer.

+                             At exit, *NumBytes contains the total number of bytes read.

+

+ @param Buffer               Pointer to a caller-allocated buffer that will be used

+                             to hold the data that is read.

+

+ @retval EFI_SUCCESS         The firmware volume was read successfully,  and contents are

+                             in Buffer.

+

+ @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.

+                             On output, NumBytes contains the total number of bytes

+                             returned in Buffer.

+

+ @retval EFI_ACCESS_DENIED   The firmware volume is in the ReadDisabled state.

+

+ @retval EFI_DEVICE_ERROR    The block device is not functioning correctly and could not be read.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbRead (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  IN        UINTN                                Offset,

+  IN OUT    UINTN                                *NumBytes,

+  IN OUT    UINT8                                *Buffer

+  )

+{

+  UINTN              BlockSize;

+  SMMSTORE_INSTANCE  *Instance;

+

+  Instance = INSTANCE_FROM_FVB_THIS (This);

+

+  DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));

+

+  // Cache the block size to avoid de-referencing pointers all the time

+  BlockSize = Instance->BlockSize;

+

+  // The read must not span block boundaries.

+  // We need to check each variable individually because adding two large values together overflows.

+  if ((Offset               >= BlockSize) ||

+      (*NumBytes            >  BlockSize) ||

+      ((Offset + *NumBytes) >  BlockSize))

+  {

+    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  // We must have some bytes to read

+  if (*NumBytes == 0) {

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  return SmmStoreLibRead (Lba, Offset, NumBytes, Buffer);

+}

+

+/**

+ Writes the specified number of bytes from the input buffer to the block.

+

+ The Write() function writes the specified number of bytes from

+ the provided buffer to the specified block and offset. If the

+ firmware volume is sticky write, the caller must ensure that

+ all the bits of the specified range to write are in the

+ EFI_FVB_ERASE_POLARITY state before calling the Write()

+ function, or else the result will be unpredictable. This

+ unpredictability arises because, for a sticky-write firmware

+ volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY

+ state but cannot flip it back again.  Before calling the

+ Write() function,  it is recommended for the caller to first call

+ the EraseBlocks() function to erase the specified block to

+ write. A block erase cycle will transition bits from the

+ (NOT)EFI_FVB_ERASE_POLARITY state back to the

+ EFI_FVB_ERASE_POLARITY state. Implementations should be

+ mindful that the firmware volume might be in the WriteDisabled

+ state. If it is in this state, the Write() function must

+ return the status code EFI_ACCESS_DENIED without modifying the

+ contents of the firmware volume. The Write() function must

+ also prevent spanning block boundaries. If a write is

+ requested that spans a block boundary, the write must store up

+ to the boundary but not beyond. The output parameter NumBytes

+ must be set to correctly indicate the number of bytes actually

+ written. The caller must be aware that a write may be

+ partially completed. All writes, partial or otherwise, must be

+ fully flushed to the hardware before the Write() service

+ returns.

+

+ @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.

+

+ @param Lba                  The starting logical block index to write to.

+

+ @param Offset               Offset into the block at which to begin writing.

+

+ @param NumBytes             The pointer to a UINTN.

+                             At entry, *NumBytes contains the total size of the buffer.

+                             At exit, *NumBytes contains the total number of bytes actually written.

+

+ @param Buffer               The pointer to a caller-allocated buffer that contains the source for the write.

+

+ @retval EFI_SUCCESS         The firmware volume was written successfully.

+

+ @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.

+                             On output, NumBytes contains the total number of bytes

+                             actually written.

+

+ @retval EFI_ACCESS_DENIED   The firmware volume is in the WriteDisabled state.

+

+ @retval EFI_DEVICE_ERROR    The block device is malfunctioning and could not be written.

+

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbWrite (

+  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  IN        EFI_LBA                              Lba,

+  IN        UINTN                                Offset,

+  IN OUT    UINTN                                *NumBytes,

+  IN        UINT8                                *Buffer

+  )

+{

+  UINTN              BlockSize;

+  SMMSTORE_INSTANCE  *Instance;

+

+  Instance = INSTANCE_FROM_FVB_THIS (This);

+

+  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));

+

+  // Cache the block size to avoid de-referencing pointers all the time

+  BlockSize = Instance->BlockSize;

+

+  // The read must not span block boundaries.

+  // We need to check each variable individually because adding two large values together overflows.

+  if ((Offset               >= BlockSize) ||

+      (*NumBytes            >  BlockSize) ||

+      ((Offset + *NumBytes) >  BlockSize))

+  {

+    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  // We must have some bytes to read

+  if (*NumBytes == 0) {

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  return SmmStoreLibWrite (Lba, Offset, NumBytes, Buffer);

+}

+

+/**

+ Erases and initialises a firmware volume block.

+

+ The EraseBlocks() function erases one or more blocks as denoted

+ by the variable argument list. The entire parameter list of

+ blocks must be verified before erasing any blocks. If a block is

+ requested that does not exist within the associated firmware

+ volume (it has a larger index than the last block of the

+ firmware volume), the EraseBlocks() function must return the

+ status code EFI_INVALID_PARAMETER without modifying the contents

+ of the firmware volume. Implementations should be mindful that

+ the firmware volume might be in the WriteDisabled state. If it

+ is in this state, the EraseBlocks() function must return the

+ status code EFI_ACCESS_DENIED without modifying the contents of

+ the firmware volume. All calls to EraseBlocks() must be fully

+ flushed to the hardware before the EraseBlocks() service

+ returns.

+

+ @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL

+ instance.

+

+ @param ...                      The variable argument list is a list of tuples.

+                                 Each tuple describes a range of LBAs to erase

+                                 and consists of the following:

+                                 - An EFI_LBA that indicates the starting LBA

+                                 - A UINTN that indicates the number of blocks to erase.

+

+                                 The list is terminated with an EFI_LBA_LIST_TERMINATOR.

+                                 For example, the following indicates that two ranges of blocks

+                                 (5-7 and 10-11) are to be erased:

+                                 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);

+

+ @retval EFI_SUCCESS             The erase request successfully completed.

+

+ @retval EFI_ACCESS_DENIED       The firmware volume is in the WriteDisabled state.

+

+ @retval EFI_DEVICE_ERROR        The block device is not functioning correctly and could not be written.

+                                 The firmware device may have been partially erased.

+

+ @retval EFI_INVALID_PARAMETER   One or more of the LBAs listed in the variable argument list do

+                                 not exist in the firmware volume.

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbEraseBlocks (

+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,

+  ...

+  )

+{

+  EFI_STATUS         Status;

+  VA_LIST            Args;

+  EFI_LBA            StartingLba; // Lba from which we start erasing

+  UINTN              NumOfLba;    // Number of Lba blocks to erase

+  SMMSTORE_INSTANCE  *Instance;

+

+  Instance = INSTANCE_FROM_FVB_THIS (This);

+

+  Status = EFI_SUCCESS;

+

+  // Before erasing, check the entire list of parameters to ensure all specified blocks are valid

+

+  VA_START (Args, This);

+  do {

+    // Get the Lba from which we start erasing

+    StartingLba = VA_ARG (Args, EFI_LBA);

+

+    // Have we reached the end of the list?

+    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {

+      // Exit the while loop

+      break;

+    }

+

+    // How many Lba blocks are we requested to erase?

+    NumOfLba = VA_ARG (Args, UINTN);

+

+    // All blocks must be within range

+    DEBUG ((

+      DEBUG_BLKIO,

+      "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",

+      StartingLba,

+      (UINT64)NumOfLba,

+      Instance->LastBlock

+      ));

+    if ((NumOfLba == 0) || ((StartingLba + NumOfLba - 1) > Instance->LastBlock)) {

+      VA_END (Args);

+      DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));

+      Status = EFI_INVALID_PARAMETER;

+      goto EXIT;

+    }

+  } while (TRUE);

+

+  VA_END (Args);

+

+  //

+  // To get here, all must be ok, so start erasing

+  //

+  VA_START (Args, This);

+  do {

+    // Get the Lba from which we start erasing

+    StartingLba = VA_ARG (Args, EFI_LBA);

+

+    // Have we reached the end of the list?

+    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {

+      // Exit the while loop

+      break;

+    }

+

+    // How many Lba blocks are we requested to erase?

+    NumOfLba = VA_ARG (Args, UINTN);

+

+    // Go through each one and erase it

+    while (NumOfLba > 0) {

+      // Erase it

+      DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld\n", StartingLba));

+      Status = SmmStoreLibEraseBlock (StartingLba);

+      if (EFI_ERROR (Status)) {

+        VA_END (Args);

+        Status = EFI_DEVICE_ERROR;

+        goto EXIT;

+      }

+

+      // Move to the next Lba

+      StartingLba++;

+      NumOfLba--;

+    }

+  } while (TRUE);

+

+  VA_END (Args);

+

+EXIT:

+  return Status;

+}

+

+/**

+  Initialized the Firmware Volume if necessary and installs the

+  gEdkiiNvVarStoreFormattedGuid protocol.

+

+  @param Instance                    Pointer to SmmStore instance

+

+ **/

+EFI_STATUS

+EFIAPI

+FvbInitialize (

+  IN SMMSTORE_INSTANCE  *Instance

+  )

+{

+  EFI_STATUS     Status;

+  UINT32         FvbNumLba;

+  EFI_BOOT_MODE  BootMode;

+

+  ASSERT ((Instance != NULL));

+

+  BootMode = GetBootModeHob ();

+  if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {

+    Status = EFI_INVALID_PARAMETER;

+  } else {

+    // Determine if there is a valid header at the beginning of the NorFlash

+    Status = ValidateFvHeader ();

+  }

+

+  // Install the Default FVB header if required

+  if (EFI_ERROR (Status)) {

+    // There is no valid header, so time to install one.

+    DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));

+    DEBUG ((

+      DEBUG_INFO,

+      "%a: Installing a correct one for this volume.\n",

+      __FUNCTION__

+      ));

+

+    // Erase all the NorFlash that is reserved for variable storage

+    FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) +

+                 PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +

+                 PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;

+

+    Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);

+    if (EFI_ERROR (Status)) {

+      return Status;

+    }

+

+    // Install all appropriate headers

+    Status = InitializeFvAndVariableStoreHeaders (Instance);

+    if (EFI_ERROR (Status)) {

+      return Status;

+    }

+  } else {

+    DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__));

+  }

+

+  //

+  // The driver implementing the variable read service can now be dispatched;

+  // the varstore headers are in place.

+  //

+  Status = gBS->InstallProtocolInterface (

+                  &gImageHandle,

+                  &gEdkiiNvVarStoreFormattedGuid,

+                  EFI_NATIVE_INTERFACE,

+                  NULL

+                  );

+  ASSERT_EFI_ERROR (Status);

+

+  return Status;

+}

diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
new file mode 100644
index 0000000000..2bd8886bcc
--- /dev/null
+++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
@@ -0,0 +1,66 @@
+## @file

+#  Component description file for SmmStore module

+#

+#  This module produces the FirmwareVolumeBlock protocol on top of the

+#  SmmStore provided by bootloader and installs the NvVarStoreFormatted protocol.

+#

+#  Copyright (c) 2022, 9elements GmbH<BR>

+#

+#  SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+##

+

+[Defines]

+  INF_VERSION                    = 0x00010005

+  BASE_NAME                      = SmmStoreFvbRuntimeDxe

+  FILE_GUID                      = A0402FCA-6B25-4CEA-B7DD-C08F99714B29

+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER

+  VERSION_STRING                 = 1.0

+  ENTRY_POINT                    = SmmStoreInitialize

+

+[Sources.common]

+  SmmStoreFvbRuntimeDxe.c

+  SmmStoreFvbRuntime.h

+  SmmStoreFvbRuntime.c

+

+[Packages]

+  MdePkg/MdePkg.dec

+  MdeModulePkg/MdeModulePkg.dec

+  EmbeddedPkg/EmbeddedPkg.dec

+  UefiPayloadPkg/UefiPayloadPkg.dec

+

+[LibraryClasses]

+  BaseLib

+  DebugLib

+  HobLib

+  SmmStoreLib

+  UefiLib

+  UefiDriverEntryPoint

+  UefiBootServicesTableLib

+  UefiRuntimeLib

+  DxeServicesTableLib

+

+[Guids]

+  gEfiSystemNvDataFvGuid

+  gEfiVariableGuid                  ## PRODUCES ## PROTOCOL

+  gEfiAuthenticatedVariableGuid

+  gEfiEventVirtualAddressChangeGuid

+  gEdkiiNvVarStoreFormattedGuid     ## PRODUCES ## PROTOCOL

+

+[Protocols]

+  gEfiDevicePathProtocolGuid          ## BY_START

+  gEfiFirmwareVolumeBlockProtocolGuid ## BY_START

+

+[Pcd]

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64

+

+[Depex]

+  TRUE

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index cf440fee58..0f6421612c 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -97,6 +97,7 @@
   #

   # EMU:      UEFI payload with EMU variable

   # SPI:      UEFI payload with SPI NV variable support

+  # SMMSTORE: UEFI payload with coreboot SMM NV variable support

   # NONE:     UEFI payload with no variable modules

   #

   DEFINE VARIABLE_SUPPORT      = EMU

@@ -261,9 +262,11 @@
   LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf

   FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf

   AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf

-  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf

 !if $(VARIABLE_SUPPORT) == "EMU"

   TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf

+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"

+  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf

+  TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf

 !elseif $(VARIABLE_SUPPORT) == "SPI"

   PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf

   TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf

@@ -379,15 +382,13 @@
   gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE

 

 [PcdsFixedAtBuild]

-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000

   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000

   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000

 !if $(VARIABLE_SUPPORT) == "EMU"

   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |TRUE

-!else

+!elseif $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"

   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |FALSE

 !endif

-

   gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0

   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE

   gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE

@@ -445,7 +446,6 @@
   gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|$(MAX_LOGICAL_PROCESSORS)

   gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0

   gUefiPayloadPkgTokenSpaceGuid.PcdBootloaderParameter|0

-

 ################################################################################

 #

 # Pcd DynamicEx Section - list of all EDK II PCD Entries defined by this Platform

@@ -473,15 +473,19 @@
 !endif

   gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FALSE

   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0

   gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3

-!if $(VARIABLE_SUPPORT) == "SPI"

+!if $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize  |0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize  |0

   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase  |0

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0

+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0

 !endif

   # Disable SMM S3 script

   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE

@@ -697,6 +701,14 @@
 

 !if $(VARIABLE_SUPPORT) == "EMU"

   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf

+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"

+  UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf

+  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf

+  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {

+    <LibraryClasses>

+      NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf

+      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf

+  }

 !elseif $(VARIABLE_SUPPORT) == "SPI"

   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf {

     <LibraryClasses>

diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
index c7b04978ad..ca44f48f6d 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -140,6 +140,10 @@ INF PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
 

 !if $(VARIABLE_SUPPORT) == "EMU"

   INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf

+!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"

+  INF UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf

+  INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf

+  INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf

 !elseif $(VARIABLE_SUPPORT) == "SPI"

   INF UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf

   INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf

-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-23 18:12   ` Guo Dong
@ 2022-03-23 18:24     ` Patrick Rudolph
  2022-03-23 19:27       ` [edk2-devel] " Guo Dong
  0 siblings, 1 reply; 14+ messages in thread
From: Patrick Rudolph @ 2022-03-23 18:24 UTC (permalink / raw)
  To: Dong, Guo
  Cc: Rhodes, Sean, devel@edk2.groups.io, Ni, Ray, Ma, Maurice,
	You, Benjamin

Hi Guo,
I don't see where it's written that UefiPayloadPkg is bootloader
independent. Can you point me to that documentation?
Reading the BuildAndIntegrationInstructions.txt I understand that it
is very bootloader specific, as it's  basically designed as a
bootloader payload.
Where do you draw the line between platform code and "generic" payload code?

Regards,
Patrick Rudolph

On Wed, Mar 23, 2022 at 7:12 PM Dong, Guo <guo.dong@intel.com> wrote:
>
>
> Hi Sean,
> SMMSTORE and this FVB module are specific for coreboot while UefiPayloadPkg in Edk2 is targeting platform/bootloader independent.
> I just submitted an "add UEFI payload platform FV" patch to edk2-platforms repo, in this patch it moves PCH specific modules from
> Edk2 UefiPayloadPkg to edk2-platforms Platform/Intel/PayloadPlatformPkg. Maybe these coreboot specific modules could also target
> to edk2-platforms repo?
>
> Thanks,
> Guo
> -----Original Message-----
> From: Sean Rhodes <sean@starlabs.systems>
> Sent: Monday, March 21, 2022 2:10 AM
> To: devel@edk2.groups.io
> Cc: Rhodes, Sean <sean@starlabs.systems>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Patrick Rudolph <patrick.rudolph@9elements.com>
> Subject: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
>
> This adds support for FVB in order to support a platform independent
> and non-volatile variable store on UefiPayloadPkg. The variable store
> makes use the SmmStoreLib to provide an unauthenticed variable store.
>
> Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
> the SMMSTORE v2 feature. It implements a SMI handler that is able to
> write, read and erase pages in the boot media (SPI flash).
> The communication is done using a fixed communication buffer that is
> allocated in CBMEM. The existence of this optional feature is advertised
> by a coreboot table.
> When the SMMSTORE feature is not available the variable emulation is used
> by setting PcdEmuVariableNvModeEnable to TRUE.
>
> The DXE component provides runtime services and takes care of virtual to
> physical mapping the communication buffers between SMM and OS.
>
> The contents of the variable store can be accessed and modified by any
> priviledged application. As authentication is done by runtime services
> only the store shouldn't be used to store authenticated variables.
>
> Tested on Linux and Windows 10 on real hardware.
> Currently this cannot be tested on coreboot and qemu as it doesn't support
> the SMMSTORE on qemu.
>
> Cc: Guo Dong <guo.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Maurice Ma <maurice.ma@intel.com>
> Cc: Benjamin You <benjamin.you@intel.com>
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> Signed-off-by: Sean Rhodes <sean@starlabs.systems>
> ---
>  .../SmmStoreFvb/SmmStoreFvbRuntime.c          | 282 ++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntime.h          | 111 +++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.c       | 849 ++++++++++++++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf     |  66 ++
>  UefiPayloadPkg/UefiPayloadPkg.dsc             |  24 +-
>  UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
>  6 files changed, 1330 insertions(+), 6 deletions(-)
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> new file mode 100644
> index 0000000000..08d5c408fb
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> @@ -0,0 +1,282 @@
> +/** @file  SmmStoreFvbRuntime.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/DevicePathLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +STATIC EFI_EVENT  mSmmStoreVirtualAddrChangeEvent;
>
> +
>
> +//
>
> +// Global variable declarations
>
> +//
>
> +SMMSTORE_INSTANCE  *mSmmStoreInstance;
>
> +
>
> +SMMSTORE_INSTANCE  mSmmStoreInstanceTemplate = {
>
> +  SMMSTORE_SIGNATURE, // Signature
>
> +  NULL,               // Handle ... NEED TO BE FILLED
>
> +  {
>
> +    FvbGetAttributes,      // GetAttributes
>
> +    FvbSetAttributes,      // SetAttributes
>
> +    FvbGetPhysicalAddress, // GetPhysicalAddress
>
> +    FvbGetBlockSize,       // GetBlockSize
>
> +    FvbRead,               // Read
>
> +    FvbWrite,              // Write
>
> +    FvbEraseBlocks,        // EraseBlocks
>
> +    NULL,                  // ParentHandle
>
> +  }, //  FvbProtoccol
>
> +  0, // BlockSize ... NEED TO BE FILLED
>
> +  0, // LastBlock ... NEED TO BE FILLED
>
> +  0, // MmioAddress ... NEED TO BE FILLED
>
> +  {
>
> +    {
>
> +      {
>
> +        HARDWARE_DEVICE_PATH,
>
> +        HW_MEMMAP_DP,
>
> +        {
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
>
> +        }
>
> +      },
>
> +      EfiMemoryMappedIO,
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +    },
>
> +    {
>
> +      END_DEVICE_PATH_TYPE,
>
> +      END_ENTIRE_DEVICE_PATH_SUBTYPE,
>
> +      {
>
> +        END_DEVICE_PATH_LENGTH,
>
> +        0
>
> +      }
>
> +    }
>
> +  } // DevicePath
>
> +};
>
> +
>
> +/**
>
> +  Initialize the SmmStore instance.
>
> +
>
> +
>
> +  @param[in]      FvBase         The physical MMIO base address of the FV containing
>
> +                                 the variable store.
>
> +
>
> +  @param[in]      NumberofBlocks Number of blocks within the FV.
>
> +  @param[in]      BlockSize      The size in bytes of one block within the FV.
>
> +  @param[in, out] Instance       The SmmStore instace to initialize
>
> +
>
> +**/
>
> +STATIC
>
> +EFI_STATUS
>
> +SmmStoreInitInstance (
>
> +  IN EFI_PHYSICAL_ADDRESS   FvBase,
>
> +  IN UINTN                  NumberofBlocks,
>
> +  IN UINTN                  BlockSize,
>
> +  IN OUT SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS             Status;
>
> +  FV_MEMMAP_DEVICE_PATH  *FvDevicePath;
>
> +
>
> +  ASSERT (Instance != NULL);
>
> +
>
> +  Instance->BlockSize   = BlockSize;
>
> +  Instance->LastBlock   = NumberofBlocks - 1;
>
> +  Instance->MmioAddress = FvBase;
>
> +
>
> +  FvDevicePath                                = &Instance->DevicePath;
>
> +  FvDevicePath->MemMapDevPath.StartingAddress = FvBase;
>
> +  FvDevicePath->MemMapDevPath.EndingAddress   = FvBase + BlockSize * NumberofBlocks - 1;
>
> +
>
> +  Status = FvbInitialize (Instance);
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = gBS->InstallMultipleProtocolInterfaces (
>
> +                  &Instance->Handle,
>
> +                  &gEfiDevicePathProtocolGuid,
>
> +                  &Instance->DevicePath,
>
> +                  &gEfiFirmwareVolumeBlockProtocolGuid,
>
> +                  &Instance->FvbProtocol,
>
> +                  NULL
>
> +                  );
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__));
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Fixup internal data so that EFI can be call in virtual mode.
>
> +  Call the passed in Child Notify event and convert any pointers in
>
> +  lib to virtual mode.
>
> +
>
> +  @param[in]    Event   The Event that is being processed
>
> +  @param[in]    Context Event Context
>
> +**/
>
> +STATIC
>
> +VOID
>
> +EFIAPI
>
> +SmmStoreVirtualNotifyEvent (
>
> +  IN EFI_EVENT  Event,
>
> +  IN VOID       *Context
>
> +  )
>
> +{
>
> +  // Convert Fvb
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.EraseBlocks);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetBlockSize);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetPhysicalAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Read);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.SetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Write);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->MmioAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance);
>
> +
>
> +  return;
>
> +}
>
> +
>
> +/**
>
> +  The user Entry Point for module SmmStoreFvbRuntimeDxe. The user code starts with this function.
>
> +
>
> +  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
>
> +  @param[in] SystemTable    A pointer to the EFI System Table.
>
> +
>
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
>
> +  @retval other             Some error occurs when executing this entry point.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +SmmStoreInitialize (
>
> +  IN EFI_HANDLE        ImageHandle,
>
> +  IN EFI_SYSTEM_TABLE  *SystemTable
>
> +  )
>
> +{
>
> +  EFI_STATUS            Status;
>
> +  EFI_PHYSICAL_ADDRESS  MmioAddress;
>
> +  UINTN                 BlockSize;
>
> +  UINTN                 BlockCount;
>
> +  UINT32                NvStorageBase;
>
> +  UINT32                NvStorageSize;
>
> +  UINT32                NvVariableSize;
>
> +  UINT32                FtwWorkingSize;
>
> +  UINT32                FtwSpareSize;
>
> +
>
> +  Status = SmmStoreLibInitialize ();
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to initialize SmmStoreLib\n", __FUNCTION__));
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetMmioAddress (&MmioAddress);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore MMIO address\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetNumBlocks (&BlockCount);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore No. blocks\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetBlockSize (&BlockSize);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore block size\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  NvStorageSize = BlockCount * BlockSize;
>
> +  NvStorageBase = MmioAddress;
>
> +
>
> +  FtwSpareSize   = (BlockCount / 2) * BlockSize;
>
> +  FtwWorkingSize = BlockSize;
>
> +  NvVariableSize = NvStorageSize - FtwSpareSize - FtwWorkingSize;
>
> +  DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase, NvStorageSize));
>
> +
>
> +  if (NvVariableSize >= 0x80000000) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableSize, NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableBase, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageVariableBase64, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  mSmmStoreInstance = AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSmmStoreInstanceTemplate);
>
> +  if (mSmmStoreInstance == NULL) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__));
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  Status = SmmStoreInitInstance (
>
> +             MmioAddress,
>
> +             BlockCount,
>
> +             BlockSize,
>
> +             mSmmStoreInstance
>
> +             );
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG (
>
> +      (
>
> +       DEBUG_ERROR,
>
> +       "%a: Fail to create instance for SmmStore\n",
>
> +       __FUNCTION__
>
> +      )
>
> +      );
>
> +    FreePool (mSmmStoreInstance);
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  //
>
> +  // Register for the virtual address change event
>
> +  //
>
> +  Status = gBS->CreateEventEx (
>
> +                  EVT_NOTIFY_SIGNAL,
>
> +                  TPL_NOTIFY,
>
> +                  SmmStoreVirtualNotifyEvent,
>
> +                  NULL,
>
> +                  &gEfiEventVirtualAddressChangeGuid,
>
> +                  &mSmmStoreVirtualAddrChangeEvent
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> new file mode 100644
> index 0000000000..8015d12d13
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> @@ -0,0 +1,111 @@
> +/** @file  SmmStoreFvbRuntime.h
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#ifndef SMM_STORE_DXE_H_
>
> +#define SMM_STORE_DXE_H_
>
> +
>
> +#include <Base.h>
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Guid/EventGroup.h>
>
> +
>
> +#include <Protocol/FirmwareVolumeBlock.h>
>
> +
>
> +#include <Library/DebugLib.h>
>
> +#include <Library/IoLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/UefiRuntimeLib.h>
>
> +
>
> +#define SMMSTORE_SIGNATURE  SIGNATURE_32('S', 'M', 'M', 'S')
>
> +#define INSTANCE_FROM_FVB_THIS(a)  CR(a, SMMSTORE_INSTANCE, FvbProtocol, SMMSTORE_SIGNATURE)
>
> +
>
> +typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE;
>
> +
>
> +typedef struct {
>
> +  MEMMAP_DEVICE_PATH          MemMapDevPath;
>
> +  EFI_DEVICE_PATH_PROTOCOL    EndDevPath;
>
> +} FV_MEMMAP_DEVICE_PATH;
>
> +
>
> +struct _SMMSTORE_INSTANCE {
>
> +  UINT32                                 Signature;
>
> +  EFI_HANDLE                             Handle;
>
> +  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL    FvbProtocol;
>
> +  UINTN                                  BlockSize;
>
> +  UINTN                                  LastBlock;
>
> +  EFI_PHYSICAL_ADDRESS                   MmioAddress;
>
> +  FV_MEMMAP_DEVICE_PATH                  DevicePath;
>
> +};
>
> +
>
> +//
>
> +// SmmStoreFvbRuntimeDxe.c
>
> +//
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  );
>
> +
>
> +#endif // SMM_STORE_DXE_H_
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> new file mode 100644
> index 0000000000..14d4ec324d
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> @@ -0,0 +1,849 @@
> +/** @file  SmmStoreFvbRuntimeDxe.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/BaseLib.h>
>
> +#include <Library/HobLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include <Guid/VariableFormat.h>
>
> +#include <Guid/SystemNvDataGuid.h>
>
> +#include <Guid/NvVarStoreFormatted.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +///
>
> +/// The Firmware Volume Block Protocol is the low-level interface
>
> +/// to a firmware volume. File-level access to a firmware volume
>
> +/// should not be done using the Firmware Volume Block Protocol.
>
> +/// Normal access to a firmware volume must use the Firmware
>
> +/// Volume Protocol. Typically, only the file system driver that
>
> +/// produces the Firmware Volume Protocol will bind to the
>
> +/// Firmware Volume Block Protocol.
>
> +///
>
> +
>
> +/**
>
> +  Initialises the FV Header and Variable Store Header
>
> +  to support variable operations.
>
> +
>
> +  @param[in]  Instance - Pointer to SmmStore instance
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +InitializeFvAndVariableStoreHeaders (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS                  Status;
>
> +  VOID                        *Headers;
>
> +  UINTN                       HeadersLength;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FirmwareVolumeHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINT32                      NvStorageFtwSpareSize;
>
> +  UINT32                      NvStorageFtwWorkingSize;
>
> +  UINT32                      NvStorageVariableSize;
>
> +  UINT64                      NvStorageFtwSpareBase;
>
> +  UINT64                      NvStorageFtwWorkingBase;
>
> +  UINT64                      NvStorageVariableBase;
>
> +
>
> +  HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);
>
> +  Headers       = AllocateZeroPool (HeadersLength);
>
> +
>
> +  NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
>
> +  NvStorageFtwSpareSize   = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  NvStorageVariableSize   = PcdGet32 (PcdFlashNvStorageVariableSize);
>
> +
>
> +  NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);
>
> +  NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?
>
> +                            PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
>
> +  NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
>
> +
>
> +  // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
>
> +  if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Check if the size of the area is at least one block size
>
> +  if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageVariableSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwWorkingSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwSpareSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Ensure the Variable area Base Addresses are aligned on a block size boundaries
>
> +  if ((NvStorageVariableBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwSpareBase % Instance->BlockSize != 0))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  //
>
> +  // EFI_FIRMWARE_VOLUME_HEADER
>
> +  //
>
> +  FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;
>
> +  CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
>
> +  FirmwareVolumeHeader->FvLength =
>
> +    PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  FirmwareVolumeHeader->Signature  = EFI_FVH_SIGNATURE;
>
> +  FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                                            EFI_FVB2_READ_ENABLED_CAP   | // Reads may be enabled
>
> +                                                            EFI_FVB2_READ_STATUS        | // Reads are currently enabled
>
> +                                                            EFI_FVB2_STICKY_WRITE       | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                                            EFI_FVB2_MEMORY_MAPPED      | // It is memory mapped
>
> +                                                            EFI_FVB2_ERASE_POLARITY     | // After erasure all bits take this value (i.e. '1')
>
> +                                                            EFI_FVB2_WRITE_STATUS       | // Writes are currently enabled
>
> +                                                            EFI_FVB2_WRITE_ENABLED_CAP    // Writes may be enabled
>
> +                                                            );
>
> +  FirmwareVolumeHeader->HeaderLength          = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);
>
> +  FirmwareVolumeHeader->Revision              = EFI_FVH_REVISION;
>
> +  FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;
>
> +  FirmwareVolumeHeader->BlockMap[0].Length    = Instance->BlockSize;
>
> +  FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
>
> +  FirmwareVolumeHeader->BlockMap[1].Length    = 0;
>
> +  FirmwareVolumeHeader->Checksum              = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
>
> +
>
> +  //
>
> +  // VARIABLE_STORE_HEADER
>
> +  //
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
>
> +  CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);
>
> +  VariableStoreHeader->Size   = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
>
> +  VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
>
> +  VariableStoreHeader->State  = VARIABLE_STORE_HEALTHY;
>
> +
>
> +  // Install the combined super-header in the NorFlash
>
> +  Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
>
> +
>
> +  FreePool (Headers);
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Check the integrity of firmware volume header.
>
> +
>
> +  @retval  EFI_SUCCESS   - The firmware volume is consistent
>
> +  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +ValidateFvHeader (
>
> +  VOID
>
> +  )
>
> +{
>
> +  UINT16                      Checksum;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINTN                       VariableStoreLength;
>
> +  UINTN                       FvLength;
>
> +  EFI_STATUS                  TempStatus;
>
> +  UINTN                       BufferSize;
>
> +  UINTN                       BufferSizeReqested;
>
> +
>
> +  BufferSizeReqested = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
>
> +  FwVolHeader        = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +             PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +
>
> +  //
>
> +  // Verify the header revision, header signature, length
>
> +  // Length of FvBlock cannot be 2**64-1
>
> +  // HeaderLength cannot be an odd number
>
> +  //
>
> +  if (  (FwVolHeader->Revision  != EFI_FVH_REVISION)
>
> +     || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
>
> +     || (FwVolHeader->FvLength  != FvLength)
>
> +        )
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: No Firmware Volume header present\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  // Check the Firmware Volume Guid
>
> +  if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Firmware Volume Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested = FwVolHeader->HeaderLength;
>
> +  FreePool (FwVolHeader);
>
> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Verify the header checksum
>
> +  Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
>
> +  if (Checksum != 0) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: FV checksum is invalid (Checksum:0x%X)\n",
>
> +      __FUNCTION__,
>
> +      Checksum
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested  = sizeof (VARIABLE_STORE_HEADER);
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!VariableStoreHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, FwVolHeader->HeaderLength, &BufferSize, (UINT8 *)VariableStoreHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (VariableStoreHeader);
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Check the Variable Store Guid
>
> +  if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
>
> +      !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
>
> +  if (VariableStoreHeader->Size != VariableStoreLength) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Length does not match\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  FreePool (FwVolHeader);
>
> +  FreePool (VariableStoreHeader);
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetAttributes() function retrieves the attributes and
>
> + current settings of the block.
>
> +
>
> + @param This         Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes   Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
>
> +                     current settings are returned.
>
> +                     Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS The firmware volume attributes were returned.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  EFI_FVB_ATTRIBUTES_2  FlashFvbAttributes;
>
> +
>
> +  FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                              EFI_FVB2_READ_STATUS      | // Reads are currently enabled
>
> +                                              EFI_FVB2_WRITE_STATUS     | // Writes are enabled
>
> +                                              EFI_FVB2_STICKY_WRITE     | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                              EFI_FVB2_MEMORY_MAPPED    | // It is memory mapped
>
> +                                              EFI_FVB2_ERASE_POLARITY     // After erasure all bits take this value (i.e. '1')
>
> +                                              );
>
> +
>
> +  *Attributes = FlashFvbAttributes;
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The SetAttributes() function sets configurable firmware volume attributes
>
> + and returns the new settings of the firmware volume.
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes               On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
>
> +                                 that contains the desired firmware volume settings.
>
> +                                 On successful return, it contains the new settings of
>
> +                                 the firmware volume.
>
> +                                 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume attributes were returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The attributes requested are in conflict with the capabilities
>
> +                                 as declared in the firmware volume header.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  DEBUG ((DEBUG_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
>
> +  return EFI_UNSUPPORTED;
>
> +}
>
> +
>
> +/**
>
> + The GetPhysicalAddress() function retrieves the base address of
>
> + a memory-mapped firmware volume. This function should be called
>
> + only for memory-mapped firmware volumes.
>
> +
>
> + @param This               Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Address            Pointer to a caller-allocated
>
> +                           EFI_PHYSICAL_ADDRESS that, on successful
>
> +                           return from GetPhysicalAddress(), contains the
>
> +                           base address of the firmware volume.
>
> +
>
> + @retval EFI_SUCCESS       The firmware volume base address was returned.
>
> +
>
> + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  )
>
> +{
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  ASSERT (Address != NULL);
>
> +  *Address = Instance->MmioAddress;
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetBlockSize() function retrieves the size of the requested
>
> + block. It also returns the number of additional blocks with
>
> + the identical size. The GetBlockSize() function is used to
>
> + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                      Indicates the block for which to return the size.
>
> +
>
> + @param BlockSize                Pointer to a caller-allocated UINTN in which
>
> +                                 the size of the block is returned.
>
> +
>
> + @param NumberOfBlocks           Pointer to a caller-allocated UINTN in
>
> +                                 which the number of consecutive blocks,
>
> +                                 starting with Lba, is returned. All
>
> +                                 blocks in this range have a size of
>
> +                                 BlockSize.
>
> +
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume base address was returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));
>
> +
>
> +  if (Lba > Instance->LastBlock) {
>
> +    DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    *BlockSize      = (UINTN)Instance->BlockSize;
>
> +    *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);
>
> +
>
> +    DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
>
> +
>
> +    Status = EFI_SUCCESS;
>
> +  }
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> + Reads the specified number of bytes into a buffer from the specified block.
>
> +
>
> + The Read() function reads the requested number of bytes from the
>
> + requested block and stores them in the provided buffer.
>
> + Implementations should be mindful that the firmware volume
>
> + might be in the ReadDisabled state. If it is in this state,
>
> + the Read() function must return the status code
>
> + EFI_ACCESS_DENIED without modifying the contents of the
>
> + buffer. The Read() function must also prevent spanning block
>
> + boundaries. If a read is requested that would span a block
>
> + boundary, the read must read up to the boundary but not
>
> + beyond. The output parameter NumBytes must be set to correctly
>
> + indicate the number of bytes actually read. The caller must be
>
> + aware that a read may be partially completed.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index from which to read.
>
> +
>
> + @param Offset               Offset into the block at which to begin reading.
>
> +
>
> + @param NumBytes             Pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes read.
>
> +
>
> + @param Buffer               Pointer to a caller-allocated buffer that will be used
>
> +                             to hold the data that is read.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was read successfully,  and contents are
>
> +                             in Buffer.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             returned in Buffer.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the ReadDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is not functioning correctly and could not be read.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibRead (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Writes the specified number of bytes from the input buffer to the block.
>
> +
>
> + The Write() function writes the specified number of bytes from
>
> + the provided buffer to the specified block and offset. If the
>
> + firmware volume is sticky write, the caller must ensure that
>
> + all the bits of the specified range to write are in the
>
> + EFI_FVB_ERASE_POLARITY state before calling the Write()
>
> + function, or else the result will be unpredictable. This
>
> + unpredictability arises because, for a sticky-write firmware
>
> + volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
>
> + state but cannot flip it back again.  Before calling the
>
> + Write() function,  it is recommended for the caller to first call
>
> + the EraseBlocks() function to erase the specified block to
>
> + write. A block erase cycle will transition bits from the
>
> + (NOT)EFI_FVB_ERASE_POLARITY state back to the
>
> + EFI_FVB_ERASE_POLARITY state. Implementations should be
>
> + mindful that the firmware volume might be in the WriteDisabled
>
> + state. If it is in this state, the Write() function must
>
> + return the status code EFI_ACCESS_DENIED without modifying the
>
> + contents of the firmware volume. The Write() function must
>
> + also prevent spanning block boundaries. If a write is
>
> + requested that spans a block boundary, the write must store up
>
> + to the boundary but not beyond. The output parameter NumBytes
>
> + must be set to correctly indicate the number of bytes actually
>
> + written. The caller must be aware that a write may be
>
> + partially completed. All writes, partial or otherwise, must be
>
> + fully flushed to the hardware before the Write() service
>
> + returns.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index to write to.
>
> +
>
> + @param Offset               Offset into the block at which to begin writing.
>
> +
>
> + @param NumBytes             The pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes actually written.
>
> +
>
> + @param Buffer               The pointer to a caller-allocated buffer that contains the source for the write.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was written successfully.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             actually written.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is malfunctioning and could not be written.
>
> +
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibWrite (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Erases and initialises a firmware volume block.
>
> +
>
> + The EraseBlocks() function erases one or more blocks as denoted
>
> + by the variable argument list. The entire parameter list of
>
> + blocks must be verified before erasing any blocks. If a block is
>
> + requested that does not exist within the associated firmware
>
> + volume (it has a larger index than the last block of the
>
> + firmware volume), the EraseBlocks() function must return the
>
> + status code EFI_INVALID_PARAMETER without modifying the contents
>
> + of the firmware volume. Implementations should be mindful that
>
> + the firmware volume might be in the WriteDisabled state. If it
>
> + is in this state, the EraseBlocks() function must return the
>
> + status code EFI_ACCESS_DENIED without modifying the contents of
>
> + the firmware volume. All calls to EraseBlocks() must be fully
>
> + flushed to the hardware before the EraseBlocks() service
>
> + returns.
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
>
> + instance.
>
> +
>
> + @param ...                      The variable argument list is a list of tuples.
>
> +                                 Each tuple describes a range of LBAs to erase
>
> +                                 and consists of the following:
>
> +                                 - An EFI_LBA that indicates the starting LBA
>
> +                                 - A UINTN that indicates the number of blocks to erase.
>
> +
>
> +                                 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
>
> +                                 For example, the following indicates that two ranges of blocks
>
> +                                 (5-7 and 10-11) are to be erased:
>
> +                                 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
>
> +
>
> + @retval EFI_SUCCESS             The erase request successfully completed.
>
> +
>
> + @retval EFI_ACCESS_DENIED       The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR        The block device is not functioning correctly and could not be written.
>
> +                                 The firmware device may have been partially erased.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   One or more of the LBAs listed in the variable argument list do
>
> +                                 not exist in the firmware volume.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  VA_LIST            Args;
>
> +  EFI_LBA            StartingLba; // Lba from which we start erasing
>
> +  UINTN              NumOfLba;    // Number of Lba blocks to erase
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  Status = EFI_SUCCESS;
>
> +
>
> +  // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
>
> +
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // All blocks must be within range
>
> +    DEBUG ((
>
> +      DEBUG_BLKIO,
>
> +      "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
>
> +      StartingLba,
>
> +      (UINT64)NumOfLba,
>
> +      Instance->LastBlock
>
> +      ));
>
> +    if ((NumOfLba == 0) || ((StartingLba + NumOfLba - 1) > Instance->LastBlock)) {
>
> +      VA_END (Args);
>
> +      DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
>
> +      Status = EFI_INVALID_PARAMETER;
>
> +      goto EXIT;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +  //
>
> +  // To get here, all must be ok, so start erasing
>
> +  //
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // Go through each one and erase it
>
> +    while (NumOfLba > 0) {
>
> +      // Erase it
>
> +      DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld\n", StartingLba));
>
> +      Status = SmmStoreLibEraseBlock (StartingLba);
>
> +      if (EFI_ERROR (Status)) {
>
> +        VA_END (Args);
>
> +        Status = EFI_DEVICE_ERROR;
>
> +        goto EXIT;
>
> +      }
>
> +
>
> +      // Move to the next Lba
>
> +      StartingLba++;
>
> +      NumOfLba--;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +EXIT:
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Initialized the Firmware Volume if necessary and installs the
>
> +  gEdkiiNvVarStoreFormattedGuid protocol.
>
> +
>
> +  @param Instance                    Pointer to SmmStore instance
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS     Status;
>
> +  UINT32         FvbNumLba;
>
> +  EFI_BOOT_MODE  BootMode;
>
> +
>
> +  ASSERT ((Instance != NULL));
>
> +
>
> +  BootMode = GetBootModeHob ();
>
> +  if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    // Determine if there is a valid header at the beginning of the NorFlash
>
> +    Status = ValidateFvHeader ();
>
> +  }
>
> +
>
> +  // Install the Default FVB header if required
>
> +  if (EFI_ERROR (Status)) {
>
> +    // There is no valid header, so time to install one.
>
> +    DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Installing a correct one for this volume.\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +
>
> +    // Erase all the NorFlash that is reserved for variable storage
>
> +    FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
>
> +
>
> +    Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +
>
> +    // Install all appropriate headers
>
> +    Status = InitializeFvAndVariableStoreHeaders (Instance);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +  } else {
>
> +    DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__));
>
> +  }
>
> +
>
> +  //
>
> +  // The driver implementing the variable read service can now be dispatched;
>
> +  // the varstore headers are in place.
>
> +  //
>
> +  Status = gBS->InstallProtocolInterface (
>
> +                  &gImageHandle,
>
> +                  &gEdkiiNvVarStoreFormattedGuid,
>
> +                  EFI_NATIVE_INTERFACE,
>
> +                  NULL
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> new file mode 100644
> index 0000000000..2bd8886bcc
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> @@ -0,0 +1,66 @@
> +## @file
>
> +#  Component description file for SmmStore module
>
> +#
>
> +#  This module produces the FirmwareVolumeBlock protocol on top of the
>
> +#  SmmStore provided by bootloader and installs the NvVarStoreFormatted protocol.
>
> +#
>
> +#  Copyright (c) 2022, 9elements GmbH<BR>
>
> +#
>
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +#
>
> +##
>
> +
>
> +[Defines]
>
> +  INF_VERSION                    = 0x00010005
>
> +  BASE_NAME                      = SmmStoreFvbRuntimeDxe
>
> +  FILE_GUID                      = A0402FCA-6B25-4CEA-B7DD-C08F99714B29
>
> +  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
>
> +  VERSION_STRING                 = 1.0
>
> +  ENTRY_POINT                    = SmmStoreInitialize
>
> +
>
> +[Sources.common]
>
> +  SmmStoreFvbRuntimeDxe.c
>
> +  SmmStoreFvbRuntime.h
>
> +  SmmStoreFvbRuntime.c
>
> +
>
> +[Packages]
>
> +  MdePkg/MdePkg.dec
>
> +  MdeModulePkg/MdeModulePkg.dec
>
> +  EmbeddedPkg/EmbeddedPkg.dec
>
> +  UefiPayloadPkg/UefiPayloadPkg.dec
>
> +
>
> +[LibraryClasses]
>
> +  BaseLib
>
> +  DebugLib
>
> +  HobLib
>
> +  SmmStoreLib
>
> +  UefiLib
>
> +  UefiDriverEntryPoint
>
> +  UefiBootServicesTableLib
>
> +  UefiRuntimeLib
>
> +  DxeServicesTableLib
>
> +
>
> +[Guids]
>
> +  gEfiSystemNvDataFvGuid
>
> +  gEfiVariableGuid                  ## PRODUCES ## PROTOCOL
>
> +  gEfiAuthenticatedVariableGuid
>
> +  gEfiEventVirtualAddressChangeGuid
>
> +  gEdkiiNvVarStoreFormattedGuid     ## PRODUCES ## PROTOCOL
>
> +
>
> +[Protocols]
>
> +  gEfiDevicePathProtocolGuid          ## BY_START
>
> +  gEfiFirmwareVolumeBlockProtocolGuid ## BY_START
>
> +
>
> +[Pcd]
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
>
> +
>
> +[Depex]
>
> +  TRUE
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
> index cf440fee58..0f6421612c 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.dsc
> +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
> @@ -97,6 +97,7 @@
>    #
>
>    # EMU:      UEFI payload with EMU variable
>
>    # SPI:      UEFI payload with SPI NV variable support
>
> +  # SMMSTORE: UEFI payload with coreboot SMM NV variable support
>
>    # NONE:     UEFI payload with no variable modules
>
>    #
>
>    DEFINE VARIABLE_SUPPORT      = EMU
>
> @@ -261,9 +262,11 @@
>    LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
>
>    FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf
>
>    AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf
>
> -  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
> +  TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf
>
>    TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
>
> @@ -379,15 +382,13 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE
>
>
>
>  [PcdsFixedAtBuild]
>
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |TRUE
>
> -!else
>
> +!elseif $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |FALSE
>
>  !endif
>
> -
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE
>
> @@ -445,7 +446,6 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|$(MAX_LOGICAL_PROCESSORS)
>
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0
>
>    gUefiPayloadPkgTokenSpaceGuid.PcdBootloaderParameter|0
>
> -
>
>  ################################################################################
>
>  #
>
>  # Pcd DynamicEx Section - list of all EDK II PCD Entries defined by this Platform
>
> @@ -473,15 +473,19 @@
>  !endif
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0
>
>    gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3
>
> -!if $(VARIABLE_SUPPORT) == "SPI"
>
> +!if $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase  |0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0
>
>  !endif
>
>    # Disable SMM S3 script
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE
>
> @@ -697,6 +701,14 @@
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
>
> +    <LibraryClasses>
>
> +      NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
>
> +      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
>
> +  }
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf {
>
>      <LibraryClasses>
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
> index c7b04978ad..ca44f48f6d 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.fdf
> +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
> @@ -140,6 +140,10 @@ INF PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  INF UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    INF UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
>
>    INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
>
> --
> 2.32.0
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-23 18:24     ` Patrick Rudolph
@ 2022-03-23 19:27       ` Guo Dong
  2022-03-23 19:52         ` Sean Rhodes
  0 siblings, 1 reply; 14+ messages in thread
From: Guo Dong @ 2022-03-23 19:27 UTC (permalink / raw)
  To: devel@edk2.groups.io, patrick.rudolph@9elements.com
  Cc: Rhodes, Sean, Ni, Ray, Ma, Maurice, You, Benjamin


Hi Rudolph,

By my understanding, edk2 repo is mainly for the modules defined by industry standards (e.g. UEFI and PI specifications).
For the modules that is specific to a vendor (e.g. Intel PCH), or a bootloader, edk2-platform would be a better places.

Currently in the EDK2 UefiPayloadpkg, beside the PCH modules I mentioned before, only ParstLib is bootloader specific.
So for PCH modules, I already submitted a patch to move to edk2-platforms repo.
For ParseLib, it is not used in universal UEFI payload. Will keep it as-is to be used for non-universal UEFI payload for back-combatable. 
Hope this could help.

Thanks,
Guo

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Patrick Rudolph
Sent: Wednesday, March 23, 2022 11:25 AM
To: Dong, Guo <guo.dong@intel.com>
Cc: Rhodes, Sean <sean@starlabs.systems>; devel@edk2.groups.io; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>
Subject: Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol

Hi Guo,
I don't see where it's written that UefiPayloadPkg is bootloader
independent. Can you point me to that documentation?
Reading the BuildAndIntegrationInstructions.txt I understand that it
is very bootloader specific, as it's  basically designed as a
bootloader payload.
Where do you draw the line between platform code and "generic" payload code?

Regards,
Patrick Rudolph

On Wed, Mar 23, 2022 at 7:12 PM Dong, Guo <guo.dong@intel.com> wrote:
>
>
> Hi Sean,
> SMMSTORE and this FVB module are specific for coreboot while UefiPayloadPkg in Edk2 is targeting platform/bootloader independent.
> I just submitted an "add UEFI payload platform FV" patch to edk2-platforms repo, in this patch it moves PCH specific modules from
> Edk2 UefiPayloadPkg to edk2-platforms Platform/Intel/PayloadPlatformPkg. Maybe these coreboot specific modules could also target
> to edk2-platforms repo?
>
> Thanks,
> Guo
> -----Original Message-----
> From: Sean Rhodes <sean@starlabs.systems>
> Sent: Monday, March 21, 2022 2:10 AM
> To: devel@edk2.groups.io
> Cc: Rhodes, Sean <sean@starlabs.systems>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Patrick Rudolph <patrick.rudolph@9elements.com>
> Subject: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
>
> This adds support for FVB in order to support a platform independent
> and non-volatile variable store on UefiPayloadPkg. The variable store
> makes use the SmmStoreLib to provide an unauthenticed variable store.
>
> Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
> the SMMSTORE v2 feature. It implements a SMI handler that is able to
> write, read and erase pages in the boot media (SPI flash).
> The communication is done using a fixed communication buffer that is
> allocated in CBMEM. The existence of this optional feature is advertised
> by a coreboot table.
> When the SMMSTORE feature is not available the variable emulation is used
> by setting PcdEmuVariableNvModeEnable to TRUE.
>
> The DXE component provides runtime services and takes care of virtual to
> physical mapping the communication buffers between SMM and OS.
>
> The contents of the variable store can be accessed and modified by any
> priviledged application. As authentication is done by runtime services
> only the store shouldn't be used to store authenticated variables.
>
> Tested on Linux and Windows 10 on real hardware.
> Currently this cannot be tested on coreboot and qemu as it doesn't support
> the SMMSTORE on qemu.
>
> Cc: Guo Dong <guo.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Maurice Ma <maurice.ma@intel.com>
> Cc: Benjamin You <benjamin.you@intel.com>
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> Signed-off-by: Sean Rhodes <sean@starlabs.systems>
> ---
>  .../SmmStoreFvb/SmmStoreFvbRuntime.c          | 282 ++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntime.h          | 111 +++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.c       | 849 ++++++++++++++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf     |  66 ++
>  UefiPayloadPkg/UefiPayloadPkg.dsc             |  24 +-
>  UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
>  6 files changed, 1330 insertions(+), 6 deletions(-)
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> new file mode 100644
> index 0000000000..08d5c408fb
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> @@ -0,0 +1,282 @@
> +/** @file  SmmStoreFvbRuntime.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/DevicePathLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +STATIC EFI_EVENT  mSmmStoreVirtualAddrChangeEvent;
>
> +
>
> +//
>
> +// Global variable declarations
>
> +//
>
> +SMMSTORE_INSTANCE  *mSmmStoreInstance;
>
> +
>
> +SMMSTORE_INSTANCE  mSmmStoreInstanceTemplate = {
>
> +  SMMSTORE_SIGNATURE, // Signature
>
> +  NULL,               // Handle ... NEED TO BE FILLED
>
> +  {
>
> +    FvbGetAttributes,      // GetAttributes
>
> +    FvbSetAttributes,      // SetAttributes
>
> +    FvbGetPhysicalAddress, // GetPhysicalAddress
>
> +    FvbGetBlockSize,       // GetBlockSize
>
> +    FvbRead,               // Read
>
> +    FvbWrite,              // Write
>
> +    FvbEraseBlocks,        // EraseBlocks
>
> +    NULL,                  // ParentHandle
>
> +  }, //  FvbProtoccol
>
> +  0, // BlockSize ... NEED TO BE FILLED
>
> +  0, // LastBlock ... NEED TO BE FILLED
>
> +  0, // MmioAddress ... NEED TO BE FILLED
>
> +  {
>
> +    {
>
> +      {
>
> +        HARDWARE_DEVICE_PATH,
>
> +        HW_MEMMAP_DP,
>
> +        {
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
>
> +        }
>
> +      },
>
> +      EfiMemoryMappedIO,
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +    },
>
> +    {
>
> +      END_DEVICE_PATH_TYPE,
>
> +      END_ENTIRE_DEVICE_PATH_SUBTYPE,
>
> +      {
>
> +        END_DEVICE_PATH_LENGTH,
>
> +        0
>
> +      }
>
> +    }
>
> +  } // DevicePath
>
> +};
>
> +
>
> +/**
>
> +  Initialize the SmmStore instance.
>
> +
>
> +
>
> +  @param[in]      FvBase         The physical MMIO base address of the FV containing
>
> +                                 the variable store.
>
> +
>
> +  @param[in]      NumberofBlocks Number of blocks within the FV.
>
> +  @param[in]      BlockSize      The size in bytes of one block within the FV.
>
> +  @param[in, out] Instance       The SmmStore instace to initialize
>
> +
>
> +**/
>
> +STATIC
>
> +EFI_STATUS
>
> +SmmStoreInitInstance (
>
> +  IN EFI_PHYSICAL_ADDRESS   FvBase,
>
> +  IN UINTN                  NumberofBlocks,
>
> +  IN UINTN                  BlockSize,
>
> +  IN OUT SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS             Status;
>
> +  FV_MEMMAP_DEVICE_PATH  *FvDevicePath;
>
> +
>
> +  ASSERT (Instance != NULL);
>
> +
>
> +  Instance->BlockSize   = BlockSize;
>
> +  Instance->LastBlock   = NumberofBlocks - 1;
>
> +  Instance->MmioAddress = FvBase;
>
> +
>
> +  FvDevicePath                                = &Instance->DevicePath;
>
> +  FvDevicePath->MemMapDevPath.StartingAddress = FvBase;
>
> +  FvDevicePath->MemMapDevPath.EndingAddress   = FvBase + BlockSize * NumberofBlocks - 1;
>
> +
>
> +  Status = FvbInitialize (Instance);
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = gBS->InstallMultipleProtocolInterfaces (
>
> +                  &Instance->Handle,
>
> +                  &gEfiDevicePathProtocolGuid,
>
> +                  &Instance->DevicePath,
>
> +                  &gEfiFirmwareVolumeBlockProtocolGuid,
>
> +                  &Instance->FvbProtocol,
>
> +                  NULL
>
> +                  );
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__));
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Fixup internal data so that EFI can be call in virtual mode.
>
> +  Call the passed in Child Notify event and convert any pointers in
>
> +  lib to virtual mode.
>
> +
>
> +  @param[in]    Event   The Event that is being processed
>
> +  @param[in]    Context Event Context
>
> +**/
>
> +STATIC
>
> +VOID
>
> +EFIAPI
>
> +SmmStoreVirtualNotifyEvent (
>
> +  IN EFI_EVENT  Event,
>
> +  IN VOID       *Context
>
> +  )
>
> +{
>
> +  // Convert Fvb
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.EraseBlocks);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetBlockSize);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetPhysicalAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Read);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.SetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Write);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->MmioAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance);
>
> +
>
> +  return;
>
> +}
>
> +
>
> +/**
>
> +  The user Entry Point for module SmmStoreFvbRuntimeDxe. The user code starts with this function.
>
> +
>
> +  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
>
> +  @param[in] SystemTable    A pointer to the EFI System Table.
>
> +
>
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
>
> +  @retval other             Some error occurs when executing this entry point.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +SmmStoreInitialize (
>
> +  IN EFI_HANDLE        ImageHandle,
>
> +  IN EFI_SYSTEM_TABLE  *SystemTable
>
> +  )
>
> +{
>
> +  EFI_STATUS            Status;
>
> +  EFI_PHYSICAL_ADDRESS  MmioAddress;
>
> +  UINTN                 BlockSize;
>
> +  UINTN                 BlockCount;
>
> +  UINT32                NvStorageBase;
>
> +  UINT32                NvStorageSize;
>
> +  UINT32                NvVariableSize;
>
> +  UINT32                FtwWorkingSize;
>
> +  UINT32                FtwSpareSize;
>
> +
>
> +  Status = SmmStoreLibInitialize ();
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to initialize SmmStoreLib\n", __FUNCTION__));
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetMmioAddress (&MmioAddress);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore MMIO address\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetNumBlocks (&BlockCount);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore No. blocks\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetBlockSize (&BlockSize);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore block size\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  NvStorageSize = BlockCount * BlockSize;
>
> +  NvStorageBase = MmioAddress;
>
> +
>
> +  FtwSpareSize   = (BlockCount / 2) * BlockSize;
>
> +  FtwWorkingSize = BlockSize;
>
> +  NvVariableSize = NvStorageSize - FtwSpareSize - FtwWorkingSize;
>
> +  DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase, NvStorageSize));
>
> +
>
> +  if (NvVariableSize >= 0x80000000) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableSize, NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableBase, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageVariableBase64, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  mSmmStoreInstance = AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSmmStoreInstanceTemplate);
>
> +  if (mSmmStoreInstance == NULL) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__));
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  Status = SmmStoreInitInstance (
>
> +             MmioAddress,
>
> +             BlockCount,
>
> +             BlockSize,
>
> +             mSmmStoreInstance
>
> +             );
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG (
>
> +      (
>
> +       DEBUG_ERROR,
>
> +       "%a: Fail to create instance for SmmStore\n",
>
> +       __FUNCTION__
>
> +      )
>
> +      );
>
> +    FreePool (mSmmStoreInstance);
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  //
>
> +  // Register for the virtual address change event
>
> +  //
>
> +  Status = gBS->CreateEventEx (
>
> +                  EVT_NOTIFY_SIGNAL,
>
> +                  TPL_NOTIFY,
>
> +                  SmmStoreVirtualNotifyEvent,
>
> +                  NULL,
>
> +                  &gEfiEventVirtualAddressChangeGuid,
>
> +                  &mSmmStoreVirtualAddrChangeEvent
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> new file mode 100644
> index 0000000000..8015d12d13
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> @@ -0,0 +1,111 @@
> +/** @file  SmmStoreFvbRuntime.h
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#ifndef SMM_STORE_DXE_H_
>
> +#define SMM_STORE_DXE_H_
>
> +
>
> +#include <Base.h>
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Guid/EventGroup.h>
>
> +
>
> +#include <Protocol/FirmwareVolumeBlock.h>
>
> +
>
> +#include <Library/DebugLib.h>
>
> +#include <Library/IoLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/UefiRuntimeLib.h>
>
> +
>
> +#define SMMSTORE_SIGNATURE  SIGNATURE_32('S', 'M', 'M', 'S')
>
> +#define INSTANCE_FROM_FVB_THIS(a)  CR(a, SMMSTORE_INSTANCE, FvbProtocol, SMMSTORE_SIGNATURE)
>
> +
>
> +typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE;
>
> +
>
> +typedef struct {
>
> +  MEMMAP_DEVICE_PATH          MemMapDevPath;
>
> +  EFI_DEVICE_PATH_PROTOCOL    EndDevPath;
>
> +} FV_MEMMAP_DEVICE_PATH;
>
> +
>
> +struct _SMMSTORE_INSTANCE {
>
> +  UINT32                                 Signature;
>
> +  EFI_HANDLE                             Handle;
>
> +  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL    FvbProtocol;
>
> +  UINTN                                  BlockSize;
>
> +  UINTN                                  LastBlock;
>
> +  EFI_PHYSICAL_ADDRESS                   MmioAddress;
>
> +  FV_MEMMAP_DEVICE_PATH                  DevicePath;
>
> +};
>
> +
>
> +//
>
> +// SmmStoreFvbRuntimeDxe.c
>
> +//
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  );
>
> +
>
> +#endif // SMM_STORE_DXE_H_
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> new file mode 100644
> index 0000000000..14d4ec324d
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> @@ -0,0 +1,849 @@
> +/** @file  SmmStoreFvbRuntimeDxe.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/BaseLib.h>
>
> +#include <Library/HobLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include <Guid/VariableFormat.h>
>
> +#include <Guid/SystemNvDataGuid.h>
>
> +#include <Guid/NvVarStoreFormatted.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +///
>
> +/// The Firmware Volume Block Protocol is the low-level interface
>
> +/// to a firmware volume. File-level access to a firmware volume
>
> +/// should not be done using the Firmware Volume Block Protocol.
>
> +/// Normal access to a firmware volume must use the Firmware
>
> +/// Volume Protocol. Typically, only the file system driver that
>
> +/// produces the Firmware Volume Protocol will bind to the
>
> +/// Firmware Volume Block Protocol.
>
> +///
>
> +
>
> +/**
>
> +  Initialises the FV Header and Variable Store Header
>
> +  to support variable operations.
>
> +
>
> +  @param[in]  Instance - Pointer to SmmStore instance
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +InitializeFvAndVariableStoreHeaders (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS                  Status;
>
> +  VOID                        *Headers;
>
> +  UINTN                       HeadersLength;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FirmwareVolumeHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINT32                      NvStorageFtwSpareSize;
>
> +  UINT32                      NvStorageFtwWorkingSize;
>
> +  UINT32                      NvStorageVariableSize;
>
> +  UINT64                      NvStorageFtwSpareBase;
>
> +  UINT64                      NvStorageFtwWorkingBase;
>
> +  UINT64                      NvStorageVariableBase;
>
> +
>
> +  HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);
>
> +  Headers       = AllocateZeroPool (HeadersLength);
>
> +
>
> +  NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
>
> +  NvStorageFtwSpareSize   = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  NvStorageVariableSize   = PcdGet32 (PcdFlashNvStorageVariableSize);
>
> +
>
> +  NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);
>
> +  NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?
>
> +                            PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
>
> +  NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
>
> +
>
> +  // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
>
> +  if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Check if the size of the area is at least one block size
>
> +  if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageVariableSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwWorkingSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwSpareSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Ensure the Variable area Base Addresses are aligned on a block size boundaries
>
> +  if ((NvStorageVariableBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwSpareBase % Instance->BlockSize != 0))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  //
>
> +  // EFI_FIRMWARE_VOLUME_HEADER
>
> +  //
>
> +  FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;
>
> +  CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
>
> +  FirmwareVolumeHeader->FvLength =
>
> +    PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  FirmwareVolumeHeader->Signature  = EFI_FVH_SIGNATURE;
>
> +  FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                                            EFI_FVB2_READ_ENABLED_CAP   | // Reads may be enabled
>
> +                                                            EFI_FVB2_READ_STATUS        | // Reads are currently enabled
>
> +                                                            EFI_FVB2_STICKY_WRITE       | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                                            EFI_FVB2_MEMORY_MAPPED      | // It is memory mapped
>
> +                                                            EFI_FVB2_ERASE_POLARITY     | // After erasure all bits take this value (i.e. '1')
>
> +                                                            EFI_FVB2_WRITE_STATUS       | // Writes are currently enabled
>
> +                                                            EFI_FVB2_WRITE_ENABLED_CAP    // Writes may be enabled
>
> +                                                            );
>
> +  FirmwareVolumeHeader->HeaderLength          = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);
>
> +  FirmwareVolumeHeader->Revision              = EFI_FVH_REVISION;
>
> +  FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;
>
> +  FirmwareVolumeHeader->BlockMap[0].Length    = Instance->BlockSize;
>
> +  FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
>
> +  FirmwareVolumeHeader->BlockMap[1].Length    = 0;
>
> +  FirmwareVolumeHeader->Checksum              = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
>
> +
>
> +  //
>
> +  // VARIABLE_STORE_HEADER
>
> +  //
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
>
> +  CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);
>
> +  VariableStoreHeader->Size   = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
>
> +  VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
>
> +  VariableStoreHeader->State  = VARIABLE_STORE_HEALTHY;
>
> +
>
> +  // Install the combined super-header in the NorFlash
>
> +  Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
>
> +
>
> +  FreePool (Headers);
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Check the integrity of firmware volume header.
>
> +
>
> +  @retval  EFI_SUCCESS   - The firmware volume is consistent
>
> +  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +ValidateFvHeader (
>
> +  VOID
>
> +  )
>
> +{
>
> +  UINT16                      Checksum;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINTN                       VariableStoreLength;
>
> +  UINTN                       FvLength;
>
> +  EFI_STATUS                  TempStatus;
>
> +  UINTN                       BufferSize;
>
> +  UINTN                       BufferSizeReqested;
>
> +
>
> +  BufferSizeReqested = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
>
> +  FwVolHeader        = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +             PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +
>
> +  //
>
> +  // Verify the header revision, header signature, length
>
> +  // Length of FvBlock cannot be 2**64-1
>
> +  // HeaderLength cannot be an odd number
>
> +  //
>
> +  if (  (FwVolHeader->Revision  != EFI_FVH_REVISION)
>
> +     || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
>
> +     || (FwVolHeader->FvLength  != FvLength)
>
> +        )
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: No Firmware Volume header present\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  // Check the Firmware Volume Guid
>
> +  if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Firmware Volume Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested = FwVolHeader->HeaderLength;
>
> +  FreePool (FwVolHeader);
>
> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Verify the header checksum
>
> +  Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
>
> +  if (Checksum != 0) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: FV checksum is invalid (Checksum:0x%X)\n",
>
> +      __FUNCTION__,
>
> +      Checksum
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested  = sizeof (VARIABLE_STORE_HEADER);
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!VariableStoreHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, FwVolHeader->HeaderLength, &BufferSize, (UINT8 *)VariableStoreHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (VariableStoreHeader);
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Check the Variable Store Guid
>
> +  if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
>
> +      !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
>
> +  if (VariableStoreHeader->Size != VariableStoreLength) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Length does not match\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  FreePool (FwVolHeader);
>
> +  FreePool (VariableStoreHeader);
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetAttributes() function retrieves the attributes and
>
> + current settings of the block.
>
> +
>
> + @param This         Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes   Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
>
> +                     current settings are returned.
>
> +                     Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS The firmware volume attributes were returned.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  EFI_FVB_ATTRIBUTES_2  FlashFvbAttributes;
>
> +
>
> +  FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                              EFI_FVB2_READ_STATUS      | // Reads are currently enabled
>
> +                                              EFI_FVB2_WRITE_STATUS     | // Writes are enabled
>
> +                                              EFI_FVB2_STICKY_WRITE     | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                              EFI_FVB2_MEMORY_MAPPED    | // It is memory mapped
>
> +                                              EFI_FVB2_ERASE_POLARITY     // After erasure all bits take this value (i.e. '1')
>
> +                                              );
>
> +
>
> +  *Attributes = FlashFvbAttributes;
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The SetAttributes() function sets configurable firmware volume attributes
>
> + and returns the new settings of the firmware volume.
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes               On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
>
> +                                 that contains the desired firmware volume settings.
>
> +                                 On successful return, it contains the new settings of
>
> +                                 the firmware volume.
>
> +                                 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume attributes were returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The attributes requested are in conflict with the capabilities
>
> +                                 as declared in the firmware volume header.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  DEBUG ((DEBUG_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
>
> +  return EFI_UNSUPPORTED;
>
> +}
>
> +
>
> +/**
>
> + The GetPhysicalAddress() function retrieves the base address of
>
> + a memory-mapped firmware volume. This function should be called
>
> + only for memory-mapped firmware volumes.
>
> +
>
> + @param This               Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Address            Pointer to a caller-allocated
>
> +                           EFI_PHYSICAL_ADDRESS that, on successful
>
> +                           return from GetPhysicalAddress(), contains the
>
> +                           base address of the firmware volume.
>
> +
>
> + @retval EFI_SUCCESS       The firmware volume base address was returned.
>
> +
>
> + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  )
>
> +{
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  ASSERT (Address != NULL);
>
> +  *Address = Instance->MmioAddress;
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetBlockSize() function retrieves the size of the requested
>
> + block. It also returns the number of additional blocks with
>
> + the identical size. The GetBlockSize() function is used to
>
> + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                      Indicates the block for which to return the size.
>
> +
>
> + @param BlockSize                Pointer to a caller-allocated UINTN in which
>
> +                                 the size of the block is returned.
>
> +
>
> + @param NumberOfBlocks           Pointer to a caller-allocated UINTN in
>
> +                                 which the number of consecutive blocks,
>
> +                                 starting with Lba, is returned. All
>
> +                                 blocks in this range have a size of
>
> +                                 BlockSize.
>
> +
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume base address was returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));
>
> +
>
> +  if (Lba > Instance->LastBlock) {
>
> +    DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    *BlockSize      = (UINTN)Instance->BlockSize;
>
> +    *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);
>
> +
>
> +    DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
>
> +
>
> +    Status = EFI_SUCCESS;
>
> +  }
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> + Reads the specified number of bytes into a buffer from the specified block.
>
> +
>
> + The Read() function reads the requested number of bytes from the
>
> + requested block and stores them in the provided buffer.
>
> + Implementations should be mindful that the firmware volume
>
> + might be in the ReadDisabled state. If it is in this state,
>
> + the Read() function must return the status code
>
> + EFI_ACCESS_DENIED without modifying the contents of the
>
> + buffer. The Read() function must also prevent spanning block
>
> + boundaries. If a read is requested that would span a block
>
> + boundary, the read must read up to the boundary but not
>
> + beyond. The output parameter NumBytes must be set to correctly
>
> + indicate the number of bytes actually read. The caller must be
>
> + aware that a read may be partially completed.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index from which to read.
>
> +
>
> + @param Offset               Offset into the block at which to begin reading.
>
> +
>
> + @param NumBytes             Pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes read.
>
> +
>
> + @param Buffer               Pointer to a caller-allocated buffer that will be used
>
> +                             to hold the data that is read.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was read successfully,  and contents are
>
> +                             in Buffer.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             returned in Buffer.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the ReadDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is not functioning correctly and could not be read.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibRead (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Writes the specified number of bytes from the input buffer to the block.
>
> +
>
> + The Write() function writes the specified number of bytes from
>
> + the provided buffer to the specified block and offset. If the
>
> + firmware volume is sticky write, the caller must ensure that
>
> + all the bits of the specified range to write are in the
>
> + EFI_FVB_ERASE_POLARITY state before calling the Write()
>
> + function, or else the result will be unpredictable. This
>
> + unpredictability arises because, for a sticky-write firmware
>
> + volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
>
> + state but cannot flip it back again.  Before calling the
>
> + Write() function,  it is recommended for the caller to first call
>
> + the EraseBlocks() function to erase the specified block to
>
> + write. A block erase cycle will transition bits from the
>
> + (NOT)EFI_FVB_ERASE_POLARITY state back to the
>
> + EFI_FVB_ERASE_POLARITY state. Implementations should be
>
> + mindful that the firmware volume might be in the WriteDisabled
>
> + state. If it is in this state, the Write() function must
>
> + return the status code EFI_ACCESS_DENIED without modifying the
>
> + contents of the firmware volume. The Write() function must
>
> + also prevent spanning block boundaries. If a write is
>
> + requested that spans a block boundary, the write must store up
>
> + to the boundary but not beyond. The output parameter NumBytes
>
> + must be set to correctly indicate the number of bytes actually
>
> + written. The caller must be aware that a write may be
>
> + partially completed. All writes, partial or otherwise, must be
>
> + fully flushed to the hardware before the Write() service
>
> + returns.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index to write to.
>
> +
>
> + @param Offset               Offset into the block at which to begin writing.
>
> +
>
> + @param NumBytes             The pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes actually written.
>
> +
>
> + @param Buffer               The pointer to a caller-allocated buffer that contains the source for the write.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was written successfully.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             actually written.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is malfunctioning and could not be written.
>
> +
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibWrite (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Erases and initialises a firmware volume block.
>
> +
>
> + The EraseBlocks() function erases one or more blocks as denoted
>
> + by the variable argument list. The entire parameter list of
>
> + blocks must be verified before erasing any blocks. If a block is
>
> + requested that does not exist within the associated firmware
>
> + volume (it has a larger index than the last block of the
>
> + firmware volume), the EraseBlocks() function must return the
>
> + status code EFI_INVALID_PARAMETER without modifying the contents
>
> + of the firmware volume. Implementations should be mindful that
>
> + the firmware volume might be in the WriteDisabled state. If it
>
> + is in this state, the EraseBlocks() function must return the
>
> + status code EFI_ACCESS_DENIED without modifying the contents of
>
> + the firmware volume. All calls to EraseBlocks() must be fully
>
> + flushed to the hardware before the EraseBlocks() service
>
> + returns.
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
>
> + instance.
>
> +
>
> + @param ...                      The variable argument list is a list of tuples.
>
> +                                 Each tuple describes a range of LBAs to erase
>
> +                                 and consists of the following:
>
> +                                 - An EFI_LBA that indicates the starting LBA
>
> +                                 - A UINTN that indicates the number of blocks to erase.
>
> +
>
> +                                 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
>
> +                                 For example, the following indicates that two ranges of blocks
>
> +                                 (5-7 and 10-11) are to be erased:
>
> +                                 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
>
> +
>
> + @retval EFI_SUCCESS             The erase request successfully completed.
>
> +
>
> + @retval EFI_ACCESS_DENIED       The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR        The block device is not functioning correctly and could not be written.
>
> +                                 The firmware device may have been partially erased.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   One or more of the LBAs listed in the variable argument list do
>
> +                                 not exist in the firmware volume.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  VA_LIST            Args;
>
> +  EFI_LBA            StartingLba; // Lba from which we start erasing
>
> +  UINTN              NumOfLba;    // Number of Lba blocks to erase
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  Status = EFI_SUCCESS;
>
> +
>
> +  // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
>
> +
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // All blocks must be within range
>
> +    DEBUG ((
>
> +      DEBUG_BLKIO,
>
> +      "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
>
> +      StartingLba,
>
> +      (UINT64)NumOfLba,
>
> +      Instance->LastBlock
>
> +      ));
>
> +    if ((NumOfLba == 0) || ((StartingLba + NumOfLba - 1) > Instance->LastBlock)) {
>
> +      VA_END (Args);
>
> +      DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
>
> +      Status = EFI_INVALID_PARAMETER;
>
> +      goto EXIT;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +  //
>
> +  // To get here, all must be ok, so start erasing
>
> +  //
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // Go through each one and erase it
>
> +    while (NumOfLba > 0) {
>
> +      // Erase it
>
> +      DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld\n", StartingLba));
>
> +      Status = SmmStoreLibEraseBlock (StartingLba);
>
> +      if (EFI_ERROR (Status)) {
>
> +        VA_END (Args);
>
> +        Status = EFI_DEVICE_ERROR;
>
> +        goto EXIT;
>
> +      }
>
> +
>
> +      // Move to the next Lba
>
> +      StartingLba++;
>
> +      NumOfLba--;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +EXIT:
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Initialized the Firmware Volume if necessary and installs the
>
> +  gEdkiiNvVarStoreFormattedGuid protocol.
>
> +
>
> +  @param Instance                    Pointer to SmmStore instance
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS     Status;
>
> +  UINT32         FvbNumLba;
>
> +  EFI_BOOT_MODE  BootMode;
>
> +
>
> +  ASSERT ((Instance != NULL));
>
> +
>
> +  BootMode = GetBootModeHob ();
>
> +  if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    // Determine if there is a valid header at the beginning of the NorFlash
>
> +    Status = ValidateFvHeader ();
>
> +  }
>
> +
>
> +  // Install the Default FVB header if required
>
> +  if (EFI_ERROR (Status)) {
>
> +    // There is no valid header, so time to install one.
>
> +    DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Installing a correct one for this volume.\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +
>
> +    // Erase all the NorFlash that is reserved for variable storage
>
> +    FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
>
> +
>
> +    Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +
>
> +    // Install all appropriate headers
>
> +    Status = InitializeFvAndVariableStoreHeaders (Instance);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +  } else {
>
> +    DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__));
>
> +  }
>
> +
>
> +  //
>
> +  // The driver implementing the variable read service can now be dispatched;
>
> +  // the varstore headers are in place.
>
> +  //
>
> +  Status = gBS->InstallProtocolInterface (
>
> +                  &gImageHandle,
>
> +                  &gEdkiiNvVarStoreFormattedGuid,
>
> +                  EFI_NATIVE_INTERFACE,
>
> +                  NULL
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> new file mode 100644
> index 0000000000..2bd8886bcc
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> @@ -0,0 +1,66 @@
> +## @file
>
> +#  Component description file for SmmStore module
>
> +#
>
> +#  This module produces the FirmwareVolumeBlock protocol on top of the
>
> +#  SmmStore provided by bootloader and installs the NvVarStoreFormatted protocol.
>
> +#
>
> +#  Copyright (c) 2022, 9elements GmbH<BR>
>
> +#
>
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +#
>
> +##
>
> +
>
> +[Defines]
>
> +  INF_VERSION                    = 0x00010005
>
> +  BASE_NAME                      = SmmStoreFvbRuntimeDxe
>
> +  FILE_GUID                      = A0402FCA-6B25-4CEA-B7DD-C08F99714B29
>
> +  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
>
> +  VERSION_STRING                 = 1.0
>
> +  ENTRY_POINT                    = SmmStoreInitialize
>
> +
>
> +[Sources.common]
>
> +  SmmStoreFvbRuntimeDxe.c
>
> +  SmmStoreFvbRuntime.h
>
> +  SmmStoreFvbRuntime.c
>
> +
>
> +[Packages]
>
> +  MdePkg/MdePkg.dec
>
> +  MdeModulePkg/MdeModulePkg.dec
>
> +  EmbeddedPkg/EmbeddedPkg.dec
>
> +  UefiPayloadPkg/UefiPayloadPkg.dec
>
> +
>
> +[LibraryClasses]
>
> +  BaseLib
>
> +  DebugLib
>
> +  HobLib
>
> +  SmmStoreLib
>
> +  UefiLib
>
> +  UefiDriverEntryPoint
>
> +  UefiBootServicesTableLib
>
> +  UefiRuntimeLib
>
> +  DxeServicesTableLib
>
> +
>
> +[Guids]
>
> +  gEfiSystemNvDataFvGuid
>
> +  gEfiVariableGuid                  ## PRODUCES ## PROTOCOL
>
> +  gEfiAuthenticatedVariableGuid
>
> +  gEfiEventVirtualAddressChangeGuid
>
> +  gEdkiiNvVarStoreFormattedGuid     ## PRODUCES ## PROTOCOL
>
> +
>
> +[Protocols]
>
> +  gEfiDevicePathProtocolGuid          ## BY_START
>
> +  gEfiFirmwareVolumeBlockProtocolGuid ## BY_START
>
> +
>
> +[Pcd]
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
>
> +
>
> +[Depex]
>
> +  TRUE
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
> index cf440fee58..0f6421612c 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.dsc
> +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
> @@ -97,6 +97,7 @@
>    #
>
>    # EMU:      UEFI payload with EMU variable
>
>    # SPI:      UEFI payload with SPI NV variable support
>
> +  # SMMSTORE: UEFI payload with coreboot SMM NV variable support
>
>    # NONE:     UEFI payload with no variable modules
>
>    #
>
>    DEFINE VARIABLE_SUPPORT      = EMU
>
> @@ -261,9 +262,11 @@
>    LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
>
>    FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf
>
>    AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf
>
> -  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
> +  TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf
>
>    TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
>
> @@ -379,15 +382,13 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE
>
>
>
>  [PcdsFixedAtBuild]
>
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |TRUE
>
> -!else
>
> +!elseif $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |FALSE
>
>  !endif
>
> -
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE
>
> @@ -445,7 +446,6 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|$(MAX_LOGICAL_PROCESSORS)
>
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0
>
>    gUefiPayloadPkgTokenSpaceGuid.PcdBootloaderParameter|0
>
> -
>
>  ################################################################################
>
>  #
>
>  # Pcd DynamicEx Section - list of all EDK II PCD Entries defined by this Platform
>
> @@ -473,15 +473,19 @@
>  !endif
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0
>
>    gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3
>
> -!if $(VARIABLE_SUPPORT) == "SPI"
>
> +!if $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase  |0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0
>
>  !endif
>
>    # Disable SMM S3 script
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE
>
> @@ -697,6 +701,14 @@
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
>
> +    <LibraryClasses>
>
> +      NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
>
> +      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
>
> +  }
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf {
>
>      <LibraryClasses>
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
> index c7b04978ad..ca44f48f6d 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.fdf
> +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
> @@ -140,6 +140,10 @@ INF PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  INF UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    INF UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
>
>    INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
>
> --
> 2.32.0
>






^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-23 19:27       ` [edk2-devel] " Guo Dong
@ 2022-03-23 19:52         ` Sean Rhodes
  2022-03-23 21:06           ` King Sumo
  0 siblings, 1 reply; 14+ messages in thread
From: Sean Rhodes @ 2022-03-23 19:52 UTC (permalink / raw)
  To: Guo Dong, devel

[-- Attachment #1: Type: text/plain, Size: 389 bytes --]

Hey Guo

To have the parselib and not SMMSTORE support is a bit of a paradox; coreboot isn't fully functional without one. I think moving this and any subsequent patches into edk2-platforms will inevitably lead to forks, which isn't beneficial to anyone.

Is there any method (or ideally exception ;) ) you can suggest for allowing bootloader specific patches?

Many thanks

Sean

[-- Attachment #2: Type: text/html, Size: 421 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-23 19:52         ` Sean Rhodes
@ 2022-03-23 21:06           ` King Sumo
  2022-03-23 21:20             ` Guo Dong
  0 siblings, 1 reply; 14+ messages in thread
From: King Sumo @ 2022-03-23 21:06 UTC (permalink / raw)
  To: devel, sean; +Cc: Guo Dong

[-- Attachment #1: Type: text/plain, Size: 807 bytes --]

Looks like for Guo's patch SMMSTORE driver is not needed since it
implements a SpiFlashLib for accessing the SPI Flash controller (via
hardware sequencer) of Intel CPU targets...
But this is for Universal Payload only right? I guess coreboot supports
only the non-universal mode.
This stuff was tested only in SBL right?

On Wed, Mar 23, 2022 at 4:52 PM Sean Rhodes <sean@starlabs.systems> wrote:

> Hey Guo
>
> To have the parselib and not SMMSTORE support is a bit of a paradox;
> coreboot isn't fully functional without one. I think moving this and any
> subsequent patches into edk2-platforms will inevitably lead to forks, which
> isn't beneficial to anyone.
>
> Is there any method (or ideally exception ;) ) you can suggest for
> allowing bootloader specific patches?
>
> Many thanks
>
> Sean
> 
>
>

[-- Attachment #2: Type: text/html, Size: 1098 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
  2022-03-23 21:06           ` King Sumo
@ 2022-03-23 21:20             ` Guo Dong
  0 siblings, 0 replies; 14+ messages in thread
From: Guo Dong @ 2022-03-23 21:20 UTC (permalink / raw)
  To: devel@edk2.groups.io, kingsumos@gmail.com, Rhodes, Sean


[-- Attachment #1.1: Type: text/plain, Size: 1277 bytes --]


I think these questions could be answered in the attached email which I replied to Rudolph for similar questions.

Thanks,
Guo

From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of King Sumo
Sent: Wednesday, March 23, 2022 2:06 PM
To: devel@edk2.groups.io; Rhodes, Sean <sean@starlabs.systems>
Cc: Dong, Guo <guo.dong@intel.com>
Subject: Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol

Looks like for Guo's patch SMMSTORE driver is not needed since it implements a SpiFlashLib for accessing the SPI Flash controller (via hardware sequencer) of Intel CPU targets...
But this is for Universal Payload only right? I guess coreboot supports only the non-universal mode.
This stuff was tested only in SBL right?

On Wed, Mar 23, 2022 at 4:52 PM Sean Rhodes <sean@starlabs.systems<mailto:sean@starlabs.systems>> wrote:
Hey Guo

To have the parselib and not SMMSTORE support is a bit of a paradox; coreboot isn't fully functional without one. I think moving this and any subsequent patches into edk2-platforms will inevitably lead to forks, which isn't beneficial to anyone.

Is there any method (or ideally exception ;) ) you can suggest for allowing bootloader specific patches?

Many thanks

Sean


[-- Attachment #1.2: Type: text/html, Size: 4136 bytes --]

[-- Attachment #2: Type: message/rfc822, Size: 103652 bytes --]

From: "Dong, Guo" <guo.dong@intel.com>
To: "devel@edk2.groups.io" <devel@edk2.groups.io>, "patrick.rudolph@9elements.com" <patrick.rudolph@9elements.com>
Cc: "Rhodes, Sean" <sean@starlabs.systems>, "Ni, Ray" <ray.ni@intel.com>, "Ma, Maurice" <maurice.ma@intel.com>, "You, Benjamin" <benjamin.you@intel.com>
Subject: Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
Date: Wed, 23 Mar 2022 19:27:41 +0000
Message-ID: <16DF192D66911040.2188@groups.io>


Hi Rudolph,

By my understanding, edk2 repo is mainly for the modules defined by industry standards (e.g. UEFI and PI specifications).
For the modules that is specific to a vendor (e.g. Intel PCH), or a bootloader, edk2-platform would be a better places.

Currently in the EDK2 UefiPayloadpkg, beside the PCH modules I mentioned before, only ParstLib is bootloader specific.
So for PCH modules, I already submitted a patch to move to edk2-platforms repo.
For ParseLib, it is not used in universal UEFI payload. Will keep it as-is to be used for non-universal UEFI payload for back-combatable.
Hope this could help.

Thanks,
Guo

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Patrick Rudolph
Sent: Wednesday, March 23, 2022 11:25 AM
To: Dong, Guo <guo.dong@intel.com>
Cc: Rhodes, Sean <sean@starlabs.systems>; devel@edk2.groups.io; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>
Subject: Re: [edk2-devel] [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol

Hi Guo,
I don't see where it's written that UefiPayloadPkg is bootloader
independent. Can you point me to that documentation?
Reading the BuildAndIntegrationInstructions.txt I understand that it
is very bootloader specific, as it's  basically designed as a
bootloader payload.
Where do you draw the line between platform code and "generic" payload code?

Regards,
Patrick Rudolph

On Wed, Mar 23, 2022 at 7:12 PM Dong, Guo <guo.dong@intel.com> wrote:
>
>
> Hi Sean,
> SMMSTORE and this FVB module are specific for coreboot while UefiPayloadPkg in Edk2 is targeting platform/bootloader independent.
> I just submitted an "add UEFI payload platform FV" patch to edk2-platforms repo, in this patch it moves PCH specific modules from
> Edk2 UefiPayloadPkg to edk2-platforms Platform/Intel/PayloadPlatformPkg. Maybe these coreboot specific modules could also target
> to edk2-platforms repo?
>
> Thanks,
> Guo
> -----Original Message-----
> From: Sean Rhodes <sean@starlabs.systems>
> Sent: Monday, March 21, 2022 2:10 AM
> To: devel@edk2.groups.io
> Cc: Rhodes, Sean <sean@starlabs.systems>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Patrick Rudolph <patrick.rudolph@9elements.com>
> Subject: [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol
>
> This adds support for FVB in order to support a platform independent
> and non-volatile variable store on UefiPayloadPkg. The variable store
> makes use the SmmStoreLib to provide an unauthenticed variable store.
>
> Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports
> the SMMSTORE v2 feature. It implements a SMI handler that is able to
> write, read and erase pages in the boot media (SPI flash).
> The communication is done using a fixed communication buffer that is
> allocated in CBMEM. The existence of this optional feature is advertised
> by a coreboot table.
> When the SMMSTORE feature is not available the variable emulation is used
> by setting PcdEmuVariableNvModeEnable to TRUE.
>
> The DXE component provides runtime services and takes care of virtual to
> physical mapping the communication buffers between SMM and OS.
>
> The contents of the variable store can be accessed and modified by any
> priviledged application. As authentication is done by runtime services
> only the store shouldn't be used to store authenticated variables.
>
> Tested on Linux and Windows 10 on real hardware.
> Currently this cannot be tested on coreboot and qemu as it doesn't support
> the SMMSTORE on qemu.
>
> Cc: Guo Dong <guo.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Maurice Ma <maurice.ma@intel.com>
> Cc: Benjamin You <benjamin.you@intel.com>
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> Signed-off-by: Sean Rhodes <sean@starlabs.systems>
> ---
>  .../SmmStoreFvb/SmmStoreFvbRuntime.c          | 282 ++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntime.h          | 111 +++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.c       | 849 ++++++++++++++++++
>  .../SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf     |  66 ++
>  UefiPayloadPkg/UefiPayloadPkg.dsc             |  24 +-
>  UefiPayloadPkg/UefiPayloadPkg.fdf             |   4 +
>  6 files changed, 1330 insertions(+), 6 deletions(-)
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
>  create mode 100644 UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> new file mode 100644
> index 0000000000..08d5c408fb
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.c
> @@ -0,0 +1,282 @@
> +/** @file  SmmStoreFvbRuntime.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/DevicePathLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +STATIC EFI_EVENT  mSmmStoreVirtualAddrChangeEvent;
>
> +
>
> +//
>
> +// Global variable declarations
>
> +//
>
> +SMMSTORE_INSTANCE  *mSmmStoreInstance;
>
> +
>
> +SMMSTORE_INSTANCE  mSmmStoreInstanceTemplate = {
>
> +  SMMSTORE_SIGNATURE, // Signature
>
> +  NULL,               // Handle ... NEED TO BE FILLED
>
> +  {
>
> +    FvbGetAttributes,      // GetAttributes
>
> +    FvbSetAttributes,      // SetAttributes
>
> +    FvbGetPhysicalAddress, // GetPhysicalAddress
>
> +    FvbGetBlockSize,       // GetBlockSize
>
> +    FvbRead,               // Read
>
> +    FvbWrite,              // Write
>
> +    FvbEraseBlocks,        // EraseBlocks
>
> +    NULL,                  // ParentHandle
>
> +  }, //  FvbProtoccol
>
> +  0, // BlockSize ... NEED TO BE FILLED
>
> +  0, // LastBlock ... NEED TO BE FILLED
>
> +  0, // MmioAddress ... NEED TO BE FILLED
>
> +  {
>
> +    {
>
> +      {
>
> +        HARDWARE_DEVICE_PATH,
>
> +        HW_MEMMAP_DP,
>
> +        {
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
>
> +          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
>
> +        }
>
> +      },
>
> +      EfiMemoryMappedIO,
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +      (EFI_PHYSICAL_ADDRESS)0, // NEED TO BE FILLED
>
> +    },
>
> +    {
>
> +      END_DEVICE_PATH_TYPE,
>
> +      END_ENTIRE_DEVICE_PATH_SUBTYPE,
>
> +      {
>
> +        END_DEVICE_PATH_LENGTH,
>
> +        0
>
> +      }
>
> +    }
>
> +  } // DevicePath
>
> +};
>
> +
>
> +/**
>
> +  Initialize the SmmStore instance.
>
> +
>
> +
>
> +  @param[in]      FvBase         The physical MMIO base address of the FV containing
>
> +                                 the variable store.
>
> +
>
> +  @param[in]      NumberofBlocks Number of blocks within the FV.
>
> +  @param[in]      BlockSize      The size in bytes of one block within the FV.
>
> +  @param[in, out] Instance       The SmmStore instace to initialize
>
> +
>
> +**/
>
> +STATIC
>
> +EFI_STATUS
>
> +SmmStoreInitInstance (
>
> +  IN EFI_PHYSICAL_ADDRESS   FvBase,
>
> +  IN UINTN                  NumberofBlocks,
>
> +  IN UINTN                  BlockSize,
>
> +  IN OUT SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS             Status;
>
> +  FV_MEMMAP_DEVICE_PATH  *FvDevicePath;
>
> +
>
> +  ASSERT (Instance != NULL);
>
> +
>
> +  Instance->BlockSize   = BlockSize;
>
> +  Instance->LastBlock   = NumberofBlocks - 1;
>
> +  Instance->MmioAddress = FvBase;
>
> +
>
> +  FvDevicePath                                = &Instance->DevicePath;
>
> +  FvDevicePath->MemMapDevPath.StartingAddress = FvBase;
>
> +  FvDevicePath->MemMapDevPath.EndingAddress   = FvBase + BlockSize * NumberofBlocks - 1;
>
> +
>
> +  Status = FvbInitialize (Instance);
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = gBS->InstallMultipleProtocolInterfaces (
>
> +                  &Instance->Handle,
>
> +                  &gEfiDevicePathProtocolGuid,
>
> +                  &Instance->DevicePath,
>
> +                  &gEfiFirmwareVolumeBlockProtocolGuid,
>
> +                  &Instance->FvbProtocol,
>
> +                  NULL
>
> +                  );
>
> +  if (EFI_ERROR (Status)) {
>
> +    return Status;
>
> +  }
>
> +
>
> +  DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__));
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Fixup internal data so that EFI can be call in virtual mode.
>
> +  Call the passed in Child Notify event and convert any pointers in
>
> +  lib to virtual mode.
>
> +
>
> +  @param[in]    Event   The Event that is being processed
>
> +  @param[in]    Context Event Context
>
> +**/
>
> +STATIC
>
> +VOID
>
> +EFIAPI
>
> +SmmStoreVirtualNotifyEvent (
>
> +  IN EFI_EVENT  Event,
>
> +  IN VOID       *Context
>
> +  )
>
> +{
>
> +  // Convert Fvb
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.EraseBlocks);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetBlockSize);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.GetPhysicalAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Read);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.SetAttributes);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->FvbProtocol.Write);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance->MmioAddress);
>
> +  EfiConvertPointer (0x0, (VOID **)&mSmmStoreInstance);
>
> +
>
> +  return;
>
> +}
>
> +
>
> +/**
>
> +  The user Entry Point for module SmmStoreFvbRuntimeDxe. The user code starts with this function.
>
> +
>
> +  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
>
> +  @param[in] SystemTable    A pointer to the EFI System Table.
>
> +
>
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
>
> +  @retval other             Some error occurs when executing this entry point.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +SmmStoreInitialize (
>
> +  IN EFI_HANDLE        ImageHandle,
>
> +  IN EFI_SYSTEM_TABLE  *SystemTable
>
> +  )
>
> +{
>
> +  EFI_STATUS            Status;
>
> +  EFI_PHYSICAL_ADDRESS  MmioAddress;
>
> +  UINTN                 BlockSize;
>
> +  UINTN                 BlockCount;
>
> +  UINT32                NvStorageBase;
>
> +  UINT32                NvStorageSize;
>
> +  UINT32                NvVariableSize;
>
> +  UINT32                FtwWorkingSize;
>
> +  UINT32                FtwSpareSize;
>
> +
>
> +  Status = SmmStoreLibInitialize ();
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to initialize SmmStoreLib\n", __FUNCTION__));
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetMmioAddress (&MmioAddress);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore MMIO address\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetNumBlocks (&BlockCount);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore No. blocks\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  Status = SmmStoreLibGetBlockSize (&BlockSize);
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG ((DEBUG_ERROR, "%a: Failed to get SmmStore block size\n", __FUNCTION__));
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  NvStorageSize = BlockCount * BlockSize;
>
> +  NvStorageBase = MmioAddress;
>
> +
>
> +  FtwSpareSize   = (BlockCount / 2) * BlockSize;
>
> +  FtwWorkingSize = BlockSize;
>
> +  NvVariableSize = NvStorageSize - FtwSpareSize - FtwWorkingSize;
>
> +  DEBUG ((DEBUG_INFO, "NvStorageBase:0x%x, NvStorageSize:0x%x\n", NvStorageBase, NvStorageSize));
>
> +
>
> +  if (NvVariableSize >= 0x80000000) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableSize, NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageVariableBase, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageVariableBase64, NvStorageBase);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingSize, FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwWorkingBase64, NvStorageBase + NvVariableSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareSize, FtwSpareSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +  Status = PcdSet64S (PcdFlashNvStorageFtwSpareBase64, NvStorageBase + NvVariableSize + FtwWorkingSize);
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  mSmmStoreInstance = AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSmmStoreInstanceTemplate);
>
> +  if (mSmmStoreInstance == NULL) {
>
> +    SmmStoreLibDeinitialize ();
>
> +    DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__));
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  Status = SmmStoreInitInstance (
>
> +             MmioAddress,
>
> +             BlockCount,
>
> +             BlockSize,
>
> +             mSmmStoreInstance
>
> +             );
>
> +  if (EFI_ERROR (Status)) {
>
> +    DEBUG (
>
> +      (
>
> +       DEBUG_ERROR,
>
> +       "%a: Fail to create instance for SmmStore\n",
>
> +       __FUNCTION__
>
> +      )
>
> +      );
>
> +    FreePool (mSmmStoreInstance);
>
> +    SmmStoreLibDeinitialize ();
>
> +    return Status;
>
> +  }
>
> +
>
> +  //
>
> +  // Register for the virtual address change event
>
> +  //
>
> +  Status = gBS->CreateEventEx (
>
> +                  EVT_NOTIFY_SIGNAL,
>
> +                  TPL_NOTIFY,
>
> +                  SmmStoreVirtualNotifyEvent,
>
> +                  NULL,
>
> +                  &gEfiEventVirtualAddressChangeGuid,
>
> +                  &mSmmStoreVirtualAddrChangeEvent
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> new file mode 100644
> index 0000000000..8015d12d13
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntime.h
> @@ -0,0 +1,111 @@
> +/** @file  SmmStoreFvbRuntime.h
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#ifndef SMM_STORE_DXE_H_
>
> +#define SMM_STORE_DXE_H_
>
> +
>
> +#include <Base.h>
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Guid/EventGroup.h>
>
> +
>
> +#include <Protocol/FirmwareVolumeBlock.h>
>
> +
>
> +#include <Library/DebugLib.h>
>
> +#include <Library/IoLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/UefiRuntimeLib.h>
>
> +
>
> +#define SMMSTORE_SIGNATURE  SIGNATURE_32('S', 'M', 'M', 'S')
>
> +#define INSTANCE_FROM_FVB_THIS(a)  CR(a, SMMSTORE_INSTANCE, FvbProtocol, SMMSTORE_SIGNATURE)
>
> +
>
> +typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE;
>
> +
>
> +typedef struct {
>
> +  MEMMAP_DEVICE_PATH          MemMapDevPath;
>
> +  EFI_DEVICE_PATH_PROTOCOL    EndDevPath;
>
> +} FV_MEMMAP_DEVICE_PATH;
>
> +
>
> +struct _SMMSTORE_INSTANCE {
>
> +  UINT32                                 Signature;
>
> +  EFI_HANDLE                             Handle;
>
> +  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL    FvbProtocol;
>
> +  UINTN                                  BlockSize;
>
> +  UINTN                                  LastBlock;
>
> +  EFI_PHYSICAL_ADDRESS                   MmioAddress;
>
> +  FV_MEMMAP_DEVICE_PATH                  DevicePath;
>
> +};
>
> +
>
> +//
>
> +// SmmStoreFvbRuntimeDxe.c
>
> +//
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  );
>
> +
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  );
>
> +
>
> +#endif // SMM_STORE_DXE_H_
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> new file mode 100644
> index 0000000000..14d4ec324d
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.c
> @@ -0,0 +1,849 @@
> +/** @file  SmmStoreFvbRuntimeDxe.c
>
> +
>
> +  Copyright (c) 2022, 9elements GmbH<BR>
>
> +
>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +
>
> +**/
>
> +
>
> +#include <PiDxe.h>
>
> +
>
> +#include <Library/PcdLib.h>
>
> +#include <Library/BaseLib.h>
>
> +#include <Library/HobLib.h>
>
> +#include <Library/UefiLib.h>
>
> +#include <Library/BaseMemoryLib.h>
>
> +#include <Library/MemoryAllocationLib.h>
>
> +#include <Library/DxeServicesTableLib.h>
>
> +#include <Library/UefiBootServicesTableLib.h>
>
> +#include <Library/SmmStoreLib.h>
>
> +
>
> +#include <Guid/VariableFormat.h>
>
> +#include <Guid/SystemNvDataGuid.h>
>
> +#include <Guid/NvVarStoreFormatted.h>
>
> +
>
> +#include "SmmStoreFvbRuntime.h"
>
> +
>
> +///
>
> +/// The Firmware Volume Block Protocol is the low-level interface
>
> +/// to a firmware volume. File-level access to a firmware volume
>
> +/// should not be done using the Firmware Volume Block Protocol.
>
> +/// Normal access to a firmware volume must use the Firmware
>
> +/// Volume Protocol. Typically, only the file system driver that
>
> +/// produces the Firmware Volume Protocol will bind to the
>
> +/// Firmware Volume Block Protocol.
>
> +///
>
> +
>
> +/**
>
> +  Initialises the FV Header and Variable Store Header
>
> +  to support variable operations.
>
> +
>
> +  @param[in]  Instance - Pointer to SmmStore instance
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +InitializeFvAndVariableStoreHeaders (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS                  Status;
>
> +  VOID                        *Headers;
>
> +  UINTN                       HeadersLength;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FirmwareVolumeHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINT32                      NvStorageFtwSpareSize;
>
> +  UINT32                      NvStorageFtwWorkingSize;
>
> +  UINT32                      NvStorageVariableSize;
>
> +  UINT64                      NvStorageFtwSpareBase;
>
> +  UINT64                      NvStorageFtwWorkingBase;
>
> +  UINT64                      NvStorageVariableBase;
>
> +
>
> +  HeadersLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER);
>
> +  Headers       = AllocateZeroPool (HeadersLength);
>
> +
>
> +  NvStorageFtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
>
> +  NvStorageFtwSpareSize   = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  NvStorageVariableSize   = PcdGet32 (PcdFlashNvStorageVariableSize);
>
> +
>
> +  NvStorageFtwSpareBase = (PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) : PcdGet32 (PcdFlashNvStorageFtwSpareBase);
>
> +  NvStorageFtwWorkingBase = (PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0) ?
>
> +                            PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) : PcdGet32 (PcdFlashNvStorageFtwWorkingBase);
>
> +  NvStorageVariableBase = (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0) ?
>
> +                          PcdGet64 (PcdFlashNvStorageVariableBase64) : PcdGet32 (PcdFlashNvStorageVariableBase);
>
> +
>
> +  // FirmwareVolumeHeader->FvLength is declared to have the Variable area AND the FTW working area AND the FTW Spare contiguous.
>
> +  if ((NvStorageVariableBase + NvStorageVariableSize) != NvStorageFtwWorkingBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingBase is not contiguous with NvStorageVariableBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingBase + NvStorageFtwWorkingSize) != NvStorageFtwSpareBase) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareBase is not contiguous with NvStorageFtwWorkingBase region\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Check if the size of the area is at least one block size
>
> +  if ((NvStorageVariableSize <= 0) || (NvStorageVariableSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageVariableSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageVariableSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwWorkingSize <= 0) || (NvStorageFtwWorkingSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwWorkingSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwWorkingSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  if ((NvStorageFtwSpareSize <= 0) || (NvStorageFtwSpareSize / Instance->BlockSize <= 0)) {
>
> +    DEBUG ((
>
> +      DEBUG_ERROR,
>
> +      "%a: NvStorageFtwSpareSize is 0x%x, should be atleast one block size\n",
>
> +      __FUNCTION__,
>
> +      NvStorageFtwSpareSize
>
> +      ));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  // Ensure the Variable area Base Addresses are aligned on a block size boundaries
>
> +  if ((NvStorageVariableBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwWorkingBase % Instance->BlockSize != 0) ||
>
> +      (NvStorageFtwSpareBase % Instance->BlockSize != 0))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "%a: NvStorage Base addresses must be aligned to block size boundaries", __FUNCTION__));
>
> +    return EFI_INVALID_PARAMETER;
>
> +  }
>
> +
>
> +  //
>
> +  // EFI_FIRMWARE_VOLUME_HEADER
>
> +  //
>
> +  FirmwareVolumeHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Headers;
>
> +  CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid);
>
> +  FirmwareVolumeHeader->FvLength =
>
> +    PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +    PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +  FirmwareVolumeHeader->Signature  = EFI_FVH_SIGNATURE;
>
> +  FirmwareVolumeHeader->Attributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                                            EFI_FVB2_READ_ENABLED_CAP   | // Reads may be enabled
>
> +                                                            EFI_FVB2_READ_STATUS        | // Reads are currently enabled
>
> +                                                            EFI_FVB2_STICKY_WRITE       | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                                            EFI_FVB2_MEMORY_MAPPED      | // It is memory mapped
>
> +                                                            EFI_FVB2_ERASE_POLARITY     | // After erasure all bits take this value (i.e. '1')
>
> +                                                            EFI_FVB2_WRITE_STATUS       | // Writes are currently enabled
>
> +                                                            EFI_FVB2_WRITE_ENABLED_CAP    // Writes may be enabled
>
> +                                                            );
>
> +  FirmwareVolumeHeader->HeaderLength          = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY);
>
> +  FirmwareVolumeHeader->Revision              = EFI_FVH_REVISION;
>
> +  FirmwareVolumeHeader->BlockMap[0].NumBlocks = Instance->LastBlock + 1;
>
> +  FirmwareVolumeHeader->BlockMap[0].Length    = Instance->BlockSize;
>
> +  FirmwareVolumeHeader->BlockMap[1].NumBlocks = 0;
>
> +  FirmwareVolumeHeader->BlockMap[1].Length    = 0;
>
> +  FirmwareVolumeHeader->Checksum              = CalculateCheckSum16 ((UINT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength);
>
> +
>
> +  //
>
> +  // VARIABLE_STORE_HEADER
>
> +  //
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINTN)Headers + FirmwareVolumeHeader->HeaderLength);
>
> +  CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid);
>
> +  VariableStoreHeader->Size   = PcdGet32 (PcdFlashNvStorageVariableSize) - FirmwareVolumeHeader->HeaderLength;
>
> +  VariableStoreHeader->Format = VARIABLE_STORE_FORMATTED;
>
> +  VariableStoreHeader->State  = VARIABLE_STORE_HEALTHY;
>
> +
>
> +  // Install the combined super-header in the NorFlash
>
> +  Status = FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Headers);
>
> +
>
> +  FreePool (Headers);
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Check the integrity of firmware volume header.
>
> +
>
> +  @retval  EFI_SUCCESS   - The firmware volume is consistent
>
> +  @retval  EFI_NOT_FOUND - The firmware volume has been corrupted.
>
> +
>
> +**/
>
> +EFI_STATUS
>
> +ValidateFvHeader (
>
> +  VOID
>
> +  )
>
> +{
>
> +  UINT16                      Checksum;
>
> +  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;
>
> +  VARIABLE_STORE_HEADER       *VariableStoreHeader;
>
> +  UINTN                       VariableStoreLength;
>
> +  UINTN                       FvLength;
>
> +  EFI_STATUS                  TempStatus;
>
> +  UINTN                       BufferSize;
>
> +  UINTN                       BufferSizeReqested;
>
> +
>
> +  BufferSizeReqested = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
>
> +  FwVolHeader        = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  FvLength = PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +             PcdGet32 (PcdFlashNvStorageFtwSpareSize);
>
> +
>
> +  //
>
> +  // Verify the header revision, header signature, length
>
> +  // Length of FvBlock cannot be 2**64-1
>
> +  // HeaderLength cannot be an odd number
>
> +  //
>
> +  if (  (FwVolHeader->Revision  != EFI_FVH_REVISION)
>
> +     || (FwVolHeader->Signature != EFI_FVH_SIGNATURE)
>
> +     || (FwVolHeader->FvLength  != FvLength)
>
> +        )
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: No Firmware Volume header present\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  // Check the Firmware Volume Guid
>
> +  if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid) == FALSE ) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Firmware Volume Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested = FwVolHeader->HeaderLength;
>
> +  FreePool (FwVolHeader);
>
> +  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!FwVolHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Verify the header checksum
>
> +  Checksum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
>
> +  if (Checksum != 0) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: FV checksum is invalid (Checksum:0x%X)\n",
>
> +      __FUNCTION__,
>
> +      Checksum
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  BufferSizeReqested  = sizeof (VARIABLE_STORE_HEADER);
>
> +  VariableStoreHeader = (VARIABLE_STORE_HEADER *)AllocatePool (BufferSizeReqested);
>
> +  if (!VariableStoreHeader) {
>
> +    return EFI_OUT_OF_RESOURCES;
>
> +  }
>
> +
>
> +  BufferSize = BufferSizeReqested;
>
> +  TempStatus = SmmStoreLibRead (0, FwVolHeader->HeaderLength, &BufferSize, (UINT8 *)VariableStoreHeader);
>
> +  if (EFI_ERROR (TempStatus) || (BufferSizeReqested != BufferSize)) {
>
> +    FreePool (VariableStoreHeader);
>
> +    FreePool (FwVolHeader);
>
> +    return EFI_DEVICE_ERROR;
>
> +  }
>
> +
>
> +  // Check the Variable Store Guid
>
> +  if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) &&
>
> +      !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid))
>
> +  {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Guid non-compatible\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  VariableStoreLength = PcdGet32 (PcdFlashNvStorageVariableSize) - FwVolHeader->HeaderLength;
>
> +  if (VariableStoreHeader->Size != VariableStoreLength) {
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Variable Store Length does not match\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +    FreePool (FwVolHeader);
>
> +    FreePool (VariableStoreHeader);
>
> +    return EFI_NOT_FOUND;
>
> +  }
>
> +
>
> +  FreePool (FwVolHeader);
>
> +  FreePool (VariableStoreHeader);
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetAttributes() function retrieves the attributes and
>
> + current settings of the block.
>
> +
>
> + @param This         Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes   Pointer to EFI_FVB_ATTRIBUTES_2 in which the attributes and
>
> +                     current settings are returned.
>
> +                     Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS The firmware volume attributes were returned.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  EFI_FVB_ATTRIBUTES_2  FlashFvbAttributes;
>
> +
>
> +  FlashFvbAttributes = (EFI_FVB_ATTRIBUTES_2)(
>
> +                                              EFI_FVB2_READ_STATUS      | // Reads are currently enabled
>
> +                                              EFI_FVB2_WRITE_STATUS     | // Writes are enabled
>
> +                                              EFI_FVB2_STICKY_WRITE     | // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY
>
> +                                              EFI_FVB2_MEMORY_MAPPED    | // It is memory mapped
>
> +                                              EFI_FVB2_ERASE_POLARITY     // After erasure all bits take this value (i.e. '1')
>
> +                                              );
>
> +
>
> +  *Attributes = FlashFvbAttributes;
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes));
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The SetAttributes() function sets configurable firmware volume attributes
>
> + and returns the new settings of the firmware volume.
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Attributes               On input, Attributes is a pointer to EFI_FVB_ATTRIBUTES_2
>
> +                                 that contains the desired firmware volume settings.
>
> +                                 On successful return, it contains the new settings of
>
> +                                 the firmware volume.
>
> +                                 Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_VOLUME_HEADER.
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume attributes were returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The attributes requested are in conflict with the capabilities
>
> +                                 as declared in the firmware volume header.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbSetAttributes (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN OUT    EFI_FVB_ATTRIBUTES_2                 *Attributes
>
> +  )
>
> +{
>
> +  DEBUG ((DEBUG_ERROR, "FvbSetAttributes(0x%X) is not supported\n", *Attributes));
>
> +  return EFI_UNSUPPORTED;
>
> +}
>
> +
>
> +/**
>
> + The GetPhysicalAddress() function retrieves the base address of
>
> + a memory-mapped firmware volume. This function should be called
>
> + only for memory-mapped firmware volumes.
>
> +
>
> + @param This               Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Address            Pointer to a caller-allocated
>
> +                           EFI_PHYSICAL_ADDRESS that, on successful
>
> +                           return from GetPhysicalAddress(), contains the
>
> +                           base address of the firmware volume.
>
> +
>
> + @retval EFI_SUCCESS       The firmware volume base address was returned.
>
> +
>
> + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetPhysicalAddress (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  OUT       EFI_PHYSICAL_ADDRESS                 *Address
>
> +  )
>
> +{
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  ASSERT (Address != NULL);
>
> +  *Address = Instance->MmioAddress;
>
> +
>
> +  return EFI_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + The GetBlockSize() function retrieves the size of the requested
>
> + block. It also returns the number of additional blocks with
>
> + the identical size. The GetBlockSize() function is used to
>
> + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER).
>
> +
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                      Indicates the block for which to return the size.
>
> +
>
> + @param BlockSize                Pointer to a caller-allocated UINTN in which
>
> +                                 the size of the block is returned.
>
> +
>
> + @param NumberOfBlocks           Pointer to a caller-allocated UINTN in
>
> +                                 which the number of consecutive blocks,
>
> +                                 starting with Lba, is returned. All
>
> +                                 blocks in this range have a size of
>
> +                                 BlockSize.
>
> +
>
> +
>
> + @retval EFI_SUCCESS             The firmware volume base address was returned.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   The requested LBA is out of range.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbGetBlockSize (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  OUT       UINTN                                *BlockSize,
>
> +  OUT       UINTN                                *NumberOfBlocks
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=%ld, BlockSize=0x%x, LastBlock=%ld)\n", Lba, Instance->BlockSize, Instance->LastBlock));
>
> +
>
> +  if (Lba > Instance->LastBlock) {
>
> +    DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is beyond the last Lba (%ld).\n", Lba, Instance->LastBlock));
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    *BlockSize      = (UINTN)Instance->BlockSize;
>
> +    *NumberOfBlocks = (UINTN)(Instance->LastBlock - Lba + 1);
>
> +
>
> +    DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=0x%x, *NumberOfBlocks=0x%x.\n", *BlockSize, *NumberOfBlocks));
>
> +
>
> +    Status = EFI_SUCCESS;
>
> +  }
>
> +
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> + Reads the specified number of bytes into a buffer from the specified block.
>
> +
>
> + The Read() function reads the requested number of bytes from the
>
> + requested block and stores them in the provided buffer.
>
> + Implementations should be mindful that the firmware volume
>
> + might be in the ReadDisabled state. If it is in this state,
>
> + the Read() function must return the status code
>
> + EFI_ACCESS_DENIED without modifying the contents of the
>
> + buffer. The Read() function must also prevent spanning block
>
> + boundaries. If a read is requested that would span a block
>
> + boundary, the read must read up to the boundary but not
>
> + beyond. The output parameter NumBytes must be set to correctly
>
> + indicate the number of bytes actually read. The caller must be
>
> + aware that a read may be partially completed.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index from which to read.
>
> +
>
> + @param Offset               Offset into the block at which to begin reading.
>
> +
>
> + @param NumBytes             Pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes read.
>
> +
>
> + @param Buffer               Pointer to a caller-allocated buffer that will be used
>
> +                             to hold the data that is read.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was read successfully,  and contents are
>
> +                             in Buffer.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             returned in Buffer.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the ReadDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is not functioning correctly and could not be read.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbRead (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN OUT    UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibRead (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Writes the specified number of bytes from the input buffer to the block.
>
> +
>
> + The Write() function writes the specified number of bytes from
>
> + the provided buffer to the specified block and offset. If the
>
> + firmware volume is sticky write, the caller must ensure that
>
> + all the bits of the specified range to write are in the
>
> + EFI_FVB_ERASE_POLARITY state before calling the Write()
>
> + function, or else the result will be unpredictable. This
>
> + unpredictability arises because, for a sticky-write firmware
>
> + volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY
>
> + state but cannot flip it back again.  Before calling the
>
> + Write() function,  it is recommended for the caller to first call
>
> + the EraseBlocks() function to erase the specified block to
>
> + write. A block erase cycle will transition bits from the
>
> + (NOT)EFI_FVB_ERASE_POLARITY state back to the
>
> + EFI_FVB_ERASE_POLARITY state. Implementations should be
>
> + mindful that the firmware volume might be in the WriteDisabled
>
> + state. If it is in this state, the Write() function must
>
> + return the status code EFI_ACCESS_DENIED without modifying the
>
> + contents of the firmware volume. The Write() function must
>
> + also prevent spanning block boundaries. If a write is
>
> + requested that spans a block boundary, the write must store up
>
> + to the boundary but not beyond. The output parameter NumBytes
>
> + must be set to correctly indicate the number of bytes actually
>
> + written. The caller must be aware that a write may be
>
> + partially completed. All writes, partial or otherwise, must be
>
> + fully flushed to the hardware before the Write() service
>
> + returns.
>
> +
>
> + @param This                 Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL instance.
>
> +
>
> + @param Lba                  The starting logical block index to write to.
>
> +
>
> + @param Offset               Offset into the block at which to begin writing.
>
> +
>
> + @param NumBytes             The pointer to a UINTN.
>
> +                             At entry, *NumBytes contains the total size of the buffer.
>
> +                             At exit, *NumBytes contains the total number of bytes actually written.
>
> +
>
> + @param Buffer               The pointer to a caller-allocated buffer that contains the source for the write.
>
> +
>
> + @retval EFI_SUCCESS         The firmware volume was written successfully.
>
> +
>
> + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.
>
> +                             On output, NumBytes contains the total number of bytes
>
> +                             actually written.
>
> +
>
> + @retval EFI_ACCESS_DENIED   The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR    The block device is malfunctioning and could not be written.
>
> +
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbWrite (
>
> +  IN CONST  EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  IN        EFI_LBA                              Lba,
>
> +  IN        UINTN                                Offset,
>
> +  IN OUT    UINTN                                *NumBytes,
>
> +  IN        UINT8                                *Buffer
>
> +  )
>
> +{
>
> +  UINTN              BlockSize;
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));
>
> +
>
> +  // Cache the block size to avoid de-referencing pointers all the time
>
> +  BlockSize = Instance->BlockSize;
>
> +
>
> +  // The read must not span block boundaries.
>
> +  // We need to check each variable individually because adding two large values together overflows.
>
> +  if ((Offset               >= BlockSize) ||
>
> +      (*NumBytes            >  BlockSize) ||
>
> +      ((Offset + *NumBytes) >  BlockSize))
>
> +  {
>
> +    DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize));
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  // We must have some bytes to read
>
> +  if (*NumBytes == 0) {
>
> +    return EFI_BAD_BUFFER_SIZE;
>
> +  }
>
> +
>
> +  return SmmStoreLibWrite (Lba, Offset, NumBytes, Buffer);
>
> +}
>
> +
>
> +/**
>
> + Erases and initialises a firmware volume block.
>
> +
>
> + The EraseBlocks() function erases one or more blocks as denoted
>
> + by the variable argument list. The entire parameter list of
>
> + blocks must be verified before erasing any blocks. If a block is
>
> + requested that does not exist within the associated firmware
>
> + volume (it has a larger index than the last block of the
>
> + firmware volume), the EraseBlocks() function must return the
>
> + status code EFI_INVALID_PARAMETER without modifying the contents
>
> + of the firmware volume. Implementations should be mindful that
>
> + the firmware volume might be in the WriteDisabled state. If it
>
> + is in this state, the EraseBlocks() function must return the
>
> + status code EFI_ACCESS_DENIED without modifying the contents of
>
> + the firmware volume. All calls to EraseBlocks() must be fully
>
> + flushed to the hardware before the EraseBlocks() service
>
> + returns.
>
> +
>
> + @param This                     Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL
>
> + instance.
>
> +
>
> + @param ...                      The variable argument list is a list of tuples.
>
> +                                 Each tuple describes a range of LBAs to erase
>
> +                                 and consists of the following:
>
> +                                 - An EFI_LBA that indicates the starting LBA
>
> +                                 - A UINTN that indicates the number of blocks to erase.
>
> +
>
> +                                 The list is terminated with an EFI_LBA_LIST_TERMINATOR.
>
> +                                 For example, the following indicates that two ranges of blocks
>
> +                                 (5-7 and 10-11) are to be erased:
>
> +                                 EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_LIST_TERMINATOR);
>
> +
>
> + @retval EFI_SUCCESS             The erase request successfully completed.
>
> +
>
> + @retval EFI_ACCESS_DENIED       The firmware volume is in the WriteDisabled state.
>
> +
>
> + @retval EFI_DEVICE_ERROR        The block device is not functioning correctly and could not be written.
>
> +                                 The firmware device may have been partially erased.
>
> +
>
> + @retval EFI_INVALID_PARAMETER   One or more of the LBAs listed in the variable argument list do
>
> +                                 not exist in the firmware volume.
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbEraseBlocks (
>
> +  IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL  *This,
>
> +  ...
>
> +  )
>
> +{
>
> +  EFI_STATUS         Status;
>
> +  VA_LIST            Args;
>
> +  EFI_LBA            StartingLba; // Lba from which we start erasing
>
> +  UINTN              NumOfLba;    // Number of Lba blocks to erase
>
> +  SMMSTORE_INSTANCE  *Instance;
>
> +
>
> +  Instance = INSTANCE_FROM_FVB_THIS (This);
>
> +
>
> +  Status = EFI_SUCCESS;
>
> +
>
> +  // Before erasing, check the entire list of parameters to ensure all specified blocks are valid
>
> +
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // All blocks must be within range
>
> +    DEBUG ((
>
> +      DEBUG_BLKIO,
>
> +      "FvbEraseBlocks: Check if: ( StartingLba=%ld + NumOfLba=%Lu - 1 ) > LastBlock=%ld.\n",
>
> +      StartingLba,
>
> +      (UINT64)NumOfLba,
>
> +      Instance->LastBlock
>
> +      ));
>
> +    if ((NumOfLba == 0) || ((StartingLba + NumOfLba - 1) > Instance->LastBlock)) {
>
> +      VA_END (Args);
>
> +      DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past the last Lba.\n"));
>
> +      Status = EFI_INVALID_PARAMETER;
>
> +      goto EXIT;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +  //
>
> +  // To get here, all must be ok, so start erasing
>
> +  //
>
> +  VA_START (Args, This);
>
> +  do {
>
> +    // Get the Lba from which we start erasing
>
> +    StartingLba = VA_ARG (Args, EFI_LBA);
>
> +
>
> +    // Have we reached the end of the list?
>
> +    if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
>
> +      // Exit the while loop
>
> +      break;
>
> +    }
>
> +
>
> +    // How many Lba blocks are we requested to erase?
>
> +    NumOfLba = VA_ARG (Args, UINTN);
>
> +
>
> +    // Go through each one and erase it
>
> +    while (NumOfLba > 0) {
>
> +      // Erase it
>
> +      DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=%ld\n", StartingLba));
>
> +      Status = SmmStoreLibEraseBlock (StartingLba);
>
> +      if (EFI_ERROR (Status)) {
>
> +        VA_END (Args);
>
> +        Status = EFI_DEVICE_ERROR;
>
> +        goto EXIT;
>
> +      }
>
> +
>
> +      // Move to the next Lba
>
> +      StartingLba++;
>
> +      NumOfLba--;
>
> +    }
>
> +  } while (TRUE);
>
> +
>
> +  VA_END (Args);
>
> +
>
> +EXIT:
>
> +  return Status;
>
> +}
>
> +
>
> +/**
>
> +  Initialized the Firmware Volume if necessary and installs the
>
> +  gEdkiiNvVarStoreFormattedGuid protocol.
>
> +
>
> +  @param Instance                    Pointer to SmmStore instance
>
> +
>
> + **/
>
> +EFI_STATUS
>
> +EFIAPI
>
> +FvbInitialize (
>
> +  IN SMMSTORE_INSTANCE  *Instance
>
> +  )
>
> +{
>
> +  EFI_STATUS     Status;
>
> +  UINT32         FvbNumLba;
>
> +  EFI_BOOT_MODE  BootMode;
>
> +
>
> +  ASSERT ((Instance != NULL));
>
> +
>
> +  BootMode = GetBootModeHob ();
>
> +  if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
>
> +    Status = EFI_INVALID_PARAMETER;
>
> +  } else {
>
> +    // Determine if there is a valid header at the beginning of the NorFlash
>
> +    Status = ValidateFvHeader ();
>
> +  }
>
> +
>
> +  // Install the Default FVB header if required
>
> +  if (EFI_ERROR (Status)) {
>
> +    // There is no valid header, so time to install one.
>
> +    DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__));
>
> +    DEBUG ((
>
> +      DEBUG_INFO,
>
> +      "%a: Installing a correct one for this volume.\n",
>
> +      __FUNCTION__
>
> +      ));
>
> +
>
> +    // Erase all the NorFlash that is reserved for variable storage
>
> +    FvbNumLba = (PcdGet32 (PcdFlashNvStorageVariableSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwWorkingSize) +
>
> +                 PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->BlockSize;
>
> +
>
> +    Status = FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumLba, EFI_LBA_LIST_TERMINATOR);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +
>
> +    // Install all appropriate headers
>
> +    Status = InitializeFvAndVariableStoreHeaders (Instance);
>
> +    if (EFI_ERROR (Status)) {
>
> +      return Status;
>
> +    }
>
> +  } else {
>
> +    DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__));
>
> +  }
>
> +
>
> +  //
>
> +  // The driver implementing the variable read service can now be dispatched;
>
> +  // the varstore headers are in place.
>
> +  //
>
> +  Status = gBS->InstallProtocolInterface (
>
> +                  &gImageHandle,
>
> +                  &gEdkiiNvVarStoreFormattedGuid,
>
> +                  EFI_NATIVE_INTERFACE,
>
> +                  NULL
>
> +                  );
>
> +  ASSERT_EFI_ERROR (Status);
>
> +
>
> +  return Status;
>
> +}
>
> diff --git a/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> new file mode 100644
> index 0000000000..2bd8886bcc
> --- /dev/null
> +++ b/UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
> @@ -0,0 +1,66 @@
> +## @file
>
> +#  Component description file for SmmStore module
>
> +#
>
> +#  This module produces the FirmwareVolumeBlock protocol on top of the
>
> +#  SmmStore provided by bootloader and installs the NvVarStoreFormatted protocol.
>
> +#
>
> +#  Copyright (c) 2022, 9elements GmbH<BR>
>
> +#
>
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +#
>
> +##
>
> +
>
> +[Defines]
>
> +  INF_VERSION                    = 0x00010005
>
> +  BASE_NAME                      = SmmStoreFvbRuntimeDxe
>
> +  FILE_GUID                      = A0402FCA-6B25-4CEA-B7DD-C08F99714B29
>
> +  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
>
> +  VERSION_STRING                 = 1.0
>
> +  ENTRY_POINT                    = SmmStoreInitialize
>
> +
>
> +[Sources.common]
>
> +  SmmStoreFvbRuntimeDxe.c
>
> +  SmmStoreFvbRuntime.h
>
> +  SmmStoreFvbRuntime.c
>
> +
>
> +[Packages]
>
> +  MdePkg/MdePkg.dec
>
> +  MdeModulePkg/MdeModulePkg.dec
>
> +  EmbeddedPkg/EmbeddedPkg.dec
>
> +  UefiPayloadPkg/UefiPayloadPkg.dec
>
> +
>
> +[LibraryClasses]
>
> +  BaseLib
>
> +  DebugLib
>
> +  HobLib
>
> +  SmmStoreLib
>
> +  UefiLib
>
> +  UefiDriverEntryPoint
>
> +  UefiBootServicesTableLib
>
> +  UefiRuntimeLib
>
> +  DxeServicesTableLib
>
> +
>
> +[Guids]
>
> +  gEfiSystemNvDataFvGuid
>
> +  gEfiVariableGuid                  ## PRODUCES ## PROTOCOL
>
> +  gEfiAuthenticatedVariableGuid
>
> +  gEfiEventVirtualAddressChangeGuid
>
> +  gEdkiiNvVarStoreFormattedGuid     ## PRODUCES ## PROTOCOL
>
> +
>
> +[Protocols]
>
> +  gEfiDevicePathProtocolGuid          ## BY_START
>
> +  gEfiFirmwareVolumeBlockProtocolGuid ## BY_START
>
> +
>
> +[Pcd]
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
>
> +
>
> +[Depex]
>
> +  TRUE
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
> index cf440fee58..0f6421612c 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.dsc
> +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
> @@ -97,6 +97,7 @@
>    #
>
>    # EMU:      UEFI payload with EMU variable
>
>    # SPI:      UEFI payload with SPI NV variable support
>
> +  # SMMSTORE: UEFI payload with coreboot SMM NV variable support
>
>    # NONE:     UEFI payload with no variable modules
>
>    #
>
>    DEFINE VARIABLE_SUPPORT      = EMU
>
> @@ -261,9 +262,11 @@
>    LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf
>
>    FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf
>
>    AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf
>
> -  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  SmmStoreLib|UefiPayloadPkg/Library/SmmStoreLib/SmmStoreLib.inf
>
> +  TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf
>
>    TpmMeasurementLib|SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
>
> @@ -379,15 +382,13 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE
>
>
>
>  [PcdsFixedAtBuild]
>
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |TRUE
>
> -!else
>
> +!elseif $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable        |FALSE
>
>  !endif
>
> -
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE
>
> @@ -445,7 +446,6 @@
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber|$(MAX_LOGICAL_PROCESSORS)
>
>    gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs|0
>
>    gUefiPayloadPkgTokenSpaceGuid.PcdBootloaderParameter|0
>
> -
>
>  ################################################################################
>
>  #
>
>  # Pcd DynamicEx Section - list of all EDK II PCD Entries defined by this Platform
>
> @@ -473,15 +473,19 @@
>  !endif
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FALSE
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0
>
>    gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3
>
> -!if $(VARIABLE_SUPPORT) == "SPI"
>
> +!if $(VARIABLE_SUPPORT) == "SPI" || $(VARIABLE_SUPPORT) == "SMMSTORE"
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize  |0
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase  |0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0
>
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0
>
>  !endif
>
>    # Disable SMM S3 script
>
>    gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE
>
> @@ -697,6 +701,14 @@
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
>
> +    <LibraryClasses>
>
> +      NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
>
> +      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
>
> +  }
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf {
>
>      <LibraryClasses>
>
> diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayloadPkg.fdf
> index c7b04978ad..ca44f48f6d 100644
> --- a/UefiPayloadPkg/UefiPayloadPkg.fdf
> +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
> @@ -140,6 +140,10 @@ INF PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
>
>
>  !if $(VARIABLE_SUPPORT) == "EMU"
>
>    INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
> +!elseif $(VARIABLE_SUPPORT) == "SMMSTORE"
>
> +  INF UefiPayloadPkg/SmmStoreFvb/SmmStoreFvbRuntimeDxe.inf
>
> +  INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>
> +  INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>
>  !elseif $(VARIABLE_SUPPORT) == "SPI"
>
>    INF UefiPayloadPkg/FvbRuntimeDxe/FvbSmm.inf
>
>    INF MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
>
> --
> 2.32.0
>







-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#87911): https://edk2.groups.io/g/devel/message/87911
Mute This Topic: https://groups.io/mt/89923799/1781375
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [guo.dong@intel.com]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry
  2022-03-23 17:58 ` [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Guo Dong
@ 2022-03-24  0:37   ` Ni, Ray
  2022-03-24  1:58     ` duntan
  0 siblings, 1 reply; 14+ messages in thread
From: Ni, Ray @ 2022-03-24  0:37 UTC (permalink / raw)
  To: Dong, Guo, Rhodes, Sean, devel@edk2.groups.io, Tan, Dun
  Cc: Patrick Rudolph, Ma, Maurice, You, Benjamin, Rhodes, Sean

Acked-by: Ray Ni <ray.ni@intel.com>

Dun, can you help review?

-----Original Message-----
From: Dong, Guo <guo.dong@intel.com> 
Sent: Thursday, March 24, 2022 1:58 AM
To: Rhodes, Sean <sean@starlabs.systems>; devel@edk2.groups.io
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: RE: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry


Reviewed-by: Guo Dong <guo.dong@intel.com>

-----Original Message-----
From: Sean Rhodes <sean@starlabs.systems> 
Sent: Monday, March 21, 2022 2:10 AM
To: devel@edk2.groups.io
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Remove a duplicated entry to fix a CI error.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/UefiPayloadPkg.ci.yaml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
index 93f4d3603d..909379eaac 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
+++ b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
@@ -20,7 +20,6 @@
         "IgnoreFiles": [

             "Include/Coreboot.h",

             "Library/CbParseLib/CbParseLib.c",

-            "Library/CbParseLib/CbParseLib.c",

             "PayloadLoaderPeim/ElfLib/ElfCommon.h",

             "PayloadLoaderPeim/ElfLib/Elf32.h",

             "PayloadLoaderPeim/ElfLib/Elf64.h"

-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry
  2022-03-24  0:37   ` Ni, Ray
@ 2022-03-24  1:58     ` duntan
  0 siblings, 0 replies; 14+ messages in thread
From: duntan @ 2022-03-24  1:58 UTC (permalink / raw)
  To: Ni, Ray, Dong, Guo, Rhodes, Sean, devel@edk2.groups.io
  Cc: Patrick Rudolph, Ma, Maurice, You, Benjamin, Rhodes, Sean

Reviewed-by: Dun Tan <dun.tan@intel.com>

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Thursday, March 24, 2022 8:38 AM
To: Dong, Guo <guo.dong@intel.com>; Rhodes, Sean <sean@starlabs.systems>; devel@edk2.groups.io; Tan, Dun <dun.tan@intel.com>
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: RE: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry

Acked-by: Ray Ni <ray.ni@intel.com>

Dun, can you help review?

-----Original Message-----
From: Dong, Guo <guo.dong@intel.com> 
Sent: Thursday, March 24, 2022 1:58 AM
To: Rhodes, Sean <sean@starlabs.systems>; devel@edk2.groups.io
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: RE: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry


Reviewed-by: Guo Dong <guo.dong@intel.com>

-----Original Message-----
From: Sean Rhodes <sean@starlabs.systems> 
Sent: Monday, March 21, 2022 2:10 AM
To: devel@edk2.groups.io
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>; Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems>
Subject: [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Remove a duplicated entry to fix a CI error.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
 UefiPayloadPkg/UefiPayloadPkg.ci.yaml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
index 93f4d3603d..909379eaac 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
+++ b/UefiPayloadPkg/UefiPayloadPkg.ci.yaml
@@ -20,7 +20,6 @@
         "IgnoreFiles": [

             "Include/Coreboot.h",

             "Library/CbParseLib/CbParseLib.c",

-            "Library/CbParseLib/CbParseLib.c",

             "PayloadLoaderPeim/ElfLib/ElfCommon.h",

             "PayloadLoaderPeim/ElfLib/Elf32.h",

             "PayloadLoaderPeim/ElfLib/Elf64.h"

-- 
2.32.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-03-24  1:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-21  9:10 [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Sean Rhodes
2022-03-21  9:10 ` [PATCH 2/5] UefiPayloadPkg/Include/Coreboot: Add headers for SMMSTOREv2 table Sean Rhodes
2022-03-21  9:10 ` [PATCH 3/5] UefiPayloadPkg: Add SmmStoreInfoGuid Sean Rhodes
2022-03-21  9:10 ` [PATCH 4/5] UefiPayloadPkg: Add SmmStoreLib Sean Rhodes
2022-03-21  9:10 ` [PATCH 5/5] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Sean Rhodes
2022-03-23 18:12   ` Guo Dong
2022-03-23 18:24     ` Patrick Rudolph
2022-03-23 19:27       ` [edk2-devel] " Guo Dong
2022-03-23 19:52         ` Sean Rhodes
2022-03-23 21:06           ` King Sumo
2022-03-23 21:20             ` Guo Dong
2022-03-23 17:58 ` [PATCH 1/5] UefiPayloadPkg/UefiPayloadPkg.ci.yaml: Remove duplicated entry Guo Dong
2022-03-24  0:37   ` Ni, Ray
2022-03-24  1:58     ` duntan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox