github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/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 6 7 import ( 8 "fmt" 9 ) 10 11 // Info contains the SMBIOS information. 12 type Info struct { 13 Entry32 *Entry32 14 Entry64 *Entry64 15 Tables []*Table 16 } 17 18 // ParseInfo parses SMBIOS information from binary data. 19 func ParseInfo(entryData, tableData []byte) (*Info, error) { 20 info := &Info{} 21 var err error 22 info.Entry32, info.Entry64, err = ParseEntry(entryData) 23 if err != nil { 24 return nil, fmt.Errorf("error parsing entry point structure: %v", err) 25 } 26 for len(tableData) > 0 { 27 t, remainder, err := ParseTable(tableData) 28 if err != nil && err != errEndOfTable { 29 return nil, err 30 } 31 info.Tables = append(info.Tables, t) 32 tableData = remainder 33 } 34 return info, nil 35 } 36 37 // GetSMBIOSMajorVersion return major version of the SMBIOS spec. 38 func (i *Info) GetSMBIOSMajorVersion() uint8 { 39 if i.Entry64 != nil { 40 return i.Entry64.SMBIOSMajorVersion 41 } 42 if i.Entry32 != nil { 43 return i.Entry32.SMBIOSMajorVersion 44 } 45 return 0 46 } 47 48 // GetSMBIOSMinorVersion return minor version of the SMBIOS spec. 49 func (i *Info) GetSMBIOSMinorVersion() uint8 { 50 if i.Entry64 != nil { 51 return i.Entry64.SMBIOSMinorVersion 52 } 53 if i.Entry32 != nil { 54 return i.Entry32.SMBIOSMinorVersion 55 } 56 return 0 57 } 58 59 // GetSMBIOSDocRev return document revision of the SMBIOS spec. 60 func (i *Info) GetSMBIOSDocRev() uint8 { 61 if i.Entry64 != nil { 62 return i.Entry64.SMBIOSDocRev 63 } 64 return 0 65 } 66 67 // GetTablesByType returns tables of specific type. 68 func (i *Info) GetTablesByType(tt TableType) []*Table { 69 var res []*Table 70 for _, t := range i.Tables { 71 if t.Type == tt { 72 res = append(res, t) 73 } 74 } 75 return res 76 } 77 78 // GetBIOSInformation returns the Bios Information (type 0) table, if present. 79 func (i *Info) GetBIOSInformation() (*BIOSInformation, error) { 80 bt := i.GetTablesByType(TableTypeBIOSInformation) 81 if len(bt) == 0 { 82 return nil, ErrTableNotFound 83 } 84 // There can only be one of these. 85 return NewBIOSInformation(bt[0]) 86 } 87 88 // GetSystemInformation returns the System Information (type 1) table, if present. 89 func (i *Info) GetSystemInformation() (*SystemInformation, error) { 90 bt := i.GetTablesByType(TableTypeSystemInformation) 91 if len(bt) == 0 { 92 return nil, ErrTableNotFound 93 } 94 // There can only be one of these. 95 return NewSystemInformation(bt[0]) 96 } 97 98 // GetBaseboardInformation returns all the Baseboard Information (type 2) tables present. 99 func (i *Info) GetBaseboardInformation() ([]*BaseboardInformation, error) { 100 var res []*BaseboardInformation 101 for _, t := range i.GetTablesByType(TableTypeBaseboardInformation) { 102 bi, err := NewBaseboardInformation(t) 103 if err != nil { 104 return nil, err 105 } 106 res = append(res, bi) 107 } 108 return res, nil 109 } 110 111 // GetChassisInformation returns all the Chassis Information (type 3) tables present. 112 func (i *Info) GetChassisInformation() ([]*ChassisInformation, error) { 113 var res []*ChassisInformation 114 for _, t := range i.GetTablesByType(TableTypeChassisInformation) { 115 ci, err := NewChassisInformation(t) 116 if err != nil { 117 return nil, err 118 } 119 res = append(res, ci) 120 } 121 return res, nil 122 } 123 124 // GetProcessorInformation returns all the Processor Information (type 4) tables present. 125 func (i *Info) GetProcessorInformation() ([]*ProcessorInformation, error) { 126 var res []*ProcessorInformation 127 for _, t := range i.GetTablesByType(TableTypeProcessorInformation) { 128 pi, err := NewProcessorInformation(t) 129 if err != nil { 130 return nil, err 131 } 132 res = append(res, pi) 133 } 134 return res, nil 135 } 136 137 // GetCacheInformation returns all the Cache Information (type 7) tables present. 138 func (i *Info) GetCacheInformation() ([]*CacheInformation, error) { 139 var res []*CacheInformation 140 for _, t := range i.GetTablesByType(TableTypeCacheInformation) { 141 ci, err := NewCacheInformation(t) 142 if err != nil { 143 return nil, err 144 } 145 res = append(res, ci) 146 } 147 return res, nil 148 } 149 150 // GetMemoryDevices returns all the Memory Device (type 17) tables present. 151 func (i *Info) GetMemoryDevices() ([]*MemoryDevice, error) { 152 var res []*MemoryDevice 153 for _, t := range i.GetTablesByType(TableTypeMemoryDevice) { 154 ci, err := NewMemoryDevice(t) 155 if err != nil { 156 return nil, err 157 } 158 res = append(res, ci) 159 } 160 return res, nil 161 } 162 163 // GetIPMIDeviceInformation returns all the IPMI Device Information (type 38) tables present. 164 func (i *Info) GetIPMIDeviceInformation() ([]*IPMIDeviceInformation, error) { 165 var res []*IPMIDeviceInformation 166 for _, t := range i.GetTablesByType(TableTypeIPMIDeviceInformation) { 167 d, err := NewIPMIDeviceInformation(t) 168 if err != nil { 169 return nil, err 170 } 171 res = append(res, d) 172 } 173 return res, nil 174 } 175 176 // GetTPMDevices returns all the TPM Device (type 43) tables present. 177 func (i *Info) GetTPMDevices() ([]*TPMDevice, error) { 178 var res []*TPMDevice 179 for _, t := range i.GetTablesByType(TableTypeTPMDevice) { 180 d, err := NewTPMDevice(t) 181 if err != nil { 182 return nil, err 183 } 184 res = append(res, d) 185 } 186 return res, nil 187 }