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 - 2014, 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 <Protocol/SmmSwapAddressRange.h>
60 #include "FaultTolerantWrite.h"
61 #include "FaultTolerantWriteSmmCommon.h"
62 #include <Protocol/SmmAccess2.h>
63 #include <Protocol/SmmEndOfDxe.h>
65 EFI_EVENT mFvbRegistration
= NULL
;
66 EFI_FTW_DEVICE
*mFtwDevice
= NULL
;
67 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
68 UINTN mSmramRangeCount
;
71 /// The flag to indicate whether the platform has left the DXE phase of execution.
73 BOOLEAN mEndOfDxe
= FALSE
;
76 This function check if the address is in SMRAM.
78 @param Buffer the buffer address to be checked.
79 @param Length the buffer length to be checked.
81 @retval TRUE this address is in SMRAM.
82 @retval FALSE this address is NOT in SMRAM.
85 InternalIsAddressInSmram (
86 IN EFI_PHYSICAL_ADDRESS Buffer
,
92 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
93 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
94 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
103 This function check if the address refered by Buffer and Length is valid.
105 @param Buffer the buffer address to be checked.
106 @param Length the buffer length to be checked.
108 @retval TRUE this address is valid.
109 @retval FALSE this address is NOT valid.
112 InternalIsAddressValid (
117 if (Buffer
> (MAX_ADDRESS
- Length
)) {
123 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)Buffer
, (UINT64
)Length
)) {
130 Retrive the SMM FVB protocol interface by HANDLE.
132 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
133 reading, writing, and erasing the target block.
134 @param[out] FvBlock The interface of SMM FVB protocol
136 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
137 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
138 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
143 IN EFI_HANDLE FvBlockHandle
,
144 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
148 // To get the SMM FVB protocol interface on the handle
150 return gSmst
->SmmHandleProtocol (
152 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
158 Retrive the SMM Swap Address Range protocol interface.
160 @param[out] SarProtocol The interface of SMM SAR protocol
162 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
163 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
164 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
169 OUT VOID
**SarProtocol
175 // Locate Smm Swap Address Range protocol
177 Status
= gSmst
->SmmLocateProtocol (
178 &gEfiSmmSwapAddressRangeProtocolGuid
,
186 Function returns an array of handles that support the SMM FVB protocol
187 in a buffer allocated from pool.
189 @param[out] NumberHandles The number of handles returned in Buffer.
190 @param[out] Buffer A pointer to the buffer to return the requested
191 array of handles that support SMM FVB protocol.
193 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
194 handles in Buffer was returned in NumberHandles.
195 @retval EFI_NOT_FOUND No SMM FVB handle was found.
196 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
197 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
201 GetFvbCountAndBuffer (
202 OUT UINTN
*NumberHandles
,
203 OUT EFI_HANDLE
**Buffer
209 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
210 return EFI_INVALID_PARAMETER
;
216 Status
= gSmst
->SmmLocateHandle (
218 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
223 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
224 return EFI_NOT_FOUND
;
227 *Buffer
= AllocatePool (BufferSize
);
228 if (*Buffer
== NULL
) {
229 return EFI_OUT_OF_RESOURCES
;
232 Status
= gSmst
->SmmLocateHandle (
234 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
240 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
241 if (EFI_ERROR(Status
)) {
252 Get the handle of the SMM FVB protocol by the FVB base address and attributes.
254 @param[in] Address The base address of SMM FVB protocol.
255 @param[in] Attributes The attributes of the SMM FVB protocol.
256 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.
258 @retval EFI_SUCCESS The FVB handle is found.
259 @retval EFI_ABORTED The FVB protocol is not found.
263 GetFvbByAddressAndAttribute (
264 IN EFI_PHYSICAL_ADDRESS Address
,
265 IN EFI_FVB_ATTRIBUTES_2 Attributes
,
266 OUT EFI_HANDLE
*SmmFvbHandle
270 EFI_HANDLE
*HandleBuffer
;
273 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
274 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
275 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
280 // Locate all handles of SMM Fvb protocol.
282 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
283 if (EFI_ERROR (Status
)) {
288 // Find the proper SMM Fvb handle by the address and attributes.
290 for (Index
= 0; Index
< HandleCount
; Index
++) {
291 Status
= FtwGetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
292 if (EFI_ERROR (Status
)) {
296 // Compare the address.
298 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
299 if (EFI_ERROR (Status
)) {
302 if (Address
!= FvbBaseAddress
) {
307 // Compare the attribute.
309 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
310 if (EFI_ERROR (Status
)) {
313 if (Attributes
!= FvbAttributes
) {
318 // Found the proper FVB handle.
320 *SmmFvbHandle
= HandleBuffer
[Index
];
321 FreePool (HandleBuffer
);
325 FreePool (HandleBuffer
);
330 Communication service SMI Handler entry.
332 This SMI handler provides services for the fault tolerant write wrapper driver.
334 Caution: This function requires additional review when modified.
335 This driver need to make sure the CommBuffer is not in the SMRAM range.
336 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +
337 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.
339 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
340 @param[in] RegisterContext Points to an optional handler context which was specified when the
341 handler was registered.
342 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed
343 from a non-SMM environment into an SMM environment.
344 @param[in, out] CommBufferSize The size of the CommBuffer.
346 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
347 should still be called.
348 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
350 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
352 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
357 SmmFaultTolerantWriteHandler (
358 IN EFI_HANDLE DispatchHandle
,
359 IN CONST VOID
*RegisterContext
,
360 IN OUT VOID
*CommBuffer
,
361 IN OUT UINTN
*CommBufferSize
365 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
366 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmGetMaxBlockSizeHeader
;
367 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
368 SMM_FTW_WRITE_HEADER
*SmmFtwWriteHeader
;
369 SMM_FTW_RESTART_HEADER
*SmmFtwRestartHeader
;
370 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
372 EFI_HANDLE SmmFvbHandle
;
374 UINTN CommBufferPayloadSize
;
375 UINTN PrivateDataSize
;
377 UINTN TempCommBufferSize
;
380 // If input is invalid, stop processing this SMI
382 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
386 TempCommBufferSize
= *CommBufferSize
;
388 if (TempCommBufferSize
< SMM_FTW_COMMUNICATE_HEADER_SIZE
) {
389 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: SMM communication buffer size invalid!\n"));
392 CommBufferPayloadSize
= TempCommBufferSize
- SMM_FTW_COMMUNICATE_HEADER_SIZE
;
394 if (!InternalIsAddressValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
395 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: SMM communication buffer in SMRAM or overflow!\n"));
399 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*)CommBuffer
;
403 // It will be not safe to expose the operations after End Of Dxe.
405 DEBUG ((EFI_D_ERROR
, "SmmFtwHandler: Not safe to do the operation: %x after End Of Dxe, so access denied!\n", SmmFtwFunctionHeader
->Function
));
406 SmmFtwFunctionHeader
->ReturnStatus
= EFI_ACCESS_DENIED
;
410 switch (SmmFtwFunctionHeader
->Function
) {
411 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE
:
412 if (CommBufferPayloadSize
< sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
)) {
413 DEBUG ((EFI_D_ERROR
, "GetMaxBlockSize: SMM communication buffer size invalid!\n"));
416 SmmGetMaxBlockSizeHeader
= (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*) SmmFtwFunctionHeader
->Data
;
418 Status
= FtwGetMaxBlockSize (
419 &mFtwDevice
->FtwInstance
,
420 &SmmGetMaxBlockSizeHeader
->BlockSize
424 case FTW_FUNCTION_ALLOCATE
:
425 if (CommBufferPayloadSize
< sizeof (SMM_FTW_ALLOCATE_HEADER
)) {
426 DEBUG ((EFI_D_ERROR
, "Allocate: SMM communication buffer size invalid!\n"));
429 SmmFtwAllocateHeader
= (SMM_FTW_ALLOCATE_HEADER
*) SmmFtwFunctionHeader
->Data
;
430 Status
= FtwAllocate (
431 &mFtwDevice
->FtwInstance
,
432 &SmmFtwAllocateHeader
->CallerId
,
433 SmmFtwAllocateHeader
->PrivateDataSize
,
434 SmmFtwAllocateHeader
->NumberOfWrites
438 case FTW_FUNCTION_WRITE
:
439 if (CommBufferPayloadSize
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
)) {
440 DEBUG ((EFI_D_ERROR
, "Write: SMM communication buffer size invalid!\n"));
443 SmmFtwWriteHeader
= (SMM_FTW_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
444 Length
= SmmFtwWriteHeader
->Length
;
445 PrivateDataSize
= SmmFtwWriteHeader
->PrivateDataSize
;
446 if (((UINTN
)(~0) - Length
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
)) ||
447 ((UINTN
)(~0) - PrivateDataSize
< OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
)) {
449 // Prevent InfoSize overflow
451 Status
= EFI_ACCESS_DENIED
;
454 InfoSize
= OFFSET_OF (SMM_FTW_WRITE_HEADER
, Data
) + Length
+ PrivateDataSize
;
457 // SMRAM range check already covered before
459 if (InfoSize
> CommBufferPayloadSize
) {
460 DEBUG ((EFI_D_ERROR
, "Write: Data size exceed communication buffer size limit!\n"));
461 Status
= EFI_ACCESS_DENIED
;
465 if (PrivateDataSize
== 0) {
468 PrivateData
= (VOID
*)&SmmFtwWriteHeader
->Data
[Length
];
470 Status
= GetFvbByAddressAndAttribute (
471 SmmFtwWriteHeader
->FvbBaseAddress
,
472 SmmFtwWriteHeader
->FvbAttributes
,
475 if (!EFI_ERROR (Status
)) {
477 &mFtwDevice
->FtwInstance
,
478 SmmFtwWriteHeader
->Lba
,
479 SmmFtwWriteHeader
->Offset
,
483 SmmFtwWriteHeader
->Data
488 case FTW_FUNCTION_RESTART
:
489 if (CommBufferPayloadSize
< sizeof (SMM_FTW_RESTART_HEADER
)) {
490 DEBUG ((EFI_D_ERROR
, "Restart: SMM communication buffer size invalid!\n"));
493 SmmFtwRestartHeader
= (SMM_FTW_RESTART_HEADER
*) SmmFtwFunctionHeader
->Data
;
494 Status
= GetFvbByAddressAndAttribute (
495 SmmFtwRestartHeader
->FvbBaseAddress
,
496 SmmFtwRestartHeader
->FvbAttributes
,
499 if (!EFI_ERROR (Status
)) {
500 Status
= FtwRestart (&mFtwDevice
->FtwInstance
, SmmFvbHandle
);
504 case FTW_FUNCTION_ABORT
:
505 Status
= FtwAbort (&mFtwDevice
->FtwInstance
);
508 case FTW_FUNCTION_GET_LAST_WRITE
:
509 if (CommBufferPayloadSize
< OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
)) {
510 DEBUG ((EFI_D_ERROR
, "GetLastWrite: SMM communication buffer size invalid!\n"));
513 SmmFtwGetLastWriteHeader
= (SMM_FTW_GET_LAST_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
514 PrivateDataSize
= SmmFtwGetLastWriteHeader
->PrivateDataSize
;
515 if ((UINTN
)(~0) - PrivateDataSize
< OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
)){
517 // Prevent InfoSize overflow
519 Status
= EFI_ACCESS_DENIED
;
522 InfoSize
= OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER
, Data
) + PrivateDataSize
;
525 // SMRAM range check already covered before
527 if (InfoSize
> CommBufferPayloadSize
) {
528 DEBUG ((EFI_D_ERROR
, "Data size exceed communication buffer size limit!\n"));
529 Status
= EFI_ACCESS_DENIED
;
533 Status
= FtwGetLastWrite (
534 &mFtwDevice
->FtwInstance
,
535 &SmmFtwGetLastWriteHeader
->CallerId
,
536 &SmmFtwGetLastWriteHeader
->Lba
,
537 &SmmFtwGetLastWriteHeader
->Offset
,
538 &SmmFtwGetLastWriteHeader
->Length
,
540 (VOID
*)SmmFtwGetLastWriteHeader
->Data
,
541 &SmmFtwGetLastWriteHeader
->Complete
543 SmmFtwGetLastWriteHeader
->PrivateDataSize
= PrivateDataSize
;
547 Status
= EFI_UNSUPPORTED
;
550 SmmFtwFunctionHeader
->ReturnStatus
= Status
;
557 SMM Firmware Volume Block Protocol notification event handler.
559 @param[in] Protocol Points to the protocol's unique identifier
560 @param[in] Interface Points to the interface instance
561 @param[in] Handle The handle on which the interface was installed
563 @retval EFI_SUCCESS SmmEventCallback runs successfully
568 FvbNotificationEvent (
569 IN CONST EFI_GUID
*Protocol
,
575 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
576 EFI_HANDLE SmmFtwHandle
;
577 EFI_HANDLE FtwHandle
;
580 // Just return to avoid install SMM FaultTolerantWriteProtocol again
581 // if SMM Fault Tolerant Write protocol had been installed.
583 Status
= gSmst
->SmmLocateProtocol (
584 &gEfiSmmFaultTolerantWriteProtocolGuid
,
586 (VOID
**) &FtwProtocol
588 if (!EFI_ERROR (Status
)) {
593 // Found proper FVB protocol and initialize FtwDevice for protocol installation
595 Status
= InitFtwProtocol (mFtwDevice
);
596 if (EFI_ERROR(Status
)) {
601 // Install protocol interface
603 Status
= gSmst
->SmmInstallProtocolInterface (
605 &gEfiSmmFaultTolerantWriteProtocolGuid
,
606 EFI_NATIVE_INTERFACE
,
607 &mFtwDevice
->FtwInstance
609 ASSERT_EFI_ERROR (Status
);
612 /// Register SMM FTW SMI handler
614 Status
= gSmst
->SmiHandlerRegister (SmmFaultTolerantWriteHandler
, &gEfiSmmFaultTolerantWriteProtocolGuid
, &SmmFtwHandle
);
615 ASSERT_EFI_ERROR (Status
);
618 // Notify the Ftw wrapper driver SMM Ftw is ready
621 Status
= gBS
->InstallProtocolInterface (
623 &gEfiSmmFaultTolerantWriteProtocolGuid
,
624 EFI_NATIVE_INTERFACE
,
627 ASSERT_EFI_ERROR (Status
);
633 SMM END_OF_DXE protocol notification event handler.
635 @param Protocol Points to the protocol's unique identifier
636 @param Interface Points to the interface instance
637 @param Handle The handle on which the interface was installed
639 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
644 SmmEndOfDxeCallback (
645 IN CONST EFI_GUID
*Protocol
,
655 This function is the entry point of the Fault Tolerant Write driver.
657 @param[in] ImageHandle A handle for the image that is initializing this driver
658 @param[in] SystemTable A pointer to the EFI system table
660 @retval EFI_SUCCESS The initialization finished successfully.
661 @retval EFI_OUT_OF_RESOURCES Allocate memory error
662 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
667 SmmFaultTolerantWriteInitialize (
668 IN EFI_HANDLE ImageHandle
,
669 IN EFI_SYSTEM_TABLE
*SystemTable
673 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
675 VOID
*SmmEndOfDxeRegistration
;
678 // Allocate private data structure for SMM FTW protocol and do some initialization
680 Status
= InitFtwDevice (&mFtwDevice
);
681 if (EFI_ERROR(Status
)) {
686 // Get SMRAM information
688 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
689 ASSERT_EFI_ERROR (Status
);
692 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
693 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
695 Status
= gSmst
->SmmAllocatePool (
696 EfiRuntimeServicesData
,
698 (VOID
**)&mSmramRanges
700 ASSERT_EFI_ERROR (Status
);
702 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
703 ASSERT_EFI_ERROR (Status
);
705 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
708 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
710 Status
= gSmst
->SmmRegisterProtocolNotify (
711 &gEfiSmmEndOfDxeProtocolGuid
,
713 &SmmEndOfDxeRegistration
715 ASSERT_EFI_ERROR (Status
);
718 // Register FvbNotificationEvent () notify function.
720 Status
= gSmst
->SmmRegisterProtocolNotify (
721 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
722 FvbNotificationEvent
,
725 ASSERT_EFI_ERROR (Status
);
727 FvbNotificationEvent (NULL
, NULL
, NULL
);