简单的虚拟设备驱动 ramdisk masm32源码
其余部分看附件 有bug.code
DispatchAddDevice proc uses esi edi ebx pDriverObject:PDRIVER_OBJECT,TargetDevice:PDEVICE_OBJECT
LOCAL DestinationString:UNICODE_STRING
LOCAL SourceString:UNICODE_STRING
LOCAL pDrvExt:DWORD
LOCAL pDevObj:DWORD
LOCAL DevExt:_DEVICE_EXTENSION_u
$DbgPrint $CTA0("DispatchAddDevice\n")
invoke IoGetDriverObjectExtension,pDriverObject,offset DriverEntry
mov pDrvExt,eax
.if DWORD ptr (_DRIVER_EXTENSION_u ptr ).DeviceInitialized == TRUE
mov edi,STATUS_DEVICE_ALREADY_ATTACHED ;0C0000038h
jmp done0
.endif
lea esi,DevExt
assume esi:ptr _DEVICE_EXTENSION_u
invoke RtlZeroMemory,esi,SizeOf _DEVICE_EXTENSION_u
invoke ExAllocatePoolWithTag,PagedPool,20,"2maR"
mov .DiskRegInfo.DriveLetter.Buffer,eax
.if !eax
mov edi,STATUS_INSUFFICIENT_RESOURCES ;0C000009Ah
jmp done1
.endif
mov .DiskRegInfo.DriveLetter.MaximumLength,20
invoke _QueryDiskRegParameters,pDrvExt,addr .DiskRegInfo
invoke RtlInitUnicodeString,addr DestinationString,$CTW0("\\Device\\HarddiskVolumeRD")
mov eax,.DiskRegInfo.MediaType ;1~4
.if eax==2
mov eax,0
mov ecx,FILE_DEVICE_DISK ;7
; .elseif eax==1
; mov eax,0
; mov ecx,FILE_DEVICE_VIRTUAL_DISK ;24h
.elseif eax==3
mov eax,1 ;FILE_REMOVABLE_MEDIA
mov ecx,FILE_DEVICE_DISK ;7
.elseif eax==4
mov eax,5 ;FILE_FLOPPY_DISKETTE or FILE_REMOVABLE_MEDIA
mov ecx,FILE_DEVICE_DISK ;7
.else
mov edi,STATUS_INSUFFICIENT_RESOURCES ;0C000009Ah
jmp done0
.endif
or eax,FILE_DEVICE_SECURE_OPEN ;100h
lea edx,pDevObj
invoke IoCreateDevice,pDriverObject,\
SizeOf _DEVICE_EXTENSION_u,\ ;DevExt size
addr DestinationString,\
ecx,\
eax,\
0,\
edx
.if eax < SDWORD ptr 0
mov edi,eax
invoke ExFreePoolWithTag,.DiskRegInfo.DriveLetter.Buffer,0
jmp done0
.endif
mov ebx,pDevObj
assume ebx:ptr DEVICE_OBJECT
mov esi,.DeviceExtension
invoke RtlMoveMemory,esi,addr DevExt,sizeof _DEVICE_EXTENSION_u
mov eax,TargetDevice
mov .TagDev,eax
mov .DeviceObject,ebx
and dword ptr .DevState,0
invoke IoInitializeRemoveLockEx,addr .RemoveLock,\
"lmaR",\
1,\
0Ah,\
SizeOf IO_REMOVE_LOCK
or .Flags,DO_POWER_PAGABLE OR DO_DIRECT_IO; 2010h
invoke KeInitializeSpinLock,addr .Spinlock
mov eax,.DiskRegInfo.NumDevicePage
.if !eax
mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
jmp done1
.endif
mov .MaxNumMDL,eax
mov ecx,eax
shl ecx,6
mov .Cylinders,ecx
mov ecx,.DiskRegInfo.DisksizeM
shl ecx,6 ;1024*1024/512/32
mov .Tracks,ecx
shl eax,2 ;eax*4
$DbgPrint $CTA0("Initialize Disk Space %d\n")
invoke ExAllocatePoolWithTag,NonPagedPool,eax,"3maR"
mov .MDL,eax
.if !eax
invoke DeviceContextCleanup,ebx
mov edi,STATUS_INSUFFICIENT_RESOURCES ;0C000009Ah
jmp done1
.endif
mov ecx,.MaxNumMDL
shl ecx,2
invoke RtlZeroMemory,eax,ecx
invoke DeviceAllocMdl,esi
.if (eax < .MaxNumMDL)
mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
jmp done1
.endif
invoke DeviceLockPresentPage,esi,0
.if !eax
mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
jmp done1
.endif
invoke FormatDisk,ebx
invoke ExAllocatePoolWithTag,PagedPool,2Eh,"4maR"
mov .SymbolicLinkName.Buffer,eax
.if !eax
mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
jmp done1
.endif
.if .DiskRegInfo.DriveLetter._Length
invoke RtlInitUnicodeString,addr SourceString,$CTW0("\\DosDevices\\")
lea edi,.SymbolicLinkName
assume edi:PTR UNICODE_STRING
mov ax,SourceString._Length
mov ._Length,ax
mov .MaximumLength,2Eh
assume edi:nothing
invoke RtlCopyUnicodeString,edi,addr SourceString
invoke RtlAppendUnicodeStringToString,edi,addr .DiskRegInfo.DriveLetter
invoke IoCreateSymbolicLink,edi,addr DestinationString
mov edi,eax
.if eax < SDWORD ptr 0
jmp done1
.endif
or dword ptr .flag,1
.endif
invoke IoAttachDeviceToDeviceStack,pDevObj,TargetDevice
mov .LoDeviceObject,eax
.if !eax
mov edi,STATUS_NO_SUCH_DEVICE ;0C000000Eh
jmp done1
.endif
mov eax,pDrvExt
mov (_DRIVER_EXTENSION_u ptr ).DeviceInitialized,TRUE
and .Flags,7Fh
mov edi,STATUS_SUCCESS
jmp done0
assume esi:nothing
done1:
invoke DeviceContextCleanup,pDevObj
done0:
$DbgPrint $CTA0("DeviceInitialize %08X\n"),edi
mov eax,edi
;done:
ret
DispatchAddDevice endp
本帖最后由 Ayala 于 2018-7-31 20:20 编辑
原来附件没上传成功!
页:
[1]