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; version 2 of the License.
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.
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.
33 #include "ao-usbload.h"
34 #include "ao-selfload.h"
35 #include "ao-verbose.h"
36 #include "ao-editaltos.h"
39 get_uint16(struct cc_usb *cc, uint32_t addr)
42 result = ao_self_get_uint16(cc, addr);
43 printf ("read 0x%08x = 0x%04x\n", addr, result);
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);
57 printf ("read 0x%08x = 0x%08x\n", addr, result);
62 * Check to see if the target device has been
63 * flashed with a similar firmware image before
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
70 check_flashed(struct cc_usb *cc)
72 uint16_t romconfig_version = get_uint16(cc, AO_ROMCONFIG_VERSION);
73 uint16_t romconfig_check = get_uint16(cc, AO_ROMCONFIG_CHECK);
75 if (romconfig_version != (uint16_t) ~romconfig_check) {
76 fprintf (stderr, "Device has not been flashed before\n");
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' },
92 static void usage(char *program)
94 fprintf(stderr, "usage: %s [--raw] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
99 done(struct cc_usb *cc, int code)
101 /* cc_usb_printf(cc, "a\n"); */
107 ends_with(char *whole, char *suffix)
109 int whole_len = strlen(whole);
110 int suffix_len = strlen(suffix);
112 if (suffix_len > whole_len)
114 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
118 main (int argc, char **argv)
125 unsigned int serial = 0;
137 struct ao_hex_image *load;
139 struct cc_usb *cc = NULL;
143 struct ao_sym *file_symbols;
144 int num_file_symbols;
146 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
158 cal = strtoul(optarg, &cal_end, 10);
159 if (cal_end == optarg || *cal_end != '\0')
163 serial = strtoul(optarg, &serial_end, 10);
164 if (serial_end == optarg || *serial_end != '\0')
176 ao_verbose = verbose;
179 ccdbg_add_debug(CC_DEBUG_BITBANG);
181 filename = argv[optind];
182 if (filename == NULL)
185 if (ends_with (filename, ".elf")) {
186 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
187 } else if (ends_with (filename, ".ihx")) {
188 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
193 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
194 fprintf(stderr, "Cannot find required symbols\n");
203 for (tries = 0; tries < 3; tries++) {
204 char *this_tty = tty;
206 this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
208 this_tty = cc_usbdevs_find_by_arg(device, "TeleMega");
210 this_tty = getenv("ALTOS_TTY");
212 this_tty="/dev/ttyACM0";
214 cc = cc_usb_open(this_tty);
218 cc_usb_printf(cc, "v\n");
222 cc_usb_getline(cc, line, sizeof(line));
223 if (!strncmp(line, "altos-loader", 12))
225 if (!strncmp(line, "software-version", 16))
230 printf ("rebooting to loader\n");
231 cc_usb_printf(cc, "X\n");
237 fprintf(stderr, "Cannot switch to boot loader\n");
245 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
254 ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
255 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
256 if (memcmp(block, check, 256) != 0) {
257 fprintf (stderr, "read differed\n");
266 /* Go fetch existing config values
269 was_flashed = check_flashed(cc);
273 fprintf (stderr, "Must provide serial number\n");
276 serial = get_uint16(cc, AO_SERIAL_NUMBER);
277 if (!serial || serial == 0xffff) {
278 fprintf (stderr, "Invalid existing serial %d\n", serial);
283 if (!cal && AO_RADIO_CAL && was_flashed) {
284 cal = get_uint32(cc, AO_RADIO_CAL);
285 if (!cal || cal == 0xffffffff) {
286 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
291 if (!ao_editaltos(load, serial, cal))
295 /* And flash the resulting image to the device
297 success = ao_self_write(cc, load);
300 fprintf (stderr, "\"%s\": Write failed\n", filename);