moel@308: /* moel@308: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@308: moel@344: Copyright (C) 2011 Michael Möller moel@344: 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: }