moel@308: /* moel@308: moel@308: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@308: moel@308: The contents of this file are subject to the Mozilla Public License Version moel@308: 1.1 (the "License"); you may not use this file except in compliance with moel@308: the License. You may obtain a copy of the License at moel@308: moel@308: http://www.mozilla.org/MPL/ moel@308: moel@308: Software distributed under the License is distributed on an "AS IS" basis, moel@308: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@308: for the specific language governing rights and limitations under the License. moel@308: moel@308: The Original Code is the Open Hardware Monitor code. moel@308: moel@308: The Initial Developer of the Original Code is moel@308: Michael Möller . moel@308: Portions created by the Initial Developer are Copyright (C) 2011 moel@308: the Initial Developer. All Rights Reserved. moel@308: moel@308: Contributor(s): moel@308: moel@308: Alternatively, the contents of this file may be used under the terms of moel@308: either the GNU General Public License Version 2 or later (the "GPL"), or moel@308: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@308: in which case the provisions of the GPL or the LGPL are applicable instead moel@308: of those above. If you wish to allow use of your version of this file only moel@308: under the terms of either the GPL or the LGPL, and not to allow others to moel@308: use your version of this file under the terms of the MPL, indicate your moel@308: decision by deleting the provisions above and replace them with the notice moel@308: and other provisions required by the GPL or the LGPL. If you do not delete moel@308: the provisions above, a recipient may use your version of this file under moel@308: the terms of any one of the MPL, the GPL or the LGPL. moel@308: moel@308: */ moel@308: moel@308: using System; moel@308: using System.ComponentModel; moel@308: using System.Runtime.InteropServices; moel@308: using System.Text; moel@308: moel@308: namespace OpenHardwareMonitor.Hardware { moel@308: moel@308: internal static class FirmwareTable { moel@308: moel@308: public static byte[] GetTable(Provider provider, string table) { moel@308: int id = table[3] << 24 | table[2] << 16 | table[1] << 8 | table[0]; moel@308: return GetTable(provider, id); moel@308: } moel@308: moel@308: public static byte[] GetTable(Provider provider, int table) { moel@308: moel@308: int size; moel@308: try { moel@308: size = NativeMethods.GetSystemFirmwareTable(provider, table, moel@308: IntPtr.Zero, 0); moel@308: } catch (DllNotFoundException) { return null; } moel@308: catch (EntryPointNotFoundException) { return null; } moel@308: moel@308: if (size <= 0) moel@308: return null; moel@308: moel@308: IntPtr nativeBuffer = Marshal.AllocHGlobal(size); moel@308: NativeMethods.GetSystemFirmwareTable(provider, table, nativeBuffer, size); moel@308: moel@308: if (Marshal.GetLastWin32Error() != 0) moel@308: return null; moel@308: moel@308: byte[] buffer = new byte[size]; moel@308: Marshal.Copy(nativeBuffer, buffer, 0, size); moel@308: Marshal.FreeHGlobal(nativeBuffer); moel@308: moel@308: return buffer; moel@308: } moel@308: moel@308: public static string[] EnumerateTables(Provider provider) { moel@308: int size; moel@308: try { moel@308: size = NativeMethods.EnumSystemFirmwareTables( moel@308: provider, IntPtr.Zero, 0); moel@308: } catch (DllNotFoundException) { return null; } moel@308: catch (EntryPointNotFoundException) { return null; } moel@308: moel@308: IntPtr nativeBuffer = Marshal.AllocHGlobal(size); moel@308: NativeMethods.EnumSystemFirmwareTables( moel@308: provider, nativeBuffer, size); moel@308: byte[] buffer = new byte[size]; moel@308: Marshal.Copy(nativeBuffer, buffer, 0, size); moel@308: Marshal.FreeHGlobal(nativeBuffer); moel@308: moel@308: string[] result = new string[size / 4]; moel@308: for (int i = 0; i < result.Length; i++) moel@308: result[i] = Encoding.ASCII.GetString(buffer, 4 * i, 4); moel@308: moel@308: return result; moel@308: } moel@308: moel@308: public enum Provider : int { moel@308: ACPI = (byte)'A' << 24 | (byte)'C' << 16 | (byte)'P' << 8 | (byte)'I', moel@308: FIRM = (byte)'F' << 24 | (byte)'I' << 16 | (byte)'R' << 8 | (byte)'M', moel@308: RSMB = (byte)'R' << 24 | (byte)'S' << 16 | (byte)'M' << 8 | (byte)'B' moel@308: } moel@308: moel@308: private static class NativeMethods { moel@308: private const string KERNEL = "kernel32.dll"; moel@308: moel@308: [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi, moel@308: SetLastError = true)] moel@308: public static extern int EnumSystemFirmwareTables( moel@308: Provider firmwareTableProviderSignature, moel@308: IntPtr firmwareTableBuffer, int bufferSize); moel@308: moel@308: [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi, moel@308: SetLastError = true)] moel@308: public static extern int GetSystemFirmwareTable( moel@308: Provider firmwareTableProviderSignature, moel@308: int firmwareTableID, IntPtr firmwareTableBuffer, int bufferSize); moel@308: } moel@308: } moel@308: }