]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg Variable: Align TPL level for (Smm)EndOfDxe callback
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableDxe.c
index bde7b4486e08bcf3f24f1e51aa8f8a0d8d5cf3e0..6b04f4f7b39451cbaff368919f8a0c27b1d0f911 100644 (file)
@@ -1,31 +1,51 @@
 /** @file\r
-\r
   Implement all four UEFI Runtime Variable services for the nonvolatile\r
   and volatile storage space and install variable architecture protocol.\r
-  \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (C) 2013, Red Hat, Inc.\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
 #include "Variable.h"\r
 \r
-extern VARIABLE_STORE_HEADER   *mNvVariableCache;\r
-VARIABLE_INFO_ENTRY            *gVariableInfo;\r
-EFI_HANDLE                     mHandle                    = NULL;\r
-EFI_EVENT                      mVirtualAddressChangeEvent = NULL;\r
+extern VARIABLE_STORE_HEADER        *mNvVariableCache;\r
+extern EFI_FIRMWARE_VOLUME_HEADER   *mNvFvHeaderCache;\r
+extern VARIABLE_INFO_ENTRY          *gVariableInfo;\r
+EFI_HANDLE                          mHandle                    = NULL;\r
+EFI_EVENT                           mVirtualAddressChangeEvent = NULL;\r
 EFI_EVENT                           mFtwRegistration           = NULL;\r
+extern BOOLEAN                      mEndOfDxe;\r
+VOID                                ***mVarCheckAddressPointer = NULL;\r
+UINTN                               mVarCheckAddressPointerCount = 0;\r
+EDKII_VARIABLE_LOCK_PROTOCOL        mVariableLock              = { VariableLockRequestToLock };\r
+EDKII_VAR_CHECK_PROTOCOL            mVarCheck                  = { VarCheckRegisterSetVariableCheckHandler,\r
+                                                                    VarCheckVariablePropertySet,\r
+                                                                    VarCheckVariablePropertyGet };\r
 \r
 /**\r
-  Return TRUE if ExitBootServices () has been called\r
-  \r
-  @retval TRUE If ExitBootServices () has been called\r
+  Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).\r
+  Record their initial State when variable write service is ready.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+RecordSecureBootPolicyVarData(\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Return TRUE if ExitBootServices () has been called.\r
+\r
+  @retval TRUE If ExitBootServices () has been called.\r
 **/\r
 BOOLEAN\r
 AtRuntime (\r
@@ -39,8 +59,8 @@ AtRuntime (
 /**\r
   Initializes a basic mutual exclusion lock.\r
 \r
-  This function initializes a basic mutual exclusion lock to the released state \r
-  and returns the lock.  Each lock provides mutual exclusion access at its task \r
+  This function initializes a basic mutual exclusion lock to the released state\r
+  and returns the lock.  Each lock provides mutual exclusion access at its task\r
   priority level.  Since there is no preemption or multiprocessor support in EFI,\r
   acquiring the lock only consists of raising to the locks TPL.\r
   If Lock is NULL, then ASSERT().\r
@@ -108,7 +128,7 @@ ReleaseLockOnlyAtBootTime (
 }\r
 \r
 /**\r
-  Retrive the Fault Tolerent Write protocol interface.\r
+  Retrieve the Fault Tolerent Write protocol interface.\r
 \r
   @param[out] FtwProtocol       The interface of Ftw protocol\r
 \r
@@ -131,12 +151,12 @@ GetFtwProtocol (
                   &gEfiFaultTolerantWriteProtocolGuid,\r
                   NULL,\r
                   FtwProtocol\r
-                  );                    \r
+                  );\r
   return Status;\r
 }\r
 \r
 /**\r
-  Retrive the FVB protocol interface by HANDLE.\r
+  Retrieve the FVB protocol interface by HANDLE.\r
 \r
   @param[in]  FvBlockHandle     The handle of FVB protocol that provides services for\r
                                 reading, writing, and erasing the target block.\r
@@ -145,7 +165,7 @@ GetFtwProtocol (
   @retval EFI_SUCCESS           The interface information for the specified protocol was returned.\r
   @retval EFI_UNSUPPORTED       The device does not support the FVB protocol.\r
   @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 GetFvbByHandle (\r
@@ -166,7 +186,7 @@ GetFvbByHandle (
 \r
 /**\r
   Function returns an array of handles that support the FVB protocol\r
-  in a buffer allocated from pool. \r
+  in a buffer allocated from pool.\r
 \r
   @param[out]  NumberHandles    The number of handles returned in Buffer.\r
   @param[out]  Buffer           A pointer to the buffer to return the requested\r
@@ -177,7 +197,7 @@ GetFvbByHandle (
   @retval EFI_NOT_FOUND         No FVB handle was found.\r
   @retval EFI_OUT_OF_RESOURCES  There is not enough pool memory to store the matching results.\r
   @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 GetFvbCountAndBuffer (\r
@@ -218,6 +238,8 @@ VariableClassAddressChangeEvent (
   IN VOID                                 *Context\r
   )\r
 {\r
+  UINTN          Index;\r
+\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
@@ -231,8 +253,22 @@ VariableClassAddressChangeEvent (
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
+  EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
-  EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);  \r
+  EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);\r
+  EfiConvertPointer (0x0, (VOID **) &mNvFvHeaderCache);\r
+\r
+  if (mAuthContextOut.AddressPointer != NULL) {\r
+    for (Index = 0; Index < mAuthContextOut.AddressPointerCount; Index++) {\r
+      EfiConvertPointer (0x0, (VOID **) mAuthContextOut.AddressPointer[Index]);\r
+    }\r
+  }\r
+\r
+  if (mVarCheckAddressPointer != NULL) {\r
+    for (Index = 0; Index < mVarCheckAddressPointerCount; Index++) {\r
+      EfiConvertPointer (0x0, (VOID **) mVarCheckAddressPointer[Index]);\r
+    }\r
+  }\r
 }\r
 \r
 \r
@@ -254,19 +290,70 @@ OnReadyToBoot (
   VOID                                    *Context\r
   )\r
 {\r
+  if (!mEndOfDxe) {\r
+    MorLockInitAtEndOfDxe ();\r
+    //\r
+    // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.\r
+    //\r
+    mEndOfDxe = TRUE;\r
+    mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);\r
+    //\r
+    // The initialization for variable quota.\r
+    //\r
+    InitializeVariableQuota ();\r
+  }\r
   ReclaimForOS ();\r
+  if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
+    if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
+      gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
+    } else {\r
+      gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
+    }\r
+  }\r
+\r
+  gBS->CloseEvent (Event);\r
 }\r
 \r
+/**\r
+  Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
+\r
+  This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
+\r
+  @param  Event        Event whose notification function is being invoked.\r
+  @param  Context      Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+OnEndOfDxe (\r
+  EFI_EVENT                               Event,\r
+  VOID                                    *Context\r
+  )\r
+{\r
+  DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
+  MorLockInitAtEndOfDxe ();\r
+  mEndOfDxe = TRUE;\r
+  mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);\r
+  //\r
+  // The initialization for variable quota.\r
+  //\r
+  InitializeVariableQuota ();\r
+  if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
+    ReclaimForOS ();\r
+  }\r
+\r
+  gBS->CloseEvent (Event);\r
+}\r
 \r
 /**\r
   Fault Tolerant Write protocol notification event handler.\r
 \r
-  Non-Volatile variable write may needs FTW protocol to reclaim when \r
+  Non-Volatile variable write may needs FTW protocol to reclaim when\r
   writting variable.\r
 \r
   @param[in] Event    Event whose notification function is being invoked.\r
   @param[in] Context  Pointer to the notification function's context.\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -279,6 +366,12 @@ FtwNotificationEvent (
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FvbProtocol;\r
   EFI_FAULT_TOLERANT_WRITE_PROTOCOL       *FtwProtocol;\r
   EFI_PHYSICAL_ADDRESS                    NvStorageVariableBase;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR         GcdDescriptor;\r
+  EFI_PHYSICAL_ADDRESS                    BaseAddress;\r
+  UINT64                                  Length;\r
+  EFI_PHYSICAL_ADDRESS                    VariableStoreBase;\r
+  UINT64                                  VariableStoreLength;\r
+  UINTN                                   FtwMaxBlockSize;\r
 \r
   //\r
   // Ensure FTW protocol is installed.\r
@@ -287,7 +380,12 @@ FtwNotificationEvent (
   if (EFI_ERROR (Status)) {\r
     return ;\r
   }\r
-  \r
+\r
+  Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
+  }\r
+\r
   //\r
   // Find the proper FVB protocol for variable.\r
   //\r
@@ -300,21 +398,52 @@ FtwNotificationEvent (
     return ;\r
   }\r
   mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
-  \r
+\r
+  //\r
+  // Mark the variable storage region of the FLASH as RUNTIME.\r
+  //\r
+  VariableStoreBase   = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength);\r
+  VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
+  BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
+  Length      = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
+  Length      = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
+\r
+  Status      = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
+  } else {\r
+    Status = gDS->SetMemorySpaceAttributes (\r
+                    BaseAddress,\r
+                    Length,\r
+                    GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
+    }\r
+  }\r
+\r
   Status = VariableWriteServiceInitialize ();\r
-  ASSERT_EFI_ERROR (Status);\r
\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
+  }\r
+\r
+  //\r
+  // Some Secure Boot Policy Var (SecureBoot, etc) updates following other\r
+  // Secure Boot Policy Variable change. Record their initial value.\r
+  //\r
+  RecordSecureBootPolicyVarData();\r
+\r
   //\r
   // Install the Variable Write Architectural protocol.\r
   //\r
   Status = gBS->InstallProtocolInterface (\r
                   &mHandle,\r
-                  &gEfiVariableWriteArchProtocolGuid, \r
+                  &gEfiVariableWriteArchProtocolGuid,\r
                   EFI_NATIVE_INTERFACE,\r
                   NULL\r
                   );\r
   ASSERT_EFI_ERROR (Status);\r
-  \r
+\r
   //\r
   // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
   //\r
@@ -325,13 +454,13 @@ FtwNotificationEvent (
 \r
 /**\r
   Variable Driver main entry point. The Variable driver places the 4 EFI\r
-  runtime services in the EFI System Table and installs arch protocols \r
-  for variable read and write services being availible. It also registers\r
+  runtime services in the EFI System Table and installs arch protocols\r
+  for variable read and write services being available. It also registers\r
   a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
 \r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS       Variable service successfully initialized.\r
 \r
 **/\r
@@ -343,22 +472,39 @@ VariableServiceInitialize (
   )\r
 {\r
   EFI_STATUS                            Status;\r
-  EFI_EVENT                             ReadyToBootEvent;    \r
+  EFI_EVENT                             ReadyToBootEvent;\r
+  EFI_EVENT                             EndOfDxeEvent;\r
 \r
   Status = VariableCommonInitialize ();\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mHandle,\r
+                  &gEdkiiVariableLockProtocolGuid,\r
+                  &mVariableLock,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mHandle,\r
+                  &gEdkiiVarCheckProtocolGuid,\r
+                  &mVarCheck,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   SystemTable->RuntimeServices->GetVariable         = VariableServiceGetVariable;\r
   SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
   SystemTable->RuntimeServices->SetVariable         = VariableServiceSetVariable;\r
   SystemTable->RuntimeServices->QueryVariableInfo   = VariableServiceQueryVariableInfo;\r
-    \r
+\r
   //\r
   // Now install the Variable Runtime Architectural protocol on a new handle.\r
   //\r
   Status = gBS->InstallProtocolInterface (\r
                   &mHandle,\r
-                  &gEfiVariableArchProtocolGuid, \r
+                  &gEfiVariableArchProtocolGuid,\r
                   EFI_NATIVE_INTERFACE,\r
                   NULL\r
                   );\r
@@ -366,7 +512,7 @@ VariableServiceInitialize (
 \r
   //\r
   // Register FtwNotificationEvent () notify function.\r
-  // \r
+  //\r
   EfiCreateProtocolNotifyEvent (\r
     &gEfiFaultTolerantWriteProtocolGuid,\r
     TPL_CALLBACK,\r
@@ -389,15 +535,26 @@ VariableServiceInitialize (
   // Register the event handling function to reclaim variable for OS usage.\r
   //\r
   Status = EfiCreateEventReadyToBootEx (\r
-             TPL_NOTIFY, \r
-             OnReadyToBoot, \r
-             NULL, \r
+             TPL_NOTIFY,\r
+             OnReadyToBoot,\r
+             NULL,\r
              &ReadyToBootEvent\r
              );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register the event handling function to set the End Of DXE flag.\r
+  //\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  OnEndOfDxe,\r
+                  NULL,\r
+                  &gEfiEndOfDxeEventGroupGuid,\r
+                  &EndOfDxeEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-  if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
-    gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
-  }\r
   return EFI_SUCCESS;\r
 }\r
 \r