Hardware/Mainboard/SMBIOS.cs
author moel.mich
Tue, 08 Jun 2010 21:07:13 +0000
changeset 138 1301992d8ae5
parent 136 fa2957aa0699
child 144 44a688398012
permissions -rw-r--r--
Added a mainboard specific configuration for the Gigabyte GA-X58A-UD3R.
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@136
    40
using System.IO;
moel@64
    41
using System.Management;
moel@64
    42
using System.Text;
moel@64
    43
moel@64
    44
namespace OpenHardwareMonitor.Hardware.Mainboard {
moel@64
    45
moel@64
    46
  public class SMBIOS {
moel@64
    47
moel@136
    48
    private byte[] raw;
moel@64
    49
    private Structure[] table;
moel@64
    50
moel@64
    51
    private BIOSInformation biosInformation = null;
moel@64
    52
    private BaseBoardInformation baseBoardInformation = null;
moel@64
    53
moel@136
    54
    private string ReadSysFS(string path) {
moel@136
    55
      try {
moel@136
    56
        if (File.Exists(path)) {
moel@136
    57
          using (StreamReader reader = new StreamReader(path)) 
moel@136
    58
            return reader.ReadLine();
moel@136
    59
        } else {
moel@136
    60
          return null;
moel@136
    61
        }
moel@136
    62
      } catch {
moel@136
    63
        return null;
moel@136
    64
      }
moel@136
    65
    }
moel@136
    66
    
moel@64
    67
    public SMBIOS() {
moel@64
    68
      int p = (int)System.Environment.OSVersion.Platform;
moel@136
    69
      if ((p == 4) || (p == 128)) {
moel@136
    70
        this.raw = null;
moel@136
    71
        this.table = null;
moel@136
    72
        
moel@136
    73
        string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
moel@136
    74
        string boardName = ReadSysFS("/sys/class/dmi/id/board_name");        
moel@136
    75
        string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");        
moel@136
    76
        this.baseBoardInformation = new BaseBoardInformation(
moel@136
    77
          boardVendor, boardName, boardVersion, null);
moel@136
    78
        
moel@136
    79
        string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
moel@136
    80
        string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
moel@136
    81
        this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
moel@136
    82
        
moel@136
    83
      } else {              
moel@136
    84
        List<Structure> structureList = new List<Structure>();
moel@64
    85
moel@136
    86
        raw = null;
moel@136
    87
        try {
moel@136
    88
          ManagementObjectCollection collection = 
moel@136
    89
            new ManagementObjectSearcher("root\\WMI", 
moel@136
    90
              "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@136
    91
         
moel@136
    92
          foreach (ManagementObject mo in collection) {
moel@136
    93
            raw = (byte[])mo["SMBiosData"];
moel@89
    94
            break;
moel@136
    95
          }
moel@136
    96
        } catch { }
moel@136
    97
  
moel@136
    98
        if (raw != null && raw.Length > 0) {
moel@136
    99
          int offset = 0;
moel@136
   100
          byte type = raw[offset];
moel@136
   101
          while (offset + 4 < raw.Length && type != 127) {
moel@136
   102
  
moel@136
   103
            type = raw[offset];
moel@136
   104
            int length = raw[offset + 1];
moel@136
   105
            ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
moel@136
   106
  
moel@136
   107
            if (offset + length > raw.Length)
moel@136
   108
              break;
moel@136
   109
            byte[] data = new byte[length];
moel@136
   110
            Array.Copy(raw, offset, data, 0, length);
moel@136
   111
            offset += length;
moel@136
   112
  
moel@136
   113
            List<string> stringsList = new List<string>();
moel@136
   114
            if (offset < raw.Length && raw[offset] == 0)
moel@136
   115
              offset++;
moel@136
   116
  
moel@89
   117
            while (offset < raw.Length && raw[offset] != 0) {
moel@136
   118
              StringBuilder sb = new StringBuilder();
moel@136
   119
              while (offset < raw.Length && raw[offset] != 0) {
moel@136
   120
                sb.Append((char)raw[offset]); offset++;
moel@136
   121
              }
moel@136
   122
              offset++;
moel@136
   123
              stringsList.Add(sb.ToString());
moel@64
   124
            }
moel@64
   125
            offset++;
moel@136
   126
            switch (type) {
moel@136
   127
              case 0x00:
moel@136
   128
                this.biosInformation = new BIOSInformation(
moel@136
   129
                  type, handle, data, stringsList.ToArray());
moel@136
   130
                structureList.Add(this.biosInformation); break;
moel@136
   131
              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@136
   132
                  type, handle, data, stringsList.ToArray());
moel@136
   133
                structureList.Add(this.baseBoardInformation); break;
moel@136
   134
              default: structureList.Add(new Structure(
moel@136
   135
                type, handle, data, stringsList.ToArray())); break;
moel@136
   136
            }
moel@64
   137
          }
moel@64
   138
        }
moel@136
   139
              
moel@136
   140
        table = structureList.ToArray();
moel@89
   141
      }
moel@64
   142
    }
moel@64
   143
moel@64
   144
    public string GetReport() {
moel@136
   145
      StringBuilder r = new StringBuilder();
moel@64
   146
moel@64
   147
      if (biosInformation != null) {
moel@64
   148
        r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
moel@64
   149
        r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
moel@64
   150
        r.AppendLine();
moel@64
   151
      }
moel@64
   152
moel@64
   153
      if (baseBoardInformation != null) {
moel@136
   154
        r.Append("Mainboard Manufacturer: ");
moel@64
   155
        r.AppendLine(baseBoardInformation.ManufacturerName);
moel@136
   156
        r.Append("Mainboard Name: ");
moel@64
   157
        r.AppendLine(baseBoardInformation.ProductName);
moel@64
   158
        r.AppendLine();
moel@64
   159
      }
moel@136
   160
moel@136
   161
      if (raw != null) {
moel@136
   162
        string base64 = Convert.ToBase64String(raw);
moel@136
   163
        r.AppendLine("SMBIOS Table");
moel@136
   164
        r.AppendLine();
moel@136
   165
moel@136
   166
        for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
moel@136
   167
          r.Append(" ");
moel@136
   168
          for (int j = 0; j < 0x40; j++) {
moel@136
   169
            int index = (i << 6) | j;
moel@136
   170
            if (index < base64.Length) {              
moel@136
   171
              r.Append(base64[index]);
moel@136
   172
            }
moel@136
   173
          }
moel@136
   174
          r.AppendLine();
moel@136
   175
        }
moel@136
   176
        r.AppendLine();
moel@136
   177
      }
moel@136
   178
moel@64
   179
      return r.ToString();
moel@64
   180
    }
moel@64
   181
moel@64
   182
    public BIOSInformation BIOS {
moel@64
   183
      get { return biosInformation; }
moel@64
   184
    }
moel@64
   185
moel@64
   186
    public BaseBoardInformation Board {
moel@64
   187
      get { return baseBoardInformation; }
moel@64
   188
    }
moel@64
   189
moel@64
   190
    public class Structure {
moel@64
   191
      private byte type;
moel@64
   192
      private ushort handle;
moel@64
   193
moel@64
   194
      private byte[] data;
moel@64
   195
      private string[] strings;
moel@64
   196
moel@64
   197
      protected string GetString(int offset) {
moel@64
   198
        if (offset < data.Length && data[offset] > 0 &&
moel@64
   199
         data[offset] <= strings.Length)
moel@64
   200
          return strings[data[offset] - 1];
moel@64
   201
        else
moel@64
   202
          return "";
moel@64
   203
      }
moel@64
   204
moel@64
   205
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@64
   206
      {
moel@64
   207
        this.type = type;
moel@64
   208
        this.handle = handle;
moel@64
   209
        this.data = data;
moel@64
   210
        this.strings = strings;
moel@64
   211
      }
moel@64
   212
moel@64
   213
      public byte Type { get { return type; } }
moel@64
   214
moel@64
   215
      public ushort Handle { get { return handle; } }
moel@64
   216
    }
moel@136
   217
      
moel@64
   218
    public class BIOSInformation : Structure {
moel@64
   219
moel@64
   220
      private string vendor;
moel@64
   221
      private string version;
moel@136
   222
      
moel@136
   223
      public BIOSInformation(string vendor, string version) 
moel@136
   224
        : base (0x00, 0, null, null) 
moel@136
   225
      {
moel@136
   226
        this.vendor = vendor;
moel@136
   227
        this.version = version;
moel@136
   228
      }
moel@136
   229
      
moel@64
   230
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64
   231
        string[] strings)
moel@136
   232
        : base(type, handle, data, strings) 
moel@136
   233
      {
moel@64
   234
        this.vendor = GetString(0x04);
moel@64
   235
        this.version = GetString(0x05);
moel@64
   236
      }
moel@64
   237
moel@64
   238
      public string Vendor { get { return vendor; } }
moel@64
   239
moel@64
   240
      public string Version { get { return version; } }
moel@64
   241
    }
moel@64
   242
moel@64
   243
    public class BaseBoardInformation : Structure {
moel@64
   244
moel@64
   245
      private string manufacturerName;
moel@64
   246
      private string productName;
moel@89
   247
      private string version;
moel@89
   248
      private string serialNumber;
moel@64
   249
      private Manufacturer manufacturer;
moel@126
   250
      private Model model;
moel@64
   251
moel@136
   252
      private void SetManufacturerName(string manufacturerName) {
moel@136
   253
        this.manufacturerName = manufacturerName;
moel@136
   254
        
moel@64
   255
        switch (manufacturerName) {
moel@64
   256
          case "ASUSTeK Computer INC.":
moel@64
   257
            manufacturer = Manufacturer.ASUS; break;
moel@64
   258
          case "DFI":
moel@72
   259
          case "DFI Inc.":            
moel@64
   260
            manufacturer = Manufacturer.DFI; break;
moel@64
   261
          case "EPoX COMPUTER CO., LTD":
moel@64
   262
            manufacturer = Manufacturer.EPoX; break;
moel@132
   263
          case "EVGA":
moel@132
   264
            manufacturer = Manufacturer.EVGA; break;
moel@64
   265
          case "Gigabyte Technology Co., Ltd.":
moel@64
   266
            manufacturer = Manufacturer.Gigabyte; break;
moel@72
   267
          case "IBM":
moel@72
   268
            manufacturer = Manufacturer.IBM; break;
moel@64
   269
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   270
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64
   271
            manufacturer = Manufacturer.MSI; break;
moel@64
   272
          default:
moel@126
   273
            manufacturer = Manufacturer.Unknown; break;
moel@126
   274
        }
moel@136
   275
      }
moel@136
   276
      
moel@136
   277
      private void SetProductName(string productName) {
moel@136
   278
        this.productName = productName;
moel@136
   279
        
moel@126
   280
        switch (productName) {
moel@133
   281
          case "Crosshair III Formula":
moel@133
   282
            model = Model.Crosshair_III_Formula; break;
moel@133
   283
          case "M2N-SLI DELUXE":
moel@133
   284
            model = Model.M2N_SLI_DELUXE; break;
moel@130
   285
          case "P5W DH Deluxe":
moel@130
   286
            model = Model.P5W_DH_Deluxe; break;
moel@126
   287
          case "LP BI P45-T2RS Elite":
moel@126
   288
            model = Model.LP_BI_P45_T2RS_Elite; break;
moel@126
   289
          case "LP DK P55-T3eH9":
moel@126
   290
            model = Model.LP_DK_P55_T3eH9; break;
moel@132
   291
          case "X58 SLI Classified":
moel@132
   292
            model = Model.X58_SLI_Classified; break;
moel@130
   293
          case "965P-S3":
moel@130
   294
            model = Model._965P_S3; break;
moel@126
   295
          case "EP45-DS3R":
moel@126
   296
            model = Model.EP45_DS3R; break;
moel@130
   297
          case "EP45-UD3R":
moel@130
   298
            model = Model.EP45_UD3R; break;
moel@133
   299
          case "EX58-EXTREME":
moel@133
   300
            model = Model.EX58_EXTREME; break;
moel@126
   301
          case "GA-MA785GMT-UD2H":
moel@126
   302
            model = Model.GA_MA785GMT_UD2H; break;
moel@126
   303
          case "P35-DS3":
moel@126
   304
            model = Model.P35_DS3; break;
moel@133
   305
          case "P35-DS3L":
moel@133
   306
            model = Model.P35_DS3L; break;
moel@130
   307
          case "X38-DS5":
moel@130
   308
            model = Model.X38_DS5; break;
moel@138
   309
          case "X58A-UD3R":
moel@138
   310
            model = Model.X58A_UD3R; break;
moel@126
   311
          default:
moel@126
   312
            model = Model.Unknown; break;
moel@64
   313
        }
moel@64
   314
      }
moel@136
   315
      
moel@136
   316
      public BaseBoardInformation(string manufacturerName, string productName, 
moel@136
   317
        string version, string serialNumber) 
moel@136
   318
        : base(0x02, 0, null, null) 
moel@136
   319
      {        
moel@136
   320
        SetManufacturerName(manufacturerName);
moel@136
   321
        SetProductName(productName);
moel@136
   322
        this.version = version;
moel@136
   323
        this.serialNumber = serialNumber;
moel@136
   324
      }
moel@136
   325
      
moel@136
   326
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@136
   327
        string[] strings)
moel@136
   328
        : base(type, handle, data, strings) {
moel@64
   329
moel@136
   330
        SetManufacturerName(GetString(0x04).Trim());
moel@136
   331
        SetProductName(GetString(0x05).Trim());
moel@136
   332
        this.version = GetString(0x06).Trim();
moel@136
   333
        this.serialNumber = GetString(0x07).Trim();               
moel@136
   334
      }
moel@136
   335
      
moel@64
   336
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   337
moel@64
   338
      public string ProductName { get { return productName; } }
moel@64
   339
moel@89
   340
      public string Version { get { return version; } }
moel@89
   341
moel@89
   342
      public string SerialNumber { get { return serialNumber; } }
moel@89
   343
moel@64
   344
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   345
moel@126
   346
      public Model Model { get { return model; } }
moel@126
   347
moel@64
   348
    }
moel@64
   349
  }
moel@64
   350
}