-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-ihex.py
executable file
·75 lines (65 loc) · 1.54 KB
/
merge-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
#!/usr/bin/env python
#
# flatten hex files
import sys
verbose = False
assert len(sys.argv) == 2
f = open(sys.argv[1], 'r')
assert f is not None
class range:
def __init__(self, addr, bs, nb):
self.addr = addr
self.bs = bs
self.nb = nb
addrs = dict()
# hexfile line format
# :<#><address><op><data><xsum>
# ops
# - 00 - data
# - 01 - eof (last line)
# - 02 - real-mode segment value
# - 03 - start segment cs:ip
# - 04 - addr [31:16] for following data ops
# - 05 - addr [31:0]
for l in f:
if l[0] != ':':
print 'invalid:' + l
continue
nb = int(l[1:3], 16)
addr = int(l[3:7], 16)
op = int(l[7:9], 16)
bs = l[9:9+2*nb]
xsum = int(l[9+nb:9+2*nb+2], 16)
if op == 0x00:
addrs[addr] = range(addr, bs, nb)
elif op == 0x01:
assert nb == 0
assert addr == 0
assert len(f.read()) == 0
else:
print 'unhandled op:%02x' % op
continue
f.close()
print 'initial pass:'
for (k, v) in addrs.items():
print 'addr:%04x:%u' % (k, v.nb)
if verbose:
print v.bs
# one more pass to fix up holes from out of order blocks
ks = addrs.keys()
ks.sort()
for addr in ks:
for (base, value) in addrs.items():
if base > addr:
break
merged = value.addr + value.nb == addr
if merged:
old = addrs.pop(addr)
value.bs += old.bs
value.nb += old.nb
break
print 'merged pass:'
for (k, v) in addrs.items():
print 'addr:%04x:%u' % (k, v.nb)
if verbose:
print v.bs