Skip to content

Commit 311e9b3

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 9769d3f commit 311e9b3

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
@@ -850,7 +850,9 @@ def merge(self, other, overlap='error'):
850850
- ignore: ignore other data and keep current data
851851
in overlapping region;
852852
- replace: replace data with other data
853-
in overlapping region.
853+
in overlapping region;
854+
- identical: raising OverlapError if data is not
855+
identical.
854856
855857
@raise TypeError if other is not instance of IntelHex
856858
@raise ValueError if other is the same object as self
@@ -863,9 +865,9 @@ def merge(self, other, overlap='error'):
863865
raise TypeError('other should be IntelHex object')
864866
if other is self:
865867
raise ValueError("Can't merge itself")
866-
if overlap not in ('error', 'ignore', 'replace'):
868+
if overlap not in ('error', 'ignore', 'replace', 'identical'):
867869
raise ValueError("overlap argument should be either "
868-
"'error', 'ignore' or 'replace'")
870+
"'error', 'ignore', 'replace' or 'identical'")
869871
# merge data
870872
this_buf = self._buf
871873
other_buf = other._buf
@@ -876,6 +878,9 @@ def merge(self, other, overlap='error'):
876878
'Data overlapped at address 0x%X' % i)
877879
elif overlap == 'ignore':
878880
continue
881+
elif overlap == 'identical' and this_buf[i] != other_buf[i]:
882+
raise AddressOverlapError(
883+
'Data at address 0x%X is different: 0x%X vs. 0x%X' % (i, this_buf[i], other_buf[i]))
879884
this_buf[i] = other_buf[i]
880885
# merge start_addr
881886
if self.start_addr != other.start_addr:

intelhex/test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ def test_merge_wrong_args(self):
11491149
ih1.merge, ih1)
11501150
ih2 = IntelHex()
11511151
self.assertRaisesMsg(ValueError, "overlap argument should be either "
1152-
"'error', 'ignore' or 'replace'",
1152+
"'error', 'ignore', 'replace' or 'identical'",
11531153
ih1.merge, ih2, overlap='spam')
11541154

11551155
def test_merge_overlap(self):
@@ -1169,6 +1169,17 @@ def test_merge_overlap(self):
11691169
ih2 = IntelHex({0:2})
11701170
ih1.merge(ih2, overlap='replace')
11711171
self.assertEqual({0:2}, ih1.todict())
1172+
# identical: same value: ok
1173+
ih1 = IntelHex({0:1})
1174+
ih2 = IntelHex({0:1})
1175+
ih1.merge(ih2, overlap='identical')
1176+
self.assertEqual({0:1}, ih1.todict())
1177+
# identical: different value: raise error
1178+
ih1 = IntelHex({0:1})
1179+
ih2 = IntelHex({0:2})
1180+
self.assertRaisesMsg(intelhex.AddressOverlapError,
1181+
'Data at address 0x0 is different: 0x1 vs. 0x2',
1182+
ih1.merge, ih2, overlap='identical')
11721183

11731184
def test_merge_start_addr(self):
11741185
# this, None

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+
* check -- 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', 'check'):
126128
overlap = a
127129
else:
128130
raise getopt.GetoptError('Bad overlap value')

0 commit comments

Comments
 (0)