49 &gEdkiiWorkingBlockSignatureGuid,
67 mWorkingBlockHeader.WorkingBlockInvalid = FTW_INVALID_STATE;
85 if (WorkingHeader ==
NULL) {
93 DEBUG ((DEBUG_INFO,
"Ftw: Work block header check mismatch\n"));
111 if (WorkingHeader ==
NULL) {
112 return EFI_INVALID_PARAMETER;
151 while (Offset >= BlockSize) {
158 if ((Offset + Length) > BlockSize) {
159 MyLength = BlockSize - Offset;
164 Status = FvBlock->Read (
171 if (EFI_ERROR (Status)) {
215 while (Offset >= BlockSize) {
222 if ((Offset + Length) > BlockSize) {
223 MyLength = BlockSize - Offset;
228 Status = FvBlock->Write (
235 if (EFI_ERROR (Status)) {
263 UINTN RemainingSpaceSize;
269 FtwDevice->FtwWorkSpace,
270 FtwDevice->FtwWorkSpaceSize,
278 FtwDevice->FtwFvBlock,
279 FtwDevice->WorkBlockSize,
280 FtwDevice->FtwWorkSpaceLba,
281 FtwDevice->FtwWorkSpaceBase,
282 FtwDevice->FtwWorkSpaceSize,
283 FtwDevice->FtwWorkSpace
285 if (EFI_ERROR (Status)) {
293 FtwDevice->FtwWorkSpaceHeader,
294 FtwDevice->FtwWorkSpaceSize,
295 &FtwDevice->FtwLastWriteHeader
297 RemainingSpaceSize = FtwDevice->FtwWorkSpaceSize - ((
UINTN)FtwDevice->FtwLastWriteHeader - (
UINTN)FtwDevice->FtwWorkSpace);
298 DEBUG ((DEBUG_INFO,
"Ftw: Remaining work space size - %x\n", RemainingSpaceSize));
310 if (EFI_ERROR (Status)) {
311 DEBUG ((DEBUG_ERROR,
"Ftw: Reclaim workspace - %r\n", Status));
319 FtwDevice->FtwFvBlock,
320 FtwDevice->WorkBlockSize,
321 FtwDevice->FtwWorkSpaceLba,
322 FtwDevice->FtwWorkSpaceBase,
323 FtwDevice->FtwWorkSpaceSize,
324 FtwDevice->FtwWorkSpace
326 if (EFI_ERROR (Status)) {
331 FtwDevice->FtwWorkSpaceHeader,
332 FtwDevice->FtwWorkSpaceSize,
333 &FtwDevice->FtwLastWriteHeader
335 if (EFI_ERROR (Status)) {
344 FtwDevice->FtwLastWriteHeader,
345 &FtwDevice->FtwLastWriteRecord
347 if (EFI_ERROR (Status)) {
368 IN BOOLEAN PreserveRecord
375 UINTN TempBufferSize;
376 UINTN SpareBufferSize;
383 DEBUG ((DEBUG_INFO,
"Ftw: start to reclaim work space\n"));
385 WorkSpaceLbaOffset = FtwDevice->FtwWorkSpaceLba - FtwDevice->FtwWorkBlockLba;
390 TempBufferSize = FtwDevice->NumberOfWorkBlock * FtwDevice->WorkBlockSize;
392 if (TempBuffer ==
NULL) {
393 return EFI_OUT_OF_RESOURCES;
397 for (Index = 0; Index < FtwDevice->NumberOfWorkBlock; Index += 1) {
398 Length = FtwDevice->WorkBlockSize;
399 Status = FtwDevice->FtwFvBlock->Read (
400 FtwDevice->FtwFvBlock,
401 FtwDevice->FtwWorkBlockLba + Index,
406 if (EFI_ERROR (Status)) {
418 (
UINTN)WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
419 FtwDevice->FtwWorkSpaceBase;
424 SetMem (Ptr, FtwDevice->FtwWorkSpaceSize, FTW_ERASED_BYTE);
431 FtwDevice->FtwWorkSpaceHeader,
434 if (PreserveRecord) {
439 FtwDevice->FtwWorkSpaceHeader,
440 FtwDevice->FtwWorkSpaceSize,
441 &FtwDevice->FtwLastWriteHeader
443 Header = FtwDevice->FtwLastWriteHeader;
444 if (!EFI_ERROR (Status) && (Header !=
NULL) && (Header->Complete != FTW_VALID_STATE) && (Header->HeaderAllocated == FTW_VALID_STATE)) {
447 FtwDevice->FtwLastWriteHeader,
448 FTW_WRITE_TOTAL_SIZE (Header->NumberOfWrites, Header->PrivateDataSize)
454 FtwDevice->FtwWorkSpace,
456 FtwDevice->FtwWorkSpaceSize
460 FtwDevice->FtwWorkSpaceHeader,
461 FtwDevice->FtwWorkSpaceSize,
462 &FtwDevice->FtwLastWriteHeader
466 FtwDevice->FtwLastWriteHeader,
467 &FtwDevice->FtwLastWriteRecord
474 (
UINTN)WorkSpaceLbaOffset * FtwDevice->WorkBlockSize +
475 FtwDevice->FtwWorkSpaceBase);
477 WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE;
483 SpareBufferSize = FtwDevice->SpareAreaLength;
485 if (SpareBuffer ==
NULL) {
487 return EFI_OUT_OF_RESOURCES;
491 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
492 Length = FtwDevice->SpareBlockSize;
493 Status = FtwDevice->FtwBackupFvb->Read (
494 FtwDevice->FtwBackupFvb,
495 FtwDevice->FtwSpareLba + Index,
500 if (EFI_ERROR (Status)) {
513 if (EFI_ERROR (Status)) {
520 for (Index = 0; TempBufferSize > 0; Index += 1) {
521 if (TempBufferSize > FtwDevice->SpareBlockSize) {
522 Length = FtwDevice->SpareBlockSize;
524 Length = TempBufferSize;
527 Status = FtwDevice->FtwBackupFvb->Write (
528 FtwDevice->FtwBackupFvb,
529 FtwDevice->FtwSpareLba + Index,
534 if (EFI_ERROR (Status)) {
541 TempBufferSize -= Length;
553 FtwDevice->FtwBackupFvb,
554 FtwDevice->SpareBlockSize,
555 FtwDevice->FtwSpareLba + FtwDevice->FtwWorkSpaceLbaInSpare,
556 FtwDevice->FtwWorkSpaceBaseInSpare + sizeof (
EFI_GUID) +
sizeof (UINT32),
559 if (EFI_ERROR (Status)) {
571 FtwDevice->FtwFvBlock,
572 FtwDevice->WorkBlockSize,
573 FtwDevice->FtwWorkSpaceLba,
574 FtwDevice->FtwWorkSpaceBase + sizeof (
EFI_GUID) +
sizeof (UINT32),
575 WORKING_BLOCK_INVALID
577 if (EFI_ERROR (Status)) {
582 FtwDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;
588 if (EFI_ERROR (Status)) {
597 if (EFI_ERROR (Status)) {
603 for (Index = 0; Index < FtwDevice->NumberOfSpareBlock; Index += 1) {
604 Length = FtwDevice->SpareBlockSize;
605 Status = FtwDevice->FtwBackupFvb->Write (
606 FtwDevice->FtwBackupFvb,
607 FtwDevice->FtwSpareLba + Index,
612 if (EFI_ERROR (Status)) {
622 DEBUG ((DEBUG_INFO,
"Ftw: reclaim work space successfully\n"));
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define DEBUG(Expression)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
EFI_STATUS FtwEraseSpareBlock(IN EFI_FTW_DEVICE *FtwDevice)
EFI_STATUS FtwGetLastWriteRecord(IN EFI_FAULT_TOLERANT_WRITE_HEADER *FtwWriteHeader, OUT EFI_FAULT_TOLERANT_WRITE_RECORD **FtwWriteRecord)
EFI_STATUS FlushSpareBlockToWorkingBlock(EFI_FTW_DEVICE *FtwDevice)
EFI_STATUS FtwGetLastWriteHeader(IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader, IN UINTN FtwWorkSpaceSize, OUT EFI_FAULT_TOLERANT_WRITE_HEADER **FtwWriteHeader)
UINT32 FtwCalculateCrc32(IN VOID *Buffer, IN UINTN Length)
EFI_STATUS FtwUpdateFvState(IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvBlock, IN UINTN BlockSize, IN EFI_LBA Lba, IN UINTN Offset, IN UINT8 NewBit)
BOOLEAN IsValidWorkSpace(IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader)
EFI_STATUS InitWorkSpaceHeader(IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader)
EFI_STATUS FtwReclaimWorkSpace(IN EFI_FTW_DEVICE *FtwDevice, IN BOOLEAN PreserveRecord)
EFI_STATUS WorkSpaceRefresh(IN EFI_FTW_DEVICE *FtwDevice)
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)
VOID InitializeLocalWorkSpaceHeader(IN UINTN WorkSpaceLength)
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)