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