WDM (Windows Driver Model) : Y.C. Hua 2005-04-29
WDM (Windows Driver Model) : Y.C. Hua 2005-04-29
Y.C. Hua
2005-04-29
1
Requirements
2
Driver Model used on various OS
• NT4
KMD (Kernel Model Driver)
• Win95
VxD (Virtual Device Driver)
• Win98/Me
VxD, WDM
• Win2000/XP
KMD, WDM
3
Use driver to access hardware
4
Driver building environment
• Visual studio 6
compiler, linker, editor
• Win98/Me/2000/XP DDK
library, header file for driver
5
Driver loading
6
Static load
1. Copy driver to <windir>\system32\drivers
98/Me/XP <windir> = c:\windows, 2k <windir> = c:\winnt
4. Restart computer
7
Service
• Only on win2k/xp.
• Service is a special program independent from multi-user
session.
• It can be running before user login.
• 2 kind of service. Application service and driver service.
• Driver service is for dynamic load.
8
Application call driver
main()
{
CreateDriverService(); // for dynamic load only
HANDLE h=OpenDriver();
CloseHandle(h);
DeleteDriverService(); // for dynamic load only
}
9
DeviceIoControl()
BOOL DeviceIoControl(
HANDLE hDevice, // in - handle to device
DWORD dwIoControlCode, // in - operation control code
LPVOID lpInBuffer, // in - input data buffer
DWORD nInBufferSize, // in - size of input data buffer
LPVOID lpOutBuffer, // out - output data buffer
DWORD nOutBufferSize, // in - size of output data buffer
LPDWORD lpBytesReturned, // out - byte count
LPOVERLAPPED lpOverlapped // in - overlapped information
);
10
Prepare driver file
11
Build driver
12
Launch build environment of DDK
13
Type ‘build’ command
14
Contains of sources
TARGETNAME=TESTDRV
TARGETTYPE=DRIVER
DRIVERTYPE=WDM
TARGETPATH=.
SOURCES = prog.cpp
15
Control code in devio.h
#define CTL_CODE( DeviceType,Function,Method,Access ) \
(((DeviceType) << 16) | ((Access) << 14) | \
((Function) << 2) | (Method))
16
DriverEntry (main of the driver)
17
Create device/symbolic link in DriverEntry
UNICODE_STRING uniNtNameString;
UNICODE_STRING uniWin32NameString;
// create device
RtlInitUnicodeString(&uniNtNameString, L"\\Device\\TESTDRV");
IoCreateDevice(DriverObject, 0, &uniNtNameString,
FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject);
18
Set major function in DriverEntry
19
Unload function
UNICODE_STRING uniWin32NameString;
RtlInitUnicodeString(&uniWin32NameString,
L"\\DosDevices\\TESTDRV");
IoDeleteSymbolicLink( &uniWin32NameString );
IoDeleteDevice( DriverObject->DeviceObject );
20
DeviceControl funciton
NTSTATUS A::DeviceControl(IN PDEVICE_OBJECT fdo, IN PIRP irp)
{
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
ULONG ControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
ULONG InputLength = irpStack->
Parameters.DeviceIoControl.InputBufferLength;
ULONG OutputLength = irpStack->
Parameters.DeviceIoControl.OutputBufferLength;
PVOID WorkBuffer = (PVOID)irp->AssociatedIrp.SystemBuffer;
switch(ControlCode)
{
case DEVIO_CallInt15: return DoCallInt15(irp);
case DEVIO_Test: return DoTest(irp);
default:
irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
irp->IoStatus.Information = 0; // return byte information
IoCompleteRequest(irp,IO_NO_INCREMENT);
return STATUS_INVALID_DEVICE_REQUEST;
}
}
21
Buffer flow
User mode Kernel mode
Application
Input buffer
Driver
Work buffer
Application
Output buffer
22
Last
Wdmprog.zip
23