]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Partially make EdkModulePkg pass intel IPF compiler with /W4 /WX switched on.
[mirror_edk2.git] / EdkModulePkg / Universal / Variable / RuntimeDxe / Variable.c
index cfd3183f0930870fcc4d9778e2769ab6d71a4caa..aa0742364e9f8a0e83a875ec6378c793e064c8d5 100644 (file)
@@ -27,6 +27,7 @@ Revision History
 //\r
 ESAL_VARIABLE_GLOBAL  *mVariableModuleGlobal;\r
 \r
+STATIC\r
 UINT32\r
 EFIAPI\r
 ArrayLength (\r
@@ -63,6 +64,7 @@ Returns:
   return (Count * 2) + 2;\r
 }\r
 \r
+STATIC\r
 BOOLEAN\r
 EFIAPI\r
 IsValidVariableHeader (\r
@@ -93,6 +95,7 @@ Returns:
   return TRUE;\r
 }\r
 \r
+STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 UpdateVariableStore (\r
@@ -242,6 +245,7 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+STATIC\r
 VARIABLE_STORE_STATUS\r
 EFIAPI\r
 GetVariableStoreStatus (\r
@@ -283,6 +287,7 @@ Returns:
   }\r
 }\r
 \r
+STATIC\r
 UINT8 *\r
 EFIAPI\r
 GetVariableDataPtr (\r
@@ -310,6 +315,7 @@ Returns:
   return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
 }\r
 \r
+STATIC\r
 VARIABLE_HEADER *\r
 EFIAPI\r
 GetNextVariablePtr (\r
@@ -340,6 +346,7 @@ Returns:
   return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
 }\r
 \r
+STATIC\r
 VARIABLE_HEADER *\r
 EFIAPI\r
 GetEndPointer (\r
@@ -367,6 +374,7 @@ Returns:
   return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r
 }\r
 \r
+STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 Reclaim (\r
@@ -487,6 +495,7 @@ Returns:
   return Status;\r
 }\r
 \r
+STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 FindVariable (\r
@@ -707,7 +716,7 @@ 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
-        return EFI_NOT_FOUND;\r
+        goto Error;\r
       }\r
 \r
       Variable.CurrPtr = Variable.StartPtr;\r
@@ -745,6 +754,7 @@ Returns:
     Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
   }\r
 \r
+Error:\r
   return EFI_NOT_FOUND;\r
 }\r
 \r
@@ -810,38 +820,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
@@ -1254,14 +1271,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
@@ -1305,35 +1322,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
@@ -1412,6 +1405,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