support ST-Link/V2-1
[gnuk/gnuk.git] / tool / stlinkv2.py
1 #! /usr/bin/python
2
3 """
4 stlinkv2.py - a tool to control ST-Link/V2
5
6 Copyright (C) 2012, 2013, 2015 Free Software Initiative of Japan
7 Author: NIIBE Yutaka <gniibe@fsij.org>
8
9 This file is a part of Gnuk, a GnuPG USB Token implementation.
10
11 Gnuk is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 Gnuk is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19 License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 """
24
25 from struct import *
26 import sys, time
27 import usb
28 from colorama import init as colorama_init, Fore, Back, Style
29 from array import array
30
31 # INPUT: binary file
32
33 # Assumes only single ST-Link/V2 device is attached to computer.
34
35 CORE_ID_CORTEX_M3=0x1ba01477
36 CORE_ID_CORTEX_M0=0x0bb11477
37
38 CHIP_ID_STM32F103xB=0x20036410
39 CHIP_ID_STM32F103xE=0x10016414
40 # CHIP_ID_STM32F0   0x20006440
41 # CHIP_ID_STM32F030??  0x10006444; FSM-55
42
43 GPIOA=0x40010800
44 GPIOB=0x40010C00
45 OPTION_BYTES_ADDR=0x1ffff800
46 RDP_KEY_F1=0x00a5                  # Unlock readprotection
47 RDP_KEY_F0=0x00aa                  # Unlock readprotection
48 FLASH_BASE_ADDR=0x40022000
49
50 FLASH_KEYR=    FLASH_BASE_ADDR+0x04
51 FLASH_OPTKEYR= FLASH_BASE_ADDR+0x08
52 FLASH_SR=      FLASH_BASE_ADDR+0x0c
53 FLASH_CR=      FLASH_BASE_ADDR+0x10
54 FLASH_AR=      FLASH_BASE_ADDR+0x14
55 FLASH_OBR=     FLASH_BASE_ADDR+0x1c
56
57 FLASH_KEY1=0x45670123
58 FLASH_KEY2=0xcdef89ab
59
60 FLASH_SR_BSY=      0x0001
61 FLASH_SR_PGERR=    0x0004
62 FLASH_SR_WRPRTERR= 0x0010
63 FLASH_SR_EOP=      0x0020
64
65 FLASH_CR_PG=     0x0001
66 FLASH_CR_PER=    0x0002
67 FLASH_CR_MER=    0x0004
68 FLASH_CR_OPTPG=  0x0010
69 FLASH_CR_OPTER=  0x0020
70 FLASH_CR_STRT=   0x0040
71 FLASH_CR_LOCK=   0x0080
72 FLASH_CR_OPTWRE= 0x0200
73
74 SPI1= 0x40013000
75
76 def uint32(v):
77     return v[0] + (v[1]<<8) + (v[2]<<16) + (v[3]<<24)
78
79 prog_flash_write_body = b"\x0D\x4A\x0B\x48\x0B\x49\x09\x4C\x01\x25\x14\x26\x00\x27\x25\x61" + \
80     b"\xC3\x5B\xCB\x53\xE3\x68\x2B\x42\xFC\xD1\x33\x42\x02\xD1\x02\x37\x97\x42\xF5\xD1" + \
81     b"\x00\x27\x27\x61\x00\xBE\xC0\x46\x00\x20\x02\x40\x3C\x00\x00\x20"
82 #   .SRC_ADDR: 0x2000003C
83 ## HERE comes: target_addr in 4-byte
84 #   .TARGET_ADDR
85 ## HERE comes: size in 4-byte
86 #   .SIZE
87
88 def gen_prog_flash_write(addr,size):
89     return prog_flash_write_body + pack("<I", addr) + pack("<I", size)
90
91 prog_option_bytes_write_body = b"\x0B\x48\x0A\x49\x08\x4A\x10\x23\x01\x24\x13\x61\x08\x80\xD0\x68" + \
92      b"\x20\x42\xFC\xD1\x02\x31\xFF\x20\x08\x80\xD0\x68\x20\x42\xFC\xD1\x00\x20\x10\x61" + \
93      b"\x00\xBE\xC0\x46\x00\x20\x02\x40"
94 ## HERE comes: target_addr in 4-byte
95 #   .TARGET_ADDR
96 ## HERE comes: option_bytes in 4-byte
97 #   .OPTION_BYTES
98
99 def gen_prog_option_bytes_write(addr,val):
100     return prog_option_bytes_write_body + pack("<I", addr) + pack("<I", val)
101
102 prog_blank_check_body = b"\x04\x49\x05\x4A\x08\x68\x01\x30\x02\xD1\x04\x31\x91\x42\xF9\xD1\x00\xBE\xC0\x46" + \
103                         b"\x00\x00\x00\x08"
104 ## HERE comes: end_addr in 4-byte
105 # .END_ADDR
106
107 def gen_prog_blank_check(size):
108     return prog_blank_check_body + pack("<I", 0x08000000 + size)
109
110
111 SRAM_ADDRESS=0x20000000
112 FLASH_BLOCK_SIZE_F1=16384       # Should be less than (20KiB - 0x0038)
113 FLASH_BLOCK_SIZE_F0=2048        # Should be less than (4KiB - 0x0038)
114 BLOCK_WRITE_TIMEOUT=80          # Increase this when you increase BLOCK_SIZE
115
116
117 class TimeOutError(Exception):
118     def __init__(self, msg):
119         self.msg = msg
120     def __str__(self):
121         return repr(self.msg)
122     def __repr__(self):
123         return "TimeoutError(" + self.msg + ")"
124
125 class OperationFailure(Exception):
126     def __init__(self, msg):
127         self.msg = msg
128     def __str__(self):
129         return repr(self.msg)
130     def __repr__(self):
131         return "OperationFailure(" + self.msg + ")"
132
133
134 class stlinkv2(object):
135     def __init__(self, dev):
136         if dev.idProduct == USB_VENDOR_STLINKV2:
137             self.__bulkout = 2
138         else:
139             self.__bulkout = 1
140         self.__bulkin  = 0x81
141
142         self.__timeout = 1000   # 1 second
143         conf = dev.configurations[0]
144         intf_alt = conf.interfaces[0]
145         intf = intf_alt[0]
146         if intf.interfaceClass != 0xff: # Vendor specific
147             raise ValueError("Wrong interface class.", intf.interfaceClass)
148         self.__devhandle = dev.open()
149         # ST-Link/V2-1 has other interfaces
150         # Some other processes or kernel would use it
151         # So, write access to configuration causes error
152         try:
153             self.__devhandle.setConfiguration(conf.value)
154         except:
155             pass
156         self.__devhandle.claimInterface(intf.interfaceNumber)
157         # self.__devhandle.setAltInterface(0)  # This was not good for libusb-win32 with wrong arg intf, new correct value 0 would be OK
158
159     def shutdown(self):
160         self.__devhandle.releaseInterface()
161
162     def execute_get(self, cmd, res_len):
163         self.__devhandle.bulkWrite(self.__bulkout, cmd, self.__timeout)
164         res = self.__devhandle.bulkRead(self.__bulkin, res_len, self.__timeout)
165         return res
166
167     def execute_put(self, cmd, data=None):
168         self.__devhandle.bulkWrite(self.__bulkout, cmd, self.__timeout)
169         if (data):
170             self.__devhandle.bulkWrite(self.__bulkout, data, self.__timeout)
171
172     def stl_mode(self):
173         v = self.execute_get(b"\xf5\x00", 2)
174         return (v[1] * 256 + v[0])
175
176     def exit_from_debug_swd(self):
177         self.execute_put(b"\xf2\x21\x00")
178         time.sleep(1)
179
180     def exit_from_dfu(self):
181         self.execute_put(b"\xf3\x07\x00")
182         time.sleep(1)
183
184     def exit_from_debug_swim(self):
185         self.execute_put(b"\xf4\x01\x00")
186         time.sleep(1)
187
188     def enter_swd(self):
189         self.execute_put(b"\xf2\x20\xa3")
190         time.sleep(1)
191
192     def get_status(self):
193         v = self.execute_get(b"\xf2\x01\x00", 2)
194         return (v[1] << 8) + v[0]
195     # RUN:128, HALT:129
196
197     def enter_debug(self):
198         v = self.execute_get(b"\xf2\x02\x00", 2)
199         return (v[1] << 8) + v[0]
200
201     def exit_debug(self):
202         self.execute_put(b"\xf2\x21\x00")
203
204     def reset_sys(self):
205         v = self.execute_get(b"\xf2\x03\x00", 2)
206         return (v[1] << 8) + v[0]
207
208     def read_memory(self, addr, length):
209         return self.execute_get(b"\xf2\x07" + pack('<IH', addr, length), length)
210
211     def read_memory_u32(self, addr):
212         return uint32(self.execute_get(b"\xf2\x07" + pack('<IH', addr, 4), 4))
213
214     def write_memory(self, addr, data):
215         return self.execute_put(b"\xf2\x08" + pack('<IH', addr, len(data)), data)
216
217     def write_memory_u32(self, addr, data):
218         return self.execute_put(b"\xf2\x08" + pack('<IH', addr, 4),
219                                 pack('<I', data))
220
221     def read_reg(self, regno):
222         return uint32(self.execute_get(b"\xf2\x05" + pack('<B', regno), 4))
223
224     def write_reg(self, regno, value):
225         return self.execute_get(b"\xf2\x06" + pack('<BI', regno, value), 2)
226
227     def write_debug_reg(self, addr, value):
228         return self.execute_get(b"\xf2\x35" + pack('<II', addr, value), 2)
229
230     def control_nrst(self, value):
231         return self.execute_get(b"\xf2\x3c" + pack('<B', value), 2)
232
233     def run(self):
234         v = self.execute_get(b"\xf2\x09\x00", 2)
235         return (v[1] << 8) + v[0]
236
237     def get_core_id(self):
238         v = self.execute_get(b"\xf2\x22\x00", 4)
239         return v[0] + (v[1]<<8) + (v[2]<<16) + (v[3]<<24)
240
241     def version(self):
242         v = self.execute_get(b"\xf1", 6)
243         val = (v[0] << 8) + v[1]
244         return ((val >> 12) & 0x0f, (val >> 6) & 0x3f, val & 0x3f)
245
246     # For FST-01-00 and FST-01: LED on, USB connect
247     def setup_gpio(self):
248         apb2enr = self.read_memory_u32(0x40021018)
249         apb2enr = apb2enr | 4 | 8 | 0x1000 # Enable port A, port B, and SPI1
250         self.write_memory_u32(0x40021018, apb2enr)    # RCC->APB2ENR
251         self.write_memory_u32(0x4002100c, 4|8|0x1000) # RCC->APB2RSTR
252         self.write_memory_u32(0x4002100c, 0)
253         self.write_memory_u32(GPIOA+0x0c, 0xffffffff) # ODR
254         self.write_memory_u32(GPIOA+0x04, 0x88888383) # CRH
255         self.write_memory_u32(GPIOA+0x00, 0xBBB38888) # CRL
256         self.write_memory_u32(GPIOB+0x0c, 0xffffffff) # ODR
257         self.write_memory_u32(GPIOB+0x04, 0x88888888) # CRH
258         self.write_memory_u32(GPIOB+0x00, 0x88888883) # CRL
259
260     # For FST-01-00 and FST-01: LED on, USB disconnect
261     def usb_disconnect(self):
262         self.write_memory_u32(GPIOA+0x0c, 0xfffffbff) # ODR
263
264     # For FST-01-00 and FST-01: LED off, USB connect
265     def finish_gpio(self):
266         self.write_memory_u32(GPIOA+0x0c, 0xfffffeff) # ODR
267         self.write_memory_u32(GPIOB+0x0c, 0xfffffffe) # ODR
268         apb2enr = self.read_memory_u32(0x40021018)
269         apb2enr = apb2enr &  ~(4 | 8 | 0x1000)
270         self.write_memory_u32(0x40021018, apb2enr)    # RCC->APB2ENR
271
272     def spi_flash_init(self):
273         self.write_memory_u32(SPI1+0x00, 0x0004); # CR1 <= MSTR
274         i2scfgr = self.read_memory_u32(SPI1+0x1c) # I2SCFGR
275         i2scfgr = i2scfgr & 0xf7ff                # 
276         self.write_memory_u32(SPI1+0x1c, i2scfgr); # I2SCFGR <= SPI mode
277         self.write_memory_u32(SPI1+0x10, 7);       # CRCPR <= 7
278         self.write_memory_u32(SPI1+0x04, 0x04);    # CR2 <= SSOE
279         self.write_memory_u32(SPI1+0x00, 0x0044);  # CR1 <= MSTR | SPE
280
281     def spi_flash_select(self, enable):
282         if enable:
283             self.write_memory_u32(GPIOA+0x0c, 0xffffffef) # ODR
284         else:
285             self.write_memory_u32(GPIOA+0x0c, 0xffffffff) # ODR
286
287     def spi_flash_sendbyte(self, v):
288         i = 0
289         while True:
290             status = self.read_memory_u32(SPI1+0x08) # SR
291             if status & 0x02 != 0:                   # TXE (Data Empty)
292                 break
293             time.sleep(0.01)
294             i = i + 1
295             if i > 10:
296                 raise TimeOutError('spi_flash_sendbyte')
297         self.write_memory_u32(SPI1+0x0c, v) # DR
298         i = 0
299         while True:
300             status = self.read_memory_u32(SPI1+0x08) # SR
301             if status & 0x01 != 0:                   # RXNE (Data Not Empty)
302                 break
303             time.sleep(0.01)
304             i = i + 1
305             if i > 10:
306                 raise TimeOutError('spi_flash_sendbyte')
307         v = self.read_memory_u32(SPI1+0x0c) # DR
308         return v
309
310     def spi_flash_read_id(self):
311         self.spi_flash_select(True)
312         self.spi_flash_sendbyte(0x9f)
313         t0 = self.spi_flash_sendbyte(0xa5)
314         t1 = self.spi_flash_sendbyte(0xa5)
315         t2 = self.spi_flash_sendbyte(0xa5)
316         self.spi_flash_select(False)
317         return (t0 << 16) | (t1 << 8) | t2
318
319     def protection(self):
320         return (self.read_memory_u32(FLASH_OBR) & 0x0002) != 0
321
322     def blank_check(self):
323         prog = gen_prog_blank_check(self.flash_size)
324         self.write_memory(SRAM_ADDRESS, prog)
325         self.write_reg(15, SRAM_ADDRESS)
326         self.run()
327         i = 0
328         while self.get_status() == 0x80:
329             time.sleep(0.050)
330             i = i + 1
331             if i >= 10:
332                 raise TimeOutError("blank check")
333
334         r0_value = self.read_reg(0)
335         return r0_value == 0
336
337     def option_bytes_read(self):
338         return self.read_memory_u32(OPTION_BYTES_ADDR)
339
340     def option_bytes_write(self,addr,val):
341         self.write_memory_u32(FLASH_KEYR, FLASH_KEY1)
342         self.write_memory_u32(FLASH_KEYR, FLASH_KEY2)
343         self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR)
344
345         self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY1)
346         self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY2)
347
348         prog = gen_prog_option_bytes_write(addr,val)
349         self.write_memory(SRAM_ADDRESS, prog)
350         self.write_reg(15, SRAM_ADDRESS)
351         self.run()
352         i = 0
353         while self.get_status() == 0x80:
354             time.sleep(0.050)
355             i = i + 1
356             if i >= 10:
357                 raise TimeOutError("option bytes write")
358
359         status = self.read_memory_u32(FLASH_SR)
360         self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK)
361         if (status & FLASH_SR_EOP) == 0:
362             raise OperationFailure("option bytes write")
363
364     def option_bytes_erase(self):
365         self.write_memory_u32(FLASH_KEYR, FLASH_KEY1)
366         self.write_memory_u32(FLASH_KEYR, FLASH_KEY2)
367         self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR)
368
369         self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY1)
370         self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY2)
371
372         self.write_memory_u32(FLASH_CR, FLASH_CR_OPTER)
373         self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_OPTER)
374
375         i = 0
376         while True:
377             status = self.read_memory_u32(FLASH_SR)
378             if (status & FLASH_SR_BSY) == 0:
379                 break
380             i = i + 1
381             if i >= 1000:
382                 break
383
384         self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK)
385         if (status & FLASH_SR_EOP) == 0:
386             raise OperationFailure("option bytes erase")
387
388     def flash_write_internal(self, addr, data, off, size):
389         prog = gen_prog_flash_write(addr,size)
390         self.write_memory(SRAM_ADDRESS, prog+data[off:off+size])
391         self.write_reg(15, SRAM_ADDRESS)
392         self.run()
393         i = 0
394         while self.get_status() == 0x80:
395             time.sleep(0.050)
396             i = i + 1
397             if i >= BLOCK_WRITE_TIMEOUT:
398                 raise TimeOutError("flash write")
399         status = self.read_memory_u32(FLASH_SR)
400         if (status & FLASH_SR_PGERR) != 0:
401             raise OperationFailure("flash write: write to not erased part")
402         if (status & FLASH_SR_WRPRTERR) != 0:
403             raise OperationFailure("flash write: write to protected part")
404
405     def flash_write(self, addr, data):
406         self.write_memory_u32(FLASH_KEYR, FLASH_KEY1)
407         self.write_memory_u32(FLASH_KEYR, FLASH_KEY2)
408         self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR)
409
410         off = 0
411         while True:
412             if len(data[off:]) > self.flash_block_size:
413                 size = self.flash_block_size
414                 self.flash_write_internal(addr, data, off, size)
415                 off = off + size
416                 addr = addr + size
417             else:
418                 size = len(data[off:])
419                 self.flash_write_internal(addr, data, off, size)
420                 break
421
422         self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK)
423
424     def flash_erase_all(self):
425         self.write_memory_u32(FLASH_KEYR, FLASH_KEY1)
426         self.write_memory_u32(FLASH_KEYR, FLASH_KEY2)
427         self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR)
428
429         self.write_memory_u32(FLASH_CR, FLASH_CR_MER)
430         self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_MER)
431
432         i = 0
433         while True:
434             status = self.read_memory_u32(FLASH_SR)
435             if (status & FLASH_SR_BSY) == 0:
436                 break
437             i = i + 1
438             time.sleep(0.050)
439             if i >= 100:
440                 break
441
442         self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK)
443
444         if (status & FLASH_SR_EOP) == 0:
445             raise OperationFailure("flash erase all")
446
447     def flash_erase_page(self, addr):
448         self.write_memory_u32(FLASH_KEYR, FLASH_KEY1)
449         self.write_memory_u32(FLASH_KEYR, FLASH_KEY2)
450
451         self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR)
452
453         self.write_memory_u32(FLASH_CR, FLASH_CR_PER)
454         self.write_memory_u32(FLASH_AR, addr)
455         self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_PER)
456
457         i = 0
458         while True:
459             status = self.read_memory_u32(FLASH_SR)
460             if (status & FLASH_SR_BSY) == 0:
461                 break
462             i = i + 1
463             if i >= 1000:
464                 break
465
466         self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK)
467
468         if (status & FLASH_SR_EOP) == 0:
469             raise OperationFailure("flash page erase")
470
471     def start(self):
472         mode = self.stl_mode()
473         if mode == 2:
474             self.exit_from_debug_swd()
475         elif mode == 5:
476             self.exit_from_debug_swim()
477         elif mode != 1 and mode != 4:
478             self.exit_from_dfu()
479         new_mode = self.stl_mode()
480         print("Change ST-Link/V2 mode %04x -> %04x" % (mode, new_mode))
481         self.control_nrst(2)
482         self.enter_swd()
483         s = self.get_status()
484         if s != 0x0080:
485             print("Status is %04x" % s)
486             self.run()
487             s = self.get_status()
488             if s != 0x0080:
489                 #                   DCB_DHCSR    DBGKEY
490                 self.write_debug_reg(0xE000EDF0, 0xA05F0000)
491                 s = self.get_status()
492                 if s != 0x0080:
493                     raise ValueError("Status of core is not running.", s)
494         mode = self.stl_mode()
495         if mode != 2:
496             raise ValueError("Failed to switch debug mode.", mode)
497
498         self.core_id = self.get_core_id()
499         if self.core_id == CORE_ID_CORTEX_M3:
500             self.chip_stm32 = True
501         elif self.core_id == CORE_ID_CORTEX_M0:
502             self.chip_stm32 = False
503         else:
504             raise ValueError("Unknown core ID", self.core_id)
505         if self.chip_stm32:
506             self.rdp_key = RDP_KEY_F1
507             self.flash_block_size = FLASH_BLOCK_SIZE_F1
508             self.require_nrst = False
509             self.external_spi_flash = True
510             self.protection_feature = True
511         else:
512             self.rdp_key = RDP_KEY_F0
513             self.flash_block_size = FLASH_BLOCK_SIZE_F0
514             self.require_nrst = True
515             self.external_spi_flash = False
516             self.protection_feature = False
517         return self.core_id
518
519     def get_chip_id(self):
520         if self.chip_stm32:
521             self.chip_id = self.read_memory_u32(0xE0042000)
522             self.flash_size = (self.read_memory_u32(0x1ffff7e0) & 0xffff)*1024
523             if self.chip_id == CHIP_ID_STM32F103xB:
524                 pass
525             elif self.chip_id == CHIP_ID_STM32F103xE:
526                 pass
527             else:
528                 raise ValueError("Unknown chip ID", self.chip_id)
529         else:
530             self.chip_id = self.read_memory_u32(0x40015800)
531             self.flash_size = (self.read_memory_u32(0x1ffff7cc) & 0xffff)*1024
532         print("Flash size: %dKiB" % (self.flash_size/1024))
533         return self.chip_id
534
535     def get_rdp_key(self):
536         return self.rdp_key
537
538     def has_spi_flash(self):
539         return self.external_spi_flash
540
541     def has_protection(self):
542         return self.protection_feature
543
544
545 USB_VENDOR_ST=0x0483            # 0x0483 SGS Thomson Microelectronics
546 USB_VENDOR_STLINKV2=0x3748      # 0x3748 ST-LINK/V2
547 USB_VENDOR_STLINKV2_1=0x374b    # 0x374b ST-LINK/V2_1
548
549 def stlinkv2_devices():
550     busses = usb.busses()
551     for bus in busses:
552         devices = bus.devices
553         for dev in devices:
554             if dev.idVendor != USB_VENDOR_ST:
555                 continue
556             if dev.idProduct != USB_VENDOR_STLINKV2 and dev.idProduct != USB_VENDOR_STLINKV2_1:
557                 continue
558             yield dev
559
560 def compare(data_original, data_in_device):
561     i = 0 
562     for d in data_original:
563         if d != data_in_device[i]:
564             raise ValueError("Verify failed at:", i)
565         i += 1
566
567 def open_stlinkv2():
568     for d in stlinkv2_devices():
569         try:
570             stl = stlinkv2(d)
571             return stl
572         except:
573             pass
574     return None
575
576 def help():
577     print("stlinkv2.py [-h]: Show this help message")
578     print("stlinkv2.py [-e]: Erase flash ROM")
579     print("stlinkv2.py [-u]: Unlock flash ROM")
580     print("stlinkv2.py [-s]: Show status")
581     print("stlinkv2.py [-b] [-n] [-r] [-i] FILE: Write content of FILE to flash ROM")
582     print("    -b: Blank check before write (auto erase when not blank)")
583     print("    -n: Don't enable read protection after write")
584     print("    -r: Don't reset after write")
585     print("    -i: Don't test SPI flash")
586
587
588 def main(show_help, erase_only, no_protect, spi_flash_check,
589          reset_after_successful_write,
590          skip_blank_check, status_only, unlock, data):
591     if show_help or len(sys.argv) != 1:
592         help()
593         return 1
594
595     stl = open_stlinkv2()
596     if not stl:
597         raise ValueError("No ST-Link/V2 device found.", None)
598
599     print("ST-Link/V2 version info: %d %d %d" % stl.version())
600     core_id = stl.start()
601
602     stl.control_nrst(2)
603     stl.enter_debug()
604     status = stl.get_status()
605     if status != 0x0081:
606         print("Core does not halt, try API V2 halt.")
607         #                   DCB_DHCSR    DBGKEY|C_HALT|C_DEBUGEN
608         stl.write_debug_reg(0xE000EDF0, 0xA05F0003)
609         status = stl.get_status()
610         stl.write_debug_reg(0xE000EDF0, 0xA05F0003)
611         status = stl.get_status()
612         if status != 0x0081:
613             raise ValueError("Status of core is not halt.", status)
614
615     chip_id = stl.get_chip_id()
616
617     # FST-01 chip id: 0x20036410
618     print("CORE: %08x, CHIP_ID: %08x" % (core_id, chip_id))
619     protection = stl.protection()
620     print("Flash ROM read protection: " + ("ON" if protection else "off"))
621     option_bytes = stl.option_bytes_read()
622     print("Option bytes: %08x" % option_bytes)
623     rdp_key = stl.get_rdp_key()
624     if (option_bytes & 0xff) == rdp_key:
625         ob_protection_enable = False
626     else:
627         ob_protection_enable = True
628
629     if protection:
630         if status_only:
631             print("The MCU is now stopped.")
632             return 0
633         elif not unlock:
634             raise OperationFailure("Flash ROM is protected")
635     else:
636         if not skip_blank_check:
637             stl.reset_sys()
638             blank = stl.blank_check()
639             print("Flash ROM blank check: %s" % blank)
640         else:
641             blank = True
642         if status_only:
643             stl.reset_sys()
644             stl.run()
645             stl.exit_debug()
646             return 0
647         elif unlock and not ob_protection_enable:
648             print("No need to unlock.  Protection is not enabled.")
649             return 1
650
651     if erase_only:
652         if blank:
653             print("No need to erase")
654             return 0
655
656     stl.setup_gpio()
657
658     if unlock:
659         if option_bytes != 0xff:
660             stl.reset_sys()
661             stl.option_bytes_erase()
662         stl.reset_sys()
663         stl.option_bytes_write(OPTION_BYTES_ADDR,rdp_key)
664         stl.usb_disconnect()
665         time.sleep(0.100)
666         stl.finish_gpio()
667         print("Flash ROM read protection disabled.  Reset the board, now.")
668         return 0
669
670     if spi_flash_check and stl.has_spi_flash():
671         stl.spi_flash_init()
672         id = stl.spi_flash_read_id()
673         print("SPI Flash ROM ID: %06x" % id)
674         if id != 0xbf254a:
675             raise ValueError("bad spi flash ROM ID")
676
677     if not blank:
678         print("ERASE ALL")
679         stl.reset_sys()
680         stl.flash_erase_all()
681
682     if erase_only:
683         stl.usb_disconnect()
684         time.sleep(0.100)
685         stl.finish_gpio()
686         return 0
687
688     time.sleep(0.100)
689
690     print("WRITE")
691     stl.flash_write(0x08000000, data)
692
693     print("VERIFY")
694     data_received = array('B')
695     size = len(data)
696     off = 0
697     while size > 0:
698         if size > 1024:
699             blk_size = 1024
700         else:
701             blk_size = size
702         data_received = data_received + array('B', stl.read_memory(0x08000000+off, blk_size))
703         size = size - blk_size
704         off = off + blk_size
705     compare(array('B', data), data_received)
706
707     if not no_protect and stl.has_protection():
708         print("PROTECT")
709         stl.option_bytes_erase()
710         print("Flash ROM read protection enabled.  Reset the board to enable protection.")
711
712     if reset_after_successful_write:
713         stl.control_nrst(2)
714         stl.usb_disconnect()
715         stl.reset_sys()
716         stl.run()
717         stl.exit_debug()
718     else:
719         stl.finish_gpio()
720
721     stl.shutdown()
722     return 0
723
724 if __name__ == '__main__':
725     show_help = False
726     erase_only = False
727     no_protect = False
728     reset_after_successful_write = True
729     skip_blank_check=True
730     status_only = False
731     unlock = False
732     data = None
733     spi_flash_check = True
734
735     while len(sys.argv) > 1:
736         if sys.argv[1] == '-h':
737             sys.argv.pop(1)
738             show_help = True
739             break
740         elif sys.argv[1] == '-e':
741             sys.argv.pop(1)
742             erase_only = True
743             skip_blank_check=False
744             break
745         elif sys.argv[1] == '-u':
746             sys.argv.pop(1)
747             unlock = True
748             break
749         elif sys.argv[1] == '-s':
750             sys.argv.pop(1)
751             status_only = True
752             skip_blank_check=False
753             break
754         elif sys.argv[1] == '-b':
755             skip_blank_check=False
756         elif sys.argv[1] == '-n':
757             no_protect = True
758         elif sys.argv[1] == '-r':
759             reset_after_successful_write = False
760         elif sys.argv[1] == '-i':
761             spi_flash_check = False
762         else:
763             filename = sys.argv[1]
764             f = open(filename,'rb')
765             data = f.read()
766             f.close()
767             if len(data) % 1:
768                 raise ValueError("The size of file should be even")
769         sys.argv.pop(1)
770
771     colorama_init()
772
773     try:
774         r = main(show_help, erase_only, no_protect, spi_flash_check,
775                  reset_after_successful_write,
776                  skip_blank_check, status_only, unlock, data)
777         if r == 0:
778             print(Fore.WHITE + Back.BLUE + Style.BRIGHT + "SUCCESS" + Style.RESET_ALL)
779         sys.exit(r)
780     except Exception as e:
781         print(Back.RED + Style.BRIGHT + repr(e) + Style.RESET_ALL)