]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/EmuVariable/RuntimeDxe/EmuVariable.c
Add a lock to protect the critical region in Service APIs for UEFI Runtime Variable...
[mirror_edk2.git] / EdkModulePkg / Universal / EmuVariable / RuntimeDxe / EmuVariable.c
index 208f3b427682c4bac482867044f144398c064bf9..d39ecd2ce07bac010d13938e3430ebad911acf12 100644 (file)
@@ -1,6 +1,6 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
 All rights reserved. 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
@@ -26,6 +26,37 @@ Revision History
 //\r
 ESAL_VARIABLE_GLOBAL  *mVariableModuleGlobal;\r
 \r
+//\r
+// This is a temperary function which will be removed \r
+// when EfiAcquireLock in UefiLib can handle the\r
+// the call in UEFI Runtimer driver in RT phase.\r
+//\r
+VOID\r
+AcquireLockOnlyAtBootTime (\r
+  IN EFI_LOCK  *Lock\r
+  )\r
+{\r
+  if (!EfiAtRuntime ()) {\r
+    EfiAcquireLock (Lock);\r
+  }\r
+}\r
+\r
+//\r
+// This is a temperary function which will be removed \r
+// when EfiAcquireLock in UefiLib can handle the\r
+// the call in UEFI Runtimer driver in RT phase.\r
+//\r
+VOID\r
+ReleaseLockOnlyAtBootTime (\r
+  IN EFI_LOCK  *Lock\r
+  )\r
+{\r
+  if (!EfiAtRuntime ()) {\r
+    EfiReleaseLock (Lock);\r
+  }\r
+}\r
+\r
+\r
 STATIC\r
 UINT32\r
 EFIAPI\r
@@ -197,6 +228,13 @@ Returns:
   VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
   UINTN                 Index;\r
 \r
+  //\r
+  // We aquire the lock at the entry of FindVariable as GetVariable, GetNextVariableName\r
+  // SetVariable all call FindVariable at entry point. Please move "Aquire Lock" to\r
+  // the correct places if this assumption does not hold TRUE anymore.\r
+  //\r
+  AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
+\r
   //\r
   // 0: Non-Volatile, 1: Volatile\r
   //\r
@@ -293,7 +331,7 @@ Returns:
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
   if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Done;\r
   }\r
   //\r
   // Get data size\r
@@ -301,7 +339,8 @@ Returns:
   VarDataSize = Variable.CurrPtr->DataSize;\r
   if (*DataSize >= VarDataSize) {\r
     if (Data == NULL) {\r
-      return EFI_INVALID_PARAMETER;\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
     }\r
 \r
     CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
@@ -310,11 +349,17 @@ Returns:
     }\r
 \r
     *DataSize = VarDataSize;\r
-    return EFI_SUCCESS;\r
+    Status = EFI_SUCCESS;\r
+    goto Done;\r
   } else {\r
     *DataSize = VarDataSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
+    Status = EFI_BUFFER_TOO_SMALL;\r
+    goto Done;\r
   }\r
+\r
+Done:\r
+  ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
+  return Status;\r
 }\r
 \r
 EFI_STATUS\r
@@ -357,7 +402,7 @@ Returns:
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
   if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   while (TRUE) {\r
@@ -377,7 +422,8 @@ Returns:
         Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
         Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
       } else {\r
-        goto Error;\r
+        Status = EFI_NOT_FOUND;\r
+        goto Done;\r
       }\r
 \r
       Variable.CurrPtr = Variable.StartPtr;\r
@@ -408,13 +454,15 @@ Returns:
         }\r
 \r
         *VariableNameSize = VarNameSize;\r
-        return Status;\r
+        goto Done;\r
       }\r
     }\r
   }\r
 \r
-Error:\r
-  return EFI_NOT_FOUND;\r
+Done:\r
+  ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
+  return Status;\r
\r
 }\r
 \r
 EFI_STATUS\r
@@ -470,36 +518,41 @@ Returns:
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
   if (Status == EFI_INVALID_PARAMETER) {\r
-    return Status;\r
+    goto Done;\r
   } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) {\r
     //\r
     // If EfiAtRuntime and the variable is Volatile and Runtime Access,  \r
     // the volatile is ReadOnly, and SetVariable should be aborted and \r
     // return EFI_WRITE_PROTECTED.\r
     //\r
-    return EFI_WRITE_PROTECTED;\r
+    Status = EFI_WRITE_PROTECTED;\r
+    goto Done;\r
   } else if (sizeof (VARIABLE_HEADER) + (ArrayLength (VariableName) + DataSize) > MAX_VARIABLE_SIZE) {\r
     //\r
     //  The size of the VariableName, including the Unicode Null in bytes plus\r
     //  the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r
     //\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS\r
           ) {\r
     //\r
     //  Make sure if runtime bit is set, boot service bit is set also\r
     //\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
     //\r
     // Runtime but Attribute is not Runtime\r
     //\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
     //\r
     // Cannot set volatile variable in Runtime\r
     //\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
     //\r
     // Setting a data variable with no access, or zero DataSize attributes\r
@@ -507,10 +560,12 @@ Returns:
     //\r
     if (!EFI_ERROR (Status)) {\r
       Variable.CurrPtr->State &= VAR_DELETED;\r
-      return EFI_SUCCESS;\r
+      Status = EFI_SUCCESS;\r
+      goto Done;\r
     }\r
 \r
-    return EFI_NOT_FOUND;\r
+    Status = EFI_NOT_FOUND;\r
+    goto Done;\r
   } else {\r
     if (!EFI_ERROR (Status)) {\r
       //\r
@@ -520,7 +575,8 @@ Returns:
       if (Variable.CurrPtr->DataSize == DataSize &&\r
           !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
             ) {\r
-        return EFI_SUCCESS;\r
+        Status = EFI_SUCCESS;\r
+        goto Done;\r
       } else if (Variable.CurrPtr->State == VAR_ADDED) {\r
         //\r
         // Mark the old variable as in delete transition\r
@@ -540,20 +596,23 @@ Returns:
       if ((UINT32) (VarSize +*NonVolatileOffset) >\r
             ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
             ) {\r
-        return EFI_OUT_OF_RESOURCES;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
       }\r
 \r
       NextVariable        = (VARIABLE_HEADER *) (UINT8 *) (*NonVolatileOffset + (UINTN) Global->NonVolatileVariableBase);\r
       *NonVolatileOffset  = *NonVolatileOffset + VarSize;\r
     } else {\r
       if (EfiAtRuntime ()) {\r
-        return EFI_INVALID_PARAMETER;\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto Done;\r
       }\r
 \r
       if ((UINT32) (VarSize +*VolatileOffset) >\r
             ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
             ) {\r
-        return EFI_OUT_OF_RESOURCES;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
       }\r
 \r
       NextVariable    = (VARIABLE_HEADER *) (UINT8 *) (*VolatileOffset + (UINTN) Global->VolatileVariableBase);\r
@@ -593,7 +652,10 @@ Returns:
     }\r
   }\r
 \r
-  return EFI_SUCCESS;\r
+  Status = EFI_SUCCESS;\r
+Done:\r
+  ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
+  return Status;\r
 }\r
 \r
 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
@@ -666,6 +728,8 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
+  \r
   if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
     //\r
     // Query is Volatile related.\r
@@ -716,6 +780,7 @@ Returns:
     Variable = NextVariable;\r
   }\r
 \r
+  ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
   return EFI_SUCCESS;\r
 }\r
 #endif\r
@@ -796,11 +861,14 @@ Returns:
   if (NULL == mVariableModuleGlobal) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
+\r
+  EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, EFI_TPL_CALLBACK);\r
+  \r
   //\r
   // Intialize volatile variable store\r
   //\r
   Status = InitializeVariableStore (\r
-            &mVariableModuleGlobal->VariableBase[Physical].VolatileVariableBase,\r
+            &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase,\r
             &mVariableModuleGlobal->VolatileLastVariableOffset\r
             );\r
 \r
@@ -811,7 +879,7 @@ Returns:
   // Intialize non volatile variable store\r
   //\r
   Status = InitializeVariableStore (\r
-            &mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase,\r
+            &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,\r
             &mVariableModuleGlobal->NonVolatileLastVariableOffset\r
             );\r
 \r