Debian Codegraph @

examples/tpm2-nvread/main.go

raw
1 //go:build !windows
2
3 // Binary tpm2-nvread reads data from NVRAM at a specified index. The data is
4 // printed out hex-encoded.
5 package main
6
7 import (
8 "flag"
9 "fmt"
10 "os"
11
12 "github.com/google/go-tpm/legacy/tpm2"
13 "github.com/google/go-tpm/tpmutil"
14 )
15
16 var (
17 tpmPath = flag.String("tpm-path", "/dev/tpm0", "Path to the TPM device (character device or a Unix socket)")
18 index = flag.Uint("index", 0, "NVRAM index of read")
19 )
20
21 func main() {
22 flag.Parse()
23
24 if *index == 0 {
25 fmt.Fprintln(os.Stderr, "--index must be set")
26 os.Exit(1)
27 }
28
29 val, err := nvRead(*tpmPath, uint32(*index))
30 if err != nil {
31 fmt.Fprintf(os.Stderr, "reading from index 0x%x: %v\n", *index, err)
32 os.Exit(1)
33 }
34 fmt.Printf("NVRAM value at index 0x%x (hex encoded):\n%x\n", *index, val)
35 }
36
37 func nvRead(path string, index uint32) ([]byte, error) {
38 rwc, err := tpm2.OpenTPM(path)
39 if err != nil {
40 return nil, fmt.Errorf("can't open TPM at %q: %v", path, err)
41 }
42 defer rwc.Close()
43 return tpm2.NVRead(rwc, tpmutil.Handle(index))
44 }