/* modified; may be broken; see thread: http://openwall.com/lists/john-dev/2015/07/26/2 */ /*! UCEcho -- C host software for ucecho examples Copyright (C) 2009-2010 ZTEX e.K. http://www.ztex.de This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/. !*/ #include #include #include #include #define BUFSIZE 256 struct usb_device *device; usb_dev_handle *handle; char buf[BUFSIZE]; // find the first ucecho device struct usb_device *find_device () { struct usb_bus *bus_search; struct usb_device *device_search; bus_search = usb_busses; while (bus_search != NULL){ device_search = bus_search->devices; while (device_search != NULL){ if ((device_search->descriptor.idVendor == 0x221a) && (device_search->descriptor.idProduct == 0x100)) { handle = usb_open(device_search); usb_get_string_simple(handle, device_search->descriptor.iProduct, buf, BUFSIZE); if (! strncmp("intraffic", buf , 9 ) ) return device_search; usb_close(handle); } device_search = device_search->next; } bus_search = bus_search->next; } return NULL; } // main int main(int argc, char *argv[]) { unsigned int sent[64] = {0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 0}; unsigned int received[8 * 1024 / sizeof(unsigned int)] = {0}; int i; pid_t pid = getpid(); printf("\npid == %zd\n", pid); /* to make unique data for each run */ for (i = 0; i < 8; i++) { sent[i] += pid; } sent[2] += 7; usb_init(); // initializing libusb usb_find_busses(); // ... finding busses usb_find_devices(); // ... and devices device = find_device(); // find the device (hopefully the correct one) if ( device == NULL ) { // nothing found fprintf(stderr, "Cannot find ucecho device\n"); return 1; } if (usb_claim_interface(handle, 0) < 0) { fprintf(stderr, "Error claiming interface 0: %s\n", usb_strerror()); return 1; } /* int ep_out = interface.endpoint[6].bEndpointAddress; */ /* int ep_in = interface.endpoint[2].bEndpointAddress; */ /* int ep_out = 6; */ /* int ep_in = 2; */ /* ep_out is used for usb_bulk_read, ep_in is for usb_bulk_write */ int ep_out = 2; int ep_in = 6; int t; for (t = 0; t < 5; t++) { sent[0] ^= t; sent[1] ^= sent[0]; //write mode i = usb_control_msg(handle, 0x40, 0x60, 0, 0, NULL, 0, 1000); if ( i < 0 ) { fprintf(stderr, "Error sending data: %s\n", usb_strerror()); return 1; } else { printf("write mode control msg, %d\n", i); } printf("Written S[0] to FPGA...\n"); /* for(i = 0; i < 32; i++) */ /* printf("S[0][%d] = 0x%08x\n", i, sent[i]); */ // write string to ucecho device i = usb_bulk_write(handle, ep_in, (const char *)(sent), sizeof(unsigned int) * 8, 1000); if (i < 0) { fprintf(stderr, "Error sending data: %s\n", usb_strerror()); return 1; } else { printf("wrote %d bytes\n", i); } //read mode i = usb_control_msg(handle, 0x40, 0x60, 1, 0, NULL, 0, 1000); if (i < 0) { fprintf(stderr, "Error sending data: %s\n", usb_strerror()); return 1; } else { printf("read mode control msg, %d\n", i); } printf("Read S[0] from FPGA: \n"); int j; for (j = 0; j < 8; j++) { // read string back from ucecho device /* i = usb_bulk_read(handle, 2, (char *)(received), 1024, 1000); */ i = usb_bulk_read(handle, ep_out, (char *)(received), 1024, 1000); if (i < 0) { fprintf(stderr, "Error reading data: %s\n", usb_strerror()); return 1; } else { printf("got %d bytes back (read #%d)\n", i, j); } /* for(i = 0; i < 8; i++) */ /* printf("S[0][%d] = 0x%08x\n", i, received[i]); */ if (i == 34) { printf("33,34: %08x\n", received[8]); } if (i) { for(i = 0; i < 8; i++) printf("S 0 %d = 0x%08x 0x%08x %s\n", i, received[i], sent[i], received[i] == sent[i] ? "ok": "<<< failure"); } } } usb_release_interface(handle, 0); usb_close(handle); return 0; }