Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sun, 01 Jul 2012 21:44:07 +0000
changeset 355 17dbf781401e
parent 337 10283dd68bbb
child 357 fb8dc26f65a4
permissions -rw-r--r--
Added experimental support for Nuvoton NCT6779D super I/O chips.
moel@64
     1
/*
moel@64
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@64
     6
 
moel@344
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@64
     9
*/
moel@64
    10
moel@64
    11
using System;
moel@64
    12
using System.Collections.Generic;
moel@136
    13
using System.IO;
moel@64
    14
using System.Management;
moel@64
    15
using System.Text;
moel@64
    16
moel@64
    17
namespace OpenHardwareMonitor.Hardware.Mainboard {
moel@64
    18
moel@165
    19
  internal class SMBIOS {
moel@64
    20
moel@195
    21
    private readonly byte[] raw;
moel@195
    22
    private readonly Structure[] table;
moel@64
    23
moel@242
    24
    private readonly Version version;
moel@195
    25
    private readonly BIOSInformation biosInformation;
moel@320
    26
    private readonly SystemInformation systemInformation;
moel@195
    27
    private readonly BaseBoardInformation baseBoardInformation;
moel@64
    28
moel@167
    29
    private static string ReadSysFS(string path) {
moel@136
    30
      try {
moel@136
    31
        if (File.Exists(path)) {
moel@136
    32
          using (StreamReader reader = new StreamReader(path)) 
moel@136
    33
            return reader.ReadLine();
moel@136
    34
        } else {
moel@136
    35
          return null;
moel@136
    36
        }
moel@136
    37
      } catch {
moel@136
    38
        return null;
moel@136
    39
      }
moel@136
    40
    }
moel@136
    41
    
moel@64
    42
    public SMBIOS() {
moel@195
    43
      int p = (int)Environment.OSVersion.Platform;
moel@136
    44
      if ((p == 4) || (p == 128)) {
moel@136
    45
        this.raw = null;
moel@136
    46
        this.table = null;
moel@136
    47
        
moel@136
    48
        string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
moel@136
    49
        string boardName = ReadSysFS("/sys/class/dmi/id/board_name");        
moel@136
    50
        string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");        
moel@136
    51
        this.baseBoardInformation = new BaseBoardInformation(
moel@136
    52
          boardVendor, boardName, boardVersion, null);
moel@320
    53
moel@320
    54
        string systemVendor = ReadSysFS("/sys/class/dmi/id/sys_vendor");
moel@320
    55
        string productName = ReadSysFS("/sys/class/dmi/id/product_name");
moel@320
    56
        string productVersion = ReadSysFS("/sys/class/dmi/id/product_version");    
moel@320
    57
        this.systemInformation = new SystemInformation(systemVendor, 
moel@320
    58
          productName, productVersion, null, null);
moel@320
    59
moel@136
    60
        string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
moel@136
    61
        string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
moel@136
    62
        this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
moel@136
    63
        
moel@136
    64
      } else {              
moel@136
    65
        List<Structure> structureList = new List<Structure>();
moel@64
    66
moel@136
    67
        raw = null;
moel@242
    68
        byte majorVersion = 0;
moel@242
    69
        byte minorVersion = 0;
moel@136
    70
        try {
moel@167
    71
          ManagementObjectCollection collection;
moel@167
    72
          using (ManagementObjectSearcher searcher = 
moel@136
    73
            new ManagementObjectSearcher("root\\WMI", 
moel@242
    74
              "SELECT * FROM MSSMBios_RawSMBiosTables")) {
moel@167
    75
            collection = searcher.Get();
moel@167
    76
          }
moel@136
    77
         
moel@136
    78
          foreach (ManagementObject mo in collection) {
moel@136
    79
            raw = (byte[])mo["SMBiosData"];
moel@242
    80
            majorVersion = (byte)mo["SmbiosMajorVersion"];
moel@242
    81
            minorVersion = (byte)mo["SmbiosMinorVersion"];            
moel@89
    82
            break;
moel@136
    83
          }
moel@136
    84
        } catch { }
moel@242
    85
moel@242
    86
        if (majorVersion > 0 || minorVersion > 0)
moel@242
    87
          version = new Version(majorVersion, minorVersion);
moel@136
    88
  
moel@136
    89
        if (raw != null && raw.Length > 0) {
moel@136
    90
          int offset = 0;
moel@136
    91
          byte type = raw[offset];
moel@136
    92
          while (offset + 4 < raw.Length && type != 127) {
moel@136
    93
  
moel@136
    94
            type = raw[offset];
moel@136
    95
            int length = raw[offset + 1];
moel@136
    96
            ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
moel@136
    97
  
moel@136
    98
            if (offset + length > raw.Length)
moel@136
    99
              break;
moel@136
   100
            byte[] data = new byte[length];
moel@136
   101
            Array.Copy(raw, offset, data, 0, length);
moel@136
   102
            offset += length;
moel@136
   103
  
moel@136
   104
            List<string> stringsList = new List<string>();
moel@136
   105
            if (offset < raw.Length && raw[offset] == 0)
moel@136
   106
              offset++;
moel@136
   107
  
moel@89
   108
            while (offset < raw.Length && raw[offset] != 0) {
moel@136
   109
              StringBuilder sb = new StringBuilder();
moel@136
   110
              while (offset < raw.Length && raw[offset] != 0) {
moel@136
   111
                sb.Append((char)raw[offset]); offset++;
moel@136
   112
              }
moel@136
   113
              offset++;
moel@136
   114
              stringsList.Add(sb.ToString());
moel@64
   115
            }
moel@64
   116
            offset++;
moel@136
   117
            switch (type) {
moel@136
   118
              case 0x00:
moel@136
   119
                this.biosInformation = new BIOSInformation(
moel@136
   120
                  type, handle, data, stringsList.ToArray());
moel@136
   121
                structureList.Add(this.biosInformation); break;
moel@320
   122
              case 0x01:
moel@320
   123
                this.systemInformation = new SystemInformation(
moel@320
   124
                  type, handle, data, stringsList.ToArray());
moel@320
   125
                structureList.Add(this.systemInformation); break;
moel@136
   126
              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@136
   127
                  type, handle, data, stringsList.ToArray());
moel@136
   128
                structureList.Add(this.baseBoardInformation); break;
moel@136
   129
              default: structureList.Add(new Structure(
moel@136
   130
                type, handle, data, stringsList.ToArray())); break;
moel@136
   131
            }
moel@64
   132
          }
moel@64
   133
        }
moel@136
   134
              
moel@136
   135
        table = structureList.ToArray();
moel@89
   136
      }
moel@64
   137
    }
moel@64
   138
moel@64
   139
    public string GetReport() {
moel@136
   140
      StringBuilder r = new StringBuilder();
moel@64
   141
moel@242
   142
      if (version != null) {
moel@242
   143
        r.Append("SMBIOS Version: "); r.AppendLine(version.ToString(2));
moel@242
   144
        r.AppendLine();
moel@242
   145
      }
moel@242
   146
moel@167
   147
      if (BIOS != null) {
moel@167
   148
        r.Append("BIOS Vendor: "); r.AppendLine(BIOS.Vendor);
moel@167
   149
        r.Append("BIOS Version: "); r.AppendLine(BIOS.Version);
moel@64
   150
        r.AppendLine();
moel@64
   151
      }
moel@64
   152
moel@320
   153
      if (System != null) {
moel@320
   154
        r.Append("System Manufacturer: ");
moel@320
   155
        r.AppendLine(System.ManufacturerName);
moel@320
   156
        r.Append("System Name: ");
moel@320
   157
        r.AppendLine(System.ProductName);
moel@320
   158
        r.Append("System Version: ");
moel@320
   159
        r.AppendLine(System.Version);
moel@320
   160
        r.AppendLine();
moel@320
   161
      }
moel@320
   162
moel@167
   163
      if (Board != null) {
moel@136
   164
        r.Append("Mainboard Manufacturer: ");
moel@167
   165
        r.AppendLine(Board.ManufacturerName);
moel@136
   166
        r.Append("Mainboard Name: ");
moel@167
   167
        r.AppendLine(Board.ProductName);
moel@167
   168
        r.Append("Mainboard Version: ");
moel@167
   169
        r.AppendLine(Board.Version);
moel@64
   170
        r.AppendLine();
moel@64
   171
      }
moel@136
   172
moel@136
   173
      if (raw != null) {
moel@136
   174
        string base64 = Convert.ToBase64String(raw);
moel@136
   175
        r.AppendLine("SMBIOS Table");
moel@136
   176
        r.AppendLine();
moel@136
   177
moel@136
   178
        for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
moel@136
   179
          r.Append(" ");
moel@136
   180
          for (int j = 0; j < 0x40; j++) {
moel@136
   181
            int index = (i << 6) | j;
moel@136
   182
            if (index < base64.Length) {              
moel@136
   183
              r.Append(base64[index]);
moel@136
   184
            }
moel@136
   185
          }
moel@136
   186
          r.AppendLine();
moel@136
   187
        }
moel@136
   188
        r.AppendLine();
moel@136
   189
      }
moel@136
   190
moel@64
   191
      return r.ToString();
moel@64
   192
    }
moel@64
   193
moel@64
   194
    public BIOSInformation BIOS {
moel@64
   195
      get { return biosInformation; }
moel@64
   196
    }
moel@64
   197
moel@320
   198
    public SystemInformation System {
moel@320
   199
      get { return systemInformation; }
moel@320
   200
    }
moel@320
   201
moel@64
   202
    public BaseBoardInformation Board {
moel@64
   203
      get { return baseBoardInformation; }
moel@64
   204
    }
moel@64
   205
moel@64
   206
    public class Structure {
moel@195
   207
      private readonly byte type;
moel@195
   208
      private readonly ushort handle;
moel@64
   209
moel@195
   210
      private readonly byte[] data;
moel@195
   211
      private readonly string[] strings;
moel@64
   212
moel@64
   213
      protected string GetString(int offset) {
moel@64
   214
        if (offset < data.Length && data[offset] > 0 &&
moel@64
   215
         data[offset] <= strings.Length)
moel@64
   216
          return strings[data[offset] - 1];
moel@64
   217
        else
moel@64
   218
          return "";
moel@64
   219
      }
moel@64
   220
moel@64
   221
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@64
   222
      {
moel@64
   223
        this.type = type;
moel@64
   224
        this.handle = handle;
moel@64
   225
        this.data = data;
moel@64
   226
        this.strings = strings;
moel@64
   227
      }
moel@64
   228
moel@64
   229
      public byte Type { get { return type; } }
moel@64
   230
moel@64
   231
      public ushort Handle { get { return handle; } }
moel@64
   232
    }
moel@136
   233
      
moel@64
   234
    public class BIOSInformation : Structure {
moel@64
   235
moel@195
   236
      private readonly string vendor;
moel@195
   237
      private readonly string version;
moel@136
   238
      
moel@136
   239
      public BIOSInformation(string vendor, string version) 
moel@136
   240
        : base (0x00, 0, null, null) 
moel@136
   241
      {
moel@136
   242
        this.vendor = vendor;
moel@136
   243
        this.version = version;
moel@136
   244
      }
moel@136
   245
      
moel@64
   246
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64
   247
        string[] strings)
moel@136
   248
        : base(type, handle, data, strings) 
moel@136
   249
      {
moel@64
   250
        this.vendor = GetString(0x04);
moel@64
   251
        this.version = GetString(0x05);
moel@64
   252
      }
moel@64
   253
moel@64
   254
      public string Vendor { get { return vendor; } }
moel@64
   255
moel@64
   256
      public string Version { get { return version; } }
moel@64
   257
    }
moel@64
   258
moel@320
   259
    public class SystemInformation : Structure {
moel@320
   260
moel@320
   261
      private readonly string manufacturerName;
moel@320
   262
      private readonly string productName;
moel@320
   263
      private readonly string version;
moel@320
   264
      private readonly string serialNumber;
moel@320
   265
      private readonly string family;
moel@320
   266
moel@320
   267
      public SystemInformation(string manufacturerName, string productName, 
moel@320
   268
        string version, string serialNumber, string family) 
moel@320
   269
        : base (0x01, 0, null, null) 
moel@320
   270
      {
moel@320
   271
        this.manufacturerName = manufacturerName;
moel@320
   272
        this.productName = productName;
moel@320
   273
        this.version = version;
moel@320
   274
        this.serialNumber = serialNumber;
moel@320
   275
        this.family = family;
moel@320
   276
      }
moel@320
   277
moel@320
   278
      public SystemInformation(byte type, ushort handle, byte[] data,
moel@320
   279
        string[] strings)
moel@320
   280
        : base(type, handle, data, strings) 
moel@320
   281
      {
moel@320
   282
        this.manufacturerName = GetString(0x04);
moel@320
   283
        this.productName = GetString(0x05);
moel@320
   284
        this.version = GetString(0x06);
moel@320
   285
        this.serialNumber = GetString(0x07);
moel@320
   286
        this.family = GetString(0x1A);
moel@320
   287
      }
moel@320
   288
moel@320
   289
      public string ManufacturerName { get { return manufacturerName; } }
moel@320
   290
moel@320
   291
      public string ProductName { get { return productName; } }
moel@320
   292
moel@320
   293
      public string Version { get { return version; } }
moel@320
   294
moel@320
   295
      public string SerialNumber { get { return serialNumber; } }
moel@320
   296
moel@320
   297
      public string Family { get { return family; } }
moel@320
   298
moel@320
   299
    }
moel@320
   300
moel@64
   301
    public class BaseBoardInformation : Structure {
moel@64
   302
moel@195
   303
      private readonly string manufacturerName;
moel@195
   304
      private readonly string productName;
moel@195
   305
      private readonly string version;
moel@195
   306
      private readonly string serialNumber;
moel@195
   307
      private readonly Manufacturer manufacturer;
moel@195
   308
      private readonly Model model;
moel@64
   309
moel@195
   310
      private static Manufacturer GetManufacturer(string name) {               
moel@167
   311
        switch (name) {
moel@296
   312
          case "Alienware":
moel@296
   313
            return Manufacturer.Alienware;
moel@296
   314
          case "Apple Inc.":
moel@296
   315
            return Manufacturer.Apple;
moel@153
   316
          case "ASRock":
moel@195
   317
            return Manufacturer.ASRock;
moel@64
   318
          case "ASUSTeK Computer INC.":
moel@332
   319
          case "ASUSTeK COMPUTER INC.":
moel@195
   320
            return Manufacturer.ASUS;
moel@152
   321
          case "Dell Inc.":
moel@195
   322
            return Manufacturer.Dell;
moel@64
   323
          case "DFI":
moel@72
   324
          case "DFI Inc.":            
moel@195
   325
            return Manufacturer.DFI;
moel@177
   326
          case "ECS":
moel@195
   327
            return Manufacturer.ECS;
moel@64
   328
          case "EPoX COMPUTER CO., LTD":
moel@195
   329
            return Manufacturer.EPoX;
moel@132
   330
          case "EVGA":
moel@195
   331
            return  Manufacturer.EVGA;
moel@152
   332
          case "First International Computer, Inc.":
moel@195
   333
            return Manufacturer.FIC;
moel@296
   334
          case "FUJITSU":
moel@296
   335
          case "FUJITSU SIEMENS":
moel@296
   336
            return Manufacturer.Fujitsu;
moel@64
   337
          case "Gigabyte Technology Co., Ltd.":
moel@195
   338
            return  Manufacturer.Gigabyte;
moel@152
   339
          case "Hewlett-Packard":
moel@195
   340
            return  Manufacturer.HP;
moel@72
   341
          case "IBM":
moel@195
   342
            return  Manufacturer.IBM;
moel@296
   343
          case "Intel":
moel@296
   344
          case "Intel Corp.":
moel@296
   345
          case "Intel Corporation":
moel@296
   346
          case "INTEL Corporation":
moel@296
   347
            return Manufacturer.Intel;   
moel@296
   348
          case "Lenovo":
moel@296
   349
          case "LENOVO":
moel@296
   350
            return Manufacturer.Lenovo;
moel@296
   351
          case "Micro-Star International":
moel@64
   352
          case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72
   353
          case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@296
   354
          case "MSI":
moel@319
   355
            return Manufacturer.MSI;
moel@319
   356
          case "Shuttle":
moel@319
   357
            return Manufacturer.Shuttle;
moel@296
   358
          case "Supermicro":
moel@296
   359
            return Manufacturer.Supermicro;
moel@296
   360
          case "TOSHIBA":
moel@296
   361
            return Manufacturer.Toshiba;
moel@152
   362
          case "XFX":
moel@195
   363
            return  Manufacturer.XFX;
moel@152
   364
          case "To be filled by O.E.M.":
moel@195
   365
            return  Manufacturer.Unknown;
moel@64
   366
          default:
moel@195
   367
            return  Manufacturer.Unknown;
moel@126
   368
        }
moel@136
   369
      }
moel@195
   370
moel@195
   371
      private static Model GetModel(string name) {
moel@167
   372
        switch (name) {
moel@153
   373
          case "880GMH/USB3":
moel@195
   374
            return Model._880GMH_USB3;
moel@220
   375
          case "ASRock AOD790GX/128M":
moel@220
   376
            return Model.AOD790GX_128M;
moel@221
   377
          case "P55 Deluxe":
moel@221
   378
            return Model.P55_Deluxe;
moel@133
   379
          case "Crosshair III Formula":
moel@195
   380
            return Model.Crosshair_III_Formula;
moel@133
   381
          case "M2N-SLI DELUXE":
moel@195
   382
            return Model.M2N_SLI_DELUXE;
moel@144
   383
          case "M4A79XTD EVO":
moel@195
   384
            return Model.M4A79XTD_EVO;
moel@130
   385
          case "P5W DH Deluxe":
moel@195
   386
            return Model.P5W_DH_Deluxe;
moel@336
   387
          case "P6T":
moel@336
   388
            return Model.P6T;
moel@152
   389
          case "P6X58D-E":
moel@195
   390
            return Model.P6X58D_E;
moel@312
   391
          case "P8P67":
moel@312
   392
            return Model.P8P67;
moel@311
   393
          case "P8P67 EVO":
moel@311
   394
            return Model.P8P67_EVO;
moel@265
   395
          case "P8P67 PRO":
moel@265
   396
            return Model.P8P67_PRO;
moel@276
   397
          case "P8P67-M PRO":
moel@276
   398
            return Model.P8P67_M_PRO;
moel@332
   399
          case "P9X79":
moel@332
   400
            return Model.P9X79;
moel@174
   401
          case "Rampage Extreme":
moel@195
   402
            return Model.Rampage_Extreme;
moel@174
   403
          case "Rampage II GENE":
moel@195
   404
            return Model.Rampage_II_GENE;
moel@126
   405
          case "LP BI P45-T2RS Elite":
moel@195
   406
            return Model.LP_BI_P45_T2RS_Elite;
moel@126
   407
          case "LP DK P55-T3eH9":
moel@195
   408
            return Model.LP_DK_P55_T3eH9;
moel@177
   409
          case "A890GXM-A":
moel@195
   410
            return Model.A890GXM_A;
moel@132
   411
          case "X58 SLI Classified":
moel@195
   412
            return Model.X58_SLI_Classified;
moel@130
   413
          case "965P-S3":
moel@195
   414
            return Model._965P_S3;
moel@126
   415
          case "EP45-DS3R":
moel@195
   416
            return Model.EP45_DS3R;
moel@130
   417
          case "EP45-UD3R":
moel@195
   418
            return Model.EP45_UD3R;
moel@133
   419
          case "EX58-EXTREME":
moel@195
   420
            return Model.EX58_EXTREME;
moel@154
   421
          case "GA-MA770T-UD3":
moel@195
   422
            return Model.GA_MA770T_UD3;
moel@126
   423
          case "GA-MA785GMT-UD2H":
moel@195
   424
            return Model.GA_MA785GMT_UD2H;
moel@290
   425
          case "H67A-UD3H-B3":
moel@290
   426
            return Model.H67A_UD3H_B3;
moel@126
   427
          case "P35-DS3":
moel@195
   428
            return Model.P35_DS3;
moel@133
   429
          case "P35-DS3L":
moel@195
   430
            return Model.P35_DS3L;
moel@148
   431
          case "P55-UD4":
moel@195
   432
            return Model.P55_UD4;
moel@168
   433
          case "P55M-UD4":
moel@195
   434
            return Model.P55M_UD4;
moel@278
   435
          case "P67A-UD4-B3":
moel@278
   436
            return Model.P67A_UD4_B3;
moel@337
   437
          case "P8Z68-V PRO":
moel@337
   438
            return Model.P8Z68_V_PRO;
moel@130
   439
          case "X38-DS5":
moel@195
   440
            return Model.X38_DS5;
moel@138
   441
          case "X58A-UD3R":
moel@195
   442
            return Model.X58A_UD3R;
moel@305
   443
          case "Z68X-UD7-B3":
moel@305
   444
            return Model.Z68X_UD7_B3;
moel@320
   445
          case "FH67":
moel@320
   446
            return Model.FH67;
moel@296
   447
          case "Base Board Product Name":
moel@152
   448
          case "To be filled by O.E.M.":
moel@195
   449
            return Model.Unknown;
moel@126
   450
          default:
moel@195
   451
            return Model.Unknown;
moel@64
   452
        }
moel@64
   453
      }
moel@136
   454
      
moel@136
   455
      public BaseBoardInformation(string manufacturerName, string productName, 
moel@136
   456
        string version, string serialNumber) 
moel@136
   457
        : base(0x02, 0, null, null) 
moel@195
   458
      {
moel@195
   459
        this.manufacturerName = manufacturerName;
moel@195
   460
        this.manufacturer = GetManufacturer(manufacturerName);
moel@195
   461
        this.productName = productName;
moel@195
   462
        this.model = GetModel(productName);
moel@136
   463
        this.version = version;
moel@136
   464
        this.serialNumber = serialNumber;
moel@136
   465
      }
moel@136
   466
      
moel@136
   467
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@136
   468
        string[] strings)
moel@136
   469
        : base(type, handle, data, strings) {
moel@64
   470
moel@195
   471
        this.manufacturerName = GetString(0x04).Trim();
moel@195
   472
        this.manufacturer = GetManufacturer(this.manufacturerName);
moel@195
   473
        this.productName = GetString(0x05).Trim();
moel@195
   474
        this.model = GetModel(this.productName);
moel@136
   475
        this.version = GetString(0x06).Trim();
moel@136
   476
        this.serialNumber = GetString(0x07).Trim();               
moel@136
   477
      }
moel@136
   478
      
moel@64
   479
      public string ManufacturerName { get { return manufacturerName; } }
moel@64
   480
moel@64
   481
      public string ProductName { get { return productName; } }
moel@64
   482
moel@89
   483
      public string Version { get { return version; } }
moel@89
   484
moel@89
   485
      public string SerialNumber { get { return serialNumber; } }
moel@89
   486
moel@64
   487
      public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89
   488
moel@126
   489
      public Model Model { get { return model; } }
moel@126
   490
moel@64
   491
    }
moel@64
   492
  }
moel@64
   493
}