FmpDxe.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /** @file
  2. Produces a Firmware Management Protocol that supports updates to a firmware
  3. image stored in a firmware device with platform and firmware device specific
  4. information provided through PCDs and libraries.
  5. Copyright (c) Microsoft Corporation.<BR>
  6. Copyright (c) 2018 - 2021, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #ifndef _FMP_DXE_H_
  10. #define _FMP_DXE_H_
  11. #include <PiDxe.h>
  12. #include <Library/DebugLib.h>
  13. #include <Library/BaseLib.h>
  14. #include <Library/BaseMemoryLib.h>
  15. #include <Library/BaseCryptLib.h>
  16. #include <Library/PcdLib.h>
  17. #include <Library/UefiBootServicesTableLib.h>
  18. #include <Library/UefiRuntimeServicesTableLib.h>
  19. #include <Library/MemoryAllocationLib.h>
  20. #include <Library/UefiLib.h>
  21. #include <Library/PrintLib.h>
  22. #include <Library/FmpAuthenticationLib.h>
  23. #include <Library/FmpDeviceLib.h>
  24. #include <Library/FmpPayloadHeaderLib.h>
  25. #include <Library/CapsuleUpdatePolicyLib.h>
  26. #include <Library/FmpDependencyLib.h>
  27. #include <Library/FmpDependencyCheckLib.h>
  28. #include <Library/FmpDependencyDeviceLib.h>
  29. #include <Protocol/FirmwareManagement.h>
  30. #include <Protocol/FirmwareManagementProgress.h>
  31. #include <Guid/SystemResourceTable.h>
  32. #include <Guid/EventGroup.h>
  33. #include <LastAttemptStatus.h>
  34. #include <FmpLastAttemptStatus.h>
  35. #include <Library/VariablePolicyHelperLib.h>
  36. #define VERSION_STRING_NOT_SUPPORTED L"VERSION STRING NOT SUPPORTED"
  37. #define VERSION_STRING_NOT_AVAILABLE L"VERSION STRING NOT AVAILABLE"
  38. ///
  39. ///
  40. ///
  41. #define FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('f','m','p','p')
  42. typedef struct {
  43. UINTN Signature;
  44. EFI_HANDLE Handle;
  45. EFI_FIRMWARE_MANAGEMENT_PROTOCOL Fmp;
  46. BOOLEAN DescriptorPopulated;
  47. EFI_FIRMWARE_IMAGE_DESCRIPTOR Descriptor;
  48. CHAR16 *ImageIdName;
  49. CHAR16 *VersionName;
  50. BOOLEAN RuntimeVersionSupported;
  51. EFI_EVENT FmpDeviceLockEvent;
  52. //
  53. // Indicates if an attempt has been made to lock a
  54. // FLASH storage device by calling FmpDeviceLock().
  55. // A FLASH storage device may not support being locked,
  56. // so this variable is set to TRUE even if FmpDeviceLock()
  57. // returns an error.
  58. //
  59. BOOLEAN FmpDeviceLocked;
  60. VOID *FmpDeviceContext;
  61. CHAR16 *VersionVariableName;
  62. CHAR16 *LsvVariableName;
  63. CHAR16 *LastAttemptStatusVariableName;
  64. CHAR16 *LastAttemptVersionVariableName;
  65. CHAR16 *FmpStateVariableName;
  66. BOOLEAN DependenciesSatisfied;
  67. } FIRMWARE_MANAGEMENT_PRIVATE_DATA;
  68. ///
  69. ///
  70. ///
  71. #define FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS(a) \
  72. CR (a, FIRMWARE_MANAGEMENT_PRIVATE_DATA, Fmp, FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE)
  73. ///
  74. /// Null-terminated Unicode string retrieved from PcdFmpDeviceImageIdName.
  75. ///
  76. extern CHAR16 *mImageIdName;
  77. /**
  78. Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
  79. the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
  80. the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
  81. SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
  82. SHA256 hash matches or there is then error computing the SHA256 hash, then
  83. set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
  84. TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
  85. bytes.
  86. **/
  87. VOID
  88. DetectTestKey (
  89. VOID
  90. );
  91. /**
  92. Returns information about the current firmware image(s) of the device.
  93. This function allows a copy of the current firmware image to be created and saved.
  94. The saved copy could later been used, for example, in firmware image recovery or rollback.
  95. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  96. @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
  97. On input, this is the size of the buffer allocated by the caller.
  98. On output, it is the size of the buffer returned by the firmware
  99. if the buffer was large enough, or the size of the buffer needed
  100. to contain the image(s) information if the buffer was too small.
  101. @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
  102. information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  103. @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
  104. associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  105. @param[out] DescriptorCount A pointer to the location in which firmware returns the number of
  106. descriptors or firmware images within this device.
  107. @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
  108. of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  109. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  110. The format is vendor specific and new version must have a greater value
  111. than the old version. If PackageVersion is not supported, the value is
  112. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
  113. is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
  114. that package version update is in progress.
  115. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
  116. package version name. The buffer is allocated by this function with
  117. AllocatePool(), and it is the caller's responsibility to free it with a call
  118. to FreePool().
  119. @retval EFI_SUCCESS The device was successfully updated with the new image.
  120. @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
  121. needed to hold the image(s) information is returned in ImageInfoSize.
  122. @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
  123. @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
  124. **/
  125. EFI_STATUS
  126. EFIAPI
  127. GetTheImageInfo (
  128. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  129. IN OUT UINTN *ImageInfoSize,
  130. IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,
  131. OUT UINT32 *DescriptorVersion,
  132. OUT UINT8 *DescriptorCount,
  133. OUT UINTN *DescriptorSize,
  134. OUT UINT32 *PackageVersion,
  135. OUT CHAR16 **PackageVersionName
  136. );
  137. /**
  138. Retrieves a copy of the current firmware image of the device.
  139. This function allows a copy of the current firmware image to be created and saved.
  140. The saved copy could later been used, for example, in firmware image recovery or rollback.
  141. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  142. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  143. The number is between 1 and DescriptorCount.
  144. @param[in, out] Image Points to the buffer where the current image is copied to.
  145. @param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
  146. On return, points to the length of the image, in bytes.
  147. @retval EFI_SUCCESS The device was successfully updated with the new image.
  148. @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
  149. image. The current buffer size needed to hold the image is returned
  150. in ImageSize.
  151. @retval EFI_INVALID_PARAMETER The Image was NULL.
  152. @retval EFI_NOT_FOUND The current image is not copied to the buffer.
  153. @retval EFI_UNSUPPORTED The operation is not supported.
  154. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  155. **/
  156. EFI_STATUS
  157. EFIAPI
  158. GetTheImage (
  159. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  160. IN UINT8 ImageIndex,
  161. IN OUT VOID *Image,
  162. IN OUT UINTN *ImageSize
  163. );
  164. /**
  165. Checks if the firmware image is valid for the device.
  166. This function allows firmware update application to validate the firmware image without
  167. invoking the SetImage() first.
  168. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  169. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  170. The number is between 1 and DescriptorCount.
  171. @param[in] Image Points to the new image.
  172. @param[in] ImageSize Size of the new image in bytes.
  173. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  174. if available, additional information if the image is invalid.
  175. @retval EFI_SUCCESS The image was successfully checked.
  176. @retval EFI_ABORTED The operation is aborted.
  177. @retval EFI_INVALID_PARAMETER The Image was NULL.
  178. @retval EFI_UNSUPPORTED The operation is not supported.
  179. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  180. **/
  181. EFI_STATUS
  182. EFIAPI
  183. CheckTheImage (
  184. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  185. IN UINT8 ImageIndex,
  186. IN CONST VOID *Image,
  187. IN UINTN ImageSize,
  188. OUT UINT32 *ImageUpdatable
  189. );
  190. /**
  191. Updates the firmware image of the device.
  192. This function updates the hardware with the new firmware image.
  193. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  194. If the firmware image is updatable, the function should perform the following minimal validations
  195. before proceeding to do the firmware image update.
  196. - Validate the image authentication if image has attribute
  197. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  198. EFI_SECURITY_VIOLATION if the validation fails.
  199. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  200. the image is unsupported. The function can optionally provide more detailed information on
  201. why the image is not a supported image.
  202. - Validate the data from VendorCode if not null. Image validation must be performed before
  203. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  204. validation failed. The function returns EFI_ABORTED if the data is invalid.
  205. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  206. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  207. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  208. firmware image version is older than the current firmware image version or bad image checksum.
  209. Sensitive operations such as those wiping the entire firmware image and render the device to be
  210. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  211. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  212. reason to the caller.
  213. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  214. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  215. The number is between 1 and DescriptorCount.
  216. @param[in] Image Points to the new image.
  217. @param[in] ImageSize Size of the new image in bytes.
  218. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  219. Null indicates the caller did not specify the policy or use the default policy.
  220. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  221. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  222. details for the aborted operation. The buffer is allocated by this function
  223. with AllocatePool(), and it is the caller's responsibility to free it with a
  224. call to FreePool().
  225. @retval EFI_SUCCESS The device was successfully updated with the new image.
  226. @retval EFI_ABORTED The operation is aborted.
  227. @retval EFI_INVALID_PARAMETER The Image was NULL.
  228. @retval EFI_UNSUPPORTED The operation is not supported.
  229. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  230. **/
  231. EFI_STATUS
  232. EFIAPI
  233. SetTheImage (
  234. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  235. IN UINT8 ImageIndex,
  236. IN CONST VOID *Image,
  237. IN UINTN ImageSize,
  238. IN CONST VOID *VendorCode,
  239. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  240. OUT CHAR16 **AbortReason
  241. );
  242. /**
  243. Returns information about the firmware package.
  244. This function returns package information.
  245. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  246. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  247. The format is vendor specific and new version must have a greater value
  248. than the old version. If PackageVersion is not supported, the value is
  249. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
  250. comparison is to be performed using PackageVersionName. A value of
  251. 0xFFFFFFFD indicates that package version update is in progress.
  252. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
  253. the package version name. The buffer is allocated by this function with
  254. AllocatePool(), and it is the caller's responsibility to free it with a
  255. call to FreePool().
  256. @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
  257. package version name. A value of 0 indicates the device does not support
  258. update of package version name. Length is the number of Unicode characters,
  259. including the terminating null character.
  260. @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
  261. Definitions' for possible returned values of this parameter. A value of 1
  262. indicates the attribute is supported and the current setting value is
  263. indicated in AttributesSetting. A value of 0 indicates the attribute is not
  264. supported and the current setting value in AttributesSetting is meaningless.
  265. @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
  266. values of this parameter
  267. @retval EFI_SUCCESS The package information was successfully returned.
  268. @retval EFI_UNSUPPORTED The operation is not supported.
  269. **/
  270. EFI_STATUS
  271. EFIAPI
  272. GetPackageInfo (
  273. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  274. OUT UINT32 *PackageVersion,
  275. OUT CHAR16 **PackageVersionName,
  276. OUT UINT32 *PackageVersionNameMaxLen,
  277. OUT UINT64 *AttributesSupported,
  278. OUT UINT64 *AttributesSetting
  279. );
  280. /**
  281. Updates information about the firmware package.
  282. This function updates package information.
  283. This function returns EFI_UNSUPPORTED if the package information is not updatable.
  284. VendorCode enables vendor to implement vendor-specific package information update policy.
  285. Null if the caller did not specify this policy or use the default policy.
  286. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  287. @param[in] Image Points to the authentication image.
  288. Null if authentication is not required.
  289. @param[in] ImageSize Size of the authentication image in bytes.
  290. 0 if authentication is not required.
  291. @param[in] VendorCode This enables vendor to implement vendor-specific firmware
  292. image update policy.
  293. Null indicates the caller did not specify this policy or use
  294. the default policy.
  295. @param[in] PackageVersion The new package version.
  296. @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
  297. the package version name.
  298. The string length is equal to or less than the value returned in
  299. PackageVersionNameMaxLen.
  300. @retval EFI_SUCCESS The device was successfully updated with the new package
  301. information.
  302. @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
  303. returned in PackageVersionNameMaxLen.
  304. @retval EFI_UNSUPPORTED The operation is not supported.
  305. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  306. **/
  307. EFI_STATUS
  308. EFIAPI
  309. SetPackageInfo (
  310. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  311. IN CONST VOID *Image,
  312. IN UINTN ImageSize,
  313. IN CONST VOID *VendorCode,
  314. IN UINT32 PackageVersion,
  315. IN CONST CHAR16 *PackageVersionName
  316. );
  317. #endif