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