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@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@64:     private Structure[] table;
moel@64: 
moel@64:     private BIOSInformation biosInformation = null;
moel@64:     private BaseBoardInformation baseBoardInformation = null;
moel@64: 
moel@64:     public SMBIOS() {
moel@64:       int p = (int)System.Environment.OSVersion.Platform;
moel@64:       if ((p == 4) || (p == 128))
moel@64:         return;
moel@64:       
moel@64:       List<Structure> structureList = new List<Structure>();
moel@64: 
moel@64:       try {
moel@64:         ManagementObjectCollection collection = new ManagementObjectSearcher(
moel@64:           "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@64: 
moel@64:         byte[] raw = null;
moel@64:         foreach (ManagementObject mo in collection) {
moel@64:           raw = (byte[])mo["SMBiosData"];
moel@64:           break;
moel@64:         }
moel@64: 
moel@64:         if (raw != null && raw.Length > 0) {
moel@64:           int offset = 0;
moel@64:           byte type = raw[offset];
moel@64:           while (offset < raw.Length && type != 127) {
moel@64: 
moel@64:             type = raw[offset]; offset++;
moel@64:             int length = raw[offset]; offset++;
moel@64:             ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]);
moel@64:             offset += 2;
moel@64: 
moel@64:             byte[] data = new byte[length];
moel@64:             Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
moel@64: 
moel@64:             List<string> stringsList = new List<string>();
moel@64:             if (raw[offset] == 0)
moel@64:               offset++;
moel@64:             while (raw[offset] != 0) {
moel@64:               StringBuilder sb = new StringBuilder();
moel@64:               while (raw[offset] != 0) {
moel@64:                 sb.Append((char)raw[offset]); offset++;
moel@64:               }
moel@64:               offset++;
moel@64:               stringsList.Add(sb.ToString());
moel@64:             }
moel@64:             offset++;
moel@64:             switch (type) {
moel@64:               case 0x00:
moel@64:                 this.biosInformation = new BIOSInformation(
moel@64:                   type, handle, data, stringsList.ToArray());
moel@64:                 structureList.Add(this.biosInformation); break;
moel@64:               case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@64:                   type, handle, data, stringsList.ToArray());
moel@64:                 structureList.Add(this.baseBoardInformation); break;
moel@64:               default: structureList.Add(new Structure(
moel@64:                 type, handle, data, stringsList.ToArray())); break;
moel@64:             }
moel@64:           }
moel@64:         }
moel@64:       } catch (NotImplementedException) { } catch (ManagementException) { }
moel@64:       
moel@64:       table = structureList.ToArray();
moel@64:     }
moel@64: 
moel@64:     public string GetReport() {
moel@64:       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@64:         r.Append("Mainboard Manufacturer: "); 
moel@64:         r.AppendLine(baseBoardInformation.ManufacturerName);
moel@64:         r.Append("Mainboard Name: "); 
moel@64:         r.AppendLine(baseBoardInformation.ProductName);
moel@64:         r.AppendLine();
moel@64:       }
moel@64:      
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@64: 
moel@64:     public class BIOSInformation : Structure {
moel@64: 
moel@64:       private string vendor;
moel@64:       private string version;
moel@64: 
moel@64:       public BIOSInformation(byte type, ushort handle, byte[] data,
moel@64:         string[] strings)
moel@64:         : base(type, handle, data, strings) {
moel@64: 
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@64:       private Manufacturer manufacturer;
moel@64: 
moel@64:       public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@64:         string[] strings)
moel@64:         : base(type, handle, data, strings) {
moel@64: 
moel@64:         this.manufacturerName = GetString(0x04).Trim();
moel@64:         this.productName = GetString(0x05).Trim();
moel@64: 
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@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@64:             manufacturer = Manufacturer.Unkown; break;
moel@64:         }
moel@64:       }
moel@64: 
moel@64:       public string ManufacturerName { get { return manufacturerName; } }
moel@64: 
moel@64:       public string ProductName { get { return productName; } }
moel@64: 
moel@64:       public Manufacturer Manufacturer { get { return manufacturer; } }
moel@64:     }
moel@64:   }
moel@64: }