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