ao-load: Make ao_sym static to avoid collision with ao-editaltos
[fw/altos] / ao-tools / ao-load / ao-load.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <stdlib.h>
20 #include <limits.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include "ccdbg.h"
25 #include "cc.h"
26
27 #define AO_USB_DESC_STRING              3
28
29 static struct sym {
30         unsigned        addr;
31         char            *name;
32         int             required;
33 } ao_symbols[] = {
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)
40 };
41
42 #define NUM_SYMBOLS             3
43 #define NUM_REQUIRED_SYMBOLS    2
44
45 static int
46 find_symbols(FILE *map)
47 {
48         char    line[2048];
49         char    *addr, *addr_end;
50         char    *name;
51         char    *save;
52         char    *colon;
53         unsigned long   a;
54         int     s;
55         int     required = 0;
56
57         while (fgets(line, sizeof(line), map) != NULL) {
58                 line[sizeof(line)-1] = '\0';
59                 addr = strtok_r(line, " \t\n", &save);
60                 if (!addr)
61                         continue;
62                 name = strtok_r(NULL, " \t\n", &save);
63                 if (!name)
64                         continue;
65                 colon = strchr (addr, ':');
66                 if (!colon)
67                         continue;
68                 a = strtoul(colon+1, &addr_end, 16);
69                 if (a == ULONG_MAX || addr_end == addr)
70                         continue;
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)
75                                         ++required;
76                                 break;
77                         }
78         }
79         return required >= NUM_REQUIRED_SYMBOLS;
80 }
81
82 static int
83 rewrite(struct ao_hex_image *image, unsigned addr, char *data, int len)
84 {
85         int i;
86         if (addr < image->address || image->address + image->length < addr + len)
87                 return 0;
88         printf("rewrite %04x:", addr);
89         for (i = 0; i < len; i++)
90                 printf (" %02x", image->data[addr - image->address + i]);
91         printf(" ->");
92         for (i = 0; i < len; i++)
93                 printf (" %02x", data[i]);
94         printf("\n");
95         memcpy(image->data + addr - image->address, data, len);
96 }
97
98 static const struct option options[] = {
99         { .name = "tty", .has_arg = 1, .val = 'T' },
100         { .name = "device", .has_arg = 1, .val = 'D' },
101         { .name = "cal", .has_arg = 1, .val = 'c' },
102         { 0, 0, 0, 0},
103 };
104
105 static void usage(char *program)
106 {
107         fprintf(stderr, "usage: %s [--tty=<tty-name>] [--device=<device-name>] [--cal=<radio-cal>] file.ihx serial-number\n", program);
108         exit(1);
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114         struct ccdbg    *dbg;
115         uint8_t         status;
116         uint16_t        pc;
117         struct ao_hex_file      *hex;
118         struct ao_hex_image *image;
119         char            *filename;
120         FILE            *file;
121         FILE            *map;
122         char            *serial_string;
123         unsigned int    serial;
124         char            *mapname, *dot;
125         char            *serial_ucs2;
126         int             serial_ucs2_len;
127         char            serial_int[2];
128         unsigned int    s;
129         int             i;
130         int             string_num;
131         char            *tty = NULL;
132         char            *device = NULL;
133         uint32_t        cal = 0;
134         char            cal_int[4];
135         char            *cal_end;
136         int             c;
137
138         while ((c = getopt_long(argc, argv, "T:D:c:", options, NULL)) != -1) {
139                 switch (c) {
140                 case 'T':
141                         tty = optarg;
142                         break;
143                 case 'D':
144                         device = optarg;
145                         break;
146                 case 'c':
147                         cal = strtoul(optarg, &cal_end, 10);
148                         if (cal_end == optarg || *cal_end != '\0')
149                                 usage(argv[0]);
150                         break;
151                 default:
152                         usage(argv[0]);
153                         break;
154                 }
155         }
156         filename = argv[optind];
157         if (filename == NULL)
158                 usage(argv[0]);
159         mapname = strdup(filename);
160         dot = strrchr(mapname, '.');
161         if (!dot || strcmp(dot, ".ihx") != 0)
162                 usage(argv[0]);
163         strcpy(dot, ".map");
164
165         serial_string = argv[optind + 1];
166         if (serial_string == NULL)
167                 usage(argv[0]);
168
169         file = fopen(filename, "r");
170         if (!file) {
171                 perror(filename);
172                 exit(1);
173         }
174         map = fopen(mapname, "r");
175         if (!map) {
176                 perror(mapname);
177                 exit(1);
178         }
179         if (!find_symbols(map)) {
180                 fprintf(stderr, "Cannot find symbols in \"%s\"\n", mapname);
181                 exit(1);
182         }
183         fclose(map);
184
185         hex = ao_hex_file_read(file, filename);
186         fclose(file);
187         if (!hex) {
188                 perror(filename);
189                 exit (1);
190         }
191         image = ao_hex_image_create(hex);
192         if (!image) {
193                 fprintf(stderr, "image create failed\n");
194                 exit (1);
195         }
196         ao_hex_file_free(hex);
197
198         serial = strtoul(serial_string, NULL, 0);
199         if (!serial)
200                 usage(argv[0]);
201
202         serial_int[0] = serial & 0xff;
203         serial_int[1] = (serial >> 8) & 0xff;
204
205         if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
206                 fprintf(stderr, "Cannot rewrite serial integer at %04x\n",
207                         AO_SERIAL_NUMBER);
208                 exit(1);
209         }
210
211         if (AO_USB_DESCRIPTORS) {
212                 unsigned        usb_descriptors;
213                 usb_descriptors = AO_USB_DESCRIPTORS - image->address;
214                 string_num = 0;
215                 while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
216                         if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
217                                 ++string_num;
218                                 if (string_num == 4)
219                                         break;
220                         }
221                         usb_descriptors += image->data[usb_descriptors];
222                 }
223                 if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
224                         fprintf(stderr, "Cannot rewrite serial string at %04x\n", AO_USB_DESCRIPTORS);
225                         exit(1);
226                 }
227
228                 serial_ucs2_len = image->data[usb_descriptors] - 2;
229                 serial_ucs2 = malloc(serial_ucs2_len);
230                 if (!serial_ucs2) {
231                         fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
232                         exit(1);
233                 }
234                 s = serial;
235                 for (i = serial_ucs2_len / 2; i; i--) {
236                         serial_ucs2[i * 2 - 1] = 0;
237                         serial_ucs2[i * 2 - 2] = (s % 10) + '0';
238                         s /= 10;
239                 }
240                 if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len))
241                         usage(argv[0]);
242         }
243
244         if (cal) {
245                 cal_int[0] = cal & 0xff;
246                 cal_int[1] = (cal >> 8) & 0xff;
247                 cal_int[2] = (cal >> 16) & 0xff;
248                 cal_int[3] = (cal >> 24) & 0xff;
249                 if (!AO_RADIO_CAL) {
250                         fprintf(stderr, "Cannot find radio calibration location in image\n");
251                         exit(1);
252                 }
253                 if (!rewrite(image, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
254                         fprintf(stderr, "Cannot rewrite radio calibration at %04x\n", AO_RADIO_CAL);
255                         exit(1);
256                 }
257         }
258         if (!tty)
259                 tty = cc_usbdevs_find_by_arg(device, "TIDongle");
260         dbg = ccdbg_open(tty);
261         if (!dbg)
262                 exit (1);
263
264         ccdbg_add_debug(CC_DEBUG_FLASH);
265
266         ccdbg_debug_mode(dbg);
267         ccdbg_halt(dbg);
268         if (image->address == 0xf000) {
269                 printf("Loading %d bytes to execute from RAM\n",
270                        image->length);
271                 ccdbg_write_hex_image(dbg, image, 0);
272         } else if (image->address == 0x0000) {
273                 printf("Loading %d bytes to execute from FLASH\n",
274                        image->length);
275                 ccdbg_flash_hex_image(dbg, image);
276         } else {
277                 printf("Cannot load code to 0x%04x\n",
278                        image->address);
279                 ao_hex_image_free(image);
280                 ccdbg_close(dbg);
281                 exit(1);
282         }
283         ccdbg_set_pc(dbg, image->address);
284         ccdbg_resume(dbg);
285         ccdbg_close(dbg);
286         exit (0);
287 }