-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht7_file.go
176 lines (147 loc) · 3.44 KB
/
t7_file.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
package symbol
import (
"bytes"
"fmt"
"log"
"os"
)
const (
T7Length = 0x80000
T7SRAMAddress = 0x0F00000
)
var T7MagicBytes = []byte{0xFF, 0xFF, 0xEF, 0xFC, 0x00}
type T7File struct {
autoFixFooter bool
data []byte // the raw data
chassisIDDetected bool
immocodeDetected bool
symbolTableMarkerDetected bool
symbolTableChecksumDetected bool
f2ChecksumDetected bool
chassisIDCounter int
symbolTableAddress int
sramOffset int
checksumF2 int
checksumFB int
bottomOfFlash int
romChecksumType int
fwLength int
valueF5 int
valueF6 int
valueF7 int
valueF8 int
romChecksumError byte
chassisID string
immobilizerID string
softwareVersion string
carDescription string
partNumber string
engineType string
vehicleIDNr string
dateModified string
ecuHardwareNr string
testserialnr string
lastModifiedBy []byte
csumArea [16]T7ChecksumArea
printFunc func(string, ...any)
*Collection // the symbol collection
}
type T7FileOpt func(*T7File) error
func WithT7AutoFixFooter() T7FileOpt {
return func(t7 *T7File) error {
t7.autoFixFooter = true
return nil
}
}
func WithT7PrintFunc(f func(string, ...any)) T7FileOpt {
return func(t7 *T7File) error {
t7.printFunc = f
return nil
}
}
func NewT7File(data []byte, opts ...T7FileOpt) (*T7File, error) {
if len(data) != T7Length {
return nil, ErrInvalidLength
}
if !bytes.HasPrefix(data, T7MagicBytes) {
return nil, ErrMagicBytesNotFound
}
t7 := &T7File{
data: data,
chassisID: "00000000000000000",
immobilizerID: "000000000000000",
engineType: "0000000000000",
vehicleIDNr: "000000000",
partNumber: "0000000",
softwareVersion: "000000000000",
carDescription: "00000000000000000000",
dateModified: "0000",
ecuHardwareNr: "0000000",
lastModifiedBy: []byte{0x42, 0xFB, 0xFA, 0xFF, 0xFF},
testserialnr: "050225",
printFunc: func(format string, v ...any) {
log.Printf(format, v...)
},
}
for _, opt := range opts {
if err := opt(t7); err != nil {
return nil, err
}
}
return t7.init()
}
func (t7 *T7File) init() (*T7File, error) {
t7.loadHeaders()
symbols, err := loadT7Symbols(t7.data, func(s string) {
t7.printFunc(s)
})
if err != nil {
return nil, err
}
t7.Collection = symbols
return t7, t7.VerifyChecksum()
}
func (t7 *T7File) UpdateSymbol(sym *Symbol) error {
addr := sym.Address
if sym.Address > 0x7FFFFF {
if sym.Address-sym.SramOffset > uint32(len(t7.data)) {
return ErrAddressOutOfRange
}
addr = sym.Address - sym.SramOffset
}
for i, b := range sym.data {
(t7.data)[addr+uint32(i)] = b
}
return nil
}
func (t7 *T7File) Bytes() []byte {
return t7.data
}
func (t7 *T7File) Save(filename string) error {
for _, sym := range t7.Symbols() {
addr := sym.Address
if sym.Address > 0x7FFFFF {
if sym.Address-sym.SramOffset > uint32(len(t7.data)) {
return ErrAddressOutOfRange
}
addr = sym.Address - sym.SramOffset
}
for idx, b := range sym.data {
(t7.data)[addr+uint32(idx)] = b
}
}
if err := t7.UpdateChecksum(); err != nil {
return err
}
if err := t7.VerifyChecksum(); err != nil {
return err
}
err := os.WriteFile(filename, t7.data, 0644)
if err != nil {
return fmt.Errorf("failed to write %s : %w", filename, err)
}
return nil
}
func (t7 *T7File) Version() string {
return t7.softwareVersion
}