FmpDeviceLib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. Copyright (c) 2016, Microsoft Corporation
  3. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/DebugLib.h>
  8. #include <Protocol/FirmwareManagement.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/MemoryAllocationLib.h>
  11. #include <Library/BaseMemoryLib.h>
  12. #include <Library/FmpDeviceLib.h>
  13. #include <Library/UefiBootServicesTableLib.h>
  14. /**
  15. Used to pass the FMP install function to this lib.
  16. This allows the library to have control of the handle
  17. that the FMP instance is installed on. This allows the library
  18. to use DriverBinding protocol model to locate its device(s) in the
  19. system.
  20. @param[in] Function pointer to FMP install function.
  21. @retval EFI_SUCCESS Library has saved function pointer and will call function pointer on each DriverBinding Start.
  22. @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports a single instance.
  23. @retval other error Error occurred. Don't install FMP
  24. **/
  25. EFI_STATUS
  26. EFIAPI
  27. RegisterFmpInstaller(
  28. IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func
  29. )
  30. {
  31. // Because this is a sample lib with very simple fake device we don't use
  32. // the driverbinding protocol to locate our device.
  33. //
  34. return EFI_UNSUPPORTED;
  35. }
  36. /**
  37. Used to get the size of the image in bytes.
  38. NOTE - Do not return zero as that will identify the device as
  39. not updatable.
  40. @retval UINTN that represents the size of the firmware.
  41. **/
  42. EFI_STATUS
  43. EFIAPI
  44. FmpDeviceGetSize (
  45. IN UINTN *Size
  46. )
  47. {
  48. if (Size == NULL) {
  49. return EFI_INVALID_PARAMETER;
  50. }
  51. *Size = 0x1000;
  52. return EFI_SUCCESS;
  53. }
  54. /**
  55. Used to return a library supplied guid that will be the ImageTypeId guid of the FMP descriptor.
  56. This is optional but can be used if at runtime the guid needs to be determined.
  57. @param Guid: Double Guid Ptr that will be updated to point to guid. This should be from static memory
  58. and will not be freed.
  59. @return EFI_UNSUPPORTED: if you library instance doesn't need dynamic guid return this.
  60. @return Error: Any error will cause the wrapper to use the GUID defined by PCD
  61. @return EFI_SUCCESS: Guid ptr should be updated to point to static memeory which contains a valid guid
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. FmpDeviceGetImageTypeIdGuidPtr(
  66. OUT EFI_GUID** Guid)
  67. {
  68. //this instance doesn't need dynamic guid detection.
  69. return EFI_UNSUPPORTED;
  70. }
  71. /**
  72. Returns values used to fill in the AttributesSupported and AttributesSettings
  73. fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the
  74. GetImageInfo() service of the Firmware Management Protocol. The following
  75. bit values from the Firmware Management Protocol may be combined:
  76. IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
  77. IMAGE_ATTRIBUTE_RESET_REQUIRED
  78. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
  79. IMAGE_ATTRIBUTE_IN_USE
  80. IMAGE_ATTRIBUTE_UEFI_IMAGE
  81. @param[out] Supported Attributes supported by this firmware device.
  82. @param[out] Setting Attributes settings for this firmware device.
  83. @retval EFI_SUCCESS The attributes supported by the firmware
  84. device were returned.
  85. @retval EFI_INVALID_PARAMETER Supported is NULL.
  86. @retval EFI_INVALID_PARAMETER Setting is NULL.
  87. **/
  88. EFI_STATUS
  89. EFIAPI
  90. FmpDeviceGetAttributes (
  91. IN OUT UINT64 *Supported,
  92. IN OUT UINT64 *Setting
  93. )
  94. {
  95. if (Supported == NULL || Setting == NULL) {
  96. return EFI_INVALID_PARAMETER;
  97. }
  98. *Supported = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);
  99. *Setting = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);
  100. return EFI_SUCCESS;
  101. }
  102. /**
  103. Gets the current Lowest Supported Version.
  104. This is a protection mechanism so that a previous version with known issue is not
  105. applied.
  106. ONLY implement this if your running firmware has a method to return this at runtime.
  107. @param[out] Version On return this value represents the
  108. current Lowest Supported Version (in same format as GetVersion).
  109. @retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved
  110. @retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV
  111. @retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV
  112. **/
  113. EFI_STATUS
  114. EFIAPI
  115. FmpDeviceGetLowestSupportedVersion (
  116. IN OUT UINT32* LowestSupportedVersion
  117. )
  118. {
  119. return EFI_UNSUPPORTED;
  120. }
  121. /**
  122. Returns the Null-terminated Unicode string that is used to fill in the
  123. VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is
  124. returned by the GetImageInfo() service of the Firmware Management Protocol.
  125. The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().
  126. @note It is recommended that all firmware devices support a method to report
  127. the VersionName string from the currently stored firmware image.
  128. @param[out] VersionString The version string retrieved from the currently
  129. stored firmware image.
  130. @retval EFI_SUCCESS The version string of currently stored
  131. firmware image was returned in Version.
  132. @retval EFI_INVALID_PARAMETER VersionString is NULL.
  133. @retval EFI_UNSUPPORTED The firmware device does not support a method
  134. to report the version string of the currently
  135. stored firmware image.
  136. @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the
  137. version string of the currently stored
  138. firmware image.
  139. @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
  140. buffer for the version string of the currently
  141. stored firmware image.
  142. **/
  143. EFI_STATUS
  144. EFIAPI
  145. FmpDeviceGetVersionString (
  146. OUT CHAR16 **VersionString
  147. )
  148. {
  149. if (VersionString == NULL) {
  150. return EFI_INVALID_PARAMETER;
  151. }
  152. *VersionString = NULL;
  153. return EFI_UNSUPPORTED;
  154. }
  155. /**
  156. Gets the current running version.
  157. ONLY implement this if your running firmware has a method to return this at runtime.
  158. @param[out] Version On return this value represents the current running version
  159. @retval EFI_SUCCESS The version was correctly retrieved
  160. @retval EFI_UNSUPPORTED Device firmware doesn't support reporting current version
  161. @retval EFI_DEVICE_ERROR Error occurred when trying to get the version
  162. **/
  163. EFI_STATUS
  164. EFIAPI
  165. FmpDeviceGetVersion(
  166. IN OUT UINT32* Version
  167. )
  168. {
  169. return EFI_UNSUPPORTED;
  170. }
  171. /**
  172. Retrieves a copy of the current firmware image of the device.
  173. This function allows a copy of the current firmware image to be created and saved.
  174. The saved copy could later been used, for example, in firmware image recovery or rollback.
  175. @param[out] Image Points to the buffer where the current image is copied to.
  176. @param[out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
  177. On return, points to the length of the image, in bytes.
  178. @retval EFI_SUCCESS The device was successfully updated with the new image.
  179. @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
  180. image. The current buffer size needed to hold the image is returned
  181. in ImageSize.
  182. @retval EFI_INVALID_PARAMETER The Image was NULL.
  183. @retval EFI_NOT_FOUND The current image is not copied to the buffer.
  184. @retval EFI_UNSUPPORTED The operation is not supported.
  185. **/
  186. EFI_STATUS
  187. EFIAPI
  188. FmpDeviceGetImage(
  189. IN OUT VOID *Image,
  190. IN OUT UINTN *ImageSize
  191. )
  192. /*++
  193. Routine Description:
  194. This is a function used to read the current firmware from the device into memory.
  195. This is an optional function and can return EFI_UNSUPPORTED. This is useful for
  196. test and diagnostics.
  197. Arguments:
  198. Image -- Buffer to place the image into.
  199. ImageSize -- Size of the Image buffer.
  200. Return Value:
  201. EFI_STATUS code.
  202. If not possible or not practical return EFI_UNSUPPORTED.
  203. --*/
  204. {
  205. return EFI_UNSUPPORTED;
  206. }//GetImage()
  207. /**
  208. Updates the firmware image of the device.
  209. This function updates the hardware with the new firmware image.
  210. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  211. If the firmware image is updatable, the function should perform the following minimal validations
  212. before proceeding to do the firmware image update.
  213. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  214. the image is unsupported. The function can optionally provide more detailed information on
  215. why the image is not a supported image.
  216. - Validate the data from VendorCode if not null. Image validation must be performed before
  217. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  218. validation failed. The function returns EFI_ABORTED if the data is invalid.
  219. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  220. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  221. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  222. firmware image version is older than the current firmware image version or bad image checksum.
  223. Sensitive operations such as those wiping the entire firmware image and render the device to be
  224. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  225. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  226. reason to the caller.
  227. @param[in] Image Points to the new image.
  228. @param[in] ImageSize Size of the new image in bytes.
  229. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  230. Null indicates the caller did not specify the policy or use the default policy.
  231. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  232. @param[in] CapsuleFwVersion FMP Payload Header version of the image
  233. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  234. details for the aborted operation. The buffer is allocated by this function
  235. with AllocatePool(), and it is the caller's responsibility to free it with a
  236. call to FreePool().
  237. @retval EFI_SUCCESS The device was successfully updated with the new image.
  238. @retval EFI_ABORTED The operation is aborted.
  239. @retval EFI_INVALID_PARAMETER The Image was NULL.
  240. @retval EFI_UNSUPPORTED The operation is not supported.
  241. **/
  242. EFI_STATUS
  243. EFIAPI
  244. FmpDeviceSetImage (
  245. IN CONST VOID *Image,
  246. IN UINTN ImageSize,
  247. IN CONST VOID *VendorCode,
  248. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  249. IN UINT32 CapsuleFwVersion,
  250. OUT CHAR16 **AbortReason
  251. )
  252. {
  253. EFI_STATUS Status = EFI_SUCCESS;
  254. UINT32 Updateable = 0;
  255. Status = FmpDeviceCheckImage(Image, ImageSize, &Updateable);
  256. if (EFI_ERROR(Status))
  257. {
  258. DEBUG((DEBUG_ERROR, "SetImage - Check Image failed with %r.\n", Status));
  259. goto cleanup;
  260. }
  261. if (Updateable != IMAGE_UPDATABLE_VALID)
  262. {
  263. DEBUG((DEBUG_ERROR, "SetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable));
  264. Status = EFI_ABORTED;
  265. goto cleanup;
  266. }
  267. if (Progress == NULL)
  268. {
  269. DEBUG((DEBUG_ERROR, "SetImage - Invalid progress callback\n"));
  270. Status = EFI_INVALID_PARAMETER;
  271. goto cleanup;
  272. }
  273. Status = Progress(15);
  274. if (EFI_ERROR(Status))
  275. {
  276. DEBUG((DEBUG_ERROR, "SetImage - Progress Callback failed with Status %r.\n", Status));
  277. }
  278. {
  279. UINTN p;
  280. for (p = 20; p < 100; p++) {
  281. gBS->Stall (100000); //us = 0.1 seconds
  282. Progress (p);
  283. }
  284. }
  285. //TODO: add support for VendorCode, and AbortReason
  286. cleanup:
  287. return Status;
  288. }// SetImage()
  289. /**
  290. Checks if the firmware image is valid for the device.
  291. This function allows firmware update application to validate the firmware image without
  292. invoking the SetImage() first.
  293. @param[in] Image Points to the new image.
  294. @param[in] ImageSize Size of the new image in bytes.
  295. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  296. if available, additional information if the image is invalid.
  297. @retval EFI_SUCCESS The image was successfully checked.
  298. @retval EFI_INVALID_PARAMETER The Image was NULL.
  299. **/
  300. EFI_STATUS
  301. EFIAPI
  302. FmpDeviceCheckImage(
  303. IN CONST VOID *Image,
  304. IN UINTN ImageSize,
  305. OUT UINT32 *ImageUpdateable
  306. )
  307. {
  308. EFI_STATUS status = EFI_SUCCESS;
  309. if (ImageUpdateable == NULL)
  310. {
  311. DEBUG((DEBUG_ERROR, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));
  312. status = EFI_INVALID_PARAMETER;
  313. goto cleanup;
  314. }
  315. //
  316. //Set to valid and then if any tests fail it will update this flag.
  317. //
  318. *ImageUpdateable = IMAGE_UPDATABLE_VALID;
  319. if (Image == NULL)
  320. {
  321. DEBUG((DEBUG_ERROR, "CheckImage - Image Pointer Parameter is NULL.\n"));
  322. *ImageUpdateable = IMAGE_UPDATABLE_INVALID; //not sure if this is needed
  323. return EFI_INVALID_PARAMETER;
  324. }
  325. cleanup:
  326. return status;
  327. }// CheckImage()
  328. /**
  329. Device firmware should trigger lock mechanism so that device fw can not be updated or tampered with.
  330. This lock mechanism is generally only cleared by a full system reset (not just sleep state/low power mode)
  331. @retval EFI_SUCCESS The device was successfully locked.
  332. @retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking
  333. **/
  334. EFI_STATUS
  335. EFIAPI
  336. FmpDeviceLock(
  337. )
  338. {
  339. return EFI_SUCCESS;
  340. }