-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
149 lines (114 loc) · 2.89 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
// Copyright (c) WithSecure Corporation
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"runtime"
"time"
"github.com/usbarmory/armory-drive/internal/ble"
"github.com/usbarmory/armory-drive/internal/crypto"
"github.com/usbarmory/armory-drive/internal/hab"
"github.com/usbarmory/armory-drive/internal/ums"
"github.com/usbarmory/tamago/arm"
"github.com/usbarmory/tamago/soc/nxp/imx6ul"
"github.com/usbarmory/tamago/soc/nxp/usb"
usbarmory "github.com/usbarmory/tamago/board/usbarmory/mk2"
)
func init() {
if err := imx6ul.SetARMFreq(900); err != nil {
panic(fmt.Sprintf("WARNING: error setting ARM frequency: %v\n", err))
}
imx6ul.DCP.Init()
imx6ul.DCP.EnableInterrupt()
log.SetFlags(0)
}
func startInterruptHandler(port *usb.USB) {
irq := imx6ul.GIC.GetInterrupt(true)
imx6ul.GIC.EnableInterrupt(port.IRQ, true)
imx6ul.GIC.EnableInterrupt(imx6ul.DCP.IRQ, true)
isr := func() {
switch irq {
case port.IRQ:
port.ServiceInterrupts()
case imx6ul.DCP.IRQ:
imx6ul.DCP.ServiceInterrupt()
default:
log.Printf("internal error, unexpected IRQ %d", irq)
}
}
arm.ServiceInterrupts(isr)
}
func main() {
usbarmory.LED("blue", false)
usbarmory.LED("white", false)
if err := usbarmory.MMC.Detect(); err != nil {
log.Fatal(err)
}
keyring := &crypto.Keyring{}
if err := keyring.Init(false); err != nil {
log.Fatal(err)
}
drive := &ums.Drive{
Cipher: true,
Keyring: keyring,
Mult: ums.BLOCK_SIZE_MULTIPLIER,
}
ble := &ble.BLE{
Drive: drive,
Keyring: keyring,
}
ble.Init()
if drive.Init(usbarmory.SD) != nil {
var code []byte
var err error
// provision Secure Boot as required
hab.Init()
// Do not offer pairing code on first time installs (or
// recovery) as that pairing might become invalid at reboot if
// Secure Boot has been just activated, rather offer pairing
// only by firmware booted internally.
if !imx6ul.SDP {
code, err = ble.PairingMode()
if err != nil {
log.Fatal(err)
}
}
drive.Cipher = false
drive.Mult = 1
drive.Ready = true
drive.Init(ums.Pairing(code, keyring))
go pairingFeedback(drive.PairingComplete)
}
port := imx6ul.USB1
port.Device = drive.ConfigureUSB()
port.Init()
// To further reduce the attack surface, start the USB stack only when
// the card is unlocked (or in pairing mode).
for !drive.Ready {
runtime.Gosched()
time.Sleep(10 * time.Millisecond)
}
port.DeviceMode()
port.EnableInterrupt(usb.IRQ_URI) // reset
port.EnableInterrupt(usb.IRQ_PCI) // port change detect
port.EnableInterrupt(usb.IRQ_UI) // transfer completion
startInterruptHandler(port)
}
func pairingFeedback(done chan bool) {
var on bool
for {
select {
case <-done:
usbarmory.LED("blue", false)
return
default:
}
on = !on
usbarmory.LED("blue", on)
runtime.Gosched()
time.Sleep(1 * time.Second)
}
}