2014-02-11 85 views
0

我有問題從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); 
    } 
} 

回答

1

字符串的早期部分有終止('\ 0')字符,%s只打印出來,直到達到這樣的字符!要麼刪除它們,要麼在循環中打印出每個字符!

+1

這不會提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 –

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

reply_buffer[4]

+0

請解釋爲什麼? – Jatin

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>