SystemFirmwareReportDxe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /** @file
  2. SetImage instance to report system firmware and act as agent to system update.
  3. Caution: This module requires additional review when modified.
  4. This module will have external input - capsule image.
  5. This external input must be validated carefully to avoid security issue like
  6. buffer overflow, integer overflow.
  7. FmpSetImage() will receive untrusted input and do basic validation.
  8. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include "SystemFirmwareDxe.h"
  12. //
  13. // SystemFmp driver private data
  14. //
  15. SYSTEM_FMP_PRIVATE_DATA *mSystemFmpPrivate = NULL;
  16. /**
  17. Dispatch system FMP images.
  18. Caution: This function may receive untrusted input.
  19. @param[in] Image The EDKII system FMP capsule image.
  20. @param[in] ImageSize The size of the EDKII system FMP capsule image in bytes.
  21. @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  22. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  23. @retval EFI_SUCCESS Process Capsule Image successfully.
  24. @retval EFI_UNSUPPORTED Capsule image is not supported by the firmware.
  25. @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted.
  26. @retval EFI_OUT_OF_RESOURCES Not enough memory.
  27. **/
  28. EFI_STATUS
  29. DispatchSystemFmpImages (
  30. IN VOID *Image,
  31. IN UINTN ImageSize,
  32. OUT UINT32 *LastAttemptVersion,
  33. OUT UINT32 *LastAttemptStatus
  34. )
  35. {
  36. EFI_STATUS Status;
  37. VOID *AuthenticatedImage;
  38. UINTN AuthenticatedImageSize;
  39. VOID *DispatchFvImage;
  40. UINTN DispatchFvImageSize;
  41. EFI_HANDLE FvProtocolHandle;
  42. EFI_FIRMWARE_VOLUME_HEADER *FvImage;
  43. BOOLEAN Result;
  44. AuthenticatedImage = NULL;
  45. AuthenticatedImageSize = 0;
  46. DEBUG ((DEBUG_INFO, "DispatchSystemFmpImages\n"));
  47. //
  48. // Verify
  49. //
  50. Status = CapsuleAuthenticateSystemFirmware (Image, ImageSize, FALSE, LastAttemptVersion, LastAttemptStatus, &AuthenticatedImage, &AuthenticatedImageSize);
  51. if (EFI_ERROR (Status)) {
  52. DEBUG ((DEBUG_INFO, "SystemFirmwareAuthenticateImage - %r\n", Status));
  53. return Status;
  54. }
  55. //
  56. // Get FV
  57. //
  58. Result = ExtractDriverFvImage (AuthenticatedImage, AuthenticatedImageSize, &DispatchFvImage, &DispatchFvImageSize);
  59. if (Result) {
  60. DEBUG ((DEBUG_INFO, "ExtractDriverFvImage\n"));
  61. //
  62. // Dispatch
  63. //
  64. if (((EFI_FIRMWARE_VOLUME_HEADER *)DispatchFvImage)->FvLength == DispatchFvImageSize) {
  65. FvImage = AllocatePages (EFI_SIZE_TO_PAGES (DispatchFvImageSize));
  66. if (FvImage != NULL) {
  67. CopyMem (FvImage, DispatchFvImage, DispatchFvImageSize);
  68. Status = gDS->ProcessFirmwareVolume (
  69. (VOID *)FvImage,
  70. (UINTN)FvImage->FvLength,
  71. &FvProtocolHandle
  72. );
  73. DEBUG ((DEBUG_INFO, "ProcessFirmwareVolume - %r\n", Status));
  74. if (!EFI_ERROR (Status)) {
  75. gDS->Dispatch ();
  76. DEBUG ((DEBUG_INFO, "Dispatch Done\n"));
  77. }
  78. }
  79. }
  80. }
  81. return EFI_SUCCESS;
  82. }
  83. /**
  84. Updates the firmware image of the device.
  85. This function updates the hardware with the new firmware image.
  86. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  87. If the firmware image is updatable, the function should perform the following minimal validations
  88. before proceeding to do the firmware image update.
  89. - Validate the image authentication if image has attribute
  90. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  91. EFI_SECURITY_VIOLATION if the validation fails.
  92. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  93. the image is unsupported. The function can optionally provide more detailed information on
  94. why the image is not a supported image.
  95. - Validate the data from VendorCode if not null. Image validation must be performed before
  96. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  97. validation failed. The function returns EFI_ABORTED if the data is invalid.
  98. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  99. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  100. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  101. firmware image version is older than the current firmware image version or bad image checksum.
  102. Sensitive operations such as those wiping the entire firmware image and render the device to be
  103. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  104. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  105. reason to the caller.
  106. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  107. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  108. The number is between 1 and DescriptorCount.
  109. @param[in] Image Points to the new image.
  110. @param[in] ImageSize Size of the new image in bytes.
  111. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  112. Null indicates the caller did not specify the policy or use the default policy.
  113. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  114. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  115. details for the aborted operation. The buffer is allocated by this function
  116. with AllocatePool(), and it is the caller's responsibility to free it with a
  117. call to FreePool().
  118. @retval EFI_SUCCESS The device was successfully updated with the new image.
  119. @retval EFI_ABORTED The operation is aborted.
  120. @retval EFI_INVALID_PARAMETER The Image was NULL.
  121. @retval EFI_UNSUPPORTED The operation is not supported.
  122. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  123. **/
  124. EFI_STATUS
  125. EFIAPI
  126. FmpSetImage (
  127. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  128. IN UINT8 ImageIndex,
  129. IN CONST VOID *Image,
  130. IN UINTN ImageSize,
  131. IN CONST VOID *VendorCode,
  132. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  133. OUT CHAR16 **AbortReason
  134. )
  135. {
  136. SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate;
  137. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *SystemFmp;
  138. EFI_STATUS Status;
  139. EFI_STATUS VarStatus;
  140. if ((Image == NULL) || (ImageSize == 0) || (AbortReason == NULL)) {
  141. return EFI_INVALID_PARAMETER;
  142. }
  143. SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP (This);
  144. *AbortReason = NULL;
  145. if ((ImageIndex == 0) || (ImageIndex > SystemFmpPrivate->DescriptorCount)) {
  146. return EFI_INVALID_PARAMETER;
  147. }
  148. //
  149. // Process FV
  150. //
  151. Status = DispatchSystemFmpImages ((VOID *)Image, ImageSize, &SystemFmpPrivate->LastAttempt.LastAttemptVersion, &SystemFmpPrivate->LastAttempt.LastAttemptStatus);
  152. DEBUG ((DEBUG_INFO, "(Agent)SetImage - LastAttempt Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));
  153. if (EFI_ERROR (Status)) {
  154. VarStatus = gRT->SetVariable (
  155. SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
  156. &gSystemFmpLastAttemptVariableGuid,
  157. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  158. sizeof (SystemFmpPrivate->LastAttempt),
  159. &SystemFmpPrivate->LastAttempt
  160. );
  161. DEBUG ((DEBUG_INFO, "(Agent)SetLastAttempt - %r\n", VarStatus));
  162. return Status;
  163. }
  164. //
  165. // Pass Thru to System FMP Protocol on same handle as FMP Protocol
  166. //
  167. Status = gBS->HandleProtocol (
  168. SystemFmpPrivate->Handle,
  169. &gSystemFmpProtocolGuid,
  170. (VOID **)&SystemFmp
  171. );
  172. if (EFI_ERROR (Status)) {
  173. Status = gBS->LocateProtocol (
  174. &gSystemFmpProtocolGuid,
  175. NULL,
  176. (VOID **)&SystemFmp
  177. );
  178. if (EFI_ERROR (Status)) {
  179. DEBUG ((DEBUG_INFO, "(Agent)SetImage - SystemFmpProtocol - %r\n", Status));
  180. SystemFmpPrivate->LastAttempt.LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  181. VarStatus = gRT->SetVariable (
  182. SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
  183. &gSystemFmpLastAttemptVariableGuid,
  184. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  185. sizeof (SystemFmpPrivate->LastAttempt),
  186. &SystemFmpPrivate->LastAttempt
  187. );
  188. DEBUG ((DEBUG_INFO, "(Agent)SetLastAttempt - %r\n", VarStatus));
  189. return Status;
  190. }
  191. }
  192. return SystemFmp->SetImage (SystemFmp, ImageIndex, Image, ImageSize, VendorCode, Progress, AbortReason);
  193. }
  194. /**
  195. System FMP module entrypoint
  196. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  197. @param[in] SystemTable A pointer to the EFI System Table.
  198. @return EFI_SUCCESS System FMP module is initialized.
  199. **/
  200. EFI_STATUS
  201. EFIAPI
  202. SystemFirmwareReportMainDxe (
  203. IN EFI_HANDLE ImageHandle,
  204. IN EFI_SYSTEM_TABLE *SystemTable
  205. )
  206. {
  207. EFI_STATUS Status;
  208. //
  209. // Initialize SystemFmpPrivateData
  210. //
  211. mSystemFmpPrivate = AllocateZeroPool (sizeof (SYSTEM_FMP_PRIVATE_DATA));
  212. if (mSystemFmpPrivate == NULL) {
  213. return EFI_OUT_OF_RESOURCES;
  214. }
  215. Status = InitializePrivateData (mSystemFmpPrivate);
  216. if (EFI_ERROR (Status)) {
  217. FreePool (mSystemFmpPrivate);
  218. mSystemFmpPrivate = NULL;
  219. return Status;
  220. }
  221. //
  222. // Install FMP protocol.
  223. //
  224. Status = gBS->InstallProtocolInterface (
  225. &mSystemFmpPrivate->Handle,
  226. &gEfiFirmwareManagementProtocolGuid,
  227. EFI_NATIVE_INTERFACE,
  228. &mSystemFmpPrivate->Fmp
  229. );
  230. if (EFI_ERROR (Status)) {
  231. FreePool (mSystemFmpPrivate);
  232. mSystemFmpPrivate = NULL;
  233. return Status;
  234. }
  235. return Status;
  236. }