Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sat, 27 Feb 2010 20:26:05 +0000
changeset 65 d94a9b712b64
child 72 c4dfd596a6c3
permissions -rw-r--r--
Fixed the version number in the AboutBox.
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@64
    59
      try {
moel@64
    60
        ManagementObjectCollection collection = new ManagementObjectSearcher(
moel@64
    61
          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@64
    62
moel@64
    63
        byte[] raw = null;
moel@64
    64
        foreach (ManagementObject mo in collection) {
moel@64
    65
          raw = (byte[])mo["SMBiosData"];
moel@64
    66
          break;
moel@64
    67
        }
moel@64
    68
moel@64
    69
        if (raw != null && raw.Length > 0) {
moel@64
    70
          int offset = 0;
moel@64
    71
          byte type = raw[offset];
moel@64
    72
          while (offset < raw.Length && type != 127) {
moel@64
    73
moel@64
    74
            type = raw[offset]; offset++;
moel@64
    75
            int length = raw[offset]; offset++;
moel@64
    76
            ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]);
moel@64
    77
            offset += 2;
moel@64
    78
moel@64
    79
            byte[] data = new byte[length];
moel@64
    80
            Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
moel@64
    81
moel@64
    82
            List<string> stringsList = new List<string>();
moel@64
    83
            if (raw[offset] == 0)
moel@64
    84
              offset++;
moel@64
    85
            while (raw[offset] != 0) {
moel@64
    86
              StringBuilder sb = new StringBuilder();
moel@64
    87
              while (raw[offset] != 0) {
moel@64
    88
                sb.Append((char)raw[offset]); offset++;
moel@64
    89
              }
moel@64
    90
              offset++;
moel@64
    91
              stringsList.Add(sb.ToString());
moel@64
    92
            }
moel@64
    93
            offset++;
moel@64
    94
            switch (type) {
moel@64
    95
              case 0x00:
moel@64
    96
                this.biosInformation = new BIOSInformation(
moel@64
    97
                  type, handle, data, stringsList.ToArray());
moel@64
    98
                structureList.Add(this.biosInformation); break;
moel@64
    99
              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@64
   100
                  type, handle, data, stringsList.ToArray());
moel@64
   101
                structureList.Add(this.baseBoardInformation); break;
moel@64
   102
              default: structureList.Add(new Structure(
moel@64
   103
                type, handle, data, stringsList.ToArray())); break;
moel@64
   104
            }
moel@64
   105
          }
moel@64
   106
        }
moel@64
   107
      } catch (NotImplementedException) { } catch (ManagementException) { }
moel@64
   108
      
moel@64
   109
      table = structureList.ToArray();
moel@64
   110
    }
moel@64
   111
moel@64
   112
    public string GetReport() {
moel@64
   113
      StringBuilder r = new StringBuilder();      
moel@64
   114
moel@64
   115
      if (biosInformation != null) {
moel@64
   116
        r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
moel@64
   117
        r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
moel@64
   118
        r.AppendLine();
moel@64
   119
      }
moel@64
   120
moel@64
   121
      if (baseBoardInformation != null) {
moel@64
   122
        r.Append("Mainboard Manufacturer: "); 
moel@64
   123
        r.AppendLine(baseBoardInformation.ManufacturerName);
moel@64
   124
        r.Append("Mainboard Name: "); 
moel@64
   125
        r.AppendLine(baseBoardInformation.ProductName);
moel@64
   126
        r.AppendLine();
moel@64
   127
      }
moel@64
   128
     
moel@64
   129
      return r.ToString();
moel@64
   130
    }
moel@64
   131
moel@64
   132
    public BIOSInformation BIOS {
moel@64
   133
      get { return biosInformation; }
moel@64
   134
    }
moel@64
   135
moel@64
   136
    public BaseBoardInformation Board {
moel@64
   137
      get { return baseBoardInformation; }
moel@64
   138
    }
moel@64
   139
moel@64
   140
    public class Structure {
moel@64
   141
      private byte type;
moel@64
   142
      private ushort handle;
moel@64
   143
moel@64
   144
      private byte[] data;
moel@64
   145
      private string[] strings;
moel@64
   146
moel@64
   147
      protected string GetString(int offset) {
moel@64
   148
        if (offset < data.Length && data[offset] > 0 &&
moel@64
   149
         data[offset] <= strings.Length)
moel@64
   150
          return strings[data[offset] - 1];
moel@64
   151
        else
moel@64
   152
          return "";
moel@64
   153
      }
moel@64
   154
moel@64
   155
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@64
   156
      {
moel@64
   157
        this.type = type;
moel@64
   158
        this.handle = handle;
moel@64
   159
        this.data = data;
moel@64
   160
        this.strings = strings;
moel@64
   161
      }
moel@64
   162
moel@64
   163
      public byte Type { get { return type; } }
moel@64
   164
moel@64
   165
      public ushort Handle { get { return handle; } }
moel@64
   166
    }
moel@64
   167
moel@64
   168
    public class BIOSInformation : Structure {
moel@64
   169
moel@64
   170
      private string vendor;
moel@64
   171
      private string version;
moel@64
   172
moel@64
   173
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64
   174
        string[] strings)
moel@64
   175
        : base(type, handle, data, strings) {
moel@64
   176
moel@64
   177
        this.vendor = GetString(0x04);
moel@64
   178
        this.version = GetString(0x05);
moel@64
   179
      }
moel@64
   180
moel@64
   181
      public string Vendor { get { return vendor; } }
moel@64
   182
moel@64
   183
      public string Version { get { return version; } }
moel@64
   184
    }
moel@64
   185
moel@64
   186
    public class BaseBoardInformation : Structure {
moel@64
   187
moel@64
   188
      private string manufacturerName;
moel@64
   189
      private string productName;
moel@64
   190
      private Manufacturer manufacturer;
moel@64
   191
moel@64
   192
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@64
   193
        string[] strings)
moel@64
   194
        : base(type, handle, data, strings) {
moel@64
   195
moel@64
   196
        this.manufacturerName = GetString(0x04).Trim();
moel@64
   197
        this.productName = GetString(0x05).Trim();
moel@64
   198
moel@64
   199
        switch (manufacturerName) {
moel@64
   200
          case "ASUSTeK Computer INC.":
moel@64
   201
            manufacturer = Manufacturer.ASUS; break;
moel@64
   202
          case "DFI":
moel@64
   203
          case "DFI Inc.:":
moel@64
   204
            manufacturer = Manufacturer.DFI; break;
moel@64
   205
          case "EPoX COMPUTER CO., LTD":
moel@64
   206
            manufacturer = Manufacturer.EPoX; break;
moel@64
   207
          case "Gigabyte Technology Co., Ltd.":
moel@64
   208
            manufacturer = Manufacturer.Gigabyte; break;
moel@64
   209
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@64
   210
            manufacturer = Manufacturer.MSI; break;
moel@64
   211
          default:
moel@64
   212
            manufacturer = Manufacturer.Unkown; break;
moel@64
   213
        }
moel@64
   214
      }
moel@64
   215
moel@64
   216
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   217
moel@64
   218
      public string ProductName { get { return productName; } }
moel@64
   219
moel@64
   220
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@64
   221
    }
moel@64
   222
  }
moel@64
   223
}