]> git.proxmox.com Git - mirror_edk2.git/commitdiff
SecurityPkg: Delete Auth Variable driver
authorStar Zeng <star.zeng@intel.com>
Wed, 1 Jul 2015 03:13:02 +0000 (03:13 +0000)
committerlzeng14 <lzeng14@Edk2>
Wed, 1 Jul 2015 03:13:02 +0000 (03:13 +0000)
1. Delete TpmMeasurementLib LibraryClass from SecurityPkg after it moved to MdeModulePkg.
2. Update DxeTpmMeasurementLib.inf to include MdeModulePkg.dec.
3. Delete authenticated variable definition from AuthenticatedVariableFormat.h after
them moved to VariableFormat.h.
4. Replace VARIABLE_HEADER with AUTHENTICATED_VARIABLE_HEADER in EsalVariableDxeSal.
5. Delete VariableInfo from SecurityPkg after it merged to VariableInfo in MdeModulePkg.
6. Delete VariablePei from SecurityPkg after it merged to VariablePei in MdeModulePkg.
7. Delete Auth Variable driver from SecurityPkg after it merged to Variable driver in
MdeModulePkg.
8. Also update PACKAGE_GUID and PACKAGE_VERSION in SecurityPkg.dec after the deletion
of authenticated variable definition, VariableInfo, VariablePei and Auth Variable
driver from SecurityPkg; update PLATFORM_VERSION in SecurityPkg.dsc.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17772 6f19259b-4bc3-4df7-8a09-765794883524

36 files changed:
SecurityPkg/Application/VariableInfo/VariableInfo.c [deleted file]
SecurityPkg/Application/VariableInfo/VariableInfo.inf [deleted file]
SecurityPkg/Application/VariableInfo/VariableInfo.uni [deleted file]
SecurityPkg/Application/VariableInfo/VariableInfoExtra.uni [deleted file]
SecurityPkg/Include/Guid/AuthenticatedVariableFormat.h
SecurityPkg/Include/Library/TpmMeasurementLib.h [deleted file]
SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
SecurityPkg/SecurityPkg.dec
SecurityPkg/SecurityPkg.dsc
SecurityPkg/VariableAuthenticated/EsalVariableDxeSal/AuthService.c
SecurityPkg/VariableAuthenticated/EsalVariableDxeSal/Variable.c
SecurityPkg/VariableAuthenticated/EsalVariableDxeSal/Variable.h
SecurityPkg/VariableAuthenticated/Pei/PeiVariableAuth.uni [deleted file]
SecurityPkg/VariableAuthenticated/Pei/PeiVariableExtra.uni [deleted file]
SecurityPkg/VariableAuthenticated/Pei/Variable.c [deleted file]
SecurityPkg/VariableAuthenticated/Pei/Variable.h [deleted file]
SecurityPkg/VariableAuthenticated/Pei/VariablePei.inf [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.h [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/Measurement.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/Reclaim.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VarCheck.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthRuntimeDxe.uni [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmm.uni [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmmRuntimeDxe.uni [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxeExtra.uni [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmExtra.uni [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.inf [deleted file]
SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxeExtra.uni [deleted file]

diff --git a/SecurityPkg/Application/VariableInfo/VariableInfo.c b/SecurityPkg/Application/VariableInfo/VariableInfo.c
deleted file mode 100644 (file)
index 0c1ee4b..0000000
+++ /dev/null
@@ -1,265 +0,0 @@
-/** @file\r
-  If the Variable services have PcdVariableCollectStatistics set to TRUE then \r
-  this utility will print out the statistics information. You can use console \r
-  redirection to capture the data.\r
-  \r
-Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <Uefi.h>\r
-#include <Library/UefiLib.h>\r
-#include <Library/UefiApplicationEntryPoint.h>\r
-#include <Library/BaseMemoryLib.h>\r
-\r
-#include <Library/BaseLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-\r
-#include <Guid/AuthenticatedVariableFormat.h>\r
-#include <Guid/SmmVariableCommon.h>\r
-#include <Protocol/SmmCommunication.h>\r
-#include <Protocol/SmmVariable.h>\r
-\r
-extern EFI_GUID gEfiVariableGuid;\r
-EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication = NULL;\r
-\r
-/**\r
-\r
-  This function get the variable statistics data from SMM variable driver. \r
-\r
-  @param[in, out] SmmCommunicateHeader In input, a pointer to a collection of data that will\r
-                                       be passed into an SMM environment. In output, a pointer \r
-                                       to a collection of data that comes from an SMM environment.\r
-  @param[in, out] SmmCommunicateSize   The size of the SmmCommunicateHeader.\r
-                      \r
-  @retval EFI_SUCCESS               Get the statistics data information.\r
-  @retval EFI_NOT_FOUND             Not found.\r
-  @retval EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-GetVariableStatisticsData (\r
-  IN OUT  EFI_SMM_COMMUNICATE_HEADER  *SmmCommunicateHeader,\r
-  IN OUT  UINTN                       *SmmCommunicateSize\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  SMM_VARIABLE_COMMUNICATE_HEADER     *SmmVariableFunctionHeader;\r
-\r
-  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
-  SmmCommunicateHeader->MessageLength = *SmmCommunicateSize - OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
-\r
-  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) &SmmCommunicateHeader->Data[0];\r
-  SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_STATISTICS;\r
-  \r
-  Status = mSmmCommunication->Communicate (mSmmCommunication, SmmCommunicateHeader, SmmCommunicateSize);\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  Status = SmmVariableFunctionHeader->ReturnStatus; \r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-\r
-  This function get and print the variable statistics data from SMM variable driver. \r
-                     \r
-  @retval EFI_SUCCESS               Print the statistics information successfully.\r
-  @retval EFI_NOT_FOUND             Not found the statistics information.\r
-\r
-**/\r
-EFI_STATUS\r
-PrintInfoFromSmm (\r
-  VOID  \r
-  )\r
-{\r
-  EFI_STATUS                                     Status;\r
-  VARIABLE_INFO_ENTRY                            *VariableInfo;\r
-  EFI_SMM_COMMUNICATE_HEADER                     *CommBuffer;\r
-  UINTN                                          RealCommSize;\r
-  UINTN                                          CommSize;\r
-  SMM_VARIABLE_COMMUNICATE_HEADER                *FunctionHeader;\r
-  EFI_SMM_VARIABLE_PROTOCOL                      *Smmvariable;\r
-  \r
-\r
-  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **) &Smmvariable);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }  \r
-\r
-  CommSize  = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-  RealCommSize = CommSize;\r
-  CommBuffer = AllocateZeroPool (CommSize);\r
-  ASSERT (CommBuffer != NULL);\r
-  \r
-  Print (L"Non-Volatile SMM Variables:\n");\r
-  do {\r
-    Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
-      FreePool (CommBuffer);\r
-      CommBuffer = AllocateZeroPool (CommSize);\r
-      ASSERT (CommBuffer != NULL);\r
-      RealCommSize = CommSize;\r
-      Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
-    }\r
-\r
-    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) { \r
-      break;\r
-    }\r
-\r
-    if (CommSize < RealCommSize) {\r
-      CommSize = RealCommSize;\r
-    }\r
-\r
-    FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;\r
-    VariableInfo   = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;\r
-\r
-    if (!VariableInfo->Volatile) {\r
-      Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
-          VariableInfo->ReadCount,\r
-          VariableInfo->CacheCount,\r
-          VariableInfo->WriteCount,\r
-          VariableInfo->DeleteCount,\r
-          (CHAR16 *)(VariableInfo + 1)\r
-          );\r
-    }\r
-  } while (TRUE);\r
-  \r
-  Print (L"Volatile SMM Variables:\n");\r
-  ZeroMem (CommBuffer, CommSize);\r
-  do {\r
-    Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
-      FreePool (CommBuffer);\r
-      CommBuffer = AllocateZeroPool (CommSize);\r
-      ASSERT (CommBuffer != NULL);\r
-      RealCommSize = CommSize;\r
-      Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
-    }\r
-\r
-    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) { \r
-      break;\r
-    }\r
-\r
-    if (CommSize < RealCommSize) {\r
-      CommSize = RealCommSize;\r
-    }\r
-\r
-    FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;\r
-    VariableInfo   = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;\r
-\r
-    if (VariableInfo->Volatile) {\r
-      Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
-          VariableInfo->ReadCount,\r
-          VariableInfo->CacheCount,\r
-          VariableInfo->WriteCount,\r
-          VariableInfo->DeleteCount,\r
-          (CHAR16 *)(VariableInfo + 1)\r
-          );\r
-    }\r
-  } while (TRUE);\r
-\r
-  FreePool (CommBuffer);  \r
-  return Status;\r
-}\r
-\r
-/**\r
-  The user Entry Point for Application. The user code starts with this function\r
-  as the real entry point for the image goes into a library that calls this \r
-  function.\r
-\r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
-  @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
-  @retval EFI_SUCCESS       The entry point is executed successfully.\r
-  @retval other             Some error occurs when executing this entry point.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-UefiMain (\r
-  IN EFI_HANDLE        ImageHandle,\r
-  IN EFI_SYSTEM_TABLE  *SystemTable\r
-  )\r
-{\r
-  EFI_STATUS            Status;\r
-  VARIABLE_INFO_ENTRY   *VariableInfo;\r
-  VARIABLE_INFO_ENTRY   *Entry;\r
-\r
-  Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);\r
-  if (EFI_ERROR (Status) || (Entry == NULL)) {\r
-    Status = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);\r
-  }\r
-\r
-  if (EFI_ERROR (Status) || (Entry == NULL)) {\r
-    Status = PrintInfoFromSmm ();\r
-    if (!EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }  \r
-\r
-  if (!EFI_ERROR (Status) && (Entry != NULL)) {\r
-    Print (L"Non-Volatile EFI Variables:\n");\r
-    VariableInfo = Entry;\r
-    do {\r
-      if (!VariableInfo->Volatile) {\r
-        Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
-          VariableInfo->ReadCount,\r
-          VariableInfo->CacheCount,\r
-          VariableInfo->WriteCount,\r
-          VariableInfo->DeleteCount,\r
-          VariableInfo->Name\r
-          );\r
-      }\r
-\r
-      VariableInfo = VariableInfo->Next;\r
-    } while (VariableInfo != NULL);\r
-\r
-    Print (L"Volatile EFI Variables:\n");\r
-    VariableInfo = Entry;\r
-    do {\r
-      if (VariableInfo->Volatile) {\r
-        Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
-          VariableInfo->ReadCount,\r
-          VariableInfo->CacheCount,\r
-          VariableInfo->WriteCount,\r
-          VariableInfo->DeleteCount,\r
-          VariableInfo->Name\r
-          );\r
-      }\r
-      VariableInfo = VariableInfo->Next;\r
-    } while (VariableInfo != NULL);\r
-\r
-  } else {\r
-    Print (L"Warning: Variable Dxe driver doesn't enable the feature of statistical information!\n");\r
-    Print (L"If you want to see this info, please:\n");\r
-    Print (L"  1. Set PcdVariableCollectStatistics as TRUE\n");\r
-    Print (L"  2. Rebuild Variable Dxe driver\n");\r
-    Print (L"  3. Run \"VariableInfo\" cmd again\n");\r
-  }\r
-\r
-  return Status;\r
-}\r
diff --git a/SecurityPkg/Application/VariableInfo/VariableInfo.inf b/SecurityPkg/Application/VariableInfo/VariableInfo.inf
deleted file mode 100644 (file)
index d4a771a..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-## @file\r
-#  A shell application that displays statistical information about variable usage\r
-#\r
-#  This application can display statistical information about variable usage for SMM variable\r
-#  driver and non-SMM variable driver.\r
-#  Note that if Variable Dxe driver doesn't enable the feature by setting PcdVariableCollectStatistics\r
-#  as TRUE, the application will not display variable statistical information.\r
-#\r
-# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-##\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = VariableInfo\r
-  MODULE_UNI_FILE                = VariableInfo.uni\r
-  FILE_GUID                      = B9EF901F-A2A2-4fc8-8D2B-3A2E07B301CC\r
-  MODULE_TYPE                    = UEFI_APPLICATION\r
-  VERSION_STRING                 = 1.0\r
-  ENTRY_POINT                    = UefiMain\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-[Sources]\r
-  VariableInfo.c\r
-\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  SecurityPkg/SecurityPkg.dec\r
-\r
-[LibraryClasses]\r
-  UefiApplicationEntryPoint\r
-  UefiLib\r
-  UefiBootServicesTableLib\r
-  BaseMemoryLib\r
-  MemoryAllocationLib\r
-\r
-[Protocols]\r
-  gEfiSmmCommunicationProtocolGuid     ## SOMETIMES_CONSUMES\r
-  \r
-    ## UNDEFINED           # Used to do smm communication\r
-    ## SOMETIMES_CONSUMES \r
-  gEfiSmmVariableProtocolGuid \r
-  \r
-[Guids]\r
-  gEfiAuthenticatedVariableGuid        ## SOMETIMES_CONSUMES    ## SystemTable\r
-  gEfiVariableGuid                     ## CONSUMES              ## SystemTable\r
-\r
-[UserExtensions.TianoCore."ExtraFiles"]\r
-  VariableInfoExtra.uni\r
-  
\ No newline at end of file
diff --git a/SecurityPkg/Application/VariableInfo/VariableInfo.uni b/SecurityPkg/Application/VariableInfo/VariableInfo.uni
deleted file mode 100644 (file)
index e2f214b..0000000
Binary files a/SecurityPkg/Application/VariableInfo/VariableInfo.uni and /dev/null differ
diff --git a/SecurityPkg/Application/VariableInfo/VariableInfoExtra.uni b/SecurityPkg/Application/VariableInfo/VariableInfoExtra.uni
deleted file mode 100644 (file)
index b7fd354..0000000
Binary files a/SecurityPkg/Application/VariableInfo/VariableInfoExtra.uni and /dev/null differ
index c7cd34a943906c048855734f6d8ae304ab7b71c0..1f007cfc73e2ea4c3d545d3a6b92d114e8e1dd31 100644 (file)
@@ -1,16 +1,17 @@
 /** @file\r
-  The variable data structures are related to EDKII-specific \r
+  The variable data structures are related to EDKII-specific\r
   implementation of UEFI authenticated variables.\r
-  AuthenticatedVariableFormat.h defines variable data headers \r
-  and variable storage region headers.\r
-\r
-Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
+  AuthenticatedVariableFormat.h defines variable data headers\r
+  and variable storage region headers that has been moved to\r
+  VariableFormat.h.\r
+\r
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
@@ -18,14 +19,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #ifndef __AUTHENTICATED_VARIABLE_FORMAT_H__\r
 #define __AUTHENTICATED_VARIABLE_FORMAT_H__\r
 \r
-#define EFI_AUTHENTICATED_VARIABLE_GUID \\r
-  { 0xaaf32c78, 0x947b, 0x439a, { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 } }\r
+#include <Guid/VariableFormat.h>\r
 \r
 #define EFI_SECURE_BOOT_ENABLE_DISABLE \\r
   { 0xf0a30bc7, 0xaf08, 0x4556, { 0x99, 0xc4, 0x0, 0x10, 0x9, 0xc9, 0x3a, 0x44 } }\r
 \r
-\r
-extern EFI_GUID gEfiAuthenticatedVariableGuid;\r
 extern EFI_GUID gEfiSecureBootEnableDisableGuid;\r
 extern EFI_GUID gEfiCertDbGuid;\r
 extern EFI_GUID gEfiCustomModeEnableGuid;\r
@@ -36,6 +34,10 @@ extern EFI_GUID gEfiVendorKeysNvGuid;
 /// This variable is used for allowing a physically present user to disable\r
 /// Secure Boot via firmware setup without the possession of PKpriv.\r
 ///\r
+/// GUID: gEfiSecureBootEnableDisableGuid\r
+///\r
+/// Format: UINT8\r
+///\r
 #define EFI_SECURE_BOOT_ENABLE_NAME      L"SecureBootEnable"\r
 #define SECURE_BOOT_ENABLE               1\r
 #define SECURE_BOOT_DISABLE              0\r
@@ -48,6 +50,10 @@ extern EFI_GUID gEfiVendorKeysNvGuid;
 ///    Can enroll or delete KEK without existing PK's private key.\r
 ///    Can enroll or delete signature from DB/DBX without KEK's private key.\r
 ///\r
+///  GUID: gEfiCustomModeEnableGuid\r
+///\r
+/// Format: UINT8\r
+///\r
 #define EFI_CUSTOM_MODE_NAME          L"CustomMode"\r
 #define CUSTOM_SECURE_BOOT_MODE       1\r
 #define STANDARD_SECURE_BOOT_MODE     0\r
@@ -58,173 +64,12 @@ extern EFI_GUID gEfiVendorKeysNvGuid;
 ///  the platform vendor has used a mechanism not defined by the UEFI Specification to\r
 ///  transition the system to setup mode or to update secure boot keys.\r
 ///\r
+///  GUID: gEfiVendorKeysNvGuid\r
+///\r
+/// Format: UINT8\r
+///\r
 #define EFI_VENDOR_KEYS_NV_VARIABLE_NAME       L"VendorKeysNv"\r
 #define VENDOR_KEYS_VALID             1\r
 #define VENDOR_KEYS_MODIFIED          0\r
 \r
-///\r
-/// Alignment of variable name and data, according to the architecture:\r
-/// * For IA-32 and Intel(R) 64 architectures: 1.\r
-/// * For IA-64 architecture: 8.\r
-///\r
-#if defined (MDE_CPU_IPF)\r
-#define ALIGNMENT         8\r
-#else\r
-#define ALIGNMENT         1\r
-#endif\r
-\r
-//\r
-// GET_PAD_SIZE calculates the miminal pad bytes needed to make the current pad size satisfy the alignment requirement.\r
-//\r
-#if (ALIGNMENT == 1)\r
-#define GET_PAD_SIZE(a) (0)\r
-#else\r
-#define GET_PAD_SIZE(a) (((~a) + 1) & (ALIGNMENT - 1))\r
-#endif\r
-\r
-///\r
-/// Alignment of Variable Data Header in Variable Store region.\r
-///\r
-#define HEADER_ALIGNMENT  4\r
-#define HEADER_ALIGN(Header)  (((UINTN) (Header) + HEADER_ALIGNMENT - 1) & (~(HEADER_ALIGNMENT - 1)))\r
-\r
-///\r
-/// Status of Variable Store Region.\r
-///\r
-typedef enum {\r
-  EfiRaw,\r
-  EfiValid,\r
-  EfiInvalid,\r
-  EfiUnknown\r
-} VARIABLE_STORE_STATUS;\r
-\r
-#pragma pack(1)\r
-\r
-#define VARIABLE_STORE_SIGNATURE  EFI_AUTHENTICATED_VARIABLE_GUID\r
-\r
-///\r
-/// Variable Store Header Format and State.\r
-///\r
-#define VARIABLE_STORE_FORMATTED          0x5a\r
-#define VARIABLE_STORE_HEALTHY            0xfe\r
-\r
-///\r
-/// Variable Store region header.\r
-///\r
-typedef struct {\r
-  ///\r
-  /// Variable store region signature.\r
-  ///\r
-  EFI_GUID  Signature;\r
-  ///\r
-  /// Size of entire variable store, \r
-  /// including size of variable store header but not including the size of FvHeader.\r
-  ///\r
-  UINT32  Size;\r
-  ///\r
-  /// Variable region format state.\r
-  ///\r
-  UINT8   Format;\r
-  ///\r
-  /// Variable region healthy state.\r
-  ///\r
-  UINT8   State;\r
-  UINT16  Reserved;\r
-  UINT32  Reserved1;\r
-} VARIABLE_STORE_HEADER;\r
-\r
-///\r
-/// Variable data start flag.\r
-///\r
-#define VARIABLE_DATA                     0x55AA\r
-\r
-///\r
-/// Variable State flags.\r
-///\r
-#define VAR_IN_DELETED_TRANSITION     0xfe  ///< Variable is in obsolete transition.\r
-#define VAR_DELETED                   0xfd  ///< Variable is obsolete.\r
-#define VAR_HEADER_VALID_ONLY         0x7f  ///< Variable header has been valid.\r
-#define VAR_ADDED                     0x3f  ///< Variable has been completely added.\r
-\r
-///\r
-/// Variable Attribute combinations.\r
-///\r
-#define VARIABLE_ATTRIBUTE_NV_BS        (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_BS_RT        (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_AT_AW        (EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT     (VARIABLE_ATTRIBUTE_BS_RT | EFI_VARIABLE_NON_VOLATILE)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_HR  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_HARDWARE_ERROR_RECORD)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_AT  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_AW  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_HR_AT_AW    (VARIABLE_ATTRIBUTE_NV_BS_RT_HR | VARIABLE_ATTRIBUTE_AT_AW)\r
-\r
-/// Single Variable Data Header Structure.\r
-///\r
-typedef struct {\r
-  ///\r
-  /// Variable Data Start Flag.\r
-  ///\r
-  UINT16      StartId;\r
-  ///\r
-  /// Variable State defined above.\r
-  ///\r
-  UINT8       State;\r
-  UINT8       Reserved;\r
-  ///\r
-  /// Attributes of variable defined in UEFI specification.\r
-  ///\r
-  UINT32      Attributes;\r
-  ///\r
-  /// Associated monotonic count value against replay attack.\r
-  ///\r
-  UINT64      MonotonicCount;\r
-  ///\r
-  /// Associated TimeStamp value against replay attack. \r
-  ///\r
-  EFI_TIME    TimeStamp;\r
-  ///\r
-  /// Index of associated public key in database.\r
-  ///\r
-  UINT32      PubKeyIndex;\r
-  ///\r
-  /// Size of variable null-terminated Unicode string name.\r
-  ///\r
-  UINT32      NameSize;\r
-  ///\r
-  /// Size of the variable data without this header.\r
-  ///\r
-  UINT32      DataSize;\r
-  ///\r
-  /// A unique identifier for the vendor that produces and consumes this varaible.\r
-  ///\r
-  EFI_GUID    VendorGuid;\r
-} VARIABLE_HEADER;\r
-\r
-typedef struct {\r
-  EFI_GUID    *Guid;\r
-  CHAR16      *Name;\r
-  UINTN       VariableSize;\r
-} VARIABLE_ENTRY_CONSISTENCY;\r
-\r
-#pragma pack()\r
-\r
-typedef struct _VARIABLE_INFO_ENTRY  VARIABLE_INFO_ENTRY;\r
-\r
-///\r
-/// This structure contains the variable list that is put in EFI system table.\r
-/// The variable driver collects all variables that were used at boot service time and produces this list.\r
-/// This is an optional feature to dump all used variables in shell environment. \r
-///\r
-struct _VARIABLE_INFO_ENTRY {\r
-  VARIABLE_INFO_ENTRY *Next;       ///< Pointer to next entry.\r
-  EFI_GUID            VendorGuid;  ///< Guid of Variable.\r
-  CHAR16              *Name;       ///< Name of Variable. \r
-  UINT32              Attributes;  ///< Attributes of variable defined in UEFI spec.\r
-  UINT32              ReadCount;   ///< Number of times to read this variable.\r
-  UINT32              WriteCount;  ///< Number of times to write this variable.\r
-  UINT32              DeleteCount; ///< Number of times to delete this variable.\r
-  UINT32              CacheCount;  ///< Number of times that cache hits this variable.\r
-  BOOLEAN             Volatile;    ///< TRUE if volatile, FALSE if non-volatile.\r
-};\r
-\r
 #endif // __AUTHENTICATED_VARIABLE_FORMAT_H__\r
diff --git a/SecurityPkg/Include/Library/TpmMeasurementLib.h b/SecurityPkg/Include/Library/TpmMeasurementLib.h
deleted file mode 100644 (file)
index 45542f4..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/** @file\r
-  This library is used by other modules to measure data to TPM.\r
-\r
-Copyright (c) 2012, Intel Corporation. All rights reserved. <BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#ifndef _TPM_MEASUREMENT_LIB_H_\r
-#define _TPM_MEASUREMENT_LIB_H_\r
-\r
-/**\r
-  Tpm measure and log data, and extend the measurement result into a specific PCR.\r
-\r
-  @param[in]  PcrIndex         PCR Index.\r
-  @param[in]  EventType        Event type.\r
-  @param[in]  EventLog         Measurement event log.\r
-  @param[in]  LogLen           Event log length in bytes.\r
-  @param[in]  HashData         The start of the data buffer to be hashed, extended.\r
-  @param[in]  HashDataLen      The length, in bytes, of the buffer referenced by HashData\r
-\r
-  @retval EFI_SUCCESS           Operation completed successfully.\r
-  @retval EFI_UNSUPPORTED       TPM device not available.\r
-  @retval EFI_OUT_OF_RESOURCES  Out of memory.\r
-  @retval EFI_DEVICE_ERROR      The operation was unsuccessful.\r
-**/\r
-EFI_STATUS\r
-EFIAPI \r
-TpmMeasureAndLogData (\r
-  IN UINT32             PcrIndex,\r
-  IN UINT32             EventType,\r
-  IN VOID               *EventLog,\r
-  IN UINT32             LogLen,\r
-  IN VOID               *HashData,\r
-  IN UINT64             HashDataLen\r
-  );\r
-\r
-#endif\r
index d99e48cea8a417248134dff9bb525cc32852370f..4c61d9a21720657268f767389d478f8da9aa6ee9 100644 (file)
@@ -1,10 +1,10 @@
 ## @file\r
 #  Provides TPM measurement functions for TPM1.2 and TPM 2.0\r
 # \r
-#  This library provides TpmMeasureAndLogData() to to measure and log data, and \r
+#  This library provides TpmMeasureAndLogData() to measure and log data, and \r
 #  extend the measurement result into a specific PCR.\r
 #\r
-# Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2012 - 2015, Intel Corporation. All rights reserved.<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution. The full text of the license may be found at\r
@@ -34,6 +34,7 @@
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
   SecurityPkg/SecurityPkg.dec\r
 \r
 [LibraryClasses]\r
index 25ec7d0d1c183e81343b8f000c48f97c2149ba30..b0ef20b05b33df2da69cecd54e892a5675bb0c65 100644 (file)
@@ -20,8 +20,8 @@
   DEC_SPECIFICATION              = 0x00010005\r
   PACKAGE_NAME                   = SecurityPkg\r
   PACKAGE_UNI_FILE               = SecurityPkg.uni\r
-  PACKAGE_GUID                   = 24369CAC-6AA6-4fb8-88DB-90BF061668AD\r
-  PACKAGE_VERSION                = 0.94\r
+  PACKAGE_GUID                   = 4EFC4F66-6219-4427-B780-FB99F470767F\r
+  PACKAGE_VERSION                = 0.95\r
 \r
 [Includes]\r
   Include\r
   ##  @libraryclass  Provides TPM Interface Specification (TIS) interfaces for TPM command.\r
   #\r
   TpmCommLib|Include/Library/TpmCommLib.h\r
-    \r
-  ##  @libraryclass  Provides common interfaces about TPM measurement for other modules.\r
-  #\r
-  TpmMeasurementLib|Include/Library/TpmMeasurementLib.h\r
   \r
   ##  @libraryclass  Provides interfaces to handle TPM 2.0 request.\r
   #\r
   # Include/Guid/SecurityPkgTokenSpace.h\r
   gEfiSecurityPkgTokenSpaceGuid      = { 0xd3fb176, 0x9569, 0x4d51, { 0xa3, 0xef, 0x7d, 0x61, 0xc6, 0x4f, 0xea, 0xba }}\r
 \r
-  ## Guid acted as the authenticated variable store header's signature, and to specify the variable list entries put in the EFI system table.\r
-  #  Include/Guid/AuthenticatedVariableFormat.h\r
-  gEfiAuthenticatedVariableGuid      = { 0xaaf32c78, 0x947b, 0x439a, { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 } }\r
-\r
   ## GUID used to "SecureBootEnable" variable for the Secure Boot feature enable/disable.\r
   #  This variable is used for allowing a physically present user to disable Secure Boot via firmware setup without the possession of PKpriv.\r
   #  Include/Guid/AuthenticatedVariableFormat.h\r
index fd74ec18657a87b72c8d9d377cd13509040f6077..fa94d90a2abd28d09e1d153b0015f46802f19cb5 100644 (file)
@@ -15,7 +15,7 @@
 [Defines]\r
   PLATFORM_NAME                  = SecurityPkg\r
   PLATFORM_GUID                  = B2C4614D-AE76-47ba-B876-5988BFED064F\r
-  PLATFORM_VERSION               = 0.94\r
+  PLATFORM_VERSION               = 0.95\r
   DSC_SPECIFICATION              = 0x00010005\r
   OUTPUT_DIRECTORY               = Build/SecurityPkg\r
   SUPPORTED_ARCHITECTURES        = IA32|IPF|X64|EBC\r
   gEfiSecurityPkgTokenSpaceGuid.PcdTpmScrtmPolicy|1\r
 \r
 [Components]\r
-  SecurityPkg/VariableAuthenticated/Pei/VariablePei.inf\r
   SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.inf\r
   #SecurityPkg/Library/DxeDeferImageLoadLib/DxeDeferImageLoadLib.inf\r
   SecurityPkg/Library/DxeImageAuthenticationStatusLib/DxeImageAuthenticationStatusLib.inf\r
   #\r
   # Application\r
   #\r
-  SecurityPkg/Application/VariableInfo/VariableInfo.inf\r
   SecurityPkg/Application/RngTest/RngTest.inf\r
 \r
   #\r
 #  SecurityPkg/UserIdentification/PwdCredentialProviderDxe/PwdCredentialProviderDxe.inf\r
 #  SecurityPkg/UserIdentification/UsbCredentialProviderDxe/UsbCredentialProviderDxe.inf\r
   SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigDxe.inf\r
-  SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf\r
 \r
   #\r
   # TPM\r
   SecurityPkg/Pkcs7Verify/Pkcs7VerifyDxe/Pkcs7VerifyDxe.inf\r
 \r
 [Components.IA32, Components.X64]\r
-  SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf\r
-  SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.inf\r
   SecurityPkg/Tcg/TcgSmm/TcgSmm.inf\r
   SecurityPkg/Tcg/TrEESmm/TrEESmm.inf\r
   #\r
index 45d5cfe3c655769d15ceef081e863f6d4d686185..490a8b34174db39eedb6f0b626de3d97078f825d 100644 (file)
@@ -39,16 +39,16 @@ AutenticatedVariableServiceInitialize (
   VOID\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINT8                   VarValue;\r
-  UINT32                  VarAttr;\r
-  UINTN                   DataSize;\r
-  UINTN                   CtxSize;\r
-  VARIABLE_HEADER         VariableHeader;\r
-  BOOLEAN                 Valid;\r
+  EFI_STATUS                        Status;\r
+  VARIABLE_POINTER_TRACK            Variable;\r
+  UINT8                             VarValue;\r
+  UINT32                            VarAttr;\r
+  UINTN                             DataSize;\r
+  UINTN                             CtxSize;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  BOOLEAN                           Valid;\r
 \r
-  ZeroMem (&VariableHeader, sizeof (VARIABLE_HEADER));\r
+  ZeroMem (&VariableHeader, sizeof (AUTHENTICATED_VARIABLE_HEADER));\r
 \r
   mVariableModuleGlobal->AuthenticatedVariableGuid[Physical] = &gEfiAuthenticatedVariableGuid;\r
   mVariableModuleGlobal->CertRsa2048Sha256Guid[Physical]     = &gEfiCertRsa2048Sha256Guid;\r
@@ -477,16 +477,16 @@ ProcessVarWithPk (
   IN  BOOLEAN                   IsPk\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  VARIABLE_POINTER_TRACK      PkVariable;\r
-  EFI_SIGNATURE_LIST          *OldPkList;\r
-  EFI_SIGNATURE_DATA          *OldPkData;\r
-  EFI_VARIABLE_AUTHENTICATION *CertData;\r
-  VARIABLE_HEADER             VariableHeader;\r
-  BOOLEAN                     Valid;\r
+  EFI_STATUS                    Status;\r
+  VARIABLE_POINTER_TRACK        PkVariable;\r
+  EFI_SIGNATURE_LIST            *OldPkList;\r
+  EFI_SIGNATURE_DATA            *OldPkData;\r
+  EFI_VARIABLE_AUTHENTICATION   *CertData;\r
+  AUTHENTICATED_VARIABLE_HEADER VariableHeader;\r
+  BOOLEAN                       Valid;\r
 \r
   OldPkList = NULL;\r
-  ZeroMem (&VariableHeader, sizeof (VARIABLE_HEADER));\r
+  ZeroMem (&VariableHeader, sizeof (AUTHENTICATED_VARIABLE_HEADER));\r
 \r
   if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
     //\r
@@ -622,11 +622,11 @@ ProcessVarWithKek (
   EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;\r
   BOOLEAN                         IsFound;\r
   UINT32                          Index;\r
-  VARIABLE_HEADER                 VariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER   VariableHeader;\r
   BOOLEAN                         Valid;\r
 \r
   KekList = NULL;\r
-  ZeroMem (&VariableHeader, sizeof (VARIABLE_HEADER));\r
+  ZeroMem (&VariableHeader, sizeof (AUTHENTICATED_VARIABLE_HEADER));\r
 \r
   if (mPlatformMode == USER_MODE) {\r
     if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
@@ -771,7 +771,7 @@ VerifyVariable (
   UINT8                           *PubKey;\r
   EFI_VARIABLE_AUTHENTICATION     *CertData;\r
   EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;\r
-  VARIABLE_HEADER                 VariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER   VariableHeader;\r
   BOOLEAN                         Valid;\r
 \r
   CertData    = NULL;\r
@@ -786,7 +786,7 @@ VerifyVariable (
   //\r
   // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.\r
   //\r
-  ZeroMem (&VariableHeader, sizeof (VARIABLE_HEADER));\r
+  ZeroMem (&VariableHeader, sizeof (AUTHENTICATED_VARIABLE_HEADER));\r
   if (Variable->CurrPtr != 0x0) {\r
     Valid = IsValidVariableHeader (\r
               Variable->CurrPtr, \r
index 0b2775dac89b0df303937ae2b391a041cb3e7593..f08adf038a2e52829c02551fe8f0b62f4590caa5 100644 (file)
@@ -347,7 +347,7 @@ GetVarStoreHeader (
                                  FALSE - Variable is non-volatile.\r
   @param[in]  Global             Pointer to VARAIBLE_GLOBAL structure.\r
   @param[in]  Instance           Instance of FV Block services.\r
-  @param[out] VariableHeader     Pointer to VARIABLE_HEADER for output.\r
+  @param[out] VariableHeader     Pointer to AUTHENTICATED_VARIABLE_HEADER for output.\r
 \r
   @retval TRUE                   Variable header is valid.\r
   @retval FALSE                  Variable header is not valid.\r
@@ -355,15 +355,15 @@ GetVarStoreHeader (
 **/\r
 BOOLEAN\r
 IsValidVariableHeader (\r
-  IN  EFI_PHYSICAL_ADDRESS   VariableAddress,\r
-  IN  BOOLEAN                Volatile,\r
-  IN  VARIABLE_GLOBAL        *Global,\r
-  IN  UINTN                  Instance,\r
-  OUT VARIABLE_HEADER        *VariableHeader  OPTIONAL\r
+  IN  EFI_PHYSICAL_ADDRESS              VariableAddress,\r
+  IN  BOOLEAN                           Volatile,\r
+  IN  VARIABLE_GLOBAL                   *Global,\r
+  IN  UINTN                             Instance,\r
+  OUT AUTHENTICATED_VARIABLE_HEADER     *VariableHeader  OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  VARIABLE_HEADER       LocalVariableHeader;\r
+  EFI_STATUS                            Status;\r
+  AUTHENTICATED_VARIABLE_HEADER         LocalVariableHeader;\r
 \r
   Status = AccessVariableStore (\r
              FALSE,\r
@@ -371,7 +371,7 @@ IsValidVariableHeader (
              Volatile,\r
              Instance,\r
              VariableAddress,\r
-             sizeof (VARIABLE_HEADER),\r
+             sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
              &LocalVariableHeader    \r
              );\r
 \r
@@ -380,7 +380,7 @@ IsValidVariableHeader (
   }\r
 \r
   if (VariableHeader != NULL) {\r
-    CopyMem (VariableHeader, &LocalVariableHeader, sizeof (VARIABLE_HEADER));\r
+    CopyMem (VariableHeader, &LocalVariableHeader, sizeof (AUTHENTICATED_VARIABLE_HEADER));\r
   }\r
 \r
   return TRUE;\r
@@ -439,7 +439,7 @@ GetVariableStoreStatus (
 **/\r
 UINTN\r
 NameSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
+  IN  AUTHENTICATED_VARIABLE_HEADER     *Variable\r
   )\r
 {\r
   if (Variable->State    == (UINT8) (-1) ||\r
@@ -465,7 +465,7 @@ NameSizeOfVariable (
 **/\r
 UINTN\r
 DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
+  IN  AUTHENTICATED_VARIABLE_HEADER     *Variable\r
   )\r
 {\r
   if (Variable->State    == (UINT8)  -1 ||\r
@@ -500,10 +500,10 @@ GetVariableNamePtr (
   OUT CHAR16                 *VariableName\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  Address;\r
-  VARIABLE_HEADER       VariableHeader;\r
-  BOOLEAN               IsValid;\r
+  EFI_STATUS                        Status;\r
+  EFI_PHYSICAL_ADDRESS              Address;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  BOOLEAN                           IsValid;\r
 \r
   IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
   ASSERT (IsValid);\r
@@ -511,7 +511,7 @@ GetVariableNamePtr (
   //\r
   // Name area follows variable header.\r
   //\r
-  Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
+  Address = VariableAddress + sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
 \r
   Status = AccessVariableStore (\r
              FALSE,\r
@@ -548,10 +548,10 @@ GetVariableDataPtr (
   OUT CHAR16                 *VariableData\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  Address;\r
-  VARIABLE_HEADER       VariableHeader;\r
-  BOOLEAN               IsValid;\r
+  EFI_STATUS                        Status;\r
+  EFI_PHYSICAL_ADDRESS              Address;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  BOOLEAN                           IsValid;\r
 \r
   IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
   ASSERT (IsValid);\r
@@ -560,7 +560,7 @@ GetVariableDataPtr (
   // Data area follows variable name.\r
   // Be careful about pad size for alignment\r
   //\r
-  Address =  VariableAddress + sizeof (VARIABLE_HEADER);\r
+  Address =  VariableAddress + sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   Address += NameSizeOfVariable (&VariableHeader);\r
   Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
 \r
@@ -601,8 +601,8 @@ GetNextVariablePtr (
   IN  UINTN                  Instance\r
   )\r
 {\r
-  EFI_PHYSICAL_ADDRESS  Address;\r
-  VARIABLE_HEADER       VariableHeader;\r
+  EFI_PHYSICAL_ADDRESS              Address;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
 \r
   if (!IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader)) {\r
     return 0x0;\r
@@ -611,7 +611,7 @@ GetNextVariablePtr (
   //\r
   // Header of next variable follows data area of this variable\r
   //\r
-  Address =  VariableAddress + sizeof (VARIABLE_HEADER);\r
+  Address =  VariableAddress + sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   Address += NameSizeOfVariable (&VariableHeader);\r
   Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
   Address += DataSizeOfVariable (&VariableHeader);\r
@@ -964,14 +964,14 @@ FindVariable (
   IN  UINTN                   Instance\r
   )\r
 {\r
-  EFI_PHYSICAL_ADDRESS    Variable[2];\r
-  EFI_PHYSICAL_ADDRESS    InDeletedVariable;\r
-  EFI_PHYSICAL_ADDRESS    VariableStoreHeader[2];\r
-  UINTN                   InDeletedStorageIndex;\r
-  UINTN                   Index;\r
-  CHAR16                  LocalVariableName[MAX_NAME_SIZE];\r
-  BOOLEAN                 Volatile;\r
-  VARIABLE_HEADER         VariableHeader;\r
+  EFI_PHYSICAL_ADDRESS              Variable[2];\r
+  EFI_PHYSICAL_ADDRESS              InDeletedVariable;\r
+  EFI_PHYSICAL_ADDRESS              VariableStoreHeader[2];\r
+  UINTN                             InDeletedStorageIndex;\r
+  UINTN                             Index;\r
+  CHAR16                            LocalVariableName[MAX_NAME_SIZE];\r
+  BOOLEAN                           Volatile;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
 \r
   //\r
   // 0: Volatile, 1: Non-Volatile\r
@@ -1120,24 +1120,24 @@ Reclaim (
   IN  EFI_PHYSICAL_ADDRESS  UpdatingVariable\r
   )\r
 {\r
-  EFI_PHYSICAL_ADDRESS  Variable;\r
-  EFI_PHYSICAL_ADDRESS  AddedVariable;\r
-  EFI_PHYSICAL_ADDRESS  NextVariable;\r
-  EFI_PHYSICAL_ADDRESS  NextAddedVariable;\r
-  VARIABLE_STORE_HEADER VariableStoreHeader;\r
-  VARIABLE_HEADER       VariableHeader;\r
-  VARIABLE_HEADER       AddedVariableHeader;\r
-  CHAR16                VariableName[MAX_NAME_SIZE];\r
-  CHAR16                AddedVariableName[MAX_NAME_SIZE];\r
-  UINT8                 *ValidBuffer;\r
-  UINTN                 MaximumBufferSize;\r
-  UINTN                 VariableSize;\r
-  UINTN                 NameSize;\r
-  UINT8                 *CurrPtr;\r
-  BOOLEAN               FoundAdded;\r
-  EFI_STATUS            Status;\r
-  VARIABLE_GLOBAL       *VariableGlobal;\r
-  UINT32                Instance;\r
+  EFI_PHYSICAL_ADDRESS              Variable;\r
+  EFI_PHYSICAL_ADDRESS              AddedVariable;\r
+  EFI_PHYSICAL_ADDRESS              NextVariable;\r
+  EFI_PHYSICAL_ADDRESS              NextAddedVariable;\r
+  VARIABLE_STORE_HEADER             VariableStoreHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER     AddedVariableHeader;\r
+  CHAR16                            VariableName[MAX_NAME_SIZE];\r
+  CHAR16                            AddedVariableName[MAX_NAME_SIZE];\r
+  UINT8                             *ValidBuffer;\r
+  UINTN                             MaximumBufferSize;\r
+  UINTN                             VariableSize;\r
+  UINTN                             NameSize;\r
+  UINT8                             *CurrPtr;\r
+  BOOLEAN                           FoundAdded;\r
+  EFI_STATUS                        Status;\r
+  VARIABLE_GLOBAL                   *VariableGlobal;\r
+  UINT32                            Instance;\r
 \r
   VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
   Instance = Global->FvbInstance;\r
@@ -1200,9 +1200,9 @@ Reclaim (
       VariableSize = NextVariable - Variable;\r
       CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
       CurrPtr += VariableSize;\r
-      if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+      if ((!IsVolatile) && ((((AUTHENTICATED_VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
         Global->HwErrVariableTotalSize += VariableSize;\r
-      } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+      } else if ((!IsVolatile) && ((((AUTHENTICATED_VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
         Global->CommonVariableTotalSize += VariableSize;\r
       }\r
     }\r
@@ -1254,12 +1254,12 @@ Reclaim (
           // 1. No valid instance of this variable exists.\r
           // 2. It is not the variable that is going to be updated.\r
           //\r
-          ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
+          ((AUTHENTICATED_VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
         }\r
         CurrPtr += VariableSize;\r
-        if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+        if ((!IsVolatile) && ((((AUTHENTICATED_VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
           Global->HwErrVariableTotalSize += VariableSize;\r
-        } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+        } else if ((!IsVolatile) && ((((AUTHENTICATED_VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
           Global->CommonVariableTotalSize += VariableSize;\r
         }\r
       }\r
@@ -1729,7 +1729,7 @@ AutoUpdateLangVariable(
                VariableGlobal,\r
                Variable.Volatile,\r
                Instance,\r
-               (UINTN) &(((VARIABLE_HEADER *)Variable.CurrPtr)->DataSize),\r
+               (UINTN) &(((AUTHENTICATED_VARIABLE_HEADER *)Variable.CurrPtr)->DataSize),\r
                sizeof (DataSize),\r
                &DataSize\r
                ); \r
@@ -1865,15 +1865,15 @@ UpdateVariable (
   )\r
 {\r
   EFI_STATUS                          Status;\r
-  VARIABLE_HEADER                     *NextVariable;\r
+  AUTHENTICATED_VARIABLE_HEADER       *NextVariable;\r
   UINTN                               VarNameOffset;\r
   UINTN                               VarDataOffset;\r
   UINTN                               VarNameSize;\r
   UINTN                               VarSize;\r
   BOOLEAN                             Volatile;\r
   UINT8                               State;\r
-  VARIABLE_HEADER                     VariableHeader;\r
-  VARIABLE_HEADER                     *NextVariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER       VariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER       *NextVariableHeader;\r
   BOOLEAN                             Valid;\r
   BOOLEAN                             Reclaimed;\r
   VARIABLE_STORE_HEADER               VariableStoreHeader;\r
@@ -1930,7 +1930,7 @@ UpdateVariable (
                  VariableGlobal,\r
                  Variable->Volatile,\r
                  Instance,\r
-                 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+                 (UINTN) &(((AUTHENTICATED_VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
                  sizeof (UINT8),\r
                  &State\r
                  ); \r
@@ -1946,7 +1946,7 @@ UpdateVariable (
     // then return to the caller immediately.\r
     //\r
     if (DataSizeOfVariable (&VariableHeader) == DataSize) {\r
-      NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
+      NextVariable = (AUTHENTICATED_VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
       GetVariableDataPtr (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, (CHAR16 *) NextVariable);\r
       if  (CompareMem (Data, (VOID *) NextVariable, DataSize) == 0) {\r
         UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
@@ -1968,7 +1968,7 @@ UpdateVariable (
                  VariableGlobal,\r
                  Variable->Volatile,\r
                  Instance,\r
-                 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+                 (UINTN) &(((AUTHENTICATED_VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
                  sizeof (UINT8),\r
                  &State\r
                  );      \r
@@ -2007,9 +2007,9 @@ UpdateVariable (
   // Tricky part: Use scratch data area at the end of volatile variable store\r
   // as a temporary storage.\r
   //\r
-  NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
+  NextVariable = (AUTHENTICATED_VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
   ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
-  NextVariableHeader = (VARIABLE_HEADER *) NextVariable;\r
+  NextVariableHeader = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;\r
 \r
   SetMem (NextVariableHeader, ScratchSize, 0xff);\r
 \r
@@ -2018,7 +2018,7 @@ UpdateVariable (
   NextVariableHeader->PubKeyIndex     = KeyIndex;\r
   NextVariableHeader->MonotonicCount  = MonotonicCount;\r
   NextVariableHeader->Reserved        = 0;\r
-  VarNameOffset                       = sizeof (VARIABLE_HEADER);\r
+  VarNameOffset                       = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   VarNameSize                         = StrSize (VariableName);\r
   CopyMem (\r
     (UINT8 *) ((UINTN)NextVariable + VarNameOffset),\r
@@ -2096,7 +2096,7 @@ UpdateVariable (
                FALSE,\r
                Instance,\r
                VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
-               sizeof (VARIABLE_HEADER),\r
+               sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
                );\r
 \r
@@ -2114,7 +2114,7 @@ UpdateVariable (
                FALSE,\r
                Instance,\r
                VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
-               sizeof (VARIABLE_HEADER),\r
+               sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
                );\r
 \r
@@ -2129,9 +2129,9 @@ UpdateVariable (
                VariableGlobal,\r
                FALSE,\r
                Instance,\r
-               VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
-               (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
-               (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
+               VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset + sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
+               (UINT32) VarSize - sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
+               (UINT8 *) NextVariable + sizeof (AUTHENTICATED_VARIABLE_HEADER)\r
                );\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -2147,7 +2147,7 @@ UpdateVariable (
                FALSE,\r
                Instance,\r
                VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
-               sizeof (VARIABLE_HEADER),\r
+               sizeof (AUTHENTICATED_VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
                );\r
 \r
@@ -2212,7 +2212,7 @@ UpdateVariable (
   // has already been eliminated, so no need to delete it.\r
   //\r
   if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != 0) {\r
-    State = ((VARIABLE_HEADER *)Variable->CurrPtr)->State;\r
+    State = ((AUTHENTICATED_VARIABLE_HEADER *)Variable->CurrPtr)->State;\r
     State &= VAR_DELETED;\r
 \r
     Status = AccessVariableStore (\r
@@ -2220,7 +2220,7 @@ UpdateVariable (
                VariableGlobal,\r
                Variable->Volatile,\r
                Instance,\r
-               (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+               (UINTN) &(((AUTHENTICATED_VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
                sizeof (UINT8),\r
                &State\r
                );\r
@@ -2277,13 +2277,13 @@ EsalGetVariable (
   IN      ESAL_VARIABLE_GLOBAL  *Global\r
   )\r
 {\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarDataSize;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_HEADER         VariableHeader;\r
-  BOOLEAN                 Valid;\r
-  VARIABLE_GLOBAL         *VariableGlobal;\r
-  UINT32                  Instance;\r
+  VARIABLE_POINTER_TRACK            Variable;\r
+  UINTN                             VarDataSize;\r
+  EFI_STATUS                        Status;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  BOOLEAN                           Valid;\r
+  VARIABLE_GLOBAL                   *VariableGlobal;\r
+  UINT32                            Instance;\r
 \r
   if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -2400,12 +2400,12 @@ EsalGetNextVariableName (
   IN      ESAL_VARIABLE_GLOBAL  *Global\r
   )\r
 {\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarNameSize;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_HEADER         VariableHeader;\r
-  VARIABLE_GLOBAL         *VariableGlobal;\r
-  UINT32                  Instance;\r
+  VARIABLE_POINTER_TRACK            Variable;\r
+  UINTN                             VarNameSize;\r
+  EFI_STATUS                        Status;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  VARIABLE_GLOBAL                   *VariableGlobal;\r
+  UINT32                            Instance;\r
 \r
   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -2607,7 +2607,7 @@ EsalSetVariable (
     // For variable for hardware error record, the size of the VariableName, including the Unicode Null\r
     // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.\r
     //\r
-    if (StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
+    if (StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (AUTHENTICATED_VARIABLE_HEADER)) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
     //\r
@@ -2623,7 +2623,7 @@ EsalSetVariable (
     // For variable not for hardware error record, the size of the VariableName, including the\r
     // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.\r
     //\r
-    if (StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER)) {\r
+    if (StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxVariableSize) - sizeof (AUTHENTICATED_VARIABLE_HEADER)) {\r
       return EFI_INVALID_PARAMETER;\r
     }  \r
   }  \r
@@ -2746,17 +2746,17 @@ EsalQueryVariableInfo (
   IN  ESAL_VARIABLE_GLOBAL   *Global\r
   )\r
 {\r
-  EFI_PHYSICAL_ADDRESS   Variable;\r
-  EFI_PHYSICAL_ADDRESS   NextVariable;\r
-  UINT64                 VariableSize;\r
-  EFI_PHYSICAL_ADDRESS   VariableStoreHeaderAddress;\r
-  BOOLEAN                Volatile;\r
-  VARIABLE_STORE_HEADER  VarStoreHeader;\r
-  VARIABLE_HEADER        VariableHeader;\r
-  UINT64                 CommonVariableTotalSize;\r
-  UINT64                 HwErrVariableTotalSize;\r
-  VARIABLE_GLOBAL        *VariableGlobal;\r
-  UINT32                 Instance;\r
+  EFI_PHYSICAL_ADDRESS              Variable;\r
+  EFI_PHYSICAL_ADDRESS              NextVariable;\r
+  UINT64                            VariableSize;\r
+  EFI_PHYSICAL_ADDRESS              VariableStoreHeaderAddress;\r
+  BOOLEAN                           Volatile;\r
+  VARIABLE_STORE_HEADER             VarStoreHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER     VariableHeader;\r
+  UINT64                            CommonVariableTotalSize;\r
+  UINT64                            HwErrVariableTotalSize;\r
+  VARIABLE_GLOBAL                   *VariableGlobal;\r
+  UINT32                            Instance;\r
 \r
   CommonVariableTotalSize = 0;\r
   HwErrVariableTotalSize = 0;\r
@@ -2818,7 +2818,7 @@ EsalQueryVariableInfo (
   //\r
   if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
     *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
-    *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
+    *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   } else {\r
     if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
       ASSERT (PcdGet32(PcdHwErrStorageSize) < VarStoreHeader.Size);\r
@@ -2828,7 +2828,7 @@ EsalQueryVariableInfo (
     //\r
     // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
     //\r
-    *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
+    *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   }\r
 \r
   //\r
@@ -2882,10 +2882,10 @@ EsalQueryVariableInfo (
     *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
   }\r
 \r
-  if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
+  if (*RemainingVariableStorageSize < sizeof (AUTHENTICATED_VARIABLE_HEADER)) {\r
     *MaximumVariableSize = 0;\r
-  } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
-    *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
+  } else if ((*RemainingVariableStorageSize - sizeof (AUTHENTICATED_VARIABLE_HEADER)) < *MaximumVariableSize) {\r
+    *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (AUTHENTICATED_VARIABLE_HEADER);\r
   }\r
 \r
   ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
@@ -2955,7 +2955,7 @@ FlushHob2Nv (
   EFI_STATUS                      Status;\r
   VOID                            *GuidHob;\r
   VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
-  VARIABLE_HEADER                 *VariableHeader;\r
+  AUTHENTICATED_VARIABLE_HEADER   *VariableHeader;\r
   //\r
   // Get HOB variable store.\r
   //\r
@@ -2970,11 +2970,11 @@ FlushHob2Nv (
       //\r
       // Flush the HOB variable to NV Variable storage.\r
       //\r
-      for ( VariableHeader = (VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1)\r
-          ; (VariableHeader < (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VariableStoreHeader + VariableStoreHeader->Size)\r
+      for ( VariableHeader = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (VariableStoreHeader + 1)\r
+          ; (VariableHeader < (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VariableStoreHeader + VariableStoreHeader->Size)\r
             &&\r
             (VariableHeader->StartId == VARIABLE_DATA))\r
-          ; VariableHeader = (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) (VariableHeader + 1)\r
+          ; VariableHeader = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) (VariableHeader + 1)\r
                            + VariableHeader->NameSize + GET_PAD_SIZE (VariableHeader->NameSize)\r
                            + VariableHeader->DataSize + GET_PAD_SIZE (VariableHeader->DataSize)\r
                            )\r
@@ -3198,7 +3198,7 @@ VariableCommonInitialize (
                        Instance\r
                        );\r
       VariableSize = NextVariable - Variable;\r
-      if ((((VARIABLE_HEADER *)Variable)->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+      if ((((AUTHENTICATED_VARIABLE_HEADER *)Variable)->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
         mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
       } else {\r
         mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
index 5e6690ed70fa272efae7ca86d1b5f98c1ca86fac..b32ef741bfd5dd69ffa541ae868674edfff7e039 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Internal header file for Extended SAL variable service module.\r
 \r
-Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials \r
 are licensed and made available under the terms and conditions of the BSD License \r
 which accompanies this distribution.  The full text of the license may be found at \r
@@ -66,7 +66,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 /// The maximum size of the public key database, restricted by maximum individal EFI \r
 /// varible size, and excluding the variable header and name size.\r
 ///\r
-#define MAX_KEYDB_SIZE  (FixedPcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER) - AUTHVAR_KEYDB_NAME_SIZE)\r
+#define MAX_KEYDB_SIZE  (FixedPcdGet32 (PcdMaxVariableSize) - sizeof (AUTHENTICATED_VARIABLE_HEADER) - AUTHVAR_KEYDB_NAME_SIZE)\r
 #define MAX_KEY_NUM     (MAX_KEYDB_SIZE / EFI_CERT_TYPE_RSA2048_SIZE)\r
 \r
 ///\r
@@ -432,7 +432,7 @@ GetVariableDataPtr (
 **/\r
 UINTN\r
 DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
+  IN  AUTHENTICATED_VARIABLE_HEADER     *Variable\r
   );\r
 \r
 /**\r
@@ -479,7 +479,7 @@ UpdateVariable (
                                  FALSE - Variable is non-volatile.\r
   @param[in]  Global             Pointer to VARAIBLE_GLOBAL structure.\r
   @param[in]  Instance           Instance of FV Block services.\r
-  @param[out] VariableHeader     Pointer to VARIABLE_HEADER for output.\r
+  @param[out] VariableHeader     Pointer to AUTHENTICATED_VARIABLE_HEADER for output.\r
 \r
   @retval TRUE                   Variable header is valid.\r
   @retval FALSE                  Variable header is not valid.\r
@@ -487,11 +487,11 @@ UpdateVariable (
 **/\r
 BOOLEAN\r
 IsValidVariableHeader (\r
-  IN  EFI_PHYSICAL_ADDRESS   VariableAddress,\r
-  IN  BOOLEAN                Volatile,\r
-  IN  VARIABLE_GLOBAL        *Global,\r
-  IN  UINTN                  Instance,\r
-  OUT VARIABLE_HEADER        *VariableHeader  OPTIONAL\r
+  IN  EFI_PHYSICAL_ADDRESS              VariableAddress,\r
+  IN  BOOLEAN                           Volatile,\r
+  IN  VARIABLE_GLOBAL                   *Global,\r
+  IN  UINTN                             Instance,\r
+  OUT AUTHENTICATED_VARIABLE_HEADER     *VariableHeader  OPTIONAL\r
   );\r
 \r
 /**\r
diff --git a/SecurityPkg/VariableAuthenticated/Pei/PeiVariableAuth.uni b/SecurityPkg/VariableAuthenticated/Pei/PeiVariableAuth.uni
deleted file mode 100644 (file)
index 0fdc805..0000000
Binary files a/SecurityPkg/VariableAuthenticated/Pei/PeiVariableAuth.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/Pei/PeiVariableExtra.uni b/SecurityPkg/VariableAuthenticated/Pei/PeiVariableExtra.uni
deleted file mode 100644 (file)
index c9f171b..0000000
Binary files a/SecurityPkg/VariableAuthenticated/Pei/PeiVariableExtra.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/Pei/Variable.c b/SecurityPkg/VariableAuthenticated/Pei/Variable.c
deleted file mode 100644 (file)
index 7ee8b88..0000000
+++ /dev/null
@@ -1,1087 +0,0 @@
-/** @file\r
-  Implement ReadOnly Variable Services required by PEIM and install PEI\r
-  ReadOnly Varaiable2 PPI. These services operates the non-volatile \r
-  storage space.\r
-\r
-Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-\r
-#include "Variable.h"\r
-\r
-//\r
-// Module globals\r
-//\r
-EFI_PEI_READ_ONLY_VARIABLE2_PPI mVariablePpi = {\r
-  PeiGetVariable,\r
-  PeiGetNextVariableName\r
-};\r
-\r
-EFI_PEI_PPI_DESCRIPTOR     mPpiListVariable = {\r
-  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
-  &gEfiPeiReadOnlyVariable2PpiGuid,\r
-  &mVariablePpi\r
-};\r
-\r
-/**\r
-  Provide the functionality of the variable services.\r
-  \r
-  @param  FileHandle   Handle of the file being invoked. \r
-                       Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().\r
-  @param  PeiServices  General purpose services available to every PEIM.\r
-\r
-  @retval EFI_SUCCESS  If the interface could be successfully installed\r
-  @retval Others       Returned from PeiServicesInstallPpi()\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeimInitializeVariableServices (\r
-  IN       EFI_PEI_FILE_HANDLE       FileHandle,\r
-  IN CONST EFI_PEI_SERVICES          **PeiServices\r
-  )\r
-{\r
-  return PeiServicesInstallPpi (&mPpiListVariable);\r
-}\r
-\r
-/**\r
-\r
-  Gets the pointer to the first variable header in given variable store area.\r
-\r
-  @param VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @return Pointer to the first variable header\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetStartPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-{\r
-  //\r
-  // The end of variable store\r
-  //\r
-  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
-}\r
-\r
-\r
-/**\r
-  This code gets the pointer to the last variable memory pointer byte.\r
-\r
-  @param  VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @return VARIABLE_HEADER* pointer to last unavailable Variable Header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-{\r
-  //\r
-  // The end of variable store\r
-  //\r
-  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
-}\r
-\r
-\r
-/**\r
-  This code checks if variable header is valid or not.\r
-\r
-  @param  Variable  Pointer to the Variable Header.\r
-\r
-  @retval TRUE      Variable header is valid.\r
-  @retval FALSE     Variable header is not valid.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidVariableHeader (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  if (Variable == NULL || Variable->StartId != VARIABLE_DATA ) {\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-\r
-/**\r
-  This code gets the size of name of variable.\r
-\r
-  @param  Variable  Pointer to the Variable Header.\r
-\r
-  @return Size of variable in bytes in type UINTN.\r
-\r
-**/\r
-UINTN\r
-NameSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  if (Variable->State    == (UINT8) (-1) ||\r
-      Variable->DataSize == (UINT32) (-1) ||\r
-      Variable->NameSize == (UINT32) (-1) ||\r
-      Variable->Attributes == (UINT32) (-1)) {\r
-    return 0;\r
-  }\r
-  return (UINTN) Variable->NameSize;\r
-}\r
-\r
-\r
-/**\r
-  This code gets the size of data of variable.\r
-\r
-  @param  Variable  Pointer to the Variable Header.\r
-\r
-  @return Size of variable in bytes in type UINTN.\r
-\r
-**/\r
-UINTN\r
-DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  if (Variable->State    == (UINT8)  (-1) ||\r
-      Variable->DataSize == (UINT32) (-1) ||\r
-      Variable->NameSize == (UINT32) (-1) ||\r
-      Variable->Attributes == (UINT32) (-1)) {\r
-    return 0;\r
-  }\r
-  return (UINTN) Variable->DataSize;\r
-}\r
-\r
-/**\r
-  This code gets the pointer to the variable name.\r
-\r
-  @param   Variable  Pointer to the Variable Header.\r
-\r
-  @return  A CHAR16* pointer to Variable Name.\r
-\r
-**/\r
-CHAR16 *\r
-GetVariableNamePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  return (CHAR16 *) (Variable + 1);\r
-}\r
-\r
-\r
-/**\r
-  This code gets the pointer to the variable data.\r
-\r
-  @param   Variable         Pointer to the Variable Header.\r
-  @param   VariableHeader   Pointer to the Variable Header that has consecutive content.\r
-\r
-  @return  A UINT8* pointer to Variable Data.\r
-\r
-**/\r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable,\r
-  IN  VARIABLE_HEADER   *VariableHeader\r
-  )\r
-{\r
-  UINTN Value;\r
-  \r
-  //\r
-  // Be careful about pad size for alignment\r
-  //\r
-  Value =  (UINTN) GetVariableNamePtr (Variable);\r
-  Value += NameSizeOfVariable (VariableHeader);\r
-  Value += GET_PAD_SIZE (NameSizeOfVariable (VariableHeader));\r
-\r
-  return (UINT8 *) Value;\r
-}\r
-\r
-\r
-/**\r
-  This code gets the pointer to the next variable header.\r
-\r
-  @param  StoreInfo         Pointer to variable store info structure.\r
-  @param  Variable          Pointer to the Variable Header.\r
-  @param  VariableHeader    Pointer to the Variable Header that has consecutive content.\r
-\r
-  @return  A VARIABLE_HEADER* pointer to next variable header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
-  IN  VARIABLE_STORE_INFO   *StoreInfo,\r
-  IN  VARIABLE_HEADER       *Variable,\r
-  IN  VARIABLE_HEADER       *VariableHeader\r
-  )\r
-{\r
-  EFI_PHYSICAL_ADDRESS  TargetAddress;\r
-  EFI_PHYSICAL_ADDRESS  SpareAddress;\r
-  UINTN                 Value;\r
-\r
-  Value =  (UINTN) GetVariableDataPtr (Variable, VariableHeader);\r
-  Value += DataSizeOfVariable (VariableHeader);\r
-  Value += GET_PAD_SIZE (DataSizeOfVariable (VariableHeader));\r
-  //\r
-  // Be careful about pad size for alignment\r
-  //\r
-  Value = HEADER_ALIGN (Value);\r
-\r
-  if (StoreInfo->FtwLastWriteData != NULL) {\r
-    TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
-    SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
-    if (((UINTN) Variable < (UINTN) TargetAddress) && (Value >= (UINTN) TargetAddress)) {\r
-      //\r
-      // Next variable is in spare block.\r
-      //\r
-      Value = (UINTN) SpareAddress + (Value - (UINTN) TargetAddress);\r
-    }\r
-  }\r
-\r
-  return (VARIABLE_HEADER *) Value;\r
-}\r
-\r
-/**\r
-  Get variable store status.\r
-\r
-  @param  VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @retval  EfiRaw      Variable store is raw\r
-  @retval  EfiValid    Variable store is valid\r
-  @retval  EfiInvalid  Variable store is invalid\r
-\r
-**/\r
-VARIABLE_STORE_STATUS\r
-GetVariableStoreStatus (\r
-  IN VARIABLE_STORE_HEADER *VarStoreHeader\r
-  )\r
-{\r
-  if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
-      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
-      VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
-      ) {\r
-\r
-    return EfiValid;\r
-  }\r
-\r
-  if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
-      ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
-      ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
-      ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
-      VarStoreHeader->Size == 0xffffffff &&\r
-      VarStoreHeader->Format == 0xff &&\r
-      VarStoreHeader->State == 0xff\r
-      ) {\r
-\r
-    return EfiRaw;\r
-  } else {\r
-    return EfiInvalid;\r
-  }\r
-}\r
-\r
-/**\r
-  Compare two variable names, one of them may be inconsecutive.\r
-\r
-  @param StoreInfo      Pointer to variable store info structure.\r
-  @param Name1          Pointer to one variable name.\r
-  @param Name2          Pointer to another variable name.\r
-  @param NameSize       Variable name size.\r
-\r
-  @retval TRUE          Name1 and Name2 are identical.\r
-  @retval FALSE         Name1 and Name2 are not identical.\r
-\r
-**/\r
-BOOLEAN\r
-CompareVariableName (\r
-  IN VARIABLE_STORE_INFO    *StoreInfo,\r
-  IN CONST CHAR16           *Name1,\r
-  IN CONST CHAR16           *Name2,\r
-  IN UINTN                  NameSize\r
-  )\r
-{\r
-  EFI_PHYSICAL_ADDRESS  TargetAddress;\r
-  EFI_PHYSICAL_ADDRESS  SpareAddress;\r
-  UINTN                 PartialNameSize;\r
-\r
-  if (StoreInfo->FtwLastWriteData != NULL) {\r
-    TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
-    SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
-    if (((UINTN) Name1 < (UINTN) TargetAddress) && (((UINTN) Name1 + NameSize) > (UINTN) TargetAddress)) {\r
-      //\r
-      // Name1 is inconsecutive.\r
-      //\r
-      PartialNameSize = (UINTN) TargetAddress - (UINTN) Name1;\r
-      //\r
-      // Partial content is in NV storage.\r
-      //\r
-      if (CompareMem ((UINT8 *) Name1, (UINT8 *) Name2, PartialNameSize) == 0) {\r
-        //\r
-        // Another partial content is in spare block.\r
-        //\r
-        if (CompareMem ((UINT8 *) (UINTN) SpareAddress, (UINT8 *) Name2 + PartialNameSize, NameSize - PartialNameSize) == 0) {\r
-          return TRUE;\r
-        }\r
-      }\r
-      return FALSE;\r
-    } else if (((UINTN) Name2 < (UINTN) TargetAddress) && (((UINTN) Name2 + NameSize) > (UINTN) TargetAddress)) {\r
-      //\r
-      // Name2 is inconsecutive.\r
-      //\r
-      PartialNameSize = (UINTN) TargetAddress - (UINTN) Name2;\r
-      //\r
-      // Partial content is in NV storage.\r
-      //\r
-      if (CompareMem ((UINT8 *) Name2, (UINT8 *) Name1, PartialNameSize) == 0) {\r
-        //\r
-        // Another partial content is in spare block.\r
-        //\r
-        if (CompareMem ((UINT8 *) (UINTN) SpareAddress, (UINT8 *) Name1 + PartialNameSize, NameSize - PartialNameSize) == 0) {\r
-          return TRUE;\r
-        }\r
-      }\r
-      return FALSE;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Both Name1 and Name2 are consecutive.\r
-  //\r
-  if (CompareMem ((UINT8 *) Name1, (UINT8 *) Name2, NameSize) == 0) {\r
-    return TRUE;\r
-  }\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-  This function compares a variable with variable entries in database.\r
-\r
-  @param  StoreInfo     Pointer to variable store info structure.\r
-  @param  Variable      Pointer to the variable in our database\r
-  @param  VariableHeader Pointer to the Variable Header that has consecutive content.\r
-  @param  VariableName  Name of the variable to compare to 'Variable'\r
-  @param  VendorGuid    GUID of the variable to compare to 'Variable'\r
-  @param  PtrTrack      Variable Track Pointer structure that contains Variable Information.\r
-\r
-  @retval EFI_SUCCESS    Found match variable\r
-  @retval EFI_NOT_FOUND  Variable not found\r
-\r
-**/\r
-EFI_STATUS\r
-CompareWithValidVariable (\r
-  IN  VARIABLE_STORE_INFO           *StoreInfo,\r
-  IN  VARIABLE_HEADER               *Variable,\r
-  IN  VARIABLE_HEADER               *VariableHeader,\r
-  IN  CONST CHAR16                  *VariableName,\r
-  IN  CONST EFI_GUID                *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK        *PtrTrack\r
-  )\r
-{\r
-  VOID  *Point;\r
-\r
-  if (VariableName[0] == 0) {\r
-    PtrTrack->CurrPtr = Variable;\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    //\r
-    // Don't use CompareGuid function here for performance reasons.\r
-    // Instead we compare the GUID a UINT32 at a time and branch\r
-    // on the first failed comparison.\r
-    //\r
-    if ((((INT32 *) VendorGuid)[0] == ((INT32 *) &VariableHeader->VendorGuid)[0]) &&\r
-        (((INT32 *) VendorGuid)[1] == ((INT32 *) &VariableHeader->VendorGuid)[1]) &&\r
-        (((INT32 *) VendorGuid)[2] == ((INT32 *) &VariableHeader->VendorGuid)[2]) &&\r
-        (((INT32 *) VendorGuid)[3] == ((INT32 *) &VariableHeader->VendorGuid)[3])\r
-        ) {\r
-      ASSERT (NameSizeOfVariable (VariableHeader) != 0);\r
-      Point = (VOID *) GetVariableNamePtr (Variable);\r
-      if (CompareVariableName (StoreInfo, VariableName, Point, NameSizeOfVariable (VariableHeader))) {\r
-        PtrTrack->CurrPtr = Variable;\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-/**\r
-  Return the variable store header and the store info based on the Index.\r
-\r
-  @param Type       The type of the variable store.\r
-  @param StoreInfo  Return the store info.\r
-\r
-  @return  Pointer to the variable store header.\r
-**/\r
-VARIABLE_STORE_HEADER *\r
-GetVariableStore (\r
-  IN VARIABLE_STORE_TYPE         Type,\r
-  OUT VARIABLE_STORE_INFO        *StoreInfo\r
-  )\r
-{\r
-  EFI_HOB_GUID_TYPE                     *GuidHob;\r
-  EFI_FIRMWARE_VOLUME_HEADER            *FvHeader;\r
-  VARIABLE_STORE_HEADER                 *VariableStoreHeader;\r
-  EFI_PHYSICAL_ADDRESS                  NvStorageBase;\r
-  UINT32                                NvStorageSize;\r
-  FAULT_TOLERANT_WRITE_LAST_WRITE_DATA  *FtwLastWriteData;\r
-  UINT32                                BackUpOffset;\r
-\r
-  StoreInfo->IndexTable = NULL;\r
-  StoreInfo->FtwLastWriteData = NULL;\r
-  VariableStoreHeader = NULL;\r
-  switch (Type) {\r
-    case VariableStoreTypeHob:\r
-      GuidHob     = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
-      if (GuidHob != NULL) {\r
-        VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
-      }\r
-      break;\r
-\r
-    case VariableStoreTypeNv:\r
-      if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE) {\r
-        //\r
-        // The content of NV storage for variable is not reliable in recovery boot mode.\r
-        //\r
-\r
-        NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
-        NvStorageBase = (EFI_PHYSICAL_ADDRESS) (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ? \r
-                                                PcdGet64 (PcdFlashNvStorageVariableBase64) : \r
-                                                PcdGet32 (PcdFlashNvStorageVariableBase)\r
-                                               );\r
-        //\r
-        // First let FvHeader point to NV storage base.\r
-        //\r
-        FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) NvStorageBase;\r
-\r
-        //\r
-        // Check the FTW last write data hob.\r
-        //\r
-        BackUpOffset = 0;\r
-        GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
-        if (GuidHob != NULL) {\r
-          FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
-          if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
-            //\r
-            // Let FvHeader point to spare block.\r
-            //\r
-            FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) FtwLastWriteData->SpareAddress;\r
-            DEBUG ((EFI_D_INFO, "PeiVariable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
-          } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) && (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
-            StoreInfo->FtwLastWriteData = FtwLastWriteData;\r
-            //\r
-            // Flash NV storage from the offset is backed up in spare block.\r
-            //\r
-            BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
-            DEBUG ((EFI_D_INFO, "PeiVariable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));\r
-            //\r
-            // At least one block data in flash NV storage is still valid, so still leave FvHeader point to NV storage base.\r
-            //\r
-          }\r
-        }\r
-\r
-        //\r
-        // Check if the Firmware Volume is not corrupted\r
-        //\r
-        if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
-          DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
-          break;\r
-        }\r
-        \r
-        VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);\r
-\r
-        GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);\r
-        if (GuidHob != NULL) {\r
-          StoreInfo->IndexTable = GET_GUID_HOB_DATA (GuidHob);\r
-        } else {\r
-          //\r
-          // If it's the first time to access variable region in flash, create a guid hob to record\r
-          // VAR_ADDED type variable info.\r
-          // Note that as the resource of PEI phase is limited, only store the limited number of \r
-          // VAR_ADDED type variables to reduce access time.\r
-          //\r
-          StoreInfo->IndexTable = (VARIABLE_INDEX_TABLE *) BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));\r
-          StoreInfo->IndexTable->Length      = 0;\r
-          StoreInfo->IndexTable->StartPtr    = GetStartPointer (VariableStoreHeader);\r
-          StoreInfo->IndexTable->EndPtr      = GetEndPointer   (VariableStoreHeader);\r
-          StoreInfo->IndexTable->GoneThrough = 0;\r
-        }\r
-      }\r
-      break;\r
-\r
-    default:\r
-      ASSERT (FALSE);\r
-      break;\r
-  }\r
-\r
-  StoreInfo->VariableStoreHeader = VariableStoreHeader;\r
-  return VariableStoreHeader;\r
-}\r
-\r
-/**\r
-  Get variable header that has consecutive content.\r
-\r
-  @param StoreInfo      Pointer to variable store info structure.\r
-  @param Variable       Pointer to the Variable Header.\r
-  @param VariableHeader Pointer to Pointer to the Variable Header that has consecutive content.\r
-\r
-  @retval TRUE          Variable header is valid.\r
-  @retval FALSE         Variable header is not valid.\r
-\r
-**/\r
-BOOLEAN\r
-GetVariableHeader (\r
-  IN VARIABLE_STORE_INFO    *StoreInfo,\r
-  IN VARIABLE_HEADER        *Variable,\r
-  OUT VARIABLE_HEADER       **VariableHeader\r
-  )\r
-{\r
-  EFI_PHYSICAL_ADDRESS  TargetAddress;\r
-  EFI_PHYSICAL_ADDRESS  SpareAddress;\r
-  EFI_HOB_GUID_TYPE     *GuidHob;\r
-  UINTN                 PartialHeaderSize;\r
-\r
-  if (Variable == NULL) {\r
-    return FALSE;\r
-  }\r
-\r
-   //\r
-   // First assume variable header pointed by Variable is consecutive.\r
-   //\r
-  *VariableHeader = Variable;\r
-\r
-  if (StoreInfo->FtwLastWriteData != NULL) {\r
-    TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
-    SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
-    if (((UINTN) Variable > (UINTN) SpareAddress) &&\r
-        (((UINTN) Variable - (UINTN) SpareAddress + (UINTN) TargetAddress) >= (UINTN) GetEndPointer (StoreInfo->VariableStoreHeader))) {\r
-      //\r
-      // Reach the end of variable store.\r
-      //\r
-      return FALSE;\r
-    }\r
-    if (((UINTN) Variable < (UINTN) TargetAddress) && (((UINTN) Variable + sizeof (VARIABLE_HEADER)) > (UINTN) TargetAddress)) {\r
-      //\r
-      // Variable header pointed by Variable is inconsecutive,\r
-      // create a guid hob to combine the two partial variable header content together.\r
-      //\r
-      GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);\r
-      if (GuidHob != NULL) {\r
-        *VariableHeader = (VARIABLE_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
-      } else {\r
-        *VariableHeader = (VARIABLE_HEADER *) BuildGuidHob (&gEfiCallerIdGuid, sizeof (VARIABLE_HEADER));\r
-        PartialHeaderSize = (UINTN) TargetAddress - (UINTN) Variable;\r
-        //\r
-        // Partial content is in NV storage.\r
-        //\r
-        CopyMem ((UINT8 *) *VariableHeader, (UINT8 *) Variable, PartialHeaderSize);\r
-        //\r
-        // Another partial content is in spare block.\r
-        //\r
-        CopyMem ((UINT8 *) *VariableHeader + PartialHeaderSize, (UINT8 *) (UINTN) SpareAddress, sizeof (VARIABLE_HEADER) - PartialHeaderSize);\r
-      }\r
-    }\r
-  } else {\r
-    if (Variable >= GetEndPointer (StoreInfo->VariableStoreHeader)) {\r
-      //\r
-      // Reach the end of variable store.\r
-      //\r
-      return FALSE;\r
-    }\r
-  }\r
-\r
-  return IsValidVariableHeader (*VariableHeader);\r
-}\r
-\r
-/**\r
-  Get variable name or data to output buffer.\r
-\r
-  @param  StoreInfo     Pointer to variable store info structure.\r
-  @param  NameOrData    Pointer to the variable name/data that may be inconsecutive.\r
-  @param  Size          Variable name/data size.\r
-  @param  Buffer        Pointer to output buffer to hold the variable name/data.\r
-\r
-**/\r
-VOID\r
-GetVariableNameOrData (\r
-  IN VARIABLE_STORE_INFO    *StoreInfo,\r
-  IN UINT8                  *NameOrData,\r
-  IN UINTN                  Size,\r
-  OUT UINT8                 *Buffer\r
-  )\r
-{\r
-  EFI_PHYSICAL_ADDRESS  TargetAddress;\r
-  EFI_PHYSICAL_ADDRESS  SpareAddress;\r
-  UINTN                 PartialSize;\r
\r
-  if (StoreInfo->FtwLastWriteData != NULL) {\r
-    TargetAddress = StoreInfo->FtwLastWriteData->TargetAddress;\r
-    SpareAddress = StoreInfo->FtwLastWriteData->SpareAddress;\r
-    if (((UINTN) NameOrData < (UINTN) TargetAddress) && (((UINTN) NameOrData + Size) > (UINTN) TargetAddress)) {\r
-      //\r
-      // Variable name/data is inconsecutive.\r
-      //\r
-      PartialSize = (UINTN) TargetAddress - (UINTN) NameOrData;\r
-      //\r
-      // Partial content is in NV storage.\r
-      //\r
-      CopyMem (Buffer, NameOrData, PartialSize);\r
-      //\r
-      // Another partial content is in spare block.\r
-      //\r
-      CopyMem (Buffer + PartialSize, (UINT8 *) (UINTN) SpareAddress, Size - PartialSize);\r
-      return;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Variable name/data is consecutive.\r
-  //\r
-  CopyMem (Buffer, NameOrData, Size);\r
-}\r
-\r
-/**\r
-  Find the variable in the specified variable store.\r
-\r
-  @param  StoreInfo           Pointer to the store info structure.\r
-  @param  VariableName        Name of the variable to be found\r
-  @param  VendorGuid          Vendor GUID to be found.\r
-  @param  PtrTrack            Variable Track Pointer structure that contains Variable Information.\r
-\r
-  @retval  EFI_SUCCESS            Variable found successfully\r
-  @retval  EFI_NOT_FOUND          Variable not found\r
-  @retval  EFI_INVALID_PARAMETER  Invalid variable name\r
-\r
-**/\r
-EFI_STATUS\r
-FindVariableEx (\r
-  IN VARIABLE_STORE_INFO         *StoreInfo,\r
-  IN CONST CHAR16                *VariableName,\r
-  IN CONST EFI_GUID              *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK     *PtrTrack\r
-  )\r
-{\r
-  VARIABLE_HEADER         *Variable;\r
-  VARIABLE_HEADER         *LastVariable;\r
-  VARIABLE_HEADER         *MaxIndex;\r
-  UINTN                   Index;\r
-  UINTN                   Offset;\r
-  BOOLEAN                 StopRecord;\r
-  VARIABLE_HEADER         *InDeletedVariable;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader;\r
-  VARIABLE_INDEX_TABLE    *IndexTable;\r
-  VARIABLE_HEADER         *VariableHeader;\r
-\r
-  VariableStoreHeader = StoreInfo->VariableStoreHeader;\r
-\r
-  if (VariableStoreHeader == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
-  if (~VariableStoreHeader->Size == 0) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  IndexTable = StoreInfo->IndexTable;\r
-  PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);\r
-  PtrTrack->EndPtr   = GetEndPointer   (VariableStoreHeader);\r
-\r
-  InDeletedVariable = NULL;\r
-\r
-  //\r
-  // No Variable Address equals zero, so 0 as initial value is safe.\r
-  //\r
-  MaxIndex   = NULL;\r
-  VariableHeader = NULL;\r
-\r
-  if (IndexTable != NULL) {\r
-    //\r
-    // traverse the variable index table to look for varible.\r
-    // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.\r
-    //\r
-    for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {\r
-      ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));\r
-      Offset   += IndexTable->Index[Index];\r
-      MaxIndex  = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);\r
-      GetVariableHeader (StoreInfo, MaxIndex, &VariableHeader);\r
-      if (CompareWithValidVariable (StoreInfo, MaxIndex, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
-        if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-          InDeletedVariable = PtrTrack->CurrPtr;\r
-        } else {\r
-          return EFI_SUCCESS;\r
-        }\r
-      }\r
-    }\r
-\r
-    if (IndexTable->GoneThrough != 0) {\r
-      //\r
-      // If the table has all the existing variables indexed, return.\r
-      //\r
-      PtrTrack->CurrPtr = InDeletedVariable;\r
-      return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
-    }\r
-  }\r
-\r
-  if (MaxIndex != NULL) {\r
-    //\r
-    // HOB exists but the variable cannot be found in HOB\r
-    // If not found in HOB, then let's start from the MaxIndex we've found.\r
-    //\r
-    Variable     = GetNextVariablePtr (StoreInfo, MaxIndex, VariableHeader);\r
-    LastVariable = MaxIndex;\r
-  } else {\r
-    //\r
-    // Start Pointers for the variable.\r
-    // Actual Data Pointer where data can be written.\r
-    //\r
-    Variable     = PtrTrack->StartPtr;\r
-    LastVariable = PtrTrack->StartPtr;\r
-  }\r
-\r
-  //\r
-  // Find the variable by walk through variable store\r
-  //\r
-  StopRecord = FALSE;\r
-  while (GetVariableHeader (StoreInfo, Variable, &VariableHeader)) {\r
-    if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-      //\r
-      // Record Variable in VariableIndex HOB\r
-      //\r
-      if ((IndexTable != NULL) && !StopRecord) {\r
-        Offset = (UINTN) Variable - (UINTN) LastVariable;\r
-        if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {\r
-          //\r
-          // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),\r
-          // or the record buffer is full.\r
-          //\r
-          StopRecord = TRUE;\r
-        } else {\r
-          IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;\r
-          LastVariable = Variable;\r
-        }\r
-      }\r
-\r
-      if (CompareWithValidVariable (StoreInfo, Variable, VariableHeader, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
-        if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-          InDeletedVariable = PtrTrack->CurrPtr;\r
-        } else {\r
-          return EFI_SUCCESS;\r
-        }\r
-      }\r
-    }\r
-\r
-    Variable = GetNextVariablePtr (StoreInfo, Variable, VariableHeader);\r
-  }\r
-  //\r
-  // If gone through the VariableStore, that means we never find in Firmware any more.\r
-  //\r
-  if ((IndexTable != NULL) && !StopRecord) {\r
-    IndexTable->GoneThrough = 1;\r
-  }\r
-\r
-  PtrTrack->CurrPtr = InDeletedVariable;\r
-\r
-  return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Find the variable in HOB and Non-Volatile variable storages.\r
-\r
-  @param  VariableName  Name of the variable to be found\r
-  @param  VendorGuid    Vendor GUID to be found.\r
-  @param  PtrTrack      Variable Track Pointer structure that contains Variable Information.\r
-  @param  StoreInfo     Return the store info.\r
-\r
-  @retval  EFI_SUCCESS            Variable found successfully\r
-  @retval  EFI_NOT_FOUND          Variable not found\r
-  @retval  EFI_INVALID_PARAMETER  Invalid variable name\r
-**/\r
-EFI_STATUS\r
-FindVariable (\r
-  IN CONST  CHAR16            *VariableName,\r
-  IN CONST  EFI_GUID          *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack,\r
-  OUT VARIABLE_STORE_INFO     *StoreInfo\r
-  )\r
-{\r
-  EFI_STATUS                  Status;\r
-  VARIABLE_STORE_TYPE         Type;\r
-\r
-  if (VariableName[0] != 0 && VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
-    GetVariableStore (Type, StoreInfo);\r
-    Status = FindVariableEx (\r
-               StoreInfo,\r
-               VariableName,\r
-               VendorGuid, \r
-               PtrTrack\r
-               );\r
-    if (!EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-/**\r
-  This service retrieves a variable's value using its name and GUID.\r
-\r
-  Read the specified variable from the UEFI variable store. If the Data \r
-  buffer is too small to hold the contents of the variable, the error\r
-  EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer\r
-  size to obtain the data.\r
-\r
-  @param  This                  A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
-  @param  VariableName          A pointer to a null-terminated string that is the variable's name.\r
-  @param  VariableGuid          A pointer to an EFI_GUID that is the variable's GUID. The combination of\r
-                                VariableGuid and VariableName must be unique.\r
-  @param  Attributes            If non-NULL, on return, points to the variable's attributes.\r
-  @param  DataSize              On entry, points to the size in bytes of the Data buffer.\r
-                                On return, points to the size of the data returned in Data.\r
-  @param  Data                  Points to the buffer which will hold the returned variable value.\r
-\r
-  @retval EFI_SUCCESS           The variable was read successfully.\r
-  @retval EFI_NOT_FOUND         The variable could not be found.\r
-  @retval EFI_BUFFER_TOO_SMALL  The DataSize is too small for the resulting data. \r
-                                DataSize is updated with the size required for \r
-                                the specified variable.\r
-  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiGetVariable (\r
-  IN CONST  EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
-  IN CONST  CHAR16                          *VariableName,\r
-  IN CONST  EFI_GUID                        *VariableGuid,\r
-  OUT       UINT32                          *Attributes,\r
-  IN OUT    UINTN                           *DataSize,\r
-  OUT       VOID                            *Data\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarDataSize;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_STORE_INFO     StoreInfo;\r
-  VARIABLE_HEADER         *VariableHeader;\r
-\r
-  if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableHeader = NULL;\r
-\r
-  //\r
-  // Find existing variable\r
-  //\r
-  Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);\r
-\r
-  //\r
-  // Get data size\r
-  //\r
-  VarDataSize = DataSizeOfVariable (VariableHeader);\r
-  if (*DataSize >= VarDataSize) {\r
-    if (Data == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
-    GetVariableNameOrData (&StoreInfo, GetVariableDataPtr (Variable.CurrPtr, VariableHeader), VarDataSize, Data);\r
-\r
-    if (Attributes != NULL) {\r
-      *Attributes = VariableHeader->Attributes;\r
-    }\r
-\r
-    *DataSize = VarDataSize;\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    *DataSize = VarDataSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-}\r
-\r
-/**\r
-  Return the next variable name and GUID.\r
-\r
-  This function is called multiple times to retrieve the VariableName \r
-  and VariableGuid of all variables currently available in the system. \r
-  On each call, the previous results are passed into the interface, \r
-  and, on return, the interface returns the data for the next \r
-  interface. When the entire variable list has been returned, \r
-  EFI_NOT_FOUND is returned.\r
-\r
-  @param  This              A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
-\r
-  @param  VariableNameSize  On entry, points to the size of the buffer pointed to by VariableName.\r
-                            On return, the size of the variable name buffer.\r
-  @param  VariableName      On entry, a pointer to a null-terminated string that is the variable's name.\r
-                            On return, points to the next variable's null-terminated name string.\r
-  @param  VariableGuid      On entry, a pointer to an EFI_GUID that is the variable's GUID. \r
-                            On return, a pointer to the next variable's GUID.\r
-\r
-  @retval EFI_SUCCESS           The variable was read successfully.\r
-  @retval EFI_NOT_FOUND         The variable could not be found.\r
-  @retval EFI_BUFFER_TOO_SMALL  The VariableNameSize is too small for the resulting\r
-                                data. VariableNameSize is updated with the size\r
-                                required for the specified variable.\r
-  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or\r
-                                VariableNameSize is NULL.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiGetNextVariableName (\r
-  IN CONST  EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
-  IN OUT UINTN                              *VariableNameSize,\r
-  IN OUT CHAR16                             *VariableName,\r
-  IN OUT EFI_GUID                           *VariableGuid\r
-  )\r
-{\r
-  VARIABLE_STORE_TYPE     Type;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  VARIABLE_POINTER_TRACK  VariableInHob;\r
-  VARIABLE_POINTER_TRACK  VariablePtrTrack;\r
-  UINTN                   VarNameSize;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];\r
-  VARIABLE_HEADER         *VariableHeader;\r
-  VARIABLE_STORE_INFO     StoreInfo;\r
-  VARIABLE_STORE_INFO     StoreInfoForNv;\r
-  VARIABLE_STORE_INFO     StoreInfoForHob;\r
-\r
-  if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableHeader = NULL;\r
-\r
-  Status = FindVariable (VariableName, VariableGuid, &Variable, &StoreInfo);\r
-  if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {\r
-    return Status;\r
-  }\r
-\r
-  if (VariableName[0] != 0) {\r
-    //\r
-    // If variable name is not NULL, get next variable\r
-    //\r
-    GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader);\r
-    Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
-  }\r
-\r
-  VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, &StoreInfoForHob);\r
-  VariableStoreHeader[VariableStoreTypeNv]  = GetVariableStore (VariableStoreTypeNv, &StoreInfoForNv);\r
-\r
-  while (TRUE) {\r
-    //\r
-    // Switch from HOB to Non-Volatile.\r
-    //\r
-    while (!GetVariableHeader (&StoreInfo, Variable.CurrPtr, &VariableHeader)) {\r
-      //\r
-      // Find current storage index\r
-      //\r
-      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
-        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
-          break;\r
-        }\r
-      }\r
-      ASSERT (Type < VariableStoreTypeMax);\r
-      //\r
-      // Switch to next storage\r
-      //\r
-      for (Type++; Type < VariableStoreTypeMax; Type++) {\r
-        if (VariableStoreHeader[Type] != NULL) {\r
-          break;\r
-        }\r
-      }\r
-      //\r
-      // Capture the case that \r
-      // 1. current storage is the last one, or\r
-      // 2. no further storage\r
-      //\r
-      if (Type == VariableStoreTypeMax) {\r
-        return EFI_NOT_FOUND;\r
-      }\r
-      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
-      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);\r
-      Variable.CurrPtr  = Variable.StartPtr;\r
-      GetVariableStore (Type, &StoreInfo);\r
-    }\r
-\r
-    if (VariableHeader->State == VAR_ADDED || VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-      if (VariableHeader->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-        //\r
-        // If it is a IN_DELETED_TRANSITION variable,\r
-        // and there is also a same ADDED one at the same time,\r
-        // don't return it.\r
-        //\r
-        Status = FindVariableEx (\r
-                   &StoreInfo,\r
-                   GetVariableNamePtr (Variable.CurrPtr),\r
-                   &VariableHeader->VendorGuid,\r
-                   &VariablePtrTrack\r
-                   );\r
-        if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr != Variable.CurrPtr) {\r
-          Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
-          continue;\r
-        }\r
-      }\r
-\r
-      //\r
-      // Don't return NV variable when HOB overrides it\r
-      //\r
-      if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
-          (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
-         ) {\r
-        Status = FindVariableEx (\r
-                   &StoreInfoForHob,\r
-                   GetVariableNamePtr (Variable.CurrPtr),\r
-                   &VariableHeader->VendorGuid, \r
-                   &VariableInHob\r
-                   );\r
-        if (!EFI_ERROR (Status)) {\r
-          Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
-          continue;\r
-        }\r
-      }\r
-\r
-      VarNameSize = NameSizeOfVariable (VariableHeader);\r
-      ASSERT (VarNameSize != 0);\r
-\r
-      if (VarNameSize <= *VariableNameSize) {\r
-        GetVariableNameOrData (&StoreInfo, (UINT8 *) GetVariableNamePtr (Variable.CurrPtr), VarNameSize, (UINT8 *) VariableName);\r
-\r
-        CopyMem (VariableGuid, &VariableHeader->VendorGuid, sizeof (EFI_GUID));\r
-\r
-        Status = EFI_SUCCESS;\r
-      } else {\r
-        Status = EFI_BUFFER_TOO_SMALL;\r
-      }\r
-\r
-      *VariableNameSize = VarNameSize;\r
-      //\r
-      // Variable is found\r
-      //\r
-      return Status;\r
-    } else {\r
-      Variable.CurrPtr = GetNextVariablePtr (&StoreInfo, Variable.CurrPtr, VariableHeader);\r
-    }\r
-  }\r
-}\r
diff --git a/SecurityPkg/VariableAuthenticated/Pei/Variable.h b/SecurityPkg/VariableAuthenticated/Pei/Variable.h
deleted file mode 100644 (file)
index 7693522..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-/** @file\r
-  The internal header file includes the common header files, defines\r
-  internal structure and functions used by PeiVariable module.\r
-\r
-Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#ifndef _PEI_VARIABLE_H_\r
-#define _PEI_VARIABLE_H_\r
-\r
-#include <PiPei.h>\r
-#include <Ppi/ReadOnlyVariable2.h>\r
-\r
-#include <Library/DebugLib.h>\r
-#include <Library/PeimEntryPoint.h>\r
-#include <Library/HobLib.h>\r
-#include <Library/PcdLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/PeiServicesTablePointerLib.h>\r
-#include <Library/PeiServicesLib.h>\r
-\r
-#include <Guid/AuthenticatedVariableFormat.h>\r
-#include <Guid/VariableIndexTable.h>\r
-#include <Guid/SystemNvDataGuid.h>\r
-#include <Guid/FaultTolerantWrite.h>\r
-\r
-typedef enum {\r
-  VariableStoreTypeHob,\r
-  VariableStoreTypeNv,\r
-  VariableStoreTypeMax\r
-} VARIABLE_STORE_TYPE;\r
-\r
-typedef struct {\r
-  VARIABLE_STORE_HEADER                   *VariableStoreHeader;\r
-  VARIABLE_INDEX_TABLE                    *IndexTable;\r
-  //\r
-  // If it is not NULL, it means there may be an inconsecutive variable whose\r
-  // partial content is still in NV storage, but another partial content is backed up\r
-  // in spare block.\r
-  //\r
-  FAULT_TOLERANT_WRITE_LAST_WRITE_DATA    *FtwLastWriteData;\r
-} VARIABLE_STORE_INFO;\r
-\r
-//\r
-// Functions\r
-//\r
-/**\r
-  Provide the functionality of the variable services.\r
-  \r
-  @param  FileHandle  Handle of the file being invoked. \r
-                      Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().\r
-  @param  PeiServices  General purpose services available to every PEIM.\r
-\r
-  @retval EFI_SUCCESS  If the interface could be successfully installed\r
-  @retval Others       Returned from PeiServicesInstallPpi()\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeimInitializeVariableServices (\r
-  IN       EFI_PEI_FILE_HANDLE       FileHandle,\r
-  IN CONST EFI_PEI_SERVICES          **PeiServices\r
-  );\r
-\r
-/**\r
-  This service retrieves a variable's value using its name and GUID.\r
-\r
-  Read the specified variable from the UEFI variable store. If the Data \r
-  buffer is too small to hold the contents of the variable, the error\r
-  EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer\r
-  size to obtain the data.\r
-\r
-  @param  This                  A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
-  @param  VariableName          A pointer to a null-terminated string that is the variable's name.\r
-  @param  VariableGuid          A pointer to an EFI_GUID that is the variable's GUID. The combination of\r
-                                VariableGuid and VariableName must be unique.\r
-  @param  Attributes            If non-NULL, on return, points to the variable's attributes.\r
-  @param  DataSize              On entry, points to the size in bytes of the Data buffer.\r
-                                On return, points to the size of the data returned in Data.\r
-  @param  Data                  Points to the buffer which will hold the returned variable value.\r
-\r
-  @retval EFI_SUCCESS           The variable was read successfully.\r
-  @retval EFI_NOT_FOUND         The variable could not be found.\r
-  @retval EFI_BUFFER_TOO_SMALL  The DataSize is too small for the resulting data. \r
-                                DataSize is updated with the size required for \r
-                                the specified variable.\r
-  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiGetVariable (\r
-  IN CONST  EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
-  IN CONST  CHAR16                          *VariableName,\r
-  IN CONST  EFI_GUID                        *VariableGuid,\r
-  OUT       UINT32                          *Attributes,\r
-  IN OUT    UINTN                           *DataSize,\r
-  OUT       VOID                            *Data\r
-  );\r
-\r
-/**\r
-  Return the next variable name and GUID.\r
-\r
-  This function is called multiple times to retrieve the VariableName \r
-  and VariableGuid of all variables currently available in the system. \r
-  On each call, the previous results are passed into the interface, \r
-  and, on return, the interface returns the data for the next \r
-  interface. When the entire variable list has been returned, \r
-  EFI_NOT_FOUND is returned.\r
-\r
-  @param  This              A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
-\r
-  @param  VariableNameSize  On entry, points to the size of the buffer pointed to by VariableName.\r
-  @param  VariableName      On entry, a pointer to a null-terminated string that is the variable's name.\r
-                            On return, points to the next variable's null-terminated name string.\r
-\r
-  @param  VariableGuid      On entry, a pointer to an UEFI _GUID that is the variable's GUID. \r
-                            On return, a pointer to the next variable's GUID.\r
-\r
-  @retval EFI_SUCCESS           The variable was read successfully.\r
-  @retval EFI_NOT_FOUND         The variable could not be found.\r
-  @retval EFI_BUFFER_TOO_SMALL  The VariableNameSize is too small for the resulting\r
-                                data. VariableNameSize is updated with the size\r
-                                required for the specified variable.\r
-  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or\r
-                                VariableNameSize is NULL.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiGetNextVariableName (\r
-  IN CONST  EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,\r
-  IN OUT UINTN                              *VariableNameSize,\r
-  IN OUT CHAR16                             *VariableName,\r
-  IN OUT EFI_GUID                           *VariableGuid\r
-  );\r
-\r
-#endif\r
diff --git a/SecurityPkg/VariableAuthenticated/Pei/VariablePei.inf b/SecurityPkg/VariableAuthenticated/Pei/VariablePei.inf
deleted file mode 100644 (file)
index fa0b5e9..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-## @file\r
-#  Implements ReadOnly Variable Services required by PEIM and installs PEI ReadOnly Varaiable2 PPI\r
-#  This module implements ReadOnly Variable Services required by PEIM and installs PEI ReadOnly Varaiable2 PPI.\r
-#\r
-# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-##\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = PeiVariableAuth\r
-  MODULE_UNI_FILE                = PeiVariableAuth.uni\r
-  FILE_GUID                      = B1F7AF2F-2807-478c-A893-2BF4DDD1F62B\r
-  MODULE_TYPE                    = PEIM\r
-  VERSION_STRING                 = 1.0\r
-  ENTRY_POINT                    = PeimInitializeVariableServices\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-[Sources]\r
-  Variable.c\r
-  Variable.h\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  SecurityPkg/SecurityPkg.dec\r
-\r
-[LibraryClasses]\r
-  BaseMemoryLib\r
-  PcdLib\r
-  HobLib\r
-  PeimEntryPoint\r
-  DebugLib\r
-  PeiServicesTablePointerLib\r
-  PeiServicesLib\r
-\r
-[Guids]\r
-  ## CONSUMES             ## GUID # Variable store header\r
-  ## SOMETIMES_CONSUMES   ## HOB\r
-  gEfiAuthenticatedVariableGuid\r
-  ## SOMETIMES_PRODUCES   ## HOB\r
-  ## SOMETIMES_CONSUMES   ## HOB\r
-  gEfiVariableIndexTableGuid\r
-  gEfiSystemNvDataFvGuid            ## SOMETIMES_CONSUMES   ## GUID\r
-  gEdkiiFaultTolerantWriteGuid      ## SOMETIMES_CONSUMES   ## HOB\r
-\r
-[Ppis]\r
-  gEfiPeiReadOnlyVariable2PpiGuid   ## PRODUCES\r
-\r
-[Pcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase      ## SOMETIMES_CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64    ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## CONSUMES\r
-\r
-[Depex]\r
-  gEdkiiFaultTolerantWriteGuid\r
-\r
-# [BootMode]\r
-# RECOVERY_FULL             ## SOMETIMES_CONSUMES\r
-\r
-[UserExtensions.TianoCore."ExtraFiles"]\r
-  PeiVariableExtra.uni
\ No newline at end of file
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.c
deleted file mode 100644 (file)
index 9599c8a..0000000
+++ /dev/null
@@ -1,2532 +0,0 @@
-/** @file\r
-  Implement authentication services for the authenticated variable\r
-  service in UEFI2.2.\r
-\r
-  Caution: This module requires additional review when modified.\r
-  This driver will have external input - variable data. It may be input in SMM mode.\r
-  This external input must be validated carefully to avoid security issue like\r
-  buffer overflow, integer overflow.\r
-  Variable attribute should also be checked to avoid authentication bypass.\r
-     The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
-  which is assumed to be protected by platform.  All variable code and metadata in flash/SMM Memory\r
-  may not be modified without authorization. If platform fails to protect these resources,\r
-  the authentication service provided in this driver will be broken, and the behavior is undefined.\r
-\r
-  ProcessVarWithPk(), ProcessVarWithKek() and ProcessVariable() are the function to do\r
-  variable authentication.\r
-\r
-  VerifyTimeBasedPayload() and VerifyCounterBasedPayload() are sub function to do verification.\r
-  They will do basic validation for authentication data structure, then call crypto library\r
-  to verify the signature.\r
-\r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-#include "AuthService.h"\r
-\r
-///\r
-/// Global database array for scratch\r
-///\r
-UINT8    *mPubKeyStore;\r
-UINT32   mPubKeyNumber;\r
-UINT32   mMaxKeyNumber;\r
-UINT32   mMaxKeyDbSize;\r
-UINT8    *mCertDbStore;\r
-UINT32   mMaxCertDbSize;\r
-UINT32   mPlatformMode;\r
-UINT8    mVendorKeyState;\r
-\r
-EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID};\r
-//\r
-// Public Exponent of RSA Key.\r
-//\r
-CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
-//\r
-// Hash context pointer\r
-//\r
-VOID  *mHashCtx = NULL;\r
-\r
-//\r
-// Requirement for different signature type which have been defined in UEFI spec.\r
-// These data are used to peform SignatureList format check while setting PK/KEK variable.\r
-//\r
-EFI_SIGNATURE_ITEM mSupportSigItem[] = {\r
-//{SigType,                       SigHeaderSize,   SigDataSize  }\r
-  {EFI_CERT_SHA256_GUID,          0,               32           },\r
-  {EFI_CERT_RSA2048_GUID,         0,               256          },\r
-  {EFI_CERT_RSA2048_SHA256_GUID,  0,               256          },\r
-  {EFI_CERT_SHA1_GUID,            0,               20           },\r
-  {EFI_CERT_RSA2048_SHA1_GUID,    0,               256          },\r
-  {EFI_CERT_X509_GUID,            0,               ((UINT32) ~0)},\r
-  {EFI_CERT_SHA224_GUID,          0,               28           },\r
-  {EFI_CERT_SHA384_GUID,          0,               48           },\r
-  {EFI_CERT_SHA512_GUID,          0,               64           },\r
-  {EFI_CERT_X509_SHA256_GUID,     0,               48           },\r
-  {EFI_CERT_X509_SHA384_GUID,     0,               64           },\r
-  {EFI_CERT_X509_SHA512_GUID,     0,               80           }\r
-};\r
-\r
-/**\r
-  Determine whether this operation needs a physical present user.\r
-\r
-  @param[in]      VariableName            Name of the Variable.\r
-  @param[in]      VendorGuid              GUID of the Variable.\r
-\r
-  @retval TRUE      This variable is protected, only a physical present user could set this variable.\r
-  @retval FALSE     This variable is not protected.\r
-\r
-**/\r
-BOOLEAN\r
-NeedPhysicallyPresent(\r
-  IN     CHAR16         *VariableName,\r
-  IN     EFI_GUID       *VendorGuid\r
-  )\r
-{\r
-  if ((CompareGuid (VendorGuid, &gEfiSecureBootEnableDisableGuid) && (StrCmp (VariableName, EFI_SECURE_BOOT_ENABLE_NAME) == 0))\r
-    || (CompareGuid (VendorGuid, &gEfiCustomModeEnableGuid) && (StrCmp (VariableName, EFI_CUSTOM_MODE_NAME) == 0))) {\r
-    return TRUE;\r
-  }\r
-\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-  Determine whether the platform is operating in Custom Secure Boot mode.\r
-\r
-  @retval TRUE           The platform is operating in Custom mode.\r
-  @retval FALSE          The platform is operating in Standard mode.\r
-\r
-**/\r
-BOOLEAN\r
-InCustomMode (\r
-  VOID\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-\r
-  FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (Variable.CurrPtr != NULL && *(GetVariableDataPtr (Variable.CurrPtr)) == CUSTOM_SECURE_BOOT_MODE) {\r
-    return TRUE;\r
-  }\r
-\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-  Initializes for authenticated varibale service.\r
-\r
-  @param[in] MaxAuthVariableSize    Reflect the overhead associated with the saving\r
-                                    of a single EFI authenticated variable with the exception\r
-                                    of the overhead associated with the length\r
-                                    of the string name of the EFI variable.\r
-\r
-  @retval EFI_SUCCESS           Function successfully executed.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resources.\r
-\r
-**/\r
-EFI_STATUS\r
-AutenticatedVariableServiceInitialize (\r
-  IN UINTN      MaxAuthVariableSize\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  VARIABLE_POINTER_TRACK  PkVariable;\r
-  UINT8                   VarValue;\r
-  UINT32                  VarAttr;\r
-  UINT8                   *Data;\r
-  UINTN                   DataSize;\r
-  UINTN                   CtxSize;\r
-  UINT8                   SecureBootMode;\r
-  UINT8                   SecureBootEnable;\r
-  UINT8                   CustomMode;\r
-  UINT32                  ListSize;\r
-\r
-  //\r
-  // Initialize hash context.\r
-  //\r
-  CtxSize   = Sha256GetContextSize ();\r
-  mHashCtx  = AllocateRuntimePool (CtxSize);\r
-  if (mHashCtx == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  //\r
-  // Reserve runtime buffer for public key database. The size excludes variable header and name size.\r
-  //\r
-  mMaxKeyDbSize = (UINT32) (MaxAuthVariableSize - sizeof (AUTHVAR_KEYDB_NAME));\r
-  mMaxKeyNumber = mMaxKeyDbSize / EFI_CERT_TYPE_RSA2048_SIZE;\r
-  mPubKeyStore  = AllocateRuntimePool (mMaxKeyDbSize);\r
-  if (mPubKeyStore == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  //\r
-  // Reserve runtime buffer for certificate database. The size excludes variable header and name size.\r
-  //\r
-  mMaxCertDbSize = (UINT32) (MaxAuthVariableSize - sizeof (EFI_CERT_DB_NAME));\r
-  mCertDbStore   = AllocateRuntimePool (mMaxCertDbSize);\r
-  if (mCertDbStore == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  //\r
-  // Check "AuthVarKeyDatabase" variable's existence.\r
-  // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
-  //\r
-  Status = FindVariable (\r
-             AUTHVAR_KEYDB_NAME,\r
-             &gEfiAuthenticatedVariableGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-\r
-  if (Variable.CurrPtr == NULL) {\r
-    VarAttr       = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;\r
-    VarValue      = 0;\r
-    mPubKeyNumber = 0;\r
-    Status        = UpdateVariable (\r
-                      AUTHVAR_KEYDB_NAME,\r
-                      &gEfiAuthenticatedVariableGuid,\r
-                      &VarValue,\r
-                      sizeof(UINT8),\r
-                      VarAttr,\r
-                      0,\r
-                      0,\r
-                      &Variable,\r
-                      NULL\r
-                      );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  } else {\r
-    //\r
-    // Load database in global variable for cache.\r
-    //\r
-    DataSize  = DataSizeOfVariable (Variable.CurrPtr);\r
-    Data      = GetVariableDataPtr (Variable.CurrPtr);\r
-    ASSERT ((DataSize != 0) && (Data != NULL));\r
-    //\r
-    // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)\r
-    //  Therefore, there is no memory overflow in underlying CopyMem.\r
-    //\r
-    CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
-    mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE);\r
-  }\r
-\r
-  FindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, &PkVariable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (PkVariable.CurrPtr == NULL) {\r
-    DEBUG ((EFI_D_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));\r
-  } else {\r
-    DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));\r
-  }\r
-\r
-  //\r
-  // Create "SetupMode" variable with BS+RT attribute set.\r
-  //\r
-  FindVariable (EFI_SETUP_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (PkVariable.CurrPtr == NULL) {\r
-    mPlatformMode = SETUP_MODE;\r
-  } else {\r
-    mPlatformMode = USER_MODE;\r
-  }\r
-  Status = UpdateVariable (\r
-             EFI_SETUP_MODE_NAME,\r
-             &gEfiGlobalVariableGuid,\r
-             &mPlatformMode,\r
-             sizeof(UINT8),\r
-             EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Create "SignatureSupport" variable with BS+RT attribute set.\r
-  //\r
-  FindVariable (EFI_SIGNATURE_SUPPORT_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  Status  = UpdateVariable (\r
-              EFI_SIGNATURE_SUPPORT_NAME,\r
-              &gEfiGlobalVariableGuid,\r
-              mSignatureSupport,\r
-              sizeof(mSignatureSupport),\r
-              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
-              0,\r
-              0,\r
-              &Variable,\r
-              NULL\r
-              );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.\r
-  // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.\r
-  // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.\r
-  //\r
-  SecureBootEnable = SECURE_BOOT_DISABLE;\r
-  FindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (Variable.CurrPtr != NULL) {\r
-    if (mPlatformMode == SETUP_MODE){\r
-      //\r
-      // PK is cleared in runtime. "SecureBootMode" is not updated before reboot \r
-      // Delete "SecureBootMode" in SetupMode\r
-      //\r
-      Status = UpdateVariable (\r
-                 EFI_SECURE_BOOT_ENABLE_NAME,\r
-                 &gEfiSecureBootEnableDisableGuid,\r
-                 &SecureBootEnable,\r
-                 0,\r
-                 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-                 0,\r
-                 0,\r
-                 &Variable,\r
-                 NULL\r
-                 );\r
-    } else {\r
-      SecureBootEnable = *(GetVariableDataPtr (Variable.CurrPtr));\r
-    }\r
-  } else if (mPlatformMode == USER_MODE) {\r
-    //\r
-    // "SecureBootEnable" not exist, initialize it in USER_MODE.\r
-    //\r
-    SecureBootEnable = SECURE_BOOT_ENABLE;\r
-    Status = UpdateVariable (\r
-               EFI_SECURE_BOOT_ENABLE_NAME,\r
-               &gEfiSecureBootEnableDisableGuid,\r
-               &SecureBootEnable,\r
-               sizeof (UINT8),\r
-               EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-               0,\r
-               0,\r
-               &Variable,\r
-               NULL\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Create "SecureBoot" variable with BS+RT attribute set.\r
-  //\r
-  if (SecureBootEnable == SECURE_BOOT_ENABLE && mPlatformMode == USER_MODE) {\r
-    SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
-  } else {\r
-    SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
-  }\r
-  FindVariable (EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  Status = UpdateVariable (\r
-             EFI_SECURE_BOOT_MODE_NAME,\r
-             &gEfiGlobalVariableGuid,\r
-             &SecureBootMode,\r
-             sizeof (UINT8),\r
-             EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));\r
-  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));\r
-  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));\r
-\r
-  //\r
-  // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.\r
-  //\r
-  FindVariable (EFI_CUSTOM_MODE_NAME, &gEfiCustomModeEnableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  CustomMode = STANDARD_SECURE_BOOT_MODE;\r
-  Status = UpdateVariable (\r
-             EFI_CUSTOM_MODE_NAME,\r
-             &gEfiCustomModeEnableGuid,\r
-             &CustomMode,\r
-             sizeof (UINT8),\r
-             EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));\r
-\r
-  //\r
-  // Check "certdb" variable's existence.\r
-  // If it doesn't exist, then create a new one with\r
-  // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
-  //\r
-  Status = FindVariable (\r
-             EFI_CERT_DB_NAME,\r
-             &gEfiCertDbGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-\r
-  if (Variable.CurrPtr == NULL) {\r
-    VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
-    ListSize = sizeof (UINT32);\r
-    Status   = UpdateVariable (\r
-                 EFI_CERT_DB_NAME,\r
-                 &gEfiCertDbGuid,\r
-                 &ListSize,\r
-                 sizeof (UINT32),\r
-                 VarAttr,\r
-                 0,\r
-                 0,\r
-                 &Variable,\r
-                 NULL\r
-                 );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.\r
-  //\r
-  FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (Variable.CurrPtr != NULL) {\r
-    mVendorKeyState = *(GetVariableDataPtr (Variable.CurrPtr));\r
-  } else {\r
-    //\r
-    // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.\r
-    //\r
-    mVendorKeyState = VENDOR_KEYS_VALID;\r
-    Status = UpdateVariable (\r
-               EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
-               &gEfiVendorKeysNvGuid,\r
-               &mVendorKeyState,\r
-               sizeof (UINT8),\r
-               EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,\r
-               0,\r
-               0,\r
-               &Variable,\r
-               NULL\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Create "VendorKeys" variable with BS+RT attribute set.\r
-  //\r
-  FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  Status = UpdateVariable (\r
-             EFI_VENDOR_KEYS_VARIABLE_NAME,\r
-             &gEfiGlobalVariableGuid,\r
-             &mVendorKeyState,\r
-             sizeof (UINT8),\r
-             EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Add public key in store and return its index.\r
-\r
-  @param[in]  PubKey                  Input pointer to Public Key data\r
-  @param[in]  VariableDataEntry       The variable data entry\r
-\r
-  @return                             Index of new added item\r
-\r
-**/\r
-UINT32\r
-AddPubKeyInStore (\r
-  IN  UINT8                        *PubKey,\r
-  IN  VARIABLE_ENTRY_CONSISTENCY   *VariableDataEntry\r
-  )\r
-{\r
-  EFI_STATUS                       Status;\r
-  BOOLEAN                          IsFound;\r
-  UINT32                           Index;\r
-  VARIABLE_POINTER_TRACK           Variable;\r
-  UINT8                            *Ptr;\r
-  UINT8                            *Data;\r
-  UINTN                            DataSize;\r
-  VARIABLE_ENTRY_CONSISTENCY       PublicKeyEntry;\r
-  UINT32                           Attributes;\r
-\r
-  if (PubKey == NULL) {\r
-    return 0;\r
-  }\r
-\r
-  Status = FindVariable (\r
-             AUTHVAR_KEYDB_NAME,\r
-             &gEfiAuthenticatedVariableGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Get public key database variable failure, Status = %r\n", Status));\r
-    return 0;\r
-  }\r
-\r
-  //\r
-  // Check whether the public key entry does exist.\r
-  //\r
-  IsFound = FALSE;\r
-  for (Ptr = mPubKeyStore, Index = 1; Index <= mPubKeyNumber; Index++) {\r
-    if (CompareMem (Ptr, PubKey, EFI_CERT_TYPE_RSA2048_SIZE) == 0) {\r
-      IsFound = TRUE;\r
-      break;\r
-    }\r
-    Ptr += EFI_CERT_TYPE_RSA2048_SIZE;\r
-  }\r
-\r
-  if (!IsFound) {\r
-    //\r
-    // Add public key in database.\r
-    //\r
-    if (mPubKeyNumber == mMaxKeyNumber) {\r
-      //\r
-      // Public key dadatase is full, try to reclaim invalid key.\r
-      //\r
-      if (AtRuntime ()) {\r
-        //\r
-        // NV storage can't reclaim at runtime.\r
-        //\r
-        return 0;\r
-      }\r
-\r
-      Status = Reclaim (\r
-                 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
-                 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
-                 FALSE,\r
-                 NULL,\r
-                 NULL,\r
-                 0,\r
-                 TRUE\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        return 0;\r
-      }\r
-\r
-      Status = FindVariable (\r
-                 AUTHVAR_KEYDB_NAME,\r
-                 &gEfiAuthenticatedVariableGuid,\r
-                 &Variable,\r
-                 &mVariableModuleGlobal->VariableGlobal,\r
-                 FALSE\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        DEBUG ((EFI_D_ERROR, "Get public key database variable failure, Status = %r\n", Status));\r
-        return 0;\r
-      }\r
-\r
-      DataSize  = DataSizeOfVariable (Variable.CurrPtr);\r
-      Data      = GetVariableDataPtr (Variable.CurrPtr);\r
-      ASSERT ((DataSize != 0) && (Data != NULL));\r
-      //\r
-      // "AuthVarKeyDatabase" is an internal used variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)\r
-      //  Therefore, there is no memory overflow in underlying CopyMem.\r
-      //\r
-      CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);\r
-      mPubKeyNumber = (UINT32) (DataSize / EFI_CERT_TYPE_RSA2048_SIZE);\r
-\r
-      if (mPubKeyNumber == mMaxKeyNumber) {\r
-        return 0;\r
-      }\r
-    }\r
-\r
-    //\r
-    // Check the variable space for both public key and variable data.\r
-    //\r
-    PublicKeyEntry.VariableSize = (mPubKeyNumber + 1) * EFI_CERT_TYPE_RSA2048_SIZE;\r
-    PublicKeyEntry.Guid         = &gEfiAuthenticatedVariableGuid;\r
-    PublicKeyEntry.Name         = AUTHVAR_KEYDB_NAME;\r
-    Attributes = VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;\r
-\r
-    if (!CheckRemainingSpaceForConsistency (Attributes, &PublicKeyEntry, VariableDataEntry, NULL)) {\r
-      //\r
-      // No enough variable space.\r
-      //\r
-      return 0;\r
-    }\r
-\r
-    CopyMem (mPubKeyStore + mPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE, PubKey, EFI_CERT_TYPE_RSA2048_SIZE);\r
-    Index = ++mPubKeyNumber;\r
-    //\r
-    // Update public key database variable.\r
-    //\r
-    Status = UpdateVariable (\r
-               AUTHVAR_KEYDB_NAME,\r
-               &gEfiAuthenticatedVariableGuid,\r
-               mPubKeyStore,\r
-               mPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE,\r
-               Attributes,\r
-               0,\r
-               0,\r
-               &Variable,\r
-               NULL\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_ERROR, "Update public key database variable failure, Status = %r\n", Status));\r
-      return 0;\r
-    }\r
-  }\r
-\r
-  return Index;\r
-}\r
-\r
-/**\r
-  Verify data payload with AuthInfo in EFI_CERT_TYPE_RSA2048_SHA256_GUID type.\r
-  Follow the steps in UEFI2.2.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-\r
-  @param[in]      Data                    Pointer to data with AuthInfo.\r
-  @param[in]      DataSize                Size of Data.\r
-  @param[in]      PubKey                  Public key used for verification.\r
-\r
-  @retval EFI_INVALID_PARAMETER       Invalid parameter.\r
-  @retval EFI_SECURITY_VIOLATION      If authentication failed.\r
-  @retval EFI_SUCCESS                 Authentication successful.\r
-\r
-**/\r
-EFI_STATUS\r
-VerifyCounterBasedPayload (\r
-  IN     UINT8          *Data,\r
-  IN     UINTN          DataSize,\r
-  IN     UINT8          *PubKey\r
-  )\r
-{\r
-  BOOLEAN                         Status;\r
-  EFI_VARIABLE_AUTHENTICATION     *CertData;\r
-  EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;\r
-  UINT8                           Digest[SHA256_DIGEST_SIZE];\r
-  VOID                            *Rsa;\r
-  UINTN                           PayloadSize;\r
-\r
-  PayloadSize = DataSize - AUTHINFO_SIZE;\r
-  Rsa         = NULL;\r
-  CertData    = NULL;\r
-  CertBlock   = NULL;\r
-\r
-  if (Data == NULL || PubKey == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  CertData  = (EFI_VARIABLE_AUTHENTICATION *) Data;\r
-  CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) (CertData->AuthInfo.CertData);\r
-\r
-  //\r
-  // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.\r
-  // Cert type should be EFI_CERT_TYPE_RSA2048_SHA256_GUID.\r
-  //\r
-  if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||\r
-      !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertTypeRsa2048Sha256Guid)\r
-        ) {\r
-    //\r
-    // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.\r
-    //\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-  //\r
-  // Hash data payload with SHA256.\r
-  //\r
-  ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
-  Status  = Sha256Init (mHashCtx);\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  Status  = Sha256Update (mHashCtx, Data + AUTHINFO_SIZE, PayloadSize);\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  //\r
-  // Hash Size.\r
-  //\r
-  Status  = Sha256Update (mHashCtx, &PayloadSize, sizeof (UINTN));\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  //\r
-  // Hash Monotonic Count.\r
-  //\r
-  Status  = Sha256Update (mHashCtx, &CertData->MonotonicCount, sizeof (UINT64));\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  Status  = Sha256Final (mHashCtx, Digest);\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  //\r
-  // Generate & Initialize RSA Context.\r
-  //\r
-  Rsa = RsaNew ();\r
-  ASSERT (Rsa != NULL);\r
-  //\r
-  // Set RSA Key Components.\r
-  // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r
-  //\r
-  Status = RsaSetKey (Rsa, RsaKeyN, PubKey, EFI_CERT_TYPE_RSA2048_SIZE);\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  Status = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r
-  if (!Status) {\r
-    goto Done;\r
-  }\r
-  //\r
-  // Verify the signature.\r
-  //\r
-  Status = RsaPkcs1Verify (\r
-             Rsa,\r
-             Digest,\r
-             SHA256_DIGEST_SIZE,\r
-             CertBlock->Signature,\r
-             EFI_CERT_TYPE_RSA2048_SHA256_SIZE\r
-             );\r
-\r
-Done:\r
-  if (Rsa != NULL) {\r
-    RsaFree (Rsa);\r
-  }\r
-  if (Status) {\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-}\r
-\r
-/**\r
-  Update platform mode.\r
-\r
-  @param[in]      Mode                    SETUP_MODE or USER_MODE.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SUCCESS                     Update platform mode successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-UpdatePlatformMode (\r
-  IN  UINT32                    Mode\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINT8                   SecureBootMode;\r
-  UINT8                   SecureBootEnable;\r
-  UINTN                   VariableDataSize;\r
-\r
-  Status = FindVariable (\r
-             EFI_SETUP_MODE_NAME,\r
-             &gEfiGlobalVariableGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Update the value of SetupMode variable by a simple mem copy, this could avoid possible\r
-  // variable storage reclaim at runtime.\r
-  //\r
-  mPlatformMode = (UINT8) Mode;\r
-  CopyMem (GetVariableDataPtr (Variable.CurrPtr), &mPlatformMode, sizeof(UINT8));\r
-\r
-  if (AtRuntime ()) {\r
-    //\r
-    // SecureBoot Variable indicates whether the platform firmware is operating\r
-    // in Secure boot mode (1) or not (0), so we should not change SecureBoot\r
-    // Variable in runtime.\r
-    //\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Check "SecureBoot" variable's existence.\r
-  // If it doesn't exist, firmware has no capability to perform driver signing verification,\r
-  // then set "SecureBoot" to 0.\r
-  //\r
-  Status = FindVariable (\r
-             EFI_SECURE_BOOT_MODE_NAME,\r
-             &gEfiGlobalVariableGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  //\r
-  // If "SecureBoot" variable exists, then check "SetupMode" variable update.\r
-  // If "SetupMode" variable is USER_MODE, "SecureBoot" variable is set to 1.\r
-  // If "SetupMode" variable is SETUP_MODE, "SecureBoot" variable is set to 0.\r
-  //\r
-  if (Variable.CurrPtr == NULL) {\r
-    SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
-  } else {\r
-    if (mPlatformMode == USER_MODE) {\r
-      SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
-    } else if (mPlatformMode == SETUP_MODE) {\r
-      SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
-    } else {\r
-      return EFI_NOT_FOUND;\r
-    }\r
-  }\r
-\r
-  Status  = UpdateVariable (\r
-              EFI_SECURE_BOOT_MODE_NAME,\r
-              &gEfiGlobalVariableGuid,\r
-              &SecureBootMode,\r
-              sizeof(UINT8),\r
-              EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-              0,\r
-              0,\r
-              &Variable,\r
-              NULL\r
-              );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Check "SecureBootEnable" variable's existence. It can enable/disable secure boot feature.\r
-  //\r
-  Status = FindVariable (\r
-             EFI_SECURE_BOOT_ENABLE_NAME,\r
-             &gEfiSecureBootEnableDisableGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-\r
-  if (SecureBootMode == SECURE_BOOT_MODE_ENABLE) {\r
-    //\r
-    // Create the "SecureBootEnable" variable as secure boot is enabled.\r
-    //\r
-    SecureBootEnable = SECURE_BOOT_ENABLE;\r
-    VariableDataSize = sizeof (SecureBootEnable);\r
-  } else {\r
-    //\r
-    // Delete the "SecureBootEnable" variable if this variable exist as "SecureBoot"\r
-    // variable is not in secure boot state.\r
-    //\r
-    if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-      return EFI_SUCCESS;\r
-    }\r
-    SecureBootEnable = SECURE_BOOT_DISABLE;\r
-    VariableDataSize = 0;\r
-  }\r
-\r
-  Status = UpdateVariable (\r
-             EFI_SECURE_BOOT_ENABLE_NAME,\r
-             &gEfiSecureBootEnableDisableGuid,\r
-             &SecureBootEnable,\r
-             VariableDataSize,\r
-             EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK/db/dbx/dbt variable.\r
-\r
-  @param[in]  VariableName                Name of Variable to be check.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Point to the variable data to be checked.\r
-  @param[in]  DataSize                    Size of Data.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid signature list format.\r
-  @return EFI_SUCCESS                     Passed signature list format check successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-CheckSignatureListFormat(\r
-  IN  CHAR16                    *VariableName,\r
-  IN  EFI_GUID                  *VendorGuid,\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize\r
-  )\r
-{\r
-  EFI_SIGNATURE_LIST     *SigList;\r
-  UINTN                  SigDataSize;\r
-  UINT32                 Index;\r
-  UINT32                 SigCount;\r
-  BOOLEAN                IsPk;\r
-  VOID                   *RsaContext;\r
-  EFI_SIGNATURE_DATA     *CertData;\r
-  UINTN                  CertLen;\r
-\r
-  if (DataSize == 0) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  ASSERT (VariableName != NULL && VendorGuid != NULL && Data != NULL);\r
-\r
-  if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
-    IsPk = TRUE;\r
-  } else if ((CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) ||\r
-             (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
-             ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
-              (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)))) {\r
-    IsPk = FALSE;\r
-  } else {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  SigCount = 0;\r
-  SigList  = (EFI_SIGNATURE_LIST *) Data;\r
-  SigDataSize  = DataSize;\r
-  RsaContext = NULL;\r
-\r
-  //\r
-  // Walk throuth the input signature list and check the data format.\r
-  // If any signature is incorrectly formed, the whole check will fail.\r
-  //\r
-  while ((SigDataSize > 0) && (SigDataSize >= SigList->SignatureListSize)) {\r
-    for (Index = 0; Index < (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM)); Index++ ) {\r
-      if (CompareGuid (&SigList->SignatureType, &mSupportSigItem[Index].SigType)) {\r
-        //\r
-        // The value of SignatureSize should always be 16 (size of SignatureOwner\r
-        // component) add the data length according to signature type.\r
-        //\r
-        if (mSupportSigItem[Index].SigDataSize != ((UINT32) ~0) &&\r
-          (SigList->SignatureSize - sizeof (EFI_GUID)) != mSupportSigItem[Index].SigDataSize) {\r
-          return EFI_INVALID_PARAMETER;\r
-        }\r
-        if (mSupportSigItem[Index].SigHeaderSize != ((UINTN) ~0) &&\r
-          SigList->SignatureHeaderSize != mSupportSigItem[Index].SigHeaderSize) {\r
-          return EFI_INVALID_PARAMETER;\r
-        }\r
-        break;\r
-      }\r
-    }\r
-\r
-    if (Index == (sizeof (mSupportSigItem) / sizeof (EFI_SIGNATURE_ITEM))) {\r
-      //\r
-      // Undefined signature type.\r
-      //\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
-    if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {\r
-      //\r
-      // Try to retrieve the RSA public key from the X.509 certificate.\r
-      // If this operation fails, it's not a valid certificate.\r
-      //\r
-      RsaContext = RsaNew ();\r
-      if (RsaContext == NULL) {\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-      CertData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) + SigList->SignatureHeaderSize);\r
-      CertLen = SigList->SignatureSize - sizeof (EFI_GUID);\r
-      if (!RsaGetPublicKeyFromX509 (CertData->SignatureData, CertLen, &RsaContext)) {\r
-        RsaFree (RsaContext);\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-      RsaFree (RsaContext);\r
-    }\r
-\r
-    if ((SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) % SigList->SignatureSize != 0) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    SigCount += (SigList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - SigList->SignatureHeaderSize) / SigList->SignatureSize;\r
-\r
-    SigDataSize -= SigList->SignatureListSize;\r
-    SigList = (EFI_SIGNATURE_LIST *) ((UINT8 *) SigList + SigList->SignatureListSize);\r
-  }\r
-\r
-  if (((UINTN) SigList - (UINTN) Data) != DataSize) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (IsPk && SigCount > 1) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Update "VendorKeys" variable to record the out of band secure boot key modification.\r
-\r
-  @return EFI_SUCCESS           Variable is updated successfully.\r
-  @return Others                Failed to update variable.\r
-\r
-**/\r
-EFI_STATUS\r
-VendorKeyIsModified (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-\r
-  if (mVendorKeyState == VENDOR_KEYS_MODIFIED) {\r
-    return EFI_SUCCESS;\r
-  }\r
-  mVendorKeyState = VENDOR_KEYS_MODIFIED;\r
-\r
-  FindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  Status = UpdateVariable (\r
-             EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
-             &gEfiVendorKeysNvGuid,\r
-             &mVendorKeyState,\r
-             sizeof (UINT8),\r
-             EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS,\r
-             0,\r
-             0,\r
-             &Variable,\r
-             NULL\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  FindVariable (EFI_VENDOR_KEYS_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  return UpdateVariable (\r
-           EFI_VENDOR_KEYS_VARIABLE_NAME,\r
-           &gEfiGlobalVariableGuid,\r
-           &mVendorKeyState,\r
-           sizeof (UINT8),\r
-           EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-           0,\r
-           0,\r
-           &Variable,\r
-           NULL\r
-           );\r
-}\r
-\r
-/**\r
-  Process variable with platform key for verification.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable\r
-  @param[in]  IsPk                        Indicate whether it is to process pk.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SECURITY_VIOLATION          The variable does NOT pass the validation.\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable passed validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVarWithPk (\r
-  IN  CHAR16                    *VariableName,\r
-  IN  EFI_GUID                  *VendorGuid,\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize,\r
-  IN  VARIABLE_POINTER_TRACK    *Variable,\r
-  IN  UINT32                    Attributes OPTIONAL,\r
-  IN  BOOLEAN                   IsPk\r
-  )\r
-{\r
-  EFI_STATUS                  Status;\r
-  BOOLEAN                     Del;\r
-  UINT8                       *Payload;\r
-  UINTN                       PayloadSize;\r
-\r
-  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 ||\r
-      (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
-    //\r
-    // PK, KEK and db/dbx/dbt should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
-    // authenticated variable.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Del = FALSE;\r
-  if ((InCustomMode() && UserPhysicalPresent()) || (mPlatformMode == SETUP_MODE && !IsPk)) {\r
-    Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data);\r
-    PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
-    if (PayloadSize == 0) {\r
-      Del = TRUE;\r
-    }\r
-\r
-    Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    Status = UpdateVariable (\r
-               VariableName,\r
-               VendorGuid,\r
-               Payload,\r
-               PayloadSize,\r
-               Attributes,\r
-               0,\r
-               0,\r
-               Variable,\r
-               &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp\r
-               );\r
-    if (EFI_ERROR(Status)) {\r
-      return Status;\r
-    }\r
-\r
-    if ((mPlatformMode != SETUP_MODE) || IsPk) {\r
-      Status = VendorKeyIsModified ();\r
-    }\r
-  } else if (mPlatformMode == USER_MODE) {\r
-    //\r
-    // Verify against X509 Cert in PK database.\r
-    //\r
-    Status = VerifyTimeBasedPayload (\r
-               VariableName,\r
-               VendorGuid,\r
-               Data,\r
-               DataSize,\r
-               Variable,\r
-               Attributes,\r
-               AuthVarTypePk,\r
-               &Del\r
-               );\r
-  } else {\r
-    //\r
-    // Verify against the certificate in data payload.\r
-    //\r
-    Status = VerifyTimeBasedPayload (\r
-               VariableName,\r
-               VendorGuid,\r
-               Data,\r
-               DataSize,\r
-               Variable,\r
-               Attributes,\r
-               AuthVarTypePayload,\r
-               &Del\r
-               );\r
-  }\r
-\r
-  if (!EFI_ERROR(Status) && IsPk) {\r
-    if (mPlatformMode == SETUP_MODE && !Del) {\r
-      //\r
-      // If enroll PK in setup mode, need change to user mode.\r
-      //\r
-      Status = UpdatePlatformMode (USER_MODE);\r
-    } else if (mPlatformMode == USER_MODE && Del){\r
-      //\r
-      // If delete PK in user mode, need change to setup mode.\r
-      //\r
-      Status = UpdatePlatformMode (SETUP_MODE);\r
-    }\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Process variable with key exchange key for verification.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SECURITY_VIOLATION          The variable does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable pass validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVarWithKek (\r
-  IN  CHAR16                               *VariableName,\r
-  IN  EFI_GUID                             *VendorGuid,\r
-  IN  VOID                                 *Data,\r
-  IN  UINTN                                DataSize,\r
-  IN  VARIABLE_POINTER_TRACK               *Variable,\r
-  IN  UINT32                               Attributes OPTIONAL\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-  UINT8                           *Payload;\r
-  UINTN                           PayloadSize;\r
-\r
-  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0 ||\r
-      (Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
-    //\r
-    // DB, DBX and DBT should set EFI_VARIABLE_NON_VOLATILE attribute and should be a time-based\r
-    // authenticated variable.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Status = EFI_SUCCESS;\r
-  if (mPlatformMode == USER_MODE && !(InCustomMode() && UserPhysicalPresent())) {\r
-    //\r
-    // Time-based, verify against X509 Cert KEK.\r
-    //\r
-    return VerifyTimeBasedPayload (\r
-             VariableName,\r
-             VendorGuid,\r
-             Data,\r
-             DataSize,\r
-             Variable,\r
-             Attributes,\r
-             AuthVarTypeKek,\r
-             NULL\r
-             );\r
-  } else {\r
-    //\r
-    // If in setup mode or custom secure boot mode, no authentication needed.\r
-    //\r
-    Payload = (UINT8 *) Data + AUTHINFO2_SIZE (Data);\r
-    PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
-\r
-    Status = CheckSignatureListFormat(VariableName, VendorGuid, Payload, PayloadSize);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    Status = UpdateVariable (\r
-               VariableName,\r
-               VendorGuid,\r
-               Payload,\r
-               PayloadSize,\r
-               Attributes,\r
-               0,\r
-               0,\r
-               Variable,\r
-               &((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->TimeStamp\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    if (mPlatformMode != SETUP_MODE) {\r
-      Status = VendorKeyIsModified ();\r
-    }\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Check if it is to delete auth variable.\r
-\r
-  @param[in] Data               Data pointer.\r
-  @param[in] DataSize           Size of Data.\r
-  @param[in] Variable           The variable information which is used to keep track of variable usage.\r
-  @param[in] Attributes         Attribute value of the variable.\r
-\r
-  @retval TRUE                  It is to delete auth variable.\r
-  @retval FALSE                 It is not to delete auth variable.\r
-\r
-**/\r
-BOOLEAN\r
-IsDeleteAuthVariable (\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize,\r
-  IN  VARIABLE_POINTER_TRACK    *Variable,\r
-  IN  UINT32                    Attributes\r
-  )\r
-{\r
-  BOOLEAN                       Del;\r
-  UINTN                         PayloadSize;\r
-\r
-  Del = FALSE;\r
-\r
-  //\r
-  // To delete a variable created with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
-  // or the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute,\r
-  // SetVariable must be used with attributes matching the existing variable\r
-  // and the DataSize set to the size of the AuthInfo descriptor.\r
-  //\r
-  if ((Variable->CurrPtr != NULL) &&\r
-      (Attributes == Variable->CurrPtr->Attributes) &&\r
-      ((Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0)) {\r
-    if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-      PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
-      if (PayloadSize == 0) {\r
-        Del = TRUE;\r
-      }\r
-    } else {\r
-      PayloadSize = DataSize - AUTHINFO_SIZE;\r
-      if (PayloadSize == 0) {\r
-        Del = TRUE;\r
-      }\r
-    }\r
-  }\r
-\r
-  return Del;\r
-}\r
-\r
-/**\r
-  Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_WRITE_PROTECTED             Variable is write-protected and needs authentication with\r
-                                          EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
-  @return EFI_OUT_OF_RESOURCES            The Database to save the public key is full.\r
-  @return EFI_SECURITY_VIOLATION          The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
-                                          set, but the AuthInfo does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable is not write-protected or pass validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVariable (\r
-  IN     CHAR16                             *VariableName,\r
-  IN     EFI_GUID                           *VendorGuid,\r
-  IN     VOID                               *Data,\r
-  IN     UINTN                              DataSize,\r
-  IN     VARIABLE_POINTER_TRACK             *Variable,\r
-  IN     UINT32                             Attributes\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-  BOOLEAN                         IsDeletion;\r
-  BOOLEAN                         IsFirstTime;\r
-  UINT8                           *PubKey;\r
-  EFI_VARIABLE_AUTHENTICATION     *CertData;\r
-  EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;\r
-  UINT32                          KeyIndex;\r
-  UINT64                          MonotonicCount;\r
-  VARIABLE_ENTRY_CONSISTENCY      VariableDataEntry;\r
-\r
-  KeyIndex    = 0;\r
-  CertData    = NULL;\r
-  CertBlock   = NULL;\r
-  PubKey      = NULL;\r
-  IsDeletion  = FALSE;\r
-  Status      = EFI_SUCCESS;\r
-\r
-  if (IsDeleteAuthVariable (Data, DataSize, Variable, Attributes) && UserPhysicalPresent()) {\r
-    //\r
-    // Allow the delete operation of common authenticated variable at user physical presence.\r
-    //\r
-    if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-      Status = DeleteCertsFromDb (VariableName, VendorGuid);\r
-    }\r
-    if (!EFI_ERROR (Status)) {\r
-      Status = UpdateVariable (\r
-                 VariableName,\r
-                 VendorGuid,\r
-                 NULL,\r
-                 0,\r
-                 0,\r
-                 0,\r
-                 0,\r
-                 Variable,\r
-                 NULL\r
-                 );\r
-    }\r
-    return Status;\r
-  }\r
-\r
-  if (NeedPhysicallyPresent (VariableName, VendorGuid) && !UserPhysicalPresent()) {\r
-    //\r
-    // This variable is protected, only physical present user could modify its value.\r
-    //\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-  //\r
-  // A time-based authenticated variable and a count-based authenticated variable\r
-  // can't be updated by each other.\r
-  //\r
-  if (Variable->CurrPtr != NULL) {\r
-    if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
-        ((Variable->CurrPtr->Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-\r
-    if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
-        ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0)) {\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Process Time-based Authenticated variable.\r
-  //\r
-  if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-    return VerifyTimeBasedPayload (\r
-             VariableName,\r
-             VendorGuid,\r
-             Data,\r
-             DataSize,\r
-             Variable,\r
-             Attributes,\r
-             AuthVarTypePriv,\r
-             NULL\r
-             );\r
-  }\r
-\r
-  //\r
-  // Determine if first time SetVariable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS.\r
-  //\r
-  if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-    //\r
-    // Determine current operation type.\r
-    //\r
-    if (DataSize == AUTHINFO_SIZE) {\r
-      IsDeletion = TRUE;\r
-    }\r
-    //\r
-    // Determine whether this is the first time with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
-    //\r
-    if (Variable->CurrPtr == NULL) {\r
-      IsFirstTime = TRUE;\r
-    } else if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == 0) {\r
-      IsFirstTime = TRUE;\r
-    } else {\r
-      KeyIndex   = Variable->CurrPtr->PubKeyIndex;\r
-      IsFirstTime = FALSE;\r
-    }\r
-  } else if ((Variable->CurrPtr != NULL) &&\r
-             ((Variable->CurrPtr->Attributes & (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) != 0)\r
-            ) {\r
-    //\r
-    // If the variable is already write-protected, it always needs authentication before update.\r
-    //\r
-    return EFI_WRITE_PROTECTED;\r
-  } else {\r
-    //\r
-    // If without EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, set and attributes collision.\r
-    // That means it is not authenticated variable, just update variable as usual.\r
-    //\r
-    Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, Variable, NULL);\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Get PubKey and check Monotonic Count value corresponding to the variable.\r
-  //\r
-  CertData  = (EFI_VARIABLE_AUTHENTICATION *) Data;\r
-  CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) (CertData->AuthInfo.CertData);\r
-  PubKey    = CertBlock->PublicKey;\r
-\r
-  //\r
-  // Update Monotonic Count value.\r
-  //\r
-  MonotonicCount = CertData->MonotonicCount;\r
-\r
-  if (!IsFirstTime) {\r
-    //\r
-    // 2 cases need to check here\r
-    //   1. Internal PubKey variable. PubKeyIndex is always 0\r
-    //   2. Other counter-based AuthVariable. Check input PubKey.\r
-    //\r
-    if (KeyIndex == 0 || CompareMem (PubKey, mPubKeyStore + (KeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE, EFI_CERT_TYPE_RSA2048_SIZE) != 0) {\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-    //\r
-    // Compare the current monotonic count and ensure that it is greater than the last SetVariable\r
-    // operation with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.\r
-    //\r
-    if (CertData->MonotonicCount <= Variable->CurrPtr->MonotonicCount) {\r
-      //\r
-      // Monotonic count check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.\r
-      //\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-  }\r
-  //\r
-  // Verify the certificate in Data payload.\r
-  //\r
-  Status = VerifyCounterBasedPayload (Data, DataSize, PubKey);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Now, the signature has been verified!\r
-  //\r
-  if (IsFirstTime && !IsDeletion) {\r
-    VariableDataEntry.VariableSize = DataSize - AUTHINFO_SIZE;\r
-    VariableDataEntry.Guid         = VendorGuid;\r
-    VariableDataEntry.Name         = VariableName;\r
-\r
-    //\r
-    // Update public key database variable if need.\r
-    //\r
-    KeyIndex = AddPubKeyInStore (PubKey, &VariableDataEntry);\r
-    if (KeyIndex == 0) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Verification pass.\r
-  //\r
-  return UpdateVariable (VariableName, VendorGuid, (UINT8*)Data + AUTHINFO_SIZE, DataSize - AUTHINFO_SIZE, Attributes, KeyIndex, MonotonicCount, Variable, NULL);\r
-}\r
-\r
-/**\r
-  Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA\r
-  will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA\r
-  will be ignored.\r
-\r
-  @param[in, out]  Data              Pointer to original EFI_SIGNATURE_LIST.\r
-  @param[in]       DataSize          Size of Data buffer.\r
-  @param[in]       FreeBufSize       Size of free data buffer\r
-  @param[in]       NewData           Pointer to new EFI_SIGNATURE_LIST to be appended.\r
-  @param[in]       NewDataSize       Size of NewData buffer.\r
-  @param[out]      MergedBufSize     Size of the merged buffer\r
-\r
-  @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed\r
-\r
-**/\r
-EFI_STATUS\r
-AppendSignatureList (\r
-  IN  OUT VOID            *Data,\r
-  IN  UINTN               DataSize,\r
-  IN  UINTN               FreeBufSize,\r
-  IN  VOID                *NewData,\r
-  IN  UINTN               NewDataSize,\r
-  OUT UINTN               *MergedBufSize\r
-  )\r
-{\r
-  EFI_SIGNATURE_LIST  *CertList;\r
-  EFI_SIGNATURE_DATA  *Cert;\r
-  UINTN               CertCount;\r
-  EFI_SIGNATURE_LIST  *NewCertList;\r
-  EFI_SIGNATURE_DATA  *NewCert;\r
-  UINTN               NewCertCount;\r
-  UINTN               Index;\r
-  UINTN               Index2;\r
-  UINTN               Size;\r
-  UINT8               *Tail;\r
-  UINTN               CopiedCount;\r
-  UINTN               SignatureListSize;\r
-  BOOLEAN             IsNewCert;\r
-\r
-  Tail = (UINT8 *) Data + DataSize;\r
-\r
-  NewCertList = (EFI_SIGNATURE_LIST *) NewData;\r
-  while ((NewDataSize > 0) && (NewDataSize >= NewCertList->SignatureListSize)) {\r
-    NewCert      = (EFI_SIGNATURE_DATA *) ((UINT8 *) NewCertList + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);\r
-    NewCertCount = (NewCertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - NewCertList->SignatureHeaderSize) / NewCertList->SignatureSize;\r
-\r
-    CopiedCount = 0;\r
-    for (Index = 0; Index < NewCertCount; Index++) {\r
-      IsNewCert = TRUE;\r
-\r
-      Size = DataSize;\r
-      CertList = (EFI_SIGNATURE_LIST *) Data;\r
-      while ((Size > 0) && (Size >= CertList->SignatureListSize)) {\r
-        if (CompareGuid (&CertList->SignatureType, &NewCertList->SignatureType) &&\r
-           (CertList->SignatureSize == NewCertList->SignatureSize)) {\r
-          Cert      = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
-          CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
-          for (Index2 = 0; Index2 < CertCount; Index2++) {\r
-            //\r
-            // Iterate each Signature Data in this Signature List.\r
-            //\r
-            if (CompareMem (NewCert, Cert, CertList->SignatureSize) == 0) {\r
-              IsNewCert = FALSE;\r
-              break;\r
-            }\r
-            Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
-          }\r
-        }\r
-\r
-        if (!IsNewCert) {\r
-          break;\r
-        }\r
-        Size -= CertList->SignatureListSize;\r
-        CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
-      }\r
-\r
-      if (IsNewCert) {\r
-        //\r
-        // New EFI_SIGNATURE_DATA, append it.\r
-        //\r
-        if (CopiedCount == 0) {\r
-          if (FreeBufSize < sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize) {\r
-            return EFI_BUFFER_TOO_SMALL;\r
-          }\r
-\r
-          //\r
-          // Copy EFI_SIGNATURE_LIST header for only once.\r
-          //\r
-\r
-          CopyMem (Tail, NewCertList, sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize);\r
-          Tail = Tail + sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize;\r
-          FreeBufSize -= sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize;\r
-        }\r
-\r
-        if (FreeBufSize < NewCertList->SignatureSize) {\r
-          return EFI_BUFFER_TOO_SMALL;\r
-        }\r
-        CopyMem (Tail, NewCert, NewCertList->SignatureSize);\r
-        Tail += NewCertList->SignatureSize;\r
-        FreeBufSize -= NewCertList->SignatureSize;\r
-        CopiedCount++;\r
-      }\r
-\r
-      NewCert = (EFI_SIGNATURE_DATA *) ((UINT8 *) NewCert + NewCertList->SignatureSize);\r
-    }\r
-\r
-    //\r
-    // Update SignatureListSize in newly appended EFI_SIGNATURE_LIST.\r
-    //\r
-    if (CopiedCount != 0) {\r
-      SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + NewCertList->SignatureHeaderSize + (CopiedCount * NewCertList->SignatureSize);\r
-      CertList = (EFI_SIGNATURE_LIST *) (Tail - SignatureListSize);\r
-      CertList->SignatureListSize = (UINT32) SignatureListSize;\r
-    }\r
-\r
-    NewDataSize -= NewCertList->SignatureListSize;\r
-    NewCertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) NewCertList + NewCertList->SignatureListSize);\r
-  }\r
-\r
-  *MergedBufSize = (Tail - (UINT8 *) Data);\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Compare two EFI_TIME data.\r
-\r
-\r
-  @param FirstTime           A pointer to the first EFI_TIME data.\r
-  @param SecondTime          A pointer to the second EFI_TIME data.\r
-\r
-  @retval  TRUE              The FirstTime is not later than the SecondTime.\r
-  @retval  FALSE             The FirstTime is later than the SecondTime.\r
-\r
-**/\r
-BOOLEAN\r
-CompareTimeStamp (\r
-  IN EFI_TIME               *FirstTime,\r
-  IN EFI_TIME               *SecondTime\r
-  )\r
-{\r
-  if (FirstTime->Year != SecondTime->Year) {\r
-    return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r
-  } else if (FirstTime->Month != SecondTime->Month) {\r
-    return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r
-  } else if (FirstTime->Day != SecondTime->Day) {\r
-    return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r
-  } else if (FirstTime->Hour != SecondTime->Hour) {\r
-    return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r
-  } else if (FirstTime->Minute != SecondTime->Minute) {\r
-    return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r
-  }\r
-\r
-  return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r
-}\r
-\r
-/**\r
-  Find matching signer's certificates for common authenticated variable\r
-  by corresponding VariableName and VendorGuid from "certdb".\r
-\r
-  The data format of "certdb":\r
-  //\r
-  //     UINT32 CertDbListSize;\r
-  // /// AUTH_CERT_DB_DATA Certs1[];\r
-  // /// AUTH_CERT_DB_DATA Certs2[];\r
-  // /// ...\r
-  // /// AUTH_CERT_DB_DATA Certsn[];\r
-  //\r
-\r
-  @param[in]  VariableName   Name of authenticated Variable.\r
-  @param[in]  VendorGuid     Vendor GUID of authenticated Variable.\r
-  @param[in]  Data           Pointer to variable "certdb".\r
-  @param[in]  DataSize       Size of variable "certdb".\r
-  @param[out] CertOffset     Offset of matching CertData, from starting of Data.\r
-  @param[out] CertDataSize   Length of CertData in bytes.\r
-  @param[out] CertNodeOffset Offset of matching AUTH_CERT_DB_DATA , from\r
-                             starting of Data.\r
-  @param[out] CertNodeSize   Length of AUTH_CERT_DB_DATA in bytes.\r
-\r
-  @retval  EFI_INVALID_PARAMETER Any input parameter is invalid.\r
-  @retval  EFI_NOT_FOUND         Fail to find matching certs.\r
-  @retval  EFI_SUCCESS           Find matching certs and output parameters.\r
-\r
-**/\r
-EFI_STATUS\r
-FindCertsFromDb (\r
-  IN     CHAR16           *VariableName,\r
-  IN     EFI_GUID         *VendorGuid,\r
-  IN     UINT8            *Data,\r
-  IN     UINTN            DataSize,\r
-  OUT    UINT32           *CertOffset,    OPTIONAL\r
-  OUT    UINT32           *CertDataSize,  OPTIONAL\r
-  OUT    UINT32           *CertNodeOffset,OPTIONAL\r
-  OUT    UINT32           *CertNodeSize   OPTIONAL\r
-  )\r
-{\r
-  UINT32                  Offset;\r
-  AUTH_CERT_DB_DATA       *Ptr;\r
-  UINT32                  CertSize;\r
-  UINT32                  NameSize;\r
-  UINT32                  NodeSize;\r
-  UINT32                  CertDbListSize;\r
-\r
-  if ((VariableName == NULL) || (VendorGuid == NULL) || (Data == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Check whether DataSize matches recorded CertDbListSize.\r
-  //\r
-  if (DataSize < sizeof (UINT32)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  CertDbListSize = ReadUnaligned32 ((UINT32 *) Data);\r
-\r
-  if (CertDbListSize != (UINT32) DataSize) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Offset = sizeof (UINT32);\r
-\r
-  //\r
-  // Get corresponding certificates by VendorGuid and VariableName.\r
-  //\r
-  while (Offset < (UINT32) DataSize) {\r
-    Ptr = (AUTH_CERT_DB_DATA *) (Data + Offset);\r
-    //\r
-    // Check whether VendorGuid matches.\r
-    //\r
-    if (CompareGuid (&Ptr->VendorGuid, VendorGuid)) {\r
-      NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);\r
-      NameSize = ReadUnaligned32 (&Ptr->NameSize);\r
-      CertSize = ReadUnaligned32 (&Ptr->CertDataSize);\r
-\r
-      if (NodeSize != sizeof (EFI_GUID) + sizeof (UINT32) * 3 + CertSize +\r
-          sizeof (CHAR16) * NameSize) {\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-\r
-      Offset = Offset + sizeof (EFI_GUID) + sizeof (UINT32) * 3;\r
-      //\r
-      // Check whether VariableName matches.\r
-      //\r
-      if ((NameSize == StrLen (VariableName)) &&\r
-          (CompareMem (Data + Offset, VariableName, NameSize * sizeof (CHAR16)) == 0)) {\r
-        Offset = Offset + NameSize * sizeof (CHAR16);\r
-\r
-        if (CertOffset != NULL) {\r
-          *CertOffset = Offset;\r
-        }\r
-\r
-        if (CertDataSize != NULL) {\r
-          *CertDataSize = CertSize;\r
-        }\r
-\r
-        if (CertNodeOffset != NULL) {\r
-          *CertNodeOffset = (UINT32) ((UINT8 *) Ptr - Data);\r
-        }\r
-\r
-        if (CertNodeSize != NULL) {\r
-          *CertNodeSize = NodeSize;\r
-        }\r
-\r
-        return EFI_SUCCESS;\r
-      } else {\r
-        Offset = Offset + NameSize * sizeof (CHAR16) + CertSize;\r
-      }\r
-    } else {\r
-      NodeSize = ReadUnaligned32 (&Ptr->CertNodeSize);\r
-      Offset   = Offset + NodeSize;\r
-    }\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-/**\r
-  Retrieve signer's certificates for common authenticated variable\r
-  by corresponding VariableName and VendorGuid from "certdb".\r
-\r
-  @param[in]  VariableName   Name of authenticated Variable.\r
-  @param[in]  VendorGuid     Vendor GUID of authenticated Variable.\r
-  @param[out] CertData       Pointer to signer's certificates.\r
-  @param[out] CertDataSize   Length of CertData in bytes.\r
-\r
-  @retval  EFI_INVALID_PARAMETER Any input parameter is invalid.\r
-  @retval  EFI_NOT_FOUND         Fail to find "certdb" or matching certs.\r
-  @retval  EFI_SUCCESS           Get signer's certificates successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-GetCertsFromDb (\r
-  IN     CHAR16           *VariableName,\r
-  IN     EFI_GUID         *VendorGuid,\r
-  OUT    UINT8            **CertData,\r
-  OUT    UINT32           *CertDataSize\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK  CertDbVariable;\r
-  EFI_STATUS              Status;\r
-  UINT8                   *Data;\r
-  UINTN                   DataSize;\r
-  UINT32                  CertOffset;\r
-\r
-  if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL) || (CertDataSize == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Get variable "certdb".\r
-  //\r
-  Status = FindVariable (\r
-             EFI_CERT_DB_NAME,\r
-             &gEfiCertDbGuid,\r
-             &CertDbVariable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DataSize  = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
-  Data      = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
-  if ((DataSize == 0) || (Data == NULL)) {\r
-    ASSERT (FALSE);\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  Status = FindCertsFromDb (\r
-             VariableName,\r
-             VendorGuid,\r
-             Data,\r
-             DataSize,\r
-             &CertOffset,\r
-             CertDataSize,\r
-             NULL,\r
-             NULL\r
-             );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  *CertData = Data + CertOffset;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Delete matching signer's certificates when deleting common authenticated\r
-  variable by corresponding VariableName and VendorGuid from "certdb".\r
-\r
-  @param[in]  VariableName   Name of authenticated Variable.\r
-  @param[in]  VendorGuid     Vendor GUID of authenticated Variable.\r
-\r
-  @retval  EFI_INVALID_PARAMETER Any input parameter is invalid.\r
-  @retval  EFI_NOT_FOUND         Fail to find "certdb" or matching certs.\r
-  @retval  EFI_OUT_OF_RESOURCES  The operation is failed due to lack of resources.\r
-  @retval  EFI_SUCCESS           The operation is completed successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-DeleteCertsFromDb (\r
-  IN     CHAR16           *VariableName,\r
-  IN     EFI_GUID         *VendorGuid\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK  CertDbVariable;\r
-  EFI_STATUS              Status;\r
-  UINT8                   *Data;\r
-  UINTN                   DataSize;\r
-  UINT32                  VarAttr;\r
-  UINT32                  CertNodeOffset;\r
-  UINT32                  CertNodeSize;\r
-  UINT8                   *NewCertDb;\r
-  UINT32                  NewCertDbSize;\r
-\r
-  if ((VariableName == NULL) || (VendorGuid == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Get variable "certdb".\r
-  //\r
-  Status = FindVariable (\r
-             EFI_CERT_DB_NAME,\r
-             &gEfiCertDbGuid,\r
-             &CertDbVariable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DataSize  = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
-  Data      = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
-  if ((DataSize == 0) || (Data == NULL)) {\r
-    ASSERT (FALSE);\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  if (DataSize == sizeof (UINT32)) {\r
-    //\r
-    // There is no certs in certdb.\r
-    //\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Get corresponding cert node from certdb.\r
-  //\r
-  Status = FindCertsFromDb (\r
-             VariableName,\r
-             VendorGuid,\r
-             Data,\r
-             DataSize,\r
-             NULL,\r
-             NULL,\r
-             &CertNodeOffset,\r
-             &CertNodeSize\r
-             );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  if (DataSize < (CertNodeOffset + CertNodeSize)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  //\r
-  // Construct new data content of variable "certdb".\r
-  //\r
-  NewCertDbSize = (UINT32) DataSize - CertNodeSize;\r
-  NewCertDb     = (UINT8*) mCertDbStore;\r
-\r
-  //\r
-  // Copy the DB entries before deleting node.\r
-  //\r
-  CopyMem (NewCertDb, Data, CertNodeOffset);\r
-  //\r
-  // Update CertDbListSize.\r
-  //\r
-  CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));\r
-  //\r
-  // Copy the DB entries after deleting node.\r
-  //\r
-  if (DataSize > (CertNodeOffset + CertNodeSize)) {\r
-    CopyMem (\r
-      NewCertDb + CertNodeOffset,\r
-      Data + CertNodeOffset + CertNodeSize,\r
-      DataSize - CertNodeOffset - CertNodeSize\r
-      );\r
-  }\r
-\r
-  //\r
-  // Set "certdb".\r
-  //\r
-  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
-  Status   = UpdateVariable (\r
-               EFI_CERT_DB_NAME,\r
-               &gEfiCertDbGuid,\r
-               NewCertDb,\r
-               NewCertDbSize,\r
-               VarAttr,\r
-               0,\r
-               0,\r
-               &CertDbVariable,\r
-               NULL\r
-               );\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Insert signer's certificates for common authenticated variable with VariableName\r
-  and VendorGuid in AUTH_CERT_DB_DATA to "certdb".\r
-\r
-  @param[in]  VariableName   Name of authenticated Variable.\r
-  @param[in]  VendorGuid     Vendor GUID of authenticated Variable.\r
-  @param[in]  CertData       Pointer to signer's certificates.\r
-  @param[in]  CertDataSize   Length of CertData in bytes.\r
-\r
-  @retval  EFI_INVALID_PARAMETER Any input parameter is invalid.\r
-  @retval  EFI_ACCESS_DENIED     An AUTH_CERT_DB_DATA entry with same VariableName\r
-                                 and VendorGuid already exists.\r
-  @retval  EFI_OUT_OF_RESOURCES  The operation is failed due to lack of resources.\r
-  @retval  EFI_SUCCESS           Insert an AUTH_CERT_DB_DATA entry to "certdb"\r
-\r
-**/\r
-EFI_STATUS\r
-InsertCertsToDb (\r
-  IN     CHAR16           *VariableName,\r
-  IN     EFI_GUID         *VendorGuid,\r
-  IN     UINT8            *CertData,\r
-  IN     UINTN            CertDataSize\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK  CertDbVariable;\r
-  EFI_STATUS              Status;\r
-  UINT8                   *Data;\r
-  UINTN                   DataSize;\r
-  UINT32                  VarAttr;\r
-  UINT8                   *NewCertDb;\r
-  UINT32                  NewCertDbSize;\r
-  UINT32                  CertNodeSize;\r
-  UINT32                  NameSize;\r
-  AUTH_CERT_DB_DATA       *Ptr;\r
-\r
-  if ((VariableName == NULL) || (VendorGuid == NULL) || (CertData == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Get variable "certdb".\r
-  //\r
-  Status = FindVariable (\r
-             EFI_CERT_DB_NAME,\r
-             &gEfiCertDbGuid,\r
-             &CertDbVariable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  DataSize  = DataSizeOfVariable (CertDbVariable.CurrPtr);\r
-  Data      = GetVariableDataPtr (CertDbVariable.CurrPtr);\r
-  if ((DataSize == 0) || (Data == NULL)) {\r
-    ASSERT (FALSE);\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  //\r
-  // Find whether matching cert node already exists in "certdb".\r
-  // If yes return error.\r
-  //\r
-  Status = FindCertsFromDb (\r
-             VariableName,\r
-             VendorGuid,\r
-             Data,\r
-             DataSize,\r
-             NULL,\r
-             NULL,\r
-             NULL,\r
-             NULL\r
-             );\r
-\r
-  if (!EFI_ERROR (Status)) {\r
-    ASSERT (FALSE);\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-\r
-  //\r
-  // Construct new data content of variable "certdb".\r
-  //\r
-  NameSize      = (UINT32) StrLen (VariableName);\r
-  CertNodeSize  = sizeof (AUTH_CERT_DB_DATA) + (UINT32) CertDataSize + NameSize * sizeof (CHAR16);\r
-  NewCertDbSize = (UINT32) DataSize + CertNodeSize;\r
-  if (NewCertDbSize > mMaxCertDbSize) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  NewCertDb     = (UINT8*) mCertDbStore;\r
-\r
-  //\r
-  // Copy the DB entries before deleting node.\r
-  //\r
-  CopyMem (NewCertDb, Data, DataSize);\r
-  //\r
-  // Update CertDbListSize.\r
-  //\r
-  CopyMem (NewCertDb, &NewCertDbSize, sizeof (UINT32));\r
-  //\r
-  // Construct new cert node.\r
-  //\r
-  Ptr = (AUTH_CERT_DB_DATA *) (NewCertDb + DataSize);\r
-  CopyGuid (&Ptr->VendorGuid, VendorGuid);\r
-  CopyMem (&Ptr->CertNodeSize, &CertNodeSize, sizeof (UINT32));\r
-  CopyMem (&Ptr->NameSize, &NameSize, sizeof (UINT32));\r
-  CopyMem (&Ptr->CertDataSize, &CertDataSize, sizeof (UINT32));\r
-\r
-  CopyMem (\r
-    (UINT8 *) Ptr + sizeof (AUTH_CERT_DB_DATA),\r
-    VariableName,\r
-    NameSize * sizeof (CHAR16)\r
-    );\r
-\r
-  CopyMem (\r
-    (UINT8 *) Ptr +  sizeof (AUTH_CERT_DB_DATA) + NameSize * sizeof (CHAR16),\r
-    CertData,\r
-    CertDataSize\r
-    );\r
-\r
-  //\r
-  // Set "certdb".\r
-  //\r
-  VarAttr  = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
-  Status   = UpdateVariable (\r
-               EFI_CERT_DB_NAME,\r
-               &gEfiCertDbGuid,\r
-               NewCertDb,\r
-               NewCertDbSize,\r
-               VarAttr,\r
-               0,\r
-               0,\r
-               &CertDbVariable,\r
-               NULL\r
-               );\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-  @param[in]  AuthVarType                 Verify against PK, KEK database, private database or certificate in data payload.\r
-  @param[out] VarDel                      Delete the variable or not.\r
-\r
-  @retval EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @retval EFI_SECURITY_VIOLATION          The variable does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @retval EFI_OUT_OF_RESOURCES            Failed to process variable due to lack\r
-                                          of resources.\r
-  @retval EFI_SUCCESS                     Variable pass validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-VerifyTimeBasedPayload (\r
-  IN     CHAR16                             *VariableName,\r
-  IN     EFI_GUID                           *VendorGuid,\r
-  IN     VOID                               *Data,\r
-  IN     UINTN                              DataSize,\r
-  IN     VARIABLE_POINTER_TRACK             *Variable,\r
-  IN     UINT32                             Attributes,\r
-  IN     AUTHVAR_TYPE                       AuthVarType,\r
-  OUT    BOOLEAN                            *VarDel\r
-  )\r
-{\r
-  UINT8                            *RootCert;\r
-  UINT8                            *SigData;\r
-  UINT8                            *PayloadPtr;\r
-  UINTN                            RootCertSize;\r
-  UINTN                            Index;\r
-  UINTN                            CertCount;\r
-  UINTN                            PayloadSize;\r
-  UINT32                           Attr;\r
-  UINT32                           SigDataSize;\r
-  UINT32                           KekDataSize;\r
-  BOOLEAN                          VerifyStatus;\r
-  EFI_STATUS                       Status;\r
-  EFI_SIGNATURE_LIST               *CertList;\r
-  EFI_SIGNATURE_DATA               *Cert;\r
-  VARIABLE_POINTER_TRACK           KekVariable;\r
-  EFI_VARIABLE_AUTHENTICATION_2    *CertData;\r
-  UINT8                            *NewData;\r
-  UINTN                            NewDataSize;\r
-  VARIABLE_POINTER_TRACK           PkVariable;\r
-  UINT8                            *Buffer;\r
-  UINTN                            Length;\r
-  UINT8                            *SignerCerts;\r
-  UINTN                            CertStackSize;\r
-  UINT8                            *CertsInCertDb;\r
-  UINT32                           CertsSizeinDb;\r
-\r
-  VerifyStatus           = FALSE;\r
-  CertData               = NULL;\r
-  NewData                = NULL;\r
-  Attr                   = Attributes;\r
-  SignerCerts            = NULL;\r
-  RootCert               = NULL;\r
-  CertsInCertDb          = NULL;\r
-\r
-  //\r
-  // When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is\r
-  // set, then the Data buffer shall begin with an instance of a complete (and serialized)\r
-  // EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be followed by the new\r
-  // variable value and DataSize shall reflect the combined size of the descriptor and the new\r
-  // variable value. The authentication descriptor is not part of the variable data and is not\r
-  // returned by subsequent calls to GetVariable().\r
-  //\r
-  CertData = (EFI_VARIABLE_AUTHENTICATION_2 *) Data;\r
-\r
-  //\r
-  // Verify that Pad1, Nanosecond, TimeZone, Daylight and Pad2 components of the\r
-  // TimeStamp value are set to zero.\r
-  //\r
-  if ((CertData->TimeStamp.Pad1 != 0) ||\r
-      (CertData->TimeStamp.Nanosecond != 0) ||\r
-      (CertData->TimeStamp.TimeZone != 0) ||\r
-      (CertData->TimeStamp.Daylight != 0) ||\r
-      (CertData->TimeStamp.Pad2 != 0)) {\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-  if ((Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) {\r
-    if (CompareTimeStamp (&CertData->TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
-      //\r
-      // TimeStamp check fail, suspicious replay attack, return EFI_SECURITY_VIOLATION.\r
-      //\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-  }\r
-\r
-  //\r
-  // wCertificateType should be WIN_CERT_TYPE_EFI_GUID.\r
-  // Cert type should be EFI_CERT_TYPE_PKCS7_GUID.\r
-  //\r
-  if ((CertData->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) ||\r
-      !CompareGuid (&CertData->AuthInfo.CertType, &gEfiCertPkcs7Guid)) {\r
-    //\r
-    // Invalid AuthInfo type, return EFI_SECURITY_VIOLATION.\r
-    //\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-  //\r
-  // Find out Pkcs7 SignedData which follows the EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
-  // AuthInfo.Hdr.dwLength is the length of the entire certificate, including the length of the header.\r
-  //\r
-  SigData = CertData->AuthInfo.CertData;\r
-  SigDataSize = CertData->AuthInfo.Hdr.dwLength - (UINT32) (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData));\r
-\r
-  //\r
-  // Find out the new data payload which follows Pkcs7 SignedData directly.\r
-  //\r
-  PayloadPtr = SigData + SigDataSize;\r
-  PayloadSize = DataSize - OFFSET_OF_AUTHINFO2_CERT_DATA - (UINTN) SigDataSize;\r
-\r
-  //\r
-  // Construct a serialization buffer of the values of the VariableName, VendorGuid and Attributes\r
-  // parameters of the SetVariable() call and the TimeStamp component of the\r
-  // EFI_VARIABLE_AUTHENTICATION_2 descriptor followed by the variable's new value\r
-  // i.e. (VariableName, VendorGuid, Attributes, TimeStamp, Data)\r
-  //\r
-  NewDataSize = PayloadSize + sizeof (EFI_TIME) + sizeof (UINT32) +\r
-                sizeof (EFI_GUID) + StrSize (VariableName) - sizeof (CHAR16);\r
-  //\r
-  // Here is to reuse scratch data area(at the end of volatile variable store)\r
-  // to reduce SMRAM consumption for SMM variable driver.\r
-  // The scratch buffer is enough to hold the serialized data and safe to use,\r
-  // because it will be used at here to do verification only first\r
-  // and then used in UpdateVariable() for a time based auth variable set.\r
-  //\r
-  NewData = (UINT8 *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
-\r
-  Buffer = NewData;\r
-  Length = StrLen (VariableName) * sizeof (CHAR16);\r
-  CopyMem (Buffer, VariableName, Length);\r
-  Buffer += Length;\r
-\r
-  Length = sizeof (EFI_GUID);\r
-  CopyMem (Buffer, VendorGuid, Length);\r
-  Buffer += Length;\r
-\r
-  Length = sizeof (UINT32);\r
-  CopyMem (Buffer, &Attr, Length);\r
-  Buffer += Length;\r
-\r
-  Length = sizeof (EFI_TIME);\r
-  CopyMem (Buffer, &CertData->TimeStamp, Length);\r
-  Buffer += Length;\r
-\r
-  CopyMem (Buffer, PayloadPtr, PayloadSize);\r
-\r
-  if (AuthVarType == AuthVarTypePk) {\r
-    //\r
-    // Verify that the signature has been made with the current Platform Key (no chaining for PK).\r
-    // First, get signer's certificates from SignedData.\r
-    //\r
-    VerifyStatus = Pkcs7GetSigners (\r
-                     SigData,\r
-                     SigDataSize,\r
-                     &SignerCerts,\r
-                     &CertStackSize,\r
-                     &RootCert,\r
-                     &RootCertSize\r
-                     );\r
-    if (!VerifyStatus) {\r
-      goto Exit;\r
-    }\r
-\r
-    //\r
-    // Second, get the current platform key from variable. Check whether it's identical with signer's certificates\r
-    // in SignedData. If not, return error immediately.\r
-    //\r
-    Status = FindVariable (\r
-               EFI_PLATFORM_KEY_NAME,\r
-               &gEfiGlobalVariableGuid,\r
-               &PkVariable,\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      VerifyStatus = FALSE;\r
-      goto Exit;\r
-    }\r
-    CertList = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (PkVariable.CurrPtr);\r
-    Cert     = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
-    if ((RootCertSize != (CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1))) ||\r
-        (CompareMem (Cert->SignatureData, RootCert, RootCertSize) != 0)) {\r
-      VerifyStatus = FALSE;\r
-      goto Exit;\r
-    }\r
-\r
-    //\r
-    // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
-    //\r
-    VerifyStatus = Pkcs7Verify (\r
-                     SigData,\r
-                     SigDataSize,\r
-                     RootCert,\r
-                     RootCertSize,\r
-                     NewData,\r
-                     NewDataSize\r
-                     );\r
-\r
-  } else if (AuthVarType == AuthVarTypeKek) {\r
-\r
-    //\r
-    // Get KEK database from variable.\r
-    //\r
-    Status = FindVariable (\r
-               EFI_KEY_EXCHANGE_KEY_NAME,\r
-               &gEfiGlobalVariableGuid,\r
-               &KekVariable,\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    //\r
-    // Ready to verify Pkcs7 SignedData. Go through KEK Signature Database to find out X.509 CertList.\r
-    //\r
-    KekDataSize      = KekVariable.CurrPtr->DataSize;\r
-    CertList         = (EFI_SIGNATURE_LIST *) GetVariableDataPtr (KekVariable.CurrPtr);\r
-    while ((KekDataSize > 0) && (KekDataSize >= CertList->SignatureListSize)) {\r
-      if (CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
-        Cert       = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
-        CertCount  = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
-        for (Index = 0; Index < CertCount; Index++) {\r
-          //\r
-          // Iterate each Signature Data Node within this CertList for a verify\r
-          //\r
-          RootCert      = Cert->SignatureData;\r
-          RootCertSize  = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);\r
-\r
-          //\r
-          // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
-          //\r
-          VerifyStatus = Pkcs7Verify (\r
-                           SigData,\r
-                           SigDataSize,\r
-                           RootCert,\r
-                           RootCertSize,\r
-                           NewData,\r
-                           NewDataSize\r
-                           );\r
-          if (VerifyStatus) {\r
-            goto Exit;\r
-          }\r
-          Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
-        }\r
-      }\r
-      KekDataSize -= CertList->SignatureListSize;\r
-      CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
-    }\r
-  } else if (AuthVarType == AuthVarTypePriv) {\r
-\r
-    //\r
-    // Process common authenticated variable except PK/KEK/DB/DBX/DBT.\r
-    // Get signer's certificates from SignedData.\r
-    //\r
-    VerifyStatus = Pkcs7GetSigners (\r
-                     SigData,\r
-                     SigDataSize,\r
-                     &SignerCerts,\r
-                     &CertStackSize,\r
-                     &RootCert,\r
-                     &RootCertSize\r
-                     );\r
-    if (!VerifyStatus) {\r
-      goto Exit;\r
-    }\r
-\r
-    //\r
-    // Get previously stored signer's certificates from certdb for existing\r
-    // variable. Check whether they are identical with signer's certificates\r
-    // in SignedData. If not, return error immediately.\r
-    //\r
-    if ((Variable->CurrPtr != NULL)) {\r
-      VerifyStatus = FALSE;\r
-\r
-      Status = GetCertsFromDb (VariableName, VendorGuid, &CertsInCertDb, &CertsSizeinDb);\r
-      if (EFI_ERROR (Status)) {\r
-        goto Exit;\r
-      }\r
-\r
-      if ((CertStackSize != CertsSizeinDb) ||\r
-          (CompareMem (SignerCerts, CertsInCertDb, CertsSizeinDb) != 0)) {\r
-        goto Exit;\r
-      }\r
-    }\r
-\r
-    VerifyStatus = Pkcs7Verify (\r
-                     SigData,\r
-                     SigDataSize,\r
-                     RootCert,\r
-                     RootCertSize,\r
-                     NewData,\r
-                     NewDataSize\r
-                     );\r
-    if (!VerifyStatus) {\r
-      goto Exit;\r
-    }\r
-\r
-    //\r
-    // Delete signer's certificates when delete the common authenticated variable.\r
-    //\r
-    if ((PayloadSize == 0) && (Variable->CurrPtr != NULL) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0)) {\r
-      Status = DeleteCertsFromDb (VariableName, VendorGuid);\r
-      if (EFI_ERROR (Status)) {\r
-        VerifyStatus = FALSE;\r
-        goto Exit;\r
-      }\r
-    } else if (Variable->CurrPtr == NULL && PayloadSize != 0) {\r
-      //\r
-      // Insert signer's certificates when adding a new common authenticated variable.\r
-      //\r
-      Status = InsertCertsToDb (VariableName, VendorGuid, SignerCerts, CertStackSize);\r
-      if (EFI_ERROR (Status)) {\r
-        VerifyStatus = FALSE;\r
-        goto Exit;\r
-      }\r
-    }\r
-  } else if (AuthVarType == AuthVarTypePayload) {\r
-    CertList = (EFI_SIGNATURE_LIST *) PayloadPtr;\r
-    Cert     = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
-    RootCert      = Cert->SignatureData;\r
-    RootCertSize  = CertList->SignatureSize - (sizeof (EFI_SIGNATURE_DATA) - 1);\r
-\r
-    // Verify Pkcs7 SignedData via Pkcs7Verify library.\r
-    //\r
-    VerifyStatus = Pkcs7Verify (\r
-                     SigData,\r
-                     SigDataSize,\r
-                     RootCert,\r
-                     RootCertSize,\r
-                     NewData,\r
-                     NewDataSize\r
-                     );\r
-  } else {\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-Exit:\r
-\r
-  if (AuthVarType == AuthVarTypePk || AuthVarType == AuthVarTypePriv) {\r
-    Pkcs7FreeSigners (RootCert);\r
-    Pkcs7FreeSigners (SignerCerts);\r
-  }\r
-\r
-  if (!VerifyStatus) {\r
-    return EFI_SECURITY_VIOLATION;\r
-  }\r
-\r
-  Status = CheckSignatureListFormat(VariableName, VendorGuid, PayloadPtr, PayloadSize);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  if ((PayloadSize == 0) && (VarDel != NULL)) {\r
-    *VarDel = TRUE;\r
-  }\r
-\r
-  //\r
-  // Final step: Update/Append Variable if it pass Pkcs7Verify\r
-  //\r
-  return UpdateVariable (\r
-           VariableName,\r
-           VendorGuid,\r
-           PayloadPtr,\r
-           PayloadSize,\r
-           Attributes,\r
-           0,\r
-           0,\r
-           Variable,\r
-           &CertData->TimeStamp\r
-           );\r
-}\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.h b/SecurityPkg/VariableAuthenticated/RuntimeDxe/AuthService.h
deleted file mode 100644 (file)
index 56def50..0000000
+++ /dev/null
@@ -1,360 +0,0 @@
-/** @file\r
-  The internal header file includes the common header files, defines\r
-  internal structure and functions used by AuthService module.\r
-\r
-  Caution: This module requires additional review when modified.\r
-  This driver will have external input - variable data. It may be input in SMM mode.\r
-  This external input must be validated carefully to avoid security issue like\r
-  buffer overflow, integer overflow.\r
-  Variable attribute should also be checked to avoid authentication bypass.\r
-     The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
-  which is assumed to be protected by platform.  All variable code and metadata in flash/SMM Memory\r
-  may not be modified without authorization. If platform fails to protect these resources, \r
-  the authentication service provided in this driver will be broken, and the behavior is undefined.\r
-\r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#ifndef _AUTHSERVICE_H_\r
-#define _AUTHSERVICE_H_\r
-\r
-#define EFI_CERT_TYPE_RSA2048_SHA256_SIZE 256\r
-#define EFI_CERT_TYPE_RSA2048_SIZE        256\r
-\r
-///\r
-/// Size of AuthInfo prior to the data payload.\r
-///\r
-#define AUTHINFO_SIZE ((OFFSET_OF (EFI_VARIABLE_AUTHENTICATION, AuthInfo)) + \\r
-                       (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) + \\r
-                       sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256))\r
-\r
-#define AUTHINFO2_SIZE(VarAuth2) ((OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) + \\r
-                                  (UINTN) ((EFI_VARIABLE_AUTHENTICATION_2 *) (VarAuth2))->AuthInfo.Hdr.dwLength)\r
-\r
-#define OFFSET_OF_AUTHINFO2_CERT_DATA ((OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) + \\r
-                                       (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)))\r
-\r
-///\r
-/// "AuthVarKeyDatabase" variable for the Public Key store.\r
-///\r
-#define AUTHVAR_KEYDB_NAME      L"AuthVarKeyDatabase"\r
-\r
-///\r
-/// "certdb" variable stores the signer's certificates for non PK/KEK/DB/DBX\r
-/// variables with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
-/// \r
-///\r
-#define EFI_CERT_DB_NAME        L"certdb"\r
-\r
-///\r
-/// Struct to record signature requirement defined by UEFI spec.\r
-/// For SigHeaderSize and SigDataSize, ((UINT32) ~0) means NO exact length requirement for this field.\r
-///\r
-typedef struct {\r
-  EFI_GUID    SigType;\r
-  // Expected SignatureHeader size in Bytes.\r
-  UINT32      SigHeaderSize;\r
-  // Expected SignatureData size in Bytes.\r
-  UINT32      SigDataSize;\r
-} EFI_SIGNATURE_ITEM;\r
-\r
-typedef enum {\r
-  AuthVarTypePk,\r
-  AuthVarTypeKek,\r
-  AuthVarTypePriv,\r
-  AuthVarTypePayload\r
-} AUTHVAR_TYPE;\r
-\r
-#pragma pack(1)\r
-typedef struct {\r
-  EFI_GUID    VendorGuid;\r
-  UINT32      CertNodeSize;\r
-  UINT32      NameSize;\r
-  UINT32      CertDataSize;\r
-  /// CHAR16  VariableName[NameSize];\r
-  /// UINT8   CertData[CertDataSize];\r
-} AUTH_CERT_DB_DATA;\r
-#pragma pack()\r
-\r
-/**\r
-  Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter\r
-  @return EFI_WRITE_PROTECTED             Variable is write-protected and needs authentication with\r
-                                          EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.\r
-  @return EFI_SECURITY_VIOLATION          The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
-                                          set, but the AuthInfo does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable is not write-protected, or passed validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVariable (\r
-  IN     CHAR16                             *VariableName,\r
-  IN     EFI_GUID                           *VendorGuid,\r
-  IN     VOID                               *Data,\r
-  IN     UINTN                              DataSize,\r
-  IN     VARIABLE_POINTER_TRACK             *Variable,\r
-  IN     UINT32                             Attributes\r
-  );\r
-\r
-/**\r
-  Update platform mode.\r
-\r
-  @param[in]      Mode                    SETUP_MODE or USER_MODE.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SUCCESS                     Update platform mode successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-UpdatePlatformMode (\r
-  IN  UINT32                    Mode\r
-  );\r
-\r
-/**\r
-  Initializes for authenticated varibale service.\r
-\r
-  @param[in] MaxAuthVariableSize    Reflect the overhead associated with the saving\r
-                                    of a single EFI authenticated variable with the exception\r
-                                    of the overhead associated with the length\r
-                                    of the string name of the EFI variable.\r
-\r
-  @retval EFI_SUCCESS           Function successfully executed.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resources.\r
-\r
-**/\r
-EFI_STATUS\r
-AutenticatedVariableServiceInitialize (\r
-  IN UINTN      MaxAuthVariableSize\r
-  );\r
-\r
-/**\r
-  Initializes for cryptlib service before use, include register algrithm and allocate scratch.\r
-\r
-**/\r
-VOID\r
-CryptLibraryInitialize (\r
-  VOID\r
-  );\r
-\r
-/**\r
-  Check input data form to make sure it is a valid EFI_SIGNATURE_LIST for PK/KEK variable.\r
-\r
-  @param[in]  VariableName                Name of Variable to be check.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Point to the variable data to be checked.\r
-  @param[in]  DataSize                    Size of Data.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid signature list format.\r
-  @return EFI_SUCCESS                     Passed signature list format check successfully.\r
-  \r
-**/\r
-EFI_STATUS\r
-CheckSignatureListFormat(\r
-  IN  CHAR16                    *VariableName,\r
-  IN  EFI_GUID                  *VendorGuid,\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize\r
-  );\r
-\r
-/**\r
-  Process variable with platform key for verification.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-  @param[in]  IsPk                        Indicate whether it is to process pk.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter\r
-  @return EFI_SECURITY_VIOLATION          The variable does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable passed validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVarWithPk (\r
-  IN  CHAR16                    *VariableName,\r
-  IN  EFI_GUID                  *VendorGuid,\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize,\r
-  IN  VARIABLE_POINTER_TRACK    *Variable,\r
-  IN  UINT32                    Attributes OPTIONAL,\r
-  IN  BOOLEAN                   IsPk\r
-  );\r
-\r
-/**\r
-  Process variable with key exchange key for verification.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information that is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SECURITY_VIOLATION          The variable does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @return EFI_SUCCESS                     Variable passed validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-ProcessVarWithKek (\r
-  IN  CHAR16                    *VariableName,\r
-  IN  EFI_GUID                  *VendorGuid,\r
-  IN  VOID                      *Data,\r
-  IN  UINTN                     DataSize,\r
-  IN  VARIABLE_POINTER_TRACK    *Variable,\r
-  IN  UINT32                    Attributes OPTIONAL\r
-  );\r
-\r
-/**\r
-  Merge two buffers which formatted as EFI_SIGNATURE_LIST. Only the new EFI_SIGNATURE_DATA\r
-  will be appended to the original EFI_SIGNATURE_LIST, duplicate EFI_SIGNATURE_DATA\r
-  will be ignored.\r
-\r
-  @param[in, out]  Data             Pointer to original EFI_SIGNATURE_LIST.\r
-  @param[in]       DataSize         Size of Data buffer.\r
-  @param[in]       FreeBufSize      Size of free data buffer \r
-  @param[in]       NewData          Pointer to new EFI_SIGNATURE_LIST to be appended.\r
-  @param[in]       NewDataSize      Size of NewData buffer.\r
-  @param[out]      MergedBufSize    Size of the merged buffer\r
-\r
-  @return EFI_BUFFER_TOO_SMALL if input Data buffer overflowed\r
-\r
-**/\r
-EFI_STATUS\r
-AppendSignatureList (\r
-  IN  OUT VOID            *Data,\r
-  IN  UINTN               DataSize,\r
-  IN  UINTN               FreeBufSize,\r
-  IN  VOID                *NewData,\r
-  IN  UINTN               NewDataSize,\r
-  OUT UINTN               *MergedBufSize\r
-  );\r
-\r
-/**\r
-  Compare two EFI_TIME data.\r
-\r
-\r
-  @param FirstTime           A pointer to the first EFI_TIME data.\r
-  @param SecondTime          A pointer to the second EFI_TIME data.\r
-\r
-  @retval  TRUE              The FirstTime is not later than the SecondTime.\r
-  @retval  FALSE             The FirstTime is later than the SecondTime.\r
-\r
-**/\r
-BOOLEAN\r
-CompareTimeStamp (\r
-  IN EFI_TIME               *FirstTime,\r
-  IN EFI_TIME               *SecondTime\r
-  );\r
-\r
-/**\r
-  Delete matching signer's certificates when deleting common authenticated\r
-  variable by corresponding VariableName and VendorGuid from "certdb".\r
-\r
-  @param[in]  VariableName   Name of authenticated Variable.\r
-  @param[in]  VendorGuid     Vendor GUID of authenticated Variable.\r
-\r
-  @retval  EFI_INVALID_PARAMETER Any input parameter is invalid.\r
-  @retval  EFI_NOT_FOUND         Fail to find "certdb" or matching certs.\r
-  @retval  EFI_OUT_OF_RESOURCES  The operation is failed due to lack of resources.\r
-  @retval  EFI_SUCCESS           The operation is completed successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-DeleteCertsFromDb (\r
-  IN     CHAR16           *VariableName,\r
-  IN     EFI_GUID         *VendorGuid\r
-  );\r
-\r
-/**\r
-  Process variable with EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-\r
-  @param[in]  VariableName                Name of Variable to be found.\r
-  @param[in]  VendorGuid                  Variable vendor GUID.\r
-  @param[in]  Data                        Data pointer.\r
-  @param[in]  DataSize                    Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in]  Variable                    The variable information which is used to keep track of variable usage.\r
-  @param[in]  Attributes                  Attribute value of the variable.\r
-  @param[in]  AuthVarType                 Verify against PK or KEK database or private database.\r
-  @param[out] VarDel                      Delete the variable or not.\r
-\r
-  @retval EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @retval EFI_SECURITY_VIOLATION          The variable does NOT pass the validation\r
-                                          check carried out by the firmware.\r
-  @retval EFI_OUT_OF_RESOURCES            Failed to process variable due to lack\r
-                                          of resources.\r
-  @retval EFI_SUCCESS                     Variable pass validation successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-VerifyTimeBasedPayload (\r
-  IN     CHAR16                             *VariableName,\r
-  IN     EFI_GUID                           *VendorGuid,\r
-  IN     VOID                               *Data,\r
-  IN     UINTN                              DataSize,\r
-  IN     VARIABLE_POINTER_TRACK             *Variable,\r
-  IN     UINT32                             Attributes,\r
-  IN     AUTHVAR_TYPE                       AuthVarType,\r
-  OUT    BOOLEAN                            *VarDel\r
-  );\r
-\r
-extern UINT8  *mPubKeyStore;\r
-extern UINT8  *mCertDbStore;\r
-extern UINT32 mPubKeyNumber;\r
-extern VOID   *mHashCtx;\r
-\r
-#endif\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Measurement.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Measurement.c
deleted file mode 100644 (file)
index 1b625e7..0000000
+++ /dev/null
@@ -1,255 +0,0 @@
-/** @file\r
-  Measure TrEE required variable.\r
-\r
-Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <PiDxe.h>\r
-#include <Guid/ImageAuthentication.h>\r
-#include <IndustryStandard/UefiTcgPlatform.h>\r
-#include <Protocol/TrEEProtocol.h>\r
-\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/UefiRuntimeServicesTableLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/TpmMeasurementLib.h>\r
-\r
-typedef struct {\r
-  CHAR16                                 *VariableName;\r
-  EFI_GUID                               *VendorGuid;\r
-} VARIABLE_TYPE;\r
-\r
-VARIABLE_TYPE  mVariableType[] = {\r
-  {EFI_SECURE_BOOT_MODE_NAME,    &gEfiGlobalVariableGuid},\r
-  {EFI_PLATFORM_KEY_NAME,        &gEfiGlobalVariableGuid},\r
-  {EFI_KEY_EXCHANGE_KEY_NAME,    &gEfiGlobalVariableGuid},\r
-  {EFI_IMAGE_SECURITY_DATABASE,  &gEfiImageSecurityDatabaseGuid},\r
-  {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},\r
-};\r
-\r
-/**\r
-  This function will return if this variable is SecureBootPolicy Variable.\r
-\r
-  @param[in]  VariableName      A Null-terminated string that is the name of the vendor's variable.\r
-  @param[in]  VendorGuid        A unique identifier for the vendor.\r
-\r
-  @retval TRUE  This is SecureBootPolicy Variable\r
-  @retval FALSE This is not SecureBootPolicy Variable\r
-**/\r
-BOOLEAN\r
-IsSecureBootPolicyVariable (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  )\r
-{\r
-  UINTN   Index;\r
-\r
-  for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {\r
-    if ((StrCmp (VariableName, mVariableType[Index].VariableName) == 0) && \r
-        (CompareGuid (VendorGuid, mVariableType[Index].VendorGuid))) {\r
-      return TRUE;\r
-    }\r
-  }\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-  Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r
-\r
-  @param[in]  VarName           A Null-terminated string that is the name of the vendor's variable.\r
-  @param[in]  VendorGuid        A unique identifier for the vendor.\r
-  @param[in]  VarData           The content of the variable data.  \r
-  @param[in]  VarSize           The size of the variable data.  \r
\r
-  @retval EFI_SUCCESS           Operation completed successfully.\r
-  @retval EFI_OUT_OF_RESOURCES  Out of memory.\r
-  @retval EFI_DEVICE_ERROR      The operation was unsuccessful.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-MeasureVariable (\r
-  IN      CHAR16                    *VarName,\r
-  IN      EFI_GUID                  *VendorGuid,\r
-  IN      VOID                      *VarData,\r
-  IN      UINTN                     VarSize\r
-  )\r
-{\r
-  EFI_STATUS                        Status;\r
-  UINTN                             VarNameLength;\r
-  EFI_VARIABLE_DATA_TREE            *VarLog;\r
-  UINT32                            VarLogSize;\r
-\r
-  ASSERT ((VarSize == 0 && VarData == NULL) || (VarSize != 0 && VarData != NULL));\r
-\r
-  VarNameLength      = StrLen (VarName);\r
-  VarLogSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize\r
-                        - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));\r
-\r
-  VarLog = (EFI_VARIABLE_DATA_TREE *) AllocateZeroPool (VarLogSize);\r
-  if (VarLog == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  CopyMem (&VarLog->VariableName, VendorGuid, sizeof(VarLog->VariableName));\r
-  VarLog->UnicodeNameLength  = VarNameLength;\r
-  VarLog->VariableDataLength = VarSize;\r
-  CopyMem (\r
-     VarLog->UnicodeName,\r
-     VarName,\r
-     VarNameLength * sizeof (*VarName)\r
-     );\r
-  if (VarSize != 0) {\r
-    CopyMem (\r
-       (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r
-       VarData,\r
-       VarSize\r
-       );\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "AuthVariableDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)7, (UINTN)EV_EFI_VARIABLE_AUTHORITY));\r
-  DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));\r
-\r
-  Status = TpmMeasureAndLogData (\r
-             7,\r
-             EV_EFI_VARIABLE_DRIVER_CONFIG,\r
-             VarLog,\r
-             VarLogSize,\r
-             VarLog,\r
-             VarLogSize\r
-             );\r
-  FreePool (VarLog);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Returns the status whether get the variable success. The function retrieves \r
-  variable  through the UEFI Runtime Service GetVariable().  The \r
-  returned buffer is allocated using AllocatePool().  The caller is responsible\r
-  for freeing this buffer with FreePool().\r
-\r
-  This API is only invoked in boot time. It may NOT be invoked at runtime.\r
-\r
-  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
-  @param[in]  Guid  The pointer to an EFI_GUID structure\r
-  @param[out] Value The buffer point saved the variable info.\r
-  @param[out] Size  The buffer size of the variable.\r
-\r
-  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return Others Errors             Return errors from call to gRT->GetVariable.\r
-\r
-**/\r
-EFI_STATUS\r
-InternalGetVariable (\r
-  IN CONST CHAR16    *Name,\r
-  IN CONST EFI_GUID  *Guid,\r
-  OUT VOID           **Value,\r
-  OUT UINTN          *Size\r
-  )\r
-{\r
-  EFI_STATUS  Status;\r
-  UINTN       BufferSize;\r
-\r
-  //\r
-  // Try to get the variable size.\r
-  //\r
-  BufferSize = 0;\r
-  *Value     = NULL;\r
-  if (Size != NULL) {\r
-    *Size  = 0;\r
-  }\r
-  \r
-  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
-  if (Status != EFI_BUFFER_TOO_SMALL) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Allocate buffer to get the variable.\r
-  //\r
-  *Value = AllocatePool (BufferSize);\r
-  ASSERT (*Value != NULL);\r
-  if (*Value == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  //\r
-  // Get the variable data.\r
-  //\r
-  Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool(*Value);\r
-    *Value = NULL;\r
-  }\r
-\r
-  if (Size != NULL) {\r
-    *Size = BufferSize;\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  SecureBoot Hook for SetVariable.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-SecureBootHook (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  )\r
-{\r
-  EFI_STATUS                        Status;\r
-  UINTN                             VariableDataSize;\r
-  VOID                              *VariableData;\r
-\r
-  if (!IsSecureBootPolicyVariable (VariableName, VendorGuid)) {\r
-    return ;\r
-  }\r
-\r
-  //\r
-  // We should NOT use Data and DataSize here,because it may include signature,\r
-  // or is just partial with append attributes, or is deleted.\r
-  // We should GetVariable again, to get full variable content.\r
-  //\r
-  Status = InternalGetVariable (\r
-             VariableName,\r
-             VendorGuid,\r
-             &VariableData,\r
-             &VariableDataSize\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    VariableData     = NULL;\r
-    VariableDataSize = 0;\r
-  }\r
-\r
-  Status = MeasureVariable (\r
-             VariableName,\r
-             VendorGuid,\r
-             VariableData,\r
-             VariableDataSize\r
-             );\r
-  DEBUG ((EFI_D_INFO, "MeasureBootPolicyVariable - %r\n", Status));\r
-\r
-  if (VariableData != NULL) {\r
-    FreePool (VariableData);\r
-  }\r
-\r
-  return ;\r
-}\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Reclaim.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Reclaim.c
deleted file mode 100644 (file)
index d79cd36..0000000
+++ /dev/null
@@ -1,161 +0,0 @@
-/** @file\r
-  Handles non-volatile variable store garbage collection, using FTW\r
-  (Fault Tolerant Write) protocol.\r
-\r
-Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-\r
-/**\r
-  Gets LBA of block and offset by given address.\r
-\r
-  This function gets the Logical Block Address (LBA) of a firmware\r
-  volume block containing the given address, and the offset of the\r
-  address on the block.\r
-\r
-  @param  Address        Address which should be contained\r
-                         by returned FVB handle.\r
-  @param  Lba            Pointer to LBA for output.\r
-  @param  Offset         Pointer to offset for output.\r
-\r
-  @retval EFI_SUCCESS    LBA and offset successfully returned.\r
-  @retval EFI_NOT_FOUND  Fail to find FVB handle by address.\r
-  @retval EFI_ABORTED    Fail to find valid LBA and offset.\r
-\r
-**/\r
-EFI_STATUS\r
-GetLbaAndOffsetByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS   Address,\r
-  OUT EFI_LBA                *Lba,\r
-  OUT UINTN                  *Offset\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
-  UINT32                              LbaIndex;\r
-\r
-  *Lba    = (EFI_LBA) (-1);\r
-  *Offset = 0;\r
-  Fvb = NULL;\r
-\r
-  //\r
-  // Get the proper FVB protocol.\r
-  //\r
-  Status = GetFvbInfoByAddress (Address, NULL, &Fvb);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // Get the Base Address of FV.\r
-  //\r
-  Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
-\r
-  //\r
-  // Get the (LBA, Offset) of Address.\r
-  //\r
-  if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-    //\r
-    // BUGBUG: Assume one FV has one type of BlockLength.\r
-    //\r
-    FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-    for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-      if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
-        //\r
-        // Found the (Lba, Offset).\r
-        //\r
-        *Lba    = LbaIndex - 1;\r
-        *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
-        return EFI_SUCCESS;\r
-     }\r
-    }\r
-  }\r
-\r
-  return EFI_ABORTED;\r
-}\r
-\r
-/**\r
-  Writes a buffer to variable storage space, in the working block.\r
-\r
-  This function writes a buffer to variable storage space into a firmware\r
-  volume block device. The destination is specified by parameter\r
-  VariableBase. Fault Tolerant Write protocol is used for writing.\r
-\r
-  @param  VariableBase   Base address of variable to write\r
-  @param  VariableBuffer Point to the variable data buffer.\r
-\r
-  @retval EFI_SUCCESS    The function completed successfully.\r
-  @retval EFI_NOT_FOUND  Fail to locate Fault Tolerant Write protocol.\r
-  @retval EFI_ABORTED    The function could not complete successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-FtwVariableSpace (\r
-  IN EFI_PHYSICAL_ADDRESS   VariableBase,\r
-  IN VARIABLE_STORE_HEADER  *VariableBuffer\r
-  )\r
-{\r
-  EFI_STATUS                         Status;\r
-  EFI_HANDLE                         FvbHandle;\r
-  EFI_LBA                            VarLba;\r
-  UINTN                              VarOffset;\r
-  UINTN                              FtwBufferSize;\r
-  EFI_FAULT_TOLERANT_WRITE_PROTOCOL  *FtwProtocol;\r
-\r
-  //\r
-  // Locate fault tolerant write protocol.\r
-  //\r
-  Status = GetFtwProtocol((VOID **) &FtwProtocol);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  // Locate Fvb handle by address.\r
-  //\r
-  Status = GetFvbInfoByAddress (VariableBase, &FvbHandle, NULL);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  //\r
-  // Get LBA and Offset by address.\r
-  //\r
-  Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
-  ASSERT (FtwBufferSize == VariableBuffer->Size);\r
-\r
-  //\r
-  // FTW write record.\r
-  //\r
-  Status = FtwProtocol->Write (\r
-                          FtwProtocol,\r
-                          VarLba,         // LBA\r
-                          VarOffset,      // Offset\r
-                          FtwBufferSize,  // NumBytes\r
-                          NULL,           // PrivateData NULL\r
-                          FvbHandle,      // Fvb Handle\r
-                          (VOID *) VariableBuffer // write buffer\r
-                          );\r
-\r
-  return Status;\r
-}\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VarCheck.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VarCheck.c
deleted file mode 100644 (file)
index 740632f..0000000
+++ /dev/null
@@ -1,1264 +0,0 @@
-/** @file\r
-  Implementation functions and structures for var check protocol.\r
-\r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-#include "AuthService.h"\r
-#include <Library/DevicePathLib.h>\r
-\r
-extern LIST_ENTRY mLockedVariableList;\r
-extern BOOLEAN mEndOfDxe;\r
-extern BOOLEAN mEnableLocking;\r
-\r
-#define VAR_CHECK_HANDLER_TABLE_SIZE    0x8\r
-\r
-UINT32                                  mNumberOfHandler = 0;\r
-UINT32                                  mMaxNumberOfHandler = 0;\r
-VAR_CHECK_SET_VARIABLE_CHECK_HANDLER    *mHandlerTable = NULL;\r
-\r
-typedef struct {\r
-  LIST_ENTRY                    Link;\r
-  EFI_GUID                      Guid;\r
-  VAR_CHECK_VARIABLE_PROPERTY   VariableProperty;\r
-  //CHAR16                        *Name;\r
-} VAR_CHECK_VARIABLE_ENTRY;\r
-\r
-LIST_ENTRY mVarCheckVariableList = INITIALIZE_LIST_HEAD_VARIABLE (mVarCheckVariableList);\r
-\r
-typedef\r
-EFI_STATUS\r
-(EFIAPI *INTERNAL_VAR_CHECK_FUNCTION) (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *Propery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  );\r
-\r
-typedef struct {\r
-  CHAR16                        *Name;\r
-  VAR_CHECK_VARIABLE_PROPERTY   VariableProperty;\r
-  INTERNAL_VAR_CHECK_FUNCTION   CheckFunction;\r
-} UEFI_DEFINED_VARIABLE_ENTRY;\r
-\r
-/**\r
-  Internal check for load option.\r
-\r
-  @param[in] VariablePropery    Pointer to variable property.\r
-  @param[in] DataSize           Data size.\r
-  @param[in] Data               Pointer to data buffer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER The data buffer is not a valid load option.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckLoadOption (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariablePropery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  )\r
-{\r
-  UINT16                    FilePathListLength;\r
-  CHAR16                    *Description;\r
-  EFI_DEVICE_PATH_PROTOCOL  *FilePathList;\r
-\r
-  FilePathListLength = *((UINT16 *) ((UINTN) Data + sizeof (UINT32)));\r
-\r
-  //\r
-  // Check Description\r
-  //\r
-  Description = (CHAR16 *) ((UINTN) Data + sizeof (UINT32) + sizeof (UINT16));\r
-  while (Description < (CHAR16 *) ((UINTN) Data + DataSize)) {\r
-    if (*Description == L'\0') {\r
-      break;\r
-    }\r
-    Description++;\r
-  }\r
-  if ((UINTN) Description >= ((UINTN) Data + DataSize)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  Description++;\r
-\r
-  //\r
-  // Check FilePathList\r
-  //\r
-  FilePathList = (EFI_DEVICE_PATH_PROTOCOL *) Description;\r
-  if ((UINTN) FilePathList > (MAX_ADDRESS - FilePathListLength)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  if (((UINTN) FilePathList + FilePathListLength) > ((UINTN) Data + DataSize)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  if (FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  if (!IsDevicePathValid (FilePathList, FilePathListLength)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Internal check for key option.\r
-\r
-  @param[in] VariablePropery    Pointer to variable property.\r
-  @param[in] DataSize           Data size.\r
-  @param[in] Data               Pointer to data buffer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER The data buffer is not a valid key option.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckKeyOption (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariablePropery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  )\r
-{\r
-  if (((DataSize - sizeof (EFI_KEY_OPTION)) % sizeof (EFI_INPUT_KEY)) != 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Internal check for device path.\r
-\r
-  @param[in] VariablePropery    Pointer to variable property.\r
-  @param[in] DataSize           Data size.\r
-  @param[in] Data               Pointer to data buffer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER The data buffer is not a valid device path.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckDevicePath (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariablePropery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  )\r
-{\r
-  if (!IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *) Data, DataSize)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Internal check for ASCII string.\r
-\r
-  @param[in] VariablePropery    Pointer to variable property.\r
-  @param[in] DataSize           Data size.\r
-  @param[in] Data               Pointer to data buffer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER The data buffer is not a Null-terminated ASCII string.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckAsciiString (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariablePropery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  )\r
-{\r
-  CHAR8     *String;\r
-  UINTN     Index;\r
-\r
-  String = (CHAR8 *) Data;\r
-  if (String[DataSize - 1] == '\0') {\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    for (Index = 1; Index < DataSize && (String[DataSize - 1 - Index] != '\0'); Index++);\r
-    if (Index == DataSize) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Internal check for size array.\r
-\r
-  @param[in] VariablePropery    Pointer to variable property.\r
-  @param[in] DataSize           Data size.\r
-  @param[in] Data               Pointer to data buffer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER The DataSize is not size array.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckSizeArray (\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariablePropery,\r
-  IN UINTN                          DataSize,\r
-  IN VOID                           *Data\r
-  )\r
-{\r
-  if ((DataSize % VariablePropery->MinSize) != 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-//\r
-// To prevent name collisions with possible future globally defined variables,\r
-// other internal firmware data variables that are not defined here must be\r
-// saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or\r
-// any other GUID defined by the UEFI Specification. Implementations must\r
-// only permit the creation of variables with a UEFI Specification-defined\r
-// VendorGuid when these variables are documented in the UEFI Specification.\r
-//\r
-UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList[] = {\r
-  {\r
-    EFI_LANG_CODES_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckAsciiString\r
-  },\r
-  {\r
-    EFI_LANG_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckAsciiString\r
-  },\r
-  {\r
-    EFI_TIME_OUT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      sizeof (UINT16)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_PLATFORM_LANG_CODES_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckAsciiString\r
-  },\r
-  {\r
-    EFI_PLATFORM_LANG_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckAsciiString\r
-  },\r
-  {\r
-    EFI_CON_IN_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_CON_OUT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_ERR_OUT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_CON_IN_DEV_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_CON_OUT_DEV_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_ERR_OUT_DEV_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckDevicePath\r
-  },\r
-  {\r
-    EFI_BOOT_ORDER_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckSizeArray\r
-  },\r
-  {\r
-    EFI_BOOT_NEXT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      sizeof (UINT16)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_BOOT_CURRENT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT16),\r
-      sizeof (UINT16)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT32),\r
-      sizeof (UINT32)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_DRIVER_ORDER_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckSizeArray\r
-  },\r
-  {\r
-    EFI_SYS_PREP_ORDER_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckSizeArray\r
-  },\r
-  {\r
-    EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT16),\r
-      sizeof (UINT16)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_SETUP_MODE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_KEY_EXCHANGE_KEY_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_PLATFORM_KEY_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_SIGNATURE_SUPPORT_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (EFI_GUID),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckSizeArray\r
-  },\r
-  {\r
-    EFI_SECURE_BOOT_MODE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_KEK_DEFAULT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_PK_DEFAULT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_DB_DEFAULT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_DBX_DEFAULT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_DBT_DEFAULT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT64),\r
-      sizeof (UINT64)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_OS_INDICATIONS_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT64),\r
-      sizeof (UINT64)\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_VENDOR_KEYS_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_BS_RT,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    },\r
-    NULL\r
-  },\r
-};\r
-UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {\r
-  {\r
-    L"Boot####",\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT32) + sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckLoadOption\r
-  },\r
-  {\r
-    L"Driver####",\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT32) + sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckLoadOption\r
-  },\r
-  {\r
-    L"SysPrep####",\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT32) + sizeof (UINT16),\r
-      MAX_UINTN\r
-    },\r
-    InternalVarCheckLoadOption\r
-  },\r
-  {\r
-    L"Key####",\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_KEY_OPTION),\r
-      sizeof (EFI_KEY_OPTION) + 3 * sizeof (EFI_INPUT_KEY)\r
-    },\r
-    InternalVarCheckKeyOption\r
-  },\r
-};\r
-\r
-//\r
-// EFI_IMAGE_SECURITY_DATABASE_GUID\r
-//\r
-UEFI_DEFINED_VARIABLE_ENTRY mImageSecurityVariableList[] = {\r
-  {\r
-    EFI_IMAGE_SECURITY_DATABASE,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_IMAGE_SECURITY_DATABASE1,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-  {\r
-    EFI_IMAGE_SECURITY_DATABASE2,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      1,\r
-      MAX_UINTN\r
-    },\r
-    NULL\r
-  },\r
-};\r
-\r
-typedef struct {\r
-  EFI_GUID                      *Guid;\r
-  CHAR16                        *Name;\r
-  VAR_CHECK_VARIABLE_PROPERTY   VariableProperty;\r
-  INTERNAL_VAR_CHECK_FUNCTION   CheckFunction;\r
-} VARIABLE_DRIVER_VARIABLE_ENTRY;\r
-\r
-VARIABLE_DRIVER_VARIABLE_ENTRY mVariableDriverVariableList[] = {\r
-  {\r
-    &gEfiSecureBootEnableDisableGuid,\r
-    EFI_SECURE_BOOT_ENABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    }\r
-  },\r
-  {\r
-    &gEfiCustomModeEnableGuid,\r
-    EFI_CUSTOM_MODE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      0,\r
-      VARIABLE_ATTRIBUTE_NV_BS,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    }\r
-  },\r
-  {\r
-    &gEfiVendorKeysNvGuid,\r
-    EFI_VENDOR_KEYS_NV_VARIABLE_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
-    }\r
-  },\r
-  {\r
-    &gEfiAuthenticatedVariableGuid,\r
-    AUTHVAR_KEYDB_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AW,\r
-      sizeof (UINT8),\r
-      MAX_UINTN\r
-    }\r
-  },\r
-  {\r
-    &gEfiCertDbGuid,\r
-    EFI_CERT_DB_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT_AT,\r
-      sizeof (UINT32),\r
-      MAX_UINTN\r
-    }\r
-  },\r
-  {\r
-    &gEdkiiVarErrorFlagGuid,\r
-    VAR_ERROR_FLAG_NAME,\r
-    {\r
-      VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
-      VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (VAR_ERROR_FLAG),\r
-      sizeof (VAR_ERROR_FLAG)\r
-    }\r
-  },\r
-};\r
-\r
-/**\r
-  Get UEFI defined global variable or image security database variable property.\r
-  The code will check if variable guid is global variable or image security database guid first.\r
-  If yes, further check if variable name is in mGlobalVariableList, mGlobalVariableList2 or mImageSecurityVariableList.\r
-\r
-  @param[in]  VariableName      Pointer to variable name.\r
-  @param[in]  VendorGuid        Variable Vendor Guid.\r
-  @param[in]  WildcardMatch     Try wildcard match or not.\r
-  @param[out] VariableProperty  Pointer to variable property.\r
-  @param[out] VarCheckFunction  Pointer to check function.\r
-\r
-  @retval EFI_SUCCESS           Variable is not global variable or image security database variable.\r
-  @retval EFI_INVALID_PARAMETER Variable is global variable or image security database variable, but variable name is not in the lists.\r
-\r
-**/\r
-EFI_STATUS\r
-GetUefiDefinedVariableProperty (\r
-  IN CHAR16                         *VariableName,\r
-  IN EFI_GUID                       *VendorGuid,\r
-  IN BOOLEAN                        WildcardMatch,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   **VariableProperty,\r
-  OUT INTERNAL_VAR_CHECK_FUNCTION   *VarCheckFunction OPTIONAL\r
-  )\r
-{\r
-  UINTN     Index;\r
-  UINTN     NameLength;\r
-\r
-  if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {\r
-    //\r
-    // Try list 1, exactly match.\r
-    //\r
-    for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {\r
-      if (StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) {\r
-        if (VarCheckFunction != NULL) {\r
-          *VarCheckFunction = mGlobalVariableList[Index].CheckFunction;\r
-        }\r
-        *VariableProperty = &mGlobalVariableList[Index].VariableProperty;\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-\r
-    //\r
-    // Try list 2.\r
-    //\r
-    NameLength = StrLen (VariableName) - 4;\r
-    for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {\r
-      if (WildcardMatch) {\r
-        if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&\r
-            (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&\r
-            IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&\r
-            IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&\r
-            IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&\r
-            IsHexaDecimalDigitCharacter (VariableName[NameLength + 3])) {\r
-          if (VarCheckFunction != NULL) {\r
-            *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;\r
-          }\r
-          *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;\r
-          return EFI_SUCCESS;\r
-        }\r
-      }\r
-      if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) {\r
-        if (VarCheckFunction != NULL) {\r
-          *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;\r
-        }\r
-        *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-\r
-    //\r
-    // The variable name is not in the lists.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid)){\r
-    for (Index = 0; Index < sizeof (mImageSecurityVariableList)/sizeof (mImageSecurityVariableList[0]); Index++) {\r
-      if (StrCmp (mImageSecurityVariableList[Index].Name, VariableName) == 0) {\r
-        if (VarCheckFunction != NULL) {\r
-          *VarCheckFunction = mImageSecurityVariableList[Index].CheckFunction;\r
-        }\r
-        *VariableProperty = &mImageSecurityVariableList[Index].VariableProperty;\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // It is not global variable or image security database variable.\r
-  //\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Get variable property for variables managed by Varaible driver.\r
-\r
-  @param[in]  VariableName      Pointer to variable name.\r
-  @param[in]  VendorGuid        Variable Vendor Guid.\r
-\r
-  @return Pointer to variable property.\r
-\r
-**/\r
-VAR_CHECK_VARIABLE_PROPERTY *\r
-GetVariableDriverVariableProperty (\r
-  IN CHAR16                         *VariableName,\r
-  IN EFI_GUID                       *VendorGuid\r
-  )\r
-{\r
-  UINTN     Index;\r
-\r
-  for (Index = 0; Index < sizeof (mVariableDriverVariableList)/sizeof (mVariableDriverVariableList[0]); Index++) {\r
-    if ((CompareGuid (mVariableDriverVariableList[Index].Guid, VendorGuid)) && (StrCmp (mVariableDriverVariableList[Index].Name, VariableName) == 0)) {\r
-      return &mVariableDriverVariableList[Index].VariableProperty;\r
-    }\r
-  }\r
-\r
-  return NULL;\r
-}\r
-\r
-/**\r
-  Internal SetVariable check.\r
-\r
-  @param[in] VariableName       Name of Variable to set.\r
-  @param[in] VendorGuid         Variable vendor GUID.\r
-  @param[in] Attributes         Attribute value of the variable.\r
-  @param[in] DataSize           Size of Data to set.\r
-  @param[in] Data               Data pointer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied,\r
-                                or the DataSize exceeds the minimum or maximum allowed,\r
-                                or the Data value is not following UEFI spec for UEFI defined variables.\r
-  @retval EFI_WRITE_PROTECTED   The variable in question is read-only.\r
-  @retval Others                The return status from check handler.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckSetVariableCheck (\r
-  IN CHAR16     *VariableName,\r
-  IN EFI_GUID   *VendorGuid,\r
-  IN UINT32     Attributes,\r
-  IN UINTN      DataSize,\r
-  IN VOID       *Data\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  UINTN                         Index;\r
-  LIST_ENTRY                    *Link;\r
-  VAR_CHECK_VARIABLE_ENTRY      *Entry;\r
-  CHAR16                        *Name;\r
-  VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
-  INTERNAL_VAR_CHECK_FUNCTION   VarCheckFunction;\r
-\r
-  if (!mEndOfDxe) {\r
-    //\r
-    // Only do check after End Of Dxe.\r
-    //\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  Property = NULL;\r
-  VarCheckFunction = NULL;\r
-\r
-  for ( Link = GetFirstNode (&mVarCheckVariableList)\r
-      ; !IsNull (&mVarCheckVariableList, Link)\r
-      ; Link = GetNextNode (&mVarCheckVariableList, Link)\r
-      ) {\r
-    Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);\r
-    Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-    if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
-      Property = &Entry->VariableProperty;\r
-      break;\r
-    }\r
-  }\r
-  if (Property == NULL) {\r
-    Property = GetVariableDriverVariableProperty (VariableName, VendorGuid);\r
-  }\r
-  if (Property == NULL) {\r
-    Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Var Check UEFI defined variable fail %r - %g:%s\n", Status, VendorGuid, VariableName));\r
-      return Status;\r
-    }\r
-  }\r
-  if (Property != NULL) {\r
-    if (mEnableLocking && ((Property->Property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) != 0)) {\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Var Check ReadOnly variable fail %r - %g:%s\n", EFI_WRITE_PROTECTED, VendorGuid, VariableName));\r
-      return EFI_WRITE_PROTECTED;\r
-    }\r
-    if (!((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0))) {\r
-      //\r
-      // Not to delete variable.\r
-      //\r
-      if ((Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Property->Attributes) {\r
-        DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes(0x%08x to 0x%08x) fail %r - %g:%s\n", Property->Attributes, Attributes, EFI_INVALID_PARAMETER, VendorGuid, VariableName));\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-      if (DataSize != 0) {\r
-        if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) {\r
-          DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail(0x%x not in 0x%x - 0x%x) %r - %g:%s\n", DataSize, Property->MinSize, Property->MaxSize, EFI_INVALID_PARAMETER, VendorGuid, VariableName));\r
-          return EFI_INVALID_PARAMETER;\r
-        }\r
-        if (VarCheckFunction != NULL) {\r
-          Status = VarCheckFunction (\r
-                     Property,\r
-                     DataSize,\r
-                     Data\r
-                     );\r
-          if (EFI_ERROR (Status)) {\r
-            DEBUG ((EFI_D_INFO, "[Variable]: Internal Var Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName));\r
-            return Status;\r
-          }\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  for (Index = 0; Index < mNumberOfHandler; Index ++) {\r
-    Status = mHandlerTable[Index] (\r
-               VariableName,\r
-               VendorGuid,\r
-               Attributes,\r
-               DataSize,\r
-               Data\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Var Check handler fail %r - %g:%s\n", Status, VendorGuid, VariableName));\r
-      return Status;\r
-    }\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Reallocates more global memory to store the registered handler list.\r
-\r
-  @retval RETURN_SUCCESS            Reallocate memory successfully.\r
-  @retval RETURN_OUT_OF_RESOURCES   No enough memory to allocate.\r
-\r
-**/\r
-RETURN_STATUS\r
-EFIAPI\r
-ReallocateHandlerTable (\r
-  VOID\r
-  )\r
-{\r
-  VAR_CHECK_SET_VARIABLE_CHECK_HANDLER  *HandlerTable;\r
-\r
-  //\r
-  // Reallocate memory for check handler table.\r
-  //\r
-  HandlerTable = ReallocateRuntimePool (\r
-                     mMaxNumberOfHandler * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER), \r
-                     (mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE) * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER), \r
-                     mHandlerTable\r
-                     );\r
-\r
-  //\r
-  // No enough resource to allocate.\r
-  //\r
-  if (HandlerTable == NULL) {\r
-    return RETURN_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  mHandlerTable = HandlerTable;\r
-  //\r
-  // Increase max handler number.\r
-  //\r
-  mMaxNumberOfHandler = mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE;\r
-  return RETURN_SUCCESS;\r
-}\r
-\r
-/**\r
-  Register SetVariable check handler.\r
-\r
-  @param[in] Handler            Pointer to check handler.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check handler was registered successfully.\r
-  @retval EFI_INVALID_PARAMETER Handler is NULL.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the SetVariable check handler register request.\r
-  @retval EFI_UNSUPPORTED       This interface is not implemented.\r
-                                For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckRegisterSetVariableCheckHandler (\r
-  IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER   Handler\r
-  )\r
-{\r
-  EFI_STATUS    Status;\r
-\r
-  if (Handler == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (mEndOfDxe) {\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "RegisterSetVariableCheckHandler - 0x%x\n", Handler));\r
-\r
-  //\r
-  // Check whether the handler list is enough to store new handler.\r
-  //\r
-  if (mNumberOfHandler == mMaxNumberOfHandler) {\r
-    //\r
-    // Allocate more resources for new handler.\r
-    //\r
-    Status = ReallocateHandlerTable();\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Register new handler into the handler list.\r
-  //\r
-  mHandlerTable[mNumberOfHandler] = Handler;\r
-  mNumberOfHandler ++;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Variable property get function.\r
-\r
-  @param[in] Name           Pointer to the variable name.\r
-  @param[in] Guid           Pointer to the vendor GUID.\r
-  @param[in] WildcardMatch  Try wildcard match or not.\r
-\r
-  @return Pointer to the property of variable specified by the Name and Guid.\r
-\r
-**/\r
-VAR_CHECK_VARIABLE_PROPERTY *\r
-VariablePropertyGetFunction (\r
-  IN CHAR16                 *Name,\r
-  IN EFI_GUID               *Guid,\r
-  IN BOOLEAN                WildcardMatch\r
-  )\r
-{\r
-  LIST_ENTRY                    *Link;\r
-  VAR_CHECK_VARIABLE_ENTRY      *Entry;\r
-  CHAR16                        *VariableName;\r
-  VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
-\r
-  for ( Link = GetFirstNode (&mVarCheckVariableList)\r
-      ; !IsNull (&mVarCheckVariableList, Link)\r
-      ; Link = GetNextNode (&mVarCheckVariableList, Link)\r
-      ) {\r
-    Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);\r
-    VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-    if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {\r
-      return &Entry->VariableProperty;\r
-    }\r
-  }\r
-\r
-  Property = GetVariableDriverVariableProperty (Name, Guid);\r
-  if (Property == NULL) {\r
-    GetUefiDefinedVariableProperty (Name, Guid, WildcardMatch, &Property, NULL);\r
-  }\r
-\r
-  return Property;\r
-}\r
-\r
-/**\r
-  Variable property set.\r
-\r
-  @param[in] Name               Pointer to the variable name.\r
-  @param[in] Guid               Pointer to the vendor GUID.\r
-  @param[in] VariableProperty   Pointer to the input variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was set successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r
-                                or the fields of VariableProperty are not valid.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the variable property set request.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertySet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariableProperty\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  VAR_CHECK_VARIABLE_ENTRY      *Entry;\r
-  CHAR16                        *VariableName;\r
-  VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
-\r
-  if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (mEndOfDxe) {\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-\r
-  Status = EFI_SUCCESS;\r
-\r
-  AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Property = VariablePropertyGetFunction (Name, Guid, FALSE);\r
-  if (Property != NULL) {\r
-    CopyMem (Property, VariableProperty, sizeof (*VariableProperty));\r
-  } else {\r
-    Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (Name));\r
-    if (Entry == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto Done;\r
-    }\r
-    VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-    StrnCpy (VariableName, Name, StrLen (Name));\r
-    CopyGuid (&Entry->Guid, Guid);\r
-    CopyMem (&Entry->VariableProperty, VariableProperty, sizeof (*VariableProperty));\r
-    InsertTailList (&mVarCheckVariableList, &Entry->Link);\r
-  }\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Internal variable property get.\r
-\r
-  @param[in]  Name              Pointer to the variable name.\r
-  @param[in]  Guid              Pointer to the vendor GUID.\r
-  @param[out] VariableProperty  Pointer to the output variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was got successfully.\r
-  @retval EFI_NOT_FOUND         The property of variable specified by the Name and Guid was not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
-  )\r
-{\r
-  LIST_ENTRY                    *Link;\r
-  VARIABLE_ENTRY                *Entry;\r
-  CHAR16                        *VariableName;\r
-  BOOLEAN                       Found;\r
-  VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
-\r
-  Found = FALSE;\r
-\r
-  Property = VariablePropertyGetFunction (Name, Guid, TRUE);\r
-  if (Property != NULL) {\r
-    CopyMem (VariableProperty, Property, sizeof (*VariableProperty));\r
-    Found = TRUE;\r
-  }\r
-\r
-  for ( Link = GetFirstNode (&mLockedVariableList)\r
-      ; !IsNull (&mLockedVariableList, Link)\r
-      ; Link = GetNextNode (&mLockedVariableList, Link)\r
-      ) {\r
-    Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
-    VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-    if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {\r
-      VariableProperty->Property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;\r
-      if (!Found) {\r
-        VariableProperty->Revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;\r
-        Found = TRUE;\r
-      }\r
-    }\r
-  }\r
-\r
-  return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);\r
-}\r
-\r
-/**\r
-  Variable property get.\r
-\r
-  @param[in]  Name              Pointer to the variable name.\r
-  @param[in]  Guid              Pointer to the vendor GUID.\r
-  @param[out] VariableProperty  Pointer to the output variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was got successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r
-  @retval EFI_NOT_FOUND         The property of variable specified by the Name and Guid was not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
-  )\r
-{\r
-  EFI_STATUS    Status;\r
-\r
-  if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Status = InternalVarCheckVariablePropertyGet (Name, Guid, VariableProperty);\r
-\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  return Status;\r
-}\r
-\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c
deleted file mode 100644 (file)
index 15d0531..0000000
+++ /dev/null
@@ -1,4170 +0,0 @@
-/** @file\r
-  The common variable operation routines shared by DXE_RUNTIME variable\r
-  module and DXE_SMM variable module.\r
-\r
-  Caution: This module requires additional review when modified.\r
-  This driver will have external input - variable data. They may be input in SMM mode.\r
-  This external input must be validated carefully to avoid security issue like\r
-  buffer overflow, integer overflow.\r
-\r
-  VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.\r
-  They need check input parameter.\r
-\r
-  VariableServiceGetVariable() and VariableServiceSetVariable() are external API\r
-  to receive datasize and data buffer. The size should be checked carefully.\r
-\r
-  VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r
-  integer overflow. It should also check attribute to avoid authentication bypass.\r
-\r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-#include "AuthService.h"\r
-\r
-VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;\r
-\r
-///\r
-/// Define a memory cache that improves the search performance for a variable.\r
-///\r
-VARIABLE_STORE_HEADER  *mNvVariableCache      = NULL;\r
-\r
-///\r
-/// The memory entry used for variable statistics data.\r
-///\r
-VARIABLE_INFO_ENTRY    *gVariableInfo         = NULL;\r
-\r
-///\r
-/// The list to store the variables which cannot be set after the EFI_END_OF_DXE_EVENT_GROUP_GUID\r
-/// or EVT_GROUP_READY_TO_BOOT event.\r
-///\r
-LIST_ENTRY             mLockedVariableList    = INITIALIZE_LIST_HEAD_VARIABLE (mLockedVariableList);\r
-\r
-///\r
-/// The flag to indicate whether the platform has left the DXE phase of execution.\r
-///\r
-BOOLEAN                mEndOfDxe              = FALSE;\r
-\r
-///\r
-/// The flag to indicate whether the variable storage locking is enabled.\r
-///\r
-BOOLEAN                mEnableLocking         = TRUE;\r
-\r
-//\r
-// It will record the current boot error flag before EndOfDxe.\r
-//\r
-VAR_ERROR_FLAG         mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;\r
-\r
-/**\r
-\r
-  SecureBoot Hook for auth variable update.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-**/\r
-VOID\r
-EFIAPI\r
-SecureBootHook (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  );\r
-\r
-/**\r
-  Routine used to track statistical information about variable usage.\r
-  The data is stored in the EFI system table so it can be accessed later.\r
-  VariableInfo.efi can dump out the table. Only Boot Services variable\r
-  accesses are tracked by this code. The PcdVariableCollectStatistics\r
-  build flag controls if this feature is enabled.\r
-\r
-  A read that hits in the cache will have Read and Cache true for\r
-  the transaction. Data is allocated by this routine, but never\r
-  freed.\r
-\r
-  @param[in] VariableName   Name of the Variable to track.\r
-  @param[in] VendorGuid     Guid of the Variable to track.\r
-  @param[in] Volatile       TRUE if volatile FALSE if non-volatile.\r
-  @param[in] Read           TRUE if GetVariable() was called.\r
-  @param[in] Write          TRUE if SetVariable() was called.\r
-  @param[in] Delete         TRUE if deleted via SetVariable().\r
-  @param[in] Cache          TRUE for a cache hit.\r
-\r
-**/\r
-VOID\r
-UpdateVariableInfo (\r
-  IN  CHAR16                  *VariableName,\r
-  IN  EFI_GUID                *VendorGuid,\r
-  IN  BOOLEAN                 Volatile,\r
-  IN  BOOLEAN                 Read,\r
-  IN  BOOLEAN                 Write,\r
-  IN  BOOLEAN                 Delete,\r
-  IN  BOOLEAN                 Cache\r
-  )\r
-{\r
-  VARIABLE_INFO_ENTRY   *Entry;\r
-\r
-  if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
-\r
-    if (AtRuntime ()) {\r
-      // Don't collect statistics at runtime.\r
-      return;\r
-    }\r
-\r
-    if (gVariableInfo == NULL) {\r
-      //\r
-      // On the first call allocate a entry and place a pointer to it in\r
-      // the EFI System Table.\r
-      //\r
-      gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
-      ASSERT (gVariableInfo != NULL);\r
-\r
-      CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
-      gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
-      ASSERT (gVariableInfo->Name != NULL);\r
-      StrCpy (gVariableInfo->Name, VariableName);\r
-      gVariableInfo->Volatile = Volatile;\r
-    }\r
-\r
-\r
-    for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
-      if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
-        if (StrCmp (VariableName, Entry->Name) == 0) {\r
-          if (Read) {\r
-            Entry->ReadCount++;\r
-          }\r
-          if (Write) {\r
-            Entry->WriteCount++;\r
-          }\r
-          if (Delete) {\r
-            Entry->DeleteCount++;\r
-          }\r
-          if (Cache) {\r
-            Entry->CacheCount++;\r
-          }\r
-\r
-          return;\r
-        }\r
-      }\r
-\r
-      if (Entry->Next == NULL) {\r
-        //\r
-        // If the entry is not in the table add it.\r
-        // Next iteration of the loop will fill in the data.\r
-        //\r
-        Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
-        ASSERT (Entry->Next != NULL);\r
-\r
-        CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
-        Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
-        ASSERT (Entry->Next->Name != NULL);\r
-        StrCpy (Entry->Next->Name, VariableName);\r
-        Entry->Next->Volatile = Volatile;\r
-      }\r
-\r
-    }\r
-  }\r
-}\r
-\r
-\r
-/**\r
-\r
-  This code checks if variable header is valid or not.\r
-\r
-  @param Variable           Pointer to the Variable Header.\r
-  @param VariableStoreEnd   Pointer to the Variable Store End.\r
-\r
-  @retval TRUE              Variable header is valid.\r
-  @retval FALSE             Variable header is not valid.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidVariableHeader (\r
-  IN  VARIABLE_HEADER       *Variable,\r
-  IN  VARIABLE_HEADER       *VariableStoreEnd\r
-  )\r
-{\r
-  if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r
-    //\r
-    // Variable is NULL or has reached the end of variable store,\r
-    // or the StartId is not correct.\r
-    //\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-\r
-/**\r
-\r
-  This function writes data to the FWH at the correct LBA even if the LBAs\r
-  are fragmented.\r
-\r
-  @param Global                  Pointer to VARAIBLE_GLOBAL structure.\r
-  @param Volatile                Point out the Variable is Volatile or Non-Volatile.\r
-  @param SetByIndex              TRUE if target pointer is given as index.\r
-                                 FALSE if target pointer is absolute.\r
-  @param Fvb                     Pointer to the writable FVB protocol.\r
-  @param DataPtrIndex            Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
-                                 structure.\r
-  @param DataSize                Size of data to be written.\r
-  @param Buffer                  Pointer to the buffer from which data is written.\r
-\r
-  @retval EFI_INVALID_PARAMETER  Parameters not valid.\r
-  @retval EFI_SUCCESS            Variable store successfully updated.\r
-\r
-**/\r
-EFI_STATUS\r
-UpdateVariableStore (\r
-  IN  VARIABLE_GLOBAL                     *Global,\r
-  IN  BOOLEAN                             Volatile,\r
-  IN  BOOLEAN                             SetByIndex,\r
-  IN  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb,\r
-  IN  UINTN                               DataPtrIndex,\r
-  IN  UINT32                              DataSize,\r
-  IN  UINT8                               *Buffer\r
-  )\r
-{\r
-  EFI_FV_BLOCK_MAP_ENTRY      *PtrBlockMapEntry;\r
-  UINTN                       BlockIndex2;\r
-  UINTN                       LinearOffset;\r
-  UINTN                       CurrWriteSize;\r
-  UINTN                       CurrWritePtr;\r
-  UINT8                       *CurrBuffer;\r
-  EFI_LBA                     LbaNumber;\r
-  UINTN                       Size;\r
-  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
-  VARIABLE_STORE_HEADER       *VolatileBase;\r
-  EFI_PHYSICAL_ADDRESS        FvVolHdr;\r
-  EFI_PHYSICAL_ADDRESS        DataPtr;\r
-  EFI_STATUS                  Status;\r
-\r
-  FwVolHeader = NULL;\r
-  DataPtr     = DataPtrIndex;\r
-\r
-  //\r
-  // Check if the Data is Volatile.\r
-  //\r
-  if (!Volatile) {\r
-    if (Fvb == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
-    //\r
-    // Data Pointer should point to the actual Address where data is to be\r
-    // written.\r
-    //\r
-    if (SetByIndex) {\r
-      DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
-    }\r
-\r
-    if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-  } else {\r
-    //\r
-    // Data Pointer should point to the actual Address where data is to be\r
-    // written.\r
-    //\r
-    VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
-    if (SetByIndex) {\r
-      DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
-    }\r
-\r
-    if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
-    //\r
-    // If Volatile Variable just do a simple mem copy.\r
-    //\r
-    CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // If we are here we are dealing with Non-Volatile Variables.\r
-  //\r
-  LinearOffset  = (UINTN) FwVolHeader;\r
-  CurrWritePtr  = (UINTN) DataPtr;\r
-  CurrWriteSize = DataSize;\r
-  CurrBuffer    = Buffer;\r
-  LbaNumber     = 0;\r
-\r
-  if (CurrWritePtr < LinearOffset) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
-    for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r
-      //\r
-      // Check to see if the Variable Writes are spanning through multiple\r
-      // blocks.\r
-      //\r
-      if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
-        if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
-          Status = Fvb->Write (\r
-                    Fvb,\r
-                    LbaNumber,\r
-                    (UINTN) (CurrWritePtr - LinearOffset),\r
-                    &CurrWriteSize,\r
-                    CurrBuffer\r
-                    );\r
-          return Status;\r
-        } else {\r
-          Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
-          Status = Fvb->Write (\r
-                    Fvb,\r
-                    LbaNumber,\r
-                    (UINTN) (CurrWritePtr - LinearOffset),\r
-                    &Size,\r
-                    CurrBuffer\r
-                    );\r
-          if (EFI_ERROR (Status)) {\r
-            return Status;\r
-          }\r
-\r
-          CurrWritePtr  = LinearOffset + PtrBlockMapEntry->Length;\r
-          CurrBuffer    = CurrBuffer + Size;\r
-          CurrWriteSize = CurrWriteSize - Size;\r
-        }\r
-      }\r
-\r
-      LinearOffset += PtrBlockMapEntry->Length;\r
-      LbaNumber++;\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-\r
-  This code gets the current status of Variable Store.\r
-\r
-  @param VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @retval EfiRaw         Variable store status is raw.\r
-  @retval EfiValid       Variable store status is valid.\r
-  @retval EfiInvalid     Variable store status is invalid.\r
-\r
-**/\r
-VARIABLE_STORE_STATUS\r
-GetVariableStoreStatus (\r
-  IN VARIABLE_STORE_HEADER *VarStoreHeader\r
-  )\r
-{\r
-  if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
-      VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
-      VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
-      ) {\r
-\r
-    return EfiValid;\r
-  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
-             VarStoreHeader->Size == 0xffffffff &&\r
-             VarStoreHeader->Format == 0xff &&\r
-             VarStoreHeader->State == 0xff\r
-          ) {\r
-\r
-    return EfiRaw;\r
-  } else {\r
-    return EfiInvalid;\r
-  }\r
-}\r
-\r
-\r
-/**\r
-\r
-  This code gets the size of name of variable.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return UINTN          Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-NameSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  if (Variable->State    == (UINT8) (-1) ||\r
-      Variable->DataSize == (UINT32) (-1) ||\r
-      Variable->NameSize == (UINT32) (-1) ||\r
-      Variable->Attributes == (UINT32) (-1)) {\r
-    return 0;\r
-  }\r
-  return (UINTN) Variable->NameSize;\r
-}\r
-\r
-/**\r
-\r
-  This code gets the size of variable data.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  if (Variable->State    == (UINT8)  (-1) ||\r
-      Variable->DataSize == (UINT32) (-1) ||\r
-      Variable->NameSize == (UINT32) (-1) ||\r
-      Variable->Attributes == (UINT32) (-1)) {\r
-    return 0;\r
-  }\r
-  return (UINTN) Variable->DataSize;\r
-}\r
-\r
-/**\r
-\r
-  This code gets the pointer to the variable name.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Pointer to Variable Name which is Unicode encoding.\r
-\r
-**/\r
-CHAR16 *\r
-GetVariableNamePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-\r
-  return (CHAR16 *) (Variable + 1);\r
-}\r
-\r
-/**\r
-\r
-  This code gets the pointer to the variable data.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Pointer to Variable Data.\r
-\r
-**/\r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  UINTN Value;\r
-\r
-  //\r
-  // Be careful about pad size for alignment.\r
-  //\r
-  Value =  (UINTN) GetVariableNamePtr (Variable);\r
-  Value += NameSizeOfVariable (Variable);\r
-  Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r
-\r
-  return (UINT8 *) Value;\r
-}\r
-\r
-\r
-/**\r
-\r
-  This code gets the pointer to the next variable header.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Pointer to next variable header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-{\r
-  UINTN Value;\r
-\r
-  Value =  (UINTN) GetVariableDataPtr (Variable);\r
-  Value += DataSizeOfVariable (Variable);\r
-  Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r
-\r
-  //\r
-  // Be careful about pad size for alignment.\r
-  //\r
-  return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
-}\r
-\r
-/**\r
-\r
-  Gets the pointer to the first variable header in given variable store area.\r
-\r
-  @param VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @return Pointer to the first variable header.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetStartPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-{\r
-  //\r
-  // The end of variable store.\r
-  //\r
-  return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
-}\r
-\r
-/**\r
-\r
-  Gets the pointer to the end of the variable storage area.\r
-\r
-  This function gets pointer to the end of the variable storage\r
-  area, according to the input variable store header.\r
-\r
-  @param VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @return Pointer to the end of the variable storage area.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-{\r
-  //\r
-  // The end of variable store\r
-  //\r
-  return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
-}\r
-\r
-/**\r
-  Record variable error flag.\r
-\r
-  @param[in] Flag               Variable error flag to record.\r
-  @param[in] VariableName       Name of variable.\r
-  @param[in] VendorGuid         Guid of variable.\r
-  @param[in] Attributes         Attributes of the variable.\r
-  @param[in] VariableSize       Size of the variable.\r
-\r
-**/\r
-VOID\r
-RecordVarErrorFlag (\r
-  IN VAR_ERROR_FLAG         Flag,\r
-  IN CHAR16                 *VariableName,\r
-  IN EFI_GUID               *VendorGuid,\r
-  IN UINT32                 Attributes,\r
-  IN UINTN                  VariableSize\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  VARIABLE_POINTER_TRACK    Variable;\r
-  VAR_ERROR_FLAG            *VarErrFlag;\r
-  VAR_ERROR_FLAG            TempFlag;\r
-\r
-  DEBUG_CODE (\r
-    DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r
-    if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r
-      if (AtRuntime ()) {\r
-        DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
-      } else {\r
-        DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r
-      }\r
-    } else {\r
-      DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r
-    }\r
-  );\r
-\r
-  if (!mEndOfDxe) {\r
-    //\r
-    // Before EndOfDxe, just record the current boot variable error flag to local variable,\r
-    // and leave the variable error flag in NV flash as the last boot variable error flag.\r
-    // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r
-    // will be initialized to this local current boot variable error flag.\r
-    //\r
-    mCurrentBootVarErrFlag &= Flag;\r
-    return;\r
-  }\r
-\r
-  //\r
-  // Record error flag (it should have be initialized).\r
-  //\r
-  Status = FindVariable (\r
-             VAR_ERROR_FLAG_NAME,\r
-             &gEdkiiVarErrorFlagGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (!EFI_ERROR (Status)) {\r
-    VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r
-    TempFlag = *VarErrFlag;\r
-    TempFlag &= Flag;\r
-    if (TempFlag == *VarErrFlag) {\r
-      return;\r
-    }\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE,\r
-               FALSE,\r
-               mVariableModuleGlobal->FvbInstance,\r
-               (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
-               sizeof (TempFlag),\r
-               &TempFlag\r
-               );\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // Update the data in NV cache.\r
-      //\r
-      *VarErrFlag = Flag;\r
-    }\r
-  }\r
-}\r
-\r
-/**\r
-  Initialize variable error flag.\r
-\r
-  Before EndOfDxe, the variable indicates the last boot variable error flag,\r
-  then it means the last boot variable error flag must be got before EndOfDxe.\r
-  After EndOfDxe, the variable indicates the current boot variable error flag,\r
-  then it means the current boot variable error flag must be got after EndOfDxe.\r
-\r
-**/\r
-VOID\r
-InitializeVarErrorFlag (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  VARIABLE_POINTER_TRACK    Variable;\r
-  VAR_ERROR_FLAG            Flag;\r
-  VAR_ERROR_FLAG            VarErrFlag;\r
-\r
-  if (!mEndOfDxe) {\r
-    return;\r
-  }\r
-\r
-  Flag = mCurrentBootVarErrFlag;\r
-  DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r
-\r
-  Status = FindVariable (\r
-             VAR_ERROR_FLAG_NAME,\r
-             &gEdkiiVarErrorFlagGuid,\r
-             &Variable,\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             FALSE\r
-             );\r
-  if (!EFI_ERROR (Status)) {\r
-    VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r
-    if (VarErrFlag == Flag) {\r
-      return;\r
-    }\r
-  }\r
-\r
-  UpdateVariable (\r
-    VAR_ERROR_FLAG_NAME,\r
-    &gEdkiiVarErrorFlagGuid,\r
-    &Flag,\r
-    sizeof (Flag),\r
-    VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-    0,\r
-    0,\r
-    &Variable,\r
-    NULL\r
-    );\r
-}\r
-\r
-/**\r
-  Is user variable?\r
-\r
-  @param[in] Variable   Pointer to variable header.\r
-\r
-  @retval TRUE          User variable.\r
-  @retval FALSE         System variable.\r
-\r
-**/\r
-BOOLEAN\r
-IsUserVariable (\r
-  IN VARIABLE_HEADER    *Variable\r
-  )\r
-{\r
-  VAR_CHECK_VARIABLE_PROPERTY   Property;\r
-\r
-  //\r
-  // Only after End Of Dxe, the variables belong to system variable are fixed.\r
-  // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
-  // then no need to check if the variable is user variable or not specially.\r
-  //\r
-  if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
-    if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
-      return TRUE;\r
-    }\r
-  }\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-  Calculate common user variable total size.\r
-\r
-**/\r
-VOID\r
-CalculateCommonUserVariableTotalSize (\r
-  VOID\r
-  )\r
-{\r
-  VARIABLE_HEADER               *Variable;\r
-  VARIABLE_HEADER               *NextVariable;\r
-  UINTN                         VariableSize;\r
-  VAR_CHECK_VARIABLE_PROPERTY   Property;\r
-\r
-  //\r
-  // Only after End Of Dxe, the variables belong to system variable are fixed.\r
-  // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r
-  // then no need to calculate the common user variable total size specially.\r
-  //\r
-  if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r
-    Variable = GetStartPointer (mNvVariableCache);\r
-    while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r
-      NextVariable = GetNextVariablePtr (Variable);\r
-      VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-      if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-        if (InternalVarCheckVariablePropertyGet (GetVariableNamePtr (Variable), &Variable->VendorGuid, &Property) == EFI_NOT_FOUND) {\r
-          //\r
-          // No property, it is user variable.\r
-          //\r
-          mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
-        }\r
-      }\r
-\r
-      Variable = NextVariable;\r
-    }\r
-  }\r
-}\r
-\r
-/**\r
-  Initialize variable quota.\r
-\r
-**/\r
-VOID\r
-InitializeVariableQuota (\r
-  VOID\r
-  )\r
-{\r
-  STATIC BOOLEAN    Initialized;\r
-\r
-  if (!mEndOfDxe || Initialized) {\r
-    return;\r
-  }\r
-  Initialized = TRUE;\r
-\r
-  InitializeVarErrorFlag ();\r
-  CalculateCommonUserVariableTotalSize ();\r
-}\r
-\r
-/**\r
-\r
-  Check the PubKeyIndex is a valid key or not.\r
-\r
-  This function will iterate the NV storage to see if this PubKeyIndex is still referenced\r
-  by any valid count-based auth variabe.\r
-\r
-  @param[in]  PubKeyIndex     Index of the public key in public key store.\r
-\r
-  @retval     TRUE            The PubKeyIndex is still in use.\r
-  @retval     FALSE           The PubKeyIndex is not referenced by any count-based auth variabe.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidPubKeyIndex (\r
-  IN   UINT32      PubKeyIndex\r
-  )\r
-{\r
-  VARIABLE_HEADER          *Variable;\r
-  VARIABLE_HEADER          *VariableStoreEnd;\r
-\r
-  if (PubKeyIndex > mPubKeyNumber) {\r
-    return FALSE;\r
-  }\r
-\r
-  Variable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-  VariableStoreEnd = GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-\r
-  while (IsValidVariableHeader (Variable, VariableStoreEnd)) {\r
-    if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
-        Variable->PubKeyIndex == PubKeyIndex) {\r
-      return TRUE;\r
-    }\r
-    Variable = GetNextVariablePtr (Variable);\r
-  }\r
-\r
-  return FALSE;\r
-}\r
-\r
-/**\r
-\r
-  Get the number of valid public key in PubKeyStore.\r
-\r
-  @param[in]  PubKeyNumber     Number of the public key in public key store.\r
-\r
-  @return     Number of valid public key in PubKeyStore.\r
-\r
-**/\r
-UINT32\r
-GetValidPubKeyNumber (\r
-  IN   UINT32       PubKeyNumber\r
-  )\r
-{\r
-  UINT32       PubKeyIndex;\r
-  UINT32       Counter;\r
-\r
-  Counter = 0;\r
-\r
-  for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) {\r
-    if (IsValidPubKeyIndex (PubKeyIndex)) {\r
-      Counter++;\r
-    }\r
-  }\r
-\r
-  return Counter;\r
-}\r
-\r
-/**\r
-\r
-  Filter the useless key in public key store.\r
-\r
-  This function will find out all valid public keys in public key database, save them in new allocated\r
-  buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer\r
-  NewPubKeyIndex and NewPubKeyStore with FreePool().\r
-\r
-  @param[in]   PubKeyStore          Point to the public key database.\r
-  @param[in]   PubKeyNumber         Number of the public key in PubKeyStore.\r
-  @param[out]  NewPubKeyIndex       Point to an array of new PubKeyIndex corresponds to NewPubKeyStore.\r
-  @param[out]  NewPubKeyStore       Saved all valid public keys in PubKeyStore.\r
-  @param[out]  NewPubKeySize        Buffer size of the NewPubKeyStore.\r
-\r
-  @retval  EFI_SUCCESS              Trim operation is complete successfully.\r
-  @retval  EFI_OUT_OF_RESOURCES     No enough memory resources, or no useless key in PubKeyStore.\r
-\r
-**/\r
-EFI_STATUS\r
-PubKeyStoreFilter (\r
-  IN   UINT8         *PubKeyStore,\r
-  IN   UINT32        PubKeyNumber,\r
-  OUT  UINT32        **NewPubKeyIndex,\r
-  OUT  UINT8         **NewPubKeyStore,\r
-  OUT  UINT32        *NewPubKeySize\r
-  )\r
-{\r
-  UINT32        PubKeyIndex;\r
-  UINT32        CopiedKey;\r
-  UINT32        NewPubKeyNumber;\r
-\r
-  NewPubKeyNumber = GetValidPubKeyNumber (PubKeyNumber);\r
-  if (NewPubKeyNumber == PubKeyNumber) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  if (NewPubKeyNumber != 0) {\r
-    *NewPubKeySize = NewPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE;\r
-  } else {\r
-    *NewPubKeySize = sizeof (UINT8);\r
-  }\r
-\r
-  *NewPubKeyStore = AllocatePool (*NewPubKeySize);\r
-  if (*NewPubKeyStore == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  *NewPubKeyIndex = AllocateZeroPool ((PubKeyNumber + 1) * sizeof (UINT32));\r
-  if (*NewPubKeyIndex == NULL) {\r
-    FreePool (*NewPubKeyStore);\r
-    *NewPubKeyStore = NULL;\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  CopiedKey = 0;\r
-  for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) {\r
-    if (IsValidPubKeyIndex (PubKeyIndex)) {\r
-      CopyMem (\r
-        *NewPubKeyStore + CopiedKey * EFI_CERT_TYPE_RSA2048_SIZE,\r
-        PubKeyStore + (PubKeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE,\r
-        EFI_CERT_TYPE_RSA2048_SIZE\r
-        );\r
-      (*NewPubKeyIndex)[PubKeyIndex] = ++CopiedKey;\r
-    }\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-\r
-  Variable store garbage collection and reclaim operation.\r
-\r
-  If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.\r
-  If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex\r
-  for all the count-based authenticate variable in NV storage.\r
-\r
-  @param[in]      VariableBase            Base address of variable store.\r
-  @param[out]     LastVariableOffset      Offset of last variable.\r
-  @param[in]      IsVolatile              The variable store is volatile or not;\r
-                                          if it is non-volatile, need FTW.\r
-  @param[in, out] UpdatingPtrTrack        Pointer to updating variable pointer track structure.\r
-  @param[in]      NewVariable             Pointer to new variable.\r
-  @param[in]      NewVariableSize         New variable size.\r
-  @param[in]      ReclaimPubKeyStore      Reclaim for public key database or not.\r
-\r
-  @return EFI_SUCCESS                  Reclaim operation has finished successfully.\r
-  @return EFI_OUT_OF_RESOURCES         No enough memory resources or variable space.\r
-  @return EFI_DEVICE_ERROR             The public key database doesn't exist.\r
-  @return Others                       Unexpect error happened during reclaim operation.\r
-\r
-**/\r
-EFI_STATUS\r
-Reclaim (\r
-  IN     EFI_PHYSICAL_ADDRESS         VariableBase,\r
-  OUT    UINTN                        *LastVariableOffset,\r
-  IN     BOOLEAN                      IsVolatile,\r
-  IN OUT VARIABLE_POINTER_TRACK       *UpdatingPtrTrack,\r
-  IN     VARIABLE_HEADER              *NewVariable,\r
-  IN     UINTN                        NewVariableSize,\r
-  IN     BOOLEAN                      ReclaimPubKeyStore\r
-  )\r
-{\r
-  VARIABLE_HEADER       *Variable;\r
-  VARIABLE_HEADER       *AddedVariable;\r
-  VARIABLE_HEADER       *NextVariable;\r
-  VARIABLE_HEADER       *NextAddedVariable;\r
-  VARIABLE_STORE_HEADER *VariableStoreHeader;\r
-  UINT8                 *ValidBuffer;\r
-  UINTN                 MaximumBufferSize;\r
-  UINTN                 VariableSize;\r
-  UINTN                 NameSize;\r
-  UINT8                 *CurrPtr;\r
-  VOID                  *Point0;\r
-  VOID                  *Point1;\r
-  BOOLEAN               FoundAdded;\r
-  EFI_STATUS            Status;\r
-  UINTN                 CommonVariableTotalSize;\r
-  UINTN                 CommonUserVariableTotalSize;\r
-  UINTN                 HwErrVariableTotalSize;\r
-  UINT32                *NewPubKeyIndex;\r
-  UINT8                 *NewPubKeyStore;\r
-  UINT32                NewPubKeySize;\r
-  VARIABLE_HEADER       *PubKeyHeader;\r
-  VARIABLE_HEADER       *UpdatingVariable;\r
-  VARIABLE_HEADER       *UpdatingInDeletedTransition;\r
-\r
-  UpdatingVariable = NULL;\r
-  UpdatingInDeletedTransition = NULL;\r
-  if (UpdatingPtrTrack != NULL) {\r
-    UpdatingVariable = UpdatingPtrTrack->CurrPtr;\r
-    UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;\r
-  }\r
-\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
-\r
-  CommonVariableTotalSize = 0;\r
-  CommonUserVariableTotalSize = 0;\r
-  HwErrVariableTotalSize  = 0;\r
-  NewPubKeyIndex = NULL;\r
-  NewPubKeyStore = NULL;\r
-  NewPubKeySize  = 0;\r
-  PubKeyHeader   = NULL;\r
-\r
-  if (IsVolatile) {\r
-    //\r
-    // Start Pointers for the variable.\r
-    //\r
-    Variable          = GetStartPointer (VariableStoreHeader);\r
-    MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
-\r
-    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
-      NextVariable = GetNextVariablePtr (Variable);\r
-      if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r
-          Variable != UpdatingVariable &&\r
-          Variable != UpdatingInDeletedTransition\r
-         ) {\r
-        VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-        MaximumBufferSize += VariableSize;\r
-      }\r
-\r
-      Variable = NextVariable;\r
-    }\r
-\r
-    if (NewVariable != NULL) {\r
-      //\r
-      // Add the new variable size.\r
-      //\r
-      MaximumBufferSize += NewVariableSize;\r
-    }\r
-\r
-    //\r
-    // Reserve the 1 Bytes with Oxff to identify the\r
-    // end of the variable buffer.\r
-    //\r
-    MaximumBufferSize += 1;\r
-    ValidBuffer = AllocatePool (MaximumBufferSize);\r
-    if (ValidBuffer == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-  } else {\r
-    //\r
-    // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache\r
-    // as the buffer to reduce SMRAM consumption for SMM variable driver.\r
-    //\r
-    MaximumBufferSize = mNvVariableCache->Size;\r
-    ValidBuffer = (UINT8 *) mNvVariableCache;\r
-  }\r
-\r
-  SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
-\r
-  //\r
-  // Copy variable store header.\r
-  //\r
-  CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
-  CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
-\r
-  if (ReclaimPubKeyStore) {\r
-    ASSERT (IsVolatile == FALSE);\r
-    //\r
-    // Trim the PubKeyStore and get new PubKeyIndex.\r
-    //\r
-    Status = PubKeyStoreFilter (\r
-               mPubKeyStore,\r
-               mPubKeyNumber,\r
-               &NewPubKeyIndex,\r
-               &NewPubKeyStore,\r
-               &NewPubKeySize\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-    ASSERT ((NewPubKeyIndex != NULL) && (NewPubKeyStore != NULL));\r
-\r
-    //\r
-    // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION).\r
-    //\r
-    Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
-      NextVariable = GetNextVariablePtr (Variable);\r
-      if (Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-        if ((StrCmp (GetVariableNamePtr (Variable), AUTHVAR_KEYDB_NAME) == 0) &&\r
-            (CompareGuid (&Variable->VendorGuid, &gEfiAuthenticatedVariableGuid))) {\r
-          //\r
-          // Skip the public key database, it will be reinstalled later.\r
-          //\r
-          PubKeyHeader = Variable;\r
-          Variable = NextVariable;\r
-          continue;\r
-        }\r
-\r
-        VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-        CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
-        ((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex];\r
-        CurrPtr += VariableSize;\r
-        if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          HwErrVariableTotalSize += VariableSize;\r
-        } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          CommonVariableTotalSize += VariableSize;\r
-          if (IsUserVariable (Variable)) {\r
-            CommonUserVariableTotalSize += VariableSize;\r
-          }\r
-        }\r
-      }\r
-      Variable = NextVariable;\r
-    }\r
-\r
-    //\r
-    // Reinstall the new public key database.\r
-    //\r
-    ASSERT (PubKeyHeader != NULL);\r
-    if (PubKeyHeader == NULL) {\r
-      Status = EFI_DEVICE_ERROR;\r
-      goto Done;\r
-    }\r
-    CopyMem (CurrPtr, (UINT8*) PubKeyHeader, sizeof (VARIABLE_HEADER));\r
-    Variable = (VARIABLE_HEADER*) CurrPtr;\r
-    Variable->DataSize = NewPubKeySize;\r
-    StrCpy (GetVariableNamePtr (Variable), GetVariableNamePtr (PubKeyHeader));\r
-    CopyMem (GetVariableDataPtr (Variable), NewPubKeyStore, NewPubKeySize);\r
-    CurrPtr = (UINT8*) GetNextVariablePtr (Variable);\r
-    CommonVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable;\r
-    if (IsUserVariable (Variable)) {\r
-      CommonUserVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable;\r
-    }\r
-  } else {\r
-    //\r
-    // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r
-    //\r
-    Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
-      NextVariable = GetNextVariablePtr (Variable);\r
-      if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r
-        VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-        CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
-        CurrPtr += VariableSize;\r
-        if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-          HwErrVariableTotalSize += VariableSize;\r
-        } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-          CommonVariableTotalSize += VariableSize;\r
-          if (IsUserVariable (Variable)) {\r
-            CommonUserVariableTotalSize += VariableSize;\r
-          }\r
-        }\r
-      }\r
-      Variable = NextVariable;\r
-    }\r
-\r
-    //\r
-    // Reinstall all in delete transition variables.\r
-    //\r
-    Variable = GetStartPointer (VariableStoreHeader);\r
-    while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
-      NextVariable = GetNextVariablePtr (Variable);\r
-      if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-\r
-        //\r
-        // Buffer has cached all ADDED variable.\r
-        // Per IN_DELETED variable, we have to guarantee that\r
-        // no ADDED one in previous buffer.\r
-        //\r
-\r
-        FoundAdded = FALSE;\r
-        AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
-        while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r
-          NextAddedVariable = GetNextVariablePtr (AddedVariable);\r
-          NameSize = NameSizeOfVariable (AddedVariable);\r
-          if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r
-              NameSize == NameSizeOfVariable (Variable)\r
-             ) {\r
-            Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r
-            Point1 = (VOID *) GetVariableNamePtr (Variable);\r
-            if (CompareMem (Point0, Point1, NameSize) == 0) {\r
-              FoundAdded = TRUE;\r
-              break;\r
-            }\r
-          }\r
-          AddedVariable = NextAddedVariable;\r
-        }\r
-        if (!FoundAdded) {\r
-          //\r
-          // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r
-          //\r
-          VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-          CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
-          ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
-          CurrPtr += VariableSize;\r
-          if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-            HwErrVariableTotalSize += VariableSize;\r
-          } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-            CommonVariableTotalSize += VariableSize;\r
-            if (IsUserVariable (Variable)) {\r
-              CommonUserVariableTotalSize += VariableSize;\r
-            }\r
-          }\r
-        }\r
-      }\r
-\r
-      Variable = NextVariable;\r
-    }\r
-\r
-    //\r
-    // Install the new variable if it is not NULL.\r
-    //\r
-    if (NewVariable != NULL) {\r
-      if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r
-        //\r
-        // No enough space to store the new variable.\r
-        //\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-        goto Done;\r
-      }\r
-      if (!IsVolatile) {\r
-        if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          HwErrVariableTotalSize += NewVariableSize;\r
-        } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          CommonVariableTotalSize += NewVariableSize;\r
-          if (IsUserVariable (NewVariable)) {\r
-            CommonUserVariableTotalSize += NewVariableSize;\r
-          }\r
-        }\r
-        if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r
-            (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||\r
-            (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
-          //\r
-          // No enough space to store the new variable by NV or NV+HR attribute.\r
-          //\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Done;\r
-        }\r
-      }\r
-\r
-      CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);\r
-      ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
-      if (UpdatingVariable != NULL) {\r
-        UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));\r
-        UpdatingPtrTrack->InDeletedTransitionPtr = NULL;\r
-      }\r
-      CurrPtr += NewVariableSize;\r
-    }\r
-  }\r
-\r
-  if (IsVolatile) {\r
-    //\r
-    // If volatile variable store, just copy valid buffer.\r
-    //\r
-    SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r
-    CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));\r
-    *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
-    Status  = EFI_SUCCESS;\r
-  } else {\r
-    //\r
-    // If non-volatile variable store, perform FTW here.\r
-    //\r
-    Status = FtwVariableSpace (\r
-              VariableBase,\r
-              (VARIABLE_STORE_HEADER *) ValidBuffer\r
-              );\r
-    if (!EFI_ERROR (Status)) {\r
-      *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
-      mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
-      mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
-      mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
-    } else {\r
-      Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
-      while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
-        NextVariable = GetNextVariablePtr (Variable);\r
-        VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-        if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
-        } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
-          if (IsUserVariable (Variable)) {\r
-            mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
-          }\r
-        }\r
-\r
-        Variable = NextVariable;\r
-      }\r
-      *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
-    }\r
-  }\r
-\r
-Done:\r
-  if (IsVolatile) {\r
-    FreePool (ValidBuffer);\r
-  } else {\r
-    //\r
-    // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r
-    //\r
-    CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r
-\r
-    if (NewPubKeyStore != NULL) {\r
-      FreePool (NewPubKeyStore);\r
-    }\r
-\r
-    if (NewPubKeyIndex != NULL) {\r
-      FreePool (NewPubKeyIndex);\r
-    }\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Find the variable in the specified variable store.\r
-\r
-  @param[in]       VariableName        Name of the variable to be found\r
-  @param[in]       VendorGuid          Vendor GUID to be found.\r
-  @param[in]       IgnoreRtCheck       Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
-                                       check at runtime when searching variable.\r
-  @param[in, out]  PtrTrack            Variable Track Pointer structure that contains Variable Information.\r
-\r
-  @retval          EFI_SUCCESS         Variable found successfully\r
-  @retval          EFI_NOT_FOUND       Variable not found\r
-**/\r
-EFI_STATUS\r
-FindVariableEx (\r
-  IN     CHAR16                  *VariableName,\r
-  IN     EFI_GUID                *VendorGuid,\r
-  IN     BOOLEAN                 IgnoreRtCheck,\r
-  IN OUT VARIABLE_POINTER_TRACK  *PtrTrack\r
-  )\r
-{\r
-  VARIABLE_HEADER                *InDeletedVariable;\r
-  VOID                           *Point;\r
-\r
-  PtrTrack->InDeletedTransitionPtr = NULL;\r
-\r
-  //\r
-  // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
-  //\r
-  InDeletedVariable  = NULL;\r
-\r
-  for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r
-      ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r
-      ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r
-      ) {\r
-    if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r
-        PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
-       ) {\r
-      if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
-        if (VariableName[0] == 0) {\r
-          if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-            InDeletedVariable   = PtrTrack->CurrPtr;\r
-          } else {\r
-            PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
-            return EFI_SUCCESS;\r
-          }\r
-        } else {\r
-          if (CompareGuid (VendorGuid, &PtrTrack->CurrPtr->VendorGuid)) {\r
-            Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r
-\r
-            ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r
-            if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r
-              if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-                InDeletedVariable     = PtrTrack->CurrPtr;\r
-              } else {\r
-                PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r
-                return EFI_SUCCESS;\r
-              }\r
-            }\r
-          }\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  PtrTrack->CurrPtr = InDeletedVariable;\r
-  return (PtrTrack->CurrPtr  == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-  Finds variable in storage blocks of volatile and non-volatile storage areas.\r
-\r
-  This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
-  If VariableName is an empty string, then we just return the first\r
-  qualified variable without comparing VariableName and VendorGuid.\r
-  If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
-  at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
-  Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
-\r
-  @param[in]   VariableName           Name of the variable to be found.\r
-  @param[in]   VendorGuid             Vendor GUID to be found.\r
-  @param[out]  PtrTrack               VARIABLE_POINTER_TRACK structure for output,\r
-                                      including the range searched and the target position.\r
-  @param[in]   Global                 Pointer to VARIABLE_GLOBAL structure, including\r
-                                      base of volatile variable storage area, base of\r
-                                      NV variable storage area, and a lock.\r
-  @param[in]   IgnoreRtCheck          Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
-                                      check at runtime when searching variable.\r
-\r
-  @retval EFI_INVALID_PARAMETER       If VariableName is not an empty string, while\r
-                                      VendorGuid is NULL.\r
-  @retval EFI_SUCCESS                 Variable successfully found.\r
-  @retval EFI_NOT_FOUND               Variable not found\r
-\r
-**/\r
-EFI_STATUS\r
-FindVariable (\r
-  IN  CHAR16                  *VariableName,\r
-  IN  EFI_GUID                *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack,\r
-  IN  VARIABLE_GLOBAL         *Global,\r
-  IN  BOOLEAN                 IgnoreRtCheck\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];\r
-  VARIABLE_STORE_TYPE     Type;\r
-\r
-  if (VariableName[0] != 0 && VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
-  // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
-  // make use of this mapping to implement search algorithm.\r
-  //\r
-  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r
-  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r
-  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;\r
-\r
-  //\r
-  // Find the variable by walk through HOB, volatile and non-volatile variable store.\r
-  //\r
-  for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
-    if (VariableStoreHeader[Type] == NULL) {\r
-      continue;\r
-    }\r
-\r
-    PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
-    PtrTrack->EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);\r
-    PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r
-\r
-    Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r
-    if (!EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-/**\r
-  Get index from supported language codes according to language string.\r
-\r
-  This code is used to get corresponding index in supported language codes. It can handle\r
-  RFC4646 and ISO639 language tags.\r
-  In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
-  In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
-\r
-  For example:\r
-    SupportedLang  = "engfraengfra"\r
-    Lang           = "eng"\r
-    Iso639Language = TRUE\r
-  The return value is "0".\r
-  Another example:\r
-    SupportedLang  = "en;fr;en-US;fr-FR"\r
-    Lang           = "fr-FR"\r
-    Iso639Language = FALSE\r
-  The return value is "3".\r
-\r
-  @param  SupportedLang               Platform supported language codes.\r
-  @param  Lang                        Configured language.\r
-  @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
-\r
-  @retval The index of language in the language codes.\r
-\r
-**/\r
-UINTN\r
-GetIndexFromSupportedLangCodes(\r
-  IN  CHAR8            *SupportedLang,\r
-  IN  CHAR8            *Lang,\r
-  IN  BOOLEAN          Iso639Language\r
-  )\r
-{\r
-  UINTN    Index;\r
-  UINTN    CompareLength;\r
-  UINTN    LanguageLength;\r
-\r
-  if (Iso639Language) {\r
-    CompareLength = ISO_639_2_ENTRY_SIZE;\r
-    for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
-      if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
-        //\r
-        // Successfully find the index of Lang string in SupportedLang string.\r
-        //\r
-        Index = Index / CompareLength;\r
-        return Index;\r
-      }\r
-    }\r
-    ASSERT (FALSE);\r
-    return 0;\r
-  } else {\r
-    //\r
-    // Compare RFC4646 language code\r
-    //\r
-    Index = 0;\r
-    for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
-\r
-    for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
-      //\r
-      // Skip ';' characters in SupportedLang\r
-      //\r
-      for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
-      //\r
-      // Determine the length of the next language code in SupportedLang\r
-      //\r
-      for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
-\r
-      if ((CompareLength == LanguageLength) &&\r
-          (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
-        //\r
-        // Successfully find the index of Lang string in SupportedLang string.\r
-        //\r
-        return Index;\r
-      }\r
-    }\r
-    ASSERT (FALSE);\r
-    return 0;\r
-  }\r
-}\r
-\r
-/**\r
-  Get language string from supported language codes according to index.\r
-\r
-  This code is used to get corresponding language strings in supported language codes. It can handle\r
-  RFC4646 and ISO639 language tags.\r
-  In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
-  In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
-\r
-  For example:\r
-    SupportedLang  = "engfraengfra"\r
-    Index          = "1"\r
-    Iso639Language = TRUE\r
-  The return value is "fra".\r
-  Another example:\r
-    SupportedLang  = "en;fr;en-US;fr-FR"\r
-    Index          = "1"\r
-    Iso639Language = FALSE\r
-  The return value is "fr".\r
-\r
-  @param  SupportedLang               Platform supported language codes.\r
-  @param  Index                       The index in supported language codes.\r
-  @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
-\r
-  @retval The language string in the language codes.\r
-\r
-**/\r
-CHAR8 *\r
-GetLangFromSupportedLangCodes (\r
-  IN  CHAR8            *SupportedLang,\r
-  IN  UINTN            Index,\r
-  IN  BOOLEAN          Iso639Language\r
-)\r
-{\r
-  UINTN    SubIndex;\r
-  UINTN    CompareLength;\r
-  CHAR8    *Supported;\r
-\r
-  SubIndex  = 0;\r
-  Supported = SupportedLang;\r
-  if (Iso639Language) {\r
-    //\r
-    // According to the index of Lang string in SupportedLang string to get the language.\r
-    // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r
-    // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
-    //\r
-    CompareLength = ISO_639_2_ENTRY_SIZE;\r
-    mVariableModuleGlobal->Lang[CompareLength] = '\0';\r
-    return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
-\r
-  } else {\r
-    while (TRUE) {\r
-      //\r
-      // Take semicolon as delimitation, sequentially traverse supported language codes.\r
-      //\r
-      for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
-        Supported++;\r
-      }\r
-      if ((*Supported == '\0') && (SubIndex != Index)) {\r
-        //\r
-        // Have completed the traverse, but not find corrsponding string.\r
-        // This case is not allowed to happen.\r
-        //\r
-        ASSERT(FALSE);\r
-        return NULL;\r
-      }\r
-      if (SubIndex == Index) {\r
-        //\r
-        // According to the index of Lang string in SupportedLang string to get the language.\r
-        // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
-        // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
-        //\r
-        mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r
-        return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
-      }\r
-      SubIndex++;\r
-\r
-      //\r
-      // Skip ';' characters in Supported\r
-      //\r
-      for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
-    }\r
-  }\r
-}\r
-\r
-/**\r
-  Returns a pointer to an allocated buffer that contains the best matching language\r
-  from a set of supported languages.\r
-\r
-  This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
-  code types may not be mixed in a single call to this function. This function\r
-  supports a variable argument list that allows the caller to pass in a prioritized\r
-  list of language codes to test against all the language codes in SupportedLanguages.\r
-\r
-  If SupportedLanguages is NULL, then ASSERT().\r
-\r
-  @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string that\r
-                                  contains a set of language codes in the format\r
-                                  specified by Iso639Language.\r
-  @param[in]  Iso639Language      If TRUE, then all language codes are assumed to be\r
-                                  in ISO 639-2 format.  If FALSE, then all language\r
-                                  codes are assumed to be in RFC 4646 language format\r
-  @param[in]  ...                 A variable argument list that contains pointers to\r
-                                  Null-terminated ASCII strings that contain one or more\r
-                                  language codes in the format specified by Iso639Language.\r
-                                  The first language code from each of these language\r
-                                  code lists is used to determine if it is an exact or\r
-                                  close match to any of the language codes in\r
-                                  SupportedLanguages.  Close matches only apply to RFC 4646\r
-                                  language codes, and the matching algorithm from RFC 4647\r
-                                  is used to determine if a close match is present.  If\r
-                                  an exact or close match is found, then the matching\r
-                                  language code from SupportedLanguages is returned.  If\r
-                                  no matches are found, then the next variable argument\r
-                                  parameter is evaluated.  The variable argument list\r
-                                  is terminated by a NULL.\r
-\r
-  @retval NULL   The best matching language could not be found in SupportedLanguages.\r
-  @retval NULL   There are not enough resources available to return the best matching\r
-                 language.\r
-  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching\r
-                 language in SupportedLanguages.\r
-\r
-**/\r
-CHAR8 *\r
-EFIAPI\r
-VariableGetBestLanguage (\r
-  IN CONST CHAR8  *SupportedLanguages,\r
-  IN BOOLEAN      Iso639Language,\r
-  ...\r
-  )\r
-{\r
-  VA_LIST      Args;\r
-  CHAR8        *Language;\r
-  UINTN        CompareLength;\r
-  UINTN        LanguageLength;\r
-  CONST CHAR8  *Supported;\r
-  CHAR8        *Buffer;\r
-\r
-  if (SupportedLanguages == NULL) {\r
-    return NULL;\r
-  }\r
-\r
-  VA_START (Args, Iso639Language);\r
-  while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
-    //\r
-    // Default to ISO 639-2 mode\r
-    //\r
-    CompareLength  = 3;\r
-    LanguageLength = MIN (3, AsciiStrLen (Language));\r
-\r
-    //\r
-    // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
-    //\r
-    if (!Iso639Language) {\r
-      for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
-    }\r
-\r
-    //\r
-    // Trim back the length of Language used until it is empty\r
-    //\r
-    while (LanguageLength > 0) {\r
-      //\r
-      // Loop through all language codes in SupportedLanguages\r
-      //\r
-      for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
-        //\r
-        // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
-        //\r
-        if (!Iso639Language) {\r
-          //\r
-          // Skip ';' characters in Supported\r
-          //\r
-          for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
-          //\r
-          // Determine the length of the next language code in Supported\r
-          //\r
-          for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
-          //\r
-          // If Language is longer than the Supported, then skip to the next language\r
-          //\r
-          if (LanguageLength > CompareLength) {\r
-            continue;\r
-          }\r
-        }\r
-        //\r
-        // See if the first LanguageLength characters in Supported match Language\r
-        //\r
-        if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
-          VA_END (Args);\r
-\r
-          Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r
-          Buffer[CompareLength] = '\0';\r
-          return CopyMem (Buffer, Supported, CompareLength);\r
-        }\r
-      }\r
-\r
-      if (Iso639Language) {\r
-        //\r
-        // If ISO 639 mode, then each language can only be tested once\r
-        //\r
-        LanguageLength = 0;\r
-      } else {\r
-        //\r
-        // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
-        //\r
-        for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
-      }\r
-    }\r
-  }\r
-  VA_END (Args);\r
-\r
-  //\r
-  // No matches were found\r
-  //\r
-  return NULL;\r
-}\r
-\r
-/**\r
-  This function is to check if the remaining variable space is enough to set\r
-  all Variables from argument list successfully. The purpose of the check\r
-  is to keep the consistency of the Variables to be in variable storage.\r
-\r
-  Note: Variables are assumed to be in same storage.\r
-  The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
-  so follow the argument sequence to check the Variables.\r
-\r
-  @param[in] Attributes         Variable attributes for Variable entries.\r
-  @param ...                    The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
-                                A NULL terminates the list. The VariableSize of\r
-                                VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
-                                It will be changed to variable total size as output.\r
-\r
-  @retval TRUE                  Have enough variable space to set the Variables successfully.\r
-  @retval FALSE                 No enough variable space to set the Variables successfully.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-CheckRemainingSpaceForConsistency (\r
-  IN UINT32                     Attributes,\r
-  ...\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  VA_LIST                       Args;\r
-  VARIABLE_ENTRY_CONSISTENCY    *VariableEntry;\r
-  UINT64                        MaximumVariableStorageSize;\r
-  UINT64                        RemainingVariableStorageSize;\r
-  UINT64                        MaximumVariableSize;\r
-  UINTN                         TotalNeededSize;\r
-  UINTN                         OriginalVarSize;\r
-  VARIABLE_STORE_HEADER         *VariableStoreHeader;\r
-  VARIABLE_POINTER_TRACK        VariablePtrTrack;\r
-  VARIABLE_HEADER               *NextVariable;\r
-  UINTN                         VarNameSize;\r
-  UINTN                         VarDataSize;\r
-\r
-  //\r
-  // Non-Volatile related.\r
-  //\r
-  VariableStoreHeader = mNvVariableCache;\r
-\r
-  Status = VariableServiceQueryVariableInfoInternal (\r
-             Attributes,\r
-             &MaximumVariableStorageSize,\r
-             &RemainingVariableStorageSize,\r
-             &MaximumVariableSize\r
-             );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  TotalNeededSize = 0;\r
-  VA_START (Args, Attributes);\r
-  VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
-  while (VariableEntry != NULL) {\r
-    //\r
-    // Calculate variable total size.\r
-    //\r
-    VarNameSize  = StrSize (VariableEntry->Name);\r
-    VarNameSize += GET_PAD_SIZE (VarNameSize);\r
-    VarDataSize  = VariableEntry->VariableSize;\r
-    VarDataSize += GET_PAD_SIZE (VarDataSize);\r
-    VariableEntry->VariableSize = HEADER_ALIGN (sizeof (VARIABLE_HEADER) + VarNameSize + VarDataSize);\r
-\r
-    TotalNeededSize += VariableEntry->VariableSize;\r
-    VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
-  }\r
-  VA_END (Args);\r
-\r
-  if (RemainingVariableStorageSize >= TotalNeededSize) {\r
-    //\r
-    // Already have enough space.\r
-    //\r
-    return TRUE;\r
-  } else if (AtRuntime ()) {\r
-    //\r
-    // At runtime, no reclaim.\r
-    // The original variable space of Variables can't be reused.\r
-    //\r
-    return FALSE;\r
-  }\r
-\r
-  VA_START (Args, Attributes);\r
-  VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
-  while (VariableEntry != NULL) {\r
-    //\r
-    // Check if Variable[Index] has been present and get its size.\r
-    //\r
-    OriginalVarSize = 0;\r
-    VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
-    VariablePtrTrack.EndPtr   = GetEndPointer   (VariableStoreHeader);\r
-    Status = FindVariableEx (\r
-               VariableEntry->Name,\r
-               VariableEntry->Guid,\r
-               FALSE,\r
-               &VariablePtrTrack\r
-               );\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // Get size of Variable[Index].\r
-      //\r
-      NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
-      OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
-      //\r
-      // Add the original size of Variable[Index] to remaining variable storage size.\r
-      //\r
-      RemainingVariableStorageSize += OriginalVarSize;\r
-    }\r
-    if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
-      //\r
-      // No enough space for Variable[Index].\r
-      //\r
-      VA_END (Args);\r
-      return FALSE;\r
-    }\r
-    //\r
-    // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
-    //\r
-    RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
-    VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
-  }\r
-  VA_END (Args);\r
-\r
-  return TRUE;\r
-}\r
-\r
-/**\r
-  Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
-\r
-  When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
-\r
-  According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
-  and are read-only. Therefore, in variable driver, only store the original value for other use.\r
-\r
-  @param[in] VariableName       Name of variable.\r
-\r
-  @param[in] Data               Variable data.\r
-\r
-  @param[in] DataSize           Size of data. 0 means delete.\r
-\r
-  @retval EFI_SUCCESS           The update operation is successful or ignored.\r
-  @retval EFI_WRITE_PROTECTED   Update PlatformLangCodes/LangCodes at runtime.\r
-  @retval EFI_OUT_OF_RESOURCES  No enough variable space to do the update operation.\r
-  @retval Others                Other errors happened during the update operation.\r
-\r
-**/\r
-EFI_STATUS\r
-AutoUpdateLangVariable (\r
-  IN  CHAR16             *VariableName,\r
-  IN  VOID               *Data,\r
-  IN  UINTN              DataSize\r
-  )\r
-{\r
-  EFI_STATUS             Status;\r
-  CHAR8                  *BestPlatformLang;\r
-  CHAR8                  *BestLang;\r
-  UINTN                  Index;\r
-  UINT32                 Attributes;\r
-  VARIABLE_POINTER_TRACK Variable;\r
-  BOOLEAN                SetLanguageCodes;\r
-  VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r
-\r
-  //\r
-  // Don't do updates for delete operation\r
-  //\r
-  if (DataSize == 0) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  SetLanguageCodes = FALSE;\r
-\r
-  if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r
-    //\r
-    // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
-    //\r
-    if (AtRuntime ()) {\r
-      return EFI_WRITE_PROTECTED;\r
-    }\r
-\r
-    SetLanguageCodes = TRUE;\r
-\r
-    //\r
-    // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
-    // Therefore, in variable driver, only store the original value for other use.\r
-    //\r
-    if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r
-      FreePool (mVariableModuleGlobal->PlatformLangCodes);\r
-    }\r
-    mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
-    ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r
-\r
-    //\r
-    // PlatformLang holds a single language from PlatformLangCodes,\r
-    // so the size of PlatformLangCodes is enough for the PlatformLang.\r
-    //\r
-    if (mVariableModuleGlobal->PlatformLang != NULL) {\r
-      FreePool (mVariableModuleGlobal->PlatformLang);\r
-    }\r
-    mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
-    ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r
-\r
-  } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r
-    //\r
-    // LangCodes is a volatile variable, so it can not be updated at runtime.\r
-    //\r
-    if (AtRuntime ()) {\r
-      return EFI_WRITE_PROTECTED;\r
-    }\r
-\r
-    SetLanguageCodes = TRUE;\r
-\r
-    //\r
-    // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
-    // Therefore, in variable driver, only store the original value for other use.\r
-    //\r
-    if (mVariableModuleGlobal->LangCodes != NULL) {\r
-      FreePool (mVariableModuleGlobal->LangCodes);\r
-    }\r
-    mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
-    ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r
-  }\r
-\r
-  if (SetLanguageCodes\r
-      && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r
-      && (mVariableModuleGlobal->LangCodes != NULL)) {\r
-    //\r
-    // Update Lang if PlatformLang is already set\r
-    // Update PlatformLang if Lang is already set\r
-    //\r
-    Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // Update Lang\r
-      //\r
-      VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
-      Data         = GetVariableDataPtr (Variable.CurrPtr);\r
-      DataSize     = Variable.CurrPtr->DataSize;\r
-    } else {\r
-      Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // Update PlatformLang\r
-        //\r
-        VariableName = EFI_LANG_VARIABLE_NAME;\r
-        Data         = GetVariableDataPtr (Variable.CurrPtr);\r
-        DataSize     = Variable.CurrPtr->DataSize;\r
-      } else {\r
-        //\r
-        // Neither PlatformLang nor Lang is set, directly return\r
-        //\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-  }\r
-\r
-  Status = EFI_SUCCESS;\r
-\r
-  //\r
-  // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
-  //\r
-  Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
-\r
-  if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r
-    //\r
-    // Update Lang when PlatformLangCodes/LangCodes were set.\r
-    //\r
-    if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
-      //\r
-      // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
-      //\r
-      BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
-      if (BestPlatformLang != NULL) {\r
-        //\r
-        // Get the corresponding index in language codes.\r
-        //\r
-        Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
-\r
-        //\r
-        // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
-        //\r
-        BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r
-\r
-        //\r
-        // Check the variable space for both Lang and PlatformLang variable.\r
-        //\r
-        VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
-        VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
-        VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r
-\r
-        VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r
-        VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
-        VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
-        if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
-          //\r
-          // No enough variable space to set both Lang and PlatformLang successfully.\r
-          //\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-        } else {\r
-          //\r
-          // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
-          //\r
-          FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-\r
-          Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r
-                                   ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r
-        }\r
-\r
-        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r
-      }\r
-    }\r
-\r
-  } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r
-    //\r
-    // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
-    //\r
-    if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r
-      //\r
-      // When setting Lang, firstly get most matched language string from supported language codes.\r
-      //\r
-      BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
-      if (BestLang != NULL) {\r
-        //\r
-        // Get the corresponding index in language codes.\r
-        //\r
-        Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
-\r
-        //\r
-        // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
-        //\r
-        BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
-\r
-        //\r
-        // Check the variable space for both PlatformLang and Lang variable.\r
-        //\r
-        VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r
-        VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r
-        VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r
-\r
-        VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r
-        VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r
-        VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r
-        if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r
-          //\r
-          // No enough variable space to set both PlatformLang and Lang successfully.\r
-          //\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-        } else {\r
-          //\r
-          // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
-          //\r
-          FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-\r
-          Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,\r
-                                   AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r
-        }\r
-\r
-        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r
-      }\r
-    }\r
-  }\r
-\r
-  if (SetLanguageCodes) {\r
-    //\r
-    // Continue to set PlatformLangCodes or LangCodes.\r
-    //\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    return Status;\r
-  }\r
-}\r
-\r
-/**\r
-  Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
-  index of associated public key is needed.\r
-\r
-  @param[in] VariableName       Name of variable.\r
-  @param[in] VendorGuid         Guid of variable.\r
-  @param[in] Data               Variable data.\r
-  @param[in] DataSize           Size of data. 0 means delete.\r
-  @param[in] Attributes         Attributes of the variable.\r
-  @param[in] KeyIndex           Index of associated public key.\r
-  @param[in] MonotonicCount     Value of associated monotonic count.\r
-  @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r
-  @param[in] TimeStamp          Value of associated TimeStamp.\r
-\r
-  @retval EFI_SUCCESS           The update operation is success.\r
-  @retval EFI_OUT_OF_RESOURCES  Variable region is full, can not write other data into this region.\r
-\r
-**/\r
-EFI_STATUS\r
-UpdateVariable (\r
-  IN      CHAR16                      *VariableName,\r
-  IN      EFI_GUID                    *VendorGuid,\r
-  IN      VOID                        *Data,\r
-  IN      UINTN                       DataSize,\r
-  IN      UINT32                      Attributes      OPTIONAL,\r
-  IN      UINT32                      KeyIndex        OPTIONAL,\r
-  IN      UINT64                      MonotonicCount  OPTIONAL,\r
-  IN OUT  VARIABLE_POINTER_TRACK      *CacheVariable,\r
-  IN      EFI_TIME                    *TimeStamp      OPTIONAL\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  VARIABLE_HEADER                     *NextVariable;\r
-  UINTN                               ScratchSize;\r
-  UINTN                               MaxDataSize;\r
-  UINTN                               VarNameOffset;\r
-  UINTN                               VarDataOffset;\r
-  UINTN                               VarNameSize;\r
-  UINTN                               VarSize;\r
-  BOOLEAN                             Volatile;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  UINT8                               State;\r
-  VARIABLE_POINTER_TRACK              *Variable;\r
-  VARIABLE_POINTER_TRACK              NvVariable;\r
-  VARIABLE_STORE_HEADER               *VariableStoreHeader;\r
-  UINTN                               CacheOffset;\r
-  UINT8                               *BufferForMerge;\r
-  UINTN                               MergedBufSize;\r
-  BOOLEAN                             DataReady;\r
-  UINTN                               DataOffset;\r
-  BOOLEAN                             IsCommonVariable;\r
-  BOOLEAN                             IsCommonUserVariable;\r
-\r
-  if (mVariableModuleGlobal->FvbInstance == NULL) {\r
-    //\r
-    // The FVB protocol is not installed, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r
-    //\r
-    if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-      //\r
-      // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
-      //\r
-      return EFI_NOT_AVAILABLE_YET;\r
-    } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
-      //\r
-      // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
-      // The authenticated variable perhaps is not initialized, just return here.\r
-      //\r
-      return EFI_NOT_AVAILABLE_YET;\r
-    }\r
-  }\r
-\r
-  if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
-    Variable = CacheVariable;\r
-  } else {\r
-    //\r
-    // Update/Delete existing NV variable.\r
-    // CacheVariable points to the variable in the memory copy of Flash area\r
-    // Now let Variable points to the same variable in Flash area.\r
-    //\r
-    VariableStoreHeader  = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-    Variable = &NvVariable;\r
-    Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
-    Variable->EndPtr   = GetEndPointer (VariableStoreHeader);\r
-    Variable->CurrPtr  = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
-    if (CacheVariable->InDeletedTransitionPtr != NULL) {\r
-      Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r
-    } else {\r
-      Variable->InDeletedTransitionPtr = NULL;\r
-    }\r
-    Variable->Volatile = FALSE;\r
-  }\r
-\r
-  Fvb       = mVariableModuleGlobal->FvbInstance;\r
-\r
-  //\r
-  // Tricky part: Use scratch data area at the end of volatile variable store\r
-  // as a temporary storage.\r
-  //\r
-  NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
-  ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\r
-  SetMem (NextVariable, ScratchSize, 0xff);\r
-  DataReady = FALSE;\r
-\r
-  if (Variable->CurrPtr != NULL) {\r
-    //\r
-    // Update/Delete existing variable.\r
-    //\r
-    if (AtRuntime ()) {\r
-      //\r
-      // If AtRuntime and the variable is Volatile and Runtime Access,\r
-      // the volatile is ReadOnly, and SetVariable should be aborted and\r
-      // return EFI_WRITE_PROTECTED.\r
-      //\r
-      if (Variable->Volatile) {\r
-        Status = EFI_WRITE_PROTECTED;\r
-        goto Done;\r
-      }\r
-      //\r
-      // Only variable that have NV attributes can be updated/deleted in Runtime.\r
-      //\r
-      if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
-        Status = EFI_INVALID_PARAMETER;\r
-        goto Done;\r
-      }\r
-\r
-      //\r
-      // Only variable that have RT attributes can be updated/deleted in Runtime.\r
-      //\r
-      if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r
-        Status = EFI_INVALID_PARAMETER;\r
-        goto Done;\r
-      }\r
-    }\r
-\r
-    //\r
-    // Setting a data variable with no access, or zero DataSize attributes\r
-    // causes it to be deleted.\r
-    // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r
-    // not delete the variable.\r
-    //\r
-    if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r
-      if (Variable->InDeletedTransitionPtr != NULL) {\r
-        //\r
-        // Both ADDED and IN_DELETED_TRANSITION variable are present,\r
-        // set IN_DELETED_TRANSITION one to DELETED state first.\r
-        //\r
-        State = Variable->InDeletedTransitionPtr->State;\r
-        State &= VAR_DELETED;\r
-        Status = UpdateVariableStore (\r
-                   &mVariableModuleGlobal->VariableGlobal,\r
-                   Variable->Volatile,\r
-                   FALSE,\r
-                   Fvb,\r
-                   (UINTN) &Variable->InDeletedTransitionPtr->State,\r
-                   sizeof (UINT8),\r
-                   &State\r
-                   );\r
-        if (!EFI_ERROR (Status)) {\r
-          if (!Variable->Volatile) {\r
-            ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
-            CacheVariable->InDeletedTransitionPtr->State = State;\r
-          }\r
-        } else {\r
-          goto Done;\r
-        }\r
-      }\r
-\r
-      State = Variable->CurrPtr->State;\r
-      State &= VAR_DELETED;\r
-\r
-      Status = UpdateVariableStore (\r
-                 &mVariableModuleGlobal->VariableGlobal,\r
-                 Variable->Volatile,\r
-                 FALSE,\r
-                 Fvb,\r
-                 (UINTN) &Variable->CurrPtr->State,\r
-                 sizeof (UINT8),\r
-                 &State\r
-                 );\r
-      if (!EFI_ERROR (Status)) {\r
-        UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
-        if (!Variable->Volatile) {\r
-          CacheVariable->CurrPtr->State = State;\r
-          FlushHobVariableToFlash (VariableName, VendorGuid);\r
-        }\r
-      }\r
-      goto Done;\r
-    }\r
-    //\r
-    // If the variable is marked valid, and the same data has been passed in,\r
-    // then return to the caller immediately.\r
-    //\r
-    if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r
-        (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0) &&\r
-        ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r
-        (TimeStamp == NULL)) {\r
-      //\r
-      // Variable content unchanged and no need to update timestamp, just return.\r
-      //\r
-      UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
-      Status = EFI_SUCCESS;\r
-      goto Done;\r
-    } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
-               (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
-\r
-      //\r
-      // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable\r
-      //\r
-      if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r
-        //\r
-        // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r
-        // From DataOffset of NextVariable is to save the existing variable data.\r
-        //\r
-        DataOffset = sizeof (VARIABLE_HEADER) + Variable->CurrPtr->NameSize + GET_PAD_SIZE (Variable->CurrPtr->NameSize);\r
-        BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r
-        CopyMem (BufferForMerge, (UINT8 *) ((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize);\r
-\r
-        //\r
-        // Set Max Common/Auth Variable Data Size as default MaxDataSize\r
-        //\r
-        if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
-          MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r
-        } else {\r
-          MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r
-        }\r
-\r
-        if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
-            ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
-             (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0))) ||\r
-             (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) {\r
-          //\r
-          // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of\r
-          // EFI_SIGNATURE_DATA values that are already part of the existing variable value.\r
-          //\r
-          Status = AppendSignatureList (\r
-                     BufferForMerge,\r
-                     Variable->CurrPtr->DataSize,\r
-                     MaxDataSize - Variable->CurrPtr->DataSize,\r
-                     Data,\r
-                     DataSize,\r
-                     &MergedBufSize\r
-                     );\r
-          if (Status == EFI_BUFFER_TOO_SMALL) {\r
-            //\r
-            // Signature List is too long, Failed to Append.\r
-            //\r
-            Status = EFI_INVALID_PARAMETER;\r
-            goto Done;\r
-          }\r
-\r
-          if (MergedBufSize == Variable->CurrPtr->DataSize) {\r
-            if ((TimeStamp == NULL) || CompareTimeStamp (TimeStamp, &Variable->CurrPtr->TimeStamp)) {\r
-              //\r
-              // New EFI_SIGNATURE_DATA is not found and timestamp is not later\r
-              // than current timestamp, return EFI_SUCCESS directly.\r
-              //\r
-              UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
-              Status = EFI_SUCCESS;\r
-              goto Done;\r
-            }\r
-          }\r
-        } else {\r
-          //\r
-          // For other Variables, append the new data to the end of existing data.\r
-          // Max Harware error record variable data size is different from common/auth variable\r
-          //\r
-          if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-            MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r
-          }\r
-\r
-          if (Variable->CurrPtr->DataSize + DataSize > MaxDataSize) {\r
-            //\r
-            // Existing data size + new data size exceed maximum variable size limitation.\r
-            //\r
-            Status = EFI_INVALID_PARAMETER;\r
-            goto Done;\r
-          }\r
-          CopyMem ((UINT8*) ((UINTN) BufferForMerge + Variable->CurrPtr->DataSize), Data, DataSize);\r
-          MergedBufSize = Variable->CurrPtr->DataSize + DataSize;\r
-        }\r
-\r
-        //\r
-        // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r
-        //\r
-        Data      = BufferForMerge;\r
-        DataSize  = MergedBufSize;\r
-        DataReady = TRUE;\r
-      }\r
-\r
-      //\r
-      // Mark the old variable as in delete transition.\r
-      //\r
-      State = Variable->CurrPtr->State;\r
-      State &= VAR_IN_DELETED_TRANSITION;\r
-\r
-      Status = UpdateVariableStore (\r
-                 &mVariableModuleGlobal->VariableGlobal,\r
-                 Variable->Volatile,\r
-                 FALSE,\r
-                 Fvb,\r
-                 (UINTN) &Variable->CurrPtr->State,\r
-                 sizeof (UINT8),\r
-                 &State\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
-      }\r
-      if (!Variable->Volatile) {\r
-        CacheVariable->CurrPtr->State = State;\r
-      }\r
-    }\r
-  } else {\r
-    //\r
-    // Not found existing variable. Create a new variable.\r
-    //\r
-\r
-    if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r
-      Status = EFI_SUCCESS;\r
-      goto Done;\r
-    }\r
-\r
-    //\r
-    // Make sure we are trying to create a new variable.\r
-    // Setting a data variable with zero DataSize or no access attributes means to delete it.\r
-    //\r
-    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
-      Status = EFI_NOT_FOUND;\r
-      goto Done;\r
-    }\r
-\r
-    //\r
-    // Only variable have NV|RT attribute can be created in Runtime.\r
-    //\r
-    if (AtRuntime () &&\r
-        (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
-      Status = EFI_INVALID_PARAMETER;\r
-      goto Done;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Function part - create a new variable and copy the data.\r
-  // Both update a variable and create a variable will come here.\r
-  //\r
-  NextVariable->StartId     = VARIABLE_DATA;\r
-  //\r
-  // NextVariable->State = VAR_ADDED;\r
-  //\r
-  NextVariable->Reserved        = 0;\r
-  NextVariable->PubKeyIndex     = KeyIndex;\r
-  NextVariable->MonotonicCount  = MonotonicCount;\r
-  ZeroMem (&NextVariable->TimeStamp, sizeof (EFI_TIME));\r
-\r
-  if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r
-      (TimeStamp != NULL)) {\r
-    if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r
-      CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
-    } else {\r
-      //\r
-      // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r
-      // when the new TimeStamp value is later than the current timestamp associated\r
-      // with the variable, we need associate the new timestamp with the updated value.\r
-      //\r
-      if (Variable->CurrPtr != NULL) {\r
-        if (CompareTimeStamp (&Variable->CurrPtr->TimeStamp, TimeStamp)) {\r
-          CopyMem (&NextVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  //\r
-  // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r
-  // Attributes bitmask parameter of a GetVariable() call.\r
-  //\r
-  NextVariable->Attributes  = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r
-\r
-  VarNameOffset                 = sizeof (VARIABLE_HEADER);\r
-  VarNameSize                   = StrSize (VariableName);\r
-  CopyMem (\r
-    (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
-    VariableName,\r
-    VarNameSize\r
-    );\r
-  VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
-\r
-  //\r
-  // If DataReady is TRUE, it means the variable data has been saved into\r
-  // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r
-  //\r
-  if (!DataReady) {\r
-    CopyMem (\r
-      (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
-      Data,\r
-      DataSize\r
-      );\r
-  }\r
-\r
-  CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
-  //\r
-  // There will be pad bytes after Data, the NextVariable->NameSize and\r
-  // NextVariable->DataSize should not include pad size so that variable\r
-  // service can get actual size in GetVariable.\r
-  //\r
-  NextVariable->NameSize  = (UINT32)VarNameSize;\r
-  NextVariable->DataSize  = (UINT32)DataSize;\r
-\r
-  //\r
-  // The actual size of the variable that stores in storage should\r
-  // include pad size.\r
-  //\r
-  VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
-  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-    //\r
-    // Create a nonvolatile variable.\r
-    //\r
-    Volatile = FALSE;\r
-\r
-    IsCommonVariable = FALSE;\r
-    IsCommonUserVariable = FALSE;\r
-    if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r
-      IsCommonVariable = TRUE;\r
-      IsCommonUserVariable = IsUserVariable (NextVariable);\r
-    }\r
-    if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r
-      && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
-      || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r
-      || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r
-      || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r
-      if (AtRuntime ()) {\r
-        if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
-          RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
-        }\r
-        if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r
-          RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
-        }\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-        goto Done;\r
-      }\r
-      //\r
-      // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
-      //\r
-      Status = Reclaim (\r
-                 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
-                 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
-                 FALSE,\r
-                 Variable,\r
-                 NextVariable,\r
-                 HEADER_ALIGN (VarSize),\r
-                 FALSE\r
-                 );\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // The new variable has been integrated successfully during reclaiming.\r
-        //\r
-        if (Variable->CurrPtr != NULL) {\r
-          CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
-          CacheVariable->InDeletedTransitionPtr = NULL;\r
-        }\r
-        UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
-        FlushHobVariableToFlash (VariableName, VendorGuid);\r
-      } else {\r
-        if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r
-          RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
-        }\r
-        if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r
-          RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r
-        }\r
-      }\r
-      goto Done;\r
-    }\r
-    //\r
-    // Four steps\r
-    // 1. Write variable header\r
-    // 2. Set variable state to header valid\r
-    // 3. Write variable data\r
-    // 4. Set variable state to valid\r
-    //\r
-    //\r
-    // Step 1:\r
-    //\r
-    CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE,\r
-               TRUE,\r
-               Fvb,\r
-               mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
-               sizeof (VARIABLE_HEADER),\r
-               (UINT8 *) NextVariable\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-\r
-    //\r
-    // Step 2:\r
-    //\r
-    NextVariable->State = VAR_HEADER_VALID_ONLY;\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE,\r
-               TRUE,\r
-               Fvb,\r
-               mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
-               sizeof (UINT8),\r
-               &NextVariable->State\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-    //\r
-    // Step 3:\r
-    //\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE,\r
-               TRUE,\r
-               Fvb,\r
-               mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
-               (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
-               (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-    //\r
-    // Step 4:\r
-    //\r
-    NextVariable->State = VAR_ADDED;\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               FALSE,\r
-               TRUE,\r
-               Fvb,\r
-               mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r
-               sizeof (UINT8),\r
-               &NextVariable->State\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-\r
-    mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
-\r
-    if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
-      mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
-    } else {\r
-      mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
-      if (IsCommonUserVariable) {\r
-        mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r
-      }\r
-    }\r
-    //\r
-    // update the memory copy of Flash region.\r
-    //\r
-    CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r
-  } else {\r
-    //\r
-    // Create a volatile variable.\r
-    //\r
-    Volatile = TRUE;\r
-\r
-    if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
-        ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r
-      //\r
-      // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r
-      //\r
-      Status = Reclaim (\r
-                 mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r
-                 &mVariableModuleGlobal->VolatileLastVariableOffset,\r
-                 TRUE,\r
-                 Variable,\r
-                 NextVariable,\r
-                 HEADER_ALIGN (VarSize),\r
-                 FALSE\r
-                 );\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // The new variable has been integrated successfully during reclaiming.\r
-        //\r
-        if (Variable->CurrPtr != NULL) {\r
-          CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r
-          CacheVariable->InDeletedTransitionPtr = NULL;\r
-        }\r
-        UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r
-      }\r
-      goto Done;\r
-    }\r
-\r
-    NextVariable->State = VAR_ADDED;\r
-    Status = UpdateVariableStore (\r
-               &mVariableModuleGlobal->VariableGlobal,\r
-               TRUE,\r
-               TRUE,\r
-               Fvb,\r
-               mVariableModuleGlobal->VolatileLastVariableOffset,\r
-               (UINT32) VarSize,\r
-               (UINT8 *) NextVariable\r
-               );\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      goto Done;\r
-    }\r
-\r
-    mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
-  }\r
-\r
-  //\r
-  // Mark the old variable as deleted.\r
-  //\r
-  if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
-    if (Variable->InDeletedTransitionPtr != NULL) {\r
-      //\r
-      // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r
-      // set IN_DELETED_TRANSITION one to DELETED state first.\r
-      //\r
-      State = Variable->InDeletedTransitionPtr->State;\r
-      State &= VAR_DELETED;\r
-      Status = UpdateVariableStore (\r
-                 &mVariableModuleGlobal->VariableGlobal,\r
-                 Variable->Volatile,\r
-                 FALSE,\r
-                 Fvb,\r
-                 (UINTN) &Variable->InDeletedTransitionPtr->State,\r
-                 sizeof (UINT8),\r
-                 &State\r
-                 );\r
-      if (!EFI_ERROR (Status)) {\r
-        if (!Variable->Volatile) {\r
-          ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r
-          CacheVariable->InDeletedTransitionPtr->State = State;\r
-        }\r
-      } else {\r
-        goto Done;\r
-      }\r
-    }\r
-\r
-    State = Variable->CurrPtr->State;\r
-    State &= VAR_DELETED;\r
-\r
-    Status = UpdateVariableStore (\r
-             &mVariableModuleGlobal->VariableGlobal,\r
-             Variable->Volatile,\r
-             FALSE,\r
-             Fvb,\r
-             (UINTN) &Variable->CurrPtr->State,\r
-             sizeof (UINT8),\r
-             &State\r
-             );\r
-    if (!EFI_ERROR (Status) && !Variable->Volatile) {\r
-      CacheVariable->CurrPtr->State = State;\r
-    }\r
-  }\r
-\r
-  if (!EFI_ERROR (Status)) {\r
-    UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
-    if (!Volatile) {\r
-      FlushHobVariableToFlash (VariableName, VendorGuid);\r
-    }\r
-  }\r
-\r
-Done:\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Check if a Unicode character is a hexadecimal character.\r
-\r
-  This function checks if a Unicode character is a\r
-  hexadecimal character.  The valid hexadecimal character is\r
-  L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
-\r
-\r
-  @param Char           The character to check against.\r
-\r
-  @retval TRUE          If the Char is a hexadecmial character.\r
-  @retval FALSE         If the Char is not a hexadecmial character.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsHexaDecimalDigitCharacter (\r
-  IN CHAR16             Char\r
-  )\r
-{\r
-  return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
-}\r
-\r
-/**\r
-\r
-  This code checks if variable is hardware error record variable or not.\r
-\r
-  According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid\r
-  and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value.\r
-\r
-  @param VariableName   Pointer to variable name.\r
-  @param VendorGuid     Variable Vendor Guid.\r
-\r
-  @retval TRUE          Variable is hardware error record variable.\r
-  @retval FALSE         Variable is not hardware error record variable.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsHwErrRecVariable (\r
-  IN CHAR16             *VariableName,\r
-  IN EFI_GUID           *VendorGuid\r
-  )\r
-{\r
-  if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) ||\r
-      (StrLen (VariableName) != StrLen (L"HwErrRec####")) ||\r
-      (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) ||\r
-      !IsHexaDecimalDigitCharacter (VariableName[0x8]) ||\r
-      !IsHexaDecimalDigitCharacter (VariableName[0x9]) ||\r
-      !IsHexaDecimalDigitCharacter (VariableName[0xA]) ||\r
-      !IsHexaDecimalDigitCharacter (VariableName[0xB])) {\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-/**\r
-  Mark a variable that will become read-only after leaving the DXE phase of execution.\r
-\r
-  @param[in] This          The VARIABLE_LOCK_PROTOCOL instance.\r
-  @param[in] VariableName  A pointer to the variable name that will be made read-only subsequently.\r
-  @param[in] VendorGuid    A pointer to the vendor GUID that will be made read-only subsequently.\r
-\r
-  @retval EFI_SUCCESS           The variable specified by the VariableName and the VendorGuid was marked\r
-                                as pending to be read-only.\r
-  @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
-                                Or VariableName is an empty string.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource to hold the lock request.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableLockRequestToLock (\r
-  IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
-  IN       CHAR16                       *VariableName,\r
-  IN       EFI_GUID                     *VendorGuid\r
-  )\r
-{\r
-  VARIABLE_ENTRY                  *Entry;\r
-  CHAR16                          *Name;\r
-  LIST_ENTRY                      *Link;\r
-  VARIABLE_ENTRY                  *LockedEntry;\r
-\r
-  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (mEndOfDxe) {\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-\r
-  Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (VariableName));\r
-  if (Entry == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "[Variable] Lock: %g:%s\n", VendorGuid, VariableName));\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  for ( Link = GetFirstNode (&mLockedVariableList)\r
-      ; !IsNull (&mLockedVariableList, Link)\r
-      ; Link = GetNextNode (&mLockedVariableList, Link)\r
-      ) {\r
-    LockedEntry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
-    Name = (CHAR16 *) ((UINTN) LockedEntry + sizeof (*LockedEntry));\r
-    if (CompareGuid (&LockedEntry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
-      goto Done;\r
-    }\r
-  }\r
-\r
-  Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-  StrnCpy   (Name, VariableName, StrLen (VariableName));\r
-  CopyGuid (&Entry->Guid, VendorGuid);\r
-  InsertTailList (&mLockedVariableList, &Entry->Link);\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-\r
-  This code finds variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize is external input.\r
-  This function will do basic validation, before parse the data.\r
-\r
-  @param VariableName               Name of Variable to be found.\r
-  @param VendorGuid                 Variable vendor GUID.\r
-  @param Attributes                 Attribute value of the variable found.\r
-  @param DataSize                   Size of Data found. If size is less than the\r
-                                    data, this value contains the required size.\r
-  @param Data                       Data pointer.\r
-\r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetVariable (\r
-  IN      CHAR16            *VariableName,\r
-  IN      EFI_GUID          *VendorGuid,\r
-  OUT     UINT32            *Attributes OPTIONAL,\r
-  IN OUT  UINTN             *DataSize,\r
-  OUT     VOID              *Data\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarDataSize;\r
-\r
-  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  //\r
-  // Get data size\r
-  //\r
-  VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
-  ASSERT (VarDataSize != 0);\r
-\r
-  if (*DataSize >= VarDataSize) {\r
-    if (Data == NULL) {\r
-      Status = EFI_INVALID_PARAMETER;\r
-      goto Done;\r
-    }\r
-\r
-    CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
-    if (Attributes != NULL) {\r
-      *Attributes = Variable.CurrPtr->Attributes;\r
-    }\r
-\r
-    *DataSize = VarDataSize;\r
-    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
-\r
-    Status = EFI_SUCCESS;\r
-    goto Done;\r
-  } else {\r
-    *DataSize = VarDataSize;\r
-    Status = EFI_BUFFER_TOO_SMALL;\r
-    goto Done;\r
-  }\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-\r
-\r
-/**\r
-\r
-  This code Finds the Next available variable.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param VariableNameSize           Size of the variable name.\r
-  @param VariableName               Pointer to variable name.\r
-  @param VendorGuid                 Variable Vendor Guid.\r
-\r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetNextVariableName (\r
-  IN OUT  UINTN             *VariableNameSize,\r
-  IN OUT  CHAR16            *VariableName,\r
-  IN OUT  EFI_GUID          *VendorGuid\r
-  )\r
-{\r
-  VARIABLE_STORE_TYPE     Type;\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  VARIABLE_POINTER_TRACK  VariableInHob;\r
-  VARIABLE_POINTER_TRACK  VariablePtrTrack;\r
-  UINTN                   VarNameSize;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];\r
-\r
-  if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
-  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  if (VariableName[0] != 0) {\r
-    //\r
-    // If variable name is not NULL, get next variable.\r
-    //\r
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-  }\r
-\r
-  //\r
-  // 0: Volatile, 1: HOB, 2: Non-Volatile.\r
-  // The index and attributes mapping must be kept in this order as FindVariable\r
-  // makes use of this mapping to implement search algorithm.\r
-  //\r
-  VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r
-  VariableStoreHeader[VariableStoreTypeHob]      = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
-  VariableStoreHeader[VariableStoreTypeNv]       = mNvVariableCache;\r
-\r
-  while (TRUE) {\r
-    //\r
-    // Switch from Volatile to HOB, to Non-Volatile.\r
-    //\r
-    while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r
-      //\r
-      // Find current storage index\r
-      //\r
-      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
-        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
-          break;\r
-        }\r
-      }\r
-      ASSERT (Type < VariableStoreTypeMax);\r
-      //\r
-      // Switch to next storage\r
-      //\r
-      for (Type++; Type < VariableStoreTypeMax; Type++) {\r
-        if (VariableStoreHeader[Type] != NULL) {\r
-          break;\r
-        }\r
-      }\r
-      //\r
-      // Capture the case that\r
-      // 1. current storage is the last one, or\r
-      // 2. no further storage\r
-      //\r
-      if (Type == VariableStoreTypeMax) {\r
-        Status = EFI_NOT_FOUND;\r
-        goto Done;\r
-      }\r
-      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
-      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);\r
-      Variable.CurrPtr  = Variable.StartPtr;\r
-    }\r
-\r
-    //\r
-    // Variable is found\r
-    //\r
-    if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-      if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
-        if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-          //\r
-          // If it is a IN_DELETED_TRANSITION variable,\r
-          // and there is also a same ADDED one at the same time,\r
-          // don't return it.\r
-          //\r
-          VariablePtrTrack.StartPtr = Variable.StartPtr;\r
-          VariablePtrTrack.EndPtr = Variable.EndPtr;\r
-          Status = FindVariableEx (\r
-                     GetVariableNamePtr (Variable.CurrPtr),\r
-                     &Variable.CurrPtr->VendorGuid,\r
-                     FALSE,\r
-                     &VariablePtrTrack\r
-                     );\r
-          if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r
-            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-            continue;\r
-          }\r
-        }\r
-\r
-        //\r
-        // Don't return NV variable when HOB overrides it\r
-        //\r
-        if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
-            (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
-           ) {\r
-          VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r
-          VariableInHob.EndPtr   = GetEndPointer   (VariableStoreHeader[VariableStoreTypeHob]);\r
-          Status = FindVariableEx (\r
-                     GetVariableNamePtr (Variable.CurrPtr),\r
-                     &Variable.CurrPtr->VendorGuid,\r
-                     FALSE,\r
-                     &VariableInHob\r
-                     );\r
-          if (!EFI_ERROR (Status)) {\r
-            Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-            continue;\r
-          }\r
-        }\r
-\r
-        VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
-        ASSERT (VarNameSize != 0);\r
-\r
-        if (VarNameSize <= *VariableNameSize) {\r
-          CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
-          CopyMem (VendorGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
-          Status = EFI_SUCCESS;\r
-        } else {\r
-          Status = EFI_BUFFER_TOO_SMALL;\r
-        }\r
-\r
-        *VariableNameSize = VarNameSize;\r
-        goto Done;\r
-      }\r
-    }\r
-\r
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-  }\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-\r
-  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param VariableName                     Name of Variable to be found.\r
-  @param VendorGuid                       Variable vendor GUID.\r
-  @param Attributes                       Attribute value of the variable found\r
-  @param DataSize                         Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param Data                             Data pointer.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SUCCESS                     Set successfully.\r
-  @return EFI_OUT_OF_RESOURCES            Resource not enough to set variable.\r
-  @return EFI_NOT_FOUND                   Not found.\r
-  @return EFI_WRITE_PROTECTED             Variable is read-only.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceSetVariable (\r
-  IN CHAR16                  *VariableName,\r
-  IN EFI_GUID                *VendorGuid,\r
-  IN UINT32                  Attributes,\r
-  IN UINTN                   DataSize,\r
-  IN VOID                    *Data\r
-  )\r
-{\r
-  VARIABLE_POINTER_TRACK              Variable;\r
-  EFI_STATUS                          Status;\r
-  VARIABLE_HEADER                     *NextVariable;\r
-  EFI_PHYSICAL_ADDRESS                Point;\r
-  UINTN                               PayloadSize;\r
-  LIST_ENTRY                          *Link;\r
-  VARIABLE_ENTRY                      *Entry;\r
-  CHAR16                              *Name;\r
-\r
-  //\r
-  // Check input parameters.\r
-  //\r
-  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (DataSize != 0 && Data == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Check for reserverd bit in variable attribute.\r
-  //\r
-  if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  //  Make sure if runtime bit is set, boot service bit is set also.\r
-  //\r
-  if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r
-  // cannot be set both.\r
-  //\r
-  if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-     && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
-    if (DataSize < AUTHINFO_SIZE) {\r
-      //\r
-      // Try to write Authenticated Variable without AuthInfo.\r
-      //\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-    PayloadSize = DataSize - AUTHINFO_SIZE;\r
-  } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r
-    //\r
-    // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r
-    //\r
-    if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r
-      ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r
-      ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
-      return EFI_SECURITY_VIOLATION;\r
-    }\r
-    PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r
-  } else {\r
-    PayloadSize = DataSize;\r
-  }\r
-\r
-  if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r
-    //\r
-    // Prevent whole variable size overflow\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  //  The size of the VariableName, including the Unicode Null in bytes plus\r
-  //  the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
-  //  bytes for HwErrRec.\r
-  //\r
-  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    if (!IsHwErrRecVariable(VariableName, VendorGuid)) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-  } else {\r
-    //\r
-    //  The size of the VariableName, including the Unicode Null in bytes plus\r
-    //  the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r
-    //\r
-    if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
-      if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER)) {\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-    } else {\r
-      if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - sizeof (VARIABLE_HEADER)) {\r
-        return EFI_INVALID_PARAMETER;\r
-      }\r
-    }\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  //\r
-  // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r
-  //\r
-  if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r
-    Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
-    //\r
-    // Parse non-volatile variable data and get last variable offset.\r
-    //\r
-    NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r
-    while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r
-      NextVariable = GetNextVariablePtr (NextVariable);\r
-    }\r
-    mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r
-  }\r
-\r
-  if (mEndOfDxe && mEnableLocking) {\r
-    //\r
-    // Treat the variables listed in the forbidden variable list as read-only after leaving DXE phase.\r
-    //\r
-    for ( Link = GetFirstNode (&mLockedVariableList)\r
-        ; !IsNull (&mLockedVariableList, Link)\r
-        ; Link = GetNextNode (&mLockedVariableList, Link)\r
-        ) {\r
-      Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
-      Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));\r
-      if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {\r
-        Status = EFI_WRITE_PROTECTED;\r
-        DEBUG ((EFI_D_INFO, "[Variable]: Changing readonly variable after leaving DXE phase - %g:%s\n", VendorGuid, VariableName));\r
-        goto Done;\r
-      }\r
-    }\r
-  }\r
-\r
-  Status = InternalVarCheckSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  //\r
-  // Check whether the input variable is already existed.\r
-  //\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r
-  if (!EFI_ERROR (Status)) {\r
-    if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r
-      Status = EFI_WRITE_PROTECTED;\r
-      goto Done;\r
-    }\r
-    if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r
-      //\r
-      // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r
-      // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r
-      // 1. No access attributes specified\r
-      // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
-      //\r
-      Status = EFI_INVALID_PARAMETER;\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName));\r
-      goto Done;\r
-    }\r
-  }\r
-\r
-  if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r
-    //\r
-    // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r
-    //\r
-    Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r
-    if (EFI_ERROR (Status)) {\r
-      //\r
-      // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r
-      //\r
-      goto Done;\r
-    }\r
-  }\r
-\r
-  //\r
-  // Process PK, KEK, Sigdb seperately.\r
-  //\r
-  if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
-    Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, TRUE);\r
-  } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
-    Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
-  } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
-             ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE)  == 0) ||\r
-              (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r
-              (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)\r
-             )\r
-            ) {\r
-    Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE);\r
-    if (EFI_ERROR (Status)) {\r
-      Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
-    }\r
-  } else {\r
-    Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes);\r
-  }\r
-\r
-Done:\r
-  InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  if (!AtRuntime ()) {\r
-    if (!EFI_ERROR (Status)) {\r
-      SecureBootHook (\r
-        VariableName,\r
-        VendorGuid\r
-        );\r
-    }\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-/**\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param Attributes                     Attributes bitmask to specify the type of variables\r
-                                        on which to return information.\r
-  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available\r
-                                        for the EFI variables associated with the attributes specified.\r
-  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available\r
-                                        for EFI variables associated with the attributes specified.\r
-  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables\r
-                                        associated with the attributes specified.\r
-\r
-  @return EFI_SUCCESS                   Query successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceQueryVariableInfoInternal (\r
-  IN  UINT32                 Attributes,\r
-  OUT UINT64                 *MaximumVariableStorageSize,\r
-  OUT UINT64                 *RemainingVariableStorageSize,\r
-  OUT UINT64                 *MaximumVariableSize\r
-  )\r
-{\r
-  VARIABLE_HEADER        *Variable;\r
-  VARIABLE_HEADER        *NextVariable;\r
-  UINT64                 VariableSize;\r
-  VARIABLE_STORE_HEADER  *VariableStoreHeader;\r
-  UINT64                 CommonVariableTotalSize;\r
-  UINT64                 HwErrVariableTotalSize;\r
-  EFI_STATUS             Status;\r
-  VARIABLE_POINTER_TRACK VariablePtrTrack;\r
-\r
-  CommonVariableTotalSize = 0;\r
-  HwErrVariableTotalSize = 0;\r
-\r
-  if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
-    //\r
-    // Query is Volatile related.\r
-    //\r
-    VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
-  } else {\r
-    //\r
-    // Query is Non-Volatile related.\r
-    //\r
-    VariableStoreHeader = mNvVariableCache;\r
-  }\r
-\r
-  //\r
-  // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
-  // with the storage size (excluding the storage header size).\r
-  //\r
-  *MaximumVariableStorageSize   = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
-\r
-  //\r
-  // Harware error record variable needs larger size.\r
-  //\r
-  if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-    *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
-    *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
-  } else {\r
-    if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-      if (AtRuntime ()) {\r
-        *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
-      } else {\r
-        *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
-      }\r
-    }\r
-\r
-    //\r
-    // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r
-    //\r
-    if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r
-      *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER);\r
-    } else {\r
-      *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - sizeof (VARIABLE_HEADER);\r
-    }\r
-  }\r
-\r
-  //\r
-  // Point to the starting address of the variables.\r
-  //\r
-  Variable = GetStartPointer (VariableStoreHeader);\r
-\r
-  //\r
-  // Now walk through the related variable store.\r
-  //\r
-  while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r
-    NextVariable = GetNextVariablePtr (Variable);\r
-    VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
-\r
-    if (AtRuntime ()) {\r
-      //\r
-      // We don't take the state of the variables in mind\r
-      // when calculating RemainingVariableStorageSize,\r
-      // since the space occupied by variables not marked with\r
-      // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
-      //\r
-      if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-        HwErrVariableTotalSize += VariableSize;\r
-      } else {\r
-        CommonVariableTotalSize += VariableSize;\r
-      }\r
-    } else {\r
-      //\r
-      // Only care about Variables with State VAR_ADDED, because\r
-      // the space not marked as VAR_ADDED is reclaimable now.\r
-      //\r
-      if (Variable->State == VAR_ADDED) {\r
-        if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          HwErrVariableTotalSize += VariableSize;\r
-        } else {\r
-          CommonVariableTotalSize += VariableSize;\r
-        }\r
-      } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
-        //\r
-        // If it is a IN_DELETED_TRANSITION variable,\r
-        // and there is not also a same ADDED one at the same time,\r
-        // this IN_DELETED_TRANSITION variable is valid.\r
-        //\r
-        VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
-        VariablePtrTrack.EndPtr   = GetEndPointer   (VariableStoreHeader);\r
-        Status = FindVariableEx (\r
-                   GetVariableNamePtr (Variable),\r
-                   &Variable->VendorGuid,\r
-                   FALSE,\r
-                   &VariablePtrTrack\r
-                   );\r
-        if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r
-          if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-            HwErrVariableTotalSize += VariableSize;\r
-          } else {\r
-            CommonVariableTotalSize += VariableSize;\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    //\r
-    // Go to the next one.\r
-    //\r
-    Variable = NextVariable;\r
-  }\r
-\r
-  if ((Attributes  & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
-    *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
-  } else {\r
-    if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r
-      *RemainingVariableStorageSize = 0;\r
-    } else {\r
-      *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
-    }\r
-  }\r
-\r
-  if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
-    *MaximumVariableSize = 0;\r
-  } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
-    *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param Attributes                     Attributes bitmask to specify the type of variables\r
-                                        on which to return information.\r
-  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available\r
-                                        for the EFI variables associated with the attributes specified.\r
-  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available\r
-                                        for EFI variables associated with the attributes specified.\r
-  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables\r
-                                        associated with the attributes specified.\r
-\r
-  @return EFI_INVALID_PARAMETER         An invalid combination of attribute bits was supplied.\r
-  @return EFI_SUCCESS                   Query successfully.\r
-  @return EFI_UNSUPPORTED               The attribute is not supported on this platform.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceQueryVariableInfo (\r
-  IN  UINT32                 Attributes,\r
-  OUT UINT64                 *MaximumVariableStorageSize,\r
-  OUT UINT64                 *RemainingVariableStorageSize,\r
-  OUT UINT64                 *MaximumVariableSize\r
-  )\r
-{\r
-  EFI_STATUS             Status;\r
-\r
-  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if ((Attributes & VARIABLE_ATTRIBUTE_NV_BS_RT_AT_HR_AW) == 0) {\r
-    //\r
-    // Make sure the Attributes combination is supported by the platform.\r
-    //\r
-    return EFI_UNSUPPORTED;\r
-  } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
-    //\r
-    // Make sure if runtime bit is set, boot service bit is set also.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
-    //\r
-    // Make sure RT Attribute is set if we are in Runtime phase.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    //\r
-    // Make sure Hw Attribute is set with NV.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Status = VariableServiceQueryVariableInfoInternal (\r
-             Attributes,\r
-             MaximumVariableStorageSize,\r
-             RemainingVariableStorageSize,\r
-             MaximumVariableSize\r
-             );\r
-\r
-  ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  This function reclaims variable storage if free size is below the threshold.\r
-\r
-  Caution: This function may be invoked at SMM mode.\r
-  Care must be taken to make sure not security issue.\r
-\r
-**/\r
-VOID\r
-ReclaimForOS(\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                     Status;\r
-  UINTN                          RemainingCommonRuntimeVariableSpace;\r
-  UINTN                          RemainingHwErrVariableSpace;\r
-  STATIC BOOLEAN                 Reclaimed;\r
-\r
-  //\r
-  // This function will be called only once at EndOfDxe or ReadyToBoot event.\r
-  //\r
-  if (Reclaimed) {\r
-    return;\r
-  }\r
-  Reclaimed = TRUE;\r
-\r
-  Status  = EFI_SUCCESS;\r
-\r
-  if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r
-    RemainingCommonRuntimeVariableSpace = 0;\r
-  } else {\r
-    RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
-  }\r
-\r
-  RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
-\r
-  //\r
-  // Check if the free area is below a threshold.\r
-  //\r
-  if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r
-       (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r
-      ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
-       (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
-    Status = Reclaim (\r
-            mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
-            &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
-            FALSE,\r
-            NULL,\r
-            NULL,\r
-            0,\r
-            FALSE\r
-            );\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-}\r
-\r
-/**\r
-  Init non-volatile variable store.\r
-\r
-  @retval EFI_SUCCESS           Function successfully executed.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resource.\r
-  @retval EFI_VOLUME_CORRUPTED  Variable Store or Firmware Volume for Variable Store is corrupted.\r
-\r
-**/\r
-EFI_STATUS\r
-InitNonVolatileVariableStore (\r
-  VOID\r
-  )\r
-{\r
-  EFI_FIRMWARE_VOLUME_HEADER            *FvHeader;\r
-  VARIABLE_HEADER                       *Variable;\r
-  VARIABLE_HEADER                       *NextVariable;\r
-  EFI_PHYSICAL_ADDRESS                  VariableStoreBase;\r
-  UINT64                                VariableStoreLength;\r
-  UINTN                                 VariableSize;\r
-  EFI_HOB_GUID_TYPE                     *GuidHob;\r
-  EFI_PHYSICAL_ADDRESS                  NvStorageBase;\r
-  UINT8                                 *NvStorageData;\r
-  UINT32                                NvStorageSize;\r
-  FAULT_TOLERANT_WRITE_LAST_WRITE_DATA  *FtwLastWriteData;\r
-  UINT32                                BackUpOffset;\r
-  UINT32                                BackUpSize;\r
-  UINT32                                HwErrStorageSize;\r
-  UINT32                                MaxUserNvVariableSpaceSize;\r
-  UINT32                                BoottimeReservedNvVariableSpaceSize;\r
-\r
-  mVariableModuleGlobal->FvbInstance = NULL;\r
-\r
-  //\r
-  // Allocate runtime memory used for a memory copy of the FLASH region.\r
-  // Keep the memory and the FLASH in sync as updates occur.\r
-  //\r
-  NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r
-  NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r
-  if (NvStorageData == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
-  if (NvStorageBase == 0) {\r
-    NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
-  }\r
-  //\r
-  // Copy NV storage data to the memory buffer.\r
-  //\r
-  CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r
-\r
-  //\r
-  // Check the FTW last write data hob.\r
-  //\r
-  GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r
-  if (GuidHob != NULL) {\r
-    FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r
-    if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r
-      DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r
-      //\r
-      // Copy the backed up NV storage data to the memory buffer from spare block.\r
-      //\r
-      CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r
-    } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r
-               (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r
-      //\r
-      // Flash NV storage from the Offset is backed up in spare block.\r
-      //\r
-      BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r
-      BackUpSize = NvStorageSize - BackUpOffset;\r
-      DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));\r
-      //\r
-      // Copy the partial backed up NV storage data to the memory buffer from spare block.\r
-      //\r
-      CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r
-    }\r
-  }\r
-\r
-  FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r
-\r
-  //\r
-  // Check if the Firmware Volume is not corrupted\r
-  //\r
-  if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r
-    FreePool (NvStorageData);\r
-    DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r
-    return EFI_VOLUME_CORRUPTED;\r
-  }\r
-\r
-  VariableStoreBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) FvHeader + FvHeader->HeaderLength);\r
-  VariableStoreLength = (UINT64) (NvStorageSize - FvHeader->HeaderLength);\r
-\r
-  mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
-  mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r
-  if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r
-    FreePool (NvStorageData);\r
-    DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r
-    return EFI_VOLUME_CORRUPTED;\r
-  }\r
-  ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
-\r
-\r
-  ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r
-\r
-  HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r
-  MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r
-  BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r
-\r
-  //\r
-  // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
-  // is stored with common variable in the same NV region. So the platform integrator should\r
-  // ensure that the value of PcdHwErrStorageSize is less than the value of\r
-  // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
-  //\r
-  ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
-  //\r
-  // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r
-  // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
-  //\r
-  ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
-  //\r
-  // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r
-  // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r
-  //\r
-  ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r
-\r
-  mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r
-  mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r
-  mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r
-\r
-  DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r
-\r
-  //\r
-  // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r
-  //\r
-  ASSERT (MAX_NV_VARIABLE_SIZE < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r
-\r
-  mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r
-  mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r
-\r
-  //\r
-  // Parse non-volatile variable data and get last variable offset.\r
-  //\r
-  Variable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
-  while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
-    NextVariable = GetNextVariablePtr (Variable);\r
-    VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-    if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-      mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
-    } else {\r
-      mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
-    }\r
-\r
-    Variable = NextVariable;\r
-  }\r
-  mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Flush the HOB variable to flash.\r
-\r
-  @param[in] VariableName       Name of variable has been updated or deleted.\r
-  @param[in] VendorGuid         Guid of variable has been updated or deleted.\r
-\r
-**/\r
-VOID\r
-FlushHobVariableToFlash (\r
-  IN CHAR16                     *VariableName,\r
-  IN EFI_GUID                   *VendorGuid\r
-  )\r
-{\r
-  EFI_STATUS                    Status;\r
-  VARIABLE_STORE_HEADER         *VariableStoreHeader;\r
-  VARIABLE_HEADER               *Variable;\r
-  VOID                          *VariableData;\r
-  BOOLEAN                       ErrorFlag;\r
-\r
-  ErrorFlag = FALSE;\r
-\r
-  //\r
-  // Flush the HOB variable to flash.\r
-  //\r
-  if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
-    VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r
-    //\r
-    // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r
-    //\r
-    mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r
-    for ( Variable = GetStartPointer (VariableStoreHeader)\r
-        ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r
-        ; Variable = GetNextVariablePtr (Variable)\r
-        ) {\r
-      if (Variable->State != VAR_ADDED) {\r
-        //\r
-        // The HOB variable has been set to DELETED state in local.\r
-        //\r
-        continue;\r
-      }\r
-      ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r
-      if (VendorGuid == NULL || VariableName == NULL ||\r
-          !CompareGuid (VendorGuid, &Variable->VendorGuid) ||\r
-          StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r
-        VariableData = GetVariableDataPtr (Variable);\r
-        Status = VariableServiceSetVariable (\r
-                   GetVariableNamePtr (Variable),\r
-                   &Variable->VendorGuid,\r
-                   Variable->Attributes,\r
-                   Variable->DataSize,\r
-                   VariableData\r
-                   );\r
-        DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status));\r
-      } else {\r
-        //\r
-        // The updated or deleted variable is matched with the HOB variable.\r
-        // Don't break here because we will try to set other HOB variables\r
-        // since this variable could be set successfully.\r
-        //\r
-        Status = EFI_SUCCESS;\r
-      }\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r
-        // set the HOB variable to DELETED state in local.\r
-        //\r
-        DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable)));\r
-        Variable->State &= VAR_DELETED;\r
-      } else {\r
-        ErrorFlag = TRUE;\r
-      }\r
-    }\r
-    if (ErrorFlag) {\r
-      //\r
-      // We still have HOB variable(s) not flushed in flash.\r
-      //\r
-      mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r
-    } else {\r
-      //\r
-      // All HOB variables have been flushed in flash.\r
-      //\r
-      DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r
-      if (!AtRuntime ()) {\r
-        FreePool ((VOID *) VariableStoreHeader);\r
-      }\r
-    }\r
-  }\r
-\r
-}\r
-\r
-/**\r
-  Initializes variable write service after FTW was ready.\r
-\r
-  @retval EFI_SUCCESS          Function successfully executed.\r
-  @retval Others               Fail to initialize the variable service.\r
-\r
-**/\r
-EFI_STATUS\r
-VariableWriteServiceInitialize (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-  VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
-  UINTN                           Index;\r
-  UINT8                           Data;\r
-  EFI_PHYSICAL_ADDRESS            VariableStoreBase;\r
-  EFI_PHYSICAL_ADDRESS            NvStorageBase;\r
-\r
-  NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
-  if (NvStorageBase == 0) {\r
-    NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
-  }\r
-  VariableStoreBase = NvStorageBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageBase))->HeaderLength);\r
-\r
-  //\r
-  // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
-  //\r
-  mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
-\r
-  //\r
-  // Check if the free area is really free.\r
-  //\r
-  for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
-    Data = ((UINT8 *) mNvVariableCache)[Index];\r
-    if (Data != 0xff) {\r
-      //\r
-      // There must be something wrong in variable store, do reclaim operation.\r
-      //\r
-      Status = Reclaim (\r
-                 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
-                 &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
-                 FALSE,\r
-                 NULL,\r
-                 NULL,\r
-                 0,\r
-                 FALSE\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        return Status;\r
-      }\r
-      break;\r
-    }\r
-  }\r
-\r
-  FlushHobVariableToFlash (NULL, NULL);\r
-\r
-  //\r
-  // Authenticated variable initialize.\r
-  //\r
-  Status = AutenticatedVariableServiceInitialize (mVariableModuleGlobal->MaxAuthVariableSize - sizeof (VARIABLE_HEADER));\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Initializes variable store area for non-volatile and volatile variable.\r
-\r
-  @retval EFI_SUCCESS           Function successfully executed.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resource.\r
-\r
-**/\r
-EFI_STATUS\r
-VariableCommonInitialize (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-  VARIABLE_STORE_HEADER           *VolatileVariableStore;\r
-  VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
-  UINT64                          VariableStoreLength;\r
-  UINTN                           ScratchSize;\r
-  EFI_HOB_GUID_TYPE               *GuidHob;\r
-\r
-  //\r
-  // Allocate runtime memory for variable driver global structure.\r
-  //\r
-  mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r
-  if (mVariableModuleGlobal == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r
-\r
-  //\r
-  // Get HOB variable store.\r
-  //\r
-  GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid);\r
-  if (GuidHob != NULL) {\r
-    VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r
-    VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE));\r
-    if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
-      mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r
-      if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r
-        FreePool (mVariableModuleGlobal);\r
-        return EFI_OUT_OF_RESOURCES;\r
-      }\r
-    } else {\r
-      DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r
-    }\r
-  }\r
-\r
-  //\r
-  // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r
-  //\r
-  ScratchSize = MAX_NV_VARIABLE_SIZE;\r
-  mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r
-  VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
-  if (VolatileVariableStore == NULL) {\r
-    if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
-      FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
-    }\r
-    FreePool (mVariableModuleGlobal);\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
-\r
-  //\r
-  // Initialize Variable Specific Data.\r
-  //\r
-  mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
-  mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
-\r
-  CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
-  VolatileVariableStore->Size        = PcdGet32 (PcdVariableStoreSize);\r
-  VolatileVariableStore->Format      = VARIABLE_STORE_FORMATTED;\r
-  VolatileVariableStore->State       = VARIABLE_STORE_HEALTHY;\r
-  VolatileVariableStore->Reserved    = 0;\r
-  VolatileVariableStore->Reserved1   = 0;\r
-\r
-  //\r
-  // Init non-volatile variable store.\r
-  //\r
-  Status = InitNonVolatileVariableStore ();\r
-  if (EFI_ERROR (Status)) {\r
-    if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r
-      FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
-    }\r
-    FreePool (mVariableModuleGlobal);\r
-    FreePool (VolatileVariableStore);\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
-\r
-  @param[in]  Address       The Flash address.\r
-  @param[out] FvbHandle     In output, if it is not NULL, it points to the proper FVB handle.\r
-  @param[out] FvbProtocol   In output, if it is not NULL, it points to the proper FVB protocol.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbInfoByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS                Address,\r
-  OUT EFI_HANDLE                          *FvbHandle OPTIONAL,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvbProtocol OPTIONAL\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_HANDLE                              *HandleBuffer;\r
-  UINTN                                   HandleCount;\r
-  UINTN                                   Index;\r
-  EFI_PHYSICAL_ADDRESS                    FvbBaseAddress;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *Fvb;\r
-  EFI_FVB_ATTRIBUTES_2                    Attributes;\r
-  UINTN                                   BlockSize;\r
-  UINTN                                   NumberOfBlocks;\r
-\r
-  HandleBuffer = NULL;\r
-  //\r
-  // Get all FVB handles.\r
-  //\r
-  Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  //\r
-  // Get the FVB to access variable store.\r
-  //\r
-  Fvb = NULL;\r
-  for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
-    Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r
-    if (EFI_ERROR (Status)) {\r
-      Status = EFI_NOT_FOUND;\r
-      break;\r
-    }\r
-\r
-    //\r
-    // Ensure this FVB protocol supported Write operation.\r
-    //\r
-    Status = Fvb->GetAttributes (Fvb, &Attributes);\r
-    if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
-      continue;\r
-    }\r
-\r
-    //\r
-    // Compare the address and select the right one.\r
-    //\r
-    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    //\r
-    // Assume one FVB has one type of BlockSize.\r
-    //\r
-    Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r
-      if (FvbHandle != NULL) {\r
-        *FvbHandle  = HandleBuffer[Index];\r
-      }\r
-      if (FvbProtocol != NULL) {\r
-        *FvbProtocol = Fvb;\r
-      }\r
-      Status = EFI_SUCCESS;\r
-      break;\r
-    }\r
-  }\r
-  FreePool (HandleBuffer);\r
-\r
-  if (Fvb == NULL) {\r
-    Status = EFI_NOT_FOUND;\r
-  }\r
-\r
-  return Status;\r
-}\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.h
deleted file mode 100644 (file)
index 20f60d4..0000000
+++ /dev/null
@@ -1,836 +0,0 @@
-/** @file\r
-  The internal header file includes the common header files, defines\r
-  internal structure and functions used by Variable modules.\r
-\r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#ifndef _VARIABLE_H_\r
-#define _VARIABLE_H_\r
-\r
-#include <PiDxe.h>\r
-#include <Protocol/VariableWrite.h>\r
-#include <Protocol/FaultTolerantWrite.h>\r
-#include <Protocol/FirmwareVolumeBlock.h>\r
-#include <Protocol/Variable.h>\r
-#include <Protocol/VariableLock.h>\r
-#include <Protocol/VarCheck.h>\r
-#include <Library/PcdLib.h>\r
-#include <Library/HobLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/DxeServicesTableLib.h>\r
-#include <Library/UefiRuntimeLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/UefiLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/SynchronizationLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/BaseCryptLib.h>\r
-#include <Library/PlatformSecureLib.h>\r
-#include <Guid/GlobalVariable.h>\r
-#include <Guid/EventGroup.h>\r
-#include <Guid/AuthenticatedVariableFormat.h>\r
-#include <Guid/ImageAuthentication.h>\r
-#include <Guid/SystemNvDataGuid.h>\r
-#include <Guid/FaultTolerantWrite.h>\r
-#include <Guid/HardwareErrorVariable.h>\r
-#include <Guid/VarErrorFlag.h>\r
-\r
-#define EFI_VARIABLE_ATTRIBUTES_MASK (EFI_VARIABLE_NON_VOLATILE | \\r
-                                      EFI_VARIABLE_BOOTSERVICE_ACCESS | \\r
-                                      EFI_VARIABLE_RUNTIME_ACCESS | \\r
-                                      EFI_VARIABLE_HARDWARE_ERROR_RECORD | \\r
-                                      EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \\r
-                                      EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \\r
-                                      EFI_VARIABLE_APPEND_WRITE)\r
-\r
-#define VARIABLE_ATTRIBUTE_NV_BS        (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_BS_RT        (EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT     (VARIABLE_ATTRIBUTE_BS_RT | EFI_VARIABLE_NON_VOLATILE)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_AT  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_HR  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_HARDWARE_ERROR_RECORD)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_AW  (VARIABLE_ATTRIBUTE_NV_BS_RT | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_NV_BS_RT_AT_HR_AW  (VARIABLE_ATTRIBUTE_NV_BS_RT_AT | EFI_VARIABLE_HARDWARE_ERROR_RECORD | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-#define VARIABLE_ATTRIBUTE_AT_AW        (EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r
-\r
-#define MAX_NV_VARIABLE_SIZE (MAX (MAX (PcdGet32 (PcdMaxVariableSize), \\r
-                                        PcdGet32 (PcdMaxAuthVariableSize)), \\r
-                                   PcdGet32 (PcdMaxHardwareErrorVariableSize)))\r
-\r
-///\r
-/// The size of a 3 character ISO639 language code.\r
-///\r
-#define ISO_639_2_ENTRY_SIZE    3\r
-\r
-typedef enum {\r
-  VariableStoreTypeVolatile,\r
-  VariableStoreTypeHob,\r
-  VariableStoreTypeNv,\r
-  VariableStoreTypeMax\r
-} VARIABLE_STORE_TYPE;\r
-\r
-typedef struct {\r
-  VARIABLE_HEADER *CurrPtr;\r
-  //\r
-  // If both ADDED and IN_DELETED_TRANSITION variable are present,\r
-  // InDeletedTransitionPtr will point to the IN_DELETED_TRANSITION one.\r
-  // Otherwise, CurrPtr will point to the ADDED or IN_DELETED_TRANSITION one,\r
-  // and InDeletedTransitionPtr will be NULL at the same time.\r
-  //\r
-  VARIABLE_HEADER *InDeletedTransitionPtr;\r
-  VARIABLE_HEADER *EndPtr;\r
-  VARIABLE_HEADER *StartPtr;\r
-  BOOLEAN         Volatile;\r
-} VARIABLE_POINTER_TRACK;\r
-\r
-typedef struct {\r
-  EFI_PHYSICAL_ADDRESS  HobVariableBase;\r
-  EFI_PHYSICAL_ADDRESS  VolatileVariableBase;\r
-  EFI_PHYSICAL_ADDRESS  NonVolatileVariableBase;\r
-  EFI_LOCK              VariableServicesLock;\r
-  UINT32                ReentrantState;\r
-} VARIABLE_GLOBAL;\r
-\r
-typedef struct {\r
-  VARIABLE_GLOBAL VariableGlobal;\r
-  UINTN           VolatileLastVariableOffset;\r
-  UINTN           NonVolatileLastVariableOffset;\r
-  UINTN           CommonVariableSpace;\r
-  UINTN           CommonMaxUserVariableSpace;\r
-  UINTN           CommonRuntimeVariableSpace;\r
-  UINTN           CommonVariableTotalSize;\r
-  UINTN           CommonUserVariableTotalSize;\r
-  UINTN           HwErrVariableTotalSize;\r
-  UINTN           MaxVariableSize;\r
-  UINTN           MaxAuthVariableSize;\r
-  UINTN           ScratchBufferSize;\r
-  CHAR8           *PlatformLangCodes;\r
-  CHAR8           *LangCodes;\r
-  CHAR8           *PlatformLang;\r
-  CHAR8           Lang[ISO_639_2_ENTRY_SIZE + 1];\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbInstance;\r
-} VARIABLE_MODULE_GLOBAL;\r
-\r
-typedef struct {\r
-  LIST_ENTRY  Link;\r
-  EFI_GUID    Guid;\r
-  //CHAR16      *Name;\r
-} VARIABLE_ENTRY;\r
-\r
-/**\r
-  Flush the HOB variable to flash.\r
-\r
-  @param[in] VariableName       Name of variable has been updated or deleted.\r
-  @param[in] VendorGuid         Guid of variable has been updated or deleted.\r
-\r
-**/\r
-VOID\r
-FlushHobVariableToFlash (\r
-  IN CHAR16                     *VariableName,\r
-  IN EFI_GUID                   *VendorGuid\r
-  );\r
-\r
-/**\r
-  Writes a buffer to variable storage space, in the working block.\r
-\r
-  This function writes a buffer to variable storage space into a firmware\r
-  volume block device. The destination is specified by the parameter\r
-  VariableBase. Fault Tolerant Write protocol is used for writing.\r
-\r
-  @param  VariableBase   Base address of the variable to write.\r
-  @param  VariableBuffer Point to the variable data buffer.\r
-\r
-  @retval EFI_SUCCESS    The function completed successfully.\r
-  @retval EFI_NOT_FOUND  Fail to locate Fault Tolerant Write protocol.\r
-  @retval EFI_ABORTED    The function could not complete successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-FtwVariableSpace (\r
-  IN EFI_PHYSICAL_ADDRESS   VariableBase,\r
-  IN VARIABLE_STORE_HEADER  *VariableBuffer\r
-  );\r
-\r
-/**\r
-  Finds variable in storage blocks of volatile and non-volatile storage areas.\r
-\r
-  This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
-  If VariableName is an empty string, then we just return the first\r
-  qualified variable without comparing VariableName and VendorGuid.\r
-  If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r
-  at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r
-  Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r
-\r
-  @param[in]   VariableName           Name of the variable to be found.\r
-  @param[in]   VendorGuid             Vendor GUID to be found.\r
-  @param[out]  PtrTrack               VARIABLE_POINTER_TRACK structure for output,\r
-                                      including the range searched and the target position.\r
-  @param[in]   Global                 Pointer to VARIABLE_GLOBAL structure, including\r
-                                      base of volatile variable storage area, base of\r
-                                      NV variable storage area, and a lock.\r
-  @param[in]   IgnoreRtCheck          Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r
-                                      check at runtime when searching variable.\r
-\r
-  @retval EFI_INVALID_PARAMETER       If VariableName is not an empty string, while\r
-                                      VendorGuid is NULL.\r
-  @retval EFI_SUCCESS                 Variable successfully found.\r
-  @retval EFI_NOT_FOUND               Variable not found\r
-\r
-**/\r
-EFI_STATUS\r
-FindVariable (\r
-  IN  CHAR16                  *VariableName,\r
-  IN  EFI_GUID                *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack,\r
-  IN  VARIABLE_GLOBAL         *Global,\r
-  IN  BOOLEAN                 IgnoreRtCheck\r
-  );\r
-\r
-/**\r
-\r
-  Gets the pointer to the end of the variable storage area.\r
-\r
-  This function gets pointer to the end of the variable storage\r
-  area, according to the input variable store header.\r
-\r
-  @param VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-  @return Pointer to the end of the variable storage area.\r
-\r
-**/\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  );\r
-\r
-/**\r
-\r
-  This code gets the pointer to the variable data.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Pointer to Variable Data.\r
-\r
-**/\r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  );\r
-\r
-/**\r
-\r
-  This code gets the size of variable data.\r
-\r
-  @param Variable        Pointer to the Variable Header.\r
-\r
-  @return Size of variable in bytes.\r
-\r
-**/\r
-UINTN\r
-DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  );\r
-\r
-/**\r
-  This function is to check if the remaining variable space is enough to set\r
-  all Variables from argument list successfully. The purpose of the check\r
-  is to keep the consistency of the Variables to be in variable storage.\r
-\r
-  Note: Variables are assumed to be in same storage.\r
-  The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
-  so follow the argument sequence to check the Variables.\r
-\r
-  @param[in] Attributes         Variable attributes for Variable entries.\r
-  @param ...                    The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
-                                A NULL terminates the list. The VariableSize of \r
-                                VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
-                                It will be changed to variable total size as output.\r
-\r
-  @retval TRUE                  Have enough variable space to set the Variables successfully.\r
-  @retval FALSE                 No enough variable space to set the Variables successfully.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-CheckRemainingSpaceForConsistency (\r
-  IN UINT32                     Attributes,\r
-  ...\r
-  );\r
-  \r
-/**\r
-  Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r
-  index of associated public key is needed.\r
-\r
-  @param[in] VariableName       Name of variable.\r
-  @param[in] VendorGuid         Guid of variable.\r
-  @param[in] Data               Variable data.\r
-  @param[in] DataSize           Size of data. 0 means delete.\r
-  @param[in] Attributes         Attributes of the variable.\r
-  @param[in] KeyIndex           Index of associated public key.\r
-  @param[in] MonotonicCount     Value of associated monotonic count.\r
-  @param[in, out] Variable      The variable information that is used to keep track of variable usage.\r
-\r
-  @param[in] TimeStamp          Value of associated TimeStamp.\r
-\r
-  @retval EFI_SUCCESS           The update operation is success.\r
-  @retval EFI_OUT_OF_RESOURCES  Variable region is full, cannot write other data into this region.\r
-\r
-**/\r
-EFI_STATUS\r
-UpdateVariable (\r
-  IN      CHAR16          *VariableName,\r
-  IN      EFI_GUID        *VendorGuid,\r
-  IN      VOID            *Data,\r
-  IN      UINTN           DataSize,\r
-  IN      UINT32          Attributes OPTIONAL,\r
-  IN      UINT32          KeyIndex  OPTIONAL,\r
-  IN      UINT64          MonotonicCount  OPTIONAL,\r
-  IN OUT  VARIABLE_POINTER_TRACK *Variable,\r
-  IN      EFI_TIME        *TimeStamp  OPTIONAL  \r
-  );\r
-\r
-\r
-/**\r
-  Return TRUE if ExitBootServices () has been called.\r
-  \r
-  @retval TRUE If ExitBootServices () has been called.\r
-**/\r
-BOOLEAN\r
-AtRuntime (\r
-  VOID\r
-  );\r
-\r
-/**\r
-  Initializes a basic mutual exclusion lock.\r
-\r
-  This function initializes a basic mutual exclusion lock to the released state \r
-  and returns the lock.  Each lock provides mutual exclusion access at its task \r
-  priority level.  Since there is no preemption or multiprocessor support in EFI,\r
-  acquiring the lock only consists of raising to the locks TPL.\r
-  If Lock is NULL, then ASSERT().\r
-  If Priority is not a valid TPL value, then ASSERT().\r
-\r
-  @param  Lock       A pointer to the lock data structure to initialize.\r
-  @param  Priority   EFI TPL is associated with the lock.\r
-\r
-  @return The lock.\r
-\r
-**/\r
-EFI_LOCK *\r
-InitializeLock (\r
-  IN OUT EFI_LOCK  *Lock,\r
-  IN EFI_TPL        Priority\r
-  );\r
-\r
-  \r
-/**\r
-  Acquires lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function that will be removed when\r
-  EfiAcquireLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiAcquireLock() at boot time, and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to acquire.\r
-\r
-**/\r
-VOID\r
-AcquireLockOnlyAtBootTime (\r
-  IN EFI_LOCK  *Lock\r
-  );\r
-\r
-\r
-/**\r
-  Releases lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function which will be removed when\r
-  EfiReleaseLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiReleaseLock() at boot time and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to release.\r
-\r
-**/\r
-VOID\r
-ReleaseLockOnlyAtBootTime (\r
-  IN EFI_LOCK  *Lock\r
-  );  \r
-\r
-/**\r
-  Retrive the FVB protocol interface by HANDLE.\r
-\r
-  @param[in]  FvBlockHandle     The handle of FVB protocol that provides services for\r
-                                reading, writing, and erasing the target block.\r
-  @param[out] FvBlock           The interface of FVB protocol\r
-\r
-  @retval EFI_SUCCESS           The interface information for the specified protocol was returned.\r
-  @retval EFI_UNSUPPORTED       The device does not support the FVB protocol.\r
-  @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
-  \r
-**/\r
-EFI_STATUS\r
-GetFvbByHandle (\r
-  IN  EFI_HANDLE                          FvBlockHandle,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
-  );\r
-\r
-\r
-/**\r
-  Retrive the Swap Address Range protocol interface.\r
-\r
-  @param[out] SarProtocol       The interface of SAR protocol\r
-\r
-  @retval EFI_SUCCESS           The SAR protocol instance was found and returned in SarProtocol.\r
-  @retval EFI_NOT_FOUND         The SAR protocol instance was not found.\r
-  @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetSarProtocol (\r
-  OUT VOID                                **SarProtocol\r
-  );\r
-\r
-/**\r
-  Function returns an array of handles that support the FVB protocol\r
-  in a buffer allocated from pool. \r
-\r
-  @param[out]  NumberHandles    The number of handles returned in Buffer.\r
-  @param[out]  Buffer           A pointer to the buffer to return the requested\r
-                                array of  handles that support FVB protocol.\r
-\r
-  @retval EFI_SUCCESS           The array of handles was returned in Buffer, and the number of\r
-                                handles in Buffer was returned in NumberHandles.\r
-  @retval EFI_NOT_FOUND         No FVB handle was found.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough pool memory to store the matching results.\r
-  @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
-  \r
-**/\r
-EFI_STATUS\r
-GetFvbCountAndBuffer (\r
-  OUT UINTN                               *NumberHandles,\r
-  OUT EFI_HANDLE                          **Buffer\r
-  );\r
-\r
-/**\r
-  Initializes variable store area for non-volatile and volatile variable.\r
-\r
-  @retval EFI_SUCCESS           Function successfully executed.\r
-  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resource.\r
-\r
-**/\r
-EFI_STATUS\r
-VariableCommonInitialize (\r
-  VOID\r
-  );\r
-\r
-/**\r
-\r
-  Variable store garbage collection and reclaim operation.\r
-\r
-  If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles.\r
-  If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex\r
-  for all the count-based authenticate variable in NV storage.\r
-\r
-  @param[in]      VariableBase            Base address of variable store.\r
-  @param[out]     LastVariableOffset      Offset of last variable.\r
-  @param[in]      IsVolatile              The variable store is volatile or not;\r
-                                          if it is non-volatile, need FTW.\r
-  @param[in, out] UpdatingPtrTrack        Pointer to updating variable pointer track structure.\r
-  @param[in]      NewVariable             Pointer to new variable.\r
-  @param[in]      NewVariableSize         New variable size.\r
-  @param[in]      ReclaimPubKeyStore      Reclaim for public key database or not.\r
-  \r
-  @return EFI_SUCCESS                  Reclaim operation has finished successfully.\r
-  @return EFI_OUT_OF_RESOURCES         No enough memory resources or variable space.\r
-  @return EFI_DEVICE_ERROR             The public key database doesn't exist.\r
-  @return Others                       Unexpect error happened during reclaim operation.\r
-\r
-**/\r
-EFI_STATUS\r
-Reclaim (\r
-  IN     EFI_PHYSICAL_ADDRESS         VariableBase,\r
-  OUT    UINTN                        *LastVariableOffset,\r
-  IN     BOOLEAN                      IsVolatile,\r
-  IN OUT VARIABLE_POINTER_TRACK       *UpdatingPtrTrack,\r
-  IN     VARIABLE_HEADER              *NewVariable,\r
-  IN     UINTN                        NewVariableSize,\r
-  IN     BOOLEAN                      ReclaimPubKeyStore\r
-  );\r
-\r
-/**\r
-  This function reclaims variable storage if free size is below the threshold.\r
-  \r
-**/\r
-VOID\r
-ReclaimForOS(\r
-  VOID\r
-  );  \r
-\r
-\r
-/**\r
-  Initializes variable write service after FVB was ready.\r
-\r
-  @retval EFI_SUCCESS          Function successfully executed.\r
-  @retval Others               Fail to initialize the variable service.\r
-\r
-**/\r
-EFI_STATUS\r
-VariableWriteServiceInitialize (\r
-  VOID\r
-  );\r
-  \r
-/**\r
-  Retrive the SMM Fault Tolerent Write protocol interface.\r
-\r
-  @param[out] FtwProtocol       The interface of SMM Ftw protocol\r
-\r
-  @retval EFI_SUCCESS           The SMM SAR protocol instance was found and returned in SarProtocol.\r
-  @retval EFI_NOT_FOUND         The SMM SAR protocol instance was not found.\r
-  @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFtwProtocol (\r
-  OUT VOID                                **FtwProtocol\r
-  );\r
-\r
-/**\r
-  Get the proper fvb handle and/or fvb protocol by the given Flash address.\r
-\r
-  @param[in] Address        The Flash address.\r
-  @param[out] FvbHandle     In output, if it is not NULL, it points to the proper FVB handle.\r
-  @param[out] FvbProtocol   In output, if it is not NULL, it points to the proper FVB protocol.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbInfoByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS                Address,\r
-  OUT EFI_HANDLE                          *FvbHandle OPTIONAL,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvbProtocol OPTIONAL\r
-  );\r
-\r
-/**\r
-\r
-  This code finds variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-\r
-  @param VariableName               Name of Variable to be found.\r
-  @param VendorGuid                 Variable vendor GUID.\r
-  @param Attributes                 Attribute value of the variable found.\r
-  @param DataSize                   Size of Data found. If size is less than the\r
-                                    data, this value contains the required size.\r
-  @param Data                       Data pointer.\r
-                      \r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetVariable (\r
-  IN      CHAR16            *VariableName,\r
-  IN      EFI_GUID          *VendorGuid,\r
-  OUT     UINT32            *Attributes OPTIONAL,\r
-  IN OUT  UINTN             *DataSize,\r
-  OUT     VOID              *Data\r
-  );\r
-\r
-/**\r
-\r
-  This code Finds the Next available variable.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param VariableNameSize           Size of the variable name.\r
-  @param VariableName               Pointer to variable name.\r
-  @param VendorGuid                 Variable Vendor Guid.\r
-\r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceGetNextVariableName (\r
-  IN OUT  UINTN             *VariableNameSize,\r
-  IN OUT  CHAR16            *VariableName,\r
-  IN OUT  EFI_GUID          *VendorGuid\r
-  );\r
-\r
-/**\r
-\r
-  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode, and datasize and data are external input.\r
-  This function will do basic validation, before parse the data.\r
-  This function will parse the authentication carefully to avoid security issues, like\r
-  buffer overflow, integer overflow.\r
-  This function will check attribute carefully to avoid authentication bypass.\r
-\r
-  @param VariableName                     Name of Variable to be found.\r
-  @param VendorGuid                       Variable vendor GUID.\r
-  @param Attributes                       Attribute value of the variable found\r
-  @param DataSize                         Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param Data                             Data pointer.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SUCCESS                     Set successfully.\r
-  @return EFI_OUT_OF_RESOURCES            Resource not enough to set variable.\r
-  @return EFI_NOT_FOUND                   Not found.\r
-  @return EFI_WRITE_PROTECTED             Variable is read-only.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceSetVariable (\r
-  IN CHAR16                  *VariableName,\r
-  IN EFI_GUID                *VendorGuid,\r
-  IN UINT32                  Attributes,\r
-  IN UINTN                   DataSize,\r
-  IN VOID                    *Data\r
-  );\r
-\r
-/**\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param Attributes                     Attributes bitmask to specify the type of variables\r
-                                        on which to return information.\r
-  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available\r
-                                        for the EFI variables associated with the attributes specified.\r
-  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available\r
-                                        for EFI variables associated with the attributes specified.\r
-  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables\r
-                                        associated with the attributes specified.\r
-\r
-  @return EFI_SUCCESS                   Query successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceQueryVariableInfoInternal (\r
-  IN  UINT32                 Attributes,\r
-  OUT UINT64                 *MaximumVariableStorageSize,\r
-  OUT UINT64                 *RemainingVariableStorageSize,\r
-  OUT UINT64                 *MaximumVariableSize\r
-  );\r
-\r
-/**\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
-\r
-  @param Attributes                     Attributes bitmask to specify the type of variables\r
-                                        on which to return information.\r
-  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available\r
-                                        for the EFI variables associated with the attributes specified.\r
-  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available\r
-                                        for EFI variables associated with the attributes specified.\r
-  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables\r
-                                        associated with the attributes specified.\r
-\r
-  @return EFI_INVALID_PARAMETER         An invalid combination of attribute bits was supplied.\r
-  @return EFI_SUCCESS                   Query successfully.\r
-  @return EFI_UNSUPPORTED               The attribute is not supported on this platform.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceQueryVariableInfo (\r
-  IN  UINT32                 Attributes,\r
-  OUT UINT64                 *MaximumVariableStorageSize,\r
-  OUT UINT64                 *RemainingVariableStorageSize,\r
-  OUT UINT64                 *MaximumVariableSize\r
-  );  \r
-\r
-/**\r
-  Mark a variable that will become read-only after leaving the DXE phase of execution.\r
-\r
-  @param[in] This          The VARIABLE_LOCK_PROTOCOL instance.\r
-  @param[in] VariableName  A pointer to the variable name that will be made read-only subsequently.\r
-  @param[in] VendorGuid    A pointer to the vendor GUID that will be made read-only subsequently.\r
-\r
-  @retval EFI_SUCCESS           The variable specified by the VariableName and the VendorGuid was marked\r
-                                as pending to be read-only.\r
-  @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
-                                Or VariableName is an empty string.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource to hold the lock request.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableLockRequestToLock (\r
-  IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
-  IN       CHAR16                       *VariableName,\r
-  IN       EFI_GUID                     *VendorGuid\r
-  );\r
-\r
-/**\r
-  Check if a Unicode character is a hexadecimal character.\r
-\r
-  This function checks if a Unicode character is a\r
-  hexadecimal character.  The valid hexadecimal character is\r
-  L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
-\r
-\r
-  @param Char           The character to check against.\r
-\r
-  @retval TRUE          If the Char is a hexadecmial character.\r
-  @retval FALSE         If the Char is not a hexadecmial character.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsHexaDecimalDigitCharacter (\r
-  IN CHAR16             Char\r
-  );\r
-\r
-/**\r
-  Internal SetVariable check.\r
-\r
-  @param[in] VariableName       Name of Variable to set.\r
-  @param[in] VendorGuid         Variable vendor GUID.\r
-  @param[in] Attributes         Attribute value of the variable.\r
-  @param[in] DataSize           Size of Data to set.\r
-  @param[in] Data               Data pointer.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check result was success.\r
-  @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID were supplied,\r
-                                or the DataSize exceeds the minimum or maximum allowed,\r
-                                or the Data value is not following UEFI spec for UEFI defined variables.\r
-  @retval EFI_WRITE_PROTECTED   The variable in question is read-only.\r
-  @retval Others                The return status from check handler.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckSetVariableCheck (\r
-  IN CHAR16     *VariableName,\r
-  IN EFI_GUID   *VendorGuid,\r
-  IN UINT32     Attributes,\r
-  IN UINTN      DataSize,\r
-  IN VOID       *Data\r
-  );\r
-\r
-/**\r
-  Register SetVariable check handler.\r
-\r
-  @param[in] Handler            Pointer to check handler.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check handler was registered successfully.\r
-  @retval EFI_INVALID_PARAMETER Handler is NULL.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the SetVariable check handler register request.\r
-  @retval EFI_UNSUPPORTED       This interface is not implemented.\r
-                                For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckRegisterSetVariableCheckHandler (\r
-  IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER   Handler\r
-  );\r
-\r
-/**\r
-  Internal variable property get.\r
-\r
-  @param[in]  Name              Pointer to the variable name.\r
-  @param[in]  Guid              Pointer to the vendor GUID.\r
-  @param[out] VariableProperty  Pointer to the output variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was got successfully.\r
-  @retval EFI_NOT_FOUND         The property of variable specified by the Name and Guid was not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InternalVarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
-  );\r
-\r
-/**\r
-  Variable property set.\r
-\r
-  @param[in] Name               Pointer to the variable name.\r
-  @param[in] Guid               Pointer to the vendor GUID.\r
-  @param[in] VariableProperty   Pointer to the input variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was set successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r
-                                or the fields of VariableProperty are not valid.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the variable property set request.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertySet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariableProperty\r
-  );\r
-\r
-/**\r
-  Variable property get.\r
-\r
-  @param[in]  Name              Pointer to the variable name.\r
-  @param[in]  Guid              Pointer to the vendor GUID.\r
-  @param[out] VariableProperty  Pointer to the output variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was got successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r
-  @retval EFI_NOT_FOUND         The property of variable specified by the Name and Guid was not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
-  );\r
-\r
-/**\r
-  Initialize variable quota.\r
-\r
-**/\r
-VOID\r
-InitializeVariableQuota (\r
-  VOID\r
-  );\r
-\r
-extern VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;\r
-\r
-#endif\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthRuntimeDxe.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthRuntimeDxe.uni
deleted file mode 100644 (file)
index 47a09d5..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthRuntimeDxe.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmm.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmm.uni
deleted file mode 100644 (file)
index 0de43d0..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmm.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmmRuntimeDxe.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmmRuntimeDxe.uni
deleted file mode 100644 (file)
index db931c4..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableAuthSmmRuntimeDxe.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c
deleted file mode 100644 (file)
index f5bb996..0000000
+++ /dev/null
@@ -1,530 +0,0 @@
-/** @file\r
-  Implement all four UEFI Runtime Variable services for the nonvolatile\r
-  and volatile storage space and install variable architecture protocol.\r
-\r
-Copyright (C) 2013, Red Hat, Inc.\r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-#include "AuthService.h"\r
-\r
-extern VARIABLE_STORE_HEADER   *mNvVariableCache;\r
-extern VARIABLE_INFO_ENTRY     *gVariableInfo;\r
-EFI_HANDLE                     mHandle                    = NULL;\r
-EFI_EVENT                      mVirtualAddressChangeEvent = NULL;\r
-EFI_EVENT                      mFtwRegistration           = NULL;\r
-extern LIST_ENTRY              mLockedVariableList;\r
-extern LIST_ENTRY              mVarCheckVariableList;\r
-extern UINT32                  mNumberOfHandler;\r
-extern VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *mHandlerTable;\r
-extern BOOLEAN                 mEndOfDxe;\r
-EDKII_VARIABLE_LOCK_PROTOCOL   mVariableLock              = { VariableLockRequestToLock };\r
-EDKII_VAR_CHECK_PROTOCOL       mVarCheck                  = { VarCheckRegisterSetVariableCheckHandler,\r
-                                                              VarCheckVariablePropertySet,\r
-                                                              VarCheckVariablePropertyGet };\r
-\r
-/**\r
-  Return TRUE if ExitBootServices () has been called.\r
-\r
-  @retval TRUE If ExitBootServices () has been called.\r
-**/\r
-BOOLEAN\r
-AtRuntime (\r
-  VOID\r
-  )\r
-{\r
-  return EfiAtRuntime ();\r
-}\r
-\r
-\r
-/**\r
-  Initializes a basic mutual exclusion lock.\r
-\r
-  This function initializes a basic mutual exclusion lock to the released state\r
-  and returns the lock.  Each lock provides mutual exclusion access at its task\r
-  priority level.  Since there is no preemption or multiprocessor support in EFI,\r
-  acquiring the lock only consists of raising to the locks TPL.\r
-  If Lock is NULL, then ASSERT().\r
-  If Priority is not a valid TPL value, then ASSERT().\r
-\r
-  @param  Lock       A pointer to the lock data structure to initialize.\r
-  @param  Priority   EFI TPL is associated with the lock.\r
-\r
-  @return The lock.\r
-\r
-**/\r
-EFI_LOCK *\r
-InitializeLock (\r
-  IN OUT EFI_LOCK                         *Lock,\r
-  IN     EFI_TPL                          Priority\r
-  )\r
-{\r
-  return EfiInitializeLock (Lock, Priority);\r
-}\r
-\r
-\r
-/**\r
-  Acquires lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function that will be removed when\r
-  EfiAcquireLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiAcquireLock() at boot time, and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to acquire.\r
-\r
-**/\r
-VOID\r
-AcquireLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-  if (!AtRuntime ()) {\r
-    EfiAcquireLock (Lock);\r
-  }\r
-}\r
-\r
-\r
-/**\r
-  Releases lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function which will be removed when\r
-  EfiReleaseLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiReleaseLock() at boot time and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to release.\r
-\r
-**/\r
-VOID\r
-ReleaseLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-  if (!AtRuntime ()) {\r
-    EfiReleaseLock (Lock);\r
-  }\r
-}\r
-\r
-/**\r
-  Retrive the Fault Tolerent Write protocol interface.\r
-\r
-  @param[out] FtwProtocol       The interface of Ftw protocol\r
-\r
-  @retval EFI_SUCCESS           The FTW protocol instance was found and returned in FtwProtocol.\r
-  @retval EFI_NOT_FOUND         The FTW protocol instance was not found.\r
-  @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFtwProtocol (\r
-  OUT VOID                                **FtwProtocol\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-\r
-  //\r
-  // Locate Fault Tolerent Write protocol\r
-  //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiFaultTolerantWriteProtocolGuid,\r
-                  NULL,\r
-                  FtwProtocol\r
-                  );\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Retrive the FVB protocol interface by HANDLE.\r
-\r
-  @param[in]  FvBlockHandle     The handle of FVB protocol that provides services for\r
-                                reading, writing, and erasing the target block.\r
-  @param[out] FvBlock           The interface of FVB protocol\r
-\r
-  @retval EFI_SUCCESS           The interface information for the specified protocol was returned.\r
-  @retval EFI_UNSUPPORTED       The device does not support the FVB protocol.\r
-  @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbByHandle (\r
-  IN  EFI_HANDLE                          FvBlockHandle,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
-  )\r
-{\r
-  //\r
-  // To get the FVB protocol interface on the handle\r
-  //\r
-  return gBS->HandleProtocol (\r
-                FvBlockHandle,\r
-                &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                (VOID **) FvBlock\r
-                );\r
-}\r
-\r
-\r
-/**\r
-  Function returns an array of handles that support the FVB protocol\r
-  in a buffer allocated from pool.\r
-\r
-  @param[out]  NumberHandles    The number of handles returned in Buffer.\r
-  @param[out]  Buffer           A pointer to the buffer to return the requested\r
-                                array of  handles that support FVB protocol.\r
-\r
-  @retval EFI_SUCCESS           The array of handles was returned in Buffer, and the number of\r
-                                handles in Buffer was returned in NumberHandles.\r
-  @retval EFI_NOT_FOUND         No FVB handle was found.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough pool memory to store the matching results.\r
-  @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbCountAndBuffer (\r
-  OUT UINTN                               *NumberHandles,\r
-  OUT EFI_HANDLE                          **Buffer\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-\r
-  //\r
-  // Locate all handles of Fvb protocol\r
-  //\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  NULL,\r
-                  NumberHandles,\r
-                  Buffer\r
-                  );\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
-\r
-  This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
-  It convers pointer to new virtual address.\r
-\r
-  @param  Event        Event whose notification function is being invoked.\r
-  @param  Context      Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-VariableClassAddressChangeEvent (\r
-  IN EFI_EVENT                            Event,\r
-  IN VOID                                 *Context\r
-  )\r
-{\r
-  EFI_STATUS     Status;\r
-  UINTN          Index;\r
-\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
-  EfiConvertPointer (0x0, (VOID **) &mHashCtx);\r
-  EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);\r
-  EfiConvertPointer (0x0, (VOID **) &mPubKeyStore);\r
-  EfiConvertPointer (0x0, (VOID **) &mCertDbStore);\r
-  EfiConvertPointer (0x0, (VOID **) &mHandlerTable);\r
-  for (Index = 0; Index < mNumberOfHandler; Index++) {\r
-    EfiConvertPointer (0x0, (VOID **) &mHandlerTable[Index]);\r
-  }\r
-\r
-  Status = EfiConvertList (0x0, &mLockedVariableList);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = EfiConvertList (0x0, &mVarCheckVariableList);\r
-  ASSERT_EFI_ERROR (Status);\r
-}\r
-\r
-\r
-/**\r
-  Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
-\r
-  This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
-  When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
-  storage if free size is below the threshold.\r
-\r
-  @param  Event        Event whose notification function is being invoked.\r
-  @param  Context      Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-OnReadyToBoot (\r
-  EFI_EVENT                               Event,\r
-  VOID                                    *Context\r
-  )\r
-{\r
-  //\r
-  // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.\r
-  //\r
-  mEndOfDxe = TRUE;\r
-  //\r
-  // The initialization for variable quota.\r
-  //\r
-  InitializeVariableQuota ();\r
-  ReclaimForOS ();\r
-  if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
-    gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
-  }\r
-}\r
-\r
-/**\r
-  Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
-\r
-  This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
-\r
-  @param  Event        Event whose notification function is being invoked.\r
-  @param  Context      Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-OnEndOfDxe (\r
-  EFI_EVENT                               Event,\r
-  VOID                                    *Context\r
-  )\r
-{\r
-  mEndOfDxe = TRUE;\r
-  //\r
-  // The initialization for variable quota.\r
-  //\r
-  InitializeVariableQuota ();\r
-  if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
-    ReclaimForOS ();\r
-  }\r
-}\r
-\r
-/**\r
-  Fault Tolerant Write protocol notification event handler.\r
-\r
-  Non-Volatile variable write may needs FTW protocol to reclaim when\r
-  writting variable.\r
-\r
-  @param[in] Event    Event whose notification function is being invoked.\r
-  @param[in] Context  Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-FtwNotificationEvent (\r
-  IN  EFI_EVENT                           Event,\r
-  IN  VOID                                *Context\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FvbProtocol;\r
-  EFI_FAULT_TOLERANT_WRITE_PROTOCOL       *FtwProtocol;\r
-  EFI_PHYSICAL_ADDRESS                    NvStorageVariableBase;\r
-  EFI_GCD_MEMORY_SPACE_DESCRIPTOR         GcdDescriptor;\r
-  EFI_PHYSICAL_ADDRESS                    BaseAddress;\r
-  UINT64                                  Length;\r
-  EFI_PHYSICAL_ADDRESS                    VariableStoreBase;\r
-  UINT64                                  VariableStoreLength;\r
-  UINTN                                   FtwMaxBlockSize;\r
-\r
-  //\r
-  // Ensure FTW protocol is installed.\r
-  //\r
-  Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r
-  if (EFI_ERROR (Status)) {\r
-    return ;\r
-  }\r
-\r
-  Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
-  if (!EFI_ERROR (Status)) {\r
-    ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
-  }\r
-\r
-  //\r
-  // Find the proper FVB protocol for variable.\r
-  //\r
-  NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
-  if (NvStorageVariableBase == 0) {\r
-    NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
-  }\r
-  Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
-  if (EFI_ERROR (Status)) {\r
-    return ;\r
-  }\r
-  mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
-\r
-  //\r
-  // Mark the variable storage region of the FLASH as RUNTIME.\r
-  //\r
-  VariableStoreBase   = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength);\r
-  VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
-  BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
-  Length      = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
-  Length      = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
-\r
-  Status      = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
-  } else {\r
-    Status = gDS->SetMemorySpaceAttributes (\r
-                    BaseAddress,\r
-                    Length,\r
-                    GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
-    }\r
-  }\r
-\r
-  Status = VariableWriteServiceInitialize ();\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
-  }\r
-\r
-  //\r
-  // Install the Variable Write Architectural protocol.\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mHandle,\r
-                  &gEfiVariableWriteArchProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
-  //\r
-  gBS->CloseEvent (Event);\r
-\r
-}\r
-\r
-\r
-/**\r
-  Variable Driver main entry point. The Variable driver places the 4 EFI\r
-  runtime services in the EFI System Table and installs arch protocols\r
-  for variable read and write services being available. It also registers\r
-  a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
-\r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
-  @param[in] SystemTable    A pointer to the EFI System Table.\r
-\r
-  @retval EFI_SUCCESS       Variable service successfully initialized.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceInitialize (\r
-  IN EFI_HANDLE                         ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                   *SystemTable\r
-  )\r
-{\r
-  EFI_STATUS                            Status;\r
-  EFI_EVENT                             ReadyToBootEvent;\r
-  EFI_EVENT                             EndOfDxeEvent;\r
-\r
-  Status = VariableCommonInitialize ();\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mHandle,\r
-                  &gEdkiiVariableLockProtocolGuid,\r
-                  &mVariableLock,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mHandle,\r
-                  &gEdkiiVarCheckProtocolGuid,\r
-                  &mVarCheck,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  SystemTable->RuntimeServices->GetVariable         = VariableServiceGetVariable;\r
-  SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
-  SystemTable->RuntimeServices->SetVariable         = VariableServiceSetVariable;\r
-  SystemTable->RuntimeServices->QueryVariableInfo   = VariableServiceQueryVariableInfo;\r
-\r
-  //\r
-  // Now install the Variable Runtime Architectural protocol on a new handle.\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mHandle,\r
-                  &gEfiVariableArchProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Register FtwNotificationEvent () notify function.\r
-  //\r
-  EfiCreateProtocolNotifyEvent (\r
-    &gEfiFaultTolerantWriteProtocolGuid,\r
-    TPL_CALLBACK,\r
-    FtwNotificationEvent,\r
-    (VOID *)SystemTable,\r
-    &mFtwRegistration\r
-    );\r
-\r
-  Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  VariableClassAddressChangeEvent,\r
-                  NULL,\r
-                  &gEfiEventVirtualAddressChangeGuid,\r
-                  &mVirtualAddressChangeEvent\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Register the event handling function to reclaim variable for OS usage.\r
-  //\r
-  Status = EfiCreateEventReadyToBootEx (\r
-             TPL_NOTIFY,\r
-             OnReadyToBoot,\r
-             NULL,\r
-             &ReadyToBootEvent\r
-             );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Register the event handling function to set the End Of DXE flag.\r
-  //\r
-  Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  OnEndOfDxe,\r
-                  NULL,\r
-                  &gEfiEndOfDxeEventGroupGuid,\r
-                  &EndOfDxeEvent\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxe.inf
deleted file mode 100644 (file)
index 2b1b1c0..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-## @file\r
-#  Provides authenticated variable service\r
-#\r
-#  This module installs variable arch protocol and variable write arch protocol to provide \r
-#  variable services: SetVariable, GetVariable, GetNextVariableName and QueryVariableInfo.\r
-#\r
-#  Caution: This module requires additional review when modified.\r
-#  This driver will have external input - variable data.\r
-#  This external input must be validated carefully to avoid security issues such as \r
-#  buffer overflow or integer overflow.\r
-#\r
-# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-##\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = VariableAuthRuntimeDxe\r
-  MODULE_UNI_FILE                = VariableAuthRuntimeDxe.uni\r
-  FILE_GUID                      = 2226F30F-3D5B-402d-9936-A97184EB4516\r
-  MODULE_TYPE                    = DXE_RUNTIME_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  ENTRY_POINT                    = VariableServiceInitialize\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 EBC\r
-#\r
-#  VIRTUAL_ADDRESS_MAP_CALLBACK  =  VariableClassAddressChangeEvent\r
-#\r
-\r
-[Sources]\r
-  Reclaim.c\r
-  Variable.c\r
-  VariableDxe.c\r
-  Variable.h\r
-  AuthService.c\r
-  AuthService.h\r
-  Measurement.c\r
-  VarCheck.c\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  CryptoPkg/CryptoPkg.dec\r
-  SecurityPkg/SecurityPkg.dec\r
-\r
-[LibraryClasses]\r
-  MemoryAllocationLib\r
-  BaseLib\r
-  SynchronizationLib\r
-  UefiLib\r
-  UefiBootServicesTableLib\r
-  BaseMemoryLib\r
-  DebugLib\r
-  UefiRuntimeLib\r
-  DxeServicesTableLib\r
-  UefiDriverEntryPoint\r
-  PcdLib\r
-  BaseCryptLib\r
-  PlatformSecureLib\r
-  HobLib\r
-  TpmMeasurementLib\r
-  DevicePathLib\r
-\r
-[Protocols]\r
-  gEfiFirmwareVolumeBlockProtocolGuid           ## CONSUMES\r
-  ## CONSUMES\r
-  ## NOTIFY\r
-  gEfiFaultTolerantWriteProtocolGuid\r
-  gEfiVariableWriteArchProtocolGuid             ## PRODUCES\r
-  gEfiVariableArchProtocolGuid                  ## PRODUCES\r
-  gEdkiiVariableLockProtocolGuid                ## PRODUCES\r
-  gEdkiiVarCheckProtocolGuid                    ## PRODUCES\r
-\r
-\r
-[Guids]\r
-  ## PRODUCES             ## GUID # Variable store header\r
-  ## CONSUMES             ## GUID # Variable store header\r
-  ## SOMETIMES_CONSUMES   ## HOB\r
-  ## SOMETIMES_PRODUCES   ## SystemTable\r
-  gEfiAuthenticatedVariableGuid\r
-  \r
-  ## SOMETIMES_CONSUMES   ## Variable:L"PlatformLang"\r
-  ## SOMETIMES_PRODUCES   ## Variable:L"PlatformLang"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"Lang"\r
-  ## SOMETIMES_PRODUCES   ## Variable:L"Lang"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"HwErrRecSupport"\r
-  ## CONSUMES             ## Variable:L"SetupMode"\r
-  ## PRODUCES             ## Variable:L"SetupMode"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"PK"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"KEK"\r
-  ## CONSUMES             ## Variable:L"SecureBoot"\r
-  ## PRODUCES             ## Variable:L"SecureBoot"\r
-  ## CONSUMES             ## Variable:L"SignatureSupport"\r
-  ## PRODUCES             ## Variable:L"SignatureSupport"  \r
-  ## PRODUCES             ## Variable:L"VendorKeys"\r
-  gEfiGlobalVariableGuid\r
-  \r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DB"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DBX"\r
-  gEfiImageSecurityDatabaseGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"SecureBootEnable"\r
-  ## PRODUCES             ## Variable:L"SecureBootEnable"\r
-  gEfiSecureBootEnableDisableGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"CustomMode"\r
-  ## PRODUCES             ## Variable:L"CustomMode"\r
-  gEfiCustomModeEnableGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"certdb"\r
-  ## PRODUCES             ## Variable:L"certdb"\r
-  gEfiCertDbGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"VendorKeysNv"\r
-  ## PRODUCES             ## Variable:L"VendorKeysNv"\r
-  gEfiVendorKeysNvGuid\r
-\r
-  gEfiEndOfDxeEventGroupGuid               ## CONSUMES               ## Event\r
-  gEfiEventVirtualAddressChangeGuid        ## CONSUMES               ## Event\r
-  gEfiCertTypeRsa2048Sha256Guid            ## SOMETIMES_CONSUMES     ## GUID     # Unique ID for the type of the certificate.\r
-  gEfiCertPkcs7Guid                        ## SOMETIMES_CONSUMES     ## GUID     # Unique ID for the type of the certificate.\r
-  gEfiCertX509Guid                         ## SOMETIMES_CONSUMES     ## GUID     # Unique ID for the type of the signature.\r
-  gEfiSystemNvDataFvGuid                   ## CONSUMES               ## GUID\r
-  gEfiHardwareErrorVariableGuid            ## SOMETIMES_CONSUMES     ## Variable:L"HwErrRec####"\r
-  gEdkiiFaultTolerantWriteGuid             ## SOMETIMES_CONSUMES     ## HOB\r
-  gEdkiiVarErrorFlagGuid                   ## CONSUMES               ## GUID\r
-\r
-[Pcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize       ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase       ## SOMETIMES_CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64     ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                  ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize              ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize     ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                 ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize           ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize  ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdReclaimVariableSpaceAtEndOfDxe   ## CONSUMES\r
-\r
-[FeaturePcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics        ## CONSUMES  # statistic the information of variable.\r
-  gEfiMdePkgTokenSpaceGuid.PcdUefiVariableDefaultLangDeprecate       ## CONSUMES  # Auto update PlatformLang/Lang\r
-\r
-[Depex]\r
-  TRUE\r
-\r
-[UserExtensions.TianoCore."ExtraFiles"]\r
-  VariableRuntimeDxeExtra.uni\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxeExtra.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxeExtra.uni
deleted file mode 100644 (file)
index 577f005..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableRuntimeDxeExtra.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
deleted file mode 100644 (file)
index 1eb169a..0000000
+++ /dev/null
@@ -1,988 +0,0 @@
-/** @file\r
-  The sample implementation for SMM variable protocol. And this driver \r
-  implements an SMI handler to communicate with the DXE runtime driver \r
-  to provide variable services.\r
-\r
-  Caution: This module requires additional review when modified.\r
-  This driver will have external input - variable data and communicate buffer in SMM mode.\r
-  This external input must be validated carefully to avoid security issue like\r
-  buffer overflow, integer overflow.\r
-\r
-  SmmVariableHandler() will receive untrusted input and do basic validation.\r
-\r
-  Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
-  VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
-  SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
-\r
-Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <Protocol/SmmVariable.h>\r
-#include <Protocol/SmmFirmwareVolumeBlock.h>\r
-#include <Protocol/SmmFaultTolerantWrite.h>\r
-#include <Protocol/SmmEndOfDxe.h>\r
-#include <Protocol/SmmVarCheck.h>\r
-\r
-#include <Library/SmmServicesTableLib.h>\r
-#include <Library/SmmMemLib.h>\r
-\r
-#include <Guid/AuthenticatedVariableFormat.h>\r
-#include <Guid/SmmVariableCommon.h>\r
-#include "Variable.h"\r
-\r
-extern VARIABLE_INFO_ENTRY                           *gVariableInfo;\r
-EFI_HANDLE                                           mSmmVariableHandle      = NULL;\r
-EFI_HANDLE                                           mVariableHandle         = NULL;\r
-BOOLEAN                                              mAtRuntime              = FALSE;\r
-EFI_GUID                                             mZeroGuid               = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
-UINT8                                                *mVariableBufferPayload = NULL;\r
-UINTN                                                mVariableBufferPayloadSize;\r
-extern BOOLEAN                                       mEndOfDxe;\r
-extern BOOLEAN                                       mEnableLocking;\r
-\r
-/**\r
-  SecureBoot Hook for SetVariable.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-SecureBootHook (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  )\r
-{\r
-  return ;\r
-}\r
-\r
-/**\r
-\r
-  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  @param VariableName                     Name of Variable to be found.\r
-  @param VendorGuid                       Variable vendor GUID.\r
-  @param Attributes                       Attribute value of the variable found\r
-  @param DataSize                         Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param Data                             Data pointer.\r
-\r
-  @return EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @return EFI_SUCCESS                     Set successfully.\r
-  @return EFI_OUT_OF_RESOURCES            Resource not enough to set variable.\r
-  @return EFI_NOT_FOUND                   Not found.\r
-  @return EFI_WRITE_PROTECTED             Variable is read-only.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-SmmVariableSetVariable (\r
-  IN CHAR16                  *VariableName,\r
-  IN EFI_GUID                *VendorGuid,\r
-  IN UINT32                  Attributes,\r
-  IN UINTN                   DataSize,\r
-  IN VOID                    *Data\r
-  )\r
-{\r
-  EFI_STATUS                 Status;\r
-\r
-  //\r
-  // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.\r
-  //\r
-  mEnableLocking = FALSE;\r
-  Status         = VariableServiceSetVariable (\r
-                     VariableName,\r
-                     VendorGuid,\r
-                     Attributes,\r
-                     DataSize,\r
-                     Data\r
-                     );\r
-  mEnableLocking = TRUE;\r
-  return Status;\r
-}\r
-\r
-EFI_SMM_VARIABLE_PROTOCOL      gSmmVariable = {\r
-  VariableServiceGetVariable,\r
-  VariableServiceGetNextVariableName,\r
-  SmmVariableSetVariable,\r
-  VariableServiceQueryVariableInfo\r
-};\r
-\r
-EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck = { VarCheckRegisterSetVariableCheckHandler,\r
-                                              VarCheckVariablePropertySet,\r
-                                              VarCheckVariablePropertyGet };\r
-\r
-/**\r
-  Return TRUE if ExitBootServices () has been called.\r
-  \r
-  @retval TRUE If ExitBootServices () has been called.\r
-**/\r
-BOOLEAN\r
-AtRuntime (\r
-  VOID\r
-  )\r
-{\r
-  return mAtRuntime;\r
-}\r
-\r
-/**\r
-  Initializes a basic mutual exclusion lock.\r
-\r
-  This function initializes a basic mutual exclusion lock to the released state \r
-  and returns the lock.  Each lock provides mutual exclusion access at its task \r
-  priority level.  Since there is no preemption or multiprocessor support in EFI,\r
-  acquiring the lock only consists of raising to the locks TPL.\r
-  If Lock is NULL, then ASSERT().\r
-  If Priority is not a valid TPL value, then ASSERT().\r
-\r
-  @param  Lock       A pointer to the lock data structure to initialize.\r
-  @param  Priority   EFI TPL is associated with the lock.\r
-\r
-  @return The lock.\r
-\r
-**/\r
-EFI_LOCK *\r
-InitializeLock (\r
-  IN OUT EFI_LOCK                         *Lock,\r
-  IN EFI_TPL                              Priority\r
-  )\r
-{\r
-  return Lock;\r
-}\r
-\r
-/**\r
-  Acquires lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function that will be removed when\r
-  EfiAcquireLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiAcquireLock() at boot time, and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to acquire.\r
-\r
-**/\r
-VOID\r
-AcquireLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-\r
-}\r
-\r
-\r
-/**\r
-  Releases lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function which will be removed when\r
-  EfiReleaseLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiReleaseLock() at boot time and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to release.\r
-\r
-**/\r
-VOID\r
-ReleaseLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-\r
-}\r
-\r
-/**\r
-  Retrive the SMM Fault Tolerent Write protocol interface.\r
-\r
-  @param[out] FtwProtocol       The interface of SMM Ftw protocol\r
-\r
-  @retval EFI_SUCCESS           The SMM FTW protocol instance was found and returned in FtwProtocol.\r
-  @retval EFI_NOT_FOUND         The SMM FTW protocol instance was not found.\r
-  @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFtwProtocol (\r
-  OUT VOID                                **FtwProtocol\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-\r
-  //\r
-  // Locate Smm Fault Tolerent Write protocol\r
-  //\r
-  Status = gSmst->SmmLocateProtocol (\r
-                    &gEfiSmmFaultTolerantWriteProtocolGuid, \r
-                    NULL, \r
-                    FtwProtocol\r
-                    );\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Retrive the SMM FVB protocol interface by HANDLE.\r
-\r
-  @param[in]  FvBlockHandle     The handle of SMM FVB protocol that provides services for\r
-                                reading, writing, and erasing the target block.\r
-  @param[out] FvBlock           The interface of SMM FVB protocol\r
-\r
-  @retval EFI_SUCCESS           The interface information for the specified protocol was returned.\r
-  @retval EFI_UNSUPPORTED       The device does not support the SMM FVB protocol.\r
-  @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbByHandle (\r
-  IN  EFI_HANDLE                          FvBlockHandle,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
-  )\r
-{\r
-  //\r
-  // To get the SMM FVB protocol interface on the handle\r
-  //\r
-  return gSmst->SmmHandleProtocol (\r
-                  FvBlockHandle,\r
-                  &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
-                  (VOID **) FvBlock\r
-                  );\r
-}\r
-\r
-\r
-/**\r
-  Function returns an array of handles that support the SMM FVB protocol\r
-  in a buffer allocated from pool. \r
-\r
-  @param[out]  NumberHandles    The number of handles returned in Buffer.\r
-  @param[out]  Buffer           A pointer to the buffer to return the requested\r
-                                array of  handles that support SMM FVB protocol.\r
-\r
-  @retval EFI_SUCCESS           The array of handles was returned in Buffer, and the number of\r
-                                handles in Buffer was returned in NumberHandles.\r
-  @retval EFI_NOT_FOUND         No SMM FVB handle was found.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough pool memory to store the matching results.\r
-  @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbCountAndBuffer (\r
-  OUT UINTN                               *NumberHandles,\r
-  OUT EFI_HANDLE                          **Buffer\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-  UINTN                                   BufferSize;\r
-\r
-  if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  BufferSize     = 0;\r
-  *NumberHandles = 0;\r
-  *Buffer        = NULL;\r
-  Status = gSmst->SmmLocateHandle (\r
-                    ByProtocol,\r
-                    &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
-                    NULL,\r
-                    &BufferSize,\r
-                    *Buffer\r
-                    );\r
-  if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  *Buffer = AllocatePool (BufferSize);\r
-  if (*Buffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Status = gSmst->SmmLocateHandle (\r
-                    ByProtocol,\r
-                    &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
-                    NULL,\r
-                    &BufferSize,\r
-                    *Buffer\r
-                    );\r
-\r
-  *NumberHandles = BufferSize / sizeof(EFI_HANDLE);\r
-  if (EFI_ERROR(Status)) {\r
-    *NumberHandles = 0;\r
-    FreePool (*Buffer);\r
-    *Buffer = NULL;\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Get the variable statistics information from the information buffer pointed by gVariableInfo.\r
-\r
-  Caution: This function may be invoked at SMM runtime.\r
-  InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.\r
-\r
-  @param[in, out]  InfoEntry    A pointer to the buffer of variable information entry.\r
-                                On input, point to the variable information returned last time. if \r
-                                InfoEntry->VendorGuid is zero, return the first information.\r
-                                On output, point to the next variable information.\r
-  @param[in, out]  InfoSize     On input, the size of the variable information buffer.\r
-                                On output, the returned variable information size.\r
-\r
-  @retval EFI_SUCCESS           The variable information is found and returned successfully.\r
-  @retval EFI_UNSUPPORTED       No variable inoformation exists in variable driver. The \r
-                                PcdVariableCollectStatistics should be set TRUE to support it.\r
-  @retval EFI_BUFFER_TOO_SMALL  The buffer is too small to hold the next variable information.\r
-  @retval EFI_INVALID_PARAMETER Input parameter is invalid.\r
-\r
-**/\r
-EFI_STATUS\r
-SmmVariableGetStatistics (\r
-  IN OUT VARIABLE_INFO_ENTRY                           *InfoEntry,\r
-  IN OUT UINTN                                         *InfoSize\r
-  )\r
-{\r
-  VARIABLE_INFO_ENTRY                                  *VariableInfo;\r
-  UINTN                                                NameLength;\r
-  UINTN                                                StatisticsInfoSize;\r
-  CHAR16                                               *InfoName;\r
-  EFI_GUID                                             VendorGuid;\r
\r
-  if (InfoEntry == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  \r
-  VariableInfo = gVariableInfo; \r
-  if (VariableInfo == NULL) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
-  StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
-  if (*InfoSize < StatisticsInfoSize) {\r
-    *InfoSize = StatisticsInfoSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-  InfoName = (CHAR16 *)(InfoEntry + 1);\r
-\r
-  CopyGuid (&VendorGuid, &InfoEntry->VendorGuid);\r
-\r
-  if (CompareGuid (&VendorGuid, &mZeroGuid)) {\r
-    //\r
-    // Return the first variable info\r
-    //\r
-    CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
-    CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
-    *InfoSize = StatisticsInfoSize;\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Get the next variable info\r
-  //\r
-  while (VariableInfo != NULL) {\r
-    if (CompareGuid (&VariableInfo->VendorGuid, &VendorGuid)) {\r
-      NameLength = StrSize (VariableInfo->Name);\r
-      if (NameLength == StrSize (InfoName)) {\r
-        if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {\r
-          //\r
-          // Find the match one\r
-          //\r
-          VariableInfo = VariableInfo->Next;\r
-          break;\r
-        }\r
-      }\r
-    }\r
-    VariableInfo = VariableInfo->Next;\r
-  };\r
-    \r
-  if (VariableInfo == NULL) {\r
-    *InfoSize = 0;\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Output the new variable info\r
-  //\r
-  StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);\r
-  if (*InfoSize < StatisticsInfoSize) {\r
-    *InfoSize = StatisticsInfoSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-\r
-  CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));\r
-  CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));\r
-  *InfoSize = StatisticsInfoSize;\r
-  \r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-  Communication service SMI Handler entry.\r
-\r
-  This SMI handler provides services for the variable wrapper driver.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  This variable data and communicate buffer are external input, so this function will do basic validation.\r
-  Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(), \r
-  VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(), \r
-  SmmVariableGetStatistics() should also do validation based on its own knowledge.\r
-\r
-  @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
-  @param[in]     RegisterContext Points to an optional handler context which was specified when the\r
-                                 handler was registered.\r
-  @param[in, out] CommBuffer     A pointer to a collection of data in memory that will\r
-                                 be conveyed from a non-SMM environment into an SMM environment.\r
-  @param[in, out] CommBufferSize The size of the CommBuffer.\r
-\r
-  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers \r
-                                              should still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should \r
-                                              still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still \r
-                                              be called.\r
-  @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-SmmVariableHandler (\r
-  IN     EFI_HANDLE                                DispatchHandle,\r
-  IN     CONST VOID                                *RegisterContext,\r
-  IN OUT VOID                                      *CommBuffer,\r
-  IN OUT UINTN                                     *CommBufferSize\r
-  )\r
-{\r
-  EFI_STATUS                                       Status;\r
-  SMM_VARIABLE_COMMUNICATE_HEADER                  *SmmVariableFunctionHeader;\r
-  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE         *SmmVariableHeader;\r
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME  *GetNextVariableName;\r
-  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO     *QueryVariableInfo;\r
-  VARIABLE_INFO_ENTRY                              *VariableInfo;\r
-  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE           *VariableToLock;\r
-  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
-  UINTN                                            InfoSize;\r
-  UINTN                                            NameBufferSize;\r
-  UINTN                                            CommBufferPayloadSize;\r
-  UINTN                                            TempCommBufferSize;\r
-\r
-  //\r
-  // If input is invalid, stop processing this SMI\r
-  //\r
-  if (CommBuffer == NULL || CommBufferSize == NULL) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  TempCommBufferSize = *CommBufferSize;\r
-\r
-  if (TempCommBufferSize < SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) {\r
-    DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer size invalid!\n"));\r
-    return EFI_SUCCESS;\r
-  }\r
-  CommBufferPayloadSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-  if (CommBufferPayloadSize > mVariableBufferPayloadSize) {\r
-    DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
-    DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
-    return EFI_SUCCESS;\r
-  }\r
-  \r
-  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;\r
-    \r
-  switch (SmmVariableFunctionHeader->Function) {\r
-    case SMM_VARIABLE_FUNCTION_GET_VARIABLE:\r
-      if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
-        DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size invalid!\n"));\r
-        return EFI_SUCCESS;\r
-      }\r
-      //\r
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
-      //\r
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
-      SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
-      if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
-         ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
-        //\r
-        // Prevent InfoSize overflow happen\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-      InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) \r
-                 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
-\r
-      //\r
-      // SMRAM range check already covered before\r
-      //\r
-      if (InfoSize > CommBufferPayloadSize) {\r
-        DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication buffer size limit!\n"));\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
-        //\r
-        // Make sure VariableName is A Null-terminated string.\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      Status = VariableServiceGetVariable (\r
-                 SmmVariableHeader->Name,\r
-                 &SmmVariableHeader->Guid,\r
-                 &SmmVariableHeader->Attributes,\r
-                 &SmmVariableHeader->DataSize,\r
-                 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
-                 );\r
-      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
-      break;\r
-      \r
-    case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:\r
-      if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
-        DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffer size invalid!\n"));\r
-        return EFI_SUCCESS;\r
-      }\r
-      //\r
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
-      //\r
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
-      GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) mVariableBufferPayload;\r
-      if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
-        //\r
-        // Prevent InfoSize overflow happen\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-      InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + GetNextVariableName->NameSize;\r
-\r
-      //\r
-      // SMRAM range check already covered before\r
-      //\r
-      if (InfoSize > CommBufferPayloadSize) {\r
-        DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      NameBufferSize = CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
-      if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[NameBufferSize/sizeof (CHAR16) - 1] != L'\0') {\r
-        //\r
-        // Make sure input VariableName is A Null-terminated string.\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      Status = VariableServiceGetNextVariableName (\r
-                 &GetNextVariableName->NameSize,\r
-                 GetNextVariableName->Name,\r
-                 &GetNextVariableName->Guid\r
-                 );\r
-      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
-      break;\r
-      \r
-    case SMM_VARIABLE_FUNCTION_SET_VARIABLE:\r
-      if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
-        DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size invalid!\n"));\r
-        return EFI_SUCCESS;\r
-      }\r
-      //\r
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
-      //\r
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
-      SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) mVariableBufferPayload;\r
-      if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
-         ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) {\r
-        //\r
-        // Prevent InfoSize overflow happen\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-      InfoSize = OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)\r
-                 + SmmVariableHeader->DataSize + SmmVariableHeader->NameSize;\r
-\r
-      //\r
-      // SMRAM range check already covered before\r
-      // Data buffer should not contain SMM range\r
-      //\r
-      if (InfoSize > CommBufferPayloadSize) {\r
-        DEBUG ((EFI_D_ERROR, "SetVariable: Data size exceed communication buffer size limit!\n"));\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHeader->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
-        //\r
-        // Make sure VariableName is A Null-terminated string.\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      Status = VariableServiceSetVariable (\r
-                 SmmVariableHeader->Name,\r
-                 &SmmVariableHeader->Guid,\r
-                 SmmVariableHeader->Attributes,\r
-                 SmmVariableHeader->DataSize,\r
-                 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize\r
-                 );\r
-      break;\r
-      \r
-    case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:\r
-      if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO)) {\r
-        DEBUG ((EFI_D_ERROR, "QueryVariableInfo: SMM communication buffer size invalid!\n"));\r
-        return EFI_SUCCESS;\r
-      }\r
-      QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;\r
-  \r
-      Status = VariableServiceQueryVariableInfo (\r
-                 QueryVariableInfo->Attributes,\r
-                 &QueryVariableInfo->MaximumVariableStorageSize,\r
-                 &QueryVariableInfo->RemainingVariableStorageSize,\r
-                 &QueryVariableInfo->MaximumVariableSize\r
-                 );\r
-      break;\r
-\r
-    case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
-      mEndOfDxe = TRUE;\r
-      //\r
-      // The initialization for variable quota.\r
-      //\r
-      InitializeVariableQuota ();\r
-      if (AtRuntime()) {\r
-        Status = EFI_UNSUPPORTED;\r
-        break;\r
-      }\r
-      ReclaimForOS ();\r
-      Status = EFI_SUCCESS;\r
-      break;\r
-  \r
-    case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:\r
-      mAtRuntime = TRUE;\r
-      Status = EFI_SUCCESS;\r
-      break;\r
-\r
-    case SMM_VARIABLE_FUNCTION_GET_STATISTICS:\r
-      VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;\r
-      InfoSize = TempCommBufferSize - SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-\r
-      //\r
-      // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here. \r
-      // It is covered by previous CommBuffer check \r
-      //\r
-     \r
-      if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {\r
-        DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in SMRAM!\n"));\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }  \r
-\r
-      Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);\r
-      *CommBufferSize = InfoSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-      break;\r
-\r
-    case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE:\r
-      if (mEndOfDxe) {\r
-        Status = EFI_ACCESS_DENIED;\r
-      } else {\r
-        VariableToLock = (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *) SmmVariableFunctionHeader->Data;\r
-        Status = VariableLockRequestToLock (\r
-                   NULL,\r
-                   VariableToLock->Name,\r
-                   &VariableToLock->Guid\r
-                   );\r
-      }\r
-      break;\r
-    case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET:\r
-      if (mEndOfDxe) {\r
-        Status = EFI_ACCESS_DENIED;\r
-      } else {\r
-        CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) SmmVariableFunctionHeader->Data;\r
-        Status = VarCheckVariablePropertySet (\r
-                   CommVariableProperty->Name,\r
-                   &CommVariableProperty->Guid,\r
-                   &CommVariableProperty->VariableProperty\r
-                   );\r
-      }\r
-      break;\r
-    case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET:\r
-      if (CommBufferPayloadSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
-        DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));\r
-        return EFI_SUCCESS;\r
-      }\r
-      //\r
-      // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.\r
-      //\r
-      CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, CommBufferPayloadSize);\r
-      CommVariableProperty = (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *) mVariableBufferPayload;\r
-      if ((UINTN) (~0) - CommVariableProperty->NameSize < OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
-        //\r
-        // Prevent InfoSize overflow happen\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-      InfoSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + CommVariableProperty->NameSize;\r
-\r
-      //\r
-      // SMRAM range check already covered before\r
-      //\r
-      if (InfoSize > CommBufferPayloadSize) {\r
-        DEBUG ((EFI_D_ERROR, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      if (CommVariableProperty->NameSize < sizeof (CHAR16) || CommVariableProperty->Name[CommVariableProperty->NameSize/sizeof (CHAR16) - 1] != L'\0') {\r
-        //\r
-        // Make sure VariableName is A Null-terminated string.\r
-        //\r
-        Status = EFI_ACCESS_DENIED;\r
-        goto EXIT;\r
-      }\r
-\r
-      Status = VarCheckVariablePropertyGet (\r
-                 CommVariableProperty->Name,\r
-                 &CommVariableProperty->Guid,\r
-                 &CommVariableProperty->VariableProperty\r
-                 );\r
-      CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, CommBufferPayloadSize);\r
-      break;\r
-\r
-    default:\r
-      Status = EFI_UNSUPPORTED;\r
-  }\r
-\r
-EXIT:\r
-\r
-  SmmVariableFunctionHeader->ReturnStatus = Status;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  SMM END_OF_DXE protocol notification event handler.\r
-\r
-  @param  Protocol   Points to the protocol's unique identifier\r
-  @param  Interface  Points to the interface instance\r
-  @param  Handle     The handle on which the interface was installed\r
-\r
-  @retval EFI_SUCCESS   SmmEndOfDxeCallback runs successfully\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-SmmEndOfDxeCallback (\r
-  IN CONST EFI_GUID                       *Protocol,\r
-  IN VOID                                 *Interface,\r
-  IN EFI_HANDLE                           Handle\r
-  )\r
-{\r
-  DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
-  mEndOfDxe = TRUE;\r
-  //\r
-  // The initialization for variable quota.\r
-  //\r
-  InitializeVariableQuota ();\r
-  if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
-    ReclaimForOS ();\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  SMM Fault Tolerant Write protocol notification event handler.\r
-\r
-  Non-Volatile variable write may needs FTW protocol to reclaim when \r
-  writting variable.\r
-  \r
-  @param  Protocol   Points to the protocol's unique identifier\r
-  @param  Interface  Points to the interface instance\r
-  @param  Handle     The handle on which the interface was installed\r
-\r
-  @retval EFI_SUCCESS   SmmEventCallback runs successfully\r
-  @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.\r
-  \r
- **/\r
-EFI_STATUS\r
-EFIAPI\r
-SmmFtwNotificationEvent (\r
-  IN CONST EFI_GUID                       *Protocol,\r
-  IN VOID                                 *Interface,\r
-  IN EFI_HANDLE                           Handle\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvbProtocol;\r
-  EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL   *FtwProtocol;\r
-  EFI_PHYSICAL_ADDRESS                    NvStorageVariableBase;\r
-  UINTN                                   FtwMaxBlockSize;\r
-  \r
-  if (mVariableModuleGlobal->FvbInstance != NULL) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  //\r
-  // Ensure SMM FTW protocol is installed.\r
-  //\r
-  Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
-  if (!EFI_ERROR (Status)) {\r
-    ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
-  }\r
-\r
-  //\r
-  // Find the proper FVB protocol for variable.\r
-  //\r
-  NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
-  if (NvStorageVariableBase == 0) {\r
-    NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
-  }\r
-  Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
-  \r
-  Status = VariableWriteServiceInitialize ();\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
-  }\r
\r
-  //\r
-  // Notify the variable wrapper driver the variable write service is ready\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mSmmVariableHandle,\r
-                  &gSmmVariableWriteGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-  Variable Driver main entry point. The Variable driver places the 4 EFI\r
-  runtime services in the EFI System Table and installs arch protocols \r
-  for variable read and write services being available. It also registers\r
-  a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
-\r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
-  @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
-  @retval EFI_SUCCESS       Variable service successfully initialized.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceInitialize (\r
-  IN EFI_HANDLE                           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                     *SystemTable\r
-  )\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_HANDLE                              VariableHandle;\r
-  VOID                                    *SmmFtwRegistration;\r
-  VOID                                    *SmmEndOfDxeRegistration;\r
-\r
-  //\r
-  // Variable initialize.\r
-  //\r
-  Status = VariableCommonInitialize ();\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Install the Smm Variable Protocol on a new handle.\r
-  //\r
-  VariableHandle = NULL;\r
-  Status = gSmst->SmmInstallProtocolInterface (\r
-                    &VariableHandle,\r
-                    &gEfiSmmVariableProtocolGuid,\r
-                    EFI_NATIVE_INTERFACE,\r
-                    &gSmmVariable\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gSmst->SmmInstallProtocolInterface (\r
-                    &VariableHandle,\r
-                    &gEdkiiSmmVarCheckProtocolGuid,\r
-                    EFI_NATIVE_INTERFACE,\r
-                    &mSmmVarCheck\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mVariableBufferPayloadSize = MAX_NV_VARIABLE_SIZE +\r
-                               OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);\r
-\r
-  Status = gSmst->SmmAllocatePool (\r
-                    EfiRuntimeServicesData,\r
-                    mVariableBufferPayloadSize,\r
-                    (VOID **)&mVariableBufferPayload\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  ///\r
-  /// Register SMM variable SMI handler\r
-  ///\r
-  VariableHandle = NULL;\r
-  Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  //\r
-  // Notify the variable wrapper driver the variable service is ready\r
-  //\r
-  Status = SystemTable->BootServices->InstallProtocolInterface (\r
-                                        &mVariableHandle,\r
-                                        &gEfiSmmVariableProtocolGuid,\r
-                                        EFI_NATIVE_INTERFACE,\r
-                                        &gSmmVariable\r
-                                        );\r
-  ASSERT_EFI_ERROR (Status);\r
\r
-  //\r
-  // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
-  //\r
-  Status = gSmst->SmmRegisterProtocolNotify (\r
-                    &gEfiSmmEndOfDxeProtocolGuid,\r
-                    SmmEndOfDxeCallback,\r
-                    &SmmEndOfDxeRegistration\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Register FtwNotificationEvent () notify function.\r
-  // \r
-  Status = gSmst->SmmRegisterProtocolNotify (\r
-                    &gEfiSmmFaultTolerantWriteProtocolGuid,\r
-                    SmmFtwNotificationEvent,\r
-                    &SmmFtwRegistration\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  SmmFtwNotificationEvent (NULL, NULL, NULL);\r
-  \r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.inf
deleted file mode 100644 (file)
index 00181db..0000000
+++ /dev/null
@@ -1,165 +0,0 @@
-## @file\r
-#  Provides SMM authenticated variable service\r
-#\r
-#  This module installs SMM variable protocol into SMM protocol database,\r
-#  which can be used by SMM driver, and installs SMM variable protocol\r
-#  into BS protocol database, which can be used to notify the SMM Runtime\r
-#  Dxe driver that the SMM variable service is ready.\r
-#  This module should be used with SMM Runtime DXE module together. The\r
-#  SMM Runtime DXE module installs variable arch protocol and variable\r
-#  write arch protocol based on SMM variable module.\r
-#\r
-#  Caution: This module requires additional review when modified.\r
-#  This driver will have external input - variable data and communicate buffer in SMM mode.\r
-#  This external input must be validated carefully to avoid security issues such as \r
-#  buffer overflow or integer overflow.\r
-#    The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
-#  which is assumed to be protected by platform.  All variable code and metadata in flash/SMM Memory\r
-#  may not be modified without authorization. If platform fails to protect these resources, \r
-#  the authentication service provided in this driver will be broken, and the behavior is undefined.\r
-#\r
-# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-##\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = VariableAuthSmm\r
-  MODULE_UNI_FILE                = VariableAuthSmm.uni\r
-  FILE_GUID                      = D34BDC5E-968A-40f5-A48C-E594F45AE211\r
-  MODULE_TYPE                    = DXE_SMM_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  PI_SPECIFICATION_VERSION       = 0x0001000A\r
-  ENTRY_POINT                    = VariableServiceInitialize\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64\r
-#\r
-\r
-\r
-[Sources]\r
-  Reclaim.c\r
-  Variable.c\r
-  VariableSmm.c\r
-  AuthService.c\r
-  VarCheck.c\r
-  Variable.h\r
-  AuthService.h\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  CryptoPkg/CryptoPkg.dec\r
-  SecurityPkg/SecurityPkg.dec\r
-\r
-[LibraryClasses]\r
-  UefiDriverEntryPoint\r
-  MemoryAllocationLib\r
-  BaseLib\r
-  SynchronizationLib\r
-  UefiLib\r
-  SmmServicesTableLib\r
-  BaseMemoryLib\r
-  DebugLib\r
-  DxeServicesTableLib\r
-  BaseCryptLib\r
-  PlatformSecureLib\r
-  HobLib\r
-  PcdLib\r
-  DevicePathLib\r
-  SmmMemLib\r
-\r
-[Protocols]\r
-  gEfiSmmFirmwareVolumeBlockProtocolGuid        ## CONSUMES\r
-  \r
-  ## PRODUCES\r
-  ## UNDEFINED     # SmiHandlerRegister\r
-  gEfiSmmVariableProtocolGuid\r
-  \r
-  ## CONSUMES\r
-  ## NOTIFY  \r
-  gEfiSmmFaultTolerantWriteProtocolGuid\r
-  gEfiSmmEndOfDxeProtocolGuid                   ## NOTIFY\r
-  gEdkiiSmmVarCheckProtocolGuid                 ## PRODUCES\r
-\r
-[Guids]\r
-  ## PRODUCES             ## GUID # Variable store header\r
-  ## CONSUMES             ## GUID # Variable store header\r
-  ## SOMETIMES_CONSUMES   ## HOB\r
-  gEfiAuthenticatedVariableGuid\r
-  \r
-  ## SOMETIMES_CONSUMES   ## Variable:L"PlatformLang"\r
-  ## SOMETIMES_PRODUCES   ## Variable:L"PlatformLang"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"Lang"\r
-  ## SOMETIMES_PRODUCES   ## Variable:L"Lang"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"HwErrRecSupport"\r
-  ## CONSUMES             ## Variable:L"SetupMode"\r
-  ## PRODUCES             ## Variable:L"SetupMode"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"PK"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"KEK"\r
-  ## CONSUMES             ## Variable:L"SecureBoot"\r
-  ## PRODUCES             ## Variable:L"SecureBoot"\r
-  ## CONSUMES             ## Variable:L"SignatureSupport"\r
-  ## PRODUCES             ## Variable:L"SignatureSupport"\r
-  ## PRODUCES             ## Variable:L"VendorKeys"\r
-  gEfiGlobalVariableGuid\r
-  \r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DB"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DBX"\r
-  gEfiImageSecurityDatabaseGuid\r
\r
-  ## CONSUMES             ## Variable:L"SecureBootEnable"\r
-  ## PRODUCES             ## Variable:L"SecureBootEnable"\r
-  gEfiSecureBootEnableDisableGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"CustomMode"\r
-  ## PRODUCES             ## Variable:L"CustomMode"\r
-  gEfiCustomModeEnableGuid\r
-  \r
-  ## CONSUMES             ## Variable:L"certdb"\r
-  ## PRODUCES             ## Variable:L"certdb"\r
-  gEfiCertDbGuid\r
-\r
-  ## CONSUMES             ## Variable:L"VendorKeysNv"\r
-  ## PRODUCES             ## Variable:L"VendorKeysNv"\r
-  gEfiVendorKeysNvGuid\r
-    \r
-  gSmmVariableWriteGuid                    ## PRODUCES               ## GUID        # Install protocol\r
-  gEfiCertTypeRsa2048Sha256Guid            ## SOMETIMES_CONSUMES     ## GUID        # Unique ID for the format of the CertData.\r
-  gEfiCertPkcs7Guid                        ## SOMETIMES_CONSUMES     ## GUID        # Unique ID for the format of the CertData.\r
-  gEfiCertX509Guid                         ## SOMETIMES_CONSUMES     ## GUID        # Unique ID for the type of the signature.\r
-  gEfiSystemNvDataFvGuid                   ## CONSUMES               ## GUID\r
-  gEfiHardwareErrorVariableGuid            ## SOMETIMES_CONSUMES     ## Variable:L"HwErrRec####"\r
-  gEdkiiFaultTolerantWriteGuid             ## SOMETIMES_CONSUMES     ## HOB\r
-  gEdkiiVarErrorFlagGuid                   ## CONSUMES               ## GUID\r
-\r
-[Pcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize       ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase       ## SOMETIMES_CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64     ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                  ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize              ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize     ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize                ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                 ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize           ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize  ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdReclaimVariableSpaceAtEndOfDxe   ## CONSUMES\r
-\r
-[FeaturePcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics        ## CONSUMES  # statistic the information of variable.\r
-  gEfiMdePkgTokenSpaceGuid.PcdUefiVariableDefaultLangDeprecate       ## CONSUMES  # Auto update PlatformLang/Lang\r
-\r
-[Depex]\r
-  TRUE\r
-  \r
-[UserExtensions.TianoCore."ExtraFiles"]\r
-  VariableSmmExtra.uni\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmExtra.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmExtra.uni
deleted file mode 100644 (file)
index 0ac11fe..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmExtra.uni and /dev/null differ
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c
deleted file mode 100644 (file)
index ffa0b07..0000000
+++ /dev/null
@@ -1,1118 +0,0 @@
-/** @file\r
-  Implement all four UEFI Runtime Variable services for the nonvolatile\r
-  and volatile storage space and install variable architecture protocol\r
-  based on SMM variable module.\r
-\r
-  Caution: This module requires additional review when modified.\r
-  This driver will have external input - variable data.\r
-  This external input must be validated carefully to avoid security issue like\r
-  buffer overflow, integer overflow.\r
-\r
-  RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API\r
-  to receive data buffer. The size should be checked carefully.\r
-\r
-  InitCommunicateBuffer() is really function to check the variable data size.\r
-\r
-Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <PiDxe.h>\r
-#include <Protocol/VariableWrite.h>\r
-#include <Protocol/Variable.h>\r
-#include <Protocol/SmmCommunication.h>\r
-#include <Protocol/SmmVariable.h>\r
-#include <Protocol/VariableLock.h>\r
-#include <Protocol/VarCheck.h>\r
-\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/UefiRuntimeServicesTableLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/UefiRuntimeLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/PcdLib.h>\r
-#include <Library/UefiLib.h>\r
-#include <Library/BaseLib.h>\r
-\r
-#include <Guid/EventGroup.h>\r
-#include <Guid/AuthenticatedVariableFormat.h>\r
-#include <Guid/SmmVariableCommon.h>\r
-\r
-EFI_HANDLE                       mHandle                    = NULL; \r
-EFI_SMM_VARIABLE_PROTOCOL       *mSmmVariable               = NULL;\r
-EFI_EVENT                        mVirtualAddressChangeEvent = NULL;\r
-EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication          = NULL;\r
-UINT8                           *mVariableBuffer            = NULL;\r
-UINT8                           *mVariableBufferPhysical    = NULL;\r
-UINTN                            mVariableBufferSize;\r
-UINTN                            mVariableBufferPayloadSize;\r
-EFI_LOCK                         mVariableServicesLock;\r
-EDKII_VARIABLE_LOCK_PROTOCOL     mVariableLock;\r
-EDKII_VAR_CHECK_PROTOCOL         mVarCheck;\r
-\r
-#define MAX_NV_VARIABLE_SIZE (MAX (MAX (PcdGet32 (PcdMaxVariableSize), \\r
-                                        PcdGet32 (PcdMaxAuthVariableSize)), \\r
-                                   PcdGet32 (PcdMaxHardwareErrorVariableSize)))\r
-\r
-/**\r
-  SecureBoot Hook for SetVariable.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-SecureBootHook (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid\r
-  );\r
-\r
-/**\r
-  Acquires lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function that will be removed when\r
-  EfiAcquireLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiAcquireLock() at boot time, and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to acquire.\r
-\r
-**/\r
-VOID\r
-AcquireLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-  if (!EfiAtRuntime ()) {\r
-    EfiAcquireLock (Lock);\r
-  }\r
-}\r
-\r
-/**\r
-  Releases lock only at boot time. Simply returns at runtime.\r
-\r
-  This is a temperary function which will be removed when\r
-  EfiReleaseLock() in UefiLib can handle the call in UEFI\r
-  Runtimer driver in RT phase.\r
-  It calls EfiReleaseLock() at boot time and simply returns\r
-  at runtime.\r
-\r
-  @param  Lock         A pointer to the lock to release.\r
-\r
-**/\r
-VOID\r
-ReleaseLockOnlyAtBootTime (\r
-  IN EFI_LOCK                             *Lock\r
-  )\r
-{\r
-  if (!EfiAtRuntime ()) {\r
-    EfiReleaseLock (Lock);\r
-  }\r
-}\r
-\r
-/**\r
-  Initialize the communicate buffer using DataSize and Function.\r
-\r
-  The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +\r
-  DataSize.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  The data size external input, so this function will validate it carefully to avoid buffer overflow.\r
-\r
-  @param[out]      DataPtr          Points to the data in the communicate buffer.\r
-  @param[in]       DataSize         The data size to send to SMM.\r
-  @param[in]       Function         The function number to initialize the communicate header.\r
-                      \r
-  @retval EFI_INVALID_PARAMETER     The data size is too big.\r
-  @retval EFI_SUCCESS               Find the specified variable.\r
-\r
-**/\r
-EFI_STATUS\r
-InitCommunicateBuffer (\r
-  OUT     VOID                              **DataPtr OPTIONAL,\r
-  IN      UINTN                             DataSize,\r
-  IN      UINTN                             Function\r
-  )\r
-{\r
-  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
-  SMM_VARIABLE_COMMUNICATE_HEADER           *SmmVariableFunctionHeader; \r
-\r
\r
-  if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  SmmCommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
-  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
-  SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-   \r
-  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCommunicateHeader->Data;\r
-  SmmVariableFunctionHeader->Function = Function;\r
-  if (DataPtr != NULL) {\r
-    *DataPtr = SmmVariableFunctionHeader->Data;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-  Send the data in communicate buffer to SMM.\r
-\r
-  @param[in]   DataSize               This size of the function header and the data.\r
-\r
-  @retval      EFI_SUCCESS            Success is returned from the functin in SMM.\r
-  @retval      Others                 Failure is returned from the function in SMM. \r
-  \r
-**/\r
-EFI_STATUS\r
-SendCommunicateBuffer (\r
-  IN      UINTN                             DataSize\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     CommSize;\r
-  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
-  SMM_VARIABLE_COMMUNICATE_HEADER           *SmmVariableFunctionHeader;\r
-  \r
-  CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
-  Status = mSmmCommunication->Communicate (mSmmCommunication, mVariableBufferPhysical, &CommSize);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  SmmCommunicateHeader      = (EFI_SMM_COMMUNICATE_HEADER *) mVariableBuffer;\r
-  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;\r
-  return  SmmVariableFunctionHeader->ReturnStatus;\r
-}\r
-\r
-/**\r
-  Mark a variable that will become read-only after leaving the DXE phase of execution.\r
-\r
-  @param[in] This          The VARIABLE_LOCK_PROTOCOL instance.\r
-  @param[in] VariableName  A pointer to the variable name that will be made read-only subsequently.\r
-  @param[in] VendorGuid    A pointer to the vendor GUID that will be made read-only subsequently.\r
-\r
-  @retval EFI_SUCCESS           The variable specified by the VariableName and the VendorGuid was marked\r
-                                as pending to be read-only.\r
-  @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.\r
-                                Or VariableName is an empty string.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource to hold the lock request.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableLockRequestToLock (\r
-  IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,\r
-  IN       CHAR16                       *VariableName,\r
-  IN       EFI_GUID                     *VendorGuid\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     VariableNameSize;\r
-  UINTN                                     PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE    *VariableToLock;\r
-\r
-  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableNameSize = StrSize (VariableName);\r
-  VariableToLock   = NULL;\r
-\r
-  //\r
-  // If VariableName exceeds SMM payload limit. Return failure\r
-  //\r
-  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE, Name) + VariableNameSize;\r
-  Status = InitCommunicateBuffer ((VOID **) &VariableToLock, PayloadSize, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (VariableToLock != NULL);\r
-\r
-  CopyGuid (&VariableToLock->Guid, VendorGuid);\r
-  VariableToLock->NameSize = VariableNameSize;\r
-  CopyMem (VariableToLock->Name, VariableName, VariableToLock->NameSize);\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Register SetVariable check handler.\r
-\r
-  @param[in] Handler            Pointer to check handler.\r
-\r
-  @retval EFI_SUCCESS           The SetVariable check handler was registered successfully.\r
-  @retval EFI_INVALID_PARAMETER Handler is NULL.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the SetVariable check handler register request.\r
-  @retval EFI_UNSUPPORTED       This interface is not implemented.\r
-                                For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckRegisterSetVariableCheckHandler (\r
-  IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER   Handler\r
-  )\r
-{\r
-  return EFI_UNSUPPORTED;\r
-}\r
-\r
-/**\r
-  Variable property set.\r
-\r
-  @param[in] Name               Pointer to the variable name.\r
-  @param[in] Guid               Pointer to the vendor GUID.\r
-  @param[in] VariableProperty   Pointer to the input variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was set successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,\r
-                                or the fields of VariableProperty are not valid.\r
-  @retval EFI_ACCESS_DENIED     EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has\r
-                                already been signaled.\r
-  @retval EFI_OUT_OF_RESOURCES  There is not enough resource for the variable property set request.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertySet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  IN VAR_CHECK_VARIABLE_PROPERTY    *VariableProperty\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     VariableNameSize;\r
-  UINTN                                     PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
-\r
-  if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableNameSize = StrSize (Name);\r
-  CommVariableProperty = NULL;\r
-\r
-  //\r
-  // If VariableName exceeds SMM payload limit. Return failure\r
-  //\r
-  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r
-  Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (CommVariableProperty != NULL);\r
-\r
-  CopyGuid (&CommVariableProperty->Guid, Guid);\r
-  CopyMem (&CommVariableProperty->VariableProperty, VariableProperty, sizeof (*VariableProperty));\r
-  CommVariableProperty->NameSize = VariableNameSize;\r
-  CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  Variable property get.\r
-\r
-  @param[in]  Name              Pointer to the variable name.\r
-  @param[in]  Guid              Pointer to the vendor GUID.\r
-  @param[out] VariableProperty  Pointer to the output variable property.\r
-\r
-  @retval EFI_SUCCESS           The property of variable specified by the Name and Guid was got successfully.\r
-  @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.\r
-  @retval EFI_NOT_FOUND         The property of variable specified by the Name and Guid was not found.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid,\r
-  OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     VariableNameSize;\r
-  UINTN                                     PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProperty;\r
-\r
-  if (Name == NULL || Name[0] == 0 || Guid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableProperty == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableNameSize = StrSize (Name);\r
-  CommVariableProperty = NULL;\r
-\r
-  //\r
-  // If VariableName exceeds SMM payload limit. Return failure\r
-  //\r
-  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime (&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) + VariableNameSize;\r
-  Status = InitCommunicateBuffer ((VOID **) &CommVariableProperty, PayloadSize, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (CommVariableProperty != NULL);\r
-\r
-  CopyGuid (&CommVariableProperty->Guid, Guid);\r
-  CommVariableProperty->NameSize = VariableNameSize;\r
-  CopyMem (CommVariableProperty->Name, Name, CommVariableProperty->NameSize);\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-  if (Status == EFI_SUCCESS) {\r
-    CopyMem (VariableProperty, &CommVariableProperty->VariableProperty, sizeof (*VariableProperty));\r
-  }\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  This code finds variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  The data size is external input, so this function will validate it carefully to avoid buffer overflow.\r
-\r
-  @param[in]      VariableName       Name of Variable to be found.\r
-  @param[in]      VendorGuid         Variable vendor GUID.\r
-  @param[out]     Attributes         Attribute value of the variable found.\r
-  @param[in, out] DataSize           Size of Data found. If size is less than the\r
-                                     data, this value contains the required size.\r
-  @param[out]     Data               Data pointer.\r
-                      \r
-  @retval EFI_INVALID_PARAMETER      Invalid parameter.\r
-  @retval EFI_SUCCESS                Find the specified variable.\r
-  @retval EFI_NOT_FOUND              Not found.\r
-  @retval EFI_BUFFER_TO_SMALL        DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeServiceGetVariable (\r
-  IN      CHAR16                            *VariableName,\r
-  IN      EFI_GUID                          *VendorGuid,\r
-  OUT     UINT32                            *Attributes OPTIONAL,\r
-  IN OUT  UINTN                             *DataSize,\r
-  OUT     VOID                              *Data\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE  *SmmVariableHeader;\r
-  UINTN                                     TempDataSize;\r
-  UINTN                                     VariableNameSize;\r
-\r
-  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  TempDataSize          = *DataSize;\r
-  VariableNameSize      = StrSize (VariableName);\r
-  SmmVariableHeader     = NULL;\r
-\r
-  //\r
-  // If VariableName exceeds SMM payload limit. Return failure\r
-  //\r
-  if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) {\r
-    //\r
-    // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
-    //\r
-    TempDataSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize;\r
-  }\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + TempDataSize;\r
-\r
-  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (SmmVariableHeader != NULL);\r
-\r
-  CopyGuid (&SmmVariableHeader->Guid, VendorGuid);\r
-  SmmVariableHeader->DataSize   = TempDataSize;\r
-  SmmVariableHeader->NameSize   = VariableNameSize;\r
-  if (Attributes == NULL) {\r
-    SmmVariableHeader->Attributes = 0;\r
-  } else {\r
-    SmmVariableHeader->Attributes = *Attributes;\r
-  }\r
-  CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-\r
-  //\r
-  // Get data from SMM.\r
-  //\r
-  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
-    //\r
-    // SMM CommBuffer DataSize can be a trimed value\r
-    // Only update DataSize when needed\r
-    //\r
-    *DataSize = SmmVariableHeader->DataSize;\r
-  }\r
-  if (Attributes != NULL) {\r
-    *Attributes = SmmVariableHeader->Attributes;\r
-  }\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  if (Data != NULL) {\r
-    CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize);\r
-  } else {\r
-    Status = EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  This code Finds the Next available variable.\r
-\r
-  @param[in, out] VariableNameSize   Size of the variable name.\r
-  @param[in, out] VariableName       Pointer to variable name.\r
-  @param[in, out] VendorGuid         Variable Vendor Guid.\r
-\r
-  @retval EFI_INVALID_PARAMETER      Invalid parameter.\r
-  @retval EFI_SUCCESS                Find the specified variable.\r
-  @retval EFI_NOT_FOUND              Not found.\r
-  @retval EFI_BUFFER_TO_SMALL        DataSize is too small for the result.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeServiceGetNextVariableName (\r
-  IN OUT  UINTN                             *VariableNameSize,\r
-  IN OUT  CHAR16                            *VariableName,\r
-  IN OUT  EFI_GUID                          *VendorGuid\r
-  )\r
-{\r
-  EFI_STATUS                                      Status;\r
-  UINTN                                           PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName;\r
-  UINTN                                           OutVariableNameSize;\r
-  UINTN                                           InVariableNameSize;\r
-\r
-  if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  OutVariableNameSize   = *VariableNameSize;\r
-  InVariableNameSize    = StrSize (VariableName);\r
-  SmmGetNextVariableName = NULL;\r
-\r
-  //\r
-  // If input string exceeds SMM payload limit. Return failure\r
-  //\r
-  if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) {\r
-    //\r
-    // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size\r
-    //\r
-    OutVariableNameSize = mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name);\r
-  }\r
-  //\r
-  // Payload should be Guid + NameSize + MAX of Input & Output buffer\r
-  //\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + MAX (OutVariableNameSize, InVariableNameSize);\r
-\r
-  Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (SmmGetNextVariableName != NULL);\r
-\r
-  //\r
-  // SMM comm buffer->NameSize is buffer size for return string\r
-  //\r
-  SmmGetNextVariableName->NameSize = OutVariableNameSize;\r
-\r
-  CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid);\r
-  //\r
-  // Copy whole string\r
-  //\r
-  CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize);\r
-  if (OutVariableNameSize > InVariableNameSize) {\r
-    ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, OutVariableNameSize - InVariableNameSize);\r
-  }\r
-\r
-  //\r
-  // Send data to SMM\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-\r
-  //\r
-  // Get data from SMM.\r
-  //\r
-  if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {\r
-    //\r
-    // SMM CommBuffer NameSize can be a trimed value\r
-    // Only update VariableNameSize when needed\r
-    //\r
-    *VariableNameSize = SmmGetNextVariableName->NameSize;\r
-  }\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  \r
-  CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid);\r
-  CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize);  \r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-/**\r
-  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
-\r
-  Caution: This function may receive untrusted input.\r
-  The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.\r
-\r
-  @param[in] VariableName                 Name of Variable to be found.\r
-  @param[in] VendorGuid                   Variable vendor GUID.\r
-  @param[in] Attributes                   Attribute value of the variable found\r
-  @param[in] DataSize                     Size of Data found. If size is less than the\r
-                                          data, this value contains the required size.\r
-  @param[in] Data                         Data pointer.\r
-\r
-  @retval EFI_INVALID_PARAMETER           Invalid parameter.\r
-  @retval EFI_SUCCESS                     Set successfully.\r
-  @retval EFI_OUT_OF_RESOURCES            Resource not enough to set variable.\r
-  @retval EFI_NOT_FOUND                   Not found.\r
-  @retval EFI_WRITE_PROTECTED             Variable is read-only.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeServiceSetVariable (\r
-  IN CHAR16                                 *VariableName,\r
-  IN EFI_GUID                               *VendorGuid,\r
-  IN UINT32                                 Attributes,\r
-  IN UINTN                                  DataSize,\r
-  IN VOID                                   *Data\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     PayloadSize; \r
-  SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE  *SmmVariableHeader;\r
-  UINTN                                     VariableNameSize;\r
-    \r
-  //\r
-  // Check input parameters.\r
-  //\r
-  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  } \r
-\r
-  if (DataSize != 0 && Data == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  VariableNameSize      = StrSize (VariableName);\r
-  SmmVariableHeader     = NULL;\r
-\r
-  //\r
-  // If VariableName or DataSize exceeds SMM payload limit. Return failure\r
-  //\r
-  if ((VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name)) ||\r
-      (DataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize)){\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.\r
-  //\r
-  PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + VariableNameSize + DataSize;\r
-  Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (SmmVariableHeader != NULL);\r
-\r
-  CopyGuid ((EFI_GUID *) &SmmVariableHeader->Guid, VendorGuid);\r
-  SmmVariableHeader->DataSize   = DataSize;\r
-  SmmVariableHeader->NameSize   = VariableNameSize;\r
-  SmmVariableHeader->Attributes = Attributes;\r
-  CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameSize);\r
-  CopyMem ((UINT8 *) SmmVariableHeader->Name + SmmVariableHeader->NameSize, Data, DataSize);\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-\r
-  if (!EfiAtRuntime ()) {\r
-    if (!EFI_ERROR (Status)) {\r
-      SecureBootHook (\r
-        VariableName,\r
-        VendorGuid\r
-        );\r
-    }\r
-  }\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  This code returns information about the EFI variables.\r
-\r
-  @param[in]  Attributes                   Attributes bitmask to specify the type of variables\r
-                                           on which to return information.\r
-  @param[out] MaximumVariableStorageSize   Pointer to the maximum size of the storage space available\r
-                                           for the EFI variables associated with the attributes specified.\r
-  @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r
-                                           for EFI variables associated with the attributes specified.\r
-  @param[out] MaximumVariableSize          Pointer to the maximum size of an individual EFI variables\r
-                                           associated with the attributes specified.\r
-\r
-  @retval EFI_INVALID_PARAMETER            An invalid combination of attribute bits was supplied.\r
-  @retval EFI_SUCCESS                      Query successfully.\r
-  @retval EFI_UNSUPPORTED                  The attribute is not supported on this platform.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-RuntimeServiceQueryVariableInfo (\r
-  IN  UINT32                                Attributes,\r
-  OUT UINT64                                *MaximumVariableStorageSize,\r
-  OUT UINT64                                *RemainingVariableStorageSize,\r
-  OUT UINT64                                *MaximumVariableSize\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  UINTN                                     PayloadSize;\r
-  SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *SmmQueryVariableInfo;\r
-\r
-  SmmQueryVariableInfo = NULL;\r
-\r
-  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  AcquireLockOnlyAtBootTime(&mVariableServicesLock);\r
-\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;\r
-  //\r
-  PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO);\r
-  Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-  ASSERT (SmmQueryVariableInfo != NULL);\r
-\r
-  SmmQueryVariableInfo->Attributes  = Attributes;\r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  Status = SendCommunicateBuffer (PayloadSize);\r
-  if (EFI_ERROR (Status)) {\r
-    goto Done;\r
-  }\r
-\r
-  //\r
-  // Get data from SMM.\r
-  //\r
-  *MaximumVariableSize          = SmmQueryVariableInfo->MaximumVariableSize;\r
-  *MaximumVariableStorageSize   = SmmQueryVariableInfo->MaximumVariableStorageSize;\r
-  *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; \r
-\r
-Done:\r
-  ReleaseLockOnlyAtBootTime (&mVariableServicesLock);\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Exit Boot Services Event notification handler.\r
-\r
-  Notify SMM variable driver about the event.\r
-\r
-  @param[in]  Event     Event whose notification function is being invoked.\r
-  @param[in]  Context   Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-OnExitBootServices (\r
-  IN      EFI_EVENT                         Event,\r
-  IN      VOID                              *Context\r
-  )\r
-{\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
-  //\r
-  InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE); \r
-\r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  SendCommunicateBuffer (0);\r
-}\r
-\r
-\r
-/**\r
-  On Ready To Boot Services Event notification handler.\r
-\r
-  Notify SMM variable driver about the event.\r
-\r
-  @param[in]  Event     Event whose notification function is being invoked\r
-  @param[in]  Context   Pointer to the notification function's context\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-OnReadyToBoot (\r
-  IN      EFI_EVENT                         Event,\r
-  IN      VOID                              *Context\r
-  )\r
-{\r
-  //\r
-  // Init the communicate buffer. The buffer data size is:\r
-  // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.\r
-  //\r
-  InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT);\r
-  \r
-  //\r
-  // Send data to SMM.\r
-  //\r
-  SendCommunicateBuffer (0);\r
-}\r
-\r
-\r
-/**\r
-  Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
-\r
-  This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
-  It convers pointer to new virtual address.\r
-\r
-  @param[in]  Event        Event whose notification function is being invoked.\r
-  @param[in]  Context      Pointer to the notification function's context.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-VariableAddressChangeEvent (\r
-  IN EFI_EVENT                              Event,\r
-  IN VOID                                   *Context\r
-  )\r
-{\r
-  EfiConvertPointer (0x0, (VOID **) &mVariableBuffer);\r
-  EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
-}\r
-\r
-\r
-/**\r
-  Initialize variable service and install Variable Architectural protocol.\r
-\r
-  @param[in] Event    Event whose notification function is being invoked.\r
-  @param[in] Context  Pointer to the notification function's context.\r
\r
-**/\r
-VOID\r
-EFIAPI\r
-SmmVariableReady (\r
-  IN  EFI_EVENT                             Event,\r
-  IN  VOID                                  *Context\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-\r
-  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable);\r
-  if (EFI_ERROR (Status)) {\r
-    return;\r
-  }\r
-  \r
-  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  //\r
-  // Allocate memory for variable communicate buffer.\r
-  //\r
-  mVariableBufferPayloadSize = MAX_NV_VARIABLE_SIZE +\r
-                               OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY, Name) - sizeof (VARIABLE_HEADER);\r
-  mVariableBufferSize  = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + mVariableBufferPayloadSize;\r
-  mVariableBuffer      = AllocateRuntimePool (mVariableBufferSize);\r
-  ASSERT (mVariableBuffer != NULL);\r
-\r
-  //\r
-  // Save the buffer physical address used for SMM conmunication.\r
-  //\r
-  mVariableBufferPhysical = mVariableBuffer;\r
-\r
-  gRT->GetVariable         = RuntimeServiceGetVariable;\r
-  gRT->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
-  gRT->SetVariable         = RuntimeServiceSetVariable;\r
-  gRT->QueryVariableInfo   = RuntimeServiceQueryVariableInfo;\r
\r
-  //\r
-  // Install the Variable Architectural Protocol on a new handle.\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mHandle,\r
-                  &gEfiVariableArchProtocolGuid, \r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mVariableLock.RequestToLock = VariableLockRequestToLock;\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mHandle,\r
-                  &gEdkiiVariableLockProtocolGuid,\r
-                  &mVariableLock,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mVarCheck.RegisterSetVariableCheckHandler = VarCheckRegisterSetVariableCheckHandler;\r
-  mVarCheck.VariablePropertySet = VarCheckVariablePropertySet;\r
-  mVarCheck.VariablePropertyGet = VarCheckVariablePropertyGet;\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mHandle,\r
-                  &gEdkiiVarCheckProtocolGuid,\r
-                  &mVarCheck,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-}\r
-\r
-\r
-/**\r
-  SMM Non-Volatile variable write service is ready notify event handler.\r
-\r
-  @param[in] Event    Event whose notification function is being invoked.\r
-  @param[in] Context  Pointer to the notification function's context.\r
-  \r
-**/\r
-VOID\r
-EFIAPI\r
-SmmVariableWriteReady (\r
-  IN  EFI_EVENT                             Event,\r
-  IN  VOID                                  *Context\r
-  )\r
-{\r
-  EFI_STATUS                                Status;\r
-  VOID                                      *ProtocolOps;\r
-\r
-  //\r
-  // Check whether the protocol is installed or not.\r
-  //\r
-  Status = gBS->LocateProtocol (&gSmmVariableWriteGuid, NULL, (VOID **) &ProtocolOps);\r
-  if (EFI_ERROR (Status)) {\r
-    return;\r
-  }\r
\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mHandle,\r
-                  &gEfiVariableWriteArchProtocolGuid, \r
-                  EFI_NATIVE_INTERFACE,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);  \r
-}\r
-\r
-\r
-/**\r
-  Variable Driver main entry point. The Variable driver places the 4 EFI\r
-  runtime services in the EFI System Table and installs arch protocols \r
-  for variable read and write services being available. It also registers\r
-  a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
-\r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
-  @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
-  @retval EFI_SUCCESS       Variable service successfully initialized.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VariableSmmRuntimeInitialize (\r
-  IN EFI_HANDLE                             ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                       *SystemTable\r
-  )\r
-{\r
-  VOID                                      *SmmVariableRegistration;\r
-  VOID                                      *SmmVariableWriteRegistration;\r
-  EFI_EVENT                                 OnReadyToBootEvent;\r
-  EFI_EVENT                                 ExitBootServiceEvent;\r
-  EFI_EVENT                                 LegacyBootEvent;\r
-\r
-  EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY);\r
-\r
-  //\r
-  // Smm variable service is ready\r
-  //\r
-  EfiCreateProtocolNotifyEvent (\r
-    &gEfiSmmVariableProtocolGuid, \r
-    TPL_CALLBACK, \r
-    SmmVariableReady, \r
-    NULL, \r
-    &SmmVariableRegistration\r
-    );\r
-\r
-  //\r
-  // Smm Non-Volatile variable write service is ready\r
-  //\r
-  EfiCreateProtocolNotifyEvent (\r
-    &gSmmVariableWriteGuid, \r
-    TPL_CALLBACK, \r
-    SmmVariableWriteReady, \r
-    NULL, \r
-    &SmmVariableWriteRegistration\r
-    );\r
-\r
-  //\r
-  // Register the event to reclaim variable for OS usage.\r
-  //\r
-  EfiCreateEventReadyToBootEx (\r
-    TPL_NOTIFY, \r
-    OnReadyToBoot, \r
-    NULL, \r
-    &OnReadyToBootEvent\r
-    );             \r
-\r
-  //\r
-  // Register the event to inform SMM variable that it is at runtime.\r
-  //\r
-  gBS->CreateEventEx (\r
-         EVT_NOTIFY_SIGNAL,\r
-         TPL_NOTIFY,\r
-         OnExitBootServices,\r
-         NULL,\r
-         &gEfiEventExitBootServicesGuid,\r
-         &ExitBootServiceEvent\r
-         ); \r
-\r
-  //\r
-  // Register the event to inform SMM variable that it is at runtime for legacy boot.\r
-  // Reuse OnExitBootServices() here.\r
-  //\r
-  EfiCreateEventLegacyBootEx(\r
-    TPL_NOTIFY,\r
-    OnExitBootServices,\r
-    NULL,\r
-    &LegacyBootEvent\r
-    );\r
-\r
-  //\r
-  // Register the event to convert the pointer for runtime.\r
-  //\r
-  gBS->CreateEventEx (\r
-         EVT_NOTIFY_SIGNAL,\r
-         TPL_NOTIFY,\r
-         VariableAddressChangeEvent,\r
-         NULL,\r
-         &gEfiEventVirtualAddressChangeGuid,\r
-         &mVirtualAddressChangeEvent\r
-         );\r
-  \r
-  return EFI_SUCCESS;\r
-}\r
-\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.inf b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.inf
deleted file mode 100644 (file)
index db6a4de..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-## @file\r
-#  Runtime DXE part corresponding to SMM authenticated variable module\r
-#\r
-#  This module installs variable arch protocol and variable write arch protocol to provide \r
-#  variable service. This module need work together with SMM authenticated variable module. \r
-#\r
-#  Caution: This module requires additional review when modified.\r
-#  This driver will have external input - variable data.\r
-#  This external input must be validated carefully to avoid security issues such as \r
-#  buffer overflow or integer overflow.\r
-#    The whole SMM authentication variable design relies on the integrity of flash part and SMM.\r
-#  which is assumed to be protected by platform.  All variable code and metadata in flash/SMM Memory\r
-#  may not be modified without authorization. If platform fails to protect these resources, \r
-#  the authentication service provided in this driver will be broken, and the behavior is undefined.\r
-#\r
-# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-##\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = VariableAuthSmmRuntimeDxe\r
-  MODULE_UNI_FILE                = VariableAuthSmmRuntimeDxe.uni\r
-  FILE_GUID                      = 067E2381-7234-4798-B49C-D5FECBFF6D07\r
-  MODULE_TYPE                    = DXE_RUNTIME_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  ENTRY_POINT                    = VariableSmmRuntimeInitialize\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64\r
-#\r
-#  VIRTUAL_ADDRESS_MAP_CALLBACK  =  VariableAddressChangeEvent\r
-#\r
-\r
-[Sources]\r
-  VariableSmmRuntimeDxe.c\r
-  Measurement.c\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  SecurityPkg/SecurityPkg.dec\r
-\r
-[LibraryClasses]\r
-  MemoryAllocationLib\r
-  BaseLib  \r
-  UefiBootServicesTableLib\r
-  DebugLib\r
-  UefiRuntimeLib\r
-  DxeServicesTableLib\r
-  UefiDriverEntryPoint\r
-  PcdLib  \r
-  TpmMeasurementLib\r
-\r
-[Protocols]\r
-  gEfiVariableWriteArchProtocolGuid                                 ## PRODUCES\r
-  gEfiVariableArchProtocolGuid                                      ## PRODUCES\r
-  gEfiSmmCommunicationProtocolGuid                                  ## CONSUMES\r
-  gEdkiiVariableLockProtocolGuid                                    ## PRODUCES\r
-\r
-  ## CONSUMES\r
-  ## NOTIFY\r
-  ## UNDEFINED   # Used to do smm communication\r
-  gEfiSmmVariableProtocolGuid\r
-  gEdkiiVarCheckProtocolGuid                                        ## PRODUCES\r
-\r
-[Guids]\r
-  gEfiEventVirtualAddressChangeGuid                                 ## CONSUMES       ## Event\r
-  gEfiEventExitBootServicesGuid                                     ## CONSUMES       ## Event\r
-    \r
-  ## CONSUMES ## UNDEFINED # Locate protocol\r
-  ## CONSUMES ## UNDEFINED # Protocol notify\r
-  gSmmVariableWriteGuid\r
-  \r
-\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"PK"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"KEK"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"SecureBoot"\r
-  gEfiGlobalVariableGuid\r
-  \r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DB"\r
-  ## SOMETIMES_CONSUMES   ## Variable:L"DBX"\r
-  gEfiImageSecurityDatabaseGuid\r
-\r
-[Pcd]\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize                 ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize             ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize    ## CONSUMES\r
-\r
-[Depex]\r
-  gEfiSmmCommunicationProtocolGuid\r
-\r
-[UserExtensions.TianoCore."ExtraFiles"]\r
-  VariableSmmRuntimeDxeExtra.uni\r
diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxeExtra.uni b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxeExtra.uni
deleted file mode 100644 (file)
index 86cbfbd..0000000
Binary files a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxeExtra.uni and /dev/null differ