]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/FMMT/FMMT.py
BaseTools: Add FMMT Python Tool
[mirror_edk2.git] / BaseTools / Source / Python / FMMT / FMMT.py
CommitLineData
a64b9449
CC
1# @file\r
2# Firmware Module Management Tool.\r
3#\r
4# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
5#\r
6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
7#\r
8##\r
9\r
10# Import Modules\r
11#\r
12import argparse\r
13from core.FMMTOperation import *\r
14\r
15parser = argparse.ArgumentParser(description='''\r
16View the Binary Structure of FD/FV/Ffs/Section, and Delete/Extract/Add/Replace a Ffs from/into a FV.\r
17''')\r
18parser.add_argument("--version", action="version", version='%(prog)s Version 1.0',\r
19 help="Print debug information.")\r
20parser.add_argument("-v", "--View", dest="View", nargs='+',\r
21 help="View each FV and the named files within each FV: '-v inputfile outputfile, inputfiletype(.Fd/.Fv/.ffs/.sec)'")\r
22parser.add_argument("-d", "--Delete", dest="Delete", nargs='+',\r
23 help="Delete a Ffs from FV: '-d inputfile TargetFvName(Optional) TargetFfsName outputfile\\r
24 If not given TargetFvName, all the existed target Ffs will be deleted'")\r
25parser.add_argument("-e", "--Extract", dest="Extract", nargs='+',\r
26 help="Extract a Ffs Info: '-e inputfile TargetFvName(Optional) TargetFfsName outputfile\\r
27 If not given TargetFvName, the first found target Ffs will be extracted'")\r
28parser.add_argument("-a", "--Add", dest="Add", nargs='+',\r
29 help="Add a Ffs into a FV:'-a inputfile TargetFvName newffsfile outputfile'")\r
30parser.add_argument("-r", "--Replace", dest="Replace", nargs='+',\r
31 help="Replace a Ffs in a FV: '-r inputfile TargetFvName(Optional) TargetFfsName newffsfile outputfile\\r
32 If not given TargetFvName, all the existed target Ffs will be replaced with new Ffs file)'")\r
33parser.add_argument("-l", "--LayoutFileName", dest="LayoutFileName", nargs='+',\r
34 help="The output file which saves Binary layout: '-l xxx.txt'/'-l xxx.json'\\r
35 If only provide file format as 'txt', \\r
36 the file will be generated with default name (Layout_'InputFileName'.txt). \\r
37 Currently supports two formats: json, txt. More formats will be added in the future")\r
38parser.add_argument("-c", "--ConfigFilePath", dest="ConfigFilePath", nargs='+',\r
39 help="Provide the target FmmtConf.ini file path: '-c C:\Code\FmmtConf.ini' \\r
40 FmmtConf file saves the target guidtool used in compress/uncompress process.\\r
41 If do not provide, FMMT tool will search the inputfile folder for FmmtConf.ini firstly, if not found,\\r
42 the FmmtConf.ini saved in FMMT tool's folder will be used as default.")\r
43\r
44def print_banner():\r
45 print("")\r
46\r
47class FMMT():\r
48 def __init__(self) -> None:\r
49 self.firmware_packet = {}\r
50\r
51 def SetConfigFilePath(self, configfilepath:str) -> str:\r
52 os.environ['FmmtConfPath'] = os.path.abspath(configfilepath)\r
53\r
54 def SetDestPath(self, inputfile:str) -> str:\r
55 os.environ['FmmtConfPath'] = ''\r
56 self.dest_path = os.path.dirname(os.path.abspath(inputfile))\r
57 old_env = os.environ['PATH']\r
58 os.environ['PATH'] = self.dest_path + os.pathsep + old_env\r
59\r
60 def CheckFfsName(self, FfsName:str) -> str:\r
61 try:\r
62 return uuid.UUID(FfsName)\r
63 except:\r
64 return FfsName\r
65\r
66 def GetFvName(self, FvName:str) -> str:\r
67 try:\r
68 return uuid.UUID(FvName)\r
69 except:\r
70 return FvName\r
71\r
72 def View(self, inputfile: str, layoutfilename: str=None, outputfile: str=None) -> None:\r
73 # ViewFile(inputfile, ROOT_TYPE, logfile, outputfile)\r
74 self.SetDestPath(inputfile)\r
75 filetype = os.path.splitext(inputfile)[1].lower()\r
76 if filetype == '.fd':\r
77 ROOT_TYPE = ROOT_TREE\r
78 elif filetype == '.fv':\r
79 ROOT_TYPE = ROOT_FV_TREE\r
80 elif filetype == '.ffs':\r
81 ROOT_TYPE = ROOT_FFS_TREE\r
82 elif filetype == '.sec':\r
83 ROOT_TYPE = ROOT_SECTION_TREE\r
84 else:\r
85 ROOT_TYPE = ROOT_TREE\r
86 ViewFile(inputfile, ROOT_TYPE, layoutfilename, outputfile)\r
87\r
88 def Delete(self, inputfile: str, TargetFfs_name: str, outputfile: str, Fv_name: str=None) -> None:\r
89 self.SetDestPath(inputfile)\r
90 if Fv_name:\r
91 DeleteFfs(inputfile, self.CheckFfsName(TargetFfs_name), outputfile, self.GetFvName(Fv_name))\r
92 else:\r
93 DeleteFfs(inputfile, self.CheckFfsName(TargetFfs_name), outputfile)\r
94\r
95 def Extract(self, inputfile: str, Ffs_name: str, outputfile: str, Fv_name: str=None) -> None:\r
96 self.SetDestPath(inputfile)\r
97 if Fv_name:\r
98 ExtractFfs(inputfile, self.CheckFfsName(Ffs_name), outputfile, self.GetFvName(Fv_name))\r
99 else:\r
100 ExtractFfs(inputfile, self.CheckFfsName(Ffs_name), outputfile)\r
101\r
102 def Add(self, inputfile: str, Fv_name: str, newffsfile: str, outputfile: str) -> None:\r
103 self.SetDestPath(inputfile)\r
104 AddNewFfs(inputfile, self.CheckFfsName(Fv_name), newffsfile, outputfile)\r
105\r
106 def Replace(self,inputfile: str, Ffs_name: str, newffsfile: str, outputfile: str, Fv_name: str=None) -> None:\r
107 self.SetDestPath(inputfile)\r
108 if Fv_name:\r
109 ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile, outputfile, self.GetFvName(Fv_name))\r
110 else:\r
111 ReplaceFfs(inputfile, self.CheckFfsName(Ffs_name), newffsfile, outputfile)\r
112\r
113\r
114def main():\r
115 args=parser.parse_args()\r
116 status=0\r
117\r
118 try:\r
119 fmmt=FMMT()\r
120 if args.ConfigFilePath:\r
121 fmmt.SetConfigFilePath(args.ConfigFilePath[0])\r
122 if args.View:\r
123 if args.LayoutFileName:\r
124 fmmt.View(args.View[0], args.LayoutFileName[0])\r
125 else:\r
126 fmmt.View(args.View[0])\r
127 elif args.Delete:\r
128 if len(args.Delete) == 4:\r
129 fmmt.Delete(args.Delete[0],args.Delete[2],args.Delete[3],args.Delete[1])\r
130 else:\r
131 fmmt.Delete(args.Delete[0],args.Delete[1],args.Delete[2])\r
132 elif args.Extract:\r
133 if len(args.Extract) == 4:\r
134 fmmt.Extract(args.Extract[0],args.Extract[2],args.Extract[3], args.Extract[1])\r
135 else:\r
136 fmmt.Extract(args.Extract[0],args.Extract[1],args.Extract[2])\r
137 elif args.Add:\r
138 fmmt.Add(args.Add[0],args.Add[1],args.Add[2],args.Add[3])\r
139 elif args.Replace:\r
140 if len(args.Replace) == 5:\r
141 fmmt.Replace(args.Replace[0],args.Replace[2],args.Replace[3],args.Replace[4],args.Replace[1])\r
142 else:\r
143 fmmt.Replace(args.Replace[0],args.Replace[1],args.Replace[2],args.Replace[3])\r
144 else:\r
145 parser.print_help()\r
146 except Exception as e:\r
147 print(e)\r
148\r
149 return status\r
150\r
151\r
152if __name__ == "__main__":\r
153 exit(main())\r