]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Add SMM FTW wrapper driver since non-SMM FTW protocol can be used by some consumers...
authorgdong1 <gdong1@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 12 Jan 2011 09:05:27 +0000 (09:05 +0000)
committergdong1 <gdong1@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 12 Jan 2011 09:05:27 +0000 (09:05 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11246 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmCommon.h [new file with mode: 0644]
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.c [new file with mode: 0644]
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.h [new file with mode: 0644]
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.inf [new file with mode: 0644]

index ffa7cb91e48b7a470ee64907f35221664b459189..4179d594dd6bddaf01a565d2879ad63d10fcf81f 100644 (file)
@@ -40,7 +40,7 @@
   If one of them is not satisfied, FtwWrite may fail.\r
   Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.\r
 \r
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
@@ -51,14 +51,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
+#include <PiSmm.h>\r
 #include <Library/SmmServicesTableLib.h>\r
-#include "FaultTolerantWrite.h"\r
-#include <Protocol/SmmFirmwareVolumeBlock.h>\r
 #include <Protocol/SmmSwapAddressRange.h>\r
-#include <Protocol/SmmFaultTolerantWrite.h>\r
+#include "FaultTolerantWrite.h"\r
+#include "FaultTolerantWriteSmmCommon.h"\r
 \r
 EFI_EVENT                                 mFvbRegistration = NULL;\r
-EFI_FTW_DEVICE                            *gFtwDevice      = NULL;\r
+EFI_FTW_DEVICE                            *mFtwDevice      = NULL;\r
 \r
 /**\r
   Retrive the SMM FVB protocol interface by HANDLE.\r
@@ -180,6 +180,211 @@ GetFvbCountAndBuffer (
 }\r
 \r
 \r
+/**\r
+  Get the handle of the SMM FVB protocol by the FVB base address and attributes.\r
+\r
+  @param[in]  Address       The base address of SMM FVB protocol.\r
+  @param[in]  Attributes    The attributes of the SMM FVB protocol.\r
+  @param[out] SmmFvbHandle  The handle of the SMM FVB protocol.\r
+\r
+  @retval  EFI_SUCCESS    The FVB handle is found.\r
+  @retval  EFI_ABORTED    The FVB protocol is not found.\r
+\r
+**/\r
+EFI_STATUS\r
+GetFvbByAddressAndAttribute (\r
+  IN  EFI_PHYSICAL_ADDRESS            Address,\r
+  IN  EFI_FVB_ATTRIBUTES_2            Attributes,\r
+  OUT EFI_HANDLE                      *SmmFvbHandle\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_HANDLE                          *HandleBuffer;\r
+  UINTN                               HandleCount;\r
+  UINTN                               Index;\r
+  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;\r
+  EFI_FVB_ATTRIBUTES_2                FvbAttributes;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
+\r
+  //\r
+  // Locate all handles of SMM Fvb protocol.\r
+  //\r
+  Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_ABORTED;\r
+  }\r
+  \r
+  //\r
+  // Find the proper SMM Fvb handle by the address and attributes.\r
+  //\r
+  for (Index = 0; Index < HandleCount; Index++) {\r
+    Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);\r
+    if (EFI_ERROR (Status)) {\r
+      break;\r
+    }\r
+    //\r
+    // Compare the address.\r
+    //\r
+    Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+    if (Address != FvbBaseAddress) {\r
+     continue;\r
+    }\r
+\r
+    //\r
+    // Compare the attribute.\r
+    //\r
+    Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+    if (Attributes != FvbAttributes) {\r
+     continue;\r
+    }\r
+\r
+    //\r
+    // Found the proper FVB handle.\r
+    //\r
+    *SmmFvbHandle = HandleBuffer[Index];\r
+    FreePool (HandleBuffer);\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  FreePool (HandleBuffer);\r
+  return EFI_ABORTED;\r
+}\r
+\r
+/**\r
+  Communication service SMI Handler entry.\r
+\r
+  This SMI handler provides services for the fault tolerant write wrapper driver.\r
+\r
+  @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param[in]     RegisterContext Points to an optional handler context which was specified when the\r
+                                 handler was registered.\r
+  @param[in, out] CommBuffer     A pointer to a collection of data in memory that will be conveyed\r
+                                 from a non-SMM environment into an SMM environment.\r
+  @param[in, out] CommBufferSize The size of the CommBuffer.\r
+\r
+  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers \r
+                                              should still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should \r
+                                              still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still \r
+                                              be called.\r
+  @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
+  \r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmFaultTolerantWriteHandler (\r
+  IN     EFI_HANDLE                                DispatchHandle,\r
+  IN     CONST VOID                                *RegisterContext,\r
+  IN OUT VOID                                      *CommBuffer,\r
+  IN OUT UINTN                                     *CommBufferSize\r
+  )\r
+{\r
+  EFI_STATUS                                       Status;\r
+  SMM_FTW_COMMUNICATE_FUNCTION_HEADER              *SmmFtwFunctionHeader;\r
+  SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER                *SmmGetMaxBlockSizeHeader;\r
+  SMM_FTW_ALLOCATE_HEADER                          *SmmFtwAllocateHeader;\r
+  SMM_FTW_WRITE_HEADER                             *SmmFtwWriteHeader;\r
+  SMM_FTW_RESTART_HEADER                           *SmmFtwRestartHeader;\r
+  SMM_FTW_GET_LAST_WRITE_HEADER                    *SmmFtwGetLastWriteHeader;\r
+  VOID                                             *PrivateData;\r
+  EFI_HANDLE                                       SmmFvbHandle;\r
+\r
+  ASSERT (CommBuffer != NULL);\r
+  ASSERT (CommBufferSize != NULL);\r
+\r
+  SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;\r
+  switch (SmmFtwFunctionHeader->Function) {\r
+    case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:\r
+      SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *) SmmFtwFunctionHeader->Data;     \r
+      Status = FtwGetMaxBlockSize (\r
+                 &mFtwDevice->FtwInstance,\r
+                 &SmmGetMaxBlockSizeHeader->BlockSize\r
+                 );\r
+      break;\r
+      \r
+    case FTW_FUNCTION_ALLOCATE:\r
+      SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *) SmmFtwFunctionHeader->Data;\r
+      Status = FtwAllocate (\r
+                 &mFtwDevice->FtwInstance,\r
+                 &SmmFtwAllocateHeader->CallerId,\r
+                 SmmFtwAllocateHeader->PrivateDataSize,\r
+                 SmmFtwAllocateHeader->NumberOfWrites\r
+                 );\r
+      break;\r
+      \r
+    case FTW_FUNCTION_WRITE:\r
+      SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
+      if (SmmFtwWriteHeader->PrivateDataSize == 0) {\r
+        PrivateData = NULL;\r
+      } else {\r
+        PrivateData = (VOID *)&SmmFtwWriteHeader->Data[SmmFtwWriteHeader->Length];\r
+      }\r
+      Status = GetFvbByAddressAndAttribute (\r
+                 SmmFtwWriteHeader->FvbBaseAddress, \r
+                 SmmFtwWriteHeader->FvbAttributes,\r
+                 &SmmFvbHandle\r
+                 );\r
+      if (!EFI_ERROR (Status)) {\r
+        Status = FtwWrite(\r
+                   &mFtwDevice->FtwInstance,\r
+                   SmmFtwWriteHeader->Lba,\r
+                   SmmFtwWriteHeader->Offset,\r
+                   SmmFtwWriteHeader->Length,\r
+                   PrivateData,\r
+                   SmmFvbHandle,\r
+                   SmmFtwWriteHeader->Data\r
+                   );\r
+      }\r
+      break;\r
+      \r
+    case FTW_FUNCTION_RESTART:\r
+      SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;\r
+      Status = GetFvbByAddressAndAttribute (\r
+                 SmmFtwRestartHeader->FvbBaseAddress, \r
+                 SmmFtwRestartHeader->FvbAttributes,\r
+                 &SmmFvbHandle\r
+                 );      \r
+      if (!EFI_ERROR (Status)) {\r
+        Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);\r
+      }\r
+      break;\r
+\r
+    case FTW_FUNCTION_ABORT:\r
+      Status = FtwAbort (&mFtwDevice->FtwInstance);\r
+      break;\r
+      \r
+    case FTW_FUNCTION_GET_LAST_WRITE:\r
+      SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
+      Status = FtwGetLastWrite (\r
+                 &mFtwDevice->FtwInstance,\r
+                 &SmmFtwGetLastWriteHeader->CallerId,\r
+                 &SmmFtwGetLastWriteHeader->Lba,\r
+                 &SmmFtwGetLastWriteHeader->Offset,\r
+                 &SmmFtwGetLastWriteHeader->Length,\r
+                 &SmmFtwGetLastWriteHeader->PrivateDataSize,\r
+                 (VOID *)SmmFtwGetLastWriteHeader->Data,\r
+                 &SmmFtwGetLastWriteHeader->Complete\r
+                 );\r
+      break;\r
+\r
+    default:\r
+      ASSERT (FALSE);\r
+      Status = EFI_UNSUPPORTED;\r
+  }\r
+\r
+  SmmFtwFunctionHeader->ReturnStatus = Status;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
 /**\r
   SMM Firmware Volume Block Protocol notification event handler.\r
   \r
@@ -200,6 +405,7 @@ FvbNotificationEvent (
 {\r
   EFI_STATUS                              Status;\r
   EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL   *FtwProtocol;\r
+  EFI_HANDLE                              SmmFtwHandle;\r
   \r
   //\r
   // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
@@ -217,7 +423,7 @@ FvbNotificationEvent (
   //\r
   // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
   //\r
-  Status = InitFtwProtocol (gFtwDevice);\r
+  Status = InitFtwProtocol (mFtwDevice);\r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
@@ -226,12 +432,24 @@ FvbNotificationEvent (
   // Install protocol interface\r
   //\r
   Status = gSmst->SmmInstallProtocolInterface (\r
-                    &gFtwDevice->Handle,\r
+                    &mFtwDevice->Handle,\r
                     &gEfiSmmFaultTolerantWriteProtocolGuid,\r
                     EFI_NATIVE_INTERFACE,\r
-                    &gFtwDevice->FtwInstance\r
+                    &mFtwDevice->FtwInstance\r
                     );\r
   ASSERT_EFI_ERROR (Status); \r
+\r
+  //\r
+  // Notify the Ftw wrapper driver SMM Ftw is ready\r
+  //\r
+  SmmFtwHandle = NULL;\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &SmmFtwHandle,\r
+                  &gEfiSmmFaultTolerantWriteProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
   \r
   return EFI_SUCCESS;\r
 }\r
@@ -256,11 +474,12 @@ SmmFaultTolerantWriteInitialize (
   )\r
 {\r
   EFI_STATUS                              Status;\r
-\r
+  EFI_HANDLE                              FtwHandle;\r
+  \r
   //\r
   // Allocate private data structure for SMM FTW protocol and do some initialization\r
   //\r
-  Status = InitFtwDevice (&gFtwDevice);\r
+  Status = InitFtwDevice (&mFtwDevice);\r
   if (EFI_ERROR(Status)) {\r
     return Status;\r
   }\r
@@ -276,6 +495,12 @@ SmmFaultTolerantWriteInitialize (
   ASSERT_EFI_ERROR (Status);\r
 \r
   FvbNotificationEvent (NULL, NULL, NULL);\r
+\r
+  ///\r
+  /// Register SMM FTW SMI handler\r
+  ///\r
+  Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &FtwHandle);\r
+  ASSERT_EFI_ERROR (Status);\r
   \r
   return EFI_SUCCESS;\r
 }\r
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmCommon.h b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmCommon.h
new file mode 100644 (file)
index 0000000..061673b
--- /dev/null
@@ -0,0 +1,80 @@
+/** @file\r
+\r
+  The common header file for SMM FTW module and SMM FTW DXE Module. \r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved. <BR>\r
+This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+#ifndef __SMM_FTW_COMMON_H__\r
+#define __SMM_FTW_COMMON_H__\r
+\r
+#include <Protocol/SmmFirmwareVolumeBlock.h>\r
+#include <Protocol/SmmFaultTolerantWrite.h>\r
+\r
+#define FTW_FUNCTION_GET_MAX_BLOCK_SIZE       1\r
+#define FTW_FUNCTION_ALLOCATE                 2\r
+#define FTW_FUNCTION_WRITE                    3\r
+#define FTW_FUNCTION_RESTART                  4\r
+#define FTW_FUNCTION_ABORT                    5\r
+#define FTW_FUNCTION_GET_LAST_WRITE           6\r
+\r
+typedef struct {\r
+  UINTN       Function;\r
+  EFI_STATUS  ReturnStatus;\r
+  UINT8       Data[1];\r
+} SMM_FTW_COMMUNICATE_FUNCTION_HEADER;\r
+\r
+///\r
+/// Size of SMM communicate header, without including the payload.\r
+///\r
+#define SMM_COMMUNICATE_HEADER_SIZE  (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data))\r
+\r
+///\r
+/// Size of SMM FTW communicate function header, without including the payload.\r
+///\r
+#define SMM_FTW_COMMUNICATE_HEADER_SIZE  (OFFSET_OF (SMM_FTW_COMMUNICATE_FUNCTION_HEADER, Data))\r
+\r
+typedef struct {\r
+  UINTN                                 BlockSize;\r
+} SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER;\r
+\r
+typedef struct {\r
+  EFI_GUID                              CallerId;\r
+  UINTN                                 PrivateDataSize;\r
+  UINTN                                 NumberOfWrites;\r
+} SMM_FTW_ALLOCATE_HEADER;\r
+\r
+typedef struct {\r
+  EFI_LBA                               Lba;\r
+  UINTN                                 Offset;\r
+  UINTN                                 PrivateDataSize;\r
+  EFI_PHYSICAL_ADDRESS                  FvbBaseAddress;\r
+  EFI_FVB_ATTRIBUTES_2                  FvbAttributes;\r
+  UINTN                                 Length;\r
+  UINT8                                 Data[1];\r
+} SMM_FTW_WRITE_HEADER;\r
+\r
+typedef struct {\r
+  EFI_PHYSICAL_ADDRESS                  FvbBaseAddress;\r
+  EFI_FVB_ATTRIBUTES_2                  FvbAttributes;\r
+} SMM_FTW_RESTART_HEADER;\r
+\r
+typedef struct {\r
+  EFI_GUID                              CallerId;\r
+  EFI_LBA                               Lba;\r
+  UINTN                                 Offset;\r
+  UINTN                                 Length;\r
+  UINTN                                 PrivateDataSize;\r
+  BOOLEAN                               Complete;\r
+  UINT8                                 Data[1];\r
+} SMM_FTW_GET_LAST_WRITE_HEADER;\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.c
new file mode 100644 (file)
index 0000000..89c7d33
--- /dev/null
@@ -0,0 +1,558 @@
+/** @file\r
+\r
+  Implement the Fault Tolerant Write (FTW) protocol based on SMM FTW \r
+  module.\r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved. <BR>\r
+This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+#include "FaultTolerantWriteSmmDxe.h"\r
+\r
+EFI_HANDLE                         mHandle                   = NULL;\r
+EFI_SMM_COMMUNICATION_PROTOCOL     *mSmmCommunication        = NULL;\r
+UINTN                              mPrivateDataSize          = 0;\r
+\r
+EFI_FAULT_TOLERANT_WRITE_PROTOCOL  mFaultTolerantWriteDriver = {\r
+  FtwGetMaxBlockSize,\r
+  FtwAllocate,\r
+  FtwWrite,\r
+  FtwRestart,\r
+  FtwAbort,\r
+  FtwGetLastWrite\r
+};\r
+\r
+/**\r
+  Initialize the communicate buffer using DataSize and Function number.\r
+\r
+  @param[out]      CommunicateBuffer The communicate buffer. Caller should free it after use.\r
+  @param[out]      DataPtr           Points to the data in the communicate buffer. Caller should not free it.\r
+  @param[in]       DataSize          The payload size.\r
+  @param[in]       Function          The function number used to initialize the communicate header.\r
+\r
+**/\r
+VOID\r
+InitCommunicateBuffer (\r
+  OUT     VOID                              **CommunicateBuffer,\r
+  OUT     VOID                              **DataPtr,\r
+  IN      UINTN                             DataSize,\r
+  IN      UINTN                             Function\r
+  )\r
+{\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_COMMUNICATE_FUNCTION_HEADER       *SmmFtwFunctionHeader; \r
+\r
+  //\r
+  // The whole buffer size: SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE + DataSize.\r
+  //\r
+  SmmCommunicateHeader = AllocateZeroPool (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE);\r
+  ASSERT (SmmCommunicateHeader != NULL);\r
+   \r
+  //\r
+  // Prepare data buffer.\r
+  //\r
+  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmFaultTolerantWriteProtocolGuid);\r
+  SmmCommunicateHeader->MessageLength = DataSize + SMM_FTW_COMMUNICATE_HEADER_SIZE;\r
\r
+  SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *) SmmCommunicateHeader->Data;\r
+  SmmFtwFunctionHeader->Function = Function;\r
+\r
+  *CommunicateBuffer = SmmCommunicateHeader;\r
+  if (DataPtr != NULL) {\r
+    *DataPtr = SmmFtwFunctionHeader->Data;\r
+  }  \r
+}\r
+\r
+\r
+/**\r
+  Send the data in communicate buffer to SMI handler and get response.\r
+\r
+  @param[out]      SmmCommunicateHeader    The communicate buffer.\r
+  @param[in]       DataSize                The payload size.\r
+                      \r
+**/\r
+EFI_STATUS\r
+SendCommunicateBuffer (\r
+  IN      EFI_SMM_COMMUNICATE_HEADER        *SmmCommunicateHeader,\r
+  IN      UINTN                             DataSize\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     CommSize;\r
+  SMM_FTW_COMMUNICATE_FUNCTION_HEADER       *SmmFtwFunctionHeader; \r
\r
+  CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE;\r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, SmmCommunicateHeader, &CommSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *) SmmCommunicateHeader->Data;\r
+  return  SmmFtwFunctionHeader->ReturnStatus;\r
+}\r
+\r
+\r
+/**\r
+  Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.\r
+\r
+  @param[in]   FvBlockHandle     The handle of FVB protocol that provides services.\r
+  @param[in]   FvbBaseAddress    The base address of the FVB attached with FvBlockHandle.\r
+  @param[out]  FvbAttributes     The attributes of the FVB attached with FvBlockHandle.\r
+    \r
+  @retval EFI_SUCCESS            The function completed successfully.\r
+  @retval Others                 The function could not complete successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+ConvertFvbHandle (\r
+  IN  EFI_HANDLE                            FvbHandle,\r
+  OUT EFI_PHYSICAL_ADDRESS                  *FvbBaseAddress,\r
+  OUT EFI_FVB_ATTRIBUTES_2                  *FvbAttributes\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL        *Fvb;\r
+\r
+  Status = gBS->HandleProtocol (FvbHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **) &Fvb);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  \r
+  Status = Fvb->GetPhysicalAddress (Fvb, FvbBaseAddress);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = Fvb->GetAttributes (Fvb, FvbAttributes);\r
+  return Status;  \r
+}\r
+\r
+\r
+/**\r
+  Get the size of the largest block that can be updated in a fault-tolerant manner.\r
+\r
+  @param[in]  This             Indicates a pointer to the calling context.\r
+  @param[out] BlockSize        A pointer to a caller-allocated UINTN that is\r
+                               updated to indicate the size of the largest block\r
+                               that can be updated.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwGetMaxBlockSize (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  OUT UINTN                                 *BlockSize\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     PayloadSize;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER         *SmmFtwBlockSizeHeader;\r
+\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  PayloadSize  = sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER);\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwBlockSizeHeader, PayloadSize, FTW_FUNCTION_GET_MAX_BLOCK_SIZE);\r
+    \r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r
+\r
+  //\r
+  // Get data from SMM\r
+  //\r
+  *BlockSize = SmmFtwBlockSizeHeader->BlockSize; \r
+  FreePool (SmmCommunicateHeader);\r
+  \r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Allocates space for the protocol to maintain information about writes.\r
+  Since writes must be completed in a fault-tolerant manner and multiple\r
+  writes require more resources to be successful, this function\r
+  enables the protocol to ensure that enough space exists to track\r
+  information about upcoming writes.\r
+\r
+  @param[in]  This             A pointer to the calling context.\r
+  @param[in]  CallerId         The GUID identifying the write.\r
+  @param[in]  PrivateDataSize  The size of the caller's private data  that must be\r
+                               recorded for each write.\r
+  @param[in]  NumberOfWrites   The number of fault tolerant block writes that will\r
+                               need to occur.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_ACCESS_DENIED    Not all allocated writes have been completed.  All\r
+                               writes must be completed or aborted before another\r
+                               fault tolerant write can occur.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwAllocate (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_GUID                               *CallerId,\r
+  IN UINTN                                  PrivateDataSize,\r
+  IN UINTN                                  NumberOfWrites\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     PayloadSize;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_ALLOCATE_HEADER                   *SmmFtwAllocateHeader;\r
+\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  PayloadSize  = sizeof (SMM_FTW_ALLOCATE_HEADER);\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwAllocateHeader, PayloadSize, FTW_FUNCTION_ALLOCATE);\r
+  CopyGuid (&SmmFtwAllocateHeader->CallerId, CallerId);\r
+  SmmFtwAllocateHeader->PrivateDataSize = PrivateDataSize;\r
+  SmmFtwAllocateHeader->NumberOfWrites  = NumberOfWrites; \r
+    \r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r
+  if (!EFI_ERROR( Status)) {\r
+    mPrivateDataSize = PrivateDataSize;\r
+  }\r
+\r
+  FreePool (SmmCommunicateHeader);\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Starts a target block update. This records information about the 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
+  @param[in]  This             The calling context.\r
+  @param[in]  Lba              The logical block address of the target block.\r
+  @param[in]  Offset           The offset within the target block to place the\r
+                               data.\r
+  @param[in]  Length           The number of bytes to write to the target block.\r
+  @param[in]  PrivateData      A pointer to private data that the caller requires\r
+                               to complete any pending writes in the event of a\r
+                               fault.\r
+  @param[in]  FvBlockHandle    The handle of FVB protocol that provides services\r
+                               for reading, writing, and erasing the target block.\r
+  @param[in]  Buffer           The data to write.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_BAD_BUFFER_SIZE  The write would span a block boundary, which is not\r
+                               a valid action.\r
+  @retval EFI_ACCESS_DENIED    No writes have been allocated.\r
+  @retval EFI_NOT_READY        The last write has not been completed. Restart()\r
+                               must be called to complete it.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwWrite (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_LBA                                Lba,\r
+  IN UINTN                                  Offset,\r
+  IN UINTN                                  Length,\r
+  IN VOID                                   *PrivateData,\r
+  IN EFI_HANDLE                             FvBlockHandle,\r
+  IN VOID                                   *Buffer\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     PayloadSize;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_WRITE_HEADER                      *SmmFtwWriteHeader;\r
+\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  PayloadSize  = OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length;\r
+  if (PrivateData != NULL) {\r
+    //\r
+    // The private data buffer size should be the same one in FtwAllocate API.\r
+    //\r
+    PayloadSize += mPrivateDataSize;\r
+  }\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwWriteHeader, PayloadSize, FTW_FUNCTION_WRITE);\r
+\r
+  //\r
+  // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address \r
+  // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.\r
+  //\r
+  Status = ConvertFvbHandle (FvBlockHandle, &SmmFtwWriteHeader->FvbBaseAddress, &SmmFtwWriteHeader->FvbAttributes);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (SmmCommunicateHeader);\r
+    return EFI_ABORTED;\r
+  }\r
+  \r
+  SmmFtwWriteHeader->Lba    = Lba;\r
+  SmmFtwWriteHeader->Offset = Offset; \r
+  SmmFtwWriteHeader->Length = Length;\r
+  CopyMem (SmmFtwWriteHeader->Data, Buffer, Length);\r
+  if (PrivateData == NULL) {\r
+    SmmFtwWriteHeader->PrivateDataSize = 0;\r
+  } else {\r
+    SmmFtwWriteHeader->PrivateDataSize = mPrivateDataSize;\r
+    CopyMem (&SmmFtwWriteHeader->Data[Length], PrivateData, mPrivateDataSize);\r
+  }\r
+\r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r
+  FreePool (SmmCommunicateHeader);  \r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Restarts a previously interrupted write. The caller must provide the\r
+  block protocol needed to complete the interrupted write.\r
+\r
+  @param[in]  This             The calling context.\r
+  @param[in]  FvBlockHandle    The handle of FVB protocol that provides services.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_ACCESS_DENIED    No pending writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwRestart (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_HANDLE                             FvBlockHandle\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     PayloadSize;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_RESTART_HEADER                    *SmmFtwRestartHeader;\r
\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  PayloadSize  = sizeof (SMM_FTW_RESTART_HEADER);\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwRestartHeader, PayloadSize, FTW_FUNCTION_RESTART); \r
+\r
+  //\r
+  // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address \r
+  // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.\r
+  //\r
+  Status = ConvertFvbHandle (FvBlockHandle, &SmmFtwRestartHeader->FvbBaseAddress, &SmmFtwRestartHeader->FvbAttributes);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (SmmCommunicateHeader); \r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r
+  FreePool (SmmCommunicateHeader);  \r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Aborts all previously allocated writes.\r
+\r
+  @param[in]  This             The calling context.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_NOT_FOUND        No allocated writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwAbort (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, NULL, 0, FTW_FUNCTION_ABORT);\r
\r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, 0);\r
+\r
+  FreePool (SmmCommunicateHeader);  \r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Starts a target block update. This function records information about the write\r
+  in fault-tolerant storage and completes 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
+  @param[in]      This            Indicates a pointer to the calling context.\r
+  @param[out]     CallerId        The GUID identifying the last write.\r
+  @param[out]     Lba             The logical block address of the last write.\r
+  @param[out]     Offset          The offset within the block of the last write.\r
+  @param[out]     Length          The length of the last write.\r
+  @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On\r
+                                  output, the size of the private data stored for\r
+                                  this write.\r
+  @param[out]     PrivateData     A pointer to a buffer. The function will copy\r
+                                  PrivateDataSize bytes from the private data stored\r
+                                  for this write.\r
+  @param[out]     Complete        A Boolean value with TRUE indicating that the write\r
+                                  was completed.\r
+\r
+  @retval EFI_SUCCESS             The function completed successfully.\r
+  @retval EFI_ABORTED             The function could not complete successfully.\r
+  @retval EFI_NOT_FOUND           No allocated writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwGetLastWrite (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  OUT EFI_GUID                              *CallerId,\r
+  OUT EFI_LBA                               *Lba,\r
+  OUT UINTN                                 *Offset,\r
+  OUT UINTN                                 *Length,\r
+  IN OUT UINTN                              *PrivateDataSize,\r
+  OUT VOID                                  *PrivateData,\r
+  OUT BOOLEAN                               *Complete\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  UINTN                                     PayloadSize;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommunicateHeader;  \r
+  SMM_FTW_GET_LAST_WRITE_HEADER             *SmmFtwGetLastWriteHeader;\r
+\r
+  //\r
+  // Initialize the communicate buffer.\r
+  //\r
+  PayloadSize  = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + *PrivateDataSize;\r
+  InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwGetLastWriteHeader, PayloadSize, FTW_FUNCTION_GET_LAST_WRITE);\r
+  SmmFtwGetLastWriteHeader->PrivateDataSize = *PrivateDataSize;\r
+\r
+  //\r
+  // Send data to SMM.\r
+  //\r
+  Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r
+\r
+  //\r
+  // Get data from SMM\r
+  //\r
+  *PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;\r
+  if (!EFI_ERROR (Status)) {\r
+    *Lba      = SmmFtwGetLastWriteHeader->Lba;\r
+    *Offset   = SmmFtwGetLastWriteHeader->Offset; \r
+    *Length   = SmmFtwGetLastWriteHeader->Length;\r
+    *Complete = SmmFtwGetLastWriteHeader->Complete;\r
+    CopyGuid (CallerId, &SmmFtwGetLastWriteHeader->CallerId);\r
+    CopyMem (PrivateData, SmmFtwGetLastWriteHeader->Data, *PrivateDataSize);\r
+  }\r
+\r
+  FreePool (SmmCommunicateHeader);  \r
+  return Status;\r
+}\r
+\r
+/**\r
+  SMM Fault Tolerant Write Protocol notification event handler.\r
+\r
+  Install Fault Tolerant Write Protocol.\r
+\r
+  @param[in] Event    Event whose notification function is being invoked.\r
+  @param[in] Context  Pointer to the notification function's context.\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmFtwReady (\r
+  IN  EFI_EVENT                             Event,\r
+  IN  VOID                                  *Context\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  EFI_FAULT_TOLERANT_WRITE_PROTOCOL         *FtwProtocol;\r
+\r
+  //\r
+  // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
+  // if Fault Tolerant Write protocol had been installed.\r
+  //  \r
+  Status = gBS->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid, NULL, (VOID **)&FtwProtocol);\r
+  if (!EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+  \r
+  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Install protocol interface\r
+  //\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &mHandle,\r
+                  &gEfiFaultTolerantWriteProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  &mFaultTolerantWriteDriver\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
+  Status = gBS->CloseEvent (Event);\r
+  ASSERT_EFI_ERROR (Status);  \r
+}\r
+\r
+\r
+/**\r
+  The driver entry point for Fault Tolerant Write driver.\r
+\r
+  The function does the necessary initialization work.\r
+\r
+  @param[in]  ImageHandle       The firmware allocated handle for the UEFI image.\r
+  @param[in]  SystemTable       A pointer to the EFI system table.\r
+\r
+  @retval     EFI_SUCCESS       This funtion always return EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FaultTolerantWriteSmmInitialize (\r
+  IN EFI_HANDLE                             ImageHandle,\r
+  IN EFI_SYSTEM_TABLE                       *SystemTable\r
+  )\r
+{\r
+  VOID                                      *SmmFtwRegistration;\r
+\r
+  //\r
+  // Smm FTW driver is ready\r
+  //\r
+  EfiCreateProtocolNotifyEvent (\r
+    &gEfiSmmFaultTolerantWriteProtocolGuid,\r
+    TPL_CALLBACK, \r
+    SmmFtwReady, \r
+    NULL, \r
+    &SmmFtwRegistration\r
+    );\r
+  \r
+  return EFI_SUCCESS;\r
+}\r
+\r
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.h b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.h
new file mode 100644 (file)
index 0000000..b4c20ae
--- /dev/null
@@ -0,0 +1,202 @@
+/** @file\r
+\r
+  The internal header file includes the common header files, defines\r
+  internal structure and functions used by FTW module.\r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved. <BR>\r
+This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+**/\r
+\r
+#ifndef __SMM_FTW_DXE_H__\r
+#define __SMM_FTW_DXE_H__\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/SmmCommunication.h>\r
+\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include <Guid/EventGroup.h>\r
+\r
+#include "FaultTolerantWriteSmmCommon.h"\r
+\r
+/**\r
+  Get the size of the largest block that can be updated in a fault-tolerant manner.\r
+\r
+  @param[in]  This             Indicates a pointer to the calling context.\r
+  @param[out] BlockSize        A pointer to a caller-allocated UINTN that is\r
+                               updated to indicate the size of the largest block\r
+                               that can be updated.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwGetMaxBlockSize (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  OUT UINTN                                 *BlockSize\r
+  );\r
+\r
+\r
+/**\r
+  Allocates space for the protocol to maintain information about writes.\r
+  Since writes must be completed in a fault-tolerant manner and multiple\r
+  writes require more resources to be successful, this function\r
+  enables the protocol to ensure that enough space exists to track\r
+  information about upcoming writes.\r
+\r
+  @param[in]  This             A pointer to the calling context.\r
+  @param[in]  CallerId         The GUID identifying the write.\r
+  @param[in]  PrivateDataSize  The size of the caller's private data  that must be\r
+                               recorded for each write.\r
+  @param[in]  NumberOfWrites   The number of fault tolerant block writes that will\r
+                               need to occur.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_ACCESS_DENIED    Not all allocated writes have been completed.  All\r
+                               writes must be completed or aborted before another\r
+                               fault tolerant write can occur.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwAllocate (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_GUID                               *CallerId,\r
+  IN UINTN                                  PrivateDataSize,\r
+  IN UINTN                                  NumberOfWrites\r
+  );\r
+\r
+\r
+/**\r
+  Starts a target block update. This records information about the 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
+  @param[in]  This             The calling context.\r
+  @param[in]  Lba              The logical block address of the target block.\r
+  @param[in]  Offset           The offset within the target block to place the\r
+                               data.\r
+  @param[in]  Length           The number of bytes to write to the target block.\r
+  @param[in]  PrivateData      A pointer to private data that the caller requires\r
+                               to complete any pending writes in the event of a\r
+                               fault.\r
+  @param[in]  FvBlockHandle    The handle of FVB protocol that provides services\r
+                               for reading, writing, and erasing the target block.\r
+  @param[in]  Buffer           The data to write.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_BAD_BUFFER_SIZE  The write would span a block boundary, which is not\r
+                               a valid action.\r
+  @retval EFI_ACCESS_DENIED    No writes have been allocated.\r
+  @retval EFI_NOT_READY        The last write has not been completed. Restart()\r
+                               must be called to complete it.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwWrite (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_LBA                                Lba,\r
+  IN UINTN                                  Offset,\r
+  IN UINTN                                  Length,\r
+  IN VOID                                   *PrivateData,\r
+  IN EFI_HANDLE                             FvBlockHandle,\r
+  IN VOID                                   *Buffer\r
+  );\r
+\r
+\r
+/**\r
+  Restarts a previously interrupted write. The caller must provide the\r
+  block protocol needed to complete the interrupted write.\r
+\r
+  @param[in]  This             The calling context.\r
+  @param[in]  FvBlockHandle    The handle of FVB protocol that provides services.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_ACCESS_DENIED    No pending writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwRestart (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  IN EFI_HANDLE                             FvBlockHandle\r
+  );\r
+\r
+\r
+/**\r
+  Aborts all previously allocated writes.\r
+\r
+  @param  This                 The calling context.\r
+\r
+  @retval EFI_SUCCESS          The function completed successfully.\r
+  @retval EFI_ABORTED          The function could not complete successfully.\r
+  @retval EFI_NOT_FOUND        No allocated writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwAbort (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This\r
+  );\r
+\r
+\r
+/**\r
+  Starts a target block update. This function records information about the write\r
+  in fault-tolerant storage and completes 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
+  @param[in]      This            Indicates a pointer to the calling context.\r
+  @param[out]     CallerId        The GUID identifying the last write.\r
+  @param[out]     Lba             The logical block address of the last write.\r
+  @param[out]     Offset          The offset within the block of the last write.\r
+  @param[out]     Length          The length of the last write.\r
+  @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On\r
+                                  output, the size of the private data stored for\r
+                                  this write.\r
+  @param[out]     PrivateData     A pointer to a buffer. The function will copy\r
+                                  PrivateDataSize bytes from the private data stored\r
+                                  for this write.\r
+  @param[out]     Complete        A Boolean value with TRUE indicating that the write\r
+                                  was completed.\r
+\r
+  @retval EFI_SUCCESS             The function completed successfully.\r
+  @retval EFI_ABORTED             The function could not complete successfully.\r
+  @retval EFI_NOT_FOUND           No allocated writes exist.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FtwGetLastWrite (\r
+  IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL      *This,\r
+  OUT EFI_GUID                              *CallerId,\r
+  OUT EFI_LBA                               *Lba,\r
+  OUT UINTN                                 *Offset,\r
+  OUT UINTN                                 *Length,\r
+  IN OUT UINTN                              *PrivateDataSize,\r
+  OUT VOID                                  *PrivateData,\r
+  OUT BOOLEAN                               *Complete\r
+  );\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.inf
new file mode 100644 (file)
index 0000000..35aff05
--- /dev/null
@@ -0,0 +1,54 @@
+## @file\r
+# This module is the Runtime DXE part corresponding to SMM Fault Tolerant Write (FTW) module. \r
+# It installs FTW protocol and works with SMM FTW module together.\r
+#\r
+# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  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
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = FaultTolerantWriteSmmDxe\r
+  FILE_GUID                      = 98948C4A-70F2-4035-8E9F-5927493CFC07\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  ENTRY_POINT                    = FaultTolerantWriteSmmInitialize\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  FaultTolerantWriteSmmDxe.c\r
+  FaultTolerantWriteSmmDxe.h\r
+  FaultTolerantWriteSmmCommon.h\r
+  \r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib  \r
+  UefiBootServicesTableLib\r
+  DebugLib\r
+  DxeServicesTableLib\r
+  UefiDriverEntryPoint\r
+  PcdLib  \r
+\r
+[Protocols]\r
+  gEfiFaultTolerantWriteProtocolGuid           ## ALWAYS_PRODUCES\r
+  gEfiSmmCommunicationProtocolGuid\r
+  gEfiSmmFaultTolerantWriteProtocolGuid\r
+  gEfiFirmwareVolumeBlockProtocolGuid\r
+   \r
+[Depex]\r
+  gEfiSmmCommunicationProtocolGuid\r