/** @file
The common variable operation routines shared by DXE_RINTIME variable
module and DXE_SMM variable module.
Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "Variable.h"
///
/// Define a memory cache that improves the search performance for a variable.
///
///
/// The memory entry used for variable statistics data.
///
/**
Routine used to track statistical information about variable usage.
The data is stored in the EFI system table so it can be accessed later.
VariableInfo.efi can dump out the table. Only Boot Services variable
accesses are tracked by this code. The PcdVariableCollectStatistics
build flag controls if this feature is enabled.
A read that hits in the cache will have Read and Cache true for
the transaction. Data is allocated by this routine, but never
freed.
@param[in] VariableName Name of the Variable to track.
@param[in] VendorGuid Guid of the Variable to track.
@param[in] Volatile TRUE if volatile FALSE if non-volatile.
@param[in] Read TRUE if GetVariable() was called.
@param[in] Write TRUE if SetVariable() was called.
@param[in] Delete TRUE if deleted via SetVariable().
@param[in] Cache TRUE for a cache hit.
**/
)
{
if (AtRuntime ()) {
// Don't collect statistics at runtime.
return;
}
if (gVariableInfo == NULL) {
//
// On the first call allocate a entry and place a pointer to it in
// the EFI System Table.
//
}
if (Read) {
}
if (Write) {
Entry->WriteCount++;
}
if (Delete) {
Entry->DeleteCount++;
}
if (Cache) {
Entry->CacheCount++;
}
return;
}
}
//
// If the entry is not in the table add it.
// Next iteration of the loop will fill in the data.
//
}
}
}
}
/**
This code checks if variable header is valid or not.
@param Variable Pointer to the Variable Header.
@retval TRUE Variable header is valid.
@retval FALSE Variable header is not valid.
**/
)
{
return FALSE;
}
return TRUE;
}
/**
This function writes data to the FWH at the correct LBA even if the LBAs
are fragmented.
@param Global Pointer to VARAIBLE_GLOBAL structure.
@param Volatile Point out the Variable is Volatile or Non-Volatile.
@param SetByIndex TRUE if target pointer is given as index.
FALSE if target pointer is absolute.
@param Fvb Pointer to the writable FVB protocol.
@param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER
structure.
@param DataSize Size of data to be written.
@param Buffer Pointer to the buffer from which data is written.
@retval EFI_INVALID_PARAMETER Parameters not valid.
@retval EFI_SUCCESS Variable store successfully updated.
**/
)
{
FwVolHeader = NULL;
//
// Check if the Data is Volatile.
//
if (!Volatile) {
//
// Data Pointer should point to the actual Address where data is to be
// written.
//
if (SetByIndex) {
}
if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {
return EFI_INVALID_PARAMETER;
}
} else {
//
// Data Pointer should point to the actual Address where data is to be
// written.
//
VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
if (SetByIndex) {
}
return EFI_INVALID_PARAMETER;
}
//
// If Volatile Variable just do a simple mem copy.
//
return EFI_SUCCESS;
}
//
// If we are here we are dealing with Non-Volatile Variables.
//
CurrBuffer = Buffer;
LbaNumber = 0;
if (CurrWritePtr < LinearOffset) {
return EFI_INVALID_PARAMETER;
}
for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
//
// Check to see if the Variable Writes are spanning through multiple
// blocks.
//
Fvb,
);
return Status;
} else {
Fvb,
&Size,
);
return Status;
}
}
}
LbaNumber++;
}
}
return EFI_SUCCESS;
}
/**
This code gets the current status of Variable Store.
@param VarStoreHeader Pointer to the Variable Store Header.
@retval EfiRaw Variable store status is raw.
@retval EfiValid Variable store status is valid.
@retval EfiInvalid Variable store status is invalid.
**/
)
{
) {
return EfiValid;
) {
return EfiRaw;
} else {
return EfiInvalid;
}
}
/**
This code gets the size of name of variable.
@param Variable Pointer to the Variable Header.
@return UINTN Size of variable in bytes.
**/
)
{
return 0;
}
}
/**
This code gets the size of variable data.
@param Variable Pointer to the Variable Header.
@return Size of variable in bytes.
**/
)
{
return 0;
}
}
/**
This code gets the pointer to the variable name.
@param Variable Pointer to the Variable Header.
@return Pointer to Variable Name which is Unicode encoding.
**/
CHAR16 *
)
{
}
/**
This code gets the pointer to the variable data.
@param Variable Pointer to the Variable Header.
@return Pointer to Variable Data.
**/
UINT8 *
)
{
//
// Be careful about pad size for alignment.
//
}
/**
This code gets the pointer to the next variable header.
@param Variable Pointer to the Variable Header.
@return Pointer to next variable header.
**/
)
{
if (!IsValidVariableHeader (Variable)) {
return NULL;
}
//
// Be careful about pad size for alignment.
//
}
/**
Gets the pointer to the first variable header in given variable store area.
@param VarStoreHeader Pointer to the Variable Store Header.
@return Pointer to the first variable header.
**/
)
{
//
// The end of variable store.
//
}
/**
Gets the pointer to the end of the variable storage area.
This function gets pointer to the end of the variable storage
area, according to the input variable store header.
@param VarStoreHeader Pointer to the Variable Store Header.
@return Pointer to the end of the variable storage area.
**/
)
{
//
// The end of variable store
//
}
/**
Variable store garbage collection and reclaim operation.
@param VariableBase Base address of variable store.
@param LastVariableOffset Offset of last variable.
@param IsVolatile The variable store is volatile or not;
if it is non-volatile, need FTW.
@param UpdatingVariable Pointer to updating variable.
@return EFI_OUT_OF_RESOURCES
@return EFI_SUCCESS
@return Others
**/
Reclaim (
)
{
//
//
if (!IsVolatile) {
}
//
// Start Pointers for the variable.
//
MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
while (IsValidVariableHeader (Variable)) {
) {
}
}
//
// Reserve the 1 Bytes with Oxff to identify the
// end of the variable buffer.
//
MaximumBufferSize += 1;
if (ValidBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Copy variable store header.
//
//
// Reinstall all ADDED variables as long as they are not identical to Updating Variable.
//
while (IsValidVariableHeader (Variable)) {
if (UpdatingVariable != NULL) {
if (UpdatingVariable == Variable) {
continue;
}
continue;
}
}
CurrPtr += VariableSize;
if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
} else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
}
}
}
//
// Reinstall the variable being updated if it is not NULL.
//
if (UpdatingVariable != NULL) {
CurrPtr += VariableSize;
if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
} else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
}
}
//
// Reinstall all in delete transition variables.
//
while (IsValidVariableHeader (Variable)) {
//
// Buffer has cached all ADDED variable.
// Per IN_DELETED variable, we have to guarantee that
// no ADDED one in previous buffer.
//
FoundAdded = FALSE;
while (IsValidVariableHeader (AddedVariable)) {
) {
FoundAdded = TRUE;
break;
}
}
}
if (!FoundAdded) {
//
// Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.
//
CurrPtr += VariableSize;
if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
} else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
}
}
}
}
if (IsVolatile) {
//
// If volatile variable store, just copy valid buffer.
//
} else {
//
// If non-volatile variable store, perform FTW here.
//
);
}
} else {
*LastVariableOffset = 0;
}
return Status;
}
/**
Find the variable in the specified variable store.
@param VariableName Name of the variable to be found
@param VendorGuid Vendor GUID to be found.
@param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
check at runtime when searching variable.
@param PtrTrack Variable Track Pointer structure that contains Variable Information.
@retval EFI_SUCCESS Variable found successfully
@retval EFI_NOT_FOUND Variable not found
**/
)
{
//
// Find the variable by walk through HOB, volatile and non-volatile variable store.
//
) {
) {
if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
if (VariableName[0] == 0) {
} else {
return EFI_SUCCESS;
}
} else {
} else {
return EFI_SUCCESS;
}
}
}
}
}
}
}
}
/**
Finds variable in storage blocks of volatile and non-volatile storage areas.
This code finds variable in storage blocks of volatile and non-volatile storage areas.
If VariableName is an empty string, then we just return the first
qualified variable without comparing VariableName and VendorGuid.
If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check
at runtime when searching existing variable, only VariableName and VendorGuid are compared.
Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.
@param VariableName Name of the variable to be found.
@param VendorGuid Vendor GUID to be found.
@param PtrTrack VARIABLE_POINTER_TRACK structure for output,
including the range searched and the target position.
@param Global Pointer to VARIABLE_GLOBAL structure, including
base of volatile variable storage area, base of
NV variable storage area, and a lock.
@param IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute
check at runtime when searching variable.
@retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
VendorGuid is NULL.
@retval EFI_SUCCESS Variable successfully found.
@retval EFI_NOT_FOUND Variable not found
**/
)
{
return EFI_INVALID_PARAMETER;
}
//
// 0: Volatile, 1: HOB, 2: Non-Volatile.
// The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
// make use of this mapping to implement search algorithm.
//
VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;
VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;
//
// Find the variable by walk through HOB, volatile and non-volatile variable store.
//
continue;
}
return Status;
}
}
return EFI_NOT_FOUND;
}
/**
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
RFC4646 and ISO639 language tags.
In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
In RFC4646 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 RFC4646.
@retval The index of language in the language codes.
**/
)
{
if (Iso639Language) {
//
// Successfully find the index of Lang string in SupportedLang string.
//
return Index;
}
}
return 0;
} else {
//
// Compare RFC4646 language code
//
Index = 0;
//
// Skip ';' characters in SupportedLang
//
//
// Determine the length of the next language code in SupportedLang
//
for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
if ((CompareLength == LanguageLength) &&
//
// Successfully find the index of Lang string in SupportedLang string.
//
return Index;
}
}
return 0;
}
}
/**
Get language string from supported language codes according to index.
This code is used to get corresponding language strings in supported language codes. It can handle
RFC4646 and ISO639 language tags.
In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
In RFC4646 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 RFC4646.
@retval The language string in the language codes.
**/
CHAR8 *
)
{
SubIndex = 0;
if (Iso639Language) {
//
// According to the index of Lang string in SupportedLang string to get the language.
// In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
//
} else {
while (TRUE) {
//
// Take semicolon as delimitation, sequentially traverse supported language codes.
//
Supported++;
}
//
// Have completed the traverse, but not find corrsponding string.
// This case is not allowed to happen.
//
return NULL;
}
//
// According to the index of Lang string in SupportedLang string to get the language.
// In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
//
}
SubIndex++;
//
// Skip ';' characters in Supported
//
}
}
}
/**
Returns a pointer to an allocated buffer that contains the best matching language
from a set of supported languages.
This function supports both ISO 639-2 and RFC 4646 language codes, but language
code types may not be mixed in a single call to this function. This function
supports a variable argument list that allows the caller to pass in a prioritized
list of language codes to test against all the language codes in SupportedLanguages.
If SupportedLanguages is NULL, then ASSERT().
@param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
contains a set of language codes in the format
specified by Iso639Language.
@param[in] Iso639Language If TRUE, then all language codes are assumed to be
in ISO 639-2 format. If FALSE, then all language
codes are assumed to be in RFC 4646 language format
@param[in] ... A variable argument list that contains pointers to
Null-terminated ASCII strings that contain one or more
language codes in the format specified by Iso639Language.
The first language code from each of these language
code lists is used to determine if it is an exact or
close match to any of the language codes in
SupportedLanguages. Close matches only apply to RFC 4646
language codes, and the matching algorithm from RFC 4647
is used to determine if a close match is present. If
an exact or close match is found, then the matching
language code from SupportedLanguages is returned. If
no matches are found, then the next variable argument
parameter is evaluated. The variable argument list
is terminated by a NULL.
@retval NULL The best matching language could not be found in SupportedLanguages.
@retval NULL There are not enough resources available to return the best matching
language.
@retval Other A pointer to a Null-terminated ASCII string that is the best matching
language in SupportedLanguages.
**/
CHAR8 *
...
)
{
//
// Default to ISO 639-2 mode
//
CompareLength = 3;
//
// If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
//
if (!Iso639Language) {
for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
}
//
// Trim back the length of Language used until it is empty
//
while (LanguageLength > 0) {
//
// Loop through all language codes in SupportedLanguages
//
//
// In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
//
if (!Iso639Language) {
//
// Skip ';' characters in Supported
//
//
// Determine the length of the next language code in Supported
//
for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
//
// If Language is longer than the Supported, then skip to the next language
//
if (LanguageLength > CompareLength) {
continue;
}
}
//
// See if the first LanguageLength characters in Supported match Language
//
}
}
if (Iso639Language) {
//
// If ISO 639 mode, then each language can only be tested once
//
LanguageLength = 0;
} else {
//
// If RFC 4646 mode, then trim Language from the right to the next '-' character
//
}
}
}
//
// No matches were found
//
return NULL;
}
/**
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.
@param[in] VariableName Name of variable.
@param[in] Data Variable data.
@param[in] DataSize Size of data. 0 means delete.
**/
)
{
//
// Don't do updates for delete operation
//
if (DataSize == 0) {
return;
}
//
// PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
//
if (AtRuntime ()) {
return;
}
//
// 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.
//
}
//
// PlatformLang holds a single language from PlatformLangCodes,
// so the size of PlatformLangCodes is enough for the PlatformLang.
//
}
//
// LangCodes is a volatile variable, so it can not be updated at runtime.
//
if (AtRuntime ()) {
return;
}
//
// 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.
//
}
}
if (SetLanguageCodes
//
// Update Lang if PlatformLang is already set
// Update PlatformLang if Lang is already set
//
Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
//
// Update Lang
//
VariableName = L"PlatformLang";
} else {
Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
//
// Update PlatformLang
//
VariableName = L"Lang";
} else {
//
// Neither PlatformLang nor Lang is set, directly return
//
return;
}
}
}
//
// 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;
//
// Update Lang when PlatformLangCodes/LangCodes were set.
//
if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
//
// When setting PlatformLang, firstly get most matched language string from supported language codes.
//
BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
if (BestPlatformLang != NULL) {
//
// Get the corresponding index in language codes.
//
Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
//
// Get the corresponding ISO639 language tag according to RFC4646 language tag.
//
//
// Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
//
FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal, FALSE);
DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
}
}
//
// Update PlatformLang when PlatformLangCodes/LangCodes were set.
//
if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {
//
// When setting Lang, firstly get most matched language string from supported language codes.
//
//
// Get the corresponding index in language codes.
//
//
// Get the corresponding RFC4646 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, &mVariableModuleGlobal->VariableGlobal, FALSE);
DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
}
}
}
}
/**
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] CacheVariable 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.
**/
)
{
if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {
//
// The FVB protocol is not ready. Trying to update NV variable prior to the installation
// of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
//
return EFI_NOT_AVAILABLE_YET;
}
} else {
//
// CacheVariable points to the variable in the memory copy of Flash area
// Now let Variable points to the same variable in Flash area.
//
VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
Variable = &NvVariable;
Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));
}
//
//
if (AtRuntime ()) {
//
// If AtRuntime and the variable is Volatile and Runtime Access,
// the volatile is ReadOnly, and SetVariable should be aborted and
// return EFI_WRITE_PROTECTED.
//
goto Done;
}
//
//
if (((Variable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0)) {
goto Done;
}
}
//
// Setting a data variable with no access, or zero DataSize attributes
// causes it to be deleted.
//
if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
State &= VAR_DELETED;
Fvb,
sizeof (UINT8),
);
}
}
goto Done;
}
//
// If the variable is marked valid, and the same data has been passed in,
// then return to the caller immediately.
//
goto Done;
//
// Mark the old variable as in delete transition.
//
Fvb,
sizeof (UINT8),
);
goto Done;
}
}
}
} else {
//
// Not found existing variable. Create a new variable.
//
//
// Make sure we are trying to create a new variable.
// Setting a data variable with zero DataSize or no access attributes means to delete it.
//
if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
goto Done;
}
//
// Only variable have NV|RT attribute can be created in Runtime.
//
if (AtRuntime () &&
(((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
goto Done;
}
}
//
// Function part - create a new variable and copy the data.
// Both update a variable and create a variable will come here.
//
// Tricky part: Use scratch data area at the end of volatile variable store
// as a temporary storage.
//
NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));
//
// NextVariable->State = VAR_ADDED;
//
NextVariable->Reserved = 0;
VarNameOffset = sizeof (VARIABLE_HEADER);
CopyMem (
);
CopyMem (
Data,
);
//
// There will be pad bytes after Data, the NextVariable->NameSize and
// NextVariable->DataSize should not include pad size so that variable
// service can get actual size in GetVariable.
//
//
// The actual size of the variable that stores in storage should
// include pad size.
//
if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
//
// Create a nonvolatile variable.
//
NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;
if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
|| (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
&& ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
if (AtRuntime ()) {
goto Done;
}
//
// Perform garbage collection & reclaim operation.
//
goto Done;
}
//
// If still no enough space, return out of resources.
//
if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
|| (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
&& ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
goto Done;
}
}
//
// Four steps
// 1. Write variable header
// 2. Set variable state to header valid
// 3. Write variable data
// 4. Set variable state to valid
//
//
// Step 1:
//
TRUE,
Fvb,
sizeof (VARIABLE_HEADER),
(UINT8 *) NextVariable
);
goto Done;
}
//
// Step 2:
//
TRUE,
Fvb,
sizeof (UINT8),
);
goto Done;
}
//
// Step 3:
//
TRUE,
Fvb,
);
goto Done;
}
//
// Step 4:
//
TRUE,
Fvb,
sizeof (UINT8),
);
goto Done;
}
if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
} else {
}
//
// update the memory copy of Flash region.
//
} else {
//
// Create a volatile variable.
//
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
//
// Perform garbage collection & reclaim operation.
//
goto Done;
}
//
// If still no enough space, return out of resources.
//
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size
) {
goto Done;
}
}
TRUE,
TRUE,
Fvb,
(UINT8 *) NextVariable
);
goto Done;
}
}
//
// Mark the old variable as deleted.
//
State &= VAR_DELETED;
Fvb,
sizeof (UINT8),
);
}
}
}
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.
**/
)
{
return EFI_INVALID_PARAMETER;
}
Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
goto Done;
}
//
// Get data size
//
ASSERT (VarDataSize != 0);
if (*DataSize >= VarDataSize) {
goto Done;
}
if (Attributes != NULL) {
}
*DataSize = VarDataSize;
goto Done;
} else {
*DataSize = VarDataSize;
goto Done;
}
Done:
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.
**/
)
{
return EFI_INVALID_PARAMETER;
}
Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
goto Done;
}
if (VariableName[0] != 0) {
//
// If variable name is not NULL, get next variable.
//
}
//
// 0: Volatile, 1: HOB, 2: Non-Volatile.
// The index and attributes mapping must be kept in this order as FindVariable
// makes use of this mapping to implement search algorithm.
//
VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;
VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
while (TRUE) {
//
// Switch from Volatile to HOB, to Non-Volatile.
//
) {
//
// Find current storage index
//
if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {
break;
}
}
//
// Switch to next storage
//
break;
}
}
//
// Capture the case that
// 1. current storage is the last one, or
// 2. no further storage
//
if (Type == VariableStoreTypeMax) {
goto Done;
}
}
//
// Variable is found
//
//
// Don't return NV variable when HOB overrides it
//
if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&
) {
Status = FindVariableEx (
);
continue;
}
}
ASSERT (VarNameSize != 0);
if (VarNameSize <= *VariableNameSize) {
} else {
}
goto Done;
}
}
}
Done:
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.
**/
)
{
//
// Check input parameters.
//
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
//
// Not support authenticated variable write yet.
//
if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
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 PcdGet32 (PcdMaxHardwareErrorVariableSize)
// bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
//
(sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {
return EFI_INVALID_PARAMETER;
}
//
// According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".
//
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 PcdGet32 (PcdMaxVariableSize) bytes.
//
return EFI_INVALID_PARAMETER;
}
}
//
//
//
// Parse non-volatile variable data and get last variable offset.
//
&& IsValidVariableHeader (NextVariable)) {
}
}
//
// Check whether the input variable is already existed.
//
Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);
return EFI_WRITE_PROTECTED;
}
}
//
// Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.
//
return Status;
}
/**
This code returns information about the EFI variables.
@param Attributes Attributes bitmask to specify the type of variables
on which to return information.
@param MaximumVariableStorageSize Pointer to the maximum size of the storage space available
for the EFI variables associated with the attributes specified.
@param RemainingVariableStorageSize Pointer to the remaining size of the storage space available
for EFI variables associated with the attributes specified.
@param MaximumVariableSize Pointer to the maximum size of an individual EFI variables
associated with the attributes specified.
@return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
@return EFI_SUCCESS Query successfully.
@return EFI_UNSUPPORTED The attribute is not supported on this platform.
**/
)
{
if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
return EFI_INVALID_PARAMETER;
}
if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
//
// Make sure the Attributes combination is supported by the platform.
//
return EFI_UNSUPPORTED;
} else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
//
// Make sure if runtime bit is set, boot service bit is set also.
//
return EFI_INVALID_PARAMETER;
//
// Make sure RT Attribute is set if we are in Runtime phase.
//
return EFI_INVALID_PARAMETER;
} else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
//
// Make sure Hw Attribute is set with NV.
//
return EFI_INVALID_PARAMETER;
} else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
//
// Not support authentiated variable write yet.
//
return EFI_UNSUPPORTED;
}
if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
//
// Query is Volatile related.
//
VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
} else {
//
// Query is Non-Volatile related.
//
}
//
// Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
// with the storage size (excluding the storage header size).
//
//
// Harware error record variable needs larger size.
//
if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
} else {
if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
*MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
}
//
// Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
//
}
//
// Point to the starting address of the variables.
//
//
// Now walk through the related variable store.
//
if (AtRuntime ()) {
//
// We don't take the state of the variables in mind
// when calculating RemainingVariableStorageSize,
// since the space occupied by variables not marked with
// VAR_ADDED is not allowed to be reclaimed in Runtime.
//
if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
} else {
}
} else {
//
// Only care about Variables with State VAR_ADDED, because
// the space not marked as VAR_ADDED is reclaimable now.
//
if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
} else {
}
}
}
//
// Go to the next one.
//
}
}else {
}
if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
*MaximumVariableSize = 0;
}
return EFI_SUCCESS;
}
/**
This function reclaims variable storage if free size is below the threshold.
**/
)
{
CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space
RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
//
// Check if the free area is blow a threshold.
//
|| ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
);
}
}
/**
Initializes variable write service after FVB was ready.
@retval EFI_SUCCESS Function successfully executed.
@retval Others Fail to initialize the variable service.
**/
)
{
//
// Check if the free area is really free.
//
for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
if (Data != 0xff) {
//
// There must be something wrong in variable store, do reclaim operation.
//
);
return Status;
}
break;
}
}
//
// Flush the HOB variable to flash and invalidate HOB variable.
//
//
// Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB
//
VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;
) {
);
}
}
return EFI_SUCCESS;
}
/**
Initializes variable store area for non-volatile and volatile variable.
@retval EFI_SUCCESS Function successfully executed.
@retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
**/
)
{
//
// Allocate runtime memory for variable driver global structure.
//
if (mVariableModuleGlobal == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Note that in EdkII variable driver implementation, Hardware Error Record type variable
// is stored with common variable in the same NV region. So the platform integrator should
// ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
// PcdFlashNvStorageVariableSize.
//
//
// Get HOB variable store.
//
mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;
} else {
}
}
//
// Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.
//
if (VolatileVariableStore == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Initialize Variable Specific Data.
//
mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;
//
// Get non-volatile variable store.
//
if (TempVariableStoreHeader == 0) {
}
//
// Check if the Firmware Volume is not corrupted
//
if ((((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->Signature != EFI_FVH_SIGNATURE) ||
(!CompareGuid (&gEfiSystemNvDataFvGuid, &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader))->FileSystemGuid))) {
goto Done;
}
goto Done;
}
//
// Parse non-volatile variable data and get last variable offset.
//
while (IsValidVariableHeader (NextVariable)) {
if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
} else {
}
}
mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;
//
// Allocate runtime memory used for a memory copy of the FLASH region.
// Keep the memory and the FLASH in sync as updates occur
//
if (mNvVariableCache == NULL) {
goto Done;
}
Done:
}
return Status;
}
/**
@param[in] Address The Flash address.
@param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.
@param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.
**/
)
{
//
// Get all FVB handles.
//
return EFI_NOT_FOUND;
}
//
// Get the FVB to access variable store.
//
break;
}
//
// Ensure this FVB protocol supported Write operation.
//
continue;
}
//
// Compare the address and select the right one.
//
continue;
}
}
if (FvbProtocol != NULL) {
*FvbProtocol = Fvb;
}
break;
}
}
}
return Status;
}