Shofel2_T124_python/tools.py

143 lines
6.0 KiB
Python

import string
JETSON_TK1_VID= 0x0955
JETSON_TK1_PID= 0x7140
SHIELD_TK1_VID =0x0955
SHIELD_TK1_PID= 0x7f40
IROM_BEGIN = 0x00100000
IROM_END = 0x0010FFFF
IROM_LEN = 0x00010000
IRAM_BEGIN = 0x40000000
IRAM_END = 0x4003FFFF
IRAM_LEN = 0x00040000
BOOTROM_DO_BCT_BOOT = 0x00100624
BOOTROM_EP1_IN_WRITE_IMM = 0x001065C0
BOOTROM_EP1_OUT_READ_IMM = 0x00106612
BOOTROM_USB_BUF_1 = 0x40004000
BOOTROM_USB_BUF_2 = 0x40008000
BOOTROM_PAYLOAD_ENTRY = 0x4000E000
BOOTROM_SMASH_TARGET = 0x4000DCD8
BOOTROM_STACK_GAP_LEN = 0x30C
BOOTROM_SMASH_LEN = (BOOTROM_SMASH_TARGET - BOOTROM_USB_BUF_2) #// 0x5CD8
VARS_LEN= 0x10
INTERMEZZO_LEN =0x100
INTERMEZZO_REL_ADD =( BOOTROM_PAYLOAD_ENTRY - INTERMEZZO_LEN ) #// 0x4000DF00
OFFSET_INTERMEZZO_START = 0x0
OFFSET_PAYLOAD_START = ( INTERMEZZO_LEN )
OFFSET_MEMCPY_RET_ADD = ( BOOTROM_SMASH_LEN - BOOTROM_STACK_GAP_LEN - 0x4 ) #// 0x59C8 ( 0x30C Bytes copied from the stack before entry )
OFFSET_PAYLOAD_BEF_LENVAR = ( OFFSET_MEMCPY_RET_ADD - 0x4 )
OFFSET_PAYLOAD_AFT_LENVAR = ( OFFSET_MEMCPY_RET_ADD - 0x8 )
OFFSET_PAYLOAD_THUMB_MODE = ( OFFSET_MEMCPY_RET_ADD - 0xC )
OFFSET_PAYLOAD_CONT = ( OFFSET_MEMCPY_RET_ADD + 0x4 )
IRAM_ADD_INTERMEZZO_START = ( BOOTROM_PAYLOAD_ENTRY + OFFSET_INTERMEZZO_START )
IRAM_ADD_PAYLOAD_START = ( BOOTROM_PAYLOAD_ENTRY + OFFSET_PAYLOAD_START )
IRAM_ADD_PAYLOAD_BEF_LENVAR = ( BOOTROM_PAYLOAD_ENTRY + OFFSET_PAYLOAD_BEF_LENVAR )
IRAM_ADD_PAYLOAD_AFT_LENVAR= ( BOOTROM_PAYLOAD_ENTRY + OFFSET_PAYLOAD_AFT_LENVAR )
IRAM_ADD_PAYLOAD_THUMB_MODE= ( BOOTROM_PAYLOAD_ENTRY + OFFSET_PAYLOAD_THUMB_MODE )
IRAM_ADD_PAYLOAD_CONT = ( BOOTROM_PAYLOAD_ENTRY + OFFSET_PAYLOAD_CONT )
RCM_EP1_IN = 0x81
RCM_EP1_OUT = 0x01
RCM_CHIP_ID_LEN =0x10
RCM_CMD_LEN =0x32274
RCM_CMD_MAX_USEFUL_LEN = 0x31000 # Ensures Header + Payload + Padding doesn't complete RCM CMD and buffer 2 is used for getstatus.
RCM_CMD_HEADER_LEN = 0x284
RCM_CMD_BUF_INTERMEZZO_START = ( RCM_CMD_HEADER_LEN + OFFSET_INTERMEZZO_START )
RCM_CMD_BUF_PAYLOAD_START = ( RCM_CMD_HEADER_LEN + OFFSET_PAYLOAD_START )
RCM_CMD_BUF_MEMCPY_RET_ADD = ( RCM_CMD_HEADER_LEN + OFFSET_MEMCPY_RET_ADD )
RCM_CMD_BUF_PAYLOAD_BEF_LENVAR = ( RCM_CMD_HEADER_LEN + OFFSET_PAYLOAD_BEF_LENVAR )
RCM_CMD_BUF_PAYLOAD_AFT_LENVAR = ( RCM_CMD_HEADER_LEN + OFFSET_PAYLOAD_AFT_LENVAR )
RCM_CMD_BUF_PAYLOAD_THUMB_MODE = ( RCM_CMD_HEADER_LEN + OFFSET_PAYLOAD_THUMB_MODE )
RCM_CMD_BUF_PAYLOAD_CONT = ( RCM_CMD_HEADER_LEN + OFFSET_PAYLOAD_CONT )
MAX_PAYLOAD_BEF_SIZE = ( OFFSET_PAYLOAD_THUMB_MODE - OFFSET_PAYLOAD_START ) # 22716 Bytes
MAX_PAYLOAD_AFT_SIZE = ( RCM_CMD_MAX_USEFUL_LEN - RCM_CMD_BUF_PAYLOAD_CONT ) # 177072 Bytes
MAX_PAYLOAD_FILE_SIZE = ( MAX_PAYLOAD_BEF_SIZE + MAX_PAYLOAD_AFT_SIZE ) # 199788 Bytes
SECURE_BOOT_BASE= 0x6000C200
SB_CSR_0 = 0x0
SB_PIROM_START_0 = 0x4
SB_PFCFG_0 = 0x8
JTAG_ON = 0x00000080
APB_BASE =0x70000000
APB_MISC_PP_CONFIG_CTL_0= 0x24
APB_MISC_PP_CONFIG_CTL_0_JTAG = 0x40
APB_MISC_PP_CONFIG_CTL_0_TBE = 0x80
FLOW_CTLR_BASE = 0x60007000
FLOW_CTLR_HALT_COP_EVENTS_0 = 0x4
FLOW_CTLR_HALT_COP_FLOW_MODE_WAITEVENT =(1 << 30)
FLOW_CTLR_HALT_COP_JTAG = (1 << 28)
PMC_BASE = 0x7000e400
PMC_CNTRL = 0x000
PMC_CNTRL_MAIN_RST = (1 << 4)
PMC_SCRATCH0 = 0x050
PMC_SCRATCH0_MODE_RCM = (1 << 1)
FUSE_BASE = 0x7000F900
FUSE_LEN = 0x300
PAYLOAD_SIZE = 0x32274
USB_CTRL_DEVICE_ENDPOINT_TO_HOST = 0x82
USB_CTRL_GET_STATUS = 0x00
SUCCESS_STATUS = 0
ERROR_STATUS = 4
def info(msg):
print("[\033[34;1mi\033[0m] %s" % (msg))
def ok(msg):
print("[\033[32;1m+\033[0m] %s" % (msg))
def warn(msg):
print("[\033[33;1mw\033[0m] %s" % (msg))
def error(msg):
print("[\033[31;1m!\033[0m] %s" % (msg))
def hexdump(buf, title="", color=6, start=0, remove_dup=True):
color_start = "\033[3%d;1m" % color
color_start_no_bold = "\033[0m\033[3%dm" % color
color_stop = "\033[0m"
address_format_size = len("0x%08x " % (len(buf) + start))
space_before = " "*address_format_size
out=("%s%s"+""*49+""+""*18+"%s\n") % (space_before, color_start,color_stop)
if title != "":
dashlen = int((46-len(title))/2)
out=("%s%s"+""*dashlen+" "+title+" "+""*(dashlen-(1-(len(title)%2)))+""+""*18+"%s\n") % (space_before, color_start,color_stop)
last_is_dup = False
for i in range(0,len(buf),16):
if remove_dup:
if i != 0 and (i+16) < len(buf):
if buf[i:i+16] == buf[i-16:i] and buf[i:i+16] == buf[i+16:i+32]:
if not last_is_dup:
out+="%s%s* ┆ %s" % (space_before[:-2], color_start, color_start_no_bold)
out+=""*47
out+="%s%s" % (color_start, color_start_no_bold)
out+=""*16
out+=" %s%s\n" % (color_start, color_stop)
last_is_dup = True
continue
else:
last_is_dup=False
out+="%s0x%08x%s" % (color_start,i+start,color_stop)
for j in range(16):
if i+j < len(buf):
if type(buf) == bytes:
out+="%02x " % (buf[i+j])
else:
out+="%02x " % (ord(buf[i+j]))
else:
out+=" "
out+="%s%s" % (color_start,color_stop)
for j in range(16):
if i+j < len(buf):
char = buf[i+j]
if type(char) == int:
char = chr(char)
if char in string.printable and char not in "\t\n\r\x0b\x0c":
out+="%s" % (char)
else:
out+="."
else:
out+=" "
out+=" %s%s\n" % (color_start,color_stop)
out+=("%s%s"+""*49+""+""*18+"%s") % (space_before, color_start,color_stop)
print(out)