diff options
Diffstat (limited to 'MdeModulePkg/Universal')
4 files changed, 491 insertions, 197 deletions
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c index 714b5d8..f08f280 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.c @@ -3,7 +3,7 @@ These are the common Fault Tolerant Write (FTW) functions that are shared
by DXE FTW driver and SMM FTW driver.
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@@ -81,7 +81,6 @@ FtwAllocate ( )
{
EFI_STATUS Status;
- UINTN Length;
UINTN Offset;
EFI_FTW_DEVICE *FtwDevice;
EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
@@ -134,14 +133,14 @@ FtwAllocate ( FtwHeader->PrivateDataSize = PrivateDataSize;
FtwHeader->HeaderAllocated = FTW_VALID_STATE;
- Length = sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER);
- Status = FtwDevice->FtwFvBlock->Write (
- FtwDevice->FtwFvBlock,
- FtwDevice->FtwWorkSpaceLba,
- FtwDevice->FtwWorkSpaceBase + Offset,
- &Length,
- (UINT8 *) FtwHeader
- );
+ Status = WriteWorkSpaceData (
+ FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
+ FtwDevice->FtwWorkSpaceLba,
+ FtwDevice->FtwWorkSpaceBase + Offset,
+ sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER),
+ (UINT8 *) FtwHeader
+ );
if (EFI_ERROR (Status)) {
return EFI_ABORTED;
}
@@ -150,6 +149,7 @@ FtwAllocate ( //
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_ALLOCATED
@@ -170,13 +170,14 @@ FtwAllocate ( /**
- Write a record with fault tolerant mannaer.
+ Write a record with fault tolerant manner.
Since the content has already backuped in spare block, the write is
guaranteed to be completed with fault tolerant manner.
@param This The pointer to this protocol instance.
@param Fvb The FVB protocol that provides services for
reading, writing, and erasing the target block.
+ @param BlockSize The size of the block.
@retval EFI_SUCCESS The function completed successfully
@retval EFI_ABORTED The function could not complete successfully
@@ -185,7 +186,8 @@ FtwAllocate ( EFI_STATUS
FtwWriteRecord (
IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,
- IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb
+ IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,
+ IN UINTN BlockSize
)
{
EFI_STATUS Status;
@@ -193,12 +195,10 @@ FtwWriteRecord ( EFI_FAULT_TOLERANT_WRITE_HEADER *Header;
EFI_FAULT_TOLERANT_WRITE_RECORD *Record;
UINTN Offset;
- EFI_LBA WorkSpaceLbaOffset;
+ UINTN NumberOfWriteBlocks;
FtwDevice = FTW_CONTEXT_FROM_THIS (This);
- WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
-
//
// Spare Complete but Destination not complete,
// Recover the target block with the spare block.
@@ -218,8 +218,9 @@ FtwWriteRecord ( Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState (
FtwDevice->FtwBackupFvb,
- FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
- FtwDevice->FtwWorkSpaceBase + Offset,
+ FtwDevice->SpareBlockSize,
+ FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
+ FtwDevice->FtwWorkSpaceBaseInSpare + Offset,
SPARE_COMPLETED
);
if (EFI_ERROR (Status)) {
@@ -227,7 +228,7 @@ FtwWriteRecord ( }
Status = FlushSpareBlockToWorkingBlock (FtwDevice);
- } else if (IsBootBlock (FtwDevice, Fvb, Record->Lba)) {
+ } else if (IsBootBlock (FtwDevice, Fvb)) {
//
// Update boot block
//
@@ -236,7 +237,8 @@ FtwWriteRecord ( //
// Update blocks other than working block or boot block
//
- Status = FlushSpareBlockToTargetBlock (FtwDevice, Fvb, Record->Lba);
+ NumberOfWriteBlocks = FTW_BLOCKS ((UINTN) (Record->Offset + Record->Length), BlockSize);
+ Status = FlushSpareBlockToTargetBlock (FtwDevice, Fvb, Record->Lba, BlockSize, NumberOfWriteBlocks);
}
if (EFI_ERROR (Status)) {
@@ -248,6 +250,7 @@ FtwWriteRecord ( Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset,
DEST_COMPLETED
@@ -266,6 +269,7 @@ FtwWriteRecord ( Offset = (UINT8 *) Header - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_COMPLETED
@@ -330,6 +334,10 @@ FtwWrite ( UINTN Index;
UINT8 *Ptr;
EFI_PHYSICAL_ADDRESS FvbPhysicalAddress;
+ UINTN BlockSize;
+ UINTN NumberOfBlocks;
+ UINTN NumberOfWriteBlocks;
+ UINTN WriteLength;
FtwDevice = FTW_CONTEXT_FROM_THIS (This);
@@ -383,12 +391,7 @@ FtwWrite ( if ((Record->SpareComplete == FTW_VALID_STATE) && (Record->DestinationComplete != FTW_VALID_STATE)) {
return EFI_NOT_READY;
}
- //
- // Check if the input data can fit within the target block
- //
- if ((Offset + Length) > FtwDevice->SpareAreaLength) {
- return EFI_BAD_BUFFER_SIZE;
- }
+
//
// Get the FVB protocol by handle
//
@@ -399,15 +402,39 @@ FtwWrite ( Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);
if (EFI_ERROR (Status)) {
- DEBUG ((EFI_D_ERROR, "FtwLite: Get FVB physical address - %r\n", Status));
+ DEBUG ((EFI_D_ERROR, "Ftw: Write(), Get FVB physical address - %r\n", Status));
+ return EFI_ABORTED;
+ }
+
+ //
+ // Now, one FVB has one type of BlockSize.
+ //
+ Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);
+ if (EFI_ERROR (Status)) {
+ DEBUG ((EFI_D_ERROR, "Ftw: Write(), Get block size - %r\n", Status));
return EFI_ABORTED;
}
+ NumberOfWriteBlocks = FTW_BLOCKS (Offset + Length, BlockSize);
+ DEBUG ((EFI_D_INFO, "Ftw: Write(), BlockSize - 0x%x, NumberOfWriteBlock - 0x%x\n", BlockSize, NumberOfWriteBlocks));
+ WriteLength = NumberOfWriteBlocks * BlockSize;
+
+ //
+ // Check if the input data can fit within the spare block.
+ //
+ if (WriteLength > FtwDevice->SpareAreaLength) {
+ return EFI_BAD_BUFFER_SIZE;
+ }
+
//
// Set BootBlockUpdate FLAG if it's updating boot block.
//
- if (IsBootBlock (FtwDevice, Fvb, Lba)) {
+ if (IsBootBlock (FtwDevice, Fvb)) {
Record->BootBlockUpdate = FTW_VALID_STATE;
+ //
+ // Boot Block and Spare Block should have same block size and block numbers.
+ //
+ ASSERT ((BlockSize == FtwDevice->SpareBlockSize) && (NumberOfWriteBlocks == FtwDevice->NumberOfSpareBlock));
}
//
// Write the record to the work space.
@@ -415,7 +442,7 @@ FtwWrite ( Record->Lba = Lba;
Record->Offset = Offset;
Record->Length = Length;
- Record->RelativeOffset = (INT64) (FvbPhysicalAddress + (UINTN) Lba * FtwDevice->BlockSize) - (INT64) FtwDevice->SpareAreaAddress;
+ Record->RelativeOffset = (INT64) (FvbPhysicalAddress + (UINTN) Lba * BlockSize) - (INT64) FtwDevice->SpareAreaAddress;
if (PrivateData != NULL) {
CopyMem ((Record + 1), PrivateData, (UINTN) Header->PrivateDataSize);
}
@@ -423,13 +450,14 @@ FtwWrite ( MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
MyLength = FTW_RECORD_SIZE (Header->PrivateDataSize);
- Status = FtwDevice->FtwFvBlock->Write (
- FtwDevice->FtwFvBlock,
- FtwDevice->FtwWorkSpaceLba,
- FtwDevice->FtwWorkSpaceBase + MyOffset,
- &MyLength,
- (UINT8 *) Record
- );
+ Status = WriteWorkSpaceData (
+ FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
+ FtwDevice->FtwWorkSpaceLba,
+ FtwDevice->FtwWorkSpaceBase + MyOffset,
+ MyLength,
+ (UINT8 *) Record
+ );
if (EFI_ERROR (Status)) {
return EFI_ABORTED;
}
@@ -439,7 +467,7 @@ FtwWrite ( //
// Allocate a memory buffer
//
- MyBufferSize = FtwDevice->SpareAreaLength;
+ MyBufferSize = WriteLength;
MyBuffer = AllocatePool (MyBufferSize);
if (MyBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
@@ -448,8 +476,8 @@ FtwWrite ( // Read all original data from target block to memory buffer
//
Ptr = MyBuffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- MyLength = FtwDevice->BlockSize;
+ for (Index = 0; Index < NumberOfWriteBlocks; Index += 1) {
+ MyLength = BlockSize;
Status = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);
if (EFI_ERROR (Status)) {
FreePool (MyBuffer);
@@ -477,7 +505,7 @@ FtwWrite ( Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- MyLength = FtwDevice->BlockSize;
+ MyLength = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -495,11 +523,16 @@ FtwWrite ( }
//
// Write the memory buffer to spare block
+ // Do not assume Spare Block and Target Block have same block size
//
Status = FtwEraseSpareBlock (FtwDevice);
Ptr = MyBuffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- MyLength = FtwDevice->BlockSize;
+ for (Index = 0; MyBufferSize > 0; Index += 1) {
+ if (MyBufferSize > FtwDevice->SpareBlockSize) {
+ MyLength = FtwDevice->SpareBlockSize;
+ } else {
+ MyLength = MyBufferSize;
+ }
Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -514,6 +547,7 @@ FtwWrite ( }
Ptr += MyLength;
+ MyBufferSize -= MyLength;
}
//
// Free MyBuffer
@@ -526,6 +560,7 @@ FtwWrite ( MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + MyOffset,
SPARE_COMPLETED
@@ -541,7 +576,7 @@ FtwWrite ( // Since the content has already backuped in spare block, the write is
// guaranteed to be completed with fault tolerant manner.
//
- Status = FtwWriteRecord (This, Fvb);
+ Status = FtwWriteRecord (This, Fvb, BlockSize);
if (EFI_ERROR (Status)) {
FreePool (SpareBuffer);
return EFI_ABORTED;
@@ -552,7 +587,7 @@ FtwWrite ( Status = FtwEraseSpareBlock (FtwDevice);
Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- MyLength = FtwDevice->BlockSize;
+ MyLength = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -609,6 +644,8 @@ FtwRestart ( EFI_FAULT_TOLERANT_WRITE_HEADER *Header;
EFI_FAULT_TOLERANT_WRITE_RECORD *Record;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
+ UINTN BlockSize;
+ UINTN NumberOfBlocks;
FtwDevice = FTW_CONTEXT_FROM_THIS (This);
@@ -630,6 +667,15 @@ FtwRestart ( }
//
+ // Now, one FVB has one type of BlockSize
+ //
+ Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);
+ if (EFI_ERROR (Status)) {
+ DEBUG ((EFI_D_ERROR, "Ftw: Restart(), Get block size - %r\n", Status));
+ return EFI_ABORTED;
+ }
+
+ //
// Check the COMPLETE flag of last write header
//
if (Header->Complete == FTW_VALID_STATE) {
@@ -651,7 +697,7 @@ FtwRestart ( // Since the content has already backuped in spare block, the write is
// guaranteed to be completed with fault tolerant manner.
//
- Status = FtwWriteRecord (This, Fvb);
+ Status = FtwWriteRecord (This, Fvb, BlockSize);
if (EFI_ERROR (Status)) {
return EFI_ABORTED;
}
@@ -706,6 +752,7 @@ FtwAbort ( Offset = (UINT8 *) FtwDevice->FtwLastWriteHeader - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_COMPLETED
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h index a460694..849d1f4 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h @@ -1,9 +1,9 @@ /** @file
The internal header file includes the common header files, defines
- internal structure and functions used by FtwLite module.
+ internal structure and functions used by Ftw module.
-Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@@ -50,6 +50,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define SPARE_COMPLETED 0x2
#define DEST_COMPLETED 0x4
+#define FTW_BLOCKS(Length, BlockSize) ((UINTN) ((Length) / (BlockSize) + (((Length) & ((BlockSize) - 1)) ? 1 : 0)))
#define FTW_DEVICE_SIGNATURE SIGNATURE_32 ('F', 'T', 'W', 'D')
@@ -63,9 +64,11 @@ typedef struct { EFI_PHYSICAL_ADDRESS WorkSpaceAddress; // Base address of working space range in flash.
EFI_PHYSICAL_ADDRESS SpareAreaAddress; // Base address of spare range in flash.
UINTN WorkSpaceLength; // Size of working space range in flash.
+ UINTN NumberOfWorkSpaceBlock; // Number of the blocks in work block for work space.
+ UINTN WorkBlockSize; // Block size in bytes of the work blocks in flash
UINTN SpareAreaLength; // Size of spare range in flash.
UINTN NumberOfSpareBlock; // Number of the blocks in spare block.
- UINTN BlockSize; // Block size in bytes of the blocks in flash
+ UINTN SpareBlockSize; // Block size in bytes of the spare blocks in flash
EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;// Pointer to Working Space Header in memory buffer
EFI_FAULT_TOLERANT_WRITE_HEADER *FtwLastWriteHeader;// Pointer to last record header in memory buffer
EFI_FAULT_TOLERANT_WRITE_RECORD *FtwLastWriteRecord;// Pointer to last record in memory buffer
@@ -73,9 +76,12 @@ typedef struct { EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwBackupFvb; // FVB of spare block
EFI_LBA FtwSpareLba; // Start LBA of spare block
EFI_LBA FtwWorkBlockLba; // Start LBA of working block that contains working space in its last block.
+ UINTN NumberOfWorkBlock; // Number of the blocks in work block.
EFI_LBA FtwWorkSpaceLba; // Start LBA of working space
UINTN FtwWorkSpaceBase; // Offset into the FtwWorkSpaceLba block.
UINTN FtwWorkSpaceSize; // Size of working space range that stores write record.
+ EFI_LBA FtwWorkSpaceLbaInSpare; // Start LBA of working space in spare block.
+ UINTN FtwWorkSpaceBaseInSpare;// Offset into the FtwWorkSpaceLbaInSpare block.
UINT8 *FtwWorkSpace; // Point to Work Space in memory buffer
//
// Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
@@ -335,7 +341,6 @@ IsWorkingBlock ( @param FtwDevice The private data of FTW driver
@param FvBlock Fvb protocol instance
- @param Lba The block specified
@return A BOOLEAN value indicating in boot block or not.
@@ -343,19 +348,20 @@ IsWorkingBlock ( BOOLEAN
IsBootBlock (
EFI_FTW_DEVICE *FtwDevice,
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
- EFI_LBA Lba
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock
);
/**
Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
- Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
- Target block is accessed by FvbBlock protocol interface. LBA is Lba.
+ Spare block is accessed by FTW backup FVB protocol interface.
+ Target block is accessed by FvBlock protocol interface.
@param FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface to access target block
@param Lba Lba of the target block
+ @param BlockSize The size of the block
+ @param NumberOfBlocks The number of consecutive blocks starting with Lba
@retval EFI_SUCCESS Spare block content is copied to target block
@retval EFI_INVALID_PARAMETER Input parameter error
@@ -367,7 +373,9 @@ EFI_STATUS FlushSpareBlockToTargetBlock (
EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
- EFI_LBA Lba
+ EFI_LBA Lba,
+ UINTN BlockSize,
+ UINTN NumberOfBlocks
);
/**
@@ -395,8 +403,8 @@ FlushSpareBlockToWorkingBlock ( /**
Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
- Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
- Target block is accessed by FvbBlock protocol interface. LBA is Lba.
+ Spare block is accessed by FTW working FVB protocol interface.
+ Target block is accessed by FvBlock protocol interface.
FTW will do extra work on boot block update.
FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
@@ -405,7 +413,7 @@ FlushSpareBlockToWorkingBlock ( 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
that boot block will be update. It shall add a FLAG in the working block.
2. When spare block is ready,
- 3. SetSwapState(EFI_SWAPPED)
+ 3. SetSwapState(SWAPPED)
4. erasing boot block,
5. programming boot block until the boot block is ok.
6. SetSwapState(UNSWAPPED)
@@ -431,6 +439,7 @@ FlushSpareBlockToBootBlock ( @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
+ @param BlockSize The size of the block
@param Lba Lba of a block
@param Offset Offset on the Lba
@param NewBit New value that will override the old value if it can be change
@@ -445,6 +454,7 @@ FlushSpareBlockToBootBlock ( EFI_STATUS
FtwUpdateFvState (
IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
IN EFI_LBA Lba,
IN UINTN Offset,
IN UINT8 NewBit
@@ -614,7 +624,7 @@ FtwReclaimWorkSpace ( /**
- Get firmware block by address.
+ Get firmware volume block by address.
@param Address Address specified the block
@@ -707,5 +717,53 @@ VOID InitializeLocalWorkSpaceHeader (
VOID
);
-
+
+/**
+ Read work space data from work block or spare block.
+
+ @param FvBlock FVB Protocol interface to access the block.
+ @param BlockSize The size of the block.
+ @param Lba Lba of the block.
+ @param Offset The offset within the block.
+ @param Length The number of bytes to read from the block.
+ @param Buffer The data is read.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ABORTED The function could not complete successfully.
+
+**/
+EFI_STATUS
+ReadWorkSpaceData (
+ IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
+ IN EFI_LBA Lba,
+ IN UINTN Offset,
+ IN UINTN Length,
+ OUT UINT8 *Buffer
+ );
+
+/**
+ Write data to work block.
+
+ @param FvBlock FVB Protocol interface to access the block.
+ @param BlockSize The size of the block.
+ @param Lba Lba of the block.
+ @param Offset The offset within the block to place the data.
+ @param Length The number of bytes to write to the block.
+ @param Buffer The data to write.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ABORTED The function could not complete successfully.
+
+**/
+EFI_STATUS
+WriteWorkSpaceData (
+ IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
+ IN EFI_LBA Lba,
+ IN UINTN Offset,
+ IN UINTN Length,
+ IN UINT8 *Buffer
+ );
+
#endif
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c index b24d66d..0922321 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c @@ -48,12 +48,13 @@ IsErasedFlashBuffer ( }
/**
- To erase the block with the spare block size.
+ To erase the block with specified blocks.
@param FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface
@param Lba Lba of the firmware block
+ @param NumberOfBlocks The number of consecutive blocks starting with Lba
@retval EFI_SUCCESS Block LBA is Erased successfully
@retval Others Error occurs
@@ -63,13 +64,14 @@ EFI_STATUS FtwEraseBlock (
IN EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
- EFI_LBA Lba
+ EFI_LBA Lba,
+ UINTN NumberOfBlocks
)
{
return FvBlock->EraseBlocks (
FvBlock,
Lba,
- FtwDevice->NumberOfSpareBlock,
+ NumberOfBlocks,
EFI_LBA_LIST_TERMINATOR
);
}
@@ -137,7 +139,7 @@ IsWorkingBlock ( /**
- Get firmware block by address.
+ Get firmware volume block by address.
@param Address Address specified the block
@@ -159,8 +161,9 @@ GetFvbByAddress ( UINTN Index;
EFI_PHYSICAL_ADDRESS FvbBaseAddress;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
- EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
EFI_HANDLE FvbHandle;
+ UINTN BlockSize;
+ UINTN NumberOfBlocks;
*FvBlock = NULL;
FvbHandle = NULL;
@@ -188,8 +191,15 @@ GetFvbByAddress ( continue;
}
- FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
- if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {
+ //
+ // Now, one FVB has one type of BlockSize
+ //
+ Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);
+ if (EFI_ERROR (Status)) {
+ continue;
+ }
+
+ if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
*FvBlock = Fvb;
FvbHandle = HandleBuffer[Index];
break;
@@ -206,7 +216,6 @@ GetFvbByAddress ( @param FtwDevice The private data of FTW driver
@param FvBlock Fvb protocol instance
- @param Lba The block specified
@return A BOOLEAN value indicating in boot block or not.
@@ -214,8 +223,7 @@ GetFvbByAddress ( BOOLEAN
IsBootBlock (
EFI_FTW_DEVICE *FtwDevice,
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
- EFI_LBA Lba
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock
)
{
EFI_STATUS Status;
@@ -274,8 +282,8 @@ IsBootBlock ( /**
Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.
- Spare block is accessed by FTW working FVB protocol interface. LBA is 1.
- Target block is accessed by FvbBlock protocol interface. LBA is Lba.
+ Spare block is accessed by FTW working FVB protocol interface.
+ Target block is accessed by FvBlock protocol interface.
FTW will do extra work on boot block update.
FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL,
@@ -284,7 +292,7 @@ IsBootBlock ( 1. GetRangeLocation(), if the Range is inside the boot block, FTW know
that boot block will be update. It shall add a FLAG in the working block.
2. When spare block is ready,
- 3. SetSwapState(EFI_SWAPPED)
+ 3. SetSwapState(SWAPPED)
4. erasing boot block,
5. programming boot block until the boot block is ok.
6. SetSwapState(UNSWAPPED)
@@ -357,7 +365,7 @@ FlushSpareBlockToBootBlock ( BootLba = 0;
Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ Count = FtwDevice->SpareBlockSize;
Status = BootFvb->Read (
BootFvb,
BootLba + Index,
@@ -378,7 +386,7 @@ FlushSpareBlockToBootBlock ( //
Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -416,7 +424,7 @@ FlushSpareBlockToBootBlock ( //
Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -444,14 +452,16 @@ FlushSpareBlockToBootBlock ( }
/**
- Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.
- Spare block is accessed by FTW backup FVB protocol interface. LBA is 1.
- Target block is accessed by FvbBlock protocol interface. LBA is Lba.
+ Copy the content of spare block to a target block.
+ Spare block is accessed by FTW backup FVB protocol interface.
+ Target block is accessed by FvBlock protocol interface.
@param FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface to access target block
@param Lba Lba of the target block
+ @param BlockSize The size of the block
+ @param NumberOfBlocks The number of consecutive blocks starting with Lba
@retval EFI_SUCCESS Spare block content is copied to target block
@retval EFI_INVALID_PARAMETER Input parameter error
@@ -463,7 +473,9 @@ EFI_STATUS FlushSpareBlockToTargetBlock (
EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
- EFI_LBA Lba
+ EFI_LBA Lba,
+ UINTN BlockSize,
+ UINTN NumberOfBlocks
)
{
EFI_STATUS Status;
@@ -489,7 +501,7 @@ FlushSpareBlockToTargetBlock ( //
Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -507,17 +519,17 @@ FlushSpareBlockToTargetBlock ( //
// Erase the target block
//
- Status = FtwEraseBlock (FtwDevice, FvBlock, Lba);
+ Status = FtwEraseBlock (FtwDevice, FvBlock, Lba, NumberOfBlocks);
if (EFI_ERROR (Status)) {
FreePool (Buffer);
return EFI_ABORTED;
}
//
- // Write memory buffer to block, using the FvbBlock protocol interface
+ // Write memory buffer to block, using the FvBlock protocol interface
//
Ptr = Buffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ for (Index = 0; Index < NumberOfBlocks; Index += 1) {
+ Count = BlockSize;
Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
@@ -563,7 +575,6 @@ FlushSpareBlockToWorkingBlock ( UINTN Count;
UINT8 *Ptr;
UINTN Index;
- EFI_LBA WorkSpaceLbaOffset;
//
// Allocate a memory buffer
@@ -574,8 +585,6 @@ FlushSpareBlockToWorkingBlock ( return EFI_OUT_OF_RESOURCES;
}
- WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
-
//
// To guarantee that the WorkingBlockValid is set on spare block
//
@@ -585,8 +594,9 @@ FlushSpareBlockToWorkingBlock ( //
FtwUpdateFvState (
FtwDevice->FtwBackupFvb,
- FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
- FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
+ FtwDevice->SpareBlockSize,
+ FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
+ FtwDevice->FtwWorkSpaceBaseInSpare + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID
);
//
@@ -594,7 +604,7 @@ FlushSpareBlockToWorkingBlock ( //
Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -612,7 +622,7 @@ FlushSpareBlockToWorkingBlock ( //
// Clear the CRC and STATE, copy data from spare to working block.
//
- WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize + FtwDevice->FtwWorkSpaceBase);
+ WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) FtwDevice->FtwWorkSpaceLbaInSpare * FtwDevice->SpareBlockSize + FtwDevice->FtwWorkSpaceBaseInSpare);
InitWorkSpaceHeader (WorkingBlockHeader);
WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;
WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;
@@ -629,6 +639,7 @@ FlushSpareBlockToWorkingBlock ( //
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_INVALID
@@ -643,17 +654,17 @@ FlushSpareBlockToWorkingBlock ( //
// Erase the working block
//
- Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba);
+ Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba, FtwDevice->NumberOfWorkBlock);
if (EFI_ERROR (Status)) {
FreePool (Buffer);
return EFI_ABORTED;
}
//
- // Write memory buffer to working block, using the FvbBlock protocol interface
+ // Write memory buffer to working block, using the FvBlock protocol interface
//
Ptr = Buffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Count = FtwDevice->BlockSize;
+ for (Index = 0; Index < FtwDevice->NumberOfWorkBlock; Index += 1) {
+ Count = FtwDevice->WorkBlockSize;
Status = FtwDevice->FtwFvBlock->Write (
FtwDevice->FtwFvBlock,
FtwDevice->FtwWorkBlockLba + Index,
@@ -682,6 +693,7 @@ FlushSpareBlockToWorkingBlock ( //
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID
@@ -703,6 +715,7 @@ FlushSpareBlockToWorkingBlock ( @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
+ @param BlockSize The size of the block
@param Lba Lba of a block
@param Offset Offset on the Lba
@param NewBit New value that will override the old value if it can be change
@@ -717,6 +730,7 @@ FlushSpareBlockToWorkingBlock ( EFI_STATUS
FtwUpdateFvState (
IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
IN EFI_LBA Lba,
IN UINTN Offset,
IN UINT8 NewBit
@@ -727,6 +741,14 @@ FtwUpdateFvState ( UINTN Length;
//
+ // Calculate the real Offset and Lba to write.
+ //
+ while (Offset >= BlockSize) {
+ Offset -= BlockSize;
+ Lba++;
+ }
+
+ //
// Read state from device, assume State is only one byte.
//
Length = sizeof (UINT8);
@@ -1015,10 +1037,10 @@ FindFvbForFtw ( UINTN Index;
EFI_PHYSICAL_ADDRESS FvbBaseAddress;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
- EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
EFI_FVB_ATTRIBUTES_2 Attributes;
- EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;
UINT32 LbaIndex;
+ UINTN BlockSize;
+ UINTN NumberOfBlocks;
HandleBuffer = NULL;
@@ -1056,80 +1078,92 @@ FindFvbForFtw ( continue;
}
- FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);
+ //
+ // Now, one FVB has one type of BlockSize.
+ //
+ Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);
+ if (EFI_ERROR (Status)) {
+ continue;
+ }
+
if ((FtwDevice->FtwFvBlock == NULL) && (FtwDevice->WorkSpaceAddress >= FvbBaseAddress) &&
- ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + FwVolHeader->FvLength))
- ) {
+ ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
FtwDevice->FtwFvBlock = Fvb;
//
// To get the LBA of work space
//
- if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
- //
- // Now, one FV has one type of BlockLength
- //
- FvbMapEntry = &FwVolHeader->BlockMap[0];
- for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
- if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))
- && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {
- FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;
+ for (LbaIndex = 1; LbaIndex <= NumberOfBlocks; LbaIndex += 1) {
+ if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + BlockSize * (LbaIndex - 1)))
+ && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + BlockSize * LbaIndex))) {
+ FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;
+ //
+ // Get the Work space size and Base(Offset)
+ //
+ FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;
+ FtwDevice->WorkBlockSize = BlockSize;
+ FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FtwDevice->WorkBlockSize * (LbaIndex - 1)));
+ FtwDevice->NumberOfWorkSpaceBlock = FTW_BLOCKS (FtwDevice->FtwWorkSpaceBase + FtwDevice->FtwWorkSpaceSize, FtwDevice->WorkBlockSize);
+ if (FtwDevice->FtwWorkSpaceSize >= FtwDevice->WorkBlockSize) {
//
- // Get the Work space size and Base(Offset)
+ // Check the alignment of work space address and length, they should be block size aligned when work space size is larger than one block size.
//
- FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;
- FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));
- break;
+ if (((FtwDevice->WorkSpaceAddress & (FtwDevice->WorkBlockSize - 1)) != 0) ||
+ ((FtwDevice->WorkSpaceLength & (FtwDevice->WorkBlockSize - 1)) != 0)) {
+ DEBUG ((EFI_D_ERROR, "Ftw: Work space address or length is not block size aligned when work space size is larger than one block size\n"));
+ FreePool (HandleBuffer);
+ ASSERT (FALSE);
+ return EFI_ABORTED;
+ }
+ } else if ((FtwDevice->FtwWorkSpaceBase + FtwDevice->FtwWorkSpaceSize) > FtwDevice->WorkBlockSize) {
+ DEBUG ((EFI_D_ERROR, "Ftw: The work space range should not span blocks when work space size is less than one block size\n"));
+ FreePool (HandleBuffer);
+ ASSERT (FALSE);
+ return EFI_ABORTED;
}
+ break;
}
}
}
-
+
if ((FtwDevice->FtwBackupFvb == NULL) && (FtwDevice->SpareAreaAddress >= FvbBaseAddress) &&
- ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + FwVolHeader->FvLength))
- ) {
+ ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
FtwDevice->FtwBackupFvb = Fvb;
//
// To get the LBA of spare
//
- if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
- //
- // Now, one FV has one type of BlockLength
- //
- FvbMapEntry = &FwVolHeader->BlockMap[0];
- for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
- if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))
- && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex))) {
- //
- // Get the NumberOfSpareBlock and BlockSize
- //
- FtwDevice->FtwSpareLba = LbaIndex - 1;
- FtwDevice->BlockSize = FvbMapEntry->Length;
- FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize;
- //
- // Check the range of spare area to make sure that it's in FV range
- //
- if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) {
- DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));
- FreePool (HandleBuffer);
- ASSERT (FALSE);
- return EFI_ABORTED;
- }
+ for (LbaIndex = 1; LbaIndex <= NumberOfBlocks; LbaIndex += 1) {
+ if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + BlockSize * (LbaIndex - 1)))
+ && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + BlockSize * LbaIndex))) {
+ //
+ // Get the NumberOfSpareBlock and BlockSize
+ //
+ FtwDevice->FtwSpareLba = LbaIndex - 1;
+ FtwDevice->SpareBlockSize = BlockSize;
+ FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->SpareBlockSize;
+ //
+ // Check the range of spare area to make sure that it's in FV range
+ //
+ if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > NumberOfBlocks) {
+ DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));
+ FreePool (HandleBuffer);
+ ASSERT (FALSE);
+ return EFI_ABORTED;
+ }
+ //
+ // Check the alignment of spare area address and length, they should be block size aligned
+ //
+ if (((FtwDevice->SpareAreaAddress & (FtwDevice->SpareBlockSize - 1)) != 0) ||
+ ((FtwDevice->SpareAreaLength & (FtwDevice->SpareBlockSize - 1)) != 0)) {
+ DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n"));
+ FreePool (HandleBuffer);
//
- // Check the alignment of spare area address and length, they should be block size aligned
+ // Report Status Code EFI_SW_EC_ABORTED.
//
- if (((FtwDevice->SpareAreaAddress & (FtwDevice->BlockSize - 1)) != 0) ||
- ((FtwDevice->SpareAreaLength & (FtwDevice->BlockSize - 1)) != 0)) {
- DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n"));
- FreePool (HandleBuffer);
- //
- // Report Status Code EFI_SW_EC_ABORTED.
- //
- REPORT_STATUS_CODE ( (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED), (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ABORTED));
- ASSERT (FALSE);
- CpuDeadLoop ();
- }
- break;
+ REPORT_STATUS_CODE ((EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED), (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ABORTED));
+ ASSERT (FALSE);
+ CpuDeadLoop ();
}
+ break;
}
}
}
@@ -1140,6 +1174,8 @@ FindFvbForFtw ( (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) {
return EFI_ABORTED;
}
+ DEBUG ((EFI_D_INFO, "Ftw: FtwWorkSpaceLba - 0x%lx, WorkBlockSize - 0x%x, FtwWorkSpaceBase - 0x%x\n", FtwDevice->FtwWorkSpaceLba, FtwDevice->WorkBlockSize, FtwDevice->FtwWorkSpaceBase));
+ DEBUG ((EFI_D_INFO, "Ftw: FtwSpareLba - 0x%lx, SpareBlockSize - 0x%x\n", FtwDevice->FtwSpareLba, FtwDevice->SpareBlockSize));
return EFI_SUCCESS;
}
@@ -1161,7 +1197,6 @@ InitFtwProtocol ( {
EFI_STATUS Status;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
- UINTN Length;
EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
UINTN Offset;
EFI_HANDLE FvbHandle;
@@ -1174,15 +1209,38 @@ InitFtwProtocol ( if (EFI_ERROR (Status)) {
return EFI_NOT_FOUND;
}
-
+
+ //
+ // Calculate the start LBA of working block.
+ //
+ if (FtwDevice->FtwWorkSpaceSize >= FtwDevice->WorkBlockSize) {
+ //
+ // Working block is a standalone area which only contains working space.
+ //
+ FtwDevice->NumberOfWorkBlock = FtwDevice->NumberOfWorkSpaceBlock;
+ } else {
+ //
+ // Working block is an area which
+ // contains working space in its last block and has the same size as spare
+ // block, unless there are not enough blocks before the block that contains
+ // working space.
+ //
+ FtwDevice->NumberOfWorkBlock = (UINTN) (FtwDevice->FtwWorkSpaceLba + FtwDevice->NumberOfWorkSpaceBlock);
+ while (FtwDevice->NumberOfWorkBlock * FtwDevice->WorkBlockSize > FtwDevice->SpareAreaLength) {
+ FtwDevice->NumberOfWorkBlock--;
+ }
+ }
+ FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba + FtwDevice->NumberOfWorkSpaceBlock - FtwDevice->NumberOfWorkBlock;
+ DEBUG ((EFI_D_INFO, "Ftw: NumberOfWorkBlock - 0x%x, FtwWorkBlockLba - 0x%lx\n", FtwDevice->NumberOfWorkBlock, FtwDevice->FtwWorkBlockLba));
+
//
- // Calculate the start LBA of working block. Working block is an area which
- // contains working space in its last block and has the same size as spare
- // block, unless there are not enough blocks before the block that contains
- // working space.
+ // Calcualte the LBA and base of work space in spare block.
+ // Note: Do not assume Spare Block and Work Block have same block size.
//
- FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1;
- ASSERT ((INT64) (FtwDevice->FtwWorkBlockLba) >= 0);
+ WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
+ FtwDevice->FtwWorkSpaceLbaInSpare = (EFI_LBA) (((UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize + FtwDevice->FtwWorkSpaceBase) / FtwDevice->SpareBlockSize);
+ FtwDevice->FtwWorkSpaceBaseInSpare = ((UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize + FtwDevice->FtwWorkSpaceBase) % FtwDevice->SpareBlockSize;
+ DEBUG ((EFI_D_INFO, "Ftw: WorkSpaceLbaInSpare - 0x%lx, WorkSpaceBaseInSpare - 0x%x\n", FtwDevice->FtwWorkSpaceLbaInSpare, FtwDevice->FtwWorkSpaceBaseInSpare));
//
// Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
@@ -1207,15 +1265,14 @@ InitFtwProtocol ( //
// Read from spare block
//
- WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
- Length = FtwDevice->FtwWorkSpaceSize;
- Status = FtwDevice->FtwBackupFvb->Read (
- FtwDevice->FtwBackupFvb,
- FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
- FtwDevice->FtwWorkSpaceBase,
- &Length,
- FtwDevice->FtwWorkSpace
- );
+ Status = ReadWorkSpaceData (
+ FtwDevice->FtwBackupFvb,
+ FtwDevice->SpareBlockSize,
+ FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
+ FtwDevice->FtwWorkSpaceBaseInSpare,
+ FtwDevice->FtwWorkSpaceSize,
+ FtwDevice->FtwWorkSpace
+ );
ASSERT_EFI_ERROR (Status);
//
@@ -1304,7 +1361,7 @@ InitFtwProtocol ( FvbHandle = GetFvbByAddress ((EFI_PHYSICAL_ADDRESS) (UINTN) ((INT64) FtwDevice->SpareAreaAddress + FtwDevice->FtwLastWriteRecord->RelativeOffset), &Fvb);
if (FvbHandle != NULL) {
Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle);
- DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));
+ DEBUG ((EFI_D_ERROR, "Ftw: Restart last write - %r\n", Status));
ASSERT_EFI_ERROR (Status);
}
FtwAbort (&FtwDevice->FtwInstance);
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c index a5fa12b..29252c4 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c @@ -126,6 +126,132 @@ InitWorkSpaceHeader ( }
/**
+ Read work space data from work block or spare block.
+
+ @param FtwDevice The private data of FTW driver.
+ @param FvBlock FVB Protocol interface to access the block.
+ @param BlockSize The size of the block.
+ @param Lba Lba of the block.
+ @param Offset The offset within the block.
+ @param Length The number of bytes to read from the block.
+ @param Buffer The data is read.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ABORTED The function could not complete successfully.
+
+**/
+EFI_STATUS
+ReadWorkSpaceData (
+ IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
+ IN EFI_LBA Lba,
+ IN UINTN Offset,
+ IN UINTN Length,
+ OUT UINT8 *Buffer
+ )
+{
+ EFI_STATUS Status;
+ UINT8 *Ptr;
+ UINTN MyLength;
+
+ //
+ // Calculate the real Offset and Lba to write.
+ //
+ while (Offset >= BlockSize) {
+ Offset -= BlockSize;
+ Lba++;
+ }
+
+ Ptr = Buffer;
+ while (Length > 0) {
+ if ((Offset + Length) > BlockSize) {
+ MyLength = BlockSize - Offset;
+ } else {
+ MyLength = Length;
+ }
+
+ Status = FvBlock->Read (
+ FvBlock,
+ Lba,
+ Offset,
+ &MyLength,
+ Ptr
+ );
+ if (EFI_ERROR (Status)) {
+ return EFI_ABORTED;
+ }
+ Offset = 0;
+ Length -= MyLength;
+ Ptr += MyLength;
+ Lba++;
+ }
+
+ return EFI_SUCCESS;
+}
+
+/**
+ Write work space data to work block.
+
+ @param FvBlock FVB Protocol interface to access the block.
+ @param BlockSize The size of the block.
+ @param Lba Lba of the block.
+ @param Offset The offset within the block to place the data.
+ @param Length The number of bytes to write to the block.
+ @param Buffer The data to write.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ABORTED The function could not complete successfully.
+
+**/
+EFI_STATUS
+WriteWorkSpaceData (
+ IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
+ IN UINTN BlockSize,
+ IN EFI_LBA Lba,
+ IN UINTN Offset,
+ IN UINTN Length,
+ IN UINT8 *Buffer
+ )
+{
+ EFI_STATUS Status;
+ UINT8 *Ptr;
+ UINTN MyLength;
+
+ //
+ // Calculate the real Offset and Lba to write.
+ //
+ while (Offset >= BlockSize) {
+ Offset -= BlockSize;
+ Lba++;
+ }
+
+ Ptr = Buffer;
+ while (Length > 0) {
+ if ((Offset + Length) > BlockSize) {
+ MyLength = BlockSize - Offset;
+ } else {
+ MyLength = Length;
+ }
+
+ Status = FvBlock->Write (
+ FvBlock,
+ Lba,
+ Offset,
+ &MyLength,
+ Ptr
+ );
+ if (EFI_ERROR (Status)) {
+ return EFI_ABORTED;
+ }
+ Offset = 0;
+ Length -= MyLength;
+ Ptr += MyLength;
+ Lba++;
+ }
+ return EFI_SUCCESS;
+}
+
+/**
Read from working block to refresh the work space in memory.
@param FtwDevice Point to private data of FTW driver
@@ -140,7 +266,6 @@ WorkSpaceRefresh ( )
{
EFI_STATUS Status;
- UINTN Length;
UINTN RemainingSpaceSize;
//
@@ -155,14 +280,14 @@ WorkSpaceRefresh ( //
// Read from working block
//
- Length = FtwDevice->FtwWorkSpaceSize;
- Status = FtwDevice->FtwFvBlock->Read (
- FtwDevice->FtwFvBlock,
- FtwDevice->FtwWorkSpaceLba,
- FtwDevice->FtwWorkSpaceBase,
- &Length,
- FtwDevice->FtwWorkSpace
- );
+ Status = ReadWorkSpaceData (
+ FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
+ FtwDevice->FtwWorkSpaceLba,
+ FtwDevice->FtwWorkSpaceBase,
+ FtwDevice->FtwWorkSpaceSize,
+ FtwDevice->FtwWorkSpace
+ );
if (EFI_ERROR (Status)) {
return EFI_ABORTED;
}
@@ -194,14 +319,14 @@ WorkSpaceRefresh ( //
// Read from working block again
//
- Length = FtwDevice->FtwWorkSpaceSize;
- Status = FtwDevice->FtwFvBlock->Read (
- FtwDevice->FtwFvBlock,
- FtwDevice->FtwWorkSpaceLba,
- FtwDevice->FtwWorkSpaceBase,
- &Length,
- FtwDevice->FtwWorkSpace
- );
+ Status = ReadWorkSpaceData (
+ FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
+ FtwDevice->FtwWorkSpaceLba,
+ FtwDevice->FtwWorkSpaceBase,
+ FtwDevice->FtwWorkSpaceSize,
+ FtwDevice->FtwWorkSpace
+ );
if (EFI_ERROR (Status)) {
return EFI_ABORTED;
}
@@ -265,15 +390,15 @@ FtwReclaimWorkSpace ( //
// Read all original data from working block to a memory buffer
//
- TempBufferSize = FtwDevice->SpareAreaLength;
+ TempBufferSize = FtwDevice->NumberOfWorkBlock * FtwDevice->WorkBlockSize;
TempBuffer = AllocateZeroPool (TempBufferSize);
if (TempBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Ptr = TempBuffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Length = FtwDevice->BlockSize;
+ for (Index = 0; Index < FtwDevice->NumberOfWorkBlock; Index += 1) {
+ Length = FtwDevice->WorkBlockSize;
Status = FtwDevice->FtwFvBlock->Read (
FtwDevice->FtwFvBlock,
FtwDevice->FtwWorkBlockLba + Index,
@@ -292,7 +417,7 @@ FtwReclaimWorkSpace ( // Clean up the workspace, remove all the completed records.
//
Ptr = TempBuffer +
- (UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize +
+ (UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
FtwDevice->FtwWorkSpaceBase;
//
@@ -348,7 +473,7 @@ FtwReclaimWorkSpace ( // Set the WorkingBlockValid and WorkingBlockInvalid as INVALID
//
WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (TempBuffer +
- (UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize +
+ (UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
FtwDevice->FtwWorkSpaceBase);
WorkingBlockHeader->WorkingBlockValid = FTW_INVALID_STATE;
WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE;
@@ -366,7 +491,7 @@ FtwReclaimWorkSpace ( Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Length = FtwDevice->BlockSize;
+ Length = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -387,8 +512,12 @@ FtwReclaimWorkSpace ( //
Status = FtwEraseSpareBlock (FtwDevice);
Ptr = TempBuffer;
- for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Length = FtwDevice->BlockSize;
+ for (Index = 0; TempBufferSize > 0; Index += 1) {
+ if (TempBufferSize > FtwDevice->SpareBlockSize) {
+ Length = FtwDevice->SpareBlockSize;
+ } else {
+ Length = TempBufferSize;
+ }
Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
@@ -403,6 +532,7 @@ FtwReclaimWorkSpace ( }
Ptr += Length;
+ TempBufferSize -= Length;
}
//
// Free TempBuffer
@@ -414,8 +544,9 @@ FtwReclaimWorkSpace ( //
Status = FtwUpdateFvState (
FtwDevice->FtwBackupFvb,
- FtwDevice->FtwSpareLba + WorkSpaceLbaOffset,
- FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
+ FtwDevice->SpareBlockSize,
+ FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
+ FtwDevice->FtwWorkSpaceBaseInSpare + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID
);
if (EFI_ERROR (Status)) {
@@ -430,6 +561,7 @@ FtwReclaimWorkSpace ( //
Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock,
+ FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_INVALID
@@ -455,7 +587,7 @@ FtwReclaimWorkSpace ( Status = FtwEraseSpareBlock (FtwDevice);
Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
- Length = FtwDevice->BlockSize;
+ Length = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index,
|