Hardware/Mainboard/SMBIOS.cs
author moel.mich
Thu, 03 Jun 2010 22:40:18 +0000
changeset 130 80065ab20b84
parent 126 2354fdb91ac4
child 132 680ed54e05ca
permissions -rw-r--r--
Fixed Issue 63.
     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       byte[] raw = null;
    60       try {
    61         ManagementObjectCollection collection = new ManagementObjectSearcher(
    62           "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    63        
    64         foreach (ManagementObject mo in collection) {
    65           raw = (byte[])mo["SMBiosData"];
    66           break;
    67         }
    68       } catch { }      
    69 
    70       if (raw != null && raw.Length > 0) {
    71         int offset = 0;
    72         byte type = raw[offset];
    73         while (offset + 4 < raw.Length && type != 127) {
    74 
    75           type = raw[offset];
    76           int length = raw[offset + 1];
    77           ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
    78 
    79           if (offset + length > raw.Length)
    80             break;
    81           byte[] data = new byte[length];
    82           Array.Copy(raw, offset, data, 0, length);
    83           offset += length;
    84 
    85           List<string> stringsList = new List<string>();
    86           if (offset < raw.Length && raw[offset] == 0)
    87             offset++;
    88 
    89           while (offset < raw.Length && raw[offset] != 0) {
    90             StringBuilder sb = new StringBuilder();
    91             while (offset < raw.Length && raw[offset] != 0) {
    92               sb.Append((char)raw[offset]); offset++;
    93             }
    94             offset++;
    95             stringsList.Add(sb.ToString());
    96           }
    97           offset++;
    98           switch (type) {
    99             case 0x00:
   100               this.biosInformation = new BIOSInformation(
   101                 type, handle, data, stringsList.ToArray());
   102               structureList.Add(this.biosInformation); break;
   103             case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   104                 type, handle, data, stringsList.ToArray());
   105               structureList.Add(this.baseBoardInformation); break;
   106             default: structureList.Add(new Structure(
   107               type, handle, data, stringsList.ToArray())); break;
   108           }
   109         }
   110       }
   111             
   112       table = structureList.ToArray();
   113     }
   114 
   115     public string GetReport() {
   116       StringBuilder r = new StringBuilder();      
   117 
   118       if (biosInformation != null) {
   119         r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   120         r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
   121         r.AppendLine();
   122       }
   123 
   124       if (baseBoardInformation != null) {
   125         r.Append("Mainboard Manufacturer: "); 
   126         r.AppendLine(baseBoardInformation.ManufacturerName);
   127         r.Append("Mainboard Name: "); 
   128         r.AppendLine(baseBoardInformation.ProductName);
   129         r.AppendLine();
   130       }
   131      
   132       return r.ToString();
   133     }
   134 
   135     public BIOSInformation BIOS {
   136       get { return biosInformation; }
   137     }
   138 
   139     public BaseBoardInformation Board {
   140       get { return baseBoardInformation; }
   141     }
   142 
   143     public class Structure {
   144       private byte type;
   145       private ushort handle;
   146 
   147       private byte[] data;
   148       private string[] strings;
   149 
   150       protected string GetString(int offset) {
   151         if (offset < data.Length && data[offset] > 0 &&
   152          data[offset] <= strings.Length)
   153           return strings[data[offset] - 1];
   154         else
   155           return "";
   156       }
   157 
   158       public Structure(byte type, ushort handle, byte[] data, string[] strings) 
   159       {
   160         this.type = type;
   161         this.handle = handle;
   162         this.data = data;
   163         this.strings = strings;
   164       }
   165 
   166       public byte Type { get { return type; } }
   167 
   168       public ushort Handle { get { return handle; } }
   169     }
   170 
   171     public class BIOSInformation : Structure {
   172 
   173       private string vendor;
   174       private string version;
   175 
   176       public BIOSInformation(byte type, ushort handle, byte[] data,
   177         string[] strings)
   178         : base(type, handle, data, strings) {
   179 
   180         this.vendor = GetString(0x04);
   181         this.version = GetString(0x05);
   182       }
   183 
   184       public string Vendor { get { return vendor; } }
   185 
   186       public string Version { get { return version; } }
   187     }
   188 
   189     public class BaseBoardInformation : Structure {
   190 
   191       private string manufacturerName;
   192       private string productName;
   193       private string version;
   194       private string serialNumber;
   195       private Manufacturer manufacturer;
   196       private Model model;
   197 
   198       public BaseBoardInformation(byte type, ushort handle, byte[] data,
   199         string[] strings)
   200         : base(type, handle, data, strings) {
   201 
   202         this.manufacturerName = GetString(0x04).Trim();
   203         this.productName = GetString(0x05).Trim();
   204         this.version = GetString(0x06).Trim();
   205         this.serialNumber = GetString(0x07).Trim();
   206 
   207         switch (manufacturerName) {
   208           case "ASUSTeK Computer INC.":
   209             manufacturer = Manufacturer.ASUS; break;
   210           case "DFI":
   211           case "DFI Inc.":            
   212             manufacturer = Manufacturer.DFI; break;
   213           case "EPoX COMPUTER CO., LTD":
   214             manufacturer = Manufacturer.EPoX; break;
   215           case "Gigabyte Technology Co., Ltd.":
   216             manufacturer = Manufacturer.Gigabyte; break;
   217           case "IBM":
   218             manufacturer = Manufacturer.IBM; break;
   219           case "MICRO-STAR INTERNATIONAL CO., LTD":
   220           case "MICRO-STAR INTERNATIONAL CO.,LTD":
   221             manufacturer = Manufacturer.MSI; break;
   222           default:
   223             manufacturer = Manufacturer.Unknown; break;
   224         }
   225 
   226         switch (productName) {
   227           case "P5W DH Deluxe":
   228             model = Model.P5W_DH_Deluxe; break;
   229           case "LP BI P45-T2RS Elite":
   230             model = Model.LP_BI_P45_T2RS_Elite; break;
   231           case "LP DK P55-T3eH9":
   232             model = Model.LP_DK_P55_T3eH9; break;
   233           case "965P-S3":
   234             model = Model._965P_S3; break;
   235           case "EP45-DS3R":
   236             model = Model.EP45_DS3R; break;
   237           case "EP45-UD3R":
   238             model = Model.EP45_UD3R; break;
   239           case "GA-MA785GMT-UD2H":
   240             model = Model.GA_MA785GMT_UD2H; break;
   241           case "P35-DS3":
   242             model = Model.P35_DS3; break;
   243           case "X38-DS5":
   244             model = Model.X38_DS5; break;
   245           default:
   246             model = Model.Unknown; break;
   247         }
   248       }
   249 
   250       public string ManufacturerName { get { return manufacturerName; } }
   251 
   252       public string ProductName { get { return productName; } }
   253 
   254       public string Version { get { return version; } }
   255 
   256       public string SerialNumber { get { return serialNumber; } }
   257 
   258       public Manufacturer Manufacturer { get { return manufacturer; } }
   259 
   260       public Model Model { get { return model; } }
   261 
   262     }
   263   }
   264 }