|
| 1 | +// |
| 2 | +// Use and distribution licensed under the Apache license version 2. |
| 3 | +// |
| 4 | +// See the COPYING file in the root project directory for full text. |
| 5 | +// |
| 6 | + |
| 7 | +package tpm |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/jaypipes/ghw/internal/config" |
| 13 | + "github.com/jaypipes/ghw/pkg/marshal" |
| 14 | +) |
| 15 | + |
| 16 | +// Device describes a single Trusted Platform Module (TPM) present on the host |
| 17 | +// system. |
| 18 | +type Device struct { |
| 19 | + // ManufacturerName is the human-readable name of the TPM manufacturer, |
| 20 | + // decoded from the four-character short name the TPM reports (e.g. "AMD", |
| 21 | + // "IFX" for Infineon, "STM" for STMicroelectronics). |
| 22 | + ManufacturerName string `json:"manufacturer_name"` |
| 23 | + // ManufacturerVendorID is the 16-bit TCG Vendor ID assigned to the TPM |
| 24 | + // manufacturer by the Trusted Computing Group. Note that this is a |
| 25 | + // separate namespace from the PCI vendor ID. It may be empty when the |
| 26 | + // host only exposes the manufacturer's short name. |
| 27 | + // See: https://trustedcomputinggroup.org/resource/vendor-id-registry/ |
| 28 | + ManufacturerVendorID string `json:"manufacturer_vendor_id"` |
| 29 | + // SpecVersion is the TCG TPM library (specification) version the device |
| 30 | + // implements, i.e. the ISO/IEC 11889 version, e.g. "1.2" or "2.0". |
| 31 | + SpecVersion string `json:"spec_version"` |
| 32 | + // FirmwareVersion is the TPM firmware version, when the host exposes it. |
| 33 | + FirmwareVersion string `json:"firmware_version"` |
| 34 | +} |
| 35 | + |
| 36 | +// String returns a short string describing the TPM device. |
| 37 | +func (d *Device) String() string { |
| 38 | + return fmt.Sprintf( |
| 39 | + "manufacturer_name=%s manufacturer_vendor_id=%s firmware_version=%s spec_version=%s", |
| 40 | + d.ManufacturerName, d.ManufacturerVendorID, d.FirmwareVersion, d.SpecVersion, |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +// Info describes the Trusted Platform Module(s) on the host system. |
| 45 | +type Info struct { |
| 46 | + // Devices are the TPM devices detected under /sys/class/tpm. A host can, |
| 47 | + // in principle, expose more than one TPM device. |
| 48 | + Devices []*Device `json:"devices"` |
| 49 | +} |
| 50 | + |
| 51 | +// String returns a short string with summary information about the TPM(s) on |
| 52 | +// the host system. |
| 53 | +func (i *Info) String() string { |
| 54 | + switch len(i.Devices) { |
| 55 | + case 0: |
| 56 | + return "tpm (no devices)" |
| 57 | + case 1: |
| 58 | + return "tpm " + i.Devices[0].String() |
| 59 | + default: |
| 60 | + return fmt.Sprintf("tpm (%d devices)", len(i.Devices)) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// New returns a pointer to an Info struct that contains information about |
| 65 | +// the TPM(s) on the host system. |
| 66 | +func New(args ...any) (*Info, error) { |
| 67 | + ctx := config.ContextFromArgs(args...) |
| 68 | + info := &Info{} |
| 69 | + if err := info.load(ctx); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + return info, nil |
| 73 | +} |
| 74 | + |
| 75 | +// simple private struct used to encapsulate TPM information in a top-level |
| 76 | +// "tpm" YAML/JSON map/object key |
| 77 | +type tpmPrinter struct { |
| 78 | + Info *Info `json:"tpm"` |
| 79 | +} |
| 80 | + |
| 81 | +// YAMLString returns a string with the TPM information formatted as YAML |
| 82 | +// under a top-level "tpm:" key |
| 83 | +func (i *Info) YAMLString() string { |
| 84 | + return marshal.SafeYAML(tpmPrinter{i}) |
| 85 | +} |
| 86 | + |
| 87 | +// JSONString returns a string with the TPM information formatted as JSON |
| 88 | +// under a top-level "tpm:" key |
| 89 | +func (i *Info) JSONString(indent bool) string { |
| 90 | + return marshal.SafeJSON(tpmPrinter{i}, indent) |
| 91 | +} |
0 commit comments