From 6ed71dcc327e0b52e262192bd737a40fc16ea526 Mon Sep 17 00:00:00 2001 From: qwang12 Date: Mon, 12 Mar 2007 07:56:16 +0000 Subject: [PATCH] Add a lock to protect the critical region in Service APIs for UEFI Runtime Variable Service to prevent re-entrance of the variable service API from from different allowable TPL level. In UEFI 2.x and EFI 1.10 spec, EFI_TPL_CALLBACK is the allowable TPL level for Variable services. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2444 6f19259b-4bc3-4df7-8a09-765794883524 --- .../EmuVariable/RuntimeDxe/EmuVariable.c | 118 +++++++++++--- .../EmuVariable/RuntimeDxe/EmuVariable.msa | 13 +- .../EmuVariable/RuntimeDxe/EmuVariableIpf.msa | 13 +- .../EmuVariable/RuntimeDxe/InitVariable.c | 14 +- .../EmuVariable/RuntimeDxe/Ipf/InitVariable.c | 16 +- .../EmuVariable/RuntimeDxe/Variable.h | 5 +- .../Variable/RuntimeDxe/InitVariable.c | 14 +- .../Variable/RuntimeDxe/Ipf/InitVariable.c | 18 +- .../Universal/Variable/RuntimeDxe/Variable.c | 154 +++++++++++++----- .../Universal/Variable/RuntimeDxe/Variable.h | 5 +- .../Variable/RuntimeDxe/Variable.msa | 15 +- .../Variable/RuntimeDxe/VariableIpf.msa | 15 +- 12 files changed, 275 insertions(+), 125 deletions(-) diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c index 208f3b4276..d39ecd2ce0 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -26,6 +26,37 @@ Revision History // ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal; +// +// This is a temperary function which will be removed +// when EfiAcquireLock in UefiLib can handle the +// the call in UEFI Runtimer driver in RT phase. +// +VOID +AcquireLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiAcquireLock (Lock); + } +} + +// +// This is a temperary function which will be removed +// when EfiAcquireLock in UefiLib can handle the +// the call in UEFI Runtimer driver in RT phase. +// +VOID +ReleaseLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiReleaseLock (Lock); + } +} + + STATIC UINT32 EFIAPI @@ -197,6 +228,13 @@ Returns: VARIABLE_STORE_HEADER *VariableStoreHeader[2]; UINTN Index; + // + // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName + // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to + // the correct places if this assumption does not hold TRUE anymore. + // + AcquireLockOnlyAtBootTime(&Global->VariableServicesLock); + // // 0: Non-Volatile, 1: Volatile // @@ -293,7 +331,7 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } // // Get data size @@ -301,7 +339,8 @@ Returns: VarDataSize = Variable.CurrPtr->DataSize; if (*DataSize >= VarDataSize) { if (Data == NULL) { - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize); @@ -310,11 +349,17 @@ Returns: } *DataSize = VarDataSize; - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } else { *DataSize = VarDataSize; - return EFI_BUFFER_TOO_SMALL; + Status = EFI_BUFFER_TOO_SMALL; + goto Done; } + +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } EFI_STATUS @@ -357,7 +402,7 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } while (TRUE) { @@ -377,7 +422,8 @@ Returns: Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER))); Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase)); } else { - goto Error; + Status = EFI_NOT_FOUND; + goto Done; } Variable.CurrPtr = Variable.StartPtr; @@ -408,13 +454,15 @@ Returns: } *VariableNameSize = VarNameSize; - return Status; + goto Done; } } } -Error: - return EFI_NOT_FOUND; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; + } EFI_STATUS @@ -470,36 +518,41 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Status == EFI_INVALID_PARAMETER) { - return Status; + goto Done; } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) { // // If EfiAtRuntime and the variable is Volatile and Runtime Access, // the volatile is ReadOnly, and SetVariable should be aborted and // return EFI_WRITE_PROTECTED. // - return EFI_WRITE_PROTECTED; + Status = EFI_WRITE_PROTECTED; + goto Done; } else if (sizeof (VARIABLE_HEADER) + (ArrayLength (VariableName) + DataSize) > MAX_VARIABLE_SIZE) { // // The size of the VariableName, including the Unicode Null in bytes plus // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes. // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS ) { // // Make sure if runtime bit is set, boot service bit is set also // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) { // // Runtime but Attribute is not Runtime // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) { // // Cannot set volatile variable in Runtime // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { // // Setting a data variable with no access, or zero DataSize attributes @@ -507,10 +560,12 @@ Returns: // if (!EFI_ERROR (Status)) { Variable.CurrPtr->State &= VAR_DELETED; - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } else { if (!EFI_ERROR (Status)) { // @@ -520,7 +575,8 @@ Returns: if (Variable.CurrPtr->DataSize == DataSize && !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) ) { - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } else if (Variable.CurrPtr->State == VAR_ADDED) { // // Mark the old variable as in delete transition @@ -540,20 +596,23 @@ Returns: if ((UINT32) (VarSize +*NonVolatileOffset) > ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size ) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } NextVariable = (VARIABLE_HEADER *) (UINT8 *) (*NonVolatileOffset + (UINTN) Global->NonVolatileVariableBase); *NonVolatileOffset = *NonVolatileOffset + VarSize; } else { if (EfiAtRuntime ()) { - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } if ((UINT32) (VarSize +*VolatileOffset) > ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size ) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } NextVariable = (VARIABLE_HEADER *) (UINT8 *) (*VolatileOffset + (UINTN) Global->VolatileVariableBase); @@ -593,7 +652,10 @@ Returns: } } - return EFI_SUCCESS; + Status = EFI_SUCCESS; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } #if (EFI_SPECIFICATION_VERSION >= 0x00020000) @@ -666,6 +728,8 @@ Returns: return EFI_INVALID_PARAMETER; } + AcquireLockOnlyAtBootTime(&Global->VariableServicesLock); + if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) { // // Query is Volatile related. @@ -716,6 +780,7 @@ Returns: Variable = NextVariable; } + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); return EFI_SUCCESS; } #endif @@ -796,11 +861,14 @@ Returns: if (NULL == mVariableModuleGlobal) { return EFI_OUT_OF_RESOURCES; } + + EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, EFI_TPL_CALLBACK); + // // Intialize volatile variable store // Status = InitializeVariableStore ( - &mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase, + &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase, &mVariableModuleGlobal->VolatileLastVariableOffset ); @@ -811,7 +879,7 @@ Returns: // Intialize non volatile variable store // Status = InitializeVariableStore ( - &mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase, + &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset ); diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.msa b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.msa index a4ae482a0e..7acd1ba819 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.msa +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.msa @@ -8,11 +8,11 @@ Emulation Variable for EFI_RUNTIME_SERVICES. This module provides three EFI_RUNTIME_SERVICES: SetVariable, GetVariable, GetNextVariableName Copyright (c) 2006 - 2007, 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 - http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + 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 + http://opensource.org/licenses/bsd-license.php + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052 @@ -40,6 +40,9 @@ UefiBootServicesTableLib + + UefiLib + Variable.h diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariableIpf.msa b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariableIpf.msa index 770fa78d68..9264ffacda 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariableIpf.msa +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariableIpf.msa @@ -8,11 +8,11 @@ Emulation Variable for EFI_RUNTIME_SERVICES. This module provides three EFI_RUNTIME_SERVICES: SetVariable, GetVariable, GetNextVariableName Copyright (c) 2006 - 2007, 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 - http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + 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 + http://opensource.org/licenses/bsd-license.php + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052 @@ -43,6 +43,9 @@ UefiBootServicesTableLib + + UefiLib + Variable.h diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/InitVariable.c b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/InitVariable.c index 477869cf00..570ba7257e 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/InitVariable.c +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/InitVariable.c @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -51,7 +51,7 @@ Returns: Attributes OPTIONAL, DataSize, Data, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -77,7 +77,7 @@ Returns: VariableNameSize, VariableName, VendorGuid, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -107,7 +107,7 @@ Returns: Attributes, DataSize, Data, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], &mVariableModuleGlobal->VolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset, mVariableModuleGlobal->FvbInstance @@ -138,7 +138,7 @@ Returns: MaximumVariableStorageSize, RemainingVariableStorageSize, MaximumVariableSize, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -162,11 +162,11 @@ Returns: { EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase ); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal); } diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Ipf/InitVariable.c b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Ipf/InitVariable.c index b4b29cf7c6..cbf5aecb88 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Ipf/InitVariable.c +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Ipf/InitVariable.c @@ -60,7 +60,7 @@ Returns: (UINT32 *) Arg4, (UINTN *) Arg5, (VOID *) Arg6, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -70,7 +70,7 @@ Returns: (UINTN *) Arg2, (CHAR16 *) Arg3, (EFI_GUID *) Arg4, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -82,7 +82,7 @@ Returns: (UINT32) Arg4, (UINTN) Arg5, (VOID *) Arg6, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], (UINTN *) &Global->VolatileLastVariableOffset, (UINTN *) &Global->NonVolatileLastVariableOffset, Global->FvbInstance @@ -96,7 +96,7 @@ Returns: (UINT64 *) Arg3, (UINT64 *) Arg4, (UINT64 *) Arg5, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -125,18 +125,18 @@ Returns: --*/ { CopyMem ( - &mVariableModuleGlobal->VariableBase[Virtual], - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Virtual], + &mVariableModuleGlobal->VariableGlobal[Physical], sizeof (VARIABLE_GLOBAL) ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Virtual].NonVolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Virtual].NonVolatileVariableBase ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Virtual].VolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Virtual].VolatileVariableBase ); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal); } diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Variable.h b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Variable.h index bf21a3e86d..4c2a468c4d 100644 --- a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Variable.h +++ b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/Variable.h @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -65,10 +65,11 @@ typedef struct { typedef struct { EFI_PHYSICAL_ADDRESS VolatileVariableBase; EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; + EFI_LOCK VariableServicesLock; } VARIABLE_GLOBAL; typedef struct { - VARIABLE_GLOBAL VariableBase[2]; + VARIABLE_GLOBAL VariableGlobal[2]; UINTN VolatileLastVariableOffset; UINTN NonVolatileLastVariableOffset; UINT32 FvbInstance; diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/InitVariable.c b/EdkModulePkg/Universal/Variable/RuntimeDxe/InitVariable.c index 477869cf00..570ba7257e 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/InitVariable.c +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/InitVariable.c @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -51,7 +51,7 @@ Returns: Attributes OPTIONAL, DataSize, Data, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -77,7 +77,7 @@ Returns: VariableNameSize, VariableName, VendorGuid, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -107,7 +107,7 @@ Returns: Attributes, DataSize, Data, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], &mVariableModuleGlobal->VolatileLastVariableOffset, &mVariableModuleGlobal->NonVolatileLastVariableOffset, mVariableModuleGlobal->FvbInstance @@ -138,7 +138,7 @@ Returns: MaximumVariableStorageSize, RemainingVariableStorageSize, MaximumVariableSize, - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], mVariableModuleGlobal->FvbInstance ); } @@ -162,11 +162,11 @@ Returns: { EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase ); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal); } diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/Ipf/InitVariable.c b/EdkModulePkg/Universal/Variable/RuntimeDxe/Ipf/InitVariable.c index b4b29cf7c6..bc74a67121 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/Ipf/InitVariable.c +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/Ipf/InitVariable.c @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -60,7 +60,7 @@ Returns: (UINT32 *) Arg4, (UINTN *) Arg5, (VOID *) Arg6, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -70,7 +70,7 @@ Returns: (UINTN *) Arg2, (CHAR16 *) Arg3, (EFI_GUID *) Arg4, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -82,7 +82,7 @@ Returns: (UINT32) Arg4, (UINTN) Arg5, (VOID *) Arg6, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], (UINTN *) &Global->VolatileLastVariableOffset, (UINTN *) &Global->NonVolatileLastVariableOffset, Global->FvbInstance @@ -96,7 +96,7 @@ Returns: (UINT64 *) Arg3, (UINT64 *) Arg4, (UINT64 *) Arg5, - &Global->VariableBase[VirtualMode], + &Global->VariableGlobal[VirtualMode], Global->FvbInstance ); return ReturnVal; @@ -125,18 +125,18 @@ Returns: --*/ { CopyMem ( - &mVariableModuleGlobal->VariableBase[Virtual], - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Virtual], + &mVariableModuleGlobal->VariableGlobal[Physical], sizeof (VARIABLE_GLOBAL) ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Virtual].NonVolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Virtual].NonVolatileVariableBase ); EfiConvertPointer ( 0x0, - (VOID **) &mVariableModuleGlobal->VariableBase[Virtual].VolatileVariableBase + (VOID **) &mVariableModuleGlobal->VariableGlobal[Virtual].VolatileVariableBase ); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal); } diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c index aa0742364e..46ffc7ddbb 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -27,6 +27,36 @@ Revision History // ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal; +// +// This is a temperary function which will be removed +// when EfiAcquireLock in UefiLib can handle the +// the call in UEFI Runtimer driver in RT phase. +// +VOID +AcquireLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiAcquireLock (Lock); + } +} + +// +// This is a temperary function which will be removed +// when EfiAcquireLock in UefiLib can handle the +// the call in UEFI Runtimer driver in RT phase. +// +VOID +ReleaseLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiReleaseLock (Lock); + } +} + STATIC UINT32 EFIAPI @@ -529,6 +559,13 @@ Returns: VARIABLE_STORE_HEADER *VariableStoreHeader[2]; UINTN Index; + // + // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName + // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to + // the correct places if this assumption does not hold TRUE anymore. + // + AcquireLockOnlyAtBootTime(&Global->VariableServicesLock); + // // 0: Non-Volatile, 1: Volatile // @@ -631,7 +668,7 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } // // Get data size @@ -639,7 +676,8 @@ Returns: VarDataSize = Variable.CurrPtr->DataSize; if (*DataSize >= VarDataSize) { if (Data == NULL) { - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize); @@ -648,11 +686,17 @@ Returns: } *DataSize = VarDataSize; - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } else { *DataSize = VarDataSize; - return EFI_BUFFER_TOO_SMALL; + Status = EFI_BUFFER_TOO_SMALL; + goto Done; } + +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } EFI_STATUS @@ -695,7 +739,7 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } if (VariableName[0] != 0) { @@ -716,7 +760,8 @@ Returns: Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER))); Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase)); } else { - goto Error; + Status = EFI_NOT_FOUND; + goto Done; } Variable.CurrPtr = Variable.StartPtr; @@ -747,15 +792,16 @@ Returns: } *VariableNameSize = VarNameSize; - return Status; + goto Done; } } Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr); } -Error: - return EFI_NOT_FOUND; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } EFI_STATUS @@ -819,41 +865,47 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Status == EFI_INVALID_PARAMETER) { - return Status; + goto Done; } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) { // // If EfiAtRuntime and the variable is Volatile and Runtime Access, // the volatile is ReadOnly, and SetVariable should be aborted and // return EFI_WRITE_PROTECTED. // - return EFI_WRITE_PROTECTED; + Status = EFI_WRITE_PROTECTED; + goto Done; } else if (sizeof (VARIABLE_HEADER) + ArrayLength (VariableName) + DataSize > MAX_VARIABLE_SIZE) { // // The size of the VariableName, including the Unicode Null in bytes plus // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes. // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (Attributes == EFI_VARIABLE_NON_VOLATILE) { // // Make sure not only EFI_VARIABLE_NON_VOLATILE is set // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) { // // Make sure if runtime bit is set, boot service bit is set also // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) { // // Runtime but Attribute is not Runtime // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) { // // Cannot set volatile variable in Runtime // - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { // // Setting a data variable with no access, or zero DataSize attributes @@ -873,13 +925,15 @@ Returns: &State ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } else { if (!EFI_ERROR (Status)) { // @@ -889,7 +943,8 @@ Returns: if (Variable.CurrPtr->DataSize == DataSize && !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) ) { - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } else if (Variable.CurrPtr->State == VAR_ADDED) { // // Mark the old variable as in delete transition @@ -907,7 +962,7 @@ Returns: &State ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } } } @@ -959,14 +1014,15 @@ Returns: ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size ) { if (EfiAtRuntime ()) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // // Perform garbage collection & reclaim operation // Status = Reclaim (Global->NonVolatileVariableBase, NonVolatileOffset, FALSE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // // If still no enough space, return out of resources @@ -974,7 +1030,8 @@ Returns: if ((UINT32) (VarSize +*NonVolatileOffset) > ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size ) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } Reclaimed = TRUE; @@ -999,7 +1056,7 @@ Returns: ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // // Step 2: @@ -1015,7 +1072,7 @@ Returns: ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // // Step 3: @@ -1032,14 +1089,15 @@ Returns: ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } *NonVolatileOffset = *NonVolatileOffset + VarSize; } else { if (EfiAtRuntime ()) { - return EFI_INVALID_PARAMETER; + Status = EFI_INVALID_PARAMETER; + goto Done; } if ((UINT32) (VarSize +*VolatileOffset) > @@ -1050,7 +1108,7 @@ Returns: // Status = Reclaim (Global->VolatileVariableBase, VolatileOffset, TRUE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // // If still no enough space, return out of resources @@ -1058,7 +1116,8 @@ Returns: if ((UINT32) (VarSize +*VolatileOffset) > ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size ) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } Reclaimed = TRUE; @@ -1076,7 +1135,7 @@ Returns: ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } *VolatileOffset = *VolatileOffset + VarSize; @@ -1099,12 +1158,15 @@ Returns: ); if (EFI_ERROR (Status)) { - return Status; + goto Done; } } } - return EFI_SUCCESS; + Status = EFI_SUCCESS; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } #if (EFI_SPECIFICATION_VERSION >= 0x00020000) @@ -1150,7 +1212,7 @@ Returns: VARIABLE_HEADER *NextVariable; UINT64 VariableSize; VARIABLE_STORE_HEADER *VariableStoreHeader; - + if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) { return EFI_INVALID_PARAMETER; } @@ -1172,6 +1234,8 @@ Returns: return EFI_INVALID_PARAMETER; } + AcquireLockOnlyAtBootTime(&Global->VariableServicesLock); + if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) { // // Query is Volatile related. @@ -1232,6 +1296,7 @@ Returns: Variable = NextVariable; } + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); return EFI_SUCCESS; } #endif @@ -1289,6 +1354,9 @@ Returns: if (EFI_ERROR (Status)) { return Status; } + + EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, EFI_TPL_CALLBACK); + // // Allocate memory for volatile variable store // @@ -1308,7 +1376,7 @@ Returns: // // Variable Specific Data // - mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore; + mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore; mVariableModuleGlobal->VolatileLastVariableOffset = sizeof (VARIABLE_STORE_HEADER); VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE; @@ -1354,7 +1422,7 @@ Returns: // // Get address of non volatile variable store base // - mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase = VariableStoreEntry.Base; + mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = VariableStoreEntry.Base; // // Check Integrity @@ -1363,7 +1431,7 @@ Returns: // Find the Correct Instance of the FV Block Service. // Instance = 0; - CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase); + CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase); while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) { FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr); if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) { @@ -1378,7 +1446,7 @@ Returns: if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) { if (~VariableStoreHeader->Size == 0) { Status = UpdateVariableStore ( - &mVariableModuleGlobal->VariableBase[Physical], + &mVariableModuleGlobal->VariableGlobal[Physical], FALSE, FALSE, mVariableModuleGlobal->FvbInstance, @@ -1392,7 +1460,7 @@ Returns: } } - mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr); + mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr); // // Parse non-volatile variable data and get last variable offset // @@ -1410,7 +1478,7 @@ Returns: // if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) { Status = Reclaim ( - mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase, + mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE ); @@ -1426,13 +1494,13 @@ Returns: // Check if the free area is really free. // for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) { - Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase)[Index]; + Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase)[Index]; if (Data != 0xff) { // // There must be something wrong in variable store, do reclaim operation. // Status = Reclaim ( - mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase, + mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE ); diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.h index 3d0264de78..790b3dc50a 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.h +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.h @@ -1,6 +1,6 @@ /*++ -Copyright (c) 2006, Intel Corporation +Copyright (c) 2006 - 2007, 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 @@ -66,10 +66,11 @@ typedef struct { typedef struct { EFI_PHYSICAL_ADDRESS VolatileVariableBase; EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; + EFI_LOCK VariableServicesLock; } VARIABLE_GLOBAL; typedef struct { - VARIABLE_GLOBAL VariableBase[2]; + VARIABLE_GLOBAL VariableGlobal[2]; UINTN VolatileLastVariableOffset; UINTN NonVolatileLastVariableOffset; UINT32 FvbInstance; diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.msa b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.msa index 50a53ed19e..227d8a1344 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.msa +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.msa @@ -8,11 +8,11 @@ Component description file for Variable module. This module installs three EFI_RUNTIME_SERVICES: SetVariable, GetVariable, GetNextVariableName. Copyright (c) 2006 - 2007, 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 - http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + 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 + http://opensource.org/licenses/bsd-license.php + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052 @@ -46,6 +46,9 @@ UefiBootServicesTableLib + + UefiLib + Variable.h @@ -87,7 +90,7 @@ PcdFlashNvStorageVariableBase gEfiGenericPlatformTokenSpaceGuid - The driver gets the Variable store base address from this PCD. This base address point to + The driver gets the Variable store base address from this PCD. This base address point to an EFI_FIRMWARE_VOLUMN_HEADER struct. diff --git a/EdkModulePkg/Universal/Variable/RuntimeDxe/VariableIpf.msa b/EdkModulePkg/Universal/Variable/RuntimeDxe/VariableIpf.msa index 68c51c07ce..cbc60df38e 100644 --- a/EdkModulePkg/Universal/Variable/RuntimeDxe/VariableIpf.msa +++ b/EdkModulePkg/Universal/Variable/RuntimeDxe/VariableIpf.msa @@ -8,11 +8,11 @@ Component description file for Variable module. This module installs three EFI_RUNTIME_SERVICES: SetVariable, GetVariable, GetNextVariableName. Copyright (c) 2006 - 2007, 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 - http://opensource.org/licenses/bsd-license.php - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + 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 + http://opensource.org/licenses/bsd-license.php + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052 @@ -49,6 +49,9 @@ UefiBootServicesTableLib + + UefiLib + Variable.h @@ -87,7 +90,7 @@ PcdFlashNvStorageVariableBase gEfiGenericPlatformTokenSpaceGuid - The driver gets the Variable store base address from this PCD. This base address point to + The driver gets the Variable store base address from this PCD. This base address point to an EFI_FIRMWARE_VOLUMN_HEADER struct. -- 2.39.2