2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
34 #include "ao-usbload.h"
35 #include "ao-selfload.h"
36 #include "ao-verbose.h"
37 #include "ao-editaltos.h"
40 get_uint16(struct cc_usb *cc, uint32_t addr)
43 result = ao_self_get_uint16(cc, addr);
48 * Read a 32-bit value from the target device with arbitrary
52 get_uint32(struct cc_usb *cc, uint32_t addr)
56 result = ao_self_get_uint32(cc, addr);
61 * Check to see if the target device has been
62 * flashed with a similar firmware image before
64 * This is done by looking for the same romconfig version,
65 * which should be at the same location as the linker script
66 * places this at 0x100 from the start of the rom section
69 check_flashed(struct cc_usb *cc)
71 uint16_t romconfig_version = get_uint16(cc, AO_ROMCONFIG_VERSION);
72 uint16_t romconfig_check = get_uint16(cc, AO_ROMCONFIG_CHECK);
74 if (romconfig_version != (uint16_t) ~romconfig_check) {
75 fprintf (stderr, "Device has not been flashed before\n");
81 static const struct option options[] = {
82 { .name = "tty", .has_arg = 1, .val = 'T' },
83 { .name = "device", .has_arg = 1, .val = 'D' },
84 { .name = "raw", .has_arg = 0, .val = 'r' },
85 { .name = "cal", .has_arg = 1, .val = 'c' },
86 { .name = "serial", .has_arg = 1, .val = 's' },
87 { .name = "verbose", .has_arg = 1, .val = 'v' },
88 { .name = "wait", .has_arg = 0, .val = 'w' },
89 { .name = "force", .has_arg = 0, .val = 'f' },
93 static void usage(char *program)
95 fprintf(stderr, "usage: %s [--raw] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] [--wait] [--force] file.{elf,ihx}\n", program);
100 done(struct cc_usb *cc, int code)
102 /* cc_usb_printf(cc, "a\n"); */
108 ends_with(char *whole, char *suffix)
110 int whole_len = strlen(whole);
111 int suffix_len = strlen(suffix);
113 if (suffix_len > whole_len)
115 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
119 ucs2len(uint16_t *ucs2)
128 putucs4(uint32_t c, FILE *file)
133 if (c < 0x80) { d = c; bits= -6; }
134 else if (c < 0x800) { d= ((c >> 6) & 0x1F) | 0xC0; bits= 0; }
135 else if (c < 0x10000) { d= ((c >> 12) & 0x0F) | 0xE0; bits= 6; }
136 else if (c < 0x200000) { d= ((c >> 18) & 0x07) | 0xF0; bits= 12; }
137 else if (c < 0x4000000) { d= ((c >> 24) & 0x03) | 0xF8; bits= 18; }
138 else if (c < 0x80000000) { d= ((c >> 30) & 0x01) | 0xFC; bits= 24; }
141 if (putc (d, file) < 0)
144 for ( ; bits >= 0; bits-= 6)
145 if (putc (((c >> bits) & 0x3F) | 0x80, file) < 0)
152 putucs2str(uint16_t *ucs2str, FILE *file)
156 while ((ucs2 = *ucs2str++) != 0)
161 main (int argc, char **argv)
168 unsigned int serial = 0;
180 struct ao_hex_image *load;
182 struct cc_usb *cc = NULL;
186 struct ao_sym *file_symbols;
187 int num_file_symbols;
188 uint32_t flash_base, flash_bound;
189 int has_flash_size = 0;
192 while ((c = getopt_long(argc, argv, "wrfT:D:c:s:v:", options, NULL)) != -1) {
204 cc_default_timeout = -1;
207 cal = strtoul(optarg, &cal_end, 10);
208 if (cal_end == optarg || *cal_end != '\0')
212 serial = strtoul(optarg, &serial_end, 10);
213 if (serial_end == optarg || *serial_end != '\0')
228 ao_verbose = verbose;
231 ccdbg_add_debug(CC_DEBUG_BITBANG);
233 filename = argv[optind];
234 if (filename == NULL)
237 if (ends_with (filename, ".elf")) {
238 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
239 } else if (ends_with (filename, ".ihx")) {
240 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
245 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
246 fprintf(stderr, "Cannot find required symbols\n");
255 for (tries = 0; tries < 3; tries++) {
256 char *this_tty = tty;
258 this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
260 this_tty = cc_usbdevs_find_by_arg(device, "TeleMega");
262 this_tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
264 this_tty = cc_usbdevs_find_by_arg(device, "TeleGPS");
266 this_tty = getenv("ALTOS_TTY");
268 this_tty="/dev/ttyACM0";
270 cc = cc_usb_open(this_tty);
274 cc_usb_printf(cc, "v\n");
278 cc_usb_getline(cc, line, sizeof(line));
279 if (!strncmp(line, "altos-loader", 12))
281 if (!strncmp(line, "flash-range", 11)) {
283 for (i = 11; i < strlen(line); i++)
286 if (sscanf(line + i, "%x %x", &flash_base, &flash_bound) == 2)
289 if (!strncmp(line, "software-version", 16))
294 printf ("rebooting to loader\n");
295 cc_usb_printf(cc, "X\n");
301 fprintf(stderr, "Cannot switch to boot loader\n");
309 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
318 ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
319 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
320 if (memcmp(block, check, 256) != 0) {
321 fprintf (stderr, "read differed\n");
329 /* If the device can tell us the size of flash, make sure
330 * the image fits in that
332 if (has_flash_size) {
333 if (load->address < flash_base ||
334 load->address + load->length > flash_bound)
336 fprintf(stderr, "Image does not fit on device.\n");
337 fprintf(stderr, " Image base: %08x bounds %08x\n",
338 load->address, load->address + load->length);
339 fprintf(stderr, " Device base: %08x bounds %08x\n",
340 flash_base, flash_bound);
346 /* Go fetch existing config values
349 was_flashed = check_flashed(cc);
353 fprintf (stderr, "Must provide serial number\n");
356 serial = get_uint16(cc, AO_SERIAL_NUMBER);
357 if (!serial || serial == 0xffff) {
358 fprintf (stderr, "Invalid existing serial %d\n", serial);
363 if (!cal && AO_RADIO_CAL && was_flashed) {
364 cal = get_uint32(cc, AO_RADIO_CAL);
365 if (!cal || cal == 0xffffffff) {
366 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
371 if (!force && was_flashed) {
372 struct ao_usb_id new_id, old_id;
373 uint16_t *new_product, *old_product;
374 int new_len, old_len;
376 if (!ao_heximage_usb_id(load, &new_id)) {
377 fprintf(stderr, "Can't get new USB id\n");
381 if (!ao_self_get_usb_id(cc, &old_id)) {
382 fprintf(stderr, "Can't get old USB id\n");
385 if (new_id.vid != old_id.vid || new_id.pid != old_id.pid) {
386 fprintf(stderr, "USB ID mismatch (device is %04x/%04x image is %04x/%04x)\n",
387 old_id.vid, old_id.pid, new_id.vid, new_id.pid);
391 new_product = ao_heximage_usb_product(load);
393 fprintf(stderr, "Can't get new USB product name\n");
396 old_product = ao_self_get_usb_product(cc);
398 fprintf(stderr, "Can't get existing USB product name\n");
401 new_len = ucs2len(new_product);
402 old_len = ucs2len(old_product);
403 if (new_len != old_len || memcmp(new_product, old_product, new_len * 2) != 0) {
404 fprintf(stderr, "USB product mismatch (device is ");
405 putucs2str(old_product, stderr);
406 fprintf(stderr, ", image is ");
407 putucs2str(new_product, stderr);
408 fprintf(stderr, ")\n");
413 if (!ao_editaltos(load, serial, cal))
417 /* And flash the resulting image to the device
419 success = ao_self_write(cc, load);
422 fprintf (stderr, "\"%s\": Write failed\n", filename);