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