成功提取oem数据并解析

This commit is contained in:
2024-10-16 23:59:20 +08:00
parent 0cc0a6cac1
commit 9a40af3319
2 changed files with 40 additions and 28 deletions

View File

@@ -233,6 +233,7 @@ def bin_to_hex(bin:bytearray,f,oem_file=None):
turned+=pack_size turned+=pack_size
else: else:
data=bin[turned:] data=bin[turned:]
turned_old=turned
turned=all_size turned=all_size
if(all_size-turned_old>=64): if(all_size-turned_old>=64):
hdr_data=bin[turned_old:turned_old+64] hdr_data=bin[turned_old:turned_old+64]
@@ -253,7 +254,7 @@ def bin_to_hex(bin:bytearray,f,oem_file=None):
if(oem_file is not None): if(oem_file is not None):
if(_ImgType[hdr_data[1]]=="imgV1OEM"): if(_ImgType[hdr_data[1]]=="imgV1OEM"):
size=int(ret["imgSize"]) size=int(ret["imgSize"])
oem_bin=bin[turned_old+32:turned_old+32+size] oem_bin=bin[turned_old+64:turned_old+64+size]
oem_file.write(oem_bin) oem_file.write(oem_bin)
out_text=f"[{hex(turned_old)}] {data.hex(' ')}\n" out_text=f"[{hex(turned_old)}] {data.hex(' ')}\n"
f.write(out_text) f.write(out_text)
@@ -272,17 +273,19 @@ def bin_to_hex_file(bin_file_name:str,hex_file_name:str,enc=0):
with open(hex_file_name,mode='w+',encoding="utf-8") as f: with open(hex_file_name,mode='w+',encoding="utf-8") as f:
# oem_bin 去除原始后缀 .bin.txt 添加新后缀 -oem.bin # oem_bin 去除原始后缀 .bin.txt 添加新后缀 -oem.bin
oem_name=hex_file_name[0:-8]+'-oem.bin' oem_name=hex_file_name[0:-8]+'-oem.bin'
oem_name=os.path.normpath(oem_name)
with open(oem_name,mode='wb+') as oem_f: with open(oem_name,mode='wb+') as oem_f:
kl=bin_to_hex(bin,f,oem_f) kl=bin_to_hex(bin,f,oem_f)
# 调用oem_tool 打印oem信息 # 调用oem_tool 打印oem信息
myprint(bin_path()) oem_tool=os.path.join(bin_path(),f"{kl}_oem.exe")
oem_tool=os.path.join(bin_path(),f"{kl}_oem.exe") if(os.path.exists(oem_tool)):
if(os.path.exists(oem_tool)): cmd_str=f"{oem_tool} --parse={oem_name}"
cmd_str=f"{oem_tool} --parse={oem_name}" with os.popen(cmd_str) as f:
myprint(cmd_str) for line in f.readlines():
os.system(cmd_str) myprint(line.strip())
else: # os.system(cmd_str)
myprint(f"oem_tool not found: {oem_tool}") else:
myprint(f"oem_tool not found: {oem_tool}")
def bin_file_decrypt(bin_file:str): def bin_file_decrypt(bin_file:str):
with open(bin_file,mode='rb') as f: with open(bin_file,mode='rb') as f:

View File

@@ -236,17 +236,30 @@ def global_def():
global time_stamp_start global time_stamp_start
global time_stamp_end global time_stamp_end
global upload global upload
global encrypt
init_str="WQKL" init_str="WQKL"
serial_com=sys.argv[1] serial_com=sys.argv[1]
b_rate=115200 b_rate=115200
nb_rate=1500000 nb_rate=1500000
arg_num=len(sys.argv)
if(sys.argv[3]=='upload'): if(arg_num==2):
upload=True if(sys.argv[1].endswith('.bin')):
elif(sys.argv[3]=='download'): iot_flash_file=sys.argv[1]
upload=False return
else: elif(arg_num==3):
if(sys.argv[1].endswith('.bin')):
iot_flash_file=sys.argv[1]
encrypt=1
return
elif(arg_num==5):
if(sys.argv[3]=='upload'):
upload=True
elif(sys.argv[3]=='download'):
upload=False
iot_flash_file=sys.argv[4]
elif(arg_num==4):
if(sys.argv[3].endswith('.bin')): if(sys.argv[3].endswith('.bin')):
if(os.path.exists(sys.argv[3])): if(os.path.exists(sys.argv[3])):
# 存在这个文件 是下载 # 存在这个文件 是下载
@@ -257,12 +270,12 @@ def global_def():
else: else:
print("param err.") print("param err.")
sys.exit(-1) sys.exit(-1)
iot_flash_file=sys.argv[3]
if(upload): if(upload):
ram_file=f'bootram_{sys.argv[2]}.bin' ram_file=f'bootram_{sys.argv[2]}.bin'
else: else:
ram_file=f'{sys.argv[2]}_ram_build.bin' ram_file=f'{sys.argv[2]}_ram_build.bin'
ram_file=os.path.join(bin_path(),ram_file) ram_file=os.path.join(bin_path(),ram_file)
iot_flash_file=sys.argv[-1]
def print_help(): def print_help():
@@ -281,23 +294,19 @@ def print_help():
# kunlun.py [com] [kl1/kl3] [upload/download] [upload.bin/download.bin] # kunlun.py [com] [kl1/kl3] [upload/download] [upload.bin/download.bin]
# kunlun.py [file.bin] <enc> # kunlun.py [file.bin] <enc>
if __name__ == '__main__': if __name__ == '__main__':
if(len(sys.argv)<4):
if(len(sys.argv)>=2):
if(sys.argv[1].endswith('.bin')):
if(len(sys.argv)>=3):
enc=1
else:
enc=0
bin_to_hex_file(sys.argv[1],"work/"+sys.argv[1]+".txt",enc)
sys.exit(0)
print("param too less")
print_help()
sys.exit(-1)
upload=None upload=None
time_stamp_start, time_stamp_end = 0, 0 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 init_str, serial_com, b_rate, nb_rate, ram_file, iot_flash_file, ser = None, None, None, None, None, None, None
encrypt=0
global_def() global_def()
if(iot_flash_file is None):
print("param err.")
print_help()
sys.exit(-1)
log_init(calc_log_file_name()) log_init(calc_log_file_name())
if(ram_file is None):
bin_to_hex_file(iot_flash_file,calc_hex_name(),encrypt)
sys.exit(0)
try: try:
ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3) ser = serial.Serial(port=serial_com, baudrate=b_rate, timeout=0.3)
except Exception: except Exception: