]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/ata_id/ata_id.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / udev / ata_id / ata_id.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
e0f83fbe 2/*
670e4705
KS
3 * ata_id - reads product/serial number from ATA drives
4 *
810adae9 5 * Copyright © 2005-2008 Kay Sievers <kay@vrfy.org>
810adae9 6 * Copyright © 2009-2010 David Zeuthen <zeuthen@gmail.com>
670e4705 7 *
55e9959b
KS
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/>.
670e4705
KS
20 */
21
670e4705
KS
22#include <ctype.h>
23#include <errno.h>
07630cea 24#include <fcntl.h>
4fa3d99d 25#include <getopt.h>
07630cea
LP
26#include <linux/bsg.h>
27#include <linux/hdreg.h>
66094a4a 28#include <scsi/scsi.h>
66094a4a 29#include <scsi/scsi_ioctl.h>
07630cea
LP
30#include <scsi/sg.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
670e4705 35#include <sys/ioctl.h>
670e4705 36#include <sys/stat.h>
07630cea
LP
37#include <sys/types.h>
38#include <unistd.h>
670e4705 39
b4bbcaa9
TA
40#include "libudev.h"
41
3ffd4af2 42#include "fd-util.h"
9060b066 43#include "libudev-private.h"
baa30fbc 44#include "log.h"
07630cea 45#include "udev-util.h"
670e4705 46
6992637e 47#define COMMAND_TIMEOUT_MSEC (30 * 1000)
66094a4a 48
6992637e 49static int disk_scsi_inquiry_command(int fd,
912541b0
KS
50 void *buf,
51 size_t buf_len)
66094a4a 52{
b8a2b0f7
ZJS
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 };
912541b0
KS
74 int ret;
75
912541b0
KS
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) {
b8a2b0f7
ZJS
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 };
912541b0
KS
91
92 ret = ioctl(fd, SG_IO, &io_hdr);
93 if (ret != 0)
b8a2b0f7 94 return ret;
912541b0
KS
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;
b8a2b0f7 101 return -1;
912541b0 102 }
b8a2b0f7
ZJS
103 } else
104 return ret;
912541b0
KS
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;
b8a2b0f7 112 return -1;
912541b0 113 }
6992637e 114
b8a2b0f7 115 return 0;
66094a4a
DZ
116}
117
912541b0
KS
118static int disk_identify_command(int fd,
119 void *buf,
120 size_t buf_len)
66094a4a 121{
b8a2b0f7
ZJS
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 };
912541b0
KS
155 int ret;
156
912541b0
KS
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) {
b8a2b0f7
ZJS
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 };
912541b0
KS
172
173 ret = ioctl(fd, SG_IO, &io_hdr);
174 if (ret != 0)
b8a2b0f7
ZJS
175 return ret;
176 } else
177 return ret;
912541b0
KS
178 }
179
180 if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
181 errno = EIO;
b8a2b0f7 182 return -1;
912541b0 183 }
66094a4a 184
b8a2b0f7 185 return 0;
66094a4a
DZ
186}
187
912541b0
KS
188static int disk_identify_packet_device_command(int fd,
189 void *buf,
190 size_t buf_len)
560de575 191{
b8a2b0f7
ZJS
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 };
912541b0
KS
231 int ret;
232
912541b0
KS
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) {
b8a2b0f7
ZJS
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 };
912541b0
KS
248
249 ret = ioctl(fd, SG_IO, &io_hdr);
250 if (ret != 0)
b8a2b0f7
ZJS
251 return ret;
252 } else
253 return ret;
912541b0
KS
254 }
255
256 if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
257 errno = EIO;
b8a2b0f7 258 return -1;
912541b0 259 }
560de575 260
b8a2b0f7 261 return 0;
560de575
DZ
262}
263
66094a4a
DZ
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 */
09db3f5b
KS
273static void disk_identify_get_string(uint8_t identify[512],
274 unsigned int offset_words,
275 char *dest,
276 size_t dest_len)
66094a4a 277{
912541b0
KS
278 unsigned int c1;
279 unsigned int c2;
280
912541b0 281 while (dest_len > 0) {
2b2823b4
AS
282 c1 = identify[offset_words * 2 + 1];
283 c2 = identify[offset_words * 2];
912541b0
KS
284 *dest = c1;
285 dest++;
286 *dest = c2;
287 dest++;
288 offset_words++;
289 dest_len -= 2;
290 }
66094a4a
DZ
291}
292
09db3f5b
KS
293static void disk_identify_fixup_string(uint8_t identify[512],
294 unsigned int offset_words,
295 size_t len)
66094a4a 296{
912541b0
KS
297 disk_identify_get_string(identify, offset_words,
298 (char *) identify + offset_words * 2, len);
66094a4a
DZ
299}
300
301static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offset_words)
302{
912541b0 303 uint16_t *p;
66094a4a 304
912541b0
KS
305 p = (uint16_t *) identify;
306 p[offset_words] = le16toh (p[offset_words]);
66094a4a
DZ
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.
560de575 314 * @out_is_packet_device: Return location for whether returned data is from a IDENTIFY PACKET DEVICE.
66094a4a 315 *
560de575
DZ
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.
66094a4a 319 *
96b2fb93 320 * This routine is based on code from libatasmart, Copyright © 2008
66094a4a
DZ
321 * Lennart Poettering, LGPL v2.1.
322 *
560de575
DZ
323 * Returns: 0 if the data was successfully obtained, otherwise
324 * non-zero with errno set.
66094a4a 325 */
09db3f5b 326static int disk_identify(struct udev *udev,
b8a2b0f7
ZJS
327 int fd,
328 uint8_t out_identify[512],
329 int *out_is_packet_device)
66094a4a 330{
912541b0
KS
331 int ret;
332 uint8_t inquiry_buf[36];
333 int peripheral_device_type;
334 int all_nul_bytes;
335 int n;
1e8e0a32 336 int is_packet_device = 0;
912541b0 337
912541b0 338 /* init results */
1e8e0a32 339 memzero(out_identify, 512);
912541b0
KS
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;
66094a4a 384
560de575 385 check_nul_bytes:
912541b0
KS
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 }
66094a4a 400
6992637e 401out:
912541b0 402 if (out_is_packet_device != NULL)
1e8e0a32 403 *out_is_packet_device = is_packet_device;
912541b0 404 return ret;
66094a4a
DZ
405}
406
8e766630
LP
407int main(int argc, char *argv[]) {
408 _cleanup_(udev_unrefp) struct udev *udev = NULL;
912541b0 409 struct hd_driveid id;
6024a6e3
SL
410 union {
411 uint8_t byte[512];
412 uint16_t wyde[256];
6024a6e3 413 } identify;
912541b0
KS
414 char model[41];
415 char model_enc[256];
416 char serial[21];
417 char revision[9];
418 const char *node = NULL;
419 int export = 0;
11c6f693 420 _cleanup_close_ int fd = -1;
912541b0 421 uint16_t word;
912541b0
KS
422 int is_packet_device = 0;
423 static const struct option options[] = {
424 { "export", no_argument, NULL, 'x' },
425 { "help", no_argument, NULL, 'h' },
426 {}
427 };
428
b237a168
ZJS
429 log_set_target(LOG_TARGET_AUTO);
430 udev_parse_config();
5168f84a
LP
431 log_parse_environment();
432 log_open();
433
912541b0
KS
434 udev = udev_new();
435 if (udev == NULL)
11c6f693 436 return 0;
912541b0 437
57255510 438 for (;;) {
912541b0
KS
439 int option;
440
441 option = getopt_long(argc, argv, "xh", options, NULL);
442 if (option == -1)
443 break;
444
445 switch (option) {
446 case 'x':
447 export = 1;
448 break;
449 case 'h':
450 printf("Usage: ata_id [--export] [--help] <device>\n"
257e968d
RM
451 " -x,--export print values as environment keys\n"
452 " -h,--help print this help text\n\n");
11c6f693 453 return 0;
912541b0
KS
454 }
455 }
456
457 node = argv[optind];
458 if (node == NULL) {
9f6445e3 459 log_error("no node specified");
11c6f693 460 return 1;
912541b0
KS
461 }
462
c8a202b7 463 fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
912541b0 464 if (fd < 0) {
9f6445e3 465 log_error("unable to open '%s'", node);
11c6f693 466 return 1;
912541b0
KS
467 }
468
6024a6e3 469 if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
912541b0
KS
470 /*
471 * fix up only the fields from the IDENTIFY data that we are going to
472 * use and copy it into the hd_driveid struct for convenience
473 */
6024a6e3
SL
474 disk_identify_fixup_string(identify.byte, 10, 20); /* serial */
475 disk_identify_fixup_string(identify.byte, 23, 8); /* fwrev */
476 disk_identify_fixup_string(identify.byte, 27, 40); /* model */
477 disk_identify_fixup_uint16(identify.byte, 0); /* configuration */
478 disk_identify_fixup_uint16(identify.byte, 75); /* queue depth */
a6affd88 479 disk_identify_fixup_uint16(identify.byte, 76); /* SATA capabilities */
6024a6e3
SL
480 disk_identify_fixup_uint16(identify.byte, 82); /* command set supported */
481 disk_identify_fixup_uint16(identify.byte, 83); /* command set supported */
482 disk_identify_fixup_uint16(identify.byte, 84); /* command set supported */
483 disk_identify_fixup_uint16(identify.byte, 85); /* command set supported */
484 disk_identify_fixup_uint16(identify.byte, 86); /* command set supported */
485 disk_identify_fixup_uint16(identify.byte, 87); /* command set supported */
486 disk_identify_fixup_uint16(identify.byte, 89); /* time required for SECURITY ERASE UNIT */
487 disk_identify_fixup_uint16(identify.byte, 90); /* time required for enhanced SECURITY ERASE UNIT */
488 disk_identify_fixup_uint16(identify.byte, 91); /* current APM values */
489 disk_identify_fixup_uint16(identify.byte, 94); /* current AAM value */
a6affd88
KS
490 disk_identify_fixup_uint16(identify.byte, 108); /* WWN */
491 disk_identify_fixup_uint16(identify.byte, 109); /* WWN */
492 disk_identify_fixup_uint16(identify.byte, 110); /* WWN */
493 disk_identify_fixup_uint16(identify.byte, 111); /* WWN */
6024a6e3
SL
494 disk_identify_fixup_uint16(identify.byte, 128); /* device lock function */
495 disk_identify_fixup_uint16(identify.byte, 217); /* nominal media rotation rate */
496 memcpy(&id, identify.byte, sizeof id);
912541b0
KS
497 } else {
498 /* If this fails, then try HDIO_GET_IDENTITY */
499 if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) {
56f64d95 500 log_debug_errno(errno, "HDIO_GET_IDENTITY failed for '%s': %m", node);
11c6f693 501 return 2;
912541b0
KS
502 }
503 }
912541b0 504
8dc26de6 505 memcpy(model, id.model, 40);
912541b0
KS
506 model[40] = '\0';
507 udev_util_encode_string(model, model_enc, sizeof(model_enc));
508 util_replace_whitespace((char *) id.model, model, 40);
509 util_replace_chars(model, NULL);
510 util_replace_whitespace((char *) id.serial_no, serial, 20);
511 util_replace_chars(serial, NULL);
512 util_replace_whitespace((char *) id.fw_rev, revision, 8);
513 util_replace_chars(revision, NULL);
514
515 if (export) {
516 /* Set this to convey the disk speaks the ATA protocol */
517 printf("ID_ATA=1\n");
518
519 if ((id.config >> 8) & 0x80) {
520 /* This is an ATAPI device */
521 switch ((id.config >> 8) & 0x1f) {
522 case 0:
523 printf("ID_TYPE=cd\n");
524 break;
525 case 1:
526 printf("ID_TYPE=tape\n");
527 break;
528 case 5:
529 printf("ID_TYPE=cd\n");
530 break;
531 case 7:
532 printf("ID_TYPE=optical\n");
533 break;
534 default:
535 printf("ID_TYPE=generic\n");
536 break;
537 }
538 } else {
539 printf("ID_TYPE=disk\n");
540 }
541 printf("ID_BUS=ata\n");
542 printf("ID_MODEL=%s\n", model);
543 printf("ID_MODEL_ENC=%s\n", model_enc);
544 printf("ID_REVISION=%s\n", revision);
545 if (serial[0] != '\0') {
546 printf("ID_SERIAL=%s_%s\n", model, serial);
547 printf("ID_SERIAL_SHORT=%s\n", serial);
548 } else {
549 printf("ID_SERIAL=%s\n", model);
550 }
551
552 if (id.command_set_1 & (1<<5)) {
11c6f693
ZJS
553 printf("ID_ATA_WRITE_CACHE=1\n");
554 printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0);
912541b0
KS
555 }
556 if (id.command_set_1 & (1<<10)) {
557 printf("ID_ATA_FEATURE_SET_HPA=1\n");
558 printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id.cfs_enable_1 & (1<<10)) ? 1 : 0);
559
560 /*
561 * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address
562 * so it is easy to check whether the protected area is in use.
563 */
564 }
565 if (id.command_set_1 & (1<<3)) {
566 printf("ID_ATA_FEATURE_SET_PM=1\n");
567 printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id.cfs_enable_1 & (1<<3)) ? 1 : 0);
568 }
569 if (id.command_set_1 & (1<<1)) {
570 printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
571 printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id.cfs_enable_1 & (1<<1)) ? 1 : 0);
572 printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id.trseuc * 2);
573 if ((id.cfs_enable_1 & (1<<1))) /* enabled */ {
574 if (id.dlf & (1<<8))
575 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
576 else
577 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
578 }
579 if (id.dlf & (1<<5))
580 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2);
581 if (id.dlf & (1<<4))
582 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
583 if (id.dlf & (1<<3))
584 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
585 if (id.dlf & (1<<2))
586 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
587 }
588 if (id.command_set_1 & (1<<0)) {
589 printf("ID_ATA_FEATURE_SET_SMART=1\n");
590 printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id.cfs_enable_1 & (1<<0)) ? 1 : 0);
591 }
592 if (id.command_set_2 & (1<<9)) {
593 printf("ID_ATA_FEATURE_SET_AAM=1\n");
594 printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<9)) ? 1 : 0);
595 printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id.acoustic >> 8);
596 printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id.acoustic & 0xff);
597 }
598 if (id.command_set_2 & (1<<5)) {
599 printf("ID_ATA_FEATURE_SET_PUIS=1\n");
600 printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id.cfs_enable_2 & (1<<5)) ? 1 : 0);
601 }
602 if (id.command_set_2 & (1<<3)) {
603 printf("ID_ATA_FEATURE_SET_APM=1\n");
604 printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<3)) ? 1 : 0);
605 if ((id.cfs_enable_2 & (1<<3)))
606 printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id.CurAPMvalues & 0xff);
607 }
608 if (id.command_set_2 & (1<<0))
609 printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
610
611 /*
612 * Word 76 indicates the capabilities of a SATA device. A PATA device shall set
613 * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then
614 * the device does not claim compliance with the Serial ATA specification and words
615 * 76 through 79 are not valid and shall be ignored.
616 */
3dd0bbeb
ZJS
617
618 word = identify.wyde[76];
4c701096 619 if (!IN_SET(word, 0x0000, 0xffff)) {
912541b0
KS
620 printf("ID_ATA_SATA=1\n");
621 /*
622 * If bit 2 of word 76 is set to one, then the device supports the Gen2
623 * signaling rate of 3.0 Gb/s (see SATA 2.6).
624 *
625 * If bit 1 of word 76 is set to one, then the device supports the Gen1
626 * signaling rate of 1.5 Gb/s (see SATA 2.6).
627 */
628 if (word & (1<<2))
629 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
630 if (word & (1<<1))
631 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
632 }
633
634 /* Word 217 indicates the nominal media rotation rate of the device */
3dd0bbeb 635 word = identify.wyde[217];
11c6f693
ZJS
636 if (word == 0x0001)
637 printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
638 else if (word >= 0x0401 && word <= 0xfffe)
639 printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
912541b0
KS
640
641 /*
642 * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
643 * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE.
644 * All other values are reserved.
645 */
3dd0bbeb 646 word = identify.wyde[108];
01f61d33
ZJS
647 if ((word & 0xf000) == 0x5000) {
648 uint64_t wwwn;
649
650 wwwn = identify.wyde[108];
651 wwwn <<= 16;
652 wwwn |= identify.wyde[109];
653 wwwn <<= 16;
654 wwwn |= identify.wyde[110];
655 wwwn <<= 16;
656 wwwn |= identify.wyde[111];
ec62e858
JE
657 printf("ID_WWN=0x%1$" PRIx64 "\n"
658 "ID_WWN_WITH_EXTENSION=0x%1$" PRIx64 "\n",
01f61d33
ZJS
659 wwwn);
660 }
912541b0
KS
661
662 /* from Linux's include/linux/ata.h */
4c701096 663 if (IN_SET(identify.wyde[0], 0x848a, 0x844a) ||
3dd0bbeb 664 (identify.wyde[83] & 0xc004) == 0x4004)
912541b0 665 printf("ID_ATA_CFA=1\n");
912541b0
KS
666 } else {
667 if (serial[0] != '\0')
668 printf("%s_%s\n", model, serial);
669 else
670 printf("%s\n", model);
671 }
11c6f693
ZJS
672
673 return 0;
670e4705 674}