--- /dev/null
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. 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
+Module Name:\r
+\r
+ FWBlockService.c\r
+\r
+Abstract:\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <PiDxe.h>\r
+#include <WinNtDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Guid/EventGroup.h>\r
+#include <Protocol/FvbExtension.h>\r
+#include <Protocol/FirmwareVolumeBlock.h>\r
+#include <Guid/AlternateFvBlock.h>\r
+#include <Protocol/DevicePath.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/UefiLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/UefiRuntimeLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/HobLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include "FWBlockService.h"\r
+\r
+#define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)\r
+\r
+ESAL_FWB_GLOBAL *mFvbModuleGlobal;\r
+\r
+EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {\r
+ FVB_DEVICE_SIGNATURE,\r
+ {\r
+ {\r
+ {\r
+ HARDWARE_DEVICE_PATH,\r
+ HW_MEMMAP_DP,\r
+ {\r
+ sizeof (MEMMAP_DEVICE_PATH),\r
+ 0\r
+ }\r
+ },\r
+ EfiMemoryMappedIO,\r
+ 0,\r
+ 0,\r
+ },\r
+ {\r
+ END_DEVICE_PATH_TYPE,\r
+ END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+ {\r
+ sizeof (EFI_DEVICE_PATH_PROTOCOL),\r
+ 0\r
+ }\r
+ }\r
+ },\r
+ 0,\r
+ {\r
+ FvbProtocolGetAttributes,\r
+ FvbProtocolSetAttributes,\r
+ FvbProtocolGetPhysicalAddress,\r
+ FvbProtocolGetBlockSize,\r
+ FvbProtocolRead,\r
+ FvbProtocolWrite,\r
+ FvbProtocolEraseBlocks,\r
+ NULL\r
+ },\r
+ {\r
+ FvbExtendProtocolEraseCustomBlockRange\r
+ }\r
+};\r
+\r
+\r
+\r
+VOID\r
+EFIAPI\r
+FvbVirtualddressChangeEvent (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Fixup internal data so that EFI and SAL can be call in virtual mode.\r
+ Call the passed in Child Notify event and convert the mFvbModuleGlobal\r
+ date items to there virtual address.\r
+\r
+ mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] - Physical copy of instance data\r
+ mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] - Virtual pointer to common\r
+ instance data.\r
+\r
+Arguments:\r
+\r
+ (Standard EFI notify event - EFI_EVENT_NOTIFY)\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ UINTN Index;\r
+\r
+ EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);\r
+\r
+ //\r
+ // Convert the base address of all the instances\r
+ //\r
+ Index = 0;\r
+ FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
+ while (Index < mFvbModuleGlobal->NumFv) {\r
+ EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r
+ FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
+ (\r
+ (UINTN) ((UINT8 *) FwhInstance) + FwhInstance->VolumeHeader.HeaderLength +\r
+ (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
+ );\r
+ Index++;\r
+ }\r
+\r
+ EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r
+ EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);\r
+}\r
+\r
+EFI_STATUS\r
+GetFvbInstance (\r
+ IN UINTN Instance,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ OUT EFI_FW_VOL_INSTANCE **FwhInstance,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieves the physical address of a memory mapped FV\r
+\r
+Arguments:\r
+ Instance - The FV instance whose base address is going to be\r
+ returned\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ FwhInstance - The EFI_FW_VOL_INSTANCE fimrware instance structure\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_INSTANCE *FwhRecord;\r
+\r
+ if (Instance >= Global->NumFv) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ //\r
+ // Find the right instance of the FVB private data\r
+ //\r
+ FwhRecord = Global->FvInstance[Virtual];\r
+ while (Instance > 0) {\r
+ FwhRecord = (EFI_FW_VOL_INSTANCE *)\r
+ (\r
+ (UINTN) ((UINT8 *) FwhRecord) + FwhRecord->VolumeHeader.HeaderLength +\r
+ (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
+ );\r
+ Instance--;\r
+ }\r
+\r
+ *FwhInstance = FwhRecord;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FvbGetPhysicalAddress (\r
+ IN UINTN Instance,\r
+ OUT EFI_PHYSICAL_ADDRESS *Address,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieves the physical address of a memory mapped FV\r
+\r
+Arguments:\r
+ Instance - The FV instance whose base address is going to be\r
+ returned\r
+ Address - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS\r
+ that on successful return, contains the base address\r
+ of the firmware volume.\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Find the right instance of the FVB private data\r
+ //\r
+ Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+ ASSERT_EFI_ERROR (Status);\r
+ *Address = FwhInstance->FvBase[Virtual];\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FvbGetVolumeAttributes (\r
+ IN UINTN Instance,\r
+ OUT EFI_FVB_ATTRIBUTES *Attributes,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieves attributes, insures positive polarity of attribute bits, returns\r
+ resulting attributes in output parameter\r
+\r
+Arguments:\r
+ Instance - The FV instance whose attributes is going to be\r
+ returned\r
+ Attributes - Output buffer which contains attributes\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Find the right instance of the FVB private data\r
+ //\r
+ Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+ ASSERT_EFI_ERROR (Status);\r
+ *Attributes = FwhInstance->VolumeHeader.Attributes;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FvbGetLbaAddress (\r
+ IN UINTN Instance,\r
+ IN EFI_LBA Lba,\r
+ OUT UINTN *LbaAddress,\r
+ OUT UINTN *LbaLength,\r
+ OUT UINTN *NumOfBlocks,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieves the starting address of an LBA in an FV\r
+\r
+Arguments:\r
+ Instance - The FV instance which the Lba belongs to\r
+ Lba - The logical block address\r
+ LbaAddress - On output, contains the physical starting address\r
+ of the Lba\r
+ LbaLength - On output, contains the length of the block\r
+ NumOfBlocks - A pointer to a caller allocated UINTN in which the\r
+ number of consecutive blocks starting with Lba is\r
+ returned. All blocks in this range have a size of\r
+ BlockSize\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+ UINT32 NumBlocks;\r
+ UINT32 BlockLength;\r
+ UINTN Offset;\r
+ EFI_LBA StartLba;\r
+ EFI_LBA NextLba;\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ EFI_FV_BLOCK_MAP_ENTRY *BlockMap;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Find the right instance of the FVB private data\r
+ //\r
+ Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ StartLba = 0;\r
+ Offset = 0;\r
+ BlockMap = &(FwhInstance->VolumeHeader.BlockMap[0]);\r
+\r
+ //\r
+ // Parse the blockmap of the FV to find which map entry the Lba belongs to\r
+ //\r
+ while (TRUE) {\r
+ NumBlocks = BlockMap->NumBlocks;\r
+ BlockLength = BlockMap->Length;\r
+\r
+ if (NumBlocks == 0 || BlockLength == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ NextLba = StartLba + NumBlocks;\r
+\r
+ //\r
+ // The map entry found\r
+ //\r
+ if (Lba >= StartLba && Lba < NextLba) {\r
+ Offset = Offset + (UINTN) MultU64x32 ((Lba - StartLba), BlockLength);\r
+ if (LbaAddress != NULL) {\r
+ *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;\r
+ }\r
+\r
+ if (LbaLength != NULL) {\r
+ *LbaLength = BlockLength;\r
+ }\r
+\r
+ if (NumOfBlocks != NULL) {\r
+ *NumOfBlocks = (UINTN) (NextLba - Lba);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ StartLba = NextLba;\r
+ Offset = Offset + NumBlocks * BlockLength;\r
+ BlockMap++;\r
+ }\r
+}\r
+\r
+EFI_STATUS\r
+FvbReadBlock (\r
+ IN UINTN Instance,\r
+ IN EFI_LBA Lba,\r
+ IN UINTN BlockOffset,\r
+ IN OUT UINTN *NumBytes,\r
+ IN UINT8 *Buffer,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Reads specified number of bytes into a buffer from the specified block\r
+\r
+Arguments:\r
+ Instance - The FV instance to be read from\r
+ Lba - The logical block address to be read from\r
+ BlockOffset - Offset into the block at which to begin reading\r
+ NumBytes - Pointer that on input contains the total size of\r
+ the buffer. On output, it contains the total number\r
+ of bytes read\r
+ Buffer - Pointer to a caller allocated buffer that will be\r
+ used to hold the data read\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was read successfully and\r
+ contents are in Buffer\r
+ EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r
+ NumBytes contains the total number of bytes returned\r
+ in Buffer\r
+ EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be read\r
+ EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r
+\r
+--*/\r
+{\r
+ EFI_FVB_ATTRIBUTES Attributes;\r
+ UINTN LbaAddress;\r
+ UINTN LbaLength;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Check for invalid conditions\r
+ //\r
+ if ((NumBytes == NULL) || (Buffer == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (*NumBytes == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ //\r
+ // Check if the FV is read enabled\r
+ //\r
+ FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
+\r
+ if ((Attributes & EFI_FVB2_READ_STATUS) == 0) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+ //\r
+ // Perform boundary checks and adjust NumBytes\r
+ //\r
+ if (BlockOffset > LbaLength) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (LbaLength < (*NumBytes + BlockOffset)) {\r
+ *NumBytes = (UINT32) (LbaLength - BlockOffset);\r
+ Status = EFI_BAD_BUFFER_SIZE;\r
+ }\r
+\r
+ CopyMem (Buffer, (UINT8 *) (LbaAddress + BlockOffset), (UINTN) (*NumBytes));\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+FvbWriteBlock (\r
+ IN UINTN Instance,\r
+ IN EFI_LBA Lba,\r
+ IN UINTN BlockOffset,\r
+ IN OUT UINTN *NumBytes,\r
+ IN UINT8 *Buffer,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Writes specified number of bytes from the input buffer to the block\r
+\r
+Arguments:\r
+ Instance - The FV instance to be written to\r
+ Lba - The starting logical block index to write to\r
+ BlockOffset - Offset into the block at which to begin writing\r
+ NumBytes - Pointer that on input contains the total size of\r
+ the buffer. On output, it contains the total number\r
+ of bytes actually written\r
+ Buffer - Pointer to a caller allocated buffer that contains\r
+ the source for the write\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was written successfully\r
+ EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r
+ NumBytes contains the total number of bytes\r
+ actually written\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written\r
+ EFI_INVALID_PARAMETER - Instance not found, or NumBytes, Buffer are NULL\r
+\r
+--*/\r
+{\r
+ EFI_FVB_ATTRIBUTES Attributes;\r
+ UINTN LbaAddress;\r
+ UINTN LbaLength;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Check for invalid conditions\r
+ //\r
+ if ((NumBytes == NULL) || (Buffer == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (*NumBytes == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ //\r
+ // Check if the FV is write enabled\r
+ //\r
+ FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
+\r
+ if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+ //\r
+ // Perform boundary checks and adjust NumBytes\r
+ //\r
+ if (BlockOffset > LbaLength) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (LbaLength < (*NumBytes + BlockOffset)) {\r
+ *NumBytes = (UINT32) (LbaLength - BlockOffset);\r
+ Status = EFI_BAD_BUFFER_SIZE;\r
+ }\r
+ //\r
+ // Write data\r
+ //\r
+ CopyMem ((UINT8 *) (LbaAddress + BlockOffset), Buffer, (UINTN) (*NumBytes));\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+FvbEraseBlock (\r
+ IN UINTN Instance,\r
+ IN EFI_LBA Lba,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Erases and initializes a firmware volume block\r
+\r
+Arguments:\r
+ Instance - The FV instance to be erased\r
+ Lba - The logical block index to be erased\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - The erase request was successfully completed\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written. Firmware device may have been\r
+ partially erased\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+\r
+ EFI_FVB_ATTRIBUTES Attributes;\r
+ UINTN LbaAddress;\r
+ UINTN LbaLength;\r
+ EFI_STATUS Status;\r
+ UINT8 Data;\r
+\r
+ //\r
+ // Check if the FV is write enabled\r
+ //\r
+ FvbGetVolumeAttributes (Instance, &Attributes, Global, Virtual);\r
+\r
+ if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+ //\r
+ // Get the starting address of the block for erase.\r
+ //\r
+ Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL, Global, Virtual);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ if ((Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
+ Data = 0xFF;\r
+ } else {\r
+ Data = 0x0;\r
+ }\r
+\r
+ SetMem ((UINT8 *) LbaAddress, LbaLength, Data);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FvbEraseCustomBlockRange (\r
+ IN UINTN Instance,\r
+ IN EFI_LBA StartLba,\r
+ IN UINTN OffsetStartLba,\r
+ IN EFI_LBA LastLba,\r
+ IN UINTN OffsetLastLba,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Erases and initializes a specified range of a firmware volume\r
+\r
+Arguments:\r
+ Instance - The FV instance to be erased\r
+ StartLba - The starting logical block index to be erased\r
+ OffsetStartLba - Offset into the starting block at which to\r
+ begin erasing\r
+ LastLba - The last logical block index to be erased\r
+ OffsetStartLba - Offset into the last block at which to end erasing\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was erased successfully\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written. Firmware device may have been\r
+ partially erased\r
+ EFI_INVALID_PARAMETER - Instance not found\r
+\r
+--*/\r
+{\r
+ EFI_LBA Index;\r
+ UINTN LbaSize;\r
+ UINTN ScratchLbaSizeData;\r
+\r
+ //\r
+ // First LBA\r
+ //\r
+ FvbGetLbaAddress (Instance, StartLba, NULL, &LbaSize, NULL, Global, Virtual);\r
+\r
+ //\r
+ // Use the scratch space as the intermediate buffer to transfer data\r
+ // Back up the first LBA in scratch space.\r
+ //\r
+ FvbReadBlock (Instance, StartLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
+\r
+ //\r
+ // erase now\r
+ //\r
+ FvbEraseBlock (Instance, StartLba, Global, Virtual);\r
+ ScratchLbaSizeData = OffsetStartLba;\r
+\r
+ //\r
+ // write the data back to the first block\r
+ //\r
+ if (ScratchLbaSizeData > 0) {\r
+ FvbWriteBlock (Instance, StartLba, 0, &ScratchLbaSizeData, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
+ }\r
+ //\r
+ // Middle LBAs\r
+ //\r
+ if (LastLba > (StartLba + 1)) {\r
+ for (Index = (StartLba + 1); Index <= (LastLba - 1); Index++) {\r
+ FvbEraseBlock (Instance, Index, Global, Virtual);\r
+ }\r
+ }\r
+ //\r
+ // Last LBAs, the same as first LBAs\r
+ //\r
+ if (LastLba > StartLba) {\r
+ FvbGetLbaAddress (Instance, LastLba, NULL, &LbaSize, NULL, Global, Virtual);\r
+ FvbReadBlock (Instance, LastLba, 0, &LbaSize, Global->FvbScratchSpace[Virtual], Global, Virtual);\r
+ FvbEraseBlock (Instance, LastLba, Global, Virtual);\r
+ }\r
+\r
+ ScratchLbaSizeData = LbaSize - (OffsetStartLba + 1);\r
+\r
+ return FvbWriteBlock (\r
+ Instance,\r
+ LastLba,\r
+ (OffsetLastLba + 1),\r
+ &ScratchLbaSizeData,\r
+ Global->FvbScratchSpace[Virtual],\r
+ Global,\r
+ Virtual\r
+ );\r
+}\r
+\r
+EFI_STATUS\r
+FvbSetVolumeAttributes (\r
+ IN UINTN Instance,\r
+ IN OUT EFI_FVB_ATTRIBUTES *Attributes,\r
+ IN ESAL_FWB_GLOBAL *Global,\r
+ IN BOOLEAN Virtual\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Modifies the current settings of the firmware volume according to the\r
+ input parameter, and returns the new setting of the volume\r
+\r
+Arguments:\r
+ Instance - The FV instance whose attributes is going to be\r
+ modified\r
+ Attributes - On input, it is a pointer to EFI_FVB_ATTRIBUTES\r
+ containing the desired firmware volume settings.\r
+ On successful return, it contains the new settings\r
+ of the firmware volume\r
+ Global - Pointer to ESAL_FWB_GLOBAL that contains all\r
+ instance data\r
+ Virtual - Whether CPU is in virtual or physical mode\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+ EFI_ACCESS_DENIED - The volume setting is locked and cannot be modified\r
+ EFI_INVALID_PARAMETER - Instance not found, or The attributes requested are\r
+ in conflict with the capabilities as declared in the\r
+ firmware volume header\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ EFI_FVB_ATTRIBUTES OldAttributes;\r
+ EFI_FVB_ATTRIBUTES *AttribPtr;\r
+ UINT32 Capabilities;\r
+ UINT32 OldStatus;\r
+ UINT32 NewStatus;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Find the right instance of the FVB private data\r
+ //\r
+ Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ AttribPtr = (EFI_FVB_ATTRIBUTES *) &(FwhInstance->VolumeHeader.Attributes);\r
+ OldAttributes = *AttribPtr;\r
+ Capabilities = OldAttributes & (EFI_FVB2_READ_DISABLED_CAP | \\r
+ EFI_FVB2_READ_ENABLED_CAP | \\r
+ EFI_FVB2_WRITE_DISABLED_CAP | \\r
+ EFI_FVB2_WRITE_ENABLED_CAP | \\r
+ EFI_FVB2_LOCK_CAP \\r
+ );\r
+ OldStatus = OldAttributes & EFI_FVB2_STATUS;\r
+ NewStatus = *Attributes & EFI_FVB2_STATUS;\r
+\r
+ //\r
+ // If firmware volume is locked, no status bit can be updated\r
+ //\r
+ if (OldAttributes & EFI_FVB2_LOCK_STATUS) {\r
+ if (OldStatus ^ NewStatus) {\r
+ return EFI_ACCESS_DENIED;\r
+ }\r
+ }\r
+ //\r
+ // Test read disable\r
+ //\r
+ if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {\r
+ if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+ //\r
+ // Test read enable\r
+ //\r
+ if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {\r
+ if (NewStatus & EFI_FVB2_READ_STATUS) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+ //\r
+ // Test write disable\r
+ //\r
+ if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {\r
+ if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+ //\r
+ // Test write enable\r
+ //\r
+ if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {\r
+ if (NewStatus & EFI_FVB2_WRITE_STATUS) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+ //\r
+ // Test lock\r
+ //\r
+ if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {\r
+ if (NewStatus & EFI_FVB2_LOCK_STATUS) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+\r
+ *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));\r
+ *AttribPtr = (*AttribPtr) | NewStatus;\r
+ *Attributes = *AttribPtr;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+//\r
+// FVB protocol APIs\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolGetPhysicalAddress (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ OUT EFI_PHYSICAL_ADDRESS *Address\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Retrieves the physical address of the device.\r
+\r
+Arguments:\r
+\r
+ This - Calling context\r
+ Address - Output buffer containing the address.\r
+\r
+Returns:\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbGetPhysicalAddress (FvbDevice->Instance, Address, mFvbModuleGlobal, EfiGoneVirtual ());\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolGetBlockSize (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ IN CONST EFI_LBA Lba,\r
+ OUT UINTN *BlockSize,\r
+ OUT UINTN *NumOfBlocks\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieve the size of a logical block\r
+\r
+Arguments:\r
+ This - Calling context\r
+ Lba - Indicates which block to return the size for.\r
+ BlockSize - A pointer to a caller allocated UINTN in which\r
+ the size of the block is returned\r
+ NumOfBlocks - a pointer to a caller allocated UINTN in which the\r
+ number of consecutive blocks starting with Lba is\r
+ returned. All blocks in this range have a size of\r
+ BlockSize\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was read successfully and\r
+ contents are in Buffer\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbGetLbaAddress (\r
+ FvbDevice->Instance,\r
+ Lba,\r
+ NULL,\r
+ BlockSize,\r
+ NumOfBlocks,\r
+ mFvbModuleGlobal,\r
+ EfiGoneVirtual ()\r
+ );\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolGetAttributes (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ OUT EFI_FVB_ATTRIBUTES *Attributes\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Retrieves Volume attributes. No polarity translations are done.\r
+\r
+Arguments:\r
+ This - Calling context\r
+ Attributes - output buffer which contains attributes\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbGetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolSetAttributes (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ IN OUT EFI_FVB_ATTRIBUTES *Attributes\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Sets Volume attributes. No polarity translations are done.\r
+\r
+Arguments:\r
+ This - Calling context\r
+ Attributes - output buffer which contains attributes\r
+\r
+Returns:\r
+ EFI_SUCCESS - Successfully returns\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbSetVolumeAttributes (FvbDevice->Instance, Attributes, mFvbModuleGlobal, EfiGoneVirtual ());\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolEraseBlocks (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ ...\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ The EraseBlock() function erases one or more blocks as denoted by the\r
+ variable argument list. The entire parameter list of blocks must be verified\r
+ prior to erasing any blocks. If a block is requested that does not exist\r
+ within the associated firmware volume (it has a larger index than the last\r
+ block of the firmware volume), the EraseBlock() function must return\r
+ EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.\r
+\r
+Arguments:\r
+ This - Calling context\r
+ ... - Starting LBA followed by Number of Lba to erase.\r
+ a -1 to terminate the list.\r
+\r
+Returns:\r
+ EFI_SUCCESS - The erase request was successfully completed\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written. Firmware device may have been\r
+ partially erased\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ UINTN NumOfBlocks;\r
+ VA_LIST args;\r
+ EFI_LBA StartingLba;\r
+ UINTN NumOfLba;\r
+ EFI_STATUS Status;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ Status = GetFvbInstance (FvbDevice->Instance, mFvbModuleGlobal, &FwhInstance, EfiGoneVirtual ());\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ NumOfBlocks = FwhInstance->NumOfBlocks;\r
+\r
+ VA_START (args, This);\r
+\r
+ do {\r
+ StartingLba = VA_ARG (args, EFI_LBA);\r
+ if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
+ break;\r
+ }\r
+\r
+ NumOfLba = VA_ARG (args, UINT32);\r
+\r
+ //\r
+ // Check input parameters\r
+ //\r
+ if (NumOfLba == 0) {\r
+ VA_END (args);\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((StartingLba + NumOfLba) > NumOfBlocks) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ } while (1);\r
+\r
+ VA_END (args);\r
+\r
+ VA_START (args, This);\r
+ do {\r
+ StartingLba = VA_ARG (args, EFI_LBA);\r
+ if (StartingLba == EFI_LBA_LIST_TERMINATOR) {\r
+ break;\r
+ }\r
+\r
+ NumOfLba = VA_ARG (args, UINT32);\r
+\r
+ while (NumOfLba > 0) {\r
+ Status = FvbEraseBlock (FvbDevice->Instance, StartingLba, mFvbModuleGlobal, EfiGoneVirtual ());\r
+ if (EFI_ERROR (Status)) {\r
+ VA_END (args);\r
+ return Status;\r
+ }\r
+\r
+ StartingLba++;\r
+ NumOfLba--;\r
+ }\r
+\r
+ } while (1);\r
+\r
+ VA_END (args);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolWrite (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ IN CONST EFI_LBA Lba,\r
+ IN CONST UINTN Offset,\r
+ IN OUT UINTN *NumBytes,\r
+ IN CONST UINT8 *Buffer\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Writes data beginning at Lba:Offset from FV. The write terminates either\r
+ when *NumBytes of data have been written, or when a block boundary is\r
+ reached. *NumBytes is updated to reflect the actual number of bytes\r
+ written. The write opertion does not include erase. This routine will\r
+ attempt to write only the specified bytes. If the writes do not stick,\r
+ it will return an error.\r
+\r
+Arguments:\r
+ This - Calling context\r
+ Lba - Block in which to begin write\r
+ Offset - Offset in the block at which to begin write\r
+ NumBytes - On input, indicates the requested write size. On\r
+ output, indicates the actual number of bytes written\r
+ Buffer - Buffer containing source data for the write.\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was written successfully\r
+ EFI_BAD_BUFFER_SIZE - Write attempted across a LBA boundary. On output,\r
+ NumBytes contains the total number of bytes\r
+ actually written\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written\r
+ EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r
+\r
+--*/\r
+{\r
+\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbProtocolRead (\r
+ IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r
+ IN CONST EFI_LBA Lba,\r
+ IN CONST UINTN Offset,\r
+ IN OUT UINTN *NumBytes,\r
+ IN UINT8 *Buffer\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Reads data beginning at Lba:Offset from FV. The Read terminates either\r
+ when *NumBytes of data have been read, or when a block boundary is\r
+ reached. *NumBytes is updated to reflect the actual number of bytes\r
+ written. The write opertion does not include erase. This routine will\r
+ attempt to write only the specified bytes. If the writes do not stick,\r
+ it will return an error.\r
+\r
+Arguments:\r
+ This - Calling context\r
+ Lba - Block in which to begin Read\r
+ Offset - Offset in the block at which to begin Read\r
+ NumBytes - On input, indicates the requested write size. On\r
+ output, indicates the actual number of bytes Read\r
+ Buffer - Buffer containing source data for the Read.\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was read successfully and\r
+ contents are in Buffer\r
+ EFI_BAD_BUFFER_SIZE - Read attempted across a LBA boundary. On output,\r
+ NumBytes contains the total number of bytes returned\r
+ in Buffer\r
+ EFI_ACCESS_DENIED - The firmware volume is in the ReadDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be read\r
+ EFI_INVALID_PARAMETER - NumBytes or Buffer are NULL\r
+\r
+--*/\r
+{\r
+\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r
+}\r
+//\r
+// FVB Extension Protocols\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+FvbExtendProtocolEraseCustomBlockRange (\r
+ IN EFI_FVB_EXTENSION_PROTOCOL *This,\r
+ IN EFI_LBA StartLba,\r
+ IN UINTN OffsetStartLba,\r
+ IN EFI_LBA LastLba,\r
+ IN UINTN OffsetLastLba\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Erases and initializes a specified range of a firmware volume\r
+\r
+Arguments:\r
+ This - Calling context\r
+ StartLba - The starting logical block index to be erased\r
+ OffsetStartLba - Offset into the starting block at which to\r
+ begin erasing\r
+ LastLba - The last logical block index to be erased\r
+ OffsetStartLba - Offset into the last block at which to end erasing\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume was erased successfully\r
+ EFI_ACCESS_DENIED - The firmware volume is in the WriteDisabled state\r
+ EFI_DEVICE_ERROR - The block device is not functioning correctly and\r
+ could not be written. Firmware device may have been\r
+ partially erased\r
+\r
+--*/\r
+{\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+\r
+ FvbDevice = FVB_EXTEND_DEVICE_FROM_THIS (This);\r
+\r
+ return FvbEraseCustomBlockRange (\r
+ FvbDevice->Instance,\r
+ StartLba,\r
+ OffsetStartLba,\r
+ LastLba,\r
+ OffsetLastLba,\r
+ mFvbModuleGlobal,\r
+ EfiGoneVirtual ()\r
+ );\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ValidateFvHeader (\r
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Check the integrity of firmware volume header\r
+\r
+Arguments:\r
+ FwVolHeader - A pointer to a firmware volume header\r
+\r
+Returns:\r
+ EFI_SUCCESS - The firmware volume is consistent\r
+ EFI_NOT_FOUND - The firmware volume has corrupted. So it is not an FV\r
+\r
+--*/\r
+{\r
+ UINT16 *Ptr;\r
+ UINT16 HeaderLength;\r
+ UINT16 Checksum;\r
+\r
+ //\r
+ // Verify the header revision, header signature, length\r
+ // Length of FvBlock cannot be 2**64-1\r
+ // HeaderLength cannot be an odd number\r
+ //\r
+ if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r
+ (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r
+ (FwVolHeader->FvLength == ((UINTN) -1)) ||\r
+ ((FwVolHeader->HeaderLength & 0x01) != 0)\r
+ ) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ //\r
+ // Verify the header checksum\r
+ //\r
+ HeaderLength = (UINT16) (FwVolHeader->HeaderLength / 2);\r
+ Ptr = (UINT16 *) FwVolHeader;\r
+ Checksum = 0;\r
+ while (HeaderLength > 0) {\r
+ Checksum = Checksum + (*Ptr);\r
+ HeaderLength--;\r
+ Ptr++;\r
+ }\r
+\r
+ if (Checksum != 0) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+FvbInitialize (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ This function does common initialization for FVB services\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_FW_VOL_INSTANCE *FwhInstance;\r
+ EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
+ EFI_DXE_SERVICES *DxeServices;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
+ UINT32 BufferSize;\r
+ EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r
+ EFI_HANDLE FwbHandle;\r
+ EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r
+ EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;\r
+ EFI_DEVICE_PATH_PROTOCOL *TempFwbDevicePath;\r
+ FV_DEVICE_PATH TempFvbDevicePathData;\r
+ UINT32 MaxLbaSize;\r
+ EFI_PHYSICAL_ADDRESS BaseAddress;\r
+ UINT64 Length;\r
+ UINTN NumOfBlocks;\r
+ EFI_PEI_HOB_POINTERS FvHob;\r
+\r
+ //\r
+ // Get the DXE services table\r
+ //\r
+ DxeServices = gDS;\r
+\r
+ //\r
+ // Allocate runtime services data for global variable, which contains\r
+ // the private data of all firmware volume block instances\r
+ //\r
+ mFvbModuleGlobal = AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL));\r
+ ASSERT (mFvbModuleGlobal != NULL);\r
+\r
+ //\r
+ // Calculate the total size for all firmware volume block instances\r
+ //\r
+ BufferSize = 0;\r
+\r
+ FvHob.Raw = GetHobList ();\r
+ while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {\r
+ BaseAddress = FvHob.FirmwareVolume->BaseAddress;\r
+ Length = FvHob.FirmwareVolume->Length;\r
+ //\r
+ // Check if it is a "real" flash\r
+ //\r
+ Status = DxeServices->GetMemorySpaceDescriptor (\r
+ BaseAddress,\r
+ &Descriptor\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+\r
+ if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ continue;\r
+ }\r
+\r
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
+ Status = ValidateFvHeader (FwVolHeader);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // Get FvbInfo\r
+ //\r
+ Status = GetFvbInfo (Length, &FwVolHeader);\r
+ if (EFI_ERROR (Status)) {\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ continue;\r
+ }\r
+ }\r
+\r
+ BufferSize += (sizeof (EFI_FW_VOL_INSTANCE) + FwVolHeader->HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ }\r
+\r
+ //\r
+ // Only need to allocate once. There is only one copy of physical memory for\r
+ // the private data of each FV instance. But in virtual mode or in physical\r
+ // mode, the address of the the physical memory may be different.\r
+ //\r
+ mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] = AllocateRuntimePool (BufferSize);\r
+ ASSERT (mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] != NULL);\r
+\r
+ //\r
+ // Make a virtual copy of the FvInstance pointer.\r
+ //\r
+ FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
+ mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;\r
+\r
+ mFvbModuleGlobal->NumFv = 0;\r
+ MaxLbaSize = 0;\r
+\r
+ FvHob.Raw = GetHobList ();\r
+ while (NULL != (FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw))) {\r
+ BaseAddress = FvHob.FirmwareVolume->BaseAddress;\r
+ Length = FvHob.FirmwareVolume->Length;\r
+ //\r
+ // Check if it is a "real" flash\r
+ //\r
+ Status = DxeServices->GetMemorySpaceDescriptor (\r
+ BaseAddress,\r
+ &Descriptor\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+\r
+ if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeMemoryMappedIo) {\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ continue;\r
+ }\r
+\r
+ FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
+ Status = ValidateFvHeader (FwVolHeader);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // Get FvbInfo to provide in FwhInstance.\r
+ //\r
+ Status = GetFvbInfo (Length, &FwVolHeader);\r
+ if (EFI_ERROR (Status)) {\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ continue;\r
+ }\r
+ //\r
+ // Write healthy FV header back.\r
+ //\r
+ CopyMem (\r
+ (VOID *) (UINTN) BaseAddress,\r
+ (VOID *) FwVolHeader,\r
+ FwVolHeader->HeaderLength\r
+ );\r
+ }\r
+\r
+ FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;\r
+ FwhInstance->FvBase[FVB_VIRTUAL] = (UINTN) BaseAddress;\r
+\r
+ CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);\r
+ FwVolHeader = &(FwhInstance->VolumeHeader);\r
+ EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r
+\r
+ NumOfBlocks = 0;\r
+\r
+ for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
+ //\r
+ // Get the maximum size of a block. The size will be used to allocate\r
+ // buffer for Scratch space, the intermediate buffer for FVB extension\r
+ // protocol\r
+ //\r
+ if (MaxLbaSize < PtrBlockMapEntry->Length) {\r
+ MaxLbaSize = PtrBlockMapEntry->Length;\r
+ }\r
+\r
+ NumOfBlocks = NumOfBlocks + PtrBlockMapEntry->NumBlocks;\r
+ }\r
+ //\r
+ // The total number of blocks in the FV.\r
+ //\r
+ FwhInstance->NumOfBlocks = NumOfBlocks;\r
+\r
+ //\r
+ // Add a FVB Protocol Instance\r
+ //\r
+ FvbDevice = AllocateRuntimePool (sizeof (EFI_FW_VOL_BLOCK_DEVICE));\r
+ ASSERT (FvbDevice != NULL);\r
+\r
+ CopyMem (FvbDevice, &mFvbDeviceTemplate, sizeof (EFI_FW_VOL_BLOCK_DEVICE));\r
+\r
+ FvbDevice->Instance = mFvbModuleGlobal->NumFv;\r
+ mFvbModuleGlobal->NumFv++;\r
+\r
+ //\r
+ // Set up the devicepath\r
+ //\r
+ FvbDevice->DevicePath.MemMapDevPath.StartingAddress = BaseAddress;\r
+ FvbDevice->DevicePath.MemMapDevPath.EndingAddress = BaseAddress + (FwVolHeader->FvLength - 1);\r
+\r
+ //\r
+ // Find a handle with a matching device path that has supports FW Block protocol\r
+ //\r
+ TempFwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) &TempFvbDevicePathData;\r
+ CopyMem (TempFwbDevicePath, &FvbDevice->DevicePath, sizeof (FV_DEVICE_PATH));\r
+ Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &TempFwbDevicePath, &FwbHandle);\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // LocateDevicePath fails so install a new interface and device path\r
+ //\r
+ FwbHandle = NULL;\r
+ Status = gBS->InstallMultipleProtocolInterfaces (\r
+ &FwbHandle,\r
+ &gEfiFirmwareVolumeBlockProtocolGuid,\r
+ &FvbDevice->FwVolBlockInstance,\r
+ &gEfiDevicePathProtocolGuid,\r
+ &FvbDevice->DevicePath,\r
+ NULL\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ } else if (EfiIsDevicePathEnd (TempFwbDevicePath)) {\r
+ //\r
+ // Device allready exists, so reinstall the FVB protocol\r
+ //\r
+ Status = gBS->HandleProtocol (\r
+ FwbHandle,\r
+ &gEfiFirmwareVolumeBlockProtocolGuid,\r
+ &OldFwbInterface\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ Status = gBS->ReinstallProtocolInterface (\r
+ FwbHandle,\r
+ &gEfiFirmwareVolumeBlockProtocolGuid,\r
+ OldFwbInterface,\r
+ &FvbDevice->FwVolBlockInstance\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ } else {\r
+ //\r
+ // There was a FVB protocol on an End Device Path node\r
+ //\r
+ ASSERT (FALSE);\r
+ }\r
+ //\r
+ // Install FVB Extension Protocol on the same handle\r
+ //\r
+ Status = gBS->InstallMultipleProtocolInterfaces (\r
+ &FwbHandle,\r
+ &gEfiFvbExtensionProtocolGuid,\r
+ &FvbDevice->FvbExtension,\r
+ &gEfiAlternateFvBlockGuid,\r
+ NULL,\r
+ NULL\r
+ );\r
+\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
+ (\r
+ (UINTN) ((UINT8 *) FwhInstance) + FwVolHeader->HeaderLength +\r
+ (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
+ );\r
+\r
+ FvHob.Raw = GET_NEXT_HOB (FvHob);\r
+ }\r
+\r
+ //\r
+ // Allocate for scratch space, an intermediate buffer for FVB extention\r
+ //\r
+ mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL] = AllocateRuntimePool (MaxLbaSize);\r
+ ASSERT (mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL] != NULL);\r
+\r
+ mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL] = mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL];\r
+\r
+ return EFI_SUCCESS;\r
+}\r