3 Implement the Fault Tolerant Write (FTW) protocol based on SMM FTW
6 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved. <BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "FaultTolerantWriteSmmDxe.h"
13 EFI_HANDLE mHandle
= NULL
;
14 EFI_MM_COMMUNICATION2_PROTOCOL
*mMmCommunication2
= NULL
;
15 UINTN mPrivateDataSize
= 0;
17 EFI_FAULT_TOLERANT_WRITE_PROTOCOL mFaultTolerantWriteDriver
= {
27 Initialize the communicate buffer using DataSize and Function number.
29 @param[out] CommunicateBuffer The communicate buffer. Caller should free it after use.
30 @param[out] DataPtr Points to the data in the communicate buffer. Caller should not free it.
31 @param[in] DataSize The payload size.
32 @param[in] Function The function number used to initialize the communicate header.
36 InitCommunicateBuffer (
37 OUT VOID
**CommunicateBuffer
,
43 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
44 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
47 // The whole buffer size: SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE + DataSize.
49 SmmCommunicateHeader
= AllocateZeroPool (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
);
50 ASSERT (SmmCommunicateHeader
!= NULL
);
53 // Prepare data buffer.
55 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmFaultTolerantWriteProtocolGuid
);
56 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
;
58 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*)SmmCommunicateHeader
->Data
;
59 SmmFtwFunctionHeader
->Function
= Function
;
61 *CommunicateBuffer
= SmmCommunicateHeader
;
62 if (DataPtr
!= NULL
) {
63 *DataPtr
= SmmFtwFunctionHeader
->Data
;
68 Send the data in communicate buffer to SMI handler and get response.
70 @param[in, out] SmmCommunicateHeader The communicate buffer.
71 @param[in] DataSize The payload size.
75 SendCommunicateBuffer (
76 IN OUT EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
,
82 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
84 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
;
85 Status
= mMmCommunication2
->Communicate (
91 ASSERT_EFI_ERROR (Status
);
93 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*)SmmCommunicateHeader
->Data
;
94 return SmmFtwFunctionHeader
->ReturnStatus
;
98 Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.
100 @param[in] FvbHandle The handle of FVB protocol that provides services.
101 @param[out] FvbBaseAddress The base address of the FVB attached with FvbHandle.
102 @param[out] FvbAttributes The attributes of the FVB attached with FvbHandle.
104 @retval EFI_SUCCESS The function completed successfully.
105 @retval Others The function could not complete successfully.
110 IN EFI_HANDLE FvbHandle
,
111 OUT EFI_PHYSICAL_ADDRESS
*FvbBaseAddress
,
112 OUT EFI_FVB_ATTRIBUTES_2
*FvbAttributes
116 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
118 Status
= gBS
->HandleProtocol (FvbHandle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**)&Fvb
);
119 if (EFI_ERROR (Status
)) {
123 Status
= Fvb
->GetPhysicalAddress (Fvb
, FvbBaseAddress
);
124 if (EFI_ERROR (Status
)) {
128 Status
= Fvb
->GetAttributes (Fvb
, FvbAttributes
);
133 Get the size of the largest block that can be updated in a fault-tolerant manner.
135 @param[in] This Indicates a pointer to the calling context.
136 @param[out] BlockSize A pointer to a caller-allocated UINTN that is
137 updated to indicate the size of the largest block
140 @retval EFI_SUCCESS The function completed successfully.
141 @retval EFI_ABORTED The function could not complete successfully.
147 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
153 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
154 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmFtwBlockSizeHeader
;
157 // Initialize the communicate buffer.
159 PayloadSize
= sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
);
160 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwBlockSizeHeader
, PayloadSize
, FTW_FUNCTION_GET_MAX_BLOCK_SIZE
);
165 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
170 *BlockSize
= SmmFtwBlockSizeHeader
->BlockSize
;
171 FreePool (SmmCommunicateHeader
);
177 Allocates space for the protocol to maintain information about writes.
178 Since writes must be completed in a fault-tolerant manner and multiple
179 writes require more resources to be successful, this function
180 enables the protocol to ensure that enough space exists to track
181 information about upcoming writes.
183 @param[in] This A pointer to the calling context.
184 @param[in] CallerId The GUID identifying the write.
185 @param[in] PrivateDataSize The size of the caller's private data that must be
186 recorded for each write.
187 @param[in] NumberOfWrites The number of fault tolerant block writes that will
190 @retval EFI_SUCCESS The function completed successfully
191 @retval EFI_ABORTED The function could not complete successfully.
192 @retval EFI_ACCESS_DENIED Not all allocated writes have been completed. All
193 writes must be completed or aborted before another
194 fault tolerant write can occur.
200 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
201 IN EFI_GUID
*CallerId
,
202 IN UINTN PrivateDataSize
,
203 IN UINTN NumberOfWrites
208 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
209 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
212 // Initialize the communicate buffer.
214 PayloadSize
= sizeof (SMM_FTW_ALLOCATE_HEADER
);
215 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwAllocateHeader
, PayloadSize
, FTW_FUNCTION_ALLOCATE
);
216 CopyGuid (&SmmFtwAllocateHeader
->CallerId
, CallerId
);
217 SmmFtwAllocateHeader
->PrivateDataSize
= PrivateDataSize
;
218 SmmFtwAllocateHeader
->NumberOfWrites
= NumberOfWrites
;
223 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
224 if (!EFI_ERROR (Status
)) {
225 mPrivateDataSize
= PrivateDataSize
;
228 FreePool (SmmCommunicateHeader
);
233 Starts a target block update. This records information about the write
234 in fault tolerant storage, and will complete the write in a recoverable
235 manner, ensuring at all times that either the original contents or
236 the modified contents are available.
238 @param[in] This The calling context.
239 @param[in] Lba The logical block address of the target block.
240 @param[in] Offset The offset within the target block to place the
242 @param[in] Length The number of bytes to write to the target block.
243 @param[in] PrivateData A pointer to private data that the caller requires
244 to complete any pending writes in the event of a
246 @param[in] FvBlockHandle The handle of FVB protocol that provides services
247 for reading, writing, and erasing the target block.
248 @param[in] Buffer The data to write.
250 @retval EFI_SUCCESS The function completed successfully.
251 @retval EFI_ABORTED The function could not complete successfully.
252 @retval EFI_BAD_BUFFER_SIZE The write would span a block boundary, which is not
254 @retval EFI_ACCESS_DENIED No writes have been allocated.
255 @retval EFI_NOT_READY The last write has not been completed. Restart()
256 must be called to complete it.
262 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
266 IN VOID
*PrivateData
,
267 IN EFI_HANDLE FvBlockHandle
,
273 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
274 SMM_FTW_WRITE_HEADER
*SmmFtwWriteHeader
;
277 // Initialize the communicate buffer.
279 PayloadSize
= OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
;
280 if (PrivateData
!= NULL
) {
282 // The private data buffer size should be the same one in FtwAllocate API.
284 PayloadSize
+= mPrivateDataSize
;
287 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwWriteHeader
, PayloadSize
, FTW_FUNCTION_WRITE
);
290 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
291 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
293 Status
= ConvertFvbHandle (FvBlockHandle
, &SmmFtwWriteHeader
->FvbBaseAddress
, &SmmFtwWriteHeader
->FvbAttributes
);
294 if (EFI_ERROR (Status
)) {
295 FreePool (SmmCommunicateHeader
);
299 SmmFtwWriteHeader
->Lba
= Lba
;
300 SmmFtwWriteHeader
->Offset
= Offset
;
301 SmmFtwWriteHeader
->Length
= Length
;
302 CopyMem (SmmFtwWriteHeader
->Data
, Buffer
, Length
);
303 if (PrivateData
== NULL
) {
304 SmmFtwWriteHeader
->PrivateDataSize
= 0;
306 SmmFtwWriteHeader
->PrivateDataSize
= mPrivateDataSize
;
307 CopyMem (&SmmFtwWriteHeader
->Data
[Length
], PrivateData
, mPrivateDataSize
);
313 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
314 FreePool (SmmCommunicateHeader
);
319 Restarts a previously interrupted write. The caller must provide the
320 block protocol needed to complete the interrupted write.
322 @param[in] This The calling context.
323 @param[in] FvBlockHandle The handle of FVB protocol that provides services.
325 @retval EFI_SUCCESS The function completed successfully.
326 @retval EFI_ABORTED The function could not complete successfully.
327 @retval EFI_ACCESS_DENIED No pending writes exist.
333 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
334 IN EFI_HANDLE FvBlockHandle
339 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
340 SMM_FTW_RESTART_HEADER
*SmmFtwRestartHeader
;
343 // Initialize the communicate buffer.
345 PayloadSize
= sizeof (SMM_FTW_RESTART_HEADER
);
346 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwRestartHeader
, PayloadSize
, FTW_FUNCTION_RESTART
);
349 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
350 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
352 Status
= ConvertFvbHandle (FvBlockHandle
, &SmmFtwRestartHeader
->FvbBaseAddress
, &SmmFtwRestartHeader
->FvbAttributes
);
353 if (EFI_ERROR (Status
)) {
354 FreePool (SmmCommunicateHeader
);
361 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
362 FreePool (SmmCommunicateHeader
);
367 Aborts all previously allocated writes.
369 @param[in] This The calling context.
371 @retval EFI_SUCCESS The function completed successfully.
372 @retval EFI_ABORTED The function could not complete successfully.
373 @retval EFI_NOT_FOUND No allocated writes exist.
379 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
383 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
386 // Initialize the communicate buffer.
388 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, NULL
, 0, FTW_FUNCTION_ABORT
);
393 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, 0);
395 FreePool (SmmCommunicateHeader
);
400 Starts a target block update. This function records information about the write
401 in fault-tolerant storage and completes the write in a recoverable
402 manner, ensuring at all times that either the original contents or
403 the modified contents are available.
405 @param[in] This Indicates a pointer to the calling context.
406 @param[out] CallerId The GUID identifying the last write.
407 @param[out] Lba The logical block address of the last write.
408 @param[out] Offset The offset within the block of the last write.
409 @param[out] Length The length of the last write.
410 @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On
411 output, the size of the private data stored for
413 @param[out] PrivateData A pointer to a buffer. The function will copy
414 PrivateDataSize bytes from the private data stored
416 @param[out] Complete A Boolean value with TRUE indicating that the write
419 @retval EFI_SUCCESS The function completed successfully.
420 @retval EFI_ABORTED The function could not complete successfully.
421 @retval EFI_NOT_FOUND No allocated writes exist.
427 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
428 OUT EFI_GUID
*CallerId
,
432 IN OUT UINTN
*PrivateDataSize
,
433 OUT VOID
*PrivateData
,
434 OUT BOOLEAN
*Complete
439 EFI_MM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
440 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
443 // Initialize the communicate buffer.
445 PayloadSize
= OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
) + *PrivateDataSize
;
446 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwGetLastWriteHeader
, PayloadSize
, FTW_FUNCTION_GET_LAST_WRITE
);
447 SmmFtwGetLastWriteHeader
->PrivateDataSize
= *PrivateDataSize
;
452 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
457 *PrivateDataSize
= SmmFtwGetLastWriteHeader
->PrivateDataSize
;
458 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_BUFFER_TOO_SMALL
)) {
459 *Lba
= SmmFtwGetLastWriteHeader
->Lba
;
460 *Offset
= SmmFtwGetLastWriteHeader
->Offset
;
461 *Length
= SmmFtwGetLastWriteHeader
->Length
;
462 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
463 CopyGuid (CallerId
, &SmmFtwGetLastWriteHeader
->CallerId
);
464 if (Status
== EFI_SUCCESS
) {
465 CopyMem (PrivateData
, SmmFtwGetLastWriteHeader
->Data
, *PrivateDataSize
);
467 } else if (Status
== EFI_NOT_FOUND
) {
468 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
471 FreePool (SmmCommunicateHeader
);
476 SMM Fault Tolerant Write Protocol notification event handler.
478 Install Fault Tolerant Write Protocol.
480 @param[in] Event Event whose notification function is being invoked.
481 @param[in] Context Pointer to the notification function's context.
491 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
494 // Just return to avoid install SMM FaultTolerantWriteProtocol again
495 // if Fault Tolerant Write protocol had been installed.
497 Status
= gBS
->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid
, NULL
, (VOID
**)&FtwProtocol
);
498 if (!EFI_ERROR (Status
)) {
502 Status
= gBS
->LocateProtocol (&gEfiMmCommunication2ProtocolGuid
, NULL
, (VOID
**)&mMmCommunication2
);
503 ASSERT_EFI_ERROR (Status
);
506 // Install protocol interface
508 Status
= gBS
->InstallProtocolInterface (
510 &gEfiFaultTolerantWriteProtocolGuid
,
511 EFI_NATIVE_INTERFACE
,
512 &mFaultTolerantWriteDriver
514 ASSERT_EFI_ERROR (Status
);
516 Status
= gBS
->CloseEvent (Event
);
517 ASSERT_EFI_ERROR (Status
);
521 The driver entry point for Fault Tolerant Write driver.
523 The function does the necessary initialization work.
525 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
526 @param[in] SystemTable A pointer to the EFI system table.
528 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
533 FaultTolerantWriteSmmInitialize (
534 IN EFI_HANDLE ImageHandle
,
535 IN EFI_SYSTEM_TABLE
*SystemTable
538 VOID
*SmmFtwRegistration
;
541 // Smm FTW driver is ready
543 EfiCreateProtocolNotifyEvent (
544 &gEfiSmmFaultTolerantWriteProtocolGuid
,