]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/FMMT/core/FMMTOperation.py
c2cc2e2467407168a1d0d8a33ab48df4c3e2a6f3
2 # This file is used to define the functions to operate bios binary file.
4 # Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 from core
.FMMTParser
import *
8 from core
.FvHandler
import *
9 from utils
.FvLayoutPrint
import *
10 from utils
.FmmtLogger
import FmmtLogger
as logger
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!")
21 with
open(inputfile
, "rb") as f
:
23 FmmtParser
= FMMTParser(inputfile
, ROOT_TYPE
)
25 logger
.debug('Parsing inputfile data......')
26 FmmtParser
.ParserFromRoot(FmmtParser
.WholeFvTree
, whole_data
)
29 InfoDict
= FmmtParser
.WholeFvTree
.ExportTree()
30 logger
.debug('BinaryTree created, start parsing BinaryTree data......')
31 FmmtParser
.WholeFvTree
.parserTree(InfoDict
, FmmtParser
.BinaryInfo
)
33 GetFormatter("").LogPrint(FmmtParser
.BinaryInfo
)
35 if os
.path
.splitext(layoutfile
)[1]:
36 layoutfilename
= layoutfile
37 layoutfileformat
= os
.path
.splitext(layoutfile
)[1][1:].lower()
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
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
))
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!")
55 with
open(inputfile
, "rb") as f
:
57 FmmtParser
= FMMTParser(inputfile
, ROOT_TREE
)
59 logger
.debug('Parsing inputfile data......')
60 FmmtParser
.ParserFromRoot(FmmtParser
.WholeFvTree
, whole_data
)
63 FmmtParser
.WholeFvTree
.FindNode(TargetFfs_name
, FmmtParser
.WholeFvTree
.Findlist
)
64 # Choose the Specfic DeleteFfs with Fv info
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
)
70 if FmmtParser
.WholeFvTree
.Findlist
!= []:
71 for Delete_Ffs
in FmmtParser
.WholeFvTree
.Findlist
:
72 FfsMod
= FvHandler(None, Delete_Ffs
)
73 Status
= FfsMod
.DeleteFfs()
75 logger
.error('Target Ffs not found!!!')
76 # 4. Data Encapsulation
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
))
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!")
92 with
open(inputfile
, "rb") as f
:
94 FmmtParser
= FMMTParser(inputfile
, ROOT_TREE
)
96 logger
.debug('Parsing inputfile data......')
97 FmmtParser
.ParserFromRoot(FmmtParser
.WholeFvTree
, whole_data
)
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
)
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
)
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()
119 logger
.error('Target Fv not found!!!')
120 # 4. Data Encapsulation
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
))
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!")
133 with
open(inputfile
, "rb") as f
:
134 whole_data
= f
.read()
135 FmmtParser
= FMMTParser(inputfile
, ROOT_TREE
)
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!')
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
)
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()
160 logger
.error('Target Ffs not found!!!')
161 # 4. Data Encapsulation
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
))
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!")
174 with
open(inputfile
, "rb") as f
:
175 whole_data
= f
.read()
176 FmmtParser
= FMMTParser(inputfile
, ROOT_TREE
)
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
)
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
:
195 logger
.debug('Extract ffs data is saved in {}.'.format(outputfile
))
197 logger
.error('Target Ffs not found!!!')