-
Notifications
You must be signed in to change notification settings - Fork 4
/
openglamo
executable file
·335 lines (243 loc) · 8.63 KB
/
openglamo
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
import argparse
import os.path
from pyftdi import ftdi
import progressbar
import pyparsing as pp
import struct
import sys
from time import sleep
def log(s):
if not args.quiet:
sys.stderr.write(s + '\n')
def die(s):
sys.stderr.write(s + '\n')
sys.exit(1)
def exchange(write, num_read=0):
dev.write_data(
# Assert chip-select
b'\x80\x00\xfb'
# Write bytes - sample on falling
b'\x11' + struct.pack('<H', len(write) - 1) + write +
# Read bytes - sample on rising
(b'' if num_read == 0 else (b'\x20' + struct.pack('<H', num_read - 1))) +
# Deassert chip-select
b'\x80\x08\xfb' +
# Flush
b'\x87'
)
return None if num_read == 0 else dev.read_data(num_read)
def parse_target_file(path):
with open(path, 'r') as f:
return {t[0]:t[1] for t in [l.strip().split(' ', 1) for l in f] if len(t) == 2}
def write(addr, data):
exchange(bytes([
0x00, (addr & 0x0100) >> 1 | (addr & 0xFE) >> 1, (addr & 0xFE00) >> 9,
data & 0x00FF, (data & 0xFF00) >> 8]), 0)
def read(addr):
return struct.unpack('<H', exchange(bytes([
0x01, (addr & 0x0100) >> 1 | (addr & 0xFE) >> 1, (addr & 0xFE00) >> 9]), 2))[0]
def read_mask(addr, mask):
return read(addr) & mask
def write_memory(addr, data):
exchange(b'\x00\x03\x01' + struct.pack('<H', addr >> 16) +
struct.pack('<H', addr & 0xffff) + b'\x80' + data)
def read_memory(addr, length):
exchange(b'\x00\x04\x01' + struct.pack('<H', addr & 0xFFFF))
exchange(b'\x00\x05\x01' + struct.pack('<H', (addr >> 16) & 0xFFFF))
exchange(b'\x00\x06\x01' + struct.pack('<H', length // 2 - 1))
exchange(b'\x00\x07\x01\x07\x80')
return exchange(b'\x01\x00\x80', length)
def pci_write(addr, value):
write(addr, value)
def wait(delay):
log(f'WAIT({delay:d});')
sleep(delay * 0.0001)
class Literal:
def __init__(self, value):
self.value = value
def __repr__(self):
return repr(self.value)
def __call__(self):
return self.value
class ScopeBlock:
def __init__(self, statements):
self.statements = statements
def __repr__(self):
return ''.join(['{\n'] + ['{}\n'.format(repr(s)) for s in self.statements] + ['}'])
def __call__(self):
for statement in self.statements:
statement()
class BinaryExpr:
def __init__(self, arg1, op, arg2):
self.arg1 = arg1
self.op = op
self.arg2 = arg2
def __repr__(self):
return '{} {} {}'.format(self.arg1, self.op, self.arg2)
def __call__(self):
if self.op == '==':
return self.arg1() == self.arg2()
elif self.op == '!=':
return self.arg1() != self.arg2()
elif self.op == '<':
return self.arg1() < self.arg2()
elif self.op == '>':
return self.arg1() > self.arg2()
elif self.op == '<=':
return self.arg1() <= self.arg2()
elif self.op == '>=':
return self.arg1() >= self.arg2()
else:
assert(False)
class Call:
def __init__(self, func, *args):
self.func = func
self.args = args
def __repr__(self):
return '{}({})'.format(self.func, ', '.join(str(a) for a in self.args))
def __call__(self):
args = [a() for a in self.args]
log('{}({});'.format(self.func.upper(), ', '.join('0x{:04x}'.join(a) for a in args)))
return globals()[self.func](*args)
class If:
def __init__(self, condition, true_block, false_block=None):
self.condition = condition
self.true_block = true_block
self.false_block = false_block
def __repr__(self):
return 'if ({}) {}{}'.format(self.condition, self.true_block,
' else {}'.format(self.false_block) if self.false_block else '')
def __call__(self):
if self.condition():
self.true_block()
elif self.false_block:
self.false_block()
def run_script(path):
with open(path, 'r') as f:
script = f.read()
dec_literal = pp.Word(pp.nums).setParseAction(lambda toks: int(toks[0]))
hex_literal = pp.Combine('0x' + pp.Word(pp.hexnums)).setParseAction(lambda toks: int(toks[0], 16))
literal = (dec_literal ^ hex_literal).setParseAction(lambda toks: Literal(*toks))
rvalue = pp.Forward()
call = pp.Group(
pp.Word(pp.alphas + '_') +
pp.Suppress('(') +
pp.delimitedList(rvalue) +
pp.Suppress(')')
).setParseAction(lambda toks: Call(*(toks[0])))
rvalue <<= (call ^ literal)
binary_op = pp.Or('==', '!=') ^ '<' ^ '>' ^ '<=' ^ '>='
binary_expr = (rvalue + binary_op + rvalue).setParseAction(lambda toks: BinaryExpr(*toks))
statement = pp.Forward()
scoped_block = (pp.Group(pp.Suppress('{') + pp.ZeroOrMore(statement) + pp.Suppress('}')) ^ statement).setParseAction(
lambda toks: ScopeBlock(toks[0]))
expression = pp.Forward()
expression <<= binary_expr ^ call ^ rvalue
if_statement = pp.Forward()
if_statement <<= (
pp.Suppress('if') + pp.Suppress('(') + expression + pp.Suppress(')') +
scoped_block +
pp.Optional(pp.Suppress('else') + (if_statement ^ scoped_block))).setParseAction(lambda toks: If(*toks))
statement <<= (call + pp.Suppress(';')) ^ if_statement
parser = pp.ZeroOrMore(statement)
parser.ignore('//' + pp.SkipTo(pp.LineEnd()))
parser.ignore('#' + pp.SkipTo(pp.LineEnd()))
script = ScopeBlock(parser.parseString(script))
script()
#
# Parse arguments
#
parser = argparse.ArgumentParser(
description='Developer tool for communication with ITE processors over SPI')
parser.add_argument('--device', '-d', default='ftdi://ftdi:2232h/1', help='PyFTDI device URL')
parser.add_argument('--quiet', '-q', action='store_true', help='Be quiet (no status messages)')
parser.add_argument('--target', '-t', type=str, help='Target device definition', required=True)
parser.add_argument('--init', '-i', action='store_true', help='Run initial script')
parser.add_argument('--load', '-l', type=str, help='Load image file into RAM')
parser.add_argument('--save', '-S', type=str, help='Read RAM contents into file')
parser.add_argument('--print-reg', '-r', action='store_true', help='Print register value')
parser.add_argument('--write-reg', '-R', type=str, help='Write register value')
parser.add_argument('--print-ram', '-m', action='store_true', help='Print RAM value')
parser.add_argument('--write-ram', '-M', type=str, help='Write RAM value')
parser.add_argument('--addr', '-a', default='0', help='Address')
parser.add_argument('--exec', '-e', action='append', default=[], help='Execute script file')
args = parser.parse_args()
quiet = args.quiet
addr = int(args.addr, 16) if args.addr else None
#
# Parse the target file
#
target = parse_target_file(args.target)
if not quiet:
print(
f'target info <{target["info"]}>\n'
f'target mcu <{target["mcu"]}> family <{target["family"]}>')
ram_size = int(target.get('ram_size', '0x0'), 16)
#
# Connect to device
#
dev = ftdi.Ftdi()
dev.open_mpsse_from_url(args.device, 0, 0, 2.0e6)
# Read device ID
dev.write_data(b'\x80\x00\xFB')
sleep(0.030)
device_id = struct.unpack('<H', exchange(b'\x01\x01\x00', 2))[0]
if device_id == 0 or id == 0xFFFF:
die('Failed to read device ID')
rev = struct.unpack('<H', exchange(b'\x01\x02\x00', 2))[0]
print(f'GLAMO device id <0x{device_id:04x}> revision id <0x{rev:04x}>')
#
# Initialise device
#
if args.init:
init_script_path = target['init_script']
init_script_path = (init_script_path if os.path.isabs(init_script_path) else
os.path.join(os.path.dirname(args.target), init_script_path))
run_script(init_script_path)
#
# Execute script files
#
for script in args.exec:
run_script(script)
#
# Load memory
#
if args.load:
write_block_size = 32768
with open(args.load, 'rb') as f:
data = f.read()
with progressbar.ProgressBar(max_value=len(data)) as pb:
for offset in range(0, len(data), write_block_size):
pb.update(offset)
write_size = min(ram_size - offset, write_block_size)
write_memory(addr + offset, data[offset:offset+write_size])
#
# Read memory
#
elif args.save:
read_block_size = 32768
with open(args.save, 'wb') as f:
with progressbar.ProgressBar(max_value=ram_size) as pb:
for offset in range(0, ram_size, read_block_size):
pb.update(offset)
read_size = min(ram_size - offset, read_block_size)
f.write(read_memory(addr + offset, read_size))
#
# Read/write register
#
elif args.write_reg:
value = int(args.write_reg, 16)
write(addr, value)
log('reg <0x{:X}> value <0x{:X}> written'.format(addr, value))
elif args.print_reg:
print('0x{:04X}: {:04X}'.format(addr, read_reg(addr)))
#
# Read/write memory
#
elif args.write_ram:
value = int(args.write_ram, 16)
write_memory(addr, struct.pack('<H', value))
log('ram <0x{:X}> value <0x{:X}> written'.format(addr, value))
elif args.print_ram:
print('0x{:08X}: {:04X}'.format(addr, struct.unpack('<H', read_memory(addr, 2))[0]))