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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Drive identification — match drives to profiles by SCSI response fields.
//!
//! Field names follow SPC-4 (INQUIRY) and MMC-6 (GET CONFIGURATION) standards.
//! No proprietary fingerprints or encrypted lookups — open matching only.
//!
//! References:
//! SPC-4 §6.4.2 — Standard INQUIRY data
//! MMC-6 §5.3.10 — Feature 010Ch (Firmware Information)
use crate::error::Result;
use crate::scsi::{DataDirection, ScsiTransport};
/// Drive identity from standard SCSI commands.
///
/// All field names follow the SCSI standards:
/// - SPC-4 §6.4.2 for INQUIRY fields
/// - MMC-6 §5.3.10 for Firmware Information
#[derive(Debug, Clone)]
pub struct DriveId {
/// T10 VENDOR IDENTIFICATION — INQUIRY bytes [8:16]
/// SPC-4 §6.4.2
pub vendor_id: String,
/// PRODUCT IDENTIFICATION — INQUIRY bytes [16:32]
/// SPC-4 §6.4.2
pub product_id: String,
/// PRODUCT REVISION LEVEL — INQUIRY bytes [32:36]
/// SPC-4 §6.4.2
pub product_revision: String,
/// VENDOR SPECIFIC — INQUIRY bytes [36:43]
/// SPC-4 §6.4.2
/// Content varies by vendor: firmware type code (MTK), date (Pioneer), etc.
pub vendor_specific: String,
/// Firmware Creation Date — GET CONFIGURATION Feature 010Ch
/// MMC-6 §5.3.10
/// Format: CCYYMMDDHHMI (12 ASCII characters)
pub firmware_date: String,
/// Drive serial number — GET CONFIGURATION Feature 0108h
pub serial_number: String,
/// Raw 96-byte INQUIRY response for additional parsing if needed.
pub raw_inquiry: Vec<u8>,
/// Raw GET CONFIGURATION Feature 010Ch response bytes.
pub raw_gc_010c: Vec<u8>,
}
impl DriveId {
/// Probe a real drive via SCSI and build its identity.
pub fn from_drive(transport: &mut dyn ScsiTransport) -> Result<Self> {
// INQUIRY — SPC-4 §6.4
let mut inquiry = vec![0u8; 96];
let cdb_inq = [0x12, 0x00, 0x00, 0x00, 0x60, 0x00];
transport.execute(&cdb_inq, DataDirection::FromDevice, &mut inquiry, 5000)?;
// GET CONFIGURATION Feature 010Ch — MMC-6 §6.6.
// Best-effort: 010Ch (Firmware Information) is an optional feature.
// A drive that lacks it may CHECK CONDITION rather than return an
// empty descriptor, so a failure here is treated as feature-absent
// (empty firmware date + empty raw bytes) instead of aborting the
// whole identity probe.
let mut gc = vec![0u8; 256];
let cdb_gc = [0x46, 0x02, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
// `bytes_transferred` is device-reported and untrusted; clamp every
// slice end to the actual buffer length before indexing.
let (firmware_date, raw_gc_010c) =
match transport.execute(&cdb_gc, DataDirection::FromDevice, &mut gc, 5000) {
Ok(result) => {
let end = result.bytes_transferred.min(gc.len());
let date = if end > 12 {
String::from_utf8_lossy(&gc[12..24.min(end)])
.trim()
.to_string()
} else {
String::new()
};
(date, gc[..end].to_vec())
}
Err(_) => (String::new(), Vec::new()),
};
// GET CONFIGURATION Feature 0108h — Serial Number.
// Best-effort, like 010Ch above: the serial-number feature is
// optional, so a drive that lacks it (CHECK CONDITION) or reports
// too few bytes deliberately yields an empty serial rather than
// failing the identity probe.
let mut gc_serial = vec![0u8; 256];
let cdb_serial = [0x46, 0x02, 0x01, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
let serial_number = if let Ok(r) =
transport.execute(&cdb_serial, DataDirection::FromDevice, &mut gc_serial, 5000)
{
if r.bytes_transferred > 12 {
// `bytes_transferred` is device-reported and untrusted; clamp
// the slice end to the buffer length to avoid an out-of-range
// panic on an oversized reported count.
let end = r.bytes_transferred.min(gc_serial.len());
String::from_utf8_lossy(&gc_serial[12..end])
.trim()
.to_string()
} else {
String::new()
}
} else {
String::new()
};
Ok(DriveId {
vendor_id: ascii_field(&inquiry, 8, 16),
product_id: ascii_field(&inquiry, 16, 32),
product_revision: ascii_field(&inquiry, 32, 36),
vendor_specific: ascii_field(&inquiry, 36, 43),
firmware_date,
serial_number,
raw_inquiry: inquiry,
raw_gc_010c,
})
}
/// Build identity from raw INQUIRY bytes and firmware date string.
/// Used by tests and when serial isn't available.
pub fn from_inquiry(inquiry: &[u8], firmware_date: &str) -> Self {
DriveId {
vendor_id: ascii_field(inquiry, 8, 16),
product_id: ascii_field(inquiry, 16, 32),
product_revision: ascii_field(inquiry, 32, 36),
vendor_specific: ascii_field(inquiry, 36, 43),
firmware_date: firmware_date.to_string(),
serial_number: String::new(),
raw_inquiry: inquiry.to_vec(),
raw_gc_010c: Vec::new(),
}
}
/// Profile match key: "VENDOR|PRODUCT|REVISION|VENDOR_SPECIFIC"
///
/// Used to look up this drive in the profile database.
/// All fields trimmed for consistent matching.
pub fn match_key(&self) -> String {
format!(
"{}|{}|{}|{}",
self.vendor_id.trim(),
self.product_id.trim(),
self.product_revision.trim(),
self.vendor_specific.trim()
)
}
}
impl std::fmt::Display for DriveId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {} {}",
self.vendor_id.trim(),
self.product_id.trim(),
self.product_revision.trim(),
self.vendor_specific.trim()
)
}
}
/// Extract an ASCII string field from raw SCSI data.
fn ascii_field(data: &[u8], start: usize, end: usize) -> String {
if data.len() > start {
let e = end.min(data.len());
String::from_utf8_lossy(&data[start..e]).to_string()
} else {
String::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::scsi::{ScsiResult, ScsiTransport};
/// Transport that returns the requested data length but reports a
/// bytes_transferred larger than the caller's buffer — models a drive
/// that lies about its transfer count. The old slicing code panicked
/// on this; the clamps must keep it from indexing out of range.
struct OversizedCountTransport;
impl ScsiTransport for OversizedCountTransport {
fn execute(
&mut self,
cdb: &[u8],
_dir: DataDirection,
buf: &mut [u8],
_timeout_ms: u32,
) -> Result<ScsiResult> {
// Fill plausible ASCII so the from_utf8_lossy paths run.
for b in buf.iter_mut() {
*b = b'A';
}
// INQUIRY (0x12): honest count. GET CONFIGURATION (0x46): lie.
let bytes_transferred = if cdb.first() == Some(&0x12) {
buf.len()
} else {
buf.len() + 4096
};
Ok(ScsiResult {
status: 0,
bytes_transferred,
sense: [0u8; 32],
})
}
}
#[test]
fn from_drive_clamps_oversized_bytes_transferred() {
// Must not panic despite the transport reporting a transfer count
// far beyond the 256-byte GET CONFIGURATION buffers.
let mut t = OversizedCountTransport;
let id = DriveId::from_drive(&mut t).expect("from_drive must not error");
// raw_gc_010c is clamped to the 256-byte buffer, never the lie.
assert_eq!(id.raw_gc_010c.len(), 256);
}
#[test]
fn test_bu40n_identity() {
let mut inquiry = vec![0u8; 96];
inquiry[4] = 0x5B;
inquiry[8..16].copy_from_slice(b"HL-DT-ST");
inquiry[16..32].copy_from_slice(b"BD-RE BU40N ");
inquiry[32..36].copy_from_slice(b"1.03");
inquiry[36..43].copy_from_slice(b"NM00000");
let id = DriveId::from_inquiry(&inquiry, "211810241934");
assert_eq!(id.vendor_id.trim(), "HL-DT-ST");
assert_eq!(id.product_id.trim(), "BD-RE BU40N");
assert_eq!(id.product_revision.trim(), "1.03");
assert_eq!(id.vendor_specific.trim(), "NM00000");
assert_eq!(id.firmware_date, "211810241934");
assert_eq!(id.match_key(), "HL-DT-ST|BD-RE BU40N|1.03|NM00000");
}
#[test]
fn test_pioneer_identity() {
let mut inquiry = vec![0u8; 96];
inquiry[4] = 0x5B;
inquiry[8..16].copy_from_slice(b"PIONEER ");
inquiry[16..32].copy_from_slice(b"BD-RW BDR-S09 ");
inquiry[32..36].copy_from_slice(b"1.34");
inquiry[36..43].copy_from_slice(b" 16/04/");
let id = DriveId::from_inquiry(&inquiry, "201604250000");
assert_eq!(id.vendor_id.trim(), "PIONEER");
assert_eq!(id.product_id.trim(), "BD-RW BDR-S09");
assert_eq!(id.product_revision.trim(), "1.34");
assert_eq!(id.vendor_specific.trim(), "16/04/");
assert_eq!(id.firmware_date, "201604250000");
}
}