Skip to content

Commit 904ec1e

Browse files
authored
Allow fetching recent logs from the OS over HID (#212)
1 parent 38e4523 commit 904ec1e

File tree

9 files changed

+49
-3
lines changed

9 files changed

+49
-3
lines changed

api/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const (
4545
U2FHID_ARMORY_OTA
4646
// Set HAB fuse to built-in SRK hash
4747
U2FHID_ARMORY_HAB
48+
// Fetch latest debug/console logs
49+
U2FHID_ARMORY_CONSOLE_LOGS
4850
)
4951

5052
var emptyResponse []byte

cmd/witnessctl/api.go

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ func (d Device) hab() error {
7878
return nil
7979
}
8080

81+
func (d Device) consoleLogs() (string, error) {
82+
buf, err := d.u2f.Command(api.U2FHID_ARMORY_CONSOLE_LOGS, nil)
83+
if err != nil {
84+
return "", err
85+
}
86+
87+
return string(buf), nil
88+
}
89+
8190
func (d Device) sendUpdateHeader(signature []byte, total int) (err error) {
8291
update := &api.AppletUpdate{
8392
Total: uint32(total),

cmd/witnessctl/witnessctl.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ type Config struct {
5555

5656
hidPath string
5757

58-
status bool
59-
hab bool
58+
status bool
59+
consoleLogs bool
60+
hab bool
6061

6162
otaELF string
6263
otaSig string
@@ -79,6 +80,7 @@ func init() {
7980

8081
flag.StringVar(&conf.hidPath, "d", "", "HID path of witness device to act upon (use -s to list devices)")
8182
flag.BoolVar(&conf.status, "s", false, "get witness status")
83+
flag.BoolVar(&conf.consoleLogs, "l", false, "get witness console/debug logs")
8284
flag.BoolVar(&conf.hab, "H", false, "set HAB fuses")
8385
flag.StringVar(&conf.otaELF, "o", "", "trusted applet payload")
8486
flag.StringVar(&conf.otaSig, "O", "", "trusted applet signature")
@@ -160,6 +162,15 @@ func main() {
160162
}
161163
log.Printf("%s\n\n", s.Print())
162164
}
165+
case conf.consoleLogs:
166+
for _, d := range conf.devs {
167+
log.Printf("👁️‍🗨️ @ %s", d.usb.Path)
168+
s, err := d.consoleLogs()
169+
if err != nil {
170+
log.Printf("Failed to get console logs on %q: %c", d.usb.Path, err)
171+
}
172+
log.Printf("%s\n\n", s)
173+
}
163174
case len(conf.otaELF) > 0 || len(conf.otaSig) > 0:
164175
if len(conf.devs) != 1 {
165176
log.Fatal("Please specify which device to OTA using -d")

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/mattn/go-runewidth v0.0.15 // indirect
3737
github.com/pierrec/lz4/v4 v4.1.14 // indirect
3838
github.com/rivo/uniseg v0.2.0 // indirect
39+
github.com/smallnest/ringbuffer v0.0.0-20230728150354-35801fa39d0e // indirect
3940
github.com/stretchr/testify v1.8.2 // indirect
4041
github.com/transparency-dev/formats v0.0.0-20230920083814-0f75b1d4e813 // indirect
4142
github.com/u-root/u-root v0.11.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
4747
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
4848
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
4949
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
50+
github.com/smallnest/ringbuffer v0.0.0-20230728150354-35801fa39d0e h1:KHiRfgBfn0d3lv2kXs4iayASb6TdInNNIHe75zX0sqg=
51+
github.com/smallnest/ringbuffer v0.0.0-20230728150354-35801fa39d0e/go.mod h1:mXcZNMJHswhQDDJZIjdtJoG97JIwIa/HdcHNM3w15T0=
5052
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5153
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
5254
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

trusted_os/console.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func printk(c byte) {
5151
// ensure that any serial output is supressed before UART2 disabling
5252
}
5353

54+
func getConsoleLogs() []byte {
55+
return nil
56+
}
57+
5458
func inspect(buf []byte, _ any) error {
5559
return nil
5660
}

trusted_os/ctl.go

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func (ctl *controlInterface) HAB(_ []byte) []byte {
154154
return api.EmptyResponse()
155155
}
156156

157+
func (ctl *controlInterface) ConsoleLogs(_ []byte) (res []byte) {
158+
logs := getConsoleLogs()
159+
return []byte(logs)
160+
}
161+
157162
func (ctl *controlInterface) Start() {
158163
device := &usb.Device{}
159164
serial := fmt.Sprintf("%X", imx6ul.UniqueID())

trusted_os/debug.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ import (
3030
usbarmory "github.com/usbarmory/tamago/board/usbarmory/mk2"
3131
"github.com/usbarmory/tamago/soc/nxp/usb"
3232

33+
"github.com/usbarmory/GoTEE/monitor"
3334
usbserial "github.com/usbarmory/imx-usbserial"
3435

35-
"github.com/usbarmory/GoTEE/monitor"
36+
"github.com/smallnest/ringbuffer"
3637
)
3738

3839
const debug = true
3940

4041
var serial *usbserial.UART
42+
var logBuffer *ringbuffer.RingBuffer
4143

4244
func init() {
4345
// TODO(al): Probably want to reinstate this check after wave0!
@@ -46,17 +48,23 @@ func init() {
4648
panic("fatal error, debug firmware not allowed on secure booted units")
4749
}
4850
*/
51+
logBuffer = ringbuffer.New(1 << 20)
4952
}
5053

5154
//go:linkname printk runtime.printk
5255
func printk(c byte) {
5356
usbarmory.UART2.Tx(c)
57+
logBuffer.WriteByte(c)
5458

5559
if serial != nil {
5660
serial.WriteByte(c)
5761
}
5862
}
5963

64+
func getConsoleLogs() []byte {
65+
return logBuffer.Bytes()
66+
}
67+
6068
func configureUART(device *usb.Device) (err error) {
6169
if LAN == nil {
6270
return

trusted_os/usb_hid.go

+4
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,9 @@ func configureHID(device *usb.Device, ctl *controlInterface) (err error) {
121121
return
122122
}
123123

124+
if err = hid.AddMapping(api.U2FHID_ARMORY_CONSOLE_LOGS, ctl.ConsoleLogs); err != nil {
125+
return
126+
}
127+
124128
return
125129
}

0 commit comments

Comments
 (0)