]>
Commit | Line | Data |
---|---|---|
8a2d4996 | 1 | /** @file\r |
2 | \r | |
3 | This is a simple fault tolerant write driver.\r | |
4 | \r | |
5 | This boot service protocol only provides fault tolerant write capability for \r | |
6 | block devices. The protocol has internal non-volatile intermediate storage \r | |
7 | of the data and private information. It should be able to recover \r | |
8 | automatically from a critical fault, such as power failure. \r | |
9 | \r | |
10 | The implementation uses an FTW (Fault Tolerant Write) Work Space. \r | |
11 | This work space is a memory copy of the work space on the Working Block,\r | |
12 | the size of the work space is the FTW_WORK_SPACE_SIZE bytes.\r | |
13 | \r | |
14 | The work space stores each write record as EFI_FTW_RECORD structure.\r | |
15 | The spare block stores the write buffer before write to the target block.\r | |
16 | \r | |
17 | The write record has three states to specify the different phase of write operation.\r | |
18 | 1) WRITE_ALLOCATED is that the record is allocated in write space.\r | |
19 | The information of write operation is stored in write record structure.\r | |
20 | 2) SPARE_COMPLETED is that the data from write buffer is writed into the spare block as the backup.\r | |
21 | 3) WRITE_COMPLETED is that the data is copied from the spare block to the target block.\r | |
22 | \r | |
23 | This driver operates the data as the whole size of spare block.\r | |
24 | It first read the SpareAreaLength data from the target block into the spare memory buffer.\r | |
25 | Then copy the write buffer data into the spare memory buffer.\r | |
26 | Then write the spare memory buffer into the spare block.\r | |
27 | Final copy the data from the spare block to the target block.\r | |
28 | \r | |
29 | To make this drive work well, the following conditions must be satisfied:\r | |
30 | 1. The write NumBytes data must be fit within Spare area. \r | |
31 | Offset + NumBytes <= SpareAreaLength\r | |
32 | 2. The whole flash range has the same block size.\r | |
33 | 3. Working block is an area which contains working space in its last block and has the same size as spare block.\r | |
34 | 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on. \r | |
35 | 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.\r | |
36 | 6. Any write data area (SpareAreaLength Area) which the data will be written into must be \r | |
37 | in the single one Firmware Volume Block range which FVB protocol is produced on.\r | |
38 | 7. If write data area (such as Variable range) is enlarged, the spare area range must be enlarged.\r | |
39 | The spare area must be enough large to store the write data before write them into the target range.\r | |
40 | If one of them is not satisfied, FtwWrite may fail.\r | |
41 | Usually, Spare area only takes one block. That's SpareAreaLength = BlockSize, NumberOfSpareBlock = 1.\r | |
42 | \r | |
8dc8879a | 43 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r |
8a2d4996 | 44 | This program and the accompanying materials \r |
45 | are licensed and made available under the terms and conditions of the BSD License \r | |
46 | which accompanies this distribution. The full text of the license may be found at \r | |
47 | http://opensource.org/licenses/bsd-license.php \r | |
48 | \r | |
49 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
50 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
51 | \r | |
52 | **/\r | |
53 | \r | |
54 | #include "FaultTolerantWrite.h"\r | |
55 | EFI_EVENT mFvbRegistration = NULL;\r | |
56 | \r | |
57 | \r | |
58 | /**\r | |
59 | Retrive the FVB protocol interface by HANDLE.\r | |
60 | \r | |
61 | @param[in] FvBlockHandle The handle of FVB protocol that provides services for\r | |
62 | reading, writing, and erasing the target block.\r | |
63 | @param[out] FvBlock The interface of FVB protocol\r | |
64 | \r | |
65 | @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r | |
66 | @retval EFI_UNSUPPORTED The device does not support the FVB protocol.\r | |
67 | @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r | |
68 | \r | |
69 | **/\r | |
70 | EFI_STATUS\r | |
71 | FtwGetFvbByHandle (\r | |
72 | IN EFI_HANDLE FvBlockHandle,\r | |
73 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r | |
74 | )\r | |
75 | {\r | |
76 | //\r | |
77 | // To get the FVB protocol interface on the handle\r | |
78 | //\r | |
79 | return gBS->HandleProtocol (\r | |
80 | FvBlockHandle,\r | |
81 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
82 | (VOID **) FvBlock\r | |
83 | );\r | |
84 | }\r | |
85 | \r | |
86 | /**\r | |
87 | Retrive the Swap Address Range protocol interface.\r | |
88 | \r | |
89 | @param[out] SarProtocol The interface of SAR protocol\r | |
90 | \r | |
91 | @retval EFI_SUCCESS The SAR protocol instance was found and returned in SarProtocol.\r | |
92 | @retval EFI_NOT_FOUND The SAR protocol instance was not found.\r | |
93 | @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r | |
94 | \r | |
95 | **/\r | |
96 | EFI_STATUS\r | |
97 | FtwGetSarProtocol (\r | |
98 | OUT VOID **SarProtocol\r | |
99 | )\r | |
100 | {\r | |
101 | EFI_STATUS Status;\r | |
102 | \r | |
103 | //\r | |
104 | // Locate Swap Address Range protocol\r | |
105 | //\r | |
106 | Status = gBS->LocateProtocol (\r | |
107 | &gEfiSwapAddressRangeProtocolGuid, \r | |
108 | NULL, \r | |
109 | SarProtocol\r | |
110 | );\r | |
111 | return Status;\r | |
112 | }\r | |
113 | \r | |
114 | /**\r | |
115 | Function returns an array of handles that support the FVB protocol\r | |
116 | in a buffer allocated from pool. \r | |
117 | \r | |
118 | @param[out] NumberHandles The number of handles returned in Buffer.\r | |
119 | @param[out] Buffer A pointer to the buffer to return the requested\r | |
120 | array of handles that support FVB protocol.\r | |
121 | \r | |
122 | @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r | |
123 | handles in Buffer was returned in NumberHandles.\r | |
124 | @retval EFI_NOT_FOUND No FVB handle was found.\r | |
125 | @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r | |
126 | @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r | |
127 | \r | |
128 | **/\r | |
129 | EFI_STATUS\r | |
130 | GetFvbCountAndBuffer (\r | |
131 | OUT UINTN *NumberHandles,\r | |
132 | OUT EFI_HANDLE **Buffer\r | |
133 | )\r | |
134 | {\r | |
135 | EFI_STATUS Status;\r | |
136 | \r | |
137 | //\r | |
138 | // Locate all handles of Fvb protocol\r | |
139 | //\r | |
140 | Status = gBS->LocateHandleBuffer (\r | |
141 | ByProtocol,\r | |
142 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
143 | NULL,\r | |
144 | NumberHandles,\r | |
145 | Buffer\r | |
146 | );\r | |
147 | return Status;\r | |
148 | }\r | |
149 | \r | |
150 | \r | |
151 | /**\r | |
152 | Firmware Volume Block Protocol notification event handler.\r | |
153 | \r | |
154 | @param[in] Event Event whose notification function is being invoked.\r | |
155 | @param[in] Context Pointer to the notification function's context.\r | |
156 | \r | |
157 | **/\r | |
158 | VOID\r | |
159 | EFIAPI\r | |
160 | FvbNotificationEvent (\r | |
161 | IN EFI_EVENT Event,\r | |
162 | IN VOID *Context\r | |
163 | )\r | |
164 | {\r | |
165 | EFI_STATUS Status;\r | |
166 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
167 | EFI_FTW_DEVICE *FtwDevice;\r | |
168 | \r | |
169 | //\r | |
8dc8879a | 170 | // Just return to avoid installing FaultTolerantWriteProtocol again\r |
171 | // if Fault Tolerant Write protocol has been installed.\r | |
8a2d4996 | 172 | // \r |
173 | Status = gBS->LocateProtocol (\r | |
174 | &gEfiFaultTolerantWriteProtocolGuid, \r | |
175 | NULL, \r | |
176 | (VOID **) &FtwProtocol\r | |
177 | );\r | |
178 | if (!EFI_ERROR (Status)) {\r | |
179 | return ;\r | |
180 | }\r | |
181 | \r | |
182 | //\r | |
183 | // Found proper FVB protocol and initialize FtwDevice for protocol installation\r | |
184 | //\r | |
185 | FtwDevice = (EFI_FTW_DEVICE *)Context;\r | |
186 | Status = InitFtwProtocol (FtwDevice);\r | |
187 | if (EFI_ERROR(Status)) {\r | |
188 | return ;\r | |
189 | } \r | |
190 | \r | |
191 | //\r | |
192 | // Install protocol interface\r | |
193 | //\r | |
194 | Status = gBS->InstallProtocolInterface (\r | |
195 | &FtwDevice->Handle,\r | |
196 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
197 | EFI_NATIVE_INTERFACE,\r | |
198 | &FtwDevice->FtwInstance\r | |
199 | );\r | |
200 | ASSERT_EFI_ERROR (Status);\r | |
201 | \r | |
202 | Status = gBS->CloseEvent (Event);\r | |
203 | ASSERT_EFI_ERROR (Status);\r | |
204 | \r | |
205 | return;\r | |
206 | }\r | |
207 | \r | |
208 | \r | |
209 | /**\r | |
210 | This function is the entry point of the Fault Tolerant Write driver.\r | |
211 | \r | |
212 | @param[in] ImageHandle A handle for the image that is initializing this driver\r | |
213 | @param[in] SystemTable A pointer to the EFI system table\r | |
214 | \r | |
215 | @retval EFI_SUCCESS The initialization finished successfully.\r | |
216 | @retval EFI_OUT_OF_RESOURCES Allocate memory error\r | |
217 | @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r | |
218 | \r | |
219 | **/\r | |
220 | EFI_STATUS\r | |
221 | EFIAPI\r | |
222 | FaultTolerantWriteInitialize (\r | |
223 | IN EFI_HANDLE ImageHandle,\r | |
224 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
225 | )\r | |
226 | {\r | |
227 | EFI_STATUS Status;\r | |
228 | EFI_FTW_DEVICE *FtwDevice;\r | |
229 | \r | |
230 | //\r | |
231 | // Allocate private data structure for FTW protocol and do some initialization\r | |
232 | //\r | |
233 | Status = InitFtwDevice (&FtwDevice);\r | |
234 | if (EFI_ERROR(Status)) {\r | |
235 | return Status;\r | |
236 | }\r | |
237 | \r | |
238 | //\r | |
239 | // Register FvbNotificationEvent () notify function.\r | |
240 | // \r | |
241 | EfiCreateProtocolNotifyEvent (\r | |
242 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
243 | TPL_CALLBACK,\r | |
244 | FvbNotificationEvent,\r | |
245 | (VOID *)FtwDevice,\r | |
246 | &mFvbRegistration\r | |
247 | );\r | |
248 | \r | |
249 | return EFI_SUCCESS;\r | |
250 | }\r |