Skip to content

Commit b0dabff

Browse files
committed
revamp stages to be driven by cmdline args
1 parent cdd0a17 commit b0dabff

File tree

1 file changed

+137
-12
lines changed

1 file changed

+137
-12
lines changed

qdload.pl

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44
use warnings;
5-
5+
use Getopt::Long;
66
use Fcntl;
77

88
$| = 1;
@@ -793,6 +793,7 @@ sub doReset2 {
793793
###
794794

795795
sub doStage1 {
796+
my $fname = shift;
796797
my $retval;
797798
my $response;
798799
my ( $fd, $tty ) = setupTTY();
@@ -802,8 +803,6 @@ sub doStage1 {
802803
exit 1;
803804
}
804805

805-
print "Using TTY: $tty\n";
806-
807806
while ( $response = readPacket( $fd, 0.1 ) ) {
808807
print "Ignoring response: ", serialize($response), "\n";
809808
}
@@ -812,16 +811,17 @@ sub doStage1 {
812811

813812
doRequestParam($fd);
814813

815-
print "Uploading file to QDLOAD...\n";
816-
exit 1 if !defined( uploadFile( $fd, 0x2a000000, "hex.bin" ) );
814+
print "Uploading file '", $fname, "' to QDLOAD...\n";
815+
exit 1 if !defined( uploadFile( $fd, 0x2a000000, $fname ) );
817816

818817
print "Executing file...\n";
819818
execute( $fd, 0x2a000000 );
820819
close($fd);
821-
sleep(1);
822820
}
823821

824822
sub doStage2 {
823+
my $laddr = shift;
824+
my $lfname = shift;
825825
my $retval;
826826
my $response;
827827
my ( $fd, $tty ) = setupTTY();
@@ -831,20 +831,145 @@ sub doStage2 {
831831
exit 1;
832832
}
833833

834-
print "Using TTY: $tty\n";
835-
836834
doMagic($fd);
837835

838836
#closeFlush($fd);
839837
doSecureMode($fd);
840838
doOpenMulti($fd);
841-
print "Uploading file...\n";
842-
uploadFile2( $fd, 0x0, "hex2.bin" );
839+
print "Uploading file '", $lfname, "'...\n";
840+
uploadFile2( $fd, $laddr, $lfname );
843841
doCloseFlush($fd);
842+
###doReset2($fd);
843+
close($fd);
844+
}
845+
846+
sub doResetStage {
847+
my $retval;
848+
my $response;
849+
my ( $fd, $tty ) = setupTTY();
850+
851+
if ( !defined $fd ) {
852+
print "Failed to find/open TTY.\n";
853+
exit 1;
854+
}
855+
844856
doReset2($fd);
845857

858+
close($fd);
859+
}
860+
861+
sub doTestCmd {
862+
my $cmd = shift;
863+
my $retval;
864+
my $response;
865+
my ( $fd, $tty ) = setupTTY();
866+
867+
if ( !defined $fd ) {
868+
print "Failed to find/open TTY.\n";
869+
exit 1;
870+
}
871+
872+
doMagic($fd);
873+
874+
print "Sending cmd '", $cmd, "'\n";
875+
if ( !sendPacket( $fd, deserialize($cmd) ) ) {
876+
print "Failed send\n";
877+
exit 1;
878+
}
879+
880+
while ( $response = readPacket( $fd, 2 ) ) {
881+
my @responseBytes = unpack( 'C*', $response );
882+
if ( $responseBytes[0] == 0x0e ) {
883+
884+
# msg
885+
my $msg = pack( 'C*',
886+
map { hex }
887+
split( /\s/, serialize( substr( $response, 1 ) ) ) );
888+
$msg =~ tr/\n/ /;
889+
$msg =~ tr/\r/ /;
890+
print "MSG: ", $msg, "\n";
891+
}
892+
elsif ( $responseBytes[0] == 0x0d ) {
893+
894+
# error
895+
my $msg = pack( 'C*',
896+
map { hex }
897+
split( /\s/, serialize( substr( $response, 5 ) ) ) );
898+
$msg =~ tr/\n//d;
899+
$msg =~ tr/\r//d;
900+
print "ERROR: ", $msg, "\n";
901+
}
902+
else {
903+
print "Response: ", serialize($response), "\n";
904+
}
905+
}
906+
907+
close($fd);
908+
}
909+
910+
sub doUsage {
911+
print "Usage: qdload --pfile <stage1_bin>\n";
912+
print " | --lfile <stage2_file>\n";
913+
print " | --lfile <stage2_file> --laddr <stage2_load_addr>\n";
914+
print " | --lreset\n";
915+
}
916+
917+
###
918+
### main
919+
###
920+
921+
my $opts_result;
922+
my $opts_pfile;
923+
my $opts_lfile;
924+
my $opts_laddr;
925+
my $opts_lreset;
926+
my $opts_testcmd;
927+
928+
$opts_result = GetOptions(
929+
"pfile=s" => \$opts_pfile,
930+
"lfile=s" => \$opts_lfile,
931+
"laddr=o" => \$opts_laddr,
932+
"testcmd=s" => \$opts_testcmd,
933+
"lreset" => \$opts_lreset
934+
);
935+
936+
my $num_remaining_args = $#ARGV + 1;
937+
938+
if ( $num_remaining_args > 0 ) {
939+
doUsage();
940+
exit(1);
941+
}
942+
943+
if ( !defined($opts_pfile)
944+
&& !defined($opts_lfile)
945+
&& !defined($opts_lreset)
946+
&& !defined($opts_testcmd) )
947+
{
948+
doUsage();
949+
exit(1);
950+
}
951+
952+
if ( defined($opts_testcmd) ) {
953+
doTestCmd($opts_testcmd);
954+
exit(0);
955+
}
956+
957+
if ( defined($opts_pfile) ) {
958+
doStage1($opts_pfile);
959+
sleep(2);
960+
}
961+
962+
if ( defined($opts_lfile) ) {
963+
my $addr = 0;
964+
if ( defined($opts_laddr) ) {
965+
$addr = $opts_laddr;
966+
}
967+
doStage2( $addr, $opts_lfile );
968+
}
969+
970+
if ( defined($opts_lreset) ) {
971+
doResetStage();
846972
}
847973

848-
doStage1();
849-
doStage2();
974+
exit 0;
850975

0 commit comments

Comments
 (0)