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