]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Adjust directory structures.
authorqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 18 Jul 2007 14:32:48 +0000 (14:32 +0000)
committerqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 18 Jul 2007 14:32:48 +0000 (14:32 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@3325 6f19259b-4bc3-4df7-8a09-765794883524

64 files changed:
MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.inf [deleted file]
MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.msa [deleted file]
MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf [new file with mode: 0644]
MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.msa [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcDxe.inf [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcDxe.msa [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcExecute.c [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcExecute.h [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcInt.c [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/EbcInt.h [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.S [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.asm [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ia32/EbcSupport.c [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ipf/EbcLowLevel.s [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.h [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.S [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.asm [new file with mode: 0644]
MdeModulePkg/Universal/EbcDxe/x64/EbcSupport.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.h [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.msa [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtractDxe.inf [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.inf [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.msa [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwMisc.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwWorkSpace.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ia32/Ia32FtwMisc.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ipf/IpfFtwMisc.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/x64/x64FtwMisc.c [new file with mode: 0644]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.dxs [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.h [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.inf [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.msa [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwMisc.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwWorkSpace.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ipf/IpfFtwMisc.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/x64/x64FtwMisc.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.c [deleted file]
MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.dxs [deleted file]
MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.h [deleted file]
MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.inf [deleted file]
MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.msa [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.c [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.dxs [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.h [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.inf [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.msa [deleted file]
MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.c [new file with mode: 0644]
MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.h [new file with mode: 0644]
MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf [new file with mode: 0644]
MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.msa [new file with mode: 0644]
MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.c [deleted file]
MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.h [deleted file]
MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.inf [deleted file]
MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.msa [deleted file]
MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.c [new file with mode: 0644]
MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.h [new file with mode: 0644]
MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.inf [new file with mode: 0644]
MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.msa [new file with mode: 0644]

diff --git a/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.inf b/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.inf
deleted file mode 100644 (file)
index 22b0792..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-#/** @file\r
-# Component description file for DebugSupport module.\r
-#\r
-# This driver installs DebugSupport protocol for the selected processor.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\r
-#  All rights reserved. This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution. The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-#\r
-#**/\r
-\r
-################################################################################\r
-#\r
-# Defines Section - statements that will be processed to create a Makefile.\r
-#\r
-################################################################################\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = DebugSupport\r
-  FILE_GUID                      = 911D584C-35F7-4955-BEF9-B452769DDC3A\r
-  MODULE_TYPE                    = DXE_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  EDK_RELEASE_VERSION            = 0x00020000\r
-  EFI_SPECIFICATION_VERSION      = 0x00020000\r
-\r
-  ENTRY_POINT                    = InitializeDebugSupportDriver\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF\r
-#\r
-\r
-################################################################################\r
-#\r
-# Sources Section - list of files that are required for the build to succeed.\r
-#\r
-################################################################################\r
-\r
-[Sources.common]\r
-  DebugSupport.c\r
-\r
-[Sources.Ia32]\r
-  Ia32/plDebugSupport.c\r
-  Ia32/plDebugSupport.h\r
-  Ia32/AsmFuncs.S\r
-  Ia32/AsmFuncs.asm\r
-\r
-[Sources.X64]\r
-  x64/plDebugSupport.c\r
-  x64/plDebugSupport.h\r
-  x64/AsmFuncs.S\r
-  x64/AsmFuncs.asm\r
-\r
-[Sources.IPF]\r
-  ipf/plDebugSupport.h\r
-  ipf/plDebugSupport.c\r
-  ipf/Ds64Macros.i\r
-  ipf/common.i\r
-  ipf/AsmFuncs.s\r
-\r
-\r
-################################################################################\r
-#\r
-# Package Dependency Section - list of Package files that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-\r
-\r
-################################################################################\r
-#\r
-# Library Class Section - list of Library Classes that are required for\r
-#                         this module.\r
-#\r
-################################################################################\r
-\r
-[LibraryClasses]\r
-  UefiBootServicesTableLib\r
-  MemoryAllocationLib\r
-  BaseMemoryLib\r
-  UefiDriverEntryPoint\r
-  DebugLib\r
-\r
-[LibraryClasses.IA32]\r
-  PcdLib\r
-  BaseLib\r
-\r
-[LibraryClasses.X64]\r
-  BaseLib\r
-\r
-\r
-################################################################################\r
-#\r
-# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
-#                           that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Protocols]\r
-  gEfiLoadedImageProtocolGuid                   # PROTOCOL ALWAYS_CONSUMED\r
-  gEfiDebugSupportProtocolGuid                  # PROTOCOL SOMETIMES_PRODUCED\r
-\r
-\r
-################################################################################\r
-#\r
-# Pcd FEATURE_FLAG - list of PCDs that this module is coded for.\r
-#\r
-################################################################################\r
-\r
-[PcdsFeatureFlag.IA32]\r
-  PcdNtEmulatorEnable|gEfiMdeModulePkgTokenSpaceGuid\r
-\r
-\r
-################################################################################\r
-#\r
-# Dependency Expression Section - list of Dependency expressions that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Depex]\r
-  TRUE\r
-\r
diff --git a/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.msa b/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.msa
deleted file mode 100644 (file)
index 23ebaf4..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
-  <MsaHeader>\r
-    <ModuleName>DebugSupport</ModuleName>\r
-    <ModuleType>DXE_DRIVER</ModuleType>\r
-    <GuidValue>911D584C-35F7-4955-BEF9-B452769DDC3A</GuidValue>\r
-    <Version>1.0</Version>\r
-    <Abstract>Component description file for DebugSupport module.</Abstract>\r
-    <Description>This driver installs DebugSupport protocol for the selected processor.</Description>\r
-    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
-    <License>All rights reserved. This program and the accompanying materials\r
-      are licensed and made available under the terms and conditions of the BSD License\r
-      which accompanies this distribution.  The full text of the license may be found at\r
-      http://opensource.org/licenses/bsd-license.php\r
-      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.</License>\r
-    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
-  </MsaHeader>\r
-  <ModuleDefinitions>\r
-    <SupportedArchitectures>IA32 X64 IPF</SupportedArchitectures>\r
-    <BinaryModule>false</BinaryModule>\r
-    <OutputFileBasename>DebugSupport</OutputFileBasename>\r
-  </ModuleDefinitions>\r
-  <LibraryClassDefinitions>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>DebugLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiDriverEntryPoint</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>BaseMemoryLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>MemoryAllocationLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiBootServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED" SupArchList="IA32 X64">\r
-      <Keyword>BaseLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED" SupArchList="IA32">\r
-      <Keyword>PcdLib</Keyword>\r
-    </LibraryClass>\r
-  </LibraryClassDefinitions>\r
-  <SourceFiles>\r
-    <Filename>DebugSupport.c</Filename>\r
-    <Filename>DebugSupport.dxs</Filename>\r
-    <Filename SupArchList="IA32" ToolChainFamily="MSFT">Ia32/AsmFuncs.asm</Filename>\r
-    <Filename SupArchList="IA32" ToolChainFamily="GCC">Ia32/AsmFuncs.S</Filename>\r
-    <Filename SupArchList="IA32">Ia32/plDebugSupport.h</Filename>\r
-    <Filename SupArchList="IA32">Ia32/plDebugSupport.c</Filename>\r
-    <Filename SupArchList="IPF">ipf/AsmFuncs.s</Filename>\r
-    <Filename SupArchList="IPF">ipf/common.i</Filename>\r
-    <Filename SupArchList="IPF">ipf/Ds64Macros.i</Filename>\r
-    <Filename SupArchList="IPF">ipf/plDebugSupport.c</Filename>\r
-    <Filename SupArchList="IPF">ipf/plDebugSupport.h</Filename>\r
-    <Filename SupArchList="X64" ToolChainFamily="MSFT">x64/AsmFuncs.asm</Filename>\r
-    <Filename SupArchList="X64" ToolChainFamily="GCC">x64/AsmFuncs.S</Filename>\r
-    <Filename SupArchList="X64">x64/plDebugSupport.h</Filename>\r
-    <Filename SupArchList="X64">x64/plDebugSupport.c</Filename>\r
-  </SourceFiles>\r
-  <PackageDependencies>\r
-    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
-    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
-  </PackageDependencies>\r
-  <Protocols>\r
-    <Protocol Usage="SOMETIMES_PRODUCED">\r
-      <ProtocolCName>gEfiDebugSupportProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-    <Protocol Usage="ALWAYS_CONSUMED">\r
-      <ProtocolCName>gEfiLoadedImageProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-  </Protocols>\r
-  <Externs>\r
-    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
-    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
-    <Extern>\r
-      <ModuleEntryPoint>InitializeDebugSupportDriver</ModuleEntryPoint>\r
-    </Extern>\r
-  </Externs>\r
-  <PcdCoded>\r
-    <PcdEntry PcdItemType="FEATURE_FLAG" Usage="ALWAYS_CONSUMED" SupArchList="IA32">\r
-      <C_Name>PcdNtEmulatorEnable</C_Name>\r
-      <TokenSpaceGuidCName>gEfiMdeModulePkgTokenSpaceGuid</TokenSpaceGuidCName>\r
-      <HelpText>If this PCD is set as TRUE, NT emulator will be endabled.</HelpText>\r
-    </PcdEntry>\r
-  </PcdCoded>\r
-</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf b/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf
new file mode 100644 (file)
index 0000000..d8c7df2
--- /dev/null
@@ -0,0 +1,133 @@
+#/** @file\r
+# Component description file for DebugSupport module.\r
+#\r
+# This driver installs DebugSupport protocol for the selected processor.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+################################################################################\r
+#\r
+# Defines Section - statements that will be processed to create a Makefile.\r
+#\r
+################################################################################\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = DebugSupportDxe\r
+  FILE_GUID                      = 911D584C-35F7-4955-BEF9-B452769DDC3A\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = InitializeDebugSupportDriver\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF\r
+#\r
+\r
+################################################################################\r
+#\r
+# Sources Section - list of files that are required for the build to succeed.\r
+#\r
+################################################################################\r
+\r
+[Sources.common]\r
+  DebugSupport.c\r
+\r
+[Sources.Ia32]\r
+  Ia32/plDebugSupport.c\r
+  Ia32/plDebugSupport.h\r
+  Ia32/AsmFuncs.S\r
+  Ia32/AsmFuncs.asm\r
+\r
+[Sources.X64]\r
+  x64/plDebugSupport.c\r
+  x64/plDebugSupport.h\r
+  x64/AsmFuncs.S\r
+  x64/AsmFuncs.asm\r
+\r
+[Sources.IPF]\r
+  ipf/plDebugSupport.h\r
+  ipf/plDebugSupport.c\r
+  ipf/Ds64Macros.i\r
+  ipf/common.i\r
+  ipf/AsmFuncs.s\r
+\r
+\r
+################################################################################\r
+#\r
+# Package Dependency Section - list of Package files that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+################################################################################\r
+#\r
+# Library Class Section - list of Library Classes that are required for\r
+#                         this module.\r
+#\r
+################################################################################\r
+\r
+[LibraryClasses]\r
+  UefiBootServicesTableLib\r
+  MemoryAllocationLib\r
+  BaseMemoryLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+\r
+[LibraryClasses.IA32]\r
+  PcdLib\r
+  BaseLib\r
+\r
+[LibraryClasses.X64]\r
+  BaseLib\r
+\r
+\r
+################################################################################\r
+#\r
+# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
+#                           that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Protocols]\r
+  gEfiLoadedImageProtocolGuid                   # PROTOCOL ALWAYS_CONSUMED\r
+  gEfiDebugSupportProtocolGuid                  # PROTOCOL SOMETIMES_PRODUCED\r
+\r
+\r
+################################################################################\r
+#\r
+# Pcd FEATURE_FLAG - list of PCDs that this module is coded for.\r
+#\r
+################################################################################\r
+\r
+[PcdsFeatureFlag.IA32]\r
+  PcdNtEmulatorEnable|gEfiMdeModulePkgTokenSpaceGuid\r
+\r
+\r
+################################################################################\r
+#\r
+# Dependency Expression Section - list of Dependency expressions that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Depex]\r
+  TRUE\r
+\r
diff --git a/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.msa b/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.msa
new file mode 100644 (file)
index 0000000..23ebaf4
--- /dev/null
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
+  <MsaHeader>\r
+    <ModuleName>DebugSupport</ModuleName>\r
+    <ModuleType>DXE_DRIVER</ModuleType>\r
+    <GuidValue>911D584C-35F7-4955-BEF9-B452769DDC3A</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for DebugSupport module.</Abstract>\r
+    <Description>This driver installs DebugSupport protocol for the selected processor.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>DebugSupport</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DebugLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseMemoryLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>MemoryAllocationLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED" SupArchList="IA32 X64">\r
+      <Keyword>BaseLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED" SupArchList="IA32">\r
+      <Keyword>PcdLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>DebugSupport.c</Filename>\r
+    <Filename>DebugSupport.dxs</Filename>\r
+    <Filename SupArchList="IA32" ToolChainFamily="MSFT">Ia32/AsmFuncs.asm</Filename>\r
+    <Filename SupArchList="IA32" ToolChainFamily="GCC">Ia32/AsmFuncs.S</Filename>\r
+    <Filename SupArchList="IA32">Ia32/plDebugSupport.h</Filename>\r
+    <Filename SupArchList="IA32">Ia32/plDebugSupport.c</Filename>\r
+    <Filename SupArchList="IPF">ipf/AsmFuncs.s</Filename>\r
+    <Filename SupArchList="IPF">ipf/common.i</Filename>\r
+    <Filename SupArchList="IPF">ipf/Ds64Macros.i</Filename>\r
+    <Filename SupArchList="IPF">ipf/plDebugSupport.c</Filename>\r
+    <Filename SupArchList="IPF">ipf/plDebugSupport.h</Filename>\r
+    <Filename SupArchList="X64" ToolChainFamily="MSFT">x64/AsmFuncs.asm</Filename>\r
+    <Filename SupArchList="X64" ToolChainFamily="GCC">x64/AsmFuncs.S</Filename>\r
+    <Filename SupArchList="X64">x64/plDebugSupport.h</Filename>\r
+    <Filename SupArchList="X64">x64/plDebugSupport.c</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
+    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="SOMETIMES_PRODUCED">\r
+      <ProtocolCName>gEfiDebugSupportProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+    <Protocol Usage="ALWAYS_CONSUMED">\r
+      <ProtocolCName>gEfiLoadedImageProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>InitializeDebugSupportDriver</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+  <PcdCoded>\r
+    <PcdEntry PcdItemType="FEATURE_FLAG" Usage="ALWAYS_CONSUMED" SupArchList="IA32">\r
+      <C_Name>PcdNtEmulatorEnable</C_Name>\r
+      <TokenSpaceGuidCName>gEfiMdeModulePkgTokenSpaceGuid</TokenSpaceGuidCName>\r
+      <HelpText>If this PCD is set as TRUE, NT emulator will be endabled.</HelpText>\r
+    </PcdEntry>\r
+  </PcdCoded>\r
+</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf
new file mode 100644 (file)
index 0000000..8ae072b
--- /dev/null
@@ -0,0 +1,115 @@
+#/** @file\r
+# Component description file for Ebc module.\r
+#\r
+# This module for the EBC virtual machine implementation produces \r
+#  EBC and EBC debug support protocols.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+################################################################################\r
+#\r
+# Defines Section - statements that will be processed to create a Makefile.\r
+#\r
+################################################################################\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = EbcDxe\r
+  FILE_GUID                      = 13AC6DD0-73D0-11D4-B06B-00AA00BD6DE7\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = InitializeEbcDriver\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF\r
+#\r
+\r
+################################################################################\r
+#\r
+# Sources Section - list of files that are required for the build to succeed.\r
+#\r
+################################################################################\r
+\r
+[Sources.common]\r
+  EbcExecute.h\r
+  EbcExecute.c\r
+  EbcInt.h\r
+  EbcInt.c\r
+\r
+[Sources.Ia32]\r
+  Ia32/EbcSupport.c\r
+  Ia32/EbcLowLevel.S\r
+  Ia32/EbcLowLevel.asm\r
+\r
+[Sources.X64]\r
+  x64/EbcSupport.c\r
+  x64/EbcLowLevel.S\r
+  x64/EbcLowLevel.asm\r
+\r
+[Sources.IPF]\r
+  Ipf/EbcSupport.h\r
+  Ipf/EbcSupport.c\r
+  Ipf/EbcLowLevel.s\r
+\r
+\r
+################################################################################\r
+#\r
+# Package Dependency Section - list of Package files that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  \r
+\r
+\r
+################################################################################\r
+#\r
+# Library Class Section - list of Library Classes that are required for\r
+#                         this module.\r
+#\r
+################################################################################\r
+\r
+[LibraryClasses]\r
+  MemoryAllocationLib\r
+  UefiBootServicesTableLib\r
+  BaseMemoryLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+  BaseLib\r
+\r
+\r
+################################################################################\r
+#\r
+# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
+#                           that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Protocols]\r
+  gEfiDebugSupportProtocolGuid                  # PROTOCOL ALWAYS_PRODUCED\r
+  gEfiEbcProtocolGuid                           # PROTOCOL ALWAYS_PRODUCED\r
+\r
+################################################################################\r
+#\r
+# Dependency Expression Section - list of Dependency expressions that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Depex]\r
+  TRUE\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcDxe.msa b/MdeModulePkg/Universal/EbcDxe/EbcDxe.msa
new file mode 100644 (file)
index 0000000..957f700
--- /dev/null
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
+  <MsaHeader>\r
+    <ModuleName>Ebc</ModuleName>\r
+    <ModuleType>DXE_DRIVER</ModuleType>\r
+    <GuidValue>13AC6DD0-73D0-11D4-B06B-00AA00BD6DE7</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for Ebc module.</Abstract>\r
+    <Description>This module for the EBC virtual machine implementation produces \r
+      EBC and EBC debug support protocols.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>Ebc</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DebugLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseMemoryLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>MemoryAllocationLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>EbcInt.c</Filename>\r
+    <Filename>EbcInt.h</Filename>\r
+    <Filename>EbcExecute.c</Filename>\r
+    <Filename>EbcExecute.h</Filename>\r
+    <Filename>Ebc.dxs</Filename>\r
+    <Filename SupArchList="IA32" ToolChainFamily="MSFT">Ia32/EbcLowLevel.asm</Filename>\r
+    <Filename SupArchList="IA32" ToolChainFamily="GCC">Ia32/EbcLowLevel.S</Filename>\r
+    <Filename SupArchList="IA32">Ia32/EbcSupport.c</Filename>\r
+    <Filename SupArchList="X64" ToolChainFamily="MSFT">x64/EbcLowLevel.asm</Filename>\r
+    <Filename SupArchList="X64" ToolChainFamily="GCC">x64/EbcLowLevel.S</Filename>\r
+    <Filename SupArchList="X64">x64/EbcSupport.c</Filename>\r
+    <Filename SupArchList="IPF">Ipf/EbcLowLevel.s</Filename>\r
+    <Filename SupArchList="IPF">Ipf/EbcSupport.c</Filename>\r
+    <Filename SupArchList="IPF">Ipf/EbcSupport.h</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
+    <Package PackageGuid="2759ded5-bb57-4b06-af4f-c398fa552719"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiEbcProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiDebugSupportProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>InitializeEbcDriver</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c b/MdeModulePkg/Universal/EbcDxe/EbcExecute.c
new file mode 100644 (file)
index 0000000..174e774
--- /dev/null
@@ -0,0 +1,4564 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  EbcExecute.c\r
+\r
+Abstract:\r
+\r
+  Contains code that implements the virtual machine.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+\r
+//\r
+// Define some useful data size constants to allow switch statements based on\r
+// size of operands or data.\r
+//\r
+#define DATA_SIZE_INVALID 0\r
+#define DATA_SIZE_8       1\r
+#define DATA_SIZE_16      2\r
+#define DATA_SIZE_32      4\r
+#define DATA_SIZE_64      8\r
+#define DATA_SIZE_N       48  // 4 or 8\r
+//\r
+// Structure we'll use to dispatch opcodes to execute functions.\r
+//\r
+typedef struct {\r
+  EFI_STATUS (*ExecuteFunction) (IN VM_CONTEXT * VmPtr);\r
+}\r
+VM_TABLE_ENTRY;\r
+\r
+typedef\r
+UINT64\r
+(*DATA_MANIP_EXEC_FUNCTION) (\r
+  IN VM_CONTEXT * VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+INT16\r
+VmReadIndex16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+INT32\r
+VmReadIndex32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+INT64\r
+VmReadIndex64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     CodeOffset\r
+  );\r
+\r
+STATIC\r
+UINT8\r
+VmReadMem8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT16\r
+VmReadMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT32\r
+VmReadMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+VmReadMem64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+UINTN\r
+VmReadMemN (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT8      Data\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT16     Data\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT32     Data\r
+  );\r
+\r
+STATIC\r
+UINT16\r
+VmReadCode16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINT32\r
+VmReadCode32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+VmReadCode64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT8\r
+VmReadImmed8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT16\r
+VmReadImmed16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT32\r
+VmReadImmed32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+INT64\r
+VmReadImmed64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  );\r
+\r
+STATIC\r
+UINTN\r
+ConvertStackAddr (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteDataManip (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN BOOLEAN      IsSignedOperation\r
+  );\r
+\r
+//\r
+// Functions that execute VM opcodes\r
+//\r
+STATIC\r
+EFI_STATUS\r
+ExecuteBREAK (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP8 (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCALL (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteRET (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMPI (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVxx (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVI (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVIn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVREL (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSHn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSH (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOPn (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSignedDataManip (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteUnsignedDataManip (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteLOADSP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSTORESP (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnd (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnw (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+//\r
+// Data manipulation subfunctions\r
+//\r
+STATIC\r
+UINT64\r
+ExecuteNOT (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNEG (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteADD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSUB (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMUL (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMULU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIV (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIVU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMOD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMODU (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteAND (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteOR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteXOR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHL (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteASHR (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDB (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDW (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDD (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT64     Op1,\r
+  IN UINT64     Op2\r
+  );\r
+\r
+//\r
+// Once we retrieve the operands for the data manipulation instructions,\r
+// call these functions to perform the operation.\r
+//\r
+static CONST DATA_MANIP_EXEC_FUNCTION mDataManipDispatchTable[] = {\r
+  ExecuteNOT,\r
+  ExecuteNEG,\r
+  ExecuteADD,\r
+  ExecuteSUB,\r
+  ExecuteMUL,\r
+  ExecuteMULU,\r
+  ExecuteDIV,\r
+  ExecuteDIVU,\r
+  ExecuteMOD,\r
+  ExecuteMODU,\r
+  ExecuteAND,\r
+  ExecuteOR,\r
+  ExecuteXOR,\r
+  ExecuteSHL,\r
+  ExecuteSHR,\r
+  ExecuteASHR,\r
+  ExecuteEXTNDB,\r
+  ExecuteEXTNDW,\r
+  ExecuteEXTNDD,\r
+};\r
+\r
+static CONST VM_TABLE_ENTRY           mVmOpcodeTable[] = {\r
+  { ExecuteBREAK },             // opcode 0x00\r
+  { ExecuteJMP },               // opcode 0x01\r
+  { ExecuteJMP8 },              // opcode 0x02\r
+  { ExecuteCALL },              // opcode 0x03\r
+  { ExecuteRET },               // opcode 0x04\r
+  { ExecuteCMP },               // opcode 0x05 CMPeq\r
+  { ExecuteCMP },               // opcode 0x06 CMPlte\r
+  { ExecuteCMP },               // opcode 0x07 CMPgte\r
+  { ExecuteCMP },               // opcode 0x08 CMPulte\r
+  { ExecuteCMP },               // opcode 0x09 CMPugte\r
+  { ExecuteUnsignedDataManip }, // opcode 0x0A NOT\r
+  { ExecuteSignedDataManip },   // opcode 0x0B NEG\r
+  { ExecuteSignedDataManip },   // opcode 0x0C ADD\r
+  { ExecuteSignedDataManip },   // opcode 0x0D SUB\r
+  { ExecuteSignedDataManip },   // opcode 0x0E MUL\r
+  { ExecuteUnsignedDataManip }, // opcode 0x0F MULU\r
+  { ExecuteSignedDataManip },   // opcode 0x10 DIV\r
+  { ExecuteUnsignedDataManip }, // opcode 0x11 DIVU\r
+  { ExecuteSignedDataManip },   // opcode 0x12 MOD\r
+  { ExecuteUnsignedDataManip }, // opcode 0x13 MODU\r
+  { ExecuteUnsignedDataManip }, // opcode 0x14 AND\r
+  { ExecuteUnsignedDataManip }, // opcode 0x15 OR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x16 XOR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x17 SHL\r
+  { ExecuteUnsignedDataManip }, // opcode 0x18 SHR\r
+  { ExecuteSignedDataManip },   // opcode 0x19 ASHR\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1A EXTNDB\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1B EXTNDW\r
+  { ExecuteUnsignedDataManip }, // opcode 0x1C EXTNDD\r
+  { ExecuteMOVxx },             // opcode 0x1D MOVBW\r
+  { ExecuteMOVxx },             // opcode 0x1E MOVWW\r
+  { ExecuteMOVxx },             // opcode 0x1F MOVDW\r
+  { ExecuteMOVxx },             // opcode 0x20 MOVQW\r
+  { ExecuteMOVxx },             // opcode 0x21 MOVBD\r
+  { ExecuteMOVxx },             // opcode 0x22 MOVWD\r
+  { ExecuteMOVxx },             // opcode 0x23 MOVDD\r
+  { ExecuteMOVxx },             // opcode 0x24 MOVQD\r
+  { ExecuteMOVsnw },            // opcode 0x25 MOVsnw\r
+  { ExecuteMOVsnd },            // opcode 0x26 MOVsnd\r
+  { NULL },                     // opcode 0x27\r
+  { ExecuteMOVxx },             // opcode 0x28 MOVqq\r
+  { ExecuteLOADSP },            // opcode 0x29 LOADSP SP1, R2\r
+  { ExecuteSTORESP },           // opcode 0x2A STORESP R1, SP2\r
+  { ExecutePUSH },              // opcode 0x2B PUSH {@}R1 [imm16]\r
+  { ExecutePOP },               // opcode 0x2C POP {@}R1 [imm16]\r
+  { ExecuteCMPI },              // opcode 0x2D CMPIEQ\r
+  { ExecuteCMPI },              // opcode 0x2E CMPILTE\r
+  { ExecuteCMPI },              // opcode 0x2F CMPIGTE\r
+  { ExecuteCMPI },              // opcode 0x30 CMPIULTE\r
+  { ExecuteCMPI },              // opcode 0x31 CMPIUGTE\r
+  { ExecuteMOVxx },             // opcode 0x32 MOVN\r
+  { ExecuteMOVxx },             // opcode 0x33 MOVND\r
+  { NULL },                     // opcode 0x34\r
+  { ExecutePUSHn },             // opcode 0x35\r
+  { ExecutePOPn },              // opcode 0x36\r
+  { ExecuteMOVI },              // opcode 0x37 - mov immediate data\r
+  { ExecuteMOVIn },             // opcode 0x38 - mov immediate natural\r
+  { ExecuteMOVREL }             // opcode 0x39 - move data relative to PC\r
+};\r
+\r
+//\r
+// Length of JMP instructions, depending on upper two bits of opcode.\r
+//\r
+static CONST UINT8                    mJMPLen[] = { 2, 2, 6, 10 };\r
+\r
+//\r
+// Simple Debugger Protocol GUID\r
+//\r
+EFI_GUID mEbcSimpleDebuggerProtocolGuid = EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID;\r
+\r
+EFI_STATUS\r
+EbcExecuteInstructions (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL *This,\r
+  IN VM_CONTEXT               *VmPtr,\r
+  IN OUT UINTN                *InstructionCount\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Given a pointer to a new VM context, execute one or more instructions. This\r
+  function is only used for test purposes via the EBC VM test protocol.\r
+\r
+Arguments:\r
+\r
+  This              - pointer to protocol interface\r
+  VmPtr             - pointer to a VM context\r
+  InstructionCount  - how many instructions to execute. 0 if don't count.\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  UINTN       ExecFunc;\r
+  EFI_STATUS  Status;\r
+  UINTN       InstructionsLeft;\r
+  UINTN       SavedInstructionCount;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  if (*InstructionCount == 0) {\r
+    InstructionsLeft = 1;\r
+  } else {\r
+    InstructionsLeft = *InstructionCount;\r
+  }\r
+\r
+  SavedInstructionCount = *InstructionCount;\r
+  *InstructionCount     = 0;\r
+\r
+  //\r
+  // Index into the opcode table using the opcode byte for this instruction.\r
+  // This gives you the execute function, which we first test for null, then\r
+  // call it if it's not null.\r
+  //\r
+  while (InstructionsLeft != 0) {\r
+    ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction;\r
+    if (ExecFunc == (UINTN) NULL) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      return EFI_UNSUPPORTED;\r
+    } else {\r
+      mVmOpcodeTable[(*VmPtr->Ip & 0x3F)].ExecuteFunction (VmPtr);\r
+      *InstructionCount = *InstructionCount + 1;\r
+    }\r
+\r
+    //\r
+    // Decrement counter if applicable\r
+    //\r
+    if (SavedInstructionCount != 0) {\r
+      InstructionsLeft--;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EbcExecute (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute an EBC image from an entry point or from a published protocol.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to prepared VM context.\r
+\r
+Returns:\r
+\r
+  Standard EBC status.\r
+\r
+--*/\r
+{\r
+  UINTN                             ExecFunc;\r
+  UINT8                             StackCorrupted;\r
+  EFI_STATUS                        Status;\r
+  EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL  *EbcSimpleDebugger;\r
+\r
+  mVmPtr            = VmPtr;\r
+  EbcSimpleDebugger = NULL;\r
+  Status            = EFI_SUCCESS;\r
+  StackCorrupted    = 0;\r
+\r
+  //\r
+  // Make sure the magic value has been put on the stack before we got here.\r
+  //\r
+  if (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE) {\r
+    StackCorrupted = 1;\r
+  }\r
+\r
+  VmPtr->FramePtr = (VOID *) ((UINT8 *) (UINTN) VmPtr->R[0] + 8);\r
+\r
+  //\r
+  // Try to get the debug support for EBC\r
+  //\r
+  DEBUG_CODE_BEGIN ();\r
+    Status = gBS->LocateProtocol (\r
+                    &mEbcSimpleDebuggerProtocolGuid,\r
+                    NULL,\r
+                    (VOID **) &EbcSimpleDebugger\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      EbcSimpleDebugger = NULL;\r
+    }\r
+  DEBUG_CODE_END ();\r
+\r
+  //\r
+  // Save the start IP for debug. For example, if we take an exception we\r
+  // can print out the location of the exception relative to the entry point,\r
+  // which could then be used in a disassembly listing to find the problem.\r
+  //\r
+  VmPtr->EntryPoint = (VOID *) VmPtr->Ip;\r
+\r
+  //\r
+  // We'll wait for this flag to know when we're done. The RET\r
+  // instruction sets it if it runs out of stack.\r
+  //\r
+  VmPtr->StopFlags = 0;\r
+  while (!(VmPtr->StopFlags & STOPFLAG_APP_DONE)) {\r
+    //\r
+    // If we've found a simple debugger protocol, call it\r
+    //\r
+    DEBUG_CODE_BEGIN ();\r
+      if (EbcSimpleDebugger != NULL) {\r
+        EbcSimpleDebugger->Debugger (EbcSimpleDebugger, VmPtr);\r
+      }\r
+    DEBUG_CODE_END ();\r
+\r
+    //\r
+    // Verify the opcode is in range. Otherwise generate an exception.\r
+    //\r
+    if ((*VmPtr->Ip & OPCODE_M_OPCODE) >= (sizeof (mVmOpcodeTable) / sizeof (mVmOpcodeTable[0]))) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      Status = EFI_UNSUPPORTED;\r
+      goto Done;\r
+    }\r
+    //\r
+    // Use the opcode bits to index into the opcode dispatch table. If the\r
+    // function pointer is null then generate an exception.\r
+    //\r
+    ExecFunc = (UINTN) mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction;\r
+    if (ExecFunc == (UINTN) NULL) {\r
+      EbcDebugSignalException (EXCEPT_EBC_INVALID_OPCODE, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      Status = EFI_UNSUPPORTED;\r
+      goto Done;\r
+    }\r
+    //\r
+    // The EBC VM is a strongly ordered processor, so perform a fence operation before\r
+    // and after each instruction is executed.\r
+    //\r
+    MemoryFence ();\r
+\r
+    mVmOpcodeTable[(*VmPtr->Ip & OPCODE_M_OPCODE)].ExecuteFunction (VmPtr);\r
+\r
+    MemoryFence ();\r
+\r
+    //\r
+    // If the step flag is set, signal an exception and continue. We don't\r
+    // clear it here. Assuming the debugger is responsible for clearing it.\r
+    //\r
+    if (VMFLAG_ISSET (VmPtr, VMFLAGS_STEP)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STEP, EXCEPTION_FLAG_NONE, VmPtr);\r
+    }\r
+    //\r
+    // Make sure stack has not been corrupted. Only report it once though.\r
+    //\r
+    if (!StackCorrupted && (*VmPtr->StackMagicPtr != (UINTN) VM_STACK_KEY_VALUE)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      StackCorrupted = 1;\r
+    }\r
+    if (!StackCorrupted && ((UINT64)VmPtr->R[0] <= (UINT64)(UINTN) VmPtr->StackTop)) {\r
+      EbcDebugSignalException (EXCEPT_EBC_STACK_FAULT, EXCEPTION_FLAG_FATAL, VmPtr);\r
+      StackCorrupted = 1;\r
+    }\r
+  }\r
+\r
+Done:\r
+  mVmPtr          = NULL;\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVxx (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the MOVxx instructions.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to a VM context.\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+Instruction format:\r
+  \r
+  MOV[b|w|d|q|n]{w|d} {@}R1 {Index16|32}, {@}R2 {Index16|32}\r
+  MOVqq {@}R1 {Index64}, {@}R2 {Index64}\r
+\r
+  Copies contents of [R2] -> [R1], zero extending where required.\r
+\r
+  First character indicates the size of the move.\r
+  Second character indicates the size of the index(s).\r
+\r
+  Invalid to have R1 direct with index.\r
+  \r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   OpcMasked;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  UINT8   MoveSize;\r
+  INT16   Index16;\r
+  INT32   Index32;\r
+  INT64   Index64Op1;\r
+  INT64   Index64Op2;\r
+  UINT64  Data64;\r
+  UINT64  DataMask;\r
+  UINTN   Source;\r
+\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  OpcMasked = (UINT8) (Opcode & OPCODE_M_OPCODE);\r
+\r
+  //\r
+  // Get the operands byte so we can get R1 and R2\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Assume no indexes\r
+  //\r
+  Index64Op1  = 0;\r
+  Index64Op2  = 0;\r
+  Data64      = 0;\r
+\r
+  //\r
+  // Determine if we have an index/immediate data. Base instruction size\r
+  // is 2 (opcode + operands). Add to this size each index specified.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & (OPCODE_M_IMMED_OP1 | OPCODE_M_IMMED_OP2)) {\r
+    //\r
+    // Determine size of the index from the opcode. Then get it.\r
+    //\r
+    if ((OpcMasked <= OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVNW)) {\r
+      //\r
+      // MOVBW, MOVWW, MOVDW, MOVQW, and MOVNW have 16-bit immediate index.\r
+      // Get one or both index values.\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index16     = VmReadIndex16 (VmPtr, 2);\r
+        Index64Op1  = (INT64) Index16;\r
+        Size += sizeof (UINT16);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index16     = VmReadIndex16 (VmPtr, Size);\r
+        Index64Op2  = (INT64) Index16;\r
+        Size += sizeof (UINT16);\r
+      }\r
+    } else if ((OpcMasked <= OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVND)) {\r
+      //\r
+      // MOVBD, MOVWD, MOVDD, MOVQD, and MOVND have 32-bit immediate index\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index32     = VmReadIndex32 (VmPtr, 2);\r
+        Index64Op1  = (INT64) Index32;\r
+        Size += sizeof (UINT32);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index32     = VmReadIndex32 (VmPtr, Size);\r
+        Index64Op2  = (INT64) Index32;\r
+        Size += sizeof (UINT32);\r
+      }\r
+    } else if (OpcMasked == OPCODE_MOVQQ) {\r
+      //\r
+      // MOVqq -- only form with a 64-bit index\r
+      //\r
+      if (Opcode & OPCODE_M_IMMED_OP1) {\r
+        Index64Op1 = VmReadIndex64 (VmPtr, 2);\r
+        Size += sizeof (UINT64);\r
+      }\r
+\r
+      if (Opcode & OPCODE_M_IMMED_OP2) {\r
+        Index64Op2 = VmReadIndex64 (VmPtr, Size);\r
+        Size += sizeof (UINT64);\r
+      }\r
+    } else {\r
+      //\r
+      // Obsolete MOVBQ, MOVWQ, MOVDQ, and MOVNQ have 64-bit immediate index\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+  }\r
+  //\r
+  // Determine the size of the move, and create a mask for it so we can\r
+  // clear unused bits.\r
+  //\r
+  if ((OpcMasked == OPCODE_MOVBW) || (OpcMasked == OPCODE_MOVBD)) {\r
+    MoveSize  = DATA_SIZE_8;\r
+    DataMask  = 0xFF;\r
+  } else if ((OpcMasked == OPCODE_MOVWW) || (OpcMasked == OPCODE_MOVWD)) {\r
+    MoveSize  = DATA_SIZE_16;\r
+    DataMask  = 0xFFFF;\r
+  } else if ((OpcMasked == OPCODE_MOVDW) || (OpcMasked == OPCODE_MOVDD)) {\r
+    MoveSize  = DATA_SIZE_32;\r
+    DataMask  = 0xFFFFFFFF;\r
+  } else if ((OpcMasked == OPCODE_MOVQW) || (OpcMasked == OPCODE_MOVQD) || (OpcMasked == OPCODE_MOVQQ)) {\r
+    MoveSize  = DATA_SIZE_64;\r
+    DataMask  = (UINT64)~0;\r
+  } else if ((OpcMasked == OPCODE_MOVNW) || (OpcMasked == OPCODE_MOVND)) {\r
+    MoveSize  = DATA_SIZE_N;\r
+    DataMask  = (UINT64)~0 >> (64 - 8 * sizeof (UINTN));\r
+  } else {\r
+    //\r
+    // We were dispatched to this function and we don't recognize the opcode\r
+    //\r
+    EbcDebugSignalException (EXCEPT_EBC_UNDEFINED, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now get the source address\r
+  //\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect form @R2. Compute address of operand2\r
+    //\r
+    Source = (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2);\r
+    //\r
+    // Now get the data from the source. Always 0-extend and let the compiler\r
+    // sign-extend where required.\r
+    //\r
+    switch (MoveSize) {\r
+    case DATA_SIZE_8:\r
+      Data64 = (UINT64) (UINT8) VmReadMem8 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_16:\r
+      Data64 = (UINT64) (UINT16) VmReadMem16 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_32:\r
+      Data64 = (UINT64) (UINT32) VmReadMem32 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_64:\r
+      Data64 = (UINT64) VmReadMem64 (VmPtr, Source);\r
+      break;\r
+\r
+    case DATA_SIZE_N:\r
+      Data64 = (UINT64) (UINTN) VmReadMemN (VmPtr, Source);\r
+      break;\r
+\r
+    default:\r
+      //\r
+      // not reached\r
+      //\r
+      break;\r
+    }\r
+  } else {\r
+    //\r
+    // Not indirect source: MOVxx {@}Rx, Ry [Index]\r
+    //\r
+    Data64 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index64Op2;\r
+    //\r
+    // Did Operand2 have an index? If so, treat as two signed values since\r
+    // indexes are signed values.\r
+    //\r
+    if (Opcode & OPCODE_M_IMMED_OP2) {\r
+      //\r
+      // NOTE: need to find a way to fix this, most likely by changing the VM\r
+      // implementation to remove the stack gap. To do that, we'd need to\r
+      // allocate stack space for the VM and actually set the system\r
+      // stack pointer to the allocated buffer when the VM starts.\r
+      //\r
+      // Special case -- if someone took the address of a function parameter\r
+      // then we need to make sure it's not in the stack gap. We can identify\r
+      // this situation if (Operand2 register == 0) && (Operand2 is direct)\r
+      // && (Index applies to Operand2) && (Index > 0) && (Operand1 register != 0)\r
+      // Situations that to be aware of:\r
+      //   * stack adjustments at beginning and end of functions R0 = R0 += stacksize\r
+      //\r
+      if ((OPERAND2_REGNUM (Operands) == 0) &&\r
+          (!OPERAND2_INDIRECT (Operands)) &&\r
+          (Index64Op2 > 0) &&\r
+          (OPERAND1_REGNUM (Operands) == 0) &&\r
+          (OPERAND1_INDIRECT (Operands))\r
+          ) {\r
+        Data64 = (UINT64) ConvertStackAddr (VmPtr, (UINTN) (INT64) Data64);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Now write it back\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Reuse the Source variable to now be dest.\r
+    //\r
+    Source = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index64Op1);\r
+    //\r
+    // Do the write based on the size\r
+    //\r
+    switch (MoveSize) {\r
+    case DATA_SIZE_8:\r
+      VmWriteMem8 (VmPtr, Source, (UINT8) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_16:\r
+      VmWriteMem16 (VmPtr, Source, (UINT16) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_32:\r
+      VmWriteMem32 (VmPtr, Source, (UINT32) Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_64:\r
+      VmWriteMem64 (VmPtr, Source, Data64);\r
+      break;\r
+\r
+    case DATA_SIZE_N:\r
+      VmWriteMemN (VmPtr, Source, (UINTN) Data64);\r
+      break;\r
+\r
+    default:\r
+      //\r
+      // not reached\r
+      //\r
+      break;\r
+    }\r
+  } else {\r
+    //\r
+    // Operand1 direct.\r
+    // Make sure we didn't have an index on operand1.\r
+    //\r
+    if (Opcode & OPCODE_M_IMMED_OP1) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // Direct storage in register. Clear unused bits and store back to\r
+    // register.\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 & DataMask;\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteBREAK (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC BREAK instruction\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to current VM context\r
+\r
+Returns:\r
+\r
+  EFI_UNSUPPORTED\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  UINT8       Operands;\r
+  VOID        *EbcEntryPoint;\r
+  VOID        *Thunk;\r
+  UINT64      U64EbcEntryPoint;\r
+  INT32       Offset;\r
+\r
+  Operands = GETOPERANDS (VmPtr);\r
+  switch (Operands) {\r
+  //\r
+  // Runaway program break. Generate an exception and terminate\r
+  //\r
+  case 0:\r
+    EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    break;\r
+\r
+  //\r
+  // Get VM version -- return VM revision number in R7\r
+  //\r
+  case 1:\r
+    //\r
+    // Bits:\r
+    //  63-17 = 0\r
+    //  16-8  = Major version\r
+    //  7-0   = Minor version\r
+    //\r
+    VmPtr->R[7] = GetVmVersion ();\r
+    break;\r
+\r
+  //\r
+  // Debugger breakpoint\r
+  //\r
+  case 3:\r
+    VmPtr->StopFlags |= STOPFLAG_BREAKPOINT;\r
+    //\r
+    // See if someone has registered a handler\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_BREAKPOINT,\r
+      EXCEPTION_FLAG_NONE,\r
+      VmPtr\r
+      );\r
+    break;\r
+\r
+  //\r
+  // System call, which there are none, so NOP it.\r
+  //\r
+  case 4:\r
+    break;\r
+\r
+  //\r
+  // Create a thunk for EBC code. R7 points to a 32-bit (in a 64-bit slot)\r
+  // "offset from self" pointer to the EBC entry point.\r
+  // After we're done, *(UINT64 *)R7 will be the address of the new thunk.\r
+  //\r
+  case 5:\r
+    Offset            = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[7]);\r
+    U64EbcEntryPoint  = (UINT64) (VmPtr->R[7] + Offset + 4);\r
+    EbcEntryPoint     = (VOID *) (UINTN) U64EbcEntryPoint;\r
+\r
+    //\r
+    // Now create a new thunk\r
+    //\r
+    EbcCreateThunks (VmPtr->ImageHandle, EbcEntryPoint, &Thunk, 0);\r
+\r
+    //\r
+    // Finally replace the EBC entry point memory with the thunk address\r
+    //\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[7], (UINT64) (UINTN) Thunk);\r
+    break;\r
+\r
+  //\r
+  // Compiler setting version per value in R7\r
+  //\r
+  case 6:\r
+    VmPtr->CompilerVersion = (UINT32) VmPtr->R[7];\r
+    //\r
+    // Check compiler version against VM version?\r
+    //\r
+    break;\r
+\r
+  //\r
+  // Unhandled break code. Signal exception.\r
+  //\r
+  default:\r
+    EbcDebugSignalException (EXCEPT_EBC_BAD_BREAK, EXCEPTION_FLAG_FATAL, VmPtr);\r
+    break;\r
+  }\r
+  //\r
+  // Advance IP\r
+  //\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the JMP instruction\r
+\r
+Arguments:\r
+  VmPtr      - pointer to VM context\r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  JMP64{cs|cc} Immed64\r
+  JMP32{cs|cc} {@}R1 {Immed32|Index32}\r
+\r
+Encoding:\r
+  b0.7 -  immediate data present\r
+  b0.6 -  1 = 64 bit immediate data\r
+          0 = 32 bit immediate data\r
+  b1.7 -  1 = conditional\r
+  b1.6    1 = CS (condition set)\r
+          0 = CC (condition clear)\r
+  b1.4    1 = relative address\r
+          0 = absolute address\r
+  b1.3    1 = operand1 indirect\r
+  b1.2-0  operand 1\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   CompareSet;\r
+  UINT8   ConditionFlag;\r
+  UINT8   Size;\r
+  UINT8   Operand;\r
+  UINT64  Data64;\r
+  INT32   Index32;\r
+  UINTN   Addr;\r
+\r
+  Operand = GETOPERANDS (VmPtr);\r
+  Opcode  = GETOPCODE (VmPtr);\r
+\r
+  //\r
+  // Get instruction length from the opcode. The upper two bits are used here\r
+  // to index into the length array.\r
+  //\r
+  Size = mJMPLen[(Opcode >> 6) & 0x03];\r
+\r
+  //\r
+  // Decode instruction conditions\r
+  // If we haven't met the condition, then simply advance the IP and return.\r
+  //\r
+  CompareSet    = (UINT8) ((Operand & JMP_M_CS) ? 1 : 0);\r
+  ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);\r
+  if (Operand & CONDITION_M_CONDITIONAL) {\r
+    if (CompareSet != ConditionFlag) {\r
+      VmPtr->Ip += Size;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  //\r
+  // Check for 64-bit form and do it right away since it's the most\r
+  // straight-forward form.\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA64) {\r
+    //\r
+    // Double check for immediate-data, which is required. If not there,\r
+    // then signal an exception\r
+    //\r
+    if (!(Opcode & OPCODE_M_IMMDATA)) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_ERROR,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // 64-bit immediate data is full address. Read the immediate data,\r
+    // check for alignment, and jump absolute.\r
+    //\r
+    Data64 = VmReadImmed64 (VmPtr, 2);\r
+    if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    //\r
+    // Take jump -- relative or absolute\r
+    //\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Data64 + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) (UINTN) Data64;\r
+    }\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+  //\r
+  // 32-bit forms:\r
+  // Get the index if there is one. May be either an index, or an immediate\r
+  // offset depending on indirect operand.\r
+  //   JMP32 @R1 Index32 -- immediate data is an index\r
+  //   JMP32 R1 Immed32  -- immedate data is an offset\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operand)) {\r
+      Index32 = VmReadIndex32 (VmPtr, 2);\r
+    } else {\r
+      Index32 = VmReadImmed32 (VmPtr, 2);\r
+    }\r
+  } else {\r
+    Index32 = 0;\r
+  }\r
+  //\r
+  // Get the register data. If R == 0, then special case where it's ignored.\r
+  //\r
+  if (OPERAND1_REGNUM (Operand) == 0) {\r
+    Data64 = 0;\r
+  } else {\r
+    Data64 = OPERAND1_REGDATA (VmPtr, Operand);\r
+  }\r
+  //\r
+  // Decode the forms\r
+  //\r
+  if (OPERAND1_INDIRECT (Operand)) {\r
+    //\r
+    // Form: JMP32 @Rx {Index32}\r
+    //\r
+    Addr = VmReadMemN (VmPtr, (UINTN) Data64 + Index32);\r
+    if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Addr + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) Addr;\r
+    }\r
+  } else {\r
+    //\r
+    // Form: JMP32 Rx {Immed32}\r
+    //\r
+    Addr = (UINTN) (Data64 + Index32);\r
+    if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    if (Operand & JMP_M_RELATIVE) {\r
+      VmPtr->Ip += (UINTN) Addr + Size;\r
+    } else {\r
+      VmPtr->Ip = (VMIP) Addr;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteJMP8 (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC JMP8 instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  JMP8{cs|cc}  Offset/2\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 ConditionFlag;\r
+  UINT8 CompareSet;\r
+  INT8  Offset;\r
+\r
+  //\r
+  // Decode instruction.\r
+  //\r
+  Opcode        = GETOPCODE (VmPtr);\r
+  CompareSet    = (UINT8) ((Opcode & JMP_M_CS) ? 1 : 0);\r
+  ConditionFlag = (UINT8) VMFLAG_ISSET (VmPtr, VMFLAGS_CC);\r
+\r
+  //\r
+  // If we haven't met the condition, then simply advance the IP and return\r
+  //\r
+  if (Opcode & CONDITION_M_CONDITIONAL) {\r
+    if (CompareSet != ConditionFlag) {\r
+      VmPtr->Ip += 2;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  //\r
+  // Get the offset from the instruction stream. It's relative to the\r
+  // following instruction, and divided by 2.\r
+  //\r
+  Offset = VmReadImmed8 (VmPtr, 1);\r
+  //\r
+  // Want to check for offset == -2 and then raise an exception?\r
+  //\r
+  VmPtr->Ip += (Offset * 2) + 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVI (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVI \r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVI[b|w|d|q][w|d|q] {@}R1 {Index16}, ImmData16|32|64\r
+\r
+  First variable character specifies the move size\r
+  Second variable character specifies size of the immediate data\r
+\r
+  Sign-extend the immediate data to the size of the operation, and zero-extend\r
+  if storing to a register.\r
+\r
+  Operand1 direct with index/immed is invalid.\r
+    \r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT64   ImmData64;\r
+  UINT64  Op1;\r
+  UINT64  Mask64;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Extract the immediate data. Sign-extend always.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmData64 = (INT64) (INT16) VmReadImmed16 (VmPtr, Size);\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmData64 = (INT64) (INT32) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmData64 = (INT64) VmReadImmed64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now write back the result\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Operand1 direct. Make sure it didn't have an index.\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+    //\r
+    // Writing directly to a register. Clear unused bits.\r
+    //\r
+    if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {\r
+      Mask64 = 0x000000FF;\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {\r
+      Mask64 = 0x0000FFFF;\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {\r
+      Mask64 = 0x00000000FFFFFFFF;\r
+    } else {\r
+      Mask64 = (UINT64)~0;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmData64 & Mask64;\r
+  } else {\r
+    //\r
+    // Get the address then write back based on size of the move\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH8) {\r
+      VmWriteMem8 (VmPtr, (UINTN) Op1, (UINT8) ImmData64);\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH16) {\r
+      VmWriteMem16 (VmPtr, (UINTN) Op1, (UINT16) ImmData64);\r
+    } else if ((Operands & MOVI_M_MOVEWIDTH) == MOVI_MOVEWIDTH32) {\r
+      VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) ImmData64);\r
+    } else {\r
+      VmWriteMem64 (VmPtr, (UINTN) Op1, ImmData64);\r
+    }\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVIn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOV immediate natural. This instruction moves an immediate\r
+  index value into a register or memory location.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVIn[w|d|q] {@}R1 {Index16}, Index16|32|64\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT16   ImmedIndex16;\r
+  INT32   ImmedIndex32;\r
+  INT64   ImmedIndex64;\r
+  UINT64  Op1;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the operand1 index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Extract the immediate data and convert to a 64-bit index.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmedIndex16  = VmReadIndex16 (VmPtr, Size);\r
+    ImmedIndex64  = (INT64) ImmedIndex16;\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmedIndex32  = VmReadIndex32 (VmPtr, Size);\r
+    ImmedIndex64  = (INT64) ImmedIndex32;\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmedIndex64 = VmReadIndex64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Now write back the result\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Check for MOVIn R1 Index16, Immed (not indirect, with index), which\r
+    // is illegal\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = ImmedIndex64;\r
+  } else {\r
+    //\r
+    // Get the address\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    VmWriteMemN (VmPtr, (UINTN) Op1, (INTN) ImmedIndex64);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVREL (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVREL instruction.\r
+  Dest <- Ip + ImmData\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVREL[w|d|q] {@}R1 {Index16}, ImmData16|32|64\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  INT64   ImmData64;\r
+  UINT64  Op1;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operands byte so we can get R1 and R2\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get the Operand 1 index (16-bit) if present\r
+  //\r
+  if (Operands & MOVI_M_IMMDATA) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size    = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Get the immediate data.\r
+  //\r
+  if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH16) {\r
+    ImmData64 = (INT64) VmReadImmed16 (VmPtr, Size);\r
+    Size += 2;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH32) {\r
+    ImmData64 = (INT64) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else if ((Opcode & MOVI_M_DATAWIDTH) == MOVI_DATAWIDTH64) {\r
+    ImmData64 = VmReadImmed64 (VmPtr, Size);\r
+    Size += 8;\r
+  } else {\r
+    //\r
+    // Invalid encoding\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Compute the value and write back the result\r
+  //\r
+  Op2 = (UINT64) ((INT64) ((UINT64) (UINTN) VmPtr->Ip) + (INT64) ImmData64 + Size);\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Check for illegal combination of operand1 direct with immediate data\r
+    //\r
+    if (Operands & MOVI_M_IMMDATA) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (VM_REGISTER) Op2;\r
+  } else {\r
+    //\r
+    // Get the address = [Rx] + Index16\r
+    // Write back the result. Always a natural size write, since\r
+    // we're talking addresses here.\r
+    //\r
+    Op1 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    VmWriteMemN (VmPtr, (UINTN) Op1, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnw (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVsnw instruction. This instruction loads a signed \r
+  natural value from memory or register to another memory or register. On\r
+  32-bit machines, the value gets sign-extended to 64 bits if the destination\r
+  is a register.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVsnw {@}R1 {Index16}, {@}R2 {Index16|Immed16}\r
+\r
+  0:7 1=>operand1 index present\r
+  0:6 1=>operand2 index present\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Op1Index;\r
+  INT16   Op2Index;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operand bytes\r
+  //\r
+  Opcode              = GETOPCODE (VmPtr);\r
+  Operands            = GETOPERANDS (VmPtr);\r
+\r
+  Op1Index            = Op2Index = 0;\r
+\r
+  //\r
+  // Get the indexes if present.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & OPCODE_M_IMMED_OP1) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Op1Index = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      //\r
+      // Illegal form operand1 direct with index:  MOVsnw R1 Index16, {@}R2\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    Size += sizeof (UINT16);\r
+  }\r
+\r
+  if (Opcode & OPCODE_M_IMMED_OP2) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Op2Index = VmReadIndex16 (VmPtr, Size);\r
+    } else {\r
+      Op2Index = VmReadImmed16 (VmPtr, Size);\r
+    }\r
+\r
+    Size += sizeof (UINT16);\r
+  }\r
+  //\r
+  // Get the data from the source.\r
+  //\r
+  Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Now write back the result.\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+  } else {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteMOVsnd (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MOVsnw instruction. This instruction loads a signed \r
+  natural value from memory or register to another memory or register. On\r
+  32-bit machines, the value gets sign-extended to 64 bits if the destination\r
+  is a register.\r
+\r
+Arguments:\r
+\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+\r
+  MOVsnd {@}R1 {Indx32}, {@}R2 {Index32|Immed32}\r
+\r
+  0:7 1=>operand1 index present\r
+  0:6 1=>operand2 index present\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT32   Op1Index;\r
+  INT32   Op2Index;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get the opcode and operand bytes\r
+  //\r
+  Opcode              = GETOPCODE (VmPtr);\r
+  Operands            = GETOPERANDS (VmPtr);\r
+\r
+  Op1Index            = Op2Index = 0;\r
+\r
+  //\r
+  // Get the indexes if present.\r
+  //\r
+  Size = 2;\r
+  if (Opcode & OPCODE_M_IMMED_OP1) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Op1Index = VmReadIndex32 (VmPtr, 2);\r
+    } else {\r
+      //\r
+      // Illegal form operand1 direct with index:  MOVsnd R1 Index16,..\r
+      //\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+\r
+    Size += sizeof (UINT32);\r
+  }\r
+\r
+  if (Opcode & OPCODE_M_IMMED_OP2) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Op2Index = VmReadIndex32 (VmPtr, Size);\r
+    } else {\r
+      Op2Index = VmReadImmed32 (VmPtr, Size);\r
+    }\r
+\r
+    Size += sizeof (UINT32);\r
+  }\r
+  //\r
+  // Get the data from the source.\r
+  //\r
+  Op2 = (INT64) ((INTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Op2Index));\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    Op2 = (INT64) (INTN) VmReadMemN (VmPtr, (UINTN) Op2);\r
+  }\r
+  //\r
+  // Now write back the result.\r
+  //\r
+  if (!OPERAND1_INDIRECT (Operands)) {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+  } else {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Op1Index), (UINTN) Op2);\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSHn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC PUSHn instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   PUSHn {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT16 Index16;\r
+  UINTN DataN;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get index if present\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data to push\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    DataN = VmReadMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+  } else {\r
+    DataN = (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16);\r
+  }\r
+  //\r
+  // Adjust the stack down.\r
+  //\r
+  VmPtr->R[0] -= sizeof (UINTN);\r
+  VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], DataN);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePUSH (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC PUSH instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   PUSH[32|64] {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT32  Data32;\r
+  UINT64  Data64;\r
+  INT16   Index16;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate index if present, then advance the IP.\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data to push\r
+  //\r
+  if (Opcode & PUSHPOP_M_64) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Data64 = VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      Data64 = (UINT64) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    }\r
+    //\r
+    // Adjust the stack down, then write back the data\r
+    //\r
+    VmPtr->R[0] -= sizeof (UINT64);\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], Data64);\r
+  } else {\r
+    //\r
+    // 32-bit data\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Data32 = VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      Data32 = (UINT32) VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16;\r
+    }\r
+    //\r
+    // Adjust the stack down and write the data\r
+    //\r
+    VmPtr->R[0] -= sizeof (UINT32);\r
+    VmWriteMem32 (VmPtr, (UINTN) VmPtr->R[0], Data32);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOPn (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC POPn instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   POPn {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT16 Index16;\r
+  UINTN DataN;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate data if present, and advance the IP\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Read the data off the stack, then adjust the stack pointer\r
+  //\r
+  DataN = VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);\r
+  VmPtr->R[0] += sizeof (UINTN);\r
+  //\r
+  // Do the write-back\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    VmWriteMemN (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), DataN);\r
+  } else {\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) (UINT64) ((UINTN) DataN + Index16);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecutePOP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC POP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  POP {@}R1 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  INT16   Index16;\r
+  INT32   Data32;\r
+  UINT64  Data64;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get immediate data if present, and advance the IP.\r
+  //\r
+  if (Opcode & PUSHPOP_M_IMMDATA) {\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    VmPtr->Ip += 4;\r
+  } else {\r
+    Index16 = 0;\r
+    VmPtr->Ip += 2;\r
+  }\r
+  //\r
+  // Get the data off the stack, then write it to the appropriate location\r
+  //\r
+  if (Opcode & PUSHPOP_M_64) {\r
+    //\r
+    // Read the data off the stack, then adjust the stack pointer\r
+    //\r
+    Data64 = VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += sizeof (UINT64);\r
+    //\r
+    // Do the write-back\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      VmWriteMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data64);\r
+    } else {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] = Data64 + Index16;\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit pop. Read it off the stack and adjust the stack pointer\r
+    //\r
+    Data32 = (INT32) VmReadMem32 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += sizeof (UINT32);\r
+    //\r
+    // Do the write-back\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      VmWriteMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND1_REGNUM (Operands)] + Index16), Data32);\r
+    } else {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] = (INT64) Data32 + Index16;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCALL (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Implements the EBC CALL instruction.\r
+\r
+  Instruction format:  \r
+\r
+    CALL64 Immed64\r
+    CALL32 {@}R1 {Immed32|Index32}\r
+    CALLEX64 Immed64\r
+    CALLEX16 {@}R1 {Immed32}\r
+\r
+  If Rx == R0, then it's a PC relative call to PC = PC + imm32.\r
+  \r
+Arguments:\r
+  VmPtr - pointer to a VM context.\r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+--*/\r
+{\r
+  UINT8 Opcode;\r
+  UINT8 Operands;\r
+  INT32 Immed32;\r
+  UINT8 Size;\r
+  INT64 Immed64;\r
+  VOID  *FramePtr;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Assign these as well to avoid compiler warnings\r
+  //\r
+  Immed64   = 0;\r
+  Immed32   = 0;\r
+\r
+  FramePtr  = VmPtr->FramePtr;\r
+  //\r
+  // Determine the instruction size, and get immediate data if present\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (Opcode & OPCODE_M_IMMDATA64) {\r
+      Immed64 = VmReadImmed64 (VmPtr, 2);\r
+      Size    = 10;\r
+    } else {\r
+      //\r
+      // If register operand is indirect, then the immediate data is an index\r
+      //\r
+      if (OPERAND1_INDIRECT (Operands)) {\r
+        Immed32 = VmReadIndex32 (VmPtr, 2);\r
+      } else {\r
+        Immed32 = VmReadImmed32 (VmPtr, 2);\r
+      }\r
+\r
+      Size = 6;\r
+    }\r
+  } else {\r
+    Size = 2;\r
+  }\r
+  //\r
+  // If it's a call to EBC, adjust the stack pointer down 16 bytes and\r
+  // put our return address and frame pointer on the VM stack.\r
+  //\r
+  if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
+  }\r
+  //\r
+  // If 64-bit data, then absolute jump only\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA64) {\r
+    //\r
+    // Native or EBC call?\r
+    //\r
+    if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\r
+      VmPtr->Ip = (VMIP) (UINTN) Immed64;\r
+    } else {\r
+      //\r
+      // Call external function, get the return value, and advance the IP\r
+      //\r
+      EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);\r
+    }\r
+  } else {\r
+    //\r
+    // Get the register data. If operand1 == 0, then ignore register and\r
+    // take immediate data as relative or absolute address.\r
+    // Compiler should take care of upper bits if 32-bit machine.\r
+    //\r
+    if (OPERAND1_REGNUM (Operands) != 0) {\r
+      Immed64 = (UINT64) (UINTN) VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+    }\r
+    //\r
+    // Get final address\r
+    //\r
+    if (OPERAND1_INDIRECT (Operands)) {\r
+      Immed64 = (INT64) (UINT64) (UINTN) VmReadMemN (VmPtr, (UINTN) (Immed64 + Immed32));\r
+    } else {\r
+      Immed64 += Immed32;\r
+    }\r
+    //\r
+    // Now determine if external call, and then if relative or absolute\r
+    //\r
+    if ((Operands & OPERAND_M_NATIVE_CALL) == 0) {\r
+      //\r
+      // EBC call. Relative or absolute? If relative, then it's relative to the\r
+      // start of the next instruction.\r
+      //\r
+      if (Operands & OPERAND_M_RELATIVE_ADDR) {\r
+        VmPtr->Ip += Immed64 + Size;\r
+      } else {\r
+        VmPtr->Ip = (VMIP) (UINTN) Immed64;\r
+      }\r
+    } else {\r
+      //\r
+      // Native call. Relative or absolute?\r
+      //\r
+      if (Operands & OPERAND_M_RELATIVE_ADDR) {\r
+        EbcLLCALLEX (VmPtr, (UINTN) (Immed64 + VmPtr->Ip + Size), (UINTN) VmPtr->R[0], FramePtr, Size);\r
+      } else {\r
+        if (VmPtr->StopFlags & STOPFLAG_BREAK_ON_CALLEX) {\r
+          CpuBreakpoint ();\r
+        }\r
+\r
+        EbcLLCALLEX (VmPtr, (UINTN) Immed64, (UINTN) VmPtr->R[0], FramePtr, Size);\r
+      }\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteRET (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC RET instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   RET\r
+\r
+--*/\r
+{\r
+  //\r
+  // If we're at the top of the stack, then simply set the done\r
+  // flag and return\r
+  //\r
+  if (VmPtr->StackRetAddr == (UINT64) VmPtr->R[0]) {\r
+    VmPtr->StopFlags |= STOPFLAG_APP_DONE;\r
+  } else {\r
+    //\r
+    // Pull the return address off the VM app's stack and set the IP\r
+    // to it\r
+    //\r
+    if (!IS_ALIGNED ((UINTN) VmPtr->R[0], sizeof (UINT16))) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_ALIGNMENT_CHECK,\r
+        EXCEPTION_FLAG_FATAL,\r
+        VmPtr\r
+        );\r
+    }\r
+    //\r
+    // Restore the IP and frame pointer from the stack\r
+    //\r
+    VmPtr->Ip = (VMIP) (UINTN) VmReadMem64 (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += 8;\r
+    VmPtr->FramePtr = (VOID *) VmReadMemN (VmPtr, (UINTN) VmPtr->R[0]);\r
+    VmPtr->R[0] += 8;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC CMP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   CMP[32|64][eq|lte|gte|ulte|ugte] R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT16   Index16;\r
+  UINT32  Flag;\r
+  INT64   Op2;\r
+  INT64   Op1;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+  //\r
+  // Get the register data we're going to compare to\r
+  //\r
+  Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  //\r
+  // Get immediate data\r
+  //\r
+  if (Opcode & OPCODE_M_IMMDATA) {\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    Size = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Now get Op2\r
+  //\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    if (Opcode & OPCODE_M_64BIT) {\r
+      Op2 = (INT64) VmReadMem64 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16));\r
+    } else {\r
+      //\r
+      // 32-bit operations. 0-extend the values for all cases.\r
+      //\r
+      Op2 = (INT64) (UINT64) ((UINT32) VmReadMem32 (VmPtr, (UINTN) (VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16)));\r
+    }\r
+  } else {\r
+    Op2 = VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;\r
+  }\r
+  //\r
+  // Now do the compare\r
+  //\r
+  Flag = 0;\r
+  if (Opcode & OPCODE_M_64BIT) {\r
+    //\r
+    // 64-bit compares\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPEQ:\r
+      if (Op1 == Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPLTE:\r
+      if (Op1 <= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPGTE:\r
+      if (Op1 >= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPULTE:\r
+      if ((UINT64) Op1 <= (UINT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPUGTE:\r
+      if ((UINT64) Op1 >= (UINT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit compares\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPEQ:\r
+      if ((INT32) Op1 == (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPLTE:\r
+      if ((INT32) Op1 <= (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPGTE:\r
+      if ((INT32) Op1 >= (INT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPULTE:\r
+      if ((UINT32) Op1 <= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPUGTE:\r
+      if ((UINT32) Op1 >= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  }\r
+  //\r
+  // Now set the flag accordingly for the comparison\r
+  //\r
+  if (Flag) {\r
+    VMFLAG_SET (VmPtr, VMFLAGS_CC);\r
+  } else {\r
+    VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);\r
+  }\r
+  //\r
+  // Advance the IP\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteCMPI (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC CMPI instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   CMPI[32|64]{w|d}[eq|lte|gte|ulte|ugte] {@}Rx {Index16}, Immed16|Immed32\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  INT64   Op1;\r
+  INT64   Op2;\r
+  INT16   Index16;\r
+  UINT32  Flag;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Get operand1 index if present\r
+  //\r
+  Size = 2;\r
+  if (Operands & OPERAND_M_CMPI_INDEX) {\r
+    Index16 = VmReadIndex16 (VmPtr, 2);\r
+    Size += 2;\r
+  } else {\r
+    Index16 = 0;\r
+  }\r
+  //\r
+  // Get operand1 data we're going to compare to\r
+  //\r
+  Op1 = (INT64) VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect operand1. Fetch 32 or 64-bit value based on compare size.\r
+    //\r
+    if (Opcode & OPCODE_M_CMPI64) {\r
+      Op1 = (INT64) VmReadMem64 (VmPtr, (UINTN) Op1 + Index16);\r
+    } else {\r
+      Op1 = (INT64) VmReadMem32 (VmPtr, (UINTN) Op1 + Index16);\r
+    }\r
+  } else {\r
+    //\r
+    // Better not have been an index with direct. That is, CMPI R1 Index,...\r
+    // is illegal.\r
+    //\r
+    if (Operands & OPERAND_M_CMPI_INDEX) {\r
+      EbcDebugSignalException (\r
+        EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+        EXCEPTION_FLAG_ERROR,\r
+        VmPtr\r
+        );\r
+      VmPtr->Ip += Size;\r
+      return EFI_UNSUPPORTED;\r
+    }\r
+  }\r
+  //\r
+  // Get immediate data -- 16- or 32-bit sign extended\r
+  //\r
+  if (Opcode & OPCODE_M_CMPI32_DATA) {\r
+    Op2 = (INT64) VmReadImmed32 (VmPtr, Size);\r
+    Size += 4;\r
+  } else {\r
+    //\r
+    // 16-bit immediate data. Sign extend always.\r
+    //\r
+    Op2 = (INT64) ((INT16) VmReadImmed16 (VmPtr, Size));\r
+    Size += 2;\r
+  }\r
+  //\r
+  // Now do the compare\r
+  //\r
+  Flag = 0;\r
+  if (Opcode & OPCODE_M_CMPI64) {\r
+    //\r
+    // 64 bit comparison\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPIEQ:\r
+      if (Op1 == (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPILTE:\r
+      if (Op1 <= (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIGTE:\r
+      if (Op1 >= (INT64) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIULTE:\r
+      if ((UINT64) Op1 <= (UINT64) ((UINT32) Op2)) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIUGTE:\r
+      if ((UINT64) Op1 >= (UINT64) ((UINT32) Op2)) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  } else {\r
+    //\r
+    // 32-bit comparisons\r
+    //\r
+    switch (Opcode & OPCODE_M_OPCODE) {\r
+    case OPCODE_CMPIEQ:\r
+      if ((INT32) Op1 == Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPILTE:\r
+      if ((INT32) Op1 <= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIGTE:\r
+      if ((INT32) Op1 >= Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIULTE:\r
+      if ((UINT32) Op1 <= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    case OPCODE_CMPIUGTE:\r
+      if ((UINT32) Op1 >= (UINT32) Op2) {\r
+        Flag = 1;\r
+      }\r
+      break;\r
+\r
+    default:\r
+      ASSERT (0);\r
+    }\r
+  }\r
+  //\r
+  // Now set the flag accordingly for the comparison\r
+  //\r
+  if (Flag) {\r
+    VMFLAG_SET (VmPtr, VMFLAGS_CC);\r
+  } else {\r
+    VMFLAG_CLEAR (VmPtr, VMFLAGS_CC);\r
+  }\r
+  //\r
+  // Advance the IP\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNOT (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT64         Op1,\r
+  IN UINT64         Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC NOT instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  ~Op2\r
+\r
+Instruction syntax:\r
+  NOT[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+  \r
+--*/\r
+{\r
+  return ~Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteNEG (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC NEG instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op2 * -1\r
+\r
+Instruction syntax:\r
+  NEG[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return ~Op2 + 1;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteADD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC ADD instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 + Op2\r
+\r
+Instruction syntax:\r
+   ADD[32|64] {@}R1, {@}R2 {Index16}\r
+\r
+--*/\r
+{\r
+  return Op1 + Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSUB (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC SUB instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 - Op2\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  SUB[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return (UINT64) ((INT64) ((INT64) Op1 - (INT64) Op2));\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 - (INT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMUL (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC MUL instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 * Op2\r
+\r
+Instruction syntax:\r
+  MUL[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return MultS64x64 ((INT64)Op1, (INT64)Op2);\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 * (INT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMULU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MULU instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (unsigned)Op1 * (unsigned)Op2 \r
+\r
+Instruction syntax:\r
+  MULU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return MultU64x64 (Op1, Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) Op1 * (UINT32) Op2);\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIV (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC DIV instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1/Op2\r
+\r
+Instruction syntax:\r
+  DIV[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  INT64   Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+\r
+    return 0;\r
+  } else {\r
+    if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+      return (UINT64) (DivS64x64Remainder (Op1, Op2, &Remainder));\r
+    } else {\r
+      return (UINT64) ((INT64) ((INT32) Op1 / (INT32) Op2));\r
+    }\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteDIVU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC DIVU instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (unsigned)Op1 / (unsigned)Op2\r
+\r
+Instruction syntax:\r
+  DIVU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  UINT64  Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    //\r
+    // Get the destination register\r
+    //\r
+    if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+      return (UINT64) (DivU64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder));\r
+    } else {\r
+      return (UINT64) ((UINT32) Op1 / (UINT32) Op2);\r
+    }\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMOD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MOD instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 MODULUS Op2\r
+\r
+Instruction syntax:\r
+  MOD[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  INT64   Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    DivS64x64Remainder ((INT64)Op1, (INT64)Op2, &Remainder);\r
+    return Remainder;\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteMODU (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC MODU instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 UNSIGNED_MODULUS Op2\r
+\r
+Instruction syntax:\r
+  MODU[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+  \r
+--*/\r
+{\r
+  UINT64  Remainder;\r
+\r
+  //\r
+  // Check for divide-by-0\r
+  //\r
+  if (Op2 == 0) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_DIVIDE_ERROR,\r
+      EXCEPTION_FLAG_FATAL,\r
+      VmPtr\r
+      );\r
+    return 0;\r
+  } else {\r
+    DivU64x64Remainder (Op1, Op2, &Remainder);\r
+    return Remainder;\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteAND (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC AND instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 AND Op2\r
+\r
+Instruction syntax:\r
+  AND[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 & Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteOR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC OR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 OR Op2\r
+\r
+Instruction syntax:\r
+  OR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 | Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteXOR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC XOR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 XOR Op2\r
+\r
+Instruction syntax:\r
+  XOR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  return Op1 ^ Op2;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHL (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  Execute the EBC SHL shift left instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 << Op2\r
+\r
+Instruction syntax:\r
+  SHL[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return LShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) ((UINT32) Op1 << (UINT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteSHR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC SHR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 >> Op2  (unsigned operands)\r
+\r
+Instruction syntax:\r
+  SHR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return RShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((UINT32) Op1 >> (UINT32) Op2);\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteASHR (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC ASHR instruction\r
+\r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  Op1 >> Op2 (signed)\r
+\r
+Instruction syntax:\r
+  ASHR[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+--*/\r
+{\r
+  if (*VmPtr->Ip & DATAMANIP_M_64) {\r
+    return ARShiftU64 (Op1, (UINTN)Op2);\r
+  } else {\r
+    return (UINT64) ((INT64) ((INT32) Op1 >> (UINT32) Op2));\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDB (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDB instruction to sign-extend a byte value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT8)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDB[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT8  Data8;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to byte, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data8   = (INT8) Op2;\r
+  Data64  = (INT64) Data8;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDW (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDW instruction to sign-extend a 16-bit value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT16)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDW[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT16 Data16;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to word, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data16  = (INT16) Op2;\r
+  Data64  = (INT64) Data16;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+//\r
+// Execute the EBC EXTNDD instruction.\r
+//\r
+// Format: EXTNDD {@}Rx, {@}Ry [Index16|Immed16]\r
+//         EXTNDD Dest, Source\r
+//\r
+// Operation:  Dest <- SignExtended((DWORD)Source))\r
+//\r
+STATIC\r
+UINT64\r
+ExecuteEXTNDD (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINT64       Op1,\r
+  IN UINT64       Op2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC EXTNDD instruction to sign-extend a 32-bit value.\r
+  \r
+Arguments:\r
+  VmPtr     - pointer to a VM context  \r
+  Op1       - Operand 1 from the instruction \r
+  Op2       - Operand 2 from the instruction\r
+\r
+Returns:\r
+  (INT64)(INT32)Op2\r
+\r
+Instruction syntax:\r
+  EXTNDD[32|64] {@}R1, {@}R2 {Index16|Immed16}\r
+\r
+  \r
+--*/\r
+{\r
+  INT32 Data32;\r
+  INT64 Data64;\r
+  //\r
+  // Convert to 32-bit value, then return as 64-bit signed value to let compiler\r
+  // sign-extend the value\r
+  //\r
+  Data32  = (INT32) Op2;\r
+  Data64  = (INT64) Data32;\r
+\r
+  return (UINT64) Data64;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSignedDataManip (\r
+  IN VM_CONTEXT   *VmPtr\r
+  )\r
+{\r
+  //\r
+  // Just call the data manipulation function with a flag indicating this\r
+  // is a signed operation.\r
+  //\r
+  return ExecuteDataManip (VmPtr, TRUE);\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteUnsignedDataManip (\r
+  IN VM_CONTEXT   *VmPtr\r
+  )\r
+{\r
+  //\r
+  // Just call the data manipulation function with a flag indicating this\r
+  // is not a signed operation.\r
+  //\r
+  return ExecuteDataManip (VmPtr, FALSE);\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteDataManip (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN BOOLEAN      IsSignedOp\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute all the EBC data manipulation instructions. \r
+  Since the EBC data manipulation instructions all have the same basic form, \r
+  they can share the code that does the fetch of operands and the write-back\r
+  of the result. This function performs the fetch of the operands (even if\r
+  both are not needed to be fetched, like NOT instruction), dispatches to the\r
+  appropriate subfunction, then writes back the returned result.\r
+\r
+Arguments:\r
+  VmPtr - pointer to VM context\r
+\r
+Returns:\r
+  Standard EBC status\r
+\r
+Format:  \r
+  INSTRUCITON[32|64] {@}R1, {@}R2 {Immed16|Index16}\r
+\r
+--*/\r
+{\r
+  UINT8   Opcode;\r
+  INT16   Index16;\r
+  UINT8   Operands;\r
+  UINT8   Size;\r
+  UINT64  Op1;\r
+  UINT64  Op2;\r
+\r
+  //\r
+  // Get opcode and operands\r
+  //\r
+  Opcode    = GETOPCODE (VmPtr);\r
+  Operands  = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Determine if we have immediate data by the opcode\r
+  //\r
+  if (Opcode & DATAMANIP_M_IMMDATA) {\r
+    //\r
+    // Index16 if Ry is indirect, or Immed16 if Ry direct.\r
+    //\r
+    if (OPERAND2_INDIRECT (Operands)) {\r
+      Index16 = VmReadIndex16 (VmPtr, 2);\r
+    } else {\r
+      Index16 = VmReadImmed16 (VmPtr, 2);\r
+    }\r
+\r
+    Size = 4;\r
+  } else {\r
+    Index16 = 0;\r
+    Size    = 2;\r
+  }\r
+  //\r
+  // Now get operand2 (source). It's of format {@}R2 {Index16|Immed16}\r
+  //\r
+  Op2 = (UINT64) VmPtr->R[OPERAND2_REGNUM (Operands)] + Index16;\r
+  if (OPERAND2_INDIRECT (Operands)) {\r
+    //\r
+    // Indirect form: @R2 Index16. Fetch as 32- or 64-bit data\r
+    //\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      Op2 = VmReadMem64 (VmPtr, (UINTN) Op2);\r
+    } else {\r
+      //\r
+      // Read as signed value where appropriate.\r
+      //\r
+      if (IsSignedOp) {\r
+        Op2 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op2));\r
+      } else {\r
+        Op2 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op2);\r
+      }\r
+    }\r
+  } else {\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      if (IsSignedOp) {\r
+        Op2 = (UINT64) (INT64) ((INT32) Op2);\r
+      } else {\r
+        Op2 = (UINT64) ((UINT32) Op2);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Get operand1 (destination and sometimes also an actual operand)\r
+  // of form {@}R1\r
+  //\r
+  Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      Op1 = VmReadMem64 (VmPtr, (UINTN) Op1);\r
+    } else {\r
+      if (IsSignedOp) {\r
+        Op1 = (UINT64) (INT64) ((INT32) VmReadMem32 (VmPtr, (UINTN) Op1));\r
+      } else {\r
+        Op1 = (UINT64) VmReadMem32 (VmPtr, (UINTN) Op1);\r
+      }\r
+    }\r
+  } else {\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      if (IsSignedOp) {\r
+        Op1 = (UINT64) (INT64) ((INT32) Op1);\r
+      } else {\r
+        Op1 = (UINT64) ((UINT32) Op1);\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Dispatch to the computation function\r
+  //\r
+  if (((Opcode & OPCODE_M_OPCODE) - OPCODE_NOT) >=\r
+        (sizeof (mDataManipDispatchTable) / sizeof (mDataManipDispatchTable[0]))\r
+        ) {\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INVALID_OPCODE,\r
+      EXCEPTION_FLAG_ERROR,\r
+      VmPtr\r
+      );\r
+    //\r
+    // Advance and return\r
+    //\r
+    VmPtr->Ip += Size;\r
+    return EFI_UNSUPPORTED;\r
+  } else {\r
+    Op2 = mDataManipDispatchTable[(Opcode & OPCODE_M_OPCODE) - OPCODE_NOT](VmPtr, Op1, Op2);\r
+  }\r
+  //\r
+  // Write back the result.\r
+  //\r
+  if (OPERAND1_INDIRECT (Operands)) {\r
+    Op1 = VmPtr->R[OPERAND1_REGNUM (Operands)];\r
+    if (Opcode & DATAMANIP_M_64) {\r
+      VmWriteMem64 (VmPtr, (UINTN) Op1, Op2);\r
+    } else {\r
+      VmWriteMem32 (VmPtr, (UINTN) Op1, (UINT32) Op2);\r
+    }\r
+  } else {\r
+    //\r
+    // Storage back to a register. Write back, clearing upper bits (as per\r
+    // the specification) if 32-bit operation.\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = Op2;\r
+    if ((Opcode & DATAMANIP_M_64) == 0) {\r
+      VmPtr->R[OPERAND1_REGNUM (Operands)] &= 0xFFFFFFFF;\r
+    }\r
+  }\r
+  //\r
+  // Advance the instruction pointer\r
+  //\r
+  VmPtr->Ip += Size;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteLOADSP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC LOADSP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+  LOADSP  SP1, R2\r
+\r
+--*/\r
+{\r
+  UINT8 Operands;\r
+\r
+  //\r
+  // Get the operands\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Do the operation\r
+  //\r
+  switch (OPERAND1_REGNUM (Operands)) {\r
+  //\r
+  // Set flags\r
+  //\r
+  case 0:\r
+    //\r
+    // Spec states that this instruction will not modify reserved bits in\r
+    // the flags register.\r
+    //\r
+    VmPtr->Flags = (VmPtr->Flags &~VMFLAGS_ALL_VALID) | (VmPtr->R[OPERAND2_REGNUM (Operands)] & VMFLAGS_ALL_VALID);\r
+    break;\r
+\r
+  default:\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+    VmPtr->Ip += 2;\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ExecuteSTORESP (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Execute the EBC STORESP instruction\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+\r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+Instruction syntax:\r
+   STORESP  Rx, FLAGS|IP\r
+\r
+--*/\r
+{\r
+  UINT8 Operands;\r
+\r
+  //\r
+  // Get the operands\r
+  //\r
+  Operands = GETOPERANDS (VmPtr);\r
+\r
+  //\r
+  // Do the operation\r
+  //\r
+  switch (OPERAND2_REGNUM (Operands)) {\r
+  //\r
+  // Get flags\r
+  //\r
+  case 0:\r
+    //\r
+    // Retrieve the value in the flags register, then clear reserved bits\r
+    //\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (VmPtr->Flags & VMFLAGS_ALL_VALID);\r
+    break;\r
+\r
+  //\r
+  // Get IP -- address of following instruction\r
+  //\r
+  case 1:\r
+    VmPtr->R[OPERAND1_REGNUM (Operands)] = (UINT64) (UINTN) VmPtr->Ip + 2;\r
+    break;\r
+\r
+  default:\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_INSTRUCTION_ENCODING,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+    VmPtr->Ip += 2;\r
+    return EFI_UNSUPPORTED;\r
+    break;\r
+  }\r
+\r
+  VmPtr->Ip += 2;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+INT16\r
+VmReadIndex16 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 16-bit index to determine the offset. Given an index value:\r
+\r
+    b15     - sign bit\r
+    b14:12  - number of bits in this index assigned to natural units (=a)\r
+    ba:11   - constant units = C\r
+    b0:a    - natural units = N\r
+  \r
+  Given this info, the offset can be computed by:\r
+    offset = sign_bit * (C + N * sizeof(UINTN))\r
+\r
+  Max offset is achieved with index = 0x7FFF giving an offset of\r
+  0x27B (32-bit machine) or 0x477 (64-bit machine).\r
+  Min offset is achieved with index = \r
+  \r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 16-bit index to decode\r
+\r
+Returns:\r
+  The decoded offset.\r
+  \r
+--*/\r
+{\r
+  UINT16  Index;\r
+  INT16   Offset;\r
+  INT16   C;\r
+  INT16   N;\r
+  INT16   NBits;\r
+  INT16   Mask;\r
+\r
+  //\r
+  // First read the index from the code stream\r
+  //\r
+  Index = VmReadCode16 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = (INT16) ((Index & 0x7000) >> 12);\r
+\r
+  //\r
+  // Scale it for 16-bit indexes\r
+  //\r
+  NBits *= 2;\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (INT16) ((INT16)~0 << NBits);\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = (INT16) (Index &~Mask);\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = (INT16) (((Index &~0xF000) & Mask) >> NBits);\r
+\r
+  Offset  = (INT16) (N * sizeof (UINTN) + C);\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x8000) {\r
+    //\r
+    // Do it the hard way to work around a bogus compiler warning\r
+    //\r
+    // Offset = -1 * Offset;\r
+    //\r
+    Offset = (INT16) ((INT32) Offset * -1);\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+INT32\r
+VmReadIndex32 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 32-bit index to determine the offset.\r
+\r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 32-bit index to decode\r
+\r
+Returns:\r
+  Converted index per EBC VM specification\r
+\r
+--*/\r
+{\r
+  UINT32  Index;\r
+  INT32   Offset;\r
+  INT32   C;\r
+  INT32   N;\r
+  INT32   NBits;\r
+  INT32   Mask;\r
+\r
+  Index = VmReadImmed32 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = (Index & 0x70000000) >> 28;\r
+\r
+  //\r
+  // Scale it for 32-bit indexes\r
+  //\r
+  NBits *= 4;\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (INT32)~0 << NBits;\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = Index &~Mask;\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = ((Index &~0xF0000000) & Mask) >> NBits;\r
+\r
+  Offset  = N * sizeof (UINTN) + C;\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x80000000) {\r
+    Offset = Offset * -1;\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+INT64\r
+VmReadIndex64 (\r
+  IN VM_CONTEXT     *VmPtr,\r
+  IN UINT32         CodeOffset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Decode a 64-bit index to determine the offset.\r
+\r
+Arguments:\r
+  VmPtr       - pointer to VM context\r
+  CodeOffset  - offset from IP of the location of the 64-bit index to decode\r
+\r
+Returns:\r
+  Converted index per EBC VM specification\r
+\r
+--*/\r
+{\r
+  UINT64  Index;\r
+  INT64   Offset;\r
+  INT64   C;\r
+  INT64   N;\r
+  INT64   NBits;\r
+  INT64   Mask;\r
+\r
+  Index = VmReadCode64 (VmPtr, CodeOffset);\r
+\r
+  //\r
+  // Get the mask for N. First get the number of bits from the index.\r
+  //\r
+  NBits = RShiftU64 ((Index & 0x7000000000000000ULL), 60);\r
+\r
+  //\r
+  // Scale it for 64-bit indexes (multiply by 8 by shifting left 3)\r
+  //\r
+  NBits = LShiftU64 ((UINT64)NBits, 3);\r
+\r
+  //\r
+  // Now using the number of bits, create a mask.\r
+  //\r
+  Mask = (LShiftU64 ((UINT64)~0, (UINTN)NBits));\r
+\r
+  //\r
+  // Now using the mask, extract N from the lower bits of the index.\r
+  //\r
+  N = Index &~Mask;\r
+\r
+  //\r
+  // Now compute C\r
+  //\r
+  C       = ARShiftU64 (((Index &~0xF000000000000000ULL) & Mask), (UINTN)NBits);\r
+\r
+  Offset  = MultU64x64 (N, sizeof (UINTN)) + C;\r
+\r
+  //\r
+  // Now set the sign\r
+  //\r
+  if (Index & 0x8000000000000000ULL) {\r
+    Offset = MultS64x64 (Offset, -1);\r
+  }\r
+\r
+  return Offset;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem8 (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr,\r
+  IN UINT8         Data\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  The following VmWriteMem? routines are called by the EBC data\r
+  movement instructions that write to memory. Since these writes\r
+  may be to the stack, which looks like (high address on top) this,\r
+\r
+  [EBC entry point arguments]\r
+  [VM stack]\r
+  [EBC stack]\r
+\r
+  we need to detect all attempts to write to the EBC entry point argument\r
+  stack area and adjust the address (which will initially point into the \r
+  VM stack) to point into the EBC entry point arguments.\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Addr    - adddress to write to\r
+  Data    - value to write to Addr\r
+  \r
+Returns:\r
+  Standard EFI_STATUS\r
+\r
+--*/\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr            = ConvertStackAddr (VmPtr, Addr);\r
+  *(UINT8 *) Addr = Data;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem16 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT16       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT16))) {\r
+    *(UINT16 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two bytes\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem8 (VmPtr, Addr, (UINT8) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem8 (VmPtr, Addr + 1, (UINT8) (Data >> 8))) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+VmWriteMem32 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT32       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT32))) {\r
+    *(UINT32 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two words\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem16 (VmPtr, Addr, (UINT16) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem16 (VmPtr, Addr + sizeof (UINT16), (UINT16) (Data >> 16))) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+VmWriteMem64 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINT64       Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      Data32;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT64))) {\r
+    *(UINT64 *) Addr = Data;\r
+  } else {\r
+    //\r
+    // Write as two 32-bit words\r
+    //\r
+    MemoryFence ();\r
+    if ((Status = VmWriteMem32 (VmPtr, Addr, (UINT32) Data)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+    Data32 = (UINT32) (((UINT32 *) &Data)[1]);\r
+    if ((Status = VmWriteMem32 (VmPtr, Addr + sizeof (UINT32), Data32)) != EFI_SUCCESS) {\r
+      return Status;\r
+    }\r
+\r
+    MemoryFence ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+VmWriteMemN (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINTN        Data\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Index;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Do a simple write if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINTN))) {\r
+    *(UINTN *) Addr = Data;\r
+  } else {\r
+    for (Index = 0; Index < sizeof (UINTN) / sizeof (UINT32); Index++) {\r
+      MemoryFence ();\r
+      Status = VmWriteMem32 (VmPtr, Addr + Index * sizeof (UINT32), (UINT32) Data);\r
+      MemoryFence ();\r
+      Data = (UINTN)RShiftU64 ((UINT64)Data, 32);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+INT8\r
+VmReadImmed8 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  \r
+  The following VmReadImmed routines are called by the EBC execute\r
+  functions to read EBC immediate values from the code stream.\r
+  Since we can't assume alignment, each tries to read in the biggest \r
+  chunks size available, but will revert to smaller reads if necessary.\r
+\r
+Arguments:\r
+  VmPtr   - pointer to a VM context  \r
+  Offset  - offset from IP of the code bytes to read.\r
+\r
+Returns:\r
+  Signed data of the requested size from the specified address.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Simply return the data in flat memory space\r
+  //\r
+  return * (INT8 *) (VmPtr->Ip + Offset);\r
+}\r
+\r
+STATIC\r
+INT16\r
+VmReadImmed16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) {\r
+    return * (INT16 *) (VmPtr->Ip + Offset);\r
+  } else {\r
+    //\r
+    // All code word reads should be aligned\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_ALIGNMENT_CHECK,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (INT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));\r
+}\r
+\r
+STATIC\r
+INT32\r
+VmReadImmed32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT32  Data;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {\r
+    return * (INT32 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadCode16 (VmPtr, Offset);\r
+  Data |= (UINT32) (VmReadCode16 (VmPtr, Offset + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+INT64\r
+VmReadImmed64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT64  Data64;\r
+  UINT32  Data32;\r
+  UINT8   *Ptr;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {\r
+    return * (UINT64 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data.\r
+  //\r
+  Ptr             = (UINT8 *) &Data64;\r
+  Data32          = VmReadCode32 (VmPtr, Offset);\r
+  *(UINT32 *) Ptr = Data32;\r
+  Ptr += sizeof (Data32);\r
+  Data32          = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));\r
+  *(UINT32 *) Ptr = Data32;\r
+  return Data64;\r
+}\r
+\r
+STATIC\r
+UINT16\r
+VmReadCode16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  The following VmReadCode() routines provide the ability to read raw \r
+  unsigned data from the code stream. \r
+  \r
+Arguments:\r
+  VmPtr   - pointer to VM context\r
+  Offset  - offset from current IP to the raw data to read.\r
+\r
+Returns:\r
+  The raw unsigned 16-bit value from the code stream.\r
+  \r
+--*/\r
+{\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) {\r
+    return * (UINT16 *) (VmPtr->Ip + Offset);\r
+  } else {\r
+    //\r
+    // All code word reads should be aligned\r
+    //\r
+    EbcDebugSignalException (\r
+      EXCEPT_EBC_ALIGNMENT_CHECK,\r
+      EXCEPTION_FLAG_WARNING,\r
+      VmPtr\r
+      );\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (UINT16) (*(UINT8 *) (VmPtr->Ip + Offset) + (*(UINT8 *) (VmPtr->Ip + Offset + 1) << 8));\r
+}\r
+\r
+STATIC\r
+UINT32\r
+VmReadCode32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT32  Data;\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {\r
+    return * (UINT32 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadCode16 (VmPtr, Offset);\r
+  Data |= (VmReadCode16 (VmPtr, Offset + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+VmReadCode64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINT32     Offset\r
+  )\r
+{\r
+  UINT64  Data64;\r
+  UINT32  Data32;\r
+  UINT8   *Ptr;\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {\r
+    return * (UINT64 *) (VmPtr->Ip + Offset);\r
+  }\r
+  //\r
+  // Return unaligned data.\r
+  //\r
+  Ptr             = (UINT8 *) &Data64;\r
+  Data32          = VmReadCode32 (VmPtr, Offset);\r
+  *(UINT32 *) Ptr = Data32;\r
+  Ptr += sizeof (Data32);\r
+  Data32          = VmReadCode32 (VmPtr, Offset + sizeof (UINT32));\r
+  *(UINT32 *) Ptr = Data32;\r
+  return Data64;\r
+}\r
+\r
+STATIC\r
+UINT8\r
+VmReadMem8 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  )\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Simply return the data in flat memory space\r
+  //\r
+  return * (UINT8 *) Addr;\r
+}\r
+\r
+STATIC\r
+UINT16\r
+VmReadMem16 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  )\r
+{\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT16))) {\r
+    return * (UINT16 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  return (UINT16) (*(UINT8 *) Addr + (*(UINT8 *) (Addr + 1) << 8));\r
+}\r
+\r
+STATIC\r
+UINT32\r
+VmReadMem32 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  IN UINTN      Addr\r
+  )\r
+{\r
+  UINT32  Data;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT32))) {\r
+    return * (UINT32 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data = (UINT32) VmReadMem16 (VmPtr, Addr);\r
+  Data |= (VmReadMem16 (VmPtr, Addr + 2) << 16);\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+VmReadMem64 (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr\r
+  )\r
+{\r
+  UINT64  Data;\r
+  UINT32  Data32;\r
+\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINT64))) {\r
+    return * (UINT64 *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data. Assume little endian.\r
+  //\r
+  Data    = (UINT64) VmReadMem32 (VmPtr, Addr);\r
+  Data32  = VmReadMem32 (VmPtr, Addr + sizeof (UINT32));\r
+  *(UINT32 *) ((UINT32 *) &Data + 1) = Data32;\r
+  return Data;\r
+}\r
+\r
+STATIC\r
+UINTN\r
+ConvertStackAddr (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Given an address that EBC is going to read from or write to, return\r
+  an appropriate address that accounts for a gap in the stack.\r
+  \r
+  The stack for this application looks like this (high addr on top)\r
+  [EBC entry point arguments]\r
+  [VM stack]\r
+  [EBC stack]\r
+\r
+  The EBC assumes that its arguments are at the top of its stack, which\r
+  is where the VM stack is really. Therefore if the EBC does memory\r
+  accesses into the VM stack area, then we need to convert the address\r
+  to point to the EBC entry point arguments area. Do this here.\r
+\r
+Arguments:\r
+\r
+  VmPtr    - pointer to VM context\r
+  Addr  - address of interest\r
+\r
+Returns:\r
+\r
+  The unchanged address if it's not in the VM stack region. Otherwise, \r
+  adjust for the stack gap and return the modified address.\r
+  \r
+--*/\r
+{ \r
+  ASSERT(((Addr < VmPtr->LowStackTop) || (Addr > VmPtr->HighStackBottom)));\r
+  return Addr;\r
+}\r
+\r
+STATIC\r
+UINTN\r
+VmReadMemN (\r
+  IN VM_CONTEXT    *VmPtr,\r
+  IN UINTN         Addr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Read a natural value from memory. May or may not be aligned.\r
+  \r
+Arguments:\r
+  VmPtr   - current VM context\r
+  Addr    - the address to read from\r
+\r
+Returns:\r
+  The natural value at address Addr.\r
+  \r
+--*/\r
+{\r
+  UINTN   Data;\r
+  volatile UINT32  Size;\r
+  UINT8   *FromPtr;\r
+  UINT8   *ToPtr;\r
+  //\r
+  // Convert the address if it's in the stack gap\r
+  //\r
+  Addr = ConvertStackAddr (VmPtr, Addr);\r
+  //\r
+  // Read direct if aligned\r
+  //\r
+  if (IS_ALIGNED (Addr, sizeof (UINTN))) {\r
+    return * (UINTN *) Addr;\r
+  }\r
+  //\r
+  // Return unaligned data\r
+  //\r
+  Data    = 0;\r
+  FromPtr = (UINT8 *) Addr;\r
+  ToPtr   = (UINT8 *) &Data;\r
+\r
+  for (Size = 0; Size < sizeof (Data); Size++) {\r
+    *ToPtr = *FromPtr;\r
+    ToPtr++;\r
+    FromPtr++;\r
+  }\r
+\r
+  return Data;\r
+}\r
+\r
+UINT64\r
+GetVmVersion (\r
+  VOID\r
+  )\r
+{\r
+  return (UINT64) (((VM_MAJOR_VERSION & 0xFFFF) << 16) | ((VM_MINOR_VERSION & 0xFFFF)));\r
+}\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcExecute.h b/MdeModulePkg/Universal/EbcDxe/EbcExecute.h
new file mode 100644 (file)
index 0000000..b1926ee
--- /dev/null
@@ -0,0 +1,323 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  EbcExecute.h\r
+\r
+Abstract:\r
+\r
+  Header file for Virtual Machine support. Contains EBC defines that can\r
+  be of use to a disassembler for the most part. Also provides function \r
+  prototypes for VM functions.\r
+\r
+--*/\r
+\r
+#ifndef _EBC_EXECUTE_H_\r
+#define _EBC_EXECUTE_H_\r
+\r
+//\r
+// VM major/minor version\r
+//\r
+#define VM_MAJOR_VERSION  1\r
+#define VM_MINOR_VERSION  0\r
+\r
+//\r
+// Macros to check and set alignment\r
+//\r
+#define ASSERT_ALIGNED(addr, size)  ASSERT (!((UINT32) (addr) & (size - 1)))\r
+#define IS_ALIGNED(addr, size)      !((UINT32) (addr) & (size - 1))\r
+\r
+//\r
+// Define a macro to get the operand. Then we can change it to be either a\r
+// direct read or have it call a function to read memory.\r
+//\r
+#define GETOPERANDS(pVM)  (UINT8) (*(UINT8 *) (pVM->Ip + 1))\r
+#define GETOPCODE(pVM)    (UINT8) (*(UINT8 *) pVM->Ip)\r
+\r
+//\r
+// Bit masks for opcode encodings\r
+//\r
+#define OPCODE_M_OPCODE       0x3F  // bits of interest for first level decode\r
+#define OPCODE_M_IMMDATA      0x80\r
+#define OPCODE_M_IMMDATA64    0x40\r
+#define OPCODE_M_64BIT        0x40  // for CMP\r
+#define OPCODE_M_RELADDR      0x10  // for CALL instruction\r
+#define OPCODE_M_CMPI32_DATA  0x80  // for CMPI\r
+#define OPCODE_M_CMPI64       0x40  // for CMPI 32 or 64 bit comparison\r
+#define OPERAND_M_MOVIN_N     0x80\r
+#define OPERAND_M_CMPI_INDEX  0x10\r
+\r
+//\r
+// Masks for instructions that encode presence of indexes for operand1 and/or\r
+// operand2.\r
+//\r
+#define OPCODE_M_IMMED_OP1  0x80\r
+#define OPCODE_M_IMMED_OP2  0x40\r
+\r
+//\r
+// Bit masks for operand encodings\r
+//\r
+#define OPERAND_M_INDIRECT1 0x08\r
+#define OPERAND_M_INDIRECT2 0x80\r
+#define OPERAND_M_OP1       0x07\r
+#define OPERAND_M_OP2       0x70\r
+\r
+//\r
+// Masks for data manipulation instructions\r
+//\r
+#define DATAMANIP_M_64      0x40  // 64-bit width operation\r
+#define DATAMANIP_M_IMMDATA 0x80\r
+\r
+//\r
+// For MOV instructions, need a mask for the opcode when immediate\r
+// data applies to R2.\r
+//\r
+#define OPCODE_M_IMMED_OP2  0x40\r
+\r
+//\r
+// The MOVI/MOVIn instructions use bit 6 of operands byte to indicate\r
+// if an index is present. Then bits 4 and 5 are used to indicate the width\r
+// of the move.\r
+//\r
+#define MOVI_M_IMMDATA    0x40\r
+#define MOVI_M_DATAWIDTH  0xC0\r
+#define MOVI_DATAWIDTH16  0x40\r
+#define MOVI_DATAWIDTH32  0x80\r
+#define MOVI_DATAWIDTH64  0xC0\r
+#define MOVI_M_MOVEWIDTH  0x30\r
+#define MOVI_MOVEWIDTH8   0x00\r
+#define MOVI_MOVEWIDTH16  0x10\r
+#define MOVI_MOVEWIDTH32  0x20\r
+#define MOVI_MOVEWIDTH64  0x30\r
+\r
+//\r
+// Masks for CALL instruction encodings\r
+//\r
+#define OPERAND_M_RELATIVE_ADDR 0x10\r
+#define OPERAND_M_NATIVE_CALL   0x20\r
+\r
+//\r
+// Masks for decoding push/pop instructions\r
+//\r
+#define PUSHPOP_M_IMMDATA 0x80  // opcode bit indicating immediate data\r
+#define PUSHPOP_M_64      0x40  // opcode bit indicating 64-bit operation\r
+//\r
+// Mask for operand of JMP instruction\r
+//\r
+#define JMP_M_RELATIVE    0x10\r
+#define JMP_M_CONDITIONAL 0x80\r
+#define JMP_M_CS          0x40\r
+\r
+//\r
+// Macros to determine if a given operand is indirect\r
+//\r
+#define OPERAND1_INDIRECT(op) ((op) & OPERAND_M_INDIRECT1)\r
+#define OPERAND2_INDIRECT(op) ((op) & OPERAND_M_INDIRECT2)\r
+\r
+//\r
+// Macros to extract the operands from second byte of instructions\r
+//\r
+#define OPERAND1_REGNUM(op)       ((op) & OPERAND_M_OP1)\r
+#define OPERAND2_REGNUM(op)       (((op) & OPERAND_M_OP2) >> 4)\r
+\r
+#define OPERAND1_CHAR(op)         ('0' + OPERAND1_REGNUM (op))\r
+#define OPERAND2_CHAR(op)         ('0' + OPERAND2_REGNUM (op))\r
+\r
+#define OPERAND1_REGDATA(pvm, op) pvm->R[OPERAND1_REGNUM (op)]\r
+#define OPERAND2_REGDATA(pvm, op) pvm->R[OPERAND2_REGNUM (op)]\r
+\r
+//\r
+// Condition masks usually for byte 1 encodings of code\r
+//\r
+#define CONDITION_M_CONDITIONAL 0x80\r
+#define CONDITION_M_CS          0x40\r
+\r
+//\r
+// Bits in the VM->StopFlags field\r
+//\r
+#define STOPFLAG_APP_DONE         0x0001\r
+#define STOPFLAG_BREAKPOINT       0x0002\r
+#define STOPFLAG_INVALID_BREAK    0x0004\r
+#define STOPFLAG_BREAK_ON_CALLEX  0x0008\r
+\r
+//\r
+// Masks for working with the VM flags register\r
+//\r
+#define VMFLAGS_CC        0x0001  // condition flag\r
+#define VMFLAGS_STEP      0x0002  // step instruction mode\r
+#define VMFLAGS_ALL_VALID (VMFLAGS_CC | VMFLAGS_STEP)\r
+\r
+//\r
+// Macros for operating on the VM flags register\r
+//\r
+#define VMFLAG_SET(pVM, Flag)   (pVM->Flags |= (Flag))\r
+#define VMFLAG_ISSET(pVM, Flag) ((pVM->Flags & (Flag)) ? 1 : 0)\r
+#define VMFLAG_CLEAR(pVM, Flag) (pVM->Flags &= ~(Flag))\r
+\r
+//\r
+// Debug macro\r
+//\r
+#define EBCMSG(s) gST->ConOut->OutputString (gST->ConOut, s)\r
+\r
+//\r
+// Define OPCODES\r
+//\r
+#define OPCODE_BREAK    0x00\r
+#define OPCODE_JMP      0x01\r
+#define OPCODE_JMP8     0x02\r
+#define OPCODE_CALL     0x03\r
+#define OPCODE_RET      0x04\r
+#define OPCODE_CMPEQ    0x05\r
+#define OPCODE_CMPLTE   0x06\r
+#define OPCODE_CMPGTE   0x07\r
+#define OPCODE_CMPULTE  0x08\r
+#define OPCODE_CMPUGTE  0x09\r
+#define OPCODE_NOT      0x0A\r
+#define OPCODE_NEG      0x0B\r
+#define OPCODE_ADD      0x0C\r
+#define OPCODE_SUB      0x0D\r
+#define OPCODE_MUL      0x0E\r
+#define OPCODE_MULU     0x0F\r
+#define OPCODE_DIV      0x10\r
+#define OPCODE_DIVU     0x11\r
+#define OPCODE_MOD      0x12\r
+#define OPCODE_MODU     0x13\r
+#define OPCODE_AND      0x14\r
+#define OPCODE_OR       0x15\r
+#define OPCODE_XOR      0x16\r
+#define OPCODE_SHL      0x17\r
+#define OPCODE_SHR      0x18\r
+#define OPCODE_ASHR     0x19\r
+#define OPCODE_EXTNDB   0x1A\r
+#define OPCODE_EXTNDW   0x1B\r
+#define OPCODE_EXTNDD   0x1C\r
+#define OPCODE_MOVBW    0x1D\r
+#define OPCODE_MOVWW    0x1E\r
+#define OPCODE_MOVDW    0x1F\r
+#define OPCODE_MOVQW    0x20\r
+#define OPCODE_MOVBD    0x21\r
+#define OPCODE_MOVWD    0x22\r
+#define OPCODE_MOVDD    0x23\r
+#define OPCODE_MOVQD    0x24\r
+#define OPCODE_MOVSNW   0x25  // Move signed natural with word index\r
+#define OPCODE_MOVSND   0x26  // Move signed natural with dword index\r
+//\r
+// #define OPCODE_27         0x27\r
+//\r
+#define OPCODE_MOVQQ    0x28  // Does this go away?\r
+#define OPCODE_LOADSP   0x29\r
+#define OPCODE_STORESP  0x2A\r
+#define OPCODE_PUSH     0x2B\r
+#define OPCODE_POP      0x2C\r
+#define OPCODE_CMPIEQ   0x2D\r
+#define OPCODE_CMPILTE  0x2E\r
+#define OPCODE_CMPIGTE  0x2F\r
+#define OPCODE_CMPIULTE 0x30\r
+#define OPCODE_CMPIUGTE 0x31\r
+#define OPCODE_MOVNW    0x32\r
+#define OPCODE_MOVND    0x33\r
+//\r
+// #define OPCODE_34         0x34\r
+//\r
+#define OPCODE_PUSHN  0x35\r
+#define OPCODE_POPN   0x36\r
+#define OPCODE_MOVI   0x37\r
+#define OPCODE_MOVIN  0x38\r
+#define OPCODE_MOVREL 0x39\r
+\r
+EFI_STATUS\r
+EbcExecute (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+;\r
+\r
+\r
+\r
+UINT64\r
+GetVmVersion (\r
+  VOID\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+VmWriteMemN (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        Addr,\r
+  IN UINTN        Data\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+VmWriteMem64 (\r
+  IN VM_CONTEXT *VmPtr,\r
+  UINTN         Addr,\r
+  IN UINT64     Data\r
+  )\r
+;\r
+\r
+//\r
+// Define a protocol for an EBC VM test interface.\r
+//\r
+#define EFI_EBC_VM_TEST_PROTOCOL_GUID \\r
+  { \\r
+    0xAAEACCFDL, 0xF27B, 0x4C17, { 0xB6, 0x10, 0x75, 0xCA, 0x1F, 0x2D, 0xFB, 0x52 } \\r
+  }\r
+\r
+//\r
+// Define for forward reference.\r
+//\r
+typedef struct _EFI_EBC_VM_TEST_PROTOCOL EFI_EBC_VM_TEST_PROTOCOL;\r
+\r
+typedef\r
+EFI_STATUS\r
+(*EBC_VM_TEST_EXECUTE) (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL         * This,\r
+  IN VM_CONTEXT                       * VmPtr,\r
+  IN OUT UINTN                        *InstructionCount\r
+  );\r
+\r
+typedef\r
+EFI_STATUS\r
+(*EBC_VM_TEST_ASM) (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL         * This,\r
+  IN CHAR16                           *AsmText,\r
+  IN OUT INT8                         *Buffer,\r
+  IN OUT UINTN                        *BufferLen\r
+  );\r
+\r
+typedef\r
+EFI_STATUS\r
+(*EBC_VM_TEST_DASM) (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL         * This,\r
+  IN OUT CHAR16                       *AsmText,\r
+  IN OUT INT8                         *Buffer,\r
+  IN OUT UINTN                        *Len\r
+  );\r
+\r
+//\r
+// Prototype for the actual EBC test protocol interface\r
+//\r
+struct _EFI_EBC_VM_TEST_PROTOCOL {\r
+  EBC_VM_TEST_EXECUTE Execute;\r
+  EBC_VM_TEST_ASM     Assemble;\r
+  EBC_VM_TEST_DASM    Disassemble;\r
+};\r
+\r
+EFI_STATUS\r
+EbcExecuteInstructions (\r
+  IN EFI_EBC_VM_TEST_PROTOCOL *This,\r
+  IN VM_CONTEXT               *VmPtr,\r
+  IN OUT UINTN                *InstructionCount\r
+  )\r
+;\r
+\r
+#endif // ifndef _EBC_EXECUTE_H_\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.c b/MdeModulePkg/Universal/EbcDxe/EbcInt.c
new file mode 100644 (file)
index 0000000..2b647d9
--- /dev/null
@@ -0,0 +1,1172 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+  EbcInt.c\r
+\r
+Abstract:\r
+\r
+  Top level module for the EBC virtual machine implementation.\r
+  Provides auxilliary support routines for the VM. That is, routines\r
+  that are not particularly related to VM execution of EBC instructions.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+//\r
+// We'll keep track of all thunks we create in a linked list. Each\r
+// thunk is tied to an image handle, so we have a linked list of\r
+// image handles, with each having a linked list of thunks allocated\r
+// to that image handle.\r
+//\r
+typedef struct _EBC_THUNK_LIST {\r
+  VOID                    *ThunkBuffer;\r
+  struct _EBC_THUNK_LIST  *Next;\r
+} EBC_THUNK_LIST;\r
+\r
+typedef struct _EBC_IMAGE_LIST {\r
+  struct _EBC_IMAGE_LIST  *Next;\r
+  EFI_HANDLE              ImageHandle;\r
+  EBC_THUNK_LIST          *ThunkList;\r
+} EBC_IMAGE_LIST;\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcUnloadImage (\r
+  IN EFI_EBC_PROTOCOL     *This,\r
+  IN EFI_HANDLE           ImageHandle\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcCreateThunk (\r
+  IN EFI_EBC_PROTOCOL     *This,\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN VOID                 *EbcEntryPoint,\r
+  OUT VOID                **Thunk\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcGetVersion (\r
+  IN EFI_EBC_PROTOCOL     *This,\r
+  IN OUT UINT64           *Version\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeEbcCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL  *This\r
+  );\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+CommonEbcExceptionHandler (\r
+  IN EFI_EXCEPTION_TYPE   InterruptType,\r
+  IN EFI_SYSTEM_CONTEXT   SystemContext\r
+  );\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+EbcPeriodicNotifyFunction (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugPeriodic (\r
+  IN VM_CONTEXT *VmPtr\r
+  );\r
+\r
+//\r
+// These two functions and the  GUID are used to produce an EBC test protocol.\r
+// This functionality is definitely not required for execution.\r
+//\r
+STATIC\r
+EFI_STATUS\r
+InitEbcVmTestProtocol (\r
+  IN EFI_HANDLE     *Handle\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EbcVmTestUnsupported (\r
+  VOID\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcRegisterICacheFlush (\r
+  IN EFI_EBC_PROTOCOL               *This,\r
+  IN EBC_ICACHE_FLUSH               Flush\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugGetMaximumProcessorIndex (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL     *This,\r
+  OUT UINTN                         *MaxProcessorIndex\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugRegisterPeriodicCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL     *This,\r
+  IN UINTN                          ProcessorIndex,\r
+  IN EFI_PERIODIC_CALLBACK          PeriodicCallback\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugRegisterExceptionCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL     *This,\r
+  IN UINTN                          ProcessorIndex,\r
+  IN EFI_EXCEPTION_CALLBACK         ExceptionCallback,\r
+  IN EFI_EXCEPTION_TYPE             ExceptionType\r
+  );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugInvalidateInstructionCache (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL     *This,\r
+  IN UINTN                          ProcessorIndex,\r
+  IN VOID                           *Start,\r
+  IN UINT64                         Length\r
+  );\r
+\r
+//\r
+// We have one linked list of image handles for the whole world. Since\r
+// there should only be one interpreter, make them global. They must\r
+// also be global since the execution of an EBC image does not provide\r
+// a This pointer.\r
+//\r
+static EBC_IMAGE_LIST         *mEbcImageList = NULL;\r
+\r
+//\r
+// Callback function to flush the icache after thunk creation\r
+//\r
+static EBC_ICACHE_FLUSH       mEbcICacheFlush;\r
+\r
+//\r
+// These get set via calls by the debug agent\r
+//\r
+static EFI_PERIODIC_CALLBACK  mDebugPeriodicCallback                            = NULL;\r
+static EFI_EXCEPTION_CALLBACK mDebugExceptionCallback[MAX_EBC_EXCEPTION + 1] = {NULL};\r
+static EFI_GUID               mEfiEbcVmTestProtocolGuid = EFI_EBC_VM_TEST_PROTOCOL_GUID;\r
+\r
+static VOID*      mStackBuffer[MAX_STACK_NUM];\r
+static EFI_HANDLE mStackBufferIndex[MAX_STACK_NUM];\r
+static UINTN      mStackNum = 0;\r
+\r
+//\r
+// Event for Periodic callback\r
+//\r
+static EFI_EVENT              mEbcPeriodicEvent;\r
+VM_CONTEXT                    *mVmPtr = NULL;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeEbcDriver (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Initializes the VM EFI interface.  Allocates memory for the VM interface\r
+  and registers the VM protocol.\r
+\r
+Arguments:\r
+\r
+  ImageHandle - EFI image handle.\r
+  SystemTable - Pointer to the EFI system table.\r
+\r
+Returns:\r
+  Standard EFI status code.\r
+\r
+--*/\r
+{\r
+  EFI_EBC_PROTOCOL            *EbcProtocol;\r
+  EFI_EBC_PROTOCOL            *OldEbcProtocol;\r
+  EFI_STATUS                  Status;\r
+  EFI_DEBUG_SUPPORT_PROTOCOL  *EbcDebugProtocol;\r
+  EFI_HANDLE                  *HandleBuffer;\r
+  UINTN                       NumHandles;\r
+  UINTN                       Index;\r
+  BOOLEAN                     Installed;\r
+\r
+  EbcProtocol      = NULL;\r
+  EbcDebugProtocol = NULL;\r
+\r
+  //\r
+  // Allocate memory for our protocol. Then fill in the blanks.\r
+  //\r
+  EbcProtocol = AllocatePool (sizeof (EFI_EBC_PROTOCOL));\r
+\r
+  if (EbcProtocol == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  EbcProtocol->CreateThunk          = EbcCreateThunk;\r
+  EbcProtocol->UnloadImage          = EbcUnloadImage;\r
+  EbcProtocol->RegisterICacheFlush  = EbcRegisterICacheFlush;\r
+  EbcProtocol->GetVersion           = EbcGetVersion;\r
+  mEbcICacheFlush                   = NULL;\r
+\r
+  //\r
+  // Find any already-installed EBC protocols and uninstall them\r
+  //\r
+  Installed     = FALSE;\r
+  HandleBuffer  = NULL;\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiEbcProtocolGuid,\r
+                  NULL,\r
+                  &NumHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (Status == EFI_SUCCESS) {\r
+    //\r
+    // Loop through the handles\r
+    //\r
+    for (Index = 0; Index < NumHandles; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      HandleBuffer[Index],\r
+                      &gEfiEbcProtocolGuid,\r
+                      (VOID **) &OldEbcProtocol\r
+                      );\r
+      if (Status == EFI_SUCCESS) {\r
+        if (gBS->ReinstallProtocolInterface (\r
+                  HandleBuffer[Index],\r
+                  &gEfiEbcProtocolGuid,\r
+                  OldEbcProtocol,\r
+                  EbcProtocol\r
+                  ) == EFI_SUCCESS) {\r
+          Installed = TRUE;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  if (HandleBuffer != NULL) {\r
+    FreePool (HandleBuffer);\r
+    HandleBuffer = NULL;\r
+  }\r
+  //\r
+  // Add the protocol so someone can locate us if we haven't already.\r
+  //\r
+  if (!Installed) {\r
+    Status = gBS->InstallProtocolInterface (\r
+                    &ImageHandle,\r
+                    &gEfiEbcProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    EbcProtocol\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (EbcProtocol);\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  Status = InitEBCStack();\r
+  if (EFI_ERROR(Status)) {\r
+    goto ErrorExit;\r
+  }\r
+\r
+  //\r
+  // Allocate memory for our debug protocol. Then fill in the blanks.\r
+  //\r
+  EbcDebugProtocol = AllocatePool (sizeof (EFI_DEBUG_SUPPORT_PROTOCOL));\r
+\r
+  if (EbcDebugProtocol == NULL) {\r
+    goto ErrorExit;\r
+  }\r
+\r
+  EbcDebugProtocol->Isa                         = IsaEbc;\r
+  EbcDebugProtocol->GetMaximumProcessorIndex    = EbcDebugGetMaximumProcessorIndex;\r
+  EbcDebugProtocol->RegisterPeriodicCallback    = EbcDebugRegisterPeriodicCallback;\r
+  EbcDebugProtocol->RegisterExceptionCallback   = EbcDebugRegisterExceptionCallback;\r
+  EbcDebugProtocol->InvalidateInstructionCache  = EbcDebugInvalidateInstructionCache;\r
+\r
+  //\r
+  // Add the protocol so the debug agent can find us\r
+  //\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &ImageHandle,\r
+                  &gEfiDebugSupportProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  EbcDebugProtocol\r
+                  );\r
+  //\r
+  // This is recoverable, so free the memory and continue.\r
+  //\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (EbcDebugProtocol);\r
+    goto ErrorExit;\r
+  }\r
+  //\r
+  // Install EbcDebugSupport Protocol Successfully\r
+  // Now we need to initialize the Ebc default Callback\r
+  //\r
+  Status = InitializeEbcCallback (EbcDebugProtocol);\r
+\r
+  //\r
+  // Produce a VM test interface protocol. Not required for execution.\r
+  //\r
+  DEBUG_CODE_BEGIN ();\r
+    InitEbcVmTestProtocol (&ImageHandle);\r
+  DEBUG_CODE_END ();\r
+\r
+  return EFI_SUCCESS;\r
+\r
+ErrorExit:\r
+  FreeEBCStack();\r
+  HandleBuffer  = NULL;\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiEbcProtocolGuid,\r
+                  NULL,\r
+                  &NumHandles,\r
+                  &HandleBuffer\r
+                  );\r
+  if (Status == EFI_SUCCESS) {\r
+    //\r
+    // Loop through the handles\r
+    //\r
+    for (Index = 0; Index < NumHandles; Index++) {\r
+      Status = gBS->HandleProtocol (\r
+                      HandleBuffer[Index],\r
+                      &gEfiEbcProtocolGuid,\r
+                      (VOID **) &OldEbcProtocol\r
+                      );\r
+      if (Status == EFI_SUCCESS) {\r
+        gBS->UninstallProtocolInterface (\r
+               HandleBuffer[Index],\r
+               &gEfiEbcProtocolGuid,\r
+               OldEbcProtocol\r
+               );\r
+      }\r
+    }\r
+  }\r
+\r
+  if (HandleBuffer != NULL) {\r
+    FreePool (HandleBuffer);\r
+    HandleBuffer = NULL;\r
+  }\r
+\r
+  FreePool (EbcProtocol);\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcCreateThunk (\r
+  IN EFI_EBC_PROTOCOL   *This,\r
+  IN EFI_HANDLE         ImageHandle,\r
+  IN VOID               *EbcEntryPoint,\r
+  OUT VOID              **Thunk\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This is the top-level routine plugged into the EBC protocol. Since thunks\r
+  are very processor-specific, from here we dispatch directly to the very\r
+  processor-specific routine EbcCreateThunks().\r
+\r
+Arguments:\r
+\r
+  This          - protocol instance pointer\r
+  ImageHandle   - handle to the image. The EBC interpreter may use this to keep\r
+                  track of any resource allocations performed in loading and\r
+                  executing the image.\r
+  EbcEntryPoint - the entry point for the image (as defined in the file header)\r
+  Thunk         - pointer to thunk pointer where the address of the created\r
+                  thunk is returned.\r
+\r
+Returns:\r
+\r
+  EFI_STATUS\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = EbcCreateThunks (\r
+            ImageHandle,\r
+            EbcEntryPoint,\r
+            Thunk,\r
+            FLAG_THUNK_ENTRY_POINT\r
+            );\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugGetMaximumProcessorIndex (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL          *This,\r
+  OUT UINTN                              *MaxProcessorIndex\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This EBC debugger protocol service is called by the debug agent\r
+\r
+Arguments:\r
+\r
+  This              - pointer to the caller's debug support protocol interface\r
+  MaxProcessorIndex - pointer to a caller allocated UINTN in which the maximum\r
+                      processor index is returned.\r
+\r
+Returns:\r
+\r
+  Standard EFI_STATUS\r
+\r
+--*/\r
+{\r
+  *MaxProcessorIndex = 0;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugRegisterPeriodicCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL  *This,\r
+  IN UINTN                       ProcessorIndex,\r
+  IN EFI_PERIODIC_CALLBACK       PeriodicCallback\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This protocol service is called by the debug agent to register a function\r
+  for us to call on a periodic basis.\r
+\r
+\r
+Arguments:\r
+\r
+  This              - pointer to the caller's debug support protocol interface\r
+  PeriodicCallback  - pointer to the function to call periodically\r
+\r
+Returns:\r
+\r
+  Always EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  if ((mDebugPeriodicCallback == NULL) && (PeriodicCallback == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if ((mDebugPeriodicCallback != NULL) && (PeriodicCallback != NULL)) {\r
+    return EFI_ALREADY_STARTED;\r
+  }\r
+\r
+  mDebugPeriodicCallback = PeriodicCallback;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugRegisterExceptionCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL  *This,\r
+  IN UINTN                       ProcessorIndex,\r
+  IN EFI_EXCEPTION_CALLBACK      ExceptionCallback,\r
+  IN EFI_EXCEPTION_TYPE          ExceptionType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This protocol service is called by the debug agent to register a function\r
+  for us to call when we detect an exception.\r
+\r
+\r
+Arguments:\r
+\r
+  This              - pointer to the caller's debug support protocol interface\r
+  ExceptionCallback - pointer to the function to the exception\r
+\r
+Returns:\r
+\r
+  Always EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  if ((ExceptionType < 0) || (ExceptionType > MAX_EBC_EXCEPTION)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if ((mDebugExceptionCallback[ExceptionType] == NULL) && (ExceptionCallback == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if ((mDebugExceptionCallback[ExceptionType] != NULL) && (ExceptionCallback != NULL)) {\r
+    return EFI_ALREADY_STARTED;\r
+  }\r
+  mDebugExceptionCallback[ExceptionType] = ExceptionCallback;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcDebugInvalidateInstructionCache (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL          *This,\r
+  IN UINTN                               ProcessorIndex,\r
+  IN VOID                                *Start,\r
+  IN UINT64                              Length\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This EBC debugger protocol service is called by the debug agent.  Required\r
+  for DebugSupport compliance but is only stubbed out for EBC.\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EbcDebugSignalException (\r
+  IN EFI_EXCEPTION_TYPE                   ExceptionType,\r
+  IN EXCEPTION_FLAGS                      ExceptionFlags,\r
+  IN VM_CONTEXT                           *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  The VM interpreter calls this function when an exception is detected.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to a VM context for passing info to the EFI debugger.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS if it returns at all\r
+\r
+--*/\r
+{\r
+  EFI_SYSTEM_CONTEXT_EBC  EbcContext;\r
+  EFI_SYSTEM_CONTEXT      SystemContext;\r
+\r
+  ASSERT ((ExceptionType >= 0) && (ExceptionType <= MAX_EBC_EXCEPTION));\r
+  //\r
+  // Save the exception in the context passed in\r
+  //\r
+  VmPtr->ExceptionFlags |= ExceptionFlags;\r
+  VmPtr->LastException = ExceptionType;\r
+  //\r
+  // If it's a fatal exception, then flag it in the VM context in case an\r
+  // attached debugger tries to return from it.\r
+  //\r
+  if (ExceptionFlags & EXCEPTION_FLAG_FATAL) {\r
+    VmPtr->StopFlags |= STOPFLAG_APP_DONE;\r
+  }\r
+\r
+  //\r
+  // If someone's registered for exception callbacks, then call them.\r
+  //\r
+  // EBC driver will register default exception callback to report the\r
+  // status code via the status code API\r
+  //\r
+  if (mDebugExceptionCallback[ExceptionType] != NULL) {\r
+\r
+    //\r
+    // Initialize the context structure\r
+    //\r
+    EbcContext.R0                   = VmPtr->R[0];\r
+    EbcContext.R1                   = VmPtr->R[1];\r
+    EbcContext.R2                   = VmPtr->R[2];\r
+    EbcContext.R3                   = VmPtr->R[3];\r
+    EbcContext.R4                   = VmPtr->R[4];\r
+    EbcContext.R5                   = VmPtr->R[5];\r
+    EbcContext.R6                   = VmPtr->R[6];\r
+    EbcContext.R7                   = VmPtr->R[7];\r
+    EbcContext.Ip                   = (UINT64)(UINTN)VmPtr->Ip;\r
+    EbcContext.Flags                = VmPtr->Flags;\r
+    EbcContext.ControlFlags         = 0;\r
+    SystemContext.SystemContextEbc  = &EbcContext;\r
+\r
+    mDebugExceptionCallback[ExceptionType] (ExceptionType, SystemContext);\r
+    //\r
+    // Restore the context structure and continue to execute\r
+    //\r
+    VmPtr->R[0]  = EbcContext.R0;\r
+    VmPtr->R[1]  = EbcContext.R1;\r
+    VmPtr->R[2]  = EbcContext.R2;\r
+    VmPtr->R[3]  = EbcContext.R3;\r
+    VmPtr->R[4]  = EbcContext.R4;\r
+    VmPtr->R[5]  = EbcContext.R5;\r
+    VmPtr->R[6]  = EbcContext.R6;\r
+    VmPtr->R[7]  = EbcContext.R7;\r
+    VmPtr->Ip    = (VMIP)(UINTN)EbcContext.Ip;\r
+    VmPtr->Flags = EbcContext.Flags;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+InitializeEbcCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL  *This\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  To install default Callback function for the VM interpreter.\r
+\r
+Arguments:\r
+\r
+  This - pointer to the instance of DebugSupport protocol\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  INTN       Index;\r
+  EFI_STATUS Status;\r
+\r
+  //\r
+  // For ExceptionCallback\r
+  //\r
+  for (Index = 0; Index <= MAX_EBC_EXCEPTION; Index++) {\r
+    EbcDebugRegisterExceptionCallback (\r
+      This,\r
+      0,\r
+      CommonEbcExceptionHandler,\r
+      Index\r
+      );\r
+  }\r
+\r
+  //\r
+  // For PeriodicCallback\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  EbcPeriodicNotifyFunction,\r
+                  &mVmPtr,\r
+                  &mEbcPeriodicEvent\r
+                  );\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->SetTimer (\r
+                  mEbcPeriodicEvent,\r
+                  TimerPeriodic,\r
+                  EBC_VM_PERIODIC_CALLBACK_RATE\r
+                  );\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+VOID\r
+CommonEbcExceptionHandler (\r
+  IN EFI_EXCEPTION_TYPE   InterruptType,\r
+  IN EFI_SYSTEM_CONTEXT   SystemContext\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  The default Exception Callback for the VM interpreter.\r
+  In this function, we report status code, and print debug information\r
+  about EBC_CONTEXT, then dead loop.\r
+\r
+Arguments:\r
+\r
+  InterruptType - Interrupt type.\r
+  SystemContext - EBC system context.\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  //\r
+  // We deadloop here to make it easy to debug this issue.\r
+  //\r
+  ASSERT (FALSE);\r
+\r
+  return ;\r
+}\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+EbcPeriodicNotifyFunction (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  The periodic callback function for EBC VM interpreter, which is used\r
+  to support the EFI debug support protocol.\r
+\r
+Arguments:\r
+\r
+  Event   - The Periodic Callback Event.\r
+  Context - It should be the address of VM_CONTEXT pointer.\r
+\r
+Returns:\r
+\r
+  None.\r
+\r
+--*/\r
+{\r
+  VM_CONTEXT *VmPtr;\r
+\r
+  VmPtr = *(VM_CONTEXT **)Context;\r
+\r
+  if (VmPtr != NULL) {\r
+    EbcDebugPeriodic (VmPtr);\r
+  }\r
+\r
+  return ;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EbcDebugPeriodic (\r
+  IN VM_CONTEXT *VmPtr\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  The VM interpreter calls this function on a periodic basis to support\r
+  the EFI debug support protocol.\r
+\r
+Arguments:\r
+\r
+  VmPtr - pointer to a VM context for passing info to the debugger.\r
+\r
+Returns:\r
+\r
+  Standard EFI status.\r
+\r
+--*/\r
+{\r
+  EFI_SYSTEM_CONTEXT_EBC   EbcContext;\r
+  EFI_SYSTEM_CONTEXT       SystemContext;\r
+\r
+  //\r
+  // If someone's registered for periodic callbacks, then call them.\r
+  //\r
+  if (mDebugPeriodicCallback != NULL) {\r
+\r
+    //\r
+    // Initialize the context structure\r
+    //\r
+    EbcContext.R0                   = VmPtr->R[0];\r
+    EbcContext.R1                   = VmPtr->R[1];\r
+    EbcContext.R2                   = VmPtr->R[2];\r
+    EbcContext.R3                   = VmPtr->R[3];\r
+    EbcContext.R4                   = VmPtr->R[4];\r
+    EbcContext.R5                   = VmPtr->R[5];\r
+    EbcContext.R6                   = VmPtr->R[6];\r
+    EbcContext.R7                   = VmPtr->R[7];\r
+    EbcContext.Ip                   = (UINT64)(UINTN)VmPtr->Ip;\r
+    EbcContext.Flags                = VmPtr->Flags;\r
+    EbcContext.ControlFlags         = 0;\r
+    SystemContext.SystemContextEbc  = &EbcContext;\r
+\r
+    mDebugPeriodicCallback (SystemContext);\r
+\r
+    //\r
+    // Restore the context structure and continue to execute\r
+    //\r
+    VmPtr->R[0]  = EbcContext.R0;\r
+    VmPtr->R[1]  = EbcContext.R1;\r
+    VmPtr->R[2]  = EbcContext.R2;\r
+    VmPtr->R[3]  = EbcContext.R3;\r
+    VmPtr->R[4]  = EbcContext.R4;\r
+    VmPtr->R[5]  = EbcContext.R5;\r
+    VmPtr->R[6]  = EbcContext.R6;\r
+    VmPtr->R[7]  = EbcContext.R7;\r
+    VmPtr->Ip    = (VMIP)(UINTN)EbcContext.Ip;\r
+    VmPtr->Flags = EbcContext.Flags;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcUnloadImage (\r
+  IN EFI_EBC_PROTOCOL   *This,\r
+  IN EFI_HANDLE         ImageHandle\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This routine is called by the core when an image is being unloaded from\r
+  memory. Basically we now have the opportunity to do any necessary cleanup.\r
+  Typically this will include freeing any memory allocated for thunk-creation.\r
+\r
+Arguments:\r
+\r
+  This          - protocol instance pointer\r
+  ImageHandle   - handle to the image being unloaded.\r
+\r
+Returns:\r
+\r
+  EFI_INVALID_PARAMETER  - the ImageHandle passed in was not found in\r
+                           the internal list of EBC image handles.\r
+  EFI_STATUS             - completed successfully\r
+\r
+--*/\r
+{\r
+  EBC_THUNK_LIST  *ThunkList;\r
+  EBC_THUNK_LIST  *NextThunkList;\r
+  EBC_IMAGE_LIST  *ImageList;\r
+  EBC_IMAGE_LIST  *PrevImageList;\r
+  //\r
+  // First go through our list of known image handles and see if we've already\r
+  // created an image list element for this image handle.\r
+  //\r
+  ReturnEBCStackByHandle(ImageHandle);\r
+  PrevImageList = NULL;\r
+  for (ImageList = mEbcImageList; ImageList != NULL; ImageList = ImageList->Next) {\r
+    if (ImageList->ImageHandle == ImageHandle) {\r
+      break;\r
+    }\r
+    //\r
+    // Save the previous so we can connect the lists when we remove this one\r
+    //\r
+    PrevImageList = ImageList;\r
+  }\r
+\r
+  if (ImageList == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Free up all the thunk buffers and thunks list elements for this image\r
+  // handle.\r
+  //\r
+  ThunkList = ImageList->ThunkList;\r
+  while (ThunkList != NULL) {\r
+    NextThunkList = ThunkList->Next;\r
+    FreePool (ThunkList->ThunkBuffer);\r
+    FreePool (ThunkList);\r
+    ThunkList = NextThunkList;\r
+  }\r
+  //\r
+  // Now remove this image list element from the chain\r
+  //\r
+  if (PrevImageList == NULL) {\r
+    //\r
+    // Remove from head\r
+    //\r
+    mEbcImageList = ImageList->Next;\r
+  } else {\r
+    PrevImageList->Next = ImageList->Next;\r
+  }\r
+  //\r
+  // Now free up the image list element\r
+  //\r
+  FreePool (ImageList);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EbcAddImageThunk (\r
+  IN EFI_HANDLE      ImageHandle,\r
+  IN VOID            *ThunkBuffer,\r
+  IN UINT32          ThunkSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Add a thunk to our list of thunks for a given image handle.\r
+  Also flush the instruction cache since we've written thunk code\r
+  to memory that will be executed eventually.\r
+\r
+Arguments:\r
+\r
+  ImageHandle - the image handle to which the thunk is tied\r
+  ThunkBuffer - the buffer we've created/allocated\r
+  ThunkSize    - the size of the thunk memory allocated\r
+\r
+Returns:\r
+\r
+  EFI_OUT_OF_RESOURCES    - memory allocation failed\r
+  EFI_SUCCESS             - successful completion\r
+\r
+--*/\r
+{\r
+  EBC_THUNK_LIST  *ThunkList;\r
+  EBC_IMAGE_LIST  *ImageList;\r
+  EFI_STATUS      Status;\r
+\r
+  //\r
+  // It so far so good, then flush the instruction cache\r
+  //\r
+  if (mEbcICacheFlush != NULL) {\r
+    Status = mEbcICacheFlush ((EFI_PHYSICAL_ADDRESS) (UINTN) ThunkBuffer, ThunkSize);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+  //\r
+  // Go through our list of known image handles and see if we've already\r
+  // created a image list element for this image handle.\r
+  //\r
+  for (ImageList = mEbcImageList; ImageList != NULL; ImageList = ImageList->Next) {\r
+    if (ImageList->ImageHandle == ImageHandle) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (ImageList == NULL) {\r
+    //\r
+    // Allocate a new one\r
+    //\r
+    ImageList = AllocatePool (sizeof (EBC_IMAGE_LIST));\r
+\r
+    if (ImageList == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    ImageList->ThunkList    = NULL;\r
+    ImageList->ImageHandle  = ImageHandle;\r
+    ImageList->Next         = mEbcImageList;\r
+    mEbcImageList           = ImageList;\r
+  }\r
+  //\r
+  // Ok, now create a new thunk element to add to the list\r
+  //\r
+  ThunkList = AllocatePool (sizeof (EBC_THUNK_LIST));\r
+\r
+  if (ThunkList == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Add it to the head of the list\r
+  //\r
+  ThunkList->Next         = ImageList->ThunkList;\r
+  ThunkList->ThunkBuffer  = ThunkBuffer;\r
+  ImageList->ThunkList    = ThunkList;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcRegisterICacheFlush (\r
+  IN EFI_EBC_PROTOCOL   *This,\r
+  IN EBC_ICACHE_FLUSH   Flush\r
+  )\r
+{\r
+  mEbcICacheFlush = Flush;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+EbcGetVersion (\r
+  IN EFI_EBC_PROTOCOL   *This,\r
+  IN OUT UINT64         *Version\r
+  )\r
+{\r
+  if (Version == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Version = GetVmVersion ();\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+GetEBCStack(\r
+  EFI_HANDLE Handle,\r
+  VOID       **StackBuffer,\r
+  UINTN      *BufferIndex\r
+  )\r
+{\r
+  UINTN   Index;\r
+  EFI_TPL OldTpl;\r
+  OldTpl = gBS->RaiseTPL(TPL_HIGH_LEVEL);\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    if (mStackBufferIndex[Index] == NULL) {\r
+      mStackBufferIndex[Index] = Handle;\r
+      break;\r
+    }\r
+  }\r
+  gBS->RestoreTPL(OldTpl);\r
+  if (Index == mStackNum) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  *BufferIndex = Index;\r
+  *StackBuffer = mStackBuffer[Index];\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+ReturnEBCStack(\r
+  UINTN Index\r
+  )\r
+{\r
+  mStackBufferIndex[Index] =NULL;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+ReturnEBCStackByHandle(\r
+  EFI_HANDLE Handle\r
+  )\r
+{\r
+  UINTN Index;\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    if (mStackBufferIndex[Index] == Handle) {\r
+      break;\r
+    }\r
+  }\r
+  if (Index == mStackNum) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  mStackBufferIndex[Index] = NULL;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+InitEBCStack (\r
+  VOID\r
+  )\r
+{\r
+  for (mStackNum = 0; mStackNum < MAX_STACK_NUM; mStackNum ++) {\r
+    mStackBuffer[mStackNum] = AllocatePool(STACK_POOL_SIZE);\r
+    mStackBufferIndex[mStackNum] = NULL;\r
+    if (mStackBuffer[mStackNum] == NULL) {\r
+      break;\r
+    }\r
+  }\r
+  if (mStackNum == 0) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FreeEBCStack(\r
+  VOID\r
+  )\r
+{\r
+  UINTN Index;\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    FreePool(mStackBuffer[Index]);\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+STATIC\r
+EFI_STATUS\r
+InitEbcVmTestProtocol (\r
+  IN EFI_HANDLE     *IHandle\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Produce an EBC VM test protocol that can be used for regression tests.\r
+\r
+Arguments:\r
+\r
+  IHandle - handle on which to install the protocol.\r
+\r
+Returns:\r
+\r
+  EFI_OUT_OF_RESOURCES  - memory allocation failed\r
+  EFI_SUCCESS           - successful completion\r
+\r
+--*/\r
+{\r
+  EFI_HANDLE Handle;\r
+  EFI_STATUS Status;\r
+  EFI_EBC_VM_TEST_PROTOCOL *EbcVmTestProtocol;\r
+\r
+  //\r
+  // Allocate memory for the protocol, then fill in the fields\r
+  //\r
+  EbcVmTestProtocol = AllocatePool (sizeof (EFI_EBC_VM_TEST_PROTOCOL));\r
+  if (EbcVmTestProtocol == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  EbcVmTestProtocol->Execute      = (EBC_VM_TEST_EXECUTE) EbcExecuteInstructions;\r
+\r
+  DEBUG_CODE_BEGIN ();\r
+    EbcVmTestProtocol->Assemble     = (EBC_VM_TEST_ASM) EbcVmTestUnsupported;\r
+    EbcVmTestProtocol->Disassemble  = (EBC_VM_TEST_DASM) EbcVmTestUnsupported;\r
+  DEBUG_CODE_END ();\r
+\r
+  //\r
+  // Publish the protocol\r
+  //\r
+  Handle  = NULL;\r
+  Status  = gBS->InstallProtocolInterface (&Handle, &mEfiEbcVmTestProtocolGuid, EFI_NATIVE_INTERFACE, EbcVmTestProtocol);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (EbcVmTestProtocol);\r
+  }\r
+  return Status;\r
+}\r
+STATIC\r
+EFI_STATUS\r
+EbcVmTestUnsupported ()\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.h b/MdeModulePkg/Universal/EbcDxe/EbcInt.h
new file mode 100644 (file)
index 0000000..d1631f8
--- /dev/null
@@ -0,0 +1,283 @@
+/*++ \r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:  \r
+\r
+  EbcInt.h\r
+  \r
+Abstract:\r
+\r
+  Main routines for the EBC interpreter.  Includes the initialization and\r
+  main interpreter routines. \r
+  \r
+--*/\r
+\r
+#ifndef _EBC_INT_H_\r
+#define _EBC_INT_H_\r
+\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <PiDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Protocol/DebugSupport.h>\r
+#include <Protocol/Ebc.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+typedef INT64   VM_REGISTER;\r
+typedef UINT8   *VMIP;      // instruction pointer for the VM\r
+typedef UINT32  EXCEPTION_FLAGS;\r
+\r
+typedef struct {\r
+  VM_REGISTER       R[8];   // General purpose registers.\r
+  UINT64            Flags;  // Flags register:\r
+                            //   0   Set to 1 if the result of the last compare was true\r
+                            //   1  Set to 1 if stepping\r
+                            //   2..63 Reserved.\r
+  VMIP              Ip;                     // Instruction pointer.\r
+  UINTN             LastException;          //\r
+  EXCEPTION_FLAGS   ExceptionFlags;         // to keep track of exceptions\r
+  UINT32            StopFlags;\r
+  UINT32            CompilerVersion;        // via break(6)\r
+  UINTN             HighStackBottom;        // bottom of the upper stack\r
+  UINTN             LowStackTop;            // top of the lower stack\r
+  UINT64            StackRetAddr;           // location of final return address on stack\r
+  UINTN             *StackMagicPtr;         // pointer to magic value on stack to detect corruption\r
+  EFI_HANDLE        ImageHandle;            // for this EBC driver\r
+  EFI_SYSTEM_TABLE  *SystemTable;           // for debugging only\r
+  UINTN             LastAddrConverted;      // for debug\r
+  UINTN             LastAddrConvertedValue; // for debug\r
+  VOID              *FramePtr;\r
+  VOID              *EntryPoint;            // entry point of EBC image\r
+  UINTN             ImageBase;\r
+  VOID              *StackPool;\r
+  VOID              *StackTop;\r
+} VM_CONTEXT;\r
+\r
+extern VM_CONTEXT                    *mVmPtr;\r
+\r
+//\r
+// Bits of exception flags field of VM context\r
+//\r
+#define EXCEPTION_FLAG_FATAL    0x80000000  // can't continue\r
+#define EXCEPTION_FLAG_ERROR    0x40000000  // bad, but try to continue\r
+#define EXCEPTION_FLAG_WARNING  0x20000000  // harmless problem\r
+#define EXCEPTION_FLAG_NONE     0x00000000  // for normal return\r
+//\r
+// Flags passed to the internal create-thunks function.\r
+//\r
+#define FLAG_THUNK_ENTRY_POINT  0x01  // thunk for an image entry point\r
+#define FLAG_THUNK_PROTOCOL     0x00  // thunk for an EBC protocol service\r
+//\r
+// Put this value at the bottom of the VM's stack gap so we can check it on\r
+// occasion to make sure the stack has not been corrupted.\r
+//\r
+#define VM_STACK_KEY_VALUE  0xDEADBEEF\r
+\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN VOID                 *EbcEntryPoint,\r
+  OUT VOID                **Thunk,\r
+  IN UINT32               Flags\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+EbcAddImageThunk (\r
+  IN EFI_HANDLE     ImageHandle,\r
+  IN VOID           *ThunkBuffer,\r
+  IN UINT32         ThunkSize\r
+  )\r
+;\r
+\r
+//\r
+// The interpreter calls these when an exception is detected,\r
+// or as a periodic callback.\r
+//\r
+EFI_STATUS\r
+EbcDebugSignalException (\r
+  IN EFI_EXCEPTION_TYPE ExceptionType,\r
+  IN EXCEPTION_FLAGS    ExceptionFlags,\r
+  IN VM_CONTEXT         *VmPtr\r
+  )\r
+;\r
+\r
+//\r
+// Define a constant of how often to call the debugger periodic callback\r
+// function.\r
+//\r
+#define EFI_TIMER_UNIT_1MS            (1000 * 10)\r
+#define EBC_VM_PERIODIC_CALLBACK_RATE (1000 * EFI_TIMER_UNIT_1MS)\r
+#define STACK_POOL_SIZE               (1024 * 1020)\r
+#define MAX_STACK_NUM                 4\r
+\r
+EFI_STATUS\r
+EbcDebugSignalPeriodic (\r
+  IN VM_CONTEXT   *VmPtr\r
+  )\r
+;\r
+\r
+//\r
+// External low level functions that are native-processor dependent\r
+//\r
+UINTN\r
+EbcLLGetEbcEntryPoint (\r
+  VOID\r
+  )\r
+;\r
+\r
+UINTN\r
+EbcLLGetStackPointer (\r
+  VOID\r
+  )\r
+;\r
+\r
+VOID\r
+EbcLLCALLEXNative (\r
+  IN UINTN        CallAddr,\r
+  IN UINTN        EbcSp,\r
+  IN VOID         *FramePtr\r
+  )\r
+;\r
+\r
+VOID\r
+EbcLLCALLEX (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        CallAddr,\r
+  IN UINTN        EbcSp,\r
+  IN VOID         *FramePtr,\r
+  IN UINT8        Size\r
+  )\r
+;\r
+\r
+INT64\r
+EbcLLGetReturnValue (\r
+  VOID\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+GetEBCStack(\r
+  EFI_HANDLE Handle,\r
+  VOID       **StackBuffer,\r
+  UINTN      *BufferIndex\r
+  );\r
+\r
+EFI_STATUS\r
+ReturnEBCStack(\r
+  UINTN Index\r
+  );\r
+\r
+EFI_STATUS\r
+InitEBCStack (\r
+  VOID\r
+  );\r
+\r
+EFI_STATUS\r
+FreeEBCStack(\r
+  VOID\r
+  );\r
+\r
+EFI_STATUS\r
+ReturnEBCStackByHandle(\r
+  EFI_HANDLE Handle\r
+  );\r
+//\r
+// Defines for a simple EBC debugger interface\r
+//\r
+typedef struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL;\r
+\r
+#define EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL_GUID \\r
+  { \\r
+    0x2a72d11e, 0x7376, 0x40f6, { 0x9c, 0x68, 0x23, 0xfa, 0x2f, 0xe3, 0x63, 0xf1 } \\r
+  }\r
+\r
+typedef\r
+EFI_STATUS\r
+(*EBC_DEBUGGER_SIGNAL_EXCEPTION) (\r
+  IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL           * This,\r
+  IN VM_CONTEXT                                 * VmPtr,\r
+  IN EFI_EXCEPTION_TYPE                         ExceptionType\r
+  );\r
+\r
+typedef\r
+VOID\r
+(*EBC_DEBUGGER_DEBUG) (\r
+  IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL           * This,\r
+  IN VM_CONTEXT                                 * VmPtr\r
+  );\r
+\r
+typedef\r
+UINT32\r
+(*EBC_DEBUGGER_DASM) (\r
+  IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL           * This,\r
+  IN VM_CONTEXT                                 * VmPtr,\r
+  IN UINT16                                     *DasmString OPTIONAL,\r
+  IN UINT32                                     DasmStringSize\r
+  );\r
+\r
+//\r
+// This interface allows you to configure the EBC debug support\r
+// driver. For example, turn on or off saving and printing of\r
+// delta VM even if called. Or to even disable the entire interface,\r
+// in which case all functions become no-ops.\r
+//\r
+typedef\r
+EFI_STATUS\r
+(*EBC_DEBUGGER_CONFIGURE) (\r
+  IN EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL           * This,\r
+  IN UINT32                                     ConfigId,\r
+  IN UINTN                                      ConfigValue\r
+  );\r
+\r
+//\r
+// Prototype for the actual EBC debug support protocol interface\r
+//\r
+struct _EFI_EBC_SIMPLE_DEBUGGER_PROTOCOL {\r
+  EBC_DEBUGGER_DEBUG            Debugger;\r
+  EBC_DEBUGGER_SIGNAL_EXCEPTION SignalException;\r
+  EBC_DEBUGGER_DASM             Dasm;\r
+  EBC_DEBUGGER_CONFIGURE        Configure;\r
+};\r
+\r
+typedef struct {\r
+  EFI_EBC_PROTOCOL  *This;\r
+  VOID              *EntryPoint;\r
+  EFI_HANDLE        ImageHandle;\r
+  VM_CONTEXT        VmContext;\r
+} EFI_EBC_THUNK_DATA;\r
+\r
+#define EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE EFI_SIGNATURE_32 ('e', 'b', 'c', 'p')\r
+\r
+struct _EBC_PROTOCOL_PRIVATE_DATA {\r
+  UINT32            Signature;\r
+  EFI_EBC_PROTOCOL  EbcProtocol;\r
+  UINTN             StackBase;\r
+  UINTN             StackTop;\r
+  UINTN             StackSize;\r
+} ;\r
+\r
+#define EBC_PROTOCOL_PRIVATE_DATA_FROM_THIS(a) \\r
+      CR(a, EBC_PROTOCOL_PRIVATE_DATA, EbcProtocol, EBC_PROTOCOL_PRIVATE_DATA_SIGNATURE)\r
+\r
+\r
+#endif // #ifndef _EBC_INT_H_\r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.S b/MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.S
new file mode 100644 (file)
index 0000000..5e9828d
--- /dev/null
@@ -0,0 +1,54 @@
+#****************************************************************************\r
+#*                                                                         \r
+#*  Copyright (c) 2006, Intel Corporation                                                         \r
+#*  All rights reserved. This program and the accompanying materials                          \r
+#*  are licensed and made available under the terms and conditions of the BSD License         \r
+#*  which accompanies this distribution.  The full text of the license may be found at        \r
+#*  http://opensource.org/licenses/bsd-license.php                                            \r
+#*                                                                                            \r
+#*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+#*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+#*                                                                          \r
+#****************************************************************************\r
+.globl ASM_PFX(CopyMem)\r
+\r
+.globl ASM_PFX(EbcLLCALLEXNative)\r
+ASM_PFX(EbcLLCALLEXNative):\r
+         push   %ebp\r
+         push   %ebx\r
+         mov    %esp,%ebp\r
+         mov    0xc(%esp),%ecx\r
+         mov    0x14(%esp),%eax\r
+         mov    0x10(%esp),%edx\r
+         sub    %edx,%eax\r
+         sub    %eax,%esp\r
+         mov    %esp,%ebx\r
+         push   %ecx\r
+         push   %eax\r
+         push   %edx\r
+         push   %ebx\r
+         call   ASM_PFX(CopyMem)\r
+         pop    %eax\r
+         pop    %eax\r
+         pop    %eax\r
+         pop    %ecx\r
+         call   *%ecx\r
+         mov    %ebp,%esp\r
+         mov    %ebp,%esp\r
+         pop    %ebx\r
+         pop    %ebp\r
+         ret    \r
+\r
+.globl ASM_PFX(EbcLLGetEbcEntryPoint)\r
+ASM_PFX(EbcLLGetEbcEntryPoint):\r
+        ret    \r
+\r
+.globl ASM_PFX(EbcLLGetStackPointer)\r
+ASM_PFX(EbcLLGetStackPointer):\r
+        mov    %esp,%eax\r
+        add    $0x4,%eax\r
+        ret    \r
+\r
+.globl ASM_PFX(EbcLLGetReturnValue)\r
+ASM_PFX(EbcLLGetReturnValue):\r
+        ret    \r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.asm b/MdeModulePkg/Universal/EbcDxe/Ia32/EbcLowLevel.asm
new file mode 100644 (file)
index 0000000..4249241
--- /dev/null
@@ -0,0 +1,163 @@
+  page    ,132\r
+  title   VM ASSEMBLY LANGUAGE ROUTINES\r
+;****************************************************************************\r
+;*                                                                         \r
+;*  Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+;*  All rights reserved. This program and the accompanying materials                          \r
+;*  are licensed and made available under the terms and conditions of the BSD License         \r
+;*  which accompanies this distribution.  The full text of the license may be found at        \r
+;*  http://opensource.org/licenses/bsd-license.php                                            \r
+;*                                                                                            \r
+;*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+;*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+;*                                                                          \r
+;****************************************************************************\r
+;****************************************************************************\r
+;                                   REV 1.0\r
+;****************************************************************************\r
+;\r
+; Rev  Date      Description\r
+; ---  --------  ------------------------------------------------------------\r
+; 1.0  03/14/01  Initial creation of file.\r
+;\r
+;****************************************************************************\r
+                             \r
+;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+; This code provides low level routines that support the Virtual Machine\r
+; for option ROMs. \r
+;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+\r
+;---------------------------------------------------------------------------\r
+; Equate files needed.\r
+;---------------------------------------------------------------------------\r
+\r
+.XLIST\r
+\r
+.LIST\r
+\r
+;---------------------------------------------------------------------------\r
+; Assembler options\r
+;---------------------------------------------------------------------------\r
+\r
+.686p\r
+.model  flat        \r
+.code        \r
+;---------------------------------------------------------------------------\r
+;;GenericPostSegment      SEGMENT USE16\r
+;---------------------------------------------------------------------------\r
+CopyMem  PROTO  C Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
+\r
+;****************************************************************************\r
+; EbcLLCALLEXNative\r
+;\r
+; This function is called to execute an EBC CALLEX instruction\r
+; to native code. \r
+; This instruction requires that we thunk out to external native\r
+; code. For IA32, we simply switch stacks and jump to the \r
+; specified function. On return, we restore the stack pointer\r
+; to its original location.\r
+;\r
+; Destroys no working registers.\r
+;****************************************************************************\r
+; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
+_EbcLLCALLEXNative        PROC    NEAR    PUBLIC\r
+      push   ebp\r
+      push   ebx\r
+      mov    ebp, esp              ; standard function prolog\r
+      \r
+      ; Get function address in a register\r
+      ; mov ecx, FuncAddr => mov ecx, dword ptr [FuncAddr]\r
+      mov    ecx, dword ptr [esp]+0Ch\r
+      \r
+      ; Set stack pointer to new value\r
+      ; mov eax, NewStackPointer => mov eax, dword ptr [NewSp]\r
+      mov    eax, dword ptr [esp] + 14h\r
+      mov    edx, dword ptr [esp] + 10h\r
+      sub    eax, edx\r
+      sub    esp, eax      \r
+      mov    ebx, esp\r
+      push   ecx\r
+      push   eax\r
+      push   edx\r
+      push   ebx\r
+      call   CopyMem\r
+      pop    eax\r
+      pop    eax\r
+      pop    eax\r
+      pop    ecx\r
+\r
+      ; Now call the external routine\r
+      call  ecx\r
+      \r
+      ; ebp is preserved by the callee. In this function it\r
+      ; equals the original esp, so set them equal\r
+      mov    esp, ebp\r
+\r
+      ; Standard function epilog\r
+      mov      esp, ebp\r
+      pop      ebx\r
+      pop      ebp\r
+      ret\r
+_EbcLLCALLEXNative    ENDP\r
+\r
+\r
+; UINTN EbcLLGetEbcEntryPoint(VOID);\r
+; Routine Description:\r
+;   The VM thunk code stuffs an EBC entry point into a processor\r
+;   register. Since we can't use inline assembly to get it from\r
+;   the interpreter C code, stuff it into the return value \r
+;   register and return.\r
+;\r
+; Arguments:\r
+;     None.\r
+;\r
+; Returns:\r
+;     The contents of the register in which the entry point is passed.\r
+;\r
+_EbcLLGetEbcEntryPoint        PROC    NEAR    PUBLIC\r
+    ret\r
+_EbcLLGetEbcEntryPoint    ENDP\r
+\r
+;/*++\r
+;\r
+;Routine Description:\r
+;  \r
+;  Return the caller's value of the stack pointer.\r
+;\r
+;Arguments:\r
+;\r
+;  None.\r
+;\r
+;Returns:\r
+;\r
+;  The current value of the stack pointer for the caller. We\r
+;  adjust it by 4 here because when they called us, the return address\r
+;  is put on the stack, thereby lowering it by 4 bytes.\r
+;\r
+;--*/\r
+\r
+; UINTN EbcLLGetStackPointer()            \r
+_EbcLLGetStackPointer        PROC    NEAR    PUBLIC\r
+    mov    eax, esp      ; get current stack pointer\r
+    add   eax, 4        ; stack adjusted by this much when we were called\r
+    ret\r
+_EbcLLGetStackPointer    ENDP\r
+\r
+; UINT64 EbcLLGetReturnValue(VOID);\r
+; Routine Description:\r
+;   When EBC calls native, on return the VM has to stuff the return\r
+;   value into a VM register. It's assumed here that the value is still\r
+;    in the register, so simply return and the caller should get the\r
+;   return result properly.\r
+;\r
+; Arguments:\r
+;     None.\r
+;\r
+; Returns:\r
+;     The unmodified value returned by the native code.\r
+;\r
+_EbcLLGetReturnValue   PROC    NEAR    PUBLIC\r
+    ret\r
+_EbcLLGetReturnValue    ENDP\r
+\r
+END\r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ia32/EbcSupport.c b/MdeModulePkg/Universal/EbcDxe/Ia32/EbcSupport.c
new file mode 100644 (file)
index 0000000..a534e00
--- /dev/null
@@ -0,0 +1,545 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  EbcSupport.c\r
+\r
+Abstract:\r
+\r
+  This module contains EBC support routines that are customized based on\r
+  the target processor.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+//\r
+// NOTE: This is the stack size allocated for the interpreter\r
+//       when it executes an EBC image. The requirements can change\r
+//       based on whether or not a debugger is present, and other\r
+//       platform-specific configurations.\r
+//\r
+#define VM_STACK_SIZE   (1024 * 4)\r
+#define EBC_THUNK_SIZE  32\r
+\r
+#define STACK_REMAIN_SIZE (1024 * 4)\r
+VOID\r
+EbcLLCALLEX (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        FuncAddr,\r
+  IN UINTN        NewStackPointer,\r
+  IN VOID         *FramePtr,\r
+  IN UINT8        Size\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function is called to execute an EBC CALLEX instruction. \r
+  The function check the callee's content to see whether it is common native\r
+  code or a thunk to another piece of EBC code.\r
+  If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
+  otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
+  be startup which cost time and stack space.\r
+  \r
+Arguments:\r
+\r
+  VmPtr             - Pointer to a VM context.\r
+  FuncAddr          - Callee's address\r
+  NewStackPointer   - New stack pointer after the call\r
+  FramePtr          - New frame pointer after the call\r
+  Size              - The size of call instruction\r
+\r
+Returns:\r
+\r
+  None.\r
+  \r
+--*/\r
+{\r
+  UINTN    IsThunk;\r
+  UINTN    TargetEbcAddr;\r
+\r
+  IsThunk       = 1;\r
+  TargetEbcAddr = 0;\r
+\r
+  //\r
+  // Processor specific code to check whether the callee is a thunk to EBC.\r
+  //\r
+  if (*((UINT8 *)FuncAddr) != 0xB8) {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 1) != 0xBC)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 2) != 0x2E)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 3) != 0x11)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 4) != 0xCA)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 5) != 0xB8)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 10) != 0xB9)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 15) != 0xFF)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 16) != 0xE1)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+\r
+  TargetEbcAddr = ((UINTN)(*((UINT8 *)FuncAddr + 9)) << 24) + ((UINTN)(*((UINT8 *)FuncAddr + 8)) << 16) +\r
+                    ((UINTN)(*((UINT8 *)FuncAddr + 7)) << 8) + ((UINTN)(*((UINT8 *)FuncAddr + 6)));\r
+\r
+Action:\r
+  if (IsThunk == 1){\r
+    //\r
+    // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
+    // put our return address and frame pointer on the VM stack.\r
+    // Then set the VM's IP to new EBC code.\r
+    //\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (UINTN) (VmPtr->Ip + Size));\r
+\r
+    VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+  } else {\r
+    //\r
+    // The callee is not a thunk to EBC, call native code.\r
+    //\r
+    EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+    \r
+    //\r
+    // Get return value and advance the IP.\r
+    //\r
+    VmPtr->R[7] = EbcLLGetReturnValue ();\r
+    VmPtr->Ip += Size;\r
+  }\r
+}\r
+\r
+STATIC\r
+UINT64\r
+EbcInterpret (\r
+  IN OUT UINTN      Arg1,\r
+  IN OUT UINTN      Arg2,\r
+  IN OUT UINTN      Arg3,\r
+  IN OUT UINTN      Arg4,\r
+  IN OUT UINTN      Arg5,\r
+  IN OUT UINTN      Arg6,\r
+  IN OUT UINTN      Arg7,\r
+  IN OUT UINTN      Arg8,\r
+  IN OUT UINTN      Arg9,\r
+  IN OUT UINTN      Arg10,\r
+  IN OUT UINTN      Arg11,\r
+  IN OUT UINTN      Arg12,\r
+  IN OUT UINTN      Arg13,\r
+  IN OUT UINTN      Arg14,\r
+  IN OUT UINTN      Arg15,\r
+  IN OUT UINTN      Arg16\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Begin executing an EBC image. The address of the entry point is passed\r
+  in via a processor register, so we'll need to make a call to get the\r
+  value.\r
+  \r
+Arguments:\r
+\r
+  None. Since we're called from a fixed up thunk (which we want to keep\r
+  small), our only so-called argument is the EBC entry point passed in\r
+  to us in a processor register.\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+  \r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+\r
+  //\r
+  // Get the EBC entry point from the processor register.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+  //\r
+  // Initialize the stack pointer for the EBC. Get the current system stack\r
+  // pointer and adjust it down by the max needed for the interpreter.\r
+  //\r
+\r
+  //\r
+  // Align the stack on a natural boundary\r
+  //\r
+\r
+  //\r
+  // Allocate stack pool\r
+  //\r
+  Status = GetEBCStack((EFI_HANDLE)-1, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
+  VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  //\r
+  // Put a magic value in the stack gap, then adjust down again\r
+  //\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.R[0];\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // For IA32, this is where we say our return address is\r
+  //\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg16;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg15;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg14;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg13;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg12;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg11;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg10;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg9;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg8;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg7;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg6;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg5;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg4;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg3;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg2;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) Arg1;\r
+  VmContext.R[0] -= 16;\r
+  VmContext.StackRetAddr  = (UINT64) VmContext.R[0];\r
+\r
+  //\r
+  // We need to keep track of where the EBC stack starts. This way, if the EBC\r
+  // accesses any stack variables above its initial stack setting, then we know\r
+  // it's accessing variables passed into it, which means the data is on the\r
+  // VM's stack.\r
+  // When we're called, on the stack (high to low) we have the parameters, the\r
+  // return address, then the saved ebp. Save the pointer to the return address.\r
+  // EBC code knows that's there, so should look above it for function parameters.\r
+  // The offset is the size of locals (VMContext + Addr + saved ebp).\r
+  // Note that the interpreter assumes there is a 16 bytes of return address on\r
+  // the stack too, so adjust accordingly.\r
+  //  VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
+  //\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEbcImageEntryPoint (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Begin executing an EBC image. The address of the entry point is passed\r
+  in via a processor register, so we'll need to make a call to get the\r
+  value.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle   - image handle for the EBC application we're executing\r
+  SystemTable   - standard system table passed into an driver's entry point\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+\r
+  //\r
+  // Get the EBC entry point from the processor register. Make sure you don't\r
+  // call any functions before this or you could mess up the register the\r
+  // entry point is passed in.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  //  Print(L"*** Thunked into EBC entry point - ImageHandle = 0x%X\n", (UINTN)ImageHandle);\r
+  //  Print(L"EBC entry point is 0x%X\n", (UINT32)(UINTN)Addr);\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+  //\r
+  // Save the image handle so we can track the thunks created for this image\r
+  //\r
+  VmContext.ImageHandle = ImageHandle;\r
+  VmContext.SystemTable = SystemTable;\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+\r
+  //\r
+  // Initialize the stack pointer for the EBC. Get the current system stack\r
+  // pointer and adjust it down by the max needed for the interpreter.\r
+  //\r
+\r
+  //\r
+  // Allocate stack pool\r
+  //\r
+  Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64)(UINTN) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN)VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  \r
+  //\r
+  // Put a magic value in the stack gap, then adjust down again\r
+  //\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // Align the stack on a natural boundary\r
+  //  VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
+  //\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) SystemTable;\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) ImageHandle;\r
+\r
+  VmContext.R[0] -= 16; \r
+  VmContext.StackRetAddr  = (UINT64) VmContext.R[0];\r
+  //\r
+  // VM pushes 16-bytes for return address. Simulate that here.\r
+  //\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN VOID                 *EbcEntryPoint,\r
+  OUT VOID                **Thunk,\r
+  IN  UINT32              Flags\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Create an IA32 thunk for the given EBC entry point.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle     - Handle of image for which this thunk is being created\r
+  EbcEntryPoint   - Address of the EBC code that the thunk is to call\r
+  Thunk           - Returned thunk we create here\r
+\r
+Returns:\r
+\r
+  Standard EFI status.\r
+  \r
+--*/\r
+{\r
+  UINT8       *Ptr;\r
+  UINT8       *ThunkBase;\r
+  UINT32      I;\r
+  UINT32      Addr;\r
+  INT32       Size;\r
+  INT32       ThunkSize;\r
+\r
+  //\r
+  // Check alignment of pointer to EBC code\r
+  //\r
+  if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Size      = EBC_THUNK_SIZE;\r
+  ThunkSize = Size;\r
+\r
+  Ptr = AllocatePool (Size);\r
+\r
+  if (Ptr == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  //  Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
+  //\r
+  // Save the start address so we can add a pointer to it to a list later.\r
+  //\r
+  ThunkBase = Ptr;\r
+\r
+  //\r
+  // Give them the address of our buffer we're going to fix up\r
+  //\r
+  *Thunk = (VOID *) Ptr;\r
+\r
+  //\r
+  // Add a magic code here to help the VM recognize the thunk..\r
+  // mov eax, 0xca112ebc  => B8 BC 2E 11 CA\r
+  //\r
+  *Ptr = 0xB8;\r
+  Ptr++;\r
+  Size--;\r
+  Addr = (UINT32) 0xCA112EBC;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) (UINTN) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+\r
+  //\r
+  // Add code bytes to load up a processor register with the EBC entry point.\r
+  // mov eax, 0xaa55aa55  => B8 55 AA 55 AA\r
+  // The first 8 bytes of the thunk entry is the address of the EBC\r
+  // entry point.\r
+  //\r
+  *Ptr = 0xB8;\r
+  Ptr++;\r
+  Size--;\r
+  Addr = (UINT32) EbcEntryPoint;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) (UINTN) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+  //\r
+  // Stick in a load of ecx with the address of appropriate VM function.\r
+  //  mov ecx 12345678h  => 0xB9 0x78 0x56 0x34 0x12\r
+  //\r
+  if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
+    Addr = (UINT32) (UINTN) ExecuteEbcImageEntryPoint;\r
+  } else {\r
+    Addr = (UINT32) (UINTN) EbcInterpret;\r
+  }\r
+\r
+  //\r
+  // MOV ecx\r
+  //\r
+  *Ptr = 0xB9;\r
+  Ptr++;\r
+  Size--;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+  //\r
+  // Stick in jump opcode bytes for jmp ecx => 0xFF 0xE1\r
+  //\r
+  *Ptr = 0xFF;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xE1;\r
+  Size--;\r
+\r
+  //\r
+  // Double check that our defined size is ok (application error)\r
+  //\r
+  if (Size < 0) {\r
+    ASSERT (FALSE);\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  //\r
+  // Add the thunk to the list for this image. Do this last since the add\r
+  // function flushes the cache for us.\r
+  //\r
+  EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ipf/EbcLowLevel.s b/MdeModulePkg/Universal/EbcDxe/Ipf/EbcLowLevel.s
new file mode 100644 (file)
index 0000000..e329b68
--- /dev/null
@@ -0,0 +1,197 @@
+//++\r
+// Copyright (c) 2006, Intel Corporation                                                         \r
+// All rights reserved. This program and the accompanying materials                          \r
+// are licensed and made available under the terms and conditions of the BSD License         \r
+// which accompanies this distribution.  The full text of the license may be found at        \r
+// http://opensource.org/licenses/bsd-license.php                                            \r
+//                                                                                           \r
+// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+// \r
+// Module Name:\r
+//\r
+//   EbcLowLevel.s\r
+//\r
+// Abstract:\r
+//\r
+//   Contains low level routines for the Virtual Machine implementation\r
+//   on an Itanium-based platform.\r
+//\r
+//\r
+//--\r
+\r
+.file  "EbcLowLevel.s"\r
+\r
+#define PROCEDURE_ENTRY(name)   .##text;            \\r
+                                .##type name, @function;    \\r
+                                .##proc name;           \\r
+name::\r
+\r
+#define PROCEDURE_EXIT(name)    .##endp name\r
+\r
+// Note: use of NESTED_SETUP requires number of locals (l) >= 3\r
+\r
+#define NESTED_SETUP(i,l,o,r) \\r
+         alloc loc1=ar##.##pfs,i,l,o,r ;\\r
+         mov loc0=b0\r
+\r
+#define NESTED_RETURN \\r
+         mov b0=loc0 ;\\r
+         mov ar##.##pfs=loc1 ;;\\r
+         br##.##ret##.##dpnt  b0;;\r
+\r
+.type CopyMem, @function;  \r
+\r
+//-----------------------------------------------------------------------------\r
+//++\r
+// EbcAsmLLCALLEX\r
+//\r
+//  Implements the low level EBC CALLEX instruction. Sets up the\r
+//  stack pointer, does the spill of function arguments, and\r
+//  calls the native function. On return it restores the original\r
+//  stack pointer and returns to the caller.\r
+//\r
+// Arguments : \r
+//\r
+// On Entry :\r
+//    in0 = Address of native code to call\r
+//    in1 = New stack pointer\r
+//\r
+// Return Value: \r
+// \r
+// As per static calling conventions. \r
+// \r
+//--\r
+//---------------------------------------------------------------------------\r
+;// void EbcAsmLLCALLEX (UINTN FunctionAddr, UINTN EbcStackPointer)\r
+PROCEDURE_ENTRY(EbcAsmLLCALLEX)\r
+  NESTED_SETUP (2,6,8,0)\r
+\r
+  // NESTED_SETUP uses loc0 and loc1 for context save\r
+\r
+  //\r
+  // Save a copy of the EBC VM stack pointer\r
+  //\r
+  mov r8 = in1;;\r
+\r
+  //\r
+  // Copy stack arguments from EBC stack into registers. \r
+  // Assume worst case and copy 8.\r
+  //\r
+  ld8   out0 = [r8], 8;;\r
+  ld8   out1 = [r8], 8;;\r
+  ld8   out2 = [r8], 8;;\r
+  ld8   out3 = [r8], 8;;\r
+  ld8   out4 = [r8], 8;;\r
+  ld8   out5 = [r8], 8;;\r
+  ld8   out6 = [r8], 8;;\r
+  ld8   out7 = [r8], 8;;\r
+\r
+  //\r
+  // Save the original stack pointer\r
+  //\r
+  mov   loc2 = r12;\r
+\r
+  //\r
+  // Save the gp\r
+  //\r
+  or    loc3 = r1, r0\r
+\r
+  //\r
+  // Set the new aligned stack pointer. Reserve space for the required \r
+  // 16-bytes of scratch area as well.\r
+  //\r
+  add  r12 = 48, in1\r
+\r
+  //\r
+  // Now call the function. Load up the function address from the descriptor\r
+  // pointed to by in0. Then get the gp from the descriptor at the following\r
+  // address in the descriptor.\r
+  //\r
+  ld8   r31 = [in0], 8;;\r
+  ld8   r30 = [in0];;\r
+  mov   b1 = r31\r
+  mov   r1 = r30\r
+  (p0) br.call.dptk.many b0 = b1;;\r
+\r
+  //\r
+  // Restore the original stack pointer and gp\r
+  //\r
+  mov   r12 = loc2\r
+  or    r1 = loc3, r0\r
+\r
+  //\r
+  // Now return\r
+  //\r
+  NESTED_RETURN\r
+\r
+PROCEDURE_EXIT(EbcAsmLLCALLEX)\r
+\r
+PROCEDURE_ENTRY(EbcLLCALLEXNative)\r
+  NESTED_SETUP (3,6,3,0)\r
+  \r
+  mov   loc2 = in2;;\r
+  mov   loc3 = in1;;\r
+  sub   loc2 = loc2, loc3\r
+  mov   loc4 = r12;;\r
+  or    loc5 = r1, r0\r
+  \r
+  sub   r12 = r12, loc2\r
+  mov   out2 = loc2;;\r
+\r
+  and   r12 = -0x10, r12\r
+  mov   out1 = in1;;\r
+  mov   out0 = r12;;\r
+  adds  r12 = -0x8, r12\r
+  (p0) br.call.dptk.many b0 = CopyMem;;\r
+  adds  r12 = 0x8, r12\r
\r
+  mov   out0 = in0;;\r
+  mov   out1 = r12;;\r
+  (p0) br.call.dptk.many b0 = EbcAsmLLCALLEX;;\r
+  mov   r12 = loc4;;\r
+  or    r1 = loc5, r0\r
+  \r
+  NESTED_RETURN\r
+PROCEDURE_EXIT(EbcLLCALLEXNative)\r
+\r
+\r
+//\r
+// UINTN EbcLLGetEbcEntryPoint(VOID)\r
+//\r
+// Description:\r
+//    Simply return, so that the caller retrieves the return register\r
+//    contents (R8). That's where the thunk-to-ebc code stuffed the\r
+//    EBC entry point.\r
+//\r
+PROCEDURE_ENTRY(EbcLLGetEbcEntryPoint)\r
+    br.ret.sptk  b0 ;;\r
+PROCEDURE_EXIT(EbcLLGetEbcEntryPoint)\r
+\r
+//\r
+// INT64 EbcLLGetReturnValue(VOID)\r
+//\r
+// Description:\r
+//    This function is called to get the value returned by native code\r
+//     to EBC. It simply returns because the return value should still\r
+//    be in the register, so the caller just gets the unmodified value.\r
+//\r
+PROCEDURE_ENTRY(EbcLLGetReturnValue)\r
+    br.ret.sptk  b0 ;;\r
+PROCEDURE_EXIT(EbcLLGetReturnValue)\r
+\r
+//\r
+// UINTN EbcLLGetStackPointer(VOID)\r
+//\r
+PROCEDURE_ENTRY(EbcLLGetStackPointer)\r
+    mov    r8 = r12 ;;\r
+    br.ret.sptk  b0 ;;\r
+    br.sptk.few b6 \r
+PROCEDURE_EXIT(EbcLLGetStackPointer)\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c b/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.c
new file mode 100644 (file)
index 0000000..3647a12
--- /dev/null
@@ -0,0 +1,869 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  EbcSupport.c\r
+\r
+Abstract:\r
+\r
+  This module contains EBC support routines that are customized based on\r
+  the target processor.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+#include "EbcSupport.h"\r
+\r
+STATIC\r
+EFI_STATUS\r
+WriteBundle (\r
+  IN    VOID    *MemPtr,\r
+  IN    UINT8   Template,\r
+  IN    UINT64  Slot0,\r
+  IN    UINT64  Slot1,\r
+  IN    UINT64  Slot2\r
+  );\r
+\r
+STATIC\r
+VOID\r
+PushU64 (\r
+  VM_CONTEXT *VmPtr,\r
+  UINT64     Arg\r
+  )\r
+{\r
+  //\r
+  // Advance the VM stack down, and then copy the argument to the stack.\r
+  // Hope it's aligned.\r
+  //\r
+  VmPtr->R[0] -= sizeof (UINT64);\r
+  *(UINT64 *) VmPtr->R[0] = Arg;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+EbcInterpret (\r
+  UINT64      Arg1,\r
+  ...\r
+  )\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+  VA_LIST     List;\r
+  UINT64      Arg2;\r
+  UINT64      Arg3;\r
+  UINT64      Arg4;\r
+  UINT64      Arg5;\r
+  UINT64      Arg6;\r
+  UINT64      Arg7;\r
+  UINT64      Arg8;\r
+  UINT64      Arg9;\r
+  UINT64      Arg10;\r
+  UINT64      Arg11;\r
+  UINT64      Arg12;\r
+  UINT64      Arg13;\r
+  UINT64      Arg14;\r
+  UINT64      Arg15;\r
+  UINT64      Arg16;\r
+  //\r
+  // Get the EBC entry point from the processor register. Make sure you don't\r
+  // call any functions before this or you could mess up the register the\r
+  // entry point is passed in.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+  //\r
+  // Need the args off the stack.\r
+  //\r
+  VA_START (List, Arg1);\r
+  Arg2      = VA_ARG (List, UINT64);\r
+  Arg3      = VA_ARG (List, UINT64);\r
+  Arg4      = VA_ARG (List, UINT64);\r
+  Arg5      = VA_ARG (List, UINT64);\r
+  Arg6      = VA_ARG (List, UINT64);\r
+  Arg7      = VA_ARG (List, UINT64);\r
+  Arg8      = VA_ARG (List, UINT64);\r
+  Arg9      = VA_ARG (List, UINT64);\r
+  Arg10     = VA_ARG (List, UINT64);\r
+  Arg11     = VA_ARG (List, UINT64);\r
+  Arg12     = VA_ARG (List, UINT64);\r
+  Arg13     = VA_ARG (List, UINT64);\r
+  Arg14     = VA_ARG (List, UINT64);\r
+  Arg15     = VA_ARG (List, UINT64);\r
+  Arg16     = VA_ARG (List, UINT64);\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+  //\r
+  // Initialize the stack pointer for the EBC. Get the current system stack\r
+  // pointer and adjust it down by the max needed for the interpreter.\r
+  //\r
+  //\r
+  // NOTE: Eventually we should have the interpreter allocate memory\r
+  //       for stack space which it will use during its execution. This\r
+  //       would likely improve performance because the interpreter would\r
+  //       no longer be required to test each memory access and adjust\r
+  //       those reading from the stack gap.\r
+  //\r
+  // For IPF, the stack looks like (assuming 10 args passed)\r
+  //   arg10\r
+  //   arg9       (Bottom of high stack)\r
+  //   [ stack gap for interpreter execution ]\r
+  //   [ magic value for detection of stack corruption ]\r
+  //   arg8       (Top of low stack)\r
+  //   arg7....\r
+  //   arg1\r
+  //   [ 64-bit return address ]\r
+  //   [ ebc stack ]\r
+  // If the EBC accesses memory in the stack gap, then we assume that it's\r
+  // actually trying to access args9 and greater. Therefore we need to\r
+  // adjust memory accesses in this region to point above the stack gap.\r
+  //\r
+  //\r
+  // Now adjust the EBC stack pointer down to leave a gap for interpreter\r
+  // execution. Then stuff a magic value there.\r
+  //\r
+  \r
+  Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  \r
+  PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
+  VmContext.StackMagicPtr = (UINTN *) VmContext.R[0];\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+  //\r
+  // Push the EBC arguments on the stack. Does not matter that they may not\r
+  // all be valid.\r
+  //\r
+  PushU64 (&VmContext, Arg16);\r
+  PushU64 (&VmContext, Arg15);\r
+  PushU64 (&VmContext, Arg14);\r
+  PushU64 (&VmContext, Arg13);\r
+  PushU64 (&VmContext, Arg12);\r
+  PushU64 (&VmContext, Arg11);\r
+  PushU64 (&VmContext, Arg10);\r
+  PushU64 (&VmContext, Arg9);\r
+  PushU64 (&VmContext, Arg8);\r
+  PushU64 (&VmContext, Arg7);\r
+  PushU64 (&VmContext, Arg6);\r
+  PushU64 (&VmContext, Arg5);\r
+  PushU64 (&VmContext, Arg4);\r
+  PushU64 (&VmContext, Arg3);\r
+  PushU64 (&VmContext, Arg2);\r
+  PushU64 (&VmContext, Arg1);\r
+  //\r
+  // Push a bogus return address on the EBC stack because the\r
+  // interpreter expects one there. For stack alignment purposes on IPF,\r
+  // EBC return addresses are always 16 bytes. Push a bogus value as well.\r
+  //\r
+  PushU64 (&VmContext, 0);\r
+  PushU64 (&VmContext, 0xDEADBEEFDEADBEEF);\r
+  VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEbcImageEntryPoint (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  IPF implementation.\r
+\r
+  Begin executing an EBC image. The address of the entry point is passed\r
+  in via a processor register, so we'll need to make a call to get the\r
+  value.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle   - image handle for the EBC application we're executing\r
+  SystemTable   - standard system table passed into an driver's entry point\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+\r
+  //\r
+  // Get the EBC entry point from the processor register. Make sure you don't\r
+  // call any functions before this or you could mess up the register the\r
+  // entry point is passed in.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+  //\r
+  // Save the image handle so we can track the thunks created for this image\r
+  //\r
+  VmContext.ImageHandle = ImageHandle;\r
+  VmContext.SystemTable = SystemTable;\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+\r
+  //\r
+  // Get the stack pointer. This is the bottom of the upper stack.\r
+  //\r
+  Addr                      = EbcLLGetStackPointer ();\r
+  \r
+  Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  \r
+  //\r
+  // Allocate stack space for the interpreter. Then put a magic value\r
+  // at the bottom so we can detect stack corruption.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) VM_STACK_KEY_VALUE);\r
+  VmContext.StackMagicPtr = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // When we thunk to external native code, we copy the last 8 qwords from\r
+  // the EBC stack into the processor registers, and adjust the stack pointer\r
+  // up. If the caller is not passing 8 parameters, then we've moved the\r
+  // stack pointer up into the stack gap. If this happens, then the caller\r
+  // can mess up the stack gap contents (in particular our magic value).\r
+  // Therefore, leave another gap below the magic value. Pick 10 qwords down,\r
+  // just as a starting point.\r
+  //\r
+  VmContext.R[0] -= 10 * sizeof (UINT64);\r
+\r
+  //\r
+  // Align the stack pointer such that after pushing the system table,\r
+  // image handle, and return address on the stack, it's aligned on a 16-byte\r
+  // boundary as required for IPF.\r
+  //\r
+  VmContext.R[0] &= (INT64)~0x0f;\r
+  VmContext.LowStackTop = (UINTN) VmContext.R[0];\r
+  //\r
+  // Simply copy the image handle and system table onto the EBC stack.\r
+  // Greatly simplifies things by not having to spill the args\r
+  //\r
+  PushU64 (&VmContext, (UINT64) SystemTable);\r
+  PushU64 (&VmContext, (UINT64) ImageHandle);\r
+\r
+  //\r
+  // Interpreter assumes 64-bit return address is pushed on the stack.\r
+  // IPF does not do this so pad the stack accordingly. Also, a\r
+  // "return address" is 16 bytes as required for IPF stack alignments.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) 0);\r
+  PushU64 (&VmContext, (UINT64) 0x1234567887654321);\r
+  VmContext.StackRetAddr = (UINT64) VmContext.R[0];\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+  IN EFI_HANDLE   ImageHandle,\r
+  IN VOID         *EbcEntryPoint,\r
+  OUT VOID        **Thunk,\r
+  IN  UINT32      Flags\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Create thunks for an EBC image entry point, or an EBC protocol service.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle     - Image handle for the EBC image. If not null, then we're\r
+                    creating a thunk for an image entry point.\r
+  EbcEntryPoint   - Address of the EBC code that the thunk is to call\r
+  Thunk           - Returned thunk we create here\r
+  Flags           - Flags indicating options for creating the thunk\r
+  \r
+Returns:\r
+\r
+  Standard EFI status.\r
+  \r
+--*/\r
+{\r
+  UINT8       *Ptr;\r
+  UINT8       *ThunkBase;\r
+  UINT64      Addr;\r
+  UINT64      Code[3];    // Code in a bundle\r
+  UINT64      RegNum;     // register number for MOVL\r
+  UINT64      I;          // bits of MOVL immediate data\r
+  UINT64      Ic;         // bits of MOVL immediate data\r
+  UINT64      Imm5c;      // bits of MOVL immediate data\r
+  UINT64      Imm9d;      // bits of MOVL immediate data\r
+  UINT64      Imm7b;      // bits of MOVL immediate data\r
+  UINT64      Br;         // branch register for loading and jumping\r
+  UINT64      *Data64Ptr;\r
+  UINT32      ThunkSize;\r
+  UINT32      Size;\r
+\r
+  //\r
+  // Check alignment of pointer to EBC code, which must always be aligned\r
+  // on a 2-byte boundary.\r
+  //\r
+  if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Allocate memory for the thunk. Make the (most likely incorrect) assumption\r
+  // that the returned buffer is not aligned, so round up to the next\r
+  // alignment size.\r
+  //\r
+  Size      = EBC_THUNK_SIZE + EBC_THUNK_ALIGNMENT - 1;\r
+  ThunkSize = Size;\r
+  Ptr = AllocatePool (Size);\r
+\r
+  if (Ptr == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Save the start address of the buffer.\r
+  //\r
+  ThunkBase = Ptr;\r
+\r
+  //\r
+  // Make sure it's aligned for code execution. If not, then\r
+  // round up.\r
+  //\r
+  if ((UINT32) (UINTN) Ptr & (EBC_THUNK_ALIGNMENT - 1)) {\r
+    Ptr = (UINT8 *) (((UINTN) Ptr + (EBC_THUNK_ALIGNMENT - 1)) &~ (UINT64) (EBC_THUNK_ALIGNMENT - 1));\r
+  }\r
+  //\r
+  // Return the pointer to the thunk to the caller to user as the\r
+  // image entry point.\r
+  //\r
+  *Thunk = (VOID *) Ptr;\r
+\r
+  //\r
+  // Clear out the thunk entry\r
+  // ZeroMem(Ptr, Size);\r
+  //\r
+  // For IPF, when you do a call via a function pointer, the function pointer\r
+  // actually points to a function descriptor which consists of a 64-bit\r
+  // address of the function, followed by a 64-bit gp for the function being\r
+  // called. See the the Software Conventions and Runtime Architecture Guide\r
+  // for details.\r
+  // So first off in our thunk, create a descriptor for our actual thunk code.\r
+  // This means we need to create a pointer to the thunk code (which follows\r
+  // the descriptor we're going to create), followed by the gp of the Vm\r
+  // interpret function we're going to eventually execute.\r
+  //\r
+  Data64Ptr = (UINT64 *) Ptr;\r
+\r
+  //\r
+  // Write the function's entry point (which is our thunk code that follows\r
+  // this descriptor we're creating).\r
+  //\r
+  *Data64Ptr = (UINT64) (Data64Ptr + 2);\r
+  //\r
+  // Get the gp from the descriptor for EbcInterpret and stuff it in our thunk\r
+  // descriptor.\r
+  //\r
+  *(Data64Ptr + 1) = *(UINT64 *) ((UINT64 *) (UINTN) EbcInterpret + 1);\r
+  //\r
+  // Advance our thunk data pointer past the descriptor. Since the\r
+  // descriptor consists of 16 bytes, the pointer is still aligned for\r
+  // IPF code execution (on 16-byte boundary).\r
+  //\r
+  Ptr += sizeof (UINT64) * 2;\r
+\r
+  //\r
+  // *************************** MAGIC BUNDLE ********************************\r
+  //\r
+  // Write magic code bundle for: movl r8 = 0xca112ebcca112ebc to help the VM\r
+  // to recognize it is a thunk.\r
+  //\r
+  Addr = (UINT64) 0xCA112EBCCA112EBC;\r
+\r
+  //\r
+  // Now generate the code bytes. First is nop.m 0x0\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // The EBC entry point will be put into r8, so r8 can be used here\r
+  // temporary. R8 is general register and is auto-serialized.\r
+  //\r
+  RegNum = 8;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] = LShiftU64 (Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** FIRST BUNDLE ********************************\r
+  //\r
+  // Write code bundle for: movl r8 = EBC_ENTRY_POINT so we pass\r
+  // the ebc entry point in to the interpreter function via a processor\r
+  // register.\r
+  // Note -- we could easily change this to pass in a pointer to a structure\r
+  // that contained, among other things, the EBC image's entry point. But\r
+  // for now pass it directly.\r
+  //\r
+  Ptr += 16;\r
+  Addr = (UINT64) EbcEntryPoint;\r
+\r
+  //\r
+  // Now generate the code bytes. First is nop.m 0x0\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // Put the EBC entry point in r8, which is the location of the return value\r
+  // for functions.\r
+  //\r
+  RegNum = 8;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] = LShiftU64 (Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Write code bundle for:\r
+  //   movl rx = offset_of(EbcInterpret|ExecuteEbcImageEntryPoint)\r
+  //\r
+  // Advance pointer to next bundle, then compute the offset from this bundle\r
+  // to the address of the entry point of the interpreter.\r
+  //\r
+  Ptr += 16;\r
+  if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
+    Addr = (UINT64) ExecuteEbcImageEntryPoint;\r
+  } else {\r
+    Addr = (UINT64) EbcInterpret;\r
+  }\r
+  //\r
+  // Indirection on Itanium-based systems\r
+  //\r
+  Addr = *(UINT64 *) Addr;\r
+\r
+  //\r
+  // Now write the code to load the offset into a register\r
+  //\r
+  Code[0] = OPCODE_NOP;\r
+\r
+  //\r
+  // Next is simply Addr[62:22] (41 bits) of the address\r
+  //\r
+  Code[1] = RShiftU64 (Addr, 22) & 0x1ffffffffff;\r
+\r
+  //\r
+  // Extract bits from the address for insertion into the instruction\r
+  // i = Addr[63:63]\r
+  //\r
+  I = RShiftU64 (Addr, 63) & 0x01;\r
+  //\r
+  // ic = Addr[21:21]\r
+  //\r
+  Ic = RShiftU64 (Addr, 21) & 0x01;\r
+  //\r
+  // imm5c = Addr[20:16] for 5 bits\r
+  //\r
+  Imm5c = RShiftU64 (Addr, 16) & 0x1F;\r
+  //\r
+  // imm9d = Addr[15:7] for 9 bits\r
+  //\r
+  Imm9d = RShiftU64 (Addr, 7) & 0x1FF;\r
+  //\r
+  // imm7b = Addr[6:0] for 7 bits\r
+  //\r
+  Imm7b = Addr & 0x7F;\r
+\r
+  //\r
+  // Put it in r31, a scratch register\r
+  //\r
+  RegNum = 31;\r
+\r
+  //\r
+  // Next is jumbled data, including opcode and rest of address\r
+  //\r
+  Code[2] =   LShiftU64(Imm7b, 13);\r
+  Code[2] = Code[2] | LShiftU64 (0x00, 20);   // vc\r
+  Code[2] = Code[2] | LShiftU64 (Ic, 21);\r
+  Code[2] = Code[2] | LShiftU64 (Imm5c, 22);\r
+  Code[2] = Code[2] | LShiftU64 (Imm9d, 27);\r
+  Code[2] = Code[2] | LShiftU64 (I, 36);\r
+  Code[2] = Code[2] | LShiftU64 ((UINT64)MOVL_OPCODE, 37);\r
+  Code[2] = Code[2] | LShiftU64 ((RegNum & 0x7F), 6);\r
+\r
+  WriteBundle ((VOID *) Ptr, 0x05, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Load branch register with EbcInterpret() function offset from the bundle\r
+  // address: mov b6 = RegNum\r
+  //\r
+  // See volume 3 page 4-29 of the Arch. Software Developer's Manual.\r
+  //\r
+  // Advance pointer to next bundle\r
+  //\r
+  Ptr += 16;\r
+  Code[0] = OPCODE_NOP;\r
+  Code[1] = OPCODE_NOP;\r
+  Code[2] = OPCODE_MOV_BX_RX;\r
+\r
+  //\r
+  // Pick a branch register to use. Then fill in the bits for the branch\r
+  // register and user register (same user register as previous bundle).\r
+  //\r
+  Br = 6;\r
+  Code[2] |= LShiftU64 (Br, 6);\r
+  Code[2] |= LShiftU64 (RegNum, 13);\r
+  WriteBundle ((VOID *) Ptr, 0x0d, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // *************************** NEXT BUNDLE *********************************\r
+  //\r
+  // Now do the branch:  (p0) br.cond.sptk.few b6\r
+  //\r
+  // Advance pointer to next bundle.\r
+  // Fill in the bits for the branch register (same reg as previous bundle)\r
+  //\r
+  Ptr += 16;\r
+  Code[0] = OPCODE_NOP;\r
+  Code[1] = OPCODE_NOP;\r
+  Code[2] = OPCODE_BR_COND_SPTK_FEW;\r
+  Code[2] |= LShiftU64 (Br, 13);\r
+  WriteBundle ((VOID *) Ptr, 0x1d, Code[0], Code[1], Code[2]);\r
+\r
+  //\r
+  // Add the thunk to our list of allocated thunks so we can do some cleanup\r
+  // when the image is unloaded. Do this last since the Add function flushes\r
+  // the instruction cache for us.\r
+  //\r
+  EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
+\r
+  //\r
+  // Done\r
+  //\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+WriteBundle (\r
+  IN    VOID    *MemPtr,\r
+  IN    UINT8   Template,\r
+  IN    UINT64  Slot0,\r
+  IN    UINT64  Slot1,\r
+  IN    UINT64  Slot2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Given raw bytes of Itanium based code, format them into a bundle and\r
+  write them out.\r
+  \r
+Arguments:\r
+\r
+  MemPtr    - pointer to memory location to write the bundles to\r
+  Template  - 5-bit template\r
+  Slot0-2   - instruction slot data for the bundle\r
+\r
+Returns:\r
+\r
+  EFI_INVALID_PARAMETER - Pointer is not aligned\r
+                        - No more than 5 bits in template\r
+                        - More than 41 bits used in code\r
+  EFI_SUCCESS           - All data is written.\r
+\r
+--*/\r
+{\r
+  UINT8   *BPtr;\r
+  UINT32  Index;\r
+  UINT64  Low64;\r
+  UINT64  High64;\r
+\r
+  //\r
+  // Verify pointer is aligned\r
+  //\r
+  if ((UINT64) MemPtr & 0xF) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Verify no more than 5 bits in template\r
+  //\r
+  if (Template &~0x1F) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Verify max of 41 bits used in code\r
+  //\r
+  if ((Slot0 | Slot1 | Slot2) &~0x1ffffffffff) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Low64   = LShiftU64 (Slot1, 46);\r
+  Low64   = Low64 | LShiftU64 (Slot0, 5) | Template;\r
+\r
+  High64  = RShiftU64 (Slot1, 18);\r
+  High64  = High64 | LShiftU64 (Slot2, 23);\r
+\r
+  //\r
+  // Now write it all out\r
+  //\r
+  BPtr = (UINT8 *) MemPtr;\r
+  for (Index = 0; Index < 8; Index++) {\r
+    *BPtr = (UINT8) Low64;\r
+    Low64 = RShiftU64 (Low64, 8);\r
+    BPtr++;\r
+  }\r
+\r
+  for (Index = 0; Index < 8; Index++) {\r
+    *BPtr   = (UINT8) High64;\r
+    High64  = RShiftU64 (High64, 8);\r
+    BPtr++;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+EbcLLCALLEX (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        FuncAddr,\r
+  IN UINTN        NewStackPointer,\r
+  IN VOID         *FramePtr,\r
+  IN UINT8        Size\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function is called to execute an EBC CALLEX instruction. \r
+  The function check the callee's content to see whether it is common native\r
+  code or a thunk to another piece of EBC code.\r
+  If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
+  otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
+  be startup which cost time and stack space.\r
+  \r
+Arguments:\r
+\r
+  VmPtr             - Pointer to a VM context.\r
+  FuncAddr          - Callee's address\r
+  NewStackPointer   - New stack pointer after the call\r
+  FramePtr          - New frame pointer after the call\r
+  Size              - The size of call instruction\r
+\r
+Returns:\r
+\r
+  None.\r
+  \r
+--*/\r
+{\r
+  UINTN    IsThunk;\r
+  UINTN    TargetEbcAddr;\r
+  UINTN    CodeOne18;\r
+  UINTN    CodeOne23;\r
+  UINTN    CodeTwoI;\r
+  UINTN    CodeTwoIc;\r
+  UINTN    CodeTwo7b;\r
+  UINTN    CodeTwo5c;\r
+  UINTN    CodeTwo9d;\r
+  UINTN    CalleeAddr;\r
+\r
+  IsThunk       = 1;\r
+  TargetEbcAddr = 0;\r
+\r
+  //\r
+  // FuncAddr points to the descriptor of the target instructions.\r
+  //\r
+  CalleeAddr = *((UINT64 *)FuncAddr);\r
+\r
+  //\r
+  // Processor specific code to check whether the callee is a thunk to EBC.\r
+  //\r
+  if (*((UINT64 *)CalleeAddr) != 0xBCCA000100000005) {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT64 *)CalleeAddr + 1) != 0x697623C1004A112E)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+\r
+  CodeOne18 = RShiftU64 (*((UINT64 *)CalleeAddr + 2), 46) & 0x3FFFF;\r
+  CodeOne23 = (*((UINT64 *)CalleeAddr + 3)) & 0x7FFFFF;\r
+  CodeTwoI  = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 59) & 0x1;\r
+  CodeTwoIc = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 44) & 0x1;\r
+  CodeTwo7b = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 36) & 0x7F;\r
+  CodeTwo5c = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 45) & 0x1F;\r
+  CodeTwo9d = RShiftU64 (*((UINT64 *)CalleeAddr + 3), 50) & 0x1FF;\r
+\r
+  TargetEbcAddr = CodeTwo7b;\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo9d, 7);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwo5c, 16);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoIc, 21);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne18, 22);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeOne23, 40);\r
+  TargetEbcAddr = TargetEbcAddr | LShiftU64 (CodeTwoI, 63);\r
+\r
+Action:\r
+  if (IsThunk == 1){\r
+    //\r
+    // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
+    // put our return address and frame pointer on the VM stack.\r
+    // Then set the VM's IP to new EBC code.\r
+    //\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
+\r
+    VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+  } else {\r
+    //\r
+    // The callee is not a thunk to EBC, call native code.\r
+    //\r
+    EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+\r
+    //\r
+    // Get return value and advance the IP.\r
+    //\r
+    VmPtr->R[7] = EbcLLGetReturnValue ();\r
+    VmPtr->Ip += Size;\r
+  }\r
+}\r
diff --git a/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.h b/MdeModulePkg/Universal/EbcDxe/Ipf/EbcSupport.h
new file mode 100644 (file)
index 0000000..f6c929b
--- /dev/null
@@ -0,0 +1,56 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+  EbcSupport.h\r
+\r
+Abstract:\r
+\r
+  Definition of EBC Support function\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+#ifndef _IPF_EBC_SUPPORT_H_\r
+#define _IPF_EBC_SUPPORT_H_\r
+\r
+#define VM_STACK_SIZE   (1024 * 32)\r
+\r
+#define EBC_THUNK_SIZE  128\r
+#define STACK_REMAIN_SIZE (1024 * 4)\r
+\r
+//\r
+// For code execution, thunks must be aligned on 16-byte boundary\r
+//\r
+#define EBC_THUNK_ALIGNMENT 16\r
+\r
+//\r
+// Opcodes for IPF instructions. We'll need to hand-create thunk code (stuffing\r
+// bits) to insert a jump to the interpreter.\r
+//\r
+#define OPCODE_NOP              (UINT64) 0x00008000000\r
+#define OPCODE_BR_COND_SPTK_FEW (UINT64) 0x00100000000\r
+#define OPCODE_MOV_BX_RX        (UINT64) 0x00E00100000\r
+\r
+//\r
+// Opcode for MOVL instruction\r
+//\r
+#define MOVL_OPCODE 0x06\r
+\r
+VOID\r
+EbcAsmLLCALLEX (\r
+  IN UINTN    CallAddr,\r
+  IN UINTN    EbcSp\r
+  );\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.S b/MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.S
new file mode 100644 (file)
index 0000000..5384625
--- /dev/null
@@ -0,0 +1,144 @@
+#****************************************************************************\r
+#*                                                                         \r
+#*  Copyright (c) 2006, Intel Corporation                                                         \r
+#*  All rights reserved. This program and the accompanying materials                          \r
+#*  are licensed and made available under the terms and conditions of the BSD License         \r
+#*  which accompanies this distribution.  The full text of the license may be found at        \r
+#*  http://opensource.org/licenses/bsd-license.php                                            \r
+#*                                                                                            \r
+#*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+#*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+#*                                                                          \r
+#****************************************************************************\r
+#****************************************************************************\r
+#                                   REV 1.0\r
+#****************************************************************************\r
+#\r
+# Rev  Date      Description\r
+# ---  --------  ------------------------------------------------------------\r
+# 1.0  05/09/12  Initial creation of file.\r
+#\r
+#****************************************************************************\r
+\r
+#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+# This code provides low level routines that support the Virtual Machine\r
+# for option ROMs. \r
+#* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+\r
+#---------------------------------------------------------------------------\r
+# Equate files needed.\r
+#---------------------------------------------------------------------------\r
+\r
+#---------------------------------------------------------------------------\r
+##GenericPostSegment      SEGMENT USE16\r
+#---------------------------------------------------------------------------\r
+\r
+#****************************************************************************\r
+# EbcLLCALLEX\r
+#\r
+# This function is called to execute an EBC CALLEX instruction. \r
+# This instruction requires that we thunk out to external native\r
+# code. For x64, we switch stacks, copy the arguments to the stack\r
+# and jump to the specified function. \r
+# On return, we restore the stack pointer to its original location.\r
+#\r
+# Destroys no working registers.\r
+#****************************************************************************\r
+.global _CopyMem;\r
+\r
+# VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
+.global _EbcLLCALLEXNative;\r
+_EbcLLCALLEXNative:\r
+      push   %rbp\r
+      push   %rbx\r
+      mov    %rsp, %rbp\r
+      # Function prolog\r
+\r
+      # Copy FuncAddr to a preserved register.\r
+      mov    %rcx, %rbx\r
+\r
+      # Set stack pointer to new value\r
+      sub    %r8,  %rdx\r
+      sub    %rsp, %r8\r
+      mov    %rsp, %rcx\r
+      sub    %rsp, 0x20\r
+      call   _CopyMem \r
+      add    %rsp, 0x20\r
+\r
+      # Considering the worst case, load 4 potiential arguments\r
+      # into registers.\r
+      mov    (%rsp), %rcx\r
+      mov    8(%rsp), %rdx\r
+      mov    10(%rsp), %r8\r
+      mov    18(%rsp), %r9\r
+\r
+      # Now call the external routine\r
+      call  *%rbx\r
+\r
+      # Function epilog\r
+      mov      %rbp, %rsp\r
+      pop      %rbx\r
+      pop      %rbp\r
+      ret\r
+\r
+\r
+# UINTN EbcLLGetEbcEntryPoint(VOID);\r
+# Routine Description:\r
+#   The VM thunk code stuffs an EBC entry point into a processor\r
+#   register. Since we can't use inline assembly to get it from\r
+#   the interpreter C code, stuff it into the return value \r
+#   register and return.\r
+#\r
+# Arguments:\r
+#     None.\r
+#\r
+# Returns:\r
+#     The contents of the register in which the entry point is passed.\r
+#\r
+.global _EbcLLGetEbcEntryPoint;\r
+_EbcLLGetEbcEntryPoint:\r
+    ret\r
+\r
+#/*++\r
+#\r
+#Routine Description:\r
+#  \r
+#  Return the caller's value of the stack pointer.\r
+#\r
+#Arguments:\r
+#\r
+#  None.\r
+#\r
+#Returns:\r
+#\r
+#  The current value of the stack pointer for the caller. We\r
+#  adjust it by 4 here because when they called us, the return address\r
+#  is put on the stack, thereby lowering it by 4 bytes.\r
+#\r
+#--*/\r
+\r
+# UINTN EbcLLGetStackPointer()            \r
+.global _EbcLLGetStackPointer;\r
+_EbcLLGetStackPointer:\r
+    mov    %rsp, %rax\r
+    # Stack adjusted by this much when we were called,\r
+    # For this function, it's 4.\r
+    add   $4, %rax\r
+    ret\r
+\r
+.global _EbcLLGetReturnValue;\r
+_EbcLLGetReturnValue:\r
+# UINT64 EbcLLGetReturnValue(VOID);\r
+# Routine Description:\r
+#   When EBC calls native, on return the VM has to stuff the return\r
+#   value into a VM register. It's assumed here that the value is still\r
+#    in the register, so simply return and the caller should get the\r
+#   return result properly.\r
+#\r
+# Arguments:\r
+#     None.\r
+#\r
+# Returns:\r
+#     The unmodified value returned by the native code.\r
+#\r
+    ret\r
diff --git a/MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.asm b/MdeModulePkg/Universal/EbcDxe/x64/EbcLowLevel.asm
new file mode 100644 (file)
index 0000000..7b59e93
--- /dev/null
@@ -0,0 +1,154 @@
+  page    ,132\r
+  title   VM ASSEMBLY LANGUAGE ROUTINES\r
+;****************************************************************************\r
+;*                                                                         \r
+;*  Copyright (c) 2006, Intel Corporation                                                         \r
+;*  All rights reserved. This program and the accompanying materials                          \r
+;*  are licensed and made available under the terms and conditions of the BSD License         \r
+;*  which accompanies this distribution.  The full text of the license may be found at        \r
+;*  http://opensource.org/licenses/bsd-license.php                                            \r
+;*                                                                                            \r
+;*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+;*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+;*                                                                          \r
+;****************************************************************************\r
+;****************************************************************************\r
+;                                   REV 1.0\r
+;****************************************************************************\r
+;\r
+; Rev  Date      Description\r
+; ---  --------  ------------------------------------------------------------\r
+; 1.0  05/09/12  Initial creation of file.\r
+;\r
+;****************************************************************************\r
+                             \r
+;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+; This code provides low level routines that support the Virtual Machine\r
+; for option ROMs. \r
+;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\r
+\r
+;---------------------------------------------------------------------------\r
+; Equate files needed.\r
+;---------------------------------------------------------------------------\r
+\r
+text SEGMENT\r
+\r
+;---------------------------------------------------------------------------\r
+;;GenericPostSegment      SEGMENT USE16\r
+;---------------------------------------------------------------------------\r
+\r
+;****************************************************************************\r
+; EbcLLCALLEX\r
+;\r
+; This function is called to execute an EBC CALLEX instruction. \r
+; This instruction requires that we thunk out to external native\r
+; code. For x64, we switch stacks, copy the arguments to the stack\r
+; and jump to the specified function. \r
+; On return, we restore the stack pointer to its original location.\r
+;\r
+; Destroys no working registers.\r
+;****************************************************************************\r
+; VOID EbcLLCALLEXNative(UINTN FuncAddr, UINTN NewStackPointer, VOID *FramePtr)\r
+\r
+CopyMem  PROTO  Destination:PTR DWORD, Source:PTR DWORD, Count:DWORD\r
+\r
+\r
+EbcLLCALLEXNative        PROC    NEAR    PUBLIC\r
+      push   rbp\r
+      push   rbx\r
+      mov    rbp, rsp\r
+      ; Function prolog\r
+      \r
+      ; Copy FuncAddr to a preserved register.\r
+      mov    rbx, rcx\r
+\r
+      ; Set stack pointer to new value\r
+      sub    r8,  rdx\r
+      sub    rsp, r8\r
+      mov    rcx, rsp\r
+      sub    rsp, 20h\r
+      call   CopyMem      \r
+      add    rsp, 20h\r
+      \r
+      ; Considering the worst case, load 4 potiential arguments\r
+      ; into registers.\r
+      mov    rcx, qword ptr [rsp]\r
+      mov    rdx, qword ptr [rsp+8h]\r
+      mov    r8,  qword ptr [rsp+10h]\r
+      mov    r9,  qword ptr [rsp+18h]\r
+\r
+      ; Now call the external routine\r
+      call  rbx\r
+      \r
+      ; Function epilog\r
+      mov      rsp, rbp\r
+      pop      rbx\r
+      pop      rbp\r
+      ret\r
+EbcLLCALLEXNative    ENDP\r
+\r
+\r
+; UINTN EbcLLGetEbcEntryPoint(VOID);\r
+; Routine Description:\r
+;   The VM thunk code stuffs an EBC entry point into a processor\r
+;   register. Since we can't use inline assembly to get it from\r
+;   the interpreter C code, stuff it into the return value \r
+;   register and return.\r
+;\r
+; Arguments:\r
+;     None.\r
+;\r
+; Returns:\r
+;     The contents of the register in which the entry point is passed.\r
+;\r
+EbcLLGetEbcEntryPoint        PROC    NEAR    PUBLIC\r
+    ret\r
+EbcLLGetEbcEntryPoint    ENDP\r
+\r
+;/*++\r
+;\r
+;Routine Description:\r
+;  \r
+;  Return the caller's value of the stack pointer.\r
+;\r
+;Arguments:\r
+;\r
+;  None.\r
+;\r
+;Returns:\r
+;\r
+;  The current value of the stack pointer for the caller. We\r
+;  adjust it by 4 here because when they called us, the return address\r
+;  is put on the stack, thereby lowering it by 4 bytes.\r
+;\r
+;--*/\r
+\r
+; UINTN EbcLLGetStackPointer()            \r
+EbcLLGetStackPointer        PROC    NEAR    PUBLIC\r
+    mov    rax, rsp      ; get current stack pointer\r
+    ; Stack adjusted by this much when we were called,\r
+    ; For this function, it's 4.\r
+    add   rax, 4\r
+    ret\r
+EbcLLGetStackPointer    ENDP\r
+\r
+; UINT64 EbcLLGetReturnValue(VOID);\r
+; Routine Description:\r
+;   When EBC calls native, on return the VM has to stuff the return\r
+;   value into a VM register. It's assumed here that the value is still\r
+;    in the register, so simply return and the caller should get the\r
+;   return result properly.\r
+;\r
+; Arguments:\r
+;     None.\r
+;\r
+; Returns:\r
+;     The unmodified value returned by the native code.\r
+;\r
+EbcLLGetReturnValue   PROC    NEAR    PUBLIC\r
+    ret\r
+EbcLLGetReturnValue    ENDP\r
+\r
+text  ENDS\r
+END\r
+\r
diff --git a/MdeModulePkg/Universal/EbcDxe/x64/EbcSupport.c b/MdeModulePkg/Universal/EbcDxe/x64/EbcSupport.c
new file mode 100644 (file)
index 0000000..bec82d6
--- /dev/null
@@ -0,0 +1,619 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  EbcSupport.c\r
+\r
+Abstract:\r
+\r
+  This module contains EBC support routines that are customized based on\r
+  the target x64 processor.\r
+\r
+--*/\r
+\r
+#include "EbcInt.h"\r
+#include "EbcExecute.h"\r
+\r
+//\r
+// NOTE: This is the stack size allocated for the interpreter\r
+//       when it executes an EBC image. The requirements can change\r
+//       based on whether or not a debugger is present, and other\r
+//       platform-specific configurations.\r
+//\r
+#define VM_STACK_SIZE   (1024 * 8)\r
+#define EBC_THUNK_SIZE  64\r
+\r
+#define STACK_REMAIN_SIZE (1024 * 4)\r
+\r
+STATIC\r
+VOID\r
+PushU64 (\r
+  VM_CONTEXT *VmPtr,\r
+  UINT64     Arg\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Push a 64 bit unsigned value to the VM stack.\r
+  \r
+Arguments:\r
+\r
+  VmPtr   -  The pointer to current VM context.\r
+  Arg     -  The value to be pushed\r
+\r
+Returns:\r
+\r
+  VOID\r
+  \r
+--*/\r
+{\r
+  //\r
+  // Advance the VM stack down, and then copy the argument to the stack.\r
+  // Hope it's aligned.\r
+  //\r
+  VmPtr->R[0] -= sizeof (UINT64);\r
+  *(UINT64 *) VmPtr->R[0] = Arg;\r
+  return;\r
+}\r
+\r
+STATIC\r
+UINT64\r
+EbcInterpret (\r
+  UINTN      Arg1,\r
+  UINTN      Arg2,\r
+  UINTN      Arg3,\r
+  UINTN      Arg4,\r
+  UINTN      Arg5,\r
+  UINTN      Arg6,\r
+  UINTN      Arg7,\r
+  UINTN      Arg8,\r
+  UINTN      Arg9,\r
+  UINTN      Arg10,\r
+  UINTN      Arg11,\r
+  UINTN      Arg12,\r
+  UINTN      Arg13,\r
+  UINTN      Arg14,\r
+  UINTN      Arg15,\r
+  UINTN      Arg16\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Begin executing an EBC image. The address of the entry point is passed\r
+  in via a processor register, so we'll need to make a call to get the\r
+  value.\r
+  \r
+Arguments:\r
+\r
+  This is a thunk function. Microsoft x64 compiler only provide fast_call\r
+  calling convention, so the first four arguments are passed by rcx, rdx, \r
+  r8, and r9, while other arguments are passed in stack.\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+  \r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+\r
+  //\r
+  // Get the EBC entry point from the processor register.\r
+  // Don't call any function before getting the EBC entry\r
+  // point because this will collab the return register.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+\r
+  //\r
+  // Initialize the stack pointer for the EBC. Get the current system stack\r
+  // pointer and adjust it down by the max needed for the interpreter.\r
+  //\r
+  Addr            = EbcLLGetStackPointer ();\r
+\r
+  //\r
+  // Adjust the VM's stack pointer down.\r
+  //\r
+  \r
+  Status = GetEBCStack((EFI_HANDLE)(UINTN)-1, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+  //\r
+  // Align the stack on a natural boundary.\r
+  //\r
+  VmContext.R[0] &= ~(sizeof (UINTN) - 1);\r
+\r
+  //\r
+  // Put a magic value in the stack gap, then adjust down again.\r
+  //\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // The stack upper to LowStackTop is belong to the VM.\r
+  //\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // For the worst case, assume there are 4 arguments passed in registers, store\r
+  // them to VM's stack.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) Arg16);\r
+  PushU64 (&VmContext, (UINT64) Arg15);\r
+  PushU64 (&VmContext, (UINT64) Arg14);\r
+  PushU64 (&VmContext, (UINT64) Arg13);\r
+  PushU64 (&VmContext, (UINT64) Arg12);\r
+  PushU64 (&VmContext, (UINT64) Arg11);\r
+  PushU64 (&VmContext, (UINT64) Arg10);\r
+  PushU64 (&VmContext, (UINT64) Arg9);\r
+  PushU64 (&VmContext, (UINT64) Arg8);\r
+  PushU64 (&VmContext, (UINT64) Arg7);\r
+  PushU64 (&VmContext, (UINT64) Arg6);\r
+  PushU64 (&VmContext, (UINT64) Arg5);\r
+  PushU64 (&VmContext, (UINT64) Arg4);\r
+  PushU64 (&VmContext, (UINT64) Arg3);\r
+  PushU64 (&VmContext, (UINT64) Arg2);\r
+  PushU64 (&VmContext, (UINT64) Arg1);\r
+\r
+  //\r
+  // Interpreter assumes 64-bit return address is pushed on the stack.\r
+  // The x64 does not do this so pad the stack accordingly.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) 0);\r
+  PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+\r
+  //\r
+  // For x64, this is where we say our return address is\r
+  //\r
+  VmContext.StackRetAddr  = (UINT64) VmContext.R[0];\r
+\r
+  //\r
+  // We need to keep track of where the EBC stack starts. This way, if the EBC\r
+  // accesses any stack variables above its initial stack setting, then we know\r
+  // it's accessing variables passed into it, which means the data is on the\r
+  // VM's stack.\r
+  // When we're called, on the stack (high to low) we have the parameters, the\r
+  // return address, then the saved ebp. Save the pointer to the return address.\r
+  // EBC code knows that's there, so should look above it for function parameters.\r
+  // The offset is the size of locals (VMContext + Addr + saved ebp).\r
+  // Note that the interpreter assumes there is a 16 bytes of return address on\r
+  // the stack too, so adjust accordingly.\r
+  //  VmContext.HighStackBottom = (UINTN)(Addr + sizeof (VmContext) + sizeof (Addr));\r
+  //\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+STATIC\r
+UINT64\r
+ExecuteEbcImageEntryPoint (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Begin executing an EBC image. The address of the entry point is passed\r
+  in via a processor register, so we'll need to make a call to get the\r
+  value.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle   - image handle for the EBC application we're executing\r
+  SystemTable   - standard system table passed into an driver's entry point\r
+\r
+Returns:\r
+\r
+  The value returned by the EBC application we're going to run.\r
+\r
+--*/\r
+{\r
+  //\r
+  // Create a new VM context on the stack\r
+  //\r
+  VM_CONTEXT  VmContext;\r
+  UINTN       Addr;\r
+  EFI_STATUS  Status;\r
+  UINTN       StackIndex;\r
+\r
+  //\r
+  // Get the EBC entry point from the processor register. Make sure you don't\r
+  // call any functions before this or you could mess up the register the\r
+  // entry point is passed in.\r
+  //\r
+  Addr = EbcLLGetEbcEntryPoint ();\r
+\r
+  //\r
+  // Now clear out our context\r
+  //\r
+  ZeroMem ((VOID *) &VmContext, sizeof (VM_CONTEXT));\r
+\r
+  //\r
+  // Save the image handle so we can track the thunks created for this image\r
+  //\r
+  VmContext.ImageHandle = ImageHandle;\r
+  VmContext.SystemTable = SystemTable;\r
+\r
+  //\r
+  // Set the VM instruction pointer to the correct location in memory.\r
+  //\r
+  VmContext.Ip = (VMIP) Addr;\r
+\r
+  //\r
+  // Initialize the stack pointer for the EBC. Get the current system stack\r
+  // pointer and adjust it down by the max needed for the interpreter.\r
+  //\r
+  Addr            = EbcLLGetStackPointer ();\r
+\r
+  Status = GetEBCStack(ImageHandle, &VmContext.StackPool, &StackIndex);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+  VmContext.StackTop = (UINT8*)VmContext.StackPool + (STACK_REMAIN_SIZE);\r
+  VmContext.R[0] = (UINT64) ((UINT8*)VmContext.StackPool + STACK_POOL_SIZE);\r
+  VmContext.HighStackBottom = (UINTN) VmContext.R[0];\r
+  VmContext.R[0] -= sizeof (UINTN);\r
+\r
+\r
+  //\r
+  // Put a magic value in the stack gap, then adjust down again\r
+  //\r
+  *(UINTN *) (UINTN) (VmContext.R[0]) = (UINTN) VM_STACK_KEY_VALUE;\r
+  VmContext.StackMagicPtr             = (UINTN *) (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // Align the stack on a natural boundary\r
+  VmContext.R[0] &= ~(sizeof(UINTN) - 1);\r
+  //\r
+  VmContext.LowStackTop   = (UINTN) VmContext.R[0];\r
+\r
+  //\r
+  // Simply copy the image handle and system table onto the EBC stack.\r
+  // Greatly simplifies things by not having to spill the args.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) SystemTable);\r
+  PushU64 (&VmContext, (UINT64) ImageHandle);\r
+\r
+  //\r
+  // VM pushes 16-bytes for return address. Simulate that here.\r
+  //\r
+  PushU64 (&VmContext, (UINT64) 0);\r
+  PushU64 (&VmContext, (UINT64) 0x1234567887654321ULL);\r
+\r
+  //\r
+  // For x64, this is where we say our return address is\r
+  //\r
+  VmContext.StackRetAddr  = (UINT64) VmContext.R[0];\r
+\r
+  //\r
+  // Entry function needn't access high stack context, simply\r
+  // put the stack pointer here.\r
+  //\r
+\r
+  //\r
+  // Begin executing the EBC code\r
+  //\r
+  EbcExecute (&VmContext);\r
+\r
+  //\r
+  // Return the value in R[7] unless there was an error\r
+  //\r
+  ReturnEBCStack(StackIndex);\r
+  return (UINT64) VmContext.R[7];\r
+}\r
+\r
+EFI_STATUS\r
+EbcCreateThunks (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN VOID                 *EbcEntryPoint,\r
+  OUT VOID                **Thunk,\r
+  IN  UINT32              Flags\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Create an IA32 thunk for the given EBC entry point.\r
+  \r
+Arguments:\r
+\r
+  ImageHandle     - Handle of image for which this thunk is being created\r
+  EbcEntryPoint   - Address of the EBC code that the thunk is to call\r
+  Thunk           - Returned thunk we create here\r
+\r
+Returns:\r
+\r
+  Standard EFI status.\r
+  \r
+--*/\r
+{\r
+  UINT8       *Ptr;\r
+  UINT8       *ThunkBase;\r
+  UINT32      I;\r
+  UINT64      Addr;\r
+  INT32       Size;\r
+  INT32       ThunkSize;\r
+\r
+  //\r
+  // Check alignment of pointer to EBC code\r
+  //\r
+  if ((UINT32) (UINTN) EbcEntryPoint & 0x01) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Size      = EBC_THUNK_SIZE;\r
+  ThunkSize = Size;\r
+\r
+  Ptr = AllocatePool (Size);\r
+\r
+  if (Ptr == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  //  Print(L"Allocate TH: 0x%X\n", (UINT32)Ptr);\r
+  //\r
+  // Save the start address so we can add a pointer to it to a list later.\r
+  //\r
+  ThunkBase = Ptr;\r
+\r
+  //\r
+  // Give them the address of our buffer we're going to fix up\r
+  //\r
+  *Thunk = (VOID *) Ptr;\r
+\r
+  //\r
+  // Add a magic code here to help the VM recognize the thunk..\r
+  // mov rax, ca112ebccall2ebch  => 48 B8 BC 2E 11 CA BC 2E 11 CA\r
+  //\r
+  *Ptr = 0x48;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xB8;\r
+  Ptr++;\r
+  Size--;\r
+  Addr = (UINT64) 0xCA112EBCCA112EBCULL;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) (UINTN) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+\r
+  //\r
+  // Add code bytes to load up a processor register with the EBC entry point.\r
+  // mov rax, 123456789abcdef0h  => 48 B8 F0 DE BC 9A 78 56 34 12\r
+  // The first 8 bytes of the thunk entry is the address of the EBC\r
+  // entry point.\r
+  //\r
+  *Ptr = 0x48;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xB8;\r
+  Ptr++;\r
+  Size--;\r
+  Addr = (UINT64) EbcEntryPoint;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) (UINTN) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+\r
+  //\r
+  // Stick in a load of ecx with the address of appropriate VM function.\r
+  // Using r11 because it's a volatile register and won't be used in this\r
+  // point.\r
+  // mov r11 123456789abcdef0h  => 49 BB F0 DE BC 9A 78 56 34 12\r
+  //\r
+  if (Flags & FLAG_THUNK_ENTRY_POINT) {\r
+    Addr = (UINTN) ExecuteEbcImageEntryPoint;\r
+  } else {\r
+    Addr = (UINTN) EbcInterpret;\r
+  }\r
+\r
+  //\r
+  // mov r11 Addr => 0x49 0xBB\r
+  //\r
+  *Ptr = 0x49;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xBB;\r
+  Ptr++;\r
+  Size--;\r
+  for (I = 0; I < sizeof (Addr); I++) {\r
+    *Ptr = (UINT8) Addr;\r
+    Addr >>= 8;\r
+    Ptr++;\r
+    Size--;\r
+  }\r
+  //\r
+  // Stick in jump opcode bytes for jmp r11 => 0x41 0xFF 0xE3\r
+  //\r
+  *Ptr = 0x41;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xFF;\r
+  Ptr++;\r
+  Size--;\r
+  *Ptr = 0xE3;\r
+  Size--;\r
+\r
+  //\r
+  // Double check that our defined size is ok (application error)\r
+  //\r
+  if (Size < 0) {\r
+    ASSERT (FALSE);\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  //\r
+  // Add the thunk to the list for this image. Do this last since the add\r
+  // function flushes the cache for us.\r
+  //\r
+  EbcAddImageThunk (ImageHandle, (VOID *) ThunkBase, ThunkSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+EbcLLCALLEX (\r
+  IN VM_CONTEXT   *VmPtr,\r
+  IN UINTN        FuncAddr,\r
+  IN UINTN        NewStackPointer,\r
+  IN VOID         *FramePtr,\r
+  IN UINT8        Size\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function is called to execute an EBC CALLEX instruction. \r
+  The function check the callee's content to see whether it is common native\r
+  code or a thunk to another piece of EBC code.\r
+  If the callee is common native code, use EbcLLCAllEXASM to manipulate,\r
+  otherwise, set the VM->IP to target EBC code directly to avoid another VM\r
+  be startup which cost time and stack space.\r
+  \r
+Arguments:\r
+\r
+  VmPtr             - Pointer to a VM context.\r
+  FuncAddr          - Callee's address\r
+  NewStackPointer   - New stack pointer after the call\r
+  FramePtr          - New frame pointer after the call\r
+  Size              - The size of call instruction\r
+\r
+Returns:\r
+\r
+  None.\r
+  \r
+--*/\r
+{\r
+  UINTN    IsThunk;\r
+  UINTN    TargetEbcAddr;\r
+\r
+  IsThunk       = 1;\r
+  TargetEbcAddr = 0;\r
+\r
+  //\r
+  // Processor specific code to check whether the callee is a thunk to EBC.\r
+  //\r
+  if (*((UINT8 *)FuncAddr) != 0x48) {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 1) != 0xB8) {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 2) != 0xBC)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 3) != 0x2E)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 4) != 0x11)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 5) != 0xCA)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 6) != 0xBC)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 7) != 0x2E)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 8) != 0x11)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 9) != 0xCA)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 10) != 0x48)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+  if (*((UINT8 *)FuncAddr + 11) != 0xB8)  {\r
+    IsThunk = 0;\r
+    goto Action;\r
+  }\r
+\r
+  CopyMem (&TargetEbcAddr, (UINT8 *)FuncAddr + 12, 8);\r
+\r
+Action:\r
+  if (IsThunk == 1){\r
+    //\r
+    // The callee is a thunk to EBC, adjust the stack pointer down 16 bytes and\r
+    // put our return address and frame pointer on the VM stack.\r
+    // Then set the VM's IP to new EBC code.\r
+    //\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMemN (VmPtr, (UINTN) VmPtr->R[0], (UINTN) FramePtr);\r
+    VmPtr->FramePtr = (VOID *) (UINTN) VmPtr->R[0];\r
+    VmPtr->R[0] -= 8;\r
+    VmWriteMem64 (VmPtr, (UINTN) VmPtr->R[0], (UINT64) (VmPtr->Ip + Size));\r
+\r
+    VmPtr->Ip = (VMIP) (UINTN) TargetEbcAddr;\r
+  } else {\r
+    //\r
+    // The callee is not a thunk to EBC, call native code.\r
+    //\r
+    EbcLLCALLEXNative (FuncAddr, NewStackPointer, FramePtr);\r
+\r
+    //\r
+    // Get return value and advance the IP.\r
+    //\r
+    VmPtr->R[7] = EbcLLGetReturnValue ();\r
+    VmPtr->Ip += Size;\r
+  }\r
+}\r
+\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c b/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c
new file mode 100644 (file)
index 0000000..ca5ee21
--- /dev/null
@@ -0,0 +1,265 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  Crc32SectionExtract.c\r
+\r
+Abstract:\r
+\r
+  Implements GUIDed section extraction protocol interface with \r
+  a specific GUID: CRC32.\r
+\r
+  Please refer to the Framewokr Firmware Volume Specification 0.9.\r
+\r
+--*/\r
+\r
+\r
+#include <Crc32SectionExtract.h>\r
+\r
+EFI_STATUS\r
+GuidedSectionExtractionProtocolConstructor (\r
+  OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL      **GuidedSep,\r
+  IN  EFI_EXTRACT_GUIDED_SECTION                  ExtractSection\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Constructor for the GUIDed section extraction protocol.  Initializes\r
+  instance data.\r
+\r
+Arguments:\r
+\r
+  This      Instance to construct\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS:  Instance initialized.\r
+\r
+--*/\r
+// TODO:    GuidedSep - add argument and description to function comment\r
+// TODO:    ExtractSection - add argument and description to function comment\r
+// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
+{\r
+  *GuidedSep = AllocatePool (sizeof (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL));\r
+  if (*GuidedSep == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  (*GuidedSep)->ExtractSection = ExtractSection;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeCrc32GuidedSectionExtractionProtocol (\r
+  IN EFI_HANDLE                   ImageHandle,\r
+  IN EFI_SYSTEM_TABLE             *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description: \r
+\r
+  Entry point of the CRC32 GUIDed section extraction protocol. \r
+  Creates and initializes an instance of the GUIDed section \r
+  extraction protocol with CRC32 GUID.\r
+\r
+Arguments:  \r
+\r
+  ImageHandle   EFI_HANDLE: A handle for the image that is initializing \r
+                this driver\r
+  SystemTable   EFI_SYSTEM_TABLE: A pointer to the EFI system table        \r
+\r
+Returns:  \r
+\r
+  EFI_SUCCESS:          Driver initialized successfully\r
+  EFI_LOAD_ERROR:       Failed to Initialize or has been loaded \r
+  EFI_OUT_OF_RESOURCES: Could not allocate needed resources\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                              Status;\r
+  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *Crc32GuidedSep;\r
+  EFI_HANDLE                              Handle;\r
+\r
+  //\r
+  // Call all constructors per produced protocols\r
+  //\r
+  Status = GuidedSectionExtractionProtocolConstructor (\r
+            &Crc32GuidedSep,\r
+            (EFI_EXTRACT_GUIDED_SECTION) Crc32ExtractSection\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    if (Crc32GuidedSep != NULL) {\r
+      FreePool (Crc32GuidedSep);\r
+    }\r
+\r
+    return Status;\r
+  }\r
+  //\r
+  // Pass in a NULL to install to a new handle\r
+  //\r
+  Handle = NULL;\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &Handle,\r
+                  &gEfiCrc32GuidedSectionExtractionProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  Crc32GuidedSep\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Crc32GuidedSep);\r
+    return EFI_LOAD_ERROR;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+UINT32\r
+EFIAPI\r
+GetSectionLength (\r
+  IN EFI_COMMON_SECTION_HEADER  *CommonHeader\r
+  )\r
+/*++\r
+\r
+  Routine Description:\r
+    Get a length of section.\r
+\r
+  Parameters:\r
+    CommonHeader      -   Pointer to the common section header.\r
+\r
+  Return Value:\r
+    The length of the section, including the section header.\r
+\r
+--*/\r
+// TODO: function comment is missing 'Arguments:'\r
+// TODO: function comment is missing 'Returns:'\r
+// TODO:    CommonHeader - add argument and description to function comment\r
+{\r
+  UINT32  Size;\r
+\r
+  Size = *(UINT32 *) CommonHeader->Size & 0x00FFFFFF;\r
+\r
+  return Size;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+Crc32ExtractSection (\r
+  IN  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *This,\r
+  IN  VOID                                    *InputSection,\r
+  OUT VOID                                    **OutputBuffer,\r
+  OUT UINTN                                   *OutputSize,\r
+  OUT UINT32                                  *AuthenticationStatus\r
+  )\r
+/*++\r
+\r
+  Routine Description:\r
+    This function reads and extracts contents of a section from an\r
+    encapsulating section.\r
+\r
+  Parameters:\r
+    This                    - Indicates the calling context.\r
+    InputSection            - Buffer containing the input GUIDed section \r
+                              to be processed.\r
+    OutputBuffer            - *OutputBuffer is allocated from boot services\r
+                              pool memory and containing the new section\r
+                              stream. The caller is responsible for freeing\r
+                              this buffer.\r
+    AuthenticationStatus    - Pointer to a caller allocated UINT32 that\r
+                              indicates the authentication status of the\r
+                              output buffer\r
+\r
+  Return Value:\r
+    EFI_SUCCESS\r
+    EFI_OUT_OF_RESOURCES\r
+    EFI_INVALID_PARAMETER\r
+    EFI_NOT_AVAILABLE_YET\r
+\r
+--*/\r
+// TODO: function comment is missing 'Arguments:'\r
+// TODO: function comment is missing 'Returns:'\r
+// TODO:    This - add argument and description to function comment\r
+// TODO:    InputSection - add argument and description to function comment\r
+// TODO:    OutputBuffer - add argument and description to function comment\r
+// TODO:    OutputSize - add argument and description to function comment\r
+// TODO:    AuthenticationStatus - add argument and description to function comment\r
+// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
+// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
+// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
+// TODO:    EFI_SUCCESS - add return value to function comment\r
+{\r
+  EFI_STATUS                Status;\r
+  CRC32_SECTION_HEADER      *Crc32SectionHeader;\r
+  EFI_GUID_DEFINED_SECTION  *GuidedSectionHeader;\r
+  UINT8                     *Image;\r
+  UINT32                    Crc32Checksum;\r
+  VOID                      *DummyInterface;\r
+\r
+  if (OutputBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *OutputBuffer = NULL;\r
+\r
+  //\r
+  // Points to the section header\r
+  //\r
+  Crc32SectionHeader  = (CRC32_SECTION_HEADER *) InputSection;\r
+  GuidedSectionHeader = (EFI_GUID_DEFINED_SECTION *) InputSection;\r
+\r
+  //\r
+  // Check if the GUID is a CRC32 section GUID\r
+  //\r
+  if (!CompareGuid (\r
+        &(GuidedSectionHeader->SectionDefinitionGuid),\r
+        &gEfiCrc32GuidedSectionExtractionProtocolGuid\r
+        )) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Image = (UINT8 *) InputSection + (UINT32) (GuidedSectionHeader->DataOffset);\r
+  *OutputSize = GetSectionLength ((EFI_COMMON_SECTION_HEADER *) InputSection) - (UINT32) GuidedSectionHeader->DataOffset;\r
+\r
+  *OutputBuffer = AllocatePool (*OutputSize);\r
+  if (*OutputBuffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Implictly CRC32 GUIDed section should have STATUS_VALID bit set\r
+  //\r
+  ASSERT (GuidedSectionHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
+  *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+\r
+  //\r
+  // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
+  if (!EFI_ERROR (Status)) {\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
+  } else {\r
+    //\r
+    // Calculate CRC32 Checksum of Image\r
+    //\r
+    gBS->CalculateCrc32 (Image, *OutputSize, &Crc32Checksum);\r
+    if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {\r
+      *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    }\r
+  }\r
+\r
+  CopyMem (*OutputBuffer, Image, *OutputSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.h b/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.h
new file mode 100644 (file)
index 0000000..e7bf276
--- /dev/null
@@ -0,0 +1,105 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  Crc32SectionExtract.h\r
+  \r
+Abstract:\r
+\r
+  Header file for Crc32SectionExtract.c\r
+  Please refer to the Framewokr Firmware Volume Specification 0.9.\r
+\r
+--*/\r
+\r
+#ifndef _CRC32_GUIDED_SECTION_EXTRACTION_H\r
+#define _CRC32_GUIDED_SECTION_EXTRACTION_H\r
+\r
+#include <PiDxe.h>\r
+#include <Protocol/SecurityPolicy.h>\r
+#include <Protocol/Crc32GuidedSectionExtraction.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+\r
+\r
+typedef struct {\r
+  EFI_GUID_DEFINED_SECTION  GuidedSectionHeader;\r
+  UINT32                    CRC32Checksum;\r
+} CRC32_SECTION_HEADER;\r
+\r
+//\r
+// Function prototype declarations\r
+//\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+Crc32ExtractSection (\r
+  IN  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *This,\r
+  IN  VOID                                    *InputSection,\r
+  OUT VOID                                    **OutputBuffer,\r
+  OUT UINTN                                   *OutputSize,\r
+  OUT UINT32                                  *AuthenticationStatus\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  This                  - TODO: add argument description\r
+  InputSection          - TODO: add argument description\r
+  OutputBuffer          - TODO: add argument description\r
+  OutputSize            - TODO: add argument description\r
+  AuthenticationStatus  - TODO: add argument description\r
+\r
+Returns:\r
+\r
+  TODO: add return values\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeCrc32GuidedSectionExtractionProtocol (\r
+  IN EFI_HANDLE                   ImageHandle,\r
+  IN EFI_SYSTEM_TABLE             *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description: \r
+\r
+  Entry point of the CRC32 GUIDed section extraction protocol. \r
+  Creates and initializes an instance of the GUIDed section \r
+  extraction protocol with CRC32 GUID.\r
+\r
+Arguments:  \r
+\r
+  ImageHandle   EFI_HANDLE: A handle for the image that is initializing \r
+                this driver\r
+  SystemTable   EFI_SYSTEM_TABLE: A pointer to the EFI system table        \r
+\r
+Returns:  \r
+\r
+  EFI_SUCCESS:          Driver initialized successfully\r
+  EFI_LOAD_ERROR:       Failed to Initialize or has been loaded \r
+  EFI_OUT_OF_RESOURCES: Could not allocate needed resources\r
+\r
+--*/\r
+;\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.msa b/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.msa
new file mode 100644 (file)
index 0000000..a4ab182
--- /dev/null
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
+  <MsaHeader>\r
+    <ModuleName>Crc32SectionExtract</ModuleName>\r
+    <ModuleType>DXE_DRIVER</ModuleType>\r
+    <GuidValue>51C9F40C-5243-4473-B265-B3C8FFAFF9FA</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for Crc32SectionExtract module.</Abstract>\r
+    <Description>This driver implements CRC32 GUIDed section extraction protocol interface.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>Crc32SectionExtract</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DebugLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseMemoryLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>MemoryAllocationLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>Crc32SectionExtract.c</Filename>\r
+    <Filename>Crc32SectionExtract.h</Filename>\r
+    <Filename>GuidedSection.c</Filename>\r
+    <Filename>GuidedSection.h</Filename>\r
+    <Filename>Crc32SectionExtract.dxs</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="SOMETIMES_CONSUMED">\r
+      <ProtocolCName>gEfiSecurityPolicyProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiCrc32GuidedSectionExtractionProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>InitializeCrc32GuidedSectionExtractionProtocol</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtractDxe.inf b/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtractDxe.inf
new file mode 100644 (file)
index 0000000..8b19b78
--- /dev/null
@@ -0,0 +1,53 @@
+#/** @file\r
+# Component description file for Crc32SectionExtract module.\r
+#\r
+# This driver implements CRC32 GUIDed section extraction protocol interface.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = Crc32SectionExtractDxe\r
+  FILE_GUID                      = 51C9F40C-5243-4473-B265-B3C8FFAFF9FA\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = InitializeCrc32GuidedSectionExtractionProtocol\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#\r
+\r
+[Sources.common]\r
+  Crc32SectionExtract.h\r
+  Crc32SectionExtract.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  MemoryAllocationLib\r
+  UefiBootServicesTableLib\r
+  BaseMemoryLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+\r
+[Protocols]\r
+  gEfiSecurityPolicyProtocolGuid                # PROTOCOL SOMETIMES_CONSUMED\r
+  gEfiCrc32GuidedSectionExtractionProtocolGuid  # PROTOCOL ALWAYS_PRODUCED\r
+\r
+[Depex]\r
+  gEfiRuntimeArchProtocolGuid\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.c
new file mode 100644 (file)
index 0000000..38fe11d
--- /dev/null
@@ -0,0 +1,931 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.  \r
+\r
+\r
+Module Name:\r
+\r
+  FtwLite.c\r
+\r
+Abstract:\r
+\r
+  This is a simple fault tolerant write driver, based on PlatformFd library.\r
+  And it only supports write BufferSize <= SpareAreaLength.\r
+\r
+  This boot service only protocol provides fault tolerant write capability for \r
+  block devices.  The protocol has internal non-volatile intermediate storage \r
+  of the data and private information. It should be able to recover \r
+  automatically from a critical fault, such as power failure. \r
+\r
+Notes:\r
+\r
+  The implementation uses an FTW Lite (Fault Tolerant Write) Work Space. \r
+  This work space is a memory copy of the work space on the Woring Block,\r
+  the size of the work space is the FTW_WORK_SPACE_SIZE bytes.\r
+\r
+--*/\r
+\r
+#include <FtwLite.h>\r
+\r
+//\r
+// In write function, we should check the target range to prevent the user\r
+// from writing Spare block and Working space directly.\r
+//\r
+//\r
+// Fault Tolerant Write Protocol API\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+FtwLiteWrite (\r
+  IN EFI_FTW_LITE_PROTOCOL                 *This,\r
+  IN EFI_HANDLE                            FvbHandle,\r
+  IN EFI_LBA                               Lba,\r
+  IN UINTN                                 Offset,\r
+  IN OUT UINTN                             *NumBytes,\r
+  IN VOID                                  *Buffer\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Starts a target block update. This function will record data about write \r
+    in fault tolerant storage and will complete the write in a recoverable \r
+    manner, ensuring at all times that either the original contents or \r
+    the modified contents are available.\r
+\r
+Arguments:\r
+    This             - Calling context\r
+    FvbHandle        - The handle of FVB protocol that provides services for \r
+                       reading, writing, and erasing the target block.\r
+    Lba              - The logical block address of the target block.  \r
+    Offset           - The offset within the target block to place the data.\r
+    NumBytes         - The number of bytes to write to the target block.\r
+    Buffer           - The data to write.\r
+\r
+Returns:\r
+    EFI_SUCCESS          - The function completed successfully\r
+    EFI_BAD_BUFFER_SIZE  - The write would span a target block, which is not \r
+                           a valid action.\r
+    EFI_ACCESS_DENIED    - No writes have been allocated.\r
+    EFI_NOT_FOUND        - Cannot find FVB by handle.\r
+    EFI_OUT_OF_RESOURCES - Cannot allocate memory.\r
+    EFI_ABORTED          - The function could not complete successfully.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice;\r
+  EFI_FTW_LITE_RECORD                 *Record;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  EFI_PHYSICAL_ADDRESS                FvbPhysicalAddress;\r
+  UINTN                               MyLength;\r
+  UINTN                               MyOffset;\r
+  UINTN                               MyBufferSize;\r
+  UINT8                               *MyBuffer;\r
+  UINTN                               SpareBufferSize;\r
+  UINT8                               *SpareBuffer;\r
+  UINTN                               Index;\r
+  UINT8                               *Ptr;\r
+  EFI_DEV_PATH_PTR                    DevPtr;\r
+\r
+  //\r
+  // Refresh work space and get last record\r
+  //\r
+  FtwLiteDevice = FTW_LITE_CONTEXT_FROM_THIS (This);\r
+  Status        = WorkSpaceRefresh (FtwLiteDevice);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  Record = FtwLiteDevice->FtwLastRecord;\r
+\r
+  //\r
+  // Check the flags of last write record\r
+  //\r
+  if ((Record->WriteAllocated == FTW_VALID_STATE) || (Record->SpareCompleted == FTW_VALID_STATE)) {\r
+    return EFI_ACCESS_DENIED;\r
+  }\r
+  //\r
+  // IF former record has completed, THEN use next record\r
+  //\r
+  if (Record->WriteCompleted == FTW_VALID_STATE) {\r
+    Record++;\r
+    FtwLiteDevice->FtwLastRecord = Record;\r
+  }\r
+\r
+  MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+\r
+  //\r
+  // Check if the input data can fit within the target block\r
+  //\r
+  if ((Offset +*NumBytes) > FtwLiteDevice->SpareAreaLength) {\r
+    return EFI_BAD_BUFFER_SIZE;\r
+  }\r
+  //\r
+  // Check if there is enough free space for allocate a record\r
+  //\r
+  if ((MyOffset + WRITE_TOTAL_SIZE) > FtwLiteDevice->FtwWorkSpaceSize) {\r
+    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "FtwLite: Reclaim work space - %r", Status));\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+  //\r
+  // Get the FVB protocol by handle\r
+  //\r
+  Status = FtwGetFvbByHandle (FvbHandle, &Fvb);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  // Allocate a write record in workspace.\r
+  // Update Header->WriteAllocated as VALID\r
+  //\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
+            WRITE_ALLOCATED\r
+            );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Allocate record - %r\n", Status));\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  Record->WriteAllocated = FTW_VALID_STATE;\r
+\r
+  //\r
+  // Prepare data of write record, filling DevPath with memory mapped address.\r
+  //\r
+  DevPtr.MemMap                 = (MEMMAP_DEVICE_PATH *) &Record->DevPath;\r
+  DevPtr.MemMap->Header.Type    = HARDWARE_DEVICE_PATH;\r
+  DevPtr.MemMap->Header.SubType = HW_MEMMAP_DP;\r
+  SetDevicePathNodeLength (&DevPtr.MemMap->Header, sizeof (MEMMAP_DEVICE_PATH));\r
+\r
+  Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Get FVB physical address - %r\n", Status));\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  DevPtr.MemMap->MemoryType       = EfiMemoryMappedIO;\r
+  DevPtr.MemMap->StartingAddress  = FvbPhysicalAddress;\r
+  DevPtr.MemMap->EndingAddress    = FvbPhysicalAddress +*NumBytes;\r
+  //\r
+  // ignored!\r
+  //\r
+  Record->Lba       = Lba;\r
+  Record->Offset    = Offset;\r
+  Record->NumBytes  = *NumBytes;\r
+\r
+  //\r
+  // Write the record to the work space.\r
+  //\r
+  MyOffset  = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+  MyLength  = FTW_LITE_RECORD_SIZE;\r
+\r
+  Status = FtwLiteDevice->FtwFvBlock->Write (\r
+                                        FtwLiteDevice->FtwFvBlock,\r
+                                        FtwLiteDevice->FtwWorkSpaceLba,\r
+                                        FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
+                                        &MyLength,\r
+                                        (UINT8 *) Record\r
+                                        );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Record has been written to working block, then write data.\r
+  //\r
+  //\r
+  // Allocate a memory buffer\r
+  //\r
+  MyBufferSize  = FtwLiteDevice->SpareAreaLength;\r
+  MyBuffer      = AllocatePool (MyBufferSize);\r
+  if (MyBuffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Starting at Lba, if the number of the rest blocks on Fvb is less\r
+  // than NumberOfSpareBlock.\r
+  //\r
+  //\r
+  // Read all original data from target block to memory buffer\r
+  //\r
+  if (IsInWorkingBlock (FtwLiteDevice, Fvb, Lba)) {\r
+    //\r
+    // If target block falls into working block, we must follow the process of\r
+    // updating working block.\r
+    //\r
+    Ptr = MyBuffer;\r
+    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+      MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+      Status = FtwLiteDevice->FtwFvBlock->Read (\r
+                                            FtwLiteDevice->FtwFvBlock,\r
+                                            FtwLiteDevice->FtwWorkBlockLba + Index,\r
+                                            0,\r
+                                            &MyLength,\r
+                                            Ptr\r
+                                            );\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (MyBuffer);\r
+        return EFI_ABORTED;\r
+      }\r
+\r
+      Ptr += MyLength;\r
+    }\r
+    //\r
+    // Update Offset by adding the offset from the start LBA of working block to\r
+    // the target LBA. The target block can not span working block!\r
+    //\r
+    Offset = (((UINTN) (Lba - FtwLiteDevice->FtwWorkBlockLba)) * FtwLiteDevice->SizeOfSpareBlock + Offset);\r
+    ASSERT ((Offset +*NumBytes) <= FtwLiteDevice->SpareAreaLength);\r
+\r
+  } else {\r
+\r
+    Ptr = MyBuffer;\r
+    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+      MyLength  = FtwLiteDevice->SizeOfSpareBlock;\r
+      Status    = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (MyBuffer);\r
+        return EFI_ABORTED;\r
+      }\r
+\r
+      Ptr += MyLength;\r
+    }\r
+  }\r
+  //\r
+  // Overwrite the updating range data with\r
+  // the input buffer content\r
+  //\r
+  CopyMem (MyBuffer + Offset, Buffer, *NumBytes);\r
+\r
+  //\r
+  // Try to keep the content of spare block\r
+  // Save spare block into a spare backup memory buffer (Sparebuffer)\r
+  //\r
+  SpareBufferSize = FtwLiteDevice->SpareAreaLength;\r
+  SpareBuffer     = AllocatePool (SpareBufferSize);\r
+  if (SpareBuffer == NULL) {\r
+    FreePool (MyBuffer);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Ptr = SpareBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &MyLength,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (MyBuffer);\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += MyLength;\r
+  }\r
+  //\r
+  // Write the memory buffer to spare block\r
+  // Don't forget to erase Flash first.\r
+  //\r
+  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
+  Ptr     = MyBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &MyLength,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (MyBuffer);\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += MyLength;\r
+  }\r
+  //\r
+  // Free MyBuffer\r
+  //\r
+  FreePool (MyBuffer);\r
+\r
+  //\r
+  // Set the SpareCompleteD in the FTW record,\r
+  //\r
+  MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
+            SPARE_COMPLETED\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (SpareBuffer);\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  Record->SpareCompleted = FTW_VALID_STATE;\r
+\r
+  //\r
+  //  Since the content has already backuped in spare block, the write is\r
+  //  guaranteed to be completed with fault tolerant manner.\r
+  //\r
+  Status = FtwWriteRecord (FtwLiteDevice, Fvb);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (SpareBuffer);\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  Record++;\r
+  FtwLiteDevice->FtwLastRecord = Record;\r
+\r
+  //\r
+  // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.\r
+  //\r
+  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
+  Ptr     = SpareBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &MyLength,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += MyLength;\r
+  }\r
+  //\r
+  // All success.\r
+  //\r
+  FreePool (SpareBuffer);\r
+\r
+  DEBUG (\r
+    (EFI_D_FTW_LITE,\r
+    "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",\r
+    Lba,\r
+    Offset,\r
+    *NumBytes)\r
+    );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+FtwWriteRecord (\r
+  IN EFI_FTW_LITE_DEVICE                   *FtwLiteDevice,\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Write a record with fault tolerant mannaer.\r
+    Since the content has already backuped in spare block, the write is \r
+    guaranteed to be completed with fault tolerant manner.\r
+    \r
+Arguments:\r
+    FtwLiteDevice       - The private data of FTW_LITE driver\r
+    Fvb                 - The FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ABORTED         - The function could not complete successfully\r
+\r
+--*/\r
+{\r
+  EFI_STATUS          Status;\r
+  EFI_FTW_LITE_RECORD *Record;\r
+  EFI_LBA             WorkSpaceLbaOffset;  \r
+  UINTN               Offset;\r
+\r
+  //\r
+  // Spare Complete but Destination not complete,\r
+  // Recover the targt block with the spare block.\r
+  //\r
+  Record = FtwLiteDevice->FtwLastRecord;\r
+\r
+  //\r
+  // IF target block is working block, THEN Flush Spare Block To Working Block;\r
+  // ELSE IF target block is boot block, THEN Flush Spare Block To boot Block;\r
+  // ELSE flush spare block to normal target block.ENDIF\r
+  //\r
+  if (IsInWorkingBlock (FtwLiteDevice, Fvb, Record->Lba)) {\r
+    //\r
+    // If target block is working block, Attention:\r
+    // it's required to set SPARE_COMPLETED to spare block.\r
+    //\r
+    WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
+    Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+    Status = FtwUpdateFvState (\r
+              FtwLiteDevice->FtwBackupFvb,\r
+              FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
+              FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
+              SPARE_COMPLETED\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
+  } else if (IsBootBlock (FtwLiteDevice, Fvb, Record->Lba)) {\r
+    //\r
+    // Update boot block\r
+    //\r
+    Status = FlushSpareBlockToBootBlock (FtwLiteDevice);\r
+  } else {\r
+    //\r
+    // Update blocks other than working block or boot block\r
+    //\r
+    Status = FlushSpareBlockToTargetBlock (FtwLiteDevice, Fvb, Record->Lba);\r
+  }\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Set WriteCompleted flag in record\r
+  //\r
+  Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
+            WRITE_COMPLETED\r
+            );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Record->WriteCompleted = FTW_VALID_STATE;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+FtwRestart (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Restarts a previously interrupted write. The caller must provide the \r
+    block protocol needed to complete the interrupted write.\r
+    \r
+Arguments:\r
+    FtwLiteDevice       - The private data of FTW_LITE driver\r
+    FvbHandle           - The handle of FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ACCESS_DENIED   - No pending writes exist\r
+    EFI_NOT_FOUND       - FVB protocol not found by the handle\r
+    EFI_ABORTED         - The function could not complete successfully\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_FTW_LITE_RECORD                 *Record;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  EFI_DEV_PATH_PTR                    DevPathPtr;\r
+\r
+  //\r
+  // Spare Completed but Destination not complete,\r
+  // Recover the targt block with the spare block.\r
+  //\r
+  Record = FtwLiteDevice->FtwLastRecord;\r
+\r
+  //\r
+  // Only support memory mapped FVB device path by now.\r
+  //\r
+  DevPathPtr.MemMap = (MEMMAP_DEVICE_PATH *) &Record->DevPath;\r
+  if (!((DevPathPtr.MemMap->Header.Type == HARDWARE_DEVICE_PATH) && (DevPathPtr.MemMap->Header.SubType == HW_MEMMAP_DP))\r
+      ) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Device Path is not memory mapped\n"));\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  Status = GetFvbByAddress (DevPathPtr.MemMap->StartingAddress, &Fvb);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  //  Since the content has already backuped in spare block, the write is\r
+  //  guaranteed to be completed with fault tolerant manner.\r
+  //\r
+  Status = FtwWriteRecord (FtwLiteDevice, Fvb);\r
+  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Restart() - %r\n", Status));\r
+\r
+  Record++;\r
+  FtwLiteDevice->FtwLastRecord = Record;\r
+\r
+  //\r
+  // Erase Spare block\r
+  // This is restart, no need to keep spareblock content.\r
+  //\r
+  FtwEraseSpareBlock (FtwLiteDevice);\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+FtwAbort (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Aborts all previous allocated writes.\r
+\r
+Arguments:\r
+    FtwLiteDevice    - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS      - The function completed successfully\r
+    EFI_ABORTED      - The function could not complete successfully.\r
+    EFI_NOT_FOUND    - No allocated writes exist.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Offset;\r
+\r
+  if (FtwLiteDevice->FtwLastRecord->WriteCompleted == FTW_VALID_STATE) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  // Update the complete state of the header as VALID and abort.\r
+  //\r
+  Offset = (UINT8 *) FtwLiteDevice->FtwLastRecord - FtwLiteDevice->FtwWorkSpace;\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
+            WRITE_COMPLETED\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  FtwLiteDevice->FtwLastRecord->WriteCompleted  = FTW_VALID_STATE;\r
+\r
+  Status = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
+\r
+  //\r
+  // Erase the spare block\r
+  //\r
+  Status = FtwEraseSpareBlock (FtwLiteDevice);\r
+\r
+  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Abort() success \n"));\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeFtwLite (\r
+  IN EFI_HANDLE         ImageHandle,\r
+  IN EFI_SYSTEM_TABLE   *SystemTable\r
+  )\r
+/*++\r
+  Routine Description: \r
+    This function is the entry point of the Fault Tolerant Write driver.\r
+  \r
+  Arguments: \r
+    ImageHandle   - EFI_HANDLE: A handle for the image that is initializing \r
+                    this driver\r
+    SystemTable   - EFI_SYSTEM_TABLE: A pointer to the EFI system table\r
+        \r
+  Returns:  \r
+    EFI_SUCCESS           - FTW has finished the initialization\r
+    EFI_ABORTED           - FTW initialization error\r
+\r
+--*/\r
+{\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  UINTN                               Index;\r
+  EFI_HANDLE                          *HandleBuffer;\r
+  UINTN                               HandleCount;\r
+  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
+  EFI_PHYSICAL_ADDRESS                BaseAddress;\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice;\r
+  EFI_FTW_LITE_RECORD                 *Record;\r
+  UINTN                               Length;\r
+  EFI_STATUS                          Status;\r
+  UINTN                               Offset;\r
+  EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
+  UINT32                              LbaIndex;\r
+\r
+  //\r
+  // Allocate Private data of this driver,\r
+  // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
+  //\r
+  FtwLiteDevice = NULL;\r
+  FtwLiteDevice = AllocatePool (sizeof (EFI_FTW_LITE_DEVICE) + FTW_WORK_SPACE_SIZE);\r
+  if (FtwLiteDevice != NULL) {\r
+    Status = EFI_SUCCESS;\r
+  } else {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ZeroMem (FtwLiteDevice, sizeof (EFI_FTW_LITE_DEVICE));\r
+  FtwLiteDevice->Signature = FTW_LITE_DEVICE_SIGNATURE;\r
+\r
+  //\r
+  // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
+  //\r
+  FtwLiteDevice->FtwWorkSpace     = (UINT8 *) (FtwLiteDevice + 1);\r
+  FtwLiteDevice->FtwWorkSpaceSize = FTW_WORK_SPACE_SIZE;\r
+  SetMem (\r
+    FtwLiteDevice->FtwWorkSpace,\r
+    FtwLiteDevice->FtwWorkSpaceSize,\r
+    FTW_ERASED_BYTE\r
+    );\r
+  FtwLiteDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwLiteDevice->FtwWorkSpace;\r
+\r
+  FtwLiteDevice->FtwLastRecord      = NULL;\r
+\r
+  FtwLiteDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
+  FtwLiteDevice->WorkSpaceLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
+\r
+  FtwLiteDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);\r
+  FtwLiteDevice->SpareAreaLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r
+\r
+  ASSERT ((FtwLiteDevice->WorkSpaceLength != 0) && (FtwLiteDevice->SpareAreaLength != 0));\r
+\r
+  //\r
+  // Locate FVB protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &HandleBuffer\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ASSERT (HandleCount > 0);\r
+\r
+  FtwLiteDevice->FtwFvBlock       = NULL;\r
+  FtwLiteDevice->FtwBackupFvb     = NULL;\r
+  FtwLiteDevice->FtwWorkSpaceLba  = (EFI_LBA) (-1);\r
+  FtwLiteDevice->FtwSpareLba      = (EFI_LBA) (-1);\r
+  for (Index = 0; Index < HandleCount; Index += 1) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[Index],\r
+                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                    (VOID **) &Fvb\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = Fvb->GetPhysicalAddress (Fvb, &BaseAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) BaseAddress);\r
+\r
+    if ((FtwLiteDevice->WorkSpaceAddress >= BaseAddress) &&\r
+        (FtwLiteDevice->WorkSpaceAddress <= (BaseAddress + FwVolHeader->FvLength))\r
+        ) {\r
+      FtwLiteDevice->FtwFvBlock = Fvb;\r
+      //\r
+      // To get the LBA of work space\r
+      //\r
+      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
+        //\r
+        // FV may have multiple types of BlockLength\r
+        //\r
+        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
+        while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {\r
+          for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
+            if (FtwLiteDevice->WorkSpaceAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
+              FtwLiteDevice->FtwWorkSpaceLba = LbaIndex - 1;\r
+              //\r
+              // Get the Work space size and Base(Offset)\r
+              //\r
+              FtwLiteDevice->FtwWorkSpaceSize = FtwLiteDevice->WorkSpaceLength;\r
+              FtwLiteDevice->FtwWorkSpaceBase = (UINTN) (FtwLiteDevice->WorkSpaceAddress - (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
+              break;\r
+            }\r
+          }\r
+          //\r
+          // end for\r
+          //\r
+          FvbMapEntry++;\r
+        }\r
+        //\r
+        // end while\r
+        //\r
+      }\r
+    }\r
+\r
+    if ((FtwLiteDevice->SpareAreaAddress >= BaseAddress) &&\r
+        (FtwLiteDevice->SpareAreaAddress <= (BaseAddress + FwVolHeader->FvLength))\r
+        ) {\r
+      FtwLiteDevice->FtwBackupFvb = Fvb;\r
+      //\r
+      // To get the LBA of spare\r
+      //\r
+      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
+        //\r
+        // FV may have multiple types of BlockLength\r
+        //\r
+        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
+        while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {\r
+          for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
+            if (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
+              //\r
+              // Get the NumberOfSpareBlock and SizeOfSpareBlock\r
+              //\r
+              FtwLiteDevice->FtwSpareLba        = LbaIndex - 1;\r
+              FtwLiteDevice->SizeOfSpareBlock   = FvbMapEntry->Length;\r
+              FtwLiteDevice->NumberOfSpareBlock = FtwLiteDevice->SpareAreaLength / FtwLiteDevice->SizeOfSpareBlock;\r
+              //\r
+              // Check the range of spare area to make sure that it's in FV range\r
+              //\r
+              ASSERT ((FtwLiteDevice->FtwSpareLba + FtwLiteDevice->NumberOfSpareBlock) <= FvbMapEntry->NumBlocks);\r
+              break;\r
+            }\r
+          }\r
+\r
+          FvbMapEntry++;\r
+        }\r
+        //\r
+        // end while\r
+        //\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Calculate the start LBA of working block. Working block is an area which\r
+  // contains working space in its last block and has the same size as spare\r
+  // block, unless there are not enough blocks before the block that contains\r
+  // working space.\r
+  //\r
+  FtwLiteDevice->FtwWorkBlockLba = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->NumberOfSpareBlock + 1;\r
+  if ((INT64) (FtwLiteDevice->FtwWorkBlockLba) < 0) {\r
+    FtwLiteDevice->FtwWorkBlockLba = 0;\r
+  }\r
+\r
+  if ((FtwLiteDevice->FtwFvBlock == NULL) ||\r
+      (FtwLiteDevice->FtwBackupFvb == NULL) ||\r
+      (FtwLiteDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) ||\r
+      (FtwLiteDevice->FtwSpareLba == (EFI_LBA) (-1))\r
+      ) {\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Working or spare FVB not ready\n"));\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  //\r
+  // Refresh workspace data from working block\r
+  //\r
+  Status = WorkSpaceRefresh (FtwLiteDevice);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // If the working block workspace is not valid, try the spare block\r
+  //\r
+  if (!IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace invalid, read from backup\n"));\r
+    //\r
+    // Read from spare block\r
+    //\r
+    Length = FtwLiteDevice->FtwWorkSpaceSize;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba,\r
+                                            FtwLiteDevice->FtwWorkSpaceBase,\r
+                                            &Length,\r
+                                            FtwLiteDevice->FtwWorkSpace\r
+                                            );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    //\r
+    // If spare block is valid, then replace working block content.\r
+    //\r
+    if (IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
+      Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart working block in Init() - %r\n", Status));\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      FtwAbort (FtwLiteDevice);\r
+      //\r
+      // Refresh work space.\r
+      //\r
+      Status = WorkSpaceRefresh (FtwLiteDevice);\r
+      if (EFI_ERROR (Status)) {\r
+        return EFI_ABORTED;\r
+      }\r
+    } else {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Both are invalid, init workspace\n"));\r
+      //\r
+      // If both are invalid, then initialize work space.\r
+      //\r
+      SetMem (\r
+        FtwLiteDevice->FtwWorkSpace,\r
+        FtwLiteDevice->FtwWorkSpaceSize,\r
+        FTW_ERASED_BYTE\r
+        );\r
+      InitWorkSpaceHeader (FtwLiteDevice->FtwWorkSpaceHeader);\r
+      //\r
+      // Write to work space on the working block\r
+      //\r
+      Length = FtwLiteDevice->FtwWorkSpaceSize;\r
+      Status = FtwLiteDevice->FtwFvBlock->Write (\r
+                                            FtwLiteDevice->FtwFvBlock,\r
+                                            FtwLiteDevice->FtwWorkSpaceLba,\r
+                                            FtwLiteDevice->FtwWorkSpaceBase,\r
+                                            &Length,\r
+                                            FtwLiteDevice->FtwWorkSpace\r
+                                            );\r
+      if (EFI_ERROR (Status)) {\r
+        return EFI_ABORTED;\r
+      }\r
+    }\r
+  }\r
+  //\r
+  // Hook the protocol API\r
+  //\r
+  FtwLiteDevice->FtwLiteInstance.Write = FtwLiteWrite;\r
+\r
+  //\r
+  // Install protocol interface\r
+  //\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &FtwLiteDevice->Handle,\r
+                  &gEfiFaultTolerantWriteLiteProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  &FtwLiteDevice->FtwLiteInstance\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // If (!SpareCompleted)  THEN  Abort to rollback.\r
+  //\r
+  if ((FtwLiteDevice->FtwLastRecord->WriteAllocated == FTW_VALID_STATE) &&\r
+      (FtwLiteDevice->FtwLastRecord->SpareCompleted != FTW_VALID_STATE)\r
+      ) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Init.. record not SpareCompleted, abort()\n"));\r
+    FtwAbort (FtwLiteDevice);\r
+  }\r
+  //\r
+  // if (SpareCompleted) THEN  Restart to fault tolerant write.\r
+  //\r
+  if ((FtwLiteDevice->FtwLastRecord->SpareCompleted == FTW_VALID_STATE) &&\r
+      (FtwLiteDevice->FtwLastRecord->WriteCompleted != FTW_VALID_STATE)\r
+      ) {\r
+\r
+    Status = FtwRestart (FtwLiteDevice);\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart last write - %r\n", Status));\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+  //\r
+  // To check the workspace buffer behind last records is EMPTY or not.\r
+  // If it's not EMPTY, FTW_LITE also need to call reclaim().\r
+  //\r
+  Record  = FtwLiteDevice->FtwLastRecord;\r
+  Offset  = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
+  if (FtwLiteDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {\r
+    Offset += WRITE_TOTAL_SIZE;\r
+  }\r
+\r
+  if (!IsErasedFlashBuffer (\r
+        FTW_ERASE_POLARITY,\r
+        FtwLiteDevice->FtwWorkSpace + Offset,\r
+        FtwLiteDevice->FtwWorkSpaceSize - Offset\r
+        )) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace is dirty, call reclaim...\n"));\r
+    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace reclaim - %r\n", Status));\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.h
new file mode 100644 (file)
index 0000000..8bc122d
--- /dev/null
@@ -0,0 +1,695 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+\r
+Module Name:\r
+\r
+  FtwLite.h\r
+\r
+Abstract:\r
+\r
+  This is a simple fault tolerant write driver, based on PlatformFd library.\r
+  And it only supports write BufferSize <= SpareAreaLength.\r
+\r
+  This boot service only protocol provides fault tolerant write capability for \r
+  block devices.  The protocol has internal non-volatile intermediate storage \r
+  of the data and private information. It should be able to recover \r
+  automatically from a critical fault, such as power failure. \r
+\r
+--*/\r
+\r
+#ifndef _EFI_FAULT_TOLERANT_WRITE_LITE_H_\r
+#define _EFI_FAULT_TOLERANT_WRITE_LITE_H_\r
+\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <PiDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Protocol/PciRootBridgeIo.h>\r
+#include <Guid/SystemNvDataGuid.h>\r
+#include <Protocol/FaultTolerantWriteLite.h>\r
+#include <Protocol/FirmwareVolumeBlock.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/PcdLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include <Common/WorkingBlockHeader.h>\r
+\r
+#define EFI_D_FTW_LITE  EFI_D_ERROR\r
+#define EFI_D_FTW_INFO  EFI_D_INFO\r
+\r
+//\r
+// Flash erase polarity is 1\r
+//\r
+#define FTW_ERASE_POLARITY  1\r
+\r
+#define FTW_VALID_STATE     0\r
+#define FTW_INVALID_STATE   1\r
+\r
+#define FTW_ERASED_BYTE     ((UINT8) (255))\r
+#define FTW_POLARITY_REVERT ((UINT8) (255))\r
+\r
+typedef struct {\r
+  UINT8         WriteAllocated : 1;\r
+  UINT8         SpareCompleted : 1;\r
+  UINT8         WriteCompleted : 1;\r
+  UINT8         Reserved : 5;\r
+#define WRITE_ALLOCATED 0x1\r
+#define SPARE_COMPLETED 0x2\r
+#define WRITE_COMPLETED 0x4\r
+\r
+  EFI_DEV_PATH  DevPath;\r
+  EFI_LBA       Lba;\r
+  UINTN         Offset;\r
+  UINTN         NumBytes;\r
+  //\r
+  // UINTN           SpareAreaOffset;\r
+  //\r
+} EFI_FTW_LITE_RECORD;\r
+\r
+#define FTW_LITE_DEVICE_SIGNATURE EFI_SIGNATURE_32 ('F', 'T', 'W', 'L')\r
+\r
+//\r
+// MACRO for Block size.\r
+// Flash Erasing will do in block granularity.\r
+//\r
+#ifdef FV_BLOCK_SIZE\r
+#define FTW_BLOCK_SIZE  FV_BLOCK_SIZE\r
+#else\r
+#define FV_BLOCK_SIZE   0x10000\r
+#define FTW_BLOCK_SIZE  FV_BLOCK_SIZE\r
+#endif\r
+//\r
+// MACRO for FTW WORK SPACE Base & Size\r
+//\r
+#ifdef EFI_FTW_WORKING_OFFSET\r
+#define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET\r
+#else\r
+#define FTW_WORK_SPACE_BASE 0x00E000\r
+#endif\r
+\r
+#ifdef EFI_FTW_WORKING_LENGTH\r
+#define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH\r
+#else\r
+#define FTW_WORK_SPACE_SIZE 0x002000\r
+#endif\r
+//\r
+// MACRO for FTW header and record\r
+//\r
+#define FTW_WORKING_QUEUE_SIZE  (FTW_WORK_SPACE_SIZE - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER))\r
+#define FTW_LITE_RECORD_SIZE    (sizeof (EFI_FTW_LITE_RECORD))\r
+#define WRITE_TOTAL_SIZE        FTW_LITE_RECORD_SIZE\r
+\r
+//\r
+// EFI Fault tolerant protocol private data structure\r
+//\r
+typedef struct {\r
+  UINTN                                   Signature;\r
+  EFI_HANDLE                              Handle;\r
+  EFI_FTW_LITE_PROTOCOL                   FtwLiteInstance;\r
+  EFI_PHYSICAL_ADDRESS                    WorkSpaceAddress;\r
+  UINTN                                   WorkSpaceLength;\r
+  EFI_PHYSICAL_ADDRESS                    SpareAreaAddress;\r
+  UINTN                                   SpareAreaLength;\r
+  UINTN                                   NumberOfSpareBlock; // Number of the blocks in spare block\r
+  UINTN                                   SizeOfSpareBlock;   // Block size in bytes of the blocks in spare block\r
+  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;\r
+  EFI_FTW_LITE_RECORD                     *FtwLastRecord;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FtwFvBlock;        // FVB of working block\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FtwBackupFvb;      // FVB of spare block\r
+  EFI_LBA                                 FtwSpareLba;\r
+  EFI_LBA                                 FtwWorkBlockLba;    // Start LBA of working block\r
+  EFI_LBA                                 FtwWorkSpaceLba;    // Start LBA of working space\r
+  UINTN                                   FtwWorkSpaceBase;   // Offset from LBA start addr\r
+  UINTN                                   FtwWorkSpaceSize;\r
+  UINT8                                   *FtwWorkSpace;\r
+  //\r
+  // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],\r
+  // Allocated with EFI_FTW_LITE_DEVICE.\r
+  //\r
+} EFI_FTW_LITE_DEVICE;\r
+\r
+#define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)\r
+\r
+//\r
+// Driver entry point\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeFtwLite (\r
+  IN EFI_HANDLE                 ImageHandle,\r
+  IN EFI_SYSTEM_TABLE           *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    This function is the entry point of the Fault Tolerant Write driver.\r
+\r
+Arguments:\r
+    ImageHandle   - EFI_HANDLE: A handle for the image that is initializing \r
+                    this driver\r
+    SystemTable   - EFI_SYSTEM_TABLE: A pointer to the EFI system table\r
+\r
+Returns:\r
+    EFI_SUCCESS           - FTW has finished the initialization\r
+    EFI_ABORTED           - FTW initialization error\r
+\r
+--*/\r
+;\r
+\r
+//\r
+// Fault Tolerant Write Protocol API\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+FtwLiteWrite (\r
+  IN EFI_FTW_LITE_PROTOCOL                 *This,\r
+  IN EFI_HANDLE                            FvbHandle,\r
+  IN EFI_LBA                               Lba,\r
+  IN UINTN                                 Offset,\r
+  IN UINTN                                 *NumBytes,\r
+  IN VOID                                  *Buffer\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Starts a target block update. This function will record data about write \r
+    in fault tolerant storage and will complete the write in a recoverable \r
+    manner, ensuring at all times that either the original contents or \r
+    the modified contents are available.\r
+\r
+Arguments:\r
+    This             - Calling context\r
+    FvbHandle        - The handle of FVB protocol that provides services for \r
+                       reading, writing, and erasing the target block.\r
+    Lba              - The logical block address of the target block.  \r
+    Offset           - The offset within the target block to place the data.\r
+    NumBytes         - The number of bytes to write to the target block.\r
+    Buffer           - The data to write.\r
+\r
+Returns:\r
+    EFI_SUCCESS          - The function completed successfully\r
+    EFI_BAD_BUFFER_SIZE  - The write would span a target block, which is not \r
+                           a valid action.\r
+    EFI_ACCESS_DENIED    - No writes have been allocated.\r
+    EFI_NOT_FOUND        - Cannot find FVB by handle.\r
+    EFI_OUT_OF_RESOURCES - Cannot allocate memory.\r
+    EFI_ABORTED          - The function could not complete successfully.\r
+\r
+--*/\r
+;\r
+\r
+//\r
+// Internal functions\r
+//\r
+EFI_STATUS\r
+FtwRestart (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Restarts a previously interrupted write. The caller must provide the \r
+    block protocol needed to complete the interrupted write.\r
+\r
+Arguments:\r
+    FtwLiteDevice       - The private data of FTW_LITE driver\r
+    FvbHandle           - The handle of FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ACCESS_DENIED   - No pending writes exist\r
+    EFI_NOT_FOUND       - FVB protocol not found by the handle\r
+    EFI_ABORTED         - The function could not complete successfully\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwAbort (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Aborts all previous allocated writes.\r
+\r
+Arguments:\r
+    FtwLiteDevice    - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS      - The function completed successfully\r
+    EFI_ABORTED      - The function could not complete successfully.\r
+    EFI_NOT_FOUND    - No allocated writes exist.\r
+\r
+--*/\r
+;\r
+\r
+\r
+EFI_STATUS\r
+FtwWriteRecord (\r
+  IN EFI_FTW_LITE_DEVICE                   *FtwLiteDevice,\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Write a record with fault tolerant mannaer.\r
+    Since the content has already backuped in spare block, the write is \r
+    guaranteed to be completed with fault tolerant manner.\r
+\r
+Arguments:\r
+    FtwLiteDevice       - The private data of FTW_LITE driver\r
+    Fvb                 - The FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ABORTED         - The function could not complete successfully\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwEraseBlock (\r
+  IN EFI_FTW_LITE_DEVICE              *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    To Erase one block. The size is FTW_BLOCK_SIZE\r
+\r
+Arguments:\r
+    FtwLiteDevice - Calling context\r
+    FvBlock       - FVB Protocol interface\r
+    Lba           - Lba of the firmware block\r
+\r
+Returns:\r
+    EFI_SUCCESS   - Block LBA is Erased successfully\r
+    Others        - Error occurs\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwEraseSpareBlock (\r
+  IN EFI_FTW_LITE_DEVICE   *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Erase spare block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+\r
+Returns:\r
+\r
+  Status code\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwGetFvbByHandle (\r
+  IN EFI_HANDLE                           FvBlockHandle,\r
+  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Retrive the proper FVB protocol interface by HANDLE.\r
+\r
+Arguments:\r
+    FvBlockHandle       - The handle of FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+    FvBlock             - The interface of FVB protocol\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ABORTED         - The function could not complete successfully\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+GetFvbByAddress (\r
+  IN  EFI_PHYSICAL_ADDRESS               Address,\r
+  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Get firmware block by address.\r
+\r
+Arguments:\r
+\r
+  Address - Address specified the block\r
+  FvBlock - The block caller wanted\r
+\r
+Returns:\r
+\r
+  Status code\r
+\r
+  EFI_NOT_FOUND - Block not found\r
+\r
+--*/\r
+;\r
+\r
+BOOLEAN\r
+IsInWorkingBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Is it in working block?\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - The block specified\r
+\r
+Returns:\r
+\r
+  In working block or not\r
+\r
+--*/\r
+;\r
+\r
+BOOLEAN\r
+IsBootBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether the block is a boot block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - Lba value\r
+\r
+Returns:\r
+\r
+  Is a boot block or not\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToTargetBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+    FvBlock        - FVB Protocol interface to access target block\r
+    Lba            - Lba of the target block\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to target block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToWorkingBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Working block is accessed by FTW working FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwWorkBlockLba.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to target block\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+    Since the working block header is important when FTW initializes, the \r
+    state of the operation should be handled carefully. The Crc value is \r
+    calculated without STATE element. \r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToBootBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to boot block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwUpdateFvState (\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  IN EFI_LBA                             Lba,\r
+  IN UINTN                               Offset,\r
+  IN UINT8                               NewBit\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Update a bit of state on a block device. The location of the bit is \r
+    calculated by the (Lba, Offset, bit). Here bit is determined by the \r
+    the name of a certain bit.\r
+\r
+Arguments:\r
+    FvBlock    - FVB Protocol interface to access SrcBlock and DestBlock\r
+    Lba        - Lba of a block\r
+    Offset     - Offset on the Lba\r
+    NewBit     - New value that will override the old value if it can be change\r
+\r
+Returns:\r
+    EFI_SUCCESS   - A state bit has been updated successfully\r
+    Others        - Access block device error.\r
+\r
+Notes:\r
+    Assume all bits of State are inside the same BYTE. \r
+\r
+    EFI_ABORTED   - Read block fail\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwGetLastRecord (\r
+  IN  EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
+  OUT EFI_FTW_LITE_RECORD  **FtwLastRecord\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Get the last Write record pointer. \r
+    The last record is the record whose 'complete' state hasn't been set.\r
+    After all, this header may be a EMPTY header entry for next Allocate. \r
+\r
+Arguments:\r
+    FtwLiteDevice   - Private data of this driver\r
+    FtwLastRecord   - Pointer to retrieve the last write record\r
+\r
+Returns:\r
+    EFI_SUCCESS     - Get the last write record successfully\r
+    EFI_ABORTED     - The FTW work space is damaged\r
+\r
+--*/\r
+;\r
+\r
+BOOLEAN\r
+IsErasedFlashBuffer (\r
+  IN BOOLEAN         Polarity,\r
+  IN UINT8           *Buffer,\r
+  IN UINTN           BufferSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether a flash buffer is erased.\r
+\r
+Arguments:\r
+\r
+  Polarity    - All 1 or all 0\r
+  Buffer      - Buffer to check\r
+  BufferSize  - Size of the buffer\r
+\r
+Returns:\r
+\r
+  Erased or not.\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+InitWorkSpaceHeader (\r
+  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Initialize a work space when there is no work space.\r
+\r
+Arguments:\r
+    WorkingHeader - Pointer of working block header \r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+WorkSpaceRefresh (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Read from working block to refresh the work space in memory.\r
+\r
+Arguments:\r
+    FtwLiteDevice     - Point to private data of FTW driver\r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+;\r
+\r
+BOOLEAN\r
+IsValidWorkSpace (\r
+  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Check to see if it is a valid work space.\r
+\r
+Arguments:\r
+    WorkingHeader - Pointer of working block header \r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+CleanupWorkSpace (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
+  IN OUT UINT8            *BlockBuffer,\r
+  IN UINTN                BufferSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Reclaim the work space. Get rid of all the completed write records\r
+    and write records in the Fault Tolerant work space.\r
+\r
+Arguments:\r
+    FtwLiteDevice   - Point to private data of FTW driver\r
+    FtwSpaceBuffer  - Buffer to contain the reclaimed clean data\r
+    BufferSize      - Size of the FtwSpaceBuffer\r
+\r
+Returns:\r
+    EFI_SUCCESS           - The function completed successfully\r
+    EFI_BUFFER_TOO_SMALL  - The FtwSpaceBuffer is too small\r
+    EFI_ABORTED           - The function could not complete successfully.\r
+\r
+--*/\r
+;\r
+\r
+EFI_STATUS\r
+FtwReclaimWorkSpace (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Reclaim the work space on the working block.\r
+\r
+Arguments:\r
+    FtwLiteDevice     - Point to private data of FTW driver\r
+\r
+Returns:\r
+    EFI_SUCCESS           - The function completed successfully\r
+    EFI_OUT_OF_RESOURCES  - Allocate memory error\r
+    EFI_ABORTED           - The function could not complete successfully\r
+\r
+--*/\r
+;\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.inf b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.inf
new file mode 100644 (file)
index 0000000..4f47d60
--- /dev/null
@@ -0,0 +1,141 @@
+#/** @file\r
+# Component description file for FtwLite module.\r
+#\r
+# This driver provides fault tolerant write capability for block devices.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+################################################################################\r
+#\r
+# Defines Section - statements that will be processed to create a Makefile.\r
+#\r
+################################################################################\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = FtwLite\r
+  FILE_GUID                      = 4C862FC6-0E54-4e36-8C8F-FF6F3167951F\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = InitializeFtwLite\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#\r
+\r
+################################################################################\r
+#\r
+# Sources Section - list of files that are required for the build to succeed.\r
+#\r
+################################################################################\r
+\r
+[Sources.common]\r
+  FtwWorkSpace.c\r
+  FtwMisc.c\r
+  FtwLite.c\r
+  FtwLite.h\r
\r
+[Sources.Ia32]\r
+  Ia32/Ia32FtwMisc.c\r
+\r
+[Sources.X64]\r
+  x64/x64FtwMisc.c\r
+\r
+[Sources.IPF]\r
+  Ipf/IpfFtwMisc.c\r
+\r
+[Sources.EBC]\r
+  Ia32/Ia32FtwMisc.c\r
+\r
+\r
+################################################################################\r
+#\r
+# Package Dependency Section - list of Package files that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+\r
+################################################################################\r
+#\r
+# Library Class Section - list of Library Classes that are required for\r
+#                         this module.\r
+#\r
+################################################################################\r
+\r
+[LibraryClasses]\r
+  UefiBootServicesTableLib\r
+  MemoryAllocationLib\r
+  BaseMemoryLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+  PcdLib\r
+  HobLib\r
+\r
+################################################################################\r
+#\r
+# Guid C Name Section - list of Guids that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Guids]\r
+  gEfiSystemNvDataFvGuid                        # ALWAYS_CONSUMED\r
+  gEfiFlashMapHobGuid\r
+\r
+################################################################################\r
+#\r
+# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
+#                           that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Protocols]\r
+  gEfiFirmwareVolumeBlockProtocolGuid           # PROTOCOL ALWAYS_CONSUMED\r
+  gEfiFaultTolerantWriteLiteProtocolGuid        # PROTOCOL ALWAYS_PRODUCED\r
+\r
+[Protocols.IA32]\r
+  gEfiPciRootBridgeIoProtocolGuid               # PROTOCOL ALWAYS_CONSUMED\r
+\r
+[Protocols.EBC]\r
+  gEfiPciRootBridgeIoProtocolGuid               # PROTOCOL ALWAYS_CONSUMED\r
+\r
+\r
+################################################################################\r
+#\r
+# Pcd DYNAMIC - list of PCDs that this module is coded for.\r
+#\r
+################################################################################\r
+\r
+[PcdsDynamic.common]\r
+  PcdFlashNvStorageFtwWorkingSize|gEfiMdeModulePkgTokenSpaceGuid\r
+  PcdFlashNvStorageFtwWorkingBase|gEfiMdeModulePkgTokenSpaceGuid\r
+  PcdFlashNvStorageFtwSpareSize|gEfiMdeModulePkgTokenSpaceGuid\r
+  PcdFlashNvStorageFtwSpareBase|gEfiMdeModulePkgTokenSpaceGuid\r
+\r
+################################################################################\r
+#\r
+# Dependency Expression Section - list of Dependency expressions that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Depex]\r
+  gEfiFirmwareVolumeBlockProtocolGuid AND gEfiAlternateFvBlockGuid\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.msa b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwLite.msa
new file mode 100644 (file)
index 0000000..7362f47
--- /dev/null
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
+  <MsaHeader>\r
+    <ModuleName>FtwLite</ModuleName>\r
+    <ModuleType>DXE_DRIVER</ModuleType>\r
+    <GuidValue>4C862FC6-0E54-4e36-8C8F-FF6F3167951F</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for FtwLite module.</Abstract>\r
+    <Description>This driver provides fault tolerant write capability for block devices.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>FtwLite</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>PcdLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DebugLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseMemoryLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>MemoryAllocationLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>FtwLite.h</Filename>\r
+    <Filename>FtwLite.c</Filename>\r
+    <Filename>FtwMisc.c</Filename>\r
+    <Filename>FtwWorkSpace.c</Filename>\r
+    <Filename>FtwLite.dxs</Filename>\r
+    <Filename SupArchList="IA32 EBC">Ia32/Ia32FtwMisc.c</Filename>\r
+    <Filename SupArchList="X64">x64/x64FtwMisc.c</Filename>\r
+    <Filename SupArchList="IPF">Ipf/IpfFtwMisc.c</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
+    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiFaultTolerantWriteLiteProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+    <Protocol Usage="ALWAYS_CONSUMED" SupArchList="IA32 EBC">\r
+      <ProtocolCName>gEfiPciRootBridgeIoProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+    <Protocol Usage="ALWAYS_CONSUMED">\r
+      <ProtocolCName>gEfiFirmwareVolumeBlockProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Guids>\r
+    <GuidCNames Usage="ALWAYS_CONSUMED">\r
+      <GuidCName>gEfiSystemNvDataFvGuid</GuidCName>\r
+    </GuidCNames>\r
+  </Guids>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>InitializeFtwLite</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+  <PcdCoded>\r
+    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
+      <C_Name>PcdFlashNvStorageFtwSpareBase</C_Name>\r
+      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
+      <HelpText>To get base address of the FTW spare block section in NV firmware volume.</HelpText>\r
+    </PcdEntry>\r
+    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
+      <C_Name>PcdFlashNvStorageFtwSpareSize</C_Name>\r
+      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
+      <HelpText>To get size of the FTW spare block section in NV firmware volume.</HelpText>\r
+    </PcdEntry>\r
+    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
+      <C_Name>PcdFlashNvStorageFtwWorkingBase</C_Name>\r
+      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
+      <HelpText>To get base address of the FTW working block section in NV firmware volume.</HelpText>\r
+    </PcdEntry>\r
+    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
+      <C_Name>PcdFlashNvStorageFtwWorkingSize</C_Name>\r
+      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
+      <HelpText>To get size of the FTW working block section in NV firmware volume.</HelpText>\r
+    </PcdEntry>\r
+  </PcdCoded>\r
+</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwMisc.c
new file mode 100644 (file)
index 0000000..eb334ea
--- /dev/null
@@ -0,0 +1,530 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+  FtwMisc.c\r
+  \r
+Abstract:\r
+  \r
+  Internal functions to support fault tolerant write.\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+#include <FtwLite.h>\r
+\r
+BOOLEAN\r
+IsErasedFlashBuffer (\r
+  IN BOOLEAN         Polarity,\r
+  IN UINT8           *Buffer,\r
+  IN UINTN           BufferSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether a flash buffer is erased.\r
+\r
+Arguments:\r
+\r
+  Polarity    - All 1 or all 0\r
+  Buffer      - Buffer to check\r
+  BufferSize  - Size of the buffer\r
+\r
+Returns:\r
+\r
+  Erased or not.\r
+\r
+--*/\r
+{\r
+  UINT8 ErasedValue;\r
+  UINT8 *Ptr;\r
+\r
+  if (Polarity) {\r
+    ErasedValue = 0xFF;\r
+  } else {\r
+    ErasedValue = 0;\r
+  }\r
+\r
+  Ptr = Buffer;\r
+  while (BufferSize--) {\r
+    if (*Ptr++ != ErasedValue) {\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+EFI_STATUS\r
+FtwEraseBlock (\r
+  IN EFI_FTW_LITE_DEVICE              *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    To Erase one block. The size is FTW_BLOCK_SIZE\r
+\r
+Arguments:\r
+    FtwLiteDevice - Calling context\r
+    FvBlock       - FVB Protocol interface\r
+    Lba        - Lba of the firmware block\r
+\r
+Returns:\r
+    EFI_SUCCESS   - Block LBA is Erased successfully\r
+    Others        - Error occurs\r
+\r
+--*/\r
+{\r
+  return FvBlock->EraseBlocks (\r
+                    FvBlock,\r
+                    Lba,\r
+                    FtwLiteDevice->NumberOfSpareBlock,\r
+                    EFI_LBA_LIST_TERMINATOR\r
+                    );\r
+}\r
+\r
+EFI_STATUS\r
+FtwEraseSpareBlock (\r
+  IN EFI_FTW_LITE_DEVICE   *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Erase spare block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+\r
+Returns:\r
+\r
+  Status code\r
+\r
+--*/\r
+{\r
+  return FtwLiteDevice->FtwBackupFvb->EraseBlocks (\r
+                                        FtwLiteDevice->FtwBackupFvb,\r
+                                        FtwLiteDevice->FtwSpareLba,\r
+                                        FtwLiteDevice->NumberOfSpareBlock,\r
+                                        EFI_LBA_LIST_TERMINATOR\r
+                                        );\r
+}\r
+\r
+EFI_STATUS\r
+FtwGetFvbByHandle (\r
+  IN EFI_HANDLE                           FvBlockHandle,\r
+  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Retrive the proper FVB protocol interface by HANDLE.\r
+\r
+Arguments:\r
+    FvBlockHandle       - The handle of FVB protocol that provides services for \r
+                          reading, writing, and erasing the target block.\r
+    FvBlock             - The interface of FVB protocol\r
+\r
+Returns:\r
+    EFI_SUCCESS         - The function completed successfully\r
+    EFI_ABORTED         - The function could not complete successfully\r
+--*/\r
+{\r
+  //\r
+  // To get the FVB protocol interface on the handle\r
+  //\r
+  return gBS->HandleProtocol (\r
+                FvBlockHandle,\r
+                &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                (VOID **) FvBlock\r
+                );\r
+}\r
+\r
+EFI_STATUS\r
+GetFvbByAddress (\r
+  IN  EFI_PHYSICAL_ADDRESS               Address,\r
+  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Get firmware block by address.\r
+\r
+Arguments:\r
+\r
+  Address - Address specified the block\r
+  FvBlock - The block caller wanted\r
+\r
+Returns:\r
+\r
+  Status code\r
+\r
+  EFI_NOT_FOUND - Block not found\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_HANDLE                          *HandleBuffer;\r
+  UINTN                               HandleCount;\r
+  UINTN                               Index;\r
+  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
+\r
+  *FvBlock = NULL;\r
+  //\r
+  // Locate all handles of Fvb protocol\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  //\r
+  // Search all FVB until find the right one\r
+  //\r
+  for (Index = 0; Index < HandleCount; Index += 1) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[Index],\r
+                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
+                    (VOID **) &Fvb\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_NOT_FOUND;\r
+      break;\r
+    }\r
+    //\r
+    // Compare the address and select the right one\r
+    //\r
+    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
+    if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {\r
+      *FvBlock  = Fvb;\r
+      Status    = EFI_SUCCESS;\r
+      break;\r
+    }\r
+  }\r
+\r
+  FreePool (HandleBuffer);\r
+  return Status;\r
+}\r
+\r
+BOOLEAN\r
+IsInWorkingBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Is it in working block?\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - The block specified\r
+\r
+Returns:\r
+\r
+  In working block or not\r
+\r
+--*/\r
+{\r
+  //\r
+  // If matching the following condition, the target block is in working block.\r
+  // 1. Target block is on the FV of working block (Using the same FVB protocol instance).\r
+  // 2. Lba falls into the range of working block.\r
+  //\r
+  return (BOOLEAN)\r
+    (\r
+      (FvBlock == FtwLiteDevice->FtwFvBlock) &&\r
+      (Lba >= FtwLiteDevice->FtwWorkBlockLba) &&\r
+      (Lba <= FtwLiteDevice->FtwWorkSpaceLba)\r
+    );\r
+}\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToTargetBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+    FvBlock        - FVB Protocol interface to access target block\r
+    Lba            - Lba of the target block\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to target block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Length;\r
+  UINT8       *Buffer;\r
+  UINTN       Count;\r
+  UINT8       *Ptr;\r
+  UINTN       Index;\r
+\r
+  if ((FtwLiteDevice == NULL) || (FvBlock == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Allocate a memory buffer\r
+  //\r
+  Length  = FtwLiteDevice->SpareAreaLength;\r
+  Buffer  = AllocatePool (Length);\r
+  if (Buffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Read all content of spare block to memory buffer\r
+  //\r
+  Ptr = Buffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Count = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Count,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+\r
+    Ptr += Count;\r
+  }\r
+  //\r
+  // Erase the target block\r
+  //\r
+  Status = FtwEraseBlock (FtwLiteDevice, FvBlock, Lba);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Write memory buffer to block, using the FvbBlock protocol interface\r
+  //\r
+  Ptr = Buffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Count   = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status  = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+\r
+    Ptr += Count;\r
+  }\r
+\r
+  FreePool (Buffer);\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToWorkingBlock (\r
+  EFI_FTW_LITE_DEVICE          *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Working block is accessed by FTW working FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwWorkBlockLba.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to target block\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+    Since the working block header is important when FTW initializes, the \r
+    state of the operation should be handled carefully. The Crc value is \r
+    calculated without STATE element. \r
+\r
+--*/\r
+{\r
+  EFI_STATUS                              Status;\r
+  UINTN                                   Length;\r
+  UINT8                                   *Buffer;\r
+  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
+  EFI_LBA                                 WorkSpaceLbaOffset;\r
+  UINTN                                   Count;\r
+  UINT8                                   *Ptr;\r
+  UINTN                                   Index;\r
+\r
+  //\r
+  // Allocate a memory buffer\r
+  //\r
+  Length  = FtwLiteDevice->SpareAreaLength;\r
+  Buffer  = AllocatePool (Length);\r
+  if (Buffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // To  guarantee that the WorkingBlockValid is set on spare block\r
+  //\r
+  WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
+  FtwUpdateFvState (\r
+    FtwLiteDevice->FtwBackupFvb,\r
+    FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
+    FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
+    WORKING_BLOCK_VALID\r
+    );\r
+  //\r
+  // Read from spare block to memory buffer\r
+  //\r
+  Ptr = Buffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Count = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Count,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+\r
+    Ptr += Count;\r
+  }\r
+  //\r
+  // Clear the CRC and STATE, copy data from spare to working block.\r
+  //\r
+  WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwLiteDevice->SizeOfSpareBlock + FtwLiteDevice->FtwWorkSpaceBase);\r
+  InitWorkSpaceHeader (WorkingBlockHeader);\r
+  WorkingBlockHeader->WorkingBlockValid   = FTW_ERASE_POLARITY;\r
+  WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;\r
+\r
+  //\r
+  // target block is working block, then\r
+  //   Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER\r
+  //   before erase the working block.\r
+  //\r
+  //  Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
+  //                            WorkingBlockInvalid);\r
+  // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).\r
+  //\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
+            WORKING_BLOCK_INVALID\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;\r
+\r
+  //\r
+  // Erase the working block\r
+  //\r
+  Status = FtwEraseBlock (\r
+            FtwLiteDevice,\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkBlockLba\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Write memory buffer to working block, using the FvbBlock protocol interface\r
+  //\r
+  Ptr = Buffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Count = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwFvBlock->Write (\r
+                                          FtwLiteDevice->FtwFvBlock,\r
+                                          FtwLiteDevice->FtwWorkBlockLba + Index,\r
+                                          0,\r
+                                          &Count,\r
+                                          Ptr\r
+                                          );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+\r
+    Ptr += Count;\r
+  }\r
+  //\r
+  // Since the memory buffer will not be used, free memory Buffer.\r
+  //\r
+  FreePool (Buffer);\r
+\r
+  //\r
+  // Update the VALID of the working block\r
+  //\r
+  // Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
+  //                           WorkingBlockValid);\r
+  // Hardcode offset sizeof(EFI_GUID)+sizeof(UINT32), to skip Signature and Crc\r
+  //\r
+  Status = FtwUpdateFvState (\r
+            FtwLiteDevice->FtwFvBlock,\r
+            FtwLiteDevice->FtwWorkSpaceLba,\r
+            FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
+            WORKING_BLOCK_VALID\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwWorkSpace.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/FtwWorkSpace.c
new file mode 100644 (file)
index 0000000..00d7d8e
--- /dev/null
@@ -0,0 +1,561 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+  FtwWorkSpace.c\r
+  \r
+Abstract:\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+\r
+#include <FtwLite.h>\r
+\r
+BOOLEAN\r
+IsValidWorkSpace (\r
+  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Check to see if it is a valid work space.\r
+\r
+Arguments:\r
+    WorkingHeader - Pointer of working block header \r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                              Status;\r
+  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER WorkingBlockHeader;\r
+\r
+  ASSERT (WorkingHeader != NULL);\r
+  if (WorkingHeader->WorkingBlockValid != FTW_VALID_STATE) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // Check signature with gEfiSystemNvDataFvGuid\r
+  //\r
+  if (!CompareGuid (&gEfiSystemNvDataFvGuid, &WorkingHeader->Signature)) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // Check the CRC of header\r
+  //\r
+  CopyMem (\r
+    &WorkingBlockHeader,\r
+    WorkingHeader,\r
+    sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER)\r
+    );\r
+\r
+  //\r
+  // Filter out the Crc and State fields\r
+  //\r
+  SetMem (\r
+    &WorkingBlockHeader.Crc,\r
+    sizeof (UINT32),\r
+    FTW_ERASED_BYTE\r
+    );\r
+  WorkingBlockHeader.WorkingBlockValid    = FTW_ERASE_POLARITY;\r
+  WorkingBlockHeader.WorkingBlockInvalid  = FTW_ERASE_POLARITY;\r
+\r
+  //\r
+  // Calculate the Crc of woking block header\r
+  //\r
+  Status = gBS->CalculateCrc32 (\r
+                  (UINT8 *) &WorkingBlockHeader,\r
+                  sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
+                  &WorkingBlockHeader.Crc\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (WorkingBlockHeader.Crc != WorkingHeader->Crc) {\r
+    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Work block header CRC check error\n"));\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+EFI_STATUS\r
+InitWorkSpaceHeader (\r
+  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Initialize a work space when there is no work space.\r
+\r
+Arguments:\r
+    WorkingHeader - Pointer of working block header \r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  ASSERT (WorkingHeader != NULL);\r
+\r
+  //\r
+  // Here using gEfiSystemNvDataFvGuid as the signature.\r
+  //\r
+  CopyMem (\r
+    &WorkingHeader->Signature,\r
+    &gEfiSystemNvDataFvGuid,\r
+    sizeof (EFI_GUID)\r
+    );\r
+  WorkingHeader->WriteQueueSize = FTW_WORKING_QUEUE_SIZE;\r
+\r
+  //\r
+  // Crc is calculated with all the fields except Crc and STATE\r
+  //\r
+  WorkingHeader->WorkingBlockValid    = FTW_ERASE_POLARITY;\r
+  WorkingHeader->WorkingBlockInvalid  = FTW_ERASE_POLARITY;\r
+  SetMem (&WorkingHeader->Crc, sizeof (UINT32), FTW_ERASED_BYTE);\r
+\r
+  //\r
+  // Calculate the CRC value\r
+  //\r
+  Status = gBS->CalculateCrc32 (\r
+                  (UINT8 *) WorkingHeader,\r
+                  sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
+                  &WorkingHeader->Crc\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Restore the WorkingBlockValid flag to VALID state\r
+  //\r
+  WorkingHeader->WorkingBlockValid    = FTW_VALID_STATE;\r
+  WorkingHeader->WorkingBlockInvalid  = FTW_INVALID_STATE;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FtwUpdateFvState (\r
+  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  IN EFI_LBA                             Lba,\r
+  IN UINTN                               Offset,\r
+  IN UINT8                               NewBit\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Update a bit of state on a block device. The location of the bit is \r
+    calculated by the (Lba, Offset, bit). Here bit is determined by the \r
+    the name of a certain bit.\r
+\r
+Arguments:\r
+    FvBlock    - FVB Protocol interface to access SrcBlock and DestBlock\r
+    Lba        - Lba of a block\r
+    Offset     - Offset on the Lba\r
+    NewBit     - New value that will override the old value if it can be change\r
+\r
+Returns:\r
+    EFI_SUCCESS   - A state bit has been updated successfully\r
+    Others        - Access block device error.\r
+\r
+Notes:\r
+    Assume all bits of State are inside the same BYTE. \r
+\r
+    EFI_ABORTED   - Read block fail\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       State;\r
+  UINTN       Length;\r
+\r
+  //\r
+  // Read state from device, assume State is only one byte.\r
+  //\r
+  Length  = sizeof (UINT8);\r
+  Status  = FvBlock->Read (FvBlock, Lba, Offset, &Length, &State);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  State ^= FTW_POLARITY_REVERT;\r
+  State = (UINT8) (State | NewBit);\r
+  State ^= FTW_POLARITY_REVERT;\r
+\r
+  //\r
+  // Write state back to device\r
+  //\r
+  Length  = sizeof (UINT8);\r
+  Status  = FvBlock->Write (FvBlock, Lba, Offset, &Length, &State);\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+FtwGetLastRecord (\r
+  IN  EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
+  OUT EFI_FTW_LITE_RECORD  **FtwLastRecord\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Get the last Write record pointer. \r
+    The last record is the record whose 'complete' state hasn't been set.\r
+    After all, this header may be a EMPTY header entry for next Allocate. \r
+\r
+Arguments:\r
+    FtwLiteDevice   - Private data of this driver\r
+    FtwLastRecord   - Pointer to retrieve the last write record\r
+\r
+Returns:\r
+    EFI_SUCCESS     - Get the last write record successfully\r
+    EFI_ABORTED     - The FTW work space is damaged\r
+\r
+--*/\r
+{\r
+  EFI_FTW_LITE_RECORD *Record;\r
+\r
+  Record = (EFI_FTW_LITE_RECORD *) (FtwLiteDevice->FtwWorkSpaceHeader + 1);\r
+  while (Record->WriteCompleted == FTW_VALID_STATE) {\r
+    //\r
+    // If Offset exceed the FTW work space boudary, return error.\r
+    //\r
+    if ((UINTN) ((UINT8 *) Record - FtwLiteDevice->FtwWorkSpace) > FtwLiteDevice->FtwWorkSpaceSize) {\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Record++;\r
+  }\r
+  //\r
+  // Last write record is found\r
+  //\r
+  *FtwLastRecord = Record;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+WorkSpaceRefresh (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Read from working block to refresh the work space in memory.\r
+\r
+Arguments:\r
+    FtwLiteDevice     - Point to private data of FTW driver\r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+    EFI_ABORTED   - The function could not complete successfully.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS          Status;\r
+  UINTN               Length;\r
+  UINTN               Offset;\r
+  EFI_FTW_LITE_RECORD *Record;\r
+\r
+  //\r
+  // Initialize WorkSpace as FTW_ERASED_BYTE\r
+  //\r
+  SetMem (\r
+    FtwLiteDevice->FtwWorkSpace,\r
+    FtwLiteDevice->FtwWorkSpaceSize,\r
+    FTW_ERASED_BYTE\r
+    );\r
+\r
+  //\r
+  // Read from working block\r
+  //\r
+  Length = FtwLiteDevice->FtwWorkSpaceSize;\r
+  Status = FtwLiteDevice->FtwFvBlock->Read (\r
+                                        FtwLiteDevice->FtwFvBlock,\r
+                                        FtwLiteDevice->FtwWorkSpaceLba,\r
+                                        FtwLiteDevice->FtwWorkSpaceBase,\r
+                                        &Length,\r
+                                        FtwLiteDevice->FtwWorkSpace\r
+                                        );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Refresh the FtwLastRecord\r
+  //\r
+  Status  = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
+\r
+  Record  = FtwLiteDevice->FtwLastRecord;\r
+  Offset  = (UINTN) (UINT8 *) Record - (UINTN) FtwLiteDevice->FtwWorkSpace;\r
+\r
+  //\r
+  // IF work space has error or Record is out of the workspace limit, THEN\r
+  //   call reclaim.\r
+  //\r
+  if (EFI_ERROR (Status) || (Offset + WRITE_TOTAL_SIZE >= FtwLiteDevice->FtwWorkSpaceSize)) {\r
+    //\r
+    // reclaim work space in working block.\r
+    //\r
+    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Reclaim workspace - %r\n", Status));\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+CleanupWorkSpace (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
+  IN OUT UINT8            *FtwSpaceBuffer,\r
+  IN UINTN                BufferSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Reclaim the work space. Get rid of all the completed write records\r
+    and write records in the Fault Tolerant work space.\r
+\r
+Arguments:\r
+    FtwLiteDevice   - Point to private data of FTW driver\r
+    FtwSpaceBuffer  - Buffer to contain the reclaimed clean data\r
+    BufferSize      - Size of the FtwSpaceBuffer\r
+\r
+Returns:\r
+    EFI_SUCCESS           - The function completed successfully\r
+    EFI_BUFFER_TOO_SMALL  - The FtwSpaceBuffer is too small\r
+    EFI_ABORTED           - The function could not complete successfully.\r
+\r
+--*/\r
+{\r
+  UINTN               Length;\r
+  EFI_FTW_LITE_RECORD *Record;\r
+\r
+  //\r
+  // To check if the buffer is large enough\r
+  //\r
+  Length = FtwLiteDevice->FtwWorkSpaceSize;\r
+  if (BufferSize < Length) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  //\r
+  // Clear the content of buffer that will save the new work space data\r
+  //\r
+  SetMem (FtwSpaceBuffer, Length, FTW_ERASED_BYTE);\r
+\r
+  //\r
+  // Copy EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER to buffer\r
+  //\r
+  CopyMem (\r
+    FtwSpaceBuffer,\r
+    FtwLiteDevice->FtwWorkSpaceHeader,\r
+    sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER)\r
+    );\r
+\r
+  //\r
+  // Get the last record\r
+  //\r
+  Record = FtwLiteDevice->FtwLastRecord;\r
+  if ((Record != NULL) && (Record->WriteAllocated == FTW_VALID_STATE) && (Record->WriteCompleted != FTW_VALID_STATE)) {\r
+    CopyMem (\r
+      (UINT8 *) FtwSpaceBuffer + sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
+      Record,\r
+      WRITE_TOTAL_SIZE\r
+      );\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FtwReclaimWorkSpace (\r
+  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Reclaim the work space on the working block.\r
+\r
+Arguments:\r
+    FtwLiteDevice     - Point to private data of FTW driver\r
+\r
+Returns:\r
+    EFI_SUCCESS           - The function completed successfully\r
+    EFI_OUT_OF_RESOURCES  - Allocate memory error\r
+    EFI_ABORTED           - The function could not complete successfully\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                              Status;\r
+  UINT8                                   *TempBuffer;\r
+  UINTN                                   TempBufferSize;\r
+  UINT8                                   *Ptr;\r
+  UINTN                                   Length;\r
+  UINTN                                   Index;\r
+  UINTN                                   SpareBufferSize;\r
+  UINT8                                   *SpareBuffer;\r
+  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
+\r
+  DEBUG ((EFI_D_FTW_LITE, "FtwLite: start to reclaim work space\n"));\r
+\r
+  //\r
+  // Read all original data from working block to a memory buffer\r
+  //\r
+  TempBufferSize = FtwLiteDevice->SpareAreaLength;\r
+  TempBuffer     = AllocateZeroPool (TempBufferSize);\r
+  if (TempBuffer != NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Ptr = TempBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Length = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwFvBlock->Read (\r
+                                          FtwLiteDevice->FtwFvBlock,\r
+                                          FtwLiteDevice->FtwWorkBlockLba + Index,\r
+                                          0,\r
+                                          &Length,\r
+                                          Ptr\r
+                                          );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (TempBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += Length;\r
+  }\r
+  //\r
+  // Clean up the workspace, remove all the completed records.\r
+  //\r
+  Ptr = TempBuffer +\r
+    ((UINTN) (FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba)) *\r
+    FtwLiteDevice->SizeOfSpareBlock + FtwLiteDevice->FtwWorkSpaceBase;\r
+\r
+  Status = CleanupWorkSpace (\r
+            FtwLiteDevice,\r
+            Ptr,\r
+            FtwLiteDevice->FtwWorkSpaceSize\r
+            );\r
+\r
+  CopyMem (\r
+    FtwLiteDevice->FtwWorkSpace,\r
+    Ptr,\r
+    FtwLiteDevice->FtwWorkSpaceSize\r
+    );\r
+\r
+  Status = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
+\r
+  //\r
+  // Set the WorkingBlockValid and WorkingBlockInvalid as INVALID\r
+  //\r
+  WorkingBlockHeader                      = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) Ptr;\r
+  WorkingBlockHeader->WorkingBlockValid   = FTW_INVALID_STATE;\r
+  WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE;\r
+\r
+  //\r
+  // Try to keep the content of spare block\r
+  // Save spare block into a spare backup memory buffer (Sparebuffer)\r
+  //\r
+  SpareBufferSize = FtwLiteDevice->SpareAreaLength;\r
+  SpareBuffer     = AllocatePool (SpareBufferSize);\r
+  if (SpareBuffer == NULL) {\r
+    FreePool (TempBuffer);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Ptr = SpareBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Length = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Length,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (TempBuffer);\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += Length;\r
+  }\r
+  //\r
+  // Write the memory buffer to spare block\r
+  //\r
+  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
+  Ptr     = TempBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Length = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Length,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (TempBuffer);\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += Length;\r
+  }\r
+  //\r
+  // Free TempBuffer\r
+  //\r
+  FreePool (TempBuffer);\r
+\r
+  //\r
+  // Write the spare block to working block\r
+  //\r
+  Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (SpareBuffer);\r
+    return Status;\r
+  }\r
+  //\r
+  // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.\r
+  //\r
+  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
+  Ptr     = SpareBuffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Length = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Length,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (SpareBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    Ptr += Length;\r
+  }\r
+\r
+  FreePool (SpareBuffer);\r
+\r
+  DEBUG ((EFI_D_FTW_LITE, "FtwLite: reclaim work space success\n"));\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ia32/Ia32FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ia32/Ia32FtwMisc.c
new file mode 100644 (file)
index 0000000..6425f29
--- /dev/null
@@ -0,0 +1,403 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+  Ia32FtwMisc.c\r
+  \r
+Abstract:\r
+  \r
+   Ia32 platform related code to support FtwLite..\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+\r
+#include <FtwLite.h>\r
+\r
+//\r
+// MACROs for boot block update\r
+//\r
+#define BOOT_BLOCK_BASE 0xFFFF0000\r
+\r
+//\r
+// (LPC -- D31:F0)\r
+//\r
+#define LPC_BUS_NUMBER    0x00\r
+#define LPC_DEVICE_NUMBER 0x1F\r
+#define LPC_IF            0xF0\r
+//\r
+// Top swap\r
+//\r
+#define GEN_STATUS    0xD4\r
+#define TOP_SWAP_BIT  (1 << 13)\r
+\r
+STATIC\r
+UINT32\r
+ReadPciRegister (\r
+  IN UINT32                 Offset\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Read PCI register value.\r
+\r
+Arguments:\r
+\r
+  Offset  - Offset of the register\r
+\r
+Returns:\r
+\r
+  The value.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINT32                          Value;\r
+  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
+\r
+  Value   = 0;\r
+  Status  = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
+    return 0;\r
+  }\r
+\r
+  Status = PciRootBridgeIo->Pci.Read (\r
+                                  PciRootBridgeIo,\r
+                                  EfiPciWidthUint32,\r
+                                  EFI_PCI_ADDRESS (\r
+                                    LPC_BUS_NUMBER,\r
+                                    LPC_DEVICE_NUMBER,\r
+                                    LPC_IF,\r
+                                    Offset\r
+                                    ),\r
+                                  1,\r
+                                  &Value\r
+                                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Value;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+GetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  OUT BOOLEAN               *SwapState\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Get swap state\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  SwapState     - Swap state\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS - State successfully got\r
+\r
+--*/\r
+{\r
+  //\r
+  // Top swap status is 13 bit\r
+  //\r
+  *SwapState = (BOOLEAN) ((ReadPciRegister (GEN_STATUS) & TOP_SWAP_BIT) != 0);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+SetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  IN  BOOLEAN               TopSwap\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Set swap state.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
+    TopSwap        - New swap state\r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+\r
+Note:\r
+    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
+    software will not be able to clear the Top-Swap bit until the system is\r
+    rebooted without GNT[A]# being pulled down.\r
+\r
+--*/\r
+{\r
+  UINT32                          GenStatus;\r
+  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
+  EFI_STATUS                      Status;\r
+\r
+  //\r
+  // Top-Swap bit (bit 13, D31: F0, Offset D4h)\r
+  //\r
+  GenStatus = ReadPciRegister (GEN_STATUS);\r
+\r
+  //\r
+  // Set 13 bit, according to input NewSwapState\r
+  //\r
+  if (TopSwap) {\r
+    GenStatus |= TOP_SWAP_BIT;\r
+  } else {\r
+    GenStatus &= ~TOP_SWAP_BIT;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
+    return Status;\r
+  }\r
+  //\r
+  // Write back the GenStatus register\r
+  //\r
+  Status = PciRootBridgeIo->Pci.Write (\r
+                                  PciRootBridgeIo,\r
+                                  EfiPciWidthUint32,\r
+                                  EFI_PCI_ADDRESS (\r
+                                    LPC_BUS_NUMBER,\r
+                                    LPC_DEVICE_NUMBER,\r
+                                    LPC_IF,\r
+                                    GEN_STATUS\r
+                                    ),\r
+                                  1,\r
+                                  &GenStatus\r
+                                  );\r
+\r
+  DEBUG_CODE_BEGIN ();\r
+    if (TopSwap) {\r
+      DEBUG ((EFI_D_ERROR, "SAR: Set top swap\n"));\r
+    } else {\r
+      DEBUG ((EFI_D_ERROR, "SAR: Clear top swap\n"));\r
+    }\r
+  DEBUG_CODE_END ();\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+BOOLEAN\r
+IsBootBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether the block is a boot block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - Lba value\r
+\r
+Returns:\r
+\r
+  Is a boot block or not\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *BootFvb;\r
+\r
+  Status = GetFvbByAddress (BOOT_BLOCK_BASE, &BootFvb);\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+  //\r
+  // Compare the Fvb\r
+  //\r
+  return (BOOLEAN) (FvBlock == BootFvb);\r
+}\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToBootBlock (\r
+  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to boot block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+    FTW will do extra work on boot block update.\r
+    FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL, \r
+    which is produced by a chipset driver.\r
+\r
+    FTW updating boot block steps:\r
+    1. Erase top swap block (0xFFFE-0xFFFEFFFF) and write data to it ready\r
+    2. Read data from top swap block to memory buffer\r
+    3. SetSwapState(EFI_SWAPPED)\r
+    4. Erasing boot block (0xFFFF-0xFFFFFFFF)\r
+    5. Programming boot block until the boot block is ok.\r
+    6. SetSwapState(UNSWAPPED)\r
+\r
+    Notes:\r
+     1. Since the SwapState bit is saved in CMOS, FTW can restore and continue \r
+     even in the scenario of power failure.\r
+     2. FTW shall not allow to update boot block when battery state is error.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                          Status;\r
+  UINTN                               Length;\r
+  UINT8                               *Buffer;\r
+  UINTN                               Count;\r
+  UINT8                               *Ptr;\r
+  UINTN                               Index;\r
+  BOOLEAN                             TopSwap;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *BootFvb;\r
+  EFI_LBA                             BootLba;\r
+\r
+  //\r
+  // Allocate a memory buffer\r
+  //\r
+  Length  = FtwLiteDevice->SpareAreaLength;\r
+  Buffer  = AllocatePool (Length);\r
+  if (Buffer == NULL) {\r
+  }\r
+  //\r
+  // Get TopSwap bit state\r
+  //\r
+  Status = GetSwapState (FtwLiteDevice, &TopSwap);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Get Top Swapped status - %r\n", Status));\r
+    FreePool (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  if (TopSwap) {\r
+    //\r
+    // Get FVB of current boot block\r
+    //\r
+    Status = GetFvbByAddress (FtwLiteDevice->SpareAreaAddress + FTW_BLOCK_SIZE, &BootFvb);\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+    //\r
+    // Read data from current boot block\r
+    //\r
+    BootLba = 0;\r
+    Ptr     = Buffer;\r
+    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+      Count = FtwLiteDevice->SizeOfSpareBlock;\r
+      Status = BootFvb->Read (\r
+                          BootFvb,\r
+                          BootLba + Index,\r
+                          0,\r
+                          &Count,\r
+                          Ptr\r
+                          );\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (Buffer);\r
+        return Status;\r
+      }\r
+\r
+      Ptr += Count;\r
+    }\r
+\r
+  } else {\r
+    //\r
+    // Read data from spare block\r
+    //\r
+    Ptr = Buffer;\r
+    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+      Count = FtwLiteDevice->SizeOfSpareBlock;\r
+      Status = FtwLiteDevice->FtwBackupFvb->Read (\r
+                                              FtwLiteDevice->FtwBackupFvb,\r
+                                              FtwLiteDevice->FtwSpareLba + Index,\r
+                                              0,\r
+                                              &Count,\r
+                                              Ptr\r
+                                              );\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (Buffer);\r
+        return Status;\r
+      }\r
+\r
+      Ptr += Count;\r
+    }\r
+    //\r
+    // Set TopSwap bit\r
+    //\r
+    Status = SetSwapState (FtwLiteDevice, TRUE);\r
+    DEBUG ((EFI_D_ERROR, "FtwLite: Set Swap State - %r\n", Status));\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  //\r
+  // Erase boot block. After setting TopSwap bit, it's spare block now!\r
+  //\r
+  Status = FtwEraseSpareBlock (FtwLiteDevice);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Write memory buffer to currenet spare block\r
+  //\r
+  Ptr = Buffer;\r
+  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
+    Count = FtwLiteDevice->SizeOfSpareBlock;\r
+    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
+                                            FtwLiteDevice->FtwBackupFvb,\r
+                                            FtwLiteDevice->FtwSpareLba + Index,\r
+                                            0,\r
+                                            &Count,\r
+                                            Ptr\r
+                                            );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write boot block - %r\n", Status));\r
+      FreePool (Buffer);\r
+      return Status;\r
+    }\r
+\r
+    Ptr += Count;\r
+  }\r
+\r
+  FreePool (Buffer);\r
+\r
+  //\r
+  // Clear TopSwap bit\r
+  //\r
+  Status = SetSwapState (FtwLiteDevice, FALSE);\r
+  DEBUG ((EFI_D_ERROR, "FtwLite: Clear Swap State - %r\n", Status));\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ipf/IpfFtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/Ipf/IpfFtwMisc.c
new file mode 100644 (file)
index 0000000..d31883b
--- /dev/null
@@ -0,0 +1,143 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+  IpfFtwMisc.c\r
+  \r
+Abstract:\r
+  \r
+  Ipf platform related code to support FtwLite..\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+\r
+#include <FtwLite.h>\r
+\r
+//\r
+// MACROs for boot block update\r
+//\r
+#define BOOT_BLOCK_BASE\r
+\r
+STATIC\r
+EFI_STATUS\r
+GetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  OUT BOOLEAN               *SwapState\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Get swap state\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  SwapState     - Swap state\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS - State successfully got\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+SetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  IN  BOOLEAN               TopSwap\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Set swap state.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
+    TopSwap        - New swap state\r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+\r
+Note:\r
+    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
+    software will not be able to clear the Top-Swap bit until the system is\r
+    rebooted without GNT[A]# being pulled down.\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+BOOLEAN\r
+IsBootBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether the block is a boot block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - Lba value\r
+\r
+Returns:\r
+\r
+  Is a boot block or not\r
+\r
+--*/\r
+{\r
+  //\r
+  // IPF doesn't support safe bootblock update\r
+  // so treat bootblock as normal block\r
+  //\r
+  return FALSE;\r
+}\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToBootBlock (\r
+  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to boot block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/x64/x64FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteDxe/x64/x64FtwMisc.c
new file mode 100644 (file)
index 0000000..d8e3a03
--- /dev/null
@@ -0,0 +1,140 @@
+\r
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+  x64FtwMisc.c\r
+  \r
+Abstract:\r
+  \r
+  X64 platform related code to support FtwLite..\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+\r
+#include <FtwLite.h>\r
+\r
+//\r
+// MACROs for boot block update\r
+//\r
+#define BOOT_BLOCK_BASE\r
+\r
+// STATIC\r
+EFI_STATUS\r
+GetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  OUT BOOLEAN               *SwapState\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Get swap state\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  SwapState     - Swap state\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS - State successfully got\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+// STATIC\r
+EFI_STATUS\r
+SetSwapState (\r
+  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
+  IN  BOOLEAN               TopSwap\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Set swap state.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
+    TopSwap        - New swap state\r
+\r
+Returns:\r
+    EFI_SUCCESS   - The function completed successfully\r
+\r
+Note:\r
+    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
+    software will not be able to clear the Top-Swap bit until the system is\r
+    rebooted without GNT[A]# being pulled down.\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+BOOLEAN\r
+IsBootBlock (\r
+  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
+  EFI_LBA                             Lba\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Check whether the block is a boot block.\r
+\r
+Arguments:\r
+\r
+  FtwLiteDevice - Calling context\r
+  FvBlock       - Fvb protocol instance\r
+  Lba           - Lba value\r
+\r
+Returns:\r
+\r
+  Is a boot block or not\r
+\r
+--*/\r
+{\r
+  return FALSE;\r
+}\r
+\r
+EFI_STATUS\r
+FlushSpareBlockToBootBlock (\r
+  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
+    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
+    FtwLiteDevice->FtwSpareLba.\r
+    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
+\r
+Arguments:\r
+    FtwLiteDevice  - The private data of FTW_LITE driver\r
+\r
+Returns:\r
+    EFI_SUCCESS              - Spare block content is copied to boot block\r
+    EFI_INVALID_PARAMETER    - Input parameter error\r
+    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
+    EFI_ABORTED              - The function could not complete successfully\r
+\r
+Notes:\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.c
deleted file mode 100644 (file)
index 38fe11d..0000000
+++ /dev/null
@@ -1,931 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.  \r
-\r
-\r
-Module Name:\r
-\r
-  FtwLite.c\r
-\r
-Abstract:\r
-\r
-  This is a simple fault tolerant write driver, based on PlatformFd library.\r
-  And it only supports write BufferSize <= SpareAreaLength.\r
-\r
-  This boot service only protocol provides fault tolerant write capability for \r
-  block devices.  The protocol has internal non-volatile intermediate storage \r
-  of the data and private information. It should be able to recover \r
-  automatically from a critical fault, such as power failure. \r
-\r
-Notes:\r
-\r
-  The implementation uses an FTW Lite (Fault Tolerant Write) Work Space. \r
-  This work space is a memory copy of the work space on the Woring Block,\r
-  the size of the work space is the FTW_WORK_SPACE_SIZE bytes.\r
-\r
---*/\r
-\r
-#include <FtwLite.h>\r
-\r
-//\r
-// In write function, we should check the target range to prevent the user\r
-// from writing Spare block and Working space directly.\r
-//\r
-//\r
-// Fault Tolerant Write Protocol API\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-FtwLiteWrite (\r
-  IN EFI_FTW_LITE_PROTOCOL                 *This,\r
-  IN EFI_HANDLE                            FvbHandle,\r
-  IN EFI_LBA                               Lba,\r
-  IN UINTN                                 Offset,\r
-  IN OUT UINTN                             *NumBytes,\r
-  IN VOID                                  *Buffer\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Starts a target block update. This function will record data about write \r
-    in fault tolerant storage and will complete the write in a recoverable \r
-    manner, ensuring at all times that either the original contents or \r
-    the modified contents are available.\r
-\r
-Arguments:\r
-    This             - Calling context\r
-    FvbHandle        - The handle of FVB protocol that provides services for \r
-                       reading, writing, and erasing the target block.\r
-    Lba              - The logical block address of the target block.  \r
-    Offset           - The offset within the target block to place the data.\r
-    NumBytes         - The number of bytes to write to the target block.\r
-    Buffer           - The data to write.\r
-\r
-Returns:\r
-    EFI_SUCCESS          - The function completed successfully\r
-    EFI_BAD_BUFFER_SIZE  - The write would span a target block, which is not \r
-                           a valid action.\r
-    EFI_ACCESS_DENIED    - No writes have been allocated.\r
-    EFI_NOT_FOUND        - Cannot find FVB by handle.\r
-    EFI_OUT_OF_RESOURCES - Cannot allocate memory.\r
-    EFI_ABORTED          - The function could not complete successfully.\r
-\r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice;\r
-  EFI_FTW_LITE_RECORD                 *Record;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_PHYSICAL_ADDRESS                FvbPhysicalAddress;\r
-  UINTN                               MyLength;\r
-  UINTN                               MyOffset;\r
-  UINTN                               MyBufferSize;\r
-  UINT8                               *MyBuffer;\r
-  UINTN                               SpareBufferSize;\r
-  UINT8                               *SpareBuffer;\r
-  UINTN                               Index;\r
-  UINT8                               *Ptr;\r
-  EFI_DEV_PATH_PTR                    DevPtr;\r
-\r
-  //\r
-  // Refresh work space and get last record\r
-  //\r
-  FtwLiteDevice = FTW_LITE_CONTEXT_FROM_THIS (This);\r
-  Status        = WorkSpaceRefresh (FtwLiteDevice);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Record = FtwLiteDevice->FtwLastRecord;\r
-\r
-  //\r
-  // Check the flags of last write record\r
-  //\r
-  if ((Record->WriteAllocated == FTW_VALID_STATE) || (Record->SpareCompleted == FTW_VALID_STATE)) {\r
-    return EFI_ACCESS_DENIED;\r
-  }\r
-  //\r
-  // IF former record has completed, THEN use next record\r
-  //\r
-  if (Record->WriteCompleted == FTW_VALID_STATE) {\r
-    Record++;\r
-    FtwLiteDevice->FtwLastRecord = Record;\r
-  }\r
-\r
-  MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-\r
-  //\r
-  // Check if the input data can fit within the target block\r
-  //\r
-  if ((Offset +*NumBytes) > FtwLiteDevice->SpareAreaLength) {\r
-    return EFI_BAD_BUFFER_SIZE;\r
-  }\r
-  //\r
-  // Check if there is enough free space for allocate a record\r
-  //\r
-  if ((MyOffset + WRITE_TOTAL_SIZE) > FtwLiteDevice->FtwWorkSpaceSize) {\r
-    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_ERROR, "FtwLite: Reclaim work space - %r", Status));\r
-      return EFI_ABORTED;\r
-    }\r
-  }\r
-  //\r
-  // Get the FVB protocol by handle\r
-  //\r
-  Status = FtwGetFvbByHandle (FvbHandle, &Fvb);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  // Allocate a write record in workspace.\r
-  // Update Header->WriteAllocated as VALID\r
-  //\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
-            WRITE_ALLOCATED\r
-            );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Allocate record - %r\n", Status));\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Record->WriteAllocated = FTW_VALID_STATE;\r
-\r
-  //\r
-  // Prepare data of write record, filling DevPath with memory mapped address.\r
-  //\r
-  DevPtr.MemMap                 = (MEMMAP_DEVICE_PATH *) &Record->DevPath;\r
-  DevPtr.MemMap->Header.Type    = HARDWARE_DEVICE_PATH;\r
-  DevPtr.MemMap->Header.SubType = HW_MEMMAP_DP;\r
-  SetDevicePathNodeLength (&DevPtr.MemMap->Header, sizeof (MEMMAP_DEVICE_PATH));\r
-\r
-  Status = Fvb->GetPhysicalAddress (Fvb, &FvbPhysicalAddress);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Get FVB physical address - %r\n", Status));\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  DevPtr.MemMap->MemoryType       = EfiMemoryMappedIO;\r
-  DevPtr.MemMap->StartingAddress  = FvbPhysicalAddress;\r
-  DevPtr.MemMap->EndingAddress    = FvbPhysicalAddress +*NumBytes;\r
-  //\r
-  // ignored!\r
-  //\r
-  Record->Lba       = Lba;\r
-  Record->Offset    = Offset;\r
-  Record->NumBytes  = *NumBytes;\r
-\r
-  //\r
-  // Write the record to the work space.\r
-  //\r
-  MyOffset  = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-  MyLength  = FTW_LITE_RECORD_SIZE;\r
-\r
-  Status = FtwLiteDevice->FtwFvBlock->Write (\r
-                                        FtwLiteDevice->FtwFvBlock,\r
-                                        FtwLiteDevice->FtwWorkSpaceLba,\r
-                                        FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
-                                        &MyLength,\r
-                                        (UINT8 *) Record\r
-                                        );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // Record has been written to working block, then write data.\r
-  //\r
-  //\r
-  // Allocate a memory buffer\r
-  //\r
-  MyBufferSize  = FtwLiteDevice->SpareAreaLength;\r
-  MyBuffer      = AllocatePool (MyBufferSize);\r
-  if (MyBuffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Starting at Lba, if the number of the rest blocks on Fvb is less\r
-  // than NumberOfSpareBlock.\r
-  //\r
-  //\r
-  // Read all original data from target block to memory buffer\r
-  //\r
-  if (IsInWorkingBlock (FtwLiteDevice, Fvb, Lba)) {\r
-    //\r
-    // If target block falls into working block, we must follow the process of\r
-    // updating working block.\r
-    //\r
-    Ptr = MyBuffer;\r
-    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
-      Status = FtwLiteDevice->FtwFvBlock->Read (\r
-                                            FtwLiteDevice->FtwFvBlock,\r
-                                            FtwLiteDevice->FtwWorkBlockLba + Index,\r
-                                            0,\r
-                                            &MyLength,\r
-                                            Ptr\r
-                                            );\r
-      if (EFI_ERROR (Status)) {\r
-        FreePool (MyBuffer);\r
-        return EFI_ABORTED;\r
-      }\r
-\r
-      Ptr += MyLength;\r
-    }\r
-    //\r
-    // Update Offset by adding the offset from the start LBA of working block to\r
-    // the target LBA. The target block can not span working block!\r
-    //\r
-    Offset = (((UINTN) (Lba - FtwLiteDevice->FtwWorkBlockLba)) * FtwLiteDevice->SizeOfSpareBlock + Offset);\r
-    ASSERT ((Offset +*NumBytes) <= FtwLiteDevice->SpareAreaLength);\r
-\r
-  } else {\r
-\r
-    Ptr = MyBuffer;\r
-    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      MyLength  = FtwLiteDevice->SizeOfSpareBlock;\r
-      Status    = Fvb->Read (Fvb, Lba + Index, 0, &MyLength, Ptr);\r
-      if (EFI_ERROR (Status)) {\r
-        FreePool (MyBuffer);\r
-        return EFI_ABORTED;\r
-      }\r
-\r
-      Ptr += MyLength;\r
-    }\r
-  }\r
-  //\r
-  // Overwrite the updating range data with\r
-  // the input buffer content\r
-  //\r
-  CopyMem (MyBuffer + Offset, Buffer, *NumBytes);\r
-\r
-  //\r
-  // Try to keep the content of spare block\r
-  // Save spare block into a spare backup memory buffer (Sparebuffer)\r
-  //\r
-  SpareBufferSize = FtwLiteDevice->SpareAreaLength;\r
-  SpareBuffer     = AllocatePool (SpareBufferSize);\r
-  if (SpareBuffer == NULL) {\r
-    FreePool (MyBuffer);\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Ptr = SpareBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &MyLength,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (MyBuffer);\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += MyLength;\r
-  }\r
-  //\r
-  // Write the memory buffer to spare block\r
-  // Don't forget to erase Flash first.\r
-  //\r
-  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
-  Ptr     = MyBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &MyLength,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (MyBuffer);\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += MyLength;\r
-  }\r
-  //\r
-  // Free MyBuffer\r
-  //\r
-  FreePool (MyBuffer);\r
-\r
-  //\r
-  // Set the SpareCompleteD in the FTW record,\r
-  //\r
-  MyOffset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + MyOffset,\r
-            SPARE_COMPLETED\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (SpareBuffer);\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Record->SpareCompleted = FTW_VALID_STATE;\r
-\r
-  //\r
-  //  Since the content has already backuped in spare block, the write is\r
-  //  guaranteed to be completed with fault tolerant manner.\r
-  //\r
-  Status = FtwWriteRecord (FtwLiteDevice, Fvb);\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (SpareBuffer);\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Record++;\r
-  FtwLiteDevice->FtwLastRecord = Record;\r
-\r
-  //\r
-  // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.\r
-  //\r
-  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
-  Ptr     = SpareBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    MyLength = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &MyLength,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += MyLength;\r
-  }\r
-  //\r
-  // All success.\r
-  //\r
-  FreePool (SpareBuffer);\r
-\r
-  DEBUG (\r
-    (EFI_D_FTW_LITE,\r
-    "FtwLite: Write() success, (Lba:Offset)=(%lx:0x%x), NumBytes: 0x%x\n",\r
-    Lba,\r
-    Offset,\r
-    *NumBytes)\r
-    );\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-FtwWriteRecord (\r
-  IN EFI_FTW_LITE_DEVICE                   *FtwLiteDevice,\r
-  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Write a record with fault tolerant mannaer.\r
-    Since the content has already backuped in spare block, the write is \r
-    guaranteed to be completed with fault tolerant manner.\r
-    \r
-Arguments:\r
-    FtwLiteDevice       - The private data of FTW_LITE driver\r
-    Fvb                 - The FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ABORTED         - The function could not complete successfully\r
-\r
---*/\r
-{\r
-  EFI_STATUS          Status;\r
-  EFI_FTW_LITE_RECORD *Record;\r
-  EFI_LBA             WorkSpaceLbaOffset;  \r
-  UINTN               Offset;\r
-\r
-  //\r
-  // Spare Complete but Destination not complete,\r
-  // Recover the targt block with the spare block.\r
-  //\r
-  Record = FtwLiteDevice->FtwLastRecord;\r
-\r
-  //\r
-  // IF target block is working block, THEN Flush Spare Block To Working Block;\r
-  // ELSE IF target block is boot block, THEN Flush Spare Block To boot Block;\r
-  // ELSE flush spare block to normal target block.ENDIF\r
-  //\r
-  if (IsInWorkingBlock (FtwLiteDevice, Fvb, Record->Lba)) {\r
-    //\r
-    // If target block is working block, Attention:\r
-    // it's required to set SPARE_COMPLETED to spare block.\r
-    //\r
-    WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
-    Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-    Status = FtwUpdateFvState (\r
-              FtwLiteDevice->FtwBackupFvb,\r
-              FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
-              FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
-              SPARE_COMPLETED\r
-              );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
-  } else if (IsBootBlock (FtwLiteDevice, Fvb, Record->Lba)) {\r
-    //\r
-    // Update boot block\r
-    //\r
-    Status = FlushSpareBlockToBootBlock (FtwLiteDevice);\r
-  } else {\r
-    //\r
-    // Update blocks other than working block or boot block\r
-    //\r
-    Status = FlushSpareBlockToTargetBlock (FtwLiteDevice, Fvb, Record->Lba);\r
-  }\r
-\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Set WriteCompleted flag in record\r
-  //\r
-  Offset = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
-            WRITE_COMPLETED\r
-            );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Record->WriteCompleted = FTW_VALID_STATE;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-FtwRestart (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Restarts a previously interrupted write. The caller must provide the \r
-    block protocol needed to complete the interrupted write.\r
-    \r
-Arguments:\r
-    FtwLiteDevice       - The private data of FTW_LITE driver\r
-    FvbHandle           - The handle of FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ACCESS_DENIED   - No pending writes exist\r
-    EFI_NOT_FOUND       - FVB protocol not found by the handle\r
-    EFI_ABORTED         - The function could not complete successfully\r
-\r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_FTW_LITE_RECORD                 *Record;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_DEV_PATH_PTR                    DevPathPtr;\r
-\r
-  //\r
-  // Spare Completed but Destination not complete,\r
-  // Recover the targt block with the spare block.\r
-  //\r
-  Record = FtwLiteDevice->FtwLastRecord;\r
-\r
-  //\r
-  // Only support memory mapped FVB device path by now.\r
-  //\r
-  DevPathPtr.MemMap = (MEMMAP_DEVICE_PATH *) &Record->DevPath;\r
-  if (!((DevPathPtr.MemMap->Header.Type == HARDWARE_DEVICE_PATH) && (DevPathPtr.MemMap->Header.SubType == HW_MEMMAP_DP))\r
-      ) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Device Path is not memory mapped\n"));\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Status = GetFvbByAddress (DevPathPtr.MemMap->StartingAddress, &Fvb);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  //  Since the content has already backuped in spare block, the write is\r
-  //  guaranteed to be completed with fault tolerant manner.\r
-  //\r
-  Status = FtwWriteRecord (FtwLiteDevice, Fvb);\r
-  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Restart() - %r\n", Status));\r
-\r
-  Record++;\r
-  FtwLiteDevice->FtwLastRecord = Record;\r
-\r
-  //\r
-  // Erase Spare block\r
-  // This is restart, no need to keep spareblock content.\r
-  //\r
-  FtwEraseSpareBlock (FtwLiteDevice);\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-FtwAbort (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Aborts all previous allocated writes.\r
-\r
-Arguments:\r
-    FtwLiteDevice    - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS      - The function completed successfully\r
-    EFI_ABORTED      - The function could not complete successfully.\r
-    EFI_NOT_FOUND    - No allocated writes exist.\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-  UINTN       Offset;\r
-\r
-  if (FtwLiteDevice->FtwLastRecord->WriteCompleted == FTW_VALID_STATE) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  // Update the complete state of the header as VALID and abort.\r
-  //\r
-  Offset = (UINT8 *) FtwLiteDevice->FtwLastRecord - FtwLiteDevice->FtwWorkSpace;\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + Offset,\r
-            WRITE_COMPLETED\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  FtwLiteDevice->FtwLastRecord->WriteCompleted  = FTW_VALID_STATE;\r
-\r
-  Status = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
-\r
-  //\r
-  // Erase the spare block\r
-  //\r
-  Status = FtwEraseSpareBlock (FtwLiteDevice);\r
-\r
-  DEBUG ((EFI_D_FTW_INFO, "FtwLite: Abort() success \n"));\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeFtwLite (\r
-  IN EFI_HANDLE         ImageHandle,\r
-  IN EFI_SYSTEM_TABLE   *SystemTable\r
-  )\r
-/*++\r
-  Routine Description: \r
-    This function is the entry point of the Fault Tolerant Write driver.\r
-  \r
-  Arguments: \r
-    ImageHandle   - EFI_HANDLE: A handle for the image that is initializing \r
-                    this driver\r
-    SystemTable   - EFI_SYSTEM_TABLE: A pointer to the EFI system table\r
-        \r
-  Returns:  \r
-    EFI_SUCCESS           - FTW has finished the initialization\r
-    EFI_ABORTED           - FTW initialization error\r
-\r
---*/\r
-{\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  UINTN                               Index;\r
-  EFI_HANDLE                          *HandleBuffer;\r
-  UINTN                               HandleCount;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-  EFI_PHYSICAL_ADDRESS                BaseAddress;\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice;\r
-  EFI_FTW_LITE_RECORD                 *Record;\r
-  UINTN                               Length;\r
-  EFI_STATUS                          Status;\r
-  UINTN                               Offset;\r
-  EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;\r
-  UINT32                              LbaIndex;\r
-\r
-  //\r
-  // Allocate Private data of this driver,\r
-  // INCLUDING THE FtwWorkSpace[FTW_WORK_SPACE_SIZE].\r
-  //\r
-  FtwLiteDevice = NULL;\r
-  FtwLiteDevice = AllocatePool (sizeof (EFI_FTW_LITE_DEVICE) + FTW_WORK_SPACE_SIZE);\r
-  if (FtwLiteDevice != NULL) {\r
-    Status = EFI_SUCCESS;\r
-  } else {\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  ZeroMem (FtwLiteDevice, sizeof (EFI_FTW_LITE_DEVICE));\r
-  FtwLiteDevice->Signature = FTW_LITE_DEVICE_SIGNATURE;\r
-\r
-  //\r
-  // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE.\r
-  //\r
-  FtwLiteDevice->FtwWorkSpace     = (UINT8 *) (FtwLiteDevice + 1);\r
-  FtwLiteDevice->FtwWorkSpaceSize = FTW_WORK_SPACE_SIZE;\r
-  SetMem (\r
-    FtwLiteDevice->FtwWorkSpace,\r
-    FtwLiteDevice->FtwWorkSpaceSize,\r
-    FTW_ERASED_BYTE\r
-    );\r
-  FtwLiteDevice->FtwWorkSpaceHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FtwLiteDevice->FtwWorkSpace;\r
-\r
-  FtwLiteDevice->FtwLastRecord      = NULL;\r
-\r
-  FtwLiteDevice->WorkSpaceAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwWorkingBase);\r
-  FtwLiteDevice->WorkSpaceLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwWorkingSize);\r
-\r
-  FtwLiteDevice->SpareAreaAddress = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageFtwSpareBase);\r
-  FtwLiteDevice->SpareAreaLength  = (UINTN) PcdGet32 (PcdFlashNvStorageFtwSpareSize);\r
-\r
-  ASSERT ((FtwLiteDevice->WorkSpaceLength != 0) && (FtwLiteDevice->SpareAreaLength != 0));\r
-\r
-  //\r
-  // Locate FVB protocol\r
-  //\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  NULL,\r
-                  &HandleCount,\r
-                  &HandleBuffer\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  ASSERT (HandleCount > 0);\r
-\r
-  FtwLiteDevice->FtwFvBlock       = NULL;\r
-  FtwLiteDevice->FtwBackupFvb     = NULL;\r
-  FtwLiteDevice->FtwWorkSpaceLba  = (EFI_LBA) (-1);\r
-  FtwLiteDevice->FtwSpareLba      = (EFI_LBA) (-1);\r
-  for (Index = 0; Index < HandleCount; Index += 1) {\r
-    Status = gBS->HandleProtocol (\r
-                    HandleBuffer[Index],\r
-                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                    (VOID **) &Fvb\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    Status = Fvb->GetPhysicalAddress (Fvb, &BaseAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) BaseAddress);\r
-\r
-    if ((FtwLiteDevice->WorkSpaceAddress >= BaseAddress) &&\r
-        (FtwLiteDevice->WorkSpaceAddress <= (BaseAddress + FwVolHeader->FvLength))\r
-        ) {\r
-      FtwLiteDevice->FtwFvBlock = Fvb;\r
-      //\r
-      // To get the LBA of work space\r
-      //\r
-      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-        //\r
-        // FV may have multiple types of BlockLength\r
-        //\r
-        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-        while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {\r
-          for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-            if (FtwLiteDevice->WorkSpaceAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
-              FtwLiteDevice->FtwWorkSpaceLba = LbaIndex - 1;\r
-              //\r
-              // Get the Work space size and Base(Offset)\r
-              //\r
-              FtwLiteDevice->FtwWorkSpaceSize = FtwLiteDevice->WorkSpaceLength;\r
-              FtwLiteDevice->FtwWorkSpaceBase = (UINTN) (FtwLiteDevice->WorkSpaceAddress - (BaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));\r
-              break;\r
-            }\r
-          }\r
-          //\r
-          // end for\r
-          //\r
-          FvbMapEntry++;\r
-        }\r
-        //\r
-        // end while\r
-        //\r
-      }\r
-    }\r
-\r
-    if ((FtwLiteDevice->SpareAreaAddress >= BaseAddress) &&\r
-        (FtwLiteDevice->SpareAreaAddress <= (BaseAddress + FwVolHeader->FvLength))\r
-        ) {\r
-      FtwLiteDevice->FtwBackupFvb = Fvb;\r
-      //\r
-      // To get the LBA of spare\r
-      //\r
-      if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {\r
-        //\r
-        // FV may have multiple types of BlockLength\r
-        //\r
-        FvbMapEntry = &FwVolHeader->BlockMap[0];\r
-        while (!((FvbMapEntry->NumBlocks == 0) && (FvbMapEntry->Length == 0))) {\r
-          for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {\r
-            if (FtwLiteDevice->SpareAreaAddress < (BaseAddress + FvbMapEntry->Length * LbaIndex)) {\r
-              //\r
-              // Get the NumberOfSpareBlock and SizeOfSpareBlock\r
-              //\r
-              FtwLiteDevice->FtwSpareLba        = LbaIndex - 1;\r
-              FtwLiteDevice->SizeOfSpareBlock   = FvbMapEntry->Length;\r
-              FtwLiteDevice->NumberOfSpareBlock = FtwLiteDevice->SpareAreaLength / FtwLiteDevice->SizeOfSpareBlock;\r
-              //\r
-              // Check the range of spare area to make sure that it's in FV range\r
-              //\r
-              ASSERT ((FtwLiteDevice->FtwSpareLba + FtwLiteDevice->NumberOfSpareBlock) <= FvbMapEntry->NumBlocks);\r
-              break;\r
-            }\r
-          }\r
-\r
-          FvbMapEntry++;\r
-        }\r
-        //\r
-        // end while\r
-        //\r
-      }\r
-    }\r
-  }\r
-  //\r
-  // Calculate the start LBA of working block. Working block is an area which\r
-  // contains working space in its last block and has the same size as spare\r
-  // block, unless there are not enough blocks before the block that contains\r
-  // working space.\r
-  //\r
-  FtwLiteDevice->FtwWorkBlockLba = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->NumberOfSpareBlock + 1;\r
-  if ((INT64) (FtwLiteDevice->FtwWorkBlockLba) < 0) {\r
-    FtwLiteDevice->FtwWorkBlockLba = 0;\r
-  }\r
-\r
-  if ((FtwLiteDevice->FtwFvBlock == NULL) ||\r
-      (FtwLiteDevice->FtwBackupFvb == NULL) ||\r
-      (FtwLiteDevice->FtwWorkSpaceLba == (EFI_LBA) (-1)) ||\r
-      (FtwLiteDevice->FtwSpareLba == (EFI_LBA) (-1))\r
-      ) {\r
-    DEBUG ((EFI_D_ERROR, "FtwLite: Working or spare FVB not ready\n"));\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-  //\r
-  // Refresh workspace data from working block\r
-  //\r
-  Status = WorkSpaceRefresh (FtwLiteDevice);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // If the working block workspace is not valid, try the spare block\r
-  //\r
-  if (!IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace invalid, read from backup\n"));\r
-    //\r
-    // Read from spare block\r
-    //\r
-    Length = FtwLiteDevice->FtwWorkSpaceSize;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba,\r
-                                            FtwLiteDevice->FtwWorkSpaceBase,\r
-                                            &Length,\r
-                                            FtwLiteDevice->FtwWorkSpace\r
-                                            );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    //\r
-    // If spare block is valid, then replace working block content.\r
-    //\r
-    if (IsValidWorkSpace (FtwLiteDevice->FtwWorkSpaceHeader)) {\r
-      Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart working block in Init() - %r\n", Status));\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
-      FtwAbort (FtwLiteDevice);\r
-      //\r
-      // Refresh work space.\r
-      //\r
-      Status = WorkSpaceRefresh (FtwLiteDevice);\r
-      if (EFI_ERROR (Status)) {\r
-        return EFI_ABORTED;\r
-      }\r
-    } else {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Both are invalid, init workspace\n"));\r
-      //\r
-      // If both are invalid, then initialize work space.\r
-      //\r
-      SetMem (\r
-        FtwLiteDevice->FtwWorkSpace,\r
-        FtwLiteDevice->FtwWorkSpaceSize,\r
-        FTW_ERASED_BYTE\r
-        );\r
-      InitWorkSpaceHeader (FtwLiteDevice->FtwWorkSpaceHeader);\r
-      //\r
-      // Write to work space on the working block\r
-      //\r
-      Length = FtwLiteDevice->FtwWorkSpaceSize;\r
-      Status = FtwLiteDevice->FtwFvBlock->Write (\r
-                                            FtwLiteDevice->FtwFvBlock,\r
-                                            FtwLiteDevice->FtwWorkSpaceLba,\r
-                                            FtwLiteDevice->FtwWorkSpaceBase,\r
-                                            &Length,\r
-                                            FtwLiteDevice->FtwWorkSpace\r
-                                            );\r
-      if (EFI_ERROR (Status)) {\r
-        return EFI_ABORTED;\r
-      }\r
-    }\r
-  }\r
-  //\r
-  // Hook the protocol API\r
-  //\r
-  FtwLiteDevice->FtwLiteInstance.Write = FtwLiteWrite;\r
-\r
-  //\r
-  // Install protocol interface\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &FtwLiteDevice->Handle,\r
-                  &gEfiFaultTolerantWriteLiteProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  &FtwLiteDevice->FtwLiteInstance\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // If (!SpareCompleted)  THEN  Abort to rollback.\r
-  //\r
-  if ((FtwLiteDevice->FtwLastRecord->WriteAllocated == FTW_VALID_STATE) &&\r
-      (FtwLiteDevice->FtwLastRecord->SpareCompleted != FTW_VALID_STATE)\r
-      ) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Init.. record not SpareCompleted, abort()\n"));\r
-    FtwAbort (FtwLiteDevice);\r
-  }\r
-  //\r
-  // if (SpareCompleted) THEN  Restart to fault tolerant write.\r
-  //\r
-  if ((FtwLiteDevice->FtwLastRecord->SpareCompleted == FTW_VALID_STATE) &&\r
-      (FtwLiteDevice->FtwLastRecord->WriteCompleted != FTW_VALID_STATE)\r
-      ) {\r
-\r
-    Status = FtwRestart (FtwLiteDevice);\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Restart last write - %r\n", Status));\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-  //\r
-  // To check the workspace buffer behind last records is EMPTY or not.\r
-  // If it's not EMPTY, FTW_LITE also need to call reclaim().\r
-  //\r
-  Record  = FtwLiteDevice->FtwLastRecord;\r
-  Offset  = (UINT8 *) Record - FtwLiteDevice->FtwWorkSpace;\r
-  if (FtwLiteDevice->FtwWorkSpace[Offset] != FTW_ERASED_BYTE) {\r
-    Offset += WRITE_TOTAL_SIZE;\r
-  }\r
-\r
-  if (!IsErasedFlashBuffer (\r
-        FTW_ERASE_POLARITY,\r
-        FtwLiteDevice->FtwWorkSpace + Offset,\r
-        FtwLiteDevice->FtwWorkSpaceSize - Offset\r
-        )) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace is dirty, call reclaim...\n"));\r
-    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Workspace reclaim - %r\n", Status));\r
-      return EFI_ABORTED;\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.dxs b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.dxs
deleted file mode 100644 (file)
index 93aa216..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-\r
-Module Name:\r
-  \r
-    FtwLite.dxs\r
-\r
-Abstract:\r
-\r
-  Dependency expression source file.\r
-  \r
---*/  \r
-#include <DxeDepex.h>\r
-\r
-\r
-DEPENDENCY_START\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID AND EFI_ALTERNATE_FV_BLOCK_GUID\r
-DEPENDENCY_END\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.h b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.h
deleted file mode 100644 (file)
index 8bc122d..0000000
+++ /dev/null
@@ -1,695 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-\r
-Module Name:\r
-\r
-  FtwLite.h\r
-\r
-Abstract:\r
-\r
-  This is a simple fault tolerant write driver, based on PlatformFd library.\r
-  And it only supports write BufferSize <= SpareAreaLength.\r
-\r
-  This boot service only protocol provides fault tolerant write capability for \r
-  block devices.  The protocol has internal non-volatile intermediate storage \r
-  of the data and private information. It should be able to recover \r
-  automatically from a critical fault, such as power failure. \r
-\r
---*/\r
-\r
-#ifndef _EFI_FAULT_TOLERANT_WRITE_LITE_H_\r
-#define _EFI_FAULT_TOLERANT_WRITE_LITE_H_\r
-\r
-//\r
-// The package level header files this module uses\r
-//\r
-#include <PiDxe.h>\r
-//\r
-// The protocols, PPI and GUID defintions for this module\r
-//\r
-#include <Protocol/PciRootBridgeIo.h>\r
-#include <Guid/SystemNvDataGuid.h>\r
-#include <Protocol/FaultTolerantWriteLite.h>\r
-#include <Protocol/FirmwareVolumeBlock.h>\r
-//\r
-// The Library classes this module consumes\r
-//\r
-#include <Library/PcdLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-\r
-#include <Common/WorkingBlockHeader.h>\r
-\r
-#define EFI_D_FTW_LITE  EFI_D_ERROR\r
-#define EFI_D_FTW_INFO  EFI_D_INFO\r
-\r
-//\r
-// Flash erase polarity is 1\r
-//\r
-#define FTW_ERASE_POLARITY  1\r
-\r
-#define FTW_VALID_STATE     0\r
-#define FTW_INVALID_STATE   1\r
-\r
-#define FTW_ERASED_BYTE     ((UINT8) (255))\r
-#define FTW_POLARITY_REVERT ((UINT8) (255))\r
-\r
-typedef struct {\r
-  UINT8         WriteAllocated : 1;\r
-  UINT8         SpareCompleted : 1;\r
-  UINT8         WriteCompleted : 1;\r
-  UINT8         Reserved : 5;\r
-#define WRITE_ALLOCATED 0x1\r
-#define SPARE_COMPLETED 0x2\r
-#define WRITE_COMPLETED 0x4\r
-\r
-  EFI_DEV_PATH  DevPath;\r
-  EFI_LBA       Lba;\r
-  UINTN         Offset;\r
-  UINTN         NumBytes;\r
-  //\r
-  // UINTN           SpareAreaOffset;\r
-  //\r
-} EFI_FTW_LITE_RECORD;\r
-\r
-#define FTW_LITE_DEVICE_SIGNATURE EFI_SIGNATURE_32 ('F', 'T', 'W', 'L')\r
-\r
-//\r
-// MACRO for Block size.\r
-// Flash Erasing will do in block granularity.\r
-//\r
-#ifdef FV_BLOCK_SIZE\r
-#define FTW_BLOCK_SIZE  FV_BLOCK_SIZE\r
-#else\r
-#define FV_BLOCK_SIZE   0x10000\r
-#define FTW_BLOCK_SIZE  FV_BLOCK_SIZE\r
-#endif\r
-//\r
-// MACRO for FTW WORK SPACE Base & Size\r
-//\r
-#ifdef EFI_FTW_WORKING_OFFSET\r
-#define FTW_WORK_SPACE_BASE EFI_FTW_WORKING_OFFSET\r
-#else\r
-#define FTW_WORK_SPACE_BASE 0x00E000\r
-#endif\r
-\r
-#ifdef EFI_FTW_WORKING_LENGTH\r
-#define FTW_WORK_SPACE_SIZE EFI_FTW_WORKING_LENGTH\r
-#else\r
-#define FTW_WORK_SPACE_SIZE 0x002000\r
-#endif\r
-//\r
-// MACRO for FTW header and record\r
-//\r
-#define FTW_WORKING_QUEUE_SIZE  (FTW_WORK_SPACE_SIZE - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER))\r
-#define FTW_LITE_RECORD_SIZE    (sizeof (EFI_FTW_LITE_RECORD))\r
-#define WRITE_TOTAL_SIZE        FTW_LITE_RECORD_SIZE\r
-\r
-//\r
-// EFI Fault tolerant protocol private data structure\r
-//\r
-typedef struct {\r
-  UINTN                                   Signature;\r
-  EFI_HANDLE                              Handle;\r
-  EFI_FTW_LITE_PROTOCOL                   FtwLiteInstance;\r
-  EFI_PHYSICAL_ADDRESS                    WorkSpaceAddress;\r
-  UINTN                                   WorkSpaceLength;\r
-  EFI_PHYSICAL_ADDRESS                    SpareAreaAddress;\r
-  UINTN                                   SpareAreaLength;\r
-  UINTN                                   NumberOfSpareBlock; // Number of the blocks in spare block\r
-  UINTN                                   SizeOfSpareBlock;   // Block size in bytes of the blocks in spare block\r
-  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FtwWorkSpaceHeader;\r
-  EFI_FTW_LITE_RECORD                     *FtwLastRecord;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FtwFvBlock;        // FVB of working block\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL      *FtwBackupFvb;      // FVB of spare block\r
-  EFI_LBA                                 FtwSpareLba;\r
-  EFI_LBA                                 FtwWorkBlockLba;    // Start LBA of working block\r
-  EFI_LBA                                 FtwWorkSpaceLba;    // Start LBA of working space\r
-  UINTN                                   FtwWorkSpaceBase;   // Offset from LBA start addr\r
-  UINTN                                   FtwWorkSpaceSize;\r
-  UINT8                                   *FtwWorkSpace;\r
-  //\r
-  // Following a buffer of FtwWorkSpace[FTW_WORK_SPACE_SIZE],\r
-  // Allocated with EFI_FTW_LITE_DEVICE.\r
-  //\r
-} EFI_FTW_LITE_DEVICE;\r
-\r
-#define FTW_LITE_CONTEXT_FROM_THIS(a) CR (a, EFI_FTW_LITE_DEVICE, FtwLiteInstance, FTW_LITE_DEVICE_SIGNATURE)\r
-\r
-//\r
-// Driver entry point\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeFtwLite (\r
-  IN EFI_HANDLE                 ImageHandle,\r
-  IN EFI_SYSTEM_TABLE           *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    This function is the entry point of the Fault Tolerant Write driver.\r
-\r
-Arguments:\r
-    ImageHandle   - EFI_HANDLE: A handle for the image that is initializing \r
-                    this driver\r
-    SystemTable   - EFI_SYSTEM_TABLE: A pointer to the EFI system table\r
-\r
-Returns:\r
-    EFI_SUCCESS           - FTW has finished the initialization\r
-    EFI_ABORTED           - FTW initialization error\r
-\r
---*/\r
-;\r
-\r
-//\r
-// Fault Tolerant Write Protocol API\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-FtwLiteWrite (\r
-  IN EFI_FTW_LITE_PROTOCOL                 *This,\r
-  IN EFI_HANDLE                            FvbHandle,\r
-  IN EFI_LBA                               Lba,\r
-  IN UINTN                                 Offset,\r
-  IN UINTN                                 *NumBytes,\r
-  IN VOID                                  *Buffer\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Starts a target block update. This function will record data about write \r
-    in fault tolerant storage and will complete the write in a recoverable \r
-    manner, ensuring at all times that either the original contents or \r
-    the modified contents are available.\r
-\r
-Arguments:\r
-    This             - Calling context\r
-    FvbHandle        - The handle of FVB protocol that provides services for \r
-                       reading, writing, and erasing the target block.\r
-    Lba              - The logical block address of the target block.  \r
-    Offset           - The offset within the target block to place the data.\r
-    NumBytes         - The number of bytes to write to the target block.\r
-    Buffer           - The data to write.\r
-\r
-Returns:\r
-    EFI_SUCCESS          - The function completed successfully\r
-    EFI_BAD_BUFFER_SIZE  - The write would span a target block, which is not \r
-                           a valid action.\r
-    EFI_ACCESS_DENIED    - No writes have been allocated.\r
-    EFI_NOT_FOUND        - Cannot find FVB by handle.\r
-    EFI_OUT_OF_RESOURCES - Cannot allocate memory.\r
-    EFI_ABORTED          - The function could not complete successfully.\r
-\r
---*/\r
-;\r
-\r
-//\r
-// Internal functions\r
-//\r
-EFI_STATUS\r
-FtwRestart (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Restarts a previously interrupted write. The caller must provide the \r
-    block protocol needed to complete the interrupted write.\r
-\r
-Arguments:\r
-    FtwLiteDevice       - The private data of FTW_LITE driver\r
-    FvbHandle           - The handle of FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ACCESS_DENIED   - No pending writes exist\r
-    EFI_NOT_FOUND       - FVB protocol not found by the handle\r
-    EFI_ABORTED         - The function could not complete successfully\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwAbort (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Aborts all previous allocated writes.\r
-\r
-Arguments:\r
-    FtwLiteDevice    - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS      - The function completed successfully\r
-    EFI_ABORTED      - The function could not complete successfully.\r
-    EFI_NOT_FOUND    - No allocated writes exist.\r
-\r
---*/\r
-;\r
-\r
-\r
-EFI_STATUS\r
-FtwWriteRecord (\r
-  IN EFI_FTW_LITE_DEVICE                   *FtwLiteDevice,\r
-  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *Fvb\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Write a record with fault tolerant mannaer.\r
-    Since the content has already backuped in spare block, the write is \r
-    guaranteed to be completed with fault tolerant manner.\r
-\r
-Arguments:\r
-    FtwLiteDevice       - The private data of FTW_LITE driver\r
-    Fvb                 - The FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ABORTED         - The function could not complete successfully\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwEraseBlock (\r
-  IN EFI_FTW_LITE_DEVICE              *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    To Erase one block. The size is FTW_BLOCK_SIZE\r
-\r
-Arguments:\r
-    FtwLiteDevice - Calling context\r
-    FvBlock       - FVB Protocol interface\r
-    Lba           - Lba of the firmware block\r
-\r
-Returns:\r
-    EFI_SUCCESS   - Block LBA is Erased successfully\r
-    Others        - Error occurs\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwEraseSpareBlock (\r
-  IN EFI_FTW_LITE_DEVICE   *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Erase spare block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-\r
-Returns:\r
-\r
-  Status code\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwGetFvbByHandle (\r
-  IN EFI_HANDLE                           FvBlockHandle,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Retrive the proper FVB protocol interface by HANDLE.\r
-\r
-Arguments:\r
-    FvBlockHandle       - The handle of FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-    FvBlock             - The interface of FVB protocol\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ABORTED         - The function could not complete successfully\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-GetFvbByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS               Address,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get firmware block by address.\r
-\r
-Arguments:\r
-\r
-  Address - Address specified the block\r
-  FvBlock - The block caller wanted\r
-\r
-Returns:\r
-\r
-  Status code\r
-\r
-  EFI_NOT_FOUND - Block not found\r
-\r
---*/\r
-;\r
-\r
-BOOLEAN\r
-IsInWorkingBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Is it in working block?\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - The block specified\r
-\r
-Returns:\r
-\r
-  In working block or not\r
-\r
---*/\r
-;\r
-\r
-BOOLEAN\r
-IsBootBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether the block is a boot block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - Lba value\r
-\r
-Returns:\r
-\r
-  Is a boot block or not\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToTargetBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-    FvBlock        - FVB Protocol interface to access target block\r
-    Lba            - Lba of the target block\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to target block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToWorkingBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Working block is accessed by FTW working FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwWorkBlockLba.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to target block\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-    Since the working block header is important when FTW initializes, the \r
-    state of the operation should be handled carefully. The Crc value is \r
-    calculated without STATE element. \r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToBootBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to boot block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwUpdateFvState (\r
-  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  IN EFI_LBA                             Lba,\r
-  IN UINTN                               Offset,\r
-  IN UINT8                               NewBit\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Update a bit of state on a block device. The location of the bit is \r
-    calculated by the (Lba, Offset, bit). Here bit is determined by the \r
-    the name of a certain bit.\r
-\r
-Arguments:\r
-    FvBlock    - FVB Protocol interface to access SrcBlock and DestBlock\r
-    Lba        - Lba of a block\r
-    Offset     - Offset on the Lba\r
-    NewBit     - New value that will override the old value if it can be change\r
-\r
-Returns:\r
-    EFI_SUCCESS   - A state bit has been updated successfully\r
-    Others        - Access block device error.\r
-\r
-Notes:\r
-    Assume all bits of State are inside the same BYTE. \r
-\r
-    EFI_ABORTED   - Read block fail\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwGetLastRecord (\r
-  IN  EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
-  OUT EFI_FTW_LITE_RECORD  **FtwLastRecord\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Get the last Write record pointer. \r
-    The last record is the record whose 'complete' state hasn't been set.\r
-    After all, this header may be a EMPTY header entry for next Allocate. \r
-\r
-Arguments:\r
-    FtwLiteDevice   - Private data of this driver\r
-    FtwLastRecord   - Pointer to retrieve the last write record\r
-\r
-Returns:\r
-    EFI_SUCCESS     - Get the last write record successfully\r
-    EFI_ABORTED     - The FTW work space is damaged\r
-\r
---*/\r
-;\r
-\r
-BOOLEAN\r
-IsErasedFlashBuffer (\r
-  IN BOOLEAN         Polarity,\r
-  IN UINT8           *Buffer,\r
-  IN UINTN           BufferSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether a flash buffer is erased.\r
-\r
-Arguments:\r
-\r
-  Polarity    - All 1 or all 0\r
-  Buffer      - Buffer to check\r
-  BufferSize  - Size of the buffer\r
-\r
-Returns:\r
-\r
-  Erased or not.\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-InitWorkSpaceHeader (\r
-  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Initialize a work space when there is no work space.\r
-\r
-Arguments:\r
-    WorkingHeader - Pointer of working block header \r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-WorkSpaceRefresh (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Read from working block to refresh the work space in memory.\r
-\r
-Arguments:\r
-    FtwLiteDevice     - Point to private data of FTW driver\r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-;\r
-\r
-BOOLEAN\r
-IsValidWorkSpace (\r
-  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Check to see if it is a valid work space.\r
-\r
-Arguments:\r
-    WorkingHeader - Pointer of working block header \r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-CleanupWorkSpace (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
-  IN OUT UINT8            *BlockBuffer,\r
-  IN UINTN                BufferSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Reclaim the work space. Get rid of all the completed write records\r
-    and write records in the Fault Tolerant work space.\r
-\r
-Arguments:\r
-    FtwLiteDevice   - Point to private data of FTW driver\r
-    FtwSpaceBuffer  - Buffer to contain the reclaimed clean data\r
-    BufferSize      - Size of the FtwSpaceBuffer\r
-\r
-Returns:\r
-    EFI_SUCCESS           - The function completed successfully\r
-    EFI_BUFFER_TOO_SMALL  - The FtwSpaceBuffer is too small\r
-    EFI_ABORTED           - The function could not complete successfully.\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-FtwReclaimWorkSpace (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Reclaim the work space on the working block.\r
-\r
-Arguments:\r
-    FtwLiteDevice     - Point to private data of FTW driver\r
-\r
-Returns:\r
-    EFI_SUCCESS           - The function completed successfully\r
-    EFI_OUT_OF_RESOURCES  - Allocate memory error\r
-    EFI_ABORTED           - The function could not complete successfully\r
-\r
---*/\r
-;\r
-\r
-#endif\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.inf b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.inf
deleted file mode 100644 (file)
index 4f47d60..0000000
+++ /dev/null
@@ -1,141 +0,0 @@
-#/** @file\r
-# Component description file for FtwLite module.\r
-#\r
-# This driver provides fault tolerant write capability for block devices.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\r
-#  All rights reserved. This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution. The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-#\r
-#**/\r
-\r
-################################################################################\r
-#\r
-# Defines Section - statements that will be processed to create a Makefile.\r
-#\r
-################################################################################\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = FtwLite\r
-  FILE_GUID                      = 4C862FC6-0E54-4e36-8C8F-FF6F3167951F\r
-  MODULE_TYPE                    = DXE_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  EDK_RELEASE_VERSION            = 0x00020000\r
-  EFI_SPECIFICATION_VERSION      = 0x00020000\r
-\r
-  ENTRY_POINT                    = InitializeFtwLite\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-################################################################################\r
-#\r
-# Sources Section - list of files that are required for the build to succeed.\r
-#\r
-################################################################################\r
-\r
-[Sources.common]\r
-  FtwWorkSpace.c\r
-  FtwMisc.c\r
-  FtwLite.c\r
-  FtwLite.h\r
\r
-[Sources.Ia32]\r
-  Ia32/Ia32FtwMisc.c\r
-\r
-[Sources.X64]\r
-  x64/x64FtwMisc.c\r
-\r
-[Sources.IPF]\r
-  Ipf/IpfFtwMisc.c\r
-\r
-[Sources.EBC]\r
-  Ia32/Ia32FtwMisc.c\r
-\r
-\r
-################################################################################\r
-#\r
-# Package Dependency Section - list of Package files that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-  MdeModulePkg/MdeModulePkg.dec\r
-\r
-\r
-\r
-################################################################################\r
-#\r
-# Library Class Section - list of Library Classes that are required for\r
-#                         this module.\r
-#\r
-################################################################################\r
-\r
-[LibraryClasses]\r
-  UefiBootServicesTableLib\r
-  MemoryAllocationLib\r
-  BaseMemoryLib\r
-  UefiDriverEntryPoint\r
-  DebugLib\r
-  PcdLib\r
-  HobLib\r
-\r
-################################################################################\r
-#\r
-# Guid C Name Section - list of Guids that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Guids]\r
-  gEfiSystemNvDataFvGuid                        # ALWAYS_CONSUMED\r
-  gEfiFlashMapHobGuid\r
-\r
-################################################################################\r
-#\r
-# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
-#                           that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Protocols]\r
-  gEfiFirmwareVolumeBlockProtocolGuid           # PROTOCOL ALWAYS_CONSUMED\r
-  gEfiFaultTolerantWriteLiteProtocolGuid        # PROTOCOL ALWAYS_PRODUCED\r
-\r
-[Protocols.IA32]\r
-  gEfiPciRootBridgeIoProtocolGuid               # PROTOCOL ALWAYS_CONSUMED\r
-\r
-[Protocols.EBC]\r
-  gEfiPciRootBridgeIoProtocolGuid               # PROTOCOL ALWAYS_CONSUMED\r
-\r
-\r
-################################################################################\r
-#\r
-# Pcd DYNAMIC - list of PCDs that this module is coded for.\r
-#\r
-################################################################################\r
-\r
-[PcdsDynamic.common]\r
-  PcdFlashNvStorageFtwWorkingSize|gEfiMdeModulePkgTokenSpaceGuid\r
-  PcdFlashNvStorageFtwWorkingBase|gEfiMdeModulePkgTokenSpaceGuid\r
-  PcdFlashNvStorageFtwSpareSize|gEfiMdeModulePkgTokenSpaceGuid\r
-  PcdFlashNvStorageFtwSpareBase|gEfiMdeModulePkgTokenSpaceGuid\r
-\r
-################################################################################\r
-#\r
-# Dependency Expression Section - list of Dependency expressions that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Depex]\r
-  gEfiFirmwareVolumeBlockProtocolGuid AND gEfiAlternateFvBlockGuid\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.msa b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwLite.msa
deleted file mode 100644 (file)
index 7362f47..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
-  <MsaHeader>\r
-    <ModuleName>FtwLite</ModuleName>\r
-    <ModuleType>DXE_DRIVER</ModuleType>\r
-    <GuidValue>4C862FC6-0E54-4e36-8C8F-FF6F3167951F</GuidValue>\r
-    <Version>1.0</Version>\r
-    <Abstract>Component description file for FtwLite module.</Abstract>\r
-    <Description>This driver provides fault tolerant write capability for block devices.</Description>\r
-    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
-    <License>All rights reserved. This program and the accompanying materials\r
-      are licensed and made available under the terms and conditions of the BSD License\r
-      which accompanies this distribution.  The full text of the license may be found at\r
-      http://opensource.org/licenses/bsd-license.php\r
-      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.</License>\r
-    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
-  </MsaHeader>\r
-  <ModuleDefinitions>\r
-    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
-    <BinaryModule>false</BinaryModule>\r
-    <OutputFileBasename>FtwLite</OutputFileBasename>\r
-  </ModuleDefinitions>\r
-  <LibraryClassDefinitions>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>PcdLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>DebugLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiDriverEntryPoint</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>BaseMemoryLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>MemoryAllocationLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiBootServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-  </LibraryClassDefinitions>\r
-  <SourceFiles>\r
-    <Filename>FtwLite.h</Filename>\r
-    <Filename>FtwLite.c</Filename>\r
-    <Filename>FtwMisc.c</Filename>\r
-    <Filename>FtwWorkSpace.c</Filename>\r
-    <Filename>FtwLite.dxs</Filename>\r
-    <Filename SupArchList="IA32 EBC">Ia32/Ia32FtwMisc.c</Filename>\r
-    <Filename SupArchList="X64">x64/x64FtwMisc.c</Filename>\r
-    <Filename SupArchList="IPF">Ipf/IpfFtwMisc.c</Filename>\r
-  </SourceFiles>\r
-  <PackageDependencies>\r
-    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
-    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
-  </PackageDependencies>\r
-  <Protocols>\r
-    <Protocol Usage="ALWAYS_PRODUCED">\r
-      <ProtocolCName>gEfiFaultTolerantWriteLiteProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-    <Protocol Usage="ALWAYS_CONSUMED" SupArchList="IA32 EBC">\r
-      <ProtocolCName>gEfiPciRootBridgeIoProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-    <Protocol Usage="ALWAYS_CONSUMED">\r
-      <ProtocolCName>gEfiFirmwareVolumeBlockProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-  </Protocols>\r
-  <Guids>\r
-    <GuidCNames Usage="ALWAYS_CONSUMED">\r
-      <GuidCName>gEfiSystemNvDataFvGuid</GuidCName>\r
-    </GuidCNames>\r
-  </Guids>\r
-  <Externs>\r
-    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
-    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
-    <Extern>\r
-      <ModuleEntryPoint>InitializeFtwLite</ModuleEntryPoint>\r
-    </Extern>\r
-  </Externs>\r
-  <PcdCoded>\r
-    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
-      <C_Name>PcdFlashNvStorageFtwSpareBase</C_Name>\r
-      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
-      <HelpText>To get base address of the FTW spare block section in NV firmware volume.</HelpText>\r
-    </PcdEntry>\r
-    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
-      <C_Name>PcdFlashNvStorageFtwSpareSize</C_Name>\r
-      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
-      <HelpText>To get size of the FTW spare block section in NV firmware volume.</HelpText>\r
-    </PcdEntry>\r
-    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
-      <C_Name>PcdFlashNvStorageFtwWorkingBase</C_Name>\r
-      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
-      <HelpText>To get base address of the FTW working block section in NV firmware volume.</HelpText>\r
-    </PcdEntry>\r
-    <PcdEntry PcdItemType="DYNAMIC" Usage="ALWAYS_CONSUMED">\r
-      <C_Name>PcdFlashNvStorageFtwWorkingSize</C_Name>\r
-      <TokenSpaceGuidCName>gEfiGenericPlatformTokenSpaceGuid</TokenSpaceGuidCName>\r
-      <HelpText>To get size of the FTW working block section in NV firmware volume.</HelpText>\r
-    </PcdEntry>\r
-  </PcdCoded>\r
-</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwMisc.c
deleted file mode 100644 (file)
index eb334ea..0000000
+++ /dev/null
@@ -1,530 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
-  FtwMisc.c\r
-  \r
-Abstract:\r
-  \r
-  Internal functions to support fault tolerant write.\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-#include <FtwLite.h>\r
-\r
-BOOLEAN\r
-IsErasedFlashBuffer (\r
-  IN BOOLEAN         Polarity,\r
-  IN UINT8           *Buffer,\r
-  IN UINTN           BufferSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether a flash buffer is erased.\r
-\r
-Arguments:\r
-\r
-  Polarity    - All 1 or all 0\r
-  Buffer      - Buffer to check\r
-  BufferSize  - Size of the buffer\r
-\r
-Returns:\r
-\r
-  Erased or not.\r
-\r
---*/\r
-{\r
-  UINT8 ErasedValue;\r
-  UINT8 *Ptr;\r
-\r
-  if (Polarity) {\r
-    ErasedValue = 0xFF;\r
-  } else {\r
-    ErasedValue = 0;\r
-  }\r
-\r
-  Ptr = Buffer;\r
-  while (BufferSize--) {\r
-    if (*Ptr++ != ErasedValue) {\r
-      return FALSE;\r
-    }\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-EFI_STATUS\r
-FtwEraseBlock (\r
-  IN EFI_FTW_LITE_DEVICE              *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    To Erase one block. The size is FTW_BLOCK_SIZE\r
-\r
-Arguments:\r
-    FtwLiteDevice - Calling context\r
-    FvBlock       - FVB Protocol interface\r
-    Lba        - Lba of the firmware block\r
-\r
-Returns:\r
-    EFI_SUCCESS   - Block LBA is Erased successfully\r
-    Others        - Error occurs\r
-\r
---*/\r
-{\r
-  return FvBlock->EraseBlocks (\r
-                    FvBlock,\r
-                    Lba,\r
-                    FtwLiteDevice->NumberOfSpareBlock,\r
-                    EFI_LBA_LIST_TERMINATOR\r
-                    );\r
-}\r
-\r
-EFI_STATUS\r
-FtwEraseSpareBlock (\r
-  IN EFI_FTW_LITE_DEVICE   *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Erase spare block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-\r
-Returns:\r
-\r
-  Status code\r
-\r
---*/\r
-{\r
-  return FtwLiteDevice->FtwBackupFvb->EraseBlocks (\r
-                                        FtwLiteDevice->FtwBackupFvb,\r
-                                        FtwLiteDevice->FtwSpareLba,\r
-                                        FtwLiteDevice->NumberOfSpareBlock,\r
-                                        EFI_LBA_LIST_TERMINATOR\r
-                                        );\r
-}\r
-\r
-EFI_STATUS\r
-FtwGetFvbByHandle (\r
-  IN EFI_HANDLE                           FvBlockHandle,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Retrive the proper FVB protocol interface by HANDLE.\r
-\r
-Arguments:\r
-    FvBlockHandle       - The handle of FVB protocol that provides services for \r
-                          reading, writing, and erasing the target block.\r
-    FvBlock             - The interface of FVB protocol\r
-\r
-Returns:\r
-    EFI_SUCCESS         - The function completed successfully\r
-    EFI_ABORTED         - The function could not complete successfully\r
---*/\r
-{\r
-  //\r
-  // To get the FVB protocol interface on the handle\r
-  //\r
-  return gBS->HandleProtocol (\r
-                FvBlockHandle,\r
-                &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                (VOID **) FvBlock\r
-                );\r
-}\r
-\r
-EFI_STATUS\r
-GetFvbByAddress (\r
-  IN  EFI_PHYSICAL_ADDRESS               Address,\r
-  OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get firmware block by address.\r
-\r
-Arguments:\r
-\r
-  Address - Address specified the block\r
-  FvBlock - The block caller wanted\r
-\r
-Returns:\r
-\r
-  Status code\r
-\r
-  EFI_NOT_FOUND - Block not found\r
-\r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_HANDLE                          *HandleBuffer;\r
-  UINTN                               HandleCount;\r
-  UINTN                               Index;\r
-  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
-  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;\r
-\r
-  *FvBlock = NULL;\r
-  //\r
-  // Locate all handles of Fvb protocol\r
-  //\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                  NULL,\r
-                  &HandleCount,\r
-                  &HandleBuffer\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  //\r
-  // Search all FVB until find the right one\r
-  //\r
-  for (Index = 0; Index < HandleCount; Index += 1) {\r
-    Status = gBS->HandleProtocol (\r
-                    HandleBuffer[Index],\r
-                    &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                    (VOID **) &Fvb\r
-                    );\r
-    if (EFI_ERROR (Status)) {\r
-      Status = EFI_NOT_FOUND;\r
-      break;\r
-    }\r
-    //\r
-    // Compare the address and select the right one\r
-    //\r
-    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      continue;\r
-    }\r
-\r
-    FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r
-    if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + (FwVolHeader->FvLength - 1)))) {\r
-      *FvBlock  = Fvb;\r
-      Status    = EFI_SUCCESS;\r
-      break;\r
-    }\r
-  }\r
-\r
-  FreePool (HandleBuffer);\r
-  return Status;\r
-}\r
-\r
-BOOLEAN\r
-IsInWorkingBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Is it in working block?\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - The block specified\r
-\r
-Returns:\r
-\r
-  In working block or not\r
-\r
---*/\r
-{\r
-  //\r
-  // If matching the following condition, the target block is in working block.\r
-  // 1. Target block is on the FV of working block (Using the same FVB protocol instance).\r
-  // 2. Lba falls into the range of working block.\r
-  //\r
-  return (BOOLEAN)\r
-    (\r
-      (FvBlock == FtwLiteDevice->FtwFvBlock) &&\r
-      (Lba >= FtwLiteDevice->FtwWorkBlockLba) &&\r
-      (Lba <= FtwLiteDevice->FtwWorkSpaceLba)\r
-    );\r
-}\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToTargetBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a target block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Target block is accessed by FvBlock protocol interface. LBA is Lba.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-    FvBlock        - FVB Protocol interface to access target block\r
-    Lba            - Lba of the target block\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to target block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-  UINTN       Length;\r
-  UINT8       *Buffer;\r
-  UINTN       Count;\r
-  UINT8       *Ptr;\r
-  UINTN       Index;\r
-\r
-  if ((FtwLiteDevice == NULL) || (FvBlock == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  //\r
-  // Allocate a memory buffer\r
-  //\r
-  Length  = FtwLiteDevice->SpareAreaLength;\r
-  Buffer  = AllocatePool (Length);\r
-  if (Buffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Read all content of spare block to memory buffer\r
-  //\r
-  Ptr = Buffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Count = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Count,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-\r
-    Ptr += Count;\r
-  }\r
-  //\r
-  // Erase the target block\r
-  //\r
-  Status = FtwEraseBlock (FtwLiteDevice, FvBlock, Lba);\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (Buffer);\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // Write memory buffer to block, using the FvbBlock protocol interface\r
-  //\r
-  Ptr = Buffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Count   = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status  = FvBlock->Write (FvBlock, Lba + Index, 0, &Count, Ptr);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-\r
-    Ptr += Count;\r
-  }\r
-\r
-  FreePool (Buffer);\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToWorkingBlock (\r
-  EFI_FTW_LITE_DEVICE          *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to working block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Working block is accessed by FTW working FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwWorkBlockLba.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to target block\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-    Since the working block header is important when FTW initializes, the \r
-    state of the operation should be handled carefully. The Crc value is \r
-    calculated without STATE element. \r
-\r
---*/\r
-{\r
-  EFI_STATUS                              Status;\r
-  UINTN                                   Length;\r
-  UINT8                                   *Buffer;\r
-  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
-  EFI_LBA                                 WorkSpaceLbaOffset;\r
-  UINTN                                   Count;\r
-  UINT8                                   *Ptr;\r
-  UINTN                                   Index;\r
-\r
-  //\r
-  // Allocate a memory buffer\r
-  //\r
-  Length  = FtwLiteDevice->SpareAreaLength;\r
-  Buffer  = AllocatePool (Length);\r
-  if (Buffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // To  guarantee that the WorkingBlockValid is set on spare block\r
-  //\r
-  WorkSpaceLbaOffset = FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba;\r
-  FtwUpdateFvState (\r
-    FtwLiteDevice->FtwBackupFvb,\r
-    FtwLiteDevice->FtwSpareLba + WorkSpaceLbaOffset,\r
-    FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
-    WORKING_BLOCK_VALID\r
-    );\r
-  //\r
-  // Read from spare block to memory buffer\r
-  //\r
-  Ptr = Buffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Count = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Count,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-\r
-    Ptr += Count;\r
-  }\r
-  //\r
-  // Clear the CRC and STATE, copy data from spare to working block.\r
-  //\r
-  WorkingBlockHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) (Buffer + (UINTN) WorkSpaceLbaOffset * FtwLiteDevice->SizeOfSpareBlock + FtwLiteDevice->FtwWorkSpaceBase);\r
-  InitWorkSpaceHeader (WorkingBlockHeader);\r
-  WorkingBlockHeader->WorkingBlockValid   = FTW_ERASE_POLARITY;\r
-  WorkingBlockHeader->WorkingBlockInvalid = FTW_ERASE_POLARITY;\r
-\r
-  //\r
-  // target block is working block, then\r
-  //   Set WorkingBlockInvalid in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER\r
-  //   before erase the working block.\r
-  //\r
-  //  Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
-  //                            WorkingBlockInvalid);\r
-  // To skip Signature and Crc: sizeof(EFI_GUID)+sizeof(UINT32).\r
-  //\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
-            WORKING_BLOCK_INVALID\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (Buffer);\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockInvalid = FTW_VALID_STATE;\r
-\r
-  //\r
-  // Erase the working block\r
-  //\r
-  Status = FtwEraseBlock (\r
-            FtwLiteDevice,\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkBlockLba\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (Buffer);\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // Write memory buffer to working block, using the FvbBlock protocol interface\r
-  //\r
-  Ptr = Buffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Count = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwFvBlock->Write (\r
-                                          FtwLiteDevice->FtwFvBlock,\r
-                                          FtwLiteDevice->FtwWorkBlockLba + Index,\r
-                                          0,\r
-                                          &Count,\r
-                                          Ptr\r
-                                          );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write block - %r\n", Status));\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-\r
-    Ptr += Count;\r
-  }\r
-  //\r
-  // Since the memory buffer will not be used, free memory Buffer.\r
-  //\r
-  FreePool (Buffer);\r
-\r
-  //\r
-  // Update the VALID of the working block\r
-  //\r
-  // Offset = EFI_FIELD_OFFSET(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER,\r
-  //                           WorkingBlockValid);\r
-  // Hardcode offset sizeof(EFI_GUID)+sizeof(UINT32), to skip Signature and Crc\r
-  //\r
-  Status = FtwUpdateFvState (\r
-            FtwLiteDevice->FtwFvBlock,\r
-            FtwLiteDevice->FtwWorkSpaceLba,\r
-            FtwLiteDevice->FtwWorkSpaceBase + sizeof (EFI_GUID) + sizeof (UINT32),\r
-            WORKING_BLOCK_VALID\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  FtwLiteDevice->FtwWorkSpaceHeader->WorkingBlockValid = FTW_VALID_STATE;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwWorkSpace.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/FtwWorkSpace.c
deleted file mode 100644 (file)
index 00d7d8e..0000000
+++ /dev/null
@@ -1,561 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
-  FtwWorkSpace.c\r
-  \r
-Abstract:\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-\r
-#include <FtwLite.h>\r
-\r
-BOOLEAN\r
-IsValidWorkSpace (\r
-  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Check to see if it is a valid work space.\r
-\r
-Arguments:\r
-    WorkingHeader - Pointer of working block header \r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER WorkingBlockHeader;\r
-\r
-  ASSERT (WorkingHeader != NULL);\r
-  if (WorkingHeader->WorkingBlockValid != FTW_VALID_STATE) {\r
-    return FALSE;\r
-  }\r
-  //\r
-  // Check signature with gEfiSystemNvDataFvGuid\r
-  //\r
-  if (!CompareGuid (&gEfiSystemNvDataFvGuid, &WorkingHeader->Signature)) {\r
-    return FALSE;\r
-  }\r
-  //\r
-  // Check the CRC of header\r
-  //\r
-  CopyMem (\r
-    &WorkingBlockHeader,\r
-    WorkingHeader,\r
-    sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER)\r
-    );\r
-\r
-  //\r
-  // Filter out the Crc and State fields\r
-  //\r
-  SetMem (\r
-    &WorkingBlockHeader.Crc,\r
-    sizeof (UINT32),\r
-    FTW_ERASED_BYTE\r
-    );\r
-  WorkingBlockHeader.WorkingBlockValid    = FTW_ERASE_POLARITY;\r
-  WorkingBlockHeader.WorkingBlockInvalid  = FTW_ERASE_POLARITY;\r
-\r
-  //\r
-  // Calculate the Crc of woking block header\r
-  //\r
-  Status = gBS->CalculateCrc32 (\r
-                  (UINT8 *) &WorkingBlockHeader,\r
-                  sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
-                  &WorkingBlockHeader.Crc\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  if (WorkingBlockHeader.Crc != WorkingHeader->Crc) {\r
-    DEBUG ((EFI_D_FTW_LITE, "FtwLite: Work block header CRC check error\n"));\r
-    return FALSE;\r
-  }\r
-\r
-  return TRUE;\r
-}\r
-\r
-EFI_STATUS\r
-InitWorkSpaceHeader (\r
-  IN EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingHeader\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Initialize a work space when there is no work space.\r
-\r
-Arguments:\r
-    WorkingHeader - Pointer of working block header \r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  ASSERT (WorkingHeader != NULL);\r
-\r
-  //\r
-  // Here using gEfiSystemNvDataFvGuid as the signature.\r
-  //\r
-  CopyMem (\r
-    &WorkingHeader->Signature,\r
-    &gEfiSystemNvDataFvGuid,\r
-    sizeof (EFI_GUID)\r
-    );\r
-  WorkingHeader->WriteQueueSize = FTW_WORKING_QUEUE_SIZE;\r
-\r
-  //\r
-  // Crc is calculated with all the fields except Crc and STATE\r
-  //\r
-  WorkingHeader->WorkingBlockValid    = FTW_ERASE_POLARITY;\r
-  WorkingHeader->WorkingBlockInvalid  = FTW_ERASE_POLARITY;\r
-  SetMem (&WorkingHeader->Crc, sizeof (UINT32), FTW_ERASED_BYTE);\r
-\r
-  //\r
-  // Calculate the CRC value\r
-  //\r
-  Status = gBS->CalculateCrc32 (\r
-                  (UINT8 *) WorkingHeader,\r
-                  sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
-                  &WorkingHeader->Crc\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Restore the WorkingBlockValid flag to VALID state\r
-  //\r
-  WorkingHeader->WorkingBlockValid    = FTW_VALID_STATE;\r
-  WorkingHeader->WorkingBlockInvalid  = FTW_INVALID_STATE;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-FtwUpdateFvState (\r
-  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  IN EFI_LBA                             Lba,\r
-  IN UINTN                               Offset,\r
-  IN UINT8                               NewBit\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Update a bit of state on a block device. The location of the bit is \r
-    calculated by the (Lba, Offset, bit). Here bit is determined by the \r
-    the name of a certain bit.\r
-\r
-Arguments:\r
-    FvBlock    - FVB Protocol interface to access SrcBlock and DestBlock\r
-    Lba        - Lba of a block\r
-    Offset     - Offset on the Lba\r
-    NewBit     - New value that will override the old value if it can be change\r
-\r
-Returns:\r
-    EFI_SUCCESS   - A state bit has been updated successfully\r
-    Others        - Access block device error.\r
-\r
-Notes:\r
-    Assume all bits of State are inside the same BYTE. \r
-\r
-    EFI_ABORTED   - Read block fail\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-  UINT8       State;\r
-  UINTN       Length;\r
-\r
-  //\r
-  // Read state from device, assume State is only one byte.\r
-  //\r
-  Length  = sizeof (UINT8);\r
-  Status  = FvBlock->Read (FvBlock, Lba, Offset, &Length, &State);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  State ^= FTW_POLARITY_REVERT;\r
-  State = (UINT8) (State | NewBit);\r
-  State ^= FTW_POLARITY_REVERT;\r
-\r
-  //\r
-  // Write state back to device\r
-  //\r
-  Length  = sizeof (UINT8);\r
-  Status  = FvBlock->Write (FvBlock, Lba, Offset, &Length, &State);\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-FtwGetLastRecord (\r
-  IN  EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
-  OUT EFI_FTW_LITE_RECORD  **FtwLastRecord\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Get the last Write record pointer. \r
-    The last record is the record whose 'complete' state hasn't been set.\r
-    After all, this header may be a EMPTY header entry for next Allocate. \r
-\r
-Arguments:\r
-    FtwLiteDevice   - Private data of this driver\r
-    FtwLastRecord   - Pointer to retrieve the last write record\r
-\r
-Returns:\r
-    EFI_SUCCESS     - Get the last write record successfully\r
-    EFI_ABORTED     - The FTW work space is damaged\r
-\r
---*/\r
-{\r
-  EFI_FTW_LITE_RECORD *Record;\r
-\r
-  Record = (EFI_FTW_LITE_RECORD *) (FtwLiteDevice->FtwWorkSpaceHeader + 1);\r
-  while (Record->WriteCompleted == FTW_VALID_STATE) {\r
-    //\r
-    // If Offset exceed the FTW work space boudary, return error.\r
-    //\r
-    if ((UINTN) ((UINT8 *) Record - FtwLiteDevice->FtwWorkSpace) > FtwLiteDevice->FtwWorkSpaceSize) {\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Record++;\r
-  }\r
-  //\r
-  // Last write record is found\r
-  //\r
-  *FtwLastRecord = Record;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-WorkSpaceRefresh (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Read from working block to refresh the work space in memory.\r
-\r
-Arguments:\r
-    FtwLiteDevice     - Point to private data of FTW driver\r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-    EFI_ABORTED   - The function could not complete successfully.\r
-\r
---*/\r
-{\r
-  EFI_STATUS          Status;\r
-  UINTN               Length;\r
-  UINTN               Offset;\r
-  EFI_FTW_LITE_RECORD *Record;\r
-\r
-  //\r
-  // Initialize WorkSpace as FTW_ERASED_BYTE\r
-  //\r
-  SetMem (\r
-    FtwLiteDevice->FtwWorkSpace,\r
-    FtwLiteDevice->FtwWorkSpaceSize,\r
-    FTW_ERASED_BYTE\r
-    );\r
-\r
-  //\r
-  // Read from working block\r
-  //\r
-  Length = FtwLiteDevice->FtwWorkSpaceSize;\r
-  Status = FtwLiteDevice->FtwFvBlock->Read (\r
-                                        FtwLiteDevice->FtwFvBlock,\r
-                                        FtwLiteDevice->FtwWorkSpaceLba,\r
-                                        FtwLiteDevice->FtwWorkSpaceBase,\r
-                                        &Length,\r
-                                        FtwLiteDevice->FtwWorkSpace\r
-                                        );\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // Refresh the FtwLastRecord\r
-  //\r
-  Status  = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
-\r
-  Record  = FtwLiteDevice->FtwLastRecord;\r
-  Offset  = (UINTN) (UINT8 *) Record - (UINTN) FtwLiteDevice->FtwWorkSpace;\r
-\r
-  //\r
-  // IF work space has error or Record is out of the workspace limit, THEN\r
-  //   call reclaim.\r
-  //\r
-  if (EFI_ERROR (Status) || (Offset + WRITE_TOTAL_SIZE >= FtwLiteDevice->FtwWorkSpaceSize)) {\r
-    //\r
-    // reclaim work space in working block.\r
-    //\r
-    Status = FtwReclaimWorkSpace (FtwLiteDevice);\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: Reclaim workspace - %r\n", Status));\r
-      return EFI_ABORTED;\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-CleanupWorkSpace (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice,\r
-  IN OUT UINT8            *FtwSpaceBuffer,\r
-  IN UINTN                BufferSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Reclaim the work space. Get rid of all the completed write records\r
-    and write records in the Fault Tolerant work space.\r
-\r
-Arguments:\r
-    FtwLiteDevice   - Point to private data of FTW driver\r
-    FtwSpaceBuffer  - Buffer to contain the reclaimed clean data\r
-    BufferSize      - Size of the FtwSpaceBuffer\r
-\r
-Returns:\r
-    EFI_SUCCESS           - The function completed successfully\r
-    EFI_BUFFER_TOO_SMALL  - The FtwSpaceBuffer is too small\r
-    EFI_ABORTED           - The function could not complete successfully.\r
-\r
---*/\r
-{\r
-  UINTN               Length;\r
-  EFI_FTW_LITE_RECORD *Record;\r
-\r
-  //\r
-  // To check if the buffer is large enough\r
-  //\r
-  Length = FtwLiteDevice->FtwWorkSpaceSize;\r
-  if (BufferSize < Length) {\r
-    return EFI_BUFFER_TOO_SMALL;\r
-  }\r
-  //\r
-  // Clear the content of buffer that will save the new work space data\r
-  //\r
-  SetMem (FtwSpaceBuffer, Length, FTW_ERASED_BYTE);\r
-\r
-  //\r
-  // Copy EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER to buffer\r
-  //\r
-  CopyMem (\r
-    FtwSpaceBuffer,\r
-    FtwLiteDevice->FtwWorkSpaceHeader,\r
-    sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER)\r
-    );\r
-\r
-  //\r
-  // Get the last record\r
-  //\r
-  Record = FtwLiteDevice->FtwLastRecord;\r
-  if ((Record != NULL) && (Record->WriteAllocated == FTW_VALID_STATE) && (Record->WriteCompleted != FTW_VALID_STATE)) {\r
-    CopyMem (\r
-      (UINT8 *) FtwSpaceBuffer + sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER),\r
-      Record,\r
-      WRITE_TOTAL_SIZE\r
-      );\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-FtwReclaimWorkSpace (\r
-  IN EFI_FTW_LITE_DEVICE  *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Reclaim the work space on the working block.\r
-\r
-Arguments:\r
-    FtwLiteDevice     - Point to private data of FTW driver\r
-\r
-Returns:\r
-    EFI_SUCCESS           - The function completed successfully\r
-    EFI_OUT_OF_RESOURCES  - Allocate memory error\r
-    EFI_ABORTED           - The function could not complete successfully\r
-\r
---*/\r
-{\r
-  EFI_STATUS                              Status;\r
-  UINT8                                   *TempBuffer;\r
-  UINTN                                   TempBufferSize;\r
-  UINT8                                   *Ptr;\r
-  UINTN                                   Length;\r
-  UINTN                                   Index;\r
-  UINTN                                   SpareBufferSize;\r
-  UINT8                                   *SpareBuffer;\r
-  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *WorkingBlockHeader;\r
-\r
-  DEBUG ((EFI_D_FTW_LITE, "FtwLite: start to reclaim work space\n"));\r
-\r
-  //\r
-  // Read all original data from working block to a memory buffer\r
-  //\r
-  TempBufferSize = FtwLiteDevice->SpareAreaLength;\r
-  TempBuffer     = AllocateZeroPool (TempBufferSize);\r
-  if (TempBuffer != NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Ptr = TempBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Length = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwFvBlock->Read (\r
-                                          FtwLiteDevice->FtwFvBlock,\r
-                                          FtwLiteDevice->FtwWorkBlockLba + Index,\r
-                                          0,\r
-                                          &Length,\r
-                                          Ptr\r
-                                          );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (TempBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += Length;\r
-  }\r
-  //\r
-  // Clean up the workspace, remove all the completed records.\r
-  //\r
-  Ptr = TempBuffer +\r
-    ((UINTN) (FtwLiteDevice->FtwWorkSpaceLba - FtwLiteDevice->FtwWorkBlockLba)) *\r
-    FtwLiteDevice->SizeOfSpareBlock + FtwLiteDevice->FtwWorkSpaceBase;\r
-\r
-  Status = CleanupWorkSpace (\r
-            FtwLiteDevice,\r
-            Ptr,\r
-            FtwLiteDevice->FtwWorkSpaceSize\r
-            );\r
-\r
-  CopyMem (\r
-    FtwLiteDevice->FtwWorkSpace,\r
-    Ptr,\r
-    FtwLiteDevice->FtwWorkSpaceSize\r
-    );\r
-\r
-  Status = FtwGetLastRecord (FtwLiteDevice, &FtwLiteDevice->FtwLastRecord);\r
-\r
-  //\r
-  // Set the WorkingBlockValid and WorkingBlockInvalid as INVALID\r
-  //\r
-  WorkingBlockHeader                      = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) Ptr;\r
-  WorkingBlockHeader->WorkingBlockValid   = FTW_INVALID_STATE;\r
-  WorkingBlockHeader->WorkingBlockInvalid = FTW_INVALID_STATE;\r
-\r
-  //\r
-  // Try to keep the content of spare block\r
-  // Save spare block into a spare backup memory buffer (Sparebuffer)\r
-  //\r
-  SpareBufferSize = FtwLiteDevice->SpareAreaLength;\r
-  SpareBuffer     = AllocatePool (SpareBufferSize);\r
-  if (SpareBuffer == NULL) {\r
-    FreePool (TempBuffer);\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  Ptr = SpareBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Length = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Length,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (TempBuffer);\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += Length;\r
-  }\r
-  //\r
-  // Write the memory buffer to spare block\r
-  //\r
-  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
-  Ptr     = TempBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Length = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Length,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (TempBuffer);\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += Length;\r
-  }\r
-  //\r
-  // Free TempBuffer\r
-  //\r
-  FreePool (TempBuffer);\r
-\r
-  //\r
-  // Write the spare block to working block\r
-  //\r
-  Status = FlushSpareBlockToWorkingBlock (FtwLiteDevice);\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (SpareBuffer);\r
-    return Status;\r
-  }\r
-  //\r
-  // Restore spare backup buffer into spare block , if no failure happened during FtwWrite.\r
-  //\r
-  Status  = FtwEraseSpareBlock (FtwLiteDevice);\r
-  Ptr     = SpareBuffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Length = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Length,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (SpareBuffer);\r
-      return EFI_ABORTED;\r
-    }\r
-\r
-    Ptr += Length;\r
-  }\r
-\r
-  FreePool (SpareBuffer);\r
-\r
-  DEBUG ((EFI_D_FTW_LITE, "FtwLite: reclaim work space success\n"));\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ia32/Ia32FtwMisc.c
deleted file mode 100644 (file)
index 6425f29..0000000
+++ /dev/null
@@ -1,403 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
-  Ia32FtwMisc.c\r
-  \r
-Abstract:\r
-  \r
-   Ia32 platform related code to support FtwLite..\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-\r
-#include <FtwLite.h>\r
-\r
-//\r
-// MACROs for boot block update\r
-//\r
-#define BOOT_BLOCK_BASE 0xFFFF0000\r
-\r
-//\r
-// (LPC -- D31:F0)\r
-//\r
-#define LPC_BUS_NUMBER    0x00\r
-#define LPC_DEVICE_NUMBER 0x1F\r
-#define LPC_IF            0xF0\r
-//\r
-// Top swap\r
-//\r
-#define GEN_STATUS    0xD4\r
-#define TOP_SWAP_BIT  (1 << 13)\r
-\r
-STATIC\r
-UINT32\r
-ReadPciRegister (\r
-  IN UINT32                 Offset\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Read PCI register value.\r
-\r
-Arguments:\r
-\r
-  Offset  - Offset of the register\r
-\r
-Returns:\r
-\r
-  The value.\r
-\r
---*/\r
-{\r
-  EFI_STATUS                      Status;\r
-  UINT32                          Value;\r
-  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
-\r
-  Value   = 0;\r
-  Status  = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
-    return 0;\r
-  }\r
-\r
-  Status = PciRootBridgeIo->Pci.Read (\r
-                                  PciRootBridgeIo,\r
-                                  EfiPciWidthUint32,\r
-                                  EFI_PCI_ADDRESS (\r
-                                    LPC_BUS_NUMBER,\r
-                                    LPC_DEVICE_NUMBER,\r
-                                    LPC_IF,\r
-                                    Offset\r
-                                    ),\r
-                                  1,\r
-                                  &Value\r
-                                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return Value;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-GetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  OUT BOOLEAN               *SwapState\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get swap state\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  SwapState     - Swap state\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - State successfully got\r
-\r
---*/\r
-{\r
-  //\r
-  // Top swap status is 13 bit\r
-  //\r
-  *SwapState = (BOOLEAN) ((ReadPciRegister (GEN_STATUS) & TOP_SWAP_BIT) != 0);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-SetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  IN  BOOLEAN               TopSwap\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Set swap state.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
-    TopSwap        - New swap state\r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-\r
-Note:\r
-    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
-    software will not be able to clear the Top-Swap bit until the system is\r
-    rebooted without GNT[A]# being pulled down.\r
-\r
---*/\r
-{\r
-  UINT32                          GenStatus;\r
-  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
-  EFI_STATUS                      Status;\r
-\r
-  //\r
-  // Top-Swap bit (bit 13, D31: F0, Offset D4h)\r
-  //\r
-  GenStatus = ReadPciRegister (GEN_STATUS);\r
-\r
-  //\r
-  // Set 13 bit, according to input NewSwapState\r
-  //\r
-  if (TopSwap) {\r
-    GenStatus |= TOP_SWAP_BIT;\r
-  } else {\r
-    GenStatus &= ~TOP_SWAP_BIT;\r
-  }\r
-\r
-  Status = gBS->LocateProtocol (&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID **) &PciRootBridgeIo);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "FtwLite: Locate PCI root bridge io protocol - %r", Status));\r
-    return Status;\r
-  }\r
-  //\r
-  // Write back the GenStatus register\r
-  //\r
-  Status = PciRootBridgeIo->Pci.Write (\r
-                                  PciRootBridgeIo,\r
-                                  EfiPciWidthUint32,\r
-                                  EFI_PCI_ADDRESS (\r
-                                    LPC_BUS_NUMBER,\r
-                                    LPC_DEVICE_NUMBER,\r
-                                    LPC_IF,\r
-                                    GEN_STATUS\r
-                                    ),\r
-                                  1,\r
-                                  &GenStatus\r
-                                  );\r
-\r
-  DEBUG_CODE_BEGIN ();\r
-    if (TopSwap) {\r
-      DEBUG ((EFI_D_ERROR, "SAR: Set top swap\n"));\r
-    } else {\r
-      DEBUG ((EFI_D_ERROR, "SAR: Clear top swap\n"));\r
-    }\r
-  DEBUG_CODE_END ();\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-BOOLEAN\r
-IsBootBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether the block is a boot block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - Lba value\r
-\r
-Returns:\r
-\r
-  Is a boot block or not\r
-\r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *BootFvb;\r
-\r
-  Status = GetFvbByAddress (BOOT_BLOCK_BASE, &BootFvb);\r
-  if (EFI_ERROR (Status)) {\r
-    return FALSE;\r
-  }\r
-  //\r
-  // Compare the Fvb\r
-  //\r
-  return (BOOLEAN) (FvBlock == BootFvb);\r
-}\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToBootBlock (\r
-  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to boot block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-    FTW will do extra work on boot block update.\r
-    FTW should depend on a protocol of EFI_ADDRESS_RANGE_SWAP_PROTOCOL, \r
-    which is produced by a chipset driver.\r
-\r
-    FTW updating boot block steps:\r
-    1. Erase top swap block (0xFFFE-0xFFFEFFFF) and write data to it ready\r
-    2. Read data from top swap block to memory buffer\r
-    3. SetSwapState(EFI_SWAPPED)\r
-    4. Erasing boot block (0xFFFF-0xFFFFFFFF)\r
-    5. Programming boot block until the boot block is ok.\r
-    6. SetSwapState(UNSWAPPED)\r
-\r
-    Notes:\r
-     1. Since the SwapState bit is saved in CMOS, FTW can restore and continue \r
-     even in the scenario of power failure.\r
-     2. FTW shall not allow to update boot block when battery state is error.\r
-\r
---*/\r
-{\r
-  EFI_STATUS                          Status;\r
-  UINTN                               Length;\r
-  UINT8                               *Buffer;\r
-  UINTN                               Count;\r
-  UINT8                               *Ptr;\r
-  UINTN                               Index;\r
-  BOOLEAN                             TopSwap;\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *BootFvb;\r
-  EFI_LBA                             BootLba;\r
-\r
-  //\r
-  // Allocate a memory buffer\r
-  //\r
-  Length  = FtwLiteDevice->SpareAreaLength;\r
-  Buffer  = AllocatePool (Length);\r
-  if (Buffer == NULL) {\r
-  }\r
-  //\r
-  // Get TopSwap bit state\r
-  //\r
-  Status = GetSwapState (FtwLiteDevice, &TopSwap);\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "FtwLite: Get Top Swapped status - %r\n", Status));\r
-    FreePool (Buffer);\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  if (TopSwap) {\r
-    //\r
-    // Get FVB of current boot block\r
-    //\r
-    Status = GetFvbByAddress (FtwLiteDevice->SpareAreaAddress + FTW_BLOCK_SIZE, &BootFvb);\r
-    if (EFI_ERROR (Status)) {\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-    //\r
-    // Read data from current boot block\r
-    //\r
-    BootLba = 0;\r
-    Ptr     = Buffer;\r
-    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      Count = FtwLiteDevice->SizeOfSpareBlock;\r
-      Status = BootFvb->Read (\r
-                          BootFvb,\r
-                          BootLba + Index,\r
-                          0,\r
-                          &Count,\r
-                          Ptr\r
-                          );\r
-      if (EFI_ERROR (Status)) {\r
-        FreePool (Buffer);\r
-        return Status;\r
-      }\r
-\r
-      Ptr += Count;\r
-    }\r
-\r
-  } else {\r
-    //\r
-    // Read data from spare block\r
-    //\r
-    Ptr = Buffer;\r
-    for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-      Count = FtwLiteDevice->SizeOfSpareBlock;\r
-      Status = FtwLiteDevice->FtwBackupFvb->Read (\r
-                                              FtwLiteDevice->FtwBackupFvb,\r
-                                              FtwLiteDevice->FtwSpareLba + Index,\r
-                                              0,\r
-                                              &Count,\r
-                                              Ptr\r
-                                              );\r
-      if (EFI_ERROR (Status)) {\r
-        FreePool (Buffer);\r
-        return Status;\r
-      }\r
-\r
-      Ptr += Count;\r
-    }\r
-    //\r
-    // Set TopSwap bit\r
-    //\r
-    Status = SetSwapState (FtwLiteDevice, TRUE);\r
-    DEBUG ((EFI_D_ERROR, "FtwLite: Set Swap State - %r\n", Status));\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-  //\r
-  // Erase boot block. After setting TopSwap bit, it's spare block now!\r
-  //\r
-  Status = FtwEraseSpareBlock (FtwLiteDevice);\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (Buffer);\r
-    return EFI_ABORTED;\r
-  }\r
-  //\r
-  // Write memory buffer to currenet spare block\r
-  //\r
-  Ptr = Buffer;\r
-  for (Index = 0; Index < FtwLiteDevice->NumberOfSpareBlock; Index += 1) {\r
-    Count = FtwLiteDevice->SizeOfSpareBlock;\r
-    Status = FtwLiteDevice->FtwBackupFvb->Write (\r
-                                            FtwLiteDevice->FtwBackupFvb,\r
-                                            FtwLiteDevice->FtwSpareLba + Index,\r
-                                            0,\r
-                                            &Count,\r
-                                            Ptr\r
-                                            );\r
-    if (EFI_ERROR (Status)) {\r
-      DEBUG ((EFI_D_FTW_LITE, "FtwLite: FVB Write boot block - %r\n", Status));\r
-      FreePool (Buffer);\r
-      return Status;\r
-    }\r
-\r
-    Ptr += Count;\r
-  }\r
-\r
-  FreePool (Buffer);\r
-\r
-  //\r
-  // Clear TopSwap bit\r
-  //\r
-  Status = SetSwapState (FtwLiteDevice, FALSE);\r
-  DEBUG ((EFI_D_ERROR, "FtwLite: Clear Swap State - %r\n", Status));\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ipf/IpfFtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/Ipf/IpfFtwMisc.c
deleted file mode 100644 (file)
index d31883b..0000000
+++ /dev/null
@@ -1,143 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
-  IpfFtwMisc.c\r
-  \r
-Abstract:\r
-  \r
-  Ipf platform related code to support FtwLite..\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-\r
-#include <FtwLite.h>\r
-\r
-//\r
-// MACROs for boot block update\r
-//\r
-#define BOOT_BLOCK_BASE\r
-\r
-STATIC\r
-EFI_STATUS\r
-GetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  OUT BOOLEAN               *SwapState\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get swap state\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  SwapState     - Swap state\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - State successfully got\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-SetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  IN  BOOLEAN               TopSwap\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Set swap state.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
-    TopSwap        - New swap state\r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-\r
-Note:\r
-    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
-    software will not be able to clear the Top-Swap bit until the system is\r
-    rebooted without GNT[A]# being pulled down.\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-BOOLEAN\r
-IsBootBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether the block is a boot block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - Lba value\r
-\r
-Returns:\r
-\r
-  Is a boot block or not\r
-\r
---*/\r
-{\r
-  //\r
-  // IPF doesn't support safe bootblock update\r
-  // so treat bootblock as normal block\r
-  //\r
-  return FALSE;\r
-}\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToBootBlock (\r
-  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to boot block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/x64/x64FtwMisc.c b/MdeModulePkg/Universal/FirmwareVolume/FaultTolerantWriteLite/Dxe/x64/x64FtwMisc.c
deleted file mode 100644 (file)
index d8e3a03..0000000
+++ /dev/null
@@ -1,140 +0,0 @@
-\r
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
-\r
-Module Name:\r
-\r
-  x64FtwMisc.c\r
-  \r
-Abstract:\r
-  \r
-  X64 platform related code to support FtwLite..\r
-\r
-Revision History\r
-\r
---*/\r
-\r
-\r
-#include <FtwLite.h>\r
-\r
-//\r
-// MACROs for boot block update\r
-//\r
-#define BOOT_BLOCK_BASE\r
-\r
-// STATIC\r
-EFI_STATUS\r
-GetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  OUT BOOLEAN               *SwapState\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Get swap state\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  SwapState     - Swap state\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - State successfully got\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-// STATIC\r
-EFI_STATUS\r
-SetSwapState (\r
-  IN EFI_FTW_LITE_DEVICE    *FtwLiteDevice,\r
-  IN  BOOLEAN               TopSwap\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Set swap state.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - Indicates a pointer to the calling context.  \r
-    TopSwap        - New swap state\r
-\r
-Returns:\r
-    EFI_SUCCESS   - The function completed successfully\r
-\r
-Note:\r
-    the Top-Swap bit (bit 13, D31: F0, Offset D4h). Note that\r
-    software will not be able to clear the Top-Swap bit until the system is\r
-    rebooted without GNT[A]# being pulled down.\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-BOOLEAN\r
-IsBootBlock (\r
-  EFI_FTW_LITE_DEVICE                 *FtwLiteDevice,\r
-  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvBlock,\r
-  EFI_LBA                             Lba\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Check whether the block is a boot block.\r
-\r
-Arguments:\r
-\r
-  FtwLiteDevice - Calling context\r
-  FvBlock       - Fvb protocol instance\r
-  Lba           - Lba value\r
-\r
-Returns:\r
-\r
-  Is a boot block or not\r
-\r
---*/\r
-{\r
-  return FALSE;\r
-}\r
-\r
-EFI_STATUS\r
-FlushSpareBlockToBootBlock (\r
-  EFI_FTW_LITE_DEVICE      *FtwLiteDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-    Copy the content of spare block to a boot block. Size is FTW_BLOCK_SIZE.\r
-    Spare block is accessed by FTW backup FVB protocol interface. LBA is \r
-    FtwLiteDevice->FtwSpareLba.\r
-    Boot block is accessed by BootFvb protocol interface. LBA is 0.\r
-\r
-Arguments:\r
-    FtwLiteDevice  - The private data of FTW_LITE driver\r
-\r
-Returns:\r
-    EFI_SUCCESS              - Spare block content is copied to boot block\r
-    EFI_INVALID_PARAMETER    - Input parameter error\r
-    EFI_OUT_OF_RESOURCES     - Allocate memory error\r
-    EFI_ABORTED              - The function could not complete successfully\r
-\r
-Notes:\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.c b/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.c
deleted file mode 100644 (file)
index ca5ee21..0000000
+++ /dev/null
@@ -1,265 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  Crc32SectionExtract.c\r
-\r
-Abstract:\r
-\r
-  Implements GUIDed section extraction protocol interface with \r
-  a specific GUID: CRC32.\r
-\r
-  Please refer to the Framewokr Firmware Volume Specification 0.9.\r
-\r
---*/\r
-\r
-\r
-#include <Crc32SectionExtract.h>\r
-\r
-EFI_STATUS\r
-GuidedSectionExtractionProtocolConstructor (\r
-  OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL      **GuidedSep,\r
-  IN  EFI_EXTRACT_GUIDED_SECTION                  ExtractSection\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Constructor for the GUIDed section extraction protocol.  Initializes\r
-  instance data.\r
-\r
-Arguments:\r
-\r
-  This      Instance to construct\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS:  Instance initialized.\r
-\r
---*/\r
-// TODO:    GuidedSep - add argument and description to function comment\r
-// TODO:    ExtractSection - add argument and description to function comment\r
-// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
-{\r
-  *GuidedSep = AllocatePool (sizeof (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL));\r
-  if (*GuidedSep == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
-  (*GuidedSep)->ExtractSection = ExtractSection;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeCrc32GuidedSectionExtractionProtocol (\r
-  IN EFI_HANDLE                   ImageHandle,\r
-  IN EFI_SYSTEM_TABLE             *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Entry point of the CRC32 GUIDed section extraction protocol. \r
-  Creates and initializes an instance of the GUIDed section \r
-  extraction protocol with CRC32 GUID.\r
-\r
-Arguments:  \r
-\r
-  ImageHandle   EFI_HANDLE: A handle for the image that is initializing \r
-                this driver\r
-  SystemTable   EFI_SYSTEM_TABLE: A pointer to the EFI system table        \r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS:          Driver initialized successfully\r
-  EFI_LOAD_ERROR:       Failed to Initialize or has been loaded \r
-  EFI_OUT_OF_RESOURCES: Could not allocate needed resources\r
-\r
---*/\r
-{\r
-  EFI_STATUS                              Status;\r
-  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *Crc32GuidedSep;\r
-  EFI_HANDLE                              Handle;\r
-\r
-  //\r
-  // Call all constructors per produced protocols\r
-  //\r
-  Status = GuidedSectionExtractionProtocolConstructor (\r
-            &Crc32GuidedSep,\r
-            (EFI_EXTRACT_GUIDED_SECTION) Crc32ExtractSection\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    if (Crc32GuidedSep != NULL) {\r
-      FreePool (Crc32GuidedSep);\r
-    }\r
-\r
-    return Status;\r
-  }\r
-  //\r
-  // Pass in a NULL to install to a new handle\r
-  //\r
-  Handle = NULL;\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &Handle,\r
-                  &gEfiCrc32GuidedSectionExtractionProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  Crc32GuidedSep\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    FreePool (Crc32GuidedSep);\r
-    return EFI_LOAD_ERROR;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-STATIC\r
-UINT32\r
-EFIAPI\r
-GetSectionLength (\r
-  IN EFI_COMMON_SECTION_HEADER  *CommonHeader\r
-  )\r
-/*++\r
-\r
-  Routine Description:\r
-    Get a length of section.\r
-\r
-  Parameters:\r
-    CommonHeader      -   Pointer to the common section header.\r
-\r
-  Return Value:\r
-    The length of the section, including the section header.\r
-\r
---*/\r
-// TODO: function comment is missing 'Arguments:'\r
-// TODO: function comment is missing 'Returns:'\r
-// TODO:    CommonHeader - add argument and description to function comment\r
-{\r
-  UINT32  Size;\r
-\r
-  Size = *(UINT32 *) CommonHeader->Size & 0x00FFFFFF;\r
-\r
-  return Size;\r
-}\r
-\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-Crc32ExtractSection (\r
-  IN  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *This,\r
-  IN  VOID                                    *InputSection,\r
-  OUT VOID                                    **OutputBuffer,\r
-  OUT UINTN                                   *OutputSize,\r
-  OUT UINT32                                  *AuthenticationStatus\r
-  )\r
-/*++\r
-\r
-  Routine Description:\r
-    This function reads and extracts contents of a section from an\r
-    encapsulating section.\r
-\r
-  Parameters:\r
-    This                    - Indicates the calling context.\r
-    InputSection            - Buffer containing the input GUIDed section \r
-                              to be processed.\r
-    OutputBuffer            - *OutputBuffer is allocated from boot services\r
-                              pool memory and containing the new section\r
-                              stream. The caller is responsible for freeing\r
-                              this buffer.\r
-    AuthenticationStatus    - Pointer to a caller allocated UINT32 that\r
-                              indicates the authentication status of the\r
-                              output buffer\r
-\r
-  Return Value:\r
-    EFI_SUCCESS\r
-    EFI_OUT_OF_RESOURCES\r
-    EFI_INVALID_PARAMETER\r
-    EFI_NOT_AVAILABLE_YET\r
-\r
---*/\r
-// TODO: function comment is missing 'Arguments:'\r
-// TODO: function comment is missing 'Returns:'\r
-// TODO:    This - add argument and description to function comment\r
-// TODO:    InputSection - add argument and description to function comment\r
-// TODO:    OutputBuffer - add argument and description to function comment\r
-// TODO:    OutputSize - add argument and description to function comment\r
-// TODO:    AuthenticationStatus - add argument and description to function comment\r
-// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
-// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
-// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-{\r
-  EFI_STATUS                Status;\r
-  CRC32_SECTION_HEADER      *Crc32SectionHeader;\r
-  EFI_GUID_DEFINED_SECTION  *GuidedSectionHeader;\r
-  UINT8                     *Image;\r
-  UINT32                    Crc32Checksum;\r
-  VOID                      *DummyInterface;\r
-\r
-  if (OutputBuffer == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  *OutputBuffer = NULL;\r
-\r
-  //\r
-  // Points to the section header\r
-  //\r
-  Crc32SectionHeader  = (CRC32_SECTION_HEADER *) InputSection;\r
-  GuidedSectionHeader = (EFI_GUID_DEFINED_SECTION *) InputSection;\r
-\r
-  //\r
-  // Check if the GUID is a CRC32 section GUID\r
-  //\r
-  if (!CompareGuid (\r
-        &(GuidedSectionHeader->SectionDefinitionGuid),\r
-        &gEfiCrc32GuidedSectionExtractionProtocolGuid\r
-        )) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Image = (UINT8 *) InputSection + (UINT32) (GuidedSectionHeader->DataOffset);\r
-  *OutputSize = GetSectionLength ((EFI_COMMON_SECTION_HEADER *) InputSection) - (UINT32) GuidedSectionHeader->DataOffset;\r
-\r
-  *OutputBuffer = AllocatePool (*OutputSize);\r
-  if (*OutputBuffer == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Implictly CRC32 GUIDed section should have STATUS_VALID bit set\r
-  //\r
-  ASSERT (GuidedSectionHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
-  *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
-\r
-  //\r
-  // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
-  //\r
-  Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
-  if (!EFI_ERROR (Status)) {\r
-    *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
-  } else {\r
-    //\r
-    // Calculate CRC32 Checksum of Image\r
-    //\r
-    gBS->CalculateCrc32 (Image, *OutputSize, &Crc32Checksum);\r
-    if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {\r
-      *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
-    }\r
-  }\r
-\r
-  CopyMem (*OutputBuffer, Image, *OutputSize);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.dxs b/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.dxs
deleted file mode 100644 (file)
index d42975a..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  Crc32SectionExtraction.dxs\r
-\r
-Abstract:\r
-\r
-  Dependency expression file.\r
-  \r
---*/  \r
-#include "DxeDepex.h"\r
-\r
-DEPENDENCY_START\r
-  EFI_RUNTIME_ARCH_PROTOCOL_GUID\r
-DEPENDENCY_END\r
-\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.h b/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.h
deleted file mode 100644 (file)
index e7bf276..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  Crc32SectionExtract.h\r
-  \r
-Abstract:\r
-\r
-  Header file for Crc32SectionExtract.c\r
-  Please refer to the Framewokr Firmware Volume Specification 0.9.\r
-\r
---*/\r
-\r
-#ifndef _CRC32_GUIDED_SECTION_EXTRACTION_H\r
-#define _CRC32_GUIDED_SECTION_EXTRACTION_H\r
-\r
-#include <PiDxe.h>\r
-#include <Protocol/SecurityPolicy.h>\r
-#include <Protocol/Crc32GuidedSectionExtraction.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-\r
-\r
-\r
-typedef struct {\r
-  EFI_GUID_DEFINED_SECTION  GuidedSectionHeader;\r
-  UINT32                    CRC32Checksum;\r
-} CRC32_SECTION_HEADER;\r
-\r
-//\r
-// Function prototype declarations\r
-//\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-Crc32ExtractSection (\r
-  IN  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *This,\r
-  IN  VOID                                    *InputSection,\r
-  OUT VOID                                    **OutputBuffer,\r
-  OUT UINTN                                   *OutputSize,\r
-  OUT UINT32                                  *AuthenticationStatus\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  TODO: Add function description\r
-\r
-Arguments:\r
-\r
-  This                  - TODO: add argument description\r
-  InputSection          - TODO: add argument description\r
-  OutputBuffer          - TODO: add argument description\r
-  OutputSize            - TODO: add argument description\r
-  AuthenticationStatus  - TODO: add argument description\r
-\r
-Returns:\r
-\r
-  TODO: add return values\r
-\r
---*/\r
-;\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeCrc32GuidedSectionExtractionProtocol (\r
-  IN EFI_HANDLE                   ImageHandle,\r
-  IN EFI_SYSTEM_TABLE             *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Entry point of the CRC32 GUIDed section extraction protocol. \r
-  Creates and initializes an instance of the GUIDed section \r
-  extraction protocol with CRC32 GUID.\r
-\r
-Arguments:  \r
-\r
-  ImageHandle   EFI_HANDLE: A handle for the image that is initializing \r
-                this driver\r
-  SystemTable   EFI_SYSTEM_TABLE: A pointer to the EFI system table        \r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS:          Driver initialized successfully\r
-  EFI_LOAD_ERROR:       Failed to Initialize or has been loaded \r
-  EFI_OUT_OF_RESOURCES: Could not allocate needed resources\r
-\r
---*/\r
-;\r
-\r
-#endif\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.inf b/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.inf
deleted file mode 100644 (file)
index 5b0c65d..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-#/** @file\r
-# Component description file for Crc32SectionExtract module.\r
-#\r
-# This driver implements CRC32 GUIDed section extraction protocol interface.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\r
-#  All rights reserved. This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution. The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-#\r
-#**/\r
-\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = Crc32SectionExtract\r
-  FILE_GUID                      = 51C9F40C-5243-4473-B265-B3C8FFAFF9FA\r
-  MODULE_TYPE                    = DXE_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  EDK_RELEASE_VERSION            = 0x00020000\r
-  EFI_SPECIFICATION_VERSION      = 0x00020000\r
-\r
-  ENTRY_POINT                    = InitializeCrc32GuidedSectionExtractionProtocol\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-[Sources.common]\r
-  Crc32SectionExtract.h\r
-  Crc32SectionExtract.c\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-\r
-[LibraryClasses]\r
-  MemoryAllocationLib\r
-  UefiBootServicesTableLib\r
-  BaseMemoryLib\r
-  UefiDriverEntryPoint\r
-  DebugLib\r
-\r
-[Protocols]\r
-  gEfiSecurityPolicyProtocolGuid                # PROTOCOL SOMETIMES_CONSUMED\r
-  gEfiCrc32GuidedSectionExtractionProtocolGuid  # PROTOCOL ALWAYS_PRODUCED\r
-\r
-[Depex]\r
-  gEfiRuntimeArchProtocolGuid\r
diff --git a/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.msa b/MdeModulePkg/Universal/FirmwareVolume/GuidedSectionExtraction/Crc32SectionExtract/Dxe/Crc32SectionExtract.msa
deleted file mode 100644 (file)
index a4ab182..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
-  <MsaHeader>\r
-    <ModuleName>Crc32SectionExtract</ModuleName>\r
-    <ModuleType>DXE_DRIVER</ModuleType>\r
-    <GuidValue>51C9F40C-5243-4473-B265-B3C8FFAFF9FA</GuidValue>\r
-    <Version>1.0</Version>\r
-    <Abstract>Component description file for Crc32SectionExtract module.</Abstract>\r
-    <Description>This driver implements CRC32 GUIDed section extraction protocol interface.</Description>\r
-    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
-    <License>All rights reserved. This program and the accompanying materials\r
-      are licensed and made available under the terms and conditions of the BSD License\r
-      which accompanies this distribution.  The full text of the license may be found at\r
-      http://opensource.org/licenses/bsd-license.php\r
-      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.</License>\r
-    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
-  </MsaHeader>\r
-  <ModuleDefinitions>\r
-    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
-    <BinaryModule>false</BinaryModule>\r
-    <OutputFileBasename>Crc32SectionExtract</OutputFileBasename>\r
-  </ModuleDefinitions>\r
-  <LibraryClassDefinitions>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>DebugLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiDriverEntryPoint</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>BaseMemoryLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiBootServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>MemoryAllocationLib</Keyword>\r
-    </LibraryClass>\r
-  </LibraryClassDefinitions>\r
-  <SourceFiles>\r
-    <Filename>Crc32SectionExtract.c</Filename>\r
-    <Filename>Crc32SectionExtract.h</Filename>\r
-    <Filename>GuidedSection.c</Filename>\r
-    <Filename>GuidedSection.h</Filename>\r
-    <Filename>Crc32SectionExtract.dxs</Filename>\r
-  </SourceFiles>\r
-  <PackageDependencies>\r
-    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
-  </PackageDependencies>\r
-  <Protocols>\r
-    <Protocol Usage="SOMETIMES_CONSUMED">\r
-      <ProtocolCName>gEfiSecurityPolicyProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-    <Protocol Usage="ALWAYS_PRODUCED">\r
-      <ProtocolCName>gEfiCrc32GuidedSectionExtractionProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-  </Protocols>\r
-  <Externs>\r
-    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
-    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
-    <Extern>\r
-      <ModuleEntryPoint>InitializeCrc32GuidedSectionExtractionProtocol</ModuleEntryPoint>\r
-    </Extern>\r
-  </Externs>\r
-</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.c b/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.c
deleted file mode 100644 (file)
index 7a20a41..0000000
+++ /dev/null
@@ -1,214 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  NullMemoryTest.c\r
-  \r
-Abstract:\r
-\r
---*/\r
-\r
-\r
-#include "NullMemoryTest.h"\r
-\r
-//\r
-// Module global members\r
-//\r
-UINT64                            mTestedSystemMemory = 0;\r
-UINT64                            mTotalSystemMemory  = 0;\r
-EFI_HANDLE                        mGenericMemoryTestHandle;\r
-\r
-//\r
-// Driver entry here\r
-//\r
-EFI_GENERIC_MEMORY_TEST_PROTOCOL  mGenericMemoryTest = {\r
-  InitializeMemoryTest,\r
-  GenPerformMemoryTest,\r
-  GenMemoryTestFinished,\r
-  GenCompatibleRangeTest\r
-};\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenericMemoryTestEntryPoint (\r
-  IN  EFI_HANDLE           ImageHandle,\r
-  IN  EFI_SYSTEM_TABLE     *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The generic memory test driver's entry point, it can initialize private data\r
-  to default value\r
-\r
-Arguments:\r
-\r
-  ImageHandle of the loaded driver\r
-  Pointer to the System Table\r
-\r
-Returns:\r
-\r
-  Status\r
-\r
-  EFI_SUCCESS           - Protocol successfully installed\r
-  EFI_OUT_OF_RESOURCES  - Can not allocate protocol data structure in base\r
-                          memory\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  //\r
-  // Install the protocol\r
-  //\r
-  Status = gBS->InstallProtocolInterface (\r
-                  &mGenericMemoryTestHandle,\r
-                  &gEfiGenericMemTestProtocolGuid,\r
-                  EFI_NATIVE_INTERFACE,\r
-                  &mGenericMemoryTest\r
-                  );\r
-\r
-  return Status;\r
-}\r
-//\r
-// EFI_GENERIC_MEMORY_TEST_PROTOCOL implementation\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeMemoryTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN  EXTENDMEM_COVERAGE_LEVEL                 Level,\r
-  OUT BOOLEAN                                  *RequireSoftECCInit\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  UINTN                           NumberOfDescriptors;\r
-  EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
-  UINTN                           Index;\r
-\r
-  gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
-  for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
-    if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
-        (MemorySpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
-          (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r
-          ) {\r
-      gDS->RemoveMemorySpace (\r
-            MemorySpaceMap[Index].BaseAddress,\r
-            MemorySpaceMap[Index].Length\r
-            );\r
-\r
-      gDS->AddMemorySpace (\r
-            EfiGcdMemoryTypeSystemMemory,\r
-            MemorySpaceMap[Index].BaseAddress,\r
-            MemorySpaceMap[Index].Length,\r
-            MemorySpaceMap[Index].Capabilities &~\r
-            (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
-            );\r
-\r
-      mTestedSystemMemory += MemorySpaceMap[Index].Length;\r
-      mTotalSystemMemory += MemorySpaceMap[Index].Length;\r
-    } else if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
-      mTotalSystemMemory += MemorySpaceMap[Index].Length;\r
-    }\r
-  }\r
-\r
-  FreePool (MemorySpaceMap);\r
-\r
-  *RequireSoftECCInit = FALSE;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenPerformMemoryTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN OUT UINT64                                *TestedMemorySize,\r
-  OUT UINT64                                   *TotalMemorySize,\r
-  OUT BOOLEAN                                  *ErrorOut,\r
-  IN BOOLEAN                                   TestAbort\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  *ErrorOut         = FALSE;\r
-  *TestedMemorySize = mTestedSystemMemory;\r
-  *TotalMemorySize  = mTotalSystemMemory;\r
-\r
-  return EFI_NOT_FOUND;\r
-\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenMemoryTestFinished (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenCompatibleRangeTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN  EFI_PHYSICAL_ADDRESS                     StartAddress,\r
-  IN  UINT64                                   Length\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_GCD_MEMORY_SPACE_DESCRIPTOR descriptor;\r
-\r
-  gDS->GetMemorySpaceDescriptor (StartAddress, &descriptor);\r
-\r
-  gDS->RemoveMemorySpace (StartAddress, Length);\r
-\r
-  gDS->AddMemorySpace (\r
-        EfiGcdMemoryTypeSystemMemory,\r
-        StartAddress,\r
-        Length,\r
-        descriptor.Capabilities &~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
-        );\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.dxs b/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.dxs
deleted file mode 100644 (file)
index a1ac031..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  NullMemoryTest.dxs\r
-\r
-Abstract:\r
-\r
-  Dependency expression source file.\r
-  \r
---*/  \r
-#include <DxeDepex.h>\r
-\r
-DEPENDENCY_START\r
-  TRUE\r
-DEPENDENCY_END\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.h b/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.h
deleted file mode 100644 (file)
index 4b04381..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-  \r
-    NullMemoryTest.h\r
-  \r
-Abstract:\r
-  The generic memory test driver definition\r
-\r
---*/\r
-\r
-#ifndef _NULL_MEMORY_TEST_H\r
-#define _NULL_MEMORY_TEST_H\r
-\r
-//\r
-// The package level header files this module uses\r
-//\r
-#include <PiDxe.h>\r
-\r
-//\r
-// The protocols, PPI and GUID defintions for this module\r
-//\r
-#include <Protocol/GenericMemoryTest.h>\r
-//\r
-// The Library classes this module consumes\r
-//\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/DxeServicesTableLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-\r
-//\r
-// attributes for reserved memory before it is promoted to system memory\r
-//\r
-#define EFI_MEMORY_PRESENT      0x0100000000000000ULL\r
-#define EFI_MEMORY_INITIALIZED  0x0200000000000000ULL\r
-#define EFI_MEMORY_TESTED       0x0400000000000000ULL\r
-\r
-\r
-//\r
-// Some global define\r
-//\r
-#define GENERIC_CACHELINE_SIZE  0x40\r
-\r
-//\r
-// The SPARSE_SPAN_SIZE size can not small then the MonoTestSize\r
-//\r
-#define TEST_BLOCK_SIZE   0x2000000\r
-#define QUICK_SPAN_SIZE   (TEST_BLOCK_SIZE >> 2)\r
-#define SPARSE_SPAN_SIZE  (TEST_BLOCK_SIZE >> 4)\r
-\r
-//\r
-// This structure records every nontested memory range parsed through GCD\r
-// service.\r
-//\r
-#define EFI_NONTESTED_MEMORY_RANGE_SIGNATURE  EFI_SIGNATURE_32 ('N', 'T', 'M', 'E')\r
-typedef struct {\r
-  UINTN                 Signature;\r
-  LIST_ENTRY            Link;\r
-  EFI_PHYSICAL_ADDRESS  StartAddress;\r
-  UINT64                Length;\r
-  UINT64                Capabilities;\r
-  BOOLEAN               Above4G;\r
-  BOOLEAN               AlreadyMapped;\r
-} NONTESTED_MEMORY_RANGE;\r
-\r
-#define NONTESTED_MEMORY_RANGE_FROM_LINK(link) \\r
-        CR(link, NONTESTED_MEMORY_RANGE, Link, EFI_NONTESTED_MEMORY_RANGE_SIGNATURE)\r
-\r
-//\r
-// This is the memory test driver's structure definition\r
-//\r
-#define EFI_GENERIC_MEMORY_TEST_PRIVATE_SIGNATURE EFI_SIGNATURE_32 ('G', 'E', 'M', 'T')\r
-\r
-//\r
-// Function Prototypes\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-InitializeMemoryTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN  EXTENDMEM_COVERAGE_LEVEL                 Level,\r
-  OUT BOOLEAN                                  *RequireSoftECCInit\r
-  )\r
-;\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenPerformMemoryTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN OUT UINT64                                *TestedMemorySize,\r
-  OUT UINT64                                   *TotalMemorySize,\r
-  OUT BOOLEAN                                  *ErrorOut,\r
-  IN BOOLEAN                                   TestAbort\r
-  )\r
-;\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenMemoryTestFinished (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This\r
-  )\r
-;\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-GenCompatibleRangeTest (\r
-  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
-  IN  EFI_PHYSICAL_ADDRESS                     StartAddress,\r
-  IN  UINT64                                   Length\r
-  )\r
-;\r
-\r
-#endif\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.inf b/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.inf
deleted file mode 100644 (file)
index 3abc0fa..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-#/** @file\r
-# Component description file for NullMemoryTest module.\r
-#\r
-# This driver installs EFI_GENERIC_MEMORY_TEST_PROTOCOL to \r
-#  provide simple generic memory test functions.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\r
-#  All rights reserved. This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution. The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-#\r
-#**/\r
-\r
-################################################################################\r
-#\r
-# Defines Section - statements that will be processed to create a Makefile.\r
-#\r
-################################################################################\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = NullMemoryTest\r
-  FILE_GUID                      = 96B5C032-DF4C-4b6e-8232-438DCF448D0E\r
-  MODULE_TYPE                    = DXE_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  EDK_RELEASE_VERSION            = 0x00020000\r
-  EFI_SPECIFICATION_VERSION      = 0x00020000\r
-\r
-  ENTRY_POINT                    = GenericMemoryTestEntryPoint\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-################################################################################\r
-#\r
-# Sources Section - list of files that are required for the build to succeed.\r
-#\r
-################################################################################\r
-\r
-[Sources.common]\r
-  NullMemoryTest.h\r
-  NullMemoryTest.c\r
-\r
-\r
-################################################################################\r
-#\r
-# Package Dependency Section - list of Package files that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Packages]\r
-  MdeModulePkg/MdeModulePkg.dec\r
-  MdePkg/MdePkg.dec\r
-\r
-\r
-################################################################################\r
-#\r
-# Library Class Section - list of Library Classes that are required for\r
-#                         this module.\r
-#\r
-################################################################################\r
-\r
-[LibraryClasses]\r
-  MemoryAllocationLib\r
-  UefiBootServicesTableLib\r
-  DxeServicesTableLib\r
-  UefiDriverEntryPoint\r
-  DebugLib\r
-\r
-\r
-################################################################################\r
-#\r
-# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
-#                           that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Protocols]\r
-  gEfiGenericMemTestProtocolGuid                # PROTOCOL ALWAYS_PRODUCED\r
-\r
-################################################################################\r
-#\r
-# Dependency Expression Section - list of Dependency expressions that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Depex]\r
-  TRUE
\ No newline at end of file
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.msa b/MdeModulePkg/Universal/GenericMemoryTest/Dxe/NullMemoryTest.msa
deleted file mode 100644 (file)
index ade8ce1..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
-  <MsaHeader>\r
-    <ModuleName>NullMemoryTest</ModuleName>\r
-    <ModuleType>DXE_DRIVER</ModuleType>\r
-    <GuidValue>96B5C032-DF4C-4b6e-8232-438DCF448D0E</GuidValue>\r
-    <Version>1.0</Version>\r
-    <Abstract>Component description file for NullMemoryTest module.</Abstract>\r
-    <Description>This driver installs EFI_GENERIC_MEMORY_TEST_PROTOCOL to \r
-      provide simple generic memory test functions.</Description>\r
-    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
-    <License>All rights reserved. This program and the accompanying materials\r
-      are licensed and made available under the terms and conditions of the BSD License\r
-      which accompanies this distribution.  The full text of the license may be found at\r
-      http://opensource.org/licenses/bsd-license.php\r
-      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.</License>\r
-    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
-  </MsaHeader>\r
-  <ModuleDefinitions>\r
-    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
-    <BinaryModule>false</BinaryModule>\r
-    <OutputFileBasename>NullMemoryTest</OutputFileBasename>\r
-  </ModuleDefinitions>\r
-  <LibraryClassDefinitions>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>DebugLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiDriverEntryPoint</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>DxeServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiBootServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>MemoryAllocationLib</Keyword>\r
-    </LibraryClass>\r
-  </LibraryClassDefinitions>\r
-  <SourceFiles>\r
-    <Filename>Common.h</Filename>\r
-    <Filename>NullMemoryTest.c</Filename>\r
-    <Filename>NullMemoryTest.h</Filename>\r
-    <Filename>NullMemoryTest.dxs</Filename>\r
-  </SourceFiles>\r
-  <PackageDependencies>\r
-    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
-    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
-  </PackageDependencies>\r
-  <Protocols>\r
-    <Protocol Usage="ALWAYS_PRODUCED">\r
-      <ProtocolCName>gEfiGenericMemTestProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-  </Protocols>\r
-  <Externs>\r
-    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
-    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
-    <Extern>\r
-      <ModuleEntryPoint>GenericMemoryTestEntryPoint</ModuleEntryPoint>\r
-    </Extern>\r
-  </Externs>\r
-</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.c b/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.c
new file mode 100644 (file)
index 0000000..7a20a41
--- /dev/null
@@ -0,0 +1,214 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  NullMemoryTest.c\r
+  \r
+Abstract:\r
+\r
+--*/\r
+\r
+\r
+#include "NullMemoryTest.h"\r
+\r
+//\r
+// Module global members\r
+//\r
+UINT64                            mTestedSystemMemory = 0;\r
+UINT64                            mTotalSystemMemory  = 0;\r
+EFI_HANDLE                        mGenericMemoryTestHandle;\r
+\r
+//\r
+// Driver entry here\r
+//\r
+EFI_GENERIC_MEMORY_TEST_PROTOCOL  mGenericMemoryTest = {\r
+  InitializeMemoryTest,\r
+  GenPerformMemoryTest,\r
+  GenMemoryTestFinished,\r
+  GenCompatibleRangeTest\r
+};\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenericMemoryTestEntryPoint (\r
+  IN  EFI_HANDLE           ImageHandle,\r
+  IN  EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  The generic memory test driver's entry point, it can initialize private data\r
+  to default value\r
+\r
+Arguments:\r
+\r
+  ImageHandle of the loaded driver\r
+  Pointer to the System Table\r
+\r
+Returns:\r
+\r
+  Status\r
+\r
+  EFI_SUCCESS           - Protocol successfully installed\r
+  EFI_OUT_OF_RESOURCES  - Can not allocate protocol data structure in base\r
+                          memory\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Install the protocol\r
+  //\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &mGenericMemoryTestHandle,\r
+                  &gEfiGenericMemTestProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  &mGenericMemoryTest\r
+                  );\r
+\r
+  return Status;\r
+}\r
+//\r
+// EFI_GENERIC_MEMORY_TEST_PROTOCOL implementation\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeMemoryTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN  EXTENDMEM_COVERAGE_LEVEL                 Level,\r
+  OUT BOOLEAN                                  *RequireSoftECCInit\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  UINTN                           NumberOfDescriptors;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
+  UINTN                           Index;\r
+\r
+  gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
+  for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
+    if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
+        (MemorySpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
+          (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r
+          ) {\r
+      gDS->RemoveMemorySpace (\r
+            MemorySpaceMap[Index].BaseAddress,\r
+            MemorySpaceMap[Index].Length\r
+            );\r
+\r
+      gDS->AddMemorySpace (\r
+            EfiGcdMemoryTypeSystemMemory,\r
+            MemorySpaceMap[Index].BaseAddress,\r
+            MemorySpaceMap[Index].Length,\r
+            MemorySpaceMap[Index].Capabilities &~\r
+            (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
+            );\r
+\r
+      mTestedSystemMemory += MemorySpaceMap[Index].Length;\r
+      mTotalSystemMemory += MemorySpaceMap[Index].Length;\r
+    } else if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+      mTotalSystemMemory += MemorySpaceMap[Index].Length;\r
+    }\r
+  }\r
+\r
+  FreePool (MemorySpaceMap);\r
+\r
+  *RequireSoftECCInit = FALSE;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenPerformMemoryTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN OUT UINT64                                *TestedMemorySize,\r
+  OUT UINT64                                   *TotalMemorySize,\r
+  OUT BOOLEAN                                  *ErrorOut,\r
+  IN BOOLEAN                                   TestAbort\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  *ErrorOut         = FALSE;\r
+  *TestedMemorySize = mTestedSystemMemory;\r
+  *TotalMemorySize  = mTotalSystemMemory;\r
+\r
+  return EFI_NOT_FOUND;\r
+\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenMemoryTestFinished (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenCompatibleRangeTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN  EFI_PHYSICAL_ADDRESS                     StartAddress,\r
+  IN  UINT64                                   Length\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR descriptor;\r
+\r
+  gDS->GetMemorySpaceDescriptor (StartAddress, &descriptor);\r
+\r
+  gDS->RemoveMemorySpace (StartAddress, Length);\r
+\r
+  gDS->AddMemorySpace (\r
+        EfiGcdMemoryTypeSystemMemory,\r
+        StartAddress,\r
+        Length,\r
+        descriptor.Capabilities &~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
+        );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.h b/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTest.h
new file mode 100644 (file)
index 0000000..4b04381
--- /dev/null
@@ -0,0 +1,124 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+  \r
+    NullMemoryTest.h\r
+  \r
+Abstract:\r
+  The generic memory test driver definition\r
+\r
+--*/\r
+\r
+#ifndef _NULL_MEMORY_TEST_H\r
+#define _NULL_MEMORY_TEST_H\r
+\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <PiDxe.h>\r
+\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Protocol/GenericMemoryTest.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+//\r
+// attributes for reserved memory before it is promoted to system memory\r
+//\r
+#define EFI_MEMORY_PRESENT      0x0100000000000000ULL\r
+#define EFI_MEMORY_INITIALIZED  0x0200000000000000ULL\r
+#define EFI_MEMORY_TESTED       0x0400000000000000ULL\r
+\r
+\r
+//\r
+// Some global define\r
+//\r
+#define GENERIC_CACHELINE_SIZE  0x40\r
+\r
+//\r
+// The SPARSE_SPAN_SIZE size can not small then the MonoTestSize\r
+//\r
+#define TEST_BLOCK_SIZE   0x2000000\r
+#define QUICK_SPAN_SIZE   (TEST_BLOCK_SIZE >> 2)\r
+#define SPARSE_SPAN_SIZE  (TEST_BLOCK_SIZE >> 4)\r
+\r
+//\r
+// This structure records every nontested memory range parsed through GCD\r
+// service.\r
+//\r
+#define EFI_NONTESTED_MEMORY_RANGE_SIGNATURE  EFI_SIGNATURE_32 ('N', 'T', 'M', 'E')\r
+typedef struct {\r
+  UINTN                 Signature;\r
+  LIST_ENTRY            Link;\r
+  EFI_PHYSICAL_ADDRESS  StartAddress;\r
+  UINT64                Length;\r
+  UINT64                Capabilities;\r
+  BOOLEAN               Above4G;\r
+  BOOLEAN               AlreadyMapped;\r
+} NONTESTED_MEMORY_RANGE;\r
+\r
+#define NONTESTED_MEMORY_RANGE_FROM_LINK(link) \\r
+        CR(link, NONTESTED_MEMORY_RANGE, Link, EFI_NONTESTED_MEMORY_RANGE_SIGNATURE)\r
+\r
+//\r
+// This is the memory test driver's structure definition\r
+//\r
+#define EFI_GENERIC_MEMORY_TEST_PRIVATE_SIGNATURE EFI_SIGNATURE_32 ('G', 'E', 'M', 'T')\r
+\r
+//\r
+// Function Prototypes\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeMemoryTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN  EXTENDMEM_COVERAGE_LEVEL                 Level,\r
+  OUT BOOLEAN                                  *RequireSoftECCInit\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenPerformMemoryTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN OUT UINT64                                *TestedMemorySize,\r
+  OUT UINT64                                   *TotalMemorySize,\r
+  OUT BOOLEAN                                  *ErrorOut,\r
+  IN BOOLEAN                                   TestAbort\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenMemoryTestFinished (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This\r
+  )\r
+;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GenCompatibleRangeTest (\r
+  IN EFI_GENERIC_MEMORY_TEST_PROTOCOL          *This,\r
+  IN  EFI_PHYSICAL_ADDRESS                     StartAddress,\r
+  IN  UINT64                                   Length\r
+  )\r
+;\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf b/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.inf
new file mode 100644 (file)
index 0000000..954ca64
--- /dev/null
@@ -0,0 +1,96 @@
+#/** @file\r
+# Component description file for NullMemoryTest module.\r
+#\r
+# This driver installs EFI_GENERIC_MEMORY_TEST_PROTOCOL to \r
+#  provide simple generic memory test functions.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+################################################################################\r
+#\r
+# Defines Section - statements that will be processed to create a Makefile.\r
+#\r
+################################################################################\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = NullMemoryTestDxe\r
+  FILE_GUID                      = 96B5C032-DF4C-4b6e-8232-438DCF448D0E\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = GenericMemoryTestEntryPoint\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#\r
+\r
+################################################################################\r
+#\r
+# Sources Section - list of files that are required for the build to succeed.\r
+#\r
+################################################################################\r
+\r
+[Sources.common]\r
+  NullMemoryTest.h\r
+  NullMemoryTest.c\r
+\r
+\r
+################################################################################\r
+#\r
+# Package Dependency Section - list of Package files that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Packages]\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+\r
+\r
+################################################################################\r
+#\r
+# Library Class Section - list of Library Classes that are required for\r
+#                         this module.\r
+#\r
+################################################################################\r
+\r
+[LibraryClasses]\r
+  MemoryAllocationLib\r
+  UefiBootServicesTableLib\r
+  DxeServicesTableLib\r
+  UefiDriverEntryPoint\r
+  DebugLib\r
+\r
+\r
+################################################################################\r
+#\r
+# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
+#                           that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Protocols]\r
+  gEfiGenericMemTestProtocolGuid                # PROTOCOL ALWAYS_PRODUCED\r
+\r
+################################################################################\r
+#\r
+# Dependency Expression Section - list of Dependency expressions that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Depex]\r
+  TRUE
\ No newline at end of file
diff --git a/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.msa b/MdeModulePkg/Universal/GenericMemoryTest/NullMemoryTestDxe/NullMemoryTestDxe.msa
new file mode 100644 (file)
index 0000000..ade8ce1
--- /dev/null
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
+  <MsaHeader>\r
+    <ModuleName>NullMemoryTest</ModuleName>\r
+    <ModuleType>DXE_DRIVER</ModuleType>\r
+    <GuidValue>96B5C032-DF4C-4b6e-8232-438DCF448D0E</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for NullMemoryTest module.</Abstract>\r
+    <Description>This driver installs EFI_GENERIC_MEMORY_TEST_PROTOCOL to \r
+      provide simple generic memory test functions.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>NullMemoryTest</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DebugLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>DxeServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>MemoryAllocationLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>Common.h</Filename>\r
+    <Filename>NullMemoryTest.c</Filename>\r
+    <Filename>NullMemoryTest.h</Filename>\r
+    <Filename>NullMemoryTest.dxs</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="1E73767F-8F52-4603-AEB4-F29B510B6766"/>\r
+    <Package PackageGuid="BA0D78D6-2CAF-414b-BD4D-B6762A894288"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiGenericMemTestProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>GenericMemoryTestEntryPoint</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.c b/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.c
deleted file mode 100644 (file)
index 31efab3..0000000
+++ /dev/null
@@ -1,299 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
-  MonotonicCounter.c\r
-\r
-Abstract:\r
-\r
-  Produced the Monotonic Counter Services as defined in the DXE CIS\r
-\r
-Revision History:\r
-\r
---*/\r
-\r
-#include "MonotonicCounter.h"\r
-\r
-//\r
-// The Monotonic Counter Handle\r
-//\r
-EFI_HANDLE  mMonotonicCounterHandle = NULL;\r
-\r
-//\r
-// The current Monotonic count value\r
-//\r
-UINT64      mEfiMtc;\r
-\r
-//\r
-// Event to use to update the Mtc's high part when wrapping\r
-//\r
-EFI_EVENT   mEfiMtcEvent;\r
-\r
-//\r
-// EfiMtcName - Variable name of the MTC value\r
-//\r
-CHAR16      *mEfiMtcName = (CHAR16 *) L"MTC";\r
-\r
-//\r
-// EfiMtcGuid - Guid of the MTC value\r
-//\r
-EFI_GUID    mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r
-\r
-//\r
-// Worker functions\r
-//\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-MonotonicCounterDriverGetNextMonotonicCount (\r
-  OUT UINT64  *Count\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_TPL OldTpl;\r
-\r
-  //\r
-  // Can not be called after ExitBootServices()\r
-  //\r
-  if (EfiAtRuntime ()) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-  //\r
-  // Check input parameters\r
-  //\r
-  if (Count == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  //\r
-  // Update the monotonic counter with a lock\r
-  //\r
-  OldTpl  = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
-  *Count  = mEfiMtc;\r
-  mEfiMtc++;\r
-  gBS->RestoreTPL (OldTpl);\r
-\r
-  //\r
-  // If the MSB bit of the low part toggled, then signal that the high\r
-  // part needs updated now\r
-  //\r
-  if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r
-    gBS->SignalEvent (mEfiMtcEvent);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-/**\r
-  Returns the next high 32 bits of the platform's monotonic counter.\r
-\r
-  The GetNextHighMonotonicCount() function returns the next high 32 bits\r
-  of the platform's monotonic counter. The platform's monotonic counter is\r
-  comprised of two 32 bit quantities:  the high 32 bits and the low 32 bits.\r
-  During boot service time the low 32 bit value is volatile:  it is reset to\r
-  zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().\r
-  The high 32 bit value is non-volatile and is increased by 1 whenever the system resets\r
-  or whenever the low 32 bit count [returned by GetNextMonoticCount()] overflows.\r
-  The GetNextMonotonicCount() function is only available at boot services time.\r
-  If the operating system wishes to extend the platform monotonic counter to runtime,\r
-  it may do so by utilizing GetNextHighMonotonicCount().  To do this, before calling\r
-  ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain\r
-  the current platform monotonic count.  The operating system would then provide an\r
-  interface that returns the next count by:\r
-    Adding 1 to the last count.\r
-    Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().\r
-    This will increase the high 32 bits of the platform's non-volatile portion of the monotonic\r
-    count by 1.\r
-\r
-  This function may only be called at Runtime.\r
-\r
-  @param[out]   HighCount      Pointer to returned value.\r
-\r
-  @retval EFI_INVALID_PARAMETER If HighCount is NULL.\r
-  @retval EFI_SUCCESS           Operation is successful.\r
-  @retval EFI_OUT_OF_RESOURCES  If variable service reports that not enough storage\r
-                                is available to hold the variable and its data.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be saved due to a hardware failure.\r
-\r
-**/\r
-STATIC\r
-EFI_STATUS\r
-EFIAPI\r
-MonotonicCounterDriverGetNextHighMonotonicCount (\r
-  OUT UINT32  *HighCount\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_TPL     OldTpl;\r
-\r
-  //\r
-  // Check input parameters\r
-  //\r
-  if (HighCount == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  if (!EfiAtRuntime ()) {\r
-    //\r
-    // Use a lock if called before ExitBootServices()\r
-    //\r
-    OldTpl      = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
-    *HighCount  = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
-    mEfiMtc     = LShiftU64 (*HighCount, 32);\r
-    gBS->RestoreTPL (OldTpl);\r
-  } else {\r
-    *HighCount  = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
-    mEfiMtc     = LShiftU64 (*HighCount, 32);\r
-  }\r
-  //\r
-  // Update the NvRam store to match the new high part\r
-  //\r
-  return EfiSetVariable (\r
-           mEfiMtcName,\r
-           &mEfiMtcGuid,\r
-           EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
-           sizeof (UINT32),\r
-           HighCount\r
-           );\r
-\r
-}\r
-\r
-STATIC\r
-VOID\r
-EFIAPI\r
-EfiMtcEventHandler (\r
-  IN EFI_EVENT                Event,\r
-  IN VOID                     *Context\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Monotonic count event handler.  This handler updates the high monotonic count.\r
-\r
-Arguments:\r
-\r
-  Event         The event to handle\r
-  Context       The event context\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS       The event has been handled properly\r
-  EFI_NOT_FOUND     An error occurred updating the variable.\r
-\r
---*/\r
-{\r
-  UINT32  HighCount;\r
-\r
-  MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-MonotonicCounterDriverInitialize (\r
-  IN EFI_HANDLE        ImageHandle,\r
-  IN EFI_SYSTEM_TABLE  *SystemTable\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-  (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
-\r
-Returns:\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-  UINT32      HighCount;\r
-  UINTN       BufferSize;\r
-\r
-  //\r
-  // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
-  //\r
-  ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
-\r
-  //\r
-  // Initialize event to handle overflows\r
-  //\r
-  Status = gBS->CreateEvent (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_CALLBACK,\r
-                  EfiMtcEventHandler,\r
-                  NULL,\r
-                  &mEfiMtcEvent\r
-                  );\r
-\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Read the last high part\r
-  //\r
-  BufferSize = sizeof (UINT32);\r
-  Status = EfiGetVariable (\r
-             mEfiMtcName,\r
-             &mEfiMtcGuid,\r
-             NULL,\r
-             &BufferSize,\r
-             &HighCount\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    HighCount = 0;\r
-  }\r
-  //\r
-  // Set the current value\r
-  //\r
-  mEfiMtc = LShiftU64 (HighCount, 32);\r
-\r
-  //\r
-  // Increment the upper 32 bits for this boot\r
-  // Continue even if it fails.  It will only fail if the variable services are\r
-  // not functional.\r
-  //\r
-  Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
-\r
-  //\r
-  // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
-  //\r
-  gBS->GetNextMonotonicCount      = MonotonicCounterDriverGetNextMonotonicCount;\r
-  gRT->GetNextHighMonotonicCount  = MonotonicCounterDriverGetNextHighMonotonicCount;\r
-\r
-  //\r
-  // Install the Monotonic Counter Architctural Protocol onto a new handle\r
-  //\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &mMonotonicCounterHandle,\r
-                  &gEfiMonotonicCounterArchProtocolGuid,\r
-                  NULL,\r
-                  NULL\r
-                  );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  return EFI_SUCCESS;\r
-}\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.h b/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.h
deleted file mode 100644 (file)
index c1ef509..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
-Module Name:\r
-\r
-  MonotonicCounter.h\r
-\r
-Abstract:\r
-\r
-  Produces the Monotonic Counter services as defined in the DXE CIS\r
-\r
---*/\r
-\r
-#ifndef _MONOTONIC_COUNTER_DRIVER_H_\r
-#define _MONOTONIC_COUNTER_DRIVER_H_\r
-\r
-//\r
-// The package level header files this module uses\r
-//\r
-#include <PiDxe.h>\r
-//\r
-// The protocols, PPI and GUID defintions for this module\r
-//\r
-#include <Protocol/MonotonicCounter.h>\r
-//\r
-// The Library classes this module consumes\r
-//\r
-#include <Library/BaseLib.h>\r
-#include <Library/UefiDriverEntryPoint.h>\r
-#include <Library/UefiRuntimeLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/UefiRuntimeServicesTableLib.h>\r
-\r
-//\r
-// Function Prototypes\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-MonotonicCounterDriverInitialize (\r
-  IN EFI_HANDLE                            ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                      *SystemTable\r
-  )\r
-;\r
-\r
-#endif\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.inf b/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.inf
deleted file mode 100644 (file)
index f16951a..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-#/** @file\r
-# Component description file for MonotonicCounter module.\r
-#\r
-# This module produces UEFI Monotonic Counter Boot and Runtime Services.\r
-# Copyright (c) 2006 - 2007, Intel Corporation\r
-#\r
-#  All rights reserved. This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution. The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-#\r
-#**/\r
-\r
-################################################################################\r
-#\r
-# Defines Section - statements that will be processed to create a Makefile.\r
-#\r
-################################################################################\r
-[Defines]\r
-  INF_VERSION                    = 0x00010005\r
-  BASE_NAME                      = MonotonicCounter\r
-  FILE_GUID                      = AD608272-D07F-4964-801E-7BD3B7888652\r
-  MODULE_TYPE                    = DXE_RUNTIME_DRIVER\r
-  VERSION_STRING                 = 1.0\r
-  EDK_RELEASE_VERSION            = 0x00020000\r
-  EFI_SPECIFICATION_VERSION      = 0x00020000\r
-\r
-  ENTRY_POINT                    = MonotonicCounterDriverInitialize\r
-\r
-#\r
-# The following information is for reference only and not required by the build tools.\r
-#\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
-#\r
-\r
-################################################################################\r
-#\r
-# Sources Section - list of files that are required for the build to succeed.\r
-#\r
-################################################################################\r
-\r
-[Sources.common]\r
-  MonotonicCounter.h\r
-  MonotonicCounter.c\r
-\r
-\r
-################################################################################\r
-#\r
-# Package Dependency Section - list of Package files that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Packages]\r
-  MdePkg/MdePkg.dec\r
-\r
-\r
-################################################################################\r
-#\r
-# Library Class Section - list of Library Classes that are required for\r
-#                         this module.\r
-#\r
-################################################################################\r
-\r
-[LibraryClasses]\r
-  UefiRuntimeServicesTableLib\r
-  UefiBootServicesTableLib\r
-  DebugLib\r
-  UefiRuntimeLib\r
-  UefiDriverEntryPoint\r
-  BaseLib\r
-\r
-\r
-################################################################################\r
-#\r
-# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
-#                           that this module uses or produces.\r
-#\r
-################################################################################\r
-\r
-[Protocols]\r
-  gEfiMonotonicCounterArchProtocolGuid          # PROTOCOL ALWAYS_PRODUCED\r
-\r
-\r
-################################################################################\r
-#\r
-# Dependency Expression Section - list of Dependency expressions that are required for\r
-#                              this module.\r
-#\r
-################################################################################\r
-\r
-[Depex]\r
-  gEfiVariableArchProtocolGuid AND gEfiVariableWriteArchProtocolGuid\r
-\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.msa b/MdeModulePkg/Universal/MonotonicCounterDxe/MonotonicCounter.msa
deleted file mode 100644 (file)
index 291f2ea..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0">\r
-  <MsaHeader>\r
-    <ModuleName>MonotonicCounter</ModuleName>\r
-    <ModuleType>DXE_RUNTIME_DRIVER</ModuleType>\r
-    <GuidValue>AD608272-D07F-4964-801E-7BD3B7888652</GuidValue>\r
-    <Version>1.0</Version>\r
-    <Abstract>Component description file for MonotonicCounter module.</Abstract>\r
-    <Description>This module produces UEFI Monotonic Counter Boot and Runtime Services.</Description>\r
-    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
-    <License>All rights reserved. This program and the accompanying materials\r
-      are licensed and made available under the terms and conditions of the BSD License\r
-      which accompanies this distribution.  The full text of the license may be found at\r
-      http://opensource.org/licenses/bsd-license.php\r
-      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.</License>\r
-    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
-  </MsaHeader>\r
-  <ModuleDefinitions>\r
-    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
-    <BinaryModule>false</BinaryModule>\r
-    <OutputFileBasename>MonotonicCounter</OutputFileBasename>\r
-  </ModuleDefinitions>\r
-  <LibraryClassDefinitions>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>BaseLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiDriverEntryPoint</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiRuntimeLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED" RecommendedInstanceGuid="bda39d3a-451b-4350-8266-81ab10fa0523">\r
-      <Keyword>DebugLib</Keyword>\r
-      <HelpText>Recommended libary Instance is PeiDxeDebugLibReportStatusCode instance in MdePkg.</HelpText>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiBootServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-    <LibraryClass Usage="ALWAYS_CONSUMED">\r
-      <Keyword>UefiRuntimeServicesTableLib</Keyword>\r
-    </LibraryClass>\r
-  </LibraryClassDefinitions>\r
-  <SourceFiles>\r
-    <Filename>MonotonicCounter.c</Filename>\r
-    <Filename>MonotonicCounter.h</Filename>\r
-    <Filename>MonotonicCounter.dxs</Filename>\r
-  </SourceFiles>\r
-  <PackageDependencies>\r
-    <Package PackageGuid="5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec"/>\r
-  </PackageDependencies>\r
-  <Protocols>\r
-    <Protocol Usage="ALWAYS_PRODUCED">\r
-      <ProtocolCName>gEfiMonotonicCounterArchProtocolGuid</ProtocolCName>\r
-    </Protocol>\r
-  </Protocols>\r
-  <Externs>\r
-    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
-    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
-    <Extern>\r
-      <ModuleEntryPoint>MonotonicCounterDriverInitialize</ModuleEntryPoint>\r
-    </Extern>\r
-  </Externs>\r
-</ModuleSurfaceArea>\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.c b/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.c
new file mode 100644 (file)
index 0000000..31efab3
--- /dev/null
@@ -0,0 +1,299 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+  MonotonicCounter.c\r
+\r
+Abstract:\r
+\r
+  Produced the Monotonic Counter Services as defined in the DXE CIS\r
+\r
+Revision History:\r
+\r
+--*/\r
+\r
+#include "MonotonicCounter.h"\r
+\r
+//\r
+// The Monotonic Counter Handle\r
+//\r
+EFI_HANDLE  mMonotonicCounterHandle = NULL;\r
+\r
+//\r
+// The current Monotonic count value\r
+//\r
+UINT64      mEfiMtc;\r
+\r
+//\r
+// Event to use to update the Mtc's high part when wrapping\r
+//\r
+EFI_EVENT   mEfiMtcEvent;\r
+\r
+//\r
+// EfiMtcName - Variable name of the MTC value\r
+//\r
+CHAR16      *mEfiMtcName = (CHAR16 *) L"MTC";\r
+\r
+//\r
+// EfiMtcGuid - Guid of the MTC value\r
+//\r
+EFI_GUID    mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r
+\r
+//\r
+// Worker functions\r
+//\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+MonotonicCounterDriverGetNextMonotonicCount (\r
+  OUT UINT64  *Count\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  EFI_TPL OldTpl;\r
+\r
+  //\r
+  // Can not be called after ExitBootServices()\r
+  //\r
+  if (EfiAtRuntime ()) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  //\r
+  // Check input parameters\r
+  //\r
+  if (Count == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Update the monotonic counter with a lock\r
+  //\r
+  OldTpl  = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
+  *Count  = mEfiMtc;\r
+  mEfiMtc++;\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  //\r
+  // If the MSB bit of the low part toggled, then signal that the high\r
+  // part needs updated now\r
+  //\r
+  if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r
+    gBS->SignalEvent (mEfiMtcEvent);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Returns the next high 32 bits of the platform's monotonic counter.\r
+\r
+  The GetNextHighMonotonicCount() function returns the next high 32 bits\r
+  of the platform's monotonic counter. The platform's monotonic counter is\r
+  comprised of two 32 bit quantities:  the high 32 bits and the low 32 bits.\r
+  During boot service time the low 32 bit value is volatile:  it is reset to\r
+  zero on every system reset and is increased by 1 on every call to GetNextMonotonicCount().\r
+  The high 32 bit value is non-volatile and is increased by 1 whenever the system resets\r
+  or whenever the low 32 bit count [returned by GetNextMonoticCount()] overflows.\r
+  The GetNextMonotonicCount() function is only available at boot services time.\r
+  If the operating system wishes to extend the platform monotonic counter to runtime,\r
+  it may do so by utilizing GetNextHighMonotonicCount().  To do this, before calling\r
+  ExitBootServices() the operating system would call GetNextMonotonicCount() to obtain\r
+  the current platform monotonic count.  The operating system would then provide an\r
+  interface that returns the next count by:\r
+    Adding 1 to the last count.\r
+    Before the lower 32 bits of the count overflows, call GetNextHighMonotonicCount().\r
+    This will increase the high 32 bits of the platform's non-volatile portion of the monotonic\r
+    count by 1.\r
+\r
+  This function may only be called at Runtime.\r
+\r
+  @param[out]   HighCount      Pointer to returned value.\r
+\r
+  @retval EFI_INVALID_PARAMETER If HighCount is NULL.\r
+  @retval EFI_SUCCESS           Operation is successful.\r
+  @retval EFI_OUT_OF_RESOURCES  If variable service reports that not enough storage\r
+                                is available to hold the variable and its data.\r
+  @retval EFI_DEVICE_ERROR      The variable could not be saved due to a hardware failure.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+MonotonicCounterDriverGetNextHighMonotonicCount (\r
+  OUT UINT32  *HighCount\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  EFI_TPL     OldTpl;\r
+\r
+  //\r
+  // Check input parameters\r
+  //\r
+  if (HighCount == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (!EfiAtRuntime ()) {\r
+    //\r
+    // Use a lock if called before ExitBootServices()\r
+    //\r
+    OldTpl      = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
+    *HighCount  = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
+    mEfiMtc     = LShiftU64 (*HighCount, 32);\r
+    gBS->RestoreTPL (OldTpl);\r
+  } else {\r
+    *HighCount  = (UINT32) RShiftU64 (mEfiMtc, 32) + 1;\r
+    mEfiMtc     = LShiftU64 (*HighCount, 32);\r
+  }\r
+  //\r
+  // Update the NvRam store to match the new high part\r
+  //\r
+  return EfiSetVariable (\r
+           mEfiMtcName,\r
+           &mEfiMtcGuid,\r
+           EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+           sizeof (UINT32),\r
+           HighCount\r
+           );\r
+\r
+}\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+EfiMtcEventHandler (\r
+  IN EFI_EVENT                Event,\r
+  IN VOID                     *Context\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Monotonic count event handler.  This handler updates the high monotonic count.\r
+\r
+Arguments:\r
+\r
+  Event         The event to handle\r
+  Context       The event context\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS       The event has been handled properly\r
+  EFI_NOT_FOUND     An error occurred updating the variable.\r
+\r
+--*/\r
+{\r
+  UINT32  HighCount;\r
+\r
+  MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+MonotonicCounterDriverInitialize (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+  (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT32      HighCount;\r
+  UINTN       BufferSize;\r
+\r
+  //\r
+  // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
+  //\r
+  ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
+\r
+  //\r
+  // Initialize event to handle overflows\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  EfiMtcEventHandler,\r
+                  NULL,\r
+                  &mEfiMtcEvent\r
+                  );\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Read the last high part\r
+  //\r
+  BufferSize = sizeof (UINT32);\r
+  Status = EfiGetVariable (\r
+             mEfiMtcName,\r
+             &mEfiMtcGuid,\r
+             NULL,\r
+             &BufferSize,\r
+             &HighCount\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    HighCount = 0;\r
+  }\r
+  //\r
+  // Set the current value\r
+  //\r
+  mEfiMtc = LShiftU64 (HighCount, 32);\r
+\r
+  //\r
+  // Increment the upper 32 bits for this boot\r
+  // Continue even if it fails.  It will only fail if the variable services are\r
+  // not functional.\r
+  //\r
+  Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
+\r
+  //\r
+  // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
+  //\r
+  gBS->GetNextMonotonicCount      = MonotonicCounterDriverGetNextMonotonicCount;\r
+  gRT->GetNextHighMonotonicCount  = MonotonicCounterDriverGetNextHighMonotonicCount;\r
+\r
+  //\r
+  // Install the Monotonic Counter Architctural Protocol onto a new handle\r
+  //\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mMonotonicCounterHandle,\r
+                  &gEfiMonotonicCounterArchProtocolGuid,\r
+                  NULL,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.h b/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.h
new file mode 100644 (file)
index 0000000..c1ef509
--- /dev/null
@@ -0,0 +1,54 @@
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  MonotonicCounter.h\r
+\r
+Abstract:\r
+\r
+  Produces the Monotonic Counter services as defined in the DXE CIS\r
+\r
+--*/\r
+\r
+#ifndef _MONOTONIC_COUNTER_DRIVER_H_\r
+#define _MONOTONIC_COUNTER_DRIVER_H_\r
+\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <PiDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Protocol/MonotonicCounter.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiRuntimeLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+\r
+//\r
+// Function Prototypes\r
+//\r
+EFI_STATUS\r
+EFIAPI\r
+MonotonicCounterDriverInitialize (\r
+  IN EFI_HANDLE                            ImageHandle,\r
+  IN EFI_SYSTEM_TABLE                      *SystemTable\r
+  )\r
+;\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.inf b/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.inf
new file mode 100644 (file)
index 0000000..4b3ac22
--- /dev/null
@@ -0,0 +1,97 @@
+#/** @file\r
+# Component description file for MonotonicCounter module.\r
+#\r
+# This module produces UEFI Monotonic Counter Boot and Runtime Services.\r
+# Copyright (c) 2006 - 2007, Intel Corporation\r
+#\r
+#  All rights reserved. This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+################################################################################\r
+#\r
+# Defines Section - statements that will be processed to create a Makefile.\r
+#\r
+################################################################################\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = MonotonicCounterRuntimeDxe\r
+  FILE_GUID                      = AD608272-D07F-4964-801E-7BD3B7888652\r
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  EDK_RELEASE_VERSION            = 0x00020000\r
+  EFI_SPECIFICATION_VERSION      = 0x00020000\r
+\r
+  ENTRY_POINT                    = MonotonicCounterDriverInitialize\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#\r
+\r
+################################################################################\r
+#\r
+# Sources Section - list of files that are required for the build to succeed.\r
+#\r
+################################################################################\r
+\r
+[Sources.common]\r
+  MonotonicCounter.h\r
+  MonotonicCounter.c\r
+\r
+\r
+################################################################################\r
+#\r
+# Package Dependency Section - list of Package files that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+\r
+\r
+################################################################################\r
+#\r
+# Library Class Section - list of Library Classes that are required for\r
+#                         this module.\r
+#\r
+################################################################################\r
+\r
+[LibraryClasses]\r
+  UefiRuntimeServicesTableLib\r
+  UefiBootServicesTableLib\r
+  DebugLib\r
+  UefiRuntimeLib\r
+  UefiDriverEntryPoint\r
+  BaseLib\r
+\r
+\r
+################################################################################\r
+#\r
+# Protocol C Name Section - list of Protocol and Protocol Notify C Names\r
+#                           that this module uses or produces.\r
+#\r
+################################################################################\r
+\r
+[Protocols]\r
+  gEfiMonotonicCounterArchProtocolGuid          # PROTOCOL ALWAYS_PRODUCED\r
+\r
+\r
+################################################################################\r
+#\r
+# Dependency Expression Section - list of Dependency expressions that are required for\r
+#                              this module.\r
+#\r
+################################################################################\r
+\r
+[Depex]\r
+  gEfiVariableArchProtocolGuid AND gEfiVariableWriteArchProtocolGuid\r
+\r
diff --git a/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.msa b/MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounterRuntimeDxe.msa
new file mode 100644 (file)
index 0000000..291f2ea
--- /dev/null
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<ModuleSurfaceArea xmlns="http://www.TianoCore.org/2006/Edk2.0">\r
+  <MsaHeader>\r
+    <ModuleName>MonotonicCounter</ModuleName>\r
+    <ModuleType>DXE_RUNTIME_DRIVER</ModuleType>\r
+    <GuidValue>AD608272-D07F-4964-801E-7BD3B7888652</GuidValue>\r
+    <Version>1.0</Version>\r
+    <Abstract>Component description file for MonotonicCounter module.</Abstract>\r
+    <Description>This module produces UEFI Monotonic Counter Boot and Runtime Services.</Description>\r
+    <Copyright>Copyright (c) 2006 - 2007, Intel Corporation</Copyright>\r
+    <License>All rights reserved. This program and the accompanying materials\r
+      are licensed and made available under the terms and conditions of the BSD License\r
+      which accompanies this distribution.  The full text of the license may be found at\r
+      http://opensource.org/licenses/bsd-license.php\r
+      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.</License>\r
+    <Specification>FRAMEWORK_BUILD_PACKAGING_SPECIFICATION   0x00000052</Specification>\r
+  </MsaHeader>\r
+  <ModuleDefinitions>\r
+    <SupportedArchitectures>IA32 X64 IPF EBC</SupportedArchitectures>\r
+    <BinaryModule>false</BinaryModule>\r
+    <OutputFileBasename>MonotonicCounter</OutputFileBasename>\r
+  </ModuleDefinitions>\r
+  <LibraryClassDefinitions>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>BaseLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiDriverEntryPoint</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiRuntimeLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED" RecommendedInstanceGuid="bda39d3a-451b-4350-8266-81ab10fa0523">\r
+      <Keyword>DebugLib</Keyword>\r
+      <HelpText>Recommended libary Instance is PeiDxeDebugLibReportStatusCode instance in MdePkg.</HelpText>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiBootServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+    <LibraryClass Usage="ALWAYS_CONSUMED">\r
+      <Keyword>UefiRuntimeServicesTableLib</Keyword>\r
+    </LibraryClass>\r
+  </LibraryClassDefinitions>\r
+  <SourceFiles>\r
+    <Filename>MonotonicCounter.c</Filename>\r
+    <Filename>MonotonicCounter.h</Filename>\r
+    <Filename>MonotonicCounter.dxs</Filename>\r
+  </SourceFiles>\r
+  <PackageDependencies>\r
+    <Package PackageGuid="5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec"/>\r
+  </PackageDependencies>\r
+  <Protocols>\r
+    <Protocol Usage="ALWAYS_PRODUCED">\r
+      <ProtocolCName>gEfiMonotonicCounterArchProtocolGuid</ProtocolCName>\r
+    </Protocol>\r
+  </Protocols>\r
+  <Externs>\r
+    <Specification>EFI_SPECIFICATION_VERSION 0x00020000</Specification>\r
+    <Specification>EDK_RELEASE_VERSION 0x00020000</Specification>\r
+    <Extern>\r
+      <ModuleEntryPoint>MonotonicCounterDriverInitialize</ModuleEntryPoint>\r
+    </Extern>\r
+  </Externs>\r
+</ModuleSurfaceArea>\r