ao-tools: Add ao-usbtrng to dump RNG data for testing
[fw/altos] / ao-tools / ao-usbtrng / ao-usbtrng.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>] <kbytes>\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 int
57 main (int argc, char **argv)
58 {
59         char                    *device = NULL;
60         char                    *filename;
61         Elf                     *e;
62         unsigned int            s;
63         int                     i;
64         int                     c;
65         int                     tries;
66         struct cc_usb           *cc = NULL;
67         char                    *tty = NULL;
68         int                     success;
69         int                     verbose = 0;
70         int                     ret = 0;
71         int                     expected_size;
72         int                     kbytes;
73         uint8_t                 bits[1024];
74
75         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
76                 switch (c) {
77                 case 'T':
78                         tty = optarg;
79                         break;
80                 case 'D':
81                         device = optarg;
82                         break;
83                 case 'v':
84                         verbose++;
85                         break;
86                 default:
87                         usage(argv[0]);
88                         break;
89                 }
90         }
91
92         if (!argv[optind])
93                 usage(argv[0]);
94
95         kbytes = atoi(argv[optind]);
96         if (kbytes < 1)
97                 kbytes = 1;
98
99         ao_verbose = verbose;
100
101         if (verbose > 1)
102                 ccdbg_add_debug(CC_DEBUG_BITBANG);
103
104         if (!tty)
105                 tty = cc_usbdevs_find_by_arg(device, "usbtrng");
106         if (!tty)
107                 tty = getenv("ALTOS_TTY");
108         if (!tty)
109                 tty="/dev/ttyACM0";
110
111         cc = cc_usb_open(tty);
112
113         if (!cc)
114                 exit(1);
115
116         cc_usb_printf(cc, "f %d\n", kbytes);
117
118         while (kbytes--) {
119                 int     i;
120                 for (i = 0; i < 1024; i++)
121                         bits[i] = cc_usb_getchar(cc);
122                 write(1, bits, 1024);
123         }
124
125         done(cc, ret);
126 }