1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2024 NervoSys
//! SATA SMART attributes on Windows, without elevation.
//!
//! `IOCTL_STORAGE_PREDICT_FAILURE` returns a `STORAGE_PREDICT_FAILURE`: the
//! drive's own failure prediction, plus 512 vendor-specific bytes which, for an
//! ATA device, are verbatim the SMART READ DATA structure that
//! [`super::ata_smart`] parses. The storage driver issues the ATA command on the
//! caller's behalf, so the caller never needs to.
//!
//! # Why not `IOCTL_ATA_PASS_THROUGH`
//!
//! Because it cannot be done unelevated, which is the whole point of the exercise.
//! `IOCTL_ATA_PASS_THROUGH` is declared `CTL_CODE(IOCTL_SCSI_BASE, 0x040b,
//! METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)`, so the I/O manager
//! checks the handle's access mask before the driver is ever reached — and a
//! handle on `\\.\PhysicalDriveN` with read and write access requires
//! Administrator. Issued on the zero-access handle that makes the NVMe path work,
//! it returns `ERROR_ACCESS_DENIED` (0x80070005). Measured on all four physical
//! drives of the development machine, unelevated.
//!
//! `IOCTL_STORAGE_PREDICT_FAILURE` is `CTL_CODE(IOCTL_STORAGE_BASE, 0x0440,
//! METHOD_BUFFERED, FILE_ANY_ACCESS)`. On the same zero-access handle it reaches
//! the driver, which is what makes this route viable at all.
//!
//! # What this cannot get
//!
//! Failure thresholds. Those come from SMART READ THRESHOLDS, a separate ATA
//! command with no `IOCTL_STORAGE_*` equivalent, so every attribute here is
//! reported with a threshold of zero — which is also ATA's own encoding for "no
//! threshold", the value that never trips.
//!
//! # Verification status
//!
//! **The ioctl path has not been run against a SATA drive.** The development
//! machine has three NVMe drives and no ATA device; on all of them this returns
//! `Error::NotSupported`, derived from the `ERROR_INVALID_FUNCTION` the NVMe
//! driver answers with — which does confirm the control code passes the access
//! check on a zero-access handle, and that the not-an-ATA-device case is handled,
//! but says nothing about the parse. The 512-byte structure is covered by
//! [`super::ata_smart`]'s tests against synthetic buffers. Anyone with a SATA SSD
//! or HDD should compare `ironmon disk smart` against `smartctl -A` once; that is
//! the check this has not had.
use ;
use Error;
use Device;
use ;
use ;
use IODeviceIoControl;
/// What the drive answered.
pub
/// Read the drive's failure prediction and, where available, its SMART attributes.
///
/// Returns [`Error::NotSupported`] when the device does not implement the control
/// code, which is how NVMe drives and most USB bridges answer.
pub