-
Notifications
You must be signed in to change notification settings - Fork 0
/
bflb_fw_post_proc.py
274 lines (238 loc) · 9.24 KB
/
bflb_fw_post_proc.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# uncompyle6 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.8.10 (default, Nov 14 2022, 12:59:47)
# [GCC 9.4.0]
# Embedded file name: libs/bflb_fw_post_proc.py
import re, os, sys, time, shutil
from bflb_mcu_tool import libs
try:
import bflb_path
except ImportError:
from libs import bflb_path
from libs import bflb_utils
from libs import bflb_pt_creater as partition
from libs import bflb_ro_params_device_tree as bl_ro_device_tree
chip_dict = ('bl602', 'bl604', 'bl702', 'bl704', 'bl706', 'bl702l', 'bl704l', 'bl606p',
'bl808', 'bl616', 'bl618')
def get_nicky_name(chipname):
if chipname == 'bl604':
return 'bl602'
if chipname == 'bl704' or chipname == 'bl706':
return 'bl702'
if chipname == 'bl704l':
return 'bl702l'
if chipname == 'bl618':
return 'bl616'
return chipname
def parse_header(bin):
try:
with open(bin, 'rb') as (fp):
content = fp.read()
if content[0:4] == b'BFNP' or content[0:4] == b'BFAP':
return True
except Exception as e:
try:
bflb_utils.printf(e)
bflb_utils.printf('[Warning] Check header fail')
finally:
e = None
del e
return False
def get_value_file(path, chipname, cpu_id=None):
if cpu_id:
path = path.replace('$(CHIPNAME)', chipname + '_' + cpu_id)
else:
path = path.replace('$(CHIPNAME)', chipname)
if os.path.isabs(path):
path = os.path.abspath(path)
if not os.path.exists(path):
dir_path = os.path.dirname(path)
file_name = os.path.basename(path)
try:
all_file_list = os.listdir(dir_path)
except Exception as e:
try:
bflb_utils.printf(e)
return
finally:
e = None
del e
result = []
if '*' in file_name:
file_name = file_name.replace('.', '\\.').replace('*', '.*[一-龥]*')
for one_name in all_file_list:
pattern = re.compile(file_name)
result += pattern.findall(one_name)
if len(result) > 1:
bflb_utils.printf('[Error] Multiple files were matched! ')
return
if len(result) == 0:
error = '[Error]: ' + path + ' image file is not existed'
bflb_utils.printf(error)
return
path = os.path.join(dir_path, result[0])
return path
def get_img_file_list(files, chipname, cpu_id):
file_list = files.split(',')
final = []
for file in file_list:
ret = get_value_file(file, chipname, cpu_id)
if ret != None:
final.append(ret)
else:
bflb_utils.printf('[Error] Get ', file, ' Fail!!!!')
return final
def parse_rfpa(bin, dts_bytearray):
try:
length = len(dts_bytearray)
with open(bin, 'rb') as (fp):
content = fp.read()
bin_bytearray = bytearray(content)
if content[0:4] == b'BFNP':
if content[4096:4100] == b'BFAP' or content[4096:4100] == b'BFNP':
if content[9216:9224] == b'BLRFPARA':
bflb_utils.printf('8K header found,append dts file')
bin_bytearray[9224:9224 + length] = dts_bytearray
with open(bin, 'wb') as (fp):
fp.write(bin_bytearray)
return
if content[0:4] == b'BFNP':
if content[5120:5128] == b'BLRFPARA':
bflb_utils.printf('4K header found,append dts file')
bin_bytearray[5128:5128 + length] = dts_bytearray
with open(bin, 'wb') as (fp):
fp.write(bin_bytearray)
return
if content[1024:1032] == b'BLRFPARA':
bflb_utils.printf('Raw image found,append dts file')
bin_bytearray[1032:1032 + length] = dts_bytearray
with open(bin, 'wb') as (fp):
fp.write(bin_bytearray)
return
bflb_utils.printf('BLRFPARA magic not found, skip append dts file')
except Exception as e:
try:
bflb_utils.printf(e)
bflb_utils.printf('[Error] Append dts file fail')
finally:
e = None
del e
def found_file(dir, suffix):
files = []
for file in os.listdir(dir):
if file.endswith(suffix):
files.append(file)
return files
def found_boot2_mfg_file(dir, target):
files = []
for file in os.listdir(dir):
if file.startswith(target) and file.endswith('.bin'):
files.append(file)
return files
def create_partiton_table(search_dir, imgfile):
files = found_file(search_dir, '.toml')
if len(files) == 0:
bflb_utils.printf('[Warning] No partiton file found in ', search_dir, ',go on next steps')
return
if len(files) > 1:
bflb_utils.printf('[Error] More than one partition file found in ', search_dir, ',go on next steps')
return
bflb_utils.printf('Create partition using ', files[0])
pt_helper = partition.PtCreater(os.path.join(search_dir, files[0]))
filedir, ext = os.path.split(imgfile)
pt_helper.create_pt_table(os.path.join(filedir, 'partition.bin'))
def append_dts_file(search_dir, imgfile):
files = found_file(search_dir, '.dts')
if len(files) == 0:
bflb_utils.printf('[Warning] No dts file found in ', search_dir, ',go on next steps')
return
if len(files) > 1:
bflb_utils.printf('[Error] More than one dts file found in ', search_dir, ',go on next steps')
return
bflb_utils.printf('Create dts using ', files[0])
try:
dts_hex = bl_ro_device_tree.bl_dts2hex(os.path.join(search_dir, files[0]))
dts_bytearray = bflb_utils.hexstr_to_bytearray(dts_hex)
except Exception as e:
try:
bflb_utils.printf(e)
bflb_utils.printf('[Error] Create fail!!!, go on next steps')
return
finally:
e = None
del e
parse_rfpa(imgfile, dts_bytearray)
def copy_boot2_file(search_dir, imgfile):
files = found_boot2_mfg_file(search_dir, 'boot')
if len(files) == 0:
bflb_utils.printf('[Warning] No boot2/bootloader file found in ', search_dir, ',go on next steps')
return
if len(files) > 1:
bflb_utils.printf('[Error] More than one boot2/bootloader file found in ', search_dir, ',go on next steps')
return
bflb_utils.printf('Copy ', files[0])
try:
filedir, ext = os.path.split(imgfile)
shutil.copy(os.path.join(search_dir, files[0]), filedir)
except Exception as e:
try:
bflb_utils.printf(e)
bflb_utils.printf('[Warning] Copy boot2/bootloader fail!!!, go on next steps')
finally:
e = None
del e
def copy_mfg_file(search_dir, imgfile):
files = found_boot2_mfg_file(search_dir, 'mfg')
if len(files) == 0:
bflb_utils.printf('[Warning] No mfg file found in ', search_dir, ',go on next steps')
return
if len(files) > 1:
bflb_utils.printf('[Error] More than one mfg file found in ', search_dir, ',go on next steps')
return
bflb_utils.printf('Copy ', files[0])
try:
filedir, ext = os.path.split(imgfile)
shutil.copy(os.path.join(search_dir, files[0]), filedir)
except Exception as e:
try:
bflb_utils.printf(e)
bflb_utils.printf('[Error] Copy mfg fail!!!, go on next steps')
finally:
e = None
del e
def firmware_post_process(args, chipname='bl60x'):
chipname = get_nicky_name(chipname.lower())
sub_module = __import__(('libs.' + chipname), fromlist=[chipname])
sub_module.firmware_post_process_do.firmware_post_proc(args)
def run():
parser = bflb_utils.firmware_post_proc_parser_init()
args = parser.parse_args()
bflb_utils.printf('Chipname: %s' % args.chipname)
if args.aesiv != None:
if len(args.aesiv) != 32:
bflb_utils.printf('[Error] AES IV length error')
return
if args.aesiv[24:32] != '00000000':
bflb_utils.printf('[Error] AES IV should end with 00000000')
return
if args.chipname.lower() in chip_dict:
img_file_list = None
if args.imgfile != None:
img_file_list = get_img_file_list(args.imgfile, args.chipname, args.cpuid)
if args.brdcfgdir != None:
bflb_utils.printf('Board config dir: %s' % args.brdcfgdir)
create_partiton_table(args.brdcfgdir, img_file_list[0])
append_dts_file(args.brdcfgdir, img_file_list[0])
copy_boot2_file(args.brdcfgdir, img_file_list[0])
copy_mfg_file(args.brdcfgdir, img_file_list[0])
for img_file in img_file_list:
bflb_utils.printf('\r\nProcess ', img_file)
args.imgfile = img_file
if parse_header(args.imgfile) == False:
bflb_utils.printf('[Warning] No boot header found,skip!!!')
break
firmware_post_process(args, args.chipname)
else:
bflb_utils.printf('[Error] Please set correct chipname config, exit!!!')
if __name__ == '__main__':
run()