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
|
/** @file
DXE capsule report related function.
Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <PiDxe.h>
#include <Protocol/FirmwareManagement.h>
#include <Guid/CapsuleReport.h>
#include <Guid/FmpCapsule.h>
#include <Guid/CapsuleVendor.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiLib.h>
#include <Library/PcdLib.h>
#include <Library/HobLib.h>
#include <Library/PrintLib.h>
#include <Library/ReportStatusCodeLib.h>
#include <Library/DevicePathLib.h>
#include <Library/CapsuleLib.h>
#include <Library/VariablePolicyHelperLib.h>
#include <IndustryStandard/WindowsUxCapsule.h>
/**
This routine is called to clear CapsuleOnDisk Relocation Info variable.
Total Capsule On Disk length is recorded in this variable
@retval EFI_SUCCESS Capsule On Disk flags are cleared
**/
EFI_STATUS
CoDClearCapsuleRelocationInfo(
VOID
);
/**
Get current capsule last variable index.
@return Current capsule last variable index.
@retval -1 No current capsule last variable.
**/
INTN
GetCurrentCapsuleLastIndex (
VOID
)
{
UINTN Size;
CHAR16 CapsuleLastStr[sizeof("Capsule####")];
EFI_STATUS Status;
UINT16 CurrentIndex;
Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
Status = gRT->GetVariable(
L"CapsuleLast",
&gEfiCapsuleReportGuid,
NULL,
&Size,
CapsuleLastStr
);
if (EFI_ERROR(Status)) {
return -1;
}
CurrentIndex = (UINT16)StrHexToUintn(&CapsuleLastStr[sizeof("Capsule") - 1]);
return CurrentIndex;
}
/**
Get a new capsule status variable index.
@return A new capsule status variable index.
@retval 0 No new capsule status variable index. Rolling over.
**/
INTN
GetNewCapsuleResultIndex (
VOID
)
{
INTN CurrentIndex;
CurrentIndex = GetCurrentCapsuleLastIndex();
if (CurrentIndex >= PcdGet16(PcdCapsuleMax)) {
DEBUG((DEBUG_INFO, " CapsuleResult variable Rolling Over!\n"));
return 0;
}
return CurrentIndex + 1;
}
/**
Lock Variable by variable policy.
@param[in] VariableGuid The Guid of the variable to be locked
@param[in] VariableName The name of the variable to be locked
@param[in] VariablePolicy The pointer of variable lock policy
**/
VOID
LockVariable (
IN CONST EFI_GUID VariableGuid,
IN CHAR16 *VariableName,
IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy
)
{
EFI_STATUS Status;
// Set the policies to protect the target variables
Status = RegisterBasicVariablePolicy (VariablePolicy,
&VariableGuid,
VariableName,
VARIABLE_POLICY_NO_MIN_SIZE,
VARIABLE_POLICY_NO_MAX_SIZE,
VARIABLE_POLICY_NO_MUST_ATTR,
VARIABLE_POLICY_NO_CANT_ATTR,
VARIABLE_POLICY_TYPE_LOCK_NOW);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "DxeCapsuleLibFmp: Failed to lock variable %g %s. Status = %r\n",
&VariableGuid,
VariableName,
Status));
ASSERT_EFI_ERROR (Status);
}
}
/**
Write a new capsule status variable.
@param[in] CapsuleResult The capsule status variable
@param[in] CapsuleResultSize The size of the capsule stauts variable in bytes
@retval EFI_SUCCESS The capsule status variable is recorded.
@retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
**/
EFI_STATUS
WriteNewCapsuleResultVariable (
IN VOID *CapsuleResult,
IN UINTN CapsuleResultSize
)
{
INTN CapsuleResultIndex;
CHAR16 CapsuleResultStr[sizeof("Capsule####")];
UINTN Size;
EFI_STATUS Status;
CapsuleResultIndex = GetNewCapsuleResultIndex();
DEBUG((DEBUG_INFO, "New CapsuleResultIndex - 0x%x\n", CapsuleResultIndex));
UnicodeSPrint(
CapsuleResultStr,
sizeof(CapsuleResultStr),
L"Capsule%04x",
CapsuleResultIndex
);
Status = gRT->SetVariable(
CapsuleResultStr,
&gEfiCapsuleReportGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
CapsuleResultSize,
CapsuleResult
);
if (!EFI_ERROR(Status)) {
Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
DEBUG((DEBUG_INFO, "Set CapsuleLast - %s\n", CapsuleResultStr));
Status = gRT->SetVariable(
L"CapsuleLast",
&gEfiCapsuleReportGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
Size,
CapsuleResultStr
);
}
return Status;
}
/**
Record capsule status variable and to local cache.
@param[in] CapsuleHeader The capsule image header
@param[in] CapsuleStatus The capsule process stauts
@retval EFI_SUCCESS The capsule status variable is recorded.
@retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
**/
EFI_STATUS
RecordCapsuleStatusVariable (
IN EFI_CAPSULE_HEADER *CapsuleHeader,
IN EFI_STATUS CapsuleStatus
)
{
EFI_CAPSULE_RESULT_VARIABLE_HEADER CapsuleResultVariable;
EFI_STATUS Status;
CapsuleResultVariable.VariableTotalSize = sizeof(CapsuleResultVariable);
CapsuleResultVariable.Reserved = 0;
CopyGuid (&CapsuleResultVariable.CapsuleGuid, &CapsuleHeader->CapsuleGuid);
ZeroMem(&CapsuleResultVariable.CapsuleProcessed, sizeof(CapsuleResultVariable.CapsuleProcessed));
gRT->GetTime(&CapsuleResultVariable.CapsuleProcessed, NULL);
CapsuleResultVariable.CapsuleStatus = CapsuleStatus;
Status = EFI_SUCCESS;
if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
Status = WriteNewCapsuleResultVariable(&CapsuleResultVariable, sizeof(CapsuleResultVariable));
}
return Status;
}
/**
Record FMP capsule status variable and to local cache.
@param[in] CapsuleHeader The capsule image header
@param[in] CapsuleStatus The capsule process stauts
@param[in] PayloadIndex FMP payload index
@param[in] ImageHeader FMP image header
@param[in] FmpDevicePath DevicePath associated with the FMP producer
@param[in] CapFileName Capsule file name
@retval EFI_SUCCESS The capsule status variable is recorded.
@retval EFI_OUT_OF_RESOURCES No resource to record the capsule status variable.
**/
EFI_STATUS
RecordFmpCapsuleStatusVariable (
IN EFI_CAPSULE_HEADER *CapsuleHeader,
IN EFI_STATUS CapsuleStatus,
IN UINTN PayloadIndex,
IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader,
IN EFI_DEVICE_PATH_PROTOCOL *FmpDevicePath, OPTIONAL
IN CHAR16 *CapFileName OPTIONAL
)
{
EFI_CAPSULE_RESULT_VARIABLE_HEADER *CapsuleResultVariableHeader;
EFI_CAPSULE_RESULT_VARIABLE_FMP *CapsuleResultVariableFmp;
EFI_STATUS Status;
UINT8 *CapsuleResultVariable;
UINTN CapsuleResultVariableSize;
CHAR16 *DevicePathStr;
UINTN DevicePathStrSize;
UINTN CapFileNameSize;
DevicePathStr = NULL;
CapFileNameSize = sizeof(CHAR16);
if (FmpDevicePath != NULL) {
DevicePathStr = ConvertDevicePathToText (FmpDevicePath, FALSE, FALSE);
}
if (DevicePathStr != NULL) {
DevicePathStrSize = StrSize(DevicePathStr);
} else {
DevicePathStrSize = sizeof(CHAR16);
}
if (CapFileName != NULL) {
CapFileNameSize = StrSize(CapFileName);
}
//
// Allocate room for CapsuleFileName.
//
CapsuleResultVariableSize = sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER) + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP) + CapFileNameSize + DevicePathStrSize;
CapsuleResultVariable = AllocateZeroPool (CapsuleResultVariableSize);
if (CapsuleResultVariable == NULL) {
return EFI_OUT_OF_RESOURCES;
}
CapsuleResultVariableHeader = (VOID *)CapsuleResultVariable;
CapsuleResultVariableHeader->VariableTotalSize = (UINT32)CapsuleResultVariableSize;
CapsuleResultVariableHeader->Reserved = 0;
CopyGuid(&CapsuleResultVariableHeader->CapsuleGuid, &CapsuleHeader->CapsuleGuid);
ZeroMem(&CapsuleResultVariableHeader->CapsuleProcessed, sizeof(CapsuleResultVariableHeader->CapsuleProcessed));
gRT->GetTime(&CapsuleResultVariableHeader->CapsuleProcessed, NULL);
CapsuleResultVariableHeader->CapsuleStatus = CapsuleStatus;
CapsuleResultVariableFmp = (VOID *)(CapsuleResultVariable + sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER));
CapsuleResultVariableFmp->Version = 0x1;
CapsuleResultVariableFmp->PayloadIndex = (UINT8)PayloadIndex;
CapsuleResultVariableFmp->UpdateImageIndex = ImageHeader->UpdateImageIndex;
CopyGuid (&CapsuleResultVariableFmp->UpdateImageTypeId, &ImageHeader->UpdateImageTypeId);
if (CapFileName != NULL) {
CopyMem((UINT8 *)CapsuleResultVariableFmp + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP), CapFileName, CapFileNameSize);
}
if (DevicePathStr != NULL) {
CopyMem ((UINT8 *)CapsuleResultVariableFmp + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP) + CapFileNameSize, DevicePathStr, DevicePathStrSize);
FreePool (DevicePathStr);
DevicePathStr = NULL;
}
Status = EFI_SUCCESS;
if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
Status = WriteNewCapsuleResultVariable(CapsuleResultVariable, CapsuleResultVariableSize);
}
FreePool (CapsuleResultVariable);
return Status;
}
/**
Initialize CapsuleMax variables.
@param[in] VariablePolicy The pointer of variable lock policy
**/
VOID
InitCapsuleMaxVariable (
EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy
)
{
EFI_STATUS Status;
UINTN Size;
CHAR16 CapsuleMaxStr[sizeof("Capsule####")];
UnicodeSPrint(
CapsuleMaxStr,
sizeof(CapsuleMaxStr),
L"Capsule%04x",
PcdGet16(PcdCapsuleMax)
);
Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
Status = gRT->SetVariable(
L"CapsuleMax",
&gEfiCapsuleReportGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
Size,
CapsuleMaxStr
);
if (!EFI_ERROR(Status)) {
// Lock it per UEFI spec.
LockVariable (gEfiCapsuleReportGuid, L"CapsuleMax", VariablePolicy);
}
}
/**
Initialize CapsuleLast variables.
@param[in] VariablePolicy The pointer of variable lock policy
**/
VOID
InitCapsuleLastVariable (
EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy
)
{
EFI_STATUS Status;
EFI_BOOT_MODE BootMode;
VOID *CapsuleResult;
UINTN Size;
CHAR16 CapsuleLastStr[sizeof("Capsule####")];
BootMode = GetBootModeHob();
if (BootMode == BOOT_ON_FLASH_UPDATE) {
Status = gRT->SetVariable(
L"CapsuleLast",
&gEfiCapsuleReportGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
0,
NULL
);
// Do not lock it because it will be updated later.
} else {
//
// Check if OS/APP cleared L"Capsule####"
//
ZeroMem(CapsuleLastStr, sizeof(CapsuleLastStr));
Size = sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminator
Status = gRT->GetVariable(
L"CapsuleLast",
&gEfiCapsuleReportGuid,
NULL,
&Size,
CapsuleLastStr
);
if (!EFI_ERROR(Status)) {
//
// L"CapsuleLast" is got, check if data is there.
//
Status = GetVariable2 (
CapsuleLastStr,
&gEfiCapsuleReportGuid,
(VOID **) &CapsuleResult,
NULL
);
if (EFI_ERROR(Status)) {
//
// If no data, delete L"CapsuleLast"
//
Status = gRT->SetVariable(
L"CapsuleLast",
&gEfiCapsuleReportGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
0,
NULL
);
} else {
if (CapsuleResult != NULL) {
FreePool (CapsuleResult);
}
}
}
// Lock it in normal boot path per UEFI spec.
LockVariable (gEfiCapsuleReportGuid, L"CapsuleLast", VariablePolicy);
}
}
/**
Initialize capsule update variables.
**/
VOID
InitCapsuleUpdateVariable (
VOID
)
{
EFI_STATUS Status;
UINTN Index;
CHAR16 CapsuleVarName[30];
CHAR16 *TempVarName;
//
// Clear all the capsule variables CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
// as early as possible which will avoid the next time boot after the capsule update
// will still into the capsule loop
//
StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CapsuleVarName[0]), EFI_CAPSULE_VARIABLE_NAME);
TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
Index = 0;
while (TRUE) {
if (Index > 0) {
UnicodeValueToStringS (
TempVarName,
sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),
0,
Index,
0
);
}
Status = gRT->SetVariable (
CapsuleVarName,
&gEfiCapsuleVendorGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
0,
(VOID *)NULL
);
if (EFI_ERROR (Status)) {
//
// There is no capsule variables, quit
//
break;
}
Index++;
}
}
/**
Initialize capsule relocation info variable.
@param[in] VariablePolicy The pointer of variable lock policy
**/
VOID
InitCapsuleRelocationInfo (
EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy
)
{
CoDClearCapsuleRelocationInfo();
//
// Unlock Capsule On Disk relocation Info variable only when Capsule On Disk flag is enabled
//
if (!CoDCheckCapsuleOnDiskFlag()) {
LockVariable (gEfiCapsuleVendorGuid, COD_RELOCATION_INFO_VAR_NAME, VariablePolicy);
}
}
/**
Initialize capsule related variables.
**/
VOID
InitCapsuleVariable (
VOID
)
{
EFI_STATUS Status;
EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy;
// Locate the VariablePolicy protocol
Status = gBS->LocateProtocol (&gEdkiiVariablePolicyProtocolGuid, NULL, (VOID**)&VariablePolicy);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "DxeCapsuleReportLib %a - Could not locate VariablePolicy protocol! %r\n", __FUNCTION__, Status));
ASSERT_EFI_ERROR (Status);
}
InitCapsuleUpdateVariable();
InitCapsuleMaxVariable (VariablePolicy);
InitCapsuleLastVariable (VariablePolicy);
InitCapsuleRelocationInfo (VariablePolicy);
//
// No need to clear L"Capsule####", because OS/APP should refer L"CapsuleLast"
// to check status and delete them.
//
}
|