Hardware/Mainboard/SMBIOS.cs
author moel.mich
Mon, 05 Apr 2010 21:31:21 +0000
changeset 89 62e0b6011e8c
parent 72 c4dfd596a6c3
child 103 0845adc5402e
permissions -rw-r--r--
Fixed Issue 25.
     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 (NotImplementedException) { } catch (ManagementException) { }
    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 
   197       public BaseBoardInformation(byte type, ushort handle, byte[] data,
   198         string[] strings)
   199         : base(type, handle, data, strings) {
   200 
   201         this.manufacturerName = GetString(0x04).Trim();
   202         this.productName = GetString(0x05).Trim();
   203         this.version = GetString(0x06).Trim();
   204         this.serialNumber = GetString(0x07).Trim();
   205 
   206         switch (manufacturerName) {
   207           case "ASUSTeK Computer INC.":
   208             manufacturer = Manufacturer.ASUS; break;
   209           case "DFI":
   210           case "DFI Inc.":            
   211             manufacturer = Manufacturer.DFI; break;
   212           case "EPoX COMPUTER CO., LTD":
   213             manufacturer = Manufacturer.EPoX; break;
   214           case "Gigabyte Technology Co., Ltd.":
   215             manufacturer = Manufacturer.Gigabyte; break;
   216           case "IBM":
   217             manufacturer = Manufacturer.IBM; break;
   218           case "MICRO-STAR INTERNATIONAL CO., LTD":
   219           case "MICRO-STAR INTERNATIONAL CO.,LTD":
   220             manufacturer = Manufacturer.MSI; break;
   221           default:
   222             manufacturer = Manufacturer.Unkown; break;
   223         }
   224       }
   225 
   226       public string ManufacturerName { get { return manufacturerName; } }
   227 
   228       public string ProductName { get { return productName; } }
   229 
   230       public string Version { get { return version; } }
   231 
   232       public string SerialNumber { get { return serialNumber; } }
   233 
   234       public Manufacturer Manufacturer { get { return manufacturer; } }
   235 
   236     }
   237   }
   238 }