]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
MdeModulePkg GraphicsConsole: Add text mode for PcdConOutColumn/Row
[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
f3b80a8e 43Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
8a2d4996 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
f3b80a8e 54#include <PiSmm.h>\r
8a2d4996 55#include <Library/SmmServicesTableLib.h>\r
8a2d4996 56#include <Protocol/SmmSwapAddressRange.h>\r
f3b80a8e 57#include "FaultTolerantWrite.h"\r
58#include "FaultTolerantWriteSmmCommon.h"\r
8a2d4996 59\r
60EFI_EVENT mFvbRegistration = NULL;\r
f3b80a8e 61EFI_FTW_DEVICE *mFtwDevice = NULL;\r
8a2d4996 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
f3b80a8e 183/**\r
184 Get the handle of the SMM FVB protocol by the FVB base address and attributes.\r
185\r
186 @param[in] Address The base address of SMM FVB protocol.\r
187 @param[in] Attributes The attributes of the SMM FVB protocol.\r
188 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.\r
189\r
190 @retval EFI_SUCCESS The FVB handle is found.\r
191 @retval EFI_ABORTED The FVB protocol is not found.\r
192\r
193**/\r
194EFI_STATUS\r
195GetFvbByAddressAndAttribute (\r
196 IN EFI_PHYSICAL_ADDRESS Address,\r
197 IN EFI_FVB_ATTRIBUTES_2 Attributes,\r
198 OUT EFI_HANDLE *SmmFvbHandle\r
199 )\r
200{\r
201 EFI_STATUS Status;\r
202 EFI_HANDLE *HandleBuffer;\r
203 UINTN HandleCount;\r
204 UINTN Index;\r
205 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
206 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
207 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
208\r
209 //\r
210 // Locate all handles of SMM Fvb protocol.\r
211 //\r
212 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
213 if (EFI_ERROR (Status)) {\r
214 return EFI_ABORTED;\r
215 }\r
216 \r
217 //\r
218 // Find the proper SMM Fvb handle by the address and attributes.\r
219 //\r
220 for (Index = 0; Index < HandleCount; Index++) {\r
221 Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);\r
222 if (EFI_ERROR (Status)) {\r
223 break;\r
224 }\r
225 //\r
226 // Compare the address.\r
227 //\r
228 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
229 if (EFI_ERROR (Status)) {\r
230 continue;\r
231 }\r
232 if (Address != FvbBaseAddress) {\r
233 continue;\r
234 }\r
235\r
236 //\r
237 // Compare the attribute.\r
238 //\r
239 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
240 if (EFI_ERROR (Status)) {\r
241 continue;\r
242 }\r
243 if (Attributes != FvbAttributes) {\r
244 continue;\r
245 }\r
246\r
247 //\r
248 // Found the proper FVB handle.\r
249 //\r
250 *SmmFvbHandle = HandleBuffer[Index];\r
251 FreePool (HandleBuffer);\r
252 return EFI_SUCCESS;\r
253 }\r
254\r
255 FreePool (HandleBuffer);\r
256 return EFI_ABORTED;\r
257}\r
258\r
259/**\r
260 Communication service SMI Handler entry.\r
261\r
262 This SMI handler provides services for the fault tolerant write wrapper driver.\r
263\r
264 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
265 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
266 handler was registered.\r
267 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed\r
268 from a non-SMM environment into an SMM environment.\r
269 @param[in, out] CommBufferSize The size of the CommBuffer.\r
270\r
271 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers \r
272 should still be called.\r
273 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should \r
274 still be called.\r
275 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still \r
276 be called.\r
277 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
278 \r
279**/\r
280EFI_STATUS\r
281EFIAPI\r
282SmmFaultTolerantWriteHandler (\r
283 IN EFI_HANDLE DispatchHandle,\r
284 IN CONST VOID *RegisterContext,\r
285 IN OUT VOID *CommBuffer,\r
286 IN OUT UINTN *CommBufferSize\r
287 )\r
288{\r
289 EFI_STATUS Status;\r
290 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;\r
291 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmGetMaxBlockSizeHeader;\r
292 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;\r
293 SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;\r
294 SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;\r
295 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;\r
296 VOID *PrivateData;\r
297 EFI_HANDLE SmmFvbHandle;\r
298\r
299 ASSERT (CommBuffer != NULL);\r
300 ASSERT (CommBufferSize != NULL);\r
301\r
302 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;\r
303 switch (SmmFtwFunctionHeader->Function) {\r
304 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:\r
305 SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *) SmmFtwFunctionHeader->Data; \r
306 Status = FtwGetMaxBlockSize (\r
307 &mFtwDevice->FtwInstance,\r
308 &SmmGetMaxBlockSizeHeader->BlockSize\r
309 );\r
310 break;\r
311 \r
312 case FTW_FUNCTION_ALLOCATE:\r
313 SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *) SmmFtwFunctionHeader->Data;\r
314 Status = FtwAllocate (\r
315 &mFtwDevice->FtwInstance,\r
316 &SmmFtwAllocateHeader->CallerId,\r
317 SmmFtwAllocateHeader->PrivateDataSize,\r
318 SmmFtwAllocateHeader->NumberOfWrites\r
319 );\r
320 break;\r
321 \r
322 case FTW_FUNCTION_WRITE:\r
323 SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
324 if (SmmFtwWriteHeader->PrivateDataSize == 0) {\r
325 PrivateData = NULL;\r
326 } else {\r
327 PrivateData = (VOID *)&SmmFtwWriteHeader->Data[SmmFtwWriteHeader->Length];\r
328 }\r
329 Status = GetFvbByAddressAndAttribute (\r
330 SmmFtwWriteHeader->FvbBaseAddress, \r
331 SmmFtwWriteHeader->FvbAttributes,\r
332 &SmmFvbHandle\r
333 );\r
334 if (!EFI_ERROR (Status)) {\r
335 Status = FtwWrite(\r
336 &mFtwDevice->FtwInstance,\r
337 SmmFtwWriteHeader->Lba,\r
338 SmmFtwWriteHeader->Offset,\r
339 SmmFtwWriteHeader->Length,\r
340 PrivateData,\r
341 SmmFvbHandle,\r
342 SmmFtwWriteHeader->Data\r
343 );\r
344 }\r
345 break;\r
346 \r
347 case FTW_FUNCTION_RESTART:\r
348 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;\r
349 Status = GetFvbByAddressAndAttribute (\r
350 SmmFtwRestartHeader->FvbBaseAddress, \r
351 SmmFtwRestartHeader->FvbAttributes,\r
352 &SmmFvbHandle\r
353 ); \r
354 if (!EFI_ERROR (Status)) {\r
355 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);\r
356 }\r
357 break;\r
358\r
359 case FTW_FUNCTION_ABORT:\r
360 Status = FtwAbort (&mFtwDevice->FtwInstance);\r
361 break;\r
362 \r
363 case FTW_FUNCTION_GET_LAST_WRITE:\r
364 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
365 Status = FtwGetLastWrite (\r
366 &mFtwDevice->FtwInstance,\r
367 &SmmFtwGetLastWriteHeader->CallerId,\r
368 &SmmFtwGetLastWriteHeader->Lba,\r
369 &SmmFtwGetLastWriteHeader->Offset,\r
370 &SmmFtwGetLastWriteHeader->Length,\r
371 &SmmFtwGetLastWriteHeader->PrivateDataSize,\r
372 (VOID *)SmmFtwGetLastWriteHeader->Data,\r
373 &SmmFtwGetLastWriteHeader->Complete\r
374 );\r
375 break;\r
376\r
377 default:\r
378 ASSERT (FALSE);\r
379 Status = EFI_UNSUPPORTED;\r
380 }\r
381\r
382 SmmFtwFunctionHeader->ReturnStatus = Status;\r
383\r
384 return EFI_SUCCESS;\r
385}\r
386\r
387\r
8a2d4996 388/**\r
389 SMM Firmware Volume Block Protocol notification event handler.\r
390 \r
391 @param[in] Protocol Points to the protocol's unique identifier\r
392 @param[in] Interface Points to the interface instance\r
393 @param[in] Handle The handle on which the interface was installed\r
394\r
395 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
396 \r
397 **/\r
398EFI_STATUS\r
399EFIAPI\r
400FvbNotificationEvent (\r
401 IN CONST EFI_GUID *Protocol,\r
402 IN VOID *Interface,\r
403 IN EFI_HANDLE Handle\r
404 )\r
405{\r
406 EFI_STATUS Status;\r
407 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
f3b80a8e 408 EFI_HANDLE SmmFtwHandle;\r
8a2d4996 409 \r
410 //\r
411 // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
412 // if SMM Fault Tolerant Write protocol had been installed.\r
413 // \r
414 Status = gSmst->SmmLocateProtocol (\r
415 &gEfiSmmFaultTolerantWriteProtocolGuid, \r
416 NULL, \r
417 (VOID **) &FtwProtocol\r
418 );\r
419 if (!EFI_ERROR (Status)) {\r
420 return EFI_SUCCESS;\r
421 }\r
422\r
423 //\r
424 // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
425 //\r
f3b80a8e 426 Status = InitFtwProtocol (mFtwDevice);\r
8a2d4996 427 if (EFI_ERROR(Status)) {\r
428 return Status;\r
429 }\r
430 \r
431 //\r
432 // Install protocol interface\r
433 //\r
434 Status = gSmst->SmmInstallProtocolInterface (\r
f3b80a8e 435 &mFtwDevice->Handle,\r
8a2d4996 436 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
437 EFI_NATIVE_INTERFACE,\r
f3b80a8e 438 &mFtwDevice->FtwInstance\r
8a2d4996 439 );\r
440 ASSERT_EFI_ERROR (Status); \r
f3b80a8e 441\r
442 //\r
443 // Notify the Ftw wrapper driver SMM Ftw is ready\r
444 //\r
445 SmmFtwHandle = NULL;\r
446 Status = gBS->InstallProtocolInterface (\r
447 &SmmFtwHandle,\r
448 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
449 EFI_NATIVE_INTERFACE,\r
450 NULL\r
451 );\r
452 ASSERT_EFI_ERROR (Status);\r
8a2d4996 453 \r
454 return EFI_SUCCESS;\r
455}\r
456\r
457\r
458/**\r
459 This function is the entry point of the Fault Tolerant Write driver.\r
460\r
461 @param[in] ImageHandle A handle for the image that is initializing this driver\r
462 @param[in] SystemTable A pointer to the EFI system table\r
463\r
464 @retval EFI_SUCCESS The initialization finished successfully.\r
465 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
466 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r
467\r
468**/\r
469EFI_STATUS\r
470EFIAPI\r
471SmmFaultTolerantWriteInitialize (\r
472 IN EFI_HANDLE ImageHandle,\r
473 IN EFI_SYSTEM_TABLE *SystemTable\r
474 )\r
475{\r
476 EFI_STATUS Status;\r
f3b80a8e 477 EFI_HANDLE FtwHandle;\r
478 \r
8a2d4996 479 //\r
480 // Allocate private data structure for SMM FTW protocol and do some initialization\r
481 //\r
f3b80a8e 482 Status = InitFtwDevice (&mFtwDevice);\r
8a2d4996 483 if (EFI_ERROR(Status)) {\r
484 return Status;\r
485 }\r
486 \r
487 //\r
488 // Register FvbNotificationEvent () notify function.\r
489 // \r
490 Status = gSmst->SmmRegisterProtocolNotify (\r
491 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
492 FvbNotificationEvent,\r
493 &mFvbRegistration\r
494 );\r
495 ASSERT_EFI_ERROR (Status);\r
496\r
497 FvbNotificationEvent (NULL, NULL, NULL);\r
f3b80a8e 498\r
499 ///\r
500 /// Register SMM FTW SMI handler\r
501 ///\r
502 Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &FtwHandle);\r
503 ASSERT_EFI_ERROR (Status);\r
8a2d4996 504 \r
505 return EFI_SUCCESS;\r
506}\r