ungureanuvladvictor

USBBoot C

Jun 10th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  usbBoot
  4. //
  5. //  Created by Vlad Victor Ungureanu on 6/11/13.
  6. //  Copyright (c) 2013 Vlad Victor Ungureanu. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <libusb.h>
  12.  
  13. int main(int argc, const char * argv[]) {
  14.     libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
  15.     libusb_device_handle *dev_handle; //a device handle
  16.     libusb_context *ctx = NULL; //a libusb session
  17.     int r; //for return values
  18.     ssize_t cnt; //holding number of devices in list
  19.     r = libusb_init(&ctx); //initialize the library for the session we just declared
  20.     if(r < 0) {
  21.         printf("Init error!\n");
  22.         return 1;
  23.     }
  24.     libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
  25.    
  26.     cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
  27.     if(cnt < 0) {
  28.         printf("Cannot get device list!\n");
  29.         return 1;
  30.     }
  31.     printf("%zd devices in list!\n",cnt);
  32.    
  33.     dev_handle = libusb_open_device_with_vid_pid(ctx, 0x0451, 0x6141); //these are vendorID and productID I found for my usb device
  34.     if(dev_handle == NULL) {
  35.         printf("Cannot open device!\n");
  36.         return 1;
  37.     }
  38.     else
  39.         printf("Device opened!\n");
  40.     libusb_free_device_list(devs, 1); //free the list, unref the devices in it
  41.    
  42.     unsigned char *data = (unsigned char*)malloc(64*sizeof(unsigned char));
  43.    
  44.     int actual; //used to find out how many bytes were written
  45.     if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
  46.         printf("Kernel has driver!\n");
  47.         if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
  48.             printf("Kernel deattached!\n!");
  49.     }
  50.     r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
  51.     if(r < 0) {
  52.         printf("Cannot Claim Interface!\n");
  53.         return 1;
  54.     }
  55.     printf("Claimed interface!\n");
  56.    
  57.     r = libusb_bulk_transfer(dev_handle, (129 | LIBUSB_ENDPOINT_IN), data, 64, &actual, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
  58.     while(r==0) {
  59.         int i;
  60.         for(i = 0; i<63; i++)printf("%c ",data[i]);
  61.         r = libusb_bulk_transfer(dev_handle, (129 | LIBUSB_ENDPOINT_IN), data, 64, &actual, 0);
  62.     }
  63.     r = libusb_release_interface(dev_handle, 0); //release the claimed interface
  64.     if(r!=0) {
  65.         printf("Cannot release interface!\n");
  66.         return 1;
  67.     }
  68.    
  69.     libusb_close(dev_handle); //close the device we opened
  70.     libusb_exit(ctx); //needs to be called to end the
  71.    
  72.     free(data); //delete the allocated memory for data
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment