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