2 * Copyright © 2008 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.
27 #define AO_USB_DESC_STRING 3
34 { 0, "_ao_serial_number", 1 },
35 #define AO_SERIAL_NUMBER (ao_symbols[0].addr)
36 { 0, "_ao_usb_descriptors", 0 },
37 #define AO_USB_DESCRIPTORS (ao_symbols[1].addr)
38 { 0, "_ao_radio_cal", 1 },
39 #define AO_RADIO_CAL (ao_symbols[2].addr)
43 #define NUM_REQUIRED_SYMBOLS 2
46 find_symbols(FILE *map)
49 char *addr, *addr_end;
57 while (fgets(line, sizeof(line), map) != NULL) {
58 line[sizeof(line)-1] = '\0';
59 addr = strtok_r(line, " \t\n", &save);
62 name = strtok_r(NULL, " \t\n", &save);
65 colon = strchr (addr, ':');
68 a = strtoul(colon+1, &addr_end, 16);
69 if (a == ULONG_MAX || addr_end == addr)
71 for (s = 0; s < NUM_SYMBOLS; s++)
72 if (!strcmp(ao_symbols[s].name, name)) {
73 ao_symbols[s].addr = (unsigned) a;
74 if (ao_symbols[s].required)
79 return required >= NUM_REQUIRED_SYMBOLS;
83 rewrite(struct ao_hex_image *image, unsigned addr, char *data, int len)
86 if (addr < image->address || image->address + image->length < addr + len)
88 printf("rewrite %04x:", addr);
89 for (i = 0; i < len; i++)
90 printf (" %02x", image->data[addr - image->address + i]);
92 for (i = 0; i < len; i++)
93 printf (" %02x", data[i]);
95 memcpy(image->data + addr - image->address, data, len);
99 static const struct option options[] = {
100 { .name = "tty", .has_arg = 1, .val = 'T' },
101 { .name = "device", .has_arg = 1, .val = 'D' },
102 { .name = "cal", .has_arg = 1, .val = 'c' },
106 static void usage(char *program)
108 fprintf(stderr, "usage: %s [--tty=<tty-name>] [--device=<device-name>] [--cal=<radio-cal>] file.ihx serial-number\n", program);
113 main (int argc, char **argv)
116 struct ao_hex_file *hex;
117 struct ao_hex_image *image;
137 while ((c = getopt_long(argc, argv, "T:D:c:", options, NULL)) != -1) {
146 cal = strtoul(optarg, &cal_end, 10);
147 if (cal_end == optarg || *cal_end != '\0')
155 filename = argv[optind];
156 if (filename == NULL)
158 mapname = strdup(filename);
159 dot = strrchr(mapname, '.');
160 if (!dot || strcmp(dot, ".ihx") != 0)
164 serial_string = argv[optind + 1];
165 if (serial_string == NULL)
168 file = fopen(filename, "r");
173 map = fopen(mapname, "r");
178 if (!find_symbols(map)) {
179 fprintf(stderr, "Cannot find symbols in \"%s\"\n", mapname);
184 hex = ao_hex_file_read(file, filename);
190 image = ao_hex_image_create(hex);
192 fprintf(stderr, "image create failed\n");
195 ao_hex_file_free(hex);
197 serial = strtoul(serial_string, NULL, 0);
201 serial_int[0] = serial & 0xff;
202 serial_int[1] = (serial >> 8) & 0xff;
204 if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
205 fprintf(stderr, "Cannot rewrite serial integer at %04x\n",
210 if (AO_USB_DESCRIPTORS) {
211 unsigned usb_descriptors;
212 usb_descriptors = AO_USB_DESCRIPTORS - image->address;
214 while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
215 if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
220 usb_descriptors += image->data[usb_descriptors];
222 if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
223 fprintf(stderr, "Cannot rewrite serial string at %04x\n", AO_USB_DESCRIPTORS);
227 serial_ucs2_len = image->data[usb_descriptors] - 2;
228 serial_ucs2 = malloc(serial_ucs2_len);
230 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
234 for (i = serial_ucs2_len / 2; i; i--) {
235 serial_ucs2[i * 2 - 1] = 0;
236 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
239 if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len))
244 cal_int[0] = cal & 0xff;
245 cal_int[1] = (cal >> 8) & 0xff;
246 cal_int[2] = (cal >> 16) & 0xff;
247 cal_int[3] = (cal >> 24) & 0xff;
249 fprintf(stderr, "Cannot find radio calibration location in image\n");
252 if (!rewrite(image, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
253 fprintf(stderr, "Cannot rewrite radio calibration at %04x\n", AO_RADIO_CAL);
258 tty = cc_usbdevs_find_by_arg(device, "TIDongle");
259 dbg = ccdbg_open(tty);
263 ccdbg_add_debug(CC_DEBUG_FLASH);
265 ccdbg_debug_mode(dbg);
267 if (image->address == 0xf000) {
268 printf("Loading %d bytes to execute from RAM\n",
270 ccdbg_write_hex_image(dbg, image, 0);
271 } else if (image->address == 0x0000) {
272 printf("Loading %d bytes to execute from FLASH\n",
274 ccdbg_flash_hex_image(dbg, image);
276 printf("Cannot load code to 0x%04x\n",
278 ao_hex_image_free(image);
282 ccdbg_set_pc(dbg, image->address);