Hardware/Mainboard/SMBIOS.cs
changeset 136 fa2957aa0699
parent 133 9ad699538c89
child 138 1301992d8ae5
     1.1 --- a/Hardware/Mainboard/SMBIOS.cs	Sun Jun 06 14:44:53 2010 +0000
     1.2 +++ b/Hardware/Mainboard/SMBIOS.cs	Mon Jun 07 20:03:48 2010 +0000
     1.3 @@ -37,6 +37,7 @@
     1.4  
     1.5  using System;
     1.6  using System.Collections.Generic;
     1.7 +using System.IO;
     1.8  using System.Management;
     1.9  using System.Text;
    1.10  
    1.11 @@ -44,76 +45,104 @@
    1.12  
    1.13    public class SMBIOS {
    1.14  
    1.15 +    private byte[] raw;
    1.16      private Structure[] table;
    1.17  
    1.18      private BIOSInformation biosInformation = null;
    1.19      private BaseBoardInformation baseBoardInformation = null;
    1.20  
    1.21 +    private string ReadSysFS(string path) {
    1.22 +      try {
    1.23 +        if (File.Exists(path)) {
    1.24 +          using (StreamReader reader = new StreamReader(path)) 
    1.25 +            return reader.ReadLine();
    1.26 +        } else {
    1.27 +          return null;
    1.28 +        }
    1.29 +      } catch {
    1.30 +        return null;
    1.31 +      }
    1.32 +    }
    1.33 +    
    1.34      public SMBIOS() {
    1.35        int p = (int)System.Environment.OSVersion.Platform;
    1.36 -      if ((p == 4) || (p == 128))
    1.37 -        return;
    1.38 -      
    1.39 -      List<Structure> structureList = new List<Structure>();
    1.40 +      if ((p == 4) || (p == 128)) {
    1.41 +        this.raw = null;
    1.42 +        this.table = null;
    1.43 +        
    1.44 +        string boardVendor = ReadSysFS("/sys/class/dmi/id/board_vendor");
    1.45 +        string boardName = ReadSysFS("/sys/class/dmi/id/board_name");        
    1.46 +        string boardVersion = ReadSysFS("/sys/class/dmi/id/board_version");        
    1.47 +        this.baseBoardInformation = new BaseBoardInformation(
    1.48 +          boardVendor, boardName, boardVersion, null);
    1.49 +        
    1.50 +        string biosVendor = ReadSysFS("/sys/class/dmi/id/bios_vendor");
    1.51 +        string biosVersion = ReadSysFS("/sys/class/dmi/id/bios_version");
    1.52 +        this.biosInformation = new BIOSInformation(biosVendor, biosVersion);
    1.53 +        
    1.54 +      } else {              
    1.55 +        List<Structure> structureList = new List<Structure>();
    1.56  
    1.57 -      byte[] raw = null;
    1.58 -      try {
    1.59 -        ManagementObjectCollection collection = new ManagementObjectSearcher(
    1.60 -          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    1.61 -       
    1.62 -        foreach (ManagementObject mo in collection) {
    1.63 -          raw = (byte[])mo["SMBiosData"];
    1.64 -          break;
    1.65 -        }
    1.66 -      } catch { }      
    1.67 -
    1.68 -      if (raw != null && raw.Length > 0) {
    1.69 -        int offset = 0;
    1.70 -        byte type = raw[offset];
    1.71 -        while (offset + 4 < raw.Length && type != 127) {
    1.72 -
    1.73 -          type = raw[offset];
    1.74 -          int length = raw[offset + 1];
    1.75 -          ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
    1.76 -
    1.77 -          if (offset + length > raw.Length)
    1.78 +        raw = null;
    1.79 +        try {
    1.80 +          ManagementObjectCollection collection = 
    1.81 +            new ManagementObjectSearcher("root\\WMI", 
    1.82 +              "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    1.83 +         
    1.84 +          foreach (ManagementObject mo in collection) {
    1.85 +            raw = (byte[])mo["SMBiosData"];
    1.86              break;
    1.87 -          byte[] data = new byte[length];
    1.88 -          Array.Copy(raw, offset, data, 0, length);
    1.89 -          offset += length;
    1.90 -
    1.91 -          List<string> stringsList = new List<string>();
    1.92 -          if (offset < raw.Length && raw[offset] == 0)
    1.93 -            offset++;
    1.94 -
    1.95 -          while (offset < raw.Length && raw[offset] != 0) {
    1.96 -            StringBuilder sb = new StringBuilder();
    1.97 +          }
    1.98 +        } catch { }
    1.99 +  
   1.100 +        if (raw != null && raw.Length > 0) {
   1.101 +          int offset = 0;
   1.102 +          byte type = raw[offset];
   1.103 +          while (offset + 4 < raw.Length && type != 127) {
   1.104 +  
   1.105 +            type = raw[offset];
   1.106 +            int length = raw[offset + 1];
   1.107 +            ushort handle = (ushort)((raw[offset + 2] << 8) | raw[offset + 3]);
   1.108 +  
   1.109 +            if (offset + length > raw.Length)
   1.110 +              break;
   1.111 +            byte[] data = new byte[length];
   1.112 +            Array.Copy(raw, offset, data, 0, length);
   1.113 +            offset += length;
   1.114 +  
   1.115 +            List<string> stringsList = new List<string>();
   1.116 +            if (offset < raw.Length && raw[offset] == 0)
   1.117 +              offset++;
   1.118 +  
   1.119              while (offset < raw.Length && raw[offset] != 0) {
   1.120 -              sb.Append((char)raw[offset]); offset++;
   1.121 +              StringBuilder sb = new StringBuilder();
   1.122 +              while (offset < raw.Length && raw[offset] != 0) {
   1.123 +                sb.Append((char)raw[offset]); offset++;
   1.124 +              }
   1.125 +              offset++;
   1.126 +              stringsList.Add(sb.ToString());
   1.127              }
   1.128              offset++;
   1.129 -            stringsList.Add(sb.ToString());
   1.130 -          }
   1.131 -          offset++;
   1.132 -          switch (type) {
   1.133 -            case 0x00:
   1.134 -              this.biosInformation = new BIOSInformation(
   1.135 -                type, handle, data, stringsList.ToArray());
   1.136 -              structureList.Add(this.biosInformation); break;
   1.137 -            case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   1.138 -                type, handle, data, stringsList.ToArray());
   1.139 -              structureList.Add(this.baseBoardInformation); break;
   1.140 -            default: structureList.Add(new Structure(
   1.141 -              type, handle, data, stringsList.ToArray())); break;
   1.142 +            switch (type) {
   1.143 +              case 0x00:
   1.144 +                this.biosInformation = new BIOSInformation(
   1.145 +                  type, handle, data, stringsList.ToArray());
   1.146 +                structureList.Add(this.biosInformation); break;
   1.147 +              case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   1.148 +                  type, handle, data, stringsList.ToArray());
   1.149 +                structureList.Add(this.baseBoardInformation); break;
   1.150 +              default: structureList.Add(new Structure(
   1.151 +                type, handle, data, stringsList.ToArray())); break;
   1.152 +            }
   1.153            }
   1.154          }
   1.155 +              
   1.156 +        table = structureList.ToArray();
   1.157        }
   1.158 -            
   1.159 -      table = structureList.ToArray();
   1.160      }
   1.161  
   1.162      public string GetReport() {
   1.163 -      StringBuilder r = new StringBuilder();      
   1.164 +      StringBuilder r = new StringBuilder();
   1.165  
   1.166        if (biosInformation != null) {
   1.167          r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   1.168 @@ -122,13 +151,31 @@
   1.169        }
   1.170  
   1.171        if (baseBoardInformation != null) {
   1.172 -        r.Append("Mainboard Manufacturer: "); 
   1.173 +        r.Append("Mainboard Manufacturer: ");
   1.174          r.AppendLine(baseBoardInformation.ManufacturerName);
   1.175 -        r.Append("Mainboard Name: "); 
   1.176 +        r.Append("Mainboard Name: ");
   1.177          r.AppendLine(baseBoardInformation.ProductName);
   1.178          r.AppendLine();
   1.179        }
   1.180 -     
   1.181 +
   1.182 +      if (raw != null) {
   1.183 +        string base64 = Convert.ToBase64String(raw);
   1.184 +        r.AppendLine("SMBIOS Table");
   1.185 +        r.AppendLine();
   1.186 +
   1.187 +        for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
   1.188 +          r.Append(" ");
   1.189 +          for (int j = 0; j < 0x40; j++) {
   1.190 +            int index = (i << 6) | j;
   1.191 +            if (index < base64.Length) {              
   1.192 +              r.Append(base64[index]);
   1.193 +            }
   1.194 +          }
   1.195 +          r.AppendLine();
   1.196 +        }
   1.197 +        r.AppendLine();
   1.198 +      }
   1.199 +
   1.200        return r.ToString();
   1.201      }
   1.202  
   1.203 @@ -167,16 +214,23 @@
   1.204  
   1.205        public ushort Handle { get { return handle; } }
   1.206      }
   1.207 -
   1.208 +      
   1.209      public class BIOSInformation : Structure {
   1.210  
   1.211        private string vendor;
   1.212        private string version;
   1.213 -
   1.214 +      
   1.215 +      public BIOSInformation(string vendor, string version) 
   1.216 +        : base (0x00, 0, null, null) 
   1.217 +      {
   1.218 +        this.vendor = vendor;
   1.219 +        this.version = version;
   1.220 +      }
   1.221 +      
   1.222        public BIOSInformation(byte type, ushort handle, byte[] data,
   1.223          string[] strings)
   1.224 -        : base(type, handle, data, strings) {
   1.225 -
   1.226 +        : base(type, handle, data, strings) 
   1.227 +      {
   1.228          this.vendor = GetString(0x04);
   1.229          this.version = GetString(0x05);
   1.230        }
   1.231 @@ -195,15 +249,9 @@
   1.232        private Manufacturer manufacturer;
   1.233        private Model model;
   1.234  
   1.235 -      public BaseBoardInformation(byte type, ushort handle, byte[] data,
   1.236 -        string[] strings)
   1.237 -        : base(type, handle, data, strings) {
   1.238 -
   1.239 -        this.manufacturerName = GetString(0x04).Trim();
   1.240 -        this.productName = GetString(0x05).Trim();
   1.241 -        this.version = GetString(0x06).Trim();
   1.242 -        this.serialNumber = GetString(0x07).Trim();
   1.243 -
   1.244 +      private void SetManufacturerName(string manufacturerName) {
   1.245 +        this.manufacturerName = manufacturerName;
   1.246 +        
   1.247          switch (manufacturerName) {
   1.248            case "ASUSTeK Computer INC.":
   1.249              manufacturer = Manufacturer.ASUS; break;
   1.250 @@ -224,7 +272,11 @@
   1.251            default:
   1.252              manufacturer = Manufacturer.Unknown; break;
   1.253          }
   1.254 -
   1.255 +      }
   1.256 +      
   1.257 +      private void SetProductName(string productName) {
   1.258 +        this.productName = productName;
   1.259 +        
   1.260          switch (productName) {
   1.261            case "Crosshair III Formula":
   1.262              model = Model.Crosshair_III_Formula; break;
   1.263 @@ -258,7 +310,27 @@
   1.264              model = Model.Unknown; break;
   1.265          }
   1.266        }
   1.267 +      
   1.268 +      public BaseBoardInformation(string manufacturerName, string productName, 
   1.269 +        string version, string serialNumber) 
   1.270 +        : base(0x02, 0, null, null) 
   1.271 +      {        
   1.272 +        SetManufacturerName(manufacturerName);
   1.273 +        SetProductName(productName);
   1.274 +        this.version = version;
   1.275 +        this.serialNumber = serialNumber;
   1.276 +      }
   1.277 +      
   1.278 +      public BaseBoardInformation(byte type, ushort handle, byte[] data,
   1.279 +        string[] strings)
   1.280 +        : base(type, handle, data, strings) {
   1.281  
   1.282 +        SetManufacturerName(GetString(0x04).Trim());
   1.283 +        SetProductName(GetString(0x05).Trim());
   1.284 +        this.version = GetString(0x06).Trim();
   1.285 +        this.serialNumber = GetString(0x07).Trim();               
   1.286 +      }
   1.287 +      
   1.288        public string ManufacturerName { get { return manufacturerName; } }
   1.289  
   1.290        public string ProductName { get { return productName; } }