上传flash镜像并转化为txt
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
log.txt
|
||||
work/
|
||||
__pycache__/
|
106
bin_to_hex.py
Normal file
106
bin_to_hex.py
Normal file
@@ -0,0 +1,106 @@
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
|
||||
# kunlun1,kunlun2,kunlun3的img类型
|
||||
_ImgType={
|
||||
0x0000:"imgROM", # Maybe,ROM doesn't need a imgType */
|
||||
0xFC00:"imgSP", # sp */
|
||||
0xFC01:"imgSBL", # sbl */
|
||||
0xFC02:"imgFRW", # kl1: 1MB fw, uncompressed for sta, run in flash; compressed for cco, run in psram */
|
||||
0xFC03:"imgCFG", # The first pib file. */
|
||||
0xFC04:"imgEFUSE", # efuse */
|
||||
0xFC05:"imgOEM", # oem */
|
||||
0xFC06:"imgFRW2", # The second fw, maybe the backup one. not used. */
|
||||
0xFC07:"imgCFG2", # The second pib file. */
|
||||
0xFC08:"imgCFRW1", # kl1: 2MB fw, compressed for sta or cco, run in flash; kl2: 4M flash fw */
|
||||
0xFC09:"imgCFRW2", # The second fw, maybe the backup one. not used. */
|
||||
0xFC0A:"imgCFRW", # kl1: 2MB fw, compressed for sta or cco, run in psram; kl2: 4M psram fw */
|
||||
0xFC0B:"imgREF", # ref */
|
||||
0xFC0C:"imgCUS", # cus */
|
||||
0xFC0D:"imgCFRW4F", # kl1: 4MB fw, compressed for sta or cco, run in flash */
|
||||
0xFC0E:"imgCFRW4P", # kl1: 4MB fw, compressed for sta or cco, run in psram */
|
||||
0xFFFF:"imgMAX", # invalid */
|
||||
0x00:"imgV1ROM", # Maybe,ROM doesn't need a imgType */
|
||||
0x01:"imgV1SBL", # sbl */
|
||||
0x02:"imgV1FRWPLC", # FW-PLC */
|
||||
0x03:"imgV1CFG", # The first pib file. */
|
||||
0x05:"imgV1OEM", # oem */
|
||||
0x06:"imgV1FRWCUS", # FW-CUS */
|
||||
0xFF:"imgV1MAX" # invalid */
|
||||
}
|
||||
|
||||
|
||||
|
||||
_DevType={
|
||||
0xDC01:"devkunlun1",
|
||||
0xDC02:"devkunlun2",
|
||||
0xDC03:"devkunlun3",
|
||||
0xFFFF:"devMax",
|
||||
0x01:"devV1kunlun1",
|
||||
0x02:"devV1kunlun2",
|
||||
0x03:"devV1kunlun3",
|
||||
0xFF:"devV1Max"
|
||||
}
|
||||
|
||||
|
||||
def load_flash_info(file_name:str):
|
||||
flash_info_list:list[dict]=[]
|
||||
with open(file_name,mode='r',encoding='utf-8') as f:
|
||||
lines=f.readlines()
|
||||
for line in lines:
|
||||
# 如果开头两个字符是数字
|
||||
info = re.match(r"^\d{2}",line)
|
||||
if(info):
|
||||
# print(info.group(0))
|
||||
sp=line.split()
|
||||
flash_info={}
|
||||
flash_info["DevType"]=_DevType[int(sp[1],base=16)]
|
||||
flash_info["ImgType"]=_ImgType[int(sp[2],base=16)]
|
||||
flash_info["Offset"]=int(sp[3],base=16)
|
||||
flash_info["Size"]=int(sp[4])
|
||||
flash_info_list.append(flash_info)
|
||||
return flash_info_list
|
||||
|
||||
|
||||
def bin_to_hex(bin:bytearray,flash_info:list[dict]=[]):
|
||||
all_size=len(bin)
|
||||
pack_size=64
|
||||
turned=0
|
||||
turned_old=0
|
||||
out_text=''
|
||||
index=0
|
||||
while turned < all_size:
|
||||
if(turned+pack_size<=all_size):
|
||||
data=bin[turned:turned+pack_size]
|
||||
turned_old=turned
|
||||
turned+=pack_size
|
||||
else:
|
||||
data=bin[turned:]
|
||||
turned=all_size
|
||||
if(index<len(flash_info)):
|
||||
if(turned_old==(flash_info[index])["Offset"]):
|
||||
out_text+=f"{(flash_info[index])['ImgType']}----------------------\n"
|
||||
index+=1
|
||||
out_text+=f"[{hex(turned_old)}] {data.hex(' ')}\n"
|
||||
return out_text
|
||||
|
||||
|
||||
|
||||
|
||||
def bin_to_hex_file(bin_file_name:str,hex_file_name:str,info_file_name:str=None):
|
||||
with open(bin_file_name,mode='rb') as f:
|
||||
bin=f.read()
|
||||
if info_file_name is None:
|
||||
info=[]
|
||||
else:
|
||||
info=load_flash_info(info_file_name)
|
||||
text=bin_to_hex(bin,info)
|
||||
with open(hex_file_name,mode='w+',encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
|
||||
# bin_to_hex.py input_file output_file
|
||||
if __name__ == "__main__":
|
||||
bin_to_hex_file(sys.argv[1],sys.argv[2],sys.argv[3])
|
||||
# load_flash_info("work/upload-test.info")
|
142
kunlun.py
142
kunlun.py
@@ -10,6 +10,9 @@ import time
|
||||
import xmodem
|
||||
import os
|
||||
import binascii
|
||||
from log import myprint
|
||||
from log import log_init
|
||||
from bin_to_hex import bin_to_hex_file
|
||||
|
||||
|
||||
def init_send(s_port:serial.Serial, send_str):
|
||||
@@ -28,36 +31,31 @@ def init_send(s_port:serial.Serial, send_str):
|
||||
m_ram=False
|
||||
|
||||
if m_ram:
|
||||
print ("Program enters transmission mode...")
|
||||
myprint ("Program enters transmission mode...")
|
||||
break
|
||||
|
||||
|
||||
def burn_ram_bin(x_modem:xmodem.XMODEM, r_file):
|
||||
global trans_time_0
|
||||
wf = open(log_file, 'a')
|
||||
stime = datetime.datetime.now()
|
||||
print ("Transferring %s..." % r_file)
|
||||
wf.writelines("Transferring %s..." % r_file)
|
||||
myprint ("Transferring %s..." % r_file)
|
||||
try:
|
||||
stream = open(r_file, 'rb')
|
||||
except Exception:
|
||||
print("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
|
||||
xmodem_send = x_modem.send(stream, callback=status_update_0)
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_0 = (etime - stime).seconds
|
||||
print ("\nTransferring ram.bin result: %s, consuming time: %s s \n" % (xmodem_send, trans_time_0))
|
||||
wf.writelines("Transferring %s..." % r_file)
|
||||
wf.close()
|
||||
myprint ("\nTransferring ram.bin result: %s, consuming time: %s s \n" % (xmodem_send, trans_time_0))
|
||||
|
||||
def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
global trans_time_1
|
||||
s_info = bytearray()
|
||||
wf = open(log_file, 'a')
|
||||
while True:
|
||||
s_info += s_port.read(1)
|
||||
print(f"recv:{s_info.hex(' ')}")
|
||||
myprint(f"recv:{s_info.hex(' ')}")
|
||||
bytes2read = s_port.in_waiting
|
||||
tmp = s_port.read(bytes2read)
|
||||
s_info += tmp
|
||||
@@ -71,95 +69,117 @@ def burn_flash_bin(s_port:serial.Serial, x_modem:xmodem.XMODEM, f_file):
|
||||
m_done=False
|
||||
|
||||
if m_flash:
|
||||
# print m_flash.group(0)
|
||||
print (r"Recieving FLASH-IMAGE in xmodem : C")
|
||||
wf.writelines("Recieving FLASH-IMAGE in xmodem : C")
|
||||
# myprint m_flash.group(0)
|
||||
myprint (r"Recieving FLASH-IMAGE in xmodem : C")
|
||||
s_info = bytearray()
|
||||
stime = datetime.datetime.now()
|
||||
print ("Transferring %s..." % f_file)
|
||||
wf.writelines("Transferring %s..." % f_file)
|
||||
myprint ("Transferring %s..." % f_file)
|
||||
try:
|
||||
stream = open(f_file, 'rb')
|
||||
except Exception:
|
||||
print("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
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=status_update_1)
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_1 = (etime - stime).seconds
|
||||
print ("\nTransferring iot_flash.bin result: %s, consuming time: %d s \n" % (xmodem_send, trans_time_1))
|
||||
wf.writelines("\nTransferring iot_flash.bin result: %s, consuming time: %d s \n" % (xmodem_send, trans_time_1))
|
||||
myprint ("\nTransferring iot_flash.bin result: %s, consuming time: %d s \n" % (xmodem_send, trans_time_1))
|
||||
elif m_done:
|
||||
print (m_done.group(0))
|
||||
myprint (m_done.group(0))
|
||||
break
|
||||
else:
|
||||
pass
|
||||
wf.close()
|
||||
|
||||
|
||||
def upload_callback(total_packets, success_count, error_count, packet_size):
|
||||
# callback(total_packets, success_count, error_count, packet_size)
|
||||
print("upload:",total_packets, success_count, error_count, packet_size)
|
||||
myprint("upload:",total_packets, success_count, error_count, packet_size)
|
||||
|
||||
# 上传固件
|
||||
def upload_bin(x_modem:xmodem.XMODEM, w_file):
|
||||
global trans_time_0
|
||||
wf = open(log_file, 'a')
|
||||
stime = datetime.datetime.now()
|
||||
print ("Transferring %s..." % w_file)
|
||||
wf.writelines("Transferring %s..." % w_file)
|
||||
myprint ("Transferring %s..." % w_file)
|
||||
try:
|
||||
stream = open(w_file, 'wb+')
|
||||
except Exception:
|
||||
print("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
myprint("Cannot load file, please check the file path and retry. Press <enter> to exit")
|
||||
sys.exit()
|
||||
|
||||
xmodem_send = x_modem.recv(stream, callback=upload_callback)
|
||||
etime = datetime.datetime.now()
|
||||
trans_time_0 = (etime - stime).seconds
|
||||
print ("\nTransferring ram.bin result: %s, consuming time: %s s \n" % (xmodem_send, trans_time_0))
|
||||
wf.writelines("Transferring %s..." % w_file)
|
||||
wf.close()
|
||||
myprint ("\nTransferring ram.bin result: %s, consuming time: %s s \n" % (xmodem_send, trans_time_0))
|
||||
|
||||
|
||||
|
||||
|
||||
def getc(size, timeout=1):
|
||||
data=ser.read(size)
|
||||
print("getc:",data.hex(' '))
|
||||
# myprint("getc:",data.hex(' '))
|
||||
return data or None
|
||||
|
||||
|
||||
def putc(data, timeout=1):
|
||||
# print("putc:",data.hex(' '))
|
||||
# myprint("putc:",data.hex(' '))
|
||||
ser.write(data)
|
||||
time.sleep(0.03)
|
||||
|
||||
|
||||
def status_update_0(total_packets, success_count, error_count):
|
||||
print ("total_packets: %s, success_count: %s, error_count: %d" % (total_packets, success_count, error_count))
|
||||
myprint ("total_packets: %s, success_count: %s, error_count: %d" % (total_packets, success_count, error_count))
|
||||
# if total_packets % 10 == 0:
|
||||
# print ('.'),
|
||||
# myprint ('.'),
|
||||
|
||||
|
||||
def status_update_1(total_packets, success_count, error_count):
|
||||
print ("total_packets: %s, success_count: %s, error_count: %d" % (total_packets, success_count, error_count))
|
||||
myprint ("total_packets: %s, success_count: %s, error_count: %d" % (total_packets, success_count, error_count))
|
||||
# if total_packets % 50 == 0:
|
||||
# print ('.'),
|
||||
# myprint ('.'),
|
||||
|
||||
log_file = "log.txt"
|
||||
|
||||
|
||||
|
||||
|
||||
'''
|
||||
IMG_TYPE 0xfc00 sp.bin
|
||||
IMG_TYPE 0xfc01 sbl.bin
|
||||
IMG_TYPE 0xfc05 oem.bin
|
||||
IMG_TYPE 0xfc03 pib.bin
|
||||
IMG_TYPE 0xfc08 ht.bin
|
||||
|
||||
'''
|
||||
|
||||
|
||||
_work_dir='work'
|
||||
if not os.path.exists(_work_dir):
|
||||
os.mkdir(_work_dir)
|
||||
|
||||
|
||||
def calc_log_file_name():
|
||||
log_file=sys.argv[1].split('.')[0]+'.log'
|
||||
return os.path.join(_work_dir,log_file)
|
||||
|
||||
def calc_upload_name():
|
||||
name=sys.argv[1]
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
def calc_hex_name():
|
||||
name=sys.argv[1]+'.txt'
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
def calc_flash_info_name():
|
||||
name=sys.argv[1].split('.')[0]+'.info'
|
||||
return os.path.join(_work_dir,name)
|
||||
|
||||
|
||||
# kunlun.py [upload.bin]
|
||||
if __name__ == '__main__':
|
||||
if(len(sys.argv)<2):
|
||||
print("param too less")
|
||||
exit(-1)
|
||||
|
||||
initial_info = """
|
||||
*** Version 11.0.0.1
|
||||
*** This is the tool for chip files transmission
|
||||
*** You can modify xmodem_config.txt to configure
|
||||
*** Serial Port, Baud Rate, Transfer Files Path
|
||||
*** Press RST Button to continue the operation...
|
||||
"""
|
||||
print (initial_info)
|
||||
wf = open(log_file, 'w+')
|
||||
wf.writelines(initial_info)
|
||||
log_init(calc_log_file_name())
|
||||
exit_flag = 0
|
||||
trans_time_0, trans_time_1 = 0, 0
|
||||
config_file = r"xmodem_config.txt"
|
||||
@@ -185,34 +205,40 @@ if __name__ == '__main__':
|
||||
try:
|
||||
ser = serial.Serial(port='COM' + str(sp_num), baudrate=b_rate, timeout=0.3)
|
||||
except Exception:
|
||||
print("Serial Port COM%s Conflicts!!! Press <enter> to Close it and retry..." % str(sp_num))
|
||||
wf.writelines("Serial Port COM%s Conflicts!!! Press <enter> to Close it and retry..." % str(sp_num))
|
||||
myprint("Serial Port COM%s Conflicts!!! Press <enter> to Close it and retry..." % str(sp_num))
|
||||
sys.exit()
|
||||
modem = xmodem.XMODEM(getc, putc, mode='xmodem1k')
|
||||
|
||||
# 发送启动字符让设备进入xmodem模式
|
||||
# init_send(ser, init_str.encode("utf-8"))
|
||||
# burn_ram_bin(modem, ram_file)
|
||||
init_send(ser, init_str.encode("utf-8"))
|
||||
burn_ram_bin(modem, ram_file)
|
||||
|
||||
# ser.baudrate=nb_rate
|
||||
time.sleep(0.5)
|
||||
# # ser.baudrate=nb_rate
|
||||
# time.sleep(0.5)
|
||||
|
||||
# burn_flash_bin(ser, modem, iot_flash_file)
|
||||
# 显示flash信息
|
||||
ser.write(b"f i\n")
|
||||
time.sleep(0.5)
|
||||
print(ser.read(4096).decode('utf-8'))
|
||||
ser_read_data=ser.read(4096).decode('utf-8')
|
||||
myprint(ser_read_data)
|
||||
with open(calc_flash_info_name(),mode='w+',encoding='utf-8') as f:
|
||||
f.writelines(ser_read_data.split('\r\n'))
|
||||
# 显示image信息
|
||||
ser.write(b"f s\n")
|
||||
time.sleep(0.5)
|
||||
print(ser.read(4096).decode('utf-8'))
|
||||
ser_read_data=ser.read(4096).decode('utf-8')
|
||||
myprint(ser_read_data)
|
||||
with open(calc_flash_info_name(),mode='a+',encoding='utf-8') as f:
|
||||
f.writelines(ser_read_data.split('\r\n'))
|
||||
# 上传整个镜像
|
||||
ser.write(b"fw u d all\n")
|
||||
time.sleep(0.5)
|
||||
print(ser.read(4096).decode('utf-8'))
|
||||
# upload_bin(modem,"upload.bin")
|
||||
myprint(ser.read(4096).decode('utf-8'))
|
||||
upload_bin(modem,calc_upload_name())
|
||||
|
||||
print ("Total transmission time: %s s" % str(trans_time_0+trans_time_1))
|
||||
wf.writelines("Total transmission time: %s s" % str(trans_time_0+trans_time_1))
|
||||
wf.close()
|
||||
myprint ("Total transmission time: %s s" % str(trans_time_0+trans_time_1))
|
||||
myprint("Start transform bin to hex.")
|
||||
bin_to_hex_file(calc_upload_name(), calc_hex_name(),calc_flash_info_name())
|
||||
myprint("Transform to hex end.")
|
||||
|
||||
|
27
log.py
Normal file
27
log.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import time
|
||||
|
||||
|
||||
|
||||
# 同一个进程中所有调用这个文件的 .py 文件都使用这个变量
|
||||
_log_fp=None
|
||||
|
||||
def _time():
|
||||
return '['+time.strftime("%Y-%m-%d %H:%M:%S")+']'
|
||||
|
||||
def myprint_dec(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
# 在这里添加额外的功能
|
||||
print(*args, **kwargs)
|
||||
kwargs["file"]=_log_fp
|
||||
result = func(*args, **kwargs)
|
||||
_log_fp.flush()
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
myprint=myprint_dec(print)
|
||||
|
||||
def log_init(file_name:str):
|
||||
global _log_fp
|
||||
if _log_fp is None:
|
||||
_log_fp=open(file_name,mode="w+",encoding="utf-8")
|
||||
|
Reference in New Issue
Block a user