forked from openhardwaremonitor/openhardwaremonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMBIOS.cs
467 lines (376 loc) · 15.4 KB
/
SMBIOS.cs
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2012 Michael Möller <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Text;
namespace OpenHardwareMonitor.Hardware {
internal class SMBIOS {
private readonly byte[] raw;
private readonly Structure[] table;
private readonly Version version;
private readonly BIOSInformation biosInformation;
private readonly SystemInformation systemInformation;
private readonly BaseBoardInformation baseBoardInformation;
private readonly ProcessorInformation processorInformation;
private readonly MemoryDevice[] memoryDevices;
private static string ReadSysFS(string path) {
try {
if (File.Exists(path)) {
using (StreamReader reader = new StreamReader(path))
return reader.ReadLine();
} else {
return null;
}
} catch {
return null;
}
}
public SMBIOS() {
int p = (int)Environment.OSVersion.Platform;
if ((p == 4) || (p == 128)) {
this.raw = null;
this.table = null;
string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
string boardName = ReadSysFS("/sys/class/dmi/id/board_name");
string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");
this.baseBoardInformation = new BaseBoardInformation(
boardVendor, boardName, boardVersion, null);
string systemVendor = ReadSysFS("/sys/class/dmi/id/sys_vendor");
string productName = ReadSysFS("/sys/class/dmi/id/product_name");
string productVersion = ReadSysFS("/sys/class/dmi/id/product_version");
this.systemInformation = new SystemInformation(systemVendor,
productName, productVersion, null, null);
string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
this.memoryDevices = new MemoryDevice[0];
} else {
List<Structure> structureList = new List<Structure>();
List<MemoryDevice> memoryDeviceList = new List<MemoryDevice>();
raw = null;
byte majorVersion = 0;
byte minorVersion = 0;
try {
ManagementObjectCollection collection;
using (ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSSMBios_RawSMBiosTables")) {
collection = searcher.Get();
}
foreach (ManagementObject mo in collection) {
raw = (byte[])mo["SMBiosData"];
majorVersion = (byte)mo["SmbiosMajorVersion"];
minorVersion = (byte)mo["SmbiosMinorVersion"];
break;
}
} catch { }
if (majorVersion > 0 || minorVersion > 0)
version = new Version(majorVersion, minorVersion);
if (raw != null && raw.Length > 0) {
int offset = 0;
byte type = raw[offset];
while (offset + 4 < raw.Length && type != 127) {
type = raw[offset];
int length = raw[offset + 1];
ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
if (offset + length > raw.Length)
break;
byte[] data = new byte[length];
Array.Copy(raw, offset, data, 0, length);
offset += length;
List<string> stringsList = new List<string>();
if (offset < raw.Length && raw[offset] == 0)
offset++;
while (offset < raw.Length && raw[offset] != 0) {
StringBuilder sb = new StringBuilder();
while (offset < raw.Length && raw[offset] != 0) {
sb.Append((char)raw[offset]); offset++;
}
offset++;
stringsList.Add(sb.ToString());
}
offset++;
switch (type) {
case 0x00:
this.biosInformation = new BIOSInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.biosInformation); break;
case 0x01:
this.systemInformation = new SystemInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.systemInformation); break;
case 0x02: this.baseBoardInformation = new BaseBoardInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.baseBoardInformation); break;
case 0x04: this.processorInformation = new ProcessorInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.processorInformation); break;
case 0x11: MemoryDevice m = new MemoryDevice(
type, handle, data, stringsList.ToArray());
memoryDeviceList.Add(m);
structureList.Add(m); break;
default: structureList.Add(new Structure(
type, handle, data, stringsList.ToArray())); break;
}
}
}
memoryDevices = memoryDeviceList.ToArray();
table = structureList.ToArray();
}
}
public string GetReport() {
StringBuilder r = new StringBuilder();
if (version != null) {
r.Append("SMBIOS Version: "); r.AppendLine(version.ToString(2));
r.AppendLine();
}
if (BIOS != null) {
r.Append("BIOS Vendor: "); r.AppendLine(BIOS.Vendor);
r.Append("BIOS Version: "); r.AppendLine(BIOS.Version);
r.AppendLine();
}
if (System != null) {
r.Append("System Manufacturer: ");
r.AppendLine(System.ManufacturerName);
r.Append("System Name: ");
r.AppendLine(System.ProductName);
r.Append("System Version: ");
r.AppendLine(System.Version);
r.AppendLine();
}
if (Board != null) {
r.Append("Mainboard Manufacturer: ");
r.AppendLine(Board.ManufacturerName);
r.Append("Mainboard Name: ");
r.AppendLine(Board.ProductName);
r.Append("Mainboard Version: ");
r.AppendLine(Board.Version);
r.AppendLine();
}
if (Processor != null) {
r.Append("Processor Manufacturer: ");
r.AppendLine(Processor.ManufacturerName);
r.Append("Processor Version: ");
r.AppendLine(Processor.Version);
r.Append("Processor Core Count: ");
r.AppendLine(Processor.CoreCount.ToString());
r.Append("Processor Core Enabled: ");
r.AppendLine(Processor.CoreEnabled.ToString());
r.Append("Processor Thread Count: ");
r.AppendLine(Processor.ThreadCount.ToString());
r.Append("Processor External Clock: ");
r.Append(Processor.ExternalClock);
r.AppendLine(" Mhz");
r.AppendLine();
}
for (int i = 0; i < MemoryDevices.Length; i++) {
r.Append("Memory Device [" + i + "] Manufacturer: ");
r.AppendLine(MemoryDevices[i].ManufacturerName);
r.Append("Memory Device [" + i + "] Part Number: ");
r.AppendLine(MemoryDevices[i].PartNumber);
r.Append("Memory Device [" + i + "] Device Locator: ");
r.AppendLine(MemoryDevices[i].DeviceLocator);
r.Append("Memory Device [" + i + "] Bank Locator: ");
r.AppendLine(MemoryDevices[i].BankLocator);
r.Append("Memory Device [" + i + "] Speed: ");
r.Append(MemoryDevices[i].Speed);
r.AppendLine(" MHz");
r.AppendLine();
}
if (raw != null) {
string base64 = Convert.ToBase64String(raw);
r.AppendLine("SMBIOS Table");
r.AppendLine();
for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
r.Append(" ");
for (int j = 0; j < 0x40; j++) {
int index = (i << 6) | j;
if (index < base64.Length) {
r.Append(base64[index]);
}
}
r.AppendLine();
}
r.AppendLine();
}
return r.ToString();
}
public BIOSInformation BIOS {
get { return biosInformation; }
}
public SystemInformation System {
get { return systemInformation; }
}
public BaseBoardInformation Board {
get { return baseBoardInformation; }
}
public ProcessorInformation Processor {
get { return processorInformation; }
}
public MemoryDevice[] MemoryDevices {
get { return memoryDevices; }
}
public class Structure {
private readonly byte type;
private readonly ushort handle;
private readonly byte[] data;
private readonly string[] strings;
protected int GetByte(int offset) {
if (offset < data.Length && offset >= 0)
return data[offset];
else
return 0;
}
protected int GetWord(int offset) {
if (offset + 1 < data.Length && offset >= 0)
return (data[offset + 1] << 8) | data[offset];
else
return 0;
}
protected string GetString(int offset) {
if (offset < data.Length && data[offset] > 0 &&
data[offset] <= strings.Length)
return strings[data[offset] - 1];
else
return "";
}
public Structure(byte type, ushort handle, byte[] data, string[] strings)
{
this.type = type;
this.handle = handle;
this.data = data;
this.strings = strings;
}
public byte Type { get { return type; } }
public ushort Handle { get { return handle; } }
}
public class BIOSInformation : Structure {
private readonly string vendor;
private readonly string version;
public BIOSInformation(string vendor, string version)
: base (0x00, 0, null, null)
{
this.vendor = vendor;
this.version = version;
}
public BIOSInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings)
{
this.vendor = GetString(0x04);
this.version = GetString(0x05);
}
public string Vendor { get { return vendor; } }
public string Version { get { return version; } }
}
public class SystemInformation : Structure {
private readonly string manufacturerName;
private readonly string productName;
private readonly string version;
private readonly string serialNumber;
private readonly string family;
public SystemInformation(string manufacturerName, string productName,
string version, string serialNumber, string family)
: base (0x01, 0, null, null)
{
this.manufacturerName = manufacturerName;
this.productName = productName;
this.version = version;
this.serialNumber = serialNumber;
this.family = family;
}
public SystemInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings)
{
this.manufacturerName = GetString(0x04);
this.productName = GetString(0x05);
this.version = GetString(0x06);
this.serialNumber = GetString(0x07);
this.family = GetString(0x1A);
}
public string ManufacturerName { get { return manufacturerName; } }
public string ProductName { get { return productName; } }
public string Version { get { return version; } }
public string SerialNumber { get { return serialNumber; } }
public string Family { get { return family; } }
}
public class BaseBoardInformation : Structure {
private readonly string manufacturerName;
private readonly string productName;
private readonly string version;
private readonly string serialNumber;
public BaseBoardInformation(string manufacturerName, string productName,
string version, string serialNumber)
: base(0x02, 0, null, null)
{
this.manufacturerName = manufacturerName;
this.productName = productName;
this.version = version;
this.serialNumber = serialNumber;
}
public BaseBoardInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings) {
this.manufacturerName = GetString(0x04).Trim();
this.productName = GetString(0x05).Trim();
this.version = GetString(0x06).Trim();
this.serialNumber = GetString(0x07).Trim();
}
public string ManufacturerName { get { return manufacturerName; } }
public string ProductName { get { return productName; } }
public string Version { get { return version; } }
public string SerialNumber { get { return serialNumber; } }
}
public class ProcessorInformation : Structure {
public ProcessorInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings)
{
this.ManufacturerName = GetString(0x07).Trim();
this.Version = GetString(0x10).Trim();
this.CoreCount = GetByte(0x23);
this.CoreEnabled = GetByte(0x24);
this.ThreadCount = GetByte(0x25);
this.ExternalClock = GetWord(0x12);
}
public string ManufacturerName { get; private set; }
public string Version { get; private set; }
public int CoreCount { get; private set; }
public int CoreEnabled { get; private set; }
public int ThreadCount { get; private set; }
public int ExternalClock { get; private set; }
}
public class MemoryDevice : Structure {
private readonly string deviceLocator;
private readonly string bankLocator;
private readonly string manufacturerName;
private readonly string serialNumber;
private readonly string partNumber;
private readonly int speed;
public MemoryDevice(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings)
{
this.deviceLocator = GetString(0x10).Trim();
this.bankLocator = GetString(0x11).Trim();
this.manufacturerName = GetString(0x17).Trim();
this.serialNumber = GetString(0x18).Trim();
this.partNumber = GetString(0x1A).Trim();
this.speed = GetWord(0x15);
}
public string DeviceLocator { get { return deviceLocator; } }
public string BankLocator { get { return bankLocator; } }
public string ManufacturerName { get { return manufacturerName; } }
public string SerialNumber { get { return serialNumber; } }
public string PartNumber { get { return partNumber; } }
public int Speed { get { return speed; } }
}
}
}