]> git.proxmox.com Git - mirror_edk2.git/commitdiff
1. Uefi2.1 feature - Add Hardware Error Record Persistence Support
authorqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 17 Sep 2007 03:50:40 +0000 (03:50 +0000)
committerqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 17 Sep 2007 03:50:40 +0000 (03:50 +0000)
2. Fix the return status accoring to approved UEFI ECRs.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@3866 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/ChangeLog.txt
MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c

index 35971b17fe0a75585df9fd1e057307450e4789df..75bd4563d08fafc067cd9987c504f168695d5314 100644 (file)
@@ -59,3 +59,12 @@ EDK_3773:  Non-Compatible: qwang12
                3) The gEfiFirmwareVolumeProtocolGuid in [Depex] section of INF file should updated.\r
                   And the package dependency should also be changed if needed due to this protocol\r
                   GUID change.\r
+\r
+\r
+==========================================================================================\r
+EDK_3865:  Compatible: qhuang8\r
+\r
+         Class_UefiEnable[0]:  Uefi2.1 feature - Add Hardware Error Record Persistence Support \r
+               Code Change :\r
+               1) Modify MdeModulePkg/Universal/Variable/RuntimeDxe\r
+                2) Modify MdeModulePkg/Universal/Variable/EmuRuntimeDxe\r
index f56cf04215501fed3401edf07bca331a49798ec6..2c5dd365db20961cc89cab300f0d38eb4388e660 100644 (file)
@@ -60,7 +60,6 @@ ReleaseLockOnlyAtBootTime (
 \r
 STATIC\r
 UINT8 *\r
-EFIAPI\r
 GetVariableDataPtr (\r
   IN  VARIABLE_HEADER   *Variable\r
   )\r
@@ -91,7 +90,6 @@ Returns:
 \r
 STATIC\r
 VARIABLE_HEADER *\r
-EFIAPI\r
 GetNextVariablePtr (\r
   IN  VARIABLE_HEADER   *Variable\r
   )\r
@@ -132,7 +130,6 @@ Returns:
 \r
 STATIC\r
 VARIABLE_HEADER *\r
-EFIAPI\r
 GetEndPointer (\r
   IN VARIABLE_STORE_HEADER       *VolHeader\r
   )\r
@@ -160,7 +157,6 @@ Returns:
 \r
 STATIC\r
 EFI_STATUS\r
-EFIAPI\r
 FindVariable (\r
   IN  CHAR16                  *VariableName,\r
   IN  EFI_GUID                *VendorGuid,\r
@@ -278,7 +274,11 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI STATUS\r
+  EFI_INVALID_PARAMETER       - Invalid parameter\r
+  EFI_SUCCESS                 - Find the specified variable\r
+  EFI_NOT_FOUND               - Not found\r
+  EFI_BUFFER_TO_SMALL         - DataSize is too small for the result\r
+\r
 \r
 --*/\r
 {\r
@@ -463,7 +463,12 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI STATUS\r
+  EFI_INVALID_PARAMETER           - Invalid parameter\r
+  EFI_SUCCESS                     - Set successfully\r
+  EFI_OUT_OF_RESOURCES            - Resource not enough to set variable\r
+  EFI_NOT_FOUND                   - Not found\r
+  EFI_DEVICE_ERROR                - Variable can not be saved due to hardware failure\r
+  EFI_WRITE_PROTECTED             - Variable is read-only\r
 \r
 --*/\r
 {\r
@@ -475,147 +480,188 @@ Returns:
   UINTN                   VarDataOffset;\r
   UINTN                   VarSize;\r
 \r
+  //\r
+  // Check input parameters\r
+  //\r
   if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
+  }  \r
+  //\r
+  //  Make sure if runtime bit is set, boot service bit is set also\r
+  //\r
+  if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\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_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r
+  //  bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r
+  //\r
+  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+    if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||                                                       \r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }    \r
+  } else {\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
+    if ((DataSize > MAX_VARIABLE_SIZE) ||\r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }  \r
+  }  \r
+  //\r
+  // Check whether the input variable is already existed\r
+  //\r
+  \r
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
-  if (Status == EFI_INVALID_PARAMETER) {\r
-    goto Done;\r
-  } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) {\r
+  if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\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
-    Status = EFI_WRITE_PROTECTED;\r
-    goto Done;\r
-  } else if (sizeof (VARIABLE_HEADER) + (StrSize (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
-    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
+    // Update/Delete existing variable\r
     //\r
-    //  Make sure if runtime bit is set, boot service bit is set also\r
-    //\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
-    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
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
-  } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
+\r
+    if (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
+      if (Variable.Volatile) {\r
+        Status = EFI_WRITE_PROTECTED;\r
+        goto Done;\r
+      }\r
+      //\r
+      // Only variable have NV attribute can be updated/deleted in Runtime\r
+      //\r
+      if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto Done;\r
+      }\r
+    }\r
+\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
+    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
       Variable.CurrPtr->State &= VAR_DELETED;\r
       Status = EFI_SUCCESS;\r
       goto Done;\r
     }\r
 \r
-    Status = EFI_NOT_FOUND;\r
-    goto Done;\r
-  } else {\r
-    if (!EFI_ERROR (Status)) {\r
+    //\r
+    // If the variable is marked valid and the same data has been passed in\r
+    // then return to the caller immediately.\r
+    //\r
+    if (Variable.CurrPtr->DataSize == DataSize &&\r
+        !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
+          ) {\r
+      Status = EFI_SUCCESS;\r
+      goto Done;\r
+    } else if (Variable.CurrPtr->State == VAR_ADDED) {\r
       //\r
-      // If the variable is marked valid and the same data has been passed in\r
-      // then return to the caller immediately.\r
+      // Mark the old variable as in delete transition\r
       //\r
-      if (Variable.CurrPtr->DataSize == DataSize &&\r
-          !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
-            ) {\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
-        //\r
-        Variable.CurrPtr->State &= VAR_IN_DELETED_TRANSITION;\r
-      }\r
+      Variable.CurrPtr->State &= VAR_IN_DELETED_TRANSITION;\r
     }\r
+    \r
+  } else if (Status == EFI_NOT_FOUND) {\r
     //\r
-    // Create a new variable and copy the data.\r
+    // Create a new variable\r
+    //  \r
+    \r
     //\r
-    VarNameOffset = sizeof (VARIABLE_HEADER);\r
-    VarNameSize   = StrSize (VariableName);\r
-    VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
-    VarSize       = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
-\r
-    if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
-      if ((UINT32) (VarSize +*NonVolatileOffset) >\r
-            ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
-            ) {\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
-        Status = EFI_INVALID_PARAMETER;\r
-        goto Done;\r
-      }\r
-\r
-      if ((UINT32) (VarSize +*VolatileOffset) >\r
-            ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
-            ) {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-        goto Done;\r
-      }\r
-\r
-      NextVariable    = (VARIABLE_HEADER *) (UINT8 *) (*VolatileOffset + (UINTN) Global->VolatileVariableBase);\r
-      *VolatileOffset = *VolatileOffset + VarSize;\r
+    // Make sure we are trying to create a new variable.\r
+    // Setting a data variable with no access, or zero DataSize attributes means to delete it.    \r
+    //\r
+    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
+      Status = EFI_NOT_FOUND;\r
+      goto Done;\r
     }\r
-\r
-    NextVariable->StartId     = VARIABLE_DATA;\r
-    NextVariable->Attributes  = Attributes;\r
-    NextVariable->State       = VAR_ADDED;\r
-    NextVariable->Reserved    = 0;\r
-\r
+        \r
     //\r
-    // There will be pad bytes after Data, the NextVariable->NameSize and\r
-    // NextVariable->NameSize should not include pad size so that variable\r
-    // service can get actual size in GetVariable\r
+    // Only variable have NV|RT attribute can be created in Runtime\r
     //\r
-    NextVariable->NameSize  = (UINT32)VarNameSize;\r
-    NextVariable->DataSize  = (UINT32)DataSize;\r
-\r
-    CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
-    CopyMem (\r
-      (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
-      VariableName,\r
-      VarNameSize\r
-      );\r
-    CopyMem (\r
-      (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
-      Data,\r
-      DataSize\r
-      );\r
-\r
+    if (EfiAtRuntime () &&\r
+        (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
+    }         \r
+  } else {\r
     //\r
-    // Mark the old variable as deleted\r
+    // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r
     //\r
-    if (!EFI_ERROR (Status)) {\r
-      Variable.CurrPtr->State &= VAR_DELETED;\r
+    ASSERT (Status == EFI_INVALID_PARAMETER);\r
+    goto Done;\r
+  } \r
+  \r
+  //\r
+  // Function part - create a new variable and copy the data.\r
+  // Both update a variable and create a variable will come here.\r
+  //\r
+  \r
+  VarNameOffset = sizeof (VARIABLE_HEADER);\r
+  VarNameSize   = StrSize (VariableName);\r
+  VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
+  VarSize       = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
+\r
+  if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
+    if ((UINT32) (VarSize +*NonVolatileOffset) >\r
+          ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
+          ) {\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 ((UINT32) (VarSize +*VolatileOffset) >\r
+          ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
+          ) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Done;\r
+    }\r
+\r
+    NextVariable    = (VARIABLE_HEADER *) (UINT8 *) (*VolatileOffset + (UINTN) Global->VolatileVariableBase);\r
+    *VolatileOffset = *VolatileOffset + VarSize;\r
   }\r
 \r
+  NextVariable->StartId     = VARIABLE_DATA;\r
+  NextVariable->Attributes  = Attributes;\r
+  NextVariable->State       = VAR_ADDED;\r
+  NextVariable->Reserved    = 0;\r
+\r
+  //\r
+  // There will be pad bytes after Data, the NextVariable->NameSize and\r
+  // NextVariable->NameSize should not include pad size so that variable\r
+  // service can get actual size in GetVariable\r
+  //\r
+  NextVariable->NameSize  = (UINT32)VarNameSize;\r
+  NextVariable->DataSize  = (UINT32)DataSize;\r
+\r
+  CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
+  CopyMem (\r
+    (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
+    VariableName,\r
+    VarNameSize\r
+    );\r
+  CopyMem (\r
+    (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
+    Data,\r
+    DataSize\r
+    );\r
+\r
+  //\r
+  // Mark the old variable as deleted\r
+  //\r
+  if (!EFI_ERROR (Status)) {\r
+    Variable.CurrPtr->State &= VAR_DELETED;\r
+  }\r
+  \r
   Status = EFI_SUCCESS;\r
 Done:\r
   ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
@@ -645,8 +691,8 @@ Arguments:
   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
+                                  for EFI variables associated with the attributes specified.\r
+  MaximumVariableSize             Pointer to the maximum size of an individual EFI variables\r
                                   associated with the attributes specified.\r
   Global                          Pointer to VARIABLE_GLOBAL structure.\r
   Instance                        Instance of the Firmware Volume.\r
@@ -665,15 +711,15 @@ Returns:
   UINT64                 VariableSize;\r
   VARIABLE_STORE_HEADER  *VariableStoreHeader;\r
 \r
-  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) {\r
+  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\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
+  if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
     //\r
     // Make sure the Attributes combination is supported by the platform.\r
     //\r
-    return EFI_UNSUPPORTED;\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
@@ -713,9 +759,16 @@ Returns:
   *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
 \r
   //\r
-  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE\r
+  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r
   //\r
-  *MaximumVariableSize = MAX_VARIABLE_SIZE;\r
+  *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+\r
+  //\r
+  // Harware error record variable needs larger size.\r
+  //\r
+  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+    *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+  }\r
 \r
   //\r
   // Point to the starting address of the variables.\r
@@ -743,13 +796,18 @@ Returns:
     Variable = NextVariable;\r
   }\r
 \r
+  if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
+    *MaximumVariableSize = 0;\r
+  } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
+    *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
+  }\r
+  \r
   ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
   return EFI_SUCCESS;\r
 }\r
 \r
 STATIC\r
 EFI_STATUS\r
-EFIAPI\r
 InitializeVariableStore (\r
   OUT EFI_PHYSICAL_ADDRESS  *VariableBase,\r
   OUT UINTN                 *LastVariableOffset\r
index 8f546bddb3e36f842035f4725bfcbe3ebd242272..ef8f72d38a777f1e914f0fdbe53fd524a49b5efe 100644 (file)
@@ -112,19 +112,20 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  Global            Pointer to VARAIBLE_GLOBAL structure\r
-  Volatile          If the Variable is Volatile or Non-Volatile\r
-  SetByIndex        TRUE: Target pointer is given as index\r
-                    FALSE: Target pointer is absolute\r
-  Instance          Instance of FV Block services\r
-  DataPtrIndex      Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
-                    structure\r
-  DataSize          Size of data to be written.\r
-  Buffer            Pointer to the buffer from which data is written\r
+  Global            Pointer to VARAIBLE_GLOBAL structure\r
+  Volatile          If the Variable is Volatile or Non-Volatile\r
+  SetByIndex        TRUE: Target pointer is given as index\r
+                      FALSE: Target pointer is absolute\r
+  Instance          Instance of FV Block services\r
+  DataPtrIndex      Pointer to the Data from the end of VARIABLE_STORE_HEADER\r
+                      structure\r
+  DataSize          Size of data to be written.\r
+  Buffer            Pointer to the buffer from which data is written\r
 \r
 Returns:\r
 \r
-  EFI STATUS\r
+  EFI_INVALID_PARAMETER   - Parameters not valid\r
+  EFI_SUCCESS             - Variable store successfully updated\r
 \r
 --*/\r
 {\r
@@ -175,14 +176,14 @@ Returns:
     if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-  }\r
-  //\r
-  // If Volatile Variable just do a simple mem copy.\r
-  //\r
-  if (Volatile) {\r
-    CopyMem ((UINT8 *) ((UINTN) DataPtr), Buffer, DataSize);\r
+    \r
+    //\r
+    // If Volatile Variable just do a simple mem copy.\r
+    //    \r
+    CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r
     return EFI_SUCCESS;\r
   }\r
+  \r
   //\r
   // If we are here we are dealing with Non-Volatile Variables\r
   //\r
@@ -211,9 +212,7 @@ Returns:
                     &CurrWriteSize,\r
                     CurrBuffer\r
                     );\r
-          if (EFI_ERROR (Status)) {\r
             return Status;\r
-          }\r
         } else {\r
           Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r
           Status = EfiFvbWriteBlock (\r
@@ -313,7 +312,6 @@ Returns:
 \r
 STATIC\r
 VARIABLE_HEADER *\r
-EFIAPI\r
 GetNextVariablePtr (\r
   IN  VARIABLE_HEADER   *Variable\r
   )\r
@@ -553,7 +551,7 @@ Returns:
 \r
     while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r
       if (Variable[Index]->State == VAR_ADDED) {\r
-        if (!(EfiAtRuntime () && !(Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r
+        if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
           if (VariableName[0] == 0) {\r
             PtrTrack->CurrPtr   = Variable[Index];\r
             PtrTrack->Volatile  = (BOOLEAN) Index;\r
@@ -587,11 +585,11 @@ EFI_STATUS
 EFIAPI\r
 GetVariable (\r
   IN      CHAR16            *VariableName,\r
-  IN      EFI_GUID          * VendorGuid,\r
+  IN      EFI_GUID          *VendorGuid,\r
   OUT     UINT32            *Attributes OPTIONAL,\r
   IN OUT  UINTN             *DataSize,\r
   OUT     VOID              *Data,\r
-  IN      VARIABLE_GLOBAL   * Global,\r
+  IN      VARIABLE_GLOBAL   *Global,\r
   IN      UINT32            Instance\r
   )\r
 /*++\r
@@ -602,18 +600,22 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  VariableName                    Name of Variable to be found\r
-  VendorGuid                      Variable vendor GUID\r
-  Attributes OPTIONAL             Attribute value of the variable found\r
-  DataSize                        Size of Data found. If size is less than the\r
-                                  data, this value contains the required size.\r
-  Data                            Data pointer\r
-  Global                          Pointer to VARIABLE_GLOBAL structure\r
-  Instance                        Instance of the Firmware Volume.\r
+  VariableName                Name of Variable to be found\r
+  VendorGuid                  Variable vendor GUID\r
+  Attributes OPTIONAL         Attribute value of the variable found\r
+  DataSize                    Size of Data found. If size is less than the\r
+                              data, this value contains the required size.\r
+  Data                        Data pointer\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       - Invalid parameter\r
+  EFI_SUCCESS                 - Find the specified variable\r
+  EFI_NOT_FOUND               - Not found\r
+  EFI_BUFFER_TO_SMALL         - DataSize is too small for the result\r
+\r
 \r
 --*/\r
 {\r
@@ -800,11 +802,12 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI STATUS\r
   EFI_INVALID_PARAMETER           - Invalid parameter\r
   EFI_SUCCESS                     - Set successfully\r
   EFI_OUT_OF_RESOURCES            - Resource not enough to set variable\r
   EFI_NOT_FOUND                   - Not found\r
+  EFI_DEVICE_ERROR                - Variable can not be saved due to hardware failure\r
+  EFI_WRITE_PROTECTED             - Variable is read-only\r
 \r
 --*/\r
 {\r
@@ -820,309 +823,339 @@ Returns:
 \r
   Reclaimed = FALSE;\r
 \r
+  //\r
+  // Check input parameters\r
+  //\r
   if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
+  }  \r
+  //\r
+  //  Make sure if runtime bit is set, boot service bit is set also\r
+  //\r
+  if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\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_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r
+  //  bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r
+  //\r
+  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+    if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) ||                                                       \r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }    \r
+  } else {\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
+    if ((DataSize > MAX_VARIABLE_SIZE) ||\r
+        (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }  \r
+  }  \r
+  //\r
+  // Check whether the input variable is already existed\r
+  //\r
+  \r
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
-  if (Status == EFI_INVALID_PARAMETER) {\r
-    goto Done;\r
-  } else if (!EFI_ERROR (Status) && Variable.Volatile && EfiAtRuntime()) {\r
+  if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\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
+    // Update/Delete existing variable\r
     //\r
-    Status = EFI_WRITE_PROTECTED;\r
-    goto Done;\r
-  } else if (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE) {\r
+    \r
+    if (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
+      if (Variable.Volatile) {\r
+        Status = EFI_WRITE_PROTECTED;\r
+        goto Done;\r
+      }\r
+      //\r
+      // Only variable have NV attribute can be updated/deleted in Runtime\r
+      //\r
+      if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto Done;      \r
+      }\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
+    // Setting a data variable with no access, or zero DataSize attributes\r
+    // specified causes it to be deleted.\r
     //\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
-  } else if (Attributes == EFI_VARIABLE_NON_VOLATILE) {\r
+    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {    \r
+      State = Variable.CurrPtr->State;\r
+      State &= VAR_DELETED;\r
+\r
+      Status = UpdateVariableStore (\r
+                 Global,\r
+                 Variable.Volatile,\r
+                 FALSE,\r
+                 Instance,\r
+                 (UINTN) &Variable.CurrPtr->State,\r
+                 sizeof (UINT8),\r
+                 &State\r
+                 ); \r
+      goto Done;     \r
+    }\r
     //\r
-    //  Make sure not only EFI_VARIABLE_NON_VOLATILE is set\r
+    // If the variable is marked valid and the same data has been passed in\r
+    // then return to the caller immediately.\r
     //\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
-  } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==\r
-                EFI_VARIABLE_RUNTIME_ACCESS) {\r
+    if (Variable.CurrPtr->DataSize == DataSize &&\r
+        (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r
+      Status = EFI_SUCCESS;\r
+      goto Done;\r
+    } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r
+               (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r
+      //\r
+      // Mark the old variable as in delete transition\r
+      //\r
+      State = Variable.CurrPtr->State;\r
+      State &= VAR_IN_DELETED_TRANSITION;\r
+\r
+      Status = UpdateVariableStore (\r
+                 Global,\r
+                 Variable.Volatile,\r
+                 FALSE,\r
+                 Instance,\r
+                 (UINTN) &Variable.CurrPtr->State,\r
+                 sizeof (UINT8),\r
+                 &State\r
+                 );      \r
+      if (EFI_ERROR (Status)) {\r
+        goto Done;  \r
+      }\r
+    }    \r
+  } else if (Status == EFI_NOT_FOUND) {\r
     //\r
-    //  Make sure if runtime bit is set, boot service bit is set also\r
+    // Create a new variable\r
+    //  \r
+    \r
     //\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
-  } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r
+    // Make sure we are trying to create a new variable.\r
+    // Setting a data variable with no access, or zero DataSize attributes means to delete it.    \r
     //\r
-    // Runtime but Attribute is not Runtime\r
+    if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
+      Status = EFI_NOT_FOUND;\r
+      goto Done;\r
+    }\r
+        \r
     //\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto Done;\r
-  } else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r
+    // Only variable have NV|RT attribute can be created in Runtime\r
     //\r
-    // Cannot set volatile variable in Runtime\r
+    if (EfiAtRuntime () &&\r
+        (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
+    }         \r
+  } else {\r
+    //\r
+    // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r
     //\r
-    Status = EFI_INVALID_PARAMETER;\r
+    ASSERT (Status == EFI_INVALID_PARAMETER);\r
     goto Done;\r
-  } else if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
+  }\r
+\r
+  //\r
+  // Function part - create a new variable and copy the data.\r
+  // Both update a variable and create a variable will come here.\r
+  //\r
+  // Tricky part: Use scratch data area at the end of volatile variable store\r
+  // as a temporary storage.\r
+  //\r
+  NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
+\r
+  SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
+\r
+  NextVariable->StartId     = VARIABLE_DATA;\r
+  NextVariable->Attributes  = Attributes;\r
+  //\r
+  // NextVariable->State = VAR_ADDED;\r
+  //\r
+  NextVariable->Reserved  = 0;\r
+  VarNameOffset           = sizeof (VARIABLE_HEADER);\r
+  VarNameSize             = StrSize (VariableName);\r
+  CopyMem (\r
+    (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
+    VariableName,\r
+    VarNameSize\r
+    );\r
+  VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
+  CopyMem (\r
+    (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
+    Data,\r
+    DataSize\r
+    );\r
+  CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
+  //\r
+  // There will be pad bytes after Data, the NextVariable->NameSize and\r
+  // NextVariable->DataSize should not include pad size so that variable\r
+  // service can get actual size in GetVariable\r
+  //\r
+  NextVariable->NameSize  = (UINT32)VarNameSize;\r
+  NextVariable->DataSize  = (UINT32)DataSize;\r
+\r
+  //\r
+  // The actual size of the variable that stores in storage should\r
+  // include pad size.\r
+  //\r
+  VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
+  if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
     //\r
-    // Setting a data variable with no access, or zero DataSize attributes\r
-    // specified causes it to be deleted.\r
+    // Create a nonvolatile variable\r
     //\r
-    if (!EFI_ERROR (Status)) {\r
-      State = Variable.CurrPtr->State;\r
-      State &= VAR_DELETED;\r
-\r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                Variable.Volatile,\r
-                FALSE,\r
-                Instance,\r
-                (UINTN) &Variable.CurrPtr->State,\r
-                sizeof (UINT8),\r
-                &State\r
-                );\r
+    \r
+    if ((UINT32) (VarSize +*NonVolatileOffset) >\r
+          ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
+          ) {\r
+      if (EfiAtRuntime ()) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto Done;\r
+      }\r
+      //\r
+      // Perform garbage collection & reclaim operation\r
+      //\r
+      Status = Reclaim (Global->NonVolatileVariableBase, NonVolatileOffset, FALSE);\r
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
-\r
-      Status = EFI_SUCCESS;\r
-      goto Done;\r
-    }\r
-\r
-    Status = EFI_NOT_FOUND;\r
-    goto Done;\r
-  } else {\r
-    if (!EFI_ERROR (Status)) {\r
       //\r
-      // If the variable is marked valid and the same data has been passed in\r
-      // then return to the caller immediately.\r
+      // If still no enough space, return out of resources\r
       //\r
-      if (Variable.CurrPtr->DataSize == DataSize &&\r
-          !CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize)\r
+      if ((UINT32) (VarSize +*NonVolatileOffset) >\r
+            ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
             ) {\r
-        Status = EFI_SUCCESS;\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
-      } else if (Variable.CurrPtr->State == VAR_ADDED) {\r
-        //\r
-        // Mark the old variable as in delete transition\r
-        //\r
-        State = Variable.CurrPtr->State;\r
-        State &= VAR_IN_DELETED_TRANSITION;\r
-\r
-        Status = UpdateVariableStore (\r
-                  Global,\r
-                  Variable.Volatile,\r
-                  FALSE,\r
-                  Instance,\r
-                  (UINTN) &Variable.CurrPtr->State,\r
-                  sizeof (UINT8),\r
-                  &State\r
-                  );\r
-        if (EFI_ERROR (Status)) {\r
-          goto Done;\r
-        }\r
       }\r
+      \r
+      Reclaimed = TRUE;\r
     }\r
     //\r
-    // Create a new variable and copy the data.\r
-    //\r
-    // Tricky part: Use scratch data area at the end of volatile variable store\r
-    // as a temporary storage.\r
+    // Three steps\r
+    // 1. Write variable header\r
+    // 2. Write variable data\r
+    // 3. Set variable state to valid\r
     //\r
-    NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
-\r
-    SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r
-\r
-    NextVariable->StartId     = VARIABLE_DATA;\r
-    NextVariable->Attributes  = Attributes;\r
     //\r
-    // NextVariable->State = VAR_ADDED;\r
+    // Step 1:\r
     //\r
-    NextVariable->Reserved  = 0;\r
-    VarNameOffset           = sizeof (VARIABLE_HEADER);\r
-    VarNameSize             = StrSize (VariableName);\r
-    CopyMem (\r
-      (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
-      VariableName,\r
-      VarNameSize\r
-      );\r
-    VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
-    CopyMem (\r
-      (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
-      Data,\r
-      DataSize\r
-      );\r
-    CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
+    Status = UpdateVariableStore (\r
+               Global,\r
+               FALSE,\r
+               TRUE,\r
+               Instance,\r
+               *NonVolatileOffset,\r
+               sizeof (VARIABLE_HEADER),\r
+               (UINT8 *) NextVariable\r
+               );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
     //\r
-    // There will be pad bytes after Data, the NextVariable->NameSize and\r
-    // NextVariable->DataSize should not include pad size so that variable\r
-    // service can get actual size in GetVariable\r
+    // Step 2:\r
     //\r
-    NextVariable->NameSize  = (UINT32)VarNameSize;\r
-    NextVariable->DataSize  = (UINT32)DataSize;\r
+    Status = UpdateVariableStore (\r
+               Global,\r
+               FALSE,\r
+               TRUE,\r
+               Instance,\r
+               *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
+               (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
+               (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
+               );\r
 \r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
     //\r
-    // The actual size of the variable that stores in storage should\r
-    // include pad size.\r
+    // Step 3:\r
     //\r
-    VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
-    if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r
-      if ((UINT32) (VarSize +*NonVolatileOffset) >\r
-            ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
-            ) {\r
-        if (EfiAtRuntime ()) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Done;\r
-        }\r
-        //\r
-        // Perform garbage collection & reclaim operation\r
-        //\r
-        Status = Reclaim (Global->NonVolatileVariableBase, NonVolatileOffset, FALSE);\r
-        if (EFI_ERROR (Status)) {\r
-          goto Done;\r
-        }\r
-        //\r
-        // If still no enough space, return out of resources\r
-        //\r
-        if ((UINT32) (VarSize +*NonVolatileOffset) >\r
-              ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->NonVolatileVariableBase)))->Size\r
-              ) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Done;\r
-        }\r
+    NextVariable->State = VAR_ADDED;\r
+    Status = UpdateVariableStore (\r
+               Global,\r
+               FALSE,\r
+               TRUE,\r
+               Instance,\r
+               *NonVolatileOffset,\r
+               sizeof (VARIABLE_HEADER),\r
+               (UINT8 *) NextVariable\r
+               );\r
 \r
-        Reclaimed = TRUE;\r
-      }\r
-      //\r
-      // Three steps\r
-      // 1. Write variable header\r
-      // 2. Write variable data\r
-      // 3. Set variable state to valid\r
-      //\r
-      //\r
-      // Step 1:\r
-      //\r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                FALSE,\r
-                TRUE,\r
-                Instance,\r
-                *NonVolatileOffset,\r
-                sizeof (VARIABLE_HEADER),\r
-                (UINT8 *) NextVariable\r
-                );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
 \r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
-      }\r
+    *NonVolatileOffset = *NonVolatileOffset + VarSize;\r
+\r
+  } else {\r
+    //\r
+    // Create a volatile variable\r
+    //      \r
+\r
+    if ((UINT32) (VarSize +*VolatileOffset) >\r
+        ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size) {\r
       //\r
-      // Step 2:\r
+      // Perform garbage collection & reclaim operation\r
       //\r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                FALSE,\r
-                TRUE,\r
-                Instance,\r
-                *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r
-                (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r
-                (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r
-                );\r
-\r
+      Status = Reclaim (Global->VolatileVariableBase, VolatileOffset, TRUE);\r
       if (EFI_ERROR (Status)) {\r
         goto Done;\r
       }\r
       //\r
-      // Step 3:\r
+      // If still no enough space, return out of resources\r
       //\r
-      NextVariable->State = VAR_ADDED;\r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                FALSE,\r
-                TRUE,\r
-                Instance,\r
-                *NonVolatileOffset,\r
-                sizeof (VARIABLE_HEADER),\r
-                (UINT8 *) NextVariable\r
-                );\r
-\r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
-      }\r
-\r
-      *NonVolatileOffset = *NonVolatileOffset + VarSize;\r
-\r
-    } else {\r
-      if (EfiAtRuntime ()) {\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
-        //\r
-        // Perform garbage collection & reclaim operation\r
-        //\r
-        Status = Reclaim (Global->VolatileVariableBase, VolatileOffset, TRUE);\r
-        if (EFI_ERROR (Status)) {\r
-          goto Done;\r
-        }\r
-        //\r
-        // If still no enough space, return out of resources\r
-        //\r
-        if ((UINT32) (VarSize +*VolatileOffset) >\r
-              ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
-              ) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Done;\r
-        }\r
-\r
-        Reclaimed = TRUE;\r
-      }\r
-\r
-      NextVariable->State = VAR_ADDED;\r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                TRUE,\r
-                TRUE,\r
-                Instance,\r
-                *VolatileOffset,\r
-                (UINT32) VarSize,\r
-                (UINT8 *) NextVariable\r
-                );\r
-\r
-      if (EFI_ERROR (Status)) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-\r
-      *VolatileOffset = *VolatileOffset + VarSize;\r
+      \r
+      Reclaimed = TRUE;\r
     }\r
-    //\r
-    // Mark the old variable as deleted\r
-    //\r
-    if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r
-      State = Variable.CurrPtr->State;\r
-      State &= VAR_DELETED;\r
 \r
-      Status = UpdateVariableStore (\r
-                Global,\r
-                Variable.Volatile,\r
-                FALSE,\r
-                Instance,\r
-                (UINTN) &Variable.CurrPtr->State,\r
-                sizeof (UINT8),\r
-                &State\r
-                );\r
+    NextVariable->State = VAR_ADDED;\r
+    Status = UpdateVariableStore (\r
+               Global,\r
+               TRUE,\r
+               TRUE,\r
+               Instance,\r
+               *VolatileOffset,\r
+               (UINT32) VarSize,\r
+               (UINT8 *) NextVariable\r
+               );\r
 \r
-      if (EFI_ERROR (Status)) {\r
-        goto Done;\r
-      }\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
     }\r
+\r
+    *VolatileOffset = *VolatileOffset + VarSize;\r
+  }\r
+  //\r
+  // Mark the old variable as deleted\r
+  //\r
+  if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r
+    State = Variable.CurrPtr->State;\r
+    State &= VAR_DELETED;\r
+\r
+    Status = UpdateVariableStore (\r
+               Global,\r
+               Variable.Volatile,\r
+               FALSE,\r
+               Instance,\r
+               (UINTN) &Variable.CurrPtr->State,\r
+               sizeof (UINT8),\r
+               &State\r
+               );\r
+    goto Done;      \r
   }\r
 \r
   Status = EFI_SUCCESS;\r
@@ -1154,8 +1187,8 @@ Arguments:
   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
+                                  for EFI variables associated with the attributes specified.\r
+  MaximumVariableSize             Pointer to the maximum size of an individual EFI variables\r
                                   associated with the attributes specified.\r
   Global                          Pointer to VARIABLE_GLOBAL structure.\r
   Instance                        Instance of the Firmware Volume.\r
@@ -1174,15 +1207,15 @@ Returns:
   UINT64                 VariableSize;\r
   VARIABLE_STORE_HEADER  *VariableStoreHeader;\r
 \r
-  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL) {\r
+  if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\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
+  if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
     //\r
     // Make sure the Attributes combination is supported by the platform.\r
     //\r
-    return EFI_UNSUPPORTED;\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
@@ -1217,9 +1250,16 @@ Returns:
   *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
 \r
   //\r
-  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE.\r
+  // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r
   //\r
-  *MaximumVariableSize = MAX_VARIABLE_SIZE;\r
+  *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+\r
+  //\r
+  // Harware error record variable needs larger size.\r
+  //\r
+  if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
+    *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r
+  }\r
 \r
   //\r
   // Point to the starting address of the variables.\r
@@ -1257,6 +1297,12 @@ Returns:
     Variable = NextVariable;\r
   }\r
 \r
+  if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
+    *MaximumVariableSize = 0;\r
+  } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
+    *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
+  }\r
+\r
   ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
   return EFI_SUCCESS;\r
 }\r
@@ -1295,9 +1341,7 @@ Returns:
   VARIABLE_HEADER                 *NextVariable;\r
   UINT32                          Instance;\r
   EFI_PHYSICAL_ADDRESS            FvVolHdr;\r
-\r
   UINT64                          TempVariableStoreHeader;\r
-\r
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
   EFI_FLASH_SUBAREA_ENTRY         VariableStoreEntry;\r
   UINT64                          BaseAddress;\r