-
Notifications
You must be signed in to change notification settings - Fork 34
/
intelhex.py
executable file
·1288 lines (1107 loc) · 48.1 KB
/
intelhex.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# Copyright (c) 2005-2013, Alexander Belchenko
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain
# the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce
# the above copyright notice, this list of conditions
# and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the author nor the names
# of its contributors may be used to endorse
# or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''Intel HEX file format reader and converter.
@author Alexander Belchenko (alexander dot belchenko at gmail dot com)
@version 1.5
'''
__docformat__ = "javadoc"
from array import array
from binascii import hexlify, unhexlify
from bisect import bisect_right
import os
import sys
from compat import asbytes, asstr
class _DeprecatedParam(object):
pass
_DEPRECATED = _DeprecatedParam()
class IntelHex(object):
''' Intel HEX file reader. '''
def __init__(self, source=None):
''' Constructor. If source specified, object will be initialized
with the contents of source. Otherwise the object will be empty.
@param source source for initialization
(file name of HEX file, file object, addr dict or
other IntelHex object)
'''
# public members
self.padding = 0x0FF
# Start Address
self.start_addr = None
# private members
self._buf = {}
self._offset = 0
if source is not None:
if isinstance(source, basestring) or getattr(source, "read", None):
# load hex file
self.loadhex(source)
elif isinstance(source, dict):
self.fromdict(source)
elif isinstance(source, IntelHex):
self.padding = source.padding
if source.start_addr:
self.start_addr = source.start_addr.copy()
self._buf = source._buf.copy()
else:
raise ValueError("source: bad initializer type")
def _decode_record(self, s, line=0):
'''Decode one record of HEX file.
@param s line with HEX record.
@param line line number (for error messages).
@raise EndOfFile if EOF record encountered.
'''
s = s.rstrip('\r\n')
if not s:
return # empty line
if s[0] == ':':
try:
bin = array('B', unhexlify(asbytes(s[1:])))
except (TypeError, ValueError):
# this might be raised by unhexlify when odd hexascii digits
raise HexRecordError(line=line)
length = len(bin)
if length < 5:
raise HexRecordError(line=line)
else:
raise HexRecordError(line=line)
record_length = bin[0]
if length != (5 + record_length):
raise RecordLengthError(line=line)
addr = bin[1]*256 + bin[2]
record_type = bin[3]
if not (0 <= record_type <= 5):
raise RecordTypeError(line=line)
crc = sum(bin)
crc &= 0x0FF
if crc != 0:
raise RecordChecksumError(line=line)
if record_type == 0:
# data record
addr += self._offset
for i in xrange(4, 4+record_length):
if not self._buf.get(addr, None) is None:
raise AddressOverlapError(address=addr, line=line)
self._buf[addr] = bin[i]
addr += 1 # FIXME: addr should be wrapped
# BUT after 02 record (at 64K boundary)
# and after 04 record (at 4G boundary)
elif record_type == 1:
# end of file record
if record_length != 0:
raise EOFRecordError(line=line)
raise _EndOfFile
elif record_type == 2:
# Extended 8086 Segment Record
if record_length != 2 or addr != 0:
raise ExtendedSegmentAddressRecordError(line=line)
self._offset = (bin[4]*256 + bin[5]) * 16
elif record_type == 4:
# Extended Linear Address Record
if record_length != 2 or addr != 0:
raise ExtendedLinearAddressRecordError(line=line)
self._offset = (bin[4]*256 + bin[5]) * 65536
elif record_type == 3:
# Start Segment Address Record
if record_length != 4 or addr != 0:
raise StartSegmentAddressRecordError(line=line)
if self.start_addr:
raise DuplicateStartAddressRecordError(line=line)
self.start_addr = {'CS': bin[4]*256 + bin[5],
'IP': bin[6]*256 + bin[7],
}
elif record_type == 5:
# Start Linear Address Record
if record_length != 4 or addr != 0:
raise StartLinearAddressRecordError(line=line)
if self.start_addr:
raise DuplicateStartAddressRecordError(line=line)
self.start_addr = {'EIP': (bin[4]*16777216 +
bin[5]*65536 +
bin[6]*256 +
bin[7]),
}
def loadhex(self, fobj):
"""Load hex file into internal buffer. This is not necessary
if object was initialized with source set. This will overwrite
addresses if object was already initialized.
@param fobj file name or file-like object
"""
if getattr(fobj, "read", None) is None:
fobj = open(fobj, "r")
fclose = fobj.close
else:
fclose = None
self._offset = 0
line = 0
try:
decode = self._decode_record
try:
for s in fobj:
line += 1
decode(s, line)
except _EndOfFile:
pass
finally:
if fclose:
fclose()
def loadbin(self, fobj, offset=0):
"""Load bin file into internal buffer. Not needed if source set in
constructor. This will overwrite addresses without warning
if object was already initialized.
@param fobj file name or file-like object
@param offset starting address offset
"""
fread = getattr(fobj, "read", None)
if fread is None:
f = open(fobj, "rb")
fread = f.read
fclose = f.close
else:
fclose = None
try:
self.frombytes(array('B', asbytes(fread())), offset=offset)
finally:
if fclose:
fclose()
def loadfile(self, fobj, format):
"""Load data file into internal buffer. Preferred wrapper over
loadbin or loadhex.
@param fobj file name or file-like object
@param format file format ("hex" or "bin")
"""
if format == "hex":
self.loadhex(fobj)
elif format == "bin":
self.loadbin(fobj)
else:
raise ValueError('format should be either "hex" or "bin";'
' got %r instead' % format)
# alias (to be consistent with method tofile)
fromfile = loadfile
def fromdict(self, dikt):
"""Load data from dictionary. Dictionary should contain int keys
representing addresses. Values should be the data to be stored in
those addresses in unsigned char form (i.e. not strings).
The dictionary may contain the key, ``start_addr``
to indicate the starting address of the data as described in README.
The contents of the dict will be merged with this object and will
overwrite any conflicts. This function is not necessary if the
object was initialized with source specified.
"""
s = dikt.copy()
start_addr = s.get('start_addr')
if start_addr is not None:
del s['start_addr']
for k in s.keys():
if type(k) not in (int, long) or k < 0:
raise ValueError('Source dictionary should have only int keys')
self._buf.update(s)
if start_addr is not None:
self.start_addr = start_addr
def frombytes(self, bytes, offset=0):
"""Load data from array or list of bytes.
Similar to loadbin() method but works directly with iterable bytes.
"""
for b in bytes:
self._buf[offset] = b
offset += 1
def _get_start_end(self, start=None, end=None, size=None):
"""Return default values for start and end if they are None.
If this IntelHex object is empty then it's error to
invoke this method with both start and end as None.
"""
if (start,end) == (None,None) and self._buf == {}:
raise EmptyIntelHexError
if size is not None:
if None not in (start, end):
raise ValueError("tobinarray: you can't use start,end and size"
" arguments in the same time")
if (start, end) == (None, None):
start = self.minaddr()
if start is not None:
end = start + size - 1
else:
start = end - size + 1
if start < 0:
raise ValueError("tobinarray: invalid size (%d) "
"for given end address (%d)" % (size,end))
else:
if start is None:
start = self.minaddr()
if end is None:
end = self.maxaddr()
if start > end:
start, end = end, start
return start, end
def tobinarray(self, start=None, end=None, pad=_DEPRECATED, size=None):
''' Convert this object to binary form as array. If start and end
unspecified, they will be inferred from the data.
@param start start address of output bytes.
@param end end address of output bytes (inclusive).
@param pad [DEPRECATED PARAMETER, please use self.padding instead]
fill empty spaces with this value
(if pad is None then this method uses self.padding).
@param size size of the block, used with start or end parameter.
@return array of unsigned char data.
'''
if not isinstance(pad, _DeprecatedParam):
print "IntelHex.tobinarray: 'pad' parameter is deprecated."
if pad is not None:
print "Please, use IntelHex.padding attribute instead."
else:
print "Please, don't pass it explicitly."
print "Use syntax like this: ih.tobinarray(start=xxx, end=yyy, size=zzz)"
else:
pad = None
return self._tobinarray_really(start, end, pad, size)
def _tobinarray_really(self, start, end, pad, size):
if pad is None:
pad = self.padding
bin = array('B')
if self._buf == {} and None in (start, end):
return bin
if size is not None and size <= 0:
raise ValueError("tobinarray: wrong value for size")
start, end = self._get_start_end(start, end, size)
for i in xrange(start, end+1):
bin.append(self._buf.get(i, pad))
return bin
def tobinstr(self, start=None, end=None, pad=_DEPRECATED, size=None):
''' Convert to binary form and return as a string.
@param start start address of output bytes.
@param end end address of output bytes (inclusive).
@param pad [DEPRECATED PARAMETER, please use self.padding instead]
fill empty spaces with this value
(if pad is None then this method uses self.padding).
@param size size of the block, used with start or end parameter.
@return string of binary data.
'''
if not isinstance(pad, _DeprecatedParam):
print "IntelHex.tobinstr: 'pad' parameter is deprecated."
if pad is not None:
print "Please, use IntelHex.padding attribute instead."
else:
print "Please, don't pass it explicitly."
print "Use syntax like this: ih.tobinstr(start=xxx, end=yyy, size=zzz)"
else:
pad = None
return self._tobinstr_really(start, end, pad, size)
def _tobinstr_really(self, start, end, pad, size):
return asstr(self._tobinarray_really(start, end, pad, size).tostring())
def tobinfile(self, fobj, start=None, end=None, pad=_DEPRECATED, size=None):
'''Convert to binary and write to file.
@param fobj file name or file object for writing output bytes.
@param start start address of output bytes.
@param end end address of output bytes (inclusive).
@param pad [DEPRECATED PARAMETER, please use self.padding instead]
fill empty spaces with this value
(if pad is None then this method uses self.padding).
@param size size of the block, used with start or end parameter.
'''
if not isinstance(pad, _DeprecatedParam):
print "IntelHex.tobinfile: 'pad' parameter is deprecated."
if pad is not None:
print "Please, use IntelHex.padding attribute instead."
else:
print "Please, don't pass it explicitly."
print "Use syntax like this: ih.tobinfile(start=xxx, end=yyy, size=zzz)"
else:
pad = None
if getattr(fobj, "write", None) is None:
fobj = open(fobj, "wb")
close_fd = True
else:
close_fd = False
fobj.write(self._tobinstr_really(start, end, pad, size))
if close_fd:
fobj.close()
def todict(self):
'''Convert to python dictionary.
@return dict suitable for initializing another IntelHex object.
'''
r = {}
r.update(self._buf)
if self.start_addr:
r['start_addr'] = self.start_addr
return r
def addresses(self):
'''Returns all used addresses in sorted order.
@return list of occupied data addresses in sorted order.
'''
aa = self._buf.keys()
aa.sort()
return aa
def minaddr(self):
'''Get minimal address of HEX content.
@return minimal address or None if no data
'''
aa = self._buf.keys()
if aa == []:
return None
else:
return min(aa)
def maxaddr(self):
'''Get maximal address of HEX content.
@return maximal address or None if no data
'''
aa = self._buf.keys()
if aa == []:
return None
else:
return max(aa)
def __getitem__(self, addr):
''' Get requested byte from address.
@param addr address of byte.
@return byte if address exists in HEX file, or self.padding
if no data found.
'''
t = type(addr)
if t in (int, long):
if addr < 0:
raise TypeError('Address should be >= 0.')
return self._buf.get(addr, self.padding)
elif t == slice:
addresses = self._buf.keys()
ih = IntelHex()
if addresses:
addresses.sort()
start = addr.start or addresses[0]
stop = addr.stop or (addresses[-1]+1)
step = addr.step or 1
for i in xrange(start, stop, step):
x = self._buf.get(i)
if x is not None:
ih[i] = x
return ih
else:
raise TypeError('Address has unsupported type: %s' % t)
def __setitem__(self, addr, byte):
"""Set byte at address."""
t = type(addr)
if t in (int, long):
if addr < 0:
raise TypeError('Address should be >= 0.')
self._buf[addr] = byte
elif t == slice:
if not isinstance(byte, (list, tuple)):
raise ValueError('Slice operation expects sequence of bytes')
start = addr.start
stop = addr.stop
step = addr.step or 1
if None not in (start, stop):
ra = range(start, stop, step)
if len(ra) != len(byte):
raise ValueError('Length of bytes sequence does not match '
'address range')
elif (start, stop) == (None, None):
raise TypeError('Unsupported address range')
elif start is None:
start = stop - len(byte)
elif stop is None:
stop = start + len(byte)
if start < 0:
raise TypeError('start address cannot be negative')
if stop < 0:
raise TypeError('stop address cannot be negative')
j = 0
for i in xrange(start, stop, step):
self._buf[i] = byte[j]
j += 1
else:
raise TypeError('Address has unsupported type: %s' % t)
def __delitem__(self, addr):
"""Delete byte at address."""
t = type(addr)
if t in (int, long):
if addr < 0:
raise TypeError('Address should be >= 0.')
del self._buf[addr]
elif t == slice:
addresses = self._buf.keys()
if addresses:
addresses.sort()
start = addr.start or addresses[0]
stop = addr.stop or (addresses[-1]+1)
step = addr.step or 1
for i in xrange(start, stop, step):
x = self._buf.get(i)
if x is not None:
del self._buf[i]
else:
raise TypeError('Address has unsupported type: %s' % t)
def __len__(self):
"""Return count of bytes with real values."""
return len(self._buf.keys())
def write_hex_file(self, f, write_start_addr=True):
"""Write data to file f in HEX format.
@param f filename or file-like object for writing
@param write_start_addr enable or disable writing start address
record to file (enabled by default).
If there is no start address in obj, nothing
will be written regardless of this setting.
"""
fwrite = getattr(f, "write", None)
if fwrite:
fobj = f
fclose = None
else:
fobj = open(f, 'w')
fwrite = fobj.write
fclose = fobj.close
# Translation table for uppercasing hex ascii string.
# timeit shows that using hexstr.translate(table)
# is faster than hexstr.upper():
# 0.452ms vs. 0.652ms (translate vs. upper)
if sys.version_info[0] >= 3:
table = bytes(range(256)).upper()
else:
table = ''.join(chr(i).upper() for i in range(256))
# start address record if any
if self.start_addr and write_start_addr:
keys = self.start_addr.keys()
keys.sort()
bin = array('B', asbytes('\0'*9))
if keys == ['CS','IP']:
# Start Segment Address Record
bin[0] = 4 # reclen
bin[1] = 0 # offset msb
bin[2] = 0 # offset lsb
bin[3] = 3 # rectyp
cs = self.start_addr['CS']
bin[4] = (cs >> 8) & 0x0FF
bin[5] = cs & 0x0FF
ip = self.start_addr['IP']
bin[6] = (ip >> 8) & 0x0FF
bin[7] = ip & 0x0FF
bin[8] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' +
asstr(hexlify(bin.tostring()).translate(table)) +
'\n')
elif keys == ['EIP']:
# Start Linear Address Record
bin[0] = 4 # reclen
bin[1] = 0 # offset msb
bin[2] = 0 # offset lsb
bin[3] = 5 # rectyp
eip = self.start_addr['EIP']
bin[4] = (eip >> 24) & 0x0FF
bin[5] = (eip >> 16) & 0x0FF
bin[6] = (eip >> 8) & 0x0FF
bin[7] = eip & 0x0FF
bin[8] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' +
asstr(hexlify(bin.tostring()).translate(table)) +
'\n')
else:
if fclose:
fclose()
raise InvalidStartAddressValueError(start_addr=self.start_addr)
# data
addresses = self._buf.keys()
addresses.sort()
addr_len = len(addresses)
if addr_len:
minaddr = addresses[0]
maxaddr = addresses[-1]
if maxaddr > 65535:
need_offset_record = True
else:
need_offset_record = False
high_ofs = 0
cur_addr = minaddr
cur_ix = 0
while cur_addr <= maxaddr:
if need_offset_record:
bin = array('B', asbytes('\0'*7))
bin[0] = 2 # reclen
bin[1] = 0 # offset msb
bin[2] = 0 # offset lsb
bin[3] = 4 # rectyp
high_ofs = int(cur_addr>>16)
b = divmod(high_ofs, 256)
bin[4] = b[0] # msb of high_ofs
bin[5] = b[1] # lsb of high_ofs
bin[6] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' +
asstr(hexlify(bin.tostring()).translate(table)) +
'\n')
while True:
# produce one record
low_addr = cur_addr & 0x0FFFF
# chain_len off by 1
chain_len = min(15, 65535-low_addr, maxaddr-cur_addr)
# search continuous chain
stop_addr = cur_addr + chain_len
if chain_len:
ix = bisect_right(addresses, stop_addr,
cur_ix,
min(cur_ix+chain_len+1, addr_len))
chain_len = ix - cur_ix # real chain_len
# there could be small holes in the chain
# but we will catch them by try-except later
# so for big continuous files we will work
# at maximum possible speed
else:
chain_len = 1 # real chain_len
bin = array('B', asbytes('\0'*(5+chain_len)))
b = divmod(low_addr, 256)
bin[1] = b[0] # msb of low_addr
bin[2] = b[1] # lsb of low_addr
bin[3] = 0 # rectype
try: # if there is small holes we'll catch them
for i in range(chain_len):
bin[4+i] = self._buf[cur_addr+i]
except KeyError:
# we catch a hole so we should shrink the chain
chain_len = i
bin = bin[:5+i]
bin[0] = chain_len
bin[4+chain_len] = (-sum(bin)) & 0x0FF # chksum
fwrite(':' +
asstr(hexlify(bin.tostring()).translate(table)) +
'\n')
# adjust cur_addr/cur_ix
cur_ix += chain_len
if cur_ix < addr_len:
cur_addr = addresses[cur_ix]
else:
cur_addr = maxaddr + 1
break
high_addr = int(cur_addr>>16)
if high_addr > high_ofs:
break
# end-of-file record
fwrite(":00000001FF\n")
if fclose:
fclose()
def tofile(self, fobj, format):
"""Write data to hex or bin file. Preferred method over tobin or tohex.
@param fobj file name or file-like object
@param format file format ("hex" or "bin")
"""
if format == 'hex':
self.write_hex_file(fobj)
elif format == 'bin':
self.tobinfile(fobj)
else:
raise ValueError('format should be either "hex" or "bin";'
' got %r instead' % format)
def gets(self, addr, length):
"""Get string of bytes from given address. If any entries are blank
from addr through addr+length, a NotEnoughDataError exception will
be raised. Padding is not used."""
a = array('B', asbytes('\0'*length))
try:
for i in xrange(length):
a[i] = self._buf[addr+i]
except KeyError:
raise NotEnoughDataError(address=addr, length=length)
return asstr(a.tostring())
def puts(self, addr, s):
"""Put string of bytes at given address. Will overwrite any previous
entries.
"""
a = array('B', asbytes(s))
for i in xrange(len(a)):
self._buf[addr+i] = a[i]
def getsz(self, addr):
"""Get zero-terminated string from given address. Will raise
NotEnoughDataError exception if a hole is encountered before a 0.
"""
i = 0
try:
while True:
if self._buf[addr+i] == 0:
break
i += 1
except KeyError:
raise NotEnoughDataError(msg=('Bad access at 0x%X: '
'not enough data to read zero-terminated string') % addr)
return self.gets(addr, i)
def putsz(self, addr, s):
"""Put string in object at addr and append terminating zero at end."""
self.puts(addr, s)
self._buf[addr+len(s)] = 0
def dump(self, tofile=None):
"""Dump object content to specified file object or to stdout if None.
Format is a hexdump with some header information at the beginning,
addresses on the left, and data on right.
@param tofile file-like object to dump to
"""
if tofile is None:
tofile = sys.stdout
# start addr possibly
if self.start_addr is not None:
cs = self.start_addr.get('CS')
ip = self.start_addr.get('IP')
eip = self.start_addr.get('EIP')
if eip is not None and cs is None and ip is None:
tofile.write('EIP = 0x%08X\n' % eip)
elif eip is None and cs is not None and ip is not None:
tofile.write('CS = 0x%04X, IP = 0x%04X\n' % (cs, ip))
else:
tofile.write('start_addr = %r\n' % start_addr)
# actual data
addresses = self._buf.keys()
if addresses:
addresses.sort()
minaddr = addresses[0]
maxaddr = addresses[-1]
startaddr = int(minaddr>>4)*16
endaddr = int((maxaddr>>4)+1)*16
maxdigits = max(len(str(endaddr)), 4)
templa = '%%0%dX' % maxdigits
range16 = range(16)
for i in xrange(startaddr, endaddr, 16):
tofile.write(templa % i)
tofile.write(' ')
s = []
for j in range16:
x = self._buf.get(i+j)
if x is not None:
tofile.write(' %02X' % x)
if 32 <= x < 127: # GNU less does not like 0x7F (128 decimal) so we'd better show it as dot
s.append(chr(x))
else:
s.append('.')
else:
tofile.write(' --')
s.append(' ')
tofile.write(' |' + ''.join(s) + '|\n')
def merge(self, other, overlap='error'):
"""Merge content of other IntelHex object into current object (self).
@param other other IntelHex object.
@param overlap action on overlap of data or starting addr:
- error: raising OverlapError;
- ignore: ignore other data and keep current data
in overlapping region;
- replace: replace data with other data
in overlapping region.
@raise TypeError if other is not instance of IntelHex
@raise ValueError if other is the same object as self
(it can't merge itself)
@raise ValueError if overlap argument has incorrect value
@raise AddressOverlapError on overlapped data
"""
# check args
if not isinstance(other, IntelHex):
raise TypeError('other should be IntelHex object')
if other is self:
raise ValueError("Can't merge itself")
if overlap not in ('error', 'ignore', 'replace'):
raise ValueError("overlap argument should be either "
"'error', 'ignore' or 'replace'")
# merge data
this_buf = self._buf
other_buf = other._buf
for i in other_buf:
if i in this_buf:
if overlap == 'error':
raise AddressOverlapError(
'Data overlapped at address 0x%X' % i)
elif overlap == 'ignore':
continue
this_buf[i] = other_buf[i]
# merge start_addr
if self.start_addr != other.start_addr:
if self.start_addr is None: # set start addr from other
self.start_addr = other.start_addr
elif other.start_addr is None: # keep existing start addr
pass
else: # conflict
if overlap == 'error':
raise AddressOverlapError(
'Starting addresses are different')
elif overlap == 'replace':
self.start_addr = other.start_addr
#/IntelHex
class IntelHex16bit(IntelHex):
"""Access to data as 16-bit words. Intended to use with Microchip HEX files."""
def __init__(self, source=None):
"""Construct class from HEX file
or from instance of ordinary IntelHex class. If IntelHex object
is passed as source, the original IntelHex object should not be used
again because this class will alter it. This class leaves padding
alone unless it was precisely 0xFF. In that instance it is sign
extended to 0xFFFF.
@param source file name of HEX file or file object
or instance of ordinary IntelHex class.
Will also accept dictionary from todict method.
"""
if isinstance(source, IntelHex):
# from ihex8
self.padding = source.padding
self.start_addr = source.start_addr
# private members
self._buf = source._buf
self._offset = source._offset
elif isinstance(source, dict):
raise IntelHexError("IntelHex16bit does not support initialization from dictionary yet.\n"
"Patches are welcome.")
else:
IntelHex.__init__(self, source)
if self.padding == 0x0FF:
self.padding = 0x0FFFF
def __getitem__(self, addr16):
"""Get 16-bit word from address.
Raise error if only one byte from the pair is set.
We assume a Little Endian interpretation of the hex file.
@param addr16 address of word (addr8 = 2 * addr16).
@return word if bytes exists in HEX file, or self.padding
if no data found.
"""
addr1 = addr16 * 2
addr2 = addr1 + 1
byte1 = self._buf.get(addr1, None)
byte2 = self._buf.get(addr2, None)
if byte1 != None and byte2 != None:
return byte1 | (byte2 << 8) # low endian
if byte1 == None and byte2 == None:
return self.padding
raise BadAccess16bit(address=addr16)
def __setitem__(self, addr16, word):
"""Sets the address at addr16 to word assuming Little Endian mode.
"""
addr_byte = addr16 * 2
b = divmod(word, 256)
self._buf[addr_byte] = b[1]
self._buf[addr_byte+1] = b[0]
def minaddr(self):
'''Get minimal address of HEX content in 16-bit mode.
@return minimal address used in this object
'''
aa = self._buf.keys()
if aa == []:
return 0
else:
return min(aa)>>1
def maxaddr(self):
'''Get maximal address of HEX content in 16-bit mode.
@return maximal address used in this object
'''
aa = self._buf.keys()
if aa == []:
return 0
else:
return max(aa)>>1
def tobinarray(self, start=None, end=None, size=None):
'''Convert this object to binary form as array (of 2-bytes word data).
If start and end unspecified, they will be inferred from the data.
@param start start address of output data.
@param end end address of output data (inclusive).
@param size size of the block (number of words),
used with start or end parameter.
@return array of unsigned short (uint16_t) data.
'''
bin = array('H')
if self._buf == {} and None in (start, end):
return bin
if size is not None and size <= 0:
raise ValueError("tobinarray: wrong value for size")
start, end = self._get_start_end(start, end, size)
for addr in xrange(start, end+1):
bin.append(self[addr])
return bin
#/class IntelHex16bit
def hex2bin(fin, fout, start=None, end=None, size=None, pad=None):
"""Hex-to-Bin convertor engine.
@return 0 if all OK
@param fin input hex file (filename or file-like object)
@param fout output bin file (filename or file-like object)
@param start start of address range (optional)
@param end end of address range (inclusive; optional)
@param size size of resulting file (in bytes) (optional)
@param pad padding byte (optional)
"""
try:
h = IntelHex(fin)
except HexReaderError, e:
txt = "ERROR: bad HEX file: %s" % str(e)
print(txt)
return 1
# start, end, size
if size != None and size != 0:
if end == None:
if start == None:
start = h.minaddr()
end = start + size - 1
else:
if (end+1) >= size:
start = end + 1 - size
else:
start = 0
try:
if pad is not None:
# using .padding attribute rather than pad argument to function call
h.padding = pad
h.tobinfile(fout, start, end)
except IOError, e:
txt = "ERROR: Could not write to file: %s: %s" % (fout, str(e))
print(txt)
return 1
return 0
#/def hex2bin
def bin2hex(fin, fout, offset=0):
"""Simple bin-to-hex convertor.