-
Notifications
You must be signed in to change notification settings - Fork 220
Introduce pkg/tpm to detect and describe TPMs #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+530
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // | ||
| // Use and distribution licensed under the Apache license version 2. | ||
| // | ||
| // See the COPYING file in the root project directory for full text. | ||
| // | ||
|
|
||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/jaypipes/ghw" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // tpmCmd represents the `tpm` command | ||
| var tpmCmd = &cobra.Command{ | ||
| Use: "tpm", | ||
| Short: "Show TPM information for the host system", | ||
| RunE: showTPM, | ||
| } | ||
|
|
||
| // showTPM shows TPM information for the host system. | ||
| func showTPM(cmd *cobra.Command, args []string) error { | ||
| tpm, err := ghw.TPM(cmd.Context()) | ||
| if err != nil { | ||
| return fmt.Errorf("error getting TPM info: %w", err) | ||
| } | ||
|
|
||
| switch outputFormat { | ||
| case outputFormatHuman: | ||
| fmt.Printf("%v\n", tpm) | ||
| case outputFormatJSON: | ||
| fmt.Printf("%s\n", tpm.JSONString(pretty)) | ||
| case outputFormatYAML: | ||
| fmt.Printf("%s", tpm.YAMLString()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(tpmCmd) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // | ||
| // Use and distribution licensed under the Apache license version 2. | ||
| // | ||
| // See the COPYING file in the root project directory for full text. | ||
| // | ||
|
|
||
| package tpm | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/jaypipes/ghw/internal/config" | ||
| "github.com/jaypipes/ghw/pkg/marshal" | ||
| ) | ||
|
|
||
| // Device describes a single Trusted Platform Module (TPM) present on the host | ||
| // system. | ||
| type Device struct { | ||
| // ManufacturerName is the human-readable name of the TPM manufacturer, | ||
| // decoded from the four-character short name the TPM reports (e.g. "AMD", | ||
| // "IFX" for Infineon, "STM" for STMicroelectronics). | ||
| ManufacturerName string `json:"manufacturer_name"` | ||
| // ManufacturerVendorID is the 16-bit TCG Vendor ID assigned to the TPM | ||
| // manufacturer by the Trusted Computing Group. Note that this is a | ||
| // separate namespace from the PCI vendor ID. It may be empty when the | ||
| // host only exposes the manufacturer's short name. | ||
| // See: https://trustedcomputinggroup.org/resource/vendor-id-registry/ | ||
| ManufacturerVendorID string `json:"manufacturer_vendor_id"` | ||
| // SpecVersion is the TCG TPM library (specification) version the device | ||
| // implements, i.e. the ISO/IEC 11889 version, e.g. "1.2" or "2.0". | ||
| SpecVersion string `json:"spec_version"` | ||
| // FirmwareVersion is the TPM firmware version, when the host exposes it. | ||
| FirmwareVersion string `json:"firmware_version"` | ||
| } | ||
|
|
||
| // String returns a short string describing the TPM device. | ||
| func (d *Device) String() string { | ||
| return fmt.Sprintf( | ||
| "manufacturer_name=%s manufacturer_vendor_id=%s firmware_version=%s spec_version=%s", | ||
| d.ManufacturerName, d.ManufacturerVendorID, d.FirmwareVersion, d.SpecVersion, | ||
| ) | ||
| } | ||
|
|
||
| // Info describes the Trusted Platform Module(s) on the host system. | ||
| type Info struct { | ||
| // Devices are the TPM devices detected under /sys/class/tpm. A host can, | ||
| // in principle, expose more than one TPM device. | ||
| Devices []*Device `json:"devices"` | ||
| } | ||
|
|
||
| // String returns a short string with summary information about the TPM(s) on | ||
| // the host system. | ||
| func (i *Info) String() string { | ||
| switch len(i.Devices) { | ||
| case 0: | ||
| return "tpm (no devices)" | ||
| case 1: | ||
| return "tpm " + i.Devices[0].String() | ||
| default: | ||
| return fmt.Sprintf("tpm (%d devices)", len(i.Devices)) | ||
| } | ||
| } | ||
|
|
||
| // New returns a pointer to an Info struct that contains information about | ||
| // the TPM(s) on the host system. | ||
| func New(args ...any) (*Info, error) { | ||
| ctx := config.ContextFromArgs(args...) | ||
| info := &Info{} | ||
| if err := info.load(ctx); err != nil { | ||
| return nil, err | ||
| } | ||
| return info, nil | ||
| } | ||
|
|
||
| // simple private struct used to encapsulate TPM information in a top-level | ||
| // "tpm" YAML/JSON map/object key | ||
| type tpmPrinter struct { | ||
| Info *Info `json:"tpm"` | ||
| } | ||
|
|
||
| // YAMLString returns a string with the TPM information formatted as YAML | ||
| // under a top-level "tpm:" key | ||
| func (i *Info) YAMLString() string { | ||
| return marshal.SafeYAML(tpmPrinter{i}) | ||
| } | ||
|
|
||
| // JSONString returns a string with the TPM information formatted as JSON | ||
| // under a top-level "tpm:" key | ||
| func (i *Info) JSONString(indent bool) string { | ||
| return marshal.SafeJSON(tpmPrinter{i}, indent) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.