您的位置:首页 > 其它

使用HD/IDE layer的ioctl接口获取磁盘详细信息device_identify

2014-10-28 09:26 666 查看
使用HD/IDE layer的ioctl接口获取磁盘详细信息device_identify。HDIO_DRIVE_CMD 能读数据,可以以nsector * 512字节的形式读取,但不能发出访问LBA地址的命令。

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include <linux/hdreg.h>

#include <linux/types.h>

#include <sys/stat.h>

#include <scsi/sg.h>

#include <scsi/scsi.h>

#include <sys/ioctl.h>

#include <linux/fs.h>

#include <fcntl.h>

#include <errno.h>

#include <sys/time.h>

 

#define ATA_DRQ             (1 << 3)    /* data request i/o */

#define ATA_ERR             (1 << 0) /* have an error */

#define DRIVER_SENSE        0x08

#define CONDITION_GOOD      0x02

#define ATA_PASS_THRU_12    12

#define ATA_12              0xa1

#define ATA_PASS_THRU_16    16

#define ATA_16              0x85

#define CMD_NO_DATA         (3 << 1)

#define FOR_SENSE_DATA      0x20

#define DEVICE_IDENTIFY     0xec

 

#define u64        unsigned long long

#define u32        unsigned int

#define u8        unsigned char

 

static u64 get_disk_size(const char*name)

{

   const char *ptr = name + strlen(name) - 3;

   u64 size;

   char buff[128];

   FILE *fp;

   

   sprintf(buff,"/sys/block/%s/size",ptr);

 

   if(NULL == (fp = fopen(buff,"r"))){

       perror("fopen");

       return 0;

   }

   fscanf(fp,"%lld",&size);

   fclose(fp);

   

   return size;

}

 

/*!

    IssuingATA commands 

*/

int ata_ioctl(int fd)

{

   unsigned char cdb[4+512];

   int err = 0;

    inti = 0;

 

  

    cdb[0]= DEVICE_IDENTIFY;   // command id

    cdb[1]= 0x00;  // number of sectors

   cdb[2] = 0x00; // feature id

    cdb[3]= 0x01;  // number of sectors

 

   

   if (-1 == (err = ioctl(fd, HDIO_DRIVE_CMD, (void *)cdb))) {

        perror("HDIO_DRIVE_TASK erro inata_ioctl!\n");

       return -1;

   }

    printf("status %02x\n", cdb[0]); 

    printf("error  %02x\n", cdb[1]);

    printf("n sect %02x\n", cdb[2]);

    //printf("undefined  %02x\n", cdb[3]);

    printf("incomingdata : \n");

    for(; i < 512; i++)

    {  

        if (i % 10 == 0 )

            printf("\n%3d   ",i+1);

        printf(" %02x",cdb[i+4]);

 

    }

    printf("\n");

   return 0;

}

 

int main(int argc, char *argv[])

{

   int fd;

   u64 capacity;

   int size;

   

   if (argc < 2) {

       printf("Usage: ./exec devname size\n");

       printf("    devname:devicename\n");

       return 0;

   }

   if(-1 == (fd = open(argv[1],O_RDWR))){

       perror("open");

       return fd;

   }

   if (ata_ioctl(fd) != 0)

    {

        printf("ata_ioctl error !\n");

        exit(-1);

    }

   close(fd);

   

   return 0;

}

博约代码分享:http://blog.csdn.net/yuesichiu/article/details/40535539
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐