Title: Ch 6. Initialization and Cleanup Routines
1Ch 6. Initialization andCleanup Routines
2Contents
- Writing a DriverEntry Routine
- Code Example Driver Initialization
- Writing Reinitialize Routine
- Writing an Unload Routine
- Code Example Driver Unload
- Writing Shutdown Routine
- Testing the driver
- Summary
31. Writing a DriverEntry Routine
4DriverEntry Routine
- ?? Windows 2000? ?? ?? ????? WDM????? ? ??? ????
DriverEntry Routine? ??. - ??? ??? ???? ???
- ?? ?? ????? ?? ???? ?? ??? ??
5DriverEntry Routine(1)
- Function Prototype for DriverEntry
NTSTATUS DriverEntry IRQL PASSIVE LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Address of driver object for this driver
IN PUNICODE_STRING pRegisterPath Registry path string for this drivers key
Return value STATUS_SUCCESS STATUS_XXX some error code
6DriverEntry Routine(2)
- Driver Entry Routine ? ??
- ?????? ??? ????? ??
- Driver Object? ?? Routine?? ??? ??
- Controller ?? , Controller Extension ???
(IoCreateController) - Device Object ??, Device Extension ???
(IoCreateDevice) - Device Object? Win32????? ???? ?
(IoCreateSymbolicLink) - Interrupt Object? ??(DPC Object??)
- 46? ??? ?? device? ?? ?? ??
- Return STATUS_SUCCESS to I/O manager
7Announcing DriverEntry Point
- Function pointer in Driver Object
- ???? slot name? ??? Function
- MajorFunction?? ?? ??? IRP Dispatch function
- Ex) Function pointer initialize
- pDO -gt DriverStartIO StartIO
- pDO -gt DrtverUnload Unload
- //
- // ?? ?? ???? ???? ?? ???
- //
- pDO -gt MajorFunction IRP_MJ_CREATE
DispatchCreate - pDO -gt MajorFunction IRP_MJ_CLOSE
DispatchClose
8Creating Device Object
- IoCreateDevice
- Driver Object? ?? ???? Device List? ??? Device
Object? ?? - AddDevice Routine?? ???? ??
- WDM ? ???? Device
- DriverEntry Routine? 1,36??? ???
- (???? ?? ? ???? ???? ??? ??)
- Dispatch Routine?? IoCreateDevice ? ??? ??
- Device? Flags Field? DO_DEVICE_INITIALIZING bit?
reset - Device? ?? or Reinitializing
9Creating Device Object(1)
- Function Prototype for IoCreateDevice
NTSTATUS IoCreateDevice IRQL PASSIVE LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Pointer to driver object
IN ULONG DeviceExtensionSize DEVICE_EXTENSION? ???? ??
IN PUNICODE_STRING pDeviceName ?? ????? ??(internal name)
IN DEVICE_TYPE DeviceType FILE_DEVICE_XXX (NTDDK.h ??)
IN ULONG DeviceCharacteristics ??? ?? ????? ?? FILE_REMOVABLE_MEDIA FILE_READ_ONLY_DEVICE Etc.
IN BOOLEAN Exclusive Device? ???? ?? ?? ?? True
OUT PDEVICE_OBJECT pDeviceObject Device object? ?? pointer? ?? ??
Return value STATUS_SUCCESS STATUS_XXX some error code
10Buffering Strategy
- Device?? ??? Buffering strategy ? I/O Manager??
?? - Device Flags Field
- DO_BUFFER_I/O
- DO_DIRECT_I/O
- Neither bit - ??? ??? ??
11Device Name
Name Directory Tree
Object Manager
Root
Ex) ?? \\Device\\Minimal0 FloppyDisk0 ,
FloppyDisk1
Ex) Symbolic Link Name \\??\\MINIMAL1 LPT1,
LPT2 , A Z
IoCreateSymbolicLink ( Device Name ,
UNICODE_STRING Symbolic Link Name )
122. Code Example Driver Initialization
13Minimal driver
- Device Name MINIMAL0
- Symbolic Link Name MINI
- DriverEntry
- ?? ??? ???? ??(Announcing)
- Logical Device ??
NTSTATUS DriverEntry ( IN PDRIVER_OBJECT
pDriverObject, IN PUNICODE_STRING
pRegistryPath ) NTSTATUS status
pDriverObject-gtDriverUnload DriverUnload
status CreateDevice ( pDriverObject,
) return status
14Minimal driver (1)
- CreateDevice
- Device Name initialization
- Device Object ??, Device Extension size ??
- Device Extension initialization
- Symbolic Link Name initialization Link
NTSTATUS CreateDevice ( IN PDRIVER_OBJECT
pDriverObject, IN ULONG
ulDeviceNumber) NTSTATUS status PDEVICE_OBJ
ECT pDevObj PDEVICE_EXTENSION pDevExt
CUString devName(\\Device\\MINIMAL) devName
CUString(ulDeviceNumber)
15Minimal driver (2)
status IoCreateDevice ( pDriverObject, )
pDevExt (PDEVICE_EXTENSION)pDevObj-gtDeviceExten
sion pDevExt-gtpDevice pDevObj // Back
pointer pDevExt-gtDeviceNumber
ulDeviceNumber pDevExt-gtustrDeviceName
devName CUString SymLinkName(\\??\\MINI) Sy
mLinkName CUString(ulDeviceNumber1) pDevExt-
gtustrSymLinkName SymLinkName status
IoCreateSymbolicLink( ) //??? ?? Device
Object? ??? ?? return STATUS_SUCCESS
163. Writing Reinitialize Routines
17Reinitialize Routine
- IoRegisterDriverReinitialize
- ???? Bootstrap? ??? ???? ??? ??? ??? ??? ??? ????
- I/O Manage? Bootstrap? ??? Reinitialize routine
?? - Booting?? ???? load?? ????? ??
- ????? ???? ??? ??
- Function Prototype for Reinitialize
VOID Reinitialize IRQL PASSIVE LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Pointer of driver object
IN PVOID Context ??? ??? Context Block
In ULONG Count Reinitialize call? ?? ???(0)
Return value (void)
184. Writing an Unload Routine
19Unload Routine
- ????? Unload?? ?? ?? ?? Unload routine? ??? ??
- I/O Manager? ??, ???? ??(Unload ??)
- Function Prototype for Unload
VOID Unload IRQL PASSIVE LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Pointer of driver object
Return value (void)
20Unload Routine(1)
- Unload Routine ? ??
- ?? load?? ?? ??? ?? Device? ????? registry? ??
- Interrupt disable, disconnect Interrupt object
- Hardware ??
- Remove Symbolic Link Name from Win32 namespace
- IoDeleteDevice Device Object ??
- ?? Controller? ????? 45?? ??, IoDeleteController
Controller Object ?? - 46? ??? ?? Device Controller? ?? ?? ??
- Deallocate pool memory
- WDM? ?? RemoveDevice?? ??
215. Code Example Driver Unload
22Unload Routine(2)
- Driver Entry Routine? ??? ??? ??
- Symbolic Link Name ? Device Object? ??
VOID DriverUnload ( IN PDRIVER_OBJECT
pDriverObject ) PDEVICE_OBJECT pNextObj
pNextobj pDriverObject-gtDeviceObject while
( pNextObj ! NULL ) PDEVICE_EXTENSION
pDevExt (PDEVICE_EXTENSION) pNextObj-gtDeviceExt
ension UNICODE_STRING pLinkName
pDevExt-gtustrSymLinkName IoDeleteSymbolicLink(
pLinkName ) pNextObj pNextObj -gt
NextDevice IoDeleteDevice ( pDevExt-gtpDevice
) //IoReportResourceUsage
236. Writing Shutdown Routines
24Shutdown Routine
- OS? ??? Driver?? ?? ? ???? ??? ??
- Shutdown??? Unload routine? ???? ??
- Device? ???? ??? ?
- Device? ?? ??? ??
- OS? ???? ??? ??? ???
- Function Prototype for Shutdown
NTSTATUS Shutdown IRQL PASSIVE LEVEL
Parameter Description
IN PDRIVER_OBJECT pDriverObject Pointer of driver object
IN PIRP pirp Shutdown IRP? ?? pointer
Return value STATUS_SUCCESS STATUS_XXX
25Shutdown Routine(1)
- ??? I/O??
- MajorFunction ?? ???????? ??
- IoRegisterShutdownNotification Shutdown ??
NTSTATUS DriverEntry ( IN PDRIVER_OBJECT
pDriverObject , IN PUNICODE_STRING
pRegistryPath) pDriverObject-gtMajorFunction
IRP_MJ_SHUTDOWN Shutdown IoCreateDevice(
pDriverObject , , pDeviceObject) IoRegisterShu
tdownNotification ( pDriverObject )
267. Testing the Driver
27Test
- ???? Compile , Link
- ??? ??? ?? load Unload ??
- Device Object ? Win32 Symbolic Link ??
- Unload? ?? ??
28Tool ??
- ?? ?? ?? Minimal.dsw? ??? ? ??
- ??? Minimal.sys? \winnt\system32\drivers? ??
- Regedit? HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlS
et\Service? ???? ??( ex Minimal )? ??? - ?????? ?????
- ErrorControl1(DWORD),Start3(DWORD),Type1(DWORD)
- ?? Minimal.reg? ???
- DisplayName ??? ?? ??? ??? ??
29Test
30Test
31Test
32Test
33Summary
34Summary
- Windows 2000? Device Driver ?? ??? ??
- ????? ?? , ??
- ?? ?? ????? ?? ???