]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
MdeModulePkg/FaultTolerantWrite:[CVE-2017-5753]Fix bounds check bypass
[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
d1102dba
LG
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
8a2d4996 9\r
d1102dba 10 The implementation uses an FTW (Fault Tolerant Write) Work Space.\r
8a2d4996 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
d1102dba 13\r
8a2d4996 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
d1102dba 16\r
8a2d4996 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
d1102dba 30 1. The write NumBytes data must be fit within Spare area.\r
8a2d4996 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
d1102dba 34 4. Working Block area must be in the single one Firmware Volume Block range which FVB protocol is produced on.\r
8a2d4996 35 5. Spare area must be in the single one Firmware Volume Block range which FVB protocol is produced on.\r
d1102dba 36 6. Any write data area (SpareAreaLength Area) which the data will be written into must be\r
8a2d4996 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 43 Caution: This module requires additional review when modified.\r
d1102dba
LG
44 This driver need to make sure the CommBuffer is not in the SMRAM range.\r
45\r
46Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
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
c219324c 51\r
d1102dba
LG
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
8a2d4996 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
cb54cd24 60#include <Library/BaseLib.h>\r
8a2d4996 61#include <Protocol/SmmSwapAddressRange.h>\r
f3b80a8e 62#include "FaultTolerantWrite.h"\r
63#include "FaultTolerantWriteSmmCommon.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
0a18956d 75 Retrieve the SMM FVB protocol interface by HANDLE.\r
8a2d4996 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
0a18956d 103 Retrieve the SMM Swap Address Range protocol interface.\r
8a2d4996 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
d1102dba
LG
123 &gEfiSmmSwapAddressRangeProtocolGuid,\r
124 NULL,\r
8a2d4996 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
d1102dba 132 in a buffer allocated from pool.\r
8a2d4996 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
d1102dba 231\r
f3b80a8e 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 279 Caution: This function requires additional review when modified.\r
d1102dba
LG
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
c219324c
ED
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
d1102dba 291 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
f3b80a8e 292 should still be called.\r
d1102dba 293 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should\r
f3b80a8e 294 still be called.\r
d1102dba 295 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still\r
f3b80a8e 296 be called.\r
297 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
d1102dba 298\r
f3b80a8e 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
d1102dba 368\r
f3b80a8e 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
d1102dba 382\r
f3b80a8e 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
d1102dba 416 SmmFtwWriteHeader->FvbBaseAddress,\r
f3b80a8e 417 SmmFtwWriteHeader->FvbAttributes,\r
418 &SmmFvbHandle\r
419 );\r
420 if (!EFI_ERROR (Status)) {\r
cb54cd24
HW
421 //\r
422 // The AsmLfence() call here is to ensure the previous range/content\r
423 // checks for the CommBuffer have been completed before calling into\r
424 // FtwWrite().\r
425 //\r
426 AsmLfence ();\r
f3b80a8e 427 Status = FtwWrite(\r
428 &mFtwDevice->FtwInstance,\r
429 SmmFtwWriteHeader->Lba,\r
430 SmmFtwWriteHeader->Offset,\r
5e5bb2a9 431 Length,\r
f3b80a8e 432 PrivateData,\r
433 SmmFvbHandle,\r
434 SmmFtwWriteHeader->Data\r
435 );\r
436 }\r
437 break;\r
d1102dba 438\r
f3b80a8e 439 case FTW_FUNCTION_RESTART:\r
5e5bb2a9
SZ
440 if (CommBufferPayloadSize < sizeof (SMM_FTW_RESTART_HEADER)) {\r
441 DEBUG ((EFI_D_ERROR, "Restart: SMM communication buffer size invalid!\n"));\r
442 return EFI_SUCCESS;\r
443 }\r
f3b80a8e 444 SmmFtwRestartHeader = (SMM_FTW_RESTART_HEADER *) SmmFtwFunctionHeader->Data;\r
445 Status = GetFvbByAddressAndAttribute (\r
d1102dba 446 SmmFtwRestartHeader->FvbBaseAddress,\r
f3b80a8e 447 SmmFtwRestartHeader->FvbAttributes,\r
448 &SmmFvbHandle\r
d1102dba 449 );\r
f3b80a8e 450 if (!EFI_ERROR (Status)) {\r
451 Status = FtwRestart (&mFtwDevice->FtwInstance, SmmFvbHandle);\r
452 }\r
453 break;\r
454\r
455 case FTW_FUNCTION_ABORT:\r
456 Status = FtwAbort (&mFtwDevice->FtwInstance);\r
457 break;\r
d1102dba 458\r
f3b80a8e 459 case FTW_FUNCTION_GET_LAST_WRITE:\r
5e5bb2a9
SZ
460 if (CommBufferPayloadSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)) {\r
461 DEBUG ((EFI_D_ERROR, "GetLastWrite: SMM communication buffer size invalid!\n"));\r
462 return EFI_SUCCESS;\r
463 }\r
f3b80a8e 464 SmmFtwGetLastWriteHeader = (SMM_FTW_GET_LAST_WRITE_HEADER *) SmmFtwFunctionHeader->Data;\r
5e5bb2a9
SZ
465 PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;\r
466 if ((UINTN)(~0) - PrivateDataSize < OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data)){\r
f07268bd
SZ
467 //\r
468 // Prevent InfoSize overflow\r
469 //\r
470 Status = EFI_ACCESS_DENIED;\r
471 break;\r
472 }\r
5e5bb2a9 473 InfoSize = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + PrivateDataSize;\r
7ea4cf3f
SZ
474\r
475 //\r
476 // SMRAM range check already covered before\r
477 //\r
5e5bb2a9 478 if (InfoSize > CommBufferPayloadSize) {\r
7ea4cf3f
SZ
479 DEBUG ((EFI_D_ERROR, "Data size exceed communication buffer size limit!\n"));\r
480 Status = EFI_ACCESS_DENIED;\r
481 break;\r
c219324c 482 }\r
7ea4cf3f
SZ
483\r
484 Status = FtwGetLastWrite (\r
485 &mFtwDevice->FtwInstance,\r
486 &SmmFtwGetLastWriteHeader->CallerId,\r
487 &SmmFtwGetLastWriteHeader->Lba,\r
488 &SmmFtwGetLastWriteHeader->Offset,\r
489 &SmmFtwGetLastWriteHeader->Length,\r
5e5bb2a9 490 &PrivateDataSize,\r
7ea4cf3f
SZ
491 (VOID *)SmmFtwGetLastWriteHeader->Data,\r
492 &SmmFtwGetLastWriteHeader->Complete\r
493 );\r
5e5bb2a9 494 SmmFtwGetLastWriteHeader->PrivateDataSize = PrivateDataSize;\r
f3b80a8e 495 break;\r
496\r
497 default:\r
f3b80a8e 498 Status = EFI_UNSUPPORTED;\r
499 }\r
500\r
501 SmmFtwFunctionHeader->ReturnStatus = Status;\r
502\r
503 return EFI_SUCCESS;\r
504}\r
505\r
506\r
8a2d4996 507/**\r
508 SMM Firmware Volume Block Protocol notification event handler.\r
d1102dba 509\r
8a2d4996 510 @param[in] Protocol Points to the protocol's unique identifier\r
511 @param[in] Interface Points to the interface instance\r
512 @param[in] Handle The handle on which the interface was installed\r
513\r
514 @retval EFI_SUCCESS SmmEventCallback runs successfully\r
d1102dba 515\r
8a2d4996 516 **/\r
517EFI_STATUS\r
518EFIAPI\r
519FvbNotificationEvent (\r
520 IN CONST EFI_GUID *Protocol,\r
521 IN VOID *Interface,\r
522 IN EFI_HANDLE Handle\r
523 )\r
524{\r
525 EFI_STATUS Status;\r
526 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
f3b80a8e 527 EFI_HANDLE SmmFtwHandle;\r
5e5bb2a9 528 EFI_HANDLE FtwHandle;\r
d1102dba 529\r
8a2d4996 530 //\r
531 // Just return to avoid install SMM FaultTolerantWriteProtocol again\r
532 // if SMM Fault Tolerant Write protocol had been installed.\r
d1102dba 533 //\r
8a2d4996 534 Status = gSmst->SmmLocateProtocol (\r
d1102dba
LG
535 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
536 NULL,\r
8a2d4996 537 (VOID **) &FtwProtocol\r
538 );\r
539 if (!EFI_ERROR (Status)) {\r
540 return EFI_SUCCESS;\r
541 }\r
542\r
543 //\r
544 // Found proper FVB protocol and initialize FtwDevice for protocol installation\r
545 //\r
f3b80a8e 546 Status = InitFtwProtocol (mFtwDevice);\r
8a2d4996 547 if (EFI_ERROR(Status)) {\r
548 return Status;\r
549 }\r
5e5bb2a9 550\r
8a2d4996 551 //\r
552 // Install protocol interface\r
553 //\r
554 Status = gSmst->SmmInstallProtocolInterface (\r
f3b80a8e 555 &mFtwDevice->Handle,\r
8a2d4996 556 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
557 EFI_NATIVE_INTERFACE,\r
f3b80a8e 558 &mFtwDevice->FtwInstance\r
8a2d4996 559 );\r
d1102dba 560 ASSERT_EFI_ERROR (Status);\r
f3b80a8e 561\r
5e5bb2a9
SZ
562 ///\r
563 /// Register SMM FTW SMI handler\r
564 ///\r
565 Status = gSmst->SmiHandlerRegister (SmmFaultTolerantWriteHandler, &gEfiSmmFaultTolerantWriteProtocolGuid, &SmmFtwHandle);\r
566 ASSERT_EFI_ERROR (Status);\r
567\r
f3b80a8e 568 //\r
569 // Notify the Ftw wrapper driver SMM Ftw is ready\r
570 //\r
5e5bb2a9 571 FtwHandle = NULL;\r
f3b80a8e 572 Status = gBS->InstallProtocolInterface (\r
5e5bb2a9 573 &FtwHandle,\r
f3b80a8e 574 &gEfiSmmFaultTolerantWriteProtocolGuid,\r
575 EFI_NATIVE_INTERFACE,\r
576 NULL\r
577 );\r
578 ASSERT_EFI_ERROR (Status);\r
d1102dba 579\r
8a2d4996 580 return EFI_SUCCESS;\r
581}\r
582\r
f07268bd
SZ
583/**\r
584 SMM END_OF_DXE protocol notification event handler.\r
d1102dba 585\r
f07268bd
SZ
586 @param Protocol Points to the protocol's unique identifier\r
587 @param Interface Points to the interface instance\r
588 @param Handle The handle on which the interface was installed\r
589\r
590 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully\r
591\r
592**/\r
593EFI_STATUS\r
594EFIAPI\r
595SmmEndOfDxeCallback (\r
596 IN CONST EFI_GUID *Protocol,\r
597 IN VOID *Interface,\r
598 IN EFI_HANDLE Handle\r
599 )\r
600{\r
601 mEndOfDxe = TRUE;\r
602 return EFI_SUCCESS;\r
603}\r
8a2d4996 604\r
605/**\r
606 This function is the entry point of the Fault Tolerant Write driver.\r
607\r
608 @param[in] ImageHandle A handle for the image that is initializing this driver\r
609 @param[in] SystemTable A pointer to the EFI system table\r
610\r
611 @retval EFI_SUCCESS The initialization finished successfully.\r
612 @retval EFI_OUT_OF_RESOURCES Allocate memory error\r
613 @retval EFI_INVALID_PARAMETER Workspace or Spare block does not exist\r
614\r
615**/\r
616EFI_STATUS\r
617EFIAPI\r
618SmmFaultTolerantWriteInitialize (\r
619 IN EFI_HANDLE ImageHandle,\r
620 IN EFI_SYSTEM_TABLE *SystemTable\r
621 )\r
622{\r
623 EFI_STATUS Status;\r
f07268bd
SZ
624 VOID *SmmEndOfDxeRegistration;\r
625\r
8a2d4996 626 //\r
627 // Allocate private data structure for SMM FTW protocol and do some initialization\r
628 //\r
f3b80a8e 629 Status = InitFtwDevice (&mFtwDevice);\r
8a2d4996 630 if (EFI_ERROR(Status)) {\r
631 return Status;\r
632 }\r
c219324c 633\r
f07268bd
SZ
634 //\r
635 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.\r
636 //\r
637 Status = gSmst->SmmRegisterProtocolNotify (\r
638 &gEfiSmmEndOfDxeProtocolGuid,\r
639 SmmEndOfDxeCallback,\r
640 &SmmEndOfDxeRegistration\r
641 );\r
642 ASSERT_EFI_ERROR (Status);\r
643\r
8a2d4996 644 //\r
645 // Register FvbNotificationEvent () notify function.\r
d1102dba 646 //\r
8a2d4996 647 Status = gSmst->SmmRegisterProtocolNotify (\r
648 &gEfiSmmFirmwareVolumeBlockProtocolGuid,\r
649 FvbNotificationEvent,\r
650 &mFvbRegistration\r
651 );\r
652 ASSERT_EFI_ERROR (Status);\r
653\r
654 FvbNotificationEvent (NULL, NULL, NULL);\r
d1102dba 655\r
8a2d4996 656 return EFI_SUCCESS;\r
657}\r