X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=SecurityPkg%2FVariableAuthenticated%2FRuntimeDxe%2FVariable.c;h=ce4f6e813ef833b800ac3223b8f6459cd6746bd0;hp=22ded16819077a34b5b1a7927fc751ca9c349a48;hb=5767f22fca7c337cdc113e14b411c1fd0ea7bd53;hpb=ecc722ad418a926af4e383f8977444717786fe20 diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c index 22ded16819..ce4f6e813e 100644 --- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c +++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/Variable.c @@ -1,8 +1,22 @@ /** @file - The common variable operation routines shared by DXE_RINTIME variable + The common variable operation routines shared by DXE_RUNTIME variable module and DXE_SMM variable module. -Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ Caution: This module requires additional review when modified. + This driver will have external input - variable data. They may be input in SMM mode. + This external input must be validated carefully to avoid security issue like + buffer overflow, integer overflow. + + VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API. + They need check input parameter. + + VariableServiceGetVariable() and VariableServiceSetVariable() are external API + to receive datasize and data buffer. The size should be checked carefully. + + VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow, + integer overflow. It should also check attribute to avoid authentication bypass. + +Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -497,20 +511,157 @@ GetEndPointer ( return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size); } +/** + + Check the PubKeyIndex is a valid key or not. + + This function will iterate the NV storage to see if this PubKeyIndex is still referenced + by any valid count-based auth variabe. + + @param[in] PubKeyIndex Index of the public key in public key store. + + @retval TRUE The PubKeyIndex is still in use. + @retval FALSE The PubKeyIndex is not referenced by any count-based auth variabe. + +**/ +BOOLEAN +IsValidPubKeyIndex ( + IN UINT32 PubKeyIndex + ) +{ + VARIABLE_HEADER *Variable; + + if (PubKeyIndex > mPubKeyNumber) { + return FALSE; + } + + Variable = GetStartPointer (mNvVariableCache); + + while (IsValidVariableHeader (Variable)) { + if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) && + Variable->PubKeyIndex == PubKeyIndex) { + return TRUE; + } + Variable = GetNextVariablePtr (Variable); + } + + return FALSE; +} /** - Variable store garbage collection and reclaim operation. + Get the number of valid public key in PubKeyStore. + + @param[in] PubKeyNumber Number of the public key in public key store. - @param VariableBase Base address of variable store. - @param LastVariableOffset Offset of last variable. - @param IsVolatile The variable store is volatile or not; - if it is non-volatile, need FTW. - @param UpdatingVariable Pointer to updating variable. + @return Number of valid public key in PubKeyStore. + +**/ +UINT32 +GetValidPubKeyNumber ( + IN UINT32 PubKeyNumber + ) +{ + UINT32 PubKeyIndex; + UINT32 Counter; - @return EFI_OUT_OF_RESOURCES - @return EFI_SUCCESS - @return Others + Counter = 0; + + for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) { + if (IsValidPubKeyIndex (PubKeyIndex)) { + Counter++; + } + } + + return Counter; +} + +/** + + Filter the useless key in public key store. + + This function will find out all valid public keys in public key database, save them in new allocated + buffer NewPubKeyStore, and give the new PubKeyIndex. The caller is responsible for freeing buffer + NewPubKeyIndex and NewPubKeyStore with FreePool(). + + @param[in] PubKeyStore Point to the public key database. + @param[in] PubKeyNumber Number of the public key in PubKeyStore. + @param[out] NewPubKeyIndex Point to an array of new PubKeyIndex corresponds to NewPubKeyStore. + @param[out] NewPubKeyStore Saved all valid public keys in PubKeyStore. + @param[out] NewPubKeySize Buffer size of the NewPubKeyStore. + + @retval EFI_SUCCESS Trim operation is complete successfully. + @retval EFI_OUT_OF_RESOURCES No enough memory resources, or no useless key in PubKeyStore. + +**/ +EFI_STATUS +PubKeyStoreFilter ( + IN UINT8 *PubKeyStore, + IN UINT32 PubKeyNumber, + OUT UINT32 **NewPubKeyIndex, + OUT UINT8 **NewPubKeyStore, + OUT UINT32 *NewPubKeySize + ) +{ + UINT32 PubKeyIndex; + UINT32 CopiedKey; + UINT32 NewPubKeyNumber; + + NewPubKeyNumber = GetValidPubKeyNumber (PubKeyNumber); + if (NewPubKeyNumber == PubKeyNumber) { + return EFI_OUT_OF_RESOURCES; + } + + if (NewPubKeyNumber != 0) { + *NewPubKeySize = NewPubKeyNumber * EFI_CERT_TYPE_RSA2048_SIZE; + } else { + *NewPubKeySize = sizeof (UINT8); + } + + *NewPubKeyStore = AllocatePool (*NewPubKeySize); + if (*NewPubKeyStore == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + *NewPubKeyIndex = AllocateZeroPool ((PubKeyNumber + 1) * sizeof (UINT32)); + if (*NewPubKeyIndex == NULL) { + FreePool (*NewPubKeyStore); + return EFI_OUT_OF_RESOURCES; + } + + CopiedKey = 0; + for (PubKeyIndex = 1; PubKeyIndex <= PubKeyNumber; PubKeyIndex++) { + if (IsValidPubKeyIndex (PubKeyIndex)) { + CopyMem ( + *NewPubKeyStore + CopiedKey * EFI_CERT_TYPE_RSA2048_SIZE, + PubKeyStore + (PubKeyIndex - 1) * EFI_CERT_TYPE_RSA2048_SIZE, + EFI_CERT_TYPE_RSA2048_SIZE + ); + (*NewPubKeyIndex)[PubKeyIndex] = ++CopiedKey; + } + } + return EFI_SUCCESS; +} + +/** + + Variable store garbage collection and reclaim operation. + + If ReclaimPubKeyStore is FALSE, reclaim variable space by deleting the obsoleted varaibles. + If ReclaimPubKeyStore is TRUE, reclaim invalid key in public key database and update the PubKeyIndex + for all the count-based authenticate variable in NV storage. + + @param[in] VariableBase Base address of variable store. + @param[out] LastVariableOffset Offset of last variable. + @param[in] IsVolatile The variable store is volatile or not; + if it is non-volatile, need FTW. + @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure. + @param[in] ReclaimPubKeyStore Reclaim for public key database or not. + @param[in] ReclaimAnyway If TRUE, do reclaim anyway. + + @return EFI_OUT_OF_RESOURCES No enough memory resources. + @return EFI_SUCCESS Reclaim operation has finished successfully. + @return Others Unexpect error happened during reclaim operation. **/ EFI_STATUS @@ -518,7 +669,9 @@ Reclaim ( IN EFI_PHYSICAL_ADDRESS VariableBase, OUT UINTN *LastVariableOffset, IN BOOLEAN IsVolatile, - IN VARIABLE_HEADER *UpdatingVariable + IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack, + IN BOOLEAN ReclaimPubKeyStore, + IN BOOLEAN ReclaimAnyway ) { VARIABLE_HEADER *Variable; @@ -539,16 +692,30 @@ Reclaim ( EFI_STATUS Status; CHAR16 *VariableNamePtr; CHAR16 *UpdatingVariableNamePtr; + UINTN CommonVariableTotalSize; + UINTN HwErrVariableTotalSize; + UINT32 *NewPubKeyIndex; + UINT8 *NewPubKeyStore; + UINT32 NewPubKeySize; + VARIABLE_HEADER *PubKeyHeader; + BOOLEAN NeedDoReclaim; + VARIABLE_HEADER *UpdatingVariable; - VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase); - // - // Recalculate the total size of Common/HwErr type variables in non-volatile area. - // - if (!IsVolatile) { - mVariableModuleGlobal->CommonVariableTotalSize = 0; - mVariableModuleGlobal->HwErrVariableTotalSize = 0; + UpdatingVariable = NULL; + if (UpdatingPtrTrack != NULL) { + UpdatingVariable = UpdatingPtrTrack->CurrPtr; } + NeedDoReclaim = FALSE; + VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase); + + CommonVariableTotalSize = 0; + HwErrVariableTotalSize = 0; + NewPubKeyIndex = NULL; + NewPubKeyStore = NULL; + NewPubKeySize = 0; + PubKeyHeader = NULL; + // // Start Pointers for the variable. // @@ -562,11 +729,18 @@ Reclaim ( ) { VariableSize = (UINTN) NextVariable - (UINTN) Variable; MaximumBufferSize += VariableSize; + } else { + NeedDoReclaim = TRUE; } Variable = NextVariable; } + if (!ReclaimAnyway && !NeedDoReclaim) { + DEBUG ((EFI_D_INFO, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n")); + return EFI_SUCCESS; + } + // // Reserve the 1 Bytes with Oxff to identify the // end of the variable buffer. @@ -585,105 +759,166 @@ Reclaim ( CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER)); CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer); - // - // Reinstall all ADDED variables as long as they are not identical to Updating Variable. - // - Variable = GetStartPointer (VariableStoreHeader); - while (IsValidVariableHeader (Variable)) { - NextVariable = GetNextVariablePtr (Variable); - if (Variable->State == VAR_ADDED) { - if (UpdatingVariable != NULL) { - if (UpdatingVariable == Variable) { - Variable = NextVariable; - continue; - } - - VariableNameSize = NameSizeOfVariable(Variable); - UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable); + if (ReclaimPubKeyStore) { + // + // Trim the PubKeyStore and get new PubKeyIndex. + // + Status = PubKeyStoreFilter ( + mPubKeyStore, + mPubKeyNumber, + &NewPubKeyIndex, + &NewPubKeyStore, + &NewPubKeySize + ); + if (EFI_ERROR (Status)) { + FreePool (ValidBuffer); + return Status; + } - VariableNamePtr = GetVariableNamePtr (Variable); - UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable); - if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) && - VariableNameSize == UpdatingVariableNameSize && - CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) { + // + // Refresh the PubKeyIndex for all valid variables (ADDED and IN_DELETED_TRANSITION). + // + Variable = GetStartPointer (mNvVariableCache); + while (IsValidVariableHeader (Variable)) { + NextVariable = GetNextVariablePtr (Variable); + if (Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + if ((StrCmp (GetVariableNamePtr (Variable), AUTHVAR_KEYDB_NAME) == 0) && + (CompareGuid (&Variable->VendorGuid, &gEfiAuthenticatedVariableGuid))) { + // + // Skip the public key database, it will be reinstalled later. + // + PubKeyHeader = Variable; Variable = NextVariable; continue; } + + VariableSize = (UINTN) NextVariable - (UINTN) Variable; + CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); + ((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex]; + CurrPtr += VariableSize; + if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + HwErrVariableTotalSize += VariableSize; + } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + CommonVariableTotalSize += VariableSize; + } } - VariableSize = (UINTN) NextVariable - (UINTN) Variable; - CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); - CurrPtr += VariableSize; - if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize; - } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->CommonVariableTotalSize += VariableSize; - } + Variable = NextVariable; } - Variable = NextVariable; - } - // - // Reinstall the variable being updated if it is not NULL. - // - if (UpdatingVariable != NULL) { - VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable; - CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize); - CurrPtr += VariableSize; - if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize; - } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->CommonVariableTotalSize += VariableSize; - } - } - - // - // Reinstall all in delete transition variables. - // - Variable = GetStartPointer (VariableStoreHeader); - while (IsValidVariableHeader (Variable)) { - NextVariable = GetNextVariablePtr (Variable); - if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + // + // Reinstall the new public key database. + // + ASSERT (PubKeyHeader != NULL); + CopyMem (CurrPtr, (UINT8*) PubKeyHeader, sizeof (VARIABLE_HEADER)); + Variable = (VARIABLE_HEADER*) CurrPtr; + Variable->DataSize = NewPubKeySize; + StrCpy (GetVariableNamePtr (Variable), GetVariableNamePtr (PubKeyHeader)); + CopyMem (GetVariableDataPtr (Variable), NewPubKeyStore, NewPubKeySize); + CurrPtr = (UINT8*) GetNextVariablePtr (Variable); + CommonVariableTotalSize += (UINTN) CurrPtr - (UINTN) Variable; + } else { + // + // Reinstall all ADDED variables as long as they are not identical to Updating Variable. + // + Variable = GetStartPointer (VariableStoreHeader); + while (IsValidVariableHeader (Variable)) { + NextVariable = GetNextVariablePtr (Variable); + if (Variable->State == VAR_ADDED) { + if (UpdatingVariable != NULL) { + if (UpdatingVariable == Variable) { + Variable = NextVariable; + continue; + } - // - // Buffer has cached all ADDED variable. - // Per IN_DELETED variable, we have to guarantee that - // no ADDED one in previous buffer. - // + VariableNameSize = NameSizeOfVariable(Variable); + UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable); - FoundAdded = FALSE; - AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer); - while (IsValidVariableHeader (AddedVariable)) { - NextAddedVariable = GetNextVariablePtr (AddedVariable); - NameSize = NameSizeOfVariable (AddedVariable); - if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) && - NameSize == NameSizeOfVariable (Variable) - ) { - Point0 = (VOID *) GetVariableNamePtr (AddedVariable); - Point1 = (VOID *) GetVariableNamePtr (Variable); - if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) { - FoundAdded = TRUE; - break; + VariableNamePtr = GetVariableNamePtr (Variable); + UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable); + if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) && + VariableNameSize == UpdatingVariableNameSize && + CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) { + Variable = NextVariable; + continue; } } - AddedVariable = NextAddedVariable; - } - if (!FoundAdded) { - // - // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED. - // VariableSize = (UINTN) NextVariable - (UINTN) Variable; CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); - ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED; CurrPtr += VariableSize; if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize; + HwErrVariableTotalSize += VariableSize; } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { - mVariableModuleGlobal->CommonVariableTotalSize += VariableSize; + CommonVariableTotalSize += VariableSize; } } + Variable = NextVariable; } - Variable = NextVariable; + // + // Reinstall the variable being updated if it is not NULL. + // + if (UpdatingVariable != NULL) { + VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable; + CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize); + UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))); + UpdatingPtrTrack->InDeletedTransitionPtr = NULL; + CurrPtr += VariableSize; + if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + HwErrVariableTotalSize += VariableSize; + } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + CommonVariableTotalSize += VariableSize; + } + } + + // + // Reinstall all in delete transition variables. + // + Variable = GetStartPointer (VariableStoreHeader); + while (IsValidVariableHeader (Variable)) { + NextVariable = GetNextVariablePtr (Variable); + if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + + // + // Buffer has cached all ADDED variable. + // Per IN_DELETED variable, we have to guarantee that + // no ADDED one in previous buffer. + // + + FoundAdded = FALSE; + AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer); + while (IsValidVariableHeader (AddedVariable)) { + NextAddedVariable = GetNextVariablePtr (AddedVariable); + NameSize = NameSizeOfVariable (AddedVariable); + if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) && + NameSize == NameSizeOfVariable (Variable) + ) { + Point0 = (VOID *) GetVariableNamePtr (AddedVariable); + Point1 = (VOID *) GetVariableNamePtr (Variable); + if (CompareMem (Point0, Point1, NameSize) == 0) { + FoundAdded = TRUE; + break; + } + } + AddedVariable = NextAddedVariable; + } + if (!FoundAdded) { + // + // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED. + // + VariableSize = (UINTN) NextVariable - (UINTN) Variable; + CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); + ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED; + CurrPtr += VariableSize; + if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + HwErrVariableTotalSize += VariableSize; + } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + CommonVariableTotalSize += VariableSize; + } + } + } + + Variable = NextVariable; + } } if (IsVolatile) { @@ -706,10 +941,33 @@ Reclaim ( } if (!EFI_ERROR (Status)) { *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer); + if (!IsVolatile) { + mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize; + mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize; + } } else { - *LastVariableOffset = 0; + NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase); + while (IsValidVariableHeader (NextVariable)) { + VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER); + if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize); + } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { + mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize); + } + + NextVariable = GetNextVariablePtr (NextVariable); + } + *LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase; + } + + if (NewPubKeyStore != NULL) { + FreePool (NewPubKeyStore); } + if (NewPubKeyIndex != NULL) { + FreePool (NewPubKeyIndex); + } + FreePool (ValidBuffer); return Status; @@ -720,7 +978,8 @@ Reclaim ( @param[in] VariableName Name of the variable to be found @param[in] VendorGuid Vendor GUID to be found. - @param[in] IgnoreRtAttribute Ignore RUNTIME_ACCESS attribute when searching variable. + @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute + check at runtime when searching variable. @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information. @retval EFI_SUCCESS Variable found successfully @@ -730,13 +989,15 @@ EFI_STATUS FindVariableEx ( IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, - IN BOOLEAN IgnoreRtAttribute, + IN BOOLEAN IgnoreRtCheck, IN OUT VARIABLE_POINTER_TRACK *PtrTrack ) { VARIABLE_HEADER *InDeletedVariable; VOID *Point; + PtrTrack->InDeletedTransitionPtr = NULL; + // // Find the variable by walk through HOB, volatile and non-volatile variable store. // @@ -749,11 +1010,12 @@ FindVariableEx ( if (PtrTrack->CurrPtr->State == VAR_ADDED || PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED) ) { - if (IgnoreRtAttribute || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) { + if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) { if (VariableName[0] == 0) { if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { InDeletedVariable = PtrTrack->CurrPtr; } else { + PtrTrack->InDeletedTransitionPtr = InDeletedVariable; return EFI_SUCCESS; } } else { @@ -765,6 +1027,7 @@ FindVariableEx ( if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { InDeletedVariable = PtrTrack->CurrPtr; } else { + PtrTrack->InDeletedTransitionPtr = InDeletedVariable; return EFI_SUCCESS; } } @@ -785,9 +1048,9 @@ FindVariableEx ( This code finds variable in storage blocks of volatile and non-volatile storage areas. If VariableName is an empty string, then we just return the first qualified variable without comparing VariableName and VendorGuid. - If IgnoreRtAttribute is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS Attribute - when searching existing variable, only VariableName and VendorGuid are compared. - Otherwise, variables with EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime. + If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check + at runtime when searching existing variable, only VariableName and VendorGuid are compared. + Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime. @param[in] VariableName Name of the variable to be found. @param[in] VendorGuid Vendor GUID to be found. @@ -796,7 +1059,8 @@ FindVariableEx ( @param[in] Global Pointer to VARIABLE_GLOBAL structure, including base of volatile variable storage area, base of NV variable storage area, and a lock. - @param[in] IgnoreRtAttribute Ignore RUNTIME_ACCESS attribute when searching variable. + @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute + check at runtime when searching variable. @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL. @@ -810,7 +1074,7 @@ FindVariable ( IN EFI_GUID *VendorGuid, OUT VARIABLE_POINTER_TRACK *PtrTrack, IN VARIABLE_GLOBAL *Global, - IN BOOLEAN IgnoreRtAttribute + IN BOOLEAN IgnoreRtCheck ) { EFI_STATUS Status; @@ -842,7 +1106,7 @@ FindVariable ( PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]); PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile); - Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtAttribute, PtrTrack); + Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack); if (!EFI_ERROR (Status)) { return Status; } @@ -1355,7 +1619,7 @@ AutoUpdateLangVariable ( @param[in] Attributes Attributes of the variable. @param[in] KeyIndex Index of associated public key. @param[in] MonotonicCount Value of associated monotonic count. - @param[in] CacheVariable The variable information which is used to keep track of variable usage. + @param[in, out] CacheVariable The variable information which is used to keep track of variable usage. @param[in] TimeStamp Value of associated TimeStamp. @retval EFI_SUCCESS The update operation is success. @@ -1371,7 +1635,7 @@ UpdateVariable ( IN UINT32 Attributes OPTIONAL, IN UINT32 KeyIndex OPTIONAL, IN UINT64 MonotonicCount OPTIONAL, - IN VARIABLE_POINTER_TRACK *CacheVariable, + IN OUT VARIABLE_POINTER_TRACK *CacheVariable, IN EFI_TIME *TimeStamp OPTIONAL ) { @@ -1387,7 +1651,6 @@ UpdateVariable ( BOOLEAN Volatile; EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; UINT8 State; - BOOLEAN Reclaimed; VARIABLE_POINTER_TRACK *Variable; VARIABLE_POINTER_TRACK NvVariable; VARIABLE_STORE_HEADER *VariableStoreHeader; @@ -1427,11 +1690,15 @@ UpdateVariable ( Variable->StartPtr = GetStartPointer (VariableStoreHeader); Variable->EndPtr = GetEndPointer (VariableStoreHeader); Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr)); + if (CacheVariable->InDeletedTransitionPtr != NULL) { + Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr)); + } else { + Variable->InDeletedTransitionPtr = NULL; + } Variable->Volatile = FALSE; } Fvb = mVariableModuleGlobal->FvbInstance; - Reclaimed = FALSE; // // Tricky part: Use scratch data area at the end of volatile variable store @@ -1479,6 +1746,32 @@ UpdateVariable ( // not delete the variable. // if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) { + if (Variable->InDeletedTransitionPtr != NULL) { + // + // Both ADDED and IN_DELETED_TRANSITION variable are present, + // set IN_DELETED_TRANSITION one to DELETED state first. + // + State = Variable->InDeletedTransitionPtr->State; + State &= VAR_DELETED; + Status = UpdateVariableStore ( + &mVariableModuleGlobal->VariableGlobal, + Variable->Volatile, + FALSE, + Fvb, + (UINTN) &Variable->InDeletedTransitionPtr->State, + sizeof (UINT8), + &State + ); + if (!EFI_ERROR (Status)) { + if (!Variable->Volatile) { + ASSERT (CacheVariable->InDeletedTransitionPtr != NULL); + CacheVariable->InDeletedTransitionPtr->State = State; + } + } else { + goto Done; + } + } + State = Variable->CurrPtr->State; State &= VAR_DELETED; @@ -1495,6 +1788,7 @@ UpdateVariable ( UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE); if (!Variable->Volatile) { CacheVariable->CurrPtr->State = State; + FlushHobVariableToFlash (VariableName, VendorGuid); } } goto Done; @@ -1526,11 +1820,11 @@ UpdateVariable ( DataOffset = sizeof (VARIABLE_HEADER) + Variable->CurrPtr->NameSize + GET_PAD_SIZE (Variable->CurrPtr->NameSize); CopyMem (mStorageArea, (UINT8*)((UINTN) Variable->CurrPtr + DataOffset), Variable->CurrPtr->DataSize); - if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) || - (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) { + if ((CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && + ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) || + (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0))) { // - // For variables with the GUID EFI_IMAGE_SECURITY_DATABASE_GUID (i.e. where the data - // buffer is formatted as EFI_SIGNATURE_LIST), the driver shall not perform an append of + // For variables with formatted as EFI_SIGNATURE_LIST, the driver shall not perform an append of // EFI_SIGNATURE_DATA values that are already part of the existing variable value. // BufSize = AppendSignatureList (mStorageArea, Variable->CurrPtr->DataSize, Data, DataSize); @@ -1703,8 +1997,14 @@ UpdateVariable ( // // Perform garbage collection & reclaim operation. // - Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, - &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr); + Status = Reclaim ( + mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, + &mVariableModuleGlobal->NonVolatileLastVariableOffset, + FALSE, + Variable, + FALSE, + FALSE + ); if (EFI_ERROR (Status)) { goto Done; } @@ -1718,7 +2018,10 @@ UpdateVariable ( Status = EFI_OUT_OF_RESOURCES; goto Done; } - Reclaimed = TRUE; + if (Variable->CurrPtr != NULL) { + CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr)); + CacheVariable->InDeletedTransitionPtr = NULL; + } } // // Four steps @@ -1818,8 +2121,14 @@ UpdateVariable ( // // Perform garbage collection & reclaim operation. // - Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, - &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr); + Status = Reclaim ( + mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, + &mVariableModuleGlobal->VolatileLastVariableOffset, + TRUE, + Variable, + FALSE, + FALSE + ); if (EFI_ERROR (Status)) { goto Done; } @@ -1832,7 +2141,10 @@ UpdateVariable ( Status = EFI_OUT_OF_RESOURCES; goto Done; } - Reclaimed = TRUE; + if (Variable->CurrPtr != NULL) { + CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr)); + CacheVariable->InDeletedTransitionPtr = NULL; + } } NextVariable->State = VAR_ADDED; @@ -1856,7 +2168,33 @@ UpdateVariable ( // // Mark the old variable as deleted. // - if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) { + if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) { + if (Variable->InDeletedTransitionPtr != NULL) { + // + // Both ADDED and IN_DELETED_TRANSITION old variable are present, + // set IN_DELETED_TRANSITION one to DELETED state first. + // + State = Variable->InDeletedTransitionPtr->State; + State &= VAR_DELETED; + Status = UpdateVariableStore ( + &mVariableModuleGlobal->VariableGlobal, + Variable->Volatile, + FALSE, + Fvb, + (UINTN) &Variable->InDeletedTransitionPtr->State, + sizeof (UINT8), + &State + ); + if (!EFI_ERROR (Status)) { + if (!Variable->Volatile) { + ASSERT (CacheVariable->InDeletedTransitionPtr != NULL); + CacheVariable->InDeletedTransitionPtr->State = State; + } + } else { + goto Done; + } + } + State = Variable->CurrPtr->State; State &= VAR_DELETED; @@ -1876,16 +2214,107 @@ UpdateVariable ( if (!EFI_ERROR (Status)) { UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE); + if (!Volatile) { + FlushHobVariableToFlash (VariableName, VendorGuid); + } } Done: return Status; } +/** + Check if a Unicode character is a hexadecimal character. + + This function checks if a Unicode character is a + hexadecimal character. The valid hexadecimal character is + L'0' to L'9', L'a' to L'f', or L'A' to L'F'. + + + @param Char The character to check against. + + @retval TRUE If the Char is a hexadecmial character. + @retval FALSE If the Char is not a hexadecmial character. + +**/ +BOOLEAN +EFIAPI +IsHexaDecimalDigitCharacter ( + IN CHAR16 Char + ) +{ + return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f')); +} + +/** + + This code checks if variable is hardware error record variable or not. + + According to UEFI spec, hardware error record variable should use the EFI_HARDWARE_ERROR_VARIABLE VendorGuid + and have the L"HwErrRec####" name convention, #### is a printed hex value and no 0x or h is included in the hex value. + + @param VariableName Pointer to variable name. + @param VendorGuid Variable Vendor Guid. + + @retval TRUE Variable is hardware error record variable. + @retval FALSE Variable is not hardware error record variable. + +**/ +BOOLEAN +EFIAPI +IsHwErrRecVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + if (!CompareGuid (VendorGuid, &gEfiHardwareErrorVariableGuid) || + (StrLen (VariableName) != StrLen (L"HwErrRec####")) || + (StrnCmp(VariableName, L"HwErrRec", StrLen (L"HwErrRec")) != 0) || + !IsHexaDecimalDigitCharacter (VariableName[0x8]) || + !IsHexaDecimalDigitCharacter (VariableName[0x9]) || + !IsHexaDecimalDigitCharacter (VariableName[0xA]) || + !IsHexaDecimalDigitCharacter (VariableName[0xB])) { + return FALSE; + } + + return TRUE; +} + +/** + This code checks if variable should be treated as read-only variable. + + @param[in] VariableName Name of the Variable. + @param[in] VendorGuid GUID of the Variable. + + @retval TRUE This variable is read-only variable. + @retval FALSE This variable is NOT read-only variable. + +**/ +BOOLEAN +IsReadOnlyVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) { + if ((StrCmp (VariableName, EFI_SETUP_MODE_NAME) == 0) || + (StrCmp (VariableName, EFI_SIGNATURE_SUPPORT_NAME) == 0) || + (StrCmp (VariableName, EFI_SECURE_BOOT_MODE_NAME) == 0)) { + return TRUE; + } + } + + return FALSE; +} + /** This code finds variable in storage blocks (Volatile or Non-Volatile). + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize is external input. + This function will do basic validation, before parse the data. + @param VariableName Name of Variable to be found. @param VendorGuid Variable vendor GUID. @param Attributes Attribute value of the variable found. @@ -1963,6 +2392,9 @@ Done: This code Finds the Next available variable. + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode. This function will do basic validation, before parse the data. + @param VariableNameSize Size of the variable name. @param VariableName Pointer to variable name. @param VendorGuid Variable Vendor Guid. @@ -1984,6 +2416,7 @@ VariableServiceGetNextVariableName ( VARIABLE_STORE_TYPE Type; VARIABLE_POINTER_TRACK Variable; VARIABLE_POINTER_TRACK VariableInHob; + VARIABLE_POINTER_TRACK VariablePtrTrack; UINTN VarNameSize; EFI_STATUS Status; VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax]; @@ -2057,8 +2490,27 @@ VariableServiceGetNextVariableName ( // // Variable is found // - if (Variable.CurrPtr->State == VAR_ADDED) { - if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) { + if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) { + if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + // + // If it is a IN_DELETED_TRANSITION variable, + // and there is also a same ADDED one at the same time, + // don't return it. + // + VariablePtrTrack.StartPtr = Variable.StartPtr; + VariablePtrTrack.EndPtr = Variable.EndPtr; + Status = FindVariableEx ( + GetVariableNamePtr (Variable.CurrPtr), + &Variable.CurrPtr->VendorGuid, + FALSE, + &VariablePtrTrack + ); + if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) { + Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr); + continue; + } + } // // Don't return NV variable when HOB overrides it @@ -2108,6 +2560,13 @@ Done: This code sets variable in storage blocks (Volatile or Non-Volatile). + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode, and datasize and data are external input. + This function will do basic validation, before parse the data. + This function will parse the authentication carefully to avoid security issues, like + buffer overflow, integer overflow. + This function will check attribute carefully to avoid authentication bypass. + @param VariableName Name of Variable to be found. @param VendorGuid Variable vendor GUID. @param Attributes Attribute value of the variable found @@ -2145,10 +2604,21 @@ VariableServiceSetVariable ( return EFI_INVALID_PARAMETER; } + if (IsReadOnlyVariable (VariableName, VendorGuid)) { + return EFI_WRITE_PROTECTED; + } + if (DataSize != 0 && Data == NULL) { return EFI_INVALID_PARAMETER; } + // + // Check for reserverd bit in variable attribute. + // + if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) { + return EFI_INVALID_PARAMETER; + } + // // Make sure if runtime bit is set, boot service bit is set also. // @@ -2197,10 +2667,7 @@ VariableServiceSetVariable ( (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) { return EFI_INVALID_PARAMETER; } - // - // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX". - // - if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) { + if (!IsHwErrRecVariable(VariableName, VendorGuid)) { return EFI_INVALID_PARAMETER; } } else { @@ -2214,6 +2681,16 @@ VariableServiceSetVariable ( } } + if (AtRuntime ()) { + // + // HwErrRecSupport Global Variable identifies the level of hardware error record persistence + // support implemented by the platform. This variable is only modified by firmware and is read-only to the OS. + // + if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, L"HwErrRecSupport") == 0)) { + return EFI_WRITE_PROTECTED; + } + } + AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock); // @@ -2255,7 +2732,10 @@ VariableServiceSetVariable ( Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE); } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) && ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) || (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0))) { - Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes); + Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes, FALSE); + if (EFI_ERROR (Status)) { + Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes); + } } else { Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, &Variable, Attributes); } @@ -2270,6 +2750,9 @@ VariableServiceSetVariable ( This code returns information about the EFI variables. + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode. This function will do basic validation, before parse the data. + @param Attributes Attributes bitmask to specify the type of variables on which to return information. @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available @@ -2431,6 +2914,9 @@ VariableServiceQueryVariableInfo ( /** This function reclaims variable storage if free size is below the threshold. + Caution: This function may be invoked at SMM mode. + Care must be taken to make sure not security issue. + **/ VOID ReclaimForOS( @@ -2459,12 +2945,103 @@ ReclaimForOS( mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, - NULL + NULL, + FALSE, + FALSE ); ASSERT_EFI_ERROR (Status); } } +/** + Flush the HOB variable to flash. + + @param[in] VariableName Name of variable has been updated or deleted. + @param[in] VendorGuid Guid of variable has been updated or deleted. + +**/ +VOID +FlushHobVariableToFlash ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + EFI_STATUS Status; + VARIABLE_STORE_HEADER *VariableStoreHeader; + VARIABLE_HEADER *Variable; + VOID *VariableData; + BOOLEAN ErrorFlag; + + ErrorFlag = FALSE; + + // + // Flush the HOB variable to flash. + // + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) { + VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase; + // + // Set HobVariableBase to 0, it can avoid SetVariable to call back. + // + mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0; + for ( Variable = GetStartPointer (VariableStoreHeader) + ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable)) + ; Variable = GetNextVariablePtr (Variable) + ) { + if (Variable->State != VAR_ADDED) { + // + // The HOB variable has been set to DELETED state in local. + // + continue; + } + ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0); + if (VendorGuid == NULL || VariableName == NULL || + !CompareGuid (VendorGuid, &Variable->VendorGuid) || + StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) { + VariableData = GetVariableDataPtr (Variable); + Status = VariableServiceSetVariable ( + GetVariableNamePtr (Variable), + &Variable->VendorGuid, + Variable->Attributes, + Variable->DataSize, + VariableData + ); + DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", &Variable->VendorGuid, GetVariableNamePtr (Variable), Status)); + } else { + // + // The updated or deleted variable is matched with the HOB variable. + // Don't break here because we will try to set other HOB variables + // since this variable could be set successfully. + // + Status = EFI_SUCCESS; + } + if (!EFI_ERROR (Status)) { + // + // If set variable successful, or the updated or deleted variable is matched with the HOB variable, + // set the HOB variable to DELETED state in local. + // + DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", &Variable->VendorGuid, GetVariableNamePtr (Variable))); + Variable->State &= VAR_DELETED; + } else { + ErrorFlag = TRUE; + } + } + if (ErrorFlag) { + // + // We still have HOB variable(s) not flushed in flash. + // + mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader; + } else { + // + // All HOB variables have been flushed in flash. + // + DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n")); + if (!AtRuntime ()) { + FreePool ((VOID *) VariableStoreHeader); + } + } + } + +} /** Initializes variable write service after FVB was ready. @@ -2483,8 +3060,6 @@ VariableWriteServiceInitialize ( UINTN Index; UINT8 Data; EFI_PHYSICAL_ADDRESS VariableStoreBase; - VARIABLE_HEADER *Variable; - VOID *VariableData; VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase; VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase; @@ -2502,7 +3077,9 @@ VariableWriteServiceInitialize ( mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, - NULL + NULL, + FALSE, + TRUE ); if (EFI_ERROR (Status)) { return Status; @@ -2511,34 +3088,7 @@ VariableWriteServiceInitialize ( } } - - // - // Flush the HOB variable to flash and invalidate HOB variable. - // - if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) { - // - // Clear the HobVariableBase to avoid SetVariable() updating the variable in HOB - // - VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase; - mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0; - - for ( Variable = GetStartPointer (VariableStoreHeader) - ; (Variable < GetEndPointer (VariableStoreHeader) && IsValidVariableHeader (Variable)) - ; Variable = GetNextVariablePtr (Variable) - ) { - ASSERT (Variable->State == VAR_ADDED); - ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0); - VariableData = GetVariableDataPtr (Variable); - Status = VariableServiceSetVariable ( - GetVariableNamePtr (Variable), - &Variable->VendorGuid, - Variable->Attributes, - Variable->DataSize, - VariableData - ); - ASSERT_EFI_ERROR (Status); - } - } + FlushHobVariableToFlash (NULL, NULL); // // Authenticated variable initialize. @@ -2596,8 +3146,12 @@ VariableCommonInitialize ( GuidHob = GetFirstGuidHob (&gEfiAuthenticatedVariableGuid); if (GuidHob != NULL) { VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob); + VariableStoreLength = (UINT64) (GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE)); if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) { - mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader; + mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader); + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) { + return EFI_OUT_OF_RESOURCES; + } } else { DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n")); }