Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 6. Initialization and Cleanup Routines

Similar presentations


Presentation on theme: "Ch 6. Initialization and Cleanup Routines"— Presentation transcript:

1 Ch 6. Initialization and Cleanup Routines

2 Contents 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 Ch 6. 초기화 및 정리 루틴

3 1. Writing a DriverEntry Routine

4 DriverEntry Routine 모든 Windows 2000의 커널 모드 드라이버나 WDM드라이버는 그 목적에 관계없이 DriverEntry Routine을 가짐. 다양한 데이터 구조체를 초기화 다른 모든 드라이버의 구성 요소들을 위한 환경을 준비 Ch 6. 초기화 및 정리 루틴

5 DriverEntry 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 driver’s key Return value STATUS_SUCCESS STATUS_XXX – some error code Ch 6. 초기화 및 정리 루틴

6 DriverEntry Routine(2)
드라이버에서 제어할 하드웨어를 획득 Driver Object의 다른 Routine들의 시작점 지정 Controller 생성 , Controller Extension 초기화 (IoCreateController) Device Object 생성, Device Extension 초기화 (IoCreateDevice) Device Object를 Win32시스템에서 보이도록 함 (IoCreateSymbolicLink) Interrupt Object와 연결(DPC Object생성) 4~6의 과정을 다른 device에 대해 반복 수행 Return STATUS_SUCCESS to I/O manager Ch 6. 초기화 및 정리 루틴

7 Announcing DriverEntry Point
Function pointer in Driver Object 명시적인 slot & name을 가지는 Function MajorFunction배열 상에 나열된 IRP Dispatch function Ex) Function pointer initialize pDO -> DriverStartIO = StartIO; pDO -> DrtverUnload = Unload; // // 다른 주요 처리함수 테이블에 대한 초기화 pDO -> MajorFunction[ IRP_MJ_CREATE ] = DispatchCreate; pDO -> MajorFunction[ IRP_MJ_CLOSE ] = DispatchClose; Ch 6. 초기화 및 정리 루틴

8 Creating Device Object
IoCreateDevice Driver Object에 의해 관리되는 Device List에 새로운 Device Object를 연결 AddDevice Routine에서 호출하는 경우 WDM 을 지원하는 Device DriverEntry Routine의 1,3~6과정을 제외함 (하드웨어 획득 및 컨트롤러 디바이스 객체의 생성) Dispatch Routine에서 IoCreateDevice 가 불리는 경우 Device의 Flags Field중 DO_DEVICE_INITIALIZING bit을 reset Device의 추가 or Reinitializing Ch 6. 초기화 및 정리 루틴

9 Creating 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 Ch 6. 초기화 및 정리 루틴

10 Buffering Strategy Device에서 사용할 Buffering strategy 를 I/O Manager에게 통보
Device Flags Field DO_BUFFER_I/O DO_DIRECT_I/O Neither bit - 두가지 모두를 사용 Ch 6. 초기화 및 정리 루틴

11 Device Name Name Directory Tree Object Manager Root Device ?? Xx0 XX1
Symbolic Link Ex) 내부 “\\Device\\Minimal0” FloppyDisk0 , FloppyDisk1 Ex) Symbolic Link Name “\\??\\MINIMAL1” LPT1, LPT2 , A: ~ Z: IoCreateSymbolicLink ( Device Name , UNICODE_STRING Symbolic Link Name ) Ch 6. 초기화 및 정리 루틴

12 2. Code Example : Driver Initialization

13 Minimal 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->DriverUnload = DriverUnload; … status = CreateDevice ( pDriverObject, … ); return status; } Ch 6. 초기화 및 정리 루틴

14 Minimal 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_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; CUString devName(“\\Device\\MINIMAL”); devName += CUString(ulDeviceNumber); … Ch 6. 초기화 및 정리 루틴

15 Minimal driver (2) status = IoCreateDevice ( pDriverObject, … );
pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; // Back pointer pDevExt->DeviceNumber = ulDeviceNumber; pDevExt->ustrDeviceName = devName; CUString SymLinkName(“\\??\\MINI”); SymLinkName += CUString(ulDeviceNumber+1); pDevExt->ustrSymLinkName = SymLinkName; status = IoCreateSymbolicLink( … ); … //실패할 경우 Device Object를 반드시 삭제 return STATUS_SUCCESS; } Ch 6. 초기화 및 정리 루틴

16 3. Writing Reinitialize Routines

17 Function Prototype for Reinitialize
Reinitialize 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) Ch 6. 초기화 및 정리 루틴

18 4. Writing an Unload Routine

19 Function Prototype for Unload
Unload 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) Ch 6. 초기화 및 정리 루틴

20 Unload Routine(1) Unload Routine 의 동작 WDM의 경우 RemoveDevice에서 수행
다음 load시에 상태 복구를 위해 Device의 상태정보를 registry에 저장 Interrupt disable, disconnect Interrupt object Hardware 해제 Remove Symbolic Link Name from Win32 namespace IoDeleteDevice : Device Object 삭제 다중 Controller를 사용한다면 4~5과정 반복, IoDeleteController : Controller Object 삭제 4~6의 과정을 다른 Device & Controller에 대해 반복 수행 Deallocate pool memory WDM의 경우 RemoveDevice에서 수행 Ch 6. 초기화 및 정리 루틴

21 5. Code Example : Driver Unload

22 Unload Routine(2) Driver Entry Routine의 동작을 반대로 수행
Symbolic Link Name 과 Device Object를 삭제 VOID DriverUnload ( IN PDRIVER_OBJECT pDriverObject ) { PDEVICE_OBJECT pNextObj; pNextobj = pDriverObject->DeviceObject; while ( pNextObj != NULL ) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION) pNextObj->DeviceExtension; UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName; IoDeleteSymbolicLink( &pLinkName ); pNextObj = pNextObj -> NextDevice; IoDeleteDevice ( pDevExt->pDevice ); } //IoReportResourceUsage } Ch 6. 초기화 및 정리 루틴

23 6. Writing Shutdown Routines

24 Function Prototype for Shutdown
Shutdown 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 Ch 6. 초기화 및 정리 루틴

25 Shutdown Routine(1) 별도의 I/O요구 MajorFunction 코드 배열의내부요소에 지정
IoRegisterShutdownNotification : Shutdown 통지 NTSTATUS DriverEntry ( IN PDRIVER_OBJECT pDriverObject , IN PUNICODE_STRING pRegistryPath) { pDriverObject->MajorFunction [ IRP_MJ_SHUTDOWN ] = Shutdown; IoCreateDevice( pDriverObject , … , pDeviceObject); IoRegisterShutdownNotification ( pDriverObject ); } Ch 6. 초기화 및 정리 루틴

26 7. Testing the Driver

27 Test 성공적인 Compile , Link 시스템 충돌이 없이 load & Unload 수행
Device Object 와 Win32 Symbolic Link 생성 Unload시 자원 해제 Ch 6. 초기화 및 정리 루틴

28 Tool 사용 예제 소스 코드 Minimal.dsw의 컴파일 및 링크
생성된 Minimal.sys를 \winnt\system32\drivers에 복사 Regedit로 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service에 드라이버 이름( ex – Minimal )로 키생성 드라이버내에 서브키생성 ErrorControl=1(DWORD),Start=3(DWORD),Type=1(DWORD) 또는 Minimal.reg를 실행함 DisplayName : 컴퓨터 관리 창에서 보이는 이름 Ch 6. 초기화 및 정리 루틴

29 Test Ch 6. 초기화 및 정리 루틴

30 Test Ch 6. 초기화 및 정리 루틴

31 Test Ch 6. 초기화 및 정리 루틴

32 Test Ch 6. 초기화 및 정리 루틴

33 Summary

34 Summary Windows 2000의 Device Driver 초기 버전의 구현 드라이버의 빌드 , 설치
커널 모드 드라이버의 기본 구조체 Ch 6. 초기화 및 정리 루틴


Download ppt "Ch 6. Initialization and Cleanup Routines"

Similar presentations


Ads by Google