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