]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c
Fix the return value bug when updating public key database variable failure.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableDxe.c
index 7b88f15163b52eb27561d759f605466cb563de28..949fb64248bf99acdab98e8924e43ec0402122dc 100644 (file)
@@ -2,13 +2,14 @@
   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
-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
+Copyright (C) 2013, Red Hat, Inc.\r
+Copyright (c) 2009 - 2014, 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
+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
@@ -16,15 +17,18 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "Variable.h"\r
 #include "AuthService.h"\r
 \r
-extern VARIABLE_STORE_HEADER        *mNvVariableCache;\r
-extern VARIABLE_INFO_ENTRY          *gVariableInfo;\r
-EFI_HANDLE                          mHandle                    = NULL;\r
-EFI_EVENT                           mVirtualAddressChangeEvent = NULL;\r
-EFI_EVENT                           mFtwRegistration           = NULL;\r
+extern VARIABLE_STORE_HEADER   *mNvVariableCache;\r
+extern VARIABLE_INFO_ENTRY     *gVariableInfo;\r
+EFI_HANDLE                     mHandle                    = NULL;\r
+EFI_EVENT                      mVirtualAddressChangeEvent = NULL;\r
+EFI_EVENT                      mFtwRegistration           = NULL;\r
+extern LIST_ENTRY              mLockedVariableList;\r
+extern BOOLEAN                 mEndOfDxe;\r
+EDKII_VARIABLE_LOCK_PROTOCOL   mVariableLock              = { VariableLockRequestToLock };\r
 \r
 /**\r
   Return TRUE if ExitBootServices () has been called.\r
-  \r
+\r
   @retval TRUE If ExitBootServices () has been called.\r
 **/\r
 BOOLEAN\r
@@ -39,8 +43,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
@@ -131,7 +135,7 @@ GetFtwProtocol (
                   &gEfiFaultTolerantWriteProtocolGuid,\r
                   NULL,\r
                   FtwProtocol\r
-                  );                    \r
+                  );\r
   return Status;\r
 }\r
 \r
@@ -145,7 +149,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 +170,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 +181,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 +222,10 @@ VariableClassAddressChangeEvent (
   IN VOID                                 *Context\r
   )\r
 {\r
+  LIST_ENTRY     *Link;\r
+  VARIABLE_ENTRY *Entry;\r
+  EFI_STATUS     Status;\r
+\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
@@ -233,8 +241,25 @@ VariableClassAddressChangeEvent (
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
   EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
   EfiConvertPointer (0x0, (VOID **) &mHashCtx);\r
-  EfiConvertPointer (0x0, (VOID **) &mStorageArea);  \r
-  EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);  \r
+  EfiConvertPointer (0x0, (VOID **) &mSerializationRuntimeBuffer);\r
+  EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);\r
+\r
+  //\r
+  // in the list of locked variables, convert the name pointers first\r
+  //\r
+  for ( Link = GetFirstNode (&mLockedVariableList)\r
+      ; !IsNull (&mLockedVariableList, Link)\r
+      ; Link = GetNextNode (&mLockedVariableList, Link)\r
+      ) {\r
+    Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);\r
+    Status = EfiConvertPointer (0x0, (VOID **) &Entry->Name);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  //\r
+  // second, convert the list itself using UefiRuntimeLib\r
+  //\r
+  Status = EfiConvertList (0x0, &mLockedVariableList);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 \r
@@ -256,22 +281,44 @@ OnReadyToBoot (
   VOID                                    *Context\r
   )\r
 {\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
   ReclaimForOS ();\r
   if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
     gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
   }\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
+  mEndOfDxe = TRUE;\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
@@ -289,6 +336,7 @@ FtwNotificationEvent (
   UINT64                                  Length;\r
   EFI_PHYSICAL_ADDRESS                    VariableStoreBase;\r
   UINT64                                  VariableStoreLength;\r
+  UINTN                                   FtwMaxBlockSize;\r
 \r
   //\r
   // Ensure FTW protocol is installed.\r
@@ -297,7 +345,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
@@ -333,21 +386,23 @@ FtwNotificationEvent (
       DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
     }\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
   // 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
@@ -358,13 +413,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
+  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
@@ -376,22 +431,31 @@ 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
   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
@@ -399,7 +463,7 @@ VariableServiceInitialize (
 \r
   //\r
   // Register FtwNotificationEvent () notify function.\r
-  // \r
+  //\r
   EfiCreateProtocolNotifyEvent (\r
     &gEfiFaultTolerantWriteProtocolGuid,\r
     TPL_CALLBACK,\r
@@ -422,11 +486,25 @@ 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_NOTIFY,\r
+                  OnEndOfDxe,\r
+                  NULL,\r
+                  &gEfiEndOfDxeEventGroupGuid,\r
+                  &EndOfDxeEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   return EFI_SUCCESS;\r
 }\r