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);
44 printf ("read 0x%08x = 0x%04x\n", addr, result);
49 * Read a 32-bit value from the target device with arbitrary
53 get_uint32(struct cc_usb *cc, uint32_t addr)
57 result = ao_self_get_uint32(cc, addr);
58 printf ("read 0x%08x = 0x%08x\n", addr, result);
63 * Check to see if the target device has been
64 * flashed with a similar firmware image before
66 * This is done by looking for the same romconfig version,
67 * which should be at the same location as the linker script
68 * places this at 0x100 from the start of the rom section
71 check_flashed(struct cc_usb *cc)
73 uint16_t romconfig_version = get_uint16(cc, AO_ROMCONFIG_VERSION);
74 uint16_t romconfig_check = get_uint16(cc, AO_ROMCONFIG_CHECK);
76 if (romconfig_version != (uint16_t) ~romconfig_check) {
77 fprintf (stderr, "Device has not been flashed before\n");
83 static const struct option options[] = {
84 { .name = "tty", .has_arg = 1, .val = 'T' },
85 { .name = "device", .has_arg = 1, .val = 'D' },
86 { .name = "raw", .has_arg = 0, .val = 'r' },
87 { .name = "cal", .has_arg = 1, .val = 'c' },
88 { .name = "serial", .has_arg = 1, .val = 's' },
89 { .name = "verbose", .has_arg = 1, .val = 'v' },
90 { .name = "wait", .has_arg = 0, .val = 'w' },
94 static void usage(char *program)
96 fprintf(stderr, "usage: %s [--raw] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] [--wait] file.{elf,ihx}\n", program);
101 done(struct cc_usb *cc, int code)
103 /* cc_usb_printf(cc, "a\n"); */
109 ends_with(char *whole, char *suffix)
111 int whole_len = strlen(whole);
112 int suffix_len = strlen(suffix);
114 if (suffix_len > whole_len)
116 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
120 main (int argc, char **argv)
127 unsigned int serial = 0;
139 struct ao_hex_image *load;
141 struct cc_usb *cc = NULL;
145 struct ao_sym *file_symbols;
146 int num_file_symbols;
147 uint32_t flash_base, flash_bound;
148 int has_flash_size = 0;
150 while ((c = getopt_long(argc, argv, "wrT:D:c:s:v:", options, NULL)) != -1) {
162 cc_default_timeout = -1;
165 cal = strtoul(optarg, &cal_end, 10);
166 if (cal_end == optarg || *cal_end != '\0')
170 serial = strtoul(optarg, &serial_end, 10);
171 if (serial_end == optarg || *serial_end != '\0')
183 ao_verbose = verbose;
186 ccdbg_add_debug(CC_DEBUG_BITBANG);
188 filename = argv[optind];
189 if (filename == NULL)
192 if (ends_with (filename, ".elf")) {
193 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
194 } else if (ends_with (filename, ".ihx")) {
195 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
200 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
201 fprintf(stderr, "Cannot find required symbols\n");
210 for (tries = 0; tries < 3; tries++) {
211 char *this_tty = tty;
213 this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
215 this_tty = cc_usbdevs_find_by_arg(device, "TeleMega");
217 this_tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
219 this_tty = cc_usbdevs_find_by_arg(device, "TeleGPS");
221 this_tty = getenv("ALTOS_TTY");
223 this_tty="/dev/ttyACM0";
225 cc = cc_usb_open(this_tty);
229 cc_usb_printf(cc, "v\n");
233 cc_usb_getline(cc, line, sizeof(line));
234 if (!strncmp(line, "altos-loader", 12))
236 if (!strncmp(line, "flash-range", 11)) {
238 for (i = 11; i < strlen(line); i++)
241 if (sscanf(line + i, "%x %x", &flash_base, &flash_bound) == 2)
244 if (!strncmp(line, "software-version", 16))
249 printf ("rebooting to loader\n");
250 cc_usb_printf(cc, "X\n");
256 fprintf(stderr, "Cannot switch to boot loader\n");
264 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
273 ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
274 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
275 if (memcmp(block, check, 256) != 0) {
276 fprintf (stderr, "read differed\n");
284 /* If the device can tell us the size of flash, make sure
285 * the image fits in that
287 if (has_flash_size) {
288 if (load->address < flash_base ||
289 load->address + load->length > flash_bound)
291 fprintf(stderr, "Image does not fit on device.\n");
292 fprintf(stderr, " Image base: %08x bounds %08x\n",
293 load->address, load->address + load->length);
294 fprintf(stderr, " Device base: %08x bounds %08x\n",
295 flash_base, flash_bound);
301 /* Go fetch existing config values
304 was_flashed = check_flashed(cc);
308 fprintf (stderr, "Must provide serial number\n");
311 serial = get_uint16(cc, AO_SERIAL_NUMBER);
312 if (!serial || serial == 0xffff) {
313 fprintf (stderr, "Invalid existing serial %d\n", serial);
318 if (!cal && AO_RADIO_CAL && was_flashed) {
319 cal = get_uint32(cc, AO_RADIO_CAL);
320 if (!cal || cal == 0xffffffff) {
321 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
326 if (!ao_editaltos(load, serial, cal))
330 /* And flash the resulting image to the device
332 success = ao_self_write(cc, load);
335 fprintf (stderr, "\"%s\": Write failed\n", filename);