+++ /dev/null
-/** @file\r
- Handles non-volatile variable store garbage collection, using FTW\r
- (Fault Tolerant Write) protocol.\r
-\r
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "Variable.h"\r
-\r
-/**\r
- Gets firmware volume block handle by given address.\r
-\r
- This function gets firmware volume block handle whose\r
- address range contains the parameter Address.\r
-\r
- @param[in] Address Address which should be contained\r
- by returned FVB handle.\r
- @param[out] FvbHandle Pointer to FVB handle for output.\r
-\r
- @retval EFI_SUCCESS FVB handle successfully returned.\r
- @retval EFI_NOT_FOUND Failed to find FVB handle by address.\r
-\r
-**/\r
-EFI_STATUS\r
-GetFvbHandleByAddress (\r
- IN EFI_PHYSICAL_ADDRESS Address,\r
- OUT EFI_HANDLE *FvbHandle\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE *HandleBuffer;\r
- UINTN HandleCount;\r
- UINTN Index;\r
- EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
- EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
-\r
- *FvbHandle = NULL;\r
- //\r
- // Locate all handles with Firmware Volume Block protocol\r
- //\r
- Status = gBS->LocateHandleBuffer (\r
- ByProtocol,\r
- &gEfiFirmwareVolumeBlockProtocolGuid,\r
- NULL,\r
- &HandleCount,\r
- &HandleBuffer\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return EFI_NOT_FOUND;\r
- }\r
- //\r
- // Traverse all the handles, searching for the one containing parameter Address\r
- //\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
- Status = EFI_NOT_FOUND;\r
- break;\r
- }\r
- //\r
- // Checks if the address range of this handle contains parameter Address\r
- //\r
- Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
- if (EFI_ERROR (Status)) {\r
- continue;\r
- }\r
-\r
- FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
- if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r
- *FvbHandle = HandleBuffer[Index];\r
- Status = EFI_SUCCESS;\r
- break;\r
- }\r
- }\r
-\r
- FreePool (HandleBuffer);\r
- return Status;\r
-}\r
-\r
-/**\r
- Gets LBA of block and offset by given address.\r
-\r
- This function gets the Logical Block Address (LBA) of firmware\r
- volume block containing the given address, and the offset of\r
- address on the block.\r
-\r
- @param[in] Address Address which should be contained\r
- by returned FVB handle.\r
- @param[out] Lba The pointer to LBA for output.\r
- @param[out] Offset The pointer to offset for output.\r
-\r
- @retval EFI_SUCCESS LBA and offset successfully returned.\r
- @retval EFI_NOT_FOUND Failed to find FVB handle by address.\r
- @retval EFI_ABORTED Failed to find valid LBA and offset.\r
-\r
-**/\r
-EFI_STATUS\r
-GetLbaAndOffsetByAddress (\r
- IN EFI_PHYSICAL_ADDRESS Address,\r
- OUT EFI_LBA *Lba,\r
- OUT UINTN *Offset\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE FvbHandle;\r
- EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
- EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
- EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
- EFI_FV_BLOCK_MAP_ENTRY *FvbMapEntry;\r
- UINT32 LbaIndex;\r
-\r
- *Lba = (EFI_LBA) (-1);\r
- *Offset = 0;\r
-\r
- //\r
- // Gets firmware volume block handle by given address.\r
- //\r
- Status = GetFvbHandleByAddress (Address, &FvbHandle);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- Status = gBS->HandleProtocol (\r
- FvbHandle,\r
- &gEfiFirmwareVolumeBlockProtocolGuid,\r
- (VOID **) &Fvb\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- //\r
- // Get the Base Address of FV\r
- //\r
- Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
-\r
- //\r
- // Get the (LBA, Offset) of Address\r
- //\r
- if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {\r
- if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
- //\r
- // BUGBUG: Assume one FV has one type of BlockLength\r
- //\r
- FvbMapEntry = &FwVolHeader->BlockMap[0];\r
- for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
- if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
- //\r
- // Found the (Lba, Offset)\r
- //\r
- *Lba = LbaIndex - 1;\r
- *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
- return EFI_SUCCESS;\r
- }\r
- }\r
- }\r
- }\r
-\r
- return EFI_ABORTED;\r
-}\r
-\r
-/**\r
- Writes a buffer to variable storage space.\r
-\r
- This function writes a buffer to variable storage space into firmware\r
- volume block device. The destination is specified by parameter\r
- VariableBase. Fault Tolerant Write protocol is used for writing.\r
-\r
- @param[in] VariableBase The base address of the variable to write.\r
- @param[in] Buffer Points to the data buffer.\r
- @param[in] BufferSize The number of bytes of the data Buffer.\r
-\r
- @retval EFI_SUCCESS The function completed successfully.\r
- @retval EFI_NOT_FOUND Fail to locate Fault Tolerant Write protocol.\r
- @retval Other The function could not complete successfully.\r
-\r
-**/\r
-EFI_STATUS\r
-FtwVariableSpace (\r
- IN EFI_PHYSICAL_ADDRESS VariableBase,\r
- IN UINT8 *Buffer,\r
- IN UINTN BufferSize\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_HANDLE FvbHandle;\r
- EFI_LBA VarLba;\r
- UINTN VarOffset;\r
- UINT8 *FtwBuffer;\r
- UINTN FtwBufferSize;\r
- EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
-\r
- //\r
- // Locate Fault Tolerant Write protocol\r
- //\r
- Status = gBS->LocateProtocol (\r
- &gEfiFaultTolerantWriteProtocolGuid,\r
- NULL,\r
- (VOID **) &FtwProtocol\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return EFI_NOT_FOUND;\r
- }\r
- //\r
- // Gets firmware volume block handle by VariableBase.\r
- //\r
- Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- //\r
- // Gets LBA of block and offset by VariableBase.\r
- //\r
- Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);\r
- if (EFI_ERROR (Status)) {\r
- return EFI_ABORTED;\r
- }\r
- //\r
- // Prepare for the variable data\r
- //\r
- FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;\r
- FtwBuffer = AllocatePool (FtwBufferSize);\r
- if (FtwBuffer == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
-\r
- SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);\r
- CopyMem (FtwBuffer, Buffer, BufferSize);\r
-\r
- //\r
- // FTW write record\r
- //\r
- Status = FtwProtocol->Write (\r
- FtwProtocol,\r
- VarLba, // LBA\r
- VarOffset, // Offset\r
- FtwBufferSize, // NumBytes,\r
- NULL,\r
- FvbHandle,\r
- FtwBuffer\r
- );\r
-\r
- FreePool (FtwBuffer);\r
- return Status;\r
-}\r