3 Copyright (c) 2016, Microsoft Corporation. All rights reserved.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/FmpDeviceLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/PlatformFlashAccessLib.h>
22 //#include <Protocol/FirmwareManagement.h>
24 //#include <Guid/SystemResourceTable.h>
27 PLATFORM_FIRMWARE_TYPE FirmwareType
;
28 FLASH_ADDRESS_TYPE AddressType
;
29 EFI_PHYSICAL_ADDRESS BaseAddress
;
34 UPDATE_CONFIG_DATA mUpdateConfigData
[] = {
35 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00000000, 0x00040000, 0x00000000 },
36 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x000C0000, 0x00050000, 0x000C0000 },
37 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00110000, 0x00210000, 0x00110000 },
38 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00320000, 0x00070000, 0x00320000 },
39 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00390000, 0x00070000, 0x00390000 }
43 Used to pass the FMP install function to this lib. This allows the library to
44 have control of the handle that the FMP instance is installed on. This allows
45 the library to use DriverBinding protocol model to locate its device(s) in the
48 @param[in] Func Function pointer to FMP install function.
50 @retval EFI_SUCCESS Library has saved function pointer and will call
51 function pointer on each DriverBinding Start.
52 @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports
54 @retval other error Error occurred. Don't install FMP
59 RegisterFmpInstaller (
60 IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func
64 // This is a system firmware update that does not use Driver Binding Protocol
66 return EFI_UNSUPPORTED
;
71 Returns the size, in bytes, of the firmware image currently stored in the
72 firmware device. This function is used to by the GetImage() and
73 GetImageInfo() services of the Firmware Management Protocol. If the image
74 size can not be determined from the firmware device, then 0 must be returned.
76 @param[out] Size Pointer to the size, in bytes, of the firmware image
77 currently stored in the firmware device.
79 @retval EFI_SUCCESS The size of the firmware image currently
80 stored in the firmware device was returned.
81 @retval EFI_INVALID_PARAMETER Size is NULL.
82 @retval EFI_UNSUPPORTED The firmware device does not support reporting
83 the size of the currently stored firmware image.
84 @retval EFI_DEVICE_ERROR An error occured attempting to determine the
85 size of the firmware image currently stored in
86 in the firmware device.
96 return EFI_INVALID_PARAMETER
;
98 *Size
= PcdGet32 (PcdBiosRomBase
);
103 Used to return a library supplied guid that will be the ImageTypeId guid of
104 the FMP descriptor. This is optional but can be used if at runtime the guid
105 needs to be determined.
107 @param[out] Guid Double Guid Ptr that will be updated to point to guid.
108 This should be from static memory and will not be freed.
110 @return EFI_UNSUPPORTED Library instance doesn't need dynamic guid.
111 @return Error Any error will cause the wrapper to use the GUID
113 @return EFI_SUCCESS Guid ptr should be updated to point to static memeory
114 which contains a valid guid.
119 FmpDeviceGetImageTypeIdGuidPtr (
123 return EFI_UNSUPPORTED
;
127 Returns values used to fill in the AttributesSupported and AttributesSettings
128 fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the
129 GetImageInfo() service of the Firmware Management Protocol. The following
130 bit values from the Firmware Management Protocol may be combined:
131 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
132 IMAGE_ATTRIBUTE_RESET_REQUIRED
133 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
134 IMAGE_ATTRIBUTE_IN_USE
135 IMAGE_ATTRIBUTE_UEFI_IMAGE
137 @param[out] Supported Attributes supported by this firmware device.
138 @param[out] Setting Attributes settings for this firmware device.
140 @retval EFI_SUCCESS The attributes supported by the firmware
141 device were returned.
142 @retval EFI_INVALID_PARAMETER Supported is NULL.
143 @retval EFI_INVALID_PARAMETER Setting is NULL.
148 FmpDeviceGetAttributes (
149 IN OUT UINT64
*Supported
,
150 IN OUT UINT64
*Setting
153 if (Supported
== NULL
|| Setting
== NULL
) {
154 return EFI_INVALID_PARAMETER
;
156 *Supported
= (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
|
157 IMAGE_ATTRIBUTE_RESET_REQUIRED
|
158 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
|
159 IMAGE_ATTRIBUTE_IN_USE
161 *Setting
= (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
|
162 IMAGE_ATTRIBUTE_RESET_REQUIRED
|
163 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
|
164 IMAGE_ATTRIBUTE_IN_USE
170 Gets the current Lowest Supported Version.
172 This is a protection mechanism so that a previous version with known issue is
173 not applied. ONLY implement this if your running firmware has a method to
174 return this at runtime. If EFI_UNSUPPORTED is returned, then the Lowest
175 Supported Version is stored in a UEFI Variable.
177 @param[out] Version On return this value represents the current Lowest
178 Supported Version (in same format as GetVersion).
180 @retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved
181 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV
182 @retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV
186 FmpDeviceGetLowestSupportedVersion (
187 IN OUT UINT32
*LowestSupportedVersion
191 // Retrieve the lowest support version from a PCD
192 // NOTE: This method of using a PCD can only be used for the system firmware
193 // FMP instance that is updated every time the system firmware is
194 // updated. If system firmware updates support partial updates that
195 // would not include the system firmware FMP instance, then a PCD can
196 // not be used and the value must come from the currently running system
199 *LowestSupportedVersion
= PcdGet32 (PcdSystemFirmwareFmpLowestSupportedVersion
);
205 Returns the Null-terminated Unicode string that is used to fill in the
206 VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is
207 returned by the GetImageInfo() service of the Firmware Management Protocol.
208 The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().
210 @note It is recommended that all firmware devices support a method to report
211 the VersionName string from the currently stored firmware image.
213 @param[out] VersionString The version string retrieved from the currently
214 stored firmware image.
216 @retval EFI_SUCCESS The version string of currently stored
217 firmware image was returned in Version.
218 @retval EFI_INVALID_PARAMETER VersionString is NULL.
219 @retval EFI_UNSUPPORTED The firmware device does not support a method
220 to report the version string of the currently
221 stored firmware image.
222 @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the
223 version string of the currently stored
225 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
226 buffer for the version string of the currently
227 stored firmware image.
232 FmpDeviceGetVersionString (
233 OUT CHAR16
**VersionString
236 if (VersionString
== NULL
) {
237 return EFI_INVALID_PARAMETER
;
241 // Retrieve the version string from a PCD
242 // NOTE: This method of using a PCD can only be used for the system firmware
243 // FMP instance that is updated every time the system firmware is
244 // updated. If system firmware updates support partial updates that
245 // would not include the system firmware FMP instance, then a PCD can
246 // not be used and the value must come from the currently running system
249 *VersionString
= (CHAR16
*)AllocateCopyPool (
250 PcdGetSize (PcdSystemFirmwareFmpVersionString
),
251 PcdGetPtr (PcdSystemFirmwareFmpVersionString
)
253 if (*VersionString
== NULL
) {
254 return EFI_OUT_OF_RESOURCES
;
260 Gets the current running version.
262 ONLY implement this if your running firmware has a method to return this at
265 @param[out] Version On return this value represents the current running
268 @retval EFI_SUCCESS The version was correctly retrieved.
269 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting current
271 @retval EFI_DEVICE_ERROR Error occurred when trying to get the version.
275 FmpDeviceGetVersion (
276 IN OUT UINT32
*Version
280 // Retrieve the version string from a PCD
281 // NOTE: This method of using a PCD can only be used for the system firmware
282 // FMP instance that is updated every time the system firmware is
283 // updated. If system firmware updates support partial updates that
284 // would not include the system firmware FMP instance, then a PCD can
285 // not be used and the value must come from the currently running system
288 *Version
= PcdGet32 (PcdSystemFirmwareFmpVersion
);
294 Retrieves a copy of the current firmware image of the device.
296 This function allows a copy of the current firmware image to be created and
297 saved. The saved copy could later been used, for example, in firmware image
298 recovery or rollback.
300 @param[out] Image Points to the buffer where the current image is copied
302 @param[out] ImageSize On entry, points to the size of the buffer pointed to
303 by Image, in bytes. On return, points to the length of
306 @retval EFI_SUCCESS The image was successfully read from the device.
307 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small
308 to hold the image. The current buffer size
309 needed to hold the image is returned in
311 @retval EFI_INVALID_PARAMETER The Image was NULL.
312 @retval EFI_NOT_FOUND The current image is not copied to the buffer.
313 @retval EFI_UNSUPPORTED The operation is not supported.
320 IN OUT UINTN
*ImageSize
324 // Check for invalid p;arameters
326 if (Image
== NULL
|| ImageSize
== NULL
) {
327 return EFI_INVALID_PARAMETER
;
331 // Make sure the buffer is big enough to hold the device image
333 if (*ImageSize
< PcdGet32 (PcdBiosRomSize
)) {
334 *ImageSize
= PcdGet32 (PcdBiosRomSize
);
335 return EFI_BUFFER_TOO_SMALL
;
339 // Copy the device image to the buffer
341 *ImageSize
= PcdGet32 (PcdBiosRomSize
);
344 (VOID
*)(UINTN
)PcdGet32 (PcdBiosRomBase
),
352 Updates the firmware image of the device.
354 This function updates the hardware with the new firmware image. This function
355 returns EFI_UNSUPPORTED if the firmware image is not updatable. If the
356 firmware image is updatable, the function should perform the following minimal
357 validations before proceeding to do the firmware image update.
358 - Validate the image is a supported image for this device. The function
359 returns EFI_ABORTED if the image is unsupported. The function can
360 optionally provide more detailed information on why the image is not a
362 - Validate the data from VendorCode if not null. Image validation must be
363 performed before VendorCode data validation. VendorCode data is ignored
364 or considered invalid if image validation failed. The function returns
365 EFI_ABORTED if the data is invalid.
367 VendorCode enables vendor to implement vendor-specific firmware image update
368 policy. Null if the caller did not specify the policy or use the default
369 policy. As an example, vendor can implement a policy to allow an option to
370 force a firmware image update when the abort reason is due to the new firmware
371 image version is older than the current firmware image version or bad image
372 checksum. Sensitive operations such as those wiping the entire firmware image
373 and render the device to be non-functional should be encoded in the image
374 itself rather than passed with the VendorCode. AbortReason enables vendor to
375 have the option to provide a more detailed description of the abort reason to
378 @param[in] Image Points to the new image.
379 @param[in] ImageSize Size of the new image in bytes.
380 @param[in] VendorCode This enables vendor to implement vendor-specific
381 firmware image update policy. Null indicates the
382 caller did not specify the policy or use the
384 @param[in] Progress A function used by the driver to report the
385 progress of the firmware update.
386 @param[in] CapsuleFwVersion FMP Payload Header version of the image.
387 @param[out] AbortReason A pointer to a pointer to a null-terminated
388 string providing more details for the aborted
389 operation. The buffer is allocated by this
390 function with AllocatePool(), and it is the
391 caller's responsibility to free it with a call
394 @retval EFI_SUCCESS The device was successfully updated with the
396 @retval EFI_ABORTED The operation is aborted.
397 @retval EFI_INVALID_PARAMETER The Image was NULL.
398 @retval EFI_UNSUPPORTED The operation is not supported.
404 IN CONST VOID
*Image
,
406 IN CONST VOID
*VendorCode
,
407 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
,
408 IN UINT32 CapsuleFwVersion
,
409 OUT CHAR16
**AbortReason
416 UPDATE_CONFIG_DATA
*ConfigData
;
421 Status
= FmpDeviceCheckImage (Image
, ImageSize
, &Updateable
);
422 if (EFI_ERROR (Status
)) {
423 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Check Image failed with %r.\n", Status
));
427 if (Updateable
!= IMAGE_UPDATABLE_VALID
) {
428 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable
));
432 if (Progress
== NULL
) {
433 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Invalid progress callback\n"));
434 return EFI_INVALID_PARAMETER
;
437 Status
= Progress (15);
438 if (EFI_ERROR (Status
)) {
439 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status
));
443 // Write the image to the firmware device
446 if (EFI_ERROR (Status
)) {
447 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status
));
451 // Simulate update with delays between progress updates
453 for (Percentage
= 20; Percentage
<= 100; Percentage
++) {
457 // gBS->Stall (50000);
459 // Progress (Percentage);
460 // if (EFI_ERROR (Status)) {
461 // DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));
465 DEBUG ((DEBUG_INFO
, "FmpDeviceSetImage - %d Images ...\n", ARRAY_SIZE (mUpdateConfigData
)));
467 if (ARRAY_SIZE (mUpdateConfigData
) == 0) {
468 DEBUG((DEBUG_INFO
, "PlatformUpdate: BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n", 0, 0, ImageSize
));
469 Status
= PerformFlashWriteWithProgress (
470 PlatformFirmwareTypeSystemFirmware
, // FirmwareType
471 0x00000000, // FlashAddress
472 FlashAddressTypeRelativeAddress
, // FlashAddressType
473 (VOID
*)(UINTN
)Image
, // Buffer
474 ImageSize
, // BufferLength
475 Progress
, // Progress
476 20, // StartPercentage
483 // Compute total size of update
485 for (Index
= 0, TotalSize
= 0; Index
< ARRAY_SIZE (mUpdateConfigData
); Index
++) {
486 TotalSize
+= mUpdateConfigData
[Index
].Length
;
490 for (Index
= 0, ConfigData
= mUpdateConfigData
; Index
< ARRAY_SIZE (mUpdateConfigData
); Index
++, ConfigData
++) {
491 DEBUG((DEBUG_INFO
, "PlatformUpdate(%d): BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n",
493 ConfigData
->BaseAddress
,
494 ConfigData
->ImageOffset
,
497 Status
= PerformFlashWriteWithProgress (
498 ConfigData
->FirmwareType
, // FirmwareType
499 ConfigData
->BaseAddress
, // FlashAddress
500 ConfigData
->AddressType
, // FlashAddressType
501 (VOID
*)((UINTN
)Image
+ (UINTN
)ConfigData
->ImageOffset
), // Buffer
502 ConfigData
->Length
, // BufferLength
503 Progress
, // Progress
504 20 + (BytesWritten
* 80) / TotalSize
, // StartPercentage
505 20 + ((BytesWritten
+ ConfigData
->Length
) * 80) / TotalSize
// EndPercentage
507 if (EFI_ERROR(Status
)) {
510 BytesWritten
+= ConfigData
->Length
;
513 DEBUG ((DEBUG_INFO
, "FmpDeviceSetImage - %r\n", Status
));
519 Checks if the firmware image is valid for the device.
521 This function allows firmware update application to validate the firmware image without
522 invoking the SetImage() first.
524 @param[in] Image Points to the new image.
525 @param[in] ImageSize Size of the new image in bytes.
526 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
527 if available, additional information if the image is invalid.
529 @retval EFI_SUCCESS The image was successfully checked.
530 @retval EFI_INVALID_PARAMETER The Image was NULL.
535 FmpDeviceCheckImage (
536 IN CONST VOID
*Image
,
538 OUT UINT32
*ImageUpdateable
541 if (ImageUpdateable
== NULL
) {
542 DEBUG((DEBUG_ERROR
, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));
543 return EFI_INVALID_PARAMETER
;
547 //Set to valid and then if any tests fail it will update this flag.
549 *ImageUpdateable
= IMAGE_UPDATABLE_VALID
;
552 DEBUG((DEBUG_ERROR
, "CheckImage - Image Pointer Parameter is NULL.\n"));
554 // Not sure if this is needed
556 *ImageUpdateable
= IMAGE_UPDATABLE_INVALID
;
557 return EFI_INVALID_PARAMETER
;
561 // Make sure the image size is correct
563 if (ImageSize
!= PcdGet32 (PcdBiosRomSize
)) {
564 *ImageUpdateable
= IMAGE_UPDATABLE_INVALID
;
565 return EFI_INVALID_PARAMETER
;
572 Device firmware should trigger lock mechanism so that device fw can not be
573 updated or tampered with. This lock mechanism is generally only cleared by a
574 full system reset (not just sleep state/low power mode)
576 @retval EFI_SUCCESS The device was successfully locked.
577 @retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking
586 DEBUG ((DEBUG_INFO
, "VLV2: FmpDeviceLock() for system FLASH\n"));
587 // TODO: Add lock logic
588 return EFI_UNSUPPORTED
;