Hardware/FirmwareTable.cs
author moel.mich
Mon, 02 Jan 2012 20:45:18 +0000
changeset 329 756af5ee409e
child 344 3145aadca3d2
permissions -rw-r--r--
Small corrections for AMD family 15h CPUs (based on documentation).
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2011
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.ComponentModel;
    40 using System.Runtime.InteropServices;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware {
    44 
    45   internal static class FirmwareTable {
    46 
    47     public static byte[] GetTable(Provider provider, string table) {
    48       int id = table[3] << 24 | table[2] << 16 | table[1] << 8 | table[0];
    49       return GetTable(provider, id);
    50     }
    51 
    52     public static byte[] GetTable(Provider provider, int table) {
    53       
    54       int size;
    55       try {
    56         size = NativeMethods.GetSystemFirmwareTable(provider, table, 
    57           IntPtr.Zero, 0);
    58       } catch (DllNotFoundException) { return null; } 
    59         catch (EntryPointNotFoundException) { return null; }
    60 
    61       if (size <= 0)
    62         return null;
    63 
    64       IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
    65       NativeMethods.GetSystemFirmwareTable(provider, table, nativeBuffer, size);
    66 
    67       if (Marshal.GetLastWin32Error() != 0)
    68         return null;
    69 
    70       byte[] buffer = new byte[size];
    71       Marshal.Copy(nativeBuffer, buffer, 0, size);
    72       Marshal.FreeHGlobal(nativeBuffer);
    73 
    74       return buffer;
    75     }
    76 
    77     public static string[] EnumerateTables(Provider provider) {
    78       int size;
    79       try {
    80         size = NativeMethods.EnumSystemFirmwareTables(
    81           provider, IntPtr.Zero, 0);
    82       } catch (DllNotFoundException) { return null; } 
    83         catch (EntryPointNotFoundException) { return null; }
    84 
    85       IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
    86       NativeMethods.EnumSystemFirmwareTables(
    87         provider, nativeBuffer, size);
    88       byte[] buffer = new byte[size];
    89       Marshal.Copy(nativeBuffer, buffer, 0, size);
    90       Marshal.FreeHGlobal(nativeBuffer);
    91 
    92       string[] result = new string[size / 4];
    93       for (int i = 0; i < result.Length; i++) 
    94         result[i] = Encoding.ASCII.GetString(buffer, 4 * i, 4);
    95 
    96       return result;
    97     }
    98 
    99     public enum Provider : int {
   100       ACPI = (byte)'A' << 24 | (byte)'C' << 16 | (byte)'P' << 8 | (byte)'I',
   101       FIRM = (byte)'F' << 24 | (byte)'I' << 16 | (byte)'R' << 8 | (byte)'M',
   102       RSMB = (byte)'R' << 24 | (byte)'S' << 16 | (byte)'M' << 8 | (byte)'B'
   103     }
   104 
   105     private static class NativeMethods {
   106       private const string KERNEL = "kernel32.dll";
   107 
   108       [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
   109         SetLastError = true)]
   110       public static extern int EnumSystemFirmwareTables(
   111         Provider firmwareTableProviderSignature,
   112         IntPtr firmwareTableBuffer, int bufferSize);
   113 
   114       [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
   115         SetLastError = true)]
   116       public static extern int GetSystemFirmwareTable(
   117         Provider firmwareTableProviderSignature,
   118         int firmwareTableID, IntPtr firmwareTableBuffer, int bufferSize);
   119     }
   120   }
   121 }