This repository has been archived by the owner on Mar 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathihex.py
196 lines (145 loc) · 4.72 KB
/
ihex.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
import struct
class IHex(object):
@classmethod
def read(cls, lines):
ihex = cls()
segbase = 0
for line in lines:
line = line.strip()
if not line: continue
t, a, d = ihex.parse_line(line)
if t == 0x00:
ihex.insert_data(segbase + a, d)
elif t == 0x01:
break # Should we check for garbage after this?
elif t == 0x02:
ihex.set_mode(16)
segbase = struct.unpack(">H", d[0:2])[0] << 4
elif t == 0x03:
ihex.set_mode(16)
cs, ip = struct.unpack(">2H", d[0:2])
ihex.set_start((cs, ip))
elif t == 0x04:
ihex.set_mode(32)
segbase = struct.unpack(">H", d[0:2])[0] << 16
elif t == 0x05:
ihex.set_mode(32)
ihex.set_start(struct.unpack(">I", d[0:4])[0])
else:
raise ValueError("Invalid type byte")
return ihex
@classmethod
def read_file(cls, fname):
f = open(fname, "r")
ihex = cls.read(f)
f.close()
return ihex
def __init__(self):
self.areas = {}
self.start = None
self.mode = 8
self.row_bytes = 16
def set_row_bytes(self, row_bytes):
"""Set output hex file row width (bytes represented per row)."""
if row_bytes < 1 or row_bytes > 0xff:
raise ValueError("Value out of range: (%r)" % row_bytes)
self.row_bytes = row_bytes
def extract_data(self, start=None, end=None):
if start is None:
start = 0
if end is None:
end = 0
result = ""
for addr, data in self.areas.iteritems():
if addr >= start:
end = max(end, addr + len(data))
result = result[:start] + data[start-addr:end-addr] + result[end:]
return result
else:
result = ""
for addr, data in self.areas.iteritems():
if addr >= start and addr < end:
result = result[:start] + data[start-addr:end-addr] + result[end:]
return result
def set_start(self, start=None):
self.start = start
def set_mode(self, mode):
self.mode = mode
def get_area(self, addr):
for start, data in self.areas.iteritems():
end = start + len(data)
if addr >= start and addr <= end:
return start
return None
def insert_data(self, istart, idata):
iend = istart + len(idata)
area = self.get_area(istart)
if area is None:
self.areas[istart] = idata
else:
data = self.areas[area]
# istart - iend + len(idata) + len(data)
self.areas[area] = data[:istart-area] + idata + data[iend-area:]
def calc_checksum(self, bytes):
total = sum(map(ord, bytes))
return (-total) & 0xFF
def parse_line(self, rawline):
if rawline[0] != ":":
raise ValueError("Invalid line start character (%r)" % rawline[0])
try:
line = rawline[1:].decode("hex")
except:
raise ValueError("Invalid hex data")
length, addr, type = struct.unpack(">BHB", line[:4])
dataend = length + 4
data = line[4:dataend]
#~ print line[dataend:dataend + 2], repr(line)
cs1 = ord(line[dataend])
cs2 = self.calc_checksum(line[:dataend])
if cs1 != cs2:
raise ValueError("Checksums do not match")
return (type, addr, data)
def make_line(self, type, addr, data):
line = struct.pack(">BHB", len(data), addr, type)
line += data
line += chr(self.calc_checksum(line))
#~ return ":" + line.encode("hex")
return ":" + line.encode("hex").upper() + "\r\n"
def write(self):
output = ""
for start, data in sorted(self.areas.iteritems()):
i = 0
segbase = 0
while i < len(data):
chunk = data[i:i + self.row_bytes]
addr = start
newsegbase = segbase
if self.mode == 8:
addr = addr & 0xFFFF
elif self.mode == 16:
t = addr & 0xFFFF
newsegbase = (addr - t) >> 4
addr = t
if newsegbase != segbase:
output += self.make_line(0x02, 0, struct.pack(">H", newsegbase))
segbase = newsegbase
elif self.mode == 32:
newsegbase = addr >> 16
addr = addr & 0xFFFF
if newsegbase != segbase:
output += self.make_line(0x04, 0, struct.pack(">H", newsegbase))
segbase = newsegbase
output += self.make_line(0x00, addr, chunk)
i += self.row_bytes
start += self.row_bytes
if self.start is not None:
if self.mode == 16:
output += self.make_line(0x03, 0, struct.pack(">2H", self.start[0], self.start[1]))
elif self.mode == 32:
output += self.make_line(0x05, 0, struct.pack(">I", self.start))
output += self.make_line(0x01, 0, "")
return output
def write_file(self, fname):
f = open(fname, "w")
f.write(self.write())
f.close()