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