scsi查询命令以获取串行和模型信息

时间:2014-02-11 06:05:43

标签: c model hard-drive serial-number scsi

通过查询从SCSI驱动器获取串行和型号信息时遇到问题。我的查询总是返回空字符串。

这是一个快速而又脏的查询代码,它至少应该输出整个查询返回值。输出未格式化为真正获取特定信息,但它仍然可以工作。但事实并非如此。

#define          DRIVEAMOUNT                20
#define          MODELINQUIRY               0x12,0x00,0x00,0x00,0x4A,0x00     /* SCSI command for standard inquiry*/
#define          SERIALINQUIRY              0x12,0x01,0x80,0x00,0x60,0x00     /* SCSI command for VPD inquiry page 0x80 for serial number */

/* used libraries */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <inttypes.h>

/* send scsi command and recieve output, designed for use with *ONLY* an inquiry command but might work with more than only inquiry*/
int SCSI_sendcmd(uint64_t sg_fd, unsigned char *cmdblk, char *output) {
    unsigned char reply_buffer[96];
    sg_io_hdr_t  io_hdr;
    int i;

    /* Send INQUIRY command */
    memset(&io_hdr,0,sizeof(sg_io_hdr_t));
    io_hdr.interface_id='S';
    io_hdr.dxfer_direction=SG_DXFER_FROM_DEV;
    io_hdr.dxfer_len=sizeof(reply_buffer);
    io_hdr.dxferp=reply_buffer;
    io_hdr.cmd_len=sizeof(cmdblk);
    io_hdr.cmdp=cmdblk;
    io_hdr.timeout=1000;

    if (ioctl(sg_fd,SG_IO,&io_hdr)<0) {return 3;}   /* cannot send inquiry command */

    if ((io_hdr.info&SG_INFO_OK_MASK)!=SG_INFO_OK) {return 2;}  /* inquiry command failed */

    for (i=0;i<96;i++) {
       output[i]=reply_buffer[i];
    }
    return 1; /*succes*/
}

int main () {
    /* initialize various vars.*/
    FILE                                    *driveptr; 

    int                                     i;

    char                                    OSpointer[9];

    unsigned char                           output1[96],
                                            output2[96],
                                            serialinq[6]={SERIALINQUIRY},                          /* var to store SCSI inquiry for serial number */
                                            modelinq[6]={MODELINQUIRY};                            /* var to store SCSI inquiry for model string */

    for (i=0;i<DRIVEAMOUNT;i++) {
        strcpy(OSpointer,"/dev/sdX");
        OSpointer[7]='a'+i;

        driveptr=fopen(OSpointer,"r");
        if (driveptr==NULL) {
            continue;
        }

        if (SCSI_sendcmd(fileno(driveptr),modelinq,output1)==1)  {printf("model:|%s|\n",output1);}
        if (SCSI_sendcmd(fileno(driveptr),serialinq,output2)==1) {printf("serial:|%s|\n",output2);}
        printf("\n");

        fclose(driveptr);
    } 
}

3 个答案:

答案 0 :(得分:1)

字符串的早期部分有终止('\ 0')字符,%s只打印出来,直到达到这样的字符!要么删除它们,要么在循环中打印出每个字符串!

答案 1 :(得分:0)

for (i=0;i<96;i++) {
       output[i]=reply_buffer[i];
}

应从reply_buffer[4]

开始

答案 2 :(得分:0)

使用SCSI_sendcmd()的调试器检查replay_bufferoutput的内容会显示结果的样子。 示例(gdb):

(gdb) n
34         output[i]=reply_buffer[i];
(gdb) p reply_buffer 
$2 = "\000\000\005\002[\000\000\000ATA     WDC WD7500AVDS-60A01", '\000' <repeats 23 times>, "`\003 \002`", '\000' <repeats 31 times>