flat assembler
Message board for the users of flat assembler.

Index > OS Construction > USB Flash Drive Info

Author
Thread Post new topic Reply to topic
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 15 Jun 2012, 21:40
Here is a simple MS cmd script to display the information of the USB flash drives plugged into the PC.
(Also shows if there is floppy drive).


Code:
@echo off
@setlocal enableextensions enabledelayedexpansion

  title USB Flash Drive Info
  dd.exe --list 2> list.txt

  set floppy_disk=FALSE
  set /a found=0
  set ufd=FALSE

  set drive1=0
  set drive2=0
  set drive3=0
  set drive4=0
  set drive5=0
  set drive6=0

  for /f "delims=" %%a in (list.txt) do (
    set AA=%%a
    if "!AA:~2,16!"=="removeable media" (
      set ufd=TRUE
    ) else if "!AA:~2,15!"=="Mounted on \\.\" (
      if "!AA:~17,1!"=="a" (
        set floppy_disk=TRUE
      ) else if !ufd!==TRUE (
        set /a found+=1
        set drive!found!=!AA:~17,1!
      )
      set ufd=FALSE
    ) else if "!AA:~0,23!"=="NT Block Device Objects" (
      set /a found=0
      set ufd=0
    ) else if "!AA:~0,19!"=="\\?\Device\Harddisk" (
      if not "!AA:~19,1!"=="0" (
        if not "!current_disk!"=="!AA:~19,1!" (
          if !ufd!==0 (
            set current_disk=!AA:~19,1!
            set ufd=1
          )
        )
      )
    ) else if "!AA:~2,15!"=="Removable media" (
      if !ufd!==1 (
        set removable=TRUE
      )
    ) else if "!AA:~2,7!"=="size is" (
      set ufd=0
      if !removable!==TRUE (
        set /a found+=1
        set size!found!=!AA:~10,32!
        set disk!found!=!current_disk!
        set removable=FALSE
      )
    )
  )

  if !floppy_disk!==TRUE (
    set /a found+=1
  )

  for /f "tokens=1 delims= " %%a in ("%size1%") do set size1=%%a
  for /f "tokens=1 delims= " %%a in ("%size2%") do set size2=%%a
  for /f "tokens=1 delims= " %%a in ("%size3%") do set size3=%%a
  for /f "tokens=1 delims= " %%a in ("%size4%") do set size4=%%a
  for /f "tokens=1 delims= " %%a in ("%size5%") do set size5=%%a
  for /f "tokens=1 delims= " %%a in ("%size6%") do set size6=%%a

  set size1=0000000000000000%size1%
  set size1=%size1:~-16%
  call :upper drive1
  call :friendly %size1%
  set size1f=%size%

  set size2=0000000000000000%size2%
  set size2=%size2:~-16%
  call :upper drive2
  call :friendly %size2%
  set size2f=%size%

  set size3=0000000000000000%size3%
  set size3=%size3:~-16%
  call :upper drive3
  call :friendly %size3%
  set size3f=%size%

  set size4=0000000000000000%size4%
  set size4=%size4:~-16%
  call :upper drive4
  call :friendly %size4%
  set size4f=%size%

  set size5=0000000000000000%size5%
  set size5=%size5:~-16%
  call :upper drive5
  call :friendly %size5%
  set size5f=%size%

  set size6=0000000000000000%size6%
  set size6=%size6:~-16%
  call :upper drive6
  call :friendly %size6%
  set size6f=%size%

  if %found%==0 (
    echo No flash drive found!
    goto :exit
  )

  echo(
  if %floppy_disk%==TRUE echo    Floppy Disk     A: [1.4MB]
  if not %drive1%==0  echo    USB Flash Drive %drive1%: [%size1f%] %size1% bytes - disk%disk1%
  if not %drive2%==0  echo    USB Flash Drive %drive2%: [%size2f%] %size2% bytes - disk%disk2%
  if not %drive3%==0  echo    USB Flash Drive %drive3%: [%size3f%] %size3% bytes - disk%disk3%
  if not %drive4%==0  echo    USB Flash Drive %drive4%: [%size4f%] %size4% bytes - disk%disk4%
  if not %drive5%==0  echo    USB Flash Drive %drive5%: [%size5f%] %size5% bytes - disk%disk5%
  if not %drive6%==0  echo    USB Flash Drive %drive6%: [%size6f%] %size6% bytes - disk%disk6%
  goto :exit

:upper
  for %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do call set "%1=%%%1:%%~i%%"
  goto :eof

:friendly
  if        %1 LSS 0000000008388608 (
    set size=  8MB
  ) else if %1 LSS 0000000016777216 (
    set size= 16MB
  ) else if %1 LSS 0000000033554432 (
    set size= 32MB
  ) else if %1 LSS 0000000067108864 (
    set size= 64MB
  ) else if %1 LSS 0000000134217728 (
    set size=128MB
  ) else if %1 LSS 0000000268435456 (
    set size=256MB
  ) else if %1 LSS 0000000536870912 (
    set size=512MB
  ) else if %1 LSS 0000001073741824 (
    set size=  1GB
  ) else if %1 LSS 0000002147483648 (
    set size=  2GB
  ) else if %1 LSS 0000004294967296 (
    set size=  4GB
  ) else if %1 LSS 0000008589934592 (
    set size=  8GB
  ) else if %1 LSS 0000017179869184 (
    set size= 16GB
  ) else if %1 LSS 0000034359738368 (
    set size= 32GB
  ) else if %1 LSS 0000068719476736 (
    set size= 64GB
  ) else if %1 LSS 0000137438953472 (
    set size=128GB
  ) else if %1 LSS 0000274877906944 (
    set size=256GB
  ) else if %1 LSS 0000549755813888 (
    set size=512GB
  )
  goto :eof

:exit
  echo(
  echo Thank you for trying USB Flash Drive Info^^!
  pause
  goto :eof    
It parses the output of the dd.exe --list option.

With the drive letter in the %drive% variable, it is easy to read and write directly to the UFD with dd.
Code:
  dd.exe --filter=removable if=file od=%drive%:    
The --filter=removable option prevents dd from writing to the hard drive (Windows also considers USB hard drives
as fixed media - and are also protected).

With the disk number in the %disk% variable, it is easy to emulate the actual UFD in qemu.
Code:
  qemu.exe -L . -localtime -hda //./PhysicalDrive%disk% -boot c    


Last edited by Mike Gonta on 05 Oct 2014, 08:19; edited 1 time in total
Post 15 Jun 2012, 21:40
View user's profile Send private message Visit poster's website Reply with quote
cwpjr



Joined: 03 Jul 2012
Posts: 44
cwpjr 03 Jul 2012, 05:28
Very nice and clean. Thank you.
Post 03 Jul 2012, 05:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.