From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.byosoft.com.cn (mail.byosoft.com.cn [58.240.74.242]) by mx.groups.io with SMTP id smtpd.web12.12125.1609757318551558043 for ; Mon, 04 Jan 2021 02:48:39 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=none, err=permanent DNS error (domain: byosoft.com.cn, ip: 58.240.74.242, mailfrom: fengyunhua@byosoft.com.cn) Received: from LAPTOP2AECFQIA ([58.246.60.130]) (envelope-sender ) by 192.168.6.13 with ESMTP for ; Mon, 04 Jan 2021 18:48:35 +0800 X-WM-Sender: fengyunhua@byosoft.com.cn X-WM-AuthFlag: YES X-WM-AuthUser: fengyunhua@byosoft.com.cn From: "fengyunhua" To: "'Bob Feng'" , Cc: "'Liming Gao'" References: <20201109015725.1733-1-bob.c.feng@intel.com> In-Reply-To: <20201109015725.1733-1-bob.c.feng@intel.com> Subject: =?UTF-8?B?5Zue5aSNOiBbUGF0Y2hdIFtlZGsyLXN0YWdpbmddQmFzZVRvb2xzL0JmbTogQXBwbHkgdGhlIEZNTVQgYWxnb3JpdGhtIG9mIExpYkZpbmRGdkluRmQ=?= Date: Mon, 4 Jan 2021 18:48:36 +0800 Message-ID: <000101d6e287$27905100$76b0f300$@byosoft.com.cn> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQKgQv+/qKnkojHlVYe0R4C0hKGR7aiEh3GQ Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: quoted-printable Content-Language: zh-cn Reviewed-by: Yunhua Feng Thanks, Yunhua -----=D3=CA=BC=FE=D4=AD=BC=FE----- =B7=A2=BC=FE=C8=CB: Bob Feng =20 =B7=A2=CB=CD=CA=B1=BC=E4: 2020=C4=EA11=D4=C29=C8=D5 9:57 =CA=D5=BC=FE=C8=CB: devel@edk2.groups.io =B3=AD=CB=CD: Yunhua Feng ; Liming Gao =D6=F7=CC=E2: [Patch] [edk2-staging]BaseTools/Bfm: Apply the FMMT = algorithm of LibFindFvInFd 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 ( ) { 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", = "Allocate 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", = "Allocate 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", = "Allocate 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 FdBufferEnd)) { if (FirstMatch) { - LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT8 *)FdBufferOri) - 0x2c; + LocalFdData->Fv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - (UINT8 *)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_INFORMATION)); - 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 - = (UINT8 *)FdBufferOri) - 0x2c; + NewFoundFv->ImageAddress =3D (UINTN)((UINT8 *)FdBuffer - = (UINT8 *)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