3 Copyright (c) 2016, Microsoft Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <Library/FmpDeviceLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/BaseLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
37 #include <Library/PlatformFlashAccessLib.h>
39 //#include <Protocol/FirmwareManagement.h>
41 //#include <Guid/SystemResourceTable.h>
44 PLATFORM_FIRMWARE_TYPE FirmwareType
;
45 FLASH_ADDRESS_TYPE AddressType
;
46 EFI_PHYSICAL_ADDRESS BaseAddress
;
51 UPDATE_CONFIG_DATA mUpdateConfigData
[] = {
52 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00000000, 0x00040000, 0x00000000 },
53 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x000C0000, 0x00050000, 0x000C0000 },
54 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00110000, 0x00210000, 0x00110000 },
55 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00320000, 0x00070000, 0x00320000 },
56 { PlatformFirmwareTypeSystemFirmware
, FlashAddressTypeRelativeAddress
, 0x00390000, 0x00070000, 0x00390000 },
57 { PlatformFirmwareTypeNvRam
, FlashAddressTypeRelativeAddress
, 0x00040000, 0x00080000, 0x00040000 }
61 Used to pass the FMP install function to this lib. This allows the library to
62 have control of the handle that the FMP instance is installed on. This allows
63 the library to use DriverBinding protocol model to locate its device(s) in the
66 @param[in] Func Function pointer to FMP install function.
68 @retval EFI_SUCCESS Library has saved function pointer and will call
69 function pointer on each DriverBinding Start.
70 @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports
72 @retval other error Error occurred. Don't install FMP
77 RegisterFmpInstaller (
78 IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func
82 // This is a system firmware update that does not use Driver Binding Protocol
84 return EFI_UNSUPPORTED
;
89 Returns the size, in bytes, of the firmware image currently stored in the
90 firmware device. This function is used to by the GetImage() and
91 GetImageInfo() services of the Firmware Management Protocol. If the image
92 size can not be determined from the firmware device, then 0 must be returned.
94 @param[out] Size Pointer to the size, in bytes, of the firmware image
95 currently stored in the firmware device.
97 @retval EFI_SUCCESS The size of the firmware image currently
98 stored in the firmware device was returned.
99 @retval EFI_INVALID_PARAMETER Size is NULL.
100 @retval EFI_UNSUPPORTED The firmware device does not support reporting
101 the size of the currently stored firmware image.
102 @retval EFI_DEVICE_ERROR An error occured attempting to determine the
103 size of the firmware image currently stored in
104 in the firmware device.
114 return EFI_INVALID_PARAMETER
;
116 *Size
= PcdGet32 (PcdBiosRomBase
);
121 Used to return a library supplied guid that will be the ImageTypeId guid of
122 the FMP descriptor. This is optional but can be used if at runtime the guid
123 needs to be determined.
125 @param[out] Guid Double Guid Ptr that will be updated to point to guid.
126 This should be from static memory and will not be freed.
128 @return EFI_UNSUPPORTED Library instance doesn't need dynamic guid.
129 @return Error Any error will cause the wrapper to use the GUID
131 @return EFI_SUCCESS Guid ptr should be updated to point to static memeory
132 which contains a valid guid.
137 FmpDeviceGetImageTypeIdGuidPtr (
141 return EFI_UNSUPPORTED
;
145 Returns values used to fill in the AttributesSupported and AttributesSettings
146 fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the
147 GetImageInfo() service of the Firmware Management Protocol. The following
148 bit values from the Firmware Management Protocol may be combined:
149 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
150 IMAGE_ATTRIBUTE_RESET_REQUIRED
151 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
152 IMAGE_ATTRIBUTE_IN_USE
153 IMAGE_ATTRIBUTE_UEFI_IMAGE
155 @param[out] Supported Attributes supported by this firmware device.
156 @param[out] Setting Attributes settings for this firmware device.
158 @retval EFI_SUCCESS The attributes supported by the firmware
159 device were returned.
160 @retval EFI_INVALID_PARAMETER Supported is NULL.
161 @retval EFI_INVALID_PARAMETER Setting is NULL.
166 FmpDeviceGetAttributes (
167 IN OUT UINT64
*Supported
,
168 IN OUT UINT64
*Setting
171 if (Supported
== NULL
|| Setting
== NULL
) {
172 return EFI_INVALID_PARAMETER
;
174 *Supported
= (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
|
175 IMAGE_ATTRIBUTE_RESET_REQUIRED
|
176 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
|
177 IMAGE_ATTRIBUTE_IN_USE
179 *Setting
= (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
|
180 IMAGE_ATTRIBUTE_RESET_REQUIRED
|
181 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
|
182 IMAGE_ATTRIBUTE_IN_USE
188 Gets the current Lowest Supported Version.
190 This is a protection mechanism so that a previous version with known issue is
191 not applied. ONLY implement this if your running firmware has a method to
192 return this at runtime. If EFI_UNSUPPORTED is returned, then the Lowest
193 Supported Version is stored in a UEFI Variable.
195 @param[out] Version On return this value represents the current Lowest
196 Supported Version (in same format as GetVersion).
198 @retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved
199 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV
200 @retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV
204 FmpDeviceGetLowestSupportedVersion (
205 IN OUT UINT32
*LowestSupportedVersion
209 // Retrieve the lowest support version from a PCD
210 // NOTE: This method of using a PCD can only be used for the system firmware
211 // FMP instance that is updated every time the system firmware is
212 // updated. If system firmware updates support partial updates that
213 // would not include the system firmware FMP instance, then a PCD can
214 // not be used and the value must come from the currently running system
217 *LowestSupportedVersion
= PcdGet32 (PcdSystemFirmwareFmpLowestSupportedVersion
);
223 Returns the Null-terminated Unicode string that is used to fill in the
224 VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is
225 returned by the GetImageInfo() service of the Firmware Management Protocol.
226 The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().
228 @note It is recommended that all firmware devices support a method to report
229 the VersionName string from the currently stored firmware image.
231 @param[out] VersionString The version string retrieved from the currently
232 stored firmware image.
234 @retval EFI_SUCCESS The version string of currently stored
235 firmware image was returned in Version.
236 @retval EFI_INVALID_PARAMETER VersionString is NULL.
237 @retval EFI_UNSUPPORTED The firmware device does not support a method
238 to report the version string of the currently
239 stored firmware image.
240 @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the
241 version string of the currently stored
243 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
244 buffer for the version string of the currently
245 stored firmware image.
250 FmpDeviceGetVersionString (
251 OUT CHAR16
**VersionString
254 if (VersionString
== NULL
) {
255 return EFI_INVALID_PARAMETER
;
259 // Retrieve the version string from a PCD
260 // NOTE: This method of using a PCD can only be used for the system firmware
261 // FMP instance that is updated every time the system firmware is
262 // updated. If system firmware updates support partial updates that
263 // would not include the system firmware FMP instance, then a PCD can
264 // not be used and the value must come from the currently running system
267 *VersionString
= (CHAR16
*)AllocateCopyPool (
268 PcdGetSize (PcdSystemFirmwareFmpVersionString
),
269 PcdGetPtr (PcdSystemFirmwareFmpVersionString
)
271 if (*VersionString
== NULL
) {
272 return EFI_OUT_OF_RESOURCES
;
278 Gets the current running version.
280 ONLY implement this if your running firmware has a method to return this at
283 @param[out] Version On return this value represents the current running
286 @retval EFI_SUCCESS The version was correctly retrieved.
287 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting current
289 @retval EFI_DEVICE_ERROR Error occurred when trying to get the version.
293 FmpDeviceGetVersion (
294 IN OUT UINT32
*Version
298 // Retrieve the version string from a PCD
299 // NOTE: This method of using a PCD can only be used for the system firmware
300 // FMP instance that is updated every time the system firmware is
301 // updated. If system firmware updates support partial updates that
302 // would not include the system firmware FMP instance, then a PCD can
303 // not be used and the value must come from the currently running system
306 *Version
= PcdGet32 (PcdSystemFirmwareFmpVersion
);
312 Retrieves a copy of the current firmware image of the device.
314 This function allows a copy of the current firmware image to be created and
315 saved. The saved copy could later been used, for example, in firmware image
316 recovery or rollback.
318 @param[out] Image Points to the buffer where the current image is copied
320 @param[out] ImageSize On entry, points to the size of the buffer pointed to
321 by Image, in bytes. On return, points to the length of
324 @retval EFI_SUCCESS The image was successfully read from the device.
325 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small
326 to hold the image. The current buffer size
327 needed to hold the image is returned in
329 @retval EFI_INVALID_PARAMETER The Image was NULL.
330 @retval EFI_NOT_FOUND The current image is not copied to the buffer.
331 @retval EFI_UNSUPPORTED The operation is not supported.
338 IN OUT UINTN
*ImageSize
342 // Check for invalid p;arameters
344 if (Image
== NULL
|| ImageSize
== NULL
) {
345 return EFI_INVALID_PARAMETER
;
349 // Make sure the buffer is big enough to hold the device image
351 if (*ImageSize
< PcdGet32 (PcdBiosRomSize
)) {
352 *ImageSize
= PcdGet32 (PcdBiosRomSize
);
353 return EFI_BUFFER_TOO_SMALL
;
357 // Copy the device image to the buffer
359 *ImageSize
= PcdGet32 (PcdBiosRomSize
);
362 (VOID
*)(UINTN
)PcdGet32 (PcdBiosRomBase
),
370 Updates the firmware image of the device.
372 This function updates the hardware with the new firmware image. This function
373 returns EFI_UNSUPPORTED if the firmware image is not updatable. If the
374 firmware image is updatable, the function should perform the following minimal
375 validations before proceeding to do the firmware image update.
376 - Validate the image is a supported image for this device. The function
377 returns EFI_ABORTED if the image is unsupported. The function can
378 optionally provide more detailed information on why the image is not a
380 - Validate the data from VendorCode if not null. Image validation must be
381 performed before VendorCode data validation. VendorCode data is ignored
382 or considered invalid if image validation failed. The function returns
383 EFI_ABORTED if the data is invalid.
385 VendorCode enables vendor to implement vendor-specific firmware image update
386 policy. Null if the caller did not specify the policy or use the default
387 policy. As an example, vendor can implement a policy to allow an option to
388 force a firmware image update when the abort reason is due to the new firmware
389 image version is older than the current firmware image version or bad image
390 checksum. Sensitive operations such as those wiping the entire firmware image
391 and render the device to be non-functional should be encoded in the image
392 itself rather than passed with the VendorCode. AbortReason enables vendor to
393 have the option to provide a more detailed description of the abort reason to
396 @param[in] Image Points to the new image.
397 @param[in] ImageSize Size of the new image in bytes.
398 @param[in] VendorCode This enables vendor to implement vendor-specific
399 firmware image update policy. Null indicates the
400 caller did not specify the policy or use the
402 @param[in] Progress A function used by the driver to report the
403 progress of the firmware update.
404 @param[in] CapsuleFwVersion FMP Payload Header version of the image.
405 @param[out] AbortReason A pointer to a pointer to a null-terminated
406 string providing more details for the aborted
407 operation. The buffer is allocated by this
408 function with AllocatePool(), and it is the
409 caller's responsibility to free it with a call
412 @retval EFI_SUCCESS The device was successfully updated with the
414 @retval EFI_ABORTED The operation is aborted.
415 @retval EFI_INVALID_PARAMETER The Image was NULL.
416 @retval EFI_UNSUPPORTED The operation is not supported.
422 IN CONST VOID
*Image
,
424 IN CONST VOID
*VendorCode
,
425 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
,
426 IN UINT32 CapsuleFwVersion
,
427 OUT CHAR16
**AbortReason
434 UPDATE_CONFIG_DATA
*ConfigData
;
439 Status
= FmpDeviceCheckImage (Image
, ImageSize
, &Updateable
);
440 if (EFI_ERROR (Status
)) {
441 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Check Image failed with %r.\n", Status
));
445 if (Updateable
!= IMAGE_UPDATABLE_VALID
) {
446 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable
));
450 if (Progress
== NULL
) {
451 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Invalid progress callback\n"));
452 return EFI_INVALID_PARAMETER
;
455 Status
= Progress (15);
456 if (EFI_ERROR (Status
)) {
457 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status
));
461 // Write the image to the firmware device
464 if (EFI_ERROR (Status
)) {
465 DEBUG((DEBUG_ERROR
, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status
));
469 // Simulate update with delays between progress updates
471 for (Percentage
= 20; Percentage
<= 100; Percentage
++) {
475 // gBS->Stall (50000);
477 // Progress (Percentage);
478 // if (EFI_ERROR (Status)) {
479 // DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));
483 DEBUG ((DEBUG_INFO
, "FmpDeviceSetImage - %d Images ...\n", ARRAY_SIZE (mUpdateConfigData
)));
485 if (ARRAY_SIZE (mUpdateConfigData
) == 0) {
486 DEBUG((DEBUG_INFO
, "PlatformUpdate: BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n", 0, 0, ImageSize
));
487 Status
= PerformFlashWriteWithProgress (
488 PlatformFirmwareTypeSystemFirmware
, // FirmwareType
489 0x00000000, // FlashAddress
490 FlashAddressTypeRelativeAddress
, // FlashAddressType
491 (VOID
*)(UINTN
)Image
, // Buffer
492 ImageSize
, // BufferLength
493 Progress
, // Progress
494 20, // StartPercentage
501 // Compute total size of update
503 for (Index
= 0, TotalSize
= 0; Index
< ARRAY_SIZE (mUpdateConfigData
); Index
++) {
504 TotalSize
+= mUpdateConfigData
[Index
].Length
;
508 for (Index
= 0, ConfigData
= mUpdateConfigData
; Index
< ARRAY_SIZE (mUpdateConfigData
); Index
++, ConfigData
++) {
509 DEBUG((DEBUG_INFO
, "PlatformUpdate(%d): BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n",
511 ConfigData
->BaseAddress
,
512 ConfigData
->ImageOffset
,
515 Status
= PerformFlashWriteWithProgress (
516 ConfigData
->FirmwareType
, // FirmwareType
517 ConfigData
->BaseAddress
, // FlashAddress
518 ConfigData
->AddressType
, // FlashAddressType
519 (VOID
*)((UINTN
)Image
+ (UINTN
)ConfigData
->ImageOffset
), // Buffer
520 ConfigData
->Length
, // BufferLength
521 Progress
, // Progress
522 20 + (BytesWritten
* 80) / TotalSize
, // StartPercentage
523 20 + ((BytesWritten
+ ConfigData
->Length
) * 80) / TotalSize
// EndPercentage
525 if (EFI_ERROR(Status
)) {
528 BytesWritten
+= ConfigData
->Length
;
531 DEBUG ((DEBUG_INFO
, "FmpDeviceSetImage - %r\n", Status
));
537 Checks if the firmware image is valid for the device.
539 This function allows firmware update application to validate the firmware image without
540 invoking the SetImage() first.
542 @param[in] Image Points to the new image.
543 @param[in] ImageSize Size of the new image in bytes.
544 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
545 if available, additional information if the image is invalid.
547 @retval EFI_SUCCESS The image was successfully checked.
548 @retval EFI_INVALID_PARAMETER The Image was NULL.
553 FmpDeviceCheckImage (
554 IN CONST VOID
*Image
,
556 OUT UINT32
*ImageUpdateable
559 if (ImageUpdateable
== NULL
) {
560 DEBUG((DEBUG_ERROR
, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));
561 return EFI_INVALID_PARAMETER
;
565 //Set to valid and then if any tests fail it will update this flag.
567 *ImageUpdateable
= IMAGE_UPDATABLE_VALID
;
570 DEBUG((DEBUG_ERROR
, "CheckImage - Image Pointer Parameter is NULL.\n"));
572 // Not sure if this is needed
574 *ImageUpdateable
= IMAGE_UPDATABLE_INVALID
;
575 return EFI_INVALID_PARAMETER
;
579 // Make sure the image size is correct
581 if (ImageSize
!= PcdGet32 (PcdBiosRomSize
)) {
582 *ImageUpdateable
= IMAGE_UPDATABLE_INVALID
;
583 return EFI_INVALID_PARAMETER
;
590 Device firmware should trigger lock mechanism so that device fw can not be
591 updated or tampered with. This lock mechanism is generally only cleared by a
592 full system reset (not just sleep state/low power mode)
594 @retval EFI_SUCCESS The device was successfully locked.
595 @retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking
604 DEBUG ((DEBUG_INFO
, "VLV2: FmpDeviceLock() for system FLASH\n"));
605 // TODO: Add lock logic
606 return EFI_UNSUPPORTED
;