chiark / gitweb /
ata_id: remove temp variable to kill warning
[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         char model[41];
419         char model_enc[256];
420         char serial[21];
421         char revision[9];
422         const char *node = NULL;
423         int export = 0;
424         _cleanup_close_ int fd = -1;
425         uint16_t word;
426         int is_packet_device = 0;
427         static const struct option options[] = {
428                 { "export", no_argument, NULL, 'x' },
429                 { "help", no_argument, NULL, 'h' },
430                 {}
431         };
432
433         log_parse_environment();
434         log_open();
435
436         udev = udev_new();
437         if (udev == NULL)
438                 return 0;
439
440         while (1) {
441                 int option;
442
443                 option = getopt_long(argc, argv, "xh", options, NULL);
444                 if (option == -1)
445                         break;
446
447                 switch (option) {
448                 case 'x':
449                         export = 1;
450                         break;
451                 case 'h':
452                         printf("Usage: ata_id [--export] [--help] <device>\n"
453                                "  --export    print values as environment keys\n"
454                                "  --help      print this help text\n\n");
455                         return 0;
456                 }
457         }
458
459         node = argv[optind];
460         if (node == NULL) {
461                 log_error("no node specified");
462                 return 1;
463         }
464
465         fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
466         if (fd < 0) {
467                 log_error("unable to open '%s'", node);
468                 return 1;
469         }
470
471         if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
472                 /*
473                  * fix up only the fields from the IDENTIFY data that we are going to
474                  * use and copy it into the hd_driveid struct for convenience
475                  */
476                 disk_identify_fixup_string(identify.byte,  10, 20); /* serial */
477                 disk_identify_fixup_string(identify.byte,  23,  8); /* fwrev */
478                 disk_identify_fixup_string(identify.byte,  27, 40); /* model */
479                 disk_identify_fixup_uint16(identify.byte,  0);      /* configuration */
480                 disk_identify_fixup_uint16(identify.byte,  75);     /* queue depth */
481                 disk_identify_fixup_uint16(identify.byte,  75);     /* SATA capabilities */
482                 disk_identify_fixup_uint16(identify.byte,  82);     /* command set supported */
483                 disk_identify_fixup_uint16(identify.byte,  83);     /* command set supported */
484                 disk_identify_fixup_uint16(identify.byte,  84);     /* command set supported */
485                 disk_identify_fixup_uint16(identify.byte,  85);     /* command set supported */
486                 disk_identify_fixup_uint16(identify.byte,  86);     /* command set supported */
487                 disk_identify_fixup_uint16(identify.byte,  87);     /* command set supported */
488                 disk_identify_fixup_uint16(identify.byte,  89);     /* time required for SECURITY ERASE UNIT */
489                 disk_identify_fixup_uint16(identify.byte,  90);     /* time required for enhanced SECURITY ERASE UNIT */
490                 disk_identify_fixup_uint16(identify.byte,  91);     /* current APM values */
491                 disk_identify_fixup_uint16(identify.byte,  94);     /* current AAM value */
492                 disk_identify_fixup_uint16(identify.byte, 128);     /* device lock function */
493                 disk_identify_fixup_uint16(identify.byte, 217);     /* nominal media rotation rate */
494                 memcpy(&id, identify.byte, sizeof id);
495         } else {
496                 /* If this fails, then try HDIO_GET_IDENTITY */
497                 if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) {
498                         log_debug_errno(errno, "HDIO_GET_IDENTITY failed for '%s': %m", node);
499                         return 2;
500                 }
501         }
502
503         memcpy (model, id.model, 40);
504         model[40] = '\0';
505         udev_util_encode_string(model, model_enc, sizeof(model_enc));
506         util_replace_whitespace((char *) id.model, model, 40);
507         util_replace_chars(model, NULL);
508         util_replace_whitespace((char *) id.serial_no, serial, 20);
509         util_replace_chars(serial, NULL);
510         util_replace_whitespace((char *) id.fw_rev, revision, 8);
511         util_replace_chars(revision, NULL);
512
513         if (export) {
514                 /* Set this to convey the disk speaks the ATA protocol */
515                 printf("ID_ATA=1\n");
516
517                 if ((id.config >> 8) & 0x80) {
518                         /* This is an ATAPI device */
519                         switch ((id.config >> 8) & 0x1f) {
520                         case 0:
521                                 printf("ID_TYPE=cd\n");
522                                 break;
523                         case 1:
524                                 printf("ID_TYPE=tape\n");
525                                 break;
526                         case 5:
527                                 printf("ID_TYPE=cd\n");
528                                 break;
529                         case 7:
530                                 printf("ID_TYPE=optical\n");
531                                 break;
532                         default:
533                                 printf("ID_TYPE=generic\n");
534                                 break;
535                         }
536                 } else {
537                         printf("ID_TYPE=disk\n");
538                 }
539                 printf("ID_BUS=ata\n");
540                 printf("ID_MODEL=%s\n", model);
541                 printf("ID_MODEL_ENC=%s\n", model_enc);
542                 printf("ID_REVISION=%s\n", revision);
543                 if (serial[0] != '\0') {
544                         printf("ID_SERIAL=%s_%s\n", model, serial);
545                         printf("ID_SERIAL_SHORT=%s\n", serial);
546                 } else {
547                         printf("ID_SERIAL=%s\n", model);
548                 }
549
550                 if (id.command_set_1 & (1<<5)) {
551                         printf("ID_ATA_WRITE_CACHE=1\n");
552                         printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0);
553                 }
554                 if (id.command_set_1 & (1<<10)) {
555                         printf("ID_ATA_FEATURE_SET_HPA=1\n");
556                         printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id.cfs_enable_1 & (1<<10)) ? 1 : 0);
557
558                         /*
559                          * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address
560                          * so it is easy to check whether the protected area is in use.
561                          */
562                 }
563                 if (id.command_set_1 & (1<<3)) {
564                         printf("ID_ATA_FEATURE_SET_PM=1\n");
565                         printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id.cfs_enable_1 & (1<<3)) ? 1 : 0);
566                 }
567                 if (id.command_set_1 & (1<<1)) {
568                         printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
569                         printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id.cfs_enable_1 & (1<<1)) ? 1 : 0);
570                         printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id.trseuc * 2);
571                         if ((id.cfs_enable_1 & (1<<1))) /* enabled */ {
572                                 if (id.dlf & (1<<8))
573                                         printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
574                                 else
575                                         printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
576                         }
577                         if (id.dlf & (1<<5))
578                                 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2);
579                         if (id.dlf & (1<<4))
580                                 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
581                         if (id.dlf & (1<<3))
582                                 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
583                         if (id.dlf & (1<<2))
584                                 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
585                 }
586                 if (id.command_set_1 & (1<<0)) {
587                         printf("ID_ATA_FEATURE_SET_SMART=1\n");
588                         printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id.cfs_enable_1 & (1<<0)) ? 1 : 0);
589                 }
590                 if (id.command_set_2 & (1<<9)) {
591                         printf("ID_ATA_FEATURE_SET_AAM=1\n");
592                         printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<9)) ? 1 : 0);
593                         printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id.acoustic >> 8);
594                         printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id.acoustic & 0xff);
595                 }
596                 if (id.command_set_2 & (1<<5)) {
597                         printf("ID_ATA_FEATURE_SET_PUIS=1\n");
598                         printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id.cfs_enable_2 & (1<<5)) ? 1 : 0);
599                 }
600                 if (id.command_set_2 & (1<<3)) {
601                         printf("ID_ATA_FEATURE_SET_APM=1\n");
602                         printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<3)) ? 1 : 0);
603                         if ((id.cfs_enable_2 & (1<<3)))
604                                 printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id.CurAPMvalues & 0xff);
605                 }
606                 if (id.command_set_2 & (1<<0))
607                         printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
608
609                 /*
610                  * Word 76 indicates the capabilities of a SATA device. A PATA device shall set
611                  * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then
612                  * the device does not claim compliance with the Serial ATA specification and words
613                  * 76 through 79 are not valid and shall be ignored.
614                  */
615
616                 word = identify.wyde[76];
617                 if (word != 0x0000 && word != 0xffff) {
618                         printf("ID_ATA_SATA=1\n");
619                         /*
620                          * If bit 2 of word 76 is set to one, then the device supports the Gen2
621                          * signaling rate of 3.0 Gb/s (see SATA 2.6).
622                          *
623                          * If bit 1 of word 76 is set to one, then the device supports the Gen1
624                          * signaling rate of 1.5 Gb/s (see SATA 2.6).
625                          */
626                         if (word & (1<<2))
627                                 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
628                         if (word & (1<<1))
629                                 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
630                 }
631
632                 /* Word 217 indicates the nominal media rotation rate of the device */
633                 word = identify.wyde[217];
634                 if (word == 0x0001)
635                         printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
636                 else if (word >= 0x0401 && word <= 0xfffe)
637                         printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
638
639                 /*
640                  * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
641                  * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE.
642                  * All other values are reserved.
643                  */
644                 word = identify.wyde[108];
645                 if ((word & 0xf000) == 0x5000)
646                         printf("ID_WWN=0x%1$"PRIu64"x\n"
647                                "ID_WWN_WITH_EXTENSION=0x%1$"PRIu64"x\n",
648                                identify.octa[108/4]);
649
650                 /* from Linux's include/linux/ata.h */
651                 if (identify.wyde[0] == 0x848a ||
652                     identify.wyde[0] == 0x844a ||
653                     (identify.wyde[83] & 0xc004) == 0x4004)
654                         printf("ID_ATA_CFA=1\n");
655         } else {
656                 if (serial[0] != '\0')
657                         printf("%s_%s\n", model, serial);
658                 else
659                         printf("%s\n", model);
660         }
661
662         return 0;
663 }