1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
## @file
# This file is used to define the Bios layout tree structure and related operations.
#
# Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
import collections
from FirmwareStorageFormat.Common import *
from utils.FmmtLogger import FmmtLogger as logger
ROOT_TREE = 'ROOT'
ROOT_FV_TREE = 'ROOT_FV_TREE'
ROOT_FFS_TREE = 'ROOT_FFS_TREE'
ROOT_SECTION_TREE = 'ROOT_SECTION_TREE'
ROOT_ELF_TREE = 'ROOT_ELF_TREE'
FV_TREE = 'FV'
DATA_FV_TREE = 'DATA_FV'
FFS_TREE = 'FFS'
FFS_PAD = 'FFS_PAD'
FFS_FREE_SPACE = 'FFS_FREE_SPACE'
SECTION_TREE = 'SECTION'
SEC_FV_TREE = 'SEC_FV_IMAGE'
BINARY_DATA = 'BINARY'
ELF_TREE = 'ELF'
RootType = [ROOT_TREE, ROOT_FV_TREE, ROOT_FFS_TREE, ROOT_SECTION_TREE]
FvType = [FV_TREE, SEC_FV_TREE]
FfsType = FFS_TREE
SecType = SECTION_TREE
ElfType = [ROOT_ELF_TREE, ELF_TREE]
class BIOSTREE:
def __init__(self, NodeName: str) -> None:
self.key = NodeName
self.type = None
self.Data = None
self.Child = []
self.Findlist = []
self.Parent = None
self.NextRel = None
self.LastRel = None
def HasChild(self) -> bool:
if self.Child == []:
return False
else:
return True
def isFinalChild(self) -> bool:
ParTree = self.Parent
if ParTree:
if ParTree.Child[-1] == self:
return True
return False
# FvTree.insertChild()
def insertChild(self, newNode, pos: int=None) -> None:
if len(self.Child) == 0:
self.Child.append(newNode)
else:
if not pos or pos == len(self.Child):
LastTree = self.Child[-1]
self.Child.append(newNode)
LastTree.NextRel = newNode
newNode.LastRel = LastTree
else:
newNode.NextRel = self.Child[pos-1].NextRel
newNode.LastRel = self.Child[pos].LastRel
self.Child[pos-1].NextRel = newNode
self.Child[pos].LastRel = newNode
self.Child.insert(pos, newNode)
newNode.Parent = self
# lastNode.insertRel(newNode)
def insertRel(self, newNode) -> None:
if self.Parent:
parentTree = self.Parent
new_index = parentTree.Child.index(self) + 1
parentTree.Child.insert(new_index, newNode)
self.NextRel = newNode
newNode.LastRel = self
def deleteNode(self, deletekey: str) -> None:
FindStatus, DeleteTree = self.FindNode(deletekey)
if FindStatus:
parentTree = DeleteTree.Parent
lastTree = DeleteTree.LastRel
nextTree = DeleteTree.NextRel
if parentTree:
index = parentTree.Child.index(DeleteTree)
del parentTree.Child[index]
if lastTree and nextTree:
lastTree.NextRel = nextTree
nextTree.LastRel = lastTree
elif lastTree:
lastTree.NextRel = None
elif nextTree:
nextTree.LastRel = None
return DeleteTree
else:
logger.error('Could not find the target tree')
return None
def FindNode(self, key: str, Findlist: list) -> None:
if self.key == key or (self.Data and self.Data.Name == key) or (self.type == FFS_TREE and self.Data.UiName == key):
Findlist.append(self)
for item in self.Child:
item.FindNode(key, Findlist)
def GetTreePath(self):
BiosTreePath = [self]
while self.Parent:
BiosTreePath.insert(0, self.Parent)
self = self.Parent
return BiosTreePath
def parserTree(self, TargetDict: dict=None, Info: list=None, space: int=0, ParFvId="") -> None:
Key = list(TargetDict.keys())[0]
if TargetDict[Key]["Type"] in RootType:
Info.append("Image File: {}".format(Key))
Info.append("FilesNum: {}".format(TargetDict.get(Key).get('FilesNum')))
Info.append("\n")
elif TargetDict[Key]["Type"] == ROOT_ELF_TREE:
Info.append("ELF File: {}\n".format(Key))
elif TargetDict[Key]["Type"] == ELF_TREE:
ProducerId = ""
ImageId = ""
if TargetDict.get(Key).get('IfExist'):
Identifier = TargetDict.get(Key).get('Identifier')
for item in TargetDict.get(Key).get('ProducerId'):
ProducerId += chr(item)
for item in TargetDict.get(Key).get('ImageId'):
ImageId += chr(item)
Info.append("- UNIVERSAL_PAYLOAD_INFO")
Info.append(" - 4 bytes align: {}".format(TargetDict.get(Key).get('Upld_Info_Align')))
Info.append(" - Identifier: {} # 0x48444c50--PLDH / 0x444c5055--UPLD".format(hex(Identifier)))
Info.append(" - SpecRevision: {}".format(hex(TargetDict.get(Key).get('SpecRevision'))))
Info.append(" - Attribute: {}".format(hex(TargetDict.get(Key).get('Attribute'))))
Info.append(" - Revision: {}".format(hex(TargetDict.get(Key).get('Revision'))))
Info.append(" - Capability: {}".format(hex(TargetDict.get(Key).get('Capability'))))
Info.append(" - ProducerId: {}".format(ProducerId))
Info.append(" - ImageId: {}".format(ImageId))
Info.append("\n")
Info.append("- UPLD buffer")
Info.append(" Buffer: {}".format(TargetDict.get(Key).get('Upld_Buffer')))
else:
print("Do not find the Upld Info section!!!\n")
elif TargetDict[Key]["Type"] in FvType:
space += 2
if TargetDict[Key]["Type"] == SEC_FV_TREE:
Info.append("{}Child FV named {} of {}".format(space*" ", Key, ParFvId))
space += 2
else:
Info.append("FvId: {}".format(Key))
ParFvId = Key
Info.append("{}FvNameGuid: {}".format(space*" ", TargetDict.get(Key).get('FvNameGuid')))
Info.append("{}Attributes: {}".format(space*" ", TargetDict.get(Key).get('Attributes')))
Info.append("{}Total Volume Size: {}".format(space*" ", TargetDict.get(Key).get('Size')))
Info.append("{}Free Volume Size: {}".format(space*" ", TargetDict.get(Key).get('FreeSize')))
Info.append("{}Volume Offset: {}".format(space*" ", TargetDict.get(Key).get('Offset')))
Info.append("{}FilesNum: {}".format(space*" ", TargetDict.get(Key).get('FilesNum')))
elif TargetDict[Key]["Type"] in FfsType:
space += 2
if TargetDict.get(Key).get('UiName') != "b''":
Info.append("{}File: {} / {}".format(space*" ", Key, TargetDict.get(Key).get('UiName')))
else:
Info.append("{}File: {}".format(space*" ", Key))
if "Files" in list(TargetDict[Key].keys()):
for item in TargetDict[Key]["Files"]:
self.parserTree(item, Info, space, ParFvId)
def ExportTree(self,TreeInfo: dict=None) -> dict:
if TreeInfo is None:
TreeInfo =collections.OrderedDict()
if self.type == ROOT_TREE or self.type == ROOT_FV_TREE or self.type == ROOT_FFS_TREE or self.type == ROOT_SECTION_TREE or self.type == ROOT_ELF_TREE:
key = str(self.key)
TreeInfo[self.key] = collections.OrderedDict()
TreeInfo[self.key]["Name"] = key
TreeInfo[self.key]["Type"] = self.type
TreeInfo[self.key]["FilesNum"] = len(self.Child)
elif self.type == ELF_TREE:
key = str(self.Data.Name)
TreeInfo[key] = collections.OrderedDict()
TreeInfo[key]["Name"] = key
TreeInfo[key]["Type"] = self.type
TreeInfo[key]["IfExist"] = self.Data.UpldInfo
if self.Data.UpldInfo:
TreeInfo[key]["Identifier"] = self.Data.UpldInfo.Identifier
TreeInfo[key]["SpecRevision"] = self.Data.UpldInfo.SpecRevision
TreeInfo[key]["Attribute"] = self.Data.UpldInfo.Attribute
TreeInfo[key]["Revision"] = self.Data.UpldInfo.Revision
TreeInfo[key]["Capability"] = self.Data.UpldInfo.Capability
TreeInfo[key]["ProducerId"] = self.Data.UpldInfo.ProducerId
TreeInfo[key]["ImageId"] = self.Data.UpldInfo.ImageId
TreeInfo[key]["Upld_Info_Align"] = self.Data.Upld_Info_Align
TreeInfo[key]["Upld_Buffer"] = self.Data.UpldBuffer
elif self.type == FV_TREE or self.type == SEC_FV_TREE:
key = str(self.Data.FvId)
TreeInfo[key] = collections.OrderedDict()
TreeInfo[key]["Name"] = key
if self.Data.FvId != self.Data.Name:
TreeInfo[key]["FvNameGuid"] = str(self.Data.Name)
TreeInfo[key]["Type"] = self.type
TreeInfo[key]["Attributes"] = hex(self.Data.Header.Attributes)
TreeInfo[key]["Size"] = hex(self.Data.Header.FvLength)
TreeInfo[key]["FreeSize"] = hex(self.Data.Free_Space)
TreeInfo[key]["Offset"] = hex(self.Data.HOffset)
TreeInfo[key]["FilesNum"] = len(self.Child)
elif self.type == FFS_TREE:
key = str(self.Data.Name)
TreeInfo[key] = collections.OrderedDict()
TreeInfo[key]["Name"] = key
TreeInfo[key]["UiName"] = '{}'.format(self.Data.UiName)
TreeInfo[key]["Version"] = '{}'.format(self.Data.Version)
TreeInfo[key]["Type"] = self.type
TreeInfo[key]["Size"] = hex(self.Data.Size)
TreeInfo[key]["Offset"] = hex(self.Data.HOffset)
TreeInfo[key]["FilesNum"] = len(self.Child)
elif self.type == SECTION_TREE and self.Data.Type == 0x02:
key = str(self.Data.Name)
TreeInfo[key] = collections.OrderedDict()
TreeInfo[key]["Name"] = key
TreeInfo[key]["Type"] = self.type
TreeInfo[key]["Size"] = hex(len(self.Data.OriData) + self.Data.HeaderLength)
TreeInfo[key]["DecompressedSize"] = hex(self.Data.Size)
TreeInfo[key]["Offset"] = hex(self.Data.HOffset)
TreeInfo[key]["FilesNum"] = len(self.Child)
elif self is not None:
key = str(self.Data.Name)
TreeInfo[key] = collections.OrderedDict()
TreeInfo[key]["Name"] = key
TreeInfo[key]["Type"] = self.type
TreeInfo[key]["Size"] = hex(self.Data.Size)
TreeInfo[key]["Offset"] = hex(self.Data.HOffset)
TreeInfo[key]["FilesNum"] = len(self.Child)
for item in self.Child:
TreeInfo[key].setdefault('Files',[]).append( item.ExportTree())
return TreeInfo
|