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
|
/** @file
Esrt management implementation.
Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "EsrtImpl.h"
/**
Find Esrt Entry stored in ESRT repository.
@param[in] FwClass Firmware class guid in Esrt entry
@param[in] Attribute Esrt from Non FMP or FMP instance
@param[out] Entry Esrt entry returned
@retval EFI_SUCCESS Successfully find an Esrt entry
@retval EF_NOT_FOUND No Esrt entry found
**/
EFI_STATUS
GetEsrtEntry (
IN EFI_GUID *FwClass,
IN UINTN Attribute,
OUT EFI_SYSTEM_RESOURCE_ENTRY *Entry
)
{
EFI_STATUS Status;
CHAR16 *VariableName;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepository;
UINTN RepositorySize;
UINTN Index;
UINTN EsrtNum;
EsrtRepository = NULL;
//
// Get Esrt index buffer
//
if (Attribute == ESRT_FROM_FMP) {
VariableName = EFI_ESRT_FMP_VARIABLE_NAME;
} else {
VariableName = EFI_ESRT_NONFMP_VARIABLE_NAME;
}
Status = GetVariable2 (
VariableName,
&gEfiCallerIdGuid,
(VOID **) &EsrtRepository,
&RepositorySize
);
if (EFI_ERROR(Status)) {
goto EXIT;
}
if (RepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY) != 0) {
DEBUG((DEBUG_ERROR, "Repository Corrupt. Need to rebuild Repository.\n"));
Status = EFI_ABORTED;
goto EXIT;
}
Status = EFI_NOT_FOUND;
EsrtNum = RepositorySize/sizeof(EFI_SYSTEM_RESOURCE_ENTRY);
for (Index = 0; Index < EsrtNum; Index++) {
if (CompareGuid(FwClass, &EsrtRepository[Index].FwClass)) {
CopyMem(Entry, &EsrtRepository[Index], sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
Status = EFI_SUCCESS;
break;
}
}
EXIT:
if (EsrtRepository != NULL) {
FreePool(EsrtRepository);
}
return Status;
}
/**
Insert a new ESRT entry into ESRT Cache repository.
@param[in] Entry Esrt entry to be set
@param[in] Attribute Esrt from Esrt private protocol or FMP instance
@retval EFI_SUCCESS Successfully set a variable.
**/
EFI_STATUS
InsertEsrtEntry(
IN EFI_SYSTEM_RESOURCE_ENTRY *Entry,
UINTN Attribute
)
{
EFI_STATUS Status;
CHAR16 *VariableName;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepository;
UINTN RepositorySize;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepositoryNew;
EsrtRepository = NULL;
EsrtRepositoryNew = NULL;
//
// Get Esrt index buffer
//
if (Attribute == ESRT_FROM_FMP) {
VariableName = EFI_ESRT_FMP_VARIABLE_NAME;
} else {
VariableName = EFI_ESRT_NONFMP_VARIABLE_NAME;
}
Status = GetVariable2 (
VariableName,
&gEfiCallerIdGuid,
(VOID **) &EsrtRepository,
&RepositorySize
);
if (Status == EFI_NOT_FOUND) {
//
// If not exist, create new Esrt cache repository
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
sizeof(EFI_SYSTEM_RESOURCE_ENTRY),
Entry
);
return Status;
} else if (Status == EFI_SUCCESS) {
//
// if exist, update Esrt cache repository
//
if (RepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY) != 0) {
DEBUG((DEBUG_ERROR, "Repository Corrupt. Need to rebuild Repository.\n"));
//
// Repository is corrupt. Clear Repository before insert new entry
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
0,
EsrtRepository
);
FreePool(EsrtRepository);
RepositorySize = 0;
EsrtRepository = NULL;
}
//
// Check Repository size constraint
//
if ((Attribute == ESRT_FROM_FMP && RepositorySize >= PcdGet32(PcdMaxFmpEsrtCacheNum) * sizeof(EFI_SYSTEM_RESOURCE_ENTRY))
||(Attribute == ESRT_FROM_NONFMP && RepositorySize >= PcdGet32(PcdMaxNonFmpEsrtCacheNum) * sizeof(EFI_SYSTEM_RESOURCE_ENTRY)) ) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
EsrtRepositoryNew = AllocatePool(RepositorySize + sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
if (EsrtRepositoryNew == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
if (RepositorySize != 0 && EsrtRepository != NULL) {
CopyMem(EsrtRepositoryNew, EsrtRepository, RepositorySize);
}
CopyMem((UINT8 *)EsrtRepositoryNew + RepositorySize, Entry, sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
RepositorySize + sizeof(EFI_SYSTEM_RESOURCE_ENTRY),
EsrtRepositoryNew
);
}
EXIT:
if (EsrtRepository != NULL) {
FreePool(EsrtRepository);
}
if (EsrtRepositoryNew != NULL) {
FreePool(EsrtRepositoryNew);
}
return Status;
}
/**
Delete ESRT Entry from ESRT repository.
@param[in] FwClass FwClass of Esrt entry to delete
@param[in] Attribute Esrt from Esrt private protocol or FMP instance
@retval EFI_SUCCESS Insert all entries Successfully
@retval EFI_NOT_FOUND ESRT entry with FwClass doesn't exsit
**/
EFI_STATUS
DeleteEsrtEntry(
IN EFI_GUID *FwClass,
IN UINTN Attribute
)
{
EFI_STATUS Status;
CHAR16 *VariableName;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepository;
UINTN RepositorySize;
UINTN Index;
UINTN EsrtNum;
EsrtRepository = NULL;
//
// Get Esrt index buffer
//
if (Attribute == ESRT_FROM_FMP) {
VariableName = EFI_ESRT_FMP_VARIABLE_NAME;
} else {
VariableName = EFI_ESRT_NONFMP_VARIABLE_NAME;
}
Status = GetVariable2 (
VariableName,
&gEfiCallerIdGuid,
(VOID **) &EsrtRepository,
&RepositorySize
);
if (EFI_ERROR(Status)) {
goto EXIT;
}
if (EsrtRepository == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
if ((RepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY)) != 0) {
DEBUG((DEBUG_ERROR, "Repository Corrupt. Need to rebuild Repository.\n"));
//
// Repository is corrupt. Clear Repository before insert new entry
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
0,
EsrtRepository
);
goto EXIT;
}
Status = EFI_NOT_FOUND;
EsrtNum = RepositorySize/sizeof(EFI_SYSTEM_RESOURCE_ENTRY);
for (Index = 0; Index < EsrtNum; Index++) {
//
// Delete Esrt entry if it is found in repository
//
if (CompareGuid(FwClass, &EsrtRepository[Index].FwClass)) {
//
// If delete Esrt entry is not at the rail
//
if (Index < EsrtNum - 1) {
CopyMem(&EsrtRepository[Index], &EsrtRepository[Index + 1], (EsrtNum - Index - 1) * sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
}
//
// Update New Repository
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
(EsrtNum - 1) * sizeof(EFI_SYSTEM_RESOURCE_ENTRY),
EsrtRepository
);
break;
}
}
EXIT:
if (EsrtRepository != NULL) {
FreePool(EsrtRepository);
}
return Status;
}
/**
Update one ESRT entry in ESRT repository
@param[in] Entry Esrt entry to be set
@param[in] Attribute Esrt from Non Esrt or FMP instance
@retval EFI_SUCCESS Successfully Update a variable.
@retval EFI_NOT_FOUND The Esrt enry doesn't exist
**/
EFI_STATUS
UpdateEsrtEntry(
IN EFI_SYSTEM_RESOURCE_ENTRY *Entry,
UINTN Attribute
)
{
EFI_STATUS Status;
CHAR16 *VariableName;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepository;
UINTN RepositorySize;
UINTN Index;
UINTN EsrtNum;
EsrtRepository = NULL;
//
// Get Esrt index buffer
//
if (Attribute == ESRT_FROM_FMP) {
VariableName = EFI_ESRT_FMP_VARIABLE_NAME;
} else {
VariableName = EFI_ESRT_NONFMP_VARIABLE_NAME;
}
Status = GetVariable2 (
VariableName,
&gEfiCallerIdGuid,
(VOID **) &EsrtRepository,
&RepositorySize
);
if (EsrtRepository == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
if (!EFI_ERROR(Status)) {
//
// if exist, update Esrt cache repository
//
if (RepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY) != 0) {
DEBUG((DEBUG_ERROR, "Repository Corrupt. Need to rebuild Repository.\n"));
//
// Repository is corrupt. Clear Repository before insert new entry
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
0,
EsrtRepository
);
Status = EFI_NOT_FOUND;
goto EXIT;
}
Status = EFI_NOT_FOUND;
EsrtNum = RepositorySize/sizeof(EFI_SYSTEM_RESOURCE_ENTRY);
for (Index = 0; Index < EsrtNum; Index++) {
//
// Update Esrt entry if it is found in repository
//
if (CompareGuid(&Entry->FwClass, &EsrtRepository[Index].FwClass)) {
CopyMem(&EsrtRepository[Index], Entry, sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
//
// Update New Repository
//
Status = gRT->SetVariable(
VariableName,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
RepositorySize,
EsrtRepository
);
break;
}
}
}
EXIT:
if (EsrtRepository != NULL) {
FreePool(EsrtRepository);
}
return Status;
}
/**
Return if this FMP is a system FMP or a device FMP, based upon FmpImageInfo.
@param[in] FmpImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR
@return TRUE It is a system FMP.
@return FALSE It is a device FMP.
**/
BOOLEAN
IsSystemFmp (
IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfo
)
{
GUID *Guid;
UINTN Count;
UINTN Index;
Guid = PcdGetPtr(PcdSystemFmpCapsuleImageTypeIdGuid);
Count = PcdGetSize(PcdSystemFmpCapsuleImageTypeIdGuid)/sizeof(GUID);
for (Index = 0; Index < Count; Index++, Guid++) {
if (CompareGuid(&FmpImageInfo->ImageTypeId, Guid)) {
return TRUE;
}
}
return FALSE;
}
/**
Init one ESRT entry according to input FmpImageInfo (V1, V2, V3) .
@param[in, out] EsrtEntry Esrt entry to be Init
@param[in] FmpImageInfo FMP image info descriptor
@param[in] DescriptorVersion FMP Image info descriptor version
**/
VOID
SetEsrtEntryFromFmpInfo (
IN OUT EFI_SYSTEM_RESOURCE_ENTRY *EsrtEntry,
IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfo,
IN UINT32 DescriptorVersion
)
{
EsrtEntry->FwVersion = FmpImageInfo->Version;
EsrtEntry->FwClass = FmpImageInfo->ImageTypeId;
if (IsSystemFmp(FmpImageInfo)) {
EsrtEntry->FwType = ESRT_FW_TYPE_SYSTEMFIRMWARE;
} else {
EsrtEntry->FwType = ESRT_FW_TYPE_DEVICEFIRMWARE;
}
EsrtEntry->LowestSupportedFwVersion = 0;
EsrtEntry->CapsuleFlags = 0;
EsrtEntry->LastAttemptVersion = 0;
EsrtEntry->LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
if (DescriptorVersion >= 2) {
//
// LowestSupportedImageVersion only available in FMP V2 or higher
//
EsrtEntry->LowestSupportedFwVersion = FmpImageInfo->LowestSupportedImageVersion;
}
if (DescriptorVersion >= 3) {
//
// LastAttemptVersion & LastAttemptStatus only available in FMP V3 or higher
//
EsrtEntry->LastAttemptVersion = FmpImageInfo->LastAttemptVersion;
EsrtEntry->LastAttemptStatus = FmpImageInfo->LastAttemptStatus;
}
//
// Set capsule customized flag
//
if ((FmpImageInfo->AttributesSupported & IMAGE_ATTRIBUTE_RESET_REQUIRED) != 0
&& (FmpImageInfo->AttributesSetting & IMAGE_ATTRIBUTE_RESET_REQUIRED) != 0) {
EsrtEntry->CapsuleFlags = PcdGet16(PcdSystemRebootAfterCapsuleProcessFlag);
}
}
|