]>
Commit | Line | Data |
---|---|---|
f3b80a8e | 1 | /** @file\r |
2 | \r | |
d1102dba | 3 | Implement the Fault Tolerant Write (FTW) protocol based on SMM FTW\r |
f3b80a8e | 4 | module.\r |
5 | \r | |
d1102dba | 6 | Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved. <BR>\r |
9d510e61 | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
f3b80a8e | 8 | \r |
9 | **/\r | |
10 | \r | |
11 | #include "FaultTolerantWriteSmmDxe.h"\r | |
12 | \r | |
1436aea4 MK |
13 | EFI_HANDLE mHandle = NULL;\r |
14 | EFI_MM_COMMUNICATION2_PROTOCOL *mMmCommunication2 = NULL;\r | |
15 | UINTN mPrivateDataSize = 0;\r | |
f3b80a8e | 16 | \r |
17 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL mFaultTolerantWriteDriver = {\r | |
18 | FtwGetMaxBlockSize,\r | |
19 | FtwAllocate,\r | |
20 | FtwWrite,\r | |
21 | FtwRestart,\r | |
22 | FtwAbort,\r | |
23 | FtwGetLastWrite\r | |
24 | };\r | |
25 | \r | |
26 | /**\r | |
27 | Initialize the communicate buffer using DataSize and Function number.\r | |
28 | \r | |
29 | @param[out] CommunicateBuffer The communicate buffer. Caller should free it after use.\r | |
30 | @param[out] DataPtr Points to the data in the communicate buffer. Caller should not free it.\r | |
31 | @param[in] DataSize The payload size.\r | |
32 | @param[in] Function The function number used to initialize the communicate header.\r | |
33 | \r | |
34 | **/\r | |
35 | VOID\r | |
36 | InitCommunicateBuffer (\r | |
1436aea4 MK |
37 | OUT VOID **CommunicateBuffer,\r |
38 | OUT VOID **DataPtr,\r | |
39 | IN UINTN DataSize,\r | |
40 | IN UINTN Function\r | |
f3b80a8e | 41 | )\r |
42 | {\r | |
1436aea4 MK |
43 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r |
44 | SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;\r | |
f3b80a8e | 45 | \r |
46 | //\r | |
47 | // The whole buffer size: SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE + DataSize.\r | |
48 | //\r | |
49 | SmmCommunicateHeader = AllocateZeroPool (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE);\r | |
50 | ASSERT (SmmCommunicateHeader != NULL);\r | |
d1102dba | 51 | \r |
f3b80a8e | 52 | //\r |
53 | // Prepare data buffer.\r | |
54 | //\r | |
55 | CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmFaultTolerantWriteProtocolGuid);\r | |
56 | SmmCommunicateHeader->MessageLength = DataSize + SMM_FTW_COMMUNICATE_HEADER_SIZE;\r | |
d1102dba | 57 | \r |
1436aea4 | 58 | SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)SmmCommunicateHeader->Data;\r |
f3b80a8e | 59 | SmmFtwFunctionHeader->Function = Function;\r |
60 | \r | |
61 | *CommunicateBuffer = SmmCommunicateHeader;\r | |
62 | if (DataPtr != NULL) {\r | |
63 | *DataPtr = SmmFtwFunctionHeader->Data;\r | |
d1102dba | 64 | }\r |
f3b80a8e | 65 | }\r |
66 | \r | |
f3b80a8e | 67 | /**\r |
68 | Send the data in communicate buffer to SMI handler and get response.\r | |
69 | \r | |
a02ab69a | 70 | @param[in, out] SmmCommunicateHeader The communicate buffer.\r |
f3b80a8e | 71 | @param[in] DataSize The payload size.\r |
d1102dba | 72 | \r |
f3b80a8e | 73 | **/\r |
74 | EFI_STATUS\r | |
75 | SendCommunicateBuffer (\r | |
1436aea4 MK |
76 | IN OUT EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader,\r |
77 | IN UINTN DataSize\r | |
f3b80a8e | 78 | )\r |
79 | {\r | |
1436aea4 MK |
80 | EFI_STATUS Status;\r |
81 | UINTN CommSize;\r | |
82 | SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;\r | |
d1102dba | 83 | \r |
f3b80a8e | 84 | CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE;\r |
1436aea4 MK |
85 | Status = mMmCommunication2->Communicate (\r |
86 | mMmCommunication2,\r | |
87 | SmmCommunicateHeader,\r | |
88 | SmmCommunicateHeader,\r | |
89 | &CommSize\r | |
90 | );\r | |
f3b80a8e | 91 | ASSERT_EFI_ERROR (Status);\r |
92 | \r | |
1436aea4 MK |
93 | SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *)SmmCommunicateHeader->Data;\r |
94 | return SmmFtwFunctionHeader->ReturnStatus;\r | |
f3b80a8e | 95 | }\r |
96 | \r | |
f3b80a8e | 97 | /**\r |
98 | Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.\r | |
99 | \r | |
a02ab69a | 100 | @param[in] FvbHandle The handle of FVB protocol that provides services.\r |
101 | @param[out] FvbBaseAddress The base address of the FVB attached with FvbHandle.\r | |
102 | @param[out] FvbAttributes The attributes of the FVB attached with FvbHandle.\r | |
d1102dba | 103 | \r |
f3b80a8e | 104 | @retval EFI_SUCCESS The function completed successfully.\r |
105 | @retval Others The function could not complete successfully.\r | |
106 | \r | |
107 | **/\r | |
108 | EFI_STATUS\r | |
109 | ConvertFvbHandle (\r | |
1436aea4 MK |
110 | IN EFI_HANDLE FvbHandle,\r |
111 | OUT EFI_PHYSICAL_ADDRESS *FvbBaseAddress,\r | |
112 | OUT EFI_FVB_ATTRIBUTES_2 *FvbAttributes\r | |
f3b80a8e | 113 | )\r |
114 | {\r | |
1436aea4 MK |
115 | EFI_STATUS Status;\r |
116 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
f3b80a8e | 117 | \r |
1436aea4 | 118 | Status = gBS->HandleProtocol (FvbHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&Fvb);\r |
f3b80a8e | 119 | if (EFI_ERROR (Status)) {\r |
120 | return Status;\r | |
121 | }\r | |
d1102dba | 122 | \r |
f3b80a8e | 123 | Status = Fvb->GetPhysicalAddress (Fvb, FvbBaseAddress);\r |
124 | if (EFI_ERROR (Status)) {\r | |
125 | return Status;\r | |
126 | }\r | |
127 | \r | |
128 | Status = Fvb->GetAttributes (Fvb, FvbAttributes);\r | |
d1102dba | 129 | return Status;\r |
f3b80a8e | 130 | }\r |
131 | \r | |
f3b80a8e | 132 | /**\r |
133 | Get the size of the largest block that can be updated in a fault-tolerant manner.\r | |
134 | \r | |
135 | @param[in] This Indicates a pointer to the calling context.\r | |
136 | @param[out] BlockSize A pointer to a caller-allocated UINTN that is\r | |
137 | updated to indicate the size of the largest block\r | |
138 | that can be updated.\r | |
139 | \r | |
140 | @retval EFI_SUCCESS The function completed successfully.\r | |
141 | @retval EFI_ABORTED The function could not complete successfully.\r | |
142 | \r | |
143 | **/\r | |
144 | EFI_STATUS\r | |
145 | EFIAPI\r | |
146 | FtwGetMaxBlockSize (\r | |
1436aea4 MK |
147 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r |
148 | OUT UINTN *BlockSize\r | |
f3b80a8e | 149 | )\r |
150 | {\r | |
1436aea4 MK |
151 | EFI_STATUS Status;\r |
152 | UINTN PayloadSize;\r | |
153 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
154 | SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmFtwBlockSizeHeader;\r | |
f3b80a8e | 155 | \r |
156 | //\r | |
157 | // Initialize the communicate buffer.\r | |
158 | //\r | |
1436aea4 | 159 | PayloadSize = sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER);\r |
f3b80a8e | 160 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwBlockSizeHeader, PayloadSize, FTW_FUNCTION_GET_MAX_BLOCK_SIZE);\r |
d1102dba | 161 | \r |
f3b80a8e | 162 | //\r |
163 | // Send data to SMM.\r | |
164 | //\r | |
165 | Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r | |
166 | \r | |
167 | //\r | |
168 | // Get data from SMM\r | |
169 | //\r | |
d1102dba | 170 | *BlockSize = SmmFtwBlockSizeHeader->BlockSize;\r |
f3b80a8e | 171 | FreePool (SmmCommunicateHeader);\r |
d1102dba | 172 | \r |
f3b80a8e | 173 | return Status;\r |
174 | }\r | |
175 | \r | |
f3b80a8e | 176 | /**\r |
177 | Allocates space for the protocol to maintain information about writes.\r | |
178 | Since writes must be completed in a fault-tolerant manner and multiple\r | |
179 | writes require more resources to be successful, this function\r | |
180 | enables the protocol to ensure that enough space exists to track\r | |
181 | information about upcoming writes.\r | |
182 | \r | |
183 | @param[in] This A pointer to the calling context.\r | |
184 | @param[in] CallerId The GUID identifying the write.\r | |
185 | @param[in] PrivateDataSize The size of the caller's private data that must be\r | |
186 | recorded for each write.\r | |
187 | @param[in] NumberOfWrites The number of fault tolerant block writes that will\r | |
188 | need to occur.\r | |
189 | \r | |
190 | @retval EFI_SUCCESS The function completed successfully\r | |
191 | @retval EFI_ABORTED The function could not complete successfully.\r | |
192 | @retval EFI_ACCESS_DENIED Not all allocated writes have been completed. All\r | |
193 | writes must be completed or aborted before another\r | |
194 | fault tolerant write can occur.\r | |
195 | \r | |
196 | **/\r | |
197 | EFI_STATUS\r | |
198 | EFIAPI\r | |
199 | FtwAllocate (\r | |
1436aea4 MK |
200 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r |
201 | IN EFI_GUID *CallerId,\r | |
202 | IN UINTN PrivateDataSize,\r | |
203 | IN UINTN NumberOfWrites\r | |
f3b80a8e | 204 | )\r |
205 | {\r | |
1436aea4 MK |
206 | EFI_STATUS Status;\r |
207 | UINTN PayloadSize;\r | |
208 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
209 | SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;\r | |
f3b80a8e | 210 | \r |
211 | //\r | |
212 | // Initialize the communicate buffer.\r | |
213 | //\r | |
1436aea4 | 214 | PayloadSize = sizeof (SMM_FTW_ALLOCATE_HEADER);\r |
f3b80a8e | 215 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwAllocateHeader, PayloadSize, FTW_FUNCTION_ALLOCATE);\r |
216 | CopyGuid (&SmmFtwAllocateHeader->CallerId, CallerId);\r | |
217 | SmmFtwAllocateHeader->PrivateDataSize = PrivateDataSize;\r | |
d1102dba LG |
218 | SmmFtwAllocateHeader->NumberOfWrites = NumberOfWrites;\r |
219 | \r | |
f3b80a8e | 220 | //\r |
221 | // Send data to SMM.\r | |
222 | //\r | |
223 | Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r | |
1436aea4 | 224 | if (!EFI_ERROR (Status)) {\r |
f3b80a8e | 225 | mPrivateDataSize = PrivateDataSize;\r |
226 | }\r | |
227 | \r | |
228 | FreePool (SmmCommunicateHeader);\r | |
229 | return Status;\r | |
230 | }\r | |
231 | \r | |
f3b80a8e | 232 | /**\r |
233 | Starts a target block update. This records information about the write\r | |
234 | in fault tolerant storage, and will complete the write in a recoverable\r | |
235 | manner, ensuring at all times that either the original contents or\r | |
236 | the modified contents are available.\r | |
237 | \r | |
238 | @param[in] This The calling context.\r | |
239 | @param[in] Lba The logical block address of the target block.\r | |
240 | @param[in] Offset The offset within the target block to place the\r | |
241 | data.\r | |
242 | @param[in] Length The number of bytes to write to the target block.\r | |
243 | @param[in] PrivateData A pointer to private data that the caller requires\r | |
244 | to complete any pending writes in the event of a\r | |
245 | fault.\r | |
246 | @param[in] FvBlockHandle The handle of FVB protocol that provides services\r | |
247 | for reading, writing, and erasing the target block.\r | |
248 | @param[in] Buffer The data to write.\r | |
249 | \r | |
250 | @retval EFI_SUCCESS The function completed successfully.\r | |
251 | @retval EFI_ABORTED The function could not complete successfully.\r | |
252 | @retval EFI_BAD_BUFFER_SIZE The write would span a block boundary, which is not\r | |
253 | a valid action.\r | |
254 | @retval EFI_ACCESS_DENIED No writes have been allocated.\r | |
255 | @retval EFI_NOT_READY The last write has not been completed. Restart()\r | |
256 | must be called to complete it.\r | |
257 | \r | |
258 | **/\r | |
259 | EFI_STATUS\r | |
260 | EFIAPI\r | |
261 | FtwWrite (\r | |
1436aea4 MK |
262 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r |
263 | IN EFI_LBA Lba,\r | |
264 | IN UINTN Offset,\r | |
265 | IN UINTN Length,\r | |
266 | IN VOID *PrivateData,\r | |
267 | IN EFI_HANDLE FvBlockHandle,\r | |
268 | IN VOID *Buffer\r | |
f3b80a8e | 269 | )\r |
270 | {\r | |
1436aea4 MK |
271 | EFI_STATUS Status;\r |
272 | UINTN PayloadSize;\r | |
273 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
274 | SMM_FTW_WRITE_HEADER *SmmFtwWriteHeader;\r | |
f3b80a8e | 275 | \r |
276 | //\r | |
277 | // Initialize the communicate buffer.\r | |
278 | //\r | |
1436aea4 | 279 | PayloadSize = OFFSET_OF (SMM_FTW_WRITE_HEADER, Data) + Length;\r |
f3b80a8e | 280 | if (PrivateData != NULL) {\r |
281 | //\r | |
282 | // The private data buffer size should be the same one in FtwAllocate API.\r | |
283 | //\r | |
284 | PayloadSize += mPrivateDataSize;\r | |
285 | }\r | |
1436aea4 | 286 | \r |
f3b80a8e | 287 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwWriteHeader, PayloadSize, FTW_FUNCTION_WRITE);\r |
288 | \r | |
289 | //\r | |
d1102dba | 290 | // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address\r |
f3b80a8e | 291 | // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.\r |
292 | //\r | |
293 | Status = ConvertFvbHandle (FvBlockHandle, &SmmFtwWriteHeader->FvbBaseAddress, &SmmFtwWriteHeader->FvbAttributes);\r | |
294 | if (EFI_ERROR (Status)) {\r | |
295 | FreePool (SmmCommunicateHeader);\r | |
296 | return EFI_ABORTED;\r | |
297 | }\r | |
d1102dba | 298 | \r |
f3b80a8e | 299 | SmmFtwWriteHeader->Lba = Lba;\r |
d1102dba | 300 | SmmFtwWriteHeader->Offset = Offset;\r |
f3b80a8e | 301 | SmmFtwWriteHeader->Length = Length;\r |
302 | CopyMem (SmmFtwWriteHeader->Data, Buffer, Length);\r | |
303 | if (PrivateData == NULL) {\r | |
304 | SmmFtwWriteHeader->PrivateDataSize = 0;\r | |
305 | } else {\r | |
306 | SmmFtwWriteHeader->PrivateDataSize = mPrivateDataSize;\r | |
307 | CopyMem (&SmmFtwWriteHeader->Data[Length], PrivateData, mPrivateDataSize);\r | |
308 | }\r | |
309 | \r | |
310 | //\r | |
311 | // Send data to SMM.\r | |
312 | //\r | |
313 | Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r | |
d1102dba | 314 | FreePool (SmmCommunicateHeader);\r |
f3b80a8e | 315 | return Status;\r |
316 | }\r | |
317 | \r | |
f3b80a8e | 318 | /**\r |
319 | Restarts a previously interrupted write. The caller must provide the\r | |
320 | block protocol needed to complete the interrupted write.\r | |
321 | \r | |
322 | @param[in] This The calling context.\r | |
323 | @param[in] FvBlockHandle The handle of FVB protocol that provides services.\r | |
324 | \r | |
325 | @retval EFI_SUCCESS The function completed successfully.\r | |
326 | @retval EFI_ABORTED The function could not complete successfully.\r | |
327 | @retval EFI_ACCESS_DENIED No pending writes exist.\r | |
328 | \r | |
329 | **/\r | |
330 | EFI_STATUS\r | |
331 | EFIAPI\r | |
332 | FtwRestart (\r | |
1436aea4 MK |
333 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r |
334 | IN EFI_HANDLE FvBlockHandle\r | |
f3b80a8e | 335 | )\r |
336 | {\r | |
1436aea4 MK |
337 | EFI_STATUS Status;\r |
338 | UINTN PayloadSize;\r | |
339 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
340 | SMM_FTW_RESTART_HEADER *SmmFtwRestartHeader;\r | |
d1102dba | 341 | \r |
f3b80a8e | 342 | //\r |
343 | // Initialize the communicate buffer.\r | |
344 | //\r | |
1436aea4 | 345 | PayloadSize = sizeof (SMM_FTW_RESTART_HEADER);\r |
d1102dba | 346 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwRestartHeader, PayloadSize, FTW_FUNCTION_RESTART);\r |
f3b80a8e | 347 | \r |
348 | //\r | |
d1102dba | 349 | // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address\r |
f3b80a8e | 350 | // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.\r |
351 | //\r | |
352 | Status = ConvertFvbHandle (FvBlockHandle, &SmmFtwRestartHeader->FvbBaseAddress, &SmmFtwRestartHeader->FvbAttributes);\r | |
353 | if (EFI_ERROR (Status)) {\r | |
d1102dba | 354 | FreePool (SmmCommunicateHeader);\r |
f3b80a8e | 355 | return EFI_ABORTED;\r |
356 | }\r | |
357 | \r | |
358 | //\r | |
359 | // Send data to SMM.\r | |
360 | //\r | |
361 | Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r | |
d1102dba | 362 | FreePool (SmmCommunicateHeader);\r |
f3b80a8e | 363 | return Status;\r |
364 | }\r | |
365 | \r | |
f3b80a8e | 366 | /**\r |
367 | Aborts all previously allocated writes.\r | |
368 | \r | |
369 | @param[in] This The calling context.\r | |
370 | \r | |
371 | @retval EFI_SUCCESS The function completed successfully.\r | |
372 | @retval EFI_ABORTED The function could not complete successfully.\r | |
373 | @retval EFI_NOT_FOUND No allocated writes exist.\r | |
374 | \r | |
375 | **/\r | |
376 | EFI_STATUS\r | |
377 | EFIAPI\r | |
378 | FtwAbort (\r | |
1436aea4 | 379 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This\r |
f3b80a8e | 380 | )\r |
381 | {\r | |
1436aea4 MK |
382 | EFI_STATUS Status;\r |
383 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
d1102dba | 384 | \r |
f3b80a8e | 385 | //\r |
386 | // Initialize the communicate buffer.\r | |
387 | //\r | |
388 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, NULL, 0, FTW_FUNCTION_ABORT);\r | |
d1102dba | 389 | \r |
f3b80a8e | 390 | //\r |
391 | // Send data to SMM.\r | |
392 | //\r | |
393 | Status = SendCommunicateBuffer (SmmCommunicateHeader, 0);\r | |
394 | \r | |
d1102dba | 395 | FreePool (SmmCommunicateHeader);\r |
f3b80a8e | 396 | return Status;\r |
397 | }\r | |
398 | \r | |
f3b80a8e | 399 | /**\r |
400 | Starts a target block update. This function records information about the write\r | |
401 | in fault-tolerant storage and completes the write in a recoverable\r | |
402 | manner, ensuring at all times that either the original contents or\r | |
403 | the modified contents are available.\r | |
404 | \r | |
405 | @param[in] This Indicates a pointer to the calling context.\r | |
406 | @param[out] CallerId The GUID identifying the last write.\r | |
407 | @param[out] Lba The logical block address of the last write.\r | |
408 | @param[out] Offset The offset within the block of the last write.\r | |
409 | @param[out] Length The length of the last write.\r | |
410 | @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On\r | |
411 | output, the size of the private data stored for\r | |
412 | this write.\r | |
413 | @param[out] PrivateData A pointer to a buffer. The function will copy\r | |
414 | PrivateDataSize bytes from the private data stored\r | |
415 | for this write.\r | |
416 | @param[out] Complete A Boolean value with TRUE indicating that the write\r | |
417 | was completed.\r | |
418 | \r | |
419 | @retval EFI_SUCCESS The function completed successfully.\r | |
420 | @retval EFI_ABORTED The function could not complete successfully.\r | |
421 | @retval EFI_NOT_FOUND No allocated writes exist.\r | |
422 | \r | |
423 | **/\r | |
424 | EFI_STATUS\r | |
425 | EFIAPI\r | |
426 | FtwGetLastWrite (\r | |
1436aea4 MK |
427 | IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,\r |
428 | OUT EFI_GUID *CallerId,\r | |
429 | OUT EFI_LBA *Lba,\r | |
430 | OUT UINTN *Offset,\r | |
431 | OUT UINTN *Length,\r | |
432 | IN OUT UINTN *PrivateDataSize,\r | |
433 | OUT VOID *PrivateData,\r | |
434 | OUT BOOLEAN *Complete\r | |
f3b80a8e | 435 | )\r |
436 | {\r | |
1436aea4 MK |
437 | EFI_STATUS Status;\r |
438 | UINTN PayloadSize;\r | |
439 | EFI_MM_COMMUNICATE_HEADER *SmmCommunicateHeader;\r | |
440 | SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;\r | |
f3b80a8e | 441 | \r |
442 | //\r | |
443 | // Initialize the communicate buffer.\r | |
444 | //\r | |
1436aea4 | 445 | PayloadSize = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + *PrivateDataSize;\r |
f3b80a8e | 446 | InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwGetLastWriteHeader, PayloadSize, FTW_FUNCTION_GET_LAST_WRITE);\r |
447 | SmmFtwGetLastWriteHeader->PrivateDataSize = *PrivateDataSize;\r | |
448 | \r | |
449 | //\r | |
450 | // Send data to SMM.\r | |
451 | //\r | |
452 | Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);\r | |
453 | \r | |
454 | //\r | |
455 | // Get data from SMM\r | |
456 | //\r | |
457 | *PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;\r | |
1436aea4 | 458 | if ((Status == EFI_SUCCESS) || (Status == EFI_BUFFER_TOO_SMALL)) {\r |
f3b80a8e | 459 | *Lba = SmmFtwGetLastWriteHeader->Lba;\r |
d1102dba | 460 | *Offset = SmmFtwGetLastWriteHeader->Offset;\r |
f3b80a8e | 461 | *Length = SmmFtwGetLastWriteHeader->Length;\r |
462 | *Complete = SmmFtwGetLastWriteHeader->Complete;\r | |
463 | CopyGuid (CallerId, &SmmFtwGetLastWriteHeader->CallerId);\r | |
5e5bb2a9 SZ |
464 | if (Status == EFI_SUCCESS) {\r |
465 | CopyMem (PrivateData, SmmFtwGetLastWriteHeader->Data, *PrivateDataSize);\r | |
466 | }\r | |
467 | } else if (Status == EFI_NOT_FOUND) {\r | |
468 | *Complete = SmmFtwGetLastWriteHeader->Complete;\r | |
f3b80a8e | 469 | }\r |
470 | \r | |
d1102dba | 471 | FreePool (SmmCommunicateHeader);\r |
f3b80a8e | 472 | return Status;\r |
473 | }\r | |
474 | \r | |
475 | /**\r | |
476 | SMM Fault Tolerant Write Protocol notification event handler.\r | |
477 | \r | |
478 | Install Fault Tolerant Write Protocol.\r | |
479 | \r | |
480 | @param[in] Event Event whose notification function is being invoked.\r | |
481 | @param[in] Context Pointer to the notification function's context.\r | |
482 | **/\r | |
483 | VOID\r | |
484 | EFIAPI\r | |
485 | SmmFtwReady (\r | |
1436aea4 MK |
486 | IN EFI_EVENT Event,\r |
487 | IN VOID *Context\r | |
f3b80a8e | 488 | )\r |
489 | {\r | |
1436aea4 MK |
490 | EFI_STATUS Status;\r |
491 | EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r | |
f3b80a8e | 492 | \r |
493 | //\r | |
494 | // Just return to avoid install SMM FaultTolerantWriteProtocol again\r | |
495 | // if Fault Tolerant Write protocol had been installed.\r | |
d1102dba | 496 | //\r |
f3b80a8e | 497 | Status = gBS->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid, NULL, (VOID **)&FtwProtocol);\r |
498 | if (!EFI_ERROR (Status)) {\r | |
499 | return;\r | |
500 | }\r | |
d1102dba | 501 | \r |
1436aea4 | 502 | Status = gBS->LocateProtocol (&gEfiMmCommunication2ProtocolGuid, NULL, (VOID **)&mMmCommunication2);\r |
f3b80a8e | 503 | ASSERT_EFI_ERROR (Status);\r |
504 | \r | |
505 | //\r | |
506 | // Install protocol interface\r | |
507 | //\r | |
508 | Status = gBS->InstallProtocolInterface (\r | |
509 | &mHandle,\r | |
510 | &gEfiFaultTolerantWriteProtocolGuid,\r | |
511 | EFI_NATIVE_INTERFACE,\r | |
512 | &mFaultTolerantWriteDriver\r | |
513 | );\r | |
514 | ASSERT_EFI_ERROR (Status);\r | |
d1102dba | 515 | \r |
f3b80a8e | 516 | Status = gBS->CloseEvent (Event);\r |
d1102dba | 517 | ASSERT_EFI_ERROR (Status);\r |
f3b80a8e | 518 | }\r |
519 | \r | |
f3b80a8e | 520 | /**\r |
521 | The driver entry point for Fault Tolerant Write driver.\r | |
522 | \r | |
523 | The function does the necessary initialization work.\r | |
524 | \r | |
525 | @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r | |
526 | @param[in] SystemTable A pointer to the EFI system table.\r | |
527 | \r | |
528 | @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.\r | |
529 | \r | |
530 | **/\r | |
531 | EFI_STATUS\r | |
532 | EFIAPI\r | |
533 | FaultTolerantWriteSmmInitialize (\r | |
1436aea4 MK |
534 | IN EFI_HANDLE ImageHandle,\r |
535 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
f3b80a8e | 536 | )\r |
537 | {\r | |
1436aea4 | 538 | VOID *SmmFtwRegistration;\r |
f3b80a8e | 539 | \r |
540 | //\r | |
541 | // Smm FTW driver is ready\r | |
542 | //\r | |
543 | EfiCreateProtocolNotifyEvent (\r | |
544 | &gEfiSmmFaultTolerantWriteProtocolGuid,\r | |
d1102dba LG |
545 | TPL_CALLBACK,\r |
546 | SmmFtwReady,\r | |
547 | NULL,\r | |
f3b80a8e | 548 | &SmmFtwRegistration\r |
549 | );\r | |
d1102dba | 550 | \r |
f3b80a8e | 551 | return EFI_SUCCESS;\r |
552 | }\r |