gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/smbios/info.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 parses SMBIOS tables into Go structures.
     6  //
     7  // smbios can read tables from binary data or from sysfs using the FromSysfs
     8  // and ParseInfo functions.
     9  package smbios
    10  
    11  import (
    12  	"fmt"
    13  )
    14  
    15  // Info contains the SMBIOS information.
    16  type Info struct {
    17  	Entry32 *Entry32
    18  	Entry64 *Entry64
    19  	Tables  []*Table
    20  }
    21  
    22  // String returns a summary of the SMBIOS version and number of tables.
    23  func (i *Info) String() string {
    24  	return fmt.Sprintf("SMBIOS %d.%d.%d (%d tables)", i.MajorVersion(), i.MinorVersion(), i.DocRev(), len(i.Tables))
    25  }
    26  
    27  // ParseInfo parses SMBIOS information from binary data.
    28  func ParseInfo(entryData, tableData []byte) (*Info, error) {
    29  	info := &Info{}
    30  	var err error
    31  	info.Entry32, info.Entry64, err = ParseEntry(entryData)
    32  	if err != nil {
    33  		return nil, fmt.Errorf("error parsing entry point structure: %v", err)
    34  	}
    35  	for len(tableData) > 0 {
    36  		t, remainder, err := ParseTable(tableData)
    37  		if err != nil && err != errEndOfTable {
    38  			return nil, err
    39  		}
    40  		info.Tables = append(info.Tables, t)
    41  		tableData = remainder
    42  	}
    43  	return info, nil
    44  }
    45  
    46  // MajorVersion return major version of the SMBIOS spec.
    47  func (i *Info) MajorVersion() uint8 {
    48  	if i.Entry64 != nil {
    49  		return i.Entry64.SMBIOSMajorVersion
    50  	}
    51  	if i.Entry32 != nil {
    52  		return i.Entry32.SMBIOSMajorVersion
    53  	}
    54  	return 0
    55  }
    56  
    57  // MinorVersion return minor version of the SMBIOS spec.
    58  func (i *Info) MinorVersion() uint8 {
    59  	if i.Entry64 != nil {
    60  		return i.Entry64.SMBIOSMinorVersion
    61  	}
    62  	if i.Entry32 != nil {
    63  		return i.Entry32.SMBIOSMinorVersion
    64  	}
    65  	return 0
    66  }
    67  
    68  // DocRev return document revision of the SMBIOS spec.
    69  func (i *Info) DocRev() uint8 {
    70  	if i.Entry64 != nil {
    71  		return i.Entry64.SMBIOSDocRev
    72  	}
    73  	return 0
    74  }
    75  
    76  // GetTablesByType returns tables of specific type.
    77  func (i *Info) GetTablesByType(tt TableType) []*Table {
    78  	var res []*Table
    79  	for _, t := range i.Tables {
    80  		if t.Type == tt {
    81  			res = append(res, t)
    82  		}
    83  	}
    84  	return res
    85  }
    86  
    87  // GetBIOSInfo returns the Bios Info (type 0) table, if present.
    88  func (i *Info) GetBIOSInfo() (*BIOSInfo, error) {
    89  	bt := i.GetTablesByType(TableTypeBIOSInfo)
    90  	if len(bt) == 0 {
    91  		return nil, ErrTableNotFound
    92  	}
    93  	// There can only be one of these.
    94  	return ParseBIOSInfo(bt[0])
    95  }
    96  
    97  // GetSystemInfo returns the System Info (type 1) table, if present.
    98  func (i *Info) GetSystemInfo() (*SystemInfo, error) {
    99  	bt := i.GetTablesByType(TableTypeSystemInfo)
   100  	if len(bt) == 0 {
   101  		return nil, ErrTableNotFound
   102  	}
   103  	// There can only be one of these.
   104  	return ParseSystemInfo(bt[0])
   105  }
   106  
   107  // GetBaseboardInfo returns all the Baseboard Info (type 2) tables present.
   108  func (i *Info) GetBaseboardInfo() ([]*BaseboardInfo, error) {
   109  	var res []*BaseboardInfo
   110  	for _, t := range i.GetTablesByType(TableTypeBaseboardInfo) {
   111  		bi, err := ParseBaseboardInfo(t)
   112  		if err != nil {
   113  			return nil, err
   114  		}
   115  		res = append(res, bi)
   116  	}
   117  	return res, nil
   118  }
   119  
   120  // GetChassisInfo returns all the Chassis Info (type 3) tables present.
   121  func (i *Info) GetChassisInfo() ([]*ChassisInfo, error) {
   122  	var res []*ChassisInfo
   123  	for _, t := range i.GetTablesByType(TableTypeChassisInfo) {
   124  		ci, err := ParseChassisInfo(t)
   125  		if err != nil {
   126  			return nil, err
   127  		}
   128  		res = append(res, ci)
   129  	}
   130  	return res, nil
   131  }
   132  
   133  // GetProcessorInfo returns all the Processor Info (type 4) tables present.
   134  func (i *Info) GetProcessorInfo() ([]*ProcessorInfo, error) {
   135  	var res []*ProcessorInfo
   136  	for _, t := range i.GetTablesByType(TableTypeProcessorInfo) {
   137  		pi, err := ParseProcessorInfo(t)
   138  		if err != nil {
   139  			return nil, err
   140  		}
   141  		res = append(res, pi)
   142  	}
   143  	return res, nil
   144  }
   145  
   146  // GetCacheInfo returns all the Cache Info (type 7) tables present.
   147  func (i *Info) GetCacheInfo() ([]*CacheInfo, error) {
   148  	var res []*CacheInfo
   149  	for _, t := range i.GetTablesByType(TableTypeCacheInfo) {
   150  		ci, err := ParseCacheInfo(t)
   151  		if err != nil {
   152  			return nil, err
   153  		}
   154  		res = append(res, ci)
   155  	}
   156  	return res, nil
   157  }
   158  
   159  // GetMemoryDevices returns all the Memory Device (type 17) tables present.
   160  func (i *Info) GetMemoryDevices() ([]*MemoryDevice, error) {
   161  	var res []*MemoryDevice
   162  	for _, t := range i.GetTablesByType(TableTypeMemoryDevice) {
   163  		ci, err := NewMemoryDevice(t)
   164  		if err != nil {
   165  			return nil, err
   166  		}
   167  		res = append(res, ci)
   168  	}
   169  	return res, nil
   170  }
   171  
   172  // GetIPMIDeviceInfo returns all the IPMI Device Info (type 38) tables present.
   173  func (i *Info) GetIPMIDeviceInfo() ([]*IPMIDeviceInfo, error) {
   174  	var res []*IPMIDeviceInfo
   175  	for _, t := range i.GetTablesByType(TableTypeIPMIDeviceInfo) {
   176  		d, err := ParseIPMIDeviceInfo(t)
   177  		if err != nil {
   178  			return nil, err
   179  		}
   180  		res = append(res, d)
   181  	}
   182  	return res, nil
   183  }
   184  
   185  // GetTPMDevices returns all the TPM Device (type 43) tables present.
   186  func (i *Info) GetTPMDevices() ([]*TPMDevice, error) {
   187  	var res []*TPMDevice
   188  	for _, t := range i.GetTablesByType(TableTypeTPMDevice) {
   189  		d, err := NewTPMDevice(t)
   190  		if err != nil {
   191  			return nil, err
   192  		}
   193  		res = append(res, d)
   194  	}
   195  	return res, nil
   196  }