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