-/**\r
- This function is the entry point of the Fault Tolerant Write driver.\r
-\r
- @param ImageHandle A handle for the image that is initializing this driver\r
- @param SystemTable A pointer to the EFI system table\r
-\r
- @return EFI_SUCCESS FTW has finished the initialization\r
- @retval EFI_NOT_FOUND Locate FVB protocol error\r
- @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
- @retval EFI_VOLUME_CORRUPTED Firmware volume is error\r
- @retval EFI_ABORTED FTW initialization error\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeFaultTolerantWrite (\r
- IN EFI_HANDLE ImageHandle,\r
- IN EFI_SYSTEM_TABLE *SystemTable\r
- )\r
-{\r
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
- UINTN Index;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN HandleCount;\r
- EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
- EFI_PHYSICAL_ADDRESS BaseAddress;\r
- EFI_FTW_DEVICE *FtwDevice;\r
- EFI_FAULT_TOLERANT_WRITE_HEADER *FtwHeader;\r
- UINTN Length;\r
- EFI_STATUS Status;\r
- UINTN Offset;\r
- EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
- UINT32 LbaIndex;\r
- EFI_HANDLE FvbHandle;\r
-\r
- //\r
- // Allocate Private data of this driver,\r
- // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
- //\r
- FvbHandle = NULL;\r
- FtwDevice = NULL;\r
- FtwDevice = AllocatePool (sizeof (EFI_FTW_DEVICE) + PcdGet32 (PcdFlashNvStorageFtwWorkingSize));\r
- if (FtwDevice == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
-\r
- ZeroMem (FtwDevice, sizeof (EFI_FTW_DEVICE));\r
- FtwDevice->Signature = FTW_DEVICE_SIGNATURE;\r
-\r
- //\r
- // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
- //\r
-\r
- FtwDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
- FtwDevice->WorkSpaceLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
-\r
- FtwDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);\r
- FtwDevice->SpareAreaLength = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r
-\r
- if ((FtwDevice->WorkSpaceLength == 0) || (FtwDevice->SpareAreaLength == 0)) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Workspace or Spare block does not exist!\n"));\r
- FreePool (FtwDevice);\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- //\r
- // Locate FVB protocol by handle\r
- //\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiFirmwareVolumeBlockProtocolGuid,\r
- NULL,\r
- &HandleCount,\r
- &HandleBuffer\r
- );\r
- if (EFI_ERROR (Status)) {\r
- FreePool (FtwDevice);\r
- return EFI_NOT_FOUND;\r
- }\r
-\r
- if (HandleCount <= 0) {\r
- FreePool (FtwDevice);\r
- return EFI_NOT_FOUND;\r
- }\r
-\r
- Fvb = NULL;\r
- FtwDevice->FtwFvBlock = NULL;\r
- FtwDevice->FtwBackupFvb = NULL;\r
- FtwDevice->FtwWorkSpaceLba = (EFI_LBA) (-1);\r
- FtwDevice->FtwSpareLba = (EFI_LBA) (-1);\r
- for (Index = 0; Index < HandleCount; Index += 1) {\r
- Status = gBS->HandleProtocol (\r
- HandleBuffer[Index],\r
- &gEfiFirmwareVolumeBlockProtocolGuid,\r
- (VOID **) &Fvb\r
- );\r
- if (EFI_ERROR (Status)) {\r
- FreePool (FtwDevice);\r
- return Status;\r
- }\r
-\r
- Status = Fvb->GetPhysicalAddress (Fvb, &BaseAddress);\r
- if (EFI_ERROR (Status)) {\r
- continue;\r
- }\r
-\r
- FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) BaseAddress);\r
-\r
- if ((FtwDevice->WorkSpaceAddress >= BaseAddress) &&\r
- ((FtwDevice->WorkSpaceAddress + FtwDevice->WorkSpaceLength) <= (BaseAddress + FwVolHeader->FvLength))\r
- ) {\r
- FtwDevice->FtwFvBlock = Fvb;\r
- //\r
- // To get the LBA of work space\r
- //\r
- if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
- //\r
- // Now, one FV has one type of BlockLength\r
- //\r
- FvbMapEntry = &FwVolHeader->BlockMap[0];\r
- for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
- if ((FtwDevice->WorkSpaceAddress >= (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
- && (FtwDevice->WorkSpaceAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
- FtwDevice->FtwWorkSpaceLba = LbaIndex - 1;\r
- //\r
- // Get the Work space size and Base(Offset)\r
- //\r
- FtwDevice->FtwWorkSpaceSize = FtwDevice->WorkSpaceLength;\r
- FtwDevice->FtwWorkSpaceBase = (UINTN) (FtwDevice->WorkSpaceAddress - (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
- break;\r
- }\r
- }\r
- }\r
- }\r
-\r
- if ((FtwDevice->SpareAreaAddress >= BaseAddress) &&\r
- ((FtwDevice->SpareAreaAddress + FtwDevice->SpareAreaLength) <= (BaseAddress + FwVolHeader->FvLength))\r
- ) {\r
- FtwDevice->FtwBackupFvb = Fvb;\r
- //\r
- // To get the LBA of spare\r
- //\r
- if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
- //\r
- // Now, one FV has one type of BlockLength\r
- //\r
- FvbMapEntry = &FwVolHeader->BlockMap[0];\r
- for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
- if ((FtwDevice->SpareAreaAddress >= (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)))\r
- && (FtwDevice->SpareAreaAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex))) {\r
- //\r
- // Get the NumberOfSpareBlock and BlockSize\r
- //\r
- FtwDevice->FtwSpareLba = LbaIndex - 1;\r
- FtwDevice->BlockSize = FvbMapEntry->Length;\r
- FtwDevice->NumberOfSpareBlock = FtwDevice->SpareAreaLength / FtwDevice->BlockSize;\r
- //\r
- // Check the range of spare area to make sure that it's in FV range\r
- //\r
- if ((FtwDevice->FtwSpareLba + FtwDevice->NumberOfSpareBlock) > FvbMapEntry->NumBlocks) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Spare area is out of FV range\n"));\r
- FreePool (FtwDevice);\r
- return EFI_ABORTED;\r
- }\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- //\r
- // Calculate the start LBA of working block. Working block is an area which\r
- // contains working space in its last block and has the same size as spare\r
- // block, unless there are not enough blocks before the block that contains\r
- // working space.\r
- //\r
- FtwDevice->FtwWorkBlockLba = FtwDevice->FtwWorkSpaceLba - FtwDevice->NumberOfSpareBlock + 1;\r
- if ((INT64) (FtwDevice->FtwWorkBlockLba) < 0) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: The spare block range is too large than the working block range!\n"));\r
- FreePool (FtwDevice);\r
- return EFI_ABORTED;\r
- }\r
-\r
- if ((FtwDevice->FtwFvBlock == NULL) ||\r
- (FtwDevice->FtwBackupFvb == NULL) ||\r
- (FtwDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) ||\r
- (FtwDevice->FtwSpareLba == (EFI_LBA) (-1))\r
- ) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Working or spare FVB not ready\n"));\r
- FreePool (FtwDevice);\r
- return EFI_ABORTED;\r
- }\r
- //\r
- // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
- //\r
- FtwDevice->FtwWorkSpace = (UINT8 *) (FtwDevice + 1);\r
- FtwDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwDevice->FtwWorkSpace;\r
-\r
- FtwDevice->FtwLastWriteHeader = NULL;\r
- FtwDevice->FtwLastWriteRecord = NULL;\r
-\r
- //\r
- // Refresh the working space data from working block\r
- //\r
- Status = WorkSpaceRefresh (FtwDevice);\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- //\r
- // If the working block workspace is not valid, try the spare block\r
- //\r
- if (!IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
- //\r
- // Read from spare block\r
- //\r
- Length = FtwDevice->FtwWorkSpaceSize;\r
- Status = FtwDevice->FtwBackupFvb->Read (\r
- FtwDevice->FtwBackupFvb,\r
- FtwDevice->FtwSpareLba,\r
- FtwDevice->FtwWorkSpaceBase,\r
- &Length,\r
- FtwDevice->FtwWorkSpace\r
- );\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- //\r
- // If spare block is valid, then replace working block content.\r
- //\r
- if (IsValidWorkSpace (FtwDevice->FtwWorkSpaceHeader)) {\r
- Status = FlushSpareBlockToWorkingBlock (FtwDevice);\r
- DEBUG ((EFI_D_ERROR, "Ftw: Restart working block update in Init() - %r\n", Status));\r
- FtwAbort (&FtwDevice->FtwInstance);\r
- //\r
- // Refresh work space.\r
- //\r
- Status = WorkSpaceRefresh (FtwDevice);\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- } else {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Both are invalid, init workspace\n"));\r
- //\r
- // If both are invalid, then initialize work space.\r
- //\r
- SetMem (\r
- FtwDevice->FtwWorkSpace,\r
- FtwDevice->FtwWorkSpaceSize,\r
- FTW_ERASED_BYTE\r
- );\r
- InitWorkSpaceHeader (FtwDevice->FtwWorkSpaceHeader);\r
- //\r
- // Initialize the work space\r
- //\r
- Status = FtwReclaimWorkSpace (FtwDevice, FALSE);\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- }\r
- }\r
-\r
- //\r
- // If the FtwDevice->FtwLastWriteRecord is 1st record of write header &&\r
- // (! SpareComplete) THEN call Abort().\r
- //\r
- if ((FtwDevice->FtwLastWriteHeader->HeaderAllocated == FTW_VALID_STATE) &&\r
- (FtwDevice->FtwLastWriteRecord->SpareComplete != FTW_VALID_STATE) &&\r
- IsFirstRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
- ) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Init.. find first record not SpareCompleted, abort()\n"));\r
- FtwAbort (&FtwDevice->FtwInstance);\r
- }\r
- //\r
- // If Header is incompleted and the last record has completed, then\r
- // call Abort() to set the Header->Complete FLAG.\r
- //\r
- if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
- (FtwDevice->FtwLastWriteRecord->DestinationComplete == FTW_VALID_STATE) &&\r
- IsLastRecordOfWrites (FtwDevice->FtwLastWriteHeader, FtwDevice->FtwLastWriteRecord)\r
- ) {\r
- DEBUG ((EFI_D_ERROR, "Ftw: Init.. find last record completed but header not, abort()\n"));\r
- FtwAbort (&FtwDevice->FtwInstance);\r
- }\r
- //\r
- // To check the workspace buffer following last Write header/records is EMPTY or not.\r
- // If it's not EMPTY, FTW also need to call reclaim().\r
- //\r
- FtwHeader = FtwDevice->FtwLastWriteHeader;\r
- Offset = (UINT8 *) FtwHeader - FtwDevice->FtwWorkSpace;\r
- if (FtwDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {\r
- Offset += WRITE_TOTAL_SIZE (FtwHeader->NumberOfWrites, FtwHeader->PrivateDataSize);\r
- }\r
-\r
- if (!IsErasedFlashBuffer (\r
- FtwDevice->FtwWorkSpace + Offset,\r
- FtwDevice->FtwWorkSpaceSize - Offset\r
- )) {\r
- Status = FtwReclaimWorkSpace (FtwDevice, TRUE);\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- }\r
- //\r
- // Restart if it's boot block\r
- //\r
- if ((FtwDevice->FtwLastWriteHeader->Complete != FTW_VALID_STATE) &&\r
- (FtwDevice->FtwLastWriteRecord->SpareComplete == FTW_VALID_STATE)\r
- ) {\r
- if (FtwDevice->FtwLastWriteRecord->BootBlockUpdate == FTW_VALID_STATE) {\r
- Status = FlushSpareBlockToBootBlock (FtwDevice);\r
- DEBUG ((EFI_D_ERROR, "Ftw: Restart boot block update - %r\n", Status));\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- \r
- FtwAbort (&FtwDevice->FtwInstance);\r
- } else {\r
- //\r
- // if (SpareCompleted) THEN Restart to fault tolerant write.\r
- //\r
- FvbHandle = GetFvbByAddress (FtwDevice->FtwLastWriteRecord->FvBaseAddress, &Fvb);\r
- if (FvbHandle != NULL) {\r
- Status = FtwRestart (&FtwDevice->FtwInstance, FvbHandle);\r
- DEBUG ((EFI_D_ERROR, "FtwLite: Restart last write - %r\n", Status));\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
- }\r
- FtwAbort (&FtwDevice->FtwInstance);\r
- }\r
- }\r
-\r
- //\r
- // Hook the protocol API\r
- //\r
- FtwDevice->FtwInstance.GetMaxBlockSize = FtwGetMaxBlockSize;\r
- FtwDevice->FtwInstance.Allocate = FtwAllocate;\r
- FtwDevice->FtwInstance.Write = FtwWrite;\r
- FtwDevice->FtwInstance.Restart = FtwRestart;\r
- FtwDevice->FtwInstance.Abort = FtwAbort;\r
- FtwDevice->FtwInstance.GetLastWrite = FtwGetLastWrite;\r
-\r
- //\r
- // Install protocol interface\r
- //\r
- Status = gBS->InstallProtocolInterface (\r
- &FtwDevice->Handle,\r
- &gEfiFaultTolerantWriteProtocolGuid,\r
- EFI_NATIVE_INTERFACE,\r
- &FtwDevice->FtwInstance\r
- );\r
- if (EFI_ERROR (Status)) {\r
- goto Recovery;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-\r
-Recovery:\r
-\r
- if (FtwDevice != NULL) {\r
- FreePool (FtwDevice);\r
- }\r
-\r
- DEBUG ((EFI_D_ERROR, "Ftw: Severe Error occurs, need to recovery\n"));\r
-\r
- return EFI_VOLUME_CORRUPTED;\r
-}\r