]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
Add SMM Variable implementation.
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWriteSmm.c
CommitLineData
8a2d4996 1/** @file\r
2\r
3 This is a simple fault tolerant write driver that is intended to use in the SMM environment.\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
43Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
44This program and the accompanying materials \r
45are licensed and made available under the terms and conditions of the BSD License \r
46which accompanies this distribution. The full text of the license may be found at \r
47http://opensource.org/licenses/bsd-license.php \r
48 \r
49THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
50WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
51\r
52**/\r
53\r
54#include <Library/SmmServicesTableLib.h>\r
55#include "FaultTolerantWrite.h"\r
56#include <Protocol/SmmFirmwareVolumeBlock.h>\r
57#include <Protocol/SmmSwapAddressRange.h>\r
58#include <Protocol/SmmFaultTolerantWrite.h>\r
59\r
60EFI_EVENT mFvbRegistration = NULL;\r
61EFI_FTW_DEVICE *gFtwDevice = NULL;\r
62\r
63/**\r
64 Retrive the SMM FVB protocol interface by HANDLE.\r
65\r
66 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for\r
67 reading, writing, and erasing the target block.\r
68 @param[out] FvBlock The interface of SMM FVB protocol\r
69\r
70 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
71 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.\r
72 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
73\r
74**/\r
75EFI_STATUS\r
76FtwGetFvbByHandle (\r
77 IN EFI_HANDLE FvBlockHandle,\r
78 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
79 )\r
80{\r
81 //\r
82 // To get the SMM FVB protocol interface on the handle\r
83 //\r
84 return gSmst->SmmHandleProtocol (\r
85 FvBlockHandle,\r
86 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
87 (VOID **) FvBlock\r
88 );\r
89}\r
90\r
91/**\r
92 Retrive the SMM Swap Address Range protocol interface.\r
93\r
94 @param[out] SarProtocol The interface of SMM SAR protocol\r
95\r
96 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.\r
97 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.\r
98 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
99\r
100**/\r
101EFI_STATUS\r
102FtwGetSarProtocol (\r
103 OUT VOID **SarProtocol\r
104 )\r
105{\r
106 EFI_STATUS Status;\r
107\r
108 //\r
109 // Locate Smm Swap Address Range protocol\r
110 //\r
111 Status = gSmst->SmmLocateProtocol (\r
112 &gEfiSmmSwapAddressRangeProtocolGuid, \r
113 NULL, \r
114 SarProtocol\r
115 );\r
116 return Status;\r
117}\r
118\r
119/**\r
120 Function returns an array of handles that support the SMM FVB protocol\r
121 in a buffer allocated from pool. \r
122\r
123 @param[out] NumberHandles The number of handles returned in Buffer.\r
124 @param[out] Buffer A pointer to the buffer to return the requested\r
125 array of handles that support SMM FVB protocol.\r
126\r
127 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
128 handles in Buffer was returned in NumberHandles.\r
129 @retval EFI_NOT_FOUND No SMM FVB handle was found.\r
130 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
131 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
132\r
133**/\r
134EFI_STATUS\r
135GetFvbCountAndBuffer (\r
136 OUT UINTN *NumberHandles,\r
137 OUT EFI_HANDLE **Buffer\r
138 )\r
139{\r
140 EFI_STATUS Status;\r
141 UINTN BufferSize;\r
142\r
143 if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
144 return EFI_INVALID_PARAMETER;\r
145 }\r
146\r
147 BufferSize = 0;\r
148 *NumberHandles = 0;\r
149 *Buffer = NULL;\r
150 Status = gSmst->SmmLocateHandle (\r
151 ByProtocol,\r
152 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
153 NULL,\r
154 &BufferSize,\r
155 *Buffer\r
156 );\r
157 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
158 return EFI_NOT_FOUND;\r
159 }\r
160\r
161 *Buffer = AllocatePool (BufferSize);\r
162 if (*Buffer == NULL) {\r
163 return EFI_OUT_OF_RESOURCES;\r
164 }\r
165\r
166 Status = gSmst->SmmLocateHandle (\r
167 ByProtocol,\r
168 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
169 NULL,\r
170 &BufferSize,\r
171 *Buffer\r
172 );\r
173\r
174 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);\r
175 if (EFI_ERROR(Status)) {\r
176 *NumberHandles = 0;\r
177 }\r
178\r
179 return Status;\r
180}\r
181\r
182\r
183/**\r
184 SMM Firmware Volume Block Protocol notification event handler.\r
185 \r
186 @param[in] Protocol Points to the protocol's unique identifier\r
187 @param[in] Interface Points to the interface instance\r
188 @param[in] Handle The handle on which the interface was installed\r
189\r
190 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
191 \r
192 **/\r
193EFI_STATUS\r
194EFIAPI\r
195FvbNotificationEvent (\r
196 IN CONST EFI_GUID *Protocol,\r
197 IN VOID *Interface,\r
198 IN EFI_HANDLE Handle\r
199 )\r
200{\r
201 EFI_STATUS Status;\r
202 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
203 \r
204 //\r
205 // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
206 // if SMM Fault Tolerant Write protocol had been installed.\r
207 // \r
208 Status = gSmst->SmmLocateProtocol (\r
209 &gEfiSmmFaultTolerantWriteProtocolGuid, \r
210 NULL, \r
211 (VOID **) &FtwProtocol\r
212 );\r
213 if (!EFI_ERROR (Status)) {\r
214 return EFI_SUCCESS;\r
215 }\r
216\r
217 //\r
218 // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
219 //\r
220 Status = InitFtwProtocol (gFtwDevice);\r
221 if (EFI_ERROR(Status)) {\r
222 return Status;\r
223 }\r
224 \r
225 //\r
226 // Install protocol interface\r
227 //\r
228 Status = gSmst->SmmInstallProtocolInterface (\r
229 &gFtwDevice->Handle,\r
230 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
231 EFI_NATIVE_INTERFACE,\r
232 &gFtwDevice->FtwInstance\r
233 );\r
234 ASSERT_EFI_ERROR (Status); \r
235 \r
236 return EFI_SUCCESS;\r
237}\r
238\r
239\r
240/**\r
241 This function is the entry point of the Fault Tolerant Write driver.\r
242\r
243 @param[in] ImageHandle A handle for the image that is initializing this driver\r
244 @param[in] SystemTable A pointer to the EFI system table\r
245\r
246 @retval EFI_SUCCESS The initialization finished successfully.\r
247 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
248 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r
249\r
250**/\r
251EFI_STATUS\r
252EFIAPI\r
253SmmFaultTolerantWriteInitialize (\r
254 IN EFI_HANDLE ImageHandle,\r
255 IN EFI_SYSTEM_TABLE *SystemTable\r
256 )\r
257{\r
258 EFI_STATUS Status;\r
259\r
260 //\r
261 // Allocate private data structure for SMM FTW protocol and do some initialization\r
262 //\r
263 Status = InitFtwDevice (&gFtwDevice);\r
264 if (EFI_ERROR(Status)) {\r
265 return Status;\r
266 }\r
267 \r
268 //\r
269 // Register FvbNotificationEvent () notify function.\r
270 // \r
271 Status = gSmst->SmmRegisterProtocolNotify (\r
272 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
273 FvbNotificationEvent,\r
274 &mFvbRegistration\r
275 );\r
276 ASSERT_EFI_ERROR (Status);\r
277\r
278 FvbNotificationEvent (NULL, NULL, NULL);\r
279 \r
280 return EFI_SUCCESS;\r
281}\r