diff --git a/doc/man/st-flash.1 b/doc/man/st-flash.1 index 252ffcba8..77a8bb550 100644 --- a/doc/man/st-flash.1 +++ b/doc/man/st-flash.1 @@ -53,6 +53,14 @@ TODO TODO .RS .RE +.TP +.B --flash=\f[I]size\f[] +Override the device's normal flash size, where size is the flash size in bytes. +It can be specified in decimal, octal or hexadecimal. +The size argument can optionally be followed by 'k' for KB or 'm' for MB. +Examples --flash=128k or --flash=0x080k. +.RS +.RE .SH EXAMPLES .PP Flash \f[C]firmware.bin\f[] to device diff --git a/doc/man/st-flash.md b/doc/man/st-flash.md index 4eacb7a07..d1844976c 100644 --- a/doc/man/st-flash.md +++ b/doc/man/st-flash.md @@ -49,6 +49,10 @@ reset --serial *iSerial* : TODO +--flash=fsize +: Where fsize is the size in decimal, octal, or hex followed by an optional multiplier +'k' for KB, or 'm' for MB. +Use a leading "0x" to specify hexadecimal, or a leading zero for octal. # EXAMPLES Flash `firmware.bin` to device diff --git a/include/stlink/tools/flash.h b/include/stlink/tools/flash.h index 6a7b05484..35b3a4251 100644 --- a/include/stlink/tools/flash.h +++ b/include/stlink/tools/flash.h @@ -20,9 +20,10 @@ struct flash_opts int reset; int log_level; enum flash_format format; + size_t flash_size; /* --flash=n[k][m] */ }; -#define FLASH_OPTS_INITIALIZER {0, NULL, {}, NULL, 0, 0, 0, 0, 0 } +#define FLASH_OPTS_INITIALIZER {0, NULL, {}, NULL, 0, 0, 0, 0, 0, 0 } int flash_get_opts(struct flash_opts* o, int ac, char** av); diff --git a/src/tools/flash.c b/src/tools/flash.c index f2c186e95..6fe365f88 100644 --- a/src/tools/flash.c +++ b/src/tools/flash.c @@ -29,12 +29,13 @@ static void cleanup(int signum) { static void usage(void) { - puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--format ] {read|write} /dev/sgX "); + puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--format ] [--flash=] {read|write} /dev/sgX "); puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase"); - puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial ] [--format ] {read|write} "); + puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial ] [--format ] [--flash=] {read|write} "); puts("stlinkv2 command line: ./st-flash [--debug] [--serial ] erase"); puts("stlinkv2 command line: ./st-flash [--debug] [--serial ] reset"); puts(" Use hex format for addr, and ."); + puts(" fsize: Use decimal, octal or hex by prefix 0xXXX for hex, optionally followed by k=KB, or m=MB (eg. --flash=128k)"); puts(" Format may be 'binary' (default) or 'ihex', although must be specified for binary format only."); puts(" ./st-flash [--version]"); } @@ -64,6 +65,11 @@ int main(int ac, char** av) if (sl == NULL) return -1; + if ( o.flash_size != 0u && o.flash_size != sl->flash_size ) { + sl->flash_size = o.flash_size; + printf("Forcing flash size: --flash=0x%08zX\n",sl->flash_size); + } + sl->verbose = o.log_level; connected_stlink = sl; diff --git a/src/tools/flash_opts.c b/src/tools/flash_opts.c index c98424348..4fa39de4c 100644 --- a/src/tools/flash_opts.c +++ b/src/tools/flash_opts.c @@ -77,7 +77,30 @@ int flash_get_opts(struct flash_opts* o, int ac, char** av) else return -1; } - else { + else if ( starts_with(av[0], "--flash=") ) { + const char *arg = av[0] + strlen("--flash="); + char *ep = 0; + + o->flash_size = (uint32_t)strtoul(arg,&ep,0); + while ( *ep ) { + switch ( *ep++ ) { + case 0: + break; + case 'k': + case 'K': + o->flash_size *= 1024u; + break; + case 'm': + case 'M': + o->flash_size *= 1024u * 1024u; + break; + default: + fprintf(stderr,"Invalid --flash=%s\n",arg); + return -1; + } + } + } + else { break; // non-option found }