提高bin_to_hex的速度

This commit is contained in:
2024-10-14 22:01:52 +08:00
parent 531a857057
commit 3057c6f5a1
2 changed files with 13 additions and 46 deletions

View File

@@ -50,6 +50,8 @@ _DevType={
_PKT_HEADER_MAGIC_NO = 0x00005566
_IMG_HEADER_MAGIC_NO = 0xa4e49a17
# 从info文件读取flash信息
def load_flash_info(file_name:str):
flash_info_list:list[dict]=[]
with open(file_name,mode='r',encoding='utf-8') as f:
@@ -193,30 +195,8 @@ def img_header_check(data:bytearray):
return ""
def bin_to_hex(bin:bytearray,flash_info:list[dict]=[]):
all_size=len(bin)
pack_size=32
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_hex2(bin:bytearray):
# 转化为hex文本
def bin_to_hex(bin:bytearray,f):
all_size=len(bin)
pack_size=32
turned=0
@@ -238,25 +218,21 @@ def bin_to_hex2(bin:bytearray):
hdr_data=bytearray()
hdr_v=img_header_check(hdr_data)
if(hdr_v=='V0'):
out_text+=tran_img_headerv0(hdr_data)+'\n'
out_text=tran_img_headerv0(hdr_data)+'\n'
f.write(out_text)
elif(hdr_v=='V1'):
out_text+=tran_img_headerv1(hdr_data)+'\n'
out_text+=f"[{hex(turned_old)}] {data.hex(' ')}\n"
return out_text
out_text=tran_img_headerv1(hdr_data)+'\n'
f.write(out_text)
out_text=f"[{hex(turned_old)}] {data.hex(' ')}\n"
f.write(out_text)
def bin_to_hex_file(bin_file_name:str,hex_file_name:str,info_file_name:str=None):
def bin_to_hex_file(bin_file_name:str,hex_file_name:str):
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_hex2(bin)
print(f"write to file {hex_file_name}")
with open(hex_file_name,mode='w+',encoding="utf-8") as f:
f.write(text)
bin_to_hex(bin,f)
# bin_to_hex.py input_file output_file
if __name__ == "__main__":

View File

@@ -143,15 +143,6 @@ def putc(data, timeout=1):
'''
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):
@@ -201,7 +192,7 @@ def upload_fun():
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())
bin_to_hex_file(calc_upload_name(), calc_hex_name())
myprint("Transform to hex end.")