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