]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
Add SMM FTW wrapper driver since non-SMM FTW protocol can be used by some consumers...
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWriteSmm.c
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