/
main.go
133 lines (102 loc) · 3.13 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
// 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 main
import (
_ "embed"
"fmt"
"log"
"os"
"runtime"
"time"
_ "unsafe"
usbarmory "github.com/usbarmory/tamago/board/usbarmory/mk2"
"github.com/usbarmory/tamago/dma"
"github.com/usbarmory/tamago/soc/nxp/imx6ul"
"github.com/usbarmory/imx-usbnet"
"github.com/usbarmory/GoTEE-example/internal/semihosting"
"github.com/usbarmory/GoTEE-example/mem"
"github.com/usbarmory/GoTEE-example/util"
"github.com/usbarmory/GoTEE-example/trusted_os_usbarmory/cmd"
"github.com/usbarmory/GoTEE-example/trusted_os_usbarmory/internal"
)
const (
sshPort = 22
IP = "10.0.0.1"
MAC = "1a:55:89:a2:69:41"
hostMAC = "1a:55:89:a2:69:42"
)
// This example embeds the Trusted Applet and Main OS ELF binaries within the
// Trusted OS executable, using Go embed package.
//
// The loading strategy is up to implementers, on the NXP i.MX6 the armory-boot
// bootloader primitives can be used to create a bootable Trusted OS with
// authenticated disk loading of applets and kernels, see loadLinux() and:
// https://pkg.go.dev/github.com/usbarmory/armory-boot
//go:embed assets/trusted_applet.elf
var taELF []byte
//go:embed assets/nonsecure_os_go.elf
var osELF []byte
//go:linkname ramStart runtime.ramStart
var ramStart uint32 = mem.SecureStart
//go:linkname ramSize runtime.ramSize
var ramSize uint32 = mem.SecureSize
func init() {
log.SetFlags(log.Ltime)
log.SetOutput(os.Stdout)
mem.Init()
// Move DMA region to prevent NonSecure access, alternatively
// iRAM/OCRAM (default DMA region) can be locked down on its own (as it
// is outside TZASC control).
dma.Init(mem.SecureDMAStart, mem.SecureDMASize)
if imx6ul.Native {
switch imx6ul.Model() {
case "i.MX6UL":
imx6ul.SetARMFreq(imx6ul.Freq528)
imx6ul.CAAM.DeriveKeyMemory = dma.Default()
case "i.MX6ULL", "i.MX6ULZ":
imx6ul.SetARMFreq(imx6ul.FreqMax)
imx6ul.DCP.Init()
imx6ul.DCP.DeriveKeyMemory = dma.Default()
}
debugConsole, _ := usbarmory.DetectDebugAccessory(250 * time.Millisecond)
<-debugConsole
}
cmd.Banner = fmt.Sprintf("%s/%s (%s) • TEE security monitor (Secure World system/monitor)", runtime.GOOS, runtime.GOARCH, runtime.Version())
gotee.TA = taELF
gotee.OS = osELF
}
func serialConsole() {
gotee.Console = util.NewScreenConsole()
cmd.SerialConsole(usbarmory.UART2)
log.Printf("SM says goodbye")
semihosting.Exit()
}
func main() {
if !imx6ul.Native {
serialConsole()
}
iface, err := usbnet.Init(IP, MAC, hostMAC, 1)
if err != nil {
log.Fatalf("SM could not initialize USB networking, %v", err)
}
iface.EnableICMP()
listener, err := iface.ListenerTCP4(sshPort)
if err != nil {
log.Fatalf("SM could not initialize SSH listener, %v", err)
}
gotee.Console = &util.Console{
Handler: cmd.Handler,
Listener: listener,
}
if err = gotee.Console.Start(); err != nil {
log.Fatalf("SM could not initialize SSH server, %v", err)
}
usbarmory.USB1.Init()
usbarmory.USB1.DeviceMode()
usbarmory.USB1.Reset()
// never returns
usbarmory.USB1.Start(iface.NIC.Device)
}