2 Module to read intel hex files into binary data blobs.
3 IntelHex files are commonly used to distribute firmware
4 See: http://en.wikipedia.org/wiki/Intel_HEX
6 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
11 Read an verify an intel hex file. Return the data as an list of bytes.
15 f = io.open(filename, "r")
21 raise Exception("Hex file has a line not starting with ':'")
22 recLen = int(line[1:3], 16)
23 addr = int(line[3:7], 16) + extraAddr
24 recType = int(line[7:9], 16)
25 if len(line) != recLen * 2 + 11:
26 raise Exception("Error in hex file: " + line)
28 for i in xrange(0, recLen + 5):
29 checkSum += int(line[i*2+1:i*2+3], 16)
32 raise Exception("Checksum error in hex file: " + line)
34 if recType == 0:#Data record
35 while len(data) < addr + recLen:
37 for i in xrange(0, recLen):
38 data[addr + i] = int(line[i*2+9:i*2+11], 16)
39 elif recType == 1: #End Of File record
41 elif recType == 2: #Extended Segment Address Record
42 extraAddr = int(line[9:13], 16) * 16
44 print(recType, recLen, addr, checkSum, line)