-
Notifications
You must be signed in to change notification settings - Fork 12
/
common.go
194 lines (151 loc) · 3.85 KB
/
common.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
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package cmd
import (
"bytes"
"fmt"
"io"
"log"
"regexp"
"runtime"
"runtime/debug"
"runtime/pprof"
"sort"
"strconv"
"strings"
"time"
"golang.org/x/term"
"github.com/usbarmory/tamago/dma"
"github.com/hako/durafmt"
)
const testDiversifier = "\xde\xad\xbe\xef"
func init() {
Add(Cmd{
Name: "help",
Help: "this help",
Fn: helpCmd,
})
Add(Cmd{
Name: "exit, quit",
Args: 1,
Pattern: regexp.MustCompile(`^(exit|quit)$`),
Help: "close session",
Fn: exitCmd,
})
Add(Cmd{
Name: "stack",
Help: "goroutine stack trace (current)",
Fn: stackCmd,
})
Add(Cmd{
Name: "stackall",
Help: "goroutine stack trace (all)",
Fn: stackallCmd,
})
Add(Cmd{
Name: "dma",
Args: 1,
Pattern: regexp.MustCompile(`^dma (free|used)$`),
Help: "show allocation of default DMA region",
Syntax: "(free|used)",
Fn: dmaCmd,
})
Add(Cmd{
Name: "date",
Args: 1,
Pattern: regexp.MustCompile(`^date(.*)`),
Syntax: "(time in RFC339 format)?",
Help: "show/change runtime date and time",
Fn: dateCmd,
})
Add(Cmd{
Name: "uptime",
Help: "show how long the system has been running",
Fn: uptimeCmd,
})
// The following commands are board specific, therefore their Fn
// pointers are defined elsewhere in the respective target files.
Add(Cmd{
Name: "info",
Help: "device information",
Fn: infoCmd,
})
Add(Cmd{
Name: "reboot",
Help: "reset device",
Fn: rebootCmd,
})
}
func helpCmd(_ *Interface, term *term.Terminal, _ []string) (string, error) {
return Help(term), nil
}
func exitCmd(_ *Interface, term *term.Terminal, _ []string) (string, error) {
fmt.Fprintf(term, "Goodbye from %s/%s\n", runtime.GOOS, runtime.GOARCH)
return "logout", io.EOF
}
func stackCmd(_ *Interface, _ *term.Terminal, _ []string) (string, error) {
return string(debug.Stack()), nil
}
func stackallCmd(_ *Interface, _ *term.Terminal, _ []string) (string, error) {
buf := new(bytes.Buffer)
pprof.Lookup("goroutine").WriteTo(buf, 1)
return buf.String(), nil
}
func dmaCmd(_ *Interface, term *term.Terminal, arg []string) (string, error) {
var res []string
var l map[uint]uint
var t uint
switch arg[0] {
case "free":
l = dma.Default().FreeBlocks()
case "used":
l = dma.Default().UsedBlocks()
}
for addr, n := range l {
t += n
res = append(res, fmt.Sprintf("%#08x-%#08x %d", addr, addr+n, n))
}
sort.Strings(res)
res = append(res, fmt.Sprintf("%s %d total bytes", strings.Repeat(" ", 21), t))
return strings.Join(res, "\n"), nil
}
func dateCmd(_ *Interface, _ *term.Terminal, arg []string) (res string, err error) {
if len(arg[0]) > 1 {
t, err := time.Parse(time.RFC3339, arg[0][1:])
if err != nil {
return "", err
}
date(t.UnixNano())
}
return fmt.Sprintf("%s", time.Now().Format(time.RFC3339)), nil
}
func uptimeCmd(_ *Interface, term *term.Terminal, _ []string) (string, error) {
return fmt.Sprintf("%s", durafmt.Parse(time.Duration(uptime())*time.Nanosecond)), nil
}
func cipherCmd(arg []string, tag string, fn func(buf []byte) (string, error)) (res string, err error) {
size, err := strconv.Atoi(arg[0])
if err != nil {
return "", fmt.Errorf("invalid size, %v", err)
}
sec, err := strconv.Atoi(arg[1])
if err != nil {
return "", fmt.Errorf("invalid duration, %v", err)
}
log.Printf("Doing %s for %ds on %d size blocks", tag, sec, size)
n := 0
buf := make([]byte, size)
start := time.Now()
duration := time.Duration(sec) * time.Second
for time.Since(start) < duration {
if _, err = fn(buf); err != nil {
return
}
n++
}
elapsed := time.Since(start)
kbps := (n * size) / int(elapsed/time.Millisecond)
return fmt.Sprintf("%d %s's in %s (%dk)", n, tag, time.Since(start), kbps), nil
}