Skip to content

Commit 5e2b05b

Browse files
merge: support 'identical' on overlapped data
When merging multiple .hex files, in case of overlap we sometimes need to just 'ensure' that values are identical. The new 'identical' mode ensures this. Note: the behavior for start_addr is currently a 'identical' (i.e. the overlap configuration is checked only when values differ). So we might want to change the behavior for data too: - if data is identical: silently bail out - add a 'stricter' mode that corresponds to the current behavior
1 parent 2fd8f41 commit 5e2b05b

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

docs/manual/part2-6.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ It can take three options:
1616
* ``error`` - stop and raise an exception (default)
1717
* ``ignore`` - keep data from the original that contains data at overlapped address
1818
* ``replace`` - use data from the new object that contains data at overlapped address
19+
* ``identical`` - raise an exception if data is different
1920

2021
You can merge only part of other hex file by using slice index notation::
2122

docs/manual/part3-4.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This is a script to merge two different hex files. It is a frontend for the
2222
contains data at overlapped address
2323
* replace -- use data from last file that
2424
contains data at overlapped address
25+
* identical -- raise error if data differ
26+
2527

2628
Arguments:
2729
FILES list of hex files for merging

intelhex/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,9 @@ def merge(self, other, overlap='error'):
853853
- ignore: ignore other data and keep current data
854854
in overlapping region;
855855
- replace: replace data with other data
856-
in overlapping region.
856+
in overlapping region;
857+
- identical: raising OverlapError if data is not
858+
identical.
857859
858860
@raise TypeError if other is not instance of IntelHex
859861
@raise ValueError if other is the same object as self
@@ -866,9 +868,9 @@ def merge(self, other, overlap='error'):
866868
raise TypeError('other should be IntelHex object')
867869
if other is self:
868870
raise ValueError("Can't merge itself")
869-
if overlap not in ('error', 'ignore', 'replace'):
871+
if overlap not in ('error', 'ignore', 'replace', 'identical'):
870872
raise ValueError("overlap argument should be either "
871-
"'error', 'ignore' or 'replace'")
873+
"'error', 'ignore', 'replace' or 'identical'")
872874
# merge data
873875
this_buf = self._buf
874876
other_buf = other._buf
@@ -879,6 +881,9 @@ def merge(self, other, overlap='error'):
879881
'Data overlapped at address 0x%X' % i)
880882
elif overlap == 'ignore':
881883
continue
884+
elif overlap == 'identical' and this_buf[i] != other_buf[i]:
885+
raise AddressOverlapError(
886+
'Data at address 0x%X is different: 0x%X vs. 0x%X' % (i, this_buf[i], other_buf[i]))
882887
this_buf[i] = other_buf[i]
883888
# merge start_addr
884889
if self.start_addr != other.start_addr:

intelhex/scripts/hexmerge.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
contains data at overlapped address
5858
* replace -- use data from last file that
5959
contains data at overlapped address
60+
* identical -- if overlapped data verifies that values
61+
are identical
6062
6163
Arguments:
6264
FILES list of hex files for merging
@@ -122,7 +124,7 @@ def main(args=None):
122124
elif o == '--no-start-addr':
123125
write_start_addr = False
124126
elif o == '--overlap':
125-
if a in ('error', 'ignore', 'replace'):
127+
if a in ('error', 'ignore', 'replace', 'identical'):
126128
overlap = a
127129
else:
128130
raise getopt.GetoptError('Bad overlap value')

intelhex/test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ def test_merge_wrong_args(self):
11711171
ih1.merge, ih1)
11721172
ih2 = IntelHex()
11731173
self.assertRaisesMsg(ValueError, "overlap argument should be either "
1174-
"'error', 'ignore' or 'replace'",
1174+
"'error', 'ignore', 'replace' or 'identical'",
11751175
ih1.merge, ih2, overlap='spam')
11761176

11771177
def test_merge_overlap(self):
@@ -1191,6 +1191,17 @@ def test_merge_overlap(self):
11911191
ih2 = IntelHex({0:2})
11921192
ih1.merge(ih2, overlap='replace')
11931193
self.assertEqual({0:2}, ih1.todict())
1194+
# identical: same value: ok
1195+
ih1 = IntelHex({0:1})
1196+
ih2 = IntelHex({0:1})
1197+
ih1.merge(ih2, overlap='identical')
1198+
self.assertEqual({0:1}, ih1.todict())
1199+
# identical: different value: raise error
1200+
ih1 = IntelHex({0:1})
1201+
ih2 = IntelHex({0:2})
1202+
self.assertRaisesMsg(intelhex.AddressOverlapError,
1203+
'Data at address 0x0 is different: 0x1 vs. 0x2',
1204+
ih1.merge, ih2, overlap='identical')
11941205

11951206
def test_merge_start_addr(self):
11961207
# this, None

0 commit comments

Comments
 (0)