MdeModulePkg and SecurityPkg Variable: Enhance code to use the new variable data instead of old variable data when variable reclaim.

It can fix the potential failure to return EFI_OUT_OF_RESOURCES of the second variable set of large variable.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14822 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Star Zeng 2013-11-04 03:13:54 +00:00 committed by lzeng14
parent d1226e25d1
commit 7baf3c69c5
4 changed files with 191 additions and 195 deletions

View File

@ -566,7 +566,8 @@ GetEndPointer (
@param IsVolatile The variable store is volatile or not; @param IsVolatile The variable store is volatile or not;
if it is non-volatile, need FTW. if it is non-volatile, need FTW.
@param UpdatingPtrTrack Pointer to updating variable pointer track structure. @param UpdatingPtrTrack Pointer to updating variable pointer track structure.
@param ReclaimAnyway If TRUE, do reclaim anyway. @param NewVariable Pointer to new variable.
@param NewVariableSize New variable size.
@return EFI_OUT_OF_RESOURCES @return EFI_OUT_OF_RESOURCES
@return EFI_SUCCESS @return EFI_SUCCESS
@ -579,7 +580,8 @@ Reclaim (
OUT UINTN *LastVariableOffset, OUT UINTN *LastVariableOffset,
IN BOOLEAN IsVolatile, IN BOOLEAN IsVolatile,
IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack, IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,
IN BOOLEAN ReclaimAnyway IN VARIABLE_HEADER *NewVariable,
IN UINTN NewVariableSize
) )
{ {
VARIABLE_HEADER *Variable; VARIABLE_HEADER *Variable;
@ -590,27 +592,24 @@ Reclaim (
UINT8 *ValidBuffer; UINT8 *ValidBuffer;
UINTN MaximumBufferSize; UINTN MaximumBufferSize;
UINTN VariableSize; UINTN VariableSize;
UINTN VariableNameSize;
UINTN UpdatingVariableNameSize;
UINTN NameSize; UINTN NameSize;
UINT8 *CurrPtr; UINT8 *CurrPtr;
VOID *Point0; VOID *Point0;
VOID *Point1; VOID *Point1;
BOOLEAN FoundAdded; BOOLEAN FoundAdded;
EFI_STATUS Status; EFI_STATUS Status;
CHAR16 *VariableNamePtr;
CHAR16 *UpdatingVariableNamePtr;
UINTN CommonVariableTotalSize; UINTN CommonVariableTotalSize;
UINTN HwErrVariableTotalSize; UINTN HwErrVariableTotalSize;
BOOLEAN NeedDoReclaim;
VARIABLE_HEADER *UpdatingVariable; VARIABLE_HEADER *UpdatingVariable;
VARIABLE_HEADER *UpdatingInDeletedTransition;
UpdatingVariable = NULL; UpdatingVariable = NULL;
UpdatingInDeletedTransition = NULL;
if (UpdatingPtrTrack != NULL) { if (UpdatingPtrTrack != NULL) {
UpdatingVariable = UpdatingPtrTrack->CurrPtr; UpdatingVariable = UpdatingPtrTrack->CurrPtr;
UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;
} }
NeedDoReclaim = FALSE;
VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase); VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
CommonVariableTotalSize = 0; CommonVariableTotalSize = 0;
@ -624,21 +623,22 @@ Reclaim (
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable->State == VAR_ADDED || if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&
Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED) Variable != UpdatingVariable &&
Variable != UpdatingInDeletedTransition
) { ) {
VariableSize = (UINTN) NextVariable - (UINTN) Variable; VariableSize = (UINTN) NextVariable - (UINTN) Variable;
MaximumBufferSize += VariableSize; MaximumBufferSize += VariableSize;
} else {
NeedDoReclaim = TRUE;
} }
Variable = NextVariable; Variable = NextVariable;
} }
if (!ReclaimAnyway && !NeedDoReclaim) { if (NewVariable != NULL) {
DEBUG ((EFI_D_INFO, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n")); //
return EFI_SUCCESS; // Add the new variable size.
//
MaximumBufferSize += NewVariableSize;
} }
// //
@ -665,25 +665,7 @@ Reclaim (
Variable = GetStartPointer (VariableStoreHeader); Variable = GetStartPointer (VariableStoreHeader);
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable->State == VAR_ADDED) { if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {
if (UpdatingVariable != NULL) {
if (UpdatingVariable == Variable) {
Variable = NextVariable;
continue;
}
VariableNameSize = NameSizeOfVariable(Variable);
UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);
VariableNamePtr = GetVariableNamePtr (Variable);
UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);
if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&
VariableNameSize == UpdatingVariableNameSize &&
CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {
Variable = NextVariable;
continue;
}
}
VariableSize = (UINTN) NextVariable - (UINTN) Variable; VariableSize = (UINTN) NextVariable - (UINTN) Variable;
CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
CurrPtr += VariableSize; CurrPtr += VariableSize;
@ -696,29 +678,13 @@ Reclaim (
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. // Reinstall all in delete transition variables.
// //
Variable = GetStartPointer (VariableStoreHeader); Variable = GetStartPointer (VariableStoreHeader);
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
// //
// Buffer has cached all ADDED variable. // Buffer has cached all ADDED variable.
@ -762,12 +728,48 @@ Reclaim (
Variable = NextVariable; Variable = NextVariable;
} }
//
// Install the new variable if it is not NULL.
//
if (NewVariable != NULL) {
if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
//
// No enough space to store the new variable.
//
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (!IsVolatile) {
if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
HwErrVariableTotalSize += NewVariableSize;
} else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
CommonVariableTotalSize += NewVariableSize;
}
if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||
(CommonVariableTotalSize > VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize))) {
//
// No enough space to store the new variable by NV or NV+HR attribute.
//
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
}
CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);
((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
if (UpdatingVariable != NULL) {
UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));
UpdatingPtrTrack->InDeletedTransitionPtr = NULL;
}
CurrPtr += NewVariableSize;
}
if (IsVolatile) { if (IsVolatile) {
// //
// If volatile variable store, just copy valid buffer. // If volatile variable store, just copy valid buffer.
// //
SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff); SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)); CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));
Status = EFI_SUCCESS; Status = EFI_SUCCESS;
} else { } else {
// //
@ -776,12 +778,12 @@ Reclaim (
Status = FtwVariableSpace ( Status = FtwVariableSpace (
VariableBase, VariableBase,
ValidBuffer, ValidBuffer,
(UINTN) (CurrPtr - (UINT8 *) ValidBuffer) (UINTN) (CurrPtr - ValidBuffer)
); );
CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size); CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
} }
if (!EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
*LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer); *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
if (!IsVolatile) { if (!IsVolatile) {
mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize; mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize; mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
@ -801,6 +803,7 @@ Reclaim (
*LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase; *LastVariableOffset = (UINTN) NextVariable - (UINTN) VariableBase;
} }
Done:
FreePool (ValidBuffer); FreePool (ValidBuffer);
return Status; return Status;
@ -1706,27 +1709,22 @@ UpdateVariable (
goto Done; goto Done;
} }
// //
// Perform garbage collection & reclaim operation. // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
// //
Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
&mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, FALSE); &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable, NextVariable, HEADER_ALIGN (VarSize));
if (EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
goto Done; //
} // The new variable has been integrated successfully during reclaiming.
// //
// If still no enough space, return out of resources. if (Variable->CurrPtr != NULL) {
// CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) CacheVariable->InDeletedTransitionPtr = NULL;
&& ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize))) }
|| (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);
&& ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) { FlushHobVariableToFlash (VariableName, VendorGuid);
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (Variable->CurrPtr != NULL) {
CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
CacheVariable->InDeletedTransitionPtr = NULL;
} }
goto Done;
} }
// //
// Four steps // Four steps
@ -1824,26 +1822,21 @@ UpdateVariable (
if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) > if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) { ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
// //
// Perform garbage collection & reclaim operation. // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
// //
Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
&mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, FALSE); &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable, NextVariable, HEADER_ALIGN (VarSize));
if (EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
goto Done; //
} // The new variable has been integrated successfully during reclaiming.
// //
// If still no enough space, return out of resources. if (Variable->CurrPtr != NULL) {
// CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) > CacheVariable->InDeletedTransitionPtr = NULL;
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size }
) { UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (Variable->CurrPtr != NULL) {
CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
CacheVariable->InDeletedTransitionPtr = NULL;
} }
goto Done;
} }
NextVariable->State = VAR_ADDED; NextVariable->State = VAR_ADDED;
@ -2693,7 +2686,8 @@ ReclaimForOS(
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
NULL, NULL,
FALSE NULL,
0
); );
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
} }
@ -2963,7 +2957,8 @@ VariableWriteServiceInitialize (
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
NULL, NULL,
TRUE NULL,
0
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;

View File

@ -531,7 +531,8 @@ AddPubKeyInStore (
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
NULL, NULL,
TRUE, NULL,
0,
TRUE TRUE
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {

View File

@ -731,11 +731,12 @@ PubKeyStoreFilter (
@param[in] IsVolatile The variable store is volatile or not; @param[in] IsVolatile The variable store is volatile or not;
if it is non-volatile, need FTW. if it is non-volatile, need FTW.
@param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure. @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
@param[in] NewVariable Pointer to new variable.
@param[in] NewVariableSize New variable size.
@param[in] ReclaimPubKeyStore Reclaim for public key database or not. @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
@param[in] ReclaimAnyway If TRUE, do reclaim anyway.
@return EFI_SUCCESS Reclaim operation has finished successfully. @return EFI_SUCCESS Reclaim operation has finished successfully.
@return EFI_OUT_OF_RESOURCES No enough memory resources. @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
@return EFI_DEVICE_ERROR The public key database doesn't exist. @return EFI_DEVICE_ERROR The public key database doesn't exist.
@return Others Unexpect error happened during reclaim operation. @return Others Unexpect error happened during reclaim operation.
@ -746,8 +747,9 @@ Reclaim (
OUT UINTN *LastVariableOffset, OUT UINTN *LastVariableOffset,
IN BOOLEAN IsVolatile, IN BOOLEAN IsVolatile,
IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack, IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,
IN BOOLEAN ReclaimPubKeyStore, IN VARIABLE_HEADER *NewVariable,
IN BOOLEAN ReclaimAnyway IN UINTN NewVariableSize,
IN BOOLEAN ReclaimPubKeyStore
) )
{ {
VARIABLE_HEADER *Variable; VARIABLE_HEADER *Variable;
@ -758,31 +760,28 @@ Reclaim (
UINT8 *ValidBuffer; UINT8 *ValidBuffer;
UINTN MaximumBufferSize; UINTN MaximumBufferSize;
UINTN VariableSize; UINTN VariableSize;
UINTN VariableNameSize;
UINTN UpdatingVariableNameSize;
UINTN NameSize; UINTN NameSize;
UINT8 *CurrPtr; UINT8 *CurrPtr;
VOID *Point0; VOID *Point0;
VOID *Point1; VOID *Point1;
BOOLEAN FoundAdded; BOOLEAN FoundAdded;
EFI_STATUS Status; EFI_STATUS Status;
CHAR16 *VariableNamePtr;
CHAR16 *UpdatingVariableNamePtr;
UINTN CommonVariableTotalSize; UINTN CommonVariableTotalSize;
UINTN HwErrVariableTotalSize; UINTN HwErrVariableTotalSize;
UINT32 *NewPubKeyIndex; UINT32 *NewPubKeyIndex;
UINT8 *NewPubKeyStore; UINT8 *NewPubKeyStore;
UINT32 NewPubKeySize; UINT32 NewPubKeySize;
VARIABLE_HEADER *PubKeyHeader; VARIABLE_HEADER *PubKeyHeader;
BOOLEAN NeedDoReclaim;
VARIABLE_HEADER *UpdatingVariable; VARIABLE_HEADER *UpdatingVariable;
VARIABLE_HEADER *UpdatingInDeletedTransition;
UpdatingVariable = NULL; UpdatingVariable = NULL;
UpdatingInDeletedTransition = NULL;
if (UpdatingPtrTrack != NULL) { if (UpdatingPtrTrack != NULL) {
UpdatingVariable = UpdatingPtrTrack->CurrPtr; UpdatingVariable = UpdatingPtrTrack->CurrPtr;
UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;
} }
NeedDoReclaim = FALSE;
VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase); VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);
CommonVariableTotalSize = 0; CommonVariableTotalSize = 0;
@ -800,21 +799,22 @@ Reclaim (
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable->State == VAR_ADDED || if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&
Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED) Variable != UpdatingVariable &&
Variable != UpdatingInDeletedTransition
) { ) {
VariableSize = (UINTN) NextVariable - (UINTN) Variable; VariableSize = (UINTN) NextVariable - (UINTN) Variable;
MaximumBufferSize += VariableSize; MaximumBufferSize += VariableSize;
} else {
NeedDoReclaim = TRUE;
} }
Variable = NextVariable; Variable = NextVariable;
} }
if (!ReclaimAnyway && !NeedDoReclaim) { if (NewVariable != NULL) {
DEBUG ((EFI_D_INFO, "Variable driver: no DELETED variable found, so no variable space could be reclaimed.\n")); //
return EFI_SUCCESS; // Add the new variable size.
//
MaximumBufferSize += NewVariableSize;
} }
// //
@ -836,6 +836,7 @@ Reclaim (
CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer); CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);
if (ReclaimPubKeyStore) { if (ReclaimPubKeyStore) {
ASSERT (IsVolatile == FALSE);
// //
// Trim the PubKeyStore and get new PubKeyIndex. // Trim the PubKeyStore and get new PubKeyIndex.
// //
@ -872,9 +873,9 @@ Reclaim (
CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex]; ((VARIABLE_HEADER*) CurrPtr)->PubKeyIndex = NewPubKeyIndex[Variable->PubKeyIndex];
CurrPtr += VariableSize; CurrPtr += VariableSize;
if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
HwErrVariableTotalSize += VariableSize; HwErrVariableTotalSize += VariableSize;
} else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) { } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
CommonVariableTotalSize += VariableSize; CommonVariableTotalSize += VariableSize;
} }
} }
@ -905,25 +906,7 @@ Reclaim (
Variable = GetStartPointer (VariableStoreHeader); Variable = GetStartPointer (VariableStoreHeader);
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable->State == VAR_ADDED) { if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {
if (UpdatingVariable != NULL) {
if (UpdatingVariable == Variable) {
Variable = NextVariable;
continue;
}
VariableNameSize = NameSizeOfVariable(Variable);
UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);
VariableNamePtr = GetVariableNamePtr (Variable);
UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);
if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&
VariableNameSize == UpdatingVariableNameSize &&
CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {
Variable = NextVariable;
continue;
}
}
VariableSize = (UINTN) NextVariable - (UINTN) Variable; VariableSize = (UINTN) NextVariable - (UINTN) Variable;
CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize); CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
CurrPtr += VariableSize; CurrPtr += VariableSize;
@ -936,29 +919,13 @@ Reclaim (
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. // Reinstall all in delete transition variables.
// //
Variable = GetStartPointer (VariableStoreHeader); Variable = GetStartPointer (VariableStoreHeader);
while (IsValidVariableHeader (Variable)) { while (IsValidVariableHeader (Variable)) {
NextVariable = GetNextVariablePtr (Variable); NextVariable = GetNextVariablePtr (Variable);
if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
// //
// Buffer has cached all ADDED variable. // Buffer has cached all ADDED variable.
@ -1001,6 +968,42 @@ Reclaim (
Variable = NextVariable; Variable = NextVariable;
} }
//
// Install the new variable if it is not NULL.
//
if (NewVariable != NULL) {
if ((UINTN) (CurrPtr - ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {
//
// No enough space to store the new variable.
//
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (!IsVolatile) {
if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
HwErrVariableTotalSize += NewVariableSize;
} else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
CommonVariableTotalSize += NewVariableSize;
}
if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||
(CommonVariableTotalSize > VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize))) {
//
// No enough space to store the new variable by NV or NV+HR attribute.
//
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
}
CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);
((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
if (UpdatingVariable != NULL) {
UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));
UpdatingPtrTrack->InDeletedTransitionPtr = NULL;
}
CurrPtr += NewVariableSize;
}
} }
if (IsVolatile) { if (IsVolatile) {
@ -1008,7 +1011,7 @@ Reclaim (
// If volatile variable store, just copy valid buffer. // If volatile variable store, just copy valid buffer.
// //
SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff); SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);
CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)); CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - ValidBuffer));
Status = EFI_SUCCESS; Status = EFI_SUCCESS;
} else { } else {
// //
@ -1017,12 +1020,12 @@ Reclaim (
Status = FtwVariableSpace ( Status = FtwVariableSpace (
VariableBase, VariableBase,
ValidBuffer, ValidBuffer,
(UINTN) (CurrPtr - (UINT8 *) ValidBuffer) (UINTN) (CurrPtr - ValidBuffer)
); );
CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size); CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);
} }
if (!EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
*LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer); *LastVariableOffset = (UINTN) (CurrPtr - ValidBuffer);
if (!IsVolatile) { if (!IsVolatile) {
mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize; mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;
mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize; mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;
@ -1050,6 +1053,7 @@ Reclaim (
FreePool (NewPubKeyIndex); FreePool (NewPubKeyIndex);
} }
Done:
FreePool (ValidBuffer); FreePool (ValidBuffer);
return Status; return Status;
@ -2101,33 +2105,29 @@ UpdateVariable (
goto Done; goto Done;
} }
// //
// Perform garbage collection & reclaim operation. // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
// //
Status = Reclaim ( Status = Reclaim (
mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
Variable, Variable,
FALSE, NextVariable,
HEADER_ALIGN (VarSize),
FALSE FALSE
); );
if (EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
goto Done; //
} // The new variable has been integrated successfully during reclaiming.
// //
// If still no enough space, return out of resources. if (Variable->CurrPtr != NULL) {
// CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) CacheVariable->InDeletedTransitionPtr = NULL;
&& ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize))) }
|| (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);
&& ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) { FlushHobVariableToFlash (VariableName, VendorGuid);
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (Variable->CurrPtr != NULL) {
CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
CacheVariable->InDeletedTransitionPtr = NULL;
} }
goto Done;
} }
// //
// Four steps // Four steps
@ -2225,32 +2225,28 @@ UpdateVariable (
if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) > if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) { ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {
// //
// Perform garbage collection & reclaim operation. // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.
// //
Status = Reclaim ( Status = Reclaim (
mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,
&mVariableModuleGlobal->VolatileLastVariableOffset, &mVariableModuleGlobal->VolatileLastVariableOffset,
TRUE, TRUE,
Variable, Variable,
FALSE, NextVariable,
HEADER_ALIGN (VarSize),
FALSE FALSE
); );
if (EFI_ERROR (Status)) { if (!EFI_ERROR (Status)) {
goto Done; //
} // The new variable has been integrated successfully during reclaiming.
// //
// If still no enough space, return out of resources. if (Variable->CurrPtr != NULL) {
// CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) > CacheVariable->InDeletedTransitionPtr = NULL;
((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size }
) { UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
if (Variable->CurrPtr != NULL) {
CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));
CacheVariable->InDeletedTransitionPtr = NULL;
} }
goto Done;
} }
NextVariable->State = VAR_ADDED; NextVariable->State = VAR_ADDED;
@ -3207,7 +3203,8 @@ ReclaimForOS(
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
NULL, NULL,
FALSE, NULL,
0,
FALSE FALSE
); );
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
@ -3478,8 +3475,9 @@ VariableWriteServiceInitialize (
&mVariableModuleGlobal->NonVolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset,
FALSE, FALSE,
NULL, NULL,
FALSE, NULL,
TRUE 0,
FALSE
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;

View File

@ -402,11 +402,12 @@ VariableCommonInitialize (
@param[in] IsVolatile The variable store is volatile or not; @param[in] IsVolatile The variable store is volatile or not;
if it is non-volatile, need FTW. if it is non-volatile, need FTW.
@param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure. @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.
@param[in] NewVariable Pointer to new variable.
@param[in] NewVariableSize New variable size.
@param[in] ReclaimPubKeyStore Reclaim for public key database or not. @param[in] ReclaimPubKeyStore Reclaim for public key database or not.
@param[in] ReclaimAnyway If TRUE, do reclaim anyway.
@return EFI_SUCCESS Reclaim operation has finished successfully. @return EFI_SUCCESS Reclaim operation has finished successfully.
@return EFI_OUT_OF_RESOURCES No enough memory resources. @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.
@return EFI_DEVICE_ERROR The public key database doesn't exist. @return EFI_DEVICE_ERROR The public key database doesn't exist.
@return Others Unexpect error happened during reclaim operation. @return Others Unexpect error happened during reclaim operation.
@ -417,8 +418,9 @@ Reclaim (
OUT UINTN *LastVariableOffset, OUT UINTN *LastVariableOffset,
IN BOOLEAN IsVolatile, IN BOOLEAN IsVolatile,
IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack, IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,
IN BOOLEAN ReclaimPubKeyStore, IN VARIABLE_HEADER *NewVariable,
IN BOOLEAN ReclaimAnyway IN UINTN NewVariableSize,
IN BOOLEAN ReclaimPubKeyStore
); );
/** /**