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