X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FUniversal%2FVariable%2FRuntimeDxe%2FVariableDxe.c;h=6b04f4f7b39451cbaff368919f8a0c27b1d0f911;hp=9e371c0a8577c3251716632c5b8137ff9e74cd26;hb=3d7ebd6434311f6cf7e9dbb42c9db0f6f3185085;hpb=ff8438477f2dcea28149514de25368ac0b2c02ee diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c index 9e371c0a85..6b04f4f7b3 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c @@ -1,33 +1,50 @@ /** @file - Implement all four UEFI Runtime Variable services for the nonvolatile and volatile storage space and install variable architecture protocol. - -Copyright (c) 2006 - 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 -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. +Copyright (C) 2013, Red Hat, Inc. +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP
+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. **/ #include "Variable.h" -extern VARIABLE_STORE_HEADER *mNvVariableCache; -extern VARIABLE_INFO_ENTRY *gVariableInfo; -EFI_HANDLE mHandle = NULL; -EFI_EVENT mVirtualAddressChangeEvent = NULL; -EFI_EVENT mFtwRegistration = NULL; -extern BOOLEAN mEndOfDxe; -extern BOOLEAN mEnableLocking; -EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock = { VariableLockRequestToLock }; +extern VARIABLE_STORE_HEADER *mNvVariableCache; +extern EFI_FIRMWARE_VOLUME_HEADER *mNvFvHeaderCache; +extern VARIABLE_INFO_ENTRY *gVariableInfo; +EFI_HANDLE mHandle = NULL; +EFI_EVENT mVirtualAddressChangeEvent = NULL; +EFI_EVENT mFtwRegistration = NULL; +extern BOOLEAN mEndOfDxe; +VOID ***mVarCheckAddressPointer = NULL; +UINTN mVarCheckAddressPointerCount = 0; +EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock = { VariableLockRequestToLock }; +EDKII_VAR_CHECK_PROTOCOL mVarCheck = { VarCheckRegisterSetVariableCheckHandler, + VarCheckVariablePropertySet, + VarCheckVariablePropertyGet }; + +/** + Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc). + Record their initial State when variable write service is ready. + +**/ +VOID +EFIAPI +RecordSecureBootPolicyVarData( + VOID + ); /** Return TRUE if ExitBootServices () has been called. - + @retval TRUE If ExitBootServices () has been called. **/ BOOLEAN @@ -42,8 +59,8 @@ AtRuntime ( /** Initializes a basic mutual exclusion lock. - This function initializes a basic mutual exclusion lock to the released state - and returns the lock. Each lock provides mutual exclusion access at its task + This function initializes a basic mutual exclusion lock to the released state + and returns the lock. Each lock provides mutual exclusion access at its task priority level. Since there is no preemption or multiprocessor support in EFI, acquiring the lock only consists of raising to the locks TPL. If Lock is NULL, then ASSERT(). @@ -111,7 +128,7 @@ ReleaseLockOnlyAtBootTime ( } /** - Retrive the Fault Tolerent Write protocol interface. + Retrieve the Fault Tolerent Write protocol interface. @param[out] FtwProtocol The interface of Ftw protocol @@ -134,12 +151,12 @@ GetFtwProtocol ( &gEfiFaultTolerantWriteProtocolGuid, NULL, FtwProtocol - ); + ); return Status; } /** - Retrive the FVB protocol interface by HANDLE. + Retrieve the FVB protocol interface by HANDLE. @param[in] FvBlockHandle The handle of FVB protocol that provides services for reading, writing, and erasing the target block. @@ -148,7 +165,7 @@ GetFtwProtocol ( @retval EFI_SUCCESS The interface information for the specified protocol was returned. @retval EFI_UNSUPPORTED The device does not support the FVB protocol. @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL. - + **/ EFI_STATUS GetFvbByHandle ( @@ -169,7 +186,7 @@ GetFvbByHandle ( /** Function returns an array of handles that support the FVB protocol - in a buffer allocated from pool. + in a buffer allocated from pool. @param[out] NumberHandles The number of handles returned in Buffer. @param[out] Buffer A pointer to the buffer to return the requested @@ -180,7 +197,7 @@ GetFvbByHandle ( @retval EFI_NOT_FOUND No FVB handle was found. @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results. @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL. - + **/ EFI_STATUS GetFvbCountAndBuffer ( @@ -221,6 +238,8 @@ VariableClassAddressChangeEvent ( IN VOID *Context ) { + UINTN Index; + EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes); @@ -236,7 +255,20 @@ VariableClassAddressChangeEvent ( EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.HobVariableBase); EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal); - EfiConvertPointer (0x0, (VOID **) &mNvVariableCache); + EfiConvertPointer (0x0, (VOID **) &mNvVariableCache); + EfiConvertPointer (0x0, (VOID **) &mNvFvHeaderCache); + + if (mAuthContextOut.AddressPointer != NULL) { + for (Index = 0; Index < mAuthContextOut.AddressPointerCount; Index++) { + EfiConvertPointer (0x0, (VOID **) mAuthContextOut.AddressPointer[Index]); + } + } + + if (mVarCheckAddressPointer != NULL) { + for (Index = 0; Index < mVarCheckAddressPointerCount; Index++) { + EfiConvertPointer (0x0, (VOID **) mVarCheckAddressPointer[Index]); + } + } } @@ -258,14 +290,28 @@ OnReadyToBoot ( VOID *Context ) { - // - // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled. - // - mEndOfDxe = TRUE; + if (!mEndOfDxe) { + MorLockInitAtEndOfDxe (); + // + // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled. + // + mEndOfDxe = TRUE; + mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount); + // + // The initialization for variable quota. + // + InitializeVariableQuota (); + } ReclaimForOS (); if (FeaturePcdGet (PcdVariableCollectStatistics)) { - gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo); + if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { + gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo); + } else { + gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo); + } } + + gBS->CloseEvent (Event); } /** @@ -284,18 +330,30 @@ OnEndOfDxe ( VOID *Context ) { + DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n")); + MorLockInitAtEndOfDxe (); mEndOfDxe = TRUE; + mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount); + // + // The initialization for variable quota. + // + InitializeVariableQuota (); + if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) { + ReclaimForOS (); + } + + gBS->CloseEvent (Event); } /** Fault Tolerant Write protocol notification event handler. - Non-Volatile variable write may needs FTW protocol to reclaim when + Non-Volatile variable write may needs FTW protocol to reclaim when writting variable. @param[in] Event Event whose notification function is being invoked. @param[in] Context Pointer to the notification function's context. - + **/ VOID EFIAPI @@ -313,6 +371,7 @@ FtwNotificationEvent ( UINT64 Length; EFI_PHYSICAL_ADDRESS VariableStoreBase; UINT64 VariableStoreLength; + UINTN FtwMaxBlockSize; // // Ensure FTW protocol is installed. @@ -321,7 +380,12 @@ FtwNotificationEvent ( if (EFI_ERROR (Status)) { return ; } - + + Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize); + if (!EFI_ERROR (Status)) { + ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize); + } + // // Find the proper FVB protocol for variable. // @@ -338,7 +402,7 @@ FtwNotificationEvent ( // // Mark the variable storage region of the FLASH as RUNTIME. // - VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase; + VariableStoreBase = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength); VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size; BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK); Length = VariableStoreLength + (VariableStoreBase - BaseAddress); @@ -346,7 +410,7 @@ FtwNotificationEvent ( Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor); if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n")); + DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n")); } else { Status = gDS->SetMemorySpaceAttributes ( BaseAddress, @@ -357,21 +421,29 @@ FtwNotificationEvent ( DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n")); } } - + Status = VariableWriteServiceInitialize (); - ASSERT_EFI_ERROR (Status); - + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status)); + } + + // + // Some Secure Boot Policy Var (SecureBoot, etc) updates following other + // Secure Boot Policy Variable change. Record their initial value. + // + RecordSecureBootPolicyVarData(); + // // Install the Variable Write Architectural protocol. // Status = gBS->InstallProtocolInterface ( &mHandle, - &gEfiVariableWriteArchProtocolGuid, + &gEfiVariableWriteArchProtocolGuid, EFI_NATIVE_INTERFACE, NULL ); ASSERT_EFI_ERROR (Status); - + // // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again. // @@ -382,13 +454,13 @@ FtwNotificationEvent ( /** Variable Driver main entry point. The Variable driver places the 4 EFI - runtime services in the EFI System Table and installs arch protocols - for variable read and write services being availible. It also registers + runtime services in the EFI System Table and installs arch protocols + for variable read and write services being available. It also registers a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event. - @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] ImageHandle The firmware allocated handle for the EFI image. @param[in] SystemTable A pointer to the EFI System Table. - + @retval EFI_SUCCESS Variable service successfully initialized. **/ @@ -414,17 +486,25 @@ VariableServiceInitialize ( ); ASSERT_EFI_ERROR (Status); + Status = gBS->InstallMultipleProtocolInterfaces ( + &mHandle, + &gEdkiiVarCheckProtocolGuid, + &mVarCheck, + NULL + ); + ASSERT_EFI_ERROR (Status); + SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable; SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName; SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable; SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo; - + // // Now install the Variable Runtime Architectural protocol on a new handle. // Status = gBS->InstallProtocolInterface ( &mHandle, - &gEfiVariableArchProtocolGuid, + &gEfiVariableArchProtocolGuid, EFI_NATIVE_INTERFACE, NULL ); @@ -432,7 +512,7 @@ VariableServiceInitialize ( // // Register FtwNotificationEvent () notify function. - // + // EfiCreateProtocolNotifyEvent ( &gEfiFaultTolerantWriteProtocolGuid, TPL_CALLBACK, @@ -455,9 +535,9 @@ VariableServiceInitialize ( // Register the event handling function to reclaim variable for OS usage. // Status = EfiCreateEventReadyToBootEx ( - TPL_NOTIFY, - OnReadyToBoot, - NULL, + TPL_NOTIFY, + OnReadyToBoot, + NULL, &ReadyToBootEvent ); ASSERT_EFI_ERROR (Status); @@ -467,7 +547,7 @@ VariableServiceInitialize ( // Status = gBS->CreateEventEx ( EVT_NOTIFY_SIGNAL, - TPL_NOTIFY, + TPL_CALLBACK, OnEndOfDxe, NULL, &gEfiEndOfDxeEventGroupGuid,