File: main.go

package info (click to toggle)
golang-github-google-go-tpm 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 13
file content (44 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download | duplicates (2)
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
//go:build !windows

// Binary tpm2-nvread reads data from NVRAM at a specified index. The data is
// printed out hex-encoded.
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/google/go-tpm/legacy/tpm2"
	"github.com/google/go-tpm/tpmutil"
)

var (
	tpmPath = flag.String("tpm-path", "/dev/tpm0", "Path to the TPM device (character device or a Unix socket)")
	index   = flag.Uint("index", 0, "NVRAM index of read")
)

func main() {
	flag.Parse()

	if *index == 0 {
		fmt.Fprintln(os.Stderr, "--index must be set")
		os.Exit(1)
	}

	val, err := nvRead(*tpmPath, uint32(*index))
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading from index 0x%x: %v\n", *index, err)
		os.Exit(1)
	}
	fmt.Printf("NVRAM value at index 0x%x (hex encoded):\n%x\n", *index, val)
}

func nvRead(path string, index uint32) ([]byte, error) {
	rwc, err := tpm2.OpenTPM(path)
	if err != nil {
		return nil, fmt.Errorf("can't open TPM at %q: %v", path, err)
	}
	defer rwc.Close()
	return tpm2.NVRead(rwc, tpmutil.Handle(index))
}