Debian Codegraph @

cmd/open.go

raw
1 package cmd
2
3 import (
4 "fmt"
5 "io"
6
7 "github.com/google/go-tpm-tools/client"
8 )
9
10 // ExternalTPM can be set to run tests against an TPM initialized by an
11 // external package (like the simulator). Setting this value will make all
12 // gotpm commands run against it, and will prevent the cmd package from
13 // closing the TPM. Setting this value and closing the TPM must be managed
14 // by the external package.
15 // ExternalTPM can have a TPM simulator or a real TPM.
16 var ExternalTPM io.ReadWriter
17
18 // extTPMWrapper is designed to wrap the ExternalTPM to provide some overriding
19 // functions.
20 type extTPMWrapper struct {
21 io.ReadWriter
22 }
23
24 // Close is no-op for extTPMWrapper to prevent it closing the underlying simulator.
25 func (et extTPMWrapper) Close() error {
26 return nil
27 }
28
29 // EventLog is a workaround so the caller can call the underlying EventLogGetter function
30 // of the underlying TPM.
31 func (et extTPMWrapper) EventLog() ([]byte, error) {
32 return client.GetEventLog(et.ReadWriter)
33 }
34
35 func openTpm() (io.ReadWriteCloser, error) {
36 if ExternalTPM != nil {
37 return extTPMWrapper{ExternalTPM}, nil
38 }
39 rwc, err := openImpl()
40 if err != nil {
41 return nil, fmt.Errorf("connecting to TPM: %w", err)
42 }
43 return rwc, nil
44 }