DriverEntry(
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING puszRegistryPath
)
{
...
return (STATUS_SUCCESS)
}
위 구조는 가장 기본적인 DriverEntry()라는 EntryPoint, 즉 시작점입니다.
DriverEntry()함수 내에 2개의 인자가 있습니다.
그 중에 PDRIVER_OBJECT pDriverObject라는 인자 값이 있습니다.
DriverObject의 포인트이고, DriverObject의 구조체를 보겠습니다.
CSHORT Type;
CSHORT Size;
//
// The following links all of the devices created by a single driver
// together on a list, and the Flags word provides an extensible flag
// location for driver objects.
//
PDEVICE_OBJECT DeviceObject;
ULONG Flags;
//
// The following section describes where the driver is loaded. The count
// field is used to count the number of times the driver has had its
// registered reinitialization routine invoked.
//
PVOID DriverStart;
ULONG DriverSize;
PVOID DriverSection;
PDRIVER_EXTENSION DriverExtension;
//
// The driver name field is used by the error log thread
// determine the name of the driver that an I/O request is/was bound.
//
UNICODE_STRING DriverName;
//
// The following section is for registry support. Thise is a pointer
// to the path to the hardware information in the registry
//
PUNICODE_STRING HardwareDatabase;
//
// The following section contains the optional pointer to an array of
// alternate entry points to a driver for "fast I/O" support. Fast I/O
// is performed by invoking the driver routine directly with separate
// parameters, rather than using the standard IRP call mechanism. Note
// that these functions may only be used for synchronous I/O, and when
// the file is cached.
//
PFAST_IO_DISPATCH FastIoDispatch;
//
// The following section describes the entry points to this particular
// driver. Note that the major function dispatch table must be the last
// field in the object so that it remains extensible.
//
PDRIVER_INITIALIZE DriverInit;
PDRIVER_STARTIO DriverStartIo;
PDRIVER_UNLOAD DriverUnload;
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];
} DRIVER_OBJECT;
typedef struct _DRIVER_OBJECT *PDRIVER_OBJECT;
15개의 필드 중에 대략 3개정도가 가장 중요합니다.
- DriverExtension
- DriverUnload
- MajorFunction
DriverExtension
- PDRIVER_EXTENSION 구조체인데 이 구조체 중에서 AddDevice란 필드에 대해서 꼭 알아야 합니다.
DriverUnload
- 드라이버를 Unload할 때 실행시킬 함수를 저장하기 위한 필드입니다.
MajorFunction
- 커널의 I/O System 중 IO Manager라는 것이 있는데, IO Manager가 발생한 IRP패킷에 대해서 처리하는 함수입니다.
- IRP(Input Output Request Packet)란 입출력을 요구할 때 발생시키는 데이터 형식이며, 총 28가지가 있습니다.
1. OS의 드라이버 로더가 .sys파일을 로드
2. DriverObject를 생성(IO Manager가 처리)
3. DriverObject가 할 일들을(IRP 핸들러 등록)지정
4. Unload 및 AddDevice의 할 일들을 지정
IO Manager는 DriverObject에게 요청한 패킷을 전달하는 것이 아니고, DriverObject의 DeviceObject를 생선된 것에 IO 패킷을 요청합니다.
그리고 DeviceObject를 연결하여 DeviceStack을 만드는데 이 DeviceStack을 형성하여 장치를 컨트롤 하게됩니다.
출처 : 마이크로소프트웨어 2006년4월 기사