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