Hardware/Mainboard/SMBIOS.cs
changeset 64 15181001ee61
child 72 c4dfd596a6c3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/Mainboard/SMBIOS.cs	Sat Feb 27 20:08:13 2010 +0000
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Management;
    1.44 +using System.Text;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware.Mainboard {
    1.47 +
    1.48 +  public class SMBIOS {
    1.49 +
    1.50 +    private Structure[] table;
    1.51 +
    1.52 +    private BIOSInformation biosInformation = null;
    1.53 +    private BaseBoardInformation baseBoardInformation = null;
    1.54 +
    1.55 +    public SMBIOS() {
    1.56 +      int p = (int)System.Environment.OSVersion.Platform;
    1.57 +      if ((p == 4) || (p == 128))
    1.58 +        return;
    1.59 +      
    1.60 +      List<Structure> structureList = new List<Structure>();
    1.61 +
    1.62 +      try {
    1.63 +        ManagementObjectCollection collection = new ManagementObjectSearcher(
    1.64 +          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    1.65 +
    1.66 +        byte[] raw = null;
    1.67 +        foreach (ManagementObject mo in collection) {
    1.68 +          raw = (byte[])mo["SMBiosData"];
    1.69 +          break;
    1.70 +        }
    1.71 +
    1.72 +        if (raw != null && raw.Length > 0) {
    1.73 +          int offset = 0;
    1.74 +          byte type = raw[offset];
    1.75 +          while (offset < raw.Length && type != 127) {
    1.76 +
    1.77 +            type = raw[offset]; offset++;
    1.78 +            int length = raw[offset]; offset++;
    1.79 +            ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]);
    1.80 +            offset += 2;
    1.81 +
    1.82 +            byte[] data = new byte[length];
    1.83 +            Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
    1.84 +
    1.85 +            List<string> stringsList = new List<string>();
    1.86 +            if (raw[offset] == 0)
    1.87 +              offset++;
    1.88 +            while (raw[offset] != 0) {
    1.89 +              StringBuilder sb = new StringBuilder();
    1.90 +              while (raw[offset] != 0) {
    1.91 +                sb.Append((char)raw[offset]); offset++;
    1.92 +              }
    1.93 +              offset++;
    1.94 +              stringsList.Add(sb.ToString());
    1.95 +            }
    1.96 +            offset++;
    1.97 +            switch (type) {
    1.98 +              case 0x00:
    1.99 +                this.biosInformation = new BIOSInformation(
   1.100 +                  type, handle, data, stringsList.ToArray());
   1.101 +                structureList.Add(this.biosInformation); break;
   1.102 +              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   1.103 +                  type, handle, data, stringsList.ToArray());
   1.104 +                structureList.Add(this.baseBoardInformation); break;
   1.105 +              default: structureList.Add(new Structure(
   1.106 +                type, handle, data, stringsList.ToArray())); break;
   1.107 +            }
   1.108 +          }
   1.109 +        }
   1.110 +      } catch (NotImplementedException) { } catch (ManagementException) { }
   1.111 +      
   1.112 +      table = structureList.ToArray();
   1.113 +    }
   1.114 +
   1.115 +    public string GetReport() {
   1.116 +      StringBuilder r = new StringBuilder();      
   1.117 +
   1.118 +      if (biosInformation != null) {
   1.119 +        r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   1.120 +        r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
   1.121 +        r.AppendLine();
   1.122 +      }
   1.123 +
   1.124 +      if (baseBoardInformation != null) {
   1.125 +        r.Append("Mainboard Manufacturer: "); 
   1.126 +        r.AppendLine(baseBoardInformation.ManufacturerName);
   1.127 +        r.Append("Mainboard Name: "); 
   1.128 +        r.AppendLine(baseBoardInformation.ProductName);
   1.129 +        r.AppendLine();
   1.130 +      }
   1.131 +     
   1.132 +      return r.ToString();
   1.133 +    }
   1.134 +
   1.135 +    public BIOSInformation BIOS {
   1.136 +      get { return biosInformation; }
   1.137 +    }
   1.138 +
   1.139 +    public BaseBoardInformation Board {
   1.140 +      get { return baseBoardInformation; }
   1.141 +    }
   1.142 +
   1.143 +    public class Structure {
   1.144 +      private byte type;
   1.145 +      private ushort handle;
   1.146 +
   1.147 +      private byte[] data;
   1.148 +      private string[] strings;
   1.149 +
   1.150 +      protected string GetString(int offset) {
   1.151 +        if (offset < data.Length && data[offset] > 0 &&
   1.152 +         data[offset] <= strings.Length)
   1.153 +          return strings[data[offset] - 1];
   1.154 +        else
   1.155 +          return "";
   1.156 +      }
   1.157 +
   1.158 +      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
   1.159 +      {
   1.160 +        this.type = type;
   1.161 +        this.handle = handle;
   1.162 +        this.data = data;
   1.163 +        this.strings = strings;
   1.164 +      }
   1.165 +
   1.166 +      public byte Type { get { return type; } }
   1.167 +
   1.168 +      public ushort Handle { get { return handle; } }
   1.169 +    }
   1.170 +
   1.171 +    public class BIOSInformation : Structure {
   1.172 +
   1.173 +      private string vendor;
   1.174 +      private string version;
   1.175 +
   1.176 +      public BIOSInformation(byte type, ushort handle, byte[] data,
   1.177 +        string[] strings)
   1.178 +        : base(type, handle, data, strings) {
   1.179 +
   1.180 +        this.vendor = GetString(0x04);
   1.181 +        this.version = GetString(0x05);
   1.182 +      }
   1.183 +
   1.184 +      public string Vendor { get { return vendor; } }
   1.185 +
   1.186 +      public string Version { get { return version; } }
   1.187 +    }
   1.188 +
   1.189 +    public class BaseBoardInformation : Structure {
   1.190 +
   1.191 +      private string manufacturerName;
   1.192 +      private string productName;
   1.193 +      private Manufacturer manufacturer;
   1.194 +
   1.195 +      public BaseBoardInformation(byte type, ushort handle, byte[] data,
   1.196 +        string[] strings)
   1.197 +        : base(type, handle, data, strings) {
   1.198 +
   1.199 +        this.manufacturerName = GetString(0x04).Trim();
   1.200 +        this.productName = GetString(0x05).Trim();
   1.201 +
   1.202 +        switch (manufacturerName) {
   1.203 +          case "ASUSTeK Computer INC.":
   1.204 +            manufacturer = Manufacturer.ASUS; break;
   1.205 +          case "DFI":
   1.206 +          case "DFI Inc.:":
   1.207 +            manufacturer = Manufacturer.DFI; break;
   1.208 +          case "EPoX COMPUTER CO., LTD":
   1.209 +            manufacturer = Manufacturer.EPoX; break;
   1.210 +          case "Gigabyte Technology Co., Ltd.":
   1.211 +            manufacturer = Manufacturer.Gigabyte; break;
   1.212 +          case "MICRO-STAR INTERNATIONAL CO., LTD":
   1.213 +            manufacturer = Manufacturer.MSI; break;
   1.214 +          default:
   1.215 +            manufacturer = Manufacturer.Unkown; break;
   1.216 +        }
   1.217 +      }
   1.218 +
   1.219 +      public string ManufacturerName { get { return manufacturerName; } }
   1.220 +
   1.221 +      public string ProductName { get { return productName; } }
   1.222 +
   1.223 +      public Manufacturer Manufacturer { get { return manufacturer; } }
   1.224 +    }
   1.225 +  }
   1.226 +}