ao-tools/ao-test-gps: Improve output formatting
[fw/altos] / ao-tools / ao-usbload / ao-usbload.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 "ao-elf.h"
30 #include "ccdbg.h"
31 #include "cc-usb.h"
32 #include "cc.h"
33 #include "ao-usbload.h"
34 #include "ao-selfload.h"
35 #include "ao-verbose.h"
36 #include "ao-editaltos.h"
37
38 static uint16_t
39 get_uint16(struct cc_usb *cc, uint32_t addr)
40 {
41         uint16_t        result;
42         result = ao_self_get_uint16(cc, addr);
43         printf ("read 0x%08x = 0x%04x\n", addr, result);
44         return result;
45 }
46
47 /*
48  * Read a 32-bit value from the target device with arbitrary
49  * alignment
50  */
51 static uint32_t
52 get_uint32(struct cc_usb *cc, uint32_t addr)
53 {
54         uint32_t        result;
55
56         result = ao_self_get_uint32(cc, addr);
57         printf ("read 0x%08x = 0x%08x\n", addr, result);
58         return result;
59 }
60
61 /*
62  * Check to see if the target device has been
63  * flashed with a similar firmware image before
64  *
65  * This is done by looking for the same romconfig version,
66  * which should be at the same location as the linker script
67  * places this at 0x100 from the start of the rom section
68  */
69 static int
70 check_flashed(struct cc_usb *cc)
71 {
72         uint16_t        romconfig_version = get_uint16(cc, AO_ROMCONFIG_VERSION);
73         uint16_t        romconfig_check = get_uint16(cc, AO_ROMCONFIG_CHECK);
74
75         if (romconfig_version != (uint16_t) ~romconfig_check) {
76                 fprintf (stderr, "Device has not been flashed before\n");
77                 return 0;
78         }
79         return 1;
80 }
81
82 static const struct option options[] = {
83         { .name = "tty", .has_arg = 1, .val = 'T' },
84         { .name = "device", .has_arg = 1, .val = 'D' },
85         { .name = "raw", .has_arg = 0, .val = 'r' },
86         { .name = "cal", .has_arg = 1, .val = 'c' },
87         { .name = "serial", .has_arg = 1, .val = 's' },
88         { .name = "verbose", .has_arg = 1, .val = 'v' },
89         { 0, 0, 0, 0},
90 };
91
92 static void usage(char *program)
93 {
94         fprintf(stderr, "usage: %s [--raw] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
95         exit(1);
96 }
97
98 void
99 done(struct cc_usb *cc, int code)
100 {
101 /*      cc_usb_printf(cc, "a\n"); */
102         cc_usb_close(cc);
103         exit (code);
104 }
105
106 static int
107 ends_with(char *whole, char *suffix)
108 {
109         int whole_len = strlen(whole);
110         int suffix_len = strlen(suffix);
111
112         if (suffix_len > whole_len)
113                 return 0;
114         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
115 }
116
117 int
118 main (int argc, char **argv)
119 {
120         char                    *device = NULL;
121         char                    *filename;
122         Elf                     *e;
123         int                     raw = 0;
124         char                    *serial_end;
125         unsigned int            serial = 0;
126         char                    *serial_ucs2;
127         int                     serial_ucs2_len;
128         char                    serial_int[2];
129         unsigned int            s;
130         int                     i;
131         int                     string_num;
132         uint32_t                cal = 0;
133         char                    cal_int[4];
134         char                    *cal_end;
135         int                     c;
136         int                     was_flashed = 0;
137         struct ao_hex_image     *load;
138         int                     tries;
139         struct cc_usb           *cc = NULL;
140         char                    *tty = NULL;
141         int                     success;
142         int                     verbose = 0;
143         struct ao_sym           *file_symbols;
144         int                     num_file_symbols;
145         uint32_t                flash_base, flash_bound;
146         int                     has_flash_size = 0;
147
148         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
149                 switch (c) {
150                 case 'T':
151                         tty = optarg;
152                         break;
153                 case 'D':
154                         device = optarg;
155                         break;
156                 case 'r':
157                         raw = 1;
158                         break;
159                 case 'c':
160                         cal = strtoul(optarg, &cal_end, 10);
161                         if (cal_end == optarg || *cal_end != '\0')
162                                 usage(argv[0]);
163                         break;
164                 case 's':
165                         serial = strtoul(optarg, &serial_end, 10);
166                         if (serial_end == optarg || *serial_end != '\0')
167                                 usage(argv[0]);
168                         break;
169                 case 'v':
170                         verbose++;
171                         break;
172                 default:
173                         usage(argv[0]);
174                         break;
175                 }
176         }
177
178         ao_verbose = verbose;
179
180         if (verbose > 1)
181                 ccdbg_add_debug(CC_DEBUG_BITBANG);
182
183         filename = argv[optind];
184         if (filename == NULL)
185                 usage(argv[0]);
186
187         if (ends_with (filename, ".elf")) {
188                 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
189         } else if (ends_with (filename, ".ihx")) {
190                 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
191         } else
192                 usage(argv[0]);
193
194         if (!raw) {
195                 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
196                         fprintf(stderr, "Cannot find required symbols\n");
197                         usage(argv[0]);
198                 }
199         }
200
201         {
202                 int     is_loader;
203                 int     tries;
204
205                 for (tries = 0; tries < 3; tries++) {
206                         char    *this_tty = tty;
207                         if (!this_tty)
208                                 this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
209                         if (!this_tty)
210                                 this_tty = cc_usbdevs_find_by_arg(device, "TeleMega");
211                         if (!this_tty)
212                                 this_tty = getenv("ALTOS_TTY");
213                         if (!this_tty)
214                                 this_tty="/dev/ttyACM0";
215
216                         cc = cc_usb_open(this_tty);
217
218                         if (!cc)
219                                 exit(1);
220                         cc_usb_printf(cc, "v\n");
221                         is_loader = 0;
222                         for (;;) {
223                                 char    line[256];
224                                 cc_usb_getline(cc, line, sizeof(line));
225                                 if (!strncmp(line, "altos-loader", 12))
226                                         is_loader = 1;
227                                 if (!strncmp(line, "flash-range", 11)) {
228                                         int i;
229                                         for (i = 11; i < strlen(line); i++)
230                                                 if (line[i] != ' ')
231                                                         break;
232                                         if (sscanf(line + i, "%x %x", &flash_base, &flash_bound) == 2)
233                                                 has_flash_size = 1;
234                                 }
235                                 if (!strncmp(line, "software-version", 16))
236                                         break;
237                         }
238                         if (is_loader)
239                                 break;
240                         printf ("rebooting to loader\n");
241                         cc_usb_printf(cc, "X\n");
242                         cc_usb_close(cc);
243                         sleep(1);
244                         cc = NULL;
245                 }
246                 if (!is_loader) {
247                         fprintf(stderr, "Cannot switch to boot loader\n");
248                         exit(1);
249                 }
250 #if 0
251                 {
252                         uint8_t check[256];
253                         int     i = 0;
254
255                         ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
256                         for (;;) {
257                                 uint8_t block[256];
258                                 putchar ('.');
259                                 if (++i == 40) {
260                                         putchar('\n');
261                                         i = 0;
262                                 }
263                                 fflush(stdout);
264                                 ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
265                                 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
266                                 if (memcmp(block, check, 256) != 0) {
267                                         fprintf (stderr, "read differed\n");
268                                         exit(1);
269                                 }
270                         }
271                 }
272 #endif
273         }
274
275         /* If the device can tell us the size of flash, make sure
276          * the image fits in that
277          */
278         if (has_flash_size) {
279                 if (load->address < flash_base ||
280                     load->address + load->length > flash_bound)
281                 {
282                         fprintf(stderr, "Image does not fit on device.\n");
283                         fprintf(stderr, "  Image base:  %08x bounds %08x\n",
284                                 load->address, load->address + load->length);
285                         fprintf(stderr, "  Device base: %08x bounds %08x\n",
286                                 flash_base, flash_bound);
287                         done(cc, 1);
288                 }
289         }
290
291         if (!raw) {
292                 /* Go fetch existing config values
293                  * if available
294                  */
295                 was_flashed = check_flashed(cc);
296
297                 if (!serial) {
298                         if (!was_flashed) {
299                                 fprintf (stderr, "Must provide serial number\n");
300                                 done(cc, 1);
301                         }
302                         serial = get_uint16(cc, AO_SERIAL_NUMBER);
303                         if (!serial || serial == 0xffff) {
304                                 fprintf (stderr, "Invalid existing serial %d\n", serial);
305                                 done(cc, 1);
306                         }
307                 }
308
309                 if (!cal && AO_RADIO_CAL && was_flashed) {
310                         cal = get_uint32(cc, AO_RADIO_CAL);
311                         if (!cal || cal == 0xffffffff) {
312                                 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
313                                 done(cc, 1);
314                         }
315                 }
316
317                 if (!ao_editaltos(load, serial, cal))
318                         done(cc, 1);
319         }
320
321         /* And flash the resulting image to the device
322          */
323         success = ao_self_write(cc, load);
324
325         if (!success) {
326                 fprintf (stderr, "\"%s\": Write failed\n", filename);
327                 done(cc, 1);
328         }
329
330         done(cc, 0);
331 }