2 Produces a Firmware Management Protocol that supports updates to a firmware
3 image stored in a firmware device with platform and firmware device specific
4 information provided through PCDs and libraries.
6 Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
7 Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "VariableSupport.h"
17 /// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the
18 /// FILE_GUID must always be overridden in the <Defines> section to provide
19 /// the ESRT GUID value associated with the updatable firmware image. A
20 /// check is made in this module's driver entry point to verify that a
21 /// new FILE_GUID value has been defined.
23 const EFI_GUID mDefaultModuleFileGuid
= {
24 0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }
28 /// TRUE if FmpDeviceLib manages a single firmware storage device.
30 BOOLEAN mFmpSingleInstance
= FALSE
;
33 /// Firmware Management Protocol instance that is initialized in the entry
34 /// point from PCD settings.
36 EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL mFmpProgress
;
39 // Template of the private context structure for the Firmware Management
42 const FIRMWARE_MANAGEMENT_PRIVATE_DATA mFirmwareManagementPrivateDataTemplate
= {
43 FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE
, // Signature
53 FALSE
, // DescriptorPopulated
59 { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
65 0, // AttributesSupported
66 0, // AttributesSetting
68 0, // LowestSupportedImageVersion
69 0, // LastAttemptVersion
70 0, // LastAttemptStatus
75 TRUE
, // RuntimeVersionSupported
76 NULL
, // FmpDeviceLockEvent
77 FALSE
, // FmpDeviceLocked
78 NULL
, // FmpDeviceContext
79 NULL
, // VersionVariableName
80 NULL
, // LsvVariableName
81 NULL
, // LastAttemptStatusVariableName
82 NULL
, // LastAttemptVersionVariableName
83 NULL
// FmpStateVariableName
87 /// GUID that is used to create event used to lock the firmware storage device.
89 EFI_GUID
*mLockGuid
= NULL
;
92 /// Progress() function pointer passed into SetTheImage()
94 EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc
= NULL
;
97 /// Null-terminated Unicode string retrieved from PcdFmpDeviceImageIdName.
99 CHAR16
*mImageIdName
= NULL
;
102 Callback function to report the process of the firmware updating.
104 Wrap the caller's version in this so that progress from the device lib is
105 within the expected range. Convert device lib 0% - 100% to 6% - 98%.
107 FmpDxe 1% - 5% for validation
108 FmpDeviceLib 6% - 98% for flashing/update
109 FmpDxe 99% - 100% finish
111 @param[in] Completion A value between 1 and 100 indicating the current
112 completion progress of the firmware update. Completion
113 progress is reported as from 1 to 100 percent. A value
114 of 0 is used by the driver to indicate that progress
115 reporting is not supported.
117 @retval EFI_SUCCESS The progress was updated.
118 @retval EFI_UNSUPPORTED Updating progress is not supported.
129 Status
= EFI_UNSUPPORTED
;
131 if (mProgressFunc
== NULL
) {
136 // Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.
138 Status
= mProgressFunc (((Completion
* 92) / 100) + 6);
140 if (Status
== EFI_UNSUPPORTED
) {
141 mProgressFunc
= NULL
;
148 Returns a pointer to the ImageTypeId GUID value. An attempt is made to get
149 the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide
150 a GUID value, then gEfiCallerIdGuid is returned.
152 @retval The ImageTypeId GUID
161 EFI_GUID
*FmpDeviceLibGuid
;
163 FmpDeviceLibGuid
= NULL
;
164 Status
= FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid
);
165 if (EFI_ERROR (Status
)) {
166 if (Status
!= EFI_UNSUPPORTED
) {
167 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", mImageIdName
, Status
));
169 return &gEfiCallerIdGuid
;
171 if (FmpDeviceLibGuid
== NULL
) {
172 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n", mImageIdName
));
173 return &gEfiCallerIdGuid
;
175 return FmpDeviceLibGuid
;
179 Returns a pointer to the Null-terminated Unicode ImageIdName string.
181 @retval Null-terminated Unicode ImageIdName string.
185 GetImageTypeNameString (
193 Lowest supported version is a combo of three parts.
194 1. Check if the device lib has a lowest supported version
195 2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)
196 3. Check Fixed at build PCD
198 @param[in] Private Pointer to the private context structure for the
199 Firmware Management Protocol instance.
201 @retval The largest value
205 GetLowestSupportedVersion (
206 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
210 UINT32 DeviceLibLowestSupportedVersion
;
211 UINT32 VariableLowestSupportedVersion
;
215 // Get the LowestSupportedVersion.
218 if (!IsLowestSupportedVersionCheckRequired ()) {
220 // Any Version can pass the 0 LowestSupportedVersion check.
225 ReturnLsv
= PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion
);
228 // Check the FmpDeviceLib
230 DeviceLibLowestSupportedVersion
= DEFAULT_LOWESTSUPPORTEDVERSION
;
231 Status
= FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion
);
232 if (EFI_ERROR (Status
)) {
233 DeviceLibLowestSupportedVersion
= DEFAULT_LOWESTSUPPORTEDVERSION
;
236 if (DeviceLibLowestSupportedVersion
> ReturnLsv
) {
237 ReturnLsv
= DeviceLibLowestSupportedVersion
;
241 // Check the lowest supported version UEFI variable for this device
243 VariableLowestSupportedVersion
= GetLowestSupportedVersionFromVariable (Private
);
244 if (VariableLowestSupportedVersion
> ReturnLsv
) {
245 ReturnLsv
= VariableLowestSupportedVersion
;
249 // Return the largest value
255 Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the private
258 @param[in] Private Pointer to the private context structure for the
259 Firmware Management Protocol instance.
264 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
269 if (Private
->DescriptorPopulated
) {
273 Private
->Descriptor
.ImageIndex
= 1;
274 CopyGuid (&Private
->Descriptor
.ImageTypeId
, GetImageTypeIdGuid());
275 Private
->Descriptor
.ImageId
= Private
->Descriptor
.ImageIndex
;
276 Private
->Descriptor
.ImageIdName
= GetImageTypeNameString();
279 // Get the hardware instance from FmpDeviceLib
281 Status
= FmpDeviceGetHardwareInstance (&Private
->Descriptor
.HardwareInstance
);
282 if (Status
== EFI_UNSUPPORTED
) {
283 Private
->Descriptor
.HardwareInstance
= 0;
287 // Generate UEFI Variable names used to store status information for this
290 GenerateFmpVariableNames (Private
);
293 // Get the version. Some devices don't support getting the firmware version
294 // at runtime. If FmpDeviceLib does not support returning a version, then
295 // it is stored in a UEFI variable.
297 Status
= FmpDeviceGetVersion (&Private
->Descriptor
.Version
);
298 if (Status
== EFI_UNSUPPORTED
) {
299 Private
->RuntimeVersionSupported
= FALSE
;
300 Private
->Descriptor
.Version
= GetVersionFromVariable (Private
);
301 } else if (EFI_ERROR (Status
)) {
303 // Unexpected error. Use default version.
305 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetVersion() from FmpDeviceLib (%s) returned %r\n", mImageIdName
, GetImageTypeNameString(), Status
));
306 Private
->Descriptor
.Version
= DEFAULT_VERSION
;
310 // Free the current version name. Shouldn't really happen but this populate
311 // function could be called multiple times (to refresh).
313 if (Private
->Descriptor
.VersionName
!= NULL
) {
314 FreePool (Private
->Descriptor
.VersionName
);
315 Private
->Descriptor
.VersionName
= NULL
;
319 // Attempt to get the version string from the FmpDeviceLib
321 Status
= FmpDeviceGetVersionString (&Private
->Descriptor
.VersionName
);
322 if (Status
== EFI_UNSUPPORTED
) {
323 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): GetVersionString() unsupported in FmpDeviceLib.\n", mImageIdName
));
324 Private
->Descriptor
.VersionName
= AllocateCopyPool (
325 sizeof (VERSION_STRING_NOT_SUPPORTED
),
326 VERSION_STRING_NOT_SUPPORTED
328 } else if (EFI_ERROR (Status
)) {
329 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): GetVersionString() not available in FmpDeviceLib.\n", mImageIdName
));
330 Private
->Descriptor
.VersionName
= AllocateCopyPool (
331 sizeof (VERSION_STRING_NOT_AVAILABLE
),
332 VERSION_STRING_NOT_AVAILABLE
336 Private
->Descriptor
.LowestSupportedImageVersion
= GetLowestSupportedVersion (Private
);
339 // Get attributes from the FmpDeviceLib
341 FmpDeviceGetAttributes (
342 &Private
->Descriptor
.AttributesSupported
,
343 &Private
->Descriptor
.AttributesSetting
347 // Force set the updatable bits in the attributes;
349 Private
->Descriptor
.AttributesSupported
|= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
;
350 Private
->Descriptor
.AttributesSetting
|= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
;
353 // Force set the authentication bits in the attributes;
355 Private
->Descriptor
.AttributesSupported
|= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
);
356 Private
->Descriptor
.AttributesSetting
|= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
);
358 Private
->Descriptor
.Compatibilities
= 0;
361 // Get the size of the firmware image from the FmpDeviceLib
363 Status
= FmpDeviceGetSize (&Private
->Descriptor
.Size
);
364 if (EFI_ERROR (Status
)) {
365 Private
->Descriptor
.Size
= 0;
368 Private
->Descriptor
.LastAttemptVersion
= GetLastAttemptVersionFromVariable (Private
);
369 Private
->Descriptor
.LastAttemptStatus
= GetLastAttemptStatusFromVariable (Private
);
371 Private
->DescriptorPopulated
= TRUE
;
375 Returns information about the current firmware image(s) of the device.
377 This function allows a copy of the current firmware image to be created and saved.
378 The saved copy could later been used, for example, in firmware image recovery or rollback.
380 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
381 @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
382 On input, this is the size of the buffer allocated by the caller.
383 On output, it is the size of the buffer returned by the firmware
384 if the buffer was large enough, or the size of the buffer needed
385 to contain the image(s) information if the buffer was too small.
386 @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
387 information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
388 @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
389 associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
390 @param[out] DescriptorCount A pointer to the location in which firmware returns the number of
391 descriptors or firmware images within this device.
392 @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
393 of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
394 @param[out] PackageVersion A version number that represents all the firmware images in the device.
395 The format is vendor specific and new version must have a greater value
396 than the old version. If PackageVersion is not supported, the value is
397 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
398 is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
399 that package version update is in progress.
400 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
401 package version name. The buffer is allocated by this function with
402 AllocatePool(), and it is the caller's responsibility to free it with a call
405 @retval EFI_SUCCESS The device was successfully updated with the new image.
406 @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
407 needed to hold the image(s) information is returned in ImageInfoSize.
408 @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
409 @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
415 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
416 IN OUT UINTN
*ImageInfoSize
,
417 IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR
*ImageInfo
,
418 OUT UINT32
*DescriptorVersion
,
419 OUT UINT8
*DescriptorCount
,
420 OUT UINTN
*DescriptorSize
,
421 OUT UINT32
*PackageVersion
,
422 OUT CHAR16
**PackageVersionName
426 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
428 Status
= EFI_SUCCESS
;
431 // Retrieve the private context structure
433 Private
= FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This
);
434 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
437 // Check for valid pointer
439 if (ImageInfoSize
== NULL
) {
440 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is NULL.\n", mImageIdName
));
441 Status
= EFI_INVALID_PARAMETER
;
446 // Check the buffer size
447 // NOTE: Check this first so caller can get the necessary memory size it must allocate.
449 if (*ImageInfoSize
< (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR
))) {
450 *ImageInfoSize
= sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR
);
451 DEBUG ((DEBUG_VERBOSE
, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is to small.\n", mImageIdName
));
452 Status
= EFI_BUFFER_TOO_SMALL
;
457 // Confirm that buffer isn't null
459 if ( (ImageInfo
== NULL
) || (DescriptorVersion
== NULL
) || (DescriptorCount
== NULL
) || (DescriptorSize
== NULL
)
460 || (PackageVersion
== NULL
)) {
461 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetImageInfo() - Pointer Parameter is NULL.\n", mImageIdName
));
462 Status
= EFI_INVALID_PARAMETER
;
467 // Set the size to whatever we need
469 *ImageInfoSize
= sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR
);
472 // Make sure the descriptor has already been loaded or refreshed
474 PopulateDescriptor (Private
);
477 // Copy the image descriptor
479 CopyMem (ImageInfo
, &Private
->Descriptor
, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR
));
481 *DescriptorVersion
= EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION
;
482 *DescriptorCount
= 1;
483 *DescriptorSize
= sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR
);
487 *PackageVersion
= 0xFFFFFFFF;
490 // Do not update PackageVersionName since it is not supported in this instance.
499 Retrieves a copy of the current firmware image of the device.
501 This function allows a copy of the current firmware image to be created and saved.
502 The saved copy could later been used, for example, in firmware image recovery or rollback.
504 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
505 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
506 The number is between 1 and DescriptorCount.
507 @param[in, out] Image Points to the buffer where the current image is copied to.
508 @param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
509 On return, points to the length of the image, in bytes.
511 @retval EFI_SUCCESS The device was successfully updated with the new image.
512 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
513 image. The current buffer size needed to hold the image is returned
515 @retval EFI_INVALID_PARAMETER The Image was NULL.
516 @retval EFI_NOT_FOUND The current image is not copied to the buffer.
517 @retval EFI_UNSUPPORTED The operation is not supported.
518 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
524 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
527 IN OUT UINTN
*ImageSize
531 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
534 Status
= EFI_SUCCESS
;
537 // Retrieve the private context structure
539 Private
= FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This
);
540 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
543 // Check to make sure index is 1 (only 1 image for this device)
545 if (ImageIndex
!= 1) {
546 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetImage() - Image Index Invalid.\n", mImageIdName
));
547 Status
= EFI_INVALID_PARAMETER
;
551 if (ImageSize
== NULL
) {
552 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetImage() - ImageSize Pointer Parameter is NULL.\n", mImageIdName
));
553 Status
= EFI_INVALID_PARAMETER
;
558 // Check the buffer size
560 Status
= FmpDeviceGetSize (&Size
);
561 if (EFI_ERROR (Status
)) {
564 if (*ImageSize
< Size
) {
566 DEBUG ((DEBUG_VERBOSE
, "FmpDxe(%s): GetImage() - ImageSize is to small.\n", mImageIdName
));
567 Status
= EFI_BUFFER_TOO_SMALL
;
572 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): GetImage() - Image Pointer Parameter is NULL.\n", mImageIdName
));
573 Status
= EFI_INVALID_PARAMETER
;
577 Status
= FmpDeviceGetImage (Image
, ImageSize
);
584 Helper function to safely retrieve the FMP header from
585 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
587 @param[in] Image Pointer to the image.
588 @param[in] ImageSize Size of the image.
589 @param[out] PayloadSize
591 @retval !NULL Valid pointer to the header.
592 @retval NULL Structure is bad and pointer cannot be found.
597 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION
*Image
,
598 IN CONST UINTN ImageSize
,
599 OUT UINTN
*PayloadSize
603 // Check to make sure that operation can be safely performed.
605 if (((UINTN
)Image
+ sizeof (Image
->MonotonicCount
) + Image
->AuthInfo
.Hdr
.dwLength
) < (UINTN
)Image
|| \
606 ((UINTN
)Image
+ sizeof (Image
->MonotonicCount
) + Image
->AuthInfo
.Hdr
.dwLength
) >= (UINTN
)Image
+ ImageSize
) {
608 // Pointer overflow. Invalid image.
613 *PayloadSize
= ImageSize
- (sizeof (Image
->MonotonicCount
) + Image
->AuthInfo
.Hdr
.dwLength
);
614 return (VOID
*)((UINT8
*)Image
+ sizeof (Image
->MonotonicCount
) + Image
->AuthInfo
.Hdr
.dwLength
);
618 Helper function to safely calculate the size of all headers
619 within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
621 @param[in] Image Pointer to the image.
622 @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
624 @retval UINT32>0 Valid size of all the headers.
625 @retval 0 Structure is bad and size cannot be found.
630 IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION
*Image
,
631 IN UINT32 AdditionalHeaderSize
634 UINT32 CalculatedSize
;
636 CalculatedSize
= sizeof (Image
->MonotonicCount
) +
637 AdditionalHeaderSize
+
638 Image
->AuthInfo
.Hdr
.dwLength
;
641 // Check to make sure that operation can be safely performed.
643 if (CalculatedSize
< sizeof (Image
->MonotonicCount
) ||
644 CalculatedSize
< AdditionalHeaderSize
||
645 CalculatedSize
< Image
->AuthInfo
.Hdr
.dwLength
) {
647 // Integer overflow. Invalid image.
652 return CalculatedSize
;
656 Checks if the firmware image is valid for the device.
658 This function allows firmware update application to validate the firmware image without
659 invoking the SetImage() first.
661 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
662 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
663 The number is between 1 and DescriptorCount.
664 @param[in] Image Points to the new image.
665 @param[in] ImageSize Size of the new image in bytes.
666 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
667 if available, additional information if the image is invalid.
669 @retval EFI_SUCCESS The image was successfully checked.
670 @retval EFI_ABORTED The operation is aborted.
671 @retval EFI_INVALID_PARAMETER The Image was NULL.
672 @retval EFI_UNSUPPORTED The operation is not supported.
673 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
679 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
681 IN CONST VOID
*Image
,
683 OUT UINT32
*ImageUpdatable
687 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
689 VOID
*FmpPayloadHeader
;
690 UINTN FmpPayloadSize
;
692 UINT32 FmpHeaderSize
;
696 UINTN PublicKeyDataLength
;
697 UINT8
*PublicKeyDataXdr
;
698 UINT8
*PublicKeyDataXdrEnd
;
700 Status
= EFI_SUCCESS
;
702 FmpPayloadHeader
= NULL
;
709 // Retrieve the private context structure
711 Private
= FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This
);
712 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
715 // Make sure the descriptor has already been loaded or refreshed
717 PopulateDescriptor (Private
);
719 if (ImageUpdatable
== NULL
) {
720 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n", mImageIdName
));
721 Status
= EFI_INVALID_PARAMETER
;
726 //Set to valid and then if any tests fail it will update this flag.
728 *ImageUpdatable
= IMAGE_UPDATABLE_VALID
;
731 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckImage() - Image Pointer Parameter is NULL.\n", mImageIdName
));
733 // not sure if this is needed
735 *ImageUpdatable
= IMAGE_UPDATABLE_INVALID
;
736 return EFI_INVALID_PARAMETER
;
739 PublicKeyDataXdr
= PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr
);
740 PublicKeyDataXdrEnd
= PublicKeyDataXdr
+ PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr
);
742 if (PublicKeyDataXdr
== NULL
|| (PublicKeyDataXdr
== PublicKeyDataXdrEnd
)) {
743 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Invalid certificate, skipping it.\n", mImageIdName
));
744 Status
= EFI_ABORTED
;
747 // Try each key from PcdFmpDevicePkcs7CertBufferXdr
749 for (Index
= 1; PublicKeyDataXdr
< PublicKeyDataXdrEnd
; Index
++) {
753 "FmpDxe(%s): Certificate #%d [%p..%p].\n",
761 if ((PublicKeyDataXdr
+ sizeof (UINT32
)) > PublicKeyDataXdrEnd
) {
763 // Key data extends beyond end of PCD
765 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Certificate size extends beyond end of PCD, skipping it.\n", mImageIdName
));
766 Status
= EFI_ABORTED
;
770 // Read key length stored in big-endian format
772 PublicKeyDataLength
= SwapBytes32 (*(UINT32
*)(PublicKeyDataXdr
));
774 // Point to the start of the key data
776 PublicKeyDataXdr
+= sizeof (UINT32
);
777 if (PublicKeyDataXdr
+ PublicKeyDataLength
> PublicKeyDataXdrEnd
) {
779 // Key data extends beyond end of PCD
781 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Certificate extends beyond end of PCD, skipping it.\n", mImageIdName
));
782 Status
= EFI_ABORTED
;
785 PublicKeyData
= PublicKeyDataXdr
;
786 Status
= AuthenticateFmpImage (
787 (EFI_FIRMWARE_IMAGE_AUTHENTICATION
*)Image
,
792 if (!EFI_ERROR (Status
)) {
795 PublicKeyDataXdr
+= PublicKeyDataLength
;
796 PublicKeyDataXdr
= (UINT8
*)ALIGN_POINTER (PublicKeyDataXdr
, sizeof (UINT32
));
800 if (EFI_ERROR (Status
)) {
801 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckTheImage() - Authentication Failed %r.\n", mImageIdName
, Status
));
806 // Check to make sure index is 1
808 if (ImageIndex
!= 1) {
809 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckImage() - Image Index Invalid.\n", mImageIdName
));
810 *ImageUpdatable
= IMAGE_UPDATABLE_INVALID_TYPE
;
811 Status
= EFI_SUCCESS
;
817 // Check the FmpPayloadHeader
819 FmpPayloadHeader
= GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION
*)Image
, ImageSize
, &FmpPayloadSize
);
820 if (FmpPayloadHeader
== NULL
) {
821 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckTheImage() - GetFmpHeader failed.\n", mImageIdName
));
822 Status
= EFI_ABORTED
;
825 Status
= GetFmpPayloadHeaderVersion (FmpPayloadHeader
, FmpPayloadSize
, &Version
);
826 if (EFI_ERROR (Status
)) {
827 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", mImageIdName
, Status
));
828 *ImageUpdatable
= IMAGE_UPDATABLE_INVALID
;
829 Status
= EFI_SUCCESS
;
834 // Check the lowest supported version
836 if (Version
< Private
->Descriptor
.LowestSupportedImageVersion
) {
839 "FmpDxe(%s): CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
840 mImageIdName
, Version
, Private
->Descriptor
.LowestSupportedImageVersion
)
842 *ImageUpdatable
= IMAGE_UPDATABLE_INVALID_OLD
;
843 Status
= EFI_SUCCESS
;
848 // Get the FmpHeaderSize so we can determine the real payload size
850 Status
= GetFmpPayloadHeaderSize (FmpPayloadHeader
, FmpPayloadSize
, &FmpHeaderSize
);
851 if (EFI_ERROR (Status
)) {
852 DEBUG ((DEBUG_ERROR
, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status
));
853 *ImageUpdatable
= IMAGE_UPDATABLE_INVALID
;
854 Status
= EFI_SUCCESS
;
859 // Call FmpDevice Lib Check Image on the
860 // Raw payload. So all headers need stripped off
862 AllHeaderSize
= GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION
*)Image
, FmpHeaderSize
);
863 if (AllHeaderSize
== 0) {
864 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckTheImage() - GetAllHeaderSize failed.\n", mImageIdName
));
865 Status
= EFI_ABORTED
;
868 RawSize
= ImageSize
- AllHeaderSize
;
871 // FmpDeviceLib CheckImage function to do any specific checks
873 Status
= FmpDeviceCheckImage ((((UINT8
*)Image
) + AllHeaderSize
), RawSize
, ImageUpdatable
);
874 if (EFI_ERROR (Status
)) {
875 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", mImageIdName
, Status
));
883 Updates the firmware image of the device.
885 This function updates the hardware with the new firmware image.
886 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
887 If the firmware image is updatable, the function should perform the following minimal validations
888 before proceeding to do the firmware image update.
889 - Validate the image authentication if image has attribute
890 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
891 EFI_SECURITY_VIOLATION if the validation fails.
892 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
893 the image is unsupported. The function can optionally provide more detailed information on
894 why the image is not a supported image.
895 - Validate the data from VendorCode if not null. Image validation must be performed before
896 VendorCode data validation. VendorCode data is ignored or considered invalid if image
897 validation failed. The function returns EFI_ABORTED if the data is invalid.
899 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
900 the caller did not specify the policy or use the default policy. As an example, vendor can implement
901 a policy to allow an option to force a firmware image update when the abort reason is due to the new
902 firmware image version is older than the current firmware image version or bad image checksum.
903 Sensitive operations such as those wiping the entire firmware image and render the device to be
904 non-functional should be encoded in the image itself rather than passed with the VendorCode.
905 AbortReason enables vendor to have the option to provide a more detailed description of the abort
906 reason to the caller.
908 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
909 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
910 The number is between 1 and DescriptorCount.
911 @param[in] Image Points to the new image.
912 @param[in] ImageSize Size of the new image in bytes.
913 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
914 Null indicates the caller did not specify the policy or use the default policy.
915 @param[in] Progress A function used by the driver to report the progress of the firmware update.
916 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
917 details for the aborted operation. The buffer is allocated by this function
918 with AllocatePool(), and it is the caller's responsibility to free it with a
921 @retval EFI_SUCCESS The device was successfully updated with the new image.
922 @retval EFI_ABORTED The operation is aborted.
923 @retval EFI_INVALID_PARAMETER The Image was NULL.
924 @retval EFI_UNSUPPORTED The operation is not supported.
925 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
931 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
933 IN CONST VOID
*Image
,
935 IN CONST VOID
*VendorCode
,
936 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
,
937 OUT CHAR16
**AbortReason
941 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
943 BOOLEAN BooleanValue
;
944 UINT32 FmpHeaderSize
;
946 UINTN FmpPayloadSize
;
947 UINT32 AllHeaderSize
;
948 UINT32 IncommingFwVersion
;
949 UINT32 LastAttemptStatus
;
951 UINT32 LowestSupportedVersion
;
953 Status
= EFI_SUCCESS
;
955 BooleanValue
= FALSE
;
960 IncommingFwVersion
= 0;
961 LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
964 // Retrieve the private context structure
966 Private
= FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This
);
967 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
970 // Make sure the descriptor has already been loaded or refreshed
972 PopulateDescriptor (Private
);
975 // Set to 0 to clear any previous results.
977 SetLastAttemptVersionInVariable (Private
, IncommingFwVersion
);
980 // if we have locked the device, then skip the set operation.
981 // it should be blocked by hardware too but we can catch here even faster
983 if (Private
->FmpDeviceLocked
) {
984 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - Device is already locked. Can't update.\n", mImageIdName
));
985 Status
= EFI_UNSUPPORTED
;
990 // Call check image to verify the image
992 Status
= CheckTheImage (This
, ImageIndex
, Image
, ImageSize
, &Updateable
);
993 if (EFI_ERROR (Status
)) {
994 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - Check The Image failed with %r.\n", mImageIdName
, Status
));
995 if (Status
== EFI_SECURITY_VIOLATION
) {
996 LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR
;
1002 // No functional error in CheckTheImage. Attempt to get the Version to
1003 // support better error reporting.
1005 FmpHeader
= GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION
*)Image
, ImageSize
, &FmpPayloadSize
);
1006 if (FmpHeader
== NULL
) {
1007 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - GetFmpHeader failed.\n", mImageIdName
));
1008 Status
= EFI_ABORTED
;
1011 Status
= GetFmpPayloadHeaderVersion (FmpHeader
, FmpPayloadSize
, &IncommingFwVersion
);
1012 if (!EFI_ERROR (Status
)) {
1014 // Set to actual value
1016 SetLastAttemptVersionInVariable (Private
, IncommingFwVersion
);
1020 if (Updateable
!= IMAGE_UPDATABLE_VALID
) {
1023 "FmpDxe(%s): SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
1024 mImageIdName
, Updateable
)
1026 Status
= EFI_ABORTED
;
1030 if (Progress
== NULL
) {
1031 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - Invalid progress callback\n", mImageIdName
));
1032 Status
= EFI_INVALID_PARAMETER
;
1036 mProgressFunc
= Progress
;
1039 // Checking the image is at least 1%
1041 Status
= Progress (1);
1042 if (EFI_ERROR (Status
)) {
1043 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - Progress Callback failed with Status %r.\n", mImageIdName
, Status
));
1047 //Check System Power
1049 Status
= CheckSystemPower (&BooleanValue
);
1050 if (EFI_ERROR (Status
)) {
1051 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - CheckSystemPower - API call failed %r.\n", mImageIdName
, Status
));
1054 if (!BooleanValue
) {
1055 Status
= EFI_ABORTED
;
1058 "FmpDxe(%s): SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n", mImageIdName
)
1060 LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT
;
1067 //Check System Thermal
1069 Status
= CheckSystemThermal (&BooleanValue
);
1070 if (EFI_ERROR (Status
)) {
1071 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - CheckSystemThermal - API call failed %r.\n", mImageIdName
, Status
));
1074 if (!BooleanValue
) {
1075 Status
= EFI_ABORTED
;
1078 "FmpDxe(%s): SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n", mImageIdName
)
1086 //Check System Environment
1088 Status
= CheckSystemEnvironment (&BooleanValue
);
1089 if (EFI_ERROR (Status
)) {
1090 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", mImageIdName
, Status
));
1093 if (!BooleanValue
) {
1094 Status
= EFI_ABORTED
;
1097 "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n", mImageIdName
)
1105 // Save LastAttemptStatus as error so that if SetImage never returns the error
1106 // state is recorded.
1108 SetLastAttemptStatusInVariable (Private
, LastAttemptStatus
);
1111 // Strip off all the headers so the device can process its firmware
1113 Status
= GetFmpPayloadHeaderSize (FmpHeader
, FmpPayloadSize
, &FmpHeaderSize
);
1114 if (EFI_ERROR (Status
)) {
1115 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", mImageIdName
, Status
));
1119 AllHeaderSize
= GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION
*)Image
, FmpHeaderSize
);
1120 if (AllHeaderSize
== 0) {
1121 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() - GetAllHeaderSize failed.\n", mImageIdName
));
1122 Status
= EFI_ABORTED
;
1127 // Indicate that control is handed off to FmpDeviceLib
1132 //Copy the requested image to the firmware using the FmpDeviceLib
1134 Status
= FmpDeviceSetImage (
1135 (((UINT8
*)Image
) + AllHeaderSize
),
1136 ImageSize
- AllHeaderSize
,
1142 if (EFI_ERROR (Status
)) {
1143 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", mImageIdName
, Status
));
1149 // Finished the update without error
1150 // Indicate that control has been returned from FmpDeviceLib
1155 // Update the version stored in variable
1157 if (!Private
->RuntimeVersionSupported
) {
1158 Version
= DEFAULT_VERSION
;
1159 GetFmpPayloadHeaderVersion (FmpHeader
, FmpPayloadSize
, &Version
);
1160 SetVersionInVariable (Private
, Version
);
1164 // Update lowest supported variable
1166 LowestSupportedVersion
= DEFAULT_LOWESTSUPPORTEDVERSION
;
1167 GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader
, FmpPayloadSize
, &LowestSupportedVersion
);
1168 SetLowestSupportedVersionInVariable (Private
, LowestSupportedVersion
);
1170 LastAttemptStatus
= LAST_ATTEMPT_STATUS_SUCCESS
;
1173 mProgressFunc
= NULL
;
1174 SetLastAttemptStatusInVariable (Private
, LastAttemptStatus
);
1176 if (Progress
!= NULL
) {
1178 // Set progress to 100 after everything is done including recording Status.
1184 // Need repopulate after SetImage is called to
1185 // update LastAttemptVersion and LastAttemptStatus.
1187 Private
->DescriptorPopulated
= FALSE
;
1193 Returns information about the firmware package.
1195 This function returns package information.
1197 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1198 @param[out] PackageVersion A version number that represents all the firmware images in the device.
1199 The format is vendor specific and new version must have a greater value
1200 than the old version. If PackageVersion is not supported, the value is
1201 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
1202 comparison is to be performed using PackageVersionName. A value of
1203 0xFFFFFFFD indicates that package version update is in progress.
1204 @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
1205 the package version name. The buffer is allocated by this function with
1206 AllocatePool(), and it is the caller's responsibility to free it with a
1208 @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
1209 package version name. A value of 0 indicates the device does not support
1210 update of package version name. Length is the number of Unicode characters,
1211 including the terminating null character.
1212 @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
1213 Definitions' for possible returned values of this parameter. A value of 1
1214 indicates the attribute is supported and the current setting value is
1215 indicated in AttributesSetting. A value of 0 indicates the attribute is not
1216 supported and the current setting value in AttributesSetting is meaningless.
1217 @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
1218 values of this parameter
1220 @retval EFI_SUCCESS The package information was successfully returned.
1221 @retval EFI_UNSUPPORTED The operation is not supported.
1227 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
1228 OUT UINT32
*PackageVersion
,
1229 OUT CHAR16
**PackageVersionName
,
1230 OUT UINT32
*PackageVersionNameMaxLen
,
1231 OUT UINT64
*AttributesSupported
,
1232 OUT UINT64
*AttributesSetting
1235 return EFI_UNSUPPORTED
;
1239 Updates information about the firmware package.
1241 This function updates package information.
1242 This function returns EFI_UNSUPPORTED if the package information is not updatable.
1243 VendorCode enables vendor to implement vendor-specific package information update policy.
1244 Null if the caller did not specify this policy or use the default policy.
1246 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
1247 @param[in] Image Points to the authentication image.
1248 Null if authentication is not required.
1249 @param[in] ImageSize Size of the authentication image in bytes.
1250 0 if authentication is not required.
1251 @param[in] VendorCode This enables vendor to implement vendor-specific firmware
1252 image update policy.
1253 Null indicates the caller did not specify this policy or use
1255 @param[in] PackageVersion The new package version.
1256 @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
1257 the package version name.
1258 The string length is equal to or less than the value returned in
1259 PackageVersionNameMaxLen.
1261 @retval EFI_SUCCESS The device was successfully updated with the new package
1263 @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
1264 returned in PackageVersionNameMaxLen.
1265 @retval EFI_UNSUPPORTED The operation is not supported.
1266 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
1272 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
1273 IN CONST VOID
*Image
,
1275 IN CONST VOID
*VendorCode
,
1276 IN UINT32 PackageVersion
,
1277 IN CONST CHAR16
*PackageVersionName
1280 return EFI_UNSUPPORTED
;
1284 Event notification function that is invoked when the event GUID specified by
1285 PcdFmpDeviceLockEventGuid is signaled.
1287 @param[in] Event Event whose notification function is being invoked.
1288 @param[in] Context The pointer to the notification function's context,
1289 which is implementation-dependent.
1293 FmpDxeLockEventNotify (
1299 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
1301 Private
= (FIRMWARE_MANAGEMENT_PRIVATE_DATA
*)Context
;
1303 if (!Private
->FmpDeviceLocked
) {
1305 // Lock the firmware device
1307 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
1308 Status
= FmpDeviceLock();
1309 if (EFI_ERROR (Status
)) {
1310 if (Status
!= EFI_UNSUPPORTED
) {
1311 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName
, Status
));
1313 DEBUG ((DEBUG_WARN
, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName
, Status
));
1316 Private
->FmpDeviceLocked
= TRUE
;
1321 Function to install FMP instance.
1323 @param[in] Handle The device handle to install a FMP instance on.
1325 @retval EFI_SUCCESS FMP Installed
1326 @retval EFI_INVALID_PARAMETER Handle was invalid
1327 @retval other Error installing FMP
1332 InstallFmpInstance (
1333 IN EFI_HANDLE Handle
1337 EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*Fmp
;
1338 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
1341 // Only allow a single FMP Protocol instance to be installed
1343 Status
= gBS
->OpenProtocol (
1345 &gEfiFirmwareManagementProtocolGuid
,
1349 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1351 if (!EFI_ERROR (Status
)) {
1352 return EFI_ALREADY_STARTED
;
1356 // Allocate FMP Protocol instance
1358 Private
= AllocateCopyPool (
1359 sizeof (mFirmwareManagementPrivateDataTemplate
),
1360 &mFirmwareManagementPrivateDataTemplate
1362 if (Private
== NULL
) {
1363 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Failed to allocate memory for private structure.\n", mImageIdName
));
1364 Status
= EFI_OUT_OF_RESOURCES
;
1369 // Initialize private context data structure
1371 Private
->Handle
= Handle
;
1372 Private
->FmpDeviceContext
= NULL
;
1373 Status
= FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
1374 if (Status
== EFI_UNSUPPORTED
) {
1375 Private
->FmpDeviceContext
= NULL
;
1376 } else if (EFI_ERROR (Status
)) {
1381 // Make sure the descriptor has already been loaded or refreshed
1383 PopulateDescriptor (Private
);
1385 if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
1387 // Register all UEFI Variables used by this module to be locked.
1389 Status
= LockAllFmpVariables (Private
);
1390 if (EFI_ERROR (Status
)) {
1391 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Failed to register variables to lock. Status = %r.\n", mImageIdName
, Status
));
1393 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): All variables registered to lock\n", mImageIdName
));
1397 // Create and register notify function to lock the FMP device.
1399 Status
= gBS
->CreateEventEx (
1402 FmpDxeLockEventNotify
,
1405 &Private
->FmpDeviceLockEvent
1407 if (EFI_ERROR (Status
)) {
1408 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Failed to register notification. Status = %r\n", mImageIdName
, Status
));
1410 ASSERT_EFI_ERROR (Status
);
1412 DEBUG ((DEBUG_VERBOSE
, "FmpDxe(%s): Not registering notification to call FmpDeviceLock() because mfg mode\n", mImageIdName
));
1416 // Install FMP Protocol and FMP Progress Protocol
1418 Status
= gBS
->InstallMultipleProtocolInterfaces (
1420 &gEfiFirmwareManagementProtocolGuid
, &Private
->Fmp
,
1421 &gEdkiiFirmwareManagementProgressProtocolGuid
, &mFmpProgress
,
1424 if (EFI_ERROR (Status
)) {
1425 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Protocol install error. Status = %r.\n", mImageIdName
, Status
));
1431 if (EFI_ERROR (Status
)) {
1432 if (Private
!= NULL
) {
1433 if (Private
->FmpDeviceLockEvent
!= NULL
) {
1434 gBS
->CloseEvent (Private
->FmpDeviceLockEvent
);
1436 if (Private
->Descriptor
.VersionName
!= NULL
) {
1437 FreePool (Private
->Descriptor
.VersionName
);
1439 if (Private
->FmpDeviceContext
!= NULL
) {
1440 FmpDeviceSetContext (NULL
, &Private
->FmpDeviceContext
);
1442 if (Private
->VersionVariableName
!= NULL
) {
1443 FreePool (Private
->VersionVariableName
);
1445 if (Private
->LsvVariableName
!= NULL
) {
1446 FreePool (Private
->LsvVariableName
);
1448 if (Private
->LastAttemptStatusVariableName
!= NULL
) {
1449 FreePool (Private
->LastAttemptStatusVariableName
);
1451 if (Private
->LastAttemptVersionVariableName
!= NULL
) {
1452 FreePool (Private
->LastAttemptVersionVariableName
);
1454 if (Private
->FmpStateVariableName
!= NULL
) {
1455 FreePool (Private
->FmpStateVariableName
);
1465 Function to uninstall FMP instance.
1467 @param[in] Handle The device handle to install a FMP instance on.
1469 @retval EFI_SUCCESS FMP Installed
1470 @retval EFI_INVALID_PARAMETER Handle was invalid
1471 @retval other Error installing FMP
1476 UninstallFmpInstance (
1477 IN EFI_HANDLE Handle
1481 EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*Fmp
;
1482 FIRMWARE_MANAGEMENT_PRIVATE_DATA
*Private
;
1484 Status
= gBS
->OpenProtocol (
1486 &gEfiFirmwareManagementProtocolGuid
,
1490 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1492 if (EFI_ERROR (Status
)) {
1493 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Protocol open error. Status = %r.\n", mImageIdName
, Status
));
1497 Private
= FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (Fmp
);
1498 FmpDeviceSetContext (Private
->Handle
, &Private
->FmpDeviceContext
);
1500 if (Private
->FmpDeviceLockEvent
!= NULL
) {
1501 gBS
->CloseEvent (Private
->FmpDeviceLockEvent
);
1504 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1506 &gEfiFirmwareManagementProtocolGuid
, &Private
->Fmp
,
1507 &gEdkiiFirmwareManagementProgressProtocolGuid
, &mFmpProgress
,
1510 if (EFI_ERROR (Status
)) {
1511 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): Protocol uninstall error. Status = %r.\n", mImageIdName
, Status
));
1515 if (Private
->Descriptor
.VersionName
!= NULL
) {
1516 FreePool (Private
->Descriptor
.VersionName
);
1518 if (Private
->FmpDeviceContext
!= NULL
) {
1519 FmpDeviceSetContext (NULL
, &Private
->FmpDeviceContext
);
1521 if (Private
->VersionVariableName
!= NULL
) {
1522 FreePool (Private
->VersionVariableName
);
1524 if (Private
->LsvVariableName
!= NULL
) {
1525 FreePool (Private
->LsvVariableName
);
1527 if (Private
->LastAttemptStatusVariableName
!= NULL
) {
1528 FreePool (Private
->LastAttemptStatusVariableName
);
1530 if (Private
->LastAttemptVersionVariableName
!= NULL
) {
1531 FreePool (Private
->LastAttemptVersionVariableName
);
1533 if (Private
->FmpStateVariableName
!= NULL
) {
1534 FreePool (Private
->FmpStateVariableName
);
1542 Unloads the application and its installed protocol.
1544 @param ImageHandle Handle that identifies the image to be unloaded.
1545 @param SystemTable The system table.
1547 @retval EFI_SUCCESS The image has been unloaded.
1552 FmpDxeLibDestructor (
1553 IN EFI_HANDLE ImageHandle
,
1554 IN EFI_SYSTEM_TABLE
*SystemTable
1557 if (mFmpSingleInstance
) {
1558 return UninstallFmpInstance (ImageHandle
);
1564 Main entry for this driver/library.
1566 @param[in] ImageHandle Image handle this driver.
1567 @param[in] SystemTable Pointer to SystemTable.
1573 IN EFI_HANDLE ImageHandle
,
1574 IN EFI_SYSTEM_TABLE
*SystemTable
1580 // Verify that a new FILE_GUID value has been provided in the <Defines>
1581 // section of this module. The FILE_GUID is the ESRT GUID that must be
1582 // unique for each updatable firmware image.
1584 if (CompareGuid (&mDefaultModuleFileGuid
, &gEfiCallerIdGuid
)) {
1585 DEBUG ((DEBUG_ERROR
, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
1587 return EFI_UNSUPPORTED
;
1591 // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
1593 mImageIdName
= (CHAR16
*) PcdGetPtr (PcdFmpDeviceImageIdName
);
1594 if (PcdGetSize (PcdFmpDeviceImageIdName
) <= 2 || mImageIdName
[0] == 0) {
1596 // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
1598 DEBUG ((DEBUG_ERROR
, "FmpDxe: PcdFmpDeviceImageIdName is an empty string.\n"));
1600 return EFI_UNSUPPORTED
;
1604 // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
1609 // Fill in FMP Progress Protocol fields for Version 1
1611 mFmpProgress
.Version
= 1;
1612 mFmpProgress
.ProgressBarForegroundColor
.Raw
= PcdGet32 (PcdFmpDeviceProgressColor
);
1613 mFmpProgress
.WatchdogSeconds
= PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds
);
1615 // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
1616 // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
1617 // gEfiEndOfDxeEventGroupGuid is used.
1619 mLockGuid
= &gEfiEndOfDxeEventGroupGuid
;
1620 if (PcdGetSize (PcdFmpDeviceLockEventGuid
) == sizeof (EFI_GUID
)) {
1621 mLockGuid
= (EFI_GUID
*)PcdGetPtr (PcdFmpDeviceLockEventGuid
);
1623 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): Lock GUID: %g\n", mImageIdName
, mLockGuid
));
1626 // Register with library the install function so if the library uses
1627 // UEFI driver model/driver binding protocol it can install FMP on its device handle
1628 // If library is simple lib that does not use driver binding then it should return
1629 // unsupported and this will install the FMP instance on the ImageHandle
1631 Status
= RegisterFmpInstaller (InstallFmpInstance
);
1632 if (Status
== EFI_UNSUPPORTED
) {
1633 mFmpSingleInstance
= TRUE
;
1634 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n", mImageIdName
));
1635 Status
= RegisterFmpUninstaller (UninstallFmpInstance
);
1636 if (Status
== EFI_UNSUPPORTED
) {
1637 Status
= InstallFmpInstance (ImageHandle
);
1639 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName
));
1640 Status
= EFI_UNSUPPORTED
;
1642 } else if (EFI_ERROR (Status
)) {
1643 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLib registration returned %r. No FMP installed.\n", mImageIdName
, Status
));
1647 "FmpDxe(%s): FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n",
1650 Status
= RegisterFmpUninstaller (UninstallFmpInstance
);
1651 if (EFI_ERROR (Status
)) {
1652 DEBUG ((DEBUG_ERROR
, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName
));