2013-05-02 18:32:24 +00:00
|
|
|
#!/usr/bin/env python
|
2011-02-04 07:25:12 +00:00
|
|
|
#
|
|
|
|
# stm32_mem.py: STM32 memory access using USB DFU class
|
2011-02-08 23:06:50 +00:00
|
|
|
# Copyright (C) 2011 Black Sphere Technologies
|
2017-09-27 23:55:59 +00:00
|
|
|
# Copyright (C) 2017 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
|
2011-02-04 07:25:12 +00:00
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
from time import sleep
|
|
|
|
import struct
|
|
|
|
from sys import stdout, argv
|
|
|
|
|
2013-01-17 10:00:35 +00:00
|
|
|
import argparse
|
2011-02-04 07:25:12 +00:00
|
|
|
import usb
|
|
|
|
import dfu
|
|
|
|
|
|
|
|
CMD_GETCOMMANDS = 0x00
|
|
|
|
CMD_SETADDRESSPOINTER = 0x21
|
|
|
|
CMD_ERASE = 0x41
|
|
|
|
|
|
|
|
def stm32_erase(dev, addr):
|
|
|
|
erase_cmd = struct.pack("<BL", CMD_ERASE, addr)
|
|
|
|
dev.download(0, erase_cmd)
|
|
|
|
while True:
|
|
|
|
status = dev.get_status()
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_BUSY:
|
|
|
|
sleep(status.bwPollTimeout / 1000.0)
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
|
|
|
|
break
|
|
|
|
|
2017-06-25 13:18:08 +00:00
|
|
|
def stm32_set_address(dev, addr):
|
|
|
|
set_address_cmd = struct.pack("<BL", CMD_SETADDRESSPOINTER, addr)
|
|
|
|
dev.download(0, set_address_cmd)
|
|
|
|
while True:
|
|
|
|
status = dev.get_status()
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_BUSY:
|
|
|
|
sleep(status.bwPollTimeout / 1000.0)
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
|
|
|
|
break
|
|
|
|
|
2011-02-04 07:25:12 +00:00
|
|
|
def stm32_write(dev, data):
|
|
|
|
dev.download(2, data)
|
|
|
|
while True:
|
|
|
|
status = dev.get_status()
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_BUSY:
|
|
|
|
sleep(status.bwPollTimeout / 1000.0)
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
|
|
|
|
break
|
2017-09-26 16:55:26 +00:00
|
|
|
|
|
|
|
def stm32_read(dev):
|
|
|
|
data = dev.upload(2, 1024)
|
|
|
|
while True:
|
|
|
|
status = dev.get_status()
|
|
|
|
if status.bState == dfu.STATE_DFU_DOWNLOAD_BUSY:
|
|
|
|
sleep(status.bwPollTimeout / 1000.0)
|
|
|
|
if status.bState == dfu.STATE_DFU_UPLOAD_IDLE:
|
|
|
|
break
|
|
|
|
return data
|
|
|
|
|
2011-02-04 07:25:12 +00:00
|
|
|
def stm32_manifest(dev):
|
|
|
|
dev.download(0, "")
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
status = dev.get_status()
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
sleep(status.bwPollTimeout / 1000.0)
|
|
|
|
if status.bState == dfu.STATE_DFU_MANIFEST:
|
|
|
|
break
|
2017-10-04 11:50:43 +00:00
|
|
|
def stm32_scan(args, test):
|
2011-02-04 07:25:12 +00:00
|
|
|
devs = dfu.finddevs()
|
2017-09-26 17:40:27 +00:00
|
|
|
bmp = 0
|
2011-02-04 07:25:12 +00:00
|
|
|
if not devs:
|
2017-10-04 11:50:43 +00:00
|
|
|
if test == True:
|
|
|
|
return
|
2017-09-26 17:40:27 +00:00
|
|
|
print "No DFU devices found!"
|
2011-02-04 07:25:12 +00:00
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
for dev in devs:
|
|
|
|
dfudev = dfu.dfu_device(*dev)
|
|
|
|
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
2017-09-26 17:40:27 +00:00
|
|
|
if man == "Black Sphere Technologies": bmp = bmp + 1
|
|
|
|
if bmp == 0 :
|
2017-10-04 11:50:43 +00:00
|
|
|
if test == True:
|
|
|
|
return
|
2017-09-26 17:40:27 +00:00
|
|
|
print "No compatible device found\n"
|
|
|
|
exit(-1)
|
|
|
|
if bmp > 1 and not args.serial_target :
|
2017-10-04 11:50:43 +00:00
|
|
|
if test == True:
|
|
|
|
return
|
2017-09-26 17:40:27 +00:00
|
|
|
print "Found multiple devices:\n"
|
|
|
|
for dev in devs:
|
|
|
|
dfudev = dfu.dfu_device(*dev)
|
|
|
|
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
|
|
|
product = dfudev.handle.getString(dfudev.dev.iProduct, 96)
|
|
|
|
serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30)
|
|
|
|
print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)
|
|
|
|
print "Manufacturer:\t %s" % man
|
|
|
|
print "Product:\t %s" % product
|
|
|
|
print "Serial:\t\t %s\n" % serial_no
|
|
|
|
print "Select device with serial number!"
|
|
|
|
exit (-1)
|
|
|
|
|
|
|
|
for dev in devs:
|
|
|
|
dfudev = dfu.dfu_device(*dev)
|
|
|
|
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
|
|
|
product = dfudev.handle.getString(dfudev.dev.iProduct, 96)
|
2013-01-17 10:00:35 +00:00
|
|
|
serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30)
|
|
|
|
if args.serial_target:
|
2017-09-26 17:40:27 +00:00
|
|
|
if man == "Black Sphere Technologies" and serial_no == args.serial_target: break
|
|
|
|
else:
|
2013-01-17 10:00:35 +00:00
|
|
|
if man == "Black Sphere Technologies": break
|
2017-09-26 17:40:27 +00:00
|
|
|
print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)
|
|
|
|
print "Manufacturer:\t %s" % man
|
|
|
|
print "Product:\t %s" % product
|
2017-09-27 19:38:54 +00:00
|
|
|
print "Serial:\t\t %s" % serial_no
|
2017-09-26 17:40:27 +00:00
|
|
|
if args.serial_target and serial_no != args.serial_target:
|
|
|
|
print "Serial number doesn't match!\n"
|
|
|
|
exit(-2)
|
2017-09-27 19:38:54 +00:00
|
|
|
return dfudev
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print
|
|
|
|
print "USB Device Firmware Upgrade - Host Utility -- version 1.2"
|
|
|
|
print "Copyright (C) 2011 Black Sphere Technologies"
|
|
|
|
print "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
|
|
|
|
print
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("progfile", help="Binary file to program")
|
|
|
|
parser.add_argument("-s", "--serial_target", help="Match Serial Number")
|
2017-10-02 13:50:40 +00:00
|
|
|
parser.add_argument("-a", "--address", help="Start address for firmware")
|
2017-09-28 11:01:59 +00:00
|
|
|
parser.add_argument("-m", "--manifest", help="Start application, if in DFU mode", action='store_true')
|
2017-09-27 19:38:54 +00:00
|
|
|
args = parser.parse_args()
|
2017-10-04 11:50:43 +00:00
|
|
|
dfudev = stm32_scan(args, False)
|
2011-02-04 07:25:12 +00:00
|
|
|
try:
|
|
|
|
state = dfudev.get_state()
|
|
|
|
except:
|
2017-09-28 11:01:59 +00:00
|
|
|
if args.manifest : exit(0)
|
2011-02-04 07:25:12 +00:00
|
|
|
print "Failed to read device state! Assuming APP_IDLE"
|
|
|
|
state = dfu.STATE_APP_IDLE
|
|
|
|
if state == dfu.STATE_APP_IDLE:
|
|
|
|
dfudev.detach()
|
2017-09-27 19:38:54 +00:00
|
|
|
dfudev.release()
|
|
|
|
print "Invoking DFU Device"
|
|
|
|
timeout = 0
|
|
|
|
while True :
|
|
|
|
sleep(0.5)
|
|
|
|
timeout = timeout + 0.5
|
2017-10-04 11:50:43 +00:00
|
|
|
dfudev = stm32_scan(args, True)
|
2017-09-27 19:38:54 +00:00
|
|
|
if dfudev: break
|
|
|
|
if timeout > 5 :
|
|
|
|
print "Error: DFU device did not appear"
|
|
|
|
exit(-1)
|
2017-09-28 11:01:59 +00:00
|
|
|
if args.manifest :
|
|
|
|
stm32_manifest(dfudev)
|
|
|
|
print "Invoking Application Device"
|
|
|
|
exit(0)
|
2011-02-04 07:25:12 +00:00
|
|
|
dfudev.make_idle()
|
2017-09-26 16:55:26 +00:00
|
|
|
file = open(args.progfile, "rb")
|
|
|
|
bin = file.read()
|
2011-02-04 07:25:12 +00:00
|
|
|
|
2017-09-27 23:55:59 +00:00
|
|
|
product = dfudev.handle.getString(dfudev.dev.iProduct, 64)
|
2017-10-02 13:50:40 +00:00
|
|
|
if args.address :
|
|
|
|
start = int(args.address, 0)
|
|
|
|
else :
|
|
|
|
if "F4" in product:
|
|
|
|
start = 0x8004000
|
|
|
|
else:
|
|
|
|
start = 0x8002000
|
2017-09-26 16:55:26 +00:00
|
|
|
addr = start
|
2011-02-04 07:25:12 +00:00
|
|
|
while bin:
|
|
|
|
print ("Programming memory at 0x%08X\r" % addr),
|
|
|
|
stdout.flush()
|
2013-01-17 13:04:43 +00:00
|
|
|
try:
|
2017-09-27 23:55:59 +00:00
|
|
|
# STM DFU bootloader erases always.
|
|
|
|
# BPM Bootloader only erases once per sector
|
|
|
|
# To support the STM DFU bootloader, the interface descriptor must
|
|
|
|
# get evaluated and erase called only once per sector!
|
2013-01-17 13:04:43 +00:00
|
|
|
stm32_erase(dfudev, addr)
|
2017-09-27 19:38:54 +00:00
|
|
|
except:
|
2013-01-17 13:04:43 +00:00
|
|
|
print "\nErase Timed out\n"
|
|
|
|
break
|
2017-06-25 13:18:08 +00:00
|
|
|
try:
|
|
|
|
stm32_set_address(dfudev, addr)
|
|
|
|
except:
|
|
|
|
print "\nSet Address Timed out\n"
|
|
|
|
break
|
2011-02-04 07:25:12 +00:00
|
|
|
stm32_write(dfudev, bin[:1024])
|
|
|
|
bin = bin[1024:]
|
|
|
|
addr += 1024
|
2017-09-26 16:55:26 +00:00
|
|
|
file.seek(0)
|
|
|
|
bin = file.read()
|
|
|
|
len = len(bin)
|
|
|
|
addr = start
|
|
|
|
print
|
|
|
|
while bin:
|
|
|
|
try:
|
|
|
|
stm32_set_address(dfudev, addr)
|
|
|
|
data = stm32_read(dfudev)
|
|
|
|
except:
|
|
|
|
# Abort silent if bootloader does not support upload
|
|
|
|
break
|
|
|
|
print ("Verifying memory at 0x%08X\r" % addr),
|
|
|
|
stdout.flush()
|
|
|
|
if len > 1024 :
|
|
|
|
size = 1024
|
|
|
|
else :
|
|
|
|
size = len
|
|
|
|
if bin[:size] != bytearray(data[:size]) :
|
|
|
|
print ("\nMitmatch in block at 0x%08X" % addr)
|
|
|
|
break;
|
|
|
|
bin = bin[1024:]
|
|
|
|
addr += 1024
|
|
|
|
len -= 1024
|
|
|
|
if len <= 0 :
|
|
|
|
print "\nVerified!"
|
2011-02-04 07:25:12 +00:00
|
|
|
stm32_manifest(dfudev)
|
|
|
|
|
2017-09-26 16:55:26 +00:00
|
|
|
print "All operations complete!\n"
|