forked from usbarmory/tamago-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
212 lines (159 loc) · 4.46 KB
/
example.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
// https://github.com/usbarmory/tamago-example
//
// 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.
// Basic test example for tamago/arm running on supported i.MX6 targets.
package main
import (
"crypto/rand"
"fmt"
"io"
"log"
"math"
"math/big"
mathrand "math/rand"
"os"
"runtime"
"time"
"github.com/usbarmory/tamago/soc/imx6"
)
var Build string
var Revision string
var banner string
var exit chan bool
var logFile *os.File
func init() {
log.SetFlags(0)
banner = fmt.Sprintf("%s/%s (%s) • %s %s",
runtime.GOOS, runtime.GOARCH, runtime.Version(),
Revision, Build)
model := imx6.Model()
if !imx6.Native {
banner += fmt.Sprintf(" • %s %d MHz (emulated)", model, imx6.ARMFreq()/1000000)
return
}
if err := imx6.SetARMFreq(900); err != nil {
log.Printf("WARNING: error setting ARM frequency: %v", err)
}
banner += fmt.Sprintf(" • %s %d MHz", model, imx6.ARMFreq()/1000000)
}
func test(init bool) {
start := time.Now()
exit = make(chan bool)
n := 0
log.Println("-- begin tests -------------------------------------------------------")
n += 1
go func() {
log.Println("-- fs ----------------------------------------------------------------")
TestDev()
TestFile()
exit <- true
}()
sleep := 100 * time.Millisecond
n += 1
go func() {
log.Println("-- timer -------------------------------------------------------------")
t := time.NewTimer(sleep)
log.Printf("waking up timer after %v", sleep)
start := time.Now()
for now := range t.C {
log.Printf("woke up at %d (%v)", now.Nanosecond(), now.Sub(start))
break
}
exit <- true
}()
n += 1
go func() {
log.Println("-- sleep -------------------------------------------------------------")
log.Printf("sleeping %s", sleep)
start := time.Now()
time.Sleep(sleep)
log.Printf("slept %s (%v)", sleep, time.Since(start))
exit <- true
}()
n += 1
go func() {
log.Println("-- rng ---------------------------------------------------------------")
size := 32
for i := 0; i < 10; i++ {
rng := make([]byte, size)
rand.Read(rng)
log.Printf("%x", rng)
}
count := 1000
start := time.Now()
for i := 0; i < count; i++ {
rng := make([]byte, size)
rand.Read(rng)
}
log.Printf("retrieved %d random bytes in %s", size*count, time.Since(start))
seed, _ := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
mathrand.Seed(seed.Int64())
exit <- true
}()
n += 1
go func() {
log.Println("-- ecdsa -------------------------------------------------------------")
TestSignAndVerify()
exit <- true
}()
n += 1
go func() {
log.Println("-- btc ---------------------------------------------------------------")
ExamplePayToAddrScript()
ExampleExtractPkScriptAddrs()
ExampleSignTxOutput()
exit <- true
}()
if imx6.Native && imx6.Family == imx6.IMX6ULL {
n += 1
go func() {
log.Println("-- i.mx6 dcp ---------------------------------------------------------")
TestDCP()
exit <- true
}()
}
log.Printf("launched %d test goroutines", n)
for i := 1; i <= n; i++ {
<-exit
}
log.Printf("----------------------------------------------------------------------")
log.Printf("completed %d goroutines (%s)", n, time.Since(start))
runs := 9
chunksMax := 50
chunks := mathrand.Intn(chunksMax) + 1
fillSize := 160 * 1024 * 1024
chunkSize := fillSize / chunks
log.Printf("-- memory allocation (%d runs) ----------------------------------------", runs)
testAlloc(runs, chunks, chunkSize)
if imx6.Native {
size := 10 * 1024 * 1024
readSize := 0x7fff
if init {
// We can use the entire iRAM before USB activation,
// accounting for required dTD alignment which takes
// additional space.
readSize = 0x20000 - 4096
}
log.Println("-- memory cards -------------------------------------------------------")
for _, card := range cards {
TestUSDHC(card, size, readSize)
}
}
}
func main() {
start := time.Now()
logFile, _ = os.OpenFile("/tamago-example.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
log.Println(banner)
if imx6.Native && (imx6.Family == imx6.IMX6UL || imx6.Family == imx6.IMX6ULL) {
log.Println("-- i.mx6 usb ---------------------------------------------------------")
startNetworking()
}
// on emulated runs start test sequence automatically
test(true)
log.Printf("Goodbye from tamago/arm (%s)", time.Since(start))
}