Skip to content

Commit 04bfc27

Browse files
authored
introduce 'explain' function for tpm-tool (#3)
1 parent 7e09116 commit 04bfc27

File tree

3 files changed

+574
-0
lines changed

3 files changed

+574
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ go build cmd/tpm-top
4545
* `extend <index> <file>`
4646
* Extends the contents of `<file>` into PCR `<index>` in all active PCR banks.
4747
* `<file>` must be 1KB or smaller.
48+
* `explain`
49+
* Formats a TPM 2.0 error code and prints out the explanation.
4850

4951
## Starting the simulator
5052
tpm-top currently only connects to a running TCP simulator, even if there is a

cmd/tpm-tool/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/chrisfenner/tpm-top/internal/opener"
66
"github.com/chrisfenner/tpm-top/internal/pcr-allocate"
7+
"github.com/chrisfenner/tpm-top/internal/rc"
78
"github.com/google/go-tpm/tpm2"
89
"github.com/google/go-tpm/tpmutil"
910
"io"
@@ -21,6 +22,12 @@ var funcMap = map[string]toolFunc{
2122
"extend": extend,
2223
}
2324

25+
type toolFuncNoTpm func([]string) int
26+
27+
var funcMapNoTpm = map[string]toolFuncNoTpm{
28+
"explain": explain,
29+
}
30+
2431
func startup(tpm io.ReadWriter, args []string) int {
2532
if len(args) != 0 {
2633
fmt.Fprintf(os.Stderr, "'startup' command expects 0 arguments")
@@ -133,12 +140,34 @@ func extend(tpm io.ReadWriter, args []string) int {
133140
return 0
134141
}
135142

143+
func explain(args []string) int {
144+
if len(args) != 1 {
145+
fmt.Fprintf(os.Stderr, "'explain' command expects 1 argument: an error code\n")
146+
return 1
147+
}
148+
code, err := strconv.ParseInt(args[0], 0, 32)
149+
if err != nil {
150+
fmt.Fprintf(os.Stderr, "could not parse error code: %v\n", err)
151+
return 1
152+
}
153+
err = rc.MakeError(int(code))
154+
if err == nil {
155+
fmt.Printf("RC_SUCCESS! 😎\n")
156+
} else {
157+
fmt.Printf("%s\n", err)
158+
}
159+
return 0
160+
}
161+
136162
func usage() {
137163
fmt.Printf("tpm-tool usage: tpm-tool (function) [(arguments)]\n")
138164
fmt.Printf("Supported functions:\n")
139165
for name, _ := range funcMap {
140166
fmt.Printf(" %s\n", name)
141167
}
168+
for name, _ := range funcMapNoTpm {
169+
fmt.Printf(" %s\n", name)
170+
}
142171
}
143172

144173
func mainWithExitCode() int {
@@ -148,6 +177,10 @@ func mainWithExitCode() int {
148177
return 1
149178
}
150179
cmd := os.Args[1]
180+
funNoTpm, ok := funcMapNoTpm[cmd]
181+
if ok {
182+
return funNoTpm(os.Args[2:])
183+
}
151184
fun, ok := funcMap[cmd]
152185
if !ok {
153186
fmt.Fprintf(os.Stderr, "Unsupported command '%s'\n", cmd)

0 commit comments

Comments
 (0)