public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH V2 0/9] UEFI Variable SMI Reduction
@ 2019-09-28  1:47 Kubacki, Michael A
  2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
                   ` (8 more replies)
  0 siblings, 9 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220

V2 Changes:

Patch #1 in V1 both moved functions to VariableParsing.c and modified some
functionality in those functions. In V2, the functions are first moved and
then functionality is modified in subsequent patches. This resulted in the
following new patches in the V2 patch series:

 1. MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list
 2. MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
 3. MdeModulePkg/Variable: Add local auth status in VariableParsing
 4. MdeModulePkg/Variable: Add a file for NV variable functions

Apart from this refactor in the patches, no functionally impacting changes
were made.

Overview
---------
This patch series reduces SMM usage when using VariableSmmRuntimeDxe with
VariableSmm. It does so by eliminating SMM usage for runtime service
GetVariable () and GetNextVariableName () invocations. Most UEFI variable
usage in typical systems after the variable store is initialized
(e.g. manufacturing boots) is due to GetVariable ( ) and
GetNextVariableName () not SetVariable (). GetVariable () calls can regularly
exceed 100 per boot while SetVariable () calls typically remain less than 10
per boot. By focusing on the common case, the majority of overhead associated
with SMM can be avoided while still using existing and proven code for
operations such as variable authentication that require an isolated execution
environment.

 * Advantage: Reduces overall system SMM usage
 * Disadvantage: Requires more Runtime data memory usage

Initial Performance Observations
---------------------------------
 * With these proposed changes, an Intel Atom based SoC saw GetVariable ( )
   time for an existing variable reduce from ~220us to ~5us.

Major Changes
--------------
 1. Two UEFI variable caches will be maintained.
     a. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to serve
         runtime service GetVariable () and GetNextVariableName () callers.
     b. "SMM cache" - Maintained in VariableSmm to service SMM GetVariable ()
         and GetNextVariableName () callers.
         i. A cache in SMRAM is retained so SMM modules do not operate on data
            outside SMRAM.
 2. A new UEFI variable read and write flow will be used as described below.

At any given time, the two caches would be coherent. On a variable write, the
runtime cache is only updated after validation in SMM and, in the case of a
non-volatile UEFI variable, the variable must also be successfully written to
non-volatile storage.

Prior RFC Feedback Addressed
-----------------------------
RFC sent Sept. 5, 2019: https://edk2.groups.io/g/devel/message/46939

1. UEFI variable data retrieval from a ring 0 buffer

   A common concern with this proposed set of changes is the potential security
   threat presented by serving runtime services callers from a ring 0 memory
   buffer of EfiRuntimeServicesData type. This conclusion was that this change
   does not fundamentally alter the attack surface. The UEFI variable Runtime
   Services are invoked from ring 0 and the data already travels through ring
   0 buffers (such as the SMM communicate buffer) to reach the caller. Even
   today if ring 0 is assumed to be malicious, the malicious code may keep one
   AP in a loop to monitor the communication data, when the BSP gets an
   (authenticated) variable. When the communication buffer is updated and the
   status is set to EFI_SUCCESS, the AP may modify the communication buffer
   contents such the tampered data is returned to the BSP caller. Or an
   interrupt handler on the BSP may alter the communication buffer contents
   before the data is returned to the caller. In summary, this was not found to
   introduce any attack not possible today.

2. VarCheckLib impact

   VarCheckLib plays a role in SetVariable () calls. This patch series only
   changes GetVariable () behavior. Therefore, VarCheckLib is expected to
   have no impact due to these changes.

Testing Performed
------------------
This code was tested with the master branch of edk2 on an Intel Kaby Lake U
Intel Whiskey Lake U reference validation platform. The set of tests performed
included:

1. Boot from S5 to Windows 10 OS with SMM variables enabled.
2. Boot from S5 to Ubuntu 18.04.1 LTS with SMM variable enabled.
3. Boot from S5 to EFI shell with DXE variables enabled.
4. Dump UEFI variable store at shell with dmpstore to verify contents.
5. Dump NvStorage FV from SPI flash after boot to verify contents written.
6. Dump UEFI variable statistics with VariableInfo at shell.
7. Boot with emulated variables enabled.
8. Cycles of adding and deleting a UEFI variable to verify cache results.
9. Set OsIndications to stop at FW UI to verify cache load of non-volatile
   contents.

Why Keep SMM on Variable Writes
--------------------------------
 * SMM provides a ubiquitous isolated execution environment in x86 for
   authenticated UEFI variables.
 * BIOS region SPI flash write restrictions to SMM in platforms today can
   be retained.

Today's UEFI Variable Cache (for reference)
--------------------------------------------
 * Maintained in SMRAM via VariableSmm.
 * A "write-through" cache of variable data in the form of a UEFI variable
   store.
 * Non-volatile and volatile variables are maintained in separate buffers
  (variable stores).

Runtime & SMM Cache Coherency
------------------------------
The non-volatile cache should always accurately reflect non-volatile storage
contents (done today) and the "SMM cache" and "Runtime cache" should always be
coherent on access. The runtime cache is updated by VariableSmm.

Updating both caches from within a SMM SetVariable () operation is fairly
straightforward but a race condition can occur if an SMI occurs during the
execution of runtime code reading from the runtime cache. To handle this case,
a runtime cache read lock is introduced that explicitly moves pending updates
from SMM to the runtime cache if an SMM update occurs while the runtime cache
is locked. Note that it is not expected a Runtime services call will interrupt
SMM processing since all CPU cores rendezvous in SMM.

New Key Elements for Coherence
-------------------------------
Runtime DXE (VariableSmmRuntimeDxe)
 1. RuntimeCacheReadLock - A global lock used to lock read access to the
                           runtime cache.
 2. RuntimeCachePendingUpdate - A global flag used to notify runtime code of a
                                pending cache update in SMM.

SMM (VariableSmm)
 1. FlushRuntimeCachePendingUpdate SMI - A SW SMI handler that synchronizes
                                         the runtime cache buffer with the SMM
                                         cache buffer.

Proposed Runtime DXE Read Flow
-------------------------------
 1. Wait for RuntimeCacheReadLock to be free
 2. Acquire RuntimeCacheReadLock
 3. If RuntimeCachePendingUpdate flag (rare) is set then:
     3.a. Trigger FlushRuntimeCachePendingUpdate SMI
     3.b. Verify RuntimeCachePendingUpdate flag is cleared
 4. Perform read from RuntimeCache
 5. Release RuntimeCacheReadLock

Proposed FlushRuntimeCachePendingUpdate SMI
--------------------------------------------
 1. If RuntimeCachePendingUpdate flag is not set:
     1.a. Return
 2. Copy the data at RuntimeCachePendingOffset of RuntimeCachePendingLength to
    RuntimeCache
 3. Clear the RuntimeCachePendingUpdate flag

Proposed SMM Write Flow
------------------------
 1. Perform variable authentication and non-volatile write. If either fail,
    return an error to the caller.
 2. If RuntimeCacheReadLock is set then:
     2.a. Set RuntimeCachePendingUpdate flag
     2.b. Update RuntimeCachePendingOffset and RuntimeCachePendingLength to
          cover the a superset of the pending chunk (for simplicity, the
          entire variable store is currently synchronized).
3. Else:
     3.a. Update RuntimeCache
4. Update SmmCache
     - Note: RT read cannot occur during SMI processing since all cores are
             locked in SMM.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>

Michael Kubacki (9):
  MdeModulePkg/Variable: Consolidate common parsing functions
  MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list
  MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  MdeModulePkg/Variable: Add local auth status in VariableParsing
  MdeModulePkg/Variable: Add a file for NV variable functions
  MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats
  MdeModulePkg/Variable: Add RT GetVariable() cache support
  MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  MdeModulePkg/VariableSmm: Remove unused SMI handler functions

 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf    |   6 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf           |   6 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf |  31 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf  |  11 +
 MdeModulePkg/Include/Guid/SmmVariableCommon.h                        |  33 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h                | 158 +---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h     |  25 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h         | 325 ++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h    |  47 ++
 MdeModulePkg/Application/VariableInfo/VariableInfo.c                 |  37 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c                | 826 ++------------------
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c           |  11 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c     |  28 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c         | 769 ++++++++++++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c    | 153 ++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c             | 212 +++--
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c   | 726 +++++++++++++----
 17 files changed, 2231 insertions(+), 1173 deletions(-)
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
 create mode 100644 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c

-- 
2.16.2.windows.1


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

* [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:03   ` Wu, Hao A
  2019-10-08  6:07   ` Wang, Jian J
  2019-09-28  1:47 ` [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list Kubacki, Michael A
                   ` (7 subsequent siblings)
  8 siblings, 2 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

This change moves the following functions into a dedicated file
so they may be used in other variable files as needed. Furthermore,
it reduces the overall size of the common Variable.c file.

 * DataSizeOfVariable ()
 * FindVariableEx ()
 * GetEndPointer ()
 * GetNextVariablePtr ()
 * GetStartPointer ()
 * GetVariableDataOffset ()
 * GetVariableDataPtr ()
 * GetVariableHeaderSize ()
 * GetVariableNamePtr ()
 * GetVariableStoreStatus ()
 * GetVendorGuidPtr ()
 * IsValidVariableHeader ()
 * NameSizeOfVariable ()
 * SetDataSizeOfVariable ()
 * SetNameSizeOfVariable ()
 * UpdateVariableInfo ()
 * VariableCompareTimeStampInternal ()
 * VariableServiceGetNextVariableInternal ()

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf   |   2 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |   2 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf |   7 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119 ----
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        | 306 ++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726 +------------------
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |   3 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        | 731 ++++++++++++++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |   1 +
 9 files changed, 1052 insertions(+), 845 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
index 641376c9c5..c35e5fe787 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
@@ -36,6 +36,8 @@
   Variable.c
   VariableDxe.c
   Variable.h
+  VariableParsing.c
+  VariableParsing.h
   PrivilegePolymorphic.h
   Measurement.c
   TcgMorLockDxe.c
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
index 0a160d269d..626738b9c7 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
@@ -45,6 +45,8 @@
   Variable.c
   VariableTraditionalMm.c
   VariableSmm.c
+  VariableParsing.c
+  VariableParsing.h
   VarCheck.c
   Variable.h
   PrivilegePolymorphic.h
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
index 21bc81163b..1ba8f9ebfb 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
@@ -45,6 +45,8 @@
   Variable.c
   VariableSmm.c
   VariableStandaloneMm.c
+  VariableParsing.c
+  VariableParsing.h
   VarCheck.c
   Variable.h
   PrivilegePolymorphic.h
@@ -99,6 +101,11 @@
   ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
   gEfiGlobalVariableGuid
 
+  ## SOMETIMES_CONSUMES   ## Variable:L"db"
+  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
+  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
+  gEfiImageSecurityDatabaseGuid
+
   gEfiMemoryOverwriteControlDataGuid            ## SOMETIMES_CONSUMES   ## Variable:L"MemoryOverwriteRequestControl"
   gEfiMemoryOverwriteRequestControlLockGuid     ## SOMETIMES_PRODUCES   ## Variable:L"MemoryOverwriteRequestControlLock"
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
index 9eac43759f..fb574b2e32 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
@@ -179,89 +179,6 @@ FindVariable (
   IN  BOOLEAN                 IgnoreRtCheck
   );
 
-/**
-
-  Gets the pointer to the end of the variable storage area.
-
-  This function gets pointer to the end of the variable storage
-  area, according to the input variable store header.
-
-  @param VarStoreHeader  Pointer to the Variable Store Header.
-
-  @return Pointer to the end of the variable storage area.
-
-**/
-VARIABLE_HEADER *
-GetEndPointer (
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader
-  );
-
-/**
-  This code gets the size of variable header.
-
-  @return Size of variable header in bytes in type UINTN.
-
-**/
-UINTN
-GetVariableHeaderSize (
-  VOID
-  );
-
-/**
-
-  This code gets the pointer to the variable name.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Pointer to Variable Name which is Unicode encoding.
-
-**/
-CHAR16 *
-GetVariableNamePtr (
-  IN  VARIABLE_HEADER   *Variable
-  );
-
-/**
-  This code gets the pointer to the variable guid.
-
-  @param Variable   Pointer to the Variable Header.
-
-  @return A EFI_GUID* pointer to Vendor Guid.
-
-**/
-EFI_GUID *
-GetVendorGuidPtr (
-  IN VARIABLE_HEADER    *Variable
-  );
-
-/**
-
-  This code gets the pointer to the variable data.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Pointer to Variable Data.
-
-**/
-UINT8 *
-GetVariableDataPtr (
-  IN  VARIABLE_HEADER   *Variable
-  );
-
-/**
-
-  This code gets the size of variable data.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Size of variable in bytes.
-
-**/
-UINTN
-DataSizeOfVariable (
-  IN  VARIABLE_HEADER   *Variable
-  );
-
 /**
   This function is to check if the remaining variable space is enough to set
   all Variables from argument list successfully. The purpose of the check
@@ -450,17 +367,6 @@ ReclaimForOS(
   VOID
   );
 
-/**
-  Get non-volatile maximum variable size.
-
-  @return Non-volatile maximum variable size.
-
-**/
-UINTN
-GetNonVolatileMaxVariableSize (
-  VOID
-  );
-
 /**
   Get maximum variable size, covering both non-volatile and volatile variables.
 
@@ -546,31 +452,6 @@ VariableServiceGetVariable (
   OUT     VOID              *Data OPTIONAL
   );
 
-/**
-  This code Finds the Next available variable.
-
-  Caution: This function may receive untrusted input.
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
-
-  @param[in] VariableName   Pointer to variable name.
-  @param[in] VendorGuid     Variable Vendor Guid.
-  @param[out] VariablePtr   Pointer to variable header address.
-
-  @retval EFI_SUCCESS           The function completed successfully.
-  @retval EFI_NOT_FOUND         The next variable was not found.
-  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
-  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
-                                GUID of an existing variable.
-
-**/
-EFI_STATUS
-EFIAPI
-VariableServiceGetNextVariableInternal (
-  IN  CHAR16                *VariableName,
-  IN  EFI_GUID              *VendorGuid,
-  OUT VARIABLE_HEADER       **VariablePtr
-  );
-
 /**
 
   This code Finds the Next available variable.
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
new file mode 100644
index 0000000000..9d77c4916c
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
@@ -0,0 +1,306 @@
+/** @file
+  Functions in this module are associated with variable parsing operations and
+  are intended to be usable across variable driver source files.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _VARIABLE_PARSING_H_
+#define _VARIABLE_PARSING_H_
+
+#include <Guid/ImageAuthentication.h>
+#include "Variable.h"
+
+/**
+
+  This code checks if variable header is valid or not.
+
+  @param Variable           Pointer to the Variable Header.
+  @param VariableStoreEnd   Pointer to the Variable Store End.
+
+  @retval TRUE              Variable header is valid.
+  @retval FALSE             Variable header is not valid.
+
+**/
+BOOLEAN
+IsValidVariableHeader (
+  IN  VARIABLE_HEADER       *Variable,
+  IN  VARIABLE_HEADER       *VariableStoreEnd
+  );
+
+/**
+
+  This code gets the current status of Variable Store.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @retval EfiRaw         Variable store status is raw.
+  @retval EfiValid       Variable store status is valid.
+  @retval EfiInvalid     Variable store status is invalid.
+
+**/
+VARIABLE_STORE_STATUS
+GetVariableStoreStatus (
+  IN VARIABLE_STORE_HEADER *VarStoreHeader
+  );
+
+/**
+  This code gets the size of variable header.
+
+  @return Size of variable header in bytes in type UINTN.
+
+**/
+UINTN
+GetVariableHeaderSize (
+  VOID
+  );
+
+/**
+
+  This code gets the size of name of variable.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return UINTN          Size of variable in bytes.
+
+**/
+UINTN
+NameSizeOfVariable (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+  This code sets the size of name of variable.
+
+  @param[in] Variable   Pointer to the Variable Header.
+  @param[in] NameSize   Name size to set.
+
+**/
+VOID
+SetNameSizeOfVariable (
+  IN VARIABLE_HEADER    *Variable,
+  IN UINTN              NameSize
+  );
+
+/**
+
+  This code gets the size of variable data.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Size of variable in bytes.
+
+**/
+UINTN
+DataSizeOfVariable (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+  This code sets the size of variable data.
+
+  @param[in] Variable   Pointer to the Variable Header.
+  @param[in] DataSize   Data size to set.
+
+**/
+VOID
+SetDataSizeOfVariable (
+  IN VARIABLE_HEADER    *Variable,
+  IN UINTN              DataSize
+  );
+
+/**
+
+  This code gets the pointer to the variable name.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to Variable Name which is Unicode encoding.
+
+**/
+CHAR16 *
+GetVariableNamePtr (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+  This code gets the pointer to the variable guid.
+
+  @param Variable   Pointer to the Variable Header.
+
+  @return A EFI_GUID* pointer to Vendor Guid.
+
+**/
+EFI_GUID *
+GetVendorGuidPtr (
+  IN VARIABLE_HEADER    *Variable
+  );
+
+/**
+
+  This code gets the pointer to the variable data.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to Variable Data.
+
+**/
+UINT8 *
+GetVariableDataPtr (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+  This code gets the variable data offset related to variable header.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Variable Data offset.
+
+**/
+UINTN
+GetVariableDataOffset (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+
+  This code gets the pointer to the next variable header.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to next variable header.
+
+**/
+VARIABLE_HEADER *
+GetNextVariablePtr (
+  IN  VARIABLE_HEADER   *Variable
+  );
+
+/**
+
+  Gets the pointer to the first variable header in given variable store area.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @return Pointer to the first variable header.
+
+**/
+VARIABLE_HEADER *
+GetStartPointer (
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader
+  );
+
+/**
+
+  Gets the pointer to the end of the variable storage area.
+
+  This function gets pointer to the end of the variable storage
+  area, according to the input variable store header.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @return Pointer to the end of the variable storage area.
+
+**/
+VARIABLE_HEADER *
+GetEndPointer (
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader
+  );
+
+/**
+  Compare two EFI_TIME data.
+
+
+  @param FirstTime           A pointer to the first EFI_TIME data.
+  @param SecondTime          A pointer to the second EFI_TIME data.
+
+  @retval  TRUE              The FirstTime is not later than the SecondTime.
+  @retval  FALSE             The FirstTime is later than the SecondTime.
+
+**/
+BOOLEAN
+VariableCompareTimeStampInternal (
+  IN EFI_TIME               *FirstTime,
+  IN EFI_TIME               *SecondTime
+  );
+
+/**
+  Find the variable in the specified variable store.
+
+  @param[in]       VariableName        Name of the variable to be found
+  @param[in]       VendorGuid          Vendor GUID to be found.
+  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
+                                       check at runtime when searching variable.
+  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains Variable Information.
+
+  @retval          EFI_SUCCESS         Variable found successfully
+  @retval          EFI_NOT_FOUND       Variable not found
+**/
+EFI_STATUS
+FindVariableEx (
+  IN     CHAR16                  *VariableName,
+  IN     EFI_GUID                *VendorGuid,
+  IN     BOOLEAN                 IgnoreRtCheck,
+  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
+  );
+
+/**
+  This code Finds the Next available variable.
+
+  Caution: This function may receive untrusted input.
+  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
+
+  @param[in]  VariableName  Pointer to variable name.
+  @param[in]  VendorGuid    Variable Vendor Guid.
+  @param[out] VariablePtr   Pointer to variable header address.
+
+  @retval EFI_SUCCESS           The function completed successfully.
+  @retval EFI_NOT_FOUND         The next variable was not found.
+  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
+  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
+                                GUID of an existing variable.
+
+**/
+EFI_STATUS
+EFIAPI
+VariableServiceGetNextVariableInternal (
+  IN  CHAR16                *VariableName,
+  IN  EFI_GUID              *VendorGuid,
+  OUT VARIABLE_HEADER       **VariablePtr
+  );
+
+/**
+  Routine used to track statistical information about variable usage.
+  The data is stored in the EFI system table so it can be accessed later.
+  VariableInfo.efi can dump out the table. Only Boot Services variable
+  accesses are tracked by this code. The PcdVariableCollectStatistics
+  build flag controls if this feature is enabled.
+
+  A read that hits in the cache will have Read and Cache true for
+  the transaction. Data is allocated by this routine, but never
+  freed.
+
+  @param[in] VariableName   Name of the Variable to track.
+  @param[in] VendorGuid     Guid of the Variable to track.
+  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
+  @param[in] Read           TRUE if GetVariable() was called.
+  @param[in] Write          TRUE if SetVariable() was called.
+  @param[in] Delete         TRUE if deleted via SetVariable().
+  @param[in] Cache          TRUE for a cache hit.
+
+**/
+VOID
+UpdateVariableInfo (
+  IN  CHAR16                  *VariableName,
+  IN  EFI_GUID                *VendorGuid,
+  IN  BOOLEAN                 Volatile,
+  IN  BOOLEAN                 Read,
+  IN  BOOLEAN                 Write,
+  IN  BOOLEAN                 Delete,
+  IN  BOOLEAN                 Cache
+  );
+
+#endif
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index f32c9c2808..76536308e6 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 **/
 
 #include "Variable.h"
+#include "VariableParsing.h"
 
 VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
 
@@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
 
 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
 
-/**
-  Routine used to track statistical information about variable usage.
-  The data is stored in the EFI system table so it can be accessed later.
-  VariableInfo.efi can dump out the table. Only Boot Services variable
-  accesses are tracked by this code. The PcdVariableCollectStatistics
-  build flag controls if this feature is enabled.
-
-  A read that hits in the cache will have Read and Cache true for
-  the transaction. Data is allocated by this routine, but never
-  freed.
-
-  @param[in] VariableName   Name of the Variable to track.
-  @param[in] VendorGuid     Guid of the Variable to track.
-  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
-  @param[in] Read           TRUE if GetVariable() was called.
-  @param[in] Write          TRUE if SetVariable() was called.
-  @param[in] Delete         TRUE if deleted via SetVariable().
-  @param[in] Cache          TRUE for a cache hit.
-
-**/
-VOID
-UpdateVariableInfo (
-  IN  CHAR16                  *VariableName,
-  IN  EFI_GUID                *VendorGuid,
-  IN  BOOLEAN                 Volatile,
-  IN  BOOLEAN                 Read,
-  IN  BOOLEAN                 Write,
-  IN  BOOLEAN                 Delete,
-  IN  BOOLEAN                 Cache
-  )
-{
-  VARIABLE_INFO_ENTRY   *Entry;
-
-  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
-
-    if (AtRuntime ()) {
-      // Don't collect statistics at runtime.
-      return;
-    }
-
-    if (gVariableInfo == NULL) {
-      //
-      // On the first call allocate a entry and place a pointer to it in
-      // the EFI System Table.
-      //
-      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
-      ASSERT (gVariableInfo != NULL);
-
-      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
-      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
-      ASSERT (gVariableInfo->Name != NULL);
-      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
-      gVariableInfo->Volatile = Volatile;
-    }
-
-
-    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
-      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
-        if (StrCmp (VariableName, Entry->Name) == 0) {
-          if (Read) {
-            Entry->ReadCount++;
-          }
-          if (Write) {
-            Entry->WriteCount++;
-          }
-          if (Delete) {
-            Entry->DeleteCount++;
-          }
-          if (Cache) {
-            Entry->CacheCount++;
-          }
-
-          return;
-        }
-      }
-
-      if (Entry->Next == NULL) {
-        //
-        // If the entry is not in the table add it.
-        // Next iteration of the loop will fill in the data.
-        //
-        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
-        ASSERT (Entry->Next != NULL);
-
-        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
-        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
-        ASSERT (Entry->Next->Name != NULL);
-        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
-        Entry->Next->Volatile = Volatile;
-      }
-
-    }
-  }
-}
-
-
-/**
-
-  This code checks if variable header is valid or not.
-
-  @param Variable           Pointer to the Variable Header.
-  @param VariableStoreEnd   Pointer to the Variable Store End.
-
-  @retval TRUE              Variable header is valid.
-  @retval FALSE             Variable header is not valid.
-
-**/
-BOOLEAN
-IsValidVariableHeader (
-  IN  VARIABLE_HEADER       *Variable,
-  IN  VARIABLE_HEADER       *VariableStoreEnd
-  )
-{
-  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {
-    //
-    // Variable is NULL or has reached the end of variable store,
-    // or the StartId is not correct.
-    //
-    return FALSE;
-  }
-
-  return TRUE;
-}
-
-
 /**
 
   This function writes data to the FWH at the correct LBA even if the LBAs
@@ -376,345 +252,6 @@ UpdateVariableStore (
   return EFI_SUCCESS;
 }
 
-
-/**
-
-  This code gets the current status of Variable Store.
-
-  @param VarStoreHeader  Pointer to the Variable Store Header.
-
-  @retval EfiRaw         Variable store status is raw.
-  @retval EfiValid       Variable store status is valid.
-  @retval EfiInvalid     Variable store status is invalid.
-
-**/
-VARIABLE_STORE_STATUS
-GetVariableStoreStatus (
-  IN VARIABLE_STORE_HEADER *VarStoreHeader
-  )
-{
-  if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||
-       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
-      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
-      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
-      ) {
-
-    return EfiValid;
-  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
-             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
-             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
-             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
-             VarStoreHeader->Size == 0xffffffff &&
-             VarStoreHeader->Format == 0xff &&
-             VarStoreHeader->State == 0xff
-          ) {
-
-    return EfiRaw;
-  } else {
-    return EfiInvalid;
-  }
-}
-
-/**
-  This code gets the size of variable header.
-
-  @return Size of variable header in bytes in type UINTN.
-
-**/
-UINTN
-GetVariableHeaderSize (
-  VOID
-  )
-{
-  UINTN Value;
-
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
-  } else {
-    Value = sizeof (VARIABLE_HEADER);
-  }
-
-  return Value;
-}
-
-/**
-
-  This code gets the size of name of variable.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return UINTN          Size of variable in bytes.
-
-**/
-UINTN
-NameSizeOfVariable (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
-
-  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    if (AuthVariable->State == (UINT8) (-1) ||
-       AuthVariable->DataSize == (UINT32) (-1) ||
-       AuthVariable->NameSize == (UINT32) (-1) ||
-       AuthVariable->Attributes == (UINT32) (-1)) {
-      return 0;
-    }
-    return (UINTN) AuthVariable->NameSize;
-  } else {
-    if (Variable->State == (UINT8) (-1) ||
-        Variable->DataSize == (UINT32) (-1) ||
-        Variable->NameSize == (UINT32) (-1) ||
-        Variable->Attributes == (UINT32) (-1)) {
-      return 0;
-    }
-    return (UINTN) Variable->NameSize;
-  }
-}
-
-/**
-  This code sets the size of name of variable.
-
-  @param[in] Variable   Pointer to the Variable Header.
-  @param[in] NameSize   Name size to set.
-
-**/
-VOID
-SetNameSizeOfVariable (
-  IN VARIABLE_HEADER    *Variable,
-  IN UINTN              NameSize
-  )
-{
-  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
-
-  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    AuthVariable->NameSize = (UINT32) NameSize;
-  } else {
-    Variable->NameSize = (UINT32) NameSize;
-  }
-}
-
-/**
-
-  This code gets the size of variable data.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Size of variable in bytes.
-
-**/
-UINTN
-DataSizeOfVariable (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
-
-  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    if (AuthVariable->State == (UINT8) (-1) ||
-       AuthVariable->DataSize == (UINT32) (-1) ||
-       AuthVariable->NameSize == (UINT32) (-1) ||
-       AuthVariable->Attributes == (UINT32) (-1)) {
-      return 0;
-    }
-    return (UINTN) AuthVariable->DataSize;
-  } else {
-    if (Variable->State == (UINT8) (-1) ||
-        Variable->DataSize == (UINT32) (-1) ||
-        Variable->NameSize == (UINT32) (-1) ||
-        Variable->Attributes == (UINT32) (-1)) {
-      return 0;
-    }
-    return (UINTN) Variable->DataSize;
-  }
-}
-
-/**
-  This code sets the size of variable data.
-
-  @param[in] Variable   Pointer to the Variable Header.
-  @param[in] DataSize   Data size to set.
-
-**/
-VOID
-SetDataSizeOfVariable (
-  IN VARIABLE_HEADER    *Variable,
-  IN UINTN              DataSize
-  )
-{
-  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
-
-  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    AuthVariable->DataSize = (UINT32) DataSize;
-  } else {
-    Variable->DataSize = (UINT32) DataSize;
-  }
-}
-
-/**
-
-  This code gets the pointer to the variable name.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Pointer to Variable Name which is Unicode encoding.
-
-**/
-CHAR16 *
-GetVariableNamePtr (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
-}
-
-/**
-  This code gets the pointer to the variable guid.
-
-  @param Variable   Pointer to the Variable Header.
-
-  @return A EFI_GUID* pointer to Vendor Guid.
-
-**/
-EFI_GUID *
-GetVendorGuidPtr (
-  IN VARIABLE_HEADER    *Variable
-  )
-{
-  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
-
-  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
-    return &AuthVariable->VendorGuid;
-  } else {
-    return &Variable->VendorGuid;
-  }
-}
-
-/**
-
-  This code gets the pointer to the variable data.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Pointer to Variable Data.
-
-**/
-UINT8 *
-GetVariableDataPtr (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  UINTN Value;
-
-  //
-  // Be careful about pad size for alignment.
-  //
-  Value =  (UINTN) GetVariableNamePtr (Variable);
-  Value += NameSizeOfVariable (Variable);
-  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
-
-  return (UINT8 *) Value;
-}
-
-/**
-  This code gets the variable data offset related to variable header.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Variable Data offset.
-
-**/
-UINTN
-GetVariableDataOffset (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  UINTN Value;
-
-  //
-  // Be careful about pad size for alignment
-  //
-  Value = GetVariableHeaderSize ();
-  Value += NameSizeOfVariable (Variable);
-  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
-
-  return Value;
-}
-
-/**
-
-  This code gets the pointer to the next variable header.
-
-  @param Variable        Pointer to the Variable Header.
-
-  @return Pointer to next variable header.
-
-**/
-VARIABLE_HEADER *
-GetNextVariablePtr (
-  IN  VARIABLE_HEADER   *Variable
-  )
-{
-  UINTN Value;
-
-  Value =  (UINTN) GetVariableDataPtr (Variable);
-  Value += DataSizeOfVariable (Variable);
-  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
-
-  //
-  // Be careful about pad size for alignment.
-  //
-  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
-}
-
-/**
-
-  Gets the pointer to the first variable header in given variable store area.
-
-  @param VarStoreHeader  Pointer to the Variable Store Header.
-
-  @return Pointer to the first variable header.
-
-**/
-VARIABLE_HEADER *
-GetStartPointer (
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader
-  )
-{
-  //
-  // The start of variable store.
-  //
-  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
-}
-
-/**
-
-  Gets the pointer to the end of the variable storage area.
-
-  This function gets pointer to the end of the variable storage
-  area, according to the input variable store header.
-
-  @param VarStoreHeader  Pointer to the Variable Store Header.
-
-  @return Pointer to the end of the variable storage area.
-
-**/
-VARIABLE_HEADER *
-GetEndPointer (
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader
-  )
-{
-  //
-  // The end of variable store
-  //
-  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
-}
-
 /**
   Record variable error flag.
 
@@ -1228,75 +765,6 @@ Done:
   return Status;
 }
 
-/**
-  Find the variable in the specified variable store.
-
-  @param[in]       VariableName        Name of the variable to be found
-  @param[in]       VendorGuid          Vendor GUID to be found.
-  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
-                                       check at runtime when searching variable.
-  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains Variable Information.
-
-  @retval          EFI_SUCCESS         Variable found successfully
-  @retval          EFI_NOT_FOUND       Variable not found
-**/
-EFI_STATUS
-FindVariableEx (
-  IN     CHAR16                  *VariableName,
-  IN     EFI_GUID                *VendorGuid,
-  IN     BOOLEAN                 IgnoreRtCheck,
-  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
-  )
-{
-  VARIABLE_HEADER                *InDeletedVariable;
-  VOID                           *Point;
-
-  PtrTrack->InDeletedTransitionPtr = NULL;
-
-  //
-  // Find the variable by walk through HOB, volatile and non-volatile variable store.
-  //
-  InDeletedVariable  = NULL;
-
-  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
-      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
-      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
-      ) {
-    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
-        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
-       ) {
-      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
-        if (VariableName[0] == 0) {
-          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
-            InDeletedVariable   = PtrTrack->CurrPtr;
-          } else {
-            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
-            return EFI_SUCCESS;
-          }
-        } else {
-          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {
-            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
-
-            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
-            if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {
-              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
-                InDeletedVariable     = PtrTrack->CurrPtr;
-              } else {
-                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
-                return EFI_SUCCESS;
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
-  PtrTrack->CurrPtr = InDeletedVariable;
-  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
-}
-
-
 /**
   Finds variable in storage blocks of volatile and non-volatile storage areas.
 
@@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
   }
 }
 
-/**
-  Compare two EFI_TIME data.
-
-
-  @param FirstTime           A pointer to the first EFI_TIME data.
-  @param SecondTime          A pointer to the second EFI_TIME data.
-
-  @retval  TRUE              The FirstTime is not later than the SecondTime.
-  @retval  FALSE             The FirstTime is later than the SecondTime.
-
-**/
-BOOLEAN
-VariableCompareTimeStampInternal (
-  IN EFI_TIME               *FirstTime,
-  IN EFI_TIME               *SecondTime
-  )
-{
-  if (FirstTime->Year != SecondTime->Year) {
-    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
-  } else if (FirstTime->Month != SecondTime->Month) {
-    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
-  } else if (FirstTime->Day != SecondTime->Day) {
-    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
-  } else if (FirstTime->Hour != SecondTime->Hour) {
-    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
-  } else if (FirstTime->Minute != SecondTime->Minute) {
-    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
-  }
-
-  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
-}
-
 /**
   Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
   index of associated public key is needed.
@@ -2885,166 +2321,6 @@ Done:
   return Status;
 }
 
-/**
-  This code Finds the Next available variable.
-
-  Caution: This function may receive untrusted input.
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
-
-  @param[in]  VariableName  Pointer to variable name.
-  @param[in]  VendorGuid    Variable Vendor Guid.
-  @param[out] VariablePtr   Pointer to variable header address.
-
-  @retval EFI_SUCCESS           The function completed successfully.
-  @retval EFI_NOT_FOUND         The next variable was not found.
-  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
-  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
-                                GUID of an existing variable.
-
-**/
-EFI_STATUS
-EFIAPI
-VariableServiceGetNextVariableInternal (
-  IN  CHAR16                *VariableName,
-  IN  EFI_GUID              *VendorGuid,
-  OUT VARIABLE_HEADER       **VariablePtr
-  )
-{
-  VARIABLE_STORE_TYPE     Type;
-  VARIABLE_POINTER_TRACK  Variable;
-  VARIABLE_POINTER_TRACK  VariableInHob;
-  VARIABLE_POINTER_TRACK  VariablePtrTrack;
-  EFI_STATUS              Status;
-  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
-
-  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
-  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
-    //
-    // For VariableName is an empty string, FindVariable() will try to find and return
-    // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)
-    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
-    //
-    if (VariableName[0] != 0) {
-      //
-      // For VariableName is not an empty string, and FindVariable() returns error as
-      // VariableName and VendorGuid are not a name and GUID of an existing variable,
-      // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
-      //
-      Status = EFI_INVALID_PARAMETER;
-    }
-    goto Done;
-  }
-
-  if (VariableName[0] != 0) {
-    //
-    // If variable name is not NULL, get next variable.
-    //
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
-  }
-
-  //
-  // 0: Volatile, 1: HOB, 2: Non-Volatile.
-  // The index and attributes mapping must be kept in this order as FindVariable
-  // makes use of this mapping to implement search algorithm.
-  //
-  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
-  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
-  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
-
-  while (TRUE) {
-    //
-    // Switch from Volatile to HOB, to Non-Volatile.
-    //
-    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
-      //
-      // Find current storage index
-      //
-      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
-        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
-          break;
-        }
-      }
-      ASSERT (Type < VariableStoreTypeMax);
-      //
-      // Switch to next storage
-      //
-      for (Type++; Type < VariableStoreTypeMax; Type++) {
-        if (VariableStoreHeader[Type] != NULL) {
-          break;
-        }
-      }
-      //
-      // Capture the case that
-      // 1. current storage is the last one, or
-      // 2. no further storage
-      //
-      if (Type == VariableStoreTypeMax) {
-        Status = EFI_NOT_FOUND;
-        goto Done;
-      }
-      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
-      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
-      Variable.CurrPtr  = Variable.StartPtr;
-    }
-
-    //
-    // Variable is found
-    //
-    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
-      if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
-        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
-          //
-          // If it is a IN_DELETED_TRANSITION variable,
-          // and there is also a same ADDED one at the same time,
-          // don't return it.
-          //
-          VariablePtrTrack.StartPtr = Variable.StartPtr;
-          VariablePtrTrack.EndPtr = Variable.EndPtr;
-          Status = FindVariableEx (
-                     GetVariableNamePtr (Variable.CurrPtr),
-                     GetVendorGuidPtr (Variable.CurrPtr),
-                     FALSE,
-                     &VariablePtrTrack
-                     );
-          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {
-            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
-            continue;
-          }
-        }
-
-        //
-        // Don't return NV variable when HOB overrides it
-        //
-        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
-            (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
-           ) {
-          VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
-          VariableInHob.EndPtr   = GetEndPointer   (VariableStoreHeader[VariableStoreTypeHob]);
-          Status = FindVariableEx (
-                     GetVariableNamePtr (Variable.CurrPtr),
-                     GetVendorGuidPtr (Variable.CurrPtr),
-                     FALSE,
-                     &VariableInHob
-                     );
-          if (!EFI_ERROR (Status)) {
-            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
-            continue;
-          }
-        }
-
-        *VariablePtr = Variable.CurrPtr;
-        Status = EFI_SUCCESS;
-        goto Done;
-      }
-    }
-
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
-  }
-
-Done:
-  return Status;
-}
-
 /**
 
   This code Finds the Next available variable.
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
index cb6fcebe2d..dc78f68fa9 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
@@ -1,12 +1,13 @@
 /** @file
   Provides variable driver extended services.
 
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include "Variable.h"
+#include "VariableParsing.h"
 
 /**
   Finds variable in storage blocks of volatile and non-volatile storage areas.
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
new file mode 100644
index 0000000000..7de0a90772
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
@@ -0,0 +1,731 @@
+/** @file
+  Functions in this module are associated with variable parsing operations and
+  are intended to be usable across variable driver source files.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "VariableParsing.h"
+
+/**
+
+  This code checks if variable header is valid or not.
+
+  @param Variable           Pointer to the Variable Header.
+  @param VariableStoreEnd   Pointer to the Variable Store End.
+
+  @retval TRUE              Variable header is valid.
+  @retval FALSE             Variable header is not valid.
+
+**/
+BOOLEAN
+IsValidVariableHeader (
+  IN  VARIABLE_HEADER       *Variable,
+  IN  VARIABLE_HEADER       *VariableStoreEnd
+  )
+{
+  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {
+    //
+    // Variable is NULL or has reached the end of variable store,
+    // or the StartId is not correct.
+    //
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+/**
+
+  This code gets the current status of Variable Store.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @retval EfiRaw         Variable store status is raw.
+  @retval EfiValid       Variable store status is valid.
+  @retval EfiInvalid     Variable store status is invalid.
+
+**/
+VARIABLE_STORE_STATUS
+GetVariableStoreStatus (
+  IN VARIABLE_STORE_HEADER *VarStoreHeader
+  )
+{
+  if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||
+       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
+      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
+      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
+      ) {
+
+    return EfiValid;
+  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
+             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
+             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
+             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
+             VarStoreHeader->Size == 0xffffffff &&
+             VarStoreHeader->Format == 0xff &&
+             VarStoreHeader->State == 0xff
+          ) {
+
+    return EfiRaw;
+  } else {
+    return EfiInvalid;
+  }
+}
+
+/**
+  This code gets the size of variable header.
+
+  @return Size of variable header in bytes in type UINTN.
+
+**/
+UINTN
+GetVariableHeaderSize (
+  VOID
+  )
+{
+  UINTN Value;
+
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
+  } else {
+    Value = sizeof (VARIABLE_HEADER);
+  }
+
+  return Value;
+}
+
+/**
+
+  This code gets the size of name of variable.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return UINTN          Size of variable in bytes.
+
+**/
+UINTN
+NameSizeOfVariable (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
+
+  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    if (AuthVariable->State == (UINT8) (-1) ||
+       AuthVariable->DataSize == (UINT32) (-1) ||
+       AuthVariable->NameSize == (UINT32) (-1) ||
+       AuthVariable->Attributes == (UINT32) (-1)) {
+      return 0;
+    }
+    return (UINTN) AuthVariable->NameSize;
+  } else {
+    if (Variable->State == (UINT8) (-1) ||
+        Variable->DataSize == (UINT32) (-1) ||
+        Variable->NameSize == (UINT32) (-1) ||
+        Variable->Attributes == (UINT32) (-1)) {
+      return 0;
+    }
+    return (UINTN) Variable->NameSize;
+  }
+}
+
+/**
+  This code sets the size of name of variable.
+
+  @param[in] Variable   Pointer to the Variable Header.
+  @param[in] NameSize   Name size to set.
+
+**/
+VOID
+SetNameSizeOfVariable (
+  IN VARIABLE_HEADER    *Variable,
+  IN UINTN              NameSize
+  )
+{
+  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
+
+  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    AuthVariable->NameSize = (UINT32) NameSize;
+  } else {
+    Variable->NameSize = (UINT32) NameSize;
+  }
+}
+
+/**
+
+  This code gets the size of variable data.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Size of variable in bytes.
+
+**/
+UINTN
+DataSizeOfVariable (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
+
+  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    if (AuthVariable->State == (UINT8) (-1) ||
+       AuthVariable->DataSize == (UINT32) (-1) ||
+       AuthVariable->NameSize == (UINT32) (-1) ||
+       AuthVariable->Attributes == (UINT32) (-1)) {
+      return 0;
+    }
+    return (UINTN) AuthVariable->DataSize;
+  } else {
+    if (Variable->State == (UINT8) (-1) ||
+        Variable->DataSize == (UINT32) (-1) ||
+        Variable->NameSize == (UINT32) (-1) ||
+        Variable->Attributes == (UINT32) (-1)) {
+      return 0;
+    }
+    return (UINTN) Variable->DataSize;
+  }
+}
+
+/**
+  This code sets the size of variable data.
+
+  @param[in] Variable   Pointer to the Variable Header.
+  @param[in] DataSize   Data size to set.
+
+**/
+VOID
+SetDataSizeOfVariable (
+  IN VARIABLE_HEADER    *Variable,
+  IN UINTN              DataSize
+  )
+{
+  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
+
+  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    AuthVariable->DataSize = (UINT32) DataSize;
+  } else {
+    Variable->DataSize = (UINT32) DataSize;
+  }
+}
+
+/**
+
+  This code gets the pointer to the variable name.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to Variable Name which is Unicode encoding.
+
+**/
+CHAR16 *
+GetVariableNamePtr (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
+}
+
+/**
+  This code gets the pointer to the variable guid.
+
+  @param Variable   Pointer to the Variable Header.
+
+  @return A EFI_GUID* pointer to Vendor Guid.
+
+**/
+EFI_GUID *
+GetVendorGuidPtr (
+  IN VARIABLE_HEADER    *Variable
+  )
+{
+  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
+
+  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
+  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+    return &AuthVariable->VendorGuid;
+  } else {
+    return &Variable->VendorGuid;
+  }
+}
+
+/**
+
+  This code gets the pointer to the variable data.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to Variable Data.
+
+**/
+UINT8 *
+GetVariableDataPtr (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  UINTN Value;
+
+  //
+  // Be careful about pad size for alignment.
+  //
+  Value =  (UINTN) GetVariableNamePtr (Variable);
+  Value += NameSizeOfVariable (Variable);
+  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
+
+  return (UINT8 *) Value;
+}
+
+/**
+  This code gets the variable data offset related to variable header.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Variable Data offset.
+
+**/
+UINTN
+GetVariableDataOffset (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  UINTN Value;
+
+  //
+  // Be careful about pad size for alignment
+  //
+  Value = GetVariableHeaderSize ();
+  Value += NameSizeOfVariable (Variable);
+  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
+
+  return Value;
+}
+
+/**
+
+  This code gets the pointer to the next variable header.
+
+  @param Variable        Pointer to the Variable Header.
+
+  @return Pointer to next variable header.
+
+**/
+VARIABLE_HEADER *
+GetNextVariablePtr (
+  IN  VARIABLE_HEADER   *Variable
+  )
+{
+  UINTN Value;
+
+  Value =  (UINTN) GetVariableDataPtr (Variable);
+  Value += DataSizeOfVariable (Variable);
+  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
+
+  //
+  // Be careful about pad size for alignment.
+  //
+  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
+}
+
+/**
+
+  Gets the pointer to the first variable header in given variable store area.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @return Pointer to the first variable header.
+
+**/
+VARIABLE_HEADER *
+GetStartPointer (
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader
+  )
+{
+  //
+  // The start of variable store.
+  //
+  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
+}
+
+/**
+
+  Gets the pointer to the end of the variable storage area.
+
+  This function gets pointer to the end of the variable storage
+  area, according to the input variable store header.
+
+  @param VarStoreHeader  Pointer to the Variable Store Header.
+
+  @return Pointer to the end of the variable storage area.
+
+**/
+VARIABLE_HEADER *
+GetEndPointer (
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader
+  )
+{
+  //
+  // The end of variable store
+  //
+  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);
+}
+
+/**
+  Compare two EFI_TIME data.
+
+
+  @param FirstTime           A pointer to the first EFI_TIME data.
+  @param SecondTime          A pointer to the second EFI_TIME data.
+
+  @retval  TRUE              The FirstTime is not later than the SecondTime.
+  @retval  FALSE             The FirstTime is later than the SecondTime.
+
+**/
+BOOLEAN
+VariableCompareTimeStampInternal (
+  IN EFI_TIME               *FirstTime,
+  IN EFI_TIME               *SecondTime
+  )
+{
+  if (FirstTime->Year != SecondTime->Year) {
+    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
+  } else if (FirstTime->Month != SecondTime->Month) {
+    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
+  } else if (FirstTime->Day != SecondTime->Day) {
+    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
+  } else if (FirstTime->Hour != SecondTime->Hour) {
+    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
+  } else if (FirstTime->Minute != SecondTime->Minute) {
+    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
+  }
+
+  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
+}
+
+/**
+  Find the variable in the specified variable store.
+
+  @param[in]       VariableName        Name of the variable to be found
+  @param[in]       VendorGuid          Vendor GUID to be found.
+  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
+                                       check at runtime when searching variable.
+  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains Variable Information.
+
+  @retval          EFI_SUCCESS         Variable found successfully
+  @retval          EFI_NOT_FOUND       Variable not found
+**/
+EFI_STATUS
+FindVariableEx (
+  IN     CHAR16                  *VariableName,
+  IN     EFI_GUID                *VendorGuid,
+  IN     BOOLEAN                 IgnoreRtCheck,
+  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
+  )
+{
+  VARIABLE_HEADER                *InDeletedVariable;
+  VOID                           *Point;
+
+  PtrTrack->InDeletedTransitionPtr = NULL;
+
+  //
+  // Find the variable by walk through HOB, volatile and non-volatile variable store.
+  //
+  InDeletedVariable  = NULL;
+
+  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
+      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
+      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
+      ) {
+    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
+        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
+       ) {
+      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
+        if (VariableName[0] == 0) {
+          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
+            InDeletedVariable   = PtrTrack->CurrPtr;
+          } else {
+            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
+            return EFI_SUCCESS;
+          }
+        } else {
+          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {
+            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
+
+            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
+            if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {
+              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
+                InDeletedVariable     = PtrTrack->CurrPtr;
+              } else {
+                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
+                return EFI_SUCCESS;
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  PtrTrack->CurrPtr = InDeletedVariable;
+  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
+}
+
+/**
+  This code Finds the Next available variable.
+
+  Caution: This function may receive untrusted input.
+  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
+
+  @param[in]  VariableName  Pointer to variable name.
+  @param[in]  VendorGuid    Variable Vendor Guid.
+  @param[out] VariablePtr   Pointer to variable header address.
+
+  @retval EFI_SUCCESS           The function completed successfully.
+  @retval EFI_NOT_FOUND         The next variable was not found.
+  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
+  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
+                                GUID of an existing variable.
+
+**/
+EFI_STATUS
+EFIAPI
+VariableServiceGetNextVariableInternal (
+  IN  CHAR16                *VariableName,
+  IN  EFI_GUID              *VendorGuid,
+  OUT VARIABLE_HEADER       **VariablePtr
+  )
+{
+  VARIABLE_STORE_TYPE     Type;
+  VARIABLE_POINTER_TRACK  Variable;
+  VARIABLE_POINTER_TRACK  VariableInHob;
+  VARIABLE_POINTER_TRACK  VariablePtrTrack;
+  EFI_STATUS              Status;
+  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
+
+  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
+  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
+    //
+    // For VariableName is an empty string, FindVariable() will try to find and return
+    // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)
+    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
+    //
+    if (VariableName[0] != 0) {
+      //
+      // For VariableName is not an empty string, and FindVariable() returns error as
+      // VariableName and VendorGuid are not a name and GUID of an existing variable,
+      // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.
+      //
+      Status = EFI_INVALID_PARAMETER;
+    }
+    goto Done;
+  }
+
+  if (VariableName[0] != 0) {
+    //
+    // If variable name is not NULL, get next variable.
+    //
+    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+  }
+
+  //
+  // 0: Volatile, 1: HOB, 2: Non-Volatile.
+  // The index and attributes mapping must be kept in this order as FindVariable
+  // makes use of this mapping to implement search algorithm.
+  //
+  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
+  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
+  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
+
+  while (TRUE) {
+    //
+    // Switch from Volatile to HOB, to Non-Volatile.
+    //
+    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
+      //
+      // Find current storage index
+      //
+      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
+        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
+          break;
+        }
+      }
+      ASSERT (Type < VariableStoreTypeMax);
+      //
+      // Switch to next storage
+      //
+      for (Type++; Type < VariableStoreTypeMax; Type++) {
+        if (VariableStoreHeader[Type] != NULL) {
+          break;
+        }
+      }
+      //
+      // Capture the case that
+      // 1. current storage is the last one, or
+      // 2. no further storage
+      //
+      if (Type == VariableStoreTypeMax) {
+        Status = EFI_NOT_FOUND;
+        goto Done;
+      }
+      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
+      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
+      Variable.CurrPtr  = Variable.StartPtr;
+    }
+
+    //
+    // Variable is found
+    //
+    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
+      if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
+        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
+          //
+          // If it is a IN_DELETED_TRANSITION variable,
+          // and there is also a same ADDED one at the same time,
+          // don't return it.
+          //
+          VariablePtrTrack.StartPtr = Variable.StartPtr;
+          VariablePtrTrack.EndPtr = Variable.EndPtr;
+          Status = FindVariableEx (
+                     GetVariableNamePtr (Variable.CurrPtr),
+                     GetVendorGuidPtr (Variable.CurrPtr),
+                     FALSE,
+                     &VariablePtrTrack
+                     );
+          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {
+            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+            continue;
+          }
+        }
+
+        //
+        // Don't return NV variable when HOB overrides it
+        //
+        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
+            (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
+           ) {
+          VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
+          VariableInHob.EndPtr   = GetEndPointer   (VariableStoreHeader[VariableStoreTypeHob]);
+          Status = FindVariableEx (
+                     GetVariableNamePtr (Variable.CurrPtr),
+                     GetVendorGuidPtr (Variable.CurrPtr),
+                     FALSE,
+                     &VariableInHob
+                     );
+          if (!EFI_ERROR (Status)) {
+            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+            continue;
+          }
+        }
+
+        *VariablePtr = Variable.CurrPtr;
+        Status = EFI_SUCCESS;
+        goto Done;
+      }
+    }
+
+    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+  }
+
+Done:
+  return Status;
+}
+
+/**
+  Routine used to track statistical information about variable usage.
+  The data is stored in the EFI system table so it can be accessed later.
+  VariableInfo.efi can dump out the table. Only Boot Services variable
+  accesses are tracked by this code. The PcdVariableCollectStatistics
+  build flag controls if this feature is enabled.
+
+  A read that hits in the cache will have Read and Cache true for
+  the transaction. Data is allocated by this routine, but never
+  freed.
+
+  @param[in] VariableName   Name of the Variable to track.
+  @param[in] VendorGuid     Guid of the Variable to track.
+  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
+  @param[in] Read           TRUE if GetVariable() was called.
+  @param[in] Write          TRUE if SetVariable() was called.
+  @param[in] Delete         TRUE if deleted via SetVariable().
+  @param[in] Cache          TRUE for a cache hit.
+
+**/
+VOID
+UpdateVariableInfo (
+  IN  CHAR16                  *VariableName,
+  IN  EFI_GUID                *VendorGuid,
+  IN  BOOLEAN                 Volatile,
+  IN  BOOLEAN                 Read,
+  IN  BOOLEAN                 Write,
+  IN  BOOLEAN                 Delete,
+  IN  BOOLEAN                 Cache
+  )
+{
+  VARIABLE_INFO_ENTRY   *Entry;
+
+  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
+
+    if (AtRuntime ()) {
+      // Don't collect statistics at runtime.
+      return;
+    }
+
+    if (gVariableInfo == NULL) {
+      //
+      // On the first call allocate a entry and place a pointer to it in
+      // the EFI System Table.
+      //
+      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
+      ASSERT (gVariableInfo != NULL);
+
+      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
+      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
+      ASSERT (gVariableInfo->Name != NULL);
+      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
+      gVariableInfo->Volatile = Volatile;
+    }
+
+
+    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
+      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
+        if (StrCmp (VariableName, Entry->Name) == 0) {
+          if (Read) {
+            Entry->ReadCount++;
+          }
+          if (Write) {
+            Entry->WriteCount++;
+          }
+          if (Delete) {
+            Entry->DeleteCount++;
+          }
+          if (Cache) {
+            Entry->CacheCount++;
+          }
+
+          return;
+        }
+      }
+
+      if (Entry->Next == NULL) {
+        //
+        // If the entry is not in the table add it.
+        // Next iteration of the loop will fill in the data.
+        //
+        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
+        ASSERT (Entry->Next != NULL);
+
+        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
+        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
+        ASSERT (Entry->Next->Name != NULL);
+        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
+        Entry->Next->Volatile = Volatile;
+      }
+
+    }
+  }
+}
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
index ec463d063e..ce409f22a3 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
@@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include <Guid/SmmVariableCommon.h>
 #include "Variable.h"
+#include "VariableParsing.h"
 
 BOOLEAN                                              mAtRuntime              = FALSE;
 UINT8                                                *mVariableBufferPayload = NULL;
-- 
2.16.2.windows.1


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

* [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
  2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:03   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer Kubacki, Michael A
                   ` (6 subsequent siblings)
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

The majority of logic related to GetNextVariableName () is currently
implemented in VariableServiceGetNextVariableInternal (). The list
of variable stores to search for the given variable name and variable
GUID is defined in the function body. This change renames the function
to GetNextVariableEx () since the function is no longer internal to
a specific source file and adds a new parameter so that the caller
must pass in the list of variable stores to be used in the variable
search.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 15 ++--
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 12 ++-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c   |  8 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 78 ++++++++++++--------
 4 files changed, 73 insertions(+), 40 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
index 9d77c4916c..0d231511ea 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
@@ -248,27 +248,30 @@ FindVariableEx (
   );
 
 /**
-  This code Finds the Next available variable.
+  This code finds the next available variable.
 
   Caution: This function may receive untrusted input.
   This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
 
-  @param[in]  VariableName  Pointer to variable name.
-  @param[in]  VendorGuid    Variable Vendor Guid.
-  @param[out] VariablePtr   Pointer to variable header address.
+  @param[in]  VariableName      Pointer to variable name.
+  @param[in]  VendorGuid        Variable Vendor Guid.
+  @param[in]  VariableStoreList A list of variable stores that should be used to get the next variable.
+                                The maximum number of entries is the max value of VARIABLE_STORE_TYPE.
+  @param[out] VariablePtr       Pointer to variable header address.
 
   @retval EFI_SUCCESS           The function completed successfully.
   @retval EFI_NOT_FOUND         The next variable was not found.
-  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.
+  @retval EFI_INVALID_PARAMETER If VariableName is nt an empty string, while VendorGuid is NULL.
   @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and
                                 GUID of an existing variable.
 
 **/
 EFI_STATUS
 EFIAPI
-VariableServiceGetNextVariableInternal (
+GetNextVariableEx (
   IN  CHAR16                *VariableName,
   IN  EFI_GUID              *VendorGuid,
+  IN  VARIABLE_STORE_HEADER **VariableStoreList,
   OUT VARIABLE_HEADER       **VariablePtr
   );
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 76536308e6..816e8f7b8f 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -2358,6 +2358,7 @@ VariableServiceGetNextVariableName (
   UINTN                   MaxLen;
   UINTN                   VarNameSize;
   VARIABLE_HEADER         *VariablePtr;
+  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
 
   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
     return EFI_INVALID_PARAMETER;
@@ -2377,7 +2378,16 @@ VariableServiceGetNextVariableName (
 
   AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
 
-  Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);
+  //
+  // 0: Volatile, 1: HOB, 2: Non-Volatile.
+  // The index and attributes mapping must be kept in this order as FindVariable
+  // makes use of this mapping to implement search algorithm.
+  //
+  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
+  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
+  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
+
+  Status = GetNextVariableEx (VariableName, VendorGuid, VariableStoreHeader, &VariablePtr);
   if (!EFI_ERROR (Status)) {
     VarNameSize = NameSizeOfVariable (VariablePtr);
     ASSERT (VarNameSize != 0);
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
index dc78f68fa9..232d9ffe25 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
@@ -98,10 +98,16 @@ VariableExLibFindNextVariable (
   EFI_STATUS                    Status;
   VARIABLE_HEADER               *VariablePtr;
   AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr;
+  VARIABLE_STORE_HEADER         *VariableStoreHeader[VariableStoreTypeMax];
 
-  Status = VariableServiceGetNextVariableInternal (
+  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
+  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
+  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
+
+  Status = GetNextVariableEx (
              VariableName,
              VendorGuid,
+             VariableStoreHeader,
              &VariablePtr
              );
   if (EFI_ERROR (Status)) {
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
index 7de0a90772..9bc5369a90 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
@@ -476,14 +476,16 @@ FindVariableEx (
 }
 
 /**
-  This code Finds the Next available variable.
+  This code finds the next available variable.
 
   Caution: This function may receive untrusted input.
   This function may be invoked in SMM mode. This function will do basic validation, before parse the data.
 
-  @param[in]  VariableName  Pointer to variable name.
-  @param[in]  VendorGuid    Variable Vendor Guid.
-  @param[out] VariablePtr   Pointer to variable header address.
+  @param[in]  VariableName      Pointer to variable name.
+  @param[in]  VendorGuid        Variable Vendor Guid.
+  @param[in]  VariableStoreList A list of variable stores that should be used to get the next variable.
+                                The maximum number of entries is the max value of VARIABLE_STORE_TYPE.
+  @param[out] VariablePtr       Pointer to variable header address.
 
   @retval EFI_SUCCESS           The function completed successfully.
   @retval EFI_NOT_FOUND         The next variable was not found.
@@ -494,20 +496,41 @@ FindVariableEx (
 **/
 EFI_STATUS
 EFIAPI
-VariableServiceGetNextVariableInternal (
+GetNextVariableEx (
   IN  CHAR16                *VariableName,
   IN  EFI_GUID              *VendorGuid,
+  IN  VARIABLE_STORE_HEADER **VariableStoreList,
   OUT VARIABLE_HEADER       **VariablePtr
   )
 {
-  VARIABLE_STORE_TYPE     Type;
+  EFI_STATUS              Status;
+  VARIABLE_STORE_TYPE     StoreType;
   VARIABLE_POINTER_TRACK  Variable;
   VARIABLE_POINTER_TRACK  VariableInHob;
   VARIABLE_POINTER_TRACK  VariablePtrTrack;
-  EFI_STATUS              Status;
-  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
 
-  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
+  Status = EFI_NOT_FOUND;
+
+  if (VariableStoreList == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Check if the variable exists in the given variable store list
+  for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {
+    if (VariableStoreList[StoreType] == NULL) {
+      continue;
+    }
+
+    Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
+    Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
+    Variable.Volatile = (BOOLEAN) (StoreType == VariableStoreTypeVolatile);
+
+    Status = FindVariableEx (VariableName, VendorGuid, FALSE, &Variable);
+    if (!EFI_ERROR (Status)) {
+      break;
+    }
+  }
+
   if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
     //
     // For VariableName is an empty string, FindVariable() will try to find and return
@@ -527,39 +550,30 @@ VariableServiceGetNextVariableInternal (
 
   if (VariableName[0] != 0) {
     //
-    // If variable name is not NULL, get next variable.
+    // If variable name is not empty, get next variable.
     //
     Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
   }
 
-  //
-  // 0: Volatile, 1: HOB, 2: Non-Volatile.
-  // The index and attributes mapping must be kept in this order as FindVariable
-  // makes use of this mapping to implement search algorithm.
-  //
-  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
-  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
-  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
-
   while (TRUE) {
     //
-    // Switch from Volatile to HOB, to Non-Volatile.
+    // Switch to the next variable store if needed
     //
     while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
       //
       // Find current storage index
       //
-      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {
-        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
+      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {
+        if ((VariableStoreList[StoreType] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreList[StoreType]))) {
           break;
         }
       }
-      ASSERT (Type < VariableStoreTypeMax);
+      ASSERT (StoreType < VariableStoreTypeMax);
       //
       // Switch to next storage
       //
-      for (Type++; Type < VariableStoreTypeMax; Type++) {
-        if (VariableStoreHeader[Type] != NULL) {
+      for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) {
+        if (VariableStoreList[StoreType] != NULL) {
           break;
         }
       }
@@ -568,12 +582,12 @@ VariableServiceGetNextVariableInternal (
       // 1. current storage is the last one, or
       // 2. no further storage
       //
-      if (Type == VariableStoreTypeMax) {
+      if (StoreType == VariableStoreTypeMax) {
         Status = EFI_NOT_FOUND;
         goto Done;
       }
-      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
-      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
+      Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
+      Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
       Variable.CurrPtr  = Variable.StartPtr;
     }
 
@@ -605,11 +619,11 @@ VariableServiceGetNextVariableInternal (
         //
         // Don't return NV variable when HOB overrides it
         //
-        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
-            (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))
+        if ((VariableStoreList[VariableStoreTypeHob] != NULL) && (VariableStoreList[VariableStoreTypeNv] != NULL) &&
+            (Variable.StartPtr == GetStartPointer (VariableStoreList[VariableStoreTypeNv]))
            ) {
-          VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);
-          VariableInHob.EndPtr   = GetEndPointer   (VariableStoreHeader[VariableStoreTypeHob]);
+          VariableInHob.StartPtr = GetStartPointer (VariableStoreList[VariableStoreTypeHob]);
+          VariableInHob.EndPtr   = GetEndPointer   (VariableStoreList[VariableStoreTypeHob]);
           Status = FindVariableEx (
                      GetVariableNamePtr (Variable.CurrPtr),
                      GetVendorGuidPtr (Variable.CurrPtr),
-- 
2.16.2.windows.1


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

* [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
  2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
  2019-09-28  1:47 ` [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:03   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing Kubacki, Michael A
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

UpdateVariableInfo () currently accepts parameters regarding updates
to be made to a global variable of type VARIABLE_INFO_ENTRY. This
change passes the structure by pointer to UpdateVariableInfo ()
so structures can be updated outside the fixed global variable.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 18 +++++----
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 14 +++----
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 41 +++++++++++---------
 3 files changed, 39 insertions(+), 34 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
index 0d231511ea..6f2000f3ee 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
@@ -286,13 +286,14 @@ GetNextVariableEx (
   the transaction. Data is allocated by this routine, but never
   freed.
 
-  @param[in] VariableName   Name of the Variable to track.
-  @param[in] VendorGuid     Guid of the Variable to track.
-  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
-  @param[in] Read           TRUE if GetVariable() was called.
-  @param[in] Write          TRUE if SetVariable() was called.
-  @param[in] Delete         TRUE if deleted via SetVariable().
-  @param[in] Cache          TRUE for a cache hit.
+  @param[in]      VariableName   Name of the Variable to track.
+  @param[in]      VendorGuid     Guid of the Variable to track.
+  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
+  @param[in]      Read           TRUE if GetVariable() was called.
+  @param[in]      Write          TRUE if SetVariable() was called.
+  @param[in]      Delete         TRUE if deleted via SetVariable().
+  @param[in]      Cache          TRUE for a cache hit.
+  @param[in,out]  VariableInfo   Pointer to a pointer of VARIABLE_INFO_ENTRY structures.
 
 **/
 VOID
@@ -303,7 +304,8 @@ UpdateVariableInfo (
   IN  BOOLEAN                 Read,
   IN  BOOLEAN                 Write,
   IN  BOOLEAN                 Delete,
-  IN  BOOLEAN                 Cache
+  IN  BOOLEAN                 Cache,
+  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
   );
 
 #endif
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 816e8f7b8f..1a57d7e1ba 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -1641,7 +1641,7 @@ UpdateVariable (
         // go to delete this variable in variable HOB and
         // try to flush other variables from HOB to flash.
         //
-        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE);
+        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
         FlushHobVariableToFlash (VariableName, VendorGuid);
         return EFI_SUCCESS;
       }
@@ -1758,7 +1758,7 @@ UpdateVariable (
                  &State
                  );
       if (!EFI_ERROR (Status)) {
-        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
+        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
         if (!Variable->Volatile) {
           CacheVariable->CurrPtr->State = State;
           FlushHobVariableToFlash (VariableName, VendorGuid);
@@ -1777,7 +1777,7 @@ UpdateVariable (
       //
       // Variable content unchanged and no need to update timestamp, just return.
       //
-      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
+      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
       Status = EFI_SUCCESS;
       goto Done;
     } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||
@@ -2006,7 +2006,7 @@ UpdateVariable (
           CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
           CacheVariable->InDeletedTransitionPtr = NULL;
         }
-        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);
+        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
         FlushHobVariableToFlash (VariableName, VendorGuid);
       } else {
         if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {
@@ -2156,7 +2156,7 @@ UpdateVariable (
           CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
           CacheVariable->InDeletedTransitionPtr = NULL;
         }
-        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);
+        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
       }
       goto Done;
     }
@@ -2227,7 +2227,7 @@ UpdateVariable (
   }
 
   if (!EFI_ERROR (Status)) {
-    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
+    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
     if (!Volatile) {
       FlushHobVariableToFlash (VariableName, VendorGuid);
     }
@@ -2306,7 +2306,7 @@ VariableServiceGetVariable (
     }
 
     *DataSize = VarDataSize;
-    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
+    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE, &gVariableInfo);
 
     Status = EFI_SUCCESS;
     goto Done;
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
index 9bc5369a90..394195342d 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
@@ -660,13 +660,14 @@ Done:
   the transaction. Data is allocated by this routine, but never
   freed.
 
-  @param[in] VariableName   Name of the Variable to track.
-  @param[in] VendorGuid     Guid of the Variable to track.
-  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
-  @param[in] Read           TRUE if GetVariable() was called.
-  @param[in] Write          TRUE if SetVariable() was called.
-  @param[in] Delete         TRUE if deleted via SetVariable().
-  @param[in] Cache          TRUE for a cache hit.
+  @param[in]      VariableName   Name of the Variable to track.
+  @param[in]      VendorGuid     Guid of the Variable to track.
+  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
+  @param[in]      Read           TRUE if GetVariable() was called.
+  @param[in]      Write          TRUE if SetVariable() was called.
+  @param[in]      Delete         TRUE if deleted via SetVariable().
+  @param[in]      Cache          TRUE for a cache hit.
+  @param[in,out]  VariableInfo   Pointer to a pointer of VARIABLE_INFO_ENTRY structures.
 
 **/
 VOID
@@ -677,35 +678,38 @@ UpdateVariableInfo (
   IN  BOOLEAN                 Read,
   IN  BOOLEAN                 Write,
   IN  BOOLEAN                 Delete,
-  IN  BOOLEAN                 Cache
+  IN  BOOLEAN                 Cache,
+  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
   )
 {
   VARIABLE_INFO_ENTRY   *Entry;
 
   if (FeaturePcdGet (PcdVariableCollectStatistics)) {
-
+    if (VariableName == NULL || VendorGuid == NULL || VariableInfo == NULL) {
+      return;
+    }
     if (AtRuntime ()) {
       // Don't collect statistics at runtime.
       return;
     }
 
-    if (gVariableInfo == NULL) {
+    if (*VariableInfo == NULL) {
       //
       // On the first call allocate a entry and place a pointer to it in
       // the EFI System Table.
       //
-      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
-      ASSERT (gVariableInfo != NULL);
+      *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
+      ASSERT (*VariableInfo != NULL);
 
-      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
-      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
-      ASSERT (gVariableInfo->Name != NULL);
-      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
-      gVariableInfo->Volatile = Volatile;
+      CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
+      (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
+      ASSERT ((*VariableInfo)->Name != NULL);
+      StrCpyS ((*VariableInfo)->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
+      (*VariableInfo)->Volatile = Volatile;
     }
 
 
-    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
+    for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next) {
       if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
         if (StrCmp (VariableName, Entry->Name) == 0) {
           if (Read) {
@@ -739,7 +743,6 @@ UpdateVariableInfo (
         StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);
         Entry->Next->Volatile = Volatile;
       }
-
     }
   }
 }
-- 
2.16.2.windows.1


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

* [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (2 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:04   ` [edk2-devel] " Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions Kubacki, Michael A
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

The file VariableParsing.c provides generic functionality related
to parsing variable related structures and information. In order to
calculate offsets for certain operations, the functions must know if
authenticated variables are enabled as this increases the size of
variable headers.

This change removes linking against a global variable in an external file
in favor of a statically scoped variable in VariableParsing.c Because this
file is unaware of how the authenticated variable status is determined, the
variable is set through a function interface invoked during variable driver
initialization.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 14 +++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10 +++---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 33 ++++++++++++++++----
 3 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
index 6f2000f3ee..3eba590634 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
@@ -308,4 +308,18 @@ UpdateVariableInfo (
   IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
   );
 
+/**
+  Initializes context needed for variable parsing functions.
+
+  @param[in]       AuthFormat          If true then indicates authenticated variables are supported
+
+  @retval          EFI_SUCCESS         Initialized successfully
+  @retval          Others              An error occurred during initialization
+**/
+EFI_STATUS
+EFIAPI
+InitVariableParsing (
+  IN  BOOLEAN   AuthFormat
+  );
+
 #endif
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 1a57d7e1ba..53d797152c 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
   mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);
   mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);
 
+  Status = InitVariableParsing (mVariableModuleGlobal->VariableGlobal.AuthFormat);
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Parse non-volatile variable data and get last variable offset.
   //
@@ -3756,18 +3759,13 @@ VariableCommonInitialize (
 
   //
   // mVariableModuleGlobal->VariableGlobal.AuthFormat
-  // has been initialized in InitNonVolatileVariableStore().
+  // is initialized in InitNonVolatileVariableStore().
   //
   if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
     DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));
-    //
-    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.
-    //
-    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
     VariableGuid = &gEfiAuthenticatedVariableGuid;
   } else {
     DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));
-    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
     VariableGuid = &gEfiVariableGuid;
   }
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
index 394195342d..0a47f6d10d 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
@@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include "VariableParsing.h"
 
+STATIC  BOOLEAN   mAuthFormat;
+
 /**
 
   This code checks if variable header is valid or not.
@@ -88,7 +90,7 @@ GetVariableHeaderSize (
 {
   UINTN Value;
 
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
   } else {
     Value = sizeof (VARIABLE_HEADER);
@@ -114,7 +116,7 @@ NameSizeOfVariable (
   AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
 
   AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     if (AuthVariable->State == (UINT8) (-1) ||
        AuthVariable->DataSize == (UINT32) (-1) ||
        AuthVariable->NameSize == (UINT32) (-1) ||
@@ -149,7 +151,7 @@ SetNameSizeOfVariable (
   AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
 
   AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     AuthVariable->NameSize = (UINT32) NameSize;
   } else {
     Variable->NameSize = (UINT32) NameSize;
@@ -173,7 +175,7 @@ DataSizeOfVariable (
   AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
 
   AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     if (AuthVariable->State == (UINT8) (-1) ||
        AuthVariable->DataSize == (UINT32) (-1) ||
        AuthVariable->NameSize == (UINT32) (-1) ||
@@ -208,7 +210,7 @@ SetDataSizeOfVariable (
   AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
 
   AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     AuthVariable->DataSize = (UINT32) DataSize;
   } else {
     Variable->DataSize = (UINT32) DataSize;
@@ -248,7 +250,7 @@ GetVendorGuidPtr (
   AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
 
   AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
-  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
+  if (mAuthFormat) {
     return &AuthVariable->VendorGuid;
   } else {
     return &Variable->VendorGuid;
@@ -746,3 +748,22 @@ UpdateVariableInfo (
     }
   }
 }
+
+/**
+  Initializes context needed for variable parsing functions.
+
+  @param[in]       AuthFormat          If true then indicates authenticated variables are supported
+
+  @retval          EFI_SUCCESS         Initialized successfully
+  @retval          Others              An error occurred during initialization
+**/
+EFI_STATUS
+EFIAPI
+InitVariableParsing (
+  IN  BOOLEAN   AuthFormat
+  )
+{
+  mAuthFormat = AuthFormat;
+
+  return EFI_SUCCESS;
+}
-- 
2.16.2.windows.1


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

* [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (3 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:04   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats Kubacki, Michael A
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

This change adds a dedicated file for variable operations specific
to non-volatile variables. This decreases the overall length of the
relatively large Variable.c file.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf   |  2 ++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |  2 ++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf |  2 ++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h    | 25 +++++++++++++++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 20 +-------------
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c    | 28 ++++++++++++++++++++
 6 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
index c35e5fe787..08a5490787 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
@@ -36,6 +36,8 @@
   Variable.c
   VariableDxe.c
   Variable.h
+  VariableNonVolatile.c
+  VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
   PrivilegePolymorphic.h
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
index 626738b9c7..6dc2721b81 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
@@ -45,6 +45,8 @@
   Variable.c
   VariableTraditionalMm.c
   VariableSmm.c
+  VariableNonVolatile.c
+  VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
   VarCheck.c
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
index 1ba8f9ebfb..ca9d23ce9f 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
@@ -45,6 +45,8 @@
   Variable.c
   VariableSmm.c
   VariableStandaloneMm.c
+  VariableNonVolatile.c
+  VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
   VarCheck.c
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
new file mode 100644
index 0000000000..82572262ef
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
@@ -0,0 +1,25 @@
+/** @file
+  Common variable non-volatile store routines.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _VARIABLE_NON_VOLATILE_H_
+#define _VARIABLE_NON_VOLATILE_H_
+
+#include "Variable.h"
+
+/**
+  Get non-volatile maximum variable size.
+
+  @return Non-volatile maximum variable size.
+
+**/
+UINTN
+GetNonVolatileMaxVariableSize (
+  VOID
+  );
+
+#endif
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 53d797152c..5da2354aa5 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 **/
 
 #include "Variable.h"
+#include "VariableNonVolatile.h"
 #include "VariableParsing.h"
 
 VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
@@ -3006,25 +3007,6 @@ ReclaimForOS(
   }
 }
 
-/**
-  Get non-volatile maximum variable size.
-
-  @return Non-volatile maximum variable size.
-
-**/
-UINTN
-GetNonVolatileMaxVariableSize (
-  VOID
-  )
-{
-  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
-    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),
-                PcdGet32 (PcdMaxHardwareErrorVariableSize));
-  } else {
-    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));
-  }
-}
-
 /**
   Get maximum variable size, covering both non-volatile and volatile variables.
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
new file mode 100644
index 0000000000..b1b6d8282f
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
@@ -0,0 +1,28 @@
+/** @file
+  Common variable non-volatile store routines.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "VariableNonVolatile.h"
+
+/**
+  Get non-volatile maximum variable size.
+
+  @return Non-volatile maximum variable size.
+
+**/
+UINTN
+GetNonVolatileMaxVariableSize (
+  VOID
+  )
+{
+  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
+    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),
+                PcdGet32 (PcdMaxHardwareErrorVariableSize));
+  } else {
+    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));
+  }
+}
-- 
2.16.2.windows.1


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

* [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (4 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:04   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support Kubacki, Michael A
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220

The current VariableInfo application only checks for variable
statistics from SMM if the variable information entries are
not present in the UEFI System Configuration table as published
by the DXE UEFI variable driver (VariableRuntimeDxe).

This change first checks for variable information entries in the
UEFI System Configuration but always checks for entries in SMM
as well. If the SMM variable driver is not present, an instance of
EFI_SMM_VARIABLE_PROTOCOL will not be found and the search for
SMM variable statistics will be aborted (an SW SMI to get variable
statistics will not be triggered).

In the case variable statistics are provided by both a Runtime DXE
driver (e.g. VariableSmmRuntimeDxe) and a SMM driver (VariableSmm),
this change will clearly identify statistics from each respective
driver.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Application/VariableInfo/VariableInfo.c | 37 ++++++++++----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/MdeModulePkg/Application/VariableInfo/VariableInfo.c b/MdeModulePkg/Application/VariableInfo/VariableInfo.c
index f213471e9a..c04ba18213 100644
--- a/MdeModulePkg/Application/VariableInfo/VariableInfo.c
+++ b/MdeModulePkg/Application/VariableInfo/VariableInfo.c
@@ -3,7 +3,7 @@
   this utility will print out the statistics information. You can use console
   redirection to capture the data.
 
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -126,7 +126,7 @@ PrintInfoFromSmm (
   ASSERT (CommBuffer != NULL);
   ZeroMem (CommBuffer, RealCommSize);
 
-  Print (L"Non-Volatile SMM Variables:\n");
+  Print (L"SMM Driver Non-Volatile Variables:\n");
   do {
     CommSize = RealCommSize;
     Status = GetVariableStatisticsData (CommBuffer, &CommSize);
@@ -155,7 +155,7 @@ PrintInfoFromSmm (
     }
   } while (TRUE);
 
-  Print (L"Volatile SMM Variables:\n");
+  Print (L"SMM Driver Volatile Variables:\n");
   ZeroMem (CommBuffer, RealCommSize);
   do {
     CommSize = RealCommSize;
@@ -207,24 +207,18 @@ UefiMain (
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_STATUS            Status;
+  EFI_STATUS            RuntimeDxeStatus;
+  EFI_STATUS            SmmStatus;
   VARIABLE_INFO_ENTRY   *VariableInfo;
   VARIABLE_INFO_ENTRY   *Entry;
 
-  Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);
-  if (EFI_ERROR (Status) || (Entry == NULL)) {
-    Status = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);
+  RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **) &Entry);
+  if (EFI_ERROR (RuntimeDxeStatus) || (Entry == NULL)) {
+    RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **) &Entry);
   }
 
-  if (EFI_ERROR (Status) || (Entry == NULL)) {
-    Status = PrintInfoFromSmm ();
-    if (!EFI_ERROR (Status)) {
-      return Status;
-    }
-  }
-
-  if (!EFI_ERROR (Status) && (Entry != NULL)) {
-    Print (L"Non-Volatile EFI Variables:\n");
+  if (!EFI_ERROR (RuntimeDxeStatus) && (Entry != NULL)) {
+    Print (L"Runtime DXE Driver Non-Volatile EFI Variables:\n");
     VariableInfo = Entry;
     do {
       if (!VariableInfo->Volatile) {
@@ -242,7 +236,7 @@ UefiMain (
       VariableInfo = VariableInfo->Next;
     } while (VariableInfo != NULL);
 
-    Print (L"Volatile EFI Variables:\n");
+    Print (L"Runtime DXE Driver Volatile EFI Variables:\n");
     VariableInfo = Entry;
     do {
       if (VariableInfo->Volatile) {
@@ -258,14 +252,19 @@ UefiMain (
       }
       VariableInfo = VariableInfo->Next;
     } while (VariableInfo != NULL);
+  }
 
-  } else {
+  SmmStatus = PrintInfoFromSmm ();
+
+  if (EFI_ERROR (RuntimeDxeStatus) && EFI_ERROR (SmmStatus)) {
     Print (L"Warning: Variable Dxe/Smm driver doesn't enable the feature of statistical information!\n");
     Print (L"If you want to see this info, please:\n");
     Print (L"  1. Set PcdVariableCollectStatistics as TRUE\n");
     Print (L"  2. Rebuild Variable Dxe/Smm driver\n");
     Print (L"  3. Run \"VariableInfo\" cmd again\n");
+
+    return EFI_NOT_FOUND;
   }
 
-  return Status;
+  return EFI_SUCCESS;
 }
-- 
2.16.2.windows.1


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

* [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (5 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:04   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() " Kubacki, Michael A
  2019-09-28  1:47 ` [PATCH V2 9/9] MdeModulePkg/VariableSmm: Remove unused SMI handler functions Kubacki, Michael A
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220

This change reduces SMIs for GetVariable () by maintaining a
UEFI variable cache in Runtime DXE in addition to the pre-
existing cache in SMRAM. When the Runtime Service GetVariable()
is invoked, a Runtime DXE cache is used instead of triggering an
SMI to VariableSmm. This can improve overall system performance
by servicing variable read requests without rendezvousing all
cores into SMM.

The following are important points regarding this change.

1. All of the non-volatile storage contents are loaded into the
   cache upon driver load. This one time load operation from storage
   is preferred as opposed to building the cache on demand. An on-
   demand cache would require a fallback SMI to load data into the
   cache as variables are requested.

2. SetVariable () requests will continue to always trigger an SMI.
   This occurs regardless of whether the variable is volatile or
   non-volatile.

3. Both volatile and non-volatile variables are cached in a runtime
   buffer. As is the case in the current EDK II variable driver, they
   continue to be cached in separate buffers.

4. The cache in Runtime DXE and SMM are intended to be exact copies
   of one another. All SMM variable accesses only return data from the
   SMM cache. The runtime caches are only updated after the variable I/O
   operation is successful in SMM. The runtime caches are only updated
   from SMM.

5. Synchronization mechanisms are in place to ensure the runtime cache
   content integrity with the SMM cache. These may result in updates to
   runtime cache that are the same in content but different in offset and
   size from updates to the SMM cache.

When using SMM variables, two caches will now be present.
1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to service
   Runtime Services GetVariable () and GetNextVariableName () callers.
2. "SMM Cache" - Maintained in VariableSmm to service SMM GetVariable ()
   and GetNextVariableName () callers.
   a. This cache is retained so SMM modules do not operate on data outside
      SMRAM.

It is possible to view UEFI variable read and write statistics by setting
the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics FeaturePcd
to TRUE and using the VariableInfo UEFI application in MdeModulePkg to dump
variable statistics to the console. By doing so, a user can view the number
of GetVariable () hits from the Runtime DXE variable driver (Runtime Cache
hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for
GetVariable () will occur when SMM modules invoke GetVariable ().

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf    |   2 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf           |   2 +
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf |  31 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf  |   2 +
 MdeModulePkg/Include/Guid/SmmVariableCommon.h                        |  29 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h                |  39 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h    |  47 ++
 MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c                |  44 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c    | 153 +++++
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c             | 114 +++-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c   | 608 +++++++++++++++++---
 11 files changed, 966 insertions(+), 105 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
index 08a5490787..ceea5d1ff9 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
@@ -40,6 +40,8 @@
   VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
+  VariableRuntimeCache.c
+  VariableRuntimeCache.h
   PrivilegePolymorphic.h
   Measurement.c
   TcgMorLockDxe.c
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
index 6dc2721b81..bc3033588d 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
@@ -49,6 +49,8 @@
   VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
+  VariableRuntimeCache.c
+  VariableRuntimeCache.h
   VarCheck.c
   Variable.h
   PrivilegePolymorphic.h
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf
index 14894e6f13..70837ac6e0 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf
@@ -13,7 +13,7 @@
 #  may not be modified without authorization. If platform fails to protect these resources,
 #  the authentication service provided in this driver will be broken, and the behavior is undefined.
 #
-# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -39,6 +39,10 @@
   VariableSmmRuntimeDxe.c
   PrivilegePolymorphic.h
   Measurement.c
+  VariableParsing.c
+  VariableParsing.h
+  VariableRuntimeCache.c
+  VariableRuntimeCache.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -49,6 +53,7 @@
   BaseLib
   UefiBootServicesTableLib
   DebugLib
+  TimerLib
   UefiRuntimeLib
   DxeServicesTableLib
   UefiDriverEntryPoint
@@ -65,7 +70,29 @@
   gEdkiiVariableLockProtocolGuid                ## PRODUCES
   gEdkiiVarCheckProtocolGuid                    ## PRODUCES
 
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                      ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize                  ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize         ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                    ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                     ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize           ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize  ## CONSUMES
+
+[FeaturePcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics            ## CONSUMES
+
 [Guids]
+  ## PRODUCES             ## GUID # Signature of Variable store header
+  ## CONSUMES             ## GUID # Signature of Variable store header
+  ## SOMETIMES_PRODUCES   ## SystemTable
+  gEfiAuthenticatedVariableGuid
+
+  ## PRODUCES             ## GUID # Signature of Variable store header
+  ## CONSUMES             ## GUID # Signature of Variable store header
+  ## SOMETIMES_PRODUCES   ## SystemTable
+  gEfiVariableGuid
+
   gEfiEventVirtualAddressChangeGuid             ## CONSUMES ## Event
   gEfiEventExitBootServicesGuid                 ## CONSUMES ## Event
   ## CONSUMES ## GUID # Locate protocol
@@ -82,6 +109,8 @@
   ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
   gEfiImageSecurityDatabaseGuid
 
+  gEdkiiPiSmmCommunicationRegionTableGuid       ## SOMETIMES_CONSUMES ## SystemTable
+
 [Depex]
   gEfiSmmCommunicationProtocolGuid
 
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
index ca9d23ce9f..95c5310c0b 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
@@ -49,6 +49,8 @@
   VariableNonVolatile.h
   VariableParsing.c
   VariableParsing.h
+  VariableRuntimeCache.c
+  VariableRuntimeCache.h
   VarCheck.c
   Variable.h
   PrivilegePolymorphic.h
diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
index c527a59891..ceef44dfd2 100644
--- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
+++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
@@ -1,7 +1,7 @@
 /** @file
   The file defined some common structures used for communicating between SMM variable module and SMM variable wrapper module.
 
-Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #ifndef _SMM_VARIABLE_COMMON_H_
 #define _SMM_VARIABLE_COMMON_H_
 
+#include <Guid/VariableFormat.h>
 #include <Protocol/VarCheck.h>
 
 #define EFI_SMM_VARIABLE_WRITE_GUID \
@@ -66,6 +67,16 @@ typedef struct {
 #define SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET  10
 
 #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE        11
+//
+// The payload for this function is SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
+//
+#define SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT   12
+
+#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE                    13
+//
+// The payload for this function is SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
+//
+#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO                14
 
 ///
 /// Size of SMM communicate header, without including the payload.
@@ -120,4 +131,20 @@ typedef struct {
   UINTN                         VariablePayloadSize;
 } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
 
+typedef struct {
+  BOOLEAN                 *ReadLock;
+  BOOLEAN                 *PendingUpdate;
+  BOOLEAN                 *HobFlushComplete;
+  VARIABLE_STORE_HEADER   *RuntimeHobCache;
+  VARIABLE_STORE_HEADER   *RuntimeNvCache;
+  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
+} SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
+
+typedef struct {
+  UINTN                   TotalHobStorageSize;
+  UINTN                   TotalNvStorageSize;
+  UINTN                   TotalVolatileStorageSize;
+  BOOLEAN                 AuthenticatedVariableUsage;
+} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
+
 #endif // _SMM_VARIABLE_COMMON_H_
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
index fb574b2e32..b9723c0250 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
@@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 ///
 #define ISO_639_2_ENTRY_SIZE    3
 
+///
+/// The timeout to in 10us units to wait for the
+/// variable runtime cache read lock to be acquired.
+///
+#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
+
 typedef enum {
   VariableStoreTypeVolatile,
   VariableStoreTypeHob,
@@ -64,6 +70,21 @@ typedef enum {
   VariableStoreTypeMax
 } VARIABLE_STORE_TYPE;
 
+typedef struct {
+  UINT32                  PendingUpdateOffset;
+  UINT32                  PendingUpdateLength;
+  VARIABLE_STORE_HEADER   *Store;
+} VARIABLE_RUNTIME_CACHE;
+
+typedef struct {
+  BOOLEAN                 *ReadLock;
+  BOOLEAN                 *PendingUpdate;
+  BOOLEAN                 *HobFlushComplete;
+  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
+  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
+  VARIABLE_RUNTIME_CACHE  VariableRuntimeVolatileCache;
+} VARIABLE_RUNTIME_CACHE_CONTEXT;
+
 typedef struct {
   VARIABLE_HEADER *CurrPtr;
   //
@@ -79,14 +100,16 @@ typedef struct {
 } VARIABLE_POINTER_TRACK;
 
 typedef struct {
-  EFI_PHYSICAL_ADDRESS  HobVariableBase;
-  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
-  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
-  EFI_LOCK              VariableServicesLock;
-  UINT32                ReentrantState;
-  BOOLEAN               AuthFormat;
-  BOOLEAN               AuthSupport;
-  BOOLEAN               EmuNvMode;
+  EFI_PHYSICAL_ADDRESS            HobVariableBase;
+  EFI_PHYSICAL_ADDRESS            HobVariableBackupBase;
+  EFI_PHYSICAL_ADDRESS            VolatileVariableBase;
+  EFI_PHYSICAL_ADDRESS            NonVolatileVariableBase;
+  VARIABLE_RUNTIME_CACHE_CONTEXT  VariableRuntimeCacheContext;
+  EFI_LOCK                        VariableServicesLock;
+  UINT32                          ReentrantState;
+  BOOLEAN                         AuthFormat;
+  BOOLEAN                         AuthSupport;
+  BOOLEAN                         EmuNvMode;
 } VARIABLE_GLOBAL;
 
 typedef struct {
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
new file mode 100644
index 0000000000..09b83eb215
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
@@ -0,0 +1,47 @@
+/** @file
+  The common variable volatile store routines shared by the DXE_RUNTIME variable
+  module and the DXE_SMM variable module.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _VARIABLE_RUNTIME_CACHE_H_
+#define _VARIABLE_RUNTIME_CACHE_H_
+
+#include "Variable.h"
+
+/**
+  Copies any pending updates to runtime variable caches.
+
+  @retval EFI_UNSUPPORTED         The volatile store to be updated is not initialized properly.
+  @retval EFI_SUCCESS             The volatile store was updated successfully.
+
+**/
+EFI_STATUS
+SynchronizeRuntimeVariableCacheEx (
+  VOID
+  );
+
+/**
+  Synchronizes the runtime variable caches with all pending updates outside runtime.
+
+  Ensures all conditions are met to maintain coherency for runtime cache updates.
+
+  @param[in] VariableRuntimeCache Variable runtime cache structure for the runtime cache being synchronized.
+  @param[in] Offset               Offset in bytes to apply the update.
+  @param[in] Length               Length of data in bytes of the update.
+
+  @retval EFI_UNSUPPORTED         The volatile store to be updated is not initialized properly.
+  @retval EFI_SUCCESS             The volatile store was updated successfully.
+
+**/
+EFI_STATUS
+SynchronizeRuntimeVariableCache (
+  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
+  IN  UINTN                           Offset,
+  IN  UINTN                           Length
+  );
+
+#endif
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 5da2354aa5..bb2fa3fc19 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "Variable.h"
 #include "VariableNonVolatile.h"
 #include "VariableParsing.h"
+#include "VariableRuntimeCache.h"
 
 VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
 
@@ -332,6 +333,12 @@ RecordVarErrorFlag (
       // Update the data in NV cache.
       //
       *VarErrFlag = TempFlag;
+      Status =  SynchronizeRuntimeVariableCache (
+                  &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache,
+                  (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
+                  sizeof (TempFlag)
+                  );
+      ASSERT_EFI_ERROR (Status);
     }
   }
 }
@@ -755,12 +762,24 @@ Reclaim (
 
 Done:
   if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode) {
+    Status =  SynchronizeRuntimeVariableCache (
+                &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache,
+                0,
+                VariableStoreHeader->Size
+                );
+    ASSERT_EFI_ERROR (Status);
     FreePool (ValidBuffer);
   } else {
     //
     // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.
     //
-    CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
+    CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size);
+    Status =  SynchronizeRuntimeVariableCache (
+                &(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache),
+                0,
+                VariableStoreHeader->Size
+                );
+    ASSERT_EFI_ERROR (Status);
   }
 
   return Status;
@@ -1592,6 +1611,7 @@ UpdateVariable (
   VARIABLE_POINTER_TRACK              *Variable;
   VARIABLE_POINTER_TRACK              NvVariable;
   VARIABLE_STORE_HEADER               *VariableStoreHeader;
+  VARIABLE_RUNTIME_CACHE              *VolatileCacheInstance;
   UINT8                               *BufferForMerge;
   UINTN                               MergedBufSize;
   BOOLEAN                             DataReady;
@@ -2235,6 +2255,21 @@ UpdateVariable (
   }
 
 Done:
+  if (!EFI_ERROR (Status)) {
+    if (Variable->Volatile) {
+      VolatileCacheInstance = &(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache);
+    } else {
+      VolatileCacheInstance = &(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache);
+    }
+
+    Status =  SynchronizeRuntimeVariableCache (
+                VolatileCacheInstance,
+                0,
+                VolatileCacheInstance->Store->Size
+                );
+    ASSERT_EFI_ERROR (Status);
+  }
+
   return Status;
 }
 
@@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
         ErrorFlag = TRUE;
       }
     }
+    Status =  SynchronizeRuntimeVariableCache (
+                &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache,
+                0,
+                mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.Store->Size
+                );
+    ASSERT_EFI_ERROR (Status);
     if (ErrorFlag) {
       //
       // We still have HOB variable(s) not flushed in flash.
@@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
       // All HOB variables have been flushed in flash.
       //
       DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));
+      *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.HobFlushComplete) = TRUE;
       if (!AtRuntime ()) {
         FreePool ((VOID *) VariableStoreHeader);
       }
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
new file mode 100644
index 0000000000..2642d9b000
--- /dev/null
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
@@ -0,0 +1,153 @@
+/** @file
+  The common variable volatile store routines shared by the DXE_RUNTIME variable
+  module and the DXE_SMM variable module.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - variable data. They may be input in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "VariableParsing.h"
+#include "VariableRuntimeCache.h"
+
+extern VARIABLE_MODULE_GLOBAL   *mVariableModuleGlobal;
+extern VARIABLE_STORE_HEADER    *mNvVariableCache;
+
+/**
+  Copies any pending updates to runtime variable caches.
+
+  @retval EFI_UNSUPPORTED         The volatile store to be updated is not initialized properly.
+  @retval EFI_SUCCESS             The volatile store was updated successfully.
+
+**/
+EFI_STATUS
+SynchronizeRuntimeVariableCacheEx (
+  VOID
+  )
+{
+  if (
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Store == NULL ||
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.Store == NULL ||
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
+    ) {
+    return EFI_UNSUPPORTED;
+  }
+
+  if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate)) {
+    if (
+      mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.Store != NULL &&
+      mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0
+      ) {
+      CopyMem (
+        (VOID *) (
+          ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.Store) +
+          mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.PendingUpdateOffset
+          ),
+        (VOID *) (
+          ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase) +
+          mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.PendingUpdateOffset
+          ),
+        mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.PendingUpdateLength
+        );
+      mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.PendingUpdateLength = 0;
+      mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.PendingUpdateOffset = 0;
+    }
+
+    CopyMem (
+      (VOID *) (
+        ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Store) +
+        mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.PendingUpdateOffset
+        ),
+      (VOID *) (
+        ((UINT8 *) (UINTN) mNvVariableCache) +
+        mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.PendingUpdateOffset
+        ),
+      mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.PendingUpdateLength
+      );
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.PendingUpdateLength = 0;
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.PendingUpdateOffset = 0;
+
+    CopyMem (
+      (VOID *) (
+        ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.Store) +
+        mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.PendingUpdateOffset
+      ),
+      (VOID *) (
+        ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase) +
+        mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.PendingUpdateOffset
+        ),
+      mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.PendingUpdateLength
+      );
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.PendingUpdateLength = 0;
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCache.PendingUpdateOffset = 0;
+    *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = FALSE;
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Synchronizes the runtime variable caches with all pending updates outside runtime.
+
+  Ensures all conditions are met to maintain coherency for runtime cache updates.
+
+  @param[in] VariableRuntimeCache Variable runtime cache structure for the runtime cache being synchronized.
+  @param[in] Offset               Offset in bytes to apply the update.
+  @param[in] Length               Length of data in bytes of the update.
+
+  @retval EFI_UNSUPPORTED         The volatile store to be updated is not initialized properly.
+  @retval EFI_SUCCESS             The volatile store was updated successfully.
+
+**/
+EFI_STATUS
+SynchronizeRuntimeVariableCache (
+  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
+  IN  UINTN                           Offset,
+  IN  UINTN                           Length
+  )
+{
+  if (VariableRuntimeCache == NULL) {
+    return EFI_INVALID_PARAMETER;
+  } else if (VariableRuntimeCache->Store == NULL) {
+      // Runtime cache is not available yet at this point,
+      // Return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET to let it progress
+      return EFI_SUCCESS;
+  }
+
+  if (
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL ||
+    mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL
+    ) {
+    return EFI_UNSUPPORTED;
+  }
+
+  if (
+    *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
+    VariableRuntimeCache->PendingUpdateLength > 0
+    ) {
+    VariableRuntimeCache->PendingUpdateLength =
+      (UINT32) (
+        MAX (
+          (UINTN) (VariableRuntimeCache->PendingUpdateOffset + VariableRuntimeCache->PendingUpdateLength),
+          Offset + Length
+        ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset)
+      );
+    VariableRuntimeCache->PendingUpdateOffset =
+      (UINT32) MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset);
+  } else {
+    VariableRuntimeCache->PendingUpdateLength = (UINT32) Length;
+    VariableRuntimeCache->PendingUpdateOffset = (UINT32) Offset;
+  }
+  *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
+
+  if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
+    return SynchronizeRuntimeVariableCacheEx ();
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
index ce409f22a3..8d767f75ac 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
@@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Guid/SmmVariableCommon.h>
 #include "Variable.h"
 #include "VariableParsing.h"
+#include "VariableRuntimeCache.h"
+
+extern VARIABLE_STORE_HEADER                         *mNvVariableCache;
 
 BOOLEAN                                              mAtRuntime              = FALSE;
 UINT8                                                *mVariableBufferPayload = NULL;
@@ -451,25 +454,29 @@ SmmVariableGetStatistics (
 EFI_STATUS
 EFIAPI
 SmmVariableHandler (
-  IN     EFI_HANDLE                                DispatchHandle,
-  IN     CONST VOID                                *RegisterContext,
-  IN OUT VOID                                      *CommBuffer,
-  IN OUT UINTN                                     *CommBufferSize
+  IN     EFI_HANDLE                                       DispatchHandle,
+  IN     CONST VOID                                       *RegisterContext,
+  IN OUT VOID                                             *CommBuffer,
+  IN OUT UINTN                                            *CommBufferSize
   )
 {
-  EFI_STATUS                                       Status;
-  SMM_VARIABLE_COMMUNICATE_HEADER                  *SmmVariableFunctionHeader;
-  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE         *SmmVariableHeader;
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME  *GetNextVariableName;
-  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO     *QueryVariableInfo;
-  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE        *GetPayloadSize;
-  VARIABLE_INFO_ENTRY                              *VariableInfo;
-  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE           *VariableToLock;
-  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;
-  UINTN                                            InfoSize;
-  UINTN                                            NameBufferSize;
-  UINTN                                            CommBufferPayloadSize;
-  UINTN                                            TempCommBufferSize;
+  EFI_STATUS                                              Status;
+  SMM_VARIABLE_COMMUNICATE_HEADER                         *SmmVariableFunctionHeader;
+  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE                *SmmVariableHeader;
+  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME         *GetNextVariableName;
+  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO            *QueryVariableInfo;
+  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE               *GetPayloadSize;
+  SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *RuntimeVariableCacheContext;
+  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO         *GetRuntimeCacheInfo;
+  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE                  *VariableToLock;
+  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY    *CommVariableProperty;
+  VARIABLE_INFO_ENTRY                                     *VariableInfo;
+  VARIABLE_RUNTIME_CACHE_CONTEXT                          *VariableCacheContext;
+  VARIABLE_STORE_HEADER                                   *VariableCache;
+  UINTN                                                   InfoSize;
+  UINTN                                                   NameBufferSize;
+  UINTN                                                   CommBufferPayloadSize;
+  UINTN                                                   TempCommBufferSize;
 
   //
   // If input is invalid, stop processing this SMI
@@ -789,6 +796,79 @@ SmmVariableHandler (
                  );
       CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
       break;
+    case SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT:
+      if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT)) {
+        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM communication buffer size invalid!\n"));
+      } else if (mEndOfDxe) {
+        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Cannot init context after end of DXE!\n"));
+      } else {
+        RuntimeVariableCacheContext = (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *) SmmVariableFunctionHeader->Data;
+        VariableCacheContext = &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext;
+
+        ASSERT (RuntimeVariableCacheContext->RuntimeVolatileCache != NULL);
+        ASSERT (RuntimeVariableCacheContext->RuntimeNvCache != NULL);
+        ASSERT (RuntimeVariableCacheContext->PendingUpdate != NULL);
+        ASSERT (RuntimeVariableCacheContext->ReadLock != NULL);
+        ASSERT (RuntimeVariableCacheContext->HobFlushComplete != NULL);
+
+        VariableCacheContext->VariableRuntimeHobCache.Store      = RuntimeVariableCacheContext->RuntimeHobCache;
+        VariableCacheContext->VariableRuntimeVolatileCache.Store = RuntimeVariableCacheContext->RuntimeVolatileCache;
+        VariableCacheContext->VariableRuntimeNvCache.Store       = RuntimeVariableCacheContext->RuntimeNvCache;
+        VariableCacheContext->PendingUpdate                      = RuntimeVariableCacheContext->PendingUpdate;
+        VariableCacheContext->ReadLock                           = RuntimeVariableCacheContext->ReadLock;
+        VariableCacheContext->HobFlushComplete                   = RuntimeVariableCacheContext->HobFlushComplete;
+
+        // Set up the intial pending request since the RT cache needs to be in sync with SMM cache
+        if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {
+          VariableCacheContext->VariableRuntimeHobCache.PendingUpdateOffset = 0;
+          VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLength = 0;
+        } else {
+          VariableCache = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
+          VariableCacheContext->VariableRuntimeHobCache.PendingUpdateOffset = 0;
+          VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLength = (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache);
+          CopyGuid (&(VariableCacheContext->VariableRuntimeHobCache.Store->Signature), &(VariableCache->Signature));
+        }
+        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
+        VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffset   = 0;
+        VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateLength   = (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache);
+        CopyGuid (&(VariableCacheContext->VariableRuntimeVolatileCache.Store->Signature), &(VariableCache->Signature));
+
+        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN) mNvVariableCache;
+        VariableCacheContext->VariableRuntimeNvCache.PendingUpdateOffset = 0;
+        VariableCacheContext->VariableRuntimeNvCache.PendingUpdateLength = (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache);
+        CopyGuid (&(VariableCacheContext->VariableRuntimeNvCache.Store->Signature), &(VariableCache->Signature));
+
+        *(VariableCacheContext->PendingUpdate) = TRUE;
+        *(VariableCacheContext->ReadLock) = FALSE;
+        *(VariableCacheContext->HobFlushComplete) = FALSE;
+      }
+      Status = EFI_SUCCESS;
+      break;
+    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
+      Status = SynchronizeRuntimeVariableCacheEx ();
+      break;
+    case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
+      if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
+        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM communication buffer size invalid!\n"));
+        return EFI_SUCCESS;
+      }
+      GetRuntimeCacheInfo = (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *) SmmVariableFunctionHeader->Data;
+
+      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) {
+        VariableCache = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
+        GetRuntimeCacheInfo->TotalHobStorageSize = VariableCache->Size;
+      } else {
+        GetRuntimeCacheInfo->TotalHobStorageSize = 0;
+      }
+
+      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
+      GetRuntimeCacheInfo->TotalVolatileStorageSize = VariableCache->Size;
+      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN) mNvVariableCache;
+      GetRuntimeCacheInfo->TotalNvStorageSize = (UINTN) VariableCache->Size;
+      GetRuntimeCacheInfo->AuthenticatedVariableUsage = mVariableModuleGlobal->VariableGlobal.AuthFormat;
+
+      Status = EFI_SUCCESS;
+      break;
 
     default:
       Status = EFI_UNSUPPORTED;
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 0a1888e5ef..46f69765a4 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -13,7 +13,7 @@
 
   InitCommunicateBuffer() is really function to check the variable data size.
 
-Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/UefiRuntimeLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
+#include <Library/TimerLib.h>
 #include <Library/UefiLib.h>
 #include <Library/BaseLib.h>
 
 #include <Guid/EventGroup.h>
+#include <Guid/PiSmmCommunicationRegionTable.h>
 #include <Guid/SmmVariableCommon.h>
 
 #include "PrivilegePolymorphic.h"
+#include "VariableParsing.h"
 
 EFI_HANDLE                       mHandle                    = NULL;
 EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable               = NULL;
@@ -46,8 +49,19 @@ EFI_EVENT                        mVirtualAddressChangeEvent = NULL;
 EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication          = NULL;
 UINT8                           *mVariableBuffer            = NULL;
 UINT8                           *mVariableBufferPhysical    = NULL;
+VARIABLE_INFO_ENTRY             *mVariableInfo              = NULL;
+VARIABLE_STORE_HEADER           *mVariableRuntimeHobCacheBuffer           = NULL;
+VARIABLE_STORE_HEADER           *mVariableRuntimeNvCacheBuffer            = NULL;
+VARIABLE_STORE_HEADER           *mVariableRuntimeVolatileCacheBuffer      = NULL;
 UINTN                            mVariableBufferSize;
+UINTN                            mVariableRuntimeHobCacheBufferSize;
+UINTN                            mVariableRuntimeNvCacheBufferSize;
+UINTN                            mVariableRuntimeVolatileCacheBufferSize;
 UINTN                            mVariableBufferPayloadSize;
+BOOLEAN                          mVariableRuntimeCachePendingUpdate;
+BOOLEAN                          mVariableRuntimeCacheReadLock;
+BOOLEAN                          mVariableAuthFormat;
+BOOLEAN                          mHobFlushComplete;
 EFI_LOCK                         mVariableServicesLock;
 EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
 EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
@@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
   }
 }
 
+/**
+  Return TRUE if ExitBootServices () has been called.
+
+  @retval TRUE If ExitBootServices () has been called.
+**/
+BOOLEAN
+AtRuntime (
+  VOID
+  )
+{
+  return EfiAtRuntime ();
+}
+
+/**
+  Initialize the variable cache buffer as an empty variable store.
+
+  @param[out]     VariableCacheBuffer     A pointer to pointer of a cache variable store.
+  @param[in,out]  TotalVariableCacheSize  On input, the minimum size needed for the UEFI variable store cache
+                                          buffer that is allocated. On output, the actual size of the buffer allocated.
+                                          If TotalVariableCacheSize is zero, a buffer will not be allocated and the
+                                          function will return with EFI_SUCCESS.
+
+  @retval EFI_SUCCESS             The variable cache was allocated and initialized successfully.
+  @retval EFI_INVALID_PARAMETER   A given pointer is NULL or an invalid variable store size was specified.
+  @retval EFI_OUT_OF_RESOURCES    Insufficient resources are available to allocate the variable store cache buffer.
+
+**/
+EFI_STATUS
+EFIAPI
+InitVariableCache (
+  OUT    VARIABLE_STORE_HEADER   **VariableCacheBuffer,
+  IN OUT UINTN                   *TotalVariableCacheSize
+  )
+{
+  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
+
+  if (TotalVariableCacheSize == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+  if (*TotalVariableCacheSize == 0) {
+    return EFI_SUCCESS;
+  }
+  if (VariableCacheBuffer == NULL || *TotalVariableCacheSize < sizeof (VARIABLE_STORE_HEADER)) {
+    return EFI_INVALID_PARAMETER;
+  }
+  *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof (UINT32));
+
+  //
+  // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
+  //
+  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *) AllocateRuntimePages (
+                            EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)
+                            );
+  if (*VariableCacheBuffer == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+  VariableCacheStorePtr = *VariableCacheBuffer;
+  SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize, (UINT32) 0xFFFFFFFF);
+
+  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER));
+  VariableCacheStorePtr->Size    = (UINT32) *TotalVariableCacheSize;
+  VariableCacheStorePtr->Format  = VARIABLE_STORE_FORMATTED;
+  VariableCacheStorePtr->State   = VARIABLE_STORE_HEALTHY;
+
+  return EFI_SUCCESS;
+}
+
 /**
   Initialize the communicate buffer using DataSize and Function.
 
@@ -153,6 +234,69 @@ InitCommunicateBuffer (
 }
 
 
+/**
+  Gets a SMM communicate buffer from the EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as an entry in the UEFI
+  system configuration table. A generic SMM communication buffer DXE driver may install the table or a custom table
+  may be installed by a platform-specific driver.
+
+  The communicate size is:  SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
+                            DataSize.
+
+  @param[in,out]   CommBufferSize   On input, the minimum size needed for the communication buffer.
+                                    On output, the SMM buffer size available at CommBuffer.
+  @param[out]      CommBuffer       A pointer to an SMM communication buffer pointer.
+
+  @retval EFI_SUCCESS               The communication buffer was found successfully.
+  @retval EFI_INVALID_PARAMETER     A given pointer is NULL or the CommBufferSize is zero.
+  @retval EFI_NOT_FOUND             The EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not found.
+  @retval EFI_OUT_OF_RESOURCES      A valid SMM communicate buffer for the requested size is not available.
+
+**/
+EFI_STATUS
+GetCommunicateBuffer (
+  IN OUT  UINTN     *CommBufferSize,
+  OUT     UINT8     **CommBuffer
+  )
+{
+  EFI_STATUS                                Status;
+  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE   *PiSmmCommunicationRegionTable;
+  EFI_MEMORY_DESCRIPTOR                     *Entry;
+  UINTN                                     EntrySize;
+  UINT32                                    Index;
+
+  if (CommBuffer == NULL || CommBufferSize == NULL || *CommBufferSize == 0) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = EfiGetSystemConfigurationTable (
+             &gEdkiiPiSmmCommunicationRegionTableGuid,
+             (VOID **) &PiSmmCommunicationRegionTable
+             );
+  if (EFI_ERROR (Status) || PiSmmCommunicationRegionTable == NULL) {
+    return EFI_NOT_FOUND;
+  }
+
+  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable + 1);
+  EntrySize = 0;
+  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {
+    if (Entry->Type == EfiConventionalMemory) {
+      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
+      if (EntrySize >= *CommBufferSize) {
+        break;
+      }
+    }
+    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry + PiSmmCommunicationRegionTable->DescriptorSize);
+  }
+
+  if (Index < PiSmmCommunicationRegionTable->NumberOfEntries) {
+    *CommBufferSize = EntrySize;
+    *CommBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;
+    return EFI_SUCCESS;
+  }
+
+  return EFI_OUT_OF_RESOURCES;
+}
+
 /**
   Send the data in communicate buffer to SMM.
 
@@ -424,6 +568,171 @@ Done:
   return Status;
 }
 
+/**
+  Signals SMM to synchronize any pending variable updates with the runtime cache(s).
+
+**/
+VOID
+EFIAPI
+SyncRuntimeCache (
+  VOID
+  )
+{
+  //
+  // Init the communicate buffer. The buffer data size is:
+  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
+  //
+  InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
+
+  //
+  // Send data to SMM.
+  //
+  SendCommunicateBuffer (0);
+}
+
+/**
+  Check whether a SMI must be triggered to retrieve pending cache updates.
+
+  If the variable HOB was finished being flushed since the last check for a runtime cache update, this function
+  will prevent the HOB cache from being used for future runtime cache hits.
+
+**/
+VOID
+EFIAPI
+CheckForRuntimeCacheSync (
+  VOID
+  )
+{
+  if (mVariableRuntimeCachePendingUpdate) {
+    SyncRuntimeCache ();
+  }
+  ASSERT (!mVariableRuntimeCachePendingUpdate);
+
+  //
+  // The HOB variable data may have finished being flushed in the runtime cache sync update
+  //
+  if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer != NULL) {
+    if (!AtRuntime ()) {
+      FreePool (mVariableRuntimeHobCacheBuffer);
+    }
+    mVariableRuntimeHobCacheBuffer = NULL;
+  }
+}
+
+/**
+  This code finds variable in a volatile memory store.
+
+  Caution: This function may receive untrusted input.
+  The data size is external input, so this function will validate it carefully to avoid buffer overflow.
+
+  @param[in]      VariableName       Name of Variable to be found.
+  @param[in]      VendorGuid         Variable vendor GUID.
+  @param[out]     Attributes         Attribute value of the variable found.
+  @param[in, out] DataSize           Size of Data found. If size is less than the
+                                     data, this value contains the required size.
+  @param[out]     Data               Data pointer.
+
+  @retval EFI_SUCCESS                Found the specified variable.
+  @retval EFI_INVALID_PARAMETER      Invalid parameter.
+  @retval EFI_NOT_FOUND              The specified variable could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+FindVariableInRuntimeCache (
+  IN      CHAR16                            *VariableName,
+  IN      EFI_GUID                          *VendorGuid,
+  OUT     UINT32                            *Attributes OPTIONAL,
+  IN OUT  UINTN                             *DataSize,
+  OUT     VOID                              *Data OPTIONAL
+  )
+{
+  EFI_STATUS              Status;
+  UINTN                   DelayIndex;
+  UINTN                   TempDataSize;
+  VARIABLE_POINTER_TRACK  RtPtrTrack;
+  VARIABLE_STORE_TYPE     StoreType;
+  VARIABLE_STORE_HEADER   *VariableStoreList[VariableStoreTypeMax];
+
+  Status = EFI_NOT_FOUND;
+
+  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
+    MicroSecondDelay (10);
+  }
+  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
+    ASSERT (!mVariableRuntimeCacheReadLock);
+
+    mVariableRuntimeCacheReadLock = TRUE;
+    CheckForRuntimeCacheSync ();
+
+    if (!mVariableRuntimeCachePendingUpdate) {
+      //
+      // 0: Volatile, 1: HOB, 2: Non-Volatile.
+      // The index and attributes mapping must be kept in this order as FindVariable
+      // makes use of this mapping to implement search algorithm.
+      //
+      VariableStoreList[VariableStoreTypeVolatile] = mVariableRuntimeVolatileCacheBuffer;
+      VariableStoreList[VariableStoreTypeHob]      = mVariableRuntimeHobCacheBuffer;
+      VariableStoreList[VariableStoreTypeNv]       = mVariableRuntimeNvCacheBuffer;
+
+      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTypeMax; StoreType++) {
+        if (VariableStoreList[StoreType] == NULL) {
+          continue;
+        }
+
+        RtPtrTrack.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
+        RtPtrTrack.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
+        RtPtrTrack.Volatile = (BOOLEAN) (StoreType == VariableStoreTypeVolatile);
+
+        Status = FindVariableEx (VariableName, VendorGuid, FALSE, &RtPtrTrack);
+        if (!EFI_ERROR (Status)) {
+          break;
+        }
+      }
+
+      if (!EFI_ERROR (Status)) {
+        //
+        // Get data size
+        //
+        TempDataSize = DataSizeOfVariable (RtPtrTrack.CurrPtr);
+        ASSERT (TempDataSize != 0);
+
+        if (*DataSize >= TempDataSize) {
+          if (Data == NULL) {
+            Status = EFI_INVALID_PARAMETER;
+            goto Done;
+          }
+
+          CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr), TempDataSize);
+          if (Attributes != NULL) {
+            *Attributes = RtPtrTrack.CurrPtr->Attributes;
+          }
+
+          *DataSize = TempDataSize;
+
+          UpdateVariableInfo (VariableName, VendorGuid, RtPtrTrack.Volatile, TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
+
+          Status = EFI_SUCCESS;
+          goto Done;
+        } else {
+          *DataSize = TempDataSize;
+          Status = EFI_BUFFER_TOO_SMALL;
+          goto Done;
+        }
+      }
+    }
+  }
+
+Done:
+  mVariableRuntimeCacheReadLock = FALSE;
+
+  return Status;
+}
+
 /**
   This code finds variable in storage blocks (Volatile or Non-Volatile).
 
@@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
   )
 {
   EFI_STATUS                                Status;
-  UINTN                                     PayloadSize;
-  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE  *SmmVariableHeader;
-  UINTN                                     TempDataSize;
-  UINTN                                     VariableNameSize;
 
   if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
     return EFI_INVALID_PARAMETER;
   }
-
-  TempDataSize          = *DataSize;
-  VariableNameSize      = StrSize (VariableName);
-  SmmVariableHeader     = NULL;
-
-  //
-  // If VariableName exceeds SMM payload limit. Return failure
-  //
-  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
-
-  //
-  // Init the communicate buffer. The buffer data size is:
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
-  //
-  if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {
-    //
-    // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
-    //
-    TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;
-  }
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;
-
-  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
-  if (EFI_ERROR (Status)) {
-    goto Done;
-  }
-  ASSERT (SmmVariableHeader != NULL);
-
-  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
-  SmmVariableHeader->DataSize   = TempDataSize;
-  SmmVariableHeader->NameSize   = VariableNameSize;
-  if (Attributes == NULL) {
-    SmmVariableHeader->Attributes = 0;
-  } else {
-    SmmVariableHeader->Attributes = *Attributes;
-  }
-  CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);
-
-  //
-  // Send data to SMM.
-  //
-  Status = SendCommunicateBuffer (PayloadSize);
-
-  //
-  // Get data from SMM.
-  //
-  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
-    //
-    // SMM CommBuffer DataSize can be a trimed value
-    // Only update DataSize when needed
-    //
-    *DataSize = SmmVariableHeader->DataSize;
-  }
-  if (Attributes != NULL) {
-    *Attributes = SmmVariableHeader->Attributes;
-  }
-
-  if (EFI_ERROR (Status)) {
-    goto Done;
-  }
-
-  if (Data != NULL) {
-    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
-  } else {
-    Status = EFI_INVALID_PARAMETER;
+  if (VariableName[0] == 0) {
+    return EFI_NOT_FOUND;
   }
 
-Done:
+  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
+  Status =  FindVariableInRuntimeCache (VariableName, VendorGuid, Attributes, DataSize, Data);
   ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
+
   return Status;
 }
 
-
 /**
   This code Finds the Next available variable.
 
@@ -870,6 +1109,17 @@ OnReadyToBoot (
   //
   SendCommunicateBuffer (0);
 
+  //
+  // Install the system configuration table for variable info data captured
+  //
+  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
+    if (mVariableAuthFormat) {
+      gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, mVariableInfo);
+    } else {
+      gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo);
+    }
+  }
+
   gBS->CloseEvent (Event);
 }
 
@@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
 {
   EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
   EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
+  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeHobCacheBuffer);
+  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeNvCacheBuffer);
+  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeVolatileCacheBuffer);
 }
 
 /**
@@ -969,6 +1222,173 @@ Done:
   return Status;
 }
 
+/**
+  This code gets information needed from SMM for runtime cache initialization.
+
+  @param[out] TotalHobStorageSize         Output pointer for the total HOB storage size in bytes.
+  @param[out] TotalNvStorageSize          Output pointer for the total non-volatile storage size in bytes.
+  @param[out] TotalVolatileStorageSize    Output pointer for the total volatile storage size in bytes.
+  @param[out] AuthenticatedVariableUsage  Output pointer that indicates if authenticated variables are to be used.
+
+  @retval EFI_SUCCESS                     Retrieved the size successfully.
+  @retval EFI_INVALID_PARAMETER           TotalNvStorageSize parameter is NULL.
+  @retval EFI_OUT_OF_RESOURCES            Could not allocate a CommBuffer.
+  @retval Others                          Could not retrieve the size successfully.;
+
+**/
+EFI_STATUS
+EFIAPI
+GetRuntimeCacheInfo (
+  OUT UINTN                         *TotalHobStorageSize,
+  OUT UINTN                         *TotalNvStorageSize,
+  OUT UINTN                         *TotalVolatileStorageSize,
+  OUT BOOLEAN                       *AuthenticatedVariableUsage
+  )
+{
+  EFI_STATUS                                          Status;
+  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO     *SmmGetRuntimeCacheInfo;
+  EFI_SMM_COMMUNICATE_HEADER                          *SmmCommunicateHeader;
+  SMM_VARIABLE_COMMUNICATE_HEADER                     *SmmVariableFunctionHeader;
+  UINTN                                               CommSize;
+  UINTN                                               CommBufferSize;
+  UINT8                                               *CommBuffer;
+
+  SmmGetRuntimeCacheInfo = NULL;
+  CommBuffer = NULL;
+
+  if (TotalHobStorageSize == NULL || TotalNvStorageSize == NULL || TotalVolatileStorageSize == NULL || AuthenticatedVariableUsage == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
+
+  CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
+  CommBufferSize = CommSize;
+  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+  if (CommBuffer == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Done;
+  }
+  ZeroMem (CommBuffer, CommBufferSize);
+
+  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;
+  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
+  SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
+
+  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;
+  SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
+  SmmGetRuntimeCacheInfo = (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *) SmmVariableFunctionHeader->Data;
+
+  //
+  // Send data to SMM.
+  //
+  Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);
+  ASSERT_EFI_ERROR (Status);
+  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
+    Status = EFI_BAD_BUFFER_SIZE;
+    goto Done;
+  }
+
+  Status = SmmVariableFunctionHeader->ReturnStatus;
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+
+  //
+  // Get data from SMM.
+  //
+  *TotalHobStorageSize = SmmGetRuntimeCacheInfo->TotalHobStorageSize;
+  *TotalNvStorageSize = SmmGetRuntimeCacheInfo->TotalNvStorageSize;
+  *TotalVolatileStorageSize = SmmGetRuntimeCacheInfo->TotalVolatileStorageSize;
+  *AuthenticatedVariableUsage = SmmGetRuntimeCacheInfo->AuthenticatedVariableUsage;
+
+Done:
+  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
+  return Status;
+}
+
+/**
+  Sends the runtime variable cache context information to SMM.
+
+  @retval EFI_SUCCESS               Retrieved the size successfully.
+  @retval EFI_INVALID_PARAMETER     TotalNvStorageSize parameter is NULL.
+  @retval EFI_OUT_OF_RESOURCES      Could not allocate a CommBuffer.
+  @retval Others                    Could not retrieve the size successfully.;
+
+**/
+EFI_STATUS
+EFIAPI
+SendRuntimeVariableCacheContextToSmm (
+  VOID
+  )
+{
+  EFI_STATUS                                                Status;
+  SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT   *SmmRuntimeVarCacheContext;
+  EFI_SMM_COMMUNICATE_HEADER                                *SmmCommunicateHeader;
+  SMM_VARIABLE_COMMUNICATE_HEADER                           *SmmVariableFunctionHeader;
+  UINTN                                                     CommSize;
+  UINTN                                                     CommBufferSize;
+  UINT8                                                     *CommBuffer;
+
+  SmmRuntimeVarCacheContext = NULL;
+  CommBuffer = NULL;
+
+  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
+
+  //
+  // Init the communicate buffer. The buffer data size is:
+  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
+  //
+  CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
+  CommBufferSize = CommSize;
+  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+  if (CommBuffer == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Done;
+  }
+  ZeroMem (CommBuffer, CommBufferSize);
+
+  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;
+  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
+  SmmCommunicateHeader->MessageLength = SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
+
+  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;
+  SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT;
+  SmmRuntimeVarCacheContext = (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *) SmmVariableFunctionHeader->Data;
+
+  SmmRuntimeVarCacheContext->RuntimeHobCache = mVariableRuntimeHobCacheBuffer;
+  SmmRuntimeVarCacheContext->RuntimeVolatileCache = mVariableRuntimeVolatileCacheBuffer;
+  SmmRuntimeVarCacheContext->RuntimeNvCache = mVariableRuntimeNvCacheBuffer;
+  SmmRuntimeVarCacheContext->PendingUpdate = &mVariableRuntimeCachePendingUpdate;
+  SmmRuntimeVarCacheContext->ReadLock = &mVariableRuntimeCacheReadLock;
+  SmmRuntimeVarCacheContext->HobFlushComplete = &mHobFlushComplete;
+
+  //
+  // Send data to SMM.
+  //
+  Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);
+  ASSERT_EFI_ERROR (Status);
+  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
+    Status = EFI_BAD_BUFFER_SIZE;
+    goto Done;
+  }
+
+  Status = SmmVariableFunctionHeader->ReturnStatus;
+  if (EFI_ERROR (Status)) {
+    goto Done;
+  }
+
+Done:
+  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
+  return Status;
+}
+
 /**
   Initialize variable service and install Variable Architectural protocol.
 
@@ -985,7 +1405,7 @@ SmmVariableReady (
 {
   EFI_STATUS                                Status;
 
-  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);
+  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **) &mSmmVariable);
   if (EFI_ERROR (Status)) {
     return;
   }
@@ -1007,6 +1427,40 @@ SmmVariableReady (
   //
   mVariableBufferPhysical = mVariableBuffer;
 
+  //
+  // Allocate runtime variable cache memory buffers.
+  //
+  Status =  GetRuntimeCacheInfo (
+              &mVariableRuntimeHobCacheBufferSize,
+              &mVariableRuntimeNvCacheBufferSize,
+              &mVariableRuntimeVolatileCacheBufferSize,
+              &mVariableAuthFormat
+              );
+  if (!EFI_ERROR (Status)) {
+    Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer, &mVariableRuntimeHobCacheBufferSize);
+    if (!EFI_ERROR (Status)) {
+      Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer, &mVariableRuntimeNvCacheBufferSize);
+      if (!EFI_ERROR (Status)) {
+        Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer, &mVariableRuntimeVolatileCacheBufferSize);
+        if (!EFI_ERROR (Status)) {
+          Status = InitVariableParsing (mVariableAuthFormat);
+          ASSERT_EFI_ERROR (Status);
+
+          Status = SendRuntimeVariableCacheContextToSmm ();
+          if (!EFI_ERROR (Status)) {
+            SyncRuntimeCache ();
+          }
+        }
+      }
+    }
+    if (EFI_ERROR (Status)) {
+      mVariableRuntimeHobCacheBuffer = NULL;
+      mVariableRuntimeNvCacheBuffer = NULL;
+      mVariableRuntimeVolatileCacheBuffer = NULL;
+    }
+  }
+  ASSERT_EFI_ERROR (Status);
+
   gRT->GetVariable         = RuntimeServiceGetVariable;
   gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
   gRT->SetVariable         = RuntimeServiceSetVariable;
-- 
2.16.2.windows.1


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

* [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (6 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  2019-10-03  8:04   ` Wu, Hao A
  2019-09-28  1:47 ` [PATCH V2 9/9] MdeModulePkg/VariableSmm: Remove unused SMI handler functions Kubacki, Michael A
  8 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

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

This change implements the Runtime Service GetNextVariableName()
using the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service
calls to GetNextVariableName() will no longer trigger a SW SMI.

Overall system performance and stability will be improved by
eliminating an SMI for these calls as they typically result in a
relatively large number of invocations to retrieve all variable
names in all variable stores present.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 118 +++++++++-----------
 1 file changed, 50 insertions(+), 68 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 46f69765a4..bc3b56b0ce 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName (
   IN OUT  EFI_GUID                          *VendorGuid
   )
 {
-  EFI_STATUS                                      Status;
-  UINTN                                           PayloadSize;
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;
-  UINTN                                           OutVariableNameSize;
-  UINTN                                           InVariableNameSize;
+  EFI_STATUS              Status;
+  UINTN                   DelayIndex;
+  UINTN                   MaxLen;
+  UINTN                   VarNameSize;
+  VARIABLE_HEADER         *VariablePtr;
+  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
+
+  Status = EFI_NOT_FOUND;
 
   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
     return EFI_INVALID_PARAMETER;
   }
 
-  OutVariableNameSize   = *VariableNameSize;
-  InVariableNameSize    = StrSize (VariableName);
-  SmmGetNextVariableName = NULL;
-
   //
-  // If input string exceeds SMM payload limit. Return failure
+  // Calculate the possible maximum length of name string, including the Null terminator.
   //
-  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
+  MaxLen = *VariableNameSize / sizeof (CHAR16);
+  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {
+    //
+    // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
+    // follow spec to return EFI_INVALID_PARAMETER.
+    //
     return EFI_INVALID_PARAMETER;
   }
 
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
+  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
 
-  //
-  // Init the communicate buffer. The buffer data size is:
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
-  //
-  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
-    //
-    // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
-    //
-    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
+  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
+    MicroSecondDelay (10);
   }
-  //
-  // Payload should be Guid + NameSize + MAX of Input & Output buffer
-  //
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);
-
-  Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
-  if (EFI_ERROR (Status)) {
-    goto Done;
-  }
-  ASSERT (SmmGetNextVariableName != NULL);
-
-  //
-  // SMM comm buffer->NameSize is buffer size for return string
-  //
-  SmmGetNextVariableName->NameSize = OutVariableNameSize;
-
-  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
-  //
-  // Copy whole string
-  //
-  CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);
-  if (OutVariableNameSize > InVariableNameSize) {
-    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);
-  }
-
-  //
-  // Send data to SMM
-  //
-  Status = SendCommunicateBuffer (PayloadSize);
-
-  //
-  // Get data from SMM.
-  //
-  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
-    //
-    // SMM CommBuffer NameSize can be a trimed value
-    // Only update VariableNameSize when needed
-    //
-    *VariableNameSize = SmmGetNextVariableName->NameSize;
-  }
-  if (EFI_ERROR (Status)) {
-    goto Done;
+  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
+    ASSERT (!mVariableRuntimeCacheReadLock);
+
+    CheckForRuntimeCacheSync ();
+    mVariableRuntimeCacheReadLock = TRUE;
+
+    if (!mVariableRuntimeCachePendingUpdate) {
+      //
+      // 0: Volatile, 1: HOB, 2: Non-Volatile.
+      // The index and attributes mapping must be kept in this order as FindVariable
+      // makes use of this mapping to implement search algorithm.
+      //
+      VariableStoreHeader[VariableStoreTypeVolatile] = mVariableRuntimeVolatileCacheBuffer;
+      VariableStoreHeader[VariableStoreTypeHob]      = mVariableRuntimeHobCacheBuffer;
+      VariableStoreHeader[VariableStoreTypeNv]       = mVariableRuntimeNvCacheBuffer;
+
+      Status = GetNextVariableEx (VariableName, VendorGuid, VariableStoreHeader, &VariablePtr);
+      if (!EFI_ERROR (Status)) {
+        VarNameSize = NameSizeOfVariable (VariablePtr);
+        ASSERT (VarNameSize != 0);
+        if (VarNameSize <= *VariableNameSize) {
+          CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);
+          CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));
+          Status = EFI_SUCCESS;
+        } else {
+          Status = EFI_BUFFER_TOO_SMALL;
+        }
+
+        *VariableNameSize = VarNameSize;
+      }
+    }
   }
-
-  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
-  CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);
-
-Done:
+  mVariableRuntimeCacheReadLock = FALSE;
   ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
   return Status;
 }
-- 
2.16.2.windows.1


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

* [PATCH V2 9/9] MdeModulePkg/VariableSmm: Remove unused SMI handler functions
  2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
                   ` (7 preceding siblings ...)
  2019-09-28  1:47 ` [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() " Kubacki, Michael A
@ 2019-09-28  1:47 ` Kubacki, Michael A
  8 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-09-28  1:47 UTC (permalink / raw)
  To: devel
  Cc: Dandan Bi, Ard Biesheuvel, Eric Dong, Laszlo Ersek, Liming Gao,
	Michael D Kinney, Ray Ni, Jian J Wang, Hao A Wu, Jiewen Yao

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220

Since Runtime Services GetVariable() and GetNextVariableName() no longer
trigger a SW SMI, this change removes the code for handling those requests
from VariableSmm.c.

The following SMM communicate functions are removed from
SmmVariableHandler():

1. SMM_VARIABLE_FUNCTION_GET_VARIABLE
2. SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME

The function numbers are retained so any calls to those functions from
previously built drivers will return EFI_UNSUPPORTED.

Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
---
 MdeModulePkg/Include/Guid/SmmVariableCommon.h            |   4 +-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 101 --------------------
 2 files changed, 1 insertion(+), 104 deletions(-)

diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
index ceef44dfd2..6a73d1e21a 100644
--- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
+++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
@@ -106,7 +106,7 @@ typedef struct {
   EFI_GUID    Guid;
   UINTN       NameSize;     // Return name buffer size
   CHAR16      Name[1];
-} SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME;
+} SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE;
 
 ///
 /// This structure is used to communicate with SMI handler by QueryVariableInfo.
@@ -118,8 +118,6 @@ typedef struct {
   UINT32          Attributes;
 } SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO;
 
-typedef SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE;
-
 typedef struct {
   EFI_GUID                      Guid;
   UINTN                         NameSize;
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
index 8d767f75ac..8ba167f889 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
@@ -463,7 +463,6 @@ SmmVariableHandler (
   EFI_STATUS                                              Status;
   SMM_VARIABLE_COMMUNICATE_HEADER                         *SmmVariableFunctionHeader;
   SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE                *SmmVariableHeader;
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME         *GetNextVariableName;
   SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO            *QueryVariableInfo;
   SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE               *GetPayloadSize;
   SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *RuntimeVariableCacheContext;
@@ -474,7 +473,6 @@ SmmVariableHandler (
   VARIABLE_RUNTIME_CACHE_CONTEXT                          *VariableCacheContext;
   VARIABLE_STORE_HEADER                                   *VariableCache;
   UINTN                                                   InfoSize;
-  UINTN                                                   NameBufferSize;
   UINTN                                                   CommBufferPayloadSize;
   UINTN                                                   TempCommBufferSize;
 
@@ -504,105 +502,6 @@ SmmVariableHandler (
 
   SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
   switch (SmmVariableFunctionHeader->Function) {
-    case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
-      if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
-        DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));
-        return EFI_SUCCESS;
-      }
-      //
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
-      //
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
-      SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;
-      if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||
-         ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {
-        //
-        // Prevent InfoSize overflow happen
-        //
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-      InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)
-                 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;
-
-      //
-      // SMRAM range check already covered before
-      //
-      if (InfoSize > CommBufferPayloadSize) {
-        DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-
-      //
-      // The VariableSpeculationBarrier() call here is to ensure the previous
-      // range/content checks for the CommBuffer have been completed before the
-      // subsequent consumption of the CommBuffer content.
-      //
-      VariableSpeculationBarrier ();
-      if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {
-        //
-        // Make sure VariableName is A Null-terminated string.
-        //
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-
-      Status = VariableServiceGetVariable (
-                 SmmVariableHeader->Name,
-                 &SmmVariableHeader->Guid,
-                 &SmmVariableHeader->Attributes,
-                 &SmmVariableHeader->DataSize,
-                 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
-                 );
-      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
-      break;
-
-    case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
-      if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
-        DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));
-        return EFI_SUCCESS;
-      }
-      //
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
-      //
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);
-      GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;
-      if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
-        //
-        // Prevent InfoSize overflow happen
-        //
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-      InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;
-
-      //
-      // SMRAM range check already covered before
-      //
-      if (InfoSize > CommBufferPayloadSize) {
-        DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-
-      NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
-      if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {
-        //
-        // Make sure input VariableName is A Null-terminated string.
-        //
-        Status = EFI_ACCESS_DENIED;
-        goto EXIT;
-      }
-
-      Status = VariableServiceGetNextVariableName (
-                 &GetNextVariableName->NameSize,
-                 GetNextVariableName->Name,
-                 &GetNextVariableName->Guid
-                 );
-      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);
-      break;
-
     case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
       if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
         DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));
-- 
2.16.2.windows.1


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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
@ 2019-10-03  8:03   ` Wu, Hao A
  2019-10-03 17:35     ` Kubacki, Michael A
  2019-10-08  6:07   ` Wang, Jian J
  1 sibling, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:03 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

A couple of inline comments below:


> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing functions
> 
> This change moves the following functions into a dedicated file
> so they may be used in other variable files as needed. Furthermore,
> it reduces the overall size of the common Variable.c file.
> 
>  * DataSizeOfVariable ()
>  * FindVariableEx ()
>  * GetEndPointer ()
>  * GetNextVariablePtr ()
>  * GetStartPointer ()
>  * GetVariableDataOffset ()
>  * GetVariableDataPtr ()
>  * GetVariableHeaderSize ()
>  * GetVariableNamePtr ()
>  * GetVariableStoreStatus ()
>  * GetVendorGuidPtr ()
>  * IsValidVariableHeader ()
>  * NameSizeOfVariable ()
>  * SetDataSizeOfVariable ()
>  * SetNameSizeOfVariable ()
>  * UpdateVariableInfo ()
>  * VariableCompareTimeStampInternal ()
>  * VariableServiceGetNextVariableInternal ()


May I know what are the criteria for the above functions being moved to a
separate file?

At first, I think all of them will be consumed by the new codes that implement
the runtime cache. But I found that, for functions like GetVariableDataOffset(),
GetVariableStoreStatus() and etc., their usages are still remained within file
Variable.c (seems not related with the runtime cache).


> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> |   2 +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |   2
> +
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> |   7 +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119 ----
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        | 306
> ++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726 +--
> ----------------
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |   3 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        | 731
> ++++++++++++++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |   1 +
>  9 files changed, 1052 insertions(+), 845 deletions(-)


For the below change in VariableStandaloneMm.inf:

[Guids]
...
  ## SOMETIMES_CONSUMES   ## Variable:L"db"
  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
  gEfiImageSecurityDatabaseGuid
...

I think the above GUID is not used by the module specified by the above INF.
Could you double confirm on this?

Best Regards,
Hao Wu


> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> index 641376c9c5..c35e5fe787 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> @@ -36,6 +36,8 @@
>    Variable.c
>    VariableDxe.c
>    Variable.h
> +  VariableParsing.c
> +  VariableParsing.h
>    PrivilegePolymorphic.h
>    Measurement.c
>    TcgMorLockDxe.c
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> index 0a160d269d..626738b9c7 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableTraditionalMm.c
>    VariableSmm.c
> +  VariableParsing.c
> +  VariableParsing.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> index 21bc81163b..1ba8f9ebfb 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableSmm.c
>    VariableStandaloneMm.c
> +  VariableParsing.c
> +  VariableParsing.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> @@ -99,6 +101,11 @@
>    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
>    gEfiGlobalVariableGuid
> 
> +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> +  gEfiImageSecurityDatabaseGuid
> +
>    gEfiMemoryOverwriteControlDataGuid            ## SOMETIMES_CONSUMES
> ## Variable:L"MemoryOverwriteRequestControl"
>    gEfiMemoryOverwriteRequestControlLockGuid     ##
> SOMETIMES_PRODUCES   ##
> Variable:L"MemoryOverwriteRequestControlLock"
> 
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> index 9eac43759f..fb574b2e32 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> @@ -179,89 +179,6 @@ FindVariable (
>    IN  BOOLEAN                 IgnoreRtCheck
>    );
> 
> -/**
> -
> -  Gets the pointer to the end of the variable storage area.
> -
> -  This function gets pointer to the end of the variable storage
> -  area, according to the input variable store header.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the end of the variable storage area.
> -
> -**/
> -VARIABLE_HEADER *
> -GetEndPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  );
> -
> -/**
> -  This code gets the size of variable header.
> -
> -  @return Size of variable header in bytes in type UINTN.
> -
> -**/
> -UINTN
> -GetVariableHeaderSize (
> -  VOID
> -  );
> -
> -/**
> -
> -  This code gets the pointer to the variable name.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Name which is Unicode encoding.
> -
> -**/
> -CHAR16 *
> -GetVariableNamePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
> -/**
> -  This code gets the pointer to the variable guid.
> -
> -  @param Variable   Pointer to the Variable Header.
> -
> -  @return A EFI_GUID* pointer to Vendor Guid.
> -
> -**/
> -EFI_GUID *
> -GetVendorGuidPtr (
> -  IN VARIABLE_HEADER    *Variable
> -  );
> -
> -/**
> -
> -  This code gets the pointer to the variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Data.
> -
> -**/
> -UINT8 *
> -GetVariableDataPtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
> -/**
> -
> -  This code gets the size of variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Size of variable in bytes.
> -
> -**/
> -UINTN
> -DataSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
>  /**
>    This function is to check if the remaining variable space is enough to set
>    all Variables from argument list successfully. The purpose of the check
> @@ -450,17 +367,6 @@ ReclaimForOS(
>    VOID
>    );
> 
> -/**
> -  Get non-volatile maximum variable size.
> -
> -  @return Non-volatile maximum variable size.
> -
> -**/
> -UINTN
> -GetNonVolatileMaxVariableSize (
> -  VOID
> -  );
> -
>  /**
>    Get maximum variable size, covering both non-volatile and volatile variables.
> 
> @@ -546,31 +452,6 @@ VariableServiceGetVariable (
>    OUT     VOID              *Data OPTIONAL
>    );
> 
> -/**
> -  This code Finds the Next available variable.
> -
> -  Caution: This function may receive untrusted input.
> -  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> -
> -  @param[in] VariableName   Pointer to variable name.
> -  @param[in] VendorGuid     Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> -
> -  @retval EFI_SUCCESS           The function completed successfully.
> -  @retval EFI_NOT_FOUND         The next variable was not found.
> -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> -                                GUID of an existing variable.
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -VariableServiceGetNextVariableInternal (
> -  IN  CHAR16                *VariableName,
> -  IN  EFI_GUID              *VendorGuid,
> -  OUT VARIABLE_HEADER       **VariablePtr
> -  );
> -
>  /**
> 
>    This code Finds the Next available variable.
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> new file mode 100644
> index 0000000000..9d77c4916c
> --- /dev/null
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> @@ -0,0 +1,306 @@
> +/** @file
> +  Functions in this module are associated with variable parsing operations
> and
> +  are intended to be usable across variable driver source files.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _VARIABLE_PARSING_H_
> +#define _VARIABLE_PARSING_H_
> +
> +#include <Guid/ImageAuthentication.h>
> +#include "Variable.h"
> +
> +/**
> +
> +  This code checks if variable header is valid or not.
> +
> +  @param Variable           Pointer to the Variable Header.
> +  @param VariableStoreEnd   Pointer to the Variable Store End.
> +
> +  @retval TRUE              Variable header is valid.
> +  @retval FALSE             Variable header is not valid.
> +
> +**/
> +BOOLEAN
> +IsValidVariableHeader (
> +  IN  VARIABLE_HEADER       *Variable,
> +  IN  VARIABLE_HEADER       *VariableStoreEnd
> +  );
> +
> +/**
> +
> +  This code gets the current status of Variable Store.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @retval EfiRaw         Variable store status is raw.
> +  @retval EfiValid       Variable store status is valid.
> +  @retval EfiInvalid     Variable store status is invalid.
> +
> +**/
> +VARIABLE_STORE_STATUS
> +GetVariableStoreStatus (
> +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> +  );
> +
> +/**
> +  This code gets the size of variable header.
> +
> +  @return Size of variable header in bytes in type UINTN.
> +
> +**/
> +UINTN
> +GetVariableHeaderSize (
> +  VOID
> +  );
> +
> +/**
> +
> +  This code gets the size of name of variable.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return UINTN          Size of variable in bytes.
> +
> +**/
> +UINTN
> +NameSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code sets the size of name of variable.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] NameSize   Name size to set.
> +
> +**/
> +VOID
> +SetNameSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              NameSize
> +  );
> +
> +/**
> +
> +  This code gets the size of variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Size of variable in bytes.
> +
> +**/
> +UINTN
> +DataSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code sets the size of variable data.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] DataSize   Data size to set.
> +
> +**/
> +VOID
> +SetDataSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              DataSize
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the variable name.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Name which is Unicode encoding.
> +
> +**/
> +CHAR16 *
> +GetVariableNamePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code gets the pointer to the variable guid.
> +
> +  @param Variable   Pointer to the Variable Header.
> +
> +  @return A EFI_GUID* pointer to Vendor Guid.
> +
> +**/
> +EFI_GUID *
> +GetVendorGuidPtr (
> +  IN VARIABLE_HEADER    *Variable
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Data.
> +
> +**/
> +UINT8 *
> +GetVariableDataPtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code gets the variable data offset related to variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Variable Data offset.
> +
> +**/
> +UINTN
> +GetVariableDataOffset (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the next variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to next variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetNextVariablePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +
> +  Gets the pointer to the first variable header in given variable store area.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the first variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetStartPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  );
> +
> +/**
> +
> +  Gets the pointer to the end of the variable storage area.
> +
> +  This function gets pointer to the end of the variable storage
> +  area, according to the input variable store header.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the end of the variable storage area.
> +
> +**/
> +VARIABLE_HEADER *
> +GetEndPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  );
> +
> +/**
> +  Compare two EFI_TIME data.
> +
> +
> +  @param FirstTime           A pointer to the first EFI_TIME data.
> +  @param SecondTime          A pointer to the second EFI_TIME data.
> +
> +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> +  @retval  FALSE             The FirstTime is later than the SecondTime.
> +
> +**/
> +BOOLEAN
> +VariableCompareTimeStampInternal (
> +  IN EFI_TIME               *FirstTime,
> +  IN EFI_TIME               *SecondTime
> +  );
> +
> +/**
> +  Find the variable in the specified variable store.
> +
> +  @param[in]       VariableName        Name of the variable to be found
> +  @param[in]       VendorGuid          Vendor GUID to be found.
> +  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> +                                       check at runtime when searching variable.
> +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains Variable Information.
> +
> +  @retval          EFI_SUCCESS         Variable found successfully
> +  @retval          EFI_NOT_FOUND       Variable not found
> +**/
> +EFI_STATUS
> +FindVariableEx (
> +  IN     CHAR16                  *VariableName,
> +  IN     EFI_GUID                *VendorGuid,
> +  IN     BOOLEAN                 IgnoreRtCheck,
> +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> +  );
> +
> +/**
> +  This code Finds the Next available variable.
> +
> +  Caution: This function may receive untrusted input.
> +  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> +
> +  @param[in]  VariableName  Pointer to variable name.
> +  @param[in]  VendorGuid    Variable Vendor Guid.
> +  @param[out] VariablePtr   Pointer to variable header address.
> +
> +  @retval EFI_SUCCESS           The function completed successfully.
> +  @retval EFI_NOT_FOUND         The next variable was not found.
> +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> +  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> +                                GUID of an existing variable.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +VariableServiceGetNextVariableInternal (
> +  IN  CHAR16                *VariableName,
> +  IN  EFI_GUID              *VendorGuid,
> +  OUT VARIABLE_HEADER       **VariablePtr
> +  );
> +
> +/**
> +  Routine used to track statistical information about variable usage.
> +  The data is stored in the EFI system table so it can be accessed later.
> +  VariableInfo.efi can dump out the table. Only Boot Services variable
> +  accesses are tracked by this code. The PcdVariableCollectStatistics
> +  build flag controls if this feature is enabled.
> +
> +  A read that hits in the cache will have Read and Cache true for
> +  the transaction. Data is allocated by this routine, but never
> +  freed.
> +
> +  @param[in] VariableName   Name of the Variable to track.
> +  @param[in] VendorGuid     Guid of the Variable to track.
> +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in] Read           TRUE if GetVariable() was called.
> +  @param[in] Write          TRUE if SetVariable() was called.
> +  @param[in] Delete         TRUE if deleted via SetVariable().
> +  @param[in] Cache          TRUE for a cache hit.
> +
> +**/
> +VOID
> +UpdateVariableInfo (
> +  IN  CHAR16                  *VariableName,
> +  IN  EFI_GUID                *VendorGuid,
> +  IN  BOOLEAN                 Volatile,
> +  IN  BOOLEAN                 Read,
> +  IN  BOOLEAN                 Write,
> +  IN  BOOLEAN                 Delete,
> +  IN  BOOLEAN                 Cache
> +  );
> +
> +#endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index f32c9c2808..76536308e6 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  **/
> 
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> 
> @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
> 
>  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> 
> -/**
> -  Routine used to track statistical information about variable usage.
> -  The data is stored in the EFI system table so it can be accessed later.
> -  VariableInfo.efi can dump out the table. Only Boot Services variable
> -  accesses are tracked by this code. The PcdVariableCollectStatistics
> -  build flag controls if this feature is enabled.
> -
> -  A read that hits in the cache will have Read and Cache true for
> -  the transaction. Data is allocated by this routine, but never
> -  freed.
> -
> -  @param[in] VariableName   Name of the Variable to track.
> -  @param[in] VendorGuid     Guid of the Variable to track.
> -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> -  @param[in] Read           TRUE if GetVariable() was called.
> -  @param[in] Write          TRUE if SetVariable() was called.
> -  @param[in] Delete         TRUE if deleted via SetVariable().
> -  @param[in] Cache          TRUE for a cache hit.
> -
> -**/
> -VOID
> -UpdateVariableInfo (
> -  IN  CHAR16                  *VariableName,
> -  IN  EFI_GUID                *VendorGuid,
> -  IN  BOOLEAN                 Volatile,
> -  IN  BOOLEAN                 Read,
> -  IN  BOOLEAN                 Write,
> -  IN  BOOLEAN                 Delete,
> -  IN  BOOLEAN                 Cache
> -  )
> -{
> -  VARIABLE_INFO_ENTRY   *Entry;
> -
> -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> -
> -    if (AtRuntime ()) {
> -      // Don't collect statistics at runtime.
> -      return;
> -    }
> -
> -    if (gVariableInfo == NULL) {
> -      //
> -      // On the first call allocate a entry and place a pointer to it in
> -      // the EFI System Table.
> -      //
> -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> -      ASSERT (gVariableInfo != NULL);
> -
> -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> -      ASSERT (gVariableInfo->Name != NULL);
> -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> -      gVariableInfo->Volatile = Volatile;
> -    }
> -
> -
> -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> -        if (StrCmp (VariableName, Entry->Name) == 0) {
> -          if (Read) {
> -            Entry->ReadCount++;
> -          }
> -          if (Write) {
> -            Entry->WriteCount++;
> -          }
> -          if (Delete) {
> -            Entry->DeleteCount++;
> -          }
> -          if (Cache) {
> -            Entry->CacheCount++;
> -          }
> -
> -          return;
> -        }
> -      }
> -
> -      if (Entry->Next == NULL) {
> -        //
> -        // If the entry is not in the table add it.
> -        // Next iteration of the loop will fill in the data.
> -        //
> -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> -        ASSERT (Entry->Next != NULL);
> -
> -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> -        ASSERT (Entry->Next->Name != NULL);
> -        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> -        Entry->Next->Volatile = Volatile;
> -      }
> -
> -    }
> -  }
> -}
> -
> -
> -/**
> -
> -  This code checks if variable header is valid or not.
> -
> -  @param Variable           Pointer to the Variable Header.
> -  @param VariableStoreEnd   Pointer to the Variable Store End.
> -
> -  @retval TRUE              Variable header is valid.
> -  @retval FALSE             Variable header is not valid.
> -
> -**/
> -BOOLEAN
> -IsValidVariableHeader (
> -  IN  VARIABLE_HEADER       *Variable,
> -  IN  VARIABLE_HEADER       *VariableStoreEnd
> -  )
> -{
> -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> >StartId != VARIABLE_DATA)) {
> -    //
> -    // Variable is NULL or has reached the end of variable store,
> -    // or the StartId is not correct.
> -    //
> -    return FALSE;
> -  }
> -
> -  return TRUE;
> -}
> -
> -
>  /**
> 
>    This function writes data to the FWH at the correct LBA even if the LBAs
> @@ -376,345 +252,6 @@ UpdateVariableStore (
>    return EFI_SUCCESS;
>  }
> 
> -
> -/**
> -
> -  This code gets the current status of Variable Store.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @retval EfiRaw         Variable store status is raw.
> -  @retval EfiValid       Variable store status is valid.
> -  @retval EfiInvalid     Variable store status is invalid.
> -
> -**/
> -VARIABLE_STORE_STATUS
> -GetVariableStoreStatus (
> -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> -  )
> -{
> -  if ((CompareGuid (&VarStoreHeader->Signature,
> &gEfiAuthenticatedVariableGuid) ||
> -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> -      ) {
> -
> -    return EfiValid;
> -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> -             VarStoreHeader->Size == 0xffffffff &&
> -             VarStoreHeader->Format == 0xff &&
> -             VarStoreHeader->State == 0xff
> -          ) {
> -
> -    return EfiRaw;
> -  } else {
> -    return EfiInvalid;
> -  }
> -}
> -
> -/**
> -  This code gets the size of variable header.
> -
> -  @return Size of variable header in bytes in type UINTN.
> -
> -**/
> -UINTN
> -GetVariableHeaderSize (
> -  VOID
> -  )
> -{
> -  UINTN Value;
> -
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> -  } else {
> -    Value = sizeof (VARIABLE_HEADER);
> -  }
> -
> -  return Value;
> -}
> -
> -/**
> -
> -  This code gets the size of name of variable.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return UINTN          Size of variable in bytes.
> -
> -**/
> -UINTN
> -NameSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    if (AuthVariable->State == (UINT8) (-1) ||
> -       AuthVariable->DataSize == (UINT32) (-1) ||
> -       AuthVariable->NameSize == (UINT32) (-1) ||
> -       AuthVariable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) AuthVariable->NameSize;
> -  } else {
> -    if (Variable->State == (UINT8) (-1) ||
> -        Variable->DataSize == (UINT32) (-1) ||
> -        Variable->NameSize == (UINT32) (-1) ||
> -        Variable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) Variable->NameSize;
> -  }
> -}
> -
> -/**
> -  This code sets the size of name of variable.
> -
> -  @param[in] Variable   Pointer to the Variable Header.
> -  @param[in] NameSize   Name size to set.
> -
> -**/
> -VOID
> -SetNameSizeOfVariable (
> -  IN VARIABLE_HEADER    *Variable,
> -  IN UINTN              NameSize
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    AuthVariable->NameSize = (UINT32) NameSize;
> -  } else {
> -    Variable->NameSize = (UINT32) NameSize;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the size of variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Size of variable in bytes.
> -
> -**/
> -UINTN
> -DataSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    if (AuthVariable->State == (UINT8) (-1) ||
> -       AuthVariable->DataSize == (UINT32) (-1) ||
> -       AuthVariable->NameSize == (UINT32) (-1) ||
> -       AuthVariable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) AuthVariable->DataSize;
> -  } else {
> -    if (Variable->State == (UINT8) (-1) ||
> -        Variable->DataSize == (UINT32) (-1) ||
> -        Variable->NameSize == (UINT32) (-1) ||
> -        Variable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) Variable->DataSize;
> -  }
> -}
> -
> -/**
> -  This code sets the size of variable data.
> -
> -  @param[in] Variable   Pointer to the Variable Header.
> -  @param[in] DataSize   Data size to set.
> -
> -**/
> -VOID
> -SetDataSizeOfVariable (
> -  IN VARIABLE_HEADER    *Variable,
> -  IN UINTN              DataSize
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    AuthVariable->DataSize = (UINT32) DataSize;
> -  } else {
> -    Variable->DataSize = (UINT32) DataSize;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the pointer to the variable name.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Name which is Unicode encoding.
> -
> -**/
> -CHAR16 *
> -GetVariableNamePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> -}
> -
> -/**
> -  This code gets the pointer to the variable guid.
> -
> -  @param Variable   Pointer to the Variable Header.
> -
> -  @return A EFI_GUID* pointer to Vendor Guid.
> -
> -**/
> -EFI_GUID *
> -GetVendorGuidPtr (
> -  IN VARIABLE_HEADER    *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    return &AuthVariable->VendorGuid;
> -  } else {
> -    return &Variable->VendorGuid;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the pointer to the variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Data.
> -
> -**/
> -UINT8 *
> -GetVariableDataPtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  //
> -  // Be careful about pad size for alignment.
> -  //
> -  Value =  (UINTN) GetVariableNamePtr (Variable);
> -  Value += NameSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> -
> -  return (UINT8 *) Value;
> -}
> -
> -/**
> -  This code gets the variable data offset related to variable header.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Variable Data offset.
> -
> -**/
> -UINTN
> -GetVariableDataOffset (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  //
> -  // Be careful about pad size for alignment
> -  //
> -  Value = GetVariableHeaderSize ();
> -  Value += NameSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> -
> -  return Value;
> -}
> -
> -/**
> -
> -  This code gets the pointer to the next variable header.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to next variable header.
> -
> -**/
> -VARIABLE_HEADER *
> -GetNextVariablePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  Value =  (UINTN) GetVariableDataPtr (Variable);
> -  Value += DataSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> -
> -  //
> -  // Be careful about pad size for alignment.
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> -}
> -
> -/**
> -
> -  Gets the pointer to the first variable header in given variable store area.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the first variable header.
> -
> -**/
> -VARIABLE_HEADER *
> -GetStartPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  )
> -{
> -  //
> -  // The start of variable store.
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> -}
> -
> -/**
> -
> -  Gets the pointer to the end of the variable storage area.
> -
> -  This function gets pointer to the end of the variable storage
> -  area, according to the input variable store header.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the end of the variable storage area.
> -
> -**/
> -VARIABLE_HEADER *
> -GetEndPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  )
> -{
> -  //
> -  // The end of variable store
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader +
> VarStoreHeader->Size);
> -}
> -
>  /**
>    Record variable error flag.
> 
> @@ -1228,75 +765,6 @@ Done:
>    return Status;
>  }
> 
> -/**
> -  Find the variable in the specified variable store.
> -
> -  @param[in]       VariableName        Name of the variable to be found
> -  @param[in]       VendorGuid          Vendor GUID to be found.
> -  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> -                                       check at runtime when searching variable.
> -  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains Variable Information.
> -
> -  @retval          EFI_SUCCESS         Variable found successfully
> -  @retval          EFI_NOT_FOUND       Variable not found
> -**/
> -EFI_STATUS
> -FindVariableEx (
> -  IN     CHAR16                  *VariableName,
> -  IN     EFI_GUID                *VendorGuid,
> -  IN     BOOLEAN                 IgnoreRtCheck,
> -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> -  )
> -{
> -  VARIABLE_HEADER                *InDeletedVariable;
> -  VOID                           *Point;
> -
> -  PtrTrack->InDeletedTransitionPtr = NULL;
> -
> -  //
> -  // Find the variable by walk through HOB, volatile and non-volatile variable
> store.
> -  //
> -  InDeletedVariable  = NULL;
> -
> -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> -      ) {
> -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)
> -       ) {
> -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> -        if (VariableName[0] == 0) {
> -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -            InDeletedVariable   = PtrTrack->CurrPtr;
> -          } else {
> -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> -            return EFI_SUCCESS;
> -          }
> -        } else {
> -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr)))
> {
> -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> -
> -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> -            if (CompareMem (VariableName, Point, NameSizeOfVariable
> (PtrTrack->CurrPtr)) == 0) {
> -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -                InDeletedVariable     = PtrTrack->CurrPtr;
> -              } else {
> -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> -                return EFI_SUCCESS;
> -              }
> -            }
> -          }
> -        }
> -      }
> -    }
> -  }
> -
> -  PtrTrack->CurrPtr = InDeletedVariable;
> -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> -}
> -
> -
>  /**
>    Finds variable in storage blocks of volatile and non-volatile storage areas.
> 
> @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
>    }
>  }
> 
> -/**
> -  Compare two EFI_TIME data.
> -
> -
> -  @param FirstTime           A pointer to the first EFI_TIME data.
> -  @param SecondTime          A pointer to the second EFI_TIME data.
> -
> -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> -  @retval  FALSE             The FirstTime is later than the SecondTime.
> -
> -**/
> -BOOLEAN
> -VariableCompareTimeStampInternal (
> -  IN EFI_TIME               *FirstTime,
> -  IN EFI_TIME               *SecondTime
> -  )
> -{
> -  if (FirstTime->Year != SecondTime->Year) {
> -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> -  } else if (FirstTime->Month != SecondTime->Month) {
> -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> -  } else if (FirstTime->Day != SecondTime->Day) {
> -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> -  } else if (FirstTime->Hour != SecondTime->Hour) {
> -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> -  } else if (FirstTime->Minute != SecondTime->Minute) {
> -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> -  }
> -
> -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> -}
> -
>  /**
>    Update the variable region with Variable information. If
> EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
>    index of associated public key is needed.
> @@ -2885,166 +2321,6 @@ Done:
>    return Status;
>  }
> 
> -/**
> -  This code Finds the Next available variable.
> -
> -  Caution: This function may receive untrusted input.
> -  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> -
> -  @param[in]  VariableName  Pointer to variable name.
> -  @param[in]  VendorGuid    Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> -
> -  @retval EFI_SUCCESS           The function completed successfully.
> -  @retval EFI_NOT_FOUND         The next variable was not found.
> -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> -                                GUID of an existing variable.
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -VariableServiceGetNextVariableInternal (
> -  IN  CHAR16                *VariableName,
> -  IN  EFI_GUID              *VendorGuid,
> -  OUT VARIABLE_HEADER       **VariablePtr
> -  )
> -{
> -  VARIABLE_STORE_TYPE     Type;
> -  VARIABLE_POINTER_TRACK  Variable;
> -  VARIABLE_POINTER_TRACK  VariableInHob;
> -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> -  EFI_STATUS              Status;
> -  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
> -
> -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> &mVariableModuleGlobal->VariableGlobal, FALSE);
> -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> -    //
> -    // For VariableName is an empty string, FindVariable() will try to find and
> return
> -    // the first qualified variable, and if FindVariable() returns error
> (EFI_NOT_FOUND)
> -    // as no any variable is found, still go to return the error
> (EFI_NOT_FOUND).
> -    //
> -    if (VariableName[0] != 0) {
> -      //
> -      // For VariableName is not an empty string, and FindVariable() returns
> error as
> -      // VariableName and VendorGuid are not a name and GUID of an existing
> variable,
> -      // there is no way to get next variable, follow spec to return
> EFI_INVALID_PARAMETER.
> -      //
> -      Status = EFI_INVALID_PARAMETER;
> -    }
> -    goto Done;
> -  }
> -
> -  if (VariableName[0] != 0) {
> -    //
> -    // If variable name is not NULL, get next variable.
> -    //
> -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -  }
> -
> -  //
> -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> -  // The index and attributes mapping must be kept in this order as
> FindVariable
> -  // makes use of this mapping to implement search algorithm.
> -  //
> -  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> -  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> -
> -  while (TRUE) {
> -    //
> -    // Switch from Volatile to HOB, to Non-Volatile.
> -    //
> -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> -      //
> -      // Find current storage index
> -      //
> -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> Type++) {
> -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreHeader[Type]))) {
> -          break;
> -        }
> -      }
> -      ASSERT (Type < VariableStoreTypeMax);
> -      //
> -      // Switch to next storage
> -      //
> -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> -        if (VariableStoreHeader[Type] != NULL) {
> -          break;
> -        }
> -      }
> -      //
> -      // Capture the case that
> -      // 1. current storage is the last one, or
> -      // 2. no further storage
> -      //
> -      if (Type == VariableStoreTypeMax) {
> -        Status = EFI_NOT_FOUND;
> -        goto Done;
> -      }
> -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> -      Variable.CurrPtr  = Variable.StartPtr;
> -    }
> -
> -    //
> -    // Variable is found
> -    //
> -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -          //
> -          // If it is a IN_DELETED_TRANSITION variable,
> -          // and there is also a same ADDED one at the same time,
> -          // don't return it.
> -          //
> -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> -          Status = FindVariableEx (
> -                     GetVariableNamePtr (Variable.CurrPtr),
> -                     GetVendorGuidPtr (Variable.CurrPtr),
> -                     FALSE,
> -                     &VariablePtrTrack
> -                     );
> -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> VAR_ADDED) {
> -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -            continue;
> -          }
> -        }
> -
> -        //
> -        // Don't return NV variable when HOB overrides it
> -        //
> -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> -            (Variable.StartPtr == GetStartPointer
> (VariableStoreHeader[VariableStoreTypeNv]))
> -           ) {
> -          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> -          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> -          Status = FindVariableEx (
> -                     GetVariableNamePtr (Variable.CurrPtr),
> -                     GetVendorGuidPtr (Variable.CurrPtr),
> -                     FALSE,
> -                     &VariableInHob
> -                     );
> -          if (!EFI_ERROR (Status)) {
> -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -            continue;
> -          }
> -        }
> -
> -        *VariablePtr = Variable.CurrPtr;
> -        Status = EFI_SUCCESS;
> -        goto Done;
> -      }
> -    }
> -
> -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -  }
> -
> -Done:
> -  return Status;
> -}
> -
>  /**
> 
>    This code Finds the Next available variable.
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> index cb6fcebe2d..dc78f68fa9 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> @@ -1,12 +1,13 @@
>  /** @file
>    Provides variable driver extended services.
> 
> -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  /**
>    Finds variable in storage blocks of volatile and non-volatile storage areas.
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> new file mode 100644
> index 0000000000..7de0a90772
> --- /dev/null
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> @@ -0,0 +1,731 @@
> +/** @file
> +  Functions in this module are associated with variable parsing operations
> and
> +  are intended to be usable across variable driver source files.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "VariableParsing.h"
> +
> +/**
> +
> +  This code checks if variable header is valid or not.
> +
> +  @param Variable           Pointer to the Variable Header.
> +  @param VariableStoreEnd   Pointer to the Variable Store End.
> +
> +  @retval TRUE              Variable header is valid.
> +  @retval FALSE             Variable header is not valid.
> +
> +**/
> +BOOLEAN
> +IsValidVariableHeader (
> +  IN  VARIABLE_HEADER       *Variable,
> +  IN  VARIABLE_HEADER       *VariableStoreEnd
> +  )
> +{
> +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> >StartId != VARIABLE_DATA)) {
> +    //
> +    // Variable is NULL or has reached the end of variable store,
> +    // or the StartId is not correct.
> +    //
> +    return FALSE;
> +  }
> +
> +  return TRUE;
> +}
> +
> +/**
> +
> +  This code gets the current status of Variable Store.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @retval EfiRaw         Variable store status is raw.
> +  @retval EfiValid       Variable store status is valid.
> +  @retval EfiInvalid     Variable store status is invalid.
> +
> +**/
> +VARIABLE_STORE_STATUS
> +GetVariableStoreStatus (
> +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> +  )
> +{
> +  if ((CompareGuid (&VarStoreHeader->Signature,
> &gEfiAuthenticatedVariableGuid) ||
> +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> +      ) {
> +
> +    return EfiValid;
> +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> +             VarStoreHeader->Size == 0xffffffff &&
> +             VarStoreHeader->Format == 0xff &&
> +             VarStoreHeader->State == 0xff
> +          ) {
> +
> +    return EfiRaw;
> +  } else {
> +    return EfiInvalid;
> +  }
> +}
> +
> +/**
> +  This code gets the size of variable header.
> +
> +  @return Size of variable header in bytes in type UINTN.
> +
> +**/
> +UINTN
> +GetVariableHeaderSize (
> +  VOID
> +  )
> +{
> +  UINTN Value;
> +
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> +  } else {
> +    Value = sizeof (VARIABLE_HEADER);
> +  }
> +
> +  return Value;
> +}
> +
> +/**
> +
> +  This code gets the size of name of variable.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return UINTN          Size of variable in bytes.
> +
> +**/
> +UINTN
> +NameSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    if (AuthVariable->State == (UINT8) (-1) ||
> +       AuthVariable->DataSize == (UINT32) (-1) ||
> +       AuthVariable->NameSize == (UINT32) (-1) ||
> +       AuthVariable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) AuthVariable->NameSize;
> +  } else {
> +    if (Variable->State == (UINT8) (-1) ||
> +        Variable->DataSize == (UINT32) (-1) ||
> +        Variable->NameSize == (UINT32) (-1) ||
> +        Variable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) Variable->NameSize;
> +  }
> +}
> +
> +/**
> +  This code sets the size of name of variable.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] NameSize   Name size to set.
> +
> +**/
> +VOID
> +SetNameSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              NameSize
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    AuthVariable->NameSize = (UINT32) NameSize;
> +  } else {
> +    Variable->NameSize = (UINT32) NameSize;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the size of variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Size of variable in bytes.
> +
> +**/
> +UINTN
> +DataSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    if (AuthVariable->State == (UINT8) (-1) ||
> +       AuthVariable->DataSize == (UINT32) (-1) ||
> +       AuthVariable->NameSize == (UINT32) (-1) ||
> +       AuthVariable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) AuthVariable->DataSize;
> +  } else {
> +    if (Variable->State == (UINT8) (-1) ||
> +        Variable->DataSize == (UINT32) (-1) ||
> +        Variable->NameSize == (UINT32) (-1) ||
> +        Variable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) Variable->DataSize;
> +  }
> +}
> +
> +/**
> +  This code sets the size of variable data.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] DataSize   Data size to set.
> +
> +**/
> +VOID
> +SetDataSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              DataSize
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    AuthVariable->DataSize = (UINT32) DataSize;
> +  } else {
> +    Variable->DataSize = (UINT32) DataSize;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the pointer to the variable name.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Name which is Unicode encoding.
> +
> +**/
> +CHAR16 *
> +GetVariableNamePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> +}
> +
> +/**
> +  This code gets the pointer to the variable guid.
> +
> +  @param Variable   Pointer to the Variable Header.
> +
> +  @return A EFI_GUID* pointer to Vendor Guid.
> +
> +**/
> +EFI_GUID *
> +GetVendorGuidPtr (
> +  IN VARIABLE_HEADER    *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    return &AuthVariable->VendorGuid;
> +  } else {
> +    return &Variable->VendorGuid;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the pointer to the variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Data.
> +
> +**/
> +UINT8 *
> +GetVariableDataPtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  //
> +  // Be careful about pad size for alignment.
> +  //
> +  Value =  (UINTN) GetVariableNamePtr (Variable);
> +  Value += NameSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> +
> +  return (UINT8 *) Value;
> +}
> +
> +/**
> +  This code gets the variable data offset related to variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Variable Data offset.
> +
> +**/
> +UINTN
> +GetVariableDataOffset (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  //
> +  // Be careful about pad size for alignment
> +  //
> +  Value = GetVariableHeaderSize ();
> +  Value += NameSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> +
> +  return Value;
> +}
> +
> +/**
> +
> +  This code gets the pointer to the next variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to next variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetNextVariablePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  Value =  (UINTN) GetVariableDataPtr (Variable);
> +  Value += DataSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> +
> +  //
> +  // Be careful about pad size for alignment.
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> +}
> +
> +/**
> +
> +  Gets the pointer to the first variable header in given variable store area.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the first variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetStartPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  )
> +{
> +  //
> +  // The start of variable store.
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> +}
> +
> +/**
> +
> +  Gets the pointer to the end of the variable storage area.
> +
> +  This function gets pointer to the end of the variable storage
> +  area, according to the input variable store header.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the end of the variable storage area.
> +
> +**/
> +VARIABLE_HEADER *
> +GetEndPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  )
> +{
> +  //
> +  // The end of variable store
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader +
> VarStoreHeader->Size);
> +}
> +
> +/**
> +  Compare two EFI_TIME data.
> +
> +
> +  @param FirstTime           A pointer to the first EFI_TIME data.
> +  @param SecondTime          A pointer to the second EFI_TIME data.
> +
> +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> +  @retval  FALSE             The FirstTime is later than the SecondTime.
> +
> +**/
> +BOOLEAN
> +VariableCompareTimeStampInternal (
> +  IN EFI_TIME               *FirstTime,
> +  IN EFI_TIME               *SecondTime
> +  )
> +{
> +  if (FirstTime->Year != SecondTime->Year) {
> +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> +  } else if (FirstTime->Month != SecondTime->Month) {
> +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> +  } else if (FirstTime->Day != SecondTime->Day) {
> +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> +  } else if (FirstTime->Hour != SecondTime->Hour) {
> +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> +  } else if (FirstTime->Minute != SecondTime->Minute) {
> +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> +  }
> +
> +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> +}
> +
> +/**
> +  Find the variable in the specified variable store.
> +
> +  @param[in]       VariableName        Name of the variable to be found
> +  @param[in]       VendorGuid          Vendor GUID to be found.
> +  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> +                                       check at runtime when searching variable.
> +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains Variable Information.
> +
> +  @retval          EFI_SUCCESS         Variable found successfully
> +  @retval          EFI_NOT_FOUND       Variable not found
> +**/
> +EFI_STATUS
> +FindVariableEx (
> +  IN     CHAR16                  *VariableName,
> +  IN     EFI_GUID                *VendorGuid,
> +  IN     BOOLEAN                 IgnoreRtCheck,
> +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> +  )
> +{
> +  VARIABLE_HEADER                *InDeletedVariable;
> +  VOID                           *Point;
> +
> +  PtrTrack->InDeletedTransitionPtr = NULL;
> +
> +  //
> +  // Find the variable by walk through HOB, volatile and non-volatile variable
> store.
> +  //
> +  InDeletedVariable  = NULL;
> +
> +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> +      ) {
> +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)
> +       ) {
> +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> +        if (VariableName[0] == 0) {
> +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +            InDeletedVariable   = PtrTrack->CurrPtr;
> +          } else {
> +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> +            return EFI_SUCCESS;
> +          }
> +        } else {
> +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr)))
> {
> +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> +
> +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> +            if (CompareMem (VariableName, Point, NameSizeOfVariable
> (PtrTrack->CurrPtr)) == 0) {
> +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +                InDeletedVariable     = PtrTrack->CurrPtr;
> +              } else {
> +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> +                return EFI_SUCCESS;
> +              }
> +            }
> +          }
> +        }
> +      }
> +    }
> +  }
> +
> +  PtrTrack->CurrPtr = InDeletedVariable;
> +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> +}
> +
> +/**
> +  This code Finds the Next available variable.
> +
> +  Caution: This function may receive untrusted input.
> +  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> +
> +  @param[in]  VariableName  Pointer to variable name.
> +  @param[in]  VendorGuid    Variable Vendor Guid.
> +  @param[out] VariablePtr   Pointer to variable header address.
> +
> +  @retval EFI_SUCCESS           The function completed successfully.
> +  @retval EFI_NOT_FOUND         The next variable was not found.
> +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> +  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> +                                GUID of an existing variable.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +VariableServiceGetNextVariableInternal (
> +  IN  CHAR16                *VariableName,
> +  IN  EFI_GUID              *VendorGuid,
> +  OUT VARIABLE_HEADER       **VariablePtr
> +  )
> +{
> +  VARIABLE_STORE_TYPE     Type;
> +  VARIABLE_POINTER_TRACK  Variable;
> +  VARIABLE_POINTER_TRACK  VariableInHob;
> +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> +  EFI_STATUS              Status;
> +  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> +
> +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> &mVariableModuleGlobal->VariableGlobal, FALSE);
> +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> +    //
> +    // For VariableName is an empty string, FindVariable() will try to find and
> return
> +    // the first qualified variable, and if FindVariable() returns error
> (EFI_NOT_FOUND)
> +    // as no any variable is found, still go to return the error
> (EFI_NOT_FOUND).
> +    //
> +    if (VariableName[0] != 0) {
> +      //
> +      // For VariableName is not an empty string, and FindVariable() returns
> error as
> +      // VariableName and VendorGuid are not a name and GUID of an
> existing variable,
> +      // there is no way to get next variable, follow spec to return
> EFI_INVALID_PARAMETER.
> +      //
> +      Status = EFI_INVALID_PARAMETER;
> +    }
> +    goto Done;
> +  }
> +
> +  if (VariableName[0] != 0) {
> +    //
> +    // If variable name is not NULL, get next variable.
> +    //
> +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +  }
> +
> +  //
> +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> +  // The index and attributes mapping must be kept in this order as
> FindVariable
> +  // makes use of this mapping to implement search algorithm.
> +  //
> +  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> +  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> +
> +  while (TRUE) {
> +    //
> +    // Switch from Volatile to HOB, to Non-Volatile.
> +    //
> +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> +      //
> +      // Find current storage index
> +      //
> +      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> Type++) {
> +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreHeader[Type]))) {
> +          break;
> +        }
> +      }
> +      ASSERT (Type < VariableStoreTypeMax);
> +      //
> +      // Switch to next storage
> +      //
> +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> +        if (VariableStoreHeader[Type] != NULL) {
> +          break;
> +        }
> +      }
> +      //
> +      // Capture the case that
> +      // 1. current storage is the last one, or
> +      // 2. no further storage
> +      //
> +      if (Type == VariableStoreTypeMax) {
> +        Status = EFI_NOT_FOUND;
> +        goto Done;
> +      }
> +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> +      Variable.CurrPtr  = Variable.StartPtr;
> +    }
> +
> +    //
> +    // Variable is found
> +    //
> +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +          //
> +          // If it is a IN_DELETED_TRANSITION variable,
> +          // and there is also a same ADDED one at the same time,
> +          // don't return it.
> +          //
> +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> +          Status = FindVariableEx (
> +                     GetVariableNamePtr (Variable.CurrPtr),
> +                     GetVendorGuidPtr (Variable.CurrPtr),
> +                     FALSE,
> +                     &VariablePtrTrack
> +                     );
> +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> VAR_ADDED) {
> +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +            continue;
> +          }
> +        }
> +
> +        //
> +        // Don't return NV variable when HOB overrides it
> +        //
> +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> +            (Variable.StartPtr == GetStartPointer
> (VariableStoreHeader[VariableStoreTypeNv]))
> +           ) {
> +          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> +          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> +          Status = FindVariableEx (
> +                     GetVariableNamePtr (Variable.CurrPtr),
> +                     GetVendorGuidPtr (Variable.CurrPtr),
> +                     FALSE,
> +                     &VariableInHob
> +                     );
> +          if (!EFI_ERROR (Status)) {
> +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +            continue;
> +          }
> +        }
> +
> +        *VariablePtr = Variable.CurrPtr;
> +        Status = EFI_SUCCESS;
> +        goto Done;
> +      }
> +    }
> +
> +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +  }
> +
> +Done:
> +  return Status;
> +}
> +
> +/**
> +  Routine used to track statistical information about variable usage.
> +  The data is stored in the EFI system table so it can be accessed later.
> +  VariableInfo.efi can dump out the table. Only Boot Services variable
> +  accesses are tracked by this code. The PcdVariableCollectStatistics
> +  build flag controls if this feature is enabled.
> +
> +  A read that hits in the cache will have Read and Cache true for
> +  the transaction. Data is allocated by this routine, but never
> +  freed.
> +
> +  @param[in] VariableName   Name of the Variable to track.
> +  @param[in] VendorGuid     Guid of the Variable to track.
> +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in] Read           TRUE if GetVariable() was called.
> +  @param[in] Write          TRUE if SetVariable() was called.
> +  @param[in] Delete         TRUE if deleted via SetVariable().
> +  @param[in] Cache          TRUE for a cache hit.
> +
> +**/
> +VOID
> +UpdateVariableInfo (
> +  IN  CHAR16                  *VariableName,
> +  IN  EFI_GUID                *VendorGuid,
> +  IN  BOOLEAN                 Volatile,
> +  IN  BOOLEAN                 Read,
> +  IN  BOOLEAN                 Write,
> +  IN  BOOLEAN                 Delete,
> +  IN  BOOLEAN                 Cache
> +  )
> +{
> +  VARIABLE_INFO_ENTRY   *Entry;
> +
> +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> +
> +    if (AtRuntime ()) {
> +      // Don't collect statistics at runtime.
> +      return;
> +    }
> +
> +    if (gVariableInfo == NULL) {
> +      //
> +      // On the first call allocate a entry and place a pointer to it in
> +      // the EFI System Table.
> +      //
> +      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> +      ASSERT (gVariableInfo != NULL);
> +
> +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> +      ASSERT (gVariableInfo->Name != NULL);
> +      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> +      gVariableInfo->Volatile = Volatile;
> +    }
> +
> +
> +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> +        if (StrCmp (VariableName, Entry->Name) == 0) {
> +          if (Read) {
> +            Entry->ReadCount++;
> +          }
> +          if (Write) {
> +            Entry->WriteCount++;
> +          }
> +          if (Delete) {
> +            Entry->DeleteCount++;
> +          }
> +          if (Cache) {
> +            Entry->CacheCount++;
> +          }
> +
> +          return;
> +        }
> +      }
> +
> +      if (Entry->Next == NULL) {
> +        //
> +        // If the entry is not in the table add it.
> +        // Next iteration of the loop will fill in the data.
> +        //
> +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> +        ASSERT (Entry->Next != NULL);
> +
> +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> +        ASSERT (Entry->Next->Name != NULL);
> +        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> +        Entry->Next->Volatile = Volatile;
> +      }
> +
> +    }
> +  }
> +}
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> index ec463d063e..ce409f22a3 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  #include <Guid/SmmVariableCommon.h>
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  BOOLEAN                                              mAtRuntime              = FALSE;
>  UINT8                                                *mVariableBufferPayload = NULL;
> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list
  2019-09-28  1:47 ` [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list Kubacki, Michael A
@ 2019-10-03  8:03   ` Wu, Hao A
  2019-10-03 18:04     ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:03 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

A couple of inline comments below:


> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize
> GetNextVariableEx() store list
> 
> The majority of logic related to GetNextVariableName () is currently
> implemented in VariableServiceGetNextVariableInternal (). The list
> of variable stores to search for the given variable name and variable
> GUID is defined in the function body. This change renames the function
> to GetNextVariableEx () since the function is no longer internal to
> a specific source file and adds a new parameter so that the caller
> must pass in the list of variable stores to be used in the variable
> search.


I am not sure if 'GetNextVariableEx' is a good name for the function, since:

1. There is no function named GetNextVariable(), so it is not clear what "Ex"
   means here.

2. The origin function VariableServiceGetNextVariableInternal() does get used
   in multiple source files (Variable.c & VariableExLib.c). I am not sure what
   is the intention for such renaming.


> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 15 ++-
> -
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 12 ++-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c   |  8 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 78
> ++++++++++++--------
>  4 files changed, 73 insertions(+), 40 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> index 9d77c4916c..0d231511ea 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> @@ -248,27 +248,30 @@ FindVariableEx (
>    );
> 
>  /**
> -  This code Finds the Next available variable.
> +  This code finds the next available variable.
> 
>    Caution: This function may receive untrusted input.
>    This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> 
> -  @param[in]  VariableName  Pointer to variable name.
> -  @param[in]  VendorGuid    Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> +  @param[in]  VariableName      Pointer to variable name.
> +  @param[in]  VendorGuid        Variable Vendor Guid.
> +  @param[in]  VariableStoreList A list of variable stores that should be used
> to get the next variable.
> +                                The maximum number of entries is the max value of
> VARIABLE_STORE_TYPE.
> +  @param[out] VariablePtr       Pointer to variable header address.
> 
>    @retval EFI_SUCCESS           The function completed successfully.
>    @retval EFI_NOT_FOUND         The next variable was not found.
> -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> +  @retval EFI_INVALID_PARAMETER If VariableName is nt an empty string,
> while VendorGuid is NULL.
>    @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
>                                  GUID of an existing variable.
> 
>  **/
>  EFI_STATUS
>  EFIAPI
> -VariableServiceGetNextVariableInternal (
> +GetNextVariableEx (
>    IN  CHAR16                *VariableName,
>    IN  EFI_GUID              *VendorGuid,
> +  IN  VARIABLE_STORE_HEADER **VariableStoreList,
>    OUT VARIABLE_HEADER       **VariablePtr
>    );
> 
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index 76536308e6..816e8f7b8f 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -2358,6 +2358,7 @@ VariableServiceGetNextVariableName (
>    UINTN                   MaxLen;
>    UINTN                   VarNameSize;
>    VARIABLE_HEADER         *VariablePtr;
> +  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> 
>    if (VariableNameSize == NULL || VariableName == NULL || VendorGuid ==
> NULL) {
>      return EFI_INVALID_PARAMETER;
> @@ -2377,7 +2378,16 @@ VariableServiceGetNextVariableName (
> 
>    AcquireLockOnlyAtBootTime(&mVariableModuleGlobal-
> >VariableGlobal.VariableServicesLock);
> 
> -  Status = VariableServiceGetNextVariableInternal (VariableName,
> VendorGuid, &VariablePtr);
> +  //
> +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> +  // The index and attributes mapping must be kept in this order as
> FindVariable
> +  // makes use of this mapping to implement search algorithm.
> +  //
> +  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> +  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> +
> +  Status = GetNextVariableEx (VariableName, VendorGuid,
> VariableStoreHeader, &VariablePtr);
>    if (!EFI_ERROR (Status)) {
>      VarNameSize = NameSizeOfVariable (VariablePtr);
>      ASSERT (VarNameSize != 0);
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> index dc78f68fa9..232d9ffe25 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> @@ -98,10 +98,16 @@ VariableExLibFindNextVariable (
>    EFI_STATUS                    Status;
>    VARIABLE_HEADER               *VariablePtr;
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr;
> +  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> 
> -  Status = VariableServiceGetNextVariableInternal (
> +  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> +  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> +
> +  Status = GetNextVariableEx (
>               VariableName,
>               VendorGuid,
> +             VariableStoreHeader,
>               &VariablePtr
>               );
>    if (EFI_ERROR (Status)) {
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> index 7de0a90772..9bc5369a90 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> @@ -476,14 +476,16 @@ FindVariableEx (
>  }
> 
>  /**
> -  This code Finds the Next available variable.
> +  This code finds the next available variable.
> 
>    Caution: This function may receive untrusted input.
>    This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> 
> -  @param[in]  VariableName  Pointer to variable name.
> -  @param[in]  VendorGuid    Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> +  @param[in]  VariableName      Pointer to variable name.
> +  @param[in]  VendorGuid        Variable Vendor Guid.
> +  @param[in]  VariableStoreList A list of variable stores that should be used
> to get the next variable.
> +                                The maximum number of entries is the max value of
> VARIABLE_STORE_TYPE.
> +  @param[out] VariablePtr       Pointer to variable header address.
> 
>    @retval EFI_SUCCESS           The function completed successfully.
>    @retval EFI_NOT_FOUND         The next variable was not found.
> @@ -494,20 +496,41 @@ FindVariableEx (
>  **/
>  EFI_STATUS
>  EFIAPI
> -VariableServiceGetNextVariableInternal (
> +GetNextVariableEx (
>    IN  CHAR16                *VariableName,
>    IN  EFI_GUID              *VendorGuid,
> +  IN  VARIABLE_STORE_HEADER **VariableStoreList,
>    OUT VARIABLE_HEADER       **VariablePtr
>    )
>  {
> -  VARIABLE_STORE_TYPE     Type;
> +  EFI_STATUS              Status;
> +  VARIABLE_STORE_TYPE     StoreType;
>    VARIABLE_POINTER_TRACK  Variable;
>    VARIABLE_POINTER_TRACK  VariableInHob;
>    VARIABLE_POINTER_TRACK  VariablePtrTrack;
> -  EFI_STATUS              Status;
> -  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
> 
> -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> &mVariableModuleGlobal->VariableGlobal, FALSE);
> +  Status = EFI_NOT_FOUND;
> +
> +  if (VariableStoreList == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  // Check if the variable exists in the given variable store list
> +  for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> VariableStoreTypeMax; StoreType++) {
> +    if (VariableStoreList[StoreType] == NULL) {
> +      continue;
> +    }
> +
> +    Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> +    Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> +    Variable.Volatile = (BOOLEAN) (StoreType == VariableStoreTypeVolatile);
> +
> +    Status = FindVariableEx (VariableName, VendorGuid, FALSE, &Variable);
> +    if (!EFI_ERROR (Status)) {
> +      break;
> +    }
> +  }
> +
>    if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
>      //
>      // For VariableName is an empty string, FindVariable() will try to find and


Some description comments within this function that mention "FindVariable()"
can be updated. Since the calling of the FindVariable() has been replaced by
the above 'for' loop.


Best Regards,
Hao Wu


> return
> @@ -527,39 +550,30 @@ VariableServiceGetNextVariableInternal (
> 
>    if (VariableName[0] != 0) {
>      //
> -    // If variable name is not NULL, get next variable.
> +    // If variable name is not empty, get next variable.
>      //
>      Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
>    }
> 
> -  //
> -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> -  // The index and attributes mapping must be kept in this order as
> FindVariable
> -  // makes use of this mapping to implement search algorithm.
> -  //
> -  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> -  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> -
>    while (TRUE) {
>      //
> -    // Switch from Volatile to HOB, to Non-Volatile.
> +    // Switch to the next variable store if needed
>      //
>      while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
>        //
>        // Find current storage index
>        //
> -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> Type++) {
> -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreHeader[Type]))) {
> +      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> VariableStoreTypeMax; StoreType++) {
> +        if ((VariableStoreList[StoreType] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreList[StoreType]))) {
>            break;
>          }
>        }
> -      ASSERT (Type < VariableStoreTypeMax);
> +      ASSERT (StoreType < VariableStoreTypeMax);
>        //
>        // Switch to next storage
>        //
> -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> -        if (VariableStoreHeader[Type] != NULL) {
> +      for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) {
> +        if (VariableStoreList[StoreType] != NULL) {
>            break;
>          }
>        }
> @@ -568,12 +582,12 @@ VariableServiceGetNextVariableInternal (
>        // 1. current storage is the last one, or
>        // 2. no further storage
>        //
> -      if (Type == VariableStoreTypeMax) {
> +      if (StoreType == VariableStoreTypeMax) {
>          Status = EFI_NOT_FOUND;
>          goto Done;
>        }
> -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> +      Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> +      Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
>        Variable.CurrPtr  = Variable.StartPtr;
>      }
> 
> @@ -605,11 +619,11 @@ VariableServiceGetNextVariableInternal (
>          //
>          // Don't return NV variable when HOB overrides it
>          //
> -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> -            (Variable.StartPtr == GetStartPointer
> (VariableStoreHeader[VariableStoreTypeNv]))
> +        if ((VariableStoreList[VariableStoreTypeHob] != NULL) &&
> (VariableStoreList[VariableStoreTypeNv] != NULL) &&
> +            (Variable.StartPtr == GetStartPointer
> (VariableStoreList[VariableStoreTypeNv]))
>             ) {
> -          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> -          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> +          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreList[VariableStoreTypeHob]);
> +          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreList[VariableStoreTypeHob]);
>            Status = FindVariableEx (
>                       GetVariableNamePtr (Variable.CurrPtr),
>                       GetVendorGuidPtr (Variable.CurrPtr),
> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  2019-09-28  1:47 ` [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer Kubacki, Michael A
@ 2019-10-03  8:03   ` Wu, Hao A
  2019-10-03 18:05     ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:03 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> VARIABLE_INFO_ENTRY buffer
> 
> UpdateVariableInfo () currently accepts parameters regarding updates
> to be made to a global variable of type VARIABLE_INFO_ENTRY. This
> change passes the structure by pointer to UpdateVariableInfo ()
> so structures can be updated outside the fixed global variable.


For:
"... so structures can be updated outside the fixed global variable "

Do you mean:

VARIABLE_INFO_ENTRY structure pointers other than "&gVariableInfo" can be
passed to UpdateVariableInfo().

Is my understanding correct? If so,
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 18
> +++++----
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 14 +++----
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 41
> +++++++++++---------
>  3 files changed, 39 insertions(+), 34 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> index 0d231511ea..6f2000f3ee 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> @@ -286,13 +286,14 @@ GetNextVariableEx (
>    the transaction. Data is allocated by this routine, but never
>    freed.
> 
> -  @param[in] VariableName   Name of the Variable to track.
> -  @param[in] VendorGuid     Guid of the Variable to track.
> -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> -  @param[in] Read           TRUE if GetVariable() was called.
> -  @param[in] Write          TRUE if SetVariable() was called.
> -  @param[in] Delete         TRUE if deleted via SetVariable().
> -  @param[in] Cache          TRUE for a cache hit.
> +  @param[in]      VariableName   Name of the Variable to track.
> +  @param[in]      VendorGuid     Guid of the Variable to track.
> +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in]      Read           TRUE if GetVariable() was called.
> +  @param[in]      Write          TRUE if SetVariable() was called.
> +  @param[in]      Delete         TRUE if deleted via SetVariable().
> +  @param[in]      Cache          TRUE for a cache hit.
> +  @param[in,out]  VariableInfo   Pointer to a pointer of
> VARIABLE_INFO_ENTRY structures.
> 
>  **/
>  VOID
> @@ -303,7 +304,8 @@ UpdateVariableInfo (
>    IN  BOOLEAN                 Read,
>    IN  BOOLEAN                 Write,
>    IN  BOOLEAN                 Delete,
> -  IN  BOOLEAN                 Cache
> +  IN  BOOLEAN                 Cache,
> +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
>    );
> 
>  #endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index 816e8f7b8f..1a57d7e1ba 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -1641,7 +1641,7 @@ UpdateVariable (
>          // go to delete this variable in variable HOB and
>          // try to flush other variables from HOB to flash.
>          //
> -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE,
> TRUE, FALSE);
> +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE,
> TRUE, FALSE, &gVariableInfo);
>          FlushHobVariableToFlash (VariableName, VendorGuid);
>          return EFI_SUCCESS;
>        }
> @@ -1758,7 +1758,7 @@ UpdateVariable (
>                   &State
>                   );
>        if (!EFI_ERROR (Status)) {
> -        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> FALSE, FALSE, TRUE, FALSE);
> +        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
>          if (!Variable->Volatile) {
>            CacheVariable->CurrPtr->State = State;
>            FlushHobVariableToFlash (VariableName, VendorGuid);
> @@ -1777,7 +1777,7 @@ UpdateVariable (
>        //
>        // Variable content unchanged and no need to update timestamp, just
> return.
>        //
> -      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> FALSE, TRUE, FALSE, FALSE);
> +      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
>        Status = EFI_SUCCESS;
>        goto Done;
>      } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||
> @@ -2006,7 +2006,7 @@ UpdateVariable (
>            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable-
> >StartPtr));
>            CacheVariable->InDeletedTransitionPtr = NULL;
>          }
> -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE,
> FALSE, FALSE);
> +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE,
> FALSE, FALSE, &gVariableInfo);
>          FlushHobVariableToFlash (VariableName, VendorGuid);
>        } else {
>          if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal-
> >CommonUserVariableTotalSize) > mVariableModuleGlobal-
> >CommonMaxUserVariableSpace)) {
> @@ -2156,7 +2156,7 @@ UpdateVariable (
>            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable-
> >StartPtr));
>            CacheVariable->InDeletedTransitionPtr = NULL;
>          }
> -        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE,
> FALSE, FALSE);
> +        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE,
> FALSE, FALSE, &gVariableInfo);
>        }
>        goto Done;
>      }
> @@ -2227,7 +2227,7 @@ UpdateVariable (
>    }
> 
>    if (!EFI_ERROR (Status)) {
> -    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE,
> FALSE, FALSE);
> +    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE,
> FALSE, FALSE, &gVariableInfo);
>      if (!Volatile) {
>        FlushHobVariableToFlash (VariableName, VendorGuid);
>      }
> @@ -2306,7 +2306,7 @@ VariableServiceGetVariable (
>      }
> 
>      *DataSize = VarDataSize;
> -    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE,
> FALSE, FALSE, FALSE);
> +    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE,
> FALSE, FALSE, FALSE, &gVariableInfo);
> 
>      Status = EFI_SUCCESS;
>      goto Done;
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> index 9bc5369a90..394195342d 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> @@ -660,13 +660,14 @@ Done:
>    the transaction. Data is allocated by this routine, but never
>    freed.
> 
> -  @param[in] VariableName   Name of the Variable to track.
> -  @param[in] VendorGuid     Guid of the Variable to track.
> -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> -  @param[in] Read           TRUE if GetVariable() was called.
> -  @param[in] Write          TRUE if SetVariable() was called.
> -  @param[in] Delete         TRUE if deleted via SetVariable().
> -  @param[in] Cache          TRUE for a cache hit.
> +  @param[in]      VariableName   Name of the Variable to track.
> +  @param[in]      VendorGuid     Guid of the Variable to track.
> +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in]      Read           TRUE if GetVariable() was called.
> +  @param[in]      Write          TRUE if SetVariable() was called.
> +  @param[in]      Delete         TRUE if deleted via SetVariable().
> +  @param[in]      Cache          TRUE for a cache hit.
> +  @param[in,out]  VariableInfo   Pointer to a pointer of
> VARIABLE_INFO_ENTRY structures.
> 
>  **/
>  VOID
> @@ -677,35 +678,38 @@ UpdateVariableInfo (
>    IN  BOOLEAN                 Read,
>    IN  BOOLEAN                 Write,
>    IN  BOOLEAN                 Delete,
> -  IN  BOOLEAN                 Cache
> +  IN  BOOLEAN                 Cache,
> +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
>    )
>  {
>    VARIABLE_INFO_ENTRY   *Entry;
> 
>    if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> -
> +    if (VariableName == NULL || VendorGuid == NULL || VariableInfo ==
> NULL) {
> +      return;
> +    }
>      if (AtRuntime ()) {
>        // Don't collect statistics at runtime.
>        return;
>      }
> 
> -    if (gVariableInfo == NULL) {
> +    if (*VariableInfo == NULL) {
>        //
>        // On the first call allocate a entry and place a pointer to it in
>        // the EFI System Table.
>        //
> -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> -      ASSERT (gVariableInfo != NULL);
> +      *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> +      ASSERT (*VariableInfo != NULL);
> 
> -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> -      ASSERT (gVariableInfo->Name != NULL);
> -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> -      gVariableInfo->Volatile = Volatile;
> +      CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
> +      (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
> +      ASSERT ((*VariableInfo)->Name != NULL);
> +      StrCpyS ((*VariableInfo)->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> +      (*VariableInfo)->Volatile = Volatile;
>      }
> 
> 
> -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> +    for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next) {
>        if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
>          if (StrCmp (VariableName, Entry->Name) == 0) {
>            if (Read) {
> @@ -739,7 +743,6 @@ UpdateVariableInfo (
>          StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
>          Entry->Next->Volatile = Volatile;
>        }
> -
>      }
>    }
>  }
> --
> 2.16.2.windows.1


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

* Re: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-09-28  1:47 ` [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing Kubacki, Michael A
@ 2019-10-03  8:04   ` Wu, Hao A
  2019-10-03 18:35     ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:04 UTC (permalink / raw)
  To: devel@edk2.groups.io, Kubacki, Michael A
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

Inline comments below:


> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> auth status in VariableParsing
> 
> The file VariableParsing.c provides generic functionality related
> to parsing variable related structures and information. In order to
> calculate offsets for certain operations, the functions must know if
> authenticated variables are enabled as this increases the size of
> variable headers.
> 
> This change removes linking against a global variable in an external file
> in favor of a statically scoped variable in VariableParsing.c Because this
> file is unaware of how the authenticated variable status is determined, the
> variable is set through a function interface invoked during variable driver
> initialization.
> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 14
> +++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10 +++---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 33
> ++++++++++++++++----
>  3 files changed, 45 insertions(+), 12 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> index 6f2000f3ee..3eba590634 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> @@ -308,4 +308,18 @@ UpdateVariableInfo (
>    IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
>    );
> 
> +/**
> +  Initializes context needed for variable parsing functions.
> +
> +  @param[in]       AuthFormat          If true then indicates authenticated
> variables are supported
> +
> +  @retval          EFI_SUCCESS         Initialized successfully
> +  @retval          Others              An error occurred during initialization
> +**/
> +EFI_STATUS
> +EFIAPI
> +InitVariableParsing (


InitVariableParsing() seems an internal function, the 'EFIAPI' keyword can be
dropped. Please help to update the function definition in .C file as well.


> +  IN  BOOLEAN   AuthFormat
> +  );
> +
>  #endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index 1a57d7e1ba..53d797152c 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
>    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> (PcdMaxVariableSize);
>    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) :
> mVariableModuleGlobal->MaxVariableSize);
> 
> +  Status = InitVariableParsing (mVariableModuleGlobal-
> >VariableGlobal.AuthFormat);
> +  ASSERT_EFI_ERROR (Status);
> +


After the above initialization, mVariableModuleGlobal->VariableGlobal.AuthFormat
will be changed temporarily within ConvertNormalVarStorageToAuthVarStorage() if
normal HOB variable store will be converted to the auth format:

VOID *
ConvertNormalVarStorageToAuthVarStorage (
  VARIABLE_STORE_HEADER *NormalVarStorage
  )
{
  ...
  //
  // Set AuthFormat as FALSE for normal variable storage
  //
  mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
  ...
  //
  // Restore AuthFormat
  //
  mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
  return AuthVarStorage;
}


I think there will be issues in such converting, since I found that at least
GetVariableHeaderSize() and NameSizeOfVariable() get called during the
execution of ConvertNormalVarStorageToAuthVarStorage(). And they are checking
'mAuthFormat' rather than 'mVariableModuleGlobal->VariableGlobal.AuthFormat'.


>    //
>    // Parse non-volatile variable data and get last variable offset.
>    //
> @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> 
>    //
>    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> -  // has been initialized in InitNonVolatileVariableStore().
> +  // is initialized in InitNonVolatileVariableStore().
>    //
>    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
>      DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable
> format!\n"));
> -    //
> -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> initialize it.
> -    //
> -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
>      VariableGuid = &gEfiAuthenticatedVariableGuid;
>    } else {
>      DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable
> support!\n"));
> -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;


Not sure why the above changes belong to this patch.
Could you help to double confirm?

Best Regards,
Hao Wu


>      VariableGuid = &gEfiVariableGuid;
>    }
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> index 394195342d..0a47f6d10d 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  #include "VariableParsing.h"
> 
> +STATIC  BOOLEAN   mAuthFormat;
> +
>  /**
> 
>    This code checks if variable header is valid or not.
> @@ -88,7 +90,7 @@ GetVariableHeaderSize (
>  {
>    UINTN Value;
> 
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
>    } else {
>      Value = sizeof (VARIABLE_HEADER);
> @@ -114,7 +116,7 @@ NameSizeOfVariable (
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> 
>    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      if (AuthVariable->State == (UINT8) (-1) ||
>         AuthVariable->DataSize == (UINT32) (-1) ||
>         AuthVariable->NameSize == (UINT32) (-1) ||
> @@ -149,7 +151,7 @@ SetNameSizeOfVariable (
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> 
>    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      AuthVariable->NameSize = (UINT32) NameSize;
>    } else {
>      Variable->NameSize = (UINT32) NameSize;
> @@ -173,7 +175,7 @@ DataSizeOfVariable (
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> 
>    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      if (AuthVariable->State == (UINT8) (-1) ||
>         AuthVariable->DataSize == (UINT32) (-1) ||
>         AuthVariable->NameSize == (UINT32) (-1) ||
> @@ -208,7 +210,7 @@ SetDataSizeOfVariable (
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> 
>    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      AuthVariable->DataSize = (UINT32) DataSize;
>    } else {
>      Variable->DataSize = (UINT32) DataSize;
> @@ -248,7 +250,7 @@ GetVendorGuidPtr (
>    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> 
>    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +  if (mAuthFormat) {
>      return &AuthVariable->VendorGuid;
>    } else {
>      return &Variable->VendorGuid;
> @@ -746,3 +748,22 @@ UpdateVariableInfo (
>      }
>    }
>  }
> +
> +/**
> +  Initializes context needed for variable parsing functions.
> +
> +  @param[in]       AuthFormat          If true then indicates authenticated
> variables are supported
> +
> +  @retval          EFI_SUCCESS         Initialized successfully
> +  @retval          Others              An error occurred during initialization
> +**/
> +EFI_STATUS
> +EFIAPI
> +InitVariableParsing (
> +  IN  BOOLEAN   AuthFormat
> +  )
> +{
> +  mAuthFormat = AuthFormat;
> +
> +  return EFI_SUCCESS;
> +}
> --
> 2.16.2.windows.1
> 
> 
> 


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

* Re: [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions
  2019-09-28  1:47 ` [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions Kubacki, Michael A
@ 2019-10-03  8:04   ` Wu, Hao A
  2019-10-03 18:43     ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:04 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable
> functions
> 
> This change adds a dedicated file for variable operations specific
> to non-volatile variables. This decreases the overall length of the
> relatively large Variable.c file.


It is not clear to me what are the criteria for moving functions into the
separate new file.

I guess the new file is for functions related with NV variables, but I saw
there are functions like:

InitRealNonVolatileVariableStore
InitEmuNonVolatileVariableStore
InitNonVolatileVariableStore

Not sure if they can be put into the new file as well.

Best Regards,
Hao Wu


> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> |  2 ++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |  2
> ++
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> |  2 ++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h    |
> 25 +++++++++++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 20 +----
> ---------
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c    |
> 28 ++++++++++++++++++++
>  6 files changed, 60 insertions(+), 19 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> index c35e5fe787..08a5490787 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> @@ -36,6 +36,8 @@
>    Variable.c
>    VariableDxe.c
>    Variable.h
> +  VariableNonVolatile.c
> +  VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
>    PrivilegePolymorphic.h
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> index 626738b9c7..6dc2721b81 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableTraditionalMm.c
>    VariableSmm.c
> +  VariableNonVolatile.c
> +  VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
>    VarCheck.c
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> index 1ba8f9ebfb..ca9d23ce9f 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableSmm.c
>    VariableStandaloneMm.c
> +  VariableNonVolatile.c
> +  VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
>    VarCheck.c
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> new file mode 100644
> index 0000000000..82572262ef
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> @@ -0,0 +1,25 @@
> +/** @file
> +  Common variable non-volatile store routines.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _VARIABLE_NON_VOLATILE_H_
> +#define _VARIABLE_NON_VOLATILE_H_
> +
> +#include "Variable.h"
> +
> +/**
> +  Get non-volatile maximum variable size.
> +
> +  @return Non-volatile maximum variable size.
> +
> +**/
> +UINTN
> +GetNonVolatileMaxVariableSize (
> +  VOID
> +  );
> +
> +#endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index 53d797152c..5da2354aa5 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  **/
> 
>  #include "Variable.h"
> +#include "VariableNonVolatile.h"
>  #include "VariableParsing.h"
> 
>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> @@ -3006,25 +3007,6 @@ ReclaimForOS(
>    }
>  }
> 
> -/**
> -  Get non-volatile maximum variable size.
> -
> -  @return Non-volatile maximum variable size.
> -
> -**/
> -UINTN
> -GetNonVolatileMaxVariableSize (
> -  VOID
> -  )
> -{
> -  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
> -    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> (PcdMaxAuthVariableSize)),
> -                PcdGet32 (PcdMaxHardwareErrorVariableSize));
> -  } else {
> -    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> (PcdMaxAuthVariableSize));
> -  }
> -}
> -
>  /**
>    Get maximum variable size, covering both non-volatile and volatile variables.
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> new file mode 100644
> index 0000000000..b1b6d8282f
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> @@ -0,0 +1,28 @@
> +/** @file
> +  Common variable non-volatile store routines.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "VariableNonVolatile.h"
> +
> +/**
> +  Get non-volatile maximum variable size.
> +
> +  @return Non-volatile maximum variable size.
> +
> +**/
> +UINTN
> +GetNonVolatileMaxVariableSize (
> +  VOID
> +  )
> +{
> +  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
> +    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> (PcdMaxAuthVariableSize)),
> +                PcdGet32 (PcdMaxHardwareErrorVariableSize));
> +  } else {
> +    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> (PcdMaxAuthVariableSize));
> +  }
> +}
> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats
  2019-09-28  1:47 ` [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats Kubacki, Michael A
@ 2019-10-03  8:04   ` Wu, Hao A
  0 siblings, 0 replies; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:04 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT
> DXE and SMM stats
> 
> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> 
> The current VariableInfo application only checks for variable
> statistics from SMM if the variable information entries are
> not present in the UEFI System Configuration table as published
> by the DXE UEFI variable driver (VariableRuntimeDxe).
> 
> This change first checks for variable information entries in the
> UEFI System Configuration but always checks for entries in SMM
> as well. If the SMM variable driver is not present, an instance of
> EFI_SMM_VARIABLE_PROTOCOL will not be found and the search for
> SMM variable statistics will be aborted (an SW SMI to get variable
> statistics will not be triggered).
> 
> In the case variable statistics are provided by both a Runtime DXE
> driver (e.g. VariableSmmRuntimeDxe) and a SMM driver (VariableSmm),
> this change will clearly identify statistics from each respective
> driver.
> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Application/VariableInfo/VariableInfo.c | 37 ++++++++++--
> --------
>  1 file changed, 18 insertions(+), 19 deletions(-)
> 
> diff --git a/MdeModulePkg/Application/VariableInfo/VariableInfo.c
> b/MdeModulePkg/Application/VariableInfo/VariableInfo.c
> index f213471e9a..c04ba18213 100644
> --- a/MdeModulePkg/Application/VariableInfo/VariableInfo.c
> +++ b/MdeModulePkg/Application/VariableInfo/VariableInfo.c
> @@ -3,7 +3,7 @@
>    this utility will print out the statistics information. You can use console
>    redirection to capture the data.
> 
> -  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -126,7 +126,7 @@ PrintInfoFromSmm (
>    ASSERT (CommBuffer != NULL);
>    ZeroMem (CommBuffer, RealCommSize);
> 
> -  Print (L"Non-Volatile SMM Variables:\n");
> +  Print (L"SMM Driver Non-Volatile Variables:\n");
>    do {
>      CommSize = RealCommSize;
>      Status = GetVariableStatisticsData (CommBuffer, &CommSize);
> @@ -155,7 +155,7 @@ PrintInfoFromSmm (
>      }
>    } while (TRUE);
> 
> -  Print (L"Volatile SMM Variables:\n");
> +  Print (L"SMM Driver Volatile Variables:\n");
>    ZeroMem (CommBuffer, RealCommSize);
>    do {
>      CommSize = RealCommSize;
> @@ -207,24 +207,18 @@ UefiMain (
>    IN EFI_SYSTEM_TABLE  *SystemTable
>    )
>  {
> -  EFI_STATUS            Status;
> +  EFI_STATUS            RuntimeDxeStatus;
> +  EFI_STATUS            SmmStatus;
>    VARIABLE_INFO_ENTRY   *VariableInfo;
>    VARIABLE_INFO_ENTRY   *Entry;
> 
> -  Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID
> **)&Entry);
> -  if (EFI_ERROR (Status) || (Entry == NULL)) {
> -    Status = EfiGetSystemConfigurationTable
> (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);
> +  RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiVariableGuid,
> (VOID **) &Entry);
> +  if (EFI_ERROR (RuntimeDxeStatus) || (Entry == NULL)) {
> +    RuntimeDxeStatus = EfiGetSystemConfigurationTable
> (&gEfiAuthenticatedVariableGuid, (VOID **) &Entry);
>    }
> 
> -  if (EFI_ERROR (Status) || (Entry == NULL)) {
> -    Status = PrintInfoFromSmm ();
> -    if (!EFI_ERROR (Status)) {
> -      return Status;
> -    }
> -  }
> -
> -  if (!EFI_ERROR (Status) && (Entry != NULL)) {
> -    Print (L"Non-Volatile EFI Variables:\n");
> +  if (!EFI_ERROR (RuntimeDxeStatus) && (Entry != NULL)) {
> +    Print (L"Runtime DXE Driver Non-Volatile EFI Variables:\n");
>      VariableInfo = Entry;
>      do {
>        if (!VariableInfo->Volatile) {
> @@ -242,7 +236,7 @@ UefiMain (
>        VariableInfo = VariableInfo->Next;
>      } while (VariableInfo != NULL);
> 
> -    Print (L"Volatile EFI Variables:\n");
> +    Print (L"Runtime DXE Driver Volatile EFI Variables:\n");
>      VariableInfo = Entry;
>      do {
>        if (VariableInfo->Volatile) {
> @@ -258,14 +252,19 @@ UefiMain (
>        }
>        VariableInfo = VariableInfo->Next;
>      } while (VariableInfo != NULL);
> +  }
> 
> -  } else {
> +  SmmStatus = PrintInfoFromSmm ();
> +
> +  if (EFI_ERROR (RuntimeDxeStatus) && EFI_ERROR (SmmStatus)) {
>      Print (L"Warning: Variable Dxe/Smm driver doesn't enable the feature of
> statistical information!\n");
>      Print (L"If you want to see this info, please:\n");
>      Print (L"  1. Set PcdVariableCollectStatistics as TRUE\n");
>      Print (L"  2. Rebuild Variable Dxe/Smm driver\n");
>      Print (L"  3. Run \"VariableInfo\" cmd again\n");
> +
> +    return EFI_NOT_FOUND;
>    }
> 
> -  return Status;
> +  return EFI_SUCCESS;
>  }
	

Acked-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-09-28  1:47 ` [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support Kubacki, Michael A
@ 2019-10-03  8:04   ` Wu, Hao A
  2019-10-03 11:00     ` Laszlo Ersek
  2019-10-03 21:53     ` Kubacki, Michael A
  0 siblings, 2 replies; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:04 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

Before any comment on the patch, since I am not experienced in the Variable
driver, I would like to ask for help from other reviewers to look into this
patch and provide feedbacks as well. Thanks in advance.

With the above fact, some comments provided below maybe wrong. So please help
to kindly correct me.


Some general comments:
1. I am not sure if bringing the TimerLib dependency (delay in acquiring the
   runtime cache read lock) to variable driver (a software driver for the most
   part) is a good idea.
   
   Hope other reviewers can provide some feedbacks for this. Thanks in advance.

2. In my opinion, I prefer a switch can be provided for platform owners to
   choose between using the runtime cache and going through SMM for GetVariable
   (and for GetNextVariableName in the next patch as well).
   
   If platform owners feel uncomfortable with using the runtime cache with
   regard to the security perspective, they can switch to the origin solution.

3. Please help to remove the 'EFIAPI' keyword for new driver internal functions;


Inline comments below:


> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> cache support
> 
> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> 
> This change reduces SMIs for GetVariable () by maintaining a
> UEFI variable cache in Runtime DXE in addition to the pre-
> existing cache in SMRAM. When the Runtime Service GetVariable()
> is invoked, a Runtime DXE cache is used instead of triggering an
> SMI to VariableSmm. This can improve overall system performance
> by servicing variable read requests without rendezvousing all
> cores into SMM.
> 
> The following are important points regarding this change.
> 
> 1. All of the non-volatile storage contents are loaded into the
>    cache upon driver load. This one time load operation from storage
>    is preferred as opposed to building the cache on demand. An on-
>    demand cache would require a fallback SMI to load data into the
>    cache as variables are requested.
> 
> 2. SetVariable () requests will continue to always trigger an SMI.
>    This occurs regardless of whether the variable is volatile or
>    non-volatile.
> 
> 3. Both volatile and non-volatile variables are cached in a runtime
>    buffer. As is the case in the current EDK II variable driver, they
>    continue to be cached in separate buffers.
> 
> 4. The cache in Runtime DXE and SMM are intended to be exact copies
>    of one another. All SMM variable accesses only return data from the
>    SMM cache. The runtime caches are only updated after the variable I/O
>    operation is successful in SMM. The runtime caches are only updated
>    from SMM.
> 
> 5. Synchronization mechanisms are in place to ensure the runtime cache
>    content integrity with the SMM cache. These may result in updates to
>    runtime cache that are the same in content but different in offset and
>    size from updates to the SMM cache.
> 
> When using SMM variables, two caches will now be present.
> 1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to
> service
>    Runtime Services GetVariable () and GetNextVariableName () callers.
> 2. "SMM Cache" - Maintained in VariableSmm to service SMM GetVariable ()
>    and GetNextVariableName () callers.
>    a. This cache is retained so SMM modules do not operate on data outside
>       SMRAM.
> 
> It is possible to view UEFI variable read and write statistics by setting
> the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics
> FeaturePcd
> to TRUE and using the VariableInfo UEFI application in MdeModulePkg to
> dump
> variable statistics to the console. By doing so, a user can view the number
> of GetVariable () hits from the Runtime DXE variable driver (Runtime Cache
> hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for
> GetVariable () will occur when SMM modules invoke GetVariable ().
> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> |   2 +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf           |   2
> +
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i
> nf |  31 +-
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> |   2 +
>  MdeModulePkg/Include/Guid/SmmVariableCommon.h                        |  29 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h                |  39 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> |  47 ++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c                |  44 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> | 153 +++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c             | 114
> +++-
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.
> c   | 608 +++++++++++++++++---
>  11 files changed, 966 insertions(+), 105 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> index 08a5490787..ceea5d1ff9 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> @@ -40,6 +40,8 @@
>    VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
> +  VariableRuntimeCache.c
> +  VariableRuntimeCache.h


Per my understanding, the module specified by VariableRuntimeDxe.inf does not
involve SMM/SMI for variable services (like GetVariable). It looks weird to me
for this INF to include the newly introduced runtime cache codes (below source
header files):

VariableRuntimeCache.c
VariableRuntimeCache.h


>    PrivilegePolymorphic.h
>    Measurement.c
>    TcgMorLockDxe.c
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> index 6dc2721b81..bc3033588d 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> @@ -49,6 +49,8 @@
>    VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
> +  VariableRuntimeCache.c
> +  VariableRuntimeCache.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.inf
> index 14894e6f13..70837ac6e0 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.inf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.inf
> @@ -13,7 +13,7 @@
>  #  may not be modified without authorization. If platform fails to protect
> these resources,
>  #  the authentication service provided in this driver will be broken, and the
> behavior is undefined.
>  #
> -# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
>  # SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
>  ##
> @@ -39,6 +39,10 @@
>    VariableSmmRuntimeDxe.c
>    PrivilegePolymorphic.h
>    Measurement.c
> +  VariableParsing.c
> +  VariableParsing.h
> +  VariableRuntimeCache.c
> +  VariableRuntimeCache.h
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> @@ -49,6 +53,7 @@
>    BaseLib
>    UefiBootServicesTableLib
>    DebugLib
> +  TimerLib
>    UefiRuntimeLib
>    DxeServicesTableLib
>    UefiDriverEntryPoint
> @@ -65,7 +70,29 @@
>    gEdkiiVariableLockProtocolGuid                ## PRODUCES
>    gEdkiiVarCheckProtocolGuid                    ## PRODUCES
> 
> +[Pcd]
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                      ##
> CONSUMES
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize                  ##
> CONSUMES
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize
> ## CONSUMES
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                    ##
> CONSUMES
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                     ##
> CONSUMES
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize
> ## CONSUMES
> +
> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpace
> Size  ## CONSUMES


Not sure if the above PCDs are really needed by VariableSmmRuntimeDxe.


> +
> +[FeaturePcd]
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics            ##
> CONSUMES
> +
>  [Guids]
> +  ## PRODUCES             ## GUID # Signature of Variable store header
> +  ## CONSUMES             ## GUID # Signature of Variable store header
> +  ## SOMETIMES_PRODUCES   ## SystemTable
> +  gEfiAuthenticatedVariableGuid
> +
> +  ## PRODUCES             ## GUID # Signature of Variable store header
> +  ## CONSUMES             ## GUID # Signature of Variable store header
> +  ## SOMETIMES_PRODUCES   ## SystemTable
> +  gEfiVariableGuid
> +
>    gEfiEventVirtualAddressChangeGuid             ## CONSUMES ## Event
>    gEfiEventExitBootServicesGuid                 ## CONSUMES ## Event
>    ## CONSUMES ## GUID # Locate protocol
> @@ -82,6 +109,8 @@
>    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
>    gEfiImageSecurityDatabaseGuid
> 
> +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> SOMETIMES_CONSUMES ## SystemTable
> +
>  [Depex]
>    gEfiSmmCommunicationProtocolGuid
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> index ca9d23ce9f..95c5310c0b 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> @@ -49,6 +49,8 @@
>    VariableNonVolatile.h
>    VariableParsing.c
>    VariableParsing.h
> +  VariableRuntimeCache.c
> +  VariableRuntimeCache.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> index c527a59891..ceef44dfd2 100644
> --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> @@ -1,7 +1,7 @@
>  /** @file
>    The file defined some common structures used for communicating
> between SMM variable module and SMM variable wrapper module.
> 
> -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #ifndef _SMM_VARIABLE_COMMON_H_
>  #define _SMM_VARIABLE_COMMON_H_
> 
> +#include <Guid/VariableFormat.h>
>  #include <Protocol/VarCheck.h>
> 
>  #define EFI_SMM_VARIABLE_WRITE_GUID \
> @@ -66,6 +67,16 @@ typedef struct {
>  #define
> SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET  10
> 
>  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE        11
> +//
> +// The payload for this function is
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> +//
> +#define
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT
> 12
> +
> +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE                    13
> +//
> +// The payload for this function is
> SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> +//
> +#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> 14
> 
>  ///
>  /// Size of SMM communicate header, without including the payload.
> @@ -120,4 +131,20 @@ typedef struct {
>    UINTN                         VariablePayloadSize;
>  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> 
> +typedef struct {
> +  BOOLEAN                 *ReadLock;
> +  BOOLEAN                 *PendingUpdate;
> +  BOOLEAN                 *HobFlushComplete;
> +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> +}
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> +
> +typedef struct {
> +  UINTN                   TotalHobStorageSize;
> +  UINTN                   TotalNvStorageSize;
> +  UINTN                   TotalVolatileStorageSize;
> +  BOOLEAN                 AuthenticatedVariableUsage;
> +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> +
>  #endif // _SMM_VARIABLE_COMMON_H_
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> index fb574b2e32..b9723c0250 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  ///
>  #define ISO_639_2_ENTRY_SIZE    3
> 
> +///
> +/// The timeout to in 10us units to wait for the
> +/// variable runtime cache read lock to be acquired.
> +///
> +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> +
>  typedef enum {
>    VariableStoreTypeVolatile,
>    VariableStoreTypeHob,
> @@ -64,6 +70,21 @@ typedef enum {
>    VariableStoreTypeMax
>  } VARIABLE_STORE_TYPE;
> 
> +typedef struct {
> +  UINT32                  PendingUpdateOffset;
> +  UINT32                  PendingUpdateLength;
> +  VARIABLE_STORE_HEADER   *Store;
> +} VARIABLE_RUNTIME_CACHE;
> +
> +typedef struct {
> +  BOOLEAN                 *ReadLock;
> +  BOOLEAN                 *PendingUpdate;
> +  BOOLEAN                 *HobFlushComplete;
> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeVolatileCache;
> +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> +
>  typedef struct {
>    VARIABLE_HEADER *CurrPtr;
>    //
> @@ -79,14 +100,16 @@ typedef struct {
>  } VARIABLE_POINTER_TRACK;
> 
>  typedef struct {
> -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> -  EFI_LOCK              VariableServicesLock;
> -  UINT32                ReentrantState;
> -  BOOLEAN               AuthFormat;
> -  BOOLEAN               AuthSupport;
> -  BOOLEAN               EmuNvMode;
> +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> +  EFI_PHYSICAL_ADDRESS            HobVariableBackupBase;


I do not see any usage of the new field "HobVariableBackupBase".
Could you help to double confirm?


> +  EFI_PHYSICAL_ADDRESS            VolatileVariableBase;
> +  EFI_PHYSICAL_ADDRESS            NonVolatileVariableBase;
> +  VARIABLE_RUNTIME_CACHE_CONTEXT  VariableRuntimeCacheContext;
> +  EFI_LOCK                        VariableServicesLock;
> +  UINT32                          ReentrantState;
> +  BOOLEAN                         AuthFormat;
> +  BOOLEAN                         AuthSupport;
> +  BOOLEAN                         EmuNvMode;
>  } VARIABLE_GLOBAL;
> 
>  typedef struct {
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> h
> new file mode 100644
> index 0000000000..09b83eb215
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> h
> @@ -0,0 +1,47 @@
> +/** @file
> +  The common variable volatile store routines shared by the DXE_RUNTIME
> variable
> +  module and the DXE_SMM variable module.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> +#define _VARIABLE_RUNTIME_CACHE_H_
> +
> +#include "Variable.h"
> +
> +/**
> +  Copies any pending updates to runtime variable caches.
> +
> +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> initialized properly.
> +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> +
> +**/
> +EFI_STATUS
> +SynchronizeRuntimeVariableCacheEx (
> +  VOID
> +  );
> +
> +/**
> +  Synchronizes the runtime variable caches with all pending updates outside
> runtime.
> +
> +  Ensures all conditions are met to maintain coherency for runtime cache
> updates.
> +
> +  @param[in] VariableRuntimeCache Variable runtime cache structure for
> the runtime cache being synchronized.
> +  @param[in] Offset               Offset in bytes to apply the update.
> +  @param[in] Length               Length of data in bytes of the update.
> +
> +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> initialized properly.
> +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> +
> +**/
> +EFI_STATUS
> +SynchronizeRuntimeVariableCache (
> +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> +  IN  UINTN                           Offset,
> +  IN  UINTN                           Length
> +  );
> +
> +#endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index 5da2354aa5..bb2fa3fc19 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include "Variable.h"
>  #include "VariableNonVolatile.h"
>  #include "VariableParsing.h"
> +#include "VariableRuntimeCache.h"
> 
>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> 
> @@ -332,6 +333,12 @@ RecordVarErrorFlag (
>        // Update the data in NV cache.
>        //
>        *VarErrFlag = TempFlag;
> +      Status =  SynchronizeRuntimeVariableCache (
> +                  &mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache,
> +                  (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN)
> mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
> +                  sizeof (TempFlag)
> +                  );
> +      ASSERT_EFI_ERROR (Status);
>      }
>    }
>  }
> @@ -755,12 +762,24 @@ Reclaim (
> 
>  Done:
>    if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode) {
> +    Status =  SynchronizeRuntimeVariableCache (
> +                &mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e,
> +                0,
> +                VariableStoreHeader->Size
> +                );
> +    ASSERT_EFI_ERROR (Status);
>      FreePool (ValidBuffer);
>    } else {
>      //
>      // For NV variable reclaim, we use mNvVariableCache as the buffer, so
> copy the data back.
>      //
> -    CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase,
> VariableStoreHeader->Size);
> +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase,
> VariableStoreHeader->Size);
> +    Status =  SynchronizeRuntimeVariableCache (
> +                &(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache),
> +                0,
> +                VariableStoreHeader->Size
> +                );
> +    ASSERT_EFI_ERROR (Status);
>    }
> 
>    return Status;
> @@ -1592,6 +1611,7 @@ UpdateVariable (
>    VARIABLE_POINTER_TRACK              *Variable;
>    VARIABLE_POINTER_TRACK              NvVariable;
>    VARIABLE_STORE_HEADER               *VariableStoreHeader;
> +  VARIABLE_RUNTIME_CACHE              *VolatileCacheInstance;
>    UINT8                               *BufferForMerge;
>    UINTN                               MergedBufSize;
>    BOOLEAN                             DataReady;
> @@ -2235,6 +2255,21 @@ UpdateVariable (
>    }
> 
>  Done:
> +  if (!EFI_ERROR (Status)) {
> +    if (Variable->Volatile) {
> +      VolatileCacheInstance = &(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e);
> +    } else {
> +      VolatileCacheInstance = &(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache);
> +    }
> +
> +    Status =  SynchronizeRuntimeVariableCache (
> +                VolatileCacheInstance,
> +                0,
> +                VolatileCacheInstance->Store->Size
> +                );
> +    ASSERT_EFI_ERROR (Status);
> +  }
> +
>    return Status;
>  }
> 
> @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
>          ErrorFlag = TRUE;
>        }
>      }
> +    Status =  SynchronizeRuntimeVariableCache (
> +                &mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache,
> +                0,
> +                mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> tore->Size
> +                );
> +    ASSERT_EFI_ERROR (Status);
>      if (ErrorFlag) {
>        //
>        // We still have HOB variable(s) not flushed in flash.
> @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
>        // All HOB variables have been flushed in flash.
>        //
>        DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been
> flushed in flash.\n"));
> +      *(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.HobFlushComplete) = TRUE;
>        if (!AtRuntime ()) {
>          FreePool ((VOID *) VariableStoreHeader);
>        }
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> new file mode 100644
> index 0000000000..2642d9b000
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> @@ -0,0 +1,153 @@
> +/** @file
> +  The common variable volatile store routines shared by the DXE_RUNTIME
> variable
> +  module and the DXE_SMM variable module.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - variable data. They may be input in
> SMM mode.
> +  This external input must be validated carefully to avoid security issue like
> +  buffer overflow, integer overflow.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "VariableParsing.h"
> +#include "VariableRuntimeCache.h"
> +
> +extern VARIABLE_MODULE_GLOBAL   *mVariableModuleGlobal;
> +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> +
> +/**
> +  Copies any pending updates to runtime variable caches.
> +
> +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> initialized properly.
> +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> +
> +**/
> +EFI_STATUS
> +SynchronizeRuntimeVariableCacheEx (


It is not clear to me why this function is named as the "Ex" version of function
SynchronizeRuntimeVariableCache(). For me, this function looks more like a basic
version.

I would suggest a name change for the functions provided in file
VariableRuntimeCache.c to better reflect their usage model.


> +  VOID
> +  )
> +{


I would recommend that at least a local variable should be introduced to reduce
the duplications of:

"mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext"

in this function in order to make it easier to read.


> +  if (
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.St
> ore == NULL ||
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.Store == NULL ||
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
> +    ) {
> +    return EFI_UNSUPPORTED;
> +  }
> +
> +  if (*(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate)) {
> +    if (
> +      mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> tore != NULL &&
> +      mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0
> +      ) {
> +      CopyMem (
> +        (VOID *) (
> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> tore) +
> +          mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> endingUpdateOffset
> +          ),
> +        (VOID *) (
> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase) +
> +          mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> endingUpdateOffset
> +          ),
> +        mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> endingUpdateLength
> +        );
> +      mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> endingUpdateLength = 0;
> +      mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> endingUpdateOffset = 0;
> +    }
> +
> +    CopyMem (
> +      (VOID *) (
> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.St
> ore) +
> +        mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> ndingUpdateOffset
> +        ),
> +      (VOID *) (
> +        ((UINT8 *) (UINTN) mNvVariableCache) +
> +        mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> ndingUpdateOffset
> +        ),
> +      mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> ndingUpdateLength
> +      );
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> ndingUpdateLength = 0;
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> ndingUpdateOffset = 0;
> +
> +    CopyMem (
> +      (VOID *) (
> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.Store) +
> +        mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.PendingUpdateOffset
> +      ),
> +      (VOID *) (
> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase) +
> +        mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.PendingUpdateOffset
> +        ),
> +      mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.PendingUpdateLength
> +      );
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.PendingUpdateLength = 0;
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> e.PendingUpdateOffset = 0;
> +    *(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = FALSE;
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Synchronizes the runtime variable caches with all pending updates outside
> runtime.
> +
> +  Ensures all conditions are met to maintain coherency for runtime cache
> updates.
> +
> +  @param[in] VariableRuntimeCache Variable runtime cache structure for
> the runtime cache being synchronized.
> +  @param[in] Offset               Offset in bytes to apply the update.
> +  @param[in] Length               Length of data in bytes of the update.
> +
> +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> initialized properly.
> +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> +
> +**/
> +EFI_STATUS
> +SynchronizeRuntimeVariableCache (
> +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> +  IN  UINTN                           Offset,
> +  IN  UINTN                           Length
> +  )
> +{
> +  if (VariableRuntimeCache == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  } else if (VariableRuntimeCache->Store == NULL) {
> +      // Runtime cache is not available yet at this point,
> +      // Return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET to let it
> progress
> +      return EFI_SUCCESS;
> +  }
> +
> +  if (
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL ||
> +    mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL
> +    ) {
> +    return EFI_UNSUPPORTED;
> +  }
> +
> +  if (
> +    *(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
> +    VariableRuntimeCache->PendingUpdateLength > 0
> +    ) {
> +    VariableRuntimeCache->PendingUpdateLength =
> +      (UINT32) (
> +        MAX (
> +          (UINTN) (VariableRuntimeCache->PendingUpdateOffset +
> VariableRuntimeCache->PendingUpdateLength),
> +          Offset + Length
> +        ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset)
> +      );
> +    VariableRuntimeCache->PendingUpdateOffset =
> +      (UINT32) MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset,
> Offset);
> +  } else {
> +    VariableRuntimeCache->PendingUpdateLength = (UINT32) Length;
> +    VariableRuntimeCache->PendingUpdateOffset = (UINT32) Offset;
> +  }
> +  *(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
> +
> +  if (*(mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
> +    return SynchronizeRuntimeVariableCacheEx ();
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> index ce409f22a3..8d767f75ac 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include <Guid/SmmVariableCommon.h>
>  #include "Variable.h"
>  #include "VariableParsing.h"
> +#include "VariableRuntimeCache.h"
> +
> +extern VARIABLE_STORE_HEADER                         *mNvVariableCache;
> 
>  BOOLEAN                                              mAtRuntime              = FALSE;
>  UINT8                                                *mVariableBufferPayload = NULL;
> @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
>  EFI_STATUS
>  EFIAPI
>  SmmVariableHandler (
> -  IN     EFI_HANDLE                                DispatchHandle,
> -  IN     CONST VOID                                *RegisterContext,
> -  IN OUT VOID                                      *CommBuffer,
> -  IN OUT UINTN                                     *CommBufferSize
> +  IN     EFI_HANDLE                                       DispatchHandle,
> +  IN     CONST VOID                                       *RegisterContext,
> +  IN OUT VOID                                             *CommBuffer,
> +  IN OUT UINTN                                            *CommBufferSize
>    )
>  {
> -  EFI_STATUS                                       Status;
> -  SMM_VARIABLE_COMMUNICATE_HEADER
> *SmmVariableFunctionHeader;
> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> *SmmVariableHeader;
> -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> *GetNextVariableName;
> -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> *QueryVariableInfo;
> -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> *GetPayloadSize;
> -  VARIABLE_INFO_ENTRY                              *VariableInfo;
> -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE           *VariableToLock;
> -  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> *CommVariableProperty;
> -  UINTN                                            InfoSize;
> -  UINTN                                            NameBufferSize;
> -  UINTN                                            CommBufferPayloadSize;
> -  UINTN                                            TempCommBufferSize;
> +  EFI_STATUS                                              Status;
> +  SMM_VARIABLE_COMMUNICATE_HEADER
> *SmmVariableFunctionHeader;
> +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> *SmmVariableHeader;
> +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> *GetNextVariableName;
> +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> *QueryVariableInfo;
> +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> *GetPayloadSize;
> +
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> *RuntimeVariableCacheContext;
> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> *GetRuntimeCacheInfo;
> +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> *VariableToLock;
> +  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> *CommVariableProperty;
> +  VARIABLE_INFO_ENTRY                                     *VariableInfo;
> +  VARIABLE_RUNTIME_CACHE_CONTEXT
> *VariableCacheContext;
> +  VARIABLE_STORE_HEADER                                   *VariableCache;
> +  UINTN                                                   InfoSize;
> +  UINTN                                                   NameBufferSize;
> +  UINTN                                                   CommBufferPayloadSize;
> +  UINTN                                                   TempCommBufferSize;
> 
>    //
>    // If input is invalid, stop processing this SMI
> @@ -789,6 +796,79 @@ SmmVariableHandler (
>                   );
>        CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload,
> CommBufferPayloadSize);
>        break;
> +    case
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT:
> +      if (CommBufferPayloadSize < sizeof
> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT))
> {


The above check is not correct, I think it should be:

if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)) {

Please help to double confirm.
Also, I recommend some security tests should be performed to these new cases in
the variable SMI handler.


> +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM
> communication buffer size invalid!\n"));
> +      } else if (mEndOfDxe) {
> +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Cannot
> init context after end of DXE!\n"));
> +      } else {
> +        RuntimeVariableCacheContext =
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> *) SmmVariableFunctionHeader->Data;


Not sure on this one:

Do you think it is necessary to copy the contents in the comm buffer to the
pre-allocated SMM variable buffer payload 'mVariableBufferPayload' to avoid
TOCTOU issue? Since there are some tests (sort of, a couple of ASSERTs) based
on the comm buffer content.


> +        VariableCacheContext = &mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext;
> +
> +        ASSERT (RuntimeVariableCacheContext->RuntimeVolatileCache !=
> NULL);
> +        ASSERT (RuntimeVariableCacheContext->RuntimeNvCache != NULL);
> +        ASSERT (RuntimeVariableCacheContext->PendingUpdate != NULL);
> +        ASSERT (RuntimeVariableCacheContext->ReadLock != NULL);
> +        ASSERT (RuntimeVariableCacheContext->HobFlushComplete != NULL);
> +
> +        VariableCacheContext->VariableRuntimeHobCache.Store      =
> RuntimeVariableCacheContext->RuntimeHobCache;
> +        VariableCacheContext->VariableRuntimeVolatileCache.Store =
> RuntimeVariableCacheContext->RuntimeVolatileCache;
> +        VariableCacheContext->VariableRuntimeNvCache.Store       =
> RuntimeVariableCacheContext->RuntimeNvCache;
> +        VariableCacheContext->PendingUpdate                      =
> RuntimeVariableCacheContext->PendingUpdate;
> +        VariableCacheContext->ReadLock                           =
> RuntimeVariableCacheContext->ReadLock;
> +        VariableCacheContext->HobFlushComplete                   =
> RuntimeVariableCacheContext->HobFlushComplete;
> +
> +        // Set up the intial pending request since the RT cache needs to be in
> sync with SMM cache
> +        if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {
> +          VariableCacheContext-
> >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> +          VariableCacheContext-
> >VariableRuntimeHobCache.PendingUpdateLength = 0;
> +        } else {
> +          VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> +          VariableCacheContext-
> >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> +          VariableCacheContext-
> >VariableRuntimeHobCache.PendingUpdateLength = (UINT32) ((UINTN)
> GetEndPointer (VariableCache) - (UINTN) VariableCache);
> +          CopyGuid (&(VariableCacheContext-
> >VariableRuntimeHobCache.Store->Signature), &(VariableCache-
> >Signature));
> +        }
> +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> +        VariableCacheContext-
> >VariableRuntimeVolatileCache.PendingUpdateOffset   = 0;
> +        VariableCacheContext-
> >VariableRuntimeVolatileCache.PendingUpdateLength   = (UINT32) ((UINTN)
> GetEndPointer (VariableCache) - (UINTN) VariableCache);
> +        CopyGuid (&(VariableCacheContext-
> >VariableRuntimeVolatileCache.Store->Signature), &(VariableCache-
> >Signature));
> +
> +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> mNvVariableCache;
> +        VariableCacheContext-
> >VariableRuntimeNvCache.PendingUpdateOffset = 0;
> +        VariableCacheContext-
> >VariableRuntimeNvCache.PendingUpdateLength = (UINT32) ((UINTN)
> GetEndPointer (VariableCache) - (UINTN) VariableCache);
> +        CopyGuid (&(VariableCacheContext->VariableRuntimeNvCache.Store-
> >Signature), &(VariableCache->Signature));
> +
> +        *(VariableCacheContext->PendingUpdate) = TRUE;
> +        *(VariableCacheContext->ReadLock) = FALSE;
> +        *(VariableCacheContext->HobFlushComplete) = FALSE;
> +      }
> +      Status = EFI_SUCCESS;
> +      break;
> +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> +      Status = SynchronizeRuntimeVariableCacheEx ();
> +      break;
> +    case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> +      if (CommBufferPayloadSize < sizeof
> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM communication
> buffer size invalid!\n"));
> +        return EFI_SUCCESS;
> +      }
> +      GetRuntimeCacheInfo =
> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> SmmVariableFunctionHeader->Data;
> +
> +      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) {
> +        VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> +        GetRuntimeCacheInfo->TotalHobStorageSize = VariableCache->Size;
> +      } else {
> +        GetRuntimeCacheInfo->TotalHobStorageSize = 0;
> +      }
> +
> +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> +      GetRuntimeCacheInfo->TotalVolatileStorageSize = VariableCache->Size;
> +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> mNvVariableCache;
> +      GetRuntimeCacheInfo->TotalNvStorageSize = (UINTN) VariableCache-
> >Size;
> +      GetRuntimeCacheInfo->AuthenticatedVariableUsage =
> mVariableModuleGlobal->VariableGlobal.AuthFormat;
> +
> +      Status = EFI_SUCCESS;
> +      break;
> 
>      default:
>        Status = EFI_UNSUPPORTED;
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.c
> index 0a1888e5ef..46f69765a4 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.c
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> e.c
> @@ -13,7 +13,7 @@
> 
>    InitCommunicateBuffer() is really function to check the variable data size.
> 
> -Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include <Library/UefiRuntimeLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/DebugLib.h>
> +#include <Library/TimerLib.h>
>  #include <Library/UefiLib.h>
>  #include <Library/BaseLib.h>
> 
>  #include <Guid/EventGroup.h>
> +#include <Guid/PiSmmCommunicationRegionTable.h>
>  #include <Guid/SmmVariableCommon.h>
> 
>  #include "PrivilegePolymorphic.h"
> +#include "VariableParsing.h"
> 
>  EFI_HANDLE                       mHandle                    = NULL;
>  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable               = NULL;
> @@ -46,8 +49,19 @@ EFI_EVENT                        mVirtualAddressChangeEvent =
> NULL;
>  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication          =
> NULL;
>  UINT8                           *mVariableBuffer            = NULL;
>  UINT8                           *mVariableBufferPhysical    = NULL;
> +VARIABLE_INFO_ENTRY             *mVariableInfo              = NULL;
> +VARIABLE_STORE_HEADER           *mVariableRuntimeHobCacheBuffer           =
> NULL;
> +VARIABLE_STORE_HEADER           *mVariableRuntimeNvCacheBuffer            =
> NULL;
> +VARIABLE_STORE_HEADER           *mVariableRuntimeVolatileCacheBuffer
> = NULL;
>  UINTN                            mVariableBufferSize;
> +UINTN                            mVariableRuntimeHobCacheBufferSize;
> +UINTN                            mVariableRuntimeNvCacheBufferSize;
> +UINTN                            mVariableRuntimeVolatileCacheBufferSize;
>  UINTN                            mVariableBufferPayloadSize;
> +BOOLEAN                          mVariableRuntimeCachePendingUpdate;
> +BOOLEAN                          mVariableRuntimeCacheReadLock;
> +BOOLEAN                          mVariableAuthFormat;
> +BOOLEAN                          mHobFlushComplete;
>  EFI_LOCK                         mVariableServicesLock;
>  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
>  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
>    }
>  }
> 
> +/**
> +  Return TRUE if ExitBootServices () has been called.
> +
> +  @retval TRUE If ExitBootServices () has been called.
> +**/
> +BOOLEAN
> +AtRuntime (
> +  VOID
> +  )


I think we can either:
1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
2. Move AtRuntime() to VariableParsing.c so that the function can be shared
   with VariableRuntimeDxe & VariableSmmRuntimeDxe. And then update the
   EfiAtRuntime() usages to AtRuntime() for VariableSmmRuntimeDxe.


> +{
> +  return EfiAtRuntime ();
> +}
> +
> +/**
> +  Initialize the variable cache buffer as an empty variable store.
> +
> +  @param[out]     VariableCacheBuffer     A pointer to pointer of a cache
> variable store.
> +  @param[in,out]  TotalVariableCacheSize  On input, the minimum size
> needed for the UEFI variable store cache
> +                                          buffer that is allocated. On output, the actual size of
> the buffer allocated.
> +                                          If TotalVariableCacheSize is zero, a buffer will not be
> allocated and the
> +                                          function will return with EFI_SUCCESS.
> +
> +  @retval EFI_SUCCESS             The variable cache was allocated and initialized
> successfully.
> +  @retval EFI_INVALID_PARAMETER   A given pointer is NULL or an invalid
> variable store size was specified.
> +  @retval EFI_OUT_OF_RESOURCES    Insufficient resources are available to
> allocate the variable store cache buffer.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +InitVariableCache (
> +  OUT    VARIABLE_STORE_HEADER   **VariableCacheBuffer,
> +  IN OUT UINTN                   *TotalVariableCacheSize
> +  )
> +{
> +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> +
> +  if (TotalVariableCacheSize == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  if (*TotalVariableCacheSize == 0) {
> +    return EFI_SUCCESS;
> +  }
> +  if (VariableCacheBuffer == NULL || *TotalVariableCacheSize < sizeof
> (VARIABLE_STORE_HEADER)) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +  *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof
> (UINT32));
> +
> +  //
> +  // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
> +  //
> +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> AllocateRuntimePages (
> +                            EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)
> +                            );
> +  if (*VariableCacheBuffer == NULL) {
> +    return EFI_OUT_OF_RESOURCES;
> +  }
> +  VariableCacheStorePtr = *VariableCacheBuffer;
> +  SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize,
> (UINT32) 0xFFFFFFFF);
> +
> +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> (VARIABLE_STORE_HEADER));
> +  VariableCacheStorePtr->Size    = (UINT32) *TotalVariableCacheSize;
> +  VariableCacheStorePtr->Format  = VARIABLE_STORE_FORMATTED;
> +  VariableCacheStorePtr->State   = VARIABLE_STORE_HEALTHY;
> +
> +  return EFI_SUCCESS;
> +}
> +
>  /**
>    Initialize the communicate buffer using DataSize and Function.
> 
> @@ -153,6 +234,69 @@ InitCommunicateBuffer (
>  }
> 
> 
> +/**
> +  Gets a SMM communicate buffer from the
> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as an entry in
> the UEFI
> +  system configuration table. A generic SMM communication buffer DXE
> driver may install the table or a custom table
> +  may be installed by a platform-specific driver.
> +
> +  The communicate size is:  SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> +                            DataSize.
> +
> +  @param[in,out]   CommBufferSize   On input, the minimum size needed
> for the communication buffer.
> +                                    On output, the SMM buffer size available at CommBuffer.
> +  @param[out]      CommBuffer       A pointer to an SMM communication
> buffer pointer.
> +
> +  @retval EFI_SUCCESS               The communication buffer was found
> successfully.
> +  @retval EFI_INVALID_PARAMETER     A given pointer is NULL or the
> CommBufferSize is zero.
> +  @retval EFI_NOT_FOUND             The
> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not found.
> +  @retval EFI_OUT_OF_RESOURCES      A valid SMM communicate buffer for
> the requested size is not available.
> +
> +**/
> +EFI_STATUS
> +GetCommunicateBuffer (
> +  IN OUT  UINTN     *CommBufferSize,
> +  OUT     UINT8     **CommBuffer
> +  )


Minor comment:

I found that the consumers of the above function are:
GetRuntimeCacheInfo()
SendRuntimeVariableCacheContextToSmm()

Both of them get called within SmmVariableReady() when the SMM variable driver
finished initialization. I am wondering if they can simply use the pre-allocated
comm buffer (via InitCommunicateBuffer() and using 'mVariableBuffer'), instead
of looking into the configuration table.

In my opinion, this function can be dropped.


> +{
> +  EFI_STATUS                                Status;
> +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> *PiSmmCommunicationRegionTable;
> +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> +  UINTN                                     EntrySize;
> +  UINT32                                    Index;
> +
> +  if (CommBuffer == NULL || CommBufferSize == NULL ||
> *CommBufferSize == 0) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  Status = EfiGetSystemConfigurationTable (
> +             &gEdkiiPiSmmCommunicationRegionTableGuid,
> +             (VOID **) &PiSmmCommunicationRegionTable
> +             );
> +  if (EFI_ERROR (Status) || PiSmmCommunicationRegionTable == NULL) {
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable
> + 1);
> +  EntrySize = 0;
> +  for (Index = 0; Index < PiSmmCommunicationRegionTable-
> >NumberOfEntries; Index++) {
> +    if (Entry->Type == EfiConventionalMemory) {
> +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
> +      if (EntrySize >= *CommBufferSize) {
> +        break;
> +      }
> +    }
> +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry +
> PiSmmCommunicationRegionTable->DescriptorSize);
> +  }
> +
> +  if (Index < PiSmmCommunicationRegionTable->NumberOfEntries) {
> +    *CommBufferSize = EntrySize;
> +    *CommBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;
> +    return EFI_SUCCESS;
> +  }
> +
> +  return EFI_OUT_OF_RESOURCES;
> +}
> +
>  /**
>    Send the data in communicate buffer to SMM.
> 
> @@ -424,6 +568,171 @@ Done:
>    return Status;
>  }
> 
> +/**
> +  Signals SMM to synchronize any pending variable updates with the
> runtime cache(s).
> +
> +**/
> +VOID
> +EFIAPI
> +SyncRuntimeCache (
> +  VOID
> +  )
> +{
> +  //
> +  // Init the communicate buffer. The buffer data size is:
> +  // SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> +  //
> +  InitCommunicateBuffer (NULL, 0,
> SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> +
> +  //
> +  // Send data to SMM.
> +  //
> +  SendCommunicateBuffer (0);
> +}
> +
> +/**
> +  Check whether a SMI must be triggered to retrieve pending cache updates.
> +
> +  If the variable HOB was finished being flushed since the last check for a
> runtime cache update, this function
> +  will prevent the HOB cache from being used for future runtime cache hits.
> +
> +**/
> +VOID
> +EFIAPI
> +CheckForRuntimeCacheSync (
> +  VOID
> +  )
> +{
> +  if (mVariableRuntimeCachePendingUpdate) {
> +    SyncRuntimeCache ();
> +  }
> +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> +
> +  //
> +  // The HOB variable data may have finished being flushed in the runtime
> cache sync update
> +  //
> +  if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer != NULL) {
> +    if (!AtRuntime ()) {
> +      FreePool (mVariableRuntimeHobCacheBuffer);
> +    }
> +    mVariableRuntimeHobCacheBuffer = NULL;
> +  }
> +}
> +
> +/**
> +  This code finds variable in a volatile memory store.
> +
> +  Caution: This function may receive untrusted input.
> +  The data size is external input, so this function will validate it carefully to
> avoid buffer overflow.
> +
> +  @param[in]      VariableName       Name of Variable to be found.
> +  @param[in]      VendorGuid         Variable vendor GUID.
> +  @param[out]     Attributes         Attribute value of the variable found.
> +  @param[in, out] DataSize           Size of Data found. If size is less than the
> +                                     data, this value contains the required size.
> +  @param[out]     Data               Data pointer.
> +
> +  @retval EFI_SUCCESS                Found the specified variable.
> +  @retval EFI_INVALID_PARAMETER      Invalid parameter.
> +  @retval EFI_NOT_FOUND              The specified variable could not be found.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +FindVariableInRuntimeCache (
> +  IN      CHAR16                            *VariableName,
> +  IN      EFI_GUID                          *VendorGuid,
> +  OUT     UINT32                            *Attributes OPTIONAL,
> +  IN OUT  UINTN                             *DataSize,
> +  OUT     VOID                              *Data OPTIONAL
> +  )
> +{
> +  EFI_STATUS              Status;
> +  UINTN                   DelayIndex;
> +  UINTN                   TempDataSize;
> +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> +  VARIABLE_STORE_TYPE     StoreType;
> +  VARIABLE_STORE_HEADER   *VariableStoreList[VariableStoreTypeMax];
> +
> +  Status = EFI_NOT_FOUND;
> +
> +  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> +    MicroSecondDelay (10);
> +  }
> +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> +    ASSERT (!mVariableRuntimeCacheReadLock);
> +
> +    mVariableRuntimeCacheReadLock = TRUE;
> +    CheckForRuntimeCacheSync ();
> +
> +    if (!mVariableRuntimeCachePendingUpdate) {
> +      //
> +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> +      // The index and attributes mapping must be kept in this order as
> FindVariable
> +      // makes use of this mapping to implement search algorithm.
> +      //
> +      VariableStoreList[VariableStoreTypeVolatile] =
> mVariableRuntimeVolatileCacheBuffer;
> +      VariableStoreList[VariableStoreTypeHob]      =
> mVariableRuntimeHobCacheBuffer;
> +      VariableStoreList[VariableStoreTypeNv]       =
> mVariableRuntimeNvCacheBuffer;
> +
> +      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> VariableStoreTypeMax; StoreType++) {
> +        if (VariableStoreList[StoreType] == NULL) {
> +          continue;
> +        }
> +
> +        RtPtrTrack.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> +        RtPtrTrack.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType ==
> VariableStoreTypeVolatile);
> +
> +        Status = FindVariableEx (VariableName, VendorGuid, FALSE,
> &RtPtrTrack);
> +        if (!EFI_ERROR (Status)) {
> +          break;
> +        }
> +      }
> +
> +      if (!EFI_ERROR (Status)) {
> +        //
> +        // Get data size
> +        //
> +        TempDataSize = DataSizeOfVariable (RtPtrTrack.CurrPtr);
> +        ASSERT (TempDataSize != 0);
> +
> +        if (*DataSize >= TempDataSize) {
> +          if (Data == NULL) {
> +            Status = EFI_INVALID_PARAMETER;
> +            goto Done;
> +          }
> +
> +          CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr),
> TempDataSize);
> +          if (Attributes != NULL) {
> +            *Attributes = RtPtrTrack.CurrPtr->Attributes;
> +          }
> +
> +          *DataSize = TempDataSize;
> +
> +          UpdateVariableInfo (VariableName, VendorGuid, RtPtrTrack.Volatile,
> TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> +
> +          Status = EFI_SUCCESS;
> +          goto Done;
> +        } else {
> +          *DataSize = TempDataSize;
> +          Status = EFI_BUFFER_TOO_SMALL;
> +          goto Done;
> +        }
> +      }
> +    }
> +  }
> +
> +Done:
> +  mVariableRuntimeCacheReadLock = FALSE;


If timeout occurs when acquiring the read lock, should this flag be set to FALSE
in such case?

Best Regards,
Hao Wu


> +
> +  return Status;
> +}
> +
>  /**
>    This code finds variable in storage blocks (Volatile or Non-Volatile).
> 
> @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
>    )
>  {
>    EFI_STATUS                                Status;
> -  UINTN                                     PayloadSize;
> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> *SmmVariableHeader;
> -  UINTN                                     TempDataSize;
> -  UINTN                                     VariableNameSize;
> 
>    if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
>      return EFI_INVALID_PARAMETER;
>    }
> -
> -  TempDataSize          = *DataSize;
> -  VariableNameSize      = StrSize (VariableName);
> -  SmmVariableHeader     = NULL;
> -
> -  //
> -  // If VariableName exceeds SMM payload limit. Return failure
> -  //
> -  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> -    return EFI_INVALID_PARAMETER;
> -  }
> -
> -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> -
> -  //
> -  // Init the communicate buffer. The buffer data size is:
> -  // SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> -  //
> -  if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> VariableNameSize) {
> -    //
> -    // If output data buffer exceed SMM payload limit. Trim output buffer to
> SMM payload size
> -    //
> -    TempDataSize = mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> VariableNameSize;
> -  }
> -  PayloadSize = OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> VariableNameSize + TempDataSize;
> -
> -  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader,
> PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> -  if (EFI_ERROR (Status)) {
> -    goto Done;
> -  }
> -  ASSERT (SmmVariableHeader != NULL);
> -
> -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> -  SmmVariableHeader->DataSize   = TempDataSize;
> -  SmmVariableHeader->NameSize   = VariableNameSize;
> -  if (Attributes == NULL) {
> -    SmmVariableHeader->Attributes = 0;
> -  } else {
> -    SmmVariableHeader->Attributes = *Attributes;
> -  }
> -  CopyMem (SmmVariableHeader->Name, VariableName,
> SmmVariableHeader->NameSize);
> -
> -  //
> -  // Send data to SMM.
> -  //
> -  Status = SendCommunicateBuffer (PayloadSize);
> -
> -  //
> -  // Get data from SMM.
> -  //
> -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
> -    //
> -    // SMM CommBuffer DataSize can be a trimed value
> -    // Only update DataSize when needed
> -    //
> -    *DataSize = SmmVariableHeader->DataSize;
> -  }
> -  if (Attributes != NULL) {
> -    *Attributes = SmmVariableHeader->Attributes;
> -  }
> -
> -  if (EFI_ERROR (Status)) {
> -    goto Done;
> -  }
> -
> -  if (Data != NULL) {
> -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name +
> SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
> -  } else {
> -    Status = EFI_INVALID_PARAMETER;
> +  if (VariableName[0] == 0) {
> +    return EFI_NOT_FOUND;
>    }
> 
> -Done:
> +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> +  Status =  FindVariableInRuntimeCache (VariableName, VendorGuid,
> Attributes, DataSize, Data);
>    ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> +
>    return Status;
>  }
> 
> -
>  /**
>    This code Finds the Next available variable.
> 
> @@ -870,6 +1109,17 @@ OnReadyToBoot (
>    //
>    SendCommunicateBuffer (0);
> 
> +  //
> +  // Install the system configuration table for variable info data captured
> +  //
> +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> +    if (mVariableAuthFormat) {
> +      gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid,
> mVariableInfo);
> +    } else {
> +      gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo);
> +    }
> +  }
> +
>    gBS->CloseEvent (Event);
>  }
> 
> @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
>  {
>    EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
>    EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
> +  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeHobCacheBuffer);
> +  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeNvCacheBuffer);
> +  EfiConvertPointer (0x0, (VOID **)
> &mVariableRuntimeVolatileCacheBuffer);
>  }
> 
>  /**
> @@ -969,6 +1222,173 @@ Done:
>    return Status;
>  }
> 
> +/**
> +  This code gets information needed from SMM for runtime cache
> initialization.
> +
> +  @param[out] TotalHobStorageSize         Output pointer for the total HOB
> storage size in bytes.
> +  @param[out] TotalNvStorageSize          Output pointer for the total non-
> volatile storage size in bytes.
> +  @param[out] TotalVolatileStorageSize    Output pointer for the total
> volatile storage size in bytes.
> +  @param[out] AuthenticatedVariableUsage  Output pointer that indicates if
> authenticated variables are to be used.
> +
> +  @retval EFI_SUCCESS                     Retrieved the size successfully.
> +  @retval EFI_INVALID_PARAMETER           TotalNvStorageSize parameter is
> NULL.
> +  @retval EFI_OUT_OF_RESOURCES            Could not allocate a CommBuffer.
> +  @retval Others                          Could not retrieve the size successfully.;
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +GetRuntimeCacheInfo (
> +  OUT UINTN                         *TotalHobStorageSize,
> +  OUT UINTN                         *TotalNvStorageSize,
> +  OUT UINTN                         *TotalVolatileStorageSize,
> +  OUT BOOLEAN                       *AuthenticatedVariableUsage
> +  )
> +{
> +  EFI_STATUS                                          Status;
> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> *SmmGetRuntimeCacheInfo;
> +  EFI_SMM_COMMUNICATE_HEADER
> *SmmCommunicateHeader;
> +  SMM_VARIABLE_COMMUNICATE_HEADER
> *SmmVariableFunctionHeader;
> +  UINTN                                               CommSize;
> +  UINTN                                               CommBufferSize;
> +  UINT8                                               *CommBuffer;
> +
> +  SmmGetRuntimeCacheInfo = NULL;
> +  CommBuffer = NULL;
> +
> +  if (TotalHobStorageSize == NULL || TotalNvStorageSize == NULL ||
> TotalVolatileStorageSize == NULL || AuthenticatedVariableUsage == NULL) {
> +    return EFI_INVALID_PARAMETER;
> +  }
> +
> +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> +
> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> +  CommBufferSize = CommSize;
> +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +  if (CommBuffer == NULL) {
> +    Status = EFI_OUT_OF_RESOURCES;
> +    goto Done;
> +  }
> +  ZeroMem (CommBuffer, CommBufferSize);
> +
> +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> CommBuffer;
> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> &gEfiSmmVariableProtocolGuid);
> +  SmmCommunicateHeader->MessageLength =
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> +
> +  SmmVariableFunctionHeader =
> (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader-
> >Data;
> +  SmmVariableFunctionHeader->Function =
> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> +  SmmGetRuntimeCacheInfo =
> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> SmmVariableFunctionHeader->Data;
> +
> +  //
> +  // Send data to SMM.
> +  //
> +  Status = mSmmCommunication->Communicate (mSmmCommunication,
> CommBuffer, &CommSize);
> +  ASSERT_EFI_ERROR (Status);
> +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> +    Status = EFI_BAD_BUFFER_SIZE;
> +    goto Done;
> +  }
> +
> +  Status = SmmVariableFunctionHeader->ReturnStatus;
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +
> +  //
> +  // Get data from SMM.
> +  //
> +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo->TotalHobStorageSize;
> +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo->TotalNvStorageSize;
> +  *TotalVolatileStorageSize = SmmGetRuntimeCacheInfo-
> >TotalVolatileStorageSize;
> +  *AuthenticatedVariableUsage = SmmGetRuntimeCacheInfo-
> >AuthenticatedVariableUsage;
> +
> +Done:
> +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> +  return Status;
> +}
> +
> +/**
> +  Sends the runtime variable cache context information to SMM.
> +
> +  @retval EFI_SUCCESS               Retrieved the size successfully.
> +  @retval EFI_INVALID_PARAMETER     TotalNvStorageSize parameter is
> NULL.
> +  @retval EFI_OUT_OF_RESOURCES      Could not allocate a CommBuffer.
> +  @retval Others                    Could not retrieve the size successfully.;
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +SendRuntimeVariableCacheContextToSmm (
> +  VOID
> +  )
> +{
> +  EFI_STATUS                                                Status;
> +
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> *SmmRuntimeVarCacheContext;
> +  EFI_SMM_COMMUNICATE_HEADER
> *SmmCommunicateHeader;
> +  SMM_VARIABLE_COMMUNICATE_HEADER
> *SmmVariableFunctionHeader;
> +  UINTN                                                     CommSize;
> +  UINTN                                                     CommBufferSize;
> +  UINT8                                                     *CommBuffer;
> +
> +  SmmRuntimeVarCacheContext = NULL;
> +  CommBuffer = NULL;
> +
> +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> +
> +  //
> +  // Init the communicate buffer. The buffer data size is:
> +  // SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
> +  //
> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
> +  CommBufferSize = CommSize;
> +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +  if (CommBuffer == NULL) {
> +    Status = EFI_OUT_OF_RESOURCES;
> +    goto Done;
> +  }
> +  ZeroMem (CommBuffer, CommBufferSize);
> +
> +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> CommBuffer;
> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> &gEfiSmmVariableProtocolGuid);
> +  SmmCommunicateHeader->MessageLength =
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
> +
> +  SmmVariableFunctionHeader =
> (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader-
> >Data;
> +  SmmVariableFunctionHeader->Function =
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT;
> +  SmmRuntimeVarCacheContext =
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> *) SmmVariableFunctionHeader->Data;
> +
> +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> mVariableRuntimeHobCacheBuffer;
> +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> mVariableRuntimeVolatileCacheBuffer;
> +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> mVariableRuntimeNvCacheBuffer;
> +  SmmRuntimeVarCacheContext->PendingUpdate =
> &mVariableRuntimeCachePendingUpdate;
> +  SmmRuntimeVarCacheContext->ReadLock =
> &mVariableRuntimeCacheReadLock;
> +  SmmRuntimeVarCacheContext->HobFlushComplete =
> &mHobFlushComplete;
> +
> +  //
> +  // Send data to SMM.
> +  //
> +  Status = mSmmCommunication->Communicate (mSmmCommunication,
> CommBuffer, &CommSize);
> +  ASSERT_EFI_ERROR (Status);
> +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> +    Status = EFI_BAD_BUFFER_SIZE;
> +    goto Done;
> +  }
> +
> +  Status = SmmVariableFunctionHeader->ReturnStatus;
> +  if (EFI_ERROR (Status)) {
> +    goto Done;
> +  }
> +
> +Done:
> +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> +  return Status;
> +}
> +
>  /**
>    Initialize variable service and install Variable Architectural protocol.
> 
> @@ -985,7 +1405,7 @@ SmmVariableReady (
>  {
>    EFI_STATUS                                Status;
> 
> -  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> (VOID **)&mSmmVariable);
> +  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> (VOID **) &mSmmVariable);
>    if (EFI_ERROR (Status)) {
>      return;
>    }
> @@ -1007,6 +1427,40 @@ SmmVariableReady (
>    //
>    mVariableBufferPhysical = mVariableBuffer;
> 
> +  //
> +  // Allocate runtime variable cache memory buffers.
> +  //
> +  Status =  GetRuntimeCacheInfo (
> +              &mVariableRuntimeHobCacheBufferSize,
> +              &mVariableRuntimeNvCacheBufferSize,
> +              &mVariableRuntimeVolatileCacheBufferSize,
> +              &mVariableAuthFormat
> +              );
> +  if (!EFI_ERROR (Status)) {
> +    Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer,
> &mVariableRuntimeHobCacheBufferSize);
> +    if (!EFI_ERROR (Status)) {
> +      Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer,
> &mVariableRuntimeNvCacheBufferSize);
> +      if (!EFI_ERROR (Status)) {
> +        Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer,
> &mVariableRuntimeVolatileCacheBufferSize);
> +        if (!EFI_ERROR (Status)) {
> +          Status = InitVariableParsing (mVariableAuthFormat);
> +          ASSERT_EFI_ERROR (Status);
> +
> +          Status = SendRuntimeVariableCacheContextToSmm ();
> +          if (!EFI_ERROR (Status)) {
> +            SyncRuntimeCache ();
> +          }
> +        }
> +      }
> +    }
> +    if (EFI_ERROR (Status)) {
> +      mVariableRuntimeHobCacheBuffer = NULL;
> +      mVariableRuntimeNvCacheBuffer = NULL;
> +      mVariableRuntimeVolatileCacheBuffer = NULL;
> +    }
> +  }
> +  ASSERT_EFI_ERROR (Status);
> +
>    gRT->GetVariable         = RuntimeServiceGetVariable;
>    gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
>    gRT->SetVariable         = RuntimeServiceSetVariable;
> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  2019-09-28  1:47 ` [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() " Kubacki, Michael A
@ 2019-10-03  8:04   ` Wu, Hao A
  2019-10-03 18:52     ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-03  8:04 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> Subject: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
> GetNextVariableName() cache support
> 
> https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> 
> This change implements the Runtime Service GetNextVariableName()
> using the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service
> calls to GetNextVariableName() will no longer trigger a SW SMI.
> 
> Overall system performance and stability will be improved by
> eliminating an SMI for these calls as they typically result in a
> relatively large number of invocations to retrieve all variable
> names in all variable stores present.
> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe
> .c | 118 +++++++++-----------
>  1 file changed, 50 insertions(+), 68 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> xe.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> xe.c
> index 46f69765a4..bc3b56b0ce 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> xe.c
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> xe.c
> @@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName (
>    IN OUT  EFI_GUID                          *VendorGuid
>    )
>  {
> -  EFI_STATUS                                      Status;
> -  UINTN                                           PayloadSize;
> -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> *SmmGetNextVariableName;
> -  UINTN                                           OutVariableNameSize;
> -  UINTN                                           InVariableNameSize;
> +  EFI_STATUS              Status;
> +  UINTN                   DelayIndex;
> +  UINTN                   MaxLen;
> +  UINTN                   VarNameSize;
> +  VARIABLE_HEADER         *VariablePtr;
> +  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> +
> +  Status = EFI_NOT_FOUND;
> 
>    if (VariableNameSize == NULL || VariableName == NULL || VendorGuid ==
> NULL) {
>      return EFI_INVALID_PARAMETER;
>    }
> 
> -  OutVariableNameSize   = *VariableNameSize;
> -  InVariableNameSize    = StrSize (VariableName);
> -  SmmGetNextVariableName = NULL;
> -
>    //
> -  // If input string exceeds SMM payload limit. Return failure
> +  // Calculate the possible maximum length of name string, including the
> Null terminator.
>    //
> -  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
> +  MaxLen = *VariableNameSize / sizeof (CHAR16);
> +  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {
> +    //
> +    // Null-terminator is not found in the first VariableNameSize bytes of the
> input VariableName buffer,
> +    // follow spec to return EFI_INVALID_PARAMETER.
> +    //
>      return EFI_INVALID_PARAMETER;
>    }
> 
> -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> 
> -  //
> -  // Init the communicate buffer. The buffer data size is:
> -  // SMM_COMMUNICATE_HEADER_SIZE +
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> -  //
> -  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {
> -    //
> -    // If output buffer exceed SMM payload limit. Trim output buffer to SMM
> payload size
> -    //
> -    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
> +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> +    MicroSecondDelay (10);
>    }
> -  //
> -  // Payload should be Guid + NameSize + MAX of Input & Output buffer
> -  //
> -  PayloadSize = OFFSET_OF
> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) +
> MAX (OutVariableNameSize, InVariableNameSize);
> -
> -  Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName,
> PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
> -  if (EFI_ERROR (Status)) {
> -    goto Done;
> -  }
> -  ASSERT (SmmGetNextVariableName != NULL);
> -
> -  //
> -  // SMM comm buffer->NameSize is buffer size for return string
> -  //
> -  SmmGetNextVariableName->NameSize = OutVariableNameSize;
> -
> -  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
> -  //
> -  // Copy whole string
> -  //
> -  CopyMem (SmmGetNextVariableName->Name, VariableName,
> InVariableNameSize);
> -  if (OutVariableNameSize > InVariableNameSize) {
> -    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name +
> InVariableNameSize, OutVariableNameSize - InVariableNameSize);
> -  }
> -
> -  //
> -  // Send data to SMM
> -  //
> -  Status = SendCommunicateBuffer (PayloadSize);
> -
> -  //
> -  // Get data from SMM.
> -  //
> -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
> -    //
> -    // SMM CommBuffer NameSize can be a trimed value
> -    // Only update VariableNameSize when needed
> -    //
> -    *VariableNameSize = SmmGetNextVariableName->NameSize;
> -  }
> -  if (EFI_ERROR (Status)) {
> -    goto Done;
> +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> +    ASSERT (!mVariableRuntimeCacheReadLock);
> +
> +    CheckForRuntimeCacheSync ();
> +    mVariableRuntimeCacheReadLock = TRUE;
> +
> +    if (!mVariableRuntimeCachePendingUpdate) {
> +      //
> +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> +      // The index and attributes mapping must be kept in this order as
> FindVariable
> +      // makes use of this mapping to implement search algorithm.
> +      //
> +      VariableStoreHeader[VariableStoreTypeVolatile] =
> mVariableRuntimeVolatileCacheBuffer;
> +      VariableStoreHeader[VariableStoreTypeHob]      =
> mVariableRuntimeHobCacheBuffer;
> +      VariableStoreHeader[VariableStoreTypeNv]       =
> mVariableRuntimeNvCacheBuffer;
> +
> +      Status = GetNextVariableEx (VariableName, VendorGuid,
> VariableStoreHeader, &VariablePtr);
> +      if (!EFI_ERROR (Status)) {
> +        VarNameSize = NameSizeOfVariable (VariablePtr);
> +        ASSERT (VarNameSize != 0);
> +        if (VarNameSize <= *VariableNameSize) {
> +          CopyMem (VariableName, GetVariableNamePtr (VariablePtr),
> VarNameSize);
> +          CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof
> (EFI_GUID));
> +          Status = EFI_SUCCESS;
> +        } else {
> +          Status = EFI_BUFFER_TOO_SMALL;
> +        }
> +
> +        *VariableNameSize = VarNameSize;
> +      }
> +    }
>    }
> -
> -  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
> -  CopyMem (VariableName, SmmGetNextVariableName->Name,
> SmmGetNextVariableName->NameSize);
> -
> -Done:
> +  mVariableRuntimeCacheReadLock = FALSE;


Similar to the previous patch (7/9),
if timeout occurs when acquiring the read lock, should this flag be set to FALSE
in such case?

Best Regards,
Hao Wu


>    ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
>    return Status;
>  }
> --
> 2.16.2.windows.1


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03  8:04   ` Wu, Hao A
@ 2019-10-03 11:00     ` Laszlo Ersek
  2019-10-03 20:53       ` Kubacki, Michael A
  2019-10-03 21:53     ` Kubacki, Michael A
  1 sibling, 1 reply; 45+ messages in thread
From: Laszlo Ersek @ 2019-10-03 11:00 UTC (permalink / raw)
  To: Wu, Hao A, Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

On 10/03/19 10:04, Wu, Hao A wrote:
> Before any comment on the patch, since I am not experienced in the Variable
> driver, I would like to ask for help from other reviewers to look into this
> patch and provide feedbacks as well. Thanks in advance.
> 
> With the above fact, some comments provided below maybe wrong. So please help
> to kindly correct me.
> 
> 
> Some general comments:
> 1. I am not sure if bringing the TimerLib dependency (delay in acquiring the
>    runtime cache read lock) to variable driver (a software driver for the most
>    part) is a good idea.

I agree. Most TimerLib instances do not expect sharing the hardware with
the OS.


Another complication is if the hardware is accessed via MMIO (that is,
not IO ports). MMIO accesses are subject to page tables.

Assuming that MicroSecondDelay() is invoked from the runtime DXE driver
at OS runtime, a platform would have to expose the MMIO area of the
timer hardware in the UEFI memory map as "runtime MMIO". (Via GCD memory
space operations in a platform driver or in the TimerLib constructor.)

Furthermore, the constructor function of the TimerLib instance would
have to register a VirtualAddressChange event handler, and convert the
MMIO address.

Thanks
Laszlo

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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-10-03  8:03   ` Wu, Hao A
@ 2019-10-03 17:35     ` Kubacki, Michael A
  2019-10-08  2:11       ` Wu, Hao A
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 17:35 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

The main reason is cohesiveness in the VariableParsing.c file. These are functions that
are commonly needed for general variable data structure parsing operations. Most of
them just read a member or two in a VARIABLE_STORE_HEADER or VARIABLE_HEADER
data structure,  perform a simple calculation if needed, and return some value. More
complex functions such as FindVariableEx (), do this in iteration across the variable store.
If a function is needed that performs these tasks, it is easier to have them grouped into a
cohesive file than search which one is in Variable.c and VariableParsing.c on a case-by-case
basis. UpdateVariableInfo () is the exception here, I can move this to a separate file
if you prefer.

Also, Variable.c is, in my opinion, far too large. It is on trend to only grow larger:
  * Today: ~4,600 LOC
  * ~2 years ago: ~4,200 LOC
  * ~4 years ago: ~4,100 LOC
  * ~5 years ago: ~3,440 LOC
  * ~8 years ago: ~2,600 LOC

I think moving out generic parsing services such as these better groups related
functionality in the short-term but also aids future refactoring efforts in the file.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:03 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing functions
> 
> A couple of inline comments below:
> 
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> Kinney,
> > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> > parsing functions
> >
> > This change moves the following functions into a dedicated file
> > so they may be used in other variable files as needed. Furthermore,
> > it reduces the overall size of the common Variable.c file.
> >
> >  * DataSizeOfVariable ()
> >  * FindVariableEx ()
> >  * GetEndPointer ()
> >  * GetNextVariablePtr ()
> >  * GetStartPointer ()
> >  * GetVariableDataOffset ()
> >  * GetVariableDataPtr ()
> >  * GetVariableHeaderSize ()
> >  * GetVariableNamePtr ()
> >  * GetVariableStoreStatus ()
> >  * GetVendorGuidPtr ()
> >  * IsValidVariableHeader ()
> >  * NameSizeOfVariable ()
> >  * SetDataSizeOfVariable ()
> >  * SetNameSizeOfVariable ()
> >  * UpdateVariableInfo ()
> >  * VariableCompareTimeStampInternal ()
> >  * VariableServiceGetNextVariableInternal ()
> 
> 
> May I know what are the criteria for the above functions being moved to a
> separate file?
> 
> At first, I think all of them will be consumed by the new codes that
> implement
> the runtime cache. But I found that, for functions like
> GetVariableDataOffset(),
> GetVariableStoreStatus() and etc., their usages are still remained within file
> Variable.c (seems not related with the runtime cache).
> 
> 
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > |   2 +
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |
> 2
> > +
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > |   7 +
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119 -
> ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        |
> 306
> > ++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726
> +--
> > ----------------
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |   3
> +-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        |
> 731
> > ++++++++++++++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |   1
> +
> >  9 files changed, 1052 insertions(+), 845 deletions(-)
> 
> 
> For the below change in VariableStandaloneMm.inf:
> 
> [Guids]
> ...
>   ## SOMETIMES_CONSUMES   ## Variable:L"db"
>   ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
>   ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
>   gEfiImageSecurityDatabaseGuid
> ...
> 
> I think the above GUID is not used by the module specified by the above INF.
> Could you double confirm on this?
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > index 641376c9c5..c35e5fe787 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > @@ -36,6 +36,8 @@
> >    Variable.c
> >    VariableDxe.c
> >    Variable.h
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    PrivilegePolymorphic.h
> >    Measurement.c
> >    TcgMorLockDxe.c
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > index 0a160d269d..626738b9c7 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableTraditionalMm.c
> >    VariableSmm.c
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > index 21bc81163b..1ba8f9ebfb 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableSmm.c
> >    VariableStandaloneMm.c
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > @@ -99,6 +101,11 @@
> >    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
> >    gEfiGlobalVariableGuid
> >
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > +  gEfiImageSecurityDatabaseGuid
> > +
> >    gEfiMemoryOverwriteControlDataGuid            ## SOMETIMES_CONSUMES
> > ## Variable:L"MemoryOverwriteRequestControl"
> >    gEfiMemoryOverwriteRequestControlLockGuid     ##
> > SOMETIMES_PRODUCES   ##
> > Variable:L"MemoryOverwriteRequestControlLock"
> >
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > index 9eac43759f..fb574b2e32 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > @@ -179,89 +179,6 @@ FindVariable (
> >    IN  BOOLEAN                 IgnoreRtCheck
> >    );
> >
> > -/**
> > -
> > -  Gets the pointer to the end of the variable storage area.
> > -
> > -  This function gets pointer to the end of the variable storage
> > -  area, according to the input variable store header.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the end of the variable storage area.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetEndPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  );
> > -
> > -/**
> > -  This code gets the size of variable header.
> > -
> > -  @return Size of variable header in bytes in type UINTN.
> > -
> > -**/
> > -UINTN
> > -GetVariableHeaderSize (
> > -  VOID
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable name.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Name which is Unicode encoding.
> > -
> > -**/
> > -CHAR16 *
> > -GetVariableNamePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> > -/**
> > -  This code gets the pointer to the variable guid.
> > -
> > -  @param Variable   Pointer to the Variable Header.
> > -
> > -  @return A EFI_GUID* pointer to Vendor Guid.
> > -
> > -**/
> > -EFI_GUID *
> > -GetVendorGuidPtr (
> > -  IN VARIABLE_HEADER    *Variable
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Data.
> > -
> > -**/
> > -UINT8 *
> > -GetVariableDataPtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the size of variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -DataSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> >  /**
> >    This function is to check if the remaining variable space is enough to set
> >    all Variables from argument list successfully. The purpose of the check
> > @@ -450,17 +367,6 @@ ReclaimForOS(
> >    VOID
> >    );
> >
> > -/**
> > -  Get non-volatile maximum variable size.
> > -
> > -  @return Non-volatile maximum variable size.
> > -
> > -**/
> > -UINTN
> > -GetNonVolatileMaxVariableSize (
> > -  VOID
> > -  );
> > -
> >  /**
> >    Get maximum variable size, covering both non-volatile and volatile
> variables.
> >
> > @@ -546,31 +452,6 @@ VariableServiceGetVariable (
> >    OUT     VOID              *Data OPTIONAL
> >    );
> >
> > -/**
> > -  This code Finds the Next available variable.
> > -
> > -  Caution: This function may receive untrusted input.
> > -  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > -
> > -  @param[in] VariableName   Pointer to variable name.
> > -  @param[in] VendorGuid     Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > -
> > -  @retval EFI_SUCCESS           The function completed successfully.
> > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> > VendorGuid are not a name and
> > -                                GUID of an existing variable.
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > -  IN  CHAR16                *VariableName,
> > -  IN  EFI_GUID              *VendorGuid,
> > -  OUT VARIABLE_HEADER       **VariablePtr
> > -  );
> > -
> >  /**
> >
> >    This code Finds the Next available variable.
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > new file mode 100644
> > index 0000000000..9d77c4916c
> > --- /dev/null
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > @@ -0,0 +1,306 @@
> > +/** @file
> > +  Functions in this module are associated with variable parsing operations
> > and
> > +  are intended to be usable across variable driver source files.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#ifndef _VARIABLE_PARSING_H_
> > +#define _VARIABLE_PARSING_H_
> > +
> > +#include <Guid/ImageAuthentication.h>
> > +#include "Variable.h"
> > +
> > +/**
> > +
> > +  This code checks if variable header is valid or not.
> > +
> > +  @param Variable           Pointer to the Variable Header.
> > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > +
> > +  @retval TRUE              Variable header is valid.
> > +  @retval FALSE             Variable header is not valid.
> > +
> > +**/
> > +BOOLEAN
> > +IsValidVariableHeader (
> > +  IN  VARIABLE_HEADER       *Variable,
> > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the current status of Variable Store.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @retval EfiRaw         Variable store status is raw.
> > +  @retval EfiValid       Variable store status is valid.
> > +  @retval EfiInvalid     Variable store status is invalid.
> > +
> > +**/
> > +VARIABLE_STORE_STATUS
> > +GetVariableStoreStatus (
> > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > +  );
> > +
> > +/**
> > +  This code gets the size of variable header.
> > +
> > +  @return Size of variable header in bytes in type UINTN.
> > +
> > +**/
> > +UINTN
> > +GetVariableHeaderSize (
> > +  VOID
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the size of name of variable.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return UINTN          Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +NameSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code sets the size of name of variable.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] NameSize   Name size to set.
> > +
> > +**/
> > +VOID
> > +SetNameSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              NameSize
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the size of variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +DataSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code sets the size of variable data.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] DataSize   Data size to set.
> > +
> > +**/
> > +VOID
> > +SetDataSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              DataSize
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable name.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Name which is Unicode encoding.
> > +
> > +**/
> > +CHAR16 *
> > +GetVariableNamePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code gets the pointer to the variable guid.
> > +
> > +  @param Variable   Pointer to the Variable Header.
> > +
> > +  @return A EFI_GUID* pointer to Vendor Guid.
> > +
> > +**/
> > +EFI_GUID *
> > +GetVendorGuidPtr (
> > +  IN VARIABLE_HEADER    *Variable
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Data.
> > +
> > +**/
> > +UINT8 *
> > +GetVariableDataPtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code gets the variable data offset related to variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Variable Data offset.
> > +
> > +**/
> > +UINTN
> > +GetVariableDataOffset (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the next variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to next variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetNextVariablePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +
> > +  Gets the pointer to the first variable header in given variable store area.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the first variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetStartPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  );
> > +
> > +/**
> > +
> > +  Gets the pointer to the end of the variable storage area.
> > +
> > +  This function gets pointer to the end of the variable storage
> > +  area, according to the input variable store header.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the end of the variable storage area.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetEndPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  );
> > +
> > +/**
> > +  Compare two EFI_TIME data.
> > +
> > +
> > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > +
> > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > +
> > +**/
> > +BOOLEAN
> > +VariableCompareTimeStampInternal (
> > +  IN EFI_TIME               *FirstTime,
> > +  IN EFI_TIME               *SecondTime
> > +  );
> > +
> > +/**
> > +  Find the variable in the specified variable store.
> > +
> > +  @param[in]       VariableName        Name of the variable to be found
> > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > +  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > +                                       check at runtime when searching variable.
> > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > contains Variable Information.
> > +
> > +  @retval          EFI_SUCCESS         Variable found successfully
> > +  @retval          EFI_NOT_FOUND       Variable not found
> > +**/
> > +EFI_STATUS
> > +FindVariableEx (
> > +  IN     CHAR16                  *VariableName,
> > +  IN     EFI_GUID                *VendorGuid,
> > +  IN     BOOLEAN                 IgnoreRtCheck,
> > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > +  );
> > +
> > +/**
> > +  This code Finds the Next available variable.
> > +
> > +  Caution: This function may receive untrusted input.
> > +  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > +
> > +  @param[in]  VariableName  Pointer to variable name.
> > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > +  @param[out] VariablePtr   Pointer to variable header address.
> > +
> > +  @retval EFI_SUCCESS           The function completed successfully.
> > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > VendorGuid are not a name and
> > +                                GUID of an existing variable.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +VariableServiceGetNextVariableInternal (
> > +  IN  CHAR16                *VariableName,
> > +  IN  EFI_GUID              *VendorGuid,
> > +  OUT VARIABLE_HEADER       **VariablePtr
> > +  );
> > +
> > +/**
> > +  Routine used to track statistical information about variable usage.
> > +  The data is stored in the EFI system table so it can be accessed later.
> > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > +  build flag controls if this feature is enabled.
> > +
> > +  A read that hits in the cache will have Read and Cache true for
> > +  the transaction. Data is allocated by this routine, but never
> > +  freed.
> > +
> > +  @param[in] VariableName   Name of the Variable to track.
> > +  @param[in] VendorGuid     Guid of the Variable to track.
> > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in] Read           TRUE if GetVariable() was called.
> > +  @param[in] Write          TRUE if SetVariable() was called.
> > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > +  @param[in] Cache          TRUE for a cache hit.
> > +
> > +**/
> > +VOID
> > +UpdateVariableInfo (
> > +  IN  CHAR16                  *VariableName,
> > +  IN  EFI_GUID                *VendorGuid,
> > +  IN  BOOLEAN                 Volatile,
> > +  IN  BOOLEAN                 Read,
> > +  IN  BOOLEAN                 Write,
> > +  IN  BOOLEAN                 Delete,
> > +  IN  BOOLEAN                 Cache
> > +  );
> > +
> > +#endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index f32c9c2808..76536308e6 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  **/
> >
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> >
> > @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
> >
> >  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> >
> > -/**
> > -  Routine used to track statistical information about variable usage.
> > -  The data is stored in the EFI system table so it can be accessed later.
> > -  VariableInfo.efi can dump out the table. Only Boot Services variable
> > -  accesses are tracked by this code. The PcdVariableCollectStatistics
> > -  build flag controls if this feature is enabled.
> > -
> > -  A read that hits in the cache will have Read and Cache true for
> > -  the transaction. Data is allocated by this routine, but never
> > -  freed.
> > -
> > -  @param[in] VariableName   Name of the Variable to track.
> > -  @param[in] VendorGuid     Guid of the Variable to track.
> > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > -  @param[in] Read           TRUE if GetVariable() was called.
> > -  @param[in] Write          TRUE if SetVariable() was called.
> > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > -  @param[in] Cache          TRUE for a cache hit.
> > -
> > -**/
> > -VOID
> > -UpdateVariableInfo (
> > -  IN  CHAR16                  *VariableName,
> > -  IN  EFI_GUID                *VendorGuid,
> > -  IN  BOOLEAN                 Volatile,
> > -  IN  BOOLEAN                 Read,
> > -  IN  BOOLEAN                 Write,
> > -  IN  BOOLEAN                 Delete,
> > -  IN  BOOLEAN                 Cache
> > -  )
> > -{
> > -  VARIABLE_INFO_ENTRY   *Entry;
> > -
> > -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > -
> > -    if (AtRuntime ()) {
> > -      // Don't collect statistics at runtime.
> > -      return;
> > -    }
> > -
> > -    if (gVariableInfo == NULL) {
> > -      //
> > -      // On the first call allocate a entry and place a pointer to it in
> > -      // the EFI System Table.
> > -      //
> > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > -      ASSERT (gVariableInfo != NULL);
> > -
> > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > -      ASSERT (gVariableInfo->Name != NULL);
> > -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > -      gVariableInfo->Volatile = Volatile;
> > -    }
> > -
> > -
> > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > -        if (StrCmp (VariableName, Entry->Name) == 0) {
> > -          if (Read) {
> > -            Entry->ReadCount++;
> > -          }
> > -          if (Write) {
> > -            Entry->WriteCount++;
> > -          }
> > -          if (Delete) {
> > -            Entry->DeleteCount++;
> > -          }
> > -          if (Cache) {
> > -            Entry->CacheCount++;
> > -          }
> > -
> > -          return;
> > -        }
> > -      }
> > -
> > -      if (Entry->Next == NULL) {
> > -        //
> > -        // If the entry is not in the table add it.
> > -        // Next iteration of the loop will fill in the data.
> > -        //
> > -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > -        ASSERT (Entry->Next != NULL);
> > -
> > -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > -        ASSERT (Entry->Next->Name != NULL);
> > -        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > -        Entry->Next->Volatile = Volatile;
> > -      }
> > -
> > -    }
> > -  }
> > -}
> > -
> > -
> > -/**
> > -
> > -  This code checks if variable header is valid or not.
> > -
> > -  @param Variable           Pointer to the Variable Header.
> > -  @param VariableStoreEnd   Pointer to the Variable Store End.
> > -
> > -  @retval TRUE              Variable header is valid.
> > -  @retval FALSE             Variable header is not valid.
> > -
> > -**/
> > -BOOLEAN
> > -IsValidVariableHeader (
> > -  IN  VARIABLE_HEADER       *Variable,
> > -  IN  VARIABLE_HEADER       *VariableStoreEnd
> > -  )
> > -{
> > -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > >StartId != VARIABLE_DATA)) {
> > -    //
> > -    // Variable is NULL or has reached the end of variable store,
> > -    // or the StartId is not correct.
> > -    //
> > -    return FALSE;
> > -  }
> > -
> > -  return TRUE;
> > -}
> > -
> > -
> >  /**
> >
> >    This function writes data to the FWH at the correct LBA even if the LBAs
> > @@ -376,345 +252,6 @@ UpdateVariableStore (
> >    return EFI_SUCCESS;
> >  }
> >
> > -
> > -/**
> > -
> > -  This code gets the current status of Variable Store.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @retval EfiRaw         Variable store status is raw.
> > -  @retval EfiValid       Variable store status is valid.
> > -  @retval EfiInvalid     Variable store status is invalid.
> > -
> > -**/
> > -VARIABLE_STORE_STATUS
> > -GetVariableStoreStatus (
> > -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > -  )
> > -{
> > -  if ((CompareGuid (&VarStoreHeader->Signature,
> > &gEfiAuthenticatedVariableGuid) ||
> > -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> > -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > -      ) {
> > -
> > -    return EfiValid;
> > -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > -             VarStoreHeader->Size == 0xffffffff &&
> > -             VarStoreHeader->Format == 0xff &&
> > -             VarStoreHeader->State == 0xff
> > -          ) {
> > -
> > -    return EfiRaw;
> > -  } else {
> > -    return EfiInvalid;
> > -  }
> > -}
> > -
> > -/**
> > -  This code gets the size of variable header.
> > -
> > -  @return Size of variable header in bytes in type UINTN.
> > -
> > -**/
> > -UINTN
> > -GetVariableHeaderSize (
> > -  VOID
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > -  } else {
> > -    Value = sizeof (VARIABLE_HEADER);
> > -  }
> > -
> > -  return Value;
> > -}
> > -
> > -/**
> > -
> > -  This code gets the size of name of variable.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return UINTN          Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -NameSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    if (AuthVariable->State == (UINT8) (-1) ||
> > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) AuthVariable->NameSize;
> > -  } else {
> > -    if (Variable->State == (UINT8) (-1) ||
> > -        Variable->DataSize == (UINT32) (-1) ||
> > -        Variable->NameSize == (UINT32) (-1) ||
> > -        Variable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) Variable->NameSize;
> > -  }
> > -}
> > -
> > -/**
> > -  This code sets the size of name of variable.
> > -
> > -  @param[in] Variable   Pointer to the Variable Header.
> > -  @param[in] NameSize   Name size to set.
> > -
> > -**/
> > -VOID
> > -SetNameSizeOfVariable (
> > -  IN VARIABLE_HEADER    *Variable,
> > -  IN UINTN              NameSize
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    AuthVariable->NameSize = (UINT32) NameSize;
> > -  } else {
> > -    Variable->NameSize = (UINT32) NameSize;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the size of variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -DataSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    if (AuthVariable->State == (UINT8) (-1) ||
> > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) AuthVariable->DataSize;
> > -  } else {
> > -    if (Variable->State == (UINT8) (-1) ||
> > -        Variable->DataSize == (UINT32) (-1) ||
> > -        Variable->NameSize == (UINT32) (-1) ||
> > -        Variable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) Variable->DataSize;
> > -  }
> > -}
> > -
> > -/**
> > -  This code sets the size of variable data.
> > -
> > -  @param[in] Variable   Pointer to the Variable Header.
> > -  @param[in] DataSize   Data size to set.
> > -
> > -**/
> > -VOID
> > -SetDataSizeOfVariable (
> > -  IN VARIABLE_HEADER    *Variable,
> > -  IN UINTN              DataSize
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    AuthVariable->DataSize = (UINT32) DataSize;
> > -  } else {
> > -    Variable->DataSize = (UINT32) DataSize;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable name.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Name which is Unicode encoding.
> > -
> > -**/
> > -CHAR16 *
> > -GetVariableNamePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > -}
> > -
> > -/**
> > -  This code gets the pointer to the variable guid.
> > -
> > -  @param Variable   Pointer to the Variable Header.
> > -
> > -  @return A EFI_GUID* pointer to Vendor Guid.
> > -
> > -**/
> > -EFI_GUID *
> > -GetVendorGuidPtr (
> > -  IN VARIABLE_HEADER    *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    return &AuthVariable->VendorGuid;
> > -  } else {
> > -    return &Variable->VendorGuid;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Data.
> > -
> > -**/
> > -UINT8 *
> > -GetVariableDataPtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  //
> > -  // Be careful about pad size for alignment.
> > -  //
> > -  Value =  (UINTN) GetVariableNamePtr (Variable);
> > -  Value += NameSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > -
> > -  return (UINT8 *) Value;
> > -}
> > -
> > -/**
> > -  This code gets the variable data offset related to variable header.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Variable Data offset.
> > -
> > -**/
> > -UINTN
> > -GetVariableDataOffset (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  //
> > -  // Be careful about pad size for alignment
> > -  //
> > -  Value = GetVariableHeaderSize ();
> > -  Value += NameSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > -
> > -  return Value;
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the next variable header.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to next variable header.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetNextVariablePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  Value =  (UINTN) GetVariableDataPtr (Variable);
> > -  Value += DataSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > -
> > -  //
> > -  // Be careful about pad size for alignment.
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > -}
> > -
> > -/**
> > -
> > -  Gets the pointer to the first variable header in given variable store area.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the first variable header.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetStartPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  )
> > -{
> > -  //
> > -  // The start of variable store.
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > -}
> > -
> > -/**
> > -
> > -  Gets the pointer to the end of the variable storage area.
> > -
> > -  This function gets pointer to the end of the variable storage
> > -  area, according to the input variable store header.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the end of the variable storage area.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetEndPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  )
> > -{
> > -  //
> > -  // The end of variable store
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader
> +
> > VarStoreHeader->Size);
> > -}
> > -
> >  /**
> >    Record variable error flag.
> >
> > @@ -1228,75 +765,6 @@ Done:
> >    return Status;
> >  }
> >
> > -/**
> > -  Find the variable in the specified variable store.
> > -
> > -  @param[in]       VariableName        Name of the variable to be found
> > -  @param[in]       VendorGuid          Vendor GUID to be found.
> > -  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > -                                       check at runtime when searching variable.
> > -  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > contains Variable Information.
> > -
> > -  @retval          EFI_SUCCESS         Variable found successfully
> > -  @retval          EFI_NOT_FOUND       Variable not found
> > -**/
> > -EFI_STATUS
> > -FindVariableEx (
> > -  IN     CHAR16                  *VariableName,
> > -  IN     EFI_GUID                *VendorGuid,
> > -  IN     BOOLEAN                 IgnoreRtCheck,
> > -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > -  )
> > -{
> > -  VARIABLE_HEADER                *InDeletedVariable;
> > -  VOID                           *Point;
> > -
> > -  PtrTrack->InDeletedTransitionPtr = NULL;
> > -
> > -  //
> > -  // Find the variable by walk through HOB, volatile and non-volatile
> variable
> > store.
> > -  //
> > -  InDeletedVariable  = NULL;
> > -
> > -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > -      ) {
> > -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)
> > -       ) {
> > -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > -        if (VariableName[0] == 0) {
> > -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -            InDeletedVariable   = PtrTrack->CurrPtr;
> > -          } else {
> > -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > -            return EFI_SUCCESS;
> > -          }
> > -        } else {
> > -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> >CurrPtr)))
> > {
> > -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > -
> > -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > -            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > (PtrTrack->CurrPtr)) == 0) {
> > -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -                InDeletedVariable     = PtrTrack->CurrPtr;
> > -              } else {
> > -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > -                return EFI_SUCCESS;
> > -              }
> > -            }
> > -          }
> > -        }
> > -      }
> > -    }
> > -  }
> > -
> > -  PtrTrack->CurrPtr = InDeletedVariable;
> > -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > -}
> > -
> > -
> >  /**
> >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> >
> > @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
> >    }
> >  }
> >
> > -/**
> > -  Compare two EFI_TIME data.
> > -
> > -
> > -  @param FirstTime           A pointer to the first EFI_TIME data.
> > -  @param SecondTime          A pointer to the second EFI_TIME data.
> > -
> > -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > -  @retval  FALSE             The FirstTime is later than the SecondTime.
> > -
> > -**/
> > -BOOLEAN
> > -VariableCompareTimeStampInternal (
> > -  IN EFI_TIME               *FirstTime,
> > -  IN EFI_TIME               *SecondTime
> > -  )
> > -{
> > -  if (FirstTime->Year != SecondTime->Year) {
> > -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > -  } else if (FirstTime->Month != SecondTime->Month) {
> > -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > -  } else if (FirstTime->Day != SecondTime->Day) {
> > -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > -  } else if (FirstTime->Hour != SecondTime->Hour) {
> > -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > -  } else if (FirstTime->Minute != SecondTime->Minute) {
> > -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > -  }
> > -
> > -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > -}
> > -
> >  /**
> >    Update the variable region with Variable information. If
> > EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
> >    index of associated public key is needed.
> > @@ -2885,166 +2321,6 @@ Done:
> >    return Status;
> >  }
> >
> > -/**
> > -  This code Finds the Next available variable.
> > -
> > -  Caution: This function may receive untrusted input.
> > -  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > -
> > -  @param[in]  VariableName  Pointer to variable name.
> > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > -
> > -  @retval EFI_SUCCESS           The function completed successfully.
> > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> > VendorGuid are not a name and
> > -                                GUID of an existing variable.
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > -  IN  CHAR16                *VariableName,
> > -  IN  EFI_GUID              *VendorGuid,
> > -  OUT VARIABLE_HEADER       **VariablePtr
> > -  )
> > -{
> > -  VARIABLE_STORE_TYPE     Type;
> > -  VARIABLE_POINTER_TRACK  Variable;
> > -  VARIABLE_POINTER_TRACK  VariableInHob;
> > -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > -  EFI_STATUS              Status;
> > -  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> > -
> > -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > -    //
> > -    // For VariableName is an empty string, FindVariable() will try to find and
> > return
> > -    // the first qualified variable, and if FindVariable() returns error
> > (EFI_NOT_FOUND)
> > -    // as no any variable is found, still go to return the error
> > (EFI_NOT_FOUND).
> > -    //
> > -    if (VariableName[0] != 0) {
> > -      //
> > -      // For VariableName is not an empty string, and FindVariable() returns
> > error as
> > -      // VariableName and VendorGuid are not a name and GUID of an
> existing
> > variable,
> > -      // there is no way to get next variable, follow spec to return
> > EFI_INVALID_PARAMETER.
> > -      //
> > -      Status = EFI_INVALID_PARAMETER;
> > -    }
> > -    goto Done;
> > -  }
> > -
> > -  if (VariableName[0] != 0) {
> > -    //
> > -    // If variable name is not NULL, get next variable.
> > -    //
> > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -  }
> > -
> > -  //
> > -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > -  // The index and attributes mapping must be kept in this order as
> > FindVariable
> > -  // makes use of this mapping to implement search algorithm.
> > -  //
> > -  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeHob]      =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > -
> > -  while (TRUE) {
> > -    //
> > -    // Switch from Volatile to HOB, to Non-Volatile.
> > -    //
> > -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > -      //
> > -      // Find current storage index
> > -      //
> > -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > Type++) {
> > -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > GetStartPointer (VariableStoreHeader[Type]))) {
> > -          break;
> > -        }
> > -      }
> > -      ASSERT (Type < VariableStoreTypeMax);
> > -      //
> > -      // Switch to next storage
> > -      //
> > -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > -        if (VariableStoreHeader[Type] != NULL) {
> > -          break;
> > -        }
> > -      }
> > -      //
> > -      // Capture the case that
> > -      // 1. current storage is the last one, or
> > -      // 2. no further storage
> > -      //
> > -      if (Type == VariableStoreTypeMax) {
> > -        Status = EFI_NOT_FOUND;
> > -        goto Done;
> > -      }
> > -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > -      Variable.CurrPtr  = Variable.StartPtr;
> > -    }
> > -
> > -    //
> > -    // Variable is found
> > -    //
> > -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -          //
> > -          // If it is a IN_DELETED_TRANSITION variable,
> > -          // and there is also a same ADDED one at the same time,
> > -          // don't return it.
> > -          //
> > -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > -          Status = FindVariableEx (
> > -                     GetVariableNamePtr (Variable.CurrPtr),
> > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > -                     FALSE,
> > -                     &VariablePtrTrack
> > -                     );
> > -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > VAR_ADDED) {
> > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -            continue;
> > -          }
> > -        }
> > -
> > -        //
> > -        // Don't return NV variable when HOB overrides it
> > -        //
> > -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > -            (Variable.StartPtr == GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeNv]))
> > -           ) {
> > -          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > -          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > -          Status = FindVariableEx (
> > -                     GetVariableNamePtr (Variable.CurrPtr),
> > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > -                     FALSE,
> > -                     &VariableInHob
> > -                     );
> > -          if (!EFI_ERROR (Status)) {
> > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -            continue;
> > -          }
> > -        }
> > -
> > -        *VariablePtr = Variable.CurrPtr;
> > -        Status = EFI_SUCCESS;
> > -        goto Done;
> > -      }
> > -    }
> > -
> > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -  }
> > -
> > -Done:
> > -  return Status;
> > -}
> > -
> >  /**
> >
> >    This code Finds the Next available variable.
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > index cb6fcebe2d..dc78f68fa9 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > @@ -1,12 +1,13 @@
> >  /** @file
> >    Provides variable driver extended services.
> >
> > -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  **/
> >
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  /**
> >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > new file mode 100644
> > index 0000000000..7de0a90772
> > --- /dev/null
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > @@ -0,0 +1,731 @@
> > +/** @file
> > +  Functions in this module are associated with variable parsing operations
> > and
> > +  are intended to be usable across variable driver source files.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "VariableParsing.h"
> > +
> > +/**
> > +
> > +  This code checks if variable header is valid or not.
> > +
> > +  @param Variable           Pointer to the Variable Header.
> > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > +
> > +  @retval TRUE              Variable header is valid.
> > +  @retval FALSE             Variable header is not valid.
> > +
> > +**/
> > +BOOLEAN
> > +IsValidVariableHeader (
> > +  IN  VARIABLE_HEADER       *Variable,
> > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > +  )
> > +{
> > +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > >StartId != VARIABLE_DATA)) {
> > +    //
> > +    // Variable is NULL or has reached the end of variable store,
> > +    // or the StartId is not correct.
> > +    //
> > +    return FALSE;
> > +  }
> > +
> > +  return TRUE;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the current status of Variable Store.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @retval EfiRaw         Variable store status is raw.
> > +  @retval EfiValid       Variable store status is valid.
> > +  @retval EfiInvalid     Variable store status is invalid.
> > +
> > +**/
> > +VARIABLE_STORE_STATUS
> > +GetVariableStoreStatus (
> > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > +  )
> > +{
> > +  if ((CompareGuid (&VarStoreHeader->Signature,
> > &gEfiAuthenticatedVariableGuid) ||
> > +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> > +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > +      ) {
> > +
> > +    return EfiValid;
> > +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > +             VarStoreHeader->Size == 0xffffffff &&
> > +             VarStoreHeader->Format == 0xff &&
> > +             VarStoreHeader->State == 0xff
> > +          ) {
> > +
> > +    return EfiRaw;
> > +  } else {
> > +    return EfiInvalid;
> > +  }
> > +}
> > +
> > +/**
> > +  This code gets the size of variable header.
> > +
> > +  @return Size of variable header in bytes in type UINTN.
> > +
> > +**/
> > +UINTN
> > +GetVariableHeaderSize (
> > +  VOID
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > +  } else {
> > +    Value = sizeof (VARIABLE_HEADER);
> > +  }
> > +
> > +  return Value;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the size of name of variable.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return UINTN          Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +NameSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    if (AuthVariable->State == (UINT8) (-1) ||
> > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) AuthVariable->NameSize;
> > +  } else {
> > +    if (Variable->State == (UINT8) (-1) ||
> > +        Variable->DataSize == (UINT32) (-1) ||
> > +        Variable->NameSize == (UINT32) (-1) ||
> > +        Variable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) Variable->NameSize;
> > +  }
> > +}
> > +
> > +/**
> > +  This code sets the size of name of variable.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] NameSize   Name size to set.
> > +
> > +**/
> > +VOID
> > +SetNameSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              NameSize
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    AuthVariable->NameSize = (UINT32) NameSize;
> > +  } else {
> > +    Variable->NameSize = (UINT32) NameSize;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the size of variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +DataSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    if (AuthVariable->State == (UINT8) (-1) ||
> > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) AuthVariable->DataSize;
> > +  } else {
> > +    if (Variable->State == (UINT8) (-1) ||
> > +        Variable->DataSize == (UINT32) (-1) ||
> > +        Variable->NameSize == (UINT32) (-1) ||
> > +        Variable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) Variable->DataSize;
> > +  }
> > +}
> > +
> > +/**
> > +  This code sets the size of variable data.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] DataSize   Data size to set.
> > +
> > +**/
> > +VOID
> > +SetDataSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              DataSize
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    AuthVariable->DataSize = (UINT32) DataSize;
> > +  } else {
> > +    Variable->DataSize = (UINT32) DataSize;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable name.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Name which is Unicode encoding.
> > +
> > +**/
> > +CHAR16 *
> > +GetVariableNamePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > +}
> > +
> > +/**
> > +  This code gets the pointer to the variable guid.
> > +
> > +  @param Variable   Pointer to the Variable Header.
> > +
> > +  @return A EFI_GUID* pointer to Vendor Guid.
> > +
> > +**/
> > +EFI_GUID *
> > +GetVendorGuidPtr (
> > +  IN VARIABLE_HEADER    *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    return &AuthVariable->VendorGuid;
> > +  } else {
> > +    return &Variable->VendorGuid;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Data.
> > +
> > +**/
> > +UINT8 *
> > +GetVariableDataPtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  //
> > +  // Be careful about pad size for alignment.
> > +  //
> > +  Value =  (UINTN) GetVariableNamePtr (Variable);
> > +  Value += NameSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > +
> > +  return (UINT8 *) Value;
> > +}
> > +
> > +/**
> > +  This code gets the variable data offset related to variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Variable Data offset.
> > +
> > +**/
> > +UINTN
> > +GetVariableDataOffset (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  //
> > +  // Be careful about pad size for alignment
> > +  //
> > +  Value = GetVariableHeaderSize ();
> > +  Value += NameSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > +
> > +  return Value;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the next variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to next variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetNextVariablePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  Value =  (UINTN) GetVariableDataPtr (Variable);
> > +  Value += DataSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > +
> > +  //
> > +  // Be careful about pad size for alignment.
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > +}
> > +
> > +/**
> > +
> > +  Gets the pointer to the first variable header in given variable store area.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the first variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetStartPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  )
> > +{
> > +  //
> > +  // The start of variable store.
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > +}
> > +
> > +/**
> > +
> > +  Gets the pointer to the end of the variable storage area.
> > +
> > +  This function gets pointer to the end of the variable storage
> > +  area, according to the input variable store header.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the end of the variable storage area.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetEndPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  )
> > +{
> > +  //
> > +  // The end of variable store
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader
> +
> > VarStoreHeader->Size);
> > +}
> > +
> > +/**
> > +  Compare two EFI_TIME data.
> > +
> > +
> > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > +
> > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > +
> > +**/
> > +BOOLEAN
> > +VariableCompareTimeStampInternal (
> > +  IN EFI_TIME               *FirstTime,
> > +  IN EFI_TIME               *SecondTime
> > +  )
> > +{
> > +  if (FirstTime->Year != SecondTime->Year) {
> > +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > +  } else if (FirstTime->Month != SecondTime->Month) {
> > +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > +  } else if (FirstTime->Day != SecondTime->Day) {
> > +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > +  } else if (FirstTime->Hour != SecondTime->Hour) {
> > +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > +  } else if (FirstTime->Minute != SecondTime->Minute) {
> > +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > +  }
> > +
> > +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > +}
> > +
> > +/**
> > +  Find the variable in the specified variable store.
> > +
> > +  @param[in]       VariableName        Name of the variable to be found
> > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > +  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > +                                       check at runtime when searching variable.
> > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > contains Variable Information.
> > +
> > +  @retval          EFI_SUCCESS         Variable found successfully
> > +  @retval          EFI_NOT_FOUND       Variable not found
> > +**/
> > +EFI_STATUS
> > +FindVariableEx (
> > +  IN     CHAR16                  *VariableName,
> > +  IN     EFI_GUID                *VendorGuid,
> > +  IN     BOOLEAN                 IgnoreRtCheck,
> > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > +  )
> > +{
> > +  VARIABLE_HEADER                *InDeletedVariable;
> > +  VOID                           *Point;
> > +
> > +  PtrTrack->InDeletedTransitionPtr = NULL;
> > +
> > +  //
> > +  // Find the variable by walk through HOB, volatile and non-volatile
> variable
> > store.
> > +  //
> > +  InDeletedVariable  = NULL;
> > +
> > +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > +      ) {
> > +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)
> > +       ) {
> > +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes
> &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > +        if (VariableName[0] == 0) {
> > +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +            InDeletedVariable   = PtrTrack->CurrPtr;
> > +          } else {
> > +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > +            return EFI_SUCCESS;
> > +          }
> > +        } else {
> > +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> >CurrPtr)))
> > {
> > +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > +
> > +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > +            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > (PtrTrack->CurrPtr)) == 0) {
> > +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +                InDeletedVariable     = PtrTrack->CurrPtr;
> > +              } else {
> > +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > +                return EFI_SUCCESS;
> > +              }
> > +            }
> > +          }
> > +        }
> > +      }
> > +    }
> > +  }
> > +
> > +  PtrTrack->CurrPtr = InDeletedVariable;
> > +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  This code Finds the Next available variable.
> > +
> > +  Caution: This function may receive untrusted input.
> > +  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > +
> > +  @param[in]  VariableName  Pointer to variable name.
> > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > +  @param[out] VariablePtr   Pointer to variable header address.
> > +
> > +  @retval EFI_SUCCESS           The function completed successfully.
> > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > VendorGuid are not a name and
> > +                                GUID of an existing variable.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +VariableServiceGetNextVariableInternal (
> > +  IN  CHAR16                *VariableName,
> > +  IN  EFI_GUID              *VendorGuid,
> > +  OUT VARIABLE_HEADER       **VariablePtr
> > +  )
> > +{
> > +  VARIABLE_STORE_TYPE     Type;
> > +  VARIABLE_POINTER_TRACK  Variable;
> > +  VARIABLE_POINTER_TRACK  VariableInHob;
> > +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > +  EFI_STATUS              Status;
> > +  VARIABLE_STORE_HEADER
> > *VariableStoreHeader[VariableStoreTypeMax];
> > +
> > +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > +    //
> > +    // For VariableName is an empty string, FindVariable() will try to find
> and
> > return
> > +    // the first qualified variable, and if FindVariable() returns error
> > (EFI_NOT_FOUND)
> > +    // as no any variable is found, still go to return the error
> > (EFI_NOT_FOUND).
> > +    //
> > +    if (VariableName[0] != 0) {
> > +      //
> > +      // For VariableName is not an empty string, and FindVariable() returns
> > error as
> > +      // VariableName and VendorGuid are not a name and GUID of an
> > existing variable,
> > +      // there is no way to get next variable, follow spec to return
> > EFI_INVALID_PARAMETER.
> > +      //
> > +      Status = EFI_INVALID_PARAMETER;
> > +    }
> > +    goto Done;
> > +  }
> > +
> > +  if (VariableName[0] != 0) {
> > +    //
> > +    // If variable name is not NULL, get next variable.
> > +    //
> > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +  }
> > +
> > +  //
> > +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > +  // The index and attributes mapping must be kept in this order as
> > FindVariable
> > +  // makes use of this mapping to implement search algorithm.
> > +  //
> > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeHob]      =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > +
> > +  while (TRUE) {
> > +    //
> > +    // Switch from Volatile to HOB, to Non-Volatile.
> > +    //
> > +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > +      //
> > +      // Find current storage index
> > +      //
> > +      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > Type++) {
> > +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > GetStartPointer (VariableStoreHeader[Type]))) {
> > +          break;
> > +        }
> > +      }
> > +      ASSERT (Type < VariableStoreTypeMax);
> > +      //
> > +      // Switch to next storage
> > +      //
> > +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > +        if (VariableStoreHeader[Type] != NULL) {
> > +          break;
> > +        }
> > +      }
> > +      //
> > +      // Capture the case that
> > +      // 1. current storage is the last one, or
> > +      // 2. no further storage
> > +      //
> > +      if (Type == VariableStoreTypeMax) {
> > +        Status = EFI_NOT_FOUND;
> > +        goto Done;
> > +      }
> > +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > +      Variable.CurrPtr  = Variable.StartPtr;
> > +    }
> > +
> > +    //
> > +    // Variable is found
> > +    //
> > +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State
> ==
> > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +          //
> > +          // If it is a IN_DELETED_TRANSITION variable,
> > +          // and there is also a same ADDED one at the same time,
> > +          // don't return it.
> > +          //
> > +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > +          Status = FindVariableEx (
> > +                     GetVariableNamePtr (Variable.CurrPtr),
> > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > +                     FALSE,
> > +                     &VariablePtrTrack
> > +                     );
> > +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > VAR_ADDED) {
> > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +            continue;
> > +          }
> > +        }
> > +
> > +        //
> > +        // Don't return NV variable when HOB overrides it
> > +        //
> > +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > +            (Variable.StartPtr == GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeNv]))
> > +           ) {
> > +          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > +          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > +          Status = FindVariableEx (
> > +                     GetVariableNamePtr (Variable.CurrPtr),
> > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > +                     FALSE,
> > +                     &VariableInHob
> > +                     );
> > +          if (!EFI_ERROR (Status)) {
> > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +            continue;
> > +          }
> > +        }
> > +
> > +        *VariablePtr = Variable.CurrPtr;
> > +        Status = EFI_SUCCESS;
> > +        goto Done;
> > +      }
> > +    }
> > +
> > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +  }
> > +
> > +Done:
> > +  return Status;
> > +}
> > +
> > +/**
> > +  Routine used to track statistical information about variable usage.
> > +  The data is stored in the EFI system table so it can be accessed later.
> > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > +  build flag controls if this feature is enabled.
> > +
> > +  A read that hits in the cache will have Read and Cache true for
> > +  the transaction. Data is allocated by this routine, but never
> > +  freed.
> > +
> > +  @param[in] VariableName   Name of the Variable to track.
> > +  @param[in] VendorGuid     Guid of the Variable to track.
> > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in] Read           TRUE if GetVariable() was called.
> > +  @param[in] Write          TRUE if SetVariable() was called.
> > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > +  @param[in] Cache          TRUE for a cache hit.
> > +
> > +**/
> > +VOID
> > +UpdateVariableInfo (
> > +  IN  CHAR16                  *VariableName,
> > +  IN  EFI_GUID                *VendorGuid,
> > +  IN  BOOLEAN                 Volatile,
> > +  IN  BOOLEAN                 Read,
> > +  IN  BOOLEAN                 Write,
> > +  IN  BOOLEAN                 Delete,
> > +  IN  BOOLEAN                 Cache
> > +  )
> > +{
> > +  VARIABLE_INFO_ENTRY   *Entry;
> > +
> > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > +
> > +    if (AtRuntime ()) {
> > +      // Don't collect statistics at runtime.
> > +      return;
> > +    }
> > +
> > +    if (gVariableInfo == NULL) {
> > +      //
> > +      // On the first call allocate a entry and place a pointer to it in
> > +      // the EFI System Table.
> > +      //
> > +      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > +      ASSERT (gVariableInfo != NULL);
> > +
> > +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > +      ASSERT (gVariableInfo->Name != NULL);
> > +      StrCpyS (gVariableInfo->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > +      gVariableInfo->Volatile = Volatile;
> > +    }
> > +
> > +
> > +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > +        if (StrCmp (VariableName, Entry->Name) == 0) {
> > +          if (Read) {
> > +            Entry->ReadCount++;
> > +          }
> > +          if (Write) {
> > +            Entry->WriteCount++;
> > +          }
> > +          if (Delete) {
> > +            Entry->DeleteCount++;
> > +          }
> > +          if (Cache) {
> > +            Entry->CacheCount++;
> > +          }
> > +
> > +          return;
> > +        }
> > +      }
> > +
> > +      if (Entry->Next == NULL) {
> > +        //
> > +        // If the entry is not in the table add it.
> > +        // Next iteration of the loop will fill in the data.
> > +        //
> > +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > +        ASSERT (Entry->Next != NULL);
> > +
> > +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > +        ASSERT (Entry->Next->Name != NULL);
> > +        StrCpyS (Entry->Next->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > +        Entry->Next->Volatile = Volatile;
> > +      }
> > +
> > +    }
> > +  }
> > +}
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > index ec463d063e..ce409f22a3 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  #include <Guid/SmmVariableCommon.h>
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  BOOLEAN                                              mAtRuntime              = FALSE;
> >  UINT8                                                *mVariableBufferPayload = NULL;
> > --
> > 2.16.2.windows.1
> 


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

* Re: [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list
  2019-10-03  8:03   ` Wu, Hao A
@ 2019-10-03 18:04     ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 18:04 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

Thanks for the feedback, I'll rename it back to VariableServiceGetNextVariableInternal ()
 and update the comments to refer to FindVariableEx () instead of FindVariable () in V3.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:03 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize
> GetNextVariableEx() store list
> 
> A couple of inline comments below:
> 
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize
> > GetNextVariableEx() store list
> >
> > The majority of logic related to GetNextVariableName () is currently
> > implemented in VariableServiceGetNextVariableInternal (). The list of
> > variable stores to search for the given variable name and variable
> > GUID is defined in the function body. This change renames the function
> > to GetNextVariableEx () since the function is no longer internal to a
> > specific source file and adds a new parameter so that the caller must
> > pass in the list of variable stores to be used in the variable search.
> 
> 
> I am not sure if 'GetNextVariableEx' is a good name for the function, since:
> 
> 1. There is no function named GetNextVariable(), so it is not clear what "Ex"
>    means here.
> 
> 2. The origin function VariableServiceGetNextVariableInternal() does get
> used
>    in multiple source files (Variable.c & VariableExLib.c). I am not sure what
>    is the intention for such renaming.
> 
> 
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 15
> ++-
> > -
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 12 ++-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c   |  8 +-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 78
> > ++++++++++++--------
> >  4 files changed, 73 insertions(+), 40 deletions(-)
> >
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > index 9d77c4916c..0d231511ea 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > @@ -248,27 +248,30 @@ FindVariableEx (
> >    );
> >
> >  /**
> > -  This code Finds the Next available variable.
> > +  This code finds the next available variable.
> >
> >    Caution: This function may receive untrusted input.
> >    This function may be invoked in SMM mode. This function will do
> > basic validation, before parse the data.
> >
> > -  @param[in]  VariableName  Pointer to variable name.
> > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > +  @param[in]  VariableName      Pointer to variable name.
> > +  @param[in]  VendorGuid        Variable Vendor Guid.
> > +  @param[in]  VariableStoreList A list of variable stores that should
> > + be used
> > to get the next variable.
> > +                                The maximum number of entries is the
> > + max value of
> > VARIABLE_STORE_TYPE.
> > +  @param[out] VariablePtr       Pointer to variable header address.
> >
> >    @retval EFI_SUCCESS           The function completed successfully.
> >    @retval EFI_NOT_FOUND         The next variable was not found.
> > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > string, while VendorGuid is NULL.
> > +  @retval EFI_INVALID_PARAMETER If VariableName is nt an empty
> > + string,
> > while VendorGuid is NULL.
> >    @retval EFI_INVALID_PARAMETER The input values of VariableName and
> > VendorGuid are not a name and
> >                                  GUID of an existing variable.
> >
> >  **/
> >  EFI_STATUS
> >  EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > +GetNextVariableEx (
> >    IN  CHAR16                *VariableName,
> >    IN  EFI_GUID              *VendorGuid,
> > +  IN  VARIABLE_STORE_HEADER **VariableStoreList,
> >    OUT VARIABLE_HEADER       **VariablePtr
> >    );
> >
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 76536308e6..816e8f7b8f 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -2358,6 +2358,7 @@ VariableServiceGetNextVariableName (
> >    UINTN                   MaxLen;
> >    UINTN                   VarNameSize;
> >    VARIABLE_HEADER         *VariablePtr;
> > +  VARIABLE_STORE_HEADER
> > *VariableStoreHeader[VariableStoreTypeMax];
> >
> >    if (VariableNameSize == NULL || VariableName == NULL || VendorGuid
> > ==
> > NULL) {
> >      return EFI_INVALID_PARAMETER;
> > @@ -2377,7 +2378,16 @@ VariableServiceGetNextVariableName (
> >
> >    AcquireLockOnlyAtBootTime(&mVariableModuleGlobal-
> > >VariableGlobal.VariableServicesLock);
> >
> > -  Status = VariableServiceGetNextVariableInternal (VariableName,
> > VendorGuid, &VariablePtr);
> > +  //
> > +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > +  // The index and attributes mapping must be kept in this order as
> > FindVariable
> > +  // makes use of this mapping to implement search algorithm.
> > +  //
> > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeHob]      =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > +
> > +  Status = GetNextVariableEx (VariableName, VendorGuid,
> > VariableStoreHeader, &VariablePtr);
> >    if (!EFI_ERROR (Status)) {
> >      VarNameSize = NameSizeOfVariable (VariablePtr);
> >      ASSERT (VarNameSize != 0);
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > index dc78f68fa9..232d9ffe25 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > @@ -98,10 +98,16 @@ VariableExLibFindNextVariable (
> >    EFI_STATUS                    Status;
> >    VARIABLE_HEADER               *VariablePtr;
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr;
> > +  VARIABLE_STORE_HEADER
> > *VariableStoreHeader[VariableStoreTypeMax];
> >
> > -  Status = VariableServiceGetNextVariableInternal (
> > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeHob]      =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > +
> > +  Status = GetNextVariableEx (
> >               VariableName,
> >               VendorGuid,
> > +             VariableStoreHeader,
> >               &VariablePtr
> >               );
> >    if (EFI_ERROR (Status)) {
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > index 7de0a90772..9bc5369a90 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > @@ -476,14 +476,16 @@ FindVariableEx (  }
> >
> >  /**
> > -  This code Finds the Next available variable.
> > +  This code finds the next available variable.
> >
> >    Caution: This function may receive untrusted input.
> >    This function may be invoked in SMM mode. This function will do
> > basic validation, before parse the data.
> >
> > -  @param[in]  VariableName  Pointer to variable name.
> > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > +  @param[in]  VariableName      Pointer to variable name.
> > +  @param[in]  VendorGuid        Variable Vendor Guid.
> > +  @param[in]  VariableStoreList A list of variable stores that should
> > + be used
> > to get the next variable.
> > +                                The maximum number of entries is the
> > + max value of
> > VARIABLE_STORE_TYPE.
> > +  @param[out] VariablePtr       Pointer to variable header address.
> >
> >    @retval EFI_SUCCESS           The function completed successfully.
> >    @retval EFI_NOT_FOUND         The next variable was not found.
> > @@ -494,20 +496,41 @@ FindVariableEx (  **/  EFI_STATUS  EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > +GetNextVariableEx (
> >    IN  CHAR16                *VariableName,
> >    IN  EFI_GUID              *VendorGuid,
> > +  IN  VARIABLE_STORE_HEADER **VariableStoreList,
> >    OUT VARIABLE_HEADER       **VariablePtr
> >    )
> >  {
> > -  VARIABLE_STORE_TYPE     Type;
> > +  EFI_STATUS              Status;
> > +  VARIABLE_STORE_TYPE     StoreType;
> >    VARIABLE_POINTER_TRACK  Variable;
> >    VARIABLE_POINTER_TRACK  VariableInHob;
> >    VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > -  EFI_STATUS              Status;
> > -  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> >
> > -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > +  Status = EFI_NOT_FOUND;
> > +
> > +  if (VariableStoreList == NULL) {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +
> > +  // Check if the variable exists in the given variable store list
> > + for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> > VariableStoreTypeMax; StoreType++) {
> > +    if (VariableStoreList[StoreType] == NULL) {
> > +      continue;
> > +    }
> > +
> > +    Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> > +    Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> > +    Variable.Volatile = (BOOLEAN) (StoreType ==
> > + VariableStoreTypeVolatile);
> > +
> > +    Status = FindVariableEx (VariableName, VendorGuid, FALSE, &Variable);
> > +    if (!EFI_ERROR (Status)) {
> > +      break;
> > +    }
> > +  }
> > +
> >    if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> >      //
> >      // For VariableName is an empty string, FindVariable() will try
> > to find and
> 
> 
> Some description comments within this function that mention
> "FindVariable()"
> can be updated. Since the calling of the FindVariable() has been replaced by
> the above 'for' loop.
> 
> 
> Best Regards,
> Hao Wu
> 
> 
> > return
> > @@ -527,39 +550,30 @@ VariableServiceGetNextVariableInternal (
> >
> >    if (VariableName[0] != 0) {
> >      //
> > -    // If variable name is not NULL, get next variable.
> > +    // If variable name is not empty, get next variable.
> >      //
> >      Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> >    }
> >
> > -  //
> > -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > -  // The index and attributes mapping must be kept in this order as
> > FindVariable
> > -  // makes use of this mapping to implement search algorithm.
> > -  //
> > -  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeHob]      =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > -
> >    while (TRUE) {
> >      //
> > -    // Switch from Volatile to HOB, to Non-Volatile.
> > +    // Switch to the next variable store if needed
> >      //
> >      while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> >        //
> >        // Find current storage index
> >        //
> > -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > Type++) {
> > -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > GetStartPointer (VariableStoreHeader[Type]))) {
> > +      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> > VariableStoreTypeMax; StoreType++) {
> > +        if ((VariableStoreList[StoreType] != NULL) &&
> > + (Variable.StartPtr ==
> > GetStartPointer (VariableStoreList[StoreType]))) {
> >            break;
> >          }
> >        }
> > -      ASSERT (Type < VariableStoreTypeMax);
> > +      ASSERT (StoreType < VariableStoreTypeMax);
> >        //
> >        // Switch to next storage
> >        //
> > -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > -        if (VariableStoreHeader[Type] != NULL) {
> > +      for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) {
> > +        if (VariableStoreList[StoreType] != NULL) {
> >            break;
> >          }
> >        }
> > @@ -568,12 +582,12 @@ VariableServiceGetNextVariableInternal (
> >        // 1. current storage is the last one, or
> >        // 2. no further storage
> >        //
> > -      if (Type == VariableStoreTypeMax) {
> > +      if (StoreType == VariableStoreTypeMax) {
> >          Status = EFI_NOT_FOUND;
> >          goto Done;
> >        }
> > -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > +      Variable.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> > +      Variable.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> >        Variable.CurrPtr  = Variable.StartPtr;
> >      }
> >
> > @@ -605,11 +619,11 @@ VariableServiceGetNextVariableInternal (
> >          //
> >          // Don't return NV variable when HOB overrides it
> >          //
> > -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > -            (Variable.StartPtr == GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeNv]))
> > +        if ((VariableStoreList[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreList[VariableStoreTypeNv] != NULL) &&
> > +            (Variable.StartPtr == GetStartPointer
> > (VariableStoreList[VariableStoreTypeNv]))
> >             ) {
> > -          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > -          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > +          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreList[VariableStoreTypeHob]);
> > +          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreList[VariableStoreTypeHob]);
> >            Status = FindVariableEx (
> >                       GetVariableNamePtr (Variable.CurrPtr),
> >                       GetVendorGuidPtr (Variable.CurrPtr),
> > --
> > 2.16.2.windows.1
> 


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

* Re: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  2019-10-03  8:03   ` Wu, Hao A
@ 2019-10-03 18:05     ` Kubacki, Michael A
  2019-10-08  2:11       ` [edk2-devel] " Wu, Hao A
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 18:05 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

Your understanding is correct.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:04 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> VARIABLE_INFO_ENTRY buffer
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> > VARIABLE_INFO_ENTRY buffer
> >
> > UpdateVariableInfo () currently accepts parameters regarding updates
> > to be made to a global variable of type VARIABLE_INFO_ENTRY. This
> > change passes the structure by pointer to UpdateVariableInfo () so
> > structures can be updated outside the fixed global variable.
> 
> 
> For:
> "... so structures can be updated outside the fixed global variable "
> 
> Do you mean:
> 
> VARIABLE_INFO_ENTRY structure pointers other than "&gVariableInfo" can
> be passed to UpdateVariableInfo().
> 
> Is my understanding correct? If so,
> Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 18
> > +++++----
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 14 +++---
> -
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 41
> > +++++++++++---------
> >  3 files changed, 39 insertions(+), 34 deletions(-)
> >
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > index 0d231511ea..6f2000f3ee 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > @@ -286,13 +286,14 @@ GetNextVariableEx (
> >    the transaction. Data is allocated by this routine, but never
> >    freed.
> >
> > -  @param[in] VariableName   Name of the Variable to track.
> > -  @param[in] VendorGuid     Guid of the Variable to track.
> > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > -  @param[in] Read           TRUE if GetVariable() was called.
> > -  @param[in] Write          TRUE if SetVariable() was called.
> > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > -  @param[in] Cache          TRUE for a cache hit.
> > +  @param[in]      VariableName   Name of the Variable to track.
> > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in]      Read           TRUE if GetVariable() was called.
> > +  @param[in]      Write          TRUE if SetVariable() was called.
> > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > +  @param[in]      Cache          TRUE for a cache hit.
> > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > VARIABLE_INFO_ENTRY structures.
> >
> >  **/
> >  VOID
> > @@ -303,7 +304,8 @@ UpdateVariableInfo (
> >    IN  BOOLEAN                 Read,
> >    IN  BOOLEAN                 Write,
> >    IN  BOOLEAN                 Delete,
> > -  IN  BOOLEAN                 Cache
> > +  IN  BOOLEAN                 Cache,
> > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> >    );
> >
> >  #endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 816e8f7b8f..1a57d7e1ba 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -1641,7 +1641,7 @@ UpdateVariable (
> >          // go to delete this variable in variable HOB and
> >          // try to flush other variables from HOB to flash.
> >          //
> > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> FALSE,
> > TRUE, FALSE);
> > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > + FALSE,
> > TRUE, FALSE, &gVariableInfo);
> >          FlushHobVariableToFlash (VariableName, VendorGuid);
> >          return EFI_SUCCESS;
> >        }
> > @@ -1758,7 +1758,7 @@ UpdateVariable (
> >                   &State
> >                   );
> >        if (!EFI_ERROR (Status)) {
> > -        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > FALSE, FALSE, TRUE, FALSE);
> > +        UpdateVariableInfo (VariableName, VendorGuid,
> > + Variable->Volatile,
> > FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
> >          if (!Variable->Volatile) {
> >            CacheVariable->CurrPtr->State = State;
> >            FlushHobVariableToFlash (VariableName, VendorGuid); @@
> > -1777,7 +1777,7 @@ UpdateVariable (
> >        //
> >        // Variable content unchanged and no need to update timestamp,
> > just return.
> >        //
> > -      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > FALSE, TRUE, FALSE, FALSE);
> > +      UpdateVariableInfo (VariableName, VendorGuid,
> > + Variable->Volatile,
> > FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
> >        Status = EFI_SUCCESS;
> >        goto Done;
> >      } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) || @@
> > -2006,7 +2006,7 @@ UpdateVariable (
> >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > CacheVariable->Variable-
> > >StartPtr));
> >            CacheVariable->InDeletedTransitionPtr = NULL;
> >          }
> > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE,
> > FALSE, FALSE);
> > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > + TRUE,
> > FALSE, FALSE, &gVariableInfo);
> >          FlushHobVariableToFlash (VariableName, VendorGuid);
> >        } else {
> >          if (IsCommonUserVariable && ((VarSize +
> > mVariableModuleGlobal-
> > >CommonUserVariableTotalSize) > mVariableModuleGlobal-
> > >CommonMaxUserVariableSpace)) {
> > @@ -2156,7 +2156,7 @@ UpdateVariable (
> >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > CacheVariable->Variable-
> > >StartPtr));
> >            CacheVariable->InDeletedTransitionPtr = NULL;
> >          }
> > -        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE,
> > FALSE, FALSE);
> > +        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE,
> > + TRUE,
> > FALSE, FALSE, &gVariableInfo);
> >        }
> >        goto Done;
> >      }
> > @@ -2227,7 +2227,7 @@ UpdateVariable (
> >    }
> >
> >    if (!EFI_ERROR (Status)) {
> > -    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE,
> > FALSE, FALSE);
> > +    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE,
> > + TRUE,
> > FALSE, FALSE, &gVariableInfo);
> >      if (!Volatile) {
> >        FlushHobVariableToFlash (VariableName, VendorGuid);
> >      }
> > @@ -2306,7 +2306,7 @@ VariableServiceGetVariable (
> >      }
> >
> >      *DataSize = VarDataSize;
> > -    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile,
> TRUE,
> > FALSE, FALSE, FALSE);
> > +    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile,
> > + TRUE,
> > FALSE, FALSE, FALSE, &gVariableInfo);
> >
> >      Status = EFI_SUCCESS;
> >      goto Done;
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > index 9bc5369a90..394195342d 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > @@ -660,13 +660,14 @@ Done:
> >    the transaction. Data is allocated by this routine, but never
> >    freed.
> >
> > -  @param[in] VariableName   Name of the Variable to track.
> > -  @param[in] VendorGuid     Guid of the Variable to track.
> > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > -  @param[in] Read           TRUE if GetVariable() was called.
> > -  @param[in] Write          TRUE if SetVariable() was called.
> > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > -  @param[in] Cache          TRUE for a cache hit.
> > +  @param[in]      VariableName   Name of the Variable to track.
> > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in]      Read           TRUE if GetVariable() was called.
> > +  @param[in]      Write          TRUE if SetVariable() was called.
> > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > +  @param[in]      Cache          TRUE for a cache hit.
> > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > VARIABLE_INFO_ENTRY structures.
> >
> >  **/
> >  VOID
> > @@ -677,35 +678,38 @@ UpdateVariableInfo (
> >    IN  BOOLEAN                 Read,
> >    IN  BOOLEAN                 Write,
> >    IN  BOOLEAN                 Delete,
> > -  IN  BOOLEAN                 Cache
> > +  IN  BOOLEAN                 Cache,
> > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> >    )
> >  {
> >    VARIABLE_INFO_ENTRY   *Entry;
> >
> >    if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > -
> > +    if (VariableName == NULL || VendorGuid == NULL || VariableInfo ==
> > NULL) {
> > +      return;
> > +    }
> >      if (AtRuntime ()) {
> >        // Don't collect statistics at runtime.
> >        return;
> >      }
> >
> > -    if (gVariableInfo == NULL) {
> > +    if (*VariableInfo == NULL) {
> >        //
> >        // On the first call allocate a entry and place a pointer to it in
> >        // the EFI System Table.
> >        //
> > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > -      ASSERT (gVariableInfo != NULL);
> > +      *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > +      ASSERT (*VariableInfo != NULL);
> >
> > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > -      ASSERT (gVariableInfo->Name != NULL);
> > -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > -      gVariableInfo->Volatile = Volatile;
> > +      CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
> > +      (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
> > +      ASSERT ((*VariableInfo)->Name != NULL);
> > +      StrCpyS ((*VariableInfo)->Name,
> > + StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > +      (*VariableInfo)->Volatile = Volatile;
> >      }
> >
> >
> > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > +    for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next)
> > + {
> >        if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> >          if (StrCmp (VariableName, Entry->Name) == 0) {
> >            if (Read) {
> > @@ -739,7 +743,6 @@ UpdateVariableInfo (
> >          StrCpyS (Entry->Next->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> >          Entry->Next->Volatile = Volatile;
> >        }
> > -
> >      }
> >    }
> >  }
> > --
> > 2.16.2.windows.1
> 


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

* Re: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-10-03  8:04   ` [edk2-devel] " Wu, Hao A
@ 2019-10-03 18:35     ` Kubacki, Michael A
  2019-10-16  7:55       ` Wu, Hao A
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 18:35 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

I will make the following changes in V3:

> InitVariableParsing() seems an internal function, the 'EFIAPI' keyword can be
> dropped. Please help to update the function definition in .C file as well.

I will remove the EFIAPI keyword.

> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 1a57d7e1ba..53d797152c 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > (PcdMaxVariableSize);
> >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) :
> > mVariableModuleGlobal->MaxVariableSize);
> >
> > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat);
> > +  ASSERT_EFI_ERROR (Status);
> > +
> 
> 
> After the above initialization, mVariableModuleGlobal-
> >VariableGlobal.AuthFormat
> will be changed temporarily within
> ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable store
> will be converted to the auth format:
> 
> VOID *
> ConvertNormalVarStorageToAuthVarStorage (
>   VARIABLE_STORE_HEADER *NormalVarStorage
>   )
> {
>   ...
>   //
>   // Set AuthFormat as FALSE for normal variable storage
>   //
>   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
>   ...
>   //
>   // Restore AuthFormat
>   //
>   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
>   return AuthVarStorage;
> }
> 
> 
> I think there will be issues in such converting, since I found that at least
> GetVariableHeaderSize() and NameSizeOfVariable() get called during the
> execution of ConvertNormalVarStorageToAuthVarStorage(). And they are
> checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> >VariableGlobal.AuthFormat'.
> 
>

You're right that will be a problem. I missed this temporary change in the value.
I'm going to have all the functions dependent on authentication status in
VariableParsing.c take it as a parameter and let the respective drivers linked
against it maintain their own single copy of the authentication state.

> >    //
> >    // Parse non-volatile variable data and get last variable offset.
> >    //
> > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> >
> >    //
> >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > -  // has been initialized in InitNonVolatileVariableStore().
> > +  // is initialized in InitNonVolatileVariableStore().
> >    //
> >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable
> > format!\n"));
> > -    //
> > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > initialize it.
> > -    //
> > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> >    } else {
> >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > variable support!\n"));
> > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> 
> 
> Not sure why the above changes belong to this patch.
> Could you help to double confirm?

This was used during testing and is not needed. I will remove it.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:04 AM
> To: devel@edk2.groups.io; Kubacki, Michael A
> <michael.a.kubacki@intel.com>
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> auth status in VariableParsing
> 
> Inline comments below:
> 
> 
> > -----Original Message-----
> > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> > Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> > auth status in VariableParsing
> >
> > The file VariableParsing.c provides generic functionality related to
> > parsing variable related structures and information. In order to
> > calculate offsets for certain operations, the functions must know if
> > authenticated variables are enabled as this increases the size of
> > variable headers.
> >
> > This change removes linking against a global variable in an external
> > file in favor of a statically scoped variable in VariableParsing.c
> > Because this file is unaware of how the authenticated variable status
> > is determined, the variable is set through a function interface
> > invoked during variable driver initialization.
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 14
> > +++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10 +++---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 33
> > ++++++++++++++++----
> >  3 files changed, 45 insertions(+), 12 deletions(-)
> >
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > index 6f2000f3ee..3eba590634 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > @@ -308,4 +308,18 @@ UpdateVariableInfo (
> >    IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> >    );
> >
> > +/**
> > +  Initializes context needed for variable parsing functions.
> > +
> > +  @param[in]       AuthFormat          If true then indicates authenticated
> > variables are supported
> > +
> > +  @retval          EFI_SUCCESS         Initialized successfully
> > +  @retval          Others              An error occurred during initialization
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +InitVariableParsing (
> 
> 
> InitVariableParsing() seems an internal function, the 'EFIAPI' keyword can be
> dropped. Please help to update the function definition in .C file as well.
> 
> 
> > +  IN  BOOLEAN   AuthFormat
> > +  );
> > +
> >  #endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 1a57d7e1ba..53d797152c 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > (PcdMaxVariableSize);
> >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) :
> > mVariableModuleGlobal->MaxVariableSize);
> >
> > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat);
> > +  ASSERT_EFI_ERROR (Status);
> > +
> 
> 
> After the above initialization, mVariableModuleGlobal-
> >VariableGlobal.AuthFormat
> will be changed temporarily within
> ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable store
> will be converted to the auth format:
> 
> VOID *
> ConvertNormalVarStorageToAuthVarStorage (
>   VARIABLE_STORE_HEADER *NormalVarStorage
>   )
> {
>   ...
>   //
>   // Set AuthFormat as FALSE for normal variable storage
>   //
>   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
>   ...
>   //
>   // Restore AuthFormat
>   //
>   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
>   return AuthVarStorage;
> }
> 
> 
> I think there will be issues in such converting, since I found that at least
> GetVariableHeaderSize() and NameSizeOfVariable() get called during the
> execution of ConvertNormalVarStorageToAuthVarStorage(). And they are
> checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> >VariableGlobal.AuthFormat'.
> 
> 
> >    //
> >    // Parse non-volatile variable data and get last variable offset.
> >    //
> > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> >
> >    //
> >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > -  // has been initialized in InitNonVolatileVariableStore().
> > +  // is initialized in InitNonVolatileVariableStore().
> >    //
> >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable
> > format!\n"));
> > -    //
> > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > initialize it.
> > -    //
> > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> >    } else {
> >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > variable support!\n"));
> > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> 
> 
> Not sure why the above changes belong to this patch.
> Could you help to double confirm?
> 
> Best Regards,
> Hao Wu
> 
> 
> >      VariableGuid = &gEfiVariableGuid;
> >    }
> >
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > index 394195342d..0a47f6d10d 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  #include "VariableParsing.h"
> >
> > +STATIC  BOOLEAN   mAuthFormat;
> > +
> >  /**
> >
> >    This code checks if variable header is valid or not.
> > @@ -88,7 +90,7 @@ GetVariableHeaderSize (  {
> >    UINTN Value;
> >
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> >    } else {
> >      Value = sizeof (VARIABLE_HEADER); @@ -114,7 +116,7 @@
> > NameSizeOfVariable (
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> >
> >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      if (AuthVariable->State == (UINT8) (-1) ||
> >         AuthVariable->DataSize == (UINT32) (-1) ||
> >         AuthVariable->NameSize == (UINT32) (-1) || @@ -149,7 +151,7 @@
> > SetNameSizeOfVariable (
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> >
> >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      AuthVariable->NameSize = (UINT32) NameSize;
> >    } else {
> >      Variable->NameSize = (UINT32) NameSize; @@ -173,7 +175,7 @@
> > DataSizeOfVariable (
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> >
> >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      if (AuthVariable->State == (UINT8) (-1) ||
> >         AuthVariable->DataSize == (UINT32) (-1) ||
> >         AuthVariable->NameSize == (UINT32) (-1) || @@ -208,7 +210,7 @@
> > SetDataSizeOfVariable (
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> >
> >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      AuthVariable->DataSize = (UINT32) DataSize;
> >    } else {
> >      Variable->DataSize = (UINT32) DataSize; @@ -248,7 +250,7 @@
> > GetVendorGuidPtr (
> >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> >
> >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +  if (mAuthFormat) {
> >      return &AuthVariable->VendorGuid;
> >    } else {
> >      return &Variable->VendorGuid;
> > @@ -746,3 +748,22 @@ UpdateVariableInfo (
> >      }
> >    }
> >  }
> > +
> > +/**
> > +  Initializes context needed for variable parsing functions.
> > +
> > +  @param[in]       AuthFormat          If true then indicates authenticated
> > variables are supported
> > +
> > +  @retval          EFI_SUCCESS         Initialized successfully
> > +  @retval          Others              An error occurred during initialization
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +InitVariableParsing (
> > +  IN  BOOLEAN   AuthFormat
> > +  )
> > +{
> > +  mAuthFormat = AuthFormat;
> > +
> > +  return EFI_SUCCESS;
> > +}
> > --
> > 2.16.2.windows.1
> >
> >
> > 
> 


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

* Re: [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions
  2019-10-03  8:04   ` Wu, Hao A
@ 2019-10-03 18:43     ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 18:43 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

I was debating on keeping this file in the patch series. I don't see a problem moving
those other functions. The goal was to break out some of the NV-specific content
from many of the other more generic functions in Variable.c Since you mentioned it,
I will make that change in V3, unless I hear back otherwise.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:04 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV
> variable functions
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV
> > variable functions
> >
> > This change adds a dedicated file for variable operations specific to
> > non-volatile variables. This decreases the overall length of the
> > relatively large Variable.c file.
> 
> 
> It is not clear to me what are the criteria for moving functions into the
> separate new file.
> 
> I guess the new file is for functions related with NV variables, but I saw there
> are functions like:
> 
> InitRealNonVolatileVariableStore
> InitEmuNonVolatileVariableStore
> InitNonVolatileVariableStore
> 
> Not sure if they can be put into the new file as well.
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > |  2 ++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |
> 2
> > ++
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > |  2 ++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> |
> > 25 +++++++++++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 20 +--
> --
> > ---------
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> |
> > 28 ++++++++++++++++++++
> >  6 files changed, 60 insertions(+), 19 deletions(-)
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > index c35e5fe787..08a5490787 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > @@ -36,6 +36,8 @@
> >    Variable.c
> >    VariableDxe.c
> >    Variable.h
> > +  VariableNonVolatile.c
> > +  VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> >    PrivilegePolymorphic.h
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > index 626738b9c7..6dc2721b81 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableTraditionalMm.c
> >    VariableSmm.c
> > +  VariableNonVolatile.c
> > +  VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> >    VarCheck.c
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > index 1ba8f9ebfb..ca9d23ce9f 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableSmm.c
> >    VariableStandaloneMm.c
> > +  VariableNonVolatile.c
> > +  VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> >    VarCheck.c
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> > new file mode 100644
> > index 0000000000..82572262ef
> > --- /dev/null
> > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h
> > @@ -0,0 +1,25 @@
> > +/** @file
> > +  Common variable non-volatile store routines.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#ifndef _VARIABLE_NON_VOLATILE_H_
> > +#define _VARIABLE_NON_VOLATILE_H_
> > +
> > +#include "Variable.h"
> > +
> > +/**
> > +  Get non-volatile maximum variable size.
> > +
> > +  @return Non-volatile maximum variable size.
> > +
> > +**/
> > +UINTN
> > +GetNonVolatileMaxVariableSize (
> > +  VOID
> > +  );
> > +
> > +#endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 53d797152c..5da2354aa5 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent  **/
> >
> >  #include "Variable.h"
> > +#include "VariableNonVolatile.h"
> >  #include "VariableParsing.h"
> >
> >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal; @@ -3006,25
> +3007,6
> > @@ ReclaimForOS(
> >    }
> >  }
> >
> > -/**
> > -  Get non-volatile maximum variable size.
> > -
> > -  @return Non-volatile maximum variable size.
> > -
> > -**/
> > -UINTN
> > -GetNonVolatileMaxVariableSize (
> > -  VOID
> > -  )
> > -{
> > -  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
> > -    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> > (PcdMaxAuthVariableSize)),
> > -                PcdGet32 (PcdMaxHardwareErrorVariableSize));
> > -  } else {
> > -    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> > (PcdMaxAuthVariableSize));
> > -  }
> > -}
> > -
> >  /**
> >    Get maximum variable size, covering both non-volatile and volatile
> variables.
> >
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> > new file mode 100644
> > index 0000000000..b1b6d8282f
> > --- /dev/null
> > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c
> > @@ -0,0 +1,28 @@
> > +/** @file
> > +  Common variable non-volatile store routines.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "VariableNonVolatile.h"
> > +
> > +/**
> > +  Get non-volatile maximum variable size.
> > +
> > +  @return Non-volatile maximum variable size.
> > +
> > +**/
> > +UINTN
> > +GetNonVolatileMaxVariableSize (
> > +  VOID
> > +  )
> > +{
> > +  if (PcdGet32 (PcdHwErrStorageSize) != 0) {
> > +    return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> > (PcdMaxAuthVariableSize)),
> > +                PcdGet32 (PcdMaxHardwareErrorVariableSize));
> > +  } else {
> > +    return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32
> > (PcdMaxAuthVariableSize));
> > +  }
> > +}
> > --
> > 2.16.2.windows.1
> 


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

* Re: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  2019-10-03  8:04   ` Wu, Hao A
@ 2019-10-03 18:52     ` Kubacki, Michael A
  2019-10-03 18:59       ` [edk2-devel] " Andrew Fish
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 18:52 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> > -Done:
> > +  mVariableRuntimeCacheReadLock = FALSE;
> 
> 
> Similar to the previous patch (7/9),
> if timeout occurs when acquiring the read lock, should this flag be set to
> FALSE in such case?
>

Given that the runtime service can be invoked in a multi-threaded OS environment,
it is possible that one thread could be stuck with the lock while another thread times
out waiting to acquire the lock. In that case, I believe the global lock should not be
released. I can move setting the flag to FALSE within the same conditional block in
which it is set.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:05 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
> GetNextVariableName() cache support
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
> > GetNextVariableName() cache support
> >
> > https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> >
> > This change implements the Runtime Service GetNextVariableName()
> using
> > the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service calls to
> > GetNextVariableName() will no longer trigger a SW SMI.
> >
> > Overall system performance and stability will be improved by
> > eliminating an SMI for these calls as they typically result in a
> > relatively large number of invocations to retrieve all variable names
> > in all variable stores present.
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe
> > .c | 118 +++++++++-----------
> >  1 file changed, 50 insertions(+), 68 deletions(-)
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> > xe.c
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> > xe.c
> > index 46f69765a4..bc3b56b0ce 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> > xe.c
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
> > xe.c
> > @@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName (
> >    IN OUT  EFI_GUID                          *VendorGuid
> >    )
> >  {
> > -  EFI_STATUS                                      Status;
> > -  UINTN                                           PayloadSize;
> > -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > *SmmGetNextVariableName;
> > -  UINTN                                           OutVariableNameSize;
> > -  UINTN                                           InVariableNameSize;
> > +  EFI_STATUS              Status;
> > +  UINTN                   DelayIndex;
> > +  UINTN                   MaxLen;
> > +  UINTN                   VarNameSize;
> > +  VARIABLE_HEADER         *VariablePtr;
> > +  VARIABLE_STORE_HEADER
> > *VariableStoreHeader[VariableStoreTypeMax];
> > +
> > +  Status = EFI_NOT_FOUND;
> >
> >    if (VariableNameSize == NULL || VariableName == NULL || VendorGuid
> > ==
> > NULL) {
> >      return EFI_INVALID_PARAMETER;
> >    }
> >
> > -  OutVariableNameSize   = *VariableNameSize;
> > -  InVariableNameSize    = StrSize (VariableName);
> > -  SmmGetNextVariableName = NULL;
> > -
> >    //
> > -  // If input string exceeds SMM payload limit. Return failure
> > +  // Calculate the possible maximum length of name string, including
> > + the
> > Null terminator.
> >    //
> > -  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
> {
> > +  MaxLen = *VariableNameSize / sizeof (CHAR16);  if ((MaxLen == 0) ||
> > + (StrnLenS (VariableName, MaxLen) == MaxLen)) {
> > +    //
> > +    // Null-terminator is not found in the first VariableNameSize
> > + bytes of the
> > input VariableName buffer,
> > +    // follow spec to return EFI_INVALID_PARAMETER.
> > +    //
> >      return EFI_INVALID_PARAMETER;
> >    }
> >
> > -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> >
> > -  //
> > -  // Init the communicate buffer. The buffer data size is:
> > -  // SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> > -  //
> > -  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
> {
> > -    //
> > -    // If output buffer exceed SMM payload limit. Trim output buffer to
> SMM
> > payload size
> > -    //
> > -    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
> > +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
> > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> > +    MicroSecondDelay (10);
> >    }
> > -  //
> > -  // Payload should be Guid + NameSize + MAX of Input & Output buffer
> > -  //
> > -  PayloadSize = OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)
> + MAX
> > (OutVariableNameSize, InVariableNameSize);
> > -
> > -  Status = InitCommunicateBuffer ((VOID
> **)&SmmGetNextVariableName,
> > PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
> > -  if (EFI_ERROR (Status)) {
> > -    goto Done;
> > -  }
> > -  ASSERT (SmmGetNextVariableName != NULL);
> > -
> > -  //
> > -  // SMM comm buffer->NameSize is buffer size for return string
> > -  //
> > -  SmmGetNextVariableName->NameSize = OutVariableNameSize;
> > -
> > -  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
> > -  //
> > -  // Copy whole string
> > -  //
> > -  CopyMem (SmmGetNextVariableName->Name, VariableName,
> > InVariableNameSize);
> > -  if (OutVariableNameSize > InVariableNameSize) {
> > -    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name +
> > InVariableNameSize, OutVariableNameSize - InVariableNameSize);
> > -  }
> > -
> > -  //
> > -  // Send data to SMM
> > -  //
> > -  Status = SendCommunicateBuffer (PayloadSize);
> > -
> > -  //
> > -  // Get data from SMM.
> > -  //
> > -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
> > -    //
> > -    // SMM CommBuffer NameSize can be a trimed value
> > -    // Only update VariableNameSize when needed
> > -    //
> > -    *VariableNameSize = SmmGetNextVariableName->NameSize;
> > -  }
> > -  if (EFI_ERROR (Status)) {
> > -    goto Done;
> > +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> > +    ASSERT (!mVariableRuntimeCacheReadLock);
> > +
> > +    CheckForRuntimeCacheSync ();
> > +    mVariableRuntimeCacheReadLock = TRUE;
> > +
> > +    if (!mVariableRuntimeCachePendingUpdate) {
> > +      //
> > +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > +      // The index and attributes mapping must be kept in this order
> > + as
> > FindVariable
> > +      // makes use of this mapping to implement search algorithm.
> > +      //
> > +      VariableStoreHeader[VariableStoreTypeVolatile] =
> > mVariableRuntimeVolatileCacheBuffer;
> > +      VariableStoreHeader[VariableStoreTypeHob]      =
> > mVariableRuntimeHobCacheBuffer;
> > +      VariableStoreHeader[VariableStoreTypeNv]       =
> > mVariableRuntimeNvCacheBuffer;
> > +
> > +      Status = GetNextVariableEx (VariableName, VendorGuid,
> > VariableStoreHeader, &VariablePtr);
> > +      if (!EFI_ERROR (Status)) {
> > +        VarNameSize = NameSizeOfVariable (VariablePtr);
> > +        ASSERT (VarNameSize != 0);
> > +        if (VarNameSize <= *VariableNameSize) {
> > +          CopyMem (VariableName, GetVariableNamePtr (VariablePtr),
> > VarNameSize);
> > +          CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof
> > (EFI_GUID));
> > +          Status = EFI_SUCCESS;
> > +        } else {
> > +          Status = EFI_BUFFER_TOO_SMALL;
> > +        }
> > +
> > +        *VariableNameSize = VarNameSize;
> > +      }
> > +    }
> >    }
> > -
> > -  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
> > -  CopyMem (VariableName, SmmGetNextVariableName->Name,
> > SmmGetNextVariableName->NameSize);
> > -
> > -Done:
> > +  mVariableRuntimeCacheReadLock = FALSE;
> 
> 
> Similar to the previous patch (7/9),
> if timeout occurs when acquiring the read lock, should this flag be set to
> FALSE in such case?
> 
> Best Regards,
> Hao Wu
> 
> 
> >    ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> >    return Status;
> >  }
> > --
> > 2.16.2.windows.1
> 


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

* Re: [edk2-devel] [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  2019-10-03 18:52     ` Kubacki, Michael A
@ 2019-10-03 18:59       ` Andrew Fish
  2019-10-03 20:12         ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Andrew Fish @ 2019-10-03 18:59 UTC (permalink / raw)
  To: devel, michael.a.kubacki
  Cc: Wu, Hao A, Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek,
	Gao, Liming, Mike Kinney, Ni, Ray, Wang, Jian J, Yao, Jiewen

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



> On Oct 3, 2019, at 1:52 PM, Kubacki, Michael A <michael.a.kubacki@intel.com> wrote:
> 
>>> -Done:
>>> +  mVariableRuntimeCacheReadLock = FALSE;
>> 
>> 
>> Similar to the previous patch (7/9),
>> if timeout occurs when acquiring the read lock, should this flag be set to
>> FALSE in such case?
>> 
> 
> Given that the runtime service can be invoked in a multi-threaded OS environment,
> it is possible that one thread could be stuck with the lock while another thread times
> out waiting to acquire the lock. In that case, I believe the global lock should not be
> released. I can move setting the flag to FALSE within the same conditional block in
> which it is set.
> 

UEFI Spec sets the rules is 8.1 ...

All callers of Runtime Services are restricted from calling the same or certain other Runtime Service functions prior to the completion and return of a previous Runtime Service call. These restrictions apply to:
• Runtime Services that have been interrupted
• Runtime Services that are active on another processor.
Callers are prohibited from using certain other services from another processor or on the same processor following an interrupt as specified in Table 35. For this table ‘Busy’ is defined as the state when a Runtime Service has been entered and has not returned to the caller.
The consequence of a caller violating these restrictions is undefined except for certain special cases described below.

Table 35 variable info:
If previous call is busy in:
GetVariable() GetNextVariableName() SetVariable() QueryVariableInfo() UpdateCapsule() QueryCapsuleCapabilities() GetNextHighMonotonicCount()

Forbidden to call:
GetVariable(), GetNextVariableName(), SetVariable(), QueryVariableInfo(), UpdateCapsule(), QueryCapsuleCapabilities(), GetNextHighMonotonicCount()

Thanks,

Andrew Fish

> Thanks,
> Michael
> 
>> -----Original Message-----
>> From: Wu, Hao A <hao.a.wu@intel.com <mailto:hao.a.wu@intel.com>>
>> Sent: Thursday, October 3, 2019 1:05 AM
>> To: Kubacki, Michael A <michael.a.kubacki@intel.com <mailto:michael.a.kubacki@intel.com>>;
>> devel@edk2.groups.io <mailto:devel@edk2.groups.io>
>> Cc: Bi, Dandan <dandan.bi@intel.com <mailto:dandan.bi@intel.com>>; Ard Biesheuvel
>> <ard.biesheuvel@linaro.org <mailto:ard.biesheuvel@linaro.org>>; Dong, Eric <eric.dong@intel.com <mailto:eric.dong@intel.com>>; Laszlo Ersek
>> <lersek@redhat.com <mailto:lersek@redhat.com>>; Gao, Liming <liming.gao@intel.com <mailto:liming.gao@intel.com>>; Kinney, Michael
>> D <michael.d.kinney@intel.com <mailto:michael.d.kinney@intel.com>>; Ni, Ray <ray.ni@intel.com <mailto:ray.ni@intel.com>>; Wang, Jian J
>> <jian.j.wang@intel.com <mailto:jian.j.wang@intel.com>>; Yao, Jiewen <jiewen.yao@intel.com <mailto:jiewen.yao@intel.com>>
>> Subject: RE: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
>> GetNextVariableName() cache support
>> 
>>> -----Original Message-----
>>> From: Kubacki, Michael A
>>> Sent: Saturday, September 28, 2019 9:47 AM
>>> To: devel@edk2.groups.io
>>> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
>>> Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
>>> Subject: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
>>> GetNextVariableName() cache support
>>> 
>>> https://bugzilla.tianocore.org/show_bug.cgi?id=2220
>>> 
>>> This change implements the Runtime Service GetNextVariableName()
>> using
>>> the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service calls to
>>> GetNextVariableName() will no longer trigger a SW SMI.
>>> 
>>> Overall system performance and stability will be improved by
>>> eliminating an SMI for these calls as they typically result in a
>>> relatively large number of invocations to retrieve all variable names
>>> in all variable stores present.
>>> 
>>> Cc: Dandan Bi <dandan.bi@intel.com>
>>> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>>> Cc: Eric Dong <eric.dong@intel.com>
>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>> Cc: Liming Gao <liming.gao@intel.com>
>>> Cc: Michael D Kinney <michael.d.kinney@intel.com>
>>> Cc: Ray Ni <ray.ni@intel.com>
>>> Cc: Jian J Wang <jian.j.wang@intel.com>
>>> Cc: Hao A Wu <hao.a.wu@intel.com>
>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>>> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
>>> ---
>>> 
>>> 
>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe
>>> .c | 118 +++++++++-----------
>>> 1 file changed, 50 insertions(+), 68 deletions(-)
>>> 
>>> diff --git
>>> 
>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
>>> xe.c
>>> 
>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
>>> xe.c
>>> index 46f69765a4..bc3b56b0ce 100644
>>> ---
>>> 
>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
>>> xe.c
>>> +++
>>> 
>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
>>> xe.c
>>> @@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName (
>>>   IN OUT  EFI_GUID                          *VendorGuid
>>>   )
>>> {
>>> -  EFI_STATUS                                      Status;
>>> -  UINTN                                           PayloadSize;
>>> -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
>>> *SmmGetNextVariableName;
>>> -  UINTN                                           OutVariableNameSize;
>>> -  UINTN                                           InVariableNameSize;
>>> +  EFI_STATUS              Status;
>>> +  UINTN                   DelayIndex;
>>> +  UINTN                   MaxLen;
>>> +  UINTN                   VarNameSize;
>>> +  VARIABLE_HEADER         *VariablePtr;
>>> +  VARIABLE_STORE_HEADER
>>> *VariableStoreHeader[VariableStoreTypeMax];
>>> +
>>> +  Status = EFI_NOT_FOUND;
>>> 
>>>   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid
>>> ==
>>> NULL) {
>>>     return EFI_INVALID_PARAMETER;
>>>   }
>>> 
>>> -  OutVariableNameSize   = *VariableNameSize;
>>> -  InVariableNameSize    = StrSize (VariableName);
>>> -  SmmGetNextVariableName = NULL;
>>> -
>>>   //
>>> -  // If input string exceeds SMM payload limit. Return failure
>>> +  // Calculate the possible maximum length of name string, including
>>> + the
>>> Null terminator.
>>>   //
>>> -  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
>>> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
>> {
>>> +  MaxLen = *VariableNameSize / sizeof (CHAR16);  if ((MaxLen == 0) ||
>>> + (StrnLenS (VariableName, MaxLen) == MaxLen)) {
>>> +    //
>>> +    // Null-terminator is not found in the first VariableNameSize
>>> + bytes of the
>>> input VariableName buffer,
>>> +    // follow spec to return EFI_INVALID_PARAMETER.
>>> +    //
>>>     return EFI_INVALID_PARAMETER;
>>>   }
>>> 
>>> -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
>>> +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
>>> 
>>> -  //
>>> -  // Init the communicate buffer. The buffer data size is:
>>> -  // SMM_COMMUNICATE_HEADER_SIZE +
>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
>>> -  //
>>> -  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
>>> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
>> {
>>> -    //
>>> -    // If output buffer exceed SMM payload limit. Trim output buffer to
>> SMM
>>> payload size
>>> -    //
>>> -    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF
>>> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
>>> +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
>>> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
>>> +    MicroSecondDelay (10);
>>>   }
>>> -  //
>>> -  // Payload should be Guid + NameSize + MAX of Input & Output buffer
>>> -  //
>>> -  PayloadSize = OFFSET_OF
>>> (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)
>> + MAX
>>> (OutVariableNameSize, InVariableNameSize);
>>> -
>>> -  Status = InitCommunicateBuffer ((VOID
>> **)&SmmGetNextVariableName,
>>> PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
>>> -  if (EFI_ERROR (Status)) {
>>> -    goto Done;
>>> -  }
>>> -  ASSERT (SmmGetNextVariableName != NULL);
>>> -
>>> -  //
>>> -  // SMM comm buffer->NameSize is buffer size for return string
>>> -  //
>>> -  SmmGetNextVariableName->NameSize = OutVariableNameSize;
>>> -
>>> -  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
>>> -  //
>>> -  // Copy whole string
>>> -  //
>>> -  CopyMem (SmmGetNextVariableName->Name, VariableName,
>>> InVariableNameSize);
>>> -  if (OutVariableNameSize > InVariableNameSize) {
>>> -    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name +
>>> InVariableNameSize, OutVariableNameSize - InVariableNameSize);
>>> -  }
>>> -
>>> -  //
>>> -  // Send data to SMM
>>> -  //
>>> -  Status = SendCommunicateBuffer (PayloadSize);
>>> -
>>> -  //
>>> -  // Get data from SMM.
>>> -  //
>>> -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
>>> -    //
>>> -    // SMM CommBuffer NameSize can be a trimed value
>>> -    // Only update VariableNameSize when needed
>>> -    //
>>> -    *VariableNameSize = SmmGetNextVariableName->NameSize;
>>> -  }
>>> -  if (EFI_ERROR (Status)) {
>>> -    goto Done;
>>> +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
>>> +    ASSERT (!mVariableRuntimeCacheReadLock);
>>> +
>>> +    CheckForRuntimeCacheSync ();
>>> +    mVariableRuntimeCacheReadLock = TRUE;
>>> +
>>> +    if (!mVariableRuntimeCachePendingUpdate) {
>>> +      //
>>> +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
>>> +      // The index and attributes mapping must be kept in this order
>>> + as
>>> FindVariable
>>> +      // makes use of this mapping to implement search algorithm.
>>> +      //
>>> +      VariableStoreHeader[VariableStoreTypeVolatile] =
>>> mVariableRuntimeVolatileCacheBuffer;
>>> +      VariableStoreHeader[VariableStoreTypeHob]      =
>>> mVariableRuntimeHobCacheBuffer;
>>> +      VariableStoreHeader[VariableStoreTypeNv]       =
>>> mVariableRuntimeNvCacheBuffer;
>>> +
>>> +      Status = GetNextVariableEx (VariableName, VendorGuid,
>>> VariableStoreHeader, &VariablePtr);
>>> +      if (!EFI_ERROR (Status)) {
>>> +        VarNameSize = NameSizeOfVariable (VariablePtr);
>>> +        ASSERT (VarNameSize != 0);
>>> +        if (VarNameSize <= *VariableNameSize) {
>>> +          CopyMem (VariableName, GetVariableNamePtr (VariablePtr),
>>> VarNameSize);
>>> +          CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof
>>> (EFI_GUID));
>>> +          Status = EFI_SUCCESS;
>>> +        } else {
>>> +          Status = EFI_BUFFER_TOO_SMALL;
>>> +        }
>>> +
>>> +        *VariableNameSize = VarNameSize;
>>> +      }
>>> +    }
>>>   }
>>> -
>>> -  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
>>> -  CopyMem (VariableName, SmmGetNextVariableName->Name,
>>> SmmGetNextVariableName->NameSize);
>>> -
>>> -Done:
>>> +  mVariableRuntimeCacheReadLock = FALSE;
>> 
>> 
>> Similar to the previous patch (7/9),
>> if timeout occurs when acquiring the read lock, should this flag be set to
>> FALSE in such case?
>> 
>> Best Regards,
>> Hao Wu
>> 
>> 
>>>   ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
>>>   return Status;
>>> }
>>> --
>>> 2.16.2.windows.1
>> 
> 
> 
> 


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

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

* Re: [edk2-devel] [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support
  2019-10-03 18:59       ` [edk2-devel] " Andrew Fish
@ 2019-10-03 20:12         ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 20:12 UTC (permalink / raw)
  To: devel@edk2.groups.io, afish@apple.com
  Cc: Wu, Hao A, Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek,
	Gao, Liming, Kinney, Michael D, Ni, Ray, Wang, Jian J,
	Yao, Jiewen

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

Thanks Andrew, I had not seen that before.

From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Andrew Fish via Groups.Io
Sent: Thursday, October 3, 2019 12:00 PM
To: devel@edk2.groups.io; Kubacki, Michael A <michael.a.kubacki@intel.com>
Cc: Wu, Hao A <hao.a.wu@intel.com>; Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
Subject: Re: [edk2-devel] [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support



On Oct 3, 2019, at 1:52 PM, Kubacki, Michael A <michael.a.kubacki@intel.com<mailto:michael.a.kubacki@intel.com>> wrote:

-Done:
+  mVariableRuntimeCacheReadLock = FALSE;


Similar to the previous patch (7/9),
if timeout occurs when acquiring the read lock, should this flag be set to
FALSE in such case?

Given that the runtime service can be invoked in a multi-threaded OS environment,
it is possible that one thread could be stuck with the lock while another thread times
out waiting to acquire the lock. In that case, I believe the global lock should not be
released. I can move setting the flag to FALSE within the same conditional block in
which it is set.

UEFI Spec sets the rules is 8.1 ...

All callers of Runtime Services are restricted from calling the same or certain other Runtime Service functions prior to the completion and return of a previous Runtime Service call. These restrictions apply to:
• Runtime Services that have been interrupted
• Runtime Services that are active on another processor.
Callers are prohibited from using certain other services from another processor or on the same processor following an interrupt as specified in Table 35. For this table ‘Busy’ is defined as the state when a Runtime Service has been entered and has not returned to the caller.
The consequence of a caller violating these restrictions is undefined except for certain special cases described below.

Table 35 variable info:
If previous call is busy in:
GetVariable() GetNextVariableName() SetVariable() QueryVariableInfo() UpdateCapsule() QueryCapsuleCapabilities() GetNextHighMonotonicCount()

Forbidden to call:
GetVariable(), GetNextVariableName(), SetVariable(), QueryVariableInfo(), UpdateCapsule(), QueryCapsuleCapabilities(), GetNextHighMonotonicCount()

Thanks,

Andrew Fish

Thanks,
Michael

-----Original Message-----
From: Wu, Hao A <hao.a.wu@intel.com<mailto:hao.a.wu@intel.com>>
Sent: Thursday, October 3, 2019 1:05 AM
To: Kubacki, Michael A <michael.a.kubacki@intel.com<mailto:michael.a.kubacki@intel.com>>;
devel@edk2.groups.io<mailto:devel@edk2.groups.io>
Cc: Bi, Dandan <dandan.bi@intel.com<mailto:dandan.bi@intel.com>>; Ard Biesheuvel
<ard.biesheuvel@linaro.org<mailto:ard.biesheuvel@linaro.org>>; Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>; Laszlo Ersek
<lersek@redhat.com<mailto:lersek@redhat.com>>; Gao, Liming <liming.gao@intel.com<mailto:liming.gao@intel.com>>; Kinney, Michael
D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Wang, Jian J
<jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Yao, Jiewen <jiewen.yao@intel.com<mailto:jiewen.yao@intel.com>>
Subject: RE: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
GetNextVariableName() cache support

-----Original Message-----
From: Kubacki, Michael A
Sent: Saturday, September 28, 2019 9:47 AM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
Subject: [PATCH V2 8/9] MdeModulePkg/Variable: Add RT
GetNextVariableName() cache support

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

This change implements the Runtime Service GetNextVariableName()
using
the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service calls to
GetNextVariableName() will no longer trigger a SW SMI.

Overall system performance and stability will be improved by
eliminating an SMI for these calls as they typically result in a
relatively large number of invocations to retrieve all variable names
in all variable stores present.

Cc: Dandan Bi <dandan.bi@intel.com<mailto:dandan.bi@intel.com>>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org<mailto:ard.biesheuvel@linaro.org>>
Cc: Eric Dong <eric.dong@intel.com<mailto:eric.dong@intel.com>>
Cc: Laszlo Ersek <lersek@redhat.com<mailto:lersek@redhat.com>>
Cc: Liming Gao <liming.gao@intel.com<mailto:liming.gao@intel.com>>
Cc: Michael D Kinney <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
Cc: Hao A Wu <hao.a.wu@intel.com<mailto:hao.a.wu@intel.com>>
Cc: Jiewen Yao <jiewen.yao@intel.com<mailto:jiewen.yao@intel.com>>
Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com<mailto:michael.a.kubacki@intel.com>>
---
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe
.c | 118 +++++++++-----------
1 file changed, 50 insertions(+), 68 deletions(-)

diff --git
a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
xe.c
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
xe.c
index 46f69765a4..bc3b56b0ce 100644
---
a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
xe.c
+++
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD
xe.c
@@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName (
  IN OUT  EFI_GUID                          *VendorGuid
  )
{
-  EFI_STATUS                                      Status;
-  UINTN                                           PayloadSize;
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*SmmGetNextVariableName;
-  UINTN                                           OutVariableNameSize;
-  UINTN                                           InVariableNameSize;
+  EFI_STATUS              Status;
+  UINTN                   DelayIndex;
+  UINTN                   MaxLen;
+  UINTN                   VarNameSize;
+  VARIABLE_HEADER         *VariablePtr;
+  VARIABLE_STORE_HEADER
*VariableStoreHeader[VariableStoreTypeMax];
+
+  Status = EFI_NOT_FOUND;

  if (VariableNameSize == NULL || VariableName == NULL || VendorGuid
==
NULL) {
    return EFI_INVALID_PARAMETER;
  }

-  OutVariableNameSize   = *VariableNameSize;
-  InVariableNameSize    = StrSize (VariableName);
-  SmmGetNextVariableName = NULL;
-
  //
-  // If input string exceeds SMM payload limit. Return failure
+  // Calculate the possible maximum length of name string, including
+ the
Null terminator.
  //
-  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
{
+  MaxLen = *VariableNameSize / sizeof (CHAR16);  if ((MaxLen == 0) ||
+ (StrnLenS (VariableName, MaxLen) == MaxLen)) {
+    //
+    // Null-terminator is not found in the first VariableNameSize
+ bytes of the
input VariableName buffer,
+    // follow spec to return EFI_INVALID_PARAMETER.
+    //
    return EFI_INVALID_PARAMETER;
  }

-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
+  AcquireLockOnlyAtBootTime (&mVariableServicesLock);

-  //
-  // Init the communicate buffer. The buffer data size is:
-  // SMM_COMMUNICATE_HEADER_SIZE +
SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
-  //
-  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name))
{
-    //
-    // If output buffer exceed SMM payload limit. Trim output buffer to
SMM
payload size
-    //
-    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF
(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);
+  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
+    MicroSecondDelay (10);
  }
-  //
-  // Payload should be Guid + NameSize + MAX of Input & Output buffer
-  //
-  PayloadSize = OFFSET_OF
(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)
+ MAX
(OutVariableNameSize, InVariableNameSize);
-
-  Status = InitCommunicateBuffer ((VOID
**)&SmmGetNextVariableName,
PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);
-  if (EFI_ERROR (Status)) {
-    goto Done;
-  }
-  ASSERT (SmmGetNextVariableName != NULL);
-
-  //
-  // SMM comm buffer->NameSize is buffer size for return string
-  //
-  SmmGetNextVariableName->NameSize = OutVariableNameSize;
-
-  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);
-  //
-  // Copy whole string
-  //
-  CopyMem (SmmGetNextVariableName->Name, VariableName,
InVariableNameSize);
-  if (OutVariableNameSize > InVariableNameSize) {
-    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name +
InVariableNameSize, OutVariableNameSize - InVariableNameSize);
-  }
-
-  //
-  // Send data to SMM
-  //
-  Status = SendCommunicateBuffer (PayloadSize);
-
-  //
-  // Get data from SMM.
-  //
-  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
-    //
-    // SMM CommBuffer NameSize can be a trimed value
-    // Only update VariableNameSize when needed
-    //
-    *VariableNameSize = SmmGetNextVariableName->NameSize;
-  }
-  if (EFI_ERROR (Status)) {
-    goto Done;
+  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
+    ASSERT (!mVariableRuntimeCacheReadLock);
+
+    CheckForRuntimeCacheSync ();
+    mVariableRuntimeCacheReadLock = TRUE;
+
+    if (!mVariableRuntimeCachePendingUpdate) {
+      //
+      // 0: Volatile, 1: HOB, 2: Non-Volatile.
+      // The index and attributes mapping must be kept in this order
+ as
FindVariable
+      // makes use of this mapping to implement search algorithm.
+      //
+      VariableStoreHeader[VariableStoreTypeVolatile] =
mVariableRuntimeVolatileCacheBuffer;
+      VariableStoreHeader[VariableStoreTypeHob]      =
mVariableRuntimeHobCacheBuffer;
+      VariableStoreHeader[VariableStoreTypeNv]       =
mVariableRuntimeNvCacheBuffer;
+
+      Status = GetNextVariableEx (VariableName, VendorGuid,
VariableStoreHeader, &VariablePtr);
+      if (!EFI_ERROR (Status)) {
+        VarNameSize = NameSizeOfVariable (VariablePtr);
+        ASSERT (VarNameSize != 0);
+        if (VarNameSize <= *VariableNameSize) {
+          CopyMem (VariableName, GetVariableNamePtr (VariablePtr),
VarNameSize);
+          CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof
(EFI_GUID));
+          Status = EFI_SUCCESS;
+        } else {
+          Status = EFI_BUFFER_TOO_SMALL;
+        }
+
+        *VariableNameSize = VarNameSize;
+      }
+    }
  }
-
-  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);
-  CopyMem (VariableName, SmmGetNextVariableName->Name,
SmmGetNextVariableName->NameSize);
-
-Done:
+  mVariableRuntimeCacheReadLock = FALSE;


Similar to the previous patch (7/9),
if timeout occurs when acquiring the read lock, should this flag be set to
FALSE in such case?

Best Regards,
Hao Wu


  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
  return Status;
}
--
2.16.2.windows.1






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

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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 11:00     ` Laszlo Ersek
@ 2019-10-03 20:53       ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 20:53 UTC (permalink / raw)
  To: Laszlo Ersek, Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen,
	afish@apple.com

I understand the concern. I'm aware code is sometimes marked as runtime compatible
though it is actually not and meeting all the criteria to work in the runtime environment
is non-trivial as you noted.

In response to series #2 patch #8, Andrew noted that the UEFI spec defines restrictions
on Runtime Services callers that prohibit re-entry into the GetVariable () and
GetNextVariableName () services. With this restriction in mind, I believe the polling can be
removed and the lock simply serve as an indicator to SMM whether a SMI that invokes
SetVariable () interrupted a runtime read.

Thanks,
Michael

> -----Original Message-----
> From: Laszlo Ersek <lersek@redhat.com>
> Sent: Thursday, October 3, 2019 4:01 AM
> To: Wu, Hao A <hao.a.wu@intel.com>; Kubacki, Michael A
> <michael.a.kubacki@intel.com>; devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;
> Ni, Ray <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; Yao,
> Jiewen <jiewen.yao@intel.com>
> Subject: Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> cache support
> 
> On 10/03/19 10:04, Wu, Hao A wrote:
> > Before any comment on the patch, since I am not experienced in the
> > Variable driver, I would like to ask for help from other reviewers to
> > look into this patch and provide feedbacks as well. Thanks in advance.
> >
> > With the above fact, some comments provided below maybe wrong. So
> > please help to kindly correct me.
> >
> >
> > Some general comments:
> > 1. I am not sure if bringing the TimerLib dependency (delay in acquiring the
> >    runtime cache read lock) to variable driver (a software driver for the most
> >    part) is a good idea.
> 
> I agree. Most TimerLib instances do not expect sharing the hardware with
> the OS.
> 
> 
> Another complication is if the hardware is accessed via MMIO (that is, not IO
> ports). MMIO accesses are subject to page tables.
> 
> Assuming that MicroSecondDelay() is invoked from the runtime DXE driver at
> OS runtime, a platform would have to expose the MMIO area of the timer
> hardware in the UEFI memory map as "runtime MMIO". (Via GCD memory
> space operations in a platform driver or in the TimerLib constructor.)
> 
> Furthermore, the constructor function of the TimerLib instance would have
> to register a VirtualAddressChange event handler, and convert the MMIO
> address.
> 
> Thanks
> Laszlo

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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03  8:04   ` Wu, Hao A
  2019-10-03 11:00     ` Laszlo Ersek
@ 2019-10-03 21:53     ` Kubacki, Michael A
  2019-10-03 22:01       ` Michael D Kinney
                         ` (2 more replies)
  1 sibling, 3 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 21:53 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

#1 - The plan is to remove the polling entirely in V3.

#2 - I'd prefer to take a definitive direction and reduce validation and maintenance
        effort but you and Laszlo both requested this so I'll add a FeaturePCD to control
        activation of the runtime cache in this patch series. Perhaps this can be removed
        in the future.

#3 - Will be done in V3. 

Other replies are inline.

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Thursday, October 3, 2019 1:05 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> cache support
> 
> Before any comment on the patch, since I am not experienced in the
> Variable
> driver, I would like to ask for help from other reviewers to look into this
> patch and provide feedbacks as well. Thanks in advance.
> 
> With the above fact, some comments provided below maybe wrong. So
> please help
> to kindly correct me.
> 
> 
> Some general comments:
> 1. I am not sure if bringing the TimerLib dependency (delay in acquiring the
>    runtime cache read lock) to variable driver (a software driver for the most
>    part) is a good idea.
> 
>    Hope other reviewers can provide some feedbacks for this. Thanks in
> advance.
> 
> 2. In my opinion, I prefer a switch can be provided for platform owners to
>    choose between using the runtime cache and going through SMM for
> GetVariable
>    (and for GetNextVariableName in the next patch as well).
> 
>    If platform owners feel uncomfortable with using the runtime cache with
>    regard to the security perspective, they can switch to the origin solution.
> 
> 3. Please help to remove the 'EFIAPI' keyword for new driver internal
> functions;
> 
> 
> Inline comments below:
> 
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> Kinney,
> > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> > cache support
> >
> > REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> >
> > This change reduces SMIs for GetVariable () by maintaining a
> > UEFI variable cache in Runtime DXE in addition to the pre-
> > existing cache in SMRAM. When the Runtime Service GetVariable()
> > is invoked, a Runtime DXE cache is used instead of triggering an
> > SMI to VariableSmm. This can improve overall system performance
> > by servicing variable read requests without rendezvousing all
> > cores into SMM.
> >
> > The following are important points regarding this change.
> >
> > 1. All of the non-volatile storage contents are loaded into the
> >    cache upon driver load. This one time load operation from storage
> >    is preferred as opposed to building the cache on demand. An on-
> >    demand cache would require a fallback SMI to load data into the
> >    cache as variables are requested.
> >
> > 2. SetVariable () requests will continue to always trigger an SMI.
> >    This occurs regardless of whether the variable is volatile or
> >    non-volatile.
> >
> > 3. Both volatile and non-volatile variables are cached in a runtime
> >    buffer. As is the case in the current EDK II variable driver, they
> >    continue to be cached in separate buffers.
> >
> > 4. The cache in Runtime DXE and SMM are intended to be exact copies
> >    of one another. All SMM variable accesses only return data from the
> >    SMM cache. The runtime caches are only updated after the variable I/O
> >    operation is successful in SMM. The runtime caches are only updated
> >    from SMM.
> >
> > 5. Synchronization mechanisms are in place to ensure the runtime cache
> >    content integrity with the SMM cache. These may result in updates to
> >    runtime cache that are the same in content but different in offset and
> >    size from updates to the SMM cache.
> >
> > When using SMM variables, two caches will now be present.
> > 1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to
> > service
> >    Runtime Services GetVariable () and GetNextVariableName () callers.
> > 2. "SMM Cache" - Maintained in VariableSmm to service SMM GetVariable
> ()
> >    and GetNextVariableName () callers.
> >    a. This cache is retained so SMM modules do not operate on data outside
> >       SMRAM.
> >
> > It is possible to view UEFI variable read and write statistics by setting
> > the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics
> > FeaturePcd
> > to TRUE and using the VariableInfo UEFI application in MdeModulePkg to
> > dump
> > variable statistics to the console. By doing so, a user can view the number
> > of GetVariable () hits from the Runtime DXE variable driver (Runtime Cache
> > hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for
> > GetVariable () will occur when SMM modules invoke GetVariable ().
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > |   2 +
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf           |
> 2
> > +
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i
> > nf |  31 +-
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > |   2 +
> >  MdeModulePkg/Include/Guid/SmmVariableCommon.h                        |  29 +-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h                |  39
> +-
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> > |  47 ++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c                |  44
> +-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > | 153 +++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c             |
> 114
> > +++-
> >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.
> > c   | 608 +++++++++++++++++---
> >  11 files changed, 966 insertions(+), 105 deletions(-)
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > index 08a5490787..ceea5d1ff9 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > @@ -40,6 +40,8 @@
> >    VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> > +  VariableRuntimeCache.c
> > +  VariableRuntimeCache.h
> 
> 
> Per my understanding, the module specified by VariableRuntimeDxe.inf
> does not
> involve SMM/SMI for variable services (like GetVariable). It looks weird to
> me
> for this INF to include the newly introduced runtime cache codes (below
> source
> header files):
> 
> VariableRuntimeCache.c
> VariableRuntimeCache.h
> 
> 

This is because Variable.c is common to the runtime DXE and SMM variable
driver and it contains the code to update variable caches. The runtime cache
synchronization function (SynchronizeRuntimeVariableCache ()) will return
if the runtime cache pointer is NULL.

> >    PrivilegePolymorphic.h
> >    Measurement.c
> >    TcgMorLockDxe.c
> > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > index 6dc2721b81..bc3033588d 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > @@ -49,6 +49,8 @@
> >    VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> > +  VariableRuntimeCache.c
> > +  VariableRuntimeCache.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.inf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.inf
> > index 14894e6f13..70837ac6e0 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.inf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.inf
> > @@ -13,7 +13,7 @@
> >  #  may not be modified without authorization. If platform fails to protect
> > these resources,
> >  #  the authentication service provided in this driver will be broken, and the
> > behavior is undefined.
> >  #
> > -# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #
> >  ##
> > @@ -39,6 +39,10 @@
> >    VariableSmmRuntimeDxe.c
> >    PrivilegePolymorphic.h
> >    Measurement.c
> > +  VariableParsing.c
> > +  VariableParsing.h
> > +  VariableRuntimeCache.c
> > +  VariableRuntimeCache.h
> >
> >  [Packages]
> >    MdePkg/MdePkg.dec
> > @@ -49,6 +53,7 @@
> >    BaseLib
> >    UefiBootServicesTableLib
> >    DebugLib
> > +  TimerLib
> >    UefiRuntimeLib
> >    DxeServicesTableLib
> >    UefiDriverEntryPoint
> > @@ -65,7 +70,29 @@
> >    gEdkiiVariableLockProtocolGuid                ## PRODUCES
> >    gEdkiiVarCheckProtocolGuid                    ## PRODUCES
> >
> > +[Pcd]
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                      ##
> > CONSUMES
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
> ##
> > CONSUMES
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize
> > ## CONSUMES
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                    ##
> > CONSUMES
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                     ##
> > CONSUMES
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize
> > ## CONSUMES
> > +
> >
> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpace
> > Size  ## CONSUMES
> 
> 
> Not sure if the above PCDs are really needed by VariableSmmRuntimeDxe.
> 
> 

I will double check and remove any not required.

> > +
> > +[FeaturePcd]
> > +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics
> ##
> > CONSUMES
> > +
> >  [Guids]
> > +  ## PRODUCES             ## GUID # Signature of Variable store header
> > +  ## CONSUMES             ## GUID # Signature of Variable store header
> > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > +  gEfiAuthenticatedVariableGuid
> > +
> > +  ## PRODUCES             ## GUID # Signature of Variable store header
> > +  ## CONSUMES             ## GUID # Signature of Variable store header
> > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > +  gEfiVariableGuid
> > +
> >    gEfiEventVirtualAddressChangeGuid             ## CONSUMES ## Event
> >    gEfiEventExitBootServicesGuid                 ## CONSUMES ## Event
> >    ## CONSUMES ## GUID # Locate protocol
> > @@ -82,6 +109,8 @@
> >    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> >    gEfiImageSecurityDatabaseGuid
> >
> > +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> > SOMETIMES_CONSUMES ## SystemTable
> > +
> >  [Depex]
> >    gEfiSmmCommunicationProtocolGuid
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > index ca9d23ce9f..95c5310c0b 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > nf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > inf
> > @@ -49,6 +49,8 @@
> >    VariableNonVolatile.h
> >    VariableParsing.c
> >    VariableParsing.h
> > +  VariableRuntimeCache.c
> > +  VariableRuntimeCache.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > index c527a59891..ceef44dfd2 100644
> > --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > @@ -1,7 +1,7 @@
> >  /** @file
> >    The file defined some common structures used for communicating
> > between SMM variable module and SMM variable wrapper module.
> >
> > -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  **/
> > @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #ifndef _SMM_VARIABLE_COMMON_H_
> >  #define _SMM_VARIABLE_COMMON_H_
> >
> > +#include <Guid/VariableFormat.h>
> >  #include <Protocol/VarCheck.h>
> >
> >  #define EFI_SMM_VARIABLE_WRITE_GUID \
> > @@ -66,6 +67,16 @@ typedef struct {
> >  #define
> > SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET  10
> >
> >  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE        11
> > +//
> > +// The payload for this function is
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > +//
> > +#define
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT
> > 12
> > +
> > +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
> 13
> > +//
> > +// The payload for this function is
> > SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > +//
> > +#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> > 14
> >
> >  ///
> >  /// Size of SMM communicate header, without including the payload.
> > @@ -120,4 +131,20 @@ typedef struct {
> >    UINTN                         VariablePayloadSize;
> >  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> >
> > +typedef struct {
> > +  BOOLEAN                 *ReadLock;
> > +  BOOLEAN                 *PendingUpdate;
> > +  BOOLEAN                 *HobFlushComplete;
> > +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> > +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> > +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> > +}
> >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > +
> > +typedef struct {
> > +  UINTN                   TotalHobStorageSize;
> > +  UINTN                   TotalNvStorageSize;
> > +  UINTN                   TotalVolatileStorageSize;
> > +  BOOLEAN                 AuthenticatedVariableUsage;
> > +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> > +
> >  #endif // _SMM_VARIABLE_COMMON_H_
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > index fb574b2e32..b9723c0250 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ///
> >  #define ISO_639_2_ENTRY_SIZE    3
> >
> > +///
> > +/// The timeout to in 10us units to wait for the
> > +/// variable runtime cache read lock to be acquired.
> > +///
> > +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> > +
> >  typedef enum {
> >    VariableStoreTypeVolatile,
> >    VariableStoreTypeHob,
> > @@ -64,6 +70,21 @@ typedef enum {
> >    VariableStoreTypeMax
> >  } VARIABLE_STORE_TYPE;
> >
> > +typedef struct {
> > +  UINT32                  PendingUpdateOffset;
> > +  UINT32                  PendingUpdateLength;
> > +  VARIABLE_STORE_HEADER   *Store;
> > +} VARIABLE_RUNTIME_CACHE;
> > +
> > +typedef struct {
> > +  BOOLEAN                 *ReadLock;
> > +  BOOLEAN                 *PendingUpdate;
> > +  BOOLEAN                 *HobFlushComplete;
> > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeVolatileCache;
> > +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> > +
> >  typedef struct {
> >    VARIABLE_HEADER *CurrPtr;
> >    //
> > @@ -79,14 +100,16 @@ typedef struct {
> >  } VARIABLE_POINTER_TRACK;
> >
> >  typedef struct {
> > -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> > -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> > -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> > -  EFI_LOCK              VariableServicesLock;
> > -  UINT32                ReentrantState;
> > -  BOOLEAN               AuthFormat;
> > -  BOOLEAN               AuthSupport;
> > -  BOOLEAN               EmuNvMode;
> > +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> > +  EFI_PHYSICAL_ADDRESS            HobVariableBackupBase;
> 
> 
> I do not see any usage of the new field "HobVariableBackupBase".
> Could you help to double confirm?
> 
> 

You are correct. I removed usage of this variable before sending the
patch series and the global variable here needs to be cleaned up.

> > +  EFI_PHYSICAL_ADDRESS            VolatileVariableBase;
> > +  EFI_PHYSICAL_ADDRESS            NonVolatileVariableBase;
> > +  VARIABLE_RUNTIME_CACHE_CONTEXT  VariableRuntimeCacheContext;
> > +  EFI_LOCK                        VariableServicesLock;
> > +  UINT32                          ReentrantState;
> > +  BOOLEAN                         AuthFormat;
> > +  BOOLEAN                         AuthSupport;
> > +  BOOLEAN                         EmuNvMode;
> >  } VARIABLE_GLOBAL;
> >
> >  typedef struct {
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> > h
> > new file mode 100644
> > index 0000000000..09b83eb215
> > --- /dev/null
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> > h
> > @@ -0,0 +1,47 @@
> > +/** @file
> > +  The common variable volatile store routines shared by the
> DXE_RUNTIME
> > variable
> > +  module and the DXE_SMM variable module.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> > +#define _VARIABLE_RUNTIME_CACHE_H_
> > +
> > +#include "Variable.h"
> > +
> > +/**
> > +  Copies any pending updates to runtime variable caches.
> > +
> > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > initialized properly.
> > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > +
> > +**/
> > +EFI_STATUS
> > +SynchronizeRuntimeVariableCacheEx (
> > +  VOID
> > +  );
> > +
> > +/**
> > +  Synchronizes the runtime variable caches with all pending updates
> outside
> > runtime.
> > +
> > +  Ensures all conditions are met to maintain coherency for runtime cache
> > updates.
> > +
> > +  @param[in] VariableRuntimeCache Variable runtime cache structure for
> > the runtime cache being synchronized.
> > +  @param[in] Offset               Offset in bytes to apply the update.
> > +  @param[in] Length               Length of data in bytes of the update.
> > +
> > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > initialized properly.
> > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > +
> > +**/
> > +EFI_STATUS
> > +SynchronizeRuntimeVariableCache (
> > +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> > +  IN  UINTN                           Offset,
> > +  IN  UINTN                           Length
> > +  );
> > +
> > +#endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index 5da2354aa5..bb2fa3fc19 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #include "Variable.h"
> >  #include "VariableNonVolatile.h"
> >  #include "VariableParsing.h"
> > +#include "VariableRuntimeCache.h"
> >
> >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> >
> > @@ -332,6 +333,12 @@ RecordVarErrorFlag (
> >        // Update the data in NV cache.
> >        //
> >        *VarErrFlag = TempFlag;
> > +      Status =  SynchronizeRuntimeVariableCache (
> > +                  &mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache,
> > +                  (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN)
> > mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
> > +                  sizeof (TempFlag)
> > +                  );
> > +      ASSERT_EFI_ERROR (Status);
> >      }
> >    }
> >  }
> > @@ -755,12 +762,24 @@ Reclaim (
> >
> >  Done:
> >    if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode) {
> > +    Status =  SynchronizeRuntimeVariableCache (
> > +                &mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e,
> > +                0,
> > +                VariableStoreHeader->Size
> > +                );
> > +    ASSERT_EFI_ERROR (Status);
> >      FreePool (ValidBuffer);
> >    } else {
> >      //
> >      // For NV variable reclaim, we use mNvVariableCache as the buffer, so
> > copy the data back.
> >      //
> > -    CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase,
> > VariableStoreHeader->Size);
> > +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase,
> > VariableStoreHeader->Size);
> > +    Status =  SynchronizeRuntimeVariableCache (
> > +                &(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache),
> > +                0,
> > +                VariableStoreHeader->Size
> > +                );
> > +    ASSERT_EFI_ERROR (Status);
> >    }
> >
> >    return Status;
> > @@ -1592,6 +1611,7 @@ UpdateVariable (
> >    VARIABLE_POINTER_TRACK              *Variable;
> >    VARIABLE_POINTER_TRACK              NvVariable;
> >    VARIABLE_STORE_HEADER               *VariableStoreHeader;
> > +  VARIABLE_RUNTIME_CACHE              *VolatileCacheInstance;
> >    UINT8                               *BufferForMerge;
> >    UINTN                               MergedBufSize;
> >    BOOLEAN                             DataReady;
> > @@ -2235,6 +2255,21 @@ UpdateVariable (
> >    }
> >
> >  Done:
> > +  if (!EFI_ERROR (Status)) {
> > +    if (Variable->Volatile) {
> > +      VolatileCacheInstance = &(mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e);
> > +    } else {
> > +      VolatileCacheInstance = &(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache);
> > +    }
> > +
> > +    Status =  SynchronizeRuntimeVariableCache (
> > +                VolatileCacheInstance,
> > +                0,
> > +                VolatileCacheInstance->Store->Size
> > +                );
> > +    ASSERT_EFI_ERROR (Status);
> > +  }
> > +
> >    return Status;
> >  }
> >
> > @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
> >          ErrorFlag = TRUE;
> >        }
> >      }
> > +    Status =  SynchronizeRuntimeVariableCache (
> > +                &mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache,
> > +                0,
> > +                mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> > tore->Size
> > +                );
> > +    ASSERT_EFI_ERROR (Status);
> >      if (ErrorFlag) {
> >        //
> >        // We still have HOB variable(s) not flushed in flash.
> > @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
> >        // All HOB variables have been flushed in flash.
> >        //
> >        DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been
> > flushed in flash.\n"));
> > +      *(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.HobFlushComplete) =
> TRUE;
> >        if (!AtRuntime ()) {
> >          FreePool ((VOID *) VariableStoreHeader);
> >        }
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > new file mode 100644
> > index 0000000000..2642d9b000
> > --- /dev/null
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > @@ -0,0 +1,153 @@
> > +/** @file
> > +  The common variable volatile store routines shared by the
> DXE_RUNTIME
> > variable
> > +  module and the DXE_SMM variable module.
> > +
> > +  Caution: This module requires additional review when modified.
> > +  This driver will have external input - variable data. They may be input in
> > SMM mode.
> > +  This external input must be validated carefully to avoid security issue like
> > +  buffer overflow, integer overflow.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "VariableParsing.h"
> > +#include "VariableRuntimeCache.h"
> > +
> > +extern VARIABLE_MODULE_GLOBAL   *mVariableModuleGlobal;
> > +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> > +
> > +/**
> > +  Copies any pending updates to runtime variable caches.
> > +
> > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > initialized properly.
> > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > +
> > +**/
> > +EFI_STATUS
> > +SynchronizeRuntimeVariableCacheEx (
> 
> 
> It is not clear to me why this function is named as the "Ex" version of function
> SynchronizeRuntimeVariableCache(). For me, this function looks more like a
> basic
> version.
> 
> I would suggest a name change for the functions provided in file
> VariableRuntimeCache.c to better reflect their usage model.
> 
> 

I'll rename it in V3.

> > +  VOID
> > +  )
> > +{
> 
> 
> I would recommend that at least a local variable should be introduced to
> reduce
> the duplications of:
> 
> "mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext"
> 
> in this function in order to make it easier to read.
> 
> 

I'll add it in V3.

> > +  if (
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.St
> > ore == NULL ||
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.Store == NULL ||
> > +    mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
> > +    ) {
> > +    return EFI_UNSUPPORTED;
> > +  }
> > +
> > +  if (*(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate)) {
> > +    if (
> > +      mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> > tore != NULL &&
> > +      mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0
> > +      ) {
> > +      CopyMem (
> > +        (VOID *) (
> > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.S
> > tore) +
> > +          mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> > endingUpdateOffset
> > +          ),
> > +        (VOID *) (
> > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase) +
> > +          mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> > endingUpdateOffset
> > +          ),
> > +        mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> > endingUpdateLength
> > +        );
> > +      mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> > endingUpdateLength = 0;
> > +      mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache.P
> > endingUpdateOffset = 0;
> > +    }
> > +
> > +    CopyMem (
> > +      (VOID *) (
> > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.St
> > ore) +
> > +        mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> > ndingUpdateOffset
> > +        ),
> > +      (VOID *) (
> > +        ((UINT8 *) (UINTN) mNvVariableCache) +
> > +        mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> > ndingUpdateOffset
> > +        ),
> > +      mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> > ndingUpdateLength
> > +      );
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> > ndingUpdateLength = 0;
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.Pe
> > ndingUpdateOffset = 0;
> > +
> > +    CopyMem (
> > +      (VOID *) (
> > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.Store) +
> > +        mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.PendingUpdateOffset
> > +      ),
> > +      (VOID *) (
> > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase) +
> > +        mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.PendingUpdateOffset
> > +        ),
> > +      mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.PendingUpdateLength
> > +      );
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.PendingUpdateLength = 0;
> > +    mVariableModuleGlobal-
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCach
> > e.PendingUpdateOffset = 0;
> > +    *(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = FALSE;
> > +  }
> > +
> > +  return EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  Synchronizes the runtime variable caches with all pending updates
> outside
> > runtime.
> > +
> > +  Ensures all conditions are met to maintain coherency for runtime cache
> > updates.
> > +
> > +  @param[in] VariableRuntimeCache Variable runtime cache structure for
> > the runtime cache being synchronized.
> > +  @param[in] Offset               Offset in bytes to apply the update.
> > +  @param[in] Length               Length of data in bytes of the update.
> > +
> > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > initialized properly.
> > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > +
> > +**/
> > +EFI_STATUS
> > +SynchronizeRuntimeVariableCache (
> > +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> > +  IN  UINTN                           Offset,
> > +  IN  UINTN                           Length
> > +  )
> > +{
> > +  if (VariableRuntimeCache == NULL) {
> > +    return EFI_INVALID_PARAMETER;
> > +  } else if (VariableRuntimeCache->Store == NULL) {
> > +      // Runtime cache is not available yet at this point,
> > +      // Return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET to let it
> > progress
> > +      return EFI_SUCCESS;
> > +  }
> > +
> > +  if (
> > +    mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
> ||
> > +    mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL
> > +    ) {
> > +    return EFI_UNSUPPORTED;
> > +  }
> > +
> > +  if (
> > +    *(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
> > +    VariableRuntimeCache->PendingUpdateLength > 0
> > +    ) {
> > +    VariableRuntimeCache->PendingUpdateLength =
> > +      (UINT32) (
> > +        MAX (
> > +          (UINTN) (VariableRuntimeCache->PendingUpdateOffset +
> > VariableRuntimeCache->PendingUpdateLength),
> > +          Offset + Length
> > +        ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset,
> Offset)
> > +      );
> > +    VariableRuntimeCache->PendingUpdateOffset =
> > +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
> >PendingUpdateOffset,
> > Offset);
> > +  } else {
> > +    VariableRuntimeCache->PendingUpdateLength = (UINT32) Length;
> > +    VariableRuntimeCache->PendingUpdateOffset = (UINT32) Offset;
> > +  }
> > +  *(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
> > +
> > +  if (*(mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
> > +    return SynchronizeRuntimeVariableCacheEx ();
> > +  }
> > +
> > +  return EFI_SUCCESS;
> > +}
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > index ce409f22a3..8d767f75ac 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #include <Guid/SmmVariableCommon.h>
> >  #include "Variable.h"
> >  #include "VariableParsing.h"
> > +#include "VariableRuntimeCache.h"
> > +
> > +extern VARIABLE_STORE_HEADER                         *mNvVariableCache;
> >
> >  BOOLEAN                                              mAtRuntime              = FALSE;
> >  UINT8                                                *mVariableBufferPayload = NULL;
> > @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
> >  EFI_STATUS
> >  EFIAPI
> >  SmmVariableHandler (
> > -  IN     EFI_HANDLE                                DispatchHandle,
> > -  IN     CONST VOID                                *RegisterContext,
> > -  IN OUT VOID                                      *CommBuffer,
> > -  IN OUT UINTN                                     *CommBufferSize
> > +  IN     EFI_HANDLE                                       DispatchHandle,
> > +  IN     CONST VOID                                       *RegisterContext,
> > +  IN OUT VOID                                             *CommBuffer,
> > +  IN OUT UINTN                                            *CommBufferSize
> >    )
> >  {
> > -  EFI_STATUS                                       Status;
> > -  SMM_VARIABLE_COMMUNICATE_HEADER
> > *SmmVariableFunctionHeader;
> > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > *SmmVariableHeader;
> > -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > *GetNextVariableName;
> > -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > *QueryVariableInfo;
> > -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > *GetPayloadSize;
> > -  VARIABLE_INFO_ENTRY                              *VariableInfo;
> > -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> *VariableToLock;
> > -  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > *CommVariableProperty;
> > -  UINTN                                            InfoSize;
> > -  UINTN                                            NameBufferSize;
> > -  UINTN                                            CommBufferPayloadSize;
> > -  UINTN                                            TempCommBufferSize;
> > +  EFI_STATUS                                              Status;
> > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > *SmmVariableFunctionHeader;
> > +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > *SmmVariableHeader;
> > +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > *GetNextVariableName;
> > +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > *QueryVariableInfo;
> > +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > *GetPayloadSize;
> > +
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > *RuntimeVariableCacheContext;
> > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > *GetRuntimeCacheInfo;
> > +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > *VariableToLock;
> > +  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > *CommVariableProperty;
> > +  VARIABLE_INFO_ENTRY                                     *VariableInfo;
> > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> > *VariableCacheContext;
> > +  VARIABLE_STORE_HEADER                                   *VariableCache;
> > +  UINTN                                                   InfoSize;
> > +  UINTN                                                   NameBufferSize;
> > +  UINTN                                                   CommBufferPayloadSize;
> > +  UINTN                                                   TempCommBufferSize;
> >
> >    //
> >    // If input is invalid, stop processing this SMI
> > @@ -789,6 +796,79 @@ SmmVariableHandler (
> >                   );
> >        CopyMem (SmmVariableFunctionHeader->Data,
> mVariableBufferPayload,
> > CommBufferPayloadSize);
> >        break;
> > +    case
> >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT:
> > +      if (CommBufferPayloadSize < sizeof
> >
> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT)
> )
> > {
> 
> 
> The above check is not correct, I think it should be:
> 
> if (CommBufferPayloadSize < sizeof
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> ) {
> 
> Please help to double confirm.
> Also, I recommend some security tests should be performed to these new
> cases in
> the variable SMI handler.
> 
> 

You're right. The wrong macro was simply copied.

> > +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM
> > communication buffer size invalid!\n"));
> > +      } else if (mEndOfDxe) {
> > +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Cannot
> > init context after end of DXE!\n"));
> > +      } else {
> > +        RuntimeVariableCacheContext =
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > *) SmmVariableFunctionHeader->Data;
> 
> 
> Not sure on this one:
> 
> Do you think it is necessary to copy the contents in the comm buffer to the
> pre-allocated SMM variable buffer payload 'mVariableBufferPayload' to
> avoid
> TOCTOU issue? Since there are some tests (sort of, a couple of ASSERTs)
> based
> on the comm buffer content.
> 
> 

I understand the TOCTOU observation. But is this still a concern with all the
cores rendezvoused in SMM prior to end of DXE?

> > +        VariableCacheContext = &mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext;
> > +
> > +        ASSERT (RuntimeVariableCacheContext->RuntimeVolatileCache !=
> > NULL);
> > +        ASSERT (RuntimeVariableCacheContext->RuntimeNvCache != NULL);
> > +        ASSERT (RuntimeVariableCacheContext->PendingUpdate != NULL);
> > +        ASSERT (RuntimeVariableCacheContext->ReadLock != NULL);
> > +        ASSERT (RuntimeVariableCacheContext->HobFlushComplete !=
> NULL);
> > +
> > +        VariableCacheContext->VariableRuntimeHobCache.Store      =
> > RuntimeVariableCacheContext->RuntimeHobCache;
> > +        VariableCacheContext->VariableRuntimeVolatileCache.Store =
> > RuntimeVariableCacheContext->RuntimeVolatileCache;
> > +        VariableCacheContext->VariableRuntimeNvCache.Store       =
> > RuntimeVariableCacheContext->RuntimeNvCache;
> > +        VariableCacheContext->PendingUpdate                      =
> > RuntimeVariableCacheContext->PendingUpdate;
> > +        VariableCacheContext->ReadLock                           =
> > RuntimeVariableCacheContext->ReadLock;
> > +        VariableCacheContext->HobFlushComplete                   =
> > RuntimeVariableCacheContext->HobFlushComplete;
> > +
> > +        // Set up the intial pending request since the RT cache needs to be in
> > sync with SMM cache
> > +        if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {
> > +          VariableCacheContext-
> > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > +          VariableCacheContext-
> > >VariableRuntimeHobCache.PendingUpdateLength = 0;
> > +        } else {
> > +          VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> > mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > +          VariableCacheContext-
> > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > +          VariableCacheContext-
> > >VariableRuntimeHobCache.PendingUpdateLength = (UINT32) ((UINTN)
> > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > +          CopyGuid (&(VariableCacheContext-
> > >VariableRuntimeHobCache.Store->Signature), &(VariableCache-
> > >Signature));
> > +        }
> > +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> > +        VariableCacheContext-
> > >VariableRuntimeVolatileCache.PendingUpdateOffset   = 0;
> > +        VariableCacheContext-
> > >VariableRuntimeVolatileCache.PendingUpdateLength   = (UINT32)
> ((UINTN)
> > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > +        CopyGuid (&(VariableCacheContext-
> > >VariableRuntimeVolatileCache.Store->Signature), &(VariableCache-
> > >Signature));
> > +
> > +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > mNvVariableCache;
> > +        VariableCacheContext-
> > >VariableRuntimeNvCache.PendingUpdateOffset = 0;
> > +        VariableCacheContext-
> > >VariableRuntimeNvCache.PendingUpdateLength = (UINT32) ((UINTN)
> > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > +        CopyGuid (&(VariableCacheContext-
> >VariableRuntimeNvCache.Store-
> > >Signature), &(VariableCache->Signature));
> > +
> > +        *(VariableCacheContext->PendingUpdate) = TRUE;
> > +        *(VariableCacheContext->ReadLock) = FALSE;
> > +        *(VariableCacheContext->HobFlushComplete) = FALSE;
> > +      }
> > +      Status = EFI_SUCCESS;
> > +      break;
> > +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> > +      Status = SynchronizeRuntimeVariableCacheEx ();
> > +      break;
> > +    case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> > +      if (CommBufferPayloadSize < sizeof
> > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> > +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM
> communication
> > buffer size invalid!\n"));
> > +        return EFI_SUCCESS;
> > +      }
> > +      GetRuntimeCacheInfo =
> > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > SmmVariableFunctionHeader->Data;
> > +
> > +      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) {
> > +        VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> > mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > +        GetRuntimeCacheInfo->TotalHobStorageSize = VariableCache->Size;
> > +      } else {
> > +        GetRuntimeCacheInfo->TotalHobStorageSize = 0;
> > +      }
> > +
> > +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> > +      GetRuntimeCacheInfo->TotalVolatileStorageSize = VariableCache-
> >Size;
> > +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > mNvVariableCache;
> > +      GetRuntimeCacheInfo->TotalNvStorageSize = (UINTN) VariableCache-
> > >Size;
> > +      GetRuntimeCacheInfo->AuthenticatedVariableUsage =
> > mVariableModuleGlobal->VariableGlobal.AuthFormat;
> > +
> > +      Status = EFI_SUCCESS;
> > +      break;
> >
> >      default:
> >        Status = EFI_UNSUPPORTED;
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.c
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.c
> > index 0a1888e5ef..46f69765a4 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.c
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > e.c
> > @@ -13,7 +13,7 @@
> >
> >    InitCommunicateBuffer() is really function to check the variable data size.
> >
> > -Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  **/
> > @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #include <Library/UefiRuntimeLib.h>
> >  #include <Library/BaseMemoryLib.h>
> >  #include <Library/DebugLib.h>
> > +#include <Library/TimerLib.h>
> >  #include <Library/UefiLib.h>
> >  #include <Library/BaseLib.h>
> >
> >  #include <Guid/EventGroup.h>
> > +#include <Guid/PiSmmCommunicationRegionTable.h>
> >  #include <Guid/SmmVariableCommon.h>
> >
> >  #include "PrivilegePolymorphic.h"
> > +#include "VariableParsing.h"
> >
> >  EFI_HANDLE                       mHandle                    = NULL;
> >  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable               = NULL;
> > @@ -46,8 +49,19 @@ EFI_EVENT                        mVirtualAddressChangeEvent
> =
> > NULL;
> >  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication          =
> > NULL;
> >  UINT8                           *mVariableBuffer            = NULL;
> >  UINT8                           *mVariableBufferPhysical    = NULL;
> > +VARIABLE_INFO_ENTRY             *mVariableInfo              = NULL;
> > +VARIABLE_STORE_HEADER           *mVariableRuntimeHobCacheBuffer
> =
> > NULL;
> > +VARIABLE_STORE_HEADER           *mVariableRuntimeNvCacheBuffer
> =
> > NULL;
> > +VARIABLE_STORE_HEADER           *mVariableRuntimeVolatileCacheBuffer
> > = NULL;
> >  UINTN                            mVariableBufferSize;
> > +UINTN                            mVariableRuntimeHobCacheBufferSize;
> > +UINTN                            mVariableRuntimeNvCacheBufferSize;
> > +UINTN                            mVariableRuntimeVolatileCacheBufferSize;
> >  UINTN                            mVariableBufferPayloadSize;
> > +BOOLEAN                          mVariableRuntimeCachePendingUpdate;
> > +BOOLEAN                          mVariableRuntimeCacheReadLock;
> > +BOOLEAN                          mVariableAuthFormat;
> > +BOOLEAN                          mHobFlushComplete;
> >  EFI_LOCK                         mVariableServicesLock;
> >  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
> >  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> > @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
> >    }
> >  }
> >
> > +/**
> > +  Return TRUE if ExitBootServices () has been called.
> > +
> > +  @retval TRUE If ExitBootServices () has been called.
> > +**/
> > +BOOLEAN
> > +AtRuntime (
> > +  VOID
> > +  )
> 
> 
> I think we can either:
> 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
> 2. Move AtRuntime() to VariableParsing.c so that the function can be shared
>    with VariableRuntimeDxe & VariableSmmRuntimeDxe. And then update
> the
>    EfiAtRuntime() usages to AtRuntime() for VariableSmmRuntimeDxe.
> 
> 

#1 will work fine.

> > +{
> > +  return EfiAtRuntime ();
> > +}
> > +
> > +/**
> > +  Initialize the variable cache buffer as an empty variable store.
> > +
> > +  @param[out]     VariableCacheBuffer     A pointer to pointer of a cache
> > variable store.
> > +  @param[in,out]  TotalVariableCacheSize  On input, the minimum size
> > needed for the UEFI variable store cache
> > +                                          buffer that is allocated. On output, the actual size of
> > the buffer allocated.
> > +                                          If TotalVariableCacheSize is zero, a buffer will not be
> > allocated and the
> > +                                          function will return with EFI_SUCCESS.
> > +
> > +  @retval EFI_SUCCESS             The variable cache was allocated and
> initialized
> > successfully.
> > +  @retval EFI_INVALID_PARAMETER   A given pointer is NULL or an invalid
> > variable store size was specified.
> > +  @retval EFI_OUT_OF_RESOURCES    Insufficient resources are available
> to
> > allocate the variable store cache buffer.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +InitVariableCache (
> > +  OUT    VARIABLE_STORE_HEADER   **VariableCacheBuffer,
> > +  IN OUT UINTN                   *TotalVariableCacheSize
> > +  )
> > +{
> > +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> > +
> > +  if (TotalVariableCacheSize == NULL) {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +  if (*TotalVariableCacheSize == 0) {
> > +    return EFI_SUCCESS;
> > +  }
> > +  if (VariableCacheBuffer == NULL || *TotalVariableCacheSize < sizeof
> > (VARIABLE_STORE_HEADER)) {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +  *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize,
> sizeof
> > (UINT32));
> > +
> > +  //
> > +  // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
> > +  //
> > +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> > AllocateRuntimePages (
> > +                            EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)
> > +                            );
> > +  if (*VariableCacheBuffer == NULL) {
> > +    return EFI_OUT_OF_RESOURCES;
> > +  }
> > +  VariableCacheStorePtr = *VariableCacheBuffer;
> > +  SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize,
> > (UINT32) 0xFFFFFFFF);
> > +
> > +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> > (VARIABLE_STORE_HEADER));
> > +  VariableCacheStorePtr->Size    = (UINT32) *TotalVariableCacheSize;
> > +  VariableCacheStorePtr->Format  = VARIABLE_STORE_FORMATTED;
> > +  VariableCacheStorePtr->State   = VARIABLE_STORE_HEALTHY;
> > +
> > +  return EFI_SUCCESS;
> > +}
> > +
> >  /**
> >    Initialize the communicate buffer using DataSize and Function.
> >
> > @@ -153,6 +234,69 @@ InitCommunicateBuffer (
> >  }
> >
> >
> > +/**
> > +  Gets a SMM communicate buffer from the
> > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as an entry in
> > the UEFI
> > +  system configuration table. A generic SMM communication buffer DXE
> > driver may install the table or a custom table
> > +  may be installed by a platform-specific driver.
> > +
> > +  The communicate size is:  SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> > +                            DataSize.
> > +
> > +  @param[in,out]   CommBufferSize   On input, the minimum size needed
> > for the communication buffer.
> > +                                    On output, the SMM buffer size available at
> CommBuffer.
> > +  @param[out]      CommBuffer       A pointer to an SMM communication
> > buffer pointer.
> > +
> > +  @retval EFI_SUCCESS               The communication buffer was found
> > successfully.
> > +  @retval EFI_INVALID_PARAMETER     A given pointer is NULL or the
> > CommBufferSize is zero.
> > +  @retval EFI_NOT_FOUND             The
> > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not found.
> > +  @retval EFI_OUT_OF_RESOURCES      A valid SMM communicate buffer
> for
> > the requested size is not available.
> > +
> > +**/
> > +EFI_STATUS
> > +GetCommunicateBuffer (
> > +  IN OUT  UINTN     *CommBufferSize,
> > +  OUT     UINT8     **CommBuffer
> > +  )
> 
> 
> Minor comment:
> 
> I found that the consumers of the above function are:
> GetRuntimeCacheInfo()
> SendRuntimeVariableCacheContextToSmm()
> 
> Both of them get called within SmmVariableReady() when the SMM variable
> driver
> finished initialization. I am wondering if they can simply use the pre-allocated
> comm buffer (via InitCommunicateBuffer() and using 'mVariableBuffer'),
> instead
> of looking into the configuration table.
> 
> In my opinion, this function can be dropped.
> 
> 

I did that initially. It was recommended to use this method.

> > +{
> > +  EFI_STATUS                                Status;
> > +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> > *PiSmmCommunicationRegionTable;
> > +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> > +  UINTN                                     EntrySize;
> > +  UINT32                                    Index;
> > +
> > +  if (CommBuffer == NULL || CommBufferSize == NULL ||
> > *CommBufferSize == 0) {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +
> > +  Status = EfiGetSystemConfigurationTable (
> > +             &gEdkiiPiSmmCommunicationRegionTableGuid,
> > +             (VOID **) &PiSmmCommunicationRegionTable
> > +             );
> > +  if (EFI_ERROR (Status) || PiSmmCommunicationRegionTable == NULL) {
> > +    return EFI_NOT_FOUND;
> > +  }
> > +
> > +  Entry = (EFI_MEMORY_DESCRIPTOR *)
> (PiSmmCommunicationRegionTable
> > + 1);
> > +  EntrySize = 0;
> > +  for (Index = 0; Index < PiSmmCommunicationRegionTable-
> > >NumberOfEntries; Index++) {
> > +    if (Entry->Type == EfiConventionalMemory) {
> > +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
> > +      if (EntrySize >= *CommBufferSize) {
> > +        break;
> > +      }
> > +    }
> > +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry +
> > PiSmmCommunicationRegionTable->DescriptorSize);
> > +  }
> > +
> > +  if (Index < PiSmmCommunicationRegionTable->NumberOfEntries) {
> > +    *CommBufferSize = EntrySize;
> > +    *CommBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;
> > +    return EFI_SUCCESS;
> > +  }
> > +
> > +  return EFI_OUT_OF_RESOURCES;
> > +}
> > +
> >  /**
> >    Send the data in communicate buffer to SMM.
> >
> > @@ -424,6 +568,171 @@ Done:
> >    return Status;
> >  }
> >
> > +/**
> > +  Signals SMM to synchronize any pending variable updates with the
> > runtime cache(s).
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +SyncRuntimeCache (
> > +  VOID
> > +  )
> > +{
> > +  //
> > +  // Init the communicate buffer. The buffer data size is:
> > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> > +  //
> > +  InitCommunicateBuffer (NULL, 0,
> > SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> > +
> > +  //
> > +  // Send data to SMM.
> > +  //
> > +  SendCommunicateBuffer (0);
> > +}
> > +
> > +/**
> > +  Check whether a SMI must be triggered to retrieve pending cache
> updates.
> > +
> > +  If the variable HOB was finished being flushed since the last check for a
> > runtime cache update, this function
> > +  will prevent the HOB cache from being used for future runtime cache
> hits.
> > +
> > +**/
> > +VOID
> > +EFIAPI
> > +CheckForRuntimeCacheSync (
> > +  VOID
> > +  )
> > +{
> > +  if (mVariableRuntimeCachePendingUpdate) {
> > +    SyncRuntimeCache ();
> > +  }
> > +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> > +
> > +  //
> > +  // The HOB variable data may have finished being flushed in the runtime
> > cache sync update
> > +  //
> > +  if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer != NULL)
> {
> > +    if (!AtRuntime ()) {
> > +      FreePool (mVariableRuntimeHobCacheBuffer);
> > +    }
> > +    mVariableRuntimeHobCacheBuffer = NULL;
> > +  }
> > +}
> > +
> > +/**
> > +  This code finds variable in a volatile memory store.
> > +
> > +  Caution: This function may receive untrusted input.
> > +  The data size is external input, so this function will validate it carefully to
> > avoid buffer overflow.
> > +
> > +  @param[in]      VariableName       Name of Variable to be found.
> > +  @param[in]      VendorGuid         Variable vendor GUID.
> > +  @param[out]     Attributes         Attribute value of the variable found.
> > +  @param[in, out] DataSize           Size of Data found. If size is less than the
> > +                                     data, this value contains the required size.
> > +  @param[out]     Data               Data pointer.
> > +
> > +  @retval EFI_SUCCESS                Found the specified variable.
> > +  @retval EFI_INVALID_PARAMETER      Invalid parameter.
> > +  @retval EFI_NOT_FOUND              The specified variable could not be
> found.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +FindVariableInRuntimeCache (
> > +  IN      CHAR16                            *VariableName,
> > +  IN      EFI_GUID                          *VendorGuid,
> > +  OUT     UINT32                            *Attributes OPTIONAL,
> > +  IN OUT  UINTN                             *DataSize,
> > +  OUT     VOID                              *Data OPTIONAL
> > +  )
> > +{
> > +  EFI_STATUS              Status;
> > +  UINTN                   DelayIndex;
> > +  UINTN                   TempDataSize;
> > +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> > +  VARIABLE_STORE_TYPE     StoreType;
> > +  VARIABLE_STORE_HEADER   *VariableStoreList[VariableStoreTypeMax];
> > +
> > +  Status = EFI_NOT_FOUND;
> > +
> > +  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +
> > +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex <
> > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> > +    MicroSecondDelay (10);
> > +  }
> > +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> > +    ASSERT (!mVariableRuntimeCacheReadLock);
> > +
> > +    mVariableRuntimeCacheReadLock = TRUE;
> > +    CheckForRuntimeCacheSync ();
> > +
> > +    if (!mVariableRuntimeCachePendingUpdate) {
> > +      //
> > +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > +      // The index and attributes mapping must be kept in this order as
> > FindVariable
> > +      // makes use of this mapping to implement search algorithm.
> > +      //
> > +      VariableStoreList[VariableStoreTypeVolatile] =
> > mVariableRuntimeVolatileCacheBuffer;
> > +      VariableStoreList[VariableStoreTypeHob]      =
> > mVariableRuntimeHobCacheBuffer;
> > +      VariableStoreList[VariableStoreTypeNv]       =
> > mVariableRuntimeNvCacheBuffer;
> > +
> > +      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> > VariableStoreTypeMax; StoreType++) {
> > +        if (VariableStoreList[StoreType] == NULL) {
> > +          continue;
> > +        }
> > +
> > +        RtPtrTrack.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> > +        RtPtrTrack.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> > +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType ==
> > VariableStoreTypeVolatile);
> > +
> > +        Status = FindVariableEx (VariableName, VendorGuid, FALSE,
> > &RtPtrTrack);
> > +        if (!EFI_ERROR (Status)) {
> > +          break;
> > +        }
> > +      }
> > +
> > +      if (!EFI_ERROR (Status)) {
> > +        //
> > +        // Get data size
> > +        //
> > +        TempDataSize = DataSizeOfVariable (RtPtrTrack.CurrPtr);
> > +        ASSERT (TempDataSize != 0);
> > +
> > +        if (*DataSize >= TempDataSize) {
> > +          if (Data == NULL) {
> > +            Status = EFI_INVALID_PARAMETER;
> > +            goto Done;
> > +          }
> > +
> > +          CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr),
> > TempDataSize);
> > +          if (Attributes != NULL) {
> > +            *Attributes = RtPtrTrack.CurrPtr->Attributes;
> > +          }
> > +
> > +          *DataSize = TempDataSize;
> > +
> > +          UpdateVariableInfo (VariableName, VendorGuid,
> RtPtrTrack.Volatile,
> > TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> > +
> > +          Status = EFI_SUCCESS;
> > +          goto Done;
> > +        } else {
> > +          *DataSize = TempDataSize;
> > +          Status = EFI_BUFFER_TOO_SMALL;
> > +          goto Done;
> > +        }
> > +      }
> > +    }
> > +  }
> > +
> > +Done:
> > +  mVariableRuntimeCacheReadLock = FALSE;
> 
> 
> If timeout occurs when acquiring the read lock, should this flag be set to
> FALSE
> in such case?
> 

Please see reply to patch #8.

> Best Regards,
> Hao Wu
> 
> 
> > +
> > +  return Status;
> > +}
> > +
> >  /**
> >    This code finds variable in storage blocks (Volatile or Non-Volatile).
> >
> > @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
> >    )
> >  {
> >    EFI_STATUS                                Status;
> > -  UINTN                                     PayloadSize;
> > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > *SmmVariableHeader;
> > -  UINTN                                     TempDataSize;
> > -  UINTN                                     VariableNameSize;
> >
> >    if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
> >      return EFI_INVALID_PARAMETER;
> >    }
> > -
> > -  TempDataSize          = *DataSize;
> > -  VariableNameSize      = StrSize (VariableName);
> > -  SmmVariableHeader     = NULL;
> > -
> > -  //
> > -  // If VariableName exceeds SMM payload limit. Return failure
> > -  //
> > -  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> > -    return EFI_INVALID_PARAMETER;
> > -  }
> > -
> > -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> > -
> > -  //
> > -  // Init the communicate buffer. The buffer data size is:
> > -  // SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> > -  //
> > -  if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > VariableNameSize) {
> > -    //
> > -    // If output data buffer exceed SMM payload limit. Trim output buffer to
> > SMM payload size
> > -    //
> > -    TempDataSize = mVariableBufferPayloadSize - OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > VariableNameSize;
> > -  }
> > -  PayloadSize = OFFSET_OF
> > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> > VariableNameSize + TempDataSize;
> > -
> > -  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader,
> > PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> > -  if (EFI_ERROR (Status)) {
> > -    goto Done;
> > -  }
> > -  ASSERT (SmmVariableHeader != NULL);
> > -
> > -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> > -  SmmVariableHeader->DataSize   = TempDataSize;
> > -  SmmVariableHeader->NameSize   = VariableNameSize;
> > -  if (Attributes == NULL) {
> > -    SmmVariableHeader->Attributes = 0;
> > -  } else {
> > -    SmmVariableHeader->Attributes = *Attributes;
> > -  }
> > -  CopyMem (SmmVariableHeader->Name, VariableName,
> > SmmVariableHeader->NameSize);
> > -
> > -  //
> > -  // Send data to SMM.
> > -  //
> > -  Status = SendCommunicateBuffer (PayloadSize);
> > -
> > -  //
> > -  // Get data from SMM.
> > -  //
> > -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
> > -    //
> > -    // SMM CommBuffer DataSize can be a trimed value
> > -    // Only update DataSize when needed
> > -    //
> > -    *DataSize = SmmVariableHeader->DataSize;
> > -  }
> > -  if (Attributes != NULL) {
> > -    *Attributes = SmmVariableHeader->Attributes;
> > -  }
> > -
> > -  if (EFI_ERROR (Status)) {
> > -    goto Done;
> > -  }
> > -
> > -  if (Data != NULL) {
> > -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name +
> > SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
> > -  } else {
> > -    Status = EFI_INVALID_PARAMETER;
> > +  if (VariableName[0] == 0) {
> > +    return EFI_NOT_FOUND;
> >    }
> >
> > -Done:
> > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > +  Status =  FindVariableInRuntimeCache (VariableName, VendorGuid,
> > Attributes, DataSize, Data);
> >    ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > +
> >    return Status;
> >  }
> >
> > -
> >  /**
> >    This code Finds the Next available variable.
> >
> > @@ -870,6 +1109,17 @@ OnReadyToBoot (
> >    //
> >    SendCommunicateBuffer (0);
> >
> > +  //
> > +  // Install the system configuration table for variable info data captured
> > +  //
> > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > +    if (mVariableAuthFormat) {
> > +      gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid,
> > mVariableInfo);
> > +    } else {
> > +      gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo);
> > +    }
> > +  }
> > +
> >    gBS->CloseEvent (Event);
> >  }
> >
> > @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
> >  {
> >    EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
> >    EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
> > +  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeHobCacheBuffer);
> > +  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeNvCacheBuffer);
> > +  EfiConvertPointer (0x0, (VOID **)
> > &mVariableRuntimeVolatileCacheBuffer);
> >  }
> >
> >  /**
> > @@ -969,6 +1222,173 @@ Done:
> >    return Status;
> >  }
> >
> > +/**
> > +  This code gets information needed from SMM for runtime cache
> > initialization.
> > +
> > +  @param[out] TotalHobStorageSize         Output pointer for the total HOB
> > storage size in bytes.
> > +  @param[out] TotalNvStorageSize          Output pointer for the total non-
> > volatile storage size in bytes.
> > +  @param[out] TotalVolatileStorageSize    Output pointer for the total
> > volatile storage size in bytes.
> > +  @param[out] AuthenticatedVariableUsage  Output pointer that indicates
> if
> > authenticated variables are to be used.
> > +
> > +  @retval EFI_SUCCESS                     Retrieved the size successfully.
> > +  @retval EFI_INVALID_PARAMETER           TotalNvStorageSize parameter is
> > NULL.
> > +  @retval EFI_OUT_OF_RESOURCES            Could not allocate a
> CommBuffer.
> > +  @retval Others                          Could not retrieve the size successfully.;
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +GetRuntimeCacheInfo (
> > +  OUT UINTN                         *TotalHobStorageSize,
> > +  OUT UINTN                         *TotalNvStorageSize,
> > +  OUT UINTN                         *TotalVolatileStorageSize,
> > +  OUT BOOLEAN                       *AuthenticatedVariableUsage
> > +  )
> > +{
> > +  EFI_STATUS                                          Status;
> > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > *SmmGetRuntimeCacheInfo;
> > +  EFI_SMM_COMMUNICATE_HEADER
> > *SmmCommunicateHeader;
> > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > *SmmVariableFunctionHeader;
> > +  UINTN                                               CommSize;
> > +  UINTN                                               CommBufferSize;
> > +  UINT8                                               *CommBuffer;
> > +
> > +  SmmGetRuntimeCacheInfo = NULL;
> > +  CommBuffer = NULL;
> > +
> > +  if (TotalHobStorageSize == NULL || TotalNvStorageSize == NULL ||
> > TotalVolatileStorageSize == NULL || AuthenticatedVariableUsage == NULL)
> {
> > +    return EFI_INVALID_PARAMETER;
> > +  }
> > +
> > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > +
> > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > +  CommBufferSize = CommSize;
> > +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> > +  if (EFI_ERROR (Status)) {
> > +    goto Done;
> > +  }
> > +  if (CommBuffer == NULL) {
> > +    Status = EFI_OUT_OF_RESOURCES;
> > +    goto Done;
> > +  }
> > +  ZeroMem (CommBuffer, CommBufferSize);
> > +
> > +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> > CommBuffer;
> > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > &gEfiSmmVariableProtocolGuid);
> > +  SmmCommunicateHeader->MessageLength =
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > +
> > +  SmmVariableFunctionHeader =
> > (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader-
> > >Data;
> > +  SmmVariableFunctionHeader->Function =
> > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> > +  SmmGetRuntimeCacheInfo =
> > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > SmmVariableFunctionHeader->Data;
> > +
> > +  //
> > +  // Send data to SMM.
> > +  //
> > +  Status = mSmmCommunication->Communicate (mSmmCommunication,
> > CommBuffer, &CommSize);
> > +  ASSERT_EFI_ERROR (Status);
> > +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > +    Status = EFI_BAD_BUFFER_SIZE;
> > +    goto Done;
> > +  }
> > +
> > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > +  if (EFI_ERROR (Status)) {
> > +    goto Done;
> > +  }
> > +
> > +  //
> > +  // Get data from SMM.
> > +  //
> > +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
> >TotalHobStorageSize;
> > +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo->TotalNvStorageSize;
> > +  *TotalVolatileStorageSize = SmmGetRuntimeCacheInfo-
> > >TotalVolatileStorageSize;
> > +  *AuthenticatedVariableUsage = SmmGetRuntimeCacheInfo-
> > >AuthenticatedVariableUsage;
> > +
> > +Done:
> > +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > +  return Status;
> > +}
> > +
> > +/**
> > +  Sends the runtime variable cache context information to SMM.
> > +
> > +  @retval EFI_SUCCESS               Retrieved the size successfully.
> > +  @retval EFI_INVALID_PARAMETER     TotalNvStorageSize parameter is
> > NULL.
> > +  @retval EFI_OUT_OF_RESOURCES      Could not allocate a CommBuffer.
> > +  @retval Others                    Could not retrieve the size successfully.;
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +SendRuntimeVariableCacheContextToSmm (
> > +  VOID
> > +  )
> > +{
> > +  EFI_STATUS                                                Status;
> > +
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > *SmmRuntimeVarCacheContext;
> > +  EFI_SMM_COMMUNICATE_HEADER
> > *SmmCommunicateHeader;
> > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > *SmmVariableFunctionHeader;
> > +  UINTN                                                     CommSize;
> > +  UINTN                                                     CommBufferSize;
> > +  UINT8                                                     *CommBuffer;
> > +
> > +  SmmRuntimeVarCacheContext = NULL;
> > +  CommBuffer = NULL;
> > +
> > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > +
> > +  //
> > +  // Init the communicate buffer. The buffer data size is:
> > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> ;
> > +  //
> > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> ;
> > +  CommBufferSize = CommSize;
> > +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> > +  if (EFI_ERROR (Status)) {
> > +    goto Done;
> > +  }
> > +  if (CommBuffer == NULL) {
> > +    Status = EFI_OUT_OF_RESOURCES;
> > +    goto Done;
> > +  }
> > +  ZeroMem (CommBuffer, CommBufferSize);
> > +
> > +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> > CommBuffer;
> > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > &gEfiSmmVariableProtocolGuid);
> > +  SmmCommunicateHeader->MessageLength =
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> ;
> > +
> > +  SmmVariableFunctionHeader =
> > (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader-
> > >Data;
> > +  SmmVariableFunctionHeader->Function =
> >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > +  SmmRuntimeVarCacheContext =
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > *) SmmVariableFunctionHeader->Data;
> > +
> > +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> > mVariableRuntimeHobCacheBuffer;
> > +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> > mVariableRuntimeVolatileCacheBuffer;
> > +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> > mVariableRuntimeNvCacheBuffer;
> > +  SmmRuntimeVarCacheContext->PendingUpdate =
> > &mVariableRuntimeCachePendingUpdate;
> > +  SmmRuntimeVarCacheContext->ReadLock =
> > &mVariableRuntimeCacheReadLock;
> > +  SmmRuntimeVarCacheContext->HobFlushComplete =
> > &mHobFlushComplete;
> > +
> > +  //
> > +  // Send data to SMM.
> > +  //
> > +  Status = mSmmCommunication->Communicate (mSmmCommunication,
> > CommBuffer, &CommSize);
> > +  ASSERT_EFI_ERROR (Status);
> > +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > +    Status = EFI_BAD_BUFFER_SIZE;
> > +    goto Done;
> > +  }
> > +
> > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > +  if (EFI_ERROR (Status)) {
> > +    goto Done;
> > +  }
> > +
> > +Done:
> > +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > +  return Status;
> > +}
> > +
> >  /**
> >    Initialize variable service and install Variable Architectural protocol.
> >
> > @@ -985,7 +1405,7 @@ SmmVariableReady (
> >  {
> >    EFI_STATUS                                Status;
> >
> > -  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> > (VOID **)&mSmmVariable);
> > +  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> > (VOID **) &mSmmVariable);
> >    if (EFI_ERROR (Status)) {
> >      return;
> >    }
> > @@ -1007,6 +1427,40 @@ SmmVariableReady (
> >    //
> >    mVariableBufferPhysical = mVariableBuffer;
> >
> > +  //
> > +  // Allocate runtime variable cache memory buffers.
> > +  //
> > +  Status =  GetRuntimeCacheInfo (
> > +              &mVariableRuntimeHobCacheBufferSize,
> > +              &mVariableRuntimeNvCacheBufferSize,
> > +              &mVariableRuntimeVolatileCacheBufferSize,
> > +              &mVariableAuthFormat
> > +              );
> > +  if (!EFI_ERROR (Status)) {
> > +    Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer,
> > &mVariableRuntimeHobCacheBufferSize);
> > +    if (!EFI_ERROR (Status)) {
> > +      Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer,
> > &mVariableRuntimeNvCacheBufferSize);
> > +      if (!EFI_ERROR (Status)) {
> > +        Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer,
> > &mVariableRuntimeVolatileCacheBufferSize);
> > +        if (!EFI_ERROR (Status)) {
> > +          Status = InitVariableParsing (mVariableAuthFormat);
> > +          ASSERT_EFI_ERROR (Status);
> > +
> > +          Status = SendRuntimeVariableCacheContextToSmm ();
> > +          if (!EFI_ERROR (Status)) {
> > +            SyncRuntimeCache ();
> > +          }
> > +        }
> > +      }
> > +    }
> > +    if (EFI_ERROR (Status)) {
> > +      mVariableRuntimeHobCacheBuffer = NULL;
> > +      mVariableRuntimeNvCacheBuffer = NULL;
> > +      mVariableRuntimeVolatileCacheBuffer = NULL;
> > +    }
> > +  }
> > +  ASSERT_EFI_ERROR (Status);
> > +
> >    gRT->GetVariable         = RuntimeServiceGetVariable;
> >    gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
> >    gRT->SetVariable         = RuntimeServiceSetVariable;
> > --
> > 2.16.2.windows.1
> 


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 21:53     ` Kubacki, Michael A
@ 2019-10-03 22:01       ` Michael D Kinney
  2019-10-03 23:31         ` Kubacki, Michael A
  2019-10-04  6:38       ` Laszlo Ersek
  2019-10-08  2:12       ` Wu, Hao A
  2 siblings, 1 reply; 45+ messages in thread
From: Michael D Kinney @ 2019-10-03 22:01 UTC (permalink / raw)
  To: Kubacki, Michael A, Wu, Hao A, devel@edk2.groups.io,
	Kinney, Michael D
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Ni, Ray, Wang, Jian J, Yao, Jiewen

Michael,

Perhaps the FeaturePCD for #2 should be enabled by default
so the platform DSC only needs to set this PCD for some
validation tests.

Mike


> -----Original Message-----
> From: Kubacki, Michael A <michael.a.kubacki@intel.com>
> Sent: Thursday, October 3, 2019 2:54 PM
> To: Wu, Hao A <hao.a.wu@intel.com>; devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Laszlo Ersek <lersek@redhat.com>;
> Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Ni, Ray
> <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen
> <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> RT GetVariable() cache support
> 
> #1 - The plan is to remove the polling entirely in V3.
> 
> #2 - I'd prefer to take a definitive direction and
> reduce validation and maintenance
>         effort but you and Laszlo both requested this so
> I'll add a FeaturePCD to control
>         activation of the runtime cache in this patch
> series. Perhaps this can be removed
>         in the future.
> 
> #3 - Will be done in V3.
> 
> Other replies are inline.
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Thursday, October 3, 2019 1:05 AM
> > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric
> <eric.dong@intel.com>; Laszlo Ersek
> > <lersek@redhat.com>; Gao, Liming
> <liming.gao@intel.com>; Kinney, Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray
> <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen
> <jiewen.yao@intel.com>
> > Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> RT GetVariable()
> > cache support
> >
> > Before any comment on the patch, since I am not
> experienced in the
> > Variable
> > driver, I would like to ask for help from other
> reviewers to look into this
> > patch and provide feedbacks as well. Thanks in
> advance.
> >
> > With the above fact, some comments provided below
> maybe wrong. So
> > please help
> > to kindly correct me.
> >
> >
> > Some general comments:
> > 1. I am not sure if bringing the TimerLib dependency
> (delay in acquiring the
> >    runtime cache read lock) to variable driver (a
> software driver for the most
> >    part) is a good idea.
> >
> >    Hope other reviewers can provide some feedbacks for
> this. Thanks in
> > advance.
> >
> > 2. In my opinion, I prefer a switch can be provided
> for platform owners to
> >    choose between using the runtime cache and going
> through SMM for
> > GetVariable
> >    (and for GetNextVariableName in the next patch as
> well).
> >
> >    If platform owners feel uncomfortable with using
> the runtime cache with
> >    regard to the security perspective, they can switch
> to the origin solution.
> >
> > 3. Please help to remove the 'EFIAPI' keyword for new
> driver internal
> > functions;
> >
> >
> > Inline comments below:
> >
> >
> > > -----Original Message-----
> > > From: Kubacki, Michael A
> > > Sent: Saturday, September 28, 2019 9:47 AM
> > > To: devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo
> Ersek; Gao, Liming;
> > Kinney,
> > > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> Jiewen
> > > Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> RT GetVariable()
> > > cache support
> > >
> > >
> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> > >
> > > This change reduces SMIs for GetVariable () by
> maintaining a
> > > UEFI variable cache in Runtime DXE in addition to
> the pre-
> > > existing cache in SMRAM. When the Runtime Service
> GetVariable()
> > > is invoked, a Runtime DXE cache is used instead of
> triggering an
> > > SMI to VariableSmm. This can improve overall system
> performance
> > > by servicing variable read requests without
> rendezvousing all
> > > cores into SMM.
> > >
> > > The following are important points regarding this
> change.
> > >
> > > 1. All of the non-volatile storage contents are
> loaded into the
> > >    cache upon driver load. This one time load
> operation from storage
> > >    is preferred as opposed to building the cache on
> demand. An on-
> > >    demand cache would require a fallback SMI to load
> data into the
> > >    cache as variables are requested.
> > >
> > > 2. SetVariable () requests will continue to always
> trigger an SMI.
> > >    This occurs regardless of whether the variable is
> volatile or
> > >    non-volatile.
> > >
> > > 3. Both volatile and non-volatile variables are
> cached in a runtime
> > >    buffer. As is the case in the current EDK II
> variable driver, they
> > >    continue to be cached in separate buffers.
> > >
> > > 4. The cache in Runtime DXE and SMM are intended to
> be exact copies
> > >    of one another. All SMM variable accesses only
> return data from the
> > >    SMM cache. The runtime caches are only updated
> after the variable I/O
> > >    operation is successful in SMM. The runtime
> caches are only updated
> > >    from SMM.
> > >
> > > 5. Synchronization mechanisms are in place to ensure
> the runtime cache
> > >    content integrity with the SMM cache. These may
> result in updates to
> > >    runtime cache that are the same in content but
> different in offset and
> > >    size from updates to the SMM cache.
> > >
> > > When using SMM variables, two caches will now be
> present.
> > > 1. "Runtime Cache" - Maintained in
> VariableSmmRuntimeDxe. Used to
> > > service
> > >    Runtime Services GetVariable () and
> GetNextVariableName () callers.
> > > 2. "SMM Cache" - Maintained in VariableSmm to
> service SMM GetVariable
> > ()
> > >    and GetNextVariableName () callers.
> > >    a. This cache is retained so SMM modules do not
> operate on data outside
> > >       SMRAM.
> > >
> > > It is possible to view UEFI variable read and write
> statistics by setting
> > > the
> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> ics
> > > FeaturePcd
> > > to TRUE and using the VariableInfo UEFI application
> in MdeModulePkg to
> > > dump
> > > variable statistics to the console. By doing so, a
> user can view the number
> > > of GetVariable () hits from the Runtime DXE variable
> driver (Runtime Cache
> > > hits) and the SMM variable driver (SMM Cache hits).
> SMM Cache hits for
> > > GetVariable () will occur when SMM modules invoke
> GetVariable ().
> > >
> > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > Cc: Eric Dong <eric.dong@intel.com>
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Liming Gao <liming.gao@intel.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Ray Ni <ray.ni@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Signed-off-by: Michael Kubacki
> <michael.a.kubacki@intel.com>
> > > ---
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> meDxe.inf
> > > |   2 +
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.i
> nf           |
> > 2
> > > +
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> ntimeDxe.i
> > > nf |  31 +-
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStand
> aloneMm.inf
> > > |   2 +
> > >  MdeModulePkg/Include/Guid/SmmVariableCommon.h
> |  29 +-
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> |  39
> > +-
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> meCache.h
> > > |  47 ++
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> |  44
> > +-
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> meCache.c
> > > | 153 +++++
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> |
> > 114
> > > +++-
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> ntimeDxe.
> > > c   | 608 +++++++++++++++++---
> > >  11 files changed, 966 insertions(+), 105
> deletions(-)
> > >
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeDxe.inf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeDxe.inf
> > > index 08a5490787..ceea5d1ff9 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeDxe.inf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeDxe.inf
> > > @@ -40,6 +40,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> >
> >
> > Per my understanding, the module specified by
> VariableRuntimeDxe.inf
> > does not
> > involve SMM/SMI for variable services (like
> GetVariable). It looks weird to
> > me
> > for this INF to include the newly introduced runtime
> cache codes (below
> > source
> > header files):
> >
> > VariableRuntimeCache.c
> > VariableRuntimeCache.h
> >
> >
> 
> This is because Variable.c is common to the runtime DXE
> and SMM variable
> driver and it contains the code to update variable
> caches. The runtime cache
> synchronization function
> (SynchronizeRuntimeVariableCache ()) will return
> if the runtime cache pointer is NULL.
> 
> > >    PrivilegePolymorphic.h
> > >    Measurement.c
> > >    TcgMorLockDxe.c
> > > diff --git
> > >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .inf
> > >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .inf
> > > index 6dc2721b81..bc3033588d 100644
> > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .inf
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .inf
> > > @@ -49,6 +49,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.inf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.inf
> > > index 14894e6f13..70837ac6e0 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.inf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.inf
> > > @@ -13,7 +13,7 @@
> > >  #  may not be modified without authorization. If
> platform fails to protect
> > > these resources,
> > >  #  the authentication service provided in this
> driver will be broken, and the
> > > behavior is undefined.
> > >  #
> > > -# Copyright (c) 2010 - 2017, Intel Corporation. All
> rights reserved.<BR>
> > > +# Copyright (c) 2010 - 2019, Intel Corporation. All
> rights reserved.<BR>
> > >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #
> > >  ##
> > > @@ -39,6 +39,10 @@
> > >    VariableSmmRuntimeDxe.c
> > >    PrivilegePolymorphic.h
> > >    Measurement.c
> > > +  VariableParsing.c
> > > +  VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >
> > >  [Packages]
> > >    MdePkg/MdePkg.dec
> > > @@ -49,6 +53,7 @@
> > >    BaseLib
> > >    UefiBootServicesTableLib
> > >    DebugLib
> > > +  TimerLib
> > >    UefiRuntimeLib
> > >    DxeServicesTableLib
> > >    UefiDriverEntryPoint
> > > @@ -65,7 +70,29 @@
> > >    gEdkiiVariableLockProtocolGuid                ##
> PRODUCES
> > >    gEdkiiVarCheckProtocolGuid                    ##
> PRODUCES
> > >
> > > +[Pcd]
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize
> ##
> > > CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
> > ##
> > > CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariab
> leSize
> > > ## CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize
> ##
> > > CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize
> ##
> > > CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpace
> Size
> > > ## CONSUMES
> > > +
> > >
> >
> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVari
> ableSpace
> > > Size  ## CONSUMES
> >
> >
> > Not sure if the above PCDs are really needed by
> VariableSmmRuntimeDxe.
> >
> >
> 
> I will double check and remove any not required.
> 
> > > +
> > > +[FeaturePcd]
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> ics
> > ##
> > > CONSUMES
> > > +
> > >  [Guids]
> > > +  ## PRODUCES             ## GUID # Signature of
> Variable store header
> > > +  ## CONSUMES             ## GUID # Signature of
> Variable store header
> > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > +  gEfiAuthenticatedVariableGuid
> > > +
> > > +  ## PRODUCES             ## GUID # Signature of
> Variable store header
> > > +  ## CONSUMES             ## GUID # Signature of
> Variable store header
> > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > +  gEfiVariableGuid
> > > +
> > >    gEfiEventVirtualAddressChangeGuid             ##
> CONSUMES ## Event
> > >    gEfiEventExitBootServicesGuid                 ##
> CONSUMES ## Event
> > >    ## CONSUMES ## GUID # Locate protocol
> > > @@ -82,6 +109,8 @@
> > >    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > >    gEfiImageSecurityDatabaseGuid
> > >
> > > +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> > > SOMETIMES_CONSUMES ## SystemTable
> > > +
> > >  [Depex]
> > >    gEfiSmmCommunicationProtocolGuid
> > >
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> ndaloneMm.i
> > > nf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> ndaloneMm.
> > > inf
> > > index ca9d23ce9f..95c5310c0b 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> ndaloneMm.i
> > > nf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> ndaloneMm.
> > > inf
> > > @@ -49,6 +49,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > diff --git
> a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > index c527a59891..ceef44dfd2 100644
> > > --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > @@ -1,7 +1,7 @@
> > >  /** @file
> > >    The file defined some common structures used for
> communicating
> > > between SMM variable module and SMM variable wrapper
> module.
> > >
> > > -Copyright (c) 2011 - 2015, Intel Corporation. All
> rights reserved.<BR>
> > > +Copyright (c) 2011 - 2019, Intel Corporation. All
> rights reserved.<BR>
> > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  **/
> > > @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-
> Clause-Patent
> > >  #ifndef _SMM_VARIABLE_COMMON_H_
> > >  #define _SMM_VARIABLE_COMMON_H_
> > >
> > > +#include <Guid/VariableFormat.h>
> > >  #include <Protocol/VarCheck.h>
> > >
> > >  #define EFI_SMM_VARIABLE_WRITE_GUID \
> > > @@ -66,6 +67,16 @@ typedef struct {
> > >  #define
> > >
> SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
> 10
> > >
> > >  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
> 11
> > > +//
> > > +// The payload for this function is
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > +//
> > > +#define
> > >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> T
> > > 12
> > > +
> > > +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
> > 13
> > > +//
> > > +// The payload for this function is
> > > SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > +//
> > > +#define
> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> > > 14
> > >
> > >  ///
> > >  /// Size of SMM communicate header, without
> including the payload.
> > > @@ -120,4 +131,20 @@ typedef struct {
> > >    UINTN
> VariablePayloadSize;
> > >  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> > >
> > > +typedef struct {
> > > +  BOOLEAN                 *ReadLock;
> > > +  BOOLEAN                 *PendingUpdate;
> > > +  BOOLEAN                 *HobFlushComplete;
> > > +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> > > +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> > > +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> > > +}
> > >
> >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > > +
> > > +typedef struct {
> > > +  UINTN                   TotalHobStorageSize;
> > > +  UINTN                   TotalNvStorageSize;
> > > +  UINTN                   TotalVolatileStorageSize;
> > > +  BOOLEAN
> AuthenticatedVariableUsage;
> > > +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> > > +
> > >  #endif // _SMM_VARIABLE_COMMON_H_
> > > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > index fb574b2e32..b9723c0250 100644
> > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-
> Clause-Patent
> > >  ///
> > >  #define ISO_639_2_ENTRY_SIZE    3
> > >
> > > +///
> > > +/// The timeout to in 10us units to wait for the
> > > +/// variable runtime cache read lock to be
> acquired.
> > > +///
> > > +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> > > +
> > >  typedef enum {
> > >    VariableStoreTypeVolatile,
> > >    VariableStoreTypeHob,
> > > @@ -64,6 +70,21 @@ typedef enum {
> > >    VariableStoreTypeMax
> > >  } VARIABLE_STORE_TYPE;
> > >
> > > +typedef struct {
> > > +  UINT32                  PendingUpdateOffset;
> > > +  UINT32                  PendingUpdateLength;
> > > +  VARIABLE_STORE_HEADER   *Store;
> > > +} VARIABLE_RUNTIME_CACHE;
> > > +
> > > +typedef struct {
> > > +  BOOLEAN                 *ReadLock;
> > > +  BOOLEAN                 *PendingUpdate;
> > > +  BOOLEAN                 *HobFlushComplete;
> > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> > > +  VARIABLE_RUNTIME_CACHE
> VariableRuntimeVolatileCache;
> > > +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> > > +
> > >  typedef struct {
> > >    VARIABLE_HEADER *CurrPtr;
> > >    //
> > > @@ -79,14 +100,16 @@ typedef struct {
> > >  } VARIABLE_POINTER_TRACK;
> > >
> > >  typedef struct {
> > > -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> > > -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> > > -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> > > -  EFI_LOCK              VariableServicesLock;
> > > -  UINT32                ReentrantState;
> > > -  BOOLEAN               AuthFormat;
> > > -  BOOLEAN               AuthSupport;
> > > -  BOOLEAN               EmuNvMode;
> > > +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> > > +  EFI_PHYSICAL_ADDRESS
> HobVariableBackupBase;
> >
> >
> > I do not see any usage of the new field
> "HobVariableBackupBase".
> > Could you help to double confirm?
> >
> >
> 
> You are correct. I removed usage of this variable before
> sending the
> patch series and the global variable here needs to be
> cleaned up.
> 
> > > +  EFI_PHYSICAL_ADDRESS
> VolatileVariableBase;
> > > +  EFI_PHYSICAL_ADDRESS
> NonVolatileVariableBase;
> > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> VariableRuntimeCacheContext;
> > > +  EFI_LOCK
> VariableServicesLock;
> > > +  UINT32                          ReentrantState;
> > > +  BOOLEAN                         AuthFormat;
> > > +  BOOLEAN                         AuthSupport;
> > > +  BOOLEAN                         EmuNvMode;
> > >  } VARIABLE_GLOBAL;
> > >
> > >  typedef struct {
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.h
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.
> > > h
> > > new file mode 100644
> > > index 0000000000..09b83eb215
> > > --- /dev/null
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.
> > > h
> > > @@ -0,0 +1,47 @@
> > > +/** @file
> > > +  The common variable volatile store routines
> shared by the
> > DXE_RUNTIME
> > > variable
> > > +  module and the DXE_SMM variable module.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights
> reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> > > +#define _VARIABLE_RUNTIME_CACHE_H_
> > > +
> > > +#include "Variable.h"
> > > +
> > > +/**
> > > +  Copies any pending updates to runtime variable
> caches.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile
> store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile
> store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCacheEx (
> > > +  VOID
> > > +  );
> > > +
> > > +/**
> > > +  Synchronizes the runtime variable caches with all
> pending updates
> > outside
> > > runtime.
> > > +
> > > +  Ensures all conditions are met to maintain
> coherency for runtime cache
> > > updates.
> > > +
> > > +  @param[in] VariableRuntimeCache Variable runtime
> cache structure for
> > > the runtime cache being synchronized.
> > > +  @param[in] Offset               Offset in bytes
> to apply the update.
> > > +  @param[in] Length               Length of data in
> bytes of the update.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile
> store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile
> store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCache (
> > > +  IN  VARIABLE_RUNTIME_CACHE
> *VariableRuntimeCache,
> > > +  IN  UINTN                           Offset,
> > > +  IN  UINTN                           Length
> > > +  );
> > > +
> > > +#endif
> > > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index 5da2354aa5..bb2fa3fc19 100644
> > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-
> Clause-Patent
> > >  #include "Variable.h"
> > >  #include "VariableNonVolatile.h"
> > >  #include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > >
> > >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> > >
> > > @@ -332,6 +333,12 @@ RecordVarErrorFlag (
> > >        // Update the data in NV cache.
> > >        //
> > >        *VarErrFlag = TempFlag;
> > > +      Status =  SynchronizeRuntimeVariableCache (
> > > +                  &mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache,
> > > +                  (UINTN) VarErrFlag - (UINTN)
> mNvVariableCache + (UINTN)
> > > mVariableModuleGlobal-
> >VariableGlobal.NonVolatileVariableBase,
> > > +                  sizeof (TempFlag)
> > > +                  );
> > > +      ASSERT_EFI_ERROR (Status);
> > >      }
> > >    }
> > >  }
> > > @@ -755,12 +762,24 @@ Reclaim (
> > >
> > >  Done:
> > >    if (IsVolatile || mVariableModuleGlobal-
> >VariableGlobal.EmuNvMode) {
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e,
> > > +                0,
> > > +                VariableStoreHeader->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >      FreePool (ValidBuffer);
> > >    } else {
> > >      //
> > >      // For NV variable reclaim, we use
> mNvVariableCache as the buffer, so
> > > copy the data back.
> > >      //
> > > -    CopyMem (mNvVariableCache, (UINT8
> *)(UINTN)VariableBase,
> > > VariableStoreHeader->Size);
> > > +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN)
> VariableBase,
> > > VariableStoreHeader->Size);
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache),
> > > +                0,
> > > +                VariableStoreHeader->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >    }
> > >
> > >    return Status;
> > > @@ -1592,6 +1611,7 @@ UpdateVariable (
> > >    VARIABLE_POINTER_TRACK              *Variable;
> > >    VARIABLE_POINTER_TRACK              NvVariable;
> > >    VARIABLE_STORE_HEADER
> *VariableStoreHeader;
> > > +  VARIABLE_RUNTIME_CACHE
> *VolatileCacheInstance;
> > >    UINT8
> *BufferForMerge;
> > >    UINTN
> MergedBufSize;
> > >    BOOLEAN                             DataReady;
> > > @@ -2235,6 +2255,21 @@ UpdateVariable (
> > >    }
> > >
> > >  Done:
> > > +  if (!EFI_ERROR (Status)) {
> > > +    if (Variable->Volatile) {
> > > +      VolatileCacheInstance =
> &(mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e);
> > > +    } else {
> > > +      VolatileCacheInstance =
> &(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache);
> > > +    }
> > > +
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                VolatileCacheInstance,
> > > +                0,
> > > +                VolatileCacheInstance->Store->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > > +  }
> > > +
> > >    return Status;
> > >  }
> > >
> > > @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
> > >          ErrorFlag = TRUE;
> > >        }
> > >      }
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache,
> > > +                0,
> > > +                mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.S
> > > tore->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >      if (ErrorFlag) {
> > >        //
> > >        // We still have HOB variable(s) not flushed
> in flash.
> > > @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
> > >        // All HOB variables have been flushed in
> flash.
> > >        //
> > >        DEBUG ((EFI_D_INFO, "Variable driver: all HOB
> variables have been
> > > flushed in flash.\n"));
> > > +      *(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.HobFlushComp
> lete) =
> > TRUE;
> > >        if (!AtRuntime ()) {
> > >          FreePool ((VOID *) VariableStoreHeader);
> > >        }
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.c
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.c
> > > new file mode 100644
> > > index 0000000000..2642d9b000
> > > --- /dev/null
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> timeCache.c
> > > @@ -0,0 +1,153 @@
> > > +/** @file
> > > +  The common variable volatile store routines
> shared by the
> > DXE_RUNTIME
> > > variable
> > > +  module and the DXE_SMM variable module.
> > > +
> > > +  Caution: This module requires additional review
> when modified.
> > > +  This driver will have external input - variable
> data. They may be input in
> > > SMM mode.
> > > +  This external input must be validated carefully
> to avoid security issue like
> > > +  buffer overflow, integer overflow.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights
> reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > > +
> > > +extern VARIABLE_MODULE_GLOBAL
> *mVariableModuleGlobal;
> > > +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> > > +
> > > +/**
> > > +  Copies any pending updates to runtime variable
> caches.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile
> store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile
> store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCacheEx (
> >
> >
> > It is not clear to me why this function is named as
> the "Ex" version of function
> > SynchronizeRuntimeVariableCache(). For me, this
> function looks more like a
> > basic
> > version.
> >
> > I would suggest a name change for the functions
> provided in file
> > VariableRuntimeCache.c to better reflect their usage
> model.
> >
> >
> 
> I'll rename it in V3.
> 
> > > +  VOID
> > > +  )
> > > +{
> >
> >
> > I would recommend that at least a local variable
> should be introduced to
> > reduce
> > the duplications of:
> >
> > "mVariableModuleGlobal-
> >VariableGlobal.VariableRuntimeCacheContext"
> >
> > in this function in order to make it easier to read.
> >
> >
> 
> I'll add it in V3.
> 
> > > +  if (
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.St
> > > ore == NULL ||
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.Store == NULL ||
> > > +    mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e == NULL
> > > +    ) {
> > > +    return EFI_UNSUPPORTED;
> > > +  }
> > > +
> > > +  if (*(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e)) {
> > > +    if (
> > > +      mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.S
> > > tore != NULL &&
> > > +      mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase > 0
> > > +      ) {
> > > +      CopyMem (
> > > +        (VOID *) (
> > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.S
> > > tore) +
> > > +          mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.P
> > > endingUpdateOffset
> > > +          ),
> > > +        (VOID *) (
> > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.HobVariableBase) +
> > > +          mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.P
> > > endingUpdateOffset
> > > +          ),
> > > +        mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.P
> > > endingUpdateLength
> > > +        );
> > > +      mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.P
> > > endingUpdateLength = 0;
> > > +      mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeHobCache.P
> > > endingUpdateOffset = 0;
> > > +    }
> > > +
> > > +    CopyMem (
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.St
> > > ore) +
> > > +        mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.Pe
> > > ndingUpdateOffset
> > > +        ),
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mNvVariableCache) +
> > > +        mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.Pe
> > > ndingUpdateOffset
> > > +        ),
> > > +      mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.Pe
> > > ndingUpdateLength
> > > +      );
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.Pe
> > > ndingUpdateLength = 0;
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeNvCache.Pe
> > > ndingUpdateOffset = 0;
> > > +
> > > +    CopyMem (
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.Store) +
> > > +        mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.PendingUpdateOffset
> > > +      ),
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.VolatileVariableBase) +
> > > +        mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.PendingUpdateOffset
> > > +        ),
> > > +      mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.PendingUpdateLength
> > > +      );
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.PendingUpdateLength = 0;
> > > +    mVariableModuleGlobal-
> > >
> >
> >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> imeVolatileCach
> > > e.PendingUpdateOffset = 0;
> > > +    *(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e) = FALSE;
> > > +  }
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Synchronizes the runtime variable caches with all
> pending updates
> > outside
> > > runtime.
> > > +
> > > +  Ensures all conditions are met to maintain
> coherency for runtime cache
> > > updates.
> > > +
> > > +  @param[in] VariableRuntimeCache Variable runtime
> cache structure for
> > > the runtime cache being synchronized.
> > > +  @param[in] Offset               Offset in bytes
> to apply the update.
> > > +  @param[in] Length               Length of data in
> bytes of the update.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile
> store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile
> store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCache (
> > > +  IN  VARIABLE_RUNTIME_CACHE
> *VariableRuntimeCache,
> > > +  IN  UINTN                           Offset,
> > > +  IN  UINTN                           Length
> > > +  )
> > > +{
> > > +  if (VariableRuntimeCache == NULL) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  } else if (VariableRuntimeCache->Store == NULL) {
> > > +      // Runtime cache is not available yet at this
> point,
> > > +      // Return EFI_SUCCESS instead of
> EFI_NOT_AVAILABLE_YET to let it
> > > progress
> > > +      return EFI_SUCCESS;
> > > +  }
> > > +
> > > +  if (
> > > +    mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e == NULL
> > ||
> > > +    mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.ReadLock
> == NULL
> > > +    ) {
> > > +    return EFI_UNSUPPORTED;
> > > +  }
> > > +
> > > +  if (
> > > +    *(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e) &&
> > > +    VariableRuntimeCache->PendingUpdateLength > 0
> > > +    ) {
> > > +    VariableRuntimeCache->PendingUpdateLength =
> > > +      (UINT32) (
> > > +        MAX (
> > > +          (UINTN) (VariableRuntimeCache-
> >PendingUpdateOffset +
> > > VariableRuntimeCache->PendingUpdateLength),
> > > +          Offset + Length
> > > +        ) - MIN ((UINTN) VariableRuntimeCache-
> >PendingUpdateOffset,
> > Offset)
> > > +      );
> > > +    VariableRuntimeCache->PendingUpdateOffset =
> > > +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
> > >PendingUpdateOffset,
> > > Offset);
> > > +  } else {
> > > +    VariableRuntimeCache->PendingUpdateLength =
> (UINT32) Length;
> > > +    VariableRuntimeCache->PendingUpdateOffset =
> (UINT32) Offset;
> > > +  }
> > > +  *(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> e) = TRUE;
> > > +
> > > +  if (*(mVariableModuleGlobal-
> > >
> >VariableGlobal.VariableRuntimeCacheContext.ReadLock) ==
> FALSE) {
> > > +    return SynchronizeRuntimeVariableCacheEx ();
> > > +  }
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .c
> > >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .c
> > > index ce409f22a3..8d767f75ac 100644
> > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .c
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> .c
> > > @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-
> Clause-Patent
> > >  #include <Guid/SmmVariableCommon.h>
> > >  #include "Variable.h"
> > >  #include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > > +
> > > +extern VARIABLE_STORE_HEADER
> *mNvVariableCache;
> > >
> > >  BOOLEAN
> mAtRuntime              = FALSE;
> > >  UINT8
> *mVariableBufferPayload = NULL;
> > > @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
> > >  EFI_STATUS
> > >  EFIAPI
> > >  SmmVariableHandler (
> > > -  IN     EFI_HANDLE
> DispatchHandle,
> > > -  IN     CONST VOID
> *RegisterContext,
> > > -  IN OUT VOID
> *CommBuffer,
> > > -  IN OUT UINTN
> *CommBufferSize
> > > +  IN     EFI_HANDLE
> DispatchHandle,
> > > +  IN     CONST VOID
> *RegisterContext,
> > > +  IN OUT VOID
> *CommBuffer,
> > > +  IN OUT UINTN
> *CommBufferSize
> > >    )
> > >  {
> > > -  EFI_STATUS
> Status;
> > > -  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > *GetNextVariableName;
> > > -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > *QueryVariableInfo;
> > > -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > *GetPayloadSize;
> > > -  VARIABLE_INFO_ENTRY
> *VariableInfo;
> > > -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > *VariableToLock;
> > > -
> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > *CommVariableProperty;
> > > -  UINTN
> InfoSize;
> > > -  UINTN
> NameBufferSize;
> > > -  UINTN
> CommBufferPayloadSize;
> > > -  UINTN
> TempCommBufferSize;
> > > +  EFI_STATUS
> Status;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > *GetNextVariableName;
> > > +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > *QueryVariableInfo;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > *GetPayloadSize;
> > > +
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *RuntimeVariableCacheContext;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > *GetRuntimeCacheInfo;
> > > +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > > *VariableToLock;
> > > +
> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > *CommVariableProperty;
> > > +  VARIABLE_INFO_ENTRY
> *VariableInfo;
> > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> > > *VariableCacheContext;
> > > +  VARIABLE_STORE_HEADER
> *VariableCache;
> > > +  UINTN
> InfoSize;
> > > +  UINTN
> NameBufferSize;
> > > +  UINTN
> CommBufferPayloadSize;
> > > +  UINTN
> TempCommBufferSize;
> > >
> > >    //
> > >    // If input is invalid, stop processing this SMI
> > > @@ -789,6 +796,79 @@ SmmVariableHandler (
> > >                   );
> > >        CopyMem (SmmVariableFunctionHeader->Data,
> > mVariableBufferPayload,
> > > CommBufferPayloadSize);
> > >        break;
> > > +    case
> > >
> >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> T:
> > > +      if (CommBufferPayloadSize < sizeof
> > >
> >
> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTE
> XT)
> > )
> > > {
> >
> >
> > The above check is not correct, I think it should be:
> >
> > if (CommBufferPayloadSize < sizeof
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> )
> > ) {
> >
> > Please help to double confirm.
> > Also, I recommend some security tests should be
> performed to these new
> > cases in
> > the variable SMI handler.
> >
> >
> 
> You're right. The wrong macro was simply copied.
> 
> > > +        DEBUG ((DEBUG_ERROR,
> "InitRuntimeVariableCacheContext: SMM
> > > communication buffer size invalid!\n"));
> > > +      } else if (mEndOfDxe) {
> > > +        DEBUG ((DEBUG_ERROR,
> "InitRuntimeVariableCacheContext: Cannot
> > > init context after end of DXE!\n"));
> > > +      } else {
> > > +        RuntimeVariableCacheContext =
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *) SmmVariableFunctionHeader->Data;
> >
> >
> > Not sure on this one:
> >
> > Do you think it is necessary to copy the contents in
> the comm buffer to the
> > pre-allocated SMM variable buffer payload
> 'mVariableBufferPayload' to
> > avoid
> > TOCTOU issue? Since there are some tests (sort of, a
> couple of ASSERTs)
> > based
> > on the comm buffer content.
> >
> >
> 
> I understand the TOCTOU observation. But is this still a
> concern with all the
> cores rendezvoused in SMM prior to end of DXE?
> 
> > > +        VariableCacheContext =
> &mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext;
> > > +
> > > +        ASSERT (RuntimeVariableCacheContext-
> >RuntimeVolatileCache !=
> > > NULL);
> > > +        ASSERT (RuntimeVariableCacheContext-
> >RuntimeNvCache != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext-
> >PendingUpdate != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext-
> >ReadLock != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext-
> >HobFlushComplete !=
> > NULL);
> > > +
> > > +        VariableCacheContext-
> >VariableRuntimeHobCache.Store      =
> > > RuntimeVariableCacheContext->RuntimeHobCache;
> > > +        VariableCacheContext-
> >VariableRuntimeVolatileCache.Store =
> > > RuntimeVariableCacheContext->RuntimeVolatileCache;
> > > +        VariableCacheContext-
> >VariableRuntimeNvCache.Store       =
> > > RuntimeVariableCacheContext->RuntimeNvCache;
> > > +        VariableCacheContext->PendingUpdate
> =
> > > RuntimeVariableCacheContext->PendingUpdate;
> > > +        VariableCacheContext->ReadLock
> =
> > > RuntimeVariableCacheContext->ReadLock;
> > > +        VariableCacheContext->HobFlushComplete
> =
> > > RuntimeVariableCacheContext->HobFlushComplete;
> > > +
> > > +        // Set up the intial pending request since
> the RT cache needs to be in
> > > sync with SMM cache
> > > +        if (mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase == 0) {
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateLength = 0;
> > > +        } else {
> > > +          VariableCache = (VARIABLE_STORE_HEADER *)
> (UINTN)
> > > mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateLength =
> (UINT32) ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN)
> VariableCache);
> > > +          CopyGuid (&(VariableCacheContext-
> > > >VariableRuntimeHobCache.Store->Signature),
> &(VariableCache-
> > > >Signature));
> > > +        }
> > > +        VariableCache = (VARIABLE_STORE_HEADER  *)
> (UINTN)
> > > mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> > > +        VariableCacheContext-
> > > >VariableRuntimeVolatileCache.PendingUpdateOffset
> = 0;
> > > +        VariableCacheContext-
> > > >VariableRuntimeVolatileCache.PendingUpdateLength
> = (UINT32)
> > ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN)
> VariableCache);
> > > +        CopyGuid (&(VariableCacheContext-
> > > >VariableRuntimeVolatileCache.Store->Signature),
> &(VariableCache-
> > > >Signature));
> > > +
> > > +        VariableCache = (VARIABLE_STORE_HEADER  *)
> (UINTN)
> > > mNvVariableCache;
> > > +        VariableCacheContext-
> > > >VariableRuntimeNvCache.PendingUpdateOffset = 0;
> > > +        VariableCacheContext-
> > > >VariableRuntimeNvCache.PendingUpdateLength =
> (UINT32) ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN)
> VariableCache);
> > > +        CopyGuid (&(VariableCacheContext-
> > >VariableRuntimeNvCache.Store-
> > > >Signature), &(VariableCache->Signature));
> > > +
> > > +        *(VariableCacheContext->PendingUpdate) =
> TRUE;
> > > +        *(VariableCacheContext->ReadLock) = FALSE;
> > > +        *(VariableCacheContext->HobFlushComplete) =
> FALSE;
> > > +      }
> > > +      Status = EFI_SUCCESS;
> > > +      break;
> > > +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> > > +      Status = SynchronizeRuntimeVariableCacheEx
> ();
> > > +      break;
> > > +    case
> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> > > +      if (CommBufferPayloadSize < sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> > > +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo:
> SMM
> > communication
> > > buffer size invalid!\n"));
> > > +        return EFI_SUCCESS;
> > > +      }
> > > +      GetRuntimeCacheInfo =
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > SmmVariableFunctionHeader->Data;
> > > +
> > > +      if (mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase > 0) {
> > > +        VariableCache = (VARIABLE_STORE_HEADER *)
> (UINTN)
> > > mVariableModuleGlobal-
> >VariableGlobal.HobVariableBase;
> > > +        GetRuntimeCacheInfo->TotalHobStorageSize =
> VariableCache->Size;
> > > +      } else {
> > > +        GetRuntimeCacheInfo->TotalHobStorageSize =
> 0;
> > > +      }
> > > +
> > > +      VariableCache = (VARIABLE_STORE_HEADER  *)
> (UINTN)
> > > mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> > > +      GetRuntimeCacheInfo->TotalVolatileStorageSize
> = VariableCache-
> > >Size;
> > > +      VariableCache = (VARIABLE_STORE_HEADER  *)
> (UINTN)
> > > mNvVariableCache;
> > > +      GetRuntimeCacheInfo->TotalNvStorageSize =
> (UINTN) VariableCache-
> > > >Size;
> > > +      GetRuntimeCacheInfo-
> >AuthenticatedVariableUsage =
> > > mVariableModuleGlobal->VariableGlobal.AuthFormat;
> > > +
> > > +      Status = EFI_SUCCESS;
> > > +      break;
> > >
> > >      default:
> > >        Status = EFI_UNSUPPORTED;
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.c
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.c
> > > index 0a1888e5ef..46f69765a4 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.c
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> RuntimeDx
> > > e.c
> > > @@ -13,7 +13,7 @@
> > >
> > >    InitCommunicateBuffer() is really function to
> check the variable data size.
> > >
> > > -Copyright (c) 2010 - 2017, Intel Corporation. All
> rights reserved.<BR>
> > > +Copyright (c) 2010 - 2019, Intel Corporation. All
> rights reserved.<BR>
> > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  **/
> > > @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-
> Clause-Patent
> > >  #include <Library/UefiRuntimeLib.h>
> > >  #include <Library/BaseMemoryLib.h>
> > >  #include <Library/DebugLib.h>
> > > +#include <Library/TimerLib.h>
> > >  #include <Library/UefiLib.h>
> > >  #include <Library/BaseLib.h>
> > >
> > >  #include <Guid/EventGroup.h>
> > > +#include <Guid/PiSmmCommunicationRegionTable.h>
> > >  #include <Guid/SmmVariableCommon.h>
> > >
> > >  #include "PrivilegePolymorphic.h"
> > > +#include "VariableParsing.h"
> > >
> > >  EFI_HANDLE                       mHandle
> = NULL;
> > >  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable
> = NULL;
> > > @@ -46,8 +49,19 @@ EFI_EVENT
> mVirtualAddressChangeEvent
> > =
> > > NULL;
> > >  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication
> =
> > > NULL;
> > >  UINT8                           *mVariableBuffer
> = NULL;
> > >  UINT8
> *mVariableBufferPhysical    = NULL;
> > > +VARIABLE_INFO_ENTRY             *mVariableInfo
> = NULL;
> > > +VARIABLE_STORE_HEADER
> *mVariableRuntimeHobCacheBuffer
> > =
> > > NULL;
> > > +VARIABLE_STORE_HEADER
> *mVariableRuntimeNvCacheBuffer
> > =
> > > NULL;
> > > +VARIABLE_STORE_HEADER
> *mVariableRuntimeVolatileCacheBuffer
> > > = NULL;
> > >  UINTN
> mVariableBufferSize;
> > > +UINTN
> mVariableRuntimeHobCacheBufferSize;
> > > +UINTN
> mVariableRuntimeNvCacheBufferSize;
> > > +UINTN
> mVariableRuntimeVolatileCacheBufferSize;
> > >  UINTN
> mVariableBufferPayloadSize;
> > > +BOOLEAN
> mVariableRuntimeCachePendingUpdate;
> > > +BOOLEAN
> mVariableRuntimeCacheReadLock;
> > > +BOOLEAN
> mVariableAuthFormat;
> > > +BOOLEAN                          mHobFlushComplete;
> > >  EFI_LOCK
> mVariableServicesLock;
> > >  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
> > >  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> > > @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
> > >    }
> > >  }
> > >
> > > +/**
> > > +  Return TRUE if ExitBootServices () has been
> called.
> > > +
> > > +  @retval TRUE If ExitBootServices () has been
> called.
> > > +**/
> > > +BOOLEAN
> > > +AtRuntime (
> > > +  VOID
> > > +  )
> >
> >
> > I think we can either:
> > 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
> > 2. Move AtRuntime() to VariableParsing.c so that the
> function can be shared
> >    with VariableRuntimeDxe & VariableSmmRuntimeDxe.
> And then update
> > the
> >    EfiAtRuntime() usages to AtRuntime() for
> VariableSmmRuntimeDxe.
> >
> >
> 
> #1 will work fine.
> 
> > > +{
> > > +  return EfiAtRuntime ();
> > > +}
> > > +
> > > +/**
> > > +  Initialize the variable cache buffer as an empty
> variable store.
> > > +
> > > +  @param[out]     VariableCacheBuffer     A pointer
> to pointer of a cache
> > > variable store.
> > > +  @param[in,out]  TotalVariableCacheSize  On input,
> the minimum size
> > > needed for the UEFI variable store cache
> > > +                                          buffer
> that is allocated. On output, the actual size of
> > > the buffer allocated.
> > > +                                          If
> TotalVariableCacheSize is zero, a buffer will not be
> > > allocated and the
> > > +                                          function
> will return with EFI_SUCCESS.
> > > +
> > > +  @retval EFI_SUCCESS             The variable
> cache was allocated and
> > initialized
> > > successfully.
> > > +  @retval EFI_INVALID_PARAMETER   A given pointer
> is NULL or an invalid
> > > variable store size was specified.
> > > +  @retval EFI_OUT_OF_RESOURCES    Insufficient
> resources are available
> > to
> > > allocate the variable store cache buffer.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +InitVariableCache (
> > > +  OUT    VARIABLE_STORE_HEADER
> **VariableCacheBuffer,
> > > +  IN OUT UINTN
> *TotalVariableCacheSize
> > > +  )
> > > +{
> > > +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> > > +
> > > +  if (TotalVariableCacheSize == NULL) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +  if (*TotalVariableCacheSize == 0) {
> > > +    return EFI_SUCCESS;
> > > +  }
> > > +  if (VariableCacheBuffer == NULL ||
> *TotalVariableCacheSize < sizeof
> > > (VARIABLE_STORE_HEADER)) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +  *TotalVariableCacheSize = ALIGN_VALUE
> (*TotalVariableCacheSize,
> > sizeof
> > > (UINT32));
> > > +
> > > +  //
> > > +  // Allocate NV Storage Cache and initialize it to
> all 1's (like an erased FV)
> > > +  //
> > > +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> > > AllocateRuntimePages (
> > > +                            EFI_SIZE_TO_PAGES
> (*TotalVariableCacheSize)
> > > +                            );
> > > +  if (*VariableCacheBuffer == NULL) {
> > > +    return EFI_OUT_OF_RESOURCES;
> > > +  }
> > > +  VariableCacheStorePtr = *VariableCacheBuffer;
> > > +  SetMem32 ((VOID *) VariableCacheStorePtr,
> *TotalVariableCacheSize,
> > > (UINT32) 0xFFFFFFFF);
> > > +
> > > +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> > > (VARIABLE_STORE_HEADER));
> > > +  VariableCacheStorePtr->Size    = (UINT32)
> *TotalVariableCacheSize;
> > > +  VariableCacheStorePtr->Format  =
> VARIABLE_STORE_FORMATTED;
> > > +  VariableCacheStorePtr->State   =
> VARIABLE_STORE_HEALTHY;
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > >  /**
> > >    Initialize the communicate buffer using DataSize
> and Function.
> > >
> > > @@ -153,6 +234,69 @@ InitCommunicateBuffer (
> > >  }
> > >
> > >
> > > +/**
> > > +  Gets a SMM communicate buffer from the
> > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as
> an entry in
> > > the UEFI
> > > +  system configuration table. A generic SMM
> communication buffer DXE
> > > driver may install the table or a custom table
> > > +  may be installed by a platform-specific driver.
> > > +
> > > +  The communicate size is:
> SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> > > +                            DataSize.
> > > +
> > > +  @param[in,out]   CommBufferSize   On input, the
> minimum size needed
> > > for the communication buffer.
> > > +                                    On output, the
> SMM buffer size available at
> > CommBuffer.
> > > +  @param[out]      CommBuffer       A pointer to an
> SMM communication
> > > buffer pointer.
> > > +
> > > +  @retval EFI_SUCCESS               The
> communication buffer was found
> > > successfully.
> > > +  @retval EFI_INVALID_PARAMETER     A given pointer
> is NULL or the
> > > CommBufferSize is zero.
> > > +  @retval EFI_NOT_FOUND             The
> > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not
> found.
> > > +  @retval EFI_OUT_OF_RESOURCES      A valid SMM
> communicate buffer
> > for
> > > the requested size is not available.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +GetCommunicateBuffer (
> > > +  IN OUT  UINTN     *CommBufferSize,
> > > +  OUT     UINT8     **CommBuffer
> > > +  )
> >
> >
> > Minor comment:
> >
> > I found that the consumers of the above function are:
> > GetRuntimeCacheInfo()
> > SendRuntimeVariableCacheContextToSmm()
> >
> > Both of them get called within SmmVariableReady() when
> the SMM variable
> > driver
> > finished initialization. I am wondering if they can
> simply use the pre-allocated
> > comm buffer (via InitCommunicateBuffer() and using
> 'mVariableBuffer'),
> > instead
> > of looking into the configuration table.
> >
> > In my opinion, this function can be dropped.
> >
> >
> 
> I did that initially. It was recommended to use this
> method.
> 
> > > +{
> > > +  EFI_STATUS                                Status;
> > > +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> > > *PiSmmCommunicationRegionTable;
> > > +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> > > +  UINTN
> EntrySize;
> > > +  UINT32                                    Index;
> > > +
> > > +  if (CommBuffer == NULL || CommBufferSize == NULL
> ||
> > > *CommBufferSize == 0) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  Status = EfiGetSystemConfigurationTable (
> > > +
> &gEdkiiPiSmmCommunicationRegionTableGuid,
> > > +             (VOID **)
> &PiSmmCommunicationRegionTable
> > > +             );
> > > +  if (EFI_ERROR (Status) ||
> PiSmmCommunicationRegionTable == NULL) {
> > > +    return EFI_NOT_FOUND;
> > > +  }
> > > +
> > > +  Entry = (EFI_MEMORY_DESCRIPTOR *)
> > (PiSmmCommunicationRegionTable
> > > + 1);
> > > +  EntrySize = 0;
> > > +  for (Index = 0; Index <
> PiSmmCommunicationRegionTable-
> > > >NumberOfEntries; Index++) {
> > > +    if (Entry->Type == EfiConventionalMemory) {
> > > +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry-
> >NumberOfPages);
> > > +      if (EntrySize >= *CommBufferSize) {
> > > +        break;
> > > +      }
> > > +    }
> > > +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *)
> Entry +
> > > PiSmmCommunicationRegionTable->DescriptorSize);
> > > +  }
> > > +
> > > +  if (Index < PiSmmCommunicationRegionTable-
> >NumberOfEntries) {
> > > +    *CommBufferSize = EntrySize;
> > > +    *CommBuffer = (UINT8 *) (UINTN) Entry-
> >PhysicalStart;
> > > +    return EFI_SUCCESS;
> > > +  }
> > > +
> > > +  return EFI_OUT_OF_RESOURCES;
> > > +}
> > > +
> > >  /**
> > >    Send the data in communicate buffer to SMM.
> > >
> > > @@ -424,6 +568,171 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > +/**
> > > +  Signals SMM to synchronize any pending variable
> updates with the
> > > runtime cache(s).
> > > +
> > > +**/
> > > +VOID
> > > +EFIAPI
> > > +SyncRuntimeCache (
> > > +  VOID
> > > +  )
> > > +{
> > > +  //
> > > +  // Init the communicate buffer. The buffer data
> size is:
> > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> > > +  //
> > > +  InitCommunicateBuffer (NULL, 0,
> > > SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  SendCommunicateBuffer (0);
> > > +}
> > > +
> > > +/**
> > > +  Check whether a SMI must be triggered to retrieve
> pending cache
> > updates.
> > > +
> > > +  If the variable HOB was finished being flushed
> since the last check for a
> > > runtime cache update, this function
> > > +  will prevent the HOB cache from being used for
> future runtime cache
> > hits.
> > > +
> > > +**/
> > > +VOID
> > > +EFIAPI
> > > +CheckForRuntimeCacheSync (
> > > +  VOID
> > > +  )
> > > +{
> > > +  if (mVariableRuntimeCachePendingUpdate) {
> > > +    SyncRuntimeCache ();
> > > +  }
> > > +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> > > +
> > > +  //
> > > +  // The HOB variable data may have finished being
> flushed in the runtime
> > > cache sync update
> > > +  //
> > > +  if (mHobFlushComplete &&
> mVariableRuntimeHobCacheBuffer != NULL)
> > {
> > > +    if (!AtRuntime ()) {
> > > +      FreePool (mVariableRuntimeHobCacheBuffer);
> > > +    }
> > > +    mVariableRuntimeHobCacheBuffer = NULL;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +  This code finds variable in a volatile memory
> store.
> > > +
> > > +  Caution: This function may receive untrusted
> input.
> > > +  The data size is external input, so this function
> will validate it carefully to
> > > avoid buffer overflow.
> > > +
> > > +  @param[in]      VariableName       Name of
> Variable to be found.
> > > +  @param[in]      VendorGuid         Variable
> vendor GUID.
> > > +  @param[out]     Attributes         Attribute
> value of the variable found.
> > > +  @param[in, out] DataSize           Size of Data
> found. If size is less than the
> > > +                                     data, this
> value contains the required size.
> > > +  @param[out]     Data               Data pointer.
> > > +
> > > +  @retval EFI_SUCCESS                Found the
> specified variable.
> > > +  @retval EFI_INVALID_PARAMETER      Invalid
> parameter.
> > > +  @retval EFI_NOT_FOUND              The specified
> variable could not be
> > found.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +FindVariableInRuntimeCache (
> > > +  IN      CHAR16
> *VariableName,
> > > +  IN      EFI_GUID
> *VendorGuid,
> > > +  OUT     UINT32
> *Attributes OPTIONAL,
> > > +  IN OUT  UINTN
> *DataSize,
> > > +  OUT     VOID                              *Data
> OPTIONAL
> > > +  )
> > > +{
> > > +  EFI_STATUS              Status;
> > > +  UINTN                   DelayIndex;
> > > +  UINTN                   TempDataSize;
> > > +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> > > +  VARIABLE_STORE_TYPE     StoreType;
> > > +  VARIABLE_STORE_HEADER
> *VariableStoreList[VariableStoreTypeMax];
> > > +
> > > +  Status = EFI_NOT_FOUND;
> > > +
> > > +  if (VariableName == NULL || VendorGuid == NULL ||
> DataSize == NULL) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  for (DelayIndex = 0;
> mVariableRuntimeCacheReadLock && DelayIndex <
> > > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> > > +    MicroSecondDelay (10);
> > > +  }
> > > +  if (DelayIndex <
> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> > > +    ASSERT (!mVariableRuntimeCacheReadLock);
> > > +
> > > +    mVariableRuntimeCacheReadLock = TRUE;
> > > +    CheckForRuntimeCacheSync ();
> > > +
> > > +    if (!mVariableRuntimeCachePendingUpdate) {
> > > +      //
> > > +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > +      // The index and attributes mapping must be
> kept in this order as
> > > FindVariable
> > > +      // makes use of this mapping to implement
> search algorithm.
> > > +      //
> > > +      VariableStoreList[VariableStoreTypeVolatile]
> =
> > > mVariableRuntimeVolatileCacheBuffer;
> > > +      VariableStoreList[VariableStoreTypeHob]
> =
> > > mVariableRuntimeHobCacheBuffer;
> > > +      VariableStoreList[VariableStoreTypeNv]
> =
> > > mVariableRuntimeNvCacheBuffer;
> > > +
> > > +      for (StoreType = (VARIABLE_STORE_TYPE) 0;
> StoreType <
> > > VariableStoreTypeMax; StoreType++) {
> > > +        if (VariableStoreList[StoreType] == NULL) {
> > > +          continue;
> > > +        }
> > > +
> > > +        RtPtrTrack.StartPtr = GetStartPointer
> (VariableStoreList[StoreType]);
> > > +        RtPtrTrack.EndPtr   = GetEndPointer
> (VariableStoreList[StoreType]);
> > > +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType
> ==
> > > VariableStoreTypeVolatile);
> > > +
> > > +        Status = FindVariableEx (VariableName,
> VendorGuid, FALSE,
> > > &RtPtrTrack);
> > > +        if (!EFI_ERROR (Status)) {
> > > +          break;
> > > +        }
> > > +      }
> > > +
> > > +      if (!EFI_ERROR (Status)) {
> > > +        //
> > > +        // Get data size
> > > +        //
> > > +        TempDataSize = DataSizeOfVariable
> (RtPtrTrack.CurrPtr);
> > > +        ASSERT (TempDataSize != 0);
> > > +
> > > +        if (*DataSize >= TempDataSize) {
> > > +          if (Data == NULL) {
> > > +            Status = EFI_INVALID_PARAMETER;
> > > +            goto Done;
> > > +          }
> > > +
> > > +          CopyMem (Data, GetVariableDataPtr
> (RtPtrTrack.CurrPtr),
> > > TempDataSize);
> > > +          if (Attributes != NULL) {
> > > +            *Attributes = RtPtrTrack.CurrPtr-
> >Attributes;
> > > +          }
> > > +
> > > +          *DataSize = TempDataSize;
> > > +
> > > +          UpdateVariableInfo (VariableName,
> VendorGuid,
> > RtPtrTrack.Volatile,
> > > TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> > > +
> > > +          Status = EFI_SUCCESS;
> > > +          goto Done;
> > > +        } else {
> > > +          *DataSize = TempDataSize;
> > > +          Status = EFI_BUFFER_TOO_SMALL;
> > > +          goto Done;
> > > +        }
> > > +      }
> > > +    }
> > > +  }
> > > +
> > > +Done:
> > > +  mVariableRuntimeCacheReadLock = FALSE;
> >
> >
> > If timeout occurs when acquiring the read lock, should
> this flag be set to
> > FALSE
> > in such case?
> >
> 
> Please see reply to patch #8.
> 
> > Best Regards,
> > Hao Wu
> >
> >
> > > +
> > > +  return Status;
> > > +}
> > > +
> > >  /**
> > >    This code finds variable in storage blocks
> (Volatile or Non-Volatile).
> > >
> > > @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
> > >    )
> > >  {
> > >    EFI_STATUS                                Status;
> > > -  UINTN
> PayloadSize;
> > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > -  UINTN
> TempDataSize;
> > > -  UINTN
> VariableNameSize;
> > >
> > >    if (VariableName == NULL || VendorGuid == NULL ||
> DataSize == NULL) {
> > >      return EFI_INVALID_PARAMETER;
> > >    }
> > > -
> > > -  TempDataSize          = *DataSize;
> > > -  VariableNameSize      = StrSize (VariableName);
> > > -  SmmVariableHeader     = NULL;
> > > -
> > > -  //
> > > -  // If VariableName exceeds SMM payload limit.
> Return failure
> > > -  //
> > > -  if (VariableNameSize > mVariableBufferPayloadSize
> - OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> > > -    return EFI_INVALID_PARAMETER;
> > > -  }
> > > -
> > > -
> AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> > > -
> > > -  //
> > > -  // Init the communicate buffer. The buffer data
> size is:
> > > -  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> > > -  //
> > > -  if (TempDataSize > mVariableBufferPayloadSize -
> OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > VariableNameSize) {
> > > -    //
> > > -    // If output data buffer exceed SMM payload
> limit. Trim output buffer to
> > > SMM payload size
> > > -    //
> > > -    TempDataSize = mVariableBufferPayloadSize -
> OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > VariableNameSize;
> > > -  }
> > > -  PayloadSize = OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> > > VariableNameSize + TempDataSize;
> > > -
> > > -  Status = InitCommunicateBuffer ((VOID
> **)&SmmVariableHeader,
> > > PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> > > -  if (EFI_ERROR (Status)) {
> > > -    goto Done;
> > > -  }
> > > -  ASSERT (SmmVariableHeader != NULL);
> > > -
> > > -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> > > -  SmmVariableHeader->DataSize   = TempDataSize;
> > > -  SmmVariableHeader->NameSize   = VariableNameSize;
> > > -  if (Attributes == NULL) {
> > > -    SmmVariableHeader->Attributes = 0;
> > > -  } else {
> > > -    SmmVariableHeader->Attributes = *Attributes;
> > > -  }
> > > -  CopyMem (SmmVariableHeader->Name, VariableName,
> > > SmmVariableHeader->NameSize);
> > > -
> > > -  //
> > > -  // Send data to SMM.
> > > -  //
> > > -  Status = SendCommunicateBuffer (PayloadSize);
> > > -
> > > -  //
> > > -  // Get data from SMM.
> > > -  //
> > > -  if (Status == EFI_SUCCESS || Status ==
> EFI_BUFFER_TOO_SMALL) {
> > > -    //
> > > -    // SMM CommBuffer DataSize can be a trimed
> value
> > > -    // Only update DataSize when needed
> > > -    //
> > > -    *DataSize = SmmVariableHeader->DataSize;
> > > -  }
> > > -  if (Attributes != NULL) {
> > > -    *Attributes = SmmVariableHeader->Attributes;
> > > -  }
> > > -
> > > -  if (EFI_ERROR (Status)) {
> > > -    goto Done;
> > > -  }
> > > -
> > > -  if (Data != NULL) {
> > > -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name
> +
> > > SmmVariableHeader->NameSize, SmmVariableHeader-
> >DataSize);
> > > -  } else {
> > > -    Status = EFI_INVALID_PARAMETER;
> > > +  if (VariableName[0] == 0) {
> > > +    return EFI_NOT_FOUND;
> > >    }
> > >
> > > -Done:
> > > +  AcquireLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +  Status =  FindVariableInRuntimeCache
> (VariableName, VendorGuid,
> > > Attributes, DataSize, Data);
> > >    ReleaseLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +
> > >    return Status;
> > >  }
> > >
> > > -
> > >  /**
> > >    This code Finds the Next available variable.
> > >
> > > @@ -870,6 +1109,17 @@ OnReadyToBoot (
> > >    //
> > >    SendCommunicateBuffer (0);
> > >
> > > +  //
> > > +  // Install the system configuration table for
> variable info data captured
> > > +  //
> > > +  if (FeaturePcdGet (PcdVariableCollectStatistics))
> {
> > > +    if (mVariableAuthFormat) {
> > > +      gBS->InstallConfigurationTable
> (&gEfiAuthenticatedVariableGuid,
> > > mVariableInfo);
> > > +    } else {
> > > +      gBS->InstallConfigurationTable
> (&gEfiVariableGuid, mVariableInfo);
> > > +    }
> > > +  }
> > > +
> > >    gBS->CloseEvent (Event);
> > >  }
> > >
> > > @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
> > >  {
> > >    EfiConvertPointer (0x0, (VOID **)
> &mVariableBuffer);
> > >    EfiConvertPointer (0x0, (VOID **)
> &mSmmCommunication);
> > > +  EfiConvertPointer (0x0, (VOID **)
> &mVariableRuntimeHobCacheBuffer);
> > > +  EfiConvertPointer (0x0, (VOID **)
> &mVariableRuntimeNvCacheBuffer);
> > > +  EfiConvertPointer (0x0, (VOID **)
> > > &mVariableRuntimeVolatileCacheBuffer);
> > >  }
> > >
> > >  /**
> > > @@ -969,6 +1222,173 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > +/**
> > > +  This code gets information needed from SMM for
> runtime cache
> > > initialization.
> > > +
> > > +  @param[out] TotalHobStorageSize         Output
> pointer for the total HOB
> > > storage size in bytes.
> > > +  @param[out] TotalNvStorageSize          Output
> pointer for the total non-
> > > volatile storage size in bytes.
> > > +  @param[out] TotalVolatileStorageSize    Output
> pointer for the total
> > > volatile storage size in bytes.
> > > +  @param[out] AuthenticatedVariableUsage  Output
> pointer that indicates
> > if
> > > authenticated variables are to be used.
> > > +
> > > +  @retval EFI_SUCCESS                     Retrieved
> the size successfully.
> > > +  @retval EFI_INVALID_PARAMETER
> TotalNvStorageSize parameter is
> > > NULL.
> > > +  @retval EFI_OUT_OF_RESOURCES            Could not
> allocate a
> > CommBuffer.
> > > +  @retval Others                          Could not
> retrieve the size successfully.;
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +GetRuntimeCacheInfo (
> > > +  OUT UINTN
> *TotalHobStorageSize,
> > > +  OUT UINTN
> *TotalNvStorageSize,
> > > +  OUT UINTN
> *TotalVolatileStorageSize,
> > > +  OUT BOOLEAN
> *AuthenticatedVariableUsage
> > > +  )
> > > +{
> > > +  EFI_STATUS
> Status;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > *SmmGetRuntimeCacheInfo;
> > > +  EFI_SMM_COMMUNICATE_HEADER
> > > *SmmCommunicateHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  UINTN
> CommSize;
> > > +  UINTN
> CommBufferSize;
> > > +  UINT8
> *CommBuffer;
> > > +
> > > +  SmmGetRuntimeCacheInfo = NULL;
> > > +  CommBuffer = NULL;
> > > +
> > > +  if (TotalHobStorageSize == NULL ||
> TotalNvStorageSize == NULL ||
> > > TotalVolatileStorageSize == NULL ||
> AuthenticatedVariableUsage == NULL)
> > {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  AcquireLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +
> > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > +  CommBufferSize = CommSize;
> > > +  Status = GetCommunicateBuffer (&CommBufferSize,
> &CommBuffer);
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +  if (CommBuffer == NULL) {
> > > +    Status = EFI_OUT_OF_RESOURCES;
> > > +    goto Done;
> > > +  }
> > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > +
> > > +  SmmCommunicateHeader =
> (EFI_SMM_COMMUNICATE_HEADER *)
> > > CommBuffer;
> > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > &gEfiSmmVariableProtocolGuid);
> > > +  SmmCommunicateHeader->MessageLength =
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > +
> > > +  SmmVariableFunctionHeader =
> > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> SmmCommunicateHeader-
> > > >Data;
> > > +  SmmVariableFunctionHeader->Function =
> > > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> > > +  SmmGetRuntimeCacheInfo =
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > SmmVariableFunctionHeader->Data;
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  Status = mSmmCommunication->Communicate
> (mSmmCommunication,
> > > CommBuffer, &CommSize);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +  if (CommSize <=
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > +    goto Done;
> > > +  }
> > > +
> > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +
> > > +  //
> > > +  // Get data from SMM.
> > > +  //
> > > +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
> > >TotalHobStorageSize;
> > > +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo-
> >TotalNvStorageSize;
> > > +  *TotalVolatileStorageSize =
> SmmGetRuntimeCacheInfo-
> > > >TotalVolatileStorageSize;
> > > +  *AuthenticatedVariableUsage =
> SmmGetRuntimeCacheInfo-
> > > >AuthenticatedVariableUsage;
> > > +
> > > +Done:
> > > +  ReleaseLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +  return Status;
> > > +}
> > > +
> > > +/**
> > > +  Sends the runtime variable cache context
> information to SMM.
> > > +
> > > +  @retval EFI_SUCCESS               Retrieved the
> size successfully.
> > > +  @retval EFI_INVALID_PARAMETER
> TotalNvStorageSize parameter is
> > > NULL.
> > > +  @retval EFI_OUT_OF_RESOURCES      Could not
> allocate a CommBuffer.
> > > +  @retval Others                    Could not
> retrieve the size successfully.;
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +SendRuntimeVariableCacheContextToSmm (
> > > +  VOID
> > > +  )
> > > +{
> > > +  EFI_STATUS
> Status;
> > > +
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *SmmRuntimeVarCacheContext;
> > > +  EFI_SMM_COMMUNICATE_HEADER
> > > *SmmCommunicateHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  UINTN
> CommSize;
> > > +  UINTN
> CommBufferSize;
> > > +  UINT8
> *CommBuffer;
> > > +
> > > +  SmmRuntimeVarCacheContext = NULL;
> > > +  CommBuffer = NULL;
> > > +
> > > +  AcquireLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +
> > > +  //
> > > +  // Init the communicate buffer. The buffer data
> size is:
> > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> )
> > ;
> > > +  //
> > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> )
> > ;
> > > +  CommBufferSize = CommSize;
> > > +  Status = GetCommunicateBuffer (&CommBufferSize,
> &CommBuffer);
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +  if (CommBuffer == NULL) {
> > > +    Status = EFI_OUT_OF_RESOURCES;
> > > +    goto Done;
> > > +  }
> > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > +
> > > +  SmmCommunicateHeader =
> (EFI_SMM_COMMUNICATE_HEADER *)
> > > CommBuffer;
> > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > &gEfiSmmVariableProtocolGuid);
> > > +  SmmCommunicateHeader->MessageLength =
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> )
> > ;
> > > +
> > > +  SmmVariableFunctionHeader =
> > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> SmmCommunicateHeader-
> > > >Data;
> > > +  SmmVariableFunctionHeader->Function =
> > >
> >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> T;
> > > +  SmmRuntimeVarCacheContext =
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *) SmmVariableFunctionHeader->Data;
> > > +
> > > +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> > > mVariableRuntimeHobCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> > > mVariableRuntimeVolatileCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> > > mVariableRuntimeNvCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->PendingUpdate =
> > > &mVariableRuntimeCachePendingUpdate;
> > > +  SmmRuntimeVarCacheContext->ReadLock =
> > > &mVariableRuntimeCacheReadLock;
> > > +  SmmRuntimeVarCacheContext->HobFlushComplete =
> > > &mHobFlushComplete;
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  Status = mSmmCommunication->Communicate
> (mSmmCommunication,
> > > CommBuffer, &CommSize);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +  if (CommSize <=
> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > +    goto Done;
> > > +  }
> > > +
> > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +
> > > +Done:
> > > +  ReleaseLockOnlyAtBootTime
> (&mVariableServicesLock);
> > > +  return Status;
> > > +}
> > > +
> > >  /**
> > >    Initialize variable service and install Variable
> Architectural protocol.
> > >
> > > @@ -985,7 +1405,7 @@ SmmVariableReady (
> > >  {
> > >    EFI_STATUS                                Status;
> > >
> > > -  Status = gBS->LocateProtocol
> (&gEfiSmmVariableProtocolGuid, NULL,
> > > (VOID **)&mSmmVariable);
> > > +  Status = gBS->LocateProtocol
> (&gEfiSmmVariableProtocolGuid, NULL,
> > > (VOID **) &mSmmVariable);
> > >    if (EFI_ERROR (Status)) {
> > >      return;
> > >    }
> > > @@ -1007,6 +1427,40 @@ SmmVariableReady (
> > >    //
> > >    mVariableBufferPhysical = mVariableBuffer;
> > >
> > > +  //
> > > +  // Allocate runtime variable cache memory
> buffers.
> > > +  //
> > > +  Status =  GetRuntimeCacheInfo (
> > > +              &mVariableRuntimeHobCacheBufferSize,
> > > +              &mVariableRuntimeNvCacheBufferSize,
> > > +
> &mVariableRuntimeVolatileCacheBufferSize,
> > > +              &mVariableAuthFormat
> > > +              );
> > > +  if (!EFI_ERROR (Status)) {
> > > +    Status = InitVariableCache
> (&mVariableRuntimeHobCacheBuffer,
> > > &mVariableRuntimeHobCacheBufferSize);
> > > +    if (!EFI_ERROR (Status)) {
> > > +      Status = InitVariableCache
> (&mVariableRuntimeNvCacheBuffer,
> > > &mVariableRuntimeNvCacheBufferSize);
> > > +      if (!EFI_ERROR (Status)) {
> > > +        Status = InitVariableCache
> (&mVariableRuntimeVolatileCacheBuffer,
> > > &mVariableRuntimeVolatileCacheBufferSize);
> > > +        if (!EFI_ERROR (Status)) {
> > > +          Status = InitVariableParsing
> (mVariableAuthFormat);
> > > +          ASSERT_EFI_ERROR (Status);
> > > +
> > > +          Status =
> SendRuntimeVariableCacheContextToSmm ();
> > > +          if (!EFI_ERROR (Status)) {
> > > +            SyncRuntimeCache ();
> > > +          }
> > > +        }
> > > +      }
> > > +    }
> > > +    if (EFI_ERROR (Status)) {
> > > +      mVariableRuntimeHobCacheBuffer = NULL;
> > > +      mVariableRuntimeNvCacheBuffer = NULL;
> > > +      mVariableRuntimeVolatileCacheBuffer = NULL;
> > > +    }
> > > +  }
> > > +  ASSERT_EFI_ERROR (Status);
> > > +
> > >    gRT->GetVariable         =
> RuntimeServiceGetVariable;
> > >    gRT->GetNextVariableName =
> RuntimeServiceGetNextVariableName;
> > >    gRT->SetVariable         =
> RuntimeServiceSetVariable;
> > > --
> > > 2.16.2.windows.1
> >
> 


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 22:01       ` Michael D Kinney
@ 2019-10-03 23:31         ` Kubacki, Michael A
  2019-10-04  6:50           ` Laszlo Ersek
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-03 23:31 UTC (permalink / raw)
  To: Kinney, Michael D, Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Ni, Ray, Wang, Jian J, Yao, Jiewen

I agree, I will make the default to enable the runtime cache.

> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Thursday, October 3, 2019 3:01 PM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>; Wu, Hao A
> <hao.a.wu@intel.com>; devel@edk2.groups.io; Kinney, Michael D
> <michael.d.kinney@intel.com>
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Ni, Ray
> <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; Yao, Jiewen
> <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> cache support
> 
> Michael,
> 
> Perhaps the FeaturePCD for #2 should be enabled by default
> so the platform DSC only needs to set this PCD for some
> validation tests.
> 
> Mike
> 
> 
> > -----Original Message-----
> > From: Kubacki, Michael A <michael.a.kubacki@intel.com>
> > Sent: Thursday, October 3, 2019 2:54 PM
> > To: Wu, Hao A <hao.a.wu@intel.com>; devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric
> > <eric.dong@intel.com>; Laszlo Ersek <lersek@redhat.com>;
> > Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> > <michael.d.kinney@intel.com>; Ni, Ray
> > <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen
> > <jiewen.yao@intel.com>
> > Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> > RT GetVariable() cache support
> >
> > #1 - The plan is to remove the polling entirely in V3.
> >
> > #2 - I'd prefer to take a definitive direction and
> > reduce validation and maintenance
> >         effort but you and Laszlo both requested this so
> > I'll add a FeaturePCD to control
> >         activation of the runtime cache in this patch
> > series. Perhaps this can be removed
> >         in the future.
> >
> > #3 - Will be done in V3.
> >
> > Other replies are inline.
> >
> > > -----Original Message-----
> > > From: Wu, Hao A <hao.a.wu@intel.com>
> > > Sent: Thursday, October 3, 2019 1:05 AM
> > > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > > devel@edk2.groups.io
> > > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > > <ard.biesheuvel@linaro.org>; Dong, Eric
> > <eric.dong@intel.com>; Laszlo Ersek
> > > <lersek@redhat.com>; Gao, Liming
> > <liming.gao@intel.com>; Kinney, Michael
> > > D <michael.d.kinney@intel.com>; Ni, Ray
> > <ray.ni@intel.com>; Wang, Jian J
> > > <jian.j.wang@intel.com>; Yao, Jiewen
> > <jiewen.yao@intel.com>
> > > Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> > RT GetVariable()
> > > cache support
> > >
> > > Before any comment on the patch, since I am not
> > experienced in the
> > > Variable
> > > driver, I would like to ask for help from other
> > reviewers to look into this
> > > patch and provide feedbacks as well. Thanks in
> > advance.
> > >
> > > With the above fact, some comments provided below
> > maybe wrong. So
> > > please help
> > > to kindly correct me.
> > >
> > >
> > > Some general comments:
> > > 1. I am not sure if bringing the TimerLib dependency
> > (delay in acquiring the
> > >    runtime cache read lock) to variable driver (a
> > software driver for the most
> > >    part) is a good idea.
> > >
> > >    Hope other reviewers can provide some feedbacks for
> > this. Thanks in
> > > advance.
> > >
> > > 2. In my opinion, I prefer a switch can be provided
> > for platform owners to
> > >    choose between using the runtime cache and going
> > through SMM for
> > > GetVariable
> > >    (and for GetNextVariableName in the next patch as
> > well).
> > >
> > >    If platform owners feel uncomfortable with using
> > the runtime cache with
> > >    regard to the security perspective, they can switch
> > to the origin solution.
> > >
> > > 3. Please help to remove the 'EFIAPI' keyword for new
> > driver internal
> > > functions;
> > >
> > >
> > > Inline comments below:
> > >
> > >
> > > > -----Original Message-----
> > > > From: Kubacki, Michael A
> > > > Sent: Saturday, September 28, 2019 9:47 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo
> > Ersek; Gao, Liming;
> > > Kinney,
> > > > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> > Jiewen
> > > > Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> > RT GetVariable()
> > > > cache support
> > > >
> > > >
> > REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> > > >
> > > > This change reduces SMIs for GetVariable () by
> > maintaining a
> > > > UEFI variable cache in Runtime DXE in addition to
> > the pre-
> > > > existing cache in SMRAM. When the Runtime Service
> > GetVariable()
> > > > is invoked, a Runtime DXE cache is used instead of
> > triggering an
> > > > SMI to VariableSmm. This can improve overall system
> > performance
> > > > by servicing variable read requests without
> > rendezvousing all
> > > > cores into SMM.
> > > >
> > > > The following are important points regarding this
> > change.
> > > >
> > > > 1. All of the non-volatile storage contents are
> > loaded into the
> > > >    cache upon driver load. This one time load
> > operation from storage
> > > >    is preferred as opposed to building the cache on
> > demand. An on-
> > > >    demand cache would require a fallback SMI to load
> > data into the
> > > >    cache as variables are requested.
> > > >
> > > > 2. SetVariable () requests will continue to always
> > trigger an SMI.
> > > >    This occurs regardless of whether the variable is
> > volatile or
> > > >    non-volatile.
> > > >
> > > > 3. Both volatile and non-volatile variables are
> > cached in a runtime
> > > >    buffer. As is the case in the current EDK II
> > variable driver, they
> > > >    continue to be cached in separate buffers.
> > > >
> > > > 4. The cache in Runtime DXE and SMM are intended to
> > be exact copies
> > > >    of one another. All SMM variable accesses only
> > return data from the
> > > >    SMM cache. The runtime caches are only updated
> > after the variable I/O
> > > >    operation is successful in SMM. The runtime
> > caches are only updated
> > > >    from SMM.
> > > >
> > > > 5. Synchronization mechanisms are in place to ensure
> > the runtime cache
> > > >    content integrity with the SMM cache. These may
> > result in updates to
> > > >    runtime cache that are the same in content but
> > different in offset and
> > > >    size from updates to the SMM cache.
> > > >
> > > > When using SMM variables, two caches will now be
> > present.
> > > > 1. "Runtime Cache" - Maintained in
> > VariableSmmRuntimeDxe. Used to
> > > > service
> > > >    Runtime Services GetVariable () and
> > GetNextVariableName () callers.
> > > > 2. "SMM Cache" - Maintained in VariableSmm to
> > service SMM GetVariable
> > > ()
> > > >    and GetNextVariableName () callers.
> > > >    a. This cache is retained so SMM modules do not
> > operate on data outside
> > > >       SMRAM.
> > > >
> > > > It is possible to view UEFI variable read and write
> > statistics by setting
> > > > the
> > gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> > ics
> > > > FeaturePcd
> > > > to TRUE and using the VariableInfo UEFI application
> > in MdeModulePkg to
> > > > dump
> > > > variable statistics to the console. By doing so, a
> > user can view the number
> > > > of GetVariable () hits from the Runtime DXE variable
> > driver (Runtime Cache
> > > > hits) and the SMM variable driver (SMM Cache hits).
> > SMM Cache hits for
> > > > GetVariable () will occur when SMM modules invoke
> > GetVariable ().
> > > >
> > > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > > Cc: Eric Dong <eric.dong@intel.com>
> > > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > > Cc: Liming Gao <liming.gao@intel.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Ray Ni <ray.ni@intel.com>
> > > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > > Signed-off-by: Michael Kubacki
> > <michael.a.kubacki@intel.com>
> > > > ---
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> > meDxe.inf
> > > > |   2 +
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.i
> > nf           |
> > > 2
> > > > +
> > > >
> > > >
> > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> > ntimeDxe.i
> > > > nf |  31 +-
> > > >
> > > >
> > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStand
> > aloneMm.inf
> > > > |   2 +
> > > >  MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > |  29 +-
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > |  39
> > > +-
> > > >
> > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> > meCache.h
> > > > |  47 ++
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > |  44
> > > +-
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> > meCache.c
> > > > | 153 +++++
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > |
> > > 114
> > > > +++-
> > > >
> > > >
> > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> > ntimeDxe.
> > > > c   | 608 +++++++++++++++++---
> > > >  11 files changed, 966 insertions(+), 105
> > deletions(-)
> > > >
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeDxe.inf
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeDxe.inf
> > > > index 08a5490787..ceea5d1ff9 100644
> > > > ---
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeDxe.inf
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeDxe.inf
> > > > @@ -40,6 +40,8 @@
> > > >    VariableNonVolatile.h
> > > >    VariableParsing.c
> > > >    VariableParsing.h
> > > > +  VariableRuntimeCache.c
> > > > +  VariableRuntimeCache.h
> > >
> > >
> > > Per my understanding, the module specified by
> > VariableRuntimeDxe.inf
> > > does not
> > > involve SMM/SMI for variable services (like
> > GetVariable). It looks weird to
> > > me
> > > for this INF to include the newly introduced runtime
> > cache codes (below
> > > source
> > > header files):
> > >
> > > VariableRuntimeCache.c
> > > VariableRuntimeCache.h
> > >
> > >
> >
> > This is because Variable.c is common to the runtime DXE
> > and SMM variable
> > driver and it contains the code to update variable
> > caches. The runtime cache
> > synchronization function
> > (SynchronizeRuntimeVariableCache ()) will return
> > if the runtime cache pointer is NULL.
> >
> > > >    PrivilegePolymorphic.h
> > > >    Measurement.c
> > > >    TcgMorLockDxe.c
> > > > diff --git
> > > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .inf
> > > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .inf
> > > > index 6dc2721b81..bc3033588d 100644
> > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .inf
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .inf
> > > > @@ -49,6 +49,8 @@
> > > >    VariableNonVolatile.h
> > > >    VariableParsing.c
> > > >    VariableParsing.h
> > > > +  VariableRuntimeCache.c
> > > > +  VariableRuntimeCache.h
> > > >    VarCheck.c
> > > >    Variable.h
> > > >    PrivilegePolymorphic.h
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.inf
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.inf
> > > > index 14894e6f13..70837ac6e0 100644
> > > > ---
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.inf
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.inf
> > > > @@ -13,7 +13,7 @@
> > > >  #  may not be modified without authorization. If
> > platform fails to protect
> > > > these resources,
> > > >  #  the authentication service provided in this
> > driver will be broken, and the
> > > > behavior is undefined.
> > > >  #
> > > > -# Copyright (c) 2010 - 2017, Intel Corporation. All
> > rights reserved.<BR>
> > > > +# Copyright (c) 2010 - 2019, Intel Corporation. All
> > rights reserved.<BR>
> > > >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >  #
> > > >  ##
> > > > @@ -39,6 +39,10 @@
> > > >    VariableSmmRuntimeDxe.c
> > > >    PrivilegePolymorphic.h
> > > >    Measurement.c
> > > > +  VariableParsing.c
> > > > +  VariableParsing.h
> > > > +  VariableRuntimeCache.c
> > > > +  VariableRuntimeCache.h
> > > >
> > > >  [Packages]
> > > >    MdePkg/MdePkg.dec
> > > > @@ -49,6 +53,7 @@
> > > >    BaseLib
> > > >    UefiBootServicesTableLib
> > > >    DebugLib
> > > > +  TimerLib
> > > >    UefiRuntimeLib
> > > >    DxeServicesTableLib
> > > >    UefiDriverEntryPoint
> > > > @@ -65,7 +70,29 @@
> > > >    gEdkiiVariableLockProtocolGuid                ##
> > PRODUCES
> > > >    gEdkiiVarCheckProtocolGuid                    ##
> > PRODUCES
> > > >
> > > > +[Pcd]
> > > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize
> > ##
> > > > CONSUMES
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
> > > ##
> > > > CONSUMES
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariab
> > leSize
> > > > ## CONSUMES
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize
> > ##
> > > > CONSUMES
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize
> > ##
> > > > CONSUMES
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpace
> > Size
> > > > ## CONSUMES
> > > > +
> > > >
> > >
> > gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVari
> > ableSpace
> > > > Size  ## CONSUMES
> > >
> > >
> > > Not sure if the above PCDs are really needed by
> > VariableSmmRuntimeDxe.
> > >
> > >
> >
> > I will double check and remove any not required.
> >
> > > > +
> > > > +[FeaturePcd]
> > > > +
> > gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> > ics
> > > ##
> > > > CONSUMES
> > > > +
> > > >  [Guids]
> > > > +  ## PRODUCES             ## GUID # Signature of
> > Variable store header
> > > > +  ## CONSUMES             ## GUID # Signature of
> > Variable store header
> > > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > > +  gEfiAuthenticatedVariableGuid
> > > > +
> > > > +  ## PRODUCES             ## GUID # Signature of
> > Variable store header
> > > > +  ## CONSUMES             ## GUID # Signature of
> > Variable store header
> > > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > > +  gEfiVariableGuid
> > > > +
> > > >    gEfiEventVirtualAddressChangeGuid             ##
> > CONSUMES ## Event
> > > >    gEfiEventExitBootServicesGuid                 ##
> > CONSUMES ## Event
> > > >    ## CONSUMES ## GUID # Locate protocol
> > > > @@ -82,6 +109,8 @@
> > > >    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > > >    gEfiImageSecurityDatabaseGuid
> > > >
> > > > +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> > > > SOMETIMES_CONSUMES ## SystemTable
> > > > +
> > > >  [Depex]
> > > >    gEfiSmmCommunicationProtocolGuid
> > > >
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> > ndaloneMm.i
> > > > nf
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> > ndaloneMm.
> > > > inf
> > > > index ca9d23ce9f..95c5310c0b 100644
> > > > ---
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> > ndaloneMm.i
> > > > nf
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> > ndaloneMm.
> > > > inf
> > > > @@ -49,6 +49,8 @@
> > > >    VariableNonVolatile.h
> > > >    VariableParsing.c
> > > >    VariableParsing.h
> > > > +  VariableRuntimeCache.c
> > > > +  VariableRuntimeCache.h
> > > >    VarCheck.c
> > > >    Variable.h
> > > >    PrivilegePolymorphic.h
> > > > diff --git
> > a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > > b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > > index c527a59891..ceef44dfd2 100644
> > > > --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > > +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > > @@ -1,7 +1,7 @@
> > > >  /** @file
> > > >    The file defined some common structures used for
> > communicating
> > > > between SMM variable module and SMM variable wrapper
> > module.
> > > >
> > > > -Copyright (c) 2011 - 2015, Intel Corporation. All
> > rights reserved.<BR>
> > > > +Copyright (c) 2011 - 2019, Intel Corporation. All
> > rights reserved.<BR>
> > > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >
> > > >  **/
> > > > @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-
> > Clause-Patent
> > > >  #ifndef _SMM_VARIABLE_COMMON_H_
> > > >  #define _SMM_VARIABLE_COMMON_H_
> > > >
> > > > +#include <Guid/VariableFormat.h>
> > > >  #include <Protocol/VarCheck.h>
> > > >
> > > >  #define EFI_SMM_VARIABLE_WRITE_GUID \
> > > > @@ -66,6 +67,16 @@ typedef struct {
> > > >  #define
> > > >
> > SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
> > 10
> > > >
> > > >  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
> > 11
> > > > +//
> > > > +// The payload for this function is
> > > >
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > > +//
> > > > +#define
> > > >
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> > T
> > > > 12
> > > > +
> > > > +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
> > > 13
> > > > +//
> > > > +// The payload for this function is
> > > > SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > > +//
> > > > +#define
> > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> > > > 14
> > > >
> > > >  ///
> > > >  /// Size of SMM communicate header, without
> > including the payload.
> > > > @@ -120,4 +131,20 @@ typedef struct {
> > > >    UINTN
> > VariablePayloadSize;
> > > >  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> > > >
> > > > +typedef struct {
> > > > +  BOOLEAN                 *ReadLock;
> > > > +  BOOLEAN                 *PendingUpdate;
> > > > +  BOOLEAN                 *HobFlushComplete;
> > > > +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> > > > +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> > > > +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> > > > +}
> > > >
> > >
> >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > > > +
> > > > +typedef struct {
> > > > +  UINTN                   TotalHobStorageSize;
> > > > +  UINTN                   TotalNvStorageSize;
> > > > +  UINTN                   TotalVolatileStorageSize;
> > > > +  BOOLEAN
> > AuthenticatedVariableUsage;
> > > > +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> > > > +
> > > >  #endif // _SMM_VARIABLE_COMMON_H_
> > > > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > index fb574b2e32..b9723c0250 100644
> > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-
> > Clause-Patent
> > > >  ///
> > > >  #define ISO_639_2_ENTRY_SIZE    3
> > > >
> > > > +///
> > > > +/// The timeout to in 10us units to wait for the
> > > > +/// variable runtime cache read lock to be
> > acquired.
> > > > +///
> > > > +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> > > > +
> > > >  typedef enum {
> > > >    VariableStoreTypeVolatile,
> > > >    VariableStoreTypeHob,
> > > > @@ -64,6 +70,21 @@ typedef enum {
> > > >    VariableStoreTypeMax
> > > >  } VARIABLE_STORE_TYPE;
> > > >
> > > > +typedef struct {
> > > > +  UINT32                  PendingUpdateOffset;
> > > > +  UINT32                  PendingUpdateLength;
> > > > +  VARIABLE_STORE_HEADER   *Store;
> > > > +} VARIABLE_RUNTIME_CACHE;
> > > > +
> > > > +typedef struct {
> > > > +  BOOLEAN                 *ReadLock;
> > > > +  BOOLEAN                 *PendingUpdate;
> > > > +  BOOLEAN                 *HobFlushComplete;
> > > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> > > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> > > > +  VARIABLE_RUNTIME_CACHE
> > VariableRuntimeVolatileCache;
> > > > +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> > > > +
> > > >  typedef struct {
> > > >    VARIABLE_HEADER *CurrPtr;
> > > >    //
> > > > @@ -79,14 +100,16 @@ typedef struct {
> > > >  } VARIABLE_POINTER_TRACK;
> > > >
> > > >  typedef struct {
> > > > -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> > > > -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> > > > -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> > > > -  EFI_LOCK              VariableServicesLock;
> > > > -  UINT32                ReentrantState;
> > > > -  BOOLEAN               AuthFormat;
> > > > -  BOOLEAN               AuthSupport;
> > > > -  BOOLEAN               EmuNvMode;
> > > > +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> > > > +  EFI_PHYSICAL_ADDRESS
> > HobVariableBackupBase;
> > >
> > >
> > > I do not see any usage of the new field
> > "HobVariableBackupBase".
> > > Could you help to double confirm?
> > >
> > >
> >
> > You are correct. I removed usage of this variable before
> > sending the
> > patch series and the global variable here needs to be
> > cleaned up.
> >
> > > > +  EFI_PHYSICAL_ADDRESS
> > VolatileVariableBase;
> > > > +  EFI_PHYSICAL_ADDRESS
> > NonVolatileVariableBase;
> > > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> > VariableRuntimeCacheContext;
> > > > +  EFI_LOCK
> > VariableServicesLock;
> > > > +  UINT32                          ReentrantState;
> > > > +  BOOLEAN                         AuthFormat;
> > > > +  BOOLEAN                         AuthSupport;
> > > > +  BOOLEAN                         EmuNvMode;
> > > >  } VARIABLE_GLOBAL;
> > > >
> > > >  typedef struct {
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.h
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.
> > > > h
> > > > new file mode 100644
> > > > index 0000000000..09b83eb215
> > > > --- /dev/null
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.
> > > > h
> > > > @@ -0,0 +1,47 @@
> > > > +/** @file
> > > > +  The common variable volatile store routines
> > shared by the
> > > DXE_RUNTIME
> > > > variable
> > > > +  module and the DXE_SMM variable module.
> > > > +
> > > > +Copyright (c) 2019, Intel Corporation. All rights
> > reserved.<BR>
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> > > > +#define _VARIABLE_RUNTIME_CACHE_H_
> > > > +
> > > > +#include "Variable.h"
> > > > +
> > > > +/**
> > > > +  Copies any pending updates to runtime variable
> > caches.
> > > > +
> > > > +  @retval EFI_UNSUPPORTED         The volatile
> > store to be updated is not
> > > > initialized properly.
> > > > +  @retval EFI_SUCCESS             The volatile
> > store was updated successfully.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +SynchronizeRuntimeVariableCacheEx (
> > > > +  VOID
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Synchronizes the runtime variable caches with all
> > pending updates
> > > outside
> > > > runtime.
> > > > +
> > > > +  Ensures all conditions are met to maintain
> > coherency for runtime cache
> > > > updates.
> > > > +
> > > > +  @param[in] VariableRuntimeCache Variable runtime
> > cache structure for
> > > > the runtime cache being synchronized.
> > > > +  @param[in] Offset               Offset in bytes
> > to apply the update.
> > > > +  @param[in] Length               Length of data in
> > bytes of the update.
> > > > +
> > > > +  @retval EFI_UNSUPPORTED         The volatile
> > store to be updated is not
> > > > initialized properly.
> > > > +  @retval EFI_SUCCESS             The volatile
> > store was updated successfully.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +SynchronizeRuntimeVariableCache (
> > > > +  IN  VARIABLE_RUNTIME_CACHE
> > *VariableRuntimeCache,
> > > > +  IN  UINTN                           Offset,
> > > > +  IN  UINTN                           Length
> > > > +  );
> > > > +
> > > > +#endif
> > > > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > index 5da2354aa5..bb2fa3fc19 100644
> > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-
> > Clause-Patent
> > > >  #include "Variable.h"
> > > >  #include "VariableNonVolatile.h"
> > > >  #include "VariableParsing.h"
> > > > +#include "VariableRuntimeCache.h"
> > > >
> > > >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> > > >
> > > > @@ -332,6 +333,12 @@ RecordVarErrorFlag (
> > > >        // Update the data in NV cache.
> > > >        //
> > > >        *VarErrFlag = TempFlag;
> > > > +      Status =  SynchronizeRuntimeVariableCache (
> > > > +                  &mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache,
> > > > +                  (UINTN) VarErrFlag - (UINTN)
> > mNvVariableCache + (UINTN)
> > > > mVariableModuleGlobal-
> > >VariableGlobal.NonVolatileVariableBase,
> > > > +                  sizeof (TempFlag)
> > > > +                  );
> > > > +      ASSERT_EFI_ERROR (Status);
> > > >      }
> > > >    }
> > > >  }
> > > > @@ -755,12 +762,24 @@ Reclaim (
> > > >
> > > >  Done:
> > > >    if (IsVolatile || mVariableModuleGlobal-
> > >VariableGlobal.EmuNvMode) {
> > > > +    Status =  SynchronizeRuntimeVariableCache (
> > > > +                &mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e,
> > > > +                0,
> > > > +                VariableStoreHeader->Size
> > > > +                );
> > > > +    ASSERT_EFI_ERROR (Status);
> > > >      FreePool (ValidBuffer);
> > > >    } else {
> > > >      //
> > > >      // For NV variable reclaim, we use
> > mNvVariableCache as the buffer, so
> > > > copy the data back.
> > > >      //
> > > > -    CopyMem (mNvVariableCache, (UINT8
> > *)(UINTN)VariableBase,
> > > > VariableStoreHeader->Size);
> > > > +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN)
> > VariableBase,
> > > > VariableStoreHeader->Size);
> > > > +    Status =  SynchronizeRuntimeVariableCache (
> > > > +                &(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache),
> > > > +                0,
> > > > +                VariableStoreHeader->Size
> > > > +                );
> > > > +    ASSERT_EFI_ERROR (Status);
> > > >    }
> > > >
> > > >    return Status;
> > > > @@ -1592,6 +1611,7 @@ UpdateVariable (
> > > >    VARIABLE_POINTER_TRACK              *Variable;
> > > >    VARIABLE_POINTER_TRACK              NvVariable;
> > > >    VARIABLE_STORE_HEADER
> > *VariableStoreHeader;
> > > > +  VARIABLE_RUNTIME_CACHE
> > *VolatileCacheInstance;
> > > >    UINT8
> > *BufferForMerge;
> > > >    UINTN
> > MergedBufSize;
> > > >    BOOLEAN                             DataReady;
> > > > @@ -2235,6 +2255,21 @@ UpdateVariable (
> > > >    }
> > > >
> > > >  Done:
> > > > +  if (!EFI_ERROR (Status)) {
> > > > +    if (Variable->Volatile) {
> > > > +      VolatileCacheInstance =
> > &(mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e);
> > > > +    } else {
> > > > +      VolatileCacheInstance =
> > &(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache);
> > > > +    }
> > > > +
> > > > +    Status =  SynchronizeRuntimeVariableCache (
> > > > +                VolatileCacheInstance,
> > > > +                0,
> > > > +                VolatileCacheInstance->Store->Size
> > > > +                );
> > > > +    ASSERT_EFI_ERROR (Status);
> > > > +  }
> > > > +
> > > >    return Status;
> > > >  }
> > > >
> > > > @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
> > > >          ErrorFlag = TRUE;
> > > >        }
> > > >      }
> > > > +    Status =  SynchronizeRuntimeVariableCache (
> > > > +                &mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache,
> > > > +                0,
> > > > +                mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.S
> > > > tore->Size
> > > > +                );
> > > > +    ASSERT_EFI_ERROR (Status);
> > > >      if (ErrorFlag) {
> > > >        //
> > > >        // We still have HOB variable(s) not flushed
> > in flash.
> > > > @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
> > > >        // All HOB variables have been flushed in
> > flash.
> > > >        //
> > > >        DEBUG ((EFI_D_INFO, "Variable driver: all HOB
> > variables have been
> > > > flushed in flash.\n"));
> > > > +      *(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.HobFlushComp
> > lete) =
> > > TRUE;
> > > >        if (!AtRuntime ()) {
> > > >          FreePool ((VOID *) VariableStoreHeader);
> > > >        }
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.c
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.c
> > > > new file mode 100644
> > > > index 0000000000..2642d9b000
> > > > --- /dev/null
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> > timeCache.c
> > > > @@ -0,0 +1,153 @@
> > > > +/** @file
> > > > +  The common variable volatile store routines
> > shared by the
> > > DXE_RUNTIME
> > > > variable
> > > > +  module and the DXE_SMM variable module.
> > > > +
> > > > +  Caution: This module requires additional review
> > when modified.
> > > > +  This driver will have external input - variable
> > data. They may be input in
> > > > SMM mode.
> > > > +  This external input must be validated carefully
> > to avoid security issue like
> > > > +  buffer overflow, integer overflow.
> > > > +
> > > > +Copyright (c) 2019, Intel Corporation. All rights
> > reserved.<BR>
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include "VariableParsing.h"
> > > > +#include "VariableRuntimeCache.h"
> > > > +
> > > > +extern VARIABLE_MODULE_GLOBAL
> > *mVariableModuleGlobal;
> > > > +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> > > > +
> > > > +/**
> > > > +  Copies any pending updates to runtime variable
> > caches.
> > > > +
> > > > +  @retval EFI_UNSUPPORTED         The volatile
> > store to be updated is not
> > > > initialized properly.
> > > > +  @retval EFI_SUCCESS             The volatile
> > store was updated successfully.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +SynchronizeRuntimeVariableCacheEx (
> > >
> > >
> > > It is not clear to me why this function is named as
> > the "Ex" version of function
> > > SynchronizeRuntimeVariableCache(). For me, this
> > function looks more like a
> > > basic
> > > version.
> > >
> > > I would suggest a name change for the functions
> > provided in file
> > > VariableRuntimeCache.c to better reflect their usage
> > model.
> > >
> > >
> >
> > I'll rename it in V3.
> >
> > > > +  VOID
> > > > +  )
> > > > +{
> > >
> > >
> > > I would recommend that at least a local variable
> > should be introduced to
> > > reduce
> > > the duplications of:
> > >
> > > "mVariableModuleGlobal-
> > >VariableGlobal.VariableRuntimeCacheContext"
> > >
> > > in this function in order to make it easier to read.
> > >
> > >
> >
> > I'll add it in V3.
> >
> > > > +  if (
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.St
> > > > ore == NULL ||
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.Store == NULL ||
> > > > +    mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e == NULL
> > > > +    ) {
> > > > +    return EFI_UNSUPPORTED;
> > > > +  }
> > > > +
> > > > +  if (*(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e)) {
> > > > +    if (
> > > > +      mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.S
> > > > tore != NULL &&
> > > > +      mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase > 0
> > > > +      ) {
> > > > +      CopyMem (
> > > > +        (VOID *) (
> > > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.S
> > > > tore) +
> > > > +          mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.P
> > > > endingUpdateOffset
> > > > +          ),
> > > > +        (VOID *) (
> > > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.HobVariableBase) +
> > > > +          mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.P
> > > > endingUpdateOffset
> > > > +          ),
> > > > +        mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.P
> > > > endingUpdateLength
> > > > +        );
> > > > +      mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.P
> > > > endingUpdateLength = 0;
> > > > +      mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeHobCache.P
> > > > endingUpdateOffset = 0;
> > > > +    }
> > > > +
> > > > +    CopyMem (
> > > > +      (VOID *) (
> > > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.St
> > > > ore) +
> > > > +        mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.Pe
> > > > ndingUpdateOffset
> > > > +        ),
> > > > +      (VOID *) (
> > > > +        ((UINT8 *) (UINTN) mNvVariableCache) +
> > > > +        mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.Pe
> > > > ndingUpdateOffset
> > > > +        ),
> > > > +      mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.Pe
> > > > ndingUpdateLength
> > > > +      );
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.Pe
> > > > ndingUpdateLength = 0;
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeNvCache.Pe
> > > > ndingUpdateOffset = 0;
> > > > +
> > > > +    CopyMem (
> > > > +      (VOID *) (
> > > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.Store) +
> > > > +        mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.PendingUpdateOffset
> > > > +      ),
> > > > +      (VOID *) (
> > > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.VolatileVariableBase) +
> > > > +        mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.PendingUpdateOffset
> > > > +        ),
> > > > +      mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.PendingUpdateLength
> > > > +      );
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.PendingUpdateLength = 0;
> > > > +    mVariableModuleGlobal-
> > > >
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> > imeVolatileCach
> > > > e.PendingUpdateOffset = 0;
> > > > +    *(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e) = FALSE;
> > > > +  }
> > > > +
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Synchronizes the runtime variable caches with all
> > pending updates
> > > outside
> > > > runtime.
> > > > +
> > > > +  Ensures all conditions are met to maintain
> > coherency for runtime cache
> > > > updates.
> > > > +
> > > > +  @param[in] VariableRuntimeCache Variable runtime
> > cache structure for
> > > > the runtime cache being synchronized.
> > > > +  @param[in] Offset               Offset in bytes
> > to apply the update.
> > > > +  @param[in] Length               Length of data in
> > bytes of the update.
> > > > +
> > > > +  @retval EFI_UNSUPPORTED         The volatile
> > store to be updated is not
> > > > initialized properly.
> > > > +  @retval EFI_SUCCESS             The volatile
> > store was updated successfully.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +SynchronizeRuntimeVariableCache (
> > > > +  IN  VARIABLE_RUNTIME_CACHE
> > *VariableRuntimeCache,
> > > > +  IN  UINTN                           Offset,
> > > > +  IN  UINTN                           Length
> > > > +  )
> > > > +{
> > > > +  if (VariableRuntimeCache == NULL) {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  } else if (VariableRuntimeCache->Store == NULL) {
> > > > +      // Runtime cache is not available yet at this
> > point,
> > > > +      // Return EFI_SUCCESS instead of
> > EFI_NOT_AVAILABLE_YET to let it
> > > > progress
> > > > +      return EFI_SUCCESS;
> > > > +  }
> > > > +
> > > > +  if (
> > > > +    mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e == NULL
> > > ||
> > > > +    mVariableModuleGlobal-
> > > > >VariableGlobal.VariableRuntimeCacheContext.ReadLock
> > == NULL
> > > > +    ) {
> > > > +    return EFI_UNSUPPORTED;
> > > > +  }
> > > > +
> > > > +  if (
> > > > +    *(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e) &&
> > > > +    VariableRuntimeCache->PendingUpdateLength > 0
> > > > +    ) {
> > > > +    VariableRuntimeCache->PendingUpdateLength =
> > > > +      (UINT32) (
> > > > +        MAX (
> > > > +          (UINTN) (VariableRuntimeCache-
> > >PendingUpdateOffset +
> > > > VariableRuntimeCache->PendingUpdateLength),
> > > > +          Offset + Length
> > > > +        ) - MIN ((UINTN) VariableRuntimeCache-
> > >PendingUpdateOffset,
> > > Offset)
> > > > +      );
> > > > +    VariableRuntimeCache->PendingUpdateOffset =
> > > > +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
> > > >PendingUpdateOffset,
> > > > Offset);
> > > > +  } else {
> > > > +    VariableRuntimeCache->PendingUpdateLength =
> > (UINT32) Length;
> > > > +    VariableRuntimeCache->PendingUpdateOffset =
> > (UINT32) Offset;
> > > > +  }
> > > > +  *(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> > e) = TRUE;
> > > > +
> > > > +  if (*(mVariableModuleGlobal-
> > > >
> > >VariableGlobal.VariableRuntimeCacheContext.ReadLock) ==
> > FALSE) {
> > > > +    return SynchronizeRuntimeVariableCacheEx ();
> > > > +  }
> > > > +
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > diff --git
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .c
> > > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .c
> > > > index ce409f22a3..8d767f75ac 100644
> > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .c
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > .c
> > > > @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-
> > Clause-Patent
> > > >  #include <Guid/SmmVariableCommon.h>
> > > >  #include "Variable.h"
> > > >  #include "VariableParsing.h"
> > > > +#include "VariableRuntimeCache.h"
> > > > +
> > > > +extern VARIABLE_STORE_HEADER
> > *mNvVariableCache;
> > > >
> > > >  BOOLEAN
> > mAtRuntime              = FALSE;
> > > >  UINT8
> > *mVariableBufferPayload = NULL;
> > > > @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
> > > >  EFI_STATUS
> > > >  EFIAPI
> > > >  SmmVariableHandler (
> > > > -  IN     EFI_HANDLE
> > DispatchHandle,
> > > > -  IN     CONST VOID
> > *RegisterContext,
> > > > -  IN OUT VOID
> > *CommBuffer,
> > > > -  IN OUT UINTN
> > *CommBufferSize
> > > > +  IN     EFI_HANDLE
> > DispatchHandle,
> > > > +  IN     CONST VOID
> > *RegisterContext,
> > > > +  IN OUT VOID
> > *CommBuffer,
> > > > +  IN OUT UINTN
> > *CommBufferSize
> > > >    )
> > > >  {
> > > > -  EFI_STATUS
> > Status;
> > > > -  SMM_VARIABLE_COMMUNICATE_HEADER
> > > > *SmmVariableFunctionHeader;
> > > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > > *SmmVariableHeader;
> > > > -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > > *GetNextVariableName;
> > > > -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > > *QueryVariableInfo;
> > > > -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > > *GetPayloadSize;
> > > > -  VARIABLE_INFO_ENTRY
> > *VariableInfo;
> > > > -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > > *VariableToLock;
> > > > -
> > SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > > *CommVariableProperty;
> > > > -  UINTN
> > InfoSize;
> > > > -  UINTN
> > NameBufferSize;
> > > > -  UINTN
> > CommBufferPayloadSize;
> > > > -  UINTN
> > TempCommBufferSize;
> > > > +  EFI_STATUS
> > Status;
> > > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > > *SmmVariableFunctionHeader;
> > > > +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > > *SmmVariableHeader;
> > > > +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > > *GetNextVariableName;
> > > > +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > > *QueryVariableInfo;
> > > > +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > > *GetPayloadSize;
> > > > +
> > > >
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > > *RuntimeVariableCacheContext;
> > > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > > *GetRuntimeCacheInfo;
> > > > +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > > > *VariableToLock;
> > > > +
> > SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > > *CommVariableProperty;
> > > > +  VARIABLE_INFO_ENTRY
> > *VariableInfo;
> > > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> > > > *VariableCacheContext;
> > > > +  VARIABLE_STORE_HEADER
> > *VariableCache;
> > > > +  UINTN
> > InfoSize;
> > > > +  UINTN
> > NameBufferSize;
> > > > +  UINTN
> > CommBufferPayloadSize;
> > > > +  UINTN
> > TempCommBufferSize;
> > > >
> > > >    //
> > > >    // If input is invalid, stop processing this SMI
> > > > @@ -789,6 +796,79 @@ SmmVariableHandler (
> > > >                   );
> > > >        CopyMem (SmmVariableFunctionHeader->Data,
> > > mVariableBufferPayload,
> > > > CommBufferPayloadSize);
> > > >        break;
> > > > +    case
> > > >
> > >
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> > T:
> > > > +      if (CommBufferPayloadSize < sizeof
> > > >
> > >
> > (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTE
> > XT)
> > > )
> > > > {
> > >
> > >
> > > The above check is not correct, I think it should be:
> > >
> > > if (CommBufferPayloadSize < sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > )
> > > ) {
> > >
> > > Please help to double confirm.
> > > Also, I recommend some security tests should be
> > performed to these new
> > > cases in
> > > the variable SMI handler.
> > >
> > >
> >
> > You're right. The wrong macro was simply copied.
> >
> > > > +        DEBUG ((DEBUG_ERROR,
> > "InitRuntimeVariableCacheContext: SMM
> > > > communication buffer size invalid!\n"));
> > > > +      } else if (mEndOfDxe) {
> > > > +        DEBUG ((DEBUG_ERROR,
> > "InitRuntimeVariableCacheContext: Cannot
> > > > init context after end of DXE!\n"));
> > > > +      } else {
> > > > +        RuntimeVariableCacheContext =
> > > >
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > > *) SmmVariableFunctionHeader->Data;
> > >
> > >
> > > Not sure on this one:
> > >
> > > Do you think it is necessary to copy the contents in
> > the comm buffer to the
> > > pre-allocated SMM variable buffer payload
> > 'mVariableBufferPayload' to
> > > avoid
> > > TOCTOU issue? Since there are some tests (sort of, a
> > couple of ASSERTs)
> > > based
> > > on the comm buffer content.
> > >
> > >
> >
> > I understand the TOCTOU observation. But is this still a
> > concern with all the
> > cores rendezvoused in SMM prior to end of DXE?
> >
> > > > +        VariableCacheContext =
> > &mVariableModuleGlobal-
> > > > >VariableGlobal.VariableRuntimeCacheContext;
> > > > +
> > > > +        ASSERT (RuntimeVariableCacheContext-
> > >RuntimeVolatileCache !=
> > > > NULL);
> > > > +        ASSERT (RuntimeVariableCacheContext-
> > >RuntimeNvCache != NULL);
> > > > +        ASSERT (RuntimeVariableCacheContext-
> > >PendingUpdate != NULL);
> > > > +        ASSERT (RuntimeVariableCacheContext-
> > >ReadLock != NULL);
> > > > +        ASSERT (RuntimeVariableCacheContext-
> > >HobFlushComplete !=
> > > NULL);
> > > > +
> > > > +        VariableCacheContext-
> > >VariableRuntimeHobCache.Store      =
> > > > RuntimeVariableCacheContext->RuntimeHobCache;
> > > > +        VariableCacheContext-
> > >VariableRuntimeVolatileCache.Store =
> > > > RuntimeVariableCacheContext->RuntimeVolatileCache;
> > > > +        VariableCacheContext-
> > >VariableRuntimeNvCache.Store       =
> > > > RuntimeVariableCacheContext->RuntimeNvCache;
> > > > +        VariableCacheContext->PendingUpdate
> > =
> > > > RuntimeVariableCacheContext->PendingUpdate;
> > > > +        VariableCacheContext->ReadLock
> > =
> > > > RuntimeVariableCacheContext->ReadLock;
> > > > +        VariableCacheContext->HobFlushComplete
> > =
> > > > RuntimeVariableCacheContext->HobFlushComplete;
> > > > +
> > > > +        // Set up the intial pending request since
> > the RT cache needs to be in
> > > > sync with SMM cache
> > > > +        if (mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase == 0) {
> > > > +          VariableCacheContext-
> > > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > > +          VariableCacheContext-
> > > > >VariableRuntimeHobCache.PendingUpdateLength = 0;
> > > > +        } else {
> > > > +          VariableCache = (VARIABLE_STORE_HEADER *)
> > (UINTN)
> > > > mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > > > +          VariableCacheContext-
> > > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > > +          VariableCacheContext-
> > > > >VariableRuntimeHobCache.PendingUpdateLength =
> > (UINT32) ((UINTN)
> > > > GetEndPointer (VariableCache) - (UINTN)
> > VariableCache);
> > > > +          CopyGuid (&(VariableCacheContext-
> > > > >VariableRuntimeHobCache.Store->Signature),
> > &(VariableCache-
> > > > >Signature));
> > > > +        }
> > > > +        VariableCache = (VARIABLE_STORE_HEADER  *)
> > (UINTN)
> > > > mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > > > +        VariableCacheContext-
> > > > >VariableRuntimeVolatileCache.PendingUpdateOffset
> > = 0;
> > > > +        VariableCacheContext-
> > > > >VariableRuntimeVolatileCache.PendingUpdateLength
> > = (UINT32)
> > > ((UINTN)
> > > > GetEndPointer (VariableCache) - (UINTN)
> > VariableCache);
> > > > +        CopyGuid (&(VariableCacheContext-
> > > > >VariableRuntimeVolatileCache.Store->Signature),
> > &(VariableCache-
> > > > >Signature));
> > > > +
> > > > +        VariableCache = (VARIABLE_STORE_HEADER  *)
> > (UINTN)
> > > > mNvVariableCache;
> > > > +        VariableCacheContext-
> > > > >VariableRuntimeNvCache.PendingUpdateOffset = 0;
> > > > +        VariableCacheContext-
> > > > >VariableRuntimeNvCache.PendingUpdateLength =
> > (UINT32) ((UINTN)
> > > > GetEndPointer (VariableCache) - (UINTN)
> > VariableCache);
> > > > +        CopyGuid (&(VariableCacheContext-
> > > >VariableRuntimeNvCache.Store-
> > > > >Signature), &(VariableCache->Signature));
> > > > +
> > > > +        *(VariableCacheContext->PendingUpdate) =
> > TRUE;
> > > > +        *(VariableCacheContext->ReadLock) = FALSE;
> > > > +        *(VariableCacheContext->HobFlushComplete) =
> > FALSE;
> > > > +      }
> > > > +      Status = EFI_SUCCESS;
> > > > +      break;
> > > > +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> > > > +      Status = SynchronizeRuntimeVariableCacheEx
> > ();
> > > > +      break;
> > > > +    case
> > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> > > > +      if (CommBufferPayloadSize < sizeof
> > > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> > > > +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo:
> > SMM
> > > communication
> > > > buffer size invalid!\n"));
> > > > +        return EFI_SUCCESS;
> > > > +      }
> > > > +      GetRuntimeCacheInfo =
> > > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > > SmmVariableFunctionHeader->Data;
> > > > +
> > > > +      if (mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase > 0) {
> > > > +        VariableCache = (VARIABLE_STORE_HEADER *)
> > (UINTN)
> > > > mVariableModuleGlobal-
> > >VariableGlobal.HobVariableBase;
> > > > +        GetRuntimeCacheInfo->TotalHobStorageSize =
> > VariableCache->Size;
> > > > +      } else {
> > > > +        GetRuntimeCacheInfo->TotalHobStorageSize =
> > 0;
> > > > +      }
> > > > +
> > > > +      VariableCache = (VARIABLE_STORE_HEADER  *)
> > (UINTN)
> > > > mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > > > +      GetRuntimeCacheInfo->TotalVolatileStorageSize
> > = VariableCache-
> > > >Size;
> > > > +      VariableCache = (VARIABLE_STORE_HEADER  *)
> > (UINTN)
> > > > mNvVariableCache;
> > > > +      GetRuntimeCacheInfo->TotalNvStorageSize =
> > (UINTN) VariableCache-
> > > > >Size;
> > > > +      GetRuntimeCacheInfo-
> > >AuthenticatedVariableUsage =
> > > > mVariableModuleGlobal->VariableGlobal.AuthFormat;
> > > > +
> > > > +      Status = EFI_SUCCESS;
> > > > +      break;
> > > >
> > > >      default:
> > > >        Status = EFI_UNSUPPORTED;
> > > > diff --git
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.c
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.c
> > > > index 0a1888e5ef..46f69765a4 100644
> > > > ---
> > > >
> > >
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.c
> > > > +++
> > > >
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> > RuntimeDx
> > > > e.c
> > > > @@ -13,7 +13,7 @@
> > > >
> > > >    InitCommunicateBuffer() is really function to
> > check the variable data size.
> > > >
> > > > -Copyright (c) 2010 - 2017, Intel Corporation. All
> > rights reserved.<BR>
> > > > +Copyright (c) 2010 - 2019, Intel Corporation. All
> > rights reserved.<BR>
> > > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >
> > > >  **/
> > > > @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-
> > Clause-Patent
> > > >  #include <Library/UefiRuntimeLib.h>
> > > >  #include <Library/BaseMemoryLib.h>
> > > >  #include <Library/DebugLib.h>
> > > > +#include <Library/TimerLib.h>
> > > >  #include <Library/UefiLib.h>
> > > >  #include <Library/BaseLib.h>
> > > >
> > > >  #include <Guid/EventGroup.h>
> > > > +#include <Guid/PiSmmCommunicationRegionTable.h>
> > > >  #include <Guid/SmmVariableCommon.h>
> > > >
> > > >  #include "PrivilegePolymorphic.h"
> > > > +#include "VariableParsing.h"
> > > >
> > > >  EFI_HANDLE                       mHandle
> > = NULL;
> > > >  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable
> > = NULL;
> > > > @@ -46,8 +49,19 @@ EFI_EVENT
> > mVirtualAddressChangeEvent
> > > =
> > > > NULL;
> > > >  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication
> > =
> > > > NULL;
> > > >  UINT8                           *mVariableBuffer
> > = NULL;
> > > >  UINT8
> > *mVariableBufferPhysical    = NULL;
> > > > +VARIABLE_INFO_ENTRY             *mVariableInfo
> > = NULL;
> > > > +VARIABLE_STORE_HEADER
> > *mVariableRuntimeHobCacheBuffer
> > > =
> > > > NULL;
> > > > +VARIABLE_STORE_HEADER
> > *mVariableRuntimeNvCacheBuffer
> > > =
> > > > NULL;
> > > > +VARIABLE_STORE_HEADER
> > *mVariableRuntimeVolatileCacheBuffer
> > > > = NULL;
> > > >  UINTN
> > mVariableBufferSize;
> > > > +UINTN
> > mVariableRuntimeHobCacheBufferSize;
> > > > +UINTN
> > mVariableRuntimeNvCacheBufferSize;
> > > > +UINTN
> > mVariableRuntimeVolatileCacheBufferSize;
> > > >  UINTN
> > mVariableBufferPayloadSize;
> > > > +BOOLEAN
> > mVariableRuntimeCachePendingUpdate;
> > > > +BOOLEAN
> > mVariableRuntimeCacheReadLock;
> > > > +BOOLEAN
> > mVariableAuthFormat;
> > > > +BOOLEAN                          mHobFlushComplete;
> > > >  EFI_LOCK
> > mVariableServicesLock;
> > > >  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
> > > >  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> > > > @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
> > > >    }
> > > >  }
> > > >
> > > > +/**
> > > > +  Return TRUE if ExitBootServices () has been
> > called.
> > > > +
> > > > +  @retval TRUE If ExitBootServices () has been
> > called.
> > > > +**/
> > > > +BOOLEAN
> > > > +AtRuntime (
> > > > +  VOID
> > > > +  )
> > >
> > >
> > > I think we can either:
> > > 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
> > > 2. Move AtRuntime() to VariableParsing.c so that the
> > function can be shared
> > >    with VariableRuntimeDxe & VariableSmmRuntimeDxe.
> > And then update
> > > the
> > >    EfiAtRuntime() usages to AtRuntime() for
> > VariableSmmRuntimeDxe.
> > >
> > >
> >
> > #1 will work fine.
> >
> > > > +{
> > > > +  return EfiAtRuntime ();
> > > > +}
> > > > +
> > > > +/**
> > > > +  Initialize the variable cache buffer as an empty
> > variable store.
> > > > +
> > > > +  @param[out]     VariableCacheBuffer     A pointer
> > to pointer of a cache
> > > > variable store.
> > > > +  @param[in,out]  TotalVariableCacheSize  On input,
> > the minimum size
> > > > needed for the UEFI variable store cache
> > > > +                                          buffer
> > that is allocated. On output, the actual size of
> > > > the buffer allocated.
> > > > +                                          If
> > TotalVariableCacheSize is zero, a buffer will not be
> > > > allocated and the
> > > > +                                          function
> > will return with EFI_SUCCESS.
> > > > +
> > > > +  @retval EFI_SUCCESS             The variable
> > cache was allocated and
> > > initialized
> > > > successfully.
> > > > +  @retval EFI_INVALID_PARAMETER   A given pointer
> > is NULL or an invalid
> > > > variable store size was specified.
> > > > +  @retval EFI_OUT_OF_RESOURCES    Insufficient
> > resources are available
> > > to
> > > > allocate the variable store cache buffer.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +InitVariableCache (
> > > > +  OUT    VARIABLE_STORE_HEADER
> > **VariableCacheBuffer,
> > > > +  IN OUT UINTN
> > *TotalVariableCacheSize
> > > > +  )
> > > > +{
> > > > +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> > > > +
> > > > +  if (TotalVariableCacheSize == NULL) {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  }
> > > > +  if (*TotalVariableCacheSize == 0) {
> > > > +    return EFI_SUCCESS;
> > > > +  }
> > > > +  if (VariableCacheBuffer == NULL ||
> > *TotalVariableCacheSize < sizeof
> > > > (VARIABLE_STORE_HEADER)) {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  }
> > > > +  *TotalVariableCacheSize = ALIGN_VALUE
> > (*TotalVariableCacheSize,
> > > sizeof
> > > > (UINT32));
> > > > +
> > > > +  //
> > > > +  // Allocate NV Storage Cache and initialize it to
> > all 1's (like an erased FV)
> > > > +  //
> > > > +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> > > > AllocateRuntimePages (
> > > > +                            EFI_SIZE_TO_PAGES
> > (*TotalVariableCacheSize)
> > > > +                            );
> > > > +  if (*VariableCacheBuffer == NULL) {
> > > > +    return EFI_OUT_OF_RESOURCES;
> > > > +  }
> > > > +  VariableCacheStorePtr = *VariableCacheBuffer;
> > > > +  SetMem32 ((VOID *) VariableCacheStorePtr,
> > *TotalVariableCacheSize,
> > > > (UINT32) 0xFFFFFFFF);
> > > > +
> > > > +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> > > > (VARIABLE_STORE_HEADER));
> > > > +  VariableCacheStorePtr->Size    = (UINT32)
> > *TotalVariableCacheSize;
> > > > +  VariableCacheStorePtr->Format  =
> > VARIABLE_STORE_FORMATTED;
> > > > +  VariableCacheStorePtr->State   =
> > VARIABLE_STORE_HEALTHY;
> > > > +
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > +
> > > >  /**
> > > >    Initialize the communicate buffer using DataSize
> > and Function.
> > > >
> > > > @@ -153,6 +234,69 @@ InitCommunicateBuffer (
> > > >  }
> > > >
> > > >
> > > > +/**
> > > > +  Gets a SMM communicate buffer from the
> > > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as
> > an entry in
> > > > the UEFI
> > > > +  system configuration table. A generic SMM
> > communication buffer DXE
> > > > driver may install the table or a custom table
> > > > +  may be installed by a platform-specific driver.
> > > > +
> > > > +  The communicate size is:
> > SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> > > > +                            DataSize.
> > > > +
> > > > +  @param[in,out]   CommBufferSize   On input, the
> > minimum size needed
> > > > for the communication buffer.
> > > > +                                    On output, the
> > SMM buffer size available at
> > > CommBuffer.
> > > > +  @param[out]      CommBuffer       A pointer to an
> > SMM communication
> > > > buffer pointer.
> > > > +
> > > > +  @retval EFI_SUCCESS               The
> > communication buffer was found
> > > > successfully.
> > > > +  @retval EFI_INVALID_PARAMETER     A given pointer
> > is NULL or the
> > > > CommBufferSize is zero.
> > > > +  @retval EFI_NOT_FOUND             The
> > > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not
> > found.
> > > > +  @retval EFI_OUT_OF_RESOURCES      A valid SMM
> > communicate buffer
> > > for
> > > > the requested size is not available.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +GetCommunicateBuffer (
> > > > +  IN OUT  UINTN     *CommBufferSize,
> > > > +  OUT     UINT8     **CommBuffer
> > > > +  )
> > >
> > >
> > > Minor comment:
> > >
> > > I found that the consumers of the above function are:
> > > GetRuntimeCacheInfo()
> > > SendRuntimeVariableCacheContextToSmm()
> > >
> > > Both of them get called within SmmVariableReady() when
> > the SMM variable
> > > driver
> > > finished initialization. I am wondering if they can
> > simply use the pre-allocated
> > > comm buffer (via InitCommunicateBuffer() and using
> > 'mVariableBuffer'),
> > > instead
> > > of looking into the configuration table.
> > >
> > > In my opinion, this function can be dropped.
> > >
> > >
> >
> > I did that initially. It was recommended to use this
> > method.
> >
> > > > +{
> > > > +  EFI_STATUS                                Status;
> > > > +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> > > > *PiSmmCommunicationRegionTable;
> > > > +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> > > > +  UINTN
> > EntrySize;
> > > > +  UINT32                                    Index;
> > > > +
> > > > +  if (CommBuffer == NULL || CommBufferSize == NULL
> > ||
> > > > *CommBufferSize == 0) {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  }
> > > > +
> > > > +  Status = EfiGetSystemConfigurationTable (
> > > > +
> > &gEdkiiPiSmmCommunicationRegionTableGuid,
> > > > +             (VOID **)
> > &PiSmmCommunicationRegionTable
> > > > +             );
> > > > +  if (EFI_ERROR (Status) ||
> > PiSmmCommunicationRegionTable == NULL) {
> > > > +    return EFI_NOT_FOUND;
> > > > +  }
> > > > +
> > > > +  Entry = (EFI_MEMORY_DESCRIPTOR *)
> > > (PiSmmCommunicationRegionTable
> > > > + 1);
> > > > +  EntrySize = 0;
> > > > +  for (Index = 0; Index <
> > PiSmmCommunicationRegionTable-
> > > > >NumberOfEntries; Index++) {
> > > > +    if (Entry->Type == EfiConventionalMemory) {
> > > > +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry-
> > >NumberOfPages);
> > > > +      if (EntrySize >= *CommBufferSize) {
> > > > +        break;
> > > > +      }
> > > > +    }
> > > > +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *)
> > Entry +
> > > > PiSmmCommunicationRegionTable->DescriptorSize);
> > > > +  }
> > > > +
> > > > +  if (Index < PiSmmCommunicationRegionTable-
> > >NumberOfEntries) {
> > > > +    *CommBufferSize = EntrySize;
> > > > +    *CommBuffer = (UINT8 *) (UINTN) Entry-
> > >PhysicalStart;
> > > > +    return EFI_SUCCESS;
> > > > +  }
> > > > +
> > > > +  return EFI_OUT_OF_RESOURCES;
> > > > +}
> > > > +
> > > >  /**
> > > >    Send the data in communicate buffer to SMM.
> > > >
> > > > @@ -424,6 +568,171 @@ Done:
> > > >    return Status;
> > > >  }
> > > >
> > > > +/**
> > > > +  Signals SMM to synchronize any pending variable
> > updates with the
> > > > runtime cache(s).
> > > > +
> > > > +**/
> > > > +VOID
> > > > +EFIAPI
> > > > +SyncRuntimeCache (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  //
> > > > +  // Init the communicate buffer. The buffer data
> > size is:
> > > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> > > > +  //
> > > > +  InitCommunicateBuffer (NULL, 0,
> > > > SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> > > > +
> > > > +  //
> > > > +  // Send data to SMM.
> > > > +  //
> > > > +  SendCommunicateBuffer (0);
> > > > +}
> > > > +
> > > > +/**
> > > > +  Check whether a SMI must be triggered to retrieve
> > pending cache
> > > updates.
> > > > +
> > > > +  If the variable HOB was finished being flushed
> > since the last check for a
> > > > runtime cache update, this function
> > > > +  will prevent the HOB cache from being used for
> > future runtime cache
> > > hits.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +EFIAPI
> > > > +CheckForRuntimeCacheSync (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  if (mVariableRuntimeCachePendingUpdate) {
> > > > +    SyncRuntimeCache ();
> > > > +  }
> > > > +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> > > > +
> > > > +  //
> > > > +  // The HOB variable data may have finished being
> > flushed in the runtime
> > > > cache sync update
> > > > +  //
> > > > +  if (mHobFlushComplete &&
> > mVariableRuntimeHobCacheBuffer != NULL)
> > > {
> > > > +    if (!AtRuntime ()) {
> > > > +      FreePool (mVariableRuntimeHobCacheBuffer);
> > > > +    }
> > > > +    mVariableRuntimeHobCacheBuffer = NULL;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code finds variable in a volatile memory
> > store.
> > > > +
> > > > +  Caution: This function may receive untrusted
> > input.
> > > > +  The data size is external input, so this function
> > will validate it carefully to
> > > > avoid buffer overflow.
> > > > +
> > > > +  @param[in]      VariableName       Name of
> > Variable to be found.
> > > > +  @param[in]      VendorGuid         Variable
> > vendor GUID.
> > > > +  @param[out]     Attributes         Attribute
> > value of the variable found.
> > > > +  @param[in, out] DataSize           Size of Data
> > found. If size is less than the
> > > > +                                     data, this
> > value contains the required size.
> > > > +  @param[out]     Data               Data pointer.
> > > > +
> > > > +  @retval EFI_SUCCESS                Found the
> > specified variable.
> > > > +  @retval EFI_INVALID_PARAMETER      Invalid
> > parameter.
> > > > +  @retval EFI_NOT_FOUND              The specified
> > variable could not be
> > > found.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +FindVariableInRuntimeCache (
> > > > +  IN      CHAR16
> > *VariableName,
> > > > +  IN      EFI_GUID
> > *VendorGuid,
> > > > +  OUT     UINT32
> > *Attributes OPTIONAL,
> > > > +  IN OUT  UINTN
> > *DataSize,
> > > > +  OUT     VOID                              *Data
> > OPTIONAL
> > > > +  )
> > > > +{
> > > > +  EFI_STATUS              Status;
> > > > +  UINTN                   DelayIndex;
> > > > +  UINTN                   TempDataSize;
> > > > +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> > > > +  VARIABLE_STORE_TYPE     StoreType;
> > > > +  VARIABLE_STORE_HEADER
> > *VariableStoreList[VariableStoreTypeMax];
> > > > +
> > > > +  Status = EFI_NOT_FOUND;
> > > > +
> > > > +  if (VariableName == NULL || VendorGuid == NULL ||
> > DataSize == NULL) {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  }
> > > > +
> > > > +  for (DelayIndex = 0;
> > mVariableRuntimeCacheReadLock && DelayIndex <
> > > > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> > > > +    MicroSecondDelay (10);
> > > > +  }
> > > > +  if (DelayIndex <
> > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> > > > +    ASSERT (!mVariableRuntimeCacheReadLock);
> > > > +
> > > > +    mVariableRuntimeCacheReadLock = TRUE;
> > > > +    CheckForRuntimeCacheSync ();
> > > > +
> > > > +    if (!mVariableRuntimeCachePendingUpdate) {
> > > > +      //
> > > > +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > > +      // The index and attributes mapping must be
> > kept in this order as
> > > > FindVariable
> > > > +      // makes use of this mapping to implement
> > search algorithm.
> > > > +      //
> > > > +      VariableStoreList[VariableStoreTypeVolatile]
> > =
> > > > mVariableRuntimeVolatileCacheBuffer;
> > > > +      VariableStoreList[VariableStoreTypeHob]
> > =
> > > > mVariableRuntimeHobCacheBuffer;
> > > > +      VariableStoreList[VariableStoreTypeNv]
> > =
> > > > mVariableRuntimeNvCacheBuffer;
> > > > +
> > > > +      for (StoreType = (VARIABLE_STORE_TYPE) 0;
> > StoreType <
> > > > VariableStoreTypeMax; StoreType++) {
> > > > +        if (VariableStoreList[StoreType] == NULL) {
> > > > +          continue;
> > > > +        }
> > > > +
> > > > +        RtPtrTrack.StartPtr = GetStartPointer
> > (VariableStoreList[StoreType]);
> > > > +        RtPtrTrack.EndPtr   = GetEndPointer
> > (VariableStoreList[StoreType]);
> > > > +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType
> > ==
> > > > VariableStoreTypeVolatile);
> > > > +
> > > > +        Status = FindVariableEx (VariableName,
> > VendorGuid, FALSE,
> > > > &RtPtrTrack);
> > > > +        if (!EFI_ERROR (Status)) {
> > > > +          break;
> > > > +        }
> > > > +      }
> > > > +
> > > > +      if (!EFI_ERROR (Status)) {
> > > > +        //
> > > > +        // Get data size
> > > > +        //
> > > > +        TempDataSize = DataSizeOfVariable
> > (RtPtrTrack.CurrPtr);
> > > > +        ASSERT (TempDataSize != 0);
> > > > +
> > > > +        if (*DataSize >= TempDataSize) {
> > > > +          if (Data == NULL) {
> > > > +            Status = EFI_INVALID_PARAMETER;
> > > > +            goto Done;
> > > > +          }
> > > > +
> > > > +          CopyMem (Data, GetVariableDataPtr
> > (RtPtrTrack.CurrPtr),
> > > > TempDataSize);
> > > > +          if (Attributes != NULL) {
> > > > +            *Attributes = RtPtrTrack.CurrPtr-
> > >Attributes;
> > > > +          }
> > > > +
> > > > +          *DataSize = TempDataSize;
> > > > +
> > > > +          UpdateVariableInfo (VariableName,
> > VendorGuid,
> > > RtPtrTrack.Volatile,
> > > > TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> > > > +
> > > > +          Status = EFI_SUCCESS;
> > > > +          goto Done;
> > > > +        } else {
> > > > +          *DataSize = TempDataSize;
> > > > +          Status = EFI_BUFFER_TOO_SMALL;
> > > > +          goto Done;
> > > > +        }
> > > > +      }
> > > > +    }
> > > > +  }
> > > > +
> > > > +Done:
> > > > +  mVariableRuntimeCacheReadLock = FALSE;
> > >
> > >
> > > If timeout occurs when acquiring the read lock, should
> > this flag be set to
> > > FALSE
> > > in such case?
> > >
> >
> > Please see reply to patch #8.
> >
> > > Best Regards,
> > > Hao Wu
> > >
> > >
> > > > +
> > > > +  return Status;
> > > > +}
> > > > +
> > > >  /**
> > > >    This code finds variable in storage blocks
> > (Volatile or Non-Volatile).
> > > >
> > > > @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
> > > >    )
> > > >  {
> > > >    EFI_STATUS                                Status;
> > > > -  UINTN
> > PayloadSize;
> > > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > > *SmmVariableHeader;
> > > > -  UINTN
> > TempDataSize;
> > > > -  UINTN
> > VariableNameSize;
> > > >
> > > >    if (VariableName == NULL || VendorGuid == NULL ||
> > DataSize == NULL) {
> > > >      return EFI_INVALID_PARAMETER;
> > > >    }
> > > > -
> > > > -  TempDataSize          = *DataSize;
> > > > -  VariableNameSize      = StrSize (VariableName);
> > > > -  SmmVariableHeader     = NULL;
> > > > -
> > > > -  //
> > > > -  // If VariableName exceeds SMM payload limit.
> > Return failure
> > > > -  //
> > > > -  if (VariableNameSize > mVariableBufferPayloadSize
> > - OFFSET_OF
> > > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> > > > -    return EFI_INVALID_PARAMETER;
> > > > -  }
> > > > -
> > > > -
> > AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> > > > -
> > > > -  //
> > > > -  // Init the communicate buffer. The buffer data
> > size is:
> > > > -  // SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> > > > -  //
> > > > -  if (TempDataSize > mVariableBufferPayloadSize -
> > OFFSET_OF
> > > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > > VariableNameSize) {
> > > > -    //
> > > > -    // If output data buffer exceed SMM payload
> > limit. Trim output buffer to
> > > > SMM payload size
> > > > -    //
> > > > -    TempDataSize = mVariableBufferPayloadSize -
> > OFFSET_OF
> > > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > > VariableNameSize;
> > > > -  }
> > > > -  PayloadSize = OFFSET_OF
> > > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> > > > VariableNameSize + TempDataSize;
> > > > -
> > > > -  Status = InitCommunicateBuffer ((VOID
> > **)&SmmVariableHeader,
> > > > PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> > > > -  if (EFI_ERROR (Status)) {
> > > > -    goto Done;
> > > > -  }
> > > > -  ASSERT (SmmVariableHeader != NULL);
> > > > -
> > > > -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> > > > -  SmmVariableHeader->DataSize   = TempDataSize;
> > > > -  SmmVariableHeader->NameSize   = VariableNameSize;
> > > > -  if (Attributes == NULL) {
> > > > -    SmmVariableHeader->Attributes = 0;
> > > > -  } else {
> > > > -    SmmVariableHeader->Attributes = *Attributes;
> > > > -  }
> > > > -  CopyMem (SmmVariableHeader->Name, VariableName,
> > > > SmmVariableHeader->NameSize);
> > > > -
> > > > -  //
> > > > -  // Send data to SMM.
> > > > -  //
> > > > -  Status = SendCommunicateBuffer (PayloadSize);
> > > > -
> > > > -  //
> > > > -  // Get data from SMM.
> > > > -  //
> > > > -  if (Status == EFI_SUCCESS || Status ==
> > EFI_BUFFER_TOO_SMALL) {
> > > > -    //
> > > > -    // SMM CommBuffer DataSize can be a trimed
> > value
> > > > -    // Only update DataSize when needed
> > > > -    //
> > > > -    *DataSize = SmmVariableHeader->DataSize;
> > > > -  }
> > > > -  if (Attributes != NULL) {
> > > > -    *Attributes = SmmVariableHeader->Attributes;
> > > > -  }
> > > > -
> > > > -  if (EFI_ERROR (Status)) {
> > > > -    goto Done;
> > > > -  }
> > > > -
> > > > -  if (Data != NULL) {
> > > > -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name
> > +
> > > > SmmVariableHeader->NameSize, SmmVariableHeader-
> > >DataSize);
> > > > -  } else {
> > > > -    Status = EFI_INVALID_PARAMETER;
> > > > +  if (VariableName[0] == 0) {
> > > > +    return EFI_NOT_FOUND;
> > > >    }
> > > >
> > > > -Done:
> > > > +  AcquireLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +  Status =  FindVariableInRuntimeCache
> > (VariableName, VendorGuid,
> > > > Attributes, DataSize, Data);
> > > >    ReleaseLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +
> > > >    return Status;
> > > >  }
> > > >
> > > > -
> > > >  /**
> > > >    This code Finds the Next available variable.
> > > >
> > > > @@ -870,6 +1109,17 @@ OnReadyToBoot (
> > > >    //
> > > >    SendCommunicateBuffer (0);
> > > >
> > > > +  //
> > > > +  // Install the system configuration table for
> > variable info data captured
> > > > +  //
> > > > +  if (FeaturePcdGet (PcdVariableCollectStatistics))
> > {
> > > > +    if (mVariableAuthFormat) {
> > > > +      gBS->InstallConfigurationTable
> > (&gEfiAuthenticatedVariableGuid,
> > > > mVariableInfo);
> > > > +    } else {
> > > > +      gBS->InstallConfigurationTable
> > (&gEfiVariableGuid, mVariableInfo);
> > > > +    }
> > > > +  }
> > > > +
> > > >    gBS->CloseEvent (Event);
> > > >  }
> > > >
> > > > @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
> > > >  {
> > > >    EfiConvertPointer (0x0, (VOID **)
> > &mVariableBuffer);
> > > >    EfiConvertPointer (0x0, (VOID **)
> > &mSmmCommunication);
> > > > +  EfiConvertPointer (0x0, (VOID **)
> > &mVariableRuntimeHobCacheBuffer);
> > > > +  EfiConvertPointer (0x0, (VOID **)
> > &mVariableRuntimeNvCacheBuffer);
> > > > +  EfiConvertPointer (0x0, (VOID **)
> > > > &mVariableRuntimeVolatileCacheBuffer);
> > > >  }
> > > >
> > > >  /**
> > > > @@ -969,6 +1222,173 @@ Done:
> > > >    return Status;
> > > >  }
> > > >
> > > > +/**
> > > > +  This code gets information needed from SMM for
> > runtime cache
> > > > initialization.
> > > > +
> > > > +  @param[out] TotalHobStorageSize         Output
> > pointer for the total HOB
> > > > storage size in bytes.
> > > > +  @param[out] TotalNvStorageSize          Output
> > pointer for the total non-
> > > > volatile storage size in bytes.
> > > > +  @param[out] TotalVolatileStorageSize    Output
> > pointer for the total
> > > > volatile storage size in bytes.
> > > > +  @param[out] AuthenticatedVariableUsage  Output
> > pointer that indicates
> > > if
> > > > authenticated variables are to be used.
> > > > +
> > > > +  @retval EFI_SUCCESS                     Retrieved
> > the size successfully.
> > > > +  @retval EFI_INVALID_PARAMETER
> > TotalNvStorageSize parameter is
> > > > NULL.
> > > > +  @retval EFI_OUT_OF_RESOURCES            Could not
> > allocate a
> > > CommBuffer.
> > > > +  @retval Others                          Could not
> > retrieve the size successfully.;
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +GetRuntimeCacheInfo (
> > > > +  OUT UINTN
> > *TotalHobStorageSize,
> > > > +  OUT UINTN
> > *TotalNvStorageSize,
> > > > +  OUT UINTN
> > *TotalVolatileStorageSize,
> > > > +  OUT BOOLEAN
> > *AuthenticatedVariableUsage
> > > > +  )
> > > > +{
> > > > +  EFI_STATUS
> > Status;
> > > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > > *SmmGetRuntimeCacheInfo;
> > > > +  EFI_SMM_COMMUNICATE_HEADER
> > > > *SmmCommunicateHeader;
> > > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > > *SmmVariableFunctionHeader;
> > > > +  UINTN
> > CommSize;
> > > > +  UINTN
> > CommBufferSize;
> > > > +  UINT8
> > *CommBuffer;
> > > > +
> > > > +  SmmGetRuntimeCacheInfo = NULL;
> > > > +  CommBuffer = NULL;
> > > > +
> > > > +  if (TotalHobStorageSize == NULL ||
> > TotalNvStorageSize == NULL ||
> > > > TotalVolatileStorageSize == NULL ||
> > AuthenticatedVariableUsage == NULL)
> > > {
> > > > +    return EFI_INVALID_PARAMETER;
> > > > +  }
> > > > +
> > > > +  AcquireLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +
> > > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > > +  CommBufferSize = CommSize;
> > > > +  Status = GetCommunicateBuffer (&CommBufferSize,
> > &CommBuffer);
> > > > +  if (EFI_ERROR (Status)) {
> > > > +    goto Done;
> > > > +  }
> > > > +  if (CommBuffer == NULL) {
> > > > +    Status = EFI_OUT_OF_RESOURCES;
> > > > +    goto Done;
> > > > +  }
> > > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > > +
> > > > +  SmmCommunicateHeader =
> > (EFI_SMM_COMMUNICATE_HEADER *)
> > > > CommBuffer;
> > > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > > &gEfiSmmVariableProtocolGuid);
> > > > +  SmmCommunicateHeader->MessageLength =
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > > +
> > > > +  SmmVariableFunctionHeader =
> > > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> > SmmCommunicateHeader-
> > > > >Data;
> > > > +  SmmVariableFunctionHeader->Function =
> > > > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> > > > +  SmmGetRuntimeCacheInfo =
> > > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > > SmmVariableFunctionHeader->Data;
> > > > +
> > > > +  //
> > > > +  // Send data to SMM.
> > > > +  //
> > > > +  Status = mSmmCommunication->Communicate
> > (mSmmCommunication,
> > > > CommBuffer, &CommSize);
> > > > +  ASSERT_EFI_ERROR (Status);
> > > > +  if (CommSize <=
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > > +    goto Done;
> > > > +  }
> > > > +
> > > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > > +  if (EFI_ERROR (Status)) {
> > > > +    goto Done;
> > > > +  }
> > > > +
> > > > +  //
> > > > +  // Get data from SMM.
> > > > +  //
> > > > +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
> > > >TotalHobStorageSize;
> > > > +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo-
> > >TotalNvStorageSize;
> > > > +  *TotalVolatileStorageSize =
> > SmmGetRuntimeCacheInfo-
> > > > >TotalVolatileStorageSize;
> > > > +  *AuthenticatedVariableUsage =
> > SmmGetRuntimeCacheInfo-
> > > > >AuthenticatedVariableUsage;
> > > > +
> > > > +Done:
> > > > +  ReleaseLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +  return Status;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sends the runtime variable cache context
> > information to SMM.
> > > > +
> > > > +  @retval EFI_SUCCESS               Retrieved the
> > size successfully.
> > > > +  @retval EFI_INVALID_PARAMETER
> > TotalNvStorageSize parameter is
> > > > NULL.
> > > > +  @retval EFI_OUT_OF_RESOURCES      Could not
> > allocate a CommBuffer.
> > > > +  @retval Others                    Could not
> > retrieve the size successfully.;
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +SendRuntimeVariableCacheContextToSmm (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  EFI_STATUS
> > Status;
> > > > +
> > > >
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > > *SmmRuntimeVarCacheContext;
> > > > +  EFI_SMM_COMMUNICATE_HEADER
> > > > *SmmCommunicateHeader;
> > > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > > *SmmVariableFunctionHeader;
> > > > +  UINTN
> > CommSize;
> > > > +  UINTN
> > CommBufferSize;
> > > > +  UINT8
> > *CommBuffer;
> > > > +
> > > > +  SmmRuntimeVarCacheContext = NULL;
> > > > +  CommBuffer = NULL;
> > > > +
> > > > +  AcquireLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +
> > > > +  //
> > > > +  // Init the communicate buffer. The buffer data
> > size is:
> > > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > >
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > )
> > > ;
> > > > +  //
> > > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > >
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > )
> > > ;
> > > > +  CommBufferSize = CommSize;
> > > > +  Status = GetCommunicateBuffer (&CommBufferSize,
> > &CommBuffer);
> > > > +  if (EFI_ERROR (Status)) {
> > > > +    goto Done;
> > > > +  }
> > > > +  if (CommBuffer == NULL) {
> > > > +    Status = EFI_OUT_OF_RESOURCES;
> > > > +    goto Done;
> > > > +  }
> > > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > > +
> > > > +  SmmCommunicateHeader =
> > (EFI_SMM_COMMUNICATE_HEADER *)
> > > > CommBuffer;
> > > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > > &gEfiSmmVariableProtocolGuid);
> > > > +  SmmCommunicateHeader->MessageLength =
> > > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > >
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > )
> > > ;
> > > > +
> > > > +  SmmVariableFunctionHeader =
> > > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> > SmmCommunicateHeader-
> > > > >Data;
> > > > +  SmmVariableFunctionHeader->Function =
> > > >
> > >
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> > T;
> > > > +  SmmRuntimeVarCacheContext =
> > > >
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > > *) SmmVariableFunctionHeader->Data;
> > > > +
> > > > +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> > > > mVariableRuntimeHobCacheBuffer;
> > > > +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> > > > mVariableRuntimeVolatileCacheBuffer;
> > > > +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> > > > mVariableRuntimeNvCacheBuffer;
> > > > +  SmmRuntimeVarCacheContext->PendingUpdate =
> > > > &mVariableRuntimeCachePendingUpdate;
> > > > +  SmmRuntimeVarCacheContext->ReadLock =
> > > > &mVariableRuntimeCacheReadLock;
> > > > +  SmmRuntimeVarCacheContext->HobFlushComplete =
> > > > &mHobFlushComplete;
> > > > +
> > > > +  //
> > > > +  // Send data to SMM.
> > > > +  //
> > > > +  Status = mSmmCommunication->Communicate
> > (mSmmCommunication,
> > > > CommBuffer, &CommSize);
> > > > +  ASSERT_EFI_ERROR (Status);
> > > > +  if (CommSize <=
> > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > > +    goto Done;
> > > > +  }
> > > > +
> > > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > > +  if (EFI_ERROR (Status)) {
> > > > +    goto Done;
> > > > +  }
> > > > +
> > > > +Done:
> > > > +  ReleaseLockOnlyAtBootTime
> > (&mVariableServicesLock);
> > > > +  return Status;
> > > > +}
> > > > +
> > > >  /**
> > > >    Initialize variable service and install Variable
> > Architectural protocol.
> > > >
> > > > @@ -985,7 +1405,7 @@ SmmVariableReady (
> > > >  {
> > > >    EFI_STATUS                                Status;
> > > >
> > > > -  Status = gBS->LocateProtocol
> > (&gEfiSmmVariableProtocolGuid, NULL,
> > > > (VOID **)&mSmmVariable);
> > > > +  Status = gBS->LocateProtocol
> > (&gEfiSmmVariableProtocolGuid, NULL,
> > > > (VOID **) &mSmmVariable);
> > > >    if (EFI_ERROR (Status)) {
> > > >      return;
> > > >    }
> > > > @@ -1007,6 +1427,40 @@ SmmVariableReady (
> > > >    //
> > > >    mVariableBufferPhysical = mVariableBuffer;
> > > >
> > > > +  //
> > > > +  // Allocate runtime variable cache memory
> > buffers.
> > > > +  //
> > > > +  Status =  GetRuntimeCacheInfo (
> > > > +              &mVariableRuntimeHobCacheBufferSize,
> > > > +              &mVariableRuntimeNvCacheBufferSize,
> > > > +
> > &mVariableRuntimeVolatileCacheBufferSize,
> > > > +              &mVariableAuthFormat
> > > > +              );
> > > > +  if (!EFI_ERROR (Status)) {
> > > > +    Status = InitVariableCache
> > (&mVariableRuntimeHobCacheBuffer,
> > > > &mVariableRuntimeHobCacheBufferSize);
> > > > +    if (!EFI_ERROR (Status)) {
> > > > +      Status = InitVariableCache
> > (&mVariableRuntimeNvCacheBuffer,
> > > > &mVariableRuntimeNvCacheBufferSize);
> > > > +      if (!EFI_ERROR (Status)) {
> > > > +        Status = InitVariableCache
> > (&mVariableRuntimeVolatileCacheBuffer,
> > > > &mVariableRuntimeVolatileCacheBufferSize);
> > > > +        if (!EFI_ERROR (Status)) {
> > > > +          Status = InitVariableParsing
> > (mVariableAuthFormat);
> > > > +          ASSERT_EFI_ERROR (Status);
> > > > +
> > > > +          Status =
> > SendRuntimeVariableCacheContextToSmm ();
> > > > +          if (!EFI_ERROR (Status)) {
> > > > +            SyncRuntimeCache ();
> > > > +          }
> > > > +        }
> > > > +      }
> > > > +    }
> > > > +    if (EFI_ERROR (Status)) {
> > > > +      mVariableRuntimeHobCacheBuffer = NULL;
> > > > +      mVariableRuntimeNvCacheBuffer = NULL;
> > > > +      mVariableRuntimeVolatileCacheBuffer = NULL;
> > > > +    }
> > > > +  }
> > > > +  ASSERT_EFI_ERROR (Status);
> > > > +
> > > >    gRT->GetVariable         =
> > RuntimeServiceGetVariable;
> > > >    gRT->GetNextVariableName =
> > RuntimeServiceGetNextVariableName;
> > > >    gRT->SetVariable         =
> > RuntimeServiceSetVariable;
> > > > --
> > > > 2.16.2.windows.1
> > >
> >
> 


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 21:53     ` Kubacki, Michael A
  2019-10-03 22:01       ` Michael D Kinney
@ 2019-10-04  6:38       ` Laszlo Ersek
  2019-10-04 16:48         ` Kubacki, Michael A
  2019-10-08  2:12       ` Wu, Hao A
  2 siblings, 1 reply; 45+ messages in thread
From: Laszlo Ersek @ 2019-10-04  6:38 UTC (permalink / raw)
  To: Kubacki, Michael A, Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen,
	Andrew Fish

On 10/03/19 23:53, Kubacki, Michael A wrote:
> #1 - The plan is to remove the polling entirely in V3.
> 
> #2 - I'd prefer to take a definitive direction and reduce validation and maintenance
>         effort but you and Laszlo both requested this so I'll add a FeaturePCD to control
>         activation of the runtime cache in this patch series. Perhaps this can be removed
>         in the future.

Thanks!

(I'm also happy with the lock / timeout resolution. I had known about
the reentrancy restriction in the UEFI spec (I happened to look at
something in the kernel just the other day that reminded me of that part
of the spec), but it wasn't clear to me that the lock + timeout in the
patch series were intended to protect against just that. Kudos to Andrew
for the comment!)

(

Meanwhile, I've received help from my colleagues wrt.
QueryVariableInfo(), but right now it's too early to tell if we'll be
able to settle on that in the long term:

[PATCH] efi/efi_test: require CAP_SYS_ADMIN to open the chardev
http://mid.mail-archive.com/20191003100712.31045-1-javierm@redhat.com

)

For the next version of this edk2 patch set (where you plan to include
the new FeaturePCD, if I understand correctly), I'd like to request the
following: either set the DEC default to FALSE please, or please include
a patch for OvmfPkg where you set the PCD to FALSE in all the OvmfPkg
DSC files.

I think the next stable release should be made like that. Then, for the
stable release following that, we can re-evaluate the question, and
might decide to invert the PCD in OVMF (enabling the feature), assuming
QueryVariableInfo() proves usable in Fedora, by then.


Two independent questions:

- Has this work been regression-tested on ARM / AARCH64? (For example,
ArmVirtPkg platforms use the unified runtime DXE driver, not the split
runtime/SMM drivers. So no change in behavior is expected; we should
test that.)

In the "Testing Performed" section of your blurb, item#3 suggests
something similar ("Boot from S5 to EFI shell with DXE variables
enabled"), but I figured I'd raise AARCH64 specifically.


- Can you please confirm that the handling of *volatile* variables is
not affected? ArmVirtPkg and OvmfPkg use quite different sizes for
volatile and non-volatile variables; see:

  - 9c7d0d499296 ("OvmfPkg/TlsAuthConfigLib: configure trusted CA certs
                  for HTTPS boot", 2018-03-30)
  - ffe048a0807b ("ArmVirtPkg: handle NETWORK_TLS_ENABLE in
                  ArmVirtQemu*", 2019-06-28)

Thank you!
Laszlo

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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 23:31         ` Kubacki, Michael A
@ 2019-10-04  6:50           ` Laszlo Ersek
  2019-10-04 16:48             ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Laszlo Ersek @ 2019-10-04  6:50 UTC (permalink / raw)
  To: Kubacki, Michael A, Kinney, Michael D, Wu, Hao A,
	devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming, Ni, Ray,
	Wang, Jian J, Yao, Jiewen

On 10/04/19 01:31, Kubacki, Michael A wrote:
> I agree, I will make the default to enable the runtime cache.

I've just made a request for the opposite :) , before reading this part
of the thread.

Let me revise my request then, seeing the above preference. From the
following three options:

(1) set DEC default to FALSE,

(2) set the DEC default to TRUE, but set the PCD in OvmfPkg DSC files to
FALSE,

(3) set the DEC default to TRUE, and inherit it in OvmfPkg,

I'd ask for (2) in the short or mid term, and entertain (3) in the long
term (dependent on the upstream kernel patch I linked elsewhere in the
thread:
<http://mid.mail-archive.com/20191003100712.31045-1-javierm@redhat.com>).


With the PCD available in the DEC file, I agree that downstream OVMF
packages could easily be built with the PCD set to FALSE, e.g. on the
"build" command line, regardless of the upstream OvmfPkg DSC setting.
Given that, option (2) might appear superfluous.

However, I'd like upstream OvmfPkg's DSC setting to reflect that as yet,
there is no alternative to GetVariable(), for exercising the SMM stack
built into OVMF without side effects to the variable store. Once the
kernel patch is merged and QueryVariableInfo() becomes *recommendable*
as a practical substitute for GetVariable(), we can switch upstream
OvmfPkg from option (2) to option (3).

Does that sound acceptable?

Thanks!
Laszlo


> 
>> -----Original Message-----
>> From: Kinney, Michael D <michael.d.kinney@intel.com>
>> Sent: Thursday, October 3, 2019 3:01 PM
>> To: Kubacki, Michael A <michael.a.kubacki@intel.com>; Wu, Hao A
>> <hao.a.wu@intel.com>; devel@edk2.groups.io; Kinney, Michael D
>> <michael.d.kinney@intel.com>
>> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
>> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
>> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Ni, Ray
>> <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; Yao, Jiewen
>> <jiewen.yao@intel.com>
>> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
>> cache support
>>
>> Michael,
>>
>> Perhaps the FeaturePCD for #2 should be enabled by default
>> so the platform DSC only needs to set this PCD for some
>> validation tests.
>>
>> Mike
>>
>>
>>> -----Original Message-----
>>> From: Kubacki, Michael A <michael.a.kubacki@intel.com>
>>> Sent: Thursday, October 3, 2019 2:54 PM
>>> To: Wu, Hao A <hao.a.wu@intel.com>; devel@edk2.groups.io
>>> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
>>> <ard.biesheuvel@linaro.org>; Dong, Eric
>>> <eric.dong@intel.com>; Laszlo Ersek <lersek@redhat.com>;
>>> Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
>>> <michael.d.kinney@intel.com>; Ni, Ray
>>> <ray.ni@intel.com>; Wang, Jian J
>>> <jian.j.wang@intel.com>; Yao, Jiewen
>>> <jiewen.yao@intel.com>
>>> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
>>> RT GetVariable() cache support
>>>
>>> #1 - The plan is to remove the polling entirely in V3.
>>>
>>> #2 - I'd prefer to take a definitive direction and
>>> reduce validation and maintenance
>>>         effort but you and Laszlo both requested this so
>>> I'll add a FeaturePCD to control
>>>         activation of the runtime cache in this patch
>>> series. Perhaps this can be removed
>>>         in the future.
>>>
>>> #3 - Will be done in V3.
>>>
>>> Other replies are inline.
>>>
>>>> -----Original Message-----
>>>> From: Wu, Hao A <hao.a.wu@intel.com>
>>>> Sent: Thursday, October 3, 2019 1:05 AM
>>>> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
>>>> devel@edk2.groups.io
>>>> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
>>>> <ard.biesheuvel@linaro.org>; Dong, Eric
>>> <eric.dong@intel.com>; Laszlo Ersek
>>>> <lersek@redhat.com>; Gao, Liming
>>> <liming.gao@intel.com>; Kinney, Michael
>>>> D <michael.d.kinney@intel.com>; Ni, Ray
>>> <ray.ni@intel.com>; Wang, Jian J
>>>> <jian.j.wang@intel.com>; Yao, Jiewen
>>> <jiewen.yao@intel.com>
>>>> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
>>> RT GetVariable()
>>>> cache support
>>>>
>>>> Before any comment on the patch, since I am not
>>> experienced in the
>>>> Variable
>>>> driver, I would like to ask for help from other
>>> reviewers to look into this
>>>> patch and provide feedbacks as well. Thanks in
>>> advance.
>>>>
>>>> With the above fact, some comments provided below
>>> maybe wrong. So
>>>> please help
>>>> to kindly correct me.
>>>>
>>>>
>>>> Some general comments:
>>>> 1. I am not sure if bringing the TimerLib dependency
>>> (delay in acquiring the
>>>>    runtime cache read lock) to variable driver (a
>>> software driver for the most
>>>>    part) is a good idea.
>>>>
>>>>    Hope other reviewers can provide some feedbacks for
>>> this. Thanks in
>>>> advance.
>>>>
>>>> 2. In my opinion, I prefer a switch can be provided
>>> for platform owners to
>>>>    choose between using the runtime cache and going
>>> through SMM for
>>>> GetVariable
>>>>    (and for GetNextVariableName in the next patch as
>>> well).
>>>>
>>>>    If platform owners feel uncomfortable with using
>>> the runtime cache with
>>>>    regard to the security perspective, they can switch
>>> to the origin solution.
>>>>
>>>> 3. Please help to remove the 'EFIAPI' keyword for new
>>> driver internal
>>>> functions;
>>>>
>>>>
>>>> Inline comments below:
>>>>
>>>>
>>>>> -----Original Message-----
>>>>> From: Kubacki, Michael A
>>>>> Sent: Saturday, September 28, 2019 9:47 AM
>>>>> To: devel@edk2.groups.io
>>>>> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo
>>> Ersek; Gao, Liming;
>>>> Kinney,
>>>>> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
>>> Jiewen
>>>>> Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add
>>> RT GetVariable()
>>>>> cache support
>>>>>
>>>>>
>>> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
>>>>>
>>>>> This change reduces SMIs for GetVariable () by
>>> maintaining a
>>>>> UEFI variable cache in Runtime DXE in addition to
>>> the pre-
>>>>> existing cache in SMRAM. When the Runtime Service
>>> GetVariable()
>>>>> is invoked, a Runtime DXE cache is used instead of
>>> triggering an
>>>>> SMI to VariableSmm. This can improve overall system
>>> performance
>>>>> by servicing variable read requests without
>>> rendezvousing all
>>>>> cores into SMM.
>>>>>
>>>>> The following are important points regarding this
>>> change.
>>>>>
>>>>> 1. All of the non-volatile storage contents are
>>> loaded into the
>>>>>    cache upon driver load. This one time load
>>> operation from storage
>>>>>    is preferred as opposed to building the cache on
>>> demand. An on-
>>>>>    demand cache would require a fallback SMI to load
>>> data into the
>>>>>    cache as variables are requested.
>>>>>
>>>>> 2. SetVariable () requests will continue to always
>>> trigger an SMI.
>>>>>    This occurs regardless of whether the variable is
>>> volatile or
>>>>>    non-volatile.
>>>>>
>>>>> 3. Both volatile and non-volatile variables are
>>> cached in a runtime
>>>>>    buffer. As is the case in the current EDK II
>>> variable driver, they
>>>>>    continue to be cached in separate buffers.
>>>>>
>>>>> 4. The cache in Runtime DXE and SMM are intended to
>>> be exact copies
>>>>>    of one another. All SMM variable accesses only
>>> return data from the
>>>>>    SMM cache. The runtime caches are only updated
>>> after the variable I/O
>>>>>    operation is successful in SMM. The runtime
>>> caches are only updated
>>>>>    from SMM.
>>>>>
>>>>> 5. Synchronization mechanisms are in place to ensure
>>> the runtime cache
>>>>>    content integrity with the SMM cache. These may
>>> result in updates to
>>>>>    runtime cache that are the same in content but
>>> different in offset and
>>>>>    size from updates to the SMM cache.
>>>>>
>>>>> When using SMM variables, two caches will now be
>>> present.
>>>>> 1. "Runtime Cache" - Maintained in
>>> VariableSmmRuntimeDxe. Used to
>>>>> service
>>>>>    Runtime Services GetVariable () and
>>> GetNextVariableName () callers.
>>>>> 2. "SMM Cache" - Maintained in VariableSmm to
>>> service SMM GetVariable
>>>> ()
>>>>>    and GetNextVariableName () callers.
>>>>>    a. This cache is retained so SMM modules do not
>>> operate on data outside
>>>>>       SMRAM.
>>>>>
>>>>> It is possible to view UEFI variable read and write
>>> statistics by setting
>>>>> the
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
>>> ics
>>>>> FeaturePcd
>>>>> to TRUE and using the VariableInfo UEFI application
>>> in MdeModulePkg to
>>>>> dump
>>>>> variable statistics to the console. By doing so, a
>>> user can view the number
>>>>> of GetVariable () hits from the Runtime DXE variable
>>> driver (Runtime Cache
>>>>> hits) and the SMM variable driver (SMM Cache hits).
>>> SMM Cache hits for
>>>>> GetVariable () will occur when SMM modules invoke
>>> GetVariable ().
>>>>>
>>>>> Cc: Dandan Bi <dandan.bi@intel.com>
>>>>> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>>>>> Cc: Eric Dong <eric.dong@intel.com>
>>>>> Cc: Laszlo Ersek <lersek@redhat.com>
>>>>> Cc: Liming Gao <liming.gao@intel.com>
>>>>> Cc: Michael D Kinney <michael.d.kinney@intel.com>
>>>>> Cc: Ray Ni <ray.ni@intel.com>
>>>>> Cc: Jian J Wang <jian.j.wang@intel.com>
>>>>> Cc: Hao A Wu <hao.a.wu@intel.com>
>>>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
>>>>> Signed-off-by: Michael Kubacki
>>> <michael.a.kubacki@intel.com>
>>>>> ---
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
>>> meDxe.inf
>>>>> |   2 +
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.i
>>> nf           |
>>>> 2
>>>>> +
>>>>>
>>>>>
>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
>>> ntimeDxe.i
>>>>> nf |  31 +-
>>>>>
>>>>>
>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStand
>>> aloneMm.inf
>>>>> |   2 +
>>>>>  MdeModulePkg/Include/Guid/SmmVariableCommon.h
>>> |  29 +-
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
>>> |  39
>>>> +-
>>>>>
>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
>>> meCache.h
>>>>> |  47 ++
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
>>> |  44
>>>> +-
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
>>> meCache.c
>>>>> | 153 +++++
>>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
>>> |
>>>> 114
>>>>> +++-
>>>>>
>>>>>
>>>>
>>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
>>> ntimeDxe.
>>>>> c   | 608 +++++++++++++++++---
>>>>>  11 files changed, 966 insertions(+), 105
>>> deletions(-)
>>>>>
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeDxe.inf
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeDxe.inf
>>>>> index 08a5490787..ceea5d1ff9 100644
>>>>> ---
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeDxe.inf
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeDxe.inf
>>>>> @@ -40,6 +40,8 @@
>>>>>    VariableNonVolatile.h
>>>>>    VariableParsing.c
>>>>>    VariableParsing.h
>>>>> +  VariableRuntimeCache.c
>>>>> +  VariableRuntimeCache.h
>>>>
>>>>
>>>> Per my understanding, the module specified by
>>> VariableRuntimeDxe.inf
>>>> does not
>>>> involve SMM/SMI for variable services (like
>>> GetVariable). It looks weird to
>>>> me
>>>> for this INF to include the newly introduced runtime
>>> cache codes (below
>>>> source
>>>> header files):
>>>>
>>>> VariableRuntimeCache.c
>>>> VariableRuntimeCache.h
>>>>
>>>>
>>>
>>> This is because Variable.c is common to the runtime DXE
>>> and SMM variable
>>> driver and it contains the code to update variable
>>> caches. The runtime cache
>>> synchronization function
>>> (SynchronizeRuntimeVariableCache ()) will return
>>> if the runtime cache pointer is NULL.
>>>
>>>>>    PrivilegePolymorphic.h
>>>>>    Measurement.c
>>>>>    TcgMorLockDxe.c
>>>>> diff --git
>>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .inf
>>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .inf
>>>>> index 6dc2721b81..bc3033588d 100644
>>>>> ---
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .inf
>>>>> +++
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .inf
>>>>> @@ -49,6 +49,8 @@
>>>>>    VariableNonVolatile.h
>>>>>    VariableParsing.c
>>>>>    VariableParsing.h
>>>>> +  VariableRuntimeCache.c
>>>>> +  VariableRuntimeCache.h
>>>>>    VarCheck.c
>>>>>    Variable.h
>>>>>    PrivilegePolymorphic.h
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.inf
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.inf
>>>>> index 14894e6f13..70837ac6e0 100644
>>>>> ---
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.inf
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.inf
>>>>> @@ -13,7 +13,7 @@
>>>>>  #  may not be modified without authorization. If
>>> platform fails to protect
>>>>> these resources,
>>>>>  #  the authentication service provided in this
>>> driver will be broken, and the
>>>>> behavior is undefined.
>>>>>  #
>>>>> -# Copyright (c) 2010 - 2017, Intel Corporation. All
>>> rights reserved.<BR>
>>>>> +# Copyright (c) 2010 - 2019, Intel Corporation. All
>>> rights reserved.<BR>
>>>>>  # SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>  #
>>>>>  ##
>>>>> @@ -39,6 +39,10 @@
>>>>>    VariableSmmRuntimeDxe.c
>>>>>    PrivilegePolymorphic.h
>>>>>    Measurement.c
>>>>> +  VariableParsing.c
>>>>> +  VariableParsing.h
>>>>> +  VariableRuntimeCache.c
>>>>> +  VariableRuntimeCache.h
>>>>>
>>>>>  [Packages]
>>>>>    MdePkg/MdePkg.dec
>>>>> @@ -49,6 +53,7 @@
>>>>>    BaseLib
>>>>>    UefiBootServicesTableLib
>>>>>    DebugLib
>>>>> +  TimerLib
>>>>>    UefiRuntimeLib
>>>>>    DxeServicesTableLib
>>>>>    UefiDriverEntryPoint
>>>>> @@ -65,7 +70,29 @@
>>>>>    gEdkiiVariableLockProtocolGuid                ##
>>> PRODUCES
>>>>>    gEdkiiVarCheckProtocolGuid                    ##
>>> PRODUCES
>>>>>
>>>>> +[Pcd]
>>>>> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize
>>> ##
>>>>> CONSUMES
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
>>>> ##
>>>>> CONSUMES
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariab
>>> leSize
>>>>> ## CONSUMES
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize
>>> ##
>>>>> CONSUMES
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize
>>> ##
>>>>> CONSUMES
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpace
>>> Size
>>>>> ## CONSUMES
>>>>> +
>>>>>
>>>>
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVari
>>> ableSpace
>>>>> Size  ## CONSUMES
>>>>
>>>>
>>>> Not sure if the above PCDs are really needed by
>>> VariableSmmRuntimeDxe.
>>>>
>>>>
>>>
>>> I will double check and remove any not required.
>>>
>>>>> +
>>>>> +[FeaturePcd]
>>>>> +
>>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
>>> ics
>>>> ##
>>>>> CONSUMES
>>>>> +
>>>>>  [Guids]
>>>>> +  ## PRODUCES             ## GUID # Signature of
>>> Variable store header
>>>>> +  ## CONSUMES             ## GUID # Signature of
>>> Variable store header
>>>>> +  ## SOMETIMES_PRODUCES   ## SystemTable
>>>>> +  gEfiAuthenticatedVariableGuid
>>>>> +
>>>>> +  ## PRODUCES             ## GUID # Signature of
>>> Variable store header
>>>>> +  ## CONSUMES             ## GUID # Signature of
>>> Variable store header
>>>>> +  ## SOMETIMES_PRODUCES   ## SystemTable
>>>>> +  gEfiVariableGuid
>>>>> +
>>>>>    gEfiEventVirtualAddressChangeGuid             ##
>>> CONSUMES ## Event
>>>>>    gEfiEventExitBootServicesGuid                 ##
>>> CONSUMES ## Event
>>>>>    ## CONSUMES ## GUID # Locate protocol
>>>>> @@ -82,6 +109,8 @@
>>>>>    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
>>>>>    gEfiImageSecurityDatabaseGuid
>>>>>
>>>>> +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
>>>>> SOMETIMES_CONSUMES ## SystemTable
>>>>> +
>>>>>  [Depex]
>>>>>    gEfiSmmCommunicationProtocolGuid
>>>>>
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
>>> ndaloneMm.i
>>>>> nf
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
>>> ndaloneMm.
>>>>> inf
>>>>> index ca9d23ce9f..95c5310c0b 100644
>>>>> ---
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
>>> ndaloneMm.i
>>>>> nf
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
>>> ndaloneMm.
>>>>> inf
>>>>> @@ -49,6 +49,8 @@
>>>>>    VariableNonVolatile.h
>>>>>    VariableParsing.c
>>>>>    VariableParsing.h
>>>>> +  VariableRuntimeCache.c
>>>>> +  VariableRuntimeCache.h
>>>>>    VarCheck.c
>>>>>    Variable.h
>>>>>    PrivilegePolymorphic.h
>>>>> diff --git
>>> a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
>>>>> b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
>>>>> index c527a59891..ceef44dfd2 100644
>>>>> --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
>>>>> +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
>>>>> @@ -1,7 +1,7 @@
>>>>>  /** @file
>>>>>    The file defined some common structures used for
>>> communicating
>>>>> between SMM variable module and SMM variable wrapper
>>> module.
>>>>>
>>>>> -Copyright (c) 2011 - 2015, Intel Corporation. All
>>> rights reserved.<BR>
>>>>> +Copyright (c) 2011 - 2019, Intel Corporation. All
>>> rights reserved.<BR>
>>>>>  SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>
>>>>>  **/
>>>>> @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-
>>> Clause-Patent
>>>>>  #ifndef _SMM_VARIABLE_COMMON_H_
>>>>>  #define _SMM_VARIABLE_COMMON_H_
>>>>>
>>>>> +#include <Guid/VariableFormat.h>
>>>>>  #include <Protocol/VarCheck.h>
>>>>>
>>>>>  #define EFI_SMM_VARIABLE_WRITE_GUID \
>>>>> @@ -66,6 +67,16 @@ typedef struct {
>>>>>  #define
>>>>>
>>> SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
>>> 10
>>>>>
>>>>>  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
>>> 11
>>>>> +//
>>>>> +// The payload for this function is
>>>>>
>>> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>>>> +//
>>>>> +#define
>>>>>
>>> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
>>> T
>>>>> 12
>>>>> +
>>>>> +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
>>>> 13
>>>>> +//
>>>>> +// The payload for this function is
>>>>> SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
>>>>> +//
>>>>> +#define
>>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
>>>>> 14
>>>>>
>>>>>  ///
>>>>>  /// Size of SMM communicate header, without
>>> including the payload.
>>>>> @@ -120,4 +131,20 @@ typedef struct {
>>>>>    UINTN
>>> VariablePayloadSize;
>>>>>  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
>>>>>
>>>>> +typedef struct {
>>>>> +  BOOLEAN                 *ReadLock;
>>>>> +  BOOLEAN                 *PendingUpdate;
>>>>> +  BOOLEAN                 *HobFlushComplete;
>>>>> +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
>>>>> +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
>>>>> +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
>>>>> +}
>>>>>
>>>>
>>>
>> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
>>>>> +
>>>>> +typedef struct {
>>>>> +  UINTN                   TotalHobStorageSize;
>>>>> +  UINTN                   TotalNvStorageSize;
>>>>> +  UINTN                   TotalVolatileStorageSize;
>>>>> +  BOOLEAN
>>> AuthenticatedVariableUsage;
>>>>> +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
>>>>> +
>>>>>  #endif // _SMM_VARIABLE_COMMON_H_
>>>>> diff --git
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
>>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
>>>>> index fb574b2e32..b9723c0250 100644
>>>>> ---
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
>>>>> +++
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
>>>>> @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-
>>> Clause-Patent
>>>>>  ///
>>>>>  #define ISO_639_2_ENTRY_SIZE    3
>>>>>
>>>>> +///
>>>>> +/// The timeout to in 10us units to wait for the
>>>>> +/// variable runtime cache read lock to be
>>> acquired.
>>>>> +///
>>>>> +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
>>>>> +
>>>>>  typedef enum {
>>>>>    VariableStoreTypeVolatile,
>>>>>    VariableStoreTypeHob,
>>>>> @@ -64,6 +70,21 @@ typedef enum {
>>>>>    VariableStoreTypeMax
>>>>>  } VARIABLE_STORE_TYPE;
>>>>>
>>>>> +typedef struct {
>>>>> +  UINT32                  PendingUpdateOffset;
>>>>> +  UINT32                  PendingUpdateLength;
>>>>> +  VARIABLE_STORE_HEADER   *Store;
>>>>> +} VARIABLE_RUNTIME_CACHE;
>>>>> +
>>>>> +typedef struct {
>>>>> +  BOOLEAN                 *ReadLock;
>>>>> +  BOOLEAN                 *PendingUpdate;
>>>>> +  BOOLEAN                 *HobFlushComplete;
>>>>> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
>>>>> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
>>>>> +  VARIABLE_RUNTIME_CACHE
>>> VariableRuntimeVolatileCache;
>>>>> +} VARIABLE_RUNTIME_CACHE_CONTEXT;
>>>>> +
>>>>>  typedef struct {
>>>>>    VARIABLE_HEADER *CurrPtr;
>>>>>    //
>>>>> @@ -79,14 +100,16 @@ typedef struct {
>>>>>  } VARIABLE_POINTER_TRACK;
>>>>>
>>>>>  typedef struct {
>>>>> -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
>>>>> -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
>>>>> -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
>>>>> -  EFI_LOCK              VariableServicesLock;
>>>>> -  UINT32                ReentrantState;
>>>>> -  BOOLEAN               AuthFormat;
>>>>> -  BOOLEAN               AuthSupport;
>>>>> -  BOOLEAN               EmuNvMode;
>>>>> +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
>>>>> +  EFI_PHYSICAL_ADDRESS
>>> HobVariableBackupBase;
>>>>
>>>>
>>>> I do not see any usage of the new field
>>> "HobVariableBackupBase".
>>>> Could you help to double confirm?
>>>>
>>>>
>>>
>>> You are correct. I removed usage of this variable before
>>> sending the
>>> patch series and the global variable here needs to be
>>> cleaned up.
>>>
>>>>> +  EFI_PHYSICAL_ADDRESS
>>> VolatileVariableBase;
>>>>> +  EFI_PHYSICAL_ADDRESS
>>> NonVolatileVariableBase;
>>>>> +  VARIABLE_RUNTIME_CACHE_CONTEXT
>>> VariableRuntimeCacheContext;
>>>>> +  EFI_LOCK
>>> VariableServicesLock;
>>>>> +  UINT32                          ReentrantState;
>>>>> +  BOOLEAN                         AuthFormat;
>>>>> +  BOOLEAN                         AuthSupport;
>>>>> +  BOOLEAN                         EmuNvMode;
>>>>>  } VARIABLE_GLOBAL;
>>>>>
>>>>>  typedef struct {
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.h
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.
>>>>> h
>>>>> new file mode 100644
>>>>> index 0000000000..09b83eb215
>>>>> --- /dev/null
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.
>>>>> h
>>>>> @@ -0,0 +1,47 @@
>>>>> +/** @file
>>>>> +  The common variable volatile store routines
>>> shared by the
>>>> DXE_RUNTIME
>>>>> variable
>>>>> +  module and the DXE_SMM variable module.
>>>>> +
>>>>> +Copyright (c) 2019, Intel Corporation. All rights
>>> reserved.<BR>
>>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>> +
>>>>> +**/
>>>>> +
>>>>> +#ifndef _VARIABLE_RUNTIME_CACHE_H_
>>>>> +#define _VARIABLE_RUNTIME_CACHE_H_
>>>>> +
>>>>> +#include "Variable.h"
>>>>> +
>>>>> +/**
>>>>> +  Copies any pending updates to runtime variable
>>> caches.
>>>>> +
>>>>> +  @retval EFI_UNSUPPORTED         The volatile
>>> store to be updated is not
>>>>> initialized properly.
>>>>> +  @retval EFI_SUCCESS             The volatile
>>> store was updated successfully.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +SynchronizeRuntimeVariableCacheEx (
>>>>> +  VOID
>>>>> +  );
>>>>> +
>>>>> +/**
>>>>> +  Synchronizes the runtime variable caches with all
>>> pending updates
>>>> outside
>>>>> runtime.
>>>>> +
>>>>> +  Ensures all conditions are met to maintain
>>> coherency for runtime cache
>>>>> updates.
>>>>> +
>>>>> +  @param[in] VariableRuntimeCache Variable runtime
>>> cache structure for
>>>>> the runtime cache being synchronized.
>>>>> +  @param[in] Offset               Offset in bytes
>>> to apply the update.
>>>>> +  @param[in] Length               Length of data in
>>> bytes of the update.
>>>>> +
>>>>> +  @retval EFI_UNSUPPORTED         The volatile
>>> store to be updated is not
>>>>> initialized properly.
>>>>> +  @retval EFI_SUCCESS             The volatile
>>> store was updated successfully.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +SynchronizeRuntimeVariableCache (
>>>>> +  IN  VARIABLE_RUNTIME_CACHE
>>> *VariableRuntimeCache,
>>>>> +  IN  UINTN                           Offset,
>>>>> +  IN  UINTN                           Length
>>>>> +  );
>>>>> +
>>>>> +#endif
>>>>> diff --git
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
>>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
>>>>> index 5da2354aa5..bb2fa3fc19 100644
>>>>> ---
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
>>>>> +++
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
>>>>> @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-
>>> Clause-Patent
>>>>>  #include "Variable.h"
>>>>>  #include "VariableNonVolatile.h"
>>>>>  #include "VariableParsing.h"
>>>>> +#include "VariableRuntimeCache.h"
>>>>>
>>>>>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
>>>>>
>>>>> @@ -332,6 +333,12 @@ RecordVarErrorFlag (
>>>>>        // Update the data in NV cache.
>>>>>        //
>>>>>        *VarErrFlag = TempFlag;
>>>>> +      Status =  SynchronizeRuntimeVariableCache (
>>>>> +                  &mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache,
>>>>> +                  (UINTN) VarErrFlag - (UINTN)
>>> mNvVariableCache + (UINTN)
>>>>> mVariableModuleGlobal-
>>>> VariableGlobal.NonVolatileVariableBase,
>>>>> +                  sizeof (TempFlag)
>>>>> +                  );
>>>>> +      ASSERT_EFI_ERROR (Status);
>>>>>      }
>>>>>    }
>>>>>  }
>>>>> @@ -755,12 +762,24 @@ Reclaim (
>>>>>
>>>>>  Done:
>>>>>    if (IsVolatile || mVariableModuleGlobal-
>>>> VariableGlobal.EmuNvMode) {
>>>>> +    Status =  SynchronizeRuntimeVariableCache (
>>>>> +                &mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e,
>>>>> +                0,
>>>>> +                VariableStoreHeader->Size
>>>>> +                );
>>>>> +    ASSERT_EFI_ERROR (Status);
>>>>>      FreePool (ValidBuffer);
>>>>>    } else {
>>>>>      //
>>>>>      // For NV variable reclaim, we use
>>> mNvVariableCache as the buffer, so
>>>>> copy the data back.
>>>>>      //
>>>>> -    CopyMem (mNvVariableCache, (UINT8
>>> *)(UINTN)VariableBase,
>>>>> VariableStoreHeader->Size);
>>>>> +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN)
>>> VariableBase,
>>>>> VariableStoreHeader->Size);
>>>>> +    Status =  SynchronizeRuntimeVariableCache (
>>>>> +                &(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache),
>>>>> +                0,
>>>>> +                VariableStoreHeader->Size
>>>>> +                );
>>>>> +    ASSERT_EFI_ERROR (Status);
>>>>>    }
>>>>>
>>>>>    return Status;
>>>>> @@ -1592,6 +1611,7 @@ UpdateVariable (
>>>>>    VARIABLE_POINTER_TRACK              *Variable;
>>>>>    VARIABLE_POINTER_TRACK              NvVariable;
>>>>>    VARIABLE_STORE_HEADER
>>> *VariableStoreHeader;
>>>>> +  VARIABLE_RUNTIME_CACHE
>>> *VolatileCacheInstance;
>>>>>    UINT8
>>> *BufferForMerge;
>>>>>    UINTN
>>> MergedBufSize;
>>>>>    BOOLEAN                             DataReady;
>>>>> @@ -2235,6 +2255,21 @@ UpdateVariable (
>>>>>    }
>>>>>
>>>>>  Done:
>>>>> +  if (!EFI_ERROR (Status)) {
>>>>> +    if (Variable->Volatile) {
>>>>> +      VolatileCacheInstance =
>>> &(mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e);
>>>>> +    } else {
>>>>> +      VolatileCacheInstance =
>>> &(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache);
>>>>> +    }
>>>>> +
>>>>> +    Status =  SynchronizeRuntimeVariableCache (
>>>>> +                VolatileCacheInstance,
>>>>> +                0,
>>>>> +                VolatileCacheInstance->Store->Size
>>>>> +                );
>>>>> +    ASSERT_EFI_ERROR (Status);
>>>>> +  }
>>>>> +
>>>>>    return Status;
>>>>>  }
>>>>>
>>>>> @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
>>>>>          ErrorFlag = TRUE;
>>>>>        }
>>>>>      }
>>>>> +    Status =  SynchronizeRuntimeVariableCache (
>>>>> +                &mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache,
>>>>> +                0,
>>>>> +                mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.S
>>>>> tore->Size
>>>>> +                );
>>>>> +    ASSERT_EFI_ERROR (Status);
>>>>>      if (ErrorFlag) {
>>>>>        //
>>>>>        // We still have HOB variable(s) not flushed
>>> in flash.
>>>>> @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
>>>>>        // All HOB variables have been flushed in
>>> flash.
>>>>>        //
>>>>>        DEBUG ((EFI_D_INFO, "Variable driver: all HOB
>>> variables have been
>>>>> flushed in flash.\n"));
>>>>> +      *(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.HobFlushComp
>>> lete) =
>>>> TRUE;
>>>>>        if (!AtRuntime ()) {
>>>>>          FreePool ((VOID *) VariableStoreHeader);
>>>>>        }
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.c
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.c
>>>>> new file mode 100644
>>>>> index 0000000000..2642d9b000
>>>>> --- /dev/null
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
>>> timeCache.c
>>>>> @@ -0,0 +1,153 @@
>>>>> +/** @file
>>>>> +  The common variable volatile store routines
>>> shared by the
>>>> DXE_RUNTIME
>>>>> variable
>>>>> +  module and the DXE_SMM variable module.
>>>>> +
>>>>> +  Caution: This module requires additional review
>>> when modified.
>>>>> +  This driver will have external input - variable
>>> data. They may be input in
>>>>> SMM mode.
>>>>> +  This external input must be validated carefully
>>> to avoid security issue like
>>>>> +  buffer overflow, integer overflow.
>>>>> +
>>>>> +Copyright (c) 2019, Intel Corporation. All rights
>>> reserved.<BR>
>>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>> +
>>>>> +**/
>>>>> +
>>>>> +#include "VariableParsing.h"
>>>>> +#include "VariableRuntimeCache.h"
>>>>> +
>>>>> +extern VARIABLE_MODULE_GLOBAL
>>> *mVariableModuleGlobal;
>>>>> +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
>>>>> +
>>>>> +/**
>>>>> +  Copies any pending updates to runtime variable
>>> caches.
>>>>> +
>>>>> +  @retval EFI_UNSUPPORTED         The volatile
>>> store to be updated is not
>>>>> initialized properly.
>>>>> +  @retval EFI_SUCCESS             The volatile
>>> store was updated successfully.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +SynchronizeRuntimeVariableCacheEx (
>>>>
>>>>
>>>> It is not clear to me why this function is named as
>>> the "Ex" version of function
>>>> SynchronizeRuntimeVariableCache(). For me, this
>>> function looks more like a
>>>> basic
>>>> version.
>>>>
>>>> I would suggest a name change for the functions
>>> provided in file
>>>> VariableRuntimeCache.c to better reflect their usage
>>> model.
>>>>
>>>>
>>>
>>> I'll rename it in V3.
>>>
>>>>> +  VOID
>>>>> +  )
>>>>> +{
>>>>
>>>>
>>>> I would recommend that at least a local variable
>>> should be introduced to
>>>> reduce
>>>> the duplications of:
>>>>
>>>> "mVariableModuleGlobal-
>>>> VariableGlobal.VariableRuntimeCacheContext"
>>>>
>>>> in this function in order to make it easier to read.
>>>>
>>>>
>>>
>>> I'll add it in V3.
>>>
>>>>> +  if (
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.St
>>>>> ore == NULL ||
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.Store == NULL ||
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e == NULL
>>>>> +    ) {
>>>>> +    return EFI_UNSUPPORTED;
>>>>> +  }
>>>>> +
>>>>> +  if (*(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e)) {
>>>>> +    if (
>>>>> +      mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.S
>>>>> tore != NULL &&
>>>>> +      mVariableModuleGlobal-
>>>> VariableGlobal.HobVariableBase > 0
>>>>> +      ) {
>>>>> +      CopyMem (
>>>>> +        (VOID *) (
>>>>> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.S
>>>>> tore) +
>>>>> +          mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.P
>>>>> endingUpdateOffset
>>>>> +          ),
>>>>> +        (VOID *) (
>>>>> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
>>>>>> VariableGlobal.HobVariableBase) +
>>>>> +          mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.P
>>>>> endingUpdateOffset
>>>>> +          ),
>>>>> +        mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.P
>>>>> endingUpdateLength
>>>>> +        );
>>>>> +      mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.P
>>>>> endingUpdateLength = 0;
>>>>> +      mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeHobCache.P
>>>>> endingUpdateOffset = 0;
>>>>> +    }
>>>>> +
>>>>> +    CopyMem (
>>>>> +      (VOID *) (
>>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.St
>>>>> ore) +
>>>>> +        mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.Pe
>>>>> ndingUpdateOffset
>>>>> +        ),
>>>>> +      (VOID *) (
>>>>> +        ((UINT8 *) (UINTN) mNvVariableCache) +
>>>>> +        mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.Pe
>>>>> ndingUpdateOffset
>>>>> +        ),
>>>>> +      mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.Pe
>>>>> ndingUpdateLength
>>>>> +      );
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.Pe
>>>>> ndingUpdateLength = 0;
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeNvCache.Pe
>>>>> ndingUpdateOffset = 0;
>>>>> +
>>>>> +    CopyMem (
>>>>> +      (VOID *) (
>>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.Store) +
>>>>> +        mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.PendingUpdateOffset
>>>>> +      ),
>>>>> +      (VOID *) (
>>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
>>>>>> VariableGlobal.VolatileVariableBase) +
>>>>> +        mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.PendingUpdateOffset
>>>>> +        ),
>>>>> +      mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.PendingUpdateLength
>>>>> +      );
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.PendingUpdateLength = 0;
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
>>> imeVolatileCach
>>>>> e.PendingUpdateOffset = 0;
>>>>> +    *(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e) = FALSE;
>>>>> +  }
>>>>> +
>>>>> +  return EFI_SUCCESS;
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> +  Synchronizes the runtime variable caches with all
>>> pending updates
>>>> outside
>>>>> runtime.
>>>>> +
>>>>> +  Ensures all conditions are met to maintain
>>> coherency for runtime cache
>>>>> updates.
>>>>> +
>>>>> +  @param[in] VariableRuntimeCache Variable runtime
>>> cache structure for
>>>>> the runtime cache being synchronized.
>>>>> +  @param[in] Offset               Offset in bytes
>>> to apply the update.
>>>>> +  @param[in] Length               Length of data in
>>> bytes of the update.
>>>>> +
>>>>> +  @retval EFI_UNSUPPORTED         The volatile
>>> store to be updated is not
>>>>> initialized properly.
>>>>> +  @retval EFI_SUCCESS             The volatile
>>> store was updated successfully.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +SynchronizeRuntimeVariableCache (
>>>>> +  IN  VARIABLE_RUNTIME_CACHE
>>> *VariableRuntimeCache,
>>>>> +  IN  UINTN                           Offset,
>>>>> +  IN  UINTN                           Length
>>>>> +  )
>>>>> +{
>>>>> +  if (VariableRuntimeCache == NULL) {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  } else if (VariableRuntimeCache->Store == NULL) {
>>>>> +      // Runtime cache is not available yet at this
>>> point,
>>>>> +      // Return EFI_SUCCESS instead of
>>> EFI_NOT_AVAILABLE_YET to let it
>>>>> progress
>>>>> +      return EFI_SUCCESS;
>>>>> +  }
>>>>> +
>>>>> +  if (
>>>>> +    mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e == NULL
>>>> ||
>>>>> +    mVariableModuleGlobal-
>>>>>> VariableGlobal.VariableRuntimeCacheContext.ReadLock
>>> == NULL
>>>>> +    ) {
>>>>> +    return EFI_UNSUPPORTED;
>>>>> +  }
>>>>> +
>>>>> +  if (
>>>>> +    *(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e) &&
>>>>> +    VariableRuntimeCache->PendingUpdateLength > 0
>>>>> +    ) {
>>>>> +    VariableRuntimeCache->PendingUpdateLength =
>>>>> +      (UINT32) (
>>>>> +        MAX (
>>>>> +          (UINTN) (VariableRuntimeCache-
>>>> PendingUpdateOffset +
>>>>> VariableRuntimeCache->PendingUpdateLength),
>>>>> +          Offset + Length
>>>>> +        ) - MIN ((UINTN) VariableRuntimeCache-
>>>> PendingUpdateOffset,
>>>> Offset)
>>>>> +      );
>>>>> +    VariableRuntimeCache->PendingUpdateOffset =
>>>>> +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
>>>>> PendingUpdateOffset,
>>>>> Offset);
>>>>> +  } else {
>>>>> +    VariableRuntimeCache->PendingUpdateLength =
>>> (UINT32) Length;
>>>>> +    VariableRuntimeCache->PendingUpdateOffset =
>>> (UINT32) Offset;
>>>>> +  }
>>>>> +  *(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
>>> e) = TRUE;
>>>>> +
>>>>> +  if (*(mVariableModuleGlobal-
>>>>>
>>>> VariableGlobal.VariableRuntimeCacheContext.ReadLock) ==
>>> FALSE) {
>>>>> +    return SynchronizeRuntimeVariableCacheEx ();
>>>>> +  }
>>>>> +
>>>>> +  return EFI_SUCCESS;
>>>>> +}
>>>>> diff --git
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .c
>>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .c
>>>>> index ce409f22a3..8d767f75ac 100644
>>>>> ---
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .c
>>>>> +++
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> .c
>>>>> @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-
>>> Clause-Patent
>>>>>  #include <Guid/SmmVariableCommon.h>
>>>>>  #include "Variable.h"
>>>>>  #include "VariableParsing.h"
>>>>> +#include "VariableRuntimeCache.h"
>>>>> +
>>>>> +extern VARIABLE_STORE_HEADER
>>> *mNvVariableCache;
>>>>>
>>>>>  BOOLEAN
>>> mAtRuntime              = FALSE;
>>>>>  UINT8
>>> *mVariableBufferPayload = NULL;
>>>>> @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
>>>>>  EFI_STATUS
>>>>>  EFIAPI
>>>>>  SmmVariableHandler (
>>>>> -  IN     EFI_HANDLE
>>> DispatchHandle,
>>>>> -  IN     CONST VOID
>>> *RegisterContext,
>>>>> -  IN OUT VOID
>>> *CommBuffer,
>>>>> -  IN OUT UINTN
>>> *CommBufferSize
>>>>> +  IN     EFI_HANDLE
>>> DispatchHandle,
>>>>> +  IN     CONST VOID
>>> *RegisterContext,
>>>>> +  IN OUT VOID
>>> *CommBuffer,
>>>>> +  IN OUT UINTN
>>> *CommBufferSize
>>>>>    )
>>>>>  {
>>>>> -  EFI_STATUS
>>> Status;
>>>>> -  SMM_VARIABLE_COMMUNICATE_HEADER
>>>>> *SmmVariableFunctionHeader;
>>>>> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
>>>>> *SmmVariableHeader;
>>>>> -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
>>>>> *GetNextVariableName;
>>>>> -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
>>>>> *QueryVariableInfo;
>>>>> -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
>>>>> *GetPayloadSize;
>>>>> -  VARIABLE_INFO_ENTRY
>>> *VariableInfo;
>>>>> -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
>>>> *VariableToLock;
>>>>> -
>>> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
>>>>> *CommVariableProperty;
>>>>> -  UINTN
>>> InfoSize;
>>>>> -  UINTN
>>> NameBufferSize;
>>>>> -  UINTN
>>> CommBufferPayloadSize;
>>>>> -  UINTN
>>> TempCommBufferSize;
>>>>> +  EFI_STATUS
>>> Status;
>>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
>>>>> *SmmVariableFunctionHeader;
>>>>> +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
>>>>> *SmmVariableHeader;
>>>>> +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
>>>>> *GetNextVariableName;
>>>>> +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
>>>>> *QueryVariableInfo;
>>>>> +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
>>>>> *GetPayloadSize;
>>>>> +
>>>>>
>>> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>>>> *RuntimeVariableCacheContext;
>>>>> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
>>>>> *GetRuntimeCacheInfo;
>>>>> +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
>>>>> *VariableToLock;
>>>>> +
>>> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
>>>>> *CommVariableProperty;
>>>>> +  VARIABLE_INFO_ENTRY
>>> *VariableInfo;
>>>>> +  VARIABLE_RUNTIME_CACHE_CONTEXT
>>>>> *VariableCacheContext;
>>>>> +  VARIABLE_STORE_HEADER
>>> *VariableCache;
>>>>> +  UINTN
>>> InfoSize;
>>>>> +  UINTN
>>> NameBufferSize;
>>>>> +  UINTN
>>> CommBufferPayloadSize;
>>>>> +  UINTN
>>> TempCommBufferSize;
>>>>>
>>>>>    //
>>>>>    // If input is invalid, stop processing this SMI
>>>>> @@ -789,6 +796,79 @@ SmmVariableHandler (
>>>>>                   );
>>>>>        CopyMem (SmmVariableFunctionHeader->Data,
>>>> mVariableBufferPayload,
>>>>> CommBufferPayloadSize);
>>>>>        break;
>>>>> +    case
>>>>>
>>>>
>>> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
>>> T:
>>>>> +      if (CommBufferPayloadSize < sizeof
>>>>>
>>>>
>>> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTE
>>> XT)
>>>> )
>>>>> {
>>>>
>>>>
>>>> The above check is not correct, I think it should be:
>>>>
>>>> if (CommBufferPayloadSize < sizeof
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>> )
>>>> ) {
>>>>
>>>> Please help to double confirm.
>>>> Also, I recommend some security tests should be
>>> performed to these new
>>>> cases in
>>>> the variable SMI handler.
>>>>
>>>>
>>>
>>> You're right. The wrong macro was simply copied.
>>>
>>>>> +        DEBUG ((DEBUG_ERROR,
>>> "InitRuntimeVariableCacheContext: SMM
>>>>> communication buffer size invalid!\n"));
>>>>> +      } else if (mEndOfDxe) {
>>>>> +        DEBUG ((DEBUG_ERROR,
>>> "InitRuntimeVariableCacheContext: Cannot
>>>>> init context after end of DXE!\n"));
>>>>> +      } else {
>>>>> +        RuntimeVariableCacheContext =
>>>>>
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>>>> *) SmmVariableFunctionHeader->Data;
>>>>
>>>>
>>>> Not sure on this one:
>>>>
>>>> Do you think it is necessary to copy the contents in
>>> the comm buffer to the
>>>> pre-allocated SMM variable buffer payload
>>> 'mVariableBufferPayload' to
>>>> avoid
>>>> TOCTOU issue? Since there are some tests (sort of, a
>>> couple of ASSERTs)
>>>> based
>>>> on the comm buffer content.
>>>>
>>>>
>>>
>>> I understand the TOCTOU observation. But is this still a
>>> concern with all the
>>> cores rendezvoused in SMM prior to end of DXE?
>>>
>>>>> +        VariableCacheContext =
>>> &mVariableModuleGlobal-
>>>>>> VariableGlobal.VariableRuntimeCacheContext;
>>>>> +
>>>>> +        ASSERT (RuntimeVariableCacheContext-
>>>> RuntimeVolatileCache !=
>>>>> NULL);
>>>>> +        ASSERT (RuntimeVariableCacheContext-
>>>> RuntimeNvCache != NULL);
>>>>> +        ASSERT (RuntimeVariableCacheContext-
>>>> PendingUpdate != NULL);
>>>>> +        ASSERT (RuntimeVariableCacheContext-
>>>> ReadLock != NULL);
>>>>> +        ASSERT (RuntimeVariableCacheContext-
>>>> HobFlushComplete !=
>>>> NULL);
>>>>> +
>>>>> +        VariableCacheContext-
>>>> VariableRuntimeHobCache.Store      =
>>>>> RuntimeVariableCacheContext->RuntimeHobCache;
>>>>> +        VariableCacheContext-
>>>> VariableRuntimeVolatileCache.Store =
>>>>> RuntimeVariableCacheContext->RuntimeVolatileCache;
>>>>> +        VariableCacheContext-
>>>> VariableRuntimeNvCache.Store       =
>>>>> RuntimeVariableCacheContext->RuntimeNvCache;
>>>>> +        VariableCacheContext->PendingUpdate
>>> =
>>>>> RuntimeVariableCacheContext->PendingUpdate;
>>>>> +        VariableCacheContext->ReadLock
>>> =
>>>>> RuntimeVariableCacheContext->ReadLock;
>>>>> +        VariableCacheContext->HobFlushComplete
>>> =
>>>>> RuntimeVariableCacheContext->HobFlushComplete;
>>>>> +
>>>>> +        // Set up the intial pending request since
>>> the RT cache needs to be in
>>>>> sync with SMM cache
>>>>> +        if (mVariableModuleGlobal-
>>>> VariableGlobal.HobVariableBase == 0) {
>>>>> +          VariableCacheContext-
>>>>>> VariableRuntimeHobCache.PendingUpdateOffset = 0;
>>>>> +          VariableCacheContext-
>>>>>> VariableRuntimeHobCache.PendingUpdateLength = 0;
>>>>> +        } else {
>>>>> +          VariableCache = (VARIABLE_STORE_HEADER *)
>>> (UINTN)
>>>>> mVariableModuleGlobal-
>>>> VariableGlobal.HobVariableBase;
>>>>> +          VariableCacheContext-
>>>>>> VariableRuntimeHobCache.PendingUpdateOffset = 0;
>>>>> +          VariableCacheContext-
>>>>>> VariableRuntimeHobCache.PendingUpdateLength =
>>> (UINT32) ((UINTN)
>>>>> GetEndPointer (VariableCache) - (UINTN)
>>> VariableCache);
>>>>> +          CopyGuid (&(VariableCacheContext-
>>>>>> VariableRuntimeHobCache.Store->Signature),
>>> &(VariableCache-
>>>>>> Signature));
>>>>> +        }
>>>>> +        VariableCache = (VARIABLE_STORE_HEADER  *)
>>> (UINTN)
>>>>> mVariableModuleGlobal-
>>>> VariableGlobal.VolatileVariableBase;
>>>>> +        VariableCacheContext-
>>>>>> VariableRuntimeVolatileCache.PendingUpdateOffset
>>> = 0;
>>>>> +        VariableCacheContext-
>>>>>> VariableRuntimeVolatileCache.PendingUpdateLength
>>> = (UINT32)
>>>> ((UINTN)
>>>>> GetEndPointer (VariableCache) - (UINTN)
>>> VariableCache);
>>>>> +        CopyGuid (&(VariableCacheContext-
>>>>>> VariableRuntimeVolatileCache.Store->Signature),
>>> &(VariableCache-
>>>>>> Signature));
>>>>> +
>>>>> +        VariableCache = (VARIABLE_STORE_HEADER  *)
>>> (UINTN)
>>>>> mNvVariableCache;
>>>>> +        VariableCacheContext-
>>>>>> VariableRuntimeNvCache.PendingUpdateOffset = 0;
>>>>> +        VariableCacheContext-
>>>>>> VariableRuntimeNvCache.PendingUpdateLength =
>>> (UINT32) ((UINTN)
>>>>> GetEndPointer (VariableCache) - (UINTN)
>>> VariableCache);
>>>>> +        CopyGuid (&(VariableCacheContext-
>>>>> VariableRuntimeNvCache.Store-
>>>>>> Signature), &(VariableCache->Signature));
>>>>> +
>>>>> +        *(VariableCacheContext->PendingUpdate) =
>>> TRUE;
>>>>> +        *(VariableCacheContext->ReadLock) = FALSE;
>>>>> +        *(VariableCacheContext->HobFlushComplete) =
>>> FALSE;
>>>>> +      }
>>>>> +      Status = EFI_SUCCESS;
>>>>> +      break;
>>>>> +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
>>>>> +      Status = SynchronizeRuntimeVariableCacheEx
>>> ();
>>>>> +      break;
>>>>> +    case
>>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
>>>>> +      if (CommBufferPayloadSize < sizeof
>>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
>>>>> +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo:
>>> SMM
>>>> communication
>>>>> buffer size invalid!\n"));
>>>>> +        return EFI_SUCCESS;
>>>>> +      }
>>>>> +      GetRuntimeCacheInfo =
>>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
>>>>> SmmVariableFunctionHeader->Data;
>>>>> +
>>>>> +      if (mVariableModuleGlobal-
>>>> VariableGlobal.HobVariableBase > 0) {
>>>>> +        VariableCache = (VARIABLE_STORE_HEADER *)
>>> (UINTN)
>>>>> mVariableModuleGlobal-
>>>> VariableGlobal.HobVariableBase;
>>>>> +        GetRuntimeCacheInfo->TotalHobStorageSize =
>>> VariableCache->Size;
>>>>> +      } else {
>>>>> +        GetRuntimeCacheInfo->TotalHobStorageSize =
>>> 0;
>>>>> +      }
>>>>> +
>>>>> +      VariableCache = (VARIABLE_STORE_HEADER  *)
>>> (UINTN)
>>>>> mVariableModuleGlobal-
>>>> VariableGlobal.VolatileVariableBase;
>>>>> +      GetRuntimeCacheInfo->TotalVolatileStorageSize
>>> = VariableCache-
>>>>> Size;
>>>>> +      VariableCache = (VARIABLE_STORE_HEADER  *)
>>> (UINTN)
>>>>> mNvVariableCache;
>>>>> +      GetRuntimeCacheInfo->TotalNvStorageSize =
>>> (UINTN) VariableCache-
>>>>>> Size;
>>>>> +      GetRuntimeCacheInfo-
>>>> AuthenticatedVariableUsage =
>>>>> mVariableModuleGlobal->VariableGlobal.AuthFormat;
>>>>> +
>>>>> +      Status = EFI_SUCCESS;
>>>>> +      break;
>>>>>
>>>>>      default:
>>>>>        Status = EFI_UNSUPPORTED;
>>>>> diff --git
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.c
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.c
>>>>> index 0a1888e5ef..46f69765a4 100644
>>>>> ---
>>>>>
>>>>
>>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.c
>>>>> +++
>>>>>
>>>>
>>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
>>> RuntimeDx
>>>>> e.c
>>>>> @@ -13,7 +13,7 @@
>>>>>
>>>>>    InitCommunicateBuffer() is really function to
>>> check the variable data size.
>>>>>
>>>>> -Copyright (c) 2010 - 2017, Intel Corporation. All
>>> rights reserved.<BR>
>>>>> +Copyright (c) 2010 - 2019, Intel Corporation. All
>>> rights reserved.<BR>
>>>>>  SPDX-License-Identifier: BSD-2-Clause-Patent
>>>>>
>>>>>  **/
>>>>> @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-
>>> Clause-Patent
>>>>>  #include <Library/UefiRuntimeLib.h>
>>>>>  #include <Library/BaseMemoryLib.h>
>>>>>  #include <Library/DebugLib.h>
>>>>> +#include <Library/TimerLib.h>
>>>>>  #include <Library/UefiLib.h>
>>>>>  #include <Library/BaseLib.h>
>>>>>
>>>>>  #include <Guid/EventGroup.h>
>>>>> +#include <Guid/PiSmmCommunicationRegionTable.h>
>>>>>  #include <Guid/SmmVariableCommon.h>
>>>>>
>>>>>  #include "PrivilegePolymorphic.h"
>>>>> +#include "VariableParsing.h"
>>>>>
>>>>>  EFI_HANDLE                       mHandle
>>> = NULL;
>>>>>  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable
>>> = NULL;
>>>>> @@ -46,8 +49,19 @@ EFI_EVENT
>>> mVirtualAddressChangeEvent
>>>> =
>>>>> NULL;
>>>>>  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication
>>> =
>>>>> NULL;
>>>>>  UINT8                           *mVariableBuffer
>>> = NULL;
>>>>>  UINT8
>>> *mVariableBufferPhysical    = NULL;
>>>>> +VARIABLE_INFO_ENTRY             *mVariableInfo
>>> = NULL;
>>>>> +VARIABLE_STORE_HEADER
>>> *mVariableRuntimeHobCacheBuffer
>>>> =
>>>>> NULL;
>>>>> +VARIABLE_STORE_HEADER
>>> *mVariableRuntimeNvCacheBuffer
>>>> =
>>>>> NULL;
>>>>> +VARIABLE_STORE_HEADER
>>> *mVariableRuntimeVolatileCacheBuffer
>>>>> = NULL;
>>>>>  UINTN
>>> mVariableBufferSize;
>>>>> +UINTN
>>> mVariableRuntimeHobCacheBufferSize;
>>>>> +UINTN
>>> mVariableRuntimeNvCacheBufferSize;
>>>>> +UINTN
>>> mVariableRuntimeVolatileCacheBufferSize;
>>>>>  UINTN
>>> mVariableBufferPayloadSize;
>>>>> +BOOLEAN
>>> mVariableRuntimeCachePendingUpdate;
>>>>> +BOOLEAN
>>> mVariableRuntimeCacheReadLock;
>>>>> +BOOLEAN
>>> mVariableAuthFormat;
>>>>> +BOOLEAN                          mHobFlushComplete;
>>>>>  EFI_LOCK
>>> mVariableServicesLock;
>>>>>  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
>>>>>  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
>>>>> @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
>>>>>    }
>>>>>  }
>>>>>
>>>>> +/**
>>>>> +  Return TRUE if ExitBootServices () has been
>>> called.
>>>>> +
>>>>> +  @retval TRUE If ExitBootServices () has been
>>> called.
>>>>> +**/
>>>>> +BOOLEAN
>>>>> +AtRuntime (
>>>>> +  VOID
>>>>> +  )
>>>>
>>>>
>>>> I think we can either:
>>>> 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
>>>> 2. Move AtRuntime() to VariableParsing.c so that the
>>> function can be shared
>>>>    with VariableRuntimeDxe & VariableSmmRuntimeDxe.
>>> And then update
>>>> the
>>>>    EfiAtRuntime() usages to AtRuntime() for
>>> VariableSmmRuntimeDxe.
>>>>
>>>>
>>>
>>> #1 will work fine.
>>>
>>>>> +{
>>>>> +  return EfiAtRuntime ();
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> +  Initialize the variable cache buffer as an empty
>>> variable store.
>>>>> +
>>>>> +  @param[out]     VariableCacheBuffer     A pointer
>>> to pointer of a cache
>>>>> variable store.
>>>>> +  @param[in,out]  TotalVariableCacheSize  On input,
>>> the minimum size
>>>>> needed for the UEFI variable store cache
>>>>> +                                          buffer
>>> that is allocated. On output, the actual size of
>>>>> the buffer allocated.
>>>>> +                                          If
>>> TotalVariableCacheSize is zero, a buffer will not be
>>>>> allocated and the
>>>>> +                                          function
>>> will return with EFI_SUCCESS.
>>>>> +
>>>>> +  @retval EFI_SUCCESS             The variable
>>> cache was allocated and
>>>> initialized
>>>>> successfully.
>>>>> +  @retval EFI_INVALID_PARAMETER   A given pointer
>>> is NULL or an invalid
>>>>> variable store size was specified.
>>>>> +  @retval EFI_OUT_OF_RESOURCES    Insufficient
>>> resources are available
>>>> to
>>>>> allocate the variable store cache buffer.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +EFIAPI
>>>>> +InitVariableCache (
>>>>> +  OUT    VARIABLE_STORE_HEADER
>>> **VariableCacheBuffer,
>>>>> +  IN OUT UINTN
>>> *TotalVariableCacheSize
>>>>> +  )
>>>>> +{
>>>>> +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
>>>>> +
>>>>> +  if (TotalVariableCacheSize == NULL) {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  }
>>>>> +  if (*TotalVariableCacheSize == 0) {
>>>>> +    return EFI_SUCCESS;
>>>>> +  }
>>>>> +  if (VariableCacheBuffer == NULL ||
>>> *TotalVariableCacheSize < sizeof
>>>>> (VARIABLE_STORE_HEADER)) {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  }
>>>>> +  *TotalVariableCacheSize = ALIGN_VALUE
>>> (*TotalVariableCacheSize,
>>>> sizeof
>>>>> (UINT32));
>>>>> +
>>>>> +  //
>>>>> +  // Allocate NV Storage Cache and initialize it to
>>> all 1's (like an erased FV)
>>>>> +  //
>>>>> +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
>>>>> AllocateRuntimePages (
>>>>> +                            EFI_SIZE_TO_PAGES
>>> (*TotalVariableCacheSize)
>>>>> +                            );
>>>>> +  if (*VariableCacheBuffer == NULL) {
>>>>> +    return EFI_OUT_OF_RESOURCES;
>>>>> +  }
>>>>> +  VariableCacheStorePtr = *VariableCacheBuffer;
>>>>> +  SetMem32 ((VOID *) VariableCacheStorePtr,
>>> *TotalVariableCacheSize,
>>>>> (UINT32) 0xFFFFFFFF);
>>>>> +
>>>>> +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
>>>>> (VARIABLE_STORE_HEADER));
>>>>> +  VariableCacheStorePtr->Size    = (UINT32)
>>> *TotalVariableCacheSize;
>>>>> +  VariableCacheStorePtr->Format  =
>>> VARIABLE_STORE_FORMATTED;
>>>>> +  VariableCacheStorePtr->State   =
>>> VARIABLE_STORE_HEALTHY;
>>>>> +
>>>>> +  return EFI_SUCCESS;
>>>>> +}
>>>>> +
>>>>>  /**
>>>>>    Initialize the communicate buffer using DataSize
>>> and Function.
>>>>>
>>>>> @@ -153,6 +234,69 @@ InitCommunicateBuffer (
>>>>>  }
>>>>>
>>>>>
>>>>> +/**
>>>>> +  Gets a SMM communicate buffer from the
>>>>> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as
>>> an entry in
>>>>> the UEFI
>>>>> +  system configuration table. A generic SMM
>>> communication buffer DXE
>>>>> driver may install the table or a custom table
>>>>> +  may be installed by a platform-specific driver.
>>>>> +
>>>>> +  The communicate size is:
>>> SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
>>>>> +                            DataSize.
>>>>> +
>>>>> +  @param[in,out]   CommBufferSize   On input, the
>>> minimum size needed
>>>>> for the communication buffer.
>>>>> +                                    On output, the
>>> SMM buffer size available at
>>>> CommBuffer.
>>>>> +  @param[out]      CommBuffer       A pointer to an
>>> SMM communication
>>>>> buffer pointer.
>>>>> +
>>>>> +  @retval EFI_SUCCESS               The
>>> communication buffer was found
>>>>> successfully.
>>>>> +  @retval EFI_INVALID_PARAMETER     A given pointer
>>> is NULL or the
>>>>> CommBufferSize is zero.
>>>>> +  @retval EFI_NOT_FOUND             The
>>>>> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not
>>> found.
>>>>> +  @retval EFI_OUT_OF_RESOURCES      A valid SMM
>>> communicate buffer
>>>> for
>>>>> the requested size is not available.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +GetCommunicateBuffer (
>>>>> +  IN OUT  UINTN     *CommBufferSize,
>>>>> +  OUT     UINT8     **CommBuffer
>>>>> +  )
>>>>
>>>>
>>>> Minor comment:
>>>>
>>>> I found that the consumers of the above function are:
>>>> GetRuntimeCacheInfo()
>>>> SendRuntimeVariableCacheContextToSmm()
>>>>
>>>> Both of them get called within SmmVariableReady() when
>>> the SMM variable
>>>> driver
>>>> finished initialization. I am wondering if they can
>>> simply use the pre-allocated
>>>> comm buffer (via InitCommunicateBuffer() and using
>>> 'mVariableBuffer'),
>>>> instead
>>>> of looking into the configuration table.
>>>>
>>>> In my opinion, this function can be dropped.
>>>>
>>>>
>>>
>>> I did that initially. It was recommended to use this
>>> method.
>>>
>>>>> +{
>>>>> +  EFI_STATUS                                Status;
>>>>> +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
>>>>> *PiSmmCommunicationRegionTable;
>>>>> +  EFI_MEMORY_DESCRIPTOR                     *Entry;
>>>>> +  UINTN
>>> EntrySize;
>>>>> +  UINT32                                    Index;
>>>>> +
>>>>> +  if (CommBuffer == NULL || CommBufferSize == NULL
>>> ||
>>>>> *CommBufferSize == 0) {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  }
>>>>> +
>>>>> +  Status = EfiGetSystemConfigurationTable (
>>>>> +
>>> &gEdkiiPiSmmCommunicationRegionTableGuid,
>>>>> +             (VOID **)
>>> &PiSmmCommunicationRegionTable
>>>>> +             );
>>>>> +  if (EFI_ERROR (Status) ||
>>> PiSmmCommunicationRegionTable == NULL) {
>>>>> +    return EFI_NOT_FOUND;
>>>>> +  }
>>>>> +
>>>>> +  Entry = (EFI_MEMORY_DESCRIPTOR *)
>>>> (PiSmmCommunicationRegionTable
>>>>> + 1);
>>>>> +  EntrySize = 0;
>>>>> +  for (Index = 0; Index <
>>> PiSmmCommunicationRegionTable-
>>>>>> NumberOfEntries; Index++) {
>>>>> +    if (Entry->Type == EfiConventionalMemory) {
>>>>> +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry-
>>>> NumberOfPages);
>>>>> +      if (EntrySize >= *CommBufferSize) {
>>>>> +        break;
>>>>> +      }
>>>>> +    }
>>>>> +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *)
>>> Entry +
>>>>> PiSmmCommunicationRegionTable->DescriptorSize);
>>>>> +  }
>>>>> +
>>>>> +  if (Index < PiSmmCommunicationRegionTable-
>>>> NumberOfEntries) {
>>>>> +    *CommBufferSize = EntrySize;
>>>>> +    *CommBuffer = (UINT8 *) (UINTN) Entry-
>>>> PhysicalStart;
>>>>> +    return EFI_SUCCESS;
>>>>> +  }
>>>>> +
>>>>> +  return EFI_OUT_OF_RESOURCES;
>>>>> +}
>>>>> +
>>>>>  /**
>>>>>    Send the data in communicate buffer to SMM.
>>>>>
>>>>> @@ -424,6 +568,171 @@ Done:
>>>>>    return Status;
>>>>>  }
>>>>>
>>>>> +/**
>>>>> +  Signals SMM to synchronize any pending variable
>>> updates with the
>>>>> runtime cache(s).
>>>>> +
>>>>> +**/
>>>>> +VOID
>>>>> +EFIAPI
>>>>> +SyncRuntimeCache (
>>>>> +  VOID
>>>>> +  )
>>>>> +{
>>>>> +  //
>>>>> +  // Init the communicate buffer. The buffer data
>>> size is:
>>>>> +  // SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
>>>>> +  //
>>>>> +  InitCommunicateBuffer (NULL, 0,
>>>>> SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
>>>>> +
>>>>> +  //
>>>>> +  // Send data to SMM.
>>>>> +  //
>>>>> +  SendCommunicateBuffer (0);
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> +  Check whether a SMI must be triggered to retrieve
>>> pending cache
>>>> updates.
>>>>> +
>>>>> +  If the variable HOB was finished being flushed
>>> since the last check for a
>>>>> runtime cache update, this function
>>>>> +  will prevent the HOB cache from being used for
>>> future runtime cache
>>>> hits.
>>>>> +
>>>>> +**/
>>>>> +VOID
>>>>> +EFIAPI
>>>>> +CheckForRuntimeCacheSync (
>>>>> +  VOID
>>>>> +  )
>>>>> +{
>>>>> +  if (mVariableRuntimeCachePendingUpdate) {
>>>>> +    SyncRuntimeCache ();
>>>>> +  }
>>>>> +  ASSERT (!mVariableRuntimeCachePendingUpdate);
>>>>> +
>>>>> +  //
>>>>> +  // The HOB variable data may have finished being
>>> flushed in the runtime
>>>>> cache sync update
>>>>> +  //
>>>>> +  if (mHobFlushComplete &&
>>> mVariableRuntimeHobCacheBuffer != NULL)
>>>> {
>>>>> +    if (!AtRuntime ()) {
>>>>> +      FreePool (mVariableRuntimeHobCacheBuffer);
>>>>> +    }
>>>>> +    mVariableRuntimeHobCacheBuffer = NULL;
>>>>> +  }
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> +  This code finds variable in a volatile memory
>>> store.
>>>>> +
>>>>> +  Caution: This function may receive untrusted
>>> input.
>>>>> +  The data size is external input, so this function
>>> will validate it carefully to
>>>>> avoid buffer overflow.
>>>>> +
>>>>> +  @param[in]      VariableName       Name of
>>> Variable to be found.
>>>>> +  @param[in]      VendorGuid         Variable
>>> vendor GUID.
>>>>> +  @param[out]     Attributes         Attribute
>>> value of the variable found.
>>>>> +  @param[in, out] DataSize           Size of Data
>>> found. If size is less than the
>>>>> +                                     data, this
>>> value contains the required size.
>>>>> +  @param[out]     Data               Data pointer.
>>>>> +
>>>>> +  @retval EFI_SUCCESS                Found the
>>> specified variable.
>>>>> +  @retval EFI_INVALID_PARAMETER      Invalid
>>> parameter.
>>>>> +  @retval EFI_NOT_FOUND              The specified
>>> variable could not be
>>>> found.
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +EFIAPI
>>>>> +FindVariableInRuntimeCache (
>>>>> +  IN      CHAR16
>>> *VariableName,
>>>>> +  IN      EFI_GUID
>>> *VendorGuid,
>>>>> +  OUT     UINT32
>>> *Attributes OPTIONAL,
>>>>> +  IN OUT  UINTN
>>> *DataSize,
>>>>> +  OUT     VOID                              *Data
>>> OPTIONAL
>>>>> +  )
>>>>> +{
>>>>> +  EFI_STATUS              Status;
>>>>> +  UINTN                   DelayIndex;
>>>>> +  UINTN                   TempDataSize;
>>>>> +  VARIABLE_POINTER_TRACK  RtPtrTrack;
>>>>> +  VARIABLE_STORE_TYPE     StoreType;
>>>>> +  VARIABLE_STORE_HEADER
>>> *VariableStoreList[VariableStoreTypeMax];
>>>>> +
>>>>> +  Status = EFI_NOT_FOUND;
>>>>> +
>>>>> +  if (VariableName == NULL || VendorGuid == NULL ||
>>> DataSize == NULL) {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  }
>>>>> +
>>>>> +  for (DelayIndex = 0;
>>> mVariableRuntimeCacheReadLock && DelayIndex <
>>>>> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
>>>>> +    MicroSecondDelay (10);
>>>>> +  }
>>>>> +  if (DelayIndex <
>>> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
>>>>> +    ASSERT (!mVariableRuntimeCacheReadLock);
>>>>> +
>>>>> +    mVariableRuntimeCacheReadLock = TRUE;
>>>>> +    CheckForRuntimeCacheSync ();
>>>>> +
>>>>> +    if (!mVariableRuntimeCachePendingUpdate) {
>>>>> +      //
>>>>> +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
>>>>> +      // The index and attributes mapping must be
>>> kept in this order as
>>>>> FindVariable
>>>>> +      // makes use of this mapping to implement
>>> search algorithm.
>>>>> +      //
>>>>> +      VariableStoreList[VariableStoreTypeVolatile]
>>> =
>>>>> mVariableRuntimeVolatileCacheBuffer;
>>>>> +      VariableStoreList[VariableStoreTypeHob]
>>> =
>>>>> mVariableRuntimeHobCacheBuffer;
>>>>> +      VariableStoreList[VariableStoreTypeNv]
>>> =
>>>>> mVariableRuntimeNvCacheBuffer;
>>>>> +
>>>>> +      for (StoreType = (VARIABLE_STORE_TYPE) 0;
>>> StoreType <
>>>>> VariableStoreTypeMax; StoreType++) {
>>>>> +        if (VariableStoreList[StoreType] == NULL) {
>>>>> +          continue;
>>>>> +        }
>>>>> +
>>>>> +        RtPtrTrack.StartPtr = GetStartPointer
>>> (VariableStoreList[StoreType]);
>>>>> +        RtPtrTrack.EndPtr   = GetEndPointer
>>> (VariableStoreList[StoreType]);
>>>>> +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType
>>> ==
>>>>> VariableStoreTypeVolatile);
>>>>> +
>>>>> +        Status = FindVariableEx (VariableName,
>>> VendorGuid, FALSE,
>>>>> &RtPtrTrack);
>>>>> +        if (!EFI_ERROR (Status)) {
>>>>> +          break;
>>>>> +        }
>>>>> +      }
>>>>> +
>>>>> +      if (!EFI_ERROR (Status)) {
>>>>> +        //
>>>>> +        // Get data size
>>>>> +        //
>>>>> +        TempDataSize = DataSizeOfVariable
>>> (RtPtrTrack.CurrPtr);
>>>>> +        ASSERT (TempDataSize != 0);
>>>>> +
>>>>> +        if (*DataSize >= TempDataSize) {
>>>>> +          if (Data == NULL) {
>>>>> +            Status = EFI_INVALID_PARAMETER;
>>>>> +            goto Done;
>>>>> +          }
>>>>> +
>>>>> +          CopyMem (Data, GetVariableDataPtr
>>> (RtPtrTrack.CurrPtr),
>>>>> TempDataSize);
>>>>> +          if (Attributes != NULL) {
>>>>> +            *Attributes = RtPtrTrack.CurrPtr-
>>>> Attributes;
>>>>> +          }
>>>>> +
>>>>> +          *DataSize = TempDataSize;
>>>>> +
>>>>> +          UpdateVariableInfo (VariableName,
>>> VendorGuid,
>>>> RtPtrTrack.Volatile,
>>>>> TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
>>>>> +
>>>>> +          Status = EFI_SUCCESS;
>>>>> +          goto Done;
>>>>> +        } else {
>>>>> +          *DataSize = TempDataSize;
>>>>> +          Status = EFI_BUFFER_TOO_SMALL;
>>>>> +          goto Done;
>>>>> +        }
>>>>> +      }
>>>>> +    }
>>>>> +  }
>>>>> +
>>>>> +Done:
>>>>> +  mVariableRuntimeCacheReadLock = FALSE;
>>>>
>>>>
>>>> If timeout occurs when acquiring the read lock, should
>>> this flag be set to
>>>> FALSE
>>>> in such case?
>>>>
>>>
>>> Please see reply to patch #8.
>>>
>>>> Best Regards,
>>>> Hao Wu
>>>>
>>>>
>>>>> +
>>>>> +  return Status;
>>>>> +}
>>>>> +
>>>>>  /**
>>>>>    This code finds variable in storage blocks
>>> (Volatile or Non-Volatile).
>>>>>
>>>>> @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
>>>>>    )
>>>>>  {
>>>>>    EFI_STATUS                                Status;
>>>>> -  UINTN
>>> PayloadSize;
>>>>> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
>>>>> *SmmVariableHeader;
>>>>> -  UINTN
>>> TempDataSize;
>>>>> -  UINTN
>>> VariableNameSize;
>>>>>
>>>>>    if (VariableName == NULL || VendorGuid == NULL ||
>>> DataSize == NULL) {
>>>>>      return EFI_INVALID_PARAMETER;
>>>>>    }
>>>>> -
>>>>> -  TempDataSize          = *DataSize;
>>>>> -  VariableNameSize      = StrSize (VariableName);
>>>>> -  SmmVariableHeader     = NULL;
>>>>> -
>>>>> -  //
>>>>> -  // If VariableName exceeds SMM payload limit.
>>> Return failure
>>>>> -  //
>>>>> -  if (VariableNameSize > mVariableBufferPayloadSize
>>> - OFFSET_OF
>>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
>>>>> -    return EFI_INVALID_PARAMETER;
>>>>> -  }
>>>>> -
>>>>> -
>>> AcquireLockOnlyAtBootTime(&mVariableServicesLock);
>>>>> -
>>>>> -  //
>>>>> -  // Init the communicate buffer. The buffer data
>>> size is:
>>>>> -  // SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
>>>>> -  //
>>>>> -  if (TempDataSize > mVariableBufferPayloadSize -
>>> OFFSET_OF
>>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
>>>>> VariableNameSize) {
>>>>> -    //
>>>>> -    // If output data buffer exceed SMM payload
>>> limit. Trim output buffer to
>>>>> SMM payload size
>>>>> -    //
>>>>> -    TempDataSize = mVariableBufferPayloadSize -
>>> OFFSET_OF
>>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
>>>>> VariableNameSize;
>>>>> -  }
>>>>> -  PayloadSize = OFFSET_OF
>>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
>>>>> VariableNameSize + TempDataSize;
>>>>> -
>>>>> -  Status = InitCommunicateBuffer ((VOID
>>> **)&SmmVariableHeader,
>>>>> PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
>>>>> -  if (EFI_ERROR (Status)) {
>>>>> -    goto Done;
>>>>> -  }
>>>>> -  ASSERT (SmmVariableHeader != NULL);
>>>>> -
>>>>> -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
>>>>> -  SmmVariableHeader->DataSize   = TempDataSize;
>>>>> -  SmmVariableHeader->NameSize   = VariableNameSize;
>>>>> -  if (Attributes == NULL) {
>>>>> -    SmmVariableHeader->Attributes = 0;
>>>>> -  } else {
>>>>> -    SmmVariableHeader->Attributes = *Attributes;
>>>>> -  }
>>>>> -  CopyMem (SmmVariableHeader->Name, VariableName,
>>>>> SmmVariableHeader->NameSize);
>>>>> -
>>>>> -  //
>>>>> -  // Send data to SMM.
>>>>> -  //
>>>>> -  Status = SendCommunicateBuffer (PayloadSize);
>>>>> -
>>>>> -  //
>>>>> -  // Get data from SMM.
>>>>> -  //
>>>>> -  if (Status == EFI_SUCCESS || Status ==
>>> EFI_BUFFER_TOO_SMALL) {
>>>>> -    //
>>>>> -    // SMM CommBuffer DataSize can be a trimed
>>> value
>>>>> -    // Only update DataSize when needed
>>>>> -    //
>>>>> -    *DataSize = SmmVariableHeader->DataSize;
>>>>> -  }
>>>>> -  if (Attributes != NULL) {
>>>>> -    *Attributes = SmmVariableHeader->Attributes;
>>>>> -  }
>>>>> -
>>>>> -  if (EFI_ERROR (Status)) {
>>>>> -    goto Done;
>>>>> -  }
>>>>> -
>>>>> -  if (Data != NULL) {
>>>>> -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name
>>> +
>>>>> SmmVariableHeader->NameSize, SmmVariableHeader-
>>>> DataSize);
>>>>> -  } else {
>>>>> -    Status = EFI_INVALID_PARAMETER;
>>>>> +  if (VariableName[0] == 0) {
>>>>> +    return EFI_NOT_FOUND;
>>>>>    }
>>>>>
>>>>> -Done:
>>>>> +  AcquireLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +  Status =  FindVariableInRuntimeCache
>>> (VariableName, VendorGuid,
>>>>> Attributes, DataSize, Data);
>>>>>    ReleaseLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +
>>>>>    return Status;
>>>>>  }
>>>>>
>>>>> -
>>>>>  /**
>>>>>    This code Finds the Next available variable.
>>>>>
>>>>> @@ -870,6 +1109,17 @@ OnReadyToBoot (
>>>>>    //
>>>>>    SendCommunicateBuffer (0);
>>>>>
>>>>> +  //
>>>>> +  // Install the system configuration table for
>>> variable info data captured
>>>>> +  //
>>>>> +  if (FeaturePcdGet (PcdVariableCollectStatistics))
>>> {
>>>>> +    if (mVariableAuthFormat) {
>>>>> +      gBS->InstallConfigurationTable
>>> (&gEfiAuthenticatedVariableGuid,
>>>>> mVariableInfo);
>>>>> +    } else {
>>>>> +      gBS->InstallConfigurationTable
>>> (&gEfiVariableGuid, mVariableInfo);
>>>>> +    }
>>>>> +  }
>>>>> +
>>>>>    gBS->CloseEvent (Event);
>>>>>  }
>>>>>
>>>>> @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
>>>>>  {
>>>>>    EfiConvertPointer (0x0, (VOID **)
>>> &mVariableBuffer);
>>>>>    EfiConvertPointer (0x0, (VOID **)
>>> &mSmmCommunication);
>>>>> +  EfiConvertPointer (0x0, (VOID **)
>>> &mVariableRuntimeHobCacheBuffer);
>>>>> +  EfiConvertPointer (0x0, (VOID **)
>>> &mVariableRuntimeNvCacheBuffer);
>>>>> +  EfiConvertPointer (0x0, (VOID **)
>>>>> &mVariableRuntimeVolatileCacheBuffer);
>>>>>  }
>>>>>
>>>>>  /**
>>>>> @@ -969,6 +1222,173 @@ Done:
>>>>>    return Status;
>>>>>  }
>>>>>
>>>>> +/**
>>>>> +  This code gets information needed from SMM for
>>> runtime cache
>>>>> initialization.
>>>>> +
>>>>> +  @param[out] TotalHobStorageSize         Output
>>> pointer for the total HOB
>>>>> storage size in bytes.
>>>>> +  @param[out] TotalNvStorageSize          Output
>>> pointer for the total non-
>>>>> volatile storage size in bytes.
>>>>> +  @param[out] TotalVolatileStorageSize    Output
>>> pointer for the total
>>>>> volatile storage size in bytes.
>>>>> +  @param[out] AuthenticatedVariableUsage  Output
>>> pointer that indicates
>>>> if
>>>>> authenticated variables are to be used.
>>>>> +
>>>>> +  @retval EFI_SUCCESS                     Retrieved
>>> the size successfully.
>>>>> +  @retval EFI_INVALID_PARAMETER
>>> TotalNvStorageSize parameter is
>>>>> NULL.
>>>>> +  @retval EFI_OUT_OF_RESOURCES            Could not
>>> allocate a
>>>> CommBuffer.
>>>>> +  @retval Others                          Could not
>>> retrieve the size successfully.;
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +EFIAPI
>>>>> +GetRuntimeCacheInfo (
>>>>> +  OUT UINTN
>>> *TotalHobStorageSize,
>>>>> +  OUT UINTN
>>> *TotalNvStorageSize,
>>>>> +  OUT UINTN
>>> *TotalVolatileStorageSize,
>>>>> +  OUT BOOLEAN
>>> *AuthenticatedVariableUsage
>>>>> +  )
>>>>> +{
>>>>> +  EFI_STATUS
>>> Status;
>>>>> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
>>>>> *SmmGetRuntimeCacheInfo;
>>>>> +  EFI_SMM_COMMUNICATE_HEADER
>>>>> *SmmCommunicateHeader;
>>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
>>>>> *SmmVariableFunctionHeader;
>>>>> +  UINTN
>>> CommSize;
>>>>> +  UINTN
>>> CommBufferSize;
>>>>> +  UINT8
>>> *CommBuffer;
>>>>> +
>>>>> +  SmmGetRuntimeCacheInfo = NULL;
>>>>> +  CommBuffer = NULL;
>>>>> +
>>>>> +  if (TotalHobStorageSize == NULL ||
>>> TotalNvStorageSize == NULL ||
>>>>> TotalVolatileStorageSize == NULL ||
>>> AuthenticatedVariableUsage == NULL)
>>>> {
>>>>> +    return EFI_INVALID_PARAMETER;
>>>>> +  }
>>>>> +
>>>>> +  AcquireLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +
>>>>> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
>>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
>>>>> +  CommBufferSize = CommSize;
>>>>> +  Status = GetCommunicateBuffer (&CommBufferSize,
>>> &CommBuffer);
>>>>> +  if (EFI_ERROR (Status)) {
>>>>> +    goto Done;
>>>>> +  }
>>>>> +  if (CommBuffer == NULL) {
>>>>> +    Status = EFI_OUT_OF_RESOURCES;
>>>>> +    goto Done;
>>>>> +  }
>>>>> +  ZeroMem (CommBuffer, CommBufferSize);
>>>>> +
>>>>> +  SmmCommunicateHeader =
>>> (EFI_SMM_COMMUNICATE_HEADER *)
>>>>> CommBuffer;
>>>>> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
>>>>> &gEfiSmmVariableProtocolGuid);
>>>>> +  SmmCommunicateHeader->MessageLength =
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
>>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
>>>>> +
>>>>> +  SmmVariableFunctionHeader =
>>>>> (SMM_VARIABLE_COMMUNICATE_HEADER *)
>>> SmmCommunicateHeader-
>>>>>> Data;
>>>>> +  SmmVariableFunctionHeader->Function =
>>>>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
>>>>> +  SmmGetRuntimeCacheInfo =
>>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
>>>>> SmmVariableFunctionHeader->Data;
>>>>> +
>>>>> +  //
>>>>> +  // Send data to SMM.
>>>>> +  //
>>>>> +  Status = mSmmCommunication->Communicate
>>> (mSmmCommunication,
>>>>> CommBuffer, &CommSize);
>>>>> +  ASSERT_EFI_ERROR (Status);
>>>>> +  if (CommSize <=
>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
>>>>> +    Status = EFI_BAD_BUFFER_SIZE;
>>>>> +    goto Done;
>>>>> +  }
>>>>> +
>>>>> +  Status = SmmVariableFunctionHeader->ReturnStatus;
>>>>> +  if (EFI_ERROR (Status)) {
>>>>> +    goto Done;
>>>>> +  }
>>>>> +
>>>>> +  //
>>>>> +  // Get data from SMM.
>>>>> +  //
>>>>> +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
>>>>> TotalHobStorageSize;
>>>>> +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo-
>>>> TotalNvStorageSize;
>>>>> +  *TotalVolatileStorageSize =
>>> SmmGetRuntimeCacheInfo-
>>>>>> TotalVolatileStorageSize;
>>>>> +  *AuthenticatedVariableUsage =
>>> SmmGetRuntimeCacheInfo-
>>>>>> AuthenticatedVariableUsage;
>>>>> +
>>>>> +Done:
>>>>> +  ReleaseLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +  return Status;
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> +  Sends the runtime variable cache context
>>> information to SMM.
>>>>> +
>>>>> +  @retval EFI_SUCCESS               Retrieved the
>>> size successfully.
>>>>> +  @retval EFI_INVALID_PARAMETER
>>> TotalNvStorageSize parameter is
>>>>> NULL.
>>>>> +  @retval EFI_OUT_OF_RESOURCES      Could not
>>> allocate a CommBuffer.
>>>>> +  @retval Others                    Could not
>>> retrieve the size successfully.;
>>>>> +
>>>>> +**/
>>>>> +EFI_STATUS
>>>>> +EFIAPI
>>>>> +SendRuntimeVariableCacheContextToSmm (
>>>>> +  VOID
>>>>> +  )
>>>>> +{
>>>>> +  EFI_STATUS
>>> Status;
>>>>> +
>>>>>
>>> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>>>> *SmmRuntimeVarCacheContext;
>>>>> +  EFI_SMM_COMMUNICATE_HEADER
>>>>> *SmmCommunicateHeader;
>>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
>>>>> *SmmVariableFunctionHeader;
>>>>> +  UINTN
>>> CommSize;
>>>>> +  UINTN
>>> CommBufferSize;
>>>>> +  UINT8
>>> *CommBuffer;
>>>>> +
>>>>> +  SmmRuntimeVarCacheContext = NULL;
>>>>> +  CommBuffer = NULL;
>>>>> +
>>>>> +  AcquireLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +
>>>>> +  //
>>>>> +  // Init the communicate buffer. The buffer data
>>> size is:
>>>>> +  // SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
>>>>>
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>> )
>>>> ;
>>>>> +  //
>>>>> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
>>>>>
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>> )
>>>> ;
>>>>> +  CommBufferSize = CommSize;
>>>>> +  Status = GetCommunicateBuffer (&CommBufferSize,
>>> &CommBuffer);
>>>>> +  if (EFI_ERROR (Status)) {
>>>>> +    goto Done;
>>>>> +  }
>>>>> +  if (CommBuffer == NULL) {
>>>>> +    Status = EFI_OUT_OF_RESOURCES;
>>>>> +    goto Done;
>>>>> +  }
>>>>> +  ZeroMem (CommBuffer, CommBufferSize);
>>>>> +
>>>>> +  SmmCommunicateHeader =
>>> (EFI_SMM_COMMUNICATE_HEADER *)
>>>>> CommBuffer;
>>>>> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
>>>>> &gEfiSmmVariableProtocolGuid);
>>>>> +  SmmCommunicateHeader->MessageLength =
>>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
>>>>>
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>> )
>>>> ;
>>>>> +
>>>>> +  SmmVariableFunctionHeader =
>>>>> (SMM_VARIABLE_COMMUNICATE_HEADER *)
>>> SmmCommunicateHeader-
>>>>>> Data;
>>>>> +  SmmVariableFunctionHeader->Function =
>>>>>
>>>>
>>> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
>>> T;
>>>>> +  SmmRuntimeVarCacheContext =
>>>>>
>>>>
>>>
>> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
>>>>> *) SmmVariableFunctionHeader->Data;
>>>>> +
>>>>> +  SmmRuntimeVarCacheContext->RuntimeHobCache =
>>>>> mVariableRuntimeHobCacheBuffer;
>>>>> +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
>>>>> mVariableRuntimeVolatileCacheBuffer;
>>>>> +  SmmRuntimeVarCacheContext->RuntimeNvCache =
>>>>> mVariableRuntimeNvCacheBuffer;
>>>>> +  SmmRuntimeVarCacheContext->PendingUpdate =
>>>>> &mVariableRuntimeCachePendingUpdate;
>>>>> +  SmmRuntimeVarCacheContext->ReadLock =
>>>>> &mVariableRuntimeCacheReadLock;
>>>>> +  SmmRuntimeVarCacheContext->HobFlushComplete =
>>>>> &mHobFlushComplete;
>>>>> +
>>>>> +  //
>>>>> +  // Send data to SMM.
>>>>> +  //
>>>>> +  Status = mSmmCommunication->Communicate
>>> (mSmmCommunication,
>>>>> CommBuffer, &CommSize);
>>>>> +  ASSERT_EFI_ERROR (Status);
>>>>> +  if (CommSize <=
>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
>>>>> +    Status = EFI_BAD_BUFFER_SIZE;
>>>>> +    goto Done;
>>>>> +  }
>>>>> +
>>>>> +  Status = SmmVariableFunctionHeader->ReturnStatus;
>>>>> +  if (EFI_ERROR (Status)) {
>>>>> +    goto Done;
>>>>> +  }
>>>>> +
>>>>> +Done:
>>>>> +  ReleaseLockOnlyAtBootTime
>>> (&mVariableServicesLock);
>>>>> +  return Status;
>>>>> +}
>>>>> +
>>>>>  /**
>>>>>    Initialize variable service and install Variable
>>> Architectural protocol.
>>>>>
>>>>> @@ -985,7 +1405,7 @@ SmmVariableReady (
>>>>>  {
>>>>>    EFI_STATUS                                Status;
>>>>>
>>>>> -  Status = gBS->LocateProtocol
>>> (&gEfiSmmVariableProtocolGuid, NULL,
>>>>> (VOID **)&mSmmVariable);
>>>>> +  Status = gBS->LocateProtocol
>>> (&gEfiSmmVariableProtocolGuid, NULL,
>>>>> (VOID **) &mSmmVariable);
>>>>>    if (EFI_ERROR (Status)) {
>>>>>      return;
>>>>>    }
>>>>> @@ -1007,6 +1427,40 @@ SmmVariableReady (
>>>>>    //
>>>>>    mVariableBufferPhysical = mVariableBuffer;
>>>>>
>>>>> +  //
>>>>> +  // Allocate runtime variable cache memory
>>> buffers.
>>>>> +  //
>>>>> +  Status =  GetRuntimeCacheInfo (
>>>>> +              &mVariableRuntimeHobCacheBufferSize,
>>>>> +              &mVariableRuntimeNvCacheBufferSize,
>>>>> +
>>> &mVariableRuntimeVolatileCacheBufferSize,
>>>>> +              &mVariableAuthFormat
>>>>> +              );
>>>>> +  if (!EFI_ERROR (Status)) {
>>>>> +    Status = InitVariableCache
>>> (&mVariableRuntimeHobCacheBuffer,
>>>>> &mVariableRuntimeHobCacheBufferSize);
>>>>> +    if (!EFI_ERROR (Status)) {
>>>>> +      Status = InitVariableCache
>>> (&mVariableRuntimeNvCacheBuffer,
>>>>> &mVariableRuntimeNvCacheBufferSize);
>>>>> +      if (!EFI_ERROR (Status)) {
>>>>> +        Status = InitVariableCache
>>> (&mVariableRuntimeVolatileCacheBuffer,
>>>>> &mVariableRuntimeVolatileCacheBufferSize);
>>>>> +        if (!EFI_ERROR (Status)) {
>>>>> +          Status = InitVariableParsing
>>> (mVariableAuthFormat);
>>>>> +          ASSERT_EFI_ERROR (Status);
>>>>> +
>>>>> +          Status =
>>> SendRuntimeVariableCacheContextToSmm ();
>>>>> +          if (!EFI_ERROR (Status)) {
>>>>> +            SyncRuntimeCache ();
>>>>> +          }
>>>>> +        }
>>>>> +      }
>>>>> +    }
>>>>> +    if (EFI_ERROR (Status)) {
>>>>> +      mVariableRuntimeHobCacheBuffer = NULL;
>>>>> +      mVariableRuntimeNvCacheBuffer = NULL;
>>>>> +      mVariableRuntimeVolatileCacheBuffer = NULL;
>>>>> +    }
>>>>> +  }
>>>>> +  ASSERT_EFI_ERROR (Status);
>>>>> +
>>>>>    gRT->GetVariable         =
>>> RuntimeServiceGetVariable;
>>>>>    gRT->GetNextVariableName =
>>> RuntimeServiceGetNextVariableName;
>>>>>    gRT->SetVariable         =
>>> RuntimeServiceSetVariable;
>>>>> --
>>>>> 2.16.2.windows.1
>>>>
>>>
>>
> 


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-04  6:38       ` Laszlo Ersek
@ 2019-10-04 16:48         ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-04 16:48 UTC (permalink / raw)
  To: Laszlo Ersek, Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen,
	Andrew Fish

> On 10/03/19 23:53, Kubacki, Michael A wrote:
> > #1 - The plan is to remove the polling entirely in V3.
> >
> > #2 - I'd prefer to take a definitive direction and reduce validation and
> maintenance
> >         effort but you and Laszlo both requested this so I'll add a FeaturePCD
> to control
> >         activation of the runtime cache in this patch series. Perhaps this can be
> removed
> >         in the future.
> 
> Thanks!
> 
> (I'm also happy with the lock / timeout resolution. I had known about the
> reentrancy restriction in the UEFI spec (I happened to look at something in
> the kernel just the other day that reminded me of that part of the spec), but
> it wasn't clear to me that the lock + timeout in the patch series were
> intended to protect against just that. Kudos to Andrew for the comment!)
> 
> (
> 
> Meanwhile, I've received help from my colleagues wrt.
> QueryVariableInfo(), but right now it's too early to tell if we'll be able to
> settle on that in the long term:
> 
> [PATCH] efi/efi_test: require CAP_SYS_ADMIN to open the chardev
> http://mid.mail-archive.com/20191003100712.31045-1-javierm@redhat.com
> 
> )
> 

Thanks for the QueryVariableInfo () update.

> For the next version of this edk2 patch set (where you plan to include the
> new FeaturePCD, if I understand correctly), I'd like to request the
> following: either set the DEC default to FALSE please, or please include a
> patch for OvmfPkg where you set the PCD to FALSE in all the OvmfPkg DSC
> files.
> 
> I think the next stable release should be made like that. Then, for the stable
> release following that, we can re-evaluate the question, and might decide to
> invert the PCD in OVMF (enabling the feature), assuming
> QueryVariableInfo() proves usable in Fedora, by then.
> 
> 

I'd like to propose the DEC default be set to TRUE and I make the changes in
all the OvmfPkg DSC files to set the PCD to FALSE. 

> Two independent questions:
> 
> - Has this work been regression-tested on ARM / AARCH64? (For example,
> ArmVirtPkg platforms use the unified runtime DXE driver, not the split
> runtime/SMM drivers. So no change in behavior is expected; we should test
> that.)
> 
> In the "Testing Performed" section of your blurb, item#3 suggests something
> similar ("Boot from S5 to EFI shell with DXE variables enabled"), but I figured
> I'd raise AARCH64 specifically.
> 
> 

I have not tested on ARM / AARCH64. I will add this test for V3.

> - Can you please confirm that the handling of *volatile* variables is not
> affected? ArmVirtPkg and OvmfPkg use quite different sizes for volatile and
> non-volatile variables; see:
> 
>   - 9c7d0d499296 ("OvmfPkg/TlsAuthConfigLib: configure trusted CA certs
>                   for HTTPS boot", 2018-03-30)
>   - ffe048a0807b ("ArmVirtPkg: handle NETWORK_TLS_ENABLE in
>                   ArmVirtQemu*", 2019-06-28)
> 

The handling of volatile variables will not be affected.

> Thank you!
> Laszlo

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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-04  6:50           ` Laszlo Ersek
@ 2019-10-04 16:48             ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-04 16:48 UTC (permalink / raw)
  To: Laszlo Ersek, Kinney, Michael D, Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Gao, Liming, Ni, Ray,
	Wang, Jian J, Yao, Jiewen

> On 10/04/19 01:31, Kubacki, Michael A wrote:
> > I agree, I will make the default to enable the runtime cache.
> 
> I've just made a request for the opposite :) , before reading this part
> of the thread.
> 
> Let me revise my request then, seeing the above preference. From the
> following three options:
> 
> (1) set DEC default to FALSE,
> 
> (2) set the DEC default to TRUE, but set the PCD in OvmfPkg DSC files to
> FALSE,
> 
> (3) set the DEC default to TRUE, and inherit it in OvmfPkg,
> 
> I'd ask for (2) in the short or mid term, and entertain (3) in the long
> term (dependent on the upstream kernel patch I linked elsewhere in the
> thread:
> <http://mid.mail-archive.com/20191003100712.31045-1-
> javierm@redhat.com>).
> 
> 
> With the PCD available in the DEC file, I agree that downstream OVMF
> packages could easily be built with the PCD set to FALSE, e.g. on the
> "build" command line, regardless of the upstream OvmfPkg DSC setting.
> Given that, option (2) might appear superfluous.
> 
> However, I'd like upstream OvmfPkg's DSC setting to reflect that as yet,
> there is no alternative to GetVariable(), for exercising the SMM stack
> built into OVMF without side effects to the variable store. Once the
> kernel patch is merged and QueryVariableInfo() becomes *recommendable*
> as a practical substitute for GetVariable(), we can switch upstream
> OvmfPkg from option (2) to option (3).
> 
> Does that sound acceptable?
> 

I have no problem setting the PCD to FALSE in all the OvmfPkg DSC files. 

> Thanks!
> Laszlo
> 
> 
> >
> >> -----Original Message-----
> >> From: Kinney, Michael D <michael.d.kinney@intel.com>
> >> Sent: Thursday, October 3, 2019 3:01 PM
> >> To: Kubacki, Michael A <michael.a.kubacki@intel.com>; Wu, Hao A
> >> <hao.a.wu@intel.com>; devel@edk2.groups.io; Kinney, Michael D
> >> <michael.d.kinney@intel.com>
> >> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> >> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> >> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Ni, Ray
> >> <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; Yao, Jiewen
> >> <jiewen.yao@intel.com>
> >> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT
> GetVariable()
> >> cache support
> >>
> >> Michael,
> >>
> >> Perhaps the FeaturePCD for #2 should be enabled by default
> >> so the platform DSC only needs to set this PCD for some
> >> validation tests.
> >>
> >> Mike
> >>
> >>
> >>> -----Original Message-----
> >>> From: Kubacki, Michael A <michael.a.kubacki@intel.com>
> >>> Sent: Thursday, October 3, 2019 2:54 PM
> >>> To: Wu, Hao A <hao.a.wu@intel.com>; devel@edk2.groups.io
> >>> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> >>> <ard.biesheuvel@linaro.org>; Dong, Eric
> >>> <eric.dong@intel.com>; Laszlo Ersek <lersek@redhat.com>;
> >>> Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> >>> <michael.d.kinney@intel.com>; Ni, Ray
> >>> <ray.ni@intel.com>; Wang, Jian J
> >>> <jian.j.wang@intel.com>; Yao, Jiewen
> >>> <jiewen.yao@intel.com>
> >>> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> >>> RT GetVariable() cache support
> >>>
> >>> #1 - The plan is to remove the polling entirely in V3.
> >>>
> >>> #2 - I'd prefer to take a definitive direction and
> >>> reduce validation and maintenance
> >>>         effort but you and Laszlo both requested this so
> >>> I'll add a FeaturePCD to control
> >>>         activation of the runtime cache in this patch
> >>> series. Perhaps this can be removed
> >>>         in the future.
> >>>
> >>> #3 - Will be done in V3.
> >>>
> >>> Other replies are inline.
> >>>
> >>>> -----Original Message-----
> >>>> From: Wu, Hao A <hao.a.wu@intel.com>
> >>>> Sent: Thursday, October 3, 2019 1:05 AM
> >>>> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> >>>> devel@edk2.groups.io
> >>>> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> >>>> <ard.biesheuvel@linaro.org>; Dong, Eric
> >>> <eric.dong@intel.com>; Laszlo Ersek
> >>>> <lersek@redhat.com>; Gao, Liming
> >>> <liming.gao@intel.com>; Kinney, Michael
> >>>> D <michael.d.kinney@intel.com>; Ni, Ray
> >>> <ray.ni@intel.com>; Wang, Jian J
> >>>> <jian.j.wang@intel.com>; Yao, Jiewen
> >>> <jiewen.yao@intel.com>
> >>>> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> >>> RT GetVariable()
> >>>> cache support
> >>>>
> >>>> Before any comment on the patch, since I am not
> >>> experienced in the
> >>>> Variable
> >>>> driver, I would like to ask for help from other
> >>> reviewers to look into this
> >>>> patch and provide feedbacks as well. Thanks in
> >>> advance.
> >>>>
> >>>> With the above fact, some comments provided below
> >>> maybe wrong. So
> >>>> please help
> >>>> to kindly correct me.
> >>>>
> >>>>
> >>>> Some general comments:
> >>>> 1. I am not sure if bringing the TimerLib dependency
> >>> (delay in acquiring the
> >>>>    runtime cache read lock) to variable driver (a
> >>> software driver for the most
> >>>>    part) is a good idea.
> >>>>
> >>>>    Hope other reviewers can provide some feedbacks for
> >>> this. Thanks in
> >>>> advance.
> >>>>
> >>>> 2. In my opinion, I prefer a switch can be provided
> >>> for platform owners to
> >>>>    choose between using the runtime cache and going
> >>> through SMM for
> >>>> GetVariable
> >>>>    (and for GetNextVariableName in the next patch as
> >>> well).
> >>>>
> >>>>    If platform owners feel uncomfortable with using
> >>> the runtime cache with
> >>>>    regard to the security perspective, they can switch
> >>> to the origin solution.
> >>>>
> >>>> 3. Please help to remove the 'EFIAPI' keyword for new
> >>> driver internal
> >>>> functions;
> >>>>
> >>>>
> >>>> Inline comments below:
> >>>>
> >>>>
> >>>>> -----Original Message-----
> >>>>> From: Kubacki, Michael A
> >>>>> Sent: Saturday, September 28, 2019 9:47 AM
> >>>>> To: devel@edk2.groups.io
> >>>>> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo
> >>> Ersek; Gao, Liming;
> >>>> Kinney,
> >>>>> Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> >>> Jiewen
> >>>>> Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add
> >>> RT GetVariable()
> >>>>> cache support
> >>>>>
> >>>>>
> >>> REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> >>>>>
> >>>>> This change reduces SMIs for GetVariable () by
> >>> maintaining a
> >>>>> UEFI variable cache in Runtime DXE in addition to
> >>> the pre-
> >>>>> existing cache in SMRAM. When the Runtime Service
> >>> GetVariable()
> >>>>> is invoked, a Runtime DXE cache is used instead of
> >>> triggering an
> >>>>> SMI to VariableSmm. This can improve overall system
> >>> performance
> >>>>> by servicing variable read requests without
> >>> rendezvousing all
> >>>>> cores into SMM.
> >>>>>
> >>>>> The following are important points regarding this
> >>> change.
> >>>>>
> >>>>> 1. All of the non-volatile storage contents are
> >>> loaded into the
> >>>>>    cache upon driver load. This one time load
> >>> operation from storage
> >>>>>    is preferred as opposed to building the cache on
> >>> demand. An on-
> >>>>>    demand cache would require a fallback SMI to load
> >>> data into the
> >>>>>    cache as variables are requested.
> >>>>>
> >>>>> 2. SetVariable () requests will continue to always
> >>> trigger an SMI.
> >>>>>    This occurs regardless of whether the variable is
> >>> volatile or
> >>>>>    non-volatile.
> >>>>>
> >>>>> 3. Both volatile and non-volatile variables are
> >>> cached in a runtime
> >>>>>    buffer. As is the case in the current EDK II
> >>> variable driver, they
> >>>>>    continue to be cached in separate buffers.
> >>>>>
> >>>>> 4. The cache in Runtime DXE and SMM are intended to
> >>> be exact copies
> >>>>>    of one another. All SMM variable accesses only
> >>> return data from the
> >>>>>    SMM cache. The runtime caches are only updated
> >>> after the variable I/O
> >>>>>    operation is successful in SMM. The runtime
> >>> caches are only updated
> >>>>>    from SMM.
> >>>>>
> >>>>> 5. Synchronization mechanisms are in place to ensure
> >>> the runtime cache
> >>>>>    content integrity with the SMM cache. These may
> >>> result in updates to
> >>>>>    runtime cache that are the same in content but
> >>> different in offset and
> >>>>>    size from updates to the SMM cache.
> >>>>>
> >>>>> When using SMM variables, two caches will now be
> >>> present.
> >>>>> 1. "Runtime Cache" - Maintained in
> >>> VariableSmmRuntimeDxe. Used to
> >>>>> service
> >>>>>    Runtime Services GetVariable () and
> >>> GetNextVariableName () callers.
> >>>>> 2. "SMM Cache" - Maintained in VariableSmm to
> >>> service SMM GetVariable
> >>>> ()
> >>>>>    and GetNextVariableName () callers.
> >>>>>    a. This cache is retained so SMM modules do not
> >>> operate on data outside
> >>>>>       SMRAM.
> >>>>>
> >>>>> It is possible to view UEFI variable read and write
> >>> statistics by setting
> >>>>> the
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> >>> ics
> >>>>> FeaturePcd
> >>>>> to TRUE and using the VariableInfo UEFI application
> >>> in MdeModulePkg to
> >>>>> dump
> >>>>> variable statistics to the console. By doing so, a
> >>> user can view the number
> >>>>> of GetVariable () hits from the Runtime DXE variable
> >>> driver (Runtime Cache
> >>>>> hits) and the SMM variable driver (SMM Cache hits).
> >>> SMM Cache hits for
> >>>>> GetVariable () will occur when SMM modules invoke
> >>> GetVariable ().
> >>>>>
> >>>>> Cc: Dandan Bi <dandan.bi@intel.com>
> >>>>> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> >>>>> Cc: Eric Dong <eric.dong@intel.com>
> >>>>> Cc: Laszlo Ersek <lersek@redhat.com>
> >>>>> Cc: Liming Gao <liming.gao@intel.com>
> >>>>> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> >>>>> Cc: Ray Ni <ray.ni@intel.com>
> >>>>> Cc: Jian J Wang <jian.j.wang@intel.com>
> >>>>> Cc: Hao A Wu <hao.a.wu@intel.com>
> >>>>> Cc: Jiewen Yao <jiewen.yao@intel.com>
> >>>>> Signed-off-by: Michael Kubacki
> >>> <michael.a.kubacki@intel.com>
> >>>>> ---
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> >>> meDxe.inf
> >>>>> |   2 +
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.i
> >>> nf           |
> >>>> 2
> >>>>> +
> >>>>>
> >>>>>
> >>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> >>> ntimeDxe.i
> >>>>> nf |  31 +-
> >>>>>
> >>>>>
> >>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStand
> >>> aloneMm.inf
> >>>>> |   2 +
> >>>>>  MdeModulePkg/Include/Guid/SmmVariableCommon.h
> >>> |  29 +-
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> >>> |  39
> >>>> +-
> >>>>>
> >>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> >>> meCache.h
> >>>>> |  47 ++
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> >>> |  44
> >>>> +-
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRunti
> >>> meCache.c
> >>>>> | 153 +++++
> >>>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> >>> |
> >>>> 114
> >>>>> +++-
> >>>>>
> >>>>>
> >>>>
> >>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRu
> >>> ntimeDxe.
> >>>>> c   | 608 +++++++++++++++++---
> >>>>>  11 files changed, 966 insertions(+), 105
> >>> deletions(-)
> >>>>>
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeDxe.inf
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeDxe.inf
> >>>>> index 08a5490787..ceea5d1ff9 100644
> >>>>> ---
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeDxe.inf
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeDxe.inf
> >>>>> @@ -40,6 +40,8 @@
> >>>>>    VariableNonVolatile.h
> >>>>>    VariableParsing.c
> >>>>>    VariableParsing.h
> >>>>> +  VariableRuntimeCache.c
> >>>>> +  VariableRuntimeCache.h
> >>>>
> >>>>
> >>>> Per my understanding, the module specified by
> >>> VariableRuntimeDxe.inf
> >>>> does not
> >>>> involve SMM/SMI for variable services (like
> >>> GetVariable). It looks weird to
> >>>> me
> >>>> for this INF to include the newly introduced runtime
> >>> cache codes (below
> >>>> source
> >>>> header files):
> >>>>
> >>>> VariableRuntimeCache.c
> >>>> VariableRuntimeCache.h
> >>>>
> >>>>
> >>>
> >>> This is because Variable.c is common to the runtime DXE
> >>> and SMM variable
> >>> driver and it contains the code to update variable
> >>> caches. The runtime cache
> >>> synchronization function
> >>> (SynchronizeRuntimeVariableCache ()) will return
> >>> if the runtime cache pointer is NULL.
> >>>
> >>>>>    PrivilegePolymorphic.h
> >>>>>    Measurement.c
> >>>>>    TcgMorLockDxe.c
> >>>>> diff --git
> >>>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .inf
> >>>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .inf
> >>>>> index 6dc2721b81..bc3033588d 100644
> >>>>> ---
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .inf
> >>>>> +++
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .inf
> >>>>> @@ -49,6 +49,8 @@
> >>>>>    VariableNonVolatile.h
> >>>>>    VariableParsing.c
> >>>>>    VariableParsing.h
> >>>>> +  VariableRuntimeCache.c
> >>>>> +  VariableRuntimeCache.h
> >>>>>    VarCheck.c
> >>>>>    Variable.h
> >>>>>    PrivilegePolymorphic.h
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.inf
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.inf
> >>>>> index 14894e6f13..70837ac6e0 100644
> >>>>> ---
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.inf
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.inf
> >>>>> @@ -13,7 +13,7 @@
> >>>>>  #  may not be modified without authorization. If
> >>> platform fails to protect
> >>>>> these resources,
> >>>>>  #  the authentication service provided in this
> >>> driver will be broken, and the
> >>>>> behavior is undefined.
> >>>>>  #
> >>>>> -# Copyright (c) 2010 - 2017, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>> +# Copyright (c) 2010 - 2019, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>>  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>>>  #
> >>>>>  ##
> >>>>> @@ -39,6 +39,10 @@
> >>>>>    VariableSmmRuntimeDxe.c
> >>>>>    PrivilegePolymorphic.h
> >>>>>    Measurement.c
> >>>>> +  VariableParsing.c
> >>>>> +  VariableParsing.h
> >>>>> +  VariableRuntimeCache.c
> >>>>> +  VariableRuntimeCache.h
> >>>>>
> >>>>>  [Packages]
> >>>>>    MdePkg/MdePkg.dec
> >>>>> @@ -49,6 +53,7 @@
> >>>>>    BaseLib
> >>>>>    UefiBootServicesTableLib
> >>>>>    DebugLib
> >>>>> +  TimerLib
> >>>>>    UefiRuntimeLib
> >>>>>    DxeServicesTableLib
> >>>>>    UefiDriverEntryPoint
> >>>>> @@ -65,7 +70,29 @@
> >>>>>    gEdkiiVariableLockProtocolGuid                ##
> >>> PRODUCES
> >>>>>    gEdkiiVarCheckProtocolGuid                    ##
> >>> PRODUCES
> >>>>>
> >>>>> +[Pcd]
> >>>>> +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize
> >>> ##
> >>>>> CONSUMES
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
> >>>> ##
> >>>>> CONSUMES
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariab
> >>> leSize
> >>>>> ## CONSUMES
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize
> >>> ##
> >>>>> CONSUMES
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize
> >>> ##
> >>>>> CONSUMES
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpace
> >>> Size
> >>>>> ## CONSUMES
> >>>>> +
> >>>>>
> >>>>
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVari
> >>> ableSpace
> >>>>> Size  ## CONSUMES
> >>>>
> >>>>
> >>>> Not sure if the above PCDs are really needed by
> >>> VariableSmmRuntimeDxe.
> >>>>
> >>>>
> >>>
> >>> I will double check and remove any not required.
> >>>
> >>>>> +
> >>>>> +[FeaturePcd]
> >>>>> +
> >>> gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatist
> >>> ics
> >>>> ##
> >>>>> CONSUMES
> >>>>> +
> >>>>>  [Guids]
> >>>>> +  ## PRODUCES             ## GUID # Signature of
> >>> Variable store header
> >>>>> +  ## CONSUMES             ## GUID # Signature of
> >>> Variable store header
> >>>>> +  ## SOMETIMES_PRODUCES   ## SystemTable
> >>>>> +  gEfiAuthenticatedVariableGuid
> >>>>> +
> >>>>> +  ## PRODUCES             ## GUID # Signature of
> >>> Variable store header
> >>>>> +  ## CONSUMES             ## GUID # Signature of
> >>> Variable store header
> >>>>> +  ## SOMETIMES_PRODUCES   ## SystemTable
> >>>>> +  gEfiVariableGuid
> >>>>> +
> >>>>>    gEfiEventVirtualAddressChangeGuid             ##
> >>> CONSUMES ## Event
> >>>>>    gEfiEventExitBootServicesGuid                 ##
> >>> CONSUMES ## Event
> >>>>>    ## CONSUMES ## GUID # Locate protocol
> >>>>> @@ -82,6 +109,8 @@
> >>>>>    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> >>>>>    gEfiImageSecurityDatabaseGuid
> >>>>>
> >>>>> +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> >>>>> SOMETIMES_CONSUMES ## SystemTable
> >>>>> +
> >>>>>  [Depex]
> >>>>>    gEfiSmmCommunicationProtocolGuid
> >>>>>
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> >>> ndaloneMm.i
> >>>>> nf
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> >>> ndaloneMm.
> >>>>> inf
> >>>>> index ca9d23ce9f..95c5310c0b 100644
> >>>>> ---
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> >>> ndaloneMm.i
> >>>>> nf
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSta
> >>> ndaloneMm.
> >>>>> inf
> >>>>> @@ -49,6 +49,8 @@
> >>>>>    VariableNonVolatile.h
> >>>>>    VariableParsing.c
> >>>>>    VariableParsing.h
> >>>>> +  VariableRuntimeCache.c
> >>>>> +  VariableRuntimeCache.h
> >>>>>    VarCheck.c
> >>>>>    Variable.h
> >>>>>    PrivilegePolymorphic.h
> >>>>> diff --git
> >>> a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> >>>>> b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> >>>>> index c527a59891..ceef44dfd2 100644
> >>>>> --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> >>>>> +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> >>>>> @@ -1,7 +1,7 @@
> >>>>>  /** @file
> >>>>>    The file defined some common structures used for
> >>> communicating
> >>>>> between SMM variable module and SMM variable wrapper
> >>> module.
> >>>>>
> >>>>> -Copyright (c) 2011 - 2015, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>> +Copyright (c) 2011 - 2019, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>>  SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>>>
> >>>>>  **/
> >>>>> @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-
> >>> Clause-Patent
> >>>>>  #ifndef _SMM_VARIABLE_COMMON_H_
> >>>>>  #define _SMM_VARIABLE_COMMON_H_
> >>>>>
> >>>>> +#include <Guid/VariableFormat.h>
> >>>>>  #include <Protocol/VarCheck.h>
> >>>>>
> >>>>>  #define EFI_SMM_VARIABLE_WRITE_GUID \
> >>>>> @@ -66,6 +67,16 @@ typedef struct {
> >>>>>  #define
> >>>>>
> >>> SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
> >>> 10
> >>>>>
> >>>>>  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
> >>> 11
> >>>>> +//
> >>>>> +// The payload for this function is
> >>>>>
> >>>
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>>>> +//
> >>>>> +#define
> >>>>>
> >>>
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> >>> T
> >>>>> 12
> >>>>> +
> >>>>> +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
> >>>> 13
> >>>>> +//
> >>>>> +// The payload for this function is
> >>>>> SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> >>>>> +//
> >>>>> +#define
> >>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> >>>>> 14
> >>>>>
> >>>>>  ///
> >>>>>  /// Size of SMM communicate header, without
> >>> including the payload.
> >>>>> @@ -120,4 +131,20 @@ typedef struct {
> >>>>>    UINTN
> >>> VariablePayloadSize;
> >>>>>  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> >>>>>
> >>>>> +typedef struct {
> >>>>> +  BOOLEAN                 *ReadLock;
> >>>>> +  BOOLEAN                 *PendingUpdate;
> >>>>> +  BOOLEAN                 *HobFlushComplete;
> >>>>> +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> >>>>> +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> >>>>> +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> >>>>> +}
> >>>>>
> >>>>
> >>>
> >>
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> >>>>> +
> >>>>> +typedef struct {
> >>>>> +  UINTN                   TotalHobStorageSize;
> >>>>> +  UINTN                   TotalNvStorageSize;
> >>>>> +  UINTN                   TotalVolatileStorageSize;
> >>>>> +  BOOLEAN
> >>> AuthenticatedVariableUsage;
> >>>>> +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> >>>>> +
> >>>>>  #endif // _SMM_VARIABLE_COMMON_H_
> >>>>> diff --git
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> >>>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> >>>>> index fb574b2e32..b9723c0250 100644
> >>>>> ---
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> >>>>> +++
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> >>>>> @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-
> >>> Clause-Patent
> >>>>>  ///
> >>>>>  #define ISO_639_2_ENTRY_SIZE    3
> >>>>>
> >>>>> +///
> >>>>> +/// The timeout to in 10us units to wait for the
> >>>>> +/// variable runtime cache read lock to be
> >>> acquired.
> >>>>> +///
> >>>>> +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> >>>>> +
> >>>>>  typedef enum {
> >>>>>    VariableStoreTypeVolatile,
> >>>>>    VariableStoreTypeHob,
> >>>>> @@ -64,6 +70,21 @@ typedef enum {
> >>>>>    VariableStoreTypeMax
> >>>>>  } VARIABLE_STORE_TYPE;
> >>>>>
> >>>>> +typedef struct {
> >>>>> +  UINT32                  PendingUpdateOffset;
> >>>>> +  UINT32                  PendingUpdateLength;
> >>>>> +  VARIABLE_STORE_HEADER   *Store;
> >>>>> +} VARIABLE_RUNTIME_CACHE;
> >>>>> +
> >>>>> +typedef struct {
> >>>>> +  BOOLEAN                 *ReadLock;
> >>>>> +  BOOLEAN                 *PendingUpdate;
> >>>>> +  BOOLEAN                 *HobFlushComplete;
> >>>>> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> >>>>> +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> >>>>> +  VARIABLE_RUNTIME_CACHE
> >>> VariableRuntimeVolatileCache;
> >>>>> +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> >>>>> +
> >>>>>  typedef struct {
> >>>>>    VARIABLE_HEADER *CurrPtr;
> >>>>>    //
> >>>>> @@ -79,14 +100,16 @@ typedef struct {
> >>>>>  } VARIABLE_POINTER_TRACK;
> >>>>>
> >>>>>  typedef struct {
> >>>>> -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> >>>>> -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> >>>>> -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> >>>>> -  EFI_LOCK              VariableServicesLock;
> >>>>> -  UINT32                ReentrantState;
> >>>>> -  BOOLEAN               AuthFormat;
> >>>>> -  BOOLEAN               AuthSupport;
> >>>>> -  BOOLEAN               EmuNvMode;
> >>>>> +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> >>>>> +  EFI_PHYSICAL_ADDRESS
> >>> HobVariableBackupBase;
> >>>>
> >>>>
> >>>> I do not see any usage of the new field
> >>> "HobVariableBackupBase".
> >>>> Could you help to double confirm?
> >>>>
> >>>>
> >>>
> >>> You are correct. I removed usage of this variable before
> >>> sending the
> >>> patch series and the global variable here needs to be
> >>> cleaned up.
> >>>
> >>>>> +  EFI_PHYSICAL_ADDRESS
> >>> VolatileVariableBase;
> >>>>> +  EFI_PHYSICAL_ADDRESS
> >>> NonVolatileVariableBase;
> >>>>> +  VARIABLE_RUNTIME_CACHE_CONTEXT
> >>> VariableRuntimeCacheContext;
> >>>>> +  EFI_LOCK
> >>> VariableServicesLock;
> >>>>> +  UINT32                          ReentrantState;
> >>>>> +  BOOLEAN                         AuthFormat;
> >>>>> +  BOOLEAN                         AuthSupport;
> >>>>> +  BOOLEAN                         EmuNvMode;
> >>>>>  } VARIABLE_GLOBAL;
> >>>>>
> >>>>>  typedef struct {
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.h
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.
> >>>>> h
> >>>>> new file mode 100644
> >>>>> index 0000000000..09b83eb215
> >>>>> --- /dev/null
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.
> >>>>> h
> >>>>> @@ -0,0 +1,47 @@
> >>>>> +/** @file
> >>>>> +  The common variable volatile store routines
> >>> shared by the
> >>>> DXE_RUNTIME
> >>>>> variable
> >>>>> +  module and the DXE_SMM variable module.
> >>>>> +
> >>>>> +Copyright (c) 2019, Intel Corporation. All rights
> >>> reserved.<BR>
> >>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>>> +
> >>>>> +**/
> >>>>> +
> >>>>> +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> >>>>> +#define _VARIABLE_RUNTIME_CACHE_H_
> >>>>> +
> >>>>> +#include "Variable.h"
> >>>>> +
> >>>>> +/**
> >>>>> +  Copies any pending updates to runtime variable
> >>> caches.
> >>>>> +
> >>>>> +  @retval EFI_UNSUPPORTED         The volatile
> >>> store to be updated is not
> >>>>> initialized properly.
> >>>>> +  @retval EFI_SUCCESS             The volatile
> >>> store was updated successfully.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +SynchronizeRuntimeVariableCacheEx (
> >>>>> +  VOID
> >>>>> +  );
> >>>>> +
> >>>>> +/**
> >>>>> +  Synchronizes the runtime variable caches with all
> >>> pending updates
> >>>> outside
> >>>>> runtime.
> >>>>> +
> >>>>> +  Ensures all conditions are met to maintain
> >>> coherency for runtime cache
> >>>>> updates.
> >>>>> +
> >>>>> +  @param[in] VariableRuntimeCache Variable runtime
> >>> cache structure for
> >>>>> the runtime cache being synchronized.
> >>>>> +  @param[in] Offset               Offset in bytes
> >>> to apply the update.
> >>>>> +  @param[in] Length               Length of data in
> >>> bytes of the update.
> >>>>> +
> >>>>> +  @retval EFI_UNSUPPORTED         The volatile
> >>> store to be updated is not
> >>>>> initialized properly.
> >>>>> +  @retval EFI_SUCCESS             The volatile
> >>> store was updated successfully.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +SynchronizeRuntimeVariableCache (
> >>>>> +  IN  VARIABLE_RUNTIME_CACHE
> >>> *VariableRuntimeCache,
> >>>>> +  IN  UINTN                           Offset,
> >>>>> +  IN  UINTN                           Length
> >>>>> +  );
> >>>>> +
> >>>>> +#endif
> >>>>> diff --git
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> >>>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> >>>>> index 5da2354aa5..bb2fa3fc19 100644
> >>>>> ---
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> >>>>> +++
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> >>>>> @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-
> >>> Clause-Patent
> >>>>>  #include "Variable.h"
> >>>>>  #include "VariableNonVolatile.h"
> >>>>>  #include "VariableParsing.h"
> >>>>> +#include "VariableRuntimeCache.h"
> >>>>>
> >>>>>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> >>>>>
> >>>>> @@ -332,6 +333,12 @@ RecordVarErrorFlag (
> >>>>>        // Update the data in NV cache.
> >>>>>        //
> >>>>>        *VarErrFlag = TempFlag;
> >>>>> +      Status =  SynchronizeRuntimeVariableCache (
> >>>>> +                  &mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache,
> >>>>> +                  (UINTN) VarErrFlag - (UINTN)
> >>> mNvVariableCache + (UINTN)
> >>>>> mVariableModuleGlobal-
> >>>> VariableGlobal.NonVolatileVariableBase,
> >>>>> +                  sizeof (TempFlag)
> >>>>> +                  );
> >>>>> +      ASSERT_EFI_ERROR (Status);
> >>>>>      }
> >>>>>    }
> >>>>>  }
> >>>>> @@ -755,12 +762,24 @@ Reclaim (
> >>>>>
> >>>>>  Done:
> >>>>>    if (IsVolatile || mVariableModuleGlobal-
> >>>> VariableGlobal.EmuNvMode) {
> >>>>> +    Status =  SynchronizeRuntimeVariableCache (
> >>>>> +                &mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e,
> >>>>> +                0,
> >>>>> +                VariableStoreHeader->Size
> >>>>> +                );
> >>>>> +    ASSERT_EFI_ERROR (Status);
> >>>>>      FreePool (ValidBuffer);
> >>>>>    } else {
> >>>>>      //
> >>>>>      // For NV variable reclaim, we use
> >>> mNvVariableCache as the buffer, so
> >>>>> copy the data back.
> >>>>>      //
> >>>>> -    CopyMem (mNvVariableCache, (UINT8
> >>> *)(UINTN)VariableBase,
> >>>>> VariableStoreHeader->Size);
> >>>>> +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN)
> >>> VariableBase,
> >>>>> VariableStoreHeader->Size);
> >>>>> +    Status =  SynchronizeRuntimeVariableCache (
> >>>>> +                &(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache),
> >>>>> +                0,
> >>>>> +                VariableStoreHeader->Size
> >>>>> +                );
> >>>>> +    ASSERT_EFI_ERROR (Status);
> >>>>>    }
> >>>>>
> >>>>>    return Status;
> >>>>> @@ -1592,6 +1611,7 @@ UpdateVariable (
> >>>>>    VARIABLE_POINTER_TRACK              *Variable;
> >>>>>    VARIABLE_POINTER_TRACK              NvVariable;
> >>>>>    VARIABLE_STORE_HEADER
> >>> *VariableStoreHeader;
> >>>>> +  VARIABLE_RUNTIME_CACHE
> >>> *VolatileCacheInstance;
> >>>>>    UINT8
> >>> *BufferForMerge;
> >>>>>    UINTN
> >>> MergedBufSize;
> >>>>>    BOOLEAN                             DataReady;
> >>>>> @@ -2235,6 +2255,21 @@ UpdateVariable (
> >>>>>    }
> >>>>>
> >>>>>  Done:
> >>>>> +  if (!EFI_ERROR (Status)) {
> >>>>> +    if (Variable->Volatile) {
> >>>>> +      VolatileCacheInstance =
> >>> &(mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e);
> >>>>> +    } else {
> >>>>> +      VolatileCacheInstance =
> >>> &(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache);
> >>>>> +    }
> >>>>> +
> >>>>> +    Status =  SynchronizeRuntimeVariableCache (
> >>>>> +                VolatileCacheInstance,
> >>>>> +                0,
> >>>>> +                VolatileCacheInstance->Store->Size
> >>>>> +                );
> >>>>> +    ASSERT_EFI_ERROR (Status);
> >>>>> +  }
> >>>>> +
> >>>>>    return Status;
> >>>>>  }
> >>>>>
> >>>>> @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
> >>>>>          ErrorFlag = TRUE;
> >>>>>        }
> >>>>>      }
> >>>>> +    Status =  SynchronizeRuntimeVariableCache (
> >>>>> +                &mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache,
> >>>>> +                0,
> >>>>> +                mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.S
> >>>>> tore->Size
> >>>>> +                );
> >>>>> +    ASSERT_EFI_ERROR (Status);
> >>>>>      if (ErrorFlag) {
> >>>>>        //
> >>>>>        // We still have HOB variable(s) not flushed
> >>> in flash.
> >>>>> @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
> >>>>>        // All HOB variables have been flushed in
> >>> flash.
> >>>>>        //
> >>>>>        DEBUG ((EFI_D_INFO, "Variable driver: all HOB
> >>> variables have been
> >>>>> flushed in flash.\n"));
> >>>>> +      *(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.HobFlushComp
> >>> lete) =
> >>>> TRUE;
> >>>>>        if (!AtRuntime ()) {
> >>>>>          FreePool ((VOID *) VariableStoreHeader);
> >>>>>        }
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.c
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.c
> >>>>> new file mode 100644
> >>>>> index 0000000000..2642d9b000
> >>>>> --- /dev/null
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRun
> >>> timeCache.c
> >>>>> @@ -0,0 +1,153 @@
> >>>>> +/** @file
> >>>>> +  The common variable volatile store routines
> >>> shared by the
> >>>> DXE_RUNTIME
> >>>>> variable
> >>>>> +  module and the DXE_SMM variable module.
> >>>>> +
> >>>>> +  Caution: This module requires additional review
> >>> when modified.
> >>>>> +  This driver will have external input - variable
> >>> data. They may be input in
> >>>>> SMM mode.
> >>>>> +  This external input must be validated carefully
> >>> to avoid security issue like
> >>>>> +  buffer overflow, integer overflow.
> >>>>> +
> >>>>> +Copyright (c) 2019, Intel Corporation. All rights
> >>> reserved.<BR>
> >>>>> +SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>>> +
> >>>>> +**/
> >>>>> +
> >>>>> +#include "VariableParsing.h"
> >>>>> +#include "VariableRuntimeCache.h"
> >>>>> +
> >>>>> +extern VARIABLE_MODULE_GLOBAL
> >>> *mVariableModuleGlobal;
> >>>>> +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> >>>>> +
> >>>>> +/**
> >>>>> +  Copies any pending updates to runtime variable
> >>> caches.
> >>>>> +
> >>>>> +  @retval EFI_UNSUPPORTED         The volatile
> >>> store to be updated is not
> >>>>> initialized properly.
> >>>>> +  @retval EFI_SUCCESS             The volatile
> >>> store was updated successfully.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +SynchronizeRuntimeVariableCacheEx (
> >>>>
> >>>>
> >>>> It is not clear to me why this function is named as
> >>> the "Ex" version of function
> >>>> SynchronizeRuntimeVariableCache(). For me, this
> >>> function looks more like a
> >>>> basic
> >>>> version.
> >>>>
> >>>> I would suggest a name change for the functions
> >>> provided in file
> >>>> VariableRuntimeCache.c to better reflect their usage
> >>> model.
> >>>>
> >>>>
> >>>
> >>> I'll rename it in V3.
> >>>
> >>>>> +  VOID
> >>>>> +  )
> >>>>> +{
> >>>>
> >>>>
> >>>> I would recommend that at least a local variable
> >>> should be introduced to
> >>>> reduce
> >>>> the duplications of:
> >>>>
> >>>> "mVariableModuleGlobal-
> >>>> VariableGlobal.VariableRuntimeCacheContext"
> >>>>
> >>>> in this function in order to make it easier to read.
> >>>>
> >>>>
> >>>
> >>> I'll add it in V3.
> >>>
> >>>>> +  if (
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.St
> >>>>> ore == NULL ||
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.Store == NULL ||
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e == NULL
> >>>>> +    ) {
> >>>>> +    return EFI_UNSUPPORTED;
> >>>>> +  }
> >>>>> +
> >>>>> +  if (*(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e)) {
> >>>>> +    if (
> >>>>> +      mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.S
> >>>>> tore != NULL &&
> >>>>> +      mVariableModuleGlobal-
> >>>> VariableGlobal.HobVariableBase > 0
> >>>>> +      ) {
> >>>>> +      CopyMem (
> >>>>> +        (VOID *) (
> >>>>> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.S
> >>>>> tore) +
> >>>>> +          mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.P
> >>>>> endingUpdateOffset
> >>>>> +          ),
> >>>>> +        (VOID *) (
> >>>>> +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >>>>>> VariableGlobal.HobVariableBase) +
> >>>>> +          mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.P
> >>>>> endingUpdateOffset
> >>>>> +          ),
> >>>>> +        mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.P
> >>>>> endingUpdateLength
> >>>>> +        );
> >>>>> +      mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.P
> >>>>> endingUpdateLength = 0;
> >>>>> +      mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeHobCache.P
> >>>>> endingUpdateOffset = 0;
> >>>>> +    }
> >>>>> +
> >>>>> +    CopyMem (
> >>>>> +      (VOID *) (
> >>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.St
> >>>>> ore) +
> >>>>> +        mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.Pe
> >>>>> ndingUpdateOffset
> >>>>> +        ),
> >>>>> +      (VOID *) (
> >>>>> +        ((UINT8 *) (UINTN) mNvVariableCache) +
> >>>>> +        mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.Pe
> >>>>> ndingUpdateOffset
> >>>>> +        ),
> >>>>> +      mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.Pe
> >>>>> ndingUpdateLength
> >>>>> +      );
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.Pe
> >>>>> ndingUpdateLength = 0;
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeNvCache.Pe
> >>>>> ndingUpdateOffset = 0;
> >>>>> +
> >>>>> +    CopyMem (
> >>>>> +      (VOID *) (
> >>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.Store) +
> >>>>> +        mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.PendingUpdateOffset
> >>>>> +      ),
> >>>>> +      (VOID *) (
> >>>>> +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> >>>>>> VariableGlobal.VolatileVariableBase) +
> >>>>> +        mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.PendingUpdateOffset
> >>>>> +        ),
> >>>>> +      mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.PendingUpdateLength
> >>>>> +      );
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.PendingUpdateLength = 0;
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.VariableRunt
> >>> imeVolatileCach
> >>>>> e.PendingUpdateOffset = 0;
> >>>>> +    *(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e) = FALSE;
> >>>>> +  }
> >>>>> +
> >>>>> +  return EFI_SUCCESS;
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> +  Synchronizes the runtime variable caches with all
> >>> pending updates
> >>>> outside
> >>>>> runtime.
> >>>>> +
> >>>>> +  Ensures all conditions are met to maintain
> >>> coherency for runtime cache
> >>>>> updates.
> >>>>> +
> >>>>> +  @param[in] VariableRuntimeCache Variable runtime
> >>> cache structure for
> >>>>> the runtime cache being synchronized.
> >>>>> +  @param[in] Offset               Offset in bytes
> >>> to apply the update.
> >>>>> +  @param[in] Length               Length of data in
> >>> bytes of the update.
> >>>>> +
> >>>>> +  @retval EFI_UNSUPPORTED         The volatile
> >>> store to be updated is not
> >>>>> initialized properly.
> >>>>> +  @retval EFI_SUCCESS             The volatile
> >>> store was updated successfully.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +SynchronizeRuntimeVariableCache (
> >>>>> +  IN  VARIABLE_RUNTIME_CACHE
> >>> *VariableRuntimeCache,
> >>>>> +  IN  UINTN                           Offset,
> >>>>> +  IN  UINTN                           Length
> >>>>> +  )
> >>>>> +{
> >>>>> +  if (VariableRuntimeCache == NULL) {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  } else if (VariableRuntimeCache->Store == NULL) {
> >>>>> +      // Runtime cache is not available yet at this
> >>> point,
> >>>>> +      // Return EFI_SUCCESS instead of
> >>> EFI_NOT_AVAILABLE_YET to let it
> >>>>> progress
> >>>>> +      return EFI_SUCCESS;
> >>>>> +  }
> >>>>> +
> >>>>> +  if (
> >>>>> +    mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e == NULL
> >>>> ||
> >>>>> +    mVariableModuleGlobal-
> >>>>>> VariableGlobal.VariableRuntimeCacheContext.ReadLock
> >>> == NULL
> >>>>> +    ) {
> >>>>> +    return EFI_UNSUPPORTED;
> >>>>> +  }
> >>>>> +
> >>>>> +  if (
> >>>>> +    *(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e) &&
> >>>>> +    VariableRuntimeCache->PendingUpdateLength > 0
> >>>>> +    ) {
> >>>>> +    VariableRuntimeCache->PendingUpdateLength =
> >>>>> +      (UINT32) (
> >>>>> +        MAX (
> >>>>> +          (UINTN) (VariableRuntimeCache-
> >>>> PendingUpdateOffset +
> >>>>> VariableRuntimeCache->PendingUpdateLength),
> >>>>> +          Offset + Length
> >>>>> +        ) - MIN ((UINTN) VariableRuntimeCache-
> >>>> PendingUpdateOffset,
> >>>> Offset)
> >>>>> +      );
> >>>>> +    VariableRuntimeCache->PendingUpdateOffset =
> >>>>> +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
> >>>>> PendingUpdateOffset,
> >>>>> Offset);
> >>>>> +  } else {
> >>>>> +    VariableRuntimeCache->PendingUpdateLength =
> >>> (UINT32) Length;
> >>>>> +    VariableRuntimeCache->PendingUpdateOffset =
> >>> (UINT32) Offset;
> >>>>> +  }
> >>>>> +  *(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.PendingUpdat
> >>> e) = TRUE;
> >>>>> +
> >>>>> +  if (*(mVariableModuleGlobal-
> >>>>>
> >>>> VariableGlobal.VariableRuntimeCacheContext.ReadLock) ==
> >>> FALSE) {
> >>>>> +    return SynchronizeRuntimeVariableCacheEx ();
> >>>>> +  }
> >>>>> +
> >>>>> +  return EFI_SUCCESS;
> >>>>> +}
> >>>>> diff --git
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .c
> >>>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .c
> >>>>> index ce409f22a3..8d767f75ac 100644
> >>>>> ---
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .c
> >>>>> +++
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> .c
> >>>>> @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-
> >>> Clause-Patent
> >>>>>  #include <Guid/SmmVariableCommon.h>
> >>>>>  #include "Variable.h"
> >>>>>  #include "VariableParsing.h"
> >>>>> +#include "VariableRuntimeCache.h"
> >>>>> +
> >>>>> +extern VARIABLE_STORE_HEADER
> >>> *mNvVariableCache;
> >>>>>
> >>>>>  BOOLEAN
> >>> mAtRuntime              = FALSE;
> >>>>>  UINT8
> >>> *mVariableBufferPayload = NULL;
> >>>>> @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
> >>>>>  EFI_STATUS
> >>>>>  EFIAPI
> >>>>>  SmmVariableHandler (
> >>>>> -  IN     EFI_HANDLE
> >>> DispatchHandle,
> >>>>> -  IN     CONST VOID
> >>> *RegisterContext,
> >>>>> -  IN OUT VOID
> >>> *CommBuffer,
> >>>>> -  IN OUT UINTN
> >>> *CommBufferSize
> >>>>> +  IN     EFI_HANDLE
> >>> DispatchHandle,
> >>>>> +  IN     CONST VOID
> >>> *RegisterContext,
> >>>>> +  IN OUT VOID
> >>> *CommBuffer,
> >>>>> +  IN OUT UINTN
> >>> *CommBufferSize
> >>>>>    )
> >>>>>  {
> >>>>> -  EFI_STATUS
> >>> Status;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_HEADER
> >>>>> *SmmVariableFunctionHeader;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> >>>>> *SmmVariableHeader;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> >>>>> *GetNextVariableName;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> >>>>> *QueryVariableInfo;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> >>>>> *GetPayloadSize;
> >>>>> -  VARIABLE_INFO_ENTRY
> >>> *VariableInfo;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> >>>> *VariableToLock;
> >>>>> -
> >>> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> >>>>> *CommVariableProperty;
> >>>>> -  UINTN
> >>> InfoSize;
> >>>>> -  UINTN
> >>> NameBufferSize;
> >>>>> -  UINTN
> >>> CommBufferPayloadSize;
> >>>>> -  UINTN
> >>> TempCommBufferSize;
> >>>>> +  EFI_STATUS
> >>> Status;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
> >>>>> *SmmVariableFunctionHeader;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> >>>>> *SmmVariableHeader;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> >>>>> *GetNextVariableName;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> >>>>> *QueryVariableInfo;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> >>>>> *GetPayloadSize;
> >>>>> +
> >>>>>
> >>>
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>>>> *RuntimeVariableCacheContext;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> >>>>> *GetRuntimeCacheInfo;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> >>>>> *VariableToLock;
> >>>>> +
> >>> SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> >>>>> *CommVariableProperty;
> >>>>> +  VARIABLE_INFO_ENTRY
> >>> *VariableInfo;
> >>>>> +  VARIABLE_RUNTIME_CACHE_CONTEXT
> >>>>> *VariableCacheContext;
> >>>>> +  VARIABLE_STORE_HEADER
> >>> *VariableCache;
> >>>>> +  UINTN
> >>> InfoSize;
> >>>>> +  UINTN
> >>> NameBufferSize;
> >>>>> +  UINTN
> >>> CommBufferPayloadSize;
> >>>>> +  UINTN
> >>> TempCommBufferSize;
> >>>>>
> >>>>>    //
> >>>>>    // If input is invalid, stop processing this SMI
> >>>>> @@ -789,6 +796,79 @@ SmmVariableHandler (
> >>>>>                   );
> >>>>>        CopyMem (SmmVariableFunctionHeader->Data,
> >>>> mVariableBufferPayload,
> >>>>> CommBufferPayloadSize);
> >>>>>        break;
> >>>>> +    case
> >>>>>
> >>>>
> >>>
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> >>> T:
> >>>>> +      if (CommBufferPayloadSize < sizeof
> >>>>>
> >>>>
> >>>
> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTE
> >>> XT)
> >>>> )
> >>>>> {
> >>>>
> >>>>
> >>>> The above check is not correct, I think it should be:
> >>>>
> >>>> if (CommBufferPayloadSize < sizeof
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>> )
> >>>> ) {
> >>>>
> >>>> Please help to double confirm.
> >>>> Also, I recommend some security tests should be
> >>> performed to these new
> >>>> cases in
> >>>> the variable SMI handler.
> >>>>
> >>>>
> >>>
> >>> You're right. The wrong macro was simply copied.
> >>>
> >>>>> +        DEBUG ((DEBUG_ERROR,
> >>> "InitRuntimeVariableCacheContext: SMM
> >>>>> communication buffer size invalid!\n"));
> >>>>> +      } else if (mEndOfDxe) {
> >>>>> +        DEBUG ((DEBUG_ERROR,
> >>> "InitRuntimeVariableCacheContext: Cannot
> >>>>> init context after end of DXE!\n"));
> >>>>> +      } else {
> >>>>> +        RuntimeVariableCacheContext =
> >>>>>
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>>>> *) SmmVariableFunctionHeader->Data;
> >>>>
> >>>>
> >>>> Not sure on this one:
> >>>>
> >>>> Do you think it is necessary to copy the contents in
> >>> the comm buffer to the
> >>>> pre-allocated SMM variable buffer payload
> >>> 'mVariableBufferPayload' to
> >>>> avoid
> >>>> TOCTOU issue? Since there are some tests (sort of, a
> >>> couple of ASSERTs)
> >>>> based
> >>>> on the comm buffer content.
> >>>>
> >>>>
> >>>
> >>> I understand the TOCTOU observation. But is this still a
> >>> concern with all the
> >>> cores rendezvoused in SMM prior to end of DXE?
> >>>
> >>>>> +        VariableCacheContext =
> >>> &mVariableModuleGlobal-
> >>>>>> VariableGlobal.VariableRuntimeCacheContext;
> >>>>> +
> >>>>> +        ASSERT (RuntimeVariableCacheContext-
> >>>> RuntimeVolatileCache !=
> >>>>> NULL);
> >>>>> +        ASSERT (RuntimeVariableCacheContext-
> >>>> RuntimeNvCache != NULL);
> >>>>> +        ASSERT (RuntimeVariableCacheContext-
> >>>> PendingUpdate != NULL);
> >>>>> +        ASSERT (RuntimeVariableCacheContext-
> >>>> ReadLock != NULL);
> >>>>> +        ASSERT (RuntimeVariableCacheContext-
> >>>> HobFlushComplete !=
> >>>> NULL);
> >>>>> +
> >>>>> +        VariableCacheContext-
> >>>> VariableRuntimeHobCache.Store      =
> >>>>> RuntimeVariableCacheContext->RuntimeHobCache;
> >>>>> +        VariableCacheContext-
> >>>> VariableRuntimeVolatileCache.Store =
> >>>>> RuntimeVariableCacheContext->RuntimeVolatileCache;
> >>>>> +        VariableCacheContext-
> >>>> VariableRuntimeNvCache.Store       =
> >>>>> RuntimeVariableCacheContext->RuntimeNvCache;
> >>>>> +        VariableCacheContext->PendingUpdate
> >>> =
> >>>>> RuntimeVariableCacheContext->PendingUpdate;
> >>>>> +        VariableCacheContext->ReadLock
> >>> =
> >>>>> RuntimeVariableCacheContext->ReadLock;
> >>>>> +        VariableCacheContext->HobFlushComplete
> >>> =
> >>>>> RuntimeVariableCacheContext->HobFlushComplete;
> >>>>> +
> >>>>> +        // Set up the intial pending request since
> >>> the RT cache needs to be in
> >>>>> sync with SMM cache
> >>>>> +        if (mVariableModuleGlobal-
> >>>> VariableGlobal.HobVariableBase == 0) {
> >>>>> +          VariableCacheContext-
> >>>>>> VariableRuntimeHobCache.PendingUpdateOffset = 0;
> >>>>> +          VariableCacheContext-
> >>>>>> VariableRuntimeHobCache.PendingUpdateLength = 0;
> >>>>> +        } else {
> >>>>> +          VariableCache = (VARIABLE_STORE_HEADER *)
> >>> (UINTN)
> >>>>> mVariableModuleGlobal-
> >>>> VariableGlobal.HobVariableBase;
> >>>>> +          VariableCacheContext-
> >>>>>> VariableRuntimeHobCache.PendingUpdateOffset = 0;
> >>>>> +          VariableCacheContext-
> >>>>>> VariableRuntimeHobCache.PendingUpdateLength =
> >>> (UINT32) ((UINTN)
> >>>>> GetEndPointer (VariableCache) - (UINTN)
> >>> VariableCache);
> >>>>> +          CopyGuid (&(VariableCacheContext-
> >>>>>> VariableRuntimeHobCache.Store->Signature),
> >>> &(VariableCache-
> >>>>>> Signature));
> >>>>> +        }
> >>>>> +        VariableCache = (VARIABLE_STORE_HEADER  *)
> >>> (UINTN)
> >>>>> mVariableModuleGlobal-
> >>>> VariableGlobal.VolatileVariableBase;
> >>>>> +        VariableCacheContext-
> >>>>>> VariableRuntimeVolatileCache.PendingUpdateOffset
> >>> = 0;
> >>>>> +        VariableCacheContext-
> >>>>>> VariableRuntimeVolatileCache.PendingUpdateLength
> >>> = (UINT32)
> >>>> ((UINTN)
> >>>>> GetEndPointer (VariableCache) - (UINTN)
> >>> VariableCache);
> >>>>> +        CopyGuid (&(VariableCacheContext-
> >>>>>> VariableRuntimeVolatileCache.Store->Signature),
> >>> &(VariableCache-
> >>>>>> Signature));
> >>>>> +
> >>>>> +        VariableCache = (VARIABLE_STORE_HEADER  *)
> >>> (UINTN)
> >>>>> mNvVariableCache;
> >>>>> +        VariableCacheContext-
> >>>>>> VariableRuntimeNvCache.PendingUpdateOffset = 0;
> >>>>> +        VariableCacheContext-
> >>>>>> VariableRuntimeNvCache.PendingUpdateLength =
> >>> (UINT32) ((UINTN)
> >>>>> GetEndPointer (VariableCache) - (UINTN)
> >>> VariableCache);
> >>>>> +        CopyGuid (&(VariableCacheContext-
> >>>>> VariableRuntimeNvCache.Store-
> >>>>>> Signature), &(VariableCache->Signature));
> >>>>> +
> >>>>> +        *(VariableCacheContext->PendingUpdate) =
> >>> TRUE;
> >>>>> +        *(VariableCacheContext->ReadLock) = FALSE;
> >>>>> +        *(VariableCacheContext->HobFlushComplete) =
> >>> FALSE;
> >>>>> +      }
> >>>>> +      Status = EFI_SUCCESS;
> >>>>> +      break;
> >>>>> +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> >>>>> +      Status = SynchronizeRuntimeVariableCacheEx
> >>> ();
> >>>>> +      break;
> >>>>> +    case
> >>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> >>>>> +      if (CommBufferPayloadSize < sizeof
> >>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> >>>>> +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo:
> >>> SMM
> >>>> communication
> >>>>> buffer size invalid!\n"));
> >>>>> +        return EFI_SUCCESS;
> >>>>> +      }
> >>>>> +      GetRuntimeCacheInfo =
> >>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> >>>>> SmmVariableFunctionHeader->Data;
> >>>>> +
> >>>>> +      if (mVariableModuleGlobal-
> >>>> VariableGlobal.HobVariableBase > 0) {
> >>>>> +        VariableCache = (VARIABLE_STORE_HEADER *)
> >>> (UINTN)
> >>>>> mVariableModuleGlobal-
> >>>> VariableGlobal.HobVariableBase;
> >>>>> +        GetRuntimeCacheInfo->TotalHobStorageSize =
> >>> VariableCache->Size;
> >>>>> +      } else {
> >>>>> +        GetRuntimeCacheInfo->TotalHobStorageSize =
> >>> 0;
> >>>>> +      }
> >>>>> +
> >>>>> +      VariableCache = (VARIABLE_STORE_HEADER  *)
> >>> (UINTN)
> >>>>> mVariableModuleGlobal-
> >>>> VariableGlobal.VolatileVariableBase;
> >>>>> +      GetRuntimeCacheInfo->TotalVolatileStorageSize
> >>> = VariableCache-
> >>>>> Size;
> >>>>> +      VariableCache = (VARIABLE_STORE_HEADER  *)
> >>> (UINTN)
> >>>>> mNvVariableCache;
> >>>>> +      GetRuntimeCacheInfo->TotalNvStorageSize =
> >>> (UINTN) VariableCache-
> >>>>>> Size;
> >>>>> +      GetRuntimeCacheInfo-
> >>>> AuthenticatedVariableUsage =
> >>>>> mVariableModuleGlobal->VariableGlobal.AuthFormat;
> >>>>> +
> >>>>> +      Status = EFI_SUCCESS;
> >>>>> +      break;
> >>>>>
> >>>>>      default:
> >>>>>        Status = EFI_UNSUPPORTED;
> >>>>> diff --git
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.c
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.c
> >>>>> index 0a1888e5ef..46f69765a4 100644
> >>>>> ---
> >>>>>
> >>>>
> >>> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.c
> >>>>> +++
> >>>>>
> >>>>
> >>> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm
> >>> RuntimeDx
> >>>>> e.c
> >>>>> @@ -13,7 +13,7 @@
> >>>>>
> >>>>>    InitCommunicateBuffer() is really function to
> >>> check the variable data size.
> >>>>>
> >>>>> -Copyright (c) 2010 - 2017, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>> +Copyright (c) 2010 - 2019, Intel Corporation. All
> >>> rights reserved.<BR>
> >>>>>  SPDX-License-Identifier: BSD-2-Clause-Patent
> >>>>>
> >>>>>  **/
> >>>>> @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-
> >>> Clause-Patent
> >>>>>  #include <Library/UefiRuntimeLib.h>
> >>>>>  #include <Library/BaseMemoryLib.h>
> >>>>>  #include <Library/DebugLib.h>
> >>>>> +#include <Library/TimerLib.h>
> >>>>>  #include <Library/UefiLib.h>
> >>>>>  #include <Library/BaseLib.h>
> >>>>>
> >>>>>  #include <Guid/EventGroup.h>
> >>>>> +#include <Guid/PiSmmCommunicationRegionTable.h>
> >>>>>  #include <Guid/SmmVariableCommon.h>
> >>>>>
> >>>>>  #include "PrivilegePolymorphic.h"
> >>>>> +#include "VariableParsing.h"
> >>>>>
> >>>>>  EFI_HANDLE                       mHandle
> >>> = NULL;
> >>>>>  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable
> >>> = NULL;
> >>>>> @@ -46,8 +49,19 @@ EFI_EVENT
> >>> mVirtualAddressChangeEvent
> >>>> =
> >>>>> NULL;
> >>>>>  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication
> >>> =
> >>>>> NULL;
> >>>>>  UINT8                           *mVariableBuffer
> >>> = NULL;
> >>>>>  UINT8
> >>> *mVariableBufferPhysical    = NULL;
> >>>>> +VARIABLE_INFO_ENTRY             *mVariableInfo
> >>> = NULL;
> >>>>> +VARIABLE_STORE_HEADER
> >>> *mVariableRuntimeHobCacheBuffer
> >>>> =
> >>>>> NULL;
> >>>>> +VARIABLE_STORE_HEADER
> >>> *mVariableRuntimeNvCacheBuffer
> >>>> =
> >>>>> NULL;
> >>>>> +VARIABLE_STORE_HEADER
> >>> *mVariableRuntimeVolatileCacheBuffer
> >>>>> = NULL;
> >>>>>  UINTN
> >>> mVariableBufferSize;
> >>>>> +UINTN
> >>> mVariableRuntimeHobCacheBufferSize;
> >>>>> +UINTN
> >>> mVariableRuntimeNvCacheBufferSize;
> >>>>> +UINTN
> >>> mVariableRuntimeVolatileCacheBufferSize;
> >>>>>  UINTN
> >>> mVariableBufferPayloadSize;
> >>>>> +BOOLEAN
> >>> mVariableRuntimeCachePendingUpdate;
> >>>>> +BOOLEAN
> >>> mVariableRuntimeCacheReadLock;
> >>>>> +BOOLEAN
> >>> mVariableAuthFormat;
> >>>>> +BOOLEAN                          mHobFlushComplete;
> >>>>>  EFI_LOCK
> >>> mVariableServicesLock;
> >>>>>  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
> >>>>>  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> >>>>> @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
> >>>>>    }
> >>>>>  }
> >>>>>
> >>>>> +/**
> >>>>> +  Return TRUE if ExitBootServices () has been
> >>> called.
> >>>>> +
> >>>>> +  @retval TRUE If ExitBootServices () has been
> >>> called.
> >>>>> +**/
> >>>>> +BOOLEAN
> >>>>> +AtRuntime (
> >>>>> +  VOID
> >>>>> +  )
> >>>>
> >>>>
> >>>> I think we can either:
> >>>> 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
> >>>> 2. Move AtRuntime() to VariableParsing.c so that the
> >>> function can be shared
> >>>>    with VariableRuntimeDxe & VariableSmmRuntimeDxe.
> >>> And then update
> >>>> the
> >>>>    EfiAtRuntime() usages to AtRuntime() for
> >>> VariableSmmRuntimeDxe.
> >>>>
> >>>>
> >>>
> >>> #1 will work fine.
> >>>
> >>>>> +{
> >>>>> +  return EfiAtRuntime ();
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> +  Initialize the variable cache buffer as an empty
> >>> variable store.
> >>>>> +
> >>>>> +  @param[out]     VariableCacheBuffer     A pointer
> >>> to pointer of a cache
> >>>>> variable store.
> >>>>> +  @param[in,out]  TotalVariableCacheSize  On input,
> >>> the minimum size
> >>>>> needed for the UEFI variable store cache
> >>>>> +                                          buffer
> >>> that is allocated. On output, the actual size of
> >>>>> the buffer allocated.
> >>>>> +                                          If
> >>> TotalVariableCacheSize is zero, a buffer will not be
> >>>>> allocated and the
> >>>>> +                                          function
> >>> will return with EFI_SUCCESS.
> >>>>> +
> >>>>> +  @retval EFI_SUCCESS             The variable
> >>> cache was allocated and
> >>>> initialized
> >>>>> successfully.
> >>>>> +  @retval EFI_INVALID_PARAMETER   A given pointer
> >>> is NULL or an invalid
> >>>>> variable store size was specified.
> >>>>> +  @retval EFI_OUT_OF_RESOURCES    Insufficient
> >>> resources are available
> >>>> to
> >>>>> allocate the variable store cache buffer.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +EFIAPI
> >>>>> +InitVariableCache (
> >>>>> +  OUT    VARIABLE_STORE_HEADER
> >>> **VariableCacheBuffer,
> >>>>> +  IN OUT UINTN
> >>> *TotalVariableCacheSize
> >>>>> +  )
> >>>>> +{
> >>>>> +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> >>>>> +
> >>>>> +  if (TotalVariableCacheSize == NULL) {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  }
> >>>>> +  if (*TotalVariableCacheSize == 0) {
> >>>>> +    return EFI_SUCCESS;
> >>>>> +  }
> >>>>> +  if (VariableCacheBuffer == NULL ||
> >>> *TotalVariableCacheSize < sizeof
> >>>>> (VARIABLE_STORE_HEADER)) {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  }
> >>>>> +  *TotalVariableCacheSize = ALIGN_VALUE
> >>> (*TotalVariableCacheSize,
> >>>> sizeof
> >>>>> (UINT32));
> >>>>> +
> >>>>> +  //
> >>>>> +  // Allocate NV Storage Cache and initialize it to
> >>> all 1's (like an erased FV)
> >>>>> +  //
> >>>>> +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> >>>>> AllocateRuntimePages (
> >>>>> +                            EFI_SIZE_TO_PAGES
> >>> (*TotalVariableCacheSize)
> >>>>> +                            );
> >>>>> +  if (*VariableCacheBuffer == NULL) {
> >>>>> +    return EFI_OUT_OF_RESOURCES;
> >>>>> +  }
> >>>>> +  VariableCacheStorePtr = *VariableCacheBuffer;
> >>>>> +  SetMem32 ((VOID *) VariableCacheStorePtr,
> >>> *TotalVariableCacheSize,
> >>>>> (UINT32) 0xFFFFFFFF);
> >>>>> +
> >>>>> +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> >>>>> (VARIABLE_STORE_HEADER));
> >>>>> +  VariableCacheStorePtr->Size    = (UINT32)
> >>> *TotalVariableCacheSize;
> >>>>> +  VariableCacheStorePtr->Format  =
> >>> VARIABLE_STORE_FORMATTED;
> >>>>> +  VariableCacheStorePtr->State   =
> >>> VARIABLE_STORE_HEALTHY;
> >>>>> +
> >>>>> +  return EFI_SUCCESS;
> >>>>> +}
> >>>>> +
> >>>>>  /**
> >>>>>    Initialize the communicate buffer using DataSize
> >>> and Function.
> >>>>>
> >>>>> @@ -153,6 +234,69 @@ InitCommunicateBuffer (
> >>>>>  }
> >>>>>
> >>>>>
> >>>>> +/**
> >>>>> +  Gets a SMM communicate buffer from the
> >>>>> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as
> >>> an entry in
> >>>>> the UEFI
> >>>>> +  system configuration table. A generic SMM
> >>> communication buffer DXE
> >>>>> driver may install the table or a custom table
> >>>>> +  may be installed by a platform-specific driver.
> >>>>> +
> >>>>> +  The communicate size is:
> >>> SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> >>>>> +                            DataSize.
> >>>>> +
> >>>>> +  @param[in,out]   CommBufferSize   On input, the
> >>> minimum size needed
> >>>>> for the communication buffer.
> >>>>> +                                    On output, the
> >>> SMM buffer size available at
> >>>> CommBuffer.
> >>>>> +  @param[out]      CommBuffer       A pointer to an
> >>> SMM communication
> >>>>> buffer pointer.
> >>>>> +
> >>>>> +  @retval EFI_SUCCESS               The
> >>> communication buffer was found
> >>>>> successfully.
> >>>>> +  @retval EFI_INVALID_PARAMETER     A given pointer
> >>> is NULL or the
> >>>>> CommBufferSize is zero.
> >>>>> +  @retval EFI_NOT_FOUND             The
> >>>>> EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not
> >>> found.
> >>>>> +  @retval EFI_OUT_OF_RESOURCES      A valid SMM
> >>> communicate buffer
> >>>> for
> >>>>> the requested size is not available.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +GetCommunicateBuffer (
> >>>>> +  IN OUT  UINTN     *CommBufferSize,
> >>>>> +  OUT     UINT8     **CommBuffer
> >>>>> +  )
> >>>>
> >>>>
> >>>> Minor comment:
> >>>>
> >>>> I found that the consumers of the above function are:
> >>>> GetRuntimeCacheInfo()
> >>>> SendRuntimeVariableCacheContextToSmm()
> >>>>
> >>>> Both of them get called within SmmVariableReady() when
> >>> the SMM variable
> >>>> driver
> >>>> finished initialization. I am wondering if they can
> >>> simply use the pre-allocated
> >>>> comm buffer (via InitCommunicateBuffer() and using
> >>> 'mVariableBuffer'),
> >>>> instead
> >>>> of looking into the configuration table.
> >>>>
> >>>> In my opinion, this function can be dropped.
> >>>>
> >>>>
> >>>
> >>> I did that initially. It was recommended to use this
> >>> method.
> >>>
> >>>>> +{
> >>>>> +  EFI_STATUS                                Status;
> >>>>> +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> >>>>> *PiSmmCommunicationRegionTable;
> >>>>> +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> >>>>> +  UINTN
> >>> EntrySize;
> >>>>> +  UINT32                                    Index;
> >>>>> +
> >>>>> +  if (CommBuffer == NULL || CommBufferSize == NULL
> >>> ||
> >>>>> *CommBufferSize == 0) {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  }
> >>>>> +
> >>>>> +  Status = EfiGetSystemConfigurationTable (
> >>>>> +
> >>> &gEdkiiPiSmmCommunicationRegionTableGuid,
> >>>>> +             (VOID **)
> >>> &PiSmmCommunicationRegionTable
> >>>>> +             );
> >>>>> +  if (EFI_ERROR (Status) ||
> >>> PiSmmCommunicationRegionTable == NULL) {
> >>>>> +    return EFI_NOT_FOUND;
> >>>>> +  }
> >>>>> +
> >>>>> +  Entry = (EFI_MEMORY_DESCRIPTOR *)
> >>>> (PiSmmCommunicationRegionTable
> >>>>> + 1);
> >>>>> +  EntrySize = 0;
> >>>>> +  for (Index = 0; Index <
> >>> PiSmmCommunicationRegionTable-
> >>>>>> NumberOfEntries; Index++) {
> >>>>> +    if (Entry->Type == EfiConventionalMemory) {
> >>>>> +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry-
> >>>> NumberOfPages);
> >>>>> +      if (EntrySize >= *CommBufferSize) {
> >>>>> +        break;
> >>>>> +      }
> >>>>> +    }
> >>>>> +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *)
> >>> Entry +
> >>>>> PiSmmCommunicationRegionTable->DescriptorSize);
> >>>>> +  }
> >>>>> +
> >>>>> +  if (Index < PiSmmCommunicationRegionTable-
> >>>> NumberOfEntries) {
> >>>>> +    *CommBufferSize = EntrySize;
> >>>>> +    *CommBuffer = (UINT8 *) (UINTN) Entry-
> >>>> PhysicalStart;
> >>>>> +    return EFI_SUCCESS;
> >>>>> +  }
> >>>>> +
> >>>>> +  return EFI_OUT_OF_RESOURCES;
> >>>>> +}
> >>>>> +
> >>>>>  /**
> >>>>>    Send the data in communicate buffer to SMM.
> >>>>>
> >>>>> @@ -424,6 +568,171 @@ Done:
> >>>>>    return Status;
> >>>>>  }
> >>>>>
> >>>>> +/**
> >>>>> +  Signals SMM to synchronize any pending variable
> >>> updates with the
> >>>>> runtime cache(s).
> >>>>> +
> >>>>> +**/
> >>>>> +VOID
> >>>>> +EFIAPI
> >>>>> +SyncRuntimeCache (
> >>>>> +  VOID
> >>>>> +  )
> >>>>> +{
> >>>>> +  //
> >>>>> +  // Init the communicate buffer. The buffer data
> >>> size is:
> >>>>> +  // SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> >>>>> +  //
> >>>>> +  InitCommunicateBuffer (NULL, 0,
> >>>>> SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> >>>>> +
> >>>>> +  //
> >>>>> +  // Send data to SMM.
> >>>>> +  //
> >>>>> +  SendCommunicateBuffer (0);
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> +  Check whether a SMI must be triggered to retrieve
> >>> pending cache
> >>>> updates.
> >>>>> +
> >>>>> +  If the variable HOB was finished being flushed
> >>> since the last check for a
> >>>>> runtime cache update, this function
> >>>>> +  will prevent the HOB cache from being used for
> >>> future runtime cache
> >>>> hits.
> >>>>> +
> >>>>> +**/
> >>>>> +VOID
> >>>>> +EFIAPI
> >>>>> +CheckForRuntimeCacheSync (
> >>>>> +  VOID
> >>>>> +  )
> >>>>> +{
> >>>>> +  if (mVariableRuntimeCachePendingUpdate) {
> >>>>> +    SyncRuntimeCache ();
> >>>>> +  }
> >>>>> +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> >>>>> +
> >>>>> +  //
> >>>>> +  // The HOB variable data may have finished being
> >>> flushed in the runtime
> >>>>> cache sync update
> >>>>> +  //
> >>>>> +  if (mHobFlushComplete &&
> >>> mVariableRuntimeHobCacheBuffer != NULL)
> >>>> {
> >>>>> +    if (!AtRuntime ()) {
> >>>>> +      FreePool (mVariableRuntimeHobCacheBuffer);
> >>>>> +    }
> >>>>> +    mVariableRuntimeHobCacheBuffer = NULL;
> >>>>> +  }
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> +  This code finds variable in a volatile memory
> >>> store.
> >>>>> +
> >>>>> +  Caution: This function may receive untrusted
> >>> input.
> >>>>> +  The data size is external input, so this function
> >>> will validate it carefully to
> >>>>> avoid buffer overflow.
> >>>>> +
> >>>>> +  @param[in]      VariableName       Name of
> >>> Variable to be found.
> >>>>> +  @param[in]      VendorGuid         Variable
> >>> vendor GUID.
> >>>>> +  @param[out]     Attributes         Attribute
> >>> value of the variable found.
> >>>>> +  @param[in, out] DataSize           Size of Data
> >>> found. If size is less than the
> >>>>> +                                     data, this
> >>> value contains the required size.
> >>>>> +  @param[out]     Data               Data pointer.
> >>>>> +
> >>>>> +  @retval EFI_SUCCESS                Found the
> >>> specified variable.
> >>>>> +  @retval EFI_INVALID_PARAMETER      Invalid
> >>> parameter.
> >>>>> +  @retval EFI_NOT_FOUND              The specified
> >>> variable could not be
> >>>> found.
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +EFIAPI
> >>>>> +FindVariableInRuntimeCache (
> >>>>> +  IN      CHAR16
> >>> *VariableName,
> >>>>> +  IN      EFI_GUID
> >>> *VendorGuid,
> >>>>> +  OUT     UINT32
> >>> *Attributes OPTIONAL,
> >>>>> +  IN OUT  UINTN
> >>> *DataSize,
> >>>>> +  OUT     VOID                              *Data
> >>> OPTIONAL
> >>>>> +  )
> >>>>> +{
> >>>>> +  EFI_STATUS              Status;
> >>>>> +  UINTN                   DelayIndex;
> >>>>> +  UINTN                   TempDataSize;
> >>>>> +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> >>>>> +  VARIABLE_STORE_TYPE     StoreType;
> >>>>> +  VARIABLE_STORE_HEADER
> >>> *VariableStoreList[VariableStoreTypeMax];
> >>>>> +
> >>>>> +  Status = EFI_NOT_FOUND;
> >>>>> +
> >>>>> +  if (VariableName == NULL || VendorGuid == NULL ||
> >>> DataSize == NULL) {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  }
> >>>>> +
> >>>>> +  for (DelayIndex = 0;
> >>> mVariableRuntimeCacheReadLock && DelayIndex <
> >>>>> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> >>>>> +    MicroSecondDelay (10);
> >>>>> +  }
> >>>>> +  if (DelayIndex <
> >>> VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> >>>>> +    ASSERT (!mVariableRuntimeCacheReadLock);
> >>>>> +
> >>>>> +    mVariableRuntimeCacheReadLock = TRUE;
> >>>>> +    CheckForRuntimeCacheSync ();
> >>>>> +
> >>>>> +    if (!mVariableRuntimeCachePendingUpdate) {
> >>>>> +      //
> >>>>> +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> >>>>> +      // The index and attributes mapping must be
> >>> kept in this order as
> >>>>> FindVariable
> >>>>> +      // makes use of this mapping to implement
> >>> search algorithm.
> >>>>> +      //
> >>>>> +      VariableStoreList[VariableStoreTypeVolatile]
> >>> =
> >>>>> mVariableRuntimeVolatileCacheBuffer;
> >>>>> +      VariableStoreList[VariableStoreTypeHob]
> >>> =
> >>>>> mVariableRuntimeHobCacheBuffer;
> >>>>> +      VariableStoreList[VariableStoreTypeNv]
> >>> =
> >>>>> mVariableRuntimeNvCacheBuffer;
> >>>>> +
> >>>>> +      for (StoreType = (VARIABLE_STORE_TYPE) 0;
> >>> StoreType <
> >>>>> VariableStoreTypeMax; StoreType++) {
> >>>>> +        if (VariableStoreList[StoreType] == NULL) {
> >>>>> +          continue;
> >>>>> +        }
> >>>>> +
> >>>>> +        RtPtrTrack.StartPtr = GetStartPointer
> >>> (VariableStoreList[StoreType]);
> >>>>> +        RtPtrTrack.EndPtr   = GetEndPointer
> >>> (VariableStoreList[StoreType]);
> >>>>> +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType
> >>> ==
> >>>>> VariableStoreTypeVolatile);
> >>>>> +
> >>>>> +        Status = FindVariableEx (VariableName,
> >>> VendorGuid, FALSE,
> >>>>> &RtPtrTrack);
> >>>>> +        if (!EFI_ERROR (Status)) {
> >>>>> +          break;
> >>>>> +        }
> >>>>> +      }
> >>>>> +
> >>>>> +      if (!EFI_ERROR (Status)) {
> >>>>> +        //
> >>>>> +        // Get data size
> >>>>> +        //
> >>>>> +        TempDataSize = DataSizeOfVariable
> >>> (RtPtrTrack.CurrPtr);
> >>>>> +        ASSERT (TempDataSize != 0);
> >>>>> +
> >>>>> +        if (*DataSize >= TempDataSize) {
> >>>>> +          if (Data == NULL) {
> >>>>> +            Status = EFI_INVALID_PARAMETER;
> >>>>> +            goto Done;
> >>>>> +          }
> >>>>> +
> >>>>> +          CopyMem (Data, GetVariableDataPtr
> >>> (RtPtrTrack.CurrPtr),
> >>>>> TempDataSize);
> >>>>> +          if (Attributes != NULL) {
> >>>>> +            *Attributes = RtPtrTrack.CurrPtr-
> >>>> Attributes;
> >>>>> +          }
> >>>>> +
> >>>>> +          *DataSize = TempDataSize;
> >>>>> +
> >>>>> +          UpdateVariableInfo (VariableName,
> >>> VendorGuid,
> >>>> RtPtrTrack.Volatile,
> >>>>> TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> >>>>> +
> >>>>> +          Status = EFI_SUCCESS;
> >>>>> +          goto Done;
> >>>>> +        } else {
> >>>>> +          *DataSize = TempDataSize;
> >>>>> +          Status = EFI_BUFFER_TOO_SMALL;
> >>>>> +          goto Done;
> >>>>> +        }
> >>>>> +      }
> >>>>> +    }
> >>>>> +  }
> >>>>> +
> >>>>> +Done:
> >>>>> +  mVariableRuntimeCacheReadLock = FALSE;
> >>>>
> >>>>
> >>>> If timeout occurs when acquiring the read lock, should
> >>> this flag be set to
> >>>> FALSE
> >>>> in such case?
> >>>>
> >>>
> >>> Please see reply to patch #8.
> >>>
> >>>> Best Regards,
> >>>> Hao Wu
> >>>>
> >>>>
> >>>>> +
> >>>>> +  return Status;
> >>>>> +}
> >>>>> +
> >>>>>  /**
> >>>>>    This code finds variable in storage blocks
> >>> (Volatile or Non-Volatile).
> >>>>>
> >>>>> @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
> >>>>>    )
> >>>>>  {
> >>>>>    EFI_STATUS                                Status;
> >>>>> -  UINTN
> >>> PayloadSize;
> >>>>> -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> >>>>> *SmmVariableHeader;
> >>>>> -  UINTN
> >>> TempDataSize;
> >>>>> -  UINTN
> >>> VariableNameSize;
> >>>>>
> >>>>>    if (VariableName == NULL || VendorGuid == NULL ||
> >>> DataSize == NULL) {
> >>>>>      return EFI_INVALID_PARAMETER;
> >>>>>    }
> >>>>> -
> >>>>> -  TempDataSize          = *DataSize;
> >>>>> -  VariableNameSize      = StrSize (VariableName);
> >>>>> -  SmmVariableHeader     = NULL;
> >>>>> -
> >>>>> -  //
> >>>>> -  // If VariableName exceeds SMM payload limit.
> >>> Return failure
> >>>>> -  //
> >>>>> -  if (VariableNameSize > mVariableBufferPayloadSize
> >>> - OFFSET_OF
> >>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> >>>>> -    return EFI_INVALID_PARAMETER;
> >>>>> -  }
> >>>>> -
> >>>>> -
> >>> AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> >>>>> -
> >>>>> -  //
> >>>>> -  // Init the communicate buffer. The buffer data
> >>> size is:
> >>>>> -  // SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> >>>>> -  //
> >>>>> -  if (TempDataSize > mVariableBufferPayloadSize -
> >>> OFFSET_OF
> >>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> >>>>> VariableNameSize) {
> >>>>> -    //
> >>>>> -    // If output data buffer exceed SMM payload
> >>> limit. Trim output buffer to
> >>>>> SMM payload size
> >>>>> -    //
> >>>>> -    TempDataSize = mVariableBufferPayloadSize -
> >>> OFFSET_OF
> >>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> >>>>> VariableNameSize;
> >>>>> -  }
> >>>>> -  PayloadSize = OFFSET_OF
> >>>>> (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> >>>>> VariableNameSize + TempDataSize;
> >>>>> -
> >>>>> -  Status = InitCommunicateBuffer ((VOID
> >>> **)&SmmVariableHeader,
> >>>>> PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> >>>>> -  if (EFI_ERROR (Status)) {
> >>>>> -    goto Done;
> >>>>> -  }
> >>>>> -  ASSERT (SmmVariableHeader != NULL);
> >>>>> -
> >>>>> -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> >>>>> -  SmmVariableHeader->DataSize   = TempDataSize;
> >>>>> -  SmmVariableHeader->NameSize   = VariableNameSize;
> >>>>> -  if (Attributes == NULL) {
> >>>>> -    SmmVariableHeader->Attributes = 0;
> >>>>> -  } else {
> >>>>> -    SmmVariableHeader->Attributes = *Attributes;
> >>>>> -  }
> >>>>> -  CopyMem (SmmVariableHeader->Name, VariableName,
> >>>>> SmmVariableHeader->NameSize);
> >>>>> -
> >>>>> -  //
> >>>>> -  // Send data to SMM.
> >>>>> -  //
> >>>>> -  Status = SendCommunicateBuffer (PayloadSize);
> >>>>> -
> >>>>> -  //
> >>>>> -  // Get data from SMM.
> >>>>> -  //
> >>>>> -  if (Status == EFI_SUCCESS || Status ==
> >>> EFI_BUFFER_TOO_SMALL) {
> >>>>> -    //
> >>>>> -    // SMM CommBuffer DataSize can be a trimed
> >>> value
> >>>>> -    // Only update DataSize when needed
> >>>>> -    //
> >>>>> -    *DataSize = SmmVariableHeader->DataSize;
> >>>>> -  }
> >>>>> -  if (Attributes != NULL) {
> >>>>> -    *Attributes = SmmVariableHeader->Attributes;
> >>>>> -  }
> >>>>> -
> >>>>> -  if (EFI_ERROR (Status)) {
> >>>>> -    goto Done;
> >>>>> -  }
> >>>>> -
> >>>>> -  if (Data != NULL) {
> >>>>> -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name
> >>> +
> >>>>> SmmVariableHeader->NameSize, SmmVariableHeader-
> >>>> DataSize);
> >>>>> -  } else {
> >>>>> -    Status = EFI_INVALID_PARAMETER;
> >>>>> +  if (VariableName[0] == 0) {
> >>>>> +    return EFI_NOT_FOUND;
> >>>>>    }
> >>>>>
> >>>>> -Done:
> >>>>> +  AcquireLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +  Status =  FindVariableInRuntimeCache
> >>> (VariableName, VendorGuid,
> >>>>> Attributes, DataSize, Data);
> >>>>>    ReleaseLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +
> >>>>>    return Status;
> >>>>>  }
> >>>>>
> >>>>> -
> >>>>>  /**
> >>>>>    This code Finds the Next available variable.
> >>>>>
> >>>>> @@ -870,6 +1109,17 @@ OnReadyToBoot (
> >>>>>    //
> >>>>>    SendCommunicateBuffer (0);
> >>>>>
> >>>>> +  //
> >>>>> +  // Install the system configuration table for
> >>> variable info data captured
> >>>>> +  //
> >>>>> +  if (FeaturePcdGet (PcdVariableCollectStatistics))
> >>> {
> >>>>> +    if (mVariableAuthFormat) {
> >>>>> +      gBS->InstallConfigurationTable
> >>> (&gEfiAuthenticatedVariableGuid,
> >>>>> mVariableInfo);
> >>>>> +    } else {
> >>>>> +      gBS->InstallConfigurationTable
> >>> (&gEfiVariableGuid, mVariableInfo);
> >>>>> +    }
> >>>>> +  }
> >>>>> +
> >>>>>    gBS->CloseEvent (Event);
> >>>>>  }
> >>>>>
> >>>>> @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
> >>>>>  {
> >>>>>    EfiConvertPointer (0x0, (VOID **)
> >>> &mVariableBuffer);
> >>>>>    EfiConvertPointer (0x0, (VOID **)
> >>> &mSmmCommunication);
> >>>>> +  EfiConvertPointer (0x0, (VOID **)
> >>> &mVariableRuntimeHobCacheBuffer);
> >>>>> +  EfiConvertPointer (0x0, (VOID **)
> >>> &mVariableRuntimeNvCacheBuffer);
> >>>>> +  EfiConvertPointer (0x0, (VOID **)
> >>>>> &mVariableRuntimeVolatileCacheBuffer);
> >>>>>  }
> >>>>>
> >>>>>  /**
> >>>>> @@ -969,6 +1222,173 @@ Done:
> >>>>>    return Status;
> >>>>>  }
> >>>>>
> >>>>> +/**
> >>>>> +  This code gets information needed from SMM for
> >>> runtime cache
> >>>>> initialization.
> >>>>> +
> >>>>> +  @param[out] TotalHobStorageSize         Output
> >>> pointer for the total HOB
> >>>>> storage size in bytes.
> >>>>> +  @param[out] TotalNvStorageSize          Output
> >>> pointer for the total non-
> >>>>> volatile storage size in bytes.
> >>>>> +  @param[out] TotalVolatileStorageSize    Output
> >>> pointer for the total
> >>>>> volatile storage size in bytes.
> >>>>> +  @param[out] AuthenticatedVariableUsage  Output
> >>> pointer that indicates
> >>>> if
> >>>>> authenticated variables are to be used.
> >>>>> +
> >>>>> +  @retval EFI_SUCCESS                     Retrieved
> >>> the size successfully.
> >>>>> +  @retval EFI_INVALID_PARAMETER
> >>> TotalNvStorageSize parameter is
> >>>>> NULL.
> >>>>> +  @retval EFI_OUT_OF_RESOURCES            Could not
> >>> allocate a
> >>>> CommBuffer.
> >>>>> +  @retval Others                          Could not
> >>> retrieve the size successfully.;
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +EFIAPI
> >>>>> +GetRuntimeCacheInfo (
> >>>>> +  OUT UINTN
> >>> *TotalHobStorageSize,
> >>>>> +  OUT UINTN
> >>> *TotalNvStorageSize,
> >>>>> +  OUT UINTN
> >>> *TotalVolatileStorageSize,
> >>>>> +  OUT BOOLEAN
> >>> *AuthenticatedVariableUsage
> >>>>> +  )
> >>>>> +{
> >>>>> +  EFI_STATUS
> >>> Status;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> >>>>> *SmmGetRuntimeCacheInfo;
> >>>>> +  EFI_SMM_COMMUNICATE_HEADER
> >>>>> *SmmCommunicateHeader;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
> >>>>> *SmmVariableFunctionHeader;
> >>>>> +  UINTN
> >>> CommSize;
> >>>>> +  UINTN
> >>> CommBufferSize;
> >>>>> +  UINT8
> >>> *CommBuffer;
> >>>>> +
> >>>>> +  SmmGetRuntimeCacheInfo = NULL;
> >>>>> +  CommBuffer = NULL;
> >>>>> +
> >>>>> +  if (TotalHobStorageSize == NULL ||
> >>> TotalNvStorageSize == NULL ||
> >>>>> TotalVolatileStorageSize == NULL ||
> >>> AuthenticatedVariableUsage == NULL)
> >>>> {
> >>>>> +    return EFI_INVALID_PARAMETER;
> >>>>> +  }
> >>>>> +
> >>>>> +  AcquireLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +
> >>>>> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> >>>>> +  CommBufferSize = CommSize;
> >>>>> +  Status = GetCommunicateBuffer (&CommBufferSize,
> >>> &CommBuffer);
> >>>>> +  if (EFI_ERROR (Status)) {
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +  if (CommBuffer == NULL) {
> >>>>> +    Status = EFI_OUT_OF_RESOURCES;
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +  ZeroMem (CommBuffer, CommBufferSize);
> >>>>> +
> >>>>> +  SmmCommunicateHeader =
> >>> (EFI_SMM_COMMUNICATE_HEADER *)
> >>>>> CommBuffer;
> >>>>> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> >>>>> &gEfiSmmVariableProtocolGuid);
> >>>>> +  SmmCommunicateHeader->MessageLength =
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> >>>>> +
> >>>>> +  SmmVariableFunctionHeader =
> >>>>> (SMM_VARIABLE_COMMUNICATE_HEADER *)
> >>> SmmCommunicateHeader-
> >>>>>> Data;
> >>>>> +  SmmVariableFunctionHeader->Function =
> >>>>> SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> >>>>> +  SmmGetRuntimeCacheInfo =
> >>>>> (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> >>>>> SmmVariableFunctionHeader->Data;
> >>>>> +
> >>>>> +  //
> >>>>> +  // Send data to SMM.
> >>>>> +  //
> >>>>> +  Status = mSmmCommunication->Communicate
> >>> (mSmmCommunication,
> >>>>> CommBuffer, &CommSize);
> >>>>> +  ASSERT_EFI_ERROR (Status);
> >>>>> +  if (CommSize <=
> >>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> >>>>> +    Status = EFI_BAD_BUFFER_SIZE;
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +
> >>>>> +  Status = SmmVariableFunctionHeader->ReturnStatus;
> >>>>> +  if (EFI_ERROR (Status)) {
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +
> >>>>> +  //
> >>>>> +  // Get data from SMM.
> >>>>> +  //
> >>>>> +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
> >>>>> TotalHobStorageSize;
> >>>>> +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo-
> >>>> TotalNvStorageSize;
> >>>>> +  *TotalVolatileStorageSize =
> >>> SmmGetRuntimeCacheInfo-
> >>>>>> TotalVolatileStorageSize;
> >>>>> +  *AuthenticatedVariableUsage =
> >>> SmmGetRuntimeCacheInfo-
> >>>>>> AuthenticatedVariableUsage;
> >>>>> +
> >>>>> +Done:
> >>>>> +  ReleaseLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +  return Status;
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> +  Sends the runtime variable cache context
> >>> information to SMM.
> >>>>> +
> >>>>> +  @retval EFI_SUCCESS               Retrieved the
> >>> size successfully.
> >>>>> +  @retval EFI_INVALID_PARAMETER
> >>> TotalNvStorageSize parameter is
> >>>>> NULL.
> >>>>> +  @retval EFI_OUT_OF_RESOURCES      Could not
> >>> allocate a CommBuffer.
> >>>>> +  @retval Others                    Could not
> >>> retrieve the size successfully.;
> >>>>> +
> >>>>> +**/
> >>>>> +EFI_STATUS
> >>>>> +EFIAPI
> >>>>> +SendRuntimeVariableCacheContextToSmm (
> >>>>> +  VOID
> >>>>> +  )
> >>>>> +{
> >>>>> +  EFI_STATUS
> >>> Status;
> >>>>> +
> >>>>>
> >>>
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>>>> *SmmRuntimeVarCacheContext;
> >>>>> +  EFI_SMM_COMMUNICATE_HEADER
> >>>>> *SmmCommunicateHeader;
> >>>>> +  SMM_VARIABLE_COMMUNICATE_HEADER
> >>>>> *SmmVariableFunctionHeader;
> >>>>> +  UINTN
> >>> CommSize;
> >>>>> +  UINTN
> >>> CommBufferSize;
> >>>>> +  UINT8
> >>> *CommBuffer;
> >>>>> +
> >>>>> +  SmmRuntimeVarCacheContext = NULL;
> >>>>> +  CommBuffer = NULL;
> >>>>> +
> >>>>> +  AcquireLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +
> >>>>> +  //
> >>>>> +  // Init the communicate buffer. The buffer data
> >>> size is:
> >>>>> +  // SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >>>>>
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>> )
> >>>> ;
> >>>>> +  //
> >>>>> +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >>>>>
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>> )
> >>>> ;
> >>>>> +  CommBufferSize = CommSize;
> >>>>> +  Status = GetCommunicateBuffer (&CommBufferSize,
> >>> &CommBuffer);
> >>>>> +  if (EFI_ERROR (Status)) {
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +  if (CommBuffer == NULL) {
> >>>>> +    Status = EFI_OUT_OF_RESOURCES;
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +  ZeroMem (CommBuffer, CommBufferSize);
> >>>>> +
> >>>>> +  SmmCommunicateHeader =
> >>> (EFI_SMM_COMMUNICATE_HEADER *)
> >>>>> CommBuffer;
> >>>>> +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> >>>>> &gEfiSmmVariableProtocolGuid);
> >>>>> +  SmmCommunicateHeader->MessageLength =
> >>>>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> >>>>>
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>> )
> >>>> ;
> >>>>> +
> >>>>> +  SmmVariableFunctionHeader =
> >>>>> (SMM_VARIABLE_COMMUNICATE_HEADER *)
> >>> SmmCommunicateHeader-
> >>>>>> Data;
> >>>>> +  SmmVariableFunctionHeader->Function =
> >>>>>
> >>>>
> >>>
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEX
> >>> T;
> >>>>> +  SmmRuntimeVarCacheContext =
> >>>>>
> >>>>
> >>>
> >>
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> >>>>> *) SmmVariableFunctionHeader->Data;
> >>>>> +
> >>>>> +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> >>>>> mVariableRuntimeHobCacheBuffer;
> >>>>> +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> >>>>> mVariableRuntimeVolatileCacheBuffer;
> >>>>> +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> >>>>> mVariableRuntimeNvCacheBuffer;
> >>>>> +  SmmRuntimeVarCacheContext->PendingUpdate =
> >>>>> &mVariableRuntimeCachePendingUpdate;
> >>>>> +  SmmRuntimeVarCacheContext->ReadLock =
> >>>>> &mVariableRuntimeCacheReadLock;
> >>>>> +  SmmRuntimeVarCacheContext->HobFlushComplete =
> >>>>> &mHobFlushComplete;
> >>>>> +
> >>>>> +  //
> >>>>> +  // Send data to SMM.
> >>>>> +  //
> >>>>> +  Status = mSmmCommunication->Communicate
> >>> (mSmmCommunication,
> >>>>> CommBuffer, &CommSize);
> >>>>> +  ASSERT_EFI_ERROR (Status);
> >>>>> +  if (CommSize <=
> >>> SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> >>>>> +    Status = EFI_BAD_BUFFER_SIZE;
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +
> >>>>> +  Status = SmmVariableFunctionHeader->ReturnStatus;
> >>>>> +  if (EFI_ERROR (Status)) {
> >>>>> +    goto Done;
> >>>>> +  }
> >>>>> +
> >>>>> +Done:
> >>>>> +  ReleaseLockOnlyAtBootTime
> >>> (&mVariableServicesLock);
> >>>>> +  return Status;
> >>>>> +}
> >>>>> +
> >>>>>  /**
> >>>>>    Initialize variable service and install Variable
> >>> Architectural protocol.
> >>>>>
> >>>>> @@ -985,7 +1405,7 @@ SmmVariableReady (
> >>>>>  {
> >>>>>    EFI_STATUS                                Status;
> >>>>>
> >>>>> -  Status = gBS->LocateProtocol
> >>> (&gEfiSmmVariableProtocolGuid, NULL,
> >>>>> (VOID **)&mSmmVariable);
> >>>>> +  Status = gBS->LocateProtocol
> >>> (&gEfiSmmVariableProtocolGuid, NULL,
> >>>>> (VOID **) &mSmmVariable);
> >>>>>    if (EFI_ERROR (Status)) {
> >>>>>      return;
> >>>>>    }
> >>>>> @@ -1007,6 +1427,40 @@ SmmVariableReady (
> >>>>>    //
> >>>>>    mVariableBufferPhysical = mVariableBuffer;
> >>>>>
> >>>>> +  //
> >>>>> +  // Allocate runtime variable cache memory
> >>> buffers.
> >>>>> +  //
> >>>>> +  Status =  GetRuntimeCacheInfo (
> >>>>> +              &mVariableRuntimeHobCacheBufferSize,
> >>>>> +              &mVariableRuntimeNvCacheBufferSize,
> >>>>> +
> >>> &mVariableRuntimeVolatileCacheBufferSize,
> >>>>> +              &mVariableAuthFormat
> >>>>> +              );
> >>>>> +  if (!EFI_ERROR (Status)) {
> >>>>> +    Status = InitVariableCache
> >>> (&mVariableRuntimeHobCacheBuffer,
> >>>>> &mVariableRuntimeHobCacheBufferSize);
> >>>>> +    if (!EFI_ERROR (Status)) {
> >>>>> +      Status = InitVariableCache
> >>> (&mVariableRuntimeNvCacheBuffer,
> >>>>> &mVariableRuntimeNvCacheBufferSize);
> >>>>> +      if (!EFI_ERROR (Status)) {
> >>>>> +        Status = InitVariableCache
> >>> (&mVariableRuntimeVolatileCacheBuffer,
> >>>>> &mVariableRuntimeVolatileCacheBufferSize);
> >>>>> +        if (!EFI_ERROR (Status)) {
> >>>>> +          Status = InitVariableParsing
> >>> (mVariableAuthFormat);
> >>>>> +          ASSERT_EFI_ERROR (Status);
> >>>>> +
> >>>>> +          Status =
> >>> SendRuntimeVariableCacheContextToSmm ();
> >>>>> +          if (!EFI_ERROR (Status)) {
> >>>>> +            SyncRuntimeCache ();
> >>>>> +          }
> >>>>> +        }
> >>>>> +      }
> >>>>> +    }
> >>>>> +    if (EFI_ERROR (Status)) {
> >>>>> +      mVariableRuntimeHobCacheBuffer = NULL;
> >>>>> +      mVariableRuntimeNvCacheBuffer = NULL;
> >>>>> +      mVariableRuntimeVolatileCacheBuffer = NULL;
> >>>>> +    }
> >>>>> +  }
> >>>>> +  ASSERT_EFI_ERROR (Status);
> >>>>> +
> >>>>>    gRT->GetVariable         =
> >>> RuntimeServiceGetVariable;
> >>>>>    gRT->GetNextVariableName =
> >>> RuntimeServiceGetNextVariableName;
> >>>>>    gRT->SetVariable         =
> >>> RuntimeServiceSetVariable;
> >>>>> --
> >>>>> 2.16.2.windows.1
> >>>>
> >>>
> >>
> >


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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-10-03 17:35     ` Kubacki, Michael A
@ 2019-10-08  2:11       ` Wu, Hao A
  2019-10-08 21:53         ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-08  2:11 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Friday, October 04, 2019 1:36 AM
> To: Wu, Hao A; devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing functions
> 
> The main reason is cohesiveness in the VariableParsing.c file. These are
> functions that
> are commonly needed for general variable data structure parsing operations.
> Most of
> them just read a member or two in a VARIABLE_STORE_HEADER or
> VARIABLE_HEADER
> data structure,  perform a simple calculation if needed, and return some
> value. More
> complex functions such as FindVariableEx (), do this in iteration across the
> variable store.
> If a function is needed that performs these tasks, it is easier to have them
> grouped into a
> cohesive file than search which one is in Variable.c and VariableParsing.c on a
> case-by-case
> basis. UpdateVariableInfo () is the exception here, I can move this to a
> separate file
> if you prefer.


I am fine to leave this function under VariableParsing.c.


> 
> Also, Variable.c is, in my opinion, far too large. It is on trend to only grow
> larger:
>   * Today: ~4,600 LOC
>   * ~2 years ago: ~4,200 LOC
>   * ~4 years ago: ~4,100 LOC
>   * ~5 years ago: ~3,440 LOC
>   * ~8 years ago: ~2,600 LOC
> 
> I think moving out generic parsing services such as these better groups
> related
> functionality in the short-term but also aids future refactoring efforts in the
> file.


Thanks for the clarification.

I suggest to update the commit message and add the above detailed criteria for
those functions that got moved to the new file.

Best Regards,
Hao Wu


> 
> Thanks,
> Michael
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Thursday, October 3, 2019 1:03 AM
> > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate
> common
> > parsing functions
> >
> > A couple of inline comments below:
> >
> >
> > > -----Original Message-----
> > > From: Kubacki, Michael A
> > > Sent: Saturday, September 28, 2019 9:47 AM
> > > To: devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney,
> > > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > > Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> > > parsing functions
> > >
> > > This change moves the following functions into a dedicated file
> > > so they may be used in other variable files as needed. Furthermore,
> > > it reduces the overall size of the common Variable.c file.
> > >
> > >  * DataSizeOfVariable ()
> > >  * FindVariableEx ()
> > >  * GetEndPointer ()
> > >  * GetNextVariablePtr ()
> > >  * GetStartPointer ()
> > >  * GetVariableDataOffset ()
> > >  * GetVariableDataPtr ()
> > >  * GetVariableHeaderSize ()
> > >  * GetVariableNamePtr ()
> > >  * GetVariableStoreStatus ()
> > >  * GetVendorGuidPtr ()
> > >  * IsValidVariableHeader ()
> > >  * NameSizeOfVariable ()
> > >  * SetDataSizeOfVariable ()
> > >  * SetNameSizeOfVariable ()
> > >  * UpdateVariableInfo ()
> > >  * VariableCompareTimeStampInternal ()
> > >  * VariableServiceGetNextVariableInternal ()
> >
> >
> > May I know what are the criteria for the above functions being moved to a
> > separate file?
> >
> > At first, I think all of them will be consumed by the new codes that
> > implement
> > the runtime cache. But I found that, for functions like
> > GetVariableDataOffset(),
> > GetVariableStoreStatus() and etc., their usages are still remained within file
> > Variable.c (seems not related with the runtime cache).
> >
> >
> > >
> > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > Cc: Eric Dong <eric.dong@intel.com>
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Liming Gao <liming.gao@intel.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Ray Ni <ray.ni@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > ---
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > |   2 +
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |
> > 2
> > > +
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > > |   7 +
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119
> -
> > ---
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        |
> > 306
> > > ++++++++
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726
> > +--
> > > ----------------
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |
> 3
> > +-
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        |
> > 731
> > > ++++++++++++++++++++
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |
> 1
> > +
> > >  9 files changed, 1052 insertions(+), 845 deletions(-)
> >
> >
> > For the below change in VariableStandaloneMm.inf:
> >
> > [Guids]
> > ...
> >   ## SOMETIMES_CONSUMES   ## Variable:L"db"
> >   ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> >   ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> >   gEfiImageSecurityDatabaseGuid
> > ...
> >
> > I think the above GUID is not used by the module specified by the above
> INF.
> > Could you double confirm on this?
> >
> > Best Regards,
> > Hao Wu
> >
> >
> > >
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > index 641376c9c5..c35e5fe787 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > @@ -36,6 +36,8 @@
> > >    Variable.c
> > >    VariableDxe.c
> > >    Variable.h
> > > +  VariableParsing.c
> > > +  VariableParsing.h
> > >    PrivilegePolymorphic.h
> > >    Measurement.c
> > >    TcgMorLockDxe.c
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > index 0a160d269d..626738b9c7 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > @@ -45,6 +45,8 @@
> > >    Variable.c
> > >    VariableTraditionalMm.c
> > >    VariableSmm.c
> > > +  VariableParsing.c
> > > +  VariableParsing.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > nf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > inf
> > > index 21bc81163b..1ba8f9ebfb 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > nf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > inf
> > > @@ -45,6 +45,8 @@
> > >    Variable.c
> > >    VariableSmm.c
> > >    VariableStandaloneMm.c
> > > +  VariableParsing.c
> > > +  VariableParsing.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > @@ -99,6 +101,11 @@
> > >    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
> > >    gEfiGlobalVariableGuid
> > >
> > > +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> > > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> > > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > > +  gEfiImageSecurityDatabaseGuid
> > > +
> > >    gEfiMemoryOverwriteControlDataGuid            ##
> SOMETIMES_CONSUMES
> > > ## Variable:L"MemoryOverwriteRequestControl"
> > >    gEfiMemoryOverwriteRequestControlLockGuid     ##
> > > SOMETIMES_PRODUCES   ##
> > > Variable:L"MemoryOverwriteRequestControlLock"
> > >
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > index 9eac43759f..fb574b2e32 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > @@ -179,89 +179,6 @@ FindVariable (
> > >    IN  BOOLEAN                 IgnoreRtCheck
> > >    );
> > >
> > > -/**
> > > -
> > > -  Gets the pointer to the end of the variable storage area.
> > > -
> > > -  This function gets pointer to the end of the variable storage
> > > -  area, according to the input variable store header.
> > > -
> > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > -
> > > -  @return Pointer to the end of the variable storage area.
> > > -
> > > -**/
> > > -VARIABLE_HEADER *
> > > -GetEndPointer (
> > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > -  );
> > > -
> > > -/**
> > > -  This code gets the size of variable header.
> > > -
> > > -  @return Size of variable header in bytes in type UINTN.
> > > -
> > > -**/
> > > -UINTN
> > > -GetVariableHeaderSize (
> > > -  VOID
> > > -  );
> > > -
> > > -/**
> > > -
> > > -  This code gets the pointer to the variable name.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Pointer to Variable Name which is Unicode encoding.
> > > -
> > > -**/
> > > -CHAR16 *
> > > -GetVariableNamePtr (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  );
> > > -
> > > -/**
> > > -  This code gets the pointer to the variable guid.
> > > -
> > > -  @param Variable   Pointer to the Variable Header.
> > > -
> > > -  @return A EFI_GUID* pointer to Vendor Guid.
> > > -
> > > -**/
> > > -EFI_GUID *
> > > -GetVendorGuidPtr (
> > > -  IN VARIABLE_HEADER    *Variable
> > > -  );
> > > -
> > > -/**
> > > -
> > > -  This code gets the pointer to the variable data.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Pointer to Variable Data.
> > > -
> > > -**/
> > > -UINT8 *
> > > -GetVariableDataPtr (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  );
> > > -
> > > -/**
> > > -
> > > -  This code gets the size of variable data.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Size of variable in bytes.
> > > -
> > > -**/
> > > -UINTN
> > > -DataSizeOfVariable (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  );
> > > -
> > >  /**
> > >    This function is to check if the remaining variable space is enough to set
> > >    all Variables from argument list successfully. The purpose of the check
> > > @@ -450,17 +367,6 @@ ReclaimForOS(
> > >    VOID
> > >    );
> > >
> > > -/**
> > > -  Get non-volatile maximum variable size.
> > > -
> > > -  @return Non-volatile maximum variable size.
> > > -
> > > -**/
> > > -UINTN
> > > -GetNonVolatileMaxVariableSize (
> > > -  VOID
> > > -  );
> > > -
> > >  /**
> > >    Get maximum variable size, covering both non-volatile and volatile
> > variables.
> > >
> > > @@ -546,31 +452,6 @@ VariableServiceGetVariable (
> > >    OUT     VOID              *Data OPTIONAL
> > >    );
> > >
> > > -/**
> > > -  This code Finds the Next available variable.
> > > -
> > > -  Caution: This function may receive untrusted input.
> > > -  This function may be invoked in SMM mode. This function will do basic
> > > validation, before parse the data.
> > > -
> > > -  @param[in] VariableName   Pointer to variable name.
> > > -  @param[in] VendorGuid     Variable Vendor Guid.
> > > -  @param[out] VariablePtr   Pointer to variable header address.
> > > -
> > > -  @retval EFI_SUCCESS           The function completed successfully.
> > > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > string,
> > > while VendorGuid is NULL.
> > > -  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > > VendorGuid are not a name and
> > > -                                GUID of an existing variable.
> > > -
> > > -**/
> > > -EFI_STATUS
> > > -EFIAPI
> > > -VariableServiceGetNextVariableInternal (
> > > -  IN  CHAR16                *VariableName,
> > > -  IN  EFI_GUID              *VendorGuid,
> > > -  OUT VARIABLE_HEADER       **VariablePtr
> > > -  );
> > > -
> > >  /**
> > >
> > >    This code Finds the Next available variable.
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > new file mode 100644
> > > index 0000000000..9d77c4916c
> > > --- /dev/null
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > @@ -0,0 +1,306 @@
> > > +/** @file
> > > +  Functions in this module are associated with variable parsing
> operations
> > > and
> > > +  are intended to be usable across variable driver source files.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#ifndef _VARIABLE_PARSING_H_
> > > +#define _VARIABLE_PARSING_H_
> > > +
> > > +#include <Guid/ImageAuthentication.h>
> > > +#include "Variable.h"
> > > +
> > > +/**
> > > +
> > > +  This code checks if variable header is valid or not.
> > > +
> > > +  @param Variable           Pointer to the Variable Header.
> > > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > +
> > > +  @retval TRUE              Variable header is valid.
> > > +  @retval FALSE             Variable header is not valid.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +IsValidVariableHeader (
> > > +  IN  VARIABLE_HEADER       *Variable,
> > > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the current status of Variable Store.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @retval EfiRaw         Variable store status is raw.
> > > +  @retval EfiValid       Variable store status is valid.
> > > +  @retval EfiInvalid     Variable store status is invalid.
> > > +
> > > +**/
> > > +VARIABLE_STORE_STATUS
> > > +GetVariableStoreStatus (
> > > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > +  );
> > > +
> > > +/**
> > > +  This code gets the size of variable header.
> > > +
> > > +  @return Size of variable header in bytes in type UINTN.
> > > +
> > > +**/
> > > +UINTN
> > > +GetVariableHeaderSize (
> > > +  VOID
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the size of name of variable.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return UINTN          Size of variable in bytes.
> > > +
> > > +**/
> > > +UINTN
> > > +NameSizeOfVariable (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +  This code sets the size of name of variable.
> > > +
> > > +  @param[in] Variable   Pointer to the Variable Header.
> > > +  @param[in] NameSize   Name size to set.
> > > +
> > > +**/
> > > +VOID
> > > +SetNameSizeOfVariable (
> > > +  IN VARIABLE_HEADER    *Variable,
> > > +  IN UINTN              NameSize
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the size of variable data.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Size of variable in bytes.
> > > +
> > > +**/
> > > +UINTN
> > > +DataSizeOfVariable (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +  This code sets the size of variable data.
> > > +
> > > +  @param[in] Variable   Pointer to the Variable Header.
> > > +  @param[in] DataSize   Data size to set.
> > > +
> > > +**/
> > > +VOID
> > > +SetDataSizeOfVariable (
> > > +  IN VARIABLE_HEADER    *Variable,
> > > +  IN UINTN              DataSize
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the variable name.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to Variable Name which is Unicode encoding.
> > > +
> > > +**/
> > > +CHAR16 *
> > > +GetVariableNamePtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +  This code gets the pointer to the variable guid.
> > > +
> > > +  @param Variable   Pointer to the Variable Header.
> > > +
> > > +  @return A EFI_GUID* pointer to Vendor Guid.
> > > +
> > > +**/
> > > +EFI_GUID *
> > > +GetVendorGuidPtr (
> > > +  IN VARIABLE_HEADER    *Variable
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the variable data.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to Variable Data.
> > > +
> > > +**/
> > > +UINT8 *
> > > +GetVariableDataPtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +  This code gets the variable data offset related to variable header.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Variable Data offset.
> > > +
> > > +**/
> > > +UINTN
> > > +GetVariableDataOffset (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the next variable header.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to next variable header.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetNextVariablePtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  Gets the pointer to the first variable header in given variable store area.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @return Pointer to the first variable header.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetStartPointer (
> > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > +  );
> > > +
> > > +/**
> > > +
> > > +  Gets the pointer to the end of the variable storage area.
> > > +
> > > +  This function gets pointer to the end of the variable storage
> > > +  area, according to the input variable store header.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @return Pointer to the end of the variable storage area.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetEndPointer (
> > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > +  );
> > > +
> > > +/**
> > > +  Compare two EFI_TIME data.
> > > +
> > > +
> > > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > > +
> > > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +VariableCompareTimeStampInternal (
> > > +  IN EFI_TIME               *FirstTime,
> > > +  IN EFI_TIME               *SecondTime
> > > +  );
> > > +
> > > +/**
> > > +  Find the variable in the specified variable store.
> > > +
> > > +  @param[in]       VariableName        Name of the variable to be found
> > > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > > +  @param[in]       IgnoreRtCheck       Ignore
> > EFI_VARIABLE_RUNTIME_ACCESS
> > > attribute
> > > +                                       check at runtime when searching variable.
> > > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > contains Variable Information.
> > > +
> > > +  @retval          EFI_SUCCESS         Variable found successfully
> > > +  @retval          EFI_NOT_FOUND       Variable not found
> > > +**/
> > > +EFI_STATUS
> > > +FindVariableEx (
> > > +  IN     CHAR16                  *VariableName,
> > > +  IN     EFI_GUID                *VendorGuid,
> > > +  IN     BOOLEAN                 IgnoreRtCheck,
> > > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > +  );
> > > +
> > > +/**
> > > +  This code Finds the Next available variable.
> > > +
> > > +  Caution: This function may receive untrusted input.
> > > +  This function may be invoked in SMM mode. This function will do basic
> > > validation, before parse the data.
> > > +
> > > +  @param[in]  VariableName  Pointer to variable name.
> > > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > > +  @param[out] VariablePtr   Pointer to variable header address.
> > > +
> > > +  @retval EFI_SUCCESS           The function completed successfully.
> > > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > string,
> > > while VendorGuid is NULL.
> > > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > and
> > > VendorGuid are not a name and
> > > +                                GUID of an existing variable.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +VariableServiceGetNextVariableInternal (
> > > +  IN  CHAR16                *VariableName,
> > > +  IN  EFI_GUID              *VendorGuid,
> > > +  OUT VARIABLE_HEADER       **VariablePtr
> > > +  );
> > > +
> > > +/**
> > > +  Routine used to track statistical information about variable usage.
> > > +  The data is stored in the EFI system table so it can be accessed later.
> > > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > +  build flag controls if this feature is enabled.
> > > +
> > > +  A read that hits in the cache will have Read and Cache true for
> > > +  the transaction. Data is allocated by this routine, but never
> > > +  freed.
> > > +
> > > +  @param[in] VariableName   Name of the Variable to track.
> > > +  @param[in] VendorGuid     Guid of the Variable to track.
> > > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > +  @param[in] Read           TRUE if GetVariable() was called.
> > > +  @param[in] Write          TRUE if SetVariable() was called.
> > > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > > +  @param[in] Cache          TRUE for a cache hit.
> > > +
> > > +**/
> > > +VOID
> > > +UpdateVariableInfo (
> > > +  IN  CHAR16                  *VariableName,
> > > +  IN  EFI_GUID                *VendorGuid,
> > > +  IN  BOOLEAN                 Volatile,
> > > +  IN  BOOLEAN                 Read,
> > > +  IN  BOOLEAN                 Write,
> > > +  IN  BOOLEAN                 Delete,
> > > +  IN  BOOLEAN                 Cache
> > > +  );
> > > +
> > > +#endif
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index f32c9c2808..76536308e6 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  **/
> > >
> > >  #include "Variable.h"
> > > +#include "VariableParsing.h"
> > >
> > >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> > >
> > > @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
> > >
> > >  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> > >
> > > -/**
> > > -  Routine used to track statistical information about variable usage.
> > > -  The data is stored in the EFI system table so it can be accessed later.
> > > -  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > -  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > -  build flag controls if this feature is enabled.
> > > -
> > > -  A read that hits in the cache will have Read and Cache true for
> > > -  the transaction. Data is allocated by this routine, but never
> > > -  freed.
> > > -
> > > -  @param[in] VariableName   Name of the Variable to track.
> > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > -  @param[in] Cache          TRUE for a cache hit.
> > > -
> > > -**/
> > > -VOID
> > > -UpdateVariableInfo (
> > > -  IN  CHAR16                  *VariableName,
> > > -  IN  EFI_GUID                *VendorGuid,
> > > -  IN  BOOLEAN                 Volatile,
> > > -  IN  BOOLEAN                 Read,
> > > -  IN  BOOLEAN                 Write,
> > > -  IN  BOOLEAN                 Delete,
> > > -  IN  BOOLEAN                 Cache
> > > -  )
> > > -{
> > > -  VARIABLE_INFO_ENTRY   *Entry;
> > > -
> > > -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > -
> > > -    if (AtRuntime ()) {
> > > -      // Don't collect statistics at runtime.
> > > -      return;
> > > -    }
> > > -
> > > -    if (gVariableInfo == NULL) {
> > > -      //
> > > -      // On the first call allocate a entry and place a pointer to it in
> > > -      // the EFI System Table.
> > > -      //
> > > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > -      ASSERT (gVariableInfo != NULL);
> > > -
> > > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > -      ASSERT (gVariableInfo->Name != NULL);
> > > -      StrCpyS (gVariableInfo->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > -      gVariableInfo->Volatile = Volatile;
> > > -    }
> > > -
> > > -
> > > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > > -        if (StrCmp (VariableName, Entry->Name) == 0) {
> > > -          if (Read) {
> > > -            Entry->ReadCount++;
> > > -          }
> > > -          if (Write) {
> > > -            Entry->WriteCount++;
> > > -          }
> > > -          if (Delete) {
> > > -            Entry->DeleteCount++;
> > > -          }
> > > -          if (Cache) {
> > > -            Entry->CacheCount++;
> > > -          }
> > > -
> > > -          return;
> > > -        }
> > > -      }
> > > -
> > > -      if (Entry->Next == NULL) {
> > > -        //
> > > -        // If the entry is not in the table add it.
> > > -        // Next iteration of the loop will fill in the data.
> > > -        //
> > > -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > -        ASSERT (Entry->Next != NULL);
> > > -
> > > -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > > -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > > -        ASSERT (Entry->Next->Name != NULL);
> > > -        StrCpyS (Entry->Next->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > -        Entry->Next->Volatile = Volatile;
> > > -      }
> > > -
> > > -    }
> > > -  }
> > > -}
> > > -
> > > -
> > > -/**
> > > -
> > > -  This code checks if variable header is valid or not.
> > > -
> > > -  @param Variable           Pointer to the Variable Header.
> > > -  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > -
> > > -  @retval TRUE              Variable header is valid.
> > > -  @retval FALSE             Variable header is not valid.
> > > -
> > > -**/
> > > -BOOLEAN
> > > -IsValidVariableHeader (
> > > -  IN  VARIABLE_HEADER       *Variable,
> > > -  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > -  )
> > > -{
> > > -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > > >StartId != VARIABLE_DATA)) {
> > > -    //
> > > -    // Variable is NULL or has reached the end of variable store,
> > > -    // or the StartId is not correct.
> > > -    //
> > > -    return FALSE;
> > > -  }
> > > -
> > > -  return TRUE;
> > > -}
> > > -
> > > -
> > >  /**
> > >
> > >    This function writes data to the FWH at the correct LBA even if the LBAs
> > > @@ -376,345 +252,6 @@ UpdateVariableStore (
> > >    return EFI_SUCCESS;
> > >  }
> > >
> > > -
> > > -/**
> > > -
> > > -  This code gets the current status of Variable Store.
> > > -
> > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > -
> > > -  @retval EfiRaw         Variable store status is raw.
> > > -  @retval EfiValid       Variable store status is valid.
> > > -  @retval EfiInvalid     Variable store status is invalid.
> > > -
> > > -**/
> > > -VARIABLE_STORE_STATUS
> > > -GetVariableStoreStatus (
> > > -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > -  )
> > > -{
> > > -  if ((CompareGuid (&VarStoreHeader->Signature,
> > > &gEfiAuthenticatedVariableGuid) ||
> > > -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid))
> &&
> > > -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > > -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > > -      ) {
> > > -
> > > -    return EfiValid;
> > > -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > > -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > > -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > > -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > > -             VarStoreHeader->Size == 0xffffffff &&
> > > -             VarStoreHeader->Format == 0xff &&
> > > -             VarStoreHeader->State == 0xff
> > > -          ) {
> > > -
> > > -    return EfiRaw;
> > > -  } else {
> > > -    return EfiInvalid;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -  This code gets the size of variable header.
> > > -
> > > -  @return Size of variable header in bytes in type UINTN.
> > > -
> > > -**/
> > > -UINTN
> > > -GetVariableHeaderSize (
> > > -  VOID
> > > -  )
> > > -{
> > > -  UINTN Value;
> > > -
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > -  } else {
> > > -    Value = sizeof (VARIABLE_HEADER);
> > > -  }
> > > -
> > > -  return Value;
> > > -}
> > > -
> > > -/**
> > > -
> > > -  This code gets the size of name of variable.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return UINTN          Size of variable in bytes.
> > > -
> > > -**/
> > > -UINTN
> > > -NameSizeOfVariable (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > -
> > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    if (AuthVariable->State == (UINT8) (-1) ||
> > > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > > -      return 0;
> > > -    }
> > > -    return (UINTN) AuthVariable->NameSize;
> > > -  } else {
> > > -    if (Variable->State == (UINT8) (-1) ||
> > > -        Variable->DataSize == (UINT32) (-1) ||
> > > -        Variable->NameSize == (UINT32) (-1) ||
> > > -        Variable->Attributes == (UINT32) (-1)) {
> > > -      return 0;
> > > -    }
> > > -    return (UINTN) Variable->NameSize;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -  This code sets the size of name of variable.
> > > -
> > > -  @param[in] Variable   Pointer to the Variable Header.
> > > -  @param[in] NameSize   Name size to set.
> > > -
> > > -**/
> > > -VOID
> > > -SetNameSizeOfVariable (
> > > -  IN VARIABLE_HEADER    *Variable,
> > > -  IN UINTN              NameSize
> > > -  )
> > > -{
> > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > -
> > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    AuthVariable->NameSize = (UINT32) NameSize;
> > > -  } else {
> > > -    Variable->NameSize = (UINT32) NameSize;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -
> > > -  This code gets the size of variable data.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Size of variable in bytes.
> > > -
> > > -**/
> > > -UINTN
> > > -DataSizeOfVariable (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > -
> > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    if (AuthVariable->State == (UINT8) (-1) ||
> > > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > > -      return 0;
> > > -    }
> > > -    return (UINTN) AuthVariable->DataSize;
> > > -  } else {
> > > -    if (Variable->State == (UINT8) (-1) ||
> > > -        Variable->DataSize == (UINT32) (-1) ||
> > > -        Variable->NameSize == (UINT32) (-1) ||
> > > -        Variable->Attributes == (UINT32) (-1)) {
> > > -      return 0;
> > > -    }
> > > -    return (UINTN) Variable->DataSize;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -  This code sets the size of variable data.
> > > -
> > > -  @param[in] Variable   Pointer to the Variable Header.
> > > -  @param[in] DataSize   Data size to set.
> > > -
> > > -**/
> > > -VOID
> > > -SetDataSizeOfVariable (
> > > -  IN VARIABLE_HEADER    *Variable,
> > > -  IN UINTN              DataSize
> > > -  )
> > > -{
> > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > -
> > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    AuthVariable->DataSize = (UINT32) DataSize;
> > > -  } else {
> > > -    Variable->DataSize = (UINT32) DataSize;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -
> > > -  This code gets the pointer to the variable name.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Pointer to Variable Name which is Unicode encoding.
> > > -
> > > -**/
> > > -CHAR16 *
> > > -GetVariableNamePtr (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > > -}
> > > -
> > > -/**
> > > -  This code gets the pointer to the variable guid.
> > > -
> > > -  @param Variable   Pointer to the Variable Header.
> > > -
> > > -  @return A EFI_GUID* pointer to Vendor Guid.
> > > -
> > > -**/
> > > -EFI_GUID *
> > > -GetVendorGuidPtr (
> > > -  IN VARIABLE_HEADER    *Variable
> > > -  )
> > > -{
> > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > -
> > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > -    return &AuthVariable->VendorGuid;
> > > -  } else {
> > > -    return &Variable->VendorGuid;
> > > -  }
> > > -}
> > > -
> > > -/**
> > > -
> > > -  This code gets the pointer to the variable data.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Pointer to Variable Data.
> > > -
> > > -**/
> > > -UINT8 *
> > > -GetVariableDataPtr (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  UINTN Value;
> > > -
> > > -  //
> > > -  // Be careful about pad size for alignment.
> > > -  //
> > > -  Value =  (UINTN) GetVariableNamePtr (Variable);
> > > -  Value += NameSizeOfVariable (Variable);
> > > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > -
> > > -  return (UINT8 *) Value;
> > > -}
> > > -
> > > -/**
> > > -  This code gets the variable data offset related to variable header.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Variable Data offset.
> > > -
> > > -**/
> > > -UINTN
> > > -GetVariableDataOffset (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  UINTN Value;
> > > -
> > > -  //
> > > -  // Be careful about pad size for alignment
> > > -  //
> > > -  Value = GetVariableHeaderSize ();
> > > -  Value += NameSizeOfVariable (Variable);
> > > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > -
> > > -  return Value;
> > > -}
> > > -
> > > -/**
> > > -
> > > -  This code gets the pointer to the next variable header.
> > > -
> > > -  @param Variable        Pointer to the Variable Header.
> > > -
> > > -  @return Pointer to next variable header.
> > > -
> > > -**/
> > > -VARIABLE_HEADER *
> > > -GetNextVariablePtr (
> > > -  IN  VARIABLE_HEADER   *Variable
> > > -  )
> > > -{
> > > -  UINTN Value;
> > > -
> > > -  Value =  (UINTN) GetVariableDataPtr (Variable);
> > > -  Value += DataSizeOfVariable (Variable);
> > > -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > > -
> > > -  //
> > > -  // Be careful about pad size for alignment.
> > > -  //
> > > -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > > -}
> > > -
> > > -/**
> > > -
> > > -  Gets the pointer to the first variable header in given variable store area.
> > > -
> > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > -
> > > -  @return Pointer to the first variable header.
> > > -
> > > -**/
> > > -VARIABLE_HEADER *
> > > -GetStartPointer (
> > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > -  )
> > > -{
> > > -  //
> > > -  // The start of variable store.
> > > -  //
> > > -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > > -}
> > > -
> > > -/**
> > > -
> > > -  Gets the pointer to the end of the variable storage area.
> > > -
> > > -  This function gets pointer to the end of the variable storage
> > > -  area, according to the input variable store header.
> > > -
> > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > -
> > > -  @return Pointer to the end of the variable storage area.
> > > -
> > > -**/
> > > -VARIABLE_HEADER *
> > > -GetEndPointer (
> > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > -  )
> > > -{
> > > -  //
> > > -  // The end of variable store
> > > -  //
> > > -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN)
> VarStoreHeader
> > +
> > > VarStoreHeader->Size);
> > > -}
> > > -
> > >  /**
> > >    Record variable error flag.
> > >
> > > @@ -1228,75 +765,6 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > -/**
> > > -  Find the variable in the specified variable store.
> > > -
> > > -  @param[in]       VariableName        Name of the variable to be found
> > > -  @param[in]       VendorGuid          Vendor GUID to be found.
> > > -  @param[in]       IgnoreRtCheck       Ignore
> > EFI_VARIABLE_RUNTIME_ACCESS
> > > attribute
> > > -                                       check at runtime when searching variable.
> > > -  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > contains Variable Information.
> > > -
> > > -  @retval          EFI_SUCCESS         Variable found successfully
> > > -  @retval          EFI_NOT_FOUND       Variable not found
> > > -**/
> > > -EFI_STATUS
> > > -FindVariableEx (
> > > -  IN     CHAR16                  *VariableName,
> > > -  IN     EFI_GUID                *VendorGuid,
> > > -  IN     BOOLEAN                 IgnoreRtCheck,
> > > -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > -  )
> > > -{
> > > -  VARIABLE_HEADER                *InDeletedVariable;
> > > -  VOID                           *Point;
> > > -
> > > -  PtrTrack->InDeletedTransitionPtr = NULL;
> > > -
> > > -  //
> > > -  // Find the variable by walk through HOB, volatile and non-volatile
> > variable
> > > store.
> > > -  //
> > > -  InDeletedVariable  = NULL;
> > > -
> > > -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > > -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > > -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > > -      ) {
> > > -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > > -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)
> > > -       ) {
> > > -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes
> &
> > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > -        if (VariableName[0] == 0) {
> > > -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)) {
> > > -            InDeletedVariable   = PtrTrack->CurrPtr;
> > > -          } else {
> > > -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > -            return EFI_SUCCESS;
> > > -          }
> > > -        } else {
> > > -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> > >CurrPtr)))
> > > {
> > > -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > > -
> > > -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > > -            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > > (PtrTrack->CurrPtr)) == 0) {
> > > -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)) {
> > > -                InDeletedVariable     = PtrTrack->CurrPtr;
> > > -              } else {
> > > -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > -                return EFI_SUCCESS;
> > > -              }
> > > -            }
> > > -          }
> > > -        }
> > > -      }
> > > -    }
> > > -  }
> > > -
> > > -  PtrTrack->CurrPtr = InDeletedVariable;
> > > -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > > -}
> > > -
> > > -
> > >  /**
> > >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> > >
> > > @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
> > >    }
> > >  }
> > >
> > > -/**
> > > -  Compare two EFI_TIME data.
> > > -
> > > -
> > > -  @param FirstTime           A pointer to the first EFI_TIME data.
> > > -  @param SecondTime          A pointer to the second EFI_TIME data.
> > > -
> > > -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > -  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > -
> > > -**/
> > > -BOOLEAN
> > > -VariableCompareTimeStampInternal (
> > > -  IN EFI_TIME               *FirstTime,
> > > -  IN EFI_TIME               *SecondTime
> > > -  )
> > > -{
> > > -  if (FirstTime->Year != SecondTime->Year) {
> > > -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > > -  } else if (FirstTime->Month != SecondTime->Month) {
> > > -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > > -  } else if (FirstTime->Day != SecondTime->Day) {
> > > -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > > -  } else if (FirstTime->Hour != SecondTime->Hour) {
> > > -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > > -  } else if (FirstTime->Minute != SecondTime->Minute) {
> > > -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > > -  }
> > > -
> > > -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > > -}
> > > -
> > >  /**
> > >    Update the variable region with Variable information. If
> > > EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
> > >    index of associated public key is needed.
> > > @@ -2885,166 +2321,6 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > -/**
> > > -  This code Finds the Next available variable.
> > > -
> > > -  Caution: This function may receive untrusted input.
> > > -  This function may be invoked in SMM mode. This function will do basic
> > > validation, before parse the data.
> > > -
> > > -  @param[in]  VariableName  Pointer to variable name.
> > > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > > -  @param[out] VariablePtr   Pointer to variable header address.
> > > -
> > > -  @retval EFI_SUCCESS           The function completed successfully.
> > > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > string,
> > > while VendorGuid is NULL.
> > > -  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > > VendorGuid are not a name and
> > > -                                GUID of an existing variable.
> > > -
> > > -**/
> > > -EFI_STATUS
> > > -EFIAPI
> > > -VariableServiceGetNextVariableInternal (
> > > -  IN  CHAR16                *VariableName,
> > > -  IN  EFI_GUID              *VendorGuid,
> > > -  OUT VARIABLE_HEADER       **VariablePtr
> > > -  )
> > > -{
> > > -  VARIABLE_STORE_TYPE     Type;
> > > -  VARIABLE_POINTER_TRACK  Variable;
> > > -  VARIABLE_POINTER_TRACK  VariableInHob;
> > > -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > > -  EFI_STATUS              Status;
> > > -  VARIABLE_STORE_HEADER
> > *VariableStoreHeader[VariableStoreTypeMax];
> > > -
> > > -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > > -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > > -    //
> > > -    // For VariableName is an empty string, FindVariable() will try to find
> and
> > > return
> > > -    // the first qualified variable, and if FindVariable() returns error
> > > (EFI_NOT_FOUND)
> > > -    // as no any variable is found, still go to return the error
> > > (EFI_NOT_FOUND).
> > > -    //
> > > -    if (VariableName[0] != 0) {
> > > -      //
> > > -      // For VariableName is not an empty string, and FindVariable()
> returns
> > > error as
> > > -      // VariableName and VendorGuid are not a name and GUID of an
> > existing
> > > variable,
> > > -      // there is no way to get next variable, follow spec to return
> > > EFI_INVALID_PARAMETER.
> > > -      //
> > > -      Status = EFI_INVALID_PARAMETER;
> > > -    }
> > > -    goto Done;
> > > -  }
> > > -
> > > -  if (VariableName[0] != 0) {
> > > -    //
> > > -    // If variable name is not NULL, get next variable.
> > > -    //
> > > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > -  }
> > > -
> > > -  //
> > > -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > -  // The index and attributes mapping must be kept in this order as
> > > FindVariable
> > > -  // makes use of this mapping to implement search algorithm.
> > > -  //
> > > -  VariableStoreHeader[VariableStoreTypeVolatile] =
> > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.VolatileVariableBase;
> > > -  VariableStoreHeader[VariableStoreTypeHob]      =
> > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.HobVariableBase;
> > > -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > > -
> > > -  while (TRUE) {
> > > -    //
> > > -    // Switch from Volatile to HOB, to Non-Volatile.
> > > -    //
> > > -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > > -      //
> > > -      // Find current storage index
> > > -      //
> > > -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > > Type++) {
> > > -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > > GetStartPointer (VariableStoreHeader[Type]))) {
> > > -          break;
> > > -        }
> > > -      }
> > > -      ASSERT (Type < VariableStoreTypeMax);
> > > -      //
> > > -      // Switch to next storage
> > > -      //
> > > -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > > -        if (VariableStoreHeader[Type] != NULL) {
> > > -          break;
> > > -        }
> > > -      }
> > > -      //
> > > -      // Capture the case that
> > > -      // 1. current storage is the last one, or
> > > -      // 2. no further storage
> > > -      //
> > > -      if (Type == VariableStoreTypeMax) {
> > > -        Status = EFI_NOT_FOUND;
> > > -        goto Done;
> > > -      }
> > > -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > > -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > > -      Variable.CurrPtr  = Variable.StartPtr;
> > > -    }
> > > -
> > > -    //
> > > -    // Variable is found
> > > -    //
> > > -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State
> ==
> > > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > > -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)) {
> > > -          //
> > > -          // If it is a IN_DELETED_TRANSITION variable,
> > > -          // and there is also a same ADDED one at the same time,
> > > -          // don't return it.
> > > -          //
> > > -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > > -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > > -          Status = FindVariableEx (
> > > -                     GetVariableNamePtr (Variable.CurrPtr),
> > > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > > -                     FALSE,
> > > -                     &VariablePtrTrack
> > > -                     );
> > > -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > > VAR_ADDED) {
> > > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > -            continue;
> > > -          }
> > > -        }
> > > -
> > > -        //
> > > -        // Don't return NV variable when HOB overrides it
> > > -        //
> > > -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > > -            (Variable.StartPtr == GetStartPointer
> > > (VariableStoreHeader[VariableStoreTypeNv]))
> > > -           ) {
> > > -          VariableInHob.StartPtr = GetStartPointer
> > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > -          VariableInHob.EndPtr   = GetEndPointer
> > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > -          Status = FindVariableEx (
> > > -                     GetVariableNamePtr (Variable.CurrPtr),
> > > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > > -                     FALSE,
> > > -                     &VariableInHob
> > > -                     );
> > > -          if (!EFI_ERROR (Status)) {
> > > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > -            continue;
> > > -          }
> > > -        }
> > > -
> > > -        *VariablePtr = Variable.CurrPtr;
> > > -        Status = EFI_SUCCESS;
> > > -        goto Done;
> > > -      }
> > > -    }
> > > -
> > > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > -  }
> > > -
> > > -Done:
> > > -  return Status;
> > > -}
> > > -
> > >  /**
> > >
> > >    This code Finds the Next available variable.
> > > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > index cb6fcebe2d..dc78f68fa9 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > @@ -1,12 +1,13 @@
> > >  /** @file
> > >    Provides variable driver extended services.
> > >
> > > -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
> > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  **/
> > >
> > >  #include "Variable.h"
> > > +#include "VariableParsing.h"
> > >
> > >  /**
> > >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > new file mode 100644
> > > index 0000000000..7de0a90772
> > > --- /dev/null
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > @@ -0,0 +1,731 @@
> > > +/** @file
> > > +  Functions in this module are associated with variable parsing
> operations
> > > and
> > > +  are intended to be usable across variable driver source files.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "VariableParsing.h"
> > > +
> > > +/**
> > > +
> > > +  This code checks if variable header is valid or not.
> > > +
> > > +  @param Variable           Pointer to the Variable Header.
> > > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > +
> > > +  @retval TRUE              Variable header is valid.
> > > +  @retval FALSE             Variable header is not valid.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +IsValidVariableHeader (
> > > +  IN  VARIABLE_HEADER       *Variable,
> > > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > +  )
> > > +{
> > > +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > > >StartId != VARIABLE_DATA)) {
> > > +    //
> > > +    // Variable is NULL or has reached the end of variable store,
> > > +    // or the StartId is not correct.
> > > +    //
> > > +    return FALSE;
> > > +  }
> > > +
> > > +  return TRUE;
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the current status of Variable Store.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @retval EfiRaw         Variable store status is raw.
> > > +  @retval EfiValid       Variable store status is valid.
> > > +  @retval EfiInvalid     Variable store status is invalid.
> > > +
> > > +**/
> > > +VARIABLE_STORE_STATUS
> > > +GetVariableStoreStatus (
> > > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > +  )
> > > +{
> > > +  if ((CompareGuid (&VarStoreHeader->Signature,
> > > &gEfiAuthenticatedVariableGuid) ||
> > > +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid))
> &&
> > > +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > > +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > > +      ) {
> > > +
> > > +    return EfiValid;
> > > +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > > +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > > +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > > +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > > +             VarStoreHeader->Size == 0xffffffff &&
> > > +             VarStoreHeader->Format == 0xff &&
> > > +             VarStoreHeader->State == 0xff
> > > +          ) {
> > > +
> > > +    return EfiRaw;
> > > +  } else {
> > > +    return EfiInvalid;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +  This code gets the size of variable header.
> > > +
> > > +  @return Size of variable header in bytes in type UINTN.
> > > +
> > > +**/
> > > +UINTN
> > > +GetVariableHeaderSize (
> > > +  VOID
> > > +  )
> > > +{
> > > +  UINTN Value;
> > > +
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > +  } else {
> > > +    Value = sizeof (VARIABLE_HEADER);
> > > +  }
> > > +
> > > +  return Value;
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the size of name of variable.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return UINTN          Size of variable in bytes.
> > > +
> > > +**/
> > > +UINTN
> > > +NameSizeOfVariable (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > +
> > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    if (AuthVariable->State == (UINT8) (-1) ||
> > > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > > +      return 0;
> > > +    }
> > > +    return (UINTN) AuthVariable->NameSize;
> > > +  } else {
> > > +    if (Variable->State == (UINT8) (-1) ||
> > > +        Variable->DataSize == (UINT32) (-1) ||
> > > +        Variable->NameSize == (UINT32) (-1) ||
> > > +        Variable->Attributes == (UINT32) (-1)) {
> > > +      return 0;
> > > +    }
> > > +    return (UINTN) Variable->NameSize;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +  This code sets the size of name of variable.
> > > +
> > > +  @param[in] Variable   Pointer to the Variable Header.
> > > +  @param[in] NameSize   Name size to set.
> > > +
> > > +**/
> > > +VOID
> > > +SetNameSizeOfVariable (
> > > +  IN VARIABLE_HEADER    *Variable,
> > > +  IN UINTN              NameSize
> > > +  )
> > > +{
> > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > +
> > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    AuthVariable->NameSize = (UINT32) NameSize;
> > > +  } else {
> > > +    Variable->NameSize = (UINT32) NameSize;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the size of variable data.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Size of variable in bytes.
> > > +
> > > +**/
> > > +UINTN
> > > +DataSizeOfVariable (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > +
> > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    if (AuthVariable->State == (UINT8) (-1) ||
> > > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > > +      return 0;
> > > +    }
> > > +    return (UINTN) AuthVariable->DataSize;
> > > +  } else {
> > > +    if (Variable->State == (UINT8) (-1) ||
> > > +        Variable->DataSize == (UINT32) (-1) ||
> > > +        Variable->NameSize == (UINT32) (-1) ||
> > > +        Variable->Attributes == (UINT32) (-1)) {
> > > +      return 0;
> > > +    }
> > > +    return (UINTN) Variable->DataSize;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +  This code sets the size of variable data.
> > > +
> > > +  @param[in] Variable   Pointer to the Variable Header.
> > > +  @param[in] DataSize   Data size to set.
> > > +
> > > +**/
> > > +VOID
> > > +SetDataSizeOfVariable (
> > > +  IN VARIABLE_HEADER    *Variable,
> > > +  IN UINTN              DataSize
> > > +  )
> > > +{
> > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > +
> > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    AuthVariable->DataSize = (UINT32) DataSize;
> > > +  } else {
> > > +    Variable->DataSize = (UINT32) DataSize;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the variable name.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to Variable Name which is Unicode encoding.
> > > +
> > > +**/
> > > +CHAR16 *
> > > +GetVariableNamePtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > > +}
> > > +
> > > +/**
> > > +  This code gets the pointer to the variable guid.
> > > +
> > > +  @param Variable   Pointer to the Variable Header.
> > > +
> > > +  @return A EFI_GUID* pointer to Vendor Guid.
> > > +
> > > +**/
> > > +EFI_GUID *
> > > +GetVendorGuidPtr (
> > > +  IN VARIABLE_HEADER    *Variable
> > > +  )
> > > +{
> > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > +
> > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +    return &AuthVariable->VendorGuid;
> > > +  } else {
> > > +    return &Variable->VendorGuid;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the variable data.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to Variable Data.
> > > +
> > > +**/
> > > +UINT8 *
> > > +GetVariableDataPtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  UINTN Value;
> > > +
> > > +  //
> > > +  // Be careful about pad size for alignment.
> > > +  //
> > > +  Value =  (UINTN) GetVariableNamePtr (Variable);
> > > +  Value += NameSizeOfVariable (Variable);
> > > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > +
> > > +  return (UINT8 *) Value;
> > > +}
> > > +
> > > +/**
> > > +  This code gets the variable data offset related to variable header.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Variable Data offset.
> > > +
> > > +**/
> > > +UINTN
> > > +GetVariableDataOffset (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  UINTN Value;
> > > +
> > > +  //
> > > +  // Be careful about pad size for alignment
> > > +  //
> > > +  Value = GetVariableHeaderSize ();
> > > +  Value += NameSizeOfVariable (Variable);
> > > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > +
> > > +  return Value;
> > > +}
> > > +
> > > +/**
> > > +
> > > +  This code gets the pointer to the next variable header.
> > > +
> > > +  @param Variable        Pointer to the Variable Header.
> > > +
> > > +  @return Pointer to next variable header.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetNextVariablePtr (
> > > +  IN  VARIABLE_HEADER   *Variable
> > > +  )
> > > +{
> > > +  UINTN Value;
> > > +
> > > +  Value =  (UINTN) GetVariableDataPtr (Variable);
> > > +  Value += DataSizeOfVariable (Variable);
> > > +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > > +
> > > +  //
> > > +  // Be careful about pad size for alignment.
> > > +  //
> > > +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > > +}
> > > +
> > > +/**
> > > +
> > > +  Gets the pointer to the first variable header in given variable store area.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @return Pointer to the first variable header.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetStartPointer (
> > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > +  )
> > > +{
> > > +  //
> > > +  // The start of variable store.
> > > +  //
> > > +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > > +}
> > > +
> > > +/**
> > > +
> > > +  Gets the pointer to the end of the variable storage area.
> > > +
> > > +  This function gets pointer to the end of the variable storage
> > > +  area, according to the input variable store header.
> > > +
> > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > +
> > > +  @return Pointer to the end of the variable storage area.
> > > +
> > > +**/
> > > +VARIABLE_HEADER *
> > > +GetEndPointer (
> > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > +  )
> > > +{
> > > +  //
> > > +  // The end of variable store
> > > +  //
> > > +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN)
> VarStoreHeader
> > +
> > > VarStoreHeader->Size);
> > > +}
> > > +
> > > +/**
> > > +  Compare two EFI_TIME data.
> > > +
> > > +
> > > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > > +
> > > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +VariableCompareTimeStampInternal (
> > > +  IN EFI_TIME               *FirstTime,
> > > +  IN EFI_TIME               *SecondTime
> > > +  )
> > > +{
> > > +  if (FirstTime->Year != SecondTime->Year) {
> > > +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > > +  } else if (FirstTime->Month != SecondTime->Month) {
> > > +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > > +  } else if (FirstTime->Day != SecondTime->Day) {
> > > +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > > +  } else if (FirstTime->Hour != SecondTime->Hour) {
> > > +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > > +  } else if (FirstTime->Minute != SecondTime->Minute) {
> > > +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > > +  }
> > > +
> > > +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > > +}
> > > +
> > > +/**
> > > +  Find the variable in the specified variable store.
> > > +
> > > +  @param[in]       VariableName        Name of the variable to be found
> > > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > > +  @param[in]       IgnoreRtCheck       Ignore
> > EFI_VARIABLE_RUNTIME_ACCESS
> > > attribute
> > > +                                       check at runtime when searching variable.
> > > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > contains Variable Information.
> > > +
> > > +  @retval          EFI_SUCCESS         Variable found successfully
> > > +  @retval          EFI_NOT_FOUND       Variable not found
> > > +**/
> > > +EFI_STATUS
> > > +FindVariableEx (
> > > +  IN     CHAR16                  *VariableName,
> > > +  IN     EFI_GUID                *VendorGuid,
> > > +  IN     BOOLEAN                 IgnoreRtCheck,
> > > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > +  )
> > > +{
> > > +  VARIABLE_HEADER                *InDeletedVariable;
> > > +  VOID                           *Point;
> > > +
> > > +  PtrTrack->InDeletedTransitionPtr = NULL;
> > > +
> > > +  //
> > > +  // Find the variable by walk through HOB, volatile and non-volatile
> > variable
> > > store.
> > > +  //
> > > +  InDeletedVariable  = NULL;
> > > +
> > > +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > > +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > > +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > > +      ) {
> > > +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > > +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)
> > > +       ) {
> > > +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes
> > &
> > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > +        if (VariableName[0] == 0) {
> > > +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)) {
> > > +            InDeletedVariable   = PtrTrack->CurrPtr;
> > > +          } else {
> > > +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > +            return EFI_SUCCESS;
> > > +          }
> > > +        } else {
> > > +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> > >CurrPtr)))
> > > {
> > > +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > > +
> > > +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > > +            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > > (PtrTrack->CurrPtr)) == 0) {
> > > +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION
> &
> > > VAR_ADDED)) {
> > > +                InDeletedVariable     = PtrTrack->CurrPtr;
> > > +              } else {
> > > +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > +                return EFI_SUCCESS;
> > > +              }
> > > +            }
> > > +          }
> > > +        }
> > > +      }
> > > +    }
> > > +  }
> > > +
> > > +  PtrTrack->CurrPtr = InDeletedVariable;
> > > +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  This code Finds the Next available variable.
> > > +
> > > +  Caution: This function may receive untrusted input.
> > > +  This function may be invoked in SMM mode. This function will do basic
> > > validation, before parse the data.
> > > +
> > > +  @param[in]  VariableName  Pointer to variable name.
> > > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > > +  @param[out] VariablePtr   Pointer to variable header address.
> > > +
> > > +  @retval EFI_SUCCESS           The function completed successfully.
> > > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > string,
> > > while VendorGuid is NULL.
> > > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > and
> > > VendorGuid are not a name and
> > > +                                GUID of an existing variable.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +VariableServiceGetNextVariableInternal (
> > > +  IN  CHAR16                *VariableName,
> > > +  IN  EFI_GUID              *VendorGuid,
> > > +  OUT VARIABLE_HEADER       **VariablePtr
> > > +  )
> > > +{
> > > +  VARIABLE_STORE_TYPE     Type;
> > > +  VARIABLE_POINTER_TRACK  Variable;
> > > +  VARIABLE_POINTER_TRACK  VariableInHob;
> > > +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > > +  EFI_STATUS              Status;
> > > +  VARIABLE_STORE_HEADER
> > > *VariableStoreHeader[VariableStoreTypeMax];
> > > +
> > > +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > > +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > > +    //
> > > +    // For VariableName is an empty string, FindVariable() will try to find
> > and
> > > return
> > > +    // the first qualified variable, and if FindVariable() returns error
> > > (EFI_NOT_FOUND)
> > > +    // as no any variable is found, still go to return the error
> > > (EFI_NOT_FOUND).
> > > +    //
> > > +    if (VariableName[0] != 0) {
> > > +      //
> > > +      // For VariableName is not an empty string, and FindVariable()
> returns
> > > error as
> > > +      // VariableName and VendorGuid are not a name and GUID of an
> > > existing variable,
> > > +      // there is no way to get next variable, follow spec to return
> > > EFI_INVALID_PARAMETER.
> > > +      //
> > > +      Status = EFI_INVALID_PARAMETER;
> > > +    }
> > > +    goto Done;
> > > +  }
> > > +
> > > +  if (VariableName[0] != 0) {
> > > +    //
> > > +    // If variable name is not NULL, get next variable.
> > > +    //
> > > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > +  }
> > > +
> > > +  //
> > > +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > +  // The index and attributes mapping must be kept in this order as
> > > FindVariable
> > > +  // makes use of this mapping to implement search algorithm.
> > > +  //
> > > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.VolatileVariableBase;
> > > +  VariableStoreHeader[VariableStoreTypeHob]      =
> > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.HobVariableBase;
> > > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > > +
> > > +  while (TRUE) {
> > > +    //
> > > +    // Switch from Volatile to HOB, to Non-Volatile.
> > > +    //
> > > +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > > +      //
> > > +      // Find current storage index
> > > +      //
> > > +      for (Type = (VARIABLE_STORE_TYPE) 0; Type <
> VariableStoreTypeMax;
> > > Type++) {
> > > +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > > GetStartPointer (VariableStoreHeader[Type]))) {
> > > +          break;
> > > +        }
> > > +      }
> > > +      ASSERT (Type < VariableStoreTypeMax);
> > > +      //
> > > +      // Switch to next storage
> > > +      //
> > > +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > > +        if (VariableStoreHeader[Type] != NULL) {
> > > +          break;
> > > +        }
> > > +      }
> > > +      //
> > > +      // Capture the case that
> > > +      // 1. current storage is the last one, or
> > > +      // 2. no further storage
> > > +      //
> > > +      if (Type == VariableStoreTypeMax) {
> > > +        Status = EFI_NOT_FOUND;
> > > +        goto Done;
> > > +      }
> > > +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > > +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > > +      Variable.CurrPtr  = Variable.StartPtr;
> > > +    }
> > > +
> > > +    //
> > > +    // Variable is found
> > > +    //
> > > +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State
> > ==
> > > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > > +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > VAR_ADDED)) {
> > > +          //
> > > +          // If it is a IN_DELETED_TRANSITION variable,
> > > +          // and there is also a same ADDED one at the same time,
> > > +          // don't return it.
> > > +          //
> > > +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > > +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > > +          Status = FindVariableEx (
> > > +                     GetVariableNamePtr (Variable.CurrPtr),
> > > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > > +                     FALSE,
> > > +                     &VariablePtrTrack
> > > +                     );
> > > +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > > VAR_ADDED) {
> > > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > +            continue;
> > > +          }
> > > +        }
> > > +
> > > +        //
> > > +        // Don't return NV variable when HOB overrides it
> > > +        //
> > > +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > > +            (Variable.StartPtr == GetStartPointer
> > > (VariableStoreHeader[VariableStoreTypeNv]))
> > > +           ) {
> > > +          VariableInHob.StartPtr = GetStartPointer
> > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > +          VariableInHob.EndPtr   = GetEndPointer
> > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > +          Status = FindVariableEx (
> > > +                     GetVariableNamePtr (Variable.CurrPtr),
> > > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > > +                     FALSE,
> > > +                     &VariableInHob
> > > +                     );
> > > +          if (!EFI_ERROR (Status)) {
> > > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > +            continue;
> > > +          }
> > > +        }
> > > +
> > > +        *VariablePtr = Variable.CurrPtr;
> > > +        Status = EFI_SUCCESS;
> > > +        goto Done;
> > > +      }
> > > +    }
> > > +
> > > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > +  }
> > > +
> > > +Done:
> > > +  return Status;
> > > +}
> > > +
> > > +/**
> > > +  Routine used to track statistical information about variable usage.
> > > +  The data is stored in the EFI system table so it can be accessed later.
> > > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > +  build flag controls if this feature is enabled.
> > > +
> > > +  A read that hits in the cache will have Read and Cache true for
> > > +  the transaction. Data is allocated by this routine, but never
> > > +  freed.
> > > +
> > > +  @param[in] VariableName   Name of the Variable to track.
> > > +  @param[in] VendorGuid     Guid of the Variable to track.
> > > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > +  @param[in] Read           TRUE if GetVariable() was called.
> > > +  @param[in] Write          TRUE if SetVariable() was called.
> > > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > > +  @param[in] Cache          TRUE for a cache hit.
> > > +
> > > +**/
> > > +VOID
> > > +UpdateVariableInfo (
> > > +  IN  CHAR16                  *VariableName,
> > > +  IN  EFI_GUID                *VendorGuid,
> > > +  IN  BOOLEAN                 Volatile,
> > > +  IN  BOOLEAN                 Read,
> > > +  IN  BOOLEAN                 Write,
> > > +  IN  BOOLEAN                 Delete,
> > > +  IN  BOOLEAN                 Cache
> > > +  )
> > > +{
> > > +  VARIABLE_INFO_ENTRY   *Entry;
> > > +
> > > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > +
> > > +    if (AtRuntime ()) {
> > > +      // Don't collect statistics at runtime.
> > > +      return;
> > > +    }
> > > +
> > > +    if (gVariableInfo == NULL) {
> > > +      //
> > > +      // On the first call allocate a entry and place a pointer to it in
> > > +      // the EFI System Table.
> > > +      //
> > > +      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > +      ASSERT (gVariableInfo != NULL);
> > > +
> > > +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > +      ASSERT (gVariableInfo->Name != NULL);
> > > +      StrCpyS (gVariableInfo->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > +      gVariableInfo->Volatile = Volatile;
> > > +    }
> > > +
> > > +
> > > +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > > +        if (StrCmp (VariableName, Entry->Name) == 0) {
> > > +          if (Read) {
> > > +            Entry->ReadCount++;
> > > +          }
> > > +          if (Write) {
> > > +            Entry->WriteCount++;
> > > +          }
> > > +          if (Delete) {
> > > +            Entry->DeleteCount++;
> > > +          }
> > > +          if (Cache) {
> > > +            Entry->CacheCount++;
> > > +          }
> > > +
> > > +          return;
> > > +        }
> > > +      }
> > > +
> > > +      if (Entry->Next == NULL) {
> > > +        //
> > > +        // If the entry is not in the table add it.
> > > +        // Next iteration of the loop will fill in the data.
> > > +        //
> > > +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > +        ASSERT (Entry->Next != NULL);
> > > +
> > > +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > > +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > > +        ASSERT (Entry->Next->Name != NULL);
> > > +        StrCpyS (Entry->Next->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > +        Entry->Next->Volatile = Volatile;
> > > +      }
> > > +
> > > +    }
> > > +  }
> > > +}
> > > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > index ec463d063e..ce409f22a3 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  #include <Guid/SmmVariableCommon.h>
> > >  #include "Variable.h"
> > > +#include "VariableParsing.h"
> > >
> > >  BOOLEAN                                              mAtRuntime              = FALSE;
> > >  UINT8                                                *mVariableBufferPayload = NULL;
> > > --
> > > 2.16.2.windows.1
> >
> 


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

* Re: [edk2-devel] [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  2019-10-03 18:05     ` Kubacki, Michael A
@ 2019-10-08  2:11       ` Wu, Hao A
  2019-10-08 21:49         ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-08  2:11 UTC (permalink / raw)
  To: devel@edk2.groups.io, Kubacki, Michael A
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Kubacki, Michael A
> Sent: Friday, October 04, 2019 2:06 AM
> To: Wu, Hao A; devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> Subject: Re: [edk2-devel] [PATCH V2 3/9] MdeModulePkg/Variable:
> Parameterize VARIABLE_INFO_ENTRY buffer
> 
> Your understanding is correct.


Thanks for the confirmation.
Could you help to refine the commit message in the next series? Thanks.

Best Regards,
Hao Wu


> 
> Thanks,
> Michael
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Thursday, October 3, 2019 1:04 AM
> > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > Subject: RE: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> > VARIABLE_INFO_ENTRY buffer
> >
> > > -----Original Message-----
> > > From: Kubacki, Michael A
> > > Sent: Saturday, September 28, 2019 9:47 AM
> > > To: devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > > Subject: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> > > VARIABLE_INFO_ENTRY buffer
> > >
> > > UpdateVariableInfo () currently accepts parameters regarding updates
> > > to be made to a global variable of type VARIABLE_INFO_ENTRY. This
> > > change passes the structure by pointer to UpdateVariableInfo () so
> > > structures can be updated outside the fixed global variable.
> >
> >
> > For:
> > "... so structures can be updated outside the fixed global variable "
> >
> > Do you mean:
> >
> > VARIABLE_INFO_ENTRY structure pointers other than "&gVariableInfo" can
> > be passed to UpdateVariableInfo().
> >
> > Is my understanding correct? If so,
> > Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
> >
> > Best Regards,
> > Hao Wu
> >
> >
> > >
> > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > Cc: Eric Dong <eric.dong@intel.com>
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Liming Gao <liming.gao@intel.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Ray Ni <ray.ni@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > ---
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 18
> > > +++++----
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 14
> +++---
> > -
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 41
> > > +++++++++++---------
> > >  3 files changed, 39 insertions(+), 34 deletions(-)
> > >
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > index 0d231511ea..6f2000f3ee 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > @@ -286,13 +286,14 @@ GetNextVariableEx (
> > >    the transaction. Data is allocated by this routine, but never
> > >    freed.
> > >
> > > -  @param[in] VariableName   Name of the Variable to track.
> > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > -  @param[in] Cache          TRUE for a cache hit.
> > > +  @param[in]      VariableName   Name of the Variable to track.
> > > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > > +  @param[in]      Read           TRUE if GetVariable() was called.
> > > +  @param[in]      Write          TRUE if SetVariable() was called.
> > > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > > +  @param[in]      Cache          TRUE for a cache hit.
> > > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > > VARIABLE_INFO_ENTRY structures.
> > >
> > >  **/
> > >  VOID
> > > @@ -303,7 +304,8 @@ UpdateVariableInfo (
> > >    IN  BOOLEAN                 Read,
> > >    IN  BOOLEAN                 Write,
> > >    IN  BOOLEAN                 Delete,
> > > -  IN  BOOLEAN                 Cache
> > > +  IN  BOOLEAN                 Cache,
> > > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > >    );
> > >
> > >  #endif
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index 816e8f7b8f..1a57d7e1ba 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -1641,7 +1641,7 @@ UpdateVariable (
> > >          // go to delete this variable in variable HOB and
> > >          // try to flush other variables from HOB to flash.
> > >          //
> > > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > FALSE,
> > > TRUE, FALSE);
> > > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > > + FALSE,
> > > TRUE, FALSE, &gVariableInfo);
> > >          FlushHobVariableToFlash (VariableName, VendorGuid);
> > >          return EFI_SUCCESS;
> > >        }
> > > @@ -1758,7 +1758,7 @@ UpdateVariable (
> > >                   &State
> > >                   );
> > >        if (!EFI_ERROR (Status)) {
> > > -        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > > FALSE, FALSE, TRUE, FALSE);
> > > +        UpdateVariableInfo (VariableName, VendorGuid,
> > > + Variable->Volatile,
> > > FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
> > >          if (!Variable->Volatile) {
> > >            CacheVariable->CurrPtr->State = State;
> > >            FlushHobVariableToFlash (VariableName, VendorGuid); @@
> > > -1777,7 +1777,7 @@ UpdateVariable (
> > >        //
> > >        // Variable content unchanged and no need to update timestamp,
> > > just return.
> > >        //
> > > -      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > > FALSE, TRUE, FALSE, FALSE);
> > > +      UpdateVariableInfo (VariableName, VendorGuid,
> > > + Variable->Volatile,
> > > FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
> > >        Status = EFI_SUCCESS;
> > >        goto Done;
> > >      } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) || @@
> > > -2006,7 +2006,7 @@ UpdateVariable (
> > >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > > CacheVariable->Variable-
> > > >StartPtr));
> > >            CacheVariable->InDeletedTransitionPtr = NULL;
> > >          }
> > > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> TRUE,
> > > FALSE, FALSE);
> > > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > > + TRUE,
> > > FALSE, FALSE, &gVariableInfo);
> > >          FlushHobVariableToFlash (VariableName, VendorGuid);
> > >        } else {
> > >          if (IsCommonUserVariable && ((VarSize +
> > > mVariableModuleGlobal-
> > > >CommonUserVariableTotalSize) > mVariableModuleGlobal-
> > > >CommonMaxUserVariableSpace)) {
> > > @@ -2156,7 +2156,7 @@ UpdateVariable (
> > >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > > CacheVariable->Variable-
> > > >StartPtr));
> > >            CacheVariable->InDeletedTransitionPtr = NULL;
> > >          }
> > > -        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE,
> > > FALSE, FALSE);
> > > +        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE,
> > > + TRUE,
> > > FALSE, FALSE, &gVariableInfo);
> > >        }
> > >        goto Done;
> > >      }
> > > @@ -2227,7 +2227,7 @@ UpdateVariable (
> > >    }
> > >
> > >    if (!EFI_ERROR (Status)) {
> > > -    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE,
> TRUE,
> > > FALSE, FALSE);
> > > +    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE,
> > > + TRUE,
> > > FALSE, FALSE, &gVariableInfo);
> > >      if (!Volatile) {
> > >        FlushHobVariableToFlash (VariableName, VendorGuid);
> > >      }
> > > @@ -2306,7 +2306,7 @@ VariableServiceGetVariable (
> > >      }
> > >
> > >      *DataSize = VarDataSize;
> > > -    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile,
> > TRUE,
> > > FALSE, FALSE, FALSE);
> > > +    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile,
> > > + TRUE,
> > > FALSE, FALSE, FALSE, &gVariableInfo);
> > >
> > >      Status = EFI_SUCCESS;
> > >      goto Done;
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > index 9bc5369a90..394195342d 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > @@ -660,13 +660,14 @@ Done:
> > >    the transaction. Data is allocated by this routine, but never
> > >    freed.
> > >
> > > -  @param[in] VariableName   Name of the Variable to track.
> > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > -  @param[in] Cache          TRUE for a cache hit.
> > > +  @param[in]      VariableName   Name of the Variable to track.
> > > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > > +  @param[in]      Read           TRUE if GetVariable() was called.
> > > +  @param[in]      Write          TRUE if SetVariable() was called.
> > > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > > +  @param[in]      Cache          TRUE for a cache hit.
> > > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > > VARIABLE_INFO_ENTRY structures.
> > >
> > >  **/
> > >  VOID
> > > @@ -677,35 +678,38 @@ UpdateVariableInfo (
> > >    IN  BOOLEAN                 Read,
> > >    IN  BOOLEAN                 Write,
> > >    IN  BOOLEAN                 Delete,
> > > -  IN  BOOLEAN                 Cache
> > > +  IN  BOOLEAN                 Cache,
> > > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > >    )
> > >  {
> > >    VARIABLE_INFO_ENTRY   *Entry;
> > >
> > >    if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > -
> > > +    if (VariableName == NULL || VendorGuid == NULL || VariableInfo ==
> > > NULL) {
> > > +      return;
> > > +    }
> > >      if (AtRuntime ()) {
> > >        // Don't collect statistics at runtime.
> > >        return;
> > >      }
> > >
> > > -    if (gVariableInfo == NULL) {
> > > +    if (*VariableInfo == NULL) {
> > >        //
> > >        // On the first call allocate a entry and place a pointer to it in
> > >        // the EFI System Table.
> > >        //
> > > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > -      ASSERT (gVariableInfo != NULL);
> > > +      *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > +      ASSERT (*VariableInfo != NULL);
> > >
> > > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > -      ASSERT (gVariableInfo->Name != NULL);
> > > -      StrCpyS (gVariableInfo->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > -      gVariableInfo->Volatile = Volatile;
> > > +      CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
> > > +      (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
> > > +      ASSERT ((*VariableInfo)->Name != NULL);
> > > +      StrCpyS ((*VariableInfo)->Name,
> > > + StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > > +      (*VariableInfo)->Volatile = Volatile;
> > >      }
> > >
> > >
> > > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > +    for (Entry = (*VariableInfo); Entry != NULL; Entry = Entry->Next)
> > > + {
> > >        if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > >          if (StrCmp (VariableName, Entry->Name) == 0) {
> > >            if (Read) {
> > > @@ -739,7 +743,6 @@ UpdateVariableInfo (
> > >          StrCpyS (Entry->Next->Name,
> > > StrSize(VariableName)/sizeof(CHAR16),
> > > VariableName);
> > >          Entry->Next->Volatile = Volatile;
> > >        }
> > > -
> > >      }
> > >    }
> > >  }
> > > --
> > > 2.16.2.windows.1
> >
> 
> 
> 


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

* Re: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support
  2019-10-03 21:53     ` Kubacki, Michael A
  2019-10-03 22:01       ` Michael D Kinney
  2019-10-04  6:38       ` Laszlo Ersek
@ 2019-10-08  2:12       ` Wu, Hao A
  2 siblings, 0 replies; 45+ messages in thread
From: Wu, Hao A @ 2019-10-08  2:12 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io, Wang, Jian J,
	Yao, Jiewen
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

Inline replies below:


> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Friday, October 04, 2019 5:54 AM
> To: Wu, Hao A; devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> cache support
> 
> #1 - The plan is to remove the polling entirely in V3.
> 
> #2 - I'd prefer to take a definitive direction and reduce validation and
> maintenance
>         effort but you and Laszlo both requested this so I'll add a FeaturePCD to
> control
>         activation of the runtime cache in this patch series. Perhaps this can be
> removed
>         in the future.
> 
> #3 - Will be done in V3.
> 
> Other replies are inline.
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Thursday, October 3, 2019 1:05 AM
> > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > Subject: RE: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> > cache support
> >
> > Before any comment on the patch, since I am not experienced in the
> > Variable
> > driver, I would like to ask for help from other reviewers to look into this
> > patch and provide feedbacks as well. Thanks in advance.
> >
> > With the above fact, some comments provided below maybe wrong. So
> > please help
> > to kindly correct me.
> >
> >
> > Some general comments:
> > 1. I am not sure if bringing the TimerLib dependency (delay in acquiring the
> >    runtime cache read lock) to variable driver (a software driver for the most
> >    part) is a good idea.
> >
> >    Hope other reviewers can provide some feedbacks for this. Thanks in
> > advance.
> >
> > 2. In my opinion, I prefer a switch can be provided for platform owners to
> >    choose between using the runtime cache and going through SMM for
> > GetVariable
> >    (and for GetNextVariableName in the next patch as well).
> >
> >    If platform owners feel uncomfortable with using the runtime cache with
> >    regard to the security perspective, they can switch to the origin solution.
> >
> > 3. Please help to remove the 'EFIAPI' keyword for new driver internal
> > functions;
> >
> >
> > Inline comments below:
> >
> >
> > > -----Original Message-----
> > > From: Kubacki, Michael A
> > > Sent: Saturday, September 28, 2019 9:47 AM
> > > To: devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney,
> > > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > > Subject: [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable()
> > > cache support
> > >
> > > REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2220
> > >
> > > This change reduces SMIs for GetVariable () by maintaining a
> > > UEFI variable cache in Runtime DXE in addition to the pre-
> > > existing cache in SMRAM. When the Runtime Service GetVariable()
> > > is invoked, a Runtime DXE cache is used instead of triggering an
> > > SMI to VariableSmm. This can improve overall system performance
> > > by servicing variable read requests without rendezvousing all
> > > cores into SMM.
> > >
> > > The following are important points regarding this change.
> > >
> > > 1. All of the non-volatile storage contents are loaded into the
> > >    cache upon driver load. This one time load operation from storage
> > >    is preferred as opposed to building the cache on demand. An on-
> > >    demand cache would require a fallback SMI to load data into the
> > >    cache as variables are requested.
> > >
> > > 2. SetVariable () requests will continue to always trigger an SMI.
> > >    This occurs regardless of whether the variable is volatile or
> > >    non-volatile.
> > >
> > > 3. Both volatile and non-volatile variables are cached in a runtime
> > >    buffer. As is the case in the current EDK II variable driver, they
> > >    continue to be cached in separate buffers.
> > >
> > > 4. The cache in Runtime DXE and SMM are intended to be exact copies
> > >    of one another. All SMM variable accesses only return data from the
> > >    SMM cache. The runtime caches are only updated after the variable I/O
> > >    operation is successful in SMM. The runtime caches are only updated
> > >    from SMM.
> > >
> > > 5. Synchronization mechanisms are in place to ensure the runtime cache
> > >    content integrity with the SMM cache. These may result in updates to
> > >    runtime cache that are the same in content but different in offset and
> > >    size from updates to the SMM cache.
> > >
> > > When using SMM variables, two caches will now be present.
> > > 1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to
> > > service
> > >    Runtime Services GetVariable () and GetNextVariableName () callers.
> > > 2. "SMM Cache" - Maintained in VariableSmm to service SMM
> GetVariable
> > ()
> > >    and GetNextVariableName () callers.
> > >    a. This cache is retained so SMM modules do not operate on data
> outside
> > >       SMRAM.
> > >
> > > It is possible to view UEFI variable read and write statistics by setting
> > > the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics
> > > FeaturePcd
> > > to TRUE and using the VariableInfo UEFI application in MdeModulePkg to
> > > dump
> > > variable statistics to the console. By doing so, a user can view the number
> > > of GetVariable () hits from the Runtime DXE variable driver (Runtime
> Cache
> > > hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for
> > > GetVariable () will occur when SMM modules invoke GetVariable ().
> > >
> > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > Cc: Eric Dong <eric.dong@intel.com>
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Liming Gao <liming.gao@intel.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Ray Ni <ray.ni@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > ---
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > |   2 +
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> |
> > 2
> > > +
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i
> > > nf |  31 +-
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > > |   2 +
> > >  MdeModulePkg/Include/Guid/SmmVariableCommon.h                        |  29
> +-
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h                |  39
> > +-
> > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> > > |  47 ++
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c                |  44
> > +-
> > >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > > | 153 +++++
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c             |
> > 114
> > > +++-
> > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.
> > > c   | 608 +++++++++++++++++---
> > >  11 files changed, 966 insertions(+), 105 deletions(-)
> > >
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > index 08a5490787..ceea5d1ff9 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > @@ -40,6 +40,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> >
> >
> > Per my understanding, the module specified by VariableRuntimeDxe.inf
> > does not
> > involve SMM/SMI for variable services (like GetVariable). It looks weird to
> > me
> > for this INF to include the newly introduced runtime cache codes (below
> > source
> > header files):
> >
> > VariableRuntimeCache.c
> > VariableRuntimeCache.h
> >
> >
> 
> This is because Variable.c is common to the runtime DXE and SMM variable
> driver and it contains the code to update variable caches. The runtime cache
> synchronization function (SynchronizeRuntimeVariableCache ()) will return
> if the runtime cache pointer is NULL.


Got it.

I was previously thinking whether it is possible to drop such dependency for
this non-SMM related runtime module. But if the dependency is coupled with
Variable.c as well, I am fine with the change above.


> 
> > >    PrivilegePolymorphic.h
> > >    Measurement.c
> > >    TcgMorLockDxe.c
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > index 6dc2721b81..bc3033588d 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > @@ -49,6 +49,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.inf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.inf
> > > index 14894e6f13..70837ac6e0 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.inf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.inf
> > > @@ -13,7 +13,7 @@
> > >  #  may not be modified without authorization. If platform fails to protect
> > > these resources,
> > >  #  the authentication service provided in this driver will be broken, and
> the
> > > behavior is undefined.
> > >  #
> > > -# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> > > +# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
> > >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #
> > >  ##
> > > @@ -39,6 +39,10 @@
> > >    VariableSmmRuntimeDxe.c
> > >    PrivilegePolymorphic.h
> > >    Measurement.c
> > > +  VariableParsing.c
> > > +  VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >
> > >  [Packages]
> > >    MdePkg/MdePkg.dec
> > > @@ -49,6 +53,7 @@
> > >    BaseLib
> > >    UefiBootServicesTableLib
> > >    DebugLib
> > > +  TimerLib
> > >    UefiRuntimeLib
> > >    DxeServicesTableLib
> > >    UefiDriverEntryPoint
> > > @@ -65,7 +70,29 @@
> > >    gEdkiiVariableLockProtocolGuid                ## PRODUCES
> > >    gEdkiiVarCheckProtocolGuid                    ## PRODUCES
> > >
> > > +[Pcd]
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                      ##
> > > CONSUMES
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize
> > ##
> > > CONSUMES
> > > +
> gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize
> > > ## CONSUMES
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                    ##
> > > CONSUMES
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize
> ##
> > > CONSUMES
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize
> > > ## CONSUMES
> > > +
> > >
> >
> gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpace
> > > Size  ## CONSUMES
> >
> >
> > Not sure if the above PCDs are really needed by VariableSmmRuntimeDxe.
> >
> >
> 
> I will double check and remove any not required.
> 
> > > +
> > > +[FeaturePcd]
> > > +  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics
> > ##
> > > CONSUMES
> > > +
> > >  [Guids]
> > > +  ## PRODUCES             ## GUID # Signature of Variable store header
> > > +  ## CONSUMES             ## GUID # Signature of Variable store header
> > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > +  gEfiAuthenticatedVariableGuid
> > > +
> > > +  ## PRODUCES             ## GUID # Signature of Variable store header
> > > +  ## CONSUMES             ## GUID # Signature of Variable store header
> > > +  ## SOMETIMES_PRODUCES   ## SystemTable
> > > +  gEfiVariableGuid
> > > +
> > >    gEfiEventVirtualAddressChangeGuid             ## CONSUMES ## Event
> > >    gEfiEventExitBootServicesGuid                 ## CONSUMES ## Event
> > >    ## CONSUMES ## GUID # Locate protocol
> > > @@ -82,6 +109,8 @@
> > >    ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > >    gEfiImageSecurityDatabaseGuid
> > >
> > > +  gEdkiiPiSmmCommunicationRegionTableGuid       ##
> > > SOMETIMES_CONSUMES ## SystemTable
> > > +
> > >  [Depex]
> > >    gEfiSmmCommunicationProtocolGuid
> > >
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > nf
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > inf
> > > index ca9d23ce9f..95c5310c0b 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > nf
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > inf
> > > @@ -49,6 +49,8 @@
> > >    VariableNonVolatile.h
> > >    VariableParsing.c
> > >    VariableParsing.h
> > > +  VariableRuntimeCache.c
> > > +  VariableRuntimeCache.h
> > >    VarCheck.c
> > >    Variable.h
> > >    PrivilegePolymorphic.h
> > > diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > index c527a59891..ceef44dfd2 100644
> > > --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h
> > > @@ -1,7 +1,7 @@
> > >  /** @file
> > >    The file defined some common structures used for communicating
> > > between SMM variable module and SMM variable wrapper module.
> > >
> > > -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
> > > +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
> > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  **/
> > > @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #ifndef _SMM_VARIABLE_COMMON_H_
> > >  #define _SMM_VARIABLE_COMMON_H_
> > >
> > > +#include <Guid/VariableFormat.h>
> > >  #include <Protocol/VarCheck.h>
> > >
> > >  #define EFI_SMM_VARIABLE_WRITE_GUID \
> > > @@ -66,6 +67,16 @@ typedef struct {
> > >  #define
> > > SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET  10
> > >
> > >  #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE        11
> > > +//
> > > +// The payload for this function is
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > +//
> > > +#define
> > >
> SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > 12
> > > +
> > > +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
> > 13
> > > +//
> > > +// The payload for this function is
> > > SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > +//
> > > +#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
> > > 14
> > >
> > >  ///
> > >  /// Size of SMM communicate header, without including the payload.
> > > @@ -120,4 +131,20 @@ typedef struct {
> > >    UINTN                         VariablePayloadSize;
> > >  } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE;
> > >
> > > +typedef struct {
> > > +  BOOLEAN                 *ReadLock;
> > > +  BOOLEAN                 *PendingUpdate;
> > > +  BOOLEAN                 *HobFlushComplete;
> > > +  VARIABLE_STORE_HEADER   *RuntimeHobCache;
> > > +  VARIABLE_STORE_HEADER   *RuntimeNvCache;
> > > +  VARIABLE_STORE_HEADER   *RuntimeVolatileCache;
> > > +}
> > >
> > SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > > +
> > > +typedef struct {
> > > +  UINTN                   TotalHobStorageSize;
> > > +  UINTN                   TotalNvStorageSize;
> > > +  UINTN                   TotalVolatileStorageSize;
> > > +  BOOLEAN                 AuthenticatedVariableUsage;
> > > +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO;
> > > +
> > >  #endif // _SMM_VARIABLE_COMMON_H_
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > index fb574b2e32..b9723c0250 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  ///
> > >  #define ISO_639_2_ENTRY_SIZE    3
> > >
> > > +///
> > > +/// The timeout to in 10us units to wait for the
> > > +/// variable runtime cache read lock to be acquired.
> > > +///
> > > +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT  200000
> > > +
> > >  typedef enum {
> > >    VariableStoreTypeVolatile,
> > >    VariableStoreTypeHob,
> > > @@ -64,6 +70,21 @@ typedef enum {
> > >    VariableStoreTypeMax
> > >  } VARIABLE_STORE_TYPE;
> > >
> > > +typedef struct {
> > > +  UINT32                  PendingUpdateOffset;
> > > +  UINT32                  PendingUpdateLength;
> > > +  VARIABLE_STORE_HEADER   *Store;
> > > +} VARIABLE_RUNTIME_CACHE;
> > > +
> > > +typedef struct {
> > > +  BOOLEAN                 *ReadLock;
> > > +  BOOLEAN                 *PendingUpdate;
> > > +  BOOLEAN                 *HobFlushComplete;
> > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeHobCache;
> > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeNvCache;
> > > +  VARIABLE_RUNTIME_CACHE  VariableRuntimeVolatileCache;
> > > +} VARIABLE_RUNTIME_CACHE_CONTEXT;
> > > +
> > >  typedef struct {
> > >    VARIABLE_HEADER *CurrPtr;
> > >    //
> > > @@ -79,14 +100,16 @@ typedef struct {
> > >  } VARIABLE_POINTER_TRACK;
> > >
> > >  typedef struct {
> > > -  EFI_PHYSICAL_ADDRESS  HobVariableBase;
> > > -  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;
> > > -  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;
> > > -  EFI_LOCK              VariableServicesLock;
> > > -  UINT32                ReentrantState;
> > > -  BOOLEAN               AuthFormat;
> > > -  BOOLEAN               AuthSupport;
> > > -  BOOLEAN               EmuNvMode;
> > > +  EFI_PHYSICAL_ADDRESS            HobVariableBase;
> > > +  EFI_PHYSICAL_ADDRESS            HobVariableBackupBase;
> >
> >
> > I do not see any usage of the new field "HobVariableBackupBase".
> > Could you help to double confirm?
> >
> >
> 
> You are correct. I removed usage of this variable before sending the
> patch series and the global variable here needs to be cleaned up.
> 
> > > +  EFI_PHYSICAL_ADDRESS            VolatileVariableBase;
> > > +  EFI_PHYSICAL_ADDRESS            NonVolatileVariableBase;
> > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> VariableRuntimeCacheContext;
> > > +  EFI_LOCK                        VariableServicesLock;
> > > +  UINT32                          ReentrantState;
> > > +  BOOLEAN                         AuthFormat;
> > > +  BOOLEAN                         AuthSupport;
> > > +  BOOLEAN                         EmuNvMode;
> > >  } VARIABLE_GLOBAL;
> > >
> > >  typedef struct {
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> > > h
> > > new file mode 100644
> > > index 0000000000..09b83eb215
> > > --- /dev/null
> > > +++
> > >
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.
> > > h
> > > @@ -0,0 +1,47 @@
> > > +/** @file
> > > +  The common variable volatile store routines shared by the
> > DXE_RUNTIME
> > > variable
> > > +  module and the DXE_SMM variable module.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#ifndef _VARIABLE_RUNTIME_CACHE_H_
> > > +#define _VARIABLE_RUNTIME_CACHE_H_
> > > +
> > > +#include "Variable.h"
> > > +
> > > +/**
> > > +  Copies any pending updates to runtime variable caches.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCacheEx (
> > > +  VOID
> > > +  );
> > > +
> > > +/**
> > > +  Synchronizes the runtime variable caches with all pending updates
> > outside
> > > runtime.
> > > +
> > > +  Ensures all conditions are met to maintain coherency for runtime cache
> > > updates.
> > > +
> > > +  @param[in] VariableRuntimeCache Variable runtime cache structure
> for
> > > the runtime cache being synchronized.
> > > +  @param[in] Offset               Offset in bytes to apply the update.
> > > +  @param[in] Length               Length of data in bytes of the update.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCache (
> > > +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> > > +  IN  UINTN                           Offset,
> > > +  IN  UINTN                           Length
> > > +  );
> > > +
> > > +#endif
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index 5da2354aa5..bb2fa3fc19 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #include "Variable.h"
> > >  #include "VariableNonVolatile.h"
> > >  #include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > >
> > >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> > >
> > > @@ -332,6 +333,12 @@ RecordVarErrorFlag (
> > >        // Update the data in NV cache.
> > >        //
> > >        *VarErrFlag = TempFlag;
> > > +      Status =  SynchronizeRuntimeVariableCache (
> > > +                  &mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCach
> e,
> > > +                  (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN)
> > > mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
> > > +                  sizeof (TempFlag)
> > > +                  );
> > > +      ASSERT_EFI_ERROR (Status);
> > >      }
> > >    }
> > >  }
> > > @@ -755,12 +762,24 @@ Reclaim (
> > >
> > >  Done:
> > >    if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode)
> {
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e,
> > > +                0,
> > > +                VariableStoreHeader->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >      FreePool (ValidBuffer);
> > >    } else {
> > >      //
> > >      // For NV variable reclaim, we use mNvVariableCache as the buffer, so
> > > copy the data back.
> > >      //
> > > -    CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase,
> > > VariableStoreHeader->Size);
> > > +    CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase,
> > > VariableStoreHeader->Size);
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCach
> e),
> > > +                0,
> > > +                VariableStoreHeader->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >    }
> > >
> > >    return Status;
> > > @@ -1592,6 +1611,7 @@ UpdateVariable (
> > >    VARIABLE_POINTER_TRACK              *Variable;
> > >    VARIABLE_POINTER_TRACK              NvVariable;
> > >    VARIABLE_STORE_HEADER               *VariableStoreHeader;
> > > +  VARIABLE_RUNTIME_CACHE              *VolatileCacheInstance;
> > >    UINT8                               *BufferForMerge;
> > >    UINTN                               MergedBufSize;
> > >    BOOLEAN                             DataReady;
> > > @@ -2235,6 +2255,21 @@ UpdateVariable (
> > >    }
> > >
> > >  Done:
> > > +  if (!EFI_ERROR (Status)) {
> > > +    if (Variable->Volatile) {
> > > +      VolatileCacheInstance = &(mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e);
> > > +    } else {
> > > +      VolatileCacheInstance = &(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCach
> e);
> > > +    }
> > > +
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                VolatileCacheInstance,
> > > +                0,
> > > +                VolatileCacheInstance->Store->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > > +  }
> > > +
> > >    return Status;
> > >  }
> > >
> > > @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash (
> > >          ErrorFlag = TRUE;
> > >        }
> > >      }
> > > +    Status =  SynchronizeRuntimeVariableCache (
> > > +                &mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache,
> > > +                0,
> > > +                mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .S
> > > tore->Size
> > > +                );
> > > +    ASSERT_EFI_ERROR (Status);
> > >      if (ErrorFlag) {
> > >        //
> > >        // We still have HOB variable(s) not flushed in flash.
> > > @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash (
> > >        // All HOB variables have been flushed in flash.
> > >        //
> > >        DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been
> > > flushed in flash.\n"));
> > > +      *(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.HobFlushComplete) =
> > TRUE;
> > >        if (!AtRuntime ()) {
> > >          FreePool ((VOID *) VariableStoreHeader);
> > >        }
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > > new file mode 100644
> > > index 0000000000..2642d9b000
> > > --- /dev/null
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c
> > > @@ -0,0 +1,153 @@
> > > +/** @file
> > > +  The common variable volatile store routines shared by the
> > DXE_RUNTIME
> > > variable
> > > +  module and the DXE_SMM variable module.
> > > +
> > > +  Caution: This module requires additional review when modified.
> > > +  This driver will have external input - variable data. They may be input in
> > > SMM mode.
> > > +  This external input must be validated carefully to avoid security issue
> like
> > > +  buffer overflow, integer overflow.
> > > +
> > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > > +
> > > +extern VARIABLE_MODULE_GLOBAL   *mVariableModuleGlobal;
> > > +extern VARIABLE_STORE_HEADER    *mNvVariableCache;
> > > +
> > > +/**
> > > +  Copies any pending updates to runtime variable caches.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCacheEx (
> >
> >
> > It is not clear to me why this function is named as the "Ex" version of
> function
> > SynchronizeRuntimeVariableCache(). For me, this function looks more like
> a
> > basic
> > version.
> >
> > I would suggest a name change for the functions provided in file
> > VariableRuntimeCache.c to better reflect their usage model.
> >
> >
> 
> I'll rename it in V3.
> 
> > > +  VOID
> > > +  )
> > > +{
> >
> >
> > I would recommend that at least a local variable should be introduced to
> > reduce
> > the duplications of:
> >
> > "mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext"
> >
> > in this function in order to make it easier to read.
> >
> >
> 
> I'll add it in V3.
> 
> > > +  if (
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> St
> > > ore == NULL ||
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.Store == NULL ||
> > > +    mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
> > > +    ) {
> > > +    return EFI_UNSUPPORTED;
> > > +  }
> > > +
> > > +  if (*(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate)) {
> > > +    if (
> > > +      mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .S
> > > tore != NULL &&
> > > +      mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0
> > > +      ) {
> > > +      CopyMem (
> > > +        (VOID *) (
> > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .S
> > > tore) +
> > > +          mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .P
> > > endingUpdateOffset
> > > +          ),
> > > +        (VOID *) (
> > > +          ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.HobVariableBase) +
> > > +          mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .P
> > > endingUpdateOffset
> > > +          ),
> > > +        mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .P
> > > endingUpdateLength
> > > +        );
> > > +      mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .P
> > > endingUpdateLength = 0;
> > > +      mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeHobCache
> .P
> > > endingUpdateOffset = 0;
> > > +    }
> > > +
> > > +    CopyMem (
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> St
> > > ore) +
> > > +        mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> Pe
> > > ndingUpdateOffset
> > > +        ),
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mNvVariableCache) +
> > > +        mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> Pe
> > > ndingUpdateOffset
> > > +        ),
> > > +      mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> Pe
> > > ndingUpdateLength
> > > +      );
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> Pe
> > > ndingUpdateLength = 0;
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeNvCache.
> Pe
> > > ndingUpdateOffset = 0;
> > > +
> > > +    CopyMem (
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.Store) +
> > > +        mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.PendingUpdateOffset
> > > +      ),
> > > +      (VOID *) (
> > > +        ((UINT8 *) (UINTN) mVariableModuleGlobal-
> > > >VariableGlobal.VolatileVariableBase) +
> > > +        mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.PendingUpdateOffset
> > > +        ),
> > > +      mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.PendingUpdateLength
> > > +      );
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.PendingUpdateLength = 0;
> > > +    mVariableModuleGlobal-
> > >
> > >VariableGlobal.VariableRuntimeCacheContext.VariableRuntimeVolatileCa
> ch
> > > e.PendingUpdateOffset = 0;
> > > +    *(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = FALSE;
> > > +  }
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Synchronizes the runtime variable caches with all pending updates
> > outside
> > > runtime.
> > > +
> > > +  Ensures all conditions are met to maintain coherency for runtime cache
> > > updates.
> > > +
> > > +  @param[in] VariableRuntimeCache Variable runtime cache structure
> for
> > > the runtime cache being synchronized.
> > > +  @param[in] Offset               Offset in bytes to apply the update.
> > > +  @param[in] Length               Length of data in bytes of the update.
> > > +
> > > +  @retval EFI_UNSUPPORTED         The volatile store to be updated is not
> > > initialized properly.
> > > +  @retval EFI_SUCCESS             The volatile store was updated successfully.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +SynchronizeRuntimeVariableCache (
> > > +  IN  VARIABLE_RUNTIME_CACHE          *VariableRuntimeCache,
> > > +  IN  UINTN                           Offset,
> > > +  IN  UINTN                           Length
> > > +  )
> > > +{
> > > +  if (VariableRuntimeCache == NULL) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  } else if (VariableRuntimeCache->Store == NULL) {
> > > +      // Runtime cache is not available yet at this point,
> > > +      // Return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET to let it
> > > progress
> > > +      return EFI_SUCCESS;
> > > +  }
> > > +
> > > +  if (
> > > +    mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate == NULL
> > ||
> > > +    mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.ReadLock == NULL
> > > +    ) {
> > > +    return EFI_UNSUPPORTED;
> > > +  }
> > > +
> > > +  if (
> > > +    *(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) &&
> > > +    VariableRuntimeCache->PendingUpdateLength > 0
> > > +    ) {
> > > +    VariableRuntimeCache->PendingUpdateLength =
> > > +      (UINT32) (
> > > +        MAX (
> > > +          (UINTN) (VariableRuntimeCache->PendingUpdateOffset +
> > > VariableRuntimeCache->PendingUpdateLength),
> > > +          Offset + Length
> > > +        ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset,
> > Offset)
> > > +      );
> > > +    VariableRuntimeCache->PendingUpdateOffset =
> > > +      (UINT32) MIN ((UINTN) VariableRuntimeCache-
> > >PendingUpdateOffset,
> > > Offset);
> > > +  } else {
> > > +    VariableRuntimeCache->PendingUpdateLength = (UINT32) Length;
> > > +    VariableRuntimeCache->PendingUpdateOffset = (UINT32) Offset;
> > > +  }
> > > +  *(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.PendingUpdate) = TRUE;
> > > +
> > > +  if (*(mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext.ReadLock) == FALSE) {
> > > +    return SynchronizeRuntimeVariableCacheEx ();
> > > +  }
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > diff --git
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > index ce409f22a3..8d767f75ac 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #include <Guid/SmmVariableCommon.h>
> > >  #include "Variable.h"
> > >  #include "VariableParsing.h"
> > > +#include "VariableRuntimeCache.h"
> > > +
> > > +extern VARIABLE_STORE_HEADER                         *mNvVariableCache;
> > >
> > >  BOOLEAN                                              mAtRuntime              = FALSE;
> > >  UINT8                                                *mVariableBufferPayload = NULL;
> > > @@ -451,25 +454,29 @@ SmmVariableGetStatistics (
> > >  EFI_STATUS
> > >  EFIAPI
> > >  SmmVariableHandler (
> > > -  IN     EFI_HANDLE                                DispatchHandle,
> > > -  IN     CONST VOID                                *RegisterContext,
> > > -  IN OUT VOID                                      *CommBuffer,
> > > -  IN OUT UINTN                                     *CommBufferSize
> > > +  IN     EFI_HANDLE                                       DispatchHandle,
> > > +  IN     CONST VOID                                       *RegisterContext,
> > > +  IN OUT VOID                                             *CommBuffer,
> > > +  IN OUT UINTN                                            *CommBufferSize
> > >    )
> > >  {
> > > -  EFI_STATUS                                       Status;
> > > -  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > -  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > *GetNextVariableName;
> > > -  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > *QueryVariableInfo;
> > > -  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > *GetPayloadSize;
> > > -  VARIABLE_INFO_ENTRY                              *VariableInfo;
> > > -  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > *VariableToLock;
> > > -  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > *CommVariableProperty;
> > > -  UINTN                                            InfoSize;
> > > -  UINTN                                            NameBufferSize;
> > > -  UINTN                                            CommBufferPayloadSize;
> > > -  UINTN                                            TempCommBufferSize;
> > > +  EFI_STATUS                                              Status;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
> > > *GetNextVariableName;
> > > +  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
> > > *QueryVariableInfo;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
> > > *GetPayloadSize;
> > > +
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *RuntimeVariableCacheContext;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > *GetRuntimeCacheInfo;
> > > +  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
> > > *VariableToLock;
> > > +  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
> > > *CommVariableProperty;
> > > +  VARIABLE_INFO_ENTRY                                     *VariableInfo;
> > > +  VARIABLE_RUNTIME_CACHE_CONTEXT
> > > *VariableCacheContext;
> > > +  VARIABLE_STORE_HEADER                                   *VariableCache;
> > > +  UINTN                                                   InfoSize;
> > > +  UINTN                                                   NameBufferSize;
> > > +  UINTN                                                   CommBufferPayloadSize;
> > > +  UINTN                                                   TempCommBufferSize;
> > >
> > >    //
> > >    // If input is invalid, stop processing this SMI
> > > @@ -789,6 +796,79 @@ SmmVariableHandler (
> > >                   );
> > >        CopyMem (SmmVariableFunctionHeader->Data,
> > mVariableBufferPayload,
> > > CommBufferPayloadSize);
> > >        break;
> > > +    case
> > >
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT:
> > > +      if (CommBufferPayloadSize < sizeof
> > >
> >
> (SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT)
> > )
> > > {
> >
> >
> > The above check is not correct, I think it should be:
> >
> > if (CommBufferPayloadSize < sizeof
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> > ) {
> >
> > Please help to double confirm.
> > Also, I recommend some security tests should be performed to these new
> > cases in
> > the variable SMI handler.
> >
> >
> 
> You're right. The wrong macro was simply copied.
> 
> > > +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM
> > > communication buffer size invalid!\n"));
> > > +      } else if (mEndOfDxe) {
> > > +        DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext:
> Cannot
> > > init context after end of DXE!\n"));
> > > +      } else {
> > > +        RuntimeVariableCacheContext =
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *) SmmVariableFunctionHeader->Data;
> >
> >
> > Not sure on this one:
> >
> > Do you think it is necessary to copy the contents in the comm buffer to the
> > pre-allocated SMM variable buffer payload 'mVariableBufferPayload' to
> > avoid
> > TOCTOU issue? Since there are some tests (sort of, a couple of ASSERTs)
> > based
> > on the comm buffer content.
> >
> >
> 
> I understand the TOCTOU observation. But is this still a concern with all the
> cores rendezvoused in SMM prior to end of DXE?


Hello Jian and Jiewen,

Could you help to provide your comments on this one? Thanks in advance.


> 
> > > +        VariableCacheContext = &mVariableModuleGlobal-
> > > >VariableGlobal.VariableRuntimeCacheContext;
> > > +
> > > +        ASSERT (RuntimeVariableCacheContext->RuntimeVolatileCache !=
> > > NULL);
> > > +        ASSERT (RuntimeVariableCacheContext->RuntimeNvCache != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext->PendingUpdate != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext->ReadLock != NULL);
> > > +        ASSERT (RuntimeVariableCacheContext->HobFlushComplete !=
> > NULL);
> > > +
> > > +        VariableCacheContext->VariableRuntimeHobCache.Store      =
> > > RuntimeVariableCacheContext->RuntimeHobCache;
> > > +        VariableCacheContext->VariableRuntimeVolatileCache.Store =
> > > RuntimeVariableCacheContext->RuntimeVolatileCache;
> > > +        VariableCacheContext->VariableRuntimeNvCache.Store       =
> > > RuntimeVariableCacheContext->RuntimeNvCache;
> > > +        VariableCacheContext->PendingUpdate                      =
> > > RuntimeVariableCacheContext->PendingUpdate;
> > > +        VariableCacheContext->ReadLock                           =
> > > RuntimeVariableCacheContext->ReadLock;
> > > +        VariableCacheContext->HobFlushComplete                   =
> > > RuntimeVariableCacheContext->HobFlushComplete;
> > > +
> > > +        // Set up the intial pending request since the RT cache needs to be
> in
> > > sync with SMM cache
> > > +        if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0)
> {
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateLength = 0;
> > > +        } else {
> > > +          VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> > > mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateOffset = 0;
> > > +          VariableCacheContext-
> > > >VariableRuntimeHobCache.PendingUpdateLength = (UINT32) ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > > +          CopyGuid (&(VariableCacheContext-
> > > >VariableRuntimeHobCache.Store->Signature), &(VariableCache-
> > > >Signature));
> > > +        }
> > > +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > > mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> > > +        VariableCacheContext-
> > > >VariableRuntimeVolatileCache.PendingUpdateOffset   = 0;
> > > +        VariableCacheContext-
> > > >VariableRuntimeVolatileCache.PendingUpdateLength   = (UINT32)
> > ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > > +        CopyGuid (&(VariableCacheContext-
> > > >VariableRuntimeVolatileCache.Store->Signature), &(VariableCache-
> > > >Signature));
> > > +
> > > +        VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > > mNvVariableCache;
> > > +        VariableCacheContext-
> > > >VariableRuntimeNvCache.PendingUpdateOffset = 0;
> > > +        VariableCacheContext-
> > > >VariableRuntimeNvCache.PendingUpdateLength = (UINT32) ((UINTN)
> > > GetEndPointer (VariableCache) - (UINTN) VariableCache);
> > > +        CopyGuid (&(VariableCacheContext-
> > >VariableRuntimeNvCache.Store-
> > > >Signature), &(VariableCache->Signature));
> > > +
> > > +        *(VariableCacheContext->PendingUpdate) = TRUE;
> > > +        *(VariableCacheContext->ReadLock) = FALSE;
> > > +        *(VariableCacheContext->HobFlushComplete) = FALSE;
> > > +      }
> > > +      Status = EFI_SUCCESS;
> > > +      break;
> > > +    case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE:
> > > +      Status = SynchronizeRuntimeVariableCacheEx ();
> > > +      break;
> > > +    case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO:
> > > +      if (CommBufferPayloadSize < sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO)) {
> > > +        DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM
> > communication
> > > buffer size invalid!\n"));
> > > +        return EFI_SUCCESS;
> > > +      }
> > > +      GetRuntimeCacheInfo =
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > SmmVariableFunctionHeader->Data;
> > > +
> > > +      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) {
> > > +        VariableCache = (VARIABLE_STORE_HEADER *) (UINTN)
> > > mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > > +        GetRuntimeCacheInfo->TotalHobStorageSize = VariableCache->Size;
> > > +      } else {
> > > +        GetRuntimeCacheInfo->TotalHobStorageSize = 0;
> > > +      }
> > > +
> > > +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > > mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> > > +      GetRuntimeCacheInfo->TotalVolatileStorageSize = VariableCache-
> > >Size;
> > > +      VariableCache = (VARIABLE_STORE_HEADER  *) (UINTN)
> > > mNvVariableCache;
> > > +      GetRuntimeCacheInfo->TotalNvStorageSize = (UINTN)
> VariableCache-
> > > >Size;
> > > +      GetRuntimeCacheInfo->AuthenticatedVariableUsage =
> > > mVariableModuleGlobal->VariableGlobal.AuthFormat;
> > > +
> > > +      Status = EFI_SUCCESS;
> > > +      break;
> > >
> > >      default:
> > >        Status = EFI_UNSUPPORTED;
> > > diff --git
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.c
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.c
> > > index 0a1888e5ef..46f69765a4 100644
> > > ---
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.c
> > > +++
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDx
> > > e.c
> > > @@ -13,7 +13,7 @@
> > >
> > >    InitCommunicateBuffer() is really function to check the variable data
> size.
> > >
> > > -Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
> > > +Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
> > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  **/
> > > @@ -32,13 +32,16 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >  #include <Library/UefiRuntimeLib.h>
> > >  #include <Library/BaseMemoryLib.h>
> > >  #include <Library/DebugLib.h>
> > > +#include <Library/TimerLib.h>
> > >  #include <Library/UefiLib.h>
> > >  #include <Library/BaseLib.h>
> > >
> > >  #include <Guid/EventGroup.h>
> > > +#include <Guid/PiSmmCommunicationRegionTable.h>
> > >  #include <Guid/SmmVariableCommon.h>
> > >
> > >  #include "PrivilegePolymorphic.h"
> > > +#include "VariableParsing.h"
> > >
> > >  EFI_HANDLE                       mHandle                    = NULL;
> > >  EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable               = NULL;
> > > @@ -46,8 +49,19 @@ EFI_EVENT
> mVirtualAddressChangeEvent
> > =
> > > NULL;
> > >  EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication
> =
> > > NULL;
> > >  UINT8                           *mVariableBuffer            = NULL;
> > >  UINT8                           *mVariableBufferPhysical    = NULL;
> > > +VARIABLE_INFO_ENTRY             *mVariableInfo              = NULL;
> > > +VARIABLE_STORE_HEADER           *mVariableRuntimeHobCacheBuffer
> > =
> > > NULL;
> > > +VARIABLE_STORE_HEADER           *mVariableRuntimeNvCacheBuffer
> > =
> > > NULL;
> > > +VARIABLE_STORE_HEADER
> *mVariableRuntimeVolatileCacheBuffer
> > > = NULL;
> > >  UINTN                            mVariableBufferSize;
> > > +UINTN                            mVariableRuntimeHobCacheBufferSize;
> > > +UINTN                            mVariableRuntimeNvCacheBufferSize;
> > > +UINTN                            mVariableRuntimeVolatileCacheBufferSize;
> > >  UINTN                            mVariableBufferPayloadSize;
> > > +BOOLEAN                          mVariableRuntimeCachePendingUpdate;
> > > +BOOLEAN                          mVariableRuntimeCacheReadLock;
> > > +BOOLEAN                          mVariableAuthFormat;
> > > +BOOLEAN                          mHobFlushComplete;
> > >  EFI_LOCK                         mVariableServicesLock;
> > >  EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;
> > >  EDKII_VAR_CHECK_PROTOCOL         mVarCheck;
> > > @@ -107,6 +121,73 @@ ReleaseLockOnlyAtBootTime (
> > >    }
> > >  }
> > >
> > > +/**
> > > +  Return TRUE if ExitBootServices () has been called.
> > > +
> > > +  @retval TRUE If ExitBootServices () has been called.
> > > +**/
> > > +BOOLEAN
> > > +AtRuntime (
> > > +  VOID
> > > +  )
> >
> >
> > I think we can either:
> > 1. Use EfiAtRuntime() for VariableSmmRuntimeDxe
> > 2. Move AtRuntime() to VariableParsing.c so that the function can be
> shared
> >    with VariableRuntimeDxe & VariableSmmRuntimeDxe. And then update
> > the
> >    EfiAtRuntime() usages to AtRuntime() for VariableSmmRuntimeDxe.
> >
> >
> 
> #1 will work fine.
> 
> > > +{
> > > +  return EfiAtRuntime ();
> > > +}
> > > +
> > > +/**
> > > +  Initialize the variable cache buffer as an empty variable store.
> > > +
> > > +  @param[out]     VariableCacheBuffer     A pointer to pointer of a cache
> > > variable store.
> > > +  @param[in,out]  TotalVariableCacheSize  On input, the minimum size
> > > needed for the UEFI variable store cache
> > > +                                          buffer that is allocated. On output, the actual size
> of
> > > the buffer allocated.
> > > +                                          If TotalVariableCacheSize is zero, a buffer will not
> be
> > > allocated and the
> > > +                                          function will return with EFI_SUCCESS.
> > > +
> > > +  @retval EFI_SUCCESS             The variable cache was allocated and
> > initialized
> > > successfully.
> > > +  @retval EFI_INVALID_PARAMETER   A given pointer is NULL or an
> invalid
> > > variable store size was specified.
> > > +  @retval EFI_OUT_OF_RESOURCES    Insufficient resources are available
> > to
> > > allocate the variable store cache buffer.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +InitVariableCache (
> > > +  OUT    VARIABLE_STORE_HEADER   **VariableCacheBuffer,
> > > +  IN OUT UINTN                   *TotalVariableCacheSize
> > > +  )
> > > +{
> > > +  VARIABLE_STORE_HEADER   *VariableCacheStorePtr;
> > > +
> > > +  if (TotalVariableCacheSize == NULL) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +  if (*TotalVariableCacheSize == 0) {
> > > +    return EFI_SUCCESS;
> > > +  }
> > > +  if (VariableCacheBuffer == NULL || *TotalVariableCacheSize < sizeof
> > > (VARIABLE_STORE_HEADER)) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +  *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize,
> > sizeof
> > > (UINT32));
> > > +
> > > +  //
> > > +  // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
> > > +  //
> > > +  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)
> > > AllocateRuntimePages (
> > > +                            EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)
> > > +                            );
> > > +  if (*VariableCacheBuffer == NULL) {
> > > +    return EFI_OUT_OF_RESOURCES;
> > > +  }
> > > +  VariableCacheStorePtr = *VariableCacheBuffer;
> > > +  SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize,
> > > (UINT32) 0xFFFFFFFF);
> > > +
> > > +  ZeroMem ((VOID *) VariableCacheStorePtr, sizeof
> > > (VARIABLE_STORE_HEADER));
> > > +  VariableCacheStorePtr->Size    = (UINT32) *TotalVariableCacheSize;
> > > +  VariableCacheStorePtr->Format  = VARIABLE_STORE_FORMATTED;
> > > +  VariableCacheStorePtr->State   = VARIABLE_STORE_HEALTHY;
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > +
> > >  /**
> > >    Initialize the communicate buffer using DataSize and Function.
> > >
> > > @@ -153,6 +234,69 @@ InitCommunicateBuffer (
> > >  }
> > >
> > >
> > > +/**
> > > +  Gets a SMM communicate buffer from the
> > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE installed as an entry
> in
> > > the UEFI
> > > +  system configuration table. A generic SMM communication buffer DXE
> > > driver may install the table or a custom table
> > > +  may be installed by a platform-specific driver.
> > > +
> > > +  The communicate size is:  SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
> > > +                            DataSize.
> > > +
> > > +  @param[in,out]   CommBufferSize   On input, the minimum size
> needed
> > > for the communication buffer.
> > > +                                    On output, the SMM buffer size available at
> > CommBuffer.
> > > +  @param[out]      CommBuffer       A pointer to an SMM communication
> > > buffer pointer.
> > > +
> > > +  @retval EFI_SUCCESS               The communication buffer was found
> > > successfully.
> > > +  @retval EFI_INVALID_PARAMETER     A given pointer is NULL or the
> > > CommBufferSize is zero.
> > > +  @retval EFI_NOT_FOUND             The
> > > EDKII_PI_SMM_COMMUNICATION_REGION_TABLE was not found.
> > > +  @retval EFI_OUT_OF_RESOURCES      A valid SMM communicate buffer
> > for
> > > the requested size is not available.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +GetCommunicateBuffer (
> > > +  IN OUT  UINTN     *CommBufferSize,
> > > +  OUT     UINT8     **CommBuffer
> > > +  )
> >
> >
> > Minor comment:
> >
> > I found that the consumers of the above function are:
> > GetRuntimeCacheInfo()
> > SendRuntimeVariableCacheContextToSmm()
> >
> > Both of them get called within SmmVariableReady() when the SMM
> variable
> > driver
> > finished initialization. I am wondering if they can simply use the pre-
> allocated
> > comm buffer (via InitCommunicateBuffer() and using 'mVariableBuffer'),
> > instead
> > of looking into the configuration table.
> >
> > In my opinion, this function can be dropped.
> >
> >
> 
> I did that initially. It was recommended to use this method.


After this patch, the driver will have 2 approaches for constructing the SMM
communication buffer:

A). Pre-allocate a buffer 'mVariableBuffer' as subsequent usage as SMM
    communication buffer via function calls InitCommunicateBuffer() and then
    SendCommunicateBuffer();
    
    This approach is used by functions like:
    VariableLockRequestToLock()
    VarCheckVariablePropertySet()
    VarCheckVariablePropertyGet()
    RuntimeServiceGetVariable()
    RuntimeServiceGetNextVariableName()
    RuntimeServiceSetVariable()
    RuntimeServiceQueryVariableInfo()
    OnExitBootServices()
    OnReadyToBoot()

B). Lookup in the system configuration table for memory ranges as SMM
    communication buffer.
    
    This approach is used by functions:
    GetRuntimeCacheInfo()
    SendRuntimeVariableCacheContextToSmm()


Could you help to confirm with the people who gave you the recommendation that
whether the driver should keep these 2 approaches or should align with 1
unified approach? Thanks.

Best Regards,
Hao Wu


> 
> > > +{
> > > +  EFI_STATUS                                Status;
> > > +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> > > *PiSmmCommunicationRegionTable;
> > > +  EFI_MEMORY_DESCRIPTOR                     *Entry;
> > > +  UINTN                                     EntrySize;
> > > +  UINT32                                    Index;
> > > +
> > > +  if (CommBuffer == NULL || CommBufferSize == NULL ||
> > > *CommBufferSize == 0) {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  Status = EfiGetSystemConfigurationTable (
> > > +             &gEdkiiPiSmmCommunicationRegionTableGuid,
> > > +             (VOID **) &PiSmmCommunicationRegionTable
> > > +             );
> > > +  if (EFI_ERROR (Status) || PiSmmCommunicationRegionTable == NULL) {
> > > +    return EFI_NOT_FOUND;
> > > +  }
> > > +
> > > +  Entry = (EFI_MEMORY_DESCRIPTOR *)
> > (PiSmmCommunicationRegionTable
> > > + 1);
> > > +  EntrySize = 0;
> > > +  for (Index = 0; Index < PiSmmCommunicationRegionTable-
> > > >NumberOfEntries; Index++) {
> > > +    if (Entry->Type == EfiConventionalMemory) {
> > > +      EntrySize = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
> > > +      if (EntrySize >= *CommBufferSize) {
> > > +        break;
> > > +      }
> > > +    }
> > > +    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry +
> > > PiSmmCommunicationRegionTable->DescriptorSize);
> > > +  }
> > > +
> > > +  if (Index < PiSmmCommunicationRegionTable->NumberOfEntries) {
> > > +    *CommBufferSize = EntrySize;
> > > +    *CommBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;
> > > +    return EFI_SUCCESS;
> > > +  }
> > > +
> > > +  return EFI_OUT_OF_RESOURCES;
> > > +}
> > > +
> > >  /**
> > >    Send the data in communicate buffer to SMM.
> > >
> > > @@ -424,6 +568,171 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > +/**
> > > +  Signals SMM to synchronize any pending variable updates with the
> > > runtime cache(s).
> > > +
> > > +**/
> > > +VOID
> > > +EFIAPI
> > > +SyncRuntimeCache (
> > > +  VOID
> > > +  )
> > > +{
> > > +  //
> > > +  // Init the communicate buffer. The buffer data size is:
> > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
> > > +  //
> > > +  InitCommunicateBuffer (NULL, 0,
> > > SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE);
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  SendCommunicateBuffer (0);
> > > +}
> > > +
> > > +/**
> > > +  Check whether a SMI must be triggered to retrieve pending cache
> > updates.
> > > +
> > > +  If the variable HOB was finished being flushed since the last check for a
> > > runtime cache update, this function
> > > +  will prevent the HOB cache from being used for future runtime cache
> > hits.
> > > +
> > > +**/
> > > +VOID
> > > +EFIAPI
> > > +CheckForRuntimeCacheSync (
> > > +  VOID
> > > +  )
> > > +{
> > > +  if (mVariableRuntimeCachePendingUpdate) {
> > > +    SyncRuntimeCache ();
> > > +  }
> > > +  ASSERT (!mVariableRuntimeCachePendingUpdate);
> > > +
> > > +  //
> > > +  // The HOB variable data may have finished being flushed in the
> runtime
> > > cache sync update
> > > +  //
> > > +  if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer !=
> NULL)
> > {
> > > +    if (!AtRuntime ()) {
> > > +      FreePool (mVariableRuntimeHobCacheBuffer);
> > > +    }
> > > +    mVariableRuntimeHobCacheBuffer = NULL;
> > > +  }
> > > +}
> > > +
> > > +/**
> > > +  This code finds variable in a volatile memory store.
> > > +
> > > +  Caution: This function may receive untrusted input.
> > > +  The data size is external input, so this function will validate it carefully
> to
> > > avoid buffer overflow.
> > > +
> > > +  @param[in]      VariableName       Name of Variable to be found.
> > > +  @param[in]      VendorGuid         Variable vendor GUID.
> > > +  @param[out]     Attributes         Attribute value of the variable found.
> > > +  @param[in, out] DataSize           Size of Data found. If size is less than
> the
> > > +                                     data, this value contains the required size.
> > > +  @param[out]     Data               Data pointer.
> > > +
> > > +  @retval EFI_SUCCESS                Found the specified variable.
> > > +  @retval EFI_INVALID_PARAMETER      Invalid parameter.
> > > +  @retval EFI_NOT_FOUND              The specified variable could not be
> > found.
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +FindVariableInRuntimeCache (
> > > +  IN      CHAR16                            *VariableName,
> > > +  IN      EFI_GUID                          *VendorGuid,
> > > +  OUT     UINT32                            *Attributes OPTIONAL,
> > > +  IN OUT  UINTN                             *DataSize,
> > > +  OUT     VOID                              *Data OPTIONAL
> > > +  )
> > > +{
> > > +  EFI_STATUS              Status;
> > > +  UINTN                   DelayIndex;
> > > +  UINTN                   TempDataSize;
> > > +  VARIABLE_POINTER_TRACK  RtPtrTrack;
> > > +  VARIABLE_STORE_TYPE     StoreType;
> > > +  VARIABLE_STORE_HEADER   *VariableStoreList[VariableStoreTypeMax];
> > > +
> > > +  Status = EFI_NOT_FOUND;
> > > +
> > > +  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL)
> {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  for (DelayIndex = 0; mVariableRuntimeCacheReadLock && DelayIndex
> <
> > > VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) {
> > > +    MicroSecondDelay (10);
> > > +  }
> > > +  if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) {
> > > +    ASSERT (!mVariableRuntimeCacheReadLock);
> > > +
> > > +    mVariableRuntimeCacheReadLock = TRUE;
> > > +    CheckForRuntimeCacheSync ();
> > > +
> > > +    if (!mVariableRuntimeCachePendingUpdate) {
> > > +      //
> > > +      // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > +      // The index and attributes mapping must be kept in this order as
> > > FindVariable
> > > +      // makes use of this mapping to implement search algorithm.
> > > +      //
> > > +      VariableStoreList[VariableStoreTypeVolatile] =
> > > mVariableRuntimeVolatileCacheBuffer;
> > > +      VariableStoreList[VariableStoreTypeHob]      =
> > > mVariableRuntimeHobCacheBuffer;
> > > +      VariableStoreList[VariableStoreTypeNv]       =
> > > mVariableRuntimeNvCacheBuffer;
> > > +
> > > +      for (StoreType = (VARIABLE_STORE_TYPE) 0; StoreType <
> > > VariableStoreTypeMax; StoreType++) {
> > > +        if (VariableStoreList[StoreType] == NULL) {
> > > +          continue;
> > > +        }
> > > +
> > > +        RtPtrTrack.StartPtr = GetStartPointer (VariableStoreList[StoreType]);
> > > +        RtPtrTrack.EndPtr   = GetEndPointer   (VariableStoreList[StoreType]);
> > > +        RtPtrTrack.Volatile = (BOOLEAN) (StoreType ==
> > > VariableStoreTypeVolatile);
> > > +
> > > +        Status = FindVariableEx (VariableName, VendorGuid, FALSE,
> > > &RtPtrTrack);
> > > +        if (!EFI_ERROR (Status)) {
> > > +          break;
> > > +        }
> > > +      }
> > > +
> > > +      if (!EFI_ERROR (Status)) {
> > > +        //
> > > +        // Get data size
> > > +        //
> > > +        TempDataSize = DataSizeOfVariable (RtPtrTrack.CurrPtr);
> > > +        ASSERT (TempDataSize != 0);
> > > +
> > > +        if (*DataSize >= TempDataSize) {
> > > +          if (Data == NULL) {
> > > +            Status = EFI_INVALID_PARAMETER;
> > > +            goto Done;
> > > +          }
> > > +
> > > +          CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr),
> > > TempDataSize);
> > > +          if (Attributes != NULL) {
> > > +            *Attributes = RtPtrTrack.CurrPtr->Attributes;
> > > +          }
> > > +
> > > +          *DataSize = TempDataSize;
> > > +
> > > +          UpdateVariableInfo (VariableName, VendorGuid,
> > RtPtrTrack.Volatile,
> > > TRUE, FALSE, FALSE, TRUE, &mVariableInfo);
> > > +
> > > +          Status = EFI_SUCCESS;
> > > +          goto Done;
> > > +        } else {
> > > +          *DataSize = TempDataSize;
> > > +          Status = EFI_BUFFER_TOO_SMALL;
> > > +          goto Done;
> > > +        }
> > > +      }
> > > +    }
> > > +  }
> > > +
> > > +Done:
> > > +  mVariableRuntimeCacheReadLock = FALSE;
> >
> >
> > If timeout occurs when acquiring the read lock, should this flag be set to
> > FALSE
> > in such case?
> >
> 
> Please see reply to patch #8.
> 
> > Best Regards,
> > Hao Wu
> >
> >
> > > +
> > > +  return Status;
> > > +}
> > > +
> > >  /**
> > >    This code finds variable in storage blocks (Volatile or Non-Volatile).
> > >
> > > @@ -454,91 +763,21 @@ RuntimeServiceGetVariable (
> > >    )
> > >  {
> > >    EFI_STATUS                                Status;
> > > -  UINTN                                     PayloadSize;
> > > -  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
> > > *SmmVariableHeader;
> > > -  UINTN                                     TempDataSize;
> > > -  UINTN                                     VariableNameSize;
> > >
> > >    if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL)
> {
> > >      return EFI_INVALID_PARAMETER;
> > >    }
> > > -
> > > -  TempDataSize          = *DataSize;
> > > -  VariableNameSize      = StrSize (VariableName);
> > > -  SmmVariableHeader     = NULL;
> > > -
> > > -  //
> > > -  // If VariableName exceeds SMM payload limit. Return failure
> > > -  //
> > > -  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {
> > > -    return EFI_INVALID_PARAMETER;
> > > -  }
> > > -
> > > -  AcquireLockOnlyAtBootTime(&mVariableServicesLock);
> > > -
> > > -  //
> > > -  // Init the communicate buffer. The buffer data size is:
> > > -  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
> > > -  //
> > > -  if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > VariableNameSize) {
> > > -    //
> > > -    // If output data buffer exceed SMM payload limit. Trim output buffer
> to
> > > SMM payload size
> > > -    //
> > > -    TempDataSize = mVariableBufferPayloadSize - OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) -
> > > VariableNameSize;
> > > -  }
> > > -  PayloadSize = OFFSET_OF
> > > (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) +
> > > VariableNameSize + TempDataSize;
> > > -
> > > -  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader,
> > > PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);
> > > -  if (EFI_ERROR (Status)) {
> > > -    goto Done;
> > > -  }
> > > -  ASSERT (SmmVariableHeader != NULL);
> > > -
> > > -  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);
> > > -  SmmVariableHeader->DataSize   = TempDataSize;
> > > -  SmmVariableHeader->NameSize   = VariableNameSize;
> > > -  if (Attributes == NULL) {
> > > -    SmmVariableHeader->Attributes = 0;
> > > -  } else {
> > > -    SmmVariableHeader->Attributes = *Attributes;
> > > -  }
> > > -  CopyMem (SmmVariableHeader->Name, VariableName,
> > > SmmVariableHeader->NameSize);
> > > -
> > > -  //
> > > -  // Send data to SMM.
> > > -  //
> > > -  Status = SendCommunicateBuffer (PayloadSize);
> > > -
> > > -  //
> > > -  // Get data from SMM.
> > > -  //
> > > -  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
> > > -    //
> > > -    // SMM CommBuffer DataSize can be a trimed value
> > > -    // Only update DataSize when needed
> > > -    //
> > > -    *DataSize = SmmVariableHeader->DataSize;
> > > -  }
> > > -  if (Attributes != NULL) {
> > > -    *Attributes = SmmVariableHeader->Attributes;
> > > -  }
> > > -
> > > -  if (EFI_ERROR (Status)) {
> > > -    goto Done;
> > > -  }
> > > -
> > > -  if (Data != NULL) {
> > > -    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name +
> > > SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);
> > > -  } else {
> > > -    Status = EFI_INVALID_PARAMETER;
> > > +  if (VariableName[0] == 0) {
> > > +    return EFI_NOT_FOUND;
> > >    }
> > >
> > > -Done:
> > > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > > +  Status =  FindVariableInRuntimeCache (VariableName, VendorGuid,
> > > Attributes, DataSize, Data);
> > >    ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > > +
> > >    return Status;
> > >  }
> > >
> > > -
> > >  /**
> > >    This code Finds the Next available variable.
> > >
> > > @@ -870,6 +1109,17 @@ OnReadyToBoot (
> > >    //
> > >    SendCommunicateBuffer (0);
> > >
> > > +  //
> > > +  // Install the system configuration table for variable info data captured
> > > +  //
> > > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > +    if (mVariableAuthFormat) {
> > > +      gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid,
> > > mVariableInfo);
> > > +    } else {
> > > +      gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo);
> > > +    }
> > > +  }
> > > +
> > >    gBS->CloseEvent (Event);
> > >  }
> > >
> > > @@ -893,6 +1143,9 @@ VariableAddressChangeEvent (
> > >  {
> > >    EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);
> > >    EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);
> > > +  EfiConvertPointer (0x0, (VOID **)
> &mVariableRuntimeHobCacheBuffer);
> > > +  EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeNvCacheBuffer);
> > > +  EfiConvertPointer (0x0, (VOID **)
> > > &mVariableRuntimeVolatileCacheBuffer);
> > >  }
> > >
> > >  /**
> > > @@ -969,6 +1222,173 @@ Done:
> > >    return Status;
> > >  }
> > >
> > > +/**
> > > +  This code gets information needed from SMM for runtime cache
> > > initialization.
> > > +
> > > +  @param[out] TotalHobStorageSize         Output pointer for the total
> HOB
> > > storage size in bytes.
> > > +  @param[out] TotalNvStorageSize          Output pointer for the total non-
> > > volatile storage size in bytes.
> > > +  @param[out] TotalVolatileStorageSize    Output pointer for the total
> > > volatile storage size in bytes.
> > > +  @param[out] AuthenticatedVariableUsage  Output pointer that
> indicates
> > if
> > > authenticated variables are to be used.
> > > +
> > > +  @retval EFI_SUCCESS                     Retrieved the size successfully.
> > > +  @retval EFI_INVALID_PARAMETER           TotalNvStorageSize parameter
> is
> > > NULL.
> > > +  @retval EFI_OUT_OF_RESOURCES            Could not allocate a
> > CommBuffer.
> > > +  @retval Others                          Could not retrieve the size successfully.;
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +GetRuntimeCacheInfo (
> > > +  OUT UINTN                         *TotalHobStorageSize,
> > > +  OUT UINTN                         *TotalNvStorageSize,
> > > +  OUT UINTN                         *TotalVolatileStorageSize,
> > > +  OUT BOOLEAN                       *AuthenticatedVariableUsage
> > > +  )
> > > +{
> > > +  EFI_STATUS                                          Status;
> > > +  SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
> > > *SmmGetRuntimeCacheInfo;
> > > +  EFI_SMM_COMMUNICATE_HEADER
> > > *SmmCommunicateHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  UINTN                                               CommSize;
> > > +  UINTN                                               CommBufferSize;
> > > +  UINT8                                               *CommBuffer;
> > > +
> > > +  SmmGetRuntimeCacheInfo = NULL;
> > > +  CommBuffer = NULL;
> > > +
> > > +  if (TotalHobStorageSize == NULL || TotalNvStorageSize == NULL ||
> > > TotalVolatileStorageSize == NULL || AuthenticatedVariableUsage ==
> NULL)
> > {
> > > +    return EFI_INVALID_PARAMETER;
> > > +  }
> > > +
> > > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > > +
> > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > +  CommBufferSize = CommSize;
> > > +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +  if (CommBuffer == NULL) {
> > > +    Status = EFI_OUT_OF_RESOURCES;
> > > +    goto Done;
> > > +  }
> > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > +
> > > +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> > > CommBuffer;
> > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > &gEfiSmmVariableProtocolGuid);
> > > +  SmmCommunicateHeader->MessageLength =
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO);
> > > +
> > > +  SmmVariableFunctionHeader =
> > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> SmmCommunicateHeader-
> > > >Data;
> > > +  SmmVariableFunctionHeader->Function =
> > > SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO;
> > > +  SmmGetRuntimeCacheInfo =
> > > (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *)
> > > SmmVariableFunctionHeader->Data;
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  Status = mSmmCommunication->Communicate
> (mSmmCommunication,
> > > CommBuffer, &CommSize);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > +    goto Done;
> > > +  }
> > > +
> > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +
> > > +  //
> > > +  // Get data from SMM.
> > > +  //
> > > +  *TotalHobStorageSize = SmmGetRuntimeCacheInfo-
> > >TotalHobStorageSize;
> > > +  *TotalNvStorageSize = SmmGetRuntimeCacheInfo-
> >TotalNvStorageSize;
> > > +  *TotalVolatileStorageSize = SmmGetRuntimeCacheInfo-
> > > >TotalVolatileStorageSize;
> > > +  *AuthenticatedVariableUsage = SmmGetRuntimeCacheInfo-
> > > >AuthenticatedVariableUsage;
> > > +
> > > +Done:
> > > +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > > +  return Status;
> > > +}
> > > +
> > > +/**
> > > +  Sends the runtime variable cache context information to SMM.
> > > +
> > > +  @retval EFI_SUCCESS               Retrieved the size successfully.
> > > +  @retval EFI_INVALID_PARAMETER     TotalNvStorageSize parameter is
> > > NULL.
> > > +  @retval EFI_OUT_OF_RESOURCES      Could not allocate a CommBuffer.
> > > +  @retval Others                    Could not retrieve the size successfully.;
> > > +
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +SendRuntimeVariableCacheContextToSmm (
> > > +  VOID
> > > +  )
> > > +{
> > > +  EFI_STATUS                                                Status;
> > > +
> > >
> SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *SmmRuntimeVarCacheContext;
> > > +  EFI_SMM_COMMUNICATE_HEADER
> > > *SmmCommunicateHeader;
> > > +  SMM_VARIABLE_COMMUNICATE_HEADER
> > > *SmmVariableFunctionHeader;
> > > +  UINTN                                                     CommSize;
> > > +  UINTN                                                     CommBufferSize;
> > > +  UINT8                                                     *CommBuffer;
> > > +
> > > +  SmmRuntimeVarCacheContext = NULL;
> > > +  CommBuffer = NULL;
> > > +
> > > +  AcquireLockOnlyAtBootTime (&mVariableServicesLock);
> > > +
> > > +  //
> > > +  // Init the communicate buffer. The buffer data size is:
> > > +  // SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> > ;
> > > +  //
> > > +  CommSize = SMM_COMMUNICATE_HEADER_SIZE +
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> > ;
> > > +  CommBufferSize = CommSize;
> > > +  Status = GetCommunicateBuffer (&CommBufferSize, &CommBuffer);
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +  if (CommBuffer == NULL) {
> > > +    Status = EFI_OUT_OF_RESOURCES;
> > > +    goto Done;
> > > +  }
> > > +  ZeroMem (CommBuffer, CommBufferSize);
> > > +
> > > +  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)
> > > CommBuffer;
> > > +  CopyGuid (&SmmCommunicateHeader->HeaderGuid,
> > > &gEfiSmmVariableProtocolGuid);
> > > +  SmmCommunicateHeader->MessageLength =
> > > SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT)
> > ;
> > > +
> > > +  SmmVariableFunctionHeader =
> > > (SMM_VARIABLE_COMMUNICATE_HEADER *)
> SmmCommunicateHeader-
> > > >Data;
> > > +  SmmVariableFunctionHeader->Function =
> > >
> > SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT;
> > > +  SmmRuntimeVarCacheContext =
> > >
> >
> (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
> > > *) SmmVariableFunctionHeader->Data;
> > > +
> > > +  SmmRuntimeVarCacheContext->RuntimeHobCache =
> > > mVariableRuntimeHobCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->RuntimeVolatileCache =
> > > mVariableRuntimeVolatileCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->RuntimeNvCache =
> > > mVariableRuntimeNvCacheBuffer;
> > > +  SmmRuntimeVarCacheContext->PendingUpdate =
> > > &mVariableRuntimeCachePendingUpdate;
> > > +  SmmRuntimeVarCacheContext->ReadLock =
> > > &mVariableRuntimeCacheReadLock;
> > > +  SmmRuntimeVarCacheContext->HobFlushComplete =
> > > &mHobFlushComplete;
> > > +
> > > +  //
> > > +  // Send data to SMM.
> > > +  //
> > > +  Status = mSmmCommunication->Communicate
> (mSmmCommunication,
> > > CommBuffer, &CommSize);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +  if (CommSize <= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {
> > > +    Status = EFI_BAD_BUFFER_SIZE;
> > > +    goto Done;
> > > +  }
> > > +
> > > +  Status = SmmVariableFunctionHeader->ReturnStatus;
> > > +  if (EFI_ERROR (Status)) {
> > > +    goto Done;
> > > +  }
> > > +
> > > +Done:
> > > +  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);
> > > +  return Status;
> > > +}
> > > +
> > >  /**
> > >    Initialize variable service and install Variable Architectural protocol.
> > >
> > > @@ -985,7 +1405,7 @@ SmmVariableReady (
> > >  {
> > >    EFI_STATUS                                Status;
> > >
> > > -  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> > > (VOID **)&mSmmVariable);
> > > +  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,
> > > (VOID **) &mSmmVariable);
> > >    if (EFI_ERROR (Status)) {
> > >      return;
> > >    }
> > > @@ -1007,6 +1427,40 @@ SmmVariableReady (
> > >    //
> > >    mVariableBufferPhysical = mVariableBuffer;
> > >
> > > +  //
> > > +  // Allocate runtime variable cache memory buffers.
> > > +  //
> > > +  Status =  GetRuntimeCacheInfo (
> > > +              &mVariableRuntimeHobCacheBufferSize,
> > > +              &mVariableRuntimeNvCacheBufferSize,
> > > +              &mVariableRuntimeVolatileCacheBufferSize,
> > > +              &mVariableAuthFormat
> > > +              );
> > > +  if (!EFI_ERROR (Status)) {
> > > +    Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer,
> > > &mVariableRuntimeHobCacheBufferSize);
> > > +    if (!EFI_ERROR (Status)) {
> > > +      Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer,
> > > &mVariableRuntimeNvCacheBufferSize);
> > > +      if (!EFI_ERROR (Status)) {
> > > +        Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer,
> > > &mVariableRuntimeVolatileCacheBufferSize);
> > > +        if (!EFI_ERROR (Status)) {
> > > +          Status = InitVariableParsing (mVariableAuthFormat);
> > > +          ASSERT_EFI_ERROR (Status);
> > > +
> > > +          Status = SendRuntimeVariableCacheContextToSmm ();
> > > +          if (!EFI_ERROR (Status)) {
> > > +            SyncRuntimeCache ();
> > > +          }
> > > +        }
> > > +      }
> > > +    }
> > > +    if (EFI_ERROR (Status)) {
> > > +      mVariableRuntimeHobCacheBuffer = NULL;
> > > +      mVariableRuntimeNvCacheBuffer = NULL;
> > > +      mVariableRuntimeVolatileCacheBuffer = NULL;
> > > +    }
> > > +  }
> > > +  ASSERT_EFI_ERROR (Status);
> > > +
> > >    gRT->GetVariable         = RuntimeServiceGetVariable;
> > >    gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;
> > >    gRT->SetVariable         = RuntimeServiceSetVariable;
> > > --
> > > 2.16.2.windows.1
> >
> 


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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
  2019-10-03  8:03   ` Wu, Hao A
@ 2019-10-08  6:07   ` Wang, Jian J
  2019-10-08 22:00     ` Kubacki, Michael A
  1 sibling, 1 reply; 45+ messages in thread
From: Wang, Jian J @ 2019-10-08  6:07 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wu, Hao A, Yao, Jiewen

Michael,

One comment below.

> -----Original Message-----
> From: Kubacki, Michael A <michael.a.kubacki@intel.com>
> Sent: Saturday, September 28, 2019 9:47 AM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>; Yao, Jiewen
> <jiewen.yao@intel.com>
> Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing
> functions
> 
> This change moves the following functions into a dedicated file
> so they may be used in other variable files as needed. Furthermore,
> it reduces the overall size of the common Variable.c file.
> 
>  * DataSizeOfVariable ()
>  * FindVariableEx ()
>  * GetEndPointer ()
>  * GetNextVariablePtr ()
>  * GetStartPointer ()
>  * GetVariableDataOffset ()
>  * GetVariableDataPtr ()
>  * GetVariableHeaderSize ()
>  * GetVariableNamePtr ()
>  * GetVariableStoreStatus ()
>  * GetVendorGuidPtr ()
>  * IsValidVariableHeader ()
>  * NameSizeOfVariable ()
>  * SetDataSizeOfVariable ()
>  * SetNameSizeOfVariable ()
>  * UpdateVariableInfo ()
>  * VariableCompareTimeStampInternal ()
>  * VariableServiceGetNextVariableInternal ()
> 
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> ---
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf   |   2
> +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |   2 +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf |
> 7 +
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119 ----
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        | 306
> ++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726 +------
> ------------
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |   3 +-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        | 731
> ++++++++++++++++++++
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |   1 +
>  9 files changed, 1052 insertions(+), 845 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> index 641376c9c5..c35e5fe787 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> @@ -36,6 +36,8 @@
>    Variable.c
>    VariableDxe.c
>    Variable.h
> +  VariableParsing.c
> +  VariableParsing.h
>    PrivilegePolymorphic.h
>    Measurement.c
>    TcgMorLockDxe.c
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> index 0a160d269d..626738b9c7 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableTraditionalMm.c
>    VariableSmm.c
> +  VariableParsing.c
> +  VariableParsing.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> index 21bc81163b..1ba8f9ebfb 100644
> ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> @@ -45,6 +45,8 @@
>    Variable.c
>    VariableSmm.c
>    VariableStandaloneMm.c
> +  VariableParsing.c
> +  VariableParsing.h
>    VarCheck.c
>    Variable.h
>    PrivilegePolymorphic.h
> @@ -99,6 +101,11 @@
>    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
>    gEfiGlobalVariableGuid
> 
> +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> +  gEfiImageSecurityDatabaseGuid

Why add the guid here? It's only consumed by Measurement.c, which is not included
in this inf file.

> +
>    gEfiMemoryOverwriteControlDataGuid            ## SOMETIMES_CONSUMES   ##
> Variable:L"MemoryOverwriteRequestControl"
>    gEfiMemoryOverwriteRequestControlLockGuid     ## SOMETIMES_PRODUCES
> ## Variable:L"MemoryOverwriteRequestControlLock"
> 
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> index 9eac43759f..fb574b2e32 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> @@ -179,89 +179,6 @@ FindVariable (
>    IN  BOOLEAN                 IgnoreRtCheck
>    );
> 
> -/**
> -
> -  Gets the pointer to the end of the variable storage area.
> -
> -  This function gets pointer to the end of the variable storage
> -  area, according to the input variable store header.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the end of the variable storage area.
> -
> -**/
> -VARIABLE_HEADER *
> -GetEndPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  );
> -
> -/**
> -  This code gets the size of variable header.
> -
> -  @return Size of variable header in bytes in type UINTN.
> -
> -**/
> -UINTN
> -GetVariableHeaderSize (
> -  VOID
> -  );
> -
> -/**
> -
> -  This code gets the pointer to the variable name.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Name which is Unicode encoding.
> -
> -**/
> -CHAR16 *
> -GetVariableNamePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
> -/**
> -  This code gets the pointer to the variable guid.
> -
> -  @param Variable   Pointer to the Variable Header.
> -
> -  @return A EFI_GUID* pointer to Vendor Guid.
> -
> -**/
> -EFI_GUID *
> -GetVendorGuidPtr (
> -  IN VARIABLE_HEADER    *Variable
> -  );
> -
> -/**
> -
> -  This code gets the pointer to the variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Data.
> -
> -**/
> -UINT8 *
> -GetVariableDataPtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
> -/**
> -
> -  This code gets the size of variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Size of variable in bytes.
> -
> -**/
> -UINTN
> -DataSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  );
> -
>  /**
>    This function is to check if the remaining variable space is enough to set
>    all Variables from argument list successfully. The purpose of the check
> @@ -450,17 +367,6 @@ ReclaimForOS(
>    VOID
>    );
> 
> -/**
> -  Get non-volatile maximum variable size.
> -
> -  @return Non-volatile maximum variable size.
> -
> -**/
> -UINTN
> -GetNonVolatileMaxVariableSize (
> -  VOID
> -  );
> -
>  /**
>    Get maximum variable size, covering both non-volatile and volatile variables.
> 
> @@ -546,31 +452,6 @@ VariableServiceGetVariable (
>    OUT     VOID              *Data OPTIONAL
>    );
> 
> -/**
> -  This code Finds the Next available variable.
> -
> -  Caution: This function may receive untrusted input.
> -  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> -
> -  @param[in] VariableName   Pointer to variable name.
> -  @param[in] VendorGuid     Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> -
> -  @retval EFI_SUCCESS           The function completed successfully.
> -  @retval EFI_NOT_FOUND         The next variable was not found.
> -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> -                                GUID of an existing variable.
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -VariableServiceGetNextVariableInternal (
> -  IN  CHAR16                *VariableName,
> -  IN  EFI_GUID              *VendorGuid,
> -  OUT VARIABLE_HEADER       **VariablePtr
> -  );
> -
>  /**
> 
>    This code Finds the Next available variable.
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> new file mode 100644
> index 0000000000..9d77c4916c
> --- /dev/null
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> @@ -0,0 +1,306 @@
> +/** @file
> +  Functions in this module are associated with variable parsing operations and
> +  are intended to be usable across variable driver source files.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _VARIABLE_PARSING_H_
> +#define _VARIABLE_PARSING_H_
> +
> +#include <Guid/ImageAuthentication.h>
> +#include "Variable.h"
> +
> +/**
> +
> +  This code checks if variable header is valid or not.
> +
> +  @param Variable           Pointer to the Variable Header.
> +  @param VariableStoreEnd   Pointer to the Variable Store End.
> +
> +  @retval TRUE              Variable header is valid.
> +  @retval FALSE             Variable header is not valid.
> +
> +**/
> +BOOLEAN
> +IsValidVariableHeader (
> +  IN  VARIABLE_HEADER       *Variable,
> +  IN  VARIABLE_HEADER       *VariableStoreEnd
> +  );
> +
> +/**
> +
> +  This code gets the current status of Variable Store.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @retval EfiRaw         Variable store status is raw.
> +  @retval EfiValid       Variable store status is valid.
> +  @retval EfiInvalid     Variable store status is invalid.
> +
> +**/
> +VARIABLE_STORE_STATUS
> +GetVariableStoreStatus (
> +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> +  );
> +
> +/**
> +  This code gets the size of variable header.
> +
> +  @return Size of variable header in bytes in type UINTN.
> +
> +**/
> +UINTN
> +GetVariableHeaderSize (
> +  VOID
> +  );
> +
> +/**
> +
> +  This code gets the size of name of variable.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return UINTN          Size of variable in bytes.
> +
> +**/
> +UINTN
> +NameSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code sets the size of name of variable.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] NameSize   Name size to set.
> +
> +**/
> +VOID
> +SetNameSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              NameSize
> +  );
> +
> +/**
> +
> +  This code gets the size of variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Size of variable in bytes.
> +
> +**/
> +UINTN
> +DataSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code sets the size of variable data.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] DataSize   Data size to set.
> +
> +**/
> +VOID
> +SetDataSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              DataSize
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the variable name.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Name which is Unicode encoding.
> +
> +**/
> +CHAR16 *
> +GetVariableNamePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code gets the pointer to the variable guid.
> +
> +  @param Variable   Pointer to the Variable Header.
> +
> +  @return A EFI_GUID* pointer to Vendor Guid.
> +
> +**/
> +EFI_GUID *
> +GetVendorGuidPtr (
> +  IN VARIABLE_HEADER    *Variable
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Data.
> +
> +**/
> +UINT8 *
> +GetVariableDataPtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +  This code gets the variable data offset related to variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Variable Data offset.
> +
> +**/
> +UINTN
> +GetVariableDataOffset (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +
> +  This code gets the pointer to the next variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to next variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetNextVariablePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  );
> +
> +/**
> +
> +  Gets the pointer to the first variable header in given variable store area.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the first variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetStartPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  );
> +
> +/**
> +
> +  Gets the pointer to the end of the variable storage area.
> +
> +  This function gets pointer to the end of the variable storage
> +  area, according to the input variable store header.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the end of the variable storage area.
> +
> +**/
> +VARIABLE_HEADER *
> +GetEndPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  );
> +
> +/**
> +  Compare two EFI_TIME data.
> +
> +
> +  @param FirstTime           A pointer to the first EFI_TIME data.
> +  @param SecondTime          A pointer to the second EFI_TIME data.
> +
> +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> +  @retval  FALSE             The FirstTime is later than the SecondTime.
> +
> +**/
> +BOOLEAN
> +VariableCompareTimeStampInternal (
> +  IN EFI_TIME               *FirstTime,
> +  IN EFI_TIME               *SecondTime
> +  );
> +
> +/**
> +  Find the variable in the specified variable store.
> +
> +  @param[in]       VariableName        Name of the variable to be found
> +  @param[in]       VendorGuid          Vendor GUID to be found.
> +  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> +                                       check at runtime when searching variable.
> +  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains
> Variable Information.
> +
> +  @retval          EFI_SUCCESS         Variable found successfully
> +  @retval          EFI_NOT_FOUND       Variable not found
> +**/
> +EFI_STATUS
> +FindVariableEx (
> +  IN     CHAR16                  *VariableName,
> +  IN     EFI_GUID                *VendorGuid,
> +  IN     BOOLEAN                 IgnoreRtCheck,
> +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> +  );
> +
> +/**
> +  This code Finds the Next available variable.
> +
> +  Caution: This function may receive untrusted input.
> +  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> +
> +  @param[in]  VariableName  Pointer to variable name.
> +  @param[in]  VendorGuid    Variable Vendor Guid.
> +  @param[out] VariablePtr   Pointer to variable header address.
> +
> +  @retval EFI_SUCCESS           The function completed successfully.
> +  @retval EFI_NOT_FOUND         The next variable was not found.
> +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> +  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> +                                GUID of an existing variable.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +VariableServiceGetNextVariableInternal (
> +  IN  CHAR16                *VariableName,
> +  IN  EFI_GUID              *VendorGuid,
> +  OUT VARIABLE_HEADER       **VariablePtr
> +  );
> +
> +/**
> +  Routine used to track statistical information about variable usage.
> +  The data is stored in the EFI system table so it can be accessed later.
> +  VariableInfo.efi can dump out the table. Only Boot Services variable
> +  accesses are tracked by this code. The PcdVariableCollectStatistics
> +  build flag controls if this feature is enabled.
> +
> +  A read that hits in the cache will have Read and Cache true for
> +  the transaction. Data is allocated by this routine, but never
> +  freed.
> +
> +  @param[in] VariableName   Name of the Variable to track.
> +  @param[in] VendorGuid     Guid of the Variable to track.
> +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in] Read           TRUE if GetVariable() was called.
> +  @param[in] Write          TRUE if SetVariable() was called.
> +  @param[in] Delete         TRUE if deleted via SetVariable().
> +  @param[in] Cache          TRUE for a cache hit.
> +
> +**/
> +VOID
> +UpdateVariableInfo (
> +  IN  CHAR16                  *VariableName,
> +  IN  EFI_GUID                *VendorGuid,
> +  IN  BOOLEAN                 Volatile,
> +  IN  BOOLEAN                 Read,
> +  IN  BOOLEAN                 Write,
> +  IN  BOOLEAN                 Delete,
> +  IN  BOOLEAN                 Cache
> +  );
> +
> +#endif
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> index f32c9c2808..76536308e6 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  **/
> 
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> 
> @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
> 
>  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> 
> -/**
> -  Routine used to track statistical information about variable usage.
> -  The data is stored in the EFI system table so it can be accessed later.
> -  VariableInfo.efi can dump out the table. Only Boot Services variable
> -  accesses are tracked by this code. The PcdVariableCollectStatistics
> -  build flag controls if this feature is enabled.
> -
> -  A read that hits in the cache will have Read and Cache true for
> -  the transaction. Data is allocated by this routine, but never
> -  freed.
> -
> -  @param[in] VariableName   Name of the Variable to track.
> -  @param[in] VendorGuid     Guid of the Variable to track.
> -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> -  @param[in] Read           TRUE if GetVariable() was called.
> -  @param[in] Write          TRUE if SetVariable() was called.
> -  @param[in] Delete         TRUE if deleted via SetVariable().
> -  @param[in] Cache          TRUE for a cache hit.
> -
> -**/
> -VOID
> -UpdateVariableInfo (
> -  IN  CHAR16                  *VariableName,
> -  IN  EFI_GUID                *VendorGuid,
> -  IN  BOOLEAN                 Volatile,
> -  IN  BOOLEAN                 Read,
> -  IN  BOOLEAN                 Write,
> -  IN  BOOLEAN                 Delete,
> -  IN  BOOLEAN                 Cache
> -  )
> -{
> -  VARIABLE_INFO_ENTRY   *Entry;
> -
> -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> -
> -    if (AtRuntime ()) {
> -      // Don't collect statistics at runtime.
> -      return;
> -    }
> -
> -    if (gVariableInfo == NULL) {
> -      //
> -      // On the first call allocate a entry and place a pointer to it in
> -      // the EFI System Table.
> -      //
> -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> -      ASSERT (gVariableInfo != NULL);
> -
> -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> -      ASSERT (gVariableInfo->Name != NULL);
> -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> -      gVariableInfo->Volatile = Volatile;
> -    }
> -
> -
> -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> -        if (StrCmp (VariableName, Entry->Name) == 0) {
> -          if (Read) {
> -            Entry->ReadCount++;
> -          }
> -          if (Write) {
> -            Entry->WriteCount++;
> -          }
> -          if (Delete) {
> -            Entry->DeleteCount++;
> -          }
> -          if (Cache) {
> -            Entry->CacheCount++;
> -          }
> -
> -          return;
> -        }
> -      }
> -
> -      if (Entry->Next == NULL) {
> -        //
> -        // If the entry is not in the table add it.
> -        // Next iteration of the loop will fill in the data.
> -        //
> -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> -        ASSERT (Entry->Next != NULL);
> -
> -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> -        ASSERT (Entry->Next->Name != NULL);
> -        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> -        Entry->Next->Volatile = Volatile;
> -      }
> -
> -    }
> -  }
> -}
> -
> -
> -/**
> -
> -  This code checks if variable header is valid or not.
> -
> -  @param Variable           Pointer to the Variable Header.
> -  @param VariableStoreEnd   Pointer to the Variable Store End.
> -
> -  @retval TRUE              Variable header is valid.
> -  @retval FALSE             Variable header is not valid.
> -
> -**/
> -BOOLEAN
> -IsValidVariableHeader (
> -  IN  VARIABLE_HEADER       *Variable,
> -  IN  VARIABLE_HEADER       *VariableStoreEnd
> -  )
> -{
> -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId !=
> VARIABLE_DATA)) {
> -    //
> -    // Variable is NULL or has reached the end of variable store,
> -    // or the StartId is not correct.
> -    //
> -    return FALSE;
> -  }
> -
> -  return TRUE;
> -}
> -
> -
>  /**
> 
>    This function writes data to the FWH at the correct LBA even if the LBAs
> @@ -376,345 +252,6 @@ UpdateVariableStore (
>    return EFI_SUCCESS;
>  }
> 
> -
> -/**
> -
> -  This code gets the current status of Variable Store.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @retval EfiRaw         Variable store status is raw.
> -  @retval EfiValid       Variable store status is valid.
> -  @retval EfiInvalid     Variable store status is invalid.
> -
> -**/
> -VARIABLE_STORE_STATUS
> -GetVariableStoreStatus (
> -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> -  )
> -{
> -  if ((CompareGuid (&VarStoreHeader->Signature,
> &gEfiAuthenticatedVariableGuid) ||
> -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> -      ) {
> -
> -    return EfiValid;
> -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> -             VarStoreHeader->Size == 0xffffffff &&
> -             VarStoreHeader->Format == 0xff &&
> -             VarStoreHeader->State == 0xff
> -          ) {
> -
> -    return EfiRaw;
> -  } else {
> -    return EfiInvalid;
> -  }
> -}
> -
> -/**
> -  This code gets the size of variable header.
> -
> -  @return Size of variable header in bytes in type UINTN.
> -
> -**/
> -UINTN
> -GetVariableHeaderSize (
> -  VOID
> -  )
> -{
> -  UINTN Value;
> -
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> -  } else {
> -    Value = sizeof (VARIABLE_HEADER);
> -  }
> -
> -  return Value;
> -}
> -
> -/**
> -
> -  This code gets the size of name of variable.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return UINTN          Size of variable in bytes.
> -
> -**/
> -UINTN
> -NameSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    if (AuthVariable->State == (UINT8) (-1) ||
> -       AuthVariable->DataSize == (UINT32) (-1) ||
> -       AuthVariable->NameSize == (UINT32) (-1) ||
> -       AuthVariable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) AuthVariable->NameSize;
> -  } else {
> -    if (Variable->State == (UINT8) (-1) ||
> -        Variable->DataSize == (UINT32) (-1) ||
> -        Variable->NameSize == (UINT32) (-1) ||
> -        Variable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) Variable->NameSize;
> -  }
> -}
> -
> -/**
> -  This code sets the size of name of variable.
> -
> -  @param[in] Variable   Pointer to the Variable Header.
> -  @param[in] NameSize   Name size to set.
> -
> -**/
> -VOID
> -SetNameSizeOfVariable (
> -  IN VARIABLE_HEADER    *Variable,
> -  IN UINTN              NameSize
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    AuthVariable->NameSize = (UINT32) NameSize;
> -  } else {
> -    Variable->NameSize = (UINT32) NameSize;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the size of variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Size of variable in bytes.
> -
> -**/
> -UINTN
> -DataSizeOfVariable (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    if (AuthVariable->State == (UINT8) (-1) ||
> -       AuthVariable->DataSize == (UINT32) (-1) ||
> -       AuthVariable->NameSize == (UINT32) (-1) ||
> -       AuthVariable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) AuthVariable->DataSize;
> -  } else {
> -    if (Variable->State == (UINT8) (-1) ||
> -        Variable->DataSize == (UINT32) (-1) ||
> -        Variable->NameSize == (UINT32) (-1) ||
> -        Variable->Attributes == (UINT32) (-1)) {
> -      return 0;
> -    }
> -    return (UINTN) Variable->DataSize;
> -  }
> -}
> -
> -/**
> -  This code sets the size of variable data.
> -
> -  @param[in] Variable   Pointer to the Variable Header.
> -  @param[in] DataSize   Data size to set.
> -
> -**/
> -VOID
> -SetDataSizeOfVariable (
> -  IN VARIABLE_HEADER    *Variable,
> -  IN UINTN              DataSize
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    AuthVariable->DataSize = (UINT32) DataSize;
> -  } else {
> -    Variable->DataSize = (UINT32) DataSize;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the pointer to the variable name.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Name which is Unicode encoding.
> -
> -**/
> -CHAR16 *
> -GetVariableNamePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> -}
> -
> -/**
> -  This code gets the pointer to the variable guid.
> -
> -  @param Variable   Pointer to the Variable Header.
> -
> -  @return A EFI_GUID* pointer to Vendor Guid.
> -
> -**/
> -EFI_GUID *
> -GetVendorGuidPtr (
> -  IN VARIABLE_HEADER    *Variable
> -  )
> -{
> -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> -
> -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> -    return &AuthVariable->VendorGuid;
> -  } else {
> -    return &Variable->VendorGuid;
> -  }
> -}
> -
> -/**
> -
> -  This code gets the pointer to the variable data.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to Variable Data.
> -
> -**/
> -UINT8 *
> -GetVariableDataPtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  //
> -  // Be careful about pad size for alignment.
> -  //
> -  Value =  (UINTN) GetVariableNamePtr (Variable);
> -  Value += NameSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> -
> -  return (UINT8 *) Value;
> -}
> -
> -/**
> -  This code gets the variable data offset related to variable header.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Variable Data offset.
> -
> -**/
> -UINTN
> -GetVariableDataOffset (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  //
> -  // Be careful about pad size for alignment
> -  //
> -  Value = GetVariableHeaderSize ();
> -  Value += NameSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> -
> -  return Value;
> -}
> -
> -/**
> -
> -  This code gets the pointer to the next variable header.
> -
> -  @param Variable        Pointer to the Variable Header.
> -
> -  @return Pointer to next variable header.
> -
> -**/
> -VARIABLE_HEADER *
> -GetNextVariablePtr (
> -  IN  VARIABLE_HEADER   *Variable
> -  )
> -{
> -  UINTN Value;
> -
> -  Value =  (UINTN) GetVariableDataPtr (Variable);
> -  Value += DataSizeOfVariable (Variable);
> -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> -
> -  //
> -  // Be careful about pad size for alignment.
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> -}
> -
> -/**
> -
> -  Gets the pointer to the first variable header in given variable store area.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the first variable header.
> -
> -**/
> -VARIABLE_HEADER *
> -GetStartPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  )
> -{
> -  //
> -  // The start of variable store.
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> -}
> -
> -/**
> -
> -  Gets the pointer to the end of the variable storage area.
> -
> -  This function gets pointer to the end of the variable storage
> -  area, according to the input variable store header.
> -
> -  @param VarStoreHeader  Pointer to the Variable Store Header.
> -
> -  @return Pointer to the end of the variable storage area.
> -
> -**/
> -VARIABLE_HEADER *
> -GetEndPointer (
> -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> -  )
> -{
> -  //
> -  // The end of variable store
> -  //
> -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader +
> VarStoreHeader->Size);
> -}
> -
>  /**
>    Record variable error flag.
> 
> @@ -1228,75 +765,6 @@ Done:
>    return Status;
>  }
> 
> -/**
> -  Find the variable in the specified variable store.
> -
> -  @param[in]       VariableName        Name of the variable to be found
> -  @param[in]       VendorGuid          Vendor GUID to be found.
> -  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> -                                       check at runtime when searching variable.
> -  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains
> Variable Information.
> -
> -  @retval          EFI_SUCCESS         Variable found successfully
> -  @retval          EFI_NOT_FOUND       Variable not found
> -**/
> -EFI_STATUS
> -FindVariableEx (
> -  IN     CHAR16                  *VariableName,
> -  IN     EFI_GUID                *VendorGuid,
> -  IN     BOOLEAN                 IgnoreRtCheck,
> -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> -  )
> -{
> -  VARIABLE_HEADER                *InDeletedVariable;
> -  VOID                           *Point;
> -
> -  PtrTrack->InDeletedTransitionPtr = NULL;
> -
> -  //
> -  // Find the variable by walk through HOB, volatile and non-volatile variable
> store.
> -  //
> -  InDeletedVariable  = NULL;
> -
> -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> -      ) {
> -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)
> -       ) {
> -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> -        if (VariableName[0] == 0) {
> -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -            InDeletedVariable   = PtrTrack->CurrPtr;
> -          } else {
> -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> -            return EFI_SUCCESS;
> -          }
> -        } else {
> -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {
> -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> -
> -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> -            if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack-
> >CurrPtr)) == 0) {
> -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -                InDeletedVariable     = PtrTrack->CurrPtr;
> -              } else {
> -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> -                return EFI_SUCCESS;
> -              }
> -            }
> -          }
> -        }
> -      }
> -    }
> -  }
> -
> -  PtrTrack->CurrPtr = InDeletedVariable;
> -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> -}
> -
> -
>  /**
>    Finds variable in storage blocks of volatile and non-volatile storage areas.
> 
> @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
>    }
>  }
> 
> -/**
> -  Compare two EFI_TIME data.
> -
> -
> -  @param FirstTime           A pointer to the first EFI_TIME data.
> -  @param SecondTime          A pointer to the second EFI_TIME data.
> -
> -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> -  @retval  FALSE             The FirstTime is later than the SecondTime.
> -
> -**/
> -BOOLEAN
> -VariableCompareTimeStampInternal (
> -  IN EFI_TIME               *FirstTime,
> -  IN EFI_TIME               *SecondTime
> -  )
> -{
> -  if (FirstTime->Year != SecondTime->Year) {
> -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> -  } else if (FirstTime->Month != SecondTime->Month) {
> -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> -  } else if (FirstTime->Day != SecondTime->Day) {
> -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> -  } else if (FirstTime->Hour != SecondTime->Hour) {
> -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> -  } else if (FirstTime->Minute != SecondTime->Minute) {
> -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> -  }
> -
> -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> -}
> -
>  /**
>    Update the variable region with Variable information. If
> EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
>    index of associated public key is needed.
> @@ -2885,166 +2321,6 @@ Done:
>    return Status;
>  }
> 
> -/**
> -  This code Finds the Next available variable.
> -
> -  Caution: This function may receive untrusted input.
> -  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> -
> -  @param[in]  VariableName  Pointer to variable name.
> -  @param[in]  VendorGuid    Variable Vendor Guid.
> -  @param[out] VariablePtr   Pointer to variable header address.
> -
> -  @retval EFI_SUCCESS           The function completed successfully.
> -  @retval EFI_NOT_FOUND         The next variable was not found.
> -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> -                                GUID of an existing variable.
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -VariableServiceGetNextVariableInternal (
> -  IN  CHAR16                *VariableName,
> -  IN  EFI_GUID              *VendorGuid,
> -  OUT VARIABLE_HEADER       **VariablePtr
> -  )
> -{
> -  VARIABLE_STORE_TYPE     Type;
> -  VARIABLE_POINTER_TRACK  Variable;
> -  VARIABLE_POINTER_TRACK  VariableInHob;
> -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> -  EFI_STATUS              Status;
> -  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
> -
> -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> &mVariableModuleGlobal->VariableGlobal, FALSE);
> -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> -    //
> -    // For VariableName is an empty string, FindVariable() will try to find and
> return
> -    // the first qualified variable, and if FindVariable() returns error
> (EFI_NOT_FOUND)
> -    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
> -    //
> -    if (VariableName[0] != 0) {
> -      //
> -      // For VariableName is not an empty string, and FindVariable() returns error
> as
> -      // VariableName and VendorGuid are not a name and GUID of an existing
> variable,
> -      // there is no way to get next variable, follow spec to return
> EFI_INVALID_PARAMETER.
> -      //
> -      Status = EFI_INVALID_PARAMETER;
> -    }
> -    goto Done;
> -  }
> -
> -  if (VariableName[0] != 0) {
> -    //
> -    // If variable name is not NULL, get next variable.
> -    //
> -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -  }
> -
> -  //
> -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> -  // The index and attributes mapping must be kept in this order as FindVariable
> -  // makes use of this mapping to implement search algorithm.
> -  //
> -  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER
> *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> -  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER
> *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> -
> -  while (TRUE) {
> -    //
> -    // Switch from Volatile to HOB, to Non-Volatile.
> -    //
> -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> -      //
> -      // Find current storage index
> -      //
> -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> Type++) {
> -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreHeader[Type]))) {
> -          break;
> -        }
> -      }
> -      ASSERT (Type < VariableStoreTypeMax);
> -      //
> -      // Switch to next storage
> -      //
> -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> -        if (VariableStoreHeader[Type] != NULL) {
> -          break;
> -        }
> -      }
> -      //
> -      // Capture the case that
> -      // 1. current storage is the last one, or
> -      // 2. no further storage
> -      //
> -      if (Type == VariableStoreTypeMax) {
> -        Status = EFI_NOT_FOUND;
> -        goto Done;
> -      }
> -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> -      Variable.CurrPtr  = Variable.StartPtr;
> -    }
> -
> -    //
> -    // Variable is found
> -    //
> -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> -          //
> -          // If it is a IN_DELETED_TRANSITION variable,
> -          // and there is also a same ADDED one at the same time,
> -          // don't return it.
> -          //
> -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> -          Status = FindVariableEx (
> -                     GetVariableNamePtr (Variable.CurrPtr),
> -                     GetVendorGuidPtr (Variable.CurrPtr),
> -                     FALSE,
> -                     &VariablePtrTrack
> -                     );
> -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> VAR_ADDED) {
> -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -            continue;
> -          }
> -        }
> -
> -        //
> -        // Don't return NV variable when HOB overrides it
> -        //
> -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> -            (Variable.StartPtr == GetStartPointer
> (VariableStoreHeader[VariableStoreTypeNv]))
> -           ) {
> -          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> -          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> -          Status = FindVariableEx (
> -                     GetVariableNamePtr (Variable.CurrPtr),
> -                     GetVendorGuidPtr (Variable.CurrPtr),
> -                     FALSE,
> -                     &VariableInHob
> -                     );
> -          if (!EFI_ERROR (Status)) {
> -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -            continue;
> -          }
> -        }
> -
> -        *VariablePtr = Variable.CurrPtr;
> -        Status = EFI_SUCCESS;
> -        goto Done;
> -      }
> -    }
> -
> -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> -  }
> -
> -Done:
> -  return Status;
> -}
> -
>  /**
> 
>    This code Finds the Next available variable.
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> index cb6fcebe2d..dc78f68fa9 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> @@ -1,12 +1,13 @@
>  /** @file
>    Provides variable driver extended services.
> 
> -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  /**
>    Finds variable in storage blocks of volatile and non-volatile storage areas.
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> new file mode 100644
> index 0000000000..7de0a90772
> --- /dev/null
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> @@ -0,0 +1,731 @@
> +/** @file
> +  Functions in this module are associated with variable parsing operations and
> +  are intended to be usable across variable driver source files.
> +
> +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "VariableParsing.h"
> +
> +/**
> +
> +  This code checks if variable header is valid or not.
> +
> +  @param Variable           Pointer to the Variable Header.
> +  @param VariableStoreEnd   Pointer to the Variable Store End.
> +
> +  @retval TRUE              Variable header is valid.
> +  @retval FALSE             Variable header is not valid.
> +
> +**/
> +BOOLEAN
> +IsValidVariableHeader (
> +  IN  VARIABLE_HEADER       *Variable,
> +  IN  VARIABLE_HEADER       *VariableStoreEnd
> +  )
> +{
> +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> >StartId != VARIABLE_DATA)) {
> +    //
> +    // Variable is NULL or has reached the end of variable store,
> +    // or the StartId is not correct.
> +    //
> +    return FALSE;
> +  }
> +
> +  return TRUE;
> +}
> +
> +/**
> +
> +  This code gets the current status of Variable Store.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @retval EfiRaw         Variable store status is raw.
> +  @retval EfiValid       Variable store status is valid.
> +  @retval EfiInvalid     Variable store status is invalid.
> +
> +**/
> +VARIABLE_STORE_STATUS
> +GetVariableStoreStatus (
> +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> +  )
> +{
> +  if ((CompareGuid (&VarStoreHeader->Signature,
> &gEfiAuthenticatedVariableGuid) ||
> +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> +      ) {
> +
> +    return EfiValid;
> +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> +             VarStoreHeader->Size == 0xffffffff &&
> +             VarStoreHeader->Format == 0xff &&
> +             VarStoreHeader->State == 0xff
> +          ) {
> +
> +    return EfiRaw;
> +  } else {
> +    return EfiInvalid;
> +  }
> +}
> +
> +/**
> +  This code gets the size of variable header.
> +
> +  @return Size of variable header in bytes in type UINTN.
> +
> +**/
> +UINTN
> +GetVariableHeaderSize (
> +  VOID
> +  )
> +{
> +  UINTN Value;
> +
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> +  } else {
> +    Value = sizeof (VARIABLE_HEADER);
> +  }
> +
> +  return Value;
> +}
> +
> +/**
> +
> +  This code gets the size of name of variable.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return UINTN          Size of variable in bytes.
> +
> +**/
> +UINTN
> +NameSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    if (AuthVariable->State == (UINT8) (-1) ||
> +       AuthVariable->DataSize == (UINT32) (-1) ||
> +       AuthVariable->NameSize == (UINT32) (-1) ||
> +       AuthVariable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) AuthVariable->NameSize;
> +  } else {
> +    if (Variable->State == (UINT8) (-1) ||
> +        Variable->DataSize == (UINT32) (-1) ||
> +        Variable->NameSize == (UINT32) (-1) ||
> +        Variable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) Variable->NameSize;
> +  }
> +}
> +
> +/**
> +  This code sets the size of name of variable.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] NameSize   Name size to set.
> +
> +**/
> +VOID
> +SetNameSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              NameSize
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    AuthVariable->NameSize = (UINT32) NameSize;
> +  } else {
> +    Variable->NameSize = (UINT32) NameSize;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the size of variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Size of variable in bytes.
> +
> +**/
> +UINTN
> +DataSizeOfVariable (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    if (AuthVariable->State == (UINT8) (-1) ||
> +       AuthVariable->DataSize == (UINT32) (-1) ||
> +       AuthVariable->NameSize == (UINT32) (-1) ||
> +       AuthVariable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) AuthVariable->DataSize;
> +  } else {
> +    if (Variable->State == (UINT8) (-1) ||
> +        Variable->DataSize == (UINT32) (-1) ||
> +        Variable->NameSize == (UINT32) (-1) ||
> +        Variable->Attributes == (UINT32) (-1)) {
> +      return 0;
> +    }
> +    return (UINTN) Variable->DataSize;
> +  }
> +}
> +
> +/**
> +  This code sets the size of variable data.
> +
> +  @param[in] Variable   Pointer to the Variable Header.
> +  @param[in] DataSize   Data size to set.
> +
> +**/
> +VOID
> +SetDataSizeOfVariable (
> +  IN VARIABLE_HEADER    *Variable,
> +  IN UINTN              DataSize
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    AuthVariable->DataSize = (UINT32) DataSize;
> +  } else {
> +    Variable->DataSize = (UINT32) DataSize;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the pointer to the variable name.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Name which is Unicode encoding.
> +
> +**/
> +CHAR16 *
> +GetVariableNamePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> +}
> +
> +/**
> +  This code gets the pointer to the variable guid.
> +
> +  @param Variable   Pointer to the Variable Header.
> +
> +  @return A EFI_GUID* pointer to Vendor Guid.
> +
> +**/
> +EFI_GUID *
> +GetVendorGuidPtr (
> +  IN VARIABLE_HEADER    *Variable
> +  )
> +{
> +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> +
> +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> +    return &AuthVariable->VendorGuid;
> +  } else {
> +    return &Variable->VendorGuid;
> +  }
> +}
> +
> +/**
> +
> +  This code gets the pointer to the variable data.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to Variable Data.
> +
> +**/
> +UINT8 *
> +GetVariableDataPtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  //
> +  // Be careful about pad size for alignment.
> +  //
> +  Value =  (UINTN) GetVariableNamePtr (Variable);
> +  Value += NameSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> +
> +  return (UINT8 *) Value;
> +}
> +
> +/**
> +  This code gets the variable data offset related to variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Variable Data offset.
> +
> +**/
> +UINTN
> +GetVariableDataOffset (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  //
> +  // Be careful about pad size for alignment
> +  //
> +  Value = GetVariableHeaderSize ();
> +  Value += NameSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> +
> +  return Value;
> +}
> +
> +/**
> +
> +  This code gets the pointer to the next variable header.
> +
> +  @param Variable        Pointer to the Variable Header.
> +
> +  @return Pointer to next variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetNextVariablePtr (
> +  IN  VARIABLE_HEADER   *Variable
> +  )
> +{
> +  UINTN Value;
> +
> +  Value =  (UINTN) GetVariableDataPtr (Variable);
> +  Value += DataSizeOfVariable (Variable);
> +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> +
> +  //
> +  // Be careful about pad size for alignment.
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> +}
> +
> +/**
> +
> +  Gets the pointer to the first variable header in given variable store area.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the first variable header.
> +
> +**/
> +VARIABLE_HEADER *
> +GetStartPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  )
> +{
> +  //
> +  // The start of variable store.
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> +}
> +
> +/**
> +
> +  Gets the pointer to the end of the variable storage area.
> +
> +  This function gets pointer to the end of the variable storage
> +  area, according to the input variable store header.
> +
> +  @param VarStoreHeader  Pointer to the Variable Store Header.
> +
> +  @return Pointer to the end of the variable storage area.
> +
> +**/
> +VARIABLE_HEADER *
> +GetEndPointer (
> +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> +  )
> +{
> +  //
> +  // The end of variable store
> +  //
> +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader +
> VarStoreHeader->Size);
> +}
> +
> +/**
> +  Compare two EFI_TIME data.
> +
> +
> +  @param FirstTime           A pointer to the first EFI_TIME data.
> +  @param SecondTime          A pointer to the second EFI_TIME data.
> +
> +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> +  @retval  FALSE             The FirstTime is later than the SecondTime.
> +
> +**/
> +BOOLEAN
> +VariableCompareTimeStampInternal (
> +  IN EFI_TIME               *FirstTime,
> +  IN EFI_TIME               *SecondTime
> +  )
> +{
> +  if (FirstTime->Year != SecondTime->Year) {
> +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> +  } else if (FirstTime->Month != SecondTime->Month) {
> +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> +  } else if (FirstTime->Day != SecondTime->Day) {
> +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> +  } else if (FirstTime->Hour != SecondTime->Hour) {
> +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> +  } else if (FirstTime->Minute != SecondTime->Minute) {
> +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> +  }
> +
> +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> +}
> +
> +/**
> +  Find the variable in the specified variable store.
> +
> +  @param[in]       VariableName        Name of the variable to be found
> +  @param[in]       VendorGuid          Vendor GUID to be found.
> +  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS
> attribute
> +                                       check at runtime when searching variable.
> +  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains
> Variable Information.
> +
> +  @retval          EFI_SUCCESS         Variable found successfully
> +  @retval          EFI_NOT_FOUND       Variable not found
> +**/
> +EFI_STATUS
> +FindVariableEx (
> +  IN     CHAR16                  *VariableName,
> +  IN     EFI_GUID                *VendorGuid,
> +  IN     BOOLEAN                 IgnoreRtCheck,
> +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> +  )
> +{
> +  VARIABLE_HEADER                *InDeletedVariable;
> +  VOID                           *Point;
> +
> +  PtrTrack->InDeletedTransitionPtr = NULL;
> +
> +  //
> +  // Find the variable by walk through HOB, volatile and non-volatile variable
> store.
> +  //
> +  InDeletedVariable  = NULL;
> +
> +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> +      ) {
> +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)
> +       ) {
> +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> +        if (VariableName[0] == 0) {
> +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +            InDeletedVariable   = PtrTrack->CurrPtr;
> +          } else {
> +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> +            return EFI_SUCCESS;
> +          }
> +        } else {
> +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {
> +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> +
> +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> +            if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack-
> >CurrPtr)) == 0) {
> +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +                InDeletedVariable     = PtrTrack->CurrPtr;
> +              } else {
> +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> +                return EFI_SUCCESS;
> +              }
> +            }
> +          }
> +        }
> +      }
> +    }
> +  }
> +
> +  PtrTrack->CurrPtr = InDeletedVariable;
> +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> +}
> +
> +/**
> +  This code Finds the Next available variable.
> +
> +  Caution: This function may receive untrusted input.
> +  This function may be invoked in SMM mode. This function will do basic
> validation, before parse the data.
> +
> +  @param[in]  VariableName  Pointer to variable name.
> +  @param[in]  VendorGuid    Variable Vendor Guid.
> +  @param[out] VariablePtr   Pointer to variable header address.
> +
> +  @retval EFI_SUCCESS           The function completed successfully.
> +  @retval EFI_NOT_FOUND         The next variable was not found.
> +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string,
> while VendorGuid is NULL.
> +  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> VendorGuid are not a name and
> +                                GUID of an existing variable.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +VariableServiceGetNextVariableInternal (
> +  IN  CHAR16                *VariableName,
> +  IN  EFI_GUID              *VendorGuid,
> +  OUT VARIABLE_HEADER       **VariablePtr
> +  )
> +{
> +  VARIABLE_STORE_TYPE     Type;
> +  VARIABLE_POINTER_TRACK  Variable;
> +  VARIABLE_POINTER_TRACK  VariableInHob;
> +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> +  EFI_STATUS              Status;
> +  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];
> +
> +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> &mVariableModuleGlobal->VariableGlobal, FALSE);
> +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> +    //
> +    // For VariableName is an empty string, FindVariable() will try to find and
> return
> +    // the first qualified variable, and if FindVariable() returns error
> (EFI_NOT_FOUND)
> +    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).
> +    //
> +    if (VariableName[0] != 0) {
> +      //
> +      // For VariableName is not an empty string, and FindVariable() returns error
> as
> +      // VariableName and VendorGuid are not a name and GUID of an existing
> variable,
> +      // there is no way to get next variable, follow spec to return
> EFI_INVALID_PARAMETER.
> +      //
> +      Status = EFI_INVALID_PARAMETER;
> +    }
> +    goto Done;
> +  }
> +
> +  if (VariableName[0] != 0) {
> +    //
> +    // If variable name is not NULL, get next variable.
> +    //
> +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +  }
> +
> +  //
> +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> +  // The index and attributes mapping must be kept in this order as FindVariable
> +  // makes use of this mapping to implement search algorithm.
> +  //
> +  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> >VariableGlobal.VolatileVariableBase;
> +  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER
> *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> +
> +  while (TRUE) {
> +    //
> +    // Switch from Volatile to HOB, to Non-Volatile.
> +    //
> +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> +      //
> +      // Find current storage index
> +      //
> +      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> Type++) {
> +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> GetStartPointer (VariableStoreHeader[Type]))) {
> +          break;
> +        }
> +      }
> +      ASSERT (Type < VariableStoreTypeMax);
> +      //
> +      // Switch to next storage
> +      //
> +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> +        if (VariableStoreHeader[Type] != NULL) {
> +          break;
> +        }
> +      }
> +      //
> +      // Capture the case that
> +      // 1. current storage is the last one, or
> +      // 2. no further storage
> +      //
> +      if (Type == VariableStoreTypeMax) {
> +        Status = EFI_NOT_FOUND;
> +        goto Done;
> +      }
> +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> +      Variable.CurrPtr  = Variable.StartPtr;
> +    }
> +
> +    //
> +    // Variable is found
> +    //
> +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> VAR_ADDED)) {
> +          //
> +          // If it is a IN_DELETED_TRANSITION variable,
> +          // and there is also a same ADDED one at the same time,
> +          // don't return it.
> +          //
> +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> +          Status = FindVariableEx (
> +                     GetVariableNamePtr (Variable.CurrPtr),
> +                     GetVendorGuidPtr (Variable.CurrPtr),
> +                     FALSE,
> +                     &VariablePtrTrack
> +                     );
> +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> VAR_ADDED) {
> +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +            continue;
> +          }
> +        }
> +
> +        //
> +        // Don't return NV variable when HOB overrides it
> +        //
> +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> +            (Variable.StartPtr == GetStartPointer
> (VariableStoreHeader[VariableStoreTypeNv]))
> +           ) {
> +          VariableInHob.StartPtr = GetStartPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> +          VariableInHob.EndPtr   = GetEndPointer
> (VariableStoreHeader[VariableStoreTypeHob]);
> +          Status = FindVariableEx (
> +                     GetVariableNamePtr (Variable.CurrPtr),
> +                     GetVendorGuidPtr (Variable.CurrPtr),
> +                     FALSE,
> +                     &VariableInHob
> +                     );
> +          if (!EFI_ERROR (Status)) {
> +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +            continue;
> +          }
> +        }
> +
> +        *VariablePtr = Variable.CurrPtr;
> +        Status = EFI_SUCCESS;
> +        goto Done;
> +      }
> +    }
> +
> +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> +  }
> +
> +Done:
> +  return Status;
> +}
> +
> +/**
> +  Routine used to track statistical information about variable usage.
> +  The data is stored in the EFI system table so it can be accessed later.
> +  VariableInfo.efi can dump out the table. Only Boot Services variable
> +  accesses are tracked by this code. The PcdVariableCollectStatistics
> +  build flag controls if this feature is enabled.
> +
> +  A read that hits in the cache will have Read and Cache true for
> +  the transaction. Data is allocated by this routine, but never
> +  freed.
> +
> +  @param[in] VariableName   Name of the Variable to track.
> +  @param[in] VendorGuid     Guid of the Variable to track.
> +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> +  @param[in] Read           TRUE if GetVariable() was called.
> +  @param[in] Write          TRUE if SetVariable() was called.
> +  @param[in] Delete         TRUE if deleted via SetVariable().
> +  @param[in] Cache          TRUE for a cache hit.
> +
> +**/
> +VOID
> +UpdateVariableInfo (
> +  IN  CHAR16                  *VariableName,
> +  IN  EFI_GUID                *VendorGuid,
> +  IN  BOOLEAN                 Volatile,
> +  IN  BOOLEAN                 Read,
> +  IN  BOOLEAN                 Write,
> +  IN  BOOLEAN                 Delete,
> +  IN  BOOLEAN                 Cache
> +  )
> +{
> +  VARIABLE_INFO_ENTRY   *Entry;
> +
> +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> +
> +    if (AtRuntime ()) {
> +      // Don't collect statistics at runtime.
> +      return;
> +    }
> +
> +    if (gVariableInfo == NULL) {
> +      //
> +      // On the first call allocate a entry and place a pointer to it in
> +      // the EFI System Table.
> +      //
> +      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> +      ASSERT (gVariableInfo != NULL);
> +
> +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> +      ASSERT (gVariableInfo->Name != NULL);
> +      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> +      gVariableInfo->Volatile = Volatile;
> +    }
> +
> +
> +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> +        if (StrCmp (VariableName, Entry->Name) == 0) {
> +          if (Read) {
> +            Entry->ReadCount++;
> +          }
> +          if (Write) {
> +            Entry->WriteCount++;
> +          }
> +          if (Delete) {
> +            Entry->DeleteCount++;
> +          }
> +          if (Cache) {
> +            Entry->CacheCount++;
> +          }
> +
> +          return;
> +        }
> +      }
> +
> +      if (Entry->Next == NULL) {
> +        //
> +        // If the entry is not in the table add it.
> +        // Next iteration of the loop will fill in the data.
> +        //
> +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> +        ASSERT (Entry->Next != NULL);
> +
> +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> +        ASSERT (Entry->Next->Name != NULL);
> +        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> VariableName);
> +        Entry->Next->Volatile = Volatile;
> +      }
> +
> +    }
> +  }
> +}
> diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> index ec463d063e..ce409f22a3 100644
> --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  #include <Guid/SmmVariableCommon.h>
>  #include "Variable.h"
> +#include "VariableParsing.h"
> 
>  BOOLEAN                                              mAtRuntime              = FALSE;
>  UINT8                                                *mVariableBufferPayload = NULL;
> --
> 2.16.2.windows.1


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

* Re: [edk2-devel] [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer
  2019-10-08  2:11       ` [edk2-devel] " Wu, Hao A
@ 2019-10-08 21:49         ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-08 21:49 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

I will clarify that aspect in the commit message.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Monday, October 7, 2019 7:12 PM
> To: devel@edk2.groups.io; Kubacki, Michael A <michael.a.kubacki@intel.com>
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [edk2-devel] [PATCH V2 3/9] MdeModulePkg/Variable:
> Parameterize VARIABLE_INFO_ENTRY buffer
> 
> > -----Original Message-----
> > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> > Kubacki, Michael A
> > Sent: Friday, October 04, 2019 2:06 AM
> > To: Wu, Hao A; devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> > Subject: Re: [edk2-devel] [PATCH V2 3/9] MdeModulePkg/Variable:
> > Parameterize VARIABLE_INFO_ENTRY buffer
> >
> > Your understanding is correct.
> 
> 
> Thanks for the confirmation.
> Could you help to refine the commit message in the next series? Thanks.
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > Thanks,
> > Michael
> >
> > > -----Original Message-----
> > > From: Wu, Hao A <hao.a.wu@intel.com>
> > > Sent: Thursday, October 3, 2019 1:04 AM
> > > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > > devel@edk2.groups.io
> > > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>;
> > > Laszlo
> > Ersek
> > > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> > Michael
> > > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang,
> > > Jian J <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > > Subject: RE: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> > > VARIABLE_INFO_ENTRY buffer
> > >
> > > > -----Original Message-----
> > > > From: Kubacki, Michael A
> > > > Sent: Saturday, September 28, 2019 9:47 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao,
> > > > Liming; Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> > > > Jiewen
> > > > Subject: [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize
> > > > VARIABLE_INFO_ENTRY buffer
> > > >
> > > > UpdateVariableInfo () currently accepts parameters regarding
> > > > updates to be made to a global variable of type
> > > > VARIABLE_INFO_ENTRY. This change passes the structure by pointer
> > > > to UpdateVariableInfo () so structures can be updated outside the fixed
> global variable.
> > >
> > >
> > > For:
> > > "... so structures can be updated outside the fixed global variable "
> > >
> > > Do you mean:
> > >
> > > VARIABLE_INFO_ENTRY structure pointers other than "&gVariableInfo"
> > > can be passed to UpdateVariableInfo().
> > >
> > > Is my understanding correct? If so,
> > > Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
> > >
> > > Best Regards,
> > > Hao Wu
> > >
> > >
> > > >
> > > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > > Cc: Eric Dong <eric.dong@intel.com>
> > > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > > Cc: Liming Gao <liming.gao@intel.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Ray Ni <ray.ni@intel.com>
> > > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > > ---
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 18
> > > > +++++----
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 14
> > +++---
> > > -
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 41
> > > > +++++++++++---------
> > > >  3 files changed, 39 insertions(+), 34 deletions(-)
> > > >
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > index 0d231511ea..6f2000f3ee 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > @@ -286,13 +286,14 @@ GetNextVariableEx (
> > > >    the transaction. Data is allocated by this routine, but never
> > > >    freed.
> > > >
> > > > -  @param[in] VariableName   Name of the Variable to track.
> > > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > > -  @param[in] Cache          TRUE for a cache hit.
> > > > +  @param[in]      VariableName   Name of the Variable to track.
> > > > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > > > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > > > +  @param[in]      Read           TRUE if GetVariable() was called.
> > > > +  @param[in]      Write          TRUE if SetVariable() was called.
> > > > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > > > +  @param[in]      Cache          TRUE for a cache hit.
> > > > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > > > VARIABLE_INFO_ENTRY structures.
> > > >
> > > >  **/
> > > >  VOID
> > > > @@ -303,7 +304,8 @@ UpdateVariableInfo (
> > > >    IN  BOOLEAN                 Read,
> > > >    IN  BOOLEAN                 Write,
> > > >    IN  BOOLEAN                 Delete,
> > > > -  IN  BOOLEAN                 Cache
> > > > +  IN  BOOLEAN                 Cache,
> > > > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > > >    );
> > > >
> > > >  #endif
> > > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > index 816e8f7b8f..1a57d7e1ba 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > @@ -1641,7 +1641,7 @@ UpdateVariable (
> > > >          // go to delete this variable in variable HOB and
> > > >          // try to flush other variables from HOB to flash.
> > > >          //
> > > > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > > FALSE,
> > > > TRUE, FALSE);
> > > > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE,
> > > > + FALSE, FALSE,
> > > > TRUE, FALSE, &gVariableInfo);
> > > >          FlushHobVariableToFlash (VariableName, VendorGuid);
> > > >          return EFI_SUCCESS;
> > > >        }
> > > > @@ -1758,7 +1758,7 @@ UpdateVariable (
> > > >                   &State
> > > >                   );
> > > >        if (!EFI_ERROR (Status)) {
> > > > -        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > > > FALSE, FALSE, TRUE, FALSE);
> > > > +        UpdateVariableInfo (VariableName, VendorGuid,
> > > > + Variable->Volatile,
> > > > FALSE, FALSE, TRUE, FALSE, &gVariableInfo);
> > > >          if (!Variable->Volatile) {
> > > >            CacheVariable->CurrPtr->State = State;
> > > >            FlushHobVariableToFlash (VariableName, VendorGuid); @@
> > > > -1777,7 +1777,7 @@ UpdateVariable (
> > > >        //
> > > >        // Variable content unchanged and no need to update
> > > > timestamp, just return.
> > > >        //
> > > > -      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile,
> > > > FALSE, TRUE, FALSE, FALSE);
> > > > +      UpdateVariableInfo (VariableName, VendorGuid,
> > > > + Variable->Volatile,
> > > > FALSE, TRUE, FALSE, FALSE, &gVariableInfo);
> > > >        Status = EFI_SUCCESS;
> > > >        goto Done;
> > > >      } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) || @@
> > > > -2006,7 +2006,7 @@ UpdateVariable (
> > > >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > > > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > > > CacheVariable->Variable-
> > > > >StartPtr));
> > > >            CacheVariable->InDeletedTransitionPtr = NULL;
> > > >          }
> > > > -        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE,
> > TRUE,
> > > > FALSE, FALSE);
> > > > +        UpdateVariableInfo (VariableName, VendorGuid, FALSE,
> > > > + FALSE, TRUE,
> > > > FALSE, FALSE, &gVariableInfo);
> > > >          FlushHobVariableToFlash (VariableName, VendorGuid);
> > > >        } else {
> > > >          if (IsCommonUserVariable && ((VarSize +
> > > > mVariableModuleGlobal-
> > > > >CommonUserVariableTotalSize) > mVariableModuleGlobal-
> > > > >CommonMaxUserVariableSpace)) {
> > > > @@ -2156,7 +2156,7 @@ UpdateVariable (
> > > >            CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN)
> > > > CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN)
> > > > CacheVariable->Variable-
> > > > >StartPtr));
> > > >            CacheVariable->InDeletedTransitionPtr = NULL;
> > > >          }
> > > > -        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE,
> > > > FALSE, FALSE);
> > > > +        UpdateVariableInfo (VariableName, VendorGuid, TRUE,
> > > > + FALSE, TRUE,
> > > > FALSE, FALSE, &gVariableInfo);
> > > >        }
> > > >        goto Done;
> > > >      }
> > > > @@ -2227,7 +2227,7 @@ UpdateVariable (
> > > >    }
> > > >
> > > >    if (!EFI_ERROR (Status)) {
> > > > -    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE,
> > TRUE,
> > > > FALSE, FALSE);
> > > > +    UpdateVariableInfo (VariableName, VendorGuid, Volatile,
> > > > + FALSE, TRUE,
> > > > FALSE, FALSE, &gVariableInfo);
> > > >      if (!Volatile) {
> > > >        FlushHobVariableToFlash (VariableName, VendorGuid);
> > > >      }
> > > > @@ -2306,7 +2306,7 @@ VariableServiceGetVariable (
> > > >      }
> > > >
> > > >      *DataSize = VarDataSize;
> > > > -    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile,
> > > TRUE,
> > > > FALSE, FALSE, FALSE);
> > > > +    UpdateVariableInfo (VariableName, VendorGuid,
> > > > + Variable.Volatile, TRUE,
> > > > FALSE, FALSE, FALSE, &gVariableInfo);
> > > >
> > > >      Status = EFI_SUCCESS;
> > > >      goto Done;
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > index 9bc5369a90..394195342d 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > @@ -660,13 +660,14 @@ Done:
> > > >    the transaction. Data is allocated by this routine, but never
> > > >    freed.
> > > >
> > > > -  @param[in] VariableName   Name of the Variable to track.
> > > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > > -  @param[in] Cache          TRUE for a cache hit.
> > > > +  @param[in]      VariableName   Name of the Variable to track.
> > > > +  @param[in]      VendorGuid     Guid of the Variable to track.
> > > > +  @param[in]      Volatile       TRUE if volatile FALSE if non-volatile.
> > > > +  @param[in]      Read           TRUE if GetVariable() was called.
> > > > +  @param[in]      Write          TRUE if SetVariable() was called.
> > > > +  @param[in]      Delete         TRUE if deleted via SetVariable().
> > > > +  @param[in]      Cache          TRUE for a cache hit.
> > > > +  @param[in,out]  VariableInfo   Pointer to a pointer of
> > > > VARIABLE_INFO_ENTRY structures.
> > > >
> > > >  **/
> > > >  VOID
> > > > @@ -677,35 +678,38 @@ UpdateVariableInfo (
> > > >    IN  BOOLEAN                 Read,
> > > >    IN  BOOLEAN                 Write,
> > > >    IN  BOOLEAN                 Delete,
> > > > -  IN  BOOLEAN                 Cache
> > > > +  IN  BOOLEAN                 Cache,
> > > > +  IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > > >    )
> > > >  {
> > > >    VARIABLE_INFO_ENTRY   *Entry;
> > > >
> > > >    if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > > -
> > > > +    if (VariableName == NULL || VendorGuid == NULL ||
> > > > + VariableInfo ==
> > > > NULL) {
> > > > +      return;
> > > > +    }
> > > >      if (AtRuntime ()) {
> > > >        // Don't collect statistics at runtime.
> > > >        return;
> > > >      }
> > > >
> > > > -    if (gVariableInfo == NULL) {
> > > > +    if (*VariableInfo == NULL) {
> > > >        //
> > > >        // On the first call allocate a entry and place a pointer to it in
> > > >        // the EFI System Table.
> > > >        //
> > > > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > > -      ASSERT (gVariableInfo != NULL);
> > > > +      *VariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > > +      ASSERT (*VariableInfo != NULL);
> > > >
> > > > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > > -      ASSERT (gVariableInfo->Name != NULL);
> > > > -      StrCpyS (gVariableInfo->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > -      gVariableInfo->Volatile = Volatile;
> > > > +      CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid);
> > > > +      (*VariableInfo)->Name = AllocateZeroPool (StrSize (VariableName));
> > > > +      ASSERT ((*VariableInfo)->Name != NULL);
> > > > +      StrCpyS ((*VariableInfo)->Name,
> > > > + StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > +      (*VariableInfo)->Volatile = Volatile;
> > > >      }
> > > >
> > > >
> > > > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > > +    for (Entry = (*VariableInfo); Entry != NULL; Entry =
> > > > + Entry->Next) {
> > > >        if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > > >          if (StrCmp (VariableName, Entry->Name) == 0) {
> > > >            if (Read) {
> > > > @@ -739,7 +743,6 @@ UpdateVariableInfo (
> > > >          StrCpyS (Entry->Next->Name,
> > > > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > >          Entry->Next->Volatile = Volatile;
> > > >        }
> > > > -
> > > >      }
> > > >    }
> > > >  }
> > > > --
> > > > 2.16.2.windows.1
> > >
> >
> >
> > 
> 


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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-10-08  2:11       ` Wu, Hao A
@ 2019-10-08 21:53         ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-08 21:53 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

The rationale for moving the generic parsing functions out would be better to capture in the
commit message. That will be added to V3.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Monday, October 7, 2019 7:12 PM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing functions
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Friday, October 04, 2019 1:36 AM
> > To: Wu, Hao A; devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> Kinney,
> > Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> > Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate
> common
> > parsing functions
> >
> > The main reason is cohesiveness in the VariableParsing.c file. These are
> > functions that
> > are commonly needed for general variable data structure parsing
> operations.
> > Most of
> > them just read a member or two in a VARIABLE_STORE_HEADER or
> > VARIABLE_HEADER
> > data structure,  perform a simple calculation if needed, and return some
> > value. More
> > complex functions such as FindVariableEx (), do this in iteration across the
> > variable store.
> > If a function is needed that performs these tasks, it is easier to have them
> > grouped into a
> > cohesive file than search which one is in Variable.c and VariableParsing.c on
> a
> > case-by-case
> > basis. UpdateVariableInfo () is the exception here, I can move this to a
> > separate file
> > if you prefer.
> 
> 
> I am fine to leave this function under VariableParsing.c.
> 
> 
> >
> > Also, Variable.c is, in my opinion, far too large. It is on trend to only grow
> > larger:
> >   * Today: ~4,600 LOC
> >   * ~2 years ago: ~4,200 LOC
> >   * ~4 years ago: ~4,100 LOC
> >   * ~5 years ago: ~3,440 LOC
> >   * ~8 years ago: ~2,600 LOC
> >
> > I think moving out generic parsing services such as these better groups
> > related
> > functionality in the short-term but also aids future refactoring efforts in the
> > file.
> 
> 
> Thanks for the clarification.
> 
> I suggest to update the commit message and add the above detailed criteria
> for
> those functions that got moved to the new file.
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > Thanks,
> > Michael
> >
> > > -----Original Message-----
> > > From: Wu, Hao A <hao.a.wu@intel.com>
> > > Sent: Thursday, October 3, 2019 1:03 AM
> > > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > > devel@edk2.groups.io
> > > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> > Ersek
> > > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> > Michael
> > > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian
> J
> > > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > > Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate
> > common
> > > parsing functions
> > >
> > > A couple of inline comments below:
> > >
> > >
> > > > -----Original Message-----
> > > > From: Kubacki, Michael A
> > > > Sent: Saturday, September 28, 2019 9:47 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > > Kinney,
> > > > Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > > > Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate
> common
> > > > parsing functions
> > > >
> > > > This change moves the following functions into a dedicated file
> > > > so they may be used in other variable files as needed. Furthermore,
> > > > it reduces the overall size of the common Variable.c file.
> > > >
> > > >  * DataSizeOfVariable ()
> > > >  * FindVariableEx ()
> > > >  * GetEndPointer ()
> > > >  * GetNextVariablePtr ()
> > > >  * GetStartPointer ()
> > > >  * GetVariableDataOffset ()
> > > >  * GetVariableDataPtr ()
> > > >  * GetVariableHeaderSize ()
> > > >  * GetVariableNamePtr ()
> > > >  * GetVariableStoreStatus ()
> > > >  * GetVendorGuidPtr ()
> > > >  * IsValidVariableHeader ()
> > > >  * NameSizeOfVariable ()
> > > >  * SetDataSizeOfVariable ()
> > > >  * SetNameSizeOfVariable ()
> > > >  * UpdateVariableInfo ()
> > > >  * VariableCompareTimeStampInternal ()
> > > >  * VariableServiceGetNextVariableInternal ()
> > >
> > >
> > > May I know what are the criteria for the above functions being moved to
> a
> > > separate file?
> > >
> > > At first, I think all of them will be consumed by the new codes that
> > > implement
> > > the runtime cache. But I found that, for functions like
> > > GetVariableDataOffset(),
> > > GetVariableStoreStatus() and etc., their usages are still remained within
> file
> > > Variable.c (seems not related with the runtime cache).
> > >
> > >
> > > >
> > > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > > Cc: Eric Dong <eric.dong@intel.com>
> > > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > > Cc: Liming Gao <liming.gao@intel.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Ray Ni <ray.ni@intel.com>
> > > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > > ---
> > > >
> > MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > > |   2 +
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> |
> > > 2
> > > > +
> > > >
> > > >
> > >
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> > > > |   7 +
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               |
> 119
> > -
> > > ---
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> |
> > > 306
> > > > ++++++++
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               |
> 726
> > > +--
> > > > ----------------
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |
> > 3
> > > +-
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> |
> > > 731
> > > > ++++++++++++++++++++
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> |
> > 1
> > > +
> > > >  9 files changed, 1052 insertions(+), 845 deletions(-)
> > >
> > >
> > > For the below change in VariableStandaloneMm.inf:
> > >
> > > [Guids]
> > > ...
> > >   ## SOMETIMES_CONSUMES   ## Variable:L"db"
> > >   ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> > >   ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > >   gEfiImageSecurityDatabaseGuid
> > > ...
> > >
> > > I think the above GUID is not used by the module specified by the above
> > INF.
> > > Could you double confirm on this?
> > >
> > > Best Regards,
> > > Hao Wu
> > >
> > >
> > > >
> > > > diff --git
> > > >
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > >
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > > index 641376c9c5..c35e5fe787 100644
> > > > ---
> > > >
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > > +++
> > > >
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > > > @@ -36,6 +36,8 @@
> > > >    Variable.c
> > > >    VariableDxe.c
> > > >    Variable.h
> > > > +  VariableParsing.c
> > > > +  VariableParsing.h
> > > >    PrivilegePolymorphic.h
> > > >    Measurement.c
> > > >    TcgMorLockDxe.c
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > > index 0a160d269d..626738b9c7 100644
> > > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > > > @@ -45,6 +45,8 @@
> > > >    Variable.c
> > > >    VariableTraditionalMm.c
> > > >    VariableSmm.c
> > > > +  VariableParsing.c
> > > > +  VariableParsing.h
> > > >    VarCheck.c
> > > >    Variable.h
> > > >    PrivilegePolymorphic.h
> > > > diff --git
> > > >
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > > nf
> > > >
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > > inf
> > > > index 21bc81163b..1ba8f9ebfb 100644
> > > > ---
> > > >
> > >
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> > > > nf
> > > > +++
> > > >
> > >
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> > > > inf
> > > > @@ -45,6 +45,8 @@
> > > >    Variable.c
> > > >    VariableSmm.c
> > > >    VariableStandaloneMm.c
> > > > +  VariableParsing.c
> > > > +  VariableParsing.h
> > > >    VarCheck.c
> > > >    Variable.h
> > > >    PrivilegePolymorphic.h
> > > > @@ -99,6 +101,11 @@
> > > >    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
> > > >    gEfiGlobalVariableGuid
> > > >
> > > > +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> > > > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> > > > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > > > +  gEfiImageSecurityDatabaseGuid
> > > > +
> > > >    gEfiMemoryOverwriteControlDataGuid            ##
> > SOMETIMES_CONSUMES
> > > > ## Variable:L"MemoryOverwriteRequestControl"
> > > >    gEfiMemoryOverwriteRequestControlLockGuid     ##
> > > > SOMETIMES_PRODUCES   ##
> > > > Variable:L"MemoryOverwriteRequestControlLock"
> > > >
> > > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > index 9eac43759f..fb574b2e32 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > > > @@ -179,89 +179,6 @@ FindVariable (
> > > >    IN  BOOLEAN                 IgnoreRtCheck
> > > >    );
> > > >
> > > > -/**
> > > > -
> > > > -  Gets the pointer to the end of the variable storage area.
> > > > -
> > > > -  This function gets pointer to the end of the variable storage
> > > > -  area, according to the input variable store header.
> > > > -
> > > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > -
> > > > -  @return Pointer to the end of the variable storage area.
> > > > -
> > > > -**/
> > > > -VARIABLE_HEADER *
> > > > -GetEndPointer (
> > > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > -  );
> > > > -
> > > > -/**
> > > > -  This code gets the size of variable header.
> > > > -
> > > > -  @return Size of variable header in bytes in type UINTN.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -GetVariableHeaderSize (
> > > > -  VOID
> > > > -  );
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the pointer to the variable name.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Pointer to Variable Name which is Unicode encoding.
> > > > -
> > > > -**/
> > > > -CHAR16 *
> > > > -GetVariableNamePtr (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  );
> > > > -
> > > > -/**
> > > > -  This code gets the pointer to the variable guid.
> > > > -
> > > > -  @param Variable   Pointer to the Variable Header.
> > > > -
> > > > -  @return A EFI_GUID* pointer to Vendor Guid.
> > > > -
> > > > -**/
> > > > -EFI_GUID *
> > > > -GetVendorGuidPtr (
> > > > -  IN VARIABLE_HEADER    *Variable
> > > > -  );
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the pointer to the variable data.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Pointer to Variable Data.
> > > > -
> > > > -**/
> > > > -UINT8 *
> > > > -GetVariableDataPtr (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  );
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the size of variable data.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Size of variable in bytes.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -DataSizeOfVariable (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  );
> > > > -
> > > >  /**
> > > >    This function is to check if the remaining variable space is enough to
> set
> > > >    all Variables from argument list successfully. The purpose of the check
> > > > @@ -450,17 +367,6 @@ ReclaimForOS(
> > > >    VOID
> > > >    );
> > > >
> > > > -/**
> > > > -  Get non-volatile maximum variable size.
> > > > -
> > > > -  @return Non-volatile maximum variable size.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -GetNonVolatileMaxVariableSize (
> > > > -  VOID
> > > > -  );
> > > > -
> > > >  /**
> > > >    Get maximum variable size, covering both non-volatile and volatile
> > > variables.
> > > >
> > > > @@ -546,31 +452,6 @@ VariableServiceGetVariable (
> > > >    OUT     VOID              *Data OPTIONAL
> > > >    );
> > > >
> > > > -/**
> > > > -  This code Finds the Next available variable.
> > > > -
> > > > -  Caution: This function may receive untrusted input.
> > > > -  This function may be invoked in SMM mode. This function will do
> basic
> > > > validation, before parse the data.
> > > > -
> > > > -  @param[in] VariableName   Pointer to variable name.
> > > > -  @param[in] VendorGuid     Variable Vendor Guid.
> > > > -  @param[out] VariablePtr   Pointer to variable header address.
> > > > -
> > > > -  @retval EFI_SUCCESS           The function completed successfully.
> > > > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > > > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > > string,
> > > > while VendorGuid is NULL.
> > > > -  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > and
> > > > VendorGuid are not a name and
> > > > -                                GUID of an existing variable.
> > > > -
> > > > -**/
> > > > -EFI_STATUS
> > > > -EFIAPI
> > > > -VariableServiceGetNextVariableInternal (
> > > > -  IN  CHAR16                *VariableName,
> > > > -  IN  EFI_GUID              *VendorGuid,
> > > > -  OUT VARIABLE_HEADER       **VariablePtr
> > > > -  );
> > > > -
> > > >  /**
> > > >
> > > >    This code Finds the Next available variable.
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > new file mode 100644
> > > > index 0000000000..9d77c4916c
> > > > --- /dev/null
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > @@ -0,0 +1,306 @@
> > > > +/** @file
> > > > +  Functions in this module are associated with variable parsing
> > operations
> > > > and
> > > > +  are intended to be usable across variable driver source files.
> > > > +
> > > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#ifndef _VARIABLE_PARSING_H_
> > > > +#define _VARIABLE_PARSING_H_
> > > > +
> > > > +#include <Guid/ImageAuthentication.h>
> > > > +#include "Variable.h"
> > > > +
> > > > +/**
> > > > +
> > > > +  This code checks if variable header is valid or not.
> > > > +
> > > > +  @param Variable           Pointer to the Variable Header.
> > > > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > > +
> > > > +  @retval TRUE              Variable header is valid.
> > > > +  @retval FALSE             Variable header is not valid.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +IsValidVariableHeader (
> > > > +  IN  VARIABLE_HEADER       *Variable,
> > > > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the current status of Variable Store.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @retval EfiRaw         Variable store status is raw.
> > > > +  @retval EfiValid       Variable store status is valid.
> > > > +  @retval EfiInvalid     Variable store status is invalid.
> > > > +
> > > > +**/
> > > > +VARIABLE_STORE_STATUS
> > > > +GetVariableStoreStatus (
> > > > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code gets the size of variable header.
> > > > +
> > > > +  @return Size of variable header in bytes in type UINTN.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +GetVariableHeaderSize (
> > > > +  VOID
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the size of name of variable.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return UINTN          Size of variable in bytes.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +NameSizeOfVariable (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code sets the size of name of variable.
> > > > +
> > > > +  @param[in] Variable   Pointer to the Variable Header.
> > > > +  @param[in] NameSize   Name size to set.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +SetNameSizeOfVariable (
> > > > +  IN VARIABLE_HEADER    *Variable,
> > > > +  IN UINTN              NameSize
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the size of variable data.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Size of variable in bytes.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +DataSizeOfVariable (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code sets the size of variable data.
> > > > +
> > > > +  @param[in] Variable   Pointer to the Variable Header.
> > > > +  @param[in] DataSize   Data size to set.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +SetDataSizeOfVariable (
> > > > +  IN VARIABLE_HEADER    *Variable,
> > > > +  IN UINTN              DataSize
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the variable name.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to Variable Name which is Unicode encoding.
> > > > +
> > > > +**/
> > > > +CHAR16 *
> > > > +GetVariableNamePtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code gets the pointer to the variable guid.
> > > > +
> > > > +  @param Variable   Pointer to the Variable Header.
> > > > +
> > > > +  @return A EFI_GUID* pointer to Vendor Guid.
> > > > +
> > > > +**/
> > > > +EFI_GUID *
> > > > +GetVendorGuidPtr (
> > > > +  IN VARIABLE_HEADER    *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the variable data.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to Variable Data.
> > > > +
> > > > +**/
> > > > +UINT8 *
> > > > +GetVariableDataPtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code gets the variable data offset related to variable header.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Variable Data offset.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +GetVariableDataOffset (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the next variable header.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to next variable header.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetNextVariablePtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  Gets the pointer to the first variable header in given variable store
> area.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @return Pointer to the first variable header.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetStartPointer (
> > > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > +  );
> > > > +
> > > > +/**
> > > > +
> > > > +  Gets the pointer to the end of the variable storage area.
> > > > +
> > > > +  This function gets pointer to the end of the variable storage
> > > > +  area, according to the input variable store header.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @return Pointer to the end of the variable storage area.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetEndPointer (
> > > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Compare two EFI_TIME data.
> > > > +
> > > > +
> > > > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > > > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > > > +
> > > > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +VariableCompareTimeStampInternal (
> > > > +  IN EFI_TIME               *FirstTime,
> > > > +  IN EFI_TIME               *SecondTime
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Find the variable in the specified variable store.
> > > > +
> > > > +  @param[in]       VariableName        Name of the variable to be found
> > > > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > > > +  @param[in]       IgnoreRtCheck       Ignore
> > > EFI_VARIABLE_RUNTIME_ACCESS
> > > > attribute
> > > > +                                       check at runtime when searching variable.
> > > > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > > contains Variable Information.
> > > > +
> > > > +  @retval          EFI_SUCCESS         Variable found successfully
> > > > +  @retval          EFI_NOT_FOUND       Variable not found
> > > > +**/
> > > > +EFI_STATUS
> > > > +FindVariableEx (
> > > > +  IN     CHAR16                  *VariableName,
> > > > +  IN     EFI_GUID                *VendorGuid,
> > > > +  IN     BOOLEAN                 IgnoreRtCheck,
> > > > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > > +  );
> > > > +
> > > > +/**
> > > > +  This code Finds the Next available variable.
> > > > +
> > > > +  Caution: This function may receive untrusted input.
> > > > +  This function may be invoked in SMM mode. This function will do
> basic
> > > > validation, before parse the data.
> > > > +
> > > > +  @param[in]  VariableName  Pointer to variable name.
> > > > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > > > +  @param[out] VariablePtr   Pointer to variable header address.
> > > > +
> > > > +  @retval EFI_SUCCESS           The function completed successfully.
> > > > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > > > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > > string,
> > > > while VendorGuid is NULL.
> > > > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > > and
> > > > VendorGuid are not a name and
> > > > +                                GUID of an existing variable.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +VariableServiceGetNextVariableInternal (
> > > > +  IN  CHAR16                *VariableName,
> > > > +  IN  EFI_GUID              *VendorGuid,
> > > > +  OUT VARIABLE_HEADER       **VariablePtr
> > > > +  );
> > > > +
> > > > +/**
> > > > +  Routine used to track statistical information about variable usage.
> > > > +  The data is stored in the EFI system table so it can be accessed later.
> > > > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > > +  build flag controls if this feature is enabled.
> > > > +
> > > > +  A read that hits in the cache will have Read and Cache true for
> > > > +  the transaction. Data is allocated by this routine, but never
> > > > +  freed.
> > > > +
> > > > +  @param[in] VariableName   Name of the Variable to track.
> > > > +  @param[in] VendorGuid     Guid of the Variable to track.
> > > > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > > +  @param[in] Read           TRUE if GetVariable() was called.
> > > > +  @param[in] Write          TRUE if SetVariable() was called.
> > > > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > > > +  @param[in] Cache          TRUE for a cache hit.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +UpdateVariableInfo (
> > > > +  IN  CHAR16                  *VariableName,
> > > > +  IN  EFI_GUID                *VendorGuid,
> > > > +  IN  BOOLEAN                 Volatile,
> > > > +  IN  BOOLEAN                 Read,
> > > > +  IN  BOOLEAN                 Write,
> > > > +  IN  BOOLEAN                 Delete,
> > > > +  IN  BOOLEAN                 Cache
> > > > +  );
> > > > +
> > > > +#endif
> > > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > index f32c9c2808..76536308e6 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >  **/
> > > >
> > > >  #include "Variable.h"
> > > > +#include "VariableParsing.h"
> > > >
> > > >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> > > >
> > > > @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn
> = {
> > > >
> > > >  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> > > >
> > > > -/**
> > > > -  Routine used to track statistical information about variable usage.
> > > > -  The data is stored in the EFI system table so it can be accessed later.
> > > > -  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > > -  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > > -  build flag controls if this feature is enabled.
> > > > -
> > > > -  A read that hits in the cache will have Read and Cache true for
> > > > -  the transaction. Data is allocated by this routine, but never
> > > > -  freed.
> > > > -
> > > > -  @param[in] VariableName   Name of the Variable to track.
> > > > -  @param[in] VendorGuid     Guid of the Variable to track.
> > > > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > > -  @param[in] Read           TRUE if GetVariable() was called.
> > > > -  @param[in] Write          TRUE if SetVariable() was called.
> > > > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > > > -  @param[in] Cache          TRUE for a cache hit.
> > > > -
> > > > -**/
> > > > -VOID
> > > > -UpdateVariableInfo (
> > > > -  IN  CHAR16                  *VariableName,
> > > > -  IN  EFI_GUID                *VendorGuid,
> > > > -  IN  BOOLEAN                 Volatile,
> > > > -  IN  BOOLEAN                 Read,
> > > > -  IN  BOOLEAN                 Write,
> > > > -  IN  BOOLEAN                 Delete,
> > > > -  IN  BOOLEAN                 Cache
> > > > -  )
> > > > -{
> > > > -  VARIABLE_INFO_ENTRY   *Entry;
> > > > -
> > > > -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > > -
> > > > -    if (AtRuntime ()) {
> > > > -      // Don't collect statistics at runtime.
> > > > -      return;
> > > > -    }
> > > > -
> > > > -    if (gVariableInfo == NULL) {
> > > > -      //
> > > > -      // On the first call allocate a entry and place a pointer to it in
> > > > -      // the EFI System Table.
> > > > -      //
> > > > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > > -      ASSERT (gVariableInfo != NULL);
> > > > -
> > > > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > > -      ASSERT (gVariableInfo->Name != NULL);
> > > > -      StrCpyS (gVariableInfo->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > -      gVariableInfo->Volatile = Volatile;
> > > > -    }
> > > > -
> > > > -
> > > > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > > -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > > > -        if (StrCmp (VariableName, Entry->Name) == 0) {
> > > > -          if (Read) {
> > > > -            Entry->ReadCount++;
> > > > -          }
> > > > -          if (Write) {
> > > > -            Entry->WriteCount++;
> > > > -          }
> > > > -          if (Delete) {
> > > > -            Entry->DeleteCount++;
> > > > -          }
> > > > -          if (Cache) {
> > > > -            Entry->CacheCount++;
> > > > -          }
> > > > -
> > > > -          return;
> > > > -        }
> > > > -      }
> > > > -
> > > > -      if (Entry->Next == NULL) {
> > > > -        //
> > > > -        // If the entry is not in the table add it.
> > > > -        // Next iteration of the loop will fill in the data.
> > > > -        //
> > > > -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > > -        ASSERT (Entry->Next != NULL);
> > > > -
> > > > -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > > > -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > > > -        ASSERT (Entry->Next->Name != NULL);
> > > > -        StrCpyS (Entry->Next->Name,
> > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > -        Entry->Next->Volatile = Volatile;
> > > > -      }
> > > > -
> > > > -    }
> > > > -  }
> > > > -}
> > > > -
> > > > -
> > > > -/**
> > > > -
> > > > -  This code checks if variable header is valid or not.
> > > > -
> > > > -  @param Variable           Pointer to the Variable Header.
> > > > -  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > > -
> > > > -  @retval TRUE              Variable header is valid.
> > > > -  @retval FALSE             Variable header is not valid.
> > > > -
> > > > -**/
> > > > -BOOLEAN
> > > > -IsValidVariableHeader (
> > > > -  IN  VARIABLE_HEADER       *Variable,
> > > > -  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > > -  )
> > > > -{
> > > > -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > > > >StartId != VARIABLE_DATA)) {
> > > > -    //
> > > > -    // Variable is NULL or has reached the end of variable store,
> > > > -    // or the StartId is not correct.
> > > > -    //
> > > > -    return FALSE;
> > > > -  }
> > > > -
> > > > -  return TRUE;
> > > > -}
> > > > -
> > > > -
> > > >  /**
> > > >
> > > >    This function writes data to the FWH at the correct LBA even if the
> LBAs
> > > > @@ -376,345 +252,6 @@ UpdateVariableStore (
> > > >    return EFI_SUCCESS;
> > > >  }
> > > >
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the current status of Variable Store.
> > > > -
> > > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > -
> > > > -  @retval EfiRaw         Variable store status is raw.
> > > > -  @retval EfiValid       Variable store status is valid.
> > > > -  @retval EfiInvalid     Variable store status is invalid.
> > > > -
> > > > -**/
> > > > -VARIABLE_STORE_STATUS
> > > > -GetVariableStoreStatus (
> > > > -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > > -  )
> > > > -{
> > > > -  if ((CompareGuid (&VarStoreHeader->Signature,
> > > > &gEfiAuthenticatedVariableGuid) ||
> > > > -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid))
> > &&
> > > > -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > > > -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > > > -      ) {
> > > > -
> > > > -    return EfiValid;
> > > > -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff
> &&
> > > > -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > > > -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > > > -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > > > -             VarStoreHeader->Size == 0xffffffff &&
> > > > -             VarStoreHeader->Format == 0xff &&
> > > > -             VarStoreHeader->State == 0xff
> > > > -          ) {
> > > > -
> > > > -    return EfiRaw;
> > > > -  } else {
> > > > -    return EfiInvalid;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -  This code gets the size of variable header.
> > > > -
> > > > -  @return Size of variable header in bytes in type UINTN.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -GetVariableHeaderSize (
> > > > -  VOID
> > > > -  )
> > > > -{
> > > > -  UINTN Value;
> > > > -
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > > -  } else {
> > > > -    Value = sizeof (VARIABLE_HEADER);
> > > > -  }
> > > > -
> > > > -  return Value;
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the size of name of variable.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return UINTN          Size of variable in bytes.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -NameSizeOfVariable (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > -
> > > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    if (AuthVariable->State == (UINT8) (-1) ||
> > > > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > > > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > > > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > > > -      return 0;
> > > > -    }
> > > > -    return (UINTN) AuthVariable->NameSize;
> > > > -  } else {
> > > > -    if (Variable->State == (UINT8) (-1) ||
> > > > -        Variable->DataSize == (UINT32) (-1) ||
> > > > -        Variable->NameSize == (UINT32) (-1) ||
> > > > -        Variable->Attributes == (UINT32) (-1)) {
> > > > -      return 0;
> > > > -    }
> > > > -    return (UINTN) Variable->NameSize;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -  This code sets the size of name of variable.
> > > > -
> > > > -  @param[in] Variable   Pointer to the Variable Header.
> > > > -  @param[in] NameSize   Name size to set.
> > > > -
> > > > -**/
> > > > -VOID
> > > > -SetNameSizeOfVariable (
> > > > -  IN VARIABLE_HEADER    *Variable,
> > > > -  IN UINTN              NameSize
> > > > -  )
> > > > -{
> > > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > -
> > > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    AuthVariable->NameSize = (UINT32) NameSize;
> > > > -  } else {
> > > > -    Variable->NameSize = (UINT32) NameSize;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the size of variable data.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Size of variable in bytes.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -DataSizeOfVariable (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > -
> > > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    if (AuthVariable->State == (UINT8) (-1) ||
> > > > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > > > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > > > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > > > -      return 0;
> > > > -    }
> > > > -    return (UINTN) AuthVariable->DataSize;
> > > > -  } else {
> > > > -    if (Variable->State == (UINT8) (-1) ||
> > > > -        Variable->DataSize == (UINT32) (-1) ||
> > > > -        Variable->NameSize == (UINT32) (-1) ||
> > > > -        Variable->Attributes == (UINT32) (-1)) {
> > > > -      return 0;
> > > > -    }
> > > > -    return (UINTN) Variable->DataSize;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -  This code sets the size of variable data.
> > > > -
> > > > -  @param[in] Variable   Pointer to the Variable Header.
> > > > -  @param[in] DataSize   Data size to set.
> > > > -
> > > > -**/
> > > > -VOID
> > > > -SetDataSizeOfVariable (
> > > > -  IN VARIABLE_HEADER    *Variable,
> > > > -  IN UINTN              DataSize
> > > > -  )
> > > > -{
> > > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > -
> > > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    AuthVariable->DataSize = (UINT32) DataSize;
> > > > -  } else {
> > > > -    Variable->DataSize = (UINT32) DataSize;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the pointer to the variable name.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Pointer to Variable Name which is Unicode encoding.
> > > > -
> > > > -**/
> > > > -CHAR16 *
> > > > -GetVariableNamePtr (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > > > -}
> > > > -
> > > > -/**
> > > > -  This code gets the pointer to the variable guid.
> > > > -
> > > > -  @param Variable   Pointer to the Variable Header.
> > > > -
> > > > -  @return A EFI_GUID* pointer to Vendor Guid.
> > > > -
> > > > -**/
> > > > -EFI_GUID *
> > > > -GetVendorGuidPtr (
> > > > -  IN VARIABLE_HEADER    *Variable
> > > > -  )
> > > > -{
> > > > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > -
> > > > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > -    return &AuthVariable->VendorGuid;
> > > > -  } else {
> > > > -    return &Variable->VendorGuid;
> > > > -  }
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the pointer to the variable data.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Pointer to Variable Data.
> > > > -
> > > > -**/
> > > > -UINT8 *
> > > > -GetVariableDataPtr (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  UINTN Value;
> > > > -
> > > > -  //
> > > > -  // Be careful about pad size for alignment.
> > > > -  //
> > > > -  Value =  (UINTN) GetVariableNamePtr (Variable);
> > > > -  Value += NameSizeOfVariable (Variable);
> > > > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > > -
> > > > -  return (UINT8 *) Value;
> > > > -}
> > > > -
> > > > -/**
> > > > -  This code gets the variable data offset related to variable header.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Variable Data offset.
> > > > -
> > > > -**/
> > > > -UINTN
> > > > -GetVariableDataOffset (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  UINTN Value;
> > > > -
> > > > -  //
> > > > -  // Be careful about pad size for alignment
> > > > -  //
> > > > -  Value = GetVariableHeaderSize ();
> > > > -  Value += NameSizeOfVariable (Variable);
> > > > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > > -
> > > > -  return Value;
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  This code gets the pointer to the next variable header.
> > > > -
> > > > -  @param Variable        Pointer to the Variable Header.
> > > > -
> > > > -  @return Pointer to next variable header.
> > > > -
> > > > -**/
> > > > -VARIABLE_HEADER *
> > > > -GetNextVariablePtr (
> > > > -  IN  VARIABLE_HEADER   *Variable
> > > > -  )
> > > > -{
> > > > -  UINTN Value;
> > > > -
> > > > -  Value =  (UINTN) GetVariableDataPtr (Variable);
> > > > -  Value += DataSizeOfVariable (Variable);
> > > > -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > > > -
> > > > -  //
> > > > -  // Be careful about pad size for alignment.
> > > > -  //
> > > > -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  Gets the pointer to the first variable header in given variable store
> area.
> > > > -
> > > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > -
> > > > -  @return Pointer to the first variable header.
> > > > -
> > > > -**/
> > > > -VARIABLE_HEADER *
> > > > -GetStartPointer (
> > > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > -  )
> > > > -{
> > > > -  //
> > > > -  // The start of variable store.
> > > > -  //
> > > > -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > > > -}
> > > > -
> > > > -/**
> > > > -
> > > > -  Gets the pointer to the end of the variable storage area.
> > > > -
> > > > -  This function gets pointer to the end of the variable storage
> > > > -  area, according to the input variable store header.
> > > > -
> > > > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > -
> > > > -  @return Pointer to the end of the variable storage area.
> > > > -
> > > > -**/
> > > > -VARIABLE_HEADER *
> > > > -GetEndPointer (
> > > > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > -  )
> > > > -{
> > > > -  //
> > > > -  // The end of variable store
> > > > -  //
> > > > -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN)
> > VarStoreHeader
> > > +
> > > > VarStoreHeader->Size);
> > > > -}
> > > > -
> > > >  /**
> > > >    Record variable error flag.
> > > >
> > > > @@ -1228,75 +765,6 @@ Done:
> > > >    return Status;
> > > >  }
> > > >
> > > > -/**
> > > > -  Find the variable in the specified variable store.
> > > > -
> > > > -  @param[in]       VariableName        Name of the variable to be found
> > > > -  @param[in]       VendorGuid          Vendor GUID to be found.
> > > > -  @param[in]       IgnoreRtCheck       Ignore
> > > EFI_VARIABLE_RUNTIME_ACCESS
> > > > attribute
> > > > -                                       check at runtime when searching variable.
> > > > -  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > > contains Variable Information.
> > > > -
> > > > -  @retval          EFI_SUCCESS         Variable found successfully
> > > > -  @retval          EFI_NOT_FOUND       Variable not found
> > > > -**/
> > > > -EFI_STATUS
> > > > -FindVariableEx (
> > > > -  IN     CHAR16                  *VariableName,
> > > > -  IN     EFI_GUID                *VendorGuid,
> > > > -  IN     BOOLEAN                 IgnoreRtCheck,
> > > > -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > > -  )
> > > > -{
> > > > -  VARIABLE_HEADER                *InDeletedVariable;
> > > > -  VOID                           *Point;
> > > > -
> > > > -  PtrTrack->InDeletedTransitionPtr = NULL;
> > > > -
> > > > -  //
> > > > -  // Find the variable by walk through HOB, volatile and non-volatile
> > > variable
> > > > store.
> > > > -  //
> > > > -  InDeletedVariable  = NULL;
> > > > -
> > > > -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > > > -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > > > -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > > > -      ) {
> > > > -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > > > -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > > VAR_ADDED)
> > > > -       ) {
> > > > -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr-
> >Attributes
> > &
> > > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > > -        if (VariableName[0] == 0) {
> > > > -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > > VAR_ADDED)) {
> > > > -            InDeletedVariable   = PtrTrack->CurrPtr;
> > > > -          } else {
> > > > -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > > -            return EFI_SUCCESS;
> > > > -          }
> > > > -        } else {
> > > > -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> > > >CurrPtr)))
> > > > {
> > > > -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > > > -
> > > > -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > > > -            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > > > (PtrTrack->CurrPtr)) == 0) {
> > > > -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION
> &
> > > > VAR_ADDED)) {
> > > > -                InDeletedVariable     = PtrTrack->CurrPtr;
> > > > -              } else {
> > > > -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > > -                return EFI_SUCCESS;
> > > > -              }
> > > > -            }
> > > > -          }
> > > > -        }
> > > > -      }
> > > > -    }
> > > > -  }
> > > > -
> > > > -  PtrTrack->CurrPtr = InDeletedVariable;
> > > > -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND :
> EFI_SUCCESS;
> > > > -}
> > > > -
> > > > -
> > > >  /**
> > > >    Finds variable in storage blocks of volatile and non-volatile storage
> areas.
> > > >
> > > > @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
> > > >    }
> > > >  }
> > > >
> > > > -/**
> > > > -  Compare two EFI_TIME data.
> > > > -
> > > > -
> > > > -  @param FirstTime           A pointer to the first EFI_TIME data.
> > > > -  @param SecondTime          A pointer to the second EFI_TIME data.
> > > > -
> > > > -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > > -  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > > -
> > > > -**/
> > > > -BOOLEAN
> > > > -VariableCompareTimeStampInternal (
> > > > -  IN EFI_TIME               *FirstTime,
> > > > -  IN EFI_TIME               *SecondTime
> > > > -  )
> > > > -{
> > > > -  if (FirstTime->Year != SecondTime->Year) {
> > > > -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > > > -  } else if (FirstTime->Month != SecondTime->Month) {
> > > > -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > > > -  } else if (FirstTime->Day != SecondTime->Day) {
> > > > -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > > > -  } else if (FirstTime->Hour != SecondTime->Hour) {
> > > > -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > > > -  } else if (FirstTime->Minute != SecondTime->Minute) {
> > > > -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > > > -  }
> > > > -
> > > > -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > > > -}
> > > > -
> > > >  /**
> > > >    Update the variable region with Variable information. If
> > > > EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
> > > >    index of associated public key is needed.
> > > > @@ -2885,166 +2321,6 @@ Done:
> > > >    return Status;
> > > >  }
> > > >
> > > > -/**
> > > > -  This code Finds the Next available variable.
> > > > -
> > > > -  Caution: This function may receive untrusted input.
> > > > -  This function may be invoked in SMM mode. This function will do
> basic
> > > > validation, before parse the data.
> > > > -
> > > > -  @param[in]  VariableName  Pointer to variable name.
> > > > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > > > -  @param[out] VariablePtr   Pointer to variable header address.
> > > > -
> > > > -  @retval EFI_SUCCESS           The function completed successfully.
> > > > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > > > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > > string,
> > > > while VendorGuid is NULL.
> > > > -  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > and
> > > > VendorGuid are not a name and
> > > > -                                GUID of an existing variable.
> > > > -
> > > > -**/
> > > > -EFI_STATUS
> > > > -EFIAPI
> > > > -VariableServiceGetNextVariableInternal (
> > > > -  IN  CHAR16                *VariableName,
> > > > -  IN  EFI_GUID              *VendorGuid,
> > > > -  OUT VARIABLE_HEADER       **VariablePtr
> > > > -  )
> > > > -{
> > > > -  VARIABLE_STORE_TYPE     Type;
> > > > -  VARIABLE_POINTER_TRACK  Variable;
> > > > -  VARIABLE_POINTER_TRACK  VariableInHob;
> > > > -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > > > -  EFI_STATUS              Status;
> > > > -  VARIABLE_STORE_HEADER
> > > *VariableStoreHeader[VariableStoreTypeMax];
> > > > -
> > > > -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > > > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > > > -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > > > -    //
> > > > -    // For VariableName is an empty string, FindVariable() will try to find
> > and
> > > > return
> > > > -    // the first qualified variable, and if FindVariable() returns error
> > > > (EFI_NOT_FOUND)
> > > > -    // as no any variable is found, still go to return the error
> > > > (EFI_NOT_FOUND).
> > > > -    //
> > > > -    if (VariableName[0] != 0) {
> > > > -      //
> > > > -      // For VariableName is not an empty string, and FindVariable()
> > returns
> > > > error as
> > > > -      // VariableName and VendorGuid are not a name and GUID of an
> > > existing
> > > > variable,
> > > > -      // there is no way to get next variable, follow spec to return
> > > > EFI_INVALID_PARAMETER.
> > > > -      //
> > > > -      Status = EFI_INVALID_PARAMETER;
> > > > -    }
> > > > -    goto Done;
> > > > -  }
> > > > -
> > > > -  if (VariableName[0] != 0) {
> > > > -    //
> > > > -    // If variable name is not NULL, get next variable.
> > > > -    //
> > > > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > -  }
> > > > -
> > > > -  //
> > > > -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > > -  // The index and attributes mapping must be kept in this order as
> > > > FindVariable
> > > > -  // makes use of this mapping to implement search algorithm.
> > > > -  //
> > > > -  VariableStoreHeader[VariableStoreTypeVolatile] =
> > > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.VolatileVariableBase;
> > > > -  VariableStoreHeader[VariableStoreTypeHob]      =
> > > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.HobVariableBase;
> > > > -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > > > -
> > > > -  while (TRUE) {
> > > > -    //
> > > > -    // Switch from Volatile to HOB, to Non-Volatile.
> > > > -    //
> > > > -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > > > -      //
> > > > -      // Find current storage index
> > > > -      //
> > > > -      for (Type = (VARIABLE_STORE_TYPE) 0; Type <
> VariableStoreTypeMax;
> > > > Type++) {
> > > > -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > > > GetStartPointer (VariableStoreHeader[Type]))) {
> > > > -          break;
> > > > -        }
> > > > -      }
> > > > -      ASSERT (Type < VariableStoreTypeMax);
> > > > -      //
> > > > -      // Switch to next storage
> > > > -      //
> > > > -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > > > -        if (VariableStoreHeader[Type] != NULL) {
> > > > -          break;
> > > > -        }
> > > > -      }
> > > > -      //
> > > > -      // Capture the case that
> > > > -      // 1. current storage is the last one, or
> > > > -      // 2. no further storage
> > > > -      //
> > > > -      if (Type == VariableStoreTypeMax) {
> > > > -        Status = EFI_NOT_FOUND;
> > > > -        goto Done;
> > > > -      }
> > > > -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > > > -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > > > -      Variable.CurrPtr  = Variable.StartPtr;
> > > > -    }
> > > > -
> > > > -    //
> > > > -    // Variable is found
> > > > -    //
> > > > -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr-
> >State
> > ==
> > > > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > > > -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > > -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > > VAR_ADDED)) {
> > > > -          //
> > > > -          // If it is a IN_DELETED_TRANSITION variable,
> > > > -          // and there is also a same ADDED one at the same time,
> > > > -          // don't return it.
> > > > -          //
> > > > -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > > > -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > > > -          Status = FindVariableEx (
> > > > -                     GetVariableNamePtr (Variable.CurrPtr),
> > > > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > > > -                     FALSE,
> > > > -                     &VariablePtrTrack
> > > > -                     );
> > > > -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > > > VAR_ADDED) {
> > > > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > -            continue;
> > > > -          }
> > > > -        }
> > > > -
> > > > -        //
> > > > -        // Don't return NV variable when HOB overrides it
> > > > -        //
> > > > -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > > > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > > > -            (Variable.StartPtr == GetStartPointer
> > > > (VariableStoreHeader[VariableStoreTypeNv]))
> > > > -           ) {
> > > > -          VariableInHob.StartPtr = GetStartPointer
> > > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > > -          VariableInHob.EndPtr   = GetEndPointer
> > > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > > -          Status = FindVariableEx (
> > > > -                     GetVariableNamePtr (Variable.CurrPtr),
> > > > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > > > -                     FALSE,
> > > > -                     &VariableInHob
> > > > -                     );
> > > > -          if (!EFI_ERROR (Status)) {
> > > > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > -            continue;
> > > > -          }
> > > > -        }
> > > > -
> > > > -        *VariablePtr = Variable.CurrPtr;
> > > > -        Status = EFI_SUCCESS;
> > > > -        goto Done;
> > > > -      }
> > > > -    }
> > > > -
> > > > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > -  }
> > > > -
> > > > -Done:
> > > > -  return Status;
> > > > -}
> > > > -
> > > >  /**
> > > >
> > > >    This code Finds the Next available variable.
> > > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > > index cb6fcebe2d..dc78f68fa9 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > > > @@ -1,12 +1,13 @@
> > > >  /** @file
> > > >    Provides variable driver extended services.
> > > >
> > > > -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > > > +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
> > > >  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >
> > > >  **/
> > > >
> > > >  #include "Variable.h"
> > > > +#include "VariableParsing.h"
> > > >
> > > >  /**
> > > >    Finds variable in storage blocks of volatile and non-volatile storage
> areas.
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > new file mode 100644
> > > > index 0000000000..7de0a90772
> > > > --- /dev/null
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > @@ -0,0 +1,731 @@
> > > > +/** @file
> > > > +  Functions in this module are associated with variable parsing
> > operations
> > > > and
> > > > +  are intended to be usable across variable driver source files.
> > > > +
> > > > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include "VariableParsing.h"
> > > > +
> > > > +/**
> > > > +
> > > > +  This code checks if variable header is valid or not.
> > > > +
> > > > +  @param Variable           Pointer to the Variable Header.
> > > > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > > > +
> > > > +  @retval TRUE              Variable header is valid.
> > > > +  @retval FALSE             Variable header is not valid.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +IsValidVariableHeader (
> > > > +  IN  VARIABLE_HEADER       *Variable,
> > > > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > > > +  )
> > > > +{
> > > > +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > > > >StartId != VARIABLE_DATA)) {
> > > > +    //
> > > > +    // Variable is NULL or has reached the end of variable store,
> > > > +    // or the StartId is not correct.
> > > > +    //
> > > > +    return FALSE;
> > > > +  }
> > > > +
> > > > +  return TRUE;
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the current status of Variable Store.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @retval EfiRaw         Variable store status is raw.
> > > > +  @retval EfiValid       Variable store status is valid.
> > > > +  @retval EfiInvalid     Variable store status is invalid.
> > > > +
> > > > +**/
> > > > +VARIABLE_STORE_STATUS
> > > > +GetVariableStoreStatus (
> > > > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > > > +  )
> > > > +{
> > > > +  if ((CompareGuid (&VarStoreHeader->Signature,
> > > > &gEfiAuthenticatedVariableGuid) ||
> > > > +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid))
> > &&
> > > > +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > > > +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > > > +      ) {
> > > > +
> > > > +    return EfiValid;
> > > > +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff
> &&
> > > > +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > > > +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > > > +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > > > +             VarStoreHeader->Size == 0xffffffff &&
> > > > +             VarStoreHeader->Format == 0xff &&
> > > > +             VarStoreHeader->State == 0xff
> > > > +          ) {
> > > > +
> > > > +    return EfiRaw;
> > > > +  } else {
> > > > +    return EfiInvalid;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code gets the size of variable header.
> > > > +
> > > > +  @return Size of variable header in bytes in type UINTN.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +GetVariableHeaderSize (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  UINTN Value;
> > > > +
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > > +  } else {
> > > > +    Value = sizeof (VARIABLE_HEADER);
> > > > +  }
> > > > +
> > > > +  return Value;
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the size of name of variable.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return UINTN          Size of variable in bytes.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +NameSizeOfVariable (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > +
> > > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    if (AuthVariable->State == (UINT8) (-1) ||
> > > > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > > > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > > > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > > > +      return 0;
> > > > +    }
> > > > +    return (UINTN) AuthVariable->NameSize;
> > > > +  } else {
> > > > +    if (Variable->State == (UINT8) (-1) ||
> > > > +        Variable->DataSize == (UINT32) (-1) ||
> > > > +        Variable->NameSize == (UINT32) (-1) ||
> > > > +        Variable->Attributes == (UINT32) (-1)) {
> > > > +      return 0;
> > > > +    }
> > > > +    return (UINTN) Variable->NameSize;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code sets the size of name of variable.
> > > > +
> > > > +  @param[in] Variable   Pointer to the Variable Header.
> > > > +  @param[in] NameSize   Name size to set.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +SetNameSizeOfVariable (
> > > > +  IN VARIABLE_HEADER    *Variable,
> > > > +  IN UINTN              NameSize
> > > > +  )
> > > > +{
> > > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > +
> > > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    AuthVariable->NameSize = (UINT32) NameSize;
> > > > +  } else {
> > > > +    Variable->NameSize = (UINT32) NameSize;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the size of variable data.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Size of variable in bytes.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +DataSizeOfVariable (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > +
> > > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    if (AuthVariable->State == (UINT8) (-1) ||
> > > > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > > > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > > > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > > > +      return 0;
> > > > +    }
> > > > +    return (UINTN) AuthVariable->DataSize;
> > > > +  } else {
> > > > +    if (Variable->State == (UINT8) (-1) ||
> > > > +        Variable->DataSize == (UINT32) (-1) ||
> > > > +        Variable->NameSize == (UINT32) (-1) ||
> > > > +        Variable->Attributes == (UINT32) (-1)) {
> > > > +      return 0;
> > > > +    }
> > > > +    return (UINTN) Variable->DataSize;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code sets the size of variable data.
> > > > +
> > > > +  @param[in] Variable   Pointer to the Variable Header.
> > > > +  @param[in] DataSize   Data size to set.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +SetDataSizeOfVariable (
> > > > +  IN VARIABLE_HEADER    *Variable,
> > > > +  IN UINTN              DataSize
> > > > +  )
> > > > +{
> > > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > +
> > > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    AuthVariable->DataSize = (UINT32) DataSize;
> > > > +  } else {
> > > > +    Variable->DataSize = (UINT32) DataSize;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the variable name.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to Variable Name which is Unicode encoding.
> > > > +
> > > > +**/
> > > > +CHAR16 *
> > > > +GetVariableNamePtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code gets the pointer to the variable guid.
> > > > +
> > > > +  @param Variable   Pointer to the Variable Header.
> > > > +
> > > > +  @return A EFI_GUID* pointer to Vendor Guid.
> > > > +
> > > > +**/
> > > > +EFI_GUID *
> > > > +GetVendorGuidPtr (
> > > > +  IN VARIABLE_HEADER    *Variable
> > > > +  )
> > > > +{
> > > > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > +
> > > > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +    return &AuthVariable->VendorGuid;
> > > > +  } else {
> > > > +    return &Variable->VendorGuid;
> > > > +  }
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the variable data.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to Variable Data.
> > > > +
> > > > +**/
> > > > +UINT8 *
> > > > +GetVariableDataPtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  UINTN Value;
> > > > +
> > > > +  //
> > > > +  // Be careful about pad size for alignment.
> > > > +  //
> > > > +  Value =  (UINTN) GetVariableNamePtr (Variable);
> > > > +  Value += NameSizeOfVariable (Variable);
> > > > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > > +
> > > > +  return (UINT8 *) Value;
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code gets the variable data offset related to variable header.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Variable Data offset.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +GetVariableDataOffset (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  UINTN Value;
> > > > +
> > > > +  //
> > > > +  // Be careful about pad size for alignment
> > > > +  //
> > > > +  Value = GetVariableHeaderSize ();
> > > > +  Value += NameSizeOfVariable (Variable);
> > > > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > > > +
> > > > +  return Value;
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  This code gets the pointer to the next variable header.
> > > > +
> > > > +  @param Variable        Pointer to the Variable Header.
> > > > +
> > > > +  @return Pointer to next variable header.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetNextVariablePtr (
> > > > +  IN  VARIABLE_HEADER   *Variable
> > > > +  )
> > > > +{
> > > > +  UINTN Value;
> > > > +
> > > > +  Value =  (UINTN) GetVariableDataPtr (Variable);
> > > > +  Value += DataSizeOfVariable (Variable);
> > > > +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > > > +
> > > > +  //
> > > > +  // Be careful about pad size for alignment.
> > > > +  //
> > > > +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  Gets the pointer to the first variable header in given variable store
> area.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @return Pointer to the first variable header.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetStartPointer (
> > > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > +  )
> > > > +{
> > > > +  //
> > > > +  // The start of variable store.
> > > > +  //
> > > > +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > > > +}
> > > > +
> > > > +/**
> > > > +
> > > > +  Gets the pointer to the end of the variable storage area.
> > > > +
> > > > +  This function gets pointer to the end of the variable storage
> > > > +  area, according to the input variable store header.
> > > > +
> > > > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > > > +
> > > > +  @return Pointer to the end of the variable storage area.
> > > > +
> > > > +**/
> > > > +VARIABLE_HEADER *
> > > > +GetEndPointer (
> > > > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > > > +  )
> > > > +{
> > > > +  //
> > > > +  // The end of variable store
> > > > +  //
> > > > +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN)
> > VarStoreHeader
> > > +
> > > > VarStoreHeader->Size);
> > > > +}
> > > > +
> > > > +/**
> > > > +  Compare two EFI_TIME data.
> > > > +
> > > > +
> > > > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > > > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > > > +
> > > > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > > > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +VariableCompareTimeStampInternal (
> > > > +  IN EFI_TIME               *FirstTime,
> > > > +  IN EFI_TIME               *SecondTime
> > > > +  )
> > > > +{
> > > > +  if (FirstTime->Year != SecondTime->Year) {
> > > > +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > > > +  } else if (FirstTime->Month != SecondTime->Month) {
> > > > +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > > > +  } else if (FirstTime->Day != SecondTime->Day) {
> > > > +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > > > +  } else if (FirstTime->Hour != SecondTime->Hour) {
> > > > +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > > > +  } else if (FirstTime->Minute != SecondTime->Minute) {
> > > > +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > > > +  }
> > > > +
> > > > +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > > > +}
> > > > +
> > > > +/**
> > > > +  Find the variable in the specified variable store.
> > > > +
> > > > +  @param[in]       VariableName        Name of the variable to be found
> > > > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > > > +  @param[in]       IgnoreRtCheck       Ignore
> > > EFI_VARIABLE_RUNTIME_ACCESS
> > > > attribute
> > > > +                                       check at runtime when searching variable.
> > > > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> > > > contains Variable Information.
> > > > +
> > > > +  @retval          EFI_SUCCESS         Variable found successfully
> > > > +  @retval          EFI_NOT_FOUND       Variable not found
> > > > +**/
> > > > +EFI_STATUS
> > > > +FindVariableEx (
> > > > +  IN     CHAR16                  *VariableName,
> > > > +  IN     EFI_GUID                *VendorGuid,
> > > > +  IN     BOOLEAN                 IgnoreRtCheck,
> > > > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > > > +  )
> > > > +{
> > > > +  VARIABLE_HEADER                *InDeletedVariable;
> > > > +  VOID                           *Point;
> > > > +
> > > > +  PtrTrack->InDeletedTransitionPtr = NULL;
> > > > +
> > > > +  //
> > > > +  // Find the variable by walk through HOB, volatile and non-volatile
> > > variable
> > > > store.
> > > > +  //
> > > > +  InDeletedVariable  = NULL;
> > > > +
> > > > +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > > > +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > > > +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > > > +      ) {
> > > > +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > > > +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > > VAR_ADDED)
> > > > +       ) {
> > > > +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr-
> >Attributes
> > > &
> > > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > > +        if (VariableName[0] == 0) {
> > > > +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION
> &
> > > > VAR_ADDED)) {
> > > > +            InDeletedVariable   = PtrTrack->CurrPtr;
> > > > +          } else {
> > > > +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > > +            return EFI_SUCCESS;
> > > > +          }
> > > > +        } else {
> > > > +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> > > >CurrPtr)))
> > > > {
> > > > +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > > > +
> > > > +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > > > +            if (CompareMem (VariableName, Point, NameSizeOfVariable
> > > > (PtrTrack->CurrPtr)) == 0) {
> > > > +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION
> > &
> > > > VAR_ADDED)) {
> > > > +                InDeletedVariable     = PtrTrack->CurrPtr;
> > > > +              } else {
> > > > +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > > > +                return EFI_SUCCESS;
> > > > +              }
> > > > +            }
> > > > +          }
> > > > +        }
> > > > +      }
> > > > +    }
> > > > +  }
> > > > +
> > > > +  PtrTrack->CurrPtr = InDeletedVariable;
> > > > +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND :
> EFI_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  This code Finds the Next available variable.
> > > > +
> > > > +  Caution: This function may receive untrusted input.
> > > > +  This function may be invoked in SMM mode. This function will do
> basic
> > > > validation, before parse the data.
> > > > +
> > > > +  @param[in]  VariableName  Pointer to variable name.
> > > > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > > > +  @param[out] VariablePtr   Pointer to variable header address.
> > > > +
> > > > +  @retval EFI_SUCCESS           The function completed successfully.
> > > > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > > > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> > > string,
> > > > while VendorGuid is NULL.
> > > > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> > > and
> > > > VendorGuid are not a name and
> > > > +                                GUID of an existing variable.
> > > > +
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +VariableServiceGetNextVariableInternal (
> > > > +  IN  CHAR16                *VariableName,
> > > > +  IN  EFI_GUID              *VendorGuid,
> > > > +  OUT VARIABLE_HEADER       **VariablePtr
> > > > +  )
> > > > +{
> > > > +  VARIABLE_STORE_TYPE     Type;
> > > > +  VARIABLE_POINTER_TRACK  Variable;
> > > > +  VARIABLE_POINTER_TRACK  VariableInHob;
> > > > +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > > > +  EFI_STATUS              Status;
> > > > +  VARIABLE_STORE_HEADER
> > > > *VariableStoreHeader[VariableStoreTypeMax];
> > > > +
> > > > +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > > > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > > > +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > > > +    //
> > > > +    // For VariableName is an empty string, FindVariable() will try to find
> > > and
> > > > return
> > > > +    // the first qualified variable, and if FindVariable() returns error
> > > > (EFI_NOT_FOUND)
> > > > +    // as no any variable is found, still go to return the error
> > > > (EFI_NOT_FOUND).
> > > > +    //
> > > > +    if (VariableName[0] != 0) {
> > > > +      //
> > > > +      // For VariableName is not an empty string, and FindVariable()
> > returns
> > > > error as
> > > > +      // VariableName and VendorGuid are not a name and GUID of an
> > > > existing variable,
> > > > +      // there is no way to get next variable, follow spec to return
> > > > EFI_INVALID_PARAMETER.
> > > > +      //
> > > > +      Status = EFI_INVALID_PARAMETER;
> > > > +    }
> > > > +    goto Done;
> > > > +  }
> > > > +
> > > > +  if (VariableName[0] != 0) {
> > > > +    //
> > > > +    // If variable name is not NULL, get next variable.
> > > > +    //
> > > > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > +  }
> > > > +
> > > > +  //
> > > > +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > > > +  // The index and attributes mapping must be kept in this order as
> > > > FindVariable
> > > > +  // makes use of this mapping to implement search algorithm.
> > > > +  //
> > > > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.VolatileVariableBase;
> > > > +  VariableStoreHeader[VariableStoreTypeHob]      =
> > > > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > > > >VariableGlobal.HobVariableBase;
> > > > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > > > +
> > > > +  while (TRUE) {
> > > > +    //
> > > > +    // Switch from Volatile to HOB, to Non-Volatile.
> > > > +    //
> > > > +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > > > +      //
> > > > +      // Find current storage index
> > > > +      //
> > > > +      for (Type = (VARIABLE_STORE_TYPE) 0; Type <
> > VariableStoreTypeMax;
> > > > Type++) {
> > > > +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > > > GetStartPointer (VariableStoreHeader[Type]))) {
> > > > +          break;
> > > > +        }
> > > > +      }
> > > > +      ASSERT (Type < VariableStoreTypeMax);
> > > > +      //
> > > > +      // Switch to next storage
> > > > +      //
> > > > +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > > > +        if (VariableStoreHeader[Type] != NULL) {
> > > > +          break;
> > > > +        }
> > > > +      }
> > > > +      //
> > > > +      // Capture the case that
> > > > +      // 1. current storage is the last one, or
> > > > +      // 2. no further storage
> > > > +      //
> > > > +      if (Type == VariableStoreTypeMax) {
> > > > +        Status = EFI_NOT_FOUND;
> > > > +        goto Done;
> > > > +      }
> > > > +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > > > +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > > > +      Variable.CurrPtr  = Variable.StartPtr;
> > > > +    }
> > > > +
> > > > +    //
> > > > +    // Variable is found
> > > > +    //
> > > > +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr-
> >State
> > > ==
> > > > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > > > +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > > > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > > > +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > > > VAR_ADDED)) {
> > > > +          //
> > > > +          // If it is a IN_DELETED_TRANSITION variable,
> > > > +          // and there is also a same ADDED one at the same time,
> > > > +          // don't return it.
> > > > +          //
> > > > +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > > > +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > > > +          Status = FindVariableEx (
> > > > +                     GetVariableNamePtr (Variable.CurrPtr),
> > > > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > > > +                     FALSE,
> > > > +                     &VariablePtrTrack
> > > > +                     );
> > > > +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > > > VAR_ADDED) {
> > > > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > +            continue;
> > > > +          }
> > > > +        }
> > > > +
> > > > +        //
> > > > +        // Don't return NV variable when HOB overrides it
> > > > +        //
> > > > +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > > > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > > > +            (Variable.StartPtr == GetStartPointer
> > > > (VariableStoreHeader[VariableStoreTypeNv]))
> > > > +           ) {
> > > > +          VariableInHob.StartPtr = GetStartPointer
> > > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > > +          VariableInHob.EndPtr   = GetEndPointer
> > > > (VariableStoreHeader[VariableStoreTypeHob]);
> > > > +          Status = FindVariableEx (
> > > > +                     GetVariableNamePtr (Variable.CurrPtr),
> > > > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > > > +                     FALSE,
> > > > +                     &VariableInHob
> > > > +                     );
> > > > +          if (!EFI_ERROR (Status)) {
> > > > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > +            continue;
> > > > +          }
> > > > +        }
> > > > +
> > > > +        *VariablePtr = Variable.CurrPtr;
> > > > +        Status = EFI_SUCCESS;
> > > > +        goto Done;
> > > > +      }
> > > > +    }
> > > > +
> > > > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > > > +  }
> > > > +
> > > > +Done:
> > > > +  return Status;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Routine used to track statistical information about variable usage.
> > > > +  The data is stored in the EFI system table so it can be accessed later.
> > > > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > > > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > > > +  build flag controls if this feature is enabled.
> > > > +
> > > > +  A read that hits in the cache will have Read and Cache true for
> > > > +  the transaction. Data is allocated by this routine, but never
> > > > +  freed.
> > > > +
> > > > +  @param[in] VariableName   Name of the Variable to track.
> > > > +  @param[in] VendorGuid     Guid of the Variable to track.
> > > > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > > > +  @param[in] Read           TRUE if GetVariable() was called.
> > > > +  @param[in] Write          TRUE if SetVariable() was called.
> > > > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > > > +  @param[in] Cache          TRUE for a cache hit.
> > > > +
> > > > +**/
> > > > +VOID
> > > > +UpdateVariableInfo (
> > > > +  IN  CHAR16                  *VariableName,
> > > > +  IN  EFI_GUID                *VendorGuid,
> > > > +  IN  BOOLEAN                 Volatile,
> > > > +  IN  BOOLEAN                 Read,
> > > > +  IN  BOOLEAN                 Write,
> > > > +  IN  BOOLEAN                 Delete,
> > > > +  IN  BOOLEAN                 Cache
> > > > +  )
> > > > +{
> > > > +  VARIABLE_INFO_ENTRY   *Entry;
> > > > +
> > > > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > > > +
> > > > +    if (AtRuntime ()) {
> > > > +      // Don't collect statistics at runtime.
> > > > +      return;
> > > > +    }
> > > > +
> > > > +    if (gVariableInfo == NULL) {
> > > > +      //
> > > > +      // On the first call allocate a entry and place a pointer to it in
> > > > +      // the EFI System Table.
> > > > +      //
> > > > +      gVariableInfo = AllocateZeroPool (sizeof
> (VARIABLE_INFO_ENTRY));
> > > > +      ASSERT (gVariableInfo != NULL);
> > > > +
> > > > +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > > > +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > > > +      ASSERT (gVariableInfo->Name != NULL);
> > > > +      StrCpyS (gVariableInfo->Name,
> > > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > +      gVariableInfo->Volatile = Volatile;
> > > > +    }
> > > > +
> > > > +
> > > > +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > > > +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > > > +        if (StrCmp (VariableName, Entry->Name) == 0) {
> > > > +          if (Read) {
> > > > +            Entry->ReadCount++;
> > > > +          }
> > > > +          if (Write) {
> > > > +            Entry->WriteCount++;
> > > > +          }
> > > > +          if (Delete) {
> > > > +            Entry->DeleteCount++;
> > > > +          }
> > > > +          if (Cache) {
> > > > +            Entry->CacheCount++;
> > > > +          }
> > > > +
> > > > +          return;
> > > > +        }
> > > > +      }
> > > > +
> > > > +      if (Entry->Next == NULL) {
> > > > +        //
> > > > +        // If the entry is not in the table add it.
> > > > +        // Next iteration of the loop will fill in the data.
> > > > +        //
> > > > +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > > > +        ASSERT (Entry->Next != NULL);
> > > > +
> > > > +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > > > +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > > > +        ASSERT (Entry->Next->Name != NULL);
> > > > +        StrCpyS (Entry->Next->Name,
> > > StrSize(VariableName)/sizeof(CHAR16),
> > > > VariableName);
> > > > +        Entry->Next->Volatile = Volatile;
> > > > +      }
> > > > +
> > > > +    }
> > > > +  }
> > > > +}
> > > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > > index ec463d063e..ce409f22a3 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > > > @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >
> > > >  #include <Guid/SmmVariableCommon.h>
> > > >  #include "Variable.h"
> > > > +#include "VariableParsing.h"
> > > >
> > > >  BOOLEAN                                              mAtRuntime              = FALSE;
> > > >  UINT8                                                *mVariableBufferPayload = NULL;
> > > > --
> > > > 2.16.2.windows.1
> > >
> >
> 


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

* Re: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions
  2019-10-08  6:07   ` Wang, Jian J
@ 2019-10-08 22:00     ` Kubacki, Michael A
  0 siblings, 0 replies; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-08 22:00 UTC (permalink / raw)
  To: Wang, Jian J, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wu, Hao A, Yao, Jiewen

Reply inline.

> -----Original Message-----
> From: Wang, Jian J <jian.j.wang@intel.com>
> Sent: Monday, October 7, 2019 11:08 PM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wu, Hao A
> <hao.a.wu@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing functions
> 
> Michael,
> 
> One comment below.
> 
> > -----Original Message-----
> > From: Kubacki, Michael A <michael.a.kubacki@intel.com>
> > Sent: Saturday, September 28, 2019 9:47 AM
> > To: devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael D
> > <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>; Yao, Jiewen
> > <jiewen.yao@intel.com>
> > Subject: [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common
> parsing
> > functions
> >
> > This change moves the following functions into a dedicated file
> > so they may be used in other variable files as needed. Furthermore,
> > it reduces the overall size of the common Variable.c file.
> >
> >  * DataSizeOfVariable ()
> >  * FindVariableEx ()
> >  * GetEndPointer ()
> >  * GetNextVariablePtr ()
> >  * GetStartPointer ()
> >  * GetVariableDataOffset ()
> >  * GetVariableDataPtr ()
> >  * GetVariableHeaderSize ()
> >  * GetVariableNamePtr ()
> >  * GetVariableStoreStatus ()
> >  * GetVendorGuidPtr ()
> >  * IsValidVariableHeader ()
> >  * NameSizeOfVariable ()
> >  * SetDataSizeOfVariable ()
> >  * SetNameSizeOfVariable ()
> >  * UpdateVariableInfo ()
> >  * VariableCompareTimeStampInternal ()
> >  * VariableServiceGetNextVariableInternal ()
> >
> > Cc: Dandan Bi <dandan.bi@intel.com>
> > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > Cc: Hao A Wu <hao.a.wu@intel.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> |   2
> > +
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf          |
> 2 +
> >
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf
> |
> > 7 +
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h               | 119 -
> ---
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h        |
> 306
> > ++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c               | 726
> +------
> > ------------
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c          |   3
> +-
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c        |
> 731
> > ++++++++++++++++++++
> >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c            |   1
> +
> >  9 files changed, 1052 insertions(+), 845 deletions(-)
> >
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > index 641376c9c5..c35e5fe787 100644
> > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
> > @@ -36,6 +36,8 @@
> >    Variable.c
> >    VariableDxe.c
> >    Variable.h
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    PrivilegePolymorphic.h
> >    Measurement.c
> >    TcgMorLockDxe.c
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > index 0a160d269d..626738b9c7 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableTraditionalMm.c
> >    VariableSmm.c
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > diff --git
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> > index 21bc81163b..1ba8f9ebfb 100644
> > ---
> >
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.i
> nf
> > +++
> >
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.
> inf
> > @@ -45,6 +45,8 @@
> >    Variable.c
> >    VariableSmm.c
> >    VariableStandaloneMm.c
> > +  VariableParsing.c
> > +  VariableParsing.h
> >    VarCheck.c
> >    Variable.h
> >    PrivilegePolymorphic.h
> > @@ -99,6 +101,11 @@
> >    ## SOMETIMES_PRODUCES   ## Variable:L"Lang"
> >    gEfiGlobalVariableGuid
> >
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"db"
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbx"
> > +  ## SOMETIMES_CONSUMES   ## Variable:L"dbt"
> > +  gEfiImageSecurityDatabaseGuid
> 
> Why add the guid here? It's only consumed by Measurement.c, which is not
> included
> in this inf file.
> 

It looks like it can be removed. I will do this in V3.

> > +
> >    gEfiMemoryOverwriteControlDataGuid            ## SOMETIMES_CONSUMES
> ##
> > Variable:L"MemoryOverwriteRequestControl"
> >    gEfiMemoryOverwriteRequestControlLockGuid     ##
> SOMETIMES_PRODUCES
> > ## Variable:L"MemoryOverwriteRequestControlLock"
> >
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > index 9eac43759f..fb574b2e32 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
> > @@ -179,89 +179,6 @@ FindVariable (
> >    IN  BOOLEAN                 IgnoreRtCheck
> >    );
> >
> > -/**
> > -
> > -  Gets the pointer to the end of the variable storage area.
> > -
> > -  This function gets pointer to the end of the variable storage
> > -  area, according to the input variable store header.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the end of the variable storage area.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetEndPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  );
> > -
> > -/**
> > -  This code gets the size of variable header.
> > -
> > -  @return Size of variable header in bytes in type UINTN.
> > -
> > -**/
> > -UINTN
> > -GetVariableHeaderSize (
> > -  VOID
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable name.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Name which is Unicode encoding.
> > -
> > -**/
> > -CHAR16 *
> > -GetVariableNamePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> > -/**
> > -  This code gets the pointer to the variable guid.
> > -
> > -  @param Variable   Pointer to the Variable Header.
> > -
> > -  @return A EFI_GUID* pointer to Vendor Guid.
> > -
> > -**/
> > -EFI_GUID *
> > -GetVendorGuidPtr (
> > -  IN VARIABLE_HEADER    *Variable
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Data.
> > -
> > -**/
> > -UINT8 *
> > -GetVariableDataPtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> > -/**
> > -
> > -  This code gets the size of variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -DataSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  );
> > -
> >  /**
> >    This function is to check if the remaining variable space is enough to set
> >    all Variables from argument list successfully. The purpose of the check
> > @@ -450,17 +367,6 @@ ReclaimForOS(
> >    VOID
> >    );
> >
> > -/**
> > -  Get non-volatile maximum variable size.
> > -
> > -  @return Non-volatile maximum variable size.
> > -
> > -**/
> > -UINTN
> > -GetNonVolatileMaxVariableSize (
> > -  VOID
> > -  );
> > -
> >  /**
> >    Get maximum variable size, covering both non-volatile and volatile
> variables.
> >
> > @@ -546,31 +452,6 @@ VariableServiceGetVariable (
> >    OUT     VOID              *Data OPTIONAL
> >    );
> >
> > -/**
> > -  This code Finds the Next available variable.
> > -
> > -  Caution: This function may receive untrusted input.
> > -  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > -
> > -  @param[in] VariableName   Pointer to variable name.
> > -  @param[in] VendorGuid     Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > -
> > -  @retval EFI_SUCCESS           The function completed successfully.
> > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> > VendorGuid are not a name and
> > -                                GUID of an existing variable.
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > -  IN  CHAR16                *VariableName,
> > -  IN  EFI_GUID              *VendorGuid,
> > -  OUT VARIABLE_HEADER       **VariablePtr
> > -  );
> > -
> >  /**
> >
> >    This code Finds the Next available variable.
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > new file mode 100644
> > index 0000000000..9d77c4916c
> > --- /dev/null
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > @@ -0,0 +1,306 @@
> > +/** @file
> > +  Functions in this module are associated with variable parsing operations
> and
> > +  are intended to be usable across variable driver source files.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#ifndef _VARIABLE_PARSING_H_
> > +#define _VARIABLE_PARSING_H_
> > +
> > +#include <Guid/ImageAuthentication.h>
> > +#include "Variable.h"
> > +
> > +/**
> > +
> > +  This code checks if variable header is valid or not.
> > +
> > +  @param Variable           Pointer to the Variable Header.
> > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > +
> > +  @retval TRUE              Variable header is valid.
> > +  @retval FALSE             Variable header is not valid.
> > +
> > +**/
> > +BOOLEAN
> > +IsValidVariableHeader (
> > +  IN  VARIABLE_HEADER       *Variable,
> > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the current status of Variable Store.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @retval EfiRaw         Variable store status is raw.
> > +  @retval EfiValid       Variable store status is valid.
> > +  @retval EfiInvalid     Variable store status is invalid.
> > +
> > +**/
> > +VARIABLE_STORE_STATUS
> > +GetVariableStoreStatus (
> > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > +  );
> > +
> > +/**
> > +  This code gets the size of variable header.
> > +
> > +  @return Size of variable header in bytes in type UINTN.
> > +
> > +**/
> > +UINTN
> > +GetVariableHeaderSize (
> > +  VOID
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the size of name of variable.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return UINTN          Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +NameSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code sets the size of name of variable.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] NameSize   Name size to set.
> > +
> > +**/
> > +VOID
> > +SetNameSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              NameSize
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the size of variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +DataSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code sets the size of variable data.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] DataSize   Data size to set.
> > +
> > +**/
> > +VOID
> > +SetDataSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              DataSize
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable name.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Name which is Unicode encoding.
> > +
> > +**/
> > +CHAR16 *
> > +GetVariableNamePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code gets the pointer to the variable guid.
> > +
> > +  @param Variable   Pointer to the Variable Header.
> > +
> > +  @return A EFI_GUID* pointer to Vendor Guid.
> > +
> > +**/
> > +EFI_GUID *
> > +GetVendorGuidPtr (
> > +  IN VARIABLE_HEADER    *Variable
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Data.
> > +
> > +**/
> > +UINT8 *
> > +GetVariableDataPtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +  This code gets the variable data offset related to variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Variable Data offset.
> > +
> > +**/
> > +UINTN
> > +GetVariableDataOffset (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +
> > +  This code gets the pointer to the next variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to next variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetNextVariablePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  );
> > +
> > +/**
> > +
> > +  Gets the pointer to the first variable header in given variable store area.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the first variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetStartPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  );
> > +
> > +/**
> > +
> > +  Gets the pointer to the end of the variable storage area.
> > +
> > +  This function gets pointer to the end of the variable storage
> > +  area, according to the input variable store header.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the end of the variable storage area.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetEndPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  );
> > +
> > +/**
> > +  Compare two EFI_TIME data.
> > +
> > +
> > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > +
> > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > +
> > +**/
> > +BOOLEAN
> > +VariableCompareTimeStampInternal (
> > +  IN EFI_TIME               *FirstTime,
> > +  IN EFI_TIME               *SecondTime
> > +  );
> > +
> > +/**
> > +  Find the variable in the specified variable store.
> > +
> > +  @param[in]       VariableName        Name of the variable to be found
> > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > +  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > +                                       check at runtime when searching variable.
> > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains
> > Variable Information.
> > +
> > +  @retval          EFI_SUCCESS         Variable found successfully
> > +  @retval          EFI_NOT_FOUND       Variable not found
> > +**/
> > +EFI_STATUS
> > +FindVariableEx (
> > +  IN     CHAR16                  *VariableName,
> > +  IN     EFI_GUID                *VendorGuid,
> > +  IN     BOOLEAN                 IgnoreRtCheck,
> > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > +  );
> > +
> > +/**
> > +  This code Finds the Next available variable.
> > +
> > +  Caution: This function may receive untrusted input.
> > +  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > +
> > +  @param[in]  VariableName  Pointer to variable name.
> > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > +  @param[out] VariablePtr   Pointer to variable header address.
> > +
> > +  @retval EFI_SUCCESS           The function completed successfully.
> > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > VendorGuid are not a name and
> > +                                GUID of an existing variable.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +VariableServiceGetNextVariableInternal (
> > +  IN  CHAR16                *VariableName,
> > +  IN  EFI_GUID              *VendorGuid,
> > +  OUT VARIABLE_HEADER       **VariablePtr
> > +  );
> > +
> > +/**
> > +  Routine used to track statistical information about variable usage.
> > +  The data is stored in the EFI system table so it can be accessed later.
> > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > +  build flag controls if this feature is enabled.
> > +
> > +  A read that hits in the cache will have Read and Cache true for
> > +  the transaction. Data is allocated by this routine, but never
> > +  freed.
> > +
> > +  @param[in] VariableName   Name of the Variable to track.
> > +  @param[in] VendorGuid     Guid of the Variable to track.
> > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in] Read           TRUE if GetVariable() was called.
> > +  @param[in] Write          TRUE if SetVariable() was called.
> > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > +  @param[in] Cache          TRUE for a cache hit.
> > +
> > +**/
> > +VOID
> > +UpdateVariableInfo (
> > +  IN  CHAR16                  *VariableName,
> > +  IN  EFI_GUID                *VendorGuid,
> > +  IN  BOOLEAN                 Volatile,
> > +  IN  BOOLEAN                 Read,
> > +  IN  BOOLEAN                 Write,
> > +  IN  BOOLEAN                 Delete,
> > +  IN  BOOLEAN                 Cache
> > +  );
> > +
> > +#endif
> > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > index f32c9c2808..76536308e6 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > @@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  **/
> >
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;
> >
> > @@ -92,131 +93,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {
> >
> >  AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;
> >
> > -/**
> > -  Routine used to track statistical information about variable usage.
> > -  The data is stored in the EFI system table so it can be accessed later.
> > -  VariableInfo.efi can dump out the table. Only Boot Services variable
> > -  accesses are tracked by this code. The PcdVariableCollectStatistics
> > -  build flag controls if this feature is enabled.
> > -
> > -  A read that hits in the cache will have Read and Cache true for
> > -  the transaction. Data is allocated by this routine, but never
> > -  freed.
> > -
> > -  @param[in] VariableName   Name of the Variable to track.
> > -  @param[in] VendorGuid     Guid of the Variable to track.
> > -  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > -  @param[in] Read           TRUE if GetVariable() was called.
> > -  @param[in] Write          TRUE if SetVariable() was called.
> > -  @param[in] Delete         TRUE if deleted via SetVariable().
> > -  @param[in] Cache          TRUE for a cache hit.
> > -
> > -**/
> > -VOID
> > -UpdateVariableInfo (
> > -  IN  CHAR16                  *VariableName,
> > -  IN  EFI_GUID                *VendorGuid,
> > -  IN  BOOLEAN                 Volatile,
> > -  IN  BOOLEAN                 Read,
> > -  IN  BOOLEAN                 Write,
> > -  IN  BOOLEAN                 Delete,
> > -  IN  BOOLEAN                 Cache
> > -  )
> > -{
> > -  VARIABLE_INFO_ENTRY   *Entry;
> > -
> > -  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > -
> > -    if (AtRuntime ()) {
> > -      // Don't collect statistics at runtime.
> > -      return;
> > -    }
> > -
> > -    if (gVariableInfo == NULL) {
> > -      //
> > -      // On the first call allocate a entry and place a pointer to it in
> > -      // the EFI System Table.
> > -      //
> > -      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > -      ASSERT (gVariableInfo != NULL);
> > -
> > -      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > -      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > -      ASSERT (gVariableInfo->Name != NULL);
> > -      StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > -      gVariableInfo->Volatile = Volatile;
> > -    }
> > -
> > -
> > -    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > -      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > -        if (StrCmp (VariableName, Entry->Name) == 0) {
> > -          if (Read) {
> > -            Entry->ReadCount++;
> > -          }
> > -          if (Write) {
> > -            Entry->WriteCount++;
> > -          }
> > -          if (Delete) {
> > -            Entry->DeleteCount++;
> > -          }
> > -          if (Cache) {
> > -            Entry->CacheCount++;
> > -          }
> > -
> > -          return;
> > -        }
> > -      }
> > -
> > -      if (Entry->Next == NULL) {
> > -        //
> > -        // If the entry is not in the table add it.
> > -        // Next iteration of the loop will fill in the data.
> > -        //
> > -        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > -        ASSERT (Entry->Next != NULL);
> > -
> > -        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > -        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > -        ASSERT (Entry->Next->Name != NULL);
> > -        StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > -        Entry->Next->Volatile = Volatile;
> > -      }
> > -
> > -    }
> > -  }
> > -}
> > -
> > -
> > -/**
> > -
> > -  This code checks if variable header is valid or not.
> > -
> > -  @param Variable           Pointer to the Variable Header.
> > -  @param VariableStoreEnd   Pointer to the Variable Store End.
> > -
> > -  @retval TRUE              Variable header is valid.
> > -  @retval FALSE             Variable header is not valid.
> > -
> > -**/
> > -BOOLEAN
> > -IsValidVariableHeader (
> > -  IN  VARIABLE_HEADER       *Variable,
> > -  IN  VARIABLE_HEADER       *VariableStoreEnd
> > -  )
> > -{
> > -  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> >StartId !=
> > VARIABLE_DATA)) {
> > -    //
> > -    // Variable is NULL or has reached the end of variable store,
> > -    // or the StartId is not correct.
> > -    //
> > -    return FALSE;
> > -  }
> > -
> > -  return TRUE;
> > -}
> > -
> > -
> >  /**
> >
> >    This function writes data to the FWH at the correct LBA even if the LBAs
> > @@ -376,345 +252,6 @@ UpdateVariableStore (
> >    return EFI_SUCCESS;
> >  }
> >
> > -
> > -/**
> > -
> > -  This code gets the current status of Variable Store.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @retval EfiRaw         Variable store status is raw.
> > -  @retval EfiValid       Variable store status is valid.
> > -  @retval EfiInvalid     Variable store status is invalid.
> > -
> > -**/
> > -VARIABLE_STORE_STATUS
> > -GetVariableStoreStatus (
> > -  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > -  )
> > -{
> > -  if ((CompareGuid (&VarStoreHeader->Signature,
> > &gEfiAuthenticatedVariableGuid) ||
> > -       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> > -      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > -      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > -      ) {
> > -
> > -    return EfiValid;
> > -  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > -             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > -             VarStoreHeader->Size == 0xffffffff &&
> > -             VarStoreHeader->Format == 0xff &&
> > -             VarStoreHeader->State == 0xff
> > -          ) {
> > -
> > -    return EfiRaw;
> > -  } else {
> > -    return EfiInvalid;
> > -  }
> > -}
> > -
> > -/**
> > -  This code gets the size of variable header.
> > -
> > -  @return Size of variable header in bytes in type UINTN.
> > -
> > -**/
> > -UINTN
> > -GetVariableHeaderSize (
> > -  VOID
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > -  } else {
> > -    Value = sizeof (VARIABLE_HEADER);
> > -  }
> > -
> > -  return Value;
> > -}
> > -
> > -/**
> > -
> > -  This code gets the size of name of variable.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return UINTN          Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -NameSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    if (AuthVariable->State == (UINT8) (-1) ||
> > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) AuthVariable->NameSize;
> > -  } else {
> > -    if (Variable->State == (UINT8) (-1) ||
> > -        Variable->DataSize == (UINT32) (-1) ||
> > -        Variable->NameSize == (UINT32) (-1) ||
> > -        Variable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) Variable->NameSize;
> > -  }
> > -}
> > -
> > -/**
> > -  This code sets the size of name of variable.
> > -
> > -  @param[in] Variable   Pointer to the Variable Header.
> > -  @param[in] NameSize   Name size to set.
> > -
> > -**/
> > -VOID
> > -SetNameSizeOfVariable (
> > -  IN VARIABLE_HEADER    *Variable,
> > -  IN UINTN              NameSize
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    AuthVariable->NameSize = (UINT32) NameSize;
> > -  } else {
> > -    Variable->NameSize = (UINT32) NameSize;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the size of variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Size of variable in bytes.
> > -
> > -**/
> > -UINTN
> > -DataSizeOfVariable (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    if (AuthVariable->State == (UINT8) (-1) ||
> > -       AuthVariable->DataSize == (UINT32) (-1) ||
> > -       AuthVariable->NameSize == (UINT32) (-1) ||
> > -       AuthVariable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) AuthVariable->DataSize;
> > -  } else {
> > -    if (Variable->State == (UINT8) (-1) ||
> > -        Variable->DataSize == (UINT32) (-1) ||
> > -        Variable->NameSize == (UINT32) (-1) ||
> > -        Variable->Attributes == (UINT32) (-1)) {
> > -      return 0;
> > -    }
> > -    return (UINTN) Variable->DataSize;
> > -  }
> > -}
> > -
> > -/**
> > -  This code sets the size of variable data.
> > -
> > -  @param[in] Variable   Pointer to the Variable Header.
> > -  @param[in] DataSize   Data size to set.
> > -
> > -**/
> > -VOID
> > -SetDataSizeOfVariable (
> > -  IN VARIABLE_HEADER    *Variable,
> > -  IN UINTN              DataSize
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    AuthVariable->DataSize = (UINT32) DataSize;
> > -  } else {
> > -    Variable->DataSize = (UINT32) DataSize;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable name.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Name which is Unicode encoding.
> > -
> > -**/
> > -CHAR16 *
> > -GetVariableNamePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > -}
> > -
> > -/**
> > -  This code gets the pointer to the variable guid.
> > -
> > -  @param Variable   Pointer to the Variable Header.
> > -
> > -  @return A EFI_GUID* pointer to Vendor Guid.
> > -
> > -**/
> > -EFI_GUID *
> > -GetVendorGuidPtr (
> > -  IN VARIABLE_HEADER    *Variable
> > -  )
> > -{
> > -  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > -
> > -  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > -    return &AuthVariable->VendorGuid;
> > -  } else {
> > -    return &Variable->VendorGuid;
> > -  }
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the variable data.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to Variable Data.
> > -
> > -**/
> > -UINT8 *
> > -GetVariableDataPtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  //
> > -  // Be careful about pad size for alignment.
> > -  //
> > -  Value =  (UINTN) GetVariableNamePtr (Variable);
> > -  Value += NameSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > -
> > -  return (UINT8 *) Value;
> > -}
> > -
> > -/**
> > -  This code gets the variable data offset related to variable header.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Variable Data offset.
> > -
> > -**/
> > -UINTN
> > -GetVariableDataOffset (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  //
> > -  // Be careful about pad size for alignment
> > -  //
> > -  Value = GetVariableHeaderSize ();
> > -  Value += NameSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > -
> > -  return Value;
> > -}
> > -
> > -/**
> > -
> > -  This code gets the pointer to the next variable header.
> > -
> > -  @param Variable        Pointer to the Variable Header.
> > -
> > -  @return Pointer to next variable header.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetNextVariablePtr (
> > -  IN  VARIABLE_HEADER   *Variable
> > -  )
> > -{
> > -  UINTN Value;
> > -
> > -  Value =  (UINTN) GetVariableDataPtr (Variable);
> > -  Value += DataSizeOfVariable (Variable);
> > -  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > -
> > -  //
> > -  // Be careful about pad size for alignment.
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > -}
> > -
> > -/**
> > -
> > -  Gets the pointer to the first variable header in given variable store area.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the first variable header.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetStartPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  )
> > -{
> > -  //
> > -  // The start of variable store.
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > -}
> > -
> > -/**
> > -
> > -  Gets the pointer to the end of the variable storage area.
> > -
> > -  This function gets pointer to the end of the variable storage
> > -  area, according to the input variable store header.
> > -
> > -  @param VarStoreHeader  Pointer to the Variable Store Header.
> > -
> > -  @return Pointer to the end of the variable storage area.
> > -
> > -**/
> > -VARIABLE_HEADER *
> > -GetEndPointer (
> > -  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > -  )
> > -{
> > -  //
> > -  // The end of variable store
> > -  //
> > -  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader
> +
> > VarStoreHeader->Size);
> > -}
> > -
> >  /**
> >    Record variable error flag.
> >
> > @@ -1228,75 +765,6 @@ Done:
> >    return Status;
> >  }
> >
> > -/**
> > -  Find the variable in the specified variable store.
> > -
> > -  @param[in]       VariableName        Name of the variable to be found
> > -  @param[in]       VendorGuid          Vendor GUID to be found.
> > -  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > -                                       check at runtime when searching variable.
> > -  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains
> > Variable Information.
> > -
> > -  @retval          EFI_SUCCESS         Variable found successfully
> > -  @retval          EFI_NOT_FOUND       Variable not found
> > -**/
> > -EFI_STATUS
> > -FindVariableEx (
> > -  IN     CHAR16                  *VariableName,
> > -  IN     EFI_GUID                *VendorGuid,
> > -  IN     BOOLEAN                 IgnoreRtCheck,
> > -  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > -  )
> > -{
> > -  VARIABLE_HEADER                *InDeletedVariable;
> > -  VOID                           *Point;
> > -
> > -  PtrTrack->InDeletedTransitionPtr = NULL;
> > -
> > -  //
> > -  // Find the variable by walk through HOB, volatile and non-volatile
> variable
> > store.
> > -  //
> > -  InDeletedVariable  = NULL;
> > -
> > -  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > -      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > -      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > -      ) {
> > -    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > -        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)
> > -       ) {
> > -      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > -        if (VariableName[0] == 0) {
> > -          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -            InDeletedVariable   = PtrTrack->CurrPtr;
> > -          } else {
> > -            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > -            return EFI_SUCCESS;
> > -          }
> > -        } else {
> > -          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> >CurrPtr))) {
> > -            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > -
> > -            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > -            if (CompareMem (VariableName, Point, NameSizeOfVariable
> (PtrTrack-
> > >CurrPtr)) == 0) {
> > -              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -                InDeletedVariable     = PtrTrack->CurrPtr;
> > -              } else {
> > -                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > -                return EFI_SUCCESS;
> > -              }
> > -            }
> > -          }
> > -        }
> > -      }
> > -    }
> > -  }
> > -
> > -  PtrTrack->CurrPtr = InDeletedVariable;
> > -  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > -}
> > -
> > -
> >  /**
> >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> >
> > @@ -2078,38 +1546,6 @@ AutoUpdateLangVariable (
> >    }
> >  }
> >
> > -/**
> > -  Compare two EFI_TIME data.
> > -
> > -
> > -  @param FirstTime           A pointer to the first EFI_TIME data.
> > -  @param SecondTime          A pointer to the second EFI_TIME data.
> > -
> > -  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > -  @retval  FALSE             The FirstTime is later than the SecondTime.
> > -
> > -**/
> > -BOOLEAN
> > -VariableCompareTimeStampInternal (
> > -  IN EFI_TIME               *FirstTime,
> > -  IN EFI_TIME               *SecondTime
> > -  )
> > -{
> > -  if (FirstTime->Year != SecondTime->Year) {
> > -    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > -  } else if (FirstTime->Month != SecondTime->Month) {
> > -    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > -  } else if (FirstTime->Day != SecondTime->Day) {
> > -    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > -  } else if (FirstTime->Hour != SecondTime->Hour) {
> > -    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > -  } else if (FirstTime->Minute != SecondTime->Minute) {
> > -    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > -  }
> > -
> > -  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > -}
> > -
> >  /**
> >    Update the variable region with Variable information. If
> > EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,
> >    index of associated public key is needed.
> > @@ -2885,166 +2321,6 @@ Done:
> >    return Status;
> >  }
> >
> > -/**
> > -  This code Finds the Next available variable.
> > -
> > -  Caution: This function may receive untrusted input.
> > -  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > -
> > -  @param[in]  VariableName  Pointer to variable name.
> > -  @param[in]  VendorGuid    Variable Vendor Guid.
> > -  @param[out] VariablePtr   Pointer to variable header address.
> > -
> > -  @retval EFI_SUCCESS           The function completed successfully.
> > -  @retval EFI_NOT_FOUND         The next variable was not found.
> > -  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > -  @retval EFI_INVALID_PARAMETER The input values of VariableName and
> > VendorGuid are not a name and
> > -                                GUID of an existing variable.
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -VariableServiceGetNextVariableInternal (
> > -  IN  CHAR16                *VariableName,
> > -  IN  EFI_GUID              *VendorGuid,
> > -  OUT VARIABLE_HEADER       **VariablePtr
> > -  )
> > -{
> > -  VARIABLE_STORE_TYPE     Type;
> > -  VARIABLE_POINTER_TRACK  Variable;
> > -  VARIABLE_POINTER_TRACK  VariableInHob;
> > -  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > -  EFI_STATUS              Status;
> > -  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> > -
> > -  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > -  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > -    //
> > -    // For VariableName is an empty string, FindVariable() will try to find and
> > return
> > -    // the first qualified variable, and if FindVariable() returns error
> > (EFI_NOT_FOUND)
> > -    // as no any variable is found, still go to return the error
> (EFI_NOT_FOUND).
> > -    //
> > -    if (VariableName[0] != 0) {
> > -      //
> > -      // For VariableName is not an empty string, and FindVariable() returns
> error
> > as
> > -      // VariableName and VendorGuid are not a name and GUID of an
> existing
> > variable,
> > -      // there is no way to get next variable, follow spec to return
> > EFI_INVALID_PARAMETER.
> > -      //
> > -      Status = EFI_INVALID_PARAMETER;
> > -    }
> > -    goto Done;
> > -  }
> > -
> > -  if (VariableName[0] != 0) {
> > -    //
> > -    // If variable name is not NULL, get next variable.
> > -    //
> > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -  }
> > -
> > -  //
> > -  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > -  // The index and attributes mapping must be kept in this order as
> FindVariable
> > -  // makes use of this mapping to implement search algorithm.
> > -  //
> > -  VariableStoreHeader[VariableStoreTypeVolatile] =
> (VARIABLE_STORE_HEADER
> > *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER
> > *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > -  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > -
> > -  while (TRUE) {
> > -    //
> > -    // Switch from Volatile to HOB, to Non-Volatile.
> > -    //
> > -    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > -      //
> > -      // Find current storage index
> > -      //
> > -      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > Type++) {
> > -        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > GetStartPointer (VariableStoreHeader[Type]))) {
> > -          break;
> > -        }
> > -      }
> > -      ASSERT (Type < VariableStoreTypeMax);
> > -      //
> > -      // Switch to next storage
> > -      //
> > -      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > -        if (VariableStoreHeader[Type] != NULL) {
> > -          break;
> > -        }
> > -      }
> > -      //
> > -      // Capture the case that
> > -      // 1. current storage is the last one, or
> > -      // 2. no further storage
> > -      //
> > -      if (Type == VariableStoreTypeMax) {
> > -        Status = EFI_NOT_FOUND;
> > -        goto Done;
> > -      }
> > -      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > -      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > -      Variable.CurrPtr  = Variable.StartPtr;
> > -    }
> > -
> > -    //
> > -    // Variable is found
> > -    //
> > -    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State ==
> > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > -      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > -        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > -          //
> > -          // If it is a IN_DELETED_TRANSITION variable,
> > -          // and there is also a same ADDED one at the same time,
> > -          // don't return it.
> > -          //
> > -          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > -          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > -          Status = FindVariableEx (
> > -                     GetVariableNamePtr (Variable.CurrPtr),
> > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > -                     FALSE,
> > -                     &VariablePtrTrack
> > -                     );
> > -          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > VAR_ADDED) {
> > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -            continue;
> > -          }
> > -        }
> > -
> > -        //
> > -        // Don't return NV variable when HOB overrides it
> > -        //
> > -        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > -            (Variable.StartPtr == GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeNv]))
> > -           ) {
> > -          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > -          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > -          Status = FindVariableEx (
> > -                     GetVariableNamePtr (Variable.CurrPtr),
> > -                     GetVendorGuidPtr (Variable.CurrPtr),
> > -                     FALSE,
> > -                     &VariableInHob
> > -                     );
> > -          if (!EFI_ERROR (Status)) {
> > -            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -            continue;
> > -          }
> > -        }
> > -
> > -        *VariablePtr = Variable.CurrPtr;
> > -        Status = EFI_SUCCESS;
> > -        goto Done;
> > -      }
> > -    }
> > -
> > -    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > -  }
> > -
> > -Done:
> > -  return Status;
> > -}
> > -
> >  /**
> >
> >    This code Finds the Next available variable.
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > index cb6fcebe2d..dc78f68fa9 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
> > @@ -1,12 +1,13 @@
> >  /** @file
> >    Provides variable driver extended services.
> >
> > -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  **/
> >
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  /**
> >    Finds variable in storage blocks of volatile and non-volatile storage areas.
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > new file mode 100644
> > index 0000000000..7de0a90772
> > --- /dev/null
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > @@ -0,0 +1,731 @@
> > +/** @file
> > +  Functions in this module are associated with variable parsing operations
> and
> > +  are intended to be usable across variable driver source files.
> > +
> > +Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include "VariableParsing.h"
> > +
> > +/**
> > +
> > +  This code checks if variable header is valid or not.
> > +
> > +  @param Variable           Pointer to the Variable Header.
> > +  @param VariableStoreEnd   Pointer to the Variable Store End.
> > +
> > +  @retval TRUE              Variable header is valid.
> > +  @retval FALSE             Variable header is not valid.
> > +
> > +**/
> > +BOOLEAN
> > +IsValidVariableHeader (
> > +  IN  VARIABLE_HEADER       *Variable,
> > +  IN  VARIABLE_HEADER       *VariableStoreEnd
> > +  )
> > +{
> > +  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable-
> > >StartId != VARIABLE_DATA)) {
> > +    //
> > +    // Variable is NULL or has reached the end of variable store,
> > +    // or the StartId is not correct.
> > +    //
> > +    return FALSE;
> > +  }
> > +
> > +  return TRUE;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the current status of Variable Store.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @retval EfiRaw         Variable store status is raw.
> > +  @retval EfiValid       Variable store status is valid.
> > +  @retval EfiInvalid     Variable store status is invalid.
> > +
> > +**/
> > +VARIABLE_STORE_STATUS
> > +GetVariableStoreStatus (
> > +  IN VARIABLE_STORE_HEADER *VarStoreHeader
> > +  )
> > +{
> > +  if ((CompareGuid (&VarStoreHeader->Signature,
> > &gEfiAuthenticatedVariableGuid) ||
> > +       CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&
> > +      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
> > +      VarStoreHeader->State == VARIABLE_STORE_HEALTHY
> > +      ) {
> > +
> > +    return EfiValid;
> > +  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
> > +             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
> > +             VarStoreHeader->Size == 0xffffffff &&
> > +             VarStoreHeader->Format == 0xff &&
> > +             VarStoreHeader->State == 0xff
> > +          ) {
> > +
> > +    return EfiRaw;
> > +  } else {
> > +    return EfiInvalid;
> > +  }
> > +}
> > +
> > +/**
> > +  This code gets the size of variable header.
> > +
> > +  @return Size of variable header in bytes in type UINTN.
> > +
> > +**/
> > +UINTN
> > +GetVariableHeaderSize (
> > +  VOID
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > +  } else {
> > +    Value = sizeof (VARIABLE_HEADER);
> > +  }
> > +
> > +  return Value;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the size of name of variable.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return UINTN          Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +NameSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    if (AuthVariable->State == (UINT8) (-1) ||
> > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) AuthVariable->NameSize;
> > +  } else {
> > +    if (Variable->State == (UINT8) (-1) ||
> > +        Variable->DataSize == (UINT32) (-1) ||
> > +        Variable->NameSize == (UINT32) (-1) ||
> > +        Variable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) Variable->NameSize;
> > +  }
> > +}
> > +
> > +/**
> > +  This code sets the size of name of variable.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] NameSize   Name size to set.
> > +
> > +**/
> > +VOID
> > +SetNameSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              NameSize
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    AuthVariable->NameSize = (UINT32) NameSize;
> > +  } else {
> > +    Variable->NameSize = (UINT32) NameSize;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the size of variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Size of variable in bytes.
> > +
> > +**/
> > +UINTN
> > +DataSizeOfVariable (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    if (AuthVariable->State == (UINT8) (-1) ||
> > +       AuthVariable->DataSize == (UINT32) (-1) ||
> > +       AuthVariable->NameSize == (UINT32) (-1) ||
> > +       AuthVariable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) AuthVariable->DataSize;
> > +  } else {
> > +    if (Variable->State == (UINT8) (-1) ||
> > +        Variable->DataSize == (UINT32) (-1) ||
> > +        Variable->NameSize == (UINT32) (-1) ||
> > +        Variable->Attributes == (UINT32) (-1)) {
> > +      return 0;
> > +    }
> > +    return (UINTN) Variable->DataSize;
> > +  }
> > +}
> > +
> > +/**
> > +  This code sets the size of variable data.
> > +
> > +  @param[in] Variable   Pointer to the Variable Header.
> > +  @param[in] DataSize   Data size to set.
> > +
> > +**/
> > +VOID
> > +SetDataSizeOfVariable (
> > +  IN VARIABLE_HEADER    *Variable,
> > +  IN UINTN              DataSize
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    AuthVariable->DataSize = (UINT32) DataSize;
> > +  } else {
> > +    Variable->DataSize = (UINT32) DataSize;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable name.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Name which is Unicode encoding.
> > +
> > +**/
> > +CHAR16 *
> > +GetVariableNamePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());
> > +}
> > +
> > +/**
> > +  This code gets the pointer to the variable guid.
> > +
> > +  @param Variable   Pointer to the Variable Header.
> > +
> > +  @return A EFI_GUID* pointer to Vendor Guid.
> > +
> > +**/
> > +EFI_GUID *
> > +GetVendorGuidPtr (
> > +  IN VARIABLE_HEADER    *Variable
> > +  )
> > +{
> > +  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > +
> > +  AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > +  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > +    return &AuthVariable->VendorGuid;
> > +  } else {
> > +    return &Variable->VendorGuid;
> > +  }
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the variable data.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to Variable Data.
> > +
> > +**/
> > +UINT8 *
> > +GetVariableDataPtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  //
> > +  // Be careful about pad size for alignment.
> > +  //
> > +  Value =  (UINTN) GetVariableNamePtr (Variable);
> > +  Value += NameSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > +
> > +  return (UINT8 *) Value;
> > +}
> > +
> > +/**
> > +  This code gets the variable data offset related to variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Variable Data offset.
> > +
> > +**/
> > +UINTN
> > +GetVariableDataOffset (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  //
> > +  // Be careful about pad size for alignment
> > +  //
> > +  Value = GetVariableHeaderSize ();
> > +  Value += NameSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));
> > +
> > +  return Value;
> > +}
> > +
> > +/**
> > +
> > +  This code gets the pointer to the next variable header.
> > +
> > +  @param Variable        Pointer to the Variable Header.
> > +
> > +  @return Pointer to next variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetNextVariablePtr (
> > +  IN  VARIABLE_HEADER   *Variable
> > +  )
> > +{
> > +  UINTN Value;
> > +
> > +  Value =  (UINTN) GetVariableDataPtr (Variable);
> > +  Value += DataSizeOfVariable (Variable);
> > +  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));
> > +
> > +  //
> > +  // Be careful about pad size for alignment.
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);
> > +}
> > +
> > +/**
> > +
> > +  Gets the pointer to the first variable header in given variable store area.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the first variable header.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetStartPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  )
> > +{
> > +  //
> > +  // The start of variable store.
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);
> > +}
> > +
> > +/**
> > +
> > +  Gets the pointer to the end of the variable storage area.
> > +
> > +  This function gets pointer to the end of the variable storage
> > +  area, according to the input variable store header.
> > +
> > +  @param VarStoreHeader  Pointer to the Variable Store Header.
> > +
> > +  @return Pointer to the end of the variable storage area.
> > +
> > +**/
> > +VARIABLE_HEADER *
> > +GetEndPointer (
> > +  IN VARIABLE_STORE_HEADER       *VarStoreHeader
> > +  )
> > +{
> > +  //
> > +  // The end of variable store
> > +  //
> > +  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader
> +
> > VarStoreHeader->Size);
> > +}
> > +
> > +/**
> > +  Compare two EFI_TIME data.
> > +
> > +
> > +  @param FirstTime           A pointer to the first EFI_TIME data.
> > +  @param SecondTime          A pointer to the second EFI_TIME data.
> > +
> > +  @retval  TRUE              The FirstTime is not later than the SecondTime.
> > +  @retval  FALSE             The FirstTime is later than the SecondTime.
> > +
> > +**/
> > +BOOLEAN
> > +VariableCompareTimeStampInternal (
> > +  IN EFI_TIME               *FirstTime,
> > +  IN EFI_TIME               *SecondTime
> > +  )
> > +{
> > +  if (FirstTime->Year != SecondTime->Year) {
> > +    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);
> > +  } else if (FirstTime->Month != SecondTime->Month) {
> > +    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);
> > +  } else if (FirstTime->Day != SecondTime->Day) {
> > +    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);
> > +  } else if (FirstTime->Hour != SecondTime->Hour) {
> > +    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);
> > +  } else if (FirstTime->Minute != SecondTime->Minute) {
> > +    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);
> > +  }
> > +
> > +  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);
> > +}
> > +
> > +/**
> > +  Find the variable in the specified variable store.
> > +
> > +  @param[in]       VariableName        Name of the variable to be found
> > +  @param[in]       VendorGuid          Vendor GUID to be found.
> > +  @param[in]       IgnoreRtCheck       Ignore
> EFI_VARIABLE_RUNTIME_ACCESS
> > attribute
> > +                                       check at runtime when searching variable.
> > +  @param[in, out]  PtrTrack            Variable Track Pointer structure that
> contains
> > Variable Information.
> > +
> > +  @retval          EFI_SUCCESS         Variable found successfully
> > +  @retval          EFI_NOT_FOUND       Variable not found
> > +**/
> > +EFI_STATUS
> > +FindVariableEx (
> > +  IN     CHAR16                  *VariableName,
> > +  IN     EFI_GUID                *VendorGuid,
> > +  IN     BOOLEAN                 IgnoreRtCheck,
> > +  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack
> > +  )
> > +{
> > +  VARIABLE_HEADER                *InDeletedVariable;
> > +  VOID                           *Point;
> > +
> > +  PtrTrack->InDeletedTransitionPtr = NULL;
> > +
> > +  //
> > +  // Find the variable by walk through HOB, volatile and non-volatile
> variable
> > store.
> > +  //
> > +  InDeletedVariable  = NULL;
> > +
> > +  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr
> > +      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)
> > +      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)
> > +      ) {
> > +    if (PtrTrack->CurrPtr->State == VAR_ADDED ||
> > +        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)
> > +       ) {
> > +      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes
> &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > +        if (VariableName[0] == 0) {
> > +          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +            InDeletedVariable   = PtrTrack->CurrPtr;
> > +          } else {
> > +            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > +            return EFI_SUCCESS;
> > +          }
> > +        } else {
> > +          if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack-
> >CurrPtr))) {
> > +            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);
> > +
> > +            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);
> > +            if (CompareMem (VariableName, Point, NameSizeOfVariable
> (PtrTrack-
> > >CurrPtr)) == 0) {
> > +              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +                InDeletedVariable     = PtrTrack->CurrPtr;
> > +              } else {
> > +                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;
> > +                return EFI_SUCCESS;
> > +              }
> > +            }
> > +          }
> > +        }
> > +      }
> > +    }
> > +  }
> > +
> > +  PtrTrack->CurrPtr = InDeletedVariable;
> > +  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;
> > +}
> > +
> > +/**
> > +  This code Finds the Next available variable.
> > +
> > +  Caution: This function may receive untrusted input.
> > +  This function may be invoked in SMM mode. This function will do basic
> > validation, before parse the data.
> > +
> > +  @param[in]  VariableName  Pointer to variable name.
> > +  @param[in]  VendorGuid    Variable Vendor Guid.
> > +  @param[out] VariablePtr   Pointer to variable header address.
> > +
> > +  @retval EFI_SUCCESS           The function completed successfully.
> > +  @retval EFI_NOT_FOUND         The next variable was not found.
> > +  @retval EFI_INVALID_PARAMETER If VariableName is not an empty
> string,
> > while VendorGuid is NULL.
> > +  @retval EFI_INVALID_PARAMETER The input values of VariableName
> and
> > VendorGuid are not a name and
> > +                                GUID of an existing variable.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +VariableServiceGetNextVariableInternal (
> > +  IN  CHAR16                *VariableName,
> > +  IN  EFI_GUID              *VendorGuid,
> > +  OUT VARIABLE_HEADER       **VariablePtr
> > +  )
> > +{
> > +  VARIABLE_STORE_TYPE     Type;
> > +  VARIABLE_POINTER_TRACK  Variable;
> > +  VARIABLE_POINTER_TRACK  VariableInHob;
> > +  VARIABLE_POINTER_TRACK  VariablePtrTrack;
> > +  EFI_STATUS              Status;
> > +  VARIABLE_STORE_HEADER
> *VariableStoreHeader[VariableStoreTypeMax];
> > +
> > +  Status = FindVariable (VariableName, VendorGuid, &Variable,
> > &mVariableModuleGlobal->VariableGlobal, FALSE);
> > +  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
> > +    //
> > +    // For VariableName is an empty string, FindVariable() will try to find
> and
> > return
> > +    // the first qualified variable, and if FindVariable() returns error
> > (EFI_NOT_FOUND)
> > +    // as no any variable is found, still go to return the error
> (EFI_NOT_FOUND).
> > +    //
> > +    if (VariableName[0] != 0) {
> > +      //
> > +      // For VariableName is not an empty string, and FindVariable() returns
> error
> > as
> > +      // VariableName and VendorGuid are not a name and GUID of an
> existing
> > variable,
> > +      // there is no way to get next variable, follow spec to return
> > EFI_INVALID_PARAMETER.
> > +      //
> > +      Status = EFI_INVALID_PARAMETER;
> > +    }
> > +    goto Done;
> > +  }
> > +
> > +  if (VariableName[0] != 0) {
> > +    //
> > +    // If variable name is not NULL, get next variable.
> > +    //
> > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +  }
> > +
> > +  //
> > +  // 0: Volatile, 1: HOB, 2: Non-Volatile.
> > +  // The index and attributes mapping must be kept in this order as
> FindVariable
> > +  // makes use of this mapping to implement search algorithm.
> > +  //
> > +  VariableStoreHeader[VariableStoreTypeVolatile] =
> > (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal-
> > >VariableGlobal.VolatileVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeHob]      =
> (VARIABLE_STORE_HEADER
> > *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
> > +  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;
> > +
> > +  while (TRUE) {
> > +    //
> > +    // Switch from Volatile to HOB, to Non-Volatile.
> > +    //
> > +    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {
> > +      //
> > +      // Find current storage index
> > +      //
> > +      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax;
> > Type++) {
> > +        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr ==
> > GetStartPointer (VariableStoreHeader[Type]))) {
> > +          break;
> > +        }
> > +      }
> > +      ASSERT (Type < VariableStoreTypeMax);
> > +      //
> > +      // Switch to next storage
> > +      //
> > +      for (Type++; Type < VariableStoreTypeMax; Type++) {
> > +        if (VariableStoreHeader[Type] != NULL) {
> > +          break;
> > +        }
> > +      }
> > +      //
> > +      // Capture the case that
> > +      // 1. current storage is the last one, or
> > +      // 2. no further storage
> > +      //
> > +      if (Type == VariableStoreTypeMax) {
> > +        Status = EFI_NOT_FOUND;
> > +        goto Done;
> > +      }
> > +      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);
> > +      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);
> > +      Variable.CurrPtr  = Variable.StartPtr;
> > +    }
> > +
> > +    //
> > +    // Variable is found
> > +    //
> > +    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State
> ==
> > (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
> > +      if (!AtRuntime () || ((Variable.CurrPtr->Attributes &
> > EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
> > +        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION &
> > VAR_ADDED)) {
> > +          //
> > +          // If it is a IN_DELETED_TRANSITION variable,
> > +          // and there is also a same ADDED one at the same time,
> > +          // don't return it.
> > +          //
> > +          VariablePtrTrack.StartPtr = Variable.StartPtr;
> > +          VariablePtrTrack.EndPtr = Variable.EndPtr;
> > +          Status = FindVariableEx (
> > +                     GetVariableNamePtr (Variable.CurrPtr),
> > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > +                     FALSE,
> > +                     &VariablePtrTrack
> > +                     );
> > +          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State ==
> > VAR_ADDED) {
> > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +            continue;
> > +          }
> > +        }
> > +
> > +        //
> > +        // Don't return NV variable when HOB overrides it
> > +        //
> > +        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) &&
> > (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
> > +            (Variable.StartPtr == GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeNv]))
> > +           ) {
> > +          VariableInHob.StartPtr = GetStartPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > +          VariableInHob.EndPtr   = GetEndPointer
> > (VariableStoreHeader[VariableStoreTypeHob]);
> > +          Status = FindVariableEx (
> > +                     GetVariableNamePtr (Variable.CurrPtr),
> > +                     GetVendorGuidPtr (Variable.CurrPtr),
> > +                     FALSE,
> > +                     &VariableInHob
> > +                     );
> > +          if (!EFI_ERROR (Status)) {
> > +            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +            continue;
> > +          }
> > +        }
> > +
> > +        *VariablePtr = Variable.CurrPtr;
> > +        Status = EFI_SUCCESS;
> > +        goto Done;
> > +      }
> > +    }
> > +
> > +    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
> > +  }
> > +
> > +Done:
> > +  return Status;
> > +}
> > +
> > +/**
> > +  Routine used to track statistical information about variable usage.
> > +  The data is stored in the EFI system table so it can be accessed later.
> > +  VariableInfo.efi can dump out the table. Only Boot Services variable
> > +  accesses are tracked by this code. The PcdVariableCollectStatistics
> > +  build flag controls if this feature is enabled.
> > +
> > +  A read that hits in the cache will have Read and Cache true for
> > +  the transaction. Data is allocated by this routine, but never
> > +  freed.
> > +
> > +  @param[in] VariableName   Name of the Variable to track.
> > +  @param[in] VendorGuid     Guid of the Variable to track.
> > +  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.
> > +  @param[in] Read           TRUE if GetVariable() was called.
> > +  @param[in] Write          TRUE if SetVariable() was called.
> > +  @param[in] Delete         TRUE if deleted via SetVariable().
> > +  @param[in] Cache          TRUE for a cache hit.
> > +
> > +**/
> > +VOID
> > +UpdateVariableInfo (
> > +  IN  CHAR16                  *VariableName,
> > +  IN  EFI_GUID                *VendorGuid,
> > +  IN  BOOLEAN                 Volatile,
> > +  IN  BOOLEAN                 Read,
> > +  IN  BOOLEAN                 Write,
> > +  IN  BOOLEAN                 Delete,
> > +  IN  BOOLEAN                 Cache
> > +  )
> > +{
> > +  VARIABLE_INFO_ENTRY   *Entry;
> > +
> > +  if (FeaturePcdGet (PcdVariableCollectStatistics)) {
> > +
> > +    if (AtRuntime ()) {
> > +      // Don't collect statistics at runtime.
> > +      return;
> > +    }
> > +
> > +    if (gVariableInfo == NULL) {
> > +      //
> > +      // On the first call allocate a entry and place a pointer to it in
> > +      // the EFI System Table.
> > +      //
> > +      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > +      ASSERT (gVariableInfo != NULL);
> > +
> > +      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
> > +      gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));
> > +      ASSERT (gVariableInfo->Name != NULL);
> > +      StrCpyS (gVariableInfo->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > +      gVariableInfo->Volatile = Volatile;
> > +    }
> > +
> > +
> > +    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
> > +      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
> > +        if (StrCmp (VariableName, Entry->Name) == 0) {
> > +          if (Read) {
> > +            Entry->ReadCount++;
> > +          }
> > +          if (Write) {
> > +            Entry->WriteCount++;
> > +          }
> > +          if (Delete) {
> > +            Entry->DeleteCount++;
> > +          }
> > +          if (Cache) {
> > +            Entry->CacheCount++;
> > +          }
> > +
> > +          return;
> > +        }
> > +      }
> > +
> > +      if (Entry->Next == NULL) {
> > +        //
> > +        // If the entry is not in the table add it.
> > +        // Next iteration of the loop will fill in the data.
> > +        //
> > +        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
> > +        ASSERT (Entry->Next != NULL);
> > +
> > +        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
> > +        Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));
> > +        ASSERT (Entry->Next->Name != NULL);
> > +        StrCpyS (Entry->Next->Name,
> StrSize(VariableName)/sizeof(CHAR16),
> > VariableName);
> > +        Entry->Next->Volatile = Volatile;
> > +      }
> > +
> > +    }
> > +  }
> > +}
> > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > index ec463d063e..ce409f22a3 100644
> > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
> > @@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  #include <Guid/SmmVariableCommon.h>
> >  #include "Variable.h"
> > +#include "VariableParsing.h"
> >
> >  BOOLEAN                                              mAtRuntime              = FALSE;
> >  UINT8                                                *mVariableBufferPayload = NULL;
> > --
> > 2.16.2.windows.1
> 


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

* Re: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-10-03 18:35     ` Kubacki, Michael A
@ 2019-10-16  7:55       ` Wu, Hao A
  2019-10-16 16:37         ` Kubacki, Michael A
  0 siblings, 1 reply; 45+ messages in thread
From: Wu, Hao A @ 2019-10-16  7:55 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Friday, October 04, 2019 2:35 AM
> To: Wu, Hao A; devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> auth status in VariableParsing
> 
> I will make the following changes in V3:
> 
> > InitVariableParsing() seems an internal function, the 'EFIAPI' keyword can
> be
> > dropped. Please help to update the function definition in .C file as well.
> 
> I will remove the EFIAPI keyword.
> 
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index 1a57d7e1ba..53d797152c 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > (PcdMaxVariableSize);
> > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) :
> > > mVariableModuleGlobal->MaxVariableSize);
> > >
> > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +
> >
> >
> > After the above initialization, mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat
> > will be changed temporarily within
> > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable store
> > will be converted to the auth format:
> >
> > VOID *
> > ConvertNormalVarStorageToAuthVarStorage (
> >   VARIABLE_STORE_HEADER *NormalVarStorage
> >   )
> > {
> >   ...
> >   //
> >   // Set AuthFormat as FALSE for normal variable storage
> >   //
> >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> >   ...
> >   //
> >   // Restore AuthFormat
> >   //
> >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> >   return AuthVarStorage;
> > }
> >
> >
> > I think there will be issues in such converting, since I found that at least
> > GetVariableHeaderSize() and NameSizeOfVariable() get called during the
> > execution of ConvertNormalVarStorageToAuthVarStorage(). And they are
> > checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat'.
> >
> >
> 
> You're right that will be a problem. I missed this temporary change in the
> value.
> I'm going to have all the functions dependent on authentication status in
> VariableParsing.c take it as a parameter and let the respective drivers linked
> against it maintain their own single copy of the authentication state.


I am really sorry for not raising this question until I saw the latest patch
series:

Is it possible to call the InitVariableParsing() function (maybe a rename for
the function for better understanding) for the temporary changes for
'mVariableModuleGlobal->VariableGlobal.AuthFormat' in function
ConvertNormalVarStorageToAuthVarStorage()?

In my opinion, doing so can avoid changing many function interfaces.

Best Regards,
Hao Wu


> 
> > >    //
> > >    // Parse non-volatile variable data and get last variable offset.
> > >    //
> > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > >
> > >    //
> > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > -  // has been initialized in InitNonVolatileVariableStore().
> > > +  // is initialized in InitNonVolatileVariableStore().
> > >    //
> > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable
> > > format!\n"));
> > > -    //
> > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > > initialize it.
> > > -    //
> > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > >    } else {
> > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > variable support!\n"));
> > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> >
> >
> > Not sure why the above changes belong to this patch.
> > Could you help to double confirm?
> 
> This was used during testing and is not needed. I will remove it.
> 
> Thanks,
> Michael
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Thursday, October 3, 2019 1:04 AM
> > To: devel@edk2.groups.io; Kubacki, Michael A
> > <michael.a.kubacki@intel.com>
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> local
> > auth status in VariableParsing
> >
> > Inline comments below:
> >
> >
> > > -----Original Message-----
> > > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf
> Of
> > > Kubacki, Michael A
> > > Sent: Saturday, September 28, 2019 9:47 AM
> > > To: devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > > Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao, Jiewen
> > > Subject: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> > > auth status in VariableParsing
> > >
> > > The file VariableParsing.c provides generic functionality related to
> > > parsing variable related structures and information. In order to
> > > calculate offsets for certain operations, the functions must know if
> > > authenticated variables are enabled as this increases the size of
> > > variable headers.
> > >
> > > This change removes linking against a global variable in an external
> > > file in favor of a statically scoped variable in VariableParsing.c
> > > Because this file is unaware of how the authenticated variable status
> > > is determined, the variable is set through a function interface
> > > invoked during variable driver initialization.
> > >
> > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > Cc: Eric Dong <eric.dong@intel.com>
> > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > Cc: Liming Gao <liming.gao@intel.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Ray Ni <ray.ni@intel.com>
> > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > ---
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 14
> > > +++++++++
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10
> +++---
> > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 33
> > > ++++++++++++++++----
> > >  3 files changed, 45 insertions(+), 12 deletions(-)
> > >
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > index 6f2000f3ee..3eba590634 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > @@ -308,4 +308,18 @@ UpdateVariableInfo (
> > >    IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > >    );
> > >
> > > +/**
> > > +  Initializes context needed for variable parsing functions.
> > > +
> > > +  @param[in]       AuthFormat          If true then indicates authenticated
> > > variables are supported
> > > +
> > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > +  @retval          Others              An error occurred during initialization
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +InitVariableParsing (
> >
> >
> > InitVariableParsing() seems an internal function, the 'EFIAPI' keyword can
> be
> > dropped. Please help to update the function definition in .C file as well.
> >
> >
> > > +  IN  BOOLEAN   AuthFormat
> > > +  );
> > > +
> > >  #endif
> > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > index 1a57d7e1ba..53d797152c 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > (PcdMaxVariableSize);
> > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) :
> > > mVariableModuleGlobal->MaxVariableSize);
> > >
> > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat);
> > > +  ASSERT_EFI_ERROR (Status);
> > > +
> >
> >
> > After the above initialization, mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat
> > will be changed temporarily within
> > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable store
> > will be converted to the auth format:
> >
> > VOID *
> > ConvertNormalVarStorageToAuthVarStorage (
> >   VARIABLE_STORE_HEADER *NormalVarStorage
> >   )
> > {
> >   ...
> >   //
> >   // Set AuthFormat as FALSE for normal variable storage
> >   //
> >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> >   ...
> >   //
> >   // Restore AuthFormat
> >   //
> >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> >   return AuthVarStorage;
> > }
> >
> >
> > I think there will be issues in such converting, since I found that at least
> > GetVariableHeaderSize() and NameSizeOfVariable() get called during the
> > execution of ConvertNormalVarStorageToAuthVarStorage(). And they are
> > checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > >VariableGlobal.AuthFormat'.
> >
> >
> > >    //
> > >    // Parse non-volatile variable data and get last variable offset.
> > >    //
> > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > >
> > >    //
> > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > -  // has been initialized in InitNonVolatileVariableStore().
> > > +  // is initialized in InitNonVolatileVariableStore().
> > >    //
> > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable
> > > format!\n"));
> > > -    //
> > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > > initialize it.
> > > -    //
> > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > >    } else {
> > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > variable support!\n"));
> > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> >
> >
> > Not sure why the above changes belong to this patch.
> > Could you help to double confirm?
> >
> > Best Regards,
> > Hao Wu
> >
> >
> > >      VariableGuid = &gEfiVariableGuid;
> > >    }
> > >
> > > diff --git
> > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > index 394195342d..0a47f6d10d 100644
> > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > +++
> b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > >
> > >  #include "VariableParsing.h"
> > >
> > > +STATIC  BOOLEAN   mAuthFormat;
> > > +
> > >  /**
> > >
> > >    This code checks if variable header is valid or not.
> > > @@ -88,7 +90,7 @@ GetVariableHeaderSize (  {
> > >    UINTN Value;
> > >
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > >    } else {
> > >      Value = sizeof (VARIABLE_HEADER); @@ -114,7 +116,7 @@
> > > NameSizeOfVariable (
> > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > >
> > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      if (AuthVariable->State == (UINT8) (-1) ||
> > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -149,7 +151,7 @@
> > > SetNameSizeOfVariable (
> > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > >
> > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      AuthVariable->NameSize = (UINT32) NameSize;
> > >    } else {
> > >      Variable->NameSize = (UINT32) NameSize; @@ -173,7 +175,7 @@
> > > DataSizeOfVariable (
> > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > >
> > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      if (AuthVariable->State == (UINT8) (-1) ||
> > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -208,7 +210,7 @@
> > > SetDataSizeOfVariable (
> > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > >
> > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      AuthVariable->DataSize = (UINT32) DataSize;
> > >    } else {
> > >      Variable->DataSize = (UINT32) DataSize; @@ -248,7 +250,7 @@
> > > GetVendorGuidPtr (
> > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > >
> > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > +  if (mAuthFormat) {
> > >      return &AuthVariable->VendorGuid;
> > >    } else {
> > >      return &Variable->VendorGuid;
> > > @@ -746,3 +748,22 @@ UpdateVariableInfo (
> > >      }
> > >    }
> > >  }
> > > +
> > > +/**
> > > +  Initializes context needed for variable parsing functions.
> > > +
> > > +  @param[in]       AuthFormat          If true then indicates authenticated
> > > variables are supported
> > > +
> > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > +  @retval          Others              An error occurred during initialization
> > > +**/
> > > +EFI_STATUS
> > > +EFIAPI
> > > +InitVariableParsing (
> > > +  IN  BOOLEAN   AuthFormat
> > > +  )
> > > +{
> > > +  mAuthFormat = AuthFormat;
> > > +
> > > +  return EFI_SUCCESS;
> > > +}
> > > --
> > > 2.16.2.windows.1
> > >
> > >
> > > 
> >
> 


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

* Re: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-10-16  7:55       ` Wu, Hao A
@ 2019-10-16 16:37         ` Kubacki, Michael A
  2019-10-17  1:00           ` Wu, Hao A
  0 siblings, 1 reply; 45+ messages in thread
From: Kubacki, Michael A @ 2019-10-16 16:37 UTC (permalink / raw)
  To: Wu, Hao A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

The InitVariableParsing () function has already been removed entirely in V3+ to
prevent issues like this. I strongly believe this is the right direction. It is only a
matter of time before someone else modifies the global and forgets to call
InitVariableParsing ().

Furthermore, VariablePei has already added a parameter to most of the equivalent
functions implemented in that driver so there's some potential to further
consolidate the variable parsing implementation in the future.

Thanks,
Michael

> -----Original Message-----
> From: Wu, Hao A <hao.a.wu@intel.com>
> Sent: Wednesday, October 16, 2019 12:55 AM
> To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo Ersek
> <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney, Michael
> D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> auth status in VariableParsing
> 
> > -----Original Message-----
> > From: Kubacki, Michael A
> > Sent: Friday, October 04, 2019 2:35 AM
> > To: Wu, Hao A; devel@edk2.groups.io
> > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > Kinney, Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> > local auth status in VariableParsing
> >
> > I will make the following changes in V3:
> >
> > > InitVariableParsing() seems an internal function, the 'EFIAPI'
> > > keyword can
> > be
> > > dropped. Please help to update the function definition in .C file as well.
> >
> > I will remove the EFIAPI keyword.
> >
> > > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > index 1a57d7e1ba..53d797152c 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > > (PcdMaxVariableSize);
> > > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize)
> :
> > > > mVariableModuleGlobal->MaxVariableSize);
> > > >
> > > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat);
> > > > +  ASSERT_EFI_ERROR (Status);
> > > > +
> > >
> > >
> > > After the above initialization, mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat
> > > will be changed temporarily within
> > > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable
> > > store will be converted to the auth format:
> > >
> > > VOID *
> > > ConvertNormalVarStorageToAuthVarStorage (
> > >   VARIABLE_STORE_HEADER *NormalVarStorage
> > >   )
> > > {
> > >   ...
> > >   //
> > >   // Set AuthFormat as FALSE for normal variable storage
> > >   //
> > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> > >   ...
> > >   //
> > >   // Restore AuthFormat
> > >   //
> > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> > >   return AuthVarStorage;
> > > }
> > >
> > >
> > > I think there will be issues in such converting, since I found that
> > > at least
> > > GetVariableHeaderSize() and NameSizeOfVariable() get called during
> > > the execution of ConvertNormalVarStorageToAuthVarStorage(). And
> they
> > > are checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat'.
> > >
> > >
> >
> > You're right that will be a problem. I missed this temporary change in
> > the value.
> > I'm going to have all the functions dependent on authentication status
> > in VariableParsing.c take it as a parameter and let the respective
> > drivers linked against it maintain their own single copy of the authentication
> state.
> 
> 
> I am really sorry for not raising this question until I saw the latest patch
> series:
> 
> Is it possible to call the InitVariableParsing() function (maybe a rename for
> the function for better understanding) for the temporary changes for
> 'mVariableModuleGlobal->VariableGlobal.AuthFormat' in function
> ConvertNormalVarStorageToAuthVarStorage()?
> 
> In my opinion, doing so can avoid changing many function interfaces.
> 
> Best Regards,
> Hao Wu
> 
> 
> >
> > > >    //
> > > >    // Parse non-volatile variable data and get last variable offset.
> > > >    //
> > > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > > >
> > > >    //
> > > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > > -  // has been initialized in InitNonVolatileVariableStore().
> > > > +  // is initialized in InitNonVolatileVariableStore().
> > > >    //
> > > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth
> > > > variable format!\n"));
> > > > -    //
> > > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > > > initialize it.
> > > > -    //
> > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > > >    } else {
> > > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > > variable support!\n"));
> > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > >
> > >
> > > Not sure why the above changes belong to this patch.
> > > Could you help to double confirm?
> >
> > This was used during testing and is not needed. I will remove it.
> >
> > Thanks,
> > Michael
> >
> > > -----Original Message-----
> > > From: Wu, Hao A <hao.a.wu@intel.com>
> > > Sent: Thursday, October 3, 2019 1:04 AM
> > > To: devel@edk2.groups.io; Kubacki, Michael A
> > > <michael.a.kubacki@intel.com>
> > > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>;
> > > Laszlo
> > Ersek
> > > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> > Michael
> > > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang,
> > > Jian J <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> > local
> > > auth status in VariableParsing
> > >
> > > Inline comments below:
> > >
> > >
> > > > -----Original Message-----
> > > > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf
> > Of
> > > > Kubacki, Michael A
> > > > Sent: Saturday, September 28, 2019 9:47 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao,
> > > > Liming; Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> > > > Jiewen
> > > > Subject: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> > > > local auth status in VariableParsing
> > > >
> > > > The file VariableParsing.c provides generic functionality related
> > > > to parsing variable related structures and information. In order
> > > > to calculate offsets for certain operations, the functions must
> > > > know if authenticated variables are enabled as this increases the
> > > > size of variable headers.
> > > >
> > > > This change removes linking against a global variable in an
> > > > external file in favor of a statically scoped variable in
> > > > VariableParsing.c Because this file is unaware of how the
> > > > authenticated variable status is determined, the variable is set
> > > > through a function interface invoked during variable driver initialization.
> > > >
> > > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > > Cc: Eric Dong <eric.dong@intel.com>
> > > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > > Cc: Liming Gao <liming.gao@intel.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Ray Ni <ray.ni@intel.com>
> > > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > > ---
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h |
> 14
> > > > +++++++++
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10
> > +++---
> > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c |
> 33
> > > > ++++++++++++++++----
> > > >  3 files changed, 45 insertions(+), 12 deletions(-)
> > > >
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > index 6f2000f3ee..3eba590634 100644
> > > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > @@ -308,4 +308,18 @@ UpdateVariableInfo (
> > > >    IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > > >    );
> > > >
> > > > +/**
> > > > +  Initializes context needed for variable parsing functions.
> > > > +
> > > > +  @param[in]       AuthFormat          If true then indicates authenticated
> > > > variables are supported
> > > > +
> > > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > > +  @retval          Others              An error occurred during initialization
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +InitVariableParsing (
> > >
> > >
> > > InitVariableParsing() seems an internal function, the 'EFIAPI'
> > > keyword can
> > be
> > > dropped. Please help to update the function definition in .C file as well.
> > >
> > >
> > > > +  IN  BOOLEAN   AuthFormat
> > > > +  );
> > > > +
> > > >  #endif
> > > > diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > index 1a57d7e1ba..53d797152c 100644
> > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > > (PcdMaxVariableSize);
> > > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize)
> :
> > > > mVariableModuleGlobal->MaxVariableSize);
> > > >
> > > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat);
> > > > +  ASSERT_EFI_ERROR (Status);
> > > > +
> > >
> > >
> > > After the above initialization, mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat
> > > will be changed temporarily within
> > > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable
> > > store will be converted to the auth format:
> > >
> > > VOID *
> > > ConvertNormalVarStorageToAuthVarStorage (
> > >   VARIABLE_STORE_HEADER *NormalVarStorage
> > >   )
> > > {
> > >   ...
> > >   //
> > >   // Set AuthFormat as FALSE for normal variable storage
> > >   //
> > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> > >   ...
> > >   //
> > >   // Restore AuthFormat
> > >   //
> > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> > >   return AuthVarStorage;
> > > }
> > >
> > >
> > > I think there will be issues in such converting, since I found that
> > > at least
> > > GetVariableHeaderSize() and NameSizeOfVariable() get called during
> > > the execution of ConvertNormalVarStorageToAuthVarStorage(). And
> they
> > > are checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > > >VariableGlobal.AuthFormat'.
> > >
> > >
> > > >    //
> > > >    // Parse non-volatile variable data and get last variable offset.
> > > >    //
> > > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > > >
> > > >    //
> > > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > > -  // has been initialized in InitNonVolatileVariableStore().
> > > > +  // is initialized in InitNonVolatileVariableStore().
> > > >    //
> > > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth
> > > > variable format!\n"));
> > > > -    //
> > > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will
> > > > initialize it.
> > > > -    //
> > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > > >    } else {
> > > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > > variable support!\n"));
> > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > >
> > >
> > > Not sure why the above changes belong to this patch.
> > > Could you help to double confirm?
> > >
> > > Best Regards,
> > > Hao Wu
> > >
> > >
> > > >      VariableGuid = &gEfiVariableGuid;
> > > >    }
> > > >
> > > > diff --git
> > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > index 394195342d..0a47f6d10d 100644
> > > > ---
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > +++
> > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > > >
> > > >  #include "VariableParsing.h"
> > > >
> > > > +STATIC  BOOLEAN   mAuthFormat;
> > > > +
> > > >  /**
> > > >
> > > >    This code checks if variable header is valid or not.
> > > > @@ -88,7 +90,7 @@ GetVariableHeaderSize (  {
> > > >    UINTN Value;
> > > >
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > >    } else {
> > > >      Value = sizeof (VARIABLE_HEADER); @@ -114,7 +116,7 @@
> > > > NameSizeOfVariable (
> > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > >
> > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      if (AuthVariable->State == (UINT8) (-1) ||
> > > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -149,7
> > > > +151,7 @@ SetNameSizeOfVariable (
> > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > >
> > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      AuthVariable->NameSize = (UINT32) NameSize;
> > > >    } else {
> > > >      Variable->NameSize = (UINT32) NameSize; @@ -173,7 +175,7 @@
> > > > DataSizeOfVariable (
> > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > >
> > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      if (AuthVariable->State == (UINT8) (-1) ||
> > > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -208,7
> > > > +210,7 @@ SetDataSizeOfVariable (
> > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > >
> > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      AuthVariable->DataSize = (UINT32) DataSize;
> > > >    } else {
> > > >      Variable->DataSize = (UINT32) DataSize; @@ -248,7 +250,7 @@
> > > > GetVendorGuidPtr (
> > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > >
> > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > +  if (mAuthFormat) {
> > > >      return &AuthVariable->VendorGuid;
> > > >    } else {
> > > >      return &Variable->VendorGuid; @@ -746,3 +748,22 @@
> > > > UpdateVariableInfo (
> > > >      }
> > > >    }
> > > >  }
> > > > +
> > > > +/**
> > > > +  Initializes context needed for variable parsing functions.
> > > > +
> > > > +  @param[in]       AuthFormat          If true then indicates authenticated
> > > > variables are supported
> > > > +
> > > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > > +  @retval          Others              An error occurred during initialization
> > > > +**/
> > > > +EFI_STATUS
> > > > +EFIAPI
> > > > +InitVariableParsing (
> > > > +  IN  BOOLEAN   AuthFormat
> > > > +  )
> > > > +{
> > > > +  mAuthFormat = AuthFormat;
> > > > +
> > > > +  return EFI_SUCCESS;
> > > > +}
> > > > --
> > > > 2.16.2.windows.1
> > > >
> > > >
> > > > 
> > >
> >
> 


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

* Re: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing
  2019-10-16 16:37         ` Kubacki, Michael A
@ 2019-10-17  1:00           ` Wu, Hao A
  0 siblings, 0 replies; 45+ messages in thread
From: Wu, Hao A @ 2019-10-17  1:00 UTC (permalink / raw)
  To: Kubacki, Michael A, devel@edk2.groups.io
  Cc: Bi, Dandan, Ard Biesheuvel, Dong, Eric, Laszlo Ersek, Gao, Liming,
	Kinney, Michael D, Ni, Ray, Wang, Jian J, Yao, Jiewen

> -----Original Message-----
> From: Kubacki, Michael A
> Sent: Thursday, October 17, 2019 12:37 AM
> To: Wu, Hao A; devel@edk2.groups.io
> Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming; Kinney,
> Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add local
> auth status in VariableParsing
> 
> The InitVariableParsing () function has already been removed entirely in V3+
> to
> prevent issues like this. I strongly believe this is the right direction. It is only a
> matter of time before someone else modifies the global and forgets to call
> InitVariableParsing ().
> 
> Furthermore, VariablePei has already added a parameter to most of the
> equivalent


Thanks for the clarification.
I agree with your point, will reply to the corresponding patch in the latest
series.

Best Regards,
Hao Wu


> functions implemented in that driver so there's some potential to further
> consolidate the variable parsing implementation in the future.
> 
> Thanks,
> Michael
> 
> > -----Original Message-----
> > From: Wu, Hao A <hao.a.wu@intel.com>
> > Sent: Wednesday, October 16, 2019 12:55 AM
> > To: Kubacki, Michael A <michael.a.kubacki@intel.com>;
> > devel@edk2.groups.io
> > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>; Laszlo
> Ersek
> > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> Michael
> > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> > <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> local
> > auth status in VariableParsing
> >
> > > -----Original Message-----
> > > From: Kubacki, Michael A
> > > Sent: Friday, October 04, 2019 2:35 AM
> > > To: Wu, Hao A; devel@edk2.groups.io
> > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao, Liming;
> > > Kinney, Michael D; Ni, Ray; Wang, Jian J; Yao, Jiewen
> > > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> > > local auth status in VariableParsing
> > >
> > > I will make the following changes in V3:
> > >
> > > > InitVariableParsing() seems an internal function, the 'EFIAPI'
> > > > keyword can
> > > be
> > > > dropped. Please help to update the function definition in .C file as well.
> > >
> > > I will remove the EFIAPI keyword.
> > >
> > > > > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > index 1a57d7e1ba..53d797152c 100644
> > > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > > > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > > > (PcdMaxVariableSize);
> > > > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32
> (PcdMaxAuthVariableSize)
> > :
> > > > > mVariableModuleGlobal->MaxVariableSize);
> > > > >
> > > > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > > > >VariableGlobal.AuthFormat);
> > > > > +  ASSERT_EFI_ERROR (Status);
> > > > > +
> > > >
> > > >
> > > > After the above initialization, mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat
> > > > will be changed temporarily within
> > > > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable
> > > > store will be converted to the auth format:
> > > >
> > > > VOID *
> > > > ConvertNormalVarStorageToAuthVarStorage (
> > > >   VARIABLE_STORE_HEADER *NormalVarStorage
> > > >   )
> > > > {
> > > >   ...
> > > >   //
> > > >   // Set AuthFormat as FALSE for normal variable storage
> > > >   //
> > > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> > > >   ...
> > > >   //
> > > >   // Restore AuthFormat
> > > >   //
> > > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> > > >   return AuthVarStorage;
> > > > }
> > > >
> > > >
> > > > I think there will be issues in such converting, since I found that
> > > > at least
> > > > GetVariableHeaderSize() and NameSizeOfVariable() get called during
> > > > the execution of ConvertNormalVarStorageToAuthVarStorage(). And
> > they
> > > > are checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat'.
> > > >
> > > >
> > >
> > > You're right that will be a problem. I missed this temporary change in
> > > the value.
> > > I'm going to have all the functions dependent on authentication status
> > > in VariableParsing.c take it as a parameter and let the respective
> > > drivers linked against it maintain their own single copy of the
> authentication
> > state.
> >
> >
> > I am really sorry for not raising this question until I saw the latest patch
> > series:
> >
> > Is it possible to call the InitVariableParsing() function (maybe a rename for
> > the function for better understanding) for the temporary changes for
> > 'mVariableModuleGlobal->VariableGlobal.AuthFormat' in function
> > ConvertNormalVarStorageToAuthVarStorage()?
> >
> > In my opinion, doing so can avoid changing many function interfaces.
> >
> > Best Regards,
> > Hao Wu
> >
> >
> > >
> > > > >    //
> > > > >    // Parse non-volatile variable data and get last variable offset.
> > > > >    //
> > > > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > > > >
> > > > >    //
> > > > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > > > -  // has been initialized in InitNonVolatileVariableStore().
> > > > > +  // is initialized in InitNonVolatileVariableStore().
> > > > >    //
> > > > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth
> > > > > variable format!\n"));
> > > > > -    //
> > > > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize()
> will
> > > > > initialize it.
> > > > > -    //
> > > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > > > >    } else {
> > > > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > > > variable support!\n"));
> > > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > >
> > > >
> > > > Not sure why the above changes belong to this patch.
> > > > Could you help to double confirm?
> > >
> > > This was used during testing and is not needed. I will remove it.
> > >
> > > Thanks,
> > > Michael
> > >
> > > > -----Original Message-----
> > > > From: Wu, Hao A <hao.a.wu@intel.com>
> > > > Sent: Thursday, October 3, 2019 1:04 AM
> > > > To: devel@edk2.groups.io; Kubacki, Michael A
> > > > <michael.a.kubacki@intel.com>
> > > > Cc: Bi, Dandan <dandan.bi@intel.com>; Ard Biesheuvel
> > > > <ard.biesheuvel@linaro.org>; Dong, Eric <eric.dong@intel.com>;
> > > > Laszlo
> > > Ersek
> > > > <lersek@redhat.com>; Gao, Liming <liming.gao@intel.com>; Kinney,
> > > Michael
> > > > D <michael.d.kinney@intel.com>; Ni, Ray <ray.ni@intel.com>; Wang,
> > > > Jian J <jian.j.wang@intel.com>; Yao, Jiewen <jiewen.yao@intel.com>
> > > > Subject: RE: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable:
> Add
> > > local
> > > > auth status in VariableParsing
> > > >
> > > > Inline comments below:
> > > >
> > > >
> > > > > -----Original Message-----
> > > > > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On
> Behalf
> > > Of
> > > > > Kubacki, Michael A
> > > > > Sent: Saturday, September 28, 2019 9:47 AM
> > > > > To: devel@edk2.groups.io
> > > > > Cc: Bi, Dandan; Ard Biesheuvel; Dong, Eric; Laszlo Ersek; Gao,
> > > > > Liming; Kinney, Michael D; Ni, Ray; Wang, Jian J; Wu, Hao A; Yao,
> > > > > Jiewen
> > > > > Subject: [edk2-devel] [PATCH V2 4/9] MdeModulePkg/Variable: Add
> > > > > local auth status in VariableParsing
> > > > >
> > > > > The file VariableParsing.c provides generic functionality related
> > > > > to parsing variable related structures and information. In order
> > > > > to calculate offsets for certain operations, the functions must
> > > > > know if authenticated variables are enabled as this increases the
> > > > > size of variable headers.
> > > > >
> > > > > This change removes linking against a global variable in an
> > > > > external file in favor of a statically scoped variable in
> > > > > VariableParsing.c Because this file is unaware of how the
> > > > > authenticated variable status is determined, the variable is set
> > > > > through a function interface invoked during variable driver
> initialization.
> > > > >
> > > > > Cc: Dandan Bi <dandan.bi@intel.com>
> > > > > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> > > > > Cc: Eric Dong <eric.dong@intel.com>
> > > > > Cc: Laszlo Ersek <lersek@redhat.com>
> > > > > Cc: Liming Gao <liming.gao@intel.com>
> > > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > > Cc: Ray Ni <ray.ni@intel.com>
> > > > > Cc: Jian J Wang <jian.j.wang@intel.com>
> > > > > Cc: Hao A Wu <hao.a.wu@intel.com>
> > > > > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > > > > Signed-off-by: Michael Kubacki <michael.a.kubacki@intel.com>
> > > > > ---
> > > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h |
> > 14
> > > > > +++++++++
> > > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c        | 10
> > > +++---
> > > > >  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c |
> > 33
> > > > > ++++++++++++++++----
> > > > >  3 files changed, 45 insertions(+), 12 deletions(-)
> > > > >
> > > > > diff --git
> > > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > > index 6f2000f3ee..3eba590634 100644
> > > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > > +++
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h
> > > > > @@ -308,4 +308,18 @@ UpdateVariableInfo (
> > > > >    IN OUT VARIABLE_INFO_ENTRY  **VariableInfo
> > > > >    );
> > > > >
> > > > > +/**
> > > > > +  Initializes context needed for variable parsing functions.
> > > > > +
> > > > > +  @param[in]       AuthFormat          If true then indicates
> authenticated
> > > > > variables are supported
> > > > > +
> > > > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > > > +  @retval          Others              An error occurred during initialization
> > > > > +**/
> > > > > +EFI_STATUS
> > > > > +EFIAPI
> > > > > +InitVariableParsing (
> > > >
> > > >
> > > > InitVariableParsing() seems an internal function, the 'EFIAPI'
> > > > keyword can
> > > be
> > > > dropped. Please help to update the function definition in .C file as well.
> > > >
> > > >
> > > > > +  IN  BOOLEAN   AuthFormat
> > > > > +  );
> > > > > +
> > > > >  #endif
> > > > > diff --git
> a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > index 1a57d7e1ba..53d797152c 100644
> > > > > --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
> > > > > @@ -3326,6 +3326,9 @@ InitNonVolatileVariableStore (
> > > > >    mVariableModuleGlobal->MaxVariableSize = PcdGet32
> > > > > (PcdMaxVariableSize);
> > > > >    mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32
> > > > > (PcdMaxAuthVariableSize) != 0) ? PcdGet32
> (PcdMaxAuthVariableSize)
> > :
> > > > > mVariableModuleGlobal->MaxVariableSize);
> > > > >
> > > > > +  Status = InitVariableParsing (mVariableModuleGlobal-
> > > > > >VariableGlobal.AuthFormat);
> > > > > +  ASSERT_EFI_ERROR (Status);
> > > > > +
> > > >
> > > >
> > > > After the above initialization, mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat
> > > > will be changed temporarily within
> > > > ConvertNormalVarStorageToAuthVarStorage() if normal HOB variable
> > > > store will be converted to the auth format:
> > > >
> > > > VOID *
> > > > ConvertNormalVarStorageToAuthVarStorage (
> > > >   VARIABLE_STORE_HEADER *NormalVarStorage
> > > >   )
> > > > {
> > > >   ...
> > > >   //
> > > >   // Set AuthFormat as FALSE for normal variable storage
> > > >   //
> > > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;
> > > >   ...
> > > >   //
> > > >   // Restore AuthFormat
> > > >   //
> > > >   mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;
> > > >   return AuthVarStorage;
> > > > }
> > > >
> > > >
> > > > I think there will be issues in such converting, since I found that
> > > > at least
> > > > GetVariableHeaderSize() and NameSizeOfVariable() get called during
> > > > the execution of ConvertNormalVarStorageToAuthVarStorage(). And
> > they
> > > > are checking 'mAuthFormat' rather than 'mVariableModuleGlobal-
> > > > >VariableGlobal.AuthFormat'.
> > > >
> > > >
> > > > >    //
> > > > >    // Parse non-volatile variable data and get last variable offset.
> > > > >    //
> > > > > @@ -3756,18 +3759,13 @@ VariableCommonInitialize (
> > > > >
> > > > >    //
> > > > >    // mVariableModuleGlobal->VariableGlobal.AuthFormat
> > > > > -  // has been initialized in InitNonVolatileVariableStore().
> > > > > +  // is initialized in InitNonVolatileVariableStore().
> > > > >    //
> > > > >    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > >      DEBUG ((EFI_D_INFO, "Variable driver will work with auth
> > > > > variable format!\n"));
> > > > > -    //
> > > > > -    // Set AuthSupport to FALSE first, VariableWriteServiceInitialize()
> will
> > > > > initialize it.
> > > > > -    //
> > > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > > >      VariableGuid = &gEfiAuthenticatedVariableGuid;
> > > > >    } else {
> > > > >      DEBUG ((EFI_D_INFO, "Variable driver will work without auth
> > > > > variable support!\n"));
> > > > > -    mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;
> > > >
> > > >
> > > > Not sure why the above changes belong to this patch.
> > > > Could you help to double confirm?
> > > >
> > > > Best Regards,
> > > > Hao Wu
> > > >
> > > >
> > > > >      VariableGuid = &gEfiVariableGuid;
> > > > >    }
> > > > >
> > > > > diff --git
> > > > > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > > index 394195342d..0a47f6d10d 100644
> > > > > ---
> > a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > > +++
> > > b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c
> > > > > @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > >
> > > > >  #include "VariableParsing.h"
> > > > >
> > > > > +STATIC  BOOLEAN   mAuthFormat;
> > > > > +
> > > > >  /**
> > > > >
> > > > >    This code checks if variable header is valid or not.
> > > > > @@ -88,7 +90,7 @@ GetVariableHeaderSize (  {
> > > > >    UINTN Value;
> > > > >
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);
> > > > >    } else {
> > > > >      Value = sizeof (VARIABLE_HEADER); @@ -114,7 +116,7 @@
> > > > > NameSizeOfVariable (
> > > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > >
> > > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      if (AuthVariable->State == (UINT8) (-1) ||
> > > > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > > > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -149,7
> > > > > +151,7 @@ SetNameSizeOfVariable (
> > > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > >
> > > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      AuthVariable->NameSize = (UINT32) NameSize;
> > > > >    } else {
> > > > >      Variable->NameSize = (UINT32) NameSize; @@ -173,7 +175,7 @@
> > > > > DataSizeOfVariable (
> > > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > >
> > > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      if (AuthVariable->State == (UINT8) (-1) ||
> > > > >         AuthVariable->DataSize == (UINT32) (-1) ||
> > > > >         AuthVariable->NameSize == (UINT32) (-1) || @@ -208,7
> > > > > +210,7 @@ SetDataSizeOfVariable (
> > > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > >
> > > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      AuthVariable->DataSize = (UINT32) DataSize;
> > > > >    } else {
> > > > >      Variable->DataSize = (UINT32) DataSize; @@ -248,7 +250,7 @@
> > > > > GetVendorGuidPtr (
> > > > >    AUTHENTICATED_VARIABLE_HEADER *AuthVariable;
> > > > >
> > > > >    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;
> > > > > -  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
> > > > > +  if (mAuthFormat) {
> > > > >      return &AuthVariable->VendorGuid;
> > > > >    } else {
> > > > >      return &Variable->VendorGuid; @@ -746,3 +748,22 @@
> > > > > UpdateVariableInfo (
> > > > >      }
> > > > >    }
> > > > >  }
> > > > > +
> > > > > +/**
> > > > > +  Initializes context needed for variable parsing functions.
> > > > > +
> > > > > +  @param[in]       AuthFormat          If true then indicates
> authenticated
> > > > > variables are supported
> > > > > +
> > > > > +  @retval          EFI_SUCCESS         Initialized successfully
> > > > > +  @retval          Others              An error occurred during initialization
> > > > > +**/
> > > > > +EFI_STATUS
> > > > > +EFIAPI
> > > > > +InitVariableParsing (
> > > > > +  IN  BOOLEAN   AuthFormat
> > > > > +  )
> > > > > +{
> > > > > +  mAuthFormat = AuthFormat;
> > > > > +
> > > > > +  return EFI_SUCCESS;
> > > > > +}
> > > > > --
> > > > > 2.16.2.windows.1
> > > > >
> > > > >
> > > > > 
> > > >
> > >
> >
> 


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

end of thread, other threads:[~2019-10-17  1:01 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-28  1:47 [PATCH V2 0/9] UEFI Variable SMI Reduction Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 1/9] MdeModulePkg/Variable: Consolidate common parsing functions Kubacki, Michael A
2019-10-03  8:03   ` Wu, Hao A
2019-10-03 17:35     ` Kubacki, Michael A
2019-10-08  2:11       ` Wu, Hao A
2019-10-08 21:53         ` Kubacki, Michael A
2019-10-08  6:07   ` Wang, Jian J
2019-10-08 22:00     ` Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 2/9] MdeModulePkg/Variable: Parameterize GetNextVariableEx() store list Kubacki, Michael A
2019-10-03  8:03   ` Wu, Hao A
2019-10-03 18:04     ` Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 3/9] MdeModulePkg/Variable: Parameterize VARIABLE_INFO_ENTRY buffer Kubacki, Michael A
2019-10-03  8:03   ` Wu, Hao A
2019-10-03 18:05     ` Kubacki, Michael A
2019-10-08  2:11       ` [edk2-devel] " Wu, Hao A
2019-10-08 21:49         ` Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 4/9] MdeModulePkg/Variable: Add local auth status in VariableParsing Kubacki, Michael A
2019-10-03  8:04   ` [edk2-devel] " Wu, Hao A
2019-10-03 18:35     ` Kubacki, Michael A
2019-10-16  7:55       ` Wu, Hao A
2019-10-16 16:37         ` Kubacki, Michael A
2019-10-17  1:00           ` Wu, Hao A
2019-09-28  1:47 ` [PATCH V2 5/9] MdeModulePkg/Variable: Add a file for NV variable functions Kubacki, Michael A
2019-10-03  8:04   ` Wu, Hao A
2019-10-03 18:43     ` Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 6/9] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats Kubacki, Michael A
2019-10-03  8:04   ` Wu, Hao A
2019-09-28  1:47 ` [PATCH V2 7/9] MdeModulePkg/Variable: Add RT GetVariable() cache support Kubacki, Michael A
2019-10-03  8:04   ` Wu, Hao A
2019-10-03 11:00     ` Laszlo Ersek
2019-10-03 20:53       ` Kubacki, Michael A
2019-10-03 21:53     ` Kubacki, Michael A
2019-10-03 22:01       ` Michael D Kinney
2019-10-03 23:31         ` Kubacki, Michael A
2019-10-04  6:50           ` Laszlo Ersek
2019-10-04 16:48             ` Kubacki, Michael A
2019-10-04  6:38       ` Laszlo Ersek
2019-10-04 16:48         ` Kubacki, Michael A
2019-10-08  2:12       ` Wu, Hao A
2019-09-28  1:47 ` [PATCH V2 8/9] MdeModulePkg/Variable: Add RT GetNextVariableName() " Kubacki, Michael A
2019-10-03  8:04   ` Wu, Hao A
2019-10-03 18:52     ` Kubacki, Michael A
2019-10-03 18:59       ` [edk2-devel] " Andrew Fish
2019-10-03 20:12         ` Kubacki, Michael A
2019-09-28  1:47 ` [PATCH V2 9/9] MdeModulePkg/VariableSmm: Remove unused SMI handler functions Kubacki, Michael A

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