-
Notifications
You must be signed in to change notification settings - Fork 16
/
tool.py
169 lines (129 loc) · 4.75 KB
/
tool.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
import argparse, subprocess, sys, os
import serial, time, binascii, os
parser = argparse.ArgumentParser(description='These are some simple scripts to interface a oneWire eeprom to a computer using a arduino Mirror..')
parser.add_argument("-s", "--save", help="Save the eeprom to disk. (Default action)", action="store_true")
parser.add_argument("-l", "--load", help="Restore the eeprom to the oldest available image.", action="store_true")
parser.add_argument("-t", "--test", help="Test eeprom (and arduino/connections etc).", action="store_true")
parser.add_argument("-c", "--clear", help="Clear the flash write all zeros.")
def readROM():
sp.write("r")
return sp.read(9)
def readFlash():
sp.flushInput()
sp.write("f")
return sp.read(512)
def pollForChip():
sp.write("x")
return sp.read(1) == "p"
def waitForChip():
while True:
print "Polling for chip. . ."
if pollForChip(): break
time.sleep(0.5)
print "\tfound!"
# Flush the serial port!
sp.flushInput()
def echo():
print "ping. . .",
sp.write("e")
if sp.read() != "e":
print "No echo :("
else:
print "pong! (success)"
def getPathForRom(rom):
return os.path.join("flash-dumps", binascii.hexlify(rom))
def dump2433():
waitForChip()
# Read the ROM. echo its hex
rom = readROM()
print(binascii.hexlify(rom) + " (" + str(len(rom)) + ")")
# Read it 5x and make sure they all match
for reread in range(5): assert rom == readROM(), "Mismatched ROM read. Aborting"
# Read the flash
flash = readFlash()
print("Flash Read: %i bytes" % len(flash))
# Read it 5x and make sure they all match
for reread in range(5): assert flash == readFlash(), "Mismatched flash read. Aborting"
# Make a path to store the dumped data
fPath = getPathForRom(rom)
if not os.path.isdir(fPath): os.makedirs(fPath)
# Mark them by the time, so we can reload the oldest one later
fullNamePath = os.path.join(fPath, str(time.time()) + ".bin")
open(fullNamePath, "wb").write(flash)
# Echo our success
print "Wrote", fullNamePath
def write2433(flash):
waitForChip()
# Retrieve the rom
currentRom = readROM()
# Make sure it matches
assert currentRom == readROM(), "ROM's do not match!"
#stupid check the flash size to 512
assert len(flash) == 512, "ROM size is not 512 ({0}".format(len(flash))
# Transmit the write command + the new flash
sp.write("w")
sp.write(flash)
result = sp.read(3)
print "Write Result:", repr(result)
if result == "t":
print("Success")
else:
print("Failure!")
def writeOldestToChip():
waitForChip()
currentRom = readROM()
print("Detected Chip with ROM: " + binascii.hexlify(currentRom))
romDIR = getPathForRom(currentRom)
flashes = [f for f in os.listdir(romDIR) if not f.startswith(".")]
flashes.sort()
flashPath = os.path.join(romDIR, flashes[0])
print("Attempting to flash with image from " + flashPath)
oldestFlashData = open(flashPath).read()
write2433(oldestFlashData)
print("Verifying write")
reread = readFlash()
if reread != oldestFlashData:
print "Write failure! Aborting."
print "First wrong offset:"
print type(oldestFlashData), len(oldestFlashData)
print type(reread), len(reread)
for n, (d1, d2) in enumerate(zip(oldestFlashData, reread)):
if d1 != d1:
print "Broken at", n, binascii.hexlify(d1), binascii.hexlify(d2)
else:
print "Write successful."
def clearFlash():
waitForChip()
currentRom = readROM()
print("Detected Chip with ROM: " + binascii.hexlify(currentRom))
write2433("\0" * 512)
# Start the serial port up. NB this is *nix specific, so needs changing for windows users.
if "linux" in sys.platform:
possibleSerialPorts = [d for d in os.listdir("/dev") if "ttyACM" in d or "ttyUSB" in d]
elif sys.platform == "darwin":
possibleSerialPorts = [d for d in os.listdir("/dev") if "tty.usbserial" in d or "tty.usbmodem" in d or "cu.wchusbserial" in d or "cu.usbmodem" in d]
else:
possibleSerialPorts = ["COM1", "COM2", "COM3", "COM4"]
spAddy = os.path.join("/dev", possibleSerialPorts[0])
print( "Using serial port: " + spAddy)
sp = serial.Serial(spAddy, baudrate=9600, timeout=3)
sp.flushInput()
args = parser.parse_args()
if args.test:
waitForChip()
print("Doing Echo Test")
echo()
print("Reading ROM ID:",)
print(binascii.hexlify(readROM()))
sys.exit(0)
if args.load:
print("Backing up eeprom")
dump2433()
# Flush the input stream b/c these are independant interations
sp.flushInput()
print("Flashing oldest stored")
writeOldestToChip()
else:
print("Dumping eeprom")
dump2433()