From nobody Sun May 5 21:15:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+67131+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+67131+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1604887135; cv=none; d=zohomail.com; s=zohoarc; b=YRGQ5zjtw4T2bv6SCT+1SCs+V4j6Gze0RMAGyX3xTkXJz4zgr2fiv2vLJ27NQ7JJjgmL4ljU7hxz8EyUpr7OJ1nbiOXsaudW9EaatREYOKW8LcTcxUls3qP3VfRWtMV307siYxZmOM+1gZbPF0+ouFDZ3LgolEwMoF80SO2Is/o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604887135; h=Content-Transfer-Encoding:Cc:Date:From:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=lK9uoadb2FW9MQjaJl6ZJsT3EWWi23Eud38Gc4C08KM=; b=JRMZGsd0a5k/bK8HDRoM17JQ2uV2+mzrMiD7nzOzD5HBvwR9kDfBZ3JERzQKipZ86Hk8lGxdVNsITDnA9eZiY2/K1Ixr2j7NyNv60znEO8xX18nalTrOLG5CxJDTHYQ4YkqU4TOhB6KJ6XXuA5OGI1Hsqc0iYmoonG6c78PUE3k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+67131+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 160488713574967.51568798670269; Sun, 8 Nov 2020 17:58:55 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id mCCcYY1788612xGq6Z2NTVAg; Sun, 08 Nov 2020 17:58:55 -0800 X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web08.5709.1604887134481933137 for ; Sun, 08 Nov 2020 17:58:54 -0800 IronPort-SDR: v2PGFsIiuxgJG2uA1qrvoCGL2jXrEYQPArtg2ZoaSToWEzg1QYXEAQnU8PLPvd1+ksgN16GKK7 08ugmx6laoRA== X-IronPort-AV: E=McAfee;i="6000,8403,9799"; a="254445694" X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="254445694" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2020 17:57:41 -0800 IronPort-SDR: gvaAX8C0o5uRRiFCpK4GB6Eky0ABkc1xL8RUgqn4UVgCOvKpz4vhjSaV6r74eqc0jPRPckWixe dLB+0sDzM9UQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,462,1596524400"; d="scan'208";a="355464167" X-Received: from shwdepsi1121.ccr.corp.intel.com ([10.239.158.32]) by fmsmga004.fm.intel.com with ESMTP; 08 Nov 2020 17:57:36 -0800 From: "Bob Feng" To: devel@edk2.groups.io Cc: Yunhua Feng , Liming Gao Subject: [edk2-devel] [Patch] [edk2-staging]BaseTools/Bfm: Apply the FMMT algorithm of LibFindFvInFd Date: Mon, 9 Nov 2020 09:57:25 +0800 Message-Id: <20201109015725.1733-1-bob.c.feng@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,bob.c.feng@intel.com X-Gm-Message-State: 1cmaNrq0P9kTqOO98YElBP4vx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1604887135; bh=ngyW6kLFoMI7UWRkc7TWUQ5lfd4sy2Eq2eE481ChOXs=; h=Cc:Date:From:Reply-To:Subject:To; b=pT0gjmzsN7phqk67swtVXIE8qcgADOiq6M/009bLKpOl/feOggqKpLuqec7ywIniTOh +MtZQOBcetsDw9EuGj7inP7A41KQEFr+vuI7SW5ATYRZcWy44rujtoDAk1iPiyf07xrUG llR42cWYTGUMoZmthYDZEDCdNEVgWSiL+b0= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" The LibFindFvInFd algorithm of FMMT and BFM are different. The LibFindFvInFd in FMMT is the correct one. By applying FMMT LibFindFvInFd, BFM can handle the case that there are two same bios images in one Firmware binary. Signed-off-by: Bob Feng Cc: Yunhua Feng Cc: Liming Gao Reviewed-by: Yuwei Chen --- BaseTools/Source/C/BfmLib/BfmLib.c | 63 ++++++++++++++++-------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/BaseTools/Source/C/BfmLib/BfmLib.c b/BaseTools/Source/C/BfmLib= /BfmLib.c index 73854fdc73..c247cc8e1d 100644 --- a/BaseTools/Source/C/BfmLib/BfmLib.c +++ b/BaseTools/Source/C/BfmLib/BfmLib.c @@ -164,34 +164,34 @@ LibFindFvInFd ( ) { FIRMWARE_DEVICE *LocalFdData; UINT16 Index; CHAR8 Ffs2Guid[16]; - CHAR8 SignatureCheck[4]; + CHAR8 SignatureCheck[5] =3D ""; CHAR8 Signature[5] =3D "_FVH"; FV_INFORMATION *CurrentFv; FV_INFORMATION *NewFoundFv; BOOLEAN FirstMatch; UINT32 FdSize; UINT16 FvCount; - VOID *FdBuffer; - VOID *FdBufferOri; - UINT32 Count; - + UINT8 *FdBuffer; + UINT8 *FdBufferEnd; + UINT8 *FdBufferOri; + EFI_FIRMWARE_VOLUME_HEADER *FvHeader; =20 CurrentFv =3D NULL; NewFoundFv =3D NULL; FdBuffer =3D NULL; FdBufferOri =3D NULL; FirstMatch =3D TRUE; Index =3D 0; FdSize =3D 0; FvCount =3D 0; - Count =3D 0; LocalFdData =3D NULL; =20 if (InputFile =3D=3D NULL) { + Error ("BFM", 0, 0001, "Error opening the input file", ""); return EFI_ABORTED; } =20 // // Find each FVs in the FD @@ -204,56 +204,66 @@ LibFindFvInFd ( =20 fseek(InputFile,0,SEEK_SET); // // Create an FD structure to store useful information. // - LocalFdData =3D (FIRMWARE_DEVICE *) calloc (sizeof (FIRMWARE_DEVICE)= , sizeof(UINT8)); + LocalFdData =3D (FIRMWARE_DEVICE *) malloc (sizeof (FIRMWARE_DEVICE)= ); if (LocalFdData =3D=3D NULL) { + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error"); return EFI_OUT_OF_RESOURCES; } - LocalFdData->Fv =3D (FV_INFORMATION *) calloc (sizeof (FV_INFORMATION),= sizeof(UINT8)); + LocalFdData->Fv =3D (FV_INFORMATION *) malloc (sizeof (FV_INFORMATION)); if (LocalFdData->Fv =3D=3D NULL) { + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error"); free (LocalFdData); return EFI_OUT_OF_RESOURCES; } + LibInitializeFvStruct (LocalFdData->Fv); =20 // // Readout the FD file data to buffer. // FdBuffer =3D malloc (FdSize); =20 if (FdBuffer =3D=3D NULL) { + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error"); free (LocalFdData->Fv); free (LocalFdData); return EFI_OUT_OF_RESOURCES; } =20 if (fread (FdBuffer, 1, FdSize, InputFile) !=3D FdSize) { + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Read FD= file error!"); free (LocalFdData->Fv); free (LocalFdData); free (FdBuffer); return EFI_ABORTED; } =20 FdBufferOri =3D FdBuffer; + FdBufferEnd =3D FdBuffer + FdSize; =20 - for (Count=3D0; Count < FdSize - 4; Count++) { + if (FdSize < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) { + Error ("BFM", 0, 0002, "Error Check the input FD, Please make sure the= FD is valid", "Check FD size error!"); + return EFI_ABORTED; + } + + while (FdBuffer <=3D FdBufferEnd - sizeof (EFI_FIRMWARE_VOLUME_HEADER)) { + FvHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *) FdBuffer; // // Copy 4 bytes of fd data to check the _FVH signature // - memcpy (SignatureCheck, FdBuffer, 4); - FdBuffer =3D(UINT8 *)FdBuffer + 4; + memcpy (SignatureCheck, &FvHeader->Signature, 4); =20 if (strncmp(SignatureCheck, Signature, 4) =3D=3D 0){ // // Still need to determine the FileSystemGuid in EFI_FIRMWARE_VOLUME= _HEADER equal to - // EFI_FIRMWARE_FILE_SYSTEM2_GUID. + // EFI_FIRMWARE_FILE_SYSTEM2_GUID or EFI_FIRMWARE_FILE_SYSTEM3_GUID. // Turn back 28 bytes to find the GUID. // - FdBuffer =3D (UINT8 *)FdBuffer - 28; - memcpy (Ffs2Guid, FdBuffer, 16); + memcpy (Ffs2Guid, &FvHeader->FileSystemGuid, 16); =20 // // Compare GUID. // for (Index =3D 0; Index < 16; Index ++) { @@ -267,32 +277,28 @@ LibFindFvInFd ( break; } } } =20 - // - // Point to the original address - // - FdBuffer =3D (UINT8 *)FdBuffer + 28; - // // Here we found an FV. // - if (Index =3D=3D 16) { + if ((Index =3D=3D 16) && ((FdBuffer + FvHeader->FvLength) <=3D FdBuf= ferEnd)) { if (FirstMatch) { - LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (U= INT8 *)FdBufferOri) - 0x2c; + LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (U= INT8 *)FdBufferOri); CurrentFv =3D LocalFdData->Fv; CurrentFv->FvNext =3D NULL; // // Store the FV name by found sequence // sprintf(CurrentFv->FvName, "FV%d", FvCount); =20 FirstMatch =3D FALSE; } else { NewFoundFv =3D (FV_INFORMATION *) malloc (sizeof (FV_INFORMATI= ON)); - if (NULL =3D=3D NewFoundFv) { + if (NewFoundFv =3D=3D NULL) { + Error ("BFM", 0, 0002, "Error searching FVs in the input fd"= , "Allocate memory error"); free (LocalFdData->Fv); free (LocalFdData); free (FdBuffer); return EFI_OUT_OF_RESOURCES; } @@ -300,11 +306,11 @@ LibFindFvInFd ( LibInitializeFvStruct (NewFoundFv); =20 // // Need to turn back 0x2c bytes // - NewFoundFv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT= 8 *)FdBufferOri) - 0x2c; + NewFoundFv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT= 8 *)FdBufferOri); =20 // // Store the FV name by found sequence // sprintf(NewFoundFv->FvName, "FV%d", FvCount); @@ -320,19 +326,18 @@ LibFindFvInFd ( // CurrentFv =3D CurrentFv->FvNext; } =20 FvCount ++; - Index =3D 0; + FdBuffer =3D FdBuffer + FvHeader->FvLength; + } else { + FdBuffer ++; } =20 + } else { + FdBuffer ++; } - - // - // We need to turn back 3 bytes. - // - FdBuffer =3D (UINT8 *)FdBuffer - 3; } =20 LocalFdData->Size =3D FdSize; =20 *FdData =3D LocalFdData; --=20 2.29.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#67131): https://edk2.groups.io/g/devel/message/67131 Mute This Topic: https://groups.io/mt/78127410/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-