-
Notifications
You must be signed in to change notification settings - Fork 0
/
srec.go
287 lines (268 loc) · 7.11 KB
/
srec.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
package srec
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
// constructed values of a field that always has a fixed length in the S record
const (
TypeLen = 1
LengthLen = 1
S0AddrLen = 2
S1AddrLen = 2
S2AddrLen = 3
S3AddrLen = 4
S5AddrLen = 0
S7AddrLen = 4
S8AddrLen = 3
S9AddrLen = 2
CSumLen = 1
)
// constructed values of Maximum size per record type
const (
maxSizeOfS1Addr = 0xFFFF
maxSizeOfS2Addr = 0xFFFFFF
maxSizeOfS3Addr = 0xFFFFFFFF
)
// Srec is a type of structure with multiple Record structures
type Srec struct {
Records []*Record
}
// Record is a type of structure that expresses S records per row
type Record struct {
Srectype string
Length uint8
Address uint32
Data []byte
Checksum uint8
}
// NewSrec returns a new Srec object
func NewSrec() *Srec {
return &Srec{}
}
// newRecord returns a new Srec object
func newRecord() *Record {
return &Record{}
}
// getAddrLen returns the length of address for each srectype as byte length
// S4, S6 are not handled
func getAddrLen(srectype string) (int, error) {
switch srectype {
case "S0":
return S0AddrLen, nil
case "S1":
return S1AddrLen, nil
case "S2":
return S2AddrLen, nil
case "S3":
return S3AddrLen, nil
case "S5":
return S5AddrLen, nil
case "S7":
return S7AddrLen, nil
case "S8":
return S8AddrLen, nil
case "S9":
return S9AddrLen, nil
default:
return 0, fmt.Errorf("%s is not srectype", srectype)
}
}
// EndAddress returns endaddress of the record
func (rec *Record) EndAddress() uint32 {
return rec.Address + uint32(len(rec.Data)) - 1
}
// CalcChecksum calculates the checksum value of the record from the information of the arguments
func (rec *Record) CalcChecksum() (uint8, error) {
return calcChecksum(rec.Srectype, rec.Length, rec.Address, rec.Data)
}
// CalcChecksum calculates the checksum value of the record from the information of the arguments
// S4, S6 are not handled
func calcChecksum(srectype string, len uint8, addr uint32, data []byte) (uint8, error) {
bytes := []byte{len}
switch srectype {
case "S0", "S1", "S9":
bytes = append(bytes, byte((addr&0xFF00)>>8), byte((addr&0x00FF)>>0))
case "S2", "S8":
bytes = append(bytes, byte((addr&0xFF0000)>>16), byte((addr&0x00FF00)>>8), byte((addr&0x0000FF)>>0))
case "S3", "S7":
bytes = append(bytes, byte((addr&0xFF000000)>>24), byte((addr&0x00FF0000)>>16), byte((addr&0x0000FF00)>>8), byte((addr&0x000000FF))>>0)
case "S5":
// Since S5 has no address part, it is not added to bytes
default:
return 0, fmt.Errorf("%s is invalid srectype", srectype)
}
bytes = append(bytes, data...)
sum := byte(0)
for _, b := range bytes {
sum += b
}
return uint8(^sum), nil
}
// Parse creates a Srec object (basically an opened file) from io.Reader
// If it contains a character string that starts with S4, S6 or does not start S [0-9] to the first 2 bytes,
// processing stops and an error will be return
func (srs *Srec) Parse(fileReader io.Reader) error {
scanner := bufio.NewScanner(fileReader)
scanner.Split(scanLinesCustom)
for scanner.Scan() {
line := scanner.Text()
srectype := line[:2]
switch srectype {
case "S0", "S1", "S2", "S3", "S5", "S7", "S8", "S9":
rec := newRecord()
err := rec.getRecordFields(line)
if err != nil {
return err
}
srs.Records = append(srs.Records, rec)
default:
return fmt.Errorf("%s is invalid srectype", srectype)
}
}
return nil
}
// getRecordFields creates a Record object from a string
func (rec *Record) getRecordFields(line string) error {
// srectype
stype := line[:2]
// Acquire the length of the field of address according to srectype
addrLen, err := getAddrLen(line[:2])
if err != nil {
return err
}
dataLen, err := strconv.ParseUint(line[2:4], 16, 32)
if err != nil {
return err
}
// address
// Since there is no address part in S5, leave the initial value at 0 (since null character does not become 0 in ParseUint)
addr := uint64(0)
if stype != "S5" {
addr, err = strconv.ParseUint(line[4:4+addrLen*2], 16, 32)
if err != nil {
return err
}
}
// data
data := make([]byte, 0)
dataIndexSt := TypeLen*2 + LengthLen*2 + addrLen*2
dataIndexEd := (TypeLen*2 + LengthLen*2) + (int(dataLen)*2 - CSumLen*2)
for i := dataIndexSt; i < dataIndexEd; i += 2 {
var b uint64
b, err = strconv.ParseUint(line[i:i+2], 16, 32)
if err != nil {
return err
}
data = append(data, byte(b))
}
// checksum
cSumIndexSt := TypeLen*2 + LengthLen*2 + dataLen*2 - CSumLen*2
cSumIndexEd := TypeLen*2 + LengthLen*2 + dataLen*2
csum, err := strconv.ParseUint(line[cSumIndexSt:cSumIndexEd], 16, 32)
if err != nil {
return err
}
rec.Srectype = stype
rec.Length = uint8(dataLen)
rec.Address = uint32(addr)
rec.Data = data
rec.Checksum = uint8(csum)
return nil
}
// EndAddr returns the record's end address from the records of the Srec object
// Since the end address of the S record is (address of the last record + data length -1), 1 is subtracted
// Please be aware that processing costs are high as it traverses the entire object
func (srs *Srec) EndAddr() uint32 {
if len(srs.Records) == 0 {
return 0
}
max := uint32(0x00000000)
dLen := uint32(0)
for _, r := range srs.Records {
switch r.Srectype {
case "S1", "S2", "S3":
if r.Address > max {
max = r.Address
dLen = uint32(len(r.Data))
}
}
}
return max + dLen - 1
}
// MakeRec creates and returns a new Record object from the argument information
func MakeRec(srectype string, addr uint32, data []byte) (*Record, error) {
r := newRecord()
r.Srectype = srectype
// Since EndAddress is calculated based on Address and Data, it is set before checking whether to convert
r.Address = addr
r.Data = data
switch r.Srectype {
case "S1", "S2":
if r.EndAddress() > maxSizeOfS1Addr {
r.Srectype = "S2"
}
if r.EndAddress() > maxSizeOfS2Addr {
r.Srectype = "S3"
}
default:
}
// Checksum, Length operation depends on srectype, so calculate them after it is fixed
l, err := getAddrLen(r.Srectype)
if err != nil {
return nil, err
}
r.Length = uint8(l) + uint8(len(data)) + CSumLen
r.Checksum, err = calcChecksum(r.Srectype, r.Length, r.Address, r.Data)
if err != nil {
return nil, err
}
return r, nil
}
// scanLinesCustom is a splitFunc corresponding to all line feed codes of CRLF, LF, CR
func scanLinesCustom(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
i, j := bytes.IndexByte(data, '\n'), bytes.IndexByte(data, '\r')
if i < j {
// if LF
if i >= 0 {
return i + 1, data[0:i], nil
}
// if CRLF
if j < len(data)-1 && isLF(data[j+1]) {
return j + 2, data[0:j], nil
}
// if CR
return j + 1, data[0:j], nil
} else if j < i {
if j >= 0 {
// if CRLF
if j < len(data)-1 && isLF(data[j+1]) {
return j + 2, data[0:j], nil
}
// if CR
return j + 1, data[0:j], nil
}
// if LF
return i + 1, data[0:i], nil
} else {
// this case is only "i == -1 && j == -1"
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// isLF returns weather b is LF
func isLF(b byte) bool {
if b == '\n' {
return true
}
return false
}