]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c
SecurityPkg Variable: Introduce PcdReclaimVariableSpaceAtEndOfDxe
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableDxe.c
index a2e764cf610c5d4046e6c947fc2e2d46006fdd69..05a90fa8fcd1d91ac0ebf8f9f38552aff0c87f82 100644 (file)
@@ -2,7 +2,8 @@
   Implement all four UEFI Runtime Variable services for the nonvolatile\r
   and volatile storage space and install variable architecture protocol.\r
 \r
-Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (C) 2013, Red Hat, Inc.\r
+Copyright (c) 2009 - 2015, 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
@@ -21,8 +22,15 @@ extern VARIABLE_INFO_ENTRY     *gVariableInfo;
 EFI_HANDLE                     mHandle                    = NULL;\r
 EFI_EVENT                      mVirtualAddressChangeEvent = NULL;\r
 EFI_EVENT                      mFtwRegistration           = NULL;\r
+extern LIST_ENTRY              mLockedVariableList;\r
+extern LIST_ENTRY              mVarCheckVariableList;\r
+extern UINT32                  mNumberOfHandler;\r
+extern VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *mHandlerTable;\r
 extern BOOLEAN                 mEndOfDxe;\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
@@ -220,6 +228,9 @@ VariableClassAddressChangeEvent (
   IN VOID                                 *Context\r
   )\r
 {\r
+  EFI_STATUS     Status;\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
@@ -233,11 +244,23 @@ 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 **) &mHashCtx);\r
-  EfiConvertPointer (0x0, (VOID **) &mStorageArea);\r
   EfiConvertPointer (0x0, (VOID **) &mSerializationRuntimeBuffer);\r
   EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);\r
+  EfiConvertPointer (0x0, (VOID **) &mPubKeyStore);\r
+  EfiConvertPointer (0x0, (VOID **) &mCertDbStore);\r
+  EfiConvertPointer (0x0, (VOID **) &mHandlerTable);\r
+  for (Index = 0; Index < mNumberOfHandler; Index++) {\r
+    EfiConvertPointer (0x0, (VOID **) &mHandlerTable[Index]);\r
+  }\r
+\r
+  Status = EfiConvertList (0x0, &mLockedVariableList);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = EfiConvertList (0x0, &mVarCheckVariableList);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 \r
@@ -263,6 +286,10 @@ OnReadyToBoot (
   // 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
+  //\r
+  // The initialization for variable quota.\r
+  //\r
+  InitializeVariableQuota ();\r
   ReclaimForOS ();\r
   if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
     gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
@@ -286,6 +313,13 @@ OnEndOfDxe (
   )\r
 {\r
   mEndOfDxe = TRUE;\r
+  //\r
+  // The initialization for variable quota.\r
+  //\r
+  InitializeVariableQuota ();\r
+  if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
+    ReclaimForOS ();\r
+  }\r
 }\r
 \r
 /**\r
@@ -314,6 +348,7 @@ FtwNotificationEvent (
   UINT64                                  Length;\r
   EFI_PHYSICAL_ADDRESS                    VariableStoreBase;\r
   UINT64                                  VariableStoreLength;\r
+  UINTN                                   FtwMaxBlockSize;\r
 \r
   //\r
   // Ensure FTW protocol is installed.\r
@@ -323,6 +358,11 @@ FtwNotificationEvent (
     return ;\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
@@ -339,7 +379,7 @@ FtwNotificationEvent (
   //\r
   // Mark the variable storage region of the FLASH as RUNTIME.\r
   //\r
-  VariableStoreBase   = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\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
@@ -347,7 +387,7 @@ FtwNotificationEvent (
 \r
   Status      = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
+    DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
   } else {\r
     Status = gDS->SetMemorySpaceAttributes (\r
                     BaseAddress,\r
@@ -360,7 +400,9 @@ FtwNotificationEvent (
   }\r
 \r
   Status = VariableWriteServiceInitialize ();\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
+  }\r
 \r
   //\r
   // Install the Variable Write Architectural protocol.\r
@@ -415,6 +457,14 @@ VariableServiceInitialize (
                   );\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