forked from vpelletier/python-dfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_device.py
executable file
·42 lines (38 loc) · 1.34 KB
/
dump_device.py
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
#!/usr/bin/env python
from optparse import OptionParser
import usb1
import dfu
import sys
parser = OptionParser()
parser.add_option('-d', '--device', help='vendor[:product] (in hexadecimal)')
parser.add_option('-s', '--address', help='bus[:devnum] (in hexadecimal)')
def main(args=None):
(options, args) = parser.parse_args(args=args)
vendor = options.device
product = None
if vendor is not None:
if ':' in vendor:
vendor, product = vendor.split(':')
product = int(product, 16)
vendor = int(vendor, 16)
bus = options.address
dev = None
if bus is not None:
if ':' in bus:
bus, dev = address.split(':', 1)
dev = int(dev, 16)
bus = int(bus, 16)
with usb1.USBContext() as context:
for device in context.getDeviceList():
if (vendor is not None and (vendor != device.getVendorID() or \
(product is not None and product != device.getProductID()))) \
or (bus is not None and (bus != device.getBusNumber() or \
(dev is not None and dev != device.getDeviceAddress()))):
continue
else:
print 'No device found.'
sys.exit(1)
dfu_device = dfu.DFU(device.open())
print dfu_device.upload()
if __name__ == '__main__':
main()