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