]> git.proxmox.com Git - mirror_edk2.git/blobdiff - DuetPkg/FvbRuntimeService/FWBlockService.c
DuetPkg: Remove DuetPkg
[mirror_edk2.git] / DuetPkg / FvbRuntimeService / FWBlockService.c
diff --git a/DuetPkg/FvbRuntimeService/FWBlockService.c b/DuetPkg/FvbRuntimeService/FWBlockService.c
deleted file mode 100644 (file)
index bc66327..0000000
+++ /dev/null
@@ -1,1659 +0,0 @@
-/**@file\r
-Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  FWBlockService.c\r
-    \r
-Abstract:\r
-\r
-Revision History\r
-\r
-**/\r
-#include "FWBlockService.h"\r
-#include "EfiFlashMap.h"\r
-#include "FileIo.h"\r
-#include "FlashLayout.h"\r
-\r
-ESAL_FWB_GLOBAL       *mFvbModuleGlobal;\r
-VOID                  *mSFSRegistration;\r
-#define TRY_ASSIGN(var, value)  if(var != NULL) {*var = value;}\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
-\r
-\r
-EFI_STATUS\r
-FlashFdWrite (\r
-  IN     UINTN                            Address,\r
-  IN     EFI_FW_VOL_INSTANCE              *FwhInstance,\r
-  IN OUT UINTN                            *NumBytes,\r
-  IN     UINT8                            *Buffer\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
-  EFI_FILE_PROTOCOL   *File;\r
-  UINTN               FileOffset;\r
-  UINTN               BufferForFile;\r
-  UINTN               Length;\r
-\r
-  Status = EFI_SUCCESS;\r
-  CopyMem ((VOID *) Address, Buffer, *NumBytes);\r
-\r
-  if (!EfiAtRuntime () && (FwhInstance->Device != NULL)) {\r
-    Status = FileOpen (FwhInstance->Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE);\r
-    ASSERT_EFI_ERROR (Status);\r
-    if (!EFI_ERROR (Status)) {\r
-      if (Address - FwhInstance->FvBase[FVB_PHYSICAL] < FwhInstance->Offset) {\r
-        FileOffset = 0;\r
-        BufferForFile = FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset;\r
-        Length = *NumBytes - (FwhInstance->Offset - (Address - FwhInstance->FvBase[FVB_PHYSICAL]));\r
-      } else {\r
-        FileOffset = Address - FwhInstance->FvBase[FVB_PHYSICAL] - FwhInstance->Offset;\r
-        BufferForFile = Address;\r
-        Length = *NumBytes;\r
-      }\r
-      \r
-      Status = FileWrite (File, FileOffset, BufferForFile, Length);\r
-      ASSERT_EFI_ERROR (Status);\r
-      FileClose (File);\r
-    }\r
-  }\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FlashFdErase (\r
-  IN UINTN                                Address,\r
-  IN EFI_FW_VOL_INSTANCE                  *FwhInstance,\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
-  EFI_FILE_PROTOCOL    *File;\r
-  UINTN                FileOffset;\r
-  UINTN                BufferForFile;\r
-  UINTN                Length;\r
-\r
-  Status = EFI_SUCCESS;\r
-\r
-  SetMem ((VOID *)Address, LbaLength, 0xff);\r
-\r
-  if (!EfiAtRuntime () && (FwhInstance->Device != NULL)) {\r
-    Status = FileOpen (FwhInstance->Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE);\r
-    ASSERT_EFI_ERROR (Status);\r
-    if (!EFI_ERROR (Status)) {\r
-      if (Address - FwhInstance->FvBase[FVB_PHYSICAL] < FwhInstance->Offset) {\r
-        FileOffset = 0;\r
-        BufferForFile = FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset;\r
-        Length = LbaLength - (FwhInstance->Offset - (Address - FwhInstance->FvBase[FVB_PHYSICAL]));\r
-      } else {\r
-        FileOffset = Address - FwhInstance->FvBase[FVB_PHYSICAL] - FwhInstance->Offset;\r
-        BufferForFile = Address;\r
-        Length = LbaLength;\r
-      }\r
-      \r
-      Status = FileWrite (File, FileOffset, BufferForFile, Length);\r
-      ASSERT_EFI_ERROR (Status);\r
-      FileClose (File);\r
-    }\r
-  }\r
-  return Status;\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 (0, (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 (0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r
-    FwhInstance = (EFI_FW_VOL_INSTANCE *) ((UINTN)((UINT8 *)FwhInstance) + FwhInstance->VolumeHeader.HeaderLength\r
-                    + (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));\r
-    Index++;\r
-  }\r
-\r
-  EfiConvertPointer (0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r
-  EfiConvertPointer (0, (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 *) ((UINTN)((UINT8 *)FwhRecord) + FwhRecord->VolumeHeader.HeaderLength \r
-                     + (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));\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_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
-  //\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  OPTIONAL,\r
-  OUT UINTN                               *LbaLength   OPTIONAL,\r
-  OUT UINTN                               *NumOfBlocks OPTIONAL,\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 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
-  //\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
-\r
-      if (LbaAddress) {\r
-        *LbaAddress = FwhInstance->FvBase[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, &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, (VOID *) (LbaAddress + BlockOffset), (UINTN) *NumBytes);\r
-\r
-  return Status;\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                 LbaLength;\r
-  EFI_FW_VOL_INSTANCE   *FwhInstance;\r
-  EFI_STATUS            Status;\r
-  EFI_STATUS            ReturnStatus;\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, &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
-                  LbaAddress + BlockOffset,\r
-                  FwhInstance,\r
-                  NumBytes,\r
-                  Buffer\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
-  EFI_FW_VOL_INSTANCE   *FwhInstance;\r
-  UINTN                 LbaLength;\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
-  //\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, &LbaLength, NULL, Global, Virtual);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  return FlashFdErase (\r
-          LbaAddress,\r
-          FwhInstance,\r
-          LbaLength\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
-  //\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
-EFIAPI\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
-          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
-  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) || ((StartingLba + NumOfLba) > NumOfBlocks)) {\r
-      VA_END (args);\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, 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 (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 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
-\r
-  FvbDevice = FVB_DEVICE_FROM_THIS (This);\r
-\r
-  return FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer, mFvbModuleGlobal, EfiGoneVirtual ());\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
-  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
-\r
-EFI_STATUS\r
-GetFvbHeader (\r
-  IN OUT EFI_PEI_HOB_POINTERS           *HobList,\r
-  OUT    EFI_FIRMWARE_VOLUME_HEADER     **FwVolHeader,\r
-  OUT    EFI_PHYSICAL_ADDRESS           *BaseAddress     OPTIONAL,\r
-  OUT    UINT32                         *VolumeId        OPTIONAL,\r
-  OUT    CHAR16                         **MappedFile     OPTIONAL,\r
-  OUT    UINT32                         *ActuralSize     OPTIONAL,\r
-  OUT    UINT32                         *Offset          OPTIONAL,\r
-  OUT    BOOLEAN                        *WriteBack       OPTIONAL\r
-  )\r
-{\r
-  EFI_STATUS                  Status;\r
-  EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntry;\r
-  EFI_FLASH_SUBAREA_ENTRY     *FlashMapSubEntry;\r
-\r
-  Status        = EFI_SUCCESS;\r
-  *FwVolHeader  = NULL;\r
-  TRY_ASSIGN (WriteBack, FALSE);\r
-\r
-  DEBUG ((EFI_D_INFO, "Hob start is 0x%x\n", (UINTN)(*HobList).Raw));\r
-  (*HobList).Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, (*HobList).Raw);\r
-  if ((*HobList).Raw == NULL) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  FlashMapEntry     = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA ((*HobList).Guid);\r
-  FlashMapSubEntry  = &FlashMapEntry->Entries[0];\r
-  \r
-  //\r
-  // Check if it is a "FVB" area\r
-  //\r
-  if (!CompareGuid (&FlashMapSubEntry->FileSystem, &gEfiFirmwareVolumeBlockProtocolGuid)) {\r
-    return Status;\r
-  }\r
-  //\r
-  // Check if it is a "real" flash\r
-  //\r
-  if (FlashMapSubEntry->Attributes != (EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV)) {\r
-    return Status;\r
-  }\r
-\r
-  TRY_ASSIGN (BaseAddress, FlashMapSubEntry->Base);\r
-\r
-  //\r
-  // Cast buffer to FLASH_AREA_INFO to get extra information related to the special FVB driver\r
-  //\r
-  TRY_ASSIGN (VolumeId,    FlashMapEntry->VolumeId);\r
-  TRY_ASSIGN (ActuralSize, FlashMapEntry->ActuralSize);\r
-  TRY_ASSIGN (MappedFile, ((CHAR16 *) FlashMapEntry->FilePath));\r
-  TRY_ASSIGN (Offset,      FlashMapEntry->Offset);\r
-\r
-  DEBUG ((\r
-    EFI_D_INFO, \r
-    "FlashMap HOB: BaseAddress = 0x%x, Length = 0x%x, ActuralLength = 0x%x, Offset = 0x%x\n", \r
-    (UINTN) FlashMapSubEntry->Base, (UINTN) FlashMapSubEntry->Length, \r
-    (UINTN) FlashMapEntry->ActuralSize, (UINTN) FlashMapEntry->Offset\r
-  ));\r
-  DEBUG ((\r
-    EFI_D_INFO,\r
-    "FlashMap HOB: VolumeId = 0x%lx, MappedFile = %s\n",\r
-    (UINTN) FlashMapEntry->VolumeId, (UINTN) FlashMapEntry->FilePath\r
-  ));\r
-  *FwVolHeader  = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (FlashMapSubEntry->Base);\r
-  Status        = ValidateFvHeader (*FwVolHeader);\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // Get FvbInfo\r
-    //\r
-    TRY_ASSIGN (WriteBack, TRUE);\r
-    Status = GetFvbInfo (FlashMapSubEntry->Length, FwVolHeader);\r
-    DEBUG ((EFI_D_ERROR, "Fvb: FV header invalid, GetFvbInfo - %r\n", Status));\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-VOID\r
-EFIAPI\r
-OnSimpleFileSystemInstall (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  UINTN                     HandleSize;\r
-  EFI_HANDLE                Handle;\r
-  UINTN                     Instance;\r
-  EFI_DEVICE_PATH_PROTOCOL  *Device;\r
-  EFI_FILE_PROTOCOL         *File;\r
-  EFI_FW_VOL_INSTANCE       *FwhInstance;\r
-  while (TRUE) {\r
-    HandleSize = sizeof (EFI_HANDLE);\r
-    Status = gBS->LocateHandle (\r
-                    ByRegisterNotify,\r
-                    NULL,\r
-                    mSFSRegistration,\r
-                    &HandleSize,\r
-                    &Handle\r
-                    );\r
-    if (Status == EFI_NOT_FOUND) {\r
-      break;\r
-    }\r
-    DEBUG ((EFI_D_ERROR, "Fwh: New FileSystem Installed!\n"));\r
-    ASSERT_EFI_ERROR (Status);\r
-    //\r
-    // Check if this is the storage we care about, and store it in FwhInstance\r
-    //\r
-    for (Instance = 0; Instance < mFvbModuleGlobal->NumFv; ++Instance) {\r
-      Status = GetFvbInstance (Instance, mFvbModuleGlobal, &FwhInstance, FALSE);\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      if (FwhInstance->MappedFile[0] == L'\0') {\r
-        //\r
-        // The instance of FVB isn't mapped to file.\r
-        //\r
-        continue;\r
-      }\r
-\r
-      if ((FwhInstance->Device != NULL) && \r
-          !EFI_ERROR (CheckStoreExists (FwhInstance->Device))\r
-          ) {\r
-        //\r
-        // The instance of FVB has already associated to a device\r
-        //  and the device is not removed from system.\r
-        //\r
-        DEBUG ((\r
-              EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Already mapped, Skip!\n", \r
-              (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r
-              (UINTN) FwhInstance->Offset\r
-              ));\r
-        continue;\r
-      }\r
-\r
-      Status = CheckStore (Handle, FwhInstance->VolumeId, &Device);\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // Write back memory content to file\r
-        //\r
-        Status = FileOpen (Device, FwhInstance->MappedFile, &File, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE);\r
-        ASSERT_EFI_ERROR (Status); \r
-        if (!EFI_ERROR (Status)) {\r
-          DEBUG ((\r
-                EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Write back to mapped file!\n", \r
-                (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r
-                (UINTN) FwhInstance->Offset\r
-                ));\r
-          Status = FileWrite (\r
-                     File, \r
-                     0, \r
-                     FwhInstance->FvBase[FVB_PHYSICAL] + FwhInstance->Offset, \r
-                     FwhInstance->ActuralSize - FwhInstance->Offset\r
-                     );\r
-          ASSERT_EFI_ERROR (Status); \r
-          if (!EFI_ERROR (Status)) {\r
-            if (FwhInstance->Device != NULL) {\r
-              gBS->FreePool (FwhInstance->Device);\r
-            }\r
-            FwhInstance->Device = Device;\r
-            DEBUG ((\r
-                  EFI_D_ERROR, "Fwh: MappedFile FVB (0x%x:0x%x) - Mapped!\n",\r
-                  (UINTN) FwhInstance->FvBase[FVB_PHYSICAL],\r
-                  (UINTN) FwhInstance->Offset\r
-                  ));\r
-          }\r
-          FileClose (File);\r
-        }\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-VOID\r
-FvbInstallSfsNotify (\r
-  VOID\r
-)\r
-{\r
-  EFI_STATUS Status;\r
-  EFI_EVENT  Event;\r
-\r
-  Status = gBS->CreateEvent (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_CALLBACK,\r
-                  OnSimpleFileSystemInstall,\r
-                  NULL,\r
-                  &Event\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->RegisterProtocolNotify (\r
-                  &gEfiSimpleFileSystemProtocolGuid,\r
-                  Event,\r
-                  &mSFSRegistration\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-}\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_PEI_HOB_POINTERS                FirmwareVolumeHobList;\r
-  UINT32                              BufferSize;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *PtrBlockMapEntry;\r
-  UINTN                               LbaAddress;\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
-  UINT32                              VolumeId;\r
-  CHAR16                              *MappedFile;\r
-  UINT32                              ActuralSize;\r
-  UINT32                              Offset;\r
-  BOOLEAN                             WriteBack;\r
-  UINTN                               NumOfBlocks;\r
-  UINTN                               HeaderLength;\r
-  BOOLEAN                             InstallSfsNotify;\r
-\r
-  HeaderLength     = 0;\r
-  InstallSfsNotify = FALSE;\r
-\r
-  //\r
-  // Allocate runtime services data for global variable, which contains\r
-  // the private data of all firmware volume block instances\r
-  //\r
-  Status = gBS->AllocatePool (\r
-                  EfiRuntimeServicesData,\r
-                  sizeof (ESAL_FWB_GLOBAL),\r
-                  &mFvbModuleGlobal\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-  //\r
-  // Calculate the total size for all firmware volume block instances\r
-  //\r
-  BufferSize            = 0;\r
-  FirmwareVolumeHobList.Raw = GetHobList();\r
-  do {\r
-    Status = GetFvbHeader (&FirmwareVolumeHobList, &FwVolHeader, NULL, NULL, NULL, NULL, NULL, NULL);\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
-    FirmwareVolumeHobList.Raw = GET_NEXT_HOB (FirmwareVolumeHobList);\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
-  Status = gBS->AllocatePool (\r
-                  EfiRuntimeServicesData,\r
-                  BufferSize,\r
-                  &mFvbModuleGlobal->FvInstance[FVB_PHYSICAL]\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\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
-  FirmwareVolumeHobList.Raw   = GetHobList();\r
-  MaxLbaSize                  = 0;\r
-\r
-  //\r
-  // Fill in the private data of each firmware volume block instance\r
-  //\r
-  do {\r
-    Status = GetFvbHeader (\r
-               &FirmwareVolumeHobList, &FwVolHeader, \r
-               &BaseAddress, &VolumeId, &MappedFile, &ActuralSize, &Offset,\r
-               &WriteBack\r
-             );\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
-    FirmwareVolumeHobList.Raw = GET_NEXT_HOB (FirmwareVolumeHobList);\r
-\r
-    if (!FwVolHeader) {\r
-      continue;\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
-    FwhInstance->Device               = NULL;\r
-    FwhInstance->Offset               = Offset;\r
-\r
-    if (*MappedFile != '\0') {\r
-      FwhInstance->VolumeId             = VolumeId;\r
-      FwhInstance->ActuralSize          = ActuralSize;\r
-      StrCpy (FwhInstance->MappedFile, MappedFile);\r
-\r
-      InstallSfsNotify = TRUE;\r
-    } else {\r
-      FwhInstance->VolumeId             = (UINT32) -1;\r
-      FwhInstance->ActuralSize          = (UINT32) -1;\r
-      FwhInstance->MappedFile[0]        = L'\0';\r
-    }\r
-    \r
-    DEBUG ((EFI_D_INFO, "FirmVolume Found! BaseAddress=0x%lx, VolumeId=0x%x, MappedFile=%s, Size=0x%x\n",\r
-           (UINTN) BaseAddress, VolumeId, MappedFile, ActuralSize));\r
-    //\r
-    // We may expose readonly FVB in future.\r
-    //\r
-    FwhInstance->WriteEnabled         = TRUE; // Ken: Why enable write?\r
-    EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r
-\r
-    LbaAddress  = (UINTN) FwhInstance->FvBase[0];\r
-    NumOfBlocks = 0;\r
-\r
-    if (FwhInstance->WriteEnabled) {\r
-      for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
-\r
-        LbaAddress += PtrBlockMapEntry->NumBlocks * PtrBlockMapEntry->Length;\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 += PtrBlockMapEntry->NumBlocks;\r
-      }\r
-      //\r
-      //  Write back a healthy FV header\r
-      //\r
-      if (WriteBack) {\r
-        Status = FlashFdErase (\r
-                  (UINTN) FwhInstance->FvBase[0],\r
-                  FwhInstance,\r
-                  FwVolHeader->BlockMap->Length\r
-                  );\r
-\r
-        HeaderLength = (UINTN) FwVolHeader->HeaderLength;\r
-\r
-        Status = FlashFdWrite (\r
-                  (UINTN) FwhInstance->FvBase[0],\r
-                  FwhInstance,\r
-                  (UINTN *) &HeaderLength,\r
-                  (UINT8 *) FwVolHeader\r
-                  );\r
-\r
-        FwVolHeader->HeaderLength = (UINT16) HeaderLength;\r
-\r
-        DEBUG ((EFI_D_ERROR, "Fvb (0x%x): FV header invalid, write back - %r\n", (UINTN) FwhInstance->FvBase[0], Status));\r
-      }\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
-    Status = gBS->AllocatePool (\r
-                    EfiRuntimeServicesData,\r
-                    sizeof (EFI_FW_VOL_BLOCK_DEVICE),\r
-                    &FvbDevice\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\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 (IsDevicePathEnd (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
-    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
-  Status = gBS->AllocatePool (\r
-                  EfiRuntimeServicesData,\r
-                  MaxLbaSize,\r
-                  &mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL]\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL] = mFvbModuleGlobal->FvbScratchSpace[FVB_PHYSICAL];\r
-\r
-  if (InstallSfsNotify) {\r
-    FvbInstallSfsNotify ();\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r