--- /dev/null
+/** @file\r
+ The implementation of Extended SAL variable services.\r
+\r
+Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials \r
+are licensed and made available under the terms and conditions of the BSD License \r
+which accompanies this distribution. The full text of the license may be found at \r
+http://opensource.org/licenses/bsd-license.php\r
+\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
+**/\r
+\r
+#include "Variable.h"\r
+#include "AuthService.h"\r
+\r
+//\r
+// Don't use module globals after the SetVirtualAddress map is signaled\r
+//\r
+ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
+CHAR16 *mVariableName[NUM_VAR_NAME] = {\r
+ L"PlatformLangCodes",\r
+ L"LangCodes",\r
+ L"PlatformLang",\r
+ L"Lang",\r
+ L"HwErrRec",\r
+ AUTHVAR_KEYDB_NAME,\r
+ EFI_SETUP_MODE_NAME,\r
+ EFI_PLATFORM_KEY_NAME,\r
+ EFI_KEY_EXCHANGE_KEY_NAME\r
+};\r
+\r
+GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
+\r
+//\r
+// The current Hii implementation accesses this variable a larg # of times on every boot.\r
+// Other common variables are only accessed a single time. This is why this cache algorithm\r
+// only targets a single variable. Probably to get an performance improvement out of\r
+// a Cache you would need a cache that improves the search performance for a variable.\r
+//\r
+VARIABLE_CACHE_ENTRY mVariableCache[] = {\r
+ {\r
+ &gEfiGlobalVariableGuid,\r
+ L"Lang",\r
+ 0x00000000,\r
+ 0x00,\r
+ NULL\r
+ },\r
+ {\r
+ &gEfiGlobalVariableGuid,\r
+ L"PlatformLang",\r
+ 0x00000000,\r
+ 0x00,\r
+ NULL\r
+ }\r
+};\r
+\r
+/**\r
+ Acquires lock only at boot time. Simply returns at runtime.\r
+\r
+ This is a temperary function which will be removed when\r
+ EfiAcquireLock() in UefiLib can handle the call in UEFI\r
+ Runtimer driver in RT phase.\r
+ It calls EfiAcquireLock() at boot time, and simply returns\r
+ at runtime.\r
+\r
+ @param[in] Lock A pointer to the lock to acquire.\r
+\r
+**/\r
+VOID\r
+AcquireLockOnlyAtBootTime (\r
+ IN EFI_LOCK *Lock\r
+ )\r
+{\r
+ if (!EfiAtRuntime ()) {\r
+ EfiAcquireLock (Lock);\r
+ }\r
+}\r
+\r
+/**\r
+ Releases lock only at boot time. Simply returns at runtime.\r
+\r
+ This is a temperary function which will be removed when\r
+ EfiReleaseLock() in UefiLib can handle the call in UEFI\r
+ Runtimer driver in RT phase.\r
+ It calls EfiReleaseLock() at boot time, and simply returns\r
+ at runtime\r
+\r
+ @param[in] Lock A pointer to the lock to release.\r
+\r
+**/\r
+VOID\r
+ReleaseLockOnlyAtBootTime (\r
+ IN EFI_LOCK *Lock\r
+ )\r
+{\r
+ if (!EfiAtRuntime ()) {\r
+ EfiReleaseLock (Lock);\r
+ }\r
+}\r
+\r
+/**\r
+ Reads/Writes variable storage, volatile or non-volatile.\r
+\r
+ This function reads or writes volatile or non-volatile variable stroage.\r
+ For volatile storage, it performs memory copy.\r
+ For non-volatile storage, it accesses data on firmware storage. Data\r
+ area to access can span multiple firmware blocks.\r
+\r
+ @param[in] Write TRUE - Write variable store.\r
+ FALSE - Read variable store.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Instance Instance of FV Block services.\r
+ @param[in] StartAddress Start address of data to access.\r
+ @param[in] DataSize Size of data to access.\r
+ @param[in, out] Buffer For write, pointer to the buffer from which data is written.\r
+ For read, pointer to the buffer to hold the data read.\r
+\r
+ @retval EFI_SUCCESS Variable store successfully accessed.\r
+ @retval EFI_INVALID_PARAMETER Data area to access exceeds valid variable storage.\r
+\r
+**/\r
+EFI_STATUS\r
+AccessVariableStore (\r
+ IN BOOLEAN Write,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN BOOLEAN Volatile,\r
+ IN UINTN Instance,\r
+ IN EFI_PHYSICAL_ADDRESS StartAddress,\r
+ IN UINT32 DataSize,\r
+ IN OUT VOID *Buffer\r
+ )\r
+{\r
+ EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
+ UINTN BlockIndex;\r
+ UINTN LinearOffset;\r
+ UINTN CurrWriteSize;\r
+ UINTN CurrWritePtr;\r
+ UINT8 *CurrBuffer;\r
+ EFI_LBA LbaNumber;\r
+ UINTN Size;\r
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
+ VARIABLE_STORE_HEADER *VolatileBase;\r
+ EFI_PHYSICAL_ADDRESS FvVolHdr;\r
+ EFI_STATUS Status;\r
+ VARIABLE_STORE_HEADER *VariableStoreHeader;\r
+\r
+ FvVolHdr = 0;\r
+ FwVolHeader = NULL;\r
+\r
+ if (Volatile) {\r
+ //\r
+ // If data is volatile, simply calculate the data pointer and copy memory.\r
+ // Data pointer should point to the actual address where data is to be\r
+ // accessed.\r
+ //\r
+ VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
+\r
+ if ((StartAddress + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+ //\r
+ // For volatile variable, a simple memory copy is enough.\r
+ //\r
+ if (Write) {\r
+ CopyMem ((VOID *) StartAddress, Buffer, DataSize);\r
+ } else {\r
+ CopyMem (Buffer, (VOID *) StartAddress, DataSize);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // If data is non-volatile, calculate firmware volume header and data pointer.\r
+ //\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ GetPhysicalAddressFunctionId, \r
+ Instance, \r
+ (UINT64) &FvVolHdr, \r
+ 0, \r
+ 0, \r
+ 0, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
+ ASSERT (FwVolHeader != NULL);\r
+ VariableStoreHeader = (VARIABLE_STORE_HEADER *)(FwVolHeader + 1);\r
+\r
+ if ((StartAddress + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((CHAR8 *)VariableStoreHeader + VariableStoreHeader->Size))) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+ LinearOffset = (UINTN) FwVolHeader;\r
+ CurrWritePtr = StartAddress;\r
+ CurrWriteSize = DataSize;\r
+ CurrBuffer = Buffer;\r
+ LbaNumber = 0;\r
+\r
+ if (CurrWritePtr < LinearOffset) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Traverse data blocks of this firmware storage to find the one where CurrWritePtr locates\r
+ //\r
+ for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
+ for (BlockIndex = 0; BlockIndex < PtrBlockMapEntry->NumBlocks; BlockIndex++) {\r
+ if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r
+ //\r
+ // Check to see if the data area to access spans multiple blocks.\r
+ //\r
+ if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r
+ //\r
+ // If data area to access is contained in one block, just access and return.\r
+ //\r
+ if (Write) {\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ WriteFunctionId, \r
+ Instance, \r
+ LbaNumber, \r
+ (CurrWritePtr - LinearOffset), \r
+ (UINT64) &CurrWriteSize, \r
+ (UINT64) CurrBuffer, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ } else {\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ ReadFunctionId, \r
+ Instance, \r
+ LbaNumber, \r
+ (CurrWritePtr - LinearOffset), \r
+ (UINT64) &CurrWriteSize, \r
+ (UINT64) CurrBuffer, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ }\r
+ return Status;\r
+ } else {\r
+ //\r
+ // If data area to access spans multiple blocks, access this one and adjust for the next one.\r
+ //\r
+ Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
+ if (Write) {\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ WriteFunctionId, \r
+ Instance, \r
+ LbaNumber, \r
+ (CurrWritePtr - LinearOffset), \r
+ (UINT64) &Size, \r
+ (UINT64) CurrBuffer, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ } else {\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ ReadFunctionId, \r
+ Instance, \r
+ LbaNumber, \r
+ (CurrWritePtr - LinearOffset), \r
+ (UINT64) &Size, \r
+ (UINT64) CurrBuffer, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ }\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ //\r
+ // Adjust for the remaining data.\r
+ //\r
+ CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r
+ CurrBuffer = CurrBuffer + Size;\r
+ CurrWriteSize = CurrWriteSize - Size;\r
+ }\r
+ }\r
+\r
+ LinearOffset += PtrBlockMapEntry->Length;\r
+ LbaNumber++;\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Retrieves header of volatile or non-volatile variable stroage.\r
+\r
+ @param[in] VarStoreAddress Start address of variable storage.\r
+ @param[in] Volatile TRUE - Variable storage is volatile.\r
+ FALSE - Variable storage is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+ @param[out] VarStoreHeader Pointer to VARIABLE_STORE_HEADER for output.\r
+\r
+**/\r
+VOID\r
+GetVarStoreHeader (\r
+ IN EFI_PHYSICAL_ADDRESS VarStoreAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance,\r
+ OUT VARIABLE_STORE_HEADER *VarStoreHeader\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ Global,\r
+ Volatile,\r
+ Instance,\r
+ VarStoreAddress,\r
+ sizeof (VARIABLE_STORE_HEADER),\r
+ VarStoreHeader \r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+ Checks variable header.\r
+\r
+ This function checks if variable header is valid or not.\r
+\r
+ @param[in] VariableAddress Start address of variable header.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+ @param[out] VariableHeader Pointer to VARIABLE_HEADER for output.\r
+\r
+ @retval TRUE Variable header is valid.\r
+ @retval FALSE Variable header is not valid.\r
+\r
+**/\r
+BOOLEAN\r
+IsValidVariableHeader (\r
+ IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance,\r
+ OUT VARIABLE_HEADER *VariableHeader OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VARIABLE_HEADER LocalVariableHeader;\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ Global,\r
+ Volatile,\r
+ Instance,\r
+ VariableAddress,\r
+ sizeof (VARIABLE_HEADER),\r
+ &LocalVariableHeader \r
+ );\r
+\r
+ if (EFI_ERROR (Status) || LocalVariableHeader.StartId != VARIABLE_DATA) {\r
+ return FALSE;\r
+ }\r
+\r
+ if (VariableHeader != NULL) {\r
+ CopyMem (VariableHeader, &LocalVariableHeader, sizeof (VARIABLE_HEADER));\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ Gets status of variable store.\r
+\r
+ This function gets the current status of variable store.\r
+\r
+ @param[in] VarStoreHeader Pointer to header of variable store.\r
+\r
+ @retval EfiRaw Variable store status is raw.\r
+ @retval EfiValid Variable store status is valid.\r
+ @retval EfiInvalid Variable store status is invalid.\r
+\r
+**/\r
+VARIABLE_STORE_STATUS\r
+GetVariableStoreStatus (\r
+ IN VARIABLE_STORE_HEADER *VarStoreHeader\r
+ )\r
+{\r
+\r
+ if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&\r
+ VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r
+ VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r
+ ) {\r
+\r
+ return EfiValid;\r
+ } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r
+ ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r
+ VarStoreHeader->Size == 0xffffffff &&\r
+ VarStoreHeader->Format == 0xff &&\r
+ VarStoreHeader->State == 0xff\r
+ ) {\r
+\r
+ return EfiRaw;\r
+ } else {\r
+ return EfiInvalid;\r
+ }\r
+}\r
+\r
+/**\r
+ Gets the size of variable name.\r
+\r
+ This function gets the size of variable name.\r
+ The variable is specified by its variable header.\r
+ If variable header contains raw data, just return 0.\r
+\r
+ @param[in] Variable Pointer to the variable header.\r
+\r
+ @return Size of variable name in bytes.\r
+\r
+**/\r
+UINTN\r
+NameSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ if (Variable->State == (UINT8) (-1) ||\r
+ Variable->DataSize == (UINT32) -1 ||\r
+ Variable->NameSize == (UINT32) -1 ||\r
+ Variable->Attributes == (UINT32) -1) {\r
+ return 0;\r
+ }\r
+ return (UINTN) Variable->NameSize;\r
+}\r
+\r
+/**\r
+ Gets the size of variable data area.\r
+\r
+ This function gets the size of variable data area.\r
+ The variable is specified by its variable header.\r
+ If variable header contains raw data, just return 0.\r
+\r
+ @param[in] Variable Pointer to the variable header.\r
+\r
+ @return Size of variable data area in bytes.\r
+\r
+**/\r
+UINTN\r
+DataSizeOfVariable (\r
+ IN VARIABLE_HEADER *Variable\r
+ )\r
+{\r
+ if (Variable->State == (UINT8) -1 ||\r
+ Variable->DataSize == (UINT32) -1 ||\r
+ Variable->NameSize == (UINT32) -1 ||\r
+ Variable->Attributes == (UINT32) -1) {\r
+ return 0;\r
+ }\r
+ return (UINTN) Variable->DataSize;\r
+}\r
+\r
+/**\r
+ Gets the pointer to variable name.\r
+\r
+ This function gets the pointer to variable name.\r
+ The variable is specified by its variable header.\r
+\r
+ @param[in] VariableAddress Start address of variable header.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+ @param[out] VariableName Buffer to hold variable name for output.\r
+\r
+**/\r
+VOID\r
+GetVariableNamePtr (\r
+ IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance,\r
+ OUT CHAR16 *VariableName\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS Address;\r
+ VARIABLE_HEADER VariableHeader;\r
+ BOOLEAN IsValid;\r
+\r
+ IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
+ ASSERT (IsValid);\r
+\r
+ //\r
+ // Name area follows variable header.\r
+ //\r
+ Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ Global,\r
+ Volatile,\r
+ Instance,\r
+ Address,\r
+ VariableHeader.NameSize,\r
+ VariableName \r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+ Gets the pointer to variable data area.\r
+\r
+ This function gets the pointer to variable data area.\r
+ The variable is specified by its variable header.\r
+\r
+ @param[in] VariableAddress Start address of variable header.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+ @param[out] VariableData Buffer to hold variable data for output.\r
+\r
+**/\r
+VOID\r
+GetVariableDataPtr (\r
+ IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance,\r
+ OUT CHAR16 *VariableData\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS Address;\r
+ VARIABLE_HEADER VariableHeader;\r
+ BOOLEAN IsValid;\r
+\r
+ IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);\r
+ ASSERT (IsValid);\r
+\r
+ //\r
+ // Data area follows variable name.\r
+ // Be careful about pad size for alignment\r
+ //\r
+ Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
+ Address += NameSizeOfVariable (&VariableHeader);\r
+ Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ Global,\r
+ Volatile,\r
+ Instance,\r
+ Address,\r
+ VariableHeader.DataSize,\r
+ VariableData \r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+\r
+/**\r
+ Gets the pointer to the next variable header.\r
+\r
+ This function gets the pointer to the next variable header.\r
+ The variable is specified by its variable header.\r
+\r
+ @param[in] VariableAddress Start address of variable header.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+\r
+ @return Pointer to the next variable header.\r
+ NULL if variable header is invalid.\r
+\r
+**/\r
+EFI_PHYSICAL_ADDRESS\r
+GetNextVariablePtr (\r
+ IN EFI_PHYSICAL_ADDRESS VariableAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance\r
+ )\r
+{\r
+ EFI_PHYSICAL_ADDRESS Address;\r
+ VARIABLE_HEADER VariableHeader;\r
+\r
+ if (!IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader)) {\r
+ return 0x0;\r
+ }\r
+\r
+ //\r
+ // Header of next variable follows data area of this variable\r
+ //\r
+ Address = VariableAddress + sizeof (VARIABLE_HEADER);\r
+ Address += NameSizeOfVariable (&VariableHeader);\r
+ Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));\r
+ Address += DataSizeOfVariable (&VariableHeader);\r
+ Address += GET_PAD_SIZE (DataSizeOfVariable (&VariableHeader));\r
+\r
+ //\r
+ // Be careful about pad size for alignment\r
+ //\r
+ return HEADER_ALIGN (Address);\r
+}\r
+\r
+/**\r
+ Gets the pointer to the first variable header in given variable store area.\r
+\r
+ This function gets the pointer to the first variable header in given variable \r
+ store area. The variable store area is given by its start address.\r
+\r
+ @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.\r
+\r
+ @return Pointer to the first variable header.\r
+\r
+**/\r
+EFI_PHYSICAL_ADDRESS\r
+GetStartPointer (\r
+ IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress\r
+ )\r
+{\r
+ return HEADER_ALIGN (VarStoreHeaderAddress + sizeof (VARIABLE_STORE_HEADER));\r
+}\r
+\r
+/**\r
+ Gets the pointer to the end of given variable store area.\r
+\r
+ This function gets the pointer to the end of given variable store area.\r
+ The variable store area is given by its start address.\r
+\r
+ @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.\r
+ @param[in] Volatile TRUE - Variable is volatile.\r
+ FALSE - Variable is non-volatile.\r
+ @param[in] Global Pointer to VARAIBLE_GLOBAL structure.\r
+ @param[in] Instance Instance of FV Block services.\r
+\r
+ @return Pointer to the end of given variable store area.\r
+\r
+**/\r
+EFI_PHYSICAL_ADDRESS\r
+GetEndPointer (\r
+ IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress,\r
+ IN BOOLEAN Volatile,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ VARIABLE_STORE_HEADER VariableStoreHeader;\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ Global,\r
+ Volatile,\r
+ Instance,\r
+ VarStoreHeaderAddress,\r
+ sizeof (VARIABLE_STORE_HEADER),\r
+ &VariableStoreHeader \r
+ );\r
+\r
+ ASSERT_EFI_ERROR (Status);\r
+ return HEADER_ALIGN (VarStoreHeaderAddress + VariableStoreHeader.Size);\r
+}\r
+\r
+/**\r
+ Updates variable info entry in EFI system table for statistical information.\r
+\r
+ Routine used to track statistical information about variable usage. \r
+ The data is stored in the EFI system table so it can be accessed later.\r
+ VariableInfo.efi can dump out the table. Only Boot Services variable \r
+ accesses are tracked by this code. The PcdVariableCollectStatistics\r
+ build flag controls if this feature is enabled. \r
+ A read that hits in the cache will have Read and Cache true for \r
+ the transaction. Data is allocated by this routine, but never\r
+ freed.\r
+\r
+ @param[in] VariableName Name of the Variable to track.\r
+ @param[in] VendorGuid Guid of the Variable to track.\r
+ @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r
+ @param[in] Read TRUE if GetVariable() was called.\r
+ @param[in] Write TRUE if SetVariable() was called.\r
+ @param[in] Delete TRUE if deleted via SetVariable().\r
+ @param[in] Cache TRUE for a cache hit.\r
+\r
+**/\r
+VOID\r
+UpdateVariableInfo (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN BOOLEAN Volatile,\r
+ IN BOOLEAN Read,\r
+ IN BOOLEAN Write,\r
+ IN BOOLEAN Delete,\r
+ IN BOOLEAN Cache\r
+ )\r
+{\r
+ VARIABLE_INFO_ENTRY *Entry;\r
+\r
+ if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
+\r
+ if (EfiAtRuntime ()) {\r
+ //\r
+ // Don't collect statistics at runtime\r
+ //\r
+ return;\r
+ }\r
+\r
+ if (gVariableInfo == NULL) {\r
+ //\r
+ // on the first call allocate a entry and place a pointer to it in\r
+ // the EFI System Table\r
+ //\r
+ gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
+ ASSERT (gVariableInfo != NULL);\r
+\r
+ CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
+ gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
+ ASSERT (gVariableInfo->Name != NULL);\r
+ StrCpy (gVariableInfo->Name, VariableName);\r
+ gVariableInfo->Volatile = Volatile;\r
+\r
+ gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
+ }\r
+\r
+ \r
+ for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
+ if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
+ if (StrCmp (VariableName, Entry->Name) == 0) {\r
+ //\r
+ // Find the entry matching both variable name and vender GUID,\r
+ // and update counters for all types.\r
+ //\r
+ if (Read) {\r
+ Entry->ReadCount++;\r
+ }\r
+ if (Write) {\r
+ Entry->WriteCount++;\r
+ }\r
+ if (Delete) {\r
+ Entry->DeleteCount++;\r
+ }\r
+ if (Cache) {\r
+ Entry->CacheCount++;\r
+ }\r
+\r
+ return;\r
+ }\r
+ }\r
+\r
+ if (Entry->Next == NULL) {\r
+ //\r
+ // If the entry is not in the table add it.\r
+ // Next iteration of the loop will fill in the data\r
+ //\r
+ Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
+ ASSERT (Entry->Next != NULL);\r
+\r
+ CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
+ Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
+ ASSERT (Entry->Next->Name != NULL);\r
+ StrCpy (Entry->Next->Name, VariableName);\r
+ Entry->Next->Volatile = Volatile;\r
+ }\r
+\r
+ }\r
+ }\r
+}\r
+\r
+/**\r
+ Updates variable in cache.\r
+\r
+ This function searches the variable cache. If the variable to set exists in the cache,\r
+ it updates the variable in cache. It has the same parameters with UEFI SetVariable()\r
+ service.\r
+\r
+ @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
+ variable. Each VariableName is unique for each VendorGuid.\r
+ @param[in] VendorGuid A unique identifier for the vendor.\r
+ @param[in] Attributes Attributes bitmask to set for the variable.\r
+ @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the\r
+ variable to be deleted.\r
+ @param[in] Data The contents for the variable.\r
+\r
+**/\r
+VOID\r
+UpdateVariableCache (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN UINT32 Attributes,\r
+ IN UINTN DataSize,\r
+ IN VOID *Data\r
+ )\r
+{\r
+ VARIABLE_CACHE_ENTRY *Entry;\r
+ UINTN Index;\r
+\r
+ if (EfiAtRuntime ()) {\r
+ //\r
+ // Don't use the cache at runtime\r
+ //\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Searches cache for the variable to update. If it exists, update it.\r
+ //\r
+ for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
+ if (CompareGuid (VendorGuid, Entry->Guid)) {\r
+ if (StrCmp (VariableName, Entry->Name) == 0) { \r
+ Entry->Attributes = Attributes;\r
+ if (DataSize == 0) {\r
+ //\r
+ // If DataSize is 0, delete the variable.\r
+ //\r
+ if (Entry->DataSize != 0) {\r
+ FreePool (Entry->Data);\r
+ }\r
+ Entry->DataSize = DataSize;\r
+ } else if (DataSize == Entry->DataSize) {\r
+ //\r
+ // If size of data does not change, simply copy data\r
+ //\r
+ CopyMem (Entry->Data, Data, DataSize);\r
+ } else {\r
+ //\r
+ // If size of data changes, allocate pool and copy data.\r
+ //\r
+ Entry->Data = AllocatePool (DataSize);\r
+ Entry->DataSize = DataSize;\r
+ CopyMem (Entry->Data, Data, DataSize);\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Search the cache to check if the variable is in it.\r
+\r
+ This function searches the variable cache. If the variable to find exists, return its data\r
+ and attributes.\r
+\r
+ @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
+ variable. Each VariableName is unique for each VendorGuid.\r
+ @param[in] VendorGuid A unique identifier for the vendor\r
+ @param[out] Attributes Pointer to the attributes bitmask of the variable for output.\r
+ @param[in, out] DataSize On input, size of the buffer of Data.\r
+ On output, size of the variable's data.\r
+ @param[out] Data Pointer to the data buffer for output.\r
+\r
+ @retval EFI_SUCCESS VariableGuid & VariableName data was returned.\r
+ @retval EFI_NOT_FOUND No matching variable found in cache.\r
+ @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.\r
+\r
+**/\r
+EFI_STATUS\r
+FindVariableInCache (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT UINT32 *Attributes OPTIONAL,\r
+ IN OUT UINTN *DataSize,\r
+ OUT VOID *Data\r
+ )\r
+{\r
+ VARIABLE_CACHE_ENTRY *Entry;\r
+ UINTN Index;\r
+\r
+ if (EfiAtRuntime ()) {\r
+ //\r
+ // Don't use the cache at runtime\r
+ //\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ //\r
+ // Searches cache for the variable\r
+ //\r
+ for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r
+ if (CompareGuid (VendorGuid, Entry->Guid)) {\r
+ if (StrCmp (VariableName, Entry->Name) == 0) {\r
+ if (Entry->DataSize == 0) {\r
+ //\r
+ // Variable has been deleted so return EFI_NOT_FOUND\r
+ //\r
+ return EFI_NOT_FOUND;\r
+ } else if (Entry->DataSize > *DataSize) {\r
+ //\r
+ // If buffer is too small, return the size needed and EFI_BUFFER_TOO_SMALL\r
+ //\r
+ *DataSize = Entry->DataSize;\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ } else {\r
+ //\r
+ // If buffer is large enough, return the data\r
+ //\r
+ *DataSize = Entry->DataSize;\r
+ CopyMem (Data, Entry->Data, Entry->DataSize);\r
+ //\r
+ // If Attributes is not NULL, return the variable's attribute.\r
+ //\r
+ if (Attributes != NULL) {\r
+ *Attributes = Entry->Attributes;\r
+ }\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Finds variable in volatile and non-volatile storage areas.\r
+\r
+ This code finds variable in volatile and non-volatile storage areas.\r
+ If VariableName is an empty string, then we just return the first\r
+ qualified variable without comparing VariableName and VendorGuid.\r
+ Otherwise, VariableName and VendorGuid are compared.\r
+\r
+ @param[in] VariableName Name of the variable to be found.\r
+ @param[in] VendorGuid Vendor GUID to be found.\r
+ @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
+ including the range searched and the target position.\r
+ @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r
+ base of volatile variable storage area, base of\r
+ NV variable storage area, and a lock.\r
+ @param[in] Instance Instance of FV Block services.\r
+\r
+ @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
+ VendorGuid is NULL.\r
+ @retval EFI_SUCCESS Variable successfully found.\r
+ @retval EFI_INVALID_PARAMETER Variable not found.\r
+\r
+**/\r
+EFI_STATUS\r
+FindVariable (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
+ IN VARIABLE_GLOBAL *Global,\r
+ IN UINTN Instance\r
+ )\r
+{\r
+ EFI_PHYSICAL_ADDRESS Variable[2];\r
+ EFI_PHYSICAL_ADDRESS InDeletedVariable;\r
+ EFI_PHYSICAL_ADDRESS VariableStoreHeader[2];\r
+ UINTN InDeletedStorageIndex;\r
+ UINTN Index;\r
+ CHAR16 LocalVariableName[MAX_NAME_SIZE];\r
+ BOOLEAN Volatile;\r
+ VARIABLE_HEADER VariableHeader;\r
+\r
+ //\r
+ // 0: Volatile, 1: Non-Volatile\r
+ // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r
+ // make use of this mapping to implement search algorithme.\r
+ //\r
+ VariableStoreHeader[0] = Global->VolatileVariableBase;\r
+ VariableStoreHeader[1] = Global->NonVolatileVariableBase;\r
+\r
+ //\r
+ // Start Pointers for the variable.\r
+ // Actual Data Pointer where data can be written.\r
+ //\r
+ Variable[0] = GetStartPointer (VariableStoreHeader[0]);\r
+ Variable[1] = GetStartPointer (VariableStoreHeader[1]);\r
+\r
+ if (VariableName[0] != 0 && VendorGuid == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Find the variable by walk through volatile and then non-volatile variable store\r
+ //\r
+ InDeletedVariable = 0x0;\r
+ InDeletedStorageIndex = 0;\r
+ Volatile = TRUE;\r
+ for (Index = 0; Index < 2; Index++) {\r
+ if (Index == 1) {\r
+ Volatile = FALSE;\r
+ }\r
+ while (IsValidVariableHeader (Variable[Index], Volatile, Global, Instance, &VariableHeader)) {\r
+ if (VariableHeader.State == VAR_ADDED || \r
+ VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
+ ) {\r
+ if (!EfiAtRuntime () || ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r
+ if (VariableName[0] == 0) {\r
+ //\r
+ // If VariableName is an empty string, then we just find the first qualified variable\r
+ // without comparing VariableName and VendorGuid\r
+ //\r
+ if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ //\r
+ // If variable is in delete transition, record it.\r
+ //\r
+ InDeletedVariable = Variable[Index];\r
+ InDeletedStorageIndex = Index;\r
+ } else {\r
+ //\r
+ // If variable is not in delete transition, return it.\r
+ //\r
+ PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
+ PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);\r
+ PtrTrack->CurrPtr = Variable[Index];\r
+ PtrTrack->Volatile = Volatile;\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+ } else {\r
+ //\r
+ // If VariableName is not an empty string, then VariableName and VendorGuid are compared.\r
+ //\r
+ if (CompareGuid (VendorGuid, &VariableHeader.VendorGuid)) {\r
+ GetVariableNamePtr (\r
+ Variable[Index],\r
+ Volatile,\r
+ Global,\r
+ Instance,\r
+ LocalVariableName\r
+ );\r
+\r
+ ASSERT (NameSizeOfVariable (&VariableHeader) != 0);\r
+ if (CompareMem (VariableName, LocalVariableName, NameSizeOfVariable (&VariableHeader)) == 0) {\r
+ if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+ //\r
+ // If variable is in delete transition, record it.\r
+ // We will use if only no VAR_ADDED variable is found.\r
+ //\r
+ InDeletedVariable = Variable[Index];\r
+ InDeletedStorageIndex = Index;\r
+ } else {\r
+ //\r
+ // If variable is not in delete transition, return it.\r
+ //\r
+ PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r
+ PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);\r
+ PtrTrack->CurrPtr = Variable[Index];\r
+ PtrTrack->Volatile = Volatile;\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ Variable[Index] = GetNextVariablePtr (\r
+ Variable[Index],\r
+ Volatile,\r
+ Global,\r
+ Instance\r
+ );\r
+ }\r
+ if (InDeletedVariable != 0x0) {\r
+ //\r
+ // If no VAR_ADDED variable is found, and only variable in delete transition, then use this one.\r
+ //\r
+ PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);\r
+ PtrTrack->EndPtr = GetEndPointer (\r
+ VariableStoreHeader[InDeletedStorageIndex],\r
+ (BOOLEAN)(InDeletedStorageIndex == 0),\r
+ Global,\r
+ Instance\r
+ );\r
+ PtrTrack->CurrPtr = InDeletedVariable;\r
+ PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ PtrTrack->CurrPtr = 0x0;\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Variable store garbage collection and reclaim operation.\r
+\r
+ @param[in] VariableBase Base address of variable store area.\r
+ @param[out] LastVariableOffset Offset of last variable.\r
+ @param[in] IsVolatile The variable store is volatile or not,\r
+ if it is non-volatile, need FTW.\r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+ @param[in] UpdatingVariable Pointer to header of the variable that is being updated.\r
+\r
+ @retval EFI_SUCCESS Variable store successfully reclaimed.\r
+ @retval EFI_OUT_OF_RESOURCES Fail to allocate memory buffer to hold all valid variables.\r
+\r
+**/\r
+EFI_STATUS\r
+Reclaim (\r
+ IN EFI_PHYSICAL_ADDRESS VariableBase,\r
+ OUT UINTN *LastVariableOffset,\r
+ IN BOOLEAN IsVolatile,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global,\r
+ IN EFI_PHYSICAL_ADDRESS UpdatingVariable\r
+ )\r
+{\r
+ EFI_PHYSICAL_ADDRESS Variable;\r
+ EFI_PHYSICAL_ADDRESS AddedVariable;\r
+ EFI_PHYSICAL_ADDRESS NextVariable;\r
+ EFI_PHYSICAL_ADDRESS NextAddedVariable;\r
+ VARIABLE_STORE_HEADER VariableStoreHeader;\r
+ VARIABLE_HEADER VariableHeader;\r
+ VARIABLE_HEADER AddedVariableHeader;\r
+ CHAR16 VariableName[MAX_NAME_SIZE];\r
+ CHAR16 AddedVariableName[MAX_NAME_SIZE];\r
+ UINT8 *ValidBuffer;\r
+ UINTN MaximumBufferSize;\r
+ UINTN VariableSize;\r
+ UINTN NameSize;\r
+ UINT8 *CurrPtr;\r
+ BOOLEAN FoundAdded;\r
+ EFI_STATUS Status;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ GetVarStoreHeader (VariableBase, IsVolatile, VariableGlobal, Instance, &VariableStoreHeader);\r
+ //\r
+ // recaluate the total size of Common/HwErr type variables in non-volatile area.\r
+ //\r
+ if (!IsVolatile) {\r
+ Global->CommonVariableTotalSize = 0;\r
+ Global->HwErrVariableTotalSize = 0;\r
+ }\r
+\r
+ //\r
+ // Calculate the size of buffer needed to gather all valid variables\r
+ //\r
+ Variable = GetStartPointer (VariableBase);\r
+ MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r
+\r
+ while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
+ NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
+ //\r
+ // Collect VAR_ADDED variables, and variables in delete transition status.\r
+ //\r
+ if (VariableHeader.State == VAR_ADDED || \r
+ VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r
+ ) {\r
+ VariableSize = NextVariable - Variable;\r
+ MaximumBufferSize += VariableSize;\r
+ }\r
+\r
+ Variable = NextVariable;\r
+ }\r
+\r
+ //\r
+ // Reserve the 1 Bytes with Oxff to identify the \r
+ // end of the variable buffer. \r
+ // \r
+ MaximumBufferSize += 1;\r
+ ValidBuffer = AllocatePool (MaximumBufferSize);\r
+ if (ValidBuffer == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r
+\r
+ //\r
+ // Copy variable store header\r
+ //\r
+ CopyMem (ValidBuffer, &VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r
+ CurrPtr = (UINT8 *) GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);\r
+\r
+ //\r
+ // Reinstall all ADDED variables\r
+ // \r
+ Variable = GetStartPointer (VariableBase);\r
+ while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
+ NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
+ if (VariableHeader.State == VAR_ADDED) {\r
+ VariableSize = NextVariable - Variable;\r
+ CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
+ CurrPtr += VariableSize;\r
+ if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+ Global->HwErrVariableTotalSize += VariableSize;\r
+ } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+ Global->CommonVariableTotalSize += VariableSize;\r
+ }\r
+ }\r
+ Variable = NextVariable;\r
+ }\r
+ //\r
+ // Reinstall in delete transition variables\r
+ // \r
+ Variable = GetStartPointer (VariableBase);\r
+ while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {\r
+ NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);\r
+ if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r
+\r
+ //\r
+ // Buffer has cached all ADDED variable. \r
+ // Per IN_DELETED variable, we have to guarantee that\r
+ // no ADDED one in previous buffer. \r
+ // \r
+ FoundAdded = FALSE;\r
+ AddedVariable = GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);\r
+ while (IsValidVariableHeader (AddedVariable, IsVolatile, VariableGlobal, Instance, &AddedVariableHeader)) {\r
+ NextAddedVariable = GetNextVariablePtr (AddedVariable, IsVolatile, VariableGlobal, Instance);\r
+ NameSize = NameSizeOfVariable (&AddedVariableHeader);\r
+ if (CompareGuid (&AddedVariableHeader.VendorGuid, &VariableHeader.VendorGuid) &&\r
+ NameSize == NameSizeOfVariable (&VariableHeader)\r
+ ) {\r
+ GetVariableNamePtr (Variable, IsVolatile, VariableGlobal, Instance, VariableName);\r
+ GetVariableNamePtr (AddedVariable, IsVolatile, VariableGlobal, Instance, AddedVariableName);\r
+ if (CompareMem (VariableName, AddedVariableName, NameSize) == 0) {\r
+ //\r
+ // If ADDED variable with the same name and vender GUID has been reinstalled,\r
+ // then discard this IN_DELETED copy.\r
+ //\r
+ FoundAdded = TRUE;\r
+ break;\r
+ }\r
+ }\r
+ AddedVariable = NextAddedVariable;\r
+ }\r
+ //\r
+ // Add IN_DELETE variables that have not been added to buffer\r
+ //\r
+ if (!FoundAdded) {\r
+ VariableSize = NextVariable - Variable;\r
+ CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r
+ if (Variable != UpdatingVariable) {\r
+ //\r
+ // Make this IN_DELETE instance valid if:\r
+ // 1. No valid instance of this variable exists.\r
+ // 2. It is not the variable that is going to be updated.\r
+ //\r
+ ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r
+ }\r
+ CurrPtr += VariableSize;\r
+ if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+ Global->HwErrVariableTotalSize += VariableSize;\r
+ } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+ Global->CommonVariableTotalSize += VariableSize;\r
+ }\r
+ }\r
+ }\r
+ Variable = NextVariable;\r
+ }\r
+\r
+ if (IsVolatile) {\r
+ //\r
+ // If volatile variable store, just copy valid buffer\r
+ //\r
+ SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader.Size, 0xff);\r
+ CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r
+ Status = EFI_SUCCESS;\r
+ } else {\r
+ //\r
+ // If non-volatile variable store, perform FTW here.\r
+ // Write ValidBuffer to destination specified by VariableBase.\r
+ //\r
+ Status = FtwVariableSpace (\r
+ VariableBase,\r
+ ValidBuffer,\r
+ (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r
+ );\r
+ }\r
+ if (!EFI_ERROR (Status)) {\r
+ *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r
+ } else {\r
+ *LastVariableOffset = 0;\r
+ }\r
+\r
+ FreePool (ValidBuffer);\r
+\r
+ return Status;\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[in] SupportedLang Platform supported language codes.\r
+ @param[in] Lang Configured language.\r
+ @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
+\r
+ @return 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[in] SupportedLang Platform supported language codes.\r
+ @param[in] Index the index in supported language codes.\r
+ @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+\r
+ @return 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
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\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
+ Global->Lang[CompareLength] = '\0';\r
+ return CopyMem (Global->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
+ Global->PlatformLang[VirtualMode][CompareLength] = '\0';\r
+ return CopyMem (Global->PlatformLang[VirtualMode], 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] VirtualMode Current calling mode for this function.\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
+VariableGetBestLanguage (\r
+ IN CONST CHAR8 *SupportedLanguages, \r
+ IN BOOLEAN Iso639Language,\r
+ IN BOOLEAN VirtualMode,\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, VirtualMode);\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[VirtualMode];\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
+ 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
+ @param[in] Data Variable data.\r
+ @param[in] DataSize Size of data. 0 means delete.\r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+\r
+**/\r
+VOID\r
+AutoUpdateLangVariable(\r
+ IN CHAR16 *VariableName,\r
+ IN VOID *Data,\r
+ IN UINTN DataSize,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\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
+ CHAR16 **PredefinedVariableName;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+\r
+ //\r
+ // Don't do updates for delete operation\r
+ //\r
+ if (DataSize == 0) {\r
+ return;\r
+ }\r
+\r
+ SetLanguageCodes = FALSE;\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+\r
+ PredefinedVariableName = &Global->VariableName[VirtualMode][0];\r
+ if (StrCmp (VariableName, PredefinedVariableName[VAR_PLATFORM_LANG_CODES]) == 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 (Global->PlatformLangCodes[VirtualMode] != NULL) {\r
+ FreePool (Global->PlatformLangCodes[VirtualMode]);\r
+ }\r
+ Global->PlatformLangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);\r
+ ASSERT (mVariableModuleGlobal->PlatformLangCodes[VirtualMode] != 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 (Global->PlatformLang[VirtualMode] != NULL) {\r
+ FreePool (Global->PlatformLang[VirtualMode]);\r
+ }\r
+ Global->PlatformLang[VirtualMode] = AllocateRuntimePool (DataSize);\r
+ ASSERT (Global->PlatformLang[VirtualMode] != NULL);\r
+\r
+ } else if (StrCmp (VariableName, PredefinedVariableName[VAR_LANG_CODES]) == 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 (Global->LangCodes[VirtualMode] != NULL) {\r
+ FreePool (Global->LangCodes[VirtualMode]);\r
+ }\r
+ Global->LangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);\r
+ ASSERT (Global->LangCodes[VirtualMode] != NULL);\r
+ }\r
+\r
+ if (SetLanguageCodes \r
+ && (Global->PlatformLangCodes[VirtualMode] != NULL)\r
+ && (Global->LangCodes[VirtualMode] != NULL)) {\r
+ //\r
+ // Update Lang if PlatformLang is already set\r
+ // Update PlatformLang if Lang is already set\r
+ //\r
+ Status = FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Update Lang\r
+ //\r
+ VariableName = PredefinedVariableName[VAR_PLATFORM_LANG];\r
+ } else {\r
+ Status = FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
+ if (!EFI_ERROR (Status)) {\r
+ //\r
+ // Update PlatformLang\r
+ //\r
+ VariableName = PredefinedVariableName[VAR_LANG];\r
+ } else {\r
+ //\r
+ // Neither PlatformLang nor Lang is set, directly return\r
+ //\r
+ return;\r
+ }\r
+ }\r
+ Data = (VOID *) GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
+ GetVariableDataPtr ((EFI_PHYSICAL_ADDRESS) Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, (CHAR16 *) Data);\r
+\r
+ Status = AccessVariableStore (\r
+ FALSE,\r
+ VariableGlobal,\r
+ Variable.Volatile,\r
+ Instance,\r
+ (UINTN) &(((VARIABLE_HEADER *)Variable.CurrPtr)->DataSize),\r
+ sizeof (DataSize),\r
+ &DataSize\r
+ ); \r
+ ASSERT_EFI_ERROR (Status);\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, PredefinedVariableName[VAR_PLATFORM_LANG]) == 0) {\r
+ //\r
+ // Update Lang when PlatformLangCodes/LangCodes were set.\r
+ //\r
+ if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {\r
+ //\r
+ // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
+ //\r
+ BestPlatformLang = VariableGetBestLanguage (Global->PlatformLangCodes[VirtualMode], FALSE, VirtualMode, Data, NULL);\r
+ if (BestPlatformLang != NULL) {\r
+ //\r
+ // Get the corresponding index in language codes.\r
+ //\r
+ Index = GetIndexFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], BestPlatformLang, FALSE);\r
+\r
+ //\r
+ // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
+ //\r
+ BestLang = GetLangFromSupportedLangCodes (Global->LangCodes[VirtualMode], Index, TRUE, VirtualMode, Global);\r
+\r
+ //\r
+ // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
+ //\r
+ FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
+\r
+ Status = UpdateVariable (\r
+ PredefinedVariableName[VAR_LANG],\r
+ Global->GlobalVariableGuid[VirtualMode],\r
+ BestLang,\r
+ ISO_639_2_ENTRY_SIZE + 1,\r
+ Attributes,\r
+ 0,\r
+ 0,\r
+ VirtualMode,\r
+ Global,\r
+ &Variable\r
+ );\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, PredefinedVariableName[VAR_LANG]) == 0) {\r
+ //\r
+ // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r
+ //\r
+ if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {\r
+ //\r
+ // When setting Lang, firstly get most matched language string from supported language codes.\r
+ //\r
+ BestLang = VariableGetBestLanguage (Global->LangCodes[VirtualMode], TRUE, VirtualMode, Data, NULL);\r
+ if (BestLang != NULL) {\r
+ //\r
+ // Get the corresponding index in language codes.\r
+ //\r
+ Index = GetIndexFromSupportedLangCodes (Global->LangCodes[VirtualMode], BestLang, TRUE);\r
+\r
+ //\r
+ // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
+ //\r
+ BestPlatformLang = GetLangFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], Index, FALSE, VirtualMode, Global);\r
+\r
+ //\r
+ // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
+ //\r
+ FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);\r
+\r
+ Status = UpdateVariable (\r
+ PredefinedVariableName[VAR_PLATFORM_LANG], \r
+ Global->GlobalVariableGuid[VirtualMode], \r
+ BestPlatformLang, \r
+ AsciiStrSize (BestPlatformLang), \r
+ Attributes, \r
+ 0,\r
+ 0,\r
+ VirtualMode, \r
+ Global, \r
+ &Variable\r
+ );\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
+ @param[in] VendorGuid Guid of variable.\r
+ @param[in] Data Variable data.\r
+ @param[in] DataSize Size of data. 0 means delete.\r
+ @param[in] Attributes Attributes of the variable.\r
+ @param[in] KeyIndex Index of associated public key.\r
+ @param[in] MonotonicCount Value of associated monotonic count. \r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\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
+ @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 UINT32 KeyIndex OPTIONAL,\r
+ IN UINT64 MonotonicCount OPTIONAL,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global,\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
+ BOOLEAN Volatile;\r
+ UINT8 State;\r
+ VARIABLE_HEADER VariableHeader;\r
+ VARIABLE_HEADER *NextVariableHeader;\r
+ BOOLEAN Valid;\r
+ BOOLEAN Reclaimed;\r
+ VARIABLE_STORE_HEADER VariableStoreHeader;\r
+ UINTN ScratchSize;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ Reclaimed = FALSE;\r
+\r
+ if (Variable->CurrPtr != 0) {\r
+\r
+ Valid = IsValidVariableHeader (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, &VariableHeader);\r
+ if (!Valid) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Update/Delete existing variable\r
+ //\r
+ Volatile = Variable->Volatile;\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->Volatile) {\r
+ Status = EFI_WRITE_PROTECTED;\r
+ goto Done;\r
+ }\r
+ //\r
+ // Only variable have NV attribute can be updated/deleted in Runtime\r
+ //\r
+ if ((VariableHeader.Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done; \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
+ State = VariableHeader.State;\r
+ State &= VAR_DELETED;\r
+\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ Variable->Volatile,\r
+ Instance,\r
+ (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+ sizeof (UINT8),\r
+ &State\r
+ ); \r
+ if (!EFI_ERROR (Status)) {\r
+ UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r
+ UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
+ }\r
+ goto Done; \r
+ }\r
+ //\r
+ // Logic comes here to update variable.\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 (DataSizeOfVariable (&VariableHeader) == DataSize) {\r
+ NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
+ GetVariableDataPtr (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, (CHAR16 *) NextVariable);\r
+ if (CompareMem (Data, (VOID *) NextVariable, DataSize) == 0) {\r
+ UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
+ Status = EFI_SUCCESS;\r
+ goto Done;\r
+ }\r
+ }\r
+ if ((VariableHeader.State == VAR_ADDED) ||\r
+ (VariableHeader.State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
+ //\r
+ // If new data is different from the old one, mark the old one as VAR_IN_DELETED_TRANSITION.\r
+ // It will be deleted if new variable is successfully written.\r
+ //\r
+ State = VariableHeader.State;\r
+ State &= VAR_IN_DELETED_TRANSITION;\r
+\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ Variable->Volatile,\r
+ Instance,\r
+ (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+ sizeof (UINT8),\r
+ &State\r
+ ); \r
+ if (EFI_ERROR (Status)) {\r
+ goto Done; \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
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ \r
+ //\r
+ // Only variable have NV|RT attribute can be created in Runtime\r
+ //\r
+ if (EfiAtRuntime () &&\r
+ (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done;\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
+ //\r
+ // Tricky part: Use scratch data area at the end of volatile variable store\r
+ // as a temporary storage.\r
+ //\r
+ NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);\r
+ ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
+ NextVariableHeader = (VARIABLE_HEADER *) NextVariable;\r
+\r
+ SetMem (NextVariableHeader, ScratchSize, 0xff);\r
+\r
+ NextVariableHeader->StartId = VARIABLE_DATA;\r
+ NextVariableHeader->Attributes = Attributes;\r
+ NextVariableHeader->PubKeyIndex = KeyIndex;\r
+ NextVariableHeader->MonotonicCount = MonotonicCount;\r
+ NextVariableHeader->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 (&NextVariableHeader->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
+ NextVariableHeader->NameSize = (UINT32)VarNameSize;\r
+ NextVariableHeader->DataSize = (UINT32)DataSize;\r
+\r
+ //\r
+ // The actual size of the variable that stores in storage should\r
+ // include pad size.\r
+ //\r
+ VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
+ if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
+ //\r
+ // Create a nonvolatile variable\r
+ //\r
+ Volatile = FALSE;\r
+ \r
+ GetVarStoreHeader (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance, &VariableStoreHeader);\r
+ if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
+ && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))\r
+ || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
+ && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {\r
+ if (EfiAtRuntime ()) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ //\r
+ // Perform garbage collection & reclaim operation\r
+ //\r
+ Status = Reclaim (VariableGlobal->NonVolatileVariableBase, &(Global->NonVolatileLastVariableOffset), FALSE, VirtualMode, Global, Variable->CurrPtr);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Reclaimed = TRUE;\r
+ //\r
+ // If still no enough space, return out of resources\r
+ //\r
+ if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
+ && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))\r
+ || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
+ && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ }\r
+ //\r
+ // Four steps\r
+ // 1. Write variable header\r
+ // 2. Set variable state to header valid \r
+ // 3. Write variable data\r
+ // 4. Set variable state to valid\r
+ //\r
+ //\r
+ // Step 1:\r
+ //\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ FALSE,\r
+ Instance,\r
+ VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
+ sizeof (VARIABLE_HEADER),\r
+ (UINT8 *) NextVariable\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Step 2:\r
+ //\r
+ NextVariableHeader->State = VAR_HEADER_VALID_ONLY;\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ FALSE,\r
+ Instance,\r
+ VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
+ sizeof (VARIABLE_HEADER),\r
+ (UINT8 *) NextVariable\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ //\r
+ // Step 3:\r
+ //\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ FALSE,\r
+ Instance,\r
+ VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r
+ (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
+ (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ //\r
+ // Step 4:\r
+ //\r
+ NextVariableHeader->State = VAR_ADDED;\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ FALSE,\r
+ Instance,\r
+ VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,\r
+ sizeof (VARIABLE_HEADER),\r
+ (UINT8 *) NextVariable\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Global->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
+\r
+ if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
+ Global->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r
+ } else {\r
+ Global->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r
+ }\r
+ } else {\r
+ //\r
+ // Create a volatile variable\r
+ // \r
+ Volatile = TRUE;\r
+\r
+ if ((UINT32) (HEADER_ALIGN(VarSize) + Global->VolatileLastVariableOffset) >\r
+ ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size) {\r
+ //\r
+ // Perform garbage collection & reclaim operation\r
+ //\r
+ Status = Reclaim (VariableGlobal->VolatileVariableBase, &Global->VolatileLastVariableOffset, TRUE, VirtualMode, Global, Variable->CurrPtr);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ //\r
+ // If still no enough space, return out of resources\r
+ //\r
+ if ((UINT32) (HEADER_ALIGN (VarSize) + Global->VolatileLastVariableOffset) >\r
+ ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size\r
+ ) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ Reclaimed = TRUE;\r
+ }\r
+\r
+ NextVariableHeader->State = VAR_ADDED;\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ TRUE,\r
+ Instance,\r
+ VariableGlobal->VolatileVariableBase + Global->VolatileLastVariableOffset,\r
+ (UINT32) VarSize,\r
+ (UINT8 *) NextVariable\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Global->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r
+ }\r
+ //\r
+ // Mark the old variable as deleted\r
+ // If storage has just been reclaimed, the old variable marked as VAR_IN_DELETED_TRANSITION\r
+ // has already been eliminated, so no need to delete it.\r
+ //\r
+ if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != 0) {\r
+ State = ((VARIABLE_HEADER *)Variable->CurrPtr)->State;\r
+ State &= VAR_DELETED;\r
+\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ VariableGlobal,\r
+ Variable->Volatile,\r
+ Instance,\r
+ (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),\r
+ sizeof (UINT8),\r
+ &State\r
+ );\r
+ }\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r
+ UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
+ }\r
+\r
+Done:\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Implements EsalGetVariable function of Extended SAL Variable Services Class.\r
+\r
+ This function implements EsalGetVariable function of Extended SAL Variable Services Class.\r
+ It is equivalent in functionality to the EFI Runtime Service GetVariable().\r
+ \r
+ @param[in] VariableName A Null-terminated Unicode string that is the name of\r
+ the vendor's variable.\r
+ @param[in] VendorGuid A unique identifier for the vendor.\r
+ @param[out] Attributes If not NULL, a pointer to the memory location to return the \r
+ attributes bitmask for the variable.\r
+ @param[in, out] DataSize Size of Data found. If size is less than the\r
+ data, this value contains the required size.\r
+ @param[out] Data On input, the size in bytes of the return Data buffer. \r
+ On output, the size of data returned in Data.\r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+\r
+ @retval EFI_SUCCESS The function completed successfully. \r
+ @retval EFI_NOT_FOUND The variable was not found.\r
+ @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has \r
+ been updated with the size needed to complete the request.\r
+ @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
+ @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
+ @retval EFI_INVALID_PARAMETER DataSize is NULL.\r
+ @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.\r
+ @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
+ @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EsalGetVariable (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ OUT UINT32 *Attributes OPTIONAL,\r
+ IN OUT UINTN *DataSize,\r
+ OUT VOID *Data,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\r
+ )\r
+{\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ UINTN VarDataSize;\r
+ EFI_STATUS Status;\r
+ VARIABLE_HEADER VariableHeader;\r
+ BOOLEAN Valid;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+\r
+ if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
+\r
+ //\r
+ // Check if this variable exists in cache.\r
+ //\r
+ Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r
+ if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r
+ //\r
+ // If variable exists in cache, just update statistical information for it and finish.\r
+ // Here UpdateVariableInfo() has already retrieved data & attributes for output.\r
+ //\r
+ UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r
+ goto Done;\r
+ }\r
+ //\r
+ // If variable does not exist in cache, search for it in variable storage area.\r
+ //\r
+ Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
+ if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {\r
+ //\r
+ // If it cannot be found in variable storage area, goto Done.\r
+ //\r
+ goto Done;\r
+ }\r
+\r
+ Valid = IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader);\r
+ if (!Valid) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ //\r
+ // If variable exists, but not in cache, get its data and attributes, update\r
+ // statistical information, and update cache.\r
+ //\r
+ VarDataSize = DataSizeOfVariable (&VariableHeader);\r
+ ASSERT (VarDataSize != 0);\r
+\r
+ if (*DataSize >= VarDataSize) {\r
+ if (Data == NULL) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done;\r
+ }\r
+\r
+ GetVariableDataPtr (\r
+ Variable.CurrPtr,\r
+ Variable.Volatile,\r
+ VariableGlobal,\r
+ Instance,\r
+ Data\r
+ );\r
+ if (Attributes != NULL) {\r
+ *Attributes = VariableHeader.Attributes;\r
+ }\r
+\r
+ *DataSize = VarDataSize;\r
+ UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
+ UpdateVariableCache (VariableName, VendorGuid, VariableHeader.Attributes, VarDataSize, Data);\r
+ \r
+ Status = EFI_SUCCESS;\r
+ goto Done;\r
+ } else {\r
+ //\r
+ // If DataSize is too small for the result, return EFI_BUFFER_TOO_SMALL.\r
+ //\r
+ *DataSize = VarDataSize;\r
+ Status = EFI_BUFFER_TOO_SMALL;\r
+ goto Done;\r
+ }\r
+\r
+Done:\r
+ ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
+\r
+ This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.\r
+ It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().\r
+ \r
+ @param[in, out] VariableNameSize Size of the variable\r
+ @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().\r
+ On output, returns the Null-terminated Unicode string of the current variable.\r
+ @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
+ On output, returns the VendorGuid of the current variable. \r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+\r
+ @retval EFI_SUCCESS The function completed successfully. \r
+ @retval EFI_NOT_FOUND The next variable was not found.\r
+ @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result. \r
+ VariableNameSize has been updated with the size needed to complete the request.\r
+ @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.\r
+ @retval EFI_INVALID_PARAMETER VariableName is NULL.\r
+ @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r
+ @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EsalGetNextVariableName (\r
+ IN OUT UINTN *VariableNameSize,\r
+ IN OUT CHAR16 *VariableName,\r
+ IN OUT EFI_GUID *VendorGuid,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\r
+ )\r
+{\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ UINTN VarNameSize;\r
+ EFI_STATUS Status;\r
+ VARIABLE_HEADER VariableHeader;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+\r
+ if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
+\r
+ Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
+ //\r
+ // If the variable does not exist, goto Done and return.\r
+ //\r
+ if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ if (VariableName[0] != 0) {\r
+ //\r
+ // If variable name is not NULL, get next variable\r
+ //\r
+ Variable.CurrPtr = GetNextVariablePtr (\r
+ Variable.CurrPtr,\r
+ Variable.Volatile,\r
+ VariableGlobal,\r
+ Instance\r
+ );\r
+ }\r
+\r
+ while (TRUE) {\r
+ if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == 0x0) {\r
+ //\r
+ // If fail to find a variable in current area, reverse the volatile attribute of area to search.\r
+ //\r
+ Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
+ //\r
+ // Here we depend on the searching sequence of FindVariable().\r
+ // It first searches volatile area, then NV area.\r
+ // So if the volatile attribute after switching is non-volatile, it means that we have finished searching volatile area,\r
+ // and EFI_NOT_FOUND is returnd.\r
+ // Otherwise, it means that we have finished searchig non-volatile area, and we will continue to search volatile area.\r
+ //\r
+ if (!Variable.Volatile) {\r
+ Variable.StartPtr = GetStartPointer (VariableGlobal->NonVolatileVariableBase);\r
+ Variable.EndPtr = GetEndPointer (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance);\r
+ } else {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ Variable.CurrPtr = Variable.StartPtr;\r
+ if (!IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, NULL)) {\r
+ continue;\r
+ }\r
+ }\r
+ //\r
+ // Variable is found\r
+ //\r
+ if (IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader)) {\r
+ if ((VariableHeader.State == VAR_ADDED) &&\r
+ (!(EfiAtRuntime () && ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)))) {\r
+ VarNameSize = NameSizeOfVariable (&VariableHeader);\r
+ ASSERT (VarNameSize != 0);\r
+\r
+ if (VarNameSize <= *VariableNameSize) {\r
+ GetVariableNamePtr (\r
+ Variable.CurrPtr,\r
+ Variable.Volatile,\r
+ VariableGlobal,\r
+ Instance,\r
+ VariableName\r
+ );\r
+ CopyMem (\r
+ VendorGuid,\r
+ &VariableHeader.VendorGuid,\r
+ sizeof (EFI_GUID)\r
+ );\r
+ Status = EFI_SUCCESS;\r
+ } else {\r
+ Status = EFI_BUFFER_TOO_SMALL;\r
+ }\r
+\r
+ *VariableNameSize = VarNameSize;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ Variable.CurrPtr = GetNextVariablePtr (\r
+ Variable.CurrPtr,\r
+ Variable.Volatile,\r
+ VariableGlobal,\r
+ Instance\r
+ );\r
+ }\r
+\r
+Done:\r
+ ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Implements EsalSetVariable function of Extended SAL Variable Services Class.\r
+\r
+ This function implements EsalSetVariable function of Extended SAL Variable Services Class.\r
+ It is equivalent in functionality to the EFI Runtime Service SetVariable().\r
+ \r
+ @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's\r
+ variable. Each VariableName is unique for each \r
+ VendorGuid. VariableName must contain 1 or more \r
+ Unicode characters. If VariableName is an empty Unicode \r
+ string, then EFI_INVALID_PARAMETER is returned.\r
+ @param[in] VendorGuid A unique identifier for the vendor.\r
+ @param[in] Attributes Attributes bitmask to set for the variable.\r
+ @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the\r
+ variable to be deleted.\r
+ @param[in] Data The contents for the variable.\r
+ @param[in] VirtualMode Current calling mode for this function.\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call.\r
+\r
+ @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as \r
+ defined by the Attributes.\r
+ @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the \r
+ DataSize exceeds the maximum allowed.\r
+ @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.\r
+ @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
+ @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
+ @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
+ @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
+ @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.\r
+ @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EsalSetVariable (\r
+ IN CHAR16 *VariableName,\r
+ IN EFI_GUID *VendorGuid,\r
+ IN UINT32 Attributes,\r
+ IN UINTN DataSize,\r
+ IN VOID *Data,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\r
+ )\r
+{\r
+ VARIABLE_POINTER_TRACK Variable;\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS NextVariable;\r
+ EFI_PHYSICAL_ADDRESS Point;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\r
+ UINT32 KeyIndex;\r
+ UINT64 MonotonicCount;\r
+ UINTN PayloadSize;\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
+ // EFI_VARIABLE_RUNTIME_ACCESS bit cannot be set without EFI_VARIABLE_BOOTSERVICE_ACCESS bit.\r
+ //\r
+ if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r
+ if (DataSize < AUTHINFO_SIZE) {\r
+ //\r
+ // Try to write Authencated Variable without AuthInfo\r
+ //\r
+ return EFI_SECURITY_VIOLATION;\r
+ } \r
+ PayloadSize = DataSize - AUTHINFO_SIZE; \r
+ } else {\r
+ PayloadSize = DataSize; \r
+ }\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+ //\r
+ // For variable for hardware error record, the size of the VariableName, including the Unicode Null\r
+ // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.\r
+ //\r
+ if ((PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
+ (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > 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, \\r
+ Global->VariableName[VirtualMode][VAR_HW_ERR_REC], \\r
+ StrLen(Global->VariableName[VirtualMode][VAR_HW_ERR_REC])) != 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ } else {\r
+ //\r
+ // For variable not for hardware error record, the size of the VariableName, including the\r
+ // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.\r
+ //\r
+ if ((PayloadSize > PcdGet32(PcdMaxVariableSize)) ||\r
+ (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxVariableSize))) {\r
+ return EFI_INVALID_PARAMETER;\r
+ } \r
+ } \r
+\r
+ AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
+\r
+ //\r
+ // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;\r
+ //\r
+ if (InterlockedIncrement (&Global->ReentrantState) > 1) {\r
+ Point = VariableGlobal->NonVolatileVariableBase;;\r
+ //\r
+ // Parse non-volatile variable data and get last variable offset\r
+ //\r
+ NextVariable = GetStartPointer (Point);\r
+ while (IsValidVariableHeader (NextVariable, FALSE, VariableGlobal, Instance, NULL)) {\r
+ NextVariable = GetNextVariablePtr (NextVariable, FALSE, VariableGlobal, Instance);\r
+ }\r
+ Global->NonVolatileLastVariableOffset = NextVariable - Point;\r
+ }\r
+\r
+ //\r
+ // Check whether the input variable exists\r
+ //\r
+\r
+ Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);\r
+\r
+ //\r
+ // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
+ //\r
+ AutoUpdateLangVariable (VariableName, Data, PayloadSize, VirtualMode, Global);\r
+\r
+ //\r
+ // Process PK, KEK, Sigdb seperately\r
+ //\r
+ if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_PLATFORM_KEY]) == 0)) {\r
+ Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, TRUE);\r
+ } else if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_KEY_EXCHANGE_KEY]) == 0)) {\r
+ Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, FALSE);\r
+ } else if (CompareGuid (VendorGuid, Global->ImageSecurityDatabaseGuid[VirtualMode])) {\r
+ Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes);\r
+ } else {\r
+ Status = VerifyVariable (Data, DataSize, VirtualMode, Global, &Variable, Attributes, &KeyIndex, &MonotonicCount);\r
+ if (!EFI_ERROR(Status)) {\r
+ //\r
+ // Verification pass\r
+ //\r
+ if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+ //\r
+ // Cut the certificate size before set\r
+ //\r
+ Status = UpdateVariable (\r
+ VariableName, \r
+ VendorGuid, \r
+ (UINT8*)Data + AUTHINFO_SIZE, \r
+ DataSize - AUTHINFO_SIZE, \r
+ Attributes, \r
+ KeyIndex, \r
+ MonotonicCount, \r
+ VirtualMode, \r
+ Global, \r
+ &Variable\r
+ );\r
+ } else {\r
+ //\r
+ // Update variable as usual \r
+ //\r
+ Status = UpdateVariable (\r
+ VariableName, \r
+ VendorGuid, \r
+ Data, \r
+ DataSize, \r
+ Attributes, \r
+ 0, \r
+ 0, \r
+ VirtualMode, \r
+ Global, \r
+ &Variable\r
+ );\r
+ }\r
+ }\r
+ }\r
+\r
+ InterlockedDecrement (&Global->ReentrantState);\r
+ ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
+\r
+ This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.\r
+ It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().\r
+\r
+ @param[in] Attributes Attributes bitmask to specify the type of variables\r
+ on which to return information.\r
+ @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for \r
+ the EFI variables associated with the attributes specified. \r
+ @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
+ variables associated with the attributes specified.\r
+ @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable \r
+ associated with the attributes specified.\r
+ @param[in] VirtualMode Current calling mode for this function\r
+ @param[in] Global Context of this Extended SAL Variable Services Class call\r
+\r
+ @retval EFI_SUCCESS Valid answer returned.\r
+ @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r
+ @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the \r
+ MaximumVariableStorageSize, RemainingVariableStorageSize, \r
+ MaximumVariableSize are undefined.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EsalQueryVariableInfo (\r
+ IN UINT32 Attributes,\r
+ OUT UINT64 *MaximumVariableStorageSize,\r
+ OUT UINT64 *RemainingVariableStorageSize,\r
+ OUT UINT64 *MaximumVariableSize,\r
+ IN BOOLEAN VirtualMode,\r
+ IN ESAL_VARIABLE_GLOBAL *Global\r
+ )\r
+{\r
+ EFI_PHYSICAL_ADDRESS Variable;\r
+ EFI_PHYSICAL_ADDRESS NextVariable;\r
+ UINT64 VariableSize;\r
+ EFI_PHYSICAL_ADDRESS VariableStoreHeaderAddress;\r
+ BOOLEAN Volatile;\r
+ VARIABLE_STORE_HEADER VarStoreHeader;\r
+ VARIABLE_HEADER VariableHeader;\r
+ UINT64 CommonVariableTotalSize;\r
+ UINT64 HwErrVariableTotalSize;\r
+ VARIABLE_GLOBAL *VariableGlobal;\r
+ UINT32 Instance;\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) == 0)) {\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
+ }\r
+\r
+ VariableGlobal = &Global->VariableGlobal[VirtualMode];\r
+ Instance = Global->FvbInstance;\r
+\r
+ AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);\r
+\r
+ if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
+ //\r
+ // Query is Volatile related.\r
+ //\r
+ Volatile = TRUE;\r
+ VariableStoreHeaderAddress = VariableGlobal->VolatileVariableBase;\r
+ } else {\r
+ //\r
+ // Query is Non-Volatile related.\r
+ //\r
+ Volatile = FALSE;\r
+ VariableStoreHeaderAddress = VariableGlobal->NonVolatileVariableBase;\r
+ }\r
+\r
+ //\r
+ // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
+ // with the storage size (excluding the storage header size).\r
+ //\r
+ GetVarStoreHeader (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance, &VarStoreHeader);\r
+\r
+ *MaximumVariableStorageSize = VarStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER);\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) < VarStoreHeader.Size);\r
+ *MaximumVariableStorageSize = VarStoreHeader.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 = GetStartPointer (VariableStoreHeaderAddress);\r
+\r
+ //\r
+ // Now walk through the related variable store.\r
+ //\r
+ while (IsValidVariableHeader (Variable, Volatile, VariableGlobal, Instance, &VariableHeader) &&\r
+ (Variable < GetEndPointer (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance))) {\r
+ NextVariable = GetNextVariablePtr (Variable, Volatile, VariableGlobal, Instance);\r
+ VariableSize = NextVariable - 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 ((VariableHeader.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 (VariableHeader.State == VAR_ADDED) {\r
+ if ((VariableHeader.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
+ if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
+ *MaximumVariableSize = 0;\r
+ } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
+ *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
+ }\r
+\r
+ ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
+\r
+ This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
+ When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
+ storage if free size is below the threshold.\r
+\r
+ @param[in] Event Event whose notification function is being invoked.\r
+ @param[in] Context Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ReclaimForOS(\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ UINT32 VarSize;\r
+ EFI_STATUS Status;\r
+ UINTN CommonVariableSpace;\r
+ UINTN RemainingCommonVariableSpace;\r
+ UINTN RemainingHwErrVariableSpace;\r
+\r
+ VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase))->Size;\r
+ Status = EFI_SUCCESS; \r
+ //\r
+ //Allowable max size of common variable storage space\r
+ //\r
+ CommonVariableSpace = VarSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r
+\r
+ RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r
+ \r
+ RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r
+ //\r
+ // If the free area is below a threshold, then performs reclaim operation.\r
+ //\r
+ if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r
+ || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r
+ (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r
+ Status = Reclaim (\r
+ mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
+ &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
+ FALSE,\r
+ Physical,\r
+ mVariableModuleGlobal,\r
+ 0x0\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
+}\r
+\r
+/**\r
+ Initializes variable store area for non-volatile and volatile variable.\r
+\r
+ This function allocates and initializes memory space for global context of ESAL\r
+ variable service and variable store area for non-volatile and volatile variable.\r
+\r
+ @param[in] ImageHandle The Image handle of this driver.\r
+ @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.\r
+\r
+ @retval EFI_SUCCESS Function successfully executed.\r
+ @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
+\r
+**/\r
+EFI_STATUS\r
+VariableCommonInitialize (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
+ EFI_PHYSICAL_ADDRESS CurrPtr;\r
+ VARIABLE_STORE_HEADER *VolatileVariableStore;\r
+ VARIABLE_STORE_HEADER *VariableStoreHeader;\r
+ EFI_PHYSICAL_ADDRESS Variable;\r
+ EFI_PHYSICAL_ADDRESS NextVariable;\r
+ UINTN VariableSize;\r
+ UINT32 Instance;\r
+ EFI_PHYSICAL_ADDRESS FvVolHdr;\r
+ EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
+ UINT64 BaseAddress;\r
+ UINT64 Length;\r
+ UINTN Index;\r
+ UINT8 Data;\r
+ EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
+ UINT64 VariableStoreLength;\r
+ EFI_EVENT ReadyToBootEvent;\r
+ UINTN ScratchSize;\r
+\r
+ //\r
+ // Allocate memory for mVariableModuleGlobal\r
+ //\r
+ mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (ESAL_VARIABLE_GLOBAL));\r
+ if (mVariableModuleGlobal == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ mVariableModuleGlobal->GlobalVariableGuid[Physical] = &gEfiGlobalVariableGuid;\r
+ CopyMem (\r
+ mVariableModuleGlobal->VariableName[Physical],\r
+ mVariableName,\r
+ sizeof (mVariableName)\r
+ );\r
+\r
+ EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);\r
+\r
+ //\r
+ // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
+ // is stored with common variable in the same NV region. So the platform integrator should\r
+ // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
+ // PcdFlashNvStorageVariableSize.\r
+ //\r
+ ASSERT (PcdGet32(PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r
+\r
+ //\r
+ // Allocate memory for volatile variable store\r
+ //\r
+ ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r
+ VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r
+ if (VolatileVariableStore == NULL) {\r
+ FreePool (mVariableModuleGlobal);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r
+\r
+ //\r
+ // Variable Specific Data\r
+ //\r
+ mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r
+ mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer ((EFI_PHYSICAL_ADDRESS) VolatileVariableStore) - (UINTN) VolatileVariableStore;\r
+\r
+ CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);\r
+ VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r
+ VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r
+ VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r
+ VolatileVariableStore->Reserved = 0;\r
+ VolatileVariableStore->Reserved1 = 0;\r
+\r
+ //\r
+ // Get non volatile varaible store\r
+ //\r
+ TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
+ VariableStoreBase = TempVariableStoreHeader + \\r
+ (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
+ VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
+ (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
+ //\r
+ // Mark the variable storage region of the FLASH as RUNTIME\r
+ //\r
+ BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
+ Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
+ Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
+\r
+ Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Status = gDS->SetMemorySpaceAttributes (\r
+ BaseAddress,\r
+ Length,\r
+ GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ //\r
+ // Get address of non volatile variable store base.\r
+ //\r
+ mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = VariableStoreBase;\r
+\r
+ //\r
+ // Check Integrity\r
+ //\r
+ //\r
+ // Find the Correct Instance of the FV Block Service.\r
+ //\r
+ Instance = 0;\r
+ CurrPtr = mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;\r
+\r
+ do {\r
+ FvVolHdr = 0;\r
+ Status = (EFI_STATUS) EsalCall (\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,\r
+ EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,\r
+ GetPhysicalAddressFunctionId, \r
+ Instance, \r
+ (UINT64) &FvVolHdr, \r
+ 0, \r
+ 0, \r
+ 0, \r
+ 0, \r
+ 0\r
+ ).Status;\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r
+ ASSERT (FwVolHeader != NULL);\r
+ if (CurrPtr >= (EFI_PHYSICAL_ADDRESS) FwVolHeader &&\r
+ CurrPtr < ((EFI_PHYSICAL_ADDRESS) FwVolHeader + FwVolHeader->FvLength)) {\r
+ mVariableModuleGlobal->FvbInstance = Instance;\r
+ break;\r
+ }\r
+\r
+ Instance++;\r
+ } while (Status == EFI_SUCCESS);\r
+\r
+ VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r
+ if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r
+ if (~VariableStoreHeader->Size == 0) {\r
+ Status = AccessVariableStore (\r
+ TRUE,\r
+ &mVariableModuleGlobal->VariableGlobal[Physical],\r
+ FALSE,\r
+ mVariableModuleGlobal->FvbInstance,\r
+ (UINTN) &VariableStoreHeader->Size,\r
+ sizeof (UINT32),\r
+ (UINT8 *) &VariableStoreLength\r
+ );\r
+ //\r
+ // As Variables are stored in NV storage, which are slow devices,such as flash.\r
+ // Variable operation may skip checking variable program result to improve performance,\r
+ // We can assume Variable program is OK through some check point.\r
+ // Variable Store Size Setting should be the first Variable write operation,\r
+ // We can assume all Read/Write is OK if we can set Variable store size successfully.\r
+ // If write fail, we will assert here.\r
+ //\r
+ ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r
+ //\r
+ // Parse non-volatile variable data and get last variable offset.\r
+ //\r
+ Variable = GetStartPointer (CurrPtr);\r
+ Status = EFI_SUCCESS;\r
+\r
+ while (IsValidVariableHeader (Variable, FALSE, &(mVariableModuleGlobal->VariableGlobal[Physical]), Instance, NULL)) {\r
+ NextVariable = GetNextVariablePtr (\r
+ Variable,\r
+ FALSE,\r
+ &(mVariableModuleGlobal->VariableGlobal[Physical]),\r
+ Instance\r
+ );\r
+ VariableSize = NextVariable - Variable;\r
+ if ((((VARIABLE_HEADER *)Variable)->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
+ mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r
+ } else {\r
+ mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r
+ }\r
+\r
+ Variable = NextVariable;\r
+ }\r
+\r
+ mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) CurrPtr;\r
+\r
+ //\r
+ // Check if the free area is really free.\r
+ //\r
+ for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r
+ Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase)[Index];\r
+ if (Data != 0xff) {\r
+ //\r
+ // There must be something wrong in variable store, do reclaim operation.\r
+ //\r
+ Status = Reclaim (\r
+ mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
+ &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
+ FALSE,\r
+ Physical,\r
+ mVariableModuleGlobal,\r
+ 0x0\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ break;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Register the event handling function to reclaim variable for OS usage.\r
+ //\r
+ Status = EfiCreateEventReadyToBootEx (\r
+ TPL_NOTIFY, \r
+ ReclaimForOS, \r
+ NULL, \r
+ &ReadyToBootEvent\r
+ );\r
+ } else {\r
+ Status = EFI_VOLUME_CORRUPTED;\r
+ DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r
+ }\r
+\r
+Done:\r
+ if (EFI_ERROR (Status)) {\r
+ FreePool (mVariableModuleGlobal);\r
+ FreePool (VolatileVariableStore);\r
+ }\r
+\r
+ return Status;\r
+}\r