From: le mentec fabien Date: Mon, 17 Oct 2011 09:00:02 +0000 (-0500) Subject: [update] add read command to flash tool X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=ffe5d1f31a80c6d889fed923e8c374a1aa079176;p=fw%2Fstlink [update] add read command to flash tool --- diff --git a/flash/main.c b/flash/main.c index 5233405..791b073 100644 --- a/flash/main.c +++ b/flash/main.c @@ -3,50 +3,115 @@ #include #include +#include +#include #include "stlink-common.h" -int main(int ac, char** av) +struct opts { - /* stlinkv1 command line: ./flash /dev/sgX path addr */ - /* stlinkv2 command line: ./flash path addr */ - - stlink_t* sl = NULL; + unsigned int do_read; + const char* devname; + const char* filename; stm32_addr_t addr; - const char* path; - int err = -1; + size_t size; +}; + + +static int get_opts(struct opts* o, int ac, char** av) +{ + /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr */ + /* stlinkv2 command line: ./flash {read|write} path addr */ + + unsigned int i = 0; + + if (ac < 3) return -1; - if (ac == 4) /* stlinkv1 */ + /* stlinkv2 */ + o->devname = NULL; + + if (strcmp(av[0], "read") == 0) { - static const int scsi_verbose = 2; - sl = stlink_quirk_open(av[1], scsi_verbose); - path = av[2]; - addr = strtoul(av[3], NULL, 16); + o->do_read = 1; + + /* stlinkv1 mode */ + if (ac == 5) + { + o->devname = av[1]; + i = 1; + } + + o->size = strtoul(av[i + 3], NULL, 10); } - else if (ac == 3) /* stlinkv2 */ + else if (strcmp(av[0], "write") == 0) { - sl = stlink_open_usb(NULL, 10); - path = av[1]; - addr = strtoul(av[2], NULL, 16); + o->do_read = 0; + + /* stlinkv1 mode */ + if (ac == 5) + { + o->devname = av[1]; + i = 1; + } + } + else + { + return -1; } - else /* invalid */ + + o->filename = av[i + 1]; + o->addr = strtoul(av[i + 2], NULL, 16); + + return 0; +} + + +int main(int ac, char** av) +{ + stlink_t* sl = NULL; + struct opts o; + int err = -1; + + if (get_opts(&o, ac - 1, av + 1) == -1) { printf("invalid command line\n"); goto on_error; } + if (o.devname != NULL) /* stlinkv1 */ + { + static const int scsi_verbose = 2; + sl = stlink_quirk_open(o.devname, scsi_verbose); + } + else /* stlinkv2 */ + { + sl = stlink_open_usb(NULL, 10); + } + if (sl == NULL) goto on_error; - if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) - stlink_exit_dfu_mode(sl); - stlink_enter_swd_mode(sl); - stlink_reset(sl); + if (o.do_read == 0) /* write */ + { + if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) + stlink_exit_dfu_mode(sl); + stlink_enter_swd_mode(sl); + stlink_reset(sl); - err = stlink_fwrite_flash(sl, path, addr); - if (err == -1) + err = stlink_fwrite_flash(sl, o.filename, o.addr); + if (err == -1) + { + printf("stlink_fwrite_flash() == -1\n"); + goto on_error; + } + } + else /* read */ { - printf("stlink_fwrite_flash() == -1\n"); - goto on_error; + err = stlink_fread(sl, o.filename, o.addr, o.size); + if (err == -1) + { + printf("stlink_fread() == -1\n"); + goto on_error; + } } /* success */