-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
129 lines (98 loc) · 2.55 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
package main
import (
"crypto/rand"
"encoding/hex"
"log"
"runtime"
"time"
pi "github.com/f-secure-foundry/tamago/board/raspberrypi"
"github.com/f-secure-foundry/tamago/board/raspberrypi/pi2"
)
func rng() {
log.Println("-- rng -------------------------------------------------------------")
c := 10
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
log.Printf("random bytes %s", hex.EncodeToString(b))
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))
}
func timer() {
log.Println("-- timer -------------------------------------------------------------")
t := time.NewTimer(time.Second)
log.Printf("waking up timer after %v", time.Second)
start := time.Now()
for now := range t.C {
log.Printf("woke up at %d (%v)", now.Nanosecond(), now.Sub(start))
break
}
}
func ram() {
log.Println("-- RAM ---------------------------------------------------------------")
// Check GC is working by forcing more total allocation than available
allocateAndWipe(700)
runtime.GC()
allocateAndWipe(700)
}
func watchdog() {
log.Println("-- watchdog ----------------------------------------------------------")
log.Println("Starting watchdog at 1s")
// Auto-reset after 1 sec
pi.Watchdog.Start(time.Second)
time.Sleep(600 * time.Millisecond)
log.Printf("Watchdog Remaining after 600ms: %v, resetting", pi.Watchdog.Remaining())
pi.Watchdog.Reset()
time.Sleep(600 * time.Millisecond)
log.Printf("Watchdog Remaining after 600ms: %v", pi.Watchdog.Remaining())
pi.Watchdog.Stop()
log.Print("Watchdog stopped, waiting for 2 sec")
time.Sleep(2 * time.Second)
}
func main() {
log.Println("Hello World!")
rng()
timer()
ram()
watchdog()
log.Println("-- LED ---------------------------------------------------------------")
log.Println("Flashing the LEDs")
board := pi2.Board
ledOn := false
for {
time.Sleep(250 * time.Millisecond)
ledOn = !ledOn
board.LED("activity", ledOn)
board.LED("power", !ledOn)
}
}
func allocateAndWipe(count int) {
log.Printf("allocating %dMB", count)
hold := make([][]byte, 0, count)
for i := 0; i < cap(hold); i++ {
mem := make([]byte, 1024*1024)
if len(mem) == 0 {
break
}
hold = append(hold, mem)
}
log.Println("wiping allocation with 0xff")
for i := 0; i < len(hold); i++ {
for j := range hold[i] {
hold[i][j] = 0xff
}
}
}