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