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