Add --device/-D support to the command line tools and manuals
[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 struct sym {
30         unsigned        addr;
31         char            *name;
32 } serial_symbols[] = {
33         { 0,    "_ao_serial_number" },
34 #define AO_SERIAL_NUMBER        (serial_symbols[0].addr)
35         { 0,    "_ao_usb_descriptors" },
36 #define AO_USB_DESCRIPTORS      (serial_symbols[1].addr)
37 };
38
39 #define NUM_SERIAL_SYMBOLS      (sizeof(serial_symbols)/sizeof(serial_symbols[0]))
40
41 static int
42 find_symbols(FILE *map)
43 {
44         char    line[2048];
45         char    *addr, *addr_end;
46         char    *name;
47         char    *save;
48         char    *colon;
49         unsigned long   a;
50         int     s;
51         int     found = 0;
52
53         while (fgets(line, sizeof(line), map) != NULL) {
54                 line[sizeof(line)-1] = '\0';
55                 addr = strtok_r(line, " \t\n", &save);
56                 if (!addr)
57                         continue;
58                 name = strtok_r(NULL, " \t\n", &save);
59                 if (!name)
60                         continue;
61                 colon = strchr (addr, ':');
62                 if (!colon)
63                         continue;
64                 a = strtoul(colon+1, &addr_end, 16);
65                 if (a == ULONG_MAX || addr_end == addr)
66                         continue;
67                 for (s = 0; s < NUM_SERIAL_SYMBOLS; s++)
68                         if (!strcmp(serial_symbols[s].name, name)) {
69                                 serial_symbols[s].addr = (unsigned) a;
70                                 ++found;
71                                 break;
72                         }
73         }
74         return found == NUM_SERIAL_SYMBOLS;
75 }
76
77 static int
78 rewrite(struct hex_image *image, unsigned addr, char *data, int len)
79 {
80         int i;
81         if (addr < image->address || image->address + image->length < addr + len)
82                 return 0;
83         printf("rewrite %04x:", addr);
84         for (i = 0; i < len; i++)
85                 printf (" %02x", image->data[addr - image->address + i]);
86         printf(" ->");
87         for (i = 0; i < len; i++)
88                 printf (" %02x", data[i]);
89         printf("\n");
90         memcpy(image->data + addr - image->address, data, len);
91 }
92
93 static const struct option options[] = {
94         { .name = "tty", .has_arg = 1, .val = 'T' },
95         { .name = "device", .has_arg = 1, .val = 'D' },
96         { 0, 0, 0, 0},
97 };
98
99 static void usage(char *program)
100 {
101         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] file.ihx serial-number\n", program);
102         exit(1);
103 }
104
105 int
106 main (int argc, char **argv)
107 {
108         struct ccdbg    *dbg;
109         uint8_t         status;
110         uint16_t        pc;
111         struct hex_file *hex;
112         struct hex_image *image;
113         char            *filename;
114         FILE            *file;
115         FILE            *map;
116         char            *serial_string;
117         unsigned int    serial;
118         char            *mapname, *dot;
119         char            *serial_ucs2;
120         int             serial_ucs2_len;
121         char            serial_int[2];
122         unsigned int    s;
123         int             i;
124         unsigned        usb_descriptors;
125         int             string_num;
126         char            *tty = NULL;
127         char            *device = NULL;
128         int             c;
129
130         while ((c = getopt_long(argc, argv, "T:D:", options, NULL)) != -1) {
131                 switch (c) {
132                 case 'T':
133                         tty = optarg;
134                         break;
135                 case 'D':
136                         device = optarg;
137                         break;
138                 default:
139                         usage(argv[0]);
140                         break;
141                 }
142         }
143         filename = argv[optind];
144         if (filename == NULL)
145                 usage(argv[0]);
146         mapname = strdup(filename);
147         dot = strrchr(mapname, '.');
148         if (!dot || strcmp(dot, ".ihx") != 0)
149                 usage(argv[0]);
150         strcpy(dot, ".map");
151
152         serial_string = argv[optind + 1];
153         if (serial_string == NULL)
154                 usage(argv[0]);
155
156         file = fopen(filename, "r");
157         if (!file) {
158                 perror(filename);
159                 exit(1);
160         }
161         map = fopen(mapname, "r");
162         if (!map) {
163                 perror(mapname);
164                 exit(1);
165         }
166         if (!find_symbols(map)) {
167                 fprintf(stderr, "Cannot find symbols in \"%s\"\n", mapname);
168                 exit(1);
169         }
170         fclose(map);
171
172         hex = ccdbg_hex_file_read(file, filename);
173         fclose(file);
174         if (!hex) {
175                 perror(filename);
176                 exit (1);
177         }
178         image = ccdbg_hex_image_create(hex);
179         if (!image) {
180                 fprintf(stderr, "image create failed\n");
181                 exit (1);
182         }
183         ccdbg_hex_file_free(hex);
184
185         serial = strtoul(serial_string, NULL, 0);
186         if (!serial)
187 (argv[0]);
188
189         serial_int[0] = serial & 0xff;
190         serial_int[1] = (serial >> 8) & 0xff;
191
192         if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
193                 fprintf(stderr, "Cannot rewrite serial integer at %04x\n",
194                         AO_SERIAL_NUMBER);
195                 exit(1);
196         }
197
198         usb_descriptors = AO_USB_DESCRIPTORS - image->address;
199         string_num = 0;
200         while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
201                 if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
202                         ++string_num;
203                         if (string_num == 4)
204                                 break;
205                 }
206                 usb_descriptors += image->data[usb_descriptors];
207         }
208         if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
209                 fprintf(stderr, "Cannot rewrite serial string at %04x\n", AO_USB_DESCRIPTORS);
210                 exit(1);
211         }
212
213         serial_ucs2_len = image->data[usb_descriptors] - 2;
214         serial_ucs2 = malloc(serial_ucs2_len);
215         if (!serial_ucs2) {
216                 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
217                 exit(1);
218         }
219         s = serial;
220         for (i = serial_ucs2_len / 2; i; i--) {
221                 serial_ucs2[i * 2 - 1] = 0;
222                 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
223                 s /= 10;
224         }
225         if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len))
226                 usage(argv[0]);
227
228         if (!tty)
229                 tty = cc_usbdevs_find_by_arg(device, "TIDongle");
230         dbg = ccdbg_open(tty);
231         if (!dbg)
232                 exit (1);
233
234         ccdbg_add_debug(CC_DEBUG_FLASH);
235
236         ccdbg_debug_mode(dbg);
237         ccdbg_halt(dbg);
238         if (image->address == 0xf000) {
239                 printf("Loading %d bytes to execute from RAM\n",
240                        image->length);
241                 ccdbg_write_hex_image(dbg, image, 0);
242         } else if (image->address == 0x0000) {
243                 printf("Loading %d bytes to execute from FLASH\n",
244                        image->length);
245                 ccdbg_flash_hex_image(dbg, image);
246         } else {
247                 printf("Cannot load code to 0x%04x\n",
248                        image->address);
249                 ccdbg_hex_image_free(image);
250                 ccdbg_close(dbg);
251                 exit(1);
252         }
253         ccdbg_set_pc(dbg, image->address);
254         ccdbg_resume(dbg);
255         ccdbg_close(dbg);
256         exit (0);
257 }