]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
Add pointer check for NULL before dereference it.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Variable.c
index 9a2468efb0802a63f9e1f7ab3d00f9aaddbe9e07..173693d9469248fd8f0a6fcb7f8a1dcb67f71416 100644 (file)
@@ -3,7 +3,7 @@
   The common variable operation routines shared by DXE_RINTIME variable \r
   module and DXE_SMM variable module.\r
   \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 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
@@ -199,6 +199,7 @@ UpdateVariableStore (
   // Check if the Data is Volatile.\r
   //\r
   if (!Volatile) {\r
+    ASSERT (Fvb != NULL);\r
     Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r
     ASSERT_EFI_ERROR (Status);\r
 \r
@@ -1363,17 +1364,19 @@ UpdateVariable (
   VARIABLE_STORE_HEADER               *VariableStoreHeader;\r
   UINTN                               CacheOffset;\r
 \r
-  if (CacheVariable->Volatile) {\r
+  if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r
+    //\r
+    // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r
+    // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r
+    //\r
+    return EFI_NOT_AVAILABLE_YET;     \r
+  }\r
+\r
+  if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r
     Variable = CacheVariable;\r
   } else {\r
-    if (mVariableModuleGlobal->FvbInstance == NULL) {\r
-      //\r
-      // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r
-      //\r
-      return EFI_NOT_AVAILABLE_YET;\r
-    }\r
-    \r
     //\r
+    // Update/Delete existing NV variable.\r
     // CacheVariable points to the variable in the memory copy of Flash area\r
     // Now let Variable points to the same variable in Flash area.\r
     //\r
@@ -1381,16 +1384,12 @@ UpdateVariable (
     Variable = &NvVariable;    \r
     Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r
     Variable->EndPtr   = GetEndPointer (VariableStoreHeader);\r
-    if (CacheVariable->CurrPtr == NULL) {\r
-      Variable->CurrPtr = NULL;\r
-    } else {\r
-      Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
-    }\r
-    Variable->Volatile  = FALSE;\r
-  }\r
-  \r
-  Fvb               = mVariableModuleGlobal->FvbInstance;\r
-  Reclaimed         = FALSE;\r
+    Variable->CurrPtr  = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r
+    Variable->Volatile = FALSE;\r
+  } \r
+\r
+  Fvb       = mVariableModuleGlobal->FvbInstance;\r
+  Reclaimed = FALSE;\r
 \r
   if (Variable->CurrPtr != NULL) {\r
     //\r
@@ -2165,7 +2164,7 @@ VariableServiceQueryVariableInfo (
       // since the space occupied by variables not marked with\r
       // VAR_ADDED is not allowed to be reclaimed in Runtime.\r
       //\r
-      if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+      if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
         HwErrVariableTotalSize += VariableSize;\r
       } else {\r
         CommonVariableTotalSize += VariableSize;\r
@@ -2176,7 +2175,7 @@ VariableServiceQueryVariableInfo (
       // the space not marked as VAR_ADDED is reclaimable now.\r
       //\r
       if (Variable->State == VAR_ADDED) {\r
-        if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+        if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
           HwErrVariableTotalSize += VariableSize;\r
         } else {\r
           CommonVariableTotalSize += VariableSize;\r
@@ -2261,9 +2260,6 @@ VariableWriteServiceInitialize (
   VARIABLE_STORE_HEADER           *VariableStoreHeader;\r
   UINTN                           Index;\r
   UINT8                           Data;\r
-  EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
-  EFI_PHYSICAL_ADDRESS            BaseAddress;\r
-  UINT64                          Length;\r
   EFI_PHYSICAL_ADDRESS            VariableStoreBase;\r
   UINT64                          VariableStoreLength;\r
 \r
@@ -2293,26 +2289,6 @@ VariableWriteServiceInitialize (
     }\r
   }\r
 \r
-  //\r
-  // Mark the variable storage region of the FLASH as RUNTIME.\r
-  //\r
-  BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
-  Length      = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
-  Length      = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
-\r
-  Status      = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
-  } else {\r
-    Status = gDS->SetMemorySpaceAttributes (\r
-                    BaseAddress,\r
-                    Length,\r
-                    GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
-    }\r
-  }\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -2384,7 +2360,7 @@ VariableCommonInitialize (
   VolatileVariableStore->Reserved1   = 0;\r
 \r
   //\r
-  // Get non-volatile varaible store.\r
+  // Get non-volatile variable store.\r
   //\r
 \r
   TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r