491 lines
13 KiB
C
491 lines
13 KiB
C
#include <unistd.h>
|
||
#include <fcntl.h>
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <errno.h>
|
||
#include <sys/ioctl.h>
|
||
#include <scsi/sg.h> /* take care: fetches glibc's /usr/include/scsi/sg.h */
|
||
#include <linux/capability.h>
|
||
#include "public.h"
|
||
|
||
#include "define.h"
|
||
#include "sham.h"
|
||
|
||
|
||
//INQUIRY COMMAND
|
||
|
||
extern int _icdev;
|
||
|
||
int USBWrite(int sg_fd, unsigned char * pData,unsigned int nLength,int nTimeOut)
|
||
{
|
||
unsigned char pCDB[12]={0};
|
||
int nCDBLen=12;
|
||
unsigned char sense_buffer[32];
|
||
sg_io_hdr_t io_hdr;
|
||
int rv = 0;
|
||
|
||
// rv = P(_icdev, 0, 10);
|
||
// if(rv == -1)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
pCDB[0]=0xfe;
|
||
pCDB[1]=1;
|
||
|
||
if(sg_fd < 0)
|
||
{
|
||
perror(" fd invalid 111");
|
||
return -1;
|
||
}
|
||
|
||
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
||
io_hdr.interface_id = 'S';
|
||
io_hdr.cmd_len = nCDBLen; //<2F><><EFBFBD><EFBFBD><EEB3A4>
|
||
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
||
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>)
|
||
io_hdr.dxfer_direction = SG_DXFER_TO_DEV; //д<><D0B4><EFBFBD><EFBFBD>
|
||
io_hdr.dxfer_len = nLength; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||
io_hdr.dxferp = pData; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
io_hdr.cmdp = pCDB; //<2F><><EFBFBD><EFBFBD>
|
||
io_hdr.sbp = sense_buffer; //<2F>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><F3B7B5BB><EFBFBD>Ϣ
|
||
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
||
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
||
/* io_hdr.pack_id = 0; */
|
||
/* io_hdr.usr_ptr = NULL; */
|
||
|
||
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
||
{
|
||
perror("USB Write-> SG_IO ioctl error");
|
||
return -1;
|
||
}
|
||
|
||
/* now for the error processing */
|
||
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
||
{
|
||
if (io_hdr.sb_len_wr > 0)
|
||
{
|
||
printf("USB Write-> sense data: ");
|
||
int k;
|
||
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
||
{
|
||
if ((k > 0) && (0 == (k % 10)))
|
||
printf("\n ");
|
||
printf("0x%02x ", sense_buffer[k]);
|
||
}
|
||
printf("\n");
|
||
}
|
||
if (io_hdr.masked_status)
|
||
printf("USB Write-> SCSI status=0x%x\n", io_hdr.status);
|
||
if (io_hdr.host_status)
|
||
printf("USB Write-> host_status=0x%x\n", io_hdr.host_status);
|
||
if (io_hdr.driver_status)
|
||
printf("USB Write-> driver_status=0x%x\n", io_hdr.driver_status);
|
||
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int USBRead(int sg_fd, unsigned char * pData,unsigned int nLength,int nTimeOut)
|
||
{
|
||
unsigned char pCDB[12]={0};
|
||
int nCDBLen=12,rv = 0;
|
||
unsigned char sense_buffer[32];
|
||
sg_io_hdr_t io_hdr;
|
||
pCDB[0]=0xfe;
|
||
pCDB[1]=2;
|
||
if(sg_fd < 0)
|
||
{
|
||
perror(" fd invalid 222");
|
||
// rv = V(_icdev, 0);
|
||
// if(rv == -1)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
return -1;
|
||
}
|
||
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
||
io_hdr.interface_id = 'S';
|
||
io_hdr.cmd_len = nCDBLen; //<2F><><EFBFBD><EFBFBD><EEB3A4>
|
||
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
||
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>)
|
||
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?
|
||
io_hdr.dxfer_len = nLength; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||
io_hdr.dxferp = pData; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
io_hdr.cmdp = pCDB; //<2F><><EFBFBD><EFBFBD>
|
||
io_hdr.sbp = sense_buffer; //<2F>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><F3B7B5BB><EFBFBD>Ϣ
|
||
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
||
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
||
/* io_hdr.pack_id = 0; */
|
||
/* io_hdr.usr_ptr = NULL; */
|
||
|
||
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
||
{
|
||
perror("USB Read-> SG_IO ioctl error");
|
||
// rv = V(_icdev, 0);
|
||
// if(rv == -1)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
return -1;
|
||
}
|
||
|
||
/* now for the error processing */
|
||
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
||
{
|
||
if (io_hdr.sb_len_wr > 0)
|
||
{
|
||
printf("USB Read-> sense data: ");
|
||
int k;
|
||
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
||
{
|
||
if ((k > 0) && (0 == (k % 10)))
|
||
printf("\n ");
|
||
printf("0x%02x ", sense_buffer[k]);
|
||
}
|
||
printf("\n");
|
||
}
|
||
if (io_hdr.masked_status)
|
||
printf("USB Read SCSI status=0x%x\n", io_hdr.status);
|
||
if (io_hdr.host_status)
|
||
printf("USB Read host_status=0x%x\n", io_hdr.host_status);
|
||
if (io_hdr.driver_status)
|
||
printf("USB Read driver_status=0x%x\n", io_hdr.driver_status);
|
||
// rv = V(_icdev, 0);
|
||
// if(rv == -1)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
return -1;
|
||
}
|
||
// rv = V(_icdev, 0);
|
||
// if(rv == -1)
|
||
// {
|
||
// return -1;
|
||
// }
|
||
return 0;
|
||
}
|
||
|
||
int scsi_inquiry(int sg_fd, unsigned char * pData)
|
||
{
|
||
/* This is a "standard" SCSI INQUIRY command. It is standard because the
|
||
* CMDDT and EVPD bits (in the second byte) are zero. All SCSI targets
|
||
* should respond promptly to a standard INQUIRY */
|
||
unsigned char inqCmdBlk[INQ_CMD_LEN] = {INQ_CMD_CODE, 0, 0, 0, INQ_REPLY_LEN, 0};
|
||
unsigned char sense_buffer[32];
|
||
sg_io_hdr_t io_hdr;
|
||
|
||
if(sg_fd < 0)
|
||
{
|
||
perror("Inquiry fd invalid 333");
|
||
return -1;
|
||
}
|
||
/* Prepare INQUIRY command */
|
||
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
||
io_hdr.interface_id = 'S';
|
||
io_hdr.cmd_len = sizeof(inqCmdBlk);//<2F><><EFBFBD><EFBFBD><EEB3A4>
|
||
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
||
io_hdr.mx_sb_len = sizeof(sense_buffer);
|
||
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
|
||
io_hdr.dxfer_len = INQ_REPLY_LEN; //<2F><><EFBFBD>س<EFBFBD><D8B3><EFBFBD>?
|
||
io_hdr.dxferp = pData; //<2F><><EFBFBD><EFBFBD>buffer?
|
||
io_hdr.cmdp = inqCmdBlk; //<2F><><EFBFBD><EFBFBD>buffer
|
||
io_hdr.sbp = sense_buffer;
|
||
io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
|
||
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
||
/* io_hdr.pack_id = 0; */
|
||
/* io_hdr.usr_ptr = NULL; */
|
||
|
||
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
||
{
|
||
perror("Inquiry SG_IO ioctl error");
|
||
// return -1;
|
||
}
|
||
|
||
/* now for the error processing */
|
||
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
||
{
|
||
if (io_hdr.sb_len_wr > 0)
|
||
{
|
||
printf(" sense data: ");
|
||
int k;
|
||
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
||
{
|
||
if ((k > 0) && (0 == (k % 10)))
|
||
printf("\n ");
|
||
printf("0x%02x ", sense_buffer[k]);
|
||
}
|
||
printf("\n");
|
||
}
|
||
if (io_hdr.masked_status)
|
||
printf("INQUIRY SCSI status=0x%x\n", io_hdr.status);
|
||
if (io_hdr.host_status)
|
||
printf("INQUIRY host_status=0x%x\n", io_hdr.host_status);
|
||
if (io_hdr.driver_status)
|
||
printf("INQUIRY driver_status=0x%x\n", io_hdr.driver_status);
|
||
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int USBSCSIWrite(int sg_fd, unsigned char * pCDB,int nCDBLen, unsigned char * pData,unsigned int nLength,int nTimeOut)
|
||
{
|
||
unsigned char sense_buffer[32];
|
||
sg_io_hdr_t io_hdr;
|
||
|
||
if(sg_fd < 0)
|
||
{
|
||
perror(" fd invalid 444 ");
|
||
return -1;
|
||
}
|
||
|
||
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
||
io_hdr.interface_id = 'S';
|
||
io_hdr.cmd_len = nCDBLen; //<2F><><EFBFBD><EFBFBD><EEB3A4>
|
||
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
||
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>)
|
||
io_hdr.dxfer_direction = SG_DXFER_TO_DEV; //д<><D0B4><EFBFBD><EFBFBD>
|
||
io_hdr.dxfer_len = nLength; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||
io_hdr.dxferp = pData; //д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
io_hdr.cmdp = pCDB; //<2F><><EFBFBD><EFBFBD>
|
||
io_hdr.sbp = sense_buffer; //<2F>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><F3B7B5BB><EFBFBD>Ϣ
|
||
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
||
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
||
/* io_hdr.pack_id = 0; */
|
||
/* io_hdr.usr_ptr = NULL; */
|
||
|
||
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
||
{
|
||
perror("USB Write-> SG_IO ioctl error");
|
||
return -1;
|
||
}
|
||
|
||
/* now for the error processing */
|
||
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
||
{
|
||
if (io_hdr.sb_len_wr > 0)
|
||
{
|
||
printf("USB Write-> sense data: ");
|
||
int k;
|
||
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
||
{
|
||
if ((k > 0) && (0 == (k % 10)))
|
||
printf("\n ");
|
||
printf("0x%02x ", sense_buffer[k]);
|
||
}
|
||
printf("\n");
|
||
}
|
||
if (io_hdr.masked_status)
|
||
printf("USB Write-> SCSI status=0x%x\n", io_hdr.status);
|
||
if (io_hdr.host_status)
|
||
printf("USB Write-> host_status=0x%x\n", io_hdr.host_status);
|
||
if (io_hdr.driver_status)
|
||
printf("USB Write-> driver_status=0x%x\n", io_hdr.driver_status);
|
||
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int USBSCSIRead(int sg_fd, unsigned char * pCDB,int nCDBLen, unsigned char * pData,unsigned int * nLength,int nTimeOut)
|
||
{
|
||
unsigned char sense_buffer[32];
|
||
sg_io_hdr_t io_hdr;
|
||
|
||
if(sg_fd < 0)
|
||
{
|
||
perror(" fd invalid 555");
|
||
return -1;
|
||
}
|
||
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
|
||
io_hdr.interface_id = 'S';
|
||
io_hdr.cmd_len = nCDBLen; //<2F><><EFBFBD><EFBFBD><EEB3A4>
|
||
/* io_hdr.iovec_count = 0; */ /* memset takes care of this */
|
||
io_hdr.mx_sb_len = sizeof(sense_buffer); //max sense buffer length(һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>)
|
||
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>?
|
||
io_hdr.dxfer_len = *nLength; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||
io_hdr.dxferp = pData; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
io_hdr.cmdp = pCDB; //<2F><><EFBFBD><EFBFBD>
|
||
io_hdr.sbp = sense_buffer; //<2F>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><F3B7B5BB><EFBFBD>Ϣ
|
||
io_hdr.timeout = nTimeOut; /* 20000 millisecs == 20 seconds */
|
||
/* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
|
||
/* io_hdr.pack_id = 0; */
|
||
/* io_hdr.usr_ptr = NULL; */
|
||
|
||
if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
|
||
{
|
||
perror("USB Read-> SG_IO ioctl error");
|
||
|
||
return -1;
|
||
}
|
||
|
||
/* now for the error processing */
|
||
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
|
||
{
|
||
if (io_hdr.sb_len_wr > 0)
|
||
{
|
||
printf("USB Read-> sense data: ");
|
||
int k;
|
||
for (k = 0; k < io_hdr.sb_len_wr; ++k)
|
||
{
|
||
if ((k > 0) && (0 == (k % 10)))
|
||
printf("\n ");
|
||
printf("0x%02x ", sense_buffer[k]);
|
||
}
|
||
printf("\n");
|
||
}
|
||
if (io_hdr.masked_status)
|
||
printf("USB Read SCSI status=0x%x\n", io_hdr.status);
|
||
if (io_hdr.host_status)
|
||
printf("USB Read host_status=0x%x\n", io_hdr.host_status);
|
||
if (io_hdr.driver_status)
|
||
printf("USB Read driver_status=0x%x\n", io_hdr.driver_status);
|
||
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
//----------do with package---------------------------
|
||
/*
|
||
func: send data and command
|
||
para:
|
||
return: success-RT_OK
|
||
fail-RT_FAIL
|
||
*/
|
||
int CD_SendPackage(int sg_fd, TPCCmd tPCCmd, unsigned char * pData)
|
||
{
|
||
unsigned char CDB[12];
|
||
CDB[0] = 0xFE;
|
||
CDB[1] = 0x01;
|
||
memcpy(CDB + 2, &tPCCmd, sizeof(tPCCmd));
|
||
|
||
if(USBSCSIWrite(sg_fd,CDB, 12, pData, tPCCmd.LC, SCSI_TIMEOUT) < 0)
|
||
return -1;
|
||
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
func: receive status and data package
|
||
para:
|
||
return: success-RT_OK
|
||
fail-RT_FAIL
|
||
*/
|
||
int CD_RecvPackage(int sg_fd, unsigned char * pData, unsigned int * lRecvLen)
|
||
{
|
||
unsigned char CDB[12]={0};
|
||
|
||
CDB[0]=0xFE;
|
||
CDB[1]=0x02;
|
||
|
||
if(USBSCSIRead(sg_fd,CDB,10,pData, lRecvLen, SCSI_TIMEOUT) < 0)
|
||
return -1;
|
||
|
||
return 0;
|
||
}
|
||
#if 0
|
||
int OpenUDisk(int id, int * fd)
|
||
{
|
||
char dev_path[12];
|
||
//char outbuf[128];
|
||
|
||
int sg_fd, k;
|
||
//for(i = 0; i < 10; i++)
|
||
{
|
||
sprintf(dev_path, "/dev/sr0");
|
||
if((sg_fd = open(dev_path, O_RDONLY)) < 0)
|
||
{
|
||
printf(dev_path);
|
||
printf(" open fail ret:%d\n",sg_fd);
|
||
return 0;
|
||
}
|
||
|
||
/* It is prudent to check we have a sg device by trying an ioctl */
|
||
if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000))
|
||
{
|
||
printf("%s is not an sg device, or old sg driver\n", dev_path);
|
||
close(sg_fd);
|
||
return 0;
|
||
}
|
||
|
||
// if(scsi_inquiry(sg_fd, (unsigned char *)outbuf) == 0)
|
||
{
|
||
// if(memcmp(outbuf + 8, "Sydtec", 6) == 0) //find ukey
|
||
{
|
||
*fd = sg_fd;
|
||
// return 1;
|
||
}
|
||
// else
|
||
{
|
||
// puts(outbuf);
|
||
// printf("compare sydtec fail\n");
|
||
}
|
||
}
|
||
// close(sg_fd);
|
||
}
|
||
return 0;
|
||
}
|
||
#else
|
||
int OpenUDisk(int id, int * fd)
|
||
{
|
||
char dev_path[12];
|
||
char outbuf[128];
|
||
|
||
int sg_fd, k;
|
||
//for(i = 0; i < 10; i++)
|
||
{
|
||
sprintf(dev_path, "/dev/sr0");
|
||
if((sg_fd = open(dev_path, O_RDONLY)) < 0)
|
||
{
|
||
printf("%s\n",dev_path);
|
||
printf(" open fail ret:%d\n",sg_fd);
|
||
return 0;
|
||
}
|
||
|
||
/* It is prudent to check we have a sg device by trying an ioctl */
|
||
if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000))
|
||
{
|
||
printf("%s is not an sg device, or old sg driver\n", dev_path);
|
||
close(sg_fd);
|
||
return 0;
|
||
}
|
||
|
||
if(scsi_inquiry(sg_fd, (unsigned char *)outbuf) == 0)
|
||
{
|
||
if(memcmp(outbuf + 8, "SUNYARD", 6) == 0) //find ukey
|
||
{
|
||
*fd = sg_fd;
|
||
return 1;
|
||
}
|
||
else
|
||
{
|
||
close(sg_fd);
|
||
return 0;
|
||
}
|
||
}
|
||
close(sg_fd);
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
|
||
#endif
|
||
int CloseUsb(int fd)
|
||
{
|
||
|
||
if(fd > 0)
|
||
{
|
||
printf("\nCloseUsb \n");
|
||
close(fd);
|
||
}
|
||
|
||
return 1;
|
||
}
|
||
|