2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.
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.
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.
33 #include "ao-verbose.h"
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' },
43 static void usage(char *program)
45 fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] <expected-size>\n", program);
50 done(struct cc_usb *cc, int code)
57 ends_with(char *whole, char *suffix)
59 int whole_len = strlen(whole);
60 int suffix_len = strlen(suffix);
62 if (suffix_len > whole_len)
64 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
68 starts_with(char *whole, char *prefix)
70 int whole_len = strlen(whole);
71 int prefix_len = strlen(prefix);
73 if (prefix_len > whole_len)
75 return strncmp(whole, prefix, prefix_len) == 0;
80 char **strs = malloc (sizeof (char *)), *str;
83 while ((str = strtok(line, " \t"))) {
85 strs = realloc(strs, (n + 2) * sizeof (char *));
86 strs[n] = strdup(str);
94 free_strs(char **strs) {
98 for (i = 0; (str = strs[i]) != NULL; i++)
109 static struct flash *
110 flash(struct cc_usb *usb)
112 struct flash *head = NULL, **tail = &head;
113 cc_usb_printf(usb, "f\nv\n");
118 cc_usb_getline(usb, line, sizeof (line));
119 b = malloc (sizeof (struct flash));
120 strcpy(b->line, line);
125 if (strstr(line, "software-version"))
132 free_flash(struct flash *b) {
144 find_flash(struct flash *b, char *word0) {
146 for (;b; b = b->next) {
147 if (strstr(b->line, word0))
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:");
159 if (!size || !erase) {
160 printf("no response\n");
165 int actual_size = atoi(size[2]);
167 if (actual_size != expected_size) {
168 printf ("weird flash size %d != %d\n", actual_size, expected_size);
173 int actual_erase = atoi(erase[3]);
175 if (actual_erase != 65536) {
176 printf ("weird erase size %d\n", actual_erase);
181 printf ("flash size %d erase block %d\n", actual_size, actual_erase);
187 main (int argc, char **argv)
196 struct cc_usb *cc = NULL;
203 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
223 ao_verbose = verbose;
226 ccdbg_add_debug(CC_DEBUG_BITBANG);
229 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
231 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
233 tty = getenv("ALTOS_TTY");
237 cc = cc_usb_open(tty);
242 if (!do_flash(cc, atoi(argv[optind])))