From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 9717E81844 for ; Sun, 25 Dec 2016 18:58:22 -0800 (PST) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP; 25 Dec 2016 18:58:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,404,1477983600"; d="scan'208";a="802287603" Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by FMSMGA003.fm.intel.com with ESMTP; 25 Dec 2016 18:58:22 -0800 Received: from fmsmsx123.amr.corp.intel.com (10.18.125.38) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 25 Dec 2016 18:58:22 -0800 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by fmsmsx123.amr.corp.intel.com (10.18.125.38) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 25 Dec 2016 18:58:20 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.88]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.204]) with mapi id 14.03.0248.002; Mon, 26 Dec 2016 10:58:16 +0800 From: "Zeng, Star" To: "Yao, Jiewen" , "edk2-devel@lists.01.org" CC: "Tian, Feng" , "Zhang, Chao B" , "Zeng, Star" Thread-Topic: [PATCH] MdeModulePkg/CapsuleLib: Follow UEFI 22.2.3 to process FMP. Thread-Index: AQHSXbqFddQD5RWWYkudNzvMsjv6xKEZjEJQ Date: Mon, 26 Dec 2016 02:58:15 +0000 Message-ID: <0C09AFA07DD0434D9E2A0C6AEB0483103B7F0F7A@shsmsx102.ccr.corp.intel.com> References: <1482565865-9500-1-git-send-email-jiewen.yao@intel.com> In-Reply-To: <1482565865-9500-1-git-send-email-jiewen.yao@intel.com> 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 Subject: Re: [PATCH] MdeModulePkg/CapsuleLib: Follow UEFI 22.2.3 to process FMP. 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: Mon, 26 Dec 2016 02:58:22 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Star Zeng -----Original Message----- From: Yao, Jiewen=20 Sent: Saturday, December 24, 2016 3:51 PM To: edk2-devel@lists.01.org Cc: Tian, Feng ; Zeng, Star ; Zha= ng, Chao B Subject: [PATCH] MdeModulePkg/CapsuleLib: Follow UEFI 22.2.3 to process FMP= . Previous logic does not follow UEFI 22.2.3 to process FMP strictly. It may cause FMP image not be processed in some corner case. The updated logic follows UEFI 22.2.3. The way to check if a capsule is processed is also simplified. The function - ProcessFmpCapsuleImage() is too big, so that we created sub-functions - StartFmpImage(), DumpAllFmpInfo(), GetFmpHandleBufferByType(), SetFmpImageData(), RecordFmpCapsuleStatus() to improve the readability. The function - ProcessTheseCapsules() is too big, so that we created sub-functions - InitCapsulePtr(), AreAllImagesProcessed(), PopulateCapsuleInConfigurationTable() to improve the readability. Cc: Feng Tian Cc: Star Zeng Cc: Chao Zhang Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jiewen Yao --- MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 883 ++++= +++++++++------- MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c | 333 ++++= ---- MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c | 109 +-- MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLibNull.c | 20 - 4 files changed, 785 insertions(+), 560 deletions(-) diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c b/MdeMod= ulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c index 2bb6ac8..71e05bd 100644 --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c @@ -47,8 +47,6 @@ #include #include =20 -BOOLEAN mAreAllImagesProcessed; - EFI_SYSTEM_RESOURCE_TABLE *mEsrtTable =3D NULL; BOOLEAN mIsVirtualAddrConverted =3D FALSE; BOOLEAN mDxeCapsuleLibEndOfDxe =3D FALSE; @@ -62,23 +60,6 @@ InitCapsuleVariable ( ); =20 /** - Check if this FMP capsule is processed. - - @param[in] CapsuleHeader The capsule image header - @param[in] PayloadIndex FMP payload index - @param[in] 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. =20 @param[in] CapsuleHeader The capsule image header @@ -769,6 +750,520 @@ DumpFmpCapsule ( } =20 /** + Dump all FMP information. +**/ +VOID +DumpAllFmpInfo ( + VOID + ) +{ + EFI_STATUS Status; + EFI_HANDLE *HandleBuffer; + UINTN NumberOfHandles; + EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; + UINTN Index; + UINTN ImageInfoSize; + EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; + UINT32 FmpImageInfoDescriptorVer; + UINT8 FmpImageInfoCount; + UINTN DescriptorSize; + UINT32 PackageVersion; + CHAR16 *PackageVersionName; + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiFirmwareManagementProtocolGuid, + NULL, + &NumberOfHandles, + &HandleBuffer + ); + if (EFI_ERROR(Status)) { + return ; + } + + for (Index =3D 0; Index < NumberOfHandles; Index++) { + Status =3D gBS->HandleProtocol( + HandleBuffer[Index], + &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 AllocateZeroPool (ImageInfoSize); + if (FmpImageInfoBuf =3D=3D NULL) { + continue; + } + + PackageVersionName =3D NULL; + Status =3D Fmp->GetImageInfo ( + Fmp, + &ImageInfoSize, // ImageInfoSize + FmpImageInfoBuf, // ImageInfo + &FmpImageInfoDescriptorVer, // DescriptorVersion + &FmpImageInfoCount, // DescriptorCount + &DescriptorSize, // DescriptorSize + &PackageVersion, // PackageVersion + &PackageVersionName // PackageVersionName + ); + if (EFI_ERROR(Status)) { + FreePool(FmpImageInfoBuf); + continue; + } + + DEBUG((DEBUG_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); + } + + FreePool(FmpImageInfoBuf); + } + + return ; +} + +/** + Get FMP handle by ImageTypeId and HardwareInstance. + + @param[in] UpdateImageTypeId Used to identify device firmware = targeted by this update. + @param[in] UpdateHardwareInstance The HardwareInstance to target wi= th this update. + @param[in,out] NoHandles The number of handles returned in= Buffer. + @param[out] Buffer[out] A pointer to the buffer to return= the requested array of handles. + + @retval EFI_SUCCESS The array of handles was returned in Buff= er, and the number of + handles in Buffer was returned in NoHandl= es. + @retval EFI_NOT_FOUND No handles match the search. + @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store = the matching results. +**/ +EFI_STATUS +GetFmpHandleBufferByType ( + IN EFI_GUID *UpdateImageTypeId, + IN UINT64 UpdateHardwareInstance, + IN OUT UINTN *NoHandles, + OUT EFI_HANDLE **Buffer + ) +{ + EFI_STATUS Status; + EFI_HANDLE *HandleBuffer; + UINTN NumberOfHandles; + EFI_HANDLE *MatchedHandleBuffer; + UINTN MatchedNumberOfHandles; + EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; + UINTN Index; + UINTN ImageInfoSize; + EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; + UINT32 FmpImageInfoDescriptorVer; + UINT8 FmpImageInfoCount; + UINTN DescriptorSize; + UINT32 PackageVersion; + CHAR16 *PackageVersionName; + UINTN Index2; + EFI_FIRMWARE_IMAGE_DESCRIPTOR *TempFmpImageInfo; + + *NoHandles =3D 0; + *Buffer =3D NULL; + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiFirmwareManagementProtocolGuid, + NULL, + &NumberOfHandles, + &HandleBuffer + ); + if (EFI_ERROR(Status)) { + return Status; + } + + MatchedNumberOfHandles =3D 0; + MatchedHandleBuffer =3D AllocateZeroPool (sizeof(EFI_HANDLE) * NumberOfH= andles); + if (MatchedHandleBuffer =3D=3D NULL) { + FreePool (HandleBuffer); + return EFI_OUT_OF_RESOURCES; + } + + for (Index =3D 0; Index < NumberOfHandles; Index++) { + Status =3D gBS->HandleProtocol( + HandleBuffer[Index], + &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 AllocateZeroPool (ImageInfoSize); + if (FmpImageInfoBuf =3D=3D NULL) { + continue; + } + + PackageVersionName =3D NULL; + Status =3D Fmp->GetImageInfo ( + Fmp, + &ImageInfoSize, // ImageInfoSize + FmpImageInfoBuf, // ImageInfo + &FmpImageInfoDescriptorVer, // DescriptorVersion + &FmpImageInfoCount, // DescriptorCount + &DescriptorSize, // DescriptorSize + &PackageVersion, // PackageVersion + &PackageVersionName // PackageVersionName + ); + if (EFI_ERROR(Status)) { + FreePool(FmpImageInfoBuf); + continue; + } + + if (PackageVersionName !=3D NULL) { + FreePool(PackageVersionName); + } + + TempFmpImageInfo =3D FmpImageInfoBuf; + for (Index2 =3D 0; Index2 < FmpImageInfoCount; Index2++) { + // + // Check if this FMP instance matches + // + if (CompareGuid(UpdateImageTypeId, &TempFmpImageInfo->ImageTypeId)) = { + if ((UpdateHardwareInstance =3D=3D 0) || + ((FmpImageInfoDescriptorVer >=3D EFI_FIRMWARE_IMAGE_DESCRIPTOR= _VERSION) && + (UpdateHardwareInstance =3D=3D TempFmpImageInfo->HardwareInst= ance))) { + MatchedHandleBuffer[MatchedNumberOfHandles] =3D HandleBuffer[Ind= ex]; + MatchedNumberOfHandles++; + break; + } + } + TempFmpImageInfo =3D (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)((UINT8 *)Temp= FmpImageInfo + DescriptorSize); + } + FreePool(FmpImageInfoBuf); + } + + if (MatchedNumberOfHandles =3D=3D 0) { + return EFI_NOT_FOUND; + } + + *NoHandles =3D MatchedNumberOfHandles; + *Buffer =3D MatchedHandleBuffer; + + return EFI_SUCCESS; +} + +/** + Return FmpImageInfoDescriptorVer by an FMP handle. + + @param[in] Handle A FMP handle. + + @return FmpImageInfoDescriptorVer associated with the FMP. +**/ +UINT32 +GetFmpImageInfoDescriptorVer ( + IN EFI_HANDLE Handle + ) +{ + EFI_STATUS Status; + EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; + UINTN ImageInfoSize; + EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; + UINT32 FmpImageInfoDescriptorVer; + UINT8 FmpImageInfoCount; + UINTN DescriptorSize; + UINT32 PackageVersion; + CHAR16 *PackageVersionName; + + Status =3D gBS->HandleProtocol( + Handle, + &gEfiFirmwareManagementProtocolGuid, + (VOID **)&Fmp + ); + if (EFI_ERROR(Status)) { + return 0; + } + + ImageInfoSize =3D 0; + Status =3D Fmp->GetImageInfo ( + Fmp, + &ImageInfoSize, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + ); + if (Status !=3D EFI_BUFFER_TOO_SMALL) { + return 0; + } + + FmpImageInfoBuf =3D AllocateZeroPool (ImageInfoSize); + if (FmpImageInfoBuf =3D=3D NULL) { + return 0; + } + + PackageVersionName =3D NULL; + Status =3D Fmp->GetImageInfo ( + Fmp, + &ImageInfoSize, // ImageInfoSize + FmpImageInfoBuf, // ImageInfo + &FmpImageInfoDescriptorVer, // DescriptorVersion + &FmpImageInfoCount, // DescriptorCount + &DescriptorSize, // DescriptorSize + &PackageVersion, // PackageVersion + &PackageVersionName // PackageVersionName + ); + if (EFI_ERROR(Status)) { + FreePool(FmpImageInfoBuf); + return 0; + } + return FmpImageInfoDescriptorVer; +} + +/** + Set FMP image data. + + @param[in] Handle A FMP handle. + @param[in] ImageHeader The payload image header. + @param[in] PayloadIndex The index of the payload. + + @return The status of FMP->SetImage. +**/ +EFI_STATUS +SetFmpImageData ( + IN EFI_HANDLE Handle, + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader, + IN UINTN PayloadIndex + ) +{ + EFI_STATUS Status; + EFI_FIRMWARE_MANAGEMENT_PROTOCOL *Fmp; + UINT8 *Image; + VOID *VendorCode; + CHAR16 *AbortReason; + + Status =3D gBS->HandleProtocol( + Handle, + &gEfiFirmwareManagementProtocolGuid, + (VOID **)&Fmp + ); + if (EFI_ERROR(Status)) { + return Status; + } + + if (ImageHeader->Version >=3D EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEAD= ER_INIT_VERSION) { + Image =3D (UINT8 *)(ImageHeader + 1); + } else { + // + // If the EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER is version 1, + // Header should exclude UpdateHardwareInstance field + // + Image =3D (UINT8 *)ImageHeader + OFFSET_OF(EFI_FIRMWARE_MANAGEMENT_CAP= SULE_IMAGE_HEADER, UpdateHardwareInstance); + } + + if (ImageHeader->UpdateVendorCodeSize =3D=3D 0) { + VendorCode =3D NULL; + } else { + VendorCode =3D Image + ImageHeader->UpdateImageSize; + } + AbortReason =3D NULL; + DEBUG((DEBUG_INFO, "Fmp->SetImage ...\n")); + DEBUG((DEBUG_INFO, "ImageTypeId - %g, ", &ImageHeader->UpdateImageTypeId= )); + DEBUG((DEBUG_INFO, "PayloadIndex - 0x%x, ", PayloadIndex)); + DEBUG((DEBUG_INFO, "ImageIndex - 0x%x ", ImageHeader->UpdateImageIndex))= ; + if (ImageHeader->Version >=3D EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEAD= ER_INIT_VERSION) { + DEBUG((DEBUG_INFO, "(UpdateHardwareInstance - 0x%x)", ImageHeader->Upd= ateHardwareInstance)); + } + DEBUG((DEBUG_INFO, "\n")); + Status =3D Fmp->SetImage( + Fmp, + ImageHeader->UpdateImageIndex, // ImageIndex + Image, // Image + ImageHeader->UpdateImageSize, // ImageSize + VendorCode, // VendorCode + Update_Image_Progress, // Progress + &AbortReason // AbortReason + ); + DEBUG((DEBUG_INFO, "Fmp->SetImage - %r\n", Status)); + if (AbortReason !=3D NULL) { + DEBUG ((DEBUG_ERROR, "%s\n", AbortReason)); + FreePool(AbortReason); + } + + return Status; +} + +/** + Start a UEFI image in the FMP payload. + + @param[in] ImageBuffer A pointer to the memory location containing a = copy of the image to be loaded.. + @param[in] ImageSize The size in bytes of ImageBuffer. + + @return The status of gBS->LoadImage and gBS->StartImage. +**/ +EFI_STATUS +StartFmpImage ( + IN VOID *ImageBuffer, + IN UINTN ImageSize + ) +{ + MEMMAP_DEVICE_PATH MemMapNode; + EFI_STATUS Status; + EFI_HANDLE ImageHandle; + EFI_DEVICE_PATH_PROTOCOL *DriverDevicePath; + UINTN ExitDataSize; + + 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)ImageBuffer; + MemMapNode.EndingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)((UINT8 *)Im= ageBuffer + ImageSize - 1); + + DriverDevicePath =3D AppendDevicePathNode (NULL, &MemMapNode.Header); + if (DriverDevicePath =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage ...\n")); + Status =3D gBS->LoadImage( + FALSE, + gImageHandle, + DriverDevicePath, + ImageBuffer, + ImageSize, + &ImageHandle + ); + DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage - %r\n", Status)); + if (EFI_ERROR(Status)) { + FreePool(DriverDevicePath); + return Status; + } + + DEBUG((DEBUG_INFO, "FmpCapsule: StartImage ...\n")); + Status =3D gBS->StartImage( + ImageHandle, + &ExitDataSize, + NULL + ); + DEBUG((DEBUG_INFO, "FmpCapsule: StartImage - %r\n", Status)); + if (EFI_ERROR(Status)) { + DEBUG ((DEBUG_ERROR, "Driver Return Status =3D %r\n", Status)); + } + + FreePool(DriverDevicePath); + return Status; +} + +/** + Record FMP capsule status. + + @param[in] Handle A FMP handle. + @param[in] CapsuleHeader The capsule image header + @param[in] CapsuleStatus The capsule process stauts + @param[in] PayloadIndex FMP payload index + @param[in] ImageHeader FMP image header +**/ +VOID +RecordFmpCapsuleStatus ( + IN EFI_HANDLE Handle, OPTIONAL + IN EFI_CAPSULE_HEADER *CapsuleHeader, + IN EFI_STATUS CapsuleStatus, + IN UINTN PayloadIndex, + IN EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *ImageHeader + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *FmpDevicePath; + UINT32 FmpImageInfoDescriptorVer; + EFI_STATUS StatusEsrt; + ESRT_MANAGEMENT_PROTOCOL *EsrtProtocol; + EFI_SYSTEM_RESOURCE_ENTRY EsrtEntry; + + FmpDevicePath =3D NULL; + if (Handle !=3D NULL) { + gBS->HandleProtocol( + Handle, + &gEfiDevicePathProtocolGuid, + (VOID **)&FmpDevicePath + ); + } + + RecordFmpCapsuleStatusVariable ( + CapsuleHeader, + CapsuleStatus, + PayloadIndex, + ImageHeader, + FmpDevicePath + ); + + // + // Update corresponding ESRT entry LastAttemp Status + // + Status =3D gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VOID= **)&EsrtProtocol); + if (EFI_ERROR (Status)) { + return ; + } + + if (Handle =3D=3D NULL) { + return ; + } + + // + // Update EsrtEntry For V1, V2 FMP instance. + // V3 FMP ESRT cache will be synced up through EsrtSyncFmp interface + // + FmpImageInfoDescriptorVer =3D GetFmpImageInfoDescriptorVer (Handle); + if (FmpImageInfoDescriptorVer < EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION) { + StatusEsrt =3D EsrtProtocol->GetEsrtEntry(&ImageHeader->UpdateImageTyp= eId, &EsrtEntry); + if (!EFI_ERROR(StatusEsrt)){ + if (!EFI_ERROR(CapsuleStatus)) { + EsrtEntry.LastAttemptStatus =3D LAST_ATTEMPT_STATUS_SUCCESS; + } else { + EsrtEntry.LastAttemptStatus =3D LAST_ATTEMPT_STATUS_ERROR_UNSUCCES= SFUL; + } + EsrtEntry.LastAttemptVersion =3D 0; + EsrtProtocol->UpdateEsrtEntry(&EsrtEntry); + } + } +} + +/** Process Firmware management protocol data capsule. =20 This function assumes the caller validated the capsule by using @@ -779,65 +1274,38 @@ DumpFmpCapsule ( This function need support nested FMP capsule. =20 @param[in] CapsuleHeader Points to a capsule header. - @param[out] AreAllImagesProcessed If all the FMP images in the capsule = are processed. =20 @retval EFI_SUCESS Process Capsule Image successfully. @retval EFI_UNSUPPORTED Capsule image is not supported by the firm= ware. @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted. @retval EFI_OUT_OF_RESOURCES Not enough memory. + @retval EFI_NOT_READY No FMP protocol to handle this FMP capsule= . **/ EFI_STATUS ProcessFmpCapsuleImage ( - IN EFI_CAPSULE_HEADER *CapsuleHeader, - OUT BOOLEAN *AreAllImagesProcessed + IN EFI_CAPSULE_HEADER *CapsuleHeader ) { 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 FmpImageInfoDescriptorVer; - UINTN ImageInfoSize; - UINT32 PackageVersion; - CHAR16 *PackageVersionName; - CHAR16 *AbortReason; - EFI_FIRMWARE_IMAGE_DESCRIPTOR *FmpImageInfoBuf; - EFI_FIRMWARE_IMAGE_DESCRIPTOR *TempFmpImageInfo; UINTN DriverLen; - UINTN Index1; + UINT64 UpdateHardwareInstance; UINTN Index2; - MEMMAP_DEVICE_PATH MemMapNode; - EFI_DEVICE_PATH_PROTOCOL *DriverDevicePath; - EFI_DEVICE_PATH_PROTOCOL *FmpDevicePath; - ESRT_MANAGEMENT_PROTOCOL *EsrtProtocol; - EFI_SYSTEM_RESOURCE_ENTRY EsrtEntry; - VOID *VendorCode; + BOOLEAN NotReady; + BOOLEAN Abort; =20 if (!IsFmpCapsuleGuid(&CapsuleHeader->CapsuleGuid)) { - return ProcessFmpCapsuleImage ((EFI_CAPSULE_HEADER *)((UINTN)CapsuleHe= ader + CapsuleHeader->HeaderSize), AreAllImagesProcessed); + return ProcessFmpCapsuleImage ((EFI_CAPSULE_HEADER *)((UINTN)CapsuleHe= ader + CapsuleHeader->HeaderSize)); } =20 - 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; + NotReady =3D FALSE; + Abort =3D FALSE; =20 DumpFmpCapsule(CapsuleHeader); =20 @@ -854,35 +1322,15 @@ ProcessFmpCapsuleImage ( // capsule in which driver count and payload count are both zero is not = processed. // if (ItemNum =3D=3D 0) { - *AreAllImagesProcessed =3D TRUE; return EFI_SUCCESS; } =20 // - // Update corresponding ESRT entry LastAttemp Status - // - Status =3D gBS->LocateProtocol(&gEsrtManagementProtocolGuid, NULL, (VOID= **)&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)CapsuleHeade= r; - MemMapNode.EndingAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)((UINT8 *)Ca= psuleHeader + 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 (UINTN= )FmpCapsuleHeader->EmbeddedDriverCount - 1) { + if ((FmpCapsuleHeader->PayloadItemCount =3D=3D 0) && + (Index =3D=3D (UINTN)FmpCapsuleHeader->EmbeddedDriverCount - 1)) { // // When driver is last element in the ItemOffsetList array, the driv= er size is calculated by reference CapsuleImageSize in EFI_CAPSULE_HEADER // @@ -891,32 +1339,13 @@ ProcessFmpCapsuleImage ( DriverLen =3D (UINTN)ItemOffsetList[Index + 1] - (UINTN)ItemOffsetLi= st[Index]; } =20 - DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage ...\n")); - Status =3D gBS->LoadImage( - FALSE, - gImageHandle, - DriverDevicePath, - (UINT8 *)FmpCapsuleHeader + ItemOffsetList[Index], - DriverLen, - &ImageHandle - ); - DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage - %r\n", Status)); - if (EFI_ERROR(Status)) { - StatusRet =3D Status; - goto EXIT; - } - - DEBUG((DEBUG_INFO, "FmpCapsule: StartImage ...\n")); - Status =3D gBS->StartImage( - ImageHandle, - &ExitDataSize, - NULL - ); - DEBUG((DEBUG_INFO, "FmpCapsule: StartImage - %r\n", Status)); + Status =3D StartFmpImage ( + (UINT8 *)FmpCapsuleHeader + ItemOffsetList[Index], + DriverLen + ); if (EFI_ERROR(Status)) { DEBUG ((DEBUG_ERROR, "Driver Return Status =3D %r\n", Status)); - StatusRet =3D Status; - goto EXIT; + return Status; } } =20 @@ -925,207 +1354,80 @@ ProcessFmpCapsuleImage ( // DEBUG((DEBUG_INFO, "FmpCapsule: route payload to right FMP instance ...\= n")); =20 - 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; - } - - FmpDevicePath =3D NULL; - gBS->HandleProtocol( - HandleBuffer[Index1], - &gEfiDevicePathProtocolGuid, - (VOID **)&FmpDevicePath - ); + DumpAllFmpInfo (); =20 - ImageInfoSize =3D 0; - Status =3D Fmp->GetImageInfo ( - Fmp, - &ImageInfoSize, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL - ); - if (Status !=3D EFI_BUFFER_TOO_SMALL) { - continue; - } + // + // Check all the payload entry in capsule payload list + // + for (Index =3D FmpCapsuleHeader->EmbeddedDriverCount; Index < ItemNum; I= ndex++) { + ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *)((UIN= T8 *)FmpCapsuleHeader + ItemOffsetList[Index]); =20 - FmpImageInfoBuf =3D NULL; - FmpImageInfoBuf =3D AllocateZeroPool (ImageInfoSize); - if (FmpImageInfoBuf =3D=3D NULL) { - StatusRet =3D EFI_OUT_OF_RESOURCES; - goto EXIT; - } + UpdateHardwareInstance =3D 0; + if (ImageHeader->Version >=3D EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HE= ADER_INIT_VERSION) { + UpdateHardwareInstance =3D ImageHeader->UpdateHardwareInstance; + } =20 - PackageVersionName =3D NULL; - Status =3D Fmp->GetImageInfo ( - Fmp, - &ImageInfoSize, // ImageInfoSize - FmpImageInfoBuf, // ImageInfo - &FmpImageInfoDescriptorVer, // DescriptorVersion - &FmpImageInfoCount, // DescriptorCount - &DescriptorSize, // DescriptorSize - &PackageVersion, // PackageVersion - &PackageVersionName // PackageVersionName - ); + Status =3D GetFmpHandleBufferByType ( + &ImageHeader->UpdateImageTypeId, + UpdateHardwareInstance, + &NumberOfHandles, + &HandleBuffer + ); + if (EFI_ERROR(Status)) { + NotReady =3D TRUE; + RecordFmpCapsuleStatus ( + NULL, + CapsuleHeader, + EFI_NOT_READY, + Index - FmpCapsuleHeader->EmbeddedDriverCount, + ImageHeader + ); + continue; + } =20 - // - // If FMP GetInformation interface failed, skip this resource - // - if (EFI_ERROR(Status)) { - FreePool(FmpImageInfoBuf); + for (Index2 =3D 0; Index2 < NumberOfHandles; Index2++) { + if (Abort) { + RecordFmpCapsuleStatus ( + HandleBuffer[Index2], + CapsuleHeader, + EFI_ABORTED, + Index - FmpCapsuleHeader->EmbeddedDriverCount, + ImageHeader + ); continue; } =20 - DEBUG((DEBUG_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); + Status =3D SetFmpImageData ( + HandleBuffer[Index2], + ImageHeader, + Index - FmpCapsuleHeader->EmbeddedDriverCount + ); + if (Status !=3D EFI_SUCCESS) { + Abort =3D TRUE; } =20 - 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 < Item= Num; Index++) { - ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *= )((UINT8 *)FmpCapsuleHeader + ItemOffsetList[Index]); - - if (IsFmpCapsuleProcessed(CapsuleHeader, Index - FmpCapsuleHeade= r->EmbeddedDriverCount, ImageHeader)) { - DEBUG((DEBUG_INFO, "FMP Capsule already processed (%g):", Caps= uleHeader)); - DEBUG((DEBUG_INFO, "ImageTypeId - %g, ", &ImageHeader->UpdateI= mageTypeId)); - DEBUG((DEBUG_INFO, "PayloadIndex - 0x%x, ImageIndex - 0x%x\n",= Index - FmpCapsuleHeader->EmbeddedDriverCount, ImageHeader->UpdateImageInd= ex)); - continue; - } - - if (CompareGuid(&ImageHeader->UpdateImageTypeId, &TempFmpImageIn= fo->ImageTypeId)) { - 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 ca= se - // 1. FMP Image info Version < 3 - // 2. HardwareInstance doesn't match - // - if (FmpImageInfoDescriptorVer < EFI_FIRMWARE_IMAGE_DESCRIP= TOR_VERSION || - ImageHeader->UpdateHardwareInstance !=3D TempFmpImageIn= fo->HardwareInstance) { - continue; - } - } - Image =3D (UINT8 *)(ImageHeader + 1); - } else { - // - // If the EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER is ve= rsion 1, only match ImageTypeId. - // Header should exclude UpdateHardwareInstance field - // - Image =3D (UINT8 *)ImageHeader + OFFSET_OF(EFI_FIRMWARE_MANA= GEMENT_CAPSULE_IMAGE_HEADER, UpdateHardwareInstance); - } - - if (ImageHeader->UpdateVendorCodeSize =3D=3D 0) { - VendorCode =3D NULL; - } else { - VendorCode =3D Image + ImageHeader->UpdateImageSize; - } - DEBUG((DEBUG_INFO, "Fmp->SetImage ...\n")); - Status =3D Fmp->SetImage( - Fmp, - ImageHeader->UpdateImageIndex, // Ima= geIndex - Image, // Ima= ge - ImageHeader->UpdateImageSize, // Ima= geSize - VendorCode, = // VendorCode - Update_Image_Progress, // Pro= gress - &AbortReason // Abo= rtReason - ); - DEBUG((DEBUG_INFO, "Fmp->SetImage - %r\n", Status)); - if (AbortReason !=3D NULL) { - DEBUG ((DEBUG_ERROR, "%s\n", AbortReason)); - FreePool(AbortReason); - } - RecordFmpCapsuleStatusVariable( - CapsuleHeader, // CapsuleGui= d - Status, // CapsuleSta= tus - Index - FmpCapsuleHeader->EmbeddedDriverCount, // PayloadInd= ex - ImageHeader, // ImageHeade= r - FmpDevicePath // FmpDeviceP= ath - ); - if (StatusRet !=3D EFI_SUCCESS) { - StatusRet =3D Status; - } - // - // Update EsrtEntry For V1, V2 FMP instance. V3 FMP ESRT cache= will be synced up through EsrtSyncFmp interface - // - if (FmpImageInfoDescriptorVer < EFI_FIRMWARE_IMAGE_DESCRIPTOR_= VERSION && EsrtProtocol !=3D NULL) { - StatusEsrt =3D EsrtProtocol->GetEsrtEntry(&TempFmpImageInfo= ->ImageTypeId, &EsrtEntry); - if (!EFI_ERROR(StatusEsrt)){ - if (!EFI_ERROR(Status)) { - EsrtEntry.LastAttemptStatus =3D LAST_ATTEMPT_STATUS_SUC= CESS; - } else { - EsrtEntry.LastAttemptStatus =3D LAST_ATTEMPT_STATUS_ERR= OR_UNSUCCESSFUL; - } - EsrtEntry.LastAttemptVersion =3D 0; - EsrtProtocol->UpdateEsrtEntry(&EsrtEntry); - } - } - } - } - // - // Use DescriptorSize to move ImageInfo Pointer to stay compatible= with different ImageInfo version - // - TempFmpImageInfo =3D (EFI_FIRMWARE_IMAGE_DESCRIPTOR *)((UINT8 *)Te= mpFmpImageInfo + DescriptorSize); - } - FreePool(FmpImageInfoBuf); + RecordFmpCapsuleStatus ( + HandleBuffer[Index2], + CapsuleHeader, + Status, + Index - FmpCapsuleHeader->EmbeddedDriverCount, + ImageHeader + ); } - } - - // - // final check for AreAllImagesProcessed - // - *AreAllImagesProcessed =3D TRUE; - for (Index =3D FmpCapsuleHeader->EmbeddedDriverCount; Index < ItemNum; I= ndex++) { - ImageHeader =3D (EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER *)((UINT= 8 *)FmpCapsuleHeader + ItemOffsetList[Index]); - - if (!IsFmpCapsuleProcessed(CapsuleHeader, Index - FmpCapsuleHeader->Em= beddedDriverCount, ImageHeader)) { - *AreAllImagesProcessed =3D FALSE; - break; + if (HandleBuffer !=3D NULL) { + FreePool(HandleBuffer); } } =20 -EXIT: - - if (HandleBuffer !=3D NULL) { - FreePool(HandleBuffer); - } - - if (DriverDevicePath !=3D NULL) { - FreePool(DriverDevicePath); + if (NotReady) { + return EFI_NOT_READY; } =20 - return StatusRet; + // + // always return SUCCESS to indicate this capsule is processed. + // The status of SetImage is recorded in capsule result variable. + // + return EFI_SUCCESS; } =20 /** @@ -1278,9 +1580,9 @@ ProcessCapsuleImage ( ) { EFI_STATUS Status; - BOOLEAN AreAllImagesProcessed; =20 if (SupportCapsuleImage (CapsuleHeader) !=3D EFI_SUCCESS) { + RecordCapsuleStatusVariable(CapsuleHeader, EFI_UNSUPPORTED); return EFI_UNSUPPORTED; } =20 @@ -1303,6 +1605,7 @@ ProcessCapsuleImage ( Status =3D ValidateFmpCapsule(CapsuleHeader, NULL); DEBUG((DEBUG_INFO, "ValidateFmpCapsule - %r\n", Status)); if (EFI_ERROR(Status)) { + RecordCapsuleStatusVariable(CapsuleHeader, Status); return Status; } =20 @@ -1310,13 +1613,9 @@ ProcessCapsuleImage ( // Press EFI FMP Capsule // DEBUG((DEBUG_INFO, "ProcessFmpCapsuleImage ...\n")); - Status =3D ProcessFmpCapsuleImage(CapsuleHeader, &AreAllImagesProcesse= d); + Status =3D ProcessFmpCapsuleImage(CapsuleHeader); DEBUG((DEBUG_INFO, "ProcessFmpCapsuleImage - %r\n", Status)); =20 - if (!AreAllImagesProcessed) { - mAreAllImagesProcessed =3D FALSE; - } - return Status; } =20 diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c b= /MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c index 62257a4..ba3ff90 100644 --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleProcessLib.c @@ -95,60 +95,23 @@ IsValidCapsuleHeader ( ); =20 extern BOOLEAN mDxeCapsuleLibEndOfDxe; -extern BOOLEAN mAreAllImagesProcessed; BOOLEAN mNeedReset; =20 -/** - - 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[in] NeedBlockDriver TRUE: Need skip the FMP capsules wit= h non zero EmbeddedDriverCount. - FALSE: No need to skip any FMP capsu= les. - - @retval EFI_SUCCESS There is no error when processing capsul= es. - @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules. +VOID **mCapsulePtr; +EFI_STATUS *mCapsuleStatusArray; +UINT32 mCapsuleTotalNumber; =20 +/** + This function initializes the mCapsulePtr, mCapsuleStatusArray and mCaps= uleTotalNumber. **/ -EFI_STATUS -ProcessTheseCapsules ( - IN BOOLEAN NeedBlockDriver +VOID +InitCapsulePtr ( + VOID ) { - 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, (EFI_SOFTWARE | PcdGet32(PcdStatus= CodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeProcessCapsulesBegin)))= ; + UINTN Index; =20 - 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 // @@ -157,61 +120,108 @@ ProcessTheseCapsules ( if (!IsValidCapsuleHeader((VOID *)(UINTN)HobPointer.Capsule->BaseAddre= ss, HobPointer.Capsule->Length)) { HobPointer.Header->HobType =3D EFI_HOB_TYPE_UNUSED; // Mark this hob= as invalid } else { - CapsuleTotalNumber++; + mCapsuleTotalNumber++; } HobPointer.Raw =3D GET_NEXT_HOB (HobPointer); } =20 - if (CapsuleTotalNumber =3D=3D 0) { - // - // We didn't find a hob, so had no errors. - // - DEBUG ((DEBUG_ERROR, "We can not find capsule data in capsule update b= oot mode.\n")); - Status =3D EFI_SUCCESS; - goto Done; + DEBUG ((DEBUG_INFO, "mCapsuleTotalNumber - 0x%x\n", mCapsuleTotalNumber)= ); + + if (mCapsuleTotalNumber =3D=3D 0) { + return ; } =20 // // Init temp Capsule Data table. // - CapsulePtr =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * Capsu= leTotalNumber); - ASSERT (CapsulePtr !=3D NULL); - if (CapsulePtr =3D=3D NULL) { - Status =3D EFI_OUT_OF_RESOURCES; - goto Done; - } - CapsulePtrCache =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * Capsu= leTotalNumber); - ASSERT (CapsulePtrCache !=3D NULL); - if (CapsulePtrCache =3D=3D NULL) { - Status =3D EFI_OUT_OF_RESOURCES; - goto Done; + mCapsulePtr =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * mCap= suleTotalNumber); + if (mCapsulePtr =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Allocate mCapsulePtr fail!\n")); + mCapsuleTotalNumber =3D 0; + return ; } - 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_STAT= US) * CapsuleTotalNumber); - ASSERT (CapsuleStatusArray !=3D NULL); - if (CapsuleStatusArray =3D=3D NULL) { - Status =3D EFI_OUT_OF_RESOURCES; - goto Done; + mCapsuleStatusArray =3D (EFI_STATUS *) AllocateZeroPool (sizeof (EFI_STA= TUS) * mCapsuleTotalNumber); + if (mCapsuleStatusArray =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Allocate mCapsuleStatusArray fail!\n")); + FreePool (mCapsulePtr); + mCapsulePtr =3D NULL; + mCapsuleTotalNumber =3D 0; + return ; } + SetMemN (mCapsuleStatusArray, sizeof (EFI_STATUS) * mCapsuleTotalNumber,= EFI_NOT_READY); =20 // // Find all capsule images from hob // HobPointer.Raw =3D GetHobList (); + Index =3D 0; while ((HobPointer.Raw =3D GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPoi= nter.Raw)) !=3D NULL) { - CapsulePtr [CapsuleNumber++] =3D (VOID *) (UINTN) HobPointer.Capsule->= BaseAddress; + mCapsulePtr [Index++] =3D (VOID *) (UINTN) HobPointer.Capsule->BaseAdd= ress; HobPointer.Raw =3D GET_NEXT_HOB (HobPointer); } +} =20 - // - // Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TAB= LE, install - // capsuleTable to configure table with EFI_CAPSULE_GUID - // +/** + This function returns if all capsule images are processed. + + @retval TRUE All capsule images are processed. + @retval FALSE Not all capsule images are processed. +**/ +BOOLEAN +AreAllImagesProcessed ( + VOID + ) +{ + UINTN Index; + + for (Index =3D 0; Index < mCapsuleTotalNumber; Index++) { + if (mCapsuleStatusArray[Index] =3D=3D EFI_NOT_READY) { + return FALSE; + } + } + + return TRUE; +} + +/** + This function populates capsule in the configuration table. +**/ +VOID +PopulateCapsuleInConfigurationTable ( + VOID + ) +{ + VOID **CapsulePtrCache; + EFI_GUID *CapsuleGuidCache; + EFI_CAPSULE_HEADER *CapsuleHeader; + EFI_CAPSULE_TABLE *CapsuleTable; + UINT32 CacheIndex; + UINT32 CacheNumber; + UINT32 CapsuleNumber; + UINTN Index; + UINTN Size; + EFI_STATUS Status; + + if (mCapsuleTotalNumber =3D=3D 0) { + return ; + } + + CapsulePtrCache =3D NULL; + CapsuleGuidCache =3D NULL; + CacheIndex =3D 0; + CacheNumber =3D 0; + + CapsulePtrCache =3D (VOID **) AllocateZeroPool (sizeof (VOID *) * mCaps= uleTotalNumber); + if (CapsulePtrCache =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Allocate CapsulePtrCache fail!\n")); + return ; + } + CapsuleGuidCache =3D (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * = mCapsuleTotalNumber); + if (CapsuleGuidCache =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "Allocate CapsuleGuidCache fail!\n")); + FreePool (CapsulePtrCache); + return ; + } =20 // // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used= for operating @@ -223,9 +233,8 @@ ProcessTheseCapsules ( // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids = 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]; + for (Index =3D 0; Index < mCapsuleTotalNumber; Index++) { + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) mCapsulePtr [Index]; if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) !=3D = 0) { // // For each capsule, we compare it with known CapsuleGuid in the Cac= heArray. @@ -254,38 +263,90 @@ ProcessTheseCapsules ( // will look in EFI System Table and search for the capsule's Guid and a= ssociated // pointer to retrieve the data. // - CacheIndex =3D 0; - while (CacheIndex < CacheNumber) { + for (CacheIndex =3D 0; CacheIndex < CacheNumber; CacheIndex++) { CapsuleNumber =3D 0; - for (Index =3D 0; Index < CapsuleTotalNumber; Index++) { - CapsuleHeader =3D (EFI_CAPSULE_HEADER*) CapsulePtr [Index]; + for (Index =3D 0; Index < mCapsuleTotalNumber; Index++) { + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) mCapsulePtr [Index]; if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != =3D 0) { if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->Ca= psuleGuid)) { // // Cache Caspuleheader to the array, this array is uniqued with = certain CapsuleGuid. // CapsulePtrCache[CapsuleNumber++] =3D (VOID*)CapsuleHeader; - // - // When a Capsule is listed in CapsulePtrCache, it will be repor= ted 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(VO= ID*); CapsuleTable =3D AllocateRuntimePool (Size); - ASSERT (CapsuleTable !=3D NULL); if (CapsuleTable =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; + DEBUG ((DEBUG_ERROR, "Allocate CapsuleTable (%g) fail!\n", &Capsul= eGuidCache[CacheIndex])); + continue; } CapsuleTable->CapsuleArrayNumber =3D CapsuleNumber; CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber= * sizeof(VOID*)); Status =3D gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIn= dex], (VOID*)CapsuleTable); - ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "InstallConfigurationTable (%g) fail!\n", &Ca= psuleGuidCache[CacheIndex])); + } } - CacheIndex++; + } + + FreePool(CapsuleGuidCache); + FreePool(CapsulePtrCache); +} + +/** + + 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[in] FirstRound TRUE: First round. Need skip the FMP cap= sules with non zero EmbeddedDriverCount. + FALSE: Process rest FMP capsules. + + @retval EFI_SUCCESS There is no error when processing capsul= es. + @retval EFI_OUT_OF_RESOURCES No enough resource to process capsules. + +**/ +EFI_STATUS +ProcessTheseCapsules ( + IN BOOLEAN FirstRound + ) +{ + EFI_STATUS Status; + EFI_CAPSULE_HEADER *CapsuleHeader; + UINT32 Index; + BOOLEAN DisplayCapsuleExist; + ESRT_MANAGEMENT_PROTOCOL *EsrtManagement; + UINT16 EmbeddedDriverCount; + + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatus= CodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeProcessCapsulesBegin)))= ; + + if (FirstRound) { + InitCapsulePtr (); + } + + if (mCapsuleTotalNumber =3D=3D 0) { + // + // We didn't find a hob, so had no errors. + // + DEBUG ((DEBUG_ERROR, "We can not find capsule data in capsule update b= oot mode.\n")); + return EFI_SUCCESS; + } + + if (AreAllImagesProcessed ()) { + return EFI_SUCCESS; + } + + // + // Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TAB= LE, install + // capsuleTable to configure table with EFI_CAPSULE_GUID + // + if (FirstRound) { + PopulateCapsuleInConfigurationTable (); } =20 REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(PcdStatus= CodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdatingFirmware))); @@ -293,15 +354,16 @@ ProcessTheseCapsules ( // // 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)) = { + DisplayCapsuleExist =3D FALSE; + for (Index =3D 0; Index < mCapsuleTotalNumber; Index++) { + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) mCapsulePtr [Index]; + if (CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid))= { DEBUG ((DEBUG_INFO, "ProcessCapsuleImage (Ux) - 0x%x\n", CapsuleHead= er)); DisplayCapsuleExist =3D TRUE; DEBUG ((DEBUG_INFO, "Display logo capsule is found.\n")); Status =3D ProcessCapsuleImage (CapsuleHeader); + mCapsuleStatusArray [Index] =3D EFI_SUCCESS; DEBUG((DEBUG_INFO, "ProcessCapsuleImage (Ux) - %r\n", Status)); - CapsuleStatusArray [Index] =3D Status; break; } } @@ -316,38 +378,48 @@ ProcessTheseCapsules ( // // 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))= { + for (Index =3D 0; Index < mCapsuleTotalNumber; Index++) { + if (mCapsuleStatusArray [Index] !=3D EFI_NOT_READY) { + // already processed + continue; + } + CapsuleHeader =3D (EFI_CAPSULE_HEADER*) mCapsulePtr [Index]; + if (!CompareGuid (&CapsuleHeader->CapsuleGuid, &gWindowsUxCapsuleGuid)= ) { // // Call capsule library to process capsule image. // EmbeddedDriverCount =3D 0; if (IsFmpCapsule(CapsuleHeader)) { - Status =3D ValidateFmpCapsule(CapsuleHeader, &EmbeddedDriverCount)= ; + Status =3D ValidateFmpCapsule (CapsuleHeader, &EmbeddedDriverCount= ); if (EFI_ERROR(Status)) { DEBUG((DEBUG_ERROR, "ValidateFmpCapsule failed. Ignore!\n")); + mCapsuleStatusArray [Index] =3D EFI_ABORTED; continue; } + } else { + mCapsuleStatusArray [Index] =3D EFI_ABORTED; + continue; } =20 - if ((!NeedBlockDriver) || (EmbeddedDriverCount =3D=3D 0)) { + if ((!FirstRound) || (EmbeddedDriverCount =3D=3D 0)) { DEBUG((DEBUG_INFO, "ProcessCapsuleImage - 0x%x\n", CapsuleHeader))= ; Status =3D ProcessCapsuleImage (CapsuleHeader); - CapsuleStatusArray [Index] =3D Status; + mCapsuleStatusArray [Index] =3D Status; DEBUG((DEBUG_INFO, "ProcessCapsuleImage - %r\n", Status)); =20 - if (EFI_ERROR(Status)) { - REPORT_STATUS_CODE(EFI_ERROR_CODE, (EFI_SOFTWARE | PcdGet32(PcdS= tatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareFail= ed))); - DEBUG ((DEBUG_ERROR, "Capsule process failed. reset the system!\= n")); - Print (L"Firmware update failed...\r\n"); - } else { - REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32(P= cdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareS= uccess))); - } - - if ((CapsuleHeader->Flags & PcdGet16(PcdSystemRebootAfterCapsulePr= ocessFlag)) !=3D 0 || - IsFmpCapsule(CapsuleHeader)) { - mNeedReset =3D TRUE; + if (Status !=3D EFI_NOT_READY) { + if (EFI_ERROR(Status)) { + REPORT_STATUS_CODE(EFI_ERROR_CODE, (EFI_SOFTWARE | PcdGet32(Pc= dStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwareFa= iled))); + DEBUG ((DEBUG_ERROR, "Capsule process failed!\n")); + Print (L"Firmware update failed...\r\n"); + } else { + REPORT_STATUS_CODE(EFI_PROGRESS_CODE, (EFI_SOFTWARE | PcdGet32= (PcdStatusCodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeUpdateFirmwar= eSuccess))); + } + + if ((CapsuleHeader->Flags & PcdGet16(PcdSystemRebootAfterCapsule= ProcessFlag)) !=3D 0 || + IsFmpCapsule(CapsuleHeader)) { + mNeedReset =3D TRUE; + } } } } @@ -362,20 +434,6 @@ ProcessTheseCapsules ( } Status =3D EFI_SUCCESS; =20 -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, (EFI_SOFTWARE | PcdGet32(PcdStatus= CodeSubClassCapsule) | PcdGet32(PcdCapsuleStatusCodeProcessCapsulesEnd))); =20 return Status; @@ -446,20 +504,13 @@ ProcessCapsules ( EFI_STATUS Status; =20 if (!mDxeCapsuleLibEndOfDxe) { - // - // Initialize mAreAllImagesProcessed to be TRUE. - // - // It will be updated to FALSE in ProcessTheseCapsules()->ProcessCapsu= leImage(), - // 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) { + if (mNeedReset && AreAllImagesProcessed()) { DoResetSystem(); } } else { diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c b/= MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c index a591beb..7c7e610 100644 --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLib.c @@ -35,17 +35,6 @@ =20 #include =20 -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. =20 @@ -78,92 +67,6 @@ GetCurrentCapsuleLastIndex ( } =20 /** - Check if this FMP capsule is processed. - - @param[in] CapsuleHeader The capsule image header - @param[in] PayloadIndex FMP payload index - @param[in] 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].CapsuleResultHea= der; - if (CapsuleResult->VariableTotalSize >=3D sizeof(EFI_CAPSULE_RESULT_VA= RIABLE_HEADER)) { - if (CompareGuid(&CapsuleResult->CapsuleGuid, &gEfiFmpCapsuleGuid)) { - if (CapsuleResult->VariableTotalSize >=3D sizeof(EFI_CAPSULE_RESUL= T_VARIABLE_HEADER) + sizeof(EFI_CAPSULE_RESULT_VARIABLE_FMP) + sizeof(CHAR1= 6) * 2) { - CapsuleResultFmp =3D (EFI_CAPSULE_RESULT_VARIABLE_FMP *)(Capsule= Result + 1); - if (CompareGuid(&CapsuleResultFmp->UpdateImageTypeId, &ImageHead= er->UpdateImageTypeId) && - (CapsuleResultFmp->UpdateImageIndex =3D=3D ImageHeader->Upda= teImageIndex) && - (CapsuleResultFmp->PayloadIndex =3D=3D PayloadIndex) ) { - return TRUE; - } - } - } - } - } - - return FALSE; -} - -/** - Write a new capsule status variable cache. - - @param[in] CapsuleResult The capsule status variable - @param[in] CapsuleResultSize The size of the capsule stauts variable in= bytes - - @retval EFI_SUCCESS The capsule status variable is cached. - @retval EFI_OUT_OF_RESOURCES No resource to cache the capsule status var= iable. -**/ -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 mCapsuleResultVariableCacheM= axCount) { - mCapsuleResultVariableCache =3D ReallocatePool( - mCapsuleResultVariableCacheMaxCount * = sizeof(CAPSULE_RESULT_VARIABLE_CACHE), - (mCapsuleResultVariableCacheMaxCount += CAPSULE_RESULT_VARIABLE_CACHE_COUNT) * sizeof(CAPSULE_RESULT_VARIABLE_CACH= E), - mCapsuleResultVariableCache - ); - if (mCapsuleResultVariableCache =3D=3D NULL) { - return EFI_OUT_OF_RESOURCES; - } - mCapsuleResultVariableCacheMaxCount +=3D CAPSULE_RESULT_VARIABLE_CACHE= _COUNT; - } - - ASSERT(mCapsuleResultVariableCacheCount < mCapsuleResultVariableCacheMax= Count); - ASSERT(mCapsuleResultVariableCache !=3D NULL); - CopyMem( - &mCapsuleResultVariableCache[mCapsuleResultVariableCacheCount], - CapsuleResult, - CapsuleResultSize - ); - mCapsuleResultVariableCacheCount++; - - return EFI_SUCCESS; -} - -/** Get a new capsule status variable index. =20 @return A new capsule status variable index. @@ -262,11 +165,7 @@ RecordCapsuleStatusVariable ( gRT->GetTime(&CapsuleResultVariable.CapsuleProcessed, NULL); CapsuleResultVariable.CapsuleStatus =3D CapsuleStatus; =20 - // - // Save Local Cache - // - Status =3D WriteNewCapsuleResultVariableCache(&CapsuleResultVariable, si= zeof(CapsuleResultVariable)); - + Status =3D EFI_SUCCESS; if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) !=3D 0) = { Status =3D WriteNewCapsuleResultVariable(&CapsuleResultVariable, sizeo= f(CapsuleResultVariable)); } @@ -338,11 +237,7 @@ RecordFmpCapsuleStatusVariable ( DevicePathStr =3D NULL; } =20 - // - // Save Local Cache - // - Status =3D WriteNewCapsuleResultVariableCache(CapsuleResultVariable, Cap= suleResultVariableSize); - + Status =3D EFI_SUCCESS; if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) !=3D 0) = { Status =3D WriteNewCapsuleResultVariable(CapsuleResultVariable, Capsul= eResultVariableSize); } diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLibNull.= c b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLibNull.c index 6ab198d..a6860ef 100644 --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLibNull.c +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleReportLibNull.c @@ -20,26 +20,6 @@ #include =20 /** - Check if this FMP capsule is processed. - - @param[in] CapsuleHeader The capsule image header - @param[in] PayloadIndex FMP payload index - @param[in] 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 - ) -{ - return FALSE; -} - -/** Record capsule status variable and to local cache. =20 @param[in] CapsuleHeader The capsule image header --=20 2.7.4.windows.1