Changed size argument to hex
[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 <string.h>
7 #include <sys/types.h>
8 #include "stlink-common.h"
9
10
11 struct opts
12 {
13   unsigned int do_read;
14   const char* devname;
15   const char* filename;
16   stm32_addr_t addr;
17   size_t size;
18 };
19
20 static void usage(void)
21 {
22     puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size>");
23     puts("stlinkv2 command line: ./flash {read|write} path addr <size>");
24     puts("                       use hex format for addr and <size>");
25 }
26
27 static int get_opts(struct opts* o, int ac, char** av)
28 {
29   /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
30   /* stlinkv2 command line: ./flash {read|write} path addr <size> */
31
32   unsigned int i = 0;
33
34   if (ac < 3) return -1;
35
36   /* stlinkv2 */
37   o->devname = NULL;
38
39   if (strcmp(av[0], "read") == 0)
40   {
41     o->do_read = 1;
42
43     /* stlinkv1 mode */
44     if (ac == 5)
45     {
46       o->devname = av[1];
47       i = 1;
48     }
49
50     o->size = strtoul(av[i + 3], NULL, 16);
51   }
52   else if (strcmp(av[0], "write") == 0)
53   {
54     o->do_read = 0;
55
56     /* stlinkv1 mode */
57     if (ac == 4)
58     {
59       o->devname = av[1];
60       i = 1;
61     }
62   }
63   else
64   {
65     return -1;
66   }
67
68   o->filename = av[i + 1];
69   o->addr = strtoul(av[i + 2], NULL, 16);
70
71   return 0;
72
73
74
75 int main(int ac, char** av)
76 {
77   stlink_t* sl = NULL;
78   struct opts o;
79   int err = -1;
80
81   if (get_opts(&o, ac - 1, av + 1) == -1)
82   {
83     printf("invalid command line\n");
84     usage();
85     goto on_error;
86   }
87
88   if (o.devname != NULL) /* stlinkv1 */
89   {
90 #if CONFIG_USE_LIBSG
91     static const int scsi_verbose = 2;
92     sl = stlink_quirk_open(o.devname, scsi_verbose);
93     if (sl == NULL) goto on_error;
94 #else
95     printf("not compiled for use with STLink/V1");
96     goto on_error;
97 #endif
98   }
99   else /* stlinkv2 */
100   {
101     sl = stlink_open_usb(1);
102     if (sl == NULL) goto on_error;
103   }
104
105   if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
106     stlink_exit_dfu_mode(sl);
107
108   if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
109     stlink_enter_swd_mode(sl);
110
111   stlink_reset(sl);
112
113   if (o.do_read == 0) /* write */
114   {
115     err = stlink_fwrite_flash(sl, o.filename, o.addr);
116     if (err == -1)
117     {
118       printf("stlink_fwrite_flash() == -1\n");
119       goto on_error;
120     }
121   }
122   else /* read */
123   {
124     err = stlink_fread(sl, o.filename, o.addr, o.size);
125     if (err == -1)
126     {
127       printf("stlink_fread() == -1\n");
128       goto on_error;
129     }
130   }
131
132   /* success */
133   err = 0;
134
135  on_error:
136   if (sl != NULL) stlink_close(sl);
137
138   return err;
139 }