EdkiiSystemCapsuleLib.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /** @file
  2. EDKII System Capsule library.
  3. EDKII System Capsule library instance.
  4. CapsuleAuthenticateSystemFirmware(), ExtractAuthenticatedImage() will receive
  5. untrusted input and do basic validation.
  6. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include <PiDxe.h>
  10. #include <Guid/SystemResourceTable.h>
  11. #include <Guid/FirmwareContentsSigned.h>
  12. #include <Guid/WinCertificate.h>
  13. #include <Guid/EdkiiSystemFmpCapsule.h>
  14. #include <Guid/WinCertificate.h>
  15. #include <Guid/ImageAuthentication.h>
  16. #include <Library/BaseLib.h>
  17. #include <Library/BaseMemoryLib.h>
  18. #include <Library/DebugLib.h>
  19. #include <Library/PcdLib.h>
  20. #include <Library/MemoryAllocationLib.h>
  21. #include <Library/EdkiiSystemCapsuleLib.h>
  22. #include <Library/FmpAuthenticationLib.h>
  23. #include <Protocol/FirmwareManagement.h>
  24. EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR *mImageFmpInfo;
  25. UINTN mImageFmpInfoSize;
  26. EFI_GUID mEdkiiSystemFirmwareFileGuid;
  27. /**
  28. Check if a block of buffer is erased.
  29. @param[in] ErasePolarity Erase polarity attribute of the firmware volume
  30. @param[in] InBuffer The buffer to be checked
  31. @param[in] BufferSize Size of the buffer in bytes
  32. @retval TRUE The block of buffer is erased
  33. @retval FALSE The block of buffer is not erased
  34. **/
  35. BOOLEAN
  36. IsBufferErased (
  37. IN UINT8 ErasePolarity,
  38. IN VOID *InBuffer,
  39. IN UINTN BufferSize
  40. )
  41. {
  42. UINTN Count;
  43. UINT8 EraseByte;
  44. UINT8 *Buffer;
  45. if (ErasePolarity == 1) {
  46. EraseByte = 0xFF;
  47. } else {
  48. EraseByte = 0;
  49. }
  50. Buffer = InBuffer;
  51. for (Count = 0; Count < BufferSize; Count++) {
  52. if (Buffer[Count] != EraseByte) {
  53. return FALSE;
  54. }
  55. }
  56. return TRUE;
  57. }
  58. /**
  59. Get Section buffer pointer by SectionType and SectionInstance.
  60. @param[in] SectionBuffer The buffer of section
  61. @param[in] SectionBufferSize The size of SectionBuffer in bytes
  62. @param[in] SectionType The SectionType of Section to be found
  63. @param[in] SectionInstance The Instance of Section to be found
  64. @param[out] OutSectionBuffer The section found, including SECTION_HEADER
  65. @param[out] OutSectionSize The size of section found, including SECTION_HEADER
  66. @retval TRUE The FFS buffer is found.
  67. @retval FALSE The FFS buffer is not found.
  68. **/
  69. BOOLEAN
  70. GetSectionByType (
  71. IN VOID *SectionBuffer,
  72. IN UINT32 SectionBufferSize,
  73. IN EFI_SECTION_TYPE SectionType,
  74. IN UINTN SectionInstance,
  75. OUT VOID **OutSectionBuffer,
  76. OUT UINTN *OutSectionSize
  77. )
  78. {
  79. EFI_COMMON_SECTION_HEADER *SectionHeader;
  80. UINTN SectionSize;
  81. UINTN Instance;
  82. DEBUG ((DEBUG_INFO, "GetSectionByType - Buffer: 0x%08x - 0x%08x\n", SectionBuffer, SectionBufferSize));
  83. //
  84. // Find Section
  85. //
  86. SectionHeader = SectionBuffer;
  87. Instance = 0;
  88. while ((UINTN)SectionHeader < (UINTN)SectionBuffer + SectionBufferSize) {
  89. DEBUG ((DEBUG_INFO, "GetSectionByType - Section: 0x%08x\n", SectionHeader));
  90. if (IS_SECTION2 (SectionHeader)) {
  91. SectionSize = SECTION2_SIZE (SectionHeader);
  92. } else {
  93. SectionSize = SECTION_SIZE (SectionHeader);
  94. }
  95. if (SectionHeader->Type == SectionType) {
  96. if (Instance == SectionInstance) {
  97. *OutSectionBuffer = (UINT8 *)SectionHeader;
  98. *OutSectionSize = SectionSize;
  99. DEBUG ((DEBUG_INFO, "GetSectionByType - 0x%x - 0x%x\n", *OutSectionBuffer, *OutSectionSize));
  100. return TRUE;
  101. } else {
  102. DEBUG ((DEBUG_INFO, "GetSectionByType - find section instance %x\n", Instance));
  103. Instance++;
  104. }
  105. } else {
  106. //
  107. // Skip other section type
  108. //
  109. DEBUG ((DEBUG_INFO, "GetSectionByType - other section type 0x%x\n", SectionHeader->Type));
  110. }
  111. //
  112. // Next Section
  113. //
  114. SectionHeader = (EFI_COMMON_SECTION_HEADER *)((UINTN)SectionHeader + ALIGN_VALUE (SectionSize, 4));
  115. }
  116. return FALSE;
  117. }
  118. /**
  119. Get FFS buffer pointer by FileName GUID and FileType.
  120. @param[in] FdStart The System Firmware FD image
  121. @param[in] FdSize The size of System Firmware FD image
  122. @param[in] FileName The FileName GUID of FFS to be found
  123. @param[in] Type The FileType of FFS to be found
  124. @param[out] OutFfsBuffer The FFS buffer found, including FFS_FILE_HEADER
  125. @param[out] OutFfsBufferSize The size of FFS buffer found, including FFS_FILE_HEADER
  126. @retval TRUE The FFS buffer is found.
  127. @retval FALSE The FFS buffer is not found.
  128. **/
  129. BOOLEAN
  130. GetFfsByName (
  131. IN VOID *FdStart,
  132. IN UINTN FdSize,
  133. IN EFI_GUID *FileName,
  134. IN EFI_FV_FILETYPE Type,
  135. OUT VOID **OutFfsBuffer,
  136. OUT UINTN *OutFfsBufferSize
  137. )
  138. {
  139. UINTN FvSize;
  140. EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
  141. EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
  142. EFI_FFS_FILE_HEADER *FfsHeader;
  143. UINT32 FfsSize;
  144. UINTN TestLength;
  145. BOOLEAN FvFound;
  146. DEBUG ((DEBUG_INFO, "GetFfsByName - FV: 0x%08x - 0x%08x\n", (UINTN)FdStart, (UINTN)FdSize));
  147. FvFound = FALSE;
  148. FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FdStart;
  149. while ((UINTN)FvHeader < (UINTN)FdStart + FdSize - 1) {
  150. FvSize = (UINTN)FdStart + FdSize - (UINTN)FvHeader;
  151. if (FvHeader->Signature != EFI_FVH_SIGNATURE) {
  152. FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvHeader + SIZE_4KB);
  153. continue;
  154. }
  155. DEBUG ((DEBUG_INFO, "checking FV....0x%08x - 0x%x\n", FvHeader, FvHeader->FvLength));
  156. FvFound = TRUE;
  157. if (FvHeader->FvLength > FvSize) {
  158. DEBUG ((DEBUG_ERROR, "GetFfsByName - FvSize: 0x%08x, MaxSize - 0x%08x\n", (UINTN)FvHeader->FvLength, (UINTN)FvSize));
  159. return FALSE;
  160. }
  161. FvSize = (UINTN)FvHeader->FvLength;
  162. //
  163. // Find FFS
  164. //
  165. if (FvHeader->ExtHeaderOffset != 0) {
  166. FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)FvHeader + FvHeader->ExtHeaderOffset);
  167. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvExtHeader + FvExtHeader->ExtHeaderSize);
  168. } else {
  169. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FvHeader + FvHeader->HeaderLength);
  170. }
  171. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FvHeader + ALIGN_VALUE ((UINTN)FfsHeader - (UINTN)FvHeader, 8));
  172. while ((UINTN)FfsHeader < (UINTN)FvHeader + FvSize - 1) {
  173. DEBUG ((DEBUG_INFO, "GetFfsByName - FFS: 0x%08x\n", FfsHeader));
  174. TestLength = (UINTN)((UINTN)FvHeader + FvSize - (UINTN)FfsHeader);
  175. if (TestLength > sizeof (EFI_FFS_FILE_HEADER)) {
  176. TestLength = sizeof (EFI_FFS_FILE_HEADER);
  177. }
  178. if (IsBufferErased (1, FfsHeader, TestLength)) {
  179. break;
  180. }
  181. if (IS_FFS_FILE2 (FfsHeader)) {
  182. FfsSize = FFS_FILE2_SIZE (FfsHeader);
  183. } else {
  184. FfsSize = FFS_FILE_SIZE (FfsHeader);
  185. }
  186. if (CompareGuid (FileName, &FfsHeader->Name) &&
  187. ((Type == EFI_FV_FILETYPE_ALL) || (FfsHeader->Type == Type)))
  188. {
  189. *OutFfsBuffer = FfsHeader;
  190. *OutFfsBufferSize = FfsSize;
  191. return TRUE;
  192. } else {
  193. //
  194. // Any other type is not allowed
  195. //
  196. DEBUG ((DEBUG_INFO, "GetFfsByName - other FFS type 0x%x, name %g\n", FfsHeader->Type, &FfsHeader->Name));
  197. }
  198. //
  199. // Next File
  200. //
  201. FfsHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FfsHeader + ALIGN_VALUE (FfsSize, 8));
  202. }
  203. //
  204. // Next FV
  205. //
  206. FvHeader = (VOID *)(UINTN)((UINTN)FvHeader + FvHeader->FvLength);
  207. }
  208. if (!FvFound) {
  209. DEBUG ((DEBUG_ERROR, "GetFfsByName - NO FV Found\n"));
  210. }
  211. return FALSE;
  212. }
  213. /**
  214. Extract the driver FV from an authenticated image.
  215. @param[in] AuthenticatedImage The authenticated capsule image.
  216. @param[in] AuthenticatedImageSize The size of the authenticated capsule image in bytes.
  217. @param[out] DriverFvImage The driver FV image.
  218. @param[out] DriverFvImageSize The size of the driver FV image in bytes.
  219. @retval TRUE The driver Fv is extracted.
  220. @retval FALSE The driver Fv is not extracted.
  221. **/
  222. BOOLEAN
  223. EFIAPI
  224. ExtractDriverFvImage (
  225. IN VOID *AuthenticatedImage,
  226. IN UINTN AuthenticatedImageSize,
  227. OUT VOID **DriverFvImage,
  228. OUT UINTN *DriverFvImageSize
  229. )
  230. {
  231. BOOLEAN Result;
  232. UINT32 FileHeaderSize;
  233. *DriverFvImage = NULL;
  234. *DriverFvImageSize = 0;
  235. Result = GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &gEdkiiSystemFmpCapsuleDriverFvFileGuid, EFI_FV_FILETYPE_RAW, DriverFvImage, DriverFvImageSize);
  236. if (!Result) {
  237. return FALSE;
  238. }
  239. if (IS_FFS_FILE2 (*DriverFvImage)) {
  240. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  241. } else {
  242. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  243. }
  244. *DriverFvImage = (UINT8 *)*DriverFvImage + FileHeaderSize;
  245. *DriverFvImageSize = *DriverFvImageSize - FileHeaderSize;
  246. return Result;
  247. }
  248. /**
  249. Extract the config image from an authenticated image.
  250. @param[in] AuthenticatedImage The authenticated capsule image.
  251. @param[in] AuthenticatedImageSize The size of the authenticated capsule image in bytes.
  252. @param[out] ConfigImage The config image.
  253. @param[out] ConfigImageSize The size of the config image in bytes.
  254. @retval TRUE The config image is extracted.
  255. @retval FALSE The config image is not extracted.
  256. **/
  257. BOOLEAN
  258. EFIAPI
  259. ExtractConfigImage (
  260. IN VOID *AuthenticatedImage,
  261. IN UINTN AuthenticatedImageSize,
  262. OUT VOID **ConfigImage,
  263. OUT UINTN *ConfigImageSize
  264. )
  265. {
  266. BOOLEAN Result;
  267. UINT32 FileHeaderSize;
  268. *ConfigImage = NULL;
  269. *ConfigImageSize = 0;
  270. Result = GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &gEdkiiSystemFmpCapsuleConfigFileGuid, EFI_FV_FILETYPE_RAW, ConfigImage, ConfigImageSize);
  271. if (!Result) {
  272. return FALSE;
  273. }
  274. if (IS_FFS_FILE2 (*ConfigImage)) {
  275. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  276. } else {
  277. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  278. }
  279. *ConfigImage = (UINT8 *)*ConfigImage + FileHeaderSize;
  280. *ConfigImageSize = *ConfigImageSize - FileHeaderSize;
  281. return Result;
  282. }
  283. /**
  284. Extract the authenticated image from an FMP capsule image.
  285. Caution: This function may receive untrusted input.
  286. @param[in] Image The FMP capsule image, including EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  287. @param[in] ImageSize The size of FMP capsule image in bytes.
  288. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  289. @param[out] AuthenticatedImage The authenticated capsule image, excluding EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  290. @param[out] AuthenticatedImageSize The size of the authenticated capsule image in bytes.
  291. @retval TRUE The authenticated image is extracted.
  292. @retval FALSE The authenticated image is not extracted.
  293. **/
  294. BOOLEAN
  295. EFIAPI
  296. ExtractAuthenticatedImage (
  297. IN VOID *Image,
  298. IN UINTN ImageSize,
  299. OUT UINT32 *LastAttemptStatus,
  300. OUT VOID **AuthenticatedImage,
  301. OUT UINTN *AuthenticatedImageSize
  302. )
  303. {
  304. EFI_FIRMWARE_IMAGE_AUTHENTICATION *ImageAuth;
  305. EFI_STATUS Status;
  306. GUID *CertType;
  307. VOID *PublicKeyData;
  308. UINTN PublicKeyDataLength;
  309. DEBUG ((DEBUG_INFO, "ExtractAuthenticatedImage - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize));
  310. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  311. if ((Image == NULL) || (ImageSize == 0)) {
  312. return FALSE;
  313. }
  314. ImageAuth = (EFI_FIRMWARE_IMAGE_AUTHENTICATION *)Image;
  315. if (ImageSize < sizeof (EFI_FIRMWARE_IMAGE_AUTHENTICATION)) {
  316. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - ImageSize too small\n"));
  317. return FALSE;
  318. }
  319. if (ImageAuth->AuthInfo.Hdr.dwLength <= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {
  320. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - dwLength too small\n"));
  321. return FALSE;
  322. }
  323. if ((UINTN)ImageAuth->AuthInfo.Hdr.dwLength > MAX_UINTN - sizeof (UINT64)) {
  324. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - dwLength too big\n"));
  325. return FALSE;
  326. }
  327. if (ImageSize <= sizeof (ImageAuth->MonotonicCount) + ImageAuth->AuthInfo.Hdr.dwLength) {
  328. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - ImageSize too small\n"));
  329. return FALSE;
  330. }
  331. if (ImageAuth->AuthInfo.Hdr.wRevision != 0x0200) {
  332. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - wRevision: 0x%02x, expect - 0x%02x\n", (UINTN)ImageAuth->AuthInfo.Hdr.wRevision, (UINTN)0x0200));
  333. return FALSE;
  334. }
  335. if (ImageAuth->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) {
  336. DEBUG ((DEBUG_ERROR, "ExtractAuthenticatedImage - wCertificateType: 0x%02x, expect - 0x%02x\n", (UINTN)ImageAuth->AuthInfo.Hdr.wCertificateType, (UINTN)WIN_CERT_TYPE_EFI_GUID));
  337. return FALSE;
  338. }
  339. CertType = &ImageAuth->AuthInfo.CertType;
  340. DEBUG ((DEBUG_INFO, "ExtractAuthenticatedImage - CertType: %g\n", CertType));
  341. if (CompareGuid (&gEfiCertPkcs7Guid, CertType)) {
  342. PublicKeyData = PcdGetPtr (PcdPkcs7CertBuffer);
  343. PublicKeyDataLength = PcdGetSize (PcdPkcs7CertBuffer);
  344. } else if (CompareGuid (&gEfiCertTypeRsa2048Sha256Guid, CertType)) {
  345. PublicKeyData = PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer);
  346. PublicKeyDataLength = PcdGetSize (PcdRsa2048Sha256PublicKeyBuffer);
  347. } else {
  348. return FALSE;
  349. }
  350. ASSERT (PublicKeyData != NULL);
  351. ASSERT (PublicKeyDataLength != 0);
  352. Status = AuthenticateFmpImage (
  353. ImageAuth,
  354. ImageSize,
  355. PublicKeyData,
  356. PublicKeyDataLength
  357. );
  358. switch (Status) {
  359. case RETURN_SUCCESS:
  360. *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  361. break;
  362. case RETURN_SECURITY_VIOLATION:
  363. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;
  364. break;
  365. case RETURN_INVALID_PARAMETER:
  366. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  367. break;
  368. case RETURN_UNSUPPORTED:
  369. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  370. break;
  371. case RETURN_OUT_OF_RESOURCES:
  372. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;
  373. break;
  374. default:
  375. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
  376. break;
  377. }
  378. if (EFI_ERROR (Status)) {
  379. return FALSE;
  380. }
  381. if (AuthenticatedImage != NULL) {
  382. *AuthenticatedImage = (UINT8 *)ImageAuth + ImageAuth->AuthInfo.Hdr.dwLength + sizeof (ImageAuth->MonotonicCount);
  383. }
  384. if (AuthenticatedImageSize != NULL) {
  385. *AuthenticatedImageSize = ImageSize - ImageAuth->AuthInfo.Hdr.dwLength - sizeof (ImageAuth->MonotonicCount);
  386. }
  387. return TRUE;
  388. }
  389. /**
  390. Extract ImageFmpInfo from system firmware.
  391. @param[in] SystemFirmwareImage The System Firmware image.
  392. @param[in] SystemFirmwareImageSize The size of the System Firmware image in bytes.
  393. @param[out] ImageFmpInfo The ImageFmpInfo.
  394. @param[out] ImageFmpInfoSize The size of the ImageFmpInfo in bytes.
  395. @retval TRUE The ImageFmpInfo is extracted.
  396. @retval FALSE The ImageFmpInfo is not extracted.
  397. **/
  398. BOOLEAN
  399. EFIAPI
  400. ExtractSystemFirmwareImageFmpInfo (
  401. IN VOID *SystemFirmwareImage,
  402. IN UINTN SystemFirmwareImageSize,
  403. OUT EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR **ImageFmpInfo,
  404. OUT UINTN *ImageFmpInfoSize
  405. )
  406. {
  407. BOOLEAN Result;
  408. UINT32 SectionHeaderSize;
  409. UINT32 FileHeaderSize;
  410. *ImageFmpInfo = NULL;
  411. *ImageFmpInfoSize = 0;
  412. Result = GetFfsByName (SystemFirmwareImage, SystemFirmwareImageSize, &gEdkiiSystemFirmwareImageDescriptorFileGuid, EFI_FV_FILETYPE_ALL, (VOID **)ImageFmpInfo, ImageFmpInfoSize);
  413. if (!Result) {
  414. return FALSE;
  415. }
  416. if (IS_FFS_FILE2 (*ImageFmpInfo)) {
  417. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  418. } else {
  419. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  420. }
  421. *ImageFmpInfo = (VOID *)((UINT8 *)*ImageFmpInfo + FileHeaderSize);
  422. *ImageFmpInfoSize = *ImageFmpInfoSize - FileHeaderSize;
  423. Result = GetSectionByType (*ImageFmpInfo, (UINT32)*ImageFmpInfoSize, EFI_SECTION_RAW, 0, (VOID **)ImageFmpInfo, ImageFmpInfoSize);
  424. if (!Result) {
  425. return FALSE;
  426. }
  427. if (IS_SECTION2 (*ImageFmpInfo)) {
  428. SectionHeaderSize = sizeof (EFI_RAW_SECTION2);
  429. } else {
  430. SectionHeaderSize = sizeof (EFI_RAW_SECTION);
  431. }
  432. *ImageFmpInfo = (VOID *)((UINT8 *)*ImageFmpInfo + SectionHeaderSize);
  433. *ImageFmpInfoSize = *ImageFmpInfoSize - SectionHeaderSize;
  434. return TRUE;
  435. }
  436. /**
  437. Extract the System Firmware image from an authenticated image.
  438. @param[in] AuthenticatedImage The authenticated capsule image.
  439. @param[in] AuthenticatedImageSize The size of the authenticated capsule image in bytes.
  440. @param[out] SystemFirmwareImage The System Firmware image.
  441. @param[out] SystemFirmwareImageSize The size of the System Firmware image in bytes.
  442. @retval TRUE The System Firmware image is extracted.
  443. @retval FALSE The System Firmware image is not extracted.
  444. **/
  445. BOOLEAN
  446. EFIAPI
  447. ExtractSystemFirmwareImage (
  448. IN VOID *AuthenticatedImage,
  449. IN UINTN AuthenticatedImageSize,
  450. OUT VOID **SystemFirmwareImage,
  451. OUT UINTN *SystemFirmwareImageSize
  452. )
  453. {
  454. BOOLEAN Result;
  455. UINT32 FileHeaderSize;
  456. *SystemFirmwareImage = NULL;
  457. *SystemFirmwareImageSize = 0;
  458. Result = GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &mEdkiiSystemFirmwareFileGuid, EFI_FV_FILETYPE_RAW, SystemFirmwareImage, SystemFirmwareImageSize);
  459. if (!Result) {
  460. // no nested FV, just return all data.
  461. *SystemFirmwareImage = AuthenticatedImage;
  462. *SystemFirmwareImageSize = AuthenticatedImageSize;
  463. return TRUE;
  464. }
  465. if (IS_FFS_FILE2 (*SystemFirmwareImage)) {
  466. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  467. } else {
  468. FileHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  469. }
  470. *SystemFirmwareImage = (UINT8 *)*SystemFirmwareImage + FileHeaderSize;
  471. *SystemFirmwareImageSize = *SystemFirmwareImageSize - FileHeaderSize;
  472. return Result;
  473. }
  474. /**
  475. Authenticated system firmware FMP capsule image.
  476. Caution: This function may receive untrusted input.
  477. @param[in] Image The FMP capsule image, including EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  478. @param[in] ImageSize The size of FMP capsule image in bytes.
  479. @param[in] ForceVersionMatch TRUE: The version of capsule must be as same as the version of current image.
  480. FALSE: The version of capsule must be as same as greater than the lowest
  481. supported version of current image.
  482. @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  483. @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
  484. @param[out] AuthenticatedImage The authenticated capsule image, excluding EFI_FIRMWARE_IMAGE_AUTHENTICATION.
  485. @param[out] AuthenticatedImageSize The size of the authenticated capsule image in bytes.
  486. @retval TRUE Authentication passes and the authenticated image is extracted.
  487. @retval FALSE Authentication fails and the authenticated image is not extracted.
  488. **/
  489. EFI_STATUS
  490. EFIAPI
  491. CapsuleAuthenticateSystemFirmware (
  492. IN VOID *Image,
  493. IN UINTN ImageSize,
  494. IN BOOLEAN ForceVersionMatch,
  495. OUT UINT32 *LastAttemptVersion,
  496. OUT UINT32 *LastAttemptStatus,
  497. OUT VOID **AuthenticatedImage,
  498. OUT UINTN *AuthenticatedImageSize
  499. )
  500. {
  501. BOOLEAN Result;
  502. EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR *ImageFmpInfo;
  503. UINTN ImageFmpInfoSize;
  504. EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR *CurrentImageFmpInfo;
  505. UINTN CurrentImageFmpInfoSize;
  506. VOID *SystemFirmwareImage;
  507. UINTN SystemFirmwareImageSize;
  508. *LastAttemptVersion = 0;
  509. //
  510. // NOTE: This function need run in an isolated environment.
  511. // Do not touch FMP protocol and its private structure.
  512. //
  513. if (mImageFmpInfo == NULL) {
  514. DEBUG ((DEBUG_INFO, "ImageFmpInfo is not set\n"));
  515. return EFI_SECURITY_VIOLATION;
  516. }
  517. Result = ExtractAuthenticatedImage ((VOID *)Image, ImageSize, LastAttemptStatus, AuthenticatedImage, AuthenticatedImageSize);
  518. if (!Result) {
  519. DEBUG ((DEBUG_INFO, "ExtractAuthenticatedImage - fail\n"));
  520. return EFI_SECURITY_VIOLATION;
  521. }
  522. DEBUG ((DEBUG_INFO, "AuthenticatedImage - 0x%x - 0x%x\n", *AuthenticatedImage, *AuthenticatedImageSize));
  523. Result = ExtractSystemFirmwareImage (*AuthenticatedImage, *AuthenticatedImageSize, &SystemFirmwareImage, &SystemFirmwareImageSize);
  524. if (!Result) {
  525. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  526. DEBUG ((DEBUG_INFO, "ExtractSystemFirmwareImage - fail\n"));
  527. return EFI_SECURITY_VIOLATION;
  528. }
  529. DEBUG ((DEBUG_INFO, "SystemFirmwareImage - 0x%x - 0x%x\n", SystemFirmwareImage, SystemFirmwareImageSize));
  530. Result = ExtractSystemFirmwareImageFmpInfo (SystemFirmwareImage, SystemFirmwareImageSize, &ImageFmpInfo, &ImageFmpInfoSize);
  531. if (!Result) {
  532. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
  533. DEBUG ((DEBUG_INFO, "ExtractSystemFirmwareImageFmpInfo - fail\n"));
  534. return EFI_SECURITY_VIOLATION;
  535. }
  536. *LastAttemptVersion = ImageFmpInfo->Version;
  537. DEBUG ((DEBUG_INFO, "ImageFmpInfo - 0x%x - 0x%x\n", ImageFmpInfo, ImageFmpInfoSize));
  538. DEBUG ((DEBUG_INFO, "NewImage Version - 0x%x\n", ImageFmpInfo->Version));
  539. DEBUG ((DEBUG_INFO, "NewImage LowestSupportedImageVersion - 0x%x\n", ImageFmpInfo->LowestSupportedImageVersion));
  540. CurrentImageFmpInfo = mImageFmpInfo;
  541. CurrentImageFmpInfoSize = mImageFmpInfoSize;
  542. DEBUG ((DEBUG_INFO, "ImageFmpInfo - 0x%x - 0x%x\n", CurrentImageFmpInfo, CurrentImageFmpInfoSize));
  543. DEBUG ((DEBUG_INFO, "Current Version - 0x%x\n", CurrentImageFmpInfo->Version));
  544. DEBUG ((DEBUG_INFO, "Current LowestSupportedImageVersion - 0x%x\n", CurrentImageFmpInfo->LowestSupportedImageVersion));
  545. if (ForceVersionMatch) {
  546. if (CurrentImageFmpInfo->Version != ImageFmpInfo->Version) {
  547. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;
  548. DEBUG ((DEBUG_INFO, "ForceVersionMatch check - fail\n"));
  549. return EFI_SECURITY_VIOLATION;
  550. }
  551. } else {
  552. if (ImageFmpInfo->Version < CurrentImageFmpInfo->LowestSupportedImageVersion) {
  553. *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;
  554. DEBUG ((DEBUG_INFO, "LowestSupportedImageVersion check - fail\n"));
  555. return EFI_SECURITY_VIOLATION;
  556. }
  557. }
  558. *LastAttemptStatus = LAST_ATTEMPT_STATUS_SUCCESS;
  559. return EFI_SUCCESS;
  560. }
  561. /**
  562. PcdCallBack gets the real set PCD value
  563. @param[in] CallBackGuid The PCD token GUID being set.
  564. @param[in] CallBackToken The PCD token number being set.
  565. @param[in, out] TokenData A pointer to the token data being set.
  566. @param[in] TokenDataSize The size, in bytes, of the data being set.
  567. **/
  568. VOID
  569. EFIAPI
  570. EdkiiSystemCapsuleLibPcdCallBack (
  571. IN CONST GUID *CallBackGuid OPTIONAL,
  572. IN UINTN CallBackToken,
  573. IN OUT VOID *TokenData,
  574. IN UINTN TokenDataSize
  575. )
  576. {
  577. if (CompareGuid (CallBackGuid, &gEfiSignedCapsulePkgTokenSpaceGuid) &&
  578. (CallBackToken == PcdToken (PcdEdkiiSystemFirmwareImageDescriptor)))
  579. {
  580. mImageFmpInfoSize = TokenDataSize;
  581. mImageFmpInfo = AllocateCopyPool (mImageFmpInfoSize, TokenData);
  582. ASSERT (mImageFmpInfo != NULL);
  583. //
  584. // Cancel Callback after get the real set value
  585. //
  586. LibPcdCancelCallback (
  587. &gEfiSignedCapsulePkgTokenSpaceGuid,
  588. PcdToken (PcdEdkiiSystemFirmwareImageDescriptor),
  589. EdkiiSystemCapsuleLibPcdCallBack
  590. );
  591. }
  592. if (CompareGuid (CallBackGuid, &gEfiSignedCapsulePkgTokenSpaceGuid) &&
  593. (CallBackToken == PcdToken (PcdEdkiiSystemFirmwareFileGuid)))
  594. {
  595. CopyGuid (&mEdkiiSystemFirmwareFileGuid, TokenData);
  596. //
  597. // Cancel Callback after get the real set value
  598. //
  599. LibPcdCancelCallback (
  600. &gEfiSignedCapsulePkgTokenSpaceGuid,
  601. PcdToken (PcdEdkiiSystemFirmwareFileGuid),
  602. EdkiiSystemCapsuleLibPcdCallBack
  603. );
  604. }
  605. }
  606. /**
  607. The constructor function.
  608. @retval EFI_SUCCESS The constructor successfully .
  609. **/
  610. EFI_STATUS
  611. EFIAPI
  612. EdkiiSystemCapsuleLibConstructor (
  613. VOID
  614. )
  615. {
  616. mImageFmpInfoSize = PcdGetSize (PcdEdkiiSystemFirmwareImageDescriptor);
  617. mImageFmpInfo = PcdGetPtr (PcdEdkiiSystemFirmwareImageDescriptor);
  618. //
  619. // Verify Firmware Image Descriptor first
  620. //
  621. if ((mImageFmpInfoSize < sizeof (EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR)) ||
  622. (mImageFmpInfo->Signature != EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR_SIGNATURE))
  623. {
  624. //
  625. // SystemFirmwareImageDescriptor is not set.
  626. // Register PCD set callback to hook PCD value set.
  627. //
  628. mImageFmpInfo = NULL;
  629. mImageFmpInfoSize = 0;
  630. LibPcdCallbackOnSet (
  631. &gEfiSignedCapsulePkgTokenSpaceGuid,
  632. PcdToken (PcdEdkiiSystemFirmwareImageDescriptor),
  633. EdkiiSystemCapsuleLibPcdCallBack
  634. );
  635. } else {
  636. mImageFmpInfo = AllocateCopyPool (mImageFmpInfoSize, mImageFmpInfo);
  637. ASSERT (mImageFmpInfo != NULL);
  638. }
  639. CopyGuid (&mEdkiiSystemFirmwareFileGuid, PcdGetPtr (PcdEdkiiSystemFirmwareFileGuid));
  640. //
  641. // Verify GUID value first
  642. //
  643. if (CompareGuid (&mEdkiiSystemFirmwareFileGuid, &gZeroGuid)) {
  644. LibPcdCallbackOnSet (
  645. &gEfiSignedCapsulePkgTokenSpaceGuid,
  646. PcdToken (PcdEdkiiSystemFirmwareFileGuid),
  647. EdkiiSystemCapsuleLibPcdCallBack
  648. );
  649. }
  650. return EFI_SUCCESS;
  651. }