-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlibusb_eeprom.py
82 lines (62 loc) · 1.8 KB
/
libusb_eeprom.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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# vim: set ts=4 sw=4 et sts=4 ai:
import array
import usb.core
import usb.util
VC_EEPROM = 0xB1
READ_EEPROM = 0xC0
WRITE_EEPROM = 0x40
def get_eeprom(dev, addr, amount):
data = array.array('B')
while len(data) < amount:
transfer_size = min(64, amount - len(data))
result = dev.ctrl_transfer(
READ_EEPROM,
VC_EEPROM,
addr + len(data),
0,
transfer_size,
)
assert len(result) == transfer_size, "len(result) %i == %i" % (
len(result), transfer_size)
data += result
return data
def set_eeprom(dev, addr, data):
offset = 0
while offset < len(data):
transfer_size = min(32, len(data) - offset)
result = dev.ctrl_transfer(
WRITE_EEPROM,
VC_EEPROM,
addr + offset,
0,
data[offset:offset + transfer_size],
)
assert result == transfer_size, "result %i == %i" % (
result, transfer_size)
offset += transfer_size
def get_dev():
# find our device
dev = usb.core.find(idVendor=0x2A19, idProduct=0x5441)
# was it found?
if dev is None:
raise ValueError('Device not found')
dev.set_configuration()
return dev
if __name__ == "__main__":
import sys
import argparse
import time
dev = get_dev()
current_eeprom_data = get_eeprom(dev, 0, 256)
old_eeprom_data = bytes(current_eeprom_data)
print(repr(old_eeprom_data))
if len(sys.argv) > 1:
new_eeprom_data = file(sys.argv[1], "rb").read()
print(repr(old_eeprom_data))
print(repr(new_eeprom_data))
if old_eeprom_data != new_eeprom_data:
set_eeprom(dev, 0, new_eeprom_data)
else:
print(repr(s))
s.check()