Create a gist now

Instantly share code, notes, and snippets.

@Palantir555 /binmaker.py Secret
Created May 21, 2016

What would you like to do?
#!/usr/local/bin/python
import sys
DUMP_MISO=True
DUMP_MOSI=True
MERGE_MEMORY_SEGMENTS=True
def strip_line(line):
line=line.rstrip('\n')
line=line.replace('0x','')
return (line.split(',')) #time, packet_id, mosi, miso
def analyse_file(csv_file):
''' Return dict format:
{int(packet_id): (int(address), bytearray(payload)}
'''
LAST_FAKE_ID = 4242424242
miso_ba_dic = {}
mosi_ba_dic = {}
written_reg = ""
miso_ba = bytearray()
mosi_ba = bytearray()
reading_packet = False
current_packet = "-1" #This will force the first `if` iteration to fail
current_cmd = '00'
current_rw_block = '000000'
for line in csv_file:
time, packet_id, mosi, miso = strip_line(line)
try:
if packet_id != "": #Last packet doesnt have an ID (analyser bug)
packet_id=int(packet_id)
else:
packet_id=LAST_FAKE_ID
except Exception as e:
print "Non-hex characters detected. Ignoring line [{0}]".format(
line.rstrip('\n'))
continue #Mostly so we don't need to strip the 1st line from the file
#The first byte will always be a command, so we're not losing data here:
if current_packet == packet_id:
if current_cmd=='02': #Page Programming
mosi_ba_dic[current_packet][1].extend(mosi.decode('hex'))
elif current_cmd=='03': #Read Data bytes
miso_ba_dic[current_packet][1].extend(miso.decode('hex'))
elif current_cmd=='01':
written_reg+=mosi
elif current_cmd=='05': #Read Status Register
pass #Ignore response. Will `continue`
elif current_cmd=='D8': #64 kB Sector Erase
pass #Shouldn't happen
elif current_cmd=='06': #Write Enable
pass #Shouldn't happen
else:
print "ERROR Unrecognised command [{0}]".format(current_cmd)
continue
elif current_packet != "-1":
if current_cmd in ['02', '03', 'D8', '06']:
print "Finished sniffing command [{0}]".format(current_cmd)
elif current_cmd == '01':
print "Written Status&Config Register [{0}]".format(written_reg)
written_reg=""
current_packet = packet_id #Next time we'll process the packet
current_cmd=mosi
if mosi=='02': #Page Programming
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
current_rw_block = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
if id != "":
mosi_ba_dic[current_packet] = (current_rw_block, bytearray())
else:
mosi_ba_dic[LAST_FAKE_ID] = (current_rw_block, bytearray())
print "Programming Address [{0}]".format(hex(current_rw_block))
continue
elif mosi=='03': #Read Data bytes
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
current_rw_block = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
if id != "":
miso_ba_dic[current_packet] = (current_rw_block, bytearray())
else:
miso_ba_dic[LAST_FAKE_ID] = (current_rw_block, bytearray())
print "Reading Address [{0}]".format(hex(current_rw_block))
continue
elif mosi=='05': #Read Status Register
t, id, si, status_reg = strip_line(csv_file.next())
print "Status Register Contents [{0}]".format(status_reg)
continue
elif mosi=='06': #Write Enable
continue #This command is just 1 byte
elif mosi=='D8': #64 kB Sector Erase
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
addr = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
print "Erasing Address [{0}]".format(hex(addr))
continue #We will ignore this one
elif mosi=='01': #Write (Status & Configuration) Register
continue
generate_files(miso_ba_dic, mosi_ba_dic)
def generate_files(miso_dic, mosi_dic):
'''
DUMP_MISO=True
DUMP_MOSI=True
MERGE_MEMORY_SEGMENTS=True
'''
last_id = 0
distance_threshold = 20
miso_final_ba = bytearray()
mosi_final_ba = bytearray()
miso_sorted_list = sorted(miso_dic.items())
mosi_sorted_list = sorted(mosi_dic.items())
print "___________________"
print "|Transmission Map|"
print "| MISO | MOSI |"
while (len(miso_sorted_list) != 0) and (len(mosi_sorted_list) != 0):
try: head_miso_id = miso_sorted_list[0][0]
except Exception as e: head_miso_id = 0xFFFFFFFFFF #Print MOSI
try: head_mosi_id = mosi_sorted_list[0][0]
except Exception as e: head_mosi_id = 0xFFFFFFFFFF #Print MISO
if head_miso_id < head_mosi_id:
pkt = miso_sorted_list.pop(0)
print "|{0:#8x}| |".format(pkt[1][0]) #Address
else:
pkt = mosi_sorted_list.pop(0)
print "| |{0:#8x}|".format(pkt[1][0]) #Address
if DUMP_MISO:
print "Dumped MISO blocks:",
for id, (addr, ba) in iter(sorted(miso_dic.items())):
#print ba
if MERGE_MEMORY_SEGMENTS:
out_miso_path = inp_name + "_miso.bin"
with open(out_miso_path, 'ab') as f:
f.write(ba)
else:
out_miso_path = inp_name + "_miso_{0}.bin".format(hex(addr))
with open(out_miso_path, 'ab') as f:
f.write(ba)
if DUMP_MOSI:
print "\nDumped MOSI blocks:",
for id, (addr, ba) in iter(sorted(mosi_dic.items())):
#print ba
print "{0}".format(hex(addr)),
if MERGE_MEMORY_SEGMENTS:
out_mosi_path = inp_name + "_mosi.bin"
with open(out_mosi_path, 'ab') as f:
f.write(ba)
else:
out_mosi_path = inp_name + "_mosi_{0}.bin".format(hex(addr))
with open(out_mosi_path, 'ab') as f:
f.write(ba)
if __name__ == '__main__':
inp_path = sys.argv[1]
inp_name = inp_path.rsplit('.', 1)[0] #Filename without the extension
out_miso_path = inp_name + "_miso.bin"
out_mosi_path = inp_name + "_mosi.bin"
with open(inp_path, 'r') as file:
analyse_file(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment