-
Notifications
You must be signed in to change notification settings - Fork 1
/
imxsb-cli.py
executable file
·216 lines (174 loc) · 6.84 KB
/
imxsb-cli.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
#!/usr/bin/env python
# Copyright (c) 2017-2018 Martin Olejar
#
# SPDX-License-Identifier: BSD-3-Clause
# The BSD-3-Clause license for this file can be found in the LICENSE file included with this distribution
# or at https://spdx.org/licenses/BSD-3-Clause.html#licenseText
import sys
import imx
import time
import argparse
# SmartBoot Core module
import core
########################################################################################################################
# Helper class
########################################################################################################################
class ProgressBar(object):
def __init__(self, total=100, nbars=30, prefix='', file=sys.stderr):
self.file = file
self.total = total
self.nbars = nbars
self.prefix = prefix
self.disabled = False
# private
self._last_printed_len = 0
self._start_time = 0
self._started = False
def _print_status(self, s):
self.file.write('\r' + s + ' ' * max(self._last_printed_len - len(s), 0))
self.file.flush()
self._last_printed_len = len(s)
def _format_interval(self, t):
mins, s = divmod(t, 60)
_, m = divmod(mins, 60)
return '%02d:%06.3f' % (int(m), s)
def _format_meter(self, value, elapsed_time):
frac = float(value) / self.total
bar_length = int(frac * self.nbars)
bar = '#' * bar_length + '-' * (self.nbars - bar_length)
return '[%s] %3d%% (%s) ' % (bar, frac * 100, self._format_interval(elapsed_time))
def start(self):
if not self.disabled:
self._print_status(self.prefix + self._format_meter(0, 0))
self._start_time = time.time()
self._started = True
def update(self, value):
if not self.disabled and self._started:
self._print_status(self.prefix + self._format_meter(value, time.time() - self._start_time))
def finish(self, leave=False):
if not self.disabled:
if not leave:
self._print_status('')
sys.stdout.write('\r')
else:
self.update(self.total)
self.file.write('\n')
self._started = False
########################################################################################################################
# main function
########################################################################################################################
def main():
# application error code
error_code = 1
# cli arguments
parser = argparse.ArgumentParser()
parser.add_argument('smx_file', help='path to *.smx file')
parser.add_argument('-i', '--info', dest='print_info', action='store_true',
help='print SMX file info and exit')
parser.add_argument('-s', '--script', dest='index', type=int, default=100,
help='select script by its index')
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true',
help='no progressbar')
parser.add_argument('-v', '--version', action='version', version=core.__version__)
results = parser.parse_args()
try:
# open and load smx file
smx = core.SmxFile(results.smx_file, True)
except Exception as e:
print("\n ERROR: %s" % str(e))
sys.exit(error_code)
if results.print_info:
print("\n Name: %s\n Desc: %s\n Chip: %s\n" % (smx.name, smx.description, smx.platform))
print(' ' + '-' * 50)
num = 0
for script in smx.scripts:
print(" %d) %s (%s)" % (num, script.name, script.description))
num += 1
print(' ' + '-' * 50)
else:
error_flg = False
error_msg = ""
script_index = results.index
device_index = 0
# scan for USB target
devices = imx.sdp.scan_usb(smx.platform)
if not devices:
print("\n No {} board connected !".format(smx.platform))
sys.exit(error_code)
if len(devices) > 1:
i = 0
print('')
for dev in devices:
print(" %d) %s" % (i, dev.usbd.info()))
i += 1
print("\n Select target device: ", end='', flush=True)
c = input()
print()
device_index = int(c, 10)
print("\n DEVICE: %s\n" % devices[device_index].usbd.info())
flasher = devices[device_index]
if len(smx.scripts) == 1:
script_index = 0
# select boot script
if script_index > len(smx.scripts):
num = 0
for script in smx.scripts:
print(" %d) %s (%s)" % (num, script.name, script.description))
num += 1
print("\n Select boot script: ", end='', flush=True)
c = input()
script_index = int(c, 10)
print()
bar = ProgressBar(nbars=40, prefix=' ', file=sys.stdout)
def progress_handler(value):
bar.update(value)
return True
try:
# connect target
if results.quiet:
flasher.open()
bar.disabled = True
else:
flasher.open(progress_handler)
flasher.pg_resolution = 20
# load script
script = smx.get_script(script_index)
print(' ' + '-' * 50)
print(" START: %s (%s)" % (script.name, script.description))
print(' ' + '-' * 50)
# execute script
num = 1
for cmd in script:
# print command info
print(" %d/%d) %s" % (num, len(script), cmd['description']))
if cmd['name'] == 'wreg':
flasher.write(cmd['address'], cmd['value'], cmd['bytes'])
elif cmd['name'] == 'wdcd':
bar.start()
flasher.write_dcd(cmd['address'], cmd['data'])
bar.finish()
elif cmd['name'] == 'wimg':
bar.start()
flasher.write_file(cmd['address'], cmd['data'])
bar.finish()
elif cmd['name'] == 'sdcd':
flasher.skip_dcd()
elif cmd['name'] == 'jrun':
flasher.jump_and_run(cmd['address'])
else:
raise Exception("Command: {} not supported".format(cmd['name']))
num += 1
except Exception as e:
error_msg = str(e) if str(e) else "Unknown Error !"
error_flg = True
finally:
flasher.close()
if error_flg:
print()
else:
print(' ' + '-' * 50)
if error_flg:
print(" ERROR: %s" % error_msg)
sys.exit(error_code)
if __name__ == '__main__':
main()