解析bin文件信息
This commit is contained in:
166
bin_to_hex.py
166
bin_to_hex.py
@@ -1,9 +1,11 @@
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import struct
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
# kunlun1,kunlun2,kunlun3的img类型
|
# kunlun1,kunlun2,kunlun3的img类型
|
||||||
|
# FRW是指未压缩的fw CFRW是指压缩之后的fw
|
||||||
_ImgType={
|
_ImgType={
|
||||||
0x0000:"imgROM", # Maybe,ROM doesn't need a imgType */
|
0x0000:"imgROM", # Maybe,ROM doesn't need a imgType */
|
||||||
0xFC00:"imgSP", # sp */
|
0xFC00:"imgSP", # sp */
|
||||||
@@ -45,6 +47,9 @@ _DevType={
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_PKT_HEADER_MAGIC_NO = 0x00005566
|
||||||
|
_IMG_HEADER_MAGIC_NO = 0xa4e49a17
|
||||||
|
|
||||||
def load_flash_info(file_name:str):
|
def load_flash_info(file_name:str):
|
||||||
flash_info_list:list[dict]=[]
|
flash_info_list:list[dict]=[]
|
||||||
with open(file_name,mode='r',encoding='utf-8') as f:
|
with open(file_name,mode='r',encoding='utf-8') as f:
|
||||||
@@ -64,9 +69,133 @@ def load_flash_info(file_name:str):
|
|||||||
return flash_info_list
|
return flash_info_list
|
||||||
|
|
||||||
|
|
||||||
|
# 查看是不是数据头,不是返回 "",是返回解析值
|
||||||
|
def tran_pkg_header(data:bytearray):
|
||||||
|
size=len(data)
|
||||||
|
ret={}
|
||||||
|
if(size>=96):
|
||||||
|
# 大写为无符号 B 单字节 H 双字节 I 4字节 L 4字节 Q 8字节
|
||||||
|
# f 单精度浮点 d 双精度浮点 s 字符串
|
||||||
|
a=struct.unpack('>BBHIIIIIIIIIIIIIIIIIIIIIIHBB',data)
|
||||||
|
if(a[5]==_PKT_HEADER_MAGIC_NO):
|
||||||
|
ret["enctype"]=str(a[0])
|
||||||
|
ret["pattern"]=hex(a[1])
|
||||||
|
ret["dev_type"]=hex(a[2])
|
||||||
|
ret["file_crc"]=hex(a[3])
|
||||||
|
ret["file_len"]=str(a[4])
|
||||||
|
ret["magic_no"]=hex(a[5])
|
||||||
|
ret["version"]=hex(a[6])
|
||||||
|
ret["sp_start"]=hex(a[7])
|
||||||
|
ret["sp_len"]=str(a[8])
|
||||||
|
ret["sbl_start"]=hex(a[9]) # 32bytes
|
||||||
|
ret["sbl_len"]=str(a[10])
|
||||||
|
ret["oem_start"]=hex(a[11])
|
||||||
|
ret["oem_len"]=str(a[12])
|
||||||
|
ret["pib_start"]=hex(a[13])
|
||||||
|
ret["pib_len"]=str(a[14])
|
||||||
|
ret["fw_start"]=hex(a[15])
|
||||||
|
ret["fw_len"]=str(a[16])
|
||||||
|
ret["efuse_start"]=hex(a[17]) # 64bytes
|
||||||
|
ret["efuse_len"]=str(a[18])
|
||||||
|
ret["fw2_start"]=hex(a[19])
|
||||||
|
ret["fw2_len"]=str(a[20])
|
||||||
|
ret["ref_start"]=hex(a[21])
|
||||||
|
ret["ref_len"]=str(a[22])
|
||||||
|
ret["cus_start"]=hex(a[23])
|
||||||
|
ret["cus_len"]=str(a[24])
|
||||||
|
ret["vendor_id"]=hex(a[25])
|
||||||
|
ret["img_type"]=hex(a[26])
|
||||||
|
ret["img_flag"]=hex(a[27])
|
||||||
|
t=str(ret)
|
||||||
|
t=t.replace(',','\n')
|
||||||
|
# t=json.dumps(ret, sort_keys=True, indent=2, separators=(',', ': '))
|
||||||
|
return t
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def tran_img_headerv0(data:bytearray):
|
||||||
|
size=len(data)
|
||||||
|
ret={}
|
||||||
|
if(size>=64):
|
||||||
|
# 大写为无符号 B 单字节 H 双字节 I 4字节 L 4字节 Q 8字节
|
||||||
|
# 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["imgSize"]=str(a[2])
|
||||||
|
ret["imgVer"]=hex(a[3])
|
||||||
|
ret["psramSize"]=str(a[4])
|
||||||
|
ret["hdrVer"]=hex(a[5])
|
||||||
|
ret["fwSize"]=str(a[6])
|
||||||
|
ret["imgCRC"]=hex(a[7])
|
||||||
|
ret["guard"]=hex(a[8])
|
||||||
|
ret["layoutIdx"]=hex(a[9])
|
||||||
|
# reserved 3bytes
|
||||||
|
ret["fwCRC"]=hex(a[13])
|
||||||
|
ret["sha256"]=data[32:].hex()
|
||||||
|
t=str(ret)
|
||||||
|
t=t.replace(',','\n')
|
||||||
|
return t
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def tran_img_headerv1(data:bytearray):
|
||||||
|
size=len(data)
|
||||||
|
ret={}
|
||||||
|
if(size>=64):
|
||||||
|
# 大写为无符号 B 单字节 H 双字节 I 4字节 L 4字节 Q 8字节
|
||||||
|
# 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["encType"]=hex(a[2])
|
||||||
|
ret["cfg"]=hex(a[3])
|
||||||
|
ret["imgSize"]=str(a[4])
|
||||||
|
ret["zipType"]=hex(a[5])
|
||||||
|
ret["flashSize"]=str(a[6])
|
||||||
|
ret["psramSize"]=hex(a[7])
|
||||||
|
ret["hdrVer"]=hex(a[8])
|
||||||
|
ret["imgVer"]=hex(a[9])
|
||||||
|
ret["imgCRC"]=hex(a[10])
|
||||||
|
ret["guard"]=hex(a[11])
|
||||||
|
ret["layoutIdx"]=hex(a[12])
|
||||||
|
# reserved 3bytes
|
||||||
|
ret["runAddr"]=hex(a[16])
|
||||||
|
ret["sha256"]=data[32:].hex()
|
||||||
|
t=str(ret)
|
||||||
|
t=t.replace(',','\n')
|
||||||
|
return t
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
# 判断是不是pkt_header
|
||||||
|
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)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 判断是不是img_header
|
||||||
|
def img_header_check(data:bytearray):
|
||||||
|
if(len(data)<32):
|
||||||
|
return ''
|
||||||
|
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}")
|
||||||
|
# 返回使用的结构体类型
|
||||||
|
if(imghdr_v==0x10):
|
||||||
|
return 'V1'
|
||||||
|
else:
|
||||||
|
return "V0"
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def bin_to_hex(bin:bytearray,flash_info:list[dict]=[]):
|
def bin_to_hex(bin:bytearray,flash_info:list[dict]=[]):
|
||||||
all_size=len(bin)
|
all_size=len(bin)
|
||||||
pack_size=64
|
pack_size=32
|
||||||
turned=0
|
turned=0
|
||||||
turned_old=0
|
turned_old=0
|
||||||
out_text=''
|
out_text=''
|
||||||
@@ -86,6 +215,34 @@ def bin_to_hex(bin:bytearray,flash_info:list[dict]=[]):
|
|||||||
out_text+=f"[{hex(turned_old)}] {data.hex(' ')}\n"
|
out_text+=f"[{hex(turned_old)}] {data.hex(' ')}\n"
|
||||||
return out_text
|
return out_text
|
||||||
|
|
||||||
|
# 这个是从固件里直接读出数据头
|
||||||
|
def bin_to_hex2(bin:bytearray):
|
||||||
|
all_size=len(bin)
|
||||||
|
pack_size=32
|
||||||
|
turned=0
|
||||||
|
turned_old=0
|
||||||
|
out_text=''
|
||||||
|
if(pkt_header_check(bin)):
|
||||||
|
out_text+=tran_pkg_header(bin[0:96])+'\n'
|
||||||
|
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(all_size-turned_old>=64):
|
||||||
|
hdr_data=bin[turned_old:turned_old+64]
|
||||||
|
else:
|
||||||
|
hdr_data=bytearray()
|
||||||
|
hdr_v=img_header_check(hdr_data)
|
||||||
|
if(hdr_v=='V0'):
|
||||||
|
out_text+=tran_img_headerv0(hdr_data)+'\n'
|
||||||
|
elif(hdr_v=='V1'):
|
||||||
|
out_text+=tran_img_headerv1(hdr_data)+'\n'
|
||||||
|
out_text+=f"[{hex(turned_old)}] {data.hex(' ')}\n"
|
||||||
|
return out_text
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -96,11 +253,12 @@ def bin_to_hex_file(bin_file_name:str,hex_file_name:str,info_file_name:str=None)
|
|||||||
info=[]
|
info=[]
|
||||||
else:
|
else:
|
||||||
info=load_flash_info(info_file_name)
|
info=load_flash_info(info_file_name)
|
||||||
text=bin_to_hex(bin,info)
|
text=bin_to_hex2(bin)
|
||||||
with open(hex_file_name,mode='w+',encoding="utf-8") as f:
|
with open(hex_file_name,mode='w+',encoding="utf-8") as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
|
|
||||||
# bin_to_hex.py input_file output_file
|
# bin_to_hex.py input_file output_file
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
bin_to_hex_file(sys.argv[1],sys.argv[2],sys.argv[3])
|
# bin_to_hex_file(sys.argv[1],sys.argv[2],sys.argv[3])
|
||||||
# load_flash_info("work/upload-test.info")
|
# load_flash_info("work/upload-test.info")
|
||||||
|
bin_to_hex_file(sys.argv[1],sys.argv[2])
|
||||||
|
Reference in New Issue
Block a user