From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: bob.c.feng@intel.com) 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 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" 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: [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> X-Mailer: git-send-email 2.29.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 --- 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 ( )=0D {=0D FIRMWARE_DEVICE *LocalFdData;=0D UINT16 Index;=0D CHAR8 Ffs2Guid[16];=0D - CHAR8 SignatureCheck[4];=0D + CHAR8 SignatureCheck[5] =3D "";=0D CHAR8 Signature[5] =3D "_FVH";=0D FV_INFORMATION *CurrentFv;=0D FV_INFORMATION *NewFoundFv;=0D BOOLEAN FirstMatch;=0D UINT32 FdSize;=0D UINT16 FvCount;=0D - VOID *FdBuffer;=0D - VOID *FdBufferOri;=0D - UINT32 Count;=0D -=0D + UINT8 *FdBuffer;=0D + UINT8 *FdBufferEnd;=0D + UINT8 *FdBufferOri;=0D + EFI_FIRMWARE_VOLUME_HEADER *FvHeader;=0D =0D CurrentFv =3D NULL;=0D NewFoundFv =3D NULL;=0D FdBuffer =3D NULL;=0D FdBufferOri =3D NULL;=0D FirstMatch =3D TRUE;=0D Index =3D 0;=0D FdSize =3D 0;=0D FvCount =3D 0;=0D - Count =3D 0;=0D LocalFdData =3D NULL;=0D =0D if (InputFile =3D=3D NULL) {=0D + Error ("BFM", 0, 0001, "Error opening the input file", "");=0D return EFI_ABORTED;=0D }=0D =0D //=0D // Find each FVs in the FD=0D @@ -204,56 +204,66 @@ LibFindFvInFd ( =0D fseek(InputFile,0,SEEK_SET);=0D //=0D // Create an FD structure to store useful information.=0D //=0D - LocalFdData =3D (FIRMWARE_DEVICE *) calloc (sizeof (FIRMWARE_DEVICE)= , sizeof(UINT8));=0D + LocalFdData =3D (FIRMWARE_DEVICE *) malloc (sizeof (FIRMWARE_DEVICE)= );=0D if (LocalFdData =3D=3D NULL) {=0D + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error");=0D return EFI_OUT_OF_RESOURCES;=0D }=0D - LocalFdData->Fv =3D (FV_INFORMATION *) calloc (sizeof (FV_INFORMATION),= sizeof(UINT8));=0D + LocalFdData->Fv =3D (FV_INFORMATION *) malloc (sizeof (FV_INFORMATION))= ;=0D if (LocalFdData->Fv =3D=3D NULL) {=0D + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error");=0D free (LocalFdData);=0D return EFI_OUT_OF_RESOURCES;=0D }=0D +=0D LibInitializeFvStruct (LocalFdData->Fv);=0D =0D //=0D // Readout the FD file data to buffer.=0D //=0D FdBuffer =3D malloc (FdSize);=0D =0D if (FdBuffer =3D=3D NULL) {=0D + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Allocat= e memory error");=0D free (LocalFdData->Fv);=0D free (LocalFdData);=0D return EFI_OUT_OF_RESOURCES;=0D }=0D =0D if (fread (FdBuffer, 1, FdSize, InputFile) !=3D FdSize) {=0D + Error ("BFM", 0, 0002, "Error searching FVs in the input fd", "Read FD= file error!");=0D free (LocalFdData->Fv);=0D free (LocalFdData);=0D free (FdBuffer);=0D return EFI_ABORTED;=0D }=0D =0D FdBufferOri =3D FdBuffer;=0D + FdBufferEnd =3D FdBuffer + FdSize;=0D =0D - for (Count=3D0; Count < FdSize - 4; Count++) {=0D + if (FdSize < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) {=0D + Error ("BFM", 0, 0002, "Error Check the input FD, Please make sure the= FD is valid", "Check FD size error!");=0D + return EFI_ABORTED;=0D + }=0D +=0D + while (FdBuffer <=3D FdBufferEnd - sizeof (EFI_FIRMWARE_VOLUME_HEADER)) = {=0D + FvHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *) FdBuffer;=0D //=0D // Copy 4 bytes of fd data to check the _FVH signature=0D //=0D - memcpy (SignatureCheck, FdBuffer, 4);=0D - FdBuffer =3D(UINT8 *)FdBuffer + 4;=0D + memcpy (SignatureCheck, &FvHeader->Signature, 4);=0D =0D if (strncmp(SignatureCheck, Signature, 4) =3D=3D 0){=0D //=0D // Still need to determine the FileSystemGuid in EFI_FIRMWARE_VOLUME= _HEADER equal to=0D - // EFI_FIRMWARE_FILE_SYSTEM2_GUID.=0D + // EFI_FIRMWARE_FILE_SYSTEM2_GUID or EFI_FIRMWARE_FILE_SYSTEM3_GUID.= =0D // Turn back 28 bytes to find the GUID.=0D //=0D - FdBuffer =3D (UINT8 *)FdBuffer - 28;=0D - memcpy (Ffs2Guid, FdBuffer, 16);=0D + memcpy (Ffs2Guid, &FvHeader->FileSystemGuid, 16);=0D =0D //=0D // Compare GUID.=0D //=0D for (Index =3D 0; Index < 16; Index ++) {=0D @@ -267,32 +277,28 @@ LibFindFvInFd ( break;=0D }=0D }=0D }=0D =0D - //=0D - // Point to the original address=0D - //=0D - FdBuffer =3D (UINT8 *)FdBuffer + 28;=0D -=0D //=0D // Here we found an FV.=0D //=0D - if (Index =3D=3D 16) {=0D + if ((Index =3D=3D 16) && ((FdBuffer + FvHeader->FvLength) <=3D FdBuf= ferEnd)) {=0D if (FirstMatch) {=0D - LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (U= INT8 *)FdBufferOri) - 0x2c;=0D + LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (U= INT8 *)FdBufferOri);=0D CurrentFv =3D LocalFdData->Fv;=0D CurrentFv->FvNext =3D NULL;=0D //=0D // Store the FV name by found sequence=0D //=0D sprintf(CurrentFv->FvName, "FV%d", FvCount);=0D =0D FirstMatch =3D FALSE;=0D } else {=0D NewFoundFv =3D (FV_INFORMATION *) malloc (sizeof (FV_INFORMATI= ON));=0D - if (NULL =3D=3D NewFoundFv) {=0D + if (NewFoundFv =3D=3D NULL) {=0D + Error ("BFM", 0, 0002, "Error searching FVs in the input fd"= , "Allocate memory error");=0D free (LocalFdData->Fv);=0D free (LocalFdData);=0D free (FdBuffer);=0D return EFI_OUT_OF_RESOURCES;=0D }=0D @@ -300,11 +306,11 @@ LibFindFvInFd ( LibInitializeFvStruct (NewFoundFv);=0D =0D //=0D // Need to turn back 0x2c bytes=0D //=0D - NewFoundFv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT= 8 *)FdBufferOri) - 0x2c;=0D + NewFoundFv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT= 8 *)FdBufferOri);=0D =0D //=0D // Store the FV name by found sequence=0D //=0D sprintf(NewFoundFv->FvName, "FV%d", FvCount);=0D @@ -320,19 +326,18 @@ LibFindFvInFd ( //=0D CurrentFv =3D CurrentFv->FvNext;=0D }=0D =0D FvCount ++;=0D - Index =3D 0;=0D + FdBuffer =3D FdBuffer + FvHeader->FvLength;=0D + } else {=0D + FdBuffer ++;=0D }=0D =0D + } else {=0D + FdBuffer ++;=0D }=0D -=0D - //=0D - // We need to turn back 3 bytes.=0D - //=0D - FdBuffer =3D (UINT8 *)FdBuffer - 3;=0D }=0D =0D LocalFdData->Size =3D FdSize;=0D =0D *FdData =3D LocalFdData;=0D --=20 2.29.1.windows.1