1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * ata_id - reads product/serial number from ATA drives
5 * Copyright © 2005-2008 Kay Sievers <kay@vrfy.org>
6 * Copyright © 2009-2010 David Zeuthen <zeuthen@gmail.com>
14 #include <linux/bsg.h>
15 #include <linux/hdreg.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_ioctl.h>
23 #include <sys/ioctl.h>
25 #include <sys/types.h>
31 #include "libudev-private.h"
33 #include "udev-util.h"
35 #define COMMAND_TIMEOUT_MSEC (30 * 1000)
37 static int disk_scsi_inquiry_command(int fd
,
43 * INQUIRY, see SPC-4 section 6.4
45 [0] = 0x12, /* OPERATION CODE: INQUIRY */
46 [3] = (buf_len
>> 8), /* ALLOCATION LENGTH */
47 [4] = (buf_len
& 0xff),
49 uint8_t sense
[32] = {};
50 struct sg_io_v4 io_v4
= {
52 .protocol
= BSG_PROTOCOL_SCSI
,
53 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
54 .request_len
= sizeof(cdb
),
55 .request
= (uintptr_t) cdb
,
56 .max_response_len
= sizeof(sense
),
57 .response
= (uintptr_t) sense
,
58 .din_xfer_len
= buf_len
,
59 .din_xferp
= (uintptr_t) buf
,
60 .timeout
= COMMAND_TIMEOUT_MSEC
,
64 ret
= ioctl(fd
, SG_IO
, &io_v4
);
66 /* could be that the driver doesn't do version 4, try version 3 */
67 if (errno
== EINVAL
) {
68 struct sg_io_hdr io_hdr
= {
70 .cmdp
= (unsigned char*) cdb
,
71 .cmd_len
= sizeof (cdb
),
75 .mx_sb_len
= sizeof(sense
),
76 .dxfer_direction
= SG_DXFER_FROM_DEV
,
77 .timeout
= COMMAND_TIMEOUT_MSEC
,
80 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
84 /* even if the ioctl succeeds, we need to check the return value */
85 if (!(io_hdr
.status
== 0 &&
86 io_hdr
.host_status
== 0 &&
87 io_hdr
.driver_status
== 0)) {
95 /* even if the ioctl succeeds, we need to check the return value */
96 if (!(io_v4
.device_status
== 0 &&
97 io_v4
.transport_status
== 0 &&
98 io_v4
.driver_status
== 0)) {
106 static int disk_identify_command(int fd
,
112 * ATA Pass-Through 12 byte command, as described in
114 * T10 04-262r8 ATA Command Pass-Through
116 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
118 [0] = 0xa1, /* OPERATION CODE: 12 byte pass through */
119 [1] = 4 << 1, /* PROTOCOL: PIO Data-in */
120 [2] = 0x2e, /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
121 [3] = 0, /* FEATURES */
122 [4] = 1, /* SECTORS */
123 [5] = 0, /* LBA LOW */
124 [6] = 0, /* LBA MID */
125 [7] = 0, /* LBA HIGH */
126 [8] = 0 & 0x4F, /* SELECT */
127 [9] = 0xEC, /* Command: ATA IDENTIFY DEVICE */
129 uint8_t sense
[32] = {};
130 uint8_t *desc
= sense
+ 8;
131 struct sg_io_v4 io_v4
= {
133 .protocol
= BSG_PROTOCOL_SCSI
,
134 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
135 .request_len
= sizeof(cdb
),
136 .request
= (uintptr_t) cdb
,
137 .max_response_len
= sizeof(sense
),
138 .response
= (uintptr_t) sense
,
139 .din_xfer_len
= buf_len
,
140 .din_xferp
= (uintptr_t) buf
,
141 .timeout
= COMMAND_TIMEOUT_MSEC
,
145 ret
= ioctl(fd
, SG_IO
, &io_v4
);
147 /* could be that the driver doesn't do version 4, try version 3 */
148 if (errno
== EINVAL
) {
149 struct sg_io_hdr io_hdr
= {
151 .cmdp
= (unsigned char*) cdb
,
152 .cmd_len
= sizeof (cdb
),
154 .dxfer_len
= buf_len
,
156 .mx_sb_len
= sizeof (sense
),
157 .dxfer_direction
= SG_DXFER_FROM_DEV
,
158 .timeout
= COMMAND_TIMEOUT_MSEC
,
161 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
168 if (!(sense
[0] == 0x72 && desc
[0] == 0x9 && desc
[1] == 0x0c)) {
176 static int disk_identify_packet_device_command(int fd
,
182 * ATA Pass-Through 16 byte command, as described in
184 * T10 04-262r8 ATA Command Pass-Through
186 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
188 [0] = 0x85, /* OPERATION CODE: 16 byte pass through */
189 [1] = 4 << 1, /* PROTOCOL: PIO Data-in */
190 [2] = 0x2e, /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
191 [3] = 0, /* FEATURES */
192 [4] = 0, /* FEATURES */
193 [5] = 0, /* SECTORS */
194 [6] = 1, /* SECTORS */
195 [7] = 0, /* LBA LOW */
196 [8] = 0, /* LBA LOW */
197 [9] = 0, /* LBA MID */
198 [10] = 0, /* LBA MID */
199 [11] = 0, /* LBA HIGH */
200 [12] = 0, /* LBA HIGH */
201 [13] = 0, /* DEVICE */
202 [14] = 0xA1, /* Command: ATA IDENTIFY PACKET DEVICE */
203 [15] = 0, /* CONTROL */
205 uint8_t sense
[32] = {};
206 uint8_t *desc
= sense
+ 8;
207 struct sg_io_v4 io_v4
= {
209 .protocol
= BSG_PROTOCOL_SCSI
,
210 .subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
,
211 .request_len
= sizeof (cdb
),
212 .request
= (uintptr_t) cdb
,
213 .max_response_len
= sizeof (sense
),
214 .response
= (uintptr_t) sense
,
215 .din_xfer_len
= buf_len
,
216 .din_xferp
= (uintptr_t) buf
,
217 .timeout
= COMMAND_TIMEOUT_MSEC
,
221 ret
= ioctl(fd
, SG_IO
, &io_v4
);
223 /* could be that the driver doesn't do version 4, try version 3 */
224 if (errno
== EINVAL
) {
225 struct sg_io_hdr io_hdr
= {
227 .cmdp
= (unsigned char*) cdb
,
228 .cmd_len
= sizeof (cdb
),
230 .dxfer_len
= buf_len
,
232 .mx_sb_len
= sizeof (sense
),
233 .dxfer_direction
= SG_DXFER_FROM_DEV
,
234 .timeout
= COMMAND_TIMEOUT_MSEC
,
237 ret
= ioctl(fd
, SG_IO
, &io_hdr
);
244 if (!(sense
[0] == 0x72 && desc
[0] == 0x9 && desc
[1] == 0x0c)) {
253 * disk_identify_get_string:
254 * @identify: A block of IDENTIFY data
255 * @offset_words: Offset of the string to get, in words.
256 * @dest: Destination buffer for the string.
257 * @dest_len: Length of destination buffer, in bytes.
259 * Copies the ATA string from @identify located at @offset_words into @dest.
261 static void disk_identify_get_string(uint8_t identify
[512],
262 unsigned int offset_words
,
269 while (dest_len
> 0) {
270 c1
= identify
[offset_words
* 2 + 1];
271 c2
= identify
[offset_words
* 2];
281 static void disk_identify_fixup_string(uint8_t identify
[512],
282 unsigned int offset_words
,
285 disk_identify_get_string(identify
, offset_words
,
286 (char *) identify
+ offset_words
* 2, len
);
289 static void disk_identify_fixup_uint16 (uint8_t identify
[512], unsigned int offset_words
)
293 p
= (uint16_t *) identify
;
294 p
[offset_words
] = le16toh (p
[offset_words
]);
299 * @udev: The libudev context.
300 * @fd: File descriptor for the block device.
301 * @out_identify: Return location for IDENTIFY data.
302 * @out_is_packet_device: Return location for whether returned data is from a IDENTIFY PACKET DEVICE.
304 * Sends the IDENTIFY DEVICE or IDENTIFY PACKET DEVICE command to the
305 * device represented by @fd. If successful, then the result will be
306 * copied into @out_identify and @out_is_packet_device.
308 * This routine is based on code from libatasmart, Copyright © 2008
309 * Lennart Poettering, LGPL v2.1.
311 * Returns: 0 if the data was successfully obtained, otherwise
312 * non-zero with errno set.
314 static int disk_identify(struct udev
*udev
,
316 uint8_t out_identify
[512],
317 int *out_is_packet_device
)
320 uint8_t inquiry_buf
[36];
321 int peripheral_device_type
;
324 int is_packet_device
= 0;
327 memzero(out_identify
, 512);
329 /* If we were to use ATA PASS_THROUGH (12) on an ATAPI device
330 * we could accidentally blank media. This is because MMC's BLANK
331 * command has the same op-code (0x61).
333 * To prevent this from happening we bail out if the device
334 * isn't a Direct Access Block Device, e.g. SCSI type 0x00
335 * (CD/DVD devices are type 0x05). So we send a SCSI INQUIRY
336 * command first... libata is handling this via its SCSI
339 * This also ensures that we're actually dealing with a device
340 * that understands SCSI commands.
342 * (Yes, it is a bit perverse that we're tunneling the ATA
343 * command through SCSI and relying on the ATA driver
344 * emulating SCSI well-enough...)
346 * (See commit 160b069c25690bfb0c785994c7c3710289179107 for
347 * the original bug-fix and see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=556635
348 * for the original bug-report.)
350 ret
= disk_scsi_inquiry_command (fd
, inquiry_buf
, sizeof (inquiry_buf
));
354 /* SPC-4, section 6.4.2: Standard INQUIRY data */
355 peripheral_device_type
= inquiry_buf
[0] & 0x1f;
356 if (peripheral_device_type
== 0x05)
358 is_packet_device
= 1;
359 ret
= disk_identify_packet_device_command(fd
, out_identify
, 512);
360 goto check_nul_bytes
;
362 if (peripheral_device_type
!= 0x00) {
368 /* OK, now issue the IDENTIFY DEVICE command */
369 ret
= disk_identify_command(fd
, out_identify
, 512);
374 /* Check if IDENTIFY data is all NUL bytes - if so, bail */
376 for (n
= 0; n
< 512; n
++) {
377 if (out_identify
[n
] != '\0') {
390 if (out_is_packet_device
!= NULL
)
391 *out_is_packet_device
= is_packet_device
;
395 int main(int argc
, char *argv
[]) {
396 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
397 struct hd_driveid id
;
406 const char *node
= NULL
;
408 _cleanup_close_
int fd
= -1;
410 int is_packet_device
= 0;
411 static const struct option options
[] = {
412 { "export", no_argument
, NULL
, 'x' },
413 { "help", no_argument
, NULL
, 'h' },
417 log_set_target(LOG_TARGET_AUTO
);
419 log_parse_environment();
429 option
= getopt_long(argc
, argv
, "xh", options
, NULL
);
438 printf("Usage: ata_id [--export] [--help] <device>\n"
439 " -x,--export print values as environment keys\n"
440 " -h,--help print this help text\n\n");
447 log_error("no node specified");
451 fd
= open(node
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
);
453 log_error("unable to open '%s'", node
);
457 if (disk_identify(udev
, fd
, identify
.byte
, &is_packet_device
) == 0) {
459 * fix up only the fields from the IDENTIFY data that we are going to
460 * use and copy it into the hd_driveid struct for convenience
462 disk_identify_fixup_string(identify
.byte
, 10, 20); /* serial */
463 disk_identify_fixup_string(identify
.byte
, 23, 8); /* fwrev */
464 disk_identify_fixup_string(identify
.byte
, 27, 40); /* model */
465 disk_identify_fixup_uint16(identify
.byte
, 0); /* configuration */
466 disk_identify_fixup_uint16(identify
.byte
, 75); /* queue depth */
467 disk_identify_fixup_uint16(identify
.byte
, 76); /* SATA capabilities */
468 disk_identify_fixup_uint16(identify
.byte
, 82); /* command set supported */
469 disk_identify_fixup_uint16(identify
.byte
, 83); /* command set supported */
470 disk_identify_fixup_uint16(identify
.byte
, 84); /* command set supported */
471 disk_identify_fixup_uint16(identify
.byte
, 85); /* command set supported */
472 disk_identify_fixup_uint16(identify
.byte
, 86); /* command set supported */
473 disk_identify_fixup_uint16(identify
.byte
, 87); /* command set supported */
474 disk_identify_fixup_uint16(identify
.byte
, 89); /* time required for SECURITY ERASE UNIT */
475 disk_identify_fixup_uint16(identify
.byte
, 90); /* time required for enhanced SECURITY ERASE UNIT */
476 disk_identify_fixup_uint16(identify
.byte
, 91); /* current APM values */
477 disk_identify_fixup_uint16(identify
.byte
, 94); /* current AAM value */
478 disk_identify_fixup_uint16(identify
.byte
, 108); /* WWN */
479 disk_identify_fixup_uint16(identify
.byte
, 109); /* WWN */
480 disk_identify_fixup_uint16(identify
.byte
, 110); /* WWN */
481 disk_identify_fixup_uint16(identify
.byte
, 111); /* WWN */
482 disk_identify_fixup_uint16(identify
.byte
, 128); /* device lock function */
483 disk_identify_fixup_uint16(identify
.byte
, 217); /* nominal media rotation rate */
484 memcpy(&id
, identify
.byte
, sizeof id
);
486 /* If this fails, then try HDIO_GET_IDENTITY */
487 if (ioctl(fd
, HDIO_GET_IDENTITY
, &id
) != 0) {
488 log_debug_errno(errno
, "HDIO_GET_IDENTITY failed for '%s': %m", node
);
493 memcpy(model
, id
.model
, 40);
495 udev_util_encode_string(model
, model_enc
, sizeof(model_enc
));
496 util_replace_whitespace((char *) id
.model
, model
, 40);
497 util_replace_chars(model
, NULL
);
498 util_replace_whitespace((char *) id
.serial_no
, serial
, 20);
499 util_replace_chars(serial
, NULL
);
500 util_replace_whitespace((char *) id
.fw_rev
, revision
, 8);
501 util_replace_chars(revision
, NULL
);
504 /* Set this to convey the disk speaks the ATA protocol */
505 printf("ID_ATA=1\n");
507 if ((id
.config
>> 8) & 0x80) {
508 /* This is an ATAPI device */
509 switch ((id
.config
>> 8) & 0x1f) {
511 printf("ID_TYPE=cd\n");
514 printf("ID_TYPE=tape\n");
517 printf("ID_TYPE=cd\n");
520 printf("ID_TYPE=optical\n");
523 printf("ID_TYPE=generic\n");
527 printf("ID_TYPE=disk\n");
529 printf("ID_BUS=ata\n");
530 printf("ID_MODEL=%s\n", model
);
531 printf("ID_MODEL_ENC=%s\n", model_enc
);
532 printf("ID_REVISION=%s\n", revision
);
533 if (serial
[0] != '\0') {
534 printf("ID_SERIAL=%s_%s\n", model
, serial
);
535 printf("ID_SERIAL_SHORT=%s\n", serial
);
537 printf("ID_SERIAL=%s\n", model
);
540 if (id
.command_set_1
& (1<<5)) {
541 printf("ID_ATA_WRITE_CACHE=1\n");
542 printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<5)) ? 1 : 0);
544 if (id
.command_set_1
& (1<<10)) {
545 printf("ID_ATA_FEATURE_SET_HPA=1\n");
546 printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<10)) ? 1 : 0);
549 * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address
550 * so it is easy to check whether the protected area is in use.
553 if (id
.command_set_1
& (1<<3)) {
554 printf("ID_ATA_FEATURE_SET_PM=1\n");
555 printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<3)) ? 1 : 0);
557 if (id
.command_set_1
& (1<<1)) {
558 printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
559 printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<1)) ? 1 : 0);
560 printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id
.trseuc
* 2);
561 if ((id
.cfs_enable_1
& (1<<1))) /* enabled */ {
563 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
565 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
568 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id
.trsEuc
* 2);
570 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
572 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
574 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
576 if (id
.command_set_1
& (1<<0)) {
577 printf("ID_ATA_FEATURE_SET_SMART=1\n");
578 printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id
.cfs_enable_1
& (1<<0)) ? 1 : 0);
580 if (id
.command_set_2
& (1<<9)) {
581 printf("ID_ATA_FEATURE_SET_AAM=1\n");
582 printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<9)) ? 1 : 0);
583 printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id
.acoustic
>> 8);
584 printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id
.acoustic
& 0xff);
586 if (id
.command_set_2
& (1<<5)) {
587 printf("ID_ATA_FEATURE_SET_PUIS=1\n");
588 printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<5)) ? 1 : 0);
590 if (id
.command_set_2
& (1<<3)) {
591 printf("ID_ATA_FEATURE_SET_APM=1\n");
592 printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id
.cfs_enable_2
& (1<<3)) ? 1 : 0);
593 if ((id
.cfs_enable_2
& (1<<3)))
594 printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id
.CurAPMvalues
& 0xff);
596 if (id
.command_set_2
& (1<<0))
597 printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
600 * Word 76 indicates the capabilities of a SATA device. A PATA device shall set
601 * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then
602 * the device does not claim compliance with the Serial ATA specification and words
603 * 76 through 79 are not valid and shall be ignored.
606 word
= identify
.wyde
[76];
607 if (!IN_SET(word
, 0x0000, 0xffff)) {
608 printf("ID_ATA_SATA=1\n");
610 * If bit 2 of word 76 is set to one, then the device supports the Gen2
611 * signaling rate of 3.0 Gb/s (see SATA 2.6).
613 * If bit 1 of word 76 is set to one, then the device supports the Gen1
614 * signaling rate of 1.5 Gb/s (see SATA 2.6).
617 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
619 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
622 /* Word 217 indicates the nominal media rotation rate of the device */
623 word
= identify
.wyde
[217];
625 printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
626 else if (word
>= 0x0401 && word
<= 0xfffe)
627 printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word
);
630 * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
631 * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE.
632 * All other values are reserved.
634 word
= identify
.wyde
[108];
635 if ((word
& 0xf000) == 0x5000) {
638 wwwn
= identify
.wyde
[108];
640 wwwn
|= identify
.wyde
[109];
642 wwwn
|= identify
.wyde
[110];
644 wwwn
|= identify
.wyde
[111];
645 printf("ID_WWN=0x%1$" PRIx64
"\n"
646 "ID_WWN_WITH_EXTENSION=0x%1$" PRIx64
"\n",
650 /* from Linux's include/linux/ata.h */
651 if (IN_SET(identify
.wyde
[0], 0x848a, 0x844a) ||
652 (identify
.wyde
[83] & 0xc004) == 0x4004)
653 printf("ID_ATA_CFA=1\n");
655 if (serial
[0] != '\0')
656 printf("%s_%s\n", model
, serial
);
658 printf("%s\n", model
);