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