?? driver.cpp
字號:
#if DBG>=1
DbgPrint("WMIEX: DpWmiQuueryRegInfo\n");
#endif
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
*pRegFlags = WMIREG_FLAG_INSTANCE_PDO;
*pRegistryPath = ®Path;
// MofResourceName is whatever name was used in the
// Resource (.rc) file for the driver project
RtlInitUnicodeString(MofResourceName, L"MofResource");
// Returning the PDO (Physical Device Object) is needed
// since we are requesting auto-naming based on the PDO
*pPdo = pDevExt->pPDO;
return STATUS_SUCCESS;
}
NTSTATUS
DpWmiQueryDataBlock(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp,
IN ULONG GuidIndex,
IN ULONG InstanceIndex,
IN ULONG InstanceCount,
IN OUT PULONG InstanceLengthArray,
IN ULONG BufferAvail,
OUT PUCHAR pBuffer
) {
#if DBG>=1
DbgPrint("WMIEX: DpWmiQueryDataBlock\n");
DbgPrint("WMIEX: GuidIndex = %d, InstanceIndex = %d\n", GuidIndex, InstanceIndex);
DbgPrint("WMIEX: InstanceCount = %d, InstanceLengthArray = %d\n", InstanceCount, InstanceLengthArray);
DbgPrint("WMIEX: BufferAvail = %d, MOF_DATA_SIZE = %d\n", BufferAvail, MOF_DATA_SIZE);
#endif
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
// Do we have enough room for the MOF data?
if ( MOF_DATA_SIZE > BufferAvail )
return WmiCompleteRequest( pDevObj,
pIrp,
STATUS_BUFFER_TOO_SMALL,
MOF_DATA_SIZE,
IO_NO_INCREMENT );
// Stuff Buffer with gathered MOF data (3 ULONGS)
// Simply cast Buffer as MofData structure
// Then let the compiler copy everything
*(PMOFDATA)pBuffer = pDevExt->mofData;
return WmiCompleteRequest( pDevObj,
pIrp,
STATUS_SUCCESS,
MOF_DATA_SIZE,
IO_NO_INCREMENT );
}
//++
// Function: DispatchCreate
//
// Description:
// Handles call from Win32 CreateFile request
// For this driver, does nothing
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchCreate (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("WMIEX: DispatchCreate\n");
#endif
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
NTSTATUS status = STATUS_SUCCESS;
if (pDevExt->state != Started)
status = STATUS_DEVICE_REMOVED;
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return status;
}
//++
// Function: DispatchClose
//
// Description:
// Handles call from Win32 CreateHandle request
// For this driver, frees any buffer
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchClose (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("WMIEX: DispatchClose\n");
#endif
// Dig out the Device Extension from the Device object
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
if (pDevExt->deviceBuffer != NULL) {
ExFreePool(pDevExt->deviceBuffer);
pDevExt->deviceBuffer = NULL;
pDevExt->deviceBufferSize = 0;
}
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
//++
// Function: DispatchCancel
//
// Description:
// Handles canceled IRP
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
VOID DispatchCancel (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("WMIEX: IRP Canceled\n");
#endif
// Just complete the IRP
pIrp->IoStatus.Status = STATUS_CANCELLED;
pIrp->IoStatus.Information = 0; // bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
IoStartNextPacket( pDevObj, TRUE );
}
//++
// Function: DispatchWrite
//
// Description:
// Handles call from Win32 WriteFile request
// For this driver, starts the device by indirectly
// invoking StartIo
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
NTSTATUS DispatchWrite (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("WMIEX: Write Operation requested (DispatchWrite)\n");
#endif
// Update MOF data
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
pDevExt->mofData.totalWrites++;
// Start the I/O
IoMarkIrpPending( pIrp );
IoStartPacket( pDevObj, pIrp, 0, DispatchCancel);
return STATUS_PENDING;
}
//++
// Function: DispatchRead
//
// Description:
// Handles call from Win32 ReadFile request
// For this driver, xfers pool buffer to user
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
NTSTATUS DispatchRead (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("WMIEX: Read Operation requested (DispatchRead)\n");
#endif
// Update MOF data
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
pDevExt->mofData.totalReads++;
NTSTATUS status = STATUS_SUCCESS;
PVOID userBuffer;
ULONG xferSize;
// The stack location contains the user buffer info
PIO_STACK_LOCATION pIrpStack =
IoGetCurrentIrpStackLocation( pIrp );
// Determine the length of the request
xferSize = pIrpStack->Parameters.Read.Length;
// Obtain user buffer pointer
userBuffer = pIrp->AssociatedIrp.SystemBuffer;
// Don't transfer more than the user's request
xferSize = (xferSize < pDevExt->deviceBufferSize) ?
xferSize : pDevExt->deviceBufferSize;
// Now copy the pool buffer into user space
RtlCopyMemory( userBuffer, pDevExt->deviceBuffer,
xferSize );
// Free the temporary pool buffer
ExFreePool( pDevExt->deviceBuffer );
pDevExt->deviceBuffer = NULL;
pDevExt->deviceBufferSize = 0;
// Now complete the IRP
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = xferSize; // bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return status;
}
//++
// Function:
// TransmitByte
//
// Description:
// This function sends one character to the device
// The polling timer is then started which will
// eventually invoke the PollingTimerDpc.
// If no characters remain to be transmitted, return FALSE.
//
// Arguments:
// Pointer to the Device Extension
//
// Return Value:
// TRUE - one more byte transmitted
// FALSE - no more bytes remain to be transmitted
//--
BOOLEAN TransmitByte(
IN PVOID pArg ) {
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pArg;
// If all the bytes have been sent, just quit
if( pDevExt->xferCount >= pDevExt->maxXferCount)
return FALSE;
// A transfer is happening.
PDEVICE_OBJECT pDevObj =
pDevExt->pDevice;
PIRP pIrp = pDevObj->CurrentIrp;
// Obtain user buffer pointer
PUCHAR userBuffer = (PUCHAR)
pIrp->AssociatedIrp.SystemBuffer;
UCHAR nextByte =
userBuffer[pDevExt->xferCount];
// Now send it...
#if DBG>=1
DbgPrint("WMIEX: TransmitByte: Sending 0x%02X to port %X\n",
nextByte, pDevExt->portBase);
#endif
// Update MOF data
pDevExt->mofData.totalTransfers++;
// The loopback connector requires us to do
// some bizzare work.
// Bit 0 of the data is sent as bit 0 of Port data.
WriteData( pDevExt, nextByte & 1);
// It will be read in Status bit 3
// Bits 1-3 of the data are sent as bits 0,1, & 3
// of the Control register.
// Control bits 0 & 1 are inverted.
UCHAR bits = (nextByte & 0x8) +
((nextByte & 0x6)>>1);
bits ^= 0x3; // Invert the 2 control bits (0 & 1)
// These bits will be read in Status bits 4,5, & 7
WriteControl( pDevExt, bits | CTL_DEFAULT);
// Now read the data from the loopback
UCHAR status =
ReadStatus( pDevExt );
// Format the nibble into the upper half of the byte
UCHAR readByte =
((status & 0x8)<< 1) |
((status & 0x30)<<1) |
(status & 0x80);
pDevExt->deviceBuffer[pDevExt->xferCount++] =
readByte;
#if DBG>=1
DbgPrint("WMIEX: TransmitByte read character: 0x%03X\n",
readByte);
#endif
// Update MOF data
pDevExt->mofData.totalTransfers++;
// Start the polling timer
#if DBG>=2
DbgPrint("WMIEX: TransmitByte starting timer\n");
#endif
KeSetTimer(
&pDevExt->pollingTimer,
pDevExt->pollingInterval,
&pDevExt->pollingDPC );
return TRUE;
}
//++
// Function:
// StartIo
//
// Description:
// This function is responsible initiating the
// actual data transfer. The ultimate result
// should be a started Timer object.
//
// Arguments:
// Pointer to the Device object
// Pointer to the IRP for this request
//
// Return Value:
// (None)
//--
VOID
StartIo(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp
) {
#if DBG>=1
DbgPrint("WMIEX: Start I/O Operation\n");
#endif
PIO_STACK_LOCATION pIrpStack =
IoGetCurrentIrpStackLocation( pIrp );
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
PUCHAR userBuffer;
ULONG xferSize;
switch( pIrpStack->MajorFunction ) {
// Use a SynchCritSection routine to
// start the write operation...
case IRP_MJ_WRITE:
// Set up counts and byte pointer
pDevExt->maxXferCount =
pIrpStack->Parameters.Write.Length;
pDevExt->xferCount = 0;
// Since we processing a new Write request,
// free up any old buffer
if (pDevExt->deviceBuffer != NULL) {
ExFreePool(pDevExt->deviceBuffer);
pDevExt->deviceBuffer = NULL;
pDevExt->deviceBufferSize = 0;
}
// Determine the length of the request
xferSize =
pIrpStack->Parameters.Write.Length;
// Obtain user buffer pointer
userBuffer = (PUCHAR)
pIrp->AssociatedIrp.SystemBuffer;
// Allocate the new buffer
pDevExt->deviceBuffer = (PUCHAR)
ExAllocatePool( PagedPool, xferSize );
if (pDevExt->deviceBuffer == NULL) {
// buffer didn't allocate???
// fail the IRP
pIrp->IoStatus.Status =
STATUS_INSUFFICIENT_RESOURCES;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
IoStartNextPacket( pDevObj, FALSE );
}
pDevExt->deviceBufferSize = xferSize;
// Try to send the first byte of data.
#if DBG>=1
DbgPrint("WMIEX: StartIO: Transmitting first byte of %d\n", pDevExt->deviceBufferSize);
#endif
TransmitByte( pDevExt );
break;
//
// Should never get here -- just get rid
// of the packet...
//
default:
pIrp->IoStatus.Status =
STATUS_NOT_SUPPORTED;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(
pIrp,
IO_NO_INCREMENT );
IoStartNextPacket( pDevObj, FALSE );
break;
}
}
//++
// Function:
// PollingTimerDpc
//
// Description:
// This function is the DPC routine called
// each time the timer "ticks".
// The routine checks on the device -
// if room available, more data is sent
//
// Arguments:
// Pointer to the Device object
// Pointer to the IRP for this request
//
// Return Value:
// (None)
//--
VOID PollingTimerDpc( IN PKDPC pDpc,
IN PVOID pContext,
IN PVOID SysArg1,
IN PVOID SysArg2 ) {
#if DBG>=1
DbgPrint("WMIEX: PollingTimerDpc Fired\n");
#endif
PDEVICE_OBJECT pDevObj = (PDEVICE_OBJECT)
pContext;
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
// Try to send more data
if (!TransmitByte( pDevExt ) ) {
// Transfer complete (normal or error)
// Complete the IRP appropriately
#if DBG>=1
DbgPrint("WMIEX: PollingTimerDpc Completing IRP\n");
#endif
PIRP pIrp = pDevObj->CurrentIrp;
pIrp->IoStatus.Information =
pDevExt->xferCount;
// Figure out what the final status should be
pIrp->IoStatus.Status = STATUS_SUCCESS;
// If an error occurred, Status would change
// Now Complete the IRP
IoCompleteRequest( pIrp, IO_PARALLEL_INCREMENT );
// And request another IRP
IoStartNextPacket( pDevObj, FALSE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -