-
Notifications
You must be signed in to change notification settings - Fork 1
/
dts_cleaner.py
201 lines (162 loc) · 6.69 KB
/
dts_cleaner.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
import os
import re
import sys
PHANDLE_VARS_OUTPUT="phandles.txt"
PHANDLE_PATTERN = re.compile(r'^( |\t)*phandle = <(.+?)>;$', re.MULTILINE)
VAR_SET_PATTERN = re.compile(r'^( |\t)*([^\n]*?)( |\t)=( |\t)?[^;]*?(<.*?>);',
re.MULTILINE | re.DOTALL)
SUBVALUES_PATTERN = re.compile(r'<(.+?)>')
def main():
if len(sys.argv) != 2:
print("Usage: dts_cleaner.py <dts>")
print("This script will automatically detect if the dts" +
"was grabbed from the kernel source code or from a compiled DTB.\n" +
"In the first case, it'll write to 'phandles.txt' which variables" +
"are phandles, while in the second case it'll replace in a copy" +
"of the dts the value of these variables with their actual pointer name.")
sys.exit(1)
dts_path = sys.argv[1]
if not os.path.isfile(dts_path):
print(f"File not found: '{dts_path}'")
sys.exit(2)
with open(dts_path, 'r') as dts:
content = dts.read()
if re.search(PHANDLE_PATTERN, content) is None:
print("DTS from kernel source detected, exporting phandle variables...")
export_phandle_vars(content)
else:
out_file = f"{dts_path}_cleaned"
print(f"DTS from compiled DTB detected, replacing phandles " +
f"and saving to '{out_file}'...")
replace_phandles(content, out_file)
print("Done!")
def export_phandle_vars(content: str):
phandle_vars = set()
for match in re.findall(VAR_SET_PATTERN, content):
var_name = match[1]
value = match[4]
if not '&' in value:
continue
i = 0
for subvalues in re.findall(SUBVALUES_PATTERN, value):
for subvalue in subvalues.split(' '):
subvalue = subvalue.strip()
if subvalue.startswith('&'):
phandle_vars.add(f'{var_name};{i}')
i += 1
if not os.path.isfile(PHANDLE_VARS_OUTPUT):
open(PHANDLE_VARS_OUTPUT, 'w').close()
with open(PHANDLE_VARS_OUTPUT, 'r+') as out:
stored_vars = out.readlines()
added_amount = len(phandle_vars)
for var in stored_vars:
var_name = var.removesuffix('\n')
if not var_name in phandle_vars:
phandle_vars.add(var_name)
else:
added_amount -= 1
# We also want 'phandle = <X>;' replaced
if not "phandle;0" in phandle_vars:
phandle_vars.add("phandle;0")
added_amount += 1
out.seek(0)
out.writelines(f'{v}\n' for v in phandle_vars)
print(f"Added {added_amount} phandle variable names to " +
f"{PHANDLE_VARS_OUTPUT}, adding a total of {len(phandle_vars)}")
def read_symbols(content: str) -> dict[str, str]:
symbols_pattern = re.compile(r'( |\t)*__symbols__ {\n(.+?)};', re.DOTALL)
m = re.search(symbols_pattern, content)
if m is None:
print("Error: Couldn't find symbols")
sys.exit(4)
symbols = m.group(2).splitlines()
symbols_dict = {}
for sym in symbols:
sym = sym.strip()
if len(sym) == 0:
continue
m = re.match(r'^(.+?) = "(.+?)";$', sym)
if m is None:
print(f"Warning: Couldn't parse symbol '{repr(sym)}'")
continue
symbols_dict[m.group(2)] = m.group(1)
print(f"Registered {len(symbols_dict)} symbols")
return symbols_dict
def read_phandle_paths(content: str) -> dict[str, str]:
current_path = ""
paths = {}
for line in content.splitlines():
line = line.strip()
if line.endswith('{'):
# print(f"open {line}")
current_path += f"{line.strip(' {')}"
if not current_path.endswith('/'):
current_path += "/"
# print(current_path)
continue
elif line.endswith('};'):
# print(line)
current_path = current_path.removesuffix('/')
if len(current_path) > 0:
current_path = current_path[:current_path.rindex('/') + 1]
if len(current_path) == 0:
current_path = "/"
continue
m = re.match(PHANDLE_PATTERN, line)
if m is None:
continue
paths[m.group(2)] = current_path
print(f"Registered the path of {len(paths)} phandles")
return paths
def replace_phandles(content: str, out_file: str):
if not os.path.isfile(PHANDLE_VARS_OUTPUT):
print(f"Couldn't find '{PHANDLE_VARS_OUTPUT}'")
print("Tip: You need to first run this script passing it " +
"one or more DTS files from the kernel source code")
sys.exit(3)
symbols = read_symbols(content)
paths = read_phandle_paths(content)
if len(symbols) != len(paths):
print(f"Warning: Amount of symbols doesn't match amount of " +
"phandle paths, something may have gone wrong")
lines = content.splitlines()
with open(PHANDLE_VARS_OUTPUT, 'r') as f:
phandle_vars: dict[str, set[int]] = {}
for entry in f.readlines():
splitted = entry.split(';')
if len(splitted) != 2:
continue
var = splitted[0]
index = splitted[1].strip()
index = int(index)
phandle_vars.setdefault(var, set()).add(index)
print("Replacing phandles... please wait")
replaced=0
for line_i, line in enumerate(lines):
m = re.match(VAR_SET_PATTERN, line)
if m is None:
continue
var_name = m.group(2)
value = m.group(5)
if not var_name in phandle_vars.keys():
continue
value = value.removeprefix('<').removesuffix('>')
new_value = ''
for i, subvalue in enumerate(value.split(' ')):
if not i in phandle_vars[var_name]:
new_value += f' {subvalue}'
continue
phandle = subvalue.strip()
if not phandle in paths.keys():
print(f'{var_name} ; {i}')
print(f"Warning: Couldn't find path of phandle '{phandle}'")
continue
sym = symbols[paths[phandle].removesuffix('/')]
new_value += f' &{sym}'
replaced += 1
lines[line_i] = lines[line_i].replace(value, new_value.strip())
print(f'{replaced} phandle references replaced with their symbol')
with open(out_file, 'w') as out:
out.writelines(f'{l}\n' for l in lines)
if __name__ == "__main__":
main()