]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/Pei/Variable.c
Fix the clang compilation failure to handle all the values in a case statement for...
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / Pei / Variable.c
index 18e252f97312fdcd027b353e2abb01183af94ae7..5043f19c51062a1ac3cf04e65532716ce51fa00c 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
 \r
   Implement ReadOnly Variable Services required by PEIM and install\r
-  PI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.\r
+  PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space.\r
 \r
-Copyright (c) 2006 - 2008 Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -32,58 +32,40 @@ EFI_PEI_PPI_DESCRIPTOR     mPpiListVariable = {
   &mVariablePpi\r
 };\r
 \r
-EFI_GUID mEfiVariableIndexTableGuid = EFI_VARIABLE_INDEX_TABLE_GUID;\r
 \r
+/**\r
+  Provide the functionality of the variable services.\r
+  \r
+  @param  FileHandle   Handle of the file being invoked. \r
+                       Type EFI_PEI_FILE_HANDLE is defined in FfsFindNextFile().\r
+  @param  PeiServices  General purpose services available to every PEIM.\r
+\r
+  @retval EFI_SUCCESS  If the interface could be successfully installed\r
+  @retval Others       Returned from PeiServicesInstallPpi()\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeimInitializeVariableServices (\r
-  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r
-  IN EFI_PEI_SERVICES          **PeiServices\r
+  IN       EFI_PEI_FILE_HANDLE       FileHandle,\r
+  IN CONST EFI_PEI_SERVICES          **PeiServices\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Provide the functionality of the variable services.\r
-\r
-Arguments:\r
-\r
-  FfsHeadher  - The FFS file header\r
-  PeiServices - General purpose services available to every PEIM.\r
+{\r
+  return PeiServicesInstallPpi (&mPpiListVariable);\r
+}\r
 \r
-Returns:\r
+/**\r
 \r
-  Status -  EFI_SUCCESS if the interface could be successfully\r
-            installed\r
+  Gets the pointer to the first variable header in given variable store area.\r
 \r
---*/\r
-{\r
-  //\r
-  // Publish the variable capability to other modules\r
-  //\r
-  return (**PeiServices).InstallPpi ((CONST EFI_PEI_SERVICES **)PeiServices, &mPpiListVariable);\r
+  @param VarStoreHeader  Pointer to the Variable Store Header.\r
 \r
-}\r
+  @return Pointer to the first variable header\r
 \r
+**/\r
 VARIABLE_HEADER *\r
 GetStartPointer (\r
   IN VARIABLE_STORE_HEADER       *VarStoreHeader\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the first variable memory pointer byte\r
-\r
-Arguments:\r
-\r
-  VarStoreHeader        Pointer to the Variable Store Header.\r
-\r
-Returns:\r
-\r
-  VARIABLE_HEADER*      Pointer to last unavailable Variable Header\r
-\r
---*/\r
 {\r
   //\r
   // The end of variable store\r
@@ -91,25 +73,19 @@ 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
-  This code gets the pointer to the last variable memory pointer byte\r
+  @param  VarStoreHeader  Pointer to the Variable Store Header.\r
 \r
-Arguments:\r
+  @return VARIABLE_HEADER* pointer to last unavailable Variable Header.\r
 \r
-  VarStoreHeader        Pointer to the Variable Store 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       *VarStoreHeader\r
+  )\r
 {\r
   //\r
   // The end of variable store\r
@@ -117,25 +93,20 @@ Returns:
   return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
 }\r
 \r
-BOOLEAN\r
-EFIAPI\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
@@ -145,108 +116,81 @@ 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 Size of variable in bytes in type UINTN.\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
+  This code gets the size of data of variable.\r
 \r
-  Variable            Pointer to the Variable Header.\r
+  @param  Variable  Pointer to the Variable Header.\r
 \r
-Returns:\r
+  @return Size of variable in bytes in type UINTN.\r
 \r
-  UINTN               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
+  @param   Variable  Pointer to the Variable Header.\r
 \r
-Returns:\r
+  @return  A CHAR16* pointer to Variable Name.\r
 \r
-  CHAR16*              Pointer to Variable Name\r
-\r
---*/\r
+**/\r
+CHAR16 *\r
+GetVariableNamePtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
 \r
   return (CHAR16 *) (Variable + 1);\r
 }\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  A UINT8* pointer to Variable Data.\r
 \r
---*/\r
+**/\r
+UINT8 *\r
+GetVariableDataPtr (\r
+  IN  VARIABLE_HEADER   *Variable\r
+  )\r
 {\r
   UINTN Value;\r
   \r
@@ -260,25 +204,19 @@ Returns:
   return (UINT8 *) Value;\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
-\r
-Returns:\r
+  @param  Variable  Pointer to the Variable Header.\r
 \r
-  VARIABLE_HEADER*      Pointer to next variable header.\r
+  @return  A 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
@@ -296,31 +234,23 @@ Returns:
   return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r
 }\r
 \r
+/**\r
+  This code gets the pointer to the variable name.\r
+\r
+  @param  VarStoreHeader  Pointer to the Variable Store Header.\r
 \r
+  @retval  EfiRaw      Variable store is raw\r
+  @retval  EfiValid    Variable store is valid\r
+  @retval  EfiInvalid  Variable store is invalid\r
+\r
+**/\r
 VARIABLE_STORE_STATUS\r
-EFIAPI\r
 GetVariableStoreStatus (\r
   IN VARIABLE_STORE_HEADER *VarStoreHeader\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the variable name.\r
-\r
-Arguments:\r
-\r
-  VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-Returns:\r
-\r
-  EfiRaw        Variable store is raw\r
-  EfiValid      Variable store is valid\r
-  EfiInvalid    Variable store is invalid\r
-\r
---*/\r
 {\r
-  if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r
+       \r
+  if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&\r
       VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
       VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
       ) {\r
@@ -328,7 +258,10 @@ Returns:
     return EfiValid;\r
   }\r
 \r
-  if (VarStoreHeader->Signature == 0xffffffff &&\r
+  if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
+      ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
+      ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
+      ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
       VarStoreHeader->Size == 0xffffffff &&\r
       VarStoreHeader->Format == 0xff &&\r
       VarStoreHeader->State == 0xff\r
@@ -340,6 +273,19 @@ Returns:
   }\r
 }\r
 \r
+\r
+/**\r
+  This function compares a variable with variable entries in database.\r
+\r
+  @param  Variable      Pointer to the variable in our database\r
+  @param  VariableName  Name of the variable to compare to 'Variable'\r
+  @param  VendorGuid    GUID of the variable to compare to 'Variable'\r
+  @param  PtrTrack      Variable Track Pointer structure that contains Variable Information.\r
+\r
+  @retval EFI_SUCCESS    Found match variable\r
+  @retval EFI_NOT_FOUND  Variable not found\r
+\r
+**/\r
 EFI_STATUS\r
 CompareWithValidVariable (\r
   IN  VARIABLE_HEADER               *Variable,\r
@@ -347,26 +293,6 @@ CompareWithValidVariable (
   IN  CONST EFI_GUID                *VendorGuid,\r
   OUT VARIABLE_POINTER_TRACK        *PtrTrack\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This function compares a variable with variable entries in database\r
-\r
-Arguments:\r
-\r
-  Variable       - Pointer to the variable in our database\r
-  VariableName   - Name of the variable to compare to 'Variable'\r
-  VendorGuid     - GUID of the variable to compare to 'Variable'\r
-  PtrTrack       - Variable Track Pointer structure that contains\r
-                   Variable Information.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS    - Found match variable\r
-  EFI_NOT_FOUND  - Variable not found\r
-\r
---*/\r
 {\r
   VOID  *Point;\r
 \r
@@ -386,7 +312,7 @@ Returns:
         ) {\r
       ASSERT (NameSizeOfVariable (Variable) != 0);\r
       Point = (VOID *) GetVariableNamePtr (Variable);\r
-      if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable))) {\r
+      if (CompareMem (VariableName, Point, NameSizeOfVariable (Variable)) == 0) {\r
         PtrTrack->CurrPtr = Variable;\r
         return EFI_SUCCESS;\r
       }\r
@@ -396,124 +322,185 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
-EFI_STATUS\r
-EFIAPI\r
-FindVariable (\r
-  IN CONST EFI_PEI_SERVICES   **PeiServices,\r
-  IN CONST  CHAR16            *VariableName,\r
-  IN CONST  EFI_GUID          *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack\r
+/**\r
+  Return the variable store header and the index table based on the Index.\r
+\r
+  @param Index      The index of the variable store.\r
+  @param IndexTable Return the index table.\r
+\r
+  @return  Pointer to the variable store header.\r
+**/\r
+VARIABLE_STORE_HEADER *\r
+GetVariableStore (\r
+  IN VARIABLE_STORE_TYPE         Type,\r
+  OUT VARIABLE_INDEX_TABLE       **IndexTable  OPTIONAL\r
   )\r
-/*++\r
+{\r
+  EFI_HOB_GUID_TYPE           *GuidHob;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;\r
+  VARIABLE_STORE_HEADER       *VariableStoreHeader;\r
+\r
+  if (IndexTable != NULL) {\r
+    *IndexTable       = NULL;\r
+  }\r
+  VariableStoreHeader = NULL;\r
+  switch (Type) {\r
+    case VariableStoreTypeHob:\r
+      GuidHob     = GetFirstGuidHob (&gEfiVariableGuid);\r
+      if (GuidHob != NULL) {\r
+        VariableStoreHeader = (VARIABLE_STORE_HEADER *) GET_GUID_HOB_DATA (GuidHob);\r
+      }\r
+      break;\r
 \r
-Routine Description:\r
+    case VariableStoreTypeNv:\r
+      if (GetBootModeHob () != BOOT_IN_RECOVERY_MODE) {\r
+        //\r
+        // The content of NV storage for variable is not reliable in recovery boot mode.\r
+        //\r
+        FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ? \r
+                                                           PcdGet64 (PcdFlashNvStorageVariableBase64) : \r
+                                                           PcdGet32 (PcdFlashNvStorageVariableBase)\r
+                                                          );\r
+        VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINT8 *) FvHeader + FvHeader->HeaderLength);\r
+\r
+        if (IndexTable != NULL) {\r
+          GuidHob = GetFirstGuidHob (&gEfiVariableIndexTableGuid);\r
+          if (GuidHob != NULL) {\r
+            *IndexTable = GET_GUID_HOB_DATA (GuidHob);\r
+          } else {\r
+            //\r
+            // If it's the first time to access variable region in flash, create a guid hob to record\r
+            // VAR_ADDED type variable info.\r
+            // Note that as the resource of PEI phase is limited, only store the limited number of \r
+            // VAR_ADDED type variables to reduce access time.\r
+            //\r
+            *IndexTable = BuildGuidHob (&gEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));\r
+            (*IndexTable)->Length      = 0;\r
+            (*IndexTable)->StartPtr    = GetStartPointer (VariableStoreHeader);\r
+            (*IndexTable)->EndPtr      = GetEndPointer   (VariableStoreHeader);\r
+            (*IndexTable)->GoneThrough = 0;\r
+          }\r
+        }\r
+      }\r
+      break;\r
 \r
-  This code finds variable in storage blocks (Non-Volatile)\r
+    default:\r
+      ASSERT (FALSE);\r
+      break;\r
+  }\r
 \r
-Arguments:\r
+  return VariableStoreHeader;\r
+}\r
 \r
-  PeiServices    - General purpose services available to every PEIM.\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
+/**\r
+  Find the variable in the specified variable store.\r
 \r
-Returns:\r
+  @param  VariableStoreHeader Pointer to the variable store header.\r
+  @param  IndexTable          Pointer to the index table.\r
+  @param  VariableName        Name of the variable to be found\r
+  @param  VendorGuid          Vendor GUID to be found.\r
+  @param  PtrTrack            Variable Track Pointer structure that contains Variable Information.\r
 \r
-  EFI_SUCCESS      - Variable found successfully\r
-  EFI_NOT_FOUND    - Variable not found\r
-  EFI_INVALID_PARAMETER  - Invalid variable name\r
+  @retval  EFI_SUCCESS            Variable found successfully\r
+  @retval  EFI_NOT_FOUND          Variable not found\r
+  @retval  EFI_INVALID_PARAMETER  Invalid variable name\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+FindVariableEx (\r
+  IN VARIABLE_STORE_HEADER       *VariableStoreHeader,\r
+  IN VARIABLE_INDEX_TABLE        *IndexTable,\r
+  IN CONST CHAR16                *VariableName,\r
+  IN CONST EFI_GUID              *VendorGuid,\r
+  OUT VARIABLE_POINTER_TRACK     *PtrTrack\r
+  )\r
 {\r
-  EFI_HOB_GUID_TYPE       *GuidHob;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader;\r
   VARIABLE_HEADER         *Variable;\r
+  VARIABLE_HEADER         *LastVariable;\r
   VARIABLE_HEADER         *MaxIndex;\r
-  VARIABLE_INDEX_TABLE    *IndexTable;\r
-  UINT32                  Count;\r
-  UINT8                   *VariableBase;\r
+  UINTN                   Index;\r
+  UINTN                   Offset;\r
+  BOOLEAN                 StopRecord;\r
 \r
-  if (VariableName != 0 && VendorGuid == NULL) {\r
+  if (VariableStoreHeader == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
+  if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (~VariableStoreHeader->Size == 0) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader);\r
+  PtrTrack->EndPtr   = GetEndPointer   (VariableStoreHeader);\r
+\r
   //\r
   // No Variable Address equals zero, so 0 as initial value is safe.\r
   //\r
-  MaxIndex = 0;\r
-\r
-  GuidHob = GetFirstGuidHob (&mEfiVariableIndexTableGuid);\r
-  if (GuidHob == NULL) {\r
-    IndexTable = BuildGuidHob (&mEfiVariableIndexTableGuid, sizeof (VARIABLE_INDEX_TABLE));\r
-    IndexTable->Length      = 0;\r
-    IndexTable->StartPtr    = NULL;\r
-    IndexTable->EndPtr      = NULL;\r
-    IndexTable->GoneThrough = 0;\r
-  } else {\r
-    IndexTable = GET_GUID_HOB_DATA (GuidHob);\r
-    for (Count = 0; Count < IndexTable->Length; Count++)\r
-    {\r
-      MaxIndex = GetVariableByIndex (IndexTable, Count);\r
+  MaxIndex   = NULL;\r
 \r
+  if (IndexTable != NULL) {\r
+    //\r
+    // traverse the variable index table to look for varible.\r
+    // The IndexTable->Index[Index] records the distance of two neighbouring VAR_ADDED type variables.\r
+    //\r
+    for (Offset = 0, Index = 0; Index < IndexTable->Length; Index++) {\r
+      ASSERT (Index < sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]));\r
+      Offset   += IndexTable->Index[Index];\r
+      MaxIndex  = (VARIABLE_HEADER *) ((UINT8 *) IndexTable->StartPtr + Offset);\r
       if (CompareWithValidVariable (MaxIndex, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
-        PtrTrack->StartPtr  = IndexTable->StartPtr;\r
-        PtrTrack->EndPtr    = IndexTable->EndPtr;\r
-\r
         return EFI_SUCCESS;\r
       }\r
     }\r
 \r
-    if (IndexTable->GoneThrough) {\r
+    if (IndexTable->GoneThrough != 0) {\r
+      //\r
+      // If the table has all the existing variables indexed and we still cannot find it.\r
+      //\r
       return EFI_NOT_FOUND;\r
     }\r
   }\r
-  //\r
-  // If not found in HOB, then let's start from the MaxIndex we've found.\r
-  //\r
+\r
   if (MaxIndex != NULL) {\r
-    Variable = GetNextVariablePtr (MaxIndex);\r
+    //\r
+    // HOB exists but the variable cannot be found in HOB\r
+    // If not found in HOB, then let's start from the MaxIndex we've found.\r
+    //\r
+    Variable     = GetNextVariablePtr (MaxIndex);\r
+    LastVariable = MaxIndex;\r
   } else {\r
-    if (IndexTable->StartPtr || IndexTable->EndPtr) {\r
-      Variable = IndexTable->StartPtr;\r
-    } else {\r
-      VariableBase = (UINT8 *) (UINTN) PcdGet32 (PcdFlashNvStorageVariableBase);\r
-      VariableStoreHeader = (VARIABLE_STORE_HEADER *) (VariableBase + \\r
-                            ((EFI_FIRMWARE_VOLUME_HEADER *) (VariableBase)) -> HeaderLength);\r
-\r
-      if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r
-        return EFI_UNSUPPORTED;\r
-      }\r
-\r
-      if (~VariableStoreHeader->Size == 0) {\r
-        return EFI_NOT_FOUND;\r
-      }\r
-      //\r
-      // Find the variable by walk through non-volatile variable store\r
-      //\r
-      IndexTable->StartPtr  = GetStartPointer (VariableStoreHeader);\r
-      IndexTable->EndPtr    = GetEndPointer (VariableStoreHeader);\r
-\r
-      //\r
-      // Start Pointers for the variable.\r
-      // Actual Data Pointer where data can be written.\r
-      //\r
-      Variable = IndexTable->StartPtr;\r
-    }\r
+    //\r
+    // Start Pointers for the variable.\r
+    // Actual Data Pointer where data can be written.\r
+    //\r
+    Variable     = PtrTrack->StartPtr;\r
+    LastVariable = PtrTrack->StartPtr;\r
   }\r
+\r
   //\r
   // Find the variable by walk through non-volatile variable store\r
   //\r
-  PtrTrack->StartPtr  = IndexTable->StartPtr;\r
-  PtrTrack->EndPtr    = IndexTable->EndPtr;\r
-\r
-  while (IsValidVariableHeader (Variable) && (Variable <= IndexTable->EndPtr)) {\r
+  StopRecord = FALSE;\r
+  while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {\r
     if (Variable->State == VAR_ADDED) {\r
       //\r
       // Record Variable in VariableIndex HOB\r
       //\r
-      if (IndexTable->Length < VARIABLE_INDEX_TABLE_VOLUME)\r
-      {\r
-        VariableIndexTableUpdate (IndexTable, Variable);\r
+      if ((IndexTable != NULL) && !StopRecord) {\r
+        Offset = (UINTN) Variable - (UINTN) LastVariable;\r
+        if ((Offset > 0x0FFFF) || (IndexTable->Length == sizeof (IndexTable->Index) / sizeof (IndexTable->Index[0]))) {\r
+          //\r
+          // Stop to record if the distance of two neighbouring VAR_ADDED variable is larger than the allowable scope(UINT16),\r
+          // or the record buffer is full.\r
+          //\r
+          StopRecord = TRUE;\r
+        } else {\r
+          IndexTable->Index[IndexTable->Length++] = (UINT16) Offset;\r
+          LastVariable = Variable;\r
+        }\r
       }\r
 \r
       if (CompareWithValidVariable (Variable, VariableName, VendorGuid, PtrTrack) == EFI_SUCCESS) {\r
@@ -526,7 +513,7 @@ Returns:
   //\r
   // If gone through the VariableStore, that means we never find in Firmware any more.\r
   //\r
-  if (IndexTable->Length < VARIABLE_INDEX_TABLE_VOLUME) {\r
+  if ((IndexTable != NULL) && !StopRecord) {\r
     IndexTable->GoneThrough = 1;\r
   }\r
 \r
@@ -535,6 +522,76 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  Find the variable in HOB and Non-Volatile variable storages.\r
+\r
+  @param  VariableName  Name of the variable to be found\r
+  @param  VendorGuid    Vendor GUID to be found.\r
+  @param  PtrTrack      Variable Track Pointer structure that contains Variable Information.\r
+\r
+  @retval  EFI_SUCCESS            Variable found successfully\r
+  @retval  EFI_NOT_FOUND          Variable not found\r
+  @retval  EFI_INVALID_PARAMETER  Invalid variable name\r
+**/\r
+EFI_STATUS\r
+FindVariable (\r
+  IN CONST  CHAR16            *VariableName,\r
+  IN CONST  EFI_GUID          *VendorGuid,\r
+  OUT VARIABLE_POINTER_TRACK  *PtrTrack\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  VARIABLE_STORE_HEADER       *VariableStoreHeader;\r
+  VARIABLE_INDEX_TABLE        *IndexTable;\r
+  VARIABLE_STORE_TYPE         Type;\r
+\r
+  if (VariableName[0] != 0 && VendorGuid == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
+    VariableStoreHeader = GetVariableStore (Type, &IndexTable);\r
+    Status = FindVariableEx (\r
+               VariableStoreHeader,\r
+               IndexTable,\r
+               VariableName,\r
+               VendorGuid, \r
+               PtrTrack\r
+               );\r
+    if (!EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  This service retrieves a variable's value using its name and GUID.\r
+\r
+  Read the specified variable from the UEFI variable store. If the Data \r
+  buffer is too small to hold the contents of the variable, the error\r
+  EFI_BUFFER_TOO_SMALL is returned and DataSize is set to the required buffer\r
+  size to obtain the data.\r
+\r
+  @param  This                  A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
+  @param  VariableName          A pointer to a null-terminated string that is the variable's name.\r
+  @param  VariableGuid          A pointer to an EFI_GUID that is the variable's GUID. The combination of\r
+                                VariableGuid and VariableName must be unique.\r
+  @param  Attributes            If non-NULL, on return, points to the variable's attributes.\r
+  @param  DataSize              On entry, points to the size in bytes of the Data buffer.\r
+                                On return, points to the size of the data returned in Data.\r
+  @param  Data                  Points to the buffer which will hold the returned variable value.\r
+\r
+  @retval EFI_SUCCESS           The variable was read successfully.\r
+  @retval EFI_NOT_FOUND         The variable could not be found.\r
+  @retval EFI_BUFFER_TOO_SMALL  The DataSize is too small for the resulting data. \r
+                                DataSize is updated with the size required for \r
+                                the specified variable.\r
+  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid, DataSize or Data is NULL.\r
+  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiGetVariable (\r
@@ -545,50 +602,20 @@ PeiGetVariable (
   IN OUT    UINTN                           *DataSize,\r
   OUT       VOID                            *Data\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Provide the read variable functionality of the variable services.\r
-\r
-Arguments:\r
-\r
-  PeiServices - General purpose services available to every PEIM.\r
-\r
-  VariableName     - The variable name\r
-\r
-  VendorGuid       - The vendor's GUID\r
-\r
-  Attributes       - Pointer to the attribute\r
-\r
-  DataSize         - Size of data\r
-\r
-  Data             - Pointer to data\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS           - The interface could be successfully installed\r
-\r
-  EFI_NOT_FOUND         - The variable could not be discovered\r
-\r
-  EFI_BUFFER_TOO_SMALL  - The caller buffer is not large enough\r
-\r
---*/\r
 {\r
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarDataSize;\r
   EFI_STATUS              Status;\r
-  CONST EFI_PEI_SERVICES  **PeiServices;\r
 \r
-  PeiServices = GetPeiServicesTablePointer ();\r
   if (VariableName == NULL || VariableGuid == NULL || DataSize == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
   //\r
   // Find existing variable\r
   //\r
-  Status = FindVariable (PeiServices, VariableName, VariableGuid, &Variable);\r
-  if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {\r
+  Status = FindVariable (VariableName, VariableGuid, &Variable);\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
   //\r
@@ -596,14 +623,11 @@ Returns:
   //\r
   VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r
   if (*DataSize >= VarDataSize) {\r
-    //\r
-    // PO-TKW: Address one checking in this place\r
-    //\r
     if (Data == NULL) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
-    (*PeiServices)->CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
+    CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
 \r
     if (Attributes != NULL) {\r
       *Attributes = Variable.CurrPtr->Attributes;\r
@@ -617,6 +641,35 @@ Returns:
   }\r
 }\r
 \r
+/**\r
+  Return the next variable name and GUID.\r
+\r
+  This function is called multiple times to retrieve the VariableName \r
+  and VariableGuid of all variables currently available in the system. \r
+  On each call, the previous results are passed into the interface, \r
+  and, on return, the interface returns the data for the next \r
+  interface. When the entire variable list has been returned, \r
+  EFI_NOT_FOUND is returned.\r
+\r
+  @param  This              A pointer to this instance of the EFI_PEI_READ_ONLY_VARIABLE2_PPI.\r
+\r
+  @param  VariableNameSize  On entry, points to the size of the buffer pointed to by VariableName.\r
+                            On return, the size of the variable name buffer.\r
+  @param  VariableName      On entry, a pointer to a null-terminated string that is the variable's name.\r
+                            On return, points to the next variable's null-terminated name string.\r
+  @param  VariableGuid      On entry, a pointer to an EFI_GUID that is the variable's GUID. \r
+                            On return, a pointer to the next variable's GUID.\r
+\r
+  @retval EFI_SUCCESS           The variable was read successfully.\r
+  @retval EFI_NOT_FOUND         The variable could not be found.\r
+  @retval EFI_BUFFER_TOO_SMALL  The VariableNameSize is too small for the resulting\r
+                                data. VariableNameSize is updated with the size\r
+                                required for the specified variable.\r
+  @retval EFI_INVALID_PARAMETER VariableName, VariableGuid or\r
+                                VariableNameSize is NULL.\r
+  @retval EFI_DEVICE_ERROR      The variable could not be retrieved because of a device error.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiGetNextVariableName (\r
@@ -625,44 +678,19 @@ PeiGetNextVariableName (
   IN OUT CHAR16                             *VariableName,\r
   IN OUT EFI_GUID                           *VariableGuid\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Provide the get next variable functionality of the variable services.\r
-\r
-Arguments:\r
-\r
-  PeiServices        - General purpose services available to every PEIM.\r
-  VariabvleNameSize  - The variable name's size.\r
-  VariableName       - A pointer to the variable's name.\r
-  VariableGuid       - A pointer to the EFI_GUID structure.\r
-\r
-  VariableNameSize - Size of the variable name\r
-\r
-  VariableName     - The variable name\r
-\r
-  VendorGuid       - The vendor's GUID\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - The interface could be successfully installed\r
-\r
-  EFI_NOT_FOUND - The variable could not be discovered\r
-\r
---*/\r
 {\r
+  VARIABLE_STORE_TYPE     Type;\r
   VARIABLE_POINTER_TRACK  Variable;\r
+  VARIABLE_POINTER_TRACK  VariableInHob;\r
   UINTN                   VarNameSize;\r
   EFI_STATUS              Status;\r
-  CONST EFI_PEI_SERVICES  **PeiServices;\r
+  VARIABLE_STORE_HEADER   *VariableStoreHeader[VariableStoreTypeMax];\r
 \r
-  PeiServices = GetPeiServicesTablePointer ();\r
   if (VariableName == NULL || VariableGuid == NULL || VariableNameSize == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Status = FindVariable (PeiServices, VariableName, VariableGuid, &Variable);\r
+  Status = FindVariable (VariableName, VariableGuid, &Variable);\r
   if (Variable.CurrPtr == NULL || Status != EFI_SUCCESS) {\r
     return Status;\r
   }\r
@@ -674,34 +702,88 @@ Returns:
     Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
   }\r
 \r
-  while (!(Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL)) {\r
-    if (IsValidVariableHeader (Variable.CurrPtr)) {\r
-      if (Variable.CurrPtr->State == VAR_ADDED) {\r
-        ASSERT (NameSizeOfVariable (Variable.CurrPtr) != 0);\r
+  VariableStoreHeader[VariableStoreTypeHob] = GetVariableStore (VariableStoreTypeHob, NULL);\r
+  VariableStoreHeader[VariableStoreTypeNv]  = GetVariableStore (VariableStoreTypeNv, NULL);\r
 \r
-        VarNameSize = (UINTN) NameSizeOfVariable (Variable.CurrPtr);\r
-        if (VarNameSize <= *VariableNameSize) {\r
-          (*PeiServices)->CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
+  while (TRUE) {\r
+    //\r
+    // Switch from HOB to Non-Volatile.\r
+    //\r
+    while ((Variable.CurrPtr >= Variable.EndPtr) ||\r
+           (Variable.CurrPtr == NULL)            ||\r
+           !IsValidVariableHeader (Variable.CurrPtr)\r
+          ) {\r
+      //\r
+      // Find current storage index\r
+      //\r
+      for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r
+        if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r
+          break;\r
+        }\r
+      }\r
+      ASSERT (Type < VariableStoreTypeMax);\r
+      //\r
+      // Switch to next storage\r
+      //\r
+      for (Type++; Type < VariableStoreTypeMax; Type++) {\r
+        if (VariableStoreHeader[Type] != NULL) {\r
+          break;\r
+        }\r
+      }\r
+      //\r
+      // Capture the case that \r
+      // 1. current storage is the last one, or\r
+      // 2. no further storage\r
+      //\r
+      if (Type == VariableStoreTypeMax) {\r
+        return EFI_NOT_FOUND;\r
+      }\r
+      Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r
+      Variable.EndPtr   = GetEndPointer   (VariableStoreHeader[Type]);\r
+      Variable.CurrPtr  = Variable.StartPtr;\r
+    }\r
 \r
-          (*PeiServices)->CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
+    if (Variable.CurrPtr->State == VAR_ADDED) {\r
 \r
-          Status = EFI_SUCCESS;\r
-        } else {\r
-          Status = EFI_BUFFER_TOO_SMALL;\r
+      //\r
+      // Don't return NV variable when HOB overrides it\r
+      //\r
+      if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r
+          (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r
+         ) {\r
+        Status = FindVariableEx (\r
+                   VariableStoreHeader[VariableStoreTypeHob],\r
+                   NULL,\r
+                   GetVariableNamePtr (Variable.CurrPtr),\r
+                   &Variable.CurrPtr->VendorGuid, \r
+                   &VariableInHob\r
+                   );\r
+        if (!EFI_ERROR (Status)) {\r
+          Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+          continue;\r
         }\r
+      }\r
 \r
-        *VariableNameSize = VarNameSize;\r
-        return Status;\r
-        //\r
-        // Variable is found\r
-        //\r
+      VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r
+      ASSERT (VarNameSize != 0);\r
+\r
+      if (VarNameSize <= *VariableNameSize) {\r
+        CopyMem (VariableName, GetVariableNamePtr (Variable.CurrPtr), VarNameSize);\r
+\r
+        CopyMem (VariableGuid, &Variable.CurrPtr->VendorGuid, sizeof (EFI_GUID));\r
+\r
+        Status = EFI_SUCCESS;\r
       } else {\r
-        Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
+        Status = EFI_BUFFER_TOO_SMALL;\r
       }\r
+\r
+      *VariableNameSize = VarNameSize;\r
+      //\r
+      // Variable is found\r
+      //\r
+      return Status;\r
     } else {\r
-      break;\r
+      Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
     }\r
   }\r
-\r
-  return EFI_NOT_FOUND;\r
 }\r