Adapt the FaultTolerantWriteDxe driver to be used as a MM_STANDALONE
driver to provide UEFI fault tolerant write protocol functionality
for variable reclaim operation on EFI variables stored on a NOR flash
that is only accessible to code executing in MM Standalone mode.

Change-Id: I132dce2de3c564227db45bd3d1b803702243c1a4
Signed-off-by: Jagadeesh Ujja <jagadeesh.u...@arm.com>
---
 .../FaultTolerantWriteMmStandalone.inf             | 100 +++++++++++++++++++++
 .../FaultTolerantWriteDxe/FaultTolerantWriteSmm.c  |  72 +++++++++++++--
 .../FaultTolerantWriteDxe/UpdateWorkingBlock.c     |   8 +-
 3 files changed, 172 insertions(+), 8 deletions(-)
 create mode 100644 
MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteMmStandalone.inf

diff --git 
a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteMmStandalone.inf
 
b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteMmStandalone.inf
new file mode 100644
index 0000000..033ba95
--- /dev/null
+++ 
b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteMmStandalone.inf
@@ -0,0 +1,100 @@
+## @file
+#   Fault Tolerant Write Smm Driver.
+#
+#   This driver installs SMM Fault Tolerant Write (FTW) protocol, which 
provides fault
+#   tolerant write capability in SMM environment for block devices. Its 
implementation
+#   depends on the full functionality SMM FVB protocol that support read, 
write/erase
+#   flash access.
+#
+# Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
+#
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD 
License
+#  which accompanies this distribution. The full text of the license may be 
found at
+#  http://opensource.org/licenses/bsd-license.php
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR 
IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = FaultTolerantWriteMmStandalone
+  MODULE_UNI_FILE                = SmmFaultTolerantWriteDxe.uni
+  FILE_GUID                      = 470CB248-E8AC-473c-BB4F-81069A1FE6FD
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = SmmFaultTolerantWriteInitialize
+
+#
+# The following information is for reference only and not required by the 
build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+#
+
+[Sources]
+  FtwMisc.c
+  UpdateWorkingBlock.c
+  FaultTolerantWrite.c
+  FaultTolerantWriteSmm.c
+  FaultTolerantWrite.h
+  FaultTolerantWriteSmmCommon.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  ArmPkg/ArmPkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  MemoryAllocationLib
+  BaseMemoryLib
+  DebugLib
+  PcdLib
+  ReportStatusCodeLib
+  MemLib
+  StandaloneMmDriverEntryPoint
+
+[Guids]
+  #
+  # Signature in EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER
+  #
+  ## CONSUMES           ## GUID
+  ## PRODUCES           ## GUID
+  gEdkiiWorkingBlockSignatureGuid
+
+[Protocols]
+  gEfiSmmSwapAddressRangeProtocolGuid | 
gEfiMdeModulePkgTokenSpaceGuid.PcdFullFtwServiceEnable  ## SOMETIMES_CONSUMES
+  ## NOTIFY
+  ## CONSUMES
+  gEfiSmmFirmwareVolumeBlockProtocolGuid
+  ## PRODUCES
+  ## UNDEFINED # SmiHandlerRegister
+  gEfiSmmFaultTolerantWriteProtocolGuid
+  gEfiSmmEndOfDxeProtocolGuid                      ## CONSUMES
+
+[FeaturePcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFullFtwServiceEnable    ## CONSUMES
+
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase    ## 
SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64  ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize    ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase      ## 
SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64    ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize      ## CONSUMES
+
+#
+# gBS->CalculateCrc32() is consumed in EntryPoint.
+# PI spec said: When the DXE Foundation is notified that the 
EFI_RUNTIME_ARCH_PROTOCOL
+# has been installed, then the Boot Service CalculateCrc32() is available.
+# So add gEfiRuntimeArchProtocolGuid Depex here.
+#
+[Depex]
+  TRUE
+  #gEfiSmmFirmwareVolumeBlockProtocolGuid AND gEfiRuntimeArchProtocolGuid
+
+[UserExtensions.TianoCore."ExtraFiles"]
+  SmmFaultTolerantWriteDxeExtra.uni
diff --git 
a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c 
b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
index 27fcab1..dea7143 100644
--- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
+++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
@@ -44,6 +44,7 @@
   This driver need to make sure the CommBuffer is not in the SMRAM range.
 
 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -53,8 +54,13 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS 
IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
-
+#ifndef MM_STANDALONE
 #include <PiSmm.h>
+#include <Protocol/SmmEndOfDxe.h>
+#else
+#include <PiMm.h>
+#include <Library/StandaloneMmMemLib.h>
+#endif
 #include <Library/SmmServicesTableLib.h>
 #include <Library/SmmMemLib.h>
 #include <Library/BaseLib.h>
@@ -66,6 +72,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER 
EXPRESS OR IMPLIED.
 EFI_EVENT                                 mFvbRegistration = NULL;
 EFI_FTW_DEVICE                            *mFtwDevice      = NULL;
 
+#ifdef MM_STANDALONE
+EFI_MM_SYSTEM_TABLE *mMmst = NULL;
+#endif
 ///
 /// The flag to indicate whether the platform has left the DXE phase of 
execution.
 ///
@@ -92,7 +101,11 @@ FtwGetFvbByHandle (
   //
   // To get the SMM FVB protocol interface on the handle
   //
+#ifndef MM_STANDALONE
   return gSmst->SmmHandleProtocol (
+#else
+  return mMmst->MmHandleProtocol (
+#endif
                   FvBlockHandle,
                   &gEfiSmmFirmwareVolumeBlockProtocolGuid,
                   (VOID **) FvBlock
@@ -119,7 +132,11 @@ FtwGetSarProtocol (
   //
   // Locate Smm Swap Address Range protocol
   //
+#ifndef MM_STANDALONE
   Status = gSmst->SmmLocateProtocol (
+#else
+  Status = mMmst->MmLocateProtocol (
+#endif
                     &gEfiSmmSwapAddressRangeProtocolGuid,
                     NULL,
                     SarProtocol
@@ -158,7 +175,11 @@ GetFvbCountAndBuffer (
   BufferSize     = 0;
   *NumberHandles = 0;
   *Buffer        = NULL;
+#ifndef MM_STANDALONE
   Status = gSmst->SmmLocateHandle (
+#else
+ Status = mMmst->MmLocateHandle (
+#endif
                     ByProtocol,
                     &gEfiSmmFirmwareVolumeBlockProtocolGuid,
                     NULL,
@@ -173,15 +194,17 @@ GetFvbCountAndBuffer (
   if (*Buffer == NULL) {
     return EFI_OUT_OF_RESOURCES;
   }
-
+#ifndef MM_STANDALONE
   Status = gSmst->SmmLocateHandle (
+#else
+ Status = mMmst->MmLocateHandle (
+#endif
                     ByProtocol,
                     &gEfiSmmFirmwareVolumeBlockProtocolGuid,
                     NULL,
                     &BufferSize,
                     *Buffer
                     );
-
   *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
   if (EFI_ERROR(Status)) {
     *NumberHandles = 0;
@@ -335,8 +358,11 @@ SmmFaultTolerantWriteHandler (
     return EFI_SUCCESS;
   }
   CommBufferPayloadSize = TempCommBufferSize - SMM_FTW_COMMUNICATE_HEADER_SIZE;
-
+#ifndef MM_STANDALONE
   if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+#else
+  if (!MmIsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+#endif
     DEBUG ((EFI_D_ERROR, "SmmFtwHandler: SMM communication buffer in SMRAM or 
overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -418,12 +444,14 @@ SmmFaultTolerantWriteHandler (
                  &SmmFvbHandle
                  );
       if (!EFI_ERROR (Status)) {
+#ifndef MM_STANDALONE
         //
         // The AsmLfence() call here is to ensure the previous range/content
         // checks for the CommBuffer have been completed before calling into
         // FtwWrite().
         //
         AsmLfence ();
+#endif
         Status = FtwWrite(
                    &mFtwDevice->FtwInstance,
                    SmmFtwWriteHeader->Lba,
@@ -525,6 +553,7 @@ FvbNotificationEvent (
   EFI_STATUS                              Status;
   EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL   *FtwProtocol;
   EFI_HANDLE                              SmmFtwHandle;
+#ifndef MM_STANDALONE
   EFI_HANDLE                              FtwHandle;
 
   //
@@ -536,6 +565,13 @@ FvbNotificationEvent (
                     NULL,
                     (VOID **) &FtwProtocol
                     );
+#else
+  Status = mMmst->MmLocateProtocol (
+                    &gEfiSmmFaultTolerantWriteProtocolGuid,
+                    NULL,
+                    (VOID **) &FtwProtocol
+                    );
+#endif
   if (!EFI_ERROR (Status)) {
     return EFI_SUCCESS;
   }
@@ -551,7 +587,11 @@ FvbNotificationEvent (
   //
   // Install protocol interface
   //
+#ifndef MM_STANDALONE
   Status = gSmst->SmmInstallProtocolInterface (
+#else
+  Status = mMmst->MmInstallProtocolInterface (
+#endif
                     &mFtwDevice->Handle,
                     &gEfiSmmFaultTolerantWriteProtocolGuid,
                     EFI_NATIVE_INTERFACE,
@@ -562,6 +602,7 @@ FvbNotificationEvent (
   ///
   /// Register SMM FTW SMI handler
   ///
+#ifndef MM_STANDALONE
   Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, 
&gEfiSmmFaultTolerantWriteProtocolGuid, &SmmFtwHandle);
   ASSERT_EFI_ERROR (Status);
 
@@ -576,6 +617,10 @@ FvbNotificationEvent (
                   NULL
                   );
   ASSERT_EFI_ERROR (Status);
+#else
+  Status = mMmst->MmiHandlerRegister (SmmFaultTolerantWriteHandler, 
&gEfiSmmFaultTolerantWriteProtocolGuid, &SmmFtwHandle);
+  ASSERT_EFI_ERROR (Status);
+#endif
 
   return EFI_SUCCESS;
 }
@@ -617,12 +662,21 @@ EFI_STATUS
 EFIAPI
 SmmFaultTolerantWriteInitialize (
   IN EFI_HANDLE                           ImageHandle,
+#ifndef MM_STANDALONE
   IN EFI_SYSTEM_TABLE                     *SystemTable
+#else
+  IN EFI_MM_SYSTEM_TABLE                     *SystemTable
+#endif
   )
 {
   EFI_STATUS                              Status;
+#ifndef MM_STANDALONE
   VOID                                    *SmmEndOfDxeRegistration;
+#endif
 
+#ifdef MM_STANDALONE
+  mMmst = SystemTable;
+#endif
   //
   // Allocate private data structure for SMM FTW protocol and do some 
initialization
   //
@@ -630,7 +684,7 @@ SmmFaultTolerantWriteInitialize (
   if (EFI_ERROR(Status)) {
     return Status;
   }
-
+#ifndef MM_STANDALONE
   //
   // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
   //
@@ -650,6 +704,14 @@ SmmFaultTolerantWriteInitialize (
                     &mFvbRegistration
                     );
   ASSERT_EFI_ERROR (Status);
+#else
+  Status = mMmst->MmRegisterProtocolNotify (
+                    &gEfiSmmFirmwareVolumeBlockProtocolGuid,
+                    FvbNotificationEvent,
+                    &mFvbRegistration
+                    );
+  ASSERT_EFI_ERROR (Status);
+#endif
 
   FvbNotificationEvent (NULL, NULL, NULL);
 
diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c 
b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c
index 50d3421..b005d3a 100644
--- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c
+++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c
@@ -3,6 +3,7 @@
    Internal functions to operate Working Block Space.
 
 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -29,8 +30,9 @@ InitializeLocalWorkSpaceHeader (
   VOID
   )
 {
+#ifndef MM_STANDALONE
   EFI_STATUS                              Status;
-
+#endif
   //
   // Check signature with gEdkiiWorkingBlockSignatureGuid.
   //
@@ -56,7 +58,7 @@ InitializeLocalWorkSpaceHeader (
     sizeof (EFI_GUID)
     );
   mWorkingBlockHeader.WriteQueueSize = PcdGet32 
(PcdFlashNvStorageFtwWorkingSize) - sizeof 
(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);
-
+#ifndef MM_STANDALONE
   //
   // Crc is calculated with all the fields except Crc and STATE, so leave them 
as FTW_ERASED_BYTE.
   //
@@ -70,7 +72,7 @@ InitializeLocalWorkSpaceHeader (
                   &mWorkingBlockHeader.Crc
                   );
   ASSERT_EFI_ERROR (Status);
-
+#endif
   mWorkingBlockHeader.WorkingBlockValid    = FTW_VALID_STATE;
   mWorkingBlockHeader.WorkingBlockInvalid  = FTW_INVALID_STATE;
 }
-- 
1.9.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to