Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sun, 25 Apr 2010 14:40:19 +0000
changeset 97 1a2d7dad320e
parent 72 c4dfd596a6c3
child 103 0845adc5402e
permissions -rw-r--r--
Fixed Issue 43.
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@89
    68
      } catch (NotImplementedException) { } catch (ManagementException) { }
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@64
   196
moel@64
   197
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@64
   198
        string[] strings)
moel@64
   199
        : base(type, handle, data, strings) {
moel@64
   200
moel@64
   201
        this.manufacturerName = GetString(0x04).Trim();
moel@64
   202
        this.productName = GetString(0x05).Trim();
moel@89
   203
        this.version = GetString(0x06).Trim();
moel@89
   204
        this.serialNumber = GetString(0x07).Trim();
moel@64
   205
moel@64
   206
        switch (manufacturerName) {
moel@64
   207
          case "ASUSTeK Computer INC.":
moel@64
   208
            manufacturer = Manufacturer.ASUS; break;
moel@64
   209
          case "DFI":
moel@72
   210
          case "DFI Inc.":            
moel@64
   211
            manufacturer = Manufacturer.DFI; break;
moel@64
   212
          case "EPoX COMPUTER CO., LTD":
moel@64
   213
            manufacturer = Manufacturer.EPoX; break;
moel@64
   214
          case "Gigabyte Technology Co., Ltd.":
moel@64
   215
            manufacturer = Manufacturer.Gigabyte; break;
moel@72
   216
          case "IBM":
moel@72
   217
            manufacturer = Manufacturer.IBM; break;
moel@64
   218
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   219
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64
   220
            manufacturer = Manufacturer.MSI; break;
moel@64
   221
          default:
moel@64
   222
            manufacturer = Manufacturer.Unkown; break;
moel@64
   223
        }
moel@64
   224
      }
moel@64
   225
moel@64
   226
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   227
moel@64
   228
      public string ProductName { get { return productName; } }
moel@64
   229
moel@89
   230
      public string Version { get { return version; } }
moel@89
   231
moel@89
   232
      public string SerialNumber { get { return serialNumber; } }
moel@89
   233
moel@64
   234
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   235
moel@64
   236
    }
moel@64
   237
  }
moel@64
   238
}