SystemFirmwareUpdateDxe.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /** @file
  2. SetImage instance to update system firmware.
  3. Caution: This module requires additional review when modified.
  4. This module will have external input - capsule image.
  5. This external input must be validated carefully to avoid security issue like
  6. buffer overflow, integer overflow.
  7. FmpSetImage() will receive untrusted input and do basic validation.
  8. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include "SystemFirmwareDxe.h"
  12. //
  13. // SystemFmp driver private data
  14. //
  15. SYSTEM_FMP_PRIVATE_DATA *mSystemFmpPrivate = NULL;
  16. EFI_GUID mCurrentImageTypeId;
  17. BOOLEAN mNvRamUpdated = FALSE;
  18. /**
  19. Parse Config data file to get the updated data array.
  20. @param[in] DataBuffer Config raw file buffer.
  21. @param[in] BufferSize Size of raw buffer.
  22. @param[in, out] ConfigHeader Pointer to the config header.
  23. @param[in, out] UpdateArray Pointer to the config of update data.
  24. @retval EFI_NOT_FOUND No config data is found.
  25. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
  26. @retval EFI_SUCCESS Parse the config file successfully.
  27. **/
  28. EFI_STATUS
  29. ParseUpdateDataFile (
  30. IN UINT8 *DataBuffer,
  31. IN UINTN BufferSize,
  32. IN OUT CONFIG_HEADER *ConfigHeader,
  33. IN OUT UPDATE_CONFIG_DATA **UpdateArray
  34. );
  35. /**
  36. Update System Firmware image component.
  37. @param[in] SystemFirmwareImage Points to the System Firmware image.
  38. @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.
  39. @param[in] ConfigData Points to the component configuration structure.
  40. @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  41. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  42. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  43. @param[in] StartPercentage The start completion percentage value that may be used to report progress during the flash write operation.
  44. @param[in] EndPercentage The end completion percentage value that may be used to report progress during the flash write operation.
  45. @retval EFI_SUCCESS The System Firmware image is updated.
  46. @retval EFI_WRITE_PROTECTED The flash device is read only.
  47. **/
  48. EFI_STATUS
  49. PerformUpdate (
  50. IN VOID *SystemFirmwareImage,
  51. IN UINTN SystemFirmwareImageSize,
  52. IN UPDATE_CONFIG_DATA *ConfigData,
  53. OUT UINT32 *LastAttemptVersion,
  54. OUT UINT32 *LastAttemptStatus,
  55. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  56. IN UINTN StartPercentage,
  57. IN UINTN EndPercentage
  58. )
  59. {
  60. EFI_STATUS Status;
  61. DEBUG ((DEBUG_INFO, "PlatformUpdate:"));
  62. DEBUG ((DEBUG_INFO, " BaseAddress - 0x%lx,", ConfigData->BaseAddress));
  63. DEBUG ((DEBUG_INFO, " ImageOffset - 0x%x,", ConfigData->ImageOffset));
  64. DEBUG ((DEBUG_INFO, " Legnth - 0x%x\n", ConfigData->Length));
  65. if (Progress != NULL) {
  66. Progress (StartPercentage);
  67. }
  68. Status = PerformFlashWriteWithProgress (
  69. ConfigData->FirmwareType,
  70. ConfigData->BaseAddress,
  71. ConfigData->AddressType,
  72. (VOID *)((UINTN)SystemFirmwareImage + (UINTN)ConfigData->ImageOffset),
  73. ConfigData->Length,
  74. Progress,
  75. StartPercentage,
  76. EndPercentage
  77. );
  78. if (Progress != NULL) {
  79. Progress (EndPercentage);
  80. }
  81. if (!EFI_ERROR (Status)) {
  82. *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  83. if (ConfigData->FirmwareType == PlatformFirmwareTypeNvRam) {
  84. mNvRamUpdated = TRUE;
  85. }
  86. } else {
  87. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  88. }
  89. return Status;
  90. }
  91. /**
  92. Update System Firmware image.
  93. @param[in] SystemFirmwareImage Points to the System Firmware image.
  94. @param[in] SystemFirmwareImageSize The length of the System Firmware image in bytes.
  95. @param[in] ConfigImage Points to the config file image.
  96. @param[in] ConfigImageSize The length of the config file image in bytes.
  97. @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  98. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  99. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  100. @retval EFI_SUCCESS The System Firmware image is updated.
  101. @retval EFI_WRITE_PROTECTED The flash device is read only.
  102. **/
  103. EFI_STATUS
  104. UpdateImage (
  105. IN VOID *SystemFirmwareImage,
  106. IN UINTN SystemFirmwareImageSize,
  107. IN VOID *ConfigImage,
  108. IN UINTN ConfigImageSize,
  109. OUT UINT32 *LastAttemptVersion,
  110. OUT UINT32 *LastAttemptStatus,
  111. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
  112. )
  113. {
  114. EFI_STATUS Status;
  115. UPDATE_CONFIG_DATA *ConfigData;
  116. UPDATE_CONFIG_DATA *UpdateConfigData;
  117. CONFIG_HEADER ConfigHeader;
  118. UINTN Index;
  119. UINTN TotalSize;
  120. UINTN BytesWritten;
  121. UINTN StartPercentage;
  122. UINTN EndPercentage;
  123. if (ConfigImage == NULL) {
  124. DEBUG ((DEBUG_INFO, "PlatformUpdate (NoConfig):"));
  125. DEBUG ((DEBUG_INFO, " BaseAddress - 0x%x,", 0));
  126. DEBUG ((DEBUG_INFO, " Length - 0x%x\n", SystemFirmwareImageSize));
  127. // ASSUME the whole System Firmware include NVRAM region.
  128. StartPercentage = 0;
  129. EndPercentage = 100;
  130. if (Progress != NULL) {
  131. Progress (StartPercentage);
  132. }
  133. Status = PerformFlashWriteWithProgress (
  134. PlatformFirmwareTypeNvRam,
  135. 0,
  136. FlashAddressTypeRelativeAddress,
  137. SystemFirmwareImage,
  138. SystemFirmwareImageSize,
  139. Progress,
  140. StartPercentage,
  141. EndPercentage
  142. );
  143. if (Progress != NULL) {
  144. Progress (EndPercentage);
  145. }
  146. if (!EFI_ERROR (Status)) {
  147. *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  148. mNvRamUpdated = TRUE;
  149. } else {
  150. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  151. }
  152. return Status;
  153. }
  154. DEBUG ((DEBUG_INFO, "PlatformUpdate (With Config):\n"));
  155. ConfigData = NULL;
  156. ZeroMem (&ConfigHeader, sizeof (ConfigHeader));
  157. Status = ParseUpdateDataFile (
  158. ConfigImage,
  159. ConfigImageSize,
  160. &ConfigHeader,
  161. &ConfigData
  162. );
  163. DEBUG ((DEBUG_INFO, "ParseUpdateDataFile - %r\n", Status));
  164. if (EFI_ERROR (Status)) {
  165. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  166. return EFI_INVALID_PARAMETER;
  167. }
  168. DEBUG ((DEBUG_INFO, "ConfigHeader.NumOfUpdates - 0x%x\n", ConfigHeader.NumOfUpdates));
  169. DEBUG ((DEBUG_INFO, "PcdEdkiiSystemFirmwareFileGuid - %g\n", PcdGetPtr (PcdEdkiiSystemFirmwareFileGuid)));
  170. TotalSize = 0;
  171. for (Index = 0; Index < ConfigHeader.NumOfUpdates; Index++) {
  172. if (CompareGuid (&ConfigData[Index].FileGuid, PcdGetPtr (PcdEdkiiSystemFirmwareFileGuid))) {
  173. TotalSize = TotalSize + ConfigData[Index].Length;
  174. }
  175. }
  176. BytesWritten = 0;
  177. Index = 0;
  178. UpdateConfigData = ConfigData;
  179. while (Index < ConfigHeader.NumOfUpdates) {
  180. if (CompareGuid (&UpdateConfigData->FileGuid, PcdGetPtr (PcdEdkiiSystemFirmwareFileGuid))) {
  181. DEBUG ((DEBUG_INFO, "FileGuid - %g (processing)\n", &UpdateConfigData->FileGuid));
  182. StartPercentage = (BytesWritten * 100) / TotalSize;
  183. EndPercentage = ((BytesWritten + UpdateConfigData->Length) * 100) / TotalSize;
  184. Status = PerformUpdate (
  185. SystemFirmwareImage,
  186. SystemFirmwareImageSize,
  187. UpdateConfigData,
  188. LastAttemptVersion,
  189. LastAttemptStatus,
  190. Progress,
  191. StartPercentage,
  192. EndPercentage
  193. );
  194. //
  195. // Shall updates be serialized so that if an update is not successfully completed,
  196. // the remaining updates won't be performed.
  197. //
  198. if (EFI_ERROR (Status)) {
  199. break;
  200. }
  201. } else {
  202. DEBUG ((DEBUG_INFO, "FileGuid - %g (ignored)\n", &UpdateConfigData->FileGuid));
  203. }
  204. BytesWritten += UpdateConfigData->Length;
  205. Index++;
  206. UpdateConfigData++;
  207. }
  208. return Status;
  209. }
  210. /**
  211. Authenticate and update System Firmware image.
  212. Caution: This function may receive untrusted input.
  213. @param[in] Image The EDKII system FMP capsule image.
  214. @param[in] ImageSize The size of the EDKII system FMP capsule image in bytes.
  215. @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  216. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  217. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  218. @retval EFI_SUCCESS EDKII system FMP capsule passes authentication and the System Firmware image is updated.
  219. @retval EFI_SECURITY_VIOLATION EDKII system FMP capsule fails authentication and the System Firmware image is not updated.
  220. @retval EFI_WRITE_PROTECTED The flash device is read only.
  221. **/
  222. EFI_STATUS
  223. SystemFirmwareAuthenticatedUpdate (
  224. IN VOID *Image,
  225. IN UINTN ImageSize,
  226. OUT UINT32 *LastAttemptVersion,
  227. OUT UINT32 *LastAttemptStatus,
  228. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress
  229. )
  230. {
  231. EFI_STATUS Status;
  232. VOID *SystemFirmwareImage;
  233. UINTN SystemFirmwareImageSize;
  234. VOID *ConfigImage;
  235. UINTN ConfigImageSize;
  236. VOID *AuthenticatedImage;
  237. UINTN AuthenticatedImageSize;
  238. AuthenticatedImage = NULL;
  239. AuthenticatedImageSize = 0;
  240. DEBUG ((DEBUG_INFO, "SystemFirmwareAuthenticatedUpdate...\n"));
  241. Status = CapsuleAuthenticateSystemFirmware (Image, ImageSize, FALSE, LastAttemptVersion, LastAttemptStatus, &AuthenticatedImage, &AuthenticatedImageSize);
  242. if (EFI_ERROR (Status)) {
  243. DEBUG ((DEBUG_INFO, "SystemFirmwareAuthenticateImage - %r\n", Status));
  244. return Status;
  245. }
  246. DEBUG ((DEBUG_INFO, "ExtractSystemFirmwareImage ...\n"));
  247. ExtractSystemFirmwareImage (AuthenticatedImage, AuthenticatedImageSize, &SystemFirmwareImage, &SystemFirmwareImageSize);
  248. DEBUG ((DEBUG_INFO, "ExtractConfigImage ...\n"));
  249. ExtractConfigImage (AuthenticatedImage, AuthenticatedImageSize, &ConfigImage, &ConfigImageSize);
  250. DEBUG ((DEBUG_INFO, "UpdateImage ...\n"));
  251. Status = UpdateImage (SystemFirmwareImage, SystemFirmwareImageSize, ConfigImage, ConfigImageSize, LastAttemptVersion, LastAttemptStatus, Progress);
  252. if (EFI_ERROR (Status)) {
  253. DEBUG ((DEBUG_INFO, "UpdateImage - %r\n", Status));
  254. return Status;
  255. }
  256. DEBUG ((DEBUG_INFO, "SystemFirmwareAuthenticatedUpdate Done\n"));
  257. return EFI_SUCCESS;
  258. }
  259. /**
  260. This code finds variable in storage blocks (Volatile or Non-Volatile).
  261. @param[in] VariableName Name of Variable to be found.
  262. @param[in] VendorGuid Variable vendor GUID.
  263. @param[out] Attributes Attribute value of the variable found.
  264. @param[in, out] DataSize Size of Data found. If size is less than the
  265. data, this value contains the required size.
  266. @param[out] Data Data pointer.
  267. @return EFI_INVALID_PARAMETER Invalid parameter.
  268. @return EFI_SUCCESS Find the specified variable.
  269. @return EFI_NOT_FOUND Not found.
  270. @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
  271. **/
  272. EFI_STATUS
  273. EFIAPI
  274. GetVariableHook (
  275. IN CHAR16 *VariableName,
  276. IN EFI_GUID *VendorGuid,
  277. OUT UINT32 *Attributes OPTIONAL,
  278. IN OUT UINTN *DataSize,
  279. OUT VOID *Data
  280. )
  281. {
  282. DEBUG ((DEBUG_INFO, "GetVariableHook - %S, %g\n", VariableName, VendorGuid));
  283. return EFI_NOT_AVAILABLE_YET;
  284. }
  285. /**
  286. This code Finds the Next available variable.
  287. @param[in, out] VariableNameSize Size of the variable name.
  288. @param[in, out] VariableName Pointer to variable name.
  289. @param[in, out] VendorGuid Variable Vendor Guid.
  290. @return EFI_INVALID_PARAMETER Invalid parameter.
  291. @return EFI_SUCCESS Find the specified variable.
  292. @return EFI_NOT_FOUND Not found.
  293. @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.
  294. **/
  295. EFI_STATUS
  296. EFIAPI
  297. GetNextVariableNameHook (
  298. IN OUT UINTN *VariableNameSize,
  299. IN OUT CHAR16 *VariableName,
  300. IN OUT EFI_GUID *VendorGuid
  301. )
  302. {
  303. DEBUG ((DEBUG_INFO, "GetNextVariableNameHook - %S, %g\n", VariableName, VendorGuid));
  304. return EFI_NOT_AVAILABLE_YET;
  305. }
  306. /**
  307. This code sets variable in storage blocks (Volatile or Non-Volatile).
  308. @param[in] VariableName Name of Variable to be found.
  309. @param[in] VendorGuid Variable vendor GUID.
  310. @param[in] Attributes Attribute value of the variable found
  311. @param[in] DataSize Size of Data found. If size is less than the
  312. data, this value contains the required size.
  313. @param[in] Data Data pointer.
  314. @return EFI_INVALID_PARAMETER Invalid parameter.
  315. @return EFI_SUCCESS Set successfully.
  316. @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
  317. @return EFI_NOT_FOUND Not found.
  318. @return EFI_WRITE_PROTECTED Variable is read-only.
  319. **/
  320. EFI_STATUS
  321. EFIAPI
  322. SetVariableHook (
  323. IN CHAR16 *VariableName,
  324. IN EFI_GUID *VendorGuid,
  325. IN UINT32 Attributes,
  326. IN UINTN DataSize,
  327. IN VOID *Data
  328. )
  329. {
  330. DEBUG ((DEBUG_INFO, "SetVariableHook - %S, %g, 0x%x (0x%x)\n", VariableName, VendorGuid, Attributes, DataSize));
  331. return EFI_NOT_AVAILABLE_YET;
  332. }
  333. /**
  334. This code returns information about the EFI variables.
  335. @param[in] Attributes Attributes bitmask to specify the type of variables
  336. on which to return information.
  337. @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
  338. for the EFI variables associated with the attributes specified.
  339. @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
  340. for EFI variables associated with the attributes specified.
  341. @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
  342. associated with the attributes specified.
  343. @return EFI_SUCCESS Query successfully.
  344. **/
  345. EFI_STATUS
  346. EFIAPI
  347. QueryVariableInfoHook (
  348. IN UINT32 Attributes,
  349. OUT UINT64 *MaximumVariableStorageSize,
  350. OUT UINT64 *RemainingVariableStorageSize,
  351. OUT UINT64 *MaximumVariableSize
  352. )
  353. {
  354. DEBUG ((DEBUG_INFO, "QueryVariableInfoHook - 0x%x\n", Attributes));
  355. return EFI_NOT_AVAILABLE_YET;
  356. }
  357. /**
  358. Updates the firmware image of the device.
  359. This function updates the hardware with the new firmware image.
  360. This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
  361. If the firmware image is updatable, the function should perform the following minimal validations
  362. before proceeding to do the firmware image update.
  363. - Validate the image authentication if image has attribute
  364. IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
  365. EFI_SECURITY_VIOLATION if the validation fails.
  366. - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
  367. the image is unsupported. The function can optionally provide more detailed information on
  368. why the image is not a supported image.
  369. - Validate the data from VendorCode if not null. Image validation must be performed before
  370. VendorCode data validation. VendorCode data is ignored or considered invalid if image
  371. validation failed. The function returns EFI_ABORTED if the data is invalid.
  372. VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
  373. the caller did not specify the policy or use the default policy. As an example, vendor can implement
  374. a policy to allow an option to force a firmware image update when the abort reason is due to the new
  375. firmware image version is older than the current firmware image version or bad image checksum.
  376. Sensitive operations such as those wiping the entire firmware image and render the device to be
  377. non-functional should be encoded in the image itself rather than passed with the VendorCode.
  378. AbortReason enables vendor to have the option to provide a more detailed description of the abort
  379. reason to the caller.
  380. @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
  381. @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
  382. The number is between 1 and DescriptorCount.
  383. @param[in] Image Points to the new image.
  384. @param[in] ImageSize Size of the new image in bytes.
  385. @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
  386. Null indicates the caller did not specify the policy or use the default policy.
  387. @param[in] Progress A function used by the driver to report the progress of the firmware update.
  388. @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
  389. details for the aborted operation. The buffer is allocated by this function
  390. with AllocatePool(), and it is the caller's responsibility to free it with a
  391. call to FreePool().
  392. @retval EFI_SUCCESS The device was successfully updated with the new image.
  393. @retval EFI_ABORTED The operation is aborted.
  394. @retval EFI_INVALID_PARAMETER The Image was NULL.
  395. @retval EFI_UNSUPPORTED The operation is not supported.
  396. @retval EFI_SECURITY_VIOLATION The operation could not be performed due to an authentication failure.
  397. **/
  398. EFI_STATUS
  399. EFIAPI
  400. FmpSetImage (
  401. IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
  402. IN UINT8 ImageIndex,
  403. IN CONST VOID *Image,
  404. IN UINTN ImageSize,
  405. IN CONST VOID *VendorCode,
  406. IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
  407. OUT CHAR16 **AbortReason
  408. )
  409. {
  410. EFI_STATUS Status;
  411. EFI_STATUS VarStatus;
  412. SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate;
  413. if ((Image == NULL) || (ImageSize == 0) || (AbortReason == NULL)) {
  414. return EFI_INVALID_PARAMETER;
  415. }
  416. SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP (This);
  417. *AbortReason = NULL;
  418. if ((ImageIndex == 0) || (ImageIndex > SystemFmpPrivate->DescriptorCount)) {
  419. return EFI_INVALID_PARAMETER;
  420. }
  421. Status = SystemFirmwareAuthenticatedUpdate ((VOID *)Image, ImageSize, &SystemFmpPrivate->LastAttempt.LastAttemptVersion, &SystemFmpPrivate->LastAttempt.LastAttemptStatus, Progress);
  422. DEBUG ((DEBUG_INFO, "SetImage - LastAttempt Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));
  423. //
  424. // If NVRAM is updated, we should no longer touch variable services, because
  425. // the current variable driver may not manage the new NVRAM region.
  426. //
  427. if (mNvRamUpdated) {
  428. DEBUG ((DEBUG_INFO, "NvRamUpdated, Update Variable Services\n"));
  429. gRT->GetVariable = GetVariableHook;
  430. gRT->GetNextVariableName = GetNextVariableNameHook;
  431. gRT->SetVariable = SetVariableHook;
  432. gRT->QueryVariableInfo = QueryVariableInfoHook;
  433. gRT->Hdr.CRC32 = 0;
  434. gBS->CalculateCrc32 (
  435. (UINT8 *)&gRT->Hdr,
  436. gRT->Hdr.HeaderSize,
  437. &gRT->Hdr.CRC32
  438. );
  439. }
  440. VarStatus = gRT->SetVariable (
  441. SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
  442. &gSystemFmpLastAttemptVariableGuid,
  443. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  444. sizeof (SystemFmpPrivate->LastAttempt),
  445. &SystemFmpPrivate->LastAttempt
  446. );
  447. DEBUG ((DEBUG_INFO, "SetLastAttempt - %r\n", VarStatus));
  448. return Status;
  449. }
  450. /**
  451. Get the set of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures from an FMP Protocol.
  452. @param[in] Handle Handle with an FMP Protocol or a System FMP
  453. Protocol.
  454. @param[in] ProtocolGuid Pointer to the FMP Protocol GUID or System FMP
  455. Protocol GUID.
  456. @param[out] FmpImageInfoCount Pointer to the number of
  457. EFI_FIRMWARE_IMAGE_DESCRIPTOR structures.
  458. @param[out] DescriptorSize Pointer to the size, in bytes, of each
  459. EFI_FIRMWARE_IMAGE_DESCRIPTOR structure.
  460. @return NULL No EFI_FIRMWARE_IMAGE_DESCRIPTOR structures found.
  461. @return !NULL Pointer to a buffer of EFI_FIRMWARE_IMAGE_DESCRIPTOR structures
  462. allocated using AllocatePool(). Caller must free buffer with
  463. FreePool().
  464. **/
  465. EFI_FIRMWARE_IMAGE_DESCRIPTOR *
  466. GetFmpImageDescriptors (
  467. IN EFI_HANDLE Handle,
  468. IN EFI_GUID *ProtocolGuid,
  469. OUT UINT8 *FmpImageInfoCount,
  470. OUT UINTN *DescriptorSize
  471. )
  472. {
  473. EFI_STATUS Status;
  474. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp;
  475. UINTN ImageInfoSize;
  476. EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf;
  477. UINT32 FmpImageInfoDescriptorVer;
  478. UINT32 PackageVersion;
  479. CHAR16 *PackageVersionName;
  480. *FmpImageInfoCount = 0;
  481. *DescriptorSize = 0;
  482. Status = gBS->HandleProtocol (
  483. Handle,
  484. ProtocolGuid,
  485. (VOID **)&Fmp
  486. );
  487. if (EFI_ERROR (Status)) {
  488. return NULL;
  489. }
  490. //
  491. // Determine the size required for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  492. //
  493. ImageInfoSize = 0;
  494. Status = Fmp->GetImageInfo (
  495. Fmp, // FMP Pointer
  496. &ImageInfoSize, // Buffer Size (in this case 0)
  497. NULL, // NULL so we can get size
  498. &FmpImageInfoDescriptorVer, // DescriptorVersion
  499. FmpImageInfoCount, // DescriptorCount
  500. DescriptorSize, // DescriptorSize
  501. &PackageVersion, // PackageVersion
  502. &PackageVersionName // PackageVersionName
  503. );
  504. if (Status != EFI_BUFFER_TOO_SMALL) {
  505. DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Unexpected Failure. Status = %r\n", Status));
  506. return NULL;
  507. }
  508. //
  509. // Allocate buffer for the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  510. //
  511. FmpImageInfoBuf = NULL;
  512. FmpImageInfoBuf = AllocateZeroPool (ImageInfoSize);
  513. if (FmpImageInfoBuf == NULL) {
  514. DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failed to allocate memory for descriptors.\n"));
  515. return NULL;
  516. }
  517. //
  518. // Retrieve the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  519. //
  520. PackageVersionName = NULL;
  521. Status = Fmp->GetImageInfo (
  522. Fmp,
  523. &ImageInfoSize, // ImageInfoSize
  524. FmpImageInfoBuf, // ImageInfo
  525. &FmpImageInfoDescriptorVer, // DescriptorVersion
  526. FmpImageInfoCount, // DescriptorCount
  527. DescriptorSize, // DescriptorSize
  528. &PackageVersion, // PackageVersion
  529. &PackageVersionName // PackageVersionName
  530. );
  531. //
  532. // Free unused PackageVersionName return buffer
  533. //
  534. if (PackageVersionName != NULL) {
  535. FreePool (PackageVersionName);
  536. PackageVersionName = NULL;
  537. }
  538. if (EFI_ERROR (Status)) {
  539. DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failure in GetImageInfo. Status = %r\n", Status));
  540. if (FmpImageInfoBuf != NULL) {
  541. FreePool (FmpImageInfoBuf);
  542. }
  543. return NULL;
  544. }
  545. return FmpImageInfoBuf;
  546. }
  547. /**
  548. Search for handles with an FMP protocol whose EFI_FIRMWARE_IMAGE_DESCRIPTOR
  549. ImageTypeId matches the ImageTypeId produced by this module.
  550. @param[in] ProtocolGuid Pointer to the GUID of the protocol to search.
  551. @param[out] HandleCount Pointer to the number of returned handles.
  552. @return NULL No matching handles found.
  553. @return !NULL Pointer to a buffer of handles allocated using AllocatePool().
  554. Caller must free buffer with FreePool().
  555. **/
  556. EFI_HANDLE *
  557. FindMatchingFmpHandles (
  558. IN EFI_GUID *ProtocolGuid,
  559. OUT UINTN *HandleCount
  560. )
  561. {
  562. EFI_STATUS Status;
  563. UINTN TempHandleCount;
  564. EFI_HANDLE *HandleBuffer;
  565. UINTN Index;
  566. UINTN Index2;
  567. UINTN Index3;
  568. EFI_FIRMWARE_IMAGE_DESCRIPTOR *OriginalFmpImageInfoBuf;
  569. EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf;
  570. UINT8 FmpImageInfoCount;
  571. UINTN DescriptorSize;
  572. BOOLEAN MatchFound;
  573. *HandleCount = 0;
  574. TempHandleCount = 0;
  575. HandleBuffer = NULL;
  576. Status = gBS->LocateHandleBuffer (
  577. ByProtocol,
  578. ProtocolGuid,
  579. NULL,
  580. &TempHandleCount,
  581. &HandleBuffer
  582. );
  583. if (EFI_ERROR (Status)) {
  584. return NULL;
  585. }
  586. for (Index = 0; Index < TempHandleCount; Index++) {
  587. OriginalFmpImageInfoBuf = GetFmpImageDescriptors (
  588. HandleBuffer[Index],
  589. ProtocolGuid,
  590. &FmpImageInfoCount,
  591. &DescriptorSize
  592. );
  593. //
  594. // Loop through the set of EFI_FIRMWARE_IMAGE_DESCRIPTORs.
  595. //
  596. MatchFound = FALSE;
  597. if (OriginalFmpImageInfoBuf != NULL) {
  598. FmpImageInfoBuf = OriginalFmpImageInfoBuf;
  599. for (Index2 = 0; Index2 < FmpImageInfoCount; Index2++) {
  600. for (Index3 = 0; Index3 < mSystemFmpPrivate->DescriptorCount; Index3++) {
  601. MatchFound = CompareGuid (
  602. &FmpImageInfoBuf->ImageTypeId,
  603. &mSystemFmpPrivate->ImageDescriptor[Index3].ImageTypeId
  604. );
  605. if (MatchFound) {
  606. break;
  607. }
  608. }
  609. if (MatchFound) {
  610. break;
  611. }
  612. //
  613. // Increment the buffer pointer ahead by the size of the descriptor
  614. //
  615. FmpImageInfoBuf = (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)(((UINT8 *)FmpImageInfoBuf) + DescriptorSize);
  616. }
  617. if (MatchFound) {
  618. HandleBuffer[*HandleCount] = HandleBuffer[Index];
  619. (*HandleCount)++;
  620. }
  621. FreePool (OriginalFmpImageInfoBuf);
  622. }
  623. }
  624. if ((*HandleCount) == 0) {
  625. //
  626. // No any matching handle.
  627. //
  628. FreePool (HandleBuffer);
  629. return NULL;
  630. }
  631. return HandleBuffer;
  632. }
  633. /**
  634. Uninstall System FMP Protocol instances that may have been installed by
  635. SystemFirmwareUpdateDxe drivers dispatches by other capsules.
  636. @retval EFI_SUCCESS All System FMP Protocols found were uninstalled.
  637. @return Other One or more System FMP Protocols could not be uninstalled.
  638. **/
  639. EFI_STATUS
  640. UninstallMatchingSystemFmpProtocols (
  641. VOID
  642. )
  643. {
  644. EFI_STATUS Status;
  645. EFI_HANDLE *HandleBuffer;
  646. UINTN HandleCount;
  647. UINTN Index;
  648. EFI_FIRMWARE_MANAGEMENT_PROTOCOL *SystemFmp;
  649. //
  650. // Uninstall SystemFmpProtocol instances that may have been produced by
  651. // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.
  652. //
  653. HandleBuffer = FindMatchingFmpHandles (
  654. &gSystemFmpProtocolGuid,
  655. &HandleCount
  656. );
  657. DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Found %d matching System FMP instances\n", HandleCount));
  658. for (Index = 0; Index < HandleCount; Index++) {
  659. Status = gBS->HandleProtocol (
  660. HandleBuffer[Index],
  661. &gSystemFmpProtocolGuid,
  662. (VOID **)&SystemFmp
  663. );
  664. if (EFI_ERROR (Status)) {
  665. continue;
  666. }
  667. DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Uninstall SystemFmp produced by another capsule\n"));
  668. Status = gBS->UninstallProtocolInterface (
  669. HandleBuffer[Index],
  670. &gSystemFmpProtocolGuid,
  671. SystemFmp
  672. );
  673. if (EFI_ERROR (Status)) {
  674. DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: Failed to uninstall SystemFmp %r. Exiting.\n", Status));
  675. FreePool (HandleBuffer);
  676. return Status;
  677. }
  678. }
  679. if (HandleBuffer != NULL) {
  680. FreePool (HandleBuffer);
  681. }
  682. return EFI_SUCCESS;
  683. }
  684. /**
  685. System FMP module entrypoint
  686. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  687. @param[in] SystemTable A pointer to the EFI System Table.
  688. @retval EFI_SUCCESS System FMP module is initialized.
  689. @retval EFI_OUT_OF_RESOURCES There are not enough resources avaulable to
  690. initialize this module.
  691. @retval Other System FMP Protocols could not be uninstalled.
  692. @retval Other System FMP Protocol could not be installed.
  693. @retval Other FMP Protocol could not be installed.
  694. **/
  695. EFI_STATUS
  696. EFIAPI
  697. SystemFirmwareUpdateMainDxe (
  698. IN EFI_HANDLE ImageHandle,
  699. IN EFI_SYSTEM_TABLE *SystemTable
  700. )
  701. {
  702. EFI_STATUS Status;
  703. EFI_HANDLE *HandleBuffer;
  704. UINTN HandleCount;
  705. //
  706. // Initialize SystemFmpPrivateData
  707. //
  708. mSystemFmpPrivate = AllocateZeroPool (sizeof (SYSTEM_FMP_PRIVATE_DATA));
  709. if (mSystemFmpPrivate == NULL) {
  710. return EFI_OUT_OF_RESOURCES;
  711. }
  712. Status = InitializePrivateData (mSystemFmpPrivate);
  713. if (EFI_ERROR (Status)) {
  714. FreePool (mSystemFmpPrivate);
  715. mSystemFmpPrivate = NULL;
  716. return Status;
  717. }
  718. //
  719. // Uninstall SystemFmpProtocol instances that may have been produced by
  720. // the SystemFirmwareUpdate drivers in FVs dispatched by other capsules.
  721. //
  722. Status = UninstallMatchingSystemFmpProtocols ();
  723. if (EFI_ERROR (Status)) {
  724. FreePool (mSystemFmpPrivate);
  725. mSystemFmpPrivate = NULL;
  726. return Status;
  727. }
  728. //
  729. // Look for a handle with matching Firmware Management Protocol
  730. //
  731. HandleCount = 0;
  732. HandleBuffer = FindMatchingFmpHandles (
  733. &gEfiFirmwareManagementProtocolGuid,
  734. &HandleCount
  735. );
  736. DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Found %d matching FMP instances\n", HandleCount));
  737. switch (HandleCount) {
  738. case 0:
  739. //
  740. // Install FMP protocol onto a new handle.
  741. //
  742. DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Install FMP onto a new handle\n"));
  743. Status = gBS->InstallMultipleProtocolInterfaces (
  744. &mSystemFmpPrivate->Handle,
  745. &gEfiFirmwareManagementProtocolGuid,
  746. &mSystemFmpPrivate->Fmp,
  747. NULL
  748. );
  749. break;
  750. case 1:
  751. //
  752. // Install System FMP protocol onto handle with matching FMP Protocol
  753. //
  754. DEBUG ((DEBUG_INFO, "SystemFirmwareUpdateDxe: Install System FMP onto matching FMP handle\n"));
  755. mSystemFmpPrivate->Handle = HandleBuffer[0];
  756. Status = gBS->InstallMultipleProtocolInterfaces (
  757. &HandleBuffer[0],
  758. &gSystemFmpProtocolGuid,
  759. &mSystemFmpPrivate->Fmp,
  760. NULL
  761. );
  762. break;
  763. default:
  764. //
  765. // More than one matching handle is not expected. Unload driver.
  766. //
  767. DEBUG ((DEBUG_ERROR, "SystemFirmwareUpdateDxe: More than one matching FMP handle. Unload driver.\n"));
  768. Status = EFI_DEVICE_ERROR;
  769. break;
  770. }
  771. if (HandleBuffer != NULL) {
  772. FreePool (HandleBuffer);
  773. }
  774. if (EFI_ERROR (Status)) {
  775. FreePool (mSystemFmpPrivate);
  776. mSystemFmpPrivate = NULL;
  777. }
  778. return Status;
  779. }