aboutsummaryrefslogtreecommitdiff
path: root/scripts/hexprog.py
blob: 128e55ebb75ced3056fd383e7ff8f8fcf9ee665c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python

# hexprog.py: Python application to flash a target with an Intel hex file
# Copyright (C) 2011  Black Sphere Technologies
# Written by Gareth McMullin <gareth@blacksphere.co.nz>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gdb
import struct
import time

# Microcode sequence to erase option bytes
stub_opterase = '\nH\x0bIA`\x0bIA`\tI\x81`\tI\x81`\x01iA\xf0 \x01\x01aA\xf0@\x01\x01a\xc4hO\xf0\x01\x064B\xfa\xd1\x00\xbe\x00 \x02@#\x01gE\xab\x89\xef\xcd'
# Microcode sequence to program option bytes
stub_optprog = '\tJ\nKS`\nKS`\x08K\x93`\x08K\x93`\x13iC\xf0\x10\x03\x13a\x01\x80\xd4hO\xf0\x01\x064B\xfa\xd1\x00\xbe\x00 \x02@#\x01gE\xab\x89\xef\xcd'

def flash_write_hex(target, hexfile, progress_cb=None):
	target.flash_probe()
	f = open(hexfile)
	addrhi = 0
	for line in f:
		if line[0] != ':': raise Exception("Error in hex file")
		reclen = int(line[1:3], 16)
		addrlo = int(line[3:7], 16)
		rectype = int(line[7:9], 16);
		if sum(ord(x) for x in gdb.unhexify(line[1:11+reclen*2])) & 0xff != 0:
			raise Exception("Checksum error in hex file") 
		if rectype == 0: # Data record
			addr = (addrhi << 16) + addrlo
			data = gdb.unhexify(line[9:9+reclen*2])
			target.flash_write_prepare(addr, data)
			pass
		elif rectype == 4: # High address record
			addrhi = int(line[9:13], 16)
			pass
		elif rectype == 5: # Entry record
			pass
		elif rectype == 1: # End of file record
			break 
		else:
			raise Exception("Invalid record in hex file")

	try:
		target.flash_commit(progress_cb)
	except:
		print "Flash write failed! Is device protected?\n"
		exit(-1)
	

if __name__ == "__main__":
	from serial import Serial, SerialException
	from sys import argv, platform, stdout
	from getopt import getopt

	if platform == "linux2":
		print ("\x1b\x5b\x48\x1b\x5b\x32\x4a") # clear terminal screen
	print("Black Magic Probe -- Target Production Programming Tool -- version 1.0")
	print "Copyright (C) 2011  Black Sphere Technologies"
	print "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
	print("")

	dev = "COM1" if platform == "win32" else "/dev/ttyACM0"
	baud = 115200
	scan = "jtag_scan"
	targetno = 1
	unprot = False; prot = False

	try:
		opts, args = getopt(argv[1:], "sd:b:t:rR")
		for opt in opts:
			if opt[0] == "-s": scan = "swdp_scan"
			elif opt[0] == "-b": baud = int(opt[1])
			elif opt[0] == "-d": dev = opt[1]
			elif opt[0] == "-t": targetno = int(opt[1])
			elif opt[0] == "-r": unprot = True
			elif opt[0] == "-R": prot = True
			else: raise Exception()

		hexfile = args[0]
	except:
		print("Usage %s [-s] [-d <dev>] [-b <baudrate>] [-t <n>] <filename>" % argv[0])
		print("\t-s : Use SW-DP instead of JTAG-DP")
		print("\t-d : Use target on interface <dev> (default: %s)" % dev)
		print("\t-b : Set device baudrate (default: %d)" % baud)
		print("\t-t : Connect to target #n (default: %d)" % targetno)
		print("\t-r : Clear flash read protection before programming")
		print("\t-R : Enable flash read protection after programming (requires power-on reset)")
		print("")
		exit(-1)

	try:
		s = Serial(dev, baud, timeout=3)
		s.setDTR(1)
		while s.read(1024):
			pass

		target = gdb.Target(s)
		
	except SerialException, e:
		print("FATAL: Failed to open serial device!\n%s\n" % e[0])
		exit(-1)

	try:
		r = target.monitor("version")
		for s in r: print s,
	except SerialException, e:
		print("FATAL: Serial communication failure!\n%s\n" % e[0])
		exit(-1)
	except: pass

	print "Target device scan:"
	targetlist = None
	r = target.monitor(scan)
	for s in r: 
		print s,
	print

	r = target.monitor("targets")
	for s in r: 
		if s.startswith("No. Att Driver"): targetlist = []
		try:
			if type(targetlist) is list: 
				targetlist.append(int(s[:2]))
		except: pass

	#if not targetlist:
	#	print("FATAL: No usable targets found!\n")
	#	exit(-1)

	if targetlist and (targetno not in targetlist):
		print("WARNING: Selected target %d not available, using %d" % (targetno, targetlist[0]))
		targetno = targetlist[0]

	print("Attaching to target %d." % targetno)
	target.attach(targetno)
	time.sleep(0.1)

	if unprot:
		print("Removing device protection.")
		# Save option bytes for later
		#optbytes = struct.unpack("8H", target.read_mem(0x1FFFF800, 16))
		# Remove protection
		target.run_stub(stub_opterase, 0x20000000)
		target.run_stub(stub_optprog, 0x20000000, 0x1FFFF800, 0x5aa5)
		target.reset()
		time.sleep(0.1)

	for m in target.flash_probe():
		print("FLASH memory -- Offset: 0x%X  BlockSize:0x%X\n" % (m.offset, m.blocksize))

	def progress(percent):
		print ("Progress: %d%%\r" % percent),
		stdout.flush()

	print("Programming target")
	flash_write_hex(target, hexfile, progress)

	print("Resetting target")
	target.reset()

	if prot:
		print("Enabling device protection.")
		target.run_stub(stub_opterase, 0x20000000)
		target.run_stub(stub_optprog, 0x20000000, 0x1FFFF800, 0x00ff)
		target.reset()

	target.detach()

	print("\nAll operations complete!\n")