--- /dev/null
+/**\r
+\r
+Copyright (c) 2016, Microsoft Corporation\r
+\r
+All rights reserved.\r
+Redistribution and use in source and binary forms, with or without\r
+modification, are permitted provided that the following conditions are met:\r
+1. Redistributions of source code must retain the above copyright notice,\r
+this list of conditions and the following disclaimer.\r
+2. Redistributions in binary form must reproduce the above copyright notice,\r
+this list of conditions and the following disclaimer in the documentation\r
+ and/or other materials provided with the distribution.\r
+\r
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Library/FmpDeviceLib.h>\r
+\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include <Library/PlatformFlashAccessLib.h>\r
+\r
+//#include <Protocol/FirmwareManagement.h>\r
+\r
+//#include <Guid/SystemResourceTable.h>\r
+\r
+typedef struct {\r
+ PLATFORM_FIRMWARE_TYPE FirmwareType;\r
+ FLASH_ADDRESS_TYPE AddressType;\r
+ EFI_PHYSICAL_ADDRESS BaseAddress;\r
+ UINTN Length;\r
+ UINTN ImageOffset;\r
+} UPDATE_CONFIG_DATA;\r
+\r
+UPDATE_CONFIG_DATA mUpdateConfigData[] = {\r
+ { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00000000, 0x00040000, 0x00000000 },\r
+ { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x000C0000, 0x00050000, 0x000C0000 },\r
+ { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00110000, 0x00210000, 0x00110000 },\r
+ { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00320000, 0x00070000, 0x00320000 },\r
+ { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00390000, 0x00070000, 0x00390000 },\r
+ { PlatformFirmwareTypeNvRam, FlashAddressTypeRelativeAddress, 0x00040000, 0x00080000, 0x00040000 }\r
+};\r
+\r
+/**\r
+ Used to pass the FMP install function to this lib. This allows the library to\r
+ have control of the handle that the FMP instance is installed on. This allows\r
+ the library to use DriverBinding protocol model to locate its device(s) in the\r
+ system.\r
+\r
+ @param[in] Func Function pointer to FMP install function.\r
+\r
+ @retval EFI_SUCCESS Library has saved function pointer and will call\r
+ function pointer on each DriverBinding Start.\r
+ @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports\r
+ a single instance.\r
+ @retval other error Error occurred. Don't install FMP\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RegisterFmpInstaller (\r
+ IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func\r
+ )\r
+{\r
+ //\r
+ // This is a system firmware update that does not use Driver Binding Protocol\r
+ //\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+\r
+/**\r
+ Returns the size, in bytes, of the firmware image currently stored in the\r
+ firmware device. This function is used to by the GetImage() and\r
+ GetImageInfo() services of the Firmware Management Protocol. If the image\r
+ size can not be determined from the firmware device, then 0 must be returned.\r
+\r
+ @param[out] Size Pointer to the size, in bytes, of the firmware image\r
+ currently stored in the firmware device.\r
+\r
+ @retval EFI_SUCCESS The size of the firmware image currently\r
+ stored in the firmware device was returned.\r
+ @retval EFI_INVALID_PARAMETER Size is NULL.\r
+ @retval EFI_UNSUPPORTED The firmware device does not support reporting\r
+ the size of the currently stored firmware image.\r
+ @retval EFI_DEVICE_ERROR An error occured attempting to determine the\r
+ size of the firmware image currently stored in\r
+ in the firmware device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetSize (\r
+ IN UINTN *Size\r
+ )\r
+{\r
+ if (Size == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *Size = PcdGet32 (PcdBiosRomBase);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Used to return a library supplied guid that will be the ImageTypeId guid of\r
+ the FMP descriptor. This is optional but can be used if at runtime the guid\r
+ needs to be determined.\r
+\r
+ @param[out] Guid Double Guid Ptr that will be updated to point to guid.\r
+ This should be from static memory and will not be freed.\r
+\r
+ @return EFI_UNSUPPORTED Library instance doesn't need dynamic guid.\r
+ @return Error Any error will cause the wrapper to use the GUID\r
+ defined by PCD.\r
+ @return EFI_SUCCESS Guid ptr should be updated to point to static memeory\r
+ which contains a valid guid.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetImageTypeIdGuidPtr (\r
+ OUT EFI_GUID **Guid\r
+ )\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+ Returns values used to fill in the AttributesSupported and AttributesSettings\r
+ fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the\r
+ GetImageInfo() service of the Firmware Management Protocol. The following\r
+ bit values from the Firmware Management Protocol may be combined:\r
+ IMAGE_ATTRIBUTE_IMAGE_UPDATABLE\r
+ IMAGE_ATTRIBUTE_RESET_REQUIRED\r
+ IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED\r
+ IMAGE_ATTRIBUTE_IN_USE\r
+ IMAGE_ATTRIBUTE_UEFI_IMAGE\r
+\r
+ @param[out] Supported Attributes supported by this firmware device.\r
+ @param[out] Setting Attributes settings for this firmware device.\r
+\r
+ @retval EFI_SUCCESS The attributes supported by the firmware\r
+ device were returned.\r
+ @retval EFI_INVALID_PARAMETER Supported is NULL.\r
+ @retval EFI_INVALID_PARAMETER Setting is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetAttributes (\r
+ IN OUT UINT64 *Supported,\r
+ IN OUT UINT64 *Setting\r
+ )\r
+{\r
+ if (Supported == NULL || Setting == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *Supported = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |\r
+ IMAGE_ATTRIBUTE_RESET_REQUIRED |\r
+ IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |\r
+ IMAGE_ATTRIBUTE_IN_USE\r
+ );\r
+ *Setting = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |\r
+ IMAGE_ATTRIBUTE_RESET_REQUIRED |\r
+ IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |\r
+ IMAGE_ATTRIBUTE_IN_USE\r
+ );\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Gets the current Lowest Supported Version.\r
+\r
+ This is a protection mechanism so that a previous version with known issue is\r
+ not applied. ONLY implement this if your running firmware has a method to\r
+ return this at runtime. If EFI_UNSUPPORTED is returned, then the Lowest\r
+ Supported Version is stored in a UEFI Variable.\r
+\r
+ @param[out] Version On return this value represents the current Lowest\r
+ Supported Version (in same format as GetVersion).\r
+\r
+ @retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved\r
+ @retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV\r
+ @retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetLowestSupportedVersion (\r
+ IN OUT UINT32 *LowestSupportedVersion\r
+ )\r
+{\r
+ //\r
+ // Retrieve the lowest support version from a PCD\r
+ // NOTE: This method of using a PCD can only be used for the system firmware\r
+ // FMP instance that is updated every time the system firmware is\r
+ // updated. If system firmware updates support partial updates that\r
+ // would not include the system firmware FMP instance, then a PCD can\r
+ // not be used and the value must come from the currently running system\r
+ // firmware image.\r
+ //\r
+ *LowestSupportedVersion = PcdGet32 (PcdSystemFirmwareFmpLowestSupportedVersion);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Returns the Null-terminated Unicode string that is used to fill in the\r
+ VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is\r
+ returned by the GetImageInfo() service of the Firmware Management Protocol.\r
+ The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().\r
+\r
+ @note It is recommended that all firmware devices support a method to report\r
+ the VersionName string from the currently stored firmware image.\r
+\r
+ @param[out] VersionString The version string retrieved from the currently\r
+ stored firmware image.\r
+\r
+ @retval EFI_SUCCESS The version string of currently stored\r
+ firmware image was returned in Version.\r
+ @retval EFI_INVALID_PARAMETER VersionString is NULL.\r
+ @retval EFI_UNSUPPORTED The firmware device does not support a method\r
+ to report the version string of the currently\r
+ stored firmware image.\r
+ @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the\r
+ version string of the currently stored\r
+ firmware image.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the\r
+ buffer for the version string of the currently\r
+ stored firmware image.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetVersionString (\r
+ OUT CHAR16 **VersionString\r
+ )\r
+{\r
+ if (VersionString == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Retrieve the version string from a PCD\r
+ // NOTE: This method of using a PCD can only be used for the system firmware\r
+ // FMP instance that is updated every time the system firmware is\r
+ // updated. If system firmware updates support partial updates that\r
+ // would not include the system firmware FMP instance, then a PCD can\r
+ // not be used and the value must come from the currently running system\r
+ // firmware image.\r
+ //\r
+ *VersionString = (CHAR16 *)AllocateCopyPool (\r
+ PcdGetSize (PcdSystemFirmwareFmpVersionString),\r
+ PcdGetPtr (PcdSystemFirmwareFmpVersionString)\r
+ );\r
+ if (*VersionString == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Gets the current running version.\r
+\r
+ ONLY implement this if your running firmware has a method to return this at\r
+ runtime.\r
+\r
+ @param[out] Version On return this value represents the current running\r
+ version.\r
+\r
+ @retval EFI_SUCCESS The version was correctly retrieved.\r
+ @retval EFI_UNSUPPORTED Device firmware doesn't support reporting current\r
+ version.\r
+ @retval EFI_DEVICE_ERROR Error occurred when trying to get the version.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetVersion (\r
+ IN OUT UINT32 *Version\r
+ )\r
+{\r
+ //\r
+ // Retrieve the version string from a PCD\r
+ // NOTE: This method of using a PCD can only be used for the system firmware\r
+ // FMP instance that is updated every time the system firmware is\r
+ // updated. If system firmware updates support partial updates that\r
+ // would not include the system firmware FMP instance, then a PCD can\r
+ // not be used and the value must come from the currently running system\r
+ // firmware image.\r
+ //\r
+ *Version = PcdGet32 (PcdSystemFirmwareFmpVersion);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Retrieves a copy of the current firmware image of the device.\r
+\r
+ This function allows a copy of the current firmware image to be created and\r
+ saved. The saved copy could later been used, for example, in firmware image\r
+ recovery or rollback.\r
+\r
+ @param[out] Image Points to the buffer where the current image is copied\r
+ to.\r
+ @param[out] ImageSize On entry, points to the size of the buffer pointed to\r
+ by Image, in bytes. On return, points to the length of\r
+ the image, in bytes.\r
+\r
+ @retval EFI_SUCCESS The image was successfully read from the device.\r
+ @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small\r
+ to hold the image. The current buffer size\r
+ needed to hold the image is returned in\r
+ ImageSize.\r
+ @retval EFI_INVALID_PARAMETER The Image was NULL.\r
+ @retval EFI_NOT_FOUND The current image is not copied to the buffer.\r
+ @retval EFI_UNSUPPORTED The operation is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetImage (\r
+ IN OUT VOID *Image,\r
+ IN OUT UINTN *ImageSize\r
+ )\r
+{\r
+ //\r
+ // Check for invalid p;arameters\r
+ //\r
+ if (Image == NULL || ImageSize == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Make sure the buffer is big enough to hold the device image\r
+ //\r
+ if (*ImageSize < PcdGet32 (PcdBiosRomSize)) {\r
+ *ImageSize = PcdGet32 (PcdBiosRomSize);\r
+ return EFI_BUFFER_TOO_SMALL;\r
+ }\r
+\r
+ //\r
+ // Copy the device image to the buffer\r
+ //\r
+ *ImageSize = PcdGet32 (PcdBiosRomSize);\r
+ CopyMem (\r
+ Image,\r
+ (VOID *)(UINTN)PcdGet32 (PcdBiosRomBase),\r
+ *ImageSize\r
+ );\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Updates the firmware image of the device.\r
+\r
+ This function updates the hardware with the new firmware image. This function\r
+ returns EFI_UNSUPPORTED if the firmware image is not updatable. If the\r
+ firmware image is updatable, the function should perform the following minimal\r
+ validations before proceeding to do the firmware image update.\r
+ - Validate the image is a supported image for this device. The function\r
+ returns EFI_ABORTED if the image is unsupported. The function can\r
+ optionally provide more detailed information on why the image is not a\r
+ supported image.\r
+ - Validate the data from VendorCode if not null. Image validation must be\r
+ performed before VendorCode data validation. VendorCode data is ignored\r
+ or considered invalid if image validation failed. The function returns\r
+ EFI_ABORTED if the data is invalid.\r
+\r
+ VendorCode enables vendor to implement vendor-specific firmware image update\r
+ policy. Null if the caller did not specify the policy or use the default\r
+ policy. As an example, vendor can implement a policy to allow an option to\r
+ force a firmware image update when the abort reason is due to the new firmware\r
+ image version is older than the current firmware image version or bad image\r
+ checksum. Sensitive operations such as those wiping the entire firmware image\r
+ and render the device to be non-functional should be encoded in the image\r
+ itself rather than passed with the VendorCode. AbortReason enables vendor to\r
+ have the option to provide a more detailed description of the abort reason to\r
+ the caller.\r
+\r
+ @param[in] Image Points to the new image.\r
+ @param[in] ImageSize Size of the new image in bytes.\r
+ @param[in] VendorCode This enables vendor to implement vendor-specific\r
+ firmware image update policy. Null indicates the\r
+ caller did not specify the policy or use the\r
+ default policy.\r
+ @param[in] Progress A function used by the driver to report the\r
+ progress of the firmware update.\r
+ @param[in] CapsuleFwVersion FMP Payload Header version of the image.\r
+ @param[out] AbortReason A pointer to a pointer to a null-terminated\r
+ string providing more details for the aborted\r
+ operation. The buffer is allocated by this\r
+ function with AllocatePool(), and it is the\r
+ caller's responsibility to free it with a call\r
+ to FreePool().\r
+\r
+ @retval EFI_SUCCESS The device was successfully updated with the\r
+ new image.\r
+ @retval EFI_ABORTED The operation is aborted.\r
+ @retval EFI_INVALID_PARAMETER The Image was NULL.\r
+ @retval EFI_UNSUPPORTED The operation is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceSetImage (\r
+ IN CONST VOID *Image,\r
+ IN UINTN ImageSize,\r
+ IN CONST VOID *VendorCode,\r
+ IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
+ IN UINT32 CapsuleFwVersion,\r
+ OUT CHAR16 **AbortReason\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT32 Updateable;\r
+ UINTN Percentage;\r
+ UINTN Index;\r
+ UPDATE_CONFIG_DATA *ConfigData;\r
+ UINTN TotalSize;\r
+ UINTN BytesWritten;\r
+\r
+ Updateable = 0;\r
+ Status = FmpDeviceCheckImage (Image, ImageSize, &Updateable);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Check Image failed with %r.\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ if (Updateable != IMAGE_UPDATABLE_VALID) {\r
+ DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable));\r
+ return EFI_ABORTED;\r
+ }\r
+\r
+ if (Progress == NULL) {\r
+ DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Invalid progress callback\n"));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Status = Progress (15);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));\r
+ }\r
+\r
+ //\r
+ // Write the image to the firmware device\r
+ //\r
+ Progress (20);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));\r
+ }\r
+\r
+ //\r
+ // Simulate update with delays between progress updates\r
+ //\r
+ for (Percentage = 20; Percentage <= 100; Percentage++) {\r
+ //\r
+ // Wait 0.05 seconds\r
+ //\r
+// gBS->Stall (50000);\r
+\r
+// Progress (Percentage);\r
+// if (EFI_ERROR (Status)) {\r
+// DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));\r
+// }\r
+ }\r
+\r
+ DEBUG ((DEBUG_INFO, "FmpDeviceSetImage - %d Images ...\n", ARRAY_SIZE (mUpdateConfigData)));\r
+\r
+ if (ARRAY_SIZE (mUpdateConfigData) == 0) {\r
+ DEBUG((DEBUG_INFO, "PlatformUpdate: BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n", 0, 0, ImageSize));\r
+ Status = PerformFlashWriteWithProgress (\r
+ PlatformFirmwareTypeSystemFirmware, // FirmwareType\r
+ 0x00000000, // FlashAddress\r
+ FlashAddressTypeRelativeAddress, // FlashAddressType\r
+ (VOID *)(UINTN)Image, // Buffer\r
+ ImageSize, // BufferLength\r
+ Progress, // Progress\r
+ 20, // StartPercentage\r
+ 100 // EndPercentage\r
+ );\r
+ }\r
+\r
+\r
+ //\r
+ // Compute total size of update\r
+ //\r
+ for (Index = 0, TotalSize = 0; Index < ARRAY_SIZE (mUpdateConfigData); Index++) {\r
+ TotalSize += mUpdateConfigData[Index].Length;\r
+ }\r
+\r
+ BytesWritten = 0;\r
+ for (Index = 0, ConfigData = mUpdateConfigData; Index < ARRAY_SIZE (mUpdateConfigData); Index++, ConfigData++) {\r
+ DEBUG((DEBUG_INFO, "PlatformUpdate(%d): BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n",\r
+ Index,\r
+ ConfigData->BaseAddress,\r
+ ConfigData->ImageOffset,\r
+ ConfigData->Length\r
+ ));\r
+ Status = PerformFlashWriteWithProgress (\r
+ ConfigData->FirmwareType, // FirmwareType\r
+ ConfigData->BaseAddress, // FlashAddress\r
+ ConfigData->AddressType, // FlashAddressType\r
+ (VOID *)((UINTN)Image + (UINTN)ConfigData->ImageOffset), // Buffer\r
+ ConfigData->Length, // BufferLength\r
+ Progress, // Progress\r
+ 20 + (BytesWritten * 80) / TotalSize, // StartPercentage\r
+ 20 + ((BytesWritten + ConfigData->Length) * 80) / TotalSize // EndPercentage\r
+ );\r
+ if (EFI_ERROR(Status)) {\r
+ break;\r
+ }\r
+ BytesWritten += ConfigData->Length;\r
+ }\r
+\r
+ DEBUG ((DEBUG_INFO, "FmpDeviceSetImage - %r\n", Status));\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+Checks if the firmware image is valid for the device.\r
+\r
+This function allows firmware update application to validate the firmware image without\r
+invoking the SetImage() first.\r
+\r
+@param[in] Image Points to the new image.\r
+@param[in] ImageSize Size of the new image in bytes.\r
+@param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
+if available, additional information if the image is invalid.\r
+\r
+@retval EFI_SUCCESS The image was successfully checked.\r
+@retval EFI_INVALID_PARAMETER The Image was NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceCheckImage (\r
+ IN CONST VOID *Image,\r
+ IN UINTN ImageSize,\r
+ OUT UINT32 *ImageUpdateable\r
+ )\r
+{\r
+ if (ImageUpdateable == NULL) {\r
+ DEBUG((DEBUG_ERROR, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ //Set to valid and then if any tests fail it will update this flag.\r
+ //\r
+ *ImageUpdateable = IMAGE_UPDATABLE_VALID;\r
+\r
+ if (Image == NULL) {\r
+ DEBUG((DEBUG_ERROR, "CheckImage - Image Pointer Parameter is NULL.\n"));\r
+ //\r
+ // Not sure if this is needed\r
+ //\r
+ *ImageUpdateable = IMAGE_UPDATABLE_INVALID;\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Make sure the image size is correct\r
+ //\r
+ if (ImageSize != PcdGet32 (PcdBiosRomSize)) {\r
+ *ImageUpdateable = IMAGE_UPDATABLE_INVALID;\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Device firmware should trigger lock mechanism so that device fw can not be\r
+ updated or tampered with. This lock mechanism is generally only cleared by a\r
+ full system reset (not just sleep state/low power mode)\r
+\r
+ @retval EFI_SUCCESS The device was successfully locked.\r
+ @retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceLock (\r
+ VOID\r
+ )\r
+{\r
+ DEBUG ((DEBUG_INFO, "VLV2: FmpDeviceLock() for system FLASH\n"));\r
+ // TODO: Add lock logic\r
+ return EFI_UNSUPPORTED;\r
+}\r
--- /dev/null
+/**\r
+\r
+Copyright (c) 2016, Microsoft Corporation\r
+\r
+All rights reserved.\r
+Redistribution and use in source and binary forms, with or without\r
+modification, are permitted provided that the following conditions are met:\r
+1. Redistributions of source code must retain the above copyright notice,\r
+this list of conditions and the following disclaimer.\r
+2. Redistributions in binary form must reproduce the above copyright notice,\r
+this list of conditions and the following disclaimer in the documentation\r
+ and/or other materials provided with the distribution.\r
+\r
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+\r
+#include <PiDxe.h>\r
+#include <Library/DebugLib.h>\r
+#include <Protocol/FirmwareManagement.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/FmpDeviceLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+/**\r
+ Used to pass the FMP install function to this lib.\r
+ This allows the library to have control of the handle\r
+ that the FMP instance is installed on. This allows the library\r
+ to use DriverBinding protocol model to locate its device(s) in the\r
+ system.\r
+\r
+ @param[in] Function pointer to FMP install function.\r
+\r
+ @retval EFI_SUCCESS Library has saved function pointer and will call function pointer on each DriverBinding Start.\r
+ @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports a single instance.\r
+ @retval other error Error occurred. Don't install FMP\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RegisterFmpInstaller(\r
+IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func\r
+)\r
+{\r
+ // Because this is a sample lib with very simple fake device we don't use\r
+ // the driverbinding protocol to locate our device.\r
+ //\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+\r
+/**\r
+Used to get the size of the image in bytes.\r
+NOTE - Do not return zero as that will identify the device as\r
+not updatable.\r
+\r
+@retval UINTN that represents the size of the firmware.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetSize (\r
+ IN UINTN *Size\r
+ )\r
+{\r
+ if (Size == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *Size = 0x1000;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+Used to return a library supplied guid that will be the ImageTypeId guid of the FMP descriptor.\r
+This is optional but can be used if at runtime the guid needs to be determined.\r
+\r
+@param Guid: Double Guid Ptr that will be updated to point to guid. This should be from static memory\r
+and will not be freed.\r
+@return EFI_UNSUPPORTED: if you library instance doesn't need dynamic guid return this.\r
+@return Error: Any error will cause the wrapper to use the GUID defined by PCD\r
+@return EFI_SUCCESS: Guid ptr should be updated to point to static memeory which contains a valid guid\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetImageTypeIdGuidPtr(\r
+ OUT EFI_GUID** Guid)\r
+{\r
+ //this instance doesn't need dynamic guid detection.\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+ Returns values used to fill in the AttributesSupported and AttributesSettings\r
+ fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the\r
+ GetImageInfo() service of the Firmware Management Protocol. The following\r
+ bit values from the Firmware Management Protocol may be combined:\r
+ IMAGE_ATTRIBUTE_IMAGE_UPDATABLE\r
+ IMAGE_ATTRIBUTE_RESET_REQUIRED\r
+ IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED\r
+ IMAGE_ATTRIBUTE_IN_USE\r
+ IMAGE_ATTRIBUTE_UEFI_IMAGE\r
+\r
+ @param[out] Supported Attributes supported by this firmware device.\r
+ @param[out] Setting Attributes settings for this firmware device.\r
+\r
+ @retval EFI_SUCCESS The attributes supported by the firmware\r
+ device were returned.\r
+ @retval EFI_INVALID_PARAMETER Supported is NULL.\r
+ @retval EFI_INVALID_PARAMETER Setting is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetAttributes (\r
+ IN OUT UINT64 *Supported,\r
+ IN OUT UINT64 *Setting\r
+ )\r
+{\r
+ if (Supported == NULL || Setting == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *Supported = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);\r
+ *Setting = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+Gets the current Lowest Supported Version.\r
+This is a protection mechanism so that a previous version with known issue is not\r
+applied.\r
+\r
+ONLY implement this if your running firmware has a method to return this at runtime.\r
+\r
+@param[out] Version On return this value represents the\r
+current Lowest Supported Version (in same format as GetVersion).\r
+\r
+@retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved\r
+@retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV\r
+@retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetLowestSupportedVersion (\r
+ IN OUT UINT32* LowestSupportedVersion\r
+ )\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+\r
+/**\r
+ Returns the Null-terminated Unicode string that is used to fill in the\r
+ VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is\r
+ returned by the GetImageInfo() service of the Firmware Management Protocol.\r
+ The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().\r
+\r
+ @note It is recommended that all firmware devices support a method to report\r
+ the VersionName string from the currently stored firmware image.\r
+\r
+ @param[out] VersionString The version string retrieved from the currently\r
+ stored firmware image.\r
+\r
+ @retval EFI_SUCCESS The version string of currently stored\r
+ firmware image was returned in Version.\r
+ @retval EFI_INVALID_PARAMETER VersionString is NULL.\r
+ @retval EFI_UNSUPPORTED The firmware device does not support a method\r
+ to report the version string of the currently\r
+ stored firmware image.\r
+ @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the\r
+ version string of the currently stored\r
+ firmware image.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the\r
+ buffer for the version string of the currently\r
+ stored firmware image.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetVersionString (\r
+ OUT CHAR16 **VersionString\r
+ )\r
+{\r
+ if (VersionString == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *VersionString = NULL;\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+Gets the current running version.\r
+ONLY implement this if your running firmware has a method to return this at runtime.\r
+\r
+@param[out] Version On return this value represents the current running version\r
+\r
+@retval EFI_SUCCESS The version was correctly retrieved\r
+@retval EFI_UNSUPPORTED Device firmware doesn't support reporting current version\r
+@retval EFI_DEVICE_ERROR Error occurred when trying to get the version\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetVersion(\r
+IN OUT UINT32* Version\r
+)\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+\r
+/**\r
+Retrieves a copy of the current firmware image of the device.\r
+\r
+This function allows a copy of the current firmware image to be created and saved.\r
+The saved copy could later been used, for example, in firmware image recovery or rollback.\r
+\r
+@param[out] Image Points to the buffer where the current image is copied to.\r
+@param[out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.\r
+On return, points to the length of the image, in bytes.\r
+\r
+@retval EFI_SUCCESS The device was successfully updated with the new image.\r
+@retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the\r
+image. The current buffer size needed to hold the image is returned\r
+in ImageSize.\r
+@retval EFI_INVALID_PARAMETER The Image was NULL.\r
+@retval EFI_NOT_FOUND The current image is not copied to the buffer.\r
+@retval EFI_UNSUPPORTED The operation is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceGetImage(\r
+IN OUT VOID *Image,\r
+IN OUT UINTN *ImageSize\r
+)\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ This is a function used to read the current firmware from the device into memory.\r
+ This is an optional function and can return EFI_UNSUPPORTED. This is useful for\r
+ test and diagnostics.\r
+\r
+Arguments:\r
+ Image -- Buffer to place the image into.\r
+ ImageSize -- Size of the Image buffer.\r
+\r
+Return Value:\r
+\r
+ EFI_STATUS code.\r
+ If not possible or not practical return EFI_UNSUPPORTED.\r
+\r
+--*/\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}//GetImage()\r
+\r
+\r
+/**\r
+Updates the firmware image of the device.\r
+\r
+This function updates the hardware with the new firmware image.\r
+This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
+If the firmware image is updatable, the function should perform the following minimal validations\r
+before proceeding to do the firmware image update.\r
+- Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
+the image is unsupported. The function can optionally provide more detailed information on\r
+why the image is not a supported image.\r
+- Validate the data from VendorCode if not null. Image validation must be performed before\r
+VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
+validation failed. The function returns EFI_ABORTED if the data is invalid.\r
+\r
+VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
+the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
+a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
+firmware image version is older than the current firmware image version or bad image checksum.\r
+Sensitive operations such as those wiping the entire firmware image and render the device to be\r
+non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
+AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
+reason to the caller.\r
+\r
+@param[in] Image Points to the new image.\r
+@param[in] ImageSize Size of the new image in bytes.\r
+@param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
+Null indicates the caller did not specify the policy or use the default policy.\r
+@param[in] Progress A function used by the driver to report the progress of the firmware update.\r
+@param[in] CapsuleFwVersion FMP Payload Header version of the image\r
+@param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
+details for the aborted operation. The buffer is allocated by this function\r
+with AllocatePool(), and it is the caller's responsibility to free it with a\r
+call to FreePool().\r
+\r
+@retval EFI_SUCCESS The device was successfully updated with the new image.\r
+@retval EFI_ABORTED The operation is aborted.\r
+@retval EFI_INVALID_PARAMETER The Image was NULL.\r
+@retval EFI_UNSUPPORTED The operation is not supported.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceSetImage (\r
+IN CONST VOID *Image,\r
+IN UINTN ImageSize,\r
+IN CONST VOID *VendorCode,\r
+IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
+IN UINT32 CapsuleFwVersion,\r
+OUT CHAR16 **AbortReason\r
+)\r
+{\r
+ EFI_STATUS Status = EFI_SUCCESS;\r
+ UINT32 Updateable = 0;\r
+\r
+ Status = FmpDeviceCheckImage(Image, ImageSize, &Updateable);\r
+ if (EFI_ERROR(Status))\r
+ {\r
+ DEBUG((DEBUG_ERROR, "SetImage - Check Image failed with %r.\n", Status));\r
+ goto cleanup;\r
+ }\r
+\r
+ if (Updateable != IMAGE_UPDATABLE_VALID)\r
+ {\r
+ DEBUG((DEBUG_ERROR, "SetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable));\r
+ Status = EFI_ABORTED;\r
+ goto cleanup;\r
+ }\r
+\r
+ if (Progress == NULL)\r
+ {\r
+ DEBUG((DEBUG_ERROR, "SetImage - Invalid progress callback\n"));\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto cleanup;\r
+ }\r
+\r
+ Status = Progress(15);\r
+ if (EFI_ERROR(Status))\r
+ {\r
+ DEBUG((DEBUG_ERROR, "SetImage - Progress Callback failed with Status %r.\n", Status));\r
+ }\r
+\r
+ {\r
+ UINTN p;\r
+\r
+ for (p = 20; p < 100; p++) {\r
+ gBS->Stall (100000); //us = 0.1 seconds\r
+ Progress (p);\r
+ }\r
+ }\r
+\r
+ //TODO: add support for VendorCode, and AbortReason\r
+cleanup:\r
+ return Status;\r
+}// SetImage()\r
+\r
+\r
+\r
+/**\r
+Checks if the firmware image is valid for the device.\r
+\r
+This function allows firmware update application to validate the firmware image without\r
+invoking the SetImage() first.\r
+\r
+@param[in] Image Points to the new image.\r
+@param[in] ImageSize Size of the new image in bytes.\r
+@param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
+if available, additional information if the image is invalid.\r
+\r
+@retval EFI_SUCCESS The image was successfully checked.\r
+@retval EFI_INVALID_PARAMETER The Image was NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceCheckImage(\r
+IN CONST VOID *Image,\r
+IN UINTN ImageSize,\r
+OUT UINT32 *ImageUpdateable\r
+)\r
+{\r
+ EFI_STATUS status = EFI_SUCCESS;\r
+\r
+ if (ImageUpdateable == NULL)\r
+ {\r
+ DEBUG((DEBUG_ERROR, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));\r
+ status = EFI_INVALID_PARAMETER;\r
+ goto cleanup;\r
+ }\r
+\r
+ //\r
+ //Set to valid and then if any tests fail it will update this flag.\r
+ //\r
+ *ImageUpdateable = IMAGE_UPDATABLE_VALID;\r
+\r
+ if (Image == NULL)\r
+ {\r
+ DEBUG((DEBUG_ERROR, "CheckImage - Image Pointer Parameter is NULL.\n"));\r
+ *ImageUpdateable = IMAGE_UPDATABLE_INVALID; //not sure if this is needed\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+cleanup:\r
+ return status;\r
+}// CheckImage()\r
+\r
+/**\r
+Device firmware should trigger lock mechanism so that device fw can not be updated or tampered with.\r
+This lock mechanism is generally only cleared by a full system reset (not just sleep state/low power mode)\r
+\r
+@retval EFI_SUCCESS The device was successfully locked.\r
+@retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FmpDeviceLock(\r
+)\r
+{\r
+ return EFI_SUCCESS;\r
+}\r