ao-chaosread: Add support for the new flash endpoint in chaoskey
[fw/altos] / ao-tools / ao-chaosread / ao-chaosread.c
index 806c2ef9e57f3b1018f886d90ecf0e48964f9671..5c0de1257fbded71cda28e0d7dbaaf2fcffaba7c 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -73,7 +74,7 @@ chaoskey_match(libusb_device *dev, char *match_serial)
                goto out;
        }
 
-       ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, device_serial, match_len + 1);
+       ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *) device_serial, match_len + 1);
 
        if (ret < 0) {
                fprintf(stderr, "failed to get serial number: %s\n", libusb_strerror(ret));
@@ -90,7 +91,6 @@ chaoskey_match(libusb_device *dev, char *match_serial)
        return handle;
 
 out:
-       free(device_serial);
        if (handle)
                libusb_close(handle);
        return 0;
@@ -103,7 +103,6 @@ chaoskey_open(char *serial)
        int             ret;
        ssize_t         num;
        libusb_device   **list;
-       libusb_device   *device = NULL;
        int             d;
 
        ck = calloc(sizeof (struct chaoskey), 1);
@@ -173,24 +172,21 @@ chaoskey_close(struct chaoskey *ck)
        free(ck);
 }
 
-void
-chaoskey_transfer_callback(struct libusb_transfer *transfer)
-{
-       struct chaoskey *ck = transfer->user_data;
-}
-
-#define ENDPOINT       0x86
+#define COOKED_ENDPOINT        0x85
+#define RAW_ENDPOINT   0x86
+#define FLASH_ENDPOINT 0x87
 
 int
-chaoskey_read(struct chaoskey *ck, uint8_t *buffer, int len)
+chaoskey_read(struct chaoskey *ck, int endpoint, void *buffer, int len)
 {
+       uint8_t *buf = buffer;
        int     total = 0;
 
        while (len) {
                int     ret;
                int     transferred;
 
-               ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buffer, len, &transferred, 10000);
+               ret = libusb_bulk_transfer(ck->handle, endpoint, buf, len, &transferred, 10000);
                if (ret) {
                        if (total)
                                return total;
@@ -200,19 +196,26 @@ chaoskey_read(struct chaoskey *ck, uint8_t *buffer, int len)
                        }
                }
                len -= transferred;
-               buffer += transferred;
+               buf += transferred;
+               total += transferred;
        }
+       return total;
 }
 
 static const struct option options[] = {
        { .name = "serial", .has_arg = 1, .val = 's' },
        { .name = "length", .has_arg = 1, .val = 'l' },
+       { .name = "infinite", .has_arg = 0, .val = 'i' },
+       { .name = "bytes", .has_arg = 0, .val = 'b' },
+       { .name = "cooked", .has_arg = 0, .val = 'c' },
+       { .name = "raw", .has_arg = 0, .val = 'r' },
+       { .name = "flash", .has_arg = 0, .val = 'f' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]]\n", program);
+       fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]] [--infinite] [--bytes] [--cooked] [--raw] [--flash]\n", program);
        exit(1);
 }
 
@@ -220,16 +223,19 @@ int
 main (int argc, char **argv)
 {
        struct chaoskey *ck;
-       char    buf[1024];
+       uint16_t        buf[512];
        int     got;
        int     c;
        char    *serial = NULL;
        char    *length_string;
        char    *length_end;
        unsigned long   length = sizeof(buf);
-       int             this_time;
+       int     this_time;
+       int     infinite = 0;
+       int     bytes = 0;
+       int     endpoint = RAW_ENDPOINT;
 
-       while ((c = getopt_long(argc, argv, "s:l:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "s:l:ibcrf", options, NULL)) != -1) {
                switch (c) {
                case 's':
                        serial = optarg;
@@ -246,6 +252,21 @@ main (int argc, char **argv)
                        else if (strlen(length_end))
                                 usage(argv[0]);
                        break;
+               case 'i':
+                       infinite = 1;
+                       break;
+               case 'b':
+                       bytes = 1;
+                       break;
+               case 'c':
+                       endpoint = COOKED_ENDPOINT;
+                       break;
+               case 'r':
+                       endpoint = RAW_ENDPOINT;
+                       break;
+               case 'f':
+                       endpoint = FLASH_ENDPOINT;
+                       break;
                default:
                        usage(argv[0]);
                        break;
@@ -256,16 +277,33 @@ main (int argc, char **argv)
        if (!ck)
                exit(1);
 
-       while (length) {
+       if (bytes)
+               length *= 2;
+
+       while (length || infinite) {
                this_time = sizeof(buf);
-               if (length < sizeof(buf))
+               if (!infinite && length < sizeof(buf))
                        this_time = (int) length;
-               got = chaoskey_read(ck, buf, this_time);
+               got = chaoskey_read(ck, endpoint, buf, this_time);
                if (got < 0) {
                        perror("read");
                        exit(1);
                }
-               write(1, buf, got);
+               if (bytes) {
+                       int i;
+                       for (i = 0; i < got / 2; i++)
+                               putchar((buf[i] >> 1 & 0xff));
+               } else {
+                       int i;
+                       int ret;
+                       for (i = 0; i < got; i += ret) {
+                               ret = write(1, ((char *) buf) + i, got - i);
+                               if (ret <= 0) {
+                                       perror("write");
+                                       exit(1);
+                               }
+                       }
+               }
                length -= got;
        }
        exit(0);