From 437ff93d4ba6eca483f19b09cb63459d623f7447 Mon Sep 17 00:00:00 2001 From: Ingo Froehlich Date: Wed, 8 May 2019 17:44:32 +0200 Subject: [PATCH] made script more save --- tools/spi_slave.pl | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tools/spi_slave.pl b/tools/spi_slave.pl index 985a037..7aa866a 100755 --- a/tools/spi_slave.pl +++ b/tools/spi_slave.pl @@ -112,15 +112,15 @@ sub conv_input_string_to_number { sub usage { print < <--chain|c=N> [--register=number] [--data=number] - -examples: spi_flash16bit_slaves.pl -e 0x1212 -c 0 -x time # reads the compile time of 0x1212, chain 0 - spi_flash16bit_slaves.pl -e 0x1212 -c 0 -x pwm --channel=1 # reads the threshold setting of channel 1 - spi_flash16bit_slaves.pl -e=0x1212 -c=0 -x=inputenable --data=0xa55a # disables some channels - spi_flash16bit_slaves.pl --endpoint=0x1212 --chain=0 -x dischargedelayinvert -d 0xff # sets the dischargedelayinvert bits - spi_flash16bit_slaves.pl --endpoint=0x1212 --chain=0 -x dischargedelayinvert # reads the dischargedelayinvert register - spi_flash16bit_slaves.pl -e 0x1212 -c 0 --enablecfgflash=1 # enables the access to the config-flash - spi_flash16bit_slaves.pl --endpoint=0x1212 --chain=0 --dumpcfgflash > flash_dump.txt +usage: spi_slave.pl <--endpoint|e=0xYYYY> <--chain|c=N> [--register=number] [--data=number] + +examples: spi_slave.pl -e 0x1212 -c 0 -x time # reads the compile time of 0x1212, chain 0 + spi_slave.pl -e 0x1212 -c 0 -x pwm --channel=1 # reads the threshold setting of channel 1 + spi_slave.pl -e=0x1212 -c=0 -x=inputenable --data=0xa55a # disables some channels + spi_slave.pl --endpoint=0x1212 --chain=0 -x dischargedelayinvert -d 0xff # sets the dischargedelayinvert bits + spi_slave.pl --endpoint=0x1212 --chain=0 -x dischargedelayinvert # reads the dischargedelayinvert register + spi_slave.pl -e 0x1212 -c 0 --enablecfgflash=1 # enables the access to the config-flash + spi_slave.pl --endpoint=0x1212 --chain=0 --dumpcfgflash > flash_dump.txt Some of the commands can also be used directly, i.e. '-rr=0x12' instead of '-x=rr -register=0x12' @@ -171,6 +171,10 @@ if ($help || !defined $endpoint || !defined $chain) { usage(); } +if (defined $enablecfgflash and (defined $erasecfgflash or defined $dumpcfgflash or defined $writecfgflash or defined $verifycfgflash)) { + die("for security reasons is not allowed to enable cfg flash and to use it in one single command"); +} + $endpoint = &conv_input_string_to_number($endpoint, "endpoint", "hex"); $chain = &conv_input_string_to_number($chain, "chain"); $register = &conv_input_string_to_number($register, "register") if (defined $register); @@ -386,11 +390,27 @@ if (defined $flashcmd) { ############################################################################### +sub checkcfgflash { + my $b = sendcmd(0x5C<<$REGNR | $READ); + foreach my $e (sort keys %$b) { + printf(" %x ",$b->{$e}&0xfff); + if (($b->{$e}&0xfff) ne 0x101) { + die "config flash not enabled"; + } + } +} + sub enableccfgflash { + sendcmd(0x51<<$REGNR | $WRITE | 0x8); + sendcmd(0x50<<$REGNR | $WRITE | 0x0); + sendcmd(0x5C<<$REGNR | $WRITE | 0x103); + sendcmd(0x5C<<$REGNR | $WRITE | 0x105); sendcmd(0x5C<<$REGNR | $WRITE | 0x101); } sub enableuserflash { + sendcmd(0x51<<$REGNR | $WRITE | 0x8); + sendcmd(0x50<<$REGNR | $WRITE | 0x0); sendcmd(0x5C<<$REGNR | $WRITE | 0x100); } @@ -408,7 +428,8 @@ if ($execute eq "enableccfgflash" | defined $enablecfgflash) { ############################################################################### if ($execute eq "dumpcfgflash" | defined $dumpcfgflash) { - enableccfgflash(); + #enableccfgflash(); + checkcfgflash(); my $b = sendcmd(0xFF<<$REGNR | $READ); my $version = $b->{$endpoint}; my $max = 0x23ff; @@ -435,7 +456,8 @@ if ($execute eq "dumpcfgflash" | defined $dumpcfgflash) { } sub erasecfgflash { - enableccfgflash(); + #enableccfgflash(); + checkcfgflash(); while (flash_is_busy()) { printf(" busy - try again\n"); usleep(300000); @@ -450,6 +472,7 @@ if ($execute eq "erasecfgflash" | defined $erasecfgflash) { } if ($execute eq "writecfgflash" | defined $writecfgflash) { + checkcfgflash(); if (!defined($filename)) {$filename=$writecfgflash;} if (!defined $filename) { die "for the command writecfgflash an option or --filename is missing.\n"; @@ -525,6 +548,7 @@ if ($execute eq "writecfgflash" | defined $writecfgflash) { } if ($execute eq "verifycfgflash" | defined $verifycfgflash) { + checkcfgflash(); printf("Verify with file: %s \n", $filename); if (!defined($filename)) {$filename=$verifycfgflash;} if (!defined $filename) { -- 2.43.0