]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/ata_id/ata_id.c
tree-wide: use IN_SET macro (#6977)
[thirdparty/systemd.git] / src / udev / ata_id / ata_id.c
CommitLineData
e0f83fbe 1/*
670e4705
KS
2 * ata_id - reads product/serial number from ATA drives
3 *
1298001e 4 * Copyright (C) 2005-2008 Kay Sievers <kay@vrfy.org>
66094a4a 5 * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
6992637e 6 * Copyright (C) 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
DZ
319 *
320 * This routine is based on code from libatasmart, Copyright 2008
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
670e4705
KS
407int main(int argc, char *argv[])
408{
11c6f693 409 _cleanup_udev_unref_ struct udev *udev = NULL;
912541b0 410 struct hd_driveid id;
6024a6e3
SL
411 union {
412 uint8_t byte[512];
413 uint16_t wyde[256];
6024a6e3 414 } identify;
912541b0
KS
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;
11c6f693 421 _cleanup_close_ int fd = -1;
912541b0 422 uint16_t word;
912541b0
KS
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
b237a168
ZJS
430 log_set_target(LOG_TARGET_AUTO);
431 udev_parse_config();
5168f84a
LP
432 log_parse_environment();
433 log_open();
434
912541b0
KS
435 udev = udev_new();
436 if (udev == NULL)
11c6f693 437 return 0;
912541b0 438
57255510 439 for (;;) {
912541b0
KS
440 int option;
441
442 option = getopt_long(argc, argv, "xh", options, NULL);
443 if (option == -1)
444 break;
445
446 switch (option) {
447 case 'x':
448 export = 1;
449 break;
450 case 'h':
451 printf("Usage: ata_id [--export] [--help] <device>\n"
257e968d
RM
452 " -x,--export print values as environment keys\n"
453 " -h,--help print this help text\n\n");
11c6f693 454 return 0;
912541b0
KS
455 }
456 }
457
458 node = argv[optind];
459 if (node == NULL) {
9f6445e3 460 log_error("no node specified");
11c6f693 461 return 1;
912541b0
KS
462 }
463
c8a202b7 464 fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
912541b0 465 if (fd < 0) {
9f6445e3 466 log_error("unable to open '%s'", node);
11c6f693 467 return 1;
912541b0
KS
468 }
469
6024a6e3 470 if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
912541b0
KS
471 /*
472 * fix up only the fields from the IDENTIFY data that we are going to
473 * use and copy it into the hd_driveid struct for convenience
474 */
6024a6e3
SL
475 disk_identify_fixup_string(identify.byte, 10, 20); /* serial */
476 disk_identify_fixup_string(identify.byte, 23, 8); /* fwrev */
477 disk_identify_fixup_string(identify.byte, 27, 40); /* model */
478 disk_identify_fixup_uint16(identify.byte, 0); /* configuration */
479 disk_identify_fixup_uint16(identify.byte, 75); /* queue depth */
a6affd88 480 disk_identify_fixup_uint16(identify.byte, 76); /* SATA capabilities */
6024a6e3
SL
481 disk_identify_fixup_uint16(identify.byte, 82); /* command set supported */
482 disk_identify_fixup_uint16(identify.byte, 83); /* command set supported */
483 disk_identify_fixup_uint16(identify.byte, 84); /* command set supported */
484 disk_identify_fixup_uint16(identify.byte, 85); /* command set supported */
485 disk_identify_fixup_uint16(identify.byte, 86); /* command set supported */
486 disk_identify_fixup_uint16(identify.byte, 87); /* command set supported */
487 disk_identify_fixup_uint16(identify.byte, 89); /* time required for SECURITY ERASE UNIT */
488 disk_identify_fixup_uint16(identify.byte, 90); /* time required for enhanced SECURITY ERASE UNIT */
489 disk_identify_fixup_uint16(identify.byte, 91); /* current APM values */
490 disk_identify_fixup_uint16(identify.byte, 94); /* current AAM value */
a6affd88
KS
491 disk_identify_fixup_uint16(identify.byte, 108); /* WWN */
492 disk_identify_fixup_uint16(identify.byte, 109); /* WWN */
493 disk_identify_fixup_uint16(identify.byte, 110); /* WWN */
494 disk_identify_fixup_uint16(identify.byte, 111); /* WWN */
6024a6e3
SL
495 disk_identify_fixup_uint16(identify.byte, 128); /* device lock function */
496 disk_identify_fixup_uint16(identify.byte, 217); /* nominal media rotation rate */
497 memcpy(&id, identify.byte, sizeof id);
912541b0
KS
498 } else {
499 /* If this fails, then try HDIO_GET_IDENTITY */
500 if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) {
56f64d95 501 log_debug_errno(errno, "HDIO_GET_IDENTITY failed for '%s': %m", node);
11c6f693 502 return 2;
912541b0
KS
503 }
504 }
912541b0 505
8dc26de6 506 memcpy(model, id.model, 40);
912541b0
KS
507 model[40] = '\0';
508 udev_util_encode_string(model, model_enc, sizeof(model_enc));
509 util_replace_whitespace((char *) id.model, model, 40);
510 util_replace_chars(model, NULL);
511 util_replace_whitespace((char *) id.serial_no, serial, 20);
512 util_replace_chars(serial, NULL);
513 util_replace_whitespace((char *) id.fw_rev, revision, 8);
514 util_replace_chars(revision, NULL);
515
516 if (export) {
517 /* Set this to convey the disk speaks the ATA protocol */
518 printf("ID_ATA=1\n");
519
520 if ((id.config >> 8) & 0x80) {
521 /* This is an ATAPI device */
522 switch ((id.config >> 8) & 0x1f) {
523 case 0:
524 printf("ID_TYPE=cd\n");
525 break;
526 case 1:
527 printf("ID_TYPE=tape\n");
528 break;
529 case 5:
530 printf("ID_TYPE=cd\n");
531 break;
532 case 7:
533 printf("ID_TYPE=optical\n");
534 break;
535 default:
536 printf("ID_TYPE=generic\n");
537 break;
538 }
539 } else {
540 printf("ID_TYPE=disk\n");
541 }
542 printf("ID_BUS=ata\n");
543 printf("ID_MODEL=%s\n", model);
544 printf("ID_MODEL_ENC=%s\n", model_enc);
545 printf("ID_REVISION=%s\n", revision);
546 if (serial[0] != '\0') {
547 printf("ID_SERIAL=%s_%s\n", model, serial);
548 printf("ID_SERIAL_SHORT=%s\n", serial);
549 } else {
550 printf("ID_SERIAL=%s\n", model);
551 }
552
553 if (id.command_set_1 & (1<<5)) {
11c6f693
ZJS
554 printf("ID_ATA_WRITE_CACHE=1\n");
555 printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0);
912541b0
KS
556 }
557 if (id.command_set_1 & (1<<10)) {
558 printf("ID_ATA_FEATURE_SET_HPA=1\n");
559 printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id.cfs_enable_1 & (1<<10)) ? 1 : 0);
560
561 /*
562 * TODO: use the READ NATIVE MAX ADDRESS command to get the native max address
563 * so it is easy to check whether the protected area is in use.
564 */
565 }
566 if (id.command_set_1 & (1<<3)) {
567 printf("ID_ATA_FEATURE_SET_PM=1\n");
568 printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id.cfs_enable_1 & (1<<3)) ? 1 : 0);
569 }
570 if (id.command_set_1 & (1<<1)) {
571 printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
572 printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id.cfs_enable_1 & (1<<1)) ? 1 : 0);
573 printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id.trseuc * 2);
574 if ((id.cfs_enable_1 & (1<<1))) /* enabled */ {
575 if (id.dlf & (1<<8))
576 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
577 else
578 printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
579 }
580 if (id.dlf & (1<<5))
581 printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2);
582 if (id.dlf & (1<<4))
583 printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
584 if (id.dlf & (1<<3))
585 printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
586 if (id.dlf & (1<<2))
587 printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
588 }
589 if (id.command_set_1 & (1<<0)) {
590 printf("ID_ATA_FEATURE_SET_SMART=1\n");
591 printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id.cfs_enable_1 & (1<<0)) ? 1 : 0);
592 }
593 if (id.command_set_2 & (1<<9)) {
594 printf("ID_ATA_FEATURE_SET_AAM=1\n");
595 printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<9)) ? 1 : 0);
596 printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id.acoustic >> 8);
597 printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id.acoustic & 0xff);
598 }
599 if (id.command_set_2 & (1<<5)) {
600 printf("ID_ATA_FEATURE_SET_PUIS=1\n");
601 printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id.cfs_enable_2 & (1<<5)) ? 1 : 0);
602 }
603 if (id.command_set_2 & (1<<3)) {
604 printf("ID_ATA_FEATURE_SET_APM=1\n");
605 printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<3)) ? 1 : 0);
606 if ((id.cfs_enable_2 & (1<<3)))
607 printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id.CurAPMvalues & 0xff);
608 }
609 if (id.command_set_2 & (1<<0))
610 printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
611
612 /*
613 * Word 76 indicates the capabilities of a SATA device. A PATA device shall set
614 * word 76 to 0000h or FFFFh. If word 76 is set to 0000h or FFFFh, then
615 * the device does not claim compliance with the Serial ATA specification and words
616 * 76 through 79 are not valid and shall be ignored.
617 */
3dd0bbeb
ZJS
618
619 word = identify.wyde[76];
4c701096 620 if (!IN_SET(word, 0x0000, 0xffff)) {
912541b0
KS
621 printf("ID_ATA_SATA=1\n");
622 /*
623 * If bit 2 of word 76 is set to one, then the device supports the Gen2
624 * signaling rate of 3.0 Gb/s (see SATA 2.6).
625 *
626 * If bit 1 of word 76 is set to one, then the device supports the Gen1
627 * signaling rate of 1.5 Gb/s (see SATA 2.6).
628 */
629 if (word & (1<<2))
630 printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
631 if (word & (1<<1))
632 printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
633 }
634
635 /* Word 217 indicates the nominal media rotation rate of the device */
3dd0bbeb 636 word = identify.wyde[217];
11c6f693
ZJS
637 if (word == 0x0001)
638 printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
639 else if (word >= 0x0401 && word <= 0xfffe)
640 printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
912541b0
KS
641
642 /*
643 * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
644 * format. Word 108 bits (15:12) shall contain 5h, indicating that the naming authority is IEEE.
645 * All other values are reserved.
646 */
3dd0bbeb 647 word = identify.wyde[108];
01f61d33
ZJS
648 if ((word & 0xf000) == 0x5000) {
649 uint64_t wwwn;
650
651 wwwn = identify.wyde[108];
652 wwwn <<= 16;
653 wwwn |= identify.wyde[109];
654 wwwn <<= 16;
655 wwwn |= identify.wyde[110];
656 wwwn <<= 16;
657 wwwn |= identify.wyde[111];
ec62e858
JE
658 printf("ID_WWN=0x%1$" PRIx64 "\n"
659 "ID_WWN_WITH_EXTENSION=0x%1$" PRIx64 "\n",
01f61d33
ZJS
660 wwwn);
661 }
912541b0
KS
662
663 /* from Linux's include/linux/ata.h */
4c701096 664 if (IN_SET(identify.wyde[0], 0x848a, 0x844a) ||
3dd0bbeb 665 (identify.wyde[83] & 0xc004) == 0x4004)
912541b0 666 printf("ID_ATA_CFA=1\n");
912541b0
KS
667 } else {
668 if (serial[0] != '\0')
669 printf("%s_%s\n", model, serial);
670 else
671 printf("%s\n", model);
672 }
11c6f693
ZJS
673
674 return 0;
670e4705 675}