Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit c394d11

Browse files
committed
Add new command line flag "-C" to compute CRC
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
1 parent 247540f commit c394d11

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

main.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int wu = 0;
5959
int rp = 0;
6060
int ur = 0;
6161
int eraseOnly = 0;
62+
int crc = 0;
6263
int npages = 0;
6364
int spage = 0;
6465
int no_erase = 0;
@@ -424,6 +425,19 @@ int main(int argc, char* argv[]) {
424425
fprintf(diag, "Done.\n");
425426
ret = 0;
426427
goto close;
428+
} else if (crc) {
429+
uint32_t crc_val = 0;
430+
431+
fprintf(diag, "CRC computation\n");
432+
433+
if (!stm32_crc_wrapper(stm, start, end - start, &crc_val)) {
434+
fprintf(stderr, "Failed to read CRC\n");
435+
goto close;
436+
}
437+
fprintf(diag, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end,
438+
crc_val);
439+
ret = 0;
440+
goto close;
427441
} else
428442
ret = 0;
429443

@@ -460,7 +474,7 @@ int main(int argc, char* argv[]) {
460474

461475
int parse_options(int argc, char *argv[]) {
462476
int c;
463-
while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfchuos:S:i:R")) != -1) {
477+
while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:i:R")) != -1) {
464478
switch(c) {
465479
case 'a':
466480
port_opts.bus_addr = strtoul(optarg, NULL, 0);
@@ -605,6 +619,10 @@ int parse_options(int argc, char *argv[]) {
605619
case 'R':
606620
reset_flag = 1;
607621
break;
622+
623+
case 'C':
624+
crc = 1;
625+
break;
608626
}
609627
}
610628

@@ -640,6 +658,7 @@ void show_help(char *name) {
640658
" -m mode Serial port mode (default 8e1)\n"
641659
" -r filename Read flash to file (or - stdout)\n"
642660
" -w filename Write flash from file (or - stdout)\n"
661+
" -C Compute CRC of flash content\n"
643662
" -u Disable the flash write-protection\n"
644663
" -j Enable the flash read-protection\n"
645664
" -k Disable the flash read-protection\n"

stm32.c

+26
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,29 @@ uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len)
742742
}
743743
return crc;
744744
}
745+
746+
char stm32_crc_wrapper(const stm32_t *stm, uint32_t address, uint32_t length,
747+
uint32_t *crc)
748+
{
749+
uint8_t buf[256];
750+
uint32_t len, current_crc;
751+
752+
if (stm->cmd->crc != STM32_CMD_ERR)
753+
return stm32_crc_memory(stm, address, length, crc);
754+
755+
current_crc = CRC_INIT_VALUE;
756+
while (length) {
757+
len = length > 256 ? 256 : length;
758+
if (!stm32_read_memory(stm, address, buf, len)) {
759+
fprintf(stderr,
760+
"Failed to read memory at address 0x%08x, target write-protected?\n",
761+
address);
762+
return 0;
763+
}
764+
current_crc = stm32_sw_crc(current_crc, buf, len);
765+
length -= len;
766+
address += len;
767+
}
768+
*crc = current_crc;
769+
return 1;
770+
}

stm32.h

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ char stm32_readprot_memory (const stm32_t *stm);
6262
char stm32_runprot_memory (const stm32_t *stm);
6363
char stm32_crc_memory(const stm32_t *stm, uint32_t address, uint32_t length,
6464
uint32_t *crc);
65+
char stm32_crc_wrapper(const stm32_t *stm, uint32_t address, uint32_t length,
66+
uint32_t *crc);
6567
uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len);
6668

6769
#endif

stm32flash.1

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
stm32flash \- flashing utility for STM32 and STM32W through UART or I2C
44
.SH SYNOPSIS
55
.B stm32flash
6-
.RB [ \-cfhjkouvR ]
6+
.RB [ \-cfhjkouvCR ]
77
.RB [ \-a
88
.IR bus_address ]
99
.RB [ \-b
@@ -138,7 +138,7 @@ Specify flash page offset (0 = flash start).
138138

139139
.TP
140140
.BI "\-S" " address" "[:" "length" "]"
141-
Specify start address and optionally length for read/write/erase operations.
141+
Specify start address and optionally length for read/write/erase/crc operations.
142142

143143
.TP
144144
.B \-f
@@ -166,6 +166,14 @@ for the format of
166166
.I GPIO_string
167167
and further explanation).
168168

169+
.TP
170+
.B \-C
171+
Specify to compute CRC on memory content.
172+
By default the CRC is computed on the whole flash content.
173+
Use
174+
.B "\-S"
175+
to provide different memory address range.
176+
169177
.TP
170178
.B \-R
171179
Specify to reset the device at exit.

0 commit comments

Comments
 (0)