Files
kunlun_ramtool/bin_to_hex.py
2024-10-12 19:16:00 +08:00

107 lines
3.1 KiB
Python

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")