chiark / gitweb /
ata_id: modernize
[elogind.git] / src / udev / ata_id / ata_id.c
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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <getopt.h>
31 #include <scsi/scsi.h>
32 #include <scsi/sg.h>
33 #include <scsi/scsi_ioctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <linux/types.h>
38 #include <linux/hdreg.h>
39 #include <linux/fs.h>
40 #include <linux/cdrom.h>
41 #include <linux/bsg.h>
42 #include <arpa/inet.h>
43
44 #include "libudev.h"
45 #include "libudev-private.h"
46 #include "udev-util.h"
47 #include "log.h"
48
49 #define COMMAND_TIMEOUT_MSEC (30 * 1000)
50
51 static int disk_scsi_inquiry_command(int      fd,
52                                      void    *buf,
53                                      size_t   buf_len)
54 {
55         uint8_t cdb[6] = {
56                 /*
57                  * INQUIRY, see SPC-4 section 6.4
58                  */
59                 [0] = 0x12,                /* OPERATION CODE: INQUIRY */
60                 [3] = (buf_len >> 8),      /* ALLOCATION LENGTH */
61                 [4] = (buf_len & 0xff),
62         };
63         uint8_t sense[32] = {};
64         struct sg_io_v4 io_v4 = {
65                 .guard = 'Q',
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,
75         };
76         int ret;
77
78         ret = ioctl(fd, SG_IO, &io_v4);
79         if (ret != 0) {
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 = {
83                                 .interface_id = 'S',
84                                 .cmdp = (unsigned char*) cdb,
85                                 .cmd_len = sizeof (cdb),
86                                 .dxferp = buf,
87                                 .dxfer_len = buf_len,
88                                 .sbp = sense,
89                                 .mx_sb_len = sizeof(sense),
90                                 .dxfer_direction = SG_DXFER_FROM_DEV,
91                                 .timeout = COMMAND_TIMEOUT_MSEC,
92                         };
93
94                         ret = ioctl(fd, SG_IO, &io_hdr);
95                         if (ret != 0)
96                                 return ret;
97
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)) {
102                                 errno = EIO;
103                                 return -1;
104                         }
105                 } else
106                         return ret;
107         }
108
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)) {
113                 errno = EIO;
114                 return -1;
115         }
116
117         return 0;
118 }
119
120 static int disk_identify_command(int          fd,
121                                  void         *buf,
122                                  size_t          buf_len)
123 {
124         uint8_t cdb[12] = {
125                 /*
126                  * ATA Pass-Through 12 byte command, as described in
127                  *
128                  *  T10 04-262r8 ATA Command Pass-Through
129                  *
130                  * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
131                  */
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 */
142         };
143         uint8_t sense[32] = {};
144         uint8_t *desc = sense + 8;
145         struct sg_io_v4 io_v4 = {
146                 .guard = 'Q',
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,
156         };
157         int ret;
158
159         ret = ioctl(fd, SG_IO, &io_v4);
160         if (ret != 0) {
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 = {
164                                 .interface_id = 'S',
165                                 .cmdp = (unsigned char*) cdb,
166                                 .cmd_len = sizeof (cdb),
167                                 .dxferp = buf,
168                                 .dxfer_len = buf_len,
169                                 .sbp = sense,
170                                 .mx_sb_len = sizeof (sense),
171                                 .dxfer_direction = SG_DXFER_FROM_DEV,
172                                 .timeout = COMMAND_TIMEOUT_MSEC,
173                         };
174
175                         ret = ioctl(fd, SG_IO, &io_hdr);
176                         if (ret != 0)
177                                 return ret;
178                 } else
179                         return ret;
180         }
181
182         if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
183                 errno = EIO;
184                 return -1;
185         }
186
187         return 0;
188 }
189
190 static int disk_identify_packet_device_command(int          fd,
191                                                void         *buf,
192                                                size_t          buf_len)
193 {
194         uint8_t cdb[16] = {
195                 /*
196                  * ATA Pass-Through 16 byte command, as described in
197                  *
198                  *  T10 04-262r8 ATA Command Pass-Through
199                  *
200                  * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
201                  */
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 */
218         };
219         uint8_t sense[32] = {};
220         uint8_t *desc = sense + 8;
221         struct sg_io_v4 io_v4 = {
222                 .guard = 'Q',
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,
232         };
233         int ret;
234
235         ret = ioctl(fd, SG_IO, &io_v4);
236         if (ret != 0) {
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 = {
240                                 .interface_id = 'S',
241                                 .cmdp = (unsigned char*) cdb,
242                                 .cmd_len = sizeof (cdb),
243                                 .dxferp = buf,
244                                 .dxfer_len = buf_len,
245                                 .sbp = sense,
246                                 .mx_sb_len = sizeof (sense),
247                                 .dxfer_direction = SG_DXFER_FROM_DEV,
248                                 .timeout = COMMAND_TIMEOUT_MSEC,
249                         };
250
251                         ret = ioctl(fd, SG_IO, &io_hdr);
252                         if (ret != 0)
253                                 return ret;
254                 } else
255                         return ret;
256         }
257
258         if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
259                 errno = EIO;
260                 return -1;
261         }
262
263         return 0;
264 }
265
266 /**
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.
272  *
273  * Copies the ATA string from @identify located at @offset_words into @dest.
274  */
275 static void disk_identify_get_string(uint8_t identify[512],
276                                      unsigned int offset_words,
277                                      char *dest,
278                                      size_t dest_len)
279 {
280         unsigned int c1;
281         unsigned int c2;
282
283         while (dest_len > 0) {
284                 c1 = identify[offset_words * 2 + 1];
285                 c2 = identify[offset_words * 2];
286                 *dest = c1;
287                 dest++;
288                 *dest = c2;
289                 dest++;
290                 offset_words++;
291                 dest_len -= 2;
292         }
293 }
294
295 static void disk_identify_fixup_string(uint8_t identify[512],
296                                        unsigned int offset_words,
297                                        size_t len)
298 {
299         disk_identify_get_string(identify, offset_words,
300                                  (char *) identify + offset_words * 2, len);
301 }
302
303 static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offset_words)
304 {
305         uint16_t *p;
306
307         p = (uint16_t *) identify;
308         p[offset_words] = le16toh (p[offset_words]);
309 }
310
311 /**
312  * disk_identify:
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.
317  *
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.
321  *
322  * This routine is based on code from libatasmart, Copyright 2008
323  * Lennart Poettering, LGPL v2.1.
324  *
325  * Returns: 0 if the data was successfully obtained, otherwise
326  * non-zero with errno set.
327  */
328 static int disk_identify(struct udev *udev,
329                          int fd,
330                          uint8_t out_identify[512],
331                          int *out_is_packet_device)
332 {
333         int ret;
334         uint8_t inquiry_buf[36];
335         int peripheral_device_type;
336         int all_nul_bytes;
337         int n;
338         int is_packet_device = 0;
339
340         /* init results */
341         memzero(out_identify, 512);
342
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).
346          *
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
351          * emulation layer.
352          *
353          * This also ensures that we're actually dealing with a device
354          * that understands SCSI commands.
355          *
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...)
359          *
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.)
363          */
364         ret = disk_scsi_inquiry_command (fd, inquiry_buf, sizeof (inquiry_buf));
365         if (ret != 0)
366                 goto out;
367
368         /* SPC-4, section 6.4.2: Standard INQUIRY data */
369         peripheral_device_type = inquiry_buf[0] & 0x1f;
370         if (peripheral_device_type == 0x05)
371           {
372             is_packet_device = 1;
373             ret = disk_identify_packet_device_command(fd, out_identify, 512);
374             goto check_nul_bytes;
375           }
376         if (peripheral_device_type != 0x00) {
377                 ret = -1;
378                 errno = EIO;
379                 goto out;
380         }
381
382         /* OK, now issue the IDENTIFY DEVICE command */
383         ret = disk_identify_command(fd, out_identify, 512);
384         if (ret != 0)
385                 goto out;
386
387  check_nul_bytes:
388          /* Check if IDENTIFY data is all NUL bytes - if so, bail */
389         all_nul_bytes = 1;
390         for (n = 0; n < 512; n++) {
391                 if (out_identify[n] != '\0') {
392                         all_nul_bytes = 0;
393                         break;
394                 }
395         }
396
397         if (all_nul_bytes) {
398                 ret = -1;
399                 errno = EIO;
400                 goto out;
401         }
402
403 out:
404         if (out_is_packet_device != NULL)
405                 *out_is_packet_device = is_packet_device;
406         return ret;
407 }
408
409 int main(int argc, char *argv[])
410 {
411         _cleanup_udev_unref_ struct udev *udev = NULL;
412         struct hd_driveid id;
413         union {
414                 uint8_t  byte[512];
415                 uint16_t wyde[256];
416                 uint64_t octa[64];
417         } identify;
418         uint16_t *identify_words;
419         char model[41];
420         char model_enc[256];
421         char serial[21];
422         char revision[9];
423         const char *node = NULL;
424         int export = 0;
425         _cleanup_close_ int fd = -1;
426         uint16_t word;
427         int is_packet_device = 0;
428         static const struct option options[] = {
429                 { "export", no_argument, NULL, 'x' },
430                 { "help", no_argument, NULL, 'h' },
431                 {}
432         };
433
434         log_parse_environment();
435         log_open();
436
437         udev = udev_new();
438         if (udev == NULL)
439                 return 0;
440
441         while (1) {
442                 int option;
443
444                 option = getopt_long(argc, argv, "xh", options, NULL);
445                 if (option == -1)
446                         break;
447
448                 switch (option) {
449                 case 'x':
450                         export = 1;
451                         break;
452                 case 'h':
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");
456                         return 0;
457                 }
458         }
459
460         node = argv[optind];
461         if (node == NULL) {
462                 log_error("no node specified");
463                 return 1;
464         }
465
466         fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
467         if (fd < 0) {
468                 log_error("unable to open '%s'", node);
469                 return 1;
470         }
471
472         if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
473                 /*
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
476                  */
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);
496         } else {
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);
500                         return 2;
501                 }
502         }
503         identify_words = &identify.wyde;
504
505         memcpy (model, id.model, 40);
506         model[40] = '\0';
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);
514
515         if (export) {
516                 /* Set this to convey the disk speaks the ATA protocol */
517                 printf("ID_ATA=1\n");
518
519                 if ((id.config >> 8) & 0x80) {
520                         /* This is an ATAPI device */
521                         switch ((id.config >> 8) & 0x1f) {
522                         case 0:
523                                 printf("ID_TYPE=cd\n");
524                                 break;
525                         case 1:
526                                 printf("ID_TYPE=tape\n");
527                                 break;
528                         case 5:
529                                 printf("ID_TYPE=cd\n");
530                                 break;
531                         case 7:
532                                 printf("ID_TYPE=optical\n");
533                                 break;
534                         default:
535                                 printf("ID_TYPE=generic\n");
536                                 break;
537                         }
538                 } else {
539                         printf("ID_TYPE=disk\n");
540                 }
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);
548                 } else {
549                         printf("ID_SERIAL=%s\n", model);
550                 }
551
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);
555                 }
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);
559
560                         /*
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.
563                          */
564                 }
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);
568                 }
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 */ {
574                                 if (id.dlf & (1<<8))
575                                         printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
576                                 else
577                                         printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
578                         }
579                         if (id.dlf & (1<<5))
580                                 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2);
581                         if (id.dlf & (1<<4))
582                                 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
583                         if (id.dlf & (1<<3))
584                                 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
585                         if (id.dlf & (1<<2))
586                                 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
587                 }
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);
591                 }
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);
597                 }
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);
601                 }
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);
607                 }
608                 if (id.command_set_2 & (1<<0))
609                         printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
610
611                 /*
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.
616                  */
617                 word = identify_words[76];
618                 if (word != 0x0000 && word != 0xffff) {
619                         printf("ID_ATA_SATA=1\n");
620                         /*
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).
623                          *
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).
626                          */
627                         if (word & (1<<2))
628                                 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
629                         if (word & (1<<1))
630                                 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
631                 }
632
633                 /* Word 217 indicates the nominal media rotation rate of the device */
634                 word = identify_words[217];
635                 if (word == 0x0001)
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);
639
640                 /*
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.
644                  */
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]);
650
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");
656         } else {
657                 if (serial[0] != '\0')
658                         printf("%s_%s\n", model, serial);
659                 else
660                         printf("%s\n", model);
661         }
662
663         return 0;
664 }