FmpDxe.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. // The image cannot be valid if an error occurred checking the image
  895. if (*ImageUpdatable == IMAGE_UPDATABLE_VALID) {
  896. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  897. }
  898. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", mImageIdName, Status));
  899. }
  900. //
  901. // Only validate the library last attempt status code if the image is not updatable.
  902. // This specifically avoids converting LAST_ATTEMPT_STATUS_SUCCESS if it set for an updatable image.
  903. //
  904. if (*ImageUpdatable != IMAGE_UPDATABLE_VALID) {
  905. //
  906. // LastAttemptStatus returned from the device library should fall within the designated error range
  907. // [LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE, LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE]
  908. //
  909. if ((*LastAttemptStatus < LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE) ||
  910. (*LastAttemptStatus > LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE))
  911. {
  912. DEBUG ((
  913. DEBUG_ERROR,
  914. "FmpDxe(%s): CheckTheImage() - LastAttemptStatus %d from FmpDeviceCheckImageWithStatus() is invalid.\n",
  915. mImageIdName,
  916. *LastAttemptStatus
  917. ));
  918. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  919. }
  920. }
  921. cleanup:
  922. return Status;
  923. }
  924. /**
  925. Checks if the firmware image is valid for the device.
  926. This function allows firmware update application to validate the firmware image without
  927. invoking the SetImage() first.
  928. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  929. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  930. The number is between 1 and DescriptorCount.
  931. @param[in] Image Points to the new image.
  932. @param[in] ImageSize Size of the new image in bytes.
  933. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  934. if available, additional information if the image is invalid.
  935. @retval EFI_SUCCESS The image was successfully checked.
  936. @retval EFI_ABORTED The operation is aborted.
  937. @retval EFI_INVALID_PARAMETER The Image was NULL.
  938. @retval EFI_UNSUPPORTED The operation is not supported.
  939. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  940. **/
  941. EFI_STATUS
  942. EFIAPI
  943. CheckTheImage (
  944. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  945. IN UINT8 ImageIndex,
  946. IN CONST VOID *Image,
  947. IN UINTN ImageSize,
  948. OUT UINT32 *ImageUpdatable
  949. )
  950. {
  951. UINT32 LastAttemptStatus;
  952. return CheckTheImageInternal (This, ImageIndex, Image, ImageSize, ImageUpdatable, &LastAttemptStatus);
  953. }
  954. /**
  955. Updates the firmware image of the device.
  956. This function updates the hardware with the new firmware image.
  957. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  958. If the firmware image is updatable, the function should perform the following minimal validations
  959. before proceeding to do the firmware image update.
  960. - Validate the image authentication if image has attribute
  961. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  962. EFI_SECURITY_VIOLATION if the validation fails.
  963. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  964. the image is unsupported. The function can optionally provide more detailed information on
  965. why the image is not a supported image.
  966. - Validate the data from VendorCode if not null. Image validation must be performed before
  967. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  968. validation failed. The function returns EFI_ABORTED if the data is invalid.
  969. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  970. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  971. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  972. firmware image version is older than the current firmware image version or bad image checksum.
  973. Sensitive operations such as those wiping the entire firmware image and render the device to be
  974. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  975. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  976. reason to the caller.
  977. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  978. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  979. The number is between 1 and DescriptorCount.
  980. @param[in] Image Points to the new image.
  981. @param[in] ImageSize Size of the new image in bytes.
  982. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  983. Null indicates the caller did not specify the policy or use the default policy.
  984. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  985. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  986. details for the aborted operation. The buffer is allocated by this function
  987. with AllocatePool(), and it is the caller's responsibility to free it with a
  988. call to FreePool().
  989. @retval EFI_SUCCESS The device was successfully updated with the new image.
  990. @retval EFI_ABORTED The operation is aborted.
  991. @retval EFI_INVALID_PARAMETER The Image was NULL.
  992. @retval EFI_UNSUPPORTED The operation is not supported.
  993. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  994. **/
  995. EFI_STATUS
  996. EFIAPI
  997. SetTheImage (
  998. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  999. IN UINT8 ImageIndex,
  1000. IN CONST VOID *Image,
  1001. IN UINTN ImageSize,
  1002. IN CONST VOID *VendorCode,
  1003. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  1004. OUT CHAR16 **AbortReason
  1005. )
  1006. {
  1007. EFI_STATUS Status;
  1008. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1009. UINT32 Updateable;
  1010. BOOLEAN BooleanValue;
  1011. UINT32 FmpHeaderSize;
  1012. VOID *FmpHeader;
  1013. UINTN FmpPayloadSize;
  1014. UINT32 AllHeaderSize;
  1015. UINT32 IncomingFwVersion;
  1016. UINT32 LastAttemptStatus;
  1017. UINT32 Version;
  1018. UINT32 LowestSupportedVersion;
  1019. EFI_FIRMWARE_IMAGE_DEP *Dependencies;
  1020. UINT32 DependenciesSize;
  1021. Status = EFI_SUCCESS;
  1022. Private = NULL;
  1023. Updateable = 0;
  1024. BooleanValue = FALSE;
  1025. FmpHeaderSize = 0;
  1026. FmpHeader = NULL;
  1027. FmpPayloadSize = 0;
  1028. AllHeaderSize = 0;
  1029. IncomingFwVersion = 0;
  1030. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  1031. Dependencies = NULL;
  1032. DependenciesSize = 0;
  1033. if (!FeaturePcdGet (PcdFmpDeviceStorageAccessEnable)) {
  1034. return EFI_UNSUPPORTED;
  1035. }
  1036. if (This == NULL) {
  1037. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - This is NULL.\n", mImageIdName));
  1038. Status = EFI_INVALID_PARAMETER;
  1039. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROTOCOL_ARG_MISSING;
  1040. goto cleanup;
  1041. }
  1042. //
  1043. // Retrieve the private context structure
  1044. //
  1045. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (This);
  1046. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1047. //
  1048. // Make sure the descriptor has already been loaded or refreshed
  1049. //
  1050. PopulateDescriptor (Private);
  1051. //
  1052. // Set to 0 to clear any previous results.
  1053. //
  1054. SetLastAttemptVersionInVariable (Private, IncomingFwVersion);
  1055. //
  1056. // if we have locked the device, then skip the set operation.
  1057. // it should be blocked by hardware too but we can catch here even faster
  1058. //
  1059. if (Private->FmpDeviceLocked) {
  1060. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Device is already locked. Can't update.\n", mImageIdName));
  1061. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_DEVICE_LOCKED;
  1062. Status = EFI_UNSUPPORTED;
  1063. goto cleanup;
  1064. }
  1065. //
  1066. // Call check image to verify the image
  1067. //
  1068. Status = CheckTheImageInternal (This, ImageIndex, Image, ImageSize, &Updateable, &LastAttemptStatus);
  1069. if (EFI_ERROR (Status)) {
  1070. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Check The Image failed with %r.\n", mImageIdName, Status));
  1071. goto cleanup;
  1072. }
  1073. //
  1074. // Get the dependency from Image.
  1075. //
  1076. Dependencies = GetImageDependency ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &DependenciesSize, &LastAttemptStatus);
  1077. //
  1078. // No functional error in CheckTheImage. Attempt to get the Version to
  1079. // support better error reporting.
  1080. //
  1081. FmpHeader = GetFmpHeader ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, DependenciesSize, &FmpPayloadSize);
  1082. if (FmpHeader == NULL) {
  1083. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpHeader failed.\n", mImageIdName));
  1084. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER;
  1085. Status = EFI_ABORTED;
  1086. goto cleanup;
  1087. }
  1088. Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncomingFwVersion);
  1089. if (!EFI_ERROR (Status)) {
  1090. //
  1091. // Set to actual value
  1092. //
  1093. SetLastAttemptVersionInVariable (Private, IncomingFwVersion);
  1094. }
  1095. if (Updateable != IMAGE_UPDATABLE_VALID) {
  1096. DEBUG (
  1097. (DEBUG_ERROR,
  1098. "FmpDxe(%s): SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
  1099. mImageIdName, Updateable)
  1100. );
  1101. if (Private->DependenciesSatisfied == FALSE) {
  1102. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSATISFIED_DEPENDENCIES;
  1103. }
  1104. Status = EFI_ABORTED;
  1105. goto cleanup;
  1106. }
  1107. if (Progress == NULL) {
  1108. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Invalid progress callback\n", mImageIdName));
  1109. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_PROGRESS_CALLBACK_ERROR;
  1110. Status = EFI_INVALID_PARAMETER;
  1111. goto cleanup;
  1112. }
  1113. mProgressFunc = Progress;
  1114. //
  1115. // Checking the image is at least 1%
  1116. //
  1117. Status = Progress (1);
  1118. if (EFI_ERROR (Status)) {
  1119. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - Progress Callback failed with Status %r.\n", mImageIdName, Status));
  1120. }
  1121. //
  1122. // Check System Power
  1123. //
  1124. Status = CheckSystemPower (&BooleanValue);
  1125. if (EFI_ERROR (Status)) {
  1126. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemPower - API call failed %r.\n", mImageIdName, Status));
  1127. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_POWER_API;
  1128. goto cleanup;
  1129. }
  1130. if (!BooleanValue) {
  1131. Status = EFI_ABORTED;
  1132. DEBUG (
  1133. (DEBUG_ERROR,
  1134. "FmpDxe(%s): SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n", mImageIdName)
  1135. );
  1136. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;
  1137. goto cleanup;
  1138. }
  1139. Progress (2);
  1140. //
  1141. // Check System Thermal
  1142. //
  1143. Status = CheckSystemThermal (&BooleanValue);
  1144. if (EFI_ERROR (Status)) {
  1145. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemThermal - API call failed %r.\n", mImageIdName, Status));
  1146. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_SYS_THERMAL_API;
  1147. goto cleanup;
  1148. }
  1149. if (!BooleanValue) {
  1150. Status = EFI_ABORTED;
  1151. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_THERMAL;
  1152. DEBUG (
  1153. (DEBUG_ERROR,
  1154. "FmpDxe(%s): SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n", mImageIdName)
  1155. );
  1156. goto cleanup;
  1157. }
  1158. Progress (3);
  1159. //
  1160. // Check System Environment
  1161. //
  1162. Status = CheckSystemEnvironment (&BooleanValue);
  1163. if (EFI_ERROR (Status)) {
  1164. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", mImageIdName, Status));
  1165. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_CHECK_SYS_ENV_API;
  1166. goto cleanup;
  1167. }
  1168. if (!BooleanValue) {
  1169. Status = EFI_ABORTED;
  1170. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_SYSTEM_ENV;
  1171. DEBUG (
  1172. (DEBUG_ERROR,
  1173. "FmpDxe(%s): SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n", mImageIdName)
  1174. );
  1175. goto cleanup;
  1176. }
  1177. Progress (4);
  1178. //
  1179. // Save LastAttemptStatus as error so that if SetImage never returns the error
  1180. // state is recorded.
  1181. //
  1182. SetLastAttemptStatusInVariable (Private, LastAttemptStatus);
  1183. //
  1184. // Strip off all the headers so the device can process its firmware
  1185. //
  1186. Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);
  1187. if (EFI_ERROR (Status)) {
  1188. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", mImageIdName, Status));
  1189. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_FMP_HEADER_SIZE;
  1190. goto cleanup;
  1191. }
  1192. AllHeaderSize = GetAllHeaderSize ((EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize + DependenciesSize);
  1193. if (AllHeaderSize == 0) {
  1194. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() - GetAllHeaderSize failed.\n", mImageIdName));
  1195. LastAttemptStatus = LAST_ATTEMPT_STATUS_DRIVER_ERROR_GET_ALL_HEADER_SIZE;
  1196. Status = EFI_ABORTED;
  1197. goto cleanup;
  1198. }
  1199. //
  1200. // Indicate that control is handed off to FmpDeviceLib
  1201. //
  1202. Progress (5);
  1203. //
  1204. // Copy the requested image to the firmware using the FmpDeviceLib
  1205. //
  1206. Status = FmpDeviceSetImageWithStatus (
  1207. (((UINT8 *)Image) + AllHeaderSize),
  1208. ImageSize - AllHeaderSize,
  1209. VendorCode,
  1210. FmpDxeProgress,
  1211. IncomingFwVersion,
  1212. AbortReason,
  1213. &LastAttemptStatus
  1214. );
  1215. if (EFI_ERROR (Status)) {
  1216. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", mImageIdName, Status));
  1217. //
  1218. // LastAttemptStatus returned from the device library should fall within the designated error range
  1219. // [LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE, LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE]
  1220. //
  1221. if ((LastAttemptStatus < LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MIN_ERROR_CODE_VALUE) ||
  1222. (LastAttemptStatus > LAST_ATTEMPT_STATUS_DEVICE_LIBRARY_MAX_ERROR_CODE_VALUE))
  1223. {
  1224. DEBUG (
  1225. (DEBUG_ERROR,
  1226. "FmpDxe(%s): SetTheImage() - LastAttemptStatus %d from FmpDeviceSetImageWithStatus() is invalid.\n",
  1227. mImageIdName,
  1228. LastAttemptStatus)
  1229. );
  1230. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  1231. }
  1232. goto cleanup;
  1233. }
  1234. //
  1235. // Store the dependency
  1236. //
  1237. if (Private->Descriptor.AttributesSetting & IMAGE_ATTRIBUTE_DEPENDENCY) {
  1238. Status = SaveFmpDependency (Dependencies, DependenciesSize);
  1239. if (EFI_ERROR (Status)) {
  1240. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): SetTheImage() SaveFmpDependency from FmpDependencyCheckLib failed. (%r)\n", mImageIdName, Status));
  1241. }
  1242. Status = EFI_SUCCESS;
  1243. }
  1244. //
  1245. // Finished the update without error
  1246. // Indicate that control has been returned from FmpDeviceLib
  1247. //
  1248. Progress (99);
  1249. //
  1250. // Update the version stored in variable
  1251. //
  1252. if (!Private->RuntimeVersionSupported) {
  1253. Version = DEFAULT_VERSION;
  1254. GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);
  1255. SetVersionInVariable (Private, Version);
  1256. }
  1257. //
  1258. // Update lowest supported variable
  1259. //
  1260. LowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  1261. GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &LowestSupportedVersion);
  1262. SetLowestSupportedVersionInVariable (Private, LowestSupportedVersion);
  1263. LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  1264. cleanup:
  1265. mProgressFunc = NULL;
  1266. if (Private != NULL) {
  1267. DEBUG ((DEBUG_INFO, "FmpDxe(%s): SetTheImage() LastAttemptStatus: %u.\n", mImageIdName, LastAttemptStatus));
  1268. SetLastAttemptStatusInVariable (Private, LastAttemptStatus);
  1269. }
  1270. if (Progress != NULL) {
  1271. //
  1272. // Set progress to 100 after everything is done including recording Status.
  1273. //
  1274. Progress (100);
  1275. }
  1276. //
  1277. // Need repopulate after SetImage is called to
  1278. // update LastAttemptVersion and LastAttemptStatus.
  1279. //
  1280. if (Private != NULL) {
  1281. Private->DescriptorPopulated = FALSE;
  1282. }
  1283. return Status;
  1284. }
  1285. /**
  1286. Returns information about the firmware package.
  1287. This function returns package information.
  1288. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  1289. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  1290. The format is vendor specific and new version must have a greater value
  1291. than the old version. If PackageVersion is not supported, the value is
  1292. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
  1293. comparison is to be performed using PackageVersionName. A value of
  1294. 0xFFFFFFFD indicates that package version update is in progress.
  1295. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
  1296. the package version name. The buffer is allocated by this function with
  1297. AllocatePool(), and it is the caller's responsibility to free it with a
  1298. call to FreePool().
  1299. @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
  1300. package version name. A value of 0 indicates the device does not support
  1301. update of package version name. Length is the number of Unicode characters,
  1302. including the terminating null character.
  1303. @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
  1304. Definitions' for possible returned values of this parameter. A value of 1
  1305. indicates the attribute is supported and the current setting value is
  1306. indicated in AttributesSetting. A value of 0 indicates the attribute is not
  1307. supported and the current setting value in AttributesSetting is meaningless.
  1308. @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
  1309. values of this parameter
  1310. @retval EFI_SUCCESS The package information was successfully returned.
  1311. @retval EFI_UNSUPPORTED The operation is not supported.
  1312. **/
  1313. EFI_STATUS
  1314. EFIAPI
  1315. GetPackageInfo (
  1316. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1317. OUT UINT32 *PackageVersion,
  1318. OUT CHAR16 **PackageVersionName,
  1319. OUT UINT32 *PackageVersionNameMaxLen,
  1320. OUT UINT64 *AttributesSupported,
  1321. OUT UINT64 *AttributesSetting
  1322. )
  1323. {
  1324. return EFI_UNSUPPORTED;
  1325. }
  1326. /**
  1327. Updates information about the firmware package.
  1328. This function updates package information.
  1329. This function returns EFI_UNSUPPORTED if the package information is not updatable.
  1330. VendorCode enables vendor to implement vendor-specific package information update policy.
  1331. Null if the caller did not specify this policy or use the default policy.
  1332. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  1333. @param[in] Image Points to the authentication image.
  1334. Null if authentication is not required.
  1335. @param[in] ImageSize Size of the authentication image in bytes.
  1336. 0 if authentication is not required.
  1337. @param[in] VendorCode This enables vendor to implement vendor-specific firmware
  1338. image update policy.
  1339. Null indicates the caller did not specify this policy or use
  1340. the default policy.
  1341. @param[in] PackageVersion The new package version.
  1342. @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
  1343. the package version name.
  1344. The string length is equal to or less than the value returned in
  1345. PackageVersionNameMaxLen.
  1346. @retval EFI_SUCCESS The device was successfully updated with the new package
  1347. information.
  1348. @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
  1349. returned in PackageVersionNameMaxLen.
  1350. @retval EFI_UNSUPPORTED The operation is not supported.
  1351. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  1352. **/
  1353. EFI_STATUS
  1354. EFIAPI
  1355. SetPackageInfo (
  1356. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1357. IN CONST VOID *Image,
  1358. IN UINTN ImageSize,
  1359. IN CONST VOID *VendorCode,
  1360. IN UINT32 PackageVersion,
  1361. IN CONST CHAR16 *PackageVersionName
  1362. )
  1363. {
  1364. return EFI_UNSUPPORTED;
  1365. }
  1366. /**
  1367. Event notification function that is invoked when the event GUID specified by
  1368. PcdFmpDeviceLockEventGuid is signaled.
  1369. @param[in] Event Event whose notification function is being invoked.
  1370. @param[in] Context The pointer to the notification function's context,
  1371. which is implementation-dependent.
  1372. **/
  1373. VOID
  1374. EFIAPI
  1375. FmpDxeLockEventNotify (
  1376. IN EFI_EVENT Event,
  1377. IN VOID *Context
  1378. )
  1379. {
  1380. EFI_STATUS Status;
  1381. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1382. if (Context == NULL) {
  1383. ASSERT (Context != NULL);
  1384. return;
  1385. }
  1386. Private = (FIRMWARE_MANAGEMENT_PRIVATE_DATA *)Context;
  1387. if (!Private->FmpDeviceLocked) {
  1388. //
  1389. // Lock the firmware device
  1390. //
  1391. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1392. Status = FmpDeviceLock ();
  1393. if (EFI_ERROR (Status)) {
  1394. if (Status != EFI_UNSUPPORTED) {
  1395. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));
  1396. } else {
  1397. DEBUG ((DEBUG_WARN, "FmpDxe(%s): FmpDeviceLock() returned error. Status = %r\n", mImageIdName, Status));
  1398. }
  1399. }
  1400. Private->FmpDeviceLocked = TRUE;
  1401. }
  1402. }
  1403. /**
  1404. Function to install FMP instance.
  1405. @param[in] Handle The device handle to install a FMP instance on.
  1406. @retval EFI_SUCCESS FMP Installed
  1407. @retval EFI_INVALID_PARAMETER Handle was invalid
  1408. @retval other Error installing FMP
  1409. **/
  1410. EFI_STATUS
  1411. EFIAPI
  1412. InstallFmpInstance (
  1413. IN EFI_HANDLE Handle
  1414. )
  1415. {
  1416. EFI_STATUS Status;
  1417. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  1418. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1419. //
  1420. // Only allow a single FMP Protocol instance to be installed
  1421. //
  1422. Status = gBS->OpenProtocol (
  1423. Handle,
  1424. &gEfiFirmwareManagementProtocolGuid,
  1425. (VOID **)&Fmp,
  1426. NULL,
  1427. NULL,
  1428. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1429. );
  1430. if (!EFI_ERROR (Status)) {
  1431. return EFI_ALREADY_STARTED;
  1432. }
  1433. //
  1434. // Allocate FMP Protocol instance
  1435. //
  1436. Private = AllocateCopyPool (
  1437. sizeof (mFirmwareManagementPrivateDataTemplate),
  1438. &mFirmwareManagementPrivateDataTemplate
  1439. );
  1440. if (Private == NULL) {
  1441. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to allocate memory for private structure.\n", mImageIdName));
  1442. Status = EFI_OUT_OF_RESOURCES;
  1443. goto cleanup;
  1444. }
  1445. //
  1446. // Initialize private context data structure
  1447. //
  1448. Private->Handle = Handle;
  1449. Private->FmpDeviceContext = NULL;
  1450. Status = FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1451. if (Status == EFI_UNSUPPORTED) {
  1452. Private->FmpDeviceContext = NULL;
  1453. } else if (EFI_ERROR (Status)) {
  1454. goto cleanup;
  1455. }
  1456. //
  1457. // Make sure the descriptor has already been loaded or refreshed
  1458. //
  1459. PopulateDescriptor (Private);
  1460. if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
  1461. //
  1462. // Register all UEFI Variables used by this module to be locked.
  1463. //
  1464. Status = LockAllFmpVariables (Private);
  1465. if (EFI_ERROR (Status)) {
  1466. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register variables to lock. Status = %r.\n", mImageIdName, Status));
  1467. } else {
  1468. DEBUG ((DEBUG_INFO, "FmpDxe(%s): All variables registered to lock\n", mImageIdName));
  1469. }
  1470. //
  1471. // Create and register notify function to lock the FMP device.
  1472. //
  1473. Status = gBS->CreateEventEx (
  1474. EVT_NOTIFY_SIGNAL,
  1475. TPL_CALLBACK,
  1476. FmpDxeLockEventNotify,
  1477. Private,
  1478. mLockGuid,
  1479. &Private->FmpDeviceLockEvent
  1480. );
  1481. if (EFI_ERROR (Status)) {
  1482. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Failed to register notification. Status = %r\n", mImageIdName, Status));
  1483. }
  1484. ASSERT_EFI_ERROR (Status);
  1485. } else {
  1486. DEBUG ((DEBUG_VERBOSE, "FmpDxe(%s): Not registering notification to call FmpDeviceLock() because mfg mode\n", mImageIdName));
  1487. }
  1488. //
  1489. // Install FMP Protocol and FMP Progress Protocol
  1490. //
  1491. Status = gBS->InstallMultipleProtocolInterfaces (
  1492. &Private->Handle,
  1493. &gEfiFirmwareManagementProtocolGuid,
  1494. &Private->Fmp,
  1495. &gEdkiiFirmwareManagementProgressProtocolGuid,
  1496. &mFmpProgress,
  1497. NULL
  1498. );
  1499. if (EFI_ERROR (Status)) {
  1500. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol install error. Status = %r.\n", mImageIdName, Status));
  1501. goto cleanup;
  1502. }
  1503. cleanup:
  1504. if (EFI_ERROR (Status)) {
  1505. if (Private != NULL) {
  1506. if (Private->FmpDeviceLockEvent != NULL) {
  1507. gBS->CloseEvent (Private->FmpDeviceLockEvent);
  1508. }
  1509. if (Private->Descriptor.VersionName != NULL) {
  1510. FreePool (Private->Descriptor.VersionName);
  1511. }
  1512. if (Private->FmpDeviceContext != NULL) {
  1513. FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);
  1514. }
  1515. if (Private->VersionVariableName != NULL) {
  1516. FreePool (Private->VersionVariableName);
  1517. }
  1518. if (Private->LsvVariableName != NULL) {
  1519. FreePool (Private->LsvVariableName);
  1520. }
  1521. if (Private->LastAttemptStatusVariableName != NULL) {
  1522. FreePool (Private->LastAttemptStatusVariableName);
  1523. }
  1524. if (Private->LastAttemptVersionVariableName != NULL) {
  1525. FreePool (Private->LastAttemptVersionVariableName);
  1526. }
  1527. if (Private->FmpStateVariableName != NULL) {
  1528. FreePool (Private->FmpStateVariableName);
  1529. }
  1530. FreePool (Private);
  1531. }
  1532. }
  1533. return Status;
  1534. }
  1535. /**
  1536. Function to uninstall FMP instance.
  1537. @param[in] Handle The device handle to install a FMP instance on.
  1538. @retval EFI_SUCCESS FMP Installed
  1539. @retval EFI_INVALID_PARAMETER Handle was invalid
  1540. @retval other Error installing FMP
  1541. **/
  1542. EFI_STATUS
  1543. EFIAPI
  1544. UninstallFmpInstance (
  1545. IN EFI_HANDLE Handle
  1546. )
  1547. {
  1548. EFI_STATUS Status;
  1549. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  1550. FIRMWARE_MANAGEMENT_PRIVATE_DATA *Private;
  1551. Status = gBS->OpenProtocol (
  1552. Handle,
  1553. &gEfiFirmwareManagementProtocolGuid,
  1554. (VOID **)&Fmp,
  1555. NULL,
  1556. NULL,
  1557. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1558. );
  1559. if (EFI_ERROR (Status)) {
  1560. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol open error. Status = %r.\n", mImageIdName, Status));
  1561. return Status;
  1562. }
  1563. Private = FIRMWARE_MANAGEMENT_PRIVATE_DATA_FROM_THIS (Fmp);
  1564. FmpDeviceSetContext (Private->Handle, &Private->FmpDeviceContext);
  1565. if (Private->FmpDeviceLockEvent != NULL) {
  1566. gBS->CloseEvent (Private->FmpDeviceLockEvent);
  1567. }
  1568. Status = gBS->UninstallMultipleProtocolInterfaces (
  1569. Private->Handle,
  1570. &gEfiFirmwareManagementProtocolGuid,
  1571. &Private->Fmp,
  1572. &gEdkiiFirmwareManagementProgressProtocolGuid,
  1573. &mFmpProgress,
  1574. NULL
  1575. );
  1576. if (EFI_ERROR (Status)) {
  1577. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): Protocol uninstall error. Status = %r.\n", mImageIdName, Status));
  1578. return Status;
  1579. }
  1580. if (Private->Descriptor.VersionName != NULL) {
  1581. FreePool (Private->Descriptor.VersionName);
  1582. }
  1583. if (Private->FmpDeviceContext != NULL) {
  1584. FmpDeviceSetContext (NULL, &Private->FmpDeviceContext);
  1585. }
  1586. if (Private->VersionVariableName != NULL) {
  1587. FreePool (Private->VersionVariableName);
  1588. }
  1589. if (Private->LsvVariableName != NULL) {
  1590. FreePool (Private->LsvVariableName);
  1591. }
  1592. if (Private->LastAttemptStatusVariableName != NULL) {
  1593. FreePool (Private->LastAttemptStatusVariableName);
  1594. }
  1595. if (Private->LastAttemptVersionVariableName != NULL) {
  1596. FreePool (Private->LastAttemptVersionVariableName);
  1597. }
  1598. if (Private->FmpStateVariableName != NULL) {
  1599. FreePool (Private->FmpStateVariableName);
  1600. }
  1601. FreePool (Private);
  1602. return EFI_SUCCESS;
  1603. }
  1604. /**
  1605. Unloads the application and its installed protocol.
  1606. @param ImageHandle Handle that identifies the image to be unloaded.
  1607. @param SystemTable The system table.
  1608. @retval EFI_SUCCESS The image has been unloaded.
  1609. **/
  1610. EFI_STATUS
  1611. EFIAPI
  1612. FmpDxeLibDestructor (
  1613. IN EFI_HANDLE ImageHandle,
  1614. IN EFI_SYSTEM_TABLE *SystemTable
  1615. )
  1616. {
  1617. if (mFmpSingleInstance) {
  1618. return UninstallFmpInstance (ImageHandle);
  1619. }
  1620. return EFI_SUCCESS;
  1621. }
  1622. /**
  1623. Main entry for this driver/library.
  1624. @param[in] ImageHandle Image handle this driver.
  1625. @param[in] SystemTable Pointer to SystemTable.
  1626. **/
  1627. EFI_STATUS
  1628. EFIAPI
  1629. FmpDxeEntryPoint (
  1630. IN EFI_HANDLE ImageHandle,
  1631. IN EFI_SYSTEM_TABLE *SystemTable
  1632. )
  1633. {
  1634. EFI_STATUS Status;
  1635. //
  1636. // Verify that a new FILE_GUID value has been provided in the <Defines>
  1637. // section of this module. The FILE_GUID is the ESRT GUID that must be
  1638. // unique for each updatable firmware image.
  1639. //
  1640. if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {
  1641. DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
  1642. ASSERT (FALSE);
  1643. return EFI_UNSUPPORTED;
  1644. }
  1645. //
  1646. // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
  1647. //
  1648. mImageIdName = (CHAR16 *)PcdGetPtr (PcdFmpDeviceImageIdName);
  1649. if ((PcdGetSize (PcdFmpDeviceImageIdName) <= 2) || (mImageIdName[0] == 0)) {
  1650. //
  1651. // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
  1652. //
  1653. DEBUG ((DEBUG_ERROR, "FmpDxe(%g): PcdFmpDeviceImageIdName is an empty string.\n", &gEfiCallerIdGuid));
  1654. ASSERT (FALSE);
  1655. return EFI_UNSUPPORTED;
  1656. }
  1657. //
  1658. // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
  1659. //
  1660. DetectTestKey ();
  1661. //
  1662. // Fill in FMP Progress Protocol fields for Version 1
  1663. //
  1664. mFmpProgress.Version = 1;
  1665. mFmpProgress.ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);
  1666. mFmpProgress.WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);
  1667. // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
  1668. // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
  1669. // gEfiEndOfDxeEventGroupGuid is used.
  1670. //
  1671. mLockGuid = &gEfiEndOfDxeEventGroupGuid;
  1672. if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {
  1673. mLockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);
  1674. }
  1675. DEBUG ((DEBUG_INFO, "FmpDxe(%s): Lock GUID: %g\n", mImageIdName, mLockGuid));
  1676. //
  1677. // Register with library the install function so if the library uses
  1678. // UEFI driver model/driver binding protocol it can install FMP on its device handle
  1679. // If library is simple lib that does not use driver binding then it should return
  1680. // unsupported and this will install the FMP instance on the ImageHandle
  1681. //
  1682. Status = RegisterFmpInstaller (InstallFmpInstance);
  1683. if (Status == EFI_UNSUPPORTED) {
  1684. mFmpSingleInstance = TRUE;
  1685. DEBUG ((DEBUG_INFO, "FmpDxe(%s): FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n", mImageIdName));
  1686. Status = RegisterFmpUninstaller (UninstallFmpInstance);
  1687. if (Status == EFI_UNSUPPORTED) {
  1688. Status = InstallFmpInstance (ImageHandle);
  1689. } else {
  1690. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));
  1691. Status = EFI_UNSUPPORTED;
  1692. }
  1693. } else if (EFI_ERROR (Status)) {
  1694. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib registration returned %r. No FMP installed.\n", mImageIdName, Status));
  1695. } else {
  1696. DEBUG ((
  1697. DEBUG_INFO,
  1698. "FmpDxe(%s): FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n",
  1699. mImageIdName
  1700. ));
  1701. Status = RegisterFmpUninstaller (UninstallFmpInstance);
  1702. if (EFI_ERROR (Status)) {
  1703. DEBUG ((DEBUG_ERROR, "FmpDxe(%s): FmpDeviceLib RegisterFmpInstaller and RegisterFmpUninstaller do not match.\n", mImageIdName));
  1704. }
  1705. }
  1706. return Status;
  1707. }