This source file includes following definitions.
- disk_scsi_inquiry_command
- disk_identify_command
- disk_identify_packet_device_command
- disk_identify_get_string
- disk_identify_fixup_string
- disk_identify_fixup_uint16
- disk_identify
- _printf_
- main
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/fs.h>
#include <linux/cdrom.h>
#include <linux/bsg.h>
#include <arpa/inet.h>
#include "libudev.h"
#include "libudev-private.h"
#include "log.h"
#define COMMAND_TIMEOUT_MSEC (30 * 1000)
static int disk_scsi_inquiry_command(int fd,
void *buf,
size_t buf_len)
{
uint8_t cdb[6] = {
[0] = 0x12,
[3] = (buf_len >> 8),
[4] = (buf_len & 0xff),
};
uint8_t sense[32] = {};
struct sg_io_v4 io_v4 = {
.guard = 'Q',
.protocol = BSG_PROTOCOL_SCSI,
.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
.request_len = sizeof(cdb),
.request = (uintptr_t) cdb,
.max_response_len = sizeof(sense),
.response = (uintptr_t) sense,
.din_xfer_len = buf_len,
.din_xferp = (uintptr_t) buf,
.timeout = COMMAND_TIMEOUT_MSEC,
};
int ret;
ret = ioctl(fd, SG_IO, &io_v4);
if (ret != 0) {
if (errno == EINVAL) {
struct sg_io_hdr io_hdr = {
.interface_id = 'S',
.cmdp = (unsigned char*) cdb,
.cmd_len = sizeof (cdb),
.dxferp = buf,
.dxfer_len = buf_len,
.sbp = sense,
.mx_sb_len = sizeof(sense),
.dxfer_direction = SG_DXFER_FROM_DEV,
.timeout = COMMAND_TIMEOUT_MSEC,
};
ret = ioctl(fd, SG_IO, &io_hdr);
if (ret != 0)
return ret;
if (!(io_hdr.status == 0 &&
io_hdr.host_status == 0 &&
io_hdr.driver_status == 0)) {
errno = EIO;
return -1;
}
} else
return ret;
}
if (!(io_v4.device_status == 0 &&
io_v4.transport_status == 0 &&
io_v4.driver_status == 0)) {
errno = EIO;
return -1;
}
return 0;
}
static int disk_identify_command(int fd,
void *buf,
size_t buf_len)
{
uint8_t cdb[12] = {
[0] = 0xa1,
[1] = 4 << 1,
[2] = 0x2e,
[3] = 0,
[4] = 1,
[5] = 0,
[6] = 0,
[7] = 0,
[8] = 0 & 0x4F,
[9] = 0xEC,
};
uint8_t sense[32] = {};
uint8_t *desc = sense + 8;
struct sg_io_v4 io_v4 = {
.guard = 'Q',
.protocol = BSG_PROTOCOL_SCSI,
.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
.request_len = sizeof(cdb),
.request = (uintptr_t) cdb,
.max_response_len = sizeof(sense),
.response = (uintptr_t) sense,
.din_xfer_len = buf_len,
.din_xferp = (uintptr_t) buf,
.timeout = COMMAND_TIMEOUT_MSEC,
};
int ret;
ret = ioctl(fd, SG_IO, &io_v4);
if (ret != 0) {
if (errno == EINVAL) {
struct sg_io_hdr io_hdr = {
.interface_id = 'S',
.cmdp = (unsigned char*) cdb,
.cmd_len = sizeof (cdb),
.dxferp = buf,
.dxfer_len = buf_len,
.sbp = sense,
.mx_sb_len = sizeof (sense),
.dxfer_direction = SG_DXFER_FROM_DEV,
.timeout = COMMAND_TIMEOUT_MSEC,
};
ret = ioctl(fd, SG_IO, &io_hdr);
if (ret != 0)
return ret;
} else
return ret;
}
if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
errno = EIO;
return -1;
}
return 0;
}
static int disk_identify_packet_device_command(int fd,
void *buf,
size_t buf_len)
{
uint8_t cdb[16] = {
[0] = 0x85,
[1] = 4 << 1,
[2] = 0x2e,
[3] = 0,
[4] = 0,
[5] = 0,
[6] = 1,
[7] = 0,
[8] = 0,
[9] = 0,
[10] = 0,
[11] = 0,
[12] = 0,
[13] = 0,
[14] = 0xA1,
[15] = 0,
};
uint8_t sense[32] = {};
uint8_t *desc = sense + 8;
struct sg_io_v4 io_v4 = {
.guard = 'Q',
.protocol = BSG_PROTOCOL_SCSI,
.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
.request_len = sizeof (cdb),
.request = (uintptr_t) cdb,
.max_response_len = sizeof (sense),
.response = (uintptr_t) sense,
.din_xfer_len = buf_len,
.din_xferp = (uintptr_t) buf,
.timeout = COMMAND_TIMEOUT_MSEC,
};
int ret;
ret = ioctl(fd, SG_IO, &io_v4);
if (ret != 0) {
if (errno == EINVAL) {
struct sg_io_hdr io_hdr = {
.interface_id = 'S',
.cmdp = (unsigned char*) cdb,
.cmd_len = sizeof (cdb),
.dxferp = buf,
.dxfer_len = buf_len,
.sbp = sense,
.mx_sb_len = sizeof (sense),
.dxfer_direction = SG_DXFER_FROM_DEV,
.timeout = COMMAND_TIMEOUT_MSEC,
};
ret = ioctl(fd, SG_IO, &io_hdr);
if (ret != 0)
return ret;
} else
return ret;
}
if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
errno = EIO;
return -1;
}
return 0;
}
static void disk_identify_get_string(uint8_t identify[512],
unsigned int offset_words,
char *dest,
size_t dest_len)
{
unsigned int c1;
unsigned int c2;
while (dest_len > 0) {
c1 = identify[offset_words * 2 + 1];
c2 = identify[offset_words * 2];
*dest = c1;
dest++;
*dest = c2;
dest++;
offset_words++;
dest_len -= 2;
}
}
static void disk_identify_fixup_string(uint8_t identify[512],
unsigned int offset_words,
size_t len)
{
disk_identify_get_string(identify, offset_words,
(char *) identify + offset_words * 2, len);
}
static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offset_words)
{
uint16_t *p;
p = (uint16_t *) identify;
p[offset_words] = le16toh (p[offset_words]);
}
static int disk_identify(struct udev *udev,
int fd,
uint8_t out_identify[512],
int *out_is_packet_device)
{
int ret;
uint8_t inquiry_buf[36];
int peripheral_device_type;
int all_nul_bytes;
int n;
int is_packet_device = 0;
memzero(out_identify, 512);
ret = disk_scsi_inquiry_command (fd, inquiry_buf, sizeof (inquiry_buf));
if (ret != 0)
goto out;
peripheral_device_type = inquiry_buf[0] & 0x1f;
if (peripheral_device_type == 0x05)
{
is_packet_device = 1;
ret = disk_identify_packet_device_command(fd, out_identify, 512);
goto check_nul_bytes;
}
if (peripheral_device_type != 0x00) {
ret = -1;
errno = EIO;
goto out;
}
ret = disk_identify_command(fd, out_identify, 512);
if (ret != 0)
goto out;
check_nul_bytes:
all_nul_bytes = 1;
for (n = 0; n < 512; n++) {
if (out_identify[n] != '\0') {
all_nul_bytes = 0;
break;
}
}
if (all_nul_bytes) {
ret = -1;
errno = EIO;
goto out;
}
out:
if (out_is_packet_device != NULL)
*out_is_packet_device = is_packet_device;
return ret;
}
_printf_(6,0)
static void log_fn(struct udev *udev, int priority,
const char *file, int line, const char *fn,
const char *format, va_list args)
{
log_metav(priority, file, line, fn, format, args);
}
int main(int argc, char *argv[])
{
struct udev *udev;
struct hd_driveid id;
uint8_t identify[512];
uint16_t *identify_words;
char model[41];
char model_enc[256];
char serial[21];
char revision[9];
const char *node = NULL;
int export = 0;
int fd;
uint16_t word;
int rc = 0;
int is_packet_device = 0;
static const struct option options[] = {
{ "export", no_argument, NULL, 'x' },
{ "help", no_argument, NULL, 'h' },
{}
};
log_parse_environment();
log_open();
udev = udev_new();
if (udev == NULL)
goto exit;
udev_set_log_fn(udev, log_fn);
while (1) {
int option;
option = getopt_long(argc, argv, "xh", options, NULL);
if (option == -1)
break;
switch (option) {
case 'x':
export = 1;
break;
case 'h':
printf("Usage: ata_id [--export] [--help] <device>\n"
" --export print values as environment keys\n"
" --help print this help text\n\n");
goto exit;
}
}
node = argv[optind];
if (node == NULL) {
log_error("no node specified");
rc = 1;
goto exit;
}
fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
if (fd < 0) {
log_error("unable to open '%s'", node);
rc = 1;
goto exit;
}
if (disk_identify(udev, fd, identify, &is_packet_device) == 0) {
disk_identify_fixup_string(identify, 10, 20);
disk_identify_fixup_string(identify, 23, 8);
disk_identify_fixup_string(identify, 27, 40);
disk_identify_fixup_uint16(identify, 0);
disk_identify_fixup_uint16(identify, 75);
disk_identify_fixup_uint16(identify, 75);
disk_identify_fixup_uint16(identify, 82);
disk_identify_fixup_uint16(identify, 83);
disk_identify_fixup_uint16(identify, 84);
disk_identify_fixup_uint16(identify, 85);
disk_identify_fixup_uint16(identify, 86);
disk_identify_fixup_uint16(identify, 87);
disk_identify_fixup_uint16(identify, 89);
disk_identify_fixup_uint16(identify, 90);
disk_identify_fixup_uint16(identify, 91);
disk_identify_fixup_uint16(identify, 94);
disk_identify_fixup_uint16(identify, 128);
disk_identify_fixup_uint16(identify, 217);
memcpy(&id, identify, sizeof id);
} else {
if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) {
log_debug("HDIO_GET_IDENTITY failed for '%s': %m", node);
rc = 2;
goto close;
}
}
identify_words = (uint16_t *) identify;
memcpy (model, id.model, 40);
model[40] = '\0';
udev_util_encode_string(model, model_enc, sizeof(model_enc));
util_replace_whitespace((char *) id.model, model, 40);
util_replace_chars(model, NULL);
util_replace_whitespace((char *) id.serial_no, serial, 20);
util_replace_chars(serial, NULL);
util_replace_whitespace((char *) id.fw_rev, revision, 8);
util_replace_chars(revision, NULL);
if (export) {
printf("ID_ATA=1\n");
if ((id.config >> 8) & 0x80) {
switch ((id.config >> 8) & 0x1f) {
case 0:
printf("ID_TYPE=cd\n");
break;
case 1:
printf("ID_TYPE=tape\n");
break;
case 5:
printf("ID_TYPE=cd\n");
break;
case 7:
printf("ID_TYPE=optical\n");
break;
default:
printf("ID_TYPE=generic\n");
break;
}
} else {
printf("ID_TYPE=disk\n");
}
printf("ID_BUS=ata\n");
printf("ID_MODEL=%s\n", model);
printf("ID_MODEL_ENC=%s\n", model_enc);
printf("ID_REVISION=%s\n", revision);
if (serial[0] != '\0') {
printf("ID_SERIAL=%s_%s\n", model, serial);
printf("ID_SERIAL_SHORT=%s\n", serial);
} else {
printf("ID_SERIAL=%s\n", model);
}
if (id.command_set_1 & (1<<5)) {
printf ("ID_ATA_WRITE_CACHE=1\n");
printf ("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0);
}
if (id.command_set_1 & (1<<10)) {
printf("ID_ATA_FEATURE_SET_HPA=1\n");
printf("ID_ATA_FEATURE_SET_HPA_ENABLED=%d\n", (id.cfs_enable_1 & (1<<10)) ? 1 : 0);
}
if (id.command_set_1 & (1<<3)) {
printf("ID_ATA_FEATURE_SET_PM=1\n");
printf("ID_ATA_FEATURE_SET_PM_ENABLED=%d\n", (id.cfs_enable_1 & (1<<3)) ? 1 : 0);
}
if (id.command_set_1 & (1<<1)) {
printf("ID_ATA_FEATURE_SET_SECURITY=1\n");
printf("ID_ATA_FEATURE_SET_SECURITY_ENABLED=%d\n", (id.cfs_enable_1 & (1<<1)) ? 1 : 0);
printf("ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=%d\n", id.trseuc * 2);
if ((id.cfs_enable_1 & (1<<1))) {
if (id.dlf & (1<<8))
printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=maximum\n");
else
printf("ID_ATA_FEATURE_SET_SECURITY_LEVEL=high\n");
}
if (id.dlf & (1<<5))
printf("ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=%d\n", id.trsEuc * 2);
if (id.dlf & (1<<4))
printf("ID_ATA_FEATURE_SET_SECURITY_EXPIRE=1\n");
if (id.dlf & (1<<3))
printf("ID_ATA_FEATURE_SET_SECURITY_FROZEN=1\n");
if (id.dlf & (1<<2))
printf("ID_ATA_FEATURE_SET_SECURITY_LOCKED=1\n");
}
if (id.command_set_1 & (1<<0)) {
printf("ID_ATA_FEATURE_SET_SMART=1\n");
printf("ID_ATA_FEATURE_SET_SMART_ENABLED=%d\n", (id.cfs_enable_1 & (1<<0)) ? 1 : 0);
}
if (id.command_set_2 & (1<<9)) {
printf("ID_ATA_FEATURE_SET_AAM=1\n");
printf("ID_ATA_FEATURE_SET_AAM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<9)) ? 1 : 0);
printf("ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=%d\n", id.acoustic >> 8);
printf("ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=%d\n", id.acoustic & 0xff);
}
if (id.command_set_2 & (1<<5)) {
printf("ID_ATA_FEATURE_SET_PUIS=1\n");
printf("ID_ATA_FEATURE_SET_PUIS_ENABLED=%d\n", (id.cfs_enable_2 & (1<<5)) ? 1 : 0);
}
if (id.command_set_2 & (1<<3)) {
printf("ID_ATA_FEATURE_SET_APM=1\n");
printf("ID_ATA_FEATURE_SET_APM_ENABLED=%d\n", (id.cfs_enable_2 & (1<<3)) ? 1 : 0);
if ((id.cfs_enable_2 & (1<<3)))
printf("ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=%d\n", id.CurAPMvalues & 0xff);
}
if (id.command_set_2 & (1<<0))
printf("ID_ATA_DOWNLOAD_MICROCODE=1\n");
word = *((uint16_t *) identify + 76);
if (word != 0x0000 && word != 0xffff) {
printf("ID_ATA_SATA=1\n");
if (word & (1<<2))
printf("ID_ATA_SATA_SIGNAL_RATE_GEN2=1\n");
if (word & (1<<1))
printf("ID_ATA_SATA_SIGNAL_RATE_GEN1=1\n");
}
word = *((uint16_t *) identify + 217);
if (word != 0x0000) {
if (word == 0x0001) {
printf ("ID_ATA_ROTATION_RATE_RPM=0\n");
} else if (word >= 0x0401 && word <= 0xfffe) {
printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
}
}
word = *((uint16_t *) identify + 108);
if ((word & 0xf000) == 0x5000) {
uint64_t wwwn;
wwwn = *((uint16_t *) identify + 108);
wwwn <<= 16;
wwwn |= *((uint16_t *) identify + 109);
wwwn <<= 16;
wwwn |= *((uint16_t *) identify + 110);
wwwn <<= 16;
wwwn |= *((uint16_t *) identify + 111);
printf("ID_WWN=0x%llx\n", (unsigned long long int) wwwn);
printf("ID_WWN_WITH_EXTENSION=0x%llx\n", (unsigned long long int) wwwn);
}
if (identify_words[0] == 0x848a || identify_words[0] == 0x844a) {
printf("ID_ATA_CFA=1\n");
} else {
if ((identify_words[83] & 0xc004) == 0x4004) {
printf("ID_ATA_CFA=1\n");
}
}
} else {
if (serial[0] != '\0')
printf("%s_%s\n", model, serial);
else
printf("%s\n", model);
}
close:
close(fd);
exit:
udev_unref(udev);
log_close();
return rc;
}