]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FwBlockService.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / QemuFlashFvbServicesRuntimeDxe / FwBlockService.c
index b56ece3931ed6693b73c9bc17b95e72c94bee7fd..b34298faf82b72ae5470930b17f6e137a5627eed 100644 (file)
@@ -1,58 +1,45 @@
 /**@file\r
 \r
-Copyright (c) 2006 - 2014, 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
+  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
-Module Name:\r
+  Module Name:\r
 \r
-  FWBlockService.c\r
+    FWBlockService.c\r
 \r
-Abstract:\r
+  Abstract:\r
 \r
-Revision History\r
+  Revision History\r
 \r
 **/\r
 \r
 //\r
-// The package level header files this module uses\r
+// The protocols, PPI and GUID definitions for this module\r
 //\r
-#include <PiDxe.h>\r
-\r
-//\r
-// The protocols, PPI and GUID defintions for this module\r
-//\r
-#include <Guid/EventGroup.h>\r
-#include <Protocol/FirmwareVolumeBlock.h>\r
 #include <Protocol/DevicePath.h>\r
+#include <Protocol/FirmwareVolumeBlock.h>\r
 \r
 //\r
 // The Library classes this module consumes\r
 //\r
-#include <Library/UefiLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
 #include <Library/BaseLib.h>\r
-#include <Library/DxeServicesTableLib.h>\r
-#include <Library/UefiRuntimeLib.h>\r
-#include <Library/DebugLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/DevicePathLib.h>\r
 \r
 #include "FwBlockService.h"\r
 #include "QemuFlash.h"\r
 \r
-#define EFI_FVB2_STATUS (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)\r
+#define EFI_FVB2_STATUS \\r
+          (EFI_FVB2_READ_STATUS | EFI_FVB2_WRITE_STATUS | EFI_FVB2_LOCK_STATUS)\r
 \r
-ESAL_FWB_GLOBAL         *mFvbModuleGlobal;\r
+ESAL_FWB_GLOBAL  *mFvbModuleGlobal;\r
 \r
-FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {\r
+FV_MEMMAP_DEVICE_PATH  mFvMemmapDevicePathTemplate = {\r
   {\r
     {\r
       HARDWARE_DEVICE_PATH,\r
@@ -63,8 +50,8 @@ FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
       }\r
     },\r
     EfiMemoryMappedIO,\r
-    (EFI_PHYSICAL_ADDRESS) 0,\r
-    (EFI_PHYSICAL_ADDRESS) 0,\r
+    (EFI_PHYSICAL_ADDRESS)0,\r
+    (EFI_PHYSICAL_ADDRESS)0,\r
   },\r
   {\r
     END_DEVICE_PATH_TYPE,\r
@@ -76,7 +63,7 @@ FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
   }\r
 };\r
 \r
-FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {\r
+FV_PIWG_DEVICE_PATH  mFvPIWGDevicePathTemplate = {\r
   {\r
     {\r
       MEDIA_DEVICE_PATH,\r
@@ -98,7 +85,7 @@ FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {
   }\r
 };\r
 \r
-EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {\r
+EFI_FW_VOL_BLOCK_DEVICE  mFvbDeviceTemplate = {\r
   FVB_DEVICE_SIGNATURE,\r
   NULL,\r
   0,\r
@@ -114,103 +101,48 @@ EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
   }\r
 };\r
 \r
-\r
-\r
-VOID\r
-EFIAPI\r
-FvbVirtualddressChangeEvent (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Fixup internal data so that EFI and SAL can be call in virtual mode.\r
-  Call the passed in Child Notify event and convert the mFvbModuleGlobal\r
-  date items to there virtual address.\r
-\r
-  mFvbModuleGlobal->FvInstance[FVB_PHYSICAL]  - Physical copy of instance data\r
-  mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]   - Virtual pointer to common\r
-                                                instance data.\r
-\r
-Arguments:\r
-\r
-  (Standard EFI notify event - EFI_EVENT_NOTIFY)\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-{\r
-  EFI_FW_VOL_INSTANCE *FwhInstance;\r
-  UINTN               Index;\r
-\r
-  EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance[FVB_VIRTUAL]);\r
-\r
-  //\r
-  // Convert the base address of all the instances\r
-  //\r
-  Index       = 0;\r
-  FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
-  while (Index < mFvbModuleGlobal->NumFv) {\r
-    EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase[FVB_VIRTUAL]);\r
-    FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
-      (\r
-        (UINTN) ((UINT8 *) FwhInstance) + FwhInstance->VolumeHeader.HeaderLength +\r
-          (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
-      );\r
-    Index++;\r
-  }\r
-\r
-  EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvbScratchSpace[FVB_VIRTUAL]);\r
-  EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);\r
-  QemuFlashConvertPointers ();\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
+  IN  UINTN                Instance,\r
+  IN  ESAL_FWB_GLOBAL      *Global,\r
+  OUT EFI_FW_VOL_INSTANCE  **FwhInstance\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Retrieves the physical address of a memory mapped FV\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
+  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 firmware instance structure\r
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
-  EFI_INVALID_PARAMETER - Instance not found\r
+  Returns:\r
+    EFI_SUCCESS           - Successfully returns\r
+    EFI_INVALID_PARAMETER - Instance not found\r
 \r
 --*/\r
 {\r
-  EFI_FW_VOL_INSTANCE *FwhRecord;\r
+  EFI_FW_VOL_INSTANCE  *FwhRecord;\r
 \r
   *FwhInstance = NULL;\r
   if (Instance >= Global->NumFv) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
   //\r
   // Find the right instance of the FVB private data\r
   //\r
-  FwhRecord = Global->FvInstance[Virtual];\r
+  FwhRecord = Global->FvInstance;\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
+                (\r
+                 (UINTN)((UINT8 *)FwhRecord) + FwhRecord->VolumeHeader.HeaderLength +\r
+                 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
+                );\r
     Instance--;\r
   }\r
 \r
@@ -221,79 +153,77 @@ Returns:
 \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
+  IN UINTN                  Instance,\r
+  OUT EFI_PHYSICAL_ADDRESS  *Address,\r
+  IN ESAL_FWB_GLOBAL        *Global\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Retrieves the physical address of a memory mapped FV\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
+  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\r
+                            address of the firmware volume.\r
+    Global                - Pointer to ESAL_FWB_GLOBAL that contains all\r
+                            instance data\r
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
-  EFI_INVALID_PARAMETER - Instance not found\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
+  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
+  Status = GetFvbInstance (Instance, Global, &FwhInstance);\r
   ASSERT_EFI_ERROR (Status);\r
-  *Address = FwhInstance->FvBase[Virtual];\r
+  *Address = FwhInstance->FvBase;\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
+  IN UINTN                  Instance,\r
+  OUT EFI_FVB_ATTRIBUTES_2  *Attributes,\r
+  IN ESAL_FWB_GLOBAL        *Global\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Retrieves attributes, insures positive polarity of attribute bits, returns\r
-  resulting attributes in output parameter\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
+  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
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
-  EFI_INVALID_PARAMETER - Instance not found\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
+  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
+  Status = GetFvbInstance (Instance, Global, &FwhInstance);\r
   ASSERT_EFI_ERROR (Status);\r
   *Attributes = FwhInstance->VolumeHeader.Attributes;\r
 \r
@@ -302,36 +232,35 @@ Returns:
 \r
 EFI_STATUS\r
 FvbGetLbaAddress (\r
-  IN  UINTN                               Instance,\r
-  IN  EFI_LBA                             Lba,\r
-  OUT UINTN                               *LbaAddress,\r
-  OUT UINTN                               *LbaLength,\r
-  OUT UINTN                               *NumOfBlocks,\r
-  IN  ESAL_FWB_GLOBAL                     *Global,\r
-  IN  BOOLEAN                             Virtual\r
+  IN  UINTN            Instance,\r
+  IN  EFI_LBA          Lba,\r
+  OUT UINTN            *LbaAddress,\r
+  OUT UINTN            *LbaLength,\r
+  OUT UINTN            *NumOfBlocks,\r
+  IN  ESAL_FWB_GLOBAL  *Global\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Retrieves the starting address of an LBA in an FV\r
-\r
-Arguments:\r
-  Instance              - The FV instance which the Lba belongs to\r
-  Lba                   - The logical block address\r
-  LbaAddress            - On output, contains the physical starting address\r
-                          of the Lba\r
-  LbaLength             - On output, contains the length of the block\r
-  NumOfBlocks           - A pointer to a caller allocated UINTN in which the\r
-                          number of consecutive blocks starting with Lba is\r
-                          returned. All blocks in this range have a size of\r
-                          BlockSize\r
-  Global                - Pointer to ESAL_FWB_GLOBAL that contains all\r
-                          instance data\r
-  Virtual               - Whether CPU is in virtual or physical mode\r
-\r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
-  EFI_INVALID_PARAMETER - Instance not found\r
+  Routine Description:\r
+    Retrieves the starting address of an LBA in an FV\r
+\r
+  Arguments:\r
+    Instance              - The FV instance which the Lba belongs to\r
+    Lba                   - The logical block address\r
+    LbaAddress            - On output, contains the physical starting address\r
+                            of the Lba\r
+    LbaLength             - On output, contains the length of the block\r
+    NumOfBlocks           - A pointer to a caller allocated UINTN in which the\r
+                            number of consecutive blocks starting with Lba is\r
+                            returned. All blocks in this range have a size of\r
+                            BlockSize\r
+    Global                - Pointer to ESAL_FWB_GLOBAL that contains all\r
+                            instance data\r
+\r
+  Returns:\r
+    EFI_SUCCESS           - Successfully returns\r
+    EFI_INVALID_PARAMETER - Instance not found\r
 \r
 --*/\r
 {\r
@@ -347,12 +276,12 @@ Returns:
   //\r
   // Find the right instance of the FVB private data\r
   //\r
-  Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+  Status = GetFvbInstance (Instance, Global, &FwhInstance);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  StartLba  = 0;\r
-  Offset    = 0;\r
-  BlockMap  = &(FwhInstance->VolumeHeader.BlockMap[0]);\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
@@ -361,7 +290,7 @@ Returns:
     NumBlocks   = BlockMap->NumBlocks;\r
     BlockLength = BlockMap->Length;\r
 \r
-    if (NumBlocks == 0 || BlockLength == 0) {\r
+    if ((NumBlocks == 0) || (BlockLength == 0)) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
@@ -370,10 +299,10 @@ Returns:
     //\r
     // The map entry found\r
     //\r
-    if (Lba >= StartLba && Lba < NextLba) {\r
-      Offset = Offset + (UINTN) MultU64x32 ((Lba - StartLba), BlockLength);\r
+    if ((Lba >= StartLba) && (Lba < NextLba)) {\r
+      Offset = Offset + (UINTN)MultU64x32 ((Lba - StartLba), BlockLength);\r
       if (LbaAddress != NULL) {\r
-        *LbaAddress = FwhInstance->FvBase[Virtual] + Offset;\r
+        *LbaAddress = FwhInstance->FvBase + Offset;\r
       }\r
 \r
       if (LbaLength != NULL) {\r
@@ -381,48 +310,47 @@ Returns:
       }\r
 \r
       if (NumOfBlocks != NULL) {\r
-        *NumOfBlocks = (UINTN) (NextLba - Lba);\r
+        *NumOfBlocks = (UINTN)(NextLba - Lba);\r
       }\r
 \r
       return EFI_SUCCESS;\r
     }\r
 \r
-    StartLba  = NextLba;\r
-    Offset    = Offset + NumBlocks * BlockLength;\r
+    StartLba = NextLba;\r
+    Offset   = Offset + NumBlocks * BlockLength;\r
     BlockMap++;\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
+  IN UINTN                     Instance,\r
+  IN OUT EFI_FVB_ATTRIBUTES_2  *Attributes,\r
+  IN ESAL_FWB_GLOBAL           *Global\r
   )\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
+  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
+\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\r
+                            the firmware volume header\r
 \r
 --*/\r
 {\r
@@ -438,10 +366,11 @@ Returns:
   //\r
   // Find the right instance of the FVB private data\r
   //\r
-  Status = GetFvbInstance (Instance, Global, &FwhInstance, Virtual);\r
+  Status = GetFvbInstance (Instance, Global, &FwhInstance);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  AttribPtr     = (EFI_FVB_ATTRIBUTES_2 *) &(FwhInstance->VolumeHeader.Attributes);\r
+  AttribPtr =\r
+    (EFI_FVB_ATTRIBUTES_2 *)&(FwhInstance->VolumeHeader.Attributes);\r
   OldAttributes = *AttribPtr;\r
   Capabilities  = OldAttributes & (EFI_FVB2_READ_DISABLED_CAP | \\r
                                    EFI_FVB2_READ_ENABLED_CAP | \\r
@@ -449,8 +378,8 @@ Returns:
                                    EFI_FVB2_WRITE_ENABLED_CAP | \\r
                                    EFI_FVB2_LOCK_CAP \\r
                                    );\r
-  OldStatus     = OldAttributes & EFI_FVB2_STATUS;\r
-  NewStatus     = *Attributes & EFI_FVB2_STATUS;\r
+  OldStatus = OldAttributes & EFI_FVB2_STATUS;\r
+  NewStatus = *Attributes & EFI_FVB2_STATUS;\r
 \r
   UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP  | \\r
                         EFI_FVB2_READ_ENABLED_CAP   | \\r
@@ -467,9 +396,12 @@ Returns:
   //\r
   // Some attributes of FV is read only can *not* be set\r
   //\r
-  if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {\r
+  if ((OldAttributes & UnchangedAttributes) ^\r
+      (*Attributes & UnchangedAttributes))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
   //\r
   // If firmware volume is locked, no status bit can be updated\r
   //\r
@@ -478,6 +410,7 @@ Returns:
       return EFI_ACCESS_DENIED;\r
     }\r
   }\r
+\r
   //\r
   // Test read disable\r
   //\r
@@ -486,6 +419,7 @@ Returns:
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
+\r
   //\r
   // Test read enable\r
   //\r
@@ -494,6 +428,7 @@ Returns:
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
+\r
   //\r
   // Test write disable\r
   //\r
@@ -502,6 +437,7 @@ Returns:
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
+\r
   //\r
   // Test write enable\r
   //\r
@@ -510,6 +446,7 @@ Returns:
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
+\r
   //\r
   // Test lock\r
   //\r
@@ -532,174 +469,193 @@ Returns:
 EFI_STATUS\r
 EFIAPI\r
 FvbProtocolGetPhysicalAddress (\r
-  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL           *This,\r
-  OUT EFI_PHYSICAL_ADDRESS                        *Address\r
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
+  OUT EFI_PHYSICAL_ADDRESS                     *Address\r
   )\r
-/*++\r
 \r
-Routine Description:\r
+/*++\r
 \r
-  Retrieves the physical address of the device.\r
+  Routine Description:\r
 \r
-Arguments:\r
+    Retrieves the physical address of the device.\r
 \r
-  This                  - Calling context\r
-  Address               - Output buffer containing the address.\r
+  Arguments:\r
 \r
-Returns:\r
+    This                  - Calling context\r
+    Address               - Output buffer containing the address.\r
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
+  Returns:\r
+    EFI_SUCCESS           - Successfully returns\r
 \r
 --*/\r
 {\r
-  EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\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
+  return FvbGetPhysicalAddress (\r
+           FvbDevice->Instance,\r
+           Address,\r
+           mFvbModuleGlobal\r
+           );\r
 }\r
 \r
 EFI_STATUS\r
 EFIAPI\r
 FvbProtocolGetBlockSize (\r
-  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL           *This,\r
-  IN CONST EFI_LBA                                     Lba,\r
-  OUT UINTN                                       *BlockSize,\r
-  OUT UINTN                                       *NumOfBlocks\r
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
+  IN CONST EFI_LBA                             Lba,\r
+  OUT UINTN                                    *BlockSize,\r
+  OUT UINTN                                    *NumOfBlocks\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Retrieve the size of a logical block\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
+  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
+  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
+  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
+           FvbDevice->Instance,\r
+           Lba,\r
+           NULL,\r
+           BlockSize,\r
+           NumOfBlocks,\r
+           mFvbModuleGlobal\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
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
+  OUT EFI_FVB_ATTRIBUTES_2                     *Attributes\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-    Retrieves Volume attributes.  No polarity translations are done.\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
+  Arguments:\r
+      This                - Calling context\r
+      Attributes          - output buffer which contains attributes\r
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
+  Returns:\r
+    EFI_SUCCESS           - Successfully returns\r
 \r
 --*/\r
 {\r
-  EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\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
+  return FvbGetVolumeAttributes (\r
+           FvbDevice->Instance,\r
+           Attributes,\r
+           mFvbModuleGlobal\r
+           );\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
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
+  IN OUT EFI_FVB_ATTRIBUTES_2                  *Attributes\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Sets Volume attributes. No polarity translations are done.\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
+  Arguments:\r
+    This                  - Calling context\r
+    Attributes            - output buffer which contains attributes\r
 \r
-Returns:\r
-  EFI_SUCCESS           - Successfully returns\r
+  Returns:\r
+    EFI_SUCCESS           - Successfully returns\r
 \r
 --*/\r
 {\r
-  EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\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
+  return FvbSetVolumeAttributes (\r
+           FvbDevice->Instance,\r
+           Attributes,\r
+           mFvbModuleGlobal\r
+           );\r
 }\r
 \r
 EFI_STATUS\r
 EFIAPI\r
 FvbProtocolEraseBlocks (\r
-  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *This,\r
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
   ...\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\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
+    The EraseBlock() function erases one or more blocks as denoted by the\r
+    variable argument list. The entire parameter list of blocks must be\r
+    verified prior to erasing any blocks.  If a block is requested that does\r
+    not exist within the associated firmware volume (it has a larger index than\r
+    the last block of the firmware volume), the EraseBlock() function must\r
+    return EFI_INVALID_PARAMETER without modifying the contents of the firmware\r
+    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
+  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
+  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
+  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
+  Status = GetFvbInstance (\r
+             FvbDevice->Instance,\r
+             mFvbModuleGlobal,\r
+             &FwhInstance\r
+             );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   NumOfBlocks = FwhInstance->NumOfBlocks;\r
@@ -712,7 +668,7 @@ Returns:
       break;\r
     }\r
 \r
-    NumOfLba = VA_ARG (args, UINT32);\r
+    NumOfLba = VA_ARG (args, UINTN);\r
 \r
     //\r
     // Check input parameters\r
@@ -732,7 +688,7 @@ Returns:
       break;\r
     }\r
 \r
-    NumOfLba = VA_ARG (args, UINT32);\r
+    NumOfLba = VA_ARG (args, UINTN);\r
 \r
     while (NumOfLba > 0) {\r
       Status = QemuFlashEraseBlock (StartingLba);\r
@@ -744,7 +700,6 @@ Returns:
       StartingLba++;\r
       NumOfLba--;\r
     }\r
-\r
   } while (1);\r
 \r
   VA_END (args);\r
@@ -755,109 +710,124 @@ Returns:
 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
+  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
 \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
+  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 operation 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\r
+                            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
-  return QemuFlashWrite ((EFI_LBA)Lba, (UINTN)Offset, NumBytes, (UINT8 *)Buffer);\r
+  return QemuFlashWrite (\r
+           (EFI_LBA)Lba,\r
+           (UINTN)Offset,\r
+           NumBytes,\r
+           (UINT8 *)Buffer\r
+           );\r
 }\r
 \r
 EFI_STATUS\r
 EFIAPI\r
 FvbProtocolRead (\r
-  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL           *This,\r
-  IN CONST EFI_LBA                                      Lba,\r
-  IN CONST UINTN                                        Offset,\r
-  IN OUT UINTN                                    *NumBytes,\r
-  IN UINT8                                        *Buffer\r
+  IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,\r
+  IN CONST EFI_LBA                             Lba,\r
+  IN CONST UINTN                               Offset,\r
+  IN OUT UINTN                                 *NumBytes,\r
+  IN UINT8                                     *Buffer\r
   )\r
+\r
 /*++\r
 \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
+  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 operation 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\r
+                            returned 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
-  return QemuFlashRead ((EFI_LBA)Lba, (UINTN)Offset, NumBytes, (UINT8 *)Buffer);\r
+  return QemuFlashRead (\r
+           (EFI_LBA)Lba,\r
+           (UINTN)Offset,\r
+           NumBytes,\r
+           (UINT8 *)Buffer\r
+           );\r
 }\r
 \r
 EFI_STATUS\r
 ValidateFvHeader (\r
-  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader\r
+  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  Check the integrity of firmware volume header\r
+  Routine Description:\r
+    Check the integrity of firmware volume header\r
 \r
-Arguments:\r
-  FwVolHeader           - A pointer to a firmware volume header\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
+  Returns:\r
+    EFI_SUCCESS           - The firmware volume is consistent\r
+    EFI_NOT_FOUND         - The firmware volume has corrupted. So it is not an\r
+                            FV\r
 \r
 --*/\r
 {\r
-  UINT16 Checksum;\r
+  UINT16  Checksum;\r
 \r
   //\r
   // Verify the header revision, header signature, length\r
@@ -866,85 +836,58 @@ Returns:
   //\r
   if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||\r
       (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||\r
-      (FwVolHeader->FvLength == ((UINTN) -1)) ||\r
+      (FwVolHeader->FvLength == ((UINTN)-1)) ||\r
       ((FwVolHeader->HeaderLength & 0x01) != 0)\r
-      ) {\r
+      )\r
+  {\r
     return EFI_NOT_FOUND;\r
   }\r
-  \r
+\r
   //\r
   // Verify the header checksum\r
   //\r
 \r
-  Checksum = CalculateSum16 ((UINT16 *) FwVolHeader, FwVolHeader->HeaderLength);\r
+  Checksum = CalculateSum16 (\r
+               (UINT16 *)FwVolHeader,\r
+               FwVolHeader->HeaderLength\r
+               );\r
   if (Checksum != 0) {\r
-    UINT16 Expected;\r
+    UINT16  Expected;\r
 \r
     Expected =\r
-      (UINT16) (((UINTN) FwVolHeader->Checksum + 0x10000 - Checksum) & 0xffff);\r
-\r
-    DEBUG ((EFI_D_INFO, "FV@%p Checksum is 0x%x, expected 0x%x\n",\r
-            FwVolHeader, FwVolHeader->Checksum, Expected));\r
+      (UINT16)(((UINTN)FwVolHeader->Checksum + 0x10000 - Checksum) & 0xffff);\r
+\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "FV@%p Checksum is 0x%x, expected 0x%x\n",\r
+      FwVolHeader,\r
+      FwVolHeader->Checksum,\r
+      Expected\r
+      ));\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-STATIC\r
-EFI_STATUS\r
-MarkMemoryRangeForRuntimeAccess (\r
-  EFI_PHYSICAL_ADDRESS                BaseAddress,\r
-  UINT64                              Length\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-\r
-  //\r
-  // Mark flash region as runtime memory\r
-  //\r
-  Status = gDS->RemoveMemorySpace (\r
-                  BaseAddress,\r
-                  Length\r
-                  );\r
-\r
-  Status = gDS->AddMemorySpace (\r
-                  EfiGcdMemoryTypeSystemMemory,\r
-                  BaseAddress,\r
-                  Length,\r
-                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = gBS->AllocatePages (\r
-                  AllocateAddress,\r
-                  EfiRuntimeServicesData,\r
-                  (UINTN) EFI_SIZE_TO_PAGES (Length),\r
-                  &BaseAddress\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return Status;\r
-}\r
-\r
 STATIC\r
 EFI_STATUS\r
 InitializeVariableFvHeader (\r
   VOID\r
   )\r
 {\r
-  EFI_STATUS                          Status;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *GoodFwVolHeader;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-  UINTN                               Length;\r
-  UINTN                               WriteLength;\r
-  UINTN                               BlockSize;\r
+  EFI_STATUS                  Status;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *GoodFwVolHeader;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
+  UINTN                       Length;\r
+  UINTN                       WriteLength;\r
+  UINTN                       BlockSize;\r
 \r
   FwVolHeader =\r
-    (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN)\r
-      PcdGet32 (PcdOvmfFlashNvStorageVariableBase);\r
+    (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)\r
+    PcdGet32 (PcdOvmfFlashNvStorageVariableBase);\r
 \r
-  Length = \r
+  Length =\r
     (FixedPcdGet32 (PcdFlashNvStorageVariableSize) +\r
      FixedPcdGet32 (PcdFlashNvStorageFtwWorkingSize) +\r
      FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize) +\r
@@ -952,18 +895,23 @@ InitializeVariableFvHeader (
 \r
   BlockSize = PcdGet32 (PcdOvmfFirmwareBlockSize);\r
 \r
-  Status      = ValidateFvHeader (FwVolHeader);\r
+  Status = ValidateFvHeader (FwVolHeader);\r
   if (!EFI_ERROR (Status)) {\r
-    if (FwVolHeader->FvLength != Length ||\r
-        FwVolHeader->BlockMap[0].Length != BlockSize) {\r
+    if ((FwVolHeader->FvLength != Length) ||\r
+        (FwVolHeader->BlockMap[0].Length != BlockSize))\r
+    {\r
       Status = EFI_VOLUME_CORRUPTED;\r
     }\r
   }\r
+\r
   if (EFI_ERROR (Status)) {\r
-    UINTN   Offset;\r
-    UINTN   Start;\r
+    UINTN  Offset;\r
+    UINTN  Start;\r
 \r
-    DEBUG ((EFI_D_INFO, "Variable FV header is not valid. It will be reinitialized.\n"));\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "Variable FV header is not valid. It will be reinitialized.\n"\r
+      ));\r
 \r
     //\r
     // Get FvbInfo to provide in FwhInstance.\r
@@ -971,7 +919,7 @@ InitializeVariableFvHeader (
     Status = GetFvbInfo (Length, &GoodFwVolHeader);\r
     ASSERT (!EFI_ERROR (Status));\r
 \r
-    Start = (UINTN)(UINT8*) FwVolHeader - PcdGet32 (PcdOvmfFdBaseAddress);\r
+    Start = (UINTN)(UINT8 *)FwVolHeader - PcdGet32 (PcdOvmfFdBaseAddress);\r
     ASSERT (Start % BlockSize == 0 && Length % BlockSize == 0);\r
     ASSERT (GoodFwVolHeader->HeaderLength <= BlockSize);\r
 \r
@@ -979,7 +927,7 @@ InitializeVariableFvHeader (
     // Erase all the blocks\r
     //\r
     for (Offset = Start; Offset < Start + Length; Offset += BlockSize) {\r
-      Status = QemuFlashEraseBlock ((EFI_LBA) Offset / BlockSize);\r
+      Status = QemuFlashEraseBlock (Offset / BlockSize);\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
 \r
@@ -987,11 +935,12 @@ InitializeVariableFvHeader (
     // Write good FV header\r
     //\r
     WriteLength = GoodFwVolHeader->HeaderLength;\r
-    Status = QemuFlashWrite (\r
-               (EFI_LBA) Start / BlockSize,\r
-               0,\r
-               &WriteLength,\r
-               (UINT8 *) GoodFwVolHeader);\r
+    Status      = QemuFlashWrite (\r
+                    Start / BlockSize,\r
+                    0,\r
+                    &WriteLength,\r
+                    (UINT8 *)GoodFwVolHeader\r
+                    );\r
     ASSERT_EFI_ERROR (Status);\r
     ASSERT (WriteLength == GoodFwVolHeader->HeaderLength);\r
   }\r
@@ -1002,39 +951,41 @@ InitializeVariableFvHeader (
 EFI_STATUS\r
 EFIAPI\r
 FvbInitialize (\r
-  IN EFI_HANDLE         ImageHandle,\r
-  IN EFI_SYSTEM_TABLE   *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
+\r
 /*++\r
 \r
-Routine Description:\r
-  This function does common initialization for FVB services\r
+  Routine Description:\r
+    This function does common initialization for FVB services\r
 \r
-Arguments:\r
+  Arguments:\r
 \r
-Returns:\r
+  Returns:\r
 \r
 --*/\r
 {\r
-  EFI_STATUS                          Status;\r
-  EFI_FW_VOL_INSTANCE                 *FwhInstance;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-  UINT32                              BufferSize;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *PtrBlockMapEntry;\r
-  EFI_HANDLE                          FwbHandle;\r
-  EFI_FW_VOL_BLOCK_DEVICE             *FvbDevice;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *OldFwbInterface;\r
-  UINT32                              MaxLbaSize;\r
-  EFI_PHYSICAL_ADDRESS                BaseAddress;\r
-  UINT64                              Length;\r
-  UINTN                               NumOfBlocks;\r
-  EFI_EVENT                           VirtualAddressChangeEvent;\r
+  EFI_STATUS                  Status;\r
+  EFI_FW_VOL_INSTANCE         *FwhInstance;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
+  UINT32                      BufferSize;\r
+  EFI_FV_BLOCK_MAP_ENTRY      *PtrBlockMapEntry;\r
+  EFI_FW_VOL_BLOCK_DEVICE     *FvbDevice;\r
+  UINT32                      MaxLbaSize;\r
+  EFI_PHYSICAL_ADDRESS        BaseAddress;\r
+  UINTN                       Length;\r
+  UINTN                       NumOfBlocks;\r
+  RETURN_STATUS               PcdStatus;\r
 \r
   if (EFI_ERROR (QemuFlashInitialize ())) {\r
     //\r
     // Return an error so image will be unloaded\r
     //\r
-    DEBUG ((EFI_D_INFO, "QEMU flash was not detected. Writable FVB is not being installed.\n"));\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "QEMU flash was not detected. Writable FVB is not being installed.\n"\r
+      ));\r
     return EFI_WRITE_PROTECTED;\r
   }\r
 \r
@@ -1045,16 +996,19 @@ Returns:
   mFvbModuleGlobal = AllocateRuntimePool (sizeof (ESAL_FWB_GLOBAL));\r
   ASSERT (mFvbModuleGlobal != NULL);\r
 \r
-  BaseAddress = (UINTN) PcdGet32 (PcdOvmfFdBaseAddress);\r
-  Length = PcdGet32 (PcdOvmfFirmwareFdSize);\r
+  BaseAddress = (UINTN)PcdGet32 (PcdOvmfFdBaseAddress);\r
+  Length      = PcdGet32 (PcdOvmfFirmwareFdSize);\r
 \r
   Status = InitializeVariableFvHeader ();\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_INFO, "QEMU Flash: Unable to initialize variable FV header\n"));\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "QEMU Flash: Unable to initialize variable FV header\n"\r
+      ));\r
     return EFI_WRITE_PROTECTED;\r
   }\r
 \r
-  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;\r
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BaseAddress;\r
   Status      = ValidateFvHeader (FwVolHeader);\r
   if (EFI_ERROR (Status)) {\r
     //\r
@@ -1062,44 +1016,42 @@ Returns:
     //\r
     Status = GetFvbInfo (Length, &FwVolHeader);\r
     if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_INFO, "EFI_ERROR (GetFvbInfo (Length, &FwVolHeader))\n"));\r
+      DEBUG ((DEBUG_INFO, "EFI_ERROR (GetFvbInfo (Length, &FwVolHeader))\n"));\r
       return EFI_WRITE_PROTECTED;\r
     }\r
   }\r
 \r
-  BufferSize = (sizeof (EFI_FW_VOL_INSTANCE) + FwVolHeader->HeaderLength - sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
+  BufferSize = (sizeof (EFI_FW_VOL_INSTANCE) +\r
+                FwVolHeader->HeaderLength -\r
+                sizeof (EFI_FIRMWARE_VOLUME_HEADER)\r
+                );\r
+  mFvbModuleGlobal->FvInstance = AllocateRuntimePool (BufferSize);\r
+  ASSERT (mFvbModuleGlobal->FvInstance != NULL);\r
 \r
-  //\r
-  // Only need to allocate once. There is only one copy of physical memory for\r
-  // the private data of each FV instance. But in virtual mode or in physical\r
-  // mode, the address of the the physical memory may be different.\r
-  //\r
-  mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] = AllocateRuntimePool (BufferSize);\r
-  ASSERT (mFvbModuleGlobal->FvInstance[FVB_PHYSICAL] != NULL);\r
-\r
-  //\r
-  // Make a virtual copy of the FvInstance pointer.\r
-  //\r
-  FwhInstance = mFvbModuleGlobal->FvInstance[FVB_PHYSICAL];\r
-  mFvbModuleGlobal->FvInstance[FVB_VIRTUAL] = FwhInstance;\r
+  FwhInstance = mFvbModuleGlobal->FvInstance;\r
 \r
-  mFvbModuleGlobal->NumFv                   = 0;\r
-  MaxLbaSize = 0;\r
+  mFvbModuleGlobal->NumFv = 0;\r
+  MaxLbaSize              = 0;\r
 \r
   FwVolHeader =\r
-    (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN)\r
-      PcdGet32 (PcdOvmfFlashNvStorageVariableBase);\r
+    (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)\r
+    PcdGet32 (PcdOvmfFlashNvStorageVariableBase);\r
 \r
-  FwhInstance->FvBase[FVB_PHYSICAL] = (UINTN) BaseAddress;\r
-  FwhInstance->FvBase[FVB_VIRTUAL]  = (UINTN) BaseAddress;\r
+  FwhInstance->FvBase = (UINTN)BaseAddress;\r
 \r
-  CopyMem ((UINTN *) &(FwhInstance->VolumeHeader), (UINTN *) FwVolHeader, FwVolHeader->HeaderLength);\r
+  CopyMem (\r
+    (UINTN *)&(FwhInstance->VolumeHeader),\r
+    (UINTN *)FwVolHeader,\r
+    FwVolHeader->HeaderLength\r
+    );\r
   FwVolHeader = &(FwhInstance->VolumeHeader);\r
-  EfiInitializeLock (&(FwhInstance->FvbDevLock), TPL_HIGH_LEVEL);\r
 \r
   NumOfBlocks = 0;\r
 \r
-  for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r
+  for (PtrBlockMapEntry = FwVolHeader->BlockMap;\r
+       PtrBlockMapEntry->NumBlocks != 0;\r
+       PtrBlockMapEntry++)\r
+  {\r
     //\r
     // Get the maximum size of a block.\r
     //\r
@@ -1130,100 +1082,54 @@ Returns:
   // Set up the devicepath\r
   //\r
   if (FwVolHeader->ExtHeaderOffset == 0) {\r
+    FV_MEMMAP_DEVICE_PATH  *FvMemmapDevicePath;\r
+\r
     //\r
     // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH\r
     //\r
-    FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &mFvMemmapDevicePathTemplate);\r
-    ((FV_MEMMAP_DEVICE_PATH *) FvbDevice->DevicePath)->MemMapDevPath.StartingAddress = BaseAddress;\r
-    ((FV_MEMMAP_DEVICE_PATH *) FvbDevice->DevicePath)->MemMapDevPath.EndingAddress   = BaseAddress + FwVolHeader->FvLength - 1;\r
+    FvMemmapDevicePath = AllocateCopyPool (\r
+                           sizeof (FV_MEMMAP_DEVICE_PATH),\r
+                           &mFvMemmapDevicePathTemplate\r
+                           );\r
+    FvMemmapDevicePath->MemMapDevPath.StartingAddress = BaseAddress;\r
+    FvMemmapDevicePath->MemMapDevPath.EndingAddress   =\r
+      BaseAddress + FwVolHeader->FvLength - 1;\r
+    FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)FvMemmapDevicePath;\r
   } else {\r
-    FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mFvPIWGDevicePathTemplate);\r
+    FV_PIWG_DEVICE_PATH  *FvPiwgDevicePath;\r
+\r
+    FvPiwgDevicePath = AllocateCopyPool (\r
+                         sizeof (FV_PIWG_DEVICE_PATH),\r
+                         &mFvPIWGDevicePathTemplate\r
+                         );\r
     CopyGuid (\r
-      &((FV_PIWG_DEVICE_PATH *)FvbDevice->DevicePath)->FvDevPath.FvName, \r
+      &FvPiwgDevicePath->FvDevPath.FvName,\r
       (GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)\r
       );\r
+    FvbDevice->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)FvPiwgDevicePath;\r
   }\r
 \r
   //\r
-  // Find a handle with a matching device path that has supports FW Block protocol\r
+  // Module type specific hook.\r
   //\r
-  Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &FvbDevice->DevicePath, &FwbHandle);\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // LocateDevicePath fails so install a new interface and device path\r
-    //\r
-    FwbHandle = NULL;\r
-    DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));\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 (FvbDevice->DevicePath)) {\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
+  InstallProtocolInterfaces (FvbDevice);\r
 \r
-    DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));\r
-    Status = gBS->ReinstallProtocolInterface (\r
-                    FwbHandle,\r
-                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                    OldFwbInterface,\r
-                    &FvbDevice->FwVolBlockInstance\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-  } else {\r
-    //\r
-    // There was a FVB protocol on an End Device Path node\r
-    //\r
-    ASSERT (FALSE);\r
-  }\r
+  MarkIoMemoryRangeForRuntimeAccess (BaseAddress, Length);\r
 \r
-  MarkMemoryRangeForRuntimeAccess (BaseAddress, Length);\r
-\r
-  //\r
-  // Set several PCD values to point to flash\r
-  //\r
-  PcdSet64 (\r
-    PcdFlashNvStorageVariableBase64,\r
-    (UINTN) PcdGet32 (PcdOvmfFlashNvStorageVariableBase)\r
-    );\r
-  PcdSet32 (\r
-    PcdFlashNvStorageFtwWorkingBase,\r
-    PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase)\r
-    );\r
-  PcdSet32 (\r
-    PcdFlashNvStorageFtwSpareBase,\r
-    PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase)\r
-    );\r
+  SetPcdFlashNvStorageBaseAddresses ();\r
 \r
   FwhInstance = (EFI_FW_VOL_INSTANCE *)\r
-    (\r
-      (UINTN) ((UINT8 *) FwhInstance) + FwVolHeader->HeaderLength +\r
-      (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
-    );\r
+                (\r
+                 (UINTN)((UINT8 *)FwhInstance) + FwVolHeader->HeaderLength +\r
+                 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r
+                );\r
 \r
-  VirtualAddressChangeEvent = NULL;\r
-  Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  FvbVirtualddressChangeEvent,\r
-                  NULL,\r
-                  &gEfiEventVirtualAddressChangeGuid,\r
-                  &VirtualAddressChangeEvent\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
+  //\r
+  // Module type specific hook.\r
+  //\r
+  InstallVirtualAddressChangeHandler ();\r
 \r
-  PcdSetBool (PcdOvmfFlashVariablesEnable, TRUE);\r
+  PcdStatus = PcdSetBoolS (PcdOvmfFlashVariablesEnable, TRUE);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
   return EFI_SUCCESS;\r
 }\r