Hardware/Mainboard/SMBIOS.cs
author moel.mich
Sun, 07 Mar 2010 12:17:21 +0000
changeset 75 be9bbc350884
parent 64 15181001ee61
child 89 62e0b6011e8c
permissions -rw-r--r--
Changed the T-Balancer fields for the actual PWM (percent) value of the channels from 156+ to 137+, because on some (older?) hardware the fields 156+ seem not to be updated. Fields 137+ have one bit less resolution, but it's what the Navigator uses as well (and used only if in PWM mode).
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Management;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.Mainboard {
    44 
    45   public class SMBIOS {
    46 
    47     private Structure[] table;
    48 
    49     private BIOSInformation biosInformation = null;
    50     private BaseBoardInformation baseBoardInformation = null;
    51 
    52     public SMBIOS() {
    53       int p = (int)System.Environment.OSVersion.Platform;
    54       if ((p == 4) || (p == 128))
    55         return;
    56       
    57       List<Structure> structureList = new List<Structure>();
    58 
    59       try {
    60         ManagementObjectCollection collection = new ManagementObjectSearcher(
    61           "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    62 
    63         byte[] raw = null;
    64         foreach (ManagementObject mo in collection) {
    65           raw = (byte[])mo["SMBiosData"];
    66           break;
    67         }
    68 
    69         if (raw != null && raw.Length > 0) {
    70           int offset = 0;
    71           byte type = raw[offset];
    72           while (offset < raw.Length && type != 127) {
    73 
    74             type = raw[offset]; offset++;
    75             int length = raw[offset]; offset++;
    76             ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]);
    77             offset += 2;
    78 
    79             byte[] data = new byte[length];
    80             Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
    81 
    82             List<string> stringsList = new List<string>();
    83             if (raw[offset] == 0)
    84               offset++;
    85             while (raw[offset] != 0) {
    86               StringBuilder sb = new StringBuilder();
    87               while (raw[offset] != 0) {
    88                 sb.Append((char)raw[offset]); offset++;
    89               }
    90               offset++;
    91               stringsList.Add(sb.ToString());
    92             }
    93             offset++;
    94             switch (type) {
    95               case 0x00:
    96                 this.biosInformation = new BIOSInformation(
    97                   type, handle, data, stringsList.ToArray());
    98                 structureList.Add(this.biosInformation); break;
    99               case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   100                   type, handle, data, stringsList.ToArray());
   101                 structureList.Add(this.baseBoardInformation); break;
   102               default: structureList.Add(new Structure(
   103                 type, handle, data, stringsList.ToArray())); break;
   104             }
   105           }
   106         }
   107       } catch (NotImplementedException) { } catch (ManagementException) { }
   108       
   109       table = structureList.ToArray();
   110     }
   111 
   112     public string GetReport() {
   113       StringBuilder r = new StringBuilder();      
   114 
   115       if (biosInformation != null) {
   116         r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   117         r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
   118         r.AppendLine();
   119       }
   120 
   121       if (baseBoardInformation != null) {
   122         r.Append("Mainboard Manufacturer: "); 
   123         r.AppendLine(baseBoardInformation.ManufacturerName);
   124         r.Append("Mainboard Name: "); 
   125         r.AppendLine(baseBoardInformation.ProductName);
   126         r.AppendLine();
   127       }
   128      
   129       return r.ToString();
   130     }
   131 
   132     public BIOSInformation BIOS {
   133       get { return biosInformation; }
   134     }
   135 
   136     public BaseBoardInformation Board {
   137       get { return baseBoardInformation; }
   138     }
   139 
   140     public class Structure {
   141       private byte type;
   142       private ushort handle;
   143 
   144       private byte[] data;
   145       private string[] strings;
   146 
   147       protected string GetString(int offset) {
   148         if (offset < data.Length && data[offset] > 0 &&
   149          data[offset] <= strings.Length)
   150           return strings[data[offset] - 1];
   151         else
   152           return "";
   153       }
   154 
   155       public Structure(byte type, ushort handle, byte[] data, string[] strings) 
   156       {
   157         this.type = type;
   158         this.handle = handle;
   159         this.data = data;
   160         this.strings = strings;
   161       }
   162 
   163       public byte Type { get { return type; } }
   164 
   165       public ushort Handle { get { return handle; } }
   166     }
   167 
   168     public class BIOSInformation : Structure {
   169 
   170       private string vendor;
   171       private string version;
   172 
   173       public BIOSInformation(byte type, ushort handle, byte[] data,
   174         string[] strings)
   175         : base(type, handle, data, strings) {
   176 
   177         this.vendor = GetString(0x04);
   178         this.version = GetString(0x05);
   179       }
   180 
   181       public string Vendor { get { return vendor; } }
   182 
   183       public string Version { get { return version; } }
   184     }
   185 
   186     public class BaseBoardInformation : Structure {
   187 
   188       private string manufacturerName;
   189       private string productName;
   190       private Manufacturer manufacturer;
   191 
   192       public BaseBoardInformation(byte type, ushort handle, byte[] data,
   193         string[] strings)
   194         : base(type, handle, data, strings) {
   195 
   196         this.manufacturerName = GetString(0x04).Trim();
   197         this.productName = GetString(0x05).Trim();
   198 
   199         switch (manufacturerName) {
   200           case "ASUSTeK Computer INC.":
   201             manufacturer = Manufacturer.ASUS; break;
   202           case "DFI":
   203           case "DFI Inc.":            
   204             manufacturer = Manufacturer.DFI; break;
   205           case "EPoX COMPUTER CO., LTD":
   206             manufacturer = Manufacturer.EPoX; break;
   207           case "Gigabyte Technology Co., Ltd.":
   208             manufacturer = Manufacturer.Gigabyte; break;
   209           case "IBM":
   210             manufacturer = Manufacturer.IBM; break;
   211           case "MICRO-STAR INTERNATIONAL CO., LTD":
   212           case "MICRO-STAR INTERNATIONAL CO.,LTD":
   213             manufacturer = Manufacturer.MSI; break;
   214           default:
   215             manufacturer = Manufacturer.Unkown; break;
   216         }
   217       }
   218 
   219       public string ManufacturerName { get { return manufacturerName; } }
   220 
   221       public string ProductName { get { return productName; } }
   222 
   223       public Manufacturer Manufacturer { get { return manufacturer; } }
   224     }
   225   }
   226 }