2 SetImage instance to update system firmware.
4 Caution: This module requires additional review when modified.
5 This module will have external input - capsule image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 FmpSetImage() will receive untrusted input and do basic validation.
11 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
12 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include "SystemFirmwareDxe.h"
19 // SystemFmp driver private data
21 SYSTEM_FMP_PRIVATE_DATA
*mSystemFmpPrivate
= NULL
;
23 EFI_GUID mCurrentImageTypeId
;
25 BOOLEAN mNvRamUpdated
= FALSE
;
28 Parse Config data file to get the updated data array.
30 @param[in] DataBuffer Config raw file buffer.
31 @param[in] BufferSize Size of raw buffer.
32 @param[in, out] ConfigHeader Pointer to the config header.
33 @param[in, out] UpdateArray Pointer to the config of update data.
35 @retval EFI_NOT_FOUND No config data is found.
36 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
37 @retval EFI_SUCCESS Parse the config file successfully.
44 IN OUT CONFIG_HEADER
*ConfigHeader
,
45 IN OUT UPDATE_CONFIG_DATA
**UpdateArray
49 Update System Firmware image component.
51 @param[in] SystemFirmwareImage Points to the System Firmware image.
52 @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.
53 @param[in] ConfigData Points to the component configuration structure.
54 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
55 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
56 @param[in] Progress A function used by the driver to report the progress of the firmware update.
57 @param[in] StartPercentage The start completion percentage value that may be used to report progress during the flash write operation.
58 @param[in] EndPercentage The end completion percentage value that may be used to report progress during the flash write operation.
60 @retval EFI_SUCCESS The System Firmware image is updated.
61 @retval EFI_WRITE_PROTECTED The flash device is read only.
65 IN VOID
*SystemFirmwareImage
,
66 IN UINTN SystemFirmwareImageSize
,
67 IN UPDATE_CONFIG_DATA
*ConfigData
,
68 OUT UINT32
*LastAttemptVersion
,
69 OUT UINT32
*LastAttemptStatus
,
70 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
,
71 IN UINTN StartPercentage
,
72 IN UINTN EndPercentage
77 DEBUG((DEBUG_INFO
, "PlatformUpdate:"));
78 DEBUG((DEBUG_INFO
, " BaseAddress - 0x%lx,", ConfigData
->BaseAddress
));
79 DEBUG((DEBUG_INFO
, " ImageOffset - 0x%x,", ConfigData
->ImageOffset
));
80 DEBUG((DEBUG_INFO
, " Legnth - 0x%x\n", ConfigData
->Length
));
81 if (Progress
!= NULL
) {
82 Progress (StartPercentage
);
84 Status
= PerformFlashWriteWithProgress (
85 ConfigData
->FirmwareType
,
86 ConfigData
->BaseAddress
,
87 ConfigData
->AddressType
,
88 (VOID
*)((UINTN
)SystemFirmwareImage
+ (UINTN
)ConfigData
->ImageOffset
),
94 if (Progress
!= NULL
) {
95 Progress (EndPercentage
);
97 if (!EFI_ERROR(Status
)) {
98 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_SUCCESS
;
99 if (ConfigData
->FirmwareType
== PlatformFirmwareTypeNvRam
) {
100 mNvRamUpdated
= TRUE
;
103 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
109 Update System Firmware image.
111 @param[in] SystemFirmwareImage Points to the System Firmware image.
112 @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.
113 @param[in] ConfigImage Points to the config file image.
114 @param[in] ConfigImageSize The length of the config file image in bytes.
115 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
116 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
117 @param[in] Progress A function used by the driver to report the progress of the firmware update.
119 @retval EFI_SUCCESS The System Firmware image is updated.
120 @retval EFI_WRITE_PROTECTED The flash device is read only.
124 IN VOID
*SystemFirmwareImage
,
125 IN UINTN SystemFirmwareImageSize
,
126 IN VOID
*ConfigImage
,
127 IN UINTN ConfigImageSize
,
128 OUT UINT32
*LastAttemptVersion
,
129 OUT UINT32
*LastAttemptStatus
,
130 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
134 UPDATE_CONFIG_DATA
*ConfigData
;
135 UPDATE_CONFIG_DATA
*UpdateConfigData
;
136 CONFIG_HEADER ConfigHeader
;
140 UINTN StartPercentage
;
143 if (ConfigImage
== NULL
) {
144 DEBUG((DEBUG_INFO
, "PlatformUpdate (NoConfig):"));
145 DEBUG((DEBUG_INFO
, " BaseAddress - 0x%x,", 0));
146 DEBUG((DEBUG_INFO
, " Length - 0x%x\n", SystemFirmwareImageSize
));
147 // ASSUME the whole System Firmware include NVRAM region.
150 if (Progress
!= NULL
) {
151 Progress (StartPercentage
);
153 Status
= PerformFlashWriteWithProgress (
154 PlatformFirmwareTypeNvRam
,
156 FlashAddressTypeRelativeAddress
,
158 SystemFirmwareImageSize
,
163 if (Progress
!= NULL
) {
164 Progress (EndPercentage
);
166 if (!EFI_ERROR(Status
)) {
167 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_SUCCESS
;
168 mNvRamUpdated
= TRUE
;
170 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
175 DEBUG((DEBUG_INFO
, "PlatformUpdate (With Config):\n"));
177 ZeroMem (&ConfigHeader
, sizeof(ConfigHeader
));
178 Status
= ParseUpdateDataFile (
184 DEBUG((DEBUG_INFO
, "ParseUpdateDataFile - %r\n", Status
));
185 if (EFI_ERROR(Status
)) {
186 *LastAttemptStatus
= LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL
;
187 return EFI_INVALID_PARAMETER
;
189 DEBUG((DEBUG_INFO
, "ConfigHeader.NumOfUpdates - 0x%x\n", ConfigHeader
.NumOfUpdates
));
190 DEBUG((DEBUG_INFO
, "PcdEdkiiSystemFirmwareFileGuid - %g\n", PcdGetPtr(PcdEdkiiSystemFirmwareFileGuid
)));
193 for (Index
= 0; Index
< ConfigHeader
.NumOfUpdates
; Index
++) {
194 if (CompareGuid(&ConfigData
[Index
].FileGuid
, PcdGetPtr(PcdEdkiiSystemFirmwareFileGuid
))) {
195 TotalSize
= TotalSize
+ ConfigData
[Index
].Length
;
201 UpdateConfigData
= ConfigData
;
202 while (Index
< ConfigHeader
.NumOfUpdates
) {
203 if (CompareGuid(&UpdateConfigData
->FileGuid
, PcdGetPtr(PcdEdkiiSystemFirmwareFileGuid
))) {
204 DEBUG((DEBUG_INFO
, "FileGuid - %g (processing)\n", &UpdateConfigData
->FileGuid
));
205 StartPercentage
= (BytesWritten
* 100) / TotalSize
;
206 EndPercentage
= ((BytesWritten
+ UpdateConfigData
->Length
) * 100) / TotalSize
;
207 Status
= PerformUpdate (
209 SystemFirmwareImageSize
,
218 // Shall updates be serialized so that if an update is not successfully completed,
219 // the remaining updates won't be performed.
221 if (EFI_ERROR (Status
)) {
225 DEBUG((DEBUG_INFO
, "FileGuid - %g (ignored)\n", &UpdateConfigData
->FileGuid
));
228 BytesWritten
+= UpdateConfigData
->Length
;
238 Authenticate and update System Firmware image.
240 Caution: This function may receive untrusted input.
242 @param[in] Image The EDKII system FMP capsule image.
243 @param[in] ImageSize The size of the EDKII system FMP capsule image in bytes.
244 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
245 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
246 @param[in] Progress A function used by the driver to report the progress of the firmware update.
248 @retval EFI_SUCCESS EDKII system FMP capsule passes authentication and the System Firmware image is updated.
249 @retval EFI_SECURITY_VIOLATION EDKII system FMP capsule fails authentication and the System Firmware image is not updated.
250 @retval EFI_WRITE_PROTECTED The flash device is read only.
253 SystemFirmwareAuthenticatedUpdate (
256 OUT UINT32
*LastAttemptVersion
,
257 OUT UINT32
*LastAttemptStatus
,
258 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
262 VOID
*SystemFirmwareImage
;
263 UINTN SystemFirmwareImageSize
;
265 UINTN ConfigImageSize
;
266 VOID
*AuthenticatedImage
;
267 UINTN AuthenticatedImageSize
;
269 AuthenticatedImage
= NULL
;
270 AuthenticatedImageSize
= 0;
272 DEBUG((DEBUG_INFO
, "SystemFirmwareAuthenticatedUpdate...\n"));
274 Status
= CapsuleAuthenticateSystemFirmware(Image
, ImageSize
, FALSE
, LastAttemptVersion
, LastAttemptStatus
, &AuthenticatedImage
, &AuthenticatedImageSize
);
275 if (EFI_ERROR(Status
)) {
276 DEBUG((DEBUG_INFO
, "SystemFirmwareAuthenticateImage - %r\n", Status
));
280 DEBUG((DEBUG_INFO
, "ExtractSystemFirmwareImage ...\n"));
281 ExtractSystemFirmwareImage(AuthenticatedImage
, AuthenticatedImageSize
, &SystemFirmwareImage
, &SystemFirmwareImageSize
);
282 DEBUG((DEBUG_INFO
, "ExtractConfigImage ...\n"));
283 ExtractConfigImage(AuthenticatedImage
, AuthenticatedImageSize
, &ConfigImage
, &ConfigImageSize
);
285 DEBUG((DEBUG_INFO
, "UpdateImage ...\n"));
286 Status
= UpdateImage(SystemFirmwareImage
, SystemFirmwareImageSize
, ConfigImage
, ConfigImageSize
, LastAttemptVersion
, LastAttemptStatus
, Progress
);
287 if (EFI_ERROR(Status
)) {
288 DEBUG((DEBUG_INFO
, "UpdateImage - %r\n", Status
));
292 DEBUG((DEBUG_INFO
, "SystemFirmwareAuthenticatedUpdate Done\n"));
299 This code finds variable in storage blocks (Volatile or Non-Volatile).
301 @param[in] VariableName Name of Variable to be found.
302 @param[in] VendorGuid Variable vendor GUID.
303 @param[out] Attributes Attribute value of the variable found.
304 @param[in, out] DataSize Size of Data found. If size is less than the
305 data, this value contains the required size.
306 @param[out] Data Data pointer.
308 @return EFI_INVALID_PARAMETER Invalid parameter.
309 @return EFI_SUCCESS Find the specified variable.
310 @return EFI_NOT_FOUND Not found.
311 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
317 IN CHAR16
*VariableName
,
318 IN EFI_GUID
*VendorGuid
,
319 OUT UINT32
*Attributes OPTIONAL
,
320 IN OUT UINTN
*DataSize
,
324 DEBUG((DEBUG_INFO
, "GetVariableHook - %S, %g\n", VariableName
, VendorGuid
));
325 return EFI_NOT_AVAILABLE_YET
;
330 This code Finds the Next available variable.
332 @param[in, out] VariableNameSize Size of the variable name.
333 @param[in, out] VariableName Pointer to variable name.
334 @param[in, out] VendorGuid Variable Vendor Guid.
336 @return EFI_INVALID_PARAMETER Invalid parameter.
337 @return EFI_SUCCESS Find the specified variable.
338 @return EFI_NOT_FOUND Not found.
339 @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
344 GetNextVariableNameHook (
345 IN OUT UINTN
*VariableNameSize
,
346 IN OUT CHAR16
*VariableName
,
347 IN OUT EFI_GUID
*VendorGuid
350 DEBUG((DEBUG_INFO
, "GetNextVariableNameHook - %S, %g\n", VariableName
, VendorGuid
));
351 return EFI_NOT_AVAILABLE_YET
;
356 This code sets variable in storage blocks (Volatile or Non-Volatile).
358 @param[in] VariableName Name of Variable to be found.
359 @param[in] VendorGuid Variable vendor GUID.
360 @param[in] Attributes Attribute value of the variable found
361 @param[in] DataSize Size of Data found. If size is less than the
362 data, this value contains the required size.
363 @param[in] Data Data pointer.
365 @return EFI_INVALID_PARAMETER Invalid parameter.
366 @return EFI_SUCCESS Set successfully.
367 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
368 @return EFI_NOT_FOUND Not found.
369 @return EFI_WRITE_PROTECTED Variable is read-only.
375 IN CHAR16
*VariableName
,
376 IN EFI_GUID
*VendorGuid
,
377 IN UINT32 Attributes
,
382 DEBUG((DEBUG_INFO
, "SetVariableHook - %S, %g, 0x%x (0x%x)\n", VariableName
, VendorGuid
, Attributes
, DataSize
));
383 return EFI_NOT_AVAILABLE_YET
;
388 This code returns information about the EFI variables.
390 @param[in] Attributes Attributes bitmask to specify the type of variables
391 on which to return information.
392 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
393 for the EFI variables associated with the attributes specified.
394 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
395 for EFI variables associated with the attributes specified.
396 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
397 associated with the attributes specified.
399 @return EFI_SUCCESS Query successfully.
404 QueryVariableInfoHook (
405 IN UINT32 Attributes
,
406 OUT UINT64
*MaximumVariableStorageSize
,
407 OUT UINT64
*RemainingVariableStorageSize
,
408 OUT UINT64
*MaximumVariableSize
411 DEBUG((DEBUG_INFO
, "QueryVariableInfoHook - 0x%x\n", Attributes
));
412 return EFI_NOT_AVAILABLE_YET
;
416 Updates the firmware image of the device.
418 This function updates the hardware with the new firmware image.
419 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
420 If the firmware image is updatable, the function should perform the following minimal validations
421 before proceeding to do the firmware image update.
422 - Validate the image authentication if image has attribute
423 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
424 EFI_SECURITY_VIOLATION if the validation fails.
425 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
426 the image is unsupported. The function can optionally provide more detailed information on
427 why the image is not a supported image.
428 - Validate the data from VendorCode if not null. Image validation must be performed before
429 VendorCode data validation. VendorCode data is ignored or considered invalid if image
430 validation failed. The function returns EFI_ABORTED if the data is invalid.
432 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
433 the caller did not specify the policy or use the default policy. As an example, vendor can implement
434 a policy to allow an option to force a firmware image update when the abort reason is due to the new
435 firmware image version is older than the current firmware image version or bad image checksum.
436 Sensitive operations such as those wiping the entire firmware image and render the device to be
437 non-functional should be encoded in the image itself rather than passed with the VendorCode.
438 AbortReason enables vendor to have the option to provide a more detailed description of the abort
439 reason to the caller.
441 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
442 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
443 The number is between 1 and DescriptorCount.
444 @param[in] Image Points to the new image.
445 @param[in] ImageSize Size of the new image in bytes.
446 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
447 Null indicates the caller did not specify the policy or use the default policy.
448 @param[in] Progress A function used by the driver to report the progress of the firmware update.
449 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
450 details for the aborted operation. The buffer is allocated by this function
451 with AllocatePool(), and it is the caller's responsibility to free it with a
454 @retval EFI_SUCCESS The device was successfully updated with the new image.
455 @retval EFI_ABORTED The operation is aborted.
456 @retval EFI_INVALID_PARAMETER The Image was NULL.
457 @retval EFI_UNSUPPORTED The operation is not supported.
458 @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
464 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*This
,
466 IN CONST VOID
*Image
,
468 IN CONST VOID
*VendorCode
,
469 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
,
470 OUT CHAR16
**AbortReason
474 EFI_STATUS VarStatus
;
475 SYSTEM_FMP_PRIVATE_DATA
*SystemFmpPrivate
;
477 if (Image
== NULL
|| ImageSize
== 0 || AbortReason
== NULL
) {
478 return EFI_INVALID_PARAMETER
;
481 SystemFmpPrivate
= SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This
);
484 if (ImageIndex
== 0 || ImageIndex
> SystemFmpPrivate
->DescriptorCount
) {
485 return EFI_INVALID_PARAMETER
;
488 Status
= SystemFirmwareAuthenticatedUpdate((VOID
*)Image
, ImageSize
, &SystemFmpPrivate
->LastAttempt
.LastAttemptVersion
, &SystemFmpPrivate
->LastAttempt
.LastAttemptStatus
, Progress
);
489 DEBUG((DEBUG_INFO
, "SetImage - LastAttempt Version - 0x%x, State - 0x%x\n", SystemFmpPrivate
->LastAttempt
.LastAttemptVersion
, SystemFmpPrivate
->LastAttempt
.LastAttemptStatus
));
492 // If NVRAM is updated, we should no longer touch variable services, because
493 // the current variable driver may not manage the new NVRAM region.
496 DEBUG ((DEBUG_INFO
, "NvRamUpdated, Update Variable Services\n"));
497 gRT
->GetVariable
= GetVariableHook
;
498 gRT
->GetNextVariableName
= GetNextVariableNameHook
;
499 gRT
->SetVariable
= SetVariableHook
;
500 gRT
->QueryVariableInfo
= QueryVariableInfoHook
;
503 gBS
->CalculateCrc32 (
510 VarStatus
= gRT
->SetVariable(
511 SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME
,
512 &gSystemFmpLastAttemptVariableGuid
,
513 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
514 sizeof(SystemFmpPrivate
->LastAttempt
),
515 &SystemFmpPrivate
->LastAttempt
517 DEBUG((DEBUG_INFO
, "SetLastAttempt - %r\n", VarStatus
));
523 Get the set of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures from an FMP Protocol.
525 @param[in] Handle Handle with an FMP Protocol or a System FMP
527 @param[in] ProtocolGuid Pointer to the FMP Protocol GUID or System FMP
529 @param[out] FmpImageInfoCount Pointer to the number of
530 EFI_FIRMWARE_IMAGE_DESCRIPTOR structures.
531 @param[out] DescriptorSize Pointer to the size, in bytes, of each
532 EFI_FIRMWARE_IMAGE_DESCRIPTOR structure.
534 @return NULL No EFI_FIRMWARE_IMAGE_DESCRIPTOR structures found.
535 @return !NULL Pointer to a buffer of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures
536 allocated using AllocatePool(). Caller must free buffer with
539 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*
540 GetFmpImageDescriptors (
541 IN EFI_HANDLE Handle
,
542 IN EFI_GUID
*ProtocolGuid
,
543 OUT UINT8
*FmpImageInfoCount
,
544 OUT UINTN
*DescriptorSize
548 EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*Fmp
;
550 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfoBuf
;
551 UINT32 FmpImageInfoDescriptorVer
;
552 UINT32 PackageVersion
;
553 CHAR16
*PackageVersionName
;
555 *FmpImageInfoCount
= 0;
558 Status
= gBS
->HandleProtocol (
563 if (EFI_ERROR (Status
)) {
568 // Determine the size required for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
571 Status
= Fmp
->GetImageInfo (
573 &ImageInfoSize
, // Buffer Size (in this case 0)
574 NULL
, // NULL so we can get size
575 &FmpImageInfoDescriptorVer
, // DescriptorVersion
576 FmpImageInfoCount
, // DescriptorCount
577 DescriptorSize
, // DescriptorSize
578 &PackageVersion
, // PackageVersion
579 &PackageVersionName
// PackageVersionName
581 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
582 DEBUG ((DEBUG_ERROR
, "SystemFirmwareUpdateDxe: Unexpected Failure. Status = %r\n", Status
));
587 // Allocate buffer for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
589 FmpImageInfoBuf
= NULL
;
590 FmpImageInfoBuf
= AllocateZeroPool (ImageInfoSize
);
591 if (FmpImageInfoBuf
== NULL
) {
592 DEBUG ((DEBUG_ERROR
, "SystemFirmwareUpdateDxe: Failed to allocate memory for descriptors.\n"));
597 // Retrieve the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
599 PackageVersionName
= NULL
;
600 Status
= Fmp
->GetImageInfo (
602 &ImageInfoSize
, // ImageInfoSize
603 FmpImageInfoBuf
, // ImageInfo
604 &FmpImageInfoDescriptorVer
, // DescriptorVersion
605 FmpImageInfoCount
, // DescriptorCount
606 DescriptorSize
, // DescriptorSize
607 &PackageVersion
, // PackageVersion
608 &PackageVersionName
// PackageVersionName
612 // Free unused PackageVersionName return buffer
614 if (PackageVersionName
!= NULL
) {
615 FreePool (PackageVersionName
);
616 PackageVersionName
= NULL
;
619 if (EFI_ERROR (Status
)) {
620 DEBUG ((DEBUG_ERROR
, "SystemFirmwareUpdateDxe: Failure in GetImageInfo. Status = %r\n", Status
));
621 if (FmpImageInfoBuf
!= NULL
) {
622 FreePool (FmpImageInfoBuf
);
627 return FmpImageInfoBuf
;
631 Search for handles with an FMP protocol whose EFI_FIRMWARE_IMAGE_DESCRIPTOR
632 ImageTypeId matches the ImageTypeId produced by this module.
634 @param[in] ProtocolGuid Pointer to the GUID of the protocol to search.
635 @param[out] HandleCount Pointer to the number of returned handles.
637 @return NULL No matching handles found.
638 @return !NULL Pointer to a buffer of handles allocated using AllocatePool().
639 Caller must free buffer with FreePool().
642 FindMatchingFmpHandles (
643 IN EFI_GUID
*ProtocolGuid
,
644 OUT UINTN
*HandleCount
648 UINTN TempHandleCount
;
649 EFI_HANDLE
*HandleBuffer
;
653 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*OriginalFmpImageInfoBuf
;
654 EFI_FIRMWARE_IMAGE_DESCRIPTOR
*FmpImageInfoBuf
;
655 UINT8 FmpImageInfoCount
;
656 UINTN DescriptorSize
;
662 Status
= gBS
->LocateHandleBuffer (
669 if (EFI_ERROR (Status
)) {
673 for (Index
= 0; Index
< TempHandleCount
; Index
++) {
674 OriginalFmpImageInfoBuf
= GetFmpImageDescriptors (
682 // Loop through the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
684 FmpImageInfoBuf
= OriginalFmpImageInfoBuf
;
686 for (Index2
= 0; Index2
< FmpImageInfoCount
; Index2
++) {
687 for (Index3
= 0; Index3
< mSystemFmpPrivate
->DescriptorCount
; Index3
++) {
688 MatchFound
= CompareGuid (
689 &FmpImageInfoBuf
->ImageTypeId
,
690 &mSystemFmpPrivate
->ImageDescriptor
[Index3
].ImageTypeId
700 // Increment the buffer pointer ahead by the size of the descriptor
702 FmpImageInfoBuf
= (EFI_FIRMWARE_IMAGE_DESCRIPTOR
*)(((UINT8
*)FmpImageInfoBuf
) + DescriptorSize
);
705 HandleBuffer
[*HandleCount
] = HandleBuffer
[Index
];
709 FreePool (OriginalFmpImageInfoBuf
);
712 if ((*HandleCount
) == 0) {
714 // No any matching handle.
716 FreePool (HandleBuffer
);
723 Uninstall System FMP Protocol instances that may have been installed by
724 SystemFirmwareUpdateDxe drivers dispatches by other capsules.
726 @retval EFI_SUCCESS All System FMP Protocols found were uninstalled.
727 @return Other One or more System FMP Protocols could not be uninstalled.
731 UninstallMatchingSystemFmpProtocols (
736 EFI_HANDLE
*HandleBuffer
;
739 EFI_FIRMWARE_MANAGEMENT_PROTOCOL
*SystemFmp
;
742 // Uninstall SystemFmpProtocol instances that may have been produced by
743 // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.
745 HandleBuffer
= FindMatchingFmpHandles (
746 &gSystemFmpProtocolGuid
,
749 DEBUG ((DEBUG_INFO
, "SystemFirmwareUpdateDxe: Found %d matching System FMP instances\n", HandleCount
));
751 for (Index
= 0; Index
< HandleCount
; Index
++) {
752 Status
= gBS
->HandleProtocol(
754 &gSystemFmpProtocolGuid
,
757 if (EFI_ERROR (Status
)) {
760 DEBUG ((DEBUG_INFO
, "SystemFirmwareUpdateDxe: Uninstall SystemFmp produced by another capsule\n"));
761 Status
= gBS
->UninstallProtocolInterface (
763 &gSystemFmpProtocolGuid
,
766 if (EFI_ERROR (Status
)) {
767 DEBUG ((DEBUG_ERROR
, "SystemFirmwareUpdateDxe: Failed to uninstall SystemFmp %r. Exiting.\n", Status
));
768 FreePool (HandleBuffer
);
772 if (HandleBuffer
!= NULL
) {
773 FreePool (HandleBuffer
);
780 System FMP module entrypoint
782 @param[in] ImageHandle The firmware allocated handle for the EFI image.
783 @param[in] SystemTable A pointer to the EFI System Table.
785 @retval EFI_SUCCESS System FMP module is initialized.
786 @retval EFI_OUT_OF_RESOURCES There are not enough resources avaulable to
787 initialize this module.
788 @retval Other System FMP Protocols could not be uninstalled.
789 @retval Other System FMP Protocol could not be installed.
790 @retval Other FMP Protocol could not be installed.
794 SystemFirmwareUpdateMainDxe (
795 IN EFI_HANDLE ImageHandle
,
796 IN EFI_SYSTEM_TABLE
*SystemTable
800 EFI_HANDLE
*HandleBuffer
;
804 // Initialize SystemFmpPrivateData
806 mSystemFmpPrivate
= AllocateZeroPool (sizeof (SYSTEM_FMP_PRIVATE_DATA
));
807 if (mSystemFmpPrivate
== NULL
) {
808 return EFI_OUT_OF_RESOURCES
;
811 Status
= InitializePrivateData (mSystemFmpPrivate
);
812 if (EFI_ERROR (Status
)) {
813 FreePool (mSystemFmpPrivate
);
814 mSystemFmpPrivate
= NULL
;
819 // Uninstall SystemFmpProtocol instances that may have been produced by
820 // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.
822 Status
= UninstallMatchingSystemFmpProtocols ();
823 if (EFI_ERROR (Status
)) {
824 FreePool (mSystemFmpPrivate
);
825 mSystemFmpPrivate
= NULL
;
830 // Look for a handle with matching Firmware Management Protocol
833 HandleBuffer
= FindMatchingFmpHandles (
834 &gEfiFirmwareManagementProtocolGuid
,
837 DEBUG ((DEBUG_INFO
, "SystemFirmwareUpdateDxe: Found %d matching FMP instances\n", HandleCount
));
839 switch (HandleCount
) {
842 // Install FMP protocol onto a new handle.
844 DEBUG ((DEBUG_INFO
, "SystemFirmwareUpdateDxe: Install FMP onto a new handle\n"));
845 Status
= gBS
->InstallMultipleProtocolInterfaces (
846 &mSystemFmpPrivate
->Handle
,
847 &gEfiFirmwareManagementProtocolGuid
,
848 &mSystemFmpPrivate
->Fmp
,
854 // Install System FMP protocol onto handle with matching FMP Protocol
856 DEBUG ((DEBUG_INFO
, "SystemFirmwareUpdateDxe: Install System FMP onto matching FMP handle\n"));
857 mSystemFmpPrivate
->Handle
= HandleBuffer
[0];
858 Status
= gBS
->InstallMultipleProtocolInterfaces (
860 &gSystemFmpProtocolGuid
,
861 &mSystemFmpPrivate
->Fmp
,
867 // More than one matching handle is not expected. Unload driver.
869 DEBUG ((DEBUG_ERROR
, "SystemFirmwareUpdateDxe: More than one matching FMP handle. Unload driver.\n"));
870 Status
= EFI_DEVICE_ERROR
;
874 if (HandleBuffer
!= NULL
) {
875 FreePool (HandleBuffer
);
878 if (EFI_ERROR (Status
)) {
879 FreePool (mSystemFmpPrivate
);
880 mSystemFmpPrivate
= NULL
;