Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.16 KB | None | 0 0
  1. #include <pspkernel.h>
  2. #include <pspsdk.h>
  3. #include <pspusb.h>
  4. #include <pspusbbus.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <pspdebug.h>
  8. #include "usb.h"
  9. #include "sha1.h"
  10.  
  11. #define printf  pspDebugScreenPrintf
  12.  
  13. #define DRIVERNAME "PS3JigUSBDriver"
  14. #define PRODUCT_ID  (0x02EB)
  15. #define DONGLE_ID (0xAAAA)
  16.  
  17. struct DeviceDescriptor device_descriptor = {
  18.     .bLength            = 0x12,
  19.     .bDescriptorType    = 0x01,
  20.     .bcdUSB             = 0x0110,
  21.     .bDeviceClass       = 0,
  22.     .bDeviceSubClass    = 0,
  23.     .bDeviceProtocol    = 0,
  24.     .bMaxPacketSize     = 0x08,
  25.     .idVendor           = 0x054C,
  26.     .idProduct          = PRODUCT_ID,
  27.     .bcdDevice          = 0x0000,
  28.     .iManufacturer      = 0x00,
  29.     .iProduct           = 0x00,
  30.     .iSerialNumber      = 0,
  31.     .bNumConfigurations = 0x01
  32. };
  33.  
  34. struct ConfigDescriptor config_descriptor = {
  35.     .bLength             = 0x09,
  36.     .bDescriptorType     = 0x02,
  37.     .wTotalLength        = 9+9+7+7,
  38.     .bNumInterfaces      = 0x01,
  39.     .bConfigurationValue = 0x01,
  40.     .iConfiguration      = 0x00,
  41.     .bmAttributes        = 0x80,
  42.     .bMaxPower           = 0x01
  43. };
  44.  
  45. struct InterfaceDescriptor interface_descriptor = {
  46.     .bLength            = 0x09,
  47.     .bDescriptorType    = 0x04,
  48.     .bInterfaceNumber   = 0x00,
  49.     .bAlternateSetting  = 0x00,
  50.     .bNumEndpoints      = 0x02,
  51.     .bInterfaceClass    = 0xFF,
  52.     .bInterfaceSubClass = 0x00,
  53.     .bInterfaceProtocol = 0x00,
  54.     .iInterface         = 0x00
  55. };
  56.  
  57. struct EndpointDescriptor endpoint1_descriptor =
  58.     {
  59.         .bLength          = 0x07,
  60.         .bDescriptorType  = 0x05,
  61.         .bEndpointAddress = 0x02,
  62.         .bmAttributes     = 0x02,
  63.         .wMaxPacketSize   = 0x0008,
  64.         .bInterval        = 0
  65.     };
  66. struct EndpointDescriptor endpoint2_descriptor =
  67.     {
  68.         .bLength          = 0x07,
  69.         .bDescriptorType  = 0x05,
  70.         .bEndpointAddress = 0x81,
  71.         .bmAttributes     = 0x02,
  72.         .wMaxPacketSize   = 0x0008,
  73.         .bInterval        = 0
  74.     };
  75.  
  76. static struct UsbEndpoint USB_Endpoints[3] = { {0,0,0}, {1,0,0}, {2,0,0} };
  77. static struct UsbInterface USB_Interface = { 0xFFFFFFFF, 0, 1 };
  78. static struct UsbData USB_Data[2];
  79. static struct UsbdDeviceReq request;
  80.  
  81. //Maybe don't need to be global...
  82. unsigned char challengeData[64];
  83. unsigned char responseData[64];
  84.  
  85. static int USB_ControlRequest (int arg1, int arg2, struct DeviceRequest *req)
  86. {
  87.     //I don't think I care anymore...
  88.     /*printf("bmReqType %d, bRequest %d, wValue %x, wIndex %x, wLength %x\n",
  89.             req->bmRequestType, req->bRequest, req->wValue, req->wIndex,
  90.             req->wLength);*/
  91.  
  92.     return 0;
  93. }
  94.  
  95. static void response_sent (void)
  96. {
  97.     printf("Sent response: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X...\n",
  98.             responseData[0], responseData[1], responseData[2], responseData[3], responseData[4],
  99.             responseData[5], responseData[6], responseData[7], responseData[8], responseData[9]);
  100.  
  101.     printf("Done.\n");
  102. }
  103.  
  104. static void challenge_received (void)
  105. {
  106.     printf("Challenge received: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X...\n",
  107.             challengeData[0], challengeData[1], challengeData[2], challengeData[3], challengeData[4],
  108.             challengeData[5], challengeData[6], challengeData[7], challengeData[8], challengeData[9]);
  109.    
  110.     //Calculate jig key for this dongle ID
  111.     printf("Calculating response...\n");
  112.     unsigned char masterKey[] = {0x46, 0xDC, 0xEA, 0xD3, 0x17, 0xFE, 0x45, 0xD8, 0x09, 0x23,
  113.                                 0xEB, 0x97, 0xE4, 0x95, 0x64, 0x10, 0xD4, 0xCD, 0xB2, 0xC2};
  114.     unsigned char dongleID[2] = {(DONGLE_ID >> 8) & 0xFF, DONGLE_ID & 0xFF};
  115.     unsigned char result[20];
  116.     HMACInit(masterKey);
  117.     HMACAddBytes(dongleID, 2);
  118.     HMACDone(result);
  119.  
  120.     //Calculate the response data
  121.     HMACInit(result);
  122.     HMACAddBytes(challengeData+7, 20);
  123.     HMACDone(result);
  124.    
  125.     //Generate the response
  126.     memset(responseData, 0, 64);
  127.     responseData[0] = 0x00;
  128.     responseData[1] = 0x00;
  129.     responseData[2] = 0xFF;
  130.     responseData[3] = 0x00;
  131.     responseData[4] = 0x2E;
  132.     responseData[5] = 0x02;
  133.     responseData[6] = 0x02;
  134.     responseData[7] = dongleID[0];
  135.     responseData[8] = dongleID[1];
  136.     memcpy(responseData+9, result, 20);
  137.    
  138.     int i;
  139.     for (i = 0; i < 0xFFFFF; i++); //waste a little time...because sending back the response is strange sometimes...
  140.  
  141.     printf("Sending response: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X...\n",
  142.             responseData[0], responseData[1], responseData[2], responseData[3], responseData[4],
  143.             responseData[5], responseData[6], responseData[7], responseData[8], responseData[9]);
  144.  
  145.     //Send back the 64-byte response
  146.     request.endp = &USB_Endpoints[1];
  147.     request.data = responseData;
  148.     request.size = 64;
  149.     request.func = &response_sent;
  150.     request.recvsize = 64;
  151.     request.link = NULL;
  152.     sceUsbbdReqSend(&request);
  153. }
  154.  
  155. static int USB_InterfaceChanged (int interfaceNumber, int alternateSetting, int unk)
  156. {
  157.     printf("Requesting jig challenge...\n");
  158.  
  159.     //Request 64 bytes of bulk data
  160.     request.endp = &USB_Endpoints[2];
  161.     request.data = challengeData;
  162.     request.size = 64;
  163.     request.func = &challenge_received;
  164.     request.recvsize = 64;
  165.     request.link = NULL;
  166.     sceUsbbdReqRecv(&request);
  167.  
  168.     return 0;
  169. }
  170.  
  171. static int USB_Attach (int speed, void *arg2, void *arg3)
  172. {
  173.     printf("USB attached.\n");
  174.     return 0;
  175. }
  176.  
  177. static int USB_Detach (int arg1, int arg2, int arg3)
  178. {
  179.     printf("USB detached.\n");
  180.     return 0;
  181. }
  182.  
  183. static int USB_Started (int size, void *p)
  184. {
  185.     //Reset USB_Data structure
  186.     memset( USB_Data, 0, sizeof(USB_Data) );
  187.  
  188.     //Set up the high-speed information
  189.     memcpy( USB_Data[0].devdesc, &device_descriptor, sizeof(device_descriptor) );
  190.     USB_Data[0].config.pconfdesc = &USB_Data[0].confdesc;
  191.     USB_Data[0].config.pinterfaces = &USB_Data[0].interfaces;
  192.     USB_Data[0].config.pinterdesc = &USB_Data[0].interdesc;
  193.     USB_Data[0].config.pendp = &USB_Data[0].endp[0];
  194.     memcpy( USB_Data[0].confdesc.desc, &config_descriptor,  sizeof(config_descriptor) );
  195.     USB_Data[0].confdesc.pinterfaces = &USB_Data[0].interfaces;
  196.     USB_Data[0].interfaces.pinterdesc[0] = USB_Data[0].interdesc.desc;
  197.     USB_Data[0].interfaces.intcount = 1;
  198.     memcpy( USB_Data[0].interdesc.desc, &interface_descriptor, sizeof(interface_descriptor) );
  199.     USB_Data[0].interdesc.pendp = &USB_Data[0].endp[0];
  200.     memcpy( USB_Data[0].endp[0].desc, &endpoint1_descriptor, sizeof(endpoint1_descriptor) );
  201.     memcpy( USB_Data[0].endp[1].desc, &endpoint2_descriptor, sizeof(endpoint2_descriptor) );
  202.  
  203.     //SEt up the full-speed information
  204.     memcpy( USB_Data[1].devdesc, &device_descriptor, sizeof(device_descriptor) );
  205.     USB_Data[1].config.pconfdesc = &USB_Data[1].confdesc;
  206.     USB_Data[1].config.pinterfaces = &USB_Data[1].interfaces;
  207.     USB_Data[1].config.pinterdesc = &USB_Data[1].interdesc;
  208.     USB_Data[1].config.pendp = &USB_Data[1].endp[0];
  209.     memcpy( USB_Data[1].confdesc.desc, &config_descriptor,  sizeof(config_descriptor) );
  210.     USB_Data[1].confdesc.pinterfaces = &USB_Data[1].interfaces;
  211.     USB_Data[1].interfaces.pinterdesc[0] = USB_Data[1].interdesc.desc;
  212.     USB_Data[1].interfaces.intcount = 1;
  213.     memcpy( USB_Data[1].interdesc.desc, &interface_descriptor, sizeof(interface_descriptor) );
  214.     USB_Data[1].interdesc.pendp = &USB_Data[1].endp[0];
  215.     memcpy( USB_Data[1].endp[0].desc, &endpoint1_descriptor, sizeof(endpoint1_descriptor) );
  216.     memcpy( USB_Data[1].endp[1].desc, &endpoint2_descriptor, sizeof(endpoint2_descriptor) );
  217.  
  218.     printf("USB started.\n");
  219.     return 0;
  220. }
  221.  
  222. static int USB_Stopped (int size, void *p)
  223. {
  224.     printf("USB stopped.\n");
  225.     return 0;
  226. }
  227.  
  228. struct UsbDriver UsbDriver = {
  229.     DRIVERNAME,
  230.     3, //number of endpoints including control endpoint
  231.     USB_Endpoints,
  232.     &USB_Interface,
  233.     &USB_Data[0].devdesc[0],
  234.     &USB_Data[0].config,
  235.     &USB_Data[1].devdesc[0],
  236.     &USB_Data[1].config,
  237.     NULL,
  238.     USB_ControlRequest,
  239.     USB_InterfaceChanged,
  240.     USB_Attach,
  241.     USB_Detach,
  242.     0,
  243.     USB_Started,
  244.     USB_Stopped,
  245.     NULL
  246. };
  247.  
  248. void USB_Register (void)
  249. {
  250.     sceUsbbdRegister(&UsbDriver);
  251. }
  252.  
  253. void USB_Unregister (void)
  254. {
  255.     sceUsbbdUnregister(&UsbDriver);
  256. }
  257.  
  258. int USB_Start (void)
  259. {
  260.     if (sceUsbStart( PSP_USBBUS_DRIVERNAME, 0, 0 ) != 0)
  261.         return -1;
  262.  
  263.     if (sceUsbStart( DRIVERNAME, 0, 0 ) != 0)
  264.         return -1;
  265.  
  266.     if (sceUsbActivate( PRODUCT_ID ) != 0)
  267.         return -1;
  268.  
  269.     return 0;
  270. }
  271.  
  272. int USB_Stop (void)
  273. {
  274.     if (sceUsbDeactivate( PRODUCT_ID ) != 0)
  275.         return -1;
  276.  
  277.     if (sceUsbStop( DRIVERNAME, 0, 0 ) != 0)
  278.         return -1;
  279.  
  280.     if (sceUsbStop( PSP_USBBUS_DRIVERNAME, 0, 0 ) != 0)
  281.         return -1;
  282.  
  283.     return 0;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement