]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/FMMT/FMMT.py
2 # Firmware Module Management Tool.
4 # Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
13 from core
.FMMTOperation
import *
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.
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.")
48 def __init__(self
) -> None:
49 self
.firmware_packet
= {}
51 def SetConfigFilePath(self
, configfilepath
:str) -> str:
52 os
.environ
['FmmtConfPath'] = os
.path
.abspath(configfilepath
)
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
60 def CheckFfsName(self
, FfsName
:str) -> str:
62 return uuid
.UUID(FfsName
)
66 def GetFvName(self
, FvName
:str) -> str:
68 return uuid
.UUID(FvName
)
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()
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
86 ViewFile(inputfile
, ROOT_TYPE
, layoutfilename
, outputfile
)
88 def Delete(self
, inputfile
: str, TargetFfs_name
: str, outputfile
: str, Fv_name
: str=None) -> None:
89 self
.SetDestPath(inputfile
)
91 DeleteFfs(inputfile
, self
.CheckFfsName(TargetFfs_name
), outputfile
, self
.GetFvName(Fv_name
))
93 DeleteFfs(inputfile
, self
.CheckFfsName(TargetFfs_name
), outputfile
)
95 def Extract(self
, inputfile
: str, Ffs_name
: str, outputfile
: str, Fv_name
: str=None) -> None:
96 self
.SetDestPath(inputfile
)
98 ExtractFfs(inputfile
, self
.CheckFfsName(Ffs_name
), outputfile
, self
.GetFvName(Fv_name
))
100 ExtractFfs(inputfile
, self
.CheckFfsName(Ffs_name
), outputfile
)
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
)
106 def Replace(self
,inputfile
: str, Ffs_name
: str, newffsfile
: str, outputfile
: str, Fv_name
: str=None) -> None:
107 self
.SetDestPath(inputfile
)
109 ReplaceFfs(inputfile
, self
.CheckFfsName(Ffs_name
), newffsfile
, outputfile
, self
.GetFvName(Fv_name
))
111 ReplaceFfs(inputfile
, self
.CheckFfsName(Ffs_name
), newffsfile
, outputfile
)
115 args
=parser
.parse_args()
120 if args
.ConfigFilePath
:
121 fmmt
.SetConfigFilePath(args
.ConfigFilePath
[0])
123 if args
.LayoutFileName
:
124 fmmt
.View(args
.View
[0], args
.LayoutFileName
[0])
126 fmmt
.View(args
.View
[0])
128 if len(args
.Delete
) == 4:
129 fmmt
.Delete(args
.Delete
[0],args
.Delete
[2],args
.Delete
[3],args
.Delete
[1])
131 fmmt
.Delete(args
.Delete
[0],args
.Delete
[1],args
.Delete
[2])
133 if len(args
.Extract
) == 4:
134 fmmt
.Extract(args
.Extract
[0],args
.Extract
[2],args
.Extract
[3], args
.Extract
[1])
136 fmmt
.Extract(args
.Extract
[0],args
.Extract
[1],args
.Extract
[2])
138 fmmt
.Add(args
.Add
[0],args
.Add
[1],args
.Add
[2],args
.Add
[3])
140 if len(args
.Replace
) == 5:
141 fmmt
.Replace(args
.Replace
[0],args
.Replace
[2],args
.Replace
[3],args
.Replace
[4],args
.Replace
[1])
143 fmmt
.Replace(args
.Replace
[0],args
.Replace
[1],args
.Replace
[2],args
.Replace
[3])
146 except Exception as e
:
152 if __name__
== "__main__":