moel@64: /*
moel@64:   
moel@64:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@64: 
moel@64:   The contents of this file are subject to the Mozilla Public License Version
moel@64:   1.1 (the "License"); you may not use this file except in compliance with
moel@64:   the License. You may obtain a copy of the License at
moel@64:  
moel@64:   http://www.mozilla.org/MPL/
moel@64: 
moel@64:   Software distributed under the License is distributed on an "AS IS" basis,
moel@64:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@64:   for the specific language governing rights and limitations under the License.
moel@64: 
moel@64:   The Original Code is the Open Hardware Monitor code.
moel@64: 
moel@64:   The Initial Developer of the Original Code is 
moel@64:   Michael Möller <m.moeller@gmx.ch>.
moel@64:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@64:   the Initial Developer. All Rights Reserved.
moel@64: 
moel@64:   Contributor(s):
moel@64: 
moel@64:   Alternatively, the contents of this file may be used under the terms of
moel@64:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@64:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@64:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@64:   of those above. If you wish to allow use of your version of this file only
moel@64:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@64:   use your version of this file under the terms of the MPL, indicate your
moel@64:   decision by deleting the provisions above and replace them with the notice
moel@64:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@64:   the provisions above, a recipient may use your version of this file under
moel@64:   the terms of any one of the MPL, the GPL or the LGPL.
moel@64:  
moel@64: */
moel@64: 
moel@64: using System;
moel@64: using System.Collections.Generic;
moel@136: using System.IO;
moel@64: using System.Management;
moel@64: using System.Text;
moel@64: 
moel@64: namespace OpenHardwareMonitor.Hardware.Mainboard {
moel@64: 
moel@64:   public class SMBIOS {
moel@64: 
moel@136:     private byte[] raw;
moel@64:     private Structure[] table;
moel@64: 
moel@64:     private BIOSInformation biosInformation = null;
moel@64:     private BaseBoardInformation baseBoardInformation = null;
moel@64: 
moel@136:     private string ReadSysFS(string path) {
moel@136:       try {
moel@136:         if (File.Exists(path)) {
moel@136:           using (StreamReader reader = new StreamReader(path)) 
moel@136:             return reader.ReadLine();
moel@136:         } else {
moel@136:           return null;
moel@136:         }
moel@136:       } catch {
moel@136:         return null;
moel@136:       }
moel@136:     }
moel@136:     
moel@64:     public SMBIOS() {
moel@64:       int p = (int)System.Environment.OSVersion.Platform;
moel@136:       if ((p == 4) || (p == 128)) {
moel@136:         this.raw = null;
moel@136:         this.table = null;
moel@136:         
moel@136:         string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
moel@136:         string boardName = ReadSysFS("/sys/class/dmi/id/board_name");        
moel@136:         string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");        
moel@136:         this.baseBoardInformation = new BaseBoardInformation(
moel@136:           boardVendor, boardName, boardVersion, null);
moel@136:         
moel@136:         string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
moel@136:         string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
moel@136:         this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
moel@136:         
moel@136:       } else {              
moel@136:         List<Structure> structureList = new List<Structure>();
moel@64: 
moel@136:         raw = null;
moel@136:         try {
moel@136:           ManagementObjectCollection collection = 
moel@136:             new ManagementObjectSearcher("root\\WMI", 
moel@136:               "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@136:          
moel@136:           foreach (ManagementObject mo in collection) {
moel@136:             raw = (byte[])mo["SMBiosData"];
moel@89:             break;
moel@136:           }
moel@136:         } catch { }
moel@136:   
moel@136:         if (raw != null && raw.Length > 0) {
moel@136:           int offset = 0;
moel@136:           byte type = raw[offset];
moel@136:           while (offset + 4 < raw.Length && type != 127) {
moel@136:   
moel@136:             type = raw[offset];
moel@136:             int length = raw[offset + 1];
moel@136:             ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
moel@136:   
moel@136:             if (offset + length > raw.Length)
moel@136:               break;
moel@136:             byte[] data = new byte[length];
moel@136:             Array.Copy(raw, offset, data, 0, length);
moel@136:             offset += length;
moel@136:   
moel@136:             List<string> stringsList = new List<string>();
moel@136:             if (offset < raw.Length && raw[offset] == 0)
moel@136:               offset++;
moel@136:   
moel@89:             while (offset < raw.Length && raw[offset] != 0) {
moel@136:               StringBuilder sb = new StringBuilder();
moel@136:               while (offset < raw.Length && raw[offset] != 0) {
moel@136:                 sb.Append((char)raw[offset]); offset++;
moel@136:               }
moel@136:               offset++;
moel@136:               stringsList.Add(sb.ToString());
moel@64:             }
moel@64:             offset++;
moel@136:             switch (type) {
moel@136:               case 0x00:
moel@136:                 this.biosInformation = new BIOSInformation(
moel@136:                   type, handle, data, stringsList.ToArray());
moel@136:                 structureList.Add(this.biosInformation); break;
moel@136:               case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@136:                   type, handle, data, stringsList.ToArray());
moel@136:                 structureList.Add(this.baseBoardInformation); break;
moel@136:               default: structureList.Add(new Structure(
moel@136:                 type, handle, data, stringsList.ToArray())); break;
moel@136:             }
moel@64:           }
moel@64:         }
moel@136:               
moel@136:         table = structureList.ToArray();
moel@89:       }
moel@64:     }
moel@64: 
moel@64:     public string GetReport() {
moel@136:       StringBuilder r = new StringBuilder();
moel@64: 
moel@64:       if (biosInformation != null) {
moel@64:         r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
moel@64:         r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
moel@64:         r.AppendLine();
moel@64:       }
moel@64: 
moel@64:       if (baseBoardInformation != null) {
moel@136:         r.Append("Mainboard Manufacturer: ");
moel@64:         r.AppendLine(baseBoardInformation.ManufacturerName);
moel@136:         r.Append("Mainboard Name: ");
moel@64:         r.AppendLine(baseBoardInformation.ProductName);
moel@64:         r.AppendLine();
moel@64:       }
moel@136: 
moel@136:       if (raw != null) {
moel@136:         string base64 = Convert.ToBase64String(raw);
moel@136:         r.AppendLine("SMBIOS Table");
moel@136:         r.AppendLine();
moel@136: 
moel@136:         for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
moel@136:           r.Append(" ");
moel@136:           for (int j = 0; j < 0x40; j++) {
moel@136:             int index = (i << 6) | j;
moel@136:             if (index < base64.Length) {              
moel@136:               r.Append(base64[index]);
moel@136:             }
moel@136:           }
moel@136:           r.AppendLine();
moel@136:         }
moel@136:         r.AppendLine();
moel@136:       }
moel@136: 
moel@64:       return r.ToString();
moel@64:     }
moel@64: 
moel@64:     public BIOSInformation BIOS {
moel@64:       get { return biosInformation; }
moel@64:     }
moel@64: 
moel@64:     public BaseBoardInformation Board {
moel@64:       get { return baseBoardInformation; }
moel@64:     }
moel@64: 
moel@64:     public class Structure {
moel@64:       private byte type;
moel@64:       private ushort handle;
moel@64: 
moel@64:       private byte[] data;
moel@64:       private string[] strings;
moel@64: 
moel@64:       protected string GetString(int offset) {
moel@64:         if (offset < data.Length && data[offset] > 0 &&
moel@64:          data[offset] <= strings.Length)
moel@64:           return strings[data[offset] - 1];
moel@64:         else
moel@64:           return "";
moel@64:       }
moel@64: 
moel@64:       public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@64:       {
moel@64:         this.type = type;
moel@64:         this.handle = handle;
moel@64:         this.data = data;
moel@64:         this.strings = strings;
moel@64:       }
moel@64: 
moel@64:       public byte Type { get { return type; } }
moel@64: 
moel@64:       public ushort Handle { get { return handle; } }
moel@64:     }
moel@136:       
moel@64:     public class BIOSInformation : Structure {
moel@64: 
moel@64:       private string vendor;
moel@64:       private string version;
moel@136:       
moel@136:       public BIOSInformation(string vendor, string version) 
moel@136:         : base (0x00, 0, null, null) 
moel@136:       {
moel@136:         this.vendor = vendor;
moel@136:         this.version = version;
moel@136:       }
moel@136:       
moel@64:       public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64:         string[] strings)
moel@136:         : base(type, handle, data, strings) 
moel@136:       {
moel@64:         this.vendor = GetString(0x04);
moel@64:         this.version = GetString(0x05);
moel@64:       }
moel@64: 
moel@64:       public string Vendor { get { return vendor; } }
moel@64: 
moel@64:       public string Version { get { return version; } }
moel@64:     }
moel@64: 
moel@64:     public class BaseBoardInformation : Structure {
moel@64: 
moel@64:       private string manufacturerName;
moel@64:       private string productName;
moel@89:       private string version;
moel@89:       private string serialNumber;
moel@64:       private Manufacturer manufacturer;
moel@126:       private Model model;
moel@64: 
moel@136:       private void SetManufacturerName(string manufacturerName) {
moel@136:         this.manufacturerName = manufacturerName;
moel@136:         
moel@64:         switch (manufacturerName) {
moel@64:           case "ASUSTeK Computer INC.":
moel@64:             manufacturer = Manufacturer.ASUS; break;
moel@64:           case "DFI":
moel@72:           case "DFI Inc.":            
moel@64:             manufacturer = Manufacturer.DFI; break;
moel@64:           case "EPoX COMPUTER CO., LTD":
moel@64:             manufacturer = Manufacturer.EPoX; break;
moel@132:           case "EVGA":
moel@132:             manufacturer = Manufacturer.EVGA; break;
moel@64:           case "Gigabyte Technology Co., Ltd.":
moel@64:             manufacturer = Manufacturer.Gigabyte; break;
moel@72:           case "IBM":
moel@72:             manufacturer = Manufacturer.IBM; break;
moel@64:           case "MICRO-STAR INTERNATIONAL CO., LTD":
moel@72:           case "MICRO-STAR INTERNATIONAL CO.,LTD":
moel@64:             manufacturer = Manufacturer.MSI; break;
moel@64:           default:
moel@126:             manufacturer = Manufacturer.Unknown; break;
moel@126:         }
moel@136:       }
moel@136:       
moel@136:       private void SetProductName(string productName) {
moel@136:         this.productName = productName;
moel@136:         
moel@126:         switch (productName) {
moel@133:           case "Crosshair III Formula":
moel@133:             model = Model.Crosshair_III_Formula; break;
moel@133:           case "M2N-SLI DELUXE":
moel@133:             model = Model.M2N_SLI_DELUXE; break;
moel@130:           case "P5W DH Deluxe":
moel@130:             model = Model.P5W_DH_Deluxe; break;
moel@126:           case "LP BI P45-T2RS Elite":
moel@126:             model = Model.LP_BI_P45_T2RS_Elite; break;
moel@126:           case "LP DK P55-T3eH9":
moel@126:             model = Model.LP_DK_P55_T3eH9; break;
moel@132:           case "X58 SLI Classified":
moel@132:             model = Model.X58_SLI_Classified; break;
moel@130:           case "965P-S3":
moel@130:             model = Model._965P_S3; break;
moel@126:           case "EP45-DS3R":
moel@126:             model = Model.EP45_DS3R; break;
moel@130:           case "EP45-UD3R":
moel@130:             model = Model.EP45_UD3R; break;
moel@133:           case "EX58-EXTREME":
moel@133:             model = Model.EX58_EXTREME; break;
moel@126:           case "GA-MA785GMT-UD2H":
moel@126:             model = Model.GA_MA785GMT_UD2H; break;
moel@126:           case "P35-DS3":
moel@126:             model = Model.P35_DS3; break;
moel@133:           case "P35-DS3L":
moel@133:             model = Model.P35_DS3L; break;
moel@130:           case "X38-DS5":
moel@130:             model = Model.X38_DS5; break;
moel@138:           case "X58A-UD3R":
moel@138:             model = Model.X58A_UD3R; break;
moel@126:           default:
moel@126:             model = Model.Unknown; break;
moel@64:         }
moel@64:       }
moel@136:       
moel@136:       public BaseBoardInformation(string manufacturerName, string productName, 
moel@136:         string version, string serialNumber) 
moel@136:         : base(0x02, 0, null, null) 
moel@136:       {        
moel@136:         SetManufacturerName(manufacturerName);
moel@136:         SetProductName(productName);
moel@136:         this.version = version;
moel@136:         this.serialNumber = serialNumber;
moel@136:       }
moel@136:       
moel@136:       public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@136:         string[] strings)
moel@136:         : base(type, handle, data, strings) {
moel@64: 
moel@136:         SetManufacturerName(GetString(0x04).Trim());
moel@136:         SetProductName(GetString(0x05).Trim());
moel@136:         this.version = GetString(0x06).Trim();
moel@136:         this.serialNumber = GetString(0x07).Trim();               
moel@136:       }
moel@136:       
moel@64:       public string ManufacturerName { get { return manufacturerName; } }
moel@64: 
moel@64:       public string ProductName { get { return productName; } }
moel@64: 
moel@89:       public string Version { get { return version; } }
moel@89: 
moel@89:       public string SerialNumber { get { return serialNumber; } }
moel@89: 
moel@64:       public Manufacturer Manufacturer { get { return manufacturer; } }
moel@89: 
moel@126:       public Model Model { get { return model; } }
moel@126: 
moel@64:     }
moel@64:   }
moel@64: }