3 This is a simple fault tolerant write driver that is intended to use in the SMM environment.
5 This boot service protocol only provides fault tolerant write capability for
6 block devices. The protocol has internal non-volatile intermediate storage
7 of the data and private information. It should be able to recover
8 automatically from a critical fault, such as power failure.
10 The implementation uses an FTW (Fault Tolerant Write) Work Space.
11 This work space is a memory copy of the work space on the Working Block,
12 the size of the work space is the FTW_WORK_SPACE_SIZE bytes.
14 The work space stores each write record as EFI_FTW_RECORD structure.
15 The spare block stores the write buffer before write to the target block.
17 The write record has three states to specify the different phase of write operation.
18 1) WRITE_ALLOCATED is that the record is allocated in write space.
19 The information of write operation is stored in write record structure.
20 2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.
21 3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.
23 This driver operates the data as the whole size of spare block.
24 It first read the SpareAreaLength data from the target block into the spare memory buffer.
25 Then copy the write buffer data into the spare memory buffer.
26 Then write the spare memory buffer into the spare block.
27 Final copy the data from the spare block to the target block.
29 To make this drive work well, the following conditions must be satisfied:
30 1. The write NumBytes data must be fit within Spare area.
31 Offset + NumBytes <= SpareAreaLength
32 2. The whole flash range has the same block size.
33 3. Working block is an area which contains working space in its last block and has the same size as spare block.
34 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
35 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.
36 6. Any write data area (SpareAreaLength Area) which the data will be written into must be
37 in the single one Firmware Volume Block range which FVB protocol is produced on.
38 7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.
39 The spare area must be enough large to store the write data before write them into the target range.
40 If one of them is not satisfied, FtwWrite may fail.
41 Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.
43 Caution: This module requires additional review when modified.
44 This driver need to make sure the CommBuffer is not in the SMRAM range.
46 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
47 This program and the accompanying materials
48 are licensed and made available under the terms and conditions of the BSD License
49 which accompanies this distribution. The full text of the license may be found at
50 http://opensource.org/licenses/bsd-license.php
52 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
53 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
58 #include <Library/SmmServicesTableLib.h>
59 #include <Library/SmmMemLib.h>
60 #include <Library/BaseLib.h>
61 #include <Protocol/SmmSwapAddressRange.h>
62 #include "FaultTolerantWrite.h"
63 #include "FaultTolerantWriteSmmCommon.h"
64 #include <Protocol/SmmEndOfDxe.h>
66 EFI_EVENT mFvbRegistration
= NULL
;
67 EFI_FTW_DEVICE
*mFtwDevice
= NULL
;
70 /// The flag to indicate whether the platform has left the DXE phase of execution.
72 BOOLEAN mEndOfDxe
= FALSE
;
75 Retrieve the SMM FVB protocol interface by HANDLE.
77 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
78 reading, writing, and erasing the target block.
79 @param[out] FvBlock The interface of SMM FVB protocol
81 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
82 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
83 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
88 IN EFI_HANDLE FvBlockHandle
,
89 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
93 // To get the SMM FVB protocol interface on the handle
95 return gSmst
->SmmHandleProtocol (
97 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
103 Retrieve the SMM Swap Address Range protocol interface.
105 @param[out] SarProtocol The interface of SMM SAR protocol
107 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
108 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
109 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
114 OUT VOID
**SarProtocol
120 // Locate Smm Swap Address Range protocol
122 Status
= gSmst
->SmmLocateProtocol (
123 &gEfiSmmSwapAddressRangeProtocolGuid
,
131 Function returns an array of handles that support the SMM FVB protocol
132 in a buffer allocated from pool.
134 @param[out] NumberHandles The number of handles returned in Buffer.
135 @param[out] Buffer A pointer to the buffer to return the requested
136 array of handles that support SMM FVB protocol.
138 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
139 handles in Buffer was returned in NumberHandles.
140 @retval EFI_NOT_FOUND No SMM FVB handle was found.
141 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
142 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
146 GetFvbCountAndBuffer (
147 OUT UINTN
*NumberHandles
,
148 OUT EFI_HANDLE
**Buffer
154 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
155 return EFI_INVALID_PARAMETER
;
161 Status
= gSmst
->SmmLocateHandle (
163 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
168 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
169 return EFI_NOT_FOUND
;
172 *Buffer
= AllocatePool (BufferSize
);
173 if (*Buffer
== NULL
) {
174 return EFI_OUT_OF_RESOURCES
;
177 Status
= gSmst
->SmmLocateHandle (
179 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
185 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
186 if (EFI_ERROR(Status
)) {
197 Get the handle of the SMM FVB protocol by the FVB base address and attributes.
199 @param[in] Address The base address of SMM FVB protocol.
200 @param[in] Attributes The attributes of the SMM FVB protocol.
201 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.
203 @retval EFI_SUCCESS The FVB handle is found.
204 @retval EFI_ABORTED The FVB protocol is not found.
208 GetFvbByAddressAndAttribute (
209 IN EFI_PHYSICAL_ADDRESS Address
,
210 IN EFI_FVB_ATTRIBUTES_2 Attributes
,
211 OUT EFI_HANDLE
*SmmFvbHandle
215 EFI_HANDLE
*HandleBuffer
;
218 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
219 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
220 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
225 // Locate all handles of SMM Fvb protocol.
227 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
228 if (EFI_ERROR (Status
)) {
233 // Find the proper SMM Fvb handle by the address and attributes.
235 for (Index
= 0; Index
< HandleCount
; Index
++) {
236 Status
= FtwGetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
237 if (EFI_ERROR (Status
)) {
241 // Compare the address.
243 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
244 if (EFI_ERROR (Status
)) {
247 if (Address
!= FvbBaseAddress
) {
252 // Compare the attribute.
254 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
255 if (EFI_ERROR (Status
)) {
258 if (Attributes
!= FvbAttributes
) {
263 // Found the proper FVB handle.
265 *SmmFvbHandle
= HandleBuffer
[Index
];
266 FreePool (HandleBuffer
);
270 FreePool (HandleBuffer
);
275 Communication service SMI Handler entry.
277 This SMI handler provides services for the fault tolerant write wrapper driver.
279 Caution: This function requires additional review when modified.
280 This driver need to make sure the CommBuffer is not in the SMRAM range.
281 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +
282 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.
284 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
285 @param[in] RegisterContext Points to an optional handler context which was specified when the
286 handler was registered.
287 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed
288 from a non-SMM environment into an SMM environment.
289 @param[in, out] CommBufferSize The size of the CommBuffer.
291 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
292 should still be called.
293 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
295 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
297 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
302 SmmFaultTolerantWriteHandler (
303 IN EFI_HANDLE DispatchHandle
,
304 IN CONST VOID
*RegisterContext
,
305 IN OUT VOID
*CommBuffer
,
306 IN OUT UINTN
*CommBufferSize
310 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
311 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmGetMaxBlockSizeHeader
;
312 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
313 SMM_FTW_WRITE_HEADER
*SmmFtwWriteHeader
;
314 SMM_FTW_RESTART_HEADER
*SmmFtwRestartHeader
;
315 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
317 EFI_HANDLE SmmFvbHandle
;
319 UINTN CommBufferPayloadSize
;
320 UINTN PrivateDataSize
;
322 UINTN TempCommBufferSize
;
325 // If input is invalid, stop processing this SMI
327 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
331 TempCommBufferSize
= *CommBufferSize
;
333 if (TempCommBufferSize
< SMM_FTW_COMMUNICATE_HEADER_SIZE
) {
334 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: SMM communication buffer size invalid!\n"));
337 CommBufferPayloadSize
= TempCommBufferSize
- SMM_FTW_COMMUNICATE_HEADER_SIZE
;
339 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
340 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: SMM communication buffer in SMRAM or overflow!\n"));
344 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*)CommBuffer
;
348 // It will be not safe to expose the operations after End Of Dxe.
350 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: Not safe to do the operation: %x after End Of Dxe, so access denied!\n", SmmFtwFunctionHeader
->Function
));
351 SmmFtwFunctionHeader
->ReturnStatus
= EFI_ACCESS_DENIED
;
355 switch (SmmFtwFunctionHeader
->Function
) {
356 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE
:
357 if (CommBufferPayloadSize
< sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
)) {
358 DEBUG ((EFI_D_ERROR
, "GetMaxBlockSize: SMM communication buffer size invalid!\n"));
361 SmmGetMaxBlockSizeHeader
= (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*) SmmFtwFunctionHeader
->Data
;
363 Status
= FtwGetMaxBlockSize (
364 &mFtwDevice
->FtwInstance
,
365 &SmmGetMaxBlockSizeHeader
->BlockSize
369 case FTW_FUNCTION_ALLOCATE
:
370 if (CommBufferPayloadSize
< sizeof (SMM_FTW_ALLOCATE_HEADER
)) {
371 DEBUG ((EFI_D_ERROR
, "Allocate: SMM communication buffer size invalid!\n"));
374 SmmFtwAllocateHeader
= (SMM_FTW_ALLOCATE_HEADER
*) SmmFtwFunctionHeader
->Data
;
375 Status
= FtwAllocate (
376 &mFtwDevice
->FtwInstance
,
377 &SmmFtwAllocateHeader
->CallerId
,
378 SmmFtwAllocateHeader
->PrivateDataSize
,
379 SmmFtwAllocateHeader
->NumberOfWrites
383 case FTW_FUNCTION_WRITE
:
384 if (CommBufferPayloadSize
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
)) {
385 DEBUG ((EFI_D_ERROR
, "Write: SMM communication buffer size invalid!\n"));
388 SmmFtwWriteHeader
= (SMM_FTW_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
389 Length
= SmmFtwWriteHeader
->Length
;
390 PrivateDataSize
= SmmFtwWriteHeader
->PrivateDataSize
;
391 if (((UINTN
)(~0) - Length
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
)) ||
392 ((UINTN
)(~0) - PrivateDataSize
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
)) {
394 // Prevent InfoSize overflow
396 Status
= EFI_ACCESS_DENIED
;
399 InfoSize
= OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
+ PrivateDataSize
;
402 // SMRAM range check already covered before
404 if (InfoSize
> CommBufferPayloadSize
) {
405 DEBUG ((EFI_D_ERROR
, "Write: Data size exceed communication buffer size limit!\n"));
406 Status
= EFI_ACCESS_DENIED
;
410 if (PrivateDataSize
== 0) {
413 PrivateData
= (VOID
*)&SmmFtwWriteHeader
->Data
[Length
];
415 Status
= GetFvbByAddressAndAttribute (
416 SmmFtwWriteHeader
->FvbBaseAddress
,
417 SmmFtwWriteHeader
->FvbAttributes
,
420 if (!EFI_ERROR (Status
)) {
422 // The SpeculationBarrier() call here is to ensure the previous
423 // range/content checks for the CommBuffer have been completed before
424 // calling into FtwWrite().
426 SpeculationBarrier ();
428 &mFtwDevice
->FtwInstance
,
429 SmmFtwWriteHeader
->Lba
,
430 SmmFtwWriteHeader
->Offset
,
434 SmmFtwWriteHeader
->Data
439 case FTW_FUNCTION_RESTART
:
440 if (CommBufferPayloadSize
< sizeof (SMM_FTW_RESTART_HEADER
)) {
441 DEBUG ((EFI_D_ERROR
, "Restart: SMM communication buffer size invalid!\n"));
444 SmmFtwRestartHeader
= (SMM_FTW_RESTART_HEADER
*) SmmFtwFunctionHeader
->Data
;
445 Status
= GetFvbByAddressAndAttribute (
446 SmmFtwRestartHeader
->FvbBaseAddress
,
447 SmmFtwRestartHeader
->FvbAttributes
,
450 if (!EFI_ERROR (Status
)) {
451 Status
= FtwRestart (&mFtwDevice
->FtwInstance
, SmmFvbHandle
);
455 case FTW_FUNCTION_ABORT
:
456 Status
= FtwAbort (&mFtwDevice
->FtwInstance
);
459 case FTW_FUNCTION_GET_LAST_WRITE
:
460 if (CommBufferPayloadSize
< OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
)) {
461 DEBUG ((EFI_D_ERROR
, "GetLastWrite: SMM communication buffer size invalid!\n"));
464 SmmFtwGetLastWriteHeader
= (SMM_FTW_GET_LAST_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
465 PrivateDataSize
= SmmFtwGetLastWriteHeader
->PrivateDataSize
;
466 if ((UINTN
)(~0) - PrivateDataSize
< OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
)){
468 // Prevent InfoSize overflow
470 Status
= EFI_ACCESS_DENIED
;
473 InfoSize
= OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
) + PrivateDataSize
;
476 // SMRAM range check already covered before
478 if (InfoSize
> CommBufferPayloadSize
) {
479 DEBUG ((EFI_D_ERROR
, "Data size exceed communication buffer size limit!\n"));
480 Status
= EFI_ACCESS_DENIED
;
484 Status
= FtwGetLastWrite (
485 &mFtwDevice
->FtwInstance
,
486 &SmmFtwGetLastWriteHeader
->CallerId
,
487 &SmmFtwGetLastWriteHeader
->Lba
,
488 &SmmFtwGetLastWriteHeader
->Offset
,
489 &SmmFtwGetLastWriteHeader
->Length
,
491 (VOID
*)SmmFtwGetLastWriteHeader
->Data
,
492 &SmmFtwGetLastWriteHeader
->Complete
494 SmmFtwGetLastWriteHeader
->PrivateDataSize
= PrivateDataSize
;
498 Status
= EFI_UNSUPPORTED
;
501 SmmFtwFunctionHeader
->ReturnStatus
= Status
;
508 SMM Firmware Volume Block Protocol notification event handler.
510 @param[in] Protocol Points to the protocol's unique identifier
511 @param[in] Interface Points to the interface instance
512 @param[in] Handle The handle on which the interface was installed
514 @retval EFI_SUCCESS SmmEventCallback runs successfully
519 FvbNotificationEvent (
520 IN CONST EFI_GUID
*Protocol
,
526 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
527 EFI_HANDLE SmmFtwHandle
;
528 EFI_HANDLE FtwHandle
;
531 // Just return to avoid install SMM FaultTolerantWriteProtocol again
532 // if SMM Fault Tolerant Write protocol had been installed.
534 Status
= gSmst
->SmmLocateProtocol (
535 &gEfiSmmFaultTolerantWriteProtocolGuid
,
537 (VOID
**) &FtwProtocol
539 if (!EFI_ERROR (Status
)) {
544 // Found proper FVB protocol and initialize FtwDevice for protocol installation
546 Status
= InitFtwProtocol (mFtwDevice
);
547 if (EFI_ERROR(Status
)) {
552 // Install protocol interface
554 Status
= gSmst
->SmmInstallProtocolInterface (
556 &gEfiSmmFaultTolerantWriteProtocolGuid
,
557 EFI_NATIVE_INTERFACE
,
558 &mFtwDevice
->FtwInstance
560 ASSERT_EFI_ERROR (Status
);
563 /// Register SMM FTW SMI handler
565 Status
= gSmst
->SmiHandlerRegister (SmmFaultTolerantWriteHandler
, &gEfiSmmFaultTolerantWriteProtocolGuid
, &SmmFtwHandle
);
566 ASSERT_EFI_ERROR (Status
);
569 // Notify the Ftw wrapper driver SMM Ftw is ready
572 Status
= gBS
->InstallProtocolInterface (
574 &gEfiSmmFaultTolerantWriteProtocolGuid
,
575 EFI_NATIVE_INTERFACE
,
578 ASSERT_EFI_ERROR (Status
);
584 SMM END_OF_DXE protocol notification event handler.
586 @param Protocol Points to the protocol's unique identifier
587 @param Interface Points to the interface instance
588 @param Handle The handle on which the interface was installed
590 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
595 SmmEndOfDxeCallback (
596 IN CONST EFI_GUID
*Protocol
,
606 This function is the entry point of the Fault Tolerant Write driver.
608 @param[in] ImageHandle A handle for the image that is initializing this driver
609 @param[in] SystemTable A pointer to the EFI system table
611 @retval EFI_SUCCESS The initialization finished successfully.
612 @retval EFI_OUT_OF_RESOURCES Allocate memory error
613 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
618 SmmFaultTolerantWriteInitialize (
619 IN EFI_HANDLE ImageHandle
,
620 IN EFI_SYSTEM_TABLE
*SystemTable
624 VOID
*SmmEndOfDxeRegistration
;
627 // Allocate private data structure for SMM FTW protocol and do some initialization
629 Status
= InitFtwDevice (&mFtwDevice
);
630 if (EFI_ERROR(Status
)) {
635 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
637 Status
= gSmst
->SmmRegisterProtocolNotify (
638 &gEfiSmmEndOfDxeProtocolGuid
,
640 &SmmEndOfDxeRegistration
642 ASSERT_EFI_ERROR (Status
);
645 // Register FvbNotificationEvent () notify function.
647 Status
= gSmst
->SmmRegisterProtocolNotify (
648 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
649 FvbNotificationEvent
,
652 ASSERT_EFI_ERROR (Status
);
654 FvbNotificationEvent (NULL
, NULL
, NULL
);