3 Implement the Fault Tolerant Write (FTW) protocol based on SMM FTW
6 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved. <BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "FaultTolerantWriteSmmDxe.h"
19 EFI_HANDLE mHandle
= NULL
;
20 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
21 UINTN mPrivateDataSize
= 0;
23 EFI_FAULT_TOLERANT_WRITE_PROTOCOL mFaultTolerantWriteDriver
= {
33 Initialize the communicate buffer using DataSize and Function number.
35 @param[out] CommunicateBuffer The communicate buffer. Caller should free it after use.
36 @param[out] DataPtr Points to the data in the communicate buffer. Caller should not free it.
37 @param[in] DataSize The payload size.
38 @param[in] Function The function number used to initialize the communicate header.
42 InitCommunicateBuffer (
43 OUT VOID
**CommunicateBuffer
,
49 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
50 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
53 // The whole buffer size: SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE + DataSize.
55 SmmCommunicateHeader
= AllocateZeroPool (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
);
56 ASSERT (SmmCommunicateHeader
!= NULL
);
59 // Prepare data buffer.
61 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmFaultTolerantWriteProtocolGuid
);
62 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
;
64 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*) SmmCommunicateHeader
->Data
;
65 SmmFtwFunctionHeader
->Function
= Function
;
67 *CommunicateBuffer
= SmmCommunicateHeader
;
68 if (DataPtr
!= NULL
) {
69 *DataPtr
= SmmFtwFunctionHeader
->Data
;
75 Send the data in communicate buffer to SMI handler and get response.
77 @param[in, out] SmmCommunicateHeader The communicate buffer.
78 @param[in] DataSize The payload size.
82 SendCommunicateBuffer (
83 IN OUT EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
,
89 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
91 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_FTW_COMMUNICATE_HEADER_SIZE
;
92 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, SmmCommunicateHeader
, &CommSize
);
93 ASSERT_EFI_ERROR (Status
);
95 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*) SmmCommunicateHeader
->Data
;
96 return SmmFtwFunctionHeader
->ReturnStatus
;
101 Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.
103 @param[in] FvbHandle The handle of FVB protocol that provides services.
104 @param[out] FvbBaseAddress The base address of the FVB attached with FvbHandle.
105 @param[out] FvbAttributes The attributes of the FVB attached with FvbHandle.
107 @retval EFI_SUCCESS The function completed successfully.
108 @retval Others The function could not complete successfully.
113 IN EFI_HANDLE FvbHandle
,
114 OUT EFI_PHYSICAL_ADDRESS
*FvbBaseAddress
,
115 OUT EFI_FVB_ATTRIBUTES_2
*FvbAttributes
119 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
121 Status
= gBS
->HandleProtocol (FvbHandle
, &gEfiFirmwareVolumeBlockProtocolGuid
, (VOID
**) &Fvb
);
122 if (EFI_ERROR (Status
)) {
126 Status
= Fvb
->GetPhysicalAddress (Fvb
, FvbBaseAddress
);
127 if (EFI_ERROR (Status
)) {
131 Status
= Fvb
->GetAttributes (Fvb
, FvbAttributes
);
137 Get the size of the largest block that can be updated in a fault-tolerant manner.
139 @param[in] This Indicates a pointer to the calling context.
140 @param[out] BlockSize A pointer to a caller-allocated UINTN that is
141 updated to indicate the size of the largest block
144 @retval EFI_SUCCESS The function completed successfully.
145 @retval EFI_ABORTED The function could not complete successfully.
151 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
157 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
158 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmFtwBlockSizeHeader
;
161 // Initialize the communicate buffer.
163 PayloadSize
= sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
);
164 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwBlockSizeHeader
, PayloadSize
, FTW_FUNCTION_GET_MAX_BLOCK_SIZE
);
169 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
174 *BlockSize
= SmmFtwBlockSizeHeader
->BlockSize
;
175 FreePool (SmmCommunicateHeader
);
182 Allocates space for the protocol to maintain information about writes.
183 Since writes must be completed in a fault-tolerant manner and multiple
184 writes require more resources to be successful, this function
185 enables the protocol to ensure that enough space exists to track
186 information about upcoming writes.
188 @param[in] This A pointer to the calling context.
189 @param[in] CallerId The GUID identifying the write.
190 @param[in] PrivateDataSize The size of the caller's private data that must be
191 recorded for each write.
192 @param[in] NumberOfWrites The number of fault tolerant block writes that will
195 @retval EFI_SUCCESS The function completed successfully
196 @retval EFI_ABORTED The function could not complete successfully.
197 @retval EFI_ACCESS_DENIED Not all allocated writes have been completed. All
198 writes must be completed or aborted before another
199 fault tolerant write can occur.
205 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
206 IN EFI_GUID
*CallerId
,
207 IN UINTN PrivateDataSize
,
208 IN UINTN NumberOfWrites
213 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
214 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
217 // Initialize the communicate buffer.
219 PayloadSize
= sizeof (SMM_FTW_ALLOCATE_HEADER
);
220 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwAllocateHeader
, PayloadSize
, FTW_FUNCTION_ALLOCATE
);
221 CopyGuid (&SmmFtwAllocateHeader
->CallerId
, CallerId
);
222 SmmFtwAllocateHeader
->PrivateDataSize
= PrivateDataSize
;
223 SmmFtwAllocateHeader
->NumberOfWrites
= NumberOfWrites
;
228 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
229 if (!EFI_ERROR( Status
)) {
230 mPrivateDataSize
= PrivateDataSize
;
233 FreePool (SmmCommunicateHeader
);
239 Starts a target block update. This records information about the write
240 in fault tolerant storage, and will complete the write in a recoverable
241 manner, ensuring at all times that either the original contents or
242 the modified contents are available.
244 @param[in] This The calling context.
245 @param[in] Lba The logical block address of the target block.
246 @param[in] Offset The offset within the target block to place the
248 @param[in] Length The number of bytes to write to the target block.
249 @param[in] PrivateData A pointer to private data that the caller requires
250 to complete any pending writes in the event of a
252 @param[in] FvBlockHandle The handle of FVB protocol that provides services
253 for reading, writing, and erasing the target block.
254 @param[in] Buffer The data to write.
256 @retval EFI_SUCCESS The function completed successfully.
257 @retval EFI_ABORTED The function could not complete successfully.
258 @retval EFI_BAD_BUFFER_SIZE The write would span a block boundary, which is not
260 @retval EFI_ACCESS_DENIED No writes have been allocated.
261 @retval EFI_NOT_READY The last write has not been completed. Restart()
262 must be called to complete it.
268 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
272 IN VOID
*PrivateData
,
273 IN EFI_HANDLE FvBlockHandle
,
279 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
280 SMM_FTW_WRITE_HEADER
*SmmFtwWriteHeader
;
283 // Initialize the communicate buffer.
285 PayloadSize
= OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
;
286 if (PrivateData
!= NULL
) {
288 // The private data buffer size should be the same one in FtwAllocate API.
290 PayloadSize
+= mPrivateDataSize
;
292 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwWriteHeader
, PayloadSize
, FTW_FUNCTION_WRITE
);
295 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
296 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
298 Status
= ConvertFvbHandle (FvBlockHandle
, &SmmFtwWriteHeader
->FvbBaseAddress
, &SmmFtwWriteHeader
->FvbAttributes
);
299 if (EFI_ERROR (Status
)) {
300 FreePool (SmmCommunicateHeader
);
304 SmmFtwWriteHeader
->Lba
= Lba
;
305 SmmFtwWriteHeader
->Offset
= Offset
;
306 SmmFtwWriteHeader
->Length
= Length
;
307 CopyMem (SmmFtwWriteHeader
->Data
, Buffer
, Length
);
308 if (PrivateData
== NULL
) {
309 SmmFtwWriteHeader
->PrivateDataSize
= 0;
311 SmmFtwWriteHeader
->PrivateDataSize
= mPrivateDataSize
;
312 CopyMem (&SmmFtwWriteHeader
->Data
[Length
], PrivateData
, mPrivateDataSize
);
318 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
319 FreePool (SmmCommunicateHeader
);
325 Restarts a previously interrupted write. The caller must provide the
326 block protocol needed to complete the interrupted write.
328 @param[in] This The calling context.
329 @param[in] FvBlockHandle The handle of FVB protocol that provides services.
331 @retval EFI_SUCCESS The function completed successfully.
332 @retval EFI_ABORTED The function could not complete successfully.
333 @retval EFI_ACCESS_DENIED No pending writes exist.
339 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
340 IN EFI_HANDLE FvBlockHandle
345 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
346 SMM_FTW_RESTART_HEADER
*SmmFtwRestartHeader
;
349 // Initialize the communicate buffer.
351 PayloadSize
= sizeof (SMM_FTW_RESTART_HEADER
);
352 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwRestartHeader
, PayloadSize
, FTW_FUNCTION_RESTART
);
355 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
356 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
358 Status
= ConvertFvbHandle (FvBlockHandle
, &SmmFtwRestartHeader
->FvbBaseAddress
, &SmmFtwRestartHeader
->FvbAttributes
);
359 if (EFI_ERROR (Status
)) {
360 FreePool (SmmCommunicateHeader
);
367 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
368 FreePool (SmmCommunicateHeader
);
374 Aborts all previously allocated writes.
376 @param[in] This The calling context.
378 @retval EFI_SUCCESS The function completed successfully.
379 @retval EFI_ABORTED The function could not complete successfully.
380 @retval EFI_NOT_FOUND No allocated writes exist.
386 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
390 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
393 // Initialize the communicate buffer.
395 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, NULL
, 0, FTW_FUNCTION_ABORT
);
400 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, 0);
402 FreePool (SmmCommunicateHeader
);
408 Starts a target block update. This function records information about the write
409 in fault-tolerant storage and completes the write in a recoverable
410 manner, ensuring at all times that either the original contents or
411 the modified contents are available.
413 @param[in] This Indicates a pointer to the calling context.
414 @param[out] CallerId The GUID identifying the last write.
415 @param[out] Lba The logical block address of the last write.
416 @param[out] Offset The offset within the block of the last write.
417 @param[out] Length The length of the last write.
418 @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On
419 output, the size of the private data stored for
421 @param[out] PrivateData A pointer to a buffer. The function will copy
422 PrivateDataSize bytes from the private data stored
424 @param[out] Complete A Boolean value with TRUE indicating that the write
427 @retval EFI_SUCCESS The function completed successfully.
428 @retval EFI_ABORTED The function could not complete successfully.
429 @retval EFI_NOT_FOUND No allocated writes exist.
435 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*This
,
436 OUT EFI_GUID
*CallerId
,
440 IN OUT UINTN
*PrivateDataSize
,
441 OUT VOID
*PrivateData
,
442 OUT BOOLEAN
*Complete
447 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
448 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
451 // Initialize the communicate buffer.
453 PayloadSize
= OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
) + *PrivateDataSize
;
454 InitCommunicateBuffer ((VOID
**)&SmmCommunicateHeader
, (VOID
**)&SmmFtwGetLastWriteHeader
, PayloadSize
, FTW_FUNCTION_GET_LAST_WRITE
);
455 SmmFtwGetLastWriteHeader
->PrivateDataSize
= *PrivateDataSize
;
460 Status
= SendCommunicateBuffer (SmmCommunicateHeader
, PayloadSize
);
465 *PrivateDataSize
= SmmFtwGetLastWriteHeader
->PrivateDataSize
;
466 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
467 *Lba
= SmmFtwGetLastWriteHeader
->Lba
;
468 *Offset
= SmmFtwGetLastWriteHeader
->Offset
;
469 *Length
= SmmFtwGetLastWriteHeader
->Length
;
470 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
471 CopyGuid (CallerId
, &SmmFtwGetLastWriteHeader
->CallerId
);
472 if (Status
== EFI_SUCCESS
) {
473 CopyMem (PrivateData
, SmmFtwGetLastWriteHeader
->Data
, *PrivateDataSize
);
475 } else if (Status
== EFI_NOT_FOUND
) {
476 *Complete
= SmmFtwGetLastWriteHeader
->Complete
;
479 FreePool (SmmCommunicateHeader
);
484 SMM Fault Tolerant Write Protocol notification event handler.
486 Install Fault Tolerant Write Protocol.
488 @param[in] Event Event whose notification function is being invoked.
489 @param[in] Context Pointer to the notification function's context.
499 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
502 // Just return to avoid install SMM FaultTolerantWriteProtocol again
503 // if Fault Tolerant Write protocol had been installed.
505 Status
= gBS
->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid
, NULL
, (VOID
**)&FtwProtocol
);
506 if (!EFI_ERROR (Status
)) {
510 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
511 ASSERT_EFI_ERROR (Status
);
514 // Install protocol interface
516 Status
= gBS
->InstallProtocolInterface (
518 &gEfiFaultTolerantWriteProtocolGuid
,
519 EFI_NATIVE_INTERFACE
,
520 &mFaultTolerantWriteDriver
522 ASSERT_EFI_ERROR (Status
);
524 Status
= gBS
->CloseEvent (Event
);
525 ASSERT_EFI_ERROR (Status
);
530 The driver entry point for Fault Tolerant Write driver.
532 The function does the necessary initialization work.
534 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
535 @param[in] SystemTable A pointer to the EFI system table.
537 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
542 FaultTolerantWriteSmmInitialize (
543 IN EFI_HANDLE ImageHandle
,
544 IN EFI_SYSTEM_TABLE
*SystemTable
547 VOID
*SmmFtwRegistration
;
550 // Smm FTW driver is ready
552 EfiCreateProtocolNotifyEvent (
553 &gEfiSmmFaultTolerantWriteProtocolGuid
,