Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sun, 18 Jul 2010 12:38:01 +0000
changeset 156 3e2ab626531c
parent 153 62162db94359
child 165 813d8bc3192f
permissions -rw-r--r--
Replaced all menus with the native implementations MainMenu and ContextMenu. Fixed a bug with the sensor renaming context menu.
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@153
   256
          case "ASRock":
moel@153
   257
            manufacturer = Manufacturer.ASRock; break;
moel@64
   258
          case "ASUSTeK Computer INC.":
moel@64
   259
            manufacturer = Manufacturer.ASUS; break;
moel@152
   260
          case "Dell Inc.":
moel@152
   261
            manufacturer = Manufacturer.Dell; break;
moel@64
   262
          case "DFI":
moel@72
   263
          case "DFI Inc.":            
moel@64
   264
            manufacturer = Manufacturer.DFI; break;
moel@64
   265
          case "EPoX COMPUTER CO., LTD":
moel@64
   266
            manufacturer = Manufacturer.EPoX; break;
moel@132
   267
          case "EVGA":
moel@132
   268
            manufacturer = Manufacturer.EVGA; break;
moel@152
   269
          case "First International Computer, Inc.":
moel@152
   270
            manufacturer = Manufacturer.FIC; break;
moel@64
   271
          case "Gigabyte Technology Co., Ltd.":
moel@64
   272
            manufacturer = Manufacturer.Gigabyte; break;
moel@152
   273
          case "Hewlett-Packard":
moel@152
   274
            manufacturer = Manufacturer.HP; break;
moel@72
   275
          case "IBM":
moel@72
   276
            manufacturer = Manufacturer.IBM; break;
moel@64
   277
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   278
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64
   279
            manufacturer = Manufacturer.MSI; break;
moel@152
   280
          case "XFX":
moel@152
   281
            manufacturer = Manufacturer.XFX; break;
moel@152
   282
          case "To be filled by O.E.M.":
moel@152
   283
            manufacturer = Manufacturer.Unknown; break;
moel@64
   284
          default:
moel@126
   285
            manufacturer = Manufacturer.Unknown; break;
moel@126
   286
        }
moel@136
   287
      }
moel@136
   288
      
moel@136
   289
      private void SetProductName(string productName) {
moel@136
   290
        this.productName = productName;
moel@136
   291
        
moel@126
   292
        switch (productName) {
moel@153
   293
          case "880GMH/USB3":
moel@153
   294
            model = Model._880GMH_USB3; break;
moel@133
   295
          case "Crosshair III Formula":
moel@133
   296
            model = Model.Crosshair_III_Formula; break;
moel@133
   297
          case "M2N-SLI DELUXE":
moel@133
   298
            model = Model.M2N_SLI_DELUXE; break;
moel@144
   299
          case "M4A79XTD EVO":
moel@144
   300
            model = Model.M4A79XTD_EVO; break;
moel@130
   301
          case "P5W DH Deluxe":
moel@130
   302
            model = Model.P5W_DH_Deluxe; break;
moel@152
   303
          case "P6X58D-E":
moel@152
   304
            model = Model.P6X58D_E; break;
moel@126
   305
          case "LP BI P45-T2RS Elite":
moel@126
   306
            model = Model.LP_BI_P45_T2RS_Elite; break;
moel@126
   307
          case "LP DK P55-T3eH9":
moel@126
   308
            model = Model.LP_DK_P55_T3eH9; break;
moel@132
   309
          case "X58 SLI Classified":
moel@132
   310
            model = Model.X58_SLI_Classified; break;
moel@130
   311
          case "965P-S3":
moel@130
   312
            model = Model._965P_S3; break;
moel@126
   313
          case "EP45-DS3R":
moel@126
   314
            model = Model.EP45_DS3R; break;
moel@130
   315
          case "EP45-UD3R":
moel@130
   316
            model = Model.EP45_UD3R; break;
moel@133
   317
          case "EX58-EXTREME":
moel@133
   318
            model = Model.EX58_EXTREME; break;
moel@154
   319
          case "GA-MA770T-UD3":
moel@154
   320
            model = Model.GA_MA770T_UD3; break;
moel@126
   321
          case "GA-MA785GMT-UD2H":
moel@126
   322
            model = Model.GA_MA785GMT_UD2H; break;
moel@126
   323
          case "P35-DS3":
moel@126
   324
            model = Model.P35_DS3; break;
moel@133
   325
          case "P35-DS3L":
moel@133
   326
            model = Model.P35_DS3L; break;
moel@148
   327
          case "P55-UD4":
moel@148
   328
            model = Model.P55_UD4; break;
moel@130
   329
          case "X38-DS5":
moel@130
   330
            model = Model.X38_DS5; break;
moel@138
   331
          case "X58A-UD3R":
moel@138
   332
            model = Model.X58A_UD3R; break;
moel@152
   333
          case "To be filled by O.E.M.":
moel@152
   334
            model = Model.Unknown; break;
moel@126
   335
          default:
moel@126
   336
            model = Model.Unknown; break;
moel@64
   337
        }
moel@64
   338
      }
moel@136
   339
      
moel@136
   340
      public BaseBoardInformation(string manufacturerName, string productName, 
moel@136
   341
        string version, string serialNumber) 
moel@136
   342
        : base(0x02, 0, null, null) 
moel@136
   343
      {        
moel@136
   344
        SetManufacturerName(manufacturerName);
moel@136
   345
        SetProductName(productName);
moel@136
   346
        this.version = version;
moel@136
   347
        this.serialNumber = serialNumber;
moel@136
   348
      }
moel@136
   349
      
moel@136
   350
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@136
   351
        string[] strings)
moel@136
   352
        : base(type, handle, data, strings) {
moel@64
   353
moel@136
   354
        SetManufacturerName(GetString(0x04).Trim());
moel@136
   355
        SetProductName(GetString(0x05).Trim());
moel@136
   356
        this.version = GetString(0x06).Trim();
moel@136
   357
        this.serialNumber = GetString(0x07).Trim();               
moel@136
   358
      }
moel@136
   359
      
moel@64
   360
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   361
moel@64
   362
      public string ProductName { get { return productName; } }
moel@64
   363
moel@89
   364
      public string Version { get { return version; } }
moel@89
   365
moel@89
   366
      public string SerialNumber { get { return serialNumber; } }
moel@89
   367
moel@64
   368
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   369
moel@126
   370
      public Model Model { get { return model; } }
moel@126
   371
moel@64
   372
    }
moel@64
   373
  }
moel@64
   374
}