Remove unused cctools code paths for old libusb and cp2103 ioctls.
[fw/altos] / cctools / aoload / aoload.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 "ccdbg.h"
23
24 #define AO_USB_DESC_STRING              3
25
26 void
27 usage(char *program)
28 {
29         fprintf(stderr, "usage: %s <filename.ihx> <serial>\n", program);
30         exit(1);
31 }
32
33 struct sym {
34         unsigned        addr;
35         char            *name;
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)
41 };
42
43 #define NUM_SERIAL_SYMBOLS      (sizeof(serial_symbols)/sizeof(serial_symbols[0]))
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     found = 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_SERIAL_SYMBOLS; s++)
72                         if (!strcmp(serial_symbols[s].name, name)) {
73                                 serial_symbols[s].addr = (unsigned) a;
74                                 ++found;
75                                 break;
76                         }
77         }
78         return found == NUM_SERIAL_SYMBOLS;
79 }
80
81 static int
82 rewrite(struct hex_image *image, unsigned addr, char *data, int len)
83 {
84         int i;
85         if (addr < image->address || image->address + image->length < addr + len)
86                 return 0;
87         printf("rewrite %04x:", addr);
88         for (i = 0; i < len; i++)
89                 printf (" %02x", image->data[addr - image->address + i]);
90         printf(" ->");
91         for (i = 0; i < len; i++)
92                 printf (" %02x", data[i]);
93         printf("\n");
94         memcpy(image->data + addr - image->address, data, len);
95 }
96
97 int
98 main (int argc, char **argv)
99 {
100         struct ccdbg    *dbg;
101         uint8_t         status;
102         uint16_t        pc;
103         struct hex_file *hex;
104         struct hex_image *image;
105         char *filename;
106         FILE *file;
107         FILE *map;
108         char *serial_string;
109         unsigned int serial;
110         char *mapname, *dot;
111         char            *serial_ucs2;
112         int             serial_ucs2_len;
113         char            serial_int[2];
114         unsigned int    s;
115         int             i;
116         unsigned        usb_descriptors;
117         int             string_num;
118
119         filename = argv[1];
120         if (filename == NULL)
121                 usage(argv[0]);
122         mapname = strdup(filename);
123         dot = strrchr(mapname, '.');
124         if (!dot || strcmp(dot, ".ihx") != 0)
125                 usage(argv[0]);
126         strcpy(dot, ".map");
127
128         serial_string = argv[2];
129         if (serial_string == NULL)
130                 usage(argv[0]);
131
132         file = fopen(filename, "r");
133         if (!file) {
134                 perror(filename);
135                 exit(1);
136         }
137         map = fopen(mapname, "r");
138         if (!map) {
139                 perror(mapname);
140                 exit(1);
141         }
142         if (!find_symbols(map)) {
143                 fprintf(stderr, "Cannot find symbols in \"%s\"\n", mapname);
144                 exit(1);
145         }
146         fclose(map);
147
148         hex = ccdbg_hex_file_read(file, filename);
149         fclose(file);
150         if (!hex) {
151                 perror(filename);
152                 exit (1);
153         }
154         image = ccdbg_hex_image_create(hex);
155         if (!image) {
156                 fprintf(stderr, "image create failed\n");
157                 exit (1);
158         }
159         ccdbg_hex_file_free(hex);
160
161         serial = strtoul(serial_string, NULL, 0);
162         if (!serial)
163                 usage(argv[0]);
164
165         serial_int[0] = serial & 0xff;
166         serial_int[1] = (serial >> 8) & 0xff;
167
168         if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
169                 fprintf(stderr, "Cannot rewrite serial integer at %04x\n",
170                         AO_SERIAL_NUMBER);
171                 exit(1);
172         }
173
174         usb_descriptors = AO_USB_DESCRIPTORS - image->address;
175         string_num = 0;
176         while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
177                 if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
178                         ++string_num;
179                         if (string_num == 4)
180                                 break;
181                 }
182                 usb_descriptors += image->data[usb_descriptors];
183         }
184         if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
185                 fprintf(stderr, "Cannot rewrite serial string at %04x\n", AO_USB_DESCRIPTORS);
186                 exit(1);
187         }
188
189         serial_ucs2_len = image->data[usb_descriptors] - 2;
190         serial_ucs2 = malloc(serial_ucs2_len);
191         if (!serial_ucs2) {
192                 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
193                 exit(1);
194         }
195         s = serial;
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';
199                 s /= 10;
200         }
201         if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len))
202                 usage(argv[0]);
203
204         dbg = ccdbg_open();
205         if (!dbg)
206                 exit (1);
207
208         ccdbg_add_debug(CC_DEBUG_FLASH);
209
210         ccdbg_debug_mode(dbg);
211         ccdbg_halt(dbg);
212         if (image->address == 0xf000) {
213                 printf("Loading %d bytes to execute from RAM\n",
214                        image->length);
215                 ccdbg_write_hex_image(dbg, image, 0);
216         } else if (image->address == 0x0000) {
217                 printf("Loading %d bytes to execute from FLASH\n",
218                        image->length);
219                 ccdbg_flash_hex_image(dbg, image);
220         } else {
221                 printf("Cannot load code to 0x%04x\n",
222                        image->address);
223                 ccdbg_hex_image_free(image);
224                 ccdbg_close(dbg);
225                 exit(1);
226         }
227         ccdbg_set_pc(dbg, image->address);
228         ccdbg_resume(dbg);
229         ccdbg_close(dbg);
230         exit (0);
231 }