github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/smbios/type43_tpm_device.go (about)

     1  // Copyright 2016-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package smbios
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // Much of this is auto-generated. If adding a new type, see README for instructions.
    14  
    15  // TPMDevice is defined in DSP0134 7.44.
    16  type TPMDevice struct {
    17  	Table
    18  	VendorID         TPMDeviceVendorID        `smbios:"-,skip=4"` // 04h
    19  	MajorSpecVersion uint8                    // 08h
    20  	MinorSpecVersion uint8                    // 09h
    21  	FirmwareVersion1 uint32                   // 0Ah
    22  	FirmwareVersion2 uint32                   // 0Eh
    23  	Description      string                   // 12h
    24  	Characteristics  TPMDeviceCharacteristics // 13h
    25  	OEMDefined       uint32                   // 1Bh
    26  }
    27  
    28  // NewTPMDevice parses a generic Table into TPMDevice.
    29  func NewTPMDevice(t *Table) (*TPMDevice, error) {
    30  	if t.Type != TableTypeTPMDevice {
    31  		return nil, fmt.Errorf("invalid table type %d", t.Type)
    32  	}
    33  	if t.Len() < 0x1f {
    34  		return nil, errors.New("required fields missing")
    35  	}
    36  	di := &TPMDevice{Table: *t}
    37  	if _, err := parseStruct(t, 0 /* off */, false /* complete */, di); err != nil {
    38  		return nil, err
    39  	}
    40  	vid, _ := di.GetBytesAt(4, 4)
    41  	copy(di.VendorID[:], vid)
    42  	return di, nil
    43  }
    44  
    45  func (di *TPMDevice) String() string {
    46  	lines := []string{
    47  		di.Header.String(),
    48  		fmt.Sprintf("Vendor ID: %s", di.VendorID),
    49  		fmt.Sprintf("Specification Version: %d.%d", di.MajorSpecVersion, di.MinorSpecVersion),
    50  	}
    51  	switch di.MajorSpecVersion {
    52  	case 1:
    53  		lines = append(lines, fmt.Sprintf("Firmware Revision: %d.%d",
    54  			(di.FirmwareVersion1>>16)&0xff, (di.FirmwareVersion1>>24)&0xff),
    55  		)
    56  	case 2:
    57  		lines = append(lines, fmt.Sprintf("Firmware Revision: %d.%d",
    58  			(di.FirmwareVersion1>>16)&0xffff, di.FirmwareVersion1&0xff),
    59  		)
    60  	}
    61  	lines = append(lines,
    62  		fmt.Sprintf("Description: %s", di.Description),
    63  		fmt.Sprintf("Characteristics:\n%s", di.Characteristics),
    64  		fmt.Sprintf("OEM-specific Information: 0x%08X", di.OEMDefined),
    65  	)
    66  	return strings.Join(lines, "\n\t")
    67  }
    68  
    69  // TPMDeviceVendorID is defined in TCG Vendor ID Registry.
    70  type TPMDeviceVendorID [4]byte
    71  
    72  func (vid TPMDeviceVendorID) String() string {
    73  	// DSP0134 specifies Vendor ID field as 4 BYTEs, not a DWORD, and gives an example value.
    74  	// But Infineon ignores it and puts their VID in LE byte order so it ends up backwards.
    75  	if vid[0] == 0 && vid[1] == 'X' && vid[2] == 'F' && vid[3] == 'I' {
    76  		return "IFX"
    77  	}
    78  	s := ""
    79  	for i := 0; i < 4 && vid[i] != 0; i++ {
    80  		s += string(vid[i])
    81  	}
    82  	return s
    83  }
    84  
    85  // TPMDeviceCharacteristics is defined in DSP0134 7.44.1.
    86  type TPMDeviceCharacteristics uint8
    87  
    88  // TPMDeviceCharacteristics fields are defined in DSP0134 x.x.x
    89  const (
    90  	TPMDeviceCharacteristicsNotSupported                                 TPMDeviceCharacteristics = 1 << 2 // TPM Device Characteristics are not supported.
    91  	TPMDeviceCharacteristicsFamilyConfigurableViaFirmwareUpdate          TPMDeviceCharacteristics = 1 << 3 // Family configurable via firmware update.
    92  	TPMDeviceCharacteristicsFamilyConfigurableViaPlatformSoftwareSupport TPMDeviceCharacteristics = 1 << 4 // Family configurable via platform software support.
    93  	TPMDeviceCharacteristicsFamilyConfigurableViaOEMProprietaryMechanism TPMDeviceCharacteristics = 1 << 5 // Family configurable via OEM proprietary mechanism.
    94  )
    95  
    96  func (v TPMDeviceCharacteristics) String() string {
    97  	if v&TPMDeviceCharacteristicsNotSupported != 0 {
    98  		return "\t\tTPM Device characteristics not supported"
    99  	}
   100  	var lines []string
   101  	if v&TPMDeviceCharacteristicsFamilyConfigurableViaFirmwareUpdate != 0 {
   102  		lines = append(lines, "Family configurable via firmware update")
   103  	}
   104  	if v&TPMDeviceCharacteristicsFamilyConfigurableViaPlatformSoftwareSupport != 0 {
   105  		lines = append(lines, "Family configurable via platform software support")
   106  	}
   107  	if v&TPMDeviceCharacteristicsFamilyConfigurableViaOEMProprietaryMechanism != 0 {
   108  		lines = append(lines, "Family configurable via OEM proprietary mechanism")
   109  	}
   110  	return "\t\t" + strings.Join(lines, "\n\t\t")
   111  }