Skip to content

Commit 0bd388c

Browse files
committed
Correctly handle 8086 extended record
Fixes arduino/arduino-cli#1009
1 parent baab252 commit 0bd388c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

gohex.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99

1010
// Constants definitions of IntelHex record types
1111
const (
12-
_DATA_RECORD byte = 0 // Record with data bytes
13-
_EOF_RECORD byte = 1 // Record with end of file indicator
14-
_ADDRESS_RECORD byte = 4 // Record with extended linear address
15-
_START_RECORD byte = 5 // Record with start linear address
12+
_DATA_RECORD byte = 0 // Record with data bytes
13+
_EOF_RECORD byte = 1 // Record with end of file indicator
14+
_EXTENDED_RECORD byte = 2 // Record with end of file indicator
15+
_ADDRESS_RECORD byte = 4 // Record with extended linear address
16+
_START_RECORD byte = 5 // Record with start linear address
1617
)
1718

1819
// Structure with binary data segment fields
@@ -33,6 +34,7 @@ type Memory struct {
3334
dataSegments []*DataSegment // Slice with pointers to DataSegments
3435
startAddress uint32 // Start linear address
3536
extendedAddress uint32 // Extended linear address
37+
offset uint32 // Extended offset inside same linear address
3638
eofFlag bool // End of file record exist flag
3739
startFlag bool // Start address record exist flag
3840
lineNum uint // Parser input line number
@@ -211,7 +213,7 @@ func (m *Memory) parseIntelHexRecord(bytes []byte) error {
211213
switch record_type := bytes[3]; record_type {
212214
case _DATA_RECORD:
213215
a, data := getDataLine(bytes)
214-
adr := uint32(a) + m.extendedAddress
216+
adr := uint32(a) + m.extendedAddress + m.offset
215217
err = m.AddBinary(adr, data)
216218
if err != nil {
217219
return err
@@ -222,6 +224,12 @@ func (m *Memory) parseIntelHexRecord(bytes []byte) error {
222224
return newParseError(_RECORD_ERROR, err.Error(), m.lineNum)
223225
}
224226
m.eofFlag = true
227+
case _EXTENDED_RECORD:
228+
//Extended 8086 Segment Record
229+
m.offset, err = getExtendedSegmentAddress(bytes)
230+
if err != nil {
231+
return newParseError(_RECORD_ERROR, err.Error(), m.lineNum)
232+
}
225233
case _ADDRESS_RECORD:
226234
m.extendedAddress, err = getExtendedAddress(bytes)
227235
if err != nil {

helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ func getExtendedAddress(bytes []byte) (adr uint32, err error) {
5656
return adr, nil
5757
}
5858

59+
func getExtendedSegmentAddress(bytes []byte) (adr uint32, err error) {
60+
if bytes[0] != 2 {
61+
return 0, errors.New("incorrect data length field in extended linear address line")
62+
}
63+
if binary.BigEndian.Uint16(bytes[1:3]) != 0 {
64+
return 0, errors.New("incorrect address field in extended linear address line")
65+
}
66+
adr = uint32(binary.BigEndian.Uint16(bytes[4:6])) << 4
67+
return adr, nil
68+
}
69+
5970
func getDataLine(bytes []byte) (adr uint16, data []byte) {
6071
size := bytes[0]
6172
adr = binary.BigEndian.Uint16(bytes[1:3])

0 commit comments

Comments
 (0)