Hardware/SMBIOS.cs
author moel.mich
Wed, 25 Jul 2012 15:33:16 +0000
changeset 373 3b8443edb0e6
parent 370 8e4dedc41924
child 378 64d3ddf8d73b
permissions -rw-r--r--
Added a new icon for RAM hardware.
moel@370
     1
/*
moel@370
     2
 
moel@370
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@370
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@370
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@370
     6
 
moel@370
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@370
     8
	
moel@370
     9
*/
moel@370
    10
moel@370
    11
using System;
moel@370
    12
using System.Collections.Generic;
moel@370
    13
using System.IO;
moel@370
    14
using System.Management;
moel@370
    15
using System.Text;
moel@370
    16
moel@370
    17
namespace OpenHardwareMonitor.Hardware {
moel@370
    18
moel@370
    19
  internal class SMBIOS {
moel@370
    20
moel@370
    21
    private readonly byte[] raw;
moel@370
    22
    private readonly Structure[] table;
moel@370
    23
moel@370
    24
    private readonly Version version;
moel@370
    25
    private readonly BIOSInformation biosInformation;
moel@370
    26
    private readonly SystemInformation systemInformation;
moel@370
    27
    private readonly BaseBoardInformation baseBoardInformation;
moel@370
    28
    private readonly MemoryDevice[] memoryDevices;
moel@370
    29
moel@370
    30
    private static string ReadSysFS(string path) {
moel@370
    31
      try {
moel@370
    32
        if (File.Exists(path)) {
moel@370
    33
          using (StreamReader reader = new StreamReader(path)) 
moel@370
    34
            return reader.ReadLine();
moel@370
    35
        } else {
moel@370
    36
          return null;
moel@370
    37
        }
moel@370
    38
      } catch {
moel@370
    39
        return null;
moel@370
    40
      }
moel@370
    41
    }
moel@370
    42
    
moel@370
    43
    public SMBIOS() {
moel@370
    44
      int p = (int)Environment.OSVersion.Platform;
moel@370
    45
      if ((p == 4) || (p == 128)) {
moel@370
    46
        this.raw = null;
moel@370
    47
        this.table = null;
moel@370
    48
        
moel@370
    49
        string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
moel@370
    50
        string boardName = ReadSysFS("/sys/class/dmi/id/board_name");        
moel@370
    51
        string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");        
moel@370
    52
        this.baseBoardInformation = new BaseBoardInformation(
moel@370
    53
          boardVendor, boardName, boardVersion, null);
moel@370
    54
moel@370
    55
        string systemVendor = ReadSysFS("/sys/class/dmi/id/sys_vendor");
moel@370
    56
        string productName = ReadSysFS("/sys/class/dmi/id/product_name");
moel@370
    57
        string productVersion = ReadSysFS("/sys/class/dmi/id/product_version");    
moel@370
    58
        this.systemInformation = new SystemInformation(systemVendor, 
moel@370
    59
          productName, productVersion, null, null);
moel@370
    60
moel@370
    61
        string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
moel@370
    62
        string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
moel@370
    63
        this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
moel@370
    64
moel@370
    65
        this.memoryDevices = new MemoryDevice[0];
moel@370
    66
      } else {              
moel@370
    67
        List<Structure> structureList = new List<Structure>();
moel@370
    68
        List<MemoryDevice> memoryDeviceList = new List<MemoryDevice>();
moel@370
    69
moel@370
    70
        raw = null;
moel@370
    71
        byte majorVersion = 0;
moel@370
    72
        byte minorVersion = 0;
moel@370
    73
        try {
moel@370
    74
          ManagementObjectCollection collection;
moel@370
    75
          using (ManagementObjectSearcher searcher = 
moel@370
    76
            new ManagementObjectSearcher("root\\WMI", 
moel@370
    77
              "SELECT * FROM MSSMBios_RawSMBiosTables")) {
moel@370
    78
            collection = searcher.Get();
moel@370
    79
          }
moel@370
    80
         
moel@370
    81
          foreach (ManagementObject mo in collection) {
moel@370
    82
            raw = (byte[])mo["SMBiosData"];
moel@370
    83
            majorVersion = (byte)mo["SmbiosMajorVersion"];
moel@370
    84
            minorVersion = (byte)mo["SmbiosMinorVersion"];            
moel@370
    85
            break;
moel@370
    86
          }
moel@370
    87
        } catch { }
moel@370
    88
moel@370
    89
        if (majorVersion > 0 || minorVersion > 0)
moel@370
    90
          version = new Version(majorVersion, minorVersion);
moel@370
    91
  
moel@370
    92
        if (raw != null && raw.Length > 0) {
moel@370
    93
          int offset = 0;
moel@370
    94
          byte type = raw[offset];
moel@370
    95
          while (offset + 4 < raw.Length && type != 127) {
moel@370
    96
  
moel@370
    97
            type = raw[offset];
moel@370
    98
            int length = raw[offset + 1];
moel@370
    99
            ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
moel@370
   100
  
moel@370
   101
            if (offset + length > raw.Length)
moel@370
   102
              break;
moel@370
   103
            byte[] data = new byte[length];
moel@370
   104
            Array.Copy(raw, offset, data, 0, length);
moel@370
   105
            offset += length;
moel@370
   106
  
moel@370
   107
            List<string> stringsList = new List<string>();
moel@370
   108
            if (offset < raw.Length && raw[offset] == 0)
moel@370
   109
              offset++;
moel@370
   110
  
moel@370
   111
            while (offset < raw.Length && raw[offset] != 0) {
moel@370
   112
              StringBuilder sb = new StringBuilder();
moel@370
   113
              while (offset < raw.Length && raw[offset] != 0) {
moel@370
   114
                sb.Append((char)raw[offset]); offset++;
moel@370
   115
              }
moel@370
   116
              offset++;
moel@370
   117
              stringsList.Add(sb.ToString());
moel@370
   118
            }
moel@370
   119
            offset++;
moel@370
   120
            switch (type) {
moel@370
   121
              case 0x00:
moel@370
   122
                this.biosInformation = new BIOSInformation(
moel@370
   123
                  type, handle, data, stringsList.ToArray());
moel@370
   124
                structureList.Add(this.biosInformation); break;
moel@370
   125
              case 0x01:
moel@370
   126
                this.systemInformation = new SystemInformation(
moel@370
   127
                  type, handle, data, stringsList.ToArray());
moel@370
   128
                structureList.Add(this.systemInformation); break;
moel@370
   129
              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@370
   130
                  type, handle, data, stringsList.ToArray());
moel@370
   131
                structureList.Add(this.baseBoardInformation); break;
moel@370
   132
              case 0x11: MemoryDevice m = new MemoryDevice(
moel@370
   133
                  type, handle, data, stringsList.ToArray());
moel@370
   134
                memoryDeviceList.Add(m);
moel@370
   135
                structureList.Add(m); break;
moel@370
   136
              default: structureList.Add(new Structure(
moel@370
   137
                type, handle, data, stringsList.ToArray())); break;
moel@370
   138
            }
moel@370
   139
          }
moel@370
   140
        }
moel@370
   141
moel@370
   142
        memoryDevices = memoryDeviceList.ToArray();
moel@370
   143
        table = structureList.ToArray();
moel@370
   144
      }
moel@370
   145
    }
moel@370
   146
moel@370
   147
    public string GetReport() {
moel@370
   148
      StringBuilder r = new StringBuilder();
moel@370
   149
moel@370
   150
      if (version != null) {
moel@370
   151
        r.Append("SMBIOS Version: "); r.AppendLine(version.ToString(2));
moel@370
   152
        r.AppendLine();
moel@370
   153
      }
moel@370
   154
moel@370
   155
      if (BIOS != null) {
moel@370
   156
        r.Append("BIOS Vendor: "); r.AppendLine(BIOS.Vendor);
moel@370
   157
        r.Append("BIOS Version: "); r.AppendLine(BIOS.Version);
moel@370
   158
        r.AppendLine();
moel@370
   159
      }
moel@370
   160
moel@370
   161
      if (System != null) {
moel@370
   162
        r.Append("System Manufacturer: ");
moel@370
   163
        r.AppendLine(System.ManufacturerName);
moel@370
   164
        r.Append("System Name: ");
moel@370
   165
        r.AppendLine(System.ProductName);
moel@370
   166
        r.Append("System Version: ");
moel@370
   167
        r.AppendLine(System.Version);
moel@370
   168
        r.AppendLine();
moel@370
   169
      }
moel@370
   170
moel@370
   171
      if (Board != null) {
moel@370
   172
        r.Append("Mainboard Manufacturer: ");
moel@370
   173
        r.AppendLine(Board.ManufacturerName);
moel@370
   174
        r.Append("Mainboard Name: ");
moel@370
   175
        r.AppendLine(Board.ProductName);
moel@370
   176
        r.Append("Mainboard Version: ");
moel@370
   177
        r.AppendLine(Board.Version);
moel@370
   178
        r.AppendLine();
moel@370
   179
      }
moel@370
   180
moel@370
   181
      for (int i = 0; i < MemoryDevices.Length; i++) {        
moel@370
   182
        r.Append("Memory Device [" + i + "] Manufacturer: ");
moel@370
   183
        r.AppendLine(MemoryDevices[i].ManufacturerName);
moel@370
   184
        r.Append("Memory Device [" + i + "] Part Number: ");
moel@370
   185
        r.AppendLine(MemoryDevices[i].PartNumber);
moel@373
   186
        r.Append("Memory Device [" + i + "] Device Locator: ");
moel@373
   187
        r.AppendLine(MemoryDevices[i].DeviceLocator);
moel@373
   188
        r.Append("Memory Device [" + i + "] Bank Locator: ");
moel@373
   189
        r.AppendLine(MemoryDevices[i].BankLocator);
moel@370
   190
        r.AppendLine();
moel@370
   191
      }
moel@370
   192
moel@370
   193
      if (raw != null) {
moel@370
   194
        string base64 = Convert.ToBase64String(raw);
moel@370
   195
        r.AppendLine("SMBIOS Table");
moel@370
   196
        r.AppendLine();
moel@370
   197
moel@370
   198
        for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
moel@370
   199
          r.Append(" ");
moel@370
   200
          for (int j = 0; j < 0x40; j++) {
moel@370
   201
            int index = (i << 6) | j;
moel@370
   202
            if (index < base64.Length) {              
moel@370
   203
              r.Append(base64[index]);
moel@370
   204
            }
moel@370
   205
          }
moel@370
   206
          r.AppendLine();
moel@370
   207
        }
moel@370
   208
        r.AppendLine();
moel@370
   209
      }
moel@370
   210
moel@370
   211
      return r.ToString();
moel@370
   212
    }
moel@370
   213
moel@370
   214
    public BIOSInformation BIOS {
moel@370
   215
      get { return biosInformation; }
moel@370
   216
    }
moel@370
   217
moel@370
   218
    public SystemInformation System {
moel@370
   219
      get { return systemInformation; }
moel@370
   220
    }
moel@370
   221
moel@370
   222
    public BaseBoardInformation Board {
moel@370
   223
      get { return baseBoardInformation; }
moel@370
   224
    }
moel@370
   225
moel@370
   226
    public MemoryDevice[] MemoryDevices {
moel@370
   227
      get { return memoryDevices; }
moel@370
   228
    }
moel@370
   229
moel@370
   230
    public class Structure {
moel@370
   231
      private readonly byte type;
moel@370
   232
      private readonly ushort handle;
moel@370
   233
moel@370
   234
      private readonly byte[] data;
moel@370
   235
      private readonly string[] strings;
moel@370
   236
moel@370
   237
      protected string GetString(int offset) {
moel@370
   238
        if (offset < data.Length && data[offset] > 0 &&
moel@370
   239
         data[offset] <= strings.Length)
moel@370
   240
          return strings[data[offset] - 1];
moel@370
   241
        else
moel@370
   242
          return "";
moel@370
   243
      }
moel@370
   244
moel@370
   245
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@370
   246
      {
moel@370
   247
        this.type = type;
moel@370
   248
        this.handle = handle;
moel@370
   249
        this.data = data;
moel@370
   250
        this.strings = strings;
moel@370
   251
      }
moel@370
   252
moel@370
   253
      public byte Type { get { return type; } }
moel@370
   254
moel@370
   255
      public ushort Handle { get { return handle; } }
moel@370
   256
    }
moel@370
   257
      
moel@370
   258
    public class BIOSInformation : Structure {
moel@370
   259
moel@370
   260
      private readonly string vendor;
moel@370
   261
      private readonly string version;
moel@370
   262
      
moel@370
   263
      public BIOSInformation(string vendor, string version) 
moel@370
   264
        : base (0x00, 0, null, null) 
moel@370
   265
      {
moel@370
   266
        this.vendor = vendor;
moel@370
   267
        this.version = version;
moel@370
   268
      }
moel@370
   269
      
moel@370
   270
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@370
   271
        string[] strings)
moel@370
   272
        : base(type, handle, data, strings) 
moel@370
   273
      {
moel@370
   274
        this.vendor = GetString(0x04);
moel@370
   275
        this.version = GetString(0x05);
moel@370
   276
      }
moel@370
   277
moel@370
   278
      public string Vendor { get { return vendor; } }
moel@370
   279
moel@370
   280
      public string Version { get { return version; } }
moel@370
   281
    }
moel@370
   282
moel@370
   283
    public class SystemInformation : Structure {
moel@370
   284
moel@370
   285
      private readonly string manufacturerName;
moel@370
   286
      private readonly string productName;
moel@370
   287
      private readonly string version;
moel@370
   288
      private readonly string serialNumber;
moel@370
   289
      private readonly string family;
moel@370
   290
moel@370
   291
      public SystemInformation(string manufacturerName, string productName, 
moel@370
   292
        string version, string serialNumber, string family) 
moel@370
   293
        : base (0x01, 0, null, null) 
moel@370
   294
      {
moel@370
   295
        this.manufacturerName = manufacturerName;
moel@370
   296
        this.productName = productName;
moel@370
   297
        this.version = version;
moel@370
   298
        this.serialNumber = serialNumber;
moel@370
   299
        this.family = family;
moel@370
   300
      }
moel@370
   301
moel@370
   302
      public SystemInformation(byte type, ushort handle, byte[] data,
moel@370
   303
        string[] strings)
moel@370
   304
        : base(type, handle, data, strings) 
moel@370
   305
      {
moel@370
   306
        this.manufacturerName = GetString(0x04);
moel@370
   307
        this.productName = GetString(0x05);
moel@370
   308
        this.version = GetString(0x06);
moel@370
   309
        this.serialNumber = GetString(0x07);
moel@370
   310
        this.family = GetString(0x1A);
moel@370
   311
      }
moel@370
   312
moel@370
   313
      public string ManufacturerName { get { return manufacturerName; } }
moel@370
   314
moel@370
   315
      public string ProductName { get { return productName; } }
moel@370
   316
moel@370
   317
      public string Version { get { return version; } }
moel@370
   318
moel@370
   319
      public string SerialNumber { get { return serialNumber; } }
moel@370
   320
moel@370
   321
      public string Family { get { return family; } }
moel@370
   322
moel@370
   323
    }
moel@370
   324
moel@370
   325
    public class BaseBoardInformation : Structure {
moel@370
   326
moel@370
   327
      private readonly string manufacturerName;
moel@370
   328
      private readonly string productName;
moel@370
   329
      private readonly string version;
moel@370
   330
      private readonly string serialNumber;
moel@370
   331
      
moel@370
   332
      public BaseBoardInformation(string manufacturerName, string productName, 
moel@370
   333
        string version, string serialNumber) 
moel@370
   334
        : base(0x02, 0, null, null) 
moel@370
   335
      {
moel@370
   336
        this.manufacturerName = manufacturerName;
moel@370
   337
        this.productName = productName;
moel@370
   338
        this.version = version;
moel@370
   339
        this.serialNumber = serialNumber;
moel@370
   340
      }
moel@370
   341
      
moel@370
   342
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@370
   343
        string[] strings)
moel@370
   344
        : base(type, handle, data, strings) {
moel@370
   345
moel@370
   346
        this.manufacturerName = GetString(0x04).Trim();
moel@370
   347
        this.productName = GetString(0x05).Trim();
moel@370
   348
        this.version = GetString(0x06).Trim();
moel@370
   349
        this.serialNumber = GetString(0x07).Trim();               
moel@370
   350
      }
moel@370
   351
      
moel@370
   352
      public string ManufacturerName { get { return manufacturerName; } }
moel@370
   353
moel@370
   354
      public string ProductName { get { return productName; } }
moel@370
   355
moel@370
   356
      public string Version { get { return version; } }
moel@370
   357
moel@370
   358
      public string SerialNumber { get { return serialNumber; } }
moel@370
   359
moel@370
   360
    }
moel@370
   361
moel@370
   362
    public class MemoryDevice : Structure {
moel@370
   363
moel@373
   364
      private readonly string deviceLocator;
moel@373
   365
      private readonly string bankLocator;
moel@370
   366
      private readonly string manufacturerName;
moel@370
   367
      private readonly string serialNumber;
moel@373
   368
      private readonly string partNumber;      
moel@370
   369
moel@370
   370
      public MemoryDevice(byte type, ushort handle, byte[] data,
moel@370
   371
        string[] strings)
moel@373
   372
        : base(type, handle, data, strings) 
moel@373
   373
      {
moel@373
   374
        this.deviceLocator = GetString(0x10).Trim();
moel@373
   375
        this.bankLocator = GetString(0x11).Trim();
moel@370
   376
        this.manufacturerName = GetString(0x17).Trim();
moel@370
   377
        this.serialNumber = GetString(0x18).Trim();
moel@370
   378
        this.partNumber = GetString(0x1A).Trim();
moel@370
   379
      }
moel@370
   380
moel@373
   381
      public string DeviceLocator { get { return deviceLocator; } }
moel@373
   382
moel@373
   383
      public string BankLocator { get { return bankLocator; } }
moel@373
   384
moel@370
   385
      public string ManufacturerName { get { return manufacturerName; } }
moel@370
   386
moel@370
   387
      public string SerialNumber { get { return serialNumber; } }
moel@370
   388
moel@370
   389
      public string PartNumber { get { return partNumber; } }
moel@370
   390
moel@373
   391
       
moel@373
   392
moel@370
   393
    }
moel@370
   394
  }
moel@370
   395
}