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.
24 #define AO_USB_DESC_STRING 3
29 fprintf(stderr, "usage: %s <filename.ihx> <serial>\n", program);
36 } serial_symbols[] = {
37 { 0, "_ao_serial_number" },
38 #define AO_SERIAL_NUMBER (serial_symbols[0].addr)
39 { 0, "_ao_usb_descriptors" },
40 #define AO_USB_DESCRIPTORS (serial_symbols[1].addr)
43 #define NUM_SERIAL_SYMBOLS (sizeof(serial_symbols)/sizeof(serial_symbols[0]))
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_SERIAL_SYMBOLS; s++)
72 if (!strcmp(serial_symbols[s].name, name)) {
73 serial_symbols[s].addr = (unsigned) a;
78 return found == NUM_SERIAL_SYMBOLS;
82 rewrite(struct hex_image *image, unsigned addr, char *data, int len)
85 if (addr < image->address || image->address + image->length < addr + len)
87 printf("rewrite %04x:", addr);
88 for (i = 0; i < len; i++)
89 printf (" %02x", image->data[addr - image->address + i]);
91 for (i = 0; i < len; i++)
92 printf (" %02x", data[i]);
94 memcpy(image->data + addr - image->address, data, len);
98 main (int argc, char **argv)
103 struct hex_file *hex;
104 struct hex_image *image;
116 unsigned usb_descriptors;
120 if (filename == NULL)
122 mapname = strdup(filename);
123 dot = strrchr(mapname, '.');
124 if (!dot || strcmp(dot, ".ihx") != 0)
128 serial_string = argv[2];
129 if (serial_string == NULL)
132 file = fopen(filename, "r");
137 map = fopen(mapname, "r");
142 if (!find_symbols(map)) {
143 fprintf(stderr, "Cannot find symbols in \"%s\"\n", mapname);
148 hex = ccdbg_hex_file_read(file, filename);
154 image = ccdbg_hex_image_create(hex);
156 fprintf(stderr, "image create failed\n");
159 ccdbg_hex_file_free(hex);
161 serial = strtoul(serial_string, NULL, 0);
165 serial_int[0] = serial & 0xff;
166 serial_int[1] = (serial >> 8) & 0xff;
168 if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
169 fprintf(stderr, "Cannot rewrite serial integer at %04x\n",
174 usb_descriptors = AO_USB_DESCRIPTORS - image->address;
176 while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
177 if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
182 usb_descriptors += image->data[usb_descriptors];
184 if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
185 fprintf(stderr, "Cannot rewrite serial string at %04x\n", AO_USB_DESCRIPTORS);
189 serial_ucs2_len = image->data[usb_descriptors] - 2;
190 serial_ucs2 = malloc(serial_ucs2_len);
192 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
196 for (i = serial_ucs2_len / 2; i; i--) {
197 serial_ucs2[i * 2 - 1] = 0;
198 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
201 if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len))
208 ccdbg_add_debug(CC_DEBUG_FLASH);
210 ccdbg_debug_mode(dbg);
212 if (image->address == 0xf000) {
213 printf("Loading %d bytes to execute from RAM\n",
215 ccdbg_write_hex_image(dbg, image, 0);
216 } else if (image->address == 0x0000) {
217 printf("Loading %d bytes to execute from FLASH\n",
219 ccdbg_flash_hex_image(dbg, image);
221 printf("Cannot load code to 0x%04x\n",
223 ccdbg_hex_image_free(image);
227 ccdbg_set_pc(dbg, image->address);