]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/FMMT/FMMT.py
BaseTools: Add FMMT Python Tool
[mirror_edk2.git] / BaseTools / Source / Python / FMMT / FMMT.py
diff --git a/BaseTools/Source/Python/FMMT/FMMT.py b/BaseTools/Source/Python/FMMT/FMMT.py
new file mode 100644 (file)
index 0000000..10800e7
--- /dev/null
@@ -0,0 +1,153 @@
+# @file\r
+#  Firmware Module Management Tool.\r
+#\r
+#  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+# Import Modules\r
+#\r
+import argparse\r
+from core.FMMTOperation import *\r
+\r
+parser = argparse.ArgumentParser(description='''\r
+View the Binary Structure of FD/FV/Ffs/Section, and Delete/Extract/Add/Replace a Ffs from/into a FV.\r
+''')\r
+parser.add_argument("--version", action="version", version='%(prog)s Version 1.0',\r
+                    help="Print debug information.")\r
+parser.add_argument("-v", "--View", dest="View", nargs='+',\r
+                    help="View each FV and the named files within each FV: '-v inputfile outputfile, inputfiletype(.Fd/.Fv/.ffs/.sec)'")\r
+parser.add_argument("-d", "--Delete", dest="Delete", nargs='+',\r
+                    help="Delete a Ffs from FV: '-d inputfile TargetFvName(Optional) TargetFfsName outputfile\\r
+                        If not given TargetFvName, all the existed target Ffs will be deleted'")\r
+parser.add_argument("-e", "--Extract", dest="Extract", nargs='+',\r
+                    help="Extract a Ffs Info: '-e inputfile TargetFvName(Optional) TargetFfsName outputfile\\r
+                        If not given TargetFvName, the first found target Ffs will be extracted'")\r
+parser.add_argument("-a", "--Add", dest="Add", nargs='+',\r
+                    help="Add a Ffs into a FV:'-a inputfile TargetFvName newffsfile outputfile'")\r
+parser.add_argument("-r", "--Replace", dest="Replace", nargs='+',\r
+                    help="Replace a Ffs in a FV: '-r inputfile TargetFvName(Optional) TargetFfsName newffsfile outputfile\\r
+                        If not given TargetFvName, all the existed target Ffs will be replaced with new Ffs file)'")\r
+parser.add_argument("-l", "--LayoutFileName", dest="LayoutFileName", nargs='+',\r
+                    help="The output file which saves Binary layout: '-l xxx.txt'/'-l xxx.json'\\r
+                        If only provide file format as 'txt', \\r
+                        the file will be generated with default name (Layout_'InputFileName'.txt). \\r
+                        Currently supports two formats: json, txt. More formats will be added in the future")\r
+parser.add_argument("-c", "--ConfigFilePath", dest="ConfigFilePath", nargs='+',\r
+                    help="Provide the target FmmtConf.ini file path: '-c C:\Code\FmmtConf.ini' \\r
+                        FmmtConf file saves the target guidtool used in compress/uncompress process.\\r
+                        If do not provide, FMMT tool will search the inputfile folder for FmmtConf.ini firstly, if not found,\\r
+                        the FmmtConf.ini saved in FMMT tool's folder will be used as default.")\r
+\r
+def print_banner():\r
+    print("")\r
+\r
+class FMMT():\r
+    def __init__(self) -> None:\r
+        self.firmware_packet = {}\r
+\r
+    def SetConfigFilePath(self, configfilepath:str) -> str:\r
+        os.environ['FmmtConfPath'] = os.path.abspath(configfilepath)\r
+\r
+    def SetDestPath(self, inputfile:str) -> str:\r
+        os.environ['FmmtConfPath'] = ''\r
+        self.dest_path = os.path.dirname(os.path.abspath(inputfile))\r
+        old_env = os.environ['PATH']\r
+        os.environ['PATH'] = self.dest_path + os.pathsep + old_env\r
+\r
+    def CheckFfsName(self, FfsName:str) -> str:\r
+        try:\r
+            return uuid.UUID(FfsName)\r
+        except:\r
+            return FfsName\r
+\r
+    def GetFvName(self, FvName:str) -> str:\r
+        try:\r
+            return uuid.UUID(FvName)\r
+        except:\r
+            return FvName\r
+\r
+    def View(self, inputfile: str, layoutfilename: str=None, outputfile: str=None) -> None:\r
+        # ViewFile(inputfile, ROOT_TYPE, logfile, outputfile)\r
+        self.SetDestPath(inputfile)\r
+        filetype = os.path.splitext(inputfile)[1].lower()\r
+        if filetype == '.fd':\r
+            ROOT_TYPE = ROOT_TREE\r
+        elif filetype == '.fv':\r
+            ROOT_TYPE = ROOT_FV_TREE\r
+        elif filetype == '.ffs':\r
+            ROOT_TYPE = ROOT_FFS_TREE\r
+        elif filetype == '.sec':\r
+            ROOT_TYPE = ROOT_SECTION_TREE\r
+        else:\r
+            ROOT_TYPE = ROOT_TREE\r
+        ViewFile(inputfile, ROOT_TYPE, layoutfilename, outputfile)\r
+\r
+    def Delete(self, inputfile: str, TargetFfs_name: str, outputfile: str, Fv_name: str=None) -> None:\r
+        self.SetDestPath(inputfile)\r
+        if Fv_name:\r
+            DeleteFfs(inputfile, self.CheckFfsName(TargetFfs_name), outputfile, self.GetFvName(Fv_name))\r
+        else:\r
+            DeleteFfs(inputfile, self.CheckFfsName(TargetFfs_name), outputfile)\r
+\r
+    def Extract(self, inputfile: str, Ffs_name: str, outputfile: str, Fv_name: str=None) -> None:\r
+        self.SetDestPath(inputfile)\r
+        if Fv_name:\r
+            ExtractFfs(inputfile, self.CheckFfsName(Ffs_name), outputfile, self.GetFvName(Fv_name))\r
+        else:\r
+            ExtractFfs(inputfile, self.CheckFfsName(Ffs_name), outputfile)\r
+\r
+    def Add(self, inputfile: str, Fv_name: str, newffsfile: str, outputfile: str) -> None:\r
+        self.SetDestPath(inputfile)\r
+        AddNewFfs(inputfile, self.CheckFfsName(Fv_name), newffsfile, outputfile)\r
+\r
+    def Replace(self,inputfile: str, Ffs_name: str, newffsfile: str, outputfile: str, Fv_name: str=None) -> None:\r
+        self.SetDestPath(inputfile)\r
+        if Fv_name:\r
+            ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile, outputfile, self.GetFvName(Fv_name))\r
+        else:\r
+            ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile, outputfile)\r
+\r
+\r
+def main():\r
+    args=parser.parse_args()\r
+    status=0\r
+\r
+    try:\r
+        fmmt=FMMT()\r
+        if args.ConfigFilePath:\r
+            fmmt.SetConfigFilePath(args.ConfigFilePath[0])\r
+        if args.View:\r
+            if args.LayoutFileName:\r
+                fmmt.View(args.View[0], args.LayoutFileName[0])\r
+            else:\r
+                fmmt.View(args.View[0])\r
+        elif args.Delete:\r
+            if len(args.Delete) == 4:\r
+                fmmt.Delete(args.Delete[0],args.Delete[2],args.Delete[3],args.Delete[1])\r
+            else:\r
+                fmmt.Delete(args.Delete[0],args.Delete[1],args.Delete[2])\r
+        elif args.Extract:\r
+            if len(args.Extract) == 4:\r
+                fmmt.Extract(args.Extract[0],args.Extract[2],args.Extract[3], args.Extract[1])\r
+            else:\r
+                fmmt.Extract(args.Extract[0],args.Extract[1],args.Extract[2])\r
+        elif args.Add:\r
+            fmmt.Add(args.Add[0],args.Add[1],args.Add[2],args.Add[3])\r
+        elif args.Replace:\r
+            if len(args.Replace) == 5:\r
+                fmmt.Replace(args.Replace[0],args.Replace[2],args.Replace[3],args.Replace[4],args.Replace[1])\r
+            else:\r
+                fmmt.Replace(args.Replace[0],args.Replace[1],args.Replace[2],args.Replace[3])\r
+        else:\r
+            parser.print_help()\r
+    except Exception as e:\r
+        print(e)\r
+\r
+    return status\r
+\r
+\r
+if __name__ == "__main__":\r
+    exit(main())\r