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