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