home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 August - Disc 1 / PCNET_CD_2006_08_1.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / jesseprobeusb < prev    next >
Encoding:
Tcl/Tk script  |  2005-03-22  |  7.0 KB  |  248 lines

  1. #!/usr/bin/tclsh
  2. # This code is extracted from:
  3. #  MUT written by Jesse Liley for Puppy Linux 2005
  4. # ...in other words, what you see below is a hack, a simple commandline
  5. # tool to grab usb drive information. hacker: Barry Kauler 2005.
  6.  
  7. # For output of debugging information, output is to stdout
  8. set debug 1
  9.  
  10. # List of info from /proc/partitions
  11. array set partitions {}
  12. # Scsi controller information (scsi hosts providing a scsi bus)
  13. array set storages {}
  14. # Scsi device info (devices on scsi bus)
  15. array set scsi_host_chips {}
  16. # List of info from /proc/mounts
  17. array set mounteds {}
  18.  
  19. # The list of displayed devices & partitions.
  20. set infolist ""
  21.  
  22. # USB SCSI last refresh info, for hot plug scanning.
  23. set last_hotplug 0
  24.  
  25.  
  26. proc remove_empties str {
  27.     set i 0
  28.     set i [lsearch $str {}]
  29.     while {$i >= 0} {
  30.         set str [lreplace $str $i $i]
  31.         set i [lsearch $str {}]
  32.     }
  33.     return $str
  34. }
  35.  
  36. proc grab_partitions {} {
  37.     set fid [open "/proc/partitions" r]
  38.     while {![eof $fid]} {
  39.         gets $fid str
  40.         set shortlist [remove_empties [split $str]]
  41.         if {[llength $str] == 4 && [lindex $str 0] > 0} {
  42.             if { [lindex $shortlist 2] > 1 } {
  43.                 set ::partitions([lindex $shortlist 3]) \
  44.                     [format "%1.1f Mb" [expr "[lindex $shortlist 2]/1000.0"]]
  45.             }
  46.         }
  47.     }
  48.     close $fid
  49. }
  50.  
  51. proc grab_proc_scsi {} {
  52.     set chip 1
  53.     set do_host ""
  54.     set Location ""
  55.     set Model ""
  56.     set Type ""
  57.     set Device ""
  58.     set alphabet [list a b c d e f g i j k l m n o p q r s t u v w x y z]
  59.     set tape 0
  60.     set disk 0
  61.     set cdrom 0
  62.     set fid [open "/proc/scsi/scsi" r]
  63.     while {![eof $fid]} {
  64.         gets $fid str
  65.         if {[regexp -inline {\S+} $str] == "Host:"} {
  66.             set line [split $str " "]
  67.             lappend Location [lindex $line 1]
  68.             lappend Location [lindex $line 3]
  69.             lappend Location [lindex $line 5]
  70.             lappend Location [lindex $line 7]
  71.             if {[lindex $Location 0] != $do_host} {
  72.                 set chip 1
  73.                 set do_host [lindex $Location 0]
  74.             } {    incr chip }
  75.         }
  76.         if {[regexp -inline {\S+} $str] == "Vendor:"} {
  77.             set line [remove_empties [split $str " "]]
  78.             set i [lsearch $line "Vendor:"]
  79.             set Model ""
  80.             incr i
  81.             while {1} {
  82.                 if {[lindex $line $i]=="" || [lindex $line $i]=="Rev:"} {break}
  83.                 if {[lindex $line $i] == "Model:"} {
  84.                     if {$Model != ""} {set Model [string trimright $Model " "]; set Model "$Model, "}
  85.                 } else {
  86.                     set Model "$Model[lindex $line $i] "
  87.                 }
  88.                 incr i
  89.             }
  90.             set Model [string trimright $Model ", "]
  91.         }
  92.         if {[regexp -inline {\S+} $str] == "Type:"} {
  93.             set line [split $str " "] 
  94.             set i [lsearch $line "Direct-Access"];
  95.             if {$i >= 0} {set Type "disk"; set Device "sd[lindex $alphabet $disk]"; incr disk}
  96.             set i [lsearch $line "CD-ROM"];
  97.             if {$i >= 0} {set Type "cdrom"; set Device "scd$cdrom"; incr cdrom}
  98.             set i [lsearch $line "Sequential-Access"];
  99.             if {$i >= 0} {set Type "tape"; set Device "st$tape"; incr tape}
  100.         }
  101.         if {$Type != "" && $Location != "" && $Model != "" && $Device != "" && $do_host != ""} {
  102.             set ::scsi_host_chips($do_host) $chip
  103.             catch {
  104.                 foreach {h t id} $::scsi_hosts([lindex $Location 0]) {
  105.                     if {$h == [lindex $Location 0]} {
  106.                         set ::storages($Device) [list $h $Type "$Model" $id $chip]
  107.                         if {$::debug} {puts "found possible $Device {$Location} {$Type} {$Model} {$chip}"}
  108.                         set devid [open "/dev/$Device" r]
  109.                         close $devid
  110.                     }
  111.                 }
  112.             }
  113.             set Location "" 
  114.             set Type ""
  115.             set Model ""
  116.             set Device ""
  117.         }
  118.     }
  119.     close $fid
  120. }
  121.  
  122. proc load_scsi_host ctrl {
  123.     catch {
  124.         set usb false
  125.         set host ""
  126.         set product ""
  127.         set vendor ""
  128.         set serial ""
  129.         set protocol ""
  130.         set transport ""
  131.         set guid ""
  132.         set attached ""
  133.         if {$::debug} {puts "finding $ctrl"}
  134.         set hid [open $ctrl r]
  135.         while {![eof $hid]} {
  136.             gets $hid str
  137.             set shortlist [remove_empties [split $str]]
  138.             switch [lindex $shortlist 0] {
  139.                 "Host"       { set host [string trimright [lindex $shortlist 1] ":"] 
  140.                     if {[lindex $shortlist 2] == "usb-storage"} {set usb true} {set usb false}
  141.                 }
  142.                 "Vendor:"    { set vendor [join [lrange $shortlist 1 end]] }
  143.                 "Product:"   { set product [join [lrange $shortlist 1 end]] }
  144.                 "Serial"     { set serial [join [lrange $shortlist 2 end]]  }
  145.                 "Protocol:"  { set protocol [join [lrange $shortlist 1 end]] }
  146.                 "Transport:" { set transport [join [lrange $shortlist 1 end]]  }
  147.                 "GUID:"      { set guid [lindex $shortlist 1]  }
  148.                 "Attached:"  { set attached [lindex $shortlist 1]  }
  149.             }
  150.         }
  151.         close $hid
  152.         if {$attached=="Yes" && $usb==true && $transport=="Bulk" && $protocol=="Transparent SCSI"} {
  153.             if { $vendor=="" || $vendor=="Unknown"} {set name $product} {set name "$vendor, $product"}
  154.             if {$::debug} {puts "$host is $name"}
  155.             set ::scsi_hosts($host) [list $host $name "usb" "$serial.$guid"]
  156.         } { if {$::debug} {puts "ignoring $host $product"} }
  157.     }
  158. }
  159.  
  160. proc grab_scsi_host_devs {} {
  161.     catch {
  162.         set hosts [glob /proc/scsi/usb-storage*]
  163.         foreach store_host $hosts {
  164.             set controler [glob "$store_host/*"]
  165.             load_scsi_host $controler
  166.         }
  167.     }
  168. }
  169.  
  170.  
  171.  
  172. proc form_usb_part_list host {
  173.     #puts  " $host [array names ::storages *]"
  174.     foreach dev [array names ::storages *] {
  175.         foreach {h type t id chip} $::storages($dev) {
  176.             if {$host == $h} {
  177.                 foreach D [array names ::partitions *] {
  178.                     if {0 == [string compare -length [string length $dev] $D $dev]} {
  179.                         if {$type=="disk" && [string length $D] > [string length $dev] 
  180.                 && $::partitions($D) > 1} {
  181.                             if {$::debug} {puts "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"}
  182.                             lappend ::infolist "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"
  183.                         }
  184.                         if {$type=="cdrom" && [string length $D] == [string length $dev] 
  185.                 && $::partitions($D) > 1} {
  186.                             if {$::debug} {puts "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"}
  187.                             lappend ::infolist "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.     }
  194. }
  195.  
  196. proc form_usb_list {} {
  197.     foreach host [array names ::scsi_hosts *] {
  198.         set product ""
  199.         set host_type ""
  200.         set devdev ""
  201.         #puts "host $host"
  202.         foreach dev [array names ::storages *] {
  203.             foreach {h type t id chip} $::storages($dev) {
  204.                 if {$host == $h} {
  205.                     set product $t
  206.                     set host_type $type
  207.                     set devdev "/dev/$dev"
  208.                 }
  209.             }
  210.         }
  211.         if {$devdev == ""} {set devdev $host}
  212.         #puts "host $host $product $devdev"
  213.  
  214.         #puts "host $host $::scsi_hosts($host) $::scsi_host_chips($host)"
  215.         if { $::scsi_host_chips($host) == 1 } {
  216.             if {$::debug} {puts "$devdev|$host_type|-|[lindex $::scsi_hosts($host) 0]|[lindex $::scsi_hosts($host) 1]" }
  217.             lappend ::infolist "$devdev|$host_type|-|$product|[lindex $::scsi_hosts($host) 1]"
  218.             form_usb_part_list $host 
  219.         } else {
  220.             if {$::debug} {puts "$devdev|[lindex $::scsi_hosts($host) 2]|-|[lindex $::scsi_hosts($host) 1] ($::scsi_host_chips($host) port)|[lindex $::scsi_hosts($host) 2]" }
  221.             lappend ::infolist "$devdev|[lindex $::scsi_hosts($host) 2]|-|$product ($::scsi_host_chips($host) port)|[lindex $::scsi_hosts($host) 2]"
  222.             form_usb_part_list $host 
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228.  
  229.  
  230. array unset ::partitions *
  231. array unset ::storages *
  232. array unset ::scsi_host_chips *
  233. array unset ::scsi_hosts *
  234. array unset ::mounteds *
  235.  
  236. # scan usb info
  237. grab_scsi_host_devs
  238. grab_proc_scsi
  239.  
  240. # scan all partition info
  241. grab_partitions
  242.  
  243. # construct ordered info about usb
  244. form_usb_list
  245.  
  246.  
  247. ###END###
  248.