-
Notifications
You must be signed in to change notification settings - Fork 3
/
sort_dts.py
executable file
·211 lines (150 loc) · 5.45 KB
/
sort_dts.py
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
#!/usr/bin/env python3
import re
import os
import sys
import fdt
from fdt_extra import *
def for_each_node(node, fn, *args, max_recurse_level=-1, recurse_level=0, **kwargs):
if node is None:
return
if max_recurse_level != -1 and recurse_level > max_recurse_level:
return
fn(node, *args, **kwargs)
for child_node in node.nodes:
for_each_node(
child_node, fn, *args,
max_recurse_level=max_recurse_level,
recurse_level=recurse_level + 1,
**kwargs)
def replace_phandle_with_label(path, name, addr, label=None, phandle_labels_map=None):
index = addr // 4
node = dt.get_node(path)
ref_prop = node.get_property(name)
if type(ref_prop) == fdt.PropWords:
new_prop = PropWordsWithPhandles(name, *ref_prop.data)
node.remove_property(name)
node.append(new_prop)
elif type(ref_prop) == PropWordsWithPhandles:
new_prop = ref_prop
else:
raise ValueError()
if not label:
phandle = new_prop.data[index]
label = phandle_labels_map[phandle][0]
new_prop.set_phandle_name(index, label)
def dt_fill_fixups(dt):
fixups_node = dt.root.get_subnode('__fixups__')
if fixups_node is None:
return
fixups_props = {}
for prop in fixups_node.props:
for value in prop.data:
node_path, prop_name, prop_data_index = value.split(':')
prop_data_index = int(prop_data_index)
replace_phandle_with_label(node_path, prop_name, prop_data_index,
prop.name)
dt.root.remove_subnode('__fixups__')
def dt_fill_symbols(dt):
SYMBOLS = '__symbols__'
symbols_node = dt.root.get_subnode(SYMBOLS)
phandle_labels_map = {}
if symbols_node is None:
return
for prop in symbols_node.props:
assert type(prop) == fdt.PropStrings
label = prop.name
path = prop.value
node = dt.get_node(path)
phandle = node.get_property('phandle').value
if phandle not in phandle_labels_map:
phandle_labels_map[phandle] = []
phandle_labels_map[phandle].append(label)
if node.label:
label = f'{node.label}: {label}'
node.set_label(label)
dt.root.remove_subnode(SYMBOLS)
LOCAL_FIXUPS = '__local_fixups__'
local_fixups_node = dt.root.get_subnode(LOCAL_FIXUPS)
if local_fixups_node is None:
return
def replace_phandles_with_label(node):
for prop in node.props:
abs_node_path = prop.path.removeprefix(f'/{LOCAL_FIXUPS}')
for value in prop.data:
replace_phandle_with_label(abs_node_path, prop.name, value,
phandle_labels_map=phandle_labels_map)
for_each_node(local_fixups_node, replace_phandles_with_label)
dt.root.remove_subnode(LOCAL_FIXUPS)
def sort_props(prop):
return str(prop)
def sort_nodes(node):
return node.name
def sort_node(node):
node._props.sort(key=sort_props)
node._nodes.sort(key=sort_nodes)
def dt_sort_nodes(dt):
for_each_node(dt.root, sort_node)
def node_is_fragment(node):
return node.name.startswith('fragment@')
def dt_extract_overlays(dt):
overlay_nodes = {}
for node in dt.root.nodes:
if not node_is_fragment(node):
continue
overlay_target = node.get_property('target')
assert type(overlay_target) == PropWordsWithPhandles
overlay_label = overlay_target.get_phandle_name(0)
if overlay_label not in overlay_nodes:
overlay_nodes[overlay_label] = []
overlay_node = node.get_subnode('__overlay__')
overlay_node.set_name(f'&{overlay_label}')
overlay_nodes[overlay_label].append(overlay_node)
names = [node.name for node in overlay_nodes[overlay_label]]
dt.root._nodes = [node for node in dt.root._nodes if not node_is_fragment(node)]
merged_overlays = []
for label in overlay_nodes:
overlays = overlay_nodes[label]
first_overlay = overlays[0]
for overlay in overlays[1:]:
first_overlay.merge(overlay, replace=True)
sort_node(first_overlay)
merged_overlays.append(first_overlay)
merged_overlays.sort(key=sort_nodes)
return merged_overlays
def remove_phandle(node):
node.remove_property('phandle')
def dt_remove_phandles(dt):
for_each_node(dt.root, remove_phandle)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit(1)
dts_file = sys.argv[1]
if len(sys.argv) == 3:
out_dts_file = sys.argv[2]
else:
out_dts_file = None
if dts_file.endswith('.dts') or dts_file.endswith('.dtsi'):
with open(dts_file, 'r') as f:
dts_text = f.read()
dt = fdt.parse_dts(dts_text)
elif dts_file.endswith('.dtb') or dts_file.endswith('.dtbo'):
with open(dts_file, 'rb') as f:
dtb_bin = f.read()
dt = fdt.parse_dtb(dtb_bin)
if out_dts_file is None:
dts_file_without_ext = os.path.splitext(dts_file)[0]
out_dts_file = dts_file_without_ext + '.dts'
else:
raise ValueError(f'Invalid file extension')
dt_fill_fixups(dt)
dt_fill_symbols(dt)
dt_remove_phandles(dt)
dt_sort_nodes(dt)
overlays = dt_extract_overlays(dt)
with open(out_dts_file, 'w') as f:
f.write(dt.to_dts())
if overlays:
f.write('\n')
for overlay in overlays:
f.write(overlay.to_dts())
f.write('\n')