]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg Variable: Implement variable quota management.
authorStar Zeng <star.zeng@intel.com>
Tue, 27 Jan 2015 08:42:47 +0000 (08:42 +0000)
committerlzeng14 <lzeng14@Edk2>
Tue, 27 Jan 2015 08:42:47 +0000 (08:42 +0000)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16669 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Include/Guid/VarErrorFlag.h [new file with mode: 0644]
MdeModulePkg/MdeModulePkg.dec
MdeModulePkg/MdeModulePkg.uni
MdeModulePkg/Universal/Variable/RuntimeDxe/VarCheck.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf

diff --git a/MdeModulePkg/Include/Guid/VarErrorFlag.h b/MdeModulePkg/Include/Guid/VarErrorFlag.h
new file mode 100644 (file)
index 0000000..1f05396
--- /dev/null
@@ -0,0 +1,41 @@
+/** @file\r
+  Variable error flag definitions.\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
+#ifndef _VARIABLE_ERROR_FLAG_H_\r
+#define _VARIABLE_ERROR_FLAG_H_\r
+\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
+// If the variable is not present, it has the same meaning with VAR_ERROR_FLAG_NO_ERROR.\r
+//\r
+#define VAR_ERROR_FLAG_NAME             L"VarErrorFlag"\r
+\r
+#define VAR_ERROR_FLAG_NO_ERROR         0xFF // 1111-1111\r
+#define VAR_ERROR_FLAG_SYSTEM_ERROR     0xEF // 1110-1111\r
+#define VAR_ERROR_FLAG_USER_ERROR       0xFE // 1111-1110\r
+\r
+typedef UINT8 VAR_ERROR_FLAG;\r
+\r
+#define EDKII_VAR_ERROR_FLAG_GUID { \\r
+  0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } \\r
+};\r
+\r
+extern EFI_GUID gEdkiiVarErrorFlagGuid;\r
+\r
+#endif\r
+\r
index 9ea8abe3b9159364c06ae52f272e5f17ca3672d7..a4cec63a8abb8344e81eb58f1623c63b45fa2372 100644 (file)
   ## Include/Guid/MemoryProfile.h\r
   gEdkiiMemoryProfileGuid              = { 0x821c9a09, 0x541a, 0x40f6, { 0x9f, 0x43, 0xa, 0xd1, 0x93, 0xa1, 0x2c, 0xfe }}\r
 \r
   ## Include/Guid/MemoryProfile.h\r
   gEdkiiMemoryProfileGuid              = { 0x821c9a09, 0x541a, 0x40f6, { 0x9f, 0x43, 0xa, 0xd1, 0x93, 0xa1, 0x2c, 0xfe }}\r
 \r
+  ## Include/Protocol/VarErrorFlag.h\r
+  gEdkiiVarErrorFlagGuid               = { 0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } }\r
+\r
 [Ppis]\r
   ## Include/Ppi/AtaController.h\r
   gPeiAtaControllerPpiGuid       = { 0xa45e60d1, 0xc719, 0x44aa, { 0xb0, 0x7a, 0xaa, 0x77, 0x7f, 0x85, 0x90, 0x6d }}\r
 [Ppis]\r
   ## Include/Ppi/AtaController.h\r
   gPeiAtaControllerPpiGuid       = { 0xa45e60d1, 0xc719, 0x44aa, { 0xb0, 0x7a, 0xaa, 0x77, 0x7f, 0x85, 0x90, 0x6d }}\r
   # @Prompt Maximum HwErr variable size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000|UINT32|0x30000004\r
 \r
   # @Prompt Maximum HwErr variable size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000|UINT32|0x30000004\r
 \r
-  ## The size of reserved HwErr variable space. Note that this value must be less than or equal to PcdFlashNvStorageVariableSize.\r
+  ## The size of reserved HwErr variable space. Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER)).\r
   # In EdkII implementation, HwErr type variable is stored with common non-volatile variables in the same NV region.\r
   # In EdkII implementation, HwErr type variable is stored with common non-volatile variables in the same NV region.\r
-  # so the platform integrator should ensure this value is less than or equal to PcdFlashNvStorageVariableSize.\r
+  # so the platform integrator should ensure this value is less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER)).\r
   # this value is used to guarantee the space of HwErr type variable and not populated by common variable.\r
   # @Prompt HwErr variable storage size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize|0x0000|UINT32|0x30000006\r
 \r
   # this value is used to guarantee the space of HwErr type variable and not populated by common variable.\r
   # @Prompt HwErr variable storage size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize|0x0000|UINT32|0x30000006\r
 \r
+  ## The size of maximum user NV variable space.<BR><BR>\r
+  # Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize).<BR>\r
+  # If the value is 0, it means user variable share the same NV storage with system variable,\r
+  # this is designed to keep the compatibility for the platform that does not allocate special region for user variable.<BR>\r
+  # If the value is non-0, the below 4 types of variables will be regarded as System Variable, their property could be got by VarCheck protocol,\r
+  # otherwise the variable will be regarded as user variable.<BR>\r
+  #  1) UEFI defined variables (gEfiGlobalVariableGuid and gEfiImageSecurityDatabaseGuid(auth variable) variables at least).<BR>\r
+  #  2) Variables managed by Variable driver internally.<BR>\r
+  #  3) Variables need to be locked, they MUST be set by VariableLock protocol.<BR>\r
+  #  4) Important during platform boot, their property SHOULD be set by VarCheck protocol.<BR>\r
+  # The PCD is used to guarantee the space of system variable and not populated by user variable.<BR>\r
+  # @Prompt Maximum user NV variable space size.\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize|0x00|UINT32|0x00000009\r
+\r
+  ## The size of NV variable space reserved at UEFI boottime.<BR><BR>\r
+  # Note that this value must be less than (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize).<BR>\r
+  # In EdkII implementation, variable driver can reserved some NV storage region for boottime settings.\r
+  # So at UEFI runtime, the variable service consumer can not exhaust full NV storage region.<BR>\r
+  # Then the common NV variable space size at boottime will be\r
+  #  (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize),<BR>\r
+  # and the common NV variable space size at runtime will be\r
+  #  (PcdFlashNvStorageVariableSize - EFI_FIRMWARE_VOLUME_HEADER.HeaderLength - sizeof (VARIABLE_STORE_HEADER) - PcdHwErrStorageSize) - PcdBoottimeReservedNvVariableSpaceSize.<BR>\r
+  # @Prompt Boottime reserved NV variable space size.\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize|0x00|UINT32|0x30000007\r
+\r
   ## The size of volatile buffer. This buffer is used to store VOLATILE attribute variables.\r
   # @Prompt Variable storage size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000|UINT32|0x30000005\r
   ## The size of volatile buffer. This buffer is used to store VOLATILE attribute variables.\r
   # @Prompt Variable storage size.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000|UINT32|0x30000005\r
index 436efaeb56958b8a3de7e092ca1d371fef3863b2..b2ed93baaf766c48e3bb1d2d588ded413b8275e1 100644 (file)
Binary files a/MdeModulePkg/MdeModulePkg.uni and b/MdeModulePkg/MdeModulePkg.uni differ
index f8173308cf40c82ad20188e89aab3b886b6bf901..01ca55a1780fecedc47afba2f79b611f666910d4 100644 (file)
@@ -48,14 +48,6 @@ typedef struct {
   INTERNAL_VAR_CHECK_FUNCTION   CheckFunction;\r
 } UEFI_DEFINED_VARIABLE_ENTRY;\r
 \r
   INTERNAL_VAR_CHECK_FUNCTION   CheckFunction;\r
 } UEFI_DEFINED_VARIABLE_ENTRY;\r
 \r
-typedef struct _EFI_LOAD_OPTION {\r
-  UINT32                   Attributes;\r
-  UINT16                   FilePathListLength;\r
-//CHAR16                   Description[];\r
-//EFI_DEVICE_PATH_PROTOCOL FilePathList[];\r
-//UINT8                    OptionalData[];\r
-} EFI_LOAD_OPTION;\r
-\r
 /**\r
   Internal check for load option.\r
 \r
 /**\r
   Internal check for load option.\r
 \r
@@ -75,16 +67,16 @@ InternalVarCheckLoadOption (
   IN VOID                           *Data\r
   )\r
 {\r
   IN VOID                           *Data\r
   )\r
 {\r
-  EFI_LOAD_OPTION           *LoadOption;\r
+  UINT16                    FilePathListLength;\r
   CHAR16                    *Description;\r
   EFI_DEVICE_PATH_PROTOCOL  *FilePathList;\r
 \r
   CHAR16                    *Description;\r
   EFI_DEVICE_PATH_PROTOCOL  *FilePathList;\r
 \r
-  LoadOption = (EFI_LOAD_OPTION *) Data;\r
+  FilePathListLength = *((UINT16 *) ((UINTN) Data + sizeof (UINT32)));\r
 \r
   //\r
   // Check Description\r
   //\r
 \r
   //\r
   // Check Description\r
   //\r
-  Description = (CHAR16 *) ((UINTN) Data + sizeof (EFI_LOAD_OPTION));\r
+  Description = (CHAR16 *) ((UINTN) Data + sizeof (UINT32) + sizeof (UINT16));\r
   while (Description < (CHAR16 *) ((UINTN) Data + DataSize)) {\r
     if (*Description == L'\0') {\r
       break;\r
   while (Description < (CHAR16 *) ((UINTN) Data + DataSize)) {\r
     if (*Description == L'\0') {\r
       break;\r
@@ -100,16 +92,16 @@ InternalVarCheckLoadOption (
   // Check FilePathList\r
   //\r
   FilePathList = (EFI_DEVICE_PATH_PROTOCOL *) Description;\r
   // Check FilePathList\r
   //\r
   FilePathList = (EFI_DEVICE_PATH_PROTOCOL *) Description;\r
-  if ((UINTN) FilePathList > (MAX_ADDRESS - LoadOption->FilePathListLength)) {\r
+  if ((UINTN) FilePathList > (MAX_ADDRESS - FilePathListLength)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (((UINTN) FilePathList + LoadOption->FilePathListLength) > ((UINTN) Data + DataSize)) {\r
+  if (((UINTN) FilePathList + FilePathListLength) > ((UINTN) Data + DataSize)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (LoadOption->FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
+  if (FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (!IsDevicePathValid (FilePathList, LoadOption->FilePathListLength)) {\r
+  if (!IsDevicePathValid (FilePathList, FilePathListLength)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -573,7 +565,7 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
       VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
       0,\r
       VARIABLE_ATTRIBUTE_NV_BS_RT,\r
       VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
       0,\r
       VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_LOAD_OPTION),\r
+      sizeof (UINT32) + sizeof (UINT16),\r
       MAX_UINTN\r
     },\r
     InternalVarCheckLoadOption\r
       MAX_UINTN\r
     },\r
     InternalVarCheckLoadOption\r
@@ -584,7 +576,7 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
       VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
       0,\r
       VARIABLE_ATTRIBUTE_NV_BS_RT,\r
       VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
       0,\r
       VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (EFI_LOAD_OPTION),\r
+      sizeof (UINT32) + sizeof (UINT16),\r
       MAX_UINTN\r
     },\r
     InternalVarCheckLoadOption\r
       MAX_UINTN\r
     },\r
     InternalVarCheckLoadOption\r
@@ -602,6 +594,26 @@ UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
   },\r
 };\r
 \r
   },\r
 };\r
 \r
+typedef struct {\r
+  EFI_GUID                      *Guid;\r
+  CHAR16                        *Name;\r
+  VAR_CHECK_VARIABLE_PROPERTY   VariableProperty;\r
+} VARIABLE_DRIVER_VARIABLE_ENTRY;\r
+\r
+VARIABLE_DRIVER_VARIABLE_ENTRY mVariableDriverVariableList[] = {\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 property.\r
   The code will check if variable guid is global variable guid first.\r
 /**\r
   Get UEFI defined global variable property.\r
   The code will check if variable guid is global variable guid first.\r
@@ -661,14 +673,13 @@ GetUefiDefinedVariableProperty (
           *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;\r
           return EFI_SUCCESS;\r
         }\r
           *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;\r
           return EFI_SUCCESS;\r
         }\r
-      } else {\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
+      if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) {\r
+        if (VarCheckFunction != NULL) {\r
+          *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;\r
         }\r
         }\r
+        *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;\r
+        return EFI_SUCCESS;\r
       }\r
     }\r
 \r
       }\r
     }\r
 \r
@@ -684,6 +695,32 @@ GetUefiDefinedVariableProperty (
   return EFI_SUCCESS;\r
 }\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
 /**\r
   Internal SetVariable check.\r
 \r
@@ -694,7 +731,7 @@ GetUefiDefinedVariableProperty (
   @param[in] Data               Data pointer.\r
 \r
   @retval EFI_SUCCESS           The SetVariable check result was success.\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
+  @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
                                 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
@@ -727,23 +764,27 @@ InternalVarCheckSetVariableCheck (
   }\r
 \r
   Property = NULL;\r
   }\r
 \r
   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
+  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
   }\r
   if (Property == NULL) {\r
-    VarCheckFunction = NULL;\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
+    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
     }\r
   }\r
   if (Property != NULL) {\r
@@ -751,29 +792,28 @@ InternalVarCheckSetVariableCheck (
       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
       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 ((DataSize == 0) || (Attributes == 0)) {\r
+    if (!((DataSize == 0) || (Attributes == 0))) {\r
       //\r
       //\r
-      // Do not check delete variable.\r
+      // Not to delete variable.\r
       //\r
       //\r
-      return EFI_SUCCESS;\r
-    }\r
-    if (Attributes != Property->Attributes) {\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes fail %r - %g:%s\n", EFI_INVALID_PARAMETER, VendorGuid, VariableName));\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) {\r
-      DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail %r - %g:%s\n", 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
+      if (Attributes != 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 < 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
   }\r
@@ -888,18 +928,20 @@ VarCheckRegisterSetVariableCheckHandler (
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Internal variable property get.\r
+  Variable property get function.\r
 \r
 \r
-  @param[in] Name   Pointer to the variable name.\r
-  @param[in] Guid   Pointer to the vendor GUID.\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
 \r
   @return Pointer to the property of variable specified by the Name and Guid.\r
 \r
 **/\r
 VAR_CHECK_VARIABLE_PROPERTY *\r
-InternalVarCheckVariablePropertyGet (\r
-  IN CHAR16                         *Name,\r
-  IN EFI_GUID                       *Guid\r
+VariablePropertyGetFunction (\r
+  IN CHAR16                 *Name,\r
+  IN EFI_GUID               *Guid,\r
+  IN BOOLEAN                WildcardMatch\r
   )\r
 {\r
   LIST_ENTRY                    *Link;\r
   )\r
 {\r
   LIST_ENTRY                    *Link;\r
@@ -907,24 +949,23 @@ InternalVarCheckVariablePropertyGet (
   CHAR16                        *VariableName;\r
   VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
 \r
   CHAR16                        *VariableName;\r
   VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
 \r
-  Property = NULL;\r
-  GetUefiDefinedVariableProperty (Name, Guid, FALSE, &Property, NULL);\r
-  if (Property != NULL) {\r
-    return Property;\r
-  } else {\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
+  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
     }\r
   }\r
 \r
-  return NULL;\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
 }\r
 \r
 /**\r
@@ -975,7 +1016,7 @@ VarCheckVariablePropertySet (
 \r
   AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
 \r
 \r
   AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
 \r
-  Property = InternalVarCheckVariablePropertyGet (Name, Guid);\r
+  Property = VariablePropertyGetFunction (Name, Guid, FALSE);\r
   if (Property != NULL) {\r
     CopyMem (Property, VariableProperty, sizeof (*VariableProperty));\r
   } else {\r
   if (Property != NULL) {\r
     CopyMem (Property, VariableProperty, sizeof (*VariableProperty));\r
   } else {\r
@@ -998,20 +1039,19 @@ Done:
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Variable property get.\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
 \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
   @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
+InternalVarCheckVariablePropertyGet (\r
   IN CHAR16                         *Name,\r
   IN EFI_GUID                       *Guid,\r
   OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
   IN CHAR16                         *Name,\r
   IN EFI_GUID                       *Guid,\r
   OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
@@ -1023,19 +1063,9 @@ VarCheckVariablePropertyGet (
   BOOLEAN                       Found;\r
   VAR_CHECK_VARIABLE_PROPERTY   *Property;\r
 \r
   BOOLEAN                       Found;\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
   Found = FALSE;\r
 \r
   Found = FALSE;\r
 \r
-  AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
-\r
-  Property = InternalVarCheckVariablePropertyGet (Name, Guid);\r
+  Property = VariablePropertyGetFunction (Name, Guid, TRUE);\r
   if (Property != NULL) {\r
     CopyMem (VariableProperty, Property, sizeof (*VariableProperty));\r
     Found = TRUE;\r
   if (Property != NULL) {\r
     CopyMem (VariableProperty, Property, sizeof (*VariableProperty));\r
     Found = TRUE;\r
@@ -1056,8 +1086,45 @@ VarCheckVariablePropertyGet (
     }\r
   }\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
   ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
 \r
-  return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);\r
+  return Status;\r
 }\r
 \r
 }\r
 \r
index e3c06b39622bd5a8e47a9dae7fed2f1f9e678948..0cf367404f92646949cd9fad2a3436313b662052 100644 (file)
@@ -524,6 +524,214 @@ GetEndPointer (
   return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
 }\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
+  //\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 = VAR_ERROR_FLAG_NO_ERROR;\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
+    &Variable\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
 \r
 /**\r
 \r
@@ -567,6 +775,7 @@ Reclaim (
   BOOLEAN               FoundAdded;\r
   EFI_STATUS            Status;\r
   UINTN                 CommonVariableTotalSize;\r
   BOOLEAN               FoundAdded;\r
   EFI_STATUS            Status;\r
   UINTN                 CommonVariableTotalSize;\r
+  UINTN                 CommonUserVariableTotalSize;\r
   UINTN                 HwErrVariableTotalSize;\r
   VARIABLE_HEADER       *UpdatingVariable;\r
   VARIABLE_HEADER       *UpdatingInDeletedTransition;\r
   UINTN                 HwErrVariableTotalSize;\r
   VARIABLE_HEADER       *UpdatingVariable;\r
   VARIABLE_HEADER       *UpdatingInDeletedTransition;\r
@@ -581,6 +790,7 @@ Reclaim (
   VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
 \r
   CommonVariableTotalSize = 0;\r
   VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r
 \r
   CommonVariableTotalSize = 0;\r
+  CommonUserVariableTotalSize = 0;\r
   HwErrVariableTotalSize  = 0;\r
 \r
   if (IsVolatile) {\r
   HwErrVariableTotalSize  = 0;\r
 \r
   if (IsVolatile) {\r
@@ -650,6 +860,9 @@ Reclaim (
         HwErrVariableTotalSize += VariableSize;\r
       } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
         CommonVariableTotalSize += VariableSize;\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
     Variable = NextVariable;\r
@@ -698,6 +911,9 @@ Reclaim (
           HwErrVariableTotalSize += VariableSize;\r
         } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
           CommonVariableTotalSize += VariableSize;\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
       }\r
     }\r
@@ -721,9 +937,13 @@ Reclaim (
         HwErrVariableTotalSize += NewVariableSize;\r
       } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
         CommonVariableTotalSize += NewVariableSize;\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
       }\r
       if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r
-          (CommonVariableTotalSize > VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - 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
         //\r
         // No enough space to store the new variable by NV or NV+HR attribute.\r
         //\r
@@ -761,19 +981,24 @@ Reclaim (
       *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
       mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
       *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);\r
       mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r
       mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r
+      mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r
     } else {\r
     } else {\r
-      NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r
-      while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r
-        VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\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
         if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
+          mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
         } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
         } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
+          mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
+          if (IsUserVariable (Variable)) {\r
+            mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r
+          }\r
         }\r
 \r
         }\r
 \r
-        NextVariable = GetNextVariablePtr (NextVariable);\r
+        Variable = NextVariable;\r
       }\r
       }\r
-      *LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase;\r
+      *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r
     }\r
   }\r
 \r
     }\r
   }\r
 \r
@@ -1627,7 +1852,6 @@ UpdateVariable (
   EFI_STATUS                          Status;\r
   VARIABLE_HEADER                     *NextVariable;\r
   UINTN                               ScratchSize;\r
   EFI_STATUS                          Status;\r
   VARIABLE_HEADER                     *NextVariable;\r
   UINTN                               ScratchSize;\r
-  UINTN                               NonVolatileVarableStoreSize;\r
   UINTN                               VarNameOffset;\r
   UINTN                               VarDataOffset;\r
   UINTN                               VarNameSize;\r
   UINTN                               VarNameOffset;\r
   UINTN                               VarDataOffset;\r
   UINTN                               VarNameSize;\r
@@ -1639,6 +1863,8 @@ UpdateVariable (
   VARIABLE_POINTER_TRACK              NvVariable;\r
   VARIABLE_STORE_HEADER               *VariableStoreHeader;\r
   UINTN                               CacheOffset;\r
   VARIABLE_POINTER_TRACK              NvVariable;\r
   VARIABLE_STORE_HEADER               *VariableStoreHeader;\r
   UINTN                               CacheOffset;\r
+  BOOLEAN                             IsCommonVariable;\r
+  BOOLEAN                             IsCommonUserVariable;\r
 \r
   if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
     //\r
 \r
   if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
     //\r
@@ -1856,12 +2082,25 @@ UpdateVariable (
     // Create a nonvolatile variable.\r
     //\r
     Volatile = FALSE;\r
     // Create a nonvolatile variable.\r
     //\r
     Volatile = FALSE;\r
-    NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r
-    if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \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
       && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r
-      || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
-      && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - 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 (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
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
@@ -1880,6 +2119,13 @@ UpdateVariable (
         }\r
         UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r
         FlushHobVariableToFlash (VariableName, VendorGuid);\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
       goto Done;\r
     }\r
@@ -1965,6 +2211,9 @@ UpdateVariable (
       mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
     } else {\r
       mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\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
     //\r
     // update the memory copy of Flash region.\r
@@ -2579,7 +2828,7 @@ VariableServiceSetVariable (
       // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r
       //\r
       Status = EFI_INVALID_PARAMETER;\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 with different attributes - %g:%s\n", VendorGuid, VariableName));\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
       goto Done;\r
     }\r
   }\r
@@ -2672,8 +2921,11 @@ VariableServiceQueryVariableInfoInternal (
     *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
   } else {\r
     if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
     *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
   } else {\r
     if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-      ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
-      *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r
+      if (AtRuntime ()) {\r
+        *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r
+      } else {\r
+        *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r
+      }\r
     }\r
 \r
     //\r
     }\r
 \r
     //\r
@@ -2749,8 +3001,12 @@ VariableServiceQueryVariableInfoInternal (
 \r
   if ((Attributes  & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
     *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
 \r
   if ((Attributes  & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
     *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
-  }else {\r
-    *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\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
   }\r
 \r
   if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
@@ -2851,22 +3107,23 @@ ReclaimForOS(
   )\r
 {\r
   EFI_STATUS                     Status;\r
   )\r
 {\r
   EFI_STATUS                     Status;\r
-  UINTN                          CommonVariableSpace;\r
-  UINTN                          RemainingCommonVariableSpace;\r
+  UINTN                          RemainingCommonRuntimeVariableSpace;\r
   UINTN                          RemainingHwErrVariableSpace;\r
 \r
   UINTN                          RemainingHwErrVariableSpace;\r
 \r
-  Status  = EFI_SUCCESS; \r
+  Status  = EFI_SUCCESS;\r
 \r
 \r
-  CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r
-\r
-  RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\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
   RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
   //\r
-  // Check if the free area is blow a threshold.\r
+  // Check if the free area is below a threshold.\r
   //\r
   //\r
-  if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
-    || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
+  if ((RemainingCommonRuntimeVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
+    || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r
        (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
     Status = Reclaim (\r
             mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
        (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
     Status = Reclaim (\r
             mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r
@@ -2894,6 +3151,7 @@ InitNonVolatileVariableStore (
   )\r
 {\r
   EFI_FIRMWARE_VOLUME_HEADER            *FvHeader;\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
   VARIABLE_HEADER                       *NextVariable;\r
   EFI_PHYSICAL_ADDRESS                  VariableStoreBase;\r
   UINT64                                VariableStoreLength;\r
@@ -2905,17 +3163,12 @@ InitNonVolatileVariableStore (
   FAULT_TOLERANT_WRITE_LAST_WRITE_DATA  *FtwLastWriteData;\r
   UINT32                                BackUpOffset;\r
   UINT32                                BackUpSize;\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
   mVariableModuleGlobal->FvbInstance = NULL;\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 or equal to the value of\r
-  // PcdFlashNvStorageVariableSize.\r
-  //\r
-  ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\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
   // 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
@@ -2985,6 +3238,37 @@ InitNonVolatileVariableStore (
   }\r
   ASSERT(mNvVariableCache->Size == VariableStoreLength);\r
 \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 variable or hardware error variable size should be < variable store size.\r
   //\r
   //\r
   // The max variable or hardware error variable size should be < variable store size.\r
   //\r
@@ -2993,18 +3277,19 @@ InitNonVolatileVariableStore (
   //\r
   // Parse non-volatile variable data and get last variable offset.\r
   //\r
   //\r
   // Parse non-volatile variable data and get last variable offset.\r
   //\r
-  NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
-  while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r
-    VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r
-    if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-      mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\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
     } else {\r
-      mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
+      mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
     }\r
 \r
     }\r
 \r
-    NextVariable = GetNextVariablePtr (NextVariable);\r
+    Variable = NextVariable;\r
   }\r
   }\r
-  mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
+  mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
index eb21cd854b7a8b53b246116bfd2ed5b607161442..8ea7378231c270adf2ddb9a3ee855e3a4fdc64e5 100644 (file)
@@ -42,6 +42,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Guid/SystemNvDataGuid.h>\r
 #include <Guid/FaultTolerantWrite.h>\r
 #include <Guid/HardwareErrorVariable.h>\r
 #include <Guid/SystemNvDataGuid.h>\r
 #include <Guid/FaultTolerantWrite.h>\r
 #include <Guid/HardwareErrorVariable.h>\r
+#include <Guid/VarErrorFlag.h>\r
 \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
 \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
@@ -85,7 +86,11 @@ typedef struct {
   VARIABLE_GLOBAL VariableGlobal;\r
   UINTN           VolatileLastVariableOffset;\r
   UINTN           NonVolatileLastVariableOffset;\r
   VARIABLE_GLOBAL VariableGlobal;\r
   UINTN           VolatileLastVariableOffset;\r
   UINTN           NonVolatileLastVariableOffset;\r
+  UINTN           CommonVariableSpace;\r
+  UINTN           CommonMaxUserVariableSpace;\r
+  UINTN           CommonRuntimeVariableSpace;\r
   UINTN           CommonVariableTotalSize;\r
   UINTN           CommonVariableTotalSize;\r
+  UINTN           CommonUserVariableTotalSize;\r
   UINTN           HwErrVariableTotalSize;\r
   CHAR8           *PlatformLangCodes;\r
   CHAR8           *LangCodes;\r
   UINTN           HwErrVariableTotalSize;\r
   CHAR8           *PlatformLangCodes;\r
   CHAR8           *LangCodes;\r
@@ -360,6 +365,41 @@ GetFvbInfoByAddress (
   OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvbProtocol OPTIONAL\r
   );\r
 \r
   OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvbProtocol OPTIONAL\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  VariableName                Name of the variable to be found.\r
+  @param  VendorGuid                  Vendor GUID to be found.\r
+  @param  PtrTrack                    VARIABLE_POINTER_TRACK structure for output,\r
+                                      including the range searched and the target position.\r
+  @param  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  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
   This code finds variable in storage blocks (Volatile or Non-Volatile).\r
 /**\r
 \r
   This code finds variable in storage blocks (Volatile or Non-Volatile).\r
@@ -615,6 +655,25 @@ VarCheckVariablePropertySet (
   IN VAR_CHECK_VARIABLE_PROPERTY    *VariableProperty\r
   );\r
 \r
   IN VAR_CHECK_VARIABLE_PROPERTY    *VariableProperty\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 get.\r
 \r
 /**\r
   Variable property get.\r
 \r
@@ -635,6 +694,15 @@ VarCheckVariablePropertyGet (
   OUT VAR_CHECK_VARIABLE_PROPERTY   *VariableProperty\r
   );\r
 \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
 extern VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;\r
 \r
 #endif\r
index 2aa04f1fef8aee935a0d9764de388f03bdd09f40..c61064efde8efe2f505c9dc04c83cfaf5c200520 100644 (file)
@@ -282,6 +282,10 @@ OnReadyToBoot (
   // 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
   // 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 (&gEfiVariableGuid, gVariableInfo);\r
   ReclaimForOS ();\r
   if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
     gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
@@ -305,6 +309,10 @@ OnEndOfDxe (
   )\r
 {\r
   mEndOfDxe = TRUE;\r
   )\r
 {\r
   mEndOfDxe = TRUE;\r
+  //\r
+  // The initialization for variable quota.\r
+  //\r
+  InitializeVariableQuota ();\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
index 007a6dc591ebf778883c7af669925821c96b11a3..ed6d1b5cbb0777d07fd5ec86e1feb1379d03adb4 100644 (file)
@@ -91,6 +91,7 @@
   gEfiEndOfDxeEventGroupGuid                    ## CONSUMES             ## Event\r
   ## SOMETIMES_CONSUMES   ## HOB\r
   gEdkiiFaultTolerantWriteGuid\r
   gEfiEndOfDxeEventGroupGuid                    ## CONSUMES             ## Event\r
   ## SOMETIMES_CONSUMES   ## HOB\r
   gEdkiiFaultTolerantWriteGuid\r
+  gEdkiiVarErrorFlagGuid                        ## CONSUMES             ## GUID\r
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## CONSUMES\r
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize    ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize               ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize    ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize               ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                ## CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize           ## CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize  ## CONSUMES\r
   \r
 [FeaturePcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics  ## CONSUMES # statistic the information of variable.\r
   \r
 [FeaturePcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics  ## CONSUMES # statistic the information of variable.\r
index 6df1660a04c3ed914110724a7a71ef57dc5fdd49..815ee2e81965dd96a6a5419712b0afab7b06c1df 100644 (file)
@@ -693,6 +693,10 @@ SmmVariableHandler (
 \r
     case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:\r
       mEndOfDxe = TRUE;\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
       if (AtRuntime()) {\r
         Status = EFI_UNSUPPORTED;\r
         break;\r
@@ -824,6 +828,10 @@ SmmEndOfDxeCallback (
 {\r
   DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
   mEndOfDxe = TRUE;\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
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
index 5e882134498041f1329bc5047e467a633f2aae83..e57a731d15eaa5cf59fe94eb359990a5cee430a5 100644 (file)
@@ -93,6 +93,7 @@
   gEfiHardwareErrorVariableGuid                 ## SOMETIMES_CONSUMES   ## Variable:L"HwErrRec####"\r
   ## SOMETIMES_CONSUMES   ## HOB\r
   gEdkiiFaultTolerantWriteGuid\r
   gEfiHardwareErrorVariableGuid                 ## SOMETIMES_CONSUMES   ## Variable:L"HwErrRec####"\r
   ## SOMETIMES_CONSUMES   ## HOB\r
   gEdkiiFaultTolerantWriteGuid\r
+  gEdkiiVarErrorFlagGuid                        ## CONSUMES             ## GUID\r
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## CONSUMES\r
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize    ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize               ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize    ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize               ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize                ## CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize           ## CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize  ## CONSUMES\r
   \r
 [FeaturePcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics   ## CONSUMES # statistic the information of variable.\r
   \r
 [FeaturePcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics   ## CONSUMES # statistic the information of variable.\r