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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/** @file
Esrt management module.
Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "EsrtImpl.h"
//
// Module globals.
//
ESRT_PRIVATE_DATA mPrivate;
ESRT_MANAGEMENT_PROTOCOL mEsrtManagementProtocolTemplate = {
EsrtDxeGetEsrtEntry,
EsrtDxeUpdateEsrtEntry,
EsrtDxeRegisterEsrtEntry,
EsrtDxeUnRegisterEsrtEntry,
EsrtDxeSyncFmp,
EsrtDxeLockEsrtRepository
};
/**
Get ESRT entry from ESRT Cache by FwClass Guid
@param[in] FwClass FwClass of Esrt entry to get
@param[in, out] Entry Esrt entry returned
@retval EFI_SUCCESS The variable saving this Esrt Entry exists.
@retval EF_NOT_FOUND No correct variable found.
@retval EFI_WRITE_PROTECTED ESRT Cache repository is locked
**/
EFI_STATUS
EFIAPI
EsrtDxeGetEsrtEntry(
IN EFI_GUID *FwClass,
IN OUT EFI_SYSTEM_RESOURCE_ENTRY *Entry
)
{
EFI_STATUS Status;
if (FwClass == NULL || Entry == NULL) {
return EFI_INVALID_PARAMETER;
}
Status = EfiAcquireLockOrFail (&mPrivate.NonFmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Find in Non-FMP Cached Esrt Repository
//
Status = GetEsrtEntry(
FwClass,
ESRT_FROM_NONFMP,
Entry
);
EfiReleaseLock(&mPrivate.NonFmpLock);
if (EFI_ERROR(Status)) {
Status = EfiAcquireLockOrFail (&mPrivate.FmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Find in FMP Cached Esrt NV Variable
//
Status = GetEsrtEntry(
FwClass,
ESRT_FROM_FMP,
Entry
);
EfiReleaseLock(&mPrivate.FmpLock);
}
return Status;
}
/**
Update one ESRT entry in ESRT Cache.
@param[in] Entry Esrt entry to be updated
@retval EFI_SUCCESS Successfully update an ESRT entry in cache.
@retval EFI_INVALID_PARAMETER Entry does't exist in ESRT Cache
@retval EFI_WRITE_PROTECTED ESRT Cache repositoy is locked
**/
EFI_STATUS
EFIAPI
EsrtDxeUpdateEsrtEntry(
IN EFI_SYSTEM_RESOURCE_ENTRY *Entry
)
{
EFI_STATUS Status;
if (Entry == NULL) {
return EFI_INVALID_PARAMETER;
}
Status = EfiAcquireLockOrFail (&mPrivate.FmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
Status = UpdateEsrtEntry(Entry, ESRT_FROM_FMP);
if (!EFI_ERROR(Status)) {
EfiReleaseLock(&mPrivate.FmpLock);
return Status;
}
EfiReleaseLock(&mPrivate.FmpLock);
Status = EfiAcquireLockOrFail (&mPrivate.NonFmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
Status = UpdateEsrtEntry(Entry, ESRT_FROM_NONFMP);
EfiReleaseLock(&mPrivate.NonFmpLock);
return Status;
}
/**
Non-FMP instance to unregister Esrt Entry from ESRT Cache.
@param[in] FwClass FwClass of Esrt entry to Unregister
@retval EFI_SUCCESS Insert all entries Successfully
@retval EFI_NOT_FOUND Entry of FwClass does not exsit
**/
EFI_STATUS
EFIAPI
EsrtDxeUnRegisterEsrtEntry(
IN EFI_GUID *FwClass
)
{
EFI_STATUS Status;
if (FwClass == NULL) {
return EFI_INVALID_PARAMETER;
}
Status = EfiAcquireLockOrFail (&mPrivate.NonFmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
Status = DeleteEsrtEntry(FwClass, ESRT_FROM_NONFMP);
EfiReleaseLock(&mPrivate.NonFmpLock);
return Status;
}
/**
Non-FMP instance to register one ESRT entry into ESRT Cache.
@param[in] Entry Esrt entry to be set
@retval EFI_SUCCESS Successfully set a variable.
@retval EFI_INVALID_PARAMETER ESRT Entry is already exist
@retval EFI_OUT_OF_RESOURCES Non-FMP ESRT repository is full
**/
EFI_STATUS
EFIAPI
EsrtDxeRegisterEsrtEntry(
IN EFI_SYSTEM_RESOURCE_ENTRY *Entry
)
{
EFI_STATUS Status;
EFI_SYSTEM_RESOURCE_ENTRY EsrtEntryTmp;
if (Entry == NULL) {
return EFI_INVALID_PARAMETER;
}
Status = EfiAcquireLockOrFail (&mPrivate.NonFmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
Status = GetEsrtEntry(
&Entry->FwClass,
ESRT_FROM_NONFMP,
&EsrtEntryTmp
);
if (Status == EFI_NOT_FOUND) {
Status = InsertEsrtEntry(Entry, ESRT_FROM_NONFMP);
}
EfiReleaseLock(&mPrivate.NonFmpLock);
return Status;
}
/**
This function syn up Cached ESRT with data from FMP instances
Function should be called after Connect All in order to locate all FMP protocols
installed.
@retval EFI_SUCCESS Successfully sync cache repository from FMP instances
@retval EFI_NOT_FOUND No FMP Instance are found
@retval EFI_OUT_OF_RESOURCES Resource allocaton fail
**/
EFI_STATUS
EFIAPI
EsrtDxeSyncFmp(
VOID
)
{
EFI_STATUS Status;
UINTN Index1;
UINTN Index2;
UINTN Index3;
EFI_HANDLE *HandleBuffer;
EFI_FIRMWARE_MANAGEMENT_PROTOCOL **FmpBuf;
UINTN NumberOfHandles;
UINTN *DescriptorSizeBuf;
EFI_FIRMWARE_IMAGE_DESCRIPTOR **FmpImageInfoBuf;
EFI_FIRMWARE_IMAGE_DESCRIPTOR *TempFmpImageInfo;
UINT8 *FmpImageInfoCountBuf;
UINT32 *FmpImageInfoDescriptorVerBuf;
UINTN ImageInfoSize;
UINT32 PackageVersion;
CHAR16 *PackageVersionName;
EFI_SYSTEM_RESOURCE_ENTRY *EsrtRepositoryNew;
UINTN EntryNumNew;
NumberOfHandles = 0;
EntryNumNew = 0;
FmpBuf = NULL;
HandleBuffer = NULL;
FmpImageInfoBuf = NULL;
FmpImageInfoCountBuf = NULL;
PackageVersionName = NULL;
DescriptorSizeBuf = NULL;
FmpImageInfoDescriptorVerBuf = NULL;
EsrtRepositoryNew = NULL;
//
// Get image information from all FMP protocol
//
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiFirmwareManagementProtocolGuid,
NULL,
&NumberOfHandles,
&HandleBuffer
);
if (Status == EFI_NOT_FOUND) {
EntryNumNew = 0;
goto UPDATE_REPOSITORY;
} else if (EFI_ERROR(Status)){
goto END;
}
//
// Allocate buffer to hold new FMP ESRT Cache repository
//
EsrtRepositoryNew = AllocateZeroPool(PcdGet32(PcdMaxFmpEsrtCacheNum) * sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
if (EsrtRepositoryNew == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
FmpBuf = AllocatePool(sizeof(EFI_FIRMWARE_MANAGEMENT_PROTOCOL *) * NumberOfHandles);
if (FmpBuf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
FmpImageInfoBuf = AllocateZeroPool(sizeof(EFI_FIRMWARE_IMAGE_DESCRIPTOR *) * NumberOfHandles);
if (FmpImageInfoBuf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
FmpImageInfoCountBuf = AllocateZeroPool(sizeof(UINT8) * NumberOfHandles);
if (FmpImageInfoCountBuf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
DescriptorSizeBuf = AllocateZeroPool(sizeof(UINTN) * NumberOfHandles);
if (DescriptorSizeBuf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
FmpImageInfoDescriptorVerBuf = AllocateZeroPool(sizeof(UINT32) * NumberOfHandles);
if (FmpImageInfoDescriptorVerBuf == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
//
// Get all FmpImageInfo Descriptor into FmpImageInfoBuf
//
for (Index1 = 0; Index1 < NumberOfHandles; Index1++){
Status = gBS->HandleProtocol(
HandleBuffer[Index1],
&gEfiFirmwareManagementProtocolGuid,
(VOID **)&FmpBuf[Index1]
);
if (EFI_ERROR(Status)) {
continue;
}
ImageInfoSize = 0;
Status = FmpBuf[Index1]->GetImageInfo (
FmpBuf[Index1],
&ImageInfoSize,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
);
if (Status == EFI_BUFFER_TOO_SMALL) {
FmpImageInfoBuf[Index1] = AllocateZeroPool(ImageInfoSize);
if (FmpImageInfoBuf[Index1] == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto END;
}
} else {
continue;
}
PackageVersionName = NULL;
Status = FmpBuf[Index1]->GetImageInfo (
FmpBuf[Index1],
&ImageInfoSize,
FmpImageInfoBuf[Index1],
&FmpImageInfoDescriptorVerBuf[Index1],
&FmpImageInfoCountBuf[Index1],
&DescriptorSizeBuf[Index1],
&PackageVersion,
&PackageVersionName
);
//
// If FMP GetInformation interface failed, skip this resource
//
if (EFI_ERROR(Status)){
FmpImageInfoCountBuf[Index1] = 0;
continue;
}
if (PackageVersionName != NULL) {
FreePool(PackageVersionName);
}
}
//
// Create new FMP cache repository based on FmpImageInfoBuf
//
for (Index2 = 0; Index2 < NumberOfHandles; Index2++){
TempFmpImageInfo = FmpImageInfoBuf[Index2];
for (Index3 = 0; Index3 < FmpImageInfoCountBuf[Index2]; Index3++){
if ((TempFmpImageInfo->AttributesSupported & IMAGE_ATTRIBUTE_IN_USE) != 0
&& (TempFmpImageInfo->AttributesSetting & IMAGE_ATTRIBUTE_IN_USE) != 0){
//
// Always put the first smallest version of Image info into ESRT cache
//
for(Index1 = 0; Index1 < EntryNumNew; Index1++) {
if (CompareGuid(&EsrtRepositoryNew[Index1].FwClass, &TempFmpImageInfo->ImageTypeId)) {
if(EsrtRepositoryNew[Index1].FwVersion > TempFmpImageInfo->Version) {
SetEsrtEntryFromFmpInfo(&EsrtRepositoryNew[Index1], TempFmpImageInfo, FmpImageInfoDescriptorVerBuf[Index2]);
}
break;
}
}
//
// New ImageTypeId can't be found in EsrtRepositoryNew. Create a new one
//
if (Index1 == EntryNumNew){
SetEsrtEntryFromFmpInfo(&EsrtRepositoryNew[EntryNumNew], TempFmpImageInfo, FmpImageInfoDescriptorVerBuf[Index2]);
EntryNumNew++;
if (EntryNumNew >= PcdGet32(PcdMaxFmpEsrtCacheNum)) {
break;
}
}
}
//
// Use DescriptorSize to move ImageInfo Pointer to stay compatible with different ImageInfo version
//
TempFmpImageInfo = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)((UINT8 *)TempFmpImageInfo + DescriptorSizeBuf[Index2]);
}
}
UPDATE_REPOSITORY:
Status = EfiAcquireLockOrFail (&mPrivate.FmpLock);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gRT->SetVariable(
EFI_ESRT_FMP_VARIABLE_NAME,
&gEfiCallerIdGuid,
EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
EntryNumNew * sizeof(EFI_SYSTEM_RESOURCE_ENTRY),
EsrtRepositoryNew
);
EfiReleaseLock(&mPrivate.FmpLock);
END:
if (EsrtRepositoryNew != NULL) {
FreePool(EsrtRepositoryNew);
}
if (HandleBuffer != NULL) {
FreePool(HandleBuffer);
}
if (FmpBuf != NULL) {
FreePool(FmpBuf);
}
if (FmpImageInfoCountBuf != NULL) {
FreePool(FmpImageInfoCountBuf);
}
if (DescriptorSizeBuf != NULL) {
FreePool(DescriptorSizeBuf);
}
if (FmpImageInfoDescriptorVerBuf != NULL) {
FreePool(FmpImageInfoDescriptorVerBuf);
}
if (FmpImageInfoBuf != NULL) {
for (Index1 = 0; Index1 < NumberOfHandles; Index1++){
if (FmpImageInfoBuf[Index1] != NULL) {
FreePool(FmpImageInfoBuf[Index1]);
}
}
FreePool(FmpImageInfoBuf);
}
return Status;
}
/**
This function locks up Esrt repository to be readonly. It should be called
before gEfiEndOfDxeEventGroupGuid event signaled
@retval EFI_SUCCESS Locks up FMP Non-FMP repository successfully
**/
EFI_STATUS
EFIAPI
EsrtDxeLockEsrtRepository(
VOID
)
{
EFI_STATUS Status;
EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
//
// Mark ACPI_GLOBAL_VARIABLE variable to read-only if the Variable Lock protocol exists
//
Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);
if (!EFI_ERROR (Status)) {
Status = VariableLock->RequestToLock (VariableLock, EFI_ESRT_FMP_VARIABLE_NAME, &gEfiCallerIdGuid);
DEBUG((EFI_D_INFO, "EsrtDxe Lock EsrtFmp Variable Status 0x%x", Status));
Status = VariableLock->RequestToLock (VariableLock, EFI_ESRT_NONFMP_VARIABLE_NAME, &gEfiCallerIdGuid);
DEBUG((EFI_D_INFO, "EsrtDxe Lock EsrtNonFmp Variable Status 0x%x", Status));
}
return Status;
}
/**
Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
install the Esrt Table into system configuration table
@param[in] Event The Event that is being processed.
@param[in] Context The Event Context.
**/
VOID
EFIAPI
EsrtReadyToBootEventNotify (
IN EFI_EVENT Event,
IN VOID *Context
)
{
EFI_STATUS Status;
EFI_SYSTEM_RESOURCE_TABLE *EsrtTable;
EFI_SYSTEM_RESOURCE_ENTRY *FmpEsrtRepository;
EFI_SYSTEM_RESOURCE_ENTRY *NonFmpEsrtRepository;
UINTN FmpRepositorySize;
UINTN NonFmpRepositorySize;
FmpEsrtRepository = NULL;
NonFmpEsrtRepository = NULL;
FmpRepositorySize = 0;
NonFmpRepositorySize = 0;
Status = EfiAcquireLockOrFail (&mPrivate.NonFmpLock);
if (EFI_ERROR (Status)) {
return;
}
Status = GetVariable2 (
EFI_ESRT_NONFMP_VARIABLE_NAME,
&gEfiCallerIdGuid,
(VOID **) &NonFmpEsrtRepository,
&NonFmpRepositorySize
);
if (EFI_ERROR(Status)) {
NonFmpRepositorySize = 0;
}
if (NonFmpRepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY) != 0) {
DEBUG((EFI_D_ERROR, "NonFmp Repository Corrupt. Need to rebuild NonFmp Repository.\n"));
NonFmpRepositorySize = 0;
}
EfiReleaseLock(&mPrivate.NonFmpLock);
Status = EfiAcquireLockOrFail (&mPrivate.FmpLock);
Status = GetVariable2 (
EFI_ESRT_FMP_VARIABLE_NAME,
&gEfiCallerIdGuid,
(VOID **) &FmpEsrtRepository,
&FmpRepositorySize
);
if (EFI_ERROR(Status)) {
FmpRepositorySize = 0;
}
if (FmpRepositorySize % sizeof(EFI_SYSTEM_RESOURCE_ENTRY) != 0) {
DEBUG((EFI_D_ERROR, "Fmp Repository Corrupt. Need to rebuild Fmp Repository.\n"));
FmpRepositorySize = 0;
}
EfiReleaseLock(&mPrivate.FmpLock);
//
// Skip ESRT table publish if no ESRT entry exists
//
if (NonFmpRepositorySize + FmpRepositorySize == 0) {
goto EXIT;
}
EsrtTable = AllocatePool(sizeof(EFI_SYSTEM_RESOURCE_TABLE) + NonFmpRepositorySize + FmpRepositorySize);
if (EsrtTable == NULL) {
DEBUG ((EFI_D_ERROR, "Esrt table memory allocation failure\n"));
goto EXIT;
}
EsrtTable->FwResourceVersion = EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION;
EsrtTable->FwResourceCount = (UINT32)((NonFmpRepositorySize + FmpRepositorySize) / sizeof(EFI_SYSTEM_RESOURCE_ENTRY));
EsrtTable->FwResourceCountMax = PcdGet32(PcdMaxNonFmpEsrtCacheNum) + PcdGet32(PcdMaxFmpEsrtCacheNum);
if (NonFmpRepositorySize != 0 && NonFmpEsrtRepository != NULL) {
CopyMem(EsrtTable + 1, NonFmpEsrtRepository, NonFmpRepositorySize);
}
if (FmpRepositorySize != 0 && FmpEsrtRepository != NULL) {
CopyMem((UINT8 *)(EsrtTable + 1) + NonFmpRepositorySize, FmpEsrtRepository, FmpRepositorySize);
}
//
// Publish Esrt to system config table
//
Status = gBS->InstallConfigurationTable (&gEfiSystemResourceTableGuid, EsrtTable);
//
// Only one successful install
//
gBS->CloseEvent(Event);
EXIT:
if (FmpEsrtRepository != NULL) {
FreePool(FmpEsrtRepository);
}
if (NonFmpEsrtRepository != NULL) {
FreePool(NonFmpEsrtRepository);
}
}
/**
The module Entry Point of the Esrt DXE driver that manages cached ESRT repository
& publishes ESRT table
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval Other Some error occurs when executing this entry point.
**/
EFI_STATUS
EFIAPI
EsrtDxeEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EfiInitializeLock (&mPrivate.FmpLock, TPL_CALLBACK);
EfiInitializeLock (&mPrivate.NonFmpLock, TPL_CALLBACK);
//
// Install Esrt management Protocol
//
Status = gBS->InstallMultipleProtocolInterfaces (
&mPrivate.Handle,
&gEsrtManagementProtocolGuid,
&mEsrtManagementProtocolTemplate,
NULL
);
ASSERT_EFI_ERROR (Status);
//
// Register notify function to install Esrt Table on ReadyToBoot Event.
//
Status = gBS->CreateEventEx (
EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
EsrtReadyToBootEventNotify,
NULL,
&gEfiEventReadyToBootGuid,
&mPrivate.Event
);
ASSERT_EFI_ERROR (Status);
return EFI_SUCCESS;
}
|