forked from tianocore/edk2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFmpDxe.c
1991 lines (1712 loc) · 71.8 KB
/
FmpDxe.c
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
Produces a Firmware Management Protocol that supports updates to a firmware
image stored in a firmware device with platform and firmware device specific
information provided through PCDs and libraries.
Copyright (c) Microsoft Corporation.<BR>
Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "FmpDxe.h"
#include "VariableSupport.h"
///
/// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the
/// FILE_GUID must always be overridden in the <Defines> section to provide
/// the ESRT GUID value associated with the updatable firmware image. A
/// check is made in this module's driver entry point to verify that a
/// new FILE_GUID value has been defined.
///
const EFI_GUID mDefaultModuleFileGuid = {
0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }
};
///
/// TRUE if FmpDeviceLib manages a single firmware storage device.
///
BOOLEAN mFmpSingleInstance = FALSE;
///
/// Firmware Management Protocol instance that is initialized in the entry
/// point from PCD settings.
///
EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL mFmpProgress;
//
// Template of the private context structure for the Firmware Management
// Protocol instance
//
const FIRMWARE_MANAGEMENT_PRIVATE_DATA mFirmwareManagementPrivateDataTemplate = {
FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE, // Signature
NULL, // Handle
{ // Fmp
GetTheImageInfo,
GetTheImage,
SetTheImage,
CheckTheImage,
GetPackageInfo,
SetPackageInfo
},
FALSE, // DescriptorPopulated
{ // Desc
1, // ImageIndex
//
// ImageTypeId
//
{ 0x00000000, 0x0000,0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
},
1, // ImageId
NULL, // ImageIdName
0, // Version
NULL, // VersionName
0, // Size
0, // AttributesSupported
0, // AttributesSetting
0, // Compatibilities
0, // LowestSupportedImageVersion
0, // LastAttemptVersion
0, // LastAttemptStatus
0 // HardwareInstance
},
NULL, // ImageIdName
NULL, // VersionName
TRUE, // RuntimeVersionSupported
NULL, // FmpDeviceLockEvent
FALSE, // FmpDeviceLocked
NULL, // FmpDeviceContext
NULL, // VersionVariableName
NULL, // LsvVariableName
NULL, // LastAttemptStatusVariableName
NULL, // LastAttemptVersionVariableName
NULL, // FmpStateVariableName
TRUE // DependenciesSatisfied
};
///
/// GUID that is used to create event used to lock the firmware storage device.
///
EFI_GUID *mLockGuid = NULL;
///
/// Progress() function pointer passed into SetTheImage()
///
EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc = NULL;
///
/// Null-terminated Unicode string retrieved from PcdFmpDeviceImageIdName.
///
CHAR16 *mImageIdName = NULL;
/**
Callback function to report the process of the firmware updating.
Wrap the caller's version in this so that progress from the device lib is
within the expected range. Convert device lib 0% - 100% to 6% - 98%.
FmpDxe 1% - 5% for validation
FmpDeviceLib 6% - 98% for flashing/update
FmpDxe 99% - 100% finish
@param[in] Completion A value between 1 and 100 indicating the current
completion progress of the firmware update. Completion
progress is reported as from 1 to 100 percent. A value
of 0 is used by the driver to indicate that progress
reporting is not supported.
@retval EFI_SUCCESS The progress was updated.
@retval EFI_UNSUPPORTED Updating progress is not supported.
**/
EFI_STATUS
EFIAPI
FmpDxeProgress (
IN UINTN Completion
)
{
EFI_STATUS Status;
Status = EFI_UNSUPPORTED;
if (mProgressFunc == NULL) {
return Status;
}
//
// Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.
//
Status = mProgressFunc (((Completion * 92) / 100) + 6);
if (Status == EFI_UNSUPPORTED) {
mProgressFunc = NULL;
}
return Status;
}
/**
Returns a pointer to the ImageTypeId GUID value. An attempt is made to get
the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide
a GUID value, then PcdFmpDeviceImageTypeIdGuid is used. If the size of
PcdFmpDeviceImageTypeIdGuid is not the size of EFI_GUID, then gEfiCallerIdGuid
is returned.
@retval The ImageTypeId GUID
**/
EFI_GUID *
GetImageTypeIdGuid (
VOID
)
{
EFI_STATUS Status;
EFI_GUID *FmpDeviceLibGuid;
UINTN ImageTypeIdGuidSize;
FmpDeviceLibGuid = NULL;
Status = FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid);
if (EFI_ERROR (Status)) {
if (Status != EFI_UNSUPPORTED) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", mImageIdName, Status));
}
} else if (FmpDeviceLibGuid == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n", mImageIdName));
Status = EFI_NOT_FOUND;
}
if (EFI_ERROR (Status)) {
ImageTypeIdGuidSize = PcdGetSize (PcdFmpDeviceImageTypeIdGuid);
if (ImageTypeIdGuidSize == sizeof (EFI_GUID)) {
FmpDeviceLibGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceImageTypeIdGuid);
} else {
DEBUG ((
DEBUG_ERROR,
"FmpDxe(%s): Fall back to ImageTypeIdGuid of gEfiCallerIdGuid. FmpDxe error: misconfiguration\n",
mImageIdName
));
ASSERT (FALSE);
FmpDeviceLibGuid = &gEfiCallerIdGuid;
}
}
return FmpDeviceLibGuid;
}
/**
Returns a pointer to the Null-terminated Unicode ImageIdName string.
@retval Null-terminated Unicode ImageIdName string.
**/
CHAR16 *
GetImageTypeNameString (
VOID
)
{
return mImageIdName;
}
/**
Lowest supported version is a combo of three parts.
1. Check if the device lib has a lowest supported version
2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)
3. Check Fixed at build PCD
@param[in] Private Pointer to the private context structure for the
Firmware Management Protocol instance.
@retval The largest value
**/
UINT32
GetLowestSupportedVersion (
FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private
)
{
EFI_STATUS Status;
UINT32 DeviceLibLowestSupportedVersion;
UINT32 VariableLowestSupportedVersion;
UINT32 ReturnLsv;
//
// Get the LowestSupportedVersion.
//
if (!IsLowestSupportedVersionCheckRequired ()) {
//
// Any Version can pass the 0 LowestSupportedVersion check.
//
return 0;
}
ReturnLsv = PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion);
//
// Check the FmpDeviceLib
//
DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);
if (EFI_ERROR (Status)) {
DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
}
if (DeviceLibLowestSupportedVersion > ReturnLsv) {
ReturnLsv = DeviceLibLowestSupportedVersion;
}
//
// Check the lowest supported version UEFI variable for this device
//
VariableLowestSupportedVersion = GetLowestSupportedVersionFromVariable (Private);
if (VariableLowestSupportedVersion > ReturnLsv) {
ReturnLsv = VariableLowestSupportedVersion;
}
//
// Return the largest value
//
return ReturnLsv;
}
/**
Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the private
context structure.
@param[in] Private Pointer to the private context structure for the
Firmware Management Protocol instance.
**/
VOID
PopulateDescriptor (
FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private
)
{
EFI_STATUS Status;
UINT32 DependenciesSize;
if (Private == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): PopulateDescriptor() - Private is NULL.\n", mImageIdName));
return;
}
if (Private->DescriptorPopulated) {
return;
}
Private->Descriptor.ImageIndex = 1;
CopyGuid (&Private->Descriptor.ImageTypeId, GetImageTypeIdGuid ());
Private->Descriptor.ImageId = Private->Descriptor.ImageIndex;
Private->Descriptor.ImageIdName = GetImageTypeNameString ();
//
// Get the hardware instance from FmpDeviceLib
//
Status = FmpDeviceGetHardwareInstance (&Private->Descriptor.HardwareInstance);
if (Status == EFI_UNSUPPORTED) {
Private->Descriptor.HardwareInstance = 0;
}
//
// Generate UEFI Variable names used to store status information for this
// FMP instance.
//
GenerateFmpVariableNames (Private);
//
// Get the version. Some devices don't support getting the firmware version
// at runtime. If FmpDeviceLib does not support returning a version, then
// it is stored in a UEFI variable.
//
Status = FmpDeviceGetVersion (&Private->Descriptor.Version);
if (Status == EFI_UNSUPPORTED) {
Private->RuntimeVersionSupported = FALSE;
Private->Descriptor.Version = GetVersionFromVariable (Private);
} else if (EFI_ERROR (Status)) {
//
// Unexpected error. Use default version.
//
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetVersion() from FmpDeviceLib (%s) returned %r\n", mImageIdName, GetImageTypeNameString (), Status));
Private->Descriptor.Version = DEFAULT_VERSION;
}
//
// Free the current version name. Shouldn't really happen but this populate
// function could be called multiple times (to refresh).
//
if (Private->Descriptor.VersionName != NULL) {
FreePool (Private->Descriptor.VersionName);
Private->Descriptor.VersionName = NULL;
}
//
// Attempt to get the version string from the FmpDeviceLib
//
Status = FmpDeviceGetVersionString (&Private->Descriptor.VersionName);
if (Status == EFI_UNSUPPORTED) {
DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() unsupported in FmpDeviceLib.\n", mImageIdName));
Private->Descriptor.VersionName = AllocateCopyPool (
sizeof (VERSION_STRING_NOT_SUPPORTED),
VERSION_STRING_NOT_SUPPORTED
);
} else if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() not available in FmpDeviceLib.\n", mImageIdName));
Private->Descriptor.VersionName = AllocateCopyPool (
sizeof (VERSION_STRING_NOT_AVAILABLE),
VERSION_STRING_NOT_AVAILABLE
);
}
Private->Descriptor.LowestSupportedImageVersion = GetLowestSupportedVersion (Private);
//
// Get attributes from the FmpDeviceLib
//
FmpDeviceGetAttributes (
&Private->Descriptor.AttributesSupported,
&Private->Descriptor.AttributesSetting
);
//
// Force set the updatable bits in the attributes;
//
Private->Descriptor.AttributesSupported |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
Private->Descriptor.AttributesSetting |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
//
// Force set the authentication bits in the attributes;
//
Private->Descriptor.AttributesSupported |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
Private->Descriptor.AttributesSetting |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
Private->Descriptor.Compatibilities = 0;
//
// Get the size of the firmware image from the FmpDeviceLib
//
Status = FmpDeviceGetSize (&Private->Descriptor.Size);
if (EFI_ERROR (Status)) {
Private->Descriptor.Size = 0;
}
Private->Descriptor.LastAttemptVersion = GetLastAttemptVersionFromVariable (Private);
Private->Descriptor.LastAttemptStatus = GetLastAttemptStatusFromVariable (Private);
//
// Get the dependency from the FmpDependencyDeviceLib.
//
Private->Descriptor.Dependencies = NULL;
//
// Check the attribute IMAGE_ATTRIBUTE_DEPENDENCY
//
if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {
Private->Descriptor.Dependencies = GetFmpDependency (&DependenciesSize);
}
Private->DescriptorPopulated = TRUE;
}
/**
Returns information about the current firmware image(s) of the device.
This function allows a copy of the current firmware image to be created and saved.
The saved copy could later been used, for example, in firmware image recovery or rollback.
@param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
@param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
On input, this is the size of the buffer allocated by the caller.
On output, it is the size of the buffer returned by the firmware
if the buffer was large enough, or the size of the buffer needed
to contain the image(s) information if the buffer was too small.
@param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
@param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
@param[out] DescriptorCount A pointer to the location in which firmware returns the number of
descriptors or firmware images within this device.
@param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
@param[out] PackageVersion A version number that represents all the firmware images in the device.
The format is vendor specific and new version must have a greater value
than the old version. If PackageVersion is not supported, the value is
0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
that package version update is in progress.
@param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
package version name. The buffer is allocated by this function with
AllocatePool(), and it is the caller's responsibility to free it with a call
to FreePool().
@retval EFI_SUCCESS The device was successfully updated with the new image.
@retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
needed to hold the image(s) information is returned in ImageInfoSize.
@retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
@retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
**/
EFI_STATUS
EFIAPI
GetTheImageInfo (
IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
IN OUT UINTN *ImageInfoSize,
IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,
OUT UINT32 *DescriptorVersion,
OUT UINT8 *DescriptorCount,
OUT UINTN *DescriptorSize,
OUT UINT32 *PackageVersion,
OUT CHAR16 **PackageVersionName
)
{
EFI_STATUS Status;
FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
Status = EFI_SUCCESS;
if (This == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - This is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// Retrieve the private context structure
//
Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
//
// Check for valid pointer
//
if (ImageInfoSize == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// Check the buffer size
// NOTE: Check this first so caller can get the necessary memory size it must allocate.
//
if (*ImageInfoSize < (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR))) {
*ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is to small.\n", mImageIdName));
Status = EFI_BUFFER_TOO_SMALL;
goto cleanup;
}
//
// Confirm that buffer isn't null
//
if ( (ImageInfo == NULL) || (DescriptorVersion == NULL) || (DescriptorCount == NULL) || (DescriptorSize == NULL)
|| (PackageVersion == NULL))
{
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - Pointer Parameter is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// Set the size to whatever we need
//
*ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
//
// Make sure the descriptor has already been loaded or refreshed
//
PopulateDescriptor (Private);
//
// Copy the image descriptor
//
CopyMem (ImageInfo, &Private->Descriptor, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR));
*DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
*DescriptorCount = 1;
*DescriptorSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
//
// means unsupported
//
*PackageVersion = 0xFFFFFFFF;
//
// Do not update PackageVersionName since it is not supported in this instance.
//
cleanup:
return Status;
}
/**
Retrieves a copy of the current firmware image of the device.
This function allows a copy of the current firmware image to be created and saved.
The saved copy could later been used, for example, in firmware image recovery or rollback.
@param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
@param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
The number is between 1 and DescriptorCount.
@param[in, out] Image Points to the buffer where the current image is copied to.
@param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
On return, points to the length of the image, in bytes.
@retval EFI_SUCCESS The device was successfully updated with the new image.
@retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
image. The current buffer size needed to hold the image is returned
in ImageSize.
@retval EFI_INVALID_PARAMETER The Image was NULL.
@retval EFI_NOT_FOUND The current image is not copied to the buffer.
@retval EFI_UNSUPPORTED The operation is not supported.
@retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
**/
EFI_STATUS
EFIAPI
GetTheImage (
IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
IN UINT8 ImageIndex,
IN OUT VOID *Image,
IN OUT UINTN *ImageSize
)
{
EFI_STATUS Status;
FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
UINTN Size;
if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
return EFI_UNSUPPORTED;
}
Status = EFI_SUCCESS;
if (This == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - This is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// Retrieve the private context structure
//
Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
//
// Check to make sure index is 1 (only 1 image for this device)
//
if (ImageIndex != 1) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Index Invalid.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
if (ImageSize == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - ImageSize Pointer Parameter is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// Check the buffer size
//
Status = FmpDeviceGetSize (&Size);
if (EFI_ERROR (Status)) {
Size = 0;
}
if (*ImageSize < Size) {
*ImageSize = Size;
DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImage() - ImageSize is to small.\n", mImageIdName));
Status = EFI_BUFFER_TOO_SMALL;
goto cleanup;
}
if (Image == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Pointer Parameter is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
Status = FmpDeviceGetImage (Image, ImageSize);
cleanup:
return Status;
}
/**
Helper function to safely retrieve the FMP header from
within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
@param[in] Image Pointer to the image.
@param[in] ImageSize Size of the image.
@param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
@param[out] PayloadSize An optional pointer to a UINTN that holds the size of the payload
(image size minus headers)
@retval !NULL Valid pointer to the header.
@retval NULL Structure is bad and pointer cannot be found.
**/
VOID *
GetFmpHeader (
IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
IN CONST UINTN ImageSize,
IN CONST UINTN AdditionalHeaderSize,
OUT UINTN *PayloadSize OPTIONAL
)
{
//
// Check to make sure that operation can be safely performed.
//
if ((((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize < (UINTN)Image) || \
(((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize >= (UINTN)Image + ImageSize))
{
//
// Pointer overflow. Invalid image.
//
return NULL;
}
if (PayloadSize != NULL) {
*PayloadSize = ImageSize - (sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);
}
return (VOID *)((UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);
}
/**
Helper function to safely calculate the size of all headers
within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
@param[in] Image Pointer to the image.
@param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
@retval UINT32>0 Valid size of all the headers.
@retval 0 Structure is bad and size cannot be found.
**/
UINT32
GetAllHeaderSize (
IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
IN UINT32 AdditionalHeaderSize
)
{
UINT32 CalculatedSize;
if (Image == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetAllHeaderSize() - Image is NULL.\n", mImageIdName));
return 0;
}
CalculatedSize = sizeof (Image->MonotonicCount) +
AdditionalHeaderSize +
Image->AuthInfo.Hdr.dwLength;
//
// Check to make sure that operation can be safely performed.
//
if ((CalculatedSize < sizeof (Image->MonotonicCount)) ||
(CalculatedSize < AdditionalHeaderSize) ||
(CalculatedSize < Image->AuthInfo.Hdr.dwLength))
{
//
// Integer overflow. Invalid image.
//
return 0;
}
return CalculatedSize;
}
/**
Checks if the firmware image is valid for the device.
This function allows firmware update application to validate the firmware image without
invoking the SetImage() first.
@param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
@param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
The number is between 1 and DescriptorCount.
@param[in] Image Points to the new image.
@param[in] ImageSize Size of the new image in bytes.
@param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
if available, additional information if the image is invalid.
@param[out] LastAttemptStatus A pointer to a UINT32 that holds the last attempt status to report
back to the ESRT table in case of error. If an error does not occur,
this function will set the value to LAST_ATTEMPT_STATUS_SUCCESS.
This function will return error codes that occur within this function
implementation within a driver range of last attempt error codes from
LAST_ATTEMPT_STATUS_DRIVER_MIN_ERROR_CODE_VALUE
to LAST_ATTEMPT_STATUS_DRIVER_MAX_ERROR_CODE_VALUE.
This function might also return error codes that occur within libraries
linked against this module that return last attempt error codes such as:
LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_LIB_MIN_ERROR_CODE_VALUE to
LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_LIB_MAX_ERROR_CODE_VALUE
LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_CHECK_LIB_MIN_ERROR_CODE_VALUE to
LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_CHECK_LIB_MAX_ERROR_CODE_VALUE
@retval EFI_SUCCESS The image was successfully checked.
@retval EFI_ABORTED The operation is aborted.
@retval EFI_INVALID_PARAMETER The Image was NULL.
@retval EFI_UNSUPPORTED The operation is not supported.
@retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
**/
EFI_STATUS
EFIAPI
CheckTheImageInternal (
IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
IN UINT8 ImageIndex,
IN CONST VOID *Image,
IN UINTN ImageSize,
OUT UINT32 *ImageUpdatable,
OUT UINT32 *LastAttemptStatus
)
{
EFI_STATUS Status;
UINT32 LocalLastAttemptStatus;
FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
UINTN RawSize;
VOID *FmpPayloadHeader;
UINTN FmpPayloadSize;
UINT32 Version;
UINT32 FmpHeaderSize;
UINTN AllHeaderSize;
UINT32 Index;
VOID *PublicKeyData;
UINTN PublicKeyDataLength;
UINT8 *PublicKeyDataXdr;
UINT8 *PublicKeyDataXdrEnd;
EFI_FIRMWARE_IMAGE_DEP *Dependencies;
UINT32 DependenciesSize;
Status = EFI_SUCCESS;
LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
RawSize = 0;
FmpPayloadHeader = NULL;
FmpPayloadSize = 0;
Version = 0;
FmpHeaderSize = 0;
AllHeaderSize = 0;
Dependencies = NULL;
DependenciesSize = 0;
if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
return EFI_UNSUPPORTED;
}
if (LastAttemptStatus == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImageInternal() - LastAttemptStatus is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
goto cleanup;
}
//
// A last attempt status error code will always override the success
// value before returning from the function
//
*LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
if (This == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - This is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROTOCOL_ARG_MISSING;
goto cleanup;
}
//
// Retrieve the private context structure
//
Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
//
// Make sure the descriptor has already been loaded or refreshed
//
PopulateDescriptor (Private);
if (ImageUpdatable == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n", mImageIdName));
Status = EFI_INVALID_PARAMETER;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_UPDATABLE;
goto cleanup;
}
//
// Set to valid and then if any tests fail it will update this flag.
//
*ImageUpdatable = IMAGE_UPDATABLE_VALID;
//
// Set to satisfied and then if dependency evaluates to false it will update this flag.
//
Private->DependenciesSatisfied = TRUE;
if (Image == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Pointer Parameter is NULL.\n", mImageIdName));
//
// not sure if this is needed
//
*ImageUpdatable = IMAGE_UPDATABLE_INVALID;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_PROVIDED;
return EFI_INVALID_PARAMETER;
}
PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
if ((PublicKeyDataXdr == NULL) || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Invalid certificate, skipping it.\n", mImageIdName));
Status = EFI_ABORTED;
LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_CERTIFICATE;
} else {
//
// Try each key from PcdFmpDevicePkcs7CertBufferXdr
//
for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {
Index++;
DEBUG (
(DEBUG_INFO,
"FmpDxe(%s): Certificate #%d [%p..%p].\n",
mImageIdName,
Index,
PublicKeyDataXdr,
PublicKeyDataXdrEnd
)
);
if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {
//
// Key data extends beyond end of PCD
//
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate size extends beyond end of PCD, skipping it.\n", mImageIdName));
Status = EFI_ABORTED;
LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH_VALUE;
break;
}
//
// Read key length stored in big-endian format
//
PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
//
// Point to the start of the key data
//
PublicKeyDataXdr += sizeof (UINT32);
if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
//
// Key data extends beyond end of PCD
//
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate extends beyond end of PCD, skipping it.\n", mImageIdName));
Status = EFI_ABORTED;
LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH;
break;
}
PublicKeyData = PublicKeyDataXdr;
Status = AuthenticateFmpImage (
(EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
ImageSize,
PublicKeyData,
PublicKeyDataLength
);
if (!EFI_ERROR (Status)) {
break;
}
PublicKeyDataXdr += PublicKeyDataLength;
PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
}
}
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - Authentication Failed %r.\n", mImageIdName, Status));
if (LocalLastAttemptStatus != LAST_ATTEMPT_STATUS_SUCCESS) {
*LastAttemptStatus = LocalLastAttemptStatus;
} else {
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_AUTH_FAILURE;
}
goto cleanup;
}
//
// Check to make sure index is 1
//
if (ImageIndex != 1) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Index Invalid.\n", mImageIdName));
*ImageUpdatable = IMAGE_UPDATABLE_INVALID_TYPE;
Status = EFI_INVALID_PARAMETER;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_IMAGE_INDEX;
goto cleanup;
}
//
// Get the dependency from Image.
//
Dependencies = GetImageDependency (
(EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
ImageSize,
&DependenciesSize,
LastAttemptStatus
);
if (*LastAttemptStatus != LAST_ATTEMPT_STATUS_SUCCESS) {
Status = EFI_ABORTED;
goto cleanup;
}
//
// Check the FmpPayloadHeader
//
FmpPayloadHeader = GetFmpHeader ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize);
if (FmpPayloadHeader == NULL) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpHeader failed.\n", mImageIdName));
Status = EFI_ABORTED;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER;
goto cleanup;
}
Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", mImageIdName, Status));
*ImageUpdatable = IMAGE_UPDATABLE_INVALID;
Status = EFI_SUCCESS;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_VERSION;
goto cleanup;
}
//
// Check the lowest supported version
//
if (Version < Private->Descriptor.LowestSupportedImageVersion) {
DEBUG (
(DEBUG_ERROR,
"FmpDxe(%s): CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
mImageIdName, Version, Private->Descriptor.LowestSupportedImageVersion)
);
*ImageUpdatable = IMAGE_UPDATABLE_INVALID_OLD;
Status = EFI_SUCCESS;
*LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_VERSION_TOO_LOW;
goto cleanup;
}
//
// Evaluate dependency expression