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_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= 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_SMM_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
;
69 Send the data in communicate buffer to SMI handler and get response.
71 @param[in, out] SmmCommunicateHeader The communicate buffer.
72 @param[in] DataSize The payload size.
76 SendCommunicateBuffer (
77 IN OUT EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
,
83 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
85 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
;
86 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, SmmCommunicateHeader
, &CommSize
);
87 ASSERT_EFI_ERROR (Status
);
89 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*) SmmCommunicateHeader
->Data
;
90 return SmmFtwFunctionHeader
->ReturnStatus
;
95 Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.
97 @param[in] FvbHandle The handle of FVB protocol that provides services.
98 @param[out] FvbBaseAddress The base address of the FVB attached with FvbHandle.
99 @param[out] FvbAttributes The attributes of the FVB attached with FvbHandle.
101 @retval EFI_SUCCESS The function completed successfully.
102 @retval Others The function could not complete successfully.
107 IN EFI_HANDLE FvbHandle
,
108 OUT EFI_PHYSICAL_ADDRESS
*FvbBaseAddress
,
109 OUT EFI_FVB_ATTRIBUTES_2
*FvbAttributes
113 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
115 Status
= gBS
->HandleProtocol (FvbHandle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**) &Fvb
);
116 if (EFI_ERROR (Status
)) {
120 Status
= Fvb
->GetPhysicalAddress (Fvb
, FvbBaseAddress
);
121 if (EFI_ERROR (Status
)) {
125 Status
= Fvb
->GetAttributes (Fvb
, FvbAttributes
);
131 Get the size of the largest block that can be updated in a fault-tolerant manner.
133 @param[in] This Indicates a pointer to the calling context.
134 @param[out] BlockSize A pointer to a caller-allocated UINTN that is
135 updated to indicate the size of the largest block
138 @retval EFI_SUCCESS The function completed successfully.
139 @retval EFI_ABORTED The function could not complete successfully.
145 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
151 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
152 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmFtwBlockSizeHeader
;
155 // Initialize the communicate buffer.
157 PayloadSize
= sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
);
158 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwBlockSizeHeader
, PayloadSize
, FTW_FUNCTION_GET_MAX_BLOCK_SIZE
);
163 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
168 *BlockSize
= SmmFtwBlockSizeHeader
->BlockSize
;
169 FreePool (SmmCommunicateHeader
);
176 Allocates space for the protocol to maintain information about writes.
177 Since writes must be completed in a fault-tolerant manner and multiple
178 writes require more resources to be successful, this function
179 enables the protocol to ensure that enough space exists to track
180 information about upcoming writes.
182 @param[in] This A pointer to the calling context.
183 @param[in] CallerId The GUID identifying the write.
184 @param[in] PrivateDataSize The size of the caller's private data that must be
185 recorded for each write.
186 @param[in] NumberOfWrites The number of fault tolerant block writes that will
189 @retval EFI_SUCCESS The function completed successfully
190 @retval EFI_ABORTED The function could not complete successfully.
191 @retval EFI_ACCESS_DENIED Not all allocated writes have been completed. All
192 writes must be completed or aborted before another
193 fault tolerant write can occur.
199 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
200 IN EFI_GUID
*CallerId
,
201 IN UINTN PrivateDataSize
,
202 IN UINTN NumberOfWrites
207 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
208 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
211 // Initialize the communicate buffer.
213 PayloadSize
= sizeof (SMM_FTW_ALLOCATE_HEADER
);
214 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwAllocateHeader
, PayloadSize
, FTW_FUNCTION_ALLOCATE
);
215 CopyGuid (&SmmFtwAllocateHeader
->CallerId
, CallerId
);
216 SmmFtwAllocateHeader
->PrivateDataSize
= PrivateDataSize
;
217 SmmFtwAllocateHeader
->NumberOfWrites
= NumberOfWrites
;
222 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
223 if (!EFI_ERROR( Status
)) {
224 mPrivateDataSize
= PrivateDataSize
;
227 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_SMM_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
;
286 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwWriteHeader
, PayloadSize
, FTW_FUNCTION_WRITE
);
289 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
290 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
292 Status
= ConvertFvbHandle (FvBlockHandle
, &SmmFtwWriteHeader
->FvbBaseAddress
, &SmmFtwWriteHeader
->FvbAttributes
);
293 if (EFI_ERROR (Status
)) {
294 FreePool (SmmCommunicateHeader
);
298 SmmFtwWriteHeader
->Lba
= Lba
;
299 SmmFtwWriteHeader
->Offset
= Offset
;
300 SmmFtwWriteHeader
->Length
= Length
;
301 CopyMem (SmmFtwWriteHeader
->Data
, Buffer
, Length
);
302 if (PrivateData
== NULL
) {
303 SmmFtwWriteHeader
->PrivateDataSize
= 0;
305 SmmFtwWriteHeader
->PrivateDataSize
= mPrivateDataSize
;
306 CopyMem (&SmmFtwWriteHeader
->Data
[Length
], PrivateData
, mPrivateDataSize
);
312 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
313 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_SMM_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
);
368 Aborts all previously allocated writes.
370 @param[in] This The calling context.
372 @retval EFI_SUCCESS The function completed successfully.
373 @retval EFI_ABORTED The function could not complete successfully.
374 @retval EFI_NOT_FOUND No allocated writes exist.
380 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
384 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
387 // Initialize the communicate buffer.
389 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, NULL
, 0, FTW_FUNCTION_ABORT
);
394 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, 0);
396 FreePool (SmmCommunicateHeader
);
402 Starts a target block update. This function records information about the write
403 in fault-tolerant storage and completes the write in a recoverable
404 manner, ensuring at all times that either the original contents or
405 the modified contents are available.
407 @param[in] This Indicates a pointer to the calling context.
408 @param[out] CallerId The GUID identifying the last write.
409 @param[out] Lba The logical block address of the last write.
410 @param[out] Offset The offset within the block of the last write.
411 @param[out] Length The length of the last write.
412 @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On
413 output, the size of the private data stored for
415 @param[out] PrivateData A pointer to a buffer. The function will copy
416 PrivateDataSize bytes from the private data stored
418 @param[out] Complete A Boolean value with TRUE indicating that the write
421 @retval EFI_SUCCESS The function completed successfully.
422 @retval EFI_ABORTED The function could not complete successfully.
423 @retval EFI_NOT_FOUND No allocated writes exist.
429 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
430 OUT EFI_GUID
*CallerId
,
434 IN OUT UINTN
*PrivateDataSize
,
435 OUT VOID
*PrivateData
,
436 OUT BOOLEAN
*Complete
441 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
442 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
445 // Initialize the communicate buffer.
447 PayloadSize
= OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
) + *PrivateDataSize
;
448 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwGetLastWriteHeader
, PayloadSize
, FTW_FUNCTION_GET_LAST_WRITE
);
449 SmmFtwGetLastWriteHeader
->PrivateDataSize
= *PrivateDataSize
;
454 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
459 *PrivateDataSize
= SmmFtwGetLastWriteHeader
->PrivateDataSize
;
460 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
461 *Lba
= SmmFtwGetLastWriteHeader
->Lba
;
462 *Offset
= SmmFtwGetLastWriteHeader
->Offset
;
463 *Length
= SmmFtwGetLastWriteHeader
->Length
;
464 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
465 CopyGuid (CallerId
, &SmmFtwGetLastWriteHeader
->CallerId
);
466 if (Status
== EFI_SUCCESS
) {
467 CopyMem (PrivateData
, SmmFtwGetLastWriteHeader
->Data
, *PrivateDataSize
);
469 } else if (Status
== EFI_NOT_FOUND
) {
470 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
473 FreePool (SmmCommunicateHeader
);
478 SMM Fault Tolerant Write Protocol notification event handler.
480 Install Fault Tolerant Write Protocol.
482 @param[in] Event Event whose notification function is being invoked.
483 @param[in] Context Pointer to the notification function's context.
493 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
496 // Just return to avoid install SMM FaultTolerantWriteProtocol again
497 // if Fault Tolerant Write protocol had been installed.
499 Status
= gBS
->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid
, NULL
, (VOID
**)&FtwProtocol
);
500 if (!EFI_ERROR (Status
)) {
504 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
505 ASSERT_EFI_ERROR (Status
);
508 // Install protocol interface
510 Status
= gBS
->InstallProtocolInterface (
512 &gEfiFaultTolerantWriteProtocolGuid
,
513 EFI_NATIVE_INTERFACE
,
514 &mFaultTolerantWriteDriver
516 ASSERT_EFI_ERROR (Status
);
518 Status
= gBS
->CloseEvent (Event
);
519 ASSERT_EFI_ERROR (Status
);
524 The driver entry point for Fault Tolerant Write driver.
526 The function does the necessary initialization work.
528 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
529 @param[in] SystemTable A pointer to the EFI system table.
531 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
536 FaultTolerantWriteSmmInitialize (
537 IN EFI_HANDLE ImageHandle
,
538 IN EFI_SYSTEM_TABLE
*SystemTable
541 VOID
*SmmFtwRegistration
;
544 // Smm FTW driver is ready
546 EfiCreateProtocolNotifyEvent (
547 &gEfiSmmFaultTolerantWriteProtocolGuid
,