443 lines
15 KiB
C#
Raw Normal View History

/*
2010-01-26 22:37:48 +00:00
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is the Open Hardware Monitor code.
The Initial Developer of the Original Code is
Michael Möller <m.moeller@gmx.ch>.
2011-03-19 22:55:05 +00:00
Portions created by the Initial Developer are Copyright (C) 2009-2011
2010-01-26 22:37:48 +00:00
the Initial Developer. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
*/
using System;
using System.Collections.Generic;
using System.IO;
2010-01-26 22:37:48 +00:00
using System.Management;
using System.Text;
namespace OpenHardwareMonitor.Hardware.Mainboard {
2010-01-26 22:37:48 +00:00
internal class SMBIOS {
2010-01-26 22:37:48 +00:00
private readonly byte[] raw;
private readonly Structure[] table;
2010-01-26 22:37:48 +00:00
2010-11-13 20:20:03 +00:00
private readonly Version version;
private readonly BIOSInformation biosInformation;
private readonly BaseBoardInformation baseBoardInformation;
2010-01-26 22:37:48 +00:00
2010-08-15 14:46:58 +00:00
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 biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
} else {
List<Structure> structureList = new List<Structure>();
raw = null;
2010-11-13 20:20:03 +00:00
byte majorVersion = 0;
byte minorVersion = 0;
try {
2010-08-15 14:46:58 +00:00
ManagementObjectCollection collection;
using (ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
2010-11-13 20:20:03 +00:00
"SELECT * FROM MSSMBios_RawSMBiosTables")) {
2010-08-15 14:46:58 +00:00
collection = searcher.Get();
}
foreach (ManagementObject mo in collection) {
raw = (byte[])mo["SMBiosData"];
2010-11-13 20:20:03 +00:00
majorVersion = (byte)mo["SmbiosMajorVersion"];
minorVersion = (byte)mo["SmbiosMinorVersion"];
2010-04-05 21:31:21 +00:00
break;
}
} catch { }
2010-11-13 20:20:03 +00:00
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++;
2010-04-05 21:31:21 +00:00
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());
}
2010-04-05 21:31:21 +00:00
offset++;
switch (type) {
case 0x00:
this.biosInformation = new BIOSInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.biosInformation); break;
case 0x02: this.baseBoardInformation = new BaseBoardInformation(
type, handle, data, stringsList.ToArray());
structureList.Add(this.baseBoardInformation); break;
default: structureList.Add(new Structure(
type, handle, data, stringsList.ToArray())); break;
}
2010-01-26 22:37:48 +00:00
}
}
table = structureList.ToArray();
2010-04-05 21:31:21 +00:00
}
2010-01-26 22:37:48 +00:00
}
public string GetReport() {
StringBuilder r = new StringBuilder();
2010-01-26 22:37:48 +00:00
2010-11-13 20:20:03 +00:00
if (version != null) {
r.Append("SMBIOS Version: "); r.AppendLine(version.ToString(2));
r.AppendLine();
}
2010-08-15 14:46:58 +00:00
if (BIOS != null) {
r.Append("BIOS Vendor: "); r.AppendLine(BIOS.Vendor);
r.Append("BIOS Version: "); r.AppendLine(BIOS.Version);
2010-01-26 22:37:48 +00:00
r.AppendLine();
}
2010-08-15 14:46:58 +00:00
if (Board != null) {
r.Append("Mainboard Manufacturer: ");
2010-08-15 14:46:58 +00:00
r.AppendLine(Board.ManufacturerName);
r.Append("Mainboard Name: ");
2010-08-15 14:46:58 +00:00
r.AppendLine(Board.ProductName);
r.Append("Mainboard Version: ");
r.AppendLine(Board.Version);
2010-01-26 22:37:48 +00:00
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();
}
2010-01-26 22:37:48 +00:00
return r.ToString();
}
public BIOSInformation BIOS {
get { return biosInformation; }
}
public BaseBoardInformation Board {
get { return baseBoardInformation; }
}
2010-01-26 22:37:48 +00:00
public class Structure {
private readonly byte type;
private readonly ushort handle;
2010-01-26 22:37:48 +00:00
private readonly byte[] data;
private readonly string[] strings;
2010-01-26 22:37:48 +00:00
protected string GetString(int offset) {
if (offset < data.Length && data[offset] > 0 &&
data[offset] <= strings.Length)
return strings[data[offset] - 1];
else
return "";
}
2010-01-26 22:37:48 +00:00
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; } }
}
2010-01-26 22:37:48 +00:00
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;
}
2010-01-26 22:37:48 +00:00
public BIOSInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings)
{
this.vendor = GetString(0x04);
this.version = GetString(0x05);
2010-01-26 22:37:48 +00:00
}
public string Vendor { get { return vendor; } }
public string Version { get { return version; } }
}
public class BaseBoardInformation : Structure {
private readonly string manufacturerName;
private readonly string productName;
private readonly string version;
private readonly string serialNumber;
private readonly Manufacturer manufacturer;
private readonly Model model;
2010-01-26 22:37:48 +00:00
private static Manufacturer GetManufacturer(string name) {
2010-08-15 14:46:58 +00:00
switch (name) {
case "Alienware":
return Manufacturer.Alienware;
case "Apple Inc.":
return Manufacturer.Apple;
case "ASRock":
return Manufacturer.ASRock;
case "ASUSTeK Computer INC.":
return Manufacturer.ASUS;
case "Dell Inc.":
return Manufacturer.Dell;
case "DFI":
2010-03-04 20:26:56 +00:00
case "DFI Inc.":
return Manufacturer.DFI;
case "ECS":
return Manufacturer.ECS;
case "EPoX COMPUTER CO., LTD":
return Manufacturer.EPoX;
2010-06-05 11:15:16 +00:00
case "EVGA":
return Manufacturer.EVGA;
case "First International Computer, Inc.":
return Manufacturer.FIC;
case "FUJITSU":
case "FUJITSU SIEMENS":
return Manufacturer.Fujitsu;
case "Gigabyte Technology Co., Ltd.":
return Manufacturer.Gigabyte;
case "Hewlett-Packard":
return Manufacturer.HP;
2010-03-04 20:26:56 +00:00
case "IBM":
return Manufacturer.IBM;
case "Intel":
case "Intel Corp.":
case "Intel Corporation":
case "INTEL Corporation":
return Manufacturer.Intel;
case "Lenovo":
case "LENOVO":
return Manufacturer.Lenovo;
case "Micro-Star International":
case "MICRO-STAR INTERNATIONAL CO., LTD":
2010-03-04 20:26:56 +00:00
case "MICRO-STAR INTERNATIONAL CO.,LTD":
case "MSI":
return Manufacturer.MSI;
case "Supermicro":
return Manufacturer.Supermicro;
case "TOSHIBA":
return Manufacturer.Toshiba;
case "XFX":
return Manufacturer.XFX;
case "To be filled by O.E.M.":
return Manufacturer.Unknown;
default:
return Manufacturer.Unknown;
}
}
private static Model GetModel(string name) {
2010-08-15 14:46:58 +00:00
switch (name) {
case "880GMH/USB3":
return Model._880GMH_USB3;
case "ASRock AOD790GX/128M":
return Model.AOD790GX_128M;
case "P55 Deluxe":
return Model.P55_Deluxe;
2010-06-05 18:59:54 +00:00
case "Crosshair III Formula":
return Model.Crosshair_III_Formula;
2010-06-05 18:59:54 +00:00
case "M2N-SLI DELUXE":
return Model.M2N_SLI_DELUXE;
case "M4A79XTD EVO":
return Model.M4A79XTD_EVO;
2010-06-03 22:40:18 +00:00
case "P5W DH Deluxe":
return Model.P5W_DH_Deluxe;
case "P6X58D-E":
return Model.P6X58D_E;
case "P8P67":
return Model.P8P67;
case "P8P67 EVO":
return Model.P8P67_EVO;
2011-03-19 22:55:05 +00:00
case "P8P67 PRO":
return Model.P8P67_PRO;
case "P8P67-M PRO":
return Model.P8P67_M_PRO;
2010-08-24 20:14:54 +00:00
case "Rampage Extreme":
return Model.Rampage_Extreme;
2010-08-24 20:14:54 +00:00
case "Rampage II GENE":
return Model.Rampage_II_GENE;
case "LP BI P45-T2RS Elite":
return Model.LP_BI_P45_T2RS_Elite;
case "LP DK P55-T3eH9":
return Model.LP_DK_P55_T3eH9;
case "A890GXM-A":
return Model.A890GXM_A;
2010-06-05 11:15:16 +00:00
case "X58 SLI Classified":
return Model.X58_SLI_Classified;
2010-06-03 22:40:18 +00:00
case "965P-S3":
return Model._965P_S3;
case "EP45-DS3R":
return Model.EP45_DS3R;
2010-06-03 22:40:18 +00:00
case "EP45-UD3R":
return Model.EP45_UD3R;
2010-06-05 18:59:54 +00:00
case "EX58-EXTREME":
return Model.EX58_EXTREME;
2010-07-14 21:10:26 +00:00
case "GA-MA770T-UD3":
return Model.GA_MA770T_UD3;
case "GA-MA785GMT-UD2H":
return Model.GA_MA785GMT_UD2H;
case "H67A-UD3H-B3":
return Model.H67A_UD3H_B3;
case "P35-DS3":
return Model.P35_DS3;
2010-06-05 18:59:54 +00:00
case "P35-DS3L":
return Model.P35_DS3L;
case "P55-UD4":
return Model.P55_UD4;
2010-08-15 17:56:57 +00:00
case "P55M-UD4":
return Model.P55M_UD4;
case "P67A-UD4-B3":
return Model.P67A_UD4_B3;
2010-06-03 22:40:18 +00:00
case "X38-DS5":
return Model.X38_DS5;
case "X58A-UD3R":
return Model.X58A_UD3R;
case "Z68X-UD7-B3":
return Model.Z68X_UD7_B3;
case "Base Board Product Name":
case "To be filled by O.E.M.":
return Model.Unknown;
default:
return Model.Unknown;
}
2010-01-26 22:37:48 +00:00
}
public BaseBoardInformation(string manufacturerName, string productName,
string version, string serialNumber)
: base(0x02, 0, null, null)
{
this.manufacturerName = manufacturerName;
this.manufacturer = GetManufacturer(manufacturerName);
this.productName = productName;
this.model = GetModel(productName);
this.version = version;
this.serialNumber = serialNumber;
}
public BaseBoardInformation(byte type, ushort handle, byte[] data,
string[] strings)
: base(type, handle, data, strings) {
2010-01-26 22:37:48 +00:00
this.manufacturerName = GetString(0x04).Trim();
this.manufacturer = GetManufacturer(this.manufacturerName);
this.productName = GetString(0x05).Trim();
this.model = GetModel(this.productName);
this.version = GetString(0x06).Trim();
this.serialNumber = GetString(0x07).Trim();
}
public string ManufacturerName { get { return manufacturerName; } }
2010-01-26 22:37:48 +00:00
public string ProductName { get { return productName; } }
2010-04-05 21:31:21 +00:00
public string Version { get { return version; } }
public string SerialNumber { get { return serialNumber; } }
public Manufacturer Manufacturer { get { return manufacturer; } }
2010-04-05 21:31:21 +00:00
public Model Model { get { return model; } }
2010-01-26 22:37:48 +00:00
}
}
}