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