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 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
44 This program and the accompanying materials
45 are licensed and made available under the terms and conditions of the BSD License
46 which accompanies this distribution. The full text of the license may be found at
47 http://opensource.org/licenses/bsd-license.php
49 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
50 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
54 #include <Library/SmmServicesTableLib.h>
55 #include "FaultTolerantWrite.h"
56 #include <Protocol/SmmFirmwareVolumeBlock.h>
57 #include <Protocol/SmmSwapAddressRange.h>
58 #include <Protocol/SmmFaultTolerantWrite.h>
60 EFI_EVENT mFvbRegistration
= NULL
;
61 EFI_FTW_DEVICE
*gFtwDevice
= NULL
;
64 Retrive the SMM FVB protocol interface by HANDLE.
66 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
67 reading, writing, and erasing the target block.
68 @param[out] FvBlock The interface of SMM FVB protocol
70 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
71 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
72 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
77 IN EFI_HANDLE FvBlockHandle
,
78 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
82 // To get the SMM FVB protocol interface on the handle
84 return gSmst
->SmmHandleProtocol (
86 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
92 Retrive the SMM Swap Address Range protocol interface.
94 @param[out] SarProtocol The interface of SMM SAR protocol
96 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.
97 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.
98 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
103 OUT VOID
**SarProtocol
109 // Locate Smm Swap Address Range protocol
111 Status
= gSmst
->SmmLocateProtocol (
112 &gEfiSmmSwapAddressRangeProtocolGuid
,
120 Function returns an array of handles that support the SMM FVB protocol
121 in a buffer allocated from pool.
123 @param[out] NumberHandles The number of handles returned in Buffer.
124 @param[out] Buffer A pointer to the buffer to return the requested
125 array of handles that support SMM FVB protocol.
127 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
128 handles in Buffer was returned in NumberHandles.
129 @retval EFI_NOT_FOUND No SMM FVB handle was found.
130 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
131 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
135 GetFvbCountAndBuffer (
136 OUT UINTN
*NumberHandles
,
137 OUT EFI_HANDLE
**Buffer
143 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
144 return EFI_INVALID_PARAMETER
;
150 Status
= gSmst
->SmmLocateHandle (
152 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
157 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
158 return EFI_NOT_FOUND
;
161 *Buffer
= AllocatePool (BufferSize
);
162 if (*Buffer
== NULL
) {
163 return EFI_OUT_OF_RESOURCES
;
166 Status
= gSmst
->SmmLocateHandle (
168 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
174 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
175 if (EFI_ERROR(Status
)) {
184 SMM Firmware Volume Block Protocol notification event handler.
186 @param[in] Protocol Points to the protocol's unique identifier
187 @param[in] Interface Points to the interface instance
188 @param[in] Handle The handle on which the interface was installed
190 @retval EFI_SUCCESS SmmEventCallback runs successfully
195 FvbNotificationEvent (
196 IN CONST EFI_GUID
*Protocol
,
202 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
205 // Just return to avoid install SMM FaultTolerantWriteProtocol again
206 // if SMM Fault Tolerant Write protocol had been installed.
208 Status
= gSmst
->SmmLocateProtocol (
209 &gEfiSmmFaultTolerantWriteProtocolGuid
,
211 (VOID
**) &FtwProtocol
213 if (!EFI_ERROR (Status
)) {
218 // Found proper FVB protocol and initialize FtwDevice for protocol installation
220 Status
= InitFtwProtocol (gFtwDevice
);
221 if (EFI_ERROR(Status
)) {
226 // Install protocol interface
228 Status
= gSmst
->SmmInstallProtocolInterface (
230 &gEfiSmmFaultTolerantWriteProtocolGuid
,
231 EFI_NATIVE_INTERFACE
,
232 &gFtwDevice
->FtwInstance
234 ASSERT_EFI_ERROR (Status
);
241 This function is the entry point of the Fault Tolerant Write driver.
243 @param[in] ImageHandle A handle for the image that is initializing this driver
244 @param[in] SystemTable A pointer to the EFI system table
246 @retval EFI_SUCCESS The initialization finished successfully.
247 @retval EFI_OUT_OF_RESOURCES Allocate memory error
248 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
253 SmmFaultTolerantWriteInitialize (
254 IN EFI_HANDLE ImageHandle
,
255 IN EFI_SYSTEM_TABLE
*SystemTable
261 // Allocate private data structure for SMM FTW protocol and do some initialization
263 Status
= InitFtwDevice (&gFtwDevice
);
264 if (EFI_ERROR(Status
)) {
269 // Register FvbNotificationEvent () notify function.
271 Status
= gSmst
->SmmRegisterProtocolNotify (
272 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
273 FvbNotificationEvent
,
276 ASSERT_EFI_ERROR (Status
);
278 FvbNotificationEvent (NULL
, NULL
, NULL
);