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 - 2012, 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>
64 EFI_EVENT mFvbRegistration
= NULL
;
65 EFI_FTW_DEVICE
*mFtwDevice
= NULL
;
66 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
67 UINTN mSmramRangeCount
;
71 This function check if the address is in SMRAM.
73 @param Buffer the buffer address to be checked.
74 @param Length the buffer length to be checked.
76 @retval TRUE this address is in SMRAM.
77 @retval FALSE this address is NOT in SMRAM.
80 InternalIsAddressInSmram (
81 IN EFI_PHYSICAL_ADDRESS Buffer
,
87 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
88 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
89 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
99 Retrive the SMM FVB protocol interface by HANDLE.
101 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
102 reading, writing, and erasing the target block.
103 @param[out] FvBlock The interface of SMM FVB protocol
105 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
106 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
107 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
112 IN EFI_HANDLE FvBlockHandle
,
113 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
117 // To get the SMM FVB protocol interface on the handle
119 return gSmst
->SmmHandleProtocol (
121 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
127 Retrive the SMM Swap Address Range protocol interface.
129 @param[out] SarProtocol The interface of SMM SAR protocol
131 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
132 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
133 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
138 OUT VOID
**SarProtocol
144 // Locate Smm Swap Address Range protocol
146 Status
= gSmst
->SmmLocateProtocol (
147 &gEfiSmmSwapAddressRangeProtocolGuid
,
155 Function returns an array of handles that support the SMM FVB protocol
156 in a buffer allocated from pool.
158 @param[out] NumberHandles The number of handles returned in Buffer.
159 @param[out] Buffer A pointer to the buffer to return the requested
160 array of handles that support SMM FVB protocol.
162 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
163 handles in Buffer was returned in NumberHandles.
164 @retval EFI_NOT_FOUND No SMM FVB handle was found.
165 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
166 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
170 GetFvbCountAndBuffer (
171 OUT UINTN
*NumberHandles
,
172 OUT EFI_HANDLE
**Buffer
178 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
179 return EFI_INVALID_PARAMETER
;
185 Status
= gSmst
->SmmLocateHandle (
187 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
192 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
193 return EFI_NOT_FOUND
;
196 *Buffer
= AllocatePool (BufferSize
);
197 if (*Buffer
== NULL
) {
198 return EFI_OUT_OF_RESOURCES
;
201 Status
= gSmst
->SmmLocateHandle (
203 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
209 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
210 if (EFI_ERROR(Status
)) {
221 Get the handle of the SMM FVB protocol by the FVB base address and attributes.
223 @param[in] Address The base address of SMM FVB protocol.
224 @param[in] Attributes The attributes of the SMM FVB protocol.
225 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.
227 @retval EFI_SUCCESS The FVB handle is found.
228 @retval EFI_ABORTED The FVB protocol is not found.
232 GetFvbByAddressAndAttribute (
233 IN EFI_PHYSICAL_ADDRESS Address
,
234 IN EFI_FVB_ATTRIBUTES_2 Attributes
,
235 OUT EFI_HANDLE
*SmmFvbHandle
239 EFI_HANDLE
*HandleBuffer
;
242 EFI_PHYSICAL_ADDRESS FvbBaseAddress
;
243 EFI_FVB_ATTRIBUTES_2 FvbAttributes
;
244 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*Fvb
;
247 // Locate all handles of SMM Fvb protocol.
249 Status
= GetFvbCountAndBuffer (&HandleCount
, &HandleBuffer
);
250 if (EFI_ERROR (Status
)) {
255 // Find the proper SMM Fvb handle by the address and attributes.
257 for (Index
= 0; Index
< HandleCount
; Index
++) {
258 Status
= FtwGetFvbByHandle (HandleBuffer
[Index
], &Fvb
);
259 if (EFI_ERROR (Status
)) {
263 // Compare the address.
265 Status
= Fvb
->GetPhysicalAddress (Fvb
, &FvbBaseAddress
);
266 if (EFI_ERROR (Status
)) {
269 if (Address
!= FvbBaseAddress
) {
274 // Compare the attribute.
276 Status
= Fvb
->GetAttributes (Fvb
, &FvbAttributes
);
277 if (EFI_ERROR (Status
)) {
280 if (Attributes
!= FvbAttributes
) {
285 // Found the proper FVB handle.
287 *SmmFvbHandle
= HandleBuffer
[Index
];
288 FreePool (HandleBuffer
);
292 FreePool (HandleBuffer
);
297 Communication service SMI Handler entry.
299 This SMI handler provides services for the fault tolerant write wrapper driver.
301 Caution: This function requires additional review when modified.
302 This driver need to make sure the CommBuffer is not in the SMRAM range.
303 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +
304 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.
306 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
307 @param[in] RegisterContext Points to an optional handler context which was specified when the
308 handler was registered.
309 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed
310 from a non-SMM environment into an SMM environment.
311 @param[in, out] CommBufferSize The size of the CommBuffer.
313 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
314 should still be called.
315 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
317 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
319 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
324 SmmFaultTolerantWriteHandler (
325 IN EFI_HANDLE DispatchHandle
,
326 IN CONST VOID
*RegisterContext
,
327 IN OUT VOID
*CommBuffer
,
328 IN OUT UINTN
*CommBufferSize
332 SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*SmmFtwFunctionHeader
;
333 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*SmmGetMaxBlockSizeHeader
;
334 SMM_FTW_ALLOCATE_HEADER
*SmmFtwAllocateHeader
;
335 SMM_FTW_WRITE_HEADER
*SmmFtwWriteHeader
;
336 SMM_FTW_RESTART_HEADER
*SmmFtwRestartHeader
;
337 SMM_FTW_GET_LAST_WRITE_HEADER
*SmmFtwGetLastWriteHeader
;
339 EFI_HANDLE SmmFvbHandle
;
341 ASSERT (CommBuffer
!= NULL
);
342 ASSERT (CommBufferSize
!= NULL
);
344 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBuffer
, *CommBufferSize
)) {
345 DEBUG ((EFI_D_ERROR
, "SMM communication buffer size is in SMRAM!\n"));
349 SmmFtwFunctionHeader
= (SMM_FTW_COMMUNICATE_FUNCTION_HEADER
*)CommBuffer
;
350 switch (SmmFtwFunctionHeader
->Function
) {
351 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE
:
352 SmmGetMaxBlockSizeHeader
= (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER
*) SmmFtwFunctionHeader
->Data
;
353 Status
= FtwGetMaxBlockSize (
354 &mFtwDevice
->FtwInstance
,
355 &SmmGetMaxBlockSizeHeader
->BlockSize
359 case FTW_FUNCTION_ALLOCATE
:
360 SmmFtwAllocateHeader
= (SMM_FTW_ALLOCATE_HEADER
*) SmmFtwFunctionHeader
->Data
;
361 Status
= FtwAllocate (
362 &mFtwDevice
->FtwInstance
,
363 &SmmFtwAllocateHeader
->CallerId
,
364 SmmFtwAllocateHeader
->PrivateDataSize
,
365 SmmFtwAllocateHeader
->NumberOfWrites
369 case FTW_FUNCTION_WRITE
:
370 SmmFtwWriteHeader
= (SMM_FTW_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
371 if (SmmFtwWriteHeader
->PrivateDataSize
== 0) {
374 PrivateData
= (VOID
*)&SmmFtwWriteHeader
->Data
[SmmFtwWriteHeader
->Length
];
376 Status
= GetFvbByAddressAndAttribute (
377 SmmFtwWriteHeader
->FvbBaseAddress
,
378 SmmFtwWriteHeader
->FvbAttributes
,
381 if (!EFI_ERROR (Status
)) {
383 &mFtwDevice
->FtwInstance
,
384 SmmFtwWriteHeader
->Lba
,
385 SmmFtwWriteHeader
->Offset
,
386 SmmFtwWriteHeader
->Length
,
389 SmmFtwWriteHeader
->Data
394 case FTW_FUNCTION_RESTART
:
395 SmmFtwRestartHeader
= (SMM_FTW_RESTART_HEADER
*) SmmFtwFunctionHeader
->Data
;
396 Status
= GetFvbByAddressAndAttribute (
397 SmmFtwRestartHeader
->FvbBaseAddress
,
398 SmmFtwRestartHeader
->FvbAttributes
,
401 if (!EFI_ERROR (Status
)) {
402 Status
= FtwRestart (&mFtwDevice
->FtwInstance
, SmmFvbHandle
);
406 case FTW_FUNCTION_ABORT
:
407 Status
= FtwAbort (&mFtwDevice
->FtwInstance
);
410 case FTW_FUNCTION_GET_LAST_WRITE
:
411 SmmFtwGetLastWriteHeader
= (SMM_FTW_GET_LAST_WRITE_HEADER
*) SmmFtwFunctionHeader
->Data
;
412 if (((UINT8
*)SmmFtwGetLastWriteHeader
->Data
> (UINT8
*)CommBuffer
) &&
413 ((UINT8
*)SmmFtwGetLastWriteHeader
->Data
+ SmmFtwGetLastWriteHeader
->PrivateDataSize
<= (UINT8
*)CommBuffer
+ (*CommBufferSize
))) {
414 Status
= FtwGetLastWrite (
415 &mFtwDevice
->FtwInstance
,
416 &SmmFtwGetLastWriteHeader
->CallerId
,
417 &SmmFtwGetLastWriteHeader
->Lba
,
418 &SmmFtwGetLastWriteHeader
->Offset
,
419 &SmmFtwGetLastWriteHeader
->Length
,
420 &SmmFtwGetLastWriteHeader
->PrivateDataSize
,
421 (VOID
*)SmmFtwGetLastWriteHeader
->Data
,
422 &SmmFtwGetLastWriteHeader
->Complete
425 Status
= EFI_INVALID_PARAMETER
;
430 Status
= EFI_UNSUPPORTED
;
433 SmmFtwFunctionHeader
->ReturnStatus
= Status
;
440 SMM Firmware Volume Block Protocol notification event handler.
442 @param[in] Protocol Points to the protocol's unique identifier
443 @param[in] Interface Points to the interface instance
444 @param[in] Handle The handle on which the interface was installed
446 @retval EFI_SUCCESS SmmEventCallback runs successfully
451 FvbNotificationEvent (
452 IN CONST EFI_GUID
*Protocol
,
458 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
459 EFI_HANDLE SmmFtwHandle
;
462 // Just return to avoid install SMM FaultTolerantWriteProtocol again
463 // if SMM Fault Tolerant Write protocol had been installed.
465 Status
= gSmst
->SmmLocateProtocol (
466 &gEfiSmmFaultTolerantWriteProtocolGuid
,
468 (VOID
**) &FtwProtocol
470 if (!EFI_ERROR (Status
)) {
475 // Found proper FVB protocol and initialize FtwDevice for protocol installation
477 Status
= InitFtwProtocol (mFtwDevice
);
478 if (EFI_ERROR(Status
)) {
483 // Install protocol interface
485 Status
= gSmst
->SmmInstallProtocolInterface (
487 &gEfiSmmFaultTolerantWriteProtocolGuid
,
488 EFI_NATIVE_INTERFACE
,
489 &mFtwDevice
->FtwInstance
491 ASSERT_EFI_ERROR (Status
);
494 // Notify the Ftw wrapper driver SMM Ftw is ready
497 Status
= gBS
->InstallProtocolInterface (
499 &gEfiSmmFaultTolerantWriteProtocolGuid
,
500 EFI_NATIVE_INTERFACE
,
503 ASSERT_EFI_ERROR (Status
);
510 This function is the entry point of the Fault Tolerant Write driver.
512 @param[in] ImageHandle A handle for the image that is initializing this driver
513 @param[in] SystemTable A pointer to the EFI system table
515 @retval EFI_SUCCESS The initialization finished successfully.
516 @retval EFI_OUT_OF_RESOURCES Allocate memory error
517 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
522 SmmFaultTolerantWriteInitialize (
523 IN EFI_HANDLE ImageHandle
,
524 IN EFI_SYSTEM_TABLE
*SystemTable
528 EFI_HANDLE FtwHandle
;
529 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
533 // Allocate private data structure for SMM FTW protocol and do some initialization
535 Status
= InitFtwDevice (&mFtwDevice
);
536 if (EFI_ERROR(Status
)) {
541 // Get SMRAM information
543 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
544 ASSERT_EFI_ERROR (Status
);
547 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
548 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
550 Status
= gSmst
->SmmAllocatePool (
551 EfiRuntimeServicesData
,
553 (VOID
**)&mSmramRanges
555 ASSERT_EFI_ERROR (Status
);
557 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
558 ASSERT_EFI_ERROR (Status
);
560 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
563 // Register FvbNotificationEvent () notify function.
565 Status
= gSmst
->SmmRegisterProtocolNotify (
566 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
567 FvbNotificationEvent
,
570 ASSERT_EFI_ERROR (Status
);
572 FvbNotificationEvent (NULL
, NULL
, NULL
);
575 /// Register SMM FTW SMI handler
577 Status
= gSmst
->SmiHandlerRegister (SmmFaultTolerantWriteHandler
, &gEfiSmmFaultTolerantWriteProtocolGuid
, &FtwHandle
);
578 ASSERT_EFI_ERROR (Status
);