fire2fire
驱动牛犊
驱动牛犊
  • 注册日期2004-03-04
  • 最后登录2013-04-07
  • 粉丝0
  • 关注0
  • 积分30分
  • 威望3点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
阅读:1303回复:2

编译WinXP驱动程序

楼主#
更多 发布于:2004-03-05 12:13
做一个BAT文件如下:(假设DDK装在D:\WINDDK)

if "%OS%" == "Windows_NT" goto WinNT

goto end

:WinNT
pushd
call D:\WINDDK\bin\setenv d:\winddk chk
popd
D:\WINDDK\bin\x86\build -cZ
@echo off
rd /s /q obj
rd /s /q objchk
del /f /s /q .\i386\*.pdb
del *.log
pause

:end

把它和MAKEFILE,SOURCES,还有*.c,*.h之类的文件放在一起,运行上面的BAT就OK了~
fire2fire
驱动牛犊
驱动牛犊
  • 注册日期2004-03-04
  • 最后登录2013-04-07
  • 粉丝0
  • 关注0
  • 积分30分
  • 威望3点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-03-05 12:24
以编译porttalk.sys为例子。

建一个目录设为TEST,下面放如下几个文件:(运行make.bat)
make.bat
if "%OS%" == "Windows_NT" goto WinNT

goto end

:WinNT
pushd
call D:\WINDDK\bin\setenv d:\winddk chk
popd
D:\WINDDK\bin\x86\build -cZ
@echo off
rd /s /q obj
rd /s /q objchk
del /f /s /q .\i386\*.pdb
del *.log
pause

:end

***********************************************************
makefile:

!INCLUDE $(NTMAKEENV)\makefile.def

sources:

TARGETNAME=porttalk
TARGETPATH=.
TARGETTYPE=DRIVER

INCLUDES=d:\winddk\inc\wxp;..\

SOURCES=porttalk.c porttalk.rc

porttalk.c:

/******************************************************************************/
/*                                                                            */
/*                    PortTalk Driver for Windows NT/2000/XP                  */
/*                        Version 2.0, 12th January 2002                      */
/*                          http://www.beyondlogic.org                        */
/*                                                                            */
/* Copyright ?2002 Craig Peacock. Craig.Peacock@beyondlogic.org              */
/* Any publication or distribution of this code in source form is prohibited  */
/* without prior written permission of the copyright holder. This source code */
/* is provided "as is", without any guarantee made as to its suitability or   */
/* fitness for any particular use. Permission is herby granted to modify or   */
/* enhance this sample code to produce a derivative program which may only be */
/* distributed in compiled object form only.                                  */
/******************************************************************************/

#include <ntddk.h>
#include <porttalk_IOCTL.h>

#define    IOPM_SIZE    0x2000

typedef UCHAR IOPM[IOPM_SIZE];

IOPM *IOPM_local = 0;

void Ke386SetIoAccessMap(int, IOPM *);
void Ke386QueryIoAccessMap(int, IOPM *);
void Ke386IoSetAccessProcess(PEPROCESS, int);

NTSTATUS PortTalkDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS PsLookupProcessByProcessId(IN ULONG ulProcId,OUT struct _EPROCESS ** pEProcess);
VOID PortTalkUnload(IN PDRIVER_OBJECT DriverObject);

NTSTATUS PortTalkCreateDispatch(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    Irp->IoStatus.Information = 0;
    Irp->IoStatus.Status = STATUS_SUCCESS;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}

NTSTATUS DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    )
{
    PDEVICE_OBJECT deviceObject;
    NTSTATUS status;
    WCHAR NameBuffer[] = L"\\Device\\PortTalk";
    WCHAR DOSNameBuffer[] = L"\\DosDevices\\PortTalk";
    UNICODE_STRING uniNameString, uniDOSString;

    KdPrint( ("PORTTALK: Porttalk V2.0 12/01/2002 has Loaded") );

    IOPM_local = MmAllocateNonCachedMemory(sizeof(IOPM));
    if(IOPM_local == 0) return STATUS_INSUFFICIENT_RESOURCES;

    RtlFillMemory(IOPM_local, sizeof(IOPM), 0xFF);

    KdPrint( ("PORTTALK: Memory Allocated at %X\n",IOPM_local) );

    RtlInitUnicodeString(&uniNameString, NameBuffer);
    RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);

    status = IoCreateDevice(DriverObject,
                            0,
                            &uniNameString,
                            FILE_DEVICE_UNKNOWN,
                            0,
                            FALSE,
                            &deviceObject);

    if(!NT_SUCCESS(status))
        return status;

    status = IoCreateSymbolicLink (&uniDOSString, &uniNameString);

    if (!NT_SUCCESS(status))
        return status;

    DriverObject->MajorFunction[IRP_MJ_CREATE] = PortTalkCreateDispatch;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = PortTalkDeviceControl;
    DriverObject->DriverUnload = PortTalkUnload;

    return STATUS_SUCCESS;
}

NTSTATUS
PortTalkDeviceControl(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP pIrp
    )

{
    PIO_STACK_LOCATION  irpSp;
    NTSTATUS            ntStatus = STATUS_SUCCESS;  

    ULONG               inBufLength;   /* Input buffer length */
    ULONG               outBufLength;  /* Output buffer length */
    ULONG               inBuf;         /* Pointer to Input and output buffer */

    PUCHAR              CharBuffer;
    PUSHORT             ShortBuffer;
    PULONG              LongBuffer;
    PVOID               ioBuffer;

    USHORT Offset;
    UCHAR Value;
 
    ULONG ProcessID;    
    struct _EPROCESS *Process;

    irpSp = IoGetCurrentIrpStackLocation( pIrp );
    inBufLength = irpSp->Parameters.DeviceIoControl.InputBufferLength;
    outBufLength = irpSp->Parameters.DeviceIoControl.OutputBufferLength;

    ioBuffer    = pIrp->AssociatedIrp.SystemBuffer;

    CharBuffer  = (PUCHAR) ioBuffer;
    ShortBuffer = (PUSHORT) ioBuffer;
    LongBuffer  = (PULONG) ioBuffer;

    switch ( irpSp->Parameters.DeviceIoControl.IoControlCode )
     {
      case IOCTL_IOPM_RESTRICT_ALL_ACCESS:

            KdPrint( ("PORTTALK: IOCTL_IOPM_RESTRICT_ALL_ACCESS - RTLFillMemory") );
            RtlFillMemory(IOPM_local, sizeof(IOPM), 0xFF);
            pIrp->IoStatus.Information = 0; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;

      case IOCTL_IOPM_ALLOW_EXCUSIVE_ACCESS:

            KdPrint( ("PORTTALK: IOCTL_IOPM_ALLOW_EXCUSIVE_ACCESS - RTLZeroMemory") );
            RtlZeroMemory(IOPM_local, sizeof(IOPM));
            pIrp->IoStatus.Information = 0; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;

      case IOCTL_SET_IOPM:

            KdPrint( ("PORTTALK: IOCTL_SET_IOPM - Set IO Permission Bitmap") );
            if (inBufLength >= 3) {
                Offset = ShortBuffer[0];
                if (Offset >= 0x2000) {
                      ntStatus = STATUS_ARRAY_BOUNDS_EXCEEDED;
                      break;
                     }
                Value  = CharBuffer[2];
                KdPrint( ("PORTTALK: Offset = %X, Value = %X\n",Offset,Value) );
                *(*IOPM_local + Offset) = Value;
                ntStatus = STATUS_SUCCESS;
            } else ntStatus = STATUS_BUFFER_TOO_SMALL;
            pIrp->IoStatus.Information = 0; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;

      case IOCTL_ENABLE_IOPM_ON_PROCESSID:

            KdPrint( ("PORTTALK: IOCTL_ENABLE_IOPM_ON_PROCESSID") );
            if (inBufLength >= 4) {
                ProcessID = LongBuffer[0];
                KdPrint( ("PORTTALK: ProcessID Received is %d\n",ProcessID) );
                PsLookupProcessByProcessId(ProcessID, &Process);
                KdPrint( ("PORTTALK: Pointer to Process is %X\n",Process) );
                KdPrint( ("PORTTALK: Address = %X\n",*(*IOPM_local + 0x6F) ) );
                Ke386SetIoAccessMap(1, IOPM_local);
                Ke386IoSetAccessProcess(Process, 1);
                ntStatus = STATUS_SUCCESS;
            } else ntStatus = STATUS_BUFFER_TOO_SMALL;
            pIrp->IoStatus.Information = 0; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;
        
      case IOCTL_READ_PORT_UCHAR:
 
            if ((inBufLength >= 2) && (outBufLength >= 1)) {
                KdPrint( ("PORTTALK: IOCTL_READ_PORT_UCHAR 0x%X",ShortBuffer[0]) );
                (UCHAR)Value = READ_PORT_UCHAR((PUCHAR)ShortBuffer[0]);
                KdPrint( ("PORTTALK: Value Read %X",Value) );
                CharBuffer[0] = Value;
            } else ntStatus = STATUS_BUFFER_TOO_SMALL;
            pIrp->IoStatus.Information = 1; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;

      case IOCTL_WRITE_PORT_UCHAR:
            if (inBufLength >= 3) {
                KdPrint( ("PORTTALK: IOCTL_WRITE_PORT_UCHAR(0x%X,0x%X)",ShortBuffer[0], CharBuffer[2]) );
                WRITE_PORT_UCHAR((PUCHAR)ShortBuffer[0], CharBuffer[2]);
            } else ntStatus = STATUS_BUFFER_TOO_SMALL;
            pIrp->IoStatus.Information = 0; /* Output Buffer Size */
            ntStatus = STATUS_SUCCESS;
            break;

      default:
            KdPrint( ("PORTTALK: Unsupported IOCTL Call\n") );
            ntStatus = STATUS_UNSUCCESSFUL;
            pIrp->IoStatus.Information = 0;
            break;

    }
    pIrp->IoStatus.Status = ntStatus;
    IoCompleteRequest( pIrp, IO_NO_INCREMENT );
    return ntStatus;
}

VOID PortTalkUnload(IN PDRIVER_OBJECT DriverObject)
{
    WCHAR DOSNameBuffer[] = L"\\DosDevices\\PortTalk";
    UNICODE_STRING uniDOSString;

    KdPrint( ("PORTTALK: PortTalk is Unloading . .\n") );

    if(IOPM_local) MmFreeNonCachedMemory(IOPM_local, sizeof(IOPM));
    RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);
    IoDeleteSymbolicLink (&uniDOSString);
    IoDeleteDevice(DriverObject->DeviceObject);
}

/**********************************************************/
PortTalk.rc:

#include <windows.h>

#include <ntverp.h>

#define VER_FILETYPE VFT_DRV
#define VER_FILESUBTYPE VFT2_DRV_SYSTEM
#define VER_FILEDESCRIPTION_STR     "PortTalk - Beyond Logic I/O Port Driver"
#define VER_INTERNALNAME_STR         "PortTalk.sys"
#define VER_ORIGINALFILENAME_STR     "PortTalk.sys"
#define VER_LEGALCOPYRIGHT_STR       "Craig.Peacock@beyondlogic.org"
#define VER_COMPANYNAME_STR         "Beyond Logic http://www.beyondlogic.org"
#define VER_PRODUCTNAME_STR         "PortTalk Driver V2.0"

#include "common.ver"

fire2fire
驱动牛犊
驱动牛犊
  • 注册日期2004-03-04
  • 最后登录2013-04-07
  • 粉丝0
  • 关注0
  • 积分30分
  • 威望3点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-03-05 12:28
还有PortTalk_IOCTL.h:

/******************************************************************************/
/*                                                                            */
/*                    PortTalk Driver for Windows NT/2000/XP                  */
/*                        Version 2.0, 12th January 2002                      */
/*                          http://www.beyondlogic.org                        */
/*                                                                            */
/* Copyright ?2002 Craig Peacock. Craig.Peacock@beyondlogic.org              */
/* Any publication or distribution of this code in source form is prohibited  */
/* without prior written permission of the copyright holder. This source code */
/* is provided "as is", without any guarantee made as to its suitability or   */
/* fitness for any particular use. Permission is herby granted to modify or   */
/* enhance this sample code to produce a derivative program which may only be */
/* distributed in compiled object form only.                                  */
/******************************************************************************/

#define PORTTALK_TYPE 40000 /* 32768-65535 are reserved for customers */

// The IOCTL function codes from 0x800 to 0xFFF are for customer use.

#define IOCTL_IOPM_RESTRICT_ALL_ACCESS \
    CTL_CODE(PORTTALK_TYPE, 0x900, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_IOPM_ALLOW_EXCUSIVE_ACCESS \
    CTL_CODE(PORTTALK_TYPE, 0x901, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_SET_IOPM \
    CTL_CODE(PORTTALK_TYPE, 0x902, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_ENABLE_IOPM_ON_PROCESSID \
    CTL_CODE(PORTTALK_TYPE, 0x903, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_READ_PORT_UCHAR \
    CTL_CODE(PORTTALK_TYPE, 0x904, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_WRITE_PORT_UCHAR \
    CTL_CODE(PORTTALK_TYPE, 0x905, METHOD_BUFFERED, FILE_ANY_ACCESS)

游客

返回顶部