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