Add smartflash utility set

This commit is contained in:
Stefan `Sec` Zehl 2011-12-25 18:40:32 +01:00
parent 806c2eb222
commit 43b1e47247
3 changed files with 208 additions and 0 deletions

3
tools/smartflash/flash Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
dd if=firmware.bin seek=4 of=/dev/$1 2>/dev/null
echo "FLASH done: $1"

52
tools/smartflash/mass Executable file
View File

@ -0,0 +1,52 @@
#!/bin/sh
dev=$1
dir=/tmp/r0ket
set -e
if [ ! -d $dir ] ; then
mkdir $dir
fi
if [ ! -d $dir/$dev ] ; then
mkdir $dir/$dev
fi
umount /dev/$dev 2>/dev/null || true
./generate-keys
mount /dev/$dev -t vfat $dir/$dev
cp files/* $dir/$dev
cp \
1files/invaders.c0d \
1files/mandel.c0d \
1files/r_player.c0d \
1files/jump.c0d \
1files/bricks.c0d \
1files/rockets.c0d \
1files/fahrplan.c0d \
1files/r0type.c0d \
1files/beaconid.c0d \
1files/people.c0d \
1files/starfld.c0d \
1files/static.c0d \
1files/sendcard.c0d \
1files/recvcard.c0d \
1files/showcard.c0d \
1files/pongo.c0d \
1files/minichat.c0d \
1files/blink.c0d \
1files/pwgen.c0d \
1files/showlcd.c0d \
1files/tedliz.c0d \
1files/leiwand.c0d \
1files/scope.c0d \
1files/Geigerct.c0d \
1files/voltage.c0d \
files/* \
$dir/$dev
#sync
#ls -l $dir/$dev
umount /dev/$dev
echo "MASS done: $dev $dir"

153
tools/smartflash/smartflash Executable file
View File

@ -0,0 +1,153 @@
#!/usr/bin/perl
use strict;
$|=1;
my $DEV="/sys/bus/usb/devices";
sub getline{
my($path)=@_;
open(my $f,"<",$DEV."/".$path) || do {
# print "failed opening $path: $!\n";
return "";
};
my $l=<$f>;
close($f);
chomp($l);
return $l;
};
my %found;
my %done;
sub gosub {
my ($dev,$grep)=@_;
my $dh;
my $dir;
opendir($dh, $DEV."/".$dev) || do {
# print "Failure for $dev?\n";
return "";
};
($dir)=grep {/$grep/} grep {/^[^.]/} readdir($dh);
if($dir eq ""){
return "";
};
close $dh;
$dev.="/".$dir;
return $dev;
};
sub getdev{
my ($dev)=@_;
my $dh;
my $dir;
$dev=gosub($dev,":1");
$dev=gosub($dev,"^host");
$dev=gosub($dev,"^target");
$dev=~/target(.*)/;
$dev=gosub($dev,$1);
$dev=gosub($dev,"^block");
$dev=gosub($dev,".");
$dev=~m!/([^/]*)$!;
my $r=$1;
if($r!~/^sd/){
# print "Strange device? $dev -> $r\n";
return "";
};
return $r;
};
my %pids;
use POSIX ":sys_wait_h";
my $kid;
$SIG{CHLD}=sub {
do {
$kid = waitpid(-1, WNOHANG);
delete $pids{$kid};
}while $kid>0;
};
sub dwim{
my($dev)=@_;
# print ("checking $dev\n");
my $pr=getline($dev."/idProduct");
return -1 if($pr eq "");
my $ve=getline($dev."/idVendor");
my $mf=getline($dev."/manufacturer");
# print "$pr:$ve\n";
if("$ve:$pr" eq "16c0:08ac") {
return 1 if $done{$dev}==1;
my $sdev=getdev($dev);
if ($sdev eq ""){
print "$dev not ready.\n";
return 1;
};
print "Mass storage $dev -> $sdev\n";
my $pid = fork();
if (not defined $pid) {
die "fork failed\n";
} elsif ($pid == 0) {
system("./mass $sdev");
exit(0);
} else {
# print "IM THE PARENT $pid\n";
$pids{$pid}=1;
# print join(",",%pids),"\n";
# print scalar keys %pids;
# print "\n";
}
$done{$dev}=1;
return 1;
};
if("$ve:$pr" eq "04cc:0003") {
return 1 if $done{$dev}==2;
my $sdev=getdev($dev);
if ($sdev eq ""){
print "$dev not ready.\n";
return 1;
};
print "flash $dev -> $sdev\n";
system("./flash $sdev");
$done{$dev}=2;
return 1;
};
return -1;
};
my %disable;
while(1){
opendir(my $dh, $DEV);
my @paths=readdir($dh);
close $dh;
# print "f: ",join(",",@files),"\n";
device:
for my $path (@paths){
next if ($disable{$path});
next if $path =~ /^usb/;
next if $path =~ /:/;
# print "p $path\n";
$found{$path}=1;
my $res=dwim($path);
if($res==-1){
print "Disabling: $path\n";
$disable{$path}=1;
};
};
for my $a (keys %done){
if (!$found{$a}){
delete $done{$a};
print "$a vanished\n";
};
};
%found=();
# print "\n";
sleep(1);
print scalar(keys %pids),"\r";
};