Kernel Mode Driver Framework >> Assembly (PNP) >> File

IRP


參考資訊:
1. Source Code

基本上,驅動程式(韌體)的基本功能就是當作User Application(軟體)和硬體的溝通橋樑,軟體可以透過很多種方式和驅動程式溝通,而最常用的溝通方式就是把驅動程式當做是一個檔案在操作,軟體可以透過檔案讀寫的方式和驅動程式做資料傳遞的動作,驅動程式最後再把資料輸出到硬體裝置上,而在Windows系統上,可以使用CreateFile(檔案開啟)、ReadFile(讀取檔案)、WriteFile(寫入檔案)、CloseHandle(關閉檔案)等檔案操作的Win32 API,而驅動程式如何跟軟體串接起來呢?驅動程式必須設定這些對應的Event Callback,這樣I/O Manager才會知道如何串起這條溝通管道。

當然,使用者可能也很好奇,為何軟體不直接跟硬體溝通就好,還要透過一個這麼複雜的驅動程式當作溝通橋樑?當軟體可以直接溝通硬體時,會造成太多衝突,以及優先順序問題,而因為可以直接控制硬體,因此,寫出一個非惡意但具有破壞性的軟體時,是間接可以把一個系統搞壞掉,這也是為何要在Win98之後,架構出一個驅動程式的原因,至少有一個溝通和協調的機制,當然隨著系統的發展,規定就會變成越來嚴格,這也是Microsoft致力於保護系統的最好方式!

對應的溝通管道

Win32 API Kernel Event
CreateFile() EvtDeviceFileCreate
ReadFile() EvtIoRead
WriteFile() EvtIoWrite
CloseHandle() EvtFileClose

驅動程式對應的CreateFile()和CloseHandle() Callback必須使用WDF_FILEOBJECT_CONFIG_INIT()設定,宣告如下:

void WDF_FILEOBJECT_CONFIG_INIT(
  PWDF_FILEOBJECT_CONFIG     FileEventCallbacks,
  PFN_WDF_DEVICE_FILE_CREATE EvtDeviceFileCreate,
  PFN_WDF_FILE_CLOSE         EvtFileClose,
  PFN_WDF_FILE_CLEANUP       EvtFileCleanup
);

至於ReadFile()和WriteFile() Callback則位於WDF_IO_QUEUE_CONFIG(EvtIoRead和EvtIoWrite)

基本的步驟如下:
1. 安裝驅動程式時,驅動程式註冊自己的名字、註冊檔案的Event Callback副程式
2. 軟體開啟檔案(使用驅動程式的名字(GUID或者Symbolic)),I/O Manager呼叫驅動程式的EvtDeviceFileCreate
3. 軟體讀寫檔案,I/O Manager呼叫驅動程式的EvtIoRead、EvtIoWrite
4. 軟體關閉檔案,I/O Manager呼叫驅動程式的EvtFileClose

main.asm

.386p
.model flat, stdcall
option casemap:none

include c:\masm32\Macros\Strings.mac
include c:\masm32\include\w2k\ntdef.inc
include c:\masm32\include\w2k\ntstatus.inc
include c:\masm32\include\w2k\ntddk.inc
include c:\masm32\include\w2k\ntoskrnl.inc
include c:\masm32\include\w2k\ntddkbd.inc
include c:\masm32\include\wdf\umdf\1.9\wudfddi_types.inc
include c:\masm32\include\wdf\kmdf\1.9\wdf.inc
include c:\masm32\include\wdf\kmdf\1.9\wdftypes.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfglobals.inc
include c:\masm32\include\wdf\kmdf\1.9\wdffuncenum.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfobject.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfdevice.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfdriver.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfrequest.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfio.inc
include c:\masm32\include\wdf\kmdf\1.9\wdfmemory.inc

includelib c:\masm32\lib\wxp\i386\BufferOverflowK.lib 
includelib c:\masm32\lib\wxp\i386\ntoskrnl.lib 
includelib c:\masm32\lib\wxp\i386\hal.lib 
includelib c:\masm32\lib\wxp\i386\wmilib.lib 
includelib c:\masm32\lib\wxp\i386\sehupd.lib 
includelib c:\masm32\lib\wdf\kmdf\i386\1.9\wdfldr.lib
includelib c:\masm32\lib\wdf\kmdf\i386\1.9\wdfdriverentry.lib

public DriverEntry

.const
DEV_NAME word "\","D","e","v","i","c","e","\","M","y","D","r","i","v","e","r",0
SYM_NAME word "\","D","o","s","D","e","v","i","c","e","s","\","M","y","D","r","i","v","e","r",0

.code
IrpFileCreate proc Device:WDFDEVICE, Request:WDFREQUEST, FileObject:WDFFILEOBJECT
  invoke DbgPrint, $CTA0("IrpFieCreate")
  invoke WdfRequestComplete, Request, STATUS_SUCCESS
  ret
IrpFileCreate endp

IrpFileClose proc FileObject:WDFFILEOBJECT
  invoke DbgPrint, $CTA0("IrpFieClose")
  ret
IrpFileClose endp

IrpRead proc Queue:WDFQUEUE, Request:WDFREQUEST, _Length:DWORD
  invoke DbgPrint, $CTA0("IrpRead")
  invoke WdfRequestCompleteWithInformation, Request, STATUS_SUCCESS, _Length
  ret
IrpRead endp

IrpWrite proc Queue:WDFQUEUE, Request:WDFREQUEST, _Length:DWORD
  invoke DbgPrint, $CTA0("IrpWrite")
  invoke WdfRequestCompleteWithInformation, Request, STATUS_SUCCESS, _Length
  ret
IrpWrite endp

AddDevice proc Driver:WDFDRIVER, pDeviceInit:PWDFDEVICE_INIT
  local device:WDFDEVICE
  local suDevName:UNICODE_STRING
  local szSymName:UNICODE_STRING
  local file_cfg:WDF_FILEOBJECT_CONFIG
  local ioqueue_cfg:WDF_IO_QUEUE_CONFIG

  invoke RtlInitUnicodeString, addr suDevName, offset DEV_NAME
  invoke RtlInitUnicodeString, addr szSymName, offset SYM_NAME
  invoke WdfDeviceInitAssignName, pDeviceInit, addr suDevName
  
  invoke WdfDeviceInitSetIoType, pDeviceInit, WdfDeviceIoBuffered
  invoke WDF_FILEOBJECT_CONFIG_INIT, addr file_cfg, offset IrpFileCreate, offset IrpFileClose, NULL
  invoke WdfDeviceInitSetFileObjectConfig, pDeviceInit, addr file_cfg, WDF_NO_OBJECT_ATTRIBUTES
  invoke WdfDeviceCreate, addr pDeviceInit, WDF_NO_OBJECT_ATTRIBUTES, addr device
  invoke WdfDeviceCreateSymbolicLink, device, addr szSymName
  
  invoke WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE, addr ioqueue_cfg, WdfIoQueueDispatchSequential
  lea eax, ioqueue_cfg
  mov (WDF_IO_QUEUE_CONFIG ptr [eax]).EvtIoRead, offset IrpRead
  mov (WDF_IO_QUEUE_CONFIG ptr [eax]).EvtIoWrite, offset IrpWrite
  invoke WdfIoQueueCreate, device, addr ioqueue_cfg, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE
  ret
AddDevice endp

DriverEntry proc pOurDriver:PDRIVER_OBJECT, pOurRegistry:PUNICODE_STRING
  local config:WDF_DRIVER_CONFIG
  
  invoke WDF_DRIVER_CONFIG_INIT, addr config, AddDevice
  invoke WdfDriverCreate, pOurDriver, pOurRegistry, WDF_NO_OBJECT_ATTRIBUTES, addr config, WDF_NO_HANDLE
  ret
DriverEntry endp
end DriverEntry
.end

AddDevice()設定檔案的Event Callback以及建立Symbolic Link
IrpFileCreate()僅列印資訊,其餘不處理,值得注意的是Complete Irp呼叫的是WdfRequestComplete()
IrpFileClose()僅列印資訊,其餘不處理,WDF簡化了FileClose的處理步驟
IrpRead()僅列印資訊,其餘不處理,透過呼叫WdfRequestCompleteWithInformation()完成Irp操作並且回傳Information
IrpWrite()僅列印資訊,其餘不處理,透過呼叫WdfRequestCompleteWithInformation()完成Irp操作並且回傳Information

為了測試驅動程式的檔案讀寫IRP,我們需要一個User Application做測試(app.asm)

.386p
.model flat, stdcall
option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\masm32.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\msvcrt.inc
include c:\masm32\include\kernel32.inc

includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\masm32.lib
includelib c:\masm32\lib\msvcrt.lib
includelib c:\masm32\lib\kernel32.lib

.const
DEV_NAME  db "\\.\MyDriver",0
MSG_ERR   db "failed to open mydriver",10,13,0
MSG_SEND  db "I am error",0

.data?
hFile    dd ?
dwRet    dd ?
szBuffer db 256 dup(?)

.code
start:
  invoke CreateFile, offset DEV_NAME, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
  .if eax == INVALID_HANDLE_VALUE
    invoke crt_printf, offset MSG_ERR
    invoke ExitProcess, -1
  .endif
  
  mov hFile, eax
  invoke StrLen, offset MSG_SEND
  inc eax
  mov dwRet, eax
  invoke WriteFile, hFile, offset MSG_SEND, dwRet, offset dwRet, 0
  invoke crt_memset, offset szBuffer, 0, 255
  invoke ReadFile, hFile, offset szBuffer, 255, offset dwRet, 0
  invoke CloseHandle, hFile
  invoke ExitProcess, 0
end start

編譯App:

c:\masm32\bin\ml /c /coff /Cp /Zi /Zd "app.asm"
c:\masm32\bin\link /SUBSYSTEM:CONSOLE /DEBUG /VERSION:4.0 /OUT:"app.exe" "app.obj"

接著執行該User Application就可以看到結果

返回上一頁