X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=EdkModulePkg%2FUniversal%2FEmuVariable%2FRuntimeDxe%2FEmuVariable.c;h=8695b7526a699d734d8faa8dfbec9b0632861744;hp=093705c60dd60c00a4989be47909ce6be1e6260f;hb=6dcb94c713b1373cc78117b7713e654889802114;hpb=3820a1717bbe60b0734951fce3cfb4d06b6a1529 diff --git a/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c b/EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c index 093705c60d..8695b7526a 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,83 +26,39 @@ Revision History // ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal; -UINT32 -EFIAPI -ArrayLength ( - IN CHAR16 *String +// +// 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. +// +STATIC +VOID +AcquireLockOnlyAtBootTime ( + IN EFI_LOCK *Lock ) -/*++ - -Routine Description: - - Determine the length of null terminated char16 array. - -Arguments: - - String Null-terminated CHAR16 array pointer. - -Returns: - - UINT32 Number of bytes in the string, including the double NULL at the end; - ---*/ { - UINT32 Count; - - if (NULL == String) { - return 0; + if (!EfiAtRuntime ()) { + EfiAcquireLock (Lock); } - - Count = 0; - - while (0 != String[Count]) { - Count++; - } - - return (Count * 2) + 2; } -VARIABLE_STORE_STATUS -EFIAPI -GetVariableStoreStatus ( - IN VARIABLE_STORE_HEADER *VarStoreHeader +// +// 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. +// +STATIC +VOID +ReleaseLockOnlyAtBootTime ( + IN EFI_LOCK *Lock ) -/*++ - -Routine Description: - - This code gets the pointer to the variable name. - -Arguments: - - VarStoreHeader Pointer to the Variable Store Header. - -Returns: - - EfiHealthy Variable store is healthy - EfiRaw Variable store is raw - EfiInvalid Variable store is invalid - ---*/ { - if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE && - VarStoreHeader->Format == VARIABLE_STORE_FORMATTED && - VarStoreHeader->State == VARIABLE_STORE_HEALTHY - ) { - - return EfiValid; - } else if (VarStoreHeader->Signature == 0xffffffff && - VarStoreHeader->Size == 0xffffffff && - VarStoreHeader->Format == 0xff && - VarStoreHeader->State == 0xff - ) { - - return EfiRaw; - } else { - return EfiInvalid; + if (!EfiAtRuntime ()) { + EfiReleaseLock (Lock); } } +STATIC UINT8 * EFIAPI GetVariableDataPtr ( @@ -133,6 +89,7 @@ Returns: return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize)); } +STATIC VARIABLE_HEADER * EFIAPI GetNextVariablePtr ( @@ -173,6 +130,7 @@ Returns: return VarHeader; } +STATIC VARIABLE_HEADER * EFIAPI GetEndPointer ( @@ -200,6 +158,7 @@ Returns: return (VARIABLE_HEADER *) ((UINTN) VolHeader + VolHeader->Size); } +STATIC EFI_STATUS EFIAPI FindVariable ( @@ -233,6 +192,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 // @@ -265,7 +231,7 @@ Returns: return EFI_SUCCESS; } else { if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) { - if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), ArrayLength (VariableName))) { + if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize)) { PtrTrack->CurrPtr = Variable[Index]; PtrTrack->Volatile = (BOOLEAN) Index; return EFI_SUCCESS; @@ -329,24 +295,35 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } // // Get data size // VarDataSize = Variable.CurrPtr->DataSize; if (*DataSize >= VarDataSize) { + if (Data == NULL) { + Status = EFI_INVALID_PARAMETER; + goto Done; + } + CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize); - if (Attributes) { + if (Attributes != NULL) { *Attributes = Variable.CurrPtr->Attributes; } *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 @@ -389,7 +366,7 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) { - return Status; + goto Done; } while (TRUE) { @@ -409,7 +386,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 { - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } Variable.CurrPtr = Variable.StartPtr; @@ -440,12 +418,15 @@ Returns: } *VariableNameSize = VarNameSize; - return Status; + goto Done; } } } - return EFI_NOT_FOUND; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; + } EFI_STATUS @@ -501,45 +482,54 @@ Returns: Status = FindVariable (VariableName, VendorGuid, &Variable, Global); if (Status == EFI_INVALID_PARAMETER) { - return Status; - } - // - // 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. - // - else if (sizeof (VARIABLE_HEADER) + (ArrayLength (VariableName) + DataSize) > MAX_VARIABLE_SIZE) { - return EFI_INVALID_PARAMETER; - } - // - // Make sure if runtime bit is set, boot service bit is set also - // - else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS + 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. + // + Status = EFI_WRITE_PROTECTED; + goto Done; + } else if (sizeof (VARIABLE_HEADER) + (StrSize (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. + // + Status = EFI_INVALID_PARAMETER; + goto Done; + } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS ) { - return EFI_INVALID_PARAMETER; - } - // - // Runtime but Attribute is not Runtime - // - else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) { - return EFI_INVALID_PARAMETER; - } - // - // Cannot set volatile variable in Runtime - // - else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) { - return EFI_INVALID_PARAMETER; - } - // - // Setting a data variable with no access, or zero DataSize attributes - // specified causes it to be deleted. - // - else if (DataSize == 0 || Attributes == 0) { + // + // Make sure if runtime bit is set, boot service bit is set also + // + Status = EFI_INVALID_PARAMETER; + goto Done; + } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) { + // + // Runtime but Attribute is not Runtime + // + Status = EFI_INVALID_PARAMETER; + goto Done; + } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) { + // + // Cannot set volatile variable in Runtime + // + 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 + // specified causes it to be deleted. + // 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)) { // @@ -549,7 +539,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 @@ -561,7 +552,7 @@ Returns: // Create a new variable and copy the data. // VarNameOffset = sizeof (VARIABLE_HEADER); - VarNameSize = ArrayLength (VariableName); + VarNameSize = StrSize (VariableName); VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize); VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize); @@ -569,20 +560,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); @@ -622,7 +616,10 @@ Returns: } } - return EFI_SUCCESS; + Status = EFI_SUCCESS; +Done: + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); + return Status; } #if (EFI_SPECIFICATION_VERSION >= 0x00020000) @@ -688,8 +685,15 @@ Returns: // Make sure RT Attribute is set if we are in Runtime phase. // return EFI_INVALID_PARAMETER; + } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) { + // + // Cannot Query volatile variable in Runtime + // + return EFI_INVALID_PARAMETER; } + AcquireLockOnlyAtBootTime(&Global->VariableServicesLock); + if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) { // // Query is Volatile related. @@ -740,10 +744,12 @@ Returns: Variable = NextVariable; } + ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock); return EFI_SUCCESS; } #endif +STATIC EFI_STATUS EFIAPI InitializeVariableStore ( @@ -819,11 +825,14 @@ Returns: if (NULL == mVariableModuleGlobal) { return EFI_OUT_OF_RESOURCES; } + + EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, EFI_TPL_NOTIFY); + // // Intialize volatile variable store // Status = InitializeVariableStore ( - &mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase, + &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase, &mVariableModuleGlobal->VolatileLastVariableOffset ); @@ -834,7 +843,7 @@ Returns: // Intialize non volatile variable store // Status = InitializeVariableStore ( - &mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase, + &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase, &mVariableModuleGlobal->NonVolatileLastVariableOffset );