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