]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Volatile variable is read-only in runtime.
[mirror_edk2.git] / EdkModulePkg / Universal / Variable / RuntimeDxe / Variable.c
index 3fa16172cc765db9e96d1dce794fc6bc9def088c..f59d2a396981102b7fecde6faad535e41e028c2a 100644 (file)
@@ -810,38 +810,45 @@ Returns:
 \r
   if (Status == EFI_INVALID_PARAMETER) {\r
     return Status;\r
-  }\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
-  else if (sizeof (VARIABLE_HEADER) + ArrayLength (VariableName) + DataSize > MAX_VARIABLE_SIZE) {\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
+  } 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
-  }\r
-  //\r
-  //  Make sure if runtime bit is set, boot service bit is set also\r
-  //\r
-  else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS\r
-          ) {\r
+  } else if (Attributes == EFI_VARIABLE_NON_VOLATILE) {\r
+    //\r
+    //  Make sure not only EFI_VARIABLE_NON_VOLATILE is set \r
+    //\r
     return EFI_INVALID_PARAMETER;\r
-  }\r
-  //\r
-  // Runtime but Attribute is not Runtime\r
-  //\r
-  else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
+  } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == \r
+                EFI_VARIABLE_RUNTIME_ACCESS) {\r
+    //\r
+    //  Make sure if runtime bit is set, boot service bit is set also\r
+    //\r
     return EFI_INVALID_PARAMETER;\r
-  }\r
-  //\r
-  // Cannot set volatile variable in Runtime\r
-  //\r
-  else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {\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
-  }\r
-  //\r
-  // Setting a data variable with no access, or zero DataSize attributes\r
-  // specified causes it to be deleted.\r
-  //\r
-  else if (DataSize == 0 || Attributes == 0) {\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
+  } 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
+    // specified causes it to be deleted.\r
+    //\r
     if (!EFI_ERROR (Status)) {\r
       State = Variable.CurrPtr->State;\r
       State &= VAR_DELETED;\r
@@ -1310,8 +1317,6 @@ Returns:
                               (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
   VariableStoreEntry.Length = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
                                 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
-\r
-  VariableStoreEntry.Length = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize);\r
   //\r
   // Mark the variable storage region of the FLASH as RUNTIME\r
   //\r
@@ -1390,6 +1395,23 @@ Returns:
 \r
     mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r
 \r
+    //\r
+    // Check if the free area is blow a threshold\r
+    //\r
+    if ((((VARIABLE_STORE_HEADER *)((UINTN) CurrPtr))->Size - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r
+      Status = Reclaim (\r
+                mVariableModuleGlobal->VariableBase[Physical].NonVolatileVariableBase,\r
+                &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r
+                FALSE\r
+                );\r
+    }\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePool (mVariableModuleGlobal);\r
+      gBS->FreePool (VolatileVariableStore);\r
+      return Status;\r
+    }\r
+\r
     //\r
     // Check if the free area is really free.\r
     //\r