From nobody Wed Dec 25 12:59:57 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 1510648051390496.8335895244985; Tue, 14 Nov 2017 00:27:31 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 8E640220D4C13; Tue, 14 Nov 2017 00:23:22 -0800 (PST) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (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 ABBE3220D4C0F for ; Tue, 14 Nov 2017 00:23:21 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Nov 2017 00:27:28 -0800 Received: from shwdeopenpsi168.ccr.corp.intel.com ([10.239.158.121]) by orsmga002.jf.intel.com with ESMTP; 14 Nov 2017 00:27:27 -0800 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=134.134.136.31; helo=mga06.intel.com; envelope-from=yonghong.zhu@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,393,1505804400"; d="scan'208";a="7398736" From: Yonghong Zhu To: edk2-devel@lists.01.org Date: Tue, 14 Nov 2017 16:27:10 +0800 Message-Id: <1510648032-7924-2-git-send-email-yonghong.zhu@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 In-Reply-To: <1510648032-7924-1-git-send-email-yonghong.zhu@intel.com> References: <1510648032-7924-1-git-send-email-yonghong.zhu@intel.com> Subject: [edk2] [Patch 1/3 V2] 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 | 135 +++++++++++++++++++++++++++++++++= +++- 1 file changed, 132 insertions(+), 3 deletions(-) diff --git a/BaseTools/Source/C/GenFfs/GenFfs.c b/BaseTools/Source/C/GenFfs= /GenFfs.c index cb16f38..e2fb3e0 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,12 +160,14 @@ 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\ - with sectionfile to point its alignment in FFS fil= e.\n"); + the alignment scope 0~16M. If SectionAlign is spec= ified\n\ + as 0, tool get alignment value from SectionFile. I= t is\n\ + specified together with sectionfile to point its\n\ + alignment in FFS file.\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"); fprintf (stdout, " -h, --help Show this help message and exi= t.\n"); @@ -455,10 +466,106 @@ 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; + // Free the allocated memory resource + if (PeFileBuffer !=3D NULL) { + free (PeFileBuffer); + PeFileBuffer =3D NULL; + } + return EFI_SUCCESS; +} + int main ( int argc, CHAR8 *argv[] ) @@ -495,10 +602,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 +797,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