Merge branch 'master' of https://github.com/texane/stlink
[fw/stlink] / flash / main.c
1 /* simple wrapper around the stlink_flash_write function */
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "stlink-common.h"
7
8
9 int main(int ac, char** av)
10 {
11   /* stlinkv1 command line: ./flash /dev/sgX path addr */
12   /* stlinkv2 command line: ./flash path addr */
13
14   stlink_t* sl = NULL;
15   stm32_addr_t addr;
16   const char* path;
17   int err;
18
19   if (ac == 4) /* stlinkv1 */
20   {
21     static const int scsi_verbose = 2;
22     sl = stlink_quirk_open(av[1], scsi_verbose);
23     path = av[2];
24     addr = strtoul(av[3], NULL, 16);
25   }
26   else if (ac == 3) /* stlinkv2 */
27   {
28     sl = stlink_open_usb(NULL, 10);
29     path = av[1];
30     addr = strtoul(av[2], NULL, 16);
31   }
32   else /* invalid */
33   {
34     printf("invalid command line\n");
35     goto on_error;
36   }
37
38   if (sl == NULL) goto on_error;
39
40   err = stlink_fwrite_flash(sl, path, addr);
41   if (err == -1)
42   {
43     printf("stlink_fwrite_flash() == -1\n");
44     goto on_error;
45   }
46
47  on_error:
48   if (sl != NULL) stlink_close(sl);
49
50   return err;
51 }