随笔-23  评论-73  文章-3  trackbacks-0

#include <ntifs.h>
#include <ntddscsi.h>
#include <ntdddisk.h>
#include <stdio.h>


 


void PrintIdeInfo (int drive, ULONG diskdata [256]);


char *ConvertToString (ULONG diskdata [256], int firstIndex, int lastIndex);


BOOLEAN getDiskID( PDEVICE_OBJECT DeviceObject,ULONG drive );


 


#define IDENTIFY_BUFFER_SIZE 512


#define  IDE_ATAPI_IDENTIFY  0xA1  //  Returns ID sector for ATAPI.
#define  IDE_ATA_IDENTIFY    0xEC  //  Returns ID sector for ATA.


#define  DFP_RECEIVE_DRIVE_DATA   0x0007c088


 


//输出地址
char IdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];


 


BOOLEAN getDiskID( PDEVICE_OBJECT DeviceObject,ULONG drive )
/*
* DeviceObject:物理PDO
* drive:磁盘设备号
*/
{
    PUCHAR buffer;
    PSRB_IO_CONTROL srbControl;
    ULONG         controlCode = 0;
    PIRP irp2;
    KEVENT event;
    IO_STATUS_BLOCK ioStatus;   
    NTSTATUS status;
    ULONG length;
 PSENDCMDINPARAMS cmdInParameters;
 
 SENDCMDINPARAMS  scip;


 memset (&scip, 0, sizeof(scip));
    memset (IdOutCmd, 0, sizeof(IdOutCmd));
 scip.cBufferSize = IDENTIFY_BUFFER_SIZE;
 scip.irDriveRegs.bFeaturesReg = 0;
 scip.irDriveRegs.bSectorCountReg = 1;
 scip.irDriveRegs.bSectorNumberReg = 1;
 scip.irDriveRegs.bCylLowReg = 0;
 scip.irDriveRegs.bCylHighReg = 0;


   // Compute the drive number.
 scip.irDriveRegs.bDriveHeadReg = 0xA0 | (((UCHAR)drive & 1) << 4);


   // The command can either be IDE identify or ATAPI identify.
 scip.irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
 scip.bDriveNumber = (UCHAR)drive;
 scip.cBufferSize = IDENTIFY_BUFFER_SIZE;


    //
    // 发DeviceIo,IdOutCmd接收结果
    //
    irp2 = IoBuildDeviceIoControlRequest(DFP_RECEIVE_DRIVE_DATA,
                            DeviceObject,
                            &scip,
                            sizeof(SENDCMDINPARAMS) - 1,
                            IdOutCmd,
                            sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
                            FALSE,
                            &event,
                            &ioStatus);


    if (irp2 == NULL) {
        DbgPrint("Build DeviceIoControl Error \n");
  return FALSE;
    }


    //
    // 向设备发Irp
    //


    status = IoCallDriver(DeviceObject, irp2);


    if (status == STATUS_PENDING) {
        KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
        status = ioStatus.Status;
    }


    //
    // 发送成功打印信息
    //
   
    if (NT_SUCCESS(status)) {
          ULONG diskdata [256];
          int ijk = 0;
          USHORT *pIdSector = (USHORT *)
                             ((PSENDCMDOUTPARAMS) IdOutCmd) -> bBuffer;
    DbgPrint("successs in read disk id \n");   


          for (ijk = 0; ijk < 256; ijk++)
            diskdata [ijk] = pIdSector [ijk];
       
     //打印函数
        PrintIdeInfo (drive, diskdata);
  //这要调用完成例程而不是释放IRP,具体请看DDK文档
  IoCompleteRequest( irp2,IO_NO_INCREMENT );
        return TRUE;    
         
    } else {
       DbgPrint("get disk id error\n");
   
    }
   
    return FALSE;
}


void PrintIdeInfo (int drive, ULONG diskdata [256])
{
 char string1 [1024];


 strcpy (string1, ConvertToString (diskdata, 10, 19));


 DbgPrint("Disk NO. %d DiskID %s \n",drive,ConvertToString (diskdata, 10, 19));
}


char *ConvertToString (ULONG diskdata [256], int firstIndex, int lastIndex)
{
/*
* 功能如下:高低字节对调,结尾补'\0'
* string[10-19]遇空结尾
*/
   static char string [1024];
   int index = 0;
   int position = 0;


      //  each integer has two characters stored in it backwards
   for (index = firstIndex; index <= lastIndex; index++)
   {
         //  高字节
      string [position] = (char) (diskdata [index] / 256);
      position++;


         //  低字节
      string [position] = (char) (diskdata [index] % 256);
      position++;
   }


      //  结尾
   string [position] = '\0';
     
   for (index = position - 1; index > 0 && ' ' == string [index]; index--)
      string [index] = '\0';


   return string;
}

posted on 2008-04-19 13:05 ViskerWong 阅读(1358) 评论(2)  编辑 收藏 引用

评论:
# re: 得到物理磁盘序列号 2008-04-19 16:57 | Lenuns
驱动的代码,ms不完整啊  回复  更多评论
  
# re: 得到物理磁盘序列号 2008-04-21 14:59 | ViskerWong
想看完整的代码可以找Diskid32的源代码
这只是一个库  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理