code starts up

This commit is contained in:
Emil Lerch 2023-04-20 14:04:18 -07:00
parent bb81a685ed
commit 9cd262ed57
64 changed files with 12325 additions and 7154 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ bin/
lib/
lib64
pyvenv.cfg
log/

219
config.py Normal file

File diff suppressed because one or more lines are too long

View File

@ -16,45 +16,45 @@ from .base import bflb_base_eflash_loader
from .base import bflb_img_create
from .base import bflb_serial
from .base import bflb_img_loader
from base.bl602 import flash_select_do
from base.bl602 import bootheader_cfg_keys
from base.bl602 import jlink_load_cfg
from base.bl602 import cklink_load_cfg
from base.bl602 import openocd_load_cfg
from base.bl602 import img_create_do
from base.bl702 import chiptype_patch
from base.bl702 import flash_select_do
from base.bl702 import bootheader_cfg_keys
from base.bl702 import jlink_load_cfg
from base.bl702 import cklink_load_cfg
from base.bl702 import openocd_load_cfg
from base.bl702 import img_create_do
from base.bl808 import chiptype_patch
from base.bl808 import flash_select_do
from base.bl808 import bootheader_cfg_keys
from base.bl808 import jlink_load_cfg
from base.bl808 import cklink_load_cfg
from base.bl808 import openocd_load_cfg
from base.bl808 import img_create_do
from base.bl616 import chiptype_patch
from base.bl616 import flash_select_do
from base.bl616 import bootheader_cfg_keys
from base.bl616 import jlink_load_cfg
from base.bl616 import cklink_load_cfg
from base.bl616 import openocd_load_cfg
from base.bl616 import img_create_do
from base.wb03 import chiptype_patch
from base.wb03 import flash_select_do
from base.wb03 import bootheader_cfg_keys
from base.wb03 import jlink_load_cfg
from base.wb03 import cklink_load_cfg
from base.wb03 import openocd_load_cfg
from base.wb03 import img_create_do
from base.bl702l import chiptype_patch
from base.bl702l import flash_select_do
from base.bl702l import bootheader_cfg_keys
from base.bl702l import jlink_load_cfg
from base.bl702l import cklink_load_cfg
from base.bl702l import openocd_load_cfg
from base.bl702l import img_create_do
from .base.bl602 import flash_select_do
from .base.bl602 import bootheader_cfg_keys
from .base.bl602 import jlink_load_cfg
from .base.bl602 import cklink_load_cfg
from .base.bl602 import openocd_load_cfg
from .base.bl602 import img_create_do
from .base.bl702 import chiptype_patch
from .base.bl702 import flash_select_do
from .base.bl702 import bootheader_cfg_keys
from .base.bl702 import jlink_load_cfg
from .base.bl702 import cklink_load_cfg
from .base.bl702 import openocd_load_cfg
from .base.bl702 import img_create_do
from .base.bl808 import chiptype_patch
from .base.bl808 import flash_select_do
from .base.bl808 import bootheader_cfg_keys
from .base.bl808 import jlink_load_cfg
from .base.bl808 import cklink_load_cfg
from .base.bl808 import openocd_load_cfg
from .base.bl808 import img_create_do
from .base.bl616 import chiptype_patch
from .base.bl616 import flash_select_do
from .base.bl616 import bootheader_cfg_keys
from .base.bl616 import jlink_load_cfg
from .base.bl616 import cklink_load_cfg
from .base.bl616 import openocd_load_cfg
from .base.bl616 import img_create_do
from .base.wb03 import chiptype_patch
from .base.wb03 import flash_select_do
from .base.wb03 import bootheader_cfg_keys
from .base.wb03 import jlink_load_cfg
from .base.wb03 import cklink_load_cfg
from .base.wb03 import openocd_load_cfg
from .base.wb03 import img_create_do
from .base.bl702l import chiptype_patch
from .base.bl702l import flash_select_do
from .base.bl702l import bootheader_cfg_keys
from .base.bl702l import jlink_load_cfg
from .base.bl702l import cklink_load_cfg
from .base.bl702l import openocd_load_cfg
from .base.bl702l import img_create_do
# okay decompiling ./libs/__init__.pyc

View File

@ -1,7 +0,0 @@
# decompyle3 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.7.16 (default, Mar 30 2023, 01:25:49)
# [GCC 12.2.1 20220924]
# Embedded file name: -
pass
# okay decompiling ./libs/base.pyc

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,241 @@
# decompyle3 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.7.16 (default, Mar 30 2023, 01:25:49)
# [GCC 12.2.1 20220924]
# Embedded file name: libs/base/bflb_efuse_boothd_create.py
import os, sys, shutil, traceback
from libs import bflb_utils
from libs.bflb_utils import app_path, chip_path, convert_path
from libs.bflb_configobj import BFConfigParser
def bootheader_update_flash_pll_crc(bootheader_data, chiptype):
flash_cfg_start = 8
flash_cfg_len = 92
if chiptype == 'wb03':
flash_cfg_start += 208
flash_cfg = bootheader_data[flash_cfg_start + 4:flash_cfg_start + flash_cfg_len - 4]
crcarray = bflb_utils.get_crc32_bytearray(flash_cfg)
bootheader_data[flash_cfg_start + flash_cfg_len - 4:flash_cfg_start + flash_cfg_len] = crcarray
pll_cfg_start = flash_cfg_start + flash_cfg_len
pll_cfg_len = 16
if chiptype == 'bl808':
pll_cfg_len = 28
else:
if chiptype == 'bl616' or chiptype == 'wb03':
pll_cfg_len = 20
pll_cfg = bootheader_data[pll_cfg_start + 4:pll_cfg_start + pll_cfg_len - 4]
crcarray = bflb_utils.get_crc32_bytearray(pll_cfg)
bootheader_data[pll_cfg_start + pll_cfg_len - 4:pll_cfg_start + pll_cfg_len] = crcarray
return bootheader_data
def get_int_mask(pos, length):
ones = '11111111111111111111111111111111'
zeros = '00000000000000000000000000000000'
mask = ones[0:32 - pos - length] + zeros[0:length] + ones[0:pos]
return int(mask, 2)
def update_data_from_cfg(config_keys, config_file, section):
bflb_utils.printf('Updating data according to <' + config_file + '[' + section + ']>')
cfg = BFConfigParser()
cfg.read(config_file)
filelen = 0
for key in config_keys:
offset = int(config_keys.get(key)['offset'], 10)
if offset > filelen:
filelen = offset
filelen += 4
bflb_utils.printf('Created file len:' + str(filelen))
data = bytearray(filelen)
data_mask = bytearray(filelen)
for key in cfg.options(section):
if config_keys.get(key) is None:
bflb_utils.printf(key + ' not exist')
continue
else:
val = cfg.get(section, key)
if val.startswith('0x'):
val = int(val, 16)
else:
val = int(val, 10)
offset = int(config_keys.get(key)['offset'], 10)
pos = int(config_keys.get(key)['pos'], 10)
bitlen = int(config_keys.get(key)['bitlen'], 10)
oldval = bflb_utils.bytearray_to_int(bflb_utils.bytearray_reverse(data[offset:offset + 4]))
oldval_mask = bflb_utils.bytearray_to_int(bflb_utils.bytearray_reverse(data_mask[offset:offset + 4]))
newval = (oldval & get_int_mask(pos, bitlen)) + (val << pos)
if val != 0:
newval_mask = oldval_mask | ~get_int_mask(pos, bitlen)
else:
newval_mask = oldval_mask
data[offset:offset + 4] = bflb_utils.int_to_4bytearray_l(newval)
data_mask[offset:offset + 4] = bflb_utils.int_to_4bytearray_l(newval_mask)
return (data, data_mask)
def bootheader_create_do(chipname, chiptype, config_file, section, output_file=None, if_img=False):
efuse_bootheader_path = os.path.join(chip_path, chipname, 'efuse_bootheader')
try:
bflb_utils.printf('Create bootheader using ', config_file)
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
bh_data, tmp = update_data_from_cfg(sub_module.bootheader_cfg_keys.bootheader_cfg_keys, config_file, section)
bh_data = bootheader_update_flash_pll_crc(bh_data, chiptype)
if output_file is None:
fp = open(efuse_bootheader_path + '/' + section.lower().replace('_cfg', '.bin'), 'wb+')
else:
fp = open(output_file, 'wb+')
if section == 'BOOTHEADER_CFG':
if chiptype == 'bl60x':
final_data = bytearray(8192)
bh_data[118] = bh_data[118] | 4
final_data[0:176] = bh_data
final_data[4096:4272] = bh_data
final_data[4098] = 65
final_data[117] = final_data[117] | 240
final_data[4214] = final_data[4214] | 3
bh_data = final_data
if if_img is True:
bh_data[8:12] = bytearray(4)
bh_data[100:104] = bytearray(4)
if chiptype == 'bl808':
fp.write(bh_data[0:384])
else:
if chiptype == 'bl616':
fp.write(bh_data[0:256])
else:
if chiptype == 'wb03':
fp.write(bh_data[0:464])
else:
if chiptype == 'bl702l':
fp.write(bh_data[0:240])
else:
fp.write(bh_data[0:176])
else:
fp.write(bh_data)
fp.close()
if chiptype == 'bl808':
if section == 'BOOTHEADER_GROUP0_CFG':
fp = open(efuse_bootheader_path + '/clock_para.bin', 'wb+')
fp.write(bh_data[100:128])
fp.close()
fp = open(efuse_bootheader_path + '/flash_para.bin', 'wb+')
fp.write(bh_data[12:96])
fp.close()
else:
if chiptype == 'bl616':
if section == 'BOOTHEADER_GROUP0_CFG':
fp = open(efuse_bootheader_path + '/clock_para.bin', 'wb+')
fp.write(bh_data[100:120])
fp.close()
fp = open(efuse_bootheader_path + '/flash_para.bin', 'wb+')
fp.write(bh_data[12:96])
fp.close()
else:
if chiptype == 'wb03':
if section == 'BOOTHEADER_GROUP0_CFG':
fp = open(efuse_bootheader_path + '/clock_para.bin', 'wb+')
fp.write(bh_data[308:328])
fp.close()
fp = open(efuse_bootheader_path + '/flash_para.bin', 'wb+')
fp.write(bh_data[220:304])
fp.close()
else:
if chiptype == 'bl702l':
if section == 'BOOTHEADER_CFG':
fp = open(efuse_bootheader_path + '/clock_para.bin', 'wb+')
fp.write(bh_data[100:116])
fp.close()
fp = open(efuse_bootheader_path + '/flash_para.bin', 'wb+')
fp.write(bh_data[12:96])
fp.close()
else:
fp = open(efuse_bootheader_path + '/flash_para.bin', 'wb+')
fp.write(bh_data[12:96])
fp.close()
except Exception as e:
try:
bflb_utils.printf('bootheader_create_do fail!!')
bflb_utils.printf(e)
traceback.print_exc(limit=5, file=(sys.stdout))
finally:
e = None
del e
def bootheader_create_process(chipname, chiptype, config_file, output_file1=None, output_file2=None, if_img=False):
fp = open(config_file, 'r')
data = fp.read()
fp.close()
if 'BOOTHEADER_CFG' in data:
bootheader_create_do(chipname, chiptype, config_file, 'BOOTHEADER_CFG', output_file1, if_img)
if 'BOOTHEADER_CPU0_CFG' in data:
bootheader_create_do(chipname, chiptype, config_file, 'BOOTHEADER_CPU0_CFG', output_file1, if_img)
if 'BOOTHEADER_CPU1_CFG' in data:
bootheader_create_do(chipname, chiptype, config_file, 'BOOTHEADER_CPU1_CFG', output_file2, if_img)
if 'BOOTHEADER_GROUP0_CFG' in data:
bootheader_create_do(chipname, chiptype, config_file, 'BOOTHEADER_GROUP0_CFG', output_file1, if_img)
if 'BOOTHEADER_GROUP1_CFG' in data:
bootheader_create_do(chipname, chiptype, config_file, 'BOOTHEADER_GROUP1_CFG', output_file2, if_img)
def efuse_create_process(chipname, chiptype, config_file, output_file=None):
efuse_bootheader_path = os.path.join(chip_path, chipname, 'efuse_bootheader')
eflash_loader_path = os.path.join(chip_path, chipname, 'eflash_loader')
filedir = ''
bflb_utils.printf('Create efuse using ', config_file)
cfgfile = eflash_loader_path + '/eflash_loader_cfg.ini'
if os.path.isfile(cfgfile) is False:
shutil.copyfile(eflash_loader_path + '/eflash_loader_cfg.conf', cfgfile)
cfg = BFConfigParser()
cfg.read(cfgfile)
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
efuse_data, mask = update_data_from_cfg(sub_module.efuse_cfg_keys.efuse_cfg_keys, config_file, 'EFUSE_CFG')
if output_file is None:
filedir = efuse_bootheader_path + '/efusedata.bin'
else:
filedir = output_file
fp = open(filedir, 'wb+')
fp.write(efuse_data)
fp.close()
bflb_utils.update_cfg(cfg, 'EFUSE_CFG', 'file', convert_path(os.path.relpath(filedir, app_path)))
if output_file is None:
filedir = efuse_bootheader_path + '/efusedata_mask.bin'
else:
filedir = output_file.replace('.bin', '_mask.bin')
fp = open(filedir, 'wb+')
fp.write(mask)
fp.close()
bflb_utils.update_cfg(cfg, 'EFUSE_CFG', 'maskfile', convert_path(os.path.relpath(filedir, app_path)))
cfg.write(cfgfile, 'w+')
def efuse_boothd_create_process(chipname, chiptype, config_file):
bootheader_create_process(chipname, chiptype, config_file)
efuse_create_process(chipname, chiptype, config_file)
def run():
chip_dict = {
'bl56x': 'bl60x',
'bl60x': 'bl60x',
'bl562': 'bl602',
'bl602': 'bl602',
'bl702': 'bl702',
'bl702l': 'bl702l',
'bl808': 'bl808',
'bl616': 'bl616',
'wb03': 'wb03'}
chipname = sys.argv[1]
chiptype = chip_dict[chipname]
img_create_path = os.path.join(chip_path, chipname, 'img_create_mcu')
bh_cfg_file = img_create_path + '/efuse_bootheader_cfg.ini'
bh_file = img_create_path + '/bootheader.bin'
bootheader_create_process(chipname, chiptype, bh_cfg_file, bh_file, img_create_path + '/bootheader_dummy.bin')
if __name__ == '__main__':
run()
# okay decompiling ./libs/base/bflb_efuse_boothd_create.pyc

View File

@ -0,0 +1,170 @@
# decompyle3 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.7.16 (default, Mar 30 2023, 01:25:49)
# [GCC 12.2.1 20220924]
# Embedded file name: libs/base/bflb_flash_select.py
import os, re
from libs import bflb_utils
from libs.bflb_utils import app_path, chip_path, conf_sign
from libs.bflb_configobj import BFConfigParser
def get_int_mask(pos, length):
ones = '11111111111111111111111111111111'
zeros = '00000000000000000000000000000000'
mask = ones[0:32 - pos - length] + zeros[0:length] + ones[0:pos]
return int(mask, 2)
def update_flash_para_from_cfg(config_keys, config_file):
section = 'FLASH_CFG'
cfg = BFConfigParser()
cfg.read(config_file)
filelen = 0
offset = 0
minOffset = 4294967295
maxOffset = 0
flashCrcOffset = 0
crcOffset = 0
if config_keys.get('crc32') != None:
crcOffset = int(config_keys.get('crc32')['offset'], 10)
if config_keys.get('flashcfg_crc32') != None:
flashCrcOffset = int(config_keys.get('flashcfg_crc32')['offset'], 10)
for key in cfg.options(section):
if config_keys.get(key) == None:
continue
else:
offset = int(config_keys.get(key)['offset'], 10)
if offset < minOffset:
minOffset = offset
if offset > maxOffset:
maxOffset = offset
filelen = maxOffset - minOffset + 4
data = bytearray(filelen)
for key in cfg.options(section):
if config_keys.get(key) == None:
bflb_utils.printf(key + ' not exist')
continue
else:
val = cfg.get(section, key)
if val.startswith('0x'):
val = int(val, 16)
else:
val = int(val, 10)
offset = int(config_keys.get(key)['offset'], 10) - minOffset
pos = int(config_keys.get(key)['pos'], 10)
bitlen = int(config_keys.get(key)['bitlen'], 10)
oldval = bflb_utils.bytearray_to_int(bflb_utils.bytearray_reverse(data[offset:offset + 4]))
newval = (oldval & get_int_mask(pos, bitlen)) + (val << pos)
data[offset:offset + 4] = bflb_utils.int_to_4bytearray_l(newval)
return (minOffset, filelen, data, flashCrcOffset, crcOffset)
def update_flash_cfg_data_do(chiptype, flash_id):
cfg_dir = app_path + '/utils/flash/' + chiptype + '/'
sub_module = __import__(('libs.base.' + chiptype), fromlist=[chiptype])
conf_name = sub_module.flash_select_do.get_suitable_file_name(cfg_dir, flash_id)
if os.path.isfile(cfg_dir + conf_name) == False:
return (None, None, None, None, None)
return update_flash_para_from_cfg(sub_module.bootheader_cfg_keys.bootheader_cfg_keys, cfg_dir + conf_name)
def flash_bootheader_config_check(chiptype, flashid, file, parafile):
magic_code = 1347307074
flash_magic_code = 1195787078
offset, flashCfgLen, data, flashCrcOffset, crcOffset = update_flash_cfg_data_do(chiptype, flashid)
if data is None:
offset = 12
flashCfgLen = 84
if parafile != '':
if data != None:
fp = open(os.path.join(app_path, parafile), 'wb')
fp.write(data)
fp.close()
fp = open(os.path.join(app_path, file), 'rb')
rdata = bytearray(fp.read())
fp.close()
i = 0
length = 128
flashCfg = bytearray(256)
while i < length:
if rdata[i:i + 4] == bflb_utils.int_to_4bytearray_l(magic_code):
if rdata[i + 8:i + 12] == bflb_utils.int_to_4bytearray_l(flash_magic_code):
if data != None:
data[2:4] = rdata[i + 14:i + 16]
flashCfg = rdata[i + offset:i + offset + flashCfgLen]
if data != None:
if not data != flashCfg and flashCfg[13:14] != b'\xff' or flashCfg[13:14] != b'\x00':
return False
else:
if flashCfg[13:14] != b'\xff':
if flashCfg[13:14] != b'\x00':
return False
i += 4
return True
def update_flash_cfg_data(chiptype, flash_id, cfg, bh_cfg_file, cfg_key):
cfg2 = BFConfigParser()
cfg2.read(bh_cfg_file)
magic_code = cfg2.get(cfg_key, 'magic_code')
magic_code = int(magic_code, 16)
flash_magic_code = cfg2.get(cfg_key, 'flashcfg_magic_code')
flash_magic_code = int(flash_magic_code, 16)
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
offset, flashCfgLen, data, flashCrcOffset, crcOffset = update_flash_cfg_data_do(chiptype, flash_id)
para_file = cfg.get('FLASH_CFG', 'flash_para')
if para_file != '':
fp = open(os.path.join(app_path, para_file), 'wb')
fp.write(data)
fp.close()
flash_file = re.compile('\\s+').split(cfg.get('FLASH_CFG', 'file'))
for f in flash_file:
fp = open(os.path.join(app_path, f), 'rb')
rdata = bytearray(fp.read())
fp.close()
i = 0
length = len(rdata)
while i < length:
if rdata[i:i + 4] == bflb_utils.int_to_4bytearray_l(magic_code):
if rdata[i + 8:i + 12] == bflb_utils.int_to_4bytearray_l(flash_magic_code):
data[2:4] = rdata[i + 14:i + 16]
flashCfg = rdata[i + offset:i + offset + flashCfgLen]
if data != flashCfg:
return False
i += 4
return True
def check_basic_flash_cfg(cfg_file, section):
if os.path.isfile(cfg_file) is False:
return False
cfg = BFConfigParser()
cfg.read(cfg_file)
if cfg.has_option(section, 'mfg_id'):
if cfg.get(section, 'mfg_id') == '0xff' or cfg.get(section, 'mfg_id') == '0xFF':
cfg.set(section, 'io_mode', '0x10')
cfg.set(section, 'cont_read_support', '0')
cfg.write(cfg_file, 'w+')
return True
if cfg.get(section, 'mfg_id') == '0x00':
return True
return False
def update_flash_cfg(chipname, chiptype, flash_id, file=None, create=False, section=None):
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
if check_basic_flash_cfg(file, section):
return True
if sub_module.flash_select_do.update_flash_cfg_do(chipname, chiptype, flash_id, file, create, section) == False:
return False
return True
def get_supported_flash(chiptype):
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
return sub_module.flash_select_do.get_supported_flash_do()
# okay decompiling ./libs/base/bflb_flash_select.pyc

View File

@ -0,0 +1,179 @@
# decompyle3 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.7.16 (default, Mar 30 2023, 01:25:49)
# [GCC 12.2.1 20220924]
# Embedded file name: libs/base/bflb_img_create.py
import re, os, sys, time, shutil, zipfile
from libs import bflb_utils
from libs.bflb_utils import app_path, chip_path, set_error_code, convert_path
from libs.bflb_configobj import BFConfigParser
from libs.base import bflb_efuse_boothd_create
def take_second(elem):
return elem[1]
def factory_mode_set(file, value):
cfg = BFConfigParser()
cfg.read(file)
if cfg.has_option('EFUSE_CFG', 'factory_mode'):
cfg.set('EFUSE_CFG', 'factory_mode', value)
cfg.write(file, 'w')
def check_pt_file(file, addr):
if len(file) > 0:
i = 0
L = []
while i < len(file):
L.append([convert_path(file[i]), int(addr[i], 16)])
i += 1
L.sort(key=take_second)
i = 0
try:
while i < len(L) - 1:
address = L[i][1]
address_next = L[i + 1][1]
file_size = os.path.getsize(os.path.join(app_path, L[i][0]))
if address_next < address + file_size:
bflb_utils.printf('pt check fail, %s is overlayed with %s in flash layout, please check your partition table to fix this issue' % (
L[i][0], L[i + 1][0]))
return False
else:
i += 1
except Exception as e:
try:
bflb_utils.printf(e)
return False
finally:
e = None
del e
return True
def compress_dir(chipname, zippath, efuse_load=False, address=None, flash_file=None, efuse_file=None, efuse_mask_file=None):
zip_file = os.path.join(chip_path, chipname, zippath, 'whole_img.pack')
dir_path = os.path.join(chip_path, chipname, chipname)
cfg_file = os.path.join(chip_path, chipname, 'eflash_loader/eflash_loader_cfg.ini')
cfg = BFConfigParser()
cfg.read(cfg_file)
if not address:
address = []
if not flash_file:
flash_file = []
if check_pt_file(flash_file, address) is not True:
bflb_utils.printf('PT Check Fail')
set_error_code('0082')
return False
flash_file.append(os.path.join(chip_path, chipname, 'eflash_loader/eflash_loader_cfg.ini'))
if efuse_load:
if efuse_file:
flash_file.append(efuse_file)
if efuse_mask_file:
flash_file.append(efuse_mask_file)
if len(flash_file) > 0:
i = 0
try:
while i < len(flash_file):
if chip_path in flash_file[i]:
relpath = os.path.relpath(os.path.join(app_path, convert_path(flash_file[i])), chip_path)
dir = os.path.join(chip_path, chipname, relpath)
if os.path.isdir(os.path.dirname(dir)) is False:
os.makedirs(os.path.dirname(dir))
shutil.copyfile(os.path.join(app_path, convert_path(flash_file[i])), dir)
else:
relpath = os.path.relpath(os.path.join(chipname, 'img_create', os.path.basename(flash_file[i])))
dir = os.path.join(chip_path, chipname, relpath)
if os.path.isdir(os.path.dirname(dir)) is False:
os.makedirs(os.path.dirname(dir))
shutil.copyfile(flash_file[i], dir)
i += 1
verfile = os.path.join(chip_path, chipname, chipname, 'version.txt')
with open(verfile, mode='w') as f:
f.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
except Exception as e:
try:
bflb_utils.printf(e)
return False
finally:
e = None
del e
try:
z = zipfile.ZipFile(zip_file, 'w')
for dirpath, dirnames, filenames in os.walk(dir_path):
for file in filenames:
z.write(os.path.join(dirpath, file), os.path.relpath(os.path.join(dirpath, file), os.path.join(chip_path, chipname)))
z.close()
shutil.rmtree(dir_path)
except Exception as e:
try:
bflb_utils.printf(e)
return False
finally:
e = None
del e
return True
def img_create(args, chipname='bl60x', chiptype='bl60x', img_dir=None, config_file=None):
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
img_dir_path = os.path.join(chip_path, chipname, 'img_create')
if img_dir is None:
sub_module.img_create_do.img_create_do(args, img_dir_path, config_file)
else:
sub_module.img_create_do.img_create_do(args, img_dir, config_file)
def create_sp_media_image_file(config, chiptype='bl60x', cpu_type=None):
sub_module = __import__(('libs.' + chiptype), fromlist=[chiptype])
sub_module.img_create_do.create_sp_media_image(config, cpu_type)
def encrypt_loader_bin(chiptype, file, sign, encrypt, encrypt_key, encrypt_iv, publickey_file, privatekey_file):
sub_module = __import__(('libs.base.' + chiptype), fromlist=[chiptype])
return sub_module.img_create_do.encrypt_loader_bin_do(file, sign, encrypt, encrypt_key, encrypt_iv, publickey_file, privatekey_file)
def run():
parser_image = bflb_utils.image_create_parser_init()
args = parser_image.parse_args()
bflb_utils.printf('Chipname: %s' % args.chipname)
if args.chipname:
chip_dict = { 'bl56x': 'bl60x',
'bl60x': 'bl60x',
'bl562': 'bl602',
'bl602': 'bl602',
'bl702': 'bl702',
'bl702l': 'bl702l',
'bl808': 'bl808',
'bl606p': 'bl808',
'bl616': 'bl616',
'wb03': 'wb03'}
chipname = args.chipname
chiptype = chip_dict[chipname]
img_create_path = os.path.join(chip_path, chipname, 'img_create_mcu')
img_create_cfg = os.path.join(chip_path, chipname, 'img_create_mcu') + '/img_create_cfg.ini'
bh_cfg_file = img_create_path + '/efuse_bootheader_cfg.ini'
bh_file = img_create_path + '/bootheader.bin'
if args.imgfile:
imgbin = args.imgfile
cfg = BFConfigParser()
cfg.read(img_create_cfg)
cfg.set('Img_Cfg', 'segdata_file', imgbin)
cfg.write(img_create_cfg, 'w')
bflb_efuse_boothd_create.bootheader_create_process(chipname, chiptype, bh_cfg_file, bh_file, img_create_path + '/bootheader_dummy.bin')
img_create(args, chipname, chiptype, img_create_path, img_create_cfg)
else:
bflb_utils.printf('Please set chipname config, exit')
if __name__ == '__main__':
run()
# okay decompiling ./libs/base/bflb_img_create.pyc

View File

@ -0,0 +1,787 @@
# decompyle3 version 3.9.0
# Python bytecode version base 3.7.0 (3394)
# Decompiled from: Python 3.7.16 (default, Mar 30 2023, 01:25:49)
# [GCC 12.2.1 20220924]
# Embedded file name: libs/base/bflb_img_loader.py
import os, sys, time, hashlib, binascii, traceback, threading
from Crypto.Cipher import AES
from libs import bflb_utils
from libs.base import bflb_img_create
from libs.base import bflb_serial
import config as gol
from libs.bflb_utils import app_path
class BflbImgLoader(object):
def __init__(self, device, speed, boot_speed, interface='uart', chip_type='bl602', chip_name='bl602', eflash_loader_file1='', eflash_loader_file2='', callback=None, do_reset=False, reset_hold_time=100, shake_hand_delay=100, reset_revert=True, cutoff_time=0, shake_hand_retry=2, isp_mode_sign=False, isp_timeout=0, encrypt_key=None, encrypt_iv=None, public_key=None, private_key=None):
self._device = device
self._speed = speed
self._interface = interface.lower()
self._chip_type = chip_type
self._chip_name = chip_name
self._eflash_loader_file1 = eflash_loader_file1
self._eflash_loader_file2 = eflash_loader_file2
self._callback = callback
self._do_reset = do_reset
self._reset_hold_time = reset_hold_time
self._shake_hand_delay = shake_hand_delay
self._reset_revert = reset_revert
self._cutoff_time = cutoff_time
self._shake_hand_retry = shake_hand_retry
self._isp_mode_sign = isp_mode_sign
self._isp_timeout = isp_timeout
self._boot_speed = boot_speed
self.encrypt_key = encrypt_key
self.encrypt_iv = encrypt_iv
self.public_key = public_key
self.private_key = private_key
self._boot_load = True
self._record_bootinfo = None
self.bflb_serial_object = None
self._imge_fp = None
self._segcnt = 0
self._602a0_dln_fix = False
self.isp_baudrate = 2000000
if interface == 'uart':
self.bflb_serial_object = bflb_serial.BLSerialUart(rts_state=True, dtr_state=True)
self._bootrom_cmds = {'get_chip_id':{'cmd_id':'05',
'data_len':'0000',
'callback':None},
'get_boot_info':{'cmd_id':'10',
'data_len':'0000',
'callback':None},
'load_boot_header':{'cmd_id':'11',
'data_len':'00b0',
'callback':None},
'808_load_boot_header':{'cmd_id':'11',
'data_len':'0160',
'callback':None},
'628_load_boot_header':{'cmd_id':'11',
'data_len':'0100',
'callback':None},
'616_load_boot_header':{'cmd_id':'11',
'data_len':'0100',
'callback':None},
'702l_load_boot_header':{'cmd_id':'11',
'data_len':'00F0',
'callback':None},
'load_publick_key':{'cmd_id':'12',
'data_len':'0044',
'callback':None},
'load_publick_key2':{'cmd_id':'13',
'data_len':'0044',
'callback':None},
'load_signature':{'cmd_id':'14',
'data_len':'0004',
'callback':None},
'load_signature2':{'cmd_id':'15',
'data_len':'0004',
'callback':None},
'load_aes_iv':{'cmd_id':'16',
'data_len':'0014',
'callback':None},
'load_seg_header':{'cmd_id':'17',
'data_len':'0010',
'callback':None},
'load_seg_data':{'cmd_id':'18',
'data_len':'0100',
'callback':None},
'check_image':{'cmd_id':'19',
'data_len':'0000',
'callback':None},
'run_image':{'cmd_id':'1a',
'data_len':'0000',
'callback':None},
'change_rate':{'cmd_id':'20',
'data_len':'0008',
'callback':None},
'reset':{'cmd_id':'21',
'data_len':'0000',
'callback':None},
'flash_erase':{'cmd_id':'30',
'data_len':'0000',
'callback':None},
'flash_write':{'cmd_id':'31',
'data_len':'0100',
'callback':None},
'flash_read':{'cmd_id':'32',
'data_len':'0100',
'callback':None},
'flash_boot':{'cmd_id':'33',
'data_len':'0000',
'callback':None},
'efuse_write':{'cmd_id':'40',
'data_len':'0080',
'callback':None},
'efuse_read':{'cmd_id':'41',
'data_len':'0000',
'callback':None}}
def close_port(self):
if self.bflb_serial_object is not None:
self.bflb_serial_object.close()
def boot_process_load_cmd(self, section, read_len):
read_data = bytearray(0)
if read_len != 0:
read_data = bytearray(self._imge_fp.read(read_len))
if len(read_data) != read_len:
bflb_utils.printf('read error,expected len=', read_len, 'read len=', len(read_data))
return bytearray(0)
if section == 'load_boot_header':
tmp = bflb_utils.bytearray_reverse(read_data[120:124])
self._segcnt = bflb_utils.bytearray_to_int(tmp)
bflb_utils.printf('segcnt is ', self._segcnt)
else:
if section == '808_load_boot_header':
tmp = bflb_utils.bytearray_reverse(read_data[140:144])
self._segcnt = bflb_utils.bytearray_to_int(tmp)
bflb_utils.printf('segcnt is ', self._segcnt)
else:
if section == '628_load_boot_header':
tmp = bflb_utils.bytearray_reverse(read_data[136:140])
self._segcnt = bflb_utils.bytearray_to_int(tmp)
bflb_utils.printf('segcnt is ', self._segcnt)
else:
if section == '616_load_boot_header':
tmp = bflb_utils.bytearray_reverse(read_data[132:136])
self._segcnt = bflb_utils.bytearray_to_int(tmp)
bflb_utils.printf('segcnt is ', self._segcnt)
else:
if section == '702l_load_boot_header':
tmp = bflb_utils.bytearray_reverse(read_data[120:124])
self._segcnt = bflb_utils.bytearray_to_int(tmp)
bflb_utils.printf('segcnt is ', self._segcnt)
if section == 'load_signature' or section == 'load_signature2':
tmp = bflb_utils.bytearray_reverse(read_data[0:4])
sig_len = bflb_utils.bytearray_to_int(tmp)
read_data = read_data + bytearray(self._imge_fp.read(sig_len + 4))
if len(read_data) != sig_len + 8:
bflb_utils.printf('read signature error,expected len=', sig_len + 4, 'read len=', len(read_data))
return read_data
def boot_process_one_cmd(self, section, cmd_id, cmd_len):
read_len = bflb_utils.bytearray_to_int(cmd_len)
read_data = self._bootrom_cmds.get(section)['callback'](section, read_len)
tmp = bytearray(2)
tmp[0] = cmd_len[1]
tmp[1] = cmd_len[0]
data_read = bytearray(0)
tmp = bflb_utils.int_to_2bytearray_l(len(read_data))
data = cmd_id + bytearray(1) + tmp + read_data
if self._chip_type == 'bl702':
if section == 'run_image':
sub_module = __import__(('libs.base.' + self._chip_type), fromlist=[self._chip_type])
data = sub_module.chiptype_patch.img_load_create_predata_before_run_img()
self.bflb_serial_object.write(data)
if section == 'get_boot_info' or section == 'load_seg_header' or section == 'get_chip_id':
res, data_read = self.bflb_serial_object.deal_response()
else:
res = self.bflb_serial_object.deal_ack()
if res.startswith('OK') is True:
pass
else:
try:
bflb_utils.printf('result: ', res)
except IOError:
bflb_utils.printf('python IO error')
return (res, data_read)
def boot_process_one_section(self, section, data_len):
cmd_id = bflb_utils.hexstr_to_bytearray(self._bootrom_cmds.get(section)['cmd_id'])
if data_len == 0:
length = bflb_utils.hexstr_to_bytearray(self._bootrom_cmds.get(section)['data_len'])
else:
length = bflb_utils.int_to_2bytearray_b(data_len)
return self.boot_process_one_cmd(section, cmd_id, length)
def boot_inf_change_rate(self, comnum, section, newrate):
cmd_id = bflb_utils.hexstr_to_bytearray(self._bootrom_cmds.get(section)['cmd_id'])
cmd_len = bflb_utils.hexstr_to_bytearray(self._bootrom_cmds.get(section)['data_len'])
bflb_utils.printf('process', section, ',cmd=', binascii.hexlify(cmd_id), ',data len=', binascii.hexlify(cmd_len))
baudrate = self.bflb_serial_object.if_get_rate()
oldv = bflb_utils.int_to_4bytearray_l(baudrate)
newv = bflb_utils.int_to_4bytearray_l(newrate)
tmp = bytearray(3)
tmp[1] = cmd_len[1]
tmp[2] = cmd_len[0]
data = cmd_id + tmp + oldv + newv
self.bflb_serial_object.if_write(data)
bflb_utils.printf()
stime = 110 / float(baudrate) * 2
if stime < 0.003:
stime = 0.003
time.sleep(stime)
self.bflb_serial_object.if_close()
self.bflb_serial_object.if_init(comnum, newrate, self._chip_type, self._chip_name)
return self.bflb_serial_object.if_deal_ack(dmy_data=False)
def boot_install_cmds_callback(self):
self._bootrom_cmds.get('get_chip_id')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('get_boot_info')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_boot_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('808_load_boot_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('628_load_boot_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('616_load_boot_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('702l_load_boot_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_publick_key')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_publick_key2')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_signature')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_signature2')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_aes_iv')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_seg_header')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('load_seg_data')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('check_image')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('run_image')['callback'] = self.boot_process_load_cmd
self._bootrom_cmds.get('reset')['callback'] = self.boot_process_load_cmd
def issue_log_print(self):
bflb_utils.printf('########################################################################')
bflb_utils.printf('请按照以下描述排查问题:')
if self._chip_type == 'bl60x':
bflb_utils.printf('GPIO24是否上拉到板子自身的3.3V而不是外部的3.3V')
bflb_utils.printf('GPIO7(RX)是否连接到USB转串口的TX引脚')
bflb_utils.printf('GPIO14(TX)是否连接到USB转串口的RX引脚')
bflb_utils.printf('在使用烧录软件进行烧录前是否在GPIO24拉高的情况下使用Reset/Chip_En复位了芯片')
else:
if self._chip_type == 'bl602':
bflb_utils.printf('GPIO8是否上拉到板子自身的3.3V而不是外部的3.3V')
bflb_utils.printf('GPIO7(RX)是否连接到USB转串口的TX引脚')
bflb_utils.printf('GPIO16(TX)是否连接到USB转串口的RX引脚')
bflb_utils.printf('在使用烧录软件进行烧录前是否在GPIO8拉高的情况下使用Reset/Chip_En复位了芯片')
else:
if self._chip_type == 'bl702':
bflb_utils.printf('GPIO28是否上拉到板子自身的3.3V而不是外部的3.3V')
bflb_utils.printf('GPIO15(RX)是否连接到USB转串口的TX引脚')
bflb_utils.printf('GPIO14(TX)是否连接到USB转串口的RX引脚')
bflb_utils.printf('在使用烧录软件进行烧录前是否在GPIO28拉高的情况下使用Reset/Chip_En复位了芯片')
else:
bflb_utils.printf('Boot pin是否上拉到板子自身的3.3V而不是外部的3.3V')
bflb_utils.printf('UART RX是否连接到USB转串口的TX引脚')
bflb_utils.printf('UART TX是否连接到USB转串口的RX引脚')
bflb_utils.printf('在使用烧录软件进行烧录前是否在Boot pin拉高的情况下使用Reset/Chip_En复位了芯片')
bflb_utils.printf('烧录软件所选择的COM口是否是连接芯片的串口')
bflb_utils.printf('烧录软件上选择的波特率是否是USB转串口支持的波特率')
bflb_utils.printf('3.3V供电是否正常')
bflb_utils.printf('板子供电电流是否正常(烧录模式下芯片耗电电流5-7mA)')
bflb_utils.printf('########################################################################')
def send_55_command(self, speed):
try:
while True:
if self._shakehand_flag is True:
break
if not self._chip_type == 'bl702' or self._chip_type == 'bl702l':
self.bflb_serial_object.write(self.get_sync_bytes(int(0.003 * speed / 10)))
else:
self.bflb_serial_object.write(self.get_sync_bytes(int(0.006 * speed / 10)))
except Exception as e:
try:
bflb_utils.printf('Error: %s' % e)
finally:
e = None
del e
def get_sync_bytes(self, length):
try:
data = bytearray(length)
i = 0
while i < length:
data[i] = 85
i += 1
return data
except Exception as e:
try:
bflb_utils.printf('Error: %s' % e)
finally:
e = None
del e
def set_isp_baudrate(self, isp_baudrate):
bflb_utils.printf('isp mode speed: ', isp_baudrate)
self.isp_baudrate = isp_baudrate
def toggle_boot_or_shake_hand(self, run_sign, do_reset=False, reset_hold_time=100, shake_hand_delay=100, reset_revert=True, cutoff_time=0, isp_mode_sign=False, isp_timeout=0, boot_load=False, shake_hand_retry=2):
"""
When run_sign is 2, it run shakehand.
"""
device = self._device
speed = self._speed
if run_sign == 2:
shake_hand_retry = shake_hand_retry
else:
if run_sign == 1:
shake_hand_retry = 1
if self.bflb_serial_object:
try:
timeout = self.bflb_serial_object.get_timeout()
blusbserialwriteflag = False
if isp_mode_sign:
if isp_timeout > 0:
wait_timeout = isp_timeout
self.bflb_serial_object.set_timeout(0.1)
self._shakehand_flag = False
cutoff_time = 0
do_reset = False
self.bflb_serial_object.repeat_init(device, self.isp_baudrate, self._chip_type, self._chip_name)
self.bflb_serial_object.write(b'\r\nispboot if\r\nreboot\r\n')
fl_thrx = None
fl_thrx = threading.Thread(target=(self.send_55_command), args=(speed,))
fl_thrx.setDaemon(True)
fl_thrx.start()
bflb_utils.printf('Please Press Reset Key!')
self.bflb_serial_object.setRTS(1)
time.sleep(0.2)
self.bflb_serial_object.setRTS(0)
time_stamp = time.time()
while time.time() - time_stamp < wait_timeout:
if self._chip_type == 'bl602' or self._chip_type == 'bl702':
self.bflb_serial_object.set_timeout(0.01)
success, ack = self.bflb_serial_object.read(3000)
if ack.find(b'Boot2 ISP Shakehand Suss') != -1:
self._shakehand_flag = True
if ack.find(b'Boot2 ISP Ready') != -1:
bflb_utils.printf('isp ready')
self.bflb_serial_object.write(bytearray.fromhex('a0000000'))
self.bflb_serial_object.set_timeout(timeout)
return 'OK'
else:
success, ack = self.bflb_serial_object.read(3000)
if ack.find(b'Boot2 ISP Ready') != -1:
bflb_utils.printf('isp ready')
self._shakehand_flag = True
if self._shakehand_flag is True:
self.bflb_serial_object.set_timeout(timeout)
tmp_timeout = self.bflb_serial_object.get_timeout()
self.bflb_serial_object.set_timeout(0.1)
if self._chip_type == 'bl602' or self._chip_type == 'bl702':
self.bflb_serial_object.set_timeout(0.5)
success, ack = self.bflb_serial_object.read(15)
self.bflb_serial_object.set_timeout(0.005)
ack += self.bflb_serial_object.read(15)[1]
self.bflb_serial_object.set_timeout(tmp_timeout)
bflb_utils.printf('read ready')
if ack.find(b'Boot2 ISP Ready') == -1:
bflb_utils.printf('Boot2 isp is not ready')
return 'FL'
self.bflb_serial_object.write(bytearray.fromhex('a0000000'))
return 'OK'
else:
while 1:
ack = self.bflb_serial_object.raw_read()
if len(ack) == 0:
break
time.sleep(0.1)
while True:
ack = self.bflb_serial_object.raw_read()
if len(ack) == 0:
break
self.bflb_serial_object.set_timeout(tmp_timeout)
break
self._shakehand_flag = True
self.bflb_serial_object.set_timeout(timeout)
self.bflb_serial_object.repeat_init(device, speed, self._chip_type, self._chip_name)
time.sleep(2.2)
if self.bflb_serial_object._is_bouffalo_chip():
if boot_load:
blusbserialwriteflag = True
while shake_hand_retry > 0:
if cutoff_time != 0 and blusbserialwriteflag is not True:
cutoff_revert = False
if cutoff_time > 1000:
cutoff_revert = True
cutoff_time = cutoff_time - 1000
self.bflb_serial_object.setRTS(1)
time.sleep(0.2)
self.bflb_serial_object.setRTS(0)
time.sleep(0.05)
self.bflb_serial_object.setRTS(1)
if cutoff_revert:
self.bflb_serial_object.setDTR(0)
else:
self.bflb_serial_object.setDTR(1)
bflb_utils.printf('tx rx and power off, press the machine!')
bflb_utils.printf('cutoff time is ', cutoff_time / 1000.0)
time.sleep(cutoff_time / 1000.0)
if cutoff_revert:
self.bflb_serial_object.setDTR(1)
else:
self.bflb_serial_object.setDTR(0)
bflb_utils.printf('power on tx and rx ')
time.sleep(0.1)
else:
if run_sign == 2:
self.bflb_serial_object.setDTR(0)
bflb_utils.printf('default set DTR high ')
time.sleep(0.1)
if do_reset is True:
if blusbserialwriteflag is not True:
self.bflb_serial_object.setRTS(0)
time.sleep(0.2)
if reset_revert:
self.bflb_serial_object.setRTS(1)
time.sleep(0.001)
reset_cnt = 2
if reset_hold_time > 1000:
reset_cnt = int(reset_hold_time // 1000)
reset_hold_time = reset_hold_time % 1000
while reset_cnt > 0:
if reset_revert:
self.bflb_serial_object.setRTS(0)
else:
self.bflb_serial_object.setRTS(1)
time.sleep(reset_hold_time / 1000.0)
if reset_revert:
self.bflb_serial_object.setRTS(1)
else:
self.bflb_serial_object.setRTS(0)
if shake_hand_delay > 0:
time.sleep(shake_hand_delay / 1000.0)
else:
time.sleep(0.005)
if reset_revert:
self.bflb_serial_object.setRTS(0)
else:
self.bflb_serial_object.setRTS(1)
time.sleep(reset_hold_time / 1000.0)
if reset_revert:
self.bflb_serial_object.setRTS(1)
else:
self.bflb_serial_object.setRTS(0)
if shake_hand_delay > 0:
time.sleep(shake_hand_delay / 1000.0)
else:
time.sleep(0.005)
reset_cnt -= 1
bflb_utils.printf('reset cnt: ' + str(reset_cnt) + ', reset hold: ' + str(reset_hold_time / 1000.0) + ', shake hand delay: ' + str(shake_hand_delay / 1000.0))
if blusbserialwriteflag:
self.bflb_serial_object.bl_usb_serial_write(cutoff_time, reset_revert)
else:
bflb_utils.printf('clean buf')
self.bflb_serial_object.set_timeout(0.1)
self.bflb_serial_object.clear_buf()
if run_sign == 1:
self.bflb_serial_object.set_timeout(timeout)
return 'OK'
if self._602a0_dln_fix:
self.bflb_serial_object.set_timeout(0.5)
else:
self.bflb_serial_object.set_timeout(0.1)
bflb_utils.printf('send sync')
if self._chip_type == 'bl702' or self._chip_type == 'bl702l':
self.bflb_serial_object.write(self.get_sync_bytes(int(0.003 * speed / 10)))
else:
self.bflb_serial_object.write(self.get_sync_bytes(int(0.006 * speed / 10)))
if self._chip_type == 'bl808':
time.sleep(0.3)
self.bflb_serial_object.write(bflb_utils.hexstr_to_bytearray('5000080038F0002000000018'))
if self._602a0_dln_fix:
time.sleep(4)
success, ack = self.bflb_serial_object.read(1000)
bflb_utils.printf('ack is ', binascii.hexlify(ack))
if ack.find(b'O') != -1 or ack.find(b'K') != -1:
self.bflb_serial_object.set_timeout(timeout)
if self._602a0_dln_fix:
self.bflb_serial_object.write(bytearray(2))
time.sleep(0.03)
return 'OK'
if len(ack) != 0:
bflb_utils.printf('reshake')
if do_reset is False:
bflb_utils.printf('sleep')
time.sleep(3)
else:
bflb_utils.printf('retry')
shake_hand_retry -= 1
self.bflb_serial_object.set_timeout(timeout)
return 'FL'
except Exception as e:
try:
bflb_utils.printf('Error: %s' % e)
finally:
e = None
del e
else:
return 'FL'
def img_load_shake_hand(self, sh_baudrate, wk_baudrate, do_reset=False, reset_hold_time=100, shake_hand_delay=100, reset_revert=True, cutoff_time=0, shake_hand_retry=2, isp_mode_sign=False, isp_timeout=0, boot_load=True):
self.bflb_serial_object.repeat_init(self._device, sh_baudrate, self._chip_type, self._chip_name)
self.boot_install_cmds_callback()
if self._chip_type == 'wb03':
self.toggle_boot_or_shake_hand(1, do_reset, reset_hold_time, shake_hand_delay, reset_revert, cutoff_time, isp_mode_sign, isp_timeout, boot_load)
bflb_utils.printf('get_chip_id')
ret, data_read = self.boot_process_one_section('get_chip_id', 0)
if ret.startswith('OK') is False:
bflb_utils.printf('fail')
return (
ret, None)
data_read = binascii.hexlify(data_read)
bflb_utils.printf('data read is ', data_read)
chip_id = data_read.decode('utf-8')
if chip_id != '43484950574230334130305f424c0000':
if chip_id != '43484950574230334130305F424C0000':
return 'shake hand fail'
else:
if self._chip_type == 'bl602':
self._602a0_dln_fix = False
ret = self.toggle_boot_or_shake_hand(2, do_reset, reset_hold_time, shake_hand_delay, reset_revert, cutoff_time, isp_mode_sign, isp_timeout, boot_load, shake_hand_retry)
if self._chip_type == 'bl602':
self._602a0_dln_fix = False
if ret != 'OK':
bflb_utils.printf('shake hand fail')
self.issue_log_print()
bflb_utils.set_error_code('0050')
return 'shake hand fail'
if sh_baudrate != wk_baudrate:
if self.boot_inf_change_rate(self._device, 'change_rate', wk_baudrate) != 'OK':
bflb_utils.printf('change rate fail')
return 'change rate fail'
bflb_utils.printf('shake hand success')
return ret
def img_load_main_process(self, file, group, record_bootinfo=None):
encrypt_blk_size = 16
bflb_utils.printf('get_boot_info')
ret, data_read = self.boot_process_one_section('get_boot_info', 0)
if ret.startswith('OK') is False:
bflb_utils.printf('fail')
return (
ret, None)
data_read = binascii.hexlify(data_read)
bflb_utils.printf('data read is ', data_read)
bootinfo = data_read.decode('utf-8')
chipid = ''
if self._chip_type == 'bl702' or self._chip_type == 'bl702l':
chipid = bootinfo[32:34] + bootinfo[34:36] + bootinfo[36:38] + bootinfo[38:40] + bootinfo[40:42] + bootinfo[42:44] + bootinfo[44:46] + bootinfo[46:48]
else:
chipid = bootinfo[34:36] + bootinfo[32:34] + bootinfo[30:32] + bootinfo[28:30] + bootinfo[26:28] + bootinfo[24:26]
bflb_utils.printf('========= ChipID: ', chipid, ' =========')
bflb_utils.printf('last boot info: ', record_bootinfo)
if record_bootinfo != None:
if bootinfo[8:] == record_bootinfo[8:]:
bflb_utils.printf('repeated chip')
return (
'repeat_burn', bootinfo)
if bootinfo[:8] == 'FFFFFFFF' or bootinfo[:8] == 'ffffffff':
bflb_utils.printf('eflash loader present')
return (
'error_shakehand', bootinfo)
sign = 0
encrypt = 0
if self._chip_type == 'bl60x':
sign = int(data_read[8:10], 16) & 3
encrypt = (int(data_read[8:10], 16) & 12) >> 2
else:
if self._chip_type == 'bl602' or self._chip_type == 'bl702' or self._chip_type == 'bl702l':
sign = int(data_read[8:10], 16)
encrypt = int(data_read[10:12], 16)
else:
if self._chip_type == 'bl808' or self._chip_type == 'bl628':
if group == 0:
sign = int(data_read[8:10], 16)
encrypt = int(data_read[12:14], 16)
else:
sign = int(data_read[10:12], 16)
encrypt = int(data_read[14:16], 16)
else:
sign = int(data_read[8:10], 16)
encrypt = int(data_read[10:12], 16)
bflb_utils.printf('sign is ', sign, ' encrypt is ', encrypt)
if encrypt == 1 or sign == 1:
if encrypt == 1 and self.encrypt_key != None and self.encrypt_iv != None and sign == 1 and self.encrypt_key != None and self.private_key != None:
ret, encrypted_data = bflb_img_create.encrypt_loader_bin(self._chip_type, file, sign, encrypt, self.encrypt_key, self.encrypt_iv, self.public_key, self.private_key)
else:
if encrypt == 1and self.en