-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmbios.go
405 lines (363 loc) · 11.6 KB
/
smbios.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package qcli
import (
"fmt"
"strings"
)
/*
smbios:
file:
bios:
system:
baseboard:
chassis:
processors:
- processor
- processor
memory:
- memory
- memory
*/
type SMBIOSInfo struct {
File string `yaml:"file,omitempty"` // -smbios file
BIOS SMTableBIOS `yaml:"bios,omitempty"` // -smbios type=0
System SMTableSystem `yaml:"system,omitempty"` // -smbios type=1
Baseboard SMTableBaseboard `yaml:"baseboard,omitempty"` // -smbios type=2
Chassis SMTableChassis `yaml:"chassis,omitempty"` // -smbios type=3
Processors []SMTableProcessor `yaml:"processors,omitempty"` // -smbios type=4
Memory []SMTableMemory `yaml:"memory,omitempty"` // -smbios type=17
}
const SMTableBIOSType = 0
type SMTableBIOS struct {
Vendor string `yaml:"vendor,omitempty"`
Version string `yaml:"version,omitempty"`
Date string `yaml:"date,omitempty"`
Release string `yaml:"release,omitempty"`
UEFI string `yaml:"uefi,omitempty"`
}
func (table SMTableBIOS) Valid() error {
if table.Release != "" {
var major int
var minor int
_, err := fmt.Sscanf(table.Release, "%d.%d", &major, &minor)
if err != nil {
return fmt.Errorf("SMTableBIOS Type=0 Release field is not in <digit>.<digit> format")
}
}
if table.UEFI != "" {
val := strings.ToLower(table.UEFI)
if val != "on" && val != "off" {
return fmt.Errorf("SMTableBIOS Type=0 UEFI field is not 'on' or 'off': %s", table.UEFI)
}
}
return nil
}
func (table SMTableBIOS) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableBIOSType)
if table.Vendor != "" {
tableParams = append(tableParams, "vendor="+table.Vendor)
}
if table.Version != "" {
tableParams = append(tableParams, "version="+table.Version)
}
if table.Date != "" {
tableParams = append(tableParams, "date="+table.Date)
}
if table.Release != "" {
tableParams = append(tableParams, "release="+table.Release)
}
if table.UEFI != "" {
tableParams = append(tableParams, "uefi="+table.UEFI)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
// fmt.Printf("SMTableBIOS: table=%v tparams=%v qparams=%v\n", table, tableParams, qemuParams)
return qemuParams
}
const SMTableSystemType = 1
type SMTableSystem struct {
Manufacturer string `yaml:"manufacturer,omitempty"`
Product string `yaml:"product,omitempty"`
Version string `yaml:"version,omitempty"`
Serial string `yaml:"serial,omitempty"`
UUID string `yaml:"uuid,omitempty"`
SKU string `yaml:"sku,omitempty"`
Family string `yaml:"family,omitempty"`
}
func (table SMTableSystem) Valid() error {
// no format requirements
return nil
}
func (table SMTableSystem) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableSystemType)
if table.Manufacturer != "" {
tableParams = append(tableParams, "manufacturer="+table.Manufacturer)
}
if table.Product != "" {
tableParams = append(tableParams, "product="+table.Product)
}
if table.Version != "" {
tableParams = append(tableParams, "version="+table.Version)
}
if table.Serial != "" {
tableParams = append(tableParams, "serial="+table.Serial)
}
if table.UUID != "" {
tableParams = append(tableParams, "uuid="+table.UUID)
}
if table.SKU != "" {
tableParams = append(tableParams, "sku="+table.SKU)
}
if table.Family != "" {
tableParams = append(tableParams, "family="+table.Family)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
return qemuParams
}
const SMTableBaseboardType = 2
type SMTableBaseboard struct {
Manufacturer string `yaml:"manufacturer,omitempty"`
Product string `yaml:"product,omitempty"`
Version string `yaml:"version,omitempty"`
Serial string `yaml:"serial,omitempty"`
Asset string `yaml:"asset,omitempty"`
Location string `yaml:"location,omitempty"`
}
func (table SMTableBaseboard) Valid() error {
// no format requirements
return nil
}
func (table SMTableBaseboard) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableBaseboardType)
if table.Manufacturer != "" {
tableParams = append(tableParams, "manufacturer="+table.Manufacturer)
}
if table.Product != "" {
tableParams = append(tableParams, "product="+table.Product)
}
if table.Version != "" {
tableParams = append(tableParams, "version="+table.Version)
}
if table.Serial != "" {
tableParams = append(tableParams, "serial="+table.Serial)
}
if table.Asset != "" {
tableParams = append(tableParams, "asset="+table.Asset)
}
if table.Location != "" {
tableParams = append(tableParams, "location="+table.Location)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
return qemuParams
}
const SMTableChassisType = 3
type SMTableChassis struct {
Manufacturer string `yaml:"manufacturer,omitempty"`
Version string `yaml:"version,omitempty"`
Serial string `yaml:"serial,omitempty"`
Asset string `yaml:"asset,omitempty"`
SKU string `yaml:"sku,omitempty"`
}
func (table SMTableChassis) Valid() error {
// no format requirements
return nil
}
func (table SMTableChassis) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableChassisType)
if table.Manufacturer != "" {
tableParams = append(tableParams, "manufacturer="+table.Manufacturer)
}
if table.Version != "" {
tableParams = append(tableParams, "version="+table.Version)
}
if table.Serial != "" {
tableParams = append(tableParams, "serial="+table.Serial)
}
if table.Asset != "" {
tableParams = append(tableParams, "asset="+table.Asset)
}
if table.SKU != "" {
tableParams = append(tableParams, "sku="+table.SKU)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
return qemuParams
}
const SMTableProcessorType = 4
type SMTableProcessor struct {
SocketPrefix string `yaml:"socket-prefix,omitempty"`
Manufacturer string `yaml:"manufacturer,omitempty"`
Version string `yaml:"version,omitempty"`
Serial string `yaml:"serial,omitempty"`
Asset string `yaml:"asset,omitempty"`
Part string `yaml:"part,omitempty"`
}
func (table SMTableProcessor) Valid() error {
// no format requirements
return nil
}
func (table SMTableProcessor) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableProcessorType)
if table.SocketPrefix != "" {
tableParams = append(tableParams, "sock_pfx="+table.SocketPrefix)
}
if table.Manufacturer != "" {
tableParams = append(tableParams, "manufacturer="+table.Manufacturer)
}
if table.Version != "" {
tableParams = append(tableParams, "version="+table.Version)
}
if table.Serial != "" {
tableParams = append(tableParams, "serial="+table.Serial)
}
if table.Asset != "" {
tableParams = append(tableParams, "asset="+table.Asset)
}
if table.Part != "" {
tableParams = append(tableParams, "part="+table.Part)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
return qemuParams
}
const SMTableMemoryType = 17
type SMTableMemory struct {
LocationPrefix string `yaml:"location-prefix,omitempty"`
Bank string `yaml:"bank,omitempty"`
Manufacturer string `yaml:"manufacturer,omitempty"`
Serial string `yaml:"serial,omitempty"`
Asset string `yaml:"asset,omitempty"`
Part string `yaml:"part,omitempty"`
Speed string `yaml:"speed,omitempty"`
}
func (table SMTableMemory) Valid() error {
if table.Speed != "" {
var speed int
_, err := fmt.Sscanf(table.Speed, "%d", &speed)
if err != nil {
return fmt.Errorf("SMTableMemory Type=17 Speed field must be a number, found: %s", table.Speed)
}
}
return nil
}
func (table SMTableMemory) QemuParams(config *Config) []string {
var qemuParams []string
var tableParams []string
typeParam := fmt.Sprintf("type=%d", SMTableMemoryType)
if table.LocationPrefix != "" {
tableParams = append(tableParams, "loc_pfx="+table.LocationPrefix)
}
if table.Bank != "" {
tableParams = append(tableParams, "bank="+table.Bank)
}
if table.Manufacturer != "" {
tableParams = append(tableParams, "manufacturer="+table.Manufacturer)
}
if table.Serial != "" {
tableParams = append(tableParams, "serial="+table.Serial)
}
if table.Asset != "" {
tableParams = append(tableParams, "asset="+table.Asset)
}
if table.Part != "" {
tableParams = append(tableParams, "part="+table.Part)
}
if table.Speed != "" {
tableParams = append(tableParams, "speed="+table.Speed)
}
if len(tableParams) > 0 {
qemuParams = append(qemuParams, "-smbios")
tableParams = append([]string{typeParam}, tableParams...)
qemuParams = append(qemuParams, strings.Join(tableParams, ","))
}
return qemuParams
}
/*
type SMBIOSInfo struct {
File string `yaml:"file,omitempty"` // -smbios file
BIOS SMTableBIOS `yaml:"bios,omitempty"` // -smbios type=0
System SMTableSystem `yaml:"system,omitempty"` // -smbios type=1
Baseboard SMTableBaseboard `yaml:"baseboard,omitempty"` // -smbios type=2
Chassis SMTableChassis `yaml:"chassis,omitempty"` // -smbios type=3
Processors []SMTableProcessor `yaml:"processors,omitempty"` // -smbios type=4
Memory []SMTableMemory `yaml:"memory,omitempty"` // -smbios type=17
}
*/
// Valid returns true if the SMBIOSInfo structure is valid and complete.
func (smb SMBIOSInfo) Valid() error {
if err := smb.BIOS.Valid(); err != nil {
return err
}
if err := smb.System.Valid(); err != nil {
return err
}
if err := smb.Baseboard.Valid(); err != nil {
return err
}
if err := smb.Chassis.Valid(); err != nil {
return err
}
for _, proc := range smb.Processors {
if err := proc.Valid(); err != nil {
return err
}
}
for _, mem := range smb.Memory {
if err := mem.Valid(); err != nil {
return err
}
}
return nil
}
// QemuParams returns the qemu parameters built out of the SMBIOSInfo object
func (smb SMBIOSInfo) QemuParams(config *Config) []string {
var qemuParams []string
if smb.File != "" {
qemuParams = append(qemuParams, "-smbios", "file="+smb.File)
}
qemuParams = append(qemuParams, smb.BIOS.QemuParams(config)...)
qemuParams = append(qemuParams, smb.System.QemuParams(config)...)
qemuParams = append(qemuParams, smb.Baseboard.QemuParams(config)...)
qemuParams = append(qemuParams, smb.Chassis.QemuParams(config)...)
for _, proc := range smb.Processors {
qemuParams = append(qemuParams, proc.QemuParams(config)...)
}
for _, mem := range smb.Memory {
qemuParams = append(qemuParams, mem.QemuParams(config)...)
}
return qemuParams
}
func (config *Config) appendSMBIOSInfo() error {
//fmt.Printf("config called appendSMBIOSInfo()\n")
if err := config.SMBIOS.Valid(); err != nil {
return err
}
config.qemuParams = append(config.qemuParams, config.SMBIOS.QemuParams(config)...)
return nil
}