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