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