Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sun, 06 Jun 2010 14:44:53 +0000
changeset 135 51a15cf90d22
parent 132 680ed54e05ca
child 136 fa2957aa0699
permissions -rw-r--r--
Added experimental lm-sensors super I/O support for Linux.
moel@64
     1
/*
moel@64
     2
  
moel@64
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@64
     4
moel@64
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@64
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@64
     7
  the License. You may obtain a copy of the License at
moel@64
     8
 
moel@64
     9
  http://www.mozilla.org/MPL/
moel@64
    10
moel@64
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@64
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@64
    13
  for the specific language governing rights and limitations under the License.
moel@64
    14
moel@64
    15
  The Original Code is the Open Hardware Monitor code.
moel@64
    16
moel@64
    17
  The Initial Developer of the Original Code is 
moel@64
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@64
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@64
    20
  the Initial Developer. All Rights Reserved.
moel@64
    21
moel@64
    22
  Contributor(s):
moel@64
    23
moel@64
    24
  Alternatively, the contents of this file may be used under the terms of
moel@64
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@64
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@64
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@64
    28
  of those above. If you wish to allow use of your version of this file only
moel@64
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@64
    30
  use your version of this file under the terms of the MPL, indicate your
moel@64
    31
  decision by deleting the provisions above and replace them with the notice
moel@64
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@64
    33
  the provisions above, a recipient may use your version of this file under
moel@64
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@64
    35
 
moel@64
    36
*/
moel@64
    37
moel@64
    38
using System;
moel@64
    39
using System.Collections.Generic;
moel@64
    40
using System.Management;
moel@64
    41
using System.Text;
moel@64
    42
moel@64
    43
namespace OpenHardwareMonitor.Hardware.Mainboard {
moel@64
    44
moel@64
    45
  public class SMBIOS {
moel@64
    46
moel@64
    47
    private Structure[] table;
moel@64
    48
moel@64
    49
    private BIOSInformation biosInformation = null;
moel@64
    50
    private BaseBoardInformation baseBoardInformation = null;
moel@64
    51
moel@64
    52
    public SMBIOS() {
moel@64
    53
      int p = (int)System.Environment.OSVersion.Platform;
moel@64
    54
      if ((p == 4) || (p == 128))
moel@64
    55
        return;
moel@64
    56
      
moel@64
    57
      List<Structure> structureList = new List<Structure>();
moel@64
    58
moel@89
    59
      byte[] raw = null;
moel@64
    60
      try {
moel@64
    61
        ManagementObjectCollection collection = new ManagementObjectSearcher(
moel@64
    62
          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@89
    63
       
moel@64
    64
        foreach (ManagementObject mo in collection) {
moel@64
    65
          raw = (byte[])mo["SMBiosData"];
moel@64
    66
          break;
moel@64
    67
        }
moel@103
    68
      } catch { }      
moel@64
    69
moel@89
    70
      if (raw != null && raw.Length > 0) {
moel@89
    71
        int offset = 0;
moel@89
    72
        byte type = raw[offset];
moel@89
    73
        while (offset + 4 < raw.Length && type != 127) {
moel@64
    74
moel@89
    75
          type = raw[offset];
moel@89
    76
          int length = raw[offset + 1];
moel@89
    77
          ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
moel@64
    78
moel@89
    79
          if (offset + length > raw.Length)
moel@89
    80
            break;
moel@89
    81
          byte[] data = new byte[length];
moel@89
    82
          Array.Copy(raw, offset, data, 0, length);
moel@89
    83
          offset += length;
moel@64
    84
moel@89
    85
          List<string> stringsList = new List<string>();
moel@89
    86
          if (offset < raw.Length && raw[offset] == 0)
moel@89
    87
            offset++;
moel@89
    88
moel@89
    89
          while (offset < raw.Length && raw[offset] != 0) {
moel@89
    90
            StringBuilder sb = new StringBuilder();
moel@89
    91
            while (offset < raw.Length && raw[offset] != 0) {
moel@89
    92
              sb.Append((char)raw[offset]); offset++;
moel@64
    93
            }
moel@64
    94
            offset++;
moel@89
    95
            stringsList.Add(sb.ToString());
moel@89
    96
          }
moel@89
    97
          offset++;
moel@89
    98
          switch (type) {
moel@89
    99
            case 0x00:
moel@89
   100
              this.biosInformation = new BIOSInformation(
moel@89
   101
                type, handle, data, stringsList.ToArray());
moel@89
   102
              structureList.Add(this.biosInformation); break;
moel@89
   103
            case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@89
   104
                type, handle, data, stringsList.ToArray());
moel@89
   105
              structureList.Add(this.baseBoardInformation); break;
moel@89
   106
            default: structureList.Add(new Structure(
moel@89
   107
              type, handle, data, stringsList.ToArray())); break;
moel@64
   108
          }
moel@64
   109
        }
moel@89
   110
      }
moel@89
   111
            
moel@64
   112
      table = structureList.ToArray();
moel@64
   113
    }
moel@64
   114
moel@64
   115
    public string GetReport() {
moel@64
   116
      StringBuilder r = new StringBuilder();      
moel@64
   117
moel@64
   118
      if (biosInformation != null) {
moel@64
   119
        r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
moel@64
   120
        r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
moel@64
   121
        r.AppendLine();
moel@64
   122
      }
moel@64
   123
moel@64
   124
      if (baseBoardInformation != null) {
moel@64
   125
        r.Append("Mainboard Manufacturer: "); 
moel@64
   126
        r.AppendLine(baseBoardInformation.ManufacturerName);
moel@64
   127
        r.Append("Mainboard Name: "); 
moel@64
   128
        r.AppendLine(baseBoardInformation.ProductName);
moel@64
   129
        r.AppendLine();
moel@64
   130
      }
moel@64
   131
     
moel@64
   132
      return r.ToString();
moel@64
   133
    }
moel@64
   134
moel@64
   135
    public BIOSInformation BIOS {
moel@64
   136
      get { return biosInformation; }
moel@64
   137
    }
moel@64
   138
moel@64
   139
    public BaseBoardInformation Board {
moel@64
   140
      get { return baseBoardInformation; }
moel@64
   141
    }
moel@64
   142
moel@64
   143
    public class Structure {
moel@64
   144
      private byte type;
moel@64
   145
      private ushort handle;
moel@64
   146
moel@64
   147
      private byte[] data;
moel@64
   148
      private string[] strings;
moel@64
   149
moel@64
   150
      protected string GetString(int offset) {
moel@64
   151
        if (offset < data.Length && data[offset] > 0 &&
moel@64
   152
         data[offset] <= strings.Length)
moel@64
   153
          return strings[data[offset] - 1];
moel@64
   154
        else
moel@64
   155
          return "";
moel@64
   156
      }
moel@64
   157
moel@64
   158
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@64
   159
      {
moel@64
   160
        this.type = type;
moel@64
   161
        this.handle = handle;
moel@64
   162
        this.data = data;
moel@64
   163
        this.strings = strings;
moel@64
   164
      }
moel@64
   165
moel@64
   166
      public byte Type { get { return type; } }
moel@64
   167
moel@64
   168
      public ushort Handle { get { return handle; } }
moel@64
   169
    }
moel@64
   170
moel@64
   171
    public class BIOSInformation : Structure {
moel@64
   172
moel@64
   173
      private string vendor;
moel@64
   174
      private string version;
moel@64
   175
moel@64
   176
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64
   177
        string[] strings)
moel@64
   178
        : base(type, handle, data, strings) {
moel@64
   179
moel@64
   180
        this.vendor = GetString(0x04);
moel@64
   181
        this.version = GetString(0x05);
moel@64
   182
      }
moel@64
   183
moel@64
   184
      public string Vendor { get { return vendor; } }
moel@64
   185
moel@64
   186
      public string Version { get { return version; } }
moel@64
   187
    }
moel@64
   188
moel@64
   189
    public class BaseBoardInformation : Structure {
moel@64
   190
moel@64
   191
      private string manufacturerName;
moel@64
   192
      private string productName;
moel@89
   193
      private string version;
moel@89
   194
      private string serialNumber;
moel@64
   195
      private Manufacturer manufacturer;
moel@126
   196
      private Model model;
moel@64
   197
moel@64
   198
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@64
   199
        string[] strings)
moel@64
   200
        : base(type, handle, data, strings) {
moel@64
   201
moel@64
   202
        this.manufacturerName = GetString(0x04).Trim();
moel@64
   203
        this.productName = GetString(0x05).Trim();
moel@89
   204
        this.version = GetString(0x06).Trim();
moel@89
   205
        this.serialNumber = GetString(0x07).Trim();
moel@64
   206
moel@64
   207
        switch (manufacturerName) {
moel@64
   208
          case "ASUSTeK Computer INC.":
moel@64
   209
            manufacturer = Manufacturer.ASUS; break;
moel@64
   210
          case "DFI":
moel@72
   211
          case "DFI Inc.":            
moel@64
   212
            manufacturer = Manufacturer.DFI; break;
moel@64
   213
          case "EPoX COMPUTER CO., LTD":
moel@64
   214
            manufacturer = Manufacturer.EPoX; break;
moel@132
   215
          case "EVGA":
moel@132
   216
            manufacturer = Manufacturer.EVGA; break;
moel@64
   217
          case "Gigabyte Technology Co., Ltd.":
moel@64
   218
            manufacturer = Manufacturer.Gigabyte; break;
moel@72
   219
          case "IBM":
moel@72
   220
            manufacturer = Manufacturer.IBM; break;
moel@64
   221
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   222
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64
   223
            manufacturer = Manufacturer.MSI; break;
moel@64
   224
          default:
moel@126
   225
            manufacturer = Manufacturer.Unknown; break;
moel@126
   226
        }
moel@126
   227
moel@126
   228
        switch (productName) {
moel@133
   229
          case "Crosshair III Formula":
moel@133
   230
            model = Model.Crosshair_III_Formula; break;
moel@133
   231
          case "M2N-SLI DELUXE":
moel@133
   232
            model = Model.M2N_SLI_DELUXE; break;
moel@130
   233
          case "P5W DH Deluxe":
moel@130
   234
            model = Model.P5W_DH_Deluxe; break;
moel@126
   235
          case "LP BI P45-T2RS Elite":
moel@126
   236
            model = Model.LP_BI_P45_T2RS_Elite; break;
moel@126
   237
          case "LP DK P55-T3eH9":
moel@126
   238
            model = Model.LP_DK_P55_T3eH9; break;
moel@132
   239
          case "X58 SLI Classified":
moel@132
   240
            model = Model.X58_SLI_Classified; break;
moel@130
   241
          case "965P-S3":
moel@130
   242
            model = Model._965P_S3; break;
moel@126
   243
          case "EP45-DS3R":
moel@126
   244
            model = Model.EP45_DS3R; break;
moel@130
   245
          case "EP45-UD3R":
moel@130
   246
            model = Model.EP45_UD3R; break;
moel@133
   247
          case "EX58-EXTREME":
moel@133
   248
            model = Model.EX58_EXTREME; break;
moel@126
   249
          case "GA-MA785GMT-UD2H":
moel@126
   250
            model = Model.GA_MA785GMT_UD2H; break;
moel@126
   251
          case "P35-DS3":
moel@126
   252
            model = Model.P35_DS3; break;
moel@133
   253
          case "P35-DS3L":
moel@133
   254
            model = Model.P35_DS3L; break;
moel@130
   255
          case "X38-DS5":
moel@130
   256
            model = Model.X38_DS5; break;
moel@126
   257
          default:
moel@126
   258
            model = Model.Unknown; break;
moel@64
   259
        }
moel@64
   260
      }
moel@64
   261
moel@64
   262
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   263
moel@64
   264
      public string ProductName { get { return productName; } }
moel@64
   265
moel@89
   266
      public string Version { get { return version; } }
moel@89
   267
moel@89
   268
      public string SerialNumber { get { return serialNumber; } }
moel@89
   269
moel@64
   270
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   271
moel@126
   272
      public Model Model { get { return model; } }
moel@126
   273
moel@64
   274
    }
moel@64
   275
  }
moel@64
   276
}