• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2 
3   This is a simple fault tolerant write driver.
4 
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.
9 
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.
13 
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.
16 
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.
22 
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.
28 
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.
42 
43 Copyright (c) 2006 - 2014, 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
48 
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.
51 
52 **/
53 
54 #include "FaultTolerantWrite.h"
55 EFI_EVENT                                 mFvbRegistration = NULL;
56 
57 
58 /**
59   Retrieve the FVB protocol interface by HANDLE.
60 
61   @param[in]  FvBlockHandle     The handle of FVB protocol that provides services for
62                                 reading, writing, and erasing the target block.
63   @param[out] FvBlock           The interface of FVB protocol
64 
65   @retval EFI_SUCCESS           The interface information for the specified protocol was returned.
66   @retval EFI_UNSUPPORTED       The device does not support the FVB protocol.
67   @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
68 
69 **/
70 EFI_STATUS
FtwGetFvbByHandle(IN EFI_HANDLE FvBlockHandle,OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL ** FvBlock)71 FtwGetFvbByHandle (
72   IN  EFI_HANDLE                          FvBlockHandle,
73   OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  **FvBlock
74   )
75 {
76   //
77   // To get the FVB protocol interface on the handle
78   //
79   return gBS->HandleProtocol (
80                 FvBlockHandle,
81                 &gEfiFirmwareVolumeBlockProtocolGuid,
82                 (VOID **) FvBlock
83                 );
84 }
85 
86 /**
87   Retrieve the Swap Address Range protocol interface.
88 
89   @param[out] SarProtocol       The interface of SAR protocol
90 
91   @retval EFI_SUCCESS           The SAR protocol instance was found and returned in SarProtocol.
92   @retval EFI_NOT_FOUND         The SAR protocol instance was not found.
93   @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
94 
95 **/
96 EFI_STATUS
FtwGetSarProtocol(OUT VOID ** SarProtocol)97 FtwGetSarProtocol (
98   OUT VOID                                **SarProtocol
99   )
100 {
101   EFI_STATUS                              Status;
102 
103   //
104   // Locate Swap Address Range protocol
105   //
106   Status = gBS->LocateProtocol (
107                   &gEfiSwapAddressRangeProtocolGuid,
108                   NULL,
109                   SarProtocol
110                   );
111   return Status;
112 }
113 
114 /**
115   Function returns an array of handles that support the FVB protocol
116   in a buffer allocated from pool.
117 
118   @param[out]  NumberHandles    The number of handles returned in Buffer.
119   @param[out]  Buffer           A pointer to the buffer to return the requested
120                                 array of  handles that support FVB protocol.
121 
122   @retval EFI_SUCCESS           The array of handles was returned in Buffer, and the number of
123                                 handles in Buffer was returned in NumberHandles.
124   @retval EFI_NOT_FOUND         No FVB handle was found.
125   @retval EFI_OUT_OF_RESOURCES  There is not enough pool memory to store the matching results.
126   @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
127 
128 **/
129 EFI_STATUS
GetFvbCountAndBuffer(OUT UINTN * NumberHandles,OUT EFI_HANDLE ** Buffer)130 GetFvbCountAndBuffer (
131   OUT UINTN                               *NumberHandles,
132   OUT EFI_HANDLE                          **Buffer
133   )
134 {
135   EFI_STATUS                              Status;
136 
137   //
138   // Locate all handles of Fvb protocol
139   //
140   Status = gBS->LocateHandleBuffer (
141                   ByProtocol,
142                   &gEfiFirmwareVolumeBlockProtocolGuid,
143                   NULL,
144                   NumberHandles,
145                   Buffer
146                   );
147   return Status;
148 }
149 
150 
151 /**
152   Firmware Volume Block Protocol notification event handler.
153 
154   @param[in] Event    Event whose notification function is being invoked.
155   @param[in] Context  Pointer to the notification function's context.
156 
157 **/
158 VOID
159 EFIAPI
FvbNotificationEvent(IN EFI_EVENT Event,IN VOID * Context)160 FvbNotificationEvent (
161   IN  EFI_EVENT                           Event,
162   IN  VOID                                *Context
163   )
164 {
165   EFI_STATUS                              Status;
166   EFI_FAULT_TOLERANT_WRITE_PROTOCOL       *FtwProtocol;
167   EFI_FTW_DEVICE                          *FtwDevice;
168 
169   //
170   // Just return to avoid installing FaultTolerantWriteProtocol again
171   // if Fault Tolerant Write protocol has been installed.
172   //
173   Status = gBS->LocateProtocol (
174                   &gEfiFaultTolerantWriteProtocolGuid,
175                   NULL,
176                   (VOID **) &FtwProtocol
177                   );
178   if (!EFI_ERROR (Status)) {
179     return ;
180   }
181 
182   //
183   // Found proper FVB protocol and initialize FtwDevice for protocol installation
184   //
185   FtwDevice = (EFI_FTW_DEVICE *)Context;
186   Status = InitFtwProtocol (FtwDevice);
187   if (EFI_ERROR(Status)) {
188     return ;
189   }
190 
191   //
192   // Install protocol interface
193   //
194   Status = gBS->InstallProtocolInterface (
195                   &FtwDevice->Handle,
196                   &gEfiFaultTolerantWriteProtocolGuid,
197                   EFI_NATIVE_INTERFACE,
198                   &FtwDevice->FtwInstance
199                   );
200   ASSERT_EFI_ERROR (Status);
201 
202   Status = gBS->CloseEvent (Event);
203   ASSERT_EFI_ERROR (Status);
204 
205   return;
206 }
207 
208 
209 /**
210   This function is the entry point of the Fault Tolerant Write driver.
211 
212   @param[in] ImageHandle        A handle for the image that is initializing this driver
213   @param[in] SystemTable        A pointer to the EFI system table
214 
215   @retval EFI_SUCCESS           The initialization finished successfully.
216   @retval EFI_OUT_OF_RESOURCES  Allocate memory error
217   @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist
218 
219 **/
220 EFI_STATUS
221 EFIAPI
FaultTolerantWriteInitialize(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)222 FaultTolerantWriteInitialize (
223   IN EFI_HANDLE                           ImageHandle,
224   IN EFI_SYSTEM_TABLE                     *SystemTable
225   )
226 {
227   EFI_STATUS                              Status;
228   EFI_FTW_DEVICE                          *FtwDevice;
229 
230   FtwDevice = NULL;
231 
232   //
233   // Allocate private data structure for FTW protocol and do some initialization
234   //
235   Status = InitFtwDevice (&FtwDevice);
236   if (EFI_ERROR(Status)) {
237     return Status;
238   }
239 
240   //
241   // Register FvbNotificationEvent () notify function.
242   //
243   EfiCreateProtocolNotifyEvent (
244     &gEfiFirmwareVolumeBlockProtocolGuid,
245     TPL_CALLBACK,
246     FvbNotificationEvent,
247     (VOID *)FtwDevice,
248     &mFvbRegistration
249     );
250 
251   return EFI_SUCCESS;
252 }
253