]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
use pcd to enable/disable variableInfo statistic feature in EmuRuntimeDxe driver.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / EmuRuntimeDxe / EmuVariable.c
index 158e396963dd837ae393fbd6f60225b38ee656e8..f17dab03b02130b2a35c2eb11c18c9012403511c 100644 (file)
@@ -16,17 +16,25 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "Variable.h"\r
 \r
-//\r
-// Don't use module globals after the SetVirtualAddress map is signaled\r
-//\r
+///\r
+/// Don't use module globals after the SetVirtualAddress map is signaled\r
+///\r
 ESAL_VARIABLE_GLOBAL  *mVariableModuleGlobal;\r
 \r
-//\r
-// This is a temperary function which will be removed\r
-// when EfiAcquireLock in UefiLib can handle the\r
-// the call in UEFI Runtimer driver in RT phase.\r
-//\r
-STATIC\r
+VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
+\r
+/**\r
+  Acquires lock only at boot time. Simply returns at runtime.\r
+\r
+  This is a temperary function which 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
@@ -37,12 +45,18 @@ AcquireLockOnlyAtBootTime (
   }\r
 }\r
 \r
-//\r
-// This is a temperary function which will be removed\r
-// when EfiAcquireLock in UefiLib can handle the\r
-// the call in UEFI Runtimer driver in RT phase.\r
-//\r
-STATIC\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
@@ -53,26 +67,21 @@ ReleaseLockOnlyAtBootTime (
   }\r
 }\r
 \r
-STATIC\r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the variable data.\r
+/**\r
+  Gets pointer to the variable data.\r
 \r
-Arguments:\r
+  This function gets the pointer to the variable data according\r
+  to the input pointer to the variable header.\r
 \r
-  Variable            Pointer to the Variable Header.\r
+  @param  Variable      Pointer to the variable header.\r
 \r
-Returns:\r
+  @return Pointer to variable data\r
 \r
-  UINT8*              Pointer to Variable Data\r
-\r
---*/\r
+**/\r
+UINT8 *\r
+GetVariableDataPtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   if (Variable->StartId != VARIABLE_DATA) {\r
     return NULL;\r
@@ -83,26 +92,21 @@ Returns:
   return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
 }\r
 \r
-STATIC\r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the next variable header.\r
-\r
-Arguments:\r
+/**\r
+  Gets pointer to header of the next variable.\r
 \r
-  Variable                  Pointer to the Variable Header.\r
+  This function gets the pointer to the next variable header according\r
+  to the input point to the variable header.\r
 \r
-Returns:\r
+  @param  Variable      Pointer to header of the next variable\r
 \r
-  VARIABLE_HEADER*      Pointer to next variable header.\r
+  @return Pointer to next variable header.\r
 \r
---*/\r
+**/\r
+VARIABLE_HEADER *\r
+GetNextVariablePtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   VARIABLE_HEADER *VarHeader;\r
 \r
@@ -115,7 +119,7 @@ Returns:
   VarHeader = (VARIABLE_HEADER *) (GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
 \r
   if (VarHeader->StartId != VARIABLE_DATA ||\r
-      (sizeof (VARIABLE_HEADER) + VarHeader->DataSize + VarHeader->NameSize) > MAX_VARIABLE_SIZE\r
+      (sizeof (VARIABLE_HEADER) + VarHeader->DataSize + VarHeader->NameSize) > FixedPcdGet32(PcdMaxVariableSize)\r
       ) {\r
     return NULL;\r
   }\r
@@ -123,26 +127,21 @@ Returns:
   return VarHeader;\r
 }\r
 \r
-STATIC\r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
-  IN VARIABLE_STORE_HEADER       *VolHeader\r
-  )\r
-/*++\r
+/**\r
+  Gets pointer to the end of the variable storage area.\r
 \r
-Routine Description:\r
+  This function gets pointer to the end of the variable storage\r
+  area, according to the input variable store header.\r
 \r
-  This code gets the pointer to the last variable memory pointer byte\r
+  @param  VolHeader     Pointer to the variale store header\r
 \r
-Arguments:\r
+  @return Pointer to the end of the variable storage area.\r
 \r
-  Variable                  Pointer to the Variable Header.\r
-\r
-Returns:\r
-\r
-  VARIABLE_HEADER*      Pointer to last unavailable Variable Header\r
-\r
---*/\r
+**/\r
+VARIABLE_HEADER *\r
+GetEndPointer (\r
+  IN VARIABLE_STORE_HEADER       *VolHeader\r
+  )\r
 {\r
   //\r
   // The end of variable store\r
@@ -150,34 +149,132 @@ Returns:
   return (VARIABLE_HEADER *) ((UINTN) VolHeader + VolHeader->Size);\r
 }\r
 \r
-STATIC\r
-EFI_STATUS\r
-FindVariable (\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
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack,\r
-  IN  VARIABLE_GLOBAL         *Global\r
+  IN  BOOLEAN                 Volatile,\r
+  IN  BOOLEAN                 Read,\r
+  IN  BOOLEAN                 Write,\r
+  IN  BOOLEAN                 Delete,\r
+  IN  BOOLEAN                 Cache\r
   )\r
-/*++\r
+{\r
+  VARIABLE_INFO_ENTRY   *Entry;\r
+\r
+  if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
+\r
+    if (EfiAtRuntime ()) {\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 (StrLen (VariableName));\r
+      ASSERT (gVariableInfo->Name != NULL);\r
+      StrCpy (gVariableInfo->Name, VariableName);\r
+      gVariableInfo->Volatile = Volatile;\r
+\r
+      gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\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
-Routine Description:\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 (StrLen (VariableName));\r
+        ASSERT (Entry->Next->Name != NULL);\r
+        StrCpy (Entry->Next->Name, VariableName);\r
+        Entry->Next->Volatile = Volatile;\r
+      }\r
 \r
-  This code finds variable in storage blocks (Volatile or Non-Volatile)\r
+    }\r
+  }\r
+}\r
 \r
-Arguments:\r
+/**\r
+  Finds variable in storage blocks of volatile and non-volatile storage areas.\r
 \r
-  VariableName                Name of the variable to be found\r
-  VendorGuid                  Vendor GUID to be found.\r
-  PtrTrack                    Variable Track Pointer structure that contains\r
-                              Variable Information.\r
-                              Contains the pointer of Variable header.\r
-  Global                      VARIABLE_GLOBAL pointer\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
+  Otherwise, VariableName and VendorGuid are compared.\r
 \r
-Returns:\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
 \r
-  EFI STATUS\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
+**/\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
+  )\r
 {\r
   VARIABLE_HEADER       *Variable[2];\r
   VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
@@ -215,14 +312,14 @@ Returns:
 \r
     while ((Variable[Index] != NULL) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r
       if (Variable[Index]->StartId == VARIABLE_DATA && Variable[Index]->State == VAR_ADDED) {\r
-        if (!(EfiAtRuntime () && !(Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
+        if (!(EfiAtRuntime () && ((Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
           if (VariableName[0] == 0) {\r
             PtrTrack->CurrPtr   = Variable[Index];\r
             PtrTrack->Volatile  = (BOOLEAN) Index;\r
             return EFI_SUCCESS;\r
           } else {\r
             if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r
-              if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize)) {\r
+              if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize) == 0) {\r
                 PtrTrack->CurrPtr   = Variable[Index];\r
                 PtrTrack->Volatile  = (BOOLEAN) Index;\r
                 return EFI_SUCCESS;\r
@@ -239,6 +336,28 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  This code finds variable in storage blocks (Volatile or Non-Volatile).\r
+  \r
+  @param  VariableName           A Null-terminated Unicode string that is the name of\r
+                                 the vendor's variable.\r
+  @param  VendorGuid             A unique identifier for the vendor.\r
+  @param  Attributes             If not NULL, a pointer to the memory location to return the \r
+                                 attributes bitmask for the variable.\r
+  @param  DataSize               Size of Data found. If size is less than the\r
+                                 data, this value contains the required size.\r
+  @param  Data                   On input, the size in bytes of the return Data buffer.  \r
+                                 On output, the size of data returned in Data.\r
+  @param  Global                 Pointer to VARIABLE_GLOBAL structure\r
+  @param  Instance               Instance of the Firmware Volume.\r
+\r
+  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_NOT_FOUND          The variable was not found.\r
+  @retval EFI_BUFFER_TOO_SMALL   DataSize is too small for the result.  DataSize has \r
+                                 been updated with the size needed to complete the request.\r
+  @retval EFI_INVALID_PARAMETER  VariableName or VendorGuid or DataSize is NULL.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GetVariable (\r
@@ -250,32 +369,6 @@ GetVariable (
   IN      VARIABLE_GLOBAL   * Global,\r
   IN      UINT32            Instance\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code finds variable in storage blocks (Volatile or Non-Volatile)\r
-\r
-Arguments:\r
-\r
-  VariableName                    Name of Variable to be found\r
-  VendorGuid                      Variable vendor GUID\r
-  Attributes OPTIONAL             Attribute value of the variable found\r
-  DataSize                        Size of Data found. If size is less than the\r
-                                  data, this value contains the required size.\r
-  Data                            Data pointer\r
-  Global                          Pointer to VARIABLE_GLOBAL structure\r
-  Instance                        Instance of the Firmware Volume.\r
-\r
-Returns:\r
-\r
-  EFI_INVALID_PARAMETER       - Invalid parameter\r
-  EFI_SUCCESS                 - Find the specified variable\r
-  EFI_NOT_FOUND               - Not found\r
-  EFI_BUFFER_TO_SMALL         - DataSize is too small for the result\r
-\r
-\r
---*/\r
 {\r
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarDataSize;\r
@@ -308,6 +401,7 @@ Returns:
     }\r
 \r
     *DataSize = VarDataSize;\r
+    UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
     Status = EFI_SUCCESS;\r
     goto Done;\r
   } else {\r
@@ -321,6 +415,25 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+\r
+  This code Finds the Next available variable.\r
+\r
+  @param  VariableNameSize       Size of the variable.\r
+  @param  VariableName           On input, supplies the last VariableName that was returned by GetNextVariableName().\r
+                                 On output, returns the Null-terminated Unicode string of the current variable.\r
+  @param  VendorGuid             On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
+                                 On output, returns the VendorGuid of the current variable.  \r
+  @param  Global                 Pointer to VARIABLE_GLOBAL structure.\r
+  @param  Instance               Instance of the Firmware Volume.\r
+\r
+  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_NOT_FOUND          The next variable was not found.\r
+  @retval EFI_BUFFER_TOO_SMALL   VariableNameSize is too small for the result. \r
+                                 VariableNameSize has been updated with the size needed to complete the request.\r
+  @retval EFI_INVALID_PARAMETER  VariableNameSize or VariableName or VendorGuid is NULL.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GetNextVariableName (\r
@@ -330,25 +443,6 @@ GetNextVariableName (
   IN      VARIABLE_GLOBAL   *Global,\r
   IN      UINT32            Instance\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code Finds the Next available variable\r
-\r
-Arguments:\r
-\r
-  VariableNameSize            Size of the variable\r
-  VariableName                Pointer to variable name\r
-  VendorGuid                  Variable Vendor Guid\r
-  Global                      VARIABLE_GLOBAL structure pointer.\r
-  Instance                    FV instance\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
 {\r
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarNameSize;\r
@@ -394,7 +488,7 @@ Returns:
     // Variable is found\r
     //\r
     if (Variable.CurrPtr->StartId == VARIABLE_DATA && Variable.CurrPtr->State == VAR_ADDED) {\r
-      if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
+      if (!(EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
         VarNameSize = Variable.CurrPtr->NameSize;\r
         if (VarNameSize <= *VariableNameSize) {\r
           CopyMem (\r
@@ -424,6 +518,36 @@ Done:
 \r
 }\r
 \r
+/**\r
+\r
+  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
+\r
+  @param  VariableName           A Null-terminated Unicode string that is the name of the vendor's\r
+                                 variable.  Each VariableName is unique for each \r
+                                 VendorGuid.  VariableName must contain 1 or more \r
+                                 Unicode characters.  If VariableName is an empty Unicode \r
+                                 string, then EFI_INVALID_PARAMETER is returned.\r
+  @param  VendorGuid             A unique identifier for the vendor\r
+  @param  Attributes             Attributes bitmask to set for the variable\r
+  @param  DataSize               The size in bytes of the Data buffer.  A size of zero causes the\r
+                                 variable to be deleted.\r
+  @param  Data                   The contents for the variable\r
+  @param  Global                 Pointer to VARIABLE_GLOBAL structure\r
+  @param  VolatileOffset         The offset of last volatile variable\r
+  @param  NonVolatileOffset      The offset of last non-volatile variable\r
+  @param  Instance               Instance of the Firmware Volume.\r
+\r
+  @retval EFI_SUCCESS            The firmware has successfully stored the variable and its data as \r
+                                 defined by the Attributes.\r
+  @retval EFI_INVALID_PARAMETER  An invalid combination of attribute bits was supplied, or the \r
+                                 DataSize exceeds the maximum allowed, or VariableName is an empty \r
+                                 Unicode string, or VendorGuid is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES   Not enough storage is available to hold the variable and its data.\r
+  @retval EFI_DEVICE_ERROR       The variable could not be saved due to a hardware failure.\r
+  @retval EFI_WRITE_PROTECTED    The variable in question is read-only or cannot be deleted.\r
+  @retval EFI_NOT_FOUND          The variable trying to be updated or deleted was not found.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 SetVariable (\r
@@ -437,35 +561,6 @@ SetVariable (
   IN UINTN                   *NonVolatileOffset,\r
   IN UINT32                  Instance\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code sets variable in storage blocks (Volatile or Non-Volatile)\r
-\r
-Arguments:\r
-\r
-  VariableName                    Name of Variable to be found\r
-  VendorGuid                      Variable vendor GUID\r
-  Attributes                      Attribute value of the variable found\r
-  DataSize                        Size of Data found. If size is less than the\r
-                                  data, this value contains the required size.\r
-  Data                            Data pointer\r
-  Global                          Pointer to VARIABLE_GLOBAL structure\r
-  VolatileOffset                  The offset of last volatile variable\r
-  NonVolatileOffset               The offset of last non-volatile variable\r
-  Instance                        Instance of the Firmware Volume.\r
-\r
-Returns:\r
-\r
-  EFI_INVALID_PARAMETER           - Invalid parameter\r
-  EFI_SUCCESS                     - Set successfully\r
-  EFI_OUT_OF_RESOURCES            - Resource not enough to set variable\r
-  EFI_NOT_FOUND                   - Not found\r
-  EFI_DEVICE_ERROR                - Variable can not be saved due to hardware failure\r
-  EFI_WRITE_PROTECTED             - Variable is read-only\r
-\r
---*/\r
 {\r
   VARIABLE_POINTER_TRACK  Variable;\r
   EFI_STATUS              Status;\r
@@ -489,21 +584,21 @@ Returns:
   }\r
   //\r
   //  The size of the VariableName, including the Unicode Null in bytes plus\r
-  //  the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r
-  //  bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r
+  //  the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)\r
+  //  bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.\r
   //\r
   if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||                                                       \r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r
+    if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) ||                                                       \r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {\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_VARIABLE_SIZE (1024) bytes.\r
+  //  the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.\r
   //\r
-    if ((DataSize > MAX_VARIABLE_SIZE) ||\r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r
+    if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||\r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {\r
       return EFI_INVALID_PARAMETER;\r
     }  \r
   }  \r
@@ -531,7 +626,7 @@ Returns:
       //\r
       // Only variable have NV attribute can be updated/deleted in Runtime\r
       //\r
-      if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
+      if ((Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
         Status = EFI_INVALID_PARAMETER;\r
         goto Done;\r
       }\r
@@ -543,6 +638,7 @@ Returns:
     //\r
     if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
       Variable.CurrPtr->State &= VAR_DELETED;\r
+      UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, FALSE, TRUE, FALSE);\r
       Status = EFI_SUCCESS;\r
       goto Done;\r
     }\r
@@ -552,7 +648,7 @@ Returns:
     // then return to the caller immediately.\r
     //\r
     if (Variable.CurrPtr->DataSize == DataSize &&\r
-        !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
+        CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0\r
           ) {\r
       Status = EFI_SUCCESS;\r
       goto Done;\r
@@ -581,7 +677,7 @@ Returns:
     // Only variable have NV|RT attribute can be created in Runtime\r
     //\r
     if (EfiAtRuntime () &&\r
-        (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
+        (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
       Status = EFI_INVALID_PARAMETER;\r
       goto Done;\r
     }         \r
@@ -603,7 +699,7 @@ Returns:
   VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
   VarSize       = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
 \r
-  if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
+  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
     if ((UINT32) (VarSize +*NonVolatileOffset) >\r
           ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
           ) {\r
@@ -657,12 +753,36 @@ Returns:
     Variable.CurrPtr->State &= VAR_DELETED;\r
   }\r
   \r
+  UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, FALSE, TRUE, FALSE, FALSE);\r
+\r
   Status = EFI_SUCCESS;\r
 Done:\r
   ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
   return Status;\r
 }\r
 \r
+/**\r
+\r
+  This code returns information about the EFI variables.\r
+\r
+  @param  Attributes                   Attributes bitmask to specify the type of variables\r
+                                       on which to return information.\r
+  @param  MaximumVariableStorageSize   On output the maximum size of the storage space available for \r
+                                       the EFI variables associated with the attributes specified.  \r
+  @param  RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
+                                       variables associated with the attributes specified.\r
+  @param  MaximumVariableSize          Returns the maximum size of an individual EFI variable \r
+                                       associated with the attributes specified.\r
+  @param  Global                       Pointer to VARIABLE_GLOBAL structure.\r
+  @param  Instance                     Instance of the Firmware Volume.\r
+\r
+  @retval EFI_SUCCESS                  Valid answer returned.\r
+  @retval EFI_INVALID_PARAMETER        An invalid combination of attribute bits was supplied\r
+  @retval EFI_UNSUPPORTED              The attribute is not supported on this platform, and the \r
+                                       MaximumVariableStorageSize, RemainingVariableStorageSize, \r
+                                       MaximumVariableSize are undefined.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 QueryVariableInfo (\r
@@ -673,33 +793,6 @@ QueryVariableInfo (
   IN  VARIABLE_GLOBAL        *Global,\r
   IN  UINT32                 Instance\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-Arguments:\r
-\r
-  Attributes                      Attributes bitmask to specify the type of variables\r
-                                  on which to return information.\r
-  MaximumVariableStorageSize      Pointer to the maximum size of the storage space available\r
-                                  for the EFI variables associated with the attributes specified.\r
-  RemainingVariableStorageSize    Pointer to the remaining size of the storage space available\r
-                                  for EFI variables associated with the attributes specified.\r
-  MaximumVariableSize             Pointer to the maximum size of an individual EFI variables\r
-                                  associated with the attributes specified.\r
-  Global                          Pointer to VARIABLE_GLOBAL structure.\r
-  Instance                        Instance of the Firmware Volume.\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-  EFI_INVALID_PARAMETER           - An invalid combination of attribute bits was supplied.\r
-  EFI_SUCCESS                     - Query successfully.\r
-  EFI_UNSUPPORTED                 - The attribute is not supported on this platform.\r
-\r
---*/\r
 {\r
   VARIABLE_HEADER        *Variable;\r
   VARIABLE_HEADER        *NextVariable;\r
@@ -720,7 +813,7 @@ Returns:
     // Make sure if runtime bit is set, boot service bit is set also.\r
     //\r
     return EFI_INVALID_PARAMETER;\r
-  } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
+  } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
     //\r
     //   Make sure RT Attribute is set if we are in Runtime phase.\r
     //\r
@@ -749,15 +842,15 @@ Returns:
   *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
 \r
   //\r
-  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r
+  // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
   //\r
-  *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+  *MaximumVariableSize = FixedPcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
 \r
   //\r
   // Harware error record variable needs larger size.\r
   //\r
   if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+    *MaximumVariableSize = FixedPcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
   }\r
 \r
   //\r
@@ -796,22 +889,20 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-STATIC\r
+/**\r
+  Initializes variable store area.\r
+\r
+  This function allocates memory space for variable store area and initializes its attributes.\r
+\r
+  @param  VariableBase         Base of the variable store area created\r
+  @param  LastVariableOffset   Size of VARIABLE_STORE_HEADER\r
+\r
+**/\r
 EFI_STATUS\r
 InitializeVariableStore (\r
   OUT EFI_PHYSICAL_ADDRESS  *VariableBase,\r
   OUT UINTN                 *LastVariableOffset\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  This function initializes variable store\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
 {\r
   VARIABLE_STORE_HEADER *VariableStore;\r
 \r
@@ -819,13 +910,13 @@ Returns:
   // Allocate memory for volatile variable store\r
   //\r
   VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (\r
-                                              VARIABLE_STORE_SIZE\r
+                                              FixedPcdGet32(PcdVariableStoreSize)\r
                                               );\r
   if (NULL == VariableStore) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  SetMem (VariableStore, VARIABLE_STORE_SIZE, 0xff);\r
+  SetMem (VariableStore, FixedPcdGet32(PcdVariableStoreSize), 0xff);\r
 \r
   //\r
   // Variable Specific Data\r
@@ -833,8 +924,8 @@ Returns:
   *VariableBase             = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;\r
   *LastVariableOffset       = sizeof (VARIABLE_STORE_HEADER);\r
 \r
-  VariableStore->Signature  = VARIABLE_STORE_SIGNATURE;\r
-  VariableStore->Size       = VARIABLE_STORE_SIZE;\r
+  CopyGuid (&VariableStore->Signature, &gEfiVariableGuid);\r
+  VariableStore->Size       = FixedPcdGet32(PcdVariableStoreSize);\r
   VariableStore->Format     = VARIABLE_STORE_FORMATTED;\r
   VariableStore->State      = VARIABLE_STORE_HEALTHY;\r
   VariableStore->Reserved   = 0;\r
@@ -843,22 +934,25 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Initializes variable store area for non-volatile and volatile variable.\r
+\r
+  This function allocates and initializes memory space for global context of ESAL\r
+  variable service and variable store area for non-volatile and volatile variable.\r
+\r
+  @param  ImageHandle           The Image handle of this driver.\r
+  @param  SystemTable           The pointer of EFI_SYSTEM_TABLE.\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
 EFIAPI\r
 VariableCommonInitialize (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  This function does common initialization for variable services\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
 {\r
   EFI_STATUS  Status;\r
 \r