]>
Commit | Line | Data |
---|---|---|
e0f83fbe | 1 | /* |
670e4705 KS |
2 | * ata_id - reads product/serial number from ATA drives |
3 | * | |
7d563a17 | 4 | * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org> |
66094a4a | 5 | * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net> |
6992637e | 6 | * Copyright (C) 2009-2010 David Zeuthen <zeuthen@gmail.com> |
670e4705 | 7 | * |
55e9959b KS |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
670e4705 KS |
20 | */ |
21 | ||
670e4705 KS |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
66094a4a | 24 | #include <stdint.h> |
670e4705 KS |
25 | #include <unistd.h> |
26 | #include <fcntl.h> | |
27 | #include <ctype.h> | |
66094a4a | 28 | #include <assert.h> |
1aa1e248 | 29 | #include <string.h> |
670e4705 | 30 | #include <errno.h> |
4fa3d99d | 31 | #include <getopt.h> |
66094a4a DZ |
32 | #include <scsi/scsi.h> |
33 | #include <scsi/sg.h> | |
34 | #include <scsi/scsi_ioctl.h> | |
670e4705 KS |
35 | #include <sys/ioctl.h> |
36 | #include <sys/types.h> | |
37 | #include <sys/stat.h> | |
38 | #include <linux/types.h> | |
39 | #include <linux/hdreg.h> | |
66094a4a | 40 | #include <linux/fs.h> |
160b069c | 41 | #include <linux/cdrom.h> |
6992637e | 42 | #include <linux/bsg.h> |
66094a4a | 43 | #include <arpa/inet.h> |
670e4705 | 44 | |
9060b066 KS |
45 | #include "libudev.h" |
46 | #include "libudev-private.h" | |
670e4705 | 47 | |
6992637e | 48 | #define COMMAND_TIMEOUT_MSEC (30 * 1000) |
66094a4a | 49 | |
6992637e | 50 | static int disk_scsi_inquiry_command(int fd, |
912541b0 KS |
51 | void *buf, |
52 | size_t buf_len) | |
66094a4a | 53 | { |
912541b0 KS |
54 | struct sg_io_v4 io_v4; |
55 | uint8_t cdb[6]; | |
56 | uint8_t sense[32]; | |
57 | int ret; | |
58 | ||
59 | /* | |
60 | * INQUIRY, see SPC-4 section 6.4 | |
61 | */ | |
62 | memset(cdb, 0, sizeof(cdb)); | |
63 | cdb[0] = 0x12; /* OPERATION CODE: INQUIRY */ | |
64 | cdb[3] = (buf_len >> 8); /* ALLOCATION LENGTH */ | |
65 | cdb[4] = (buf_len & 0xff); | |
66 | ||
67 | memset(sense, 0, sizeof(sense)); | |
68 | ||
69 | memset(&io_v4, 0, sizeof(struct sg_io_v4)); | |
70 | io_v4.guard = 'Q'; | |
71 | io_v4.protocol = BSG_PROTOCOL_SCSI; | |
72 | io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; | |
73 | io_v4.request_len = sizeof (cdb); | |
74 | io_v4.request = (uintptr_t) cdb; | |
75 | io_v4.max_response_len = sizeof (sense); | |
76 | io_v4.response = (uintptr_t) sense; | |
77 | io_v4.din_xfer_len = buf_len; | |
78 | io_v4.din_xferp = (uintptr_t) buf; | |
79 | io_v4.timeout = COMMAND_TIMEOUT_MSEC; | |
80 | ||
81 | ret = ioctl(fd, SG_IO, &io_v4); | |
82 | if (ret != 0) { | |
83 | /* could be that the driver doesn't do version 4, try version 3 */ | |
84 | if (errno == EINVAL) { | |
85 | struct sg_io_hdr io_hdr; | |
86 | ||
87 | memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); | |
88 | io_hdr.interface_id = 'S'; | |
89 | io_hdr.cmdp = (unsigned char*) cdb; | |
90 | io_hdr.cmd_len = sizeof (cdb); | |
91 | io_hdr.dxferp = buf; | |
92 | io_hdr.dxfer_len = buf_len; | |
93 | io_hdr.sbp = sense; | |
94 | io_hdr.mx_sb_len = sizeof (sense); | |
95 | io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
96 | io_hdr.timeout = COMMAND_TIMEOUT_MSEC; | |
97 | ||
98 | ret = ioctl(fd, SG_IO, &io_hdr); | |
99 | if (ret != 0) | |
100 | goto out; | |
101 | ||
102 | /* even if the ioctl succeeds, we need to check the return value */ | |
103 | if (!(io_hdr.status == 0 && | |
104 | io_hdr.host_status == 0 && | |
105 | io_hdr.driver_status == 0)) { | |
106 | errno = EIO; | |
107 | ret = -1; | |
108 | goto out; | |
109 | } | |
110 | } else { | |
111 | goto out; | |
112 | } | |
113 | } | |
114 | ||
115 | /* even if the ioctl succeeds, we need to check the return value */ | |
116 | if (!(io_v4.device_status == 0 && | |
117 | io_v4.transport_status == 0 && | |
118 | io_v4.driver_status == 0)) { | |
119 | errno = EIO; | |
120 | ret = -1; | |
121 | goto out; | |
122 | } | |
6992637e DZ |
123 | |
124 | out: | |
912541b0 | 125 | return ret; |
66094a4a DZ |
126 | } |
127 | ||
912541b0 KS |
128 | static int disk_identify_command(int fd, |
129 | void *buf, | |
130 | size_t buf_len) | |
66094a4a | 131 | { |
912541b0 KS |
132 | struct sg_io_v4 io_v4; |
133 | uint8_t cdb[12]; | |
134 | uint8_t sense[32]; | |
135 | uint8_t *desc = sense+8; | |
136 | int ret; | |
137 | ||
138 | /* | |
139 | * ATA Pass-Through 12 byte command, as described in | |
140 | * | |
141 | * T10 04-262r8 ATA Command Pass-Through | |
142 | * | |
143 | * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf | |
144 | */ | |
145 | memset(cdb, 0, sizeof(cdb)); | |
146 | cdb[0] = 0xa1; /* OPERATION CODE: 12 byte pass through */ | |
147 | cdb[1] = 4 << 1; /* PROTOCOL: PIO Data-in */ | |
148 | cdb[2] = 0x2e; /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */ | |
149 | cdb[3] = 0; /* FEATURES */ | |
150 | cdb[4] = 1; /* SECTORS */ | |
151 | cdb[5] = 0; /* LBA LOW */ | |
152 | cdb[6] = 0; /* LBA MID */ | |
153 | cdb[7] = 0; /* LBA HIGH */ | |
154 | cdb[8] = 0 & 0x4F; /* SELECT */ | |
155 | cdb[9] = 0xEC; /* Command: ATA IDENTIFY DEVICE */; | |
156 | memset(sense, 0, sizeof(sense)); | |
157 | ||
158 | memset(&io_v4, 0, sizeof(struct sg_io_v4)); | |
159 | io_v4.guard = 'Q'; | |
160 | io_v4.protocol = BSG_PROTOCOL_SCSI; | |
161 | io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; | |
162 | io_v4.request_len = sizeof (cdb); | |
163 | io_v4.request = (uintptr_t) cdb; | |
164 | io_v4.max_response_len = sizeof (sense); | |
165 | io_v4.response = (uintptr_t) sense; | |
166 | io_v4.din_xfer_len = buf_len; | |
167 | io_v4.din_xferp = (uintptr_t) buf; | |
168 | io_v4.timeout = COMMAND_TIMEOUT_MSEC; | |
169 | ||
170 | ret = ioctl(fd, SG_IO, &io_v4); | |
171 | if (ret != 0) { | |
172 | /* could be that the driver doesn't do version 4, try version 3 */ | |
173 | if (errno == EINVAL) { | |
174 | struct sg_io_hdr io_hdr; | |
175 | ||
176 | memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); | |
177 | io_hdr.interface_id = 'S'; | |
178 | io_hdr.cmdp = (unsigned char*) cdb; | |
179 | io_hdr.cmd_len = sizeof (cdb); | |
180 | io_hdr.dxferp = buf; | |
181 | io_hdr.dxfer_len = buf_len; | |
182 | io_hdr.sbp = sense; | |
183 | io_hdr.mx_sb_len = sizeof (sense); | |
184 | io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
185 | io_hdr.timeout = COMMAND_TIMEOUT_MSEC; | |
186 | ||
187 | ret = ioctl(fd, SG_IO, &io_hdr); | |
188 | if (ret != 0) | |
189 | goto out; | |
190 | } else { | |
191 | goto out; | |
192 | } | |
193 | } | |
194 | ||
195 | if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) { | |
196 | errno = EIO; | |
197 | ret = -1; | |
198 | goto out; | |
199 | } | |
66094a4a | 200 | |
6992637e | 201 | out: |
912541b0 | 202 | return ret; |
66094a4a DZ |
203 | } |
204 | ||
912541b0 KS |
205 | static int disk_identify_packet_device_command(int fd, |
206 | void *buf, | |
207 | size_t buf_len) | |
560de575 | 208 | { |
912541b0 KS |
209 | struct sg_io_v4 io_v4; |
210 | uint8_t cdb[16]; | |
211 | uint8_t sense[32]; | |
212 | uint8_t *desc = sense+8; | |
213 | int ret; | |
214 | ||
215 | /* | |
216 | * ATA Pass-Through 16 byte command, as described in | |
217 | * | |
218 | * T10 04-262r8 ATA Command Pass-Through | |
219 | * | |
220 | * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf | |
221 | */ | |
222 | memset(cdb, 0, sizeof(cdb)); | |
223 | cdb[0] = 0x85; /* OPERATION CODE: 16 byte pass through */ | |
224 | cdb[1] = 4 << 1; /* PROTOCOL: PIO Data-in */ | |
225 | cdb[2] = 0x2e; /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */ | |
226 | cdb[3] = 0; /* FEATURES */ | |
227 | cdb[4] = 0; /* FEATURES */ | |
228 | cdb[5] = 0; /* SECTORS */ | |
229 | cdb[6] = 1; /* SECTORS */ | |
230 | cdb[7] = 0; /* LBA LOW */ | |
231 | cdb[8] = 0; /* LBA LOW */ | |
232 | cdb[9] = 0; /* LBA MID */ | |
233 | cdb[10] = 0; /* LBA MID */ | |
234 | cdb[11] = 0; /* LBA HIGH */ | |
235 | cdb[12] = 0; /* LBA HIGH */ | |
236 | cdb[13] = 0; /* DEVICE */ | |
237 | cdb[14] = 0xA1; /* Command: ATA IDENTIFY PACKET DEVICE */; | |
238 | cdb[15] = 0; /* CONTROL */ | |
239 | memset(sense, 0, sizeof(sense)); | |
240 | ||
241 | memset(&io_v4, 0, sizeof(struct sg_io_v4)); | |
242 | io_v4.guard = 'Q'; | |
243 | io_v4.protocol = BSG_PROTOCOL_SCSI; | |
244 | io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; | |
245 | io_v4.request_len = sizeof (cdb); | |
246 | io_v4.request = (uintptr_t) cdb; | |
247 | io_v4.max_response_len = sizeof (sense); | |
248 | io_v4.response = (uintptr_t) sense; | |
249 | io_v4.din_xfer_len = buf_len; | |
250 | io_v4.din_xferp = (uintptr_t) buf; | |
251 | io_v4.timeout = COMMAND_TIMEOUT_MSEC; | |
252 | ||
253 | ret = ioctl(fd, SG_IO, &io_v4); | |
254 | if (ret != 0) { | |
255 | /* could be that the driver doesn't do version 4, try version 3 */ | |
256 | if (errno == EINVAL) { | |
257 | struct sg_io_hdr io_hdr; | |
258 | ||
259 | memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); | |
260 | io_hdr.interface_id = 'S'; | |
261 | io_hdr.cmdp = (unsigned char*) cdb; | |
262 | io_hdr.cmd_len = sizeof (cdb); | |
263 | io_hdr.dxferp = buf; | |
264 | io_hdr.dxfer_len = buf_len; | |
265 | io_hdr.sbp = sense; | |
266 | io_hdr.mx_sb_len = sizeof (sense); | |
267 | io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
268 | io_hdr.timeout = COMMAND_TIMEOUT_MSEC; | |
269 | ||
270 | ret = ioctl(fd, SG_IO, &io_hdr); | |
271 | if (ret != 0) | |
272 | goto out; | |
273 | } else { | |
274 | goto out; | |
275 | } | |
276 | } | |
277 | ||
278 | if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) { | |
279 | errno = EIO; | |
280 | ret = -1; | |
281 | goto out; | |
282 | } | |
560de575 DZ |
283 | |
284 | out: | |
912541b0 | 285 | return ret; |
560de575 DZ |
286 | } |
287 | ||
66094a4a DZ |
288 | /** |
289 | * disk_identify_get_string: | |
290 | * @identify: A block of IDENTIFY data | |
291 | * @offset_words: Offset of the string to get, in words. | |
292 | * @dest: Destination buffer for the string. | |
293 | * @dest_len: Length of destination buffer, in bytes. | |
294 | * | |
295 | * Copies the ATA string from @identify located at @offset_words into @dest. | |
296 | */ | |
09db3f5b KS |
297 | static void disk_identify_get_string(uint8_t identify[512], |
298 | unsigned int offset_words, | |
299 | char *dest, | |
300 | size_t dest_len) | |
66094a4a | 301 | { |
912541b0 KS |
302 | unsigned int c1; |
303 | unsigned int c2; | |
304 | ||
09db3f5b KS |
305 | assert(identify != NULL); |
306 | assert(dest != NULL); | |
307 | assert((dest_len & 1) == 0); | |
912541b0 KS |
308 | |
309 | while (dest_len > 0) { | |
310 | c1 = ((uint16_t *) identify)[offset_words] >> 8; | |
311 | c2 = ((uint16_t *) identify)[offset_words] & 0xff; | |
312 | *dest = c1; | |
313 | dest++; | |
314 | *dest = c2; | |
315 | dest++; | |
316 | offset_words++; | |
317 | dest_len -= 2; | |
318 | } | |
66094a4a DZ |
319 | } |
320 | ||
09db3f5b KS |
321 | static void disk_identify_fixup_string(uint8_t identify[512], |
322 | unsigned int offset_words, | |
323 | size_t len) | |
66094a4a | 324 | { |
912541b0 KS |
325 | disk_identify_get_string(identify, offset_words, |
326 | (char *) identify + offset_words * 2, len); | |
66094a4a DZ |
327 | } |
328 | ||
329 | static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offset_words) | |
330 | { | |
912541b0 | 331 | uint16_t *p; |
66094a4a | 332 | |
912541b0 KS |
333 | p = (uint16_t *) identify; |
334 | p[offset_words] = le16toh (p[offset_words]); | |
66094a4a DZ |
335 | } |
336 | ||
337 | /** | |
338 | * disk_identify: | |
339 | * @udev: The libudev context. | |
340 | * @fd: File descriptor for the block device. | |
341 | * @out_identify: Return location for IDENTIFY data. | |
560de575 | 342 | * @out_is_packet_device: Return location for whether returned data is from a IDENTIFY PACKET DEVICE. |
66094a4a | 343 | * |
560de575 DZ |
344 | * Sends the IDENTIFY DEVICE or IDENTIFY PACKET DEVICE command to the |
345 | * device represented by @fd. If successful, then the result will be | |
346 | * copied into @out_identify and @out_is_packet_device. | |
66094a4a DZ |
347 | * |
348 | * This routine is based on code from libatasmart, Copyright 2008 | |
349 | * Lennart Poettering, LGPL v2.1. | |
350 | * | |
560de575 DZ |
351 | * Returns: 0 if the data was successfully obtained, otherwise |
352 | * non-zero with errno set. | |
66094a4a | 353 | */ |
09db3f5b KS |
354 | static int disk_identify(struct udev *udev, |
355 | int fd, | |
356 | uint8_t out_identify[512], | |
357 | int *out_is_packet_device) | |
66094a4a | 358 | { |
912541b0 KS |
359 | int ret; |
360 | uint8_t inquiry_buf[36]; | |
361 | int peripheral_device_type; | |
362 | int all_nul_bytes; | |
363 | int n; | |
364 | int is_packet_device; | |
365 | ||
09db3f5b | 366 | assert(out_identify != NULL); |
912541b0 KS |
367 | |
368 | /* init results */ | |
369 | ret = -1; | |
09db3f5b | 370 | memset(out_identify, '\0', 512); |
912541b0 KS |
371 | is_packet_device = 0; |
372 | ||
373 | /* If we were to use ATA PASS_THROUGH (12) on an ATAPI device | |
374 | * we could accidentally blank media. This is because MMC's BLANK | |
375 | * command has the same op-code (0x61). | |
376 | * | |
377 | * To prevent this from happening we bail out if the device | |
378 | * isn't a Direct Access Block Device, e.g. SCSI type 0x00 | |
379 | * (CD/DVD devices are type 0x05). So we send a SCSI INQUIRY | |
380 | * command first... libata is handling this via its SCSI | |
381 | * emulation layer. | |
382 | * | |
383 | * This also ensures that we're actually dealing with a device | |
384 | * that understands SCSI commands. | |
385 | * | |
386 | * (Yes, it is a bit perverse that we're tunneling the ATA | |
387 | * command through SCSI and relying on the ATA driver | |
388 | * emulating SCSI well-enough...) | |
389 | * | |
390 | * (See commit 160b069c25690bfb0c785994c7c3710289179107 for | |
391 | * the original bug-fix and see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=556635 | |
392 | * for the original bug-report.) | |
393 | */ | |
394 | ret = disk_scsi_inquiry_command (fd, inquiry_buf, sizeof (inquiry_buf)); | |
395 | if (ret != 0) | |
396 | goto out; | |
397 | ||
398 | /* SPC-4, section 6.4.2: Standard INQUIRY data */ | |
399 | peripheral_device_type = inquiry_buf[0] & 0x1f; | |
400 | if (peripheral_device_type == 0x05) | |
401 | { | |
402 | is_packet_device = 1; | |
403 | ret = disk_identify_packet_device_command(fd, out_identify, 512); | |
404 | goto check_nul_bytes; | |
405 | } | |
406 | if (peripheral_device_type != 0x00) { | |
407 | ret = -1; | |
408 | errno = EIO; | |
409 | goto out; | |
410 | } | |
411 | ||
412 | /* OK, now issue the IDENTIFY DEVICE command */ | |
413 | ret = disk_identify_command(fd, out_identify, 512); | |
414 | if (ret != 0) | |
415 | goto out; | |
66094a4a | 416 | |
560de575 | 417 | check_nul_bytes: |
912541b0 KS |
418 | /* Check if IDENTIFY data is all NUL bytes - if so, bail */ |
419 | all_nul_bytes = 1; | |
420 | for (n = 0; n < 512; n++) { | |
421 | if (out_identify[n] != '\0') { | |
422 | all_nul_bytes = 0; | |
423 | break; | |
424 | } | |
425 | } | |
426 | ||
427 | if (all_nul_bytes) { | |
428 | ret = -1; | |
429 | errno = EIO; | |
430 | goto out; | |
431 | } | |
66094a4a | 432 | |
6992637e | 433 | out: |
912541b0 KS |
434 | if (out_is_packet_device != NULL) |
435 | *out_is_packet_device = is_packet_device; | |
436 | return ret; | |
66094a4a DZ |
437 | } |
438 | ||
7d563a17 | 439 | static void log_fn(struct udev *udev, int priority, |
912541b0 KS |
440 | const char *file, int line, const char *fn, |
441 | const char *format, va_list args) | |
670e4705 | 442 | { |
912541b0 | 443 | vsyslog(priority, format, args); |
670e4705 | 444 | } |
670e4705 | 445 | |
670e4705 KS |
446 | int main(int argc, char *argv[]) |
447 | { | |
912541b0 KS |
448 | struct udev *udev; |
449 | struct hd_driveid id; | |
450 | uint8_t identify[512]; | |
451 | uint16_t *identify_words; | |
452 | char model[41]; | |
453 | char model_enc[256]; | |
454 | char serial[21]; | |
455 | char revision[9]; | |
456 | const char *node = NULL; | |
457 | int export = 0; | |
458 | int fd; | |
459 | uint16_t word; | |
460 | int rc = 0; | |
461 | int is_packet_device = 0; | |
462 | static const struct option options[] = { | |
463 | { "export", no_argument, NULL, 'x' }, | |
464 | { "help", no_argument, NULL, 'h' }, | |
465 | {} | |
466 | }; | |
467 | ||
468 | udev = udev_new(); | |
469 | if (udev == NULL) | |
470 | goto exit; | |
471 | ||
472 | udev_log_init("ata_id"); | |
473 | udev_set_log_fn(udev, log_fn); | |
474 | ||
475 | while (1) { | |
476 | int option; | |
477 | ||
478 | option = getopt_long(argc, argv, "xh", options, NULL); | |
479 | if (option == -1) | |
480 | break; | |
481 | ||
482 | switch (option) { | |
483 | case 'x': | |
484 | export = 1; | |
485 | break; | |
486 | case 'h': | |
487 | printf("Usage: ata_id [--export] [--help] <device>\n" | |
488 | " --export print values as environment keys\n" | |
489 | " --help print this help text\n\n"); | |
490 | goto exit; | |
491 | } | |
492 | } | |
493 | ||
494 | node = argv[optind]; | |
495 | if (node == NULL) { | |
496 | err(udev, "no node specified\n"); | |
497 | rc = 1; | |
498 | goto exit; | |
499 | } | |
500 | ||
501 | fd = open(node, O_RDONLY|O_NONBLOCK); | |
502 | if (fd < 0) { | |
503 | err(udev, "unable to open '%s'\n", node); | |
504 | rc = 1; | |
505 | goto exit; | |
506 | } | |
507 | ||
508 | if (disk_identify(udev, fd, identify, &is_packet_device) == 0) { | |
509 | /* | |
510 | * fix up only the fields from the IDENTIFY data that we are going to | |
511 | * use and copy it into the hd_driveid struct for convenience | |
512 | */ | |
09db3f5b KS |
513 | disk_identify_fixup_string (identify, 10, 20); /* serial */ |
514 | disk_identify_fixup_string (identify, 23, 6); /* fwrev */ | |
515 | disk_identify_fixup_string (identify, 27, 40); /* model */ | |
516 | disk_identify_fixup_uint16 (identify, 0); /* configuration */ | |
517 | disk_identify_fixup_uint16 (identify, 75); /* queue depth */ | |
518 | disk_identify_fixup_uint16 (identify, 75); /* SATA capabilities */ | |
519 | disk_identify_fixup_uint16 (identify, 82); /* command set supported */ | |
520 | disk_identify_fixup_uint16 (identify, 83); /* command set supported */ | |
521 | disk_identify_fixup_uint16 (identify, 84); /* command set supported */ | |
522 | disk_identify_fixup_uint16 (identify, 85); /* command set supported */ | |
523 | disk_identify_fixup_uint16 (identify, 86); /* command set supported */ | |
524 | disk_identify_fixup_uint16 (identify, 87); /* command set supported */ | |
525 | disk_identify_fixup_uint16 (identify, 89); /* time required for SECURITY ERASE UNIT */ | |
526 | disk_identify_fixup_uint16 (identify, 90); /* time required for enhanced SECURITY ERASE UNIT */ | |
527 | disk_identify_fixup_uint16 (identify, 91); /* current APM values */ | |
528 | disk_identify_fixup_uint16 (identify, 94); /* current AAM value */ | |
529 | disk_identify_fixup_uint16 (identify, 128); /* device lock function */ | |
530 | disk_identify_fixup_uint16 (identify, 217); /* nominal media rotation rate */ | |
912541b0 KS |
531 | memcpy(&id, identify, sizeof id); |
532 | } else { | |
533 | /* If this fails, then try HDIO_GET_IDENTITY */ | |
534 | if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) { | |
535 | if (errno == ENOTTY) { | |
536 | info(udev, "HDIO_GET_IDENTITY unsupported for '%s'\n", node); | |
537 | rc = 2; | |
538 | } else { | |
539 | err(udev, "HDIO_GET_IDENTITY failed for '%s': %m\n", node); | |
540 | rc = 3; | |
541 | } | |
542 | goto close; | |
543 | } | |
544 | } | |
545 | identify_words = (uint16_t *) identify; | |
546 | ||
547 | memcpy (model, id.model, 40); | |
548 | model[40] = '\0'; | |
549 | udev_util_encode_string(model, model_enc, sizeof(model_enc)); | |
550 | util_replace_whitespace((char *) id.model, model, 40); | |
551 | util_replace_chars(model, NULL); | |
552 | util_replace_whitespace((char *) id.serial_no, serial, 20); | |
553 | util_replace_chars(serial, NULL); | |
554 | util_replace_whitespace((char *) id.fw_rev, revision, 8); | |
555 | util_replace_chars(revision, NULL); | |
556 | ||
557 | if (export) { | |
558 | /* Set this to convey the disk speaks the ATA protocol */ | |
559 | printf("ID_ATA=1\n"); | |
560 | ||
561 | if ((id.config >> 8) & 0x80) { | |
562 | /* This is an ATAPI device */ | |
563 | switch ((id.config >> 8) & 0x1f) { | |
564 | case 0: | |
565 | printf("ID_TYPE=cd\n"); | |
566 | break; | |
567 | case 1: | |
568 | printf("ID_TYPE=tape\n"); | |
569 | break; | |
570 | case 5: | |
571 | printf("ID_TYPE=cd\n"); | |
572 | break; | |
573 | case 7: | |
574 | printf("ID_TYPE=optical\n"); | |
575 | break; | |
576 | default: | |
577 | printf("ID_TYPE=generic\n"); | |
578 | break; | |
579 | } | |
580 | } else { | |
581 | printf("ID_TYPE=disk\n"); | |
582 | } | |
583 | printf("ID_BUS=ata\n"); | |
584 | printf("ID_MODEL=%s\n", model); | |
585 | printf("ID_MODEL_ENC=%s\n", model_enc); | |
586 | printf("ID_REVISION=%s\n", revision); | |
587 | if (serial[0] != '\0') { | |
588 | printf("ID_SERIAL=%s_%s\n", model, serial); | |
589 | printf("ID_SERIAL_SHORT=%s\n", serial); | |
590 | } else { | |
591 | printf("ID_SERIAL=%s\n", model); | |
592 | } | |
593 | ||
594 | if (id.command_set_1 & (1<<5)) { | |
595 | printf ("ID_ATA_WRITE_CACHE=1\n"); | |
596 | printf ("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0); | |
597 | } | |
598 | if (id.command_set_1 & (1<<10)) { | |
599 | printf("ID_ATA_FEATURE_SET_HPA=1\n"); | |
600 | printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id.cfs_enable_1 & (1<<10)) ? 1 : 0); | |
601 | ||
602 | /* | |
603 | * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address | |
604 | * so it is easy to check whether the protected area is in use. | |
605 | */ | |
606 | } | |
607 | if (id.command_set_1 & (1<<3)) { | |
608 | printf("ID_ATA_FEATURE_SET_PM=1\n"); | |
609 | printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id.cfs_enable_1 & (1<<3)) ? 1 : 0); | |
610 | } | |
611 | if (id.command_set_1 & (1<<1)) { | |
612 | printf("ID_ATA_FEATURE_SET_SECURITY=1\n"); | |
613 | printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id.cfs_enable_1 & (1<<1)) ? 1 : 0); | |
614 | printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id.trseuc * 2); | |
615 | if ((id.cfs_enable_1 & (1<<1))) /* enabled */ { | |
616 | if (id.dlf & (1<<8)) | |
617 | printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n"); | |
618 | else | |
619 | printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n"); | |
620 | } | |
621 | if (id.dlf & (1<<5)) | |
622 | printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2); | |
623 | if (id.dlf & (1<<4)) | |
624 | printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n"); | |
625 | if (id.dlf & (1<<3)) | |
626 | printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n"); | |
627 | if (id.dlf & (1<<2)) | |
628 | printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n"); | |
629 | } | |
630 | if (id.command_set_1 & (1<<0)) { | |
631 | printf("ID_ATA_FEATURE_SET_SMART=1\n"); | |
632 | printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id.cfs_enable_1 & (1<<0)) ? 1 : 0); | |
633 | } | |
634 | if (id.command_set_2 & (1<<9)) { | |
635 | printf("ID_ATA_FEATURE_SET_AAM=1\n"); | |
636 | printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<9)) ? 1 : 0); | |
637 | printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id.acoustic >> 8); | |
638 | printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id.acoustic & 0xff); | |
639 | } | |
640 | if (id.command_set_2 & (1<<5)) { | |
641 | printf("ID_ATA_FEATURE_SET_PUIS=1\n"); | |
642 | printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id.cfs_enable_2 & (1<<5)) ? 1 : 0); | |
643 | } | |
644 | if (id.command_set_2 & (1<<3)) { | |
645 | printf("ID_ATA_FEATURE_SET_APM=1\n"); | |
646 | printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<3)) ? 1 : 0); | |
647 | if ((id.cfs_enable_2 & (1<<3))) | |
648 | printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id.CurAPMvalues & 0xff); | |
649 | } | |
650 | if (id.command_set_2 & (1<<0)) | |
651 | printf("ID_ATA_DOWNLOAD_MICROCODE=1\n"); | |
652 | ||
653 | /* | |
654 | * Word 76 indicates the capabilities of a SATA device. A PATA device shall set | |
655 | * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then | |
656 | * the device does not claim compliance with the Serial ATA specification and words | |
657 | * 76 through 79 are not valid and shall be ignored. | |
658 | */ | |
659 | word = *((uint16_t *) identify + 76); | |
660 | if (word != 0x0000 && word != 0xffff) { | |
661 | printf("ID_ATA_SATA=1\n"); | |
662 | /* | |
663 | * If bit 2 of word 76 is set to one, then the device supports the Gen2 | |
664 | * signaling rate of 3.0 Gb/s (see SATA 2.6). | |
665 | * | |
666 | * If bit 1 of word 76 is set to one, then the device supports the Gen1 | |
667 | * signaling rate of 1.5 Gb/s (see SATA 2.6). | |
668 | */ | |
669 | if (word & (1<<2)) | |
670 | printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n"); | |
671 | if (word & (1<<1)) | |
672 | printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n"); | |
673 | } | |
674 | ||
675 | /* Word 217 indicates the nominal media rotation rate of the device */ | |
676 | word = *((uint16_t *) identify + 217); | |
677 | if (word != 0x0000) { | |
678 | if (word == 0x0001) { | |
679 | printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */ | |
680 | } else if (word >= 0x0401 && word <= 0xfffe) { | |
681 | printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word); | |
682 | } | |
683 | } | |
684 | ||
685 | /* | |
686 | * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier | |
687 | * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE. | |
688 | * All other values are reserved. | |
689 | */ | |
690 | word = *((uint16_t *) identify + 108); | |
691 | if ((word & 0xf000) == 0x5000) { | |
692 | uint64_t wwwn; | |
693 | ||
694 | wwwn = *((uint16_t *) identify + 108); | |
695 | wwwn <<= 16; | |
696 | wwwn |= *((uint16_t *) identify + 109); | |
697 | wwwn <<= 16; | |
698 | wwwn |= *((uint16_t *) identify + 110); | |
699 | wwwn <<= 16; | |
700 | wwwn |= *((uint16_t *) identify + 111); | |
701 | printf("ID_WWN=0x%llx\n", (unsigned long long int) wwwn); | |
702 | /* ATA devices have no vendor extension */ | |
703 | printf("ID_WWN_WITH_EXTENSION=0x%llx\n", (unsigned long long int) wwwn); | |
704 | } | |
705 | ||
706 | /* from Linux's include/linux/ata.h */ | |
707 | if (identify_words[0] == 0x848a || identify_words[0] == 0x844a) { | |
708 | printf("ID_ATA_CFA=1\n"); | |
709 | } else { | |
710 | if ((identify_words[83] & 0xc004) == 0x4004) { | |
711 | printf("ID_ATA_CFA=1\n"); | |
712 | } | |
713 | } | |
714 | } else { | |
715 | if (serial[0] != '\0') | |
716 | printf("%s_%s\n", model, serial); | |
717 | else | |
718 | printf("%s\n", model); | |
719 | } | |
670e4705 | 720 | close: |
912541b0 | 721 | close(fd); |
670e4705 | 722 | exit: |
912541b0 KS |
723 | udev_unref(udev); |
724 | udev_log_close(); | |
725 | return rc; | |
670e4705 | 726 | } |