News:

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

name of USB

Started by 0x401000, September 29, 2009, 08:30:16 AM

Previous topic - Next topic

0x401000

How to get the name of USB, namely, C, G, F, D, after the device has been attached?
There is a link obtained from spdidd.DevicePath ....
\\?\usbstor#disk&ven_kingston & prod_datatraveler_2.0&rev_pmap # 5b6b06809113&0 # { 53f56307-b6bf-11d0-94f2-00a0c91efb8b }


0x401000

Quote from: 0x401000 on September 29, 2009, 08:30:16 AM
How to get the name of USB, namely, C, G, F, D, after the device has been attached?
There is a link obtained from spdidd.DevicePath ....
\\?\usbstor#disk&ven_kingston & prod_datatraveler_2.0&rev_pmap # 5b6b06809113&0 # { 53f56307-b6bf-11d0-94f2-00a0c91efb8b }

Question asked differently. Connection and disconnection of the disk is not happening already from the above described. Thank you! =)

fearless

You can open the drive letter first using something like:

LOCAL DevDesc[512]:byte ; DeviceIO DevDesc information. Note: place this at start of a routine and not in middle of code, just here to show in our example

invoke CreateFile, Addr szDriveName, NULL, NULL, NULL, OPEN_EXISTING, NULL, NULL
.IF eax != INVALID_HANDLE_VALUE
mov hDrive, eax
Invoke GetDriveInformation, hDrive, Addr DevDesc
.
.

Here is the routine that is called in the above code
GetDriveInformation PROC hDrive:DWORD, lpdwDevDesc:DWORD
   
LOCAL notuse:dword
LOCAL Query:STORAGE_PROPERTY_QUERY

mov Query.PropertyId,StorageDeviceProperty 
mov Query.QueryType,PropertyStandardQuery
invoke   DeviceIoControl,hDrive,IOCTL_STORAGE_QUERY_PROPERTY,addr Query, sizeof Query, lpdwDevDesc,512,addr notuse, NULL
ret
GetDriveInformation ENDP

then to use this code somewhere to fetch the details - their is some pci/ide bug that doesnt split the vendor and product string properly, but this code should handle that for any drive as well:

; Get vendor and product id offsets
; if product id > 0 and vendor id = 0 then IDE type and need to parse vendor from product string bug
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.VendorIdOffset]
mov ebx,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.ProductIdOffset]

.if eax == 0 && ebx > 0 ; bug, so parse out

mov nProductVendorSplit, 0
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.ProductIdOffset]
xor ecx, ecx
lea edx, DevDesc
add edx, eax

VendorProductSplit:

xor eax, eax
mov al, byte ptr [edx]

.if eax == 20h || eax == 0 ; stop here
inc ecx
mov nProductVendorSplit, ecx
.else
inc ecx
inc edx
jmp VendorProductSplit
.endif 

; Get Vender Name
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.ProductIdOffset]
lea edx, DevDesc
add edx, eax
;Invoke lstrcpyn, ebx, edx, nProductVendorSplit ; you could copy out using lstrcypn to some variable pointed to at ebx or addr myvar instead or whatever

; Get Device Name
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.ProductIdOffset]
lea edx, DevDesc
add edx, eax
add edx, nProductVendorSplit ; eax now points to device name

.else ; not product/vendor bug, so just get details

; Get Vender Name
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.VendorIdOffset]
lea edx, DevDesc
add edx, eax ; eax now points to vendor name, copy this to an array or store it in some variable maybe

; Get Device Name
mov eax,[DevDesc.STORAGE_DEVICE_DESCRIPTOR.ProductIdOffset]
lea edx, DevDesc
add edx, eax ; eax now points to device name, copy this to an array or store it in some variable maybe
.endif 
.
.


I usually save the information to a big array of drive information, so you would just need to loop for all available drive letters and open each one in turn, you can use GetLogicalDriveStrings to get all drive letters available. Hope that helps.
ƒearless

0x401000

 ::) Thank you! Such a name I have already received, but a little differently. It turns out that's a name

0012FAB4                                      57 44 43 20              WDC
0012FAC4  57 44 32 35 30 30 42 45 56 53 2D 32 32 55 53 54  WD2500BEVS-22UST
0012FAD4  30 20 20 20 20 20 20 20 57 44 43 20 57 44 32 35  0       WDC WD25
0012FAE4  00 30 30 42 45 56 53 2D 32 32 55 53 54 30 20 20  .00BEVS-22UST0



But I need a letter of the hard disk  :'(

ragdog

Thanks Fearless  :U

why need  this function 2 x Get Device Name and  Get Device Name?

; Get Vender Name
....

; Get Device Name
.....
.else ; not product/vendor bug, so just get details

; Get Vender Name
.....

; Get Device Name
.....
lea edx, DevDesc
add edx, eax ; eax now points to vendor name, copy this to an arry

And how can fill this structur dwDriveDeviceType ?
Is this for cd_rom and dvdrom ?

jj2007

Use GetLogicalDriveStrings before and after attachment - the one that has been added is the USB.

Edit: It seems a "valid drive" does not have to be present... so that method won't work, sorry.

include \masm32\include\masm32rt.inc

.code
start: sub esp, 120
mov esi, esp
invoke GetLogicalDriveStrings, 120, esi
.Repeat
mov edi, esi
.Repeat
lodsb
.Until !al
print edi, 13, 10
.Until !byte ptr [esi]
add esp, 120
invoke ExitProcess, 0
end start

ragdog

Hi

My problem have i solved with
invoke  wsprintf,addr szDevice, CTEXT ("\\.\%s"),lpszDevice

this woks fine now have i only this problem
with check if cdrom or dvd

mov ebx,offset DevDesc.STORAGE_DEVICE_DESCRIPTOR.DeviceType
        .if ebx == FILE_DEVICE_CD_ROM
         ;;  msg cdrom true
        .endif



fearless

Quotewhy need  this function 2 x Get Device Name and  Get Device Name?
From what i remember there was a bug in the way vendor id and product id where represented, i cant tell you any more because i dont know myself, but possibly in early versions or early products the two weren't split properly into their proper fields, so its to separate this if it detects that, otherwise continue on and grab the stuff as normal.

For the cd/dvd type detection thats a little bit tougher as GetDriveType will return the same drive type for a cd-rom or dvd-rom drive. And using the Storage Device Descriptor structure for devicetype i could only get it to return FILE_DEVICE_CD_ROM for cd or dvd, there is a FILE_DEVICE_DVD defined but im unsure on how it is used in this context, perhaps you will have more success.

I feel a project for dedndave coming up where he compiles a library that detects all drive types, vendors, product ids, serial numbers, file systems and sizes ;-)

I have managed to create a few procs that detects (in additional to HDD drives and Removable (CD/DVD) drives) SUBST drives, RAM drives and i think USB drives as well - been a while since i looked at it, but im more than happy to share any code if someone wants to put something together for such a library.
ƒearless

ragdog

HI thanks for you help  :U

This is very nice it gives little asm examples

Quote
For the cd/dvd type detection thats a little bit tougher as GetDriveType will return the same drive type for a cd-rom or dvd-rom drive. And using the Storage Device Descriptor structure for devicetype i could only get it to return FILE_DEVICE_CD_ROM for cd or dvd, there is a FILE_DEVICE_DVD defined but im unsure on how it is used in this context, perhaps you will have more success.

Yes i have solved it with IOCTL_STORAGE_GET_MEDIA_TYPES_EX
and fill this struct for if cd or dvd

Now rip i part for part from the Ioctl includes what important is for my project :'(
I hope it gives good guys for translate the rest of my beginning ioctl includes

@jj2007

I use GetDriveType and scan from c-z and this works fine

Thanks guys :U