添加提取bin文件中log的功能
This commit is contained in:
113
bin/get_log.py
Normal file
113
bin/get_log.py
Normal file
@@ -0,0 +1,113 @@
|
||||
import os
|
||||
import sys
|
||||
import struct
|
||||
import dataclasses
|
||||
from bin.log import myprint
|
||||
from bin.log import mywrite
|
||||
|
||||
STR_YELLOW="\033[0;33m"
|
||||
STR_END="\033[0m"
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class cli_msg_hdr_t:
|
||||
src_mac:bytearray
|
||||
target_mac:bytearray
|
||||
module_id:int
|
||||
cli_crc:int
|
||||
msg_id:int
|
||||
auto_ack:int
|
||||
reserved2:int
|
||||
msg_len:int
|
||||
sn:int
|
||||
@classmethod
|
||||
def __init__(self, data:bytearray):
|
||||
self.src_mac=data[0:6]
|
||||
self.target_mac=data[6:12]
|
||||
self.module_id=struct.unpack("<H", data[12:14])[0]
|
||||
self.cli_crc=struct.unpack("<H", data[14:16])[0]
|
||||
self.msg_id=struct.unpack("<H", data[16:18])[0]
|
||||
self.auto_ack=struct.unpack("<B", data[18:19])[0]&0x01
|
||||
self.reserved2=struct.unpack("<B", data[19:20])[0]
|
||||
self.msg_len=struct.unpack("<H", data[20:22])[0]
|
||||
self.sn=struct.unpack("<H", data[22:24])[0]
|
||||
|
||||
@dataclasses.dataclass
|
||||
class payload_t:
|
||||
fw_version:int
|
||||
time_stamp:int
|
||||
seq:int
|
||||
mod_msg:int
|
||||
payload_len:int
|
||||
@classmethod
|
||||
def __init__(self, data:bytearray):
|
||||
self.fw_version=struct.unpack("<I", data[0:4])[0]
|
||||
self.time_stamp=struct.unpack("<I", data[4:8])[0]
|
||||
self.seq=struct.unpack("<I", data[8:12])[0]
|
||||
self.mod_msg=struct.unpack("<I", data[12:16])[0]
|
||||
self.payload_len=struct.unpack("<I", data[16:20])[0]>>16
|
||||
|
||||
|
||||
|
||||
|
||||
def get_log(log_file:str):
|
||||
with open(log_file, 'rb') as f:
|
||||
data=f.read()
|
||||
save=data[0x143000:0x144580]
|
||||
with open("save.bin", 'wb+') as f:
|
||||
f.write(save)
|
||||
|
||||
def find_log(data):
|
||||
for i in range(0, len(data), 0x20):
|
||||
if data[i:i+2]==b'$$':
|
||||
myprint(f"log offset={hex(i)}")
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def print_log(log_file:str):
|
||||
with open(log_file, 'rb') as f:
|
||||
data=f.read()
|
||||
off=0
|
||||
# addr表示相对于bin文件的地址
|
||||
addr=off
|
||||
if(off<0):
|
||||
myprint("not found log")
|
||||
return
|
||||
data=data[off:]
|
||||
while len(data)>0:
|
||||
off=data.find(b'$$')
|
||||
if(off<0):
|
||||
break
|
||||
addr+=off
|
||||
myprint(f"off={hex(addr)}")
|
||||
data=data[off+2:]
|
||||
addr+=2
|
||||
end=data.find(b'&&')
|
||||
if(end<0):
|
||||
break
|
||||
a=cli_msg_hdr_t(data[0:24])
|
||||
# msg_len减去的是payload的长度
|
||||
myprint(f"msg_len={a.msg_len-20}")
|
||||
if(a.msg_len!=end-24):
|
||||
myprint(f"msg_len error")
|
||||
continue
|
||||
# a=payload_t(data[24:44])
|
||||
# print(a)
|
||||
pstr=data[44:end]
|
||||
for i in range(len(pstr)):
|
||||
if(pstr[i]&0x80):
|
||||
pstr=pstr[:i]
|
||||
break
|
||||
try:
|
||||
mywrite(f"{pstr.decode('utf-8')}")
|
||||
except:
|
||||
mywrite(f"{pstr}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python get_log.py <log_file>")
|
||||
sys.exit(1)
|
||||
log_file = sys.argv[1]
|
||||
print_log(log_file)
|
10
kunlun.py
10
kunlun.py
@@ -24,6 +24,7 @@ from bin.bin_to_hex import check_bin_type
|
||||
from bin.base import bin_path
|
||||
from bin.factory_mode import ftm_handle
|
||||
from bin.crc import CRC16
|
||||
from bin.get_log import print_log
|
||||
|
||||
|
||||
def init_send(s_port:serial.Serial, send_str:str):
|
||||
@@ -379,6 +380,7 @@ def global_def():
|
||||
global layout_index
|
||||
global log_file
|
||||
global skip_ram
|
||||
global extract_log
|
||||
args = parser.parse_args()
|
||||
|
||||
init_str="WQKL"
|
||||
@@ -389,6 +391,7 @@ def global_def():
|
||||
log_timeout=args.timeout
|
||||
layout_index=args.layout_index
|
||||
skip_ram=args.skip_ram
|
||||
extract_log=args.extract_log
|
||||
|
||||
|
||||
# 上传或者下载flash镜像
|
||||
@@ -464,6 +467,7 @@ def parser_init():
|
||||
parser.add_argument('--ftm',action='store_true',default=False,help='进入工厂模式')
|
||||
parser.add_argument('--list',action='store_true',default=False,help='列出所有.bin文件')
|
||||
parser.add_argument('--skip_ram',action='store_true',default=False,help='下载flash文件时是否跳过下载ram.bin的步骤,默认否')
|
||||
parser.add_argument('--extract_log',action='store_true',default=False,help='是否提取bin中的log信息,默认否')
|
||||
|
||||
|
||||
|
||||
@@ -481,7 +485,7 @@ if __name__ == '__main__':
|
||||
function_type=None # 功能类型 "download" 下载,"upload" 上传,"ram" 下载ram,"convert"" 转换bin
|
||||
time_stamp_start, time_stamp_end = 0, 0
|
||||
init_str, serial_com, b_rate, nb_rate, ram_file, iot_flash_file, ser = None, None, None, None, None, None, None
|
||||
upload_key, parser, layout_index, log_file=None, None, None, None
|
||||
upload_key, parser, layout_index, log_file, extract_log=None, None, None, None, False
|
||||
user_log=False
|
||||
log_timeout=0
|
||||
skip_ram=False
|
||||
@@ -494,6 +498,8 @@ if __name__ == '__main__':
|
||||
log_init(calc_log_file_name())
|
||||
if(function_type=='convert'):
|
||||
bin_to_hex_file(iot_flash_file,calc_hex_name(),layout_index)
|
||||
if(extract_log):
|
||||
print_log(iot_flash_file)
|
||||
sys.exit(0)
|
||||
if(function_type=="list"):
|
||||
l=list_bin_file()
|
||||
@@ -526,6 +532,8 @@ if __name__ == '__main__':
|
||||
|
||||
if(function_type=='upload'):
|
||||
upload_fun()
|
||||
if(extract_log):
|
||||
print_log(calc_upload_name())
|
||||
elif(function_type=='download'):
|
||||
burn_fun()
|
||||
clear_tmp()
|
||||
|
Reference in New Issue
Block a user