]> git.proxmox.com Git - mirror_edk2.git/blobdiff - QuarkPlatformPkg/Platform/SpiFvbServices/FwBlockService.c
edk2: Remove packages moved to edk2-platforms
[mirror_edk2.git] / QuarkPlatformPkg / Platform / SpiFvbServices / FwBlockService.c
diff --git a/QuarkPlatformPkg/Platform/SpiFvbServices/FwBlockService.c b/QuarkPlatformPkg/Platform/SpiFvbServices/FwBlockService.c
deleted file mode 100644 (file)
index 5ecb1cf..0000000
+++ /dev/null
@@ -1,2053 +0,0 @@
-/** @file\r
-\r
-Copyright (c) 2013-2016 Intel Corporation.\r
-\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-\r
-**/\r
-\r
-#include "FwBlockService.h"\r
-\r
-ESAL_FWB_GLOBAL         *mFvbModuleGlobal;\r
-\r
-EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {\r
-  FVB_DEVICE_SIGNATURE,  // Signature\r
-  //\r
-  // FV_DEVICE_PATH                      FvDevicePath\r
-  //\r
-  {\r
-    {\r
-      {\r
-        HARDWARE_DEVICE_PATH,\r
-        HW_MEMMAP_DP,\r
-        {\r
-          (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),\r
-          (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)\r
-        }\r
-      },\r
-      EfiMemoryMappedIO,\r
-      (EFI_PHYSICAL_ADDRESS) 0,\r
-      (EFI_PHYSICAL_ADDRESS) 0\r
-    },\r
-    {\r
-      END_DEVICE_PATH_TYPE,\r
-      END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
-      {\r
-        END_DEVICE_PATH_LENGTH,\r
-        0\r
-      }\r
-    }\r
-  },\r
-  //\r
-  //   UEFI_FV_DEVICE_PATH                 UefiFvDevicePath\r
-  //\r
-  {\r
-    {\r
-      {\r
-        MEDIA_DEVICE_PATH,\r
-        MEDIA_PIWG_FW_VOL_DP,\r
-        {\r
-          (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),\r
-          (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)\r
-        }\r
-      },\r
-      { 0 }\r
-    },\r
-    {\r
-      END_DEVICE_PATH_TYPE,\r
-      END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
-      {\r
-        END_DEVICE_PATH_LENGTH,\r
-        0\r
-      }\r
-    }\r
-  },\r
-  0,      // Instance\r
-  //\r
-  // EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  FwVolBlockInstance\r
-  //\r
-  {\r
-    FvbProtocolGetAttributes,\r
-    FvbProtocolSetAttributes,\r
-    FvbProtocolGetPhysicalAddress,\r
-    FvbProtocolGetBlockSize,\r
-    FvbProtocolRead,\r
-    FvbProtocolWrite,\r
-    FvbProtocolEraseBlocks,\r
-    NULL\r
-  }\r
-};\r
-\r
-UINT32 mInSmmMode = 0;\r
-EFI_SMM_SYSTEM_TABLE2*   mSmst = NULL;\r
-\r
-VOID\r
-PublishFlashDeviceInfo (\r
-  IN SPI_INIT_TABLE   *Found\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Publish info on found flash device to other drivers via PcdSpiFlashDeviceSize.\r
-\r
-Arguments:\r
-  Found                 - Pointer to entry in mSpiInitTable for found flash part.\r
-\r
-Returns:\r
-  None\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  //\r
-  // Publish Byte Size of found flash device.\r
-  //\r
-  Status = PcdSet32S (PcdSpiFlashDeviceSize, (UINT32)(Found->BiosStartOffset + Found->BiosSize));\r
-  ASSERT_EFI_ERROR (Status);\r
-}\r
-\r
-VOID\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
-  gRT->ConvertPointer (EFI_INTERNAL_POINTER, (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
-\r
-  gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r
-    //\r
-    // SpiWrite and SpiErase always use Physical Address instead of\r
-    // Virtual Address, even in Runtime. So we need not convert pointer\r
-    // for FvWriteBase[FVB_VIRTUAL]\r
-    //\r
-    // EfiConvertPointer (0, (VOID **) &FwhInstance->FvWriteBase[FVB_VIRTUAL]);\r
-    //\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
-  gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r
-  //\r
-  // Convert SPI_PROTOCOL instance for runtime\r
-  //\r
-  gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID **) &mFvbModuleGlobal->SpiProtocol);\r
-  gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID **) &mFvbModuleGlobal);\r
-}\r
-\r
-VOID\r
-FvbMemWrite8 (\r
-  IN  UINT64                              Dest,\r
-  IN  UINT8                               Byte\r
-  )\r
-{\r
-  MmioWrite8 ((UINTN)Dest, Byte);\r
-\r
-  return ;\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
-  FwhInstance = NULL;\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_2                  *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
-  FwhInstance = NULL;\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                               *LbaWriteAddress,\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
-  LbaWriteAddress       - On output, contains the physical starting address\r
-                          of the Lba for writing\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
-  FwhInstance = NULL;\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) {\r
-        *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;\r
-      }\r
-\r
-      if (LbaWriteAddress) {\r
-        *LbaWriteAddress = FwhInstance->FvWriteBase[Virtual] + Offset;\r
-      }\r
-\r
-      if (LbaLength) {\r
-        *LbaLength = BlockLength;\r
-      }\r
-\r
-      if (NumOfBlocks) {\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_2  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, NULL, &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
-  MmioReadBuffer8 (LbaAddress + BlockOffset, (UINTN) *NumBytes, Buffer);\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FlashFdWrite (\r
-  IN  UINTN                               WriteAddress,\r
-  IN  UINTN                               Address,\r
-  IN OUT UINTN                            *NumBytes,\r
-  IN  UINT8                               *Buffer,\r
-  IN  UINTN                               LbaLength\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  Writes specified number of bytes from the input buffer to the address\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  Status = EFI_SUCCESS;\r
-\r
-  //\r
-  // TODO:  Suggested that this code be "critical section"\r
-  //\r
-  WriteAddress -= ( PcdGet32 (PcdFlashAreaBaseAddress) );\r
-  if (mInSmmMode == 0) { // !(EfiInManagementInterrupt ())) {\r
-    Status = mFvbModuleGlobal->SpiProtocol->Execute (\r
-                                            mFvbModuleGlobal->SpiProtocol,\r
-                                            SPI_OPCODE_WRITE_INDEX, // OpcodeIndex\r
-                                            0,                      // PrefixOpcodeIndex\r
-                                            TRUE,                   // DataCycle\r
-                                            TRUE,                   // Atomic\r
-                                            TRUE,                   // ShiftOut\r
-                                            WriteAddress,           // Address\r
-                                            (UINT32) (*NumBytes),   // Data Number\r
-                                            Buffer,\r
-                                            EnumSpiRegionBios\r
-                                            );\r
-\r
-  } else {\r
-    Status = mFvbModuleGlobal->SmmSpiProtocol->Execute (\r
-                                            mFvbModuleGlobal->SmmSpiProtocol,\r
-                                            SPI_OPCODE_WRITE_INDEX, // OpcodeIndex\r
-                                            0,                      // PrefixOpcodeIndex\r
-                                            TRUE,                   // DataCycle\r
-                                            TRUE,                   // Atomic\r
-                                            TRUE,                   // ShiftOut\r
-                                            WriteAddress,           // Address\r
-                                            (UINT32) (*NumBytes),   // Data Number\r
-                                            Buffer,\r
-                                            EnumSpiRegionBios\r
-                                            );\r
-  }\r
-\r
-    AsmWbinvd ();\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FlashFdErase (\r
-  IN UINTN                                WriteAddress,\r
-  IN UINTN                                Address,\r
-  IN UINTN                                LbaLength\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  Erase a certain block from address LbaWriteAddress\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  WriteAddress -= (PcdGet32 (PcdFlashAreaBaseAddress));\r
-  if (mInSmmMode == 0 ) { // !(EfiInManagementInterrupt ())) {\r
-    Status = mFvbModuleGlobal->SpiProtocol->Execute (\r
-                                            mFvbModuleGlobal->SpiProtocol,\r
-                                            SPI_OPCODE_ERASE_INDEX, // OpcodeIndex\r
-                                            0,                      // PrefixOpcodeIndex\r
-                                            FALSE,                  // DataCycle\r
-                                            TRUE,                   // Atomic\r
-                                            FALSE,                  // ShiftOut\r
-                                            WriteAddress,           // Address\r
-                                            0,                      // Data Number\r
-                                            NULL,\r
-                                            EnumSpiRegionBios       // SPI_REGION_TYPE\r
-                                            );\r
-  } else {\r
-    Status = mFvbModuleGlobal->SmmSpiProtocol->Execute (\r
-                                            mFvbModuleGlobal->SmmSpiProtocol,\r
-                                            SPI_OPCODE_ERASE_INDEX, // OpcodeIndex\r
-                                            0,                      // PrefixOpcodeIndex\r
-                                            FALSE,                  // DataCycle\r
-                                            TRUE,                   // Atomic\r
-                                            FALSE,                  // ShiftOut\r
-                                            WriteAddress,           // Address\r
-                                            0,                      // Data Number\r
-                                            NULL,\r
-                                            EnumSpiRegionBios       // SPI_REGION_TYPE\r
-                                            );\r
-  }\r
-\r
-  AsmWbinvd ();\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_2  Attributes;\r
-  UINTN               LbaAddress;\r
-  UINTN               LbaWriteAddress;\r
-  UINTN               LbaLength;\r
-  EFI_FW_VOL_INSTANCE *FwhInstance;\r
-  EFI_STATUS          Status;\r
-  EFI_STATUS          ReturnStatus;\r
-\r
-  FwhInstance = NULL;\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
-  //\r
-  // Writes are enabled in the init routine itself\r
-  //\r
-  if (!FwhInstance->WriteEnabled) {\r
-    return EFI_ACCESS_DENIED;\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, &LbaWriteAddress, &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
-  ReturnStatus = FlashFdWrite (\r
-                  LbaWriteAddress + BlockOffset,\r
-                  LbaAddress,\r
-                  NumBytes,\r
-                  Buffer,\r
-                  LbaLength\r
-                  );\r
-  if (EFI_ERROR (ReturnStatus)) {\r
-    return ReturnStatus;\r
-  }\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_2  Attributes;\r
-  UINTN               LbaAddress;\r
-  UINTN               LbaWriteAddress;\r
-  EFI_FW_VOL_INSTANCE *FwhInstance;\r
-  UINTN               LbaLength;\r
-  EFI_STATUS          Status;\r
-  UINTN               SectorNum;\r
-  UINTN               Index;\r
-\r
-  FwhInstance = NULL;\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
-  //\r
-  // Writes are enabled in the init routine itself\r
-  //\r
-  if (!FwhInstance->WriteEnabled) {\r
-    return EFI_ACCESS_DENIED;\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. For debug reasons,\r
-  // LbaWriteAddress may not be the same as LbaAddress.\r
-  //\r
-  Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaWriteAddress, &LbaLength, NULL, Global, Virtual);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  SectorNum = LbaLength / SPI_ERASE_SECTOR_SIZE;\r
-  for (Index = 0; Index < SectorNum; Index++){\r
-    Status = FlashFdErase (\r
-               LbaWriteAddress + Index * SPI_ERASE_SECTOR_SIZE,\r
-               LbaAddress,\r
-               SPI_ERASE_SECTOR_SIZE\r
-               );\r
-    if (Status != EFI_SUCCESS){\r
-      break;\r
-    }\r
-  }\r
-\r
-  return Status;\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, 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, 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_2               *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_2\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_2  OldAttributes;\r
-  EFI_FVB_ATTRIBUTES_2  *AttribPtr;\r
-  UINT32              Capabilities;\r
-  UINT32              OldStatus;\r
-  UINT32              NewStatus;\r
-  EFI_STATUS          Status;\r
-\r
-  FwhInstance = NULL;\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_2 *) &(FwhInstance->VolumeHeader.Attributes);\r
-  OldAttributes = *AttribPtr;\r
-  Capabilities  = OldAttributes & EFI_FVB2_CAPABILITIES;\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
-FvbProtocolGetBlockSize (\r
-  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL       *This,\r
-  IN  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
-          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_2                            *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_2                         *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
-  FwhInstance = NULL;\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, UINTN);\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 (TRUE);\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, UINTN);\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 (TRUE);\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 EFI_LBA                                        Lba,\r
-  IN UINTN                                          Offset,\r
-  IN OUT UINTN                                      *NumBytes,\r
-  IN 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 EFI_LBA                                        Lba,\r
-  IN 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
-  EFI_STATUS              Status;\r
-\r
-  FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
-  Status    = FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\r
-\r
-  return Status;\r
-}\r
-\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
-  #ifndef R864_BUILD\r
-  if (((FwVolHeader->Revision != EFI_FVH_REVISION) && (FwVolHeader->Revision != EFI_FVH_REVISION)) ||\r
-  #else\r
-  if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r
-  #endif\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
-    Ptr++;\r
-    HeaderLength--;\r
-  }\r
-\r
-  if (Checksum != 0) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-GetFvbHeader (\r
-  VOID                                **HobList,\r
-  OUT EFI_FIRMWARE_VOLUME_HEADER      **FwVolHeader,\r
-  OUT EFI_PHYSICAL_ADDRESS            *BaseAddress,\r
-  OUT BOOLEAN                         *WriteBack\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-\r
-  Status        = EFI_SUCCESS;\r
-  *WriteBack    = FALSE;\r
-\r
-  if (*FwVolHeader == NULL) {\r
-    *BaseAddress = PcdGet32 (PcdFlashFvRecoveryBase);\r
-  } else if (*FwVolHeader == (VOID *)(UINTN)PcdGet32 (PcdFlashFvRecoveryBase)) {\r
-    *BaseAddress = PcdGet32 (PcdFlashFvMainBase);\r
-  } else if (*FwVolHeader == (VOID *)(UINTN)PcdGet32 (PcdFlashFvMainBase)) {\r
-    *BaseAddress = PcdGet32 (PcdFlashNvStorageVariableBase);\r
-  } else {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  DEBUG((EFI_D_INFO, "Fvb base : %08x\n",*BaseAddress));\r
-\r
-  *FwVolHeader  = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (*BaseAddress);\r
-  Status        = ValidateFvHeader (*FwVolHeader);\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // Get FvbInfo\r
-    //\r
-    *WriteBack  = TRUE;\r
-\r
-    Status      = GetFvbInfo (*BaseAddress, FwVolHeader);\r
-    DEBUG(( DEBUG_ERROR, "Through GetFvbInfo: %08x!\n",*BaseAddress));\r
-\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-SmmSpiInit (\r
-  VOID\r
-  )\r
-{\r
-  UINT8       SpiStatus;\r
-  UINT8       FlashIndex;\r
-  UINT8       FlashID[3];\r
-  EFI_STATUS  Status;\r
-\r
-  //\r
-  // Obtain a handle for ICH SPI Protocol\r
-  //\r
-  ASSERT(mSmst != NULL);\r
-  if (mFvbModuleGlobal->SmmSpiProtocol == NULL){\r
-    Status = mSmst->SmmLocateProtocol (&gEfiSmmSpiProtocolGuid, NULL, (VOID **) &mFvbModuleGlobal->SmmSpiProtocol);\r
-    ASSERT_EFI_ERROR(Status);\r
-  }\r
-  //\r
-  // attempt to identify flash part and initialize spi table\r
-  //\r
-  for (FlashIndex = 0; FlashIndex < EnumSpiFlashMax; FlashIndex++) {\r
-    Status = mFvbModuleGlobal->SmmSpiProtocol->Init (\r
-                                                mFvbModuleGlobal->SmmSpiProtocol,\r
-                                                &(mSpiInitTable[FlashIndex])\r
-                                                );\r
-    if (!EFI_ERROR (Status)) {\r
-      //\r
-      // read vendor/device IDs to check if flash device is supported\r
-      //\r
-      Status = mFvbModuleGlobal->SmmSpiProtocol->Execute (\r
-                                                  mFvbModuleGlobal->SmmSpiProtocol,\r
-                                                  SPI_OPCODE_JEDEC_ID_INDEX,\r
-                                                  SPI_WREN_INDEX,\r
-                                                  TRUE,\r
-                                                  FALSE,\r
-                                                  FALSE,\r
-                                                  0,\r
-                                                  3,\r
-                                                  FlashID,\r
-                                                  EnumSpiRegionAll\r
-                                                  );\r
-      if (!EFI_ERROR (Status)) {\r
-        if (((FlashID[0] == mSpiInitTable[FlashIndex].VendorId) &&\r
-               (FlashID[2] == mSpiInitTable[FlashIndex].DeviceId1)) ||\r
-              ((FlashID[0] == SPI_AT26DF321_ID1) &&\r
-               (FlashID[0] == mSpiInitTable[FlashIndex].VendorId) &&\r
-               (FlashID[1] == mSpiInitTable[FlashIndex].DeviceId0))) {\r
-          //\r
-          // Supported SPI device found\r
-          //\r
-          DEBUG (\r
-              ((EFI_D_INFO),\r
-              "Smm Mode: Supported SPI Flash device found, Vendor Id: 0x%02x, Device ID: 0x%02x%02x!\n",\r
-              FlashID[0],\r
-              FlashID[1],\r
-              FlashID[2])\r
-              );\r
-          break;\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  if (FlashIndex >= EnumSpiFlashMax) {\r
-    Status = EFI_UNSUPPORTED;\r
-    DEBUG (\r
-        (EFI_D_ERROR,\r
-        "ERROR - Unknown SPI Flash Device, Vendor Id: 0x%02x, Device ID: 0x%02x%02x!\n",\r
-        FlashID[0],\r
-        FlashID[1],\r
-        FlashID[2])\r
-        );\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
-  SpiStatus = 0;\r
-  Status = mFvbModuleGlobal->SmmSpiProtocol->Execute (\r
-                                            mFvbModuleGlobal->SmmSpiProtocol,\r
-                                            SPI_OPCODE_WRITE_S_INDEX, // OpcodeIndex\r
-                                            1,                        // PrefixOpcodeIndex\r
-                                            TRUE,                     // DataCycle\r
-                                            TRUE,                     // Atomic\r
-                                            TRUE,                     // ShiftOut\r
-                                            0,                        // Address\r
-                                            1,                        // Data Number\r
-                                            &SpiStatus,\r
-                                            EnumSpiRegionAll          // SPI_REGION_TYPE\r
-                                            );\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-SmmSpiNotificationFunction (\r
-  IN CONST EFI_GUID  *Protocol,\r
-  IN VOID            *Interface,\r
-  IN EFI_HANDLE      Handle\r
-  )\r
-{\r
-  return SmmSpiInit();\r
-}\r
-\r
-\r
-VOID\r
-EFIAPI\r
-GetFullDriverPath (\r
-  IN  EFI_HANDLE                  ImageHandle,\r
-  IN  EFI_SYSTEM_TABLE            *SystemTable,\r
-  OUT EFI_DEVICE_PATH_PROTOCOL    **CompleteFilePath\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Function is used to get the full device path for this driver.\r
-\r
-Arguments:\r
-\r
-  ImageHandle        - The loaded image handle of this driver.\r
-  SystemTable        - The pointer of system table.\r
-  CompleteFilePath   - The pointer of returned full file path\r
-\r
-Returns:\r
-\r
-  none\r
-\r
---*/\r
-{\r
-  EFI_STATUS                Status;\r
-  EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
-  EFI_DEVICE_PATH_PROTOCOL  *ImageDevicePath;\r
-\r
-\r
-  Status = gBS->HandleProtocol (\r
-                  ImageHandle,\r
-                  &gEfiLoadedImageProtocolGuid,\r
-                  (VOID **) &LoadedImage\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->HandleProtocol (\r
-                  LoadedImage->DeviceHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID *) &ImageDevicePath\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  *CompleteFilePath = AppendDevicePath (\r
-                        ImageDevicePath,\r
-                        LoadedImage->FilePath\r
-                        );\r
-\r
-  return ;\r
-}\r
-\r
-\r
-\r
-EFI_STATUS\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_FIRMWARE_VOLUME_HEADER          *TempFwVolHeader;\r
-  VOID                                *HobList;\r
-  VOID                                *FirmwareVolumeHobList;\r
-  UINT32                              BufferSize;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *PtrBlockMapEntry;\r
-  BOOLEAN                             WriteEnabled;\r
-  BOOLEAN                             WriteLocked;\r
-  EFI_HANDLE                          FwbHandle;\r
-  EFI_FW_VOL_BLOCK_DEVICE             *FvbDevice;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *OldFwbInterface;\r
-  EFI_DEVICE_PATH_PROTOCOL            *FwbDevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL            *TempFwbDevicePath;\r
-  UINT32                              MaxLbaSize;\r
-  EFI_PHYSICAL_ADDRESS                BaseAddress;\r
-  BOOLEAN                             WriteBack;\r
-  UINTN                               NumOfBlocks;\r
-  UINTN                               HeaderLength;\r
-  UINT8                               SpiStatus;\r
-  UINT8                               FlashIndex;\r
-  UINT8                               FlashID[3];\r
-  EFI_DEVICE_PATH_PROTOCOL            *CompleteFilePath;\r
-  UINT8                               PrefixOpcodeIndex;\r
-  BOOLEAN                             InSmm;\r
-  EFI_SMM_BASE2_PROTOCOL              *mSmmBase2;\r
-  EFI_HANDLE                          Handle;\r
-\r
-  VOID                                *Registration;\r
-  EFI_EVENT                           Event;\r
-\r
-  CompleteFilePath = NULL;\r
-  GetFullDriverPath (ImageHandle, SystemTable, &CompleteFilePath);\r
-\r
- Status = EfiGetSystemConfigurationTable (&gEfiHobListGuid, &HobList);\r
-\r
-  //\r
-  // No FV HOBs found\r
-  //\r
-  ASSERT_EFI_ERROR (Status);\r
-\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 = (ESAL_FWB_GLOBAL *)AllocateRuntimeZeroPool(sizeof (ESAL_FWB_GLOBAL  ));\r
-  ASSERT(mFvbModuleGlobal);\r
-  mSmmBase2 = NULL;\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiSmmBase2ProtocolGuid,\r
-                  NULL,\r
-                  (VOID **) &mSmmBase2\r
-                  );\r
-\r
-  if (mSmmBase2 == NULL) {\r
-    InSmm = FALSE;\r
-  } else {\r
-    mSmmBase2->InSmm (mSmmBase2, &InSmm);\r
-    mSmmBase2->GetSmstLocation (mSmmBase2, &mSmst);\r
-\r
-  }\r
-\r
-  if (!InSmm) {\r
-    mInSmmMode = 0;\r
-    //\r
-    // Obtain a handle for ICH SPI Protocol\r
-    //\r
-    Status = gBS->LocateProtocol (&gEfiSpiProtocolGuid, NULL, (VOID **) &mFvbModuleGlobal->SpiProtocol);\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    //\r
-    // attempt to identify flash part and initialize spi table\r
-    //\r
-    for (FlashIndex = 0; FlashIndex < EnumSpiFlashMax; FlashIndex++) {\r
-      Status = mFvbModuleGlobal->SpiProtocol->Init (\r
-                                                mFvbModuleGlobal->SpiProtocol,\r
-                                                &(mSpiInitTable[FlashIndex])\r
-                                                );\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // read vendor/device IDs to check if flash device is supported\r
-        //\r
-        Status = mFvbModuleGlobal->SpiProtocol->Execute (\r
-                                                  mFvbModuleGlobal->SpiProtocol,\r
-                                                  SPI_OPCODE_JEDEC_ID_INDEX,\r
-                                                  SPI_WREN_INDEX,\r
-                                                  TRUE,\r
-                                                  FALSE,\r
-                                                  FALSE,\r
-                                                  0,\r
-                                                  3,\r
-                                                  FlashID,\r
-                                                  EnumSpiRegionAll\r
-                                                  );\r
-        if (!EFI_ERROR (Status)) {\r
-          if (((FlashID[0] == mSpiInitTable[FlashIndex].VendorId) &&\r
-               (FlashID[2] == mSpiInitTable[FlashIndex].DeviceId1)) ||\r
-              ((FlashID[0] == SPI_AT26DF321_ID1) &&\r
-               (FlashID[0] == mSpiInitTable[FlashIndex].VendorId) &&\r
-               (FlashID[1] == mSpiInitTable[FlashIndex].DeviceId0))) {\r
-            //\r
-            // Supported SPI device found\r
-            //\r
-            DEBUG (\r
-              ((EFI_D_INFO),\r
-              "Supported SPI Flash device found, Vendor Id: 0x%02x, Device ID: 0x%02x%02x!\n",\r
-              FlashID[0],\r
-              FlashID[1],\r
-              FlashID[2])\r
-              );\r
-\r
-            PublishFlashDeviceInfo (&mSpiInitTable[FlashIndex]);\r
-            break;\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    if (FlashIndex >= EnumSpiFlashMax) {\r
-      Status = EFI_UNSUPPORTED;\r
-      DEBUG (\r
-        (DEBUG_ERROR,\r
-        "ERROR - Unknown SPI Flash Device, Vendor Id: 0x%02x, Device ID: 0x%02x%02x!\n",\r
-        FlashID[0],\r
-        FlashID[1],\r
-        FlashID[2])\r
-        );\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\r
-\r
-    //\r
-    // Unlock all regions by writing to status register\r
-    // This could be SPI device specific, need to follow the datasheet\r
-    // To write to Write Status Register the Spi PrefixOpcode needs to be:\r
-    //   0 for Atmel parts\r
-    //   0 for Intel parts\r
-    //   0 for Macronix parts\r
-    //   0 for Winbond parts\r
-    //   1 for SST parts\r
-    SpiStatus = 0;\r
-    if (FlashID[0] == SPI_SST25VF016B_ID1) {\r
-      PrefixOpcodeIndex = 1;\r
-    } else {\r
-      PrefixOpcodeIndex = 0;\r
-    }\r
-    Status = mFvbModuleGlobal->SpiProtocol->Execute (\r
-                                              mFvbModuleGlobal->SpiProtocol,\r
-                                              SPI_OPCODE_WRITE_S_INDEX, // OpcodeIndex\r
-                                              PrefixOpcodeIndex,        // PrefixOpcodeIndex\r
-                                              TRUE,                     // DataCycle\r
-                                              TRUE,                     // Atomic\r
-                                              TRUE,                     // ShiftOut\r
-                                              0,                        // Address\r
-                                              1,                        // Data Number\r
-                                              &SpiStatus,\r
-                                              EnumSpiRegionAll          // SPI_REGION_TYPE\r
-                                              );\r
-\r
-\r
-  } else  {\r
-    mInSmmMode = 1;\r
-\r
-    Status = mSmst->SmmLocateProtocol (&gEfiSmmSpiProtocolGuid, NULL, (VOID **) &mFvbModuleGlobal->SmmSpiProtocol);\r
-    if (EFI_ERROR(Status)) {\r
-      Registration = NULL;\r
-      Status = mSmst->SmmRegisterProtocolNotify (\r
-                   &gEfiSmmSpiProtocolGuid,\r
-                   SmmSpiNotificationFunction,\r
-                   &Registration\r
-                   );\r
-    } else  {\r
-      Status  = SmmSpiInit();\r
-    }\r
-\r
-  }\r
-\r
-  //\r
-  // Calculate the total size for all firmware volume block instances\r
-  //\r
-  BufferSize            = 0;\r
-  FirmwareVolumeHobList = HobList;\r
-  FwVolHeader           = NULL;\r
-  do {\r
-    Status = GetFvbHeader (&FirmwareVolumeHobList, &FwVolHeader, &BaseAddress, &WriteBack);\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
-\r
-    if (FwVolHeader) {\r
-      BufferSize += (FwVolHeader->HeaderLength + sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
-    }\r
-  } while (TRUE);\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] = (EFI_FW_VOL_INSTANCE *) AllocateRuntimeZeroPool (BufferSize);\r
-  ASSERT(mFvbModuleGlobal->FvInstance[FVB_PHYSICAL]);\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
-  FirmwareVolumeHobList                     = HobList;\r
-  TempFwVolHeader                           = NULL;\r
-\r
-  MaxLbaSize = 0;\r
-\r
-  //\r
-  // Fill in the private data of each firmware volume block instance\r
-  //\r
-  // Foreach Fv HOB in the FirmwareVolumeHobList, loop\r
-  //\r
-  do {\r
-    Status = GetFvbHeader (&FirmwareVolumeHobList, &TempFwVolHeader, &BaseAddress, &WriteBack);\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
-    FwVolHeader = TempFwVolHeader;\r
-\r
-    if (!FwVolHeader) {\r
-      continue;\r
-    }\r
-\r
-\r
-    CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);\r
-    FwVolHeader                       = &(FwhInstance->VolumeHeader);\r
-\r
-    FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;\r
-    FwhInstance->FvBase[FVB_VIRTUAL]  = (UINTN) BaseAddress;\r
-\r
-    //\r
-    // FwhInstance->FvWriteBase may not be the same as FwhInstance->FvBase\r
-    //\r
-    FwhInstance->FvWriteBase[FVB_PHYSICAL]  = (UINTN) BaseAddress;\r
-    WriteEnabled = TRUE;\r
-\r
-    //\r
-    // Every pointer should have a virtual copy.\r
-    //\r
-    FwhInstance->FvWriteBase[FVB_VIRTUAL] = FwhInstance->FvWriteBase[FVB_PHYSICAL];\r
-\r
-    FwhInstance->WriteEnabled             = WriteEnabled;\r
-    EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r
-\r
-    NumOfBlocks = 0;\r
-    WriteLocked = FALSE;\r
-\r
-    if (WriteEnabled) {\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
-      //  Write back a healthy FV header\r
-      //\r
-      if (WriteBack && (!WriteLocked)) {\r
-\r
-        Status = FlashFdErase (\r
-                  (UINTN) FwhInstance->FvWriteBase[0],\r
-                  (UINTN) BaseAddress,\r
-                  FwVolHeader->BlockMap->Length\r
-                  );\r
-\r
-        HeaderLength = (UINTN) FwVolHeader->HeaderLength;\r
-        Status = FlashFdWrite (\r
-                  (UINTN) FwhInstance->FvWriteBase[0],\r
-                  (UINTN) BaseAddress,\r
-                  &HeaderLength,\r
-                  (UINT8 *) FwVolHeader,\r
-                  FwVolHeader->BlockMap->Length\r
-                  );\r
-\r
-      }\r
-    }\r
-    //\r
-    // The total number of blocks in the FV.\r
-    //\r
-    FwhInstance->NumOfBlocks = NumOfBlocks;\r
-\r
-    //\r
-    // If the FV is write locked, set the appropriate attributes\r
-    //\r
-    if (WriteLocked) {\r
-      //\r
-      // write disabled\r
-      //\r
-      FwhInstance->VolumeHeader.Attributes &= ~EFI_FVB2_WRITE_STATUS;\r
-      //\r
-      // lock enabled\r
-      //\r
-      FwhInstance->VolumeHeader.Attributes |= EFI_FVB2_LOCK_STATUS;\r
-    }\r
-\r
-    //\r
-    // Allocate and initialize FVB Device in a runtime data buffer\r
-    //\r
-    FvbDevice = AllocateRuntimeCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFvbDeviceTemplate);\r
-    ASSERT (FvbDevice);\r
-\r
-    FvbDevice->Instance = mFvbModuleGlobal->NumFv;\r
-    mFvbModuleGlobal->NumFv++;\r
-\r
-    //\r
-    // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH\r
-    //\r
-    if (FwVolHeader->ExtHeaderOffset == 0) {\r
-      FvbDevice->FvDevicePath.MemMapDevPath.StartingAddress = BaseAddress;\r
-      FvbDevice->FvDevicePath.MemMapDevPath.EndingAddress   = BaseAddress + (FwVolHeader->FvLength - 1);\r
-      FwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)&FvbDevice->FvDevicePath;\r
-    } else {\r
-      CopyGuid (\r
-        &FvbDevice->UefiFvDevicePath.FvDevPath.FvName,\r
-        (EFI_GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)\r
-        );\r
-      FwbDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)&FvbDevice->UefiFvDevicePath;\r
-    }\r
-\r
-    if (!InSmm) {\r
-      //\r
-      // Find a handle with a matching device path that has supports FW Block protocol\r
-      //\r
-      TempFwbDevicePath = FwbDevicePath;\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
-                        FwbDevicePath,\r
-                        NULL\r
-                        );\r
-        ASSERT_EFI_ERROR (Status);\r
-      } else if (EfiIsDevicePathEnd (TempFwbDevicePath)) {\r
-        //\r
-        // Device already exists, so reinstall the FVB protocol\r
-        //\r
-        Status = gBS->HandleProtocol (\r
-                        FwbHandle,\r
-                        &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                        (VOID **) &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
-    } else {\r
-      FwbHandle = NULL;\r
-      Status = mSmst->SmmInstallProtocolInterface (\r
-                &FwbHandle,\r
-                &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
-                EFI_NATIVE_INTERFACE,\r
-                &FvbDevice->FwVolBlockInstance\r
-                );\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\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
-  } while (TRUE);\r
-\r
-  //\r
-  // Allocate for scratch space, an intermediate buffer for FVB extention\r
-  //\r
-\r
-  mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL] = AllocateRuntimeZeroPool (MaxLbaSize);\r
-\r
-  ASSERT (mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL]);\r
-\r
-  mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL] = mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL];\r
-\r
-  if (!InSmm) {\r
-    Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                 FvbVirtualddressChangeEvent,\r
-                  NULL,\r
-                  &gEfiEventVirtualAddressChangeGuid,\r
-                  &Event\r
-                  );\r
-    ASSERT_EFI_ERROR (Status);\r
-  } else {\r
-    //\r
-    // Inform other platform drivers that SPI device discovered and\r
-    // SPI interface ready for use.\r
-    //\r
-    Handle = NULL;\r
-    Status = gBS->InstallProtocolInterface (\r
-                    &Handle,\r
-                    &gEfiSmmSpiReadyProtocolGuid,\r
-                    EFI_NATIVE_INTERFACE,\r
-                    NULL\r
-                    );\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r