添加加密固件烧录
This commit is contained in:
Binary file not shown.
@@ -2,6 +2,7 @@ import re
|
||||
import sys
|
||||
import struct
|
||||
import json
|
||||
from log import myprint
|
||||
|
||||
|
||||
# kunlun1,kunlun2,kunlun3的img类型
|
||||
@@ -47,6 +48,23 @@ _DevType={
|
||||
}
|
||||
|
||||
|
||||
_PatternType={
|
||||
0x00:"HTZD",
|
||||
0x55:"HTZD",
|
||||
0xaa:"JSMT",
|
||||
0x99:"FLX",
|
||||
0x66:"QJ",
|
||||
0xcc:"SPE",
|
||||
0x33:"GX",
|
||||
0xdd:"DT",
|
||||
0xa9:"YP",
|
||||
0xc6:"WTZ",
|
||||
0x93:"TCE"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
_PKT_HEADER_MAGIC_NO = 0x00005566
|
||||
_IMG_HEADER_MAGIC_NO = 0xa4e49a17
|
||||
|
||||
@@ -123,8 +141,8 @@ def tran_img_headerv0(data:bytearray):
|
||||
# f 单精度浮点 d 双精度浮点 s 字符串
|
||||
a=struct.unpack('>HHIHBBIIIBBBBI',data[0:32])
|
||||
if(a[8]==_IMG_HEADER_MAGIC_NO):
|
||||
ret["devType"]=hex(a[0])+f"({_DevType[a[0]]})"
|
||||
ret["imgType"]=hex(a[1])+f"({_ImgType[a[1]]})"
|
||||
ret["devType"]=hex(a[0])+f"({_DevType.get(a[0],'unknown')})"
|
||||
ret["imgType"]=hex(a[1])+f"({_ImgType.get(a[1],'unknown')})"
|
||||
ret["imgSize"]=str(a[2])
|
||||
ret["imgVer"]=hex(a[3])
|
||||
ret["psramSize"]=str(a[4])
|
||||
@@ -149,8 +167,8 @@ def tran_img_headerv1(data:bytearray):
|
||||
# f 单精度浮点 d 双精度浮点 s 字符串
|
||||
a=struct.unpack('>BBBBIBBBBIIIBBBBI',data[0:32])
|
||||
if(a[11]==_IMG_HEADER_MAGIC_NO):
|
||||
ret["devType"]=hex(a[0])+f"({_DevType[a[0]]})"
|
||||
ret["imgType"]=hex(a[1])+f"({_ImgType[a[1]]})"
|
||||
ret["devType"]=hex(a[0])+f"({_DevType.get(a[0],'unknown')})"
|
||||
ret["imgType"]=hex(a[1])+f"({_ImgType.get(a[1],'unknown')})"
|
||||
ret["encType"]=hex(a[2])
|
||||
ret["cfg"]=hex(a[3])
|
||||
ret["imgSize"]=str(a[4])
|
||||
@@ -176,7 +194,7 @@ def pkt_header_check(data:bytearray):
|
||||
magic=(data[12]<<24)|(data[13]<<16)|(data[14]<<8)|(data[15])
|
||||
if (magic==_PKT_HEADER_MAGIC_NO):
|
||||
return True
|
||||
print(f"magic={hex(magic)}")
|
||||
myprint(f"magic={hex(magic)}")
|
||||
return False
|
||||
|
||||
# 判断是不是img_header
|
||||
@@ -186,7 +204,7 @@ def img_header_check(data:bytearray):
|
||||
magic=(data[20]<<24)|(data[21]<<16)|(data[22]<<8)|(data[23])
|
||||
if (magic==_IMG_HEADER_MAGIC_NO):
|
||||
imghdr_v=data[11]
|
||||
print(f"magic={hex(magic)}, imghdr_v={imghdr_v}")
|
||||
myprint(f"magic={hex(magic)}, imghdr_v={imghdr_v}")
|
||||
# 返回使用的结构体类型
|
||||
if(imghdr_v==0x10):
|
||||
return 'V1'
|
||||
@@ -233,12 +251,33 @@ def bin_to_hex_file(bin_file_name:str,hex_file_name:str,enc=0):
|
||||
with open(bin_file_name,mode='rb') as f:
|
||||
bin=bytearray(f.read())
|
||||
if(enc):
|
||||
pat=(bin[0]^0x03)&0xff
|
||||
myprint("start decrypt")
|
||||
pat=(bin[12]^0x00)&0xff
|
||||
myprint(f"decrypt image:{hex(pat)}({_PatternType.get(pat,'unknown')})")
|
||||
for index in range(len(bin)):
|
||||
bin[index]=bin[index]^pat
|
||||
with open(hex_file_name,mode='w+',encoding="utf-8") as f:
|
||||
bin_to_hex(bin,f)
|
||||
|
||||
|
||||
def bin_file_decrypt(bin_file:str):
|
||||
with open(bin_file,mode='rb') as f:
|
||||
bin=bytearray(f.read())
|
||||
# 自动判断是否需要解密
|
||||
if ((bin[12]==bin[13])and(bin[12]!=0x00)):
|
||||
pat=(bin[12]^0x00)&0xff
|
||||
myprint(f"decrypt image:{hex(pat)}({_PatternType.get(pat,'unknown')})")
|
||||
for index in range(len(bin)):
|
||||
bin[index]=bin[index]^pat
|
||||
else:
|
||||
pat=bin[1]
|
||||
myprint(f"copy image:{hex(pat)}({_PatternType.get(pat,'unknown')})")
|
||||
with open('tmp.bin',mode='wb+') as f:
|
||||
f.write(bin)
|
||||
return "tmp.bin"
|
||||
|
||||
|
||||
|
||||
# bin_to_hex.py input_file output_file
|
||||
if __name__ == "__main__":
|
||||
# bin_to_hex_file(sys.argv[1],sys.argv[2],sys.argv[3])
|
||||
|
28
kunlun.py
28
kunlun.py
@@ -13,6 +13,7 @@ import binascii
|
||||
from log import myprint
|
||||
from log import log_init
|
||||
from bin_to_hex import bin_to_hex_file
|
||||
from bin_to_hex import bin_file_decrypt
|
||||
|
||||
|
||||
def init_send(s_port:serial.Serial, send_str):
|
||||
@@ -59,7 +60,7 @@ def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
continue
|
||||
print_device_str(tmp)
|
||||
s_info += tmp
|
||||
if(s_info.find(b'C')>=0 and bytes2read==1):
|
||||
if(s_info.find(b'CCC')>=0 and bytes2read==1):
|
||||
m_flash=True
|
||||
else:
|
||||
m_flash=False
|
||||
@@ -79,10 +80,12 @@ def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
|
||||
xmodem_send = x_modem.send(stream, quiet=True, callback=download_callback)
|
||||
xmodem_send = x_modem.send(stream, quiet=True, callback=download_callback,retry=16)
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_1 = (etime - stime).seconds
|
||||
myprint ("\nTransferring %s result: %s, consuming time: %d s \n" % (f_file,xmodem_send, trans_time_1))
|
||||
if(xmodem_send is False):
|
||||
break
|
||||
elif m_done:
|
||||
myprint("Update done.")
|
||||
break
|
||||
@@ -107,9 +110,9 @@ def print_device_str(data:bytearray):
|
||||
for item in data:
|
||||
try:
|
||||
d=item.decode('utf-8')
|
||||
myprint(d.strip())
|
||||
myprint("DEVICE:",d.strip())
|
||||
except Exception as e:
|
||||
myprint(f" {item.hex()}")
|
||||
myprint("DEVICE:",item)
|
||||
|
||||
# 上传固件
|
||||
def upload_bin(x_modem:xmodem.XMODEM, w_file):
|
||||
@@ -250,8 +253,17 @@ if __name__ == '__main__':
|
||||
|
||||
# 发送启动字符让设备进入xmodem模式
|
||||
init_send(ser, init_str.encode("utf-8"))
|
||||
burn_ram_bin(modem, ram_file)
|
||||
# burn_ram_bin(modem, "kl3_ram_build.bin")
|
||||
|
||||
upload_fun()
|
||||
# burn_fun()
|
||||
# burn_ram_bin(modem, ram_file)
|
||||
# upload_fun()
|
||||
|
||||
burn_ram_bin(modem, "bin/kl3_ram_build.bin")
|
||||
dir_path="\\\\10.0.15.200\\内网拷贝进来的\\ran.chuan\\"
|
||||
for item in os.listdir(dir_path):
|
||||
if(item.endswith('.bin')):
|
||||
iot_flash_file=os.path.join(dir_path,item)
|
||||
break
|
||||
# iot_flash_file="C:\\new\\buildout\\kl3_sta_kunlun.bin"
|
||||
iot_flash_file="C:\\new\\buildout\\HZ-SG-TCE3PS03.bin"
|
||||
iot_flash_file=bin_file_decrypt(iot_flash_file)
|
||||
burn_fun()
|
||||
|
9
log.py
9
log.py
@@ -12,9 +12,12 @@ def myprint_dec(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
# 在这里添加额外的功能
|
||||
print(*args, **kwargs)
|
||||
kwargs["file"]=_log_fp
|
||||
result = func(*args, **kwargs)
|
||||
_log_fp.flush()
|
||||
if(_log_fp is not None):
|
||||
kwargs["file"]=_log_fp
|
||||
result = func(*args, **kwargs)
|
||||
_log_fp.flush()
|
||||
else:
|
||||
result=None
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
|
Reference in New Issue
Block a user