]> 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 0d91520d9722ce74286dbecf64f8b8acc2a29efd..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
@@ -1090,6 +1097,135 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
+EFI_STATUS\r
+EFIAPI\r
+QueryVariableInfo (\r
+  IN  UINT32                 Attributes,\r
+  OUT UINT64                 *MaximumVariableStorageSize,\r
+  OUT UINT64                 *RemainingVariableStorageSize,\r
+  OUT UINT64                 *MaximumVariableSize,\r
+  IN  VARIABLE_GLOBAL        *Global,\r
+  IN  UINT32                 Instance\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This code returns information about the EFI variables.\r
+\r
+Arguments:\r
+\r
+  Attributes                      Attributes bitmask to specify the type of variables \r
+                                  on which to return information.\r
+  MaximumVariableStorageSize      Pointer to the maximum size of the storage space available\r
+                                  for the EFI variables associated with the attributes specified.\r
+  RemainingVariableStorageSize    Pointer to the remaining size of the storage space available \r
+                                  for the EFI variables associated with the attributes specified.\r
+  MaximumVariableSize             Pointer to the maximum size of the individual EFI variables\r
+                                  associated with the attributes specified.\r
+  Global                          Pointer to VARIABLE_GLOBAL structure.\r
+  Instance                        Instance of the Firmware Volume.\r
+\r
+Returns:\r
+\r
+  EFI STATUS\r
+  EFI_INVALID_PARAMETER           - An invalid combination of attribute bits was supplied.\r
+  EFI_SUCCESS                     - Query successfully.\r
+  EFI_UNSUPPORTED                 - The attribute is not supported on this platform.\r
+\r
+--*/\r
+{\r
+  VARIABLE_HEADER        *Variable;\r
+  VARIABLE_HEADER        *NextVariable;\r
+  UINT64                 VariableSize;\r
+  VARIABLE_STORE_HEADER  *VariableStoreHeader;\r
+  \r
+  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)) == 0) {\r
+    //\r
+    // Make sure the Attributes combination is supported by the platform.\r
+    //\r
+    return EFI_UNSUPPORTED;\r
+  } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 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
+  } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
+    //\r
+    // Make sure RT Attribute is set if we are in Runtime phase.\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
+    //\r
+    // Query is Volatile related.\r
+    //\r
+    VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);    \r
+  } else {\r
+    //\r
+    // Query is Non-Volatile related.\r
+    //\r
+    VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
+  }\r
+\r
+  //\r
+  // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize \r
+  // with the storage size (excluding the storage header size).\r
+  //\r
+  *MaximumVariableStorageSize   = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
+  *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
+\r
+  //\r
+  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE.\r
+  //\r
+  *MaximumVariableSize = MAX_VARIABLE_SIZE;\r
+\r
+  //\r
+  // Point to the starting address of the variables.\r
+  //\r
+  Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
+\r
+  //\r
+  // Now walk through the related variable store.\r
+  //\r
+  while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r
+    NextVariable = GetNextVariablePtr (Variable);\r
+    VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
+\r
+    if (EfiAtRuntime ()) {\r
+      //\r
+      // we don't take the state of the variables in mind \r
+      // when calculating RemainingVariableStorageSize,\r
+      // since the space occupied by variables not marked with \r
+      // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
+      //\r
+      *RemainingVariableStorageSize -= VariableSize;\r
+    } else {\r
+      //\r
+      // Only care about Variables with State VAR_ADDED,because \r
+      // the space not marked as VAR_ADDED is reclaimable now.\r
+      //\r
+      if (Variable->State == VAR_ADDED) {\r
+        *RemainingVariableStorageSize -= VariableSize;\r
+      }\r
+    }\r
+    \r
+    //\r
+    // Go to the next one\r
+    //\r
+    Variable = NextVariable;\r
+  }\r
\r
+  return EFI_SUCCESS;\r
+}\r
+#endif\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 VariableCommonInitialize (\r
@@ -1125,14 +1261,14 @@ Returns:
   UINT32                          Instance;\r
   EFI_PHYSICAL_ADDRESS            FvVolHdr;\r
 \r
-  EFI_FLASH_MAP_ENTRY_DATA        *FlashMapEntryData;\r
+  UINT64                          TempVariableStoreHeader;\r
+\r
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
   EFI_FLASH_SUBAREA_ENTRY         VariableStoreEntry;\r
   UINT64                          BaseAddress;\r
   UINT64                          Length;\r
   UINTN                           Index;\r
   UINT8                           Data;\r
-  EFI_PEI_HOB_POINTERS            GuidHob;\r
 \r
   Status = gBS->AllocatePool (\r
                   EfiRuntimeServicesData,\r
@@ -1176,35 +1312,11 @@ Returns:
   // Get non volatile varaible store\r
   //\r
 \r
-  FlashMapEntryData = NULL;\r
-\r
-  GuidHob.Raw = GetHobList ();\r
-  while (NULL != (GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw))) {\r
-    FlashMapEntryData = (EFI_FLASH_MAP_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r
-\r
-    if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
-      break;\r
-    }\r
-    GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
-  }\r
-\r
-  if (NULL == GuidHob.Raw || FlashMapEntryData == NULL) {\r
-    gBS->FreePool (mVariableModuleGlobal);\r
-    gBS->FreePool (VolatileVariableStore);\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  //\r
-  // Currently only one non-volatile variable store is supported\r
-  //\r
-  if (FlashMapEntryData->NumEntries != 1) {\r
-    gBS->FreePool (mVariableModuleGlobal);\r
-    gBS->FreePool (VolatileVariableStore);\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
-  CopyMem (&VariableStoreEntry, &FlashMapEntryData->Entries[0], sizeof (VariableStoreEntry));\r
-\r
+  TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r
+  VariableStoreEntry.Base = TempVariableStoreHeader + \\r
+                              (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
+  VariableStoreEntry.Length = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r
+                                (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r
   //\r
   // Mark the variable storage region of the FLASH as RUNTIME\r
   //\r
@@ -1283,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