+**/\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
+ if (SupportedLanguages == NULL) {\r
+ return NULL;\r
+ }\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 ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->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
+ This function is to check if the remaining variable space is enough to set\r
+ all Variables from argument list successfully. The purpose of the check\r
+ is to keep the consistency of the Variables to be in variable storage.\r
+\r
+ Note: Variables are assumed to be in same storage.\r
+ The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
+ so follow the argument sequence to check the Variables.\r
+\r
+ @param[in] Attributes Variable attributes for Variable entries.\r
+ @param[in] Marker VA_LIST style variable argument list.\r
+ The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
+ A NULL terminates the list. The VariableSize of\r
+ VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
+ It will be changed to variable total size as output.\r
+\r
+ @retval TRUE Have enough variable space to set the Variables successfully.\r
+ @retval FALSE No enough variable space to set the Variables successfully.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+CheckRemainingSpaceForConsistencyInternal (\r
+ IN UINT32 Attributes,\r
+ IN VA_LIST Marker\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VA_LIST Args;\r
+ VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r
+ UINT64 MaximumVariableStorageSize;\r
+ UINT64 RemainingVariableStorageSize;\r
+ UINT64 MaximumVariableSize;\r
+ UINTN TotalNeededSize;\r
+ UINTN OriginalVarSize;\r
+ VARIABLE_STORE_HEADER *VariableStoreHeader;\r
+ VARIABLE_POINTER_TRACK VariablePtrTrack;\r
+ VARIABLE_HEADER *NextVariable;\r
+ UINTN VarNameSize;\r
+ UINTN VarDataSize;\r
+\r
+ //\r
+ // Non-Volatile related.\r
+ //\r
+ VariableStoreHeader = mNvVariableCache;\r
+\r
+ Status = VariableServiceQueryVariableInfoInternal (\r
+ Attributes,\r
+ &MaximumVariableStorageSize,\r
+ &RemainingVariableStorageSize,\r
+ &MaximumVariableSize\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ TotalNeededSize = 0;\r
+ VA_COPY (Args, Marker);\r
+ VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
+ while (VariableEntry != NULL) {\r
+ //\r
+ // Calculate variable total size.\r
+ //\r
+ VarNameSize = StrSize (VariableEntry->Name);\r
+ VarNameSize += GET_PAD_SIZE (VarNameSize);\r
+ VarDataSize = VariableEntry->VariableSize;\r
+ VarDataSize += GET_PAD_SIZE (VarDataSize);\r
+ VariableEntry->VariableSize = HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize + VarDataSize);\r
+\r
+ TotalNeededSize += VariableEntry->VariableSize;\r
+ VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
+ }\r
+ VA_END (Args);\r
+\r
+ if (RemainingVariableStorageSize >= TotalNeededSize) {\r
+ //\r
+ // Already have enough space.\r
+ //\r
+ return TRUE;\r
+ } else if (AtRuntime ()) {\r
+ //\r
+ // At runtime, no reclaim.\r
+ // The original variable space of Variables can't be reused.\r
+ //\r
+ return FALSE;\r
+ }\r
+\r
+ VA_COPY (Args, Marker);\r
+ VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
+ while (VariableEntry != NULL) {\r
+ //\r
+ // Check if Variable[Index] has been present and get its size.\r
+ //\r
+ OriginalVarSize = 0;\r
+ VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r
+ VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r
+ Status = FindVariableEx (\r
+ VariableEntry->Name,\r
+ VariableEntry->Guid,\r
+ FALSE,\r
+ &VariablePtrTrack\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Get size of Variable[Index].\r
+ //\r
+ NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r
+ OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r
+ //\r
+ // Add the original size of Variable[Index] to remaining variable storage size.\r
+ //\r
+ RemainingVariableStorageSize += OriginalVarSize;\r
+ }\r
+ if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r
+ //\r
+ // No enough space for Variable[Index].\r
+ //\r
+ VA_END (Args);\r
+ return FALSE;\r
+ }\r
+ //\r
+ // Sub the (new) size of Variable[Index] from remaining variable storage size.\r
+ //\r
+ RemainingVariableStorageSize -= VariableEntry->VariableSize;\r
+ VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r
+ }\r
+ VA_END (Args);\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ This function is to check if the remaining variable space is enough to set\r
+ all Variables from argument list successfully. The purpose of the check\r
+ is to keep the consistency of the Variables to be in variable storage.\r
+\r
+ Note: Variables are assumed to be in same storage.\r
+ The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r
+ so follow the argument sequence to check the Variables.\r
+\r
+ @param[in] Attributes Variable attributes for Variable entries.\r
+ @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r
+ A NULL terminates the list. The VariableSize of\r
+ VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r
+ It will be changed to variable total size as output.\r
+\r
+ @retval TRUE Have enough variable space to set the Variables successfully.\r
+ @retval FALSE No enough variable space to set the Variables successfully.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+CheckRemainingSpaceForConsistency (\r
+ IN UINT32 Attributes,\r
+ ...\r
+ )\r
+{\r
+ VA_LIST Marker;\r
+ BOOLEAN Return;\r
+\r
+ VA_START (Marker, Attributes);\r
+\r
+ Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);\r
+\r
+ VA_END (Marker);\r
+\r
+ return Return;\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