-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed-update.py
executable file
·212 lines (171 loc) · 4.74 KB
/
feed-update.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
#!/usr/bin/env python
import sys
import getopt
import struct
import re
import serial
import zlib
import base64
SYSTEM = 'linux'
com_port = None
if (SYSTEM == 'windows'):
ports = ['COM6', 'COM5', 'COM4', 'COM3', 'COM2', 'COM1']
if (SYSTEM == 'linux'):
ports = [sys.argv[1]]
if (SYSTEM == 'windows'):
filename = sys.argv[1]
if (SYSTEM == 'linux'):
filename = sys.argv[2]
fd = None
BLOCK_SIZE = 512
def open_com_port():
for port in ports:
try:
print ('open {0:s} ... '.format(port)),
com_port = serial.Serial(port, 19200, timeout=0.1)
print ('O.K.')
return (com_port)
except Exception:
print ('fail')
return (None)
def com_port_recv_char():
return (com_port.read(1))
def com_port_recv_line(*arg):
line = ""
timeout_N = 100
timeout_count = 0
if (len(arg) == 1):
timeout_N = arg[0]
while True:
recv_char = com_port_recv_char()
if (len(recv_char) == 0):
timeout_count = timeout_count + 1
if (not(timeout_count < timeout_N)):
line = ""
break
continue
if (ord(recv_char) == 0x0d):
break
if ((ord(recv_char) >= 0x20) and (ord(recv_char) <= 0x7e)):
line = line + recv_char
timeout_count = 0
return (line)
def update_failure():
raw_input("feed control board update FAILED - press any key to exit")
sys.exit(1)
def update_success():
raw_input("feed control board update successful - press any key to exit")
sys.exit(0)
def main():
global com_port
global fd
com_port = open_com_port()
if (not(com_port)):
update_failure()
print("got port")
try:
fd = open(filename, 'rb')
except Exception, e:
print ('unable to open disk image ' + filename)
update_failure()
print('got file')
com_port.write(chr(0x0d))
com_port_recv_line()
com_port.write(chr(0x0d))
com_port_recv_line()
print('writing')
cmnd = 'bootloader'
com_port.write(cmnd)
com_port.write(chr(0x0d))
print (cmnd)
rspns = com_port_recv_line()
print(rspns)
print ("done")
if not ((re.search(r'Bootloader', rspns)) ):
print('resetting')
com_port_recv_line()
cmnd = 'reset'
com_port.write(cmnd)
com_port.write(chr(0x0d))
print (cmnd)
rspns = com_port_recv_line()
print (rspns)
if (not (re.search(r'Bootloader', rspns) )):
update_failure()
print("writing disk")
com_port_recv_line()
if (not(writedisk())):
update_failure()
print("erase flash")
com_port_recv_line()
cmnd = 'eraseflash'
com_port.write(cmnd)
com_port.write(chr(0x0d))
print (cmnd)
rspns = com_port_recv_line(70)
print (rspns)
if (not(re.search(r'OK', rspns))):
update_failure()
com_port_recv_line()
cmnd = 'programflash ANTONI~1.HEX'
com_port.write(cmnd)
com_port.write(chr(0x0d))
print (cmnd)
rspnsln1 = com_port_recv_line(70)
print (rspnsln1)
rspnsln2 = com_port_recv_line()
print (rspnsln2)
if (not(re.search(r'records', rspnsln1))):
update_failure()
if (not(re.search(r'OK', rspnsln2))):
update_failure()
com_port_recv_line()
cmnd = 'jumptoapp'
com_port.write(cmnd)
com_port.write(chr(0x0d))
print (cmnd)
rspns = com_port_recv_line(100)
print (rspns)
if (not(re.search(r'Monitor and Control Firmware', rspns))):
update_failure()
update_success()
def format_request(blk, offset):
request = 'writedisk' + ' '
request += str(offset)
request += ' '
request += str(BLOCK_SIZE)
request += ' '
request += '{0:08x}'.format(zlib.adler32(blk) & 0xffffffff)
request += ' '
request += base64.b64encode(blk)
return (request)
def writedisk():
offset = 0
blk = fd.read(BLOCK_SIZE)
if (not(blk)):
return (False)
magic_num = struct.unpack('<510x1H', blk)
if (not(magic_num[0] == 0xaa55)):
print ('MBR not found in ' + filename)
return (False)
retry_count = 0
while (blk):
request = format_request(blk, offset)
print (request[0:54] + ' ... ' + request[-16:] + ' ')
com_port.write(request)
com_port.write(chr(0x0d))
rspns = com_port_recv_line()
print (rspns)
if (re.search(r'ACK', rspns)):
retry_count = 0
else:
com_port_recv_line()
retry_count += 1
if (retry_count == 1) or (retry_count == 2):
continue
if (retry_count > 2):
return (False)
offset += BLOCK_SIZE
blk = fd.read(BLOCK_SIZE)
return (True)
main()