FmpDxe.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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 - 2020, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "FmpDxe.h"
  10. #include "VariableSupport.h"
  11. ///
  12. /// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the
  13. /// FILE_GUID must always be overridden in the <Defines> section to provide
  14. /// the ESRT GUID value associated with the updatable firmware image. A
  15. /// check is made in this module's driver entry point to verify that a
  16. /// new FILE_GUID value has been defined.
  17. ///
  18. const EFI_GUID mDefaultModuleFileGuid = {
  19. 0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }
  20. };
  21. ///
  22. /// TRUE if FmpDeviceLib manages a single firmware storage device.
  23. ///
  24. BOOLEAN mFmpSingleInstance = FALSE;
  25. ///
  26. /// Firmware Management Protocol instance that is initialized in the entry
  27. /// point from PCD settings.
  28. ///
  29. EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL mFmpProgress;
  30. //
  31. // Template of the private context structure for the Firmware Management
  32. // Protocol instance
  33. //
  34. const FIRMWARE_MANAGEMENT_PRIVATE_DATA mFirmwareManagementPrivateDataTemplate = {
  35. FIRMWARE_MANAGEMENT_PRIVATE_DATA_SIGNATURE, // Signature
  36. NULL, // Handle
  37. { // Fmp
  38. GetTheImageInfo,
  39. GetTheImage,
  40. SetTheImage,
  41. CheckTheImage,
  42. GetPackageInfo,
  43. SetPackageInfo
  44. },
  45. FALSE, // DescriptorPopulated
  46. { // Desc
  47. 1, // ImageIndex
  48. //
  49. // ImageTypeId
  50. //
  51. { 0x00000000, 0x0000,0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  52. },
  53. 1, // ImageId
  54. NULL, // ImageIdName
  55. 0, // Version
  56. NULL, // VersionName
  57. 0, // Size
  58. 0, // AttributesSupported
  59. 0, // AttributesSetting
  60. 0, // Compatibilities
  61. 0, // LowestSupportedImageVersion
  62. 0, // LastAttemptVersion
  63. 0, // LastAttemptStatus
  64. 0 // HardwareInstance
  65. },
  66. NULL, // ImageIdName
  67. NULL, // VersionName
  68. TRUE, // RuntimeVersionSupported
  69. NULL, // FmpDeviceLockEvent
  70. FALSE, // FmpDeviceLocked
  71. NULL, // FmpDeviceContext
  72. NULL, // VersionVariableName
  73. NULL, // LsvVariableName
  74. NULL, // LastAttemptStatusVariableName
  75. NULL, // LastAttemptVersionVariableName
  76. NULL, // FmpStateVariableName
  77. TRUE // DependenciesSatisfied
  78. };
  79. ///
  80. /// GUID that is used to create event used to lock the firmware storage device.
  81. ///
  82. EFI_GUID *mLockGuid = NULL;
  83. ///
  84. /// Progress() function pointer passed into SetTheImage()
  85. ///
  86. EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc = NULL;
  87. ///
  88. /// Null-terminated Unicode string retrieved from PcdFmpDeviceImageIdName.
  89. ///
  90. CHAR16 *mImageIdName = NULL;
  91. /**
  92. Callback function to report the process of the firmware updating.
  93. Wrap the caller's version in this so that progress from the device lib is
  94. within the expected range. Convert device lib 0% - 100% to 6% - 98%.
  95. FmpDxe 1% - 5% for validation
  96. FmpDeviceLib 6% - 98% for flashing/update
  97. FmpDxe 99% - 100% finish
  98. @param[in] Completion A value between 1 and 100 indicating the current
  99. completion progress of the firmware update. Completion
  100. progress is reported as from 1 to 100 percent. A value
  101. of 0 is used by the driver to indicate that progress
  102. reporting is not supported.
  103. @retval EFI_SUCCESS The progress was updated.
  104. @retval EFI_UNSUPPORTED Updating progress is not supported.
  105. **/
  106. EFI_STATUS
  107. EFIAPI
  108. FmpDxeProgress (
  109. IN UINTN Completion
  110. )
  111. {
  112. EFI_STATUS Status;
  113. Status = EFI_UNSUPPORTED;
  114. if (mProgressFunc == NULL) {
  115. return Status;
  116. }
  117. //
  118. // Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.
  119. //
  120. Status = mProgressFunc (((Completion * 92) / 100) + 6);
  121. if (Status == EFI_UNSUPPORTED) {
  122. mProgressFunc = NULL;
  123. }
  124. return Status;
  125. }
  126. /**
  127. Returns a pointer to the ImageTypeId GUID value. An attempt is made to get
  128. the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide
  129. a GUID value, then PcdFmpDeviceImageTypeIdGuid is used. If the size of
  130. PcdFmpDeviceImageTypeIdGuid is not the size of EFI_GUID, then gEfiCallerIdGuid
  131. is returned.
  132. @retval The ImageTypeId GUID
  133. **/
  134. EFI_GUID *
  135. GetImageTypeIdGuid (
  136. VOID
  137. )
  138. {
  139. EFI_STATUS Status;
  140. EFI_GUID *FmpDeviceLibGuid;
  141. UINTN ImageTypeIdGuidSize;
  142. FmpDeviceLibGuid = NULL;
  143. Status = FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid);
  144. if (EFI_ERROR (Status)) {
  145. if (Status != EFI_UNSUPPORTED) {
  146. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", mImageIdName, Status));
  147. }
  148. } else if (FmpDeviceLibGuid == NULL) {
  149. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n", mImageIdName));
  150. Status = EFI_NOT_FOUND;
  151. }
  152. if (EFI_ERROR (Status)) {
  153. ImageTypeIdGuidSize = PcdGetSize (PcdFmpDeviceImageTypeIdGuid);
  154. if (ImageTypeIdGuidSize == sizeof (EFI_GUID)) {
  155. FmpDeviceLibGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceImageTypeIdGuid);
  156. } else {
  157. DEBUG ((DEBUG_WARN, "FmpDxe(%s): Fall back to ImageTypeIdGuid of gEfiCallerIdGuid\n", mImageIdName));
  158. FmpDeviceLibGuid = &gEfiCallerIdGuid;
  159. }
  160. }
  161. return FmpDeviceLibGuid;
  162. }
  163. /**
  164. Returns a pointer to the Null-terminated Unicode ImageIdName string.
  165. @retval Null-terminated Unicode ImageIdName string.
  166. **/
  167. CHAR16 *
  168. GetImageTypeNameString (
  169. VOID
  170. )
  171. {
  172. return mImageIdName;
  173. }
  174. /**
  175. Lowest supported version is a combo of three parts.
  176. 1. Check if the device lib has a lowest supported version
  177. 2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)
  178. 3. Check Fixed at build PCD
  179. @param[in] Private Pointer to the private context structure for the
  180. Firmware Management Protocol instance.
  181. @retval The largest value
  182. **/
  183. UINT32
  184. GetLowestSupportedVersion (
  185. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private
  186. )
  187. {
  188. EFI_STATUS Status;
  189. UINT32 DeviceLibLowestSupportedVersion;
  190. UINT32 VariableLowestSupportedVersion;
  191. UINT32 ReturnLsv;
  192. //
  193. // Get the LowestSupportedVersion.
  194. //
  195. if (!IsLowestSupportedVersionCheckRequired ()) {
  196. //
  197. // Any Version can pass the 0 LowestSupportedVersion check.
  198. //
  199. return 0;
  200. }
  201. ReturnLsv = PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion);
  202. //
  203. // Check the FmpDeviceLib
  204. //
  205. DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  206. Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);
  207. if (EFI_ERROR (Status)) {
  208. DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  209. }
  210. if (DeviceLibLowestSupportedVersion > ReturnLsv) {
  211. ReturnLsv = DeviceLibLowestSupportedVersion;
  212. }
  213. //
  214. // Check the lowest supported version UEFI variable for this device
  215. //
  216. VariableLowestSupportedVersion = GetLowestSupportedVersionFromVariable (Private);
  217. if (VariableLowestSupportedVersion > ReturnLsv) {
  218. ReturnLsv = VariableLowestSupportedVersion;
  219. }
  220. //
  221. // Return the largest value
  222. //
  223. return ReturnLsv;
  224. }
  225. /**
  226. Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the private
  227. context structure.
  228. @param[in] Private Pointer to the private context structure for the
  229. Firmware Management Protocol instance.
  230. **/
  231. VOID
  232. PopulateDescriptor (
  233. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private
  234. )
  235. {
  236. EFI_STATUS Status;
  237. UINT32 DependenciesSize;
  238. if (Private == NULL) {
  239. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): PopulateDescriptor() - Private is NULL.\n", mImageIdName));
  240. return;
  241. }
  242. if (Private->DescriptorPopulated) {
  243. return;
  244. }
  245. Private->Descriptor.ImageIndex = 1;
  246. CopyGuid (&Private->Descriptor.ImageTypeId, GetImageTypeIdGuid ());
  247. Private->Descriptor.ImageId = Private->Descriptor.ImageIndex;
  248. Private->Descriptor.ImageIdName = GetImageTypeNameString ();
  249. //
  250. // Get the hardware instance from FmpDeviceLib
  251. //
  252. Status = FmpDeviceGetHardwareInstance (&Private->Descriptor.HardwareInstance);
  253. if (Status == EFI_UNSUPPORTED) {
  254. Private->Descriptor.HardwareInstance = 0;
  255. }
  256. //
  257. // Generate UEFI Variable names used to store status information for this
  258. // FMP instance.
  259. //
  260. GenerateFmpVariableNames (Private);
  261. //
  262. // Get the version. Some devices don't support getting the firmware version
  263. // at runtime. If FmpDeviceLib does not support returning a version, then
  264. // it is stored in a UEFI variable.
  265. //
  266. Status = FmpDeviceGetVersion (&Private->Descriptor.Version);
  267. if (Status == EFI_UNSUPPORTED) {
  268. Private->RuntimeVersionSupported = FALSE;
  269. Private->Descriptor.Version = GetVersionFromVariable (Private);
  270. } else if (EFI_ERROR (Status)) {
  271. //
  272. // Unexpected error. Use default version.
  273. //
  274. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetVersion() from FmpDeviceLib (%s) returned %r\n", mImageIdName, GetImageTypeNameString (), Status));
  275. Private->Descriptor.Version = DEFAULT_VERSION;
  276. }
  277. //
  278. // Free the current version name. Shouldn't really happen but this populate
  279. // function could be called multiple times (to refresh).
  280. //
  281. if (Private->Descriptor.VersionName != NULL) {
  282. FreePool (Private->Descriptor.VersionName);
  283. Private->Descriptor.VersionName = NULL;
  284. }
  285. //
  286. // Attempt to get the version string from the FmpDeviceLib
  287. //
  288. Status = FmpDeviceGetVersionString (&Private->Descriptor.VersionName);
  289. if (Status == EFI_UNSUPPORTED) {
  290. DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() unsupported in FmpDeviceLib.\n", mImageIdName));
  291. Private->Descriptor.VersionName = AllocateCopyPool (
  292. sizeof (VERSION_STRING_NOT_SUPPORTED),
  293. VERSION_STRING_NOT_SUPPORTED
  294. );
  295. } else if (EFI_ERROR (Status)) {
  296. DEBUG ((DEBUG_INFO, "FmpDxe(%s): GetVersionString() not available in FmpDeviceLib.\n", mImageIdName));
  297. Private->Descriptor.VersionName = AllocateCopyPool (
  298. sizeof (VERSION_STRING_NOT_AVAILABLE),
  299. VERSION_STRING_NOT_AVAILABLE
  300. );
  301. }
  302. Private->Descriptor.LowestSupportedImageVersion = GetLowestSupportedVersion (Private);
  303. //
  304. // Get attributes from the FmpDeviceLib
  305. //
  306. FmpDeviceGetAttributes (
  307. &Private->Descriptor.AttributesSupported,
  308. &Private->Descriptor.AttributesSetting
  309. );
  310. //
  311. // Force set the updatable bits in the attributes;
  312. //
  313. Private->Descriptor.AttributesSupported |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
  314. Private->Descriptor.AttributesSetting |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
  315. //
  316. // Force set the authentication bits in the attributes;
  317. //
  318. Private->Descriptor.AttributesSupported |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
  319. Private->Descriptor.AttributesSetting |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
  320. Private->Descriptor.Compatibilities = 0;
  321. //
  322. // Get the size of the firmware image from the FmpDeviceLib
  323. //
  324. Status = FmpDeviceGetSize (&Private->Descriptor.Size);
  325. if (EFI_ERROR (Status)) {
  326. Private->Descriptor.Size = 0;
  327. }
  328. Private->Descriptor.LastAttemptVersion = GetLastAttemptVersionFromVariable (Private);
  329. Private->Descriptor.LastAttemptStatus = GetLastAttemptStatusFromVariable (Private);
  330. //
  331. // Get the dependency from the FmpDependencyDeviceLib.
  332. //
  333. Private->Descriptor.Dependencies = NULL;
  334. //
  335. // Check the attribute IMAGE_ATTRIBUTE_DEPENDENCY
  336. //
  337. if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {
  338. Private->Descriptor.Dependencies = GetFmpDependency (&DependenciesSize);
  339. }
  340. Private->DescriptorPopulated = TRUE;
  341. }
  342. /**
  343. Returns information about the current firmware image(s) of the device.
  344. This function allows a copy of the current firmware image to be created and saved.
  345. The saved copy could later been used, for example, in firmware image recovery or rollback.
  346. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  347. @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
  348. On input, this is the size of the buffer allocated by the caller.
  349. On output, it is the size of the buffer returned by the firmware
  350. if the buffer was large enough, or the size of the buffer needed
  351. to contain the image(s) information if the buffer was too small.
  352. @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
  353. information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  354. @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
  355. associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  356. @param[out] DescriptorCount A pointer to the location in which firmware returns the number of
  357. descriptors or firmware images within this device.
  358. @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
  359. of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  360. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  361. The format is vendor specific and new version must have a greater value
  362. than the old version. If PackageVersion is not supported, the value is
  363. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
  364. is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
  365. that package version update is in progress.
  366. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
  367. package version name. The buffer is allocated by this function with
  368. AllocatePool(), and it is the caller's responsibility to free it with a call
  369. to FreePool().
  370. @retval EFI_SUCCESS The device was successfully updated with the new image.
  371. @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
  372. needed to hold the image(s) information is returned in ImageInfoSize.
  373. @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
  374. @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
  375. **/
  376. EFI_STATUS
  377. EFIAPI
  378. GetTheImageInfo (
  379. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  380. IN OUT UINTN *ImageInfoSize,
  381. IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,
  382. OUT UINT32 *DescriptorVersion,
  383. OUT UINT8 *DescriptorCount,
  384. OUT UINTN *DescriptorSize,
  385. OUT UINT32 *PackageVersion,
  386. OUT CHAR16 **PackageVersionName
  387. )
  388. {
  389. EFI_STATUS Status;
  390. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  391. Status = EFI_SUCCESS;
  392. if (This == NULL) {
  393. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - This is NULL.\n", mImageIdName));
  394. Status = EFI_INVALID_PARAMETER;
  395. goto cleanup;
  396. }
  397. //
  398. // Retrieve the private context structure
  399. //
  400. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
  401. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  402. //
  403. // Check for valid pointer
  404. //
  405. if (ImageInfoSize == NULL) {
  406. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is NULL.\n", mImageIdName));
  407. Status = EFI_INVALID_PARAMETER;
  408. goto cleanup;
  409. }
  410. //
  411. // Check the buffer size
  412. // NOTE: Check this first so caller can get the necessary memory size it must allocate.
  413. //
  414. if (*ImageInfoSize < (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR))) {
  415. *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  416. DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImageInfo() - ImageInfoSize is to small.\n", mImageIdName));
  417. Status = EFI_BUFFER_TOO_SMALL;
  418. goto cleanup;
  419. }
  420. //
  421. // Confirm that buffer isn't null
  422. //
  423. if ( (ImageInfo == NULL) || (DescriptorVersion == NULL) || (DescriptorCount == NULL) || (DescriptorSize == NULL)
  424. || (PackageVersion == NULL))
  425. {
  426. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImageInfo() - Pointer Parameter is NULL.\n", mImageIdName));
  427. Status = EFI_INVALID_PARAMETER;
  428. goto cleanup;
  429. }
  430. //
  431. // Set the size to whatever we need
  432. //
  433. *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  434. //
  435. // Make sure the descriptor has already been loaded or refreshed
  436. //
  437. PopulateDescriptor (Private);
  438. //
  439. // Copy the image descriptor
  440. //
  441. CopyMem (ImageInfo, &Private->Descriptor, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR));
  442. *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
  443. *DescriptorCount = 1;
  444. *DescriptorSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  445. //
  446. // means unsupported
  447. //
  448. *PackageVersion = 0xFFFFFFFF;
  449. //
  450. // Do not update PackageVersionName since it is not supported in this instance.
  451. //
  452. cleanup:
  453. return Status;
  454. }
  455. /**
  456. Retrieves a copy of the current firmware image of the device.
  457. This function allows a copy of the current firmware image to be created and saved.
  458. The saved copy could later been used, for example, in firmware image recovery or rollback.
  459. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  460. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  461. The number is between 1 and DescriptorCount.
  462. @param[in, out] Image Points to the buffer where the current image is copied to.
  463. @param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
  464. On return, points to the length of the image, in bytes.
  465. @retval EFI_SUCCESS The device was successfully updated with the new image.
  466. @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
  467. image. The current buffer size needed to hold the image is returned
  468. in ImageSize.
  469. @retval EFI_INVALID_PARAMETER The Image was NULL.
  470. @retval EFI_NOT_FOUND The current image is not copied to the buffer.
  471. @retval EFI_UNSUPPORTED The operation is not supported.
  472. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  473. **/
  474. EFI_STATUS
  475. EFIAPI
  476. GetTheImage (
  477. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  478. IN UINT8 ImageIndex,
  479. IN OUT VOID *Image,
  480. IN OUT UINTN *ImageSize
  481. )
  482. {
  483. EFI_STATUS Status;
  484. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  485. UINTN Size;
  486. if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
  487. return EFI_UNSUPPORTED;
  488. }
  489. Status = EFI_SUCCESS;
  490. if (This == NULL) {
  491. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - This is NULL.\n", mImageIdName));
  492. Status = EFI_INVALID_PARAMETER;
  493. goto cleanup;
  494. }
  495. //
  496. // Retrieve the private context structure
  497. //
  498. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
  499. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  500. //
  501. // Check to make sure index is 1 (only 1 image for this device)
  502. //
  503. if (ImageIndex != 1) {
  504. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Index Invalid.\n", mImageIdName));
  505. Status = EFI_INVALID_PARAMETER;
  506. goto cleanup;
  507. }
  508. if (ImageSize == NULL) {
  509. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - ImageSize Pointer Parameter is NULL.\n", mImageIdName));
  510. Status = EFI_INVALID_PARAMETER;
  511. goto cleanup;
  512. }
  513. //
  514. // Check the buffer size
  515. //
  516. Status = FmpDeviceGetSize (&Size);
  517. if (EFI_ERROR (Status)) {
  518. Size = 0;
  519. }
  520. if (*ImageSize < Size) {
  521. *ImageSize = Size;
  522. DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): GetImage() - ImageSize is to small.\n", mImageIdName));
  523. Status = EFI_BUFFER_TOO_SMALL;
  524. goto cleanup;
  525. }
  526. if (Image == NULL) {
  527. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetImage() - Image Pointer Parameter is NULL.\n", mImageIdName));
  528. Status = EFI_INVALID_PARAMETER;
  529. goto cleanup;
  530. }
  531. Status = FmpDeviceGetImage (Image, ImageSize);
  532. cleanup:
  533. return Status;
  534. }
  535. /**
  536. Helper function to safely retrieve the FMP header from
  537. within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
  538. @param[in] Image Pointer to the image.
  539. @param[in] ImageSize Size of the image.
  540. @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
  541. @param[out] PayloadSize An optional pointer to a UINTN that holds the size of the payload
  542. (image size minus headers)
  543. @retval !NULL Valid pointer to the header.
  544. @retval NULL Structure is bad and pointer cannot be found.
  545. **/
  546. VOID *
  547. GetFmpHeader (
  548. IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  549. IN CONST UINTN ImageSize,
  550. IN CONST UINTN AdditionalHeaderSize,
  551. OUT UINTN *PayloadSize OPTIONAL
  552. )
  553. {
  554. //
  555. // Check to make sure that operation can be safely performed.
  556. //
  557. if ((((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize < (UINTN)Image) || \
  558. (((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) + AdditionalHeaderSize >= (UINTN)Image + ImageSize))
  559. {
  560. //
  561. // Pointer overflow. Invalid image.
  562. //
  563. return NULL;
  564. }
  565. if (PayloadSize != NULL) {
  566. *PayloadSize = ImageSize - (sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);
  567. }
  568. return (VOID *)((UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength + AdditionalHeaderSize);
  569. }
  570. /**
  571. Helper function to safely calculate the size of all headers
  572. within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
  573. @param[in] Image Pointer to the image.
  574. @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
  575. @retval UINT32>0 Valid size of all the headers.
  576. @retval 0 Structure is bad and size cannot be found.
  577. **/
  578. UINT32
  579. GetAllHeaderSize (
  580. IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  581. IN UINT32 AdditionalHeaderSize
  582. )
  583. {
  584. UINT32 CalculatedSize;
  585. if (Image == NULL) {
  586. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): GetAllHeaderSize() - Image is NULL.\n", mImageIdName));
  587. return 0;
  588. }
  589. CalculatedSize = sizeof (Image->MonotonicCount) +
  590. AdditionalHeaderSize +
  591. Image->AuthInfo.Hdr.dwLength;
  592. //
  593. // Check to make sure that operation can be safely performed.
  594. //
  595. if ((CalculatedSize < sizeof (Image->MonotonicCount)) ||
  596. (CalculatedSize < AdditionalHeaderSize) ||
  597. (CalculatedSize < Image->AuthInfo.Hdr.dwLength))
  598. {
  599. //
  600. // Integer overflow. Invalid image.
  601. //
  602. return 0;
  603. }
  604. return CalculatedSize;
  605. }
  606. /**
  607. Checks if the firmware image is valid for the device.
  608. This function allows firmware update application to validate the firmware image without
  609. invoking the SetImage() first.
  610. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  611. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  612. The number is between 1 and DescriptorCount.
  613. @param[in] Image Points to the new image.
  614. @param[in] ImageSize Size of the new image in bytes.
  615. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  616. if available, additional information if the image is invalid.
  617. @param[out] LastAttemptStatus A pointer to a UINT32 that holds the last attempt status to report
  618. back to the ESRT table in case of error. If an error does not occur,
  619. this function will set the value to LAST_ATTEMPT_STATUS_SUCCESS.
  620. This function will return error codes that occur within this function
  621. implementation within a driver range of last attempt error codes from
  622. LAST_ATTEMPT_STATUS_DRIVER_MIN_ERROR_CODE_VALUE
  623. to LAST_ATTEMPT_STATUS_DRIVER_MAX_ERROR_CODE_VALUE.
  624. This function might also return error codes that occur within libraries
  625. linked against this module that return last attempt error codes such as:
  626. LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_LIB_MIN_ERROR_CODE_VALUE to
  627. LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_LIB_MAX_ERROR_CODE_VALUE
  628. LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_CHECK_LIB_MIN_ERROR_CODE_VALUE to
  629. LAST_ATTEMPT_STATUS_FMP_DEPENDENCY_CHECK_LIB_MAX_ERROR_CODE_VALUE
  630. @retval EFI_SUCCESS The image was successfully checked.
  631. @retval EFI_ABORTED The operation is aborted.
  632. @retval EFI_INVALID_PARAMETER The Image was NULL.
  633. @retval EFI_UNSUPPORTED The operation is not supported.
  634. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  635. **/
  636. EFI_STATUS
  637. EFIAPI
  638. CheckTheImageInternal (
  639. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  640. IN UINT8 ImageIndex,
  641. IN CONST VOID *Image,
  642. IN UINTN ImageSize,
  643. OUT UINT32 *ImageUpdatable,
  644. OUT UINT32 *LastAttemptStatus
  645. )
  646. {
  647. EFI_STATUS Status;
  648. UINT32 LocalLastAttemptStatus;
  649. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  650. UINTN RawSize;
  651. VOID *FmpPayloadHeader;
  652. UINTN FmpPayloadSize;
  653. UINT32 Version;
  654. UINT32 FmpHeaderSize;
  655. UINTN AllHeaderSize;
  656. UINT32 Index;
  657. VOID *PublicKeyData;
  658. UINTN PublicKeyDataLength;
  659. UINT8 *PublicKeyDataXdr;
  660. UINT8 *PublicKeyDataXdrEnd;
  661. EFI_FIRMWARE_IMAGE_DEP *Dependencies;
  662. UINT32 DependenciesSize;
  663. Status = EFI_SUCCESS;
  664. LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  665. RawSize = 0;
  666. FmpPayloadHeader = NULL;
  667. FmpPayloadSize = 0;
  668. Version = 0;
  669. FmpHeaderSize = 0;
  670. AllHeaderSize = 0;
  671. Dependencies = NULL;
  672. DependenciesSize = 0;
  673. if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
  674. return EFI_UNSUPPORTED;
  675. }
  676. if (LastAttemptStatus == NULL) {
  677. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImageInternal() - LastAttemptStatus is NULL.\n", mImageIdName));
  678. Status = EFI_INVALID_PARAMETER;
  679. goto cleanup;
  680. }
  681. //
  682. // A last attempt status error code will always override the success
  683. // value before returning from the function
  684. //
  685. *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  686. if (This == NULL) {
  687. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - This is NULL.\n", mImageIdName));
  688. Status = EFI_INVALID_PARAMETER;
  689. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROTOCOL_ARG_MISSING;
  690. goto cleanup;
  691. }
  692. //
  693. // Retrieve the private context structure
  694. //
  695. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
  696. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  697. //
  698. // Make sure the descriptor has already been loaded or refreshed
  699. //
  700. PopulateDescriptor (Private);
  701. if (ImageUpdatable == NULL) {
  702. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n", mImageIdName));
  703. Status = EFI_INVALID_PARAMETER;
  704. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_UPDATABLE;
  705. goto cleanup;
  706. }
  707. //
  708. // Set to valid and then if any tests fail it will update this flag.
  709. //
  710. *ImageUpdatable = IMAGE_UPDATABLE_VALID;
  711. //
  712. // Set to satisfied and then if dependency evaluates to false it will update this flag.
  713. //
  714. Private->DependenciesSatisfied = TRUE;
  715. if (Image == NULL) {
  716. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Pointer Parameter is NULL.\n", mImageIdName));
  717. //
  718. // not sure if this is needed
  719. //
  720. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  721. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_NOT_PROVIDED;
  722. return EFI_INVALID_PARAMETER;
  723. }
  724. PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
  725. PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
  726. if ((PublicKeyDataXdr == NULL) || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
  727. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Invalid certificate, skipping it.\n", mImageIdName));
  728. Status = EFI_ABORTED;
  729. LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_CERTIFICATE;
  730. } else {
  731. //
  732. // Try each key from PcdFmpDevicePkcs7CertBufferXdr
  733. //
  734. for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {
  735. Index++;
  736. DEBUG (
  737. (DEBUG_INFO,
  738. "FmpDxe(%s): Certificate #%d [%p..%p].\n",
  739. mImageIdName,
  740. Index,
  741. PublicKeyDataXdr,
  742. PublicKeyDataXdrEnd
  743. )
  744. );
  745. if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {
  746. //
  747. // Key data extends beyond end of PCD
  748. //
  749. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate size extends beyond end of PCD, skipping it.\n", mImageIdName));
  750. Status = EFI_ABORTED;
  751. LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH_VALUE;
  752. break;
  753. }
  754. //
  755. // Read key length stored in big-endian format
  756. //
  757. PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
  758. //
  759. // Point to the start of the key data
  760. //
  761. PublicKeyDataXdr += sizeof (UINT32);
  762. if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
  763. //
  764. // Key data extends beyond end of PCD
  765. //
  766. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Certificate extends beyond end of PCD, skipping it.\n", mImageIdName));
  767. Status = EFI_ABORTED;
  768. LocalLastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_KEY_LENGTH;
  769. break;
  770. }
  771. PublicKeyData = PublicKeyDataXdr;
  772. Status = AuthenticateFmpImage (
  773. (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
  774. ImageSize,
  775. PublicKeyData,
  776. PublicKeyDataLength
  777. );
  778. if (!EFI_ERROR (Status)) {
  779. break;
  780. }
  781. PublicKeyDataXdr += PublicKeyDataLength;
  782. PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
  783. }
  784. }
  785. if (EFI_ERROR (Status)) {
  786. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - Authentication Failed %r.\n", mImageIdName, Status));
  787. if (LocalLastAttemptStatus != LAST_ATTEMPT_STATUS_SUCCESS) {
  788. *LastAttemptStatus = LocalLastAttemptStatus;
  789. } else {
  790. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_IMAGE_AUTH_FAILURE;
  791. }
  792. goto cleanup;
  793. }
  794. //
  795. // Check to make sure index is 1
  796. //
  797. if (ImageIndex != 1) {
  798. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckImage() - Image Index Invalid.\n", mImageIdName));
  799. *ImageUpdatable = IMAGE_UPDATABLE_INVALID_TYPE;
  800. Status = EFI_INVALID_PARAMETER;
  801. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_INVALID_IMAGE_INDEX;
  802. goto cleanup;
  803. }
  804. //
  805. // Get the dependency from Image.
  806. //
  807. Dependencies = GetImageDependency (
  808. (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
  809. ImageSize,
  810. &DependenciesSize,
  811. LastAttemptStatus
  812. );
  813. if (*LastAttemptStatus != LAST_ATTEMPT_STATUS_SUCCESS) {
  814. Status = EFI_ABORTED;
  815. goto cleanup;
  816. }
  817. //
  818. // Check the FmpPayloadHeader
  819. //
  820. FmpPayloadHeader = GetFmpHeader ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize);
  821. if (FmpPayloadHeader == NULL) {
  822. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpHeader failed.\n", mImageIdName));
  823. Status = EFI_ABORTED;
  824. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER;
  825. goto cleanup;
  826. }
  827. Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);
  828. if (EFI_ERROR (Status)) {
  829. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", mImageIdName, Status));
  830. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  831. Status = EFI_SUCCESS;
  832. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_VERSION;
  833. goto cleanup;
  834. }
  835. //
  836. // Check the lowest supported version
  837. //
  838. if (Version < Private->Descriptor.LowestSupportedImageVersion) {
  839. DEBUG (
  840. (DEBUG_ERROR,
  841. "FmpDxe(%s): CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
  842. mImageIdName, Version, Private->Descriptor.LowestSupportedImageVersion)
  843. );
  844. *ImageUpdatable = IMAGE_UPDATABLE_INVALID_OLD;
  845. Status = EFI_SUCCESS;
  846. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_VERSION_TOO_LOW;
  847. goto cleanup;
  848. }
  849. //
  850. // Evaluate dependency expression
  851. //
  852. Private->DependenciesSatisfied = CheckFmpDependency (
  853. Private->Descriptor.ImageTypeId,
  854. Version,
  855. Dependencies,
  856. DependenciesSize,
  857. &LocalLastAttemptStatus
  858. );
  859. if (!Private->DependenciesSatisfied) {
  860. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - Dependency check failed.\n", mImageIdName));
  861. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  862. Status = EFI_SUCCESS;
  863. *LastAttemptStatus = LocalLastAttemptStatus;
  864. goto cleanup;
  865. }
  866. //
  867. // Get the FmpHeaderSize so we can determine the real payload size
  868. //
  869. Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);
  870. if (EFI_ERROR (Status)) {
  871. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
  872. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  873. Status = EFI_SUCCESS;
  874. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_SIZE;
  875. goto cleanup;
  876. }
  877. //
  878. // Call FmpDevice Lib Check Image on the
  879. // Raw payload. So all headers need stripped off
  880. //
  881. AllHeaderSize = GetAllHeaderSize ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize + DependenciesSize);
  882. if (AllHeaderSize == 0) {
  883. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - GetAllHeaderSize failed.\n", mImageIdName));
  884. Status = EFI_ABORTED;
  885. *LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_ALL_HEADER_SIZE;
  886. goto cleanup;
  887. }
  888. RawSize = ImageSize - AllHeaderSize;
  889. //
  890. // FmpDeviceLib CheckImage function to do any specific checks
  891. //
  892. Status = FmpDeviceCheckImageWithStatus ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdatable, LastAttemptStatus);
  893. if (EFI_ERROR (Status)) {
  894. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", mImageIdName, Status));
  895. //
  896. // LastAttemptStatus returned from the device library should fall within the designated error range
  897. // [LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE, LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE]
  898. //
  899. if ((*LastAttemptStatus < LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE) ||
  900. (*LastAttemptStatus > LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE))
  901. {
  902. DEBUG (
  903. (DEBUG_ERROR,
  904. "FmpDxe(%s): CheckTheImage() - LastAttemptStatus %d from FmpDeviceCheckImageWithStatus() is invalid.\n",
  905. mImageIdName,
  906. *LastAttemptStatus)
  907. );
  908. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  909. }
  910. }
  911. cleanup:
  912. return Status;
  913. }
  914. /**
  915. Checks if the firmware image is valid for the device.
  916. This function allows firmware update application to validate the firmware image without
  917. invoking the SetImage() first.
  918. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  919. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  920. The number is between 1 and DescriptorCount.
  921. @param[in] Image Points to the new image.
  922. @param[in] ImageSize Size of the new image in bytes.
  923. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  924. if available, additional information if the image is invalid.
  925. @retval EFI_SUCCESS The image was successfully checked.
  926. @retval EFI_ABORTED The operation is aborted.
  927. @retval EFI_INVALID_PARAMETER The Image was NULL.
  928. @retval EFI_UNSUPPORTED The operation is not supported.
  929. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  930. **/
  931. EFI_STATUS
  932. EFIAPI
  933. CheckTheImage (
  934. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  935. IN UINT8 ImageIndex,
  936. IN CONST VOID *Image,
  937. IN UINTN ImageSize,
  938. OUT UINT32 *ImageUpdatable
  939. )
  940. {
  941. UINT32 LastAttemptStatus;
  942. return CheckTheImageInternal (This, ImageIndex, Image, ImageSize, ImageUpdatable, &LastAttemptStatus);
  943. }
  944. /**
  945. Updates the firmware image of the device.
  946. This function updates the hardware with the new firmware image.
  947. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  948. If the firmware image is updatable, the function should perform the following minimal validations
  949. before proceeding to do the firmware image update.
  950. - Validate the image authentication if image has attribute
  951. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  952. EFI_SECURITY_VIOLATION if the validation fails.
  953. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  954. the image is unsupported. The function can optionally provide more detailed information on
  955. why the image is not a supported image.
  956. - Validate the data from VendorCode if not null. Image validation must be performed before
  957. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  958. validation failed. The function returns EFI_ABORTED if the data is invalid.
  959. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  960. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  961. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  962. firmware image version is older than the current firmware image version or bad image checksum.
  963. Sensitive operations such as those wiping the entire firmware image and render the device to be
  964. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  965. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  966. reason to the caller.
  967. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  968. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  969. The number is between 1 and DescriptorCount.
  970. @param[in] Image Points to the new image.
  971. @param[in] ImageSize Size of the new image in bytes.
  972. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  973. Null indicates the caller did not specify the policy or use the default policy.
  974. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  975. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  976. details for the aborted operation. The buffer is allocated by this function
  977. with AllocatePool(), and it is the caller's responsibility to free it with a
  978. call to FreePool().
  979. @retval EFI_SUCCESS The device was successfully updated with the new image.
  980. @retval EFI_ABORTED The operation is aborted.
  981. @retval EFI_INVALID_PARAMETER The Image was NULL.
  982. @retval EFI_UNSUPPORTED The operation is not supported.
  983. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  984. **/
  985. EFI_STATUS
  986. EFIAPI
  987. SetTheImage (
  988. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  989. IN UINT8 ImageIndex,
  990. IN CONST VOID *Image,
  991. IN UINTN ImageSize,
  992. IN CONST VOID *VendorCode,
  993. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  994. OUT CHAR16 **AbortReason
  995. )
  996. {
  997. EFI_STATUS Status;
  998. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  999. UINT32 Updateable;
  1000. BOOLEAN BooleanValue;
  1001. UINT32 FmpHeaderSize;
  1002. VOID *FmpHeader;
  1003. UINTN FmpPayloadSize;
  1004. UINT32 AllHeaderSize;
  1005. UINT32 IncomingFwVersion;
  1006. UINT32 LastAttemptStatus;
  1007. UINT32 Version;
  1008. UINT32 LowestSupportedVersion;
  1009. EFI_FIRMWARE_IMAGE_DEP *Dependencies;
  1010. UINT32 DependenciesSize;
  1011. Status = EFI_SUCCESS;
  1012. Private = NULL;
  1013. Updateable = 0;
  1014. BooleanValue = FALSE;
  1015. FmpHeaderSize = 0;
  1016. FmpHeader = NULL;
  1017. FmpPayloadSize = 0;
  1018. AllHeaderSize = 0;
  1019. IncomingFwVersion = 0;
  1020. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  1021. Dependencies = NULL;
  1022. DependenciesSize = 0;
  1023. if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
  1024. return EFI_UNSUPPORTED;
  1025. }
  1026. if (This == NULL) {
  1027. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - This is NULL.\n", mImageIdName));
  1028. Status = EFI_INVALID_PARAMETER;
  1029. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROTOCOL_ARG_MISSING;
  1030. goto cleanup;
  1031. }
  1032. //
  1033. // Retrieve the private context structure
  1034. //
  1035. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
  1036. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1037. //
  1038. // Make sure the descriptor has already been loaded or refreshed
  1039. //
  1040. PopulateDescriptor (Private);
  1041. //
  1042. // Set to 0 to clear any previous results.
  1043. //
  1044. SetLastAttemptVersionInVariable (Private, IncomingFwVersion);
  1045. //
  1046. // if we have locked the device, then skip the set operation.
  1047. // it should be blocked by hardware too but we can catch here even faster
  1048. //
  1049. if (Private->FmpDeviceLocked) {
  1050. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Device is already locked. Can't update.\n", mImageIdName));
  1051. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_DEVICE_LOCKED;
  1052. Status = EFI_UNSUPPORTED;
  1053. goto cleanup;
  1054. }
  1055. //
  1056. // Call check image to verify the image
  1057. //
  1058. Status = CheckTheImageInternal (This, ImageIndex, Image, ImageSize, &Updateable, &LastAttemptStatus);
  1059. if (EFI_ERROR (Status)) {
  1060. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Check The Image failed with %r.\n", mImageIdName, Status));
  1061. goto cleanup;
  1062. }
  1063. //
  1064. // Get the dependency from Image.
  1065. //
  1066. Dependencies = GetImageDependency ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &DependenciesSize, &LastAttemptStatus);
  1067. //
  1068. // No functional error in CheckTheImage. Attempt to get the Version to
  1069. // support better error reporting.
  1070. //
  1071. FmpHeader = GetFmpHeader ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize);
  1072. if (FmpHeader == NULL) {
  1073. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpHeader failed.\n", mImageIdName));
  1074. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER;
  1075. Status = EFI_ABORTED;
  1076. goto cleanup;
  1077. }
  1078. Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncomingFwVersion);
  1079. if (!EFI_ERROR (Status)) {
  1080. //
  1081. // Set to actual value
  1082. //
  1083. SetLastAttemptVersionInVariable (Private, IncomingFwVersion);
  1084. }
  1085. if (Updateable != IMAGE_UPDATABLE_VALID) {
  1086. DEBUG (
  1087. (DEBUG_ERROR,
  1088. "FmpDxe(%s): SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
  1089. mImageIdName, Updateable)
  1090. );
  1091. if (Private->DependenciesSatisfied == FALSE) {
  1092. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSATISFIED_DEPENDENCIES;
  1093. }
  1094. Status = EFI_ABORTED;
  1095. goto cleanup;
  1096. }
  1097. if (Progress == NULL) {
  1098. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Invalid progress callback\n", mImageIdName));
  1099. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROGRESS_CALLBACK_ERROR;
  1100. Status = EFI_INVALID_PARAMETER;
  1101. goto cleanup;
  1102. }
  1103. mProgressFunc = Progress;
  1104. //
  1105. // Checking the image is at least 1%
  1106. //
  1107. Status = Progress (1);
  1108. if (EFI_ERROR (Status)) {
  1109. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Progress Callback failed with Status %r.\n", mImageIdName, Status));
  1110. }
  1111. //
  1112. // Check System Power
  1113. //
  1114. Status = CheckSystemPower (&BooleanValue);
  1115. if (EFI_ERROR (Status)) {
  1116. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemPower - API call failed %r.\n", mImageIdName, Status));
  1117. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_POWER_API;
  1118. goto cleanup;
  1119. }
  1120. if (!BooleanValue) {
  1121. Status = EFI_ABORTED;
  1122. DEBUG (
  1123. (DEBUG_ERROR,
  1124. "FmpDxe(%s): SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n", mImageIdName)
  1125. );
  1126. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;
  1127. goto cleanup;
  1128. }
  1129. Progress (2);
  1130. //
  1131. // Check System Thermal
  1132. //
  1133. Status = CheckSystemThermal (&BooleanValue);
  1134. if (EFI_ERROR (Status)) {
  1135. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemThermal - API call failed %r.\n", mImageIdName, Status));
  1136. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_SYS_THERMAL_API;
  1137. goto cleanup;
  1138. }
  1139. if (!BooleanValue) {
  1140. Status = EFI_ABORTED;
  1141. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_THERMAL;
  1142. DEBUG (
  1143. (DEBUG_ERROR,
  1144. "FmpDxe(%s): SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n", mImageIdName)
  1145. );
  1146. goto cleanup;
  1147. }
  1148. Progress (3);
  1149. //
  1150. // Check System Environment
  1151. //
  1152. Status = CheckSystemEnvironment (&BooleanValue);
  1153. if (EFI_ERROR (Status)) {
  1154. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", mImageIdName, Status));
  1155. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_SYS_ENV_API;
  1156. goto cleanup;
  1157. }
  1158. if (!BooleanValue) {
  1159. Status = EFI_ABORTED;
  1160. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_SYSTEM_ENV;
  1161. DEBUG (
  1162. (DEBUG_ERROR,
  1163. "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n", mImageIdName)
  1164. );
  1165. goto cleanup;
  1166. }
  1167. Progress (4);
  1168. //
  1169. // Save LastAttemptStatus as error so that if SetImage never returns the error
  1170. // state is recorded.
  1171. //
  1172. SetLastAttemptStatusInVariable (Private, LastAttemptStatus);
  1173. //
  1174. // Strip off all the headers so the device can process its firmware
  1175. //
  1176. Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);
  1177. if (EFI_ERROR (Status)) {
  1178. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", mImageIdName, Status));
  1179. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_SIZE;
  1180. goto cleanup;
  1181. }
  1182. AllHeaderSize = GetAllHeaderSize ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize + DependenciesSize);
  1183. if (AllHeaderSize == 0) {
  1184. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetAllHeaderSize failed.\n", mImageIdName));
  1185. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_ALL_HEADER_SIZE;
  1186. Status = EFI_ABORTED;
  1187. goto cleanup;
  1188. }
  1189. //
  1190. // Indicate that control is handed off to FmpDeviceLib
  1191. //
  1192. Progress (5);
  1193. //
  1194. // Copy the requested image to the firmware using the FmpDeviceLib
  1195. //
  1196. Status = FmpDeviceSetImageWithStatus (
  1197. (((UINT8 *)Image) + AllHeaderSize),
  1198. ImageSize - AllHeaderSize,
  1199. VendorCode,
  1200. FmpDxeProgress,
  1201. IncomingFwVersion,
  1202. AbortReason,
  1203. &LastAttemptStatus
  1204. );
  1205. if (EFI_ERROR (Status)) {
  1206. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", mImageIdName, Status));
  1207. //
  1208. // LastAttemptStatus returned from the device library should fall within the designated error range
  1209. // [LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE, LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE]
  1210. //
  1211. if ((LastAttemptStatus < LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE) ||
  1212. (LastAttemptStatus > LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE))
  1213. {
  1214. DEBUG (
  1215. (DEBUG_ERROR,
  1216. "FmpDxe(%s): SetTheImage() - LastAttemptStatus %d from FmpDeviceSetImageWithStatus() is invalid.\n",
  1217. mImageIdName,
  1218. LastAttemptStatus)
  1219. );
  1220. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  1221. }
  1222. goto cleanup;
  1223. }
  1224. //
  1225. // Store the dependency
  1226. //
  1227. if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {
  1228. Status = SaveFmpDependency (Dependencies, DependenciesSize);
  1229. if (EFI_ERROR (Status)) {
  1230. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SaveFmpDependency from FmpDependencyCheckLib failed. (%r)\n", mImageIdName, Status));
  1231. }
  1232. Status = EFI_SUCCESS;
  1233. }
  1234. //
  1235. // Finished the update without error
  1236. // Indicate that control has been returned from FmpDeviceLib
  1237. //
  1238. Progress (99);
  1239. //
  1240. // Update the version stored in variable
  1241. //
  1242. if (!Private->RuntimeVersionSupported) {
  1243. Version = DEFAULT_VERSION;
  1244. GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);
  1245. SetVersionInVariable (Private, Version);
  1246. }
  1247. //
  1248. // Update lowest supported variable
  1249. //
  1250. LowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  1251. GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &LowestSupportedVersion);
  1252. SetLowestSupportedVersionInVariable (Private, LowestSupportedVersion);
  1253. LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  1254. cleanup:
  1255. mProgressFunc = NULL;
  1256. if (Private != NULL) {
  1257. DEBUG ((DEBUG_INFO, "FmpDxe(%s): SetTheImage() LastAttemptStatus: %u.\n", mImageIdName, LastAttemptStatus));
  1258. SetLastAttemptStatusInVariable (Private, LastAttemptStatus);
  1259. }
  1260. if (Progress != NULL) {
  1261. //
  1262. // Set progress to 100 after everything is done including recording Status.
  1263. //
  1264. Progress (100);
  1265. }
  1266. //
  1267. // Need repopulate after SetImage is called to
  1268. // update LastAttemptVersion and LastAttemptStatus.
  1269. //
  1270. if (Private != NULL) {
  1271. Private->DescriptorPopulated = FALSE;
  1272. }
  1273. return Status;
  1274. }
  1275. /**
  1276. Returns information about the firmware package.
  1277. This function returns package information.
  1278. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  1279. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  1280. The format is vendor specific and new version must have a greater value
  1281. than the old version. If PackageVersion is not supported, the value is
  1282. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
  1283. comparison is to be performed using PackageVersionName. A value of
  1284. 0xFFFFFFFD indicates that package version update is in progress.
  1285. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
  1286. the package version name. The buffer is allocated by this function with
  1287. AllocatePool(), and it is the caller's responsibility to free it with a
  1288. call to FreePool().
  1289. @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
  1290. package version name. A value of 0 indicates the device does not support
  1291. update of package version name. Length is the number of Unicode characters,
  1292. including the terminating null character.
  1293. @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
  1294. Definitions' for possible returned values of this parameter. A value of 1
  1295. indicates the attribute is supported and the current setting value is
  1296. indicated in AttributesSetting. A value of 0 indicates the attribute is not
  1297. supported and the current setting value in AttributesSetting is meaningless.
  1298. @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
  1299. values of this parameter
  1300. @retval EFI_SUCCESS The package information was successfully returned.
  1301. @retval EFI_UNSUPPORTED The operation is not supported.
  1302. **/
  1303. EFI_STATUS
  1304. EFIAPI
  1305. GetPackageInfo (
  1306. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1307. OUT UINT32 *PackageVersion,
  1308. OUT CHAR16 **PackageVersionName,
  1309. OUT UINT32 *PackageVersionNameMaxLen,
  1310. OUT UINT64 *AttributesSupported,
  1311. OUT UINT64 *AttributesSetting
  1312. )
  1313. {
  1314. return EFI_UNSUPPORTED;
  1315. }
  1316. /**
  1317. Updates information about the firmware package.
  1318. This function updates package information.
  1319. This function returns EFI_UNSUPPORTED if the package information is not updatable.
  1320. VendorCode enables vendor to implement vendor-specific package information update policy.
  1321. Null if the caller did not specify this policy or use the default policy.
  1322. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  1323. @param[in] Image Points to the authentication image.
  1324. Null if authentication is not required.
  1325. @param[in] ImageSize Size of the authentication image in bytes.
  1326. 0 if authentication is not required.
  1327. @param[in] VendorCode This enables vendor to implement vendor-specific firmware
  1328. image update policy.
  1329. Null indicates the caller did not specify this policy or use
  1330. the default policy.
  1331. @param[in] PackageVersion The new package version.
  1332. @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
  1333. the package version name.
  1334. The string length is equal to or less than the value returned in
  1335. PackageVersionNameMaxLen.
  1336. @retval EFI_SUCCESS The device was successfully updated with the new package
  1337. information.
  1338. @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
  1339. returned in PackageVersionNameMaxLen.
  1340. @retval EFI_UNSUPPORTED The operation is not supported.
  1341. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  1342. **/
  1343. EFI_STATUS
  1344. EFIAPI
  1345. SetPackageInfo (
  1346. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1347. IN CONST VOID *Image,
  1348. IN UINTN ImageSize,
  1349. IN CONST VOID *VendorCode,
  1350. IN UINT32 PackageVersion,
  1351. IN CONST CHAR16 *PackageVersionName
  1352. )
  1353. {
  1354. return EFI_UNSUPPORTED;
  1355. }
  1356. /**
  1357. Event notification function that is invoked when the event GUID specified by
  1358. PcdFmpDeviceLockEventGuid is signaled.
  1359. @param[in] Event Event whose notification function is being invoked.
  1360. @param[in] Context The pointer to the notification function's context,
  1361. which is implementation-dependent.
  1362. **/
  1363. VOID
  1364. EFIAPI
  1365. FmpDxeLockEventNotify (
  1366. IN EFI_EVENT Event,
  1367. IN VOID *Context
  1368. )
  1369. {
  1370. EFI_STATUS Status;
  1371. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1372. if (Context == NULL) {
  1373. ASSERT (Context != NULL);
  1374. return;
  1375. }
  1376. Private = (FIRMWARE_MANAGEMENT_PRIVATE_DATA *)Context;
  1377. if (!Private->FmpDeviceLocked) {
  1378. //
  1379. // Lock the firmware device
  1380. //
  1381. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1382. Status = FmpDeviceLock ();
  1383. if (EFI_ERROR (Status)) {
  1384. if (Status != EFI_UNSUPPORTED) {
  1385. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));
  1386. } else {
  1387. DEBUG ((DEBUG_WARN, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));
  1388. }
  1389. }
  1390. Private->FmpDeviceLocked = TRUE;
  1391. }
  1392. }
  1393. /**
  1394. Function to install FMP instance.
  1395. @param[in] Handle The device handle to install a FMP instance on.
  1396. @retval EFI_SUCCESS FMP Installed
  1397. @retval EFI_INVALID_PARAMETER Handle was invalid
  1398. @retval other Error installing FMP
  1399. **/
  1400. EFI_STATUS
  1401. EFIAPI
  1402. InstallFmpInstance (
  1403. IN EFI_HANDLE Handle
  1404. )
  1405. {
  1406. EFI_STATUS Status;
  1407. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  1408. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1409. //
  1410. // Only allow a single FMP Protocol instance to be installed
  1411. //
  1412. Status = gBS->OpenProtocol (
  1413. Handle,
  1414. &gEfiFirmwareManagementProtocolGuid,
  1415. (VOID **)&Fmp,
  1416. NULL,
  1417. NULL,
  1418. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1419. );
  1420. if (!EFI_ERROR (Status)) {
  1421. return EFI_ALREADY_STARTED;
  1422. }
  1423. //
  1424. // Allocate FMP Protocol instance
  1425. //
  1426. Private = AllocateCopyPool (
  1427. sizeof (mFirmwareManagementPrivateDataTemplate),
  1428. &mFirmwareManagementPrivateDataTemplate
  1429. );
  1430. if (Private == NULL) {
  1431. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to allocate memory for private structure.\n", mImageIdName));
  1432. Status = EFI_OUT_OF_RESOURCES;
  1433. goto cleanup;
  1434. }
  1435. //
  1436. // Initialize private context data structure
  1437. //
  1438. Private->Handle = Handle;
  1439. Private->FmpDeviceContext = NULL;
  1440. Status = FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1441. if (Status == EFI_UNSUPPORTED) {
  1442. Private->FmpDeviceContext = NULL;
  1443. } else if (EFI_ERROR (Status)) {
  1444. goto cleanup;
  1445. }
  1446. //
  1447. // Make sure the descriptor has already been loaded or refreshed
  1448. //
  1449. PopulateDescriptor (Private);
  1450. if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
  1451. //
  1452. // Register all UEFI Variables used by this module to be locked.
  1453. //
  1454. Status = LockAllFmpVariables (Private);
  1455. if (EFI_ERROR (Status)) {
  1456. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register variables to lock. Status = %r.\n", mImageIdName, Status));
  1457. } else {
  1458. DEBUG ((DEBUG_INFO, "FmpDxe(%s): All variables registered to lock\n", mImageIdName));
  1459. }
  1460. //
  1461. // Create and register notify function to lock the FMP device.
  1462. //
  1463. Status = gBS->CreateEventEx (
  1464. EVT_NOTIFY_SIGNAL,
  1465. TPL_CALLBACK,
  1466. FmpDxeLockEventNotify,
  1467. Private,
  1468. mLockGuid,
  1469. &Private->FmpDeviceLockEvent
  1470. );
  1471. if (EFI_ERROR (Status)) {
  1472. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register notification. Status = %r\n", mImageIdName, Status));
  1473. }
  1474. ASSERT_EFI_ERROR (Status);
  1475. } else {
  1476. DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): Not registering notification to call FmpDeviceLock() because mfg mode\n", mImageIdName));
  1477. }
  1478. //
  1479. // Install FMP Protocol and FMP Progress Protocol
  1480. //
  1481. Status = gBS->InstallMultipleProtocolInterfaces (
  1482. &Private->Handle,
  1483. &gEfiFirmwareManagementProtocolGuid,
  1484. &Private->Fmp,
  1485. &gEdkiiFirmwareManagementProgressProtocolGuid,
  1486. &mFmpProgress,
  1487. NULL
  1488. );
  1489. if (EFI_ERROR (Status)) {
  1490. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol install error. Status = %r.\n", mImageIdName, Status));
  1491. goto cleanup;
  1492. }
  1493. cleanup:
  1494. if (EFI_ERROR (Status)) {
  1495. if (Private != NULL) {
  1496. if (Private->FmpDeviceLockEvent != NULL) {
  1497. gBS->CloseEvent (Private->FmpDeviceLockEvent);
  1498. }
  1499. if (Private->Descriptor.VersionName != NULL) {
  1500. FreePool (Private->Descriptor.VersionName);
  1501. }
  1502. if (Private->FmpDeviceContext != NULL) {
  1503. FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);
  1504. }
  1505. if (Private->VersionVariableName != NULL) {
  1506. FreePool (Private->VersionVariableName);
  1507. }
  1508. if (Private->LsvVariableName != NULL) {
  1509. FreePool (Private->LsvVariableName);
  1510. }
  1511. if (Private->LastAttemptStatusVariableName != NULL) {
  1512. FreePool (Private->LastAttemptStatusVariableName);
  1513. }
  1514. if (Private->LastAttemptVersionVariableName != NULL) {
  1515. FreePool (Private->LastAttemptVersionVariableName);
  1516. }
  1517. if (Private->FmpStateVariableName != NULL) {
  1518. FreePool (Private->FmpStateVariableName);
  1519. }
  1520. FreePool (Private);
  1521. }
  1522. }
  1523. return Status;
  1524. }
  1525. /**
  1526. Function to uninstall FMP instance.
  1527. @param[in] Handle The device handle to install a FMP instance on.
  1528. @retval EFI_SUCCESS FMP Installed
  1529. @retval EFI_INVALID_PARAMETER Handle was invalid
  1530. @retval other Error installing FMP
  1531. **/
  1532. EFI_STATUS
  1533. EFIAPI
  1534. UninstallFmpInstance (
  1535. IN EFI_HANDLE Handle
  1536. )
  1537. {
  1538. EFI_STATUS Status;
  1539. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  1540. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1541. Status = gBS->OpenProtocol (
  1542. Handle,
  1543. &gEfiFirmwareManagementProtocolGuid,
  1544. (VOID **)&Fmp,
  1545. NULL,
  1546. NULL,
  1547. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1548. );
  1549. if (EFI_ERROR (Status)) {
  1550. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol open error. Status = %r.\n", mImageIdName, Status));
  1551. return Status;
  1552. }
  1553. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (Fmp);
  1554. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1555. if (Private->FmpDeviceLockEvent != NULL) {
  1556. gBS->CloseEvent (Private->FmpDeviceLockEvent);
  1557. }
  1558. Status = gBS->UninstallMultipleProtocolInterfaces (
  1559. Private->Handle,
  1560. &gEfiFirmwareManagementProtocolGuid,
  1561. &Private->Fmp,
  1562. &gEdkiiFirmwareManagementProgressProtocolGuid,
  1563. &mFmpProgress,
  1564. NULL
  1565. );
  1566. if (EFI_ERROR (Status)) {
  1567. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol uninstall error. Status = %r.\n", mImageIdName, Status));
  1568. return Status;
  1569. }
  1570. if (Private->Descriptor.VersionName != NULL) {
  1571. FreePool (Private->Descriptor.VersionName);
  1572. }
  1573. if (Private->FmpDeviceContext != NULL) {
  1574. FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);
  1575. }
  1576. if (Private->VersionVariableName != NULL) {
  1577. FreePool (Private->VersionVariableName);
  1578. }
  1579. if (Private->LsvVariableName != NULL) {
  1580. FreePool (Private->LsvVariableName);
  1581. }
  1582. if (Private->LastAttemptStatusVariableName != NULL) {
  1583. FreePool (Private->LastAttemptStatusVariableName);
  1584. }
  1585. if (Private->LastAttemptVersionVariableName != NULL) {
  1586. FreePool (Private->LastAttemptVersionVariableName);
  1587. }
  1588. if (Private->FmpStateVariableName != NULL) {
  1589. FreePool (Private->FmpStateVariableName);
  1590. }
  1591. FreePool (Private);
  1592. return EFI_SUCCESS;
  1593. }
  1594. /**
  1595. Unloads the application and its installed protocol.
  1596. @param ImageHandle Handle that identifies the image to be unloaded.
  1597. @param SystemTable The system table.
  1598. @retval EFI_SUCCESS The image has been unloaded.
  1599. **/
  1600. EFI_STATUS
  1601. EFIAPI
  1602. FmpDxeLibDestructor (
  1603. IN EFI_HANDLE ImageHandle,
  1604. IN EFI_SYSTEM_TABLE *SystemTable
  1605. )
  1606. {
  1607. if (mFmpSingleInstance) {
  1608. return UninstallFmpInstance (ImageHandle);
  1609. }
  1610. return EFI_SUCCESS;
  1611. }
  1612. /**
  1613. Main entry for this driver/library.
  1614. @param[in] ImageHandle Image handle this driver.
  1615. @param[in] SystemTable Pointer to SystemTable.
  1616. **/
  1617. EFI_STATUS
  1618. EFIAPI
  1619. FmpDxeEntryPoint (
  1620. IN EFI_HANDLE ImageHandle,
  1621. IN EFI_SYSTEM_TABLE *SystemTable
  1622. )
  1623. {
  1624. EFI_STATUS Status;
  1625. //
  1626. // Verify that a new FILE_GUID value has been provided in the <Defines>
  1627. // section of this module. The FILE_GUID is the ESRT GUID that must be
  1628. // unique for each updatable firmware image.
  1629. //
  1630. if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {
  1631. DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
  1632. ASSERT (FALSE);
  1633. return EFI_UNSUPPORTED;
  1634. }
  1635. //
  1636. // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
  1637. //
  1638. mImageIdName = (CHAR16 *)PcdGetPtr (PcdFmpDeviceImageIdName);
  1639. if ((PcdGetSize (PcdFmpDeviceImageIdName) <= 2) || (mImageIdName[0] == 0)) {
  1640. //
  1641. // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
  1642. //
  1643. DEBUG ((DEBUG_ERROR, "FmpDxe(%g): PcdFmpDeviceImageIdName is an empty string.\n", &gEfiCallerIdGuid));
  1644. ASSERT (FALSE);
  1645. return EFI_UNSUPPORTED;
  1646. }
  1647. //
  1648. // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
  1649. //
  1650. DetectTestKey ();
  1651. //
  1652. // Fill in FMP Progress Protocol fields for Version 1
  1653. //
  1654. mFmpProgress.Version = 1;
  1655. mFmpProgress.ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);
  1656. mFmpProgress.WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);
  1657. // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
  1658. // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
  1659. // gEfiEndOfDxeEventGroupGuid is used.
  1660. //
  1661. mLockGuid = &gEfiEndOfDxeEventGroupGuid;
  1662. if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {
  1663. mLockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);
  1664. }
  1665. DEBUG ((DEBUG_INFO, "FmpDxe(%s): Lock GUID: %g\n", mImageIdName, mLockGuid));
  1666. //
  1667. // Register with library the install function so if the library uses
  1668. // UEFI driver model/driver binding protocol it can install FMP on its device handle
  1669. // If library is simple lib that does not use driver binding then it should return
  1670. // unsupported and this will install the FMP instance on the ImageHandle
  1671. //
  1672. Status = RegisterFmpInstaller (InstallFmpInstance);
  1673. if (Status == EFI_UNSUPPORTED) {
  1674. mFmpSingleInstance = TRUE;
  1675. DEBUG ((DEBUG_INFO, "FmpDxe(%s): FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n", mImageIdName));
  1676. Status = RegisterFmpUninstaller (UninstallFmpInstance);
  1677. if (Status == EFI_UNSUPPORTED) {
  1678. Status = InstallFmpInstance (ImageHandle);
  1679. } else {
  1680. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));
  1681. Status = EFI_UNSUPPORTED;
  1682. }
  1683. } else if (EFI_ERROR (Status)) {
  1684. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib registration returned %r. No FMP installed.\n", mImageIdName, Status));
  1685. } else {
  1686. DEBUG ((
  1687. DEBUG_INFO,
  1688. "FmpDxe(%s): FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n",
  1689. mImageIdName
  1690. ));
  1691. Status = RegisterFmpUninstaller (UninstallFmpInstance);
  1692. if (EFI_ERROR (Status)) {
  1693. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));
  1694. }
  1695. }
  1696. return Status;
  1697. }