]> git.proxmox.com Git - mirror_edk2.git/blobdiff - DuetPkg/FSVariable/FSVariable.c
DuetPkg: Remove DuetPkg
[mirror_edk2.git] / DuetPkg / FSVariable / FSVariable.c
diff --git a/DuetPkg/FSVariable/FSVariable.c b/DuetPkg/FSVariable/FSVariable.c
deleted file mode 100644 (file)
index 5feeade..0000000
+++ /dev/null
@@ -1,1992 +0,0 @@
-/*++\r
-\r
-Caution: This file is used for Duet platform only, do not use them in real platform.\r
-All variable code, variable metadata, and variable data used by Duet platform are on \r
-disk. They can be changed by user. BIOS is not able to protoect those.\r
-Duet trusts all meta data from disk. If variable code, variable metadata and variable\r
-data is modified in inproper way, the behavior is undefined.\r
-\r
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
-  FSVariable.c\r
-\r
-Abstract:\r
-\r
-  Provide support functions for variable services.\r
-\r
---*/\r
-\r
-#include "FSVariable.h"\r
-\r
-VARIABLE_STORE_HEADER mStoreHeaderTemplate = {\r
-  VARIABLE_STORE_SIGNATURE,\r
-  VOLATILE_VARIABLE_STORE_SIZE,\r
-  VARIABLE_STORE_FORMATTED,\r
-  VARIABLE_STORE_HEALTHY,\r
-  0,\r
-  0\r
-};\r
-\r
-//\r
-// Don't use module globals after the SetVirtualAddress map is signaled\r
-//\r
-VARIABLE_GLOBAL  *mGlobal;\r
-\r
-/**\r
-  Update the variable region with Variable information. These are the same \r
-  arguments as the EFI Variable services.\r
-\r
-  @param[in] VariableName       Name of variable\r
-\r
-  @param[in] VendorGuid         Guid of variable\r
-\r
-  @param[in] Data               Variable data\r
-\r
-  @param[in] DataSize           Size of data. 0 means delete\r
-\r
-  @param[in] Attributes         Attribues of the variable\r
-\r
-  @param[in] Variable           The variable information which is used to keep track of variable usage.\r
-\r
-  @retval EFI_SUCCESS           The update operation is success.\r
-\r
-  @retval EFI_OUT_OF_RESOURCES  Variable region is full, can not write other data into this region.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-UpdateVariable (\r
-  IN      CHAR16                 *VariableName,\r
-  IN      EFI_GUID               *VendorGuid,\r
-  IN      VOID                   *Data,\r
-  IN      UINTN                  DataSize,\r
-  IN      UINT32                 Attributes OPTIONAL,\r
-  IN      VARIABLE_POINTER_TRACK *Variable\r
-  );\r
-\r
-VOID\r
-EFIAPI\r
-OnVirtualAddressChangeFsv (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  );\r
-\r
-VOID\r
-EFIAPI\r
-OnSimpleFileSystemInstall (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  );\r
-\r
-BOOLEAN\r
-IsValidVariableHeader (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code checks if variable header is valid or not.\r
-\r
-Arguments:\r
-  Variable        Pointer to the Variable Header.\r
-\r
-Returns:\r
-  TRUE            Variable header is valid.\r
-  FALSE           Variable header is not valid.\r
-\r
---*/\r
-{\r
-  if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-VARIABLE_STORE_STATUS\r
-GetVariableStoreStatus (\r
-  IN VARIABLE_STORE_HEADER *VarStoreHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the current status of Variable Store.\r
-\r
-Arguments:\r
-\r
-  VarStoreHeader  Pointer to the Variable Store Header.\r
-\r
-Returns:\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
-  if (CompareGuid (&VarStoreHeader->Signature, &mStoreHeaderTemplate.Signature) &&\r
-      (VarStoreHeader->Format == mStoreHeaderTemplate.Format) &&\r
-      (VarStoreHeader->State == mStoreHeaderTemplate.State)\r
-     ) {\r
-    return EfiValid;\r
-  } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == VAR_DEFAULT_VALUE_32 &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[1] == VAR_DEFAULT_VALUE_32 &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[2] == VAR_DEFAULT_VALUE_32 &&\r
-             ((UINT32 *)(&VarStoreHeader->Signature))[3] == VAR_DEFAULT_VALUE_32 &&\r
-             VarStoreHeader->Size == VAR_DEFAULT_VALUE_32 &&\r
-             VarStoreHeader->Format == VAR_DEFAULT_VALUE &&\r
-             VarStoreHeader->State == VAR_DEFAULT_VALUE\r
-          ) {\r
-\r
-    return EfiRaw;\r
-  } else {\r
-    return EfiInvalid;\r
-  }\r
-}\r
-\r
-UINT8 *\r
-GetVariableDataPtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the variable data.\r
-\r
-Arguments:\r
-\r
-  Variable            Pointer to the Variable Header.\r
-\r
-Returns:\r
-\r
-  UINT8*              Pointer to Variable Data\r
-\r
---*/\r
-{\r
-  //\r
-  // Be careful about pad size for alignment\r
-  //\r
-  return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
-}\r
-\r
-VARIABLE_HEADER *\r
-GetNextVariablePtr (\r
-  IN  VARIABLE_HEADER   *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code gets the pointer to the next variable header.\r
-\r
-Arguments:\r
-\r
-  Variable              Pointer to the Variable Header.\r
-\r
-Returns:\r
-\r
-  VARIABLE_HEADER*      Pointer to next variable header.\r
-\r
---*/\r
-{\r
-  if (!IsValidVariableHeader (Variable)) {\r
-    return NULL;\r
-  }\r
-  //\r
-  // Be careful about pad size for alignment\r
-  //\r
-  return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\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
-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
-  //\r
-  return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
-}\r
-\r
-BOOLEAN\r
-ExistNewerVariable (\r
-  IN  VARIABLE_HEADER         *Variable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check if exist newer variable when doing reclaim\r
-\r
-Arguments:\r
-\r
-  Variable                    Pointer to start position\r
-\r
-Returns:\r
-\r
-  TRUE - Exists another variable, which is newer than the current one\r
-  FALSE  - Doesn't exist another vairable which is newer than the current one\r
-\r
---*/\r
-{\r
-  VARIABLE_HEADER       *NextVariable;\r
-  CHAR16                *VariableName;\r
-  EFI_GUID              *VendorGuid;\r
-  \r
-  VendorGuid   = &Variable->VendorGuid;\r
-  VariableName = GET_VARIABLE_NAME_PTR(Variable);\r
-  \r
-  NextVariable = GetNextVariablePtr (Variable);\r
-  while (IsValidVariableHeader (NextVariable)) {\r
-    if ((NextVariable->State == VAR_ADDED) || (NextVariable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
-      //\r
-      // If match Guid and Name\r
-      //\r
-      if (CompareGuid (VendorGuid, &NextVariable->VendorGuid)) {\r
-         if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (NextVariable), StrSize (VariableName)) == 0) {\r
-           return TRUE;\r
-         }\r
-       }\r
-    }\r
-    NextVariable = GetNextVariablePtr (NextVariable);\r
-  }\r
-  return FALSE;\r
-}\r
-\r
-EFI_STATUS\r
-Reclaim (\r
-  IN  VARIABLE_STORAGE_TYPE StorageType,\r
-  IN  VARIABLE_HEADER       *CurrentVariable OPTIONAL\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Variable store garbage collection and reclaim operation\r
-\r
-Arguments:\r
-\r
-  IsVolatile                  The variable store is volatile or not,\r
-                              if it is non-volatile, need FTW\r
-  CurrentVairable             If it is not NULL, it means not to process\r
-                              current variable for Reclaim.\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
-{\r
-  VARIABLE_HEADER       *Variable;\r
-  VARIABLE_HEADER       *NextVariable;\r
-  VARIABLE_STORE_HEADER *VariableStoreHeader;\r
-  UINT8                 *ValidBuffer;\r
-  UINTN                 ValidBufferSize;\r
-  UINTN                 VariableSize;\r
-  UINT8                 *CurrPtr;\r
-  EFI_STATUS            Status;\r
-\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType];\r
-\r
-  //\r
-  // Start Pointers for the variable.\r
-  //\r
-  Variable        = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
-\r
-  //\r
-  // recaluate the total size of Common/HwErr type variables in non-volatile area.\r
-  //\r
-  if (!StorageType) {\r
-    mGlobal->CommonVariableTotalSize = 0;\r
-    mGlobal->HwErrVariableTotalSize  = 0;\r
-  }\r
-  //\r
-  // To make the reclaim, here we just allocate a memory that equal to the original memory\r
-  //\r
-  ValidBufferSize = sizeof (VARIABLE_STORE_HEADER) + VariableStoreHeader->Size;\r
-\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  ValidBufferSize,\r
-                  (VOID**) &ValidBuffer\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  CurrPtr = ValidBuffer;\r
-\r
-  //\r
-  // Copy variable store header\r
-  //\r
-  CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
-  CurrPtr += sizeof (VARIABLE_STORE_HEADER);\r
-\r
-  //\r
-  // Start Pointers for the variable.\r
-  //\r
-  Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
-\r
-  \r
-  ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
-  while (IsValidVariableHeader (Variable)) {\r
-    NextVariable = GetNextVariablePtr (Variable);\r
-    //\r
-    // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,\r
-    // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space\r
-    //\r
-    if (Variable->State == VAR_ADDED) {\r
-      VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-      CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
-      ValidBufferSize += VariableSize;\r
-      CurrPtr += VariableSize;\r
-      if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-        mGlobal->HwErrVariableTotalSize += VariableSize;\r
-      } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-        mGlobal->CommonVariableTotalSize += VariableSize;\r
-      }\r
-    } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r
-      //\r
-      // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,\r
-      // we will only save the latest valid one\r
-      //\r
-      if (!ExistNewerVariable(Variable)) {\r
-        VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r
-        CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
-        //\r
-        // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION\r
-        //\r
-        if (Variable != CurrentVariable){\r
-          ((VARIABLE_HEADER *)CurrPtr)->State = VAR_ADDED;\r
-        }\r
-        CurrPtr += VariableSize;\r
-        ValidBufferSize += VariableSize;\r
-        if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-          mGlobal->HwErrVariableTotalSize += VariableSize;\r
-        } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-          mGlobal->CommonVariableTotalSize += VariableSize;\r
-        }\r
-      }\r
-    }\r
-    Variable = NextVariable;\r
-  }\r
-\r
-  mGlobal->LastVariableOffset[StorageType] = ValidBufferSize;\r
-\r
-  //\r
-  // TODO: cannot restore to original state, basic FTW needed\r
-  //\r
-  Status = mGlobal->VariableStore[StorageType]->Erase (\r
-                                                  mGlobal->VariableStore[StorageType]\r
-                                                  );\r
-  Status = mGlobal->VariableStore[StorageType]->Write (\r
-                                                    mGlobal->VariableStore[StorageType],\r
-                                                    0,\r
-                                                    ValidBufferSize,\r
-                                                    ValidBuffer\r
-                                                    );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // If error, then reset the last variable offset to zero.\r
-    //\r
-    mGlobal->LastVariableOffset[StorageType] = 0;\r
-  };\r
-\r
-  gBS->FreePool (ValidBuffer);\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FindVariable (\r
-  IN  CHAR16                  *VariableName,\r
-  IN  EFI_GUID                *VendorGuid,\r
-  OUT VARIABLE_POINTER_TRACK  *PtrTrack\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
-\r
-Returns:\r
-\r
-  EFI_INVALID_PARAMETER       - Invalid parameter\r
-  EFI_SUCCESS                 - Find the specified variable\r
-  EFI_NOT_FOUND               - Not found\r
-\r
---*/\r
-{\r
-  VARIABLE_HEADER         *Variable;\r
-  VARIABLE_STORE_HEADER   *VariableStoreHeader;\r
-  UINTN                   Index;\r
-  VARIABLE_HEADER         *InDeleteVariable;\r
-  UINTN                   InDeleteIndex;\r
-  VARIABLE_HEADER         *InDeleteStartPtr;\r
-  VARIABLE_HEADER         *InDeleteEndPtr;\r
-\r
-  if (VariableName[0] != 0 && VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  InDeleteVariable = NULL;\r
-  InDeleteIndex    = (UINTN)-1;\r
-  InDeleteStartPtr = NULL;\r
-  InDeleteEndPtr   = NULL;\r
-\r
-  for (Index = 0; Index < MaxType; Index ++) {\r
-    //\r
-    // 0: Non-Volatile, 1: Volatile\r
-    //\r
-    VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Index];\r
-\r
-    //\r
-    // Start Pointers for the variable.\r
-    // Actual Data Pointer where data can be written.\r
-    //\r
-    Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
-\r
-    //\r
-    // Find the variable by walk through non-volatile and volatile variable store\r
-    //\r
-    PtrTrack->StartPtr = Variable;\r
-    PtrTrack->EndPtr   = GetEndPointer (VariableStoreHeader);\r
-\r
-    while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {\r
-      if (Variable->State == VAR_ADDED) {\r
-        if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
-          if (VariableName[0] == 0) {\r
-            PtrTrack->CurrPtr = Variable;\r
-            PtrTrack->Type    = (VARIABLE_STORAGE_TYPE) Index;\r
-            return EFI_SUCCESS;\r
-          } else {\r
-            if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r
-              if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r
-                PtrTrack->CurrPtr = Variable;\r
-                PtrTrack->Type    = (VARIABLE_STORAGE_TYPE) Index;\r
-                return EFI_SUCCESS;\r
-              }\r
-            }\r
-          }\r
-        }\r
-      } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r
-        //\r
-        // VAR_IN_DELETED_TRANSITION should also be checked.\r
-        //\r
-        if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
-          if (VariableName[0] == 0) {\r
-            InDeleteVariable = Variable;\r
-            InDeleteIndex    = Index;\r
-            InDeleteStartPtr = PtrTrack->StartPtr;\r
-            InDeleteEndPtr   = PtrTrack->EndPtr;\r
-          } else {\r
-            if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r
-              if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r
-                InDeleteVariable = Variable;\r
-                InDeleteIndex    = Index;\r
-                InDeleteStartPtr = PtrTrack->StartPtr;\r
-                InDeleteEndPtr   = PtrTrack->EndPtr;\r
-              }\r
-            }\r
-          }\r
-        }\r
-      }\r
-\r
-      Variable = GetNextVariablePtr (Variable);\r
-    }\r
-    //\r
-    // While (...)\r
-    //\r
-  }\r
-  //\r
-  // for (...)\r
-  //\r
-\r
-  //\r
-  // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,\r
-  // we return it.\r
-  //\r
-  if (InDeleteVariable != NULL) {\r
-    PtrTrack->CurrPtr  = InDeleteVariable;\r
-    PtrTrack->Type     = (VARIABLE_STORAGE_TYPE) InDeleteIndex;\r
-    PtrTrack->StartPtr = InDeleteStartPtr;\r
-    PtrTrack->EndPtr   = InDeleteEndPtr;\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
-  PtrTrack->CurrPtr = NULL;\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-/**\r
-  Get index from supported language codes according to language string.\r
-\r
-  This code is used to get corresponding index in supported language codes. It can handle\r
-  RFC4646 and ISO639 language tags.\r
-  In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
-  In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
-\r
-  For example:\r
-    SupportedLang  = "engfraengfra"\r
-    Lang           = "eng"\r
-    Iso639Language = TRUE\r
-  The return value is "0".\r
-  Another example:\r
-    SupportedLang  = "en;fr;en-US;fr-FR"\r
-    Lang           = "fr-FR"\r
-    Iso639Language = FALSE\r
-  The return value is "3".\r
-\r
-  @param  SupportedLang               Platform supported language codes.\r
-  @param  Lang                        Configured language.\r
-  @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
-\r
-  @retval the index of language in the language codes.\r
-\r
-**/\r
-UINTN\r
-GetIndexFromSupportedLangCodes(\r
-  IN  CHAR8            *SupportedLang,\r
-  IN  CHAR8            *Lang,\r
-  IN  BOOLEAN          Iso639Language\r
-  ) \r
-{\r
-  UINTN    Index;\r
-  UINTN    CompareLength;\r
-  UINTN    LanguageLength;\r
-\r
-  if (Iso639Language) {\r
-    CompareLength = ISO_639_2_ENTRY_SIZE;\r
-    for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
-      if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
-        //\r
-        // Successfully find the index of Lang string in SupportedLang string.\r
-        //\r
-        Index = Index / CompareLength;\r
-        return Index;\r
-      }\r
-    }\r
-    ASSERT (FALSE);\r
-    return 0;\r
-  } else {\r
-    //\r
-    // Compare RFC4646 language code\r
-    //\r
-    Index = 0;\r
-    for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r
-\r
-    for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r
-      //\r
-      // Skip ';' characters in SupportedLang\r
-      //\r
-      for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r
-      //\r
-      // Determine the length of the next language code in SupportedLang\r
-      //\r
-      for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r
-      \r
-      if ((CompareLength == LanguageLength) && \r
-          (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r
-        //\r
-        // Successfully find the index of Lang string in SupportedLang string.\r
-        //\r
-        return Index;\r
-      }\r
-    }\r
-    ASSERT (FALSE);\r
-    return 0;\r
-  }\r
-}\r
-\r
-/**\r
-  Get language string from supported language codes according to index.\r
-\r
-  This code is used to get corresponding language string in supported language codes. It can handle\r
-  RFC4646 and ISO639 language tags.\r
-  In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
-  In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
-\r
-  For example:\r
-    SupportedLang  = "engfraengfra"\r
-    Index          = "1"\r
-    Iso639Language = TRUE\r
-  The return value is "fra".\r
-  Another example:\r
-    SupportedLang  = "en;fr;en-US;fr-FR"\r
-    Index          = "1"\r
-    Iso639Language = FALSE\r
-  The return value is "fr".\r
-\r
-  @param  SupportedLang               Platform supported language codes.\r
-  @param  Index                       the index in supported language codes.\r
-  @param  Iso639Language              A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
-\r
-  @retval the language string in the language codes.\r
-\r
-**/\r
-CHAR8 *\r
-GetLangFromSupportedLangCodes (\r
-  IN  CHAR8            *SupportedLang,\r
-  IN  UINTN            Index,\r
-  IN  BOOLEAN          Iso639Language\r
-)\r
-{\r
-  UINTN    SubIndex;\r
-  UINTN    CompareLength;\r
-  CHAR8    *Supported;\r
-\r
-  SubIndex  = 0;\r
-  Supported = SupportedLang;\r
-  if (Iso639Language) {\r
-    //\r
-    // according to the index of Lang string in SupportedLang string to get the language.\r
-    // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
-    // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
-    //\r
-    CompareLength = ISO_639_2_ENTRY_SIZE;\r
-    mGlobal->Lang[CompareLength] = '\0';\r
-    return CopyMem (mGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
-\r
-  } else {\r
-    while (TRUE) {\r
-      //\r
-      // take semicolon as delimitation, sequentially traverse supported language codes.\r
-      //\r
-      for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
-        Supported++;\r
-      }\r
-      if ((*Supported == '\0') && (SubIndex != Index)) {\r
-        //\r
-        // Have completed the traverse, but not find corrsponding string.\r
-        // This case is not allowed to happen.\r
-        //\r
-        ASSERT(FALSE);\r
-        return NULL;\r
-      }\r
-      if (SubIndex == Index) {\r
-        //\r
-        // according to the index of Lang string in SupportedLang string to get the language.\r
-        // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
-        // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
-        //\r
-        mGlobal->PlatformLang[CompareLength] = '\0';\r
-        return CopyMem (mGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
-      }\r
-      SubIndex++;\r
-\r
-      //\r
-      // Skip ';' characters in Supported\r
-      //\r
-      for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
-    }\r
-  }\r
-}\r
-\r
-/**\r
-  Returns a pointer to an allocated buffer that contains the best matching language \r
-  from a set of supported languages.  \r
-  \r
-  This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
-  code types may not be mixed in a single call to this function. This function\r
-  supports a variable argument list that allows the caller to pass in a prioritized\r
-  list of language codes to test against all the language codes in SupportedLanguages.\r
-\r
-  If SupportedLanguages is NULL, then ASSERT().\r
-\r
-  @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string that\r
-                                  contains a set of language codes in the format \r
-                                  specified by Iso639Language.\r
-  @param[in]  Iso639Language      If TRUE, then all language codes are assumed to be\r
-                                  in ISO 639-2 format.  If FALSE, then all language\r
-                                  codes are assumed to be in RFC 4646 language format\r
-  @param[in]  ...                 A variable argument list that contains pointers to \r
-                                  Null-terminated ASCII strings that contain one or more\r
-                                  language codes in the format specified by Iso639Language.\r
-                                  The first language code from each of these language\r
-                                  code lists is used to determine if it is an exact or\r
-                                  close match to any of the language codes in \r
-                                  SupportedLanguages.  Close matches only apply to RFC 4646\r
-                                  language codes, and the matching algorithm from RFC 4647\r
-                                  is used to determine if a close match is present.  If \r
-                                  an exact or close match is found, then the matching\r
-                                  language code from SupportedLanguages is returned.  If\r
-                                  no matches are found, then the next variable argument\r
-                                  parameter is evaluated.  The variable argument list \r
-                                  is terminated by a NULL.\r
-\r
-  @retval NULL   The best matching language could not be found in SupportedLanguages.\r
-  @retval NULL   There are not enough resources available to return the best matching \r
-                 language.\r
-  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching \r
-                 language in SupportedLanguages.\r
-\r
-**/\r
-CHAR8 *\r
-EFIAPI\r
-VariableGetBestLanguage (\r
-  IN CONST CHAR8  *SupportedLanguages, \r
-  IN BOOLEAN      Iso639Language,\r
-  ...\r
-  )\r
-{\r
-  VA_LIST      Args;\r
-  CHAR8        *Language;\r
-  UINTN        CompareLength;\r
-  UINTN        LanguageLength;\r
-  CONST CHAR8  *Supported;\r
-  CHAR8        *Buffer;\r
-\r
-  ASSERT (SupportedLanguages != NULL);\r
-\r
-  VA_START (Args, Iso639Language);\r
-  while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r
-    //\r
-    // Default to ISO 639-2 mode\r
-    //\r
-    CompareLength  = 3;\r
-    LanguageLength = MIN (3, AsciiStrLen (Language));\r
-\r
-    //\r
-    // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
-    //\r
-    if (!Iso639Language) {\r
-      for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
-    }\r
-\r
-    //\r
-    // Trim back the length of Language used until it is empty\r
-    //\r
-    while (LanguageLength > 0) {\r
-      //\r
-      // Loop through all language codes in SupportedLanguages\r
-      //\r
-      for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r
-        //\r
-        // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
-        //\r
-        if (!Iso639Language) {\r
-          //\r
-          // Skip ';' characters in Supported\r
-          //\r
-          for (; *Supported != '\0' && *Supported == ';'; Supported++);\r
-          //\r
-          // Determine the length of the next language code in Supported\r
-          //\r
-          for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r
-          //\r
-          // If Language is longer than the Supported, then skip to the next language\r
-          //\r
-          if (LanguageLength > CompareLength) {\r
-            continue;\r
-          }\r
-        }\r
-        //\r
-        // See if the first LanguageLength characters in Supported match Language\r
-        //\r
-        if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r
-          VA_END (Args);\r
-\r
-          Buffer = Iso639Language ? mGlobal->Lang : mGlobal->PlatformLang;\r
-          Buffer[CompareLength] = '\0';\r
-          return CopyMem (Buffer, Supported, CompareLength);\r
-        }\r
-      }\r
-\r
-      if (Iso639Language) {\r
-        //\r
-        // If ISO 639 mode, then each language can only be tested once\r
-        //\r
-        LanguageLength = 0;\r
-      } else {\r
-        //\r
-        // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
-        //\r
-        for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
-      }\r
-    }\r
-  }\r
-  VA_END (Args);\r
-\r
-  //\r
-  // No matches were found \r
-  //\r
-  return NULL;\r
-}\r
-\r
-/**\r
-  Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
-\r
-  When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
-\r
-  According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
-  and are read-only. Therefore, in variable driver, only store the original value for other use.\r
-\r
-  @param[in] VariableName       Name of variable\r
-\r
-  @param[in] Data               Variable data\r
-\r
-  @param[in] DataSize           Size of data. 0 means delete\r
-\r
-**/\r
-VOID\r
-AutoUpdateLangVariable(\r
-  IN  CHAR16             *VariableName,\r
-  IN  VOID               *Data,\r
-  IN  UINTN              DataSize\r
-  )\r
-{\r
-  EFI_STATUS             Status;\r
-  CHAR8                  *BestPlatformLang;\r
-  CHAR8                  *BestLang;\r
-  UINTN                  Index;\r
-  UINT32                 Attributes;\r
-  VARIABLE_POINTER_TRACK Variable;\r
-  BOOLEAN                SetLanguageCodes;\r
-\r
-  //\r
-  // Don't do updates for delete operation\r
-  //\r
-  if (DataSize == 0) {\r
-    return;\r
-  }\r
-\r
-  SetLanguageCodes = FALSE;\r
-\r
-  if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
-    //\r
-    // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r
-    //\r
-    if (EfiAtRuntime ()) {\r
-      return;\r
-    }\r
-\r
-    SetLanguageCodes = TRUE;\r
-\r
-    //\r
-    // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
-    // Therefore, in variable driver, only store the original value for other use.\r
-    //\r
-    if (mGlobal->PlatformLangCodes != NULL) {\r
-      FreePool (mGlobal->PlatformLangCodes);\r
-    }\r
-    mGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
-    ASSERT (mGlobal->PlatformLangCodes != NULL);\r
-\r
-    //\r
-    // PlatformLang holds a single language from PlatformLangCodes, \r
-    // so the size of PlatformLangCodes is enough for the PlatformLang.\r
-    //\r
-    if (mGlobal->PlatformLang != NULL) {\r
-      FreePool (mGlobal->PlatformLang);\r
-    }\r
-    mGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r
-    ASSERT (mGlobal->PlatformLang != NULL);\r
-\r
-  } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
-    //\r
-    // LangCodes is a volatile variable, so it can not be updated at runtime.\r
-    //\r
-    if (EfiAtRuntime ()) {\r
-      return;\r
-    }\r
-\r
-    SetLanguageCodes = TRUE;\r
-\r
-    //\r
-    // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
-    // Therefore, in variable driver, only store the original value for other use.\r
-    //\r
-    if (mGlobal->LangCodes != NULL) {\r
-      FreePool (mGlobal->LangCodes);\r
-    }\r
-    mGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r
-    ASSERT (mGlobal->LangCodes != NULL);\r
-  }\r
-\r
-  if (SetLanguageCodes \r
-      && (mGlobal->PlatformLangCodes != NULL)\r
-      && (mGlobal->LangCodes != NULL)) {\r
-    //\r
-    // Update Lang if PlatformLang is already set\r
-    // Update PlatformLang if Lang is already set\r
-    //\r
-    Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // Update Lang\r
-      //\r
-      VariableName = L"PlatformLang";\r
-      Data         = GetVariableDataPtr (Variable.CurrPtr);\r
-      DataSize     = Variable.CurrPtr->DataSize;\r
-    } else {\r
-      Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // Update PlatformLang\r
-        //\r
-        VariableName = L"Lang";\r
-        Data         = GetVariableDataPtr (Variable.CurrPtr);\r
-        DataSize     = Variable.CurrPtr->DataSize;\r
-      } else {\r
-        //\r
-        // Neither PlatformLang nor Lang is set, directly return\r
-        //\r
-        return;\r
-      }\r
-    }\r
-  }\r
-  \r
-  //\r
-  // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
-  //\r
-  Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
-\r
-  if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
-    //\r
-    // Update Lang when PlatformLangCodes/LangCodes were set.\r
-    //\r
-    if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
-      //\r
-      // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
-      //\r
-      BestPlatformLang = VariableGetBestLanguage (mGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
-      if (BestPlatformLang != NULL) {\r
-        //\r
-        // Get the corresponding index in language codes.\r
-        //\r
-        Index = GetIndexFromSupportedLangCodes (mGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
-\r
-        //\r
-        // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
-        //\r
-        BestLang = GetLangFromSupportedLangCodes (mGlobal->LangCodes, Index, TRUE);\r
-\r
-        //\r
-        // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
-        //\r
-        FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable);\r
-\r
-        Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
-\r
-        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
-\r
-        ASSERT_EFI_ERROR(Status);\r
-      }\r
-    }\r
-\r
-  } else if (StrCmp (VariableName, L"Lang") == 0) {\r
-    //\r
-    // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
-    //\r
-    if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r
-      //\r
-      // When setting Lang, firstly get most matched language string from supported language codes.\r
-      //\r
-      BestLang = VariableGetBestLanguage (mGlobal->LangCodes, TRUE, Data, NULL);\r
-      if (BestLang != NULL) {\r
-        //\r
-        // Get the corresponding index in language codes.\r
-        //\r
-        Index = GetIndexFromSupportedLangCodes (mGlobal->LangCodes, BestLang, TRUE);\r
-\r
-        //\r
-        // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
-        //\r
-        BestPlatformLang = GetLangFromSupportedLangCodes (mGlobal->PlatformLangCodes, Index, FALSE);\r
-\r
-        //\r
-        // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
-        //\r
-        FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r
-\r
-        Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r
-                                 AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
-\r
-        DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
-        ASSERT_EFI_ERROR (Status);\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-/**\r
-  Update the variable region with Variable information. These are the same \r
-  arguments as the EFI Variable services.\r
-\r
-  @param[in] VariableName       Name of variable\r
-\r
-  @param[in] VendorGuid         Guid of variable\r
-\r
-  @param[in] Data               Variable data\r
-\r
-  @param[in] DataSize           Size of data. 0 means delete\r
-\r
-  @param[in] Attributes         Attribues of the variable\r
-\r
-  @param[in] Variable           The variable information which is used to keep track of variable usage.\r
-\r
-  @retval EFI_SUCCESS           The update operation is success.\r
-\r
-  @retval EFI_OUT_OF_RESOURCES  Variable region is full, can not write other data into this region.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-UpdateVariable (\r
-  IN      CHAR16                 *VariableName,\r
-  IN      EFI_GUID               *VendorGuid,\r
-  IN      VOID                   *Data,\r
-  IN      UINTN                  DataSize,\r
-  IN      UINT32                 Attributes OPTIONAL,\r
-  IN      VARIABLE_POINTER_TRACK *Variable\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  VARIABLE_HEADER                     *NextVariable;\r
-  UINTN                               VarNameOffset;\r
-  UINTN                               VarDataOffset;\r
-  UINTN                               VarNameSize;\r
-  UINTN                               VarSize;\r
-  UINT8                               State;\r
-  BOOLEAN                             Reclaimed;\r
-  VARIABLE_STORAGE_TYPE               StorageType;\r
-\r
-  Reclaimed         = FALSE;\r
-\r
-  if (Variable->CurrPtr != NULL) {  \r
-    //\r
-    // Update/Delete existing variable\r
-    //\r
-    \r
-    if (EfiAtRuntime ()) {              \r
-      //\r
-      // If EfiAtRuntime and the variable is Volatile and Runtime Access,  \r
-      // the volatile is ReadOnly, and SetVariable should be aborted and \r
-      // return EFI_WRITE_PROTECTED.\r
-      //\r
-      if (Variable->Type == Volatile) {\r
-        return EFI_WRITE_PROTECTED;\r
-      }\r
-      //\r
-      // Only variable have NV attribute can be updated/deleted in Runtime\r
-      //\r
-      if (!(Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
-        return EFI_INVALID_PARAMETER;      \r
-      }\r
-    }\r
-    \r
-    //\r
-    // Setting a data variable with no access, or zero DataSize attributes\r
-    // specified causes it to be deleted.\r
-    //\r
-    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
-      //\r
-      // Found this variable in storage\r
-      //\r
-      State = Variable->CurrPtr->State;\r
-      State &= VAR_DELETED;\r
-\r
-      Status = mGlobal->VariableStore[Variable->Type]->Write (\r
-                                                        mGlobal->VariableStore[Variable->Type],\r
-                                                        VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
-                                                        sizeof (Variable->CurrPtr->State),\r
-                                                        &State\r
-                                                        );\r
-      //\r
-      // NOTE: Write operation at least can write data to memory cache\r
-      //       Discard file writing failure here.\r
-      //\r
-      return EFI_SUCCESS;\r
-    }\r
-    \r
-    //\r
-    // Found this variable in storage\r
-    // If the variable is marked valid and the same data has been passed in\r
-    // then return to the caller immediately.\r
-    //\r
-    if ((Variable->CurrPtr->DataSize == DataSize) &&\r
-        (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)\r
-          ) {\r
-      return EFI_SUCCESS;\r
-    } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r
-               (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
-      //\r
-      // Mark the old variable as in delete transition\r
-      //\r
-      State = Variable->CurrPtr->State;\r
-      State &= VAR_IN_DELETED_TRANSITION;\r
-\r
-      Status = mGlobal->VariableStore[Variable->Type]->Write (\r
-                                                        mGlobal->VariableStore[Variable->Type],\r
-                                                        VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
-                                                        sizeof (Variable->CurrPtr->State),\r
-                                                        &State\r
-                                                        );\r
-      //\r
-      // NOTE: Write operation at least can write data to memory cache\r
-      //       Discard file writing failure here.\r
-      //\r
-    }\r
-  } else {\r
-    //\r
-    // Create a new variable\r
-    //  \r
-    \r
-    //\r
-    // Make sure we are trying to create a new variable.\r
-    // Setting a data variable with no access, or zero DataSize attributes means to delete it.    \r
-    //\r
-    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
-      return EFI_NOT_FOUND;\r
-    }    \r
-    //\r
-    // 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
-      return EFI_INVALID_PARAMETER;\r
-    }        \r
-    \r
-  } \r
-\r
-  //\r
-  // Function part - create a new variable and copy the data.\r
-  // Both update a variable and create a variable will come here.  \r
-  // We can firstly write all the data in memory, then write them to file\r
-  // This can reduce the times of write operation\r
-  //\r
-  \r
-  NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;\r
-\r
-  NextVariable->StartId     = VARIABLE_DATA;\r
-  NextVariable->Attributes  = Attributes;\r
-  NextVariable->State       = VAR_ADDED;\r
-  NextVariable->Reserved    = 0;\r
-  VarNameOffset             = sizeof (VARIABLE_HEADER);\r
-  VarNameSize               = StrSize (VariableName);\r
-  CopyMem (\r
-    (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
-    VariableName,\r
-    VarNameSize\r
-    );\r
-  VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
-  CopyMem (\r
-    (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
-    Data,\r
-    DataSize\r
-    );\r
-  CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
-  //\r
-  // There will be pad bytes after Data, the NextVariable->NameSize and\r
-  // NextVariable->DataSize should not include pad size so that variable\r
-  // service can get actual size in GetVariable\r
-  //\r
-  NextVariable->NameSize  = (UINT32)VarNameSize;\r
-  NextVariable->DataSize  = (UINT32)DataSize;\r
-\r
-  //\r
-  // The actual size of the variable that stores in storage should\r
-  // include pad size.\r
-  // VarDataOffset: offset from begin of current variable header\r
-  //\r
-  VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
-\r
-  StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;\r
-\r
-  if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
-      ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
-      ) {\r
-    if ((StorageType == NonVolatile) && EfiAtRuntime ()) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-    //\r
-    // Perform garbage collection & reclaim operation\r
-    //\r
-    Status = Reclaim (StorageType, Variable->CurrPtr);\r
-    if (EFI_ERROR (Status)) {\r
-      //\r
-      // Reclaim error\r
-      // we cannot restore to original state, fetal error, report to user\r
-      //\r
-      DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));\r
-      return Status;\r
-    }\r
-    //\r
-    // If still no enough space, return out of resources\r
-    //\r
-    if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r
-        ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r
-       ) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-\r
-    Reclaimed = TRUE;\r
-  }\r
-  Status = mGlobal->VariableStore[StorageType]->Write (\r
-                                                  mGlobal->VariableStore[StorageType],\r
-                                                  mGlobal->LastVariableOffset[StorageType],\r
-                                                  VarSize,\r
-                                                  NextVariable\r
-                                                  );\r
-  //\r
-  // NOTE: Write operation at least can write data to memory cache\r
-  //       Discard file writing failure here.\r
-  //\r
-  mGlobal->LastVariableOffset[StorageType] += VarSize;\r
-\r
-  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
-    mGlobal->HwErrVariableTotalSize += VarSize;\r
-  } else {\r
-    mGlobal->CommonVariableTotalSize += VarSize;\r
-  }\r
-\r
-  //\r
-  // Mark the old variable as deleted\r
-  //\r
-  if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r
-    State = Variable->CurrPtr->State;\r
-    State &= VAR_DELETED;\r
-\r
-    Status = mGlobal->VariableStore[StorageType]->Write (\r
-                                                    mGlobal->VariableStore[StorageType],\r
-                                                    VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r
-                                                    sizeof (Variable->CurrPtr->State),\r
-                                                    &State\r
-                                                    );\r
-    //\r
-    // NOTE: Write operation at least can write data to memory cache\r
-    //       Discard file writing failure here.\r
-    //\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-DuetGetVariable (\r
-  IN      CHAR16            *VariableName,\r
-  IN      EFI_GUID          *VendorGuid,\r
-  OUT     UINT32            *Attributes OPTIONAL,\r
-  IN OUT  UINTN             *DataSize,\r
-  OUT     VOID              *Data OPTIONAL\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                            The buffer to return the contents of the variable. May be NULL\r
-                                  with a zero DataSize in order to determine the size buffer needed.\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
-{\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarDataSize;\r
-  EFI_STATUS              Status;\r
-\r
-  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (VariableName[0] == 0) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  //\r
-  // Find existing variable\r
-  //\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable);\r
-\r
-  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-  //\r
-  // Get data size\r
-  //\r
-  VarDataSize = Variable.CurrPtr->DataSize;\r
-  if (*DataSize >= VarDataSize) {\r
-    if (Data == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
-\r
-    if (Attributes != NULL) {\r
-      *Attributes = Variable.CurrPtr->Attributes;\r
-    }\r
-\r
-    *DataSize = VarDataSize;\r
-\r
-    return EFI_SUCCESS;\r
-  } else {\r
-    *DataSize = VarDataSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GetNextVariableName (\r
-  IN OUT  UINTN             *VariableNameSize,\r
-  IN OUT  CHAR16            *VariableName,\r
-  IN OUT  EFI_GUID          *VendorGuid\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code Finds the Next available variable\r
-\r
-Arguments:\r
-\r
-  VariableNameSize            The size of the VariableName buffer. The size must be large\r
-                              enough to fit input string supplied in VariableName buffer.\r
-  VariableName                Pointer to variable name\r
-  VendorGuid                  Variable Vendor Guid\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-\r
---*/\r
-{\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  UINTN                   VarNameSize;\r
-  EFI_STATUS              Status;\r
-  UINTN                   MaxLen;\r
-\r
-  if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Calculate the possible maximum length of name string, including the Null terminator.\r
-  //\r
-  MaxLen = *VariableNameSize / sizeof (CHAR16);\r
-  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
-    //\r
-    // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
-    // follow spec to return EFI_INVALID_PARAMETER.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable);\r
-\r
-  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    //\r
-    // For VariableName is an empty string, FindVariable() will try to find and return\r
-    // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
-    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
-    //\r
-    if (VariableName[0] != 0) {\r
-      //\r
-      // For VariableName is not an empty string, and FindVariable() returns error as\r
-      // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
-      // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
-      //\r
-      Status = EFI_INVALID_PARAMETER;\r
-    }\r
-    return Status;\r
-  }\r
-\r
-  if (VariableName[0] != 0) {\r
-    //\r
-    // If variable name is not NULL, get next variable\r
-    //\r
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-  }\r
-\r
-  while (TRUE) {\r
-    //\r
-    // The order we find variable is: 1). NonVolatile; 2). Volatile\r
-    // If both volatile and non-volatile variable store are parsed,\r
-    // return not found\r
-    //\r
-    if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
-      if (Variable.Type == Volatile) {\r
-        //\r
-        // Since we met the end of Volatile storage, we have parsed all the stores.\r
-        //\r
-        return EFI_NOT_FOUND;\r
-      }\r
-\r
-      //\r
-      // End of NonVolatile, continue to parse Volatile\r
-      //\r
-      Variable.Type = Volatile;\r
-      Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);\r
-      Variable.EndPtr   = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);\r
-\r
-      Variable.CurrPtr = Variable.StartPtr;\r
-      if (!IsValidVariableHeader (Variable.CurrPtr)) {\r
-        continue;\r
-      }\r
-    }\r
-    //\r
-    // Variable is found\r
-    //\r
-    if (IsValidVariableHeader (Variable.CurrPtr) &&\r
-        ((Variable.CurrPtr->State == VAR_ADDED) ||\r
-         (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {\r
-      if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
-        VarNameSize = Variable.CurrPtr->NameSize;\r
-        if (VarNameSize <= *VariableNameSize) {\r
-          CopyMem (\r
-            VariableName,\r
-            GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
-            VarNameSize\r
-            );\r
-          CopyMem (\r
-            VendorGuid,\r
-            &Variable.CurrPtr->VendorGuid,\r
-            sizeof (EFI_GUID)\r
-            );\r
-          Status = EFI_SUCCESS;\r
-        } else {\r
-          Status = EFI_BUFFER_TOO_SMALL;\r
-        }\r
-\r
-        *VariableNameSize = VarNameSize;\r
-        return Status;\r
-      }\r
-    }\r
-\r
-    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
-  }\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SetVariable (\r
-  IN CHAR16                  *VariableName,\r
-  IN EFI_GUID                *VendorGuid,\r
-  IN UINT32                  Attributes,\r
-  IN UINTN                   DataSize,\r
-  IN VOID                    *Data\r
-  )\r
-/*++\r
-\r
-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
-\r
-Returns:\r
-  \r
-  EFI_INVALID_PARAMETER           - Invalid parameter\r
-  EFI_SUCCESS                     - Set successfully\r
-  EFI_OUT_OF_RESOURCES            - Resource not enough to set variable\r
-  EFI_NOT_FOUND                   - Not found\r
-  EFI_DEVICE_ERROR                - Variable can not be saved due to hardware failure\r
-  EFI_WRITE_PROTECTED             - Variable is read-only\r
-\r
---*/\r
-{\r
-  VARIABLE_POINTER_TRACK  Variable;\r
-  EFI_STATUS              Status;\r
-\r
-  //\r
-  // Check input parameters\r
-  // \r
-  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  \r
-  if (DataSize != 0 && Data == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Not support authenticated variable write yet.\r
-  //\r
-  if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  //  Make sure if runtime bit is set, boot service bit is set also\r
-  //\r
-  if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  \r
-  //\r
-  //  The size of the VariableName, including the Unicode Null in bytes plus\r
-  //  the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r
-  //  bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r
-  //\r
-  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    if ((DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) ||                                                       \r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-    //\r
-    // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
-    //\r
-    if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-  } else {\r
-    if ((DataSize > PcdGet32(PcdMaxVariableSize)) ||\r
-        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxVariableSize))) {\r
-      return EFI_INVALID_PARAMETER;\r
-    }  \r
-  }  \r
-\r
-  //\r
-  // Check whether the input variable is already existed\r
-  //\r
-  Status = FindVariable (VariableName, VendorGuid, &Variable);\r
-\r
-  //\r
-  // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
-  //\r
-  AutoUpdateLangVariable (VariableName, Data, DataSize);\r
-\r
-  Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-QueryVariableInfo (\r
-  IN  UINT32                 Attributes,\r
-  OUT UINT64                 *MaximumVariableStorageSize,\r
-  OUT UINT64                 *RemainingVariableStorageSize,\r
-  OUT UINT64                 *MaximumVariableSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This code returns information about the EFI variables.\r
-\r
-Arguments:\r
-\r
-  Attributes                      Attributes bitmask to specify the type of variables\r
-                                  on which to return information.\r
-  MaximumVariableStorageSize      Pointer to the maximum size of the storage space available\r
-                                  for the EFI variables associated with the attributes specified.\r
-  RemainingVariableStorageSize    Pointer to the remaining size of the storage space available\r
-                                  for the EFI variables associated with the attributes specified.\r
-  MaximumVariableSize             Pointer to the maximum size of the individual EFI variables\r
-                                  associated with the attributes specified.\r
-\r
-Returns:\r
-\r
-  EFI STATUS\r
-  EFI_INVALID_PARAMETER           - An invalid combination of attribute bits was supplied.\r
-  EFI_SUCCESS                     - Query successfully.\r
-  EFI_UNSUPPORTED                 - The attribute is not supported on this platform.\r
-\r
---*/\r
-{\r
-  VARIABLE_HEADER        *Variable;\r
-  VARIABLE_HEADER        *NextVariable;\r
-  UINT64                 VariableSize;\r
-  VARIABLE_STORE_HEADER  *VariableStoreHeader;\r
-  UINT64                 CommonVariableTotalSize;\r
-  UINT64                 HwErrVariableTotalSize;\r
-\r
-  CommonVariableTotalSize = 0;\r
-  HwErrVariableTotalSize = 0;\r
-\r
-  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  \r
-  if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
-    //\r
-    // Make sure the Attributes combination is supported by the platform.\r
-    //\r
-    return EFI_UNSUPPORTED;  \r
-  } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
-    //\r
-    // Make sure if runtime bit is set, boot service bit is set also.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
-    //\r
-    // Make sure RT Attribute is set if we are in Runtime phase.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-    //\r
-    // Make sure Hw Attribute is set with NV.\r
-    //\r
-    return EFI_INVALID_PARAMETER;\r
-  } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
-    //\r
-    // Not support authentiated variable write yet.\r
-    //\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-  \r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[\r
-                                (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile\r
-                                ];\r
-  //\r
-  // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
-  // with the storage size (excluding the storage header size).\r
-  //\r
-  *MaximumVariableStorageSize   = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
-\r
-  //\r
-  // Harware error record variable needs larger size.\r
-  //\r
-  if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-    *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r
-    *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
-  } else {\r
-    if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
-      ASSERT (PcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
-      *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
-    }\r
-\r
-    //\r
-    // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
-    //\r
-    *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
-  }\r
-  \r
-  //\r
-  // Point to the starting address of the variables.\r
-  //\r
-  Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
-\r
-  //\r
-  // Now walk through the related variable store.\r
-  //\r
-  while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r
-    NextVariable = GetNextVariablePtr (Variable);\r
-    VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
-\r
-    if (EfiAtRuntime ()) {\r
-      //\r
-      // we don't take the state of the variables in mind\r
-      // when calculating RemainingVariableStorageSize,\r
-      // since the space occupied by variables not marked with\r
-      // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
-      //\r
-      if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-        HwErrVariableTotalSize += VariableSize;\r
-      } else {\r
-        CommonVariableTotalSize += VariableSize;\r
-      }\r
-    } else {\r
-      //\r
-      // Only care about Variables with State VAR_ADDED,because\r
-      // the space not marked as VAR_ADDED is reclaimable now.\r
-      //\r
-      if ((Variable->State == VAR_ADDED) || (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
-        if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
-          HwErrVariableTotalSize += VariableSize;\r
-        } else {\r
-          CommonVariableTotalSize += VariableSize;\r
-        }\r
-      }\r
-    }\r
-\r
-    //\r
-    // Go to the next one\r
-    //\r
-    Variable = NextVariable;\r
-  }\r
-  \r
-  if ((Attributes  & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
-    *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
-  } else {\r
-    *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-VariableServiceInitialize (\r
-  IN EFI_HANDLE         ImageHandle,\r
-  IN EFI_SYSTEM_TABLE   *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  This function does 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
-  Status code.\r
-\r
-  EFI_NOT_FOUND     - Variable store area not found.\r
-  EFI_SUCCESS       - Variable services successfully initialized.\r
-\r
---*/\r
-{\r
-  EFI_STATUS                      Status;\r
-  EFI_HANDLE                      NewHandle;\r
-  VS_DEV                          *Dev;\r
-  EFI_PEI_HOB_POINTERS            GuidHob;\r
-  VARIABLE_HEADER                 *Variable;\r
-  VARIABLE_HEADER                 *NextVariable;\r
-  VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
-  EFI_FLASH_MAP_FS_ENTRY_DATA     *FlashMapEntryData;\r
-  EFI_FLASH_SUBAREA_ENTRY         VariableStoreEntry;\r
-  UINT64                          BaseAddress;\r
-  UINT64                          Length;\r
-  EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
-\r
-  Status = gBS->AllocatePool (\r
-                  EfiRuntimeServicesData,\r
-                  (UINTN) sizeof (VARIABLE_GLOBAL),\r
-                  (VOID**) &mGlobal\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  ZeroMem (mGlobal, (UINTN) sizeof (VARIABLE_GLOBAL));\r
-\r
-  GuidHob.Raw = GetHobList ();\r
-  FlashMapEntryData = NULL;\r
-  while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {\r
-    FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r
-    if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
-      break;\r
-    }\r
-    GuidHob.Raw = GET_NEXT_HOB (GuidHob); \r
-  }\r
-\r
-  if (FlashMapEntryData == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));\r
-    Status = EFI_NOT_FOUND;\r
-    return Status;\r
-  }\r
-  \r
-  CopyMem(\r
-    (VOID*)&VariableStoreEntry,\r
-    (VOID*)&FlashMapEntryData->Entries[0],\r
-    sizeof(EFI_FLASH_SUBAREA_ENTRY)\r
-    );\r
-\r
-  //\r
-  // Mark the variable storage region of the FLASH as RUNTIME\r
-  //\r
-  BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r
-  Length      = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r
-  Length      = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
-  Status      = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
-  if (EFI_ERROR (Status)) {\r
-    Status = EFI_UNSUPPORTED;\r
-    return Status;\r
-  }\r
-  Status = gDS->SetMemorySpaceAttributes (\r
-                  BaseAddress,\r
-                  Length,\r
-                  GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    Status = EFI_UNSUPPORTED;\r
-    return Status;\r
-  }\r
-\r
-  Status = FileStorageConstructor (\r
-             &mGlobal->VariableStore[NonVolatile], \r
-             &mGlobal->GoVirtualChildEvent[NonVolatile],\r
-             VariableStoreEntry.Base,\r
-             (UINT32) VariableStoreEntry.Length,\r
-             FlashMapEntryData->VolumeId,\r
-             FlashMapEntryData->FilePath\r
-             );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Volatile Storage\r
-  //\r
-  Status = MemStorageConstructor (\r
-             &mGlobal->VariableStore[Volatile],\r
-             &mGlobal->GoVirtualChildEvent[Volatile],\r
-             VOLATILE_VARIABLE_STORE_SIZE\r
-             );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Scratch\r
-  //\r
-  Status = gBS->AllocatePool (\r
-                  EfiRuntimeServicesData,\r
-                  VARIABLE_SCRATCH_SIZE,\r
-                  &mGlobal->Scratch\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // 1. NV Storage\r
-  //\r
-  Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
-  if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
-    if (~VariableStoreHeader->Size == 0) {\r
-      VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;\r
-    }\r
-  }\r
-  //\r
-  // Calculate LastVariableOffset\r
-  //\r
-  Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
-  while (IsValidVariableHeader (Variable)) {\r
-    UINTN VariableSize = 0;\r
-    NextVariable = GetNextVariablePtr (Variable);\r
-    VariableSize = NextVariable - Variable;\r
-    if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
-      mGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r
-    } else {\r
-      mGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r
-    }\r
-    Variable = NextVariable;\r
-  }\r
-\r
-  mGlobal->LastVariableOffset[NonVolatile] = (UINTN) Variable - (UINTN) VariableStoreHeader;\r
-  mGlobal->VariableBase[NonVolatile]       = VariableStoreHeader;\r
-\r
-  //\r
-  // Reclaim if remaining space is too small\r
-  //\r
-  if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {\r
-    Status = Reclaim (NonVolatile, NULL);\r
-    if (EFI_ERROR (Status)) {\r
-      //\r
-      // Reclaim error\r
-      // we cannot restore to original state\r
-      //\r
-      DEBUG ((EFI_D_ERROR, "FSVariable: Reclaim error (fatal error) - %r\n", Status));\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\r
-  }\r
-  \r
-  //\r
-  // 2. Volatile Storage\r
-  //\r
-  Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);\r
-  VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r
-  mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev);  \r
-  mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);\r
-  //\r
-  // init store_header & body in memory.\r
-  //\r
-  mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);\r
-  mGlobal->VariableStore[Volatile]->Write (\r
-                                   mGlobal->VariableStore[Volatile],\r
-                                   0,\r
-                                   sizeof (VARIABLE_STORE_HEADER),\r
-                                   &mStoreHeaderTemplate\r
-                                   );\r
-\r
-\r
-  SystemTable->RuntimeServices->GetVariable         = DuetGetVariable;\r
-  SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;\r
-  SystemTable->RuntimeServices->SetVariable         = SetVariable;\r
-\r
-  SystemTable->RuntimeServices->QueryVariableInfo   = QueryVariableInfo;\r
-\r
-  //\r
-  // Now install the Variable Runtime Architectural Protocol on a new handle\r
-  //\r
-  NewHandle = NULL;\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &NewHandle,\r
-                  &gEfiVariableArchProtocolGuid,\r
-                  NULL,\r
-                  &gEfiVariableWriteArchProtocolGuid,\r
-                  NULL,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-\r
-VOID\r
-EFIAPI\r
-OnVirtualAddressChangeFsv (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  )\r
-{\r
-  UINTN Index;\r
-\r
-  for (Index = 0; Index < MaxType; Index++) {\r
-    mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);\r
-    EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);\r
-    EfiConvertPointer (0, &mGlobal->VariableBase[Index]);\r
-  }\r
-  EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLangCodes);\r
-  EfiConvertPointer (0, (VOID **) &mGlobal->LangCodes);\r
-  EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLang);\r
-  EfiConvertPointer (0, &mGlobal->Scratch);\r
-  EfiConvertPointer (0, (VOID**) &mGlobal);\r
-}\r