]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
File st32_64.S added for X64.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
index bc4e6eb38d0d29a94cc365391bd0ede4e5ef3c5f..98da3b8b792a1b8bebc2e45975cd9098ca572aaa 100644 (file)
@@ -3,7 +3,7 @@
   Implement all four UEFI Runtime Variable services for the nonvolatile\r
   and volatile storage space and install variable architecture protocol.\r
   \r
-Copyright (c) 2006 - 2008, Intel Corporation                                                         \r
+Copyright (c) 2006 - 2009, Intel Corporation                                                         \r
 All rights reserved. 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
@@ -14,19 +14,44 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-\r
 #include "Variable.h"\r
 \r
 VARIABLE_MODULE_GLOBAL  *mVariableModuleGlobal;\r
 EFI_EVENT   mVirtualAddressChangeEvent = NULL;\r
 EFI_HANDLE  mHandle = NULL;\r
 \r
+///\r
+/// The current Hii implementation accesses this variable many times on every boot.\r
+/// Other common variables are only accessed once. This is why this cache algorithm\r
+/// only targets a single variable. Probably to get an performance improvement out of\r
+/// a Cache you would need a cache that improves the search performance for a variable.\r
+///\r
+VARIABLE_CACHE_ENTRY mVariableCache[] = {\r
+  {\r
+    &gEfiGlobalVariableGuid,\r
+    L"Lang",\r
+    0x00000000,\r
+    0x00,\r
+    NULL\r
+  }\r
+};\r
+\r
+VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
+EFI_EVENT          mFvbRegistration = NULL;\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
+\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,11 +62,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
+/**\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,9 +85,6 @@ ReleaseLockOnlyAtBootTime (
 }\r
 \r
 \r
-GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
-\r
-\r
 /**\r
   Routine used to track statistical information about variable usage. \r
   The data is stored in the EFI system table so it can be accessed later.\r
@@ -106,10 +135,11 @@ UpdateVariableInfo (
 \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 (&gEfiVariableInfoGuid, gVariableInfo);\r
+      gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
     }\r
 \r
     \r
@@ -143,6 +173,7 @@ UpdateVariableInfo (
 \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
@@ -152,24 +183,20 @@ UpdateVariableInfo (
 }\r
 \r
 \r
-BOOLEAN\r
-IsValidVariableHeader (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code checks if variable header is valid or not.\r
 \r
-Arguments:\r
-  Variable              Pointer to the Variable Header.\r
+  @param Variable        Pointer to the Variable Header.\r
 \r
-Returns:\r
-  TRUE            Variable header is valid.\r
-  FALSE           Variable header is not valid.\r
+  @retval TRUE           Variable header is valid.\r
+  @retval FALSE          Variable header is not valid.\r
 \r
---*/\r
+**/\r
+BOOLEAN\r
+IsValidVariableHeader (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
     return FALSE;\r
@@ -179,41 +206,35 @@ Returns:
 }\r
 \r
 \r
-EFI_STATUS\r
-UpdateVariableStore (\r
-  IN  VARIABLE_GLOBAL         *Global,\r
-  IN  BOOLEAN                 Volatile,\r
-  IN  BOOLEAN                 SetByIndex,\r
-  IN  UINTN                   Instance,\r
-  IN  UINTN                   DataPtrIndex,\r
-  IN  UINT32                  DataSize,\r
-  IN  UINT8                   *Buffer\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This function writes data to the FWH at the correct LBA even if the LBAs\r
   are fragmented.\r
 \r
-Arguments:\r
+  @param Global                  Pointer to VARAIBLE_GLOBAL structure\r
+  @param Volatile                Point out the Variable is Volatile or Non-Volatile\r
+  @param SetByIndex              TRUE if target pointer is given as index\r
+                                 FALSE if target pointer is absolute\r
+  @param Fvb                     Pointer to the writable FVB protocol\r
+  @param DataPtrIndex            Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
+                                 structure\r
+  @param DataSize                Size of data to be written\r
+  @param Buffer                  Pointer to the buffer from which data is written\r
 \r
-  Global            - Pointer to VARAIBLE_GLOBAL structure\r
-  Volatile          - If the Variable is Volatile or Non-Volatile\r
-  SetByIndex        - TRUE: Target pointer is given as index\r
-                      FALSE: Target pointer is absolute\r
-  Instance          - Instance of FV Block services\r
-  DataPtrIndex      - Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
-                      structure\r
-  DataSize          - Size of data to be written.\r
-  Buffer            - Pointer to the buffer from which data is written\r
+  @retval EFI_INVALID_PARAMETER  Parameters not valid\r
+  @retval EFI_SUCCESS            Variable store successfully updated\r
 \r
-Returns:\r
-\r
-  EFI_INVALID_PARAMETER   - Parameters not valid\r
-  EFI_SUCCESS             - Variable store successfully updated\r
-\r
---*/\r
+**/\r
+EFI_STATUS\r
+UpdateVariableStore (\r
+  IN  VARIABLE_GLOBAL                     *Global,\r
+  IN  BOOLEAN                             Volatile,\r
+  IN  BOOLEAN                             SetByIndex,\r
+  IN  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb,\r
+  IN  UINTN                               DataPtrIndex,\r
+  IN  UINT32                              DataSize,\r
+  IN  UINT8                               *Buffer\r
+  )\r
 {\r
   EFI_FV_BLOCK_MAP_ENTRY      *PtrBlockMapEntry;\r
   UINTN                       BlockIndex2;\r
@@ -236,7 +257,9 @@ Returns:
   // Check if the Data is Volatile\r
   //\r
   if (!Volatile) {\r
-    EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r
+    Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
     FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
     //\r
     // Data Pointer should point to the actual Address where data is to be\r
@@ -291,18 +314,18 @@ Returns:
       //\r
       if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
         if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
-          Status = EfiFvbWriteBlock (\r
-                    Instance,\r
+          Status = Fvb->Write (\r
+                    Fvb,\r
                     LbaNumber,\r
                     (UINTN) (CurrWritePtr - LinearOffset),\r
                     &CurrWriteSize,\r
                     CurrBuffer\r
                     );\r
-            return Status;\r
+          return Status;\r
         } else {\r
           Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
-          Status = EfiFvbWriteBlock (\r
-                    Instance,\r
+          Status = Fvb->Write (\r
+                    Fvb,\r
                     LbaNumber,\r
                     (UINTN) (CurrWritePtr - LinearOffset),\r
                     &Size,\r
@@ -327,38 +350,35 @@ Returns:
 }\r
 \r
 \r
-VARIABLE_STORE_STATUS\r
-GetVariableStoreStatus (\r
-  IN VARIABLE_STORE_HEADER *VarStoreHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code gets the current status of Variable Store.\r
 \r
-Arguments:\r
-\r
-  VarStoreHeader  Pointer to the Variable Store Header.\r
+  @param VarStoreHeader  Pointer to the Variable Store Header.\r
 \r
-Returns:\r
+  @retval EfiRaw         Variable store status is raw\r
+  @retval EfiValid       Variable store status is valid\r
+  @retval EfiInvalid     Variable store status is invalid\r
 \r
-  EfiRaw        Variable store status is raw\r
-  EfiValid      Variable store status is valid\r
-  EfiInvalid    Variable store status is invalid\r
-\r
---*/\r
+**/\r
+VARIABLE_STORE_STATUS\r
+GetVariableStoreStatus (\r
+  IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+  )\r
 {\r
-  if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r
+  if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&\r
       VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
       VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
       ) {\r
 \r
     return EfiValid;\r
-  } else if (VarStoreHeader->Signature == 0xffffffff &&\r
-           VarStoreHeader->Size == 0xffffffff &&\r
-           VarStoreHeader->Format == 0xff &&\r
-           VarStoreHeader->State == 0xff\r
+  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
+             ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
+             ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
+             ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
+             VarStoreHeader->Size == 0xffffffff &&\r
+             VarStoreHeader->Format == 0xff &&\r
+             VarStoreHeader->State == 0xff\r
           ) {\r
 \r
     return EfiRaw;\r
@@ -368,107 +388,83 @@ Returns:
 }\r
 \r
 \r
-UINTN\r
-NameSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code gets the size of name of variable.\r
 \r
-Arguments:\r
-\r
-  Variable            Pointer to the Variable Header.\r
+  @param Variable        Pointer to the Variable Header\r
 \r
-Returns:\r
+  @return UINTN          Size of variable in bytes\r
 \r
-  UINTN               Size of variable in bytes\r
-\r
---*/\r
+**/\r
+UINTN\r
+NameSizeOfVariable (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   if (Variable->State    == (UINT8) (-1) ||\r
-      Variable->DataSize == (UINT32) -1 ||\r
-      Variable->NameSize == (UINT32) -1 ||\r
-      Variable->Attributes == (UINT32) -1) {\r
+      Variable->DataSize == (UINT32) (-1) ||\r
+      Variable->NameSize == (UINT32) (-1) ||\r
+      Variable->Attributes == (UINT32) (-1)) {\r
     return 0;\r
   }\r
   return (UINTN) Variable->NameSize;\r
 }\r
 \r
-UINTN\r
-DataSizeOfVariable (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the size of name of variable.\r
-\r
-Arguments:\r
+/**\r
 \r
-  Variable            Pointer to the Variable Header.\r
+  This code gets the size of variable data.\r
 \r
-Returns:\r
+  @param Variable        Pointer to the Variable Header\r
 \r
-  UINTN               Size of variable in bytes\r
+  @return Size of variable in bytes\r
 \r
---*/\r
+**/\r
+UINTN\r
+DataSizeOfVariable (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
-  if (Variable->State    == (UINT8)  -1 ||\r
-      Variable->DataSize == (UINT32) -1 ||\r
-      Variable->NameSize == (UINT32) -1 ||\r
-      Variable->Attributes == (UINT32) -1) {\r
+  if (Variable->State    == (UINT8)  (-1) ||\r
+      Variable->DataSize == (UINT32) (-1) ||\r
+      Variable->NameSize == (UINT32) (-1) ||\r
+      Variable->Attributes == (UINT32) (-1)) {\r
     return 0;\r
   }\r
   return (UINTN) Variable->DataSize;\r
 }\r
 \r
-CHAR16 *\r
-GetVariableNamePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code gets the pointer to the variable name.\r
 \r
-Arguments:\r
-\r
-  Variable            Pointer to the Variable Header.\r
-\r
-Returns:\r
+  @param Variable        Pointer to the Variable Header\r
 \r
-  CHAR16*              Pointer to Variable Name\r
+  @return Pointer to Variable Name which is Unicode encoding\r
 \r
---*/\r
+**/\r
+CHAR16 *\r
+GetVariableNamePtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
 \r
   return (CHAR16 *) (Variable + 1);\r
 }\r
 \r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code gets the pointer to the variable data.\r
 \r
-Arguments:\r
-\r
-  Variable            Pointer to the Variable Header.\r
-\r
-Returns:\r
+  @param Variable        Pointer to the Variable Header\r
 \r
-  UINT8*              Pointer to Variable Data\r
+  @return Pointer to Variable Data\r
 \r
---*/\r
+**/\r
+UINT8 *\r
+GetVariableDataPtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   UINTN Value;\r
   \r
@@ -483,25 +479,19 @@ Returns:
 }\r
 \r
 \r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code gets the pointer to the next variable header.\r
 \r
-Arguments:\r
-\r
-  Variable              Pointer to the Variable Header.\r
+  @param Variable        Pointer to the Variable Header\r
 \r
-Returns:\r
+  @return Pointer to next variable header\r
 \r
-  VARIABLE_HEADER*      Pointer to next variable header.\r
-\r
---*/\r
+**/\r
+VARIABLE_HEADER *\r
+GetNextVariablePtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   UINTN Value;\r
 \r
@@ -519,25 +509,19 @@ Returns:
   return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
 }\r
 \r
-VARIABLE_HEADER *\r
-GetStartPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the first variable memory pointer byte\r
-\r
-Arguments:\r
+/**\r
 \r
-  VarStoreHeader        Pointer to the Variable Store Header.\r
+  Gets the pointer to the first variable header in given variable store area.\r
 \r
-Returns:\r
+  @param VarStoreHeader  Pointer to the Variable Store Header.\r
 \r
-  VARIABLE_HEADER*      Pointer to last unavailable Variable Header\r
+  @return Pointer to the first variable header\r
 \r
---*/\r
+**/\r
+VARIABLE_HEADER *\r
+GetStartPointer (\r
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
+  )\r
 {\r
   //\r
   // The end of variable store\r
@@ -545,25 +529,22 @@ Returns:
   return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r
 }\r
 \r
-VARIABLE_HEADER *\r
-GetEndPointer (\r
-  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the last variable memory pointer byte\r
+/**\r
 \r
-Arguments:\r
+  Gets the pointer to the end of the variable storage area.\r
 \r
-  VarStoreHeader        Pointer to the Variable Store Header.\r
+  This function gets pointer to the end of the variable storage\r
+  area, according to the input variable store header.\r
 \r
-Returns:\r
+  @param VarStoreHeader  Pointer to the Variable Store Header\r
 \r
-  VARIABLE_HEADER*      Pointer to last unavailable Variable Header\r
+  @return Pointer to the end of the variable storage area  \r
 \r
---*/\r
+**/\r
+VARIABLE_HEADER *\r
+GetEndPointer (\r
+  IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
+  )\r
 {\r
   //\r
   // The end of variable store\r
@@ -572,6 +553,21 @@ Returns:
 }\r
 \r
 \r
+/**\r
+\r
+  Variable store garbage collection and reclaim operation.\r
+\r
+  @param VariableBase            Base address of variable store\r
+  @param LastVariableOffset      Offset of last variable\r
+  @param IsVolatile              The variable store is volatile or not,\r
+                                 if it is non-volatile, need FTW\r
+  @param UpdatingVariable        Pointer to updateing variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES\r
+  @return EFI_SUCCESS\r
+  @return Others\r
+\r
+**/\r
 EFI_STATUS\r
 Reclaim (\r
   IN  EFI_PHYSICAL_ADDRESS  VariableBase,\r
@@ -579,24 +575,6 @@ Reclaim (
   IN  BOOLEAN               IsVolatile,\r
   IN  VARIABLE_HEADER       *UpdatingVariable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Variable store garbage collection and reclaim operation\r
-\r
-Arguments:\r
-\r
-  VariableBase                Base address of variable store\r
-  LastVariableOffset          Offset of last variable\r
-  IsVolatile                  The variable store is volatile or not,\r
-                              if it is non-volatile, need FTW\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
 {\r
   VARIABLE_HEADER       *Variable;\r
   VARIABLE_HEADER       *AddedVariable;\r
@@ -655,10 +633,6 @@ Returns:
   CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
   CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r
 \r
-  //\r
-  // Start Pointers for the variable.\r
-  //\r
-\r
   //\r
   // Reinstall all ADDED variables as long as they are not identical to Updating Variable\r
   // \r
@@ -774,30 +748,13 @@ Returns:
 }\r
 \r
 \r
-//\r
-// The current Hii implementation accesses this variable a larg # of times on every boot.\r
-// Other common variables are only accessed a single time. This is why this cache algorithm\r
-// only targets a single variable. Probably to get an performance improvement out of\r
-// a Cache you would need a cache that improves the search performance for a variable.\r
-//\r
-VARIABLE_CACHE_ENTRY mVariableCache[] = {\r
-  {\r
-    &gEfiGlobalVariableGuid,\r
-    L"Lang",\r
-    0x00000000,\r
-    0x00,\r
-    NULL\r
-  }\r
-};\r
-\r
-\r
 /**\r
   Update the Cache with Variable information. These are the same \r
   arguments as the EFI Variable services.\r
 \r
   @param[in] VariableName  Name of variable\r
   @param[in] VendorGuid    Guid of variable\r
-  @param[in] Attribute     Attribue of the variable\r
+  @param[in] Attributes    Attribues of the variable\r
   @param[in] DataSize      Size of data. 0 means delete\r
   @param[in] Data          Variable data\r
 \r
@@ -815,7 +772,9 @@ UpdateVariableCache (
   UINTN                     Index;\r
 \r
   if (EfiAtRuntime ()) {\r
+    //\r
     // Don't use the cache at runtime\r
+    // \r
     return;\r
   }\r
 \r
@@ -824,7 +783,9 @@ UpdateVariableCache (
       if (StrCmp (VariableName, Entry->Name) == 0) { \r
         Entry->Attributes = Attributes;\r
         if (DataSize == 0) {\r
+          //\r
           // Delete Case\r
+          //\r
           if (Entry->DataSize != 0) {\r
             FreePool (Entry->Data);\r
           }\r
@@ -833,6 +794,8 @@ UpdateVariableCache (
           CopyMem (Entry->Data, Data, DataSize);\r
         } else {\r
           Entry->Data = AllocatePool (DataSize);\r
+          ASSERT (Entry->Data != NULL);\r
+\r
           Entry->DataSize = DataSize;\r
           CopyMem (Entry->Data, Data, DataSize);\r
         }\r
@@ -843,16 +806,23 @@ UpdateVariableCache (
 \r
 \r
 /**\r
-  Search the cache to see if the variable is in the cache.\r
+  Search the cache to check if the variable is in it.\r
 \r
-  @param[in] VariableName  Name of variable\r
-  @param[in] VendorGuid    Guid of variable\r
-  @param[in] Attribute     Attribue returned \r
-  @param[in] DataSize      Size of data returned\r
-  @param[in] Data          Variable data returned\r
+  This function searches the variable cache. If the variable to find exists, return its data\r
+  and attributes.\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.\r
+  @param  VendorGuid            A unique identifier for the vendor\r
+  @param  Attributes            Pointer to the attributes bitmask of the variable for output.\r
+  @param  DataSize              On input, size of the buffer of Data.\r
+                                On output, size of the variable's data.\r
+  @param  Data                  Pointer to the data buffer for output.\r
 \r
-  @retval EFI_SUCCESS      VariableGuid & VariableName data was returned.\r
-  @retval other            Not found.\r
+  @retval EFI_SUCCESS           VariableGuid & VariableName data was returned.\r
+  @retval EFI_NOT_FOUND         No matching variable found in cache.\r
+  @retval EFI_BUFFER_TOO_SMALL  *DataSize is smaller than size of the variable's data to return.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -898,7 +868,28 @@ FindVariableInCache (
   return EFI_NOT_FOUND;\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
+  Otherwise, VariableName and VendorGuid are compared.\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
+\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_INVALID_PARAMETER       Variable not found\r
 \r
+**/\r
 EFI_STATUS\r
 FindVariable (\r
   IN  CHAR16                  *VariableName,\r
@@ -906,26 +897,6 @@ FindVariable (
   OUT VARIABLE_POINTER_TRACK  *PtrTrack,\r
   IN  VARIABLE_GLOBAL         *Global\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 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
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
 {\r
   VARIABLE_HEADER         *Variable[2];\r
   VARIABLE_HEADER         *InDeletedVariable;\r
@@ -963,7 +934,7 @@ Returns:
       if (Variable[Index]->State == VAR_ADDED || \r
           Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
          ) {\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
             if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
               InDeletedVariable     = Variable[Index];\r
@@ -981,7 +952,7 @@ Returns:
               Point = (VOID *) GetVariableNamePtr (Variable[Index]);\r
 \r
               ASSERT (NameSizeOfVariable (Variable[Index]) != 0);\r
-              if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {\r
+              if (CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index])) == 0) {\r
                 if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
                   InDeletedVariable     = Variable[Index];\r
                   InDeletedStorageIndex = Index;\r
@@ -1014,33 +985,23 @@ Returns:
 }\r
 \r
 \r
+/**\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
+  This code finds variable in storage blocks (Volatile or Non-Volatile).\r
 \r
+  @param VariableName               Name of Variable to be found.\r
+  @param VendorGuid                 Variable vendor GUID.\r
+  @param Attributes                 Attribute value of the variable found.\r
+  @param DataSize                   Size of Data found. If size is less than the\r
+                                    data, this value contains the required size.\r
+  @param Data                       Data pointer.\r
+                      \r
+  @return EFI_INVALID_PARAMETER     Invalid parameter\r
+  @return EFI_SUCCESS               Find the specified variable\r
+  @return EFI_NOT_FOUND             Not found\r
+  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result\r
 \r
---*/\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 RuntimeServiceGetVariable (\r
@@ -1112,25 +1073,20 @@ Done:
 \r
 \r
 \r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code Finds the Next available variable\r
-\r
-Arguments:\r
+/**\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
+  This code Finds the Next available variable.\r
 \r
-Returns:\r
+  @param VariableNameSize           Size of the variable name\r
+  @param VariableName               Pointer to variable name\r
+  @param VendorGuid                 Variable Vendor Guid\r
 \r
-  EFI STATUS\r
+  @return EFI_INVALID_PARAMETER     Invalid parameter\r
+  @return EFI_SUCCESS               Find the specified variable\r
+  @return EFI_NOT_FOUND             Not found\r
+  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result\r
 \r
---*/\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 RuntimeServiceGetNextVariableName (\r
@@ -1185,7 +1141,7 @@ RuntimeServiceGetNextVariableName (
     // Variable is found\r
     //\r
     if (IsValidVariableHeader (Variable.CurrPtr) && 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)) == 0) {\r
         VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
         ASSERT (VarNameSize != 0);\r
 \r
@@ -1218,36 +1174,24 @@ Done:
   return Status;\r
 }\r
 \r
+/**\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
+  This code sets variable in storage blocks (Volatile or Non-Volatile).\r
 \r
-Returns:\r
+  @param VariableName                     Name of Variable to be found\r
+  @param VendorGuid                       Variable vendor GUID\r
+  @param Attributes                       Attribute value of the variable found\r
+  @param DataSize                         Size of Data found. If size is less than the\r
+                                          data, this value contains the required size.\r
+  @param Data                             Data pointer\r
 \r
-  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
+  @return EFI_INVALID_PARAMETER           Invalid parameter\r
+  @return EFI_SUCCESS                     Set successfully\r
+  @return EFI_OUT_OF_RESOURCES            Resource not enough to set variable\r
+  @return EFI_NOT_FOUND                   Not found\r
+  @return EFI_WRITE_PROTECTED             Variable is read-only\r
 \r
---*/\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 RuntimeServiceSetVariable (\r
@@ -1258,20 +1202,20 @@ RuntimeServiceSetVariable (
   IN VOID                    *Data\r
   )\r
 {\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  EFI_STATUS              Status;\r
-  VARIABLE_HEADER         *NextVariable;\r
-  UINTN                   VarNameSize;\r
-  UINTN                   VarNameOffset;\r
-  UINTN                   VarDataOffset;\r
-  UINTN                   VarSize;\r
-  UINT8                   State;\r
-  BOOLEAN                 Reclaimed;\r
-  UINTN                   *VolatileOffset;\r
-  UINTN                   *NonVolatileOffset;\r
-  UINT32                  Instance;\r
-  BOOLEAN                 Volatile;\r
-  EFI_PHYSICAL_ADDRESS    Point;\r
+  VARIABLE_POINTER_TRACK              Variable;\r
+  EFI_STATUS                          Status;\r
+  VARIABLE_HEADER                     *NextVariable;\r
+  UINTN                               VarNameSize;\r
+  UINTN                               VarNameOffset;\r
+  UINTN                               VarDataOffset;\r
+  UINTN                               VarSize;\r
+  UINT8                               State;\r
+  BOOLEAN                             Reclaimed;\r
+  UINTN                               *VolatileOffset;\r
+  UINTN                               *NonVolatileOffset;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  BOOLEAN                             Volatile;\r
+  EFI_PHYSICAL_ADDRESS                Point;\r
 \r
   //\r
   // Check input parameters\r
@@ -1288,21 +1232,21 @@ RuntimeServiceSetVariable (
 \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
@@ -1310,7 +1254,7 @@ RuntimeServiceSetVariable (
   AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
 \r
   Reclaimed         = FALSE;\r
-  Instance          = mVariableModuleGlobal->FvbInstance;\r
+  Fvb               = mVariableModuleGlobal->FvbInstance;\r
   VolatileOffset    = &mVariableModuleGlobal->VolatileLastVariableOffset;\r
 \r
   //\r
@@ -1355,7 +1299,7 @@ RuntimeServiceSetVariable (
       //\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
@@ -1372,7 +1316,7 @@ RuntimeServiceSetVariable (
                  &mVariableModuleGlobal->VariableGlobal,\r
                  Variable.Volatile,\r
                  FALSE,\r
-                 Instance,\r
+                 Fvb,\r
                  (UINTN) &Variable.CurrPtr->State,\r
                  sizeof (UINT8),\r
                  &State\r
@@ -1406,7 +1350,7 @@ RuntimeServiceSetVariable (
                  &mVariableModuleGlobal->VariableGlobal,\r
                  Variable.Volatile,\r
                  FALSE,\r
-                 Instance,\r
+                 Fvb,\r
                  (UINTN) &Variable.CurrPtr->State,\r
                  sizeof (UINT8),\r
                  &State\r
@@ -1433,7 +1377,7 @@ RuntimeServiceSetVariable (
     // 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
@@ -1454,7 +1398,7 @@ RuntimeServiceSetVariable (
   //\r
   NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r
 \r
-  SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
+  SetMem (NextVariable, FixedPcdGet32(PcdMaxVariableSize), 0xff);\r
 \r
   NextVariable->StartId     = VARIABLE_DATA;\r
   NextVariable->Attributes  = Attributes;\r
@@ -1489,7 +1433,7 @@ RuntimeServiceSetVariable (
   // include pad size.\r
   //\r
   VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
-  if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
+  if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
     //\r
     // Create a nonvolatile variable\r
     //\r
@@ -1535,7 +1479,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                FALSE,\r
                TRUE,\r
-               Instance,\r
+               Fvb,\r
                *NonVolatileOffset,\r
                sizeof (VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
@@ -1553,7 +1497,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                FALSE,\r
                TRUE,\r
-               Instance,\r
+               Fvb,\r
                *NonVolatileOffset,\r
                sizeof (VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
@@ -1569,7 +1513,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                FALSE,\r
                TRUE,\r
-               Instance,\r
+               Fvb,\r
                *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
                (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
                (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
@@ -1586,7 +1530,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                FALSE,\r
                TRUE,\r
-               Instance,\r
+               Fvb,\r
                *NonVolatileOffset,\r
                sizeof (VARIABLE_HEADER),\r
                (UINT8 *) NextVariable\r
@@ -1631,7 +1575,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                TRUE,\r
                TRUE,\r
-               Instance,\r
+               Fvb,\r
                *VolatileOffset,\r
                (UINT32) VarSize,\r
                (UINT8 *) NextVariable\r
@@ -1654,7 +1598,7 @@ RuntimeServiceSetVariable (
                &mVariableModuleGlobal->VariableGlobal,\r
                Variable.Volatile,\r
                FALSE,\r
-               Instance,\r
+               Fvb,\r
                (UINTN) &Variable.CurrPtr->State,\r
                sizeof (UINT8),\r
                &State\r
@@ -1678,34 +1622,24 @@ Done:
   return Status;\r
 }\r
 \r
-\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   This code returns information about the EFI variables.\r
 \r
-Arguments:\r
+  @param Attributes                     Attributes bitmask to specify the type of variables\r
+                                        on which to return information.\r
+  @param MaximumVariableStorageSize     Pointer to the maximum size of the storage space available\r
+                                        for the EFI variables associated with the attributes specified.\r
+  @param RemainingVariableStorageSize   Pointer to the remaining size of the storage space available\r
+                                        for EFI variables associated with the attributes specified.\r
+  @param MaximumVariableSize            Pointer to the maximum size of an individual EFI variables\r
+                                        associated with the attributes specified.\r
 \r
-  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
+  @return EFI_INVALID_PARAMETER         An invalid combination of attribute bits was supplied.\r
+  @return EFI_SUCCESS                   Query successfully.\r
+  @return EFI_UNSUPPORTED               The attribute is not supported on this platform.\r
 \r
-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
 EFI_STATUS\r
 EFIAPI\r
 RuntimeServiceQueryVariableInfo (\r
@@ -1734,7 +1668,7 @@ RuntimeServiceQueryVariableInfo (
     // 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
@@ -1763,15 +1697,15 @@ RuntimeServiceQueryVariableInfo (
   *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
@@ -1820,6 +1754,18 @@ RuntimeServiceQueryVariableInfo (
   return EFI_SUCCESS;\r
 }\r
 \r
+\r
+/**\r
+  Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
+\r
+  This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
+  When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
+  storage if free size is below the threshold.\r
+\r
+  @param  Event        Event whose notification function is being invoked\r
+  @param  Context      Pointer to the notification function's context\r
+\r
+**/\r
 VOID\r
 EFIAPI\r
 ReclaimForOS(\r
@@ -1847,46 +1793,31 @@ ReclaimForOS(
   }\r
 }\r
 \r
-EFI_STATUS\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
-  ImageHandle   - The firmware allocated handle for the EFI image.\r
-  SystemTable   - A pointer to the EFI System Table.\r
-\r
-Returns:\r
+/**\r
+  Initializes variable store area for non-volatile and volatile variable.\r
 \r
-  Status code.\r
+  @param  SystemTable           The pointer of EFI_SYSTEM_TABLE.\r
 \r
-  EFI_NOT_FOUND     - Variable store area not found.\r
-  EFI_UNSUPPORTED   - Currently only one non-volatile variable store is supported.\r
-  EFI_SUCCESS       - Variable services successfully initialized.\r
+  @retval EFI_SUCCESS           Function successfully executed.\r
+  @retval EFI_OUT_OF_RESOURCES  Fail to allocate enough memory resource.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+VariableCommonInitialize (\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol\r
+  )\r
 {\r
   EFI_STATUS                      Status;\r
-  EFI_FIRMWARE_VOLUME_HEADER      *FwVolHeader;\r
-  CHAR8                           *CurrPtr;\r
   VARIABLE_STORE_HEADER           *VolatileVariableStore;\r
   VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
   VARIABLE_HEADER                 *NextVariable;\r
-  UINT32                          Instance;\r
-  EFI_PHYSICAL_ADDRESS            FvVolHdr;\r
-  UINT64                          TempVariableStoreHeader;\r
+  EFI_PHYSICAL_ADDRESS            TempVariableStoreHeader;\r
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
-  UINT64                          BaseAddress;\r
+  EFI_PHYSICAL_ADDRESS            BaseAddress;\r
   UINT64                          Length;\r
   UINTN                           Index;\r
   UINT8                           Data;\r
-  UINT64                          VariableStoreBase;\r
+  EFI_PHYSICAL_ADDRESS            VariableStoreBase;\r
   UINT64                          VariableStoreLength;\r
   EFI_EVENT                       ReadyToBootEvent;\r
 \r
@@ -1905,22 +1836,23 @@ Returns:
   //\r
   // Allocate memory for volatile variable store\r
   //\r
-  VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r
+  VolatileVariableStore = AllocateRuntimePool (FixedPcdGet32(PcdVariableStoreSize) + FixedPcdGet32(PcdMaxVariableSize));\r
   if (VolatileVariableStore == NULL) {\r
     FreePool (mVariableModuleGlobal);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r
+  SetMem (VolatileVariableStore, FixedPcdGet32(PcdVariableStoreSize) + FixedPcdGet32(PcdMaxVariableSize), 0xff);\r
 \r
   //\r
   //  Variable Specific Data\r
   //\r
   mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
   mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
+  mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
 \r
-  VolatileVariableStore->Signature                  = VARIABLE_STORE_SIGNATURE;\r
-  VolatileVariableStore->Size                       = VARIABLE_STORE_SIZE;\r
+  CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r
+  VolatileVariableStore->Size                       = FixedPcdGet32(PcdVariableStoreSize);\r
   VolatileVariableStore->Format                     = VARIABLE_STORE_FORMATTED;\r
   VolatileVariableStore->State                      = VARIABLE_STORE_HEALTHY;\r
   VolatileVariableStore->Reserved                   = 0;\r
@@ -1930,11 +1862,11 @@ Returns:
   // Get non volatile varaible store\r
   //\r
 \r
-  TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
+  TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
   VariableStoreBase = TempVariableStoreHeader + \\r
-                              (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
+                              (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
   VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
-                                (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
+                                (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r
   //\r
   // Mark the variable storage region of the FLASH as RUNTIME\r
   //\r
@@ -1959,26 +1891,7 @@ Returns:
   // Get address of non volatile variable store base\r
   //\r
   mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
-\r
-  //\r
-  // Check Integrity\r
-  //\r
-  //\r
-  // Find the Correct Instance of the FV Block Service.\r
-  //\r
-  Instance  = 0;\r
-  CurrPtr   = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
-  while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
-    if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r
-      mVariableModuleGlobal->FvbInstance = Instance;\r
-      break;\r
-    }\r
-\r
-    Instance++;\r
-  }\r
-\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
+  VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r
   if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
     if (~VariableStoreHeader->Size == 0) {\r
       Status = UpdateVariableStore (\r
@@ -2005,18 +1918,17 @@ Returns:
       }\r
     }\r
 \r
-    mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
     //\r
     // Parse non-volatile variable data and get last variable offset\r
     //\r
-    NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *) CurrPtr);\r
+    NextVariable  = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r
     Status        = EFI_SUCCESS;\r
 \r
     while (IsValidVariableHeader (NextVariable)) {\r
       NextVariable = GetNextVariablePtr (NextVariable);\r
     }\r
 \r
-    mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r
+    mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r
 \r
     //\r
     // Check if the free area is really free.\r
@@ -2051,6 +1963,9 @@ Returns:
                NULL, \r
                &ReadyToBootEvent\r
                );\r
+  } else {\r
+    Status = EFI_VOLUME_CORRUPTED;\r
+    DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
   }\r
 \r
 Done:\r
@@ -2062,6 +1977,16 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+  Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
+\r
+  This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
+  It convers pointer to new virtual address.\r
+\r
+  @param  Event        Event whose notification function is being invoked\r
+  @param  Context      Pointer to the notification function's context\r
+\r
+**/\r
 VOID\r
 EFIAPI\r
 VariableClassAddressChangeEvent (\r
@@ -2069,6 +1994,14 @@ VariableClassAddressChangeEvent (
   IN VOID             *Context\r
   )\r
 {\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);\r
   EfiConvertPointer (\r
     0x0,\r
     (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r
@@ -2080,17 +2013,122 @@ VariableClassAddressChangeEvent (
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
 }\r
 \r
+VOID\r
+EFIAPI\r
+FvbNotificationEvent (\r
+  IN  EFI_EVENT       Event,\r
+  IN  VOID            *Context\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_HANDLE                          *HandleBuffer;\r
+  UINTN                               HandleCount;\r
+  UINTN                               Index;\r
+  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
+  EFI_FVB_ATTRIBUTES_2                Attributes;\r
+  EFI_SYSTEM_TABLE                    *SystemTable;\r
+  EFI_PHYSICAL_ADDRESS                NvStorageVariableBase;\r
+\r
+  SystemTable = (EFI_SYSTEM_TABLE *)Context;\r
+  Fvb         = NULL;\r
+  \r
+  //\r
+  // Locate all handles of Fvb protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return ;\r
+  }\r
+  \r
+  //\r
+  // Get the FVB to access variable store\r
+  //\r
+  for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[Index],\r
+                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                    (VOID **) &Fvb\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_NOT_FOUND;\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Ensure this FVB protocol supported Write operation.\r
+    //\r
+    Status = Fvb->GetAttributes (Fvb, &Attributes);\r
+    if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
+      continue;     \r
+    }\r
+    //\r
+    // Compare the address and select the right one\r
+    //\r
+    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
+    NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
+    if ((NvStorageVariableBase >= FvbBaseAddress) && (NvStorageVariableBase < (FvbBaseAddress + FwVolHeader->FvLength))) {\r
+      Status      = EFI_SUCCESS;\r
+      break;\r
+    }\r
+  }\r
+\r
+  FreePool (HandleBuffer);\r
+  if (!EFI_ERROR (Status) && Fvb != NULL) {\r
+    Status = VariableCommonInitialize (Fvb);\r
+    ASSERT_EFI_ERROR (Status);\r
+  \r
+    SystemTable->RuntimeServices->GetVariable            = RuntimeServiceGetVariable;\r
+    SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
+    SystemTable->RuntimeServices->SetVariable            = RuntimeServiceSetVariable;\r
+    SystemTable->RuntimeServices->QueryVariableInfo      = RuntimeServiceQueryVariableInfo;\r
+  \r
+    //\r
+    // Now install the Variable Runtime Architectural Protocol on a new handle\r
+    //\r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mHandle,\r
+                  &gEfiVariableArchProtocolGuid, NULL,\r
+                  &gEfiVariableWriteArchProtocolGuid, NULL,\r
+                  NULL\r
+                  );\r
+    ASSERT_EFI_ERROR (Status);\r
+  \r
+    Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  VariableClassAddressChangeEvent,\r
+                  NULL,\r
+                  &gEfiEventVirtualAddressChangeGuid,\r
+                  &mVirtualAddressChangeEvent\r
+                  );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+}\r
 \r
 /**\r
   Variable Driver main entry point. The Variable driver places the 4 EFI\r
   runtime services in the EFI System Table and installs arch protocols \r
-  for variable read and write services being availible. \r
+  for variable read and write services being availible. It also registers\r
+  notification function for EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
 \r
   @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
   @param[in] SystemTable    A pointer to the EFI System Table.\r
   \r
-  @retval EFI_SUCCESS       The entry point is executed successfully.\r
-  @retval other             Some error occurs when executing this entry point.\r
+  @retval EFI_SUCCESS       Variable service successfully initialized.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -2100,35 +2138,16 @@ VariableServiceInitialize (
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-\r
-  Status = VariableCommonInitialize (ImageHandle, SystemTable);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  SystemTable->RuntimeServices->GetVariable         = RuntimeServiceGetVariable;\r
-  SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r
-  SystemTable->RuntimeServices->SetVariable         = RuntimeServiceSetVariable;\r
-  SystemTable->RuntimeServices->QueryVariableInfo   = RuntimeServiceQueryVariableInfo;\r
-\r
   //\r
-  // Now install the Variable Runtime Architectural Protocol on a new handle\r
-  //\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mHandle,\r
-                  &gEfiVariableArchProtocolGuid,        NULL,\r
-                  &gEfiVariableWriteArchProtocolGuid,   NULL,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->CreateEvent (\r
-                  EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r
-                  TPL_NOTIFY,\r
-                  VariableClassAddressChangeEvent,\r
-                  NULL,\r
-                  &mVirtualAddressChangeEvent\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
+  // Register FvbNotificationEvent () notify function.\r
+  // \r
+  EfiCreateProtocolNotifyEvent (\r
+    &gEfiFirmwareVolumeBlockProtocolGuid,\r
+    TPL_CALLBACK,\r
+    FvbNotificationEvent,\r
+    (VOID *)SystemTable,\r
+    &mFvbRegistration\r
+    );\r
 \r
   return EFI_SUCCESS;\r
 }\r