And, of course, forgot to attach. Here it is. -- tejun
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <scsi/sg.h> #include <scsi/scsi_ioctl.h> #include <linux/bsg.h> #define COMMAND_TIMEOUT_MSEC (30 * 1000) static int disk_identify_packet_device_command(int fd, void *buf, size_t buf_len) { struct sg_io_v4 io_v4; uint8_t cdb[16]; uint8_t sense[32]; uint8_t *desc = sense+8; int ret; /* * ATA Pass-Through 16 byte command, as described in * * T10 04-262r8 ATA Command Pass-Through * * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf */ memset(cdb, 0, sizeof(cdb)); cdb[0] = 0x85; /* OPERATION CODE: 16 byte pass through */ cdb[1] = 4 << 1; /* PROTOCOL: PIO Data-in */ cdb[2] = 0x2e; /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */ cdb[3] = 0; /* FEATURES */ cdb[4] = 0; /* FEATURES */ cdb[5] = 0; /* SECTORS */ cdb[6] = 1; /* SECTORS */ cdb[7] = 0; /* LBA LOW */ cdb[8] = 0; /* LBA LOW */ cdb[9] = 0; /* LBA MID */ cdb[10] = 0; /* LBA MID */ cdb[11] = 0; /* LBA HIGH */ cdb[12] = 0; /* LBA HIGH */ cdb[13] = 0; /* DEVICE */ cdb[14] = 0xA1; /* Command: ATA IDENTIFY PACKET DEVICE */; cdb[15] = 0; /* CONTROL */ memset(sense, 0, sizeof(sense)); memset(&io_v4, 0, sizeof(struct sg_io_v4)); io_v4.guard = 'Q'; io_v4.protocol = BSG_PROTOCOL_SCSI; io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; io_v4.request_len = sizeof (cdb); io_v4.request = (uintptr_t) cdb; io_v4.max_response_len = sizeof (sense); io_v4.response = (uintptr_t) sense; io_v4.din_xfer_len = buf_len; io_v4.din_xferp = (uintptr_t) buf; io_v4.timeout = COMMAND_TIMEOUT_MSEC; ret = ioctl(fd, SG_IO, &io_v4); if (ret != 0) { /* could be that the driver doesn't do version 4, try version 3 */ if (errno == EINVAL) { struct sg_io_hdr io_hdr; memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); io_hdr.interface_id = 'S'; io_hdr.cmdp = (unsigned char*) cdb; io_hdr.cmd_len = sizeof (cdb); io_hdr.dxferp = buf; io_hdr.dxfer_len = buf_len; io_hdr.sbp = sense; io_hdr.mx_sb_len = sizeof (sense); io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; io_hdr.timeout = COMMAND_TIMEOUT_MSEC; ret = ioctl(fd, SG_IO, &io_hdr); if (ret != 0) goto out; } else { goto out; } } if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) { errno = EIO; ret = -1; goto out; } out: return ret; } int main(int argc, char *argv[]) { char buf[2048]; char *id; char *path; int offset = 0; int fd; FILE *fp; if (argc < 2) { fprintf(stderr, "Usage: test-identify-packet /dev/srX [OFFSET]\n"); return 1; } path = argv[1]; if (argc > 2) offset = atoi(argv[2]); if (offset < 0 || offset > 512) { fprintf(stderr, "offset out of range\n"); return 1; } fd = open(path, O_RDONLY|O_NONBLOCK); if (fd < 0) { perror("open"); return 1; } fp = popen("hexdump -Cv", "w"); if (!fp) { perror("popen"); return 1; } id = (void *)((((unsigned long)buf + 511) & ~511) + offset); printf("id buffer=%p\n", id); disk_identify_packet_device_command(fd, id, 512); fwrite(id, 512, 1, fp); fclose(fp); return 0; }