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