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