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