]> 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 e0ac6ee17c87e2da01d33a71285f8670c130b736..3d1d29dfd9d3400f99e2c8183f9c5210543c4f61 100644 (file)
@@ -1,47 +1,10 @@
 /** @file\r
 \r
-  This is a simple fault tolerant write driver.\r
+  These are the common Fault Tolerant Write (FTW) functions that are shared \r
+  by DXE FTW driver and SMM FTW driver.\r
 \r
-  This boot service protocol only provides fault tolerant write capability for \r
-  block devices.  The protocol has internal non-volatile intermediate storage \r
-  of the data and private information. It should be able to recover \r
-  automatically from a critical fault, such as power failure. \r
-\r
-  The implementation uses an FTW (Fault Tolerant Write) Work Space. \r
-  This work space is a memory copy of the work space on the Working Block,\r
-  the size of the work space is the FTW_WORK_SPACE_SIZE bytes.\r
-  \r
-  The work space stores each write record as EFI_FTW_RECORD structure.\r
-  The spare block stores the write buffer before write to the target block.\r
-  \r
-  The write record has three states to specify the different phase of write operation.\r
-  1) WRITE_ALLOCATED is that the record is allocated in write space.\r
-     The information of write operation is stored in write record structure.\r
-  2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.\r
-  3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.\r
-\r
-  This driver operates the data as the whole size of spare block.\r
-  It first read the SpareAreaLength data from the target block into the spare memory buffer.\r
-  Then copy the write buffer data into the spare memory buffer.\r
-  Then write the spare memory buffer into the spare block.\r
-  Final copy the data from the spare block to the target block.\r
-\r
-  To make this drive work well, the following conditions must be satisfied:\r
-  1. The write NumBytes data must be fit within Spare area. \r
-     Offset + NumBytes <= SpareAreaLength\r
-  2. The whole flash range has the same block size.\r
-  3. Working block is an area which contains working space in its last block and has the same size as spare block.\r
-  4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on.  \r
-  5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.\r
-  6. Any write data area (SpareAreaLength Area) which the data will be written into must be \r
-     in the single one Firmware Volume Block range which FVB protocol is produced on.\r
-  7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.\r
-     The spare area must be enough large to store the write data before write them into the target range.\r
-  If one of them is not satisfied, FtwWrite may fail.\r
-  Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.\r
-\r
-Copyright (c) 2006 - 2009, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \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
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -53,8 +16,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "FaultTolerantWrite.h"\r
 \r
-EFI_EVENT          mFvbRegistration = NULL;\r
-\r
 //\r
 // Fault Tolerant Write Protocol API\r
 //\r
@@ -134,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
@@ -154,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
@@ -232,12 +193,15 @@ FtwWriteRecord (
   EFI_FAULT_TOLERANT_WRITE_HEADER *Header;\r
   EFI_FAULT_TOLERANT_WRITE_RECORD *Record;\r
   UINTN                           Offset;\r
+  EFI_LBA                         WorkSpaceLbaOffset;\r
 \r
   FtwDevice = FTW_CONTEXT_FROM_THIS (This);\r
 \r
+  WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;\r
+\r
   //\r
   // Spare Complete but Destination not complete,\r
-  // Recover the targt block with the spare block.\r
+  // Recover the target block with the spare block.\r
   //\r
   Header  = FtwDevice->FtwLastWriteHeader;\r
   Record  = FtwDevice->FtwLastWriteRecord;\r
@@ -254,7 +218,7 @@ FtwWriteRecord (
     Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;\r
     Status = FtwUpdateFvState (\r
               FtwDevice->FtwBackupFvb,\r
-              FtwDevice->FtwWorkSpaceLba,\r
+              FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
               FtwDevice->FtwWorkSpaceBase + Offset,\r
               SPARE_COMPLETED\r
               );\r
@@ -401,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
@@ -451,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
@@ -729,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
@@ -837,6 +805,7 @@ FtwGetLastWrite (
       *Complete = TRUE;\r
       return EFI_NOT_FOUND;\r
     }\r
+    ASSERT (Record != NULL);\r
   }\r
 \r
   //\r
@@ -844,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
@@ -863,373 +832,3 @@ FtwGetLastWrite (
   return Status;\r
 }\r
 \r
-VOID\r
-EFIAPI\r
-FvbNotificationEvent (\r
-  IN  EFI_EVENT       Event,\r
-  IN  VOID            *Context\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_HANDLE                          *HandleBuffer;\r
-  UINTN                               HandleCount;\r
-  UINTN                               Index;\r
-  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-  EFI_FVB_ATTRIBUTES_2                Attributes;\r
-  EFI_FTW_DEVICE                      *FtwDevice;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
-  UINT32                              LbaIndex;\r
-  UINTN                               Length;\r
-  EFI_FAULT_TOLERANT_WRITE_HEADER     *FtwHeader;\r
-  UINTN                               Offset;\r
-  EFI_HANDLE                          FvbHandle;\r
-\r
-  FtwDevice = (EFI_FTW_DEVICE *)Context;\r
-  FvbHandle = NULL;\r
-  Fvb       = NULL;\r
-\r
-  //\r
-  // Locate all handles of Fvb protocol\r
-  //\r
-  Status = gBS->LocateHandleBuffer (\r
-                ByProtocol,\r
-                &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                NULL,\r
-                &HandleCount,\r
-                &HandleBuffer\r
-                );\r
-  if (EFI_ERROR (Status)) {\r
-    return;\r
-  }\r
-\r
-  //\r
-  // Get the FVB to access variable store\r
-  //\r
-  for (Index = 0; Index < HandleCount; Index += 1) {\r
-    Status = gBS->HandleProtocol (\r
-                  HandleBuffer[Index],\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  (VOID **) &Fvb\r
-                  );\r
-    if (EFI_ERROR (Status)) {\r
-      Status = EFI_NOT_FOUND;\r
-      break;\r
-    }\r
-\r
-    //\r
-    // Ensure this FVB protocol supported Write operation.\r
-    //\r
-    Status = Fvb->GetAttributes (Fvb, &Attributes);\r
-    if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r
-      continue;     \r
-    }\r
-    //\r
-    // Compare the address and select the right one\r
-    //\r
-    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
-    if ((FtwDevice->FtwFvBlock == NULL) && (FtwDevice->WorkSpaceAddress >= FvbBaseAddress) &&\r
-      ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + FwVolHeader->FvLength))\r
-      ) {\r
-      FtwDevice->FtwFvBlock = Fvb;\r
-      //\r
-      // To get the LBA of work space\r
-      //\r
-      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-        //\r
-        // Now, one FV has one type of BlockLength\r
-        //\r
-        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-        for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-          if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
-              && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
-            FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;\r
-            //\r
-            // Get the Work space size and Base(Offset)\r
-            //\r
-            FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;\r
-            FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
-            break;\r
-          }\r
-        }\r
-      }\r
-    }\r
-    \r
-    if ((FtwDevice->FtwBackupFvb == NULL) && (FtwDevice->SpareAreaAddress >= FvbBaseAddress) &&\r
-      ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + FwVolHeader->FvLength))\r
-      ) {\r
-      FtwDevice->FtwBackupFvb = Fvb;\r
-      //\r
-      // To get the LBA of spare\r
-      //\r
-      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-        //\r
-        // Now, one FV has one type of BlockLength\r
-        //\r
-        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-        for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-          if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
-              && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
-            //\r
-            // Get the NumberOfSpareBlock and BlockSize\r
-            //\r
-            FtwDevice->FtwSpareLba   = LbaIndex - 1;\r
-            FtwDevice->BlockSize     = FvbMapEntry->Length;\r
-            FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize;\r
-            //\r
-            // Check the range of spare area to make sure that it's in FV range\r
-            //\r
-            if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) {\r
-              DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));\r
-              ASSERT (FALSE);\r
-              return;\r
-            }\r
-            break;\r
-          }\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) ||\r
-    (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) {\r
-    return;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "Ftw: Working and spare FVB is ready\n"));\r
-  //\r
-  // Calculate the start LBA of working block. Working block is an area which\r
-  // contains working space in its last block and has the same size as spare\r
-  // block, unless there are not enough blocks before the block that contains\r
-  // working space.\r
-  //\r
-  FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1;\r
-  ASSERT ((INT64) (FtwDevice->FtwWorkBlockLba) >= 0); \r
-\r
-  //\r
-  // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
-  //\r
-  FtwDevice->FtwWorkSpace = (UINT8 *) (FtwDevice + 1);\r
-  FtwDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwDevice->FtwWorkSpace;\r
-\r
-  FtwDevice->FtwLastWriteHeader = NULL;\r
-  FtwDevice->FtwLastWriteRecord = NULL;\r
-\r
-  //\r
-  // Refresh the working space data from working block\r
-  //\r
-  Status = WorkSpaceRefresh (FtwDevice);\r
-  ASSERT_EFI_ERROR (Status);\r
-  //\r
-  // If the working block workspace is not valid, try the spare block\r
-  //\r
-  if (!IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
-    //\r
-    // Read from spare block\r
-    //\r
-    Length = FtwDevice->FtwWorkSpaceSize;\r
-    Status = FtwDevice->FtwBackupFvb->Read (\r
-                    FtwDevice->FtwBackupFvb,\r
-                    FtwDevice->FtwSpareLba,\r
-                    FtwDevice->FtwWorkSpaceBase,\r
-                    &Length,\r
-                    FtwDevice->FtwWorkSpace\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    //\r
-    // If spare block is valid, then replace working block content.\r
-    //\r
-    if (IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
-      Status = FlushSpareBlockToWorkingBlock (FtwDevice);\r
-      DEBUG ((EFI_D_ERROR, "Ftw: Restart working block update in Init() - %r\n", Status));\r
-      FtwAbort (&FtwDevice->FtwInstance);\r
-      //\r
-      // Refresh work space.\r
-      //\r
-      Status = WorkSpaceRefresh (FtwDevice);\r
-      ASSERT_EFI_ERROR (Status);\r
-    } else {\r
-      DEBUG ((EFI_D_ERROR, "Ftw: Both are invalid, init workspace\n"));\r
-      //\r
-      // If both are invalid, then initialize work space.\r
-      //\r
-      SetMem (\r
-        FtwDevice->FtwWorkSpace,\r
-        FtwDevice->FtwWorkSpaceSize,\r
-        FTW_ERASED_BYTE\r
-        );\r
-      InitWorkSpaceHeader (FtwDevice->FtwWorkSpaceHeader);\r
-      //\r
-      // Initialize the work space\r
-      //\r
-      Status = FtwReclaimWorkSpace (FtwDevice, FALSE);\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\r
-  }\r
-  //\r
-  // If the FtwDevice->FtwLastWriteRecord is 1st record of write header &&\r
-  // (! SpareComplete) THEN call Abort().\r
-  //\r
-  if ((FtwDevice->FtwLastWriteHeader->HeaderAllocated == FTW_VALID_STATE) &&\r
-    (FtwDevice->FtwLastWriteRecord->SpareComplete != FTW_VALID_STATE) &&\r
-    IsFirstRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
-    ) {\r
-    DEBUG ((EFI_D_ERROR, "Ftw: Init.. find first record not SpareCompleted, abort()\n"));\r
-    FtwAbort (&FtwDevice->FtwInstance);\r
-  }\r
-  //\r
-  // If Header is incompleted and the last record has completed, then\r
-  // call Abort() to set the Header->Complete FLAG.\r
-  //\r
-  if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
-    (FtwDevice->FtwLastWriteRecord->DestinationComplete == FTW_VALID_STATE) &&\r
-    IsLastRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
-    ) {\r
-    DEBUG ((EFI_D_ERROR, "Ftw: Init.. find last record completed but header not, abort()\n"));\r
-    FtwAbort (&FtwDevice->FtwInstance);\r
-  }\r
-  //\r
-  // To check the workspace buffer following last Write header/records is EMPTY or not.\r
-  // If it's not EMPTY, FTW also need to call reclaim().\r
-  //\r
-  FtwHeader = FtwDevice->FtwLastWriteHeader;\r
-  Offset    = (UINT8 *) FtwHeader - FtwDevice->FtwWorkSpace;\r
-  if (FtwDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {\r
-    Offset += WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);\r
-  }\r
-  \r
-  if (!IsErasedFlashBuffer (FtwDevice->FtwWorkSpace + Offset, FtwDevice->FtwWorkSpaceSize - Offset)) {\r
-    Status = FtwReclaimWorkSpace (FtwDevice, TRUE);\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
-  //\r
-  // Restart if it's boot block\r
-  //\r
-  if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
-    (FtwDevice->FtwLastWriteRecord->SpareComplete == FTW_VALID_STATE)\r
-    ) {\r
-    if (FtwDevice->FtwLastWriteRecord->BootBlockUpdate == FTW_VALID_STATE) {\r
-      Status = FlushSpareBlockToBootBlock (FtwDevice);\r
-      DEBUG ((EFI_D_ERROR, "Ftw: Restart boot block update - %r\n", Status));\r
-      ASSERT_EFI_ERROR (Status);\r
-      FtwAbort (&FtwDevice->FtwInstance);\r
-    } else {\r
-      //\r
-      // if (SpareCompleted) THEN  Restart to fault tolerant write.\r
-      //\r
-      FvbHandle = GetFvbByAddress (FtwDevice->FtwLastWriteRecord->FvBaseAddress, &Fvb);\r
-      if (FvbHandle != NULL) {\r
-        Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle);\r
-        DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));\r
-        ASSERT_EFI_ERROR (Status);\r
-      }\r
-      FtwAbort (&FtwDevice->FtwInstance);\r
-    }\r
-  }\r
-  //\r
-  // Hook the protocol API\r
-  //\r
-  FtwDevice->FtwInstance.GetMaxBlockSize = FtwGetMaxBlockSize;\r
-  FtwDevice->FtwInstance.Allocate        = FtwAllocate;\r
-  FtwDevice->FtwInstance.Write           = FtwWrite;\r
-  FtwDevice->FtwInstance.Restart         = FtwRestart;\r
-  FtwDevice->FtwInstance.Abort           = FtwAbort;\r
-  FtwDevice->FtwInstance.GetLastWrite    = FtwGetLastWrite;\r
-  \r
-  //\r
-  // Install protocol interface\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-            &FtwDevice->Handle,\r
-            &gEfiFaultTolerantWriteProtocolGuid,\r
-            EFI_NATIVE_INTERFACE,\r
-            &FtwDevice->FtwInstance\r
-            );\r
-\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  //\r
-  // Close the notify event to avoid install FaultTolerantWriteProtocol again.\r
-  //\r
-  Status = gBS->CloseEvent (Event);    \r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
-  return;\r
-}\r
-\r
-/**\r
-  This function is the entry point of the Fault Tolerant Write driver.\r
-\r
-  @param ImageHandle     A handle for the image that is initializing this driver\r
-  @param SystemTable     A pointer to the EFI system table\r
-\r
-  @return EFI_SUCCESS           FTW has finished the initialization\r
-  @retval EFI_NOT_FOUND         Locate FVB protocol error\r
-  @retval EFI_OUT_OF_RESOURCES  Allocate memory error\r
-  @retval EFI_VOLUME_CORRUPTED  Firmware volume is error\r
-  @retval EFI_ABORTED           FTW initialization error\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeFaultTolerantWrite (\r
-  IN EFI_HANDLE         ImageHandle,\r
-  IN EFI_SYSTEM_TABLE   *SystemTable\r
-  )\r
-{\r
-  EFI_FTW_DEVICE                      *FtwDevice;\r
-\r
-  //\r
-  // Allocate Private data of this driver,\r
-  // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
-  //\r
-  FtwDevice = NULL;\r
-  FtwDevice = AllocateZeroPool (sizeof (EFI_FTW_DEVICE) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize));\r
-  if (FtwDevice == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  ZeroMem (FtwDevice, sizeof (EFI_FTW_DEVICE));\r
-  FtwDevice->Signature = FTW_DEVICE_SIGNATURE;\r
-\r
-  //\r
-  // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
-  //\r
-\r
-  FtwDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
-  FtwDevice->WorkSpaceLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
-\r
-  FtwDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);\r
-  FtwDevice->SpareAreaLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r
-\r
-  if ((FtwDevice->WorkSpaceLength == 0) || (FtwDevice->SpareAreaLength == 0)) {\r
-    DEBUG ((EFI_D_ERROR, "Ftw: Workspace or Spare block does not exist!\n"));\r
-    FreePool (FtwDevice);\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  FtwDevice->FtwFvBlock       = NULL;\r
-  FtwDevice->FtwBackupFvb     = NULL;\r
-  FtwDevice->FtwWorkSpaceLba  = (EFI_LBA) (-1);\r
-  FtwDevice->FtwSpareLba      = (EFI_LBA) (-1);\r
-\r
-  //\r
-  // Register FvbNotificationEvent () notify function.\r
-  // \r
-  EfiCreateProtocolNotifyEvent (\r
-    &gEfiFirmwareVolumeBlockProtocolGuid,\r
-    TPL_CALLBACK,\r
-    FvbNotificationEvent,\r
-    (VOID *)FtwDevice,\r
-    &mFvbRegistration\r
-    );\r
-\r
-  return EFI_SUCCESS;\r
-}\r