From nobody Thu Dec 26 02:03:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1506648171316979.0874772513091; Thu, 28 Sep 2017 18:22:51 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E68CC2095E51A; Thu, 28 Sep 2017 18:19:34 -0700 (PDT) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (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 C689121F30415 for ; Thu, 28 Sep 2017 18:19:33 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga105.fm.intel.com with ESMTP; 28 Sep 2017 18:22:49 -0700 Received: from shwdeopenpsi168.ccr.corp.intel.com ([10.239.158.121]) by fmsmga001.fm.intel.com with ESMTP; 28 Sep 2017 18:22:48 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=yonghong.zhu@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,451,1500966000"; d="scan'208";a="1200258392" From: Yonghong Zhu To: edk2-devel@lists.01.org Date: Fri, 29 Sep 2017 09:22:42 +0800 Message-Id: <1506648164-5624-2-git-send-email-yonghong.zhu@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 In-Reply-To: <1506648164-5624-1-git-send-email-yonghong.zhu@intel.com> References: <1506648164-5624-1-git-send-email-yonghong.zhu@intel.com> Subject: [edk2] [Patch 1/3] BaseTools: GenFfs support to get alignment value from SectionFile X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Update GenFfs tool to get alignment value from SectionFile when use the new option -n 0. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yonghong Zhu --- BaseTools/Source/C/GenFfs/GenFfs.c | 127 +++++++++++++++++++++++++++++++++= +++- 1 file changed, 125 insertions(+), 2 deletions(-) diff --git a/BaseTools/Source/C/GenFfs/GenFfs.c b/BaseTools/Source/C/GenFfs= /GenFfs.c index cb16f38..26a3c88 100644 --- a/BaseTools/Source/C/GenFfs/GenFfs.c +++ b/BaseTools/Source/C/GenFfs/GenFfs.c @@ -10,10 +10,17 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, = =20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLI= ED. =20 =20 **/ =20 +#ifndef __GNUC__ +#include +#include +#include +#include +#endif + #include #include #include =20 #include @@ -22,10 +29,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITH= ER EXPRESS OR IMPLIED. #include =20 #include "CommonLib.h" #include "ParseInf.h" #include "EfiUtilityMsgs.h" +#include "FvLib.h" +#include "PeCoffLib.h" =20 #define UTILITY_NAME "GenFfs" #define UTILITY_MAJOR_VERSION 0 #define UTILITY_MINOR_VERSION 1 =20 @@ -151,11 +160,12 @@ Returns: 128K,256K,512K,1M,2M,4M,8M,16M\n"); fprintf (stdout, " -i SectionFile, --sectionfile SectionFile\n\ Section file will be contained in this FFS file.\n= "); fprintf (stdout, " -n SectionAlign, --sectionalign SectionAlign\n\ SectionAlign points to section alignment, which su= pport\n\ - the alignment scope 1~16M. It is specified togethe= r\n\ + the alignment scope 0~16M. If SectionAlign is spec= ified as 0, tool\n\ + get alignment value from SectionFile. It is specif= ied together\n\ with sectionfile to point its alignment in FFS fil= e.\n"); fprintf (stdout, " -v, --verbose Turn on verbose output with in= formational messages.\n"); fprintf (stdout, " -q, --quiet Disable all messages except ke= y message and fatal error\n"); fprintf (stdout, " -d, --debug level Enable debug messages, at inpu= t debug level.\n"); fprintf (stdout, " --version Show program's version number = and exit.\n"); @@ -455,10 +465,101 @@ Returns: *BufferLength =3D Size; return EFI_SUCCESS; } } =20 +EFI_STATUS +FfsRebaseImageRead ( + IN VOID *FileHandle, + IN UINTN FileOffset, + IN OUT UINT32 *ReadSize, + OUT VOID *Buffer + ) + /*++ + + Routine Description: + + Support routine for the PE/COFF Loader that reads a buffer from a PE/C= OFF file + + Arguments: + + FileHandle - The handle to the PE/COFF file + + FileOffset - The offset, in bytes, into the file to read + + ReadSize - The number of bytes to read from the file starting at File= Offset + + Buffer - A pointer to the buffer to read the data into. + + Returns: + + EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/= COFF file starting at FileOffset + + --*/ +{ + CHAR8 *Destination8; + CHAR8 *Source8; + UINT32 Length; + + Destination8 =3D Buffer; + Source8 =3D (CHAR8 *) ((UINTN) FileHandle + FileOffset); + Length =3D *ReadSize; + while (Length--) { + *(Destination8++) =3D *(Source8++); + } + + return EFI_SUCCESS; +} + +STATIC +EFI_STATUS +GetAlignmentFromFile(char *InFile, UINT32 *Alignment) + /*++ + InFile is input file for getting alignment + return the alignment + --*/ +{ + FILE *InFileHandle; + UINT8 *PeFileBuffer; + UINTN PeFileSize; + UINT32 CurSecHdrSize; + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + EFI_COMMON_SECTION_HEADER *CommonHeader; + EFI_STATUS Status; + + InFileHandle =3D NULL; + PeFileBuffer =3D NULL; + + memset (&ImageContext, 0, sizeof (ImageContext)); + + InFileHandle =3D fopen(LongFilePath(InFile), "rb"); + if (InFileHandle =3D=3D NULL){ + Error (NULL, 0, 0001, "Error opening file", InFile); + return EFI_ABORTED; + } + PeFileSize =3D _filelength (fileno(InFileHandle)); + PeFileBuffer =3D (UINT8 *) malloc (PeFileSize); + if (PeFileBuffer =3D=3D NULL) { + fclose (InFileHandle); + Error(NULL, 0, 4001, "Resource", "memory cannot be allocated of %s", = InFileHandle); + return EFI_OUT_OF_RESOURCES; + } + fread (PeFileBuffer, sizeof (UINT8), PeFileSize, InFileHandle); + fclose (InFileHandle); + CommonHeader =3D (EFI_COMMON_SECTION_HEADER *) PeFileBuffer; + CurSecHdrSize =3D GetSectionHeaderLength(CommonHeader); + ImageContext.Handle =3D (VOID *) ((UINTN)PeFileBuffer + CurSecHdrSize); + ImageContext.ImageRead =3D (PE_COFF_LOADER_READ_FILE)FfsRebaseImageRead; + Status =3D PeCoffLoaderGetImageInfo(&ImageContext); + if (EFI_ERROR (Status)) { + Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and ret= urn status is %x", InFile, (int) Status); + return Status; + } + *Alignment =3D ImageContext.SectionAlignment; + return EFI_SUCCESS; +} + int main ( int argc, CHAR8 *argv[] ) @@ -495,10 +596,12 @@ Returns: FILE *FfsFile; UINT32 Index; UINT64 LogLevel; UINT8 PeSectionNum; UINT32 HeaderSize; + UINT32 Alignment; + CHAR8 AlignmentBuffer[8]; =20 // // Init local variables // LogLevel =3D 0; @@ -688,11 +791,31 @@ Returns: =20 // // Section File alignment requirement // if ((stricmp (argv[0], "-n") =3D=3D 0) || (stricmp (argv[0], "--sect= ionalign") =3D=3D 0)) { - Status =3D StringtoAlignment (argv[1], &(InputFileAlign[InputFileN= um])); + if ((argv[1] !=3D NULL) && (stricmp("0", argv[1]) =3D=3D 0)) { + Status =3D GetAlignmentFromFile(InputFileName[InputFileNum], &Al= ignment); + if (EFI_ERROR(Status)) { + Error (NULL, 0, 1003, "Fail to get Alignment from %s", InputFi= leName[InputFileNum]); + goto Finish; + } + if (Alignment < 0x400){ + sprintf (AlignmentBuffer, "%d", Alignment); + } + else if (Alignment >=3D 0x400) { + if (Alignment >=3D 0x100000) { + sprintf (AlignmentBuffer, "%dM", Alignment/0x100000); + } else { + sprintf (AlignmentBuffer, "%dK", Alignment/0x400); + } + } + Status =3D StringtoAlignment (AlignmentBuffer, &(InputFileAlign[= InputFileNum])); + } + else { + Status =3D StringtoAlignment (argv[1], &(InputFileAlign[InputFil= eNum])); + } if (EFI_ERROR (Status)) { Error (NULL, 0, 1003, "Invalid option value", "%s =3D %s", argv[= 0], argv[1]); goto Finish; } argc -=3D 2; --=20 2.6.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel