5cbc56d750ae6f4b6e19da8c1cf6562b3d71e6a6
[fw/altos] / ao-tools / ao-test-flash / ao-test-flash.c
1 /*
2  * Copyright © 2014 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 <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include "ao-elf.h"
31 #include "ccdbg.h"
32 #include "cc-usb.h"
33 #include "cc.h"
34 #include "ao-verbose.h"
35
36 static const struct option options[] = {
37         { .name = "tty", .has_arg = 1, .val = 'T' },
38         { .name = "device", .has_arg = 1, .val = 'D' },
39         { .name = "raw", .has_arg = 0, .val = 'r' },
40         { .name = "verbose", .has_arg = 1, .val = 'v' },
41         { 0, 0, 0, 0},
42 };
43
44 static void usage(char *program)
45 {
46         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] <expected-size>\n", program);
47         exit(1);
48 }
49
50 void
51 done(struct cc_usb *cc, int code)
52 {
53         cc_usb_close(cc);
54         exit (code);
55 }
56
57 static int
58 ends_with(char *whole, char *suffix)
59 {
60         int whole_len = strlen(whole);
61         int suffix_len = strlen(suffix);
62
63         if (suffix_len > whole_len)
64                 return 0;
65         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
66 }
67
68 static int
69 starts_with(char *whole, char *prefix)
70 {
71         int whole_len = strlen(whole);
72         int prefix_len = strlen(prefix);
73
74         if (prefix_len > whole_len)
75                 return 0;
76         return strncmp(whole, prefix, prefix_len) == 0;
77 }
78
79 static char **
80 tok(char *line) {
81         char    **strs = malloc (sizeof (char *)), *str;
82         int     n = 0;
83
84         while ((str = strtok(line, " \t"))) {
85                 line = NULL;
86                 strs = realloc(strs, (n + 2) * sizeof (char *));
87                 strs[n] = strdup(str);
88                 n++;
89         }
90         strs[n] = '\0';
91         return strs;
92 }
93
94 static void
95 free_strs(char **strs) {
96         char    *str;
97         int     i;
98
99         for (i = 0; (str = strs[i]) != NULL; i++)
100                 free(str);
101         free(strs);
102 }
103
104 struct flash {
105         struct flash    *next;
106         char            line[512];
107         char            **strs;
108 };
109
110 static struct flash *
111 flash(struct cc_usb *usb)
112 {
113         struct flash    *head = NULL, **tail = &head;
114         cc_usb_printf(usb, "f\nv\n");
115         for (;;) {
116                 char    line[512];
117                 struct flash    *b;
118
119                 cc_usb_getline(usb, line, sizeof (line));
120                 b = malloc (sizeof (struct flash));
121                 strcpy(b->line, line);
122                 b->strs = tok(line);
123                 b->next = NULL;
124                 *tail = b;
125                 tail = &b->next;
126                 if (strstr(line, "software-version"))
127                         break;
128         }
129         return head;
130 }
131
132 static void
133 free_flash(struct flash *b) {
134         struct flash *n;
135
136         while (b) {
137                 n = b->next;
138                 free_strs(b->strs);
139                 free(b);
140                 b = n;
141         }
142 }
143
144 char **
145 find_flash(struct flash *b, char *word0) {
146         int i;
147         for (;b; b = b->next) {
148                 if (strstr(b->line, word0))
149                         return b->strs;
150         }
151         return NULL;
152 }
153
154 int
155 do_flash(struct cc_usb *usb, int expected_size) {
156         struct flash *b = flash(usb);
157         char **size = find_flash(b, "Storage size:");
158         char **erase = find_flash(b, "Storage erase unit:");
159
160         if (!size || !erase) {
161                 printf("no response\n");
162                 free_flash(b);
163                 return 0;
164         }
165
166         int actual_size = atoi(size[2]);
167
168         if (actual_size != expected_size) {
169                 printf ("weird flash size %d != %d\n", actual_size, expected_size);
170                 free_flash(b);
171                 return 0;
172         }
173
174         int actual_erase = atoi(erase[3]);
175
176         if (actual_erase != 65536) {
177                 printf ("weird erase size %d\n", actual_erase);
178                 free_flash(b);
179                 return 0;
180         }
181
182         printf ("flash size %d erase block %d\n", actual_size, actual_erase);
183
184         return 1;
185 }
186
187 int
188 main (int argc, char **argv)
189 {
190         char                    *device = NULL;
191         char                    *filename;
192         Elf                     *e;
193         unsigned int            s;
194         int                     i;
195         int                     c;
196         int                     tries;
197         struct cc_usb           *cc = NULL;
198         char                    *tty = NULL;
199         int                     success;
200         int                     verbose = 0;
201         int                     ret = 0;
202         int                     expected_size;
203
204         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
205                 switch (c) {
206                 case 'T':
207                         tty = optarg;
208                         break;
209                 case 'D':
210                         device = optarg;
211                         break;
212                 case 'v':
213                         verbose++;
214                         break;
215                 default:
216                         usage(argv[0]);
217                         break;
218                 }
219         }
220
221         if (!argv[optind])
222                 usage(argv[0]);
223
224         ao_verbose = verbose;
225
226         if (verbose > 1)
227                 ccdbg_add_debug(CC_DEBUG_BITBANG);
228
229         if (!tty)
230                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
231         if (!tty)
232                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
233         if (!tty)
234                 tty = getenv("ALTOS_TTY");
235         if (!tty)
236                 tty="/dev/ttyACM0";
237
238         cc = cc_usb_open(tty);
239
240         if (!cc)
241                 exit(1);
242
243         if (!do_flash(cc, atoi(argv[optind])))
244                 ret = 1;
245         done(cc, ret);
246 }