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