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