diff options
Diffstat (limited to 'downloader/downloader.py')
-rwxr-xr-x | downloader/downloader.py | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/downloader/downloader.py b/downloader/downloader.py index 9efb30e..f90eced 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -100,7 +100,7 @@ def calc_csum(str): cs ^= c return cs -def exec_command(dev, cmd, answer): +def exec_command(dev, cmd, param, answer): import struct return_codes = { 0: "OK", 1: "invalid command", 2: "bad checksum", @@ -108,21 +108,22 @@ def exec_command(dev, cmd, answer): 5: "address invalid", 6: "address prohibited", 7: "value out of bounds" } - cstr = bytearray(cmd) + cstr = bytearray(struct.pack('<BB', cmd, 0) + param) cstr.extend(struct.pack("<B", calc_csum(cstr))) + cstr[1] = len(cstr) dev.write(cstr) astr = bytearray() - astr += dev.read(3) - if len(astr) < 3: - print "ERROR: timeout while reading response header" + astr += dev.read(4) + if len(astr) < 4: + print "ERROR: timeout while reading response header (expected %d bytes, got %d)" % (4, len(astr)) sys.exit(4) if astr[0] != cstr[0]: print "ERROR: bootloader returned wrong command code" sys.exit(4) - ret = astr[1] + ret = astr[2] if ret != 0: rstr = "invalid return code" try: @@ -132,12 +133,16 @@ def exec_command(dev, cmd, answer): print "ERROR: bootloader returned %d: %s" % (ret, rstr) sys.exit(4) - answer_len = struct.calcsize(answer) + answer_len = astr[1] - 4 + if answer_len < struct.calcsize(answer): + print "ERROR: short answer %d bytes received: expected %s bytes" % (answer_len, struct.calcsize(answer)) + sys.exit(4) + if answer_len > 0: tmp = bytearray() tmp += dev.read(answer_len) if len(tmp) < answer_len: - print "ERROR: timeout while reading response" + print "ERROR: timeout while reading response (expected %d bytes, got %d)" % (answer_len, len(tmp)) sys.exit(4) astr += tmp @@ -146,12 +151,12 @@ def exec_command(dev, cmd, answer): print "ERROR: checksum error" sys.exit(4) - return struct.unpack_from(answer, astr, 2) + return struct.unpack_from(answer, astr, 3) ### Commands def identify(dev): - data = exec_command(dev, struct.pack('<B', 1), '<BB10sHBHH') + data = exec_command(dev, 1, '', '<BB10sHBBH') id = { 'ver_min': data[0], 'ver_maj': data[1], 'name': data[2], 'devid': data[3], 'fss': data[4], 'mess': data[5], 'supported': data[6] } @@ -166,35 +171,35 @@ def identify(dev): return id def boot(dev): - exec_command(dev, struct.pack('<B', 2), '<') + exec_command(dev, 2, '', '<') def reset(dev, id): - exec_command(dev, struct.pack('<B', 3), '<') + exec_command(dev, 3, '', '<') def read_flash_segment(dev, id, addr): - cmd = struct.pack('<BH', 4, addr) - return exec_command(dev, cmd, '<%dH' % id['fss']) + param = struct.pack('<H', addr) + return exec_command(dev, 4, param, '<%dH' % id['fss']) def write_flash_segment(dev, id, addr, data): - cmd = struct.pack('<BH%dH' % id['fss'], 5, addr, *data) - return exec_command(dev, cmd, '<') + param = struct.pack('<H%dH' % id['fss'], addr, *data) + return exec_command(dev, 5, param, '<') def read_eeprom(dev, id, addr, len): - cmd = struct.pack('<BHH', 6, addr, len) - return exec_command(dev, cmd, '<%dB' % len) + param = struct.pack('<HB', addr, len) + return exec_command(dev, 6, param, '<%dB' % len) def write_eeprom(dev, id, addr, data): - cmd = struct.pack('<BHH%dB' % len(data), 7, addr, len(data), *data) - return exec_command(dev, cmd, '<') + param = struct.pack('<HB%dB' % len(data), addr, len(data), *data) + return exec_command(dev, 7, param, '<') def read_config(dev, id, nr): - cmd = struct.pack('<BB', 8, nr) - data = exec_command(dev, cmd, '<H') + param = struct.pack('<B', nr) + data = exec_command(dev, 8, param, '<H') return data[0] def write_config(dev, id, nr, word): - cmd = struct.pack('<BBH', 9, nr, word) - return exec_command(dev, cmd, '<') + param = struct.pack('<BH', nr, word) + return exec_command(dev, 9, param, '<') ### Main |