From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web10.1403.1663293083462735389 for ; Thu, 15 Sep 2022 18:51:23 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=gVq8ZeMN; spf=pass (domain: intel.com, ip: 192.55.52.93, mailfrom: yuwei.chen@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663293083; x=1694829083; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=WMLeFi+psjU+yZ3VqPPJBSYMJpt/kfLMhGLcmh6kZzA=; b=gVq8ZeMNBa9HBeyddtXBW8yYwUHQno0P6uD/Emaqd43fniQbDMGrcSCM DfTyfnykM9MoWaiV94Ff5fd6PsuD9hvEITJxkYtM3nI5SEsSjDFSZh6KA QTLdfgThublrtg7tzFMt0Il6AW0lFYTfWPa7hMXCcloatY3QU5mqx0LIs 8562Jwd4DUTEV/3Tfu2BvL+OREIk7Fb9tciP1lBtZbw5QopezZxmvuMtA 82ftF2nRifs1RrCmsSoQCZSjmJO4jnageXFBEw6c05zxH/27arbh1D2Rm X4SFTNP3hC1ZJkEDvhWF4p+XFSFGlN7O6gH71RvdUlMa+i22UbfdwdPj1 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10471"; a="296476038" X-IronPort-AV: E=Sophos;i="5.93,319,1654585200"; d="scan'208";a="296476038" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Sep 2022 18:51:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,319,1654585200"; d="scan'208";a="595067915" Received: from yuweipc.ccr.corp.intel.com ([10.239.158.38]) by orsmga006.jf.intel.com with ESMTP; 15 Sep 2022 18:51:21 -0700 From: "Yuwei Chen" To: devel@edk2.groups.io Cc: Bob Feng , Liming Gao Subject: [PATCH 2/2] BaseTools/FMMT: Add Shrink Fv function Date: Fri, 16 Sep 2022 09:51:18 +0800 Message-Id: <20220916015118.1367-1-yuwei.chen@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3938 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="ConfigFilePath", nargs='+', FmmtConf file saves the target guidtool used in compress/uncompress process.\ If do not provide, FMMT tool will search the inputfile 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="ShrinkFv", nargs='+', + help="Shrink the Fv file: '-s InputFvfile OutputFvfile") def print_banner(): print("") @@ -111,6 +113,9 @@ class FMMT(): else: ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile, outputfile) + def Shrink(self,inputfile: str, outputfile: str) -> None: + self.SetDestPath(inputfile) + ShrinkFv(inputfile, outputfile) def main(): args=parser.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, outputfile: str, Fv_name: str=None logger.debug('Extract ffs data is saved in {}.'.format(outputfile)) 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(inputfile)) + raise Exception("Process Failed: Invalid inputfile!") + # 1. Data Prepare + with open(inputfile, "rb") as f: + whole_data = f.read() + FmmtParser = FMMTParser(inputfile, ROOT_TREE) + # 2. DataTree Create + logger.debug('Parsing inputfile data......') + FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data) + logger.debug('Done!') + TargetFv = FmmtParser.WholeFvTree.Child[0] + if TargetFv: + FvMod = FvHandler(TargetFv) + Status = 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/Source/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 += struct2stream(item.Data.Header)+ item.Data.Data + item.Data.PadData class FvHandler: - def __init__(self, NewFfs, TargetFfs) -> None: + def __init__(self, NewFfs, TargetFfs=None) -> None: self.NewFfs = NewFfs self.TargetFfs = TargetFfs self.Status = False @@ -638,3 +638,29 @@ class FvHandler: self.Status = True logger.debug('Done!') return self.Status + + def ShrinkFv(self) -> bool: + TargetFv = self.NewFfs + TargetFv.Data.Data = b'' + if not TargetFv.Data.Free_Space: + self.Status = True + else: + BlockSize = TargetFv.Data.Header.BlockMap[0].Length + New_Free_Space = TargetFv.Data.Free_Space%BlockSize + Removed_Space = TargetFv.Data.Free_Space - New_Free_Space + TargetFv.Child[-1].Data.Data = b'\xff' * New_Free_Space + TargetFv.Data.Size -= Removed_Space + TargetFv.Data.Header.Fvlength = TargetFv.Data.Size + ModifyFvSystemGuid(TargetFv) + for item in TargetFv.Child: + if item.type == FFS_FREE_SPACE: + TargetFv.Data.Data += item.Data.Data + item.Data.PadData + else: + TargetFv.Data.Data += 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 = True + return self.Status -- 2.27.0.windows.1