34UINTN mImageFmpInfoSize;
35EFI_GUID mEdkiiSystemFirmwareFileGuid;
49 IN UINT8 ErasePolarity,
58 if (ErasePolarity == 1) {
65 for (Count = 0; Count < BufferSize; Count++) {
66 if (Buffer[Count] != EraseByte) {
89 IN VOID *SectionBuffer,
90 IN UINT32 SectionBufferSize,
91 IN EFI_SECTION_TYPE SectionType,
93 OUT VOID **OutSectionBuffer,
101 DEBUG ((DEBUG_INFO,
"GetSectionByType - Buffer: 0x%08x - 0x%08x\n", SectionBuffer, SectionBufferSize));
106 SectionHeader = SectionBuffer;
109 while ((
UINTN)SectionHeader < (
UINTN)SectionBuffer + SectionBufferSize) {
110 DEBUG ((DEBUG_INFO,
"GetSectionByType - Section: 0x%08x\n", SectionHeader));
111 if (IS_SECTION2 (SectionHeader)) {
112 SectionSize = SECTION2_SIZE (SectionHeader);
117 if (SectionHeader->Type == SectionType) {
118 if (Instance == SectionInstance) {
119 *OutSectionBuffer = (UINT8 *)SectionHeader;
120 *OutSectionSize = SectionSize;
121 DEBUG ((DEBUG_INFO,
"GetSectionByType - 0x%x - 0x%x\n", *OutSectionBuffer, *OutSectionSize));
124 DEBUG ((DEBUG_INFO,
"GetSectionByType - find section instance %x\n", Instance));
131 DEBUG ((DEBUG_INFO,
"GetSectionByType - other section type 0x%x\n", SectionHeader->Type));
161 IN EFI_FV_FILETYPE Type,
162 OUT VOID **OutFfsBuffer,
174 DEBUG ((DEBUG_INFO,
"GetFfsByName - FV: 0x%08x - 0x%08x\n", (
UINTN)FdStart, (
UINTN)FdSize));
178 while ((
UINTN)FvHeader < (
UINTN)FdStart + FdSize - 1) {
179 FvSize = (
UINTN)FdStart + FdSize - (
UINTN)FvHeader;
181 if (FvHeader->
Signature != EFI_FVH_SIGNATURE) {
186 DEBUG ((DEBUG_INFO,
"checking FV....0x%08x - 0x%x\n", FvHeader, FvHeader->
FvLength));
189 DEBUG ((DEBUG_ERROR,
"GetFfsByName - FvSize: 0x%08x, MaxSize - 0x%08x\n", (
UINTN)FvHeader->
FvLength, (
UINTN)FvSize));
207 while ((
UINTN)FfsHeader < (
UINTN)FvHeader + FvSize - 1) {
208 DEBUG ((DEBUG_INFO,
"GetFfsByName - FFS: 0x%08x\n", FfsHeader));
218 if (IS_FFS_FILE2 (FfsHeader)) {
219 FfsSize = FFS_FILE2_SIZE (FfsHeader);
227 *OutFfsBuffer = FfsHeader;
228 *OutFfsBufferSize = FfsSize;
234 DEBUG ((DEBUG_INFO,
"GetFfsByName - other FFS type 0x%x, name %g\n", FfsHeader->Type, &FfsHeader->Name));
246 FvHeader = (VOID *)(
UINTN)((
UINTN)FvHeader + FvHeader->FvLength);
250 DEBUG ((DEBUG_ERROR,
"GetFfsByName - NO FV Found\n"));
270 IN VOID *AuthenticatedImage,
271 IN UINTN AuthenticatedImageSize,
272 OUT VOID **DriverFvImage,
277 UINT32 FileHeaderSize;
279 *DriverFvImage =
NULL;
280 *DriverFvImageSize = 0;
282 Result =
GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &gEdkiiSystemFmpCapsuleDriverFvFileGuid, EFI_FV_FILETYPE_RAW, DriverFvImage, DriverFvImageSize);
287 if (IS_FFS_FILE2 (*DriverFvImage)) {
293 *DriverFvImage = (UINT8 *)*DriverFvImage + FileHeaderSize;
294 *DriverFvImageSize = *DriverFvImageSize - FileHeaderSize;
313 IN VOID *AuthenticatedImage,
314 IN UINTN AuthenticatedImageSize,
315 OUT VOID **ConfigImage,
320 UINT32 FileHeaderSize;
323 *ConfigImageSize = 0;
325 Result =
GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &gEdkiiSystemFmpCapsuleConfigFileGuid, EFI_FV_FILETYPE_RAW, ConfigImage, ConfigImageSize);
330 if (IS_FFS_FILE2 (*ConfigImage)) {
336 *ConfigImage = (UINT8 *)*ConfigImage + FileHeaderSize;
337 *ConfigImageSize = *ConfigImageSize - FileHeaderSize;
361 OUT UINT32 *LastAttemptStatus,
362 OUT VOID **AuthenticatedImage,
370 UINTN PublicKeyDataLength;
372 DEBUG ((DEBUG_INFO,
"ExtractAuthenticatedImage - Image: 0x%08x - 0x%08x\n", (
UINTN)Image, (
UINTN)ImageSize));
374 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
375 if ((Image ==
NULL) || (ImageSize == 0)) {
381 DEBUG ((DEBUG_ERROR,
"ExtractAuthenticatedImage - ImageSize too small\n"));
386 DEBUG ((DEBUG_ERROR,
"ExtractAuthenticatedImage - dwLength too small\n"));
391 DEBUG ((DEBUG_ERROR,
"ExtractAuthenticatedImage - dwLength too big\n"));
396 DEBUG ((DEBUG_ERROR,
"ExtractAuthenticatedImage - ImageSize too small\n"));
411 DEBUG ((DEBUG_INFO,
"ExtractAuthenticatedImage - CertType: %g\n", CertType));
414 PublicKeyData =
PcdGetPtr (PcdPkcs7CertBuffer);
415 PublicKeyDataLength =
PcdGetSize (PcdPkcs7CertBuffer);
416 }
else if (
CompareGuid (&gEfiCertTypeRsa2048Sha256Guid, CertType)) {
417 PublicKeyData =
PcdGetPtr (PcdRsa2048Sha256PublicKeyBuffer);
418 PublicKeyDataLength =
PcdGetSize (PcdRsa2048Sha256PublicKeyBuffer);
423 ASSERT (PublicKeyData !=
NULL);
424 ASSERT (PublicKeyDataLength != 0);
437 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR;
440 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
443 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
446 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES;
449 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL;
453 if (EFI_ERROR (Status)) {
457 if (AuthenticatedImage !=
NULL) {
461 if (AuthenticatedImageSize !=
NULL) {
482 IN VOID *SystemFirmwareImage,
483 IN UINTN SystemFirmwareImageSize,
489 UINT32 SectionHeaderSize;
490 UINT32 FileHeaderSize;
492 *ImageFmpInfo =
NULL;
493 *ImageFmpInfoSize = 0;
500 if (IS_FFS_FILE2 (*ImageFmpInfo)) {
506 *ImageFmpInfo = (VOID *)((UINT8 *)*ImageFmpInfo + FileHeaderSize);
507 *ImageFmpInfoSize = *ImageFmpInfoSize - FileHeaderSize;
509 Result =
GetSectionByType (*ImageFmpInfo, (UINT32)*ImageFmpInfoSize, EFI_SECTION_RAW, 0, (VOID **)ImageFmpInfo, ImageFmpInfoSize);
514 if (IS_SECTION2 (*ImageFmpInfo)) {
520 *ImageFmpInfo = (VOID *)((UINT8 *)*ImageFmpInfo + SectionHeaderSize);
521 *ImageFmpInfoSize = *ImageFmpInfoSize - SectionHeaderSize;
540 IN VOID *AuthenticatedImage,
541 IN UINTN AuthenticatedImageSize,
542 OUT VOID **SystemFirmwareImage,
547 UINT32 FileHeaderSize;
549 *SystemFirmwareImage =
NULL;
550 *SystemFirmwareImageSize = 0;
552 Result =
GetFfsByName (AuthenticatedImage, AuthenticatedImageSize, &mEdkiiSystemFirmwareFileGuid, EFI_FV_FILETYPE_RAW, SystemFirmwareImage, SystemFirmwareImageSize);
555 *SystemFirmwareImage = AuthenticatedImage;
556 *SystemFirmwareImageSize = AuthenticatedImageSize;
561 if (IS_FFS_FILE2 (*SystemFirmwareImage)) {
567 *SystemFirmwareImage = (UINT8 *)*SystemFirmwareImage + FileHeaderSize;
568 *SystemFirmwareImageSize = *SystemFirmwareImageSize - FileHeaderSize;
596 IN BOOLEAN ForceVersionMatch,
597 OUT UINT32 *LastAttemptVersion,
598 OUT UINT32 *LastAttemptStatus,
599 OUT VOID **AuthenticatedImage,
605 UINTN ImageFmpInfoSize;
607 UINTN CurrentImageFmpInfoSize;
608 VOID *SystemFirmwareImage;
609 UINTN SystemFirmwareImageSize;
611 *LastAttemptVersion = 0;
617 if (mImageFmpInfo ==
NULL) {
618 DEBUG ((DEBUG_INFO,
"ImageFmpInfo is not set\n"));
619 return EFI_SECURITY_VIOLATION;
622 Result =
ExtractAuthenticatedImage ((VOID *)Image, ImageSize, LastAttemptStatus, AuthenticatedImage, AuthenticatedImageSize);
624 DEBUG ((DEBUG_INFO,
"ExtractAuthenticatedImage - fail\n"));
625 return EFI_SECURITY_VIOLATION;
628 DEBUG ((DEBUG_INFO,
"AuthenticatedImage - 0x%x - 0x%x\n", *AuthenticatedImage, *AuthenticatedImageSize));
630 Result =
ExtractSystemFirmwareImage (*AuthenticatedImage, *AuthenticatedImageSize, &SystemFirmwareImage, &SystemFirmwareImageSize);
632 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
633 DEBUG ((DEBUG_INFO,
"ExtractSystemFirmwareImage - fail\n"));
634 return EFI_SECURITY_VIOLATION;
637 DEBUG ((DEBUG_INFO,
"SystemFirmwareImage - 0x%x - 0x%x\n", SystemFirmwareImage, SystemFirmwareImageSize));
641 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
642 DEBUG ((DEBUG_INFO,
"ExtractSystemFirmwareImageFmpInfo - fail\n"));
643 return EFI_SECURITY_VIOLATION;
646 *LastAttemptVersion = ImageFmpInfo->Version;
647 DEBUG ((DEBUG_INFO,
"ImageFmpInfo - 0x%x - 0x%x\n", ImageFmpInfo, ImageFmpInfoSize));
648 DEBUG ((DEBUG_INFO,
"NewImage Version - 0x%x\n", ImageFmpInfo->Version));
649 DEBUG ((DEBUG_INFO,
"NewImage LowestSupportedImageVersion - 0x%x\n", ImageFmpInfo->LowestSupportedImageVersion));
651 CurrentImageFmpInfo = mImageFmpInfo;
652 CurrentImageFmpInfoSize = mImageFmpInfoSize;
654 DEBUG ((DEBUG_INFO,
"ImageFmpInfo - 0x%x - 0x%x\n", CurrentImageFmpInfo, CurrentImageFmpInfoSize));
655 DEBUG ((DEBUG_INFO,
"Current Version - 0x%x\n", CurrentImageFmpInfo->Version));
656 DEBUG ((DEBUG_INFO,
"Current LowestSupportedImageVersion - 0x%x\n", CurrentImageFmpInfo->LowestSupportedImageVersion));
658 if (ForceVersionMatch) {
659 if (CurrentImageFmpInfo->Version != ImageFmpInfo->Version) {
660 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;
661 DEBUG ((DEBUG_INFO,
"ForceVersionMatch check - fail\n"));
662 return EFI_SECURITY_VIOLATION;
665 if (ImageFmpInfo->Version < CurrentImageFmpInfo->LowestSupportedImageVersion) {
666 *LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION;
667 DEBUG ((DEBUG_INFO,
"LowestSupportedImageVersion check - fail\n"));
668 return EFI_SECURITY_VIOLATION;
690 IN OUT VOID *TokenData,
694 if (
CompareGuid (CallBackGuid, &gEfiSignedCapsulePkgTokenSpaceGuid) &&
695 (CallBackToken ==
PcdToken (PcdEdkiiSystemFirmwareImageDescriptor)))
697 mImageFmpInfoSize = TokenDataSize;
699 ASSERT (mImageFmpInfo !=
NULL);
704 &gEfiSignedCapsulePkgTokenSpaceGuid,
705 PcdToken (PcdEdkiiSystemFirmwareImageDescriptor),
710 if (
CompareGuid (CallBackGuid, &gEfiSignedCapsulePkgTokenSpaceGuid) &&
711 (CallBackToken ==
PcdToken (PcdEdkiiSystemFirmwareFileGuid)))
713 CopyGuid (&mEdkiiSystemFirmwareFileGuid, TokenData);
718 &gEfiSignedCapsulePkgTokenSpaceGuid,
719 PcdToken (PcdEdkiiSystemFirmwareFileGuid),
736 mImageFmpInfoSize =
PcdGetSize (PcdEdkiiSystemFirmwareImageDescriptor);
737 mImageFmpInfo =
PcdGetPtr (PcdEdkiiSystemFirmwareImageDescriptor);
748 mImageFmpInfo =
NULL;
749 mImageFmpInfoSize = 0;
751 &gEfiSignedCapsulePkgTokenSpaceGuid,
752 PcdToken (PcdEdkiiSystemFirmwareImageDescriptor),
757 ASSERT (mImageFmpInfo !=
NULL);
760 CopyGuid (&mEdkiiSystemFirmwareFileGuid,
PcdGetPtr (PcdEdkiiSystemFirmwareFileGuid));
764 if (
CompareGuid (&mEdkiiSystemFirmwareFileGuid, &gZeroGuid)) {
766 &gEfiSignedCapsulePkgTokenSpaceGuid,
767 PcdToken (PcdEdkiiSystemFirmwareFileGuid),
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
BOOLEAN EFIAPI ExtractDriverFvImage(IN VOID *AuthenticatedImage, IN UINTN AuthenticatedImageSize, OUT VOID **DriverFvImage, OUT UINTN *DriverFvImageSize)
BOOLEAN EFIAPI ExtractSystemFirmwareImage(IN VOID *AuthenticatedImage, IN UINTN AuthenticatedImageSize, OUT VOID **SystemFirmwareImage, OUT UINTN *SystemFirmwareImageSize)
EFI_STATUS EFIAPI EdkiiSystemCapsuleLibConstructor(VOID)
VOID EFIAPI EdkiiSystemCapsuleLibPcdCallBack(IN CONST GUID *CallBackGuid OPTIONAL, IN UINTN CallBackToken, IN OUT VOID *TokenData, IN UINTN TokenDataSize)
BOOLEAN EFIAPI ExtractSystemFirmwareImageFmpInfo(IN VOID *SystemFirmwareImage, IN UINTN SystemFirmwareImageSize, OUT EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR **ImageFmpInfo, OUT UINTN *ImageFmpInfoSize)
BOOLEAN EFIAPI ExtractAuthenticatedImage(IN VOID *Image, IN UINTN ImageSize, OUT UINT32 *LastAttemptStatus, OUT VOID **AuthenticatedImage, OUT UINTN *AuthenticatedImageSize)
EFI_STATUS EFIAPI CapsuleAuthenticateSystemFirmware(IN VOID *Image, IN UINTN ImageSize, IN BOOLEAN ForceVersionMatch, OUT UINT32 *LastAttemptVersion, OUT UINT32 *LastAttemptStatus, OUT VOID **AuthenticatedImage, OUT UINTN *AuthenticatedImageSize)
BOOLEAN IsBufferErased(IN UINT8 ErasePolarity, IN VOID *InBuffer, IN UINTN BufferSize)
BOOLEAN EFIAPI ExtractConfigImage(IN VOID *AuthenticatedImage, IN UINTN AuthenticatedImageSize, OUT VOID **ConfigImage, OUT UINTN *ConfigImageSize)
BOOLEAN GetFfsByName(IN VOID *FdStart, IN UINTN FdSize, IN EFI_GUID *FileName, IN EFI_FV_FILETYPE Type, OUT VOID **OutFfsBuffer, OUT UINTN *OutFfsBufferSize)
BOOLEAN GetSectionByType(IN VOID *SectionBuffer, IN UINT32 SectionBufferSize, IN EFI_SECTION_TYPE SectionType, IN UINTN SectionInstance, OUT VOID **OutSectionBuffer, OUT UINTN *OutSectionSize)
#define EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR_SIGNATURE
EFI_GUID gEdkiiSystemFirmwareImageDescriptorFileGuid
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
RETURN_STATUS EFIAPI AuthenticateFmpImage(IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image, IN UINTN ImageSize, IN CONST UINT8 *PublicKeyData, IN UINTN PublicKeyDataLength)
#define RETURN_UNSUPPORTED
#define RETURN_OUT_OF_RESOURCES
#define RETURN_SECURITY_VIOLATION
#define ALIGN_VALUE(Value, Alignment)
#define OFFSET_OF(TYPE, Field)
#define RETURN_INVALID_PARAMETER
#define DEBUG(Expression)
VOID EFIAPI LibPcdCallbackOnSet(IN CONST GUID *Guid OPTIONAL, IN UINTN TokenNumber, IN PCD_CALLBACK NotificationFunction)
#define PcdGetSize(TokenName)
#define PcdToken(TokenName)
VOID EFIAPI LibPcdCancelCallback(IN CONST GUID *Guid OPTIONAL, IN UINTN TokenNumber, IN PCD_CALLBACK NotificationFunction)
#define PcdGetPtr(TokenName)
#define EFI_FV_FILETYPE_ALL
EFI_COMMON_SECTION_HEADER EFI_RAW_SECTION
#define FFS_FILE_SIZE(FfsFileHeaderPtr)
#define SECTION_SIZE(SectionHeaderPtr)
#define LAST_ATTEMPT_STATUS_SUCCESS
WIN_CERTIFICATE_UEFI_GUID AuthInfo