#include <sys/types.h>
#include "stlink-common.h"
-
+enum st_cmds {DO_WRITE = 0, DO_READ = 1, DO_ERASE = 2};
struct opts
{
- unsigned int do_read;
+ enum st_cmds cmd;
const char* devname;
const char* filename;
stm32_addr_t addr;
static void usage(void)
{
puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size>");
+ puts("stlinkv1 command line: ./flash /dev/sgX erase");
puts("stlinkv2 command line: ./flash {read|write} path addr <size>");
+ puts("stlinkv2 command line: ./flash erase");
puts(" use hex format for addr and <size>");
}
unsigned int i = 0;
- if (ac < 3) return -1;
+ if (ac < 1) return -1;
/* stlinkv2 */
o->devname = NULL;
- if (strcmp(av[0], "read") == 0)
+ if (strcmp(av[0], "erase") == 0)
{
- o->do_read = 1;
+ o->cmd = DO_ERASE;
/* stlinkv1 mode */
- if (ac == 5)
+ if (ac == 2)
{
o->devname = av[1];
i = 1;
}
- if (ac > 3)
- o->size = strtoul(av[i + 3], NULL, 16);
}
- else if (strcmp(av[0], "write") == 0)
- {
- o->do_read = 0;
-
- /* stlinkv1 mode */
- if (ac == 4)
- {
- o->devname = av[1];
- i = 1;
- }
- }
- else
- {
- return -1;
+ else {
+ if (ac < 3) return -1;
+ if (strcmp(av[0], "read") == 0)
+ {
+ o->cmd = DO_READ;
+
+ /* stlinkv1 mode */
+ if (ac == 5)
+ {
+ o->devname = av[1];
+ i = 1;
+ }
+ if (ac > 3)
+ o->size = strtoul(av[i + 3], NULL, 16);
+ }
+ else if (strcmp(av[0], "write") == 0)
+ {
+ o->cmd = DO_WRITE;
+
+ /* stlinkv1 mode */
+ if (ac == 4)
+ {
+ o->devname = av[1];
+ i = 1;
+ }
+ }
+ else
+ {
+ return -1;
+ }
}
o->filename = av[i + 1];
if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
stlink_enter_swd_mode(sl);
- if (o.do_read == 0) /* write */
+ if (o.cmd == DO_WRITE) /* write */
{
if ((o.addr >= sl->flash_base) &&
(o.addr < sl->flash_base + sl->flash_size))
goto on_error;
}
}
+ else if (o.cmd == DO_ERASE)
+ {
+ err = stlink_erase_flash_mass(sl);
+ if (err == -1)
+ {
+ printf("stlink_fwrite_flash() == -1\n");
+ goto on_error;
+ }
+ }
else /* read */
{
err = stlink_fread(sl, o.filename, o.addr, o.size);
}
int stlink_erase_flash_mass(stlink_t *sl) {
- /* wait for ongoing op to finish */
- wait_flash_busy(sl);
-
- /* unlock if locked */
- unlock_flash_if(sl);
-
- /* set the mass erase bit */
- set_flash_cr_mer(sl);
-
- /* start erase operation, reset by hw with bsy bit */
- set_flash_cr_strt(sl);
-
- /* wait for completion */
- wait_flash_busy(sl);
-
- /* relock the flash */
- lock_flash(sl);
-
- /* todo: verify the erased memory */
-
+ if (sl->chip_id == STM32_CHIPID_F4) {
+ DLOG("(FIXME) Mass erase of STM32F4\n");
+ }
+ else if (sl->chip_id == STM32_CHIPID_L1_MEDIUM) {
+ /* erase each page */
+ int i = 0, num_pages = sl->flash_size/sl->flash_pgsz;
+ for (i = 0; i < num_pages; i++) {
+ /* addr must be an addr inside the page */
+ stm32_addr_t addr = sl->flash_base + i * sl->flash_pgsz;
+ if (stlink_erase_flash_page(sl, addr) == -1) {
+ WLOG("Failed to erase_flash_page(%#zx) == -1\n", addr);
+ return -1;
+ }
+ fprintf(stdout,"\rFlash page at %5d/%5d erased", i, num_pages);
+ fflush(stdout);
+ }
+ }
+ else {
+ /* wait for ongoing op to finish */
+ wait_flash_busy(sl);
+
+ /* unlock if locked */
+ unlock_flash_if(sl);
+
+ /* set the mass erase bit */
+ set_flash_cr_mer(sl);
+
+ /* start erase operation, reset by hw with bsy bit */
+ set_flash_cr_strt(sl);
+
+ /* wait for completion */
+ wait_flash_busy(sl);
+
+ /* relock the flash */
+ lock_flash(sl);
+
+ /* todo: verify the erased memory */
+ }
return 0;
}