From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web12.4129.1664677569445232220 for ; Sat, 01 Oct 2022 19:26:09 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=Wm4OK9/W; spf=pass (domain: intel.com, ip: 192.55.52.88, mailfrom: bob.c.feng@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664677569; x=1696213569; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=5/lEdMyBquR/DlHJOfozEnZz6YngdWyjYwYfRpWJXEk=; b=Wm4OK9/WcE0x539gVmbWBxOqAeHFQA9nSbXYTps/fESNGHYzqDjG434H GFFnxJoP4GTynsJJt+udZK0gdpsNIzPD7IY0ipVa/egd4juQfN1/kvBlo qLXeeDY8sfXs0+IqnYgbpJXdCofrOnNel2LfoxXmgFkiP7NURuXsb9hrM 0TsweJ1mG6QmXyVVks3Mea1p8s3MTvUyB3Xi8U60donC83V99u87ARx9c KfhKHn1kuP7aGsUBn5wmYIr5szks8TZv9JiklCwmHirL4S+YRuH2kQ2KP SQqauE2/AoqrFeKQWk8w4UNs9i+AT0ZjhsyZlTBwe8nXX7eN8R8B5TGZH w==; X-IronPort-AV: E=McAfee;i="6500,9779,10487"; a="328826803" X-IronPort-AV: E=Sophos;i="5.93,361,1654585200"; d="scan'208";a="328826803" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Oct 2022 19:26:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10487"; a="653978172" X-IronPort-AV: E=Sophos;i="5.93,361,1654585200"; d="scan'208";a="653978172" Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by orsmga008.jf.intel.com with ESMTP; 01 Oct 2022 19:26:08 -0700 Received: from fmsmsx611.amr.corp.intel.com (10.18.126.91) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 1 Oct 2022 19:26:08 -0700 Received: from fmsedg601.ED.cps.intel.com (10.1.192.135) by fmsmsx611.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31 via Frontend Transport; Sat, 1 Oct 2022 19:26:08 -0700 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.168) by edgegateway.intel.com (192.55.55.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2375.31; Sat, 1 Oct 2022 19:26:07 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k0sseqopb705YcyCqvIFU/Q5W+QJAs8NC37MjDoYmxrltUETKzQbtUVmczKc+MV74a1htbrVTQRT3eBdKCMyUUJ1YuCCRl3dhSZaL0WWny43SVoEBKoxlDApVMWrYiBoG1gU9/JKLmlhRH43nnWVlU3+aLbU9GmjkF5d8MEDQ2UR6Ngv49K1fu17cG/vHo7E/9ysroAFA7ccQrbyMKCVfWQiBss9NxQtx9RV4+8BMBnazWIsnRwbQGW+gmoCF4oHKcfRjkEl4zyJrG7ljDFk8GzUU7+v/v9PP5zUijKpTbAJfp4gP7PrTU5WR1XqozPKvrXh39t9yZr7ZK2Rb36aIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=u4lusq7SWcJ+A9eB2PjbXuw0j7r2fl+VJHY2l7QQxeY=; b=P3vLTgZm0sxWlJkwFBv505LakPe/va3N3wTPaekdau9wjW8mKbqVePUCl0l8+yH+P5QqfRNmHof4sB3ctmhwZoresyGEfqAKt8EYCuprh4p7rYC+7ZiUO8Wlt1CCy6HXr/Bz8tM3huNiyA/c1hKH89QmaLfYNSnPB1/460Zfrb6UJ/2r+GiwZ9waPLi80c5v97CGymMNUyGH6ktjyx8J51NGpNar26EGgJS8+phuQl6hPCJChjmEPPnohp0Yt71VnvpIig2sCZf+9pm4zKjK1l9UMmsppqtGUPCkZuxdyr7HoaIsKLPRR6Hq2TdCdiRq1GGGxk8Oi5ZEtKKOey2nNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from PH7PR11MB5863.namprd11.prod.outlook.com (2603:10b6:510:135::11) by DM6PR11MB4659.namprd11.prod.outlook.com (2603:10b6:5:2a5::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Sun, 2 Oct 2022 02:26:05 +0000 Received: from PH7PR11MB5863.namprd11.prod.outlook.com ([fe80::b6b:85ec:5ee0:d1]) by PH7PR11MB5863.namprd11.prod.outlook.com ([fe80::b6b:85ec:5ee0:d1%9]) with mapi id 15.20.5676.017; Sun, 2 Oct 2022 02:26:05 +0000 From: "Bob Feng" To: "Chen, Christine" , "devel@edk2.groups.io" CC: "Gao, Liming" Subject: Re: [PATCH 2/2] BaseTools/FMMT: Add Shrink Fv function Thread-Topic: [PATCH 2/2] BaseTools/FMMT: Add Shrink Fv function Thread-Index: AQHYyW7XJjDV6cYXSU2MDEI97QwAFa36ebLQ Date: Sun, 2 Oct 2022 02:26:05 +0000 Message-ID: References: <20220916015118.1367-1-yuwei.chen@intel.com> In-Reply-To: <20220916015118.1367-1-yuwei.chen@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-version: 11.6.500.17 dlp-product: dlpe-windows dlp-reaction: no-action authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PH7PR11MB5863:EE_|DM6PR11MB4659:EE_ x-ms-office365-filtering-correlation-id: f4a3be28-0f0d-45e2-c422-08daa41d7519 x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 2MXDwX0/421IYRWrOFmd6klyL4XJiR+F4COIGrK3aSjdxEoCxF2X4EuCgwhArpO7C/eTc6Up+LUH1ubgv1fHc16QRXzupHxaADKmbkcj/gzNnjtP9Lq6/hCdbiKJqKR6QHrmJyIFS1je90rvKhqk7LTLRPdevG1NQSK1/HTcEZfeXTK0FrNnMkqcRYYNtwcG0phoHDwLrBmCykbyMLc0ID+8E89bJztySw0b0qd8qy8tUX7RIbeW4HXkNgDYK/bADPeRXQf7Rr2g/mecWy4apPZbsEUNmTweahgrW/a8/GCHtxZck0KO3oB9z+mOugFQGiJn7vRjoV7eNUvtPkIMwvHGaQlZgmsab+xv2Lew8J5rSEZLealMjlenkXelXevU8YPZxbgdaZXUrv+DsvUXZj3CIrjfsWGY3KrqeL5Pryy/q2jfXtc7ZizW/MMrhpUQO8XO3/qlKnodZhHsU+0DGpDhPWe8dAogwzE30HRFke1T15EzHbnmWk64+GyIlACOvd+XDoOZZ5jylwqEti7UhToVxp4DcJ1Qcekg0ytyiSfhlSSxMVuq/XIuW5dLX1KCv94/lvkKvg6O5rox7sgNcqpw1fqT4jj4ZMGuJAKIfZTmOE0qNE5ln5Lo3WTmdLgz+dRVgP0z7xAwUEZc8FQjpUOqw07YwObkNzq2XlGs1oWzvj7qikbWjeZSRABo+FXRKOA4OB0G+iIt1YHTzuuJ8nYyWFByT4FqT0cw8zktTNiJmihSEbDUksvZ8LGi9l/ZeChVdC2Zl8ayYxIEcYyt49vhX56vych2UO34ZDGOelM= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH7PR11MB5863.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(346002)(366004)(396003)(136003)(376002)(451199015)(55016003)(38070700005)(33656002)(86362001)(82960400001)(122000001)(38100700002)(66946007)(66476007)(66556008)(66446008)(64756008)(4326008)(76116006)(8676002)(110136005)(316002)(2906002)(52536014)(41300700001)(8936002)(5660300002)(83380400001)(478600001)(186003)(966005)(53546011)(71200400001)(9686003)(26005)(6506007)(7696005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?qtYYxKH7/hkRcC563dPPs7BoQb/6YUnkojqbMMOptxQNg9272RUNAMARf65o?= =?us-ascii?Q?lp4U7cylyBVXsd1a2OkiNNknAHvms7x6a+smCiGiEy4Nsb4iagYCC5Pkq7H4?= =?us-ascii?Q?wPqjKCTPvkin7P+qSzmdX4h4Pl10MUUPCgbz+dgqBeFM5V4kpljRbBmEJV5c?= =?us-ascii?Q?LlNtRfz3swUU7YIdnJzN4vE4vjHJaHzB1HOtNH+OwvfqF3Mh0effr/MNunio?= =?us-ascii?Q?OWOQNhZ8wbv3ZUudrYeOAkFb1v0aUi8yyOsmB+QB0QBtpaxadAetOONhWBxi?= =?us-ascii?Q?RGz/Tw9fa+3+ZfU/zWrRDwFx88ZyQxiQG4WBBDZep6qKccvq8yNcsoOFy+35?= =?us-ascii?Q?L1XRawMflyQ5bijwe482ybB3mMNWDLboZS8weBHCWYZuZ/y59wXwraN92dR2?= =?us-ascii?Q?tbWXRFqO6Y2YX5mD8RCyt71TIVK6wPrs7BavrBtHUkF4qjPgL92z6oa8F/wg?= =?us-ascii?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k5R6pJNDqDN/KWv5p3ecD6N71G?= =?us-ascii?Q?JUvij4+yXgn+KH7JOrspemi2S73bnqVcoTVo/diTVs3F+GVIAY7UN0fpWK5l?= =?us-ascii?Q?ycZduZZ1tGeIdOJyKJojMQMvlhXuKJHfYKm3/q715frps61oV6qjTuy9udYK?= =?us-ascii?Q?kO+2R2UaxYJB4oGxIUKC5FWu16TP/2B2cz7Qkvqg/+kxALQh4e2Ogx4WdVsW?= =?us-ascii?Q?RWjraHvftBSnBAhp+dQnCb8upLOJmUwLiK2g3ald?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PH7PR11MB5863.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4a3be28-0f0d-45e2-c422-08daa41d7519 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Oct 2022 02:26:05.1663 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: R23PRDoozdjdXMrFtXank1sSKeAJL0F4vt08SLrr3/1Beqlo4G9oyePC73G4g8I+jFf1kG6bfIh58wZ19+jLKw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB4659 Return-Path: bob.c.feng@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Bob Feng -----Original Message----- From: Chen, Christine =20 Sent: Friday, September 16, 2022 9:51 AM To: devel@edk2.groups.io Cc: Feng, Bob C ; Gao, Liming Subject: [PATCH 2/2] BaseTools/FMMT: Add Shrink Fv function REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3938 This function is used to remove the useless FV free space. Usage: FMMT -s Inputfile Outputfile Cc: Bob Feng Cc: Liming Gao Signed-off-by: Yuwei Chen --- BaseTools/Source/Python/FMMT/FMMT.py | 7 +++++ .../Source/Python/FMMT/core/FMMTOperation.py | 26 +++++++++++++++++ .../Source/Python/FMMT/core/FvHandler.py | 28 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/BaseTools/Source/Python/FMMT/FMMT.py b/BaseTools/Source/Python= /FMMT/FMMT.py index 3590f3340ec3..bf580b3843a8 100644 --- a/BaseTools/Source/Python/FMMT/FMMT.py +++ b/BaseTools/Source/Python/FMMT/FMMT.py @@ -41,6 +41,8 @@ parser.add_argument("-c", "--ConfigFilePath", dest=3D"Con= figFilePath", nargs=3D'+', FmmtConf file saves the target guidtool used in co= mpress/uncompress process.\ If do not provide, FMMT tool will search the input= file folder for FmmtConf.ini firstly, if not found,\ the FmmtConf.ini saved in FMMT tool's folder will = be used as default.") +parser.add_argument("-s", "--ShrinkFv", dest=3D"ShrinkFv", nargs=3D'+', + help=3D"Shrink the Fv file: '-s InputFvfile OutputFvfi= le") =20 def print_banner(): print("") @@ -111,6 +113,9 @@ class FMMT(): else: ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile,= outputfile) =20 + def Shrink(self,inputfile: str, outputfile: str) -> None: + self.SetDestPath(inputfile) + ShrinkFv(inputfile, outputfile) =20 def main(): args=3Dparser.parse_args() @@ -142,6 +147,8 @@ def main(): fmmt.Replace(args.Replace[0],args.Replace[2],args.Replace[= 3],args.Replace[4],args.Replace[1]) else: fmmt.Replace(args.Replace[0],args.Replace[1],args.Replace[= 2],args.Replace[3]) + elif args.ShrinkFv: + fmmt.Shrink(args.ShrinkFv[0], args.ShrinkFv[1]) else: parser.print_help() except Exception as e: diff --git a/BaseTools/Source/Python/FMMT/core/FMMTOperation.py b/BaseTools= /Source/Python/FMMT/core/FMMTOperation.py index 4e58c91b5c41..a86f8dda9a1a 100644 --- a/BaseTools/Source/Python/FMMT/core/FMMTOperation.py +++ b/BaseTools/Source/Python/FMMT/core/FMMTOperation.py @@ -204,3 +204,29 @@ def ExtractFfs(inputfile: str, Ffs_name: str, outputfi= le: str, Fv_name: str=3DNone logger.debug('Extract ffs data is saved in {}.'.format(outputf= ile)) else: logger.error('Target Ffs/Fv not found!!!') + +def ShrinkFv(inputfile: str, outputfile: str) -> None: + if not os.path.exists(inputfile): + logger.error("Invalid inputfile, can not open {}.".format(inputfil= e)) + raise Exception("Process Failed: Invalid inputfile!") + # 1. Data Prepare + with open(inputfile, "rb") as f: + whole_data =3D f.read() + FmmtParser =3D FMMTParser(inputfile, ROOT_TREE) + # 2. DataTree Create + logger.debug('Parsing inputfile data......') + FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data) + logger.debug('Done!') + TargetFv =3D FmmtParser.WholeFvTree.Child[0] + if TargetFv: + FvMod =3D FvHandler(TargetFv) + Status =3D FvMod.ShrinkFv() + else: + logger.error('Target Fv not found!!!') + # 4. Data Encapsulation + if Status: + logger.debug('Start encapsulating data......') + FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False) + with open(outputfile, "wb") as f: + f.write(FmmtParser.FinalData) + logger.debug('Encapsulated data is saved in {}.'.format(outputfile= )) diff --git a/BaseTools/Source/Python/FMMT/core/FvHandler.py b/BaseTools/Sou= rce/Python/FMMT/core/FvHandler.py index e8b848009878..ff3d637623f8 100644 --- a/BaseTools/Source/Python/FMMT/core/FvHandler.py +++ b/BaseTools/Source/Python/FMMT/core/FvHandler.py @@ -145,7 +145,7 @@ def ModifyFvSystemGuid(TargetFv) -> None: TargetFv.Data.Data +=3D struct2stream(item.Data.Header)+ item.= Data.Data + item.Data.PadData =20 class FvHandler: - def __init__(self, NewFfs, TargetFfs) -> None: + def __init__(self, NewFfs, TargetFfs=3DNone) -> None: self.NewFfs =3D NewFfs self.TargetFfs =3D TargetFfs self.Status =3D False @@ -638,3 +638,29 @@ class FvHandler: self.Status =3D True logger.debug('Done!') return self.Status + + def ShrinkFv(self) -> bool: + TargetFv =3D self.NewFfs + TargetFv.Data.Data =3D b'' + if not TargetFv.Data.Free_Space: + self.Status =3D True + else: + BlockSize =3D TargetFv.Data.Header.BlockMap[0].Length + New_Free_Space =3D TargetFv.Data.Free_Space%BlockSize + Removed_Space =3D TargetFv.Data.Free_Space - New_Free_Space + TargetFv.Child[-1].Data.Data =3D b'\xff' * New_Free_Space + TargetFv.Data.Size -=3D Removed_Space + TargetFv.Data.Header.Fvlength =3D TargetFv.Data.Size + ModifyFvSystemGuid(TargetFv) + for item in TargetFv.Child: + if item.type =3D=3D FFS_FREE_SPACE: + TargetFv.Data.Data +=3D item.Data.Data + item.Data.Pad= Data + else: + TargetFv.Data.Data +=3D struct2stream(item.Data.Header= )+ item.Data.Data + item.Data.PadData + TargetFv.Data.ModFvExt() + TargetFv.Data.ModFvSize() + TargetFv.Data.ModExtHeaderData() + ModifyFvExtData(TargetFv) + TargetFv.Data.ModCheckSum() + self.Status =3D True + return self.Status --=20 2.27.0.windows.1