-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathath11k_bdf_tool.py
More file actions
249 lines (189 loc) · 8.4 KB
/
ath11k_bdf_tool.py
File metadata and controls
249 lines (189 loc) · 8.4 KB
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
243
244
245
246
247
248
249
#! /usr/bin/python
'''
The MIT License (MIT)
Copyright (c) 2024 Paweł Owoc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
'''
# usage: ath11k_bdf_tool.py [-h] (-e BDF | -u BDF REGDB | -r BDF) [-o FILE]
#
# ath11k BDF tool
#
# options:
# -h, --help show this help message and exit
# -e BDF, --extract-regdb BDF
# extract regdb from ath11k BDF
# -u BDF REGDB, --update-regdb BDF REGDB
# update regdb in ath11k BDF
# -r BDF, --remove-regdomain BDF
# remove regdomain from ath11k BDF
# -p BDF ADDR VAL, --patch-bdf BDF ADDR VAL
# patch ath11k BDF
# -o FILE, --output FILE
# output file name
import io
from argparse import ArgumentParser
from hashlib import md5
from mmap import mmap, ACCESS_COPY
from struct import pack, unpack, iter_unpack
from os.path import isfile
CHECKSUM_ADDR = 0xa
BDF_HEADER = '010004040000'
REGDB_HEADER = '0000000004003700'
REGDB_FILE = 'regdb.bin'
REGDB_BDF = {
0x127c6: 'IPQ5018/QCN6122',
0x10f46: 'IPQ5018/QCN6122(old)',
0xae2c: 'IPQ6018',
0x1978c: 'IPQ8074',
0x12dee: 'IPQ9574',
0x12dec: 'QCN9074'
}
REGDB_VERSION_ADDR = {
0x1c04: 0x17d0,
0x2804: 0x2342
}
REGDOMAIN_CODE = 0x0000
REGDOMAIN_ADDR = [
0x34,
0x450,
0x458,
0x500,
0x5a8
]
def calculate_checksum(checksum, data, new_data):
result = unpack('<H', checksum)[0]
for data_byte in iter_unpack('<H', data + new_data):
result = result ^ data_byte[0]
return(pack('<H', result))
def cmd_extract_regdb(args):
with io.open(args.extract_regdb, 'rb') as b:
bdf = mmap(b.fileno(), 0, access=ACCESS_COPY)
if bdf[0:6] != bytes.fromhex(BDF_HEADER):
exit('Not valid ath11k BDF file')
regdb_addr = bdf.find(bytes.fromhex(REGDB_HEADER))
if regdb_addr == -1:
exit('Unable to find regdb in BDF')
regdb_size = unpack('<H', bdf[regdb_addr - 2:regdb_addr])[0]
regdb_version = bdf[regdb_addr + REGDB_VERSION_ADDR.get(regdb_size)]
print(f'Extracting regdb (v{regdb_version}) from {REGDB_BDF.get(regdb_addr, "unknown")} BDF')
with io.open(args.output or REGDB_FILE, 'wb') as r:
r.write(bdf[regdb_addr:regdb_addr + regdb_size])
def cmd_update_regdb(args):
with io.open(args.update_regdb[0], 'rb') as b:
bdf = mmap(b.fileno(), 0, access=ACCESS_COPY)
if bdf[0:6] != bytes.fromhex(BDF_HEADER):
exit('Not valid ath11k BDF file')
regdb_addr = bdf.find(bytes.fromhex(REGDB_HEADER))
if regdb_addr == -1:
exit('Unable to find regdb in BDF')
regdb_size = unpack('<H', bdf[regdb_addr - 2:regdb_addr])[0]
regdb_version = bdf[regdb_addr + REGDB_VERSION_ADDR.get(regdb_size)]
with io.open(args.update_regdb[1], 'rb') as r:
regdb = mmap(r.fileno(), 0, access=ACCESS_COPY)
if regdb[0:8] != bytes.fromhex(REGDB_HEADER):
exit('Not valid ath11k regdb file')
if len(regdb) != regdb_size:
exit(f'Incorrect regdb file size (should be {regdb_size}B)')
if md5(regdb).hexdigest() != md5(bdf[regdb_addr:regdb_addr + regdb_size]).hexdigest():
checksum = bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2]
checksum = calculate_checksum(checksum, bdf[regdb_addr:regdb_addr + regdb_size], regdb)
bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2] = checksum
bdf[regdb_addr:regdb_addr + regdb_size] = regdb
bdf_file = bdf.read()
bdf.close()
regdb_version_update = regdb[REGDB_VERSION_ADDR.get(regdb_size)]
print(f'Updating regdb (v{regdb_version} -> v{regdb_version_update}) in {REGDB_BDF.get(regdb_addr, "unknown")} BDF')
with io.open(args.output or args.update_regdb[0], 'wb') as bdf:
bdf.write(bdf_file)
else:
print(f'Regdb (v{regdb_version}) is up to date')
def cmd_remove_regdomain(args):
with io.open(args.remove_regdomain, 'rb') as b:
bdf = mmap(b.fileno(), 0, access=ACCESS_COPY)
if bdf[0:6] != bytes.fromhex(BDF_HEADER):
exit('Not valid ath11k BDF file')
regdomain = bdf[REGDOMAIN_ADDR[0]:REGDOMAIN_ADDR[0] + 2]
no_regdomain = pack('<H', REGDOMAIN_CODE)
if regdomain != no_regdomain:
checksum = bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2]
print(f'Remove regdomain {format(unpack("<H", regdomain)[0], "#06x")}')
for addr in REGDOMAIN_ADDR:
addr_regdomain = bdf[addr:addr + 2]
if addr_regdomain == regdomain:
checksum = calculate_checksum(checksum, addr_regdomain, no_regdomain)
bdf[addr:addr + 2] = no_regdomain
bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2] = checksum
bdf_file = bdf.read()
bdf.close()
with io.open(args.output or args.remove_regdomain, 'wb') as bdf:
bdf.write(bdf_file)
else:
print('Regdomain is not set')
def cmd_patch_bdf(args):
with io.open(args.patch_bdf[0], 'rb') as b:
bdf = mmap(b.fileno(), 0, access=ACCESS_COPY)
if bdf[0:6] != bytes.fromhex(BDF_HEADER):
exit('Not valid ath11k BDF file')
if isfile(args.patch_bdf[2]):
with io.open(args.patch_bdf[2], 'rb') as f:
patch = mmap(f.fileno(), 0, access=ACCESS_COPY)
patch_len = int(len(patch))
else:
patch = bytes.fromhex(args.patch_bdf[2])
patch_len = int(len(args.patch_bdf[2])/2)
patch_addr = int(args.patch_bdf[1], 0)
patch_data = bdf[patch_addr:patch_addr + patch_len]
if patch != patch_data:
checksum = bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2]
if isfile(args.patch_bdf[2]):
print(f'Patch {args.patch_bdf[2]}')
else:
print(f'Patch {bytes(patch_data).hex()}')
patch_pre = bytes()
patch_post = bytes()
if (patch_addr % 2) != 0:
patch_pre = bdf[patch_addr - 1:patch_addr]
if (patch_len % 2) == 0:
patch_post = bdf[patch_addr + 1:patch_addr + 2]
elif (patch_len % 2) != 0:
patch_post = bdf[patch_addr + 1:patch_addr + 2]
checksum = calculate_checksum(checksum, patch_pre + patch_data + patch_post, patch_pre + patch + patch_post)
bdf[patch_addr:patch_addr + patch_len] = patch
bdf[CHECKSUM_ADDR:CHECKSUM_ADDR + 2] = checksum
bdf_file = bdf.read()
bdf.close()
with io.open(args.output or args.patch_bdf[0], 'wb') as bdf:
bdf.write(bdf_file)
else:
print('Patch not needed')
def main():
parser = ArgumentParser(description='ath11k BDF tool')
cmd_group = parser.add_mutually_exclusive_group(required=True)
cmd_group.add_argument('-e', '--extract-regdb', metavar='BDF',
help='extract regdb from ath11k BDF')
cmd_group.add_argument('-u', '--update-regdb', metavar=('BDF', 'REGDB'), nargs=2,
help='update regdb in ath11k BDF')
cmd_group.add_argument('-r', '--remove-regdomain', metavar='BDF',
help='remove regdomain from ath11k BDF')
cmd_group.add_argument('-p', '--patch-bdf', metavar=('BDF', 'ADDR', 'VAL'), nargs=3,
help='patch ath11k BDF')
parser.add_argument('-o', '--output', metavar='FILE',
help='output file name')
args = parser.parse_args()
if args.extract_regdb:
return cmd_extract_regdb(args)
elif args.update_regdb:
return cmd_update_regdb(args)
elif args.remove_regdomain:
return cmd_remove_regdomain(args)
elif args.patch_bdf:
return cmd_patch_bdf(args)
if __name__ == '__main__':
main()