summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c')
-rw-r--r--MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c279
1 files changed, 168 insertions, 111 deletions
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c
index b24d66dde3..09223217e4 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);