summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c')
-rw-r--r--MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c932
1 files changed, 637 insertions, 295 deletions
diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
index 62025c9ee1..611d1904ec 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
@@ -19,6 +19,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;
EFI_EVENT mVirtualAddressChangeEvent = NULL;
EFI_HANDLE mHandle = NULL;
+///
+/// The size of a 3 character ISO639 language code.
+///
+#define ISO_639_2_ENTRY_SIZE 3
///
/// The current Hii implementation accesses this variable many times on every boot.
@@ -33,12 +37,50 @@ VARIABLE_CACHE_ENTRY mVariableCache[] = {
0x00000000,
0x00,
NULL
+ },
+ {
+ &gEfiGlobalVariableGuid,
+ L"PlatformLang",
+ 0x00000000,
+ 0x00,
+ NULL
}
};
-VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
-EFI_EVENT mFvbRegistration = NULL;
+VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
+EFI_EVENT mFvbRegistration = NULL;
+
+/**
+ Update the variable region with Variable information. These are the same
+ arguments as the EFI Variable services.
+
+ @param[in] VariableName Name of variable
+
+ @param[in] VendorGuid Guid of variable
+
+ @param[in] Data Variable data
+
+ @param[in] DataSize Size of data. 0 means delete
+
+ @param[in] Attributes Attribues of the variable
+ @param[in] Variable The variable information which is used to keep track of variable usage.
+
+ @retval EFI_SUCCESS The update operation is success.
+
+ @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
+
+**/
+EFI_STATUS
+EFIAPI
+UpdateVariable (
+ IN CHAR16 *VariableName,
+ IN EFI_GUID *VendorGuid,
+ IN VOID *Data,
+ IN UINTN DataSize,
+ IN UINT32 Attributes OPTIONAL,
+ IN VARIABLE_POINTER_TRACK *Variable
+ );
/**
Acquires lock only at boot time. Simply returns at runtime.
@@ -1006,309 +1048,327 @@ FindVariable (
return EFI_NOT_FOUND;
}
-
/**
-
- This code finds variable in storage blocks (Volatile or Non-Volatile).
-
- @param VariableName Name of Variable to be found.
- @param VendorGuid Variable vendor GUID.
- @param Attributes Attribute value of the variable found.
- @param DataSize Size of Data found. If size is less than the
- data, this value contains the required size.
- @param Data Data pointer.
-
- @return EFI_INVALID_PARAMETER Invalid parameter
- @return EFI_SUCCESS Find the specified variable
- @return EFI_NOT_FOUND Not found
- @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
+ Get index from supported language codes according to language string.
+
+ This code is used to get corresponding index in supported language codes. It can handle
+ RFC3066 and ISO639 language tags.
+ In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
+ In RFC3066 language tags, take semicolon as a delimitation to find matched string and calculate the index.
+
+ For example:
+ SupportedLang = "engfraengfra"
+ Lang = "eng"
+ Iso639Language = TRUE
+ The return value is "0".
+ Another example:
+ SupportedLang = "en;fr;en-US;fr-FR"
+ Lang = "fr-FR"
+ Iso639Language = FALSE
+ The return value is "3".
+
+ @param SupportedLang Platform supported language codes.
+ @param Lang Configured language.
+ @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC3066.
+
+ @retval the index of language in the language codes.
**/
-EFI_STATUS
+UINTN
EFIAPI
-RuntimeServiceGetVariable (
- IN CHAR16 *VariableName,
- IN EFI_GUID *VendorGuid,
- OUT UINT32 *Attributes OPTIONAL,
- IN OUT UINTN *DataSize,
- OUT VOID *Data
- )
+GetIndexFromSupportedLangCodes(
+ IN CHAR8 *SupportedLang,
+ IN CHAR8 *Lang,
+ IN BOOLEAN Iso639Language
+ )
{
- EFI_STATUS Status;
- VARIABLE_POINTER_TRACK Variable;
- UINTN VarDataSize;
-
- if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
- return EFI_INVALID_PARAMETER;
- }
-
- AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
-
- //
- // Find existing variable
- //
- Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
- if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
- // Hit in the Cache
- UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
- goto Done;
- }
-
- Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
- if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
- goto Done;
+ UINTN Index;
+ UINT32 CompareLength;
+ CHAR8 *Supported;
+
+ Index = 0;
+ Supported = SupportedLang;
+ if (Iso639Language) {
+ CompareLength = 3;
+ for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
+ if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
+ //
+ // Successfully find the index of Lang string in SupportedLang string.
+ //
+ Index = Index / CompareLength;
+ return Index;
+ }
+ }
+ ASSERT (FALSE);
+ return 0;
+ } else {
+ //
+ // Compare RFC3066 language code
+ //
+ while (*Supported != '\0') {
+ //
+ // take semicolon as delimitation, sequentially traverse supported language codes.
+ //
+ for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
+ Supported++;
+ }
+ if (AsciiStrnCmp (Lang, Supported - CompareLength, CompareLength) == 0) {
+ //
+ // Successfully find the index of Lang string in SupportedLang string.
+ //
+ return Index;
+ }
+ Index++;
+ }
+ ASSERT (FALSE);
+ return 0;
}
+}
- //
- // Get data size
- //
- VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
- ASSERT (VarDataSize != 0);
-
- if (*DataSize >= VarDataSize) {
- if (Data == NULL) {
- Status = EFI_INVALID_PARAMETER;
- goto Done;
- }
+/**
+ Get language string from supported language codes according to index.
+
+ This code is used to get corresponding language string in supported language codes. It can handle
+ RFC3066 and ISO639 language tags.
+ In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
+ In RFC3066 language tags, take semicolon as a delimitation. Find language string according to the index.
+
+ For example:
+ SupportedLang = "engfraengfra"
+ Index = "1"
+ Iso639Language = TRUE
+ The return value is "fra".
+ Another example:
+ SupportedLang = "en;fr;en-US;fr-FR"
+ Index = "1"
+ Iso639Language = FALSE
+ The return value is "fr".
+
+ @param SupportedLang Platform supported language codes.
+ @param Index the index in supported language codes.
+ @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC3066.
+
+ @retval the language string in the language codes.
- CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
- if (Attributes != NULL) {
- *Attributes = Variable.CurrPtr->Attributes;
- }
+**/
+CHAR8 *
+EFIAPI
+GetLangFromSupportedLangCodes (
+ IN CHAR8 *SupportedLang,
+ IN UINTN Index,
+ IN BOOLEAN Iso639Language
+)
+{
+ UINTN SubIndex;
+ UINT32 CompareLength;
+ CHAR8 *Supported;
- *DataSize = VarDataSize;
- UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
- UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);
-
- Status = EFI_SUCCESS;
- goto Done;
+ SubIndex = 0;
+ Supported = SupportedLang;
+ if (Iso639Language) {
+ //
+ // according to the index of Lang string in SupportedLang string to get the language.
+ // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
+ // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
+ //
+ CompareLength = 3;
+ SetMem (mVariableModuleGlobal->Lang, sizeof(mVariableModuleGlobal->Lang), 0);
+ return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);
+
} else {
- *DataSize = VarDataSize;
- Status = EFI_BUFFER_TOO_SMALL;
- goto Done;
+ while (TRUE) {
+ //
+ // take semicolon as delimitation, sequentially traverse supported language codes.
+ //
+ for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
+ Supported++;
+ }
+ if ((*Supported == '\0') && (SubIndex != Index)) {
+ //
+ // Have completed the traverse, but not find corrsponding string.
+ // This case is not allowed to happen.
+ //
+ ASSERT(FALSE);
+ return NULL;
+ }
+ if (SubIndex == Index) {
+ //
+ // according to the index of Lang string in SupportedLang string to get the language.
+ // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
+ // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
+ //
+ SetMem (mVariableModuleGlobal->PlatformLang, sizeof (mVariableModuleGlobal->PlatformLang), 0);
+ return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);
+ }
+ SubIndex++;
+ }
}
-
-Done:
- ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
- return Status;
}
+/**
+ Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
+ When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
-/**
+ According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
+ and are read-only. Therefore, in variable driver, only store the original value for other use.
- This code Finds the Next available variable.
+ @param[in] VariableName Name of variable
- @param VariableNameSize Size of the variable name
- @param VariableName Pointer to variable name
- @param VendorGuid Variable Vendor Guid
+ @param[in] Data Variable data
- @return EFI_INVALID_PARAMETER Invalid parameter
- @return EFI_SUCCESS Find the specified variable
- @return EFI_NOT_FOUND Not found
- @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
+ @param[in] DataSize Size of data. 0 means delete
+
+ @retval EFI_SUCCESS auto update operation is successful.
**/
EFI_STATUS
EFIAPI
-RuntimeServiceGetNextVariableName (
- IN OUT UINTN *VariableNameSize,
- IN OUT CHAR16 *VariableName,
- IN OUT EFI_GUID *VendorGuid
+AutoUpdateLangVariable(
+ IN CHAR16 *VariableName,
+ IN VOID *Data,
+ IN UINTN DataSize
)
{
- VARIABLE_POINTER_TRACK Variable;
- UINTN VarNameSize;
- EFI_STATUS Status;
+ EFI_STATUS Status;
+ CHAR8 *BestPlatformLang;
+ CHAR8 *BestLang;
+ UINTN Index;
+ UINT32 Attributes;
+ VARIABLE_POINTER_TRACK Variable;
- if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
- return EFI_INVALID_PARAMETER;
- }
+ //
+ // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
+ //
+ Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
- AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+ if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {
+ //
+ // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
+ // Therefore, in variable driver, only store the original value for other use.
+ //
+ AsciiStrnCpy (mVariableModuleGlobal->PlatformLangCodes, Data, DataSize);
+ } else if (StrCmp (VariableName, L"LangCodes") == 0) {
+ //
+ // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
+ // Therefore, in variable driver, only store the original value for other use.
+ //
+ AsciiStrnCpy (mVariableModuleGlobal->LangCodes, Data, DataSize);
+ } else if (StrCmp (VariableName, L"PlatformLang") == 0) {
+ ASSERT (AsciiStrLen (mVariableModuleGlobal->PlatformLangCodes) != 0);
- Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
- if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
- goto Done;
- }
+ //
+ // When setting PlatformLang, firstly get most matched language string from supported language codes.
+ //
+ BestPlatformLang = GetBestLanguage(mVariableModuleGlobal->PlatformLangCodes, FALSE, Data);
- if (VariableName[0] != 0) {
//
- // If variable name is not NULL, get next variable
+ // Get the corresponding index in language codes.
//
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
- }
+ Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
- while (TRUE) {
//
- // If both volatile and non-volatile variable store are parsed,
- // return not found
+ // Get the corresponding ISO639 language tag according to RFC3066 language tag.
//
- if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
- Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
- if (!Variable.Volatile) {
- Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
- Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));
- } else {
- Status = EFI_NOT_FOUND;
- goto Done;
- }
+ BestLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, Index, TRUE);
- Variable.CurrPtr = Variable.StartPtr;
- if (!IsValidVariableHeader (Variable.CurrPtr)) {
- continue;
- }
- }
//
- // Variable is found
+ // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
//
- if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
- if ((EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {
- VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
- ASSERT (VarNameSize != 0);
+ FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
- if (VarNameSize <= *VariableNameSize) {
- CopyMem (
- VariableName,
- GetVariableNamePtr (Variable.CurrPtr),
- VarNameSize
- );
- CopyMem (
- VendorGuid,
- &Variable.CurrPtr->VendorGuid,
- sizeof (EFI_GUID)
- );
- Status = EFI_SUCCESS;
- } else {
- Status = EFI_BUFFER_TOO_SMALL;
- }
+ Status = UpdateVariable(L"Lang", &gEfiGlobalVariableGuid,
+ BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);
- *VariableNameSize = VarNameSize;
- goto Done;
- }
- }
+ DEBUG((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
- Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
- }
+ ASSERT_EFI_ERROR(Status);
+
+ } else if (StrCmp (VariableName, L"Lang") == 0) {
+ ASSERT (AsciiStrLen (mVariableModuleGlobal->LangCodes) != 0);
-Done:
- ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
- return Status;
+ //
+ // When setting Lang, firstly get most matched language string from supported language codes.
+ //
+ BestLang = GetBestLanguage(mVariableModuleGlobal->LangCodes, TRUE, Data);
+
+ //
+ // Get the corresponding index in language codes.
+ //
+ Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, BestLang, TRUE);
+
+ //
+ // Get the corresponding RFC3066 language tag according to ISO639 language tag.
+ //
+ BestPlatformLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
+
+ //
+ // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
+ //
+ FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
+
+ Status = UpdateVariable(L"PlatformLang", &gEfiGlobalVariableGuid,
+ BestPlatformLang, AsciiStrLen (BestPlatformLang), Attributes, &Variable);
+
+ DEBUG((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
+ ASSERT_EFI_ERROR(Status);
+ }
+ return EFI_SUCCESS;
}
/**
+ Update the variable region with Variable information. These are the same
+ arguments as the EFI Variable services.
- This code sets variable in storage blocks (Volatile or Non-Volatile).
+ @param[in] VariableName Name of variable
- @param VariableName Name of Variable to be found
- @param VendorGuid Variable vendor GUID
- @param Attributes Attribute value of the variable found
- @param DataSize Size of Data found. If size is less than the
- data, this value contains the required size.
- @param Data Data pointer
+ @param[in] VendorGuid Guid of variable
- @return EFI_INVALID_PARAMETER Invalid parameter
- @return EFI_SUCCESS Set successfully
- @return EFI_OUT_OF_RESOURCES Resource not enough to set variable
- @return EFI_NOT_FOUND Not found
- @return EFI_WRITE_PROTECTED Variable is read-only
+ @param[in] Data Variable data
+
+ @param[in] DataSize Size of data. 0 means delete
+
+ @param[in] Attributes Attribues of the variable
+
+ @param[in] Variable The variable information which is used to keep track of variable usage.
+
+ @retval EFI_SUCCESS The update operation is success.
+
+ @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
**/
EFI_STATUS
EFIAPI
-RuntimeServiceSetVariable (
- IN CHAR16 *VariableName,
- IN EFI_GUID *VendorGuid,
- IN UINT32 Attributes,
- IN UINTN DataSize,
- IN VOID *Data
+UpdateVariable (
+ IN CHAR16 *VariableName,
+ IN EFI_GUID *VendorGuid,
+ IN VOID *Data,
+ IN UINTN DataSize,
+ IN UINT32 Attributes OPTIONAL,
+ IN VARIABLE_POINTER_TRACK *Variable
)
{
- VARIABLE_POINTER_TRACK Variable;
EFI_STATUS Status;
VARIABLE_HEADER *NextVariable;
- UINTN VarNameSize;
+ UINTN ScratchSize;
+ UINTN NonVolatileVarableStoreSize;
UINTN VarNameOffset;
UINTN VarDataOffset;
+ UINTN VarNameSize;
UINTN VarSize;
+ BOOLEAN Volatile;
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
UINT8 State;
BOOLEAN Reclaimed;
- UINTN *VolatileOffset;
- UINTN *NonVolatileOffset;
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
- BOOLEAN Volatile;
- EFI_PHYSICAL_ADDRESS Point;
- UINTN ScratchSize;
- UINTN NonVolatileVarableStoreSize;
-
- //
- // Check input parameters
- //
- if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // Make sure if runtime bit is set, boot service bit is set also
- //
- if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
- return EFI_INVALID_PARAMETER;
- }
- //
- // The size of the VariableName, including the Unicode Null in bytes plus
- // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
- // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
- //
- if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
- if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) ||
- (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {
- return EFI_INVALID_PARAMETER;
- }
- } else {
- //
- // The size of the VariableName, including the Unicode Null in bytes plus
- // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.
- //
- if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||
- (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {
- return EFI_INVALID_PARAMETER;
- }
- }
-
- AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
-
- Reclaimed = FALSE;
Fvb = mVariableModuleGlobal->FvbInstance;
- VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;
-
- //
- // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
- //
- if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
- Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;
- //
- // Parse non-volatile variable data and get last variable offset
- //
- NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
- while (IsValidVariableHeader (NextVariable)) {
- NextVariable = GetNextVariablePtr (NextVariable);
- }
- mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
- }
-
- NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;
-
+ Reclaimed = FALSE;
- //
- // Check whether the input variable is already existed
- //
-
- Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
- if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {
+ if (Variable->CurrPtr != NULL) {
//
// Update/Delete existing variable
//
- Volatile = Variable.Volatile;
+ Volatile = Variable->Volatile;
if (EfiAtRuntime ()) {
//
@@ -1316,14 +1376,14 @@ RuntimeServiceSetVariable (
// the volatile is ReadOnly, and SetVariable should be aborted and
// return EFI_WRITE_PROTECTED.
//
- if (Variable.Volatile) {
+ if (Variable->Volatile) {
Status = EFI_WRITE_PROTECTED;
goto Done;
}
//
// Only variable have NV attribute can be updated/deleted in Runtime
//
- if ((Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
+ if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
Status = EFI_INVALID_PARAMETER;
goto Done;
}
@@ -1333,15 +1393,15 @@ RuntimeServiceSetVariable (
// specified causes it to be deleted.
//
if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
- State = Variable.CurrPtr->State;
+ State = Variable->CurrPtr->State;
State &= VAR_DELETED;
Status = UpdateVariableStore (
&mVariableModuleGlobal->VariableGlobal,
- Variable.Volatile,
+ Variable->Volatile,
FALSE,
Fvb,
- (UINTN) &Variable.CurrPtr->State,
+ (UINTN) &Variable->CurrPtr->State,
sizeof (UINT8),
&State
);
@@ -1355,27 +1415,27 @@ RuntimeServiceSetVariable (
// If the variable is marked valid and the same data has been passed in
// then return to the caller immediately.
//
- if (DataSizeOfVariable (Variable.CurrPtr) == DataSize &&
- (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {
+ if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&
+ (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {
UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
Status = EFI_SUCCESS;
goto Done;
- } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
- (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
+ } else if ((Variable->CurrPtr->State == VAR_ADDED) ||
+ (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
//
// Mark the old variable as in delete transition
//
- State = Variable.CurrPtr->State;
+ State = Variable->CurrPtr->State;
State &= VAR_IN_DELETED_TRANSITION;
Status = UpdateVariableStore (
&mVariableModuleGlobal->VariableGlobal,
- Variable.Volatile,
+ Variable->Volatile,
FALSE,
Fvb,
- (UINTN) &Variable.CurrPtr->State,
+ (UINTN) &Variable->CurrPtr->State,
sizeof (UINT8),
&State
);
@@ -1383,9 +1443,9 @@ RuntimeServiceSetVariable (
goto Done;
}
}
- } else if (Status == EFI_NOT_FOUND) {
+ } else {
//
- // Create a new variable
+ // Not found existing variable. Create a new variable
//
//
@@ -1405,12 +1465,6 @@ RuntimeServiceSetVariable (
Status = EFI_INVALID_PARAMETER;
goto Done;
}
- } else {
- //
- // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().
- //
- ASSERT (Status == EFI_INVALID_PARAMETER);
- goto Done;
}
//
@@ -1475,7 +1529,8 @@ RuntimeServiceSetVariable (
//
// Perform garbage collection & reclaim operation
//
- Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE, Variable.CurrPtr);
+ Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
+ &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);
if (EFI_ERROR (Status)) {
goto Done;
}
@@ -1489,7 +1544,6 @@ RuntimeServiceSetVariable (
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
-
Reclaimed = TRUE;
}
//
@@ -1507,7 +1561,7 @@ RuntimeServiceSetVariable (
FALSE,
TRUE,
Fvb,
- *NonVolatileOffset,
+ mVariableModuleGlobal->NonVolatileLastVariableOffset,
sizeof (VARIABLE_HEADER),
(UINT8 *) NextVariable
);
@@ -1525,7 +1579,7 @@ RuntimeServiceSetVariable (
FALSE,
TRUE,
Fvb,
- *NonVolatileOffset,
+ mVariableModuleGlobal->NonVolatileLastVariableOffset,
sizeof (VARIABLE_HEADER),
(UINT8 *) NextVariable
);
@@ -1541,7 +1595,7 @@ RuntimeServiceSetVariable (
FALSE,
TRUE,
Fvb,
- *NonVolatileOffset + sizeof (VARIABLE_HEADER),
+ mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
(UINT32) VarSize - sizeof (VARIABLE_HEADER),
(UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
);
@@ -1558,7 +1612,7 @@ RuntimeServiceSetVariable (
FALSE,
TRUE,
Fvb,
- *NonVolatileOffset,
+ mVariableModuleGlobal->NonVolatileLastVariableOffset,
sizeof (VARIABLE_HEADER),
(UINT8 *) NextVariable
);
@@ -1567,7 +1621,7 @@ RuntimeServiceSetVariable (
goto Done;
}
- *NonVolatileOffset = HEADER_ALIGN (*NonVolatileOffset + VarSize);
+ mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
@@ -1580,25 +1634,25 @@ RuntimeServiceSetVariable (
//
Volatile = TRUE;
- if ((UINT32) (VarSize +*VolatileOffset) >
+ if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
//
// Perform garbage collection & reclaim operation
//
- Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE, Variable.CurrPtr);
+ Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
+ &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);
if (EFI_ERROR (Status)) {
goto Done;
}
//
// If still no enough space, return out of resources
//
- if ((UINT32) (VarSize +*VolatileOffset) >
+ if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
) {
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
-
Reclaimed = TRUE;
}
@@ -1608,7 +1662,7 @@ RuntimeServiceSetVariable (
TRUE,
TRUE,
Fvb,
- *VolatileOffset,
+ mVariableModuleGlobal->VolatileLastVariableOffset,
(UINT32) VarSize,
(UINT8 *) NextVariable
);
@@ -1617,37 +1671,328 @@ RuntimeServiceSetVariable (
goto Done;
}
- *VolatileOffset = HEADER_ALIGN (*VolatileOffset + VarSize);
+ mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
}
+
//
// Mark the old variable as deleted
//
- if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
- State = Variable.CurrPtr->State;
+ if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {
+ State = Variable->CurrPtr->State;
State &= VAR_DELETED;
Status = UpdateVariableStore (
- &mVariableModuleGlobal->VariableGlobal,
- Variable.Volatile,
- FALSE,
- Fvb,
- (UINTN) &Variable.CurrPtr->State,
- sizeof (UINT8),
- &State
- );
-
- if (!EFI_ERROR (Status)) {
- UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
- UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
+ &mVariableModuleGlobal->VariableGlobal,
+ Variable->Volatile,
+ FALSE,
+ Fvb,
+ (UINTN) &Variable->CurrPtr->State,
+ sizeof (UINT8),
+ &State
+ );
+ }
+
+ if (!EFI_ERROR (Status)) {
+ UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
+ UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
+ }
+
+Done:
+ return Status;
+}
+
+/**
+
+ This code finds variable in storage blocks (Volatile or Non-Volatile).
+
+ @param VariableName Name of Variable to be found.
+ @param VendorGuid Variable vendor GUID.
+ @param Attributes Attribute value of the variable found.
+ @param DataSize Size of Data found. If size is less than the
+ data, this value contains the required size.
+ @param Data Data pointer.
+
+ @return EFI_INVALID_PARAMETER Invalid parameter
+ @return EFI_SUCCESS Find the specified variable
+ @return EFI_NOT_FOUND Not found
+ @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
+
+**/
+EFI_STATUS
+EFIAPI
+RuntimeServiceGetVariable (
+ IN CHAR16 *VariableName,
+ IN EFI_GUID *VendorGuid,
+ OUT UINT32 *Attributes OPTIONAL,
+ IN OUT UINTN *DataSize,
+ OUT VOID *Data
+ )
+{
+ EFI_STATUS Status;
+ VARIABLE_POINTER_TRACK Variable;
+ UINTN VarDataSize;
+
+ if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+
+ //
+ // Find existing variable
+ //
+ Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
+ if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
+ // Hit in the Cache
+ UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
+ goto Done;
+ }
+
+ Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
+ if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
+ goto Done;
+ }
+
+ //
+ // Get data size
+ //
+ VarDataSize = DataSizeOfVariable (Variable.CurrPtr);
+ ASSERT (VarDataSize != 0);
+
+ if (*DataSize >= VarDataSize) {
+ if (Data == NULL) {
+ Status = EFI_INVALID_PARAMETER;
+ goto Done;
}
- goto Done;
+
+ CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
+ if (Attributes != NULL) {
+ *Attributes = Variable.CurrPtr->Attributes;
+ }
+
+ *DataSize = VarDataSize;
+ UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
+ UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);
+
+ Status = EFI_SUCCESS;
+ goto Done;
+ } else {
+ *DataSize = VarDataSize;
+ Status = EFI_BUFFER_TOO_SMALL;
+ goto Done;
}
- Status = EFI_SUCCESS;
- UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
- UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
+Done:
+ ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+ return Status;
+}
+
+
+
+/**
+
+ This code Finds the Next available variable.
+
+ @param VariableNameSize Size of the variable name
+ @param VariableName Pointer to variable name
+ @param VendorGuid Variable Vendor Guid
+
+ @return EFI_INVALID_PARAMETER Invalid parameter
+ @return EFI_SUCCESS Find the specified variable
+ @return EFI_NOT_FOUND Not found
+ @return EFI_BUFFER_TO_SMALL DataSize is too small for the result
+
+**/
+EFI_STATUS
+EFIAPI
+RuntimeServiceGetNextVariableName (
+ IN OUT UINTN *VariableNameSize,
+ IN OUT CHAR16 *VariableName,
+ IN OUT EFI_GUID *VendorGuid
+ )
+{
+ VARIABLE_POINTER_TRACK Variable;
+ UINTN VarNameSize;
+ EFI_STATUS Status;
+
+ if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+
+ Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
+ if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
+ goto Done;
+ }
+
+ if (VariableName[0] != 0) {
+ //
+ // If variable name is not NULL, get next variable
+ //
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+ }
+
+ while (TRUE) {
+ //
+ // If both volatile and non-volatile variable store are parsed,
+ // return not found
+ //
+ if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
+ Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
+ if (!Variable.Volatile) {
+ Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
+ Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));
+ } else {
+ Status = EFI_NOT_FOUND;
+ goto Done;
+ }
+
+ Variable.CurrPtr = Variable.StartPtr;
+ if (!IsValidVariableHeader (Variable.CurrPtr)) {
+ continue;
+ }
+ }
+ //
+ // Variable is found
+ //
+ if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {
+ if ((EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {
+ VarNameSize = NameSizeOfVariable (Variable.CurrPtr);
+ ASSERT (VarNameSize != 0);
+
+ if (VarNameSize <= *VariableNameSize) {
+ CopyMem (
+ VariableName,
+ GetVariableNamePtr (Variable.CurrPtr),
+ VarNameSize
+ );
+ CopyMem (
+ VendorGuid,
+ &Variable.CurrPtr->VendorGuid,
+ sizeof (EFI_GUID)
+ );
+ Status = EFI_SUCCESS;
+ } else {
+ Status = EFI_BUFFER_TOO_SMALL;
+ }
+
+ *VariableNameSize = VarNameSize;
+ goto Done;
+ }
+ }
+
+ Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
+ }
Done:
+ ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+ return Status;
+}
+
+/**
+
+ This code sets variable in storage blocks (Volatile or Non-Volatile).
+
+ @param VariableName Name of Variable to be found
+ @param VendorGuid Variable vendor GUID
+ @param Attributes Attribute value of the variable found
+ @param DataSize Size of Data found. If size is less than the
+ data, this value contains the required size.
+ @param Data Data pointer
+
+ @return EFI_INVALID_PARAMETER Invalid parameter
+ @return EFI_SUCCESS Set successfully
+ @return EFI_OUT_OF_RESOURCES Resource not enough to set variable
+ @return EFI_NOT_FOUND Not found
+ @return EFI_WRITE_PROTECTED Variable is read-only
+
+**/
+EFI_STATUS
+EFIAPI
+RuntimeServiceSetVariable (
+ IN CHAR16 *VariableName,
+ IN EFI_GUID *VendorGuid,
+ IN UINT32 Attributes,
+ IN UINTN DataSize,
+ IN VOID *Data
+ )
+{
+ VARIABLE_POINTER_TRACK Variable;
+ EFI_STATUS Status;
+ VARIABLE_HEADER *NextVariable;
+ EFI_PHYSICAL_ADDRESS Point;
+
+ //
+ // Check input parameters
+ //
+ if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+ //
+ // Make sure if runtime bit is set, boot service bit is set also
+ //
+ if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ //
+ // The size of the VariableName, including the Unicode Null in bytes plus
+ // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)
+ // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.
+ //
+ if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
+ if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) ||
+ (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {
+ return EFI_INVALID_PARAMETER;
+ }
+ //
+ // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
+ //
+ if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {
+ return EFI_INVALID_PARAMETER;
+ }
+ } else {
+ //
+ // The size of the VariableName, including the Unicode Null in bytes plus
+ // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.
+ //
+ if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||
+ (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {
+ return EFI_INVALID_PARAMETER;
+ }
+ }
+
+ AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
+
+ //
+ // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
+ //
+ if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {
+ Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;
+ //
+ // Parse non-volatile variable data and get last variable offset
+ //
+ NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);
+ while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))
+ && IsValidVariableHeader (NextVariable)) {
+ NextVariable = GetNextVariablePtr (NextVariable);
+ }
+ mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;
+ }
+
+ //
+ // Check whether the input variable is already existed
+ //
+ FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);
+
+ //
+ // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
+ //
+ AutoUpdateLangVariable (VariableName, Data, DataSize);
+
+ Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
+
InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);
ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
@@ -1894,15 +2239,12 @@ VariableCommonInitialize (
//
// Allocate runtime memory for variable driver global structure.
//
- mVariableModuleGlobal = AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL));
+ mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));
if (mVariableModuleGlobal == NULL) {
return EFI_OUT_OF_RESOURCES;
}
EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);
- mVariableModuleGlobal->VariableGlobal.ReentrantState = 0;
- mVariableModuleGlobal->CommonVariableTotalSize = 0;
- mVariableModuleGlobal->HwErrVariableTotalSize = 0;
//
// Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.