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