moel@1
|
1 |
/*
|
moel@1
|
2 |
|
moel@1
|
3 |
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
moel@1
|
4 |
|
moel@1
|
5 |
The contents of this file are subject to the Mozilla Public License Version
|
moel@1
|
6 |
1.1 (the "License"); you may not use this file except in compliance with
|
moel@1
|
7 |
the License. You may obtain a copy of the License at
|
moel@1
|
8 |
|
moel@1
|
9 |
http://www.mozilla.org/MPL/
|
moel@1
|
10 |
|
moel@1
|
11 |
Software distributed under the License is distributed on an "AS IS" basis,
|
moel@1
|
12 |
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
moel@1
|
13 |
for the specific language governing rights and limitations under the License.
|
moel@1
|
14 |
|
moel@1
|
15 |
The Original Code is the Open Hardware Monitor code.
|
moel@1
|
16 |
|
moel@1
|
17 |
The Initial Developer of the Original Code is
|
moel@1
|
18 |
Michael Möller <m.moeller@gmx.ch>.
|
moel@1
|
19 |
Portions created by the Initial Developer are Copyright (C) 2009-2010
|
moel@1
|
20 |
the Initial Developer. All Rights Reserved.
|
moel@1
|
21 |
|
paulwerelds@218
|
22 |
Contributor(s): Paul Werelds
|
moel@1
|
23 |
|
moel@1
|
24 |
Alternatively, the contents of this file may be used under the terms of
|
moel@1
|
25 |
either the GNU General Public License Version 2 or later (the "GPL"), or
|
moel@1
|
26 |
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
moel@1
|
27 |
in which case the provisions of the GPL or the LGPL are applicable instead
|
moel@1
|
28 |
of those above. If you wish to allow use of your version of this file only
|
moel@1
|
29 |
under the terms of either the GPL or the LGPL, and not to allow others to
|
moel@1
|
30 |
use your version of this file under the terms of the MPL, indicate your
|
moel@1
|
31 |
decision by deleting the provisions above and replace them with the notice
|
moel@1
|
32 |
and other provisions required by the GPL or the LGPL. If you do not delete
|
moel@1
|
33 |
the provisions above, a recipient may use your version of this file under
|
moel@1
|
34 |
the terms of any one of the MPL, the GPL or the LGPL.
|
moel@1
|
35 |
|
moel@1
|
36 |
*/
|
moel@1
|
37 |
|
moel@1
|
38 |
using System;
|
paulwerelds@218
|
39 |
using System.Collections.Generic;
|
moel@1
|
40 |
using System.Runtime.InteropServices;
|
moel@1
|
41 |
|
moel@1
|
42 |
namespace OpenHardwareMonitor.Hardware.HDD {
|
moel@165
|
43 |
|
moel@165
|
44 |
internal class SMART {
|
moel@1
|
45 |
|
moel@1
|
46 |
[Flags]
|
moel@1
|
47 |
public enum Status : ushort {
|
moel@1
|
48 |
PreFailureWarranty = 0x01,
|
moel@1
|
49 |
OnLineCollection = 0x02,
|
moel@1
|
50 |
Performance = 0x04,
|
moel@1
|
51 |
ErrorRate = 0x08,
|
moel@1
|
52 |
EventCount = 0x10,
|
moel@1
|
53 |
SelfPreserving = 0x20
|
moel@1
|
54 |
}
|
moel@1
|
55 |
|
moel@1
|
56 |
public enum AttributeID : byte {
|
moel@1
|
57 |
ReadErrorRate = 0x01,
|
moel@1
|
58 |
ThroughputPerformance = 0x02,
|
moel@1
|
59 |
SpinUpTime = 0x03,
|
moel@1
|
60 |
StartStopCount = 0x04,
|
moel@1
|
61 |
ReallocatedSectorsCount = 0x05,
|
moel@1
|
62 |
ReadChannelMargin = 0x06,
|
moel@1
|
63 |
SeekErrorRate = 0x07,
|
moel@1
|
64 |
SeekTimePerformance = 0x08,
|
moel@1
|
65 |
PowerOnHours = 0x09,
|
moel@1
|
66 |
SpinRetryCount = 0x0A,
|
moel@1
|
67 |
RecalibrationRetries = 0x0B,
|
moel@1
|
68 |
PowerCycleCount = 0x0C,
|
moel@1
|
69 |
SoftReadErrorRate = 0x0D,
|
moel@48
|
70 |
AirflowTemperature = 0xBE,
|
moel@1
|
71 |
Temperature = 0xC2,
|
moel@1
|
72 |
HardwareECCRecovered = 0xC3,
|
moel@1
|
73 |
ReallocationEventCount = 0xC4,
|
moel@1
|
74 |
CurrentPendingSectorCount = 0xC5,
|
moel@1
|
75 |
UncorrectableSectorCount = 0xC6,
|
moel@1
|
76 |
UltraDMACRCErrorCount = 0xC7,
|
moel@48
|
77 |
WriteErrorRate = 0xC8,
|
moel@48
|
78 |
DriveTemperature = 0xE7
|
moel@1
|
79 |
}
|
moel@1
|
80 |
|
paulwerelds@218
|
81 |
public enum SSDLifeID {
|
paulwerelds@218
|
82 |
None = 0x00,
|
paulwerelds@218
|
83 |
Indilinx = 0xD1,
|
paulwerelds@218
|
84 |
Intel = 0xE8,
|
paulwerelds@218
|
85 |
Samsung = 0xB4,
|
paulwerelds@218
|
86 |
SandForce = 0xE7
|
paulwerelds@218
|
87 |
}
|
paulwerelds@218
|
88 |
|
moel@1
|
89 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@1
|
90 |
public struct DriveAttribute {
|
moel@1
|
91 |
public AttributeID ID;
|
moel@1
|
92 |
public Status StatusFlags;
|
moel@1
|
93 |
public byte AttrValue;
|
moel@1
|
94 |
public byte WorstValue;
|
moel@1
|
95 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
|
moel@1
|
96 |
public byte[] RawValue;
|
moel@1
|
97 |
public byte Reserved;
|
moel@1
|
98 |
};
|
moel@1
|
99 |
|
moel@1
|
100 |
[Flags]
|
moel@195
|
101 |
protected enum AccessMode : uint {
|
moel@1
|
102 |
Read = 0x80000000,
|
moel@1
|
103 |
Write = 0x40000000,
|
moel@1
|
104 |
Execute = 0x20000000,
|
moel@1
|
105 |
All = 0x10000000
|
moel@1
|
106 |
}
|
moel@1
|
107 |
|
moel@1
|
108 |
[Flags]
|
moel@195
|
109 |
protected enum ShareMode : uint {
|
moel@1
|
110 |
None = 0,
|
moel@1
|
111 |
Read = 1,
|
moel@1
|
112 |
Write = 2,
|
moel@1
|
113 |
Delete = 4
|
moel@1
|
114 |
}
|
moel@1
|
115 |
|
moel@195
|
116 |
protected enum CreationMode : uint {
|
moel@1
|
117 |
New = 1,
|
moel@1
|
118 |
CreateAlways = 2,
|
moel@1
|
119 |
OpenExisting = 3,
|
moel@1
|
120 |
OpenAlways = 4,
|
moel@1
|
121 |
TruncateExisting = 5
|
moel@1
|
122 |
}
|
moel@1
|
123 |
|
moel@1
|
124 |
[Flags]
|
moel@195
|
125 |
protected enum FileAttribute : uint {
|
moel@1
|
126 |
Readonly = 0x00000001,
|
moel@1
|
127 |
Hidden = 0x00000002,
|
moel@1
|
128 |
System = 0x00000004,
|
moel@1
|
129 |
Directory = 0x00000010,
|
moel@1
|
130 |
Archive = 0x00000020,
|
moel@1
|
131 |
Device = 0x00000040,
|
moel@1
|
132 |
Normal = 0x00000080,
|
moel@1
|
133 |
Temporary = 0x00000100,
|
moel@1
|
134 |
SparseFile = 0x00000200,
|
moel@1
|
135 |
ReparsePoint = 0x00000400,
|
moel@1
|
136 |
Compressed = 0x00000800,
|
moel@1
|
137 |
Offline = 0x00001000,
|
moel@1
|
138 |
NotContentIndexed = 0x00002000,
|
moel@1
|
139 |
Encrypted = 0x00004000,
|
moel@1
|
140 |
}
|
moel@1
|
141 |
|
moel@195
|
142 |
protected enum DriveCommand : uint {
|
moel@1
|
143 |
GetVersion = 0x00074080,
|
moel@1
|
144 |
SendDriveCommand = 0x0007c084,
|
moel@1
|
145 |
ReceiveDriveData = 0x0007c088
|
moel@1
|
146 |
}
|
moel@1
|
147 |
|
moel@1
|
148 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
149 |
protected struct CommandBlockRegisters {
|
moel@1
|
150 |
public byte Features;
|
moel@1
|
151 |
public byte SectorCount;
|
moel@1
|
152 |
public byte LBALow;
|
moel@1
|
153 |
public byte LBAMid;
|
moel@1
|
154 |
public byte LBAHigh;
|
moel@1
|
155 |
public byte Device;
|
moel@1
|
156 |
public byte Command;
|
moel@1
|
157 |
public byte Reserved;
|
moel@1
|
158 |
}
|
moel@1
|
159 |
|
moel@1
|
160 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
161 |
protected struct DriveCommandParameter {
|
moel@195
|
162 |
public uint BufferSize;
|
moel@1
|
163 |
public CommandBlockRegisters Registers;
|
moel@1
|
164 |
public byte DriveNumber;
|
moel@188
|
165 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
|
moel@1
|
166 |
public byte[] Reserved;
|
moel@1
|
167 |
}
|
moel@1
|
168 |
|
moel@1
|
169 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
170 |
protected struct DriverStatus {
|
moel@1
|
171 |
public byte DriverError;
|
moel@1
|
172 |
public byte IDEError;
|
moel@1
|
173 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
|
moel@1
|
174 |
public byte[] Reserved;
|
moel@1
|
175 |
}
|
moel@1
|
176 |
|
moel@1
|
177 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
178 |
protected struct DriveCommandResult {
|
moel@1
|
179 |
public uint BufferSize;
|
moel@1
|
180 |
public DriverStatus DriverStatus;
|
moel@1
|
181 |
}
|
moel@1
|
182 |
|
moel@1
|
183 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
184 |
protected struct DriveSmartReadResult {
|
moel@1
|
185 |
public uint BufferSize;
|
moel@1
|
186 |
public DriverStatus DriverStatus;
|
moel@1
|
187 |
public byte Version;
|
moel@1
|
188 |
public byte Reserved;
|
moel@1
|
189 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DRIVE_ATTRIBUTES)]
|
moel@1
|
190 |
public DriveAttribute[] Attributes;
|
moel@1
|
191 |
}
|
moel@1
|
192 |
|
moel@1
|
193 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
194 |
protected struct Identify {
|
moel@1
|
195 |
public ushort GeneralConfiguration;
|
moel@1
|
196 |
public ushort NumberOfCylinders;
|
moel@1
|
197 |
public ushort Reserved;
|
moel@1
|
198 |
public ushort NumberOfHeads;
|
moel@1
|
199 |
public ushort UnformattedBytesPerTrack;
|
moel@1
|
200 |
public ushort UnformattedBytesPerSector;
|
moel@1
|
201 |
public ushort SectorsPerTrack;
|
moel@1
|
202 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
|
moel@1
|
203 |
public ushort[] VendorUnique;
|
moel@1
|
204 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
|
moel@1
|
205 |
public byte[] SerialNumber;
|
moel@1
|
206 |
public ushort BufferType;
|
moel@1
|
207 |
public ushort BufferSectorSize;
|
moel@1
|
208 |
public ushort NumberOfEccBytes;
|
moel@1
|
209 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
|
moel@1
|
210 |
public byte[] FirmwareRevision;
|
moel@1
|
211 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
|
moel@1
|
212 |
public byte[] ModelNumber;
|
moel@1
|
213 |
public ushort MoreVendorUnique;
|
moel@1
|
214 |
public ushort DoubleWordIo;
|
moel@1
|
215 |
public ushort Capabilities;
|
moel@1
|
216 |
public ushort MoreReserved;
|
moel@1
|
217 |
public ushort PioCycleTimingMode;
|
moel@1
|
218 |
public ushort DmaCycleTimingMode;
|
moel@1
|
219 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 406)]
|
moel@1
|
220 |
public byte[] More;
|
moel@1
|
221 |
}
|
moel@1
|
222 |
|
moel@1
|
223 |
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
moel@195
|
224 |
protected struct DriveIdentifyResult {
|
moel@1
|
225 |
public uint BufferSize;
|
moel@1
|
226 |
public DriverStatus DriverStatus;
|
moel@1
|
227 |
public Identify Identify;
|
moel@1
|
228 |
}
|
moel@1
|
229 |
|
moel@1
|
230 |
public static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
|
moel@1
|
231 |
|
moel@1
|
232 |
private const byte SMART_CMD = 0xB0;
|
moel@1
|
233 |
private const byte ID_CMD = 0xEC;
|
moel@1
|
234 |
|
moel@1
|
235 |
private const byte SMART_READ_DATA = 0xD0;
|
moel@1
|
236 |
private const byte SMART_ENABLE_OPERATIONS = 0xD8;
|
moel@1
|
237 |
|
moel@1
|
238 |
private const byte SMART_LBA_MID = 0x4F;
|
moel@1
|
239 |
private const byte SMART_LBA_HI = 0xC2;
|
moel@1
|
240 |
|
moel@1
|
241 |
private const int MAX_DRIVE_ATTRIBUTES = 512;
|
moel@1
|
242 |
|
moel@167
|
243 |
private SMART() { }
|
moel@1
|
244 |
|
moel@1
|
245 |
public static IntPtr OpenPhysicalDrive(int driveNumber) {
|
moel@167
|
246 |
return NativeMethods.CreateFile(@"\\.\PhysicalDrive" + driveNumber,
|
moel@1
|
247 |
AccessMode.Read | AccessMode.Write, ShareMode.Read | ShareMode.Write,
|
moel@1
|
248 |
IntPtr.Zero, CreationMode.OpenExisting, FileAttribute.Device,
|
moel@1
|
249 |
IntPtr.Zero);
|
moel@1
|
250 |
}
|
moel@1
|
251 |
|
moel@1
|
252 |
public static bool EnableSmart(IntPtr handle, int driveNumber) {
|
moel@1
|
253 |
DriveCommandParameter parameter = new DriveCommandParameter();
|
moel@1
|
254 |
DriveCommandResult result;
|
moel@1
|
255 |
uint bytesReturned;
|
moel@1
|
256 |
|
moel@1
|
257 |
parameter.DriveNumber = (byte)driveNumber;
|
moel@1
|
258 |
parameter.Registers.Features = SMART_ENABLE_OPERATIONS;
|
moel@1
|
259 |
parameter.Registers.LBAMid = SMART_LBA_MID;
|
moel@1
|
260 |
parameter.Registers.LBAHigh = SMART_LBA_HI;
|
moel@1
|
261 |
parameter.Registers.Command = SMART_CMD;
|
moel@1
|
262 |
|
moel@167
|
263 |
return NativeMethods.DeviceIoControl(handle, DriveCommand.SendDriveCommand,
|
moel@167
|
264 |
ref parameter, Marshal.SizeOf(typeof(DriveCommandParameter)), out result,
|
moel@1
|
265 |
Marshal.SizeOf(typeof(DriveCommandResult)), out bytesReturned,
|
moel@1
|
266 |
IntPtr.Zero);
|
moel@1
|
267 |
}
|
moel@1
|
268 |
|
paulwerelds@218
|
269 |
public static List<DriveAttribute> ReadSmart(IntPtr handle,
|
paulwerelds@218
|
270 |
int driveNumber)
|
paulwerelds@218
|
271 |
{
|
moel@1
|
272 |
DriveCommandParameter parameter = new DriveCommandParameter();
|
moel@1
|
273 |
DriveSmartReadResult result;
|
moel@1
|
274 |
uint bytesReturned;
|
moel@1
|
275 |
|
moel@1
|
276 |
parameter.DriveNumber = (byte)driveNumber;
|
moel@1
|
277 |
parameter.Registers.Features = SMART_READ_DATA;
|
moel@1
|
278 |
parameter.Registers.LBAMid = SMART_LBA_MID;
|
moel@1
|
279 |
parameter.Registers.LBAHigh = SMART_LBA_HI;
|
moel@1
|
280 |
parameter.Registers.Command = SMART_CMD;
|
moel@1
|
281 |
|
paulwerelds@218
|
282 |
bool isValid = NativeMethods.DeviceIoControl(handle,
|
moel@167
|
283 |
DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter),
|
moel@167
|
284 |
out result, Marshal.SizeOf(typeof(DriveSmartReadResult)),
|
moel@167
|
285 |
out bytesReturned, IntPtr.Zero);
|
moel@1
|
286 |
|
paulwerelds@218
|
287 |
return (isValid)
|
paulwerelds@218
|
288 |
? new List<DriveAttribute>(result.Attributes)
|
paulwerelds@218
|
289 |
: new List<DriveAttribute>();
|
moel@1
|
290 |
}
|
moel@1
|
291 |
|
moel@1
|
292 |
public static string ReadName(IntPtr handle, int driveNumber) {
|
moel@1
|
293 |
DriveCommandParameter parameter = new DriveCommandParameter();
|
moel@1
|
294 |
DriveIdentifyResult result;
|
moel@1
|
295 |
uint bytesReturned;
|
moel@1
|
296 |
|
moel@1
|
297 |
parameter.DriveNumber = (byte)driveNumber;
|
moel@1
|
298 |
parameter.Registers.Command = ID_CMD;
|
moel@1
|
299 |
|
moel@167
|
300 |
bool valid = NativeMethods.DeviceIoControl(handle,
|
moel@167
|
301 |
DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter),
|
moel@167
|
302 |
out result, Marshal.SizeOf(typeof(DriveIdentifyResult)),
|
moel@167
|
303 |
out bytesReturned, IntPtr.Zero);
|
moel@1
|
304 |
|
moel@1
|
305 |
if (!valid)
|
moel@1
|
306 |
return null;
|
moel@1
|
307 |
else {
|
moel@1
|
308 |
|
moel@1
|
309 |
byte[] bytes = result.Identify.ModelNumber;
|
moel@1
|
310 |
char[] chars = new char[bytes.Length];
|
moel@1
|
311 |
for (int i = 0; i < bytes.Length; i += 2) {
|
moel@1
|
312 |
chars[i] = (char)bytes[i + 1];
|
moel@1
|
313 |
chars[i + 1] = (char)bytes[i];
|
moel@1
|
314 |
}
|
moel@1
|
315 |
|
moel@1
|
316 |
return new string(chars).Trim();
|
moel@1
|
317 |
}
|
moel@1
|
318 |
}
|
moel@1
|
319 |
|
moel@167
|
320 |
public static int CloseHandle(IntPtr handle) {
|
moel@167
|
321 |
return NativeMethods.CloseHandle(handle);
|
moel@167
|
322 |
}
|
moel@167
|
323 |
|
moel@195
|
324 |
protected static class NativeMethods {
|
moel@167
|
325 |
private const string KERNEL = "kernel32.dll";
|
moel@167
|
326 |
|
moel@167
|
327 |
[DllImport(KERNEL, CallingConvention = CallingConvention.Winapi,
|
moel@167
|
328 |
CharSet = CharSet.Unicode)]
|
moel@167
|
329 |
public static extern IntPtr CreateFile(string fileName,
|
moel@167
|
330 |
AccessMode desiredAccess, ShareMode shareMode, IntPtr securityAttributes,
|
moel@167
|
331 |
CreationMode creationDisposition, FileAttribute flagsAndAttributes,
|
moel@167
|
332 |
IntPtr templateFilehandle);
|
moel@167
|
333 |
|
moel@167
|
334 |
[DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
|
moel@167
|
335 |
public static extern int CloseHandle(IntPtr handle);
|
moel@167
|
336 |
|
moel@167
|
337 |
[DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
|
moel@167
|
338 |
[return: MarshalAsAttribute(UnmanagedType.Bool)]
|
moel@167
|
339 |
public static extern bool DeviceIoControl(IntPtr handle,
|
moel@167
|
340 |
DriveCommand command, ref DriveCommandParameter parameter,
|
moel@167
|
341 |
int parameterSize, out DriveSmartReadResult result, int resultSize,
|
moel@167
|
342 |
out uint bytesReturned, IntPtr overlapped);
|
moel@167
|
343 |
|
moel@167
|
344 |
[DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
|
moel@167
|
345 |
[return: MarshalAsAttribute(UnmanagedType.Bool)]
|
moel@167
|
346 |
public static extern bool DeviceIoControl(IntPtr handle,
|
moel@167
|
347 |
DriveCommand command, ref DriveCommandParameter parameter,
|
moel@167
|
348 |
int parameterSize, out DriveCommandResult result, int resultSize,
|
moel@167
|
349 |
out uint bytesReturned, IntPtr overlapped);
|
moel@167
|
350 |
|
moel@167
|
351 |
[DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
|
moel@167
|
352 |
[return: MarshalAsAttribute(UnmanagedType.Bool)]
|
moel@167
|
353 |
public static extern bool DeviceIoControl(IntPtr handle,
|
moel@167
|
354 |
DriveCommand command, ref DriveCommandParameter parameter,
|
moel@167
|
355 |
int parameterSize, out DriveIdentifyResult result, int resultSize,
|
moel@167
|
356 |
out uint bytesReturned, IntPtr overlapped);
|
moel@167
|
357 |
}
|
moel@1
|
358 |
}
|
moel@1
|
359 |
}
|