News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

DeviceIoControl

Started by bomz, April 05, 2012, 05:39:39 AM

Previous topic - Next topic

bomz

Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

_DISK_EXTENT struct
  DiskNumber      DWORD ?
  StartingOffset   INT64 ?
  ExtentLength      INT64 ?
_DISK_EXTENT ends

_VOLUME_DISK_EXTENTS struct
  NumberOfDiskExtents   DWORD ?
  Extents      _DISK_EXTENT <>
_VOLUME_DISK_EXTENTS ends

.data
PhysicalDrive0   db '\\.\\PhysicalDrive0',0
NumberD      db 'partition %u',0
form      db "error: %u", 0
perenos      db 13,10,0

.data?
buffer      db 512 dup(?)
string      db 1536 dup(?)
cw      dd ?
data1      _VOLUME_DISK_EXTENTS <>
      db 1024 dup (?)

.code
start:

;invoke GetProcessHeap
;mov edi, eax
;invoke HeapAlloc, eax, HEAP_ZERO_MEMORY, 1024
;mov esi, eax

invoke CreateFile,addr PhysicalDrive0,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ OR FILE_SHARE_WRITE,0, OPEN_EXISTING,0,0
mov ebx, eax

;invoke DeviceIoControl, ebx, 560000,0,0,esi,1024, addr cw, 0      ;IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,\
invoke DeviceIoControl, ebx, 560000,0,0,addr data1,sizeof data1, addr cw, 0
.if eax!=0
invoke wsprintf,ADDR buffer,addr NumberD, data1.NumberOfDiskExtents
.else
invoke GetLastError
invoke wsprintf,ADDR buffer,addr form, eax
.endif
invoke MessageBox,0,ADDR buffer,0,MB_ICONASTERISK
invoke CloseHandle, ebx

;invoke HeapFree, edi,1,esi

invoke ExitProcess,0
end start
ERROR_INVALID_FUNCTION  1 (0x1)
http://reboot.pro/10854/   

How declare array? ~  Extents      _DISK_EXTENT 26 dup <>

dedndave

the correct string for a opening the volume is
'\\.\PhysicalDrive0',0
(one less backslash) - although, that is probably ok

the value i get for IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS is
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS EQU 5636096

if you download Japheth's wininc package, he has a macro that calculates these equates from the control code sequences

the disk extents array has a header, and any number of disk extent structures
http://msdn.microsoft.com/en-us/library/aa365727%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa363968%28v=vs.85%29.aspx

i think, if your buffer is too small, you can make successive calls to get it in pieces   :P

bomz

error1   

Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

_DISK_EXTENT struct
  DiskNumber      DWORD ?
  StartingOffset   INT64 ?
  ExtentLength      INT64 ?
_DISK_EXTENT ends

_VOLUME_DISK_EXTENTS struct
  NumberOfDiskExtents   DWORD ?
  Extents      _DISK_EXTENT <>
_VOLUME_DISK_EXTENTS ends

.data
PhysicalDrive0   db '\\.\PhysicalDrive0',0
NumberD      db 'partition %u',0
form      db "error: %u", 0
perenos      db 13,10,0

.data?
buffer      db 512 dup(?)
string      db 1536 dup(?)
cw      dd ?
data1      _VOLUME_DISK_EXTENTS <>
      db 1024 dup (?)

.code
start:

invoke GetProcessHeap
mov edi, eax
invoke HeapAlloc, eax, HEAP_ZERO_MEMORY, 1024
mov esi, eax

invoke CreateFile,addr PhysicalDrive0,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ OR FILE_SHARE_WRITE,0, OPEN_EXISTING,0,0
mov ebx, eax

invoke DeviceIoControl, ebx, 5636096,0,0,esi,1024, addr cw, 0      ;IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,\
;invoke DeviceIoControl, ebx, 5636096,0,0,addr data1,sizeof data1, addr cw, 0
.if eax!=0
invoke wsprintf,ADDR buffer,addr NumberD, data1.NumberOfDiskExtents
.else
invoke GetLastError
invoke wsprintf,ADDR buffer,addr form, eax
.endif
invoke MessageBox,0,ADDR buffer,0,MB_ICONASTERISK
invoke CloseHandle, ebx

invoke HeapFree, edi,1,esi

invoke ExitProcess,0
end start

dedndave

ok
i changed the string to
PhysicalDrive0   db '\\.\C:',0

it may be that opening it as a device is not the same as opening it as a volume   :P
we are using one of the "volume" functions

also
invoke DeviceIoControl, ebx, 5636096,0,0,addr data1,sizeof data1+1024, addr cw, 0
notice that i added the size of the 1 kb buffer to the size of the structure   :U

bomz


I am trying to set Letter to USB disk under Win PE (Windows XP SP2) . How it (better) do?

bomz

Quote from: dedndave on April 05, 2012, 07:13:40 AM
ok
i changed the string to
PhysicalDrive0   db '\\.\C:',0

it may be that opening it as a device is not the same as opening it as a volume   :P
we are using one of the "volume" functions

also
invoke DeviceIoControl, ebx, 5636096,0,0,addr data1,sizeof data1+1024, addr cw, 0
notice that i added the size of the 1 kb buffer to the size of the structure   :U



but I trying get ALL partitions on the HARDDISK

dedndave

i use the disk management snap-in under admin tools, computer management   :bg

dedndave

well - step through the drive letters
PhysicalDrive0   db '\\.\C:',0
change the letter and call the function again

bomz

It's OK for the case when partitons on HD HAVE letters. But if not?

I makes a very little WinPE for USB1.1 flash, and need very small tools

dedndave

if it doesn't have a letter, then it isn't a mounted volume
the IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS function only works for mounted volumes

bomz

It was wrong path for usb

dedndave

you might try IOCTL_STORAGE_GET_MEDIA_TYPES or IOCTL_STORAGE_GET_MEDIA_TYPES_EX
i remember playing with this about a year ago   :P

bomz


dedndave

here is some code we were playing with
you will want to add USB as a device type and step through the drive letters...