C   7

usb-storace.c

Guest on 14th May 2021 12:46:46 AM

  1. /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
  2. /* Latest version of the script should be in http://iki.fi/lindi/usbsnoop2libusb.pl */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #include <usb.h>
  10. #if 0
  11.  #include <linux/usbdevice_fs.h>
  12.  #define LIBUSB_AUGMENT
  13.  #include "libusb_augment.h"
  14. #endif
  15.  
  16. struct usb_dev_handle *devh;
  17.  
  18. void release_usb_device(int dummy) {
  19.     int ret;
  20.     ret = usb_release_interface(devh, 0);
  21.     if (!ret)
  22.     printf("failed to release interface: %d\n", ret);
  23.     usb_close(devh);
  24.     if (!ret)
  25.     printf("failed to close interface: %d\n", ret);
  26.     exit(1);
  27. }
  28.  
  29. void list_devices() {
  30.     struct usb_bus *bus;
  31.     for (bus = usb_get_busses(); bus; bus = bus->next) {
  32.     struct usb_device *dev;
  33.    
  34.     for (dev = bus->devices; dev; dev = dev->next)
  35.         printf("0x%04x 0x%04x\n",
  36.            dev->descriptor.idVendor,
  37.            dev->descriptor.idProduct);
  38.     }
  39. }    
  40.  
  41. struct usb_device *find_device(int vendor, int product) {
  42.     struct usb_bus *bus;
  43.    
  44.     for (bus = usb_get_busses(); bus; bus = bus->next) {
  45.     struct usb_device *dev;
  46.    
  47.     for (dev = bus->devices; dev; dev = dev->next) {
  48.         if (dev->descriptor.idVendor == vendor
  49.         && dev->descriptor.idProduct == product)
  50.         return dev;
  51.     }
  52.     }
  53.     return NULL;
  54. }
  55.  
  56. void print_bytes(char *bytes, int len) {
  57.     int i;
  58.     if (len > 0) {
  59.     for (i=0; i<len; i++) {
  60.         printf("%02x ", (int)((unsigned char)bytes[i]));
  61.     }
  62.     printf("\"");
  63.         for (i=0; i<len; i++) {
  64.         printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
  65.         }
  66.         printf("\"");
  67.     }
  68. }
  69.  
  70.  
  71. int main(int argc, char **argv) {
  72.     int ret, vendor, product;
  73.     struct usb_device *dev;
  74.     char buf[65535], *endptr;
  75. #if 0
  76.     usb_urb *isourb;
  77.     struct timeval isotv;
  78.     char isobuf[32768];
  79. #endif
  80.  
  81.     usb_init();
  82.     usb_set_debug(255);
  83.     usb_find_busses();
  84.     usb_find_devices();
  85.  
  86.     if (argc!=6) {
  87.     printf("usage: %s vendorID productID firstSector numberOfSectors outputFile\n", argv[0]);
  88.     printf("ID numbers of currently attached devices:\n");
  89.     list_devices();
  90.     exit(1);
  91.     }
  92.     vendor = strtol(argv[1], &endptr, 16);
  93.     if (*endptr != '\0') {
  94.     printf("invalid vendor id\\n");
  95.     exit(1);
  96.     }
  97.     product = strtol(argv[2], &endptr, 16);
  98.     if (*endptr != '\0') {
  99.     printf("invalid product id\\n");
  100.     exit(1);
  101.     }
  102.     dev = find_device(vendor, product);
  103.     assert(dev);
  104.  
  105.     devh = usb_open(dev);
  106.     assert(devh);
  107.    
  108.     signal(SIGTERM, release_usb_device);
  109.  
  110.     ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
  111.     printf("usb_get_driver_np returned %d\n", ret);
  112.     if (ret == 0) {
  113.     printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
  114.     ret = usb_detach_kernel_driver_np(devh, 0);
  115.     printf("usb_detach_kernel_driver_np returned %d\n", ret);
  116.     }
  117.     ret = usb_claim_interface(devh, 0);
  118.     if (ret != 0) {
  119.     printf("claim failed with error %d\n", ret);
  120.         exit(1);
  121.     }
  122.    
  123.     ret = usb_set_altinterface(devh, 0);
  124.     assert(ret >= 0);
  125.  
  126. ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
  127. printf("1 get descriptor returned %d, bytes: ", ret);
  128. print_bytes(buf, ret);
  129. printf("\n");
  130. ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
  131. printf("2 get descriptor returned %d, bytes: ", ret);
  132. print_bytes(buf, ret);
  133. printf("\n");
  134. ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
  135. printf("3 get descriptor returned %d, bytes: ", ret);
  136. print_bytes(buf, ret);
  137. printf("\n");
  138. usleep(1*1000);
  139. ret = usb_get_descriptor(devh, 0x0000003, 0x0000000, buf, 0x0000002);
  140. printf("4 get descriptor returned %d, bytes: ", ret);
  141. print_bytes(buf, ret);
  142. printf("\n");
  143. usleep(1*1000);
  144. ret = usb_get_descriptor(devh, 0x0000003, 0x0000000, buf, 0x0000004);
  145. printf("5 get descriptor returned %d, bytes: ", ret);
  146. print_bytes(buf, ret);
  147. printf("\n");
  148. ret = usb_get_descriptor(devh, 0x0000003, 0x0000003, buf, 0x0000002);
  149. printf("6 get descriptor returned %d, bytes: ", ret);
  150. print_bytes(buf, ret);
  151. printf("\n");
  152. usleep(1*1000);
  153. ret = usb_get_descriptor(devh, 0x0000003, 0x0000003, buf, 0x0000022);
  154. printf("7 get descriptor returned %d, bytes: ", ret);
  155. print_bytes(buf, ret);
  156. printf("\n");
  157. ret = usb_release_interface(devh, 0);
  158. if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
  159. ret = usb_set_configuration(devh, 0x0000001);
  160. printf("8 set configuration returned %d\n", ret);
  161. ret = usb_claim_interface(devh, 0);
  162. if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
  163. ret = usb_set_altinterface(devh, 0);
  164. printf("8 set alternate setting returned %d\n", ret);
  165. usleep(98*1000);
  166. printf("9 select interface not implemented yet :(\n");
  167. usleep(94*1000);
  168. /* GET MAX LUN */
  169. ret = usb_control_msg(devh, USB_TYPE_CLASS + USB_RECIP_INTERFACE + USB_ENDPOINT_IN, 0x00000fe, 0x0000000, 0x0000000, buf, 0x0000001, 1000);
  170. printf("10 control msg returned %d, bytes: ", ret);
  171. print_bytes(buf, ret);
  172. printf("\n");
  173.  
  174. if (1) {
  175.     int sector, nr_sectors, tmp, i;
  176.     FILE *fp;
  177.  sector = atoi(argv[3]);
  178.  nr_sectors = atoi(argv[4]);
  179.  fp = fopen(argv[5], "w");
  180.  for (i=0; i<nr_sectors; i++) {
  181.      printf("reading sector %d\n", sector+i);
  182. /*memcpy        | Signature     | Tag           | TransferLength| F |LUN|len|                     CB                                       */
  183. /*memcpy                                                                   6| op|lun| addr  |len|ctr|                                      */
  184. /*memcpy                                                                   a| op|lun| addr          | R |  len  |ctr|                      */
  185.  /*              0   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5   6   7              */
  186.  memcpy(buf, "\x55\x53\x42\x43\x08\xd0\x3e\x85\x00\x02\x00\x00\x80\x00\x0a\x28\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", 0x000001f);
  187.  tmp = htonl(sector+i);
  188.  memcpy(buf+17, &tmp, 4);
  189.  ret = usb_bulk_write(devh, 0x00000002, buf, 0x000001f, 1000);
  190. #if 0
  191.  printf("A1 bulk write returned %d, bytes: ", ret);
  192.  print_bytes(buf, ret);
  193.  printf("\n");
  194. #endif
  195. ret = usb_bulk_read(devh, 0x00000081, buf, 0x0000200, 1000);
  196.  if (ret > 0) {
  197.      fwrite(buf, ret, 1, fp);
  198.      fflush(fp);
  199.  } else {
  200.      printf("reading sector %d failed with %d\n", sector+i, ret);
  201.      break;
  202.  }
  203. #if 0
  204. printf("A2 bulk read returned %d, bytes: ", ret);
  205. print_bytes(buf, ret);
  206. printf("\n");
  207. #endif
  208. usleep(1*1000);
  209. ret = usb_bulk_read(devh, 0x00000081, buf, 0x0000200, 1030);
  210. #if 0
  211. printf("A3 bulk read returned %d, bytes: ", ret);
  212. print_bytes(buf, ret);
  213. printf("\n");
  214. #endif
  215.  }
  216.  fclose(fp);
  217.     ret = usb_release_interface(devh, 0);
  218.     assert(ret == 0);
  219.     ret = usb_close(devh);
  220.     assert(ret == 0);
  221.     return 0;
  222. }
  223. }

Raw Paste


Login or Register to edit or fork this paste. It's free.