]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c
MdeModulePkg: Variable drivers robustly handle crashes during Reclaim().
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWrite.c
index 2f110167d7cc19cc46f89447d757bea0a0f8c859..3d1d29dfd9d3400f99e2c8183f9c5210543c4f61 100644 (file)
@@ -3,7 +3,7 @@
   These are the common Fault Tolerant Write (FTW) functions that are shared \r
   by DXE FTW driver and SMM FTW driver.\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2013, 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
@@ -95,7 +95,7 @@ FtwAllocate (
   //\r
   // Check if there is enough space for the coming allocation\r
   //\r
-  if (WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceHeader->WriteQueueSize) {\r
+  if (FTW_WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceHeader->WriteQueueSize) {\r
     DEBUG ((EFI_D_ERROR, "Ftw: Allocate() request exceed Workspace, Caller: %g\n", CallerId));\r
     return EFI_BUFFER_TOO_SMALL;\r
   }\r
@@ -115,7 +115,7 @@ FtwAllocate (
   // If workspace is not enough, then reclaim workspace\r
   //\r
   Offset = (UINT8 *) FtwHeader - (UINT8 *) FtwDevice->FtwWorkSpace;\r
-  if (Offset + WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceSize) {\r
+  if (Offset + FTW_WRITE_TOTAL_SIZE (NumberOfWrites, PrivateDataSize) > FtwDevice->FtwWorkSpaceSize) {\r
     Status = FtwReclaimWorkSpace (FtwDevice, TRUE);\r
     if (EFI_ERROR (Status)) {\r
       return EFI_ABORTED;\r
@@ -365,7 +365,7 @@ FtwWrite (
   //\r
   // If Record is out of the range of Header, return access denied.\r
   //\r
-  if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {\r
+  if (((UINTN)((UINT8 *) Record - (UINT8 *) Header)) > FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites - 1, Header->PrivateDataSize)) {\r
     return EFI_ACCESS_DENIED;\r
   }\r
 \r
@@ -415,13 +415,13 @@ FtwWrite (
   Record->Lba     = Lba;\r
   Record->Offset  = Offset;\r
   Record->Length  = Length;\r
-  Record->FvBaseAddress = FvbPhysicalAddress;\r
+  Record->RelativeOffset = (INT64) (FvbPhysicalAddress + (UINTN) Lba * FtwDevice->BlockSize) - (INT64) FtwDevice->SpareAreaAddress;\r
   if (PrivateData != NULL) {\r
-    CopyMem ((Record + 1), PrivateData, Header->PrivateDataSize);\r
+    CopyMem ((Record + 1), PrivateData, (UINTN) Header->PrivateDataSize);\r
   }\r
 \r
   MyOffset  = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
-  MyLength  = RECORD_SIZE (Header->PrivateDataSize);\r
+  MyLength  = FTW_RECORD_SIZE (Header->PrivateDataSize);\r
 \r
   Status = FtwDevice->FtwFvBlock->Write (\r
                                     FtwDevice->FtwFvBlock,\r
@@ -693,6 +693,10 @@ FtwAbort (
     return EFI_ABORTED;\r
   }\r
 \r
+  if (FtwDevice->FtwLastWriteHeader->HeaderAllocated != FTW_VALID_STATE) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
   if (FtwDevice->FtwLastWriteHeader->Complete == FTW_VALID_STATE) {\r
     return EFI_NOT_FOUND;\r
   }\r
@@ -809,16 +813,16 @@ FtwGetLastWrite (
   //\r
   CopyMem (CallerId, &Header->CallerId, sizeof (EFI_GUID));\r
   *Lba      = Record->Lba;\r
-  *Offset   = Record->Offset;\r
-  *Length   = Record->Length;\r
+  *Offset   = (UINTN) Record->Offset;\r
+  *Length   = (UINTN) Record->Length;\r
   *Complete = (BOOLEAN) (Record->DestinationComplete == FTW_VALID_STATE);\r
 \r
   if (*PrivateDataSize < Header->PrivateDataSize) {\r
-    *PrivateDataSize  = Header->PrivateDataSize;\r
+    *PrivateDataSize  = (UINTN) Header->PrivateDataSize;\r
     PrivateData       = NULL;\r
     Status            = EFI_BUFFER_TOO_SMALL;\r
   } else {\r
-    *PrivateDataSize = Header->PrivateDataSize;\r
+    *PrivateDataSize = (UINTN) Header->PrivateDataSize;\r
     CopyMem (PrivateData, Record + 1, *PrivateDataSize);\r
     Status = EFI_SUCCESS;\r
   }\r