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