-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautobuspirateupgrade.py
236 lines (197 loc) · 7.29 KB
/
autobuspirateupgrade.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# pip install pyserial
# https://github.com/therealdreg/autobuspirateupgrade
# MIT LICENSE
# David Reguera Garcia aka Dreg @therealdreg
#
# bullshit crappies code in the world
COMNAME_ARDUINO = 'COM69'
COMNAME_BUSPIRATE = 'COM96'
import serial
import time
import os
import pprint
import sys
pp = pprint.PrettyPrinter(indent=4)
CONNECT_USB_POWER_CHAR = b"\x30"
DISCONNECT_USB_POWER_CHAR = b"\x31"
CONNECT_PGD_PGC_CHAR = b"\x32"
DISCONNECT_PGD_PGC_CHAR = b"\x33"
def serial_ports():
ports = ['COM%s' % (i + 1) for i in range(256)]
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def yes_bootloader():
try:
bser = serial.Serial(
port=COMNAME_BUSPIRATE,
baudrate=115200,
timeout = 1
)
except Exception as e:
print("error open serial port: ", str(e))
exit()
if bser.isOpen():
try:
bser.flushInput() #flush input buffer, discarding all its contents
bser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
bser.write(b"\x20")
print("please wait.. dont close this program!!")
time.sleep(2)
response = bser.readlines()
print("read data: ")
pp.pprint(response)
bser.write(b"Yes\r\n")
print("please wait.. dont close this program!!")
time.sleep(5)
response = bser.readlines()
print("read data: ")
pp.pprint(response)
except Exception as e1:
print("error communicating...: ", str(e1))
exit()
else:
print("cannot open serial port ")
exit()
def self_test():
try:
bser = serial.Serial(
port=COMNAME_BUSPIRATE,
baudrate=115200,
timeout = 1
)
except Exception as e:
print("error open serial port: ", str(e))
exit()
if bser.isOpen():
try:
bser.flushInput() #flush input buffer, discarding all its contents
bser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
bser.write(b"i\r\n")
bser.write(b"m\r\n")
print("please wait.. dont close this program!!")
time.sleep(2)
response = bser.readlines()
print("read data: ")
pp.pprint(response)
if str(response).find("Community Firmware v7.1") != -1 and str(response).find("Bootloader v4.5") != -1:
print("\n\nOK!! installed correctly new firmware (7.1) + new bootloader (4.5)\n\n")
else:
print("\n\n\Craap incorrect bootloader+firm")
exit()
bser.write(b"~\r\n")
print("please wait.. dont close this program!!")
time.sleep(5)
response = bser.readlines()
print("read data: ")
pp.pprint(response)
input("\nHey ! Check leds and Press Enter to continue...")
bser.write(b"\x20")
print("please wait.. dont close this program!!")
time.sleep(2)
response = bser.readlines()
print("read data: ")
pp.pprint(response)
if str(response).find("Found 0 errors") != -1:
print("\n\nHooorayyyy self_test + new_bootloader + new_firmware works fine!!! bus pirate is ready to use")
exit()
except Exception as e1:
print("error communicating...: ", str(e1))
exit()
else:
print("cannot open serial port ")
exit()
if len(sys.argv) >= 2:
COMNAME_ARDUINO = sys.argv[1]
print("Arduino PORT: ", COMNAME_ARDUINO)
try:
ser = serial.Serial(
port=COMNAME_ARDUINO,
baudrate=9600,
timeout = 1
)
except Exception as e:
print("error open serial port: ", str(e))
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
response = ser.readline()
print("read data: ", str(response))
ser.write(DISCONNECT_USB_POWER_CHAR)
time.sleep(5)
print("Serial ports: ")
old_ports = serial_ports()
pp.pprint(old_ports)
print(" - ")
ser.write(CONNECT_USB_POWER_CHAR)
time.sleep(5)
print("New serial ports: ")
new_ports = serial_ports()
pp.pprint(new_ports)
print(" - ")
new_ports = list(set(new_ports) - set(old_ports))
pp.pprint(new_ports)
COMNAME_BUSPIRATE = new_ports[0]
print("Bus pirate: ", COMNAME_BUSPIRATE)
time.sleep(1)
ser.write(DISCONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(8)
ser.write(CONNECT_PGD_PGC_CHAR)
print("please wait.. dont close this program!!")
time.sleep(2)
ser.write(CONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(10)
os.system("pirate-loader.exe --dev=" + COMNAME_BUSPIRATE + " --hex=BPv3-bootloader-upgrade-v4xtov4.5.hex")
print("please wait.. dont close this program!!")
time.sleep(5)
ser.write(DISCONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(2)
ser.write(DISCONNECT_PGD_PGC_CHAR)
print("please wait.. dont close this program!!")
time.sleep(5)
ser.write(CONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(10)
yes_bootloader()
ser.write(DISCONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(5)
ser.write(CONNECT_PGD_PGC_CHAR)
print("please wait.. dont close this program!!")
time.sleep(2)
ser.write(CONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(10)
os.system("pirate-loader.exe --dev=" + COMNAME_BUSPIRATE + " --hex=busPirate-JTAG_SAFE_1.hex")
print("please wait.. dont close this program!!")
time.sleep(5)
ser.write(DISCONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(2)
ser.write(DISCONNECT_PGD_PGC_CHAR)
print("please wait.. dont close this program!!")
time.sleep(2)
ser.write(CONNECT_USB_POWER_CHAR)
print("please wait.. dont close this program!!")
time.sleep(10)
self_test()
except Exception as e1:
print("error communicating...: ", str(e1))
else:
print("cannot open serial port ")
print("\n\ncraaap dont works, check log")