天天看点

驱动开发起步

驱动开发起步

看了近半个月的书,有了一小点的进步。主要是熟悉了一下驱动开发的环境和配置,USB的协议传输等等。下面是这段时间的体会。以开发一个简单的"hello world"为例。

1、先装VC,然后装DDK,我装的是2000DDK,这个顺序不要弄反。

2、配置好环境(1)在setting->link->libary modules添加ntoskrnl.lib hal.lib usbd.lib wdm.lib 。可能不需要这么多。(2)在setting->C/C++->category选preprocessor->additional include directories中添加c:/NTDDK/inc,c:/NTDDK/inc/ddk(3)在option中添加合适的lib,include路径.option->directories选include files添加C:/NTDDK/INC和C:/NTDDK/INC/DDK,在选library files中添加C:/NTDDK/libchk/i386和C:/NTDDK/libfre/i386。整个环境基本上就搭建好了。

3、然后添加代码。

//1.c

#ifdef _cplusplus

extern "C"

{

#endif

#include "1.h"

#ifdef _cplusplus

}

#endif

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,

      IN PUNICODE_STRING RegistryPath)

{

 DriverObject->DriverExtension->AddDevice = WDMAddDevice;

 DriverObject->MajorFunction[IRP_MJ_PNP] = WDMPnp;

 return STATUS_SUCCESS;

}

NTSTATUS WDMAddDevice(IN PDRIVER_OBJECT DriverObject,

       IN PDEVICE_OBJECT PhysicalDeviceObject)

{

 NTSTATUS status;

 PDEVICE_OBJECT fdo;

 PDEVICE_EXTENSION dx;

 status = IoCreateDevice(

  DriverObject,

  sizeof(DEVICE_EXTENSION),

  NULL,

  FILE_DEVICE_UNKNOWN,

  0,

  FALSE,

  &fdo);

 if(!NT_SUCCESS(status))

  return status;

 dx = (PDEVICE_EXTENSION)fdo->DeviceExtension;

 dx->fdo=fdo;

 dx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo,PhysicalDeviceObject);

 fdo->Flags |= DO_BUFFERED_IO|DO_POWER_PAGABLE;

 fdo->Flags &= ~DO_DEVICE_INITIALIZING;

 return STATUS_SUCCESS;

}

NTSTATUS WDMPnp( IN PDEVICE_OBJECT fdo,

    IN PIRP Irp)

{

 PDEVICE_EXTENSION dx = (PDEVICE_EXTENSION)fdo->DeviceExtension;

 PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);

 NTSTATUS status;

 ULONG MinorFunction = IrpStack->MinorFunction;

 IoSkipCurrentIrpStackLocation(Irp);

 status = IoCallDriver(dx->NextStackDevice,Irp);

 if( MinorFunction==IRP_MN_REMOVE_DEVICE)

 {

  IoSetDeviceInterfaceState(&dx->ifSymLinkName,FALSE);

  RtlFreeUnicodeString(&dx->ifSymLinkName);

  if(dx->NextStackDevice)

   IoDetachDevice(dx->NextStackDevice);

  IoDeleteDevice(fdo);

 }

 return status;

}

1.h

#ifdef _cplusplus

extern "C"

{

#endif

#include "ntddk.h"

#ifdef _cplusplus

}

#endif

typedef struct _DEVICE_EXTENSION

{

    PDEVICE_OBJECT    fdo;

    PDEVICE_OBJECT    NextStackDevice;

    UNICODE_STRING    ifSymLinkName;

} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

NTSTATUS WDMAddDevice(IN PDRIVER_OBJECT DriverObject,

       IN PDEVICE_OBJECT PhysicalDeviceObject);

NTSTATUS WDMPnp( IN PDEVICE_OBJECT fdo,

    IN PIRP Irp);

4、在文件中添加makefile和source,makefile从其它驱动程序中就可以原封不动的考过来,source需要改一点,我的source是:

TARGETNAME=1

TARGETTYPE=DRIVER

DRIVERTYPE=WDM

TARGETPATH=OBJ

INCLUDES=$(BASEDIR)/inc;/

         $(BASEDIR)/inc/ddk;/

TARGETLIBS=$(DDKROOT)/libfre/i386/ntoskrnl.lib/

           $(DDKROOT)/libfre/i386/hal.lib/

           $(DDKROOT)/libfre/i386/wdm.lib

SOURCES=1.c/

makefile为(不用改地):

#

# DO NOT EDIT THIS FILE!!!  Edit ./sources. if you want to add a new source

# file to this component.  This file merely indirects to the real make file

# that is shared by all the driver components of the Windows NT DDK

#

!INCLUDE $(NTMAKEENV)/makefile.def

5、编译,用DDK中的checked(free) build environment来编译。进入控制台后执行build -cz,其中如果编译不通可以用nmake来调试。

6、build完成后,就会生成一个1.sys文件。

7、给设备写inf文件,即安装驱动,inf文件可以从其它驱动文件中拷贝过来,做点小改动就可以了。下面是我写的,

[Version]

Signature="$Chicago$"

Class=Unknown

Provider=%THU%

[Manufacturer]

%THU% = WdmDriver

[WdmDriver]

%WdmDriver%=WdmDriver.Install, *WdmDriver

[DestinationDirs]

WdmDriver.Files.Driver=10, System32/Drivers

WdmDriver.Files.Driver.NT=10, System32/Drivers

[SourceDisksNames]

1="WdmDriver directory",,,obj/i386/

[SourceDisksFiles]

1.sys=1

;; The Win2K DDK documentation contains an excellent INF reference.

;--------- Version Section ---------------------------------------------------

[Version]

Signature="$CHICAGO$"

Provider=LC_Device

DriverVer=8/21/2002,3.0.0.3

; If device fits one of the standard classes, use the name and GUID here,

; otherwise create your own device class and GUID as this example shows.

Class=Unknown

ClassGUID={ff646f80-8def-11d2-9449-00105a075f6b}

;--------- SourceDiskNames and SourceDiskFiles Section -----------------------

; These sections identify source disks and files for installation. They are

; shown here as an example, but commented out.

[SourceDisksNames]

1 = "1",Disk1,,

[SourceDisksFiles]

1.sys = 1,objfre/i386,

;--------- ClassInstall/ClassInstall32 Section -------------------------------

; Not necessary if using a standard class

; 9X Style

[ClassInstall]

Addreg=Class_AddReg

; NT Style

[ClassInstall32]

Addreg=Class_AddReg

[Class_AddReg]

HKR,,,,%DeviceClassName%

HKR,,Icon,,"-5"

;--------- DestinationDirs Section -------------------------------------------

[DestinationDirs]

YouMark_Files_Driver = 10,System32/Drivers

;--------- Manufacturer and Models Sections ----------------------------------

[Manufacturer]

%MfgName%=Mfg0

[Mfg0]

; PCI hardware Ids use the form

; PCI/VEN_aaaa&DEV_bbbb&SUBSYS_cccccccc&REV_dd

;改成你自己的ID

%DeviceDesc%=YouMark_DDI, PCI/VEN_9999&DEV_9999

;---------- DDInstall Sections -----------------------------------------------

; --------- Windows 9X -----------------

; Experimentation has shown that DDInstall root names greater than 19 characters

; cause problems in Windows 98

[YouMark_DDI]

CopyFiles=YouMark_Files_Driver

AddReg=YouMark_9X_AddReg

[YouMark_9X_AddReg]

HKR,,DevLoader,,*ntkern

HKR,,NTMPDriver,,HelloWDM.sys

HKR, "Parameters", "BreakOnEntry", 0x00010001, 0

; --------- Windows NT -----------------

[YouMark_DDI.NT]

CopyFiles=YouMark_Files_Driver

AddReg=YouMark_NT_AddReg

[YouMark_DDI.NT.Services]

Addservice = HelloWDM, 0x00000002, YouMark_AddService

[YouMark_AddService]

DisplayName = %SvcDesc%

ServiceType = 1 ; SERVICE_KERNEL_DRIVER

StartType = 3 ; SERVICE_DEMAND_START

ErrorControl = 1 ; SERVICE_ERROR_NORMAL

ServiceBinary = %10%/System32/Drivers/1.sys

[YouMark_NT_AddReg]

HKLM, "System/CurrentControlSet/Services/HelloWDM/Parameters",/

"BreakOnEntry", 0x00010001, 0

; --------- Files (common) -------------

[YouMark_Files_Driver]

1.sys

;--------- Strings Section ---------------------------------------------------

[Strings]

ProviderName="Flying L Co.,Ltd."

MfgName="LC Soft"

DeviceDesc="Hello World WDM!"

DeviceClassName="LC_Device"

SvcDesc="???"

对inf文件的一点小注释,:),inf文件是按照部分来划分的,每个部分用[]来标记。分号;表示注释,%表示(%文本%)表示一个字符串。

一个最基本的起步,未来还有很多工作要做,继续努力!

继续阅读