X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/2aded84aa83e242945a6304f67c79695d1409a36..1f42ccddfb11c6a862c6cb058af765acae038e4d:/common/usb_cdc.c diff --git a/common/usb_cdc.c b/common/usb_cdc.c index 97551692..5577e354 100644 --- a/common/usb_cdc.c +++ b/common/usb_cdc.c @@ -33,34 +33,33 @@ */ #include "usb_cdc.h" +#include "at91sam7s512.h" #include "config_gpio.h" -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define AT91C_EP_IN_SIZE 0x40 #define AT91C_EP_OUT 1 #define AT91C_EP_OUT_SIZE 0x40 #define AT91C_EP_IN 2 -const char devDescriptor[] = { +static const char devDescriptor[] = { /* Device descriptor */ 0x12, // bLength 0x01, // bDescriptorType - 0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10) + 0x00,0x02, // Complies with USB Spec. Release (0200h = release 2.0) 0x02, // bDeviceClass: CDC class code 0x00, // bDeviceSubclass: CDC class sub code 0x00, // bDeviceProtocol: CDC Device protocol 0x08, // bMaxPacketSize0 - 0x2d,0x2d, // Vendor ID (--) - 0x4d,0x50, // Product ID (PM), transmitted in reverse + 0xc4,0x9a, // Vendor ID (0x9ac4 = J. Westhues) + 0x8f,0x4b, // Product ID (0x4b8f = Proxmark-3 RFID Instrument) 0x01,0x00, // Device release number (0001) - 0x01, // iManufacturer // 0x01 - 0x00, // iProduct - 0x00, // SerialNumber + 0x01, // iManufacturer + 0x02, // iProduct + 0x00, // iSerialNumber 0x01 // bNumConfigs }; -const char cfgDescriptor[] = { +static const char cfgDescriptor[] = { /* ============== CONFIGURATION 1 =========== */ /* Configuration 1 descriptor */ 0x09, // CbLength @@ -71,7 +70,7 @@ const char cfgDescriptor[] = { 0x01, // CbConfigurationValue 0x00, // CiConfiguration 0xC0, // CbmAttributes 0xA0 - 0x00, // CMaxPower + 0xFA, // CMaxPower /* Communication Class Interface Descriptor Requirement */ 0x09, // bLength @@ -95,7 +94,7 @@ const char cfgDescriptor[] = { 0x04, // bFunctionLength 0x24, // bDescriptor Type: CS_INTERFACE 0x02, // bDescriptor Subtype: ACM Func Desc - 0x00, // bmCapabilities + 0x02, // bmCapabilities /* Union Functional Descriptor */ 0x05, // bFunctionLength @@ -151,8 +150,14 @@ const char cfgDescriptor[] = { 0x00 // bInterval }; -const char strDescriptor[] = { - 26, // Length +static const char StrDescLanguageCodes[] = { + 4, // Length + 0x03, // Type is string + 0x09, 0x04 // supported language Code 0 = 0x0409 (English) +}; + +static const char StrDescManufacturer[] = { + 26, // Length 0x03, // Type is string 'p', 0x00, 'r', 0x00, @@ -165,9 +170,32 @@ const char strDescriptor[] = { '.', 0x00, 'o', 0x00, 'r', 0x00, - 'g', 0x00, + 'g', 0x00 }; +static const char StrDescProduct[] = { + 8, // Length + 0x03, // Type is string + 'P', 0x00, + 'M', 0x00, + '3', 0x00 +}; + +static const char* const pStrings[] = +{ + StrDescLanguageCodes, + StrDescManufacturer, + StrDescProduct +}; + +const char* getStringDescriptor(uint8_t idx) +{ + if(idx >= (sizeof(pStrings) / sizeof(pStrings[0]))) { + return(NULL); + } else { + return(pStrings[idx]); + } +} /* USB standard request code */ #define STD_GET_STATUS_ZERO 0x0080 @@ -223,7 +251,6 @@ byte_t btReceiveBank = AT91C_UDP_RX_DATA_BK0; void usb_disable() { // Disconnect the USB device AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU; -// SpinDelay(100); // Clear all lingering interrupts if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) { @@ -257,7 +284,6 @@ void usb_enable() { // Wait for a short while for (volatile size_t i=0; i<0x100000; i++); -// SpinDelay(100); // Reconnect USB reconnect AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU; @@ -295,33 +321,47 @@ bool usb_poll() return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank); } +/** + In github PR #129, some users appears to get a false positive from + usb_poll, which returns true, but the usb_read operation + still returns 0. + This check is basically the same as above, but also checks + that the length available to read is non-zero, thus hopefully fixes the + bug. +**/ +bool usb_poll_validate_length() +{ + + if (!usb_check()) return false; + if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false; + return (pUdp->UDP_CSR[AT91C_EP_OUT] >> 16) > 0; +} + //*---------------------------------------------------------------------------- //* \fn usb_read //* \brief Read available data from Endpoint OUT //*---------------------------------------------------------------------------- uint32_t usb_read(byte_t* data, size_t len) { - byte_t bank = btReceiveBank; + byte_t bank = btReceiveBank; uint32_t packetSize, nbBytesRcv = 0; - uint32_t time_out = 0; + uint32_t time_out = 0; - while (len) - { + while (len) { if (!usb_check()) break; if ( pUdp->UDP_CSR[AT91C_EP_OUT] & bank ) { packetSize = MIN(pUdp->UDP_CSR[AT91C_EP_OUT] >> 16, len); - len -= packetSize; + len -= packetSize; while(packetSize--) data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; pUdp->UDP_CSR[AT91C_EP_OUT] &= ~(bank); - if (bank == AT91C_UDP_RX_DATA_BK0) - { + if (bank == AT91C_UDP_RX_DATA_BK0) { bank = AT91C_UDP_RX_DATA_BK1; - } else { + } else { bank = AT91C_UDP_RX_DATA_BK0; - } + } } - if (time_out++ == 0x1fff) break; + if (time_out++ == 0x1fff) break; } btReceiveBank = bank; @@ -350,7 +390,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) { cpt = MIN(length, AT91C_EP_IN_SIZE-1); length -= cpt; while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++; - // Wait for the the first bank to be sent + // Wait for the first bank to be sent while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { if (!usb_check()) return length; } @@ -374,7 +414,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) { //* \fn AT91F_USB_SendData //* \brief Send Data through the control endpoint //*---------------------------------------------------------------------------- -unsigned int csrTab[100]; +unsigned int csrTab[100] = {0x00}; unsigned char csrIdx = 0; static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) { @@ -468,8 +508,14 @@ void AT91F_CDC_Enumerate() { AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength)); else if (wValue == 0x200) // Return Configuration Descriptor AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength)); - else if ((wValue & 0x300) == 0x300) // Return String Descriptor - AT91F_USB_SendData(pUdp, strDescriptor, MIN(sizeof(strDescriptor), wLength)); + else if ((wValue & 0xF00) == 0x300) { // Return String Descriptor + const char *strDescriptor = getStringDescriptor(wValue & 0xff); + if (strDescriptor != NULL) { + AT91F_USB_SendData(pUdp, strDescriptor, MIN(strDescriptor[0], wLength)); + } else { + AT91F_USB_SendStall(pUdp); + } + } else AT91F_USB_SendStall(pUdp); break;