From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6848C1A1E43 for ; Wed, 26 Oct 2016 18:34:12 -0700 (PDT) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP; 26 Oct 2016 18:34:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,404,1473145200"; d="scan'208,217";a="184318537" Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by fmsmga004.fm.intel.com with ESMTP; 26 Oct 2016 18:34:11 -0700 Received: from fmsmsx157.amr.corp.intel.com (10.18.116.73) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.248.2; Wed, 26 Oct 2016 18:34:10 -0700 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by FMSMSX157.amr.corp.intel.com (10.18.116.73) with Microsoft SMTP Server (TLS) id 14.3.248.2; Wed, 26 Oct 2016 18:34:08 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.206]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.96]) with mapi id 14.03.0248.002; Thu, 27 Oct 2016 09:33:43 +0800 From: "Yao, Jiewen" To: "Kinney, Michael D" , "edk2-devel@lists.01.org" CC: "Tian, Feng" , "Zeng, Star" , "Gao, Liming" , "Zhang, Chao B" , "Sean Brogan (sean.brogan@microsoft.com)" Thread-Topic: [PATCH V4 06/15] MdeModulePkg/DxeCapsuleLibFmp: Add DxeCapsuleLibFmp instance. Thread-Index: AQHSL+ZtsLvLFXmxx0SHfHoDZp+dLaC7gNmQ Date: Thu, 27 Oct 2016 01:33:42 +0000 Message-ID: <74D8A39837DF1E4DA445A8C0B3885C50386B827B@shsmsx102.ccr.corp.intel.com> References: <1477189240-11336-1-git-send-email-jiewen.yao@intel.com> <1477189240-11336-7-git-send-email-jiewen.yao@intel.com> In-Reply-To: Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 Subject: Re: [PATCH V4 06/15] MdeModulePkg/DxeCapsuleLibFmp: Add DxeCapsuleLibFmp instance. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Oct 2016 01:34:12 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Mike I think that is misunderstanding. Chao did test before and the UX capsule from Windows does contain a BMP fil= e. He told me that I should use BMP file. I will let Chao to clarify more. I also include Sean Brogan to double check the Windows side. Thank you Yao Jiewen From: Kinney, Michael D Sent: Thursday, October 27, 2016 8:10 AM To: Yao, Jiewen ; edk2-devel@lists.01.org; Kinney, Mi= chael D Cc: Tian, Feng ; Zeng, Star ; Gao= , Liming ; Zhang, Chao B Subject: RE: [PATCH V4 06/15] MdeModulePkg/DxeCapsuleLibFmp: Add DxeCapsule= LibFmp instance. Jiewen, I have looked Microsoft UX capsule in the Microsoft Windows UEFI Firmware Update Platform Specification, and that specification only define support for an ImageType value of 0, which is bitmap format based on ACPI 5.0 BGRT. There are no defined ImageType values for BMP. We need to remove BMP graphics file format decoding from all capsule processing. If a Microsoft UX capsule is present, it is already a raw bitmap that can be passed to GOP. Best regards, Mike > -----Original Message----- > From: Yao, Jiewen > Sent: Saturday, October 22, 2016 7:21 PM > To: edk2-devel@lists.01.org > Cc: Tian, Feng >; Zeng, S= tar >; Kinney, Michael > D >; Gao, L= iming >; Zhang, Chao B > > > Subject: [PATCH V4 06/15] MdeModulePkg/DxeCapsuleLibFmp: Add DxeCapsuleLi= bFmp instance. > > This instance handles Microsoft UX capsule, UEFI defined FMP capsule. > This instance should not assume any capsule image format. > > Cc: Feng Tian > > Cc: Star Zeng > > Cc: Michael D Kinney > > Cc: Liming Gao > > Cc: Chao Zhang > > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Jiewen Yao > > Reviewed-by: Liming Gao > > --- > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 1363 > ++++++++++++++++++++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.inf | 80 ++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.uni | 22 + > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c | 486 ++= +++++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c | 489 ++= +++++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleRuntime.c | 112 ++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.inf | 83 ++ > MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.uni | 22 + > 8 files changed, 2657 insertions(+) > > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c > new file mode 100644 > index 0000000..20ef762 > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c > @@ -0,0 +1,1363 @@ > +/** @file > + DXE capsule library. > + > + Caution: This module requires additional review when modified. > + This module will have external input - capsule image. > + This external input must be validated carefully to avoid security issu= e like > + buffer overflow, integer overflow. > + > + SupportCapsuleImage(), ProcessCapsuleImage(), IsValidCapsuleHeader(), > + ValidateFmpCapsule(), DisplayCapsuleImage(), ConvertBmpToGopBlt() will > + receive untrusted input and do basic validation. > + > + Copyright (c) 2016, Intel Corporation. All rights reserved.
> + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the = BSD License > + which accompanies this distribution. The full text of the license may= be found at > + http://opensource.org/licenses/bsd-license.php > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR I= MPLIED. > + > +**/ > + > +#include > + > +#include > +#include > + > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > + > +extern BOOLEAN mAreAllImagesProcessed; > + > +EFI_SYSTEM_RESOURCE_TABLE *mEsrtTable =3D NULL; > +BOOLEAN mIsVirtualAddrConverted =3D FALSE; > +BOOLEAN mDxeCapsuleLibEndOfDxe =3D FALSE; > + > +/** > + Initialize capsule related variables. > +**/ > +VOID > +InitCapsuleVariable( > + VOID > + ); > + > +/** > + Check if this FMP capsule is processed. > + > + @param CapsuleHeader The capsule image header > + @param PayloadIndex FMP payload index > + @param ImageHeader FMP image header > + > + @retval TRUE This FMP capsule is processed. > + @retval FALSE This FMP capsule is not processed. > +**/ > +BOOLEAN > +IsFmpCapsuleProcessed( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN UINTN PayloadIndex, > + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader > + ); > + > +/** > + Record capsule status variable. > + > + @param CapsuleHeader The capsule image header > + @param CapsuleStatus The capsule process stauts > + > + @retval EFI_SUCCESS The capsule status variable is recorded. > + @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status = variable. > +**/ > +EFI_STATUS > +RecordCapsuleStatusVariable( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN EFI_STATUS CapsuleStatus > + ); > + > +/** > + Record FMP capsule status variable. > + > + @param CapsuleHeader The capsule image header > + @param CapsuleStatus The capsule process stauts > + @param PayloadIndex FMP payload index > + @param ImageHeader FMP image header > + > + @retval EFI_SUCCESS The capsule status variable is recorded. > + @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status = variable. > +**/ > +EFI_STATUS > +RecordFmpCapsuleStatusVariable( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN EFI_STATUS CapsuleStatus, > + IN UINTN PayloadIndex, > + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader > + ); > + > +/** > + Function indicate the current completion progress of the firmware > + update. Platform may override with own specific progress function. > + > + @param Completion A value between 1 and 100 indicating the current= completion > progress of the firmware update > + > + @retval EFI_SUCESS Input capsule is a correct FMP capsule. > +**/ > +EFI_STATUS > +EFIAPI > +Update_Image_Progress ( > + IN UINTN Completion > + ) > +{ > + return EFI_SUCCESS; > +} > + > +/** > + Return if this CapsuleGuid is a FMP capsule GUID or not. > + > + @param[in] CapsuleGuid A pointer to EFI_GUID > + > + @retval TRUE It is a FMP capsule GUID. > + @retval FALSE It is not a FMP capsule GUID. > +**/ > +BOOLEAN > +IsFmpCapsuleGuid( > + IN EFI_GUID *CapsuleGuid > + ) > +{ > + if (CompareGuid(&gEfiFmpCapsuleGuid, CapsuleGuid)) { > + return TRUE; > + } > + > + return FALSE; > +} > + > +/** > + Validate if it is valid capsule header > + > + Caution: This function may receive untrusted input. > + > + This function assumes the caller provided correct CapsuleHeader pointe= r > + and CapsuleSize. > + > + This function validates the fields in EFI_CAPSULE_HEADER. > + > + @param CapsuleHeader Points to a capsule header. > + @param CapsuleSize Size of the whole capsule image. > + > +**/ > +BOOLEAN > +IsValidCapsuleHeader( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN UINT64 CapsuleSize > + ) > +{ > + if (CapsuleHeader->CapsuleImageSize !=3D CapsuleSize) { > + return FALSE; > + } > + if (CapsuleHeader->HeaderSize >=3D CapsuleHeader->CapsuleImageSize) { > + return FALSE; > + } > + return TRUE; > +} > + > +/** > + Validate Fmp capsules layout. > + > + Caution: This function may receive untrusted input. > + > + This function assumes the caller validated the capsule by using > + IsValidCapsuleHeader(), so that all fields in EFI_CAPSULE_HEADER are c= orrect. > + The capsule buffer size is CapsuleHeader->CapsuleImageSize. > + > + This function validates the fields in EFI_FIRMWARE_MANAGEMENT_CAPSULE_= HEADER > + and EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER. > + > + This function need support nested FMP capsule. > + > + @param[in] CapsuleHeader Points to a capsule header. > + @param[out] EmbeddedDriverCount The EmbeddedDriverCount in the FMP c= apsule. > + > + @retval EFI_SUCESS Input capsule is a correct FMP capsule. > + @retval EFI_INVALID_PARAMETER Input capsule is not a correct FMP caps= ule. > +**/ > +EFI_STATUS > +ValidateFmpCapsule ( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + OUT UINT16 *EmbeddedDriverCount OPTIONAL > + ) > +{ > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *FmpCapsuleHeader; > + UINT8 *EndOfCapsule; > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader; > + UINT8 *EndOfPayload; > + UINT64 *ItemOffsetList; > + UINT32 ItemNum; > + UINTN Index; > + UINTN FmpCapsuleSize; > + UINTN FmpCapsuleHeaderSize; > + UINT64 FmpImageSize; > + UINTN FmpImageHeaderSize; > + > + if (!IsFmpCapsuleGuid(&CapsuleHeader->CapsuleGuid)) { > + return ValidateFmpCapsule ((EFI_CAPSULE_HEADER *)((UINTN)CapsuleHead= er + > CapsuleHeader->HeaderSize), EmbeddedDriverCount); > + } > + > + if (CapsuleHeader->HeaderSize >=3D CapsuleHeader->CapsuleImageSize) { > + DEBUG((EFI_D_ERROR, "HeaderSize(0x%x) >=3D CapsuleImageSize(0x%x)\n"= , CapsuleHeader- > >HeaderSize, CapsuleHeader->CapsuleImageSize)); > + return EFI_INVALID_PARAMETER; > + } > + > + FmpCapsuleHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *) ((UINT= 8 *) > CapsuleHeader + CapsuleHeader->HeaderSize); > + EndOfCapsule =3D (UINT8 *) CapsuleHeader + CapsuleHeader->CapsuleI= mageSize; > + FmpCapsuleSize =3D (UINTN)EndOfCapsule - (UINTN)FmpCapsuleHeader; > + > + if (FmpCapsuleSize < sizeof(EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER)) { > + DEBUG((EFI_D_ERROR, "FmpCapsuleSize(0x%x) < > EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER\n", FmpCapsuleSize)); > + return EFI_INVALID_PARAMETER; > + } > + > + // Check EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER > + if (FmpCapsuleHeader->Version !=3D > EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER_INIT_VERSION) { > + DEBUG((EFI_D_ERROR, "FmpCapsuleHeader->Version(0x%x) !=3D > EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER_INIT_VERSION\n", FmpCapsuleHeader-= >Version)); > + return EFI_INVALID_PARAMETER; > + } > + ItemOffsetList =3D (UINT64 *)(FmpCapsuleHeader + 1); > + > + // No overflow > + ItemNum =3D FmpCapsuleHeader->EmbeddedDriverCount + FmpCapsuleHeader- > >PayloadItemCount; > + > + if ((FmpCapsuleSize - sizeof(EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER))/= sizeof(UINT64) > < ItemNum) { > + DEBUG((EFI_D_ERROR, "ItemNum(0x%x) too big\n", ItemNum)); > + return EFI_INVALID_PARAMETER; > + } > + FmpCapsuleHeaderSize =3D sizeof(EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER= ) + > sizeof(UINT64)*ItemNum; > + > + // Check ItemOffsetList > + for (Index =3D 0; Index < ItemNum; Index++) { > + if (ItemOffsetList[Index] >=3D FmpCapsuleSize) { > + DEBUG((EFI_D_ERROR, "ItemOffsetList[%d](0x%lx) >=3D FmpCapsuleSize= (0x%x)\n", > Index, ItemOffsetList[Index], FmpCapsuleSize)); > + return EFI_INVALID_PARAMETER; > + } > + if (ItemOffsetList[Index] < FmpCapsuleHeaderSize) { > + DEBUG((EFI_D_ERROR, "ItemOffsetList[%d](0x%lx) < FmpCapsuleHeaderS= ize(0x%x)\n", > Index, ItemOffsetList[Index], FmpCapsuleHeaderSize)); > + return EFI_INVALID_PARAMETER; > + } > + // > + // All the address in ItemOffsetList must be stored in ascending ord= er > + // > + if (Index > 0) { > + if (ItemOffsetList[Index] <=3D ItemOffsetList[Index - 1]) { > + DEBUG((EFI_D_ERROR, "ItemOffsetList[%d](0x%lx) < ItemOffsetList[= %d](0x%x)\n", > Index, ItemOffsetList[Index], Index, ItemOffsetList[Index - 1])); > + return EFI_INVALID_PARAMETER; > + } > + } > + } > + > + // Check EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER > + for (Index =3D FmpCapsuleHeader->EmbeddedDriverCount; Index < ItemNum;= Index++) { > + ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *)((U= INT8 > *)FmpCapsuleHeader + ItemOffsetList[Index]); > + if (Index =3D=3D ItemNum - 1) { > + EndOfPayload =3D (UINT8 *)((UINTN)EndOfCapsule - (UINTN)FmpCapsule= Header); > + } else { > + EndOfPayload =3D (UINT8 *)(UINTN)ItemOffsetList[Index+1]; > + } > + FmpImageSize =3D (UINTN)EndOfPayload - ItemOffsetList[Index]; > + > + if (FmpImageSize < OFFSET_OF(EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_H= EADER, > UpdateHardwareInstance)) { > + DEBUG((EFI_D_ERROR, "FmpImageSize(0x%lx) < > EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER\n", FmpImageSize)); > + return EFI_INVALID_PARAMETER; > + } > + FmpImageHeaderSize =3D sizeof(EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_= HEADER); > + if ((ImageHeader->Version > > EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER_INIT_VERSION) || > + (ImageHeader->Version < 1)) { > + DEBUG((EFI_D_ERROR, "ImageHeader->Version(0x%x) Unknown\n", ImageH= eader- > >Version)); > + return EFI_INVALID_PARAMETER; > + } > + if (ImageHeader->Version < > EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER_INIT_VERSION) { > + FmpImageHeaderSize =3D OFFSET_OF(EFI_FIRMWARE_MANAGEMENT_CAPSULE_I= MAGE_HEADER, > UpdateHardwareInstance); > + } > + > + // No overflow > + if (FmpImageSize !=3D (UINT64)FmpImageHeaderSize + (UINT64)ImageHead= er- > >UpdateImageSize + (UINT64)ImageHeader->UpdateVendorCodeSize) { > + DEBUG((EFI_D_ERROR, "FmpImageSize(0x%lx) mismatch, UpdateImageSize= (0x%x) > UpdateVendorCodeSize(0x%x)\n", FmpImageSize, ImageHeader->UpdateImageSize= , ImageHeader- > >UpdateVendorCodeSize)); > + return EFI_INVALID_PARAMETER; > + } > + } > + > + if (ItemNum =3D=3D 0) { > + // > + // No driver & payload element in FMP > + // > + EndOfPayload =3D (UINT8 *)(FmpCapsuleHeader + 1); > + if (EndOfPayload !=3D EndOfCapsule) { > + DEBUG((EFI_D_ERROR, "EndOfPayload(0x%x) mismatch, EndOfCapsule(0x%= x)\n", > EndOfPayload, EndOfCapsule)); > + return EFI_INVALID_PARAMETER; > + } > + return EFI_UNSUPPORTED; > + } > + > + if (EmbeddedDriverCount !=3D NULL) { > + *EmbeddedDriverCount =3D FmpCapsuleHeader->EmbeddedDriverCount; > + } > + > + return EFI_SUCCESS; > +} > + > +/** > + Convert a *.BMP graphics image to a GOP blt buffer. If a NULL Blt buff= er > + is passed in a GopBlt buffer will be allocated by this routine. If a G= opBlt > + buffer is passed in it will be used if it is big enough. > + > + Caution: This function may receive untrusted input. > + > + @param BmpImage Pointer to BMP file > + @param BmpImageSize Number of bytes in BmpImage > + @param GopBlt Buffer containing GOP version of BmpImage. > + @param GopBltSize Size of GopBlt in bytes. > + @param PixelHeight Height of GopBlt/BmpImage in pixels > + @param PixelWidth Width of GopBlt/BmpImage in pixels > + > + @retval EFI_SUCCESS GopBlt and GopBltSize are returned. > + @retval EFI_UNSUPPORTED BmpImage is not a valid *.BMP image > + @retval EFI_BUFFER_TOO_SMALL The passed in GopBlt buffer is not big e= nough. > + GopBltSize will contain the required siz= e. > + @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate. > + > +**/ > +STATIC > +EFI_STATUS > +ConvertBmpToGopBlt ( > + IN VOID *BmpImage, > + IN UINTN BmpImageSize, > + IN OUT VOID **GopBlt, > + IN OUT UINTN *GopBltSize, > + OUT UINTN *PixelHeight, > + OUT UINTN *PixelWidth > + ) > +{ > + UINT8 *Image; > + UINT8 *ImageHeader; > + BMP_IMAGE_HEADER *BmpHeader; > + BMP_COLOR_MAP *BmpColorMap; > + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer; > + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt; > + UINT64 BltBufferSize; > + UINTN Index; > + UINTN Height; > + UINTN Width; > + UINTN ImageIndex; > + UINT32 DataSizePerLine; > + BOOLEAN IsAllocated; > + UINT32 ColorMapNum; > + > + if (sizeof (BMP_IMAGE_HEADER) > BmpImageSize) { > + return EFI_INVALID_PARAMETER; > + } > + > + BmpHeader =3D (BMP_IMAGE_HEADER *) BmpImage; > + > + if (BmpHeader->CharB !=3D 'B' || BmpHeader->CharM !=3D 'M') { > + return EFI_UNSUPPORTED; > + } > + > + // > + // Doesn't support compress. > + // > + if (BmpHeader->CompressionType !=3D 0) { > + return EFI_UNSUPPORTED; > + } > + > + // > + // Only support BITMAPINFOHEADER format. > + // BITMAPFILEHEADER + BITMAPINFOHEADER =3D BMP_IMAGE_HEADER > + // > + if (BmpHeader->HeaderSize !=3D sizeof (BMP_IMAGE_HEADER) - OFFSET_OF(B= MP_IMAGE_HEADER, > HeaderSize)) { > + return EFI_UNSUPPORTED; > + } > + > + // > + // The data size in each line must be 4 byte alignment. > + // > + DataSizePerLine =3D ((BmpHeader->PixelWidth * BmpHeader->BitPerPixel += 31) >> 3) & > (~0x3); > + BltBufferSize =3D MultU64x32 (DataSizePerLine, BmpHeader->PixelHeight)= ; > + if (BltBufferSize > (UINT32) ~0) { > + return EFI_INVALID_PARAMETER; > + } > + > + if ((BmpHeader->Size !=3D BmpImageSize) || > + (BmpHeader->Size < BmpHeader->ImageOffset) || > + (BmpHeader->Size - BmpHeader->ImageOffset !=3D BmpHeader->PixelHe= ight * > DataSizePerLine)) { > + return EFI_INVALID_PARAMETER; > + } > + > + // > + // Calculate Color Map offset in the image. > + // > + Image =3D BmpImage; > + BmpColorMap =3D (BMP_COLOR_MAP *) (Image + sizeof (BMP_IMAGE_HEADER)); > + if (BmpHeader->ImageOffset < sizeof (BMP_IMAGE_HEADER)) { > + return EFI_INVALID_PARAMETER; > + } > + > + if (BmpHeader->ImageOffset > sizeof (BMP_IMAGE_HEADER)) { > + switch (BmpHeader->BitPerPixel) { > + case 1: > + ColorMapNum =3D 2; > + break; > + case 4: > + ColorMapNum =3D 16; > + break; > + case 8: > + ColorMapNum =3D 256; > + break; > + default: > + ColorMapNum =3D 0; > + break; > + } > + // > + // BMP file may has padding data between the bmp header section and = the bmp data > section. > + // > + if (BmpHeader->ImageOffset - sizeof (BMP_IMAGE_HEADER) < sizeof (BMP= _COLOR_MAP) * > ColorMapNum) { > + return EFI_INVALID_PARAMETER; > + } > + } > + > + // > + // Calculate graphics image data address in the image > + // > + Image =3D ((UINT8 *) BmpImage) + BmpHeader->ImageOffset; > + ImageHeader =3D Image; > + > + // > + // Calculate the BltBuffer needed size. > + // > + BltBufferSize =3D MultU64x32 ((UINT64) BmpHeader->PixelWidth, BmpHeade= r->PixelHeight); > + // > + // Ensure the BltBufferSize * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) d= oesn't > overflow > + // > + if (BltBufferSize > DivU64x32 ((UINTN) ~0, sizeof (EFI_GRAPHICS_OUTPUT= _BLT_PIXEL))) > { > + return EFI_UNSUPPORTED; > + } > + BltBufferSize =3D MultU64x32 (BltBufferSize, sizeof (EFI_GRAPHICS_OUTP= UT_BLT_PIXEL)); > + > + IsAllocated =3D FALSE; > + if (*GopBlt =3D=3D NULL) { > + // > + // GopBlt is not allocated by caller. > + // > + *GopBltSize =3D (UINTN) BltBufferSize; > + *GopBlt =3D AllocatePool (*GopBltSize); > + IsAllocated =3D TRUE; > + if (*GopBlt =3D=3D NULL) { > + return EFI_OUT_OF_RESOURCES; > + } > + } else { > + // > + // GopBlt has been allocated by caller. > + // > + if (*GopBltSize < (UINTN) BltBufferSize) { > + *GopBltSize =3D (UINTN) BltBufferSize; > + return EFI_BUFFER_TOO_SMALL; > + } > + } > + > + *PixelWidth =3D BmpHeader->PixelWidth; > + *PixelHeight =3D BmpHeader->PixelHeight; > + > + // > + // Convert image from BMP to Blt buffer format > + // > + BltBuffer =3D *GopBlt; > + for (Height =3D 0; Height < BmpHeader->PixelHeight; Height++) { > + Blt =3D &BltBuffer[(BmpHeader->PixelHeight - Height - 1) * BmpHeader= ->PixelWidth]; > + for (Width =3D 0; Width < BmpHeader->PixelWidth; Width++, Image++, B= lt++) { > + switch (BmpHeader->BitPerPixel) { > + case 1: > + // > + // Convert 1-bit (2 colors) BMP to 24-bit color > + // > + for (Index =3D 0; Index < 8 && Width < BmpHeader->PixelWidth; In= dex++) { > + Blt->Red =3D BmpColorMap[((*Image) >> (7 - Index)) & 0x1].R= ed; > + Blt->Green =3D BmpColorMap[((*Image) >> (7 - Index)) & 0x1].G= reen; > + Blt->Blue =3D BmpColorMap[((*Image) >> (7 - Index)) & 0x1].B= lue; > + Blt++; > + Width++; > + } > + > + Blt--; > + Width--; > + break; > + > + case 4: > + // > + // Convert 4-bit (16 colors) BMP Palette to 24-bit color > + // > + Index =3D (*Image) >> 4; > + Blt->Red =3D BmpColorMap[Index].Red; > + Blt->Green =3D BmpColorMap[Index].Green; > + Blt->Blue =3D BmpColorMap[Index].Blue; > + if (Width < (BmpHeader->PixelWidth - 1)) { > + Blt++; > + Width++; > + Index =3D (*Image) & 0x0f; > + Blt->Red =3D BmpColorMap[Index].Red; > + Blt->Green =3D BmpColorMap[Index].Green; > + Blt->Blue =3D BmpColorMap[Index].Blue; > + } > + break; > + > + case 8: > + // > + // Convert 8-bit (256 colors) BMP Palette to 24-bit color > + // > + Blt->Red =3D BmpColorMap[*Image].Red; > + Blt->Green =3D BmpColorMap[*Image].Green; > + Blt->Blue =3D BmpColorMap[*Image].Blue; > + break; > + > + case 24: > + // > + // It is 24-bit BMP. > + // > + Blt->Blue =3D *Image++; > + Blt->Green =3D *Image++; > + Blt->Red =3D *Image; > + break; > + > + case 32: > + // > + // it is 32-bit BMP. Skip pixel's highest byte > + // > + Blt->Blue =3D *Image++; > + Blt->Green =3D *Image++; > + Blt->Red =3D *Image++; > + break; > + > + default: > + // > + // Other bit format BMP is not supported. > + // > + if (IsAllocated) { > + FreePool (*GopBlt); > + *GopBlt =3D NULL; > + } > + return EFI_UNSUPPORTED; > + }; > + > + } > + > + ImageIndex =3D (UINTN) (Image - ImageHeader); > + if ((ImageIndex % 4) !=3D 0) { > + // > + // Bmp Image starts each row on a 32-bit boundary! > + // > + Image =3D Image + (4 - (ImageIndex % 4)); > + } > + } > + > + return EFI_SUCCESS; > +} > + > + > +/** > + Those capsules supported by the firmwares. > + > + Caution: This function may receive untrusted input. > + > + @param CapsuleHeader Points to a capsule header. > + > + @retval EFI_SUCESS Input capsule is supported by firmware. > + @retval EFI_UNSUPPORTED Input capsule is not supported by the firmwar= e. > +**/ > +EFI_STATUS > +DisplayCapsuleImage ( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + DISPLAY_DISPLAY_PAYLOAD *ImagePayload; > + UINTN PayloadSize; > + EFI_STATUS Status; > + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt; > + UINTN BltSize; > + UINTN Height; > + UINTN Width; > + EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput; > + > + ImagePayload =3D (DISPLAY_DISPLAY_PAYLOAD *)(CapsuleHeader + 1); > + PayloadSize =3D (UINTN)(CapsuleHeader->CapsuleImageSize - sizeof(EFI_C= APSULE_HEADER)); > + > + if (ImagePayload->Version !=3D 1) { > + return EFI_UNSUPPORTED; > + } > + if (CalculateCheckSum8((UINT8 *)CapsuleHeader, CapsuleHeader->CapsuleI= mageSize) !=3D > 0) { > + return EFI_UNSUPPORTED; > + } > + // > + // Only Support Bitmap by now > + // > + if (ImagePayload->ImageType !=3D 0) { > + return EFI_UNSUPPORTED; > + } > + > + // > + // Try to open GOP > + // > + Status =3D gBS->HandleProtocol (gST->ConsoleOutHandle, > &gEfiGraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput); > + if (EFI_ERROR (Status)) { > + Status =3D gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid, NULL= , (VOID > **)&GraphicsOutput); > + if (EFI_ERROR(Status)) { > + return EFI_UNSUPPORTED; > + } > + } > + > + if (GraphicsOutput->Mode->Mode !=3D ImagePayload->Mode) { > + return EFI_UNSUPPORTED; > + } > + > + Blt =3D NULL; > + Width =3D 0; > + Height =3D 0; > + Status =3D ConvertBmpToGopBlt ( > + ImagePayload + 1, > + PayloadSize - sizeof(DISPLAY_DISPLAY_PAYLOAD), > + (VOID **)&Blt, > + &BltSize, > + &Height, > + &Width > + ); > + > + if (EFI_ERROR (Status)) { > + return Status; > + } > + > + Status =3D GraphicsOutput->Blt ( > + GraphicsOutput, > + Blt, > + EfiBltBufferToVideo, > + 0, > + 0, > + (UINTN) ImagePayload->OffsetX, > + (UINTN) ImagePayload->OffsetY, > + Width, > + Height, > + Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIX= EL) > + ); > + > + FreePool(Blt); > + > + return Status; > +} > + > +/** > + Dump FMP information. > + > + @param ImageInfoSize The size of ImageInfo, in bytes. > + @param ImageInfo A pointer to EFI_FIRMWARE_IMAGE_DESCRIPTOR. > + @param DescriptorVersion The version of EFI_FIRMWARE_IMAGE_DESCRIPTO= R. > + @param DescriptorCount The count of EFI_FIRMWARE_IMAGE_DESCRIPTOR. > + @param DescriptorSize The size of an individual EFI_FIRMWARE_IMAG= E_DESCRIPTOR, > in bytes. > + @param PackageVersion The version of package. > + @param PackageVersionName The version name of package. > +**/ > +VOID > +DumpFmpImageInfo( > + IN UINTN ImageInfoSize, > + IN EFI_FIRMWARE_IMAGE_DESCRIPTOR *ImageInfo, > + IN UINT32 DescriptorVersion, > + IN UINT8 DescriptorCount, > + IN UINTN DescriptorSize, > + IN UINT32 PackageVersion, > + IN CHAR16 *PackageVersionName > + ) > +{ > + EFI_FIRMWARE_IMAGE_DESCRIPTOR *CurrentImageInfo; > + UINTN Index; > + > + DEBUG((DEBUG_VERBOSE, " DescriptorVersion - 0x%x\n", DescriptorVersi= on)); > + DEBUG((DEBUG_VERBOSE, " DescriptorCount - 0x%x\n", DescriptorCount= )); > + DEBUG((DEBUG_VERBOSE, " DescriptorSize - 0x%x\n", DescriptorSize)= ); > + DEBUG((DEBUG_VERBOSE, " PackageVersion - 0x%x\n", PackageVersion)= ); > + DEBUG((DEBUG_VERBOSE, " PackageVersionName - %s\n\n", PackageVersionN= ame)); > + CurrentImageInfo =3D ImageInfo; > + for (Index =3D 0; Index < DescriptorCount; Index++) { > + DEBUG((DEBUG_VERBOSE, " ImageDescriptor (%d)\n", Index)); > + DEBUG((DEBUG_VERBOSE, " ImageIndex - 0x%x\n", > CurrentImageInfo->ImageIndex)); > + DEBUG((DEBUG_VERBOSE, " ImageTypeId - %g\n", &Cur= rentImageInfo- > >ImageTypeId)); > + DEBUG((DEBUG_VERBOSE, " ImageId - 0x%lx\n", > CurrentImageInfo->ImageId)); > + DEBUG((DEBUG_VERBOSE, " ImageIdName - %s\n", Curr= entImageInfo- > >ImageIdName)); > + DEBUG((DEBUG_VERBOSE, " Version - 0x%x\n", > CurrentImageInfo->Version)); > + DEBUG((DEBUG_VERBOSE, " VersionName - %s\n", Curr= entImageInfo- > >VersionName)); > + DEBUG((DEBUG_VERBOSE, " Size - 0x%x\n", > CurrentImageInfo->Size)); > + DEBUG((DEBUG_VERBOSE, " AttributesSupported - 0x%lx\n", > CurrentImageInfo->AttributesSupported)); > + DEBUG((DEBUG_VERBOSE, " AttributesSetting - 0x%lx\n", > CurrentImageInfo->AttributesSetting)); > + DEBUG((DEBUG_VERBOSE, " Compatibilities - 0x%lx\n", > CurrentImageInfo->Compatibilities)); > + if (DescriptorVersion > 1) { > + DEBUG((DEBUG_VERBOSE, " LowestSupportedImageVersion - 0x%x\n", > CurrentImageInfo->LowestSupportedImageVersion)); > + if (DescriptorVersion > 2) { > + DEBUG((DEBUG_VERBOSE, " LastAttemptVersion - 0x%x\n"= , > CurrentImageInfo->LastAttemptVersion)); > + DEBUG((DEBUG_VERBOSE, " LastAttemptStatus - 0x%x\n"= , > CurrentImageInfo->LastAttemptStatus)); > + DEBUG((DEBUG_VERBOSE, " HardwareInstance - 0x%lx\n= ", > CurrentImageInfo->HardwareInstance)); > + } > + } > + // > + // Use DescriptorSize to move ImageInfo Pointer to stay compatible w= ith different > ImageInfo version > + // > + CurrentImageInfo =3D (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)((UINT8 *)Curr= entImageInfo + > DescriptorSize); > + } > +} > + > +/** > + Dump a non-nested FMP capsule. > + > + @param CapsuleHeader A pointer to CapsuleHeader > +**/ > +VOID > +DumpFmpCapsule( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *FmpCapsuleHeader; > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader; > + UINTN Index; > + UINT64 *ItemOffsetList; > + > + FmpCapsuleHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *)((UINT8= *)CapsuleHeader > + CapsuleHeader->HeaderSize); > + > + DEBUG((DEBUG_VERBOSE, "FmpCapsule:\n")); > + DEBUG((DEBUG_VERBOSE, " Version - 0x%x\n", FmpCapsuleH= eader- > >Version)); > + DEBUG((DEBUG_VERBOSE, " EmbeddedDriverCount - 0x%x\n", FmpCapsuleH= eader- > >EmbeddedDriverCount)); > + DEBUG((DEBUG_VERBOSE, " PayloadItemCount - 0x%x\n", FmpCapsuleH= eader- > >PayloadItemCount)); > + > + ItemOffsetList =3D (UINT64 *)(FmpCapsuleHeader + 1); > + for (Index =3D 0; Index < FmpCapsuleHeader->EmbeddedDriverCount; Index= ++) { > + DEBUG((DEBUG_VERBOSE, " ItemOffsetList[%d] - 0x%lx\n", Index, > ItemOffsetList[Index])); > + } > + for (; Index < (UINTN)(FmpCapsuleHeader->EmbeddedDriverCount + FmpCaps= uleHeader- > >PayloadItemCount); Index++) { > + DEBUG((DEBUG_VERBOSE, " ItemOffsetList[%d] - 0x%lx\n", Index, > ItemOffsetList[Index])); > + ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *)((UI= NT8 > *)FmpCapsuleHeader + ItemOffsetList[Index]); > + > + DEBUG((DEBUG_VERBOSE, " ImageHeader:\n")); > + DEBUG((DEBUG_VERBOSE, " Version - 0x%x\n", ImageHe= ader- > >Version)); > + DEBUG((DEBUG_VERBOSE, " UpdateImageTypeId - %g\n", &ImageHea= der- > >UpdateImageTypeId)); > + DEBUG((DEBUG_VERBOSE, " UpdateImageIndex - 0x%x\n", ImageHe= ader- > >UpdateImageIndex)); > + DEBUG((DEBUG_VERBOSE, " UpdateImageSize - 0x%x\n", ImageHe= ader- > >UpdateImageSize)); > + DEBUG((DEBUG_VERBOSE, " UpdateVendorCodeSize - 0x%x\n", ImageHe= ader- > >UpdateVendorCodeSize)); > + if (ImageHeader->Version >=3D > EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER_INIT_VERSION) { > + DEBUG((DEBUG_VERBOSE, " UpdateHardwareInstance - 0x%lx\n", Imag= eHeader- > >UpdateHardwareInstance)); > + } > + } > +} > + > +/** > + Process Firmware management protocol data capsule. > + > + This function assumes the caller validated the capsule by using > + ValidateFmpCapsule(), so that all fields in EFI_CAPSULE_HEADER, > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER and > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER are correct. > + > + This function need support nested FMP capsule. > + > + @param CapsuleHeader Points to a capsule header. > + @param AreAllImagesProcessed If all the FMP images in the capsule are= processed. > + > + @retval EFI_SUCESS Process Capsule Image successfully. > + @retval EFI_UNSUPPORTED Capsule image is not supported by the fi= rmware. > + @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted. > + @retval EFI_OUT_OF_RESOURCES Not enough memory. > +**/ > +EFI_STATUS > +ProcessFmpCapsuleImage ( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + OUT BOOLEAN *AreAllImagesProcessed > + ) > +{ > + EFI_STATUS Status; > + EFI_STATUS StatusEsrt; > + EFI_STATUS StatusRet; > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *FmpCapsuleHeader; > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader; > + UINT8 *Image; > + EFI_HANDLE ImageHandle; > + UINT64 *ItemOffsetList; > + UINT32 ItemNum; > + UINTN Index; > + UINTN ExitDataSize; > + EFI_HANDLE *HandleBuffer; > + EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; > + UINTN NumberOfHandles; > + UINTN DescriptorSize; > + UINT8 FmpImageInfoCount; > + UINT32 FmpImageInfoDescriptorVe= r; > + UINTN ImageInfoSize; > + UINT32 PackageVersion; > + CHAR16 *PackageVersionName; > + CHAR16 *AbortReason; > + EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; > + EFI_FIRMWARE_IMAGE_DESCRIPTOR *TempFmpImageInfo; > + UINTN DriverLen; > + UINTN Index1; > + UINTN Index2; > + MEMMAP_DEVICE_PATH MemMapNode; > + EFI_DEVICE_PATH_PROTOCOL *DriverDevicePath; > + ESRT_MANAGEMENT_PROTOCOL *EsrtProtocol; > + EFI_SYSTEM_RESOURCE_ENTRY EsrtEntry; > + VOID *VendorCode; > + > + if (!IsFmpCapsuleGuid(&CapsuleHeader->CapsuleGuid)) { > + return ProcessFmpCapsuleImage ((EFI_CAPSULE_HEADER *)((UINTN)Capsule= Header + > CapsuleHeader->HeaderSize), AreAllImagesProcessed); > + } > + > + ASSERT(AreAllImagesProcessed !=3D NULL); > + > + Status =3D EFI_SUCCESS; > + StatusRet =3D EFI_NOT_FOUND; > + HandleBuffer =3D NULL; > + ExitDataSize =3D 0; > + DriverDevicePath =3D NULL; > + EsrtProtocol =3D NULL; > + *AreAllImagesProcessed =3D FALSE; > + > + DumpFmpCapsule(CapsuleHeader); > + > + FmpCapsuleHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER *) ((UINT= 8 *) > CapsuleHeader + CapsuleHeader->HeaderSize); > + > + if (FmpCapsuleHeader->Version > EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER= _INIT_VERSION) > { > + return EFI_INVALID_PARAMETER; > + } > + ItemOffsetList =3D (UINT64 *)(FmpCapsuleHeader + 1); > + > + ItemNum =3D FmpCapsuleHeader->EmbeddedDriverCount + FmpCapsuleHeader- > >PayloadItemCount; > + > + // > + // capsule in which driver count and payload count are both zero is no= t processed. > + // > + if (ItemNum =3D=3D 0) { > + *AreAllImagesProcessed =3D TRUE; > + return EFI_SUCCESS; > + } > + > + // > + // Update corresponding ESRT entry LastAttemp Status > + // > + Status =3D gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VO= ID > **)&EsrtProtocol); > + if (EFI_ERROR (Status)) { > + EsrtProtocol =3D NULL; > + } > + > + // > + // 1. Try to load & start all the drivers within capsule > + // > + SetDevicePathNodeLength (&MemMapNode.Header, sizeof (MemMapNode)); > + MemMapNode.Header.Type =3D HARDWARE_DEVICE_PATH; > + MemMapNode.Header.SubType =3D HW_MEMMAP_DP; > + MemMapNode.MemoryType =3D EfiBootServicesCode; > + MemMapNode.StartingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)CapsuleHea= der; > + MemMapNode.EndingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)((UINT8 *)= CapsuleHeader + > CapsuleHeader->CapsuleImageSize - 1); > + > + DriverDevicePath =3D AppendDevicePathNode (NULL, &MemMapNode.Header); > + if (DriverDevicePath =3D=3D NULL) { > + return EFI_OUT_OF_RESOURCES; > + } > + > + for (Index =3D 0; Index < FmpCapsuleHeader->EmbeddedDriverCount; Index= ++) { > + if (FmpCapsuleHeader->PayloadItemCount =3D=3D 0 && Index =3D=3D (UIN= TN)FmpCapsuleHeader- > >EmbeddedDriverCount - 1) { > + // > + // When driver is last element in the ItemOffsetList array, the dr= iver size is > calculated by reference CapsuleImageSize in EFI_CAPSULE_HEADER > + // > + DriverLen =3D CapsuleHeader->CapsuleImageSize - CapsuleHeader->Hea= derSize - > (UINTN)ItemOffsetList[Index]; > + } else { > + DriverLen =3D (UINTN)ItemOffsetList[Index + 1] - (UINTN)ItemOffset= List[Index]; > + } > + > + DEBUG((EFI_D_INFO, "FmpCapsule: LoadImage ...\n")); > + Status =3D gBS->LoadImage( > + FALSE, > + gImageHandle, > + DriverDevicePath, > + (UINT8 *)FmpCapsuleHeader + ItemOffsetList[Index], > + DriverLen, > + &ImageHandle > + ); > + DEBUG((EFI_D_INFO, "FmpCapsule: LoadImage - %r\n", Status)); > + if (EFI_ERROR(Status)) { > + StatusRet =3D Status; > + goto EXIT; > + } > + > + DEBUG((EFI_D_INFO, "FmpCapsule: StartImage ...\n")); > + Status =3D gBS->StartImage( > + ImageHandle, > + &ExitDataSize, > + NULL > + ); > + DEBUG((EFI_D_INFO, "FmpCapsule: StartImage - %r\n", Status)); > + if (EFI_ERROR(Status)) { > + DEBUG ((DEBUG_ERROR, "Driver Return Status =3D %r\n", Status)); > + StatusRet =3D Status; > + goto EXIT; > + } > + } > + > + // > + // 2. Route payload to right FMP instance > + // > + DEBUG((EFI_D_INFO, "FmpCapsule: route payload to right FMP instance ..= .\n")); > + > + Status =3D gBS->LocateHandleBuffer ( > + ByProtocol, > + &gEfiFirmwareManagementProtocolGuid, > + NULL, > + &NumberOfHandles, > + &HandleBuffer > + ); > + > + if (!EFI_ERROR(Status)) { > + for(Index1 =3D 0; Index1 < NumberOfHandles; Index1++) { > + Status =3D gBS->HandleProtocol( > + HandleBuffer[Index1], > + &gEfiFirmwareManagementProtocolGuid, > + (VOID **)&Fmp > + ); > + if (EFI_ERROR(Status)) { > + continue; > + } > + > + ImageInfoSize =3D 0; > + Status =3D Fmp->GetImageInfo ( > + Fmp, > + &ImageInfoSize, > + NULL, > + NULL, > + NULL, > + NULL, > + NULL, > + NULL > + ); > + if (Status !=3D EFI_BUFFER_TOO_SMALL) { > + continue; > + } > + > + FmpImageInfoBuf =3D NULL; > + FmpImageInfoBuf =3D AllocateZeroPool (ImageInfoSize); > + if (FmpImageInfoBuf =3D=3D NULL) { > + StatusRet =3D EFI_OUT_OF_RESOURCES; > + goto EXIT; > + } > + > + PackageVersionName =3D NULL; > + Status =3D Fmp->GetImageInfo ( > + Fmp, > + &ImageInfoSize, // ImageInfoSize > + FmpImageInfoBuf, // ImageInfo > + &FmpImageInfoDescriptorVer, // DescriptorVersion > + &FmpImageInfoCount, // DescriptorCount > + &DescriptorSize, // DescriptorSize > + &PackageVersion, // PackageVersion > + &PackageVersionName // PackageVersionNam= e > + ); > + > + // > + // If FMP GetInformation interface failed, skip this resource > + // > + if (EFI_ERROR(Status)) { > + FreePool(FmpImageInfoBuf); > + continue; > + } > + > + DEBUG((EFI_D_INFO, "FMP (%d) ImageInfo:\n", Index)); > + DumpFmpImageInfo( > + ImageInfoSize, // ImageInfoSize > + FmpImageInfoBuf, // ImageInfo > + FmpImageInfoDescriptorVer, // DescriptorVersion > + FmpImageInfoCount, // DescriptorCount > + DescriptorSize, // DescriptorSize > + PackageVersion, // PackageVersion > + PackageVersionName // PackageVersionName > + ); > + > + if (PackageVersionName !=3D NULL) { > + FreePool(PackageVersionName); > + } > + > + TempFmpImageInfo =3D FmpImageInfoBuf; > + for (Index2 =3D 0; Index2 < FmpImageInfoCount; Index2++) { > + // > + // Check all the payload entry in capsule payload list > + // > + for (Index =3D FmpCapsuleHeader->EmbeddedDriverCount; Index < It= emNum; Index++) > { > + ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER= *)((UINT8 > *)FmpCapsuleHeader + ItemOffsetList[Index]); > + > + if (IsFmpCapsuleProcessed(CapsuleHeader, Index - FmpCapsuleHea= der- > >EmbeddedDriverCount, ImageHeader)) { > + DEBUG((EFI_D_INFO, "FMP Capsule already processed (%g):", Ca= psuleHeader)); > + DEBUG((EFI_D_INFO, "ImageTypeId - %g, ", ImageHeader->Update= ImageTypeId)); > + DEBUG((EFI_D_INFO, "PayloadIndex - 0x%x, ImageIndex - 0x%x\n= ", Index - > FmpCapsuleHeader->EmbeddedDriverCount, ImageHeader->UpdateImageIndex)); > + continue; > + } > + > + if (CompareGuid(&ImageHeader->UpdateImageTypeId, &TempFmpImage= Info- > >ImageTypeId) && > + ImageHeader->UpdateImageIndex =3D=3D TempFmpImageInfo->Ima= geIndex) { > + AbortReason =3D NULL; > + if (ImageHeader->Version >=3D > EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER_INIT_VERSION) { > + if(ImageHeader->UpdateHardwareInstance !=3D 0){ > + // > + // FMP Version is >=3D2 & UpdateHardwareInstance Skip 2 = case > + // 1. FMP Image info Version < 3 > + // 2. HardwareInstance doesn't match > + // > + if (FmpImageInfoDescriptorVer < EFI_FIRMWARE_IMAGE_DESCR= IPTOR_VERSION > || > + ImageHeader->UpdateHardwareInstance !=3D TempFmpImage= Info- > >HardwareInstance) { > + continue; > + } > + } > + Image =3D (UINT8 *)(ImageHeader + 1); > + } else { > + // > + // If the EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER is = version 1, > only match ImageTypeId. > + // Header should exclude UpdateHardwareInstance field > + // > + Image =3D (UINT8 *)ImageHeader + > OFFSET_OF(EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER, UpdateHardwareIns= tance); > + } > + > + if (ImageHeader->UpdateVendorCodeSize =3D=3D 0) { > + VendorCode =3D NULL; > + } else { > + VendorCode =3D Image + ImageHeader->UpdateImageSize; > + } > + DEBUG((EFI_D_INFO, "Fmp->SetImage ...\n")); > + Status =3D Fmp->SetImage( > + Fmp, > + ImageHeader->UpdateImageIndex, // I= mageIndex > + Image, // I= mage > + ImageHeader->UpdateImageSize, // I= mageSize > + VendorCode, = // > VendorCode > + Update_Image_Progress, // P= rogress > + &AbortReason // A= bortReason > + ); > + DEBUG((EFI_D_INFO, "Fmp->SetImage - %r\n", Status)); > + if (AbortReason !=3D NULL) { > + DEBUG ((EFI_D_ERROR, "%s\n", AbortReason)); > + FreePool(AbortReason); > + } > + RecordFmpCapsuleStatusVariable( > + CapsuleHeader, // CapsuleG= uid > + Status, // CapsuleS= tatus > + Index - FmpCapsuleHeader->EmbeddedDriverCount, // PayloadI= ndex > + ImageHeader // ImageHea= der > + ); > + if (StatusRet !=3D EFI_SUCCESS) { > + StatusRet =3D Status; > + } > + // > + // Update EsrtEntry For V1, V2 FMP instance. V3 FMP ESRT cac= he will be > synced up through EsrtSyncFmp interface > + // > + if (FmpImageInfoDescriptorVer < EFI_FIRMWARE_IMAGE_DESCRIPTO= R_VERSION && > EsrtProtocol !=3D NULL) { > + StatusEsrt =3D EsrtProtocol->GetEsrtEntry(&TempFmpImageIn= fo->ImageTypeId, > &EsrtEntry); > + if (!EFI_ERROR(StatusEsrt)){ > + if (!EFI_ERROR(Status)) { > + EsrtEntry.LastAttemptStatus =3D LAST_ATTEMPT_STATUS_S= UCCESS; > + } else { > + EsrtEntry.LastAttemptStatus =3D > LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL; > + } > + EsrtEntry.LastAttemptVersion =3D 0; > + EsrtProtocol->UpdateEsrtEntry(&EsrtEntry); > + } > + } > + } > + } > + // > + // Use DescriptorSize to move ImageInfo Pointer to stay compatib= le with > different ImageInfo version > + // > + TempFmpImageInfo =3D (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)((UINT8 *)= TempFmpImageInfo > + DescriptorSize); > + } > + FreePool(FmpImageInfoBuf); > + } > + } > + > + // > + // final check for AreAllImagesProcessed > + // > + *AreAllImagesProcessed =3D TRUE; > + for (Index =3D FmpCapsuleHeader->EmbeddedDriverCount; Index < ItemNum;= Index++) { > + ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *)((UI= NT8 > *)FmpCapsuleHeader + ItemOffsetList[Index]); > + > + if (!IsFmpCapsuleProcessed(CapsuleHeader, Index - FmpCapsuleHeader- > >EmbeddedDriverCount, ImageHeader)) { > + *AreAllImagesProcessed =3D FALSE; > + break; > + } > + } > + > +EXIT: > + > + if (HandleBuffer !=3D NULL) { > + FreePool(HandleBuffer); > + } > + > + if (DriverDevicePath !=3D NULL) { > + FreePool(DriverDevicePath); > + } > + > + return StatusRet; > +} > + > +/** > + Return if there is a FMP header below capsule header. > + > + @param[in] CapsuleHeader A pointer to EFI_CAPSULE_HEADER > + > + @retval TRUE There is a FMP header below capsule header. > + @retval FALSE There is not a FMP header below capsule header > +**/ > +BOOLEAN > +IsNestedFmpCapsule( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + EFI_STATUS Status; > + EFI_SYSTEM_RESOURCE_TABLE *Esrt; > + EFI_SYSTEM_RESOURCE_ENTRY *EsrtEntry; > + UINTN Index; > + BOOLEAN EsrtGuidFound; > + EFI_CAPSULE_HEADER *NestedCapsuleHeader; > + UINTN NestedCapsuleSize; > + ESRT_MANAGEMENT_PROTOCOL *EsrtProtocol; > + EFI_SYSTEM_RESOURCE_ENTRY Entry; > + > + EsrtGuidFound =3D FALSE; > + > + // > + // Check ESRT protocol > + // > + Status =3D gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VO= ID > **)&EsrtProtocol); > + if (!EFI_ERROR(Status)) { > + Status =3D EsrtProtocol->GetEsrtEntry(&CapsuleHeader->CapsuleGuid, &= Entry); > + if (!EFI_ERROR(Status)) { > + EsrtGuidFound =3D TRUE; > + } > + } > + > + // > + // Check ESRT configuration table > + // > + if (!EsrtGuidFound) { > + Status =3D EfiGetSystemConfigurationTable(&gEfiSystemResourceTableGu= id, (VOID > **)&Esrt); > + if (!EFI_ERROR(Status)) { > + EsrtEntry =3D (VOID *)(Esrt + 1); > + for (Index =3D 0; Index < Esrt->FwResourceCount; Index++, EsrtEntr= y++) { > + if (CompareGuid(&EsrtEntry->FwClass, &CapsuleHeader->CapsuleGuid= )) { > + EsrtGuidFound =3D TRUE; > + break; > + } > + } > + } > + } > + if (!EsrtGuidFound) { > + return FALSE; > + } > + > + // > + // Check nested capsule header > + // FMP GUID after ESRT one > + // > + NestedCapsuleHeader =3D (EFI_CAPSULE_HEADER *)((UINT8 *)CapsuleHeader = + CapsuleHeader- > >HeaderSize); > + NestedCapsuleSize =3D (UINTN)CapsuleHeader + CapsuleHeader->CapsuleIma= geSize - > (UINTN)NestedCapsuleHeader; > + if (NestedCapsuleSize < sizeof(EFI_CAPSULE_HEADER)) { > + return FALSE; > + } > + if (!IsValidCapsuleHeader(NestedCapsuleHeader, NestedCapsuleSize)) { > + return FALSE; > + } > + if (!IsFmpCapsuleGuid(&NestedCapsuleHeader->CapsuleGuid)) { > + return FALSE; > + } > + DEBUG ((EFI_D_INFO, "IsNestedFmpCapsule\n")); > + return TRUE; > +} > + > +/** > + Return if this FMP is a system FMP or a device FMP, based upon Capsule= Header. > + > + @param[in] CapsuleHeader A pointer to EFI_CAPSULE_HEADER > + > + @retval TRUE It is a system FMP. > + @retval FALSE It is a device FMP. > +**/ > +BOOLEAN > +IsFmpCapsule( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + if (IsFmpCapsuleGuid(&CapsuleHeader->CapsuleGuid)) { > + return TRUE; > + } > + if (IsNestedFmpCapsule(CapsuleHeader)) { > + return TRUE; > + } > + return FALSE; > +} > + > +/** > + Those capsules supported by the firmwares. > + > + Caution: This function may receive untrusted input. > + > + @param CapsuleHeader Points to a capsule header. > + > + @retval EFI_SUCESS Input capsule is supported by firmware. > + @retval EFI_UNSUPPORTED Input capsule is not supported by the firmwar= e. > + @retval EFI_INVALID_PARAMETER Input capsule layout is not correct > +**/ > +EFI_STATUS > +EFIAPI > +SupportCapsuleImage ( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + // > + // check Display Capsule Guid > + // > + if (CompareGuid (&gWindowsUxCapsuleGuid, &CapsuleHeader->CapsuleGuid))= { > + return EFI_SUCCESS; > + } > + > + if (IsFmpCapsule(CapsuleHeader)) { > + // > + // Check layout of FMP capsule > + // > + return ValidateFmpCapsule(CapsuleHeader, NULL); > + } > + DEBUG((EFI_D_ERROR, "Unknown Capsule Guid - %g\n", &CapsuleHeader->Cap= suleGuid)); > + return EFI_UNSUPPORTED; > +} > + > +/** > + The firmware implements to process the capsule image. > + > + Caution: This function may receive untrusted input. > + > + @param CapsuleHeader Points to a capsule header. > + > + @retval EFI_SUCESS Process Capsule Image successfully. > + @retval EFI_UNSUPPORTED Capsule image is not supported by the fi= rmware. > + @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted. > + @retval EFI_OUT_OF_RESOURCES Not enough memory. > +**/ > +EFI_STATUS > +EFIAPI > +ProcessCapsuleImage ( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ) > +{ > + EFI_STATUS Status; > + BOOLEAN AreAllImagesProcessed; > + > + if (SupportCapsuleImage (CapsuleHeader) !=3D EFI_SUCCESS) { > + return EFI_UNSUPPORTED; > + } > + > + // > + // Display image in firmware update display capsule > + // > + if (CompareGuid (&gWindowsUxCapsuleGuid, &CapsuleHeader->CapsuleGuid))= { > + DEBUG((EFI_D_INFO, "ProcessCapsuleImage for WindowsUxCapsule ...\n")= ); > + Status =3D DisplayCapsuleImage(CapsuleHeader); > + RecordCapsuleStatusVariable(CapsuleHeader, Status); > + return Status; > + } > + > + // > + // Check FMP capsule layout > + // > + if (IsFmpCapsule (CapsuleHeader)) { > + DEBUG((EFI_D_INFO, "ProcessCapsuleImage for FmpCapsule ...\n")); > + DEBUG((EFI_D_INFO, "ValidateFmpCapsule ...\n")); > + Status =3D ValidateFmpCapsule(CapsuleHeader, NULL); > + DEBUG((EFI_D_INFO, "ValidateFmpCapsule - %r\n", Status)); > + if (EFI_ERROR(Status)) { > + return Status; > + } > + > + // > + // Press EFI FMP Capsule > + // > + DEBUG((EFI_D_INFO, "ProcessFmpCapsuleImage ...\n")); > + Status =3D ProcessFmpCapsuleImage(CapsuleHeader, &AreAllImagesProces= sed); > + DEBUG((EFI_D_INFO, "ProcessFmpCapsuleImage - %r\n", Status)); > + > + if (!AreAllImagesProcessed) { > + mAreAllImagesProcessed =3D FALSE; > + } > + > + return Status; > + } > + > + return EFI_UNSUPPORTED; > +} > + > +/** > + Callback function executed when the EndOfDxe event group is signaled. > + > + @param[in] Event Event whose notification function is being invok= ed. > + @param[in] Context The pointer to the notification function's conte= xt, which > + is implementation-dependent. > +**/ > +VOID > +EFIAPI > +DxeCapsuleLibEndOfDxe( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + mDxeCapsuleLibEndOfDxe =3D TRUE; > +} > + > +/** > + The constructor function. > + > + @param ImageHandle The firmware allocated handle for the EFI image. > + @param SystemTable A pointer to the EFI System Table. > + > + @retval EFI_SUCCESS The constructor successfully . > +**/ > +EFI_STATUS > +EFIAPI > +DxeCapsuleLibConstructor( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + EFI_EVENT EndOfDxeEvent; > + EFI_STATUS Status; > + > + Status =3D gBS->CreateEventEx ( > + EVT_NOTIFY_SIGNAL, > + TPL_CALLBACK, > + DxeCapsuleLibEndOfDxe, > + NULL, > + &gEfiEndOfDxeEventGroupGuid, > + &EndOfDxeEvent > + ); > + ASSERT_EFI_ERROR (Status); > + > + InitCapsuleVariable(); > + > + return EFI_SUCCESS; > +} > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.inf > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.inf > new file mode 100644 > index 0000000..5e437dc > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.inf > @@ -0,0 +1,80 @@ > +## @file > +# Capsule library instance for DXE_DRIVER. > +# > +# Capsule library instance for DXE_DRIVER module types. > +# > +# Copyright (c) 2016, Intel Corporation. All rights reserved.
> +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the= BSD License > +# which accompanies this distribution. The full text of the license ma= y be found at > +# http://opensource.org/licenses/bsd-license.php > +# > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D DxeCapsuleLib > + MODULE_UNI_FILE =3D DxeCapsuleLib.uni > + FILE_GUID =3D 534E35DE-8EB3-47b3-A4E0-72A571E5073= 3 > + MODULE_TYPE =3D DXE_DRIVER > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D CapsuleLib|DXE_DRIVER UEFI_APPLICAT= ION > + CONSTRUCTOR =3D DxeCapsuleLibConstructor > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC > +# > + > +[Sources] > + DxeCapsuleLib.c > + DxeCapsuleProcessLib.c > + DxeCapsuleReportLib.c > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + MemoryAllocationLib > + DxeServicesTableLib > + UefiBootServicesTableLib > + DevicePathLib > + ReportStatusCodeLib > + PrintLib > + HobLib > + > +[Pcd] > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleMax = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdSystemRebootAfterCapsuleProcessFlag = ## > CONSUMES > + > + gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeSubClassCapsule = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeProcessCapsulesBegi= n ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeProcessCapsulesEnd = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdatingFirmware = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdateFirmwareSucce= ss ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdateFirmwareFaile= d ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeResettingSystem = ## > CONSUMES > + > +[Protocols] > + gEsrtManagementProtocolGuid ## CONSUMES > + gEfiFirmwareManagementProtocolGuid ## SOMETIMES_CONSUMES > + gEdkiiVariableLockProtocolGuid ## SOMETIMES_CONSUMES > + > +[Guids] > + gEfiFmpCapsuleGuid ## SOMETIMES_CONSUMES ## GUID > + gWindowsUxCapsuleGuid ## SOMETIMES_CONSUMES ## GUID > + gEfiSystemResourceTableGuid ## SOMETIMES_CONSUMES ## GUID > + gEfiCapsuleReportGuid ## CONSUMES ## Variable > + gEfiCapsuleVendorGuid ## CONSUMES ## Variable > + gEfiEndOfDxeEventGroupGuid ## CONSUMES ## Event > + > +[Depex] > + gEfiVariableWriteArchProtocolGuid > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.uni > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.uni > new file mode 100644 > index 0000000..05a80d0 > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.uni > @@ -0,0 +1,22 @@ > +// /** @file > +// Capsule library instance for DXE_DRIVER. > +// > +// Capsule library instance for DXE_DRIVER module types. > +// > +// Copyright (c) 2016, Intel Corporation. All rights reserved.
> +// > +// This program and the accompanying materials > +// are licensed and made available under the terms and conditions of the= BSD License > +// which accompanies this distribution. The full text of the license ma= y be found at > +// http://opensource.org/licenses/bsd-license.php > +// > +// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. > +// > +// **/ > + > + > +#string STR_MODULE_ABSTRACT #language en-US "Capsule Support= Library" > + > +#string STR_MODULE_DESCRIPTION #language en-US "Capsule library= instance for > DXE_DRIVER module types." > + > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c > new file mode 100644 > index 0000000..c84cd3e > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c > @@ -0,0 +1,486 @@ > +/** @file > + DXE capsule process. > + > + Caution: This module requires additional review when modified. > + This module will have external input - capsule image. > + This external input must be validated carefully to avoid security issu= e like > + buffer overflow, integer overflow. > + > + ProcessCapsules(), ProcessTheseCapsules() will receive untrusted > + input and do basic validation. > + > + Copyright (c) 2016, Intel Corporation. All rights reserved.
> + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the = BSD License > + which accompanies this distribution. The full text of the license may= be found at > + http://opensource.org/licenses/bsd-license.php > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR I= MPLIED. > + > +**/ > + > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +/** > + Return if this FMP is a system FMP or a device FMP, based upon Capsule= Header. > + > + @param[in] CapsuleHeader A pointer to EFI_CAPSULE_HEADER > + > + @retval TRUE It is a system FMP. > + @retval FALSE It is a device FMP. > +**/ > +BOOLEAN > +IsFmpCapsule( > + IN EFI_CAPSULE_HEADER *CapsuleHeader > + ); > + > +/** > + Validate Fmp capsules layout. > + > + Caution: This function may receive untrusted input. > + > + This function assumes the caller validated the capsule by using > + IsValidCapsuleHeader(), so that all fields in EFI_CAPSULE_HEADER are c= orrect. > + The capsule buffer size is CapsuleHeader->CapsuleImageSize. > + > + This function validates the fields in EFI_FIRMWARE_MANAGEMENT_CAPSULE_= HEADER > + and EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER. > + > + This function need support nested FMP capsule. > + > + @param[in] CapsuleHeader Points to a capsule header. > + @param[out] EmbeddedDriverCount The EmbeddedDriverCount in the FMP c= apsule. > + > + @retval EFI_SUCESS Input capsule is a correct FMP capsule. > + @retval EFI_INVALID_PARAMETER Input capsule is not a correct FMP caps= ule. > +**/ > +EFI_STATUS > +ValidateFmpCapsule( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + OUT UINT16 *EmbeddedDriverCount OPTIONAL > + ); > + > +/** > + Validate if it is valid capsule header > + > + This function assumes the caller provided correct CapsuleHeader pointe= r > + and CapsuleSize. > + > + This function validates the fields in EFI_CAPSULE_HEADER. > + > + @param CapsuleHeader Points to a capsule header. > + @param CapsuleSize Size of the whole capsule image. > + > +**/ > +BOOLEAN > +IsValidCapsuleHeader( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN UINT64 CapsuleSize > + ); > + > +extern BOOLEAN mDxeCapsuleLibEndOfDxe; > +BOOLEAN mNeedReset; > +BOOLEAN mAreAllImagesProcessed; > + > +/** > + > + This routine is called to process capsules. > + > + Caution: This function may receive untrusted input. > + > + Each individual capsule result is recorded in capsule record variable. > + > + @param NeedBlockDriver TRUE: Need skip the FMP capsules with = non zero > EmbeddedDriverCount. > + FALSE: No need to skip any FMP capsule= s. > + > + @retval EFI_SUCCESS There is no error when processing caps= ules. > + @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules= . > + > +**/ > +EFI_STATUS > +ProcessTheseCapsules ( > + IN BOOLEAN NeedBlockDriver > + ) > +{ > + EFI_STATUS Status; > + EFI_PEI_HOB_POINTERS HobPointer; > + EFI_CAPSULE_HEADER *CapsuleHeader; > + UINT32 Size; > + UINT32 CapsuleNumber; > + UINT32 CapsuleTotalNumber; > + EFI_CAPSULE_TABLE *CapsuleTable; > + UINT32 Index; > + UINT32 CacheIndex; > + UINT32 CacheNumber; > + VOID **CapsulePtr; > + VOID **CapsulePtrCache; > + EFI_GUID *CapsuleGuidCache; > + EFI_STATUS *CapsuleStatusArray; > + BOOLEAN DisplayCapsuleExist; > + ESRT_MANAGEMENT_PROTOCOL *EsrtManagement; > + UINT16 EmbeddedDriverCount; > + > + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (PcdGet32(PcdStatusCodeSubClassC= apsule) | > PcdGet32(PcdCapsuleStatusCodeProcessCapsulesBegin))); > + > + CapsuleNumber =3D 0; > + CapsuleTotalNumber =3D 0; > + CacheIndex =3D 0; > + CacheNumber =3D 0; > + CapsulePtr =3D NULL; > + CapsulePtrCache =3D NULL; > + CapsuleGuidCache =3D NULL; > + DisplayCapsuleExist =3D FALSE; > + EsrtManagement =3D NULL; > + > + Status =3D EFI_SUCCESS; > + // > + // Find all capsule images from hob > + // > + HobPointer.Raw =3D GetHobList (); > + while ((HobPointer.Raw =3D GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobP= ointer.Raw)) !=3D > NULL) { > + if (!IsValidCapsuleHeader((VOID *)(UINTN)HobPointer.Capsule->BaseAdd= ress, > HobPointer.Capsule->Length)) { > + HobPointer.Header->HobType =3D EFI_HOB_TYPE_UNUSED; // Mark this h= ob as invalid > + } else { > + CapsuleTotalNumber++; > + } > + HobPointer.Raw =3D GET_NEXT_HOB (HobPointer); > + } > + > + if (CapsuleTotalNumber =3D=3D 0) { > + // > + // We didn't find a hob, so had no errors. > + // > + DEBUG ((EFI_D_ERROR, "We can not find capsule data in capsule update= boot > mode.\n")); > + Status =3D EFI_SUCCESS; > + goto Done; > + } > + > + // > + // Init temp Capsule Data table. > + // > + CapsulePtr =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * > CapsuleTotalNumber); > + ASSERT (CapsulePtr !=3D NULL); > + if (CapsulePtr =3D=3D NULL) { > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > + } > + CapsulePtrCache =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * > CapsuleTotalNumber); > + ASSERT (CapsulePtrCache !=3D NULL); > + if (CapsulePtrCache =3D=3D NULL) { > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > + } > + CapsuleGuidCache =3D (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) = * > CapsuleTotalNumber); > + ASSERT (CapsuleGuidCache !=3D NULL); > + if (CapsuleGuidCache =3D=3D NULL) { > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > + } > + CapsuleStatusArray =3D (EFI_STATUS *) AllocateZeroPool (sizeof (EFI_ST= ATUS) * > CapsuleTotalNumber); > + ASSERT (CapsuleStatusArray !=3D NULL); > + if (CapsuleStatusArray =3D=3D NULL) { > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > + } > + > + // > + // Find all capsule images from hob > + // > + HobPointer.Raw =3D GetHobList (); > + while ((HobPointer.Raw =3D GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobP= ointer.Raw)) !=3D > NULL) { > + CapsulePtr [CapsuleNumber++] =3D (VOID *) (UINTN) HobPointer.Capsule= ->BaseAddress; > + HobPointer.Raw =3D GET_NEXT_HOB (HobPointer); > + } > + > + // > + // Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_T= ABLE, install > + // capsuleTable to configure table with EFI_CAPSULE_GUID > + // > + > + // > + // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are us= ed for > operating > + // System to have information persist across a system reset. EFI Syste= m Table must > + // point to an array of capsules that contains the same CapsuleGuid va= lue. And > agents > + // searching for this type capsule will look in EFI System Table and s= earch for the > + // capsule's Guid and associated pointer to retrieve the data. Two ste= ps below > describes > + // how to sorting the capsules by the unique guid and install the arra= y to EFI > System Table. > + // Firstly, Loop for all coalesced capsules, record unique CapsuleGuid= s and cache > them in an > + // array for later sorting capsules by CapsuleGuid. > + // > + for (Index =3D 0; Index < CapsuleTotalNumber; Index++) { > + CapsuleStatusArray [Index] =3D EFI_UNSUPPORTED; > + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) CapsulePtr [Index]; > + if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != =3D 0) { > + // > + // For each capsule, we compare it with known CapsuleGuid in the C= acheArray. > + // If already has the Guid, skip it. Whereas, record it in the Cac= heArray as > + // an additional one. > + // > + CacheIndex =3D 0; > + while (CacheIndex < CacheNumber) { > + if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->Ca= psuleGuid)) { > + break; > + } > + CacheIndex++; > + } > + if (CacheIndex =3D=3D CacheNumber) { > + CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader- > >CapsuleGuid,sizeof(EFI_GUID)); > + } > + } > + } > + > + // > + // Secondly, for each unique CapsuleGuid in CacheArray, gather all coa= lesced > capsules > + // whose guid is the same as it, and malloc memory for an array which = preceding > + // with UINT32. The array fills with entry point of capsules that have= the same > + // CapsuleGuid, and UINT32 represents the size of the array of capsule= s. Then > install > + // this array into EFI System Table, so that agents searching for this= type capsule > + // will look in EFI System Table and search for the capsule's Guid and= associated > + // pointer to retrieve the data. > + // > + CacheIndex =3D 0; > + while (CacheIndex < CacheNumber) { > + CapsuleNumber =3D 0; > + for (Index =3D 0; Index < CapsuleTotalNumber; Index++) { > + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) CapsulePtr [Index]; > + if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != =3D 0) { > + if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->= CapsuleGuid)) > { > + // > + // Cache Caspuleheader to the array, this array is uniqued wit= h certain > CapsuleGuid. > + // > + CapsulePtrCache[CapsuleNumber++] =3D (VOID*)CapsuleHeader; > + // > + // When a Capsule is listed in CapsulePtrCache, it will be rep= orted in > ConfigurationTable > + // So, report the CapsuleStatus as "processed successfully". > + // > + CapsuleStatusArray [Index] =3D EFI_SUCCESS; > + } > + } > + } > + if (CapsuleNumber !=3D 0) { > + Size =3D sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(= VOID*); > + CapsuleTable =3D AllocateRuntimePool (Size); > + ASSERT (CapsuleTable !=3D NULL); > + if (CapsuleTable =3D=3D NULL) { > + return EFI_OUT_OF_RESOURCES; > + } > + CapsuleTable->CapsuleArrayNumber =3D CapsuleNumber; > + CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumb= er * > sizeof(VOID*)); > + Status =3D gBS->InstallConfigurationTable (&CapsuleGuidCache[Cache= Index], > (VOID*)CapsuleTable); > + ASSERT_EFI_ERROR (Status); > + } > + CacheIndex++; > + } > + > + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (PcdGet32(PcdStatusCodeSubClassC= apsule) | > PcdGet32(PcdCapsuleStatusCodeUpdatingFirmware))); > + > + // > + // If Windows UX capsule exist, process it first > + // > + for (Index =3D 0; Index < CapsuleTotalNumber; Index++) { > + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) CapsulePtr [Index]; > + if (CompareGuid(&CapsuleHeader->CapsuleGuid ,&gWindowsUxCapsuleGuid)= ) { > + DEBUG ((EFI_D_INFO, "ProcessCapsuleImage (Ux) - 0x%x\n", CapsuleHe= ader)); > + DisplayCapsuleExist =3D TRUE; > + DEBUG ((DEBUG_INFO, "Display logo capsule is found.\n")); > + Status =3D ProcessCapsuleImage (CapsuleHeader); > + DEBUG((EFI_D_INFO, "ProcessCapsuleImage (Ux) - %r\n", Status)); > + CapsuleStatusArray [Index] =3D Status; > + break; > + } > + } > + > + if (!DisplayCapsuleExist) { > + // > + // Display Capsule not found. Display the default string. > + // > + Print (L"Updating the firmware ......\r\n"); > + } > + > + // > + // All capsules left are recognized by platform. > + // > + for (Index =3D 0; Index < CapsuleTotalNumber; Index++) { > + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) CapsulePtr [Index]; > + if (!CompareGuid(&CapsuleHeader->CapsuleGuid ,&gWindowsUxCapsuleGuid= )) { > + // > + // Call capsule library to process capsule image. > + // > + EmbeddedDriverCount =3D 0; > + if (IsFmpCapsule(CapsuleHeader)) { > + Status =3D ValidateFmpCapsule(CapsuleHeader, &EmbeddedDriverCoun= t); > + if (EFI_ERROR(Status)) { > + DEBUG((DEBUG_ERROR, "ValidateFmpCapsule failed. Ignore!\n")); > + continue; > + } > + } > + > + if ((!NeedBlockDriver) || (EmbeddedDriverCount =3D=3D 0)) { > + DEBUG((EFI_D_INFO, "ProcessCapsuleImage - 0x%x\n", CapsuleHeader= )); > + Status =3D ProcessCapsuleImage (CapsuleHeader); > + CapsuleStatusArray [Index] =3D Status; > + DEBUG((EFI_D_INFO, "ProcessCapsuleImage - %r\n", Status)); > + > + if (EFI_ERROR(Status)) { > + REPORT_STATUS_CODE(EFI_ERROR_CODE, (PcdGet32(PcdStatusCodeSubC= lassCapsule) | > PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareFailed))); > + DEBUG ((DEBUG_ERROR, "Capsule process failed. reset the system= !\n")); > + Print (L"Firmware update failed...\r\n"); > + } else { > + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, > (PcdGet32(PcdStatusCodeSubClassCapsule) | > PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareSuccess))); > + } > + > + if ((CapsuleHeader->Flags & PcdGet16(PcdSystemRebootAfterCapsule= ProcessFlag)) > !=3D 0 || > + IsFmpCapsule(CapsuleHeader)) { > + mNeedReset =3D TRUE; > + } > + } > + } > + } > + > + Status =3D gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VO= ID > **)&EsrtManagement); > + // > + // Always sync ESRT Cache from FMP Instance > + // > + if (!EFI_ERROR(Status)) { > + EsrtManagement->SyncEsrtFmp(); > + } > + Status =3D EFI_SUCCESS; > + > +Done: > + // > + // Free the allocated temp memory space. > + // > + if (CapsuleGuidCache !=3D NULL) { > + FreePool(CapsuleGuidCache); > + } > + if (CapsulePtrCache !=3D NULL) { > + FreePool(CapsulePtrCache); > + } > + if (CapsulePtr !=3D NULL) { > + FreePool(CapsulePtr); > + } > + > + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (PcdGet32(PcdStatusCodeSubClassC= apsule) | > PcdGet32(PcdCapsuleStatusCodeProcessCapsulesEnd))); > + > + return Status; > +} > + > +/** > +**/ > +BOOLEAN > +IsAllCapsulesProcessed( > + VOID > + ) > +{ > + // TBD > + return TRUE; > +} > + > +/** > + Do reset system. > +**/ > +VOID > +DoResetSystem( > + VOID > + ) > +{ > + UINTN Index; > + > + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (PcdGet32(PcdStatusCodeSubClassC= apsule) | > PcdGet32(PcdCapsuleStatusCodeResettingSystem))); > + > + Print(L"Capsule Request Cold Reboot.\n"); > + DEBUG((EFI_D_INFO, "Capsule Request Cold Reboot.")); > + > + for (Index =3D 5; Index > 0; Index--) { > + Print(L"\rResetting system in %d seconds ...", Index); > + DEBUG((EFI_D_INFO, "\rResetting system in %d seconds ...", Index)); > + gBS->Stall(1000000); > + } > + > + gRT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); > + > + CpuDeadLoop(); > +} > + > +/** > + > + This routine is called to process capsules. > + > + Caution: This function may receive untrusted input. > + > + The capsules reported in EFI_HOB_UEFI_CAPSULE are processed. > + If there is no EFI_HOB_UEFI_CAPSULE, this routine does nothing. > + > + This routine should be called twice in BDS. > + 1) The first call must be before EndOfDxe. The system capsules is proc= essed. > + If device capsule FMP protocols are exposted at this time and devic= e FMP > + capsule has zero EmbeddedDriverCount, the device capsules are proce= ssed. > + Each individual capsule result is recorded in capsule record variab= le. > + System may reset in this function, if reset is required by capsule = and > + all capsules are processed. > + If not all capsules are processed, reset will be defered to second = call. > + > + 2) The second call must be after EndOfDxe and after ConnectAll, so tha= t all > + device capsule FMP protocols are exposed. > + The system capsules are skipped. If the device capsules are NOT pro= cessed > + in first call, they are processed here. > + Each individual capsule result is recorded in capsule record variab= le. > + System may reset in this function, if reset is required by capsule > + processed in first call and second call. > + > + @retval EFI_SUCCESS There is no error when processing caps= ules. > + @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules= . > + > +**/ > +EFI_STATUS > +EFIAPI > +ProcessCapsules( > + VOID > + ) > +{ > + EFI_STATUS Status; > + > + if (!mDxeCapsuleLibEndOfDxe) { > + // > + // Initialize mAreAllImagesProcessed to be TRUE. > + // > + // It will be updated to FALSE in ProcessTheseCapsules()->ProcessCap= suleImage(), > + // if there is any FMP image in any FMP capsule not processed. > + // > + mAreAllImagesProcessed =3D TRUE; > + > + Status =3D ProcessTheseCapsules(TRUE); > + // > + // Reboot System if and only if all capsule processed. > + // If not, defer reset to 2nd process. > + // > + if (mNeedReset && mAreAllImagesProcessed) { > + DoResetSystem(); > + } > + } else { > + Status =3D ProcessTheseCapsules(FALSE); > + // > + // Reboot System if required after all capsule processed > + // > + if (mNeedReset) { > + DoResetSystem(); > + } > + } > + return Status; > +} > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c > new file mode 100644 > index 0000000..9edc353 > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c > @@ -0,0 +1,489 @@ > +/** @file > + DXE capsule report related function. > + > + Copyright (c) 2016, Intel Corporation. All rights reserved.
> + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the = BSD License > + which accompanies this distribution. The full text of the license may= be found at > + http://opensource.org/licenses/bsd-license.php > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR I= MPLIED. > + > +**/ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +typedef struct { > + EFI_CAPSULE_RESULT_VARIABLE_HEADER CapsuleResultHeader; > + EFI_CAPSULE_RESULT_VARIABLE_FMP CapsuleResultFmp; > +} CAPSULE_RESULT_VARIABLE_CACHE; > + > +#define CAPSULE_RESULT_VARIABLE_CACHE_COUNT 0x10 > + > +CAPSULE_RESULT_VARIABLE_CACHE *mCapsuleResultVariableCache; > +UINTN mCapsuleResultVariableCacheMaxCount; > +UINTN mCapsuleResultVariableCacheCount; > + > +/** > + Get current capsule last variable index. > + > + @return Current capsule last variable index. > + @retval -1 No current capsule last variable. > +**/ > +INTN > +GetCurrentCapsuleLastIndex( > + VOID > + ) > +{ > + UINTN Size; > + CHAR16 CapsuleLastStr[sizeof("Capsule####")]= ; > + EFI_STATUS Status; > + UINT16 CurrentIndex; > + > + Size =3D sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminato= r > + Status =3D gRT->GetVariable( > + L"CapsuleLast", > + &gEfiCapsuleReportGuid, > + NULL, > + &Size, > + CapsuleLastStr > + ); > + if (EFI_ERROR(Status)) { > + return -1; > + } > + CurrentIndex =3D (UINT16)StrHexToUintn(&CapsuleLastStr[sizeof("Capsule= ") - 1]); > + return CurrentIndex; > +} > + > +/** > + Check if this FMP capsule is processed. > + > + @param CapsuleHeader The capsule image header > + @param PayloadIndex FMP payload index > + @param ImageHeader FMP image header > + > + @retval TRUE This FMP capsule is processed. > + @retval FALSE This FMP capsule is not processed. > +**/ > +BOOLEAN > +IsFmpCapsuleProcessed( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN UINTN PayloadIndex, > + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader > + ) > +{ > + UINTN Index; > + EFI_CAPSULE_RESULT_VARIABLE_HEADER *CapsuleResult; > + EFI_CAPSULE_RESULT_VARIABLE_FMP *CapsuleResultFmp; > + > + for (Index =3D 0; Index < mCapsuleResultVariableCacheCount; Index++) { > + // > + // Check > + // > + CapsuleResult =3D &mCapsuleResultVariableCache[Index].CapsuleResultH= eader; > + if (CapsuleResult->VariableTotalSize >=3D > sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER)) { > + if (CompareGuid(&CapsuleResult->CapsuleGuid, &gEfiFmpCapsuleGuid))= { > + if (CapsuleResult->VariableTotalSize >=3D > sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER) + sizeof(EFI_CAPSULE_RESULT_VA= RIABLE_FMP)) { > + CapsuleResultFmp =3D (EFI_CAPSULE_RESULT_VARIABLE_FMP *)(Capsu= leResult + 1); > + if (CompareGuid(&CapsuleResultFmp->UpdateImageTypeId, &ImageHe= ader- > >UpdateImageTypeId) && > + (CapsuleResultFmp->UpdateImageIndex =3D=3D ImageHeader->Up= dateImageIndex) && > + (CapsuleResultFmp->PayloadIndex =3D=3D PayloadIndex) ) { > + return TRUE; > + } > + } > + } > + } > + } > + > + return FALSE; > +} > + > +/** > + Write a new capsule status variable cache. > + > + @param CapsuleResult The capsule status variable > + @param CapsuleResultSize The size of the capsule stauts variable in b= ytes > + > + @retval EFI_SUCCESS The capsule status variable is cached. > + @retval EFI_OUT_OF_RESOURCES No resource to cache the capsule status v= ariable. > +**/ > +EFI_STATUS > +WriteNewCapsuleResultVariableCache( > + IN VOID *CapsuleResult, > + IN UINTN CapsuleResultSize > + ) > +{ > + if (CapsuleResultSize > sizeof(CAPSULE_RESULT_VARIABLE_CACHE)) { > + CapsuleResultSize =3D sizeof(CAPSULE_RESULT_VARIABLE_CACHE); > + } > + > + if (mCapsuleResultVariableCacheCount =3D=3D mCapsuleResultVariableCach= eMaxCount) { > + mCapsuleResultVariableCache =3D ReallocatePool( > + mCapsuleResultVariableCacheMaxCount = * > sizeof(CAPSULE_RESULT_VARIABLE_CACHE), > + (mCapsuleResultVariableCacheMaxCount= + > CAPSULE_RESULT_VARIABLE_CACHE_COUNT) * sizeof(CAPSULE_RESULT_VARIABLE_CAC= HE), > + mCapsuleResultVariableCache > + ); > + if (mCapsuleResultVariableCache =3D=3D NULL) { > + return EFI_OUT_OF_RESOURCES; > + } > + mCapsuleResultVariableCacheMaxCount +=3D CAPSULE_RESULT_VARIABLE_CAC= HE_COUNT; > + } > + > + ASSERT(mCapsuleResultVariableCacheCount < mCapsuleResultVariableCacheM= axCount); > + ASSERT(mCapsuleResultVariableCache !=3D NULL); > + CopyMem( > + &mCapsuleResultVariableCache[mCapsuleResultVariableCacheCount], > + CapsuleResult, > + CapsuleResultSize > + ); > + mCapsuleResultVariableCacheCount++; > + > + return EFI_SUCCESS; > +} > + > +/** > + Get a new capsule status variable index. > + > + @return A new capsule status variable index. > + @retval -1 No new capsule status variable index. > +**/ > +INTN > +GetNewCapsuleResultIndex( > + VOID > + ) > +{ > + INTN CurrentIndex; > + > + CurrentIndex =3D GetCurrentCapsuleLastIndex(); > + if (CurrentIndex >=3D PcdGet16(PcdCapsuleMax)) { > + return -1; > + } > + > + return CurrentIndex + 1; > +} > + > +/** > + Write a new capsule status variable. > + > + @param CapsuleResult The capsule status variable > + @param CapsuleResultSize The size of the capsule stauts variable in b= ytes > + > + @retval EFI_SUCCESS The capsule status variable is recorded. > + @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status = variable. > +**/ > +EFI_STATUS > +WriteNewCapsuleResultVariable( > + IN VOID *CapsuleResult, > + IN UINTN CapsuleResultSize > + ) > +{ > + INTN CapsuleResultIndex; > + CHAR16 CapsuleResultStr[sizeof("Capsule##= ##")]; > + UINTN Size; > + EFI_STATUS Status; > + > + CapsuleResultIndex =3D GetNewCapsuleResultIndex(); > + DEBUG((EFI_D_INFO, "New CapsuleResultIndex - 0x%x\n", CapsuleResultInd= ex)); > + if (CapsuleResultIndex =3D=3D -1) { > + return EFI_OUT_OF_RESOURCES; > + } > + UnicodeSPrint( > + CapsuleResultStr, > + sizeof(CapsuleResultStr), > + L"Capsule%04x", > + CapsuleResultIndex > + ); > + > + Status =3D gRT->SetVariable( > + CapsuleResultStr, > + &gEfiCapsuleReportGuid, > + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_A= CCESS | > EFI_VARIABLE_RUNTIME_ACCESS, > + CapsuleResultSize, > + CapsuleResult > + ); > + if (!EFI_ERROR(Status)) { > + Size =3D sizeof(L"Capsule####") - sizeof(CHAR16); // no zero termina= tor > + DEBUG((EFI_D_INFO, "Set CapsuleLast - %s\n", CapsuleResultStr)); > + Status =3D gRT->SetVariable( > + L"CapsuleLast", > + &gEfiCapsuleReportGuid, > + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE= _ACCESS | > EFI_VARIABLE_RUNTIME_ACCESS, > + Size, > + CapsuleResultStr > + ); > + } > + > + return Status; > +} > + > +/** > + Record capsule status variable and to local cache. > + > + @param CapsuleHeader The capsule image header > + @param CapsuleStatus The capsule process stauts > + > + @retval EFI_SUCCESS The capsule status variable is recorded. > + @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status = variable. > +**/ > +EFI_STATUS > +RecordCapsuleStatusVariable( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN EFI_STATUS CapsuleStatus > + ) > +{ > + EFI_CAPSULE_RESULT_VARIABLE_HEADER CapsuleResultVariable; > + EFI_STATUS Status; > + > + CapsuleResultVariable.VariableTotalSize =3D sizeof(CapsuleResultVariab= le); > + CopyGuid (&CapsuleResultVariable.CapsuleGuid, &CapsuleHeader->CapsuleG= uid); > + ZeroMem(&CapsuleResultVariable.CapsuleProcessed, > sizeof(CapsuleResultVariable.CapsuleProcessed)); > + gRT->GetTime(&CapsuleResultVariable.CapsuleProcessed, NULL); > + CapsuleResultVariable.CapsuleStatus =3D CapsuleStatus; > + > + // > + // Save Local Cache > + // > + Status =3D WriteNewCapsuleResultVariableCache(&CapsuleResultVariable, > sizeof(CapsuleResultVariable)); > + > + if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) !=3D 0= ) { > + Status =3D WriteNewCapsuleResultVariable(&CapsuleResultVariable, > sizeof(CapsuleResultVariable)); > + } > + return Status; > +} > + > +/** > + Record FMP capsule status variable and to local cache. > + > + @param CapsuleHeader The capsule image header > + @param CapsuleStatus The capsule process stauts > + @param PayloadIndex FMP payload index > + @param ImageHeader FMP image header > + > + @retval EFI_SUCCESS The capsule status variable is recorded. > + @retval EFI_OUT_OF_RESOURCES No resource to record the capsule status = variable. > +**/ > +EFI_STATUS > +RecordFmpCapsuleStatusVariable( > + IN EFI_CAPSULE_HEADER *CapsuleHeader, > + IN EFI_STATUS CapsuleStatus, > + IN UINTN PayloadIndex, > + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader > + ) > +{ > + UINT8 > CapsuleResultVariable[sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER) + > sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP)]; > + EFI_CAPSULE_RESULT_VARIABLE_HEADER *CapsuleResultVariableHeader; > + EFI_CAPSULE_RESULT_VARIABLE_FMP *CapsuleResultVariableFmp; > + EFI_STATUS Status; > + > + CapsuleResultVariableHeader =3D (VOID *)&CapsuleResultVariable[0]; > + CapsuleResultVariableHeader->VariableTotalSize =3D sizeof(CapsuleResul= tVariable); > + CopyGuid(&CapsuleResultVariableHeader->CapsuleGuid, &CapsuleHeader->Ca= psuleGuid); > + ZeroMem(&CapsuleResultVariableHeader->CapsuleProcessed, > sizeof(CapsuleResultVariableHeader->CapsuleProcessed)); > + gRT->GetTime(&CapsuleResultVariableHeader->CapsuleProcessed, NULL); > + CapsuleResultVariableHeader->CapsuleStatus =3D CapsuleStatus; > + > + CapsuleResultVariableFmp =3D (VOID > *)&CapsuleResultVariable[sizeof(EFI_CAPSULE_RESULT_VARIABLE_HEADER)]; > + CapsuleResultVariableFmp->Version =3D 0x1; > + CapsuleResultVariableFmp->PayloadIndex =3D (UINT8)PayloadIndex; > + CapsuleResultVariableFmp->UpdateImageIndex =3D ImageHeader->UpdateImag= eIndex; > + CopyGuid (&CapsuleResultVariableFmp->UpdateImageTypeId, &ImageHeader- > >UpdateImageTypeId); > + > + // > + // Save Local Cache > + // > + Status =3D WriteNewCapsuleResultVariableCache(&CapsuleResultVariable, > sizeof(CapsuleResultVariable)); > + > + if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) !=3D 0= ) { > + Status =3D WriteNewCapsuleResultVariable(&CapsuleResultVariable, > sizeof(CapsuleResultVariable)); > + } > + return Status; > +} > + > +/** > + Initialize CapsuleMax variables. > +**/ > +VOID > +InitCapsuleMaxVariable( > + VOID > + ) > +{ > + EFI_STATUS Status; > + UINTN Size; > + CHAR16 CapsuleMaxStr[sizeof("Capsule####")]; > + EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock; > + > + UnicodeSPrint( > + CapsuleMaxStr, > + sizeof(CapsuleMaxStr), > + L"Capsule%04x", > + PcdGet16(PcdCapsuleMax) > + ); > + > + Size =3D sizeof(L"Capsule####") - sizeof(CHAR16); // no zero terminato= r > + Status =3D gRT->SetVariable( > + L"CapsuleMax", > + &gEfiCapsuleReportGuid, > + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME= _ACCESS, > + Size, > + CapsuleMaxStr > + ); > + if (!EFI_ERROR(Status)) { > + // Lock it per UEFI spec. > + Status =3D gBS->LocateProtocol(&gEdkiiVariableLockProtocolGuid, NULL= , (VOID > **)&VariableLock); > + if (!EFI_ERROR(Status)) { > + Status =3D VariableLock->RequestToLock(VariableLock, L"CapsuleMax"= , > &gEfiCapsuleReportGuid); > + ASSERT_EFI_ERROR(Status); > + } > + } > +} > + > +/** > + Initialize CapsuleLast variables. > +**/ > +VOID > +InitCapsuleLastVariable( > + VOID > + ) > +{ > + EFI_STATUS Status; > + EFI_BOOT_MODE BootMode; > + EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock; > + VOID *CapsuleResult; > + UINTN Size; > + CHAR16 CapsuleLastStr[sizeof("Capsule####")]= ; > + > + BootMode =3D GetBootModeHob(); > + if (BootMode =3D=3D BOOT_ON_FLASH_UPDATE) { > + Status =3D gRT->SetVariable( > + L"CapsuleLast", > + &gEfiCapsuleReportGuid, > + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE= _ACCESS | > EFI_VARIABLE_RUNTIME_ACCESS, > + 0, > + NULL > + ); > + // Do not lock it because it will be updated later. > + } else { > + // > + // Check if OS/APP cleared L"Capsule####" > + // > + ZeroMem(CapsuleLastStr, sizeof(CapsuleLastStr)); > + Size =3D sizeof(L"Capsule####") - sizeof(CHAR16); // no zero termina= tor > + Status =3D gRT->GetVariable( > + L"CapsuleLast", > + &gEfiCapsuleReportGuid, > + NULL, > + &Size, > + CapsuleLastStr > + ); > + if (!EFI_ERROR(Status)) { > + // > + // L"CapsuleLast" is got, check if data is there. > + // > + Status =3D GetVariable2 ( > + CapsuleLastStr, > + &gEfiCapsuleReportGuid, > + (VOID **) &CapsuleResult, > + NULL > + ); > + if (EFI_ERROR(Status)) { > + // > + // If no data, delete L"CapsuleLast" > + // > + Status =3D gRT->SetVariable( > + L"CapsuleLast", > + &gEfiCapsuleReportGuid, > + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSER= VICE_ACCESS | > EFI_VARIABLE_RUNTIME_ACCESS, > + 0, > + NULL > + ); > + } > + } > + > + // Lock it in normal boot path per UEFI spec. > + Status =3D gBS->LocateProtocol(&gEdkiiVariableLockProtocolGuid, NULL= , (VOID > **)&VariableLock); > + if (!EFI_ERROR(Status)) { > + Status =3D VariableLock->RequestToLock(VariableLock, L"CapsuleLast= ", > &gEfiCapsuleReportGuid); > + ASSERT_EFI_ERROR(Status); > + } > + } > +} > + > +/** > + Initialize capsule update variables. > +**/ > +VOID > +InitCapsuleUpdateVariable( > + VOID > + ) > +{ > + EFI_STATUS Status; > + UINTN Index; > + CHAR16 CapsuleVarName[30]; > + CHAR16 *TempVarName; > + > + // > + // Clear all the capsule variables CapsuleUpdateData, CapsuleUpdateDat= a1, > CapsuleUpdateData2... > + // as early as possible which will avoid the next time boot after the = capsule update > + // will still into the capsule loop > + // > + StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CapsuleVarName[= 0]), > EFI_CAPSULE_VARIABLE_NAME); > + TempVarName =3D CapsuleVarName + StrLen (CapsuleVarName); > + Index =3D 0; > + while (TRUE) { > + if (Index > 0) { > + UnicodeValueToString (TempVarName, 0, Index, 0); > + } > + Status =3D gRT->SetVariable ( > + CapsuleVarName, > + &gEfiCapsuleVendorGuid, > + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACC= ESS | > EFI_VARIABLE_BOOTSERVICE_ACCESS, > + 0, > + (VOID *)NULL > + ); > + if (EFI_ERROR (Status)) { > + // > + // There is no capsule variables, quit > + // > + break; > + } > + Index++; > + } > +} > + > +/** > + Initialize capsule related variables. > +**/ > +VOID > +InitCapsuleVariable( > + VOID > + ) > +{ > + InitCapsuleUpdateVariable(); > + InitCapsuleMaxVariable(); > + InitCapsuleLastVariable(); > + // > + // No need to clear L"Capsule####", because OS/APP should refer L"Caps= uleLast" > + // to check status and delete them. > + // > +} > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleRuntime.c > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleRuntime.c > new file mode 100644 > index 0000000..8801439 > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleRuntime.c > @@ -0,0 +1,112 @@ > +/** @file > + Capsule library runtime support. > + > + Copyright (c) 2016, Intel Corporation. All rights reserved.
> + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the = BSD License > + which accompanies this distribution. The full text of the license may= be found at > + http://opensource.org/licenses/bsd-license.php > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR I= MPLIED. > + > +**/ > + > +#include > + > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +extern EFI_SYSTEM_RESOURCE_TABLE *mEsrtTable; > +extern BOOLEAN mIsVirtualAddrConverted; > + > +/** > + Convert EsrtTable physical address to virtual address. > + > + @param[in] Event Event whose notification function is being invok= ed. > + @param[in] Context The pointer to the notification function's conte= xt, which > + is implementation-dependent. > +**/ > +VOID > +EFIAPI > +DxeCapsuleLibVirtualAddressChangeEvent ( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + UINTN Index; > + EFI_CONFIGURATION_TABLE *ConfigEntry; > + > + // > + // Get Esrt table first > + // > + ConfigEntry =3D gST->ConfigurationTable; > + for (Index =3D 0; Index < gST->NumberOfTableEntries; Index++) { > + if (CompareGuid(&gEfiSystemResourceTableGuid, &ConfigEntry->VendorGu= id)) { > + break; > + } > + ConfigEntry++; > + } > + > + // > + // If no Esrt table installed in Configure Table > + // > + if (Index < gST->NumberOfTableEntries) { > + // > + // Search Esrt to check given capsule is qualified > + // > + mEsrtTable =3D (EFI_SYSTEM_RESOURCE_TABLE *) ConfigEntry->VendorTabl= e; > + > + // > + // Update protocol pointer to Esrt Table. > + // > + gRT->ConvertPointer (0x00, (VOID**) &(mEsrtTable)); > + } > + > + mIsVirtualAddrConverted =3D TRUE; > + > +} > + > +/** > + The constructor function hook VirtualAddressChange event to use ESRT t= able as > capsule routing table. > + > + @param ImageHandle The firmware allocated handle for the EFI image. > + @param SystemTable A pointer to the EFI System Table. > + > + @retval EFI_SUCCESS The constructor successfully . > +**/ > +EFI_STATUS > +EFIAPI > +DxeRuntimeCapsuleLibConstructor ( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + EFI_STATUS Status; > + EFI_EVENT Event; > + > + // > + // Make sure we can handle virtual address changes. > + // > + Event =3D NULL; > + Status =3D gBS->CreateEventEx ( > + EVT_NOTIFY_SIGNAL, > + TPL_NOTIFY, > + DxeCapsuleLibVirtualAddressChangeEvent, > + NULL, > + &gEfiEventVirtualAddressChangeGuid, > + &Event > + ); > + ASSERT_EFI_ERROR (Status); > + > + return EFI_SUCCESS; > +} > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.i= nf > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.inf > new file mode 100644 > index 0000000..cd3738e > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.inf > @@ -0,0 +1,83 @@ > +## @file > +# Capsule library instance for DXE_RUNTIME_DRIVER. > +# > +# Capsule library instance for DXE_RUNTIME_DRIVER module types. > +# > +# Copyright (c) 2016, Intel Corporation. All rights reserved.
> +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the= BSD License > +# which accompanies this distribution. The full text of the license ma= y be found at > +# http://opensource.org/licenses/bsd-license.php > +# > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D DxeRuntimeCapsuleLib > + MODULE_UNI_FILE =3D DxeRuntimeCapsuleLib.uni > + FILE_GUID =3D 19BE1E4B-1A9A-44c1-8F12-32DD0470516= A > + MODULE_TYPE =3D DXE_DRIVER > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D CapsuleLib|DXE_RUNTIME_DRIVER > + CONSTRUCTOR =3D DxeCapsuleLibConstructor > + CONSTRUCTOR =3D DxeRuntimeCapsuleLibConstructor > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC > +# > + > +[Sources] > + DxeCapsuleLib.c > + DxeCapsuleProcessLib.c > + DxeCapsuleReportLib.c > + DxeCapsuleRuntime.c > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + MemoryAllocationLib > + DxeServicesTableLib > + UefiBootServicesTableLib > + DevicePathLib > + ReportStatusCodeLib > + PrintLib > + HobLib > + > +[Pcd] > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleMax = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdSystemRebootAfterCapsuleProcessFlag = ## > CONSUMES > + > + gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeSubClassCapsule = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeProcessCapsulesBegi= n ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeProcessCapsulesEnd = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdatingFirmware = ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdateFirmwareSucce= ss ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeUpdateFirmwareFaile= d ## > CONSUMES > + gEfiMdeModulePkgTokenSpaceGuid.PcdCapsuleStatusCodeResettingSystem = ## > CONSUMES > + > +[Protocols] > + gEsrtManagementProtocolGuid ## CONSUMES > + gEfiFirmwareManagementProtocolGuid ## SOMETIMES_CONSUMES > + gEdkiiVariableLockProtocolGuid ## SOMETIMES_CONSUMES > + > +[Guids] > + gEfiFmpCapsuleGuid ## SOMETIMES_CONSUMES ## GUID > + gWindowsUxCapsuleGuid ## SOMETIMES_CONSUMES ## GUID > + gEfiSystemResourceTableGuid ## SOMETIMES_CONSUMES ## GUID > + gEfiCapsuleReportGuid ## CONSUMES ## Variable > + gEfiCapsuleVendorGuid ## CONSUMES ## Variable > + gEfiEndOfDxeEventGroupGuid ## CONSUMES ## Event > + gEfiEventVirtualAddressChangeGuid ## CONSUMES ## Event > + > +[Depex] > + gEfiVariableWriteArchProtocolGuid > diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.u= ni > b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.uni > new file mode 100644 > index 0000000..cd89b13 > --- /dev/null > +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.uni > @@ -0,0 +1,22 @@ > +// /** @file > +// Capsule library instance for DXE_RUNTIME_DRIVER. > +// > +// Capsule library instance for DXE_RUNTIME_DRIVER module types. > +// > +// Copyright (c) 2016, Intel Corporation. All rights reserved.
> +// > +// This program and the accompanying materials > +// are licensed and made available under the terms and conditions of the= BSD License > +// which accompanies this distribution. The full text of the license ma= y be found at > +// http://opensource.org/licenses/bsd-license.php > +// > +// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. > +// > +// **/ > + > + > +#string STR_MODULE_ABSTRACT #language en-US "Capsule Support= Library" > + > +#string STR_MODULE_DESCRIPTION #language en-US "Capsule library= instance for > DXE_RUNTIME_DRIVER module types." > + > -- > 2.7.4.windows.1