forked from gligli/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XFlash.py
executable file
·74 lines (53 loc) · 1.62 KB
/
XFlash.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
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
#!/usr/bin/env python
import usb
import sys
import struct
import pprint
pp = pprint.PrettyPrinter()
class XFlash:
def __init__(self, usbdev):
self.devhandle = usbdev.open()
self.devhandle.setConfiguration(1)
self.devhandle.claimInterface(0)
self.ep_out = 0x05
self.ep_in = 0x82
def __del__(self):
try:
self.devhandle.releaseInterface(0)
del self.devhandle
except:
pass
def cmd(self, cmd, argA=0, argB=0):
buffer = struct.pack("LL", argA, argB)
self.devhandle.controlMsg(requestType = usb.TYPE_VENDOR,
request = cmd,
value = 0x00,
index = 0x00,
buffer = buffer)
def flashInit(self):
self.cmd(0x03)
buffer = self.devhandle.bulkRead(self.ep_in, 4, 1000)
buffer = ''.join([chr(x) for x in buffer])
return struct.unpack("L", buffer)[0]
def flashDeInit(self):
self.cmd(0x04)
def flashStatus(self):
self.cmd(0x05)
buffer = self.devhandle.bulkRead(self.ep_in, 4, 1000)
buffer = ''.join([chr(x) for x in buffer])
return struct.unpack("L", buffer)[0]
usbdev = None
for bus in usb.busses():
for dev in bus.devices:
if dev.idVendor == 0xFFFF and dev.idProduct == 4:
usbdev = dev
if not usbdev:
print "XFlash USB hardware not found."
sys.exit(1)
print "Using XFlash @ [%s]" % (usbdev.filename)
xf = XFlash(usbdev)
try:
print "FlashConfig: 0x%08x" % (xf.flashInit())
print "FlashStatus: 0x%08x" % (xf.flashStatus())
except:
xf.flashDeInit()