Added more report output to the kernel driver loading code. Hopefully this helps to find the problems in Issue 253.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2010-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
13 using System.Reflection;
14 using System.Runtime.InteropServices;
15 using System.Security.AccessControl;
16 using System.Threading;
19 namespace OpenHardwareMonitor.Hardware {
20 internal static class Ring0 {
22 private static KernelDriver driver;
23 private static string fileName;
24 private static Mutex isaBusMutex;
25 private static readonly StringBuilder report = new StringBuilder();
27 private const uint OLS_TYPE = 40000;
28 private static IOControlCode
29 IOCTL_OLS_GET_REFCOUNT = new IOControlCode(OLS_TYPE, 0x801,
30 IOControlCode.Access.Any),
31 IOCTL_OLS_GET_DRIVER_VERSION = new IOControlCode(OLS_TYPE, 0x800,
32 IOControlCode.Access.Any),
33 IOCTL_OLS_READ_MSR = new IOControlCode(OLS_TYPE, 0x821,
34 IOControlCode.Access.Any),
35 IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822,
36 IOControlCode.Access.Any),
37 IOCTL_OLS_READ_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x833,
38 IOControlCode.Access.Read),
39 IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836,
40 IOControlCode.Access.Write),
41 IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851,
42 IOControlCode.Access.Read),
43 IOCTL_OLS_WRITE_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x852,
44 IOControlCode.Access.Write),
45 IOCTL_OLS_READ_MEMORY = new IOControlCode(OLS_TYPE, 0x841,
46 IOControlCode.Access.Read);
48 private static string GetTempFileName() {
50 // try to get a file in the temporary folder
52 return Path.GetTempFileName();
53 } catch (IOException) {
56 catch (UnauthorizedAccessException) {
57 // we do not have the right to create a file in the temp folder
59 catch (NotSupportedException) {
60 // invalid path format of the TMP system environment variable
63 // if this failed, we try to create one in the application folder
64 string fileName = Path.ChangeExtension(
65 Assembly.GetExecutingAssembly().Location, ".sys");
67 using (FileStream stream = File.Create(fileName)) {
70 } catch (IOException) { }
71 catch (UnauthorizedAccessException) { }
76 private static bool ExtractDriver(string fileName) {
77 string resourceName = "OpenHardwareMonitor.Hardware." +
78 (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" :
82 Assembly.GetExecutingAssembly().GetManifestResourceNames();
84 for (int i = 0; i < names.Length; i++) {
85 if (names[i].Replace('\\', '.') == resourceName) {
86 using (Stream stream = Assembly.GetExecutingAssembly().
87 GetManifestResourceStream(names[i]))
89 buffer = new byte[stream.Length];
90 stream.Read(buffer, 0, buffer.Length);
99 using (FileStream target = new FileStream(fileName, FileMode.Create)) {
100 target.Write(buffer, 0, buffer.Length);
102 } catch (IOException) {
103 // for example there is not enough space on the disk
110 public static void Open() {
111 // no implementation for unix systems
112 int p = (int)Environment.OSVersion.Platform;
113 if ((p == 4) || (p == 128))
119 // clear the current report
122 driver = new KernelDriver("WinRing0_1_2_0");
125 if (!driver.IsOpen) {
126 // driver is not loaded, try to install and open
128 fileName = GetTempFileName();
129 if (fileName != null && ExtractDriver(fileName)) {
131 if (driver.Install(fileName, out installError)) {
134 if (!driver.IsOpen) {
136 report.AppendLine("Status: Opening driver failed after install");
139 string errorFirstInstall = installError;
141 // install failed, try to delete and reinstall
144 string errorSecondInstall;
145 if (driver.Install(fileName, out errorSecondInstall)) {
148 if (!driver.IsOpen) {
151 "Status: Opening driver failed after reinstall");
154 report.AppendLine("Status: Installing driver \"" +
155 fileName + "\" failed" +
156 (File.Exists(fileName) ? " and file exists" : ""));
157 report.AppendLine("First Exception: " + errorFirstInstall);
158 report.AppendLine("Second Exception: " + errorSecondInstall);
162 report.AppendLine("Status: Extracting driver failed");
166 // try to delte the driver file
167 if (File.Exists(fileName))
168 File.Delete(fileName);
170 } catch (IOException) { }
171 catch (UnauthorizedAccessException) { }
177 string mutexName = "Global\\Access_ISABUS.HTP.Method";
179 isaBusMutex = new Mutex(false, mutexName);
180 } catch (UnauthorizedAccessException) {
182 isaBusMutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize);
187 public static bool IsOpen {
188 get { return driver != null; }
191 public static void Close() {
196 driver.DeviceIOControl(IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
205 if (isaBusMutex != null) {
210 // try to delete temporary driver file again if failed during open
211 if (fileName != null && File.Exists(fileName)) {
213 File.Delete(fileName);
215 } catch (IOException) { }
216 catch (UnauthorizedAccessException) { }
220 public static string GetReport() {
221 if (report.Length > 0) {
222 StringBuilder r = new StringBuilder();
223 r.AppendLine("Ring0");
232 public static bool WaitIsaBusMutex(int millisecondsTimeout) {
233 if (isaBusMutex == null)
236 return isaBusMutex.WaitOne(millisecondsTimeout, false);
237 } catch (AbandonedMutexException) { return false; }
238 catch (InvalidOperationException) { return false; }
241 public static void ReleaseIsaBusMutex() {
242 if (isaBusMutex == null)
244 isaBusMutex.ReleaseMutex();
247 public static bool Rdmsr(uint index, out uint eax, out uint edx) {
248 if (driver == null) {
255 bool result = driver.DeviceIOControl(IOCTL_OLS_READ_MSR, index,
258 edx = (uint)((buffer >> 32) & 0xFFFFFFFF);
259 eax = (uint)(buffer & 0xFFFFFFFF);
263 public static bool RdmsrTx(uint index, out uint eax, out uint edx,
264 ulong threadAffinityMask)
266 ulong mask = ThreadAffinity.Set(threadAffinityMask);
268 bool result = Rdmsr(index, out eax, out edx);
270 ThreadAffinity.Set(mask);
274 [StructLayout(LayoutKind.Sequential, Pack = 1)]
275 private struct WrmsrInput {
276 public uint Register;
280 public static bool Wrmsr(uint index, uint eax, uint edx) {
284 WrmsrInput input = new WrmsrInput();
285 input.Register = index;
286 input.Value = ((ulong)edx << 32) | eax;
288 return driver.DeviceIOControl(IOCTL_OLS_WRITE_MSR, input);
291 public static byte ReadIoPort(uint port) {
296 driver.DeviceIOControl(IOCTL_OLS_READ_IO_PORT_BYTE, port, ref value);
298 return (byte)(value & 0xFF);
301 [StructLayout(LayoutKind.Sequential, Pack = 1)]
302 private struct WriteIoPortInput {
303 public uint PortNumber;
307 public static void WriteIoPort(uint port, byte value) {
311 WriteIoPortInput input = new WriteIoPortInput();
312 input.PortNumber = port;
315 driver.DeviceIOControl(IOCTL_OLS_WRITE_IO_PORT_BYTE, input);
318 public const uint InvalidPciAddress = 0xFFFFFFFF;
320 public static uint GetPciAddress(byte bus, byte device, byte function) {
322 (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
325 [StructLayout(LayoutKind.Sequential, Pack = 1)]
326 private struct ReadPciConfigInput {
327 public uint PciAddress;
328 public uint RegAddress;
331 public static bool ReadPciConfig(uint pciAddress, uint regAddress,
334 if (driver == null || (regAddress & 3) != 0) {
339 ReadPciConfigInput input = new ReadPciConfigInput();
340 input.PciAddress = pciAddress;
341 input.RegAddress = regAddress;
344 return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input,
348 [StructLayout(LayoutKind.Sequential, Pack = 1)]
349 private struct WritePciConfigInput {
350 public uint PciAddress;
351 public uint RegAddress;
355 public static bool WritePciConfig(uint pciAddress, uint regAddress,
358 if (driver == null || (regAddress & 3) != 0)
361 WritePciConfigInput input = new WritePciConfigInput();
362 input.PciAddress = pciAddress;
363 input.RegAddress = regAddress;
366 return driver.DeviceIOControl(IOCTL_OLS_WRITE_PCI_CONFIG, input);
369 [StructLayout(LayoutKind.Sequential, Pack = 1)]
370 private struct ReadMemoryInput {
371 public ulong address;
372 public uint unitSize;
376 public static bool ReadMemory<T>(ulong address, ref T buffer) {
377 if (driver == null) {
381 ReadMemoryInput input = new ReadMemoryInput();
382 input.address = address;
384 input.count = (uint)Marshal.SizeOf(buffer);
386 return driver.DeviceIOControl(IOCTL_OLS_READ_MEMORY, input,