2 * ata_id - reads product/serial number from ATA drives
4 * Copyright (C) 2005-2008 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
6 * Copyright (C) 2009-2010 David Zeuthen <zeuthen@gmail.com>
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.
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.
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/>.
31 #include <scsi/scsi.h>
33 #include <scsi/scsi_ioctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
37 #include <linux/types.h>
38 #include <linux/hdreg.h>
40 #include <linux/cdrom.h>
41 #include <linux/bsg.h>
42 #include <arpa/inet.h>
45 #include "libudev-private.h"
46 #include "udev-util.h"
49 #define COMMAND_TIMEOUT_MSEC (30 * 1000)
51 static int disk_scsi_inquiry_command(int fd
,
57 * INQUIRY, see SPC-4 section 6.4
59 [0] = 0x12, /* OPERATION CODE: INQUIRY */
60 [3] = (buf_len
>> 8), /* ALLOCATION LENGTH */
61 [4] = (buf_len
& 0xff),
63 uint8_t sense
[32] = {};
64 struct sg_io_v4 io_v4
= {
66 .protocol
= BSG_PROTOCOL_SCSI
,
67 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
68 .request_len
= sizeof(cdb
),
69 .request
= (uintptr_t) cdb
,
70 .max_response_len
= sizeof(sense
),
71 .response
= (uintptr_t) sense
,
72 .din_xfer_len
= buf_len
,
73 .din_xferp
= (uintptr_t) buf
,
74 .timeout
= COMMAND_TIMEOUT_MSEC
,
78 ret
= ioctl(fd
, SG_IO
, &io_v4
);
80 /* could be that the driver doesn't do version 4, try version 3 */
81 if (errno
== EINVAL
) {
82 struct sg_io_hdr io_hdr
= {
84 .cmdp
= (unsigned char*) cdb
,
85 .cmd_len
= sizeof (cdb
),
89 .mx_sb_len
= sizeof(sense
),
90 .dxfer_direction
= SG_DXFER_FROM_DEV
,
91 .timeout
= COMMAND_TIMEOUT_MSEC
,
94 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
98 /* even if the ioctl succeeds, we need to check the return value */
99 if (!(io_hdr
.status
== 0 &&
100 io_hdr
.host_status
== 0 &&
101 io_hdr
.driver_status
== 0)) {
109 /* even if the ioctl succeeds, we need to check the return value */
110 if (!(io_v4
.device_status
== 0 &&
111 io_v4
.transport_status
== 0 &&
112 io_v4
.driver_status
== 0)) {
120 static int disk_identify_command(int fd
,
126 * ATA Pass-Through 12 byte command, as described in
128 * T10 04-262r8 ATA Command Pass-Through
130 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
132 [0] = 0xa1, /* OPERATION CODE: 12 byte pass through */
133 [1] = 4 << 1, /* PROTOCOL: PIO Data-in */
134 [2] = 0x2e, /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
135 [3] = 0, /* FEATURES */
136 [4] = 1, /* SECTORS */
137 [5] = 0, /* LBA LOW */
138 [6] = 0, /* LBA MID */
139 [7] = 0, /* LBA HIGH */
140 [8] = 0 & 0x4F, /* SELECT */
141 [9] = 0xEC, /* Command: ATA IDENTIFY DEVICE */
143 uint8_t sense
[32] = {};
144 uint8_t *desc
= sense
+ 8;
145 struct sg_io_v4 io_v4
= {
147 .protocol
= BSG_PROTOCOL_SCSI
,
148 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
149 .request_len
= sizeof(cdb
),
150 .request
= (uintptr_t) cdb
,
151 .max_response_len
= sizeof(sense
),
152 .response
= (uintptr_t) sense
,
153 .din_xfer_len
= buf_len
,
154 .din_xferp
= (uintptr_t) buf
,
155 .timeout
= COMMAND_TIMEOUT_MSEC
,
159 ret
= ioctl(fd
, SG_IO
, &io_v4
);
161 /* could be that the driver doesn't do version 4, try version 3 */
162 if (errno
== EINVAL
) {
163 struct sg_io_hdr io_hdr
= {
165 .cmdp
= (unsigned char*) cdb
,
166 .cmd_len
= sizeof (cdb
),
168 .dxfer_len
= buf_len
,
170 .mx_sb_len
= sizeof (sense
),
171 .dxfer_direction
= SG_DXFER_FROM_DEV
,
172 .timeout
= COMMAND_TIMEOUT_MSEC
,
175 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
182 if (!(sense
[0] == 0x72 && desc
[0] == 0x9 && desc
[1] == 0x0c)) {
190 static int disk_identify_packet_device_command(int fd
,
196 * ATA Pass-Through 16 byte command, as described in
198 * T10 04-262r8 ATA Command Pass-Through
200 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
202 [0] = 0x85, /* OPERATION CODE: 16 byte pass through */
203 [1] = 4 << 1, /* PROTOCOL: PIO Data-in */
204 [2] = 0x2e, /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
205 [3] = 0, /* FEATURES */
206 [4] = 0, /* FEATURES */
207 [5] = 0, /* SECTORS */
208 [6] = 1, /* SECTORS */
209 [7] = 0, /* LBA LOW */
210 [8] = 0, /* LBA LOW */
211 [9] = 0, /* LBA MID */
212 [10] = 0, /* LBA MID */
213 [11] = 0, /* LBA HIGH */
214 [12] = 0, /* LBA HIGH */
215 [13] = 0, /* DEVICE */
216 [14] = 0xA1, /* Command: ATA IDENTIFY PACKET DEVICE */
217 [15] = 0, /* CONTROL */
219 uint8_t sense
[32] = {};
220 uint8_t *desc
= sense
+ 8;
221 struct sg_io_v4 io_v4
= {
223 .protocol
= BSG_PROTOCOL_SCSI
,
224 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
225 .request_len
= sizeof (cdb
),
226 .request
= (uintptr_t) cdb
,
227 .max_response_len
= sizeof (sense
),
228 .response
= (uintptr_t) sense
,
229 .din_xfer_len
= buf_len
,
230 .din_xferp
= (uintptr_t) buf
,
231 .timeout
= COMMAND_TIMEOUT_MSEC
,
235 ret
= ioctl(fd
, SG_IO
, &io_v4
);
237 /* could be that the driver doesn't do version 4, try version 3 */
238 if (errno
== EINVAL
) {
239 struct sg_io_hdr io_hdr
= {
241 .cmdp
= (unsigned char*) cdb
,
242 .cmd_len
= sizeof (cdb
),
244 .dxfer_len
= buf_len
,
246 .mx_sb_len
= sizeof (sense
),
247 .dxfer_direction
= SG_DXFER_FROM_DEV
,
248 .timeout
= COMMAND_TIMEOUT_MSEC
,
251 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
258 if (!(sense
[0] == 0x72 && desc
[0] == 0x9 && desc
[1] == 0x0c)) {
267 * disk_identify_get_string:
268 * @identify: A block of IDENTIFY data
269 * @offset_words: Offset of the string to get, in words.
270 * @dest: Destination buffer for the string.
271 * @dest_len: Length of destination buffer, in bytes.
273 * Copies the ATA string from @identify located at @offset_words into @dest.
275 static void disk_identify_get_string(uint8_t identify
[512],
276 unsigned int offset_words
,
283 while (dest_len
> 0) {
284 c1
= identify
[offset_words
* 2 + 1];
285 c2
= identify
[offset_words
* 2];
295 static void disk_identify_fixup_string(uint8_t identify
[512],
296 unsigned int offset_words
,
299 disk_identify_get_string(identify
, offset_words
,
300 (char *) identify
+ offset_words
* 2, len
);
303 static void disk_identify_fixup_uint16 (uint8_t identify
[512], unsigned int offset_words
)
307 p
= (uint16_t *) identify
;
308 p
[offset_words
] = le16toh (p
[offset_words
]);
313 * @udev: The libudev context.
314 * @fd: File descriptor for the block device.
315 * @out_identify: Return location for IDENTIFY data.
316 * @out_is_packet_device: Return location for whether returned data is from a IDENTIFY PACKET DEVICE.
318 * Sends the IDENTIFY DEVICE or IDENTIFY PACKET DEVICE command to the
319 * device represented by @fd. If successful, then the result will be
320 * copied into @out_identify and @out_is_packet_device.
322 * This routine is based on code from libatasmart, Copyright 2008
323 * Lennart Poettering, LGPL v2.1.
325 * Returns: 0 if the data was successfully obtained, otherwise
326 * non-zero with errno set.
328 static int disk_identify(struct udev
*udev
,
330 uint8_t out_identify
[512],
331 int *out_is_packet_device
)
334 uint8_t inquiry_buf
[36];
335 int peripheral_device_type
;
338 int is_packet_device
= 0;
341 memzero(out_identify
, 512);
343 /* If we were to use ATA PASS_THROUGH (12) on an ATAPI device
344 * we could accidentally blank media. This is because MMC's BLANK
345 * command has the same op-code (0x61).
347 * To prevent this from happening we bail out if the device
348 * isn't a Direct Access Block Device, e.g. SCSI type 0x00
349 * (CD/DVD devices are type 0x05). So we send a SCSI INQUIRY
350 * command first... libata is handling this via its SCSI
353 * This also ensures that we're actually dealing with a device
354 * that understands SCSI commands.
356 * (Yes, it is a bit perverse that we're tunneling the ATA
357 * command through SCSI and relying on the ATA driver
358 * emulating SCSI well-enough...)
360 * (See commit 160b069c25690bfb0c785994c7c3710289179107 for
361 * the original bug-fix and see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=556635
362 * for the original bug-report.)
364 ret
= disk_scsi_inquiry_command (fd
, inquiry_buf
, sizeof (inquiry_buf
));
368 /* SPC-4, section 6.4.2: Standard INQUIRY data */
369 peripheral_device_type
= inquiry_buf
[0] & 0x1f;
370 if (peripheral_device_type
== 0x05)
372 is_packet_device
= 1;
373 ret
= disk_identify_packet_device_command(fd
, out_identify
, 512);
374 goto check_nul_bytes
;
376 if (peripheral_device_type
!= 0x00) {
382 /* OK, now issue the IDENTIFY DEVICE command */
383 ret
= disk_identify_command(fd
, out_identify
, 512);
388 /* Check if IDENTIFY data is all NUL bytes - if so, bail */
390 for (n
= 0; n
< 512; n
++) {
391 if (out_identify
[n
] != '\0') {
404 if (out_is_packet_device
!= NULL
)
405 *out_is_packet_device
= is_packet_device
;
409 int main(int argc
, char *argv
[])
411 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
412 struct hd_driveid id
;
418 uint16_t *identify_words
;
423 const char *node
= NULL
;
425 _cleanup_close_
int fd
= -1;
427 int is_packet_device
= 0;
428 static const struct option options
[] = {
429 { "export", no_argument
, NULL
, 'x' },
430 { "help", no_argument
, NULL
, 'h' },
434 log_parse_environment();
444 option
= getopt_long(argc
, argv
, "xh", options
, NULL
);
453 printf("Usage: ata_id [--export] [--help] <device>\n"
454 " --export print values as environment keys\n"
455 " --help print this help text\n\n");
462 log_error("no node specified");
466 fd
= open(node
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
);
468 log_error("unable to open '%s'", node
);
472 if (disk_identify(udev
, fd
, identify
.byte
, &is_packet_device
) == 0) {
474 * fix up only the fields from the IDENTIFY data that we are going to
475 * use and copy it into the hd_driveid struct for convenience
477 disk_identify_fixup_string(identify
.byte
, 10, 20); /* serial */
478 disk_identify_fixup_string(identify
.byte
, 23, 8); /* fwrev */
479 disk_identify_fixup_string(identify
.byte
, 27, 40); /* model */
480 disk_identify_fixup_uint16(identify
.byte
, 0); /* configuration */
481 disk_identify_fixup_uint16(identify
.byte
, 75); /* queue depth */
482 disk_identify_fixup_uint16(identify
.byte
, 75); /* SATA capabilities */
483 disk_identify_fixup_uint16(identify
.byte
, 82); /* command set supported */
484 disk_identify_fixup_uint16(identify
.byte
, 83); /* command set supported */
485 disk_identify_fixup_uint16(identify
.byte
, 84); /* command set supported */
486 disk_identify_fixup_uint16(identify
.byte
, 85); /* command set supported */
487 disk_identify_fixup_uint16(identify
.byte
, 86); /* command set supported */
488 disk_identify_fixup_uint16(identify
.byte
, 87); /* command set supported */
489 disk_identify_fixup_uint16(identify
.byte
, 89); /* time required for SECURITY ERASE UNIT */
490 disk_identify_fixup_uint16(identify
.byte
, 90); /* time required for enhanced SECURITY ERASE UNIT */
491 disk_identify_fixup_uint16(identify
.byte
, 91); /* current APM values */
492 disk_identify_fixup_uint16(identify
.byte
, 94); /* current AAM value */
493 disk_identify_fixup_uint16(identify
.byte
, 128); /* device lock function */
494 disk_identify_fixup_uint16(identify
.byte
, 217); /* nominal media rotation rate */
495 memcpy(&id
, identify
.byte
, sizeof id
);
497 /* If this fails, then try HDIO_GET_IDENTITY */
498 if (ioctl(fd
, HDIO_GET_IDENTITY
, &id
) != 0) {
499 log_debug_errno(errno
, "HDIO_GET_IDENTITY failed for '%s': %m", node
);
503 identify_words
= &identify
.wyde
;
505 memcpy (model
, id
.model
, 40);
507 udev_util_encode_string(model
, model_enc
, sizeof(model_enc
));
508 util_replace_whitespace((char *) id
.model
, model
, 40);
509 util_replace_chars(model
, NULL
);
510 util_replace_whitespace((char *) id
.serial_no
, serial
, 20);
511 util_replace_chars(serial
, NULL
);
512 util_replace_whitespace((char *) id
.fw_rev
, revision
, 8);
513 util_replace_chars(revision
, NULL
);
516 /* Set this to convey the disk speaks the ATA protocol */
517 printf("ID_ATA=1\n");
519 if ((id
.config
>> 8) & 0x80) {
520 /* This is an ATAPI device */
521 switch ((id
.config
>> 8) & 0x1f) {
523 printf("ID_TYPE=cd\n");
526 printf("ID_TYPE=tape\n");
529 printf("ID_TYPE=cd\n");
532 printf("ID_TYPE=optical\n");
535 printf("ID_TYPE=generic\n");
539 printf("ID_TYPE=disk\n");
541 printf("ID_BUS=ata\n");
542 printf("ID_MODEL=%s\n", model
);
543 printf("ID_MODEL_ENC=%s\n", model_enc
);
544 printf("ID_REVISION=%s\n", revision
);
545 if (serial
[0] != '\0') {
546 printf("ID_SERIAL=%s_%s\n", model
, serial
);
547 printf("ID_SERIAL_SHORT=%s\n", serial
);
549 printf("ID_SERIAL=%s\n", model
);
552 if (id
.command_set_1
& (1<<5)) {
553 printf("ID_ATA_WRITE_CACHE=1\n");
554 printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<5)) ? 1 : 0);
556 if (id
.command_set_1
& (1<<10)) {
557 printf("ID_ATA_FEATURE_SET_HPA=1\n");
558 printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<10)) ? 1 : 0);
561 * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address
562 * so it is easy to check whether the protected area is in use.
565 if (id
.command_set_1
& (1<<3)) {
566 printf("ID_ATA_FEATURE_SET_PM=1\n");
567 printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<3)) ? 1 : 0);
569 if (id
.command_set_1
& (1<<1)) {
570 printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
571 printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<1)) ? 1 : 0);
572 printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id
.trseuc
* 2);
573 if ((id
.cfs_enable_1
& (1<<1))) /* enabled */ {
575 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
577 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
580 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id
.trsEuc
* 2);
582 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
584 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
586 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
588 if (id
.command_set_1
& (1<<0)) {
589 printf("ID_ATA_FEATURE_SET_SMART=1\n");
590 printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<0)) ? 1 : 0);
592 if (id
.command_set_2
& (1<<9)) {
593 printf("ID_ATA_FEATURE_SET_AAM=1\n");
594 printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<9)) ? 1 : 0);
595 printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id
.acoustic
>> 8);
596 printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id
.acoustic
& 0xff);
598 if (id
.command_set_2
& (1<<5)) {
599 printf("ID_ATA_FEATURE_SET_PUIS=1\n");
600 printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<5)) ? 1 : 0);
602 if (id
.command_set_2
& (1<<3)) {
603 printf("ID_ATA_FEATURE_SET_APM=1\n");
604 printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<3)) ? 1 : 0);
605 if ((id
.cfs_enable_2
& (1<<3)))
606 printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id
.CurAPMvalues
& 0xff);
608 if (id
.command_set_2
& (1<<0))
609 printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
612 * Word 76 indicates the capabilities of a SATA device. A PATA device shall set
613 * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then
614 * the device does not claim compliance with the Serial ATA specification and words
615 * 76 through 79 are not valid and shall be ignored.
617 word
= identify_words
[76];
618 if (word
!= 0x0000 && word
!= 0xffff) {
619 printf("ID_ATA_SATA=1\n");
621 * If bit 2 of word 76 is set to one, then the device supports the Gen2
622 * signaling rate of 3.0 Gb/s (see SATA 2.6).
624 * If bit 1 of word 76 is set to one, then the device supports the Gen1
625 * signaling rate of 1.5 Gb/s (see SATA 2.6).
628 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
630 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
633 /* Word 217 indicates the nominal media rotation rate of the device */
634 word
= identify_words
[217];
636 printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
637 else if (word
>= 0x0401 && word
<= 0xfffe)
638 printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word
);
641 * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
642 * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE.
643 * All other values are reserved.
645 word
= identify_words
[108];
646 if ((word
& 0xf000) == 0x5000)
647 printf("ID_WWN=0x%1$"PRIu64
"x\n"
648 "ID_WWN_WITH_EXTENSION=0x%1$"PRIu64
"x\n",
649 identify
.octa
[108/4]);
651 /* from Linux's include/linux/ata.h */
652 if (identify_words
[0] == 0x848a ||
653 identify_words
[0] == 0x844a ||
654 (identify_words
[83] & 0xc004) == 0x4004)
655 printf("ID_ATA_CFA=1\n");
657 if (serial
[0] != '\0')
658 printf("%s_%s\n", model
, serial
);
660 printf("%s\n", model
);