admin管理员组文章数量:1654383
使用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/yuesichiu/article/details/40535539
版权声明:本文标题:使用HDIDE layer的ioctl接口获取磁盘详细信息device_identify 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729650453a1208976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论