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