]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/FMMT/core/FMMTOperation.py
c2cc2e2467407168a1d0d8a33ab48df4c3e2a6f3
[mirror_edk2.git] / BaseTools / Source / Python / FMMT / core / FMMTOperation.py
1 ## @file
2 # This file is used to define the functions to operate bios binary file.
3 #
4 # Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 ##
7 from core.FMMTParser import *
8 from core.FvHandler import *
9 from utils.FvLayoutPrint import *
10 from utils.FmmtLogger import FmmtLogger as logger
11
12 global Fv_count
13 Fv_count = 0
14
15 # The ROOT_TYPE can be 'ROOT_TREE', 'ROOT_FV_TREE', 'ROOT_FFS_TREE', 'ROOT_SECTION_TREE'
16 def ViewFile(inputfile: str, ROOT_TYPE: str, layoutfile: str=None, outputfile: str=None) -> None:
17 if not os.path.exists(inputfile):
18 logger.error("Invalid inputfile, can not open {}.".format(inputfile))
19 raise Exception("Process Failed: Invalid inputfile!")
20 # 1. Data Prepare
21 with open(inputfile, "rb") as f:
22 whole_data = f.read()
23 FmmtParser = FMMTParser(inputfile, ROOT_TYPE)
24 # 2. DataTree Create
25 logger.debug('Parsing inputfile data......')
26 FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
27 logger.debug('Done!')
28 # 3. Log Output
29 InfoDict = FmmtParser.WholeFvTree.ExportTree()
30 logger.debug('BinaryTree created, start parsing BinaryTree data......')
31 FmmtParser.WholeFvTree.parserTree(InfoDict, FmmtParser.BinaryInfo)
32 logger.debug('Done!')
33 GetFormatter("").LogPrint(FmmtParser.BinaryInfo)
34 if layoutfile:
35 if os.path.splitext(layoutfile)[1]:
36 layoutfilename = layoutfile
37 layoutfileformat = os.path.splitext(layoutfile)[1][1:].lower()
38 else:
39 layoutfilename = "Layout_{}{}".format(os.path.basename(inputfile),".{}".format(layoutfile.lower()))
40 layoutfileformat = layoutfile.lower()
41 GetFormatter(layoutfileformat).dump(InfoDict, FmmtParser.BinaryInfo, layoutfilename)
42 # 4. Data Encapsulation
43 if outputfile:
44 logger.debug('Start encapsulating data......')
45 FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)
46 with open(outputfile, "wb") as f:
47 f.write(FmmtParser.FinalData)
48 logger.debug('Encapsulated data is saved in {}.'.format(outputfile))
49
50 def DeleteFfs(inputfile: str, TargetFfs_name: str, outputfile: str, Fv_name: str=None) -> None:
51 if not os.path.exists(inputfile):
52 logger.error("Invalid inputfile, can not open {}.".format(inputfile))
53 raise Exception("Process Failed: Invalid inputfile!")
54 # 1. Data Prepare
55 with open(inputfile, "rb") as f:
56 whole_data = f.read()
57 FmmtParser = FMMTParser(inputfile, ROOT_TREE)
58 # 2. DataTree Create
59 logger.debug('Parsing inputfile data......')
60 FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
61 logger.debug('Done!')
62 # 3. Data Modify
63 FmmtParser.WholeFvTree.FindNode(TargetFfs_name, FmmtParser.WholeFvTree.Findlist)
64 # Choose the Specfic DeleteFfs with Fv info
65 if Fv_name:
66 for item in FmmtParser.WholeFvTree.Findlist:
67 if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:
68 FmmtParser.WholeFvTree.Findlist.remove(item)
69 Status = False
70 if FmmtParser.WholeFvTree.Findlist != []:
71 for Delete_Ffs in FmmtParser.WholeFvTree.Findlist:
72 FfsMod = FvHandler(None, Delete_Ffs)
73 Status = FfsMod.DeleteFfs()
74 else:
75 logger.error('Target Ffs not found!!!')
76 # 4. Data Encapsulation
77 if Status:
78 logger.debug('Start encapsulating data......')
79 FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)
80 with open(outputfile, "wb") as f:
81 f.write(FmmtParser.FinalData)
82 logger.debug('Encapsulated data is saved in {}.'.format(outputfile))
83
84 def AddNewFfs(inputfile: str, Fv_name: str, newffsfile: str, outputfile: str) -> None:
85 if not os.path.exists(inputfile):
86 logger.error("Invalid inputfile, can not open {}.".format(inputfile))
87 raise Exception("Process Failed: Invalid inputfile!")
88 if not os.path.exists(newffsfile):
89 logger.error("Invalid ffsfile, can not open {}.".format(newffsfile))
90 raise Exception("Process Failed: Invalid ffs file!")
91 # 1. Data Prepare
92 with open(inputfile, "rb") as f:
93 whole_data = f.read()
94 FmmtParser = FMMTParser(inputfile, ROOT_TREE)
95 # 2. DataTree Create
96 logger.debug('Parsing inputfile data......')
97 FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
98 logger.debug('Done!')
99 # Get Target Fv and Target Ffs_Pad
100 FmmtParser.WholeFvTree.FindNode(Fv_name, FmmtParser.WholeFvTree.Findlist)
101 # Create new ffs Tree
102 with open(newffsfile, "rb") as f:
103 new_ffs_data = f.read()
104 NewFmmtParser = FMMTParser(newffsfile, ROOT_FFS_TREE)
105 Status = False
106 # 3. Data Modify
107 if FmmtParser.WholeFvTree.Findlist:
108 for TargetFv in FmmtParser.WholeFvTree.Findlist:
109 TargetFfsPad = TargetFv.Child[-1]
110 logger.debug('Parsing newffsfile data......')
111 if TargetFfsPad.type == FFS_FREE_SPACE:
112 NewFmmtParser.ParserFromRoot(NewFmmtParser.WholeFvTree, new_ffs_data, TargetFfsPad.Data.HOffset)
113 else:
114 NewFmmtParser.ParserFromRoot(NewFmmtParser.WholeFvTree, new_ffs_data, TargetFfsPad.Data.HOffset+TargetFfsPad.Data.Size)
115 logger.debug('Done!')
116 FfsMod = FvHandler(NewFmmtParser.WholeFvTree.Child[0], TargetFfsPad)
117 Status = FfsMod.AddFfs()
118 else:
119 logger.error('Target Fv not found!!!')
120 # 4. Data Encapsulation
121 if Status:
122 logger.debug('Start encapsulating data......')
123 FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)
124 with open(outputfile, "wb") as f:
125 f.write(FmmtParser.FinalData)
126 logger.debug('Encapsulated data is saved in {}.'.format(outputfile))
127
128 def ReplaceFfs(inputfile: str, Ffs_name: str, newffsfile: str, outputfile: str, Fv_name: str=None) -> None:
129 if not os.path.exists(inputfile):
130 logger.error("Invalid inputfile, can not open {}.".format(inputfile))
131 raise Exception("Process Failed: Invalid inputfile!")
132 # 1. Data Prepare
133 with open(inputfile, "rb") as f:
134 whole_data = f.read()
135 FmmtParser = FMMTParser(inputfile, ROOT_TREE)
136 # 2. DataTree Create
137 logger.debug('Parsing inputfile data......')
138 FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
139 logger.debug('Done!')
140 with open(newffsfile, "rb") as f:
141 new_ffs_data = f.read()
142 newFmmtParser = FMMTParser(newffsfile, FV_TREE)
143 logger.debug('Parsing newffsfile data......')
144 newFmmtParser.ParserFromRoot(newFmmtParser.WholeFvTree, new_ffs_data)
145 logger.debug('Done!')
146 Status = False
147 # 3. Data Modify
148 new_ffs = newFmmtParser.WholeFvTree.Child[0]
149 new_ffs.Data.PadData = GetPadSize(new_ffs.Data.Size, FFS_COMMON_ALIGNMENT) * b'\xff'
150 FmmtParser.WholeFvTree.FindNode(Ffs_name, FmmtParser.WholeFvTree.Findlist)
151 if Fv_name:
152 for item in FmmtParser.WholeFvTree.Findlist:
153 if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:
154 FmmtParser.WholeFvTree.Findlist.remove(item)
155 if FmmtParser.WholeFvTree.Findlist != []:
156 for TargetFfs in FmmtParser.WholeFvTree.Findlist:
157 FfsMod = FvHandler(newFmmtParser.WholeFvTree.Child[0], TargetFfs)
158 Status = FfsMod.ReplaceFfs()
159 else:
160 logger.error('Target Ffs not found!!!')
161 # 4. Data Encapsulation
162 if Status:
163 logger.debug('Start encapsulating data......')
164 FmmtParser.Encapsulation(FmmtParser.WholeFvTree, False)
165 with open(outputfile, "wb") as f:
166 f.write(FmmtParser.FinalData)
167 logger.debug('Encapsulated data is saved in {}.'.format(outputfile))
168
169 def ExtractFfs(inputfile: str, Ffs_name: str, outputfile: str, Fv_name: str=None) -> None:
170 if not os.path.exists(inputfile):
171 logger.error("Invalid inputfile, can not open {}.".format(inputfile))
172 raise Exception("Process Failed: Invalid inputfile!")
173 # 1. Data Prepare
174 with open(inputfile, "rb") as f:
175 whole_data = f.read()
176 FmmtParser = FMMTParser(inputfile, ROOT_TREE)
177 # 2. DataTree Create
178 logger.debug('Parsing inputfile data......')
179 FmmtParser.ParserFromRoot(FmmtParser.WholeFvTree, whole_data)
180 logger.debug('Done!')
181 FmmtParser.WholeFvTree.FindNode(Ffs_name, FmmtParser.WholeFvTree.Findlist)
182 if Fv_name:
183 for item in FmmtParser.WholeFvTree.Findlist:
184 if item.Parent.key != Fv_name and item.Parent.Data.Name != Fv_name:
185 FmmtParser.WholeFvTree.Findlist.remove(item)
186 if FmmtParser.WholeFvTree.Findlist != []:
187 TargetNode = FmmtParser.WholeFvTree.Findlist[0]
188 TargetFv = TargetNode.Parent
189 if TargetFv.Data.Header.Attributes & EFI_FVB2_ERASE_POLARITY:
190 TargetNode.Data.Header.State = c_uint8(
191 ~TargetNode.Data.Header.State)
192 FinalData = struct2stream(TargetNode.Data.Header) + TargetNode.Data.Data
193 with open(outputfile, "wb") as f:
194 f.write(FinalData)
195 logger.debug('Extract ffs data is saved in {}.'.format(outputfile))
196 else:
197 logger.error('Target Ffs not found!!!')