FmpDxe.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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) 2016, Microsoft Corporation. All rights reserved.<BR>
  6. Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include <PiDxe.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/BaseLib.h>
  12. #include <Library/BaseMemoryLib.h>
  13. #include <Library/UefiBootServicesTableLib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include <Library/UefiLib.h>
  16. #include <Library/FmpAuthenticationLib.h>
  17. #include <Library/FmpDeviceLib.h>
  18. #include <Library/FmpPayloadHeaderLib.h>
  19. #include <Library/CapsuleUpdatePolicyLib.h>
  20. #include <Protocol/FirmwareManagement.h>
  21. #include <Protocol/FirmwareManagementProgress.h>
  22. #include <Guid/SystemResourceTable.h>
  23. #include <Guid/EventGroup.h>
  24. #include "VariableSupport.h"
  25. #define VERSION_STRING_NOT_SUPPORTED L"VERSION STRING NOT SUPPORTED"
  26. #define VERSION_STRING_NOT_AVAILABLE L"VERSION STRING NOT AVAILABLE"
  27. /**
  28. Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
  29. the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
  30. the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
  31. SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
  32. SHA256 hash matches or there is then error computing the SHA256 hash, then
  33. set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
  34. TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
  35. bytes.
  36. **/
  37. VOID
  38. DetectTestKey (
  39. VOID
  40. );
  41. ///
  42. /// FILE_GUID from FmpDxe.inf. When FmpDxe.inf is used in a platform, the
  43. /// FILE_GUID must always be overridden in the <Defines> section to provide
  44. /// the ESRT GUID value associated with the updatable firmware image. A
  45. /// check is made in this module's driver entry point to verify that a
  46. /// new FILE_GUID value has been defined.
  47. ///
  48. const EFI_GUID mDefaultModuleFileGuid = {
  49. 0x78ef0a56, 0x1cf0, 0x4535, { 0xb5, 0xda, 0xf6, 0xfd, 0x2f, 0x40, 0x5a, 0x11 }
  50. };
  51. EFI_FIRMWARE_IMAGE_DESCRIPTOR mDesc;
  52. BOOLEAN mDescriptorPopulated = FALSE;
  53. BOOLEAN mRuntimeVersionSupported = TRUE;
  54. BOOLEAN mFmpInstalled = FALSE;
  55. ///
  56. /// Function pointer to progress function
  57. ///
  58. EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS mProgressFunc = NULL;
  59. BOOLEAN mProgressSupported = FALSE;
  60. CHAR16 *mImageIdName = NULL;
  61. UINT64 mImageId = 0x1;
  62. CHAR16 *mVersionName = NULL;
  63. EFI_EVENT mFmpDeviceLockEvent;
  64. //
  65. // Indicates if an attempt has been made to lock a
  66. // FLASH storage device by calling FmpDeviceLock().
  67. // A FLASH storage device may not support being locked,
  68. // so this variable is set to TRUE even if FmpDeviceLock()
  69. // returns an error.
  70. //
  71. BOOLEAN mFmpDeviceLocked = FALSE;
  72. /**
  73. Callback function to report the process of the firmware updating.
  74. Wrap the caller's version in this so that progress from the device lib is
  75. within the expected range. Convert device lib 0% - 100% to 6% - 98%.
  76. FmpDxe 1% - 5% for validation
  77. FmpDeviceLib 6% - 98% for flashing/update
  78. FmpDxe 99% - 100% finish
  79. @param[in] Completion A value between 1 and 100 indicating the current
  80. completion progress of the firmware update. Completion
  81. progress is reported as from 1 to 100 percent. A value
  82. of 0 is used by the driver to indicate that progress
  83. reporting is not supported.
  84. @retval EFI_SUCCESS The progress was updated.
  85. @retval EFI_UNSUPPORTED Updating progress is not supported.
  86. **/
  87. EFI_STATUS
  88. EFIAPI
  89. FmpDxeProgress (
  90. IN UINTN Completion
  91. )
  92. {
  93. EFI_STATUS Status;
  94. Status = EFI_UNSUPPORTED;
  95. if (!mProgressSupported) {
  96. return Status;
  97. }
  98. if (mProgressFunc == NULL) {
  99. return Status;
  100. }
  101. //
  102. // Reserve 6% - 98% for the FmpDeviceLib. Call the real progress function.
  103. //
  104. Status = mProgressFunc (((Completion * 92) / 100) + 6);
  105. if (Status == EFI_UNSUPPORTED) {
  106. mProgressSupported = FALSE;
  107. mProgressFunc = NULL;
  108. }
  109. return Status;
  110. }
  111. /**
  112. Returns a pointer to the ImageTypeId GUID value. An attempt is made to get
  113. the GUID value from the FmpDeviceLib. If the FmpDeviceLib does not provide
  114. a GUID value, then gEfiCallerIdGuid is returned.
  115. @return The ImageTypeId GUID
  116. **/
  117. EFI_GUID *
  118. GetImageTypeIdGuid (
  119. VOID
  120. )
  121. {
  122. EFI_STATUS Status;
  123. EFI_GUID *FmpDeviceLibGuid;
  124. FmpDeviceLibGuid = NULL;
  125. Status = FmpDeviceGetImageTypeIdGuidPtr (&FmpDeviceLibGuid);
  126. if (EFI_ERROR (Status)) {
  127. if (Status != EFI_UNSUPPORTED) {
  128. DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid error %r\n", Status));
  129. }
  130. return &gEfiCallerIdGuid;
  131. }
  132. if (FmpDeviceLibGuid == NULL) {
  133. DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib GetImageTypeIdGuidPtr() returned invalid GUID\n"));
  134. return &gEfiCallerIdGuid;
  135. }
  136. return FmpDeviceLibGuid;
  137. }
  138. /**
  139. Returns a pointer to the Null-terminated Unicode ImageIdName string.
  140. @return Null-terminated Unicode ImageIdName string.
  141. **/
  142. CHAR16 *
  143. GetImageTypeNameString (
  144. VOID
  145. )
  146. {
  147. return mImageIdName;
  148. }
  149. /**
  150. Lowest supported version is a combo of three parts.
  151. 1. Check if the device lib has a lowest supported version
  152. 2. Check if we have a variable for lowest supported version (this will be updated with each capsule applied)
  153. 3. Check Fixed at build PCD
  154. Take the largest value
  155. **/
  156. UINT32
  157. GetLowestSupportedVersion (
  158. VOID
  159. )
  160. {
  161. EFI_STATUS Status;
  162. UINT32 DeviceLibLowestSupportedVersion;
  163. UINT32 VariableLowestSupportedVersion;
  164. UINT32 ReturnLsv;
  165. //
  166. // Get the LowestSupportedVersion.
  167. //
  168. if (!IsLowestSupportedVersionCheckRequired ()) {
  169. //
  170. // Any Version can pass the 0 LowestSupportedVersion check.
  171. //
  172. return 0;
  173. }
  174. ReturnLsv = PcdGet32 (PcdFmpDeviceBuildTimeLowestSupportedVersion);
  175. //
  176. // Check the FmpDeviceLib
  177. //
  178. DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  179. Status = FmpDeviceGetLowestSupportedVersion (&DeviceLibLowestSupportedVersion);
  180. if (EFI_ERROR (Status)) {
  181. DeviceLibLowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  182. }
  183. if (DeviceLibLowestSupportedVersion > ReturnLsv) {
  184. ReturnLsv = DeviceLibLowestSupportedVersion;
  185. }
  186. //
  187. // Check the lowest supported version UEFI variable for this device
  188. //
  189. VariableLowestSupportedVersion = GetLowestSupportedVersionFromVariable();
  190. if (VariableLowestSupportedVersion > ReturnLsv) {
  191. ReturnLsv = VariableLowestSupportedVersion;
  192. }
  193. //
  194. // Return the largest value
  195. //
  196. return ReturnLsv;
  197. }
  198. /**
  199. Populates the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure in the module global
  200. variable mDesc.
  201. **/
  202. VOID
  203. PopulateDescriptor (
  204. VOID
  205. )
  206. {
  207. EFI_STATUS Status;
  208. mDesc.ImageIndex = 1;
  209. CopyGuid (&mDesc.ImageTypeId, GetImageTypeIdGuid());
  210. mDesc.ImageId = mImageId;
  211. mDesc.ImageIdName = GetImageTypeNameString();
  212. //
  213. // Get the version. Some devices don't support getting the firmware version
  214. // at runtime. If FmpDeviceLib does not support returning a version, then
  215. // it is stored in a UEFI variable.
  216. //
  217. Status = FmpDeviceGetVersion (&mDesc.Version);
  218. if (Status == EFI_UNSUPPORTED) {
  219. mRuntimeVersionSupported = FALSE;
  220. mDesc.Version = GetVersionFromVariable();
  221. } else if (EFI_ERROR (Status)) {
  222. //
  223. // Unexpected error. Use default version.
  224. //
  225. DEBUG ((DEBUG_ERROR, "FmpDxe: GetVersion() from FmpDeviceLib (%s) returned %r\n", GetImageTypeNameString(), Status));
  226. mDesc.Version = DEFAULT_VERSION;
  227. }
  228. //
  229. // Free the current version name. Shouldn't really happen but this populate
  230. // function could be called multiple times (to refresh).
  231. //
  232. if (mVersionName != NULL) {
  233. FreePool (mVersionName);
  234. mVersionName = NULL;
  235. }
  236. //
  237. // Attempt to get the version string from the FmpDeviceLib
  238. //
  239. Status = FmpDeviceGetVersionString (&mVersionName);
  240. if (Status == EFI_UNSUPPORTED) {
  241. DEBUG ((DEBUG_INFO, "FmpDxe: GetVersionString() unsupported in FmpDeviceLib.\n"));
  242. mVersionName = AllocateCopyPool (
  243. sizeof (VERSION_STRING_NOT_SUPPORTED),
  244. VERSION_STRING_NOT_SUPPORTED
  245. );
  246. } else if (EFI_ERROR (Status)) {
  247. DEBUG ((DEBUG_INFO, "FmpDxe: GetVersionString() not available in FmpDeviceLib.\n"));
  248. mVersionName = AllocateCopyPool (
  249. sizeof (VERSION_STRING_NOT_AVAILABLE),
  250. VERSION_STRING_NOT_AVAILABLE
  251. );
  252. }
  253. mDesc.VersionName = mVersionName;
  254. mDesc.LowestSupportedImageVersion = GetLowestSupportedVersion();
  255. //
  256. // Get attributes from the FmpDeviceLib
  257. //
  258. FmpDeviceGetAttributes (&mDesc.AttributesSupported, &mDesc.AttributesSetting);
  259. //
  260. // Force set the updatable bits in the attributes;
  261. //
  262. mDesc.AttributesSupported |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
  263. mDesc.AttributesSetting |= IMAGE_ATTRIBUTE_IMAGE_UPDATABLE;
  264. //
  265. // Force set the authentication bits in the attributes;
  266. //
  267. mDesc.AttributesSupported |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
  268. mDesc.AttributesSetting |= (IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED);
  269. mDesc.Compatibilities = 0;
  270. //
  271. // Get the size of the firmware image from the FmpDeviceLib
  272. //
  273. Status = FmpDeviceGetSize (&mDesc.Size);
  274. if (EFI_ERROR (Status)) {
  275. mDesc.Size = 0;
  276. }
  277. mDesc.LastAttemptVersion = GetLastAttemptVersionFromVariable ();
  278. mDesc.LastAttemptStatus = GetLastAttemptStatusFromVariable ();
  279. mDescriptorPopulated = TRUE;
  280. }
  281. /**
  282. Returns information about the current firmware image(s) of the device.
  283. This function allows a copy of the current firmware image to be created and saved.
  284. The saved copy could later been used, for example, in firmware image recovery or rollback.
  285. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  286. @param[in, out] ImageInfoSize A pointer to the size, in bytes, of the ImageInfo buffer.
  287. On input, this is the size of the buffer allocated by the caller.
  288. On output, it is the size of the buffer returned by the firmware
  289. if the buffer was large enough, or the size of the buffer needed
  290. to contain the image(s) information if the buffer was too small.
  291. @param[in, out] ImageInfo A pointer to the buffer in which firmware places the current image(s)
  292. information. The information is an array of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  293. @param[out] DescriptorVersion A pointer to the location in which firmware returns the version number
  294. associated with the EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  295. @param[out] DescriptorCount A pointer to the location in which firmware returns the number of
  296. descriptors or firmware images within this device.
  297. @param[out] DescriptorSize A pointer to the location in which firmware returns the size, in bytes,
  298. of an individual EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  299. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  300. The format is vendor specific and new version must have a greater value
  301. than the old version. If PackageVersion is not supported, the value is
  302. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version comparison
  303. is to be performed using PackageVersionName. A value of 0xFFFFFFFD indicates
  304. that package version update is in progress.
  305. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing the
  306. package version name. The buffer is allocated by this function with
  307. AllocatePool(), and it is the caller's responsibility to free it with a call
  308. to FreePool().
  309. @retval EFI_SUCCESS The device was successfully updated with the new image.
  310. @retval EFI_BUFFER_TOO_SMALL The ImageInfo buffer was too small. The current buffer size
  311. needed to hold the image(s) information is returned in ImageInfoSize.
  312. @retval EFI_INVALID_PARAMETER ImageInfoSize is NULL.
  313. @retval EFI_DEVICE_ERROR Valid information could not be returned. Possible corrupted image.
  314. **/
  315. EFI_STATUS
  316. EFIAPI
  317. GetTheImageInfo (
  318. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  319. IN OUT UINTN *ImageInfoSize,
  320. IN OUT EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo,
  321. OUT UINT32 *DescriptorVersion,
  322. OUT UINT8 *DescriptorCount,
  323. OUT UINTN *DescriptorSize,
  324. OUT UINT32 *PackageVersion,
  325. OUT CHAR16 **PackageVersionName
  326. )
  327. {
  328. EFI_STATUS Status;
  329. Status = EFI_SUCCESS;
  330. //
  331. // Check for valid pointer
  332. //
  333. if (ImageInfoSize == NULL) {
  334. DEBUG ((DEBUG_ERROR, "FmpDxe: GetImageInfo() - ImageInfoSize is NULL.\n"));
  335. Status = EFI_INVALID_PARAMETER;
  336. goto cleanup;
  337. }
  338. //
  339. // Check the buffer size
  340. // NOTE: Check this first so caller can get the necessary memory size it must allocate.
  341. //
  342. if (*ImageInfoSize < (sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR))) {
  343. *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  344. DEBUG ((DEBUG_VERBOSE, "FmpDxe: GetImageInfo() - ImageInfoSize is to small.\n"));
  345. Status = EFI_BUFFER_TOO_SMALL;
  346. goto cleanup;
  347. }
  348. //
  349. // Confirm that buffer isn't null
  350. //
  351. if ( (ImageInfo == NULL) || (DescriptorVersion == NULL) || (DescriptorCount == NULL) || (DescriptorSize == NULL)
  352. || (PackageVersion == NULL)) {
  353. DEBUG ((DEBUG_ERROR, "FmpDxe: GetImageInfo() - Pointer Parameter is NULL.\n"));
  354. Status = EFI_INVALID_PARAMETER;
  355. goto cleanup;
  356. }
  357. //
  358. // Set the size to whatever we need
  359. //
  360. *ImageInfoSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  361. if (!mDescriptorPopulated) {
  362. PopulateDescriptor();
  363. }
  364. //
  365. // Copy the image descriptor
  366. //
  367. CopyMem (ImageInfo, &mDesc, sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR));
  368. *DescriptorVersion = EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION;
  369. *DescriptorCount = 1;
  370. *DescriptorSize = sizeof (EFI_FIRMWARE_IMAGE_DESCRIPTOR);
  371. //
  372. // means unsupported
  373. //
  374. *PackageVersion = 0xFFFFFFFF;
  375. //
  376. // Do not update PackageVersionName since it is not supported in this instance.
  377. //
  378. cleanup:
  379. return Status;
  380. }
  381. /**
  382. Retrieves a copy of the current firmware image of the device.
  383. This function allows a copy of the current firmware image to be created and saved.
  384. The saved copy could later been used, for example, in firmware image recovery or rollback.
  385. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  386. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  387. The number is between 1 and DescriptorCount.
  388. @param[in, out] Image Points to the buffer where the current image is copied to.
  389. @param[in, out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.
  390. On return, points to the length of the image, in bytes.
  391. @retval EFI_SUCCESS The device was successfully updated with the new image.
  392. @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the
  393. image. The current buffer size needed to hold the image is returned
  394. in ImageSize.
  395. @retval EFI_INVALID_PARAMETER The Image was NULL.
  396. @retval EFI_NOT_FOUND The current image is not copied to the buffer.
  397. @retval EFI_UNSUPPORTED The operation is not supported.
  398. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  399. **/
  400. EFI_STATUS
  401. EFIAPI
  402. GetTheImage (
  403. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  404. IN UINT8 ImageIndex,
  405. IN OUT VOID *Image,
  406. IN OUT UINTN *ImageSize
  407. )
  408. {
  409. EFI_STATUS Status;
  410. UINTN Size;
  411. Status = EFI_SUCCESS;
  412. //
  413. // Check to make sure index is 1 (only 1 image for this device)
  414. //
  415. if (ImageIndex != 1) {
  416. DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Index Invalid.\n"));
  417. Status = EFI_INVALID_PARAMETER;
  418. goto cleanup;
  419. }
  420. if (ImageSize == NULL) {
  421. DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - ImageSize Pointer Parameter is NULL.\n"));
  422. Status = EFI_INVALID_PARAMETER;
  423. goto cleanup;
  424. }
  425. //
  426. // Check the buffer size
  427. //
  428. Status = FmpDeviceGetSize (&Size);
  429. if (EFI_ERROR (Status)) {
  430. Size = 0;
  431. }
  432. if (*ImageSize < Size) {
  433. *ImageSize = Size;
  434. DEBUG ((DEBUG_VERBOSE, "FmpDxe: GetImage() - ImageSize is to small.\n"));
  435. Status = EFI_BUFFER_TOO_SMALL;
  436. goto cleanup;
  437. }
  438. if (Image == NULL) {
  439. DEBUG ((DEBUG_ERROR, "FmpDxe: GetImage() - Image Pointer Parameter is NULL.\n"));
  440. Status = EFI_INVALID_PARAMETER;
  441. goto cleanup;
  442. }
  443. Status = FmpDeviceGetImage (Image, ImageSize);
  444. cleanup:
  445. return Status;
  446. }
  447. /**
  448. Helper function to safely retrieve the FMP header from
  449. within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
  450. @param[in] Image Pointer to the image.
  451. @param[in] ImageSize Size of the image.
  452. @param[out] PayloadSize
  453. @retval !NULL Valid pointer to the header.
  454. @retval NULL Structure is bad and pointer cannot be found.
  455. **/
  456. VOID *
  457. GetFmpHeader (
  458. IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  459. IN CONST UINTN ImageSize,
  460. OUT UINTN *PayloadSize
  461. )
  462. {
  463. //
  464. // Check to make sure that operation can be safely performed.
  465. //
  466. if (((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) < (UINTN)Image || \
  467. ((UINTN)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) >= (UINTN)Image + ImageSize) {
  468. //
  469. // Pointer overflow. Invalid image.
  470. //
  471. return NULL;
  472. }
  473. *PayloadSize = ImageSize - (sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength);
  474. return (VOID *)((UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength);
  475. }
  476. /**
  477. Helper function to safely calculate the size of all headers
  478. within an EFI_FIRMWARE_IMAGE_AUTHENTICATION structure.
  479. @param[in] Image Pointer to the image.
  480. @param[in] AdditionalHeaderSize Size of any headers that cannot be calculated by this function.
  481. @retval UINT32>0 Valid size of all the headers.
  482. @retval 0 Structure is bad and size cannot be found.
  483. **/
  484. UINT32
  485. GetAllHeaderSize (
  486. IN CONST EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,
  487. IN UINT32 AdditionalHeaderSize
  488. )
  489. {
  490. UINT32 CalculatedSize;
  491. CalculatedSize = sizeof (Image->MonotonicCount) +
  492. AdditionalHeaderSize +
  493. Image->AuthInfo.Hdr.dwLength;
  494. //
  495. // Check to make sure that operation can be safely performed.
  496. //
  497. if (CalculatedSize < sizeof (Image->MonotonicCount) ||
  498. CalculatedSize < AdditionalHeaderSize ||
  499. CalculatedSize < Image->AuthInfo.Hdr.dwLength ) {
  500. //
  501. // Integer overflow. Invalid image.
  502. //
  503. return 0;
  504. }
  505. return CalculatedSize;
  506. }
  507. /**
  508. Checks if the firmware image is valid for the device.
  509. This function allows firmware update application to validate the firmware image without
  510. invoking the SetImage() first.
  511. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  512. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  513. The number is between 1 and DescriptorCount.
  514. @param[in] Image Points to the new image.
  515. @param[in] ImageSize Size of the new image in bytes.
  516. @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
  517. if available, additional information if the image is invalid.
  518. @retval EFI_SUCCESS The image was successfully checked.
  519. @retval EFI_ABORTED The operation is aborted.
  520. @retval EFI_INVALID_PARAMETER The Image was NULL.
  521. @retval EFI_UNSUPPORTED The operation is not supported.
  522. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  523. **/
  524. EFI_STATUS
  525. EFIAPI
  526. CheckTheImage (
  527. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  528. IN UINT8 ImageIndex,
  529. IN CONST VOID *Image,
  530. IN UINTN ImageSize,
  531. OUT UINT32 *ImageUpdatable
  532. )
  533. {
  534. EFI_STATUS Status;
  535. UINTN RawSize;
  536. VOID *FmpPayloadHeader;
  537. UINTN FmpPayloadSize;
  538. UINT32 Version;
  539. UINT32 FmpHeaderSize;
  540. UINTN AllHeaderSize;
  541. UINT32 Index;
  542. VOID *PublicKeyData;
  543. UINTN PublicKeyDataLength;
  544. UINT8 *PublicKeyDataXdr;
  545. UINT8 *PublicKeyDataXdrEnd;
  546. Status = EFI_SUCCESS;
  547. RawSize = 0;
  548. FmpPayloadHeader = NULL;
  549. FmpPayloadSize = 0;
  550. Version = 0;
  551. FmpHeaderSize = 0;
  552. AllHeaderSize = 0;
  553. //
  554. // make sure the descriptor has already been loaded
  555. //
  556. if (!mDescriptorPopulated) {
  557. PopulateDescriptor();
  558. }
  559. if (ImageUpdatable == NULL) {
  560. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - ImageUpdatable Pointer Parameter is NULL.\n"));
  561. Status = EFI_INVALID_PARAMETER;
  562. goto cleanup;
  563. }
  564. //
  565. //Set to valid and then if any tests fail it will update this flag.
  566. //
  567. *ImageUpdatable = IMAGE_UPDATABLE_VALID;
  568. if (Image == NULL) {
  569. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Pointer Parameter is NULL.\n"));
  570. //
  571. // not sure if this is needed
  572. //
  573. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  574. return EFI_INVALID_PARAMETER;
  575. }
  576. PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
  577. PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
  578. if (PublicKeyDataXdr == NULL || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
  579. DEBUG ((DEBUG_ERROR, "FmpDxe: Invalid certificate, skipping it.\n"));
  580. Status = EFI_ABORTED;
  581. } else {
  582. //
  583. // Try each key from PcdFmpDevicePkcs7CertBufferXdr
  584. //
  585. for (Index = 1; PublicKeyDataXdr < PublicKeyDataXdrEnd; Index++) {
  586. Index++;
  587. DEBUG (
  588. (DEBUG_INFO,
  589. "FmpDxe: Certificate #%d [%p..%p].\n",
  590. Index,
  591. PublicKeyDataXdr,
  592. PublicKeyDataXdrEnd
  593. )
  594. );
  595. if ((PublicKeyDataXdr + sizeof (UINT32)) > PublicKeyDataXdrEnd) {
  596. //
  597. // Key data extends beyond end of PCD
  598. //
  599. DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate size extends beyond end of PCD, skipping it.\n"));
  600. Status = EFI_ABORTED;
  601. break;
  602. }
  603. //
  604. // Read key length stored in big-endian format
  605. //
  606. PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
  607. //
  608. // Point to the start of the key data
  609. //
  610. PublicKeyDataXdr += sizeof (UINT32);
  611. if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
  612. //
  613. // Key data extends beyond end of PCD
  614. //
  615. DEBUG ((DEBUG_ERROR, "FmpDxe: Certificate extends beyond end of PCD, skipping it.\n"));
  616. Status = EFI_ABORTED;
  617. break;
  618. }
  619. PublicKeyData = PublicKeyDataXdr;
  620. Status = AuthenticateFmpImage (
  621. (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image,
  622. ImageSize,
  623. PublicKeyData,
  624. PublicKeyDataLength
  625. );
  626. if (!EFI_ERROR (Status)) {
  627. break;
  628. }
  629. PublicKeyDataXdr += PublicKeyDataLength;
  630. PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
  631. }
  632. }
  633. if (EFI_ERROR (Status)) {
  634. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - Authentication Failed %r.\n", Status));
  635. goto cleanup;
  636. }
  637. //
  638. // Check to make sure index is 1
  639. //
  640. if (ImageIndex != 1) {
  641. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckImage() - Image Index Invalid.\n"));
  642. *ImageUpdatable = IMAGE_UPDATABLE_INVALID_TYPE;
  643. Status = EFI_SUCCESS;
  644. goto cleanup;
  645. }
  646. //
  647. // Check the FmpPayloadHeader
  648. //
  649. FmpPayloadHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
  650. if (FmpPayloadHeader == NULL) {
  651. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpHeader failed.\n"));
  652. Status = EFI_ABORTED;
  653. goto cleanup;
  654. }
  655. Status = GetFmpPayloadHeaderVersion (FmpPayloadHeader, FmpPayloadSize, &Version);
  656. if (EFI_ERROR (Status)) {
  657. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderVersion failed %r.\n", Status));
  658. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  659. Status = EFI_SUCCESS;
  660. goto cleanup;
  661. }
  662. //
  663. // Check the lowest supported version
  664. //
  665. if (Version < mDesc.LowestSupportedImageVersion) {
  666. DEBUG (
  667. (DEBUG_ERROR,
  668. "FmpDxe: CheckTheImage() - Version Lower than lowest supported version. 0x%08X < 0x%08X\n",
  669. Version, mDesc.LowestSupportedImageVersion)
  670. );
  671. *ImageUpdatable = IMAGE_UPDATABLE_INVALID_OLD;
  672. Status = EFI_SUCCESS;
  673. goto cleanup;
  674. }
  675. //
  676. // Get the FmpHeaderSize so we can determine the real payload size
  677. //
  678. Status = GetFmpPayloadHeaderSize (FmpPayloadHeader, FmpPayloadSize, &FmpHeaderSize);
  679. if (EFI_ERROR (Status)) {
  680. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
  681. *ImageUpdatable = IMAGE_UPDATABLE_INVALID;
  682. Status = EFI_SUCCESS;
  683. goto cleanup;
  684. }
  685. //
  686. // Call FmpDevice Lib Check Image on the
  687. // Raw payload. So all headers need stripped off
  688. //
  689. AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
  690. if (AllHeaderSize == 0) {
  691. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - GetAllHeaderSize failed.\n"));
  692. Status = EFI_ABORTED;
  693. goto cleanup;
  694. }
  695. RawSize = ImageSize - AllHeaderSize;
  696. //
  697. // FmpDeviceLib CheckImage function to do any specific checks
  698. //
  699. Status = FmpDeviceCheckImage ((((UINT8 *)Image) + AllHeaderSize), RawSize, ImageUpdatable);
  700. if (EFI_ERROR (Status)) {
  701. DEBUG ((DEBUG_ERROR, "FmpDxe: CheckTheImage() - FmpDeviceLib CheckImage failed. Status = %r\n", Status));
  702. }
  703. cleanup:
  704. return Status;
  705. }
  706. /**
  707. Updates the firmware image of the device.
  708. This function updates the hardware with the new firmware image.
  709. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  710. If the firmware image is updatable, the function should perform the following minimal validations
  711. before proceeding to do the firmware image update.
  712. - Validate the image authentication if image has attribute
  713. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  714. EFI_SECURITY_VIOLATION if the validation fails.
  715. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  716. the image is unsupported. The function can optionally provide more detailed information on
  717. why the image is not a supported image.
  718. - Validate the data from VendorCode if not null. Image validation must be performed before
  719. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  720. validation failed. The function returns EFI_ABORTED if the data is invalid.
  721. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  722. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  723. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  724. firmware image version is older than the current firmware image version or bad image checksum.
  725. Sensitive operations such as those wiping the entire firmware image and render the device to be
  726. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  727. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  728. reason to the caller.
  729. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  730. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  731. The number is between 1 and DescriptorCount.
  732. @param[in] Image Points to the new image.
  733. @param[in] ImageSize Size of the new image in bytes.
  734. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  735. Null indicates the caller did not specify the policy or use the default policy.
  736. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  737. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  738. details for the aborted operation. The buffer is allocated by this function
  739. with AllocatePool(), and it is the caller's responsibility to free it with a
  740. call to FreePool().
  741. @retval EFI_SUCCESS The device was successfully updated with the new image.
  742. @retval EFI_ABORTED The operation is aborted.
  743. @retval EFI_INVALID_PARAMETER The Image was NULL.
  744. @retval EFI_UNSUPPORTED The operation is not supported.
  745. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  746. **/
  747. EFI_STATUS
  748. EFIAPI
  749. SetTheImage (
  750. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  751. IN UINT8 ImageIndex,
  752. IN CONST VOID *Image,
  753. IN UINTN ImageSize,
  754. IN CONST VOID *VendorCode,
  755. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  756. OUT CHAR16 **AbortReason
  757. )
  758. {
  759. EFI_STATUS Status;
  760. UINT32 Updateable;
  761. BOOLEAN BooleanValue;
  762. UINT32 FmpHeaderSize;
  763. VOID *FmpHeader;
  764. UINTN FmpPayloadSize;
  765. UINT32 AllHeaderSize;
  766. UINT32 IncommingFwVersion;
  767. UINT32 LastAttemptStatus;
  768. UINT32 Version;
  769. UINT32 LowestSupportedVersion;
  770. Status = EFI_SUCCESS;
  771. Updateable = 0;
  772. BooleanValue = FALSE;
  773. FmpHeaderSize = 0;
  774. FmpHeader = NULL;
  775. FmpPayloadSize = 0;
  776. AllHeaderSize = 0;
  777. IncommingFwVersion = 0;
  778. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  779. SetLastAttemptVersionInVariable (IncommingFwVersion); //set to 0 to clear any previous results.
  780. //
  781. // if we have locked the device, then skip the set operation.
  782. // it should be blocked by hardware too but we can catch here even faster
  783. //
  784. if (mFmpDeviceLocked) {
  785. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Device is already locked. Can't update.\n"));
  786. Status = EFI_UNSUPPORTED;
  787. goto cleanup;
  788. }
  789. //
  790. // Call check image to verify the image
  791. //
  792. Status = CheckTheImage (This, ImageIndex, Image, ImageSize, &Updateable);
  793. if (EFI_ERROR (Status)) {
  794. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Check The Image failed with %r.\n", Status));
  795. if (Status == EFI_SECURITY_VIOLATION) {
  796. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;
  797. }
  798. goto cleanup;
  799. }
  800. //
  801. // No functional error in CheckTheImage. Attempt to get the Version to
  802. // support better error reporting.
  803. //
  804. FmpHeader = GetFmpHeader ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, ImageSize, &FmpPayloadSize );
  805. if (FmpHeader == NULL) {
  806. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpHeader failed.\n"));
  807. Status = EFI_ABORTED;
  808. goto cleanup;
  809. }
  810. Status = GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &IncommingFwVersion);
  811. if (!EFI_ERROR (Status)) {
  812. //
  813. // Set to actual value
  814. //
  815. SetLastAttemptVersionInVariable (IncommingFwVersion);
  816. }
  817. if (Updateable != IMAGE_UPDATABLE_VALID) {
  818. DEBUG (
  819. (DEBUG_ERROR,
  820. "FmpDxed: SetTheImage() - Check The Image returned that the Image was not valid for update. Updatable value = 0x%X.\n",
  821. Updateable)
  822. );
  823. Status = EFI_ABORTED;
  824. goto cleanup;
  825. }
  826. if (Progress == NULL) {
  827. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Invalid progress callback\n"));
  828. Status = EFI_INVALID_PARAMETER;
  829. goto cleanup;
  830. }
  831. mProgressFunc = Progress;
  832. mProgressSupported = TRUE;
  833. //
  834. // Checking the image is at least 1%
  835. //
  836. Status = Progress (1);
  837. if (EFI_ERROR (Status)) {
  838. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - Progress Callback failed with Status %r.\n", Status));
  839. }
  840. //
  841. //Check System Power
  842. //
  843. Status = CheckSystemPower (&BooleanValue);
  844. if (EFI_ERROR (Status)) {
  845. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemPower - API call failed %r.\n", Status));
  846. goto cleanup;
  847. }
  848. if (!BooleanValue) {
  849. Status = EFI_ABORTED;
  850. DEBUG (
  851. (DEBUG_ERROR,
  852. "FmpDxe: SetTheImage() - CheckSystemPower - returned False. Update not allowed due to System Power.\n")
  853. );
  854. LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT;
  855. goto cleanup;
  856. }
  857. Progress (2);
  858. //
  859. //Check System Thermal
  860. //
  861. Status = CheckSystemThermal (&BooleanValue);
  862. if (EFI_ERROR (Status)) {
  863. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemThermal - API call failed %r.\n", Status));
  864. goto cleanup;
  865. }
  866. if (!BooleanValue) {
  867. Status = EFI_ABORTED;
  868. DEBUG (
  869. (DEBUG_ERROR,
  870. "FmpDxe: SetTheImage() - CheckSystemThermal - returned False. Update not allowed due to System Thermal.\n")
  871. );
  872. goto cleanup;
  873. }
  874. Progress (3);
  875. //
  876. //Check System Environment
  877. //
  878. Status = CheckSystemEnvironment (&BooleanValue);
  879. if (EFI_ERROR (Status)) {
  880. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - CheckSystemEnvironment - API call failed %r.\n", Status));
  881. goto cleanup;
  882. }
  883. if (!BooleanValue) {
  884. Status = EFI_ABORTED;
  885. DEBUG (
  886. (DEBUG_ERROR,
  887. "FmpDxe: SetTheImage() - CheckSystemEnvironment - returned False. Update not allowed due to System Environment.\n")
  888. );
  889. goto cleanup;
  890. }
  891. Progress (4);
  892. //
  893. // Save LastAttemptStatus as error so that if SetImage never returns the error
  894. // state is recorded.
  895. //
  896. SetLastAttemptStatusInVariable (LastAttemptStatus);
  897. //
  898. // Strip off all the headers so the device can process its firmware
  899. //
  900. Status = GetFmpPayloadHeaderSize (FmpHeader, FmpPayloadSize, &FmpHeaderSize);
  901. if (EFI_ERROR (Status)) {
  902. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetFmpPayloadHeaderSize failed %r.\n", Status));
  903. goto cleanup;
  904. }
  905. AllHeaderSize = GetAllHeaderSize ( (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image, FmpHeaderSize );
  906. if (AllHeaderSize == 0) {
  907. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() - GetAllHeaderSize failed.\n"));
  908. Status = EFI_ABORTED;
  909. goto cleanup;
  910. }
  911. //
  912. // Indicate that control is handed off to FmpDeviceLib
  913. //
  914. Progress (5);
  915. //
  916. //Copy the requested image to the firmware using the FmpDeviceLib
  917. //
  918. Status = FmpDeviceSetImage (
  919. (((UINT8 *)Image) + AllHeaderSize),
  920. ImageSize - AllHeaderSize,
  921. VendorCode,
  922. FmpDxeProgress,
  923. IncommingFwVersion,
  924. AbortReason
  925. );
  926. if (EFI_ERROR (Status)) {
  927. DEBUG ((DEBUG_ERROR, "FmpDxe: SetTheImage() SetImage from FmpDeviceLib failed. Status = %r.\n", Status));
  928. goto cleanup;
  929. }
  930. //
  931. // Finished the update without error
  932. // Indicate that control has been returned from FmpDeviceLib
  933. //
  934. Progress (99);
  935. //
  936. // Update the version stored in variable
  937. //
  938. if (!mRuntimeVersionSupported) {
  939. Version = DEFAULT_VERSION;
  940. GetFmpPayloadHeaderVersion (FmpHeader, FmpPayloadSize, &Version);
  941. SetVersionInVariable (Version);
  942. }
  943. //
  944. // Update lowest supported variable
  945. //
  946. {
  947. LowestSupportedVersion = DEFAULT_LOWESTSUPPORTEDVERSION;
  948. GetFmpPayloadHeaderLowestSupportedVersion (FmpHeader, FmpPayloadSize, &LowestSupportedVersion);
  949. SetLowestSupportedVersionInVariable (LowestSupportedVersion);
  950. }
  951. LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  952. cleanup:
  953. mProgressFunc = NULL;
  954. mProgressSupported = FALSE;
  955. SetLastAttemptStatusInVariable (LastAttemptStatus);
  956. if (Progress != NULL) {
  957. //
  958. // Set progress to 100 after everything is done including recording Status.
  959. //
  960. Progress (100);
  961. }
  962. //
  963. // Need repopulate after SetImage is called to
  964. // update LastAttemptVersion and LastAttemptStatus.
  965. //
  966. mDescriptorPopulated = FALSE;
  967. return Status;
  968. }
  969. /**
  970. Returns information about the firmware package.
  971. This function returns package information.
  972. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  973. @param[out] PackageVersion A version number that represents all the firmware images in the device.
  974. The format is vendor specific and new version must have a greater value
  975. than the old version. If PackageVersion is not supported, the value is
  976. 0xFFFFFFFF. A value of 0xFFFFFFFE indicates that package version
  977. comparison is to be performed using PackageVersionName. A value of
  978. 0xFFFFFFFD indicates that package version update is in progress.
  979. @param[out] PackageVersionName A pointer to a pointer to a null-terminated string representing
  980. the package version name. The buffer is allocated by this function with
  981. AllocatePool(), and it is the caller's responsibility to free it with a
  982. call to FreePool().
  983. @param[out] PackageVersionNameMaxLen The maximum length of package version name if device supports update of
  984. package version name. A value of 0 indicates the device does not support
  985. update of package version name. Length is the number of Unicode characters,
  986. including the terminating null character.
  987. @param[out] AttributesSupported Package attributes that are supported by this device. See 'Package Attribute
  988. Definitions' for possible returned values of this parameter. A value of 1
  989. indicates the attribute is supported and the current setting value is
  990. indicated in AttributesSetting. A value of 0 indicates the attribute is not
  991. supported and the current setting value in AttributesSetting is meaningless.
  992. @param[out] AttributesSetting Package attributes. See 'Package Attribute Definitions' for possible returned
  993. values of this parameter
  994. @retval EFI_SUCCESS The package information was successfully returned.
  995. @retval EFI_UNSUPPORTED The operation is not supported.
  996. **/
  997. EFI_STATUS
  998. EFIAPI
  999. GetPackageInfo (
  1000. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1001. OUT UINT32 *PackageVersion,
  1002. OUT CHAR16 **PackageVersionName,
  1003. OUT UINT32 *PackageVersionNameMaxLen,
  1004. OUT UINT64 *AttributesSupported,
  1005. OUT UINT64 *AttributesSetting
  1006. )
  1007. {
  1008. return EFI_UNSUPPORTED;
  1009. }
  1010. /**
  1011. Updates information about the firmware package.
  1012. This function updates package information.
  1013. This function returns EFI_UNSUPPORTED if the package information is not updatable.
  1014. VendorCode enables vendor to implement vendor-specific package information update policy.
  1015. Null if the caller did not specify this policy or use the default policy.
  1016. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  1017. @param[in] Image Points to the authentication image.
  1018. Null if authentication is not required.
  1019. @param[in] ImageSize Size of the authentication image in bytes.
  1020. 0 if authentication is not required.
  1021. @param[in] VendorCode This enables vendor to implement vendor-specific firmware
  1022. image update policy.
  1023. Null indicates the caller did not specify this policy or use
  1024. the default policy.
  1025. @param[in] PackageVersion The new package version.
  1026. @param[in] PackageVersionName A pointer to the new null-terminated Unicode string representing
  1027. the package version name.
  1028. The string length is equal to or less than the value returned in
  1029. PackageVersionNameMaxLen.
  1030. @retval EFI_SUCCESS The device was successfully updated with the new package
  1031. information.
  1032. @retval EFI_INVALID_PARAMETER The PackageVersionName length is longer than the value
  1033. returned in PackageVersionNameMaxLen.
  1034. @retval EFI_UNSUPPORTED The operation is not supported.
  1035. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  1036. **/
  1037. EFI_STATUS
  1038. EFIAPI
  1039. SetPackageInfo (
  1040. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  1041. IN CONST VOID *Image,
  1042. IN UINTN ImageSize,
  1043. IN CONST VOID *VendorCode,
  1044. IN UINT32 PackageVersion,
  1045. IN CONST CHAR16 *PackageVersionName
  1046. )
  1047. {
  1048. return EFI_UNSUPPORTED;
  1049. }
  1050. /**
  1051. Event notification function that is invoked when the event GUID specified by
  1052. PcdFmpDeviceLockEventGuid is signaled.
  1053. @param[in] Event Event whose notification function is being invoked.
  1054. @param[in] Context The pointer to the notification function's context,
  1055. which is implementation-dependent.
  1056. **/
  1057. VOID
  1058. EFIAPI
  1059. FmpDxeLockEventNotify (
  1060. IN EFI_EVENT Event,
  1061. IN VOID *Context
  1062. )
  1063. {
  1064. EFI_STATUS Status;
  1065. if (!mFmpDeviceLocked) {
  1066. //
  1067. // Lock the firmware device
  1068. //
  1069. Status = FmpDeviceLock();
  1070. if (EFI_ERROR (Status)) {
  1071. if (Status != EFI_UNSUPPORTED) {
  1072. DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
  1073. } else {
  1074. DEBUG ((DEBUG_WARN, "FmpDxe: FmpDeviceLock() returned error. Status = %r\n", Status));
  1075. }
  1076. }
  1077. mFmpDeviceLocked = TRUE;
  1078. }
  1079. }
  1080. /**
  1081. Function to install FMP instance.
  1082. @param[in] Handle The device handle to install a FMP instance on.
  1083. @retval EFI_SUCCESS FMP Installed
  1084. @retval EFI_INVALID_PARAMETER Handle was invalid
  1085. @retval other Error installing FMP
  1086. **/
  1087. EFI_STATUS
  1088. EFIAPI
  1089. InstallFmpInstance (
  1090. IN EFI_HANDLE Handle
  1091. )
  1092. {
  1093. EFI_STATUS Status;
  1094. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  1095. EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL *FmpProgress;
  1096. Status = EFI_SUCCESS;
  1097. Fmp = NULL;
  1098. FmpProgress = NULL;
  1099. //
  1100. // Only allow a single FMP Protocol instance to be installed
  1101. //
  1102. if (mFmpInstalled) {
  1103. return EFI_ALREADY_STARTED;
  1104. }
  1105. //
  1106. // Allocate FMP Protocol instance
  1107. //
  1108. Fmp = AllocateZeroPool (sizeof (EFI_FIRMWARE_MANAGEMENT_PROTOCOL));
  1109. if (Fmp == NULL) {
  1110. DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Protocol instance.\n"));
  1111. Status = EFI_OUT_OF_RESOURCES;
  1112. goto cleanup;
  1113. }
  1114. //
  1115. // Allocate FMP Progress Protocol instance
  1116. //
  1117. FmpProgress = AllocateZeroPool (sizeof (EDKII_FIRMWARE_MANAGEMENT_PROGRESS_PROTOCOL));
  1118. if (FmpProgress == NULL) {
  1119. DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to allocate memory for FMP Progress Protocol instance.\n"));
  1120. Status = EFI_OUT_OF_RESOURCES;
  1121. FreePool (Fmp);
  1122. goto cleanup;
  1123. }
  1124. //
  1125. // Set up FMP Protocol function pointers
  1126. //
  1127. Fmp->GetImageInfo = GetTheImageInfo;
  1128. Fmp->GetImage = GetTheImage;
  1129. Fmp->SetImage = SetTheImage;
  1130. Fmp->CheckImage = CheckTheImage;
  1131. Fmp->GetPackageInfo = GetPackageInfo;
  1132. Fmp->SetPackageInfo = SetPackageInfo;
  1133. //
  1134. // Fill in FMP Progress Protocol fields for Version 1
  1135. //
  1136. FmpProgress->Version = 1;
  1137. FmpProgress->ProgressBarForegroundColor.Raw = PcdGet32 (PcdFmpDeviceProgressColor);
  1138. FmpProgress->WatchdogSeconds = PcdGet8 (PcdFmpDeviceProgressWatchdogTimeInSeconds);
  1139. //
  1140. // Install FMP Protocol and FMP Progress Protocol
  1141. //
  1142. Status = gBS->InstallMultipleProtocolInterfaces (
  1143. &Handle,
  1144. &gEfiFirmwareManagementProtocolGuid,
  1145. Fmp,
  1146. &gEdkiiFirmwareManagementProgressProtocolGuid,
  1147. FmpProgress,
  1148. NULL
  1149. );
  1150. if (EFI_ERROR (Status)) {
  1151. DEBUG ((DEBUG_ERROR, "FmpDxe: FMP Protocol install error. Status = %r.\n", Status));
  1152. FreePool (Fmp);
  1153. goto cleanup;
  1154. }
  1155. DEBUG ((DEBUG_INFO, "FmpDxe: FMP Protocol Installed!\n"));
  1156. mFmpInstalled = TRUE;
  1157. cleanup:
  1158. return Status;
  1159. }
  1160. /**
  1161. Main entry for this driver/library.
  1162. @param[in] ImageHandle Image handle this driver.
  1163. @param[in] SystemTable Pointer to SystemTable.
  1164. **/
  1165. EFI_STATUS
  1166. EFIAPI
  1167. FmpDxeEntryPoint (
  1168. IN EFI_HANDLE ImageHandle,
  1169. IN EFI_SYSTEM_TABLE *SystemTable
  1170. )
  1171. {
  1172. EFI_STATUS Status;
  1173. EFI_GUID *LockGuid;
  1174. //
  1175. // Verify that a new FILE_GUID value has been provided in the <Defines>
  1176. // section of this module. The FILE_GUID is the ESRT GUID that must be
  1177. // unique for each updatable firmware image.
  1178. //
  1179. if (CompareGuid (&mDefaultModuleFileGuid, &gEfiCallerIdGuid)) {
  1180. DEBUG ((DEBUG_ERROR, "FmpDxe: Use of default FILE_GUID detected. FILE_GUID must be set to a unique value.\n"));
  1181. ASSERT (FALSE);
  1182. return EFI_UNSUPPORTED;
  1183. }
  1184. //
  1185. // Get the ImageIdName value for the EFI_FIRMWARE_IMAGE_DESCRIPTOR from a PCD.
  1186. //
  1187. mImageIdName = (CHAR16 *) PcdGetPtr (PcdFmpDeviceImageIdName);
  1188. if (PcdGetSize (PcdFmpDeviceImageIdName) <= 2 || mImageIdName[0] == 0) {
  1189. //
  1190. // PcdFmpDeviceImageIdName must be set to a non-empty Unicode string
  1191. //
  1192. DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib PcdFmpDeviceImageIdName is an empty string.\n"));
  1193. ASSERT (FALSE);
  1194. }
  1195. //
  1196. // Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
  1197. //
  1198. DetectTestKey ();
  1199. if (IsLockFmpDeviceAtLockEventGuidRequired ()) {
  1200. //
  1201. // Lock all UEFI Variables used by this module.
  1202. //
  1203. Status = LockAllFmpVariables ();
  1204. if (EFI_ERROR (Status)) {
  1205. DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to lock variables. Status = %r.\n", Status));
  1206. } else {
  1207. DEBUG ((DEBUG_INFO, "FmpDxe: All variables locked\n"));
  1208. }
  1209. //
  1210. // Register notify function to lock the FMP device.
  1211. // The lock event GUID is retrieved from PcdFmpDeviceLockEventGuid.
  1212. // If PcdFmpDeviceLockEventGuid is not the size of an EFI_GUID, then
  1213. // gEfiEndOfDxeEventGroupGuid is used.
  1214. //
  1215. LockGuid = &gEfiEndOfDxeEventGroupGuid;
  1216. if (PcdGetSize (PcdFmpDeviceLockEventGuid) == sizeof (EFI_GUID)) {
  1217. LockGuid = (EFI_GUID *)PcdGetPtr (PcdFmpDeviceLockEventGuid);
  1218. }
  1219. DEBUG ((DEBUG_INFO, "FmpDxe: Lock GUID: %g\n", LockGuid));
  1220. Status = gBS->CreateEventEx (
  1221. EVT_NOTIFY_SIGNAL,
  1222. TPL_CALLBACK,
  1223. FmpDxeLockEventNotify,
  1224. NULL,
  1225. LockGuid,
  1226. &mFmpDeviceLockEvent
  1227. );
  1228. if (EFI_ERROR (Status)) {
  1229. DEBUG ((DEBUG_ERROR, "FmpDxe: Failed to register notification. Status = %r\n", Status));
  1230. }
  1231. ASSERT_EFI_ERROR (Status);
  1232. } else {
  1233. DEBUG ((DEBUG_VERBOSE, "FmpDxe: Not registering notification to call FmpDeviceLock() because mfg mode\n"));
  1234. }
  1235. //
  1236. // Register with library the install function so if the library uses
  1237. // UEFI driver model/driver binding protocol it can install FMP on its device handle
  1238. // If library is simple lib that does not use driver binding then it should return
  1239. // unsupported and this will install the FMP instance on the ImageHandle
  1240. //
  1241. Status = RegisterFmpInstaller (InstallFmpInstance);
  1242. if (Status == EFI_UNSUPPORTED) {
  1243. DEBUG ((DEBUG_INFO, "FmpDxe: FmpDeviceLib registration returned EFI_UNSUPPORTED. Installing single FMP instance.\n"));
  1244. Status = InstallFmpInstance (ImageHandle);
  1245. } else if (EFI_ERROR (Status)) {
  1246. DEBUG ((DEBUG_ERROR, "FmpDxe: FmpDeviceLib registration returned %r. No FMP installed.\n", Status));
  1247. } else {
  1248. DEBUG ((
  1249. DEBUG_INFO,
  1250. "FmpDxe: FmpDeviceLib registration returned EFI_SUCCESS. Expect FMP to be installed during the BDS/Device connection phase.\n"
  1251. ));
  1252. }
  1253. return Status;
  1254. }