Hardware/FirmwareTable.cs
changeset 308 d882720734bf
child 344 3145aadca3d2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/FirmwareTable.cs	Thu Jul 07 20:41:09 2011 +0000
     1.3 @@ -0,0 +1,121 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2011
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.ComponentModel;
    1.43 +using System.Runtime.InteropServices;
    1.44 +using System.Text;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware {
    1.47 +
    1.48 +  internal static class FirmwareTable {
    1.49 +
    1.50 +    public static byte[] GetTable(Provider provider, string table) {
    1.51 +      int id = table[3] << 24 | table[2] << 16 | table[1] << 8 | table[0];
    1.52 +      return GetTable(provider, id);
    1.53 +    }
    1.54 +
    1.55 +    public static byte[] GetTable(Provider provider, int table) {
    1.56 +      
    1.57 +      int size;
    1.58 +      try {
    1.59 +        size = NativeMethods.GetSystemFirmwareTable(provider, table, 
    1.60 +          IntPtr.Zero, 0);
    1.61 +      } catch (DllNotFoundException) { return null; } 
    1.62 +        catch (EntryPointNotFoundException) { return null; }
    1.63 +
    1.64 +      if (size <= 0)
    1.65 +        return null;
    1.66 +
    1.67 +      IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
    1.68 +      NativeMethods.GetSystemFirmwareTable(provider, table, nativeBuffer, size);
    1.69 +
    1.70 +      if (Marshal.GetLastWin32Error() != 0)
    1.71 +        return null;
    1.72 +
    1.73 +      byte[] buffer = new byte[size];
    1.74 +      Marshal.Copy(nativeBuffer, buffer, 0, size);
    1.75 +      Marshal.FreeHGlobal(nativeBuffer);
    1.76 +
    1.77 +      return buffer;
    1.78 +    }
    1.79 +
    1.80 +    public static string[] EnumerateTables(Provider provider) {
    1.81 +      int size;
    1.82 +      try {
    1.83 +        size = NativeMethods.EnumSystemFirmwareTables(
    1.84 +          provider, IntPtr.Zero, 0);
    1.85 +      } catch (DllNotFoundException) { return null; } 
    1.86 +        catch (EntryPointNotFoundException) { return null; }
    1.87 +
    1.88 +      IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
    1.89 +      NativeMethods.EnumSystemFirmwareTables(
    1.90 +        provider, nativeBuffer, size);
    1.91 +      byte[] buffer = new byte[size];
    1.92 +      Marshal.Copy(nativeBuffer, buffer, 0, size);
    1.93 +      Marshal.FreeHGlobal(nativeBuffer);
    1.94 +
    1.95 +      string[] result = new string[size / 4];
    1.96 +      for (int i = 0; i < result.Length; i++) 
    1.97 +        result[i] = Encoding.ASCII.GetString(buffer, 4 * i, 4);
    1.98 +
    1.99 +      return result;
   1.100 +    }
   1.101 +
   1.102 +    public enum Provider : int {
   1.103 +      ACPI = (byte)'A' << 24 | (byte)'C' << 16 | (byte)'P' << 8 | (byte)'I',
   1.104 +      FIRM = (byte)'F' << 24 | (byte)'I' << 16 | (byte)'R' << 8 | (byte)'M',
   1.105 +      RSMB = (byte)'R' << 24 | (byte)'S' << 16 | (byte)'M' << 8 | (byte)'B'
   1.106 +    }
   1.107 +
   1.108 +    private static class NativeMethods {
   1.109 +      private const string KERNEL = "kernel32.dll";
   1.110 +
   1.111 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
   1.112 +        SetLastError = true)]
   1.113 +      public static extern int EnumSystemFirmwareTables(
   1.114 +        Provider firmwareTableProviderSignature,
   1.115 +        IntPtr firmwareTableBuffer, int bufferSize);
   1.116 +
   1.117 +      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
   1.118 +        SetLastError = true)]
   1.119 +      public static extern int GetSystemFirmwareTable(
   1.120 +        Provider firmwareTableProviderSignature,
   1.121 +        int firmwareTableID, IntPtr firmwareTableBuffer, int bufferSize);
   1.122 +    }
   1.123 +  }
   1.124 +}