]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / FaultTolerantWriteDxe / FaultTolerantWriteSmm.c
... / ...
CommitLineData
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
43 Caution: This module requires additional review when modified.\r
44 This driver need to make sure the CommBuffer is not in the SMRAM range.\r
45\r
46Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
47SPDX-License-Identifier: BSD-2-Clause-Patent\r
48\r
49**/\r
50\r
51#include <PiMm.h>\r
52#include <Library/MmServicesTableLib.h>\r
53#include <Library/BaseLib.h>\r
54#include <Protocol/SmmSwapAddressRange.h>\r
55#include "FaultTolerantWrite.h"\r
56#include "FaultTolerantWriteSmmCommon.h"\r
57#include <Protocol/MmEndOfDxe.h>\r
58\r
59VOID *mFvbRegistration = NULL;\r
60EFI_FTW_DEVICE *mFtwDevice = NULL;\r
61\r
62///\r
63/// The flag to indicate whether the platform has left the DXE phase of execution.\r
64///\r
65BOOLEAN mEndOfDxe = FALSE;\r
66\r
67/**\r
68 Retrieve the SMM FVB protocol interface by HANDLE.\r
69\r
70 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for\r
71 reading, writing, and erasing the target block.\r
72 @param[out] FvBlock The interface of SMM FVB protocol\r
73\r
74 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
75 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.\r
76 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
77\r
78**/\r
79EFI_STATUS\r
80FtwGetFvbByHandle (\r
81 IN EFI_HANDLE FvBlockHandle,\r
82 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
83 )\r
84{\r
85 //\r
86 // To get the SMM FVB protocol interface on the handle\r
87 //\r
88 return gMmst->MmHandleProtocol (\r
89 FvBlockHandle,\r
90 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
91 (VOID **)FvBlock\r
92 );\r
93}\r
94\r
95/**\r
96 Retrieve the SMM Swap Address Range protocol interface.\r
97\r
98 @param[out] SarProtocol The interface of SMM SAR protocol\r
99\r
100 @retval EFI_SUCCESS The SMM SAR protocol instance was found and returned in SarProtocol.\r
101 @retval EFI_NOT_FOUND The SMM SAR protocol instance was not found.\r
102 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
103\r
104**/\r
105EFI_STATUS\r
106FtwGetSarProtocol (\r
107 OUT VOID **SarProtocol\r
108 )\r
109{\r
110 EFI_STATUS Status;\r
111\r
112 //\r
113 // Locate Smm Swap Address Range protocol\r
114 //\r
115 Status = gMmst->MmLocateProtocol (\r
116 &gEfiSmmSwapAddressRangeProtocolGuid,\r
117 NULL,\r
118 SarProtocol\r
119 );\r
120 return Status;\r
121}\r
122\r
123/**\r
124 Function returns an array of handles that support the SMM FVB protocol\r
125 in a buffer allocated from pool.\r
126\r
127 @param[out] NumberHandles The number of handles returned in Buffer.\r
128 @param[out] Buffer A pointer to the buffer to return the requested\r
129 array of handles that support SMM FVB protocol.\r
130\r
131 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
132 handles in Buffer was returned in NumberHandles.\r
133 @retval EFI_NOT_FOUND No SMM FVB handle was found.\r
134 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
135 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
136\r
137**/\r
138EFI_STATUS\r
139GetFvbCountAndBuffer (\r
140 OUT UINTN *NumberHandles,\r
141 OUT EFI_HANDLE **Buffer\r
142 )\r
143{\r
144 EFI_STATUS Status;\r
145 UINTN BufferSize;\r
146\r
147 if ((NumberHandles == NULL) || (Buffer == NULL)) {\r
148 return EFI_INVALID_PARAMETER;\r
149 }\r
150\r
151 BufferSize = 0;\r
152 *NumberHandles = 0;\r
153 *Buffer = NULL;\r
154 Status = gMmst->MmLocateHandle (\r
155 ByProtocol,\r
156 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
157 NULL,\r
158 &BufferSize,\r
159 *Buffer\r
160 );\r
161 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {\r
162 return EFI_NOT_FOUND;\r
163 }\r
164\r
165 *Buffer = AllocatePool (BufferSize);\r
166 if (*Buffer == NULL) {\r
167 return EFI_OUT_OF_RESOURCES;\r
168 }\r
169\r
170 Status = gMmst->MmLocateHandle (\r
171 ByProtocol,\r
172 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
173 NULL,\r
174 &BufferSize,\r
175 *Buffer\r
176 );\r
177\r
178 *NumberHandles = BufferSize / sizeof (EFI_HANDLE);\r
179 if (EFI_ERROR (Status)) {\r
180 *NumberHandles = 0;\r
181 FreePool (*Buffer);\r
182 *Buffer = NULL;\r
183 }\r
184\r
185 return Status;\r
186}\r
187\r
188/**\r
189 Get the handle of the SMM FVB protocol by the FVB base address and attributes.\r
190\r
191 @param[in] Address The base address of SMM FVB protocol.\r
192 @param[in] Attributes The attributes of the SMM FVB protocol.\r
193 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.\r
194\r
195 @retval EFI_SUCCESS The FVB handle is found.\r
196 @retval EFI_ABORTED The FVB protocol is not found.\r
197\r
198**/\r
199EFI_STATUS\r
200GetFvbByAddressAndAttribute (\r
201 IN EFI_PHYSICAL_ADDRESS Address,\r
202 IN EFI_FVB_ATTRIBUTES_2 Attributes,\r
203 OUT EFI_HANDLE *SmmFvbHandle\r
204 )\r
205{\r
206 EFI_STATUS Status;\r
207 EFI_HANDLE *HandleBuffer;\r
208 UINTN HandleCount;\r
209 UINTN Index;\r
210 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
211 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
212 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
213\r
214 HandleBuffer = NULL;\r
215\r
216 //\r
217 // Locate all handles of SMM Fvb protocol.\r
218 //\r
219 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
220 if (EFI_ERROR (Status)) {\r
221 return EFI_ABORTED;\r
222 }\r
223\r
224 //\r
225 // Find the proper SMM Fvb handle by the address and attributes.\r
226 //\r
227 for (Index = 0; Index < HandleCount; Index++) {\r
228 Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);\r
229 if (EFI_ERROR (Status)) {\r
230 break;\r
231 }\r
232\r
233 //\r
234 // Compare the address.\r
235 //\r
236 Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r
237 if (EFI_ERROR (Status)) {\r
238 continue;\r
239 }\r
240\r
241 if (Address != FvbBaseAddress) {\r
242 continue;\r
243 }\r
244\r
245 //\r
246 // Compare the attribute.\r
247 //\r
248 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
249 if (EFI_ERROR (Status)) {\r
250 continue;\r
251 }\r
252\r
253 if (Attributes != FvbAttributes) {\r
254 continue;\r
255 }\r
256\r
257 //\r
258 // Found the proper FVB handle.\r
259 //\r
260 *SmmFvbHandle = HandleBuffer[Index];\r
261 FreePool (HandleBuffer);\r
262 return EFI_SUCCESS;\r
263 }\r
264\r
265 FreePool (HandleBuffer);\r
266 return EFI_ABORTED;\r
267}\r
268\r
269/**\r
270 Communication service SMI Handler entry.\r
271\r
272 This SMI handler provides services for the fault tolerant write wrapper driver.\r
273\r
274 Caution: This function requires additional review when modified.\r
275 This driver need to make sure the CommBuffer is not in the SMRAM range.\r
276 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +\r
277 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.\r
278\r
279 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
280 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
281 handler was registered.\r
282 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed\r
283 from a non-SMM environment into an SMM environment.\r
284 @param[in, out] CommBufferSize The size of the CommBuffer.\r
285\r
286 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
287 should still be called.\r
288 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should\r
289 still be called.\r
290 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still\r
291 be called.\r
292 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
293\r
294**/\r
295EFI_STATUS\r
296EFIAPI\r
297SmmFaultTolerantWriteHandler (\r
298 IN EFI_HANDLE DispatchHandle,\r
299 IN CONST VOID *RegisterContext,\r
300 IN OUT VOID *CommBuffer,\r
301 IN OUT UINTN *CommBufferSize\r
302 )\r
303{\r
304 EFI_STATUS Status;\r
305 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;\r
306 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmGetMaxBlockSizeHeader;\r
307 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;\r
308 SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;\r
309 SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;\r
310 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;\r
311 VOID *PrivateData;\r
312 EFI_HANDLE SmmFvbHandle;\r
313 UINTN InfoSize;\r
314 UINTN CommBufferPayloadSize;\r
315 UINTN PrivateDataSize;\r
316 UINTN Length;\r
317 UINTN TempCommBufferSize;\r
318\r
319 //\r
320 // If input is invalid, stop processing this SMI\r
321 //\r
322 if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {\r
323 return EFI_SUCCESS;\r
324 }\r
325\r
326 TempCommBufferSize = *CommBufferSize;\r
327\r
328 if (TempCommBufferSize < SMM_FTW_COMMUNICATE_HEADER_SIZE) {\r
329 DEBUG ((DEBUG_ERROR, "SmmFtwHandler: SMM communication buffer size invalid!\n"));\r
330 return EFI_SUCCESS;\r
331 }\r
332\r
333 CommBufferPayloadSize = TempCommBufferSize - SMM_FTW_COMMUNICATE_HEADER_SIZE;\r
334\r
335 if (!FtwSmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
336 DEBUG ((DEBUG_ERROR, "SmmFtwHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
337 return EFI_SUCCESS;\r
338 }\r
339\r
340 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;\r
341\r
342 if (mEndOfDxe) {\r
343 //\r
344 // It will be not safe to expose the operations after End Of Dxe.\r
345 //\r
346 DEBUG ((DEBUG_ERROR, "SmmFtwHandler: Not safe to do the operation: %x after End Of Dxe, so access denied!\n", SmmFtwFunctionHeader->Function));\r
347 SmmFtwFunctionHeader->ReturnStatus = EFI_ACCESS_DENIED;\r
348 return EFI_SUCCESS;\r
349 }\r
350\r
351 switch (SmmFtwFunctionHeader->Function) {\r
352 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:\r
353 if (CommBufferPayloadSize < sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER)) {\r
354 DEBUG ((DEBUG_ERROR, "GetMaxBlockSize: SMM communication buffer size invalid!\n"));\r
355 return EFI_SUCCESS;\r
356 }\r
357\r
358 SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *)SmmFtwFunctionHeader->Data;\r
359\r
360 Status = FtwGetMaxBlockSize (\r
361 &mFtwDevice->FtwInstance,\r
362 &SmmGetMaxBlockSizeHeader->BlockSize\r
363 );\r
364 break;\r
365\r
366 case FTW_FUNCTION_ALLOCATE:\r
367 if (CommBufferPayloadSize < sizeof (SMM_FTW_ALLOCATE_HEADER)) {\r
368 DEBUG ((DEBUG_ERROR, "Allocate: SMM communication buffer size invalid!\n"));\r
369 return EFI_SUCCESS;\r
370 }\r
371\r
372 SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *)SmmFtwFunctionHeader->Data;\r
373 Status = FtwAllocate (\r
374 &mFtwDevice->FtwInstance,\r
375 &SmmFtwAllocateHeader->CallerId,\r
376 SmmFtwAllocateHeader->PrivateDataSize,\r
377 SmmFtwAllocateHeader->NumberOfWrites\r
378 );\r
379 break;\r
380\r
381 case FTW_FUNCTION_WRITE:\r
382 if (CommBufferPayloadSize < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data)) {\r
383 DEBUG ((DEBUG_ERROR, "Write: SMM communication buffer size invalid!\n"));\r
384 return EFI_SUCCESS;\r
385 }\r
386\r
387 SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *)SmmFtwFunctionHeader->Data;\r
388 Length = SmmFtwWriteHeader->Length;\r
389 PrivateDataSize = SmmFtwWriteHeader->PrivateDataSize;\r
390 if (((UINTN)(~0) - Length < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data)) ||\r
391 ((UINTN)(~0) - PrivateDataSize < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length))\r
392 {\r
393 //\r
394 // Prevent InfoSize overflow\r
395 //\r
396 Status = EFI_ACCESS_DENIED;\r
397 break;\r
398 }\r
399\r
400 InfoSize = OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length + PrivateDataSize;\r
401\r
402 //\r
403 // SMRAM range check already covered before\r
404 //\r
405 if (InfoSize > CommBufferPayloadSize) {\r
406 DEBUG ((DEBUG_ERROR, "Write: Data size exceed communication buffer size limit!\n"));\r
407 Status = EFI_ACCESS_DENIED;\r
408 break;\r
409 }\r
410\r
411 if (PrivateDataSize == 0) {\r
412 PrivateData = NULL;\r
413 } else {\r
414 PrivateData = (VOID *)&SmmFtwWriteHeader->Data[Length];\r
415 }\r
416\r
417 Status = GetFvbByAddressAndAttribute (\r
418 SmmFtwWriteHeader->FvbBaseAddress,\r
419 SmmFtwWriteHeader->FvbAttributes,\r
420 &SmmFvbHandle\r
421 );\r
422 if (!EFI_ERROR (Status)) {\r
423 //\r
424 // The SpeculationBarrier() call here is to ensure the previous\r
425 // range/content checks for the CommBuffer have been completed before\r
426 // calling into FtwWrite().\r
427 //\r
428 SpeculationBarrier ();\r
429 Status = FtwWrite (\r
430 &mFtwDevice->FtwInstance,\r
431 SmmFtwWriteHeader->Lba,\r
432 SmmFtwWriteHeader->Offset,\r
433 Length,\r
434 PrivateData,\r
435 SmmFvbHandle,\r
436 SmmFtwWriteHeader->Data\r
437 );\r
438 }\r
439\r
440 break;\r
441\r
442 case FTW_FUNCTION_RESTART:\r
443 if (CommBufferPayloadSize < sizeof (SMM_FTW_RESTART_HEADER)) {\r
444 DEBUG ((DEBUG_ERROR, "Restart: SMM communication buffer size invalid!\n"));\r
445 return EFI_SUCCESS;\r
446 }\r
447\r
448 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *)SmmFtwFunctionHeader->Data;\r
449 Status = GetFvbByAddressAndAttribute (\r
450 SmmFtwRestartHeader->FvbBaseAddress,\r
451 SmmFtwRestartHeader->FvbAttributes,\r
452 &SmmFvbHandle\r
453 );\r
454 if (!EFI_ERROR (Status)) {\r
455 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);\r
456 }\r
457\r
458 break;\r
459\r
460 case FTW_FUNCTION_ABORT:\r
461 Status = FtwAbort (&mFtwDevice->FtwInstance);\r
462 break;\r
463\r
464 case FTW_FUNCTION_GET_LAST_WRITE:\r
465 if (CommBufferPayloadSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)) {\r
466 DEBUG ((DEBUG_ERROR, "GetLastWrite: SMM communication buffer size invalid!\n"));\r
467 return EFI_SUCCESS;\r
468 }\r
469\r
470 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *)SmmFtwFunctionHeader->Data;\r
471 PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;\r
472 if ((UINTN)(~0) - PrivateDataSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)) {\r
473 //\r
474 // Prevent InfoSize overflow\r
475 //\r
476 Status = EFI_ACCESS_DENIED;\r
477 break;\r
478 }\r
479\r
480 InfoSize = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + PrivateDataSize;\r
481\r
482 //\r
483 // SMRAM range check already covered before\r
484 //\r
485 if (InfoSize > CommBufferPayloadSize) {\r
486 DEBUG ((DEBUG_ERROR, "Data size exceed communication buffer size limit!\n"));\r
487 Status = EFI_ACCESS_DENIED;\r
488 break;\r
489 }\r
490\r
491 Status = FtwGetLastWrite (\r
492 &mFtwDevice->FtwInstance,\r
493 &SmmFtwGetLastWriteHeader->CallerId,\r
494 &SmmFtwGetLastWriteHeader->Lba,\r
495 &SmmFtwGetLastWriteHeader->Offset,\r
496 &SmmFtwGetLastWriteHeader->Length,\r
497 &PrivateDataSize,\r
498 (VOID *)SmmFtwGetLastWriteHeader->Data,\r
499 &SmmFtwGetLastWriteHeader->Complete\r
500 );\r
501 SmmFtwGetLastWriteHeader->PrivateDataSize = PrivateDataSize;\r
502 break;\r
503\r
504 default:\r
505 Status = EFI_UNSUPPORTED;\r
506 }\r
507\r
508 SmmFtwFunctionHeader->ReturnStatus = Status;\r
509\r
510 return EFI_SUCCESS;\r
511}\r
512\r
513/**\r
514 SMM Firmware Volume Block Protocol notification event handler.\r
515\r
516 @param[in] Protocol Points to the protocol's unique identifier\r
517 @param[in] Interface Points to the interface instance\r
518 @param[in] Handle The handle on which the interface was installed\r
519\r
520 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
521\r
522 **/\r
523EFI_STATUS\r
524EFIAPI\r
525FvbNotificationEvent (\r
526 IN CONST EFI_GUID *Protocol,\r
527 IN VOID *Interface,\r
528 IN EFI_HANDLE Handle\r
529 )\r
530{\r
531 EFI_STATUS Status;\r
532 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
533 EFI_HANDLE SmmFtwHandle;\r
534\r
535 //\r
536 // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
537 // if SMM Fault Tolerant Write protocol had been installed.\r
538 //\r
539 Status = gMmst->MmLocateProtocol (\r
540 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
541 NULL,\r
542 (VOID **)&FtwProtocol\r
543 );\r
544 if (!EFI_ERROR (Status)) {\r
545 return EFI_SUCCESS;\r
546 }\r
547\r
548 //\r
549 // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
550 //\r
551 Status = InitFtwProtocol (mFtwDevice);\r
552 if (EFI_ERROR (Status)) {\r
553 return Status;\r
554 }\r
555\r
556 //\r
557 // Install protocol interface\r
558 //\r
559 Status = gMmst->MmInstallProtocolInterface (\r
560 &mFtwDevice->Handle,\r
561 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
562 EFI_NATIVE_INTERFACE,\r
563 &mFtwDevice->FtwInstance\r
564 );\r
565 ASSERT_EFI_ERROR (Status);\r
566\r
567 ///\r
568 /// Register SMM FTW SMI handler\r
569 ///\r
570 Status = gMmst->MmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &SmmFtwHandle);\r
571 ASSERT_EFI_ERROR (Status);\r
572\r
573 //\r
574 // Notify the Ftw wrapper driver SMM Ftw is ready\r
575 //\r
576 FtwNotifySmmReady ();\r
577\r
578 return EFI_SUCCESS;\r
579}\r
580\r
581/**\r
582 SMM END_OF_DXE protocol notification event handler.\r
583\r
584 @param Protocol Points to the protocol's unique identifier\r
585 @param Interface Points to the interface instance\r
586 @param Handle The handle on which the interface was installed\r
587\r
588 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
589\r
590**/\r
591EFI_STATUS\r
592EFIAPI\r
593MmEndOfDxeCallback (\r
594 IN CONST EFI_GUID *Protocol,\r
595 IN VOID *Interface,\r
596 IN EFI_HANDLE Handle\r
597 )\r
598{\r
599 mEndOfDxe = TRUE;\r
600 return EFI_SUCCESS;\r
601}\r
602\r
603/**\r
604 Shared entry point of the module\r
605\r
606 @retval EFI_SUCCESS The initialization finished successfully.\r
607 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
608 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r
609**/\r
610EFI_STATUS\r
611MmFaultTolerantWriteInitialize (\r
612 VOID\r
613 )\r
614{\r
615 EFI_STATUS Status;\r
616 VOID *MmEndOfDxeRegistration;\r
617\r
618 //\r
619 // Allocate private data structure for SMM FTW protocol and do some initialization\r
620 //\r
621 Status = InitFtwDevice (&mFtwDevice);\r
622 if (EFI_ERROR (Status)) {\r
623 return Status;\r
624 }\r
625\r
626 //\r
627 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
628 //\r
629 Status = gMmst->MmRegisterProtocolNotify (\r
630 &gEfiMmEndOfDxeProtocolGuid,\r
631 MmEndOfDxeCallback,\r
632 &MmEndOfDxeRegistration\r
633 );\r
634 ASSERT_EFI_ERROR (Status);\r
635\r
636 //\r
637 // Register FvbNotificationEvent () notify function.\r
638 //\r
639 Status = gMmst->MmRegisterProtocolNotify (\r
640 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
641 FvbNotificationEvent,\r
642 &mFvbRegistration\r
643 );\r
644 ASSERT_EFI_ERROR (Status);\r
645\r
646 FvbNotificationEvent (NULL, NULL, NULL);\r
647\r
648 return EFI_SUCCESS;\r
649}\r