]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
MdeModulePkg: stop abusing EFI_EVENT for protocol notify registration
[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/**\r
190 Get the handle of the SMM FVB protocol by the FVB base address and attributes.\r
191\r
192 @param[in] Address The base address of SMM FVB protocol.\r
193 @param[in] Attributes The attributes of the SMM FVB protocol.\r
194 @param[out] SmmFvbHandle The handle of the SMM FVB protocol.\r
195\r
196 @retval EFI_SUCCESS The FVB handle is found.\r
197 @retval EFI_ABORTED The FVB protocol is not found.\r
198\r
199**/\r
200EFI_STATUS\r
201GetFvbByAddressAndAttribute (\r
202 IN EFI_PHYSICAL_ADDRESS Address,\r
203 IN EFI_FVB_ATTRIBUTES_2 Attributes,\r
204 OUT EFI_HANDLE *SmmFvbHandle\r
205 )\r
206{\r
207 EFI_STATUS Status;\r
208 EFI_HANDLE *HandleBuffer;\r
209 UINTN HandleCount;\r
210 UINTN Index;\r
211 EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r
212 EFI_FVB_ATTRIBUTES_2 FvbAttributes;\r
213 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r
214\r
215 HandleBuffer = NULL;\r
216\r
217 //\r
218 // Locate all handles of SMM Fvb protocol.\r
219 //\r
220 Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r
221 if (EFI_ERROR (Status)) {\r
222 return EFI_ABORTED;\r
223 }\r
224\r
225 //\r
226 // Find the proper SMM Fvb handle by the address and attributes.\r
227 //\r
228 for (Index = 0; Index < HandleCount; Index++) {\r
229 Status = FtwGetFvbByHandle (HandleBuffer[Index], &Fvb);\r
230 if (EFI_ERROR (Status)) {\r
231 break;\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 if (Address != FvbBaseAddress) {\r
241 continue;\r
242 }\r
243\r
244 //\r
245 // Compare the attribute.\r
246 //\r
247 Status = Fvb->GetAttributes (Fvb, &FvbAttributes);\r
248 if (EFI_ERROR (Status)) {\r
249 continue;\r
250 }\r
251 if (Attributes != FvbAttributes) {\r
252 continue;\r
253 }\r
254\r
255 //\r
256 // Found the proper FVB handle.\r
257 //\r
258 *SmmFvbHandle = HandleBuffer[Index];\r
259 FreePool (HandleBuffer);\r
260 return EFI_SUCCESS;\r
261 }\r
262\r
263 FreePool (HandleBuffer);\r
264 return EFI_ABORTED;\r
265}\r
266\r
267/**\r
268 Communication service SMI Handler entry.\r
269\r
270 This SMI handler provides services for the fault tolerant write wrapper driver.\r
271\r
272 Caution: This function requires additional review when modified.\r
273 This driver need to make sure the CommBuffer is not in the SMRAM range.\r
274 Also in FTW_FUNCTION_GET_LAST_WRITE case, check SmmFtwGetLastWriteHeader->Data +\r
275 SmmFtwGetLastWriteHeader->PrivateDataSize within communication buffer.\r
276\r
277 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
278 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
279 handler was registered.\r
280 @param[in, out] CommBuffer A pointer to a collection of data in memory that will be conveyed\r
281 from a non-SMM environment into an SMM environment.\r
282 @param[in, out] CommBufferSize The size of the CommBuffer.\r
283\r
284 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
285 should still be called.\r
286 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should\r
287 still be called.\r
288 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still\r
289 be called.\r
290 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
291\r
292**/\r
293EFI_STATUS\r
294EFIAPI\r
295SmmFaultTolerantWriteHandler (\r
296 IN EFI_HANDLE DispatchHandle,\r
297 IN CONST VOID *RegisterContext,\r
298 IN OUT VOID *CommBuffer,\r
299 IN OUT UINTN *CommBufferSize\r
300 )\r
301{\r
302 EFI_STATUS Status;\r
303 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;\r
304 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmGetMaxBlockSizeHeader;\r
305 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;\r
306 SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;\r
307 SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;\r
308 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;\r
309 VOID *PrivateData;\r
310 EFI_HANDLE SmmFvbHandle;\r
311 UINTN InfoSize;\r
312 UINTN CommBufferPayloadSize;\r
313 UINTN PrivateDataSize;\r
314 UINTN Length;\r
315 UINTN TempCommBufferSize;\r
316\r
317 //\r
318 // If input is invalid, stop processing this SMI\r
319 //\r
320 if (CommBuffer == NULL || CommBufferSize == NULL) {\r
321 return EFI_SUCCESS;\r
322 }\r
323\r
324 TempCommBufferSize = *CommBufferSize;\r
325\r
326 if (TempCommBufferSize < SMM_FTW_COMMUNICATE_HEADER_SIZE) {\r
327 DEBUG ((EFI_D_ERROR, "SmmFtwHandler: SMM communication buffer size invalid!\n"));\r
328 return EFI_SUCCESS;\r
329 }\r
330 CommBufferPayloadSize = TempCommBufferSize - SMM_FTW_COMMUNICATE_HEADER_SIZE;\r
331\r
332 if (!FtwSmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
333 DEBUG ((EFI_D_ERROR, "SmmFtwHandler: SMM communication buffer in SMRAM or overflow!\n"));\r
334 return EFI_SUCCESS;\r
335 }\r
336\r
337 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)CommBuffer;\r
338\r
339 if (mEndOfDxe) {\r
340 //\r
341 // It will be not safe to expose the operations after End Of Dxe.\r
342 //\r
343 DEBUG ((EFI_D_ERROR, "SmmFtwHandler: Not safe to do the operation: %x after End Of Dxe, so access denied!\n", SmmFtwFunctionHeader->Function));\r
344 SmmFtwFunctionHeader->ReturnStatus = EFI_ACCESS_DENIED;\r
345 return EFI_SUCCESS;\r
346 }\r
347\r
348 switch (SmmFtwFunctionHeader->Function) {\r
349 case FTW_FUNCTION_GET_MAX_BLOCK_SIZE:\r
350 if (CommBufferPayloadSize < sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER)) {\r
351 DEBUG ((EFI_D_ERROR, "GetMaxBlockSize: SMM communication buffer size invalid!\n"));\r
352 return EFI_SUCCESS;\r
353 }\r
354 SmmGetMaxBlockSizeHeader = (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *) SmmFtwFunctionHeader->Data;\r
355\r
356 Status = FtwGetMaxBlockSize (\r
357 &mFtwDevice->FtwInstance,\r
358 &SmmGetMaxBlockSizeHeader->BlockSize\r
359 );\r
360 break;\r
361\r
362 case FTW_FUNCTION_ALLOCATE:\r
363 if (CommBufferPayloadSize < sizeof (SMM_FTW_ALLOCATE_HEADER)) {\r
364 DEBUG ((EFI_D_ERROR, "Allocate: SMM communication buffer size invalid!\n"));\r
365 return EFI_SUCCESS;\r
366 }\r
367 SmmFtwAllocateHeader = (SMM_FTW_ALLOCATE_HEADER *) SmmFtwFunctionHeader->Data;\r
368 Status = FtwAllocate (\r
369 &mFtwDevice->FtwInstance,\r
370 &SmmFtwAllocateHeader->CallerId,\r
371 SmmFtwAllocateHeader->PrivateDataSize,\r
372 SmmFtwAllocateHeader->NumberOfWrites\r
373 );\r
374 break;\r
375\r
376 case FTW_FUNCTION_WRITE:\r
377 if (CommBufferPayloadSize < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data)) {\r
378 DEBUG ((EFI_D_ERROR, "Write: SMM communication buffer size invalid!\n"));\r
379 return EFI_SUCCESS;\r
380 }\r
381 SmmFtwWriteHeader = (SMM_FTW_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
382 Length = SmmFtwWriteHeader->Length;\r
383 PrivateDataSize = SmmFtwWriteHeader->PrivateDataSize;\r
384 if (((UINTN)(~0) - Length < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data)) ||\r
385 ((UINTN)(~0) - PrivateDataSize < OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length)) {\r
386 //\r
387 // Prevent InfoSize overflow\r
388 //\r
389 Status = EFI_ACCESS_DENIED;\r
390 break;\r
391 }\r
392 InfoSize = OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length + PrivateDataSize;\r
393\r
394 //\r
395 // SMRAM range check already covered before\r
396 //\r
397 if (InfoSize > CommBufferPayloadSize) {\r
398 DEBUG ((EFI_D_ERROR, "Write: Data size exceed communication buffer size limit!\n"));\r
399 Status = EFI_ACCESS_DENIED;\r
400 break;\r
401 }\r
402\r
403 if (PrivateDataSize == 0) {\r
404 PrivateData = NULL;\r
405 } else {\r
406 PrivateData = (VOID *)&SmmFtwWriteHeader->Data[Length];\r
407 }\r
408 Status = GetFvbByAddressAndAttribute (\r
409 SmmFtwWriteHeader->FvbBaseAddress,\r
410 SmmFtwWriteHeader->FvbAttributes,\r
411 &SmmFvbHandle\r
412 );\r
413 if (!EFI_ERROR (Status)) {\r
414 //\r
415 // The SpeculationBarrier() call here is to ensure the previous\r
416 // range/content checks for the CommBuffer have been completed before\r
417 // calling into FtwWrite().\r
418 //\r
419 SpeculationBarrier ();\r
420 Status = FtwWrite(\r
421 &mFtwDevice->FtwInstance,\r
422 SmmFtwWriteHeader->Lba,\r
423 SmmFtwWriteHeader->Offset,\r
424 Length,\r
425 PrivateData,\r
426 SmmFvbHandle,\r
427 SmmFtwWriteHeader->Data\r
428 );\r
429 }\r
430 break;\r
431\r
432 case FTW_FUNCTION_RESTART:\r
433 if (CommBufferPayloadSize < sizeof (SMM_FTW_RESTART_HEADER)) {\r
434 DEBUG ((EFI_D_ERROR, "Restart: SMM communication buffer size invalid!\n"));\r
435 return EFI_SUCCESS;\r
436 }\r
437 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;\r
438 Status = GetFvbByAddressAndAttribute (\r
439 SmmFtwRestartHeader->FvbBaseAddress,\r
440 SmmFtwRestartHeader->FvbAttributes,\r
441 &SmmFvbHandle\r
442 );\r
443 if (!EFI_ERROR (Status)) {\r
444 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);\r
445 }\r
446 break;\r
447\r
448 case FTW_FUNCTION_ABORT:\r
449 Status = FtwAbort (&mFtwDevice->FtwInstance);\r
450 break;\r
451\r
452 case FTW_FUNCTION_GET_LAST_WRITE:\r
453 if (CommBufferPayloadSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)) {\r
454 DEBUG ((EFI_D_ERROR, "GetLastWrite: SMM communication buffer size invalid!\n"));\r
455 return EFI_SUCCESS;\r
456 }\r
457 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
458 PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;\r
459 if ((UINTN)(~0) - PrivateDataSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)){\r
460 //\r
461 // Prevent InfoSize overflow\r
462 //\r
463 Status = EFI_ACCESS_DENIED;\r
464 break;\r
465 }\r
466 InfoSize = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + PrivateDataSize;\r
467\r
468 //\r
469 // SMRAM range check already covered before\r
470 //\r
471 if (InfoSize > CommBufferPayloadSize) {\r
472 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
473 Status = EFI_ACCESS_DENIED;\r
474 break;\r
475 }\r
476\r
477 Status = FtwGetLastWrite (\r
478 &mFtwDevice->FtwInstance,\r
479 &SmmFtwGetLastWriteHeader->CallerId,\r
480 &SmmFtwGetLastWriteHeader->Lba,\r
481 &SmmFtwGetLastWriteHeader->Offset,\r
482 &SmmFtwGetLastWriteHeader->Length,\r
483 &PrivateDataSize,\r
484 (VOID *)SmmFtwGetLastWriteHeader->Data,\r
485 &SmmFtwGetLastWriteHeader->Complete\r
486 );\r
487 SmmFtwGetLastWriteHeader->PrivateDataSize = PrivateDataSize;\r
488 break;\r
489\r
490 default:\r
491 Status = EFI_UNSUPPORTED;\r
492 }\r
493\r
494 SmmFtwFunctionHeader->ReturnStatus = Status;\r
495\r
496 return EFI_SUCCESS;\r
497}\r
498\r
499\r
500/**\r
501 SMM Firmware Volume Block Protocol notification event handler.\r
502\r
503 @param[in] Protocol Points to the protocol's unique identifier\r
504 @param[in] Interface Points to the interface instance\r
505 @param[in] Handle The handle on which the interface was installed\r
506\r
507 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
508\r
509 **/\r
510EFI_STATUS\r
511EFIAPI\r
512FvbNotificationEvent (\r
513 IN CONST EFI_GUID *Protocol,\r
514 IN VOID *Interface,\r
515 IN EFI_HANDLE Handle\r
516 )\r
517{\r
518 EFI_STATUS Status;\r
519 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
520 EFI_HANDLE SmmFtwHandle;\r
521\r
522 //\r
523 // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
524 // if SMM Fault Tolerant Write protocol had been installed.\r
525 //\r
526 Status = gMmst->MmLocateProtocol (\r
527 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
528 NULL,\r
529 (VOID **) &FtwProtocol\r
530 );\r
531 if (!EFI_ERROR (Status)) {\r
532 return EFI_SUCCESS;\r
533 }\r
534\r
535 //\r
536 // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
537 //\r
538 Status = InitFtwProtocol (mFtwDevice);\r
539 if (EFI_ERROR(Status)) {\r
540 return Status;\r
541 }\r
542\r
543 //\r
544 // Install protocol interface\r
545 //\r
546 Status = gMmst->MmInstallProtocolInterface (\r
547 &mFtwDevice->Handle,\r
548 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
549 EFI_NATIVE_INTERFACE,\r
550 &mFtwDevice->FtwInstance\r
551 );\r
552 ASSERT_EFI_ERROR (Status);\r
553\r
554 ///\r
555 /// Register SMM FTW SMI handler\r
556 ///\r
557 Status = gMmst->MmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &SmmFtwHandle);\r
558 ASSERT_EFI_ERROR (Status);\r
559\r
560 //\r
561 // Notify the Ftw wrapper driver SMM Ftw is ready\r
562 //\r
563 FtwNotifySmmReady ();\r
564\r
565 return EFI_SUCCESS;\r
566}\r
567\r
568/**\r
569 SMM END_OF_DXE protocol notification event handler.\r
570\r
571 @param Protocol Points to the protocol's unique identifier\r
572 @param Interface Points to the interface instance\r
573 @param Handle The handle on which the interface was installed\r
574\r
575 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
576\r
577**/\r
578EFI_STATUS\r
579EFIAPI\r
580MmEndOfDxeCallback (\r
581 IN CONST EFI_GUID *Protocol,\r
582 IN VOID *Interface,\r
583 IN EFI_HANDLE Handle\r
584 )\r
585{\r
586 mEndOfDxe = TRUE;\r
587 return EFI_SUCCESS;\r
588}\r
589\r
590/**\r
591 Shared entry point of the module\r
592\r
593 @retval EFI_SUCCESS The initialization finished successfully.\r
594 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
595 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r
596**/\r
597EFI_STATUS\r
598MmFaultTolerantWriteInitialize (\r
599 VOID\r
600 )\r
601{\r
602 EFI_STATUS Status;\r
603 VOID *MmEndOfDxeRegistration;\r
604\r
605 //\r
606 // Allocate private data structure for SMM FTW protocol and do some initialization\r
607 //\r
608 Status = InitFtwDevice (&mFtwDevice);\r
609 if (EFI_ERROR(Status)) {\r
610 return Status;\r
611 }\r
612\r
613 //\r
614 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
615 //\r
616 Status = gMmst->MmRegisterProtocolNotify (\r
617 &gEfiMmEndOfDxeProtocolGuid,\r
618 MmEndOfDxeCallback,\r
619 &MmEndOfDxeRegistration\r
620 );\r
621 ASSERT_EFI_ERROR (Status);\r
622\r
623 //\r
624 // Register FvbNotificationEvent () notify function.\r
625 //\r
626 Status = gMmst->MmRegisterProtocolNotify (\r
627 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
628 FvbNotificationEvent,\r
629 &mFvbRegistration\r
630 );\r
631 ASSERT_EFI_ERROR (Status);\r
632\r
633 FvbNotificationEvent (NULL, NULL, NULL);\r
634\r
635 return EFI_SUCCESS;\r
636}\r