Hardware/SMBIOS/SMBIOSGroup.cs
author moel.mich
Tue, 26 Jan 2010 22:37:48 +0000
changeset 1 361e324a0ed4
child 8 39067efce486
permissions -rw-r--r--
Initial commit.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.Management;
moel@1
    41
using System.Text;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.SMBIOS {
moel@1
    44
moel@1
    45
  public class SMBIOSGroup : IGroup {
moel@1
    46
moel@1
    47
    private Structure[] table;
moel@1
    48
moel@1
    49
    private BIOSInformation biosInformation = null;
moel@1
    50
moel@1
    51
    private BaseBoardInformation baseBoardInformation = null;
moel@1
    52
moel@1
    53
    public SMBIOSGroup() {
moel@1
    54
      int p = (int)System.Environment.OSVersion.Platform;
moel@1
    55
      if ((p == 4) || (p == 128))
moel@1
    56
        return;
moel@1
    57
          
moel@1
    58
      ManagementObjectCollection collection = new ManagementObjectSearcher(
moel@1
    59
         "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@1
    60
moel@1
    61
      byte[] raw = null;
moel@1
    62
      foreach (ManagementObject mo in collection) {
moel@1
    63
        raw = (byte[])mo["SMBiosData"];
moel@1
    64
        break;
moel@1
    65
      }
moel@1
    66
moel@1
    67
      List<Structure> structureList = new List<Structure>();
moel@1
    68
      if (raw != null && raw.Length > 0) {
moel@1
    69
        int offset = 0;
moel@1
    70
        byte type = raw[offset];
moel@1
    71
        while (offset < raw.Length && type != 127) {
moel@1
    72
          
moel@1
    73
          type = raw[offset]; offset++;
moel@1
    74
          int length = raw[offset]; offset++;
moel@1
    75
          ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]); 
moel@1
    76
          offset += 2;
moel@1
    77
          
moel@1
    78
          byte[] data = new byte[length];
moel@1
    79
          Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
moel@1
    80
moel@1
    81
          List<string> stringsList = new List<string>();
moel@1
    82
          if (raw[offset] == 0)
moel@1
    83
            offset++;
moel@1
    84
          while (raw[offset] != 0) {   
moel@1
    85
            StringBuilder sb = new StringBuilder();
moel@1
    86
            while (raw[offset] != 0) {
moel@1
    87
              sb.Append((char)raw[offset]); offset++;
moel@1
    88
            }
moel@1
    89
            offset++;
moel@1
    90
            stringsList.Add(sb.ToString());
moel@1
    91
          }
moel@1
    92
          offset++;
moel@1
    93
          switch (type) {
moel@1
    94
            case 0x00:
moel@1
    95
              this.biosInformation = new BIOSInformation(
moel@1
    96
                type, handle, data, stringsList.ToArray());
moel@1
    97
              structureList.Add(this.biosInformation); break;
moel@1
    98
            case 0x02: this.baseBoardInformation = new BaseBoardInformation(
moel@1
    99
                type, handle, data, stringsList.ToArray());
moel@1
   100
              structureList.Add(this.baseBoardInformation); break;
moel@1
   101
            default: structureList.Add(new Structure(
moel@1
   102
              type, handle, data, stringsList.ToArray())); break;
moel@1
   103
          }
moel@1
   104
        }
moel@1
   105
      }
moel@1
   106
      table = structureList.ToArray();
moel@1
   107
    }
moel@1
   108
moel@1
   109
    public IHardware[] Hardware { get { return new IHardware[0]; } }
moel@1
   110
moel@1
   111
    public string GetReport() {
moel@1
   112
      StringBuilder r = new StringBuilder();
moel@1
   113
moel@1
   114
      r.AppendLine("SMBIOS");
moel@1
   115
      r.AppendLine();
moel@1
   116
moel@1
   117
      if (biosInformation != null) {
moel@1
   118
        r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
moel@1
   119
        r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
moel@1
   120
        r.AppendLine();
moel@1
   121
      }
moel@1
   122
moel@1
   123
      if (baseBoardInformation != null) {
moel@1
   124
        r.Append("Mainboard Manufacturer: "); 
moel@1
   125
        r.AppendLine(baseBoardInformation.Manufacturer);
moel@1
   126
        r.Append("Mainboard Name: "); 
moel@1
   127
        r.AppendLine(baseBoardInformation.ProductName);
moel@1
   128
        r.AppendLine();
moel@1
   129
      }
moel@1
   130
     
moel@1
   131
      return r.ToString();
moel@1
   132
    }
moel@1
   133
moel@1
   134
    public void Close() { }
moel@1
   135
moel@1
   136
    public class Structure {
moel@1
   137
      private byte type;
moel@1
   138
      private ushort handle;
moel@1
   139
moel@1
   140
      private byte[] data;
moel@1
   141
      private string[] strings;
moel@1
   142
moel@1
   143
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@1
   144
      {
moel@1
   145
        this.type = type;
moel@1
   146
        this.handle = handle;
moel@1
   147
        this.data = data;
moel@1
   148
        this.strings = strings;
moel@1
   149
      }
moel@1
   150
moel@1
   151
      public byte Type { get { return type; } }
moel@1
   152
moel@1
   153
      public ushort Handle { get { return handle; } }
moel@1
   154
    }
moel@1
   155
moel@1
   156
    public class BIOSInformation : Structure {
moel@1
   157
moel@1
   158
      private string vendor = "";
moel@1
   159
      private string version = "";
moel@1
   160
moel@1
   161
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@1
   162
        string[] strings)
moel@1
   163
        : base(type, handle, data, strings) {
moel@1
   164
moel@1
   165
        if (data[0x04] > 0 && data[0x04] <= strings.Length)
moel@1
   166
          vendor = strings[data[0x04] - 1];
moel@1
   167
moel@1
   168
        if (data[0x05] > 0 && data[0x05] <= strings.Length)
moel@1
   169
          version = strings[data[0x05] - 1];
moel@1
   170
      }
moel@1
   171
moel@1
   172
      public string Vendor { get { return vendor; } }
moel@1
   173
moel@1
   174
      public string Version { get { return version; } }
moel@1
   175
    }
moel@1
   176
moel@1
   177
    public class BaseBoardInformation : Structure {
moel@1
   178
moel@1
   179
      private string manufacturer  = "";
moel@1
   180
      private string productName = "";
moel@1
   181
moel@1
   182
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@1
   183
        string[] strings)
moel@1
   184
        : base(type, handle, data, strings) {
moel@1
   185
moel@1
   186
        if (data[0x04] > 0 && data[0x04] <= strings.Length)
moel@1
   187
          manufacturer = strings[data[0x04] - 1];
moel@1
   188
moel@1
   189
        if (data[0x05] > 0 && data[0x05] <= strings.Length)
moel@1
   190
          productName = strings[data[0x05] - 1];
moel@1
   191
      }
moel@1
   192
moel@1
   193
      public string Manufacturer { get { return manufacturer; } }
moel@1
   194
moel@1
   195
      public string ProductName { get { return productName; } }
moel@1
   196
    }
moel@1
   197
  }
moel@1
   198
}