Skip to content

Instantly share code, notes, and snippets.

@d-schmidt
Last active April 27, 2016 17:40
Show Gist options
  • Select an option

  • Save d-schmidt/a7bbdc46a36536dbbdc90cb546995106 to your computer and use it in GitHub Desktop.

Select an option

Save d-schmidt/a7bbdc46a36536dbbdc90cb546995106 to your computer and use it in GitHub Desktop.
reading flash from Arcadyan/o2 IAD 6431 1.01.25b
#!/usr/bin/python
# -*- coding: utf-8 -*-
# see https://github.com/rvalles/brntool
#Keep python2 working
from __future__ import division #1/2 = float, 1//2 = integer
from __future__ import print_function #print("blah", file=whatever)
#Keep python2 working end
from optparse import OptionParser
import serial
import sys
#import struct
import re
lineregex = re.compile(r'0x(?:[0-9A-F]{8})((?: [0-9A-F]{2}){1,16})')
def get2menu(ser,verbose):
if verbose:
print("Waiting for a prompt...", file=sys.stderr)
while True:
print("sending", file=sys.stderr)
ser.write(" !".encode())
print("done", file=sys.stderr)
r = ser.read(1)
re = r
while r:
r = ser.read(1)
re += r
if(re[-2:] == b']:'):
while ser.read(256):
pass
if verbose:
print("Ok.", file=sys.stderr)
return
def memreadblock(ser,addr,size):
while ser.read(1):
pass
ser.write('R'.encode())
r = ser.read(100)
while not (r[-2:] == b'0x'):
print("R '" + r.decode() + "'", file=sys.stderr)
r = ser.read(100)
pass
ser.write(("%x"%addr).encode())
ser.write('\r'.encode())
r = ser.read(100)
while not (r[-3:] == b'...'):
print("A '" + r.decode() + "'", file=sys.stderr)
r = ser.read(100)
pass
ser.write('3'.encode())
r = ser.read(100)
while not (r[-1:] == b')'):
print("3 '" + r.decode() + "'", file=sys.stderr)
r = ser.read(100)
pass
ser.write(str(size).encode())
ser.write('\r'.encode())
buf=[]
m = False
while not m:
m = lineregex.match(ser.readline().strip().decode())
while m:
bytes = [int(x, 16) for x in m.group(1)[1:].split(' ')]
buf+=bytes
m = lineregex.match(ser.readline().strip().decode())
return buf
def memreadblock2file(ser,fd,addr,size):
while True:
buf=memreadblock(ser,addr,size)
if len(buf)==size:
break
sys.stderr.write('!')
sys.stderr.write('.')
fd.write(bytes(buf))
return
def memread(ser,path,addr,size,verbose):
#bs=1024
bs=10000 #10000 is usually the maximum size for an hexdump on brnboot.
get2menu(ser,verbose)
if path == "-":
fd=sys.stdout
else:
fd=open(path,"wb")
while 0<size:
if size>bs:
memreadblock2file(ser,fd,addr,bs)
size-=bs
addr+=bs
else:
memreadblock2file(ser,fd,addr,size)
size=0
fd.close()
return
def main():
optparser = OptionParser("usage: %prog [options]",version="%prog 0.1")
optparser.add_option("--verbose", action="store_true", dest="verbose", help="be verbose", default=False)
optparser.add_option("--serial", dest="serial", help="specify serial port", default="/dev/ttyUSB0", metavar="dev")
optparser.add_option("--read", dest="read", help="read mem to file", metavar="path")
#optparser.add_option("--write", dest="write",help="write mem from file", metavar="path")
optparser.add_option("--addr", dest="addr",help="mem address", metavar="addr")
optparser.add_option("--size", dest="size",help="size to copy", metavar="bytes")
(options, args) = optparser.parse_args()
if len(args) != 0:
optparser.error("incorrect number of arguments")
ser = serial.Serial(options.serial, 115200, timeout=1)
if options.read:
memread(ser,options.read,int(options.addr,0),int(options.size,0),options.verbose)
return
if __name__ == '__main__':
main()
@d-schmidt
Copy link
Copy Markdown
Author

brntool.py --serial COM3 --read=o2box6431.bin --addr=0xB0040000 --verbose --size=0x40000

@d-schmidt
Copy link
Copy Markdown
Author

tested with python 3.5 and pip install pyserial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment