Hardware/Mainboard/SMBIOS.cs
author moel.mich
Tue, 25 May 2010 18:57:28 +0000
changeset 127 76aaf45a01c7
parent 103 0845adc5402e
child 130 80065ab20b84
permissions -rw-r--r--
Added a workaround for the "You must keep the stream open for the lifetime of the Image." problem of the Image.FromStream method. This also reduced the overall memory usage (private working set).
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@64
   215
          case "Gigabyte Technology Co., Ltd.":
moel@64
   216
            manufacturer = Manufacturer.Gigabyte; break;
moel@72
   217
          case "IBM":
moel@72
   218
            manufacturer = Manufacturer.IBM; break;
moel@64
   219
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   220
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64
   221
            manufacturer = Manufacturer.MSI; break;
moel@64
   222
          default:
moel@126
   223
            manufacturer = Manufacturer.Unknown; break;
moel@126
   224
        }
moel@126
   225
moel@126
   226
        switch (productName) {
moel@126
   227
          case "LP BI P45-T2RS Elite":
moel@126
   228
            model = Model.LP_BI_P45_T2RS_Elite; break;
moel@126
   229
          case "LP DK P55-T3eH9":
moel@126
   230
            model = Model.LP_DK_P55_T3eH9; break;
moel@126
   231
          case "EP45-DS3R":
moel@126
   232
            model = Model.EP45_DS3R; break;
moel@126
   233
          case "GA-MA785GMT-UD2H":
moel@126
   234
            model = Model.GA_MA785GMT_UD2H; break;
moel@126
   235
          case "P35-DS3":
moel@126
   236
            model = Model.P35_DS3; break;
moel@126
   237
          default:
moel@126
   238
            model = Model.Unknown; break;
moel@64
   239
        }
moel@64
   240
      }
moel@64
   241
moel@64
   242
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   243
moel@64
   244
      public string ProductName { get { return productName; } }
moel@64
   245
moel@89
   246
      public string Version { get { return version; } }
moel@89
   247
moel@89
   248
      public string SerialNumber { get { return serialNumber; } }
moel@89
   249
moel@64
   250
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   251
moel@126
   252
      public Model Model { get { return model; } }
moel@126
   253
moel@64
   254
    }
moel@64
   255
  }
moel@64
   256
}