1 /* simple wrapper around the stlink_flash_write function */
8 #include "stlink-common.h"
21 static int get_opts(struct opts* o, int ac, char** av)
23 /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
24 /* stlinkv2 command line: ./flash {read|write} path addr <size> */
28 if (ac < 3) return -1;
33 if (strcmp(av[0], "read") == 0)
44 o->size = strtoul(av[i + 3], NULL, 10);
46 else if (strcmp(av[0], "write") == 0)
62 o->filename = av[i + 1];
63 o->addr = strtoul(av[i + 2], NULL, 16);
69 int main(int ac, char** av)
75 if (get_opts(&o, ac - 1, av + 1) == -1)
77 printf("invalid command line\n");
81 if (o.devname != NULL) /* stlinkv1 */
83 static const int scsi_verbose = 2;
84 sl = stlink_quirk_open(o.devname, scsi_verbose);
85 if (sl == NULL) goto on_error;
89 sl = stlink_open_usb(10);
90 if (sl == NULL) goto on_error;
93 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
94 stlink_exit_dfu_mode(sl);
96 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
97 stlink_enter_swd_mode(sl);
101 if (o.do_read == 0) /* write */
103 err = stlink_fwrite_flash(sl, o.filename, o.addr);
106 printf("stlink_fwrite_flash() == -1\n");
112 err = stlink_fread(sl, o.filename, o.addr, o.size);
115 printf("stlink_fread() == -1\n");
124 if (sl != NULL) stlink_close(sl);