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