-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
367 lines (340 loc) · 9.4 KB
/
main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2017 OpenChirp. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// March 13, 2017
// Craig Hesling <craig@hesling.com>
package main
import (
"flag"
"fmt"
"io"
"os/signal"
log "github.com/sirupsen/logrus"
"os"
"strconv"
"github.com/jacobsa/go-serial/serial"
"github.com/openchirp/ccboot"
)
const (
// This is the default speed that the SmartRf Flash Programmer 2 uses
SmartRFFlashProgrammer2Speed = 460800
)
const (
// timeout in ms
readTimeout = 500
)
var commands = []string{
"sync",
"ping",
"download <address_with_0x_prefix> <size>",
"getstatus",
"getchipid",
"bankerase",
"memoryread <address_with_0x_prefix> <access_type_as_8_or_32> <count>",
"reset",
"setccfg <field_id1> <field_value1> [<field_id2> <field_value2> [...]]",
"flash <program.elf>",
"verify <program.elf>",
"prgm <program.elf> -- sync, erase, flash, verify, and then reset",
"console",
}
var verbose bool
var portSpeed uint
func setupFlags() {
flag.Usage = func() {
fmt.Printf("Usage: ccbootutil [options] <portname> <command> [parameters]\n\n")
fmt.Printf("Commands:\n")
for _, cmd := range commands {
fmt.Printf("\t%s\n", cmd)
}
fmt.Printf("\n")
fmt.Printf("Options:\n")
flag.PrintDefaults()
}
flag.BoolVar(&verbose, "v", false, "Toggles the verbose setting")
flag.UintVar(&portSpeed, "speed", SmartRFFlashProgrammer2Speed, "The serial baud rate to use")
}
func main() {
setupFlags()
flag.Parse()
if len(flag.Args()) < 2 {
flag.Usage()
os.Exit(1)
}
args := flag.Args()[:]
portName := args[0]
cmd := args[1]
args = args[2:]
// Set up options.
options := serial.OpenOptions{
PortName: portName,
BaudRate: portSpeed,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 0,
InterCharacterTimeout: readTimeout,
}
log.SetOutput(os.Stderr)
if !verbose {
log.SetLevel(log.ErrorLevel)
}
// Open the port.
log.WithField("dev", portName).WithField("speed", portSpeed).Println("Opening Serial")
port, err := serial.Open(options)
if err != nil {
log.Fatalf("Failed to open serial port: %v", err)
}
// Make sure to close it later.
defer port.Close()
d := ccboot.NewDevice(port)
switch cmd {
case "sync":
// Synchronize
log.Println("Synchronizing")
if err = d.Sync(); err != nil {
log.Fatalf("Error synchronizing device: %v\n", err)
}
log.Println("Synchronization success")
case "ping":
// Ping Device Bootloader
log.Println("Pinging")
err = d.Ping()
if err != nil {
log.Fatalf("Error pinging device: %v\n", err)
}
log.Println("Ping success")
case "download":
// Send Download command
log.Println("Downloading")
if len(args) < 2 {
log.Fatalf("Error - does not specify address and size")
}
// 0 as base allows inputting 0x or decimal value
addr, err := strconv.ParseUint(args[0], 0, 32)
if err != nil {
log.Fatalf("Error parsing address: %v\n", err)
}
size, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
log.Fatalf("Error parsing size: %v\n", err)
}
log.Printf("Sending Download command with 0x%x and %d\n", uint32(addr), uint32(size))
err = d.Download(uint32(addr), uint32(size))
if err != nil {
log.Fatalf("Error sending download todevice: %v\n", err)
}
log.Println("Download success")
case "getstatus":
// Get Status
log.Println("Getting status")
status, err := d.GetStatus()
if err != nil {
fmt.Println() // maintain parsibility
log.Fatalf("# Error - %v\n", err)
}
fmt.Println(status)
case "getchipid":
// Get Chip ID
log.Println("Getting chip ID")
id, err := d.GetChipID()
if err != nil {
log.Fatalf("Error reading chip id: %v\n", err)
}
fmt.Printf("0x%.8X\n", id)
case "bankerase":
// Bank Erase
log.Println("Bank erasing")
err = d.BankErase()
if err != nil {
log.Fatalf("Error - Could not bank erase device: %v\n", err)
}
log.Println("Bank erase success")
case "memoryread":
// Memory Read
log.Println("Memory read")
if len(args) != 3 {
log.Fatalf("Error - Parameters for memory read should be <address_with_0x_prefix> <access_type_as_8_or_32> <count>")
}
// 0 as base allows inputting 0x or decimal value
addr, err := strconv.ParseUint(args[0], 0, 32)
if err != nil {
log.Fatalf("Error parsing address: %v\n", err)
}
atype, err := strconv.ParseUint(args[1], 10, 8)
if err != nil {
log.Fatalf("Error parsing access type: %v\n", err)
}
typ := ccboot.ReadWriteType8Bit
if atype == 8 {
typ = ccboot.ReadWriteType8Bit
} else if atype == 32 {
typ = ccboot.ReadWriteType32Bit
} else {
log.Fatalf("Invalid access type \"%d\". Must be 8 or 32.\n", atype)
}
count, err := strconv.ParseUint(args[2], 10, 8)
if err != nil {
log.Fatalf("Error parsing count: %v\n", err)
}
log.Printf("Reading %d %v word(s) from address 0x%X\n", count, typ, uint32(addr))
data, err := d.MemoryRead(uint32(addr), typ, uint8(count))
if err != nil {
log.Fatalf("Error - Could not read memory: %v\n", err)
}
log.Println("Memory read success")
n, err := os.Stdout.Write(data)
if err != nil {
log.Fatalf("Error writing data to stdout: %v\n", err)
}
if n != len(data) {
log.Fatalf("Error - Size of data received was not fully written to stdout\n")
}
log.Println("Memory stdout dump success")
case "reset":
// Reset Device
log.Println("Resetting device")
err = d.Reset()
if err != nil {
log.Fatalf("Error - Could not reset chip: %v\n", err)
}
log.Println("Device reset")
/*
* CCFG is just another part of the flash space.
* Note that flash bits can only be zeroed out without the help of an erase cycle.
* So, this setccfg can only mask bits that were previously a 1.
*/
case "setccfg":
// Set CCFGs
if (len(args) < 2) || (len(args)%2 != 0) {
log.Fatalf("Error - Parameters for CCFG should specify <CCFG_FIELD_ID> followed by <value>")
}
for i := 0; i < len(args)/2; i++ {
fieldid, err := ccboot.ParseCCFGFieldID(args[i*2])
if err != nil {
log.Fatalf("Error - Could not parse Field ID %s\n", args[i*2])
}
// base=0 means it will try to auto detect base
fieldvalue, err := strconv.ParseUint(args[i*2+1], 0, 32)
if err != nil {
log.Fatalf("Error - Could not parse Field Value %s\n", args[i*2+1])
}
log.Printf("Setting CCFG %v to 0x%X (%d)", fieldid, uint32(fieldvalue), uint32(fieldvalue))
err = d.SetCCFG(fieldid, uint32(fieldvalue))
if err != nil {
log.Fatalf("Error - Could not set CCFG %v to 0x%X: %v\n", fieldid, uint32(fieldvalue), err)
}
}
log.Println("Device CCFG set")
case "flash":
log.Println("Flashing device")
if len(args) != 1 {
fmt.Println("FAILURE")
log.Fatalf("Error - No ELF binary specified")
}
if err := flash(d, args[0]); err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error - Failed to flash: %v\n", err)
}
fmt.Println("SUCCESS")
case "verify":
rcount := uint64(0)
log.Println("Verifying device image")
if len(args) != 1 {
fmt.Println("FAILURE")
log.Fatalf("Error - No ELF binary specified")
}
// TODO: Fix rcount > 0
// if len(args) == 2 {
// rcount, err = strconv.ParseUint(args[1], 0, 32)
// if err != nil {
// log.Fatalf("Error - Failed to parse read cycle count %s: %v\n", args[1], err)
// }
// }
pass, err := verify(d, args[0], uint32(rcount))
if err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error - Failed to verify: %v\n", err)
}
if pass {
fmt.Println("SUCCESS")
} else {
fmt.Println("FAILURE")
}
case "program":
fallthrough
case "prgm":
log.Println("Programming device")
// Ensure required ELF file argument
if len(args) != 1 {
fmt.Println("FAILURE")
log.Fatalf("Error - No ELF binary specified")
}
// Check for active sync first
log.Println("Ping")
if err = d.Ping(); err != nil {
// Must need to Synchronize
log.Println("Falling back to Synchronizing")
if err = d.Sync(); err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error synchronizing device: %v\n", err)
}
log.Println("Synchronization success")
} else {
log.Println("Ping successful")
}
// Flash
log.Println("Flashing device")
if err := flash(d, args[0]); err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error - Failed to flash: %v\n", err)
}
// Verify
log.Println("Verifying device image")
rcount := uint64(0)
pass, err := verify(d, args[0], uint32(rcount))
if err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error - Failed to verify: %v\n", err)
}
if !pass {
fmt.Println("FAILURE")
os.Exit(1)
}
// Reset Device
log.Println("Resetting device")
err = d.Reset()
if err != nil {
fmt.Println("FAILURE")
log.Fatalf("Error - Could not reset chip: %v\n", err)
}
log.Println("Device reset")
fmt.Println("SUCCESS")
case "console":
log.Println("Opening console to device")
go func() {
logitem := log.WithField("copy direction", "from dev")
for {
// Will loop because of read timeout
if _, err := io.Copy(os.Stdout, port); err != nil {
logitem.Fatal("Encountered error: ", err)
}
}
}()
go func() {
logitem := log.WithField("copy direction", "to dev")
if _, err := io.Copy(port, os.Stdin); err != nil {
logitem.Fatal("Encountered error: ", err)
}
// If user sends EOF (CTRL-D)
logitem.Println("Closed stdin")
}()
// Wait for sigint
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
default:
log.Fatalf("Error - Invalid command given")
}
log.Printf("Exiting\n")
}