/
data.go
317 lines (288 loc) · 9.78 KB
/
data.go
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
/*
* Copyright (c) 2014-2015, 2020-2023 Vadim Vygonets <vadik@vygo.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
Package ihex implements access to Intel HEX files.
IHEX files consist of records representing instructions for a PROM
programmer to write data to memory locations (referred to here as "the
address space") and set certain registers ("the start address"), along
with record types this package only handles internally (EOF and extended
addressing). As these records may appear in a file in any order and
are defined to have peculiar corner cases, this package only presents
the user a simplified view of the address space, losing details of a
particular representation on input and generating conservative output.
Documentation for (*IHex).ReadFrom describes the abstraction in more
detail.
IHEX files come in three formats. The format termed "8-bit" has,
naturally, contiguous 16-bit address space (64KB), "16-bit" format has
crazy Intel-segmeted 20-bit address space (1MB) and "32-bit" has 32-bit
(4GB) addressing which is contiguous but the high 16 bits of the address
are still set separately. This package only allows Extended Segment
Address and Start Segment Address records in 16-bit files and Extended
Linear Address and Start Linear Address records in 32-bit files.
*/
package ihex
import (
"bufio"
"errors"
"io"
"sort"
"strconv"
)
// IHEX file formats
const (
FormatAuto = iota // Reading: auto-detect format; writing: I8HEX
Format8Bit // I8HEX format, 16-bit address space
Format16Bit // I16HEX format, 20-bit address space
Format32Bit // I32HEX format, 32-bit address space
)
// record types
const (
dataRec = byte(iota) // Data
eofRec // End of File
extSegmentAddrRec // Extended Segment Address
startSegmentAddrRec // Start Segment Address
extLinearAddrRec // Extended Linear Address
startLinearAddrRec // Start Linear Address
)
// field offsets within record
const (
lenOff = 0 // RECLEN
addrOff = 1 // LOAD OFFSET
typeOff = 3 // RECTYP
dataOff = 4 // INFO or DATA
// last byte: CHKSUM
)
var (
ErrArgs = errors.New("ihex: invalid arguments")
ErrChecksum = errors.New("ihex: checksum error")
ErrClosed = errors.New("ihex: writer is closed")
ErrFormat = errors.New("ihex: invalid record for format")
ErrRange = errors.New("ihex: address out of range")
ErrRecord = errors.New("ihex: unknown record type")
ErrSyntax = errors.New("ihex: invalid syntax")
)
type SyntaxError struct {
Err error // ErrChecksum, ErrFormat, ErrRecord or ErrSyntax
Line int // 0 for missing EOF record, otherwise input line number
Format byte // Active IHEX format
Record string // "" for missing EOF record, otherwise input line
}
var formatName = []string{"unspecified", "I8HEX", "I16HEX", "I32HEX"}
// Error returns the error formatted as one of:
// "ihex: <invalid syntax/checksum error> on line <n>"
// "ihex: invalid record for <unspecified/I8HEX/I16HEX/I32HEX> format on line <n>"
// "ihex: missing EOF record"
func (e SyntaxError) Error() string {
switch {
case e.Line == 0:
return "ihex: missing EOF record"
case e.Err == ErrFormat:
return "ihex: invalid record for " + formatName[e.Format] +
" format on line " + strconv.Itoa(e.Line)
default:
return e.Err.Error() + " on line " + strconv.Itoa(e.Line)
}
}
// Chunk represents a contiguous area in the IHEX address space.
type Chunk struct {
Addr uint32
Data []byte
}
// end returns the address at the end of c.
func (c Chunk) end() int64 {
return int64(c.Addr) + int64(len(c.Data))
}
// overlaps returns true if two Chunks overlap or are adjacent.
// XXX misnomer
func (c Chunk) overlaps(cc Chunk) bool {
return int64(c.Addr) <= cc.end() && int64(cc.Addr) <= c.end()
}
// over returns a Chunk with data from two adjacent or overlapping Chunks,
// over and under, the former taking the precedence over the latter.
// over may overwrite data in the Chunks.
func (over Chunk) over(under Chunk) Chunk {
switch {
case over.Addr <= under.Addr && over.end() >= under.end():
return over
case over.Addr < under.Addr:
over.Data = append(over.Data,
under.Data[over.end()-int64(under.Addr):]...)
return over
case over.end() > under.end():
under.Data = append(under.Data[:over.Addr-under.Addr],
over.Data...)
return under
default:
copy(under.Data[over.Addr-under.Addr:], over.Data)
return under
}
}
// ChunkList is a slice of Chunks.
type ChunkList []Chunk
// find finds the first Chunk in a sorted slice cl whose end is
// at or after addr.
func (cl ChunkList) find(addr int64) int {
return sort.Search(len(cl),
func(i int) bool { return cl[i].end() >= addr })
}
// add adds data in c to the address space represented by a
// sorted slice cl, returning the merged Chunk.
func (cl *ChunkList) add(c Chunk) Chunk {
if len(c.Data) == 0 {
return c
}
if i := cl.find(int64(c.Addr)); i == len(*cl) {
*cl = append(*cl, c)
} else {
j := i
for j < len(*cl) && c.overlaps((*cl)[j]) {
c = c.over((*cl)[j])
j++
}
if j != i+1 {
*cl = append((*cl)[:i+1], (*cl)[j:]...)
}
(*cl)[i] = c
}
return c
}
// normal returns true if cl is a sorted list of nonadjacent
// non-zero-legth Chunks.
func (cl ChunkList) normal() bool {
end := int64(-1)
for _, v := range cl {
if int64(v.Addr) <= end || len(v.Data) == 0 {
return false
}
end = v.end()
}
return true
}
// Normalize turns cl into a sorted list of nonadjacent non-zero-legth
// Chunks representing the address space as it would look after the
// data in cl would be written to it sequentially.
// Normalize may mutate data in place.
func (cl *ChunkList) Normalize() {
if cl.normal() {
return
}
sorted := make(ChunkList, 0, len(*cl))
for _, v := range *cl {
sorted.add(v)
}
*cl = sorted
}
// IHex represents the contents of an IHEX file.
type IHex struct {
// Format describes the file format. Legal formats are
// FormatAuto, Format8Bit, Format16Bit and Format32Bit;
// for writing, FormatAuto is equivalent to Format8Bit.
Format byte
// DataRecLen is the maximum number of bytes in a Data
// record length generated by WriteTo. Must be a power
// of two or 0. In the latter case the default length of
// 16 is used.
DataRecLen byte
// Start is the "start address". For 32-bit format it
// represents the contents of EIP on 80386, and for
// 16-bit, the pair of 16-bit registers CS:IP on 8086.
// 8-bit format does not support setting the start
// address.
Start uint32
// StartSet indicates that Start has been set by
// ReadFrom, or should be written by WriteTo even if it's
// zero.
StartSet bool
// Chunks are the data written to the address space.
Chunks ChunkList
}
/*
ReadFrom reads an IHEX file from r, filling ix. ReadFrom returns nil
on success, ErrSyntax or ErrChecksum in case of invalid input
and anything else on read errors. ReadFrom may overread r.
ix.Format defines the format of the file being read. If ReadFrom is
called with the ix.Format equal to FormatAuto (zero value) and a
record specific to 16-bit or 32-bit format is encoutered,
ix.Format is set accordingly. Due to different semantics of Data records
spanning 64KB address boundaries, such records are disallowed with
FormatAuto (however, one shouldn't expect to encounter such records
the wild).
ix.Chunks is set to a sorted list of nonadjacent contiguous data areas
representing programmed areas in a (potentially sparse) address space of
a target machine, with later writes overwriting results of earlier ones.
This does not necessarily represent the behaviour of actual hardware;
e.g., a location in flash memory contains conjunction (binary AND) of
all values written to it since last erase. If ix.Chunks is non-empty
before calling ReadFrom, it is normalized to allow interleaving reads
from several files with direct data manipulation.
If any Start Segment/Linear Address records are encountered,
ix.StartSet is set to true, and ix.Start to the value in the last such
record.
*/
func (ix *IHex) ReadFrom(r io.Reader) error {
if ix.Format > Format32Bit {
return ErrArgs
}
var (
p = &parser{ix: ix}
s = bufio.NewScanner(r)
line int
)
ix.Chunks.Normalize()
for s.Scan() {
line++
if err := p.parseLine(s.Text()); err != nil {
if err == io.EOF {
return nil
}
return SyntaxError{
Err: err,
Line: line,
Format: ix.Format,
Record: s.Text(),
}
}
}
if err := s.Err(); err != nil {
return err
}
return SyntaxError{Err: ErrSyntax}
}
// WriteTo writes data from ix to an IHEX file, using a Writer with
// parameters specified by ix.Format ix.DataRecLength. It writes
// ix.Chunks in order, flushing the write buffer between Chunks.
// If ix.StartSet is true or ix.Start is not zero, it then sets the
// start address.
func (ix *IHex) WriteTo(w io.Writer) error {
xw, err := NewWriter(w, ix.Format, ix.DataRecLen)
if err != nil {
return err
}
for _, v := range ix.Chunks {
if _, err = xw.Seek(int64(v.Addr), 0); err != nil {
return err
}
if _, err = xw.Write(v.Data); err != nil {
return err
}
}
if ix.StartSet || ix.Start != 0 {
if err = xw.WriteStart(ix.Start); err != nil {
return err
}
}
return xw.Close()
}