b6b4abc63720c2be99a518d21f9913ad81a317e2
[fw/altos] / ao-tools / ao-stmload / ao-stmload.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <err.h>
19 #include <fcntl.h>
20 #include <gelf.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <sysexits.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include "stlink-common.h"
30 #include "ao-elf.h"
31 #include "ccdbg.h"
32 #include "cc.h"
33 #include "ao-stmload.h"
34 #include "ao-selfload.h"
35 #include "ao-verbose.h"
36 #include "ao-editaltos.h"
37
38
39 /*
40  * Read a 16-bit value from the target device with arbitrary
41  * alignment
42  */
43 static uint16_t
44 get_uint16_sl(stlink_t *sl, uint32_t addr)
45 {
46         const           uint8_t *data = sl->q_buf;
47         uint32_t        actual_addr;
48         int             off;
49         uint16_t        result;
50
51         sl->q_len = 0;
52
53
54         actual_addr = addr & ~3;
55         
56         stlink_read_mem32(sl, actual_addr, 8);
57
58         if (sl->q_len != 8)
59                 abort();
60
61         off = addr & 3;
62         result = data[off] | (data[off + 1] << 8);
63         return result;
64 }
65
66 static uint16_t
67 get_uint16(stlink_t *sl, uint32_t addr)
68 {
69         uint16_t        result;
70         result = get_uint16_sl(sl, addr);
71         printf ("read 0x%08x = 0x%04x\n", addr, result);
72         return result;
73 }
74
75 /*
76  * Read a 32-bit value from the target device with arbitrary
77  * alignment
78  */
79 static uint32_t
80 get_uint32_sl(stlink_t *sl, uint32_t addr)
81 {
82         const           uint8_t *data = sl->q_buf;
83         uint32_t        actual_addr;
84         int             off;
85         uint32_t        result;
86
87         sl->q_len = 0;
88
89         printf ("read 0x%x\n", addr);
90
91         actual_addr = addr & ~3;
92         
93         stlink_read_mem32(sl, actual_addr, 8);
94
95         if (sl->q_len != 8)
96                 abort();
97
98         off = addr & 3;
99         result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
100         return result;
101 }
102
103 /*
104  * Read a 32-bit value from the target device with arbitrary
105  * alignment
106  */
107 static uint32_t
108 get_uint32(stlink_t *sl, uint32_t addr)
109 {
110         uint32_t        result;
111
112         result = get_uint32_sl(sl, addr);
113         printf ("read 0x%08x = 0x%08x\n", addr, result);
114         return result;
115 }
116
117 /*
118  * Check to see if the target device has been
119  * flashed with a similar firmware image before
120  *
121  * This is done by looking for the same romconfig version,
122  * which should be at the same location as the linker script
123  * places this at 0x100 from the start of the rom section
124  */
125 static int
126 check_flashed(stlink_t *sl)
127 {
128         uint16_t        romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
129         uint16_t        romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
130
131         if (romconfig_version != (uint16_t) ~romconfig_check) {
132                 fprintf (stderr, "Device has not been flashed before\n");
133                 return 0;
134         }
135         return 1;
136 }
137
138 static const struct option options[] = {
139         { .name = "tty", .has_arg = 1, .val = 'T' },
140         { .name = "device", .has_arg = 1, .val = 'D' },
141         { .name = "cal", .has_arg = 1, .val = 'c' },
142         { .name = "serial", .has_arg = 1, .val = 's' },
143         { .name = "verbose", .has_arg = 1, .val = 'v' },
144         { 0, 0, 0, 0},
145 };
146
147 static void usage(char *program)
148 {
149         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
150         exit(1);
151 }
152
153 void
154 done(stlink_t *sl, int code)
155 {
156         stlink_reset(sl);
157         stlink_run(sl);
158         stlink_exit_debug_mode(sl);
159         stlink_close(sl);
160         exit (code);
161 }
162
163 static int
164 ends_with(char *whole, char *suffix)
165 {
166         int whole_len = strlen(whole);
167         int suffix_len = strlen(suffix);
168
169         if (suffix_len > whole_len)
170                 return 0;
171         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
172 }
173
174 int
175 main (int argc, char **argv)
176 {
177         char                    *device = NULL;
178         char                    *filename;
179         Elf                     *e;
180         char                    *serial_end;
181         unsigned int            serial = 0;
182         char                    *serial_ucs2;
183         int                     serial_ucs2_len;
184         char                    serial_int[2];
185         unsigned int            s;
186         int                     i;
187         int                     string_num;
188         uint32_t                cal = 0;
189         char                    cal_int[4];
190         char                    *cal_end;
191         int                     c;
192         stlink_t                *sl = NULL;
193         int                     was_flashed = 0;
194         struct ao_hex_image     *load;
195         int                     tries;
196         char                    *tty = NULL;
197         int                     success;
198         int                     verbose = 0;
199         struct ao_sym           *file_symbols;
200         int                     num_file_symbols;
201
202         while ((c = getopt_long(argc, argv, "T:D:c:s:v:", options, NULL)) != -1) {
203                 switch (c) {
204                 case 'T':
205                         tty = optarg;
206                         break;
207                 case 'D':
208                         device = optarg;
209                         break;
210                 case 'c':
211                         cal = strtoul(optarg, &cal_end, 10);
212                         if (cal_end == optarg || *cal_end != '\0')
213                                 usage(argv[0]);
214                         break;
215                 case 's':
216                         serial = strtoul(optarg, &serial_end, 10);
217                         if (serial_end == optarg || *serial_end != '\0')
218                                 usage(argv[0]);
219                         break;
220                 case 'v':
221                         verbose++;
222                         break;
223                 default:
224                         usage(argv[0]);
225                         break;
226                 }
227         }
228
229         ao_verbose = verbose;
230
231         if (verbose > 1)
232                 ccdbg_add_debug(CC_DEBUG_BITBANG);
233
234         filename = argv[optind];
235         if (filename == NULL)
236                 usage(argv[0]);
237
238         if (ends_with (filename, ".elf")) {
239                 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
240         } else if (ends_with (filename, ".ihx")) {
241                 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
242         } else
243                 usage(argv[0]);
244
245         if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
246                 fprintf(stderr, "Cannot find required symbols\n");
247                 usage(argv[0]);
248         }
249
250         /* Connect to the programming dongle
251          */
252         
253         for (tries = 0; tries < 3; tries++) {
254                 if (device) {
255                         sl = stlink_v1_open(50);
256                 } else {
257                         sl = stlink_open_usb(50);
258                 
259                 }
260                 if (!sl) {
261                         fprintf (stderr, "No STLink devices present\n");
262                         done (sl, 1);
263                 }
264
265                 if (sl->chip_id != 0)
266                         break;
267                 stlink_reset(sl);
268                 stlink_close(sl);
269                 sl = NULL;
270         }
271         if (!sl) {
272                 fprintf (stderr, "Debugger connection failed\n");
273                 exit(1);
274         }
275
276         /* Verify that the loaded image fits entirely within device flash
277          */
278         if (load->address < sl->flash_base ||
279             sl->flash_base + sl->flash_size < load->address + load->length) {
280                 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
281                          load->address, load->address + load->length);
282                 done(sl, 1);
283         }
284
285         /* Enter debugging mode
286          */
287         if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
288                 stlink_exit_dfu_mode(sl);
289
290         if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
291                 stlink_enter_swd_mode(sl);
292
293         /* Go fetch existing config values
294          * if available
295          */
296         was_flashed = check_flashed(sl);
297
298         if (!serial) {
299                 if (!was_flashed) {
300                         fprintf (stderr, "Must provide serial number\n");
301                         done(sl, 1);
302                 }
303                 serial = get_uint16(sl, AO_SERIAL_NUMBER);
304                 if (!serial || serial == 0xffff) {
305                         fprintf (stderr, "Invalid existing serial %d\n", serial);
306                         done(sl, 1);
307                 }
308         }
309
310         if (!cal && AO_RADIO_CAL && was_flashed) {
311                 cal = get_uint32(sl, AO_RADIO_CAL);
312                 if (!cal || cal == 0xffffffff) {
313                         fprintf (stderr, "Invalid existing rf cal %d\n", cal);
314                         done(sl, 1);
315                 }
316         }
317
318         if (!ao_editaltos(load, serial, cal))
319                 done(sl, 1);
320
321         /* And flash the resulting image to the device
322          */
323
324         success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
325                 
326         if (!success) {
327                 fprintf (stderr, "\"%s\": Write failed\n", filename);
328                 done(sl, 1);
329         }
330
331         done(sl, 0);
332 }