MdeModulePkg FaultTolerantWriteDxe: Remove assumptions below in FTW driver.

1. Work space and spare block must be in a FVB with FV header.
Updated to work space and spare block could be in independent FVBs that are without FV header.
2. NV region, work space and spare block must have same BlockSize.
Updated to NV region, work space and spare block could have different BlockSize.
3. Works space size must be <= one block size.
Update to work space size could be <= one block size (not span blocks) or > one block size (block size aligned).

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16201 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Star Zeng 2014-10-10 02:27:14 +00:00 committed by lzeng14
parent d2a867217d
commit 0d3edd9d26
6 changed files with 502 additions and 204 deletions

View File

@ -877,10 +877,13 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize|0x0|UINT32|0x30000014 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize|0x0|UINT32|0x30000014
## Base address of the FTW working block range in flash device. ## Base address of the FTW working block range in flash device.
# If PcdFlashNvStorageFtwWorkingSize is larger than one block size, this value should be block size aligned.
# @Prompt Base address of flash FTW working block range. # @Prompt Base address of flash FTW working block range.
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0x0|UINT32|0x30000010 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0x0|UINT32|0x30000010
## Size of the FTW working block range. ## Size of the FTW working block range.
# If the value is less than one block size, the work space range should not span blocks.
# If the value is larger than one block size, it should be block size aligned.
# @Prompt Size of flash FTW working block range. # @Prompt Size of flash FTW working block range.
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0x0|UINT32|0x30000011 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0x0|UINT32|0x30000011
@ -893,6 +896,7 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0x0|UINT64|0x80000013 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0x0|UINT64|0x80000013
## 64-bit Base address of the FTW working block range in flash device. ## 64-bit Base address of the FTW working block range in flash device.
# If PcdFlashNvStorageFtwWorkingSize is larger than one block size, this value should be block size aligned.
# @Prompt 64-bit Base address of flash FTW working block range. # @Prompt 64-bit Base address of flash FTW working block range.
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0x0|UINT64|0x80000010 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0x0|UINT64|0x80000010

Binary file not shown.

View File

@ -3,7 +3,7 @@
These are the common Fault Tolerant Write (FTW) functions that are shared These are the common Fault Tolerant Write (FTW) functions that are shared
by DXE FTW driver and SMM FTW driver. 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 This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License 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 which accompanies this distribution. The full text of the license may be found at
@ -81,7 +81,6 @@ FtwAllocate (
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
UINTN Length;
UINTN Offset; UINTN Offset;
EFI_FTW_DEVICE *FtwDevice; EFI_FTW_DEVICE *FtwDevice;
EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader; EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
@ -134,12 +133,12 @@ FtwAllocate (
FtwHeader->PrivateDataSize = PrivateDataSize; FtwHeader->PrivateDataSize = PrivateDataSize;
FtwHeader->HeaderAllocated = FTW_VALID_STATE; FtwHeader->HeaderAllocated = FTW_VALID_STATE;
Length = sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER); Status = WriteWorkSpaceData (
Status = FtwDevice->FtwFvBlock->Write (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwWorkSpaceBase + Offset,
&Length, sizeof (EFI_FAULT_TOLERANT_WRITE_HEADER),
(UINT8 *) FtwHeader (UINT8 *) FtwHeader
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -150,6 +149,7 @@ FtwAllocate (
// //
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_ALLOCATED 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 Since the content has already backuped in spare block, the write is
guaranteed to be completed with fault tolerant manner. guaranteed to be completed with fault tolerant manner.
@param This The pointer to this protocol instance. @param This The pointer to this protocol instance.
@param Fvb The FVB protocol that provides services for @param Fvb The FVB protocol that provides services for
reading, writing, and erasing the target block. reading, writing, and erasing the target block.
@param BlockSize The size of the block.
@retval EFI_SUCCESS The function completed successfully @retval EFI_SUCCESS The function completed successfully
@retval EFI_ABORTED The function could not complete successfully @retval EFI_ABORTED The function could not complete successfully
@ -185,7 +186,8 @@ FtwAllocate (
EFI_STATUS EFI_STATUS
FtwWriteRecord ( FtwWriteRecord (
IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This, 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; EFI_STATUS Status;
@ -193,12 +195,10 @@ FtwWriteRecord (
EFI_FAULT_TOLERANT_WRITE_HEADER *Header; EFI_FAULT_TOLERANT_WRITE_HEADER *Header;
EFI_FAULT_TOLERANT_WRITE_RECORD *Record; EFI_FAULT_TOLERANT_WRITE_RECORD *Record;
UINTN Offset; UINTN Offset;
EFI_LBA WorkSpaceLbaOffset; UINTN NumberOfWriteBlocks;
FtwDevice = FTW_CONTEXT_FROM_THIS (This); FtwDevice = FTW_CONTEXT_FROM_THIS (This);
WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
// //
// Spare Complete but Destination not complete, // Spare Complete but Destination not complete,
// Recover the target block with the spare block. // Recover the target block with the spare block.
@ -218,8 +218,9 @@ FtwWriteRecord (
Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace; Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + WorkSpaceLbaOffset, FtwDevice->SpareBlockSize,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
FtwDevice->FtwWorkSpaceBaseInSpare + Offset,
SPARE_COMPLETED SPARE_COMPLETED
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -227,7 +228,7 @@ FtwWriteRecord (
} }
Status = FlushSpareBlockToWorkingBlock (FtwDevice); Status = FlushSpareBlockToWorkingBlock (FtwDevice);
} else if (IsBootBlock (FtwDevice, Fvb, Record->Lba)) { } else if (IsBootBlock (FtwDevice, Fvb)) {
// //
// Update boot block // Update boot block
// //
@ -236,7 +237,8 @@ FtwWriteRecord (
// //
// Update blocks other than working block or boot block // 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)) { if (EFI_ERROR (Status)) {
@ -248,6 +250,7 @@ FtwWriteRecord (
Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace; Offset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwWorkSpaceBase + Offset,
DEST_COMPLETED DEST_COMPLETED
@ -266,6 +269,7 @@ FtwWriteRecord (
Offset = (UINT8 *) Header - FtwDevice->FtwWorkSpace; Offset = (UINT8 *) Header - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_COMPLETED WRITES_COMPLETED
@ -330,6 +334,10 @@ FtwWrite (
UINTN Index; UINTN Index;
UINT8 *Ptr; UINT8 *Ptr;
EFI_PHYSICAL_ADDRESS FvbPhysicalAddress; EFI_PHYSICAL_ADDRESS FvbPhysicalAddress;
UINTN BlockSize;
UINTN NumberOfBlocks;
UINTN NumberOfWriteBlocks;
UINTN WriteLength;
FtwDevice = FTW_CONTEXT_FROM_THIS (This); FtwDevice = FTW_CONTEXT_FROM_THIS (This);
@ -383,12 +391,7 @@ FtwWrite (
if ((Record->SpareComplete == FTW_VALID_STATE) && (Record->DestinationComplete != FTW_VALID_STATE)) { if ((Record->SpareComplete == FTW_VALID_STATE) && (Record->DestinationComplete != FTW_VALID_STATE)) {
return EFI_NOT_READY; 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 // Get the FVB protocol by handle
// //
@ -399,15 +402,39 @@ FtwWrite (
Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress); Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);
if (EFI_ERROR (Status)) { 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; 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. // Set BootBlockUpdate FLAG if it's updating boot block.
// //
if (IsBootBlock (FtwDevice, Fvb, Lba)) { if (IsBootBlock (FtwDevice, Fvb)) {
Record->BootBlockUpdate = FTW_VALID_STATE; 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. // Write the record to the work space.
@ -415,7 +442,7 @@ FtwWrite (
Record->Lba = Lba; Record->Lba = Lba;
Record->Offset = Offset; Record->Offset = Offset;
Record->Length = Length; 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) { if (PrivateData != NULL) {
CopyMem ((Record + 1), PrivateData, (UINTN) Header->PrivateDataSize); CopyMem ((Record + 1), PrivateData, (UINTN) Header->PrivateDataSize);
} }
@ -423,11 +450,12 @@ FtwWrite (
MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace; MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
MyLength = FTW_RECORD_SIZE (Header->PrivateDataSize); MyLength = FTW_RECORD_SIZE (Header->PrivateDataSize);
Status = FtwDevice->FtwFvBlock->Write ( Status = WriteWorkSpaceData (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + MyOffset, FtwDevice->FtwWorkSpaceBase + MyOffset,
&MyLength, MyLength,
(UINT8 *) Record (UINT8 *) Record
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -439,7 +467,7 @@ FtwWrite (
// //
// Allocate a memory buffer // Allocate a memory buffer
// //
MyBufferSize = FtwDevice->SpareAreaLength; MyBufferSize = WriteLength;
MyBuffer = AllocatePool (MyBufferSize); MyBuffer = AllocatePool (MyBufferSize);
if (MyBuffer == NULL) { if (MyBuffer == NULL) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
@ -448,8 +476,8 @@ FtwWrite (
// Read all original data from target block to memory buffer // Read all original data from target block to memory buffer
// //
Ptr = MyBuffer; Ptr = MyBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < NumberOfWriteBlocks; Index += 1) {
MyLength = FtwDevice->BlockSize; MyLength = BlockSize;
Status = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr); Status = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
FreePool (MyBuffer); FreePool (MyBuffer);
@ -477,7 +505,7 @@ FtwWrite (
Ptr = SpareBuffer; Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
MyLength = FtwDevice->BlockSize; MyLength = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read ( Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -495,11 +523,16 @@ FtwWrite (
} }
// //
// Write the memory buffer to spare block // Write the memory buffer to spare block
// Do not assume Spare Block and Target Block have same block size
// //
Status = FtwEraseSpareBlock (FtwDevice); Status = FtwEraseSpareBlock (FtwDevice);
Ptr = MyBuffer; Ptr = MyBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; MyBufferSize > 0; Index += 1) {
MyLength = FtwDevice->BlockSize; if (MyBufferSize > FtwDevice->SpareBlockSize) {
MyLength = FtwDevice->SpareBlockSize;
} else {
MyLength = MyBufferSize;
}
Status = FtwDevice->FtwBackupFvb->Write ( Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -514,6 +547,7 @@ FtwWrite (
} }
Ptr += MyLength; Ptr += MyLength;
MyBufferSize -= MyLength;
} }
// //
// Free MyBuffer // Free MyBuffer
@ -526,6 +560,7 @@ FtwWrite (
MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace; MyOffset = (UINT8 *) Record - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + MyOffset, FtwDevice->FtwWorkSpaceBase + MyOffset,
SPARE_COMPLETED SPARE_COMPLETED
@ -541,7 +576,7 @@ FtwWrite (
// Since the content has already backuped in spare block, the write is // Since the content has already backuped in spare block, the write is
// guaranteed to be completed with fault tolerant manner. // guaranteed to be completed with fault tolerant manner.
// //
Status = FtwWriteRecord (This, Fvb); Status = FtwWriteRecord (This, Fvb, BlockSize);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
FreePool (SpareBuffer); FreePool (SpareBuffer);
return EFI_ABORTED; return EFI_ABORTED;
@ -552,7 +587,7 @@ FtwWrite (
Status = FtwEraseSpareBlock (FtwDevice); Status = FtwEraseSpareBlock (FtwDevice);
Ptr = SpareBuffer; Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
MyLength = FtwDevice->BlockSize; MyLength = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write ( Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -609,6 +644,8 @@ FtwRestart (
EFI_FAULT_TOLERANT_WRITE_HEADER *Header; EFI_FAULT_TOLERANT_WRITE_HEADER *Header;
EFI_FAULT_TOLERANT_WRITE_RECORD *Record; EFI_FAULT_TOLERANT_WRITE_RECORD *Record;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
UINTN BlockSize;
UINTN NumberOfBlocks;
FtwDevice = FTW_CONTEXT_FROM_THIS (This); FtwDevice = FTW_CONTEXT_FROM_THIS (This);
@ -629,6 +666,15 @@ FtwRestart (
return EFI_NOT_FOUND; return EFI_NOT_FOUND;
} }
//
// 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 // Check the COMPLETE flag of last write header
// //
@ -651,7 +697,7 @@ FtwRestart (
// Since the content has already backuped in spare block, the write is // Since the content has already backuped in spare block, the write is
// guaranteed to be completed with fault tolerant manner. // guaranteed to be completed with fault tolerant manner.
// //
Status = FtwWriteRecord (This, Fvb); Status = FtwWriteRecord (This, Fvb, BlockSize);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return EFI_ABORTED; return EFI_ABORTED;
} }
@ -706,6 +752,7 @@ FtwAbort (
Offset = (UINT8 *) FtwDevice->FtwLastWriteHeader - FtwDevice->FtwWorkSpace; Offset = (UINT8 *) FtwDevice->FtwLastWriteHeader - FtwDevice->FtwWorkSpace;
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + Offset, FtwDevice->FtwWorkSpaceBase + Offset,
WRITES_COMPLETED WRITES_COMPLETED

View File

@ -1,9 +1,9 @@
/** @file /** @file
The internal header file includes the common header files, defines 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 This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License 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 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 SPARE_COMPLETED 0x2
#define DEST_COMPLETED 0x4 #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') #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 WorkSpaceAddress; // Base address of working space range in flash.
EFI_PHYSICAL_ADDRESS SpareAreaAddress; // Base address of spare range in flash. EFI_PHYSICAL_ADDRESS SpareAreaAddress; // Base address of spare range in flash.
UINTN WorkSpaceLength; // Size of working space 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 SpareAreaLength; // Size of spare range in flash.
UINTN NumberOfSpareBlock; // Number of the blocks in spare block. 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_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_HEADER *FtwLastWriteHeader;// Pointer to last record header in memory buffer
EFI_FAULT_TOLERANT_WRITE_RECORD *FtwLastWriteRecord;// Pointer to last record 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_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FtwBackupFvb; // FVB of spare block
EFI_LBA FtwSpareLba; // Start LBA 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. 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 EFI_LBA FtwWorkSpaceLba; // Start LBA of working space
UINTN FtwWorkSpaceBase; // Offset into the FtwWorkSpaceLba block. UINTN FtwWorkSpaceBase; // Offset into the FtwWorkSpaceLba block.
UINTN FtwWorkSpaceSize; // Size of working space range that stores write record. 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 UINT8 *FtwWorkSpace; // Point to Work Space in memory buffer
// //
// Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE], // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],
@ -335,7 +341,6 @@ IsWorkingBlock (
@param FtwDevice The private data of FTW driver @param FtwDevice The private data of FTW driver
@param FvBlock Fvb protocol instance @param FvBlock Fvb protocol instance
@param Lba The block specified
@return A BOOLEAN value indicating in boot block or not. @return A BOOLEAN value indicating in boot block or not.
@ -343,19 +348,20 @@ IsWorkingBlock (
BOOLEAN BOOLEAN
IsBootBlock ( IsBootBlock (
EFI_FTW_DEVICE *FtwDevice, EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock
EFI_LBA Lba
); );
/** /**
Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE. 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. Spare block is accessed by FTW backup FVB protocol interface.
Target block is accessed by FvbBlock protocol interface. LBA is Lba. Target block is accessed by FvBlock protocol interface.
@param FtwDevice The private data of FTW driver @param FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface to access target block @param FvBlock FVB Protocol interface to access target block
@param Lba Lba of the 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_SUCCESS Spare block content is copied to target block
@retval EFI_INVALID_PARAMETER Input parameter error @retval EFI_INVALID_PARAMETER Input parameter error
@ -367,7 +373,9 @@ EFI_STATUS
FlushSpareBlockToTargetBlock ( FlushSpareBlockToTargetBlock (
EFI_FTW_DEVICE *FtwDevice, EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, 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. 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. Spare block is accessed by FTW working FVB protocol interface.
Target block is accessed by FvbBlock protocol interface. LBA is Lba. Target block is accessed by FvBlock protocol interface.
FTW will do extra work on boot block update. FTW will do extra work on boot block update.
FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL, 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 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. that boot block will be update. It shall add a FLAG in the working block.
2. When spare block is ready, 2. When spare block is ready,
3. SetSwapState(EFI_SWAPPED) 3. SetSwapState(SWAPPED)
4. erasing boot block, 4. erasing boot block,
5. programming boot block until the boot block is ok. 5. programming boot block until the boot block is ok.
6. SetSwapState(UNSWAPPED) 6. SetSwapState(UNSWAPPED)
@ -431,6 +439,7 @@ FlushSpareBlockToBootBlock (
@param FvBlock FVB Protocol interface to access SrcBlock and DestBlock @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
@param BlockSize The size of the block
@param Lba Lba of a block @param Lba Lba of a block
@param Offset Offset on the Lba @param Offset Offset on the Lba
@param NewBit New value that will override the old value if it can be change @param NewBit New value that will override the old value if it can be change
@ -445,6 +454,7 @@ FlushSpareBlockToBootBlock (
EFI_STATUS EFI_STATUS
FtwUpdateFvState ( FtwUpdateFvState (
IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
IN UINTN BlockSize,
IN EFI_LBA Lba, IN EFI_LBA Lba,
IN UINTN Offset, IN UINTN Offset,
IN UINT8 NewBit 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 @param Address Address specified the block
@ -708,4 +718,52 @@ InitializeLocalWorkSpaceHeader (
VOID 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 #endif

View File

@ -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 FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface @param FvBlock FVB Protocol interface
@param Lba Lba of the firmware block @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 EFI_SUCCESS Block LBA is Erased successfully
@retval Others Error occurs @retval Others Error occurs
@ -63,13 +64,14 @@ EFI_STATUS
FtwEraseBlock ( FtwEraseBlock (
IN EFI_FTW_DEVICE *FtwDevice, IN EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
EFI_LBA Lba EFI_LBA Lba,
UINTN NumberOfBlocks
) )
{ {
return FvBlock->EraseBlocks ( return FvBlock->EraseBlocks (
FvBlock, FvBlock,
Lba, Lba,
FtwDevice->NumberOfSpareBlock, NumberOfBlocks,
EFI_LBA_LIST_TERMINATOR 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 @param Address Address specified the block
@ -159,8 +161,9 @@ GetFvbByAddress (
UINTN Index; UINTN Index;
EFI_PHYSICAL_ADDRESS FvbBaseAddress; EFI_PHYSICAL_ADDRESS FvbBaseAddress;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
EFI_HANDLE FvbHandle; EFI_HANDLE FvbHandle;
UINTN BlockSize;
UINTN NumberOfBlocks;
*FvBlock = NULL; *FvBlock = NULL;
FvbHandle = NULL; FvbHandle = NULL;
@ -188,8 +191,15 @@ GetFvbByAddress (
continue; 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; *FvBlock = Fvb;
FvbHandle = HandleBuffer[Index]; FvbHandle = HandleBuffer[Index];
break; break;
@ -206,7 +216,6 @@ GetFvbByAddress (
@param FtwDevice The private data of FTW driver @param FtwDevice The private data of FTW driver
@param FvBlock Fvb protocol instance @param FvBlock Fvb protocol instance
@param Lba The block specified
@return A BOOLEAN value indicating in boot block or not. @return A BOOLEAN value indicating in boot block or not.
@ -214,8 +223,7 @@ GetFvbByAddress (
BOOLEAN BOOLEAN
IsBootBlock ( IsBootBlock (
EFI_FTW_DEVICE *FtwDevice, EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock
EFI_LBA Lba
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
@ -274,8 +282,8 @@ IsBootBlock (
/** /**
Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE. 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. Spare block is accessed by FTW working FVB protocol interface.
Target block is accessed by FvbBlock protocol interface. LBA is Lba. Target block is accessed by FvBlock protocol interface.
FTW will do extra work on boot block update. FTW will do extra work on boot block update.
FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL, 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 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. that boot block will be update. It shall add a FLAG in the working block.
2. When spare block is ready, 2. When spare block is ready,
3. SetSwapState(EFI_SWAPPED) 3. SetSwapState(SWAPPED)
4. erasing boot block, 4. erasing boot block,
5. programming boot block until the boot block is ok. 5. programming boot block until the boot block is ok.
6. SetSwapState(UNSWAPPED) 6. SetSwapState(UNSWAPPED)
@ -357,7 +365,7 @@ FlushSpareBlockToBootBlock (
BootLba = 0; BootLba = 0;
Ptr = Buffer; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->SpareBlockSize;
Status = BootFvb->Read ( Status = BootFvb->Read (
BootFvb, BootFvb,
BootLba + Index, BootLba + Index,
@ -378,7 +386,7 @@ FlushSpareBlockToBootBlock (
// //
Ptr = Buffer; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read ( Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -416,7 +424,7 @@ FlushSpareBlockToBootBlock (
// //
Ptr = Buffer; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write ( Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -444,14 +452,16 @@ FlushSpareBlockToBootBlock (
} }
/** /**
Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE. Copy the content of spare block to a target block.
Spare block is accessed by FTW backup FVB protocol interface. LBA is 1. Spare block is accessed by FTW backup FVB protocol interface.
Target block is accessed by FvbBlock protocol interface. LBA is Lba. Target block is accessed by FvBlock protocol interface.
@param FtwDevice The private data of FTW driver @param FtwDevice The private data of FTW driver
@param FvBlock FVB Protocol interface to access target block @param FvBlock FVB Protocol interface to access target block
@param Lba Lba of the 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_SUCCESS Spare block content is copied to target block
@retval EFI_INVALID_PARAMETER Input parameter error @retval EFI_INVALID_PARAMETER Input parameter error
@ -463,7 +473,9 @@ EFI_STATUS
FlushSpareBlockToTargetBlock ( FlushSpareBlockToTargetBlock (
EFI_FTW_DEVICE *FtwDevice, EFI_FTW_DEVICE *FtwDevice,
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
EFI_LBA Lba EFI_LBA Lba,
UINTN BlockSize,
UINTN NumberOfBlocks
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
@ -489,7 +501,7 @@ FlushSpareBlockToTargetBlock (
// //
Ptr = Buffer; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read ( Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -507,17 +519,17 @@ FlushSpareBlockToTargetBlock (
// //
// Erase the target block // Erase the target block
// //
Status = FtwEraseBlock (FtwDevice, FvBlock, Lba); Status = FtwEraseBlock (FtwDevice, FvBlock, Lba, NumberOfBlocks);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
FreePool (Buffer); FreePool (Buffer);
return EFI_ABORTED; 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; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < NumberOfBlocks; Index += 1) {
Count = FtwDevice->BlockSize; Count = BlockSize;
Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr); Status = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status)); DEBUG ((EFI_D_ERROR, "Ftw: FVB Write block - %r\n", Status));
@ -563,7 +575,6 @@ FlushSpareBlockToWorkingBlock (
UINTN Count; UINTN Count;
UINT8 *Ptr; UINT8 *Ptr;
UINTN Index; UINTN Index;
EFI_LBA WorkSpaceLbaOffset;
// //
// Allocate a memory buffer // Allocate a memory buffer
@ -574,8 +585,6 @@ FlushSpareBlockToWorkingBlock (
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }
WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
// //
// To guarantee that the WorkingBlockValid is set on spare block // To guarantee that the WorkingBlockValid is set on spare block
// //
@ -585,8 +594,9 @@ FlushSpareBlockToWorkingBlock (
// //
FtwUpdateFvState ( FtwUpdateFvState (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + WorkSpaceLbaOffset, FtwDevice->SpareBlockSize,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32), FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
FtwDevice->FtwWorkSpaceBaseInSpare + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID WORKING_BLOCK_VALID
); );
// //
@ -594,7 +604,7 @@ FlushSpareBlockToWorkingBlock (
// //
Ptr = Buffer; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read ( Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -612,7 +622,7 @@ FlushSpareBlockToWorkingBlock (
// //
// Clear the CRC and STATE, copy data from spare to working block. // 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); InitWorkSpaceHeader (WorkingBlockHeader);
WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY; WorkingBlockHeader->WorkingBlockValid = FTW_ERASE_POLARITY;
WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY; WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;
@ -629,6 +639,7 @@ FlushSpareBlockToWorkingBlock (
// //
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32), FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_INVALID WORKING_BLOCK_INVALID
@ -643,17 +654,17 @@ FlushSpareBlockToWorkingBlock (
// //
// Erase the working block // Erase the working block
// //
Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba); Status = FtwEraseBlock (FtwDevice, FtwDevice->FtwFvBlock, FtwDevice->FtwWorkBlockLba, FtwDevice->NumberOfWorkBlock);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
FreePool (Buffer); FreePool (Buffer);
return EFI_ABORTED; 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; Ptr = Buffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfWorkBlock; Index += 1) {
Count = FtwDevice->BlockSize; Count = FtwDevice->WorkBlockSize;
Status = FtwDevice->FtwFvBlock->Write ( Status = FtwDevice->FtwFvBlock->Write (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->FtwWorkBlockLba + Index, FtwDevice->FtwWorkBlockLba + Index,
@ -682,6 +693,7 @@ FlushSpareBlockToWorkingBlock (
// //
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32), FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID WORKING_BLOCK_VALID
@ -703,6 +715,7 @@ FlushSpareBlockToWorkingBlock (
@param FvBlock FVB Protocol interface to access SrcBlock and DestBlock @param FvBlock FVB Protocol interface to access SrcBlock and DestBlock
@param BlockSize The size of the block
@param Lba Lba of a block @param Lba Lba of a block
@param Offset Offset on the Lba @param Offset Offset on the Lba
@param NewBit New value that will override the old value if it can be change @param NewBit New value that will override the old value if it can be change
@ -717,6 +730,7 @@ FlushSpareBlockToWorkingBlock (
EFI_STATUS EFI_STATUS
FtwUpdateFvState ( FtwUpdateFvState (
IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock,
IN UINTN BlockSize,
IN EFI_LBA Lba, IN EFI_LBA Lba,
IN UINTN Offset, IN UINTN Offset,
IN UINT8 NewBit IN UINT8 NewBit
@ -726,6 +740,14 @@ FtwUpdateFvState (
UINT8 State; UINT8 State;
UINTN Length; 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. // Read state from device, assume State is only one byte.
// //
@ -1015,10 +1037,10 @@ FindFvbForFtw (
UINTN Index; UINTN Index;
EFI_PHYSICAL_ADDRESS FvbBaseAddress; EFI_PHYSICAL_ADDRESS FvbBaseAddress;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
EFI_FVB_ATTRIBUTES_2 Attributes; EFI_FVB_ATTRIBUTES_2 Attributes;
EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;
UINT32 LbaIndex; UINT32 LbaIndex;
UINTN BlockSize;
UINTN NumberOfBlocks;
HandleBuffer = NULL; HandleBuffer = NULL;
@ -1056,59 +1078,72 @@ FindFvbForFtw (
continue; 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) && if ((FtwDevice->FtwFvBlock == NULL) && (FtwDevice->WorkSpaceAddress >= FvbBaseAddress) &&
((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + FwVolHeader->FvLength)) ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
) {
FtwDevice->FtwFvBlock = Fvb; FtwDevice->FtwFvBlock = Fvb;
// //
// To get the LBA of work space // To get the LBA of work space
// //
if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) { for (LbaIndex = 1; LbaIndex <= NumberOfBlocks; LbaIndex += 1) {
// if ((FtwDevice->WorkSpaceAddress >= (FvbBaseAddress + BlockSize * (LbaIndex - 1)))
// Now, one FV has one type of BlockLength && (FtwDevice->WorkSpaceAddress < (FvbBaseAddress + BlockSize * LbaIndex))) {
//
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; FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;
// //
// Get the Work space size and Base(Offset) // Get the Work space size and Base(Offset)
// //
FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength; FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;
FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1))); FtwDevice->WorkBlockSize = BlockSize;
break; 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) {
//
// 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.
//
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) && if ((FtwDevice->FtwBackupFvb == NULL) && (FtwDevice->SpareAreaAddress >= FvbBaseAddress) &&
((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + FwVolHeader->FvLength)) ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (FvbBaseAddress + BlockSize * NumberOfBlocks))) {
) {
FtwDevice->FtwBackupFvb = Fvb; FtwDevice->FtwBackupFvb = Fvb;
// //
// To get the LBA of spare // To get the LBA of spare
// //
if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) { for (LbaIndex = 1; LbaIndex <= NumberOfBlocks; LbaIndex += 1) {
// if ((FtwDevice->SpareAreaAddress >= (FvbBaseAddress + BlockSize * (LbaIndex - 1)))
// Now, one FV has one type of BlockLength && (FtwDevice->SpareAreaAddress < (FvbBaseAddress + BlockSize * LbaIndex))) {
//
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 // Get the NumberOfSpareBlock and BlockSize
// //
FtwDevice->FtwSpareLba = LbaIndex - 1; FtwDevice->FtwSpareLba = LbaIndex - 1;
FtwDevice->BlockSize = FvbMapEntry->Length; FtwDevice->SpareBlockSize = BlockSize;
FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize; FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->SpareBlockSize;
// //
// Check the range of spare area to make sure that it's in FV range // Check the range of spare area to make sure that it's in FV range
// //
if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) { if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > NumberOfBlocks) {
DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n")); DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));
FreePool (HandleBuffer); FreePool (HandleBuffer);
ASSERT (FALSE); ASSERT (FALSE);
@ -1117,8 +1152,8 @@ FindFvbForFtw (
// //
// Check the alignment of spare area address and length, they should be block size aligned // Check the alignment of spare area address and length, they should be block size aligned
// //
if (((FtwDevice->SpareAreaAddress & (FtwDevice->BlockSize - 1)) != 0) || if (((FtwDevice->SpareAreaAddress & (FtwDevice->SpareBlockSize - 1)) != 0) ||
((FtwDevice->SpareAreaLength & (FtwDevice->BlockSize - 1)) != 0)) { ((FtwDevice->SpareAreaLength & (FtwDevice->SpareBlockSize - 1)) != 0)) {
DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n")); DEBUG ((EFI_D_ERROR, "Ftw: Spare area address or length is not block size aligned\n"));
FreePool (HandleBuffer); FreePool (HandleBuffer);
// //
@ -1133,13 +1168,14 @@ FindFvbForFtw (
} }
} }
} }
}
FreePool (HandleBuffer); FreePool (HandleBuffer);
if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) || if ((FtwDevice->FtwBackupFvb == NULL) || (FtwDevice->FtwFvBlock == NULL) ||
(FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) { (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) || (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))) {
return EFI_ABORTED; 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; return EFI_SUCCESS;
} }
@ -1161,7 +1197,6 @@ InitFtwProtocol (
{ {
EFI_STATUS Status; EFI_STATUS Status;
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb; EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
UINTN Length;
EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader; EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;
UINTN Offset; UINTN Offset;
EFI_HANDLE FvbHandle; EFI_HANDLE FvbHandle;
@ -1176,13 +1211,36 @@ InitFtwProtocol (
} }
// //
// Calculate the start LBA of working block. Working block is an area which // 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 // 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 // block, unless there are not enough blocks before the block that contains
// working space. // working space.
// //
FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1; FtwDevice->NumberOfWorkBlock = (UINTN) (FtwDevice->FtwWorkSpaceLba + FtwDevice->NumberOfWorkSpaceBlock);
ASSERT ((INT64) (FtwDevice->FtwWorkBlockLba) >= 0); 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));
//
// Calcualte the LBA and base of work space in spare block.
// Note: Do not assume Spare Block and Work Block have same block size.
//
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. // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.
@ -1207,13 +1265,12 @@ InitFtwProtocol (
// //
// Read from spare block // Read from spare block
// //
WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba; Status = ReadWorkSpaceData (
Length = FtwDevice->FtwWorkSpaceSize;
Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + WorkSpaceLbaOffset, FtwDevice->SpareBlockSize,
FtwDevice->FtwWorkSpaceBase, FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
&Length, FtwDevice->FtwWorkSpaceBaseInSpare,
FtwDevice->FtwWorkSpaceSize,
FtwDevice->FtwWorkSpace FtwDevice->FtwWorkSpace
); );
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
@ -1304,7 +1361,7 @@ InitFtwProtocol (
FvbHandle = GetFvbByAddress ((EFI_PHYSICAL_ADDRESS) (UINTN) ((INT64) FtwDevice->SpareAreaAddress + FtwDevice->FtwLastWriteRecord->RelativeOffset), &Fvb); FvbHandle = GetFvbByAddress ((EFI_PHYSICAL_ADDRESS) (UINTN) ((INT64) FtwDevice->SpareAreaAddress + FtwDevice->FtwLastWriteRecord->RelativeOffset), &Fvb);
if (FvbHandle != NULL) { if (FvbHandle != NULL) {
Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle); 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); ASSERT_EFI_ERROR (Status);
} }
FtwAbort (&FtwDevice->FtwInstance); FtwAbort (&FtwDevice->FtwInstance);

View File

@ -125,6 +125,132 @@ InitWorkSpaceHeader (
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/**
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. Read from working block to refresh the work space in memory.
@ -140,7 +266,6 @@ WorkSpaceRefresh (
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
UINTN Length;
UINTN RemainingSpaceSize; UINTN RemainingSpaceSize;
// //
@ -155,12 +280,12 @@ WorkSpaceRefresh (
// //
// Read from working block // Read from working block
// //
Length = FtwDevice->FtwWorkSpaceSize; Status = ReadWorkSpaceData (
Status = FtwDevice->FtwFvBlock->Read (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase, FtwDevice->FtwWorkSpaceBase,
&Length, FtwDevice->FtwWorkSpaceSize,
FtwDevice->FtwWorkSpace FtwDevice->FtwWorkSpace
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -194,12 +319,12 @@ WorkSpaceRefresh (
// //
// Read from working block again // Read from working block again
// //
Length = FtwDevice->FtwWorkSpaceSize; Status = ReadWorkSpaceData (
Status = FtwDevice->FtwFvBlock->Read (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase, FtwDevice->FtwWorkSpaceBase,
&Length, FtwDevice->FtwWorkSpaceSize,
FtwDevice->FtwWorkSpace FtwDevice->FtwWorkSpace
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -265,15 +390,15 @@ FtwReclaimWorkSpace (
// //
// Read all original data from working block to a memory buffer // Read all original data from working block to a memory buffer
// //
TempBufferSize = FtwDevice->SpareAreaLength; TempBufferSize = FtwDevice->NumberOfWorkBlock * FtwDevice->WorkBlockSize;
TempBuffer = AllocateZeroPool (TempBufferSize); TempBuffer = AllocateZeroPool (TempBufferSize);
if (TempBuffer == NULL) { if (TempBuffer == NULL) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }
Ptr = TempBuffer; Ptr = TempBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfWorkBlock; Index += 1) {
Length = FtwDevice->BlockSize; Length = FtwDevice->WorkBlockSize;
Status = FtwDevice->FtwFvBlock->Read ( Status = FtwDevice->FtwFvBlock->Read (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->FtwWorkBlockLba + Index, FtwDevice->FtwWorkBlockLba + Index,
@ -292,7 +417,7 @@ FtwReclaimWorkSpace (
// Clean up the workspace, remove all the completed records. // Clean up the workspace, remove all the completed records.
// //
Ptr = TempBuffer + Ptr = TempBuffer +
(UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize + (UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
FtwDevice->FtwWorkSpaceBase; FtwDevice->FtwWorkSpaceBase;
// //
@ -348,7 +473,7 @@ FtwReclaimWorkSpace (
// Set the WorkingBlockValid and WorkingBlockInvalid as INVALID // Set the WorkingBlockValid and WorkingBlockInvalid as INVALID
// //
WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (TempBuffer + WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (TempBuffer +
(UINTN) WorkSpaceLbaOffset * FtwDevice->BlockSize + (UINTN) WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
FtwDevice->FtwWorkSpaceBase); FtwDevice->FtwWorkSpaceBase);
WorkingBlockHeader->WorkingBlockValid = FTW_INVALID_STATE; WorkingBlockHeader->WorkingBlockValid = FTW_INVALID_STATE;
WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE; WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE;
@ -366,7 +491,7 @@ FtwReclaimWorkSpace (
Ptr = SpareBuffer; Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Length = FtwDevice->BlockSize; Length = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Read ( Status = FtwDevice->FtwBackupFvb->Read (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -387,8 +512,12 @@ FtwReclaimWorkSpace (
// //
Status = FtwEraseSpareBlock (FtwDevice); Status = FtwEraseSpareBlock (FtwDevice);
Ptr = TempBuffer; Ptr = TempBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; TempBufferSize > 0; Index += 1) {
Length = FtwDevice->BlockSize; if (TempBufferSize > FtwDevice->SpareBlockSize) {
Length = FtwDevice->SpareBlockSize;
} else {
Length = TempBufferSize;
}
Status = FtwDevice->FtwBackupFvb->Write ( Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,
@ -403,6 +532,7 @@ FtwReclaimWorkSpace (
} }
Ptr += Length; Ptr += Length;
TempBufferSize -= Length;
} }
// //
// Free TempBuffer // Free TempBuffer
@ -414,8 +544,9 @@ FtwReclaimWorkSpace (
// //
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + WorkSpaceLbaOffset, FtwDevice->SpareBlockSize,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32), FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
FtwDevice->FtwWorkSpaceBaseInSpare + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_VALID WORKING_BLOCK_VALID
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -430,6 +561,7 @@ FtwReclaimWorkSpace (
// //
Status = FtwUpdateFvState ( Status = FtwUpdateFvState (
FtwDevice->FtwFvBlock, FtwDevice->FtwFvBlock,
FtwDevice->WorkBlockSize,
FtwDevice->FtwWorkSpaceLba, FtwDevice->FtwWorkSpaceLba,
FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32), FtwDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),
WORKING_BLOCK_INVALID WORKING_BLOCK_INVALID
@ -455,7 +587,7 @@ FtwReclaimWorkSpace (
Status = FtwEraseSpareBlock (FtwDevice); Status = FtwEraseSpareBlock (FtwDevice);
Ptr = SpareBuffer; Ptr = SpareBuffer;
for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) { for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
Length = FtwDevice->BlockSize; Length = FtwDevice->SpareBlockSize;
Status = FtwDevice->FtwBackupFvb->Write ( Status = FtwDevice->FtwBackupFvb->Write (
FtwDevice->FtwBackupFvb, FtwDevice->FtwBackupFvb,
FtwDevice->FtwSpareLba + Index, FtwDevice->FtwSpareLba + Index,