]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = 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_SMM_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 /**
69 Send the data in communicate buffer to SMI handler and get response.
70
71 @param[in, out] SmmCommunicateHeader The communicate buffer.
72 @param[in] DataSize The payload size.
73
74 **/
75 EFI_STATUS
76 SendCommunicateBuffer (
77 IN OUT EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader,
78 IN UINTN DataSize
79 )
80 {
81 EFI_STATUS Status;
82 UINTN CommSize;
83 SMM_FTW_COMMUNICATE_FUNCTION_HEADER *SmmFtwFunctionHeader;
84
85 CommSize = DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_FTW_COMMUNICATE_HEADER_SIZE;
86 Status = mSmmCommunication->Communicate (mSmmCommunication, SmmCommunicateHeader, &CommSize);
87 ASSERT_EFI_ERROR (Status);
88
89 SmmFtwFunctionHeader = (SMM_FTW_COMMUNICATE_FUNCTION_HEADER *) SmmCommunicateHeader->Data;
90 return SmmFtwFunctionHeader->ReturnStatus;
91 }
92
93
94 /**
95 Get the FvbBaseAddress and FvbAttributes from the FVB handle FvbHandle.
96
97 @param[in] FvbHandle The handle of FVB protocol that provides services.
98 @param[out] FvbBaseAddress The base address of the FVB attached with FvbHandle.
99 @param[out] FvbAttributes The attributes of the FVB attached with FvbHandle.
100
101 @retval EFI_SUCCESS The function completed successfully.
102 @retval Others The function could not complete successfully.
103
104 **/
105 EFI_STATUS
106 ConvertFvbHandle (
107 IN EFI_HANDLE FvbHandle,
108 OUT EFI_PHYSICAL_ADDRESS *FvbBaseAddress,
109 OUT EFI_FVB_ATTRIBUTES_2 *FvbAttributes
110 )
111 {
112 EFI_STATUS Status;
113 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;
114
115 Status = gBS->HandleProtocol (FvbHandle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **) &Fvb);
116 if (EFI_ERROR (Status)) {
117 return Status;
118 }
119
120 Status = Fvb->GetPhysicalAddress (Fvb, FvbBaseAddress);
121 if (EFI_ERROR (Status)) {
122 return Status;
123 }
124
125 Status = Fvb->GetAttributes (Fvb, FvbAttributes);
126 return Status;
127 }
128
129
130 /**
131 Get the size of the largest block that can be updated in a fault-tolerant manner.
132
133 @param[in] This Indicates a pointer to the calling context.
134 @param[out] BlockSize A pointer to a caller-allocated UINTN that is
135 updated to indicate the size of the largest block
136 that can be updated.
137
138 @retval EFI_SUCCESS The function completed successfully.
139 @retval EFI_ABORTED The function could not complete successfully.
140
141 **/
142 EFI_STATUS
143 EFIAPI
144 FtwGetMaxBlockSize (
145 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,
146 OUT UINTN *BlockSize
147 )
148 {
149 EFI_STATUS Status;
150 UINTN PayloadSize;
151 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
152 SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER *SmmFtwBlockSizeHeader;
153
154 //
155 // Initialize the communicate buffer.
156 //
157 PayloadSize = sizeof (SMM_FTW_GET_MAX_BLOCK_SIZE_HEADER);
158 InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwBlockSizeHeader, PayloadSize, FTW_FUNCTION_GET_MAX_BLOCK_SIZE);
159
160 //
161 // Send data to SMM.
162 //
163 Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);
164
165 //
166 // Get data from SMM
167 //
168 *BlockSize = SmmFtwBlockSizeHeader->BlockSize;
169 FreePool (SmmCommunicateHeader);
170
171 return Status;
172 }
173
174
175 /**
176 Allocates space for the protocol to maintain information about writes.
177 Since writes must be completed in a fault-tolerant manner and multiple
178 writes require more resources to be successful, this function
179 enables the protocol to ensure that enough space exists to track
180 information about upcoming writes.
181
182 @param[in] This A pointer to the calling context.
183 @param[in] CallerId The GUID identifying the write.
184 @param[in] PrivateDataSize The size of the caller's private data that must be
185 recorded for each write.
186 @param[in] NumberOfWrites The number of fault tolerant block writes that will
187 need to occur.
188
189 @retval EFI_SUCCESS The function completed successfully
190 @retval EFI_ABORTED The function could not complete successfully.
191 @retval EFI_ACCESS_DENIED Not all allocated writes have been completed. All
192 writes must be completed or aborted before another
193 fault tolerant write can occur.
194
195 **/
196 EFI_STATUS
197 EFIAPI
198 FtwAllocate (
199 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,
200 IN EFI_GUID *CallerId,
201 IN UINTN PrivateDataSize,
202 IN UINTN NumberOfWrites
203 )
204 {
205 EFI_STATUS Status;
206 UINTN PayloadSize;
207 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
208 SMM_FTW_ALLOCATE_HEADER *SmmFtwAllocateHeader;
209
210 //
211 // Initialize the communicate buffer.
212 //
213 PayloadSize = sizeof (SMM_FTW_ALLOCATE_HEADER);
214 InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwAllocateHeader, PayloadSize, FTW_FUNCTION_ALLOCATE);
215 CopyGuid (&SmmFtwAllocateHeader->CallerId, CallerId);
216 SmmFtwAllocateHeader->PrivateDataSize = PrivateDataSize;
217 SmmFtwAllocateHeader->NumberOfWrites = NumberOfWrites;
218
219 //
220 // Send data to SMM.
221 //
222 Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);
223 if (!EFI_ERROR( Status)) {
224 mPrivateDataSize = PrivateDataSize;
225 }
226
227 FreePool (SmmCommunicateHeader);
228 return Status;
229 }
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_SMM_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 InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwWriteHeader, PayloadSize, FTW_FUNCTION_WRITE);
287
288 //
289 // FvBlockHandle can not be used in SMM environment. Here we get the FVB protocol first, then get FVB base address
290 // and its attribute. Send these information to SMM handler, the SMM handler will find the proper FVB to write data.
291 //
292 Status = ConvertFvbHandle (FvBlockHandle, &SmmFtwWriteHeader->FvbBaseAddress, &SmmFtwWriteHeader->FvbAttributes);
293 if (EFI_ERROR (Status)) {
294 FreePool (SmmCommunicateHeader);
295 return EFI_ABORTED;
296 }
297
298 SmmFtwWriteHeader->Lba = Lba;
299 SmmFtwWriteHeader->Offset = Offset;
300 SmmFtwWriteHeader->Length = Length;
301 CopyMem (SmmFtwWriteHeader->Data, Buffer, Length);
302 if (PrivateData == NULL) {
303 SmmFtwWriteHeader->PrivateDataSize = 0;
304 } else {
305 SmmFtwWriteHeader->PrivateDataSize = mPrivateDataSize;
306 CopyMem (&SmmFtwWriteHeader->Data[Length], PrivateData, mPrivateDataSize);
307 }
308
309 //
310 // Send data to SMM.
311 //
312 Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);
313 FreePool (SmmCommunicateHeader);
314 return Status;
315 }
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_SMM_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 /**
368 Aborts all previously allocated writes.
369
370 @param[in] This The calling context.
371
372 @retval EFI_SUCCESS The function completed successfully.
373 @retval EFI_ABORTED The function could not complete successfully.
374 @retval EFI_NOT_FOUND No allocated writes exist.
375
376 **/
377 EFI_STATUS
378 EFIAPI
379 FtwAbort (
380 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This
381 )
382 {
383 EFI_STATUS Status;
384 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
385
386 //
387 // Initialize the communicate buffer.
388 //
389 InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, NULL, 0, FTW_FUNCTION_ABORT);
390
391 //
392 // Send data to SMM.
393 //
394 Status = SendCommunicateBuffer (SmmCommunicateHeader, 0);
395
396 FreePool (SmmCommunicateHeader);
397 return Status;
398 }
399
400
401 /**
402 Starts a target block update. This function records information about the write
403 in fault-tolerant storage and completes the write in a recoverable
404 manner, ensuring at all times that either the original contents or
405 the modified contents are available.
406
407 @param[in] This Indicates a pointer to the calling context.
408 @param[out] CallerId The GUID identifying the last write.
409 @param[out] Lba The logical block address of the last write.
410 @param[out] Offset The offset within the block of the last write.
411 @param[out] Length The length of the last write.
412 @param[in, out] PrivateDataSize On input, the size of the PrivateData buffer. On
413 output, the size of the private data stored for
414 this write.
415 @param[out] PrivateData A pointer to a buffer. The function will copy
416 PrivateDataSize bytes from the private data stored
417 for this write.
418 @param[out] Complete A Boolean value with TRUE indicating that the write
419 was completed.
420
421 @retval EFI_SUCCESS The function completed successfully.
422 @retval EFI_ABORTED The function could not complete successfully.
423 @retval EFI_NOT_FOUND No allocated writes exist.
424
425 **/
426 EFI_STATUS
427 EFIAPI
428 FtwGetLastWrite (
429 IN EFI_FAULT_TOLERANT_WRITE_PROTOCOL *This,
430 OUT EFI_GUID *CallerId,
431 OUT EFI_LBA *Lba,
432 OUT UINTN *Offset,
433 OUT UINTN *Length,
434 IN OUT UINTN *PrivateDataSize,
435 OUT VOID *PrivateData,
436 OUT BOOLEAN *Complete
437 )
438 {
439 EFI_STATUS Status;
440 UINTN PayloadSize;
441 EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader;
442 SMM_FTW_GET_LAST_WRITE_HEADER *SmmFtwGetLastWriteHeader;
443
444 //
445 // Initialize the communicate buffer.
446 //
447 PayloadSize = OFFSET_OF (SMM_FTW_GET_LAST_WRITE_HEADER, Data) + *PrivateDataSize;
448 InitCommunicateBuffer ((VOID **)&SmmCommunicateHeader, (VOID **)&SmmFtwGetLastWriteHeader, PayloadSize, FTW_FUNCTION_GET_LAST_WRITE);
449 SmmFtwGetLastWriteHeader->PrivateDataSize = *PrivateDataSize;
450
451 //
452 // Send data to SMM.
453 //
454 Status = SendCommunicateBuffer (SmmCommunicateHeader, PayloadSize);
455
456 //
457 // Get data from SMM
458 //
459 *PrivateDataSize = SmmFtwGetLastWriteHeader->PrivateDataSize;
460 if (Status == EFI_SUCCESS || Status == EFI_BUFFER_TOO_SMALL) {
461 *Lba = SmmFtwGetLastWriteHeader->Lba;
462 *Offset = SmmFtwGetLastWriteHeader->Offset;
463 *Length = SmmFtwGetLastWriteHeader->Length;
464 *Complete = SmmFtwGetLastWriteHeader->Complete;
465 CopyGuid (CallerId, &SmmFtwGetLastWriteHeader->CallerId);
466 if (Status == EFI_SUCCESS) {
467 CopyMem (PrivateData, SmmFtwGetLastWriteHeader->Data, *PrivateDataSize);
468 }
469 } else if (Status == EFI_NOT_FOUND) {
470 *Complete = SmmFtwGetLastWriteHeader->Complete;
471 }
472
473 FreePool (SmmCommunicateHeader);
474 return Status;
475 }
476
477 /**
478 SMM Fault Tolerant Write Protocol notification event handler.
479
480 Install Fault Tolerant Write Protocol.
481
482 @param[in] Event Event whose notification function is being invoked.
483 @param[in] Context Pointer to the notification function's context.
484 **/
485 VOID
486 EFIAPI
487 SmmFtwReady (
488 IN EFI_EVENT Event,
489 IN VOID *Context
490 )
491 {
492 EFI_STATUS Status;
493 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
494
495 //
496 // Just return to avoid install SMM FaultTolerantWriteProtocol again
497 // if Fault Tolerant Write protocol had been installed.
498 //
499 Status = gBS->LocateProtocol (&gEfiFaultTolerantWriteProtocolGuid, NULL, (VOID **)&FtwProtocol);
500 if (!EFI_ERROR (Status)) {
501 return;
502 }
503
504 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
505 ASSERT_EFI_ERROR (Status);
506
507 //
508 // Install protocol interface
509 //
510 Status = gBS->InstallProtocolInterface (
511 &mHandle,
512 &gEfiFaultTolerantWriteProtocolGuid,
513 EFI_NATIVE_INTERFACE,
514 &mFaultTolerantWriteDriver
515 );
516 ASSERT_EFI_ERROR (Status);
517
518 Status = gBS->CloseEvent (Event);
519 ASSERT_EFI_ERROR (Status);
520 }
521
522
523 /**
524 The driver entry point for Fault Tolerant Write driver.
525
526 The function does the necessary initialization work.
527
528 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
529 @param[in] SystemTable A pointer to the EFI system table.
530
531 @retval EFI_SUCCESS This funtion always return EFI_SUCCESS.
532
533 **/
534 EFI_STATUS
535 EFIAPI
536 FaultTolerantWriteSmmInitialize (
537 IN EFI_HANDLE ImageHandle,
538 IN EFI_SYSTEM_TABLE *SystemTable
539 )
540 {
541 VOID *SmmFtwRegistration;
542
543 //
544 // Smm FTW driver is ready
545 //
546 EfiCreateProtocolNotifyEvent (
547 &gEfiSmmFaultTolerantWriteProtocolGuid,
548 TPL_CALLBACK,
549 SmmFtwReady,
550 NULL,
551 &SmmFtwRegistration
552 );
553
554 return EFI_SUCCESS;
555 }
556