|
参照别人的读写硬盘的代码,我写了一段代码,但是没有读取到U盘扇区,大家给看看
#pragma LOCKEDCODE
NTSTATUS DispatchForSCSI(IN PDEVICE_OBJECT fido, IN PIRP Irp)
{
KdPrint((DRIVERNAME " - Enter DispatchForSCSI \n"));
//获得设备扩展
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;
//获得I/O堆栈
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
PDEVICE_OBJECT DeviceObject = pdx->DeviceObject;
NTSTATUS status;
if (_wcsnicmp(DeviceObject->AttachedDevice->DriverObject->DriverName.Buffer,L"\\Driver\\USBSTOR",15)==0)
{
CHAR buf[512];
RtlZeroMemory(buf,sizeof(buf));
//RtlCopyMemory(buf,"HT",2);
status = AtapiReadWriteDisk(fido,IRP_MJ_READ,buf,1,1,Irp);
if (NT_SUCCESS(status))
{
Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
Irp->IoStatus.Information = 0;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return STATUS_ACCESS_DENIED;
}
//获取自旋锁
status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
//判断是否成功获取自旋锁
if (!NT_SUCCESS(status))
//结束IRP请求
return CompleteRequest(Irp, status, 0);
//略过当前I/O堆栈
IoSkipCurrentIrpStackLocation(Irp);
//调用底层驱动程序
status = IoCallDriver(pdx->LowerDeviceObject, Irp);
//释放自旋锁
IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
ExFreePool(buf);
return status;
}
}
ULONG AtapiReadWriteDisk(PDEVICE_OBJECT dev_object,ULONG MajorFunction, PVOID buffer,ULONG DiskPos, int BlockCount,PIRP Irp)
{
NTSTATUS status;
PSCSI_REQUEST_BLOCK srb;
PSENSE_DATA sense;
KEVENT Event;
//PIRP irp;
PMDL mdl;
IO_STATUS_BLOCK isb;
PIO_STACK_LOCATION isl;
PVOID psense;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) dev_object->DeviceExtension;
int count=8;
while(1)
{
srb = (PSCSI_REQUEST_BLOCK)ExAllocatePool(NonPagedPool,sizeof(SCSI_REQUEST_BLOCK));
if(!srb)
break;
sense = (PSENSE_DATA)ExAllocatePool(NonPagedPool,sizeof(SENSE_DATA));
psense=sense;
if(!sense)
break;
memset(srb,0,sizeof(SCSI_REQUEST_BLOCK));
memset(sense,0,sizeof(SENSE_DATA));
//更多关于srb,请看《SCSI 总线和IDE接口:协议、应用和编程》和《SCSI程序员指南》
srb->Length=sizeof(SCSI_REQUEST_BLOCK);
srb->Function=0;
srb->DataBuffer=buffer;
srb->DataTransferLength=BlockCount<<9;//sector size*number of sector
srb->QueueAction=SRB_FLAGS_DISABLE_AUTOSENSE;
srb->SrbStatus=0;
srb->ScsiStatus=0;
srb->NextSrb=0;
srb->SenseInfoBuffer=sense;
srb->SenseInfoBufferLength=sizeof(SENSE_DATA);
if(MajorFunction==IRP_MJ_READ)
srb->SrbFlags=SRB_FLAGS_DATA_IN;
else
srb->SrbFlags=SRB_FLAGS_DATA_OUT;
if(MajorFunction==IRP_MJ_READ)
srb->SrbFlags|=SRB_FLAGS_ADAPTER_CACHE_ENABLE;
srb->SrbFlags|=SRB_FLAGS_DISABLE_AUTOSENSE;
srb->TimeOutValue=(srb->DataTransferLength>>10)+1;
srb->QueueSortKey=DiskPos;
srb->CdbLength=10;
srb->Cdb[0]=2*((UCHAR)MajorFunction+ 17);
srb->Cdb[1]=srb->Cdb[1] & 0x1F | 0x80;
srb->Cdb[2]=(unsigned char)(DiskPos>>0x18)&0xFF; //
srb->Cdb[3]=(unsigned char)(DiskPos>>0x10)&0xFF; //
srb->Cdb[4]=(unsigned char)(DiskPos>>0x08)&0xFF; //
srb->Cdb[5]=(UCHAR)DiskPos; //填写sector位置
srb->Cdb[7]=(UCHAR)BlockCount>>0x08;
srb->Cdb[8]=(UCHAR)BlockCount;
//By:Eros412
//KeInitializeEvent(&Event, 0, 0);
KeInitializeEvent(&Event, NotificationEvent, FALSE);
//irp=IoAllocateIrp(dev_object->StackSize,0);
mdl=IoAllocateMdl(buffer, BlockCount<<9, 0, 0, Irp);
Irp->MdlAddress=mdl;
if(!mdl)
{
ExFreePool(srb);
ExFreePool(psense);
IoFreeIrp(Irp);
return STATUS_INSUFFICIENT_RESOURCES;
}
//MmProbeAndLockPages(mdl,0,(MajorFunction==IRP_MJ_READ?0:1));
MmProbeAndLockPages(mdl,0,IoReadAccess);
srb->OriginalRequest=Irp;
Irp->UserIosb=&isb;
Irp->UserEvent=&Event;
Irp->IoStatus.Status=0;
Irp->IoStatus.Information=0;
Irp->Flags=IRP_SYNCHRONOUS_API|IRP_NOCACHE;
Irp->AssociatedIrp.SystemBuffer=0;
Irp->Cancel=0;
Irp->RequestorMode=0;
Irp->CancelRoutine=0;
Irp->Tail.Overlay.Thread=PsGetCurrentThread();
isl=IoGetNextIrpStackLocation(Irp);
//isl->DeviceObject=dev_object;
//isl->MajorFunction=IRP_MJ_SCSI;
isl->Parameters.Scsi.Srb=srb;
isl->CompletionRoutine=IrpCompletionRoutine_0;
//isl->Context=srb;
//isl->Control=SL_INVOKE_ON_CANCEL|SL_INVOKE_ON_SUCCESS|SL_INVOKE_ON_ERROR;
// //获取自旋锁
// status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
// //判断是否成功获取自旋锁
// if (!NT_SUCCESS(status))
// //结束IRP请求
// return CompleteRequest(irp, status, 0);
// //略过当前I/O堆栈
// IoSkipCurrentIrpStackLocation(irp);
// //调用底层驱动程序
// status = IoCallDriver(pdx->LowerDeviceObject, Irp);
// //释放自旋锁
// IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
status=IoCallDriver(pdx->LowerDeviceObject,Irp);
//KeWaitForSingleObject(&Event, 0, 0, 0, 0);
KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, 0);
if(srb->SenseInfoBuffer!=psense&&srb->SenseInfoBuffer)
ExFreePool(srb->SenseInfoBuffer);
ExFreePool(srb);
ExFreePool(psense);
if ( status >= 0 || !count )
return status;
DbgPrint("Send XXX Failed..%08x\r\n", status);
KeStallExecutionProcessor(1u);
--count;
}
return STATUS_INSUFFICIENT_RESOURCES;
} |
|