aboutsummaryrefslogtreecommitdiff
path: root/scripts/bootprog.py
blob: ab7832595fb6895dec744d152407d8d86c271eba (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
183
184
185
186
187
188
189
#!/usr/bin/env python
#
# bootprog.py: STM32 SystemMemory Production Programmer -- version 1.1
# 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 serial
import struct
from time import sleep

class stm32_boot:
	def __init__(self, port, baud=115200):
		self.serial = serial.Serial(port, baud, 8, 'E', 1, 
				timeout=1)

		# Turn on target device in SystemMemory boot mode
		self.serial.setDTR(1)
		sleep(0.1);

		self._sync()

	def _sync(self):
		# Send sync byte
		#print "sending sync byte"
		self.serial.write("\x7F")
		self._checkack()

	def _sendcmd(self, cmd):
		if type(cmd) == int:
			cmd = chr(cmd)
		cmd += chr(ord(cmd) ^ 0xff)
		#print "sendcmd:", repr(cmd)
		self.serial.write(cmd)

	def _send(self, data):
		csum = 0
		for c in data: csum ^= ord(c)
		data = data + chr(csum)
		#print "sending:", repr(data)
		self.serial.write(data)

	def _checkack(self):
		ACK = "\x79"
		b = self.serial.read(1)
		if b != ACK: raise Exception("Invalid ack: %r" % b)
		#print "got ack!"



	def get(self):
		self._sendcmd("\x00")
		self._checkack()
		num = ord(self.serial.read(1))
		data = self.serial.read(num+1)
		self._checkack()
		return data

	def eraseall(self):
		# Send erase cmd
		self._sendcmd("\x43")
		self._checkack()
		# Global erase
		self._sendcmd("\xff")
		self._checkack()

	def read(self, addr, len):
		# Send read cmd
		self._sendcmd("\x11")
		self._checkack()
		# Send address
		self._send(struct.pack(">L", addr))
		self._checkack()
		# Send length
		self._sendcmd(chr(len-1))
		self._checkack()
		return self.serial.read(len)

	def write(self, addr, data):
		# Send write cmd
		self._sendcmd("\x31")
		self._checkack()
		# Send address
		self._send(struct.pack(">L", addr))
		self._checkack()
		# Send data
		self._send(chr(len(data)-1) + data)
		self._checkack()
	

	def write_protect(self, sectors):
		# Send WP cmd
		self._sendcmd("\x63")
		self._checkack()
		# Send sector list
		self._send(chr(len(sectors)-1) + "".join(chr(i) for i in sectors))
		self._checkack()
		# Resync after system reset
		self._sync()

	def write_unprotect(self):
		self._sendcmd("\x73")
		self._checkack()
		self._checkack()
		self._sync()

	def read_protect(self):
		self._sendcmd("\x82")
		self._checkack()
		self._checkack()
		self._sync()

	def read_unprotect(self):
		self._sendcmd("\x92")
		self._checkack()
		self._checkack()
		self._sync()


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

	if platform == "linux2":
		print  "\x1b\x5b\x48\x1b\x5b\x32\x4a" # clear terminal screen
	print "STM32 SystemMemory Production Programmer -- version 1.1"
	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/ttyUSB0"
	baud = 115200
	addr = 0x8000000
	try:
		opts, args = getopt(argv[1:], "b:d:a:")
		for opt in opts:
			if opt[0] == "-b": baud = int(opt[1])
			elif opt[0] == "-d": dev = opt[1]
			else: raise Exception()

		progfile = args[0]
	except:
		print "Usage %s [-d <dev>] [-b <baudrate>] [-a <address>] <filename>" % argv[0]
		print "\t-d : Use target on interface <dev> (default: %s)" % dev
		print "\t-b : Set device baudrate (default: %d)" % baud
		print "\t-a : Set programming address (default: 0x%X)" % addr
		print
		exit(-1)

	prog = open(progfile, "rb").read()
	boot = stm32_boot(dev, baud)

	cmds = boot.get()
	print "Target bootloader version: %d.%d\n" % (ord(cmds[0]) >> 4, ord(cmds[0]) % 0xf)

	print "Removing device protection..."
	boot.read_unprotect()
	boot.write_unprotect()
	print "Erasing target device..."
	boot.eraseall()
	addr = 0x8000000
	while prog:
		print ("Programming address 0x%08X..0x%08X...\r" % (addr, addr + min(len(prog), 255))),
		stdout.flush();
		boot.write(addr, prog[:256])
		addr += 256
		prog = prog[256:]

	print
	print "Enabling device protection..."
	boot.write_protect(range(0,2))
	#boot.read_protect()

	print "All operations completed."
	print