]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.c
Clean up FaultTolerantWriteDxe to remove the duplicated definition.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / FaultTolerantWriteDxe / FtwLite.c
index 9a999c900e3a9b2fe749836360950b94d75aa783..5e3b7af3f01588fcfa9dcffc3f6b0872ddb650f9 100644 (file)
@@ -1,7 +1,6 @@
 /** @file\r
 \r
   This is a simple fault tolerant write driver.\r
-  And it only supports write BufferSize <= SpareAreaLength.\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
   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. It also assumes that \r
-  working block is an area which contains working space in its last block and has the same size as spare block.\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
-Copyright (c) 2006 - 2008, Intel Corporation                                                         \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, FtwLiteWrite 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
 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
@@ -89,7 +101,6 @@ FtwLiteWrite (
   UINTN                               Index;\r
   UINT8                               *Ptr;\r
   EFI_DEV_PATH_PTR                    DevPtr;\r
-\r
   //\r
   // Refresh work space and get last record\r
   //\r
@@ -121,6 +132,7 @@ FtwLiteWrite (
   // Check if the input data can fit within the target block\r
   //\r
   if ((Offset +*NumBytes) > FtwLiteDevice->SpareAreaLength) {\r
+    *NumBytes = FtwLiteDevice->SpareAreaLength - Offset;\r
     return EFI_BAD_BUFFER_SIZE;\r
   }\r
   //\r
@@ -152,7 +164,7 @@ FtwLiteWrite (
             );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Allocate record - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Allocate record - %r\n", Status));\r
     return EFI_ABORTED;\r
   }\r
 \r
@@ -168,7 +180,7 @@ FtwLiteWrite (
 \r
   Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Get FVB physical address - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Get FVB physical address - %r\n", Status));\r
     return EFI_ABORTED;\r
   }\r
 \r
@@ -223,7 +235,7 @@ FtwLiteWrite (
     //\r
     Ptr = MyBuffer;\r
     for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+      MyLength = FtwLiteDevice->BlockSize;\r
       Status = FtwLiteDevice->FtwFvBlock->Read (\r
                                             FtwLiteDevice->FtwFvBlock,\r
                                             FtwLiteDevice->FtwWorkBlockLba + Index,\r
@@ -242,14 +254,14 @@ FtwLiteWrite (
     // Update Offset by adding the offset from the start LBA of working block to\r
     // the target LBA. The target block can not span working block!\r
     //\r
-    Offset = (((UINTN) (Lba - FtwLiteDevice->FtwWorkBlockLba)) * FtwLiteDevice->SizeOfSpareBlock + Offset);\r
+    Offset = (((UINTN) (Lba - FtwLiteDevice->FtwWorkBlockLba)) * FtwLiteDevice->BlockSize + Offset);\r
     ASSERT ((Offset +*NumBytes) <= FtwLiteDevice->SpareAreaLength);\r
 \r
   } else {\r
 \r
     Ptr = MyBuffer;\r
     for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      MyLength  = FtwLiteDevice->SizeOfSpareBlock;\r
+      MyLength  = FtwLiteDevice->BlockSize;\r
       Status    = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);\r
       if (EFI_ERROR (Status)) {\r
         FreePool (MyBuffer);\r
@@ -278,7 +290,7 @@ FtwLiteWrite (
 \r
   Ptr = SpareBuffer;\r
   for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    MyLength = FtwLiteDevice->BlockSize;\r
     Status = FtwLiteDevice->FtwBackupFvb->Read (\r
                                             FtwLiteDevice->FtwBackupFvb,\r
                                             FtwLiteDevice->FtwSpareLba + Index,\r
@@ -301,7 +313,7 @@ FtwLiteWrite (
   Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
   Ptr     = MyBuffer;\r
   for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    MyLength = FtwLiteDevice->BlockSize;\r
     Status = FtwLiteDevice->FtwBackupFvb->Write (\r
                                             FtwLiteDevice->FtwBackupFvb,\r
                                             FtwLiteDevice->FtwSpareLba + Index,\r
@@ -358,7 +370,7 @@ FtwLiteWrite (
   Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
   Ptr     = SpareBuffer;\r
   for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    MyLength = FtwLiteDevice->BlockSize;\r
     Status = FtwLiteDevice->FtwBackupFvb->Write (\r
                                             FtwLiteDevice->FtwBackupFvb,\r
                                             FtwLiteDevice->FtwSpareLba + Index,\r
@@ -379,7 +391,7 @@ FtwLiteWrite (
   FreePool (SpareBuffer);\r
 \r
   DEBUG (\r
-    (EFI_D_FTW_LITE,\r
+    (EFI_D_ERROR,\r
     "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",\r
     Lba,\r
     Offset,\r
@@ -504,7 +516,7 @@ FtwRestart (
   DevPathPtr.MemMap = (MEMMAP_DEVICE_PATH *) &Record->DevPath;\r
   if (!((DevPathPtr.MemMap->Header.Type == HARDWARE_DEVICE_PATH) && (DevPathPtr.MemMap->Header.SubType == HW_MEMMAP_DP))\r
       ) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Device Path is not memory mapped\n"));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: FVB Device Path is not memory mapped\n"));\r
     return EFI_ABORTED;\r
   }\r
 \r
@@ -517,7 +529,7 @@ FtwRestart (
   //  guaranteed to be completed with fault tolerant manner.\r
   //\r
   Status = FtwWriteRecord (FtwLiteDevice, Fvb);\r
-  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Restart() - %r\n", Status));\r
+  DEBUG ((EFI_D_INFO, "FtwLite: Restart() - %r\n", Status));\r
 \r
   Record++;\r
   FtwLiteDevice->FtwLastRecord = Record;\r
@@ -577,7 +589,7 @@ FtwAbort (
   //\r
   Status = FtwEraseSpareBlock (FtwLiteDevice);\r
 \r
-  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Abort() success \n"));\r
+  DEBUG ((EFI_D_INFO, "FtwLite: Abort() success \n"));\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -606,22 +618,16 @@ InitializeFtwLite (
   EFI_PHYSICAL_ADDRESS                BaseAddress;\r
   EFI_FTW_LITE_DEVICE                 *FtwLiteDevice;\r
   EFI_FTW_LITE_RECORD                 *Record;\r
-  UINTN                               Length;\r
   EFI_STATUS                          Status;\r
   UINTN                               Offset;\r
+  UINTN                               Length;\r
   EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
   UINT32                              LbaIndex;\r
-\r
   //\r
   // Allocate Private data of this driver, including the FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
   //\r
-  Length = FTW_WORK_SPACE_SIZE;\r
-  if (Length < PcdGet32 (PcdFlashNvStorageFtwWorkingSize)) {\r
-    Length = PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
-  }\r
-\r
   FtwLiteDevice = NULL;\r
-  FtwLiteDevice = AllocatePool (sizeof (EFI_FTW_LITE_DEVICE) + Length);\r
+  FtwLiteDevice = AllocatePool (sizeof (EFI_FTW_LITE_DEVICE) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize));\r
   ASSERT (FtwLiteDevice != NULL);\r
 \r
   ZeroMem (FtwLiteDevice, sizeof (EFI_FTW_LITE_DEVICE));\r
@@ -631,15 +637,7 @@ InitializeFtwLite (
   // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
   //\r
   FtwLiteDevice->FtwWorkSpace     = (UINT8 *) (FtwLiteDevice + 1);\r
-  FtwLiteDevice->FtwWorkSpaceSize = FTW_WORK_SPACE_SIZE;\r
-  FtwLiteDevice->FtwWorkSpaceBase = FTW_WORK_SPACE_BASE;\r
-  SetMem (\r
-    FtwLiteDevice->FtwWorkSpace,\r
-    FtwLiteDevice->FtwWorkSpaceSize,\r
-    FTW_ERASED_BYTE\r
-    );\r
   FtwLiteDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwLiteDevice->FtwWorkSpace;\r
-\r
   FtwLiteDevice->FtwLastRecord      = NULL;\r
 \r
   FtwLiteDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
@@ -684,7 +682,7 @@ InitializeFtwLite (
     FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) BaseAddress);\r
 \r
     if ((FtwLiteDevice->WorkSpaceAddress >= BaseAddress) &&\r
-        (FtwLiteDevice->WorkSpaceAddress < (BaseAddress + FwVolHeader->FvLength))\r
+        ((FtwLiteDevice->WorkSpaceAddress + FtwLiteDevice->WorkSpaceLength) <= (BaseAddress + FwVolHeader->FvLength))\r
         ) {\r
       FtwLiteDevice->FtwFvBlock = Fvb;\r
       //\r
@@ -726,7 +724,7 @@ InitializeFtwLite (
     }\r
 \r
     if ((FtwLiteDevice->SpareAreaAddress >= BaseAddress) &&\r
-        (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FwVolHeader->FvLength))\r
+        ((FtwLiteDevice->SpareAreaAddress + FtwLiteDevice->SpareAreaLength) <= (BaseAddress + FwVolHeader->FvLength))\r
         ) {\r
       FtwLiteDevice->FtwBackupFvb = Fvb;\r
       //\r
@@ -742,13 +740,14 @@ InitializeFtwLite (
             if ((FtwLiteDevice->SpareAreaAddress >= (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
               && (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
               //\r
-              // Get the NumberOfSpareBlock and SizeOfSpareBlock\r
+              // Get the NumberOfSpareBlock and BlockSize\r
               //\r
               FtwLiteDevice->FtwSpareLba        = LbaIndex - 1;\r
-              FtwLiteDevice->SizeOfSpareBlock   = FvbMapEntry->Length;\r
-              FtwLiteDevice->NumberOfSpareBlock = FtwLiteDevice->SpareAreaLength / FtwLiteDevice->SizeOfSpareBlock;\r
+              FtwLiteDevice->BlockSize   = FvbMapEntry->Length;\r
+              FtwLiteDevice->NumberOfSpareBlock = FtwLiteDevice->SpareAreaLength / FtwLiteDevice->BlockSize;\r
               //\r
               // Check the range of spare area to make sure that it's in FV range\r
+              // To do delete\r
               //\r
               ASSERT ((FtwLiteDevice->FtwSpareLba + FtwLiteDevice->NumberOfSpareBlock) <= FvbMapEntry->NumBlocks);\r
               break;\r
@@ -768,6 +767,7 @@ InitializeFtwLite (
       }\r
     }\r
   }\r
+\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
@@ -776,7 +776,9 @@ InitializeFtwLite (
   //\r
   FtwLiteDevice->FtwWorkBlockLba = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->NumberOfSpareBlock + 1;\r
   if ((INT64) (FtwLiteDevice->FtwWorkBlockLba) < 0) {\r
-    FtwLiteDevice->FtwWorkBlockLba = 0;\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: The spare block range is too large than the working block range!\n"));\r
+    FreePool (FtwLiteDevice);\r
+    return EFI_ABORTED;\r
   }\r
 \r
   if ((FtwLiteDevice->FtwFvBlock == NULL) ||\r
@@ -788,6 +790,7 @@ InitializeFtwLite (
     FreePool (FtwLiteDevice);\r
     return EFI_ABORTED;\r
   }\r
+\r
   //\r
   // Refresh workspace data from working block\r
   //\r
@@ -798,7 +801,7 @@ InitializeFtwLite (
   // If the working block workspace is not valid, try the spare block\r
   //\r
   if (!IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace invalid, read from backup\n"));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Workspace invalid, read from backup\n"));\r
     //\r
     // Read from spare block\r
     //\r
@@ -817,7 +820,7 @@ InitializeFtwLite (
     //\r
     if (IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
       Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart working block in Init() - %r\n", Status));\r
+      DEBUG ((EFI_D_ERROR, "FtwLite: Restart working block in Init() - %r\n", Status));\r
       ASSERT_EFI_ERROR (Status);\r
 \r
       FtwAbort (FtwLiteDevice);\r
@@ -830,7 +833,7 @@ InitializeFtwLite (
         return EFI_ABORTED;\r
       }\r
     } else {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Both are invalid, init workspace\n"));\r
+      DEBUG ((EFI_D_ERROR, "FtwLite: Both are invalid, init workspace\n"));\r
       //\r
       // If both are invalid, then initialize work space.\r
       //\r
@@ -875,7 +878,7 @@ InitializeFtwLite (
   if ((FtwLiteDevice->FtwLastRecord->WriteAllocated == FTW_VALID_STATE) &&\r
       (FtwLiteDevice->FtwLastRecord->SpareCompleted != FTW_VALID_STATE)\r
       ) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Init.. record not SpareCompleted, abort()\n"));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Init.. record not SpareCompleted, abort()\n"));\r
     FtwAbort (FtwLiteDevice);\r
   }\r
   //\r
@@ -886,7 +889,7 @@ InitializeFtwLite (
       ) {\r
 \r
     Status = FtwRestart (FtwLiteDevice);\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart last write - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -906,10 +909,10 @@ InitializeFtwLite (
         FtwLiteDevice->FtwWorkSpace + Offset,\r
         FtwLiteDevice->FtwWorkSpaceSize - Offset\r
         )) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace is dirty, call reclaim...\n"));\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Workspace is dirty, call reclaim...\n"));\r
     Status = FtwReclaimWorkSpace (FtwLiteDevice, TRUE);\r
     if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace reclaim - %r\n", Status));\r
+      DEBUG ((EFI_D_ERROR, "FtwLite: Workspace reclaim - %r\n", Status));\r
       FreePool (FtwLiteDevice);\r
       return EFI_ABORTED;\r
     }\r