-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfctrl.go
268 lines (244 loc) · 5.86 KB
/
bfctrl.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
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"go.bug.st/serial"
"log"
"os"
"strings"
"time"
)
type BfIo struct {
Port serial.Port
BlockSize uint8
}
var devName string
var baudRate int
var radioType int
func initCmdline() {
flag.StringVar(&devName, "dev", "", "serial port device")
flag.IntVar(&baudRate, "b", 115200, "baud rate")
flag.IntVar(&radioType, "t", -1, "radio type")
flag.Parse()
}
func HexString(data []byte) string {
var buf bytes.Buffer
for i := 0; i < len(data)-1; i++ {
buf.WriteString(fmt.Sprintf("%02X ", data[i]))
}
buf.WriteString(fmt.Sprintf("%02X", data[len(data)-1]))
return buf.String()
}
func HexStringS(data []byte, sep string) string {
var buf bytes.Buffer
for i := 0; i < len(data)-1; i++ {
buf.WriteString(fmt.Sprintf("%02X%s", data[i], sep))
}
buf.WriteString(fmt.Sprintf("%02X", data[len(data)-1]))
return buf.String()
}
func (b *BfIo) sendReceive(tx []uint8, rspCount int) ([]uint8, error) {
b.Port.Write(tx)
res := make([]uint8, rspCount)
ofs := 0
for {
n, err := b.Port.Read(res[ofs:])
if nil != err {
return nil, err
}
ofs += n
if ofs == rspCount {
return res, nil
} else if 0 == n {
return res[:ofs], errors.New(fmt.Sprintf("short read [%x %x] ", rspCount, ofs))
}
}
return res, nil
}
func (b *BfIo) initialComm() error {
if res, err := b.sendReceive([]uint8("PROGRAMBFNORMALU"), 1); nil != err {
return err
} else {
fmt.Printf("res:%v\n", HexString(res))
}
/*
send some data from 0xF230
*/
if res, err := b.sendReceive([]uint8{'F'}, 16); nil != err {
return err
} else {
fmt.Printf("res:%v\n", HexString(res))
}
// return some device REV[8] ,'+','L', 5 bytes from flash 0xF251
if res, err := b.sendReceive([]uint8{'M'}, 15); nil != err {
return err
} else {
fmt.Printf("res:%v\n", string(res))
}
/*
key Selection:
Cmd: "SEND" + DATA...
var idx int
if data[0] >= 0x20{
a := data[0] - 0x20
if a <= 4{
idx = 2 * a + 2
}else {
return -1
}
}else{
a := data[0] - 0x10
if a <= 4{
idx = 2 * a + 1
}else{
return -1
}
}
keyIndex := data[idx]
if keyIndex > 0x13{
return -1
}
*/
if res, err := b.sendReceive([]uint8{'S', 'E', 'N', 'D', 0x21, 0x05, 0x0D, 0x01, 0x01, 0x01, 0x04, 0x11, 0x08, 0x05, 0x0D, 0x0D, 0x01, 0x11, 0x0F, 0x09, 0x12, 0x09, 0x10, 0x04, 0x00}, 1); nil != err {
return err
} else {
fmt.Printf("res:%v\n", HexString(res))
}
return nil
}
func (b *BfIo) ReadBlock(addr uint16) ([]uint8, error) {
cmd := []uint8{'R', uint8(addr >> 8), uint8(addr), b.BlockSize}
if res, err := b.sendReceive(cmd, 4+int(b.BlockSize)); nil != err {
return res, err
} else {
if 0x52 != res[0] || uint8(addr>>8) != res[1] || uint8(addr) != res[2] {
return res[4:], errors.New("wrong read response")
}
// fmt.Printf("read[%04X]:%v\n", HexString(res))
return res[4:], nil
}
}
func (b *BfIo) WriteBlock(addr uint16, data []uint8) error {
cmd := make([]uint8, b.BlockSize+4)
cmd[0] = 'W'
cmd[1] = uint8(addr >> 8)
cmd[2] = uint8(addr)
cmd[3] = b.BlockSize
copy(cmd[4:], data)
if res, err := b.sendReceive(cmd, 1); nil != err {
return err
} else {
if 0x06 != res[0] {
return errors.New("wrong write response")
}
}
return nil
}
func (b *BfIo) ReadMemory(keyIndex int, addr uint16, count int) ([]uint8, error) {
res := make([]uint8, count)
for i := 0; i < count; i += int(b.BlockSize) {
buf, err := b.ReadBlock(addr)
if nil != err {
return res, err
}
if keyIndex >= 0 {
buf2 := b.Crypt(buf, keyIndex)
copy(res[i:], buf2)
} else {
copy(res[i:], buf)
}
addr += uint16(b.BlockSize)
}
return res, nil
}
func (b *BfIo) WriteMemory(keyIndex int, addr uint16, data []uint8) error {
for i := 0; i < len(data); i += int(b.BlockSize) {
var d []uint8
if keyIndex >= 0 {
d = b.Crypt(data[i:i+int(b.BlockSize)], keyIndex)
} else {
d = data[i : i+int(b.BlockSize)]
}
err := b.WriteBlock(addr, d)
if nil != err {
return err
}
addr += uint16(b.BlockSize)
}
return nil
}
func (b *BfIo) Crypt(data []uint8, index int) []uint8 {
res := make([]uint8, len(data))
keys := []uint8("BHT " + "CO 7" + "A ES" + " EIY" + "M PQ" + "XN Y" + "RVB " + " HQP" + "W RC" + "MS N" + " SAT" + "K DH" + "ZO R" + "C SL" + "6RB " + " JCG" + "PN V" + "J PK" + "EK L" + "I LZ")
key := keys[4*index : 4*index+4]
j := 0
for i := 0; i < len(data); i++ {
if 0x20 != key[j] && 0 != data[i] && 0xFF != data[i] && key[j] != data[i] && (key[j]^data[i]) != 0xff {
res[i] = data[i] ^ key[j]
} else {
res[i] = data[i]
}
j = (j + 1) & 0x3
}
return res
}
func main() {
initCmdline()
if "" == devName {
entries, err := os.ReadDir("/dev")
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
if strings.HasPrefix(e.Name(), "cu.usbserial") {
devName = "/dev/" + e.Name()
fmt.Printf("using %s\n", e.Name())
break
}
}
}
mode := &serial.Mode{
BaudRate: baudRate,
Parity: serial.NoParity,
DataBits: 8,
StopBits: serial.OneStopBit,
}
p, err := serial.Open(devName, mode)
if nil != err {
fmt.Printf("error %v\n", err)
return
}
p.ResetInputBuffer()
p.SetReadTimeout(time.Second)
bf := &BfIo{Port: p, BlockSize: 0x40}
if err := bf.initialComm(); nil != err {
fmt.Printf("error %v\n", err)
return
}
p.ResetInputBuffer()
// 0xF000, erase executed when addr is sector start (0xF000)
// no encryption is used for data from 0xF000 on
bd, err := bf.ReadMemory(-1, 0xF000, 0x1000)
if nil != err {
fmt.Printf("error %v %v\n", err, HexString(bd))
return
} else {
fmt.Printf("block data %x\n", bd[0x255])
}
if radioType >= 0 && radioType <= 9 && radioType != int(bd[0x255]-'0') {
bd[0x255] = '0' + uint8(radioType)
err = bf.WriteMemory(-1, 0xF000, bd)
if nil != err {
fmt.Printf("error %v\n", err)
return
}
}
d, err := bf.ReadBlock(0xF240)
if nil != err {
fmt.Printf("error %v %v\n", err, HexString(d))
return
}
fmt.Printf("Block Data %x\n", d[0x15])
}