]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/FMMT/core/FMMTOperation.py
BaseTools/FMMT: Add Shrink Fv function
[mirror_edk2.git] / BaseTools / Source / Python / FMMT / core / FMMTOperation.py
index 4e58c91b5c4128320084a839e13857d7b3ee423e..a86f8dda9a1a99edc14981781413b55c009e1c1b 100644 (file)
@@ -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))\r
     else:\r
         logger.error('Target Ffs/Fv not found!!!')\r
+\r
+def ShrinkFv(inputfile: str, outputfile: str) -> None:\r
+    if not os.path.exists(inputfile):\r
+        logger.error("Invalid inputfile, can not open {}.".format(inputfile))\r
+        raise Exception("Process Failed: Invalid inputfile!")\r
+    # 1. Data Prepare\r
+    with open(inputfile, "rb") as f:\r
+        whole_data = f.read()\r
+    FmmtParser = FMMTParser(inputfile, ROOT_TREE)\r
+    # 2. DataTree Create\r
+    logger.debug('Parsing inputfile data......')\r
+    FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)\r
+    logger.debug('Done!')\r
+    TargetFv = FmmtParser.WholeFvTree.Child[0]\r
+    if TargetFv:\r
+        FvMod = FvHandler(TargetFv)\r
+        Status = FvMod.ShrinkFv()\r
+    else:\r
+        logger.error('Target Fv not found!!!')\r
+    # 4. Data Encapsulation\r
+    if Status:\r
+        logger.debug('Start encapsulating data......')\r
+        FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)\r
+        with open(outputfile, "wb") as f:\r
+            f.write(FmmtParser.FinalData)\r
+        logger.debug('Encapsulated data is saved in {}.'.format(outputfile))\r