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