moel@64: /* moel@64: moel@64: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@64: moel@64: The contents of this file are subject to the Mozilla Public License Version moel@64: 1.1 (the "License"); you may not use this file except in compliance with moel@64: the License. You may obtain a copy of the License at moel@64: moel@64: http://www.mozilla.org/MPL/ moel@64: moel@64: Software distributed under the License is distributed on an "AS IS" basis, moel@64: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@64: for the specific language governing rights and limitations under the License. moel@64: moel@64: The Original Code is the Open Hardware Monitor code. moel@64: moel@64: The Initial Developer of the Original Code is moel@64: Michael Möller . moel@64: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@64: the Initial Developer. All Rights Reserved. moel@64: moel@64: Contributor(s): moel@64: moel@64: Alternatively, the contents of this file may be used under the terms of moel@64: either the GNU General Public License Version 2 or later (the "GPL"), or moel@64: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@64: in which case the provisions of the GPL or the LGPL are applicable instead moel@64: of those above. If you wish to allow use of your version of this file only moel@64: under the terms of either the GPL or the LGPL, and not to allow others to moel@64: use your version of this file under the terms of the MPL, indicate your moel@64: decision by deleting the provisions above and replace them with the notice moel@64: and other provisions required by the GPL or the LGPL. If you do not delete moel@64: the provisions above, a recipient may use your version of this file under moel@64: the terms of any one of the MPL, the GPL or the LGPL. moel@64: moel@64: */ moel@64: moel@64: using System; moel@64: using System.Collections.Generic; moel@64: using System.Management; moel@64: using System.Text; moel@64: moel@64: namespace OpenHardwareMonitor.Hardware.Mainboard { moel@64: moel@64: public class SMBIOS { moel@64: moel@64: private Structure[] table; moel@64: moel@64: private BIOSInformation biosInformation = null; moel@64: private BaseBoardInformation baseBoardInformation = null; moel@64: moel@64: public SMBIOS() { moel@64: int p = (int)System.Environment.OSVersion.Platform; moel@64: if ((p == 4) || (p == 128)) moel@64: return; moel@64: moel@64: List structureList = new List(); moel@64: moel@89: byte[] raw = null; moel@64: try { moel@64: ManagementObjectCollection collection = new ManagementObjectSearcher( moel@64: "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get(); moel@89: moel@64: foreach (ManagementObject mo in collection) { moel@64: raw = (byte[])mo["SMBiosData"]; moel@64: break; moel@64: } moel@103: } catch { } moel@64: moel@89: if (raw != null && raw.Length > 0) { moel@89: int offset = 0; moel@89: byte type = raw[offset]; moel@89: while (offset + 4 < raw.Length && type != 127) { moel@64: moel@89: type = raw[offset]; moel@89: int length = raw[offset + 1]; moel@89: ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]); moel@64: moel@89: if (offset + length > raw.Length) moel@89: break; moel@89: byte[] data = new byte[length]; moel@89: Array.Copy(raw, offset, data, 0, length); moel@89: offset += length; moel@64: moel@89: List stringsList = new List(); moel@89: if (offset < raw.Length && raw[offset] == 0) moel@89: offset++; moel@89: moel@89: while (offset < raw.Length && raw[offset] != 0) { moel@89: StringBuilder sb = new StringBuilder(); moel@89: while (offset < raw.Length && raw[offset] != 0) { moel@89: sb.Append((char)raw[offset]); offset++; moel@64: } moel@64: offset++; moel@89: stringsList.Add(sb.ToString()); moel@89: } moel@89: offset++; moel@89: switch (type) { moel@89: case 0x00: moel@89: this.biosInformation = new BIOSInformation( moel@89: type, handle, data, stringsList.ToArray()); moel@89: structureList.Add(this.biosInformation); break; moel@89: case 0x02: this.baseBoardInformation = new BaseBoardInformation( moel@89: type, handle, data, stringsList.ToArray()); moel@89: structureList.Add(this.baseBoardInformation); break; moel@89: default: structureList.Add(new Structure( moel@89: type, handle, data, stringsList.ToArray())); break; moel@64: } moel@64: } moel@89: } moel@89: moel@64: table = structureList.ToArray(); moel@64: } moel@64: moel@64: public string GetReport() { moel@64: StringBuilder r = new StringBuilder(); moel@64: moel@64: if (biosInformation != null) { moel@64: r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor); moel@64: r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version); moel@64: r.AppendLine(); moel@64: } moel@64: moel@64: if (baseBoardInformation != null) { moel@64: r.Append("Mainboard Manufacturer: "); moel@64: r.AppendLine(baseBoardInformation.ManufacturerName); moel@64: r.Append("Mainboard Name: "); moel@64: r.AppendLine(baseBoardInformation.ProductName); moel@64: r.AppendLine(); moel@64: } moel@64: moel@64: return r.ToString(); moel@64: } moel@64: moel@64: public BIOSInformation BIOS { moel@64: get { return biosInformation; } moel@64: } moel@64: moel@64: public BaseBoardInformation Board { moel@64: get { return baseBoardInformation; } moel@64: } moel@64: moel@64: public class Structure { moel@64: private byte type; moel@64: private ushort handle; moel@64: moel@64: private byte[] data; moel@64: private string[] strings; moel@64: moel@64: protected string GetString(int offset) { moel@64: if (offset < data.Length && data[offset] > 0 && moel@64: data[offset] <= strings.Length) moel@64: return strings[data[offset] - 1]; moel@64: else moel@64: return ""; moel@64: } moel@64: moel@64: public Structure(byte type, ushort handle, byte[] data, string[] strings) moel@64: { moel@64: this.type = type; moel@64: this.handle = handle; moel@64: this.data = data; moel@64: this.strings = strings; moel@64: } moel@64: moel@64: public byte Type { get { return type; } } moel@64: moel@64: public ushort Handle { get { return handle; } } moel@64: } moel@64: moel@64: public class BIOSInformation : Structure { moel@64: moel@64: private string vendor; moel@64: private string version; moel@64: moel@64: public BIOSInformation(byte type, ushort handle, byte[] data, moel@64: string[] strings) moel@64: : base(type, handle, data, strings) { moel@64: moel@64: this.vendor = GetString(0x04); moel@64: this.version = GetString(0x05); moel@64: } moel@64: moel@64: public string Vendor { get { return vendor; } } moel@64: moel@64: public string Version { get { return version; } } moel@64: } moel@64: moel@64: public class BaseBoardInformation : Structure { moel@64: moel@64: private string manufacturerName; moel@64: private string productName; moel@89: private string version; moel@89: private string serialNumber; moel@64: private Manufacturer manufacturer; moel@64: moel@64: public BaseBoardInformation(byte type, ushort handle, byte[] data, moel@64: string[] strings) moel@64: : base(type, handle, data, strings) { moel@64: moel@64: this.manufacturerName = GetString(0x04).Trim(); moel@64: this.productName = GetString(0x05).Trim(); moel@89: this.version = GetString(0x06).Trim(); moel@89: this.serialNumber = GetString(0x07).Trim(); moel@64: moel@64: switch (manufacturerName) { moel@64: case "ASUSTeK Computer INC.": moel@64: manufacturer = Manufacturer.ASUS; break; moel@64: case "DFI": moel@72: case "DFI Inc.": moel@64: manufacturer = Manufacturer.DFI; break; moel@64: case "EPoX COMPUTER CO., LTD": moel@64: manufacturer = Manufacturer.EPoX; break; moel@64: case "Gigabyte Technology Co., Ltd.": moel@64: manufacturer = Manufacturer.Gigabyte; break; moel@72: case "IBM": moel@72: manufacturer = Manufacturer.IBM; break; moel@64: case "MICRO-STAR INTERNATIONAL CO., LTD": moel@72: case "MICRO-STAR INTERNATIONAL CO.,LTD": moel@64: manufacturer = Manufacturer.MSI; break; moel@64: default: moel@64: manufacturer = Manufacturer.Unkown; break; moel@64: } moel@64: } moel@64: moel@64: public string ManufacturerName { get { return manufacturerName; } } moel@64: moel@64: public string ProductName { get { return productName; } } moel@64: moel@89: public string Version { get { return version; } } moel@89: moel@89: public string SerialNumber { get { return serialNumber; } } moel@89: moel@64: public Manufacturer Manufacturer { get { return manufacturer; } } moel@89: moel@64: } moel@64: } moel@64: }