]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
Finally, rewrote bootrom and flasher program, much faster now
authorroel@libnfc.org <roel@libnfc.org@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Thu, 28 Feb 2013 15:11:52 +0000 (15:11 +0000)
committerroel@libnfc.org <roel@libnfc.org@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Thu, 28 Feb 2013 15:11:52 +0000 (15:11 +0000)
42 files changed:
armsrc/Makefile
armsrc/appmain.c
armsrc/cmd.c [deleted file]
armsrc/cmd.h [deleted file]
armsrc/usb_cdc.c [deleted file]
armsrc/usb_cdc.h [deleted file]
armsrc/util.h
bootrom/Makefile
bootrom/bootrom.c
bootrom/stdint.h [deleted file]
bootrom/usb_hid.c [deleted file]
bootrom/usb_hid.h [deleted file]
client/Makefile
client/cmddata.c
client/cmdhf.c
client/cmdhf14a.c
client/cmdhf14b.c
client/cmdhf15.c
client/cmdhfepa.c
client/cmdhficlass.c
client/cmdhflegic.c
client/cmdhfmf.h
client/cmdhw.c
client/cmdlf.c
client/cmdlfem4x.c
client/cmdlfhid.c
client/cmdlfhitag.c
client/cmdlfti.c
client/data.c
client/flash.c
client/flasher.c
client/mifarehost.c
client/mifarehost.h
client/proxusb.c [deleted file]
client/proxusb.h [deleted file]
common/Makefile.common
common/cmd.c [new file with mode: 0644]
common/cmd.h [new file with mode: 0644]
common/usb_cdc.c [new file with mode: 0644]
common/usb_cdc.h [new file with mode: 0644]
include/common.h
include/usb_cmd.h

index 47d56a2e5701b77d58935df6fa5757356dbcc642..2e5350bb3a406dac1d67a7ba3601ee2bef0d7ec5 100644 (file)
@@ -10,7 +10,7 @@ APP_INCLUDES = apps.h
 
 #remove one of the following defines and comment out the relevant line
 #in the next section to remove that particular feature from compilation  
-APP_CFLAGS     = -O2 -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG
+APP_CFLAGS     = -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG
 #-DWITH_LCD 
 
 #SRC_LCD = fonts.c LCD.c
index bdd04757f7a7da13d6606ca4e4f179d836a2cd22..2bd8caeaaf7e992ce8e4de91cbb1be89f2600d7b 100644 (file)
@@ -955,7 +955,7 @@ void  __attribute__((noreturn)) AppMain(void)
        LED_B_OFF();
        LED_A_OFF();
 
-  // Init USB device
+  // Init USB device`
   usb_enable();
 //     UsbStart();
 
diff --git a/armsrc/cmd.c b/armsrc/cmd.c
deleted file mode 100644 (file)
index ff5c4f8..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*\r
- * Proxmark send and receive commands\r
- *\r
- * Copyright (c) 2012, Roel Verdult\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. Neither the name of the copyright holders nor the\r
- * names of its contributors may be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * @file cmd.c\r
- * @brief\r
- */\r
-\r
-#include "cmd.h"\r
-#include "string.h"\r
-#include "util.h"\r
-#include "proxmark3.h"\r
-\r
-//static UsbCommand txcmd;\r
-\r
-bool cmd_receive(UsbCommand* cmd) {\r
\r
-  // Check if there is a usb packet available\r
-  if (!usb_poll()) return false;\r
-  \r
-  // Try to retrieve the available command frame\r
-  size_t rxlen = usb_read((byte_t*)cmd,sizeof(UsbCommand));\r
-\r
-  // Check if the transfer was complete\r
-  if (rxlen != sizeof(UsbCommand)) return false;\r
-  \r
-  // Received command successfully\r
-  return true;\r
-}\r
-\r
-bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r
-  UsbCommand txcmd;\r
-\r
-  // Compose the outgoing command frame\r
-  txcmd.cmd = cmd;\r
-  txcmd.arg[0] = arg0;\r
-  txcmd.arg[1] = arg1; \r
-  txcmd.arg[2] = arg2;\r
-\r
-  // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
-  if (data && len) {\r
-    memcpy(txcmd.d.asBytes,(byte_t*)data,MIN(len,USB_CMD_DATA_SIZE));\r
-         }\r
-  \r
-  // Send frame and make sure all bytes are transmitted\r
-  if (usb_write((byte_t*)&txcmd,sizeof(UsbCommand)) != 0) return false;\r
-  \r
-  return true;\r
-}\r
-\r
-\r
diff --git a/armsrc/cmd.h b/armsrc/cmd.h
deleted file mode 100644 (file)
index b330a21..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*\r
- * Proxmark send and receive commands\r
- *\r
- * Copyright (c) 2010, Roel Verdult\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. Neither the name of the copyright holders nor the\r
- * names of its contributors may be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * @file cmd.h\r
- * @brief\r
- */\r
-\r
-#ifndef _PROXMARK_CMD_H_\r
-#define _PROXMARK_CMD_H_\r
-\r
-#include <common.h>\r
-#include <usb_cmd.h>\r
-#include "usb_cdc.h"\r
-\r
-bool cmd_receive(UsbCommand* cmd);\r
-bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);\r
-\r
-#endif // _PROXMARK_CMD_H_\r
-\r
diff --git a/armsrc/usb_cdc.c b/armsrc/usb_cdc.c
deleted file mode 100644 (file)
index fa1f849..0000000
+++ /dev/null
@@ -1,567 +0,0 @@
-/*\r
- * at91sam7s USB CDC device implementation\r
- *\r
- * Copyright (c) 2012, Roel Verdult\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. Neither the name of the copyright holders nor the\r
- * names of its contributors may be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * based on the "Basic USB Example" from ATMEL (doc6123.pdf)\r
- *\r
- * @file usb_cdc.c\r
- * @brief\r
- */\r
-\r
-#include "usb_cdc.h"\r
-#include "util.h"\r
-#include "config_gpio.h"\r
-\r
-#define MIN(a, b) (((a) < (b)) ? (a) : (b))\r
-#define MAX(a, b) (((a) > (b)) ? (a) : (b))\r
-#define AT91C_EP_IN_SIZE  0x40\r
-#define AT91C_EP_OUT         1\r
-#define AT91C_EP_OUT_SIZE 0x40\r
-#define AT91C_EP_IN          2\r
-\r
-const char devDescriptor[] = {\r
-       /* Device descriptor */\r
-       0x12,      // bLength\r
-       0x01,      // bDescriptorType\r
-       0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10)\r
-       0x02,      // bDeviceClass:    CDC class code\r
-       0x00,      // bDeviceSubclass: CDC class sub code\r
-       0x00,      // bDeviceProtocol: CDC Device protocol\r
-       0x08,      // bMaxPacketSize0\r
-       0x2d,0x2d, // Vendor ID (--)\r
-       0x4d,0x50, // Product ID (PM), transmitted in reverse\r
-       0x01,0x00, // Device release number (0001)\r
-       0x01,      // iManufacturer    // 0x01\r
-       0x00,      // iProduct\r
-       0x00,      // SerialNumber\r
-       0x01       // bNumConfigs\r
-};\r
-\r
-const char cfgDescriptor[] = {\r
-       /* ============== CONFIGURATION 1 =========== */\r
-       /* Configuration 1 descriptor */\r
-       0x09,   // CbLength\r
-       0x02,   // CbDescriptorType\r
-       0x43,   // CwTotalLength 2 EP + Control\r
-       0x00,\r
-       0x02,   // CbNumInterfaces\r
-       0x01,   // CbConfigurationValue\r
-       0x00,   // CiConfiguration\r
-       0xC0,   // CbmAttributes 0xA0\r
-       0x00,   // CMaxPower\r
-\r
-       /* Communication Class Interface Descriptor Requirement */\r
-       0x09, // bLength\r
-       0x04, // bDescriptorType\r
-       0x00, // bInterfaceNumber\r
-       0x00, // bAlternateSetting\r
-       0x01, // bNumEndpoints\r
-       0x02, // bInterfaceClass\r
-       0x02, // bInterfaceSubclass\r
-       0x00, // bInterfaceProtocol\r
-       0x00, // iInterface\r
-\r
-       /* Header Functional Descriptor */\r
-       0x05, // bFunction Length\r
-       0x24, // bDescriptor type: CS_INTERFACE\r
-       0x00, // bDescriptor subtype: Header Func Desc\r
-       0x10, // bcdCDC:1.1\r
-       0x01,\r
-\r
-       /* ACM Functional Descriptor */\r
-       0x04, // bFunctionLength\r
-       0x24, // bDescriptor Type: CS_INTERFACE\r
-       0x02, // bDescriptor Subtype: ACM Func Desc\r
-       0x00, // bmCapabilities\r
-\r
-       /* Union Functional Descriptor */\r
-       0x05, // bFunctionLength\r
-       0x24, // bDescriptorType: CS_INTERFACE\r
-       0x06, // bDescriptor Subtype: Union Func Desc\r
-       0x00, // bMasterInterface: Communication Class Interface\r
-       0x01, // bSlaveInterface0: Data Class Interface\r
-\r
-       /* Call Management Functional Descriptor */\r
-       0x05, // bFunctionLength\r
-       0x24, // bDescriptor Type: CS_INTERFACE\r
-       0x01, // bDescriptor Subtype: Call Management Func Desc\r
-       0x00, // bmCapabilities: D1 + D0\r
-       0x01, // bDataInterface: Data Class Interface 1\r
-\r
-       /* Endpoint 1 descriptor */\r
-       0x07,   // bLength\r
-       0x05,   // bDescriptorType\r
-       0x83,   // bEndpointAddress, Endpoint 03 - IN\r
-       0x03,   // bmAttributes      INT\r
-       0x08,   // wMaxPacketSize\r
-       0x00,\r
-       0xFF,   // bInterval\r
-\r
-       /* Data Class Interface Descriptor Requirement */\r
-       0x09, // bLength\r
-       0x04, // bDescriptorType\r
-       0x01, // bInterfaceNumber\r
-       0x00, // bAlternateSetting\r
-       0x02, // bNumEndpoints\r
-       0x0A, // bInterfaceClass\r
-       0x00, // bInterfaceSubclass\r
-       0x00, // bInterfaceProtocol\r
-       0x00, // iInterface\r
-\r
-       /* First alternate setting */\r
-       /* Endpoint 1 descriptor */\r
-       0x07,   // bLength\r
-       0x05,   // bDescriptorType\r
-       0x01,   // bEndpointAddress, Endpoint 01 - OUT\r
-       0x02,   // bmAttributes      BULK\r
-       AT91C_EP_OUT_SIZE,   // wMaxPacketSize\r
-       0x00,\r
-       0x00,   // bInterval\r
-\r
-       /* Endpoint 2 descriptor */\r
-       0x07,   // bLength\r
-       0x05,   // bDescriptorType\r
-       0x82,   // bEndpointAddress, Endpoint 02 - IN\r
-       0x02,   // bmAttributes      BULK\r
-       AT91C_EP_IN_SIZE,   // wMaxPacketSize\r
-       0x00,\r
-       0x00    // bInterval\r
-};\r
-\r
-const char strDescriptor[] = {\r
-  26,                          // Length\r
-  0x03,                        // Type is string\r
-  'p', 0x00,\r
-  'r', 0x00,\r
-  'o', 0x00,\r
-  'x', 0x00,\r
-  'm', 0x00,\r
-  'a', 0x00,\r
-  'r', 0x00,\r
-  'k', 0x00,\r
-  '.', 0x00,\r
-  'o', 0x00,\r
-  'r', 0x00,\r
-  'g', 0x00,\r
-};\r
-\r
-\r
-/* USB standard request code */\r
-#define STD_GET_STATUS_ZERO           0x0080\r
-#define STD_GET_STATUS_INTERFACE      0x0081\r
-#define STD_GET_STATUS_ENDPOINT       0x0082\r
-\r
-#define STD_CLEAR_FEATURE_ZERO        0x0100\r
-#define STD_CLEAR_FEATURE_INTERFACE   0x0101\r
-#define STD_CLEAR_FEATURE_ENDPOINT    0x0102\r
-\r
-#define STD_SET_FEATURE_ZERO          0x0300\r
-#define STD_SET_FEATURE_INTERFACE     0x0301\r
-#define STD_SET_FEATURE_ENDPOINT      0x0302\r
-\r
-#define STD_SET_ADDRESS               0x0500\r
-#define STD_GET_DESCRIPTOR            0x0680\r
-#define STD_SET_DESCRIPTOR            0x0700\r
-#define STD_GET_CONFIGURATION         0x0880\r
-#define STD_SET_CONFIGURATION         0x0900\r
-#define STD_GET_INTERFACE             0x0A81\r
-#define STD_SET_INTERFACE             0x0B01\r
-#define STD_SYNCH_FRAME               0x0C82\r
-\r
-/* CDC Class Specific Request Code */\r
-#define GET_LINE_CODING               0x21A1\r
-#define SET_LINE_CODING               0x2021\r
-#define SET_CONTROL_LINE_STATE        0x2221\r
-\r
-typedef struct {\r
-       unsigned int dwDTERRate;\r
-       char bCharFormat;\r
-       char bParityType;\r
-       char bDataBits;\r
-} AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
-\r
-AT91S_CDC_LINE_CODING line = {\r
-       115200, // baudrate\r
-       0,      // 1 Stop Bit\r
-       0,      // None Parity\r
-       8};     // 8 Data bits\r
-\r
-void AT91F_CDC_Enumerate();\r
-\r
-AT91PS_UDP pUdp = AT91C_BASE_UDP;\r
-byte_t btConfiguration = 0;\r
-byte_t btConnection    = 0;\r
-byte_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_disable\r
-//* \brief This function deactivates the USB device\r
-//*----------------------------------------------------------------------------\r
-void usb_disable() {\r
-  // Disconnect the USB device\r
-  AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;\r
-  SpinDelay(100);\r
-  \r
-  // Clear all lingering interrupts\r
-  if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
-    pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
-  }\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_enable\r
-//* \brief This function Activates the USB device\r
-//*----------------------------------------------------------------------------\r
-void usb_enable() {\r
-  // Set the PLL USB Divider\r
-  AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1 ;\r
-  \r
-  // Specific Chip USB Initialisation\r
-  // Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock\r
-  AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;\r
-  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);\r
-  \r
-  // Enable UDP PullUp (USB_DP_PUP) : enable & Clear of the corresponding PIO\r
-  // Set in PIO mode and Configure in Output\r
-  AT91C_BASE_PIOA->PIO_PER = GPIO_USB_PU; // Set in PIO mode\r
-       AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU; // Configure as Output\r
-  \r
-  // Clear for set the Pullup resistor\r
-       AT91C_BASE_PIOA->PIO_CODR = GPIO_USB_PU;\r
-  \r
-  // Disconnect and reconnect USB controller for 100ms\r
-  usb_disable();\r
-  \r
-  // Wait for a short while\r
-  SpinDelay(100);\r
-\r
-  // Reconnect USB reconnect\r
-  AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;\r
-  AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU;\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_check\r
-//* \brief Test if the device is configured and handle enumeration\r
-//*----------------------------------------------------------------------------\r
-bool usb_check() {\r
-       AT91_REG isr = pUdp->UDP_ISR;\r
-\r
-       if (isr & AT91C_UDP_ENDBUSRES) {\r
-               pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
-               // reset all endpoints\r
-               pUdp->UDP_RSTEP  = (unsigned int)-1;\r
-               pUdp->UDP_RSTEP  = 0;\r
-               // Enable the function\r
-               pUdp->UDP_FADDR = AT91C_UDP_FEN;\r
-               // Configure endpoint 0\r
-               pUdp->UDP_CSR[0] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);\r
-       }\r
-       else if (isr & AT91C_UDP_EPINT0) {\r
-               pUdp->UDP_ICR = AT91C_UDP_EPINT0;\r
-               AT91F_CDC_Enumerate();\r
-       }\r
-       return (btConfiguration) ? true : false;\r
-}\r
-\r
-\r
-bool usb_poll()\r
-{\r
-  if (!usb_check()) return false;\r
-  return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_read\r
-//* \brief Read available data from Endpoint OUT\r
-//*----------------------------------------------------------------------------\r
-uint32_t usb_read(byte_t* data, size_t len) {\r
-  byte_t bank = btReceiveBank;\r
-       uint32_t packetSize, nbBytesRcv = 0;\r
-  uint32_t time_out = 0;\r
-  \r
-       while (len)\r
-  {\r
-               if (!usb_check()) break;\r
-\r
-               if ( pUdp->UDP_CSR[AT91C_EP_OUT] & bank ) {\r
-                       packetSize = MIN(pUdp->UDP_CSR[AT91C_EP_OUT] >> 16, len);\r
-      len -= packetSize;\r
-                       while(packetSize--)\r
-                               data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];\r
-                       pUdp->UDP_CSR[AT91C_EP_OUT] &= ~(bank);\r
-                       if (bank == AT91C_UDP_RX_DATA_BK0)\r
-      {\r
-                               bank = AT91C_UDP_RX_DATA_BK1;\r
-      } else {\r
-                               bank = AT91C_UDP_RX_DATA_BK0;\r
-      }\r
-               }\r
-    if (time_out++ == 0x1fff) break;\r
-       }\r
-\r
-       btReceiveBank = bank;\r
-       return nbBytesRcv;\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_write\r
-//* \brief Send through endpoint 2\r
-//*----------------------------------------------------------------------------\r
-uint32_t usb_write(const byte_t* data, const size_t len) {\r
-  size_t length = len;\r
-       uint32_t cpt = 0;\r
-\r
-  if (!length) return 0;\r
-  if (!usb_check()) return 0;\r
-  \r
-       // Send the first packet\r
-       cpt = MIN(length, AT91C_EP_IN_SIZE-1);\r
-       length -= cpt;\r
-       while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
-       pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;\r
-\r
-       while (length) {\r
-               // Fill the second bank\r
-               cpt = MIN(length, AT91C_EP_IN_SIZE-1);\r
-               length -= cpt;\r
-               while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
-               // Wait for the the first bank to be sent\r
-               while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
-                       if (!usb_check()) return length;\r
-    }\r
-               pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
-               pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;\r
-       }\r
-  \r
-       // Wait for the end of transfer\r
-       while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
-               if (!usb_check()) return length;\r
-  }\r
-  \r
-       pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
-\r
-       return length;\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    AT91F_USB_SendData\r
-//* \brief Send Data through the control endpoint\r
-//*----------------------------------------------------------------------------\r
-unsigned int csrTab[100];\r
-unsigned char csrIdx = 0;\r
-\r
-static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {\r
-       uint32_t cpt = 0;\r
-       AT91_REG csr;\r
-\r
-       do {\r
-               cpt = MIN(length, 8);\r
-               length -= cpt;\r
-\r
-               while (cpt--)\r
-                       pUdp->UDP_FDR[0] = *pData++;\r
-\r
-               if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
-                       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
-                       while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
-               }\r
-\r
-               pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
-               do {\r
-                       csr = pUdp->UDP_CSR[0];\r
-\r
-                       // Data IN stage has been stopped by a status OUT\r
-                       if (csr & AT91C_UDP_RX_DATA_BK0) {\r
-                               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0);\r
-                               return;\r
-                       }\r
-               } while ( !(csr & AT91C_UDP_TXCOMP) );\r
-\r
-       } while (length);\r
-\r
-       if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
-               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
-       }\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    AT91F_USB_SendZlp\r
-//* \brief Send zero length packet through the control endpoint\r
-//*----------------------------------------------------------------------------\r
-void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
-       pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
-       while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) );\r
-       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    AT91F_USB_SendStall\r
-//* \brief Stall the control endpoint\r
-//*----------------------------------------------------------------------------\r
-void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
-       pUdp->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;\r
-       while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_ISOERROR) );\r
-       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\r
-       while (pUdp->UDP_CSR[0] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    AT91F_CDC_Enumerate\r
-//* \brief This function is a callback invoked when a SETUP packet is received\r
-//*----------------------------------------------------------------------------\r
-void AT91F_CDC_Enumerate() {\r
-       byte_t bmRequestType, bRequest;\r
-       uint16_t wValue, wIndex, wLength, wStatus;\r
-\r
-       if ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RXSETUP) )\r
-               return;\r
-\r
-       bmRequestType = pUdp->UDP_FDR[0];\r
-       bRequest      = pUdp->UDP_FDR[0];\r
-       wValue        = (pUdp->UDP_FDR[0] & 0xFF);\r
-       wValue       |= (pUdp->UDP_FDR[0] << 8);\r
-       wIndex        = (pUdp->UDP_FDR[0] & 0xFF);\r
-       wIndex       |= (pUdp->UDP_FDR[0] << 8);\r
-       wLength       = (pUdp->UDP_FDR[0] & 0xFF);\r
-       wLength      |= (pUdp->UDP_FDR[0] << 8);\r
-\r
-       if (bmRequestType & 0x80) {\r
-               pUdp->UDP_CSR[0] |= AT91C_UDP_DIR;\r
-               while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_DIR) );\r
-       }\r
-       pUdp->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP;\r
-       while ( (pUdp->UDP_CSR[0]  & AT91C_UDP_RXSETUP)  );\r
-\r
-       // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1\r
-       switch ((bRequest << 8) | bmRequestType) {\r
-       case STD_GET_DESCRIPTOR:\r
-               if (wValue == 0x100)       // Return Device Descriptor\r
-                       AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength));\r
-               else if (wValue == 0x200)  // Return Configuration Descriptor\r
-                       AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength));\r
-               else if ((wValue & 0x300) == 0x300)  // Return String Descriptor\r
-                       AT91F_USB_SendData(pUdp, strDescriptor, MIN(sizeof(strDescriptor), wLength));\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
-               break;\r
-       case STD_SET_ADDRESS:\r
-               AT91F_USB_SendZlp(pUdp);\r
-               pUdp->UDP_FADDR = (AT91C_UDP_FEN | wValue);\r
-               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_FADDEN : 0;\r
-               break;\r
-       case STD_SET_CONFIGURATION:\r
-               btConfiguration = wValue;\r
-               AT91F_USB_SendZlp(pUdp);\r
-               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_CONFG : AT91C_UDP_FADDEN;\r
-               pUdp->UDP_CSR[1] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT) : 0;\r
-               pUdp->UDP_CSR[2] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN)  : 0;\r
-               pUdp->UDP_CSR[3] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN)   : 0;\r
-               break;\r
-       case STD_GET_CONFIGURATION:\r
-               AT91F_USB_SendData(pUdp, (char *) &(btConfiguration), sizeof(btConfiguration));\r
-               break;\r
-       case STD_GET_STATUS_ZERO:\r
-               wStatus = 0;\r
-               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               break;\r
-       case STD_GET_STATUS_INTERFACE:\r
-               wStatus = 0;\r
-               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               break;\r
-       case STD_GET_STATUS_ENDPOINT:\r
-               wStatus = 0;\r
-               wIndex &= 0x0F;\r
-               if ((pUdp->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= 3)) {\r
-                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
-                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == 0)) {\r
-                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
-                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
-               break;\r
-       case STD_SET_FEATURE_ZERO:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
-       case STD_SET_FEATURE_INTERFACE:\r
-               AT91F_USB_SendZlp(pUdp);\r
-               break;\r
-       case STD_SET_FEATURE_ENDPOINT:\r
-               wIndex &= 0x0F;\r
-               if ((wValue == 0) && wIndex && (wIndex <= 3)) {\r
-                       pUdp->UDP_CSR[wIndex] = 0;\r
-                       AT91F_USB_SendZlp(pUdp);\r
-               }\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
-               break;\r
-       case STD_CLEAR_FEATURE_ZERO:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
-       case STD_CLEAR_FEATURE_INTERFACE:\r
-               AT91F_USB_SendZlp(pUdp);\r
-               break;\r
-       case STD_CLEAR_FEATURE_ENDPOINT:\r
-               wIndex &= 0x0F;\r
-               if ((wValue == 0) && wIndex && (wIndex <= 3)) {\r
-                       if (wIndex == 1)\r
-                               pUdp->UDP_CSR[1] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT);\r
-                       else if (wIndex == 2)\r
-                               pUdp->UDP_CSR[2] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN);\r
-                       else if (wIndex == 3)\r
-                               pUdp->UDP_CSR[3] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_ISO_IN);\r
-                       AT91F_USB_SendZlp(pUdp);\r
-               }\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
-               break;\r
-\r
-       // handle CDC class requests\r
-       case SET_LINE_CODING:\r
-               while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) );\r
-               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0);\r
-               AT91F_USB_SendZlp(pUdp);\r
-               break;\r
-       case GET_LINE_CODING:\r
-               AT91F_USB_SendData(pUdp, (char *) &line, MIN(sizeof(line), wLength));\r
-               break;\r
-       case SET_CONTROL_LINE_STATE:\r
-               btConnection = wValue;\r
-               AT91F_USB_SendZlp(pUdp);\r
-               break;\r
-       default:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
-       }\r
-}\r
diff --git a/armsrc/usb_cdc.h b/armsrc/usb_cdc.h
deleted file mode 100644 (file)
index d7b9c2e..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*\r
- * at91sam7s USB CDC device implementation\r
- *\r
- * Copyright (c) 2012, Roel Verdult\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. Neither the name of the copyright holders nor the\r
- * names of its contributors may be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * based on the "Basic USB Example" from ATMEL (doc6123.pdf)\r
- *\r
- * @file usb_cdc.c\r
- * @brief\r
- */\r
-\r
-#ifndef _USB_CDC_H_\r
-#define _USB_CDC_H_\r
-\r
-#include <common.h>\r
-\r
-void usb_disable();\r
-void usb_enable();\r
-bool usb_check();\r
-bool usb_poll();\r
-uint32_t usb_read(byte_t* data, size_t len);\r
-uint32_t usb_write(const byte_t* data, const size_t len);\r
-\r
-#endif // _USB_CDC_H_\r
-\r
index b68c511c7c5007a52c8f721bb3c26e2eb355285a..d2a85ba0059786b8c1d272b77317097ac08329d9 100644 (file)
 #include <stdint.h>
 
 #define RAMFUNC __attribute((long_call, section(".ramfunc")))
-
 #define BYTEx(x, n) (((x) >> (n * 8)) & 0xff )
-#define MIN(a, b) (((a) < (b)) ? (a) : (b))
-#define MAX(a, b) (((a) > (b)) ? (a) : (b))
 
 #define LED_RED 1
 #define LED_ORANGE 2
index e2dc0dbdfddd613a7357d1adf28b7495547c18df..9237399566e786335d377b3a88a59f8cbcdd08e9 100644 (file)
@@ -8,15 +8,15 @@
 
 # DO NOT use thumb mode in the phase 1 bootloader since that generates a section with glue code
 ARMSRC = 
-THUMBSRC = usb_hid.c bootrom.c
+THUMBSRC = cmd.c usb_cdc.c bootrom.c
 ASMSRC = ram-reset.s flash-reset.s
 
 ## There is a strange bug with the linker: Sometimes it will not emit the glue to call
 ## BootROM from ARM mode. The symbol is emitted, but the section will be filled with
 ## zeroes. As a temporary workaround, do not use thumb for the phase 2 bootloader
 ## -- Henryk Plötz <henryk@ploetzli.ch> 2009-09-01
-ARMSRC := $(ARMSRC) $(THUMBSRC)
-THUMBSRC := 
+ARMSRC := $(ARMSRC) $(THUMBSRC)
+THUMBSRC := 
 
 # stdint.h provided locally until GCC 4.5 becomes C99 compliant
 APP_CFLAGS = -I.
index fc1c8a2c55c82d59d4b34d010543ecee2fca6596..afb49c3c791df9ac1214cbef5a554f8c5505d5f2 100644 (file)
@@ -7,7 +7,17 @@
 //-----------------------------------------------------------------------------
 
 #include <proxmark3.h>
-#include "usb_hid.h"
+#include "usb_cdc.h"
+#include "cmd.h"
+//#include "usb_hid.h"
+
+void DbpString(char *str) {
+  byte_t len = 0;
+  while (str[len] != 0x00) {
+    len++;
+  }
+  cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len);
+}
 
 struct common_area common_area __attribute__((section(".commonarea")));
 unsigned int start_addr, end_addr, bootrom_unlocked;
@@ -76,110 +86,130 @@ static void ConfigClocks(void)
 
 static void Fatal(void)
 {
-    for(;;);
+  LED_D_OFF();
+  LED_C_OFF();
+  LED_B_OFF();
+  LED_A_OFF();
+  for(;;);
 }
 
-void UsbPacketReceived(uint8_t *packet, int len)
-{
-    int i, dont_ack=0;
-    UsbCommand *c = (UsbCommand *)packet;
-    volatile uint32_t *p;
-
-    if(len != sizeof(*c)) {
-        Fatal();
-    }
-
-    switch(c->cmd) {
-        case CMD_DEVICE_INFO:
-            dont_ack = 1;
-            c->cmd = CMD_DEVICE_INFO;
-            c->arg[0] = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
-                DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
-            if(common_area.flags.osimage_present) c->arg[0] |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
-            UsbSendPacket(packet, len);
-            break;
-
-        case CMD_SETUP_WRITE:
-            /* The temporary write buffer of the embedded flash controller is mapped to the
-             * whole memory region, only the last 8 bits are decoded.
-             */
-            p = (volatile uint32_t *)&_flash_start;
-            for(i = 0; i < 12; i++) {
-                p[i+c->arg[0]] = c->d.asDwords[i];
-            }
-            break;
-
-        case CMD_FINISH_WRITE:
-            p = (volatile uint32_t *)&_flash_start;
-            for(i = 0; i < 4; i++) {
-                p[i+60] = c->d.asDwords[i];
-            }
-
-            /* Check that the address that we are supposed to write to is within our allowed region */
-            if( ((c->arg[0]+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (c->arg[0] < start_addr) ) {
-                /* Disallow write */
-                dont_ack = 1;
-                c->cmd = CMD_NACK;
-                UsbSendPacket(packet, len);
-            } else {
-                /* Translate address to flash page and do flash, update here for the 512k part */
-                AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
-                    MC_FLASH_COMMAND_PAGEN((c->arg[0]-(int)&_flash_start)/AT91C_IFLASH_PAGE_SIZE) |
-                    AT91C_MC_FCMD_START_PROG;
-            }
-
-            uint32_t sr;
-
-            while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY))
-                ;
-            if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
-                   dont_ack = 1;
-                    c->cmd = CMD_NACK;
-                    UsbSendPacket(packet, len);
-            }
-            break;
-
-        case CMD_HARDWARE_RESET:
-            USB_D_PLUS_PULLUP_OFF();
-            AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
-            break;
-
-        case CMD_START_FLASH:
-            if(c->arg[2] == START_FLASH_MAGIC) bootrom_unlocked = 1;
-            else bootrom_unlocked = 0;
-            {
-                int prot_start = (int)&_bootrom_start;
-                int prot_end = (int)&_bootrom_end;
-                int allow_start = (int)&_flash_start;
-                int allow_end = (int)&_flash_end;
-                int cmd_start = c->arg[0];
-                int cmd_end = c->arg[1];
-
-                /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
-                 * bootrom area. In any case they must be within the flash area.
-                 */
-                if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start)))
-                    && (cmd_start >= allow_start) && (cmd_end <= allow_end) ) {
-                    start_addr = cmd_start;
-                    end_addr = cmd_end;
-                } else {
-                    start_addr = end_addr = 0;
-                    dont_ack = 1;
-                    c->cmd = CMD_NACK;
-                    UsbSendPacket(packet, len);
-                }
-            }
-            break;
-
-        default:
-            Fatal();
-            break;
-    }
-
-    if(!dont_ack) {
-        c->cmd = CMD_ACK;
-        UsbSendPacket(packet, len);
-    }
+void UsbPacketReceived(uint8_t *packet, int len) {
+  int i, dont_ack=0;
+  UsbCommand* c = (UsbCommand *)packet;
+  volatile uint32_t *p;
+  
+  if(len != sizeof(UsbCommand)) {
+    Fatal();
+  }
+  
+  uint32_t arg0 = (uint32_t)c->arg[0];
+  
+  switch(c->cmd) {
+    case CMD_DEVICE_INFO: {
+      dont_ack = 1;
+//      c->cmd = CMD_DEVICE_INFO;
+      arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
+      DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
+      if(common_area.flags.osimage_present) {
+        arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
+      }
+//      UsbSendPacket(packet, len);
+      cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
+    } break;
+      
+    case CMD_SETUP_WRITE: {
+      /* The temporary write buffer of the embedded flash controller is mapped to the
+       * whole memory region, only the last 8 bits are decoded.
+       */
+      p = (volatile uint32_t *)&_flash_start;
+      for(i = 0; i < 12; i++) {
+        p[i+arg0] = c->d.asDwords[i];
+      }
+    } break;
+      
+    case CMD_FINISH_WRITE: {
+      uint32_t* flash_mem = (uint32_t*)(&_flash_start);
+//      p = (volatile uint32_t *)&_flash_start;
+      for (size_t j=0; j<2; j++) {
+        for(i = 0+(64*j); i < 64+(64*j); i++) {
+          //p[i+60] = c->d.asDwords[i];
+          flash_mem[i] = c->d.asDwords[i];
+        }
+        
+        uint32_t flash_address = arg0 + (0x100*j);
+        
+        /* Check that the address that we are supposed to write to is within our allowed region */
+        if( ((flash_address+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (flash_address < start_addr) ) {
+          /* Disallow write */
+          dont_ack = 1;
+          //        c->cmd = CMD_NACK;
+          //        UsbSendPacket(packet, len);
+          cmd_send(CMD_NACK,0,0,0,0,0);
+        } else {
+          uint32_t page_n = (flash_address - ((uint32_t)flash_mem)) / AT91C_IFLASH_PAGE_SIZE;
+          /* Translate address to flash page and do flash, update here for the 512k part */
+          AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
+          MC_FLASH_COMMAND_PAGEN(page_n) |
+          AT91C_MC_FCMD_START_PROG;
+          //        arg0 = (address - ((uint32_t)flash_s));
+        }
+        
+        // Wait until flashing of page finishes
+        uint32_t sr;
+        while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY));
+        if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
+          dont_ack = 1;
+          //        c->cmd = CMD_NACK;
+          cmd_send(CMD_NACK,0,0,0,0,0);
+          //        UsbSendPacket(packet, len);
+        }
+      }
+    } break;
+      
+    case CMD_HARDWARE_RESET: {
+//      USB_D_PLUS_PULLUP_OFF();
+      usb_disable();
+      AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
+    } break;
+      
+    case CMD_START_FLASH: {
+      if(c->arg[2] == START_FLASH_MAGIC) bootrom_unlocked = 1;
+      else bootrom_unlocked = 0;
+      {
+        int prot_start = (int)&_bootrom_start;
+        int prot_end = (int)&_bootrom_end;
+        int allow_start = (int)&_flash_start;
+        int allow_end = (int)&_flash_end;
+        int cmd_start = c->arg[0];
+        int cmd_end = c->arg[1];
+        
+        /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
+         * bootrom area. In any case they must be within the flash area.
+         */
+        if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start)))
+           && (cmd_start >= allow_start) && (cmd_end <= allow_end) ) {
+          start_addr = cmd_start;
+          end_addr = cmd_end;
+        } else {
+          start_addr = end_addr = 0;
+          dont_ack = 1;
+//          c->cmd = CMD_NACK;
+//          UsbSendPacket(packet, len);
+          cmd_send(CMD_NACK,0,0,0,0,0);
+        }
+      }
+    } break;
+      
+    default: {
+      Fatal();
+    } break;
+  }
+  
+  if(!dont_ack) {
+//    c->cmd = CMD_ACK;
+//    UsbSendPacket(packet, len);
+    cmd_send(CMD_ACK,arg0,0,0,0,0);
+  }
 }
 
 static void flash_mode(int externally_entered)
@@ -187,16 +217,34 @@ static void flash_mode(int externally_entered)
        start_addr = 0;
        end_addr = 0;
        bootrom_unlocked = 0;
+  byte_t rx[sizeof(UsbCommand)];
+       size_t rx_len;
+
+  usb_enable();
+  for (volatile size_t i=0; i<0x100000; i++);
+  LED_D_ON();
+  LED_C_ON();
+  LED_B_ON();
+  LED_A_ON();
 
-       UsbStart();
+//     UsbStart();
        for(;;) {
                WDT_HIT();
 
-               UsbPoll(TRUE);
+    if (usb_poll()) {
+      rx_len = usb_read(rx,sizeof(UsbCommand));
+      if (rx_len) {
+//        DbpString("starting to flash");
+        UsbPacketReceived(rx,rx_len);
+      }
+    }
+
+//             UsbPoll(TRUE);
 
                if(!externally_entered && !BUTTON_PRESS()) {
                        /* Perform a reset to leave flash mode */
-                       USB_D_PLUS_PULLUP_OFF();
+//                     USB_D_PLUS_PULLUP_OFF();
+      usb_disable();
                        LED_B_ON();
                        AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
                        for(;;);
@@ -208,7 +256,7 @@ static void flash_mode(int externally_entered)
        }
 }
 
-extern char _osimage_entry;
+extern uint32_t _osimage_entry;
 void BootROM(void)
 {
     //------------
@@ -252,7 +300,8 @@ void BootROM(void)
                GPIO_LED_C                      |
                GPIO_LED_D;
 
-    USB_D_PLUS_PULLUP_OFF();
+//    USB_D_PLUS_PULLUP_OFF();
+  usb_disable();
     LED_D_OFF();
     LED_C_ON();
     LED_B_OFF();
@@ -297,7 +346,7 @@ void BootROM(void)
            flash_mode(1);
     } else if(BUTTON_PRESS()) {
            flash_mode(0);
-    } else if(*(uint32_t*)&_osimage_entry == 0xffffffffU) {
+    } else if(_osimage_entry == 0xffffffffU) {
            flash_mode(1);
     } else {
            // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
diff --git a/bootrom/stdint.h b/bootrom/stdint.h
deleted file mode 100644 (file)
index 78a0b05..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
-//
-// This code is licensed to you under the terms of the GNU GPL, version 2 or,
-// at your option, any later version. See the LICENSE.txt file for the text of
-// the license.
-//-----------------------------------------------------------------------------
-// Replacement stdint.h because GCC doesn't come with it yet (C99)
-//-----------------------------------------------------------------------------
-
-#ifndef __STDINT_H
-#define __STDINT_H
-
-typedef signed char                            int8_t;
-typedef short int                              int16_t;
-typedef int                                            int32_t;
-typedef long long int                  int64_t;
-
-typedef unsigned char                  uint8_t;
-typedef unsigned short int             uint16_t;
-typedef unsigned int                   uint32_t;
-typedef unsigned long long int uint64_t;
-
-typedef int                                            intptr_t;
-typedef unsigned int                   uintptr_t;
-
-#endif /* __STDINT_H */
diff --git a/bootrom/usb_hid.c b/bootrom/usb_hid.c
deleted file mode 100644 (file)
index 189b409..0000000
+++ /dev/null
@@ -1,524 +0,0 @@
-//-----------------------------------------------------------------------------
-// Jonathan Westhues, split Aug 14 2005
-//
-// This code is licensed to you under the terms of the GNU GPL, version 2 or,
-// at your option, any later version. See the LICENSE.txt file for the text of
-// the license.
-//-----------------------------------------------------------------------------
-// The common USB driver used for both the bootloader and the application.
-//-----------------------------------------------------------------------------
-
-#include "proxmark3.h"
-#include "usb_hid.h"
-
-#define min(a, b) (((a) > (b)) ? (b) : (a))
-
-#define USB_REPORT_PACKET_SIZE 64
-
-typedef struct PACKED {
-       uint8_t         bmRequestType;
-       uint8_t         bRequest;
-       uint16_t        wValue;
-       uint16_t        wIndex;
-       uint16_t        wLength;
-} UsbSetupData;
-
-#define USB_REQUEST_GET_STATUS                                 0
-#define USB_REQUEST_CLEAR_FEATURE                              1
-#define USB_REQUEST_SET_FEATURE                                        3
-#define USB_REQUEST_SET_ADDRESS                                        5
-#define USB_REQUEST_GET_DESCRIPTOR                             6
-#define USB_REQUEST_SET_DESCRIPTOR                             7
-#define USB_REQUEST_GET_CONFIGURATION                  8
-#define USB_REQUEST_SET_CONFIGURATION                  9
-#define USB_REQUEST_GET_INTERFACE                              10
-#define USB_REQUEST_SET_INTERFACE                              11
-#define USB_REQUEST_SYNC_FRAME                                 12
-
-#define USB_DESCRIPTOR_TYPE_DEVICE                             1
-#define USB_DESCRIPTOR_TYPE_CONFIGURATION              2
-#define USB_DESCRIPTOR_TYPE_STRING                             3
-#define USB_DESCRIPTOR_TYPE_INTERFACE                  4
-#define USB_DESCRIPTOR_TYPE_ENDPOINT                   5
-#define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER   6
-#define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONF   7
-#define USB_DESCRIPTOR_TYPE_INTERFACE_POWER            8
-#define USB_DESCRIPTOR_TYPE_HID                                        0x21
-#define USB_DESCRIPTOR_TYPE_HID_REPORT                 0x22
-
-#define USB_DEVICE_CLASS_HID                                   0x03
-
-static const uint8_t HidReportDescriptor[] = {
-       0x06,0xA0,0xFF, // Usage Page (vendor defined) FFA0
-       0x09,0x01,              // Usage (vendor defined)
-       0xA1,0x01,              // Collection (Application)
-       0x09,0x02,      // Usage (vendor defined)
-       0xA1,0x00,      // Collection (Physical)
-       0x06,0xA1,0xFF, // Usage Page (vendor defined)
-
-       //The,input report
-       0x09,0x03,      // usage - vendor defined
-       0x09,0x04,      // usage - vendor defined
-       0x15,0x80,      // Logical Minimum (-128)
-       0x25,0x7F,      // Logical Maximum (127)
-       0x35,0x00,      // Physical Minimum (0)
-       0x45,0xFF,      // Physical Maximum (255)
-       0x75,0x08,      // Report Size (8)  (bits)
-       0x95,0x40,      // Report Count (64)  (fields)
-       0x81,0x02,      // Input (Data,Variable,Absolute)
-
-       //The,output report
-       0x09,0x05,      // usage - vendor defined
-       0x09,0x06,      // usage - vendor defined
-       0x15,0x80,      // Logical Minimum (-128)
-       0x25,0x7F,      // Logical Maximum (127)
-       0x35,0x00,      // Physical Minimum (0)
-       0x45,0xFF,      // Physical Maximum (255)
-       0x75,0x08,      // Report Size (8)  (bits)
-       0x95,0x40,      // Report Count (64)  (fields)
-       0x91,0x02,      // Output (Data,Variable,Absolute)
-
-       0xC0,                   // End Collection
-
-       0xC0,                   // End Collection
-};
-
-static const uint8_t DeviceDescriptor[] = {
-       0x12,                   // Descriptor length (18 bytes)
-       0x01,                   // Descriptor type (Device)
-       0x10,0x01,              // Complies with USB Spec. Release (0110h = release 1.10)
-       0x00,                   // Class code (0)
-       0x00,                   // Subclass code (0)
-       0x00,                   // Protocol (No specific protocol)
-       0x08,                   // Maximum packet size for Endpoint 0 (8 bytes)
-       0xc4,0x9a,              // Vendor ID (random numbers)
-       0x8f,0x4b,              // Product ID (random numbers)
-       0x01,0x00,              // Device release number (0001)
-       0x01,                   // Manufacturer string descriptor index
-       0x02,                   // Product string descriptor index
-       0x03,                   // Serial Number string descriptor index
-       0x01,                   // Number of possible configurations (1)
-};
-
-static const uint8_t ConfigurationDescriptor[] = {
-       0x09,                   // Descriptor length (9 bytes)
-       0x02,                   // Descriptor type (Configuration)
-       0x29,0x00,              // Total data length (41 bytes)
-       0x01,                   // Interface supported (1)
-       0x01,                   // Configuration value (1)
-       0x00,                   // Index of string descriptor (None)
-       0x80,                   // Configuration (Bus powered)
-       250,                    // Maximum power consumption (500mA)
-
-       //interface
-       0x09,                   // Descriptor length (9 bytes)
-       0x04,                   // Descriptor type (Interface)
-       0x00,                   // Number of interface (0)
-       0x00,                   // Alternate setting (0)
-       0x02,                   // Number of interface endpoint (2)
-       0x03,                   // Class code (HID)
-       0x00,                   // Subclass code ()
-       0x00,                   // Protocol code ()
-       0x00,                   // Index of string()
-
-       // class
-       0x09,                   // Descriptor length (9 bytes)
-       0x21,                   // Descriptor type (HID)
-       0x00,0x01,              // HID class release number (1.00)
-       0x00,                   // Localized country code (None)
-       0x01,                   // # of HID class dscrptr to follow (1)
-       0x22,                   // Report descriptor type (HID)
-       // Total length of report descriptor
-       sizeof(HidReportDescriptor),0x00,
-
-       // endpoint 1
-       0x07,                   // Descriptor length (7 bytes)
-       0x05,                   // Descriptor type (Endpoint)
-       0x01,                   // Encoded address (Respond to OUT)
-       0x03,                   // Endpoint attribute (Interrupt transfer)
-       0x08,0x00,              // Maximum packet size (8 bytes)
-       0x01,                   // Polling interval (1 ms)
-
-       // endpoint 2
-       0x07,                   // Descriptor length (7 bytes)
-       0x05,                   // Descriptor type (Endpoint)
-       0x82,                   // Encoded address (Respond to IN)
-       0x03,                   // Endpoint attribute (Interrupt transfer)
-       0x08,0x00,              // Maximum packet size (8 bytes)
-       0x01,                   // Polling interval (1 ms)
-};
-
-static const uint8_t StringDescriptor0[] = {
-       0x04,                   // Length
-       0x03,                   // Type is string
-       0x09,                   // English
-       0x04,                   //  US
-};
-
-static const uint8_t StringDescriptor1[] = {
-       24,                             // Length
-       0x03,                   // Type is string
-       'J', 0x00,
-       '.', 0x00,
-       ' ', 0x00,
-       'W', 0x00,
-       'e', 0x00,
-       's', 0x00,
-       't', 0x00,
-       'h', 0x00,
-       'u', 0x00,
-       'e', 0x00,
-       's', 0x00,
-};
-
-static const uint8_t StringDescriptor2[] = {
-       54,                             // Length
-       0x03,                   // Type is string
-       'P', 0x00,
-       'r', 0x00,
-       'o', 0x00,
-       'x', 0x00,
-       'M', 0x00,
-       'a', 0x00,
-       'r', 0x00,
-       'k', 0x00,
-       '-', 0x00,
-       '3', 0x00,
-       ' ', 0x00,
-       'R', 0x00,
-       'F', 0x00,
-       'I', 0x00,
-       'D', 0x00,
-       ' ', 0x00,
-       'I', 0x00,
-       'n', 0x00,
-       's', 0x00,
-       't', 0x00,
-       'r', 0x00,
-       'u', 0x00,
-       'm', 0x00,
-       'e', 0x00,
-       'n', 0x00,
-       't', 0x00,
-};
-
-// Serial Number
-// TODO: Pick yours! Don't forget to modify the length, if needed.
-static const uint8_t StringDescriptor3[] = {
-       18,             // Length
-       0x03,           // Type is string
-       'C', 0x00,
-       'h', 0x00,
-       'a', 0x00,
-       'n', 0x00,
-       'g', 0x00,
-       'e', 0x00,
-       'M', 0x00,
-       'e', 0x00,
-};
-
-static const uint8_t * const StringDescriptors[] = {
-       StringDescriptor0,
-       StringDescriptor1,
-       StringDescriptor2,
-       StringDescriptor3,
-};
-
-
-static uint8_t UsbBuffer[64];
-static int  UsbSoFarCount;
-
-static uint8_t CurrentConfiguration;
-
-static void UsbSendEp0(const uint8_t *data, int len)
-{
-       int thisTime, i;
-
-       do {
-               thisTime = min(len, 8);
-               len -= thisTime;
-
-               for(i = 0; i < thisTime; i++) {
-                       AT91C_BASE_UDP->UDP_FDR[0] = *data;
-                       data++;
-               }
-
-               if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {
-                       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
-                       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
-                               ;
-               }
-
-               AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;
-
-               do {
-                       if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) {
-                               // This means that the host is trying to write to us, so
-                               // abandon our write to them.
-                               AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RX_DATA_BK0;
-                               return;
-                       }
-               } while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP));
-       } while(len > 0);
-
-       if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {
-               AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
-               while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
-                       ;
-       }
-}
-
-static void UsbSendZeroLength(void)
-{
-       AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;
-
-       while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP))
-               ;
-
-       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
-
-       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
-               ;
-}
-
-static void UsbSendStall(void)
-{
-       AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;
-
-       while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT))
-               ;
-
-       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_STALLSENT;
-
-       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT)
-               ;
-}
-
-static void HandleRxdSetupData(void)
-{
-       int i;
-       UsbSetupData usd;
-
-       for(i = 0; i < sizeof(usd); i++) {
-               ((uint8_t *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0];
-       }
-
-       if(usd.bmRequestType & 0x80) {
-               AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_DIR;
-               while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_DIR))
-                       ;
-       }
-
-       AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP;
-       while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP)
-               ;
-
-       switch(usd.bRequest) {
-               case USB_REQUEST_GET_DESCRIPTOR:
-                       if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_DEVICE) {
-                               UsbSendEp0((uint8_t *)&DeviceDescriptor,
-                                       min(sizeof(DeviceDescriptor), usd.wLength));
-                       } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
-                               UsbSendEp0((uint8_t *)&ConfigurationDescriptor,
-                                       min(sizeof(ConfigurationDescriptor), usd.wLength));
-                       } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_STRING) {
-                               const uint8_t *s = StringDescriptors[usd.wValue & 0xff];
-                               UsbSendEp0(s, min(s[0], usd.wLength));
-                       } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_HID_REPORT) {
-                               UsbSendEp0((uint8_t *)&HidReportDescriptor,
-                                       min(sizeof(HidReportDescriptor), usd.wLength));
-                       } else {
-                               *((uint32_t *)0x00200000) = usd.wValue;
-                       }
-                       break;
-
-               case USB_REQUEST_SET_ADDRESS:
-                       UsbSendZeroLength();
-                       AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN | usd.wValue ;
-                       if(usd.wValue != 0) {
-                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
-                       } else {
-                               AT91C_BASE_UDP->UDP_GLBSTATE = 0;
-                       }
-                       break;
-
-               case USB_REQUEST_GET_CONFIGURATION:
-                       UsbSendEp0(&CurrentConfiguration, sizeof(CurrentConfiguration));
-                       break;
-
-               case USB_REQUEST_GET_STATUS: {
-                       if(usd.bmRequestType & 0x80) {
-                               uint16_t w = 0;
-                               UsbSendEp0((uint8_t *)&w, sizeof(w));
-                       }
-                       break;
-               }
-               case USB_REQUEST_SET_CONFIGURATION:
-                       CurrentConfiguration = usd.wValue;
-                       if(CurrentConfiguration) {
-                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
-                               AT91C_BASE_UDP->UDP_CSR[1] = AT91C_UDP_EPEDS |
-                                       AT91C_UDP_EPTYPE_INT_OUT;
-                               AT91C_BASE_UDP->UDP_CSR[2] = AT91C_UDP_EPEDS |
-                                       AT91C_UDP_EPTYPE_INT_IN;
-                       } else {
-                               AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
-                               AT91C_BASE_UDP->UDP_CSR[1] = 0;
-                               AT91C_BASE_UDP->UDP_CSR[2] = 0;
-                       }
-                       UsbSendZeroLength();
-                       break;
-
-               case USB_REQUEST_GET_INTERFACE: {
-                       uint8_t b = 0;
-                       UsbSendEp0(&b, sizeof(b));
-                       break;
-               }
-
-               case USB_REQUEST_SET_INTERFACE:
-                       UsbSendZeroLength();
-                       break;
-
-               case USB_REQUEST_CLEAR_FEATURE:
-               case USB_REQUEST_SET_FEATURE:
-                       UsbSendStall();
-                       break;
-               case USB_REQUEST_SET_DESCRIPTOR:
-               case USB_REQUEST_SYNC_FRAME:
-               default:
-                       break;
-       }
-}
-
-void UsbSendPacket(uint8_t *packet, int len)
-{
-       int i, thisTime;
-
-       while(len > 0) {
-               thisTime = min(len, 8);
-
-               for(i = 0; i < thisTime; i++) {
-                       AT91C_BASE_UDP->UDP_FDR[2] = packet[i];
-               }
-               AT91C_BASE_UDP->UDP_CSR[2] |= AT91C_UDP_TXPKTRDY;
-
-               while(!(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP)) {
-            WDT_HIT();
-        }
-
-               AT91C_BASE_UDP->UDP_CSR[2] &= ~AT91C_UDP_TXCOMP;
-
-               while(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP) {
-            WDT_HIT();
-        }
-
-               len -= thisTime;
-               packet += thisTime;
-       }
-}
-
-static void HandleRxdData(void)
-{
-       int i, len;
-
-       if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {
-               len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);
-
-               for(i = 0; i < len; i++) {
-                       UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];
-                       UsbSoFarCount++;
-               }
-
-               AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK0;
-               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {
-            WDT_HIT();
-        }
-
-               if(UsbSoFarCount >= 64) {
-                       UsbPacketReceived(UsbBuffer, UsbSoFarCount);
-                       UsbSoFarCount = 0;
-               }
-       }
-
-       if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {
-               len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);
-
-               for(i = 0; i < len; i++) {
-                       UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];
-                       UsbSoFarCount++;
-               }
-
-               AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK1;
-               while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {
-            WDT_HIT();
-        }
-        
-               if(UsbSoFarCount >= 64) {
-                       UsbPacketReceived(UsbBuffer, UsbSoFarCount);
-                       UsbSoFarCount = 0;
-               }
-       }
-    
-    WDT_HIT();
-}
-
-void UsbStart(void)
-{
-       volatile int i;
-
-       UsbSoFarCount = 0;
-
-       USB_D_PLUS_PULLUP_OFF();
-
-       for(i = 0; i < 1000000; i++)
-               ;
-
-       USB_D_PLUS_PULLUP_ON();
-
-       if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {
-               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
-       }
-}
-
-int UsbConnected()
-{
-       if (AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG)
-               return TRUE;
-       else
-               return FALSE;
-}
-
-int UsbPoll(int blinkLeds)
-{
-       int ret = FALSE;
-
-       if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {
-               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
-
-               // following a reset we should be ready to receive a setup packet
-               AT91C_BASE_UDP->UDP_RSTEP = 0xf;
-               AT91C_BASE_UDP->UDP_RSTEP = 0;
-
-               AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
-
-               AT91C_BASE_UDP->UDP_CSR[0] = AT91C_UDP_EPTYPE_CTRL | AT91C_UDP_EPEDS;
-
-               CurrentConfiguration = 0;
-
-               ret = TRUE;
-       }
-
-       if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(0)) {
-               if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP) {
-                       HandleRxdSetupData();
-                       ret = TRUE;
-               }
-       }
-
-       if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(1)) {
-               HandleRxdData();
-               ret = TRUE;
-       }
-
-       return ret;
-}
diff --git a/bootrom/usb_hid.h b/bootrom/usb_hid.h
deleted file mode 100644 (file)
index bbc6cec..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-#ifndef _USB_HID_H_\r
-#define _USB_HID_H_\r
-\r
-#include <common.h>\r
-#include <proxmark3.h>\r
-\r
-//--------------------------------\r
-// USB defines\r
-\r
-#define USB_D_PLUS_PULLUP_ON() { \\r
-HIGH(GPIO_USB_PU); \\r
-AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU; \\r
-}\r
-#define USB_D_PLUS_PULLUP_OFF() AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU\r
-\r
-//--------------------------------\r
-// USB declarations\r
-\r
-void UsbSendPacket(uint8_t *packet, int len);\r
-int UsbConnected();\r
-int UsbPoll(int blinkLeds);\r
-void UsbStart(void);\r
-\r
-// This function is provided by the apps/bootrom, and called from UsbPoll\r
-// if data are available.\r
-void UsbPacketReceived(uint8_t *packet, int len);\r
-\r
-#endif // _USB_HID_H_\r
-\r
index 364070e8364b4fc0b45913cb6e75153493e93276..4ab1b806f67069f11a1d3d0b9e0524e9b2233df9 100644 (file)
@@ -14,7 +14,7 @@ OBJDIR = obj
 
 LDLIBS = -L/opt/local/lib -L/usr/local/lib -lusb -lreadline -lpthread
 LDFLAGS = $(COMMON_FLAGS)
-CFLAGS = -std=gnu99 -I. -I../include -I../common -I/opt/local/include -Wall -Wno-unused-function $(COMMON_FLAGS) -g -O3
+CFLAGS = -std=c99 -I. -I../include -I../common -I/opt/local/include -Wall -Wno-unused-function $(COMMON_FLAGS) -g -O4
 
 ifneq (,$(findstring MINGW,$(platform)))
 CXXFLAGS = -I$(QTDIR)/include -I$(QTDIR)/include/QtCore -I$(QTDIR)/include/QtGui
@@ -30,7 +30,7 @@ CXXFLAGS = -I/Library/Frameworks/QtGui.framework/Versions/Current/Headers -I/Lib
 QTLDLIBS = -framework QtGui -framework QtCore
 MOC = moc 
 else
-CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall -O3
+CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall -O4
 QTLDLIBS = $(shell pkg-config --libs QtCore QtGui 2>/dev/null)
 MOC = $(shell pkg-config --variable=moc_location QtCore)
 endif
@@ -67,14 +67,15 @@ CMDSRCS = \
                        cmdhfmf.c \
                        cmdhw.c \
                        cmdlf.c \
-                       cmdlfem4x.c \
                        cmdlfhid.c \
+                       cmdlfem4x.c \
                        cmdlfhitag.c \
                        cmdlfti.c \
                        cmdparser.c \
                        cmdmain.c \
                        uart.c
 
+
 CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o)
 
 RM = rm -f
@@ -87,16 +88,16 @@ all-static: LDLIBS:=-static $(LDLIBS)
 all-static: snooper cli flasher
        
 proxmark3: LDLIBS+=$(QTLDLIBS)
-proxmark3: $(OBJDIR)/proxmark3.o $(CMDOBJS) $(OBJDIR)/proxusb.o $(QTGUI)
+proxmark3: $(OBJDIR)/proxmark3.o $(CMDOBJS) $(OBJDIR)/uart.o $(QTGUI)
        $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
 
-snooper: $(OBJDIR)/snooper.o $(CMDOBJS) $(OBJDIR)/proxusb.o $(OBJDIR)/guidummy.o
+snooper: $(OBJDIR)/snooper.o $(CMDOBJS) $(OBJDIR)/uart.o $(OBJDIR)/guidummy.o
        $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
 
-cli: $(OBJDIR)/cli.o $(CMDOBJS) $(OBJDIR)/proxusb.o $(OBJDIR)/guidummy.o
+cli: $(OBJDIR)/cli.o $(CMDOBJS) $(OBJDIR)/uart.o $(OBJDIR)/guidummy.o
        $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
 
-flasher: $(OBJDIR)/flash.o $(OBJDIR)/flasher.o $(OBJDIR)/proxusb.o
+flasher: $(OBJDIR)/flash.o $(OBJDIR)/flasher.o $(OBJDIR)/uart.o
        $(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
 
 $(OBJDIR)/%.o: %.c
index 1c58c69b956c5ac9468464bb754572607827653b..6c2c7841775fc0ab9a580bca22f386199a53165e 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
index cfbd9e168b837d241131a4cf5b96de4d438903ff..d955fc8321456ee48f74d935d0c68d511b738357 100644 (file)
@@ -9,7 +9,7 @@
 //-----------------------------------------------------------------------------
 
 #include <stdio.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "graph.h"
 #include "ui.h"
index 79273040271ee6bdb444d8c1bd17cda3641b9ea7..ad89fe96e5d4b5743bb22fe90f2d8e2a9790be45 100644 (file)
@@ -16,7 +16,7 @@
 #include "util.h"
 #include "iso14443crc.h"
 #include "data.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
@@ -169,7 +169,6 @@ int CmdHF14AReader(const char *Cmd)
   WaitForResponse(CMD_ACK,&resp);
        
        iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes;
-  uint8_t              * uid = card->uid;
 
        if(resp.arg[0] == 0) {
                PrintAndLog("iso14443a card select failed");
index 9b2fa753903fadad3eb999a196e1b655b8a7c1b3..f1238d223f19920b87c28cecaad77d76d9d84121 100644 (file)
@@ -14,7 +14,7 @@
 #include <string.h>
 #include <stdint.h>
 #include "iso14443crc.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
index e1e5e02a79e988ea397ffe3f10ba735e0360879a..ec8987559b94ee3377e4babf8be988ff7f2adfed 100644 (file)
@@ -26,7 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
index 41f801e705aa83c1b1242bfe31f65549e518a853..d9413cf3e2d84c03ed1149966b0514652a6a6907 100644 (file)
@@ -9,7 +9,7 @@
 //-----------------------------------------------------------------------------
 
 #include "util.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
index 944ec498e6103c0b4f25a544343cbf0e4cbdb628..f807e97215825f58c3253c566094a062dd7b3a98 100644 (file)
@@ -14,7 +14,7 @@
 #include <string.h>
 #include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
 #include "data.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
index 7a268e9235059d7b5c4bf57a43c8577dfc422ddd..26a2c7241feff2d324dfe9ec3718ab56a16cc6dd 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <stdio.h>
 #include <string.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
@@ -324,7 +324,7 @@ int CmdLegicRfSim(const char *Cmd)
    c.arg[0] = 6;
    c.arg[1] = 3;
    c.arg[2] = 0;
-   sscanf(Cmd, " %i %i %i", &c.arg[0], &c.arg[1], &c.arg[2]);
+   sscanf(Cmd, " %lli %lli %lli", &c.arg[0], &c.arg[1], &c.arg[2]);
    SendCommand(&c);
    return 0;
 }
@@ -332,7 +332,7 @@ int CmdLegicRfSim(const char *Cmd)
 int CmdLegicRfWrite(const char *Cmd)
 {
     UsbCommand c={CMD_WRITER_LEGIC_RF};
-    int res = sscanf(Cmd, " 0x%x 0x%x", &c.arg[0], &c.arg[1]);
+    int res = sscanf(Cmd, " 0x%llx 0x%llx", &c.arg[0], &c.arg[1]);
        if(res != 2) {
                PrintAndLog("Please specify the offset and length as two hex strings");
         return -1;
@@ -344,7 +344,7 @@ int CmdLegicRfWrite(const char *Cmd)
 int CmdLegicRfFill(const char *Cmd)
 {
     UsbCommand cmd ={CMD_WRITER_LEGIC_RF};
-    int res = sscanf(Cmd, " 0x%x 0x%x 0x%x", &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
+    int res = sscanf(Cmd, " 0x%llx 0x%llx 0x%llx", &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
     if(res != 3) {
         PrintAndLog("Please specify the offset, length and value as two hex strings");
         return -1;
index d7ee5a4b050fc9f2f9618ae7997c3012f509e3ff..65b789bf5df1d51f007fcbbf387a07bcc5fcebd5 100644 (file)
@@ -18,7 +18,7 @@
 #include "proxmark3.h"\r
 #include "iso14443crc.h"\r
 #include "data.h"\r
-#include "proxusb.h"\r
+//#include "proxusb.h"\r
 #include "ui.h"\r
 #include "cmdparser.h"\r
 #include "common.h"\r
index 991cd5324710f0558c1efd57a13e6ed39a3a1cf2..cdeb48b82914642fb8ec6384d05183c8c70169ec 100644 (file)
@@ -13,7 +13,7 @@
 #include <string.h>
 #include <limits.h>
 #include "ui.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "cmdparser.h"
 #include "cmdhw.h"
index 98a6c1f05ba6dc8f9384a42c9c63a9d742da9a5b..00f10088ead9055b9c8a978bee4397201c2ff8e4 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
@@ -36,7 +36,7 @@ int CmdLFCommandRead(const char *Cmd)
   dummy[0]= ' ';
 
   UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
-  sscanf(Cmd, "%i %i %i %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);
+  sscanf(Cmd, "%lli %lli %lli %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);
   // in case they specified 'h'
   strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
   SendCommand(&c);
index 83ed673b357fe86f4f03ab9eb2d06eae7effeada..efbd4c48a16cb199720de78f0cda5588da04f1cb 100644 (file)
@@ -11,7 +11,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <inttypes.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "graph.h"
index 93c351f1081774eee6c717c94c0239824d55f59b..d13856f4a3cc9442df48c188c8d67d3fbde671a8 100644 (file)
@@ -9,7 +9,7 @@
 //-----------------------------------------------------------------------------
 
 #include <stdio.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "graph.h"
index 1ee884017da9615c3f51460f03848775cdf5f31c..c648f6a5912862697219d76f735b3f73ce00a0f9 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "data.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
index 4e8b1150bfc52527738e0ee3f1d0d3909488a5d1..5a88f57953e3dc2f039d70b121ca116a6d1fedbd 100644 (file)
@@ -11,7 +11,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "crc16.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
@@ -274,7 +274,7 @@ int CmdTIWrite(const char *Cmd)
   UsbCommand c = {CMD_WRITE_TI_TYPE};
   int res = 0;
 
-  res = sscanf(Cmd, "0x%x 0x%x 0x%x ", &c.arg[0], &c.arg[1], &c.arg[2]);
+  res = sscanf(Cmd, "0x%llx 0x%llx 0x%llx ", &c.arg[0], &c.arg[1], &c.arg[2]);
   if (res == 2) c.arg[2]=0;
   if (res < 2)
     PrintAndLog("Please specify the data as two hex strings, optionally the CRC as a third");
index b6639867be6dcc9d04daf3f60436682f382fca5f..51134d487a279901e873616a1767120dee0e8511 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdint.h>
 #include "data.h"
 #include "ui.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "cmdmain.h"
 
index 8fe874e5cab6d0722def4396280073e88e018aa4..4d091126273a773a044b8e898f56cd533b15ef9f 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include "sleep.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "flash.h"
 #include "elf.h"
 #include "proxendian.h"
+#include "usb_cmd.h"
+
+void SendCommand(UsbCommand* txcmd);
+void ReceiveCommand(UsbCommand* rxcmd);
+void CloseProxmark();
+int OpenProxmark(size_t i);
 
 // FIXME: what the fuckity fuck
 unsigned int current_command = CMD_UNKNOWN;
@@ -26,7 +32,7 @@ unsigned int current_command = CMD_UNKNOWN;
 #define BOOTLOADER_SIZE        0x2000
 #define BOOTLOADER_END         (FLASH_START + BOOTLOADER_SIZE)
 
-#define BLOCK_SIZE             0x100
+#define BLOCK_SIZE             0x200
 
 static const uint8_t elf_ident[] = {
        0x7f, 'E', 'L', 'F',
@@ -267,11 +273,11 @@ fail:
 // Get the state of the proxmark, backwards compatible
 static int get_proxmark_state(uint32_t *state)
 {
-       HidCommand c;
+       UsbCommand c;
        c.cmd = CMD_DEVICE_INFO;
-       SendCommand_(&c);
-
-       HidCommand resp;
+//     SendCommand_(&c);
+  SendCommand(&c);
+       UsbCommand resp;
        ReceiveCommand(&resp);
 
        // Three outcomes:
@@ -290,7 +296,7 @@ static int get_proxmark_state(uint32_t *state)
                        *state = resp.arg[0];
                        break;
                default:
-                       fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04x\n", resp.cmd);
+                       fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04llx\n", resp.cmd);
                        return -1;
                        break;
        }
@@ -313,7 +319,7 @@ static int enter_bootloader(void)
 
        if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
                fprintf(stderr,"Entering bootloader...\n");
-               HidCommand c;
+               UsbCommand c;
                memset(&c, 0, sizeof (c));
 
                if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
@@ -322,12 +328,12 @@ static int enter_bootloader(void)
                        // New style handover: Send CMD_START_FLASH, which will reset the board
                        // and enter the bootrom on the next boot.
                        c.cmd = CMD_START_FLASH;
-                       SendCommand_(&c);
+                       SendCommand(&c);
                        fprintf(stderr,"(Press and release the button only to abort)\n");
                } else {
                        // Old style handover: Ask the user to press the button, then reset the board
                        c.cmd = CMD_HARDWARE_RESET;
-                       SendCommand_(&c);
+                       SendCommand(&c);
                        fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
                }
                fprintf(stderr,"Waiting for Proxmark to reappear on USB...");
@@ -349,10 +355,10 @@ static int enter_bootloader(void)
 
 static int wait_for_ack(void)
 {
-       HidCommand ack;
+  UsbCommand ack;
        ReceiveCommand(&ack);
        if (ack.cmd != CMD_ACK) {
-               printf("Error: Unexpected reply 0x%04x (expected ACK)\n", ack.cmd);
+               printf("Error: Unexpected reply 0x%04llx (expected ACK)\n", ack.cmd);
                return -1;
        }
        return 0;
@@ -372,7 +378,7 @@ int flash_start_flashing(int enable_bl_writes)
        if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
                // This command is stupid. Why the heck does it care which area we're
                // flashing, as long as it's not the bootloader area? The mind boggles.
-               HidCommand c = {CMD_START_FLASH};
+               UsbCommand c = {CMD_START_FLASH};
 
                if (enable_bl_writes) {
                        c.arg[0] = FLASH_START;
@@ -383,7 +389,8 @@ int flash_start_flashing(int enable_bl_writes)
                        c.arg[1] = FLASH_END;
                        c.arg[2] = 0;
                }
-               SendCommand_(&c);
+               SendCommand(&c);
+//             SendCommand_(&c);
                return wait_for_ack();
        } else {
                fprintf(stderr, "Note: Your bootloader does not understand the new START_FLASH command\n");
@@ -399,21 +406,26 @@ static int write_block(uint32_t address, uint8_t *data, uint32_t length)
 
        memset(block_buf, 0xFF, BLOCK_SIZE);
        memcpy(block_buf, data, length);
-
-       HidCommand c = {CMD_SETUP_WRITE};
+  UsbCommand c;
+/*
+       c.cmd = {CMD_SETUP_WRITE};
        for (int i = 0; i < 240; i += 48) {
                memcpy(c.d.asBytes, block_buf + i, 48);
                c.arg[0] = i / 4;
-               SendCommand_(&c);
-               if (wait_for_ack() < 0)
+               SendCommand(&c);
+//             SendCommand_(&c);
+               if (wait_for_ack() < 0) {
                        return -1;
+    }
        }
-
+*/
        c.cmd = CMD_FINISH_WRITE;
        c.arg[0] = address;
-       memcpy(c.d.asBytes, block_buf+240, 16);
-       SendCommand_(&c);
-       return wait_for_ack();
+//     memcpy(c.d.asBytes, block_buf+240, 16);
+//     SendCommand_(&c);
+       memcpy(c.d.asBytes, block_buf, length);
+  SendCommand(&c);
+  return wait_for_ack();
 }
 
 // Write a file's segments to Flash
@@ -472,7 +484,8 @@ void flash_free(flash_file_t *ctx)
 
 // just reset the unit
 int flash_stop_flashing(void) {
-       HidCommand c = {CMD_HARDWARE_RESET};
-       SendCommand_(&c);
-       return 0;
+       UsbCommand c = {CMD_HARDWARE_RESET};
+//     SendCommand_(&c);
+  SendCommand(&c);
+  return 0;
 }
index 85aae04970d59de1e2273998f849605a7edc823e..2e8bd2edb02372c201f09335af8ac336b7cbb115 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include "sleep.h"
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "flash.h"
+#include "uart.h"
+#include "usb_cmd.h"
+
+static serial_port sp;
+static char* serial_port_name;
+
+void cmd_debug(UsbCommand* UC) {
+  //  Debug
+  printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
+  printf("  cmd[len=%zd]: %016llx\n",sizeof(UC->cmd),UC->cmd);
+  printf(" arg0[len=%zd]: %016llx\n",sizeof(UC->arg[0]),UC->arg[0]);
+  printf(" arg1[len=%zd]: %016llx\n",sizeof(UC->arg[1]),UC->arg[1]);
+  printf(" arg2[len=%zd]: %016llx\n",sizeof(UC->arg[2]),UC->arg[2]);
+  printf(" data[len=%zd]: ",sizeof(UC->d.asBytes));
+  for (size_t i=0; i<16; i++) {
+    printf("%02x",UC->d.asBytes[i]);
+  }
+  printf("...\n");
+}
+
+void SendCommand(UsbCommand* txcmd) {
+//  printf("send: ");
+//  cmd_debug(txcmd);
+  if (!uart_send(sp,(byte_t*)txcmd,sizeof(UsbCommand))) {
+    printf("Sending bytes to proxmark failed\n");
+    exit(1);
+  }
+}
+
+void ReceiveCommand(UsbCommand* rxcmd) {
+  byte_t* prxcmd = (byte_t*)rxcmd;
+  byte_t* prx = prxcmd;
+  size_t rxlen;
+  while (true) {
+    rxlen = sizeof(UsbCommand) - (prx-prxcmd);
+    if (uart_receive(sp,prx,&rxlen)) {
+//      printf("received [%zd] bytes\n",rxlen);
+      prx += rxlen;
+      if ((prx-prxcmd) >= sizeof(UsbCommand)) {
+//        printf("received: ");
+//        cmd_debug(rxcmd);
+        return;
+      }
+    }
+  }
+}
+
+void CloseProxmark() {
+  // Clean up the port
+  uart_close(sp);
+}
+
+int OpenProxmark(size_t i) {
+  sp = uart_open(serial_port_name);
+  if (sp == INVALID_SERIAL_PORT) {
+    return 0;
+  }
+  return 1;
+}
 
 static void usage(char *argv0)
 {
-       fprintf(stderr, "Usage:   %s [-b] image.elf [image.elf...]\n\n", argv0);
+       fprintf(stderr, "Usage:   %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
        fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
        fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
 }
@@ -31,12 +90,12 @@ int main(int argc, char **argv)
 
        memset(files, 0, sizeof(files));
 
-       if (argc < 2) {
+       if (argc < 3) {
                usage(argv[0]);
                return -1;
        }
 
-       for (int i = 1; i < argc; i++) {
+       for (int i = 2; i < argc; i++) {
                if (argv[i][0] == '-') {
                        if (!strcmp(argv[i], "-b")) {
                                can_write_bl = 1;
@@ -55,11 +114,9 @@ int main(int argc, char **argv)
                }
        }
 
-       usb_init();
-
+  serial_port_name = argv[1];
        fprintf(stderr, "Waiting for Proxmark to appear on USB...");
        while (!OpenProxmark(0)) {
-               sleep(1);
                fprintf(stderr, ".");
        }
        fprintf(stderr, " Found.\n");
index 825e06d88609b74d8dcafc6495e72b3bed1baab6..14674b1635d83de45902ebf3c62821440506fecf 100644 (file)
@@ -15,7 +15,6 @@
 #include "proxmark3.h"\r
 \r
 // MIFARE\r
-\r
 int compar_int(const void * a, const void * b) {\r
        return (*(uint64_t*)b - *(uint64_t*)a);\r
 }\r
index a264002fd075b8ffcef2a1a29f35ea0a23e76bb5..9e026a55824527642af9fe331d74e35b5a2fcdf7 100644 (file)
@@ -15,7 +15,7 @@
 #include "cmdmain.h"\r
 #include "ui.h"\r
 #include "data.h"\r
-#include "proxusb.h"\r
+//#include "proxusb.h"\r
 #include "util.h"\r
 #include "nonce2key/nonce2key.h"\r
 #include "nonce2key/crapto1.h"\r
diff --git a/client/proxusb.c b/client/proxusb.c
deleted file mode 100644 (file)
index 2f152ac..0000000
+++ /dev/null
@@ -1,222 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
-// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
-//
-// This code is licensed to you under the terms of the GNU GPL, version 2 or,
-// at your option, any later version. See the LICENSE.txt file for the text of
-// the license.
-//-----------------------------------------------------------------------------
-// USB utilities
-//-----------------------------------------------------------------------------
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdbool.h>
-#include <unistd.h>
-#include <usb.h>
-#include <strings.h>
-#include <errno.h>
-
-#include "sleep.h"
-#include "proxusb.h"
-#include "proxmark3.h"
-#include "usb_cmd.h"
-
-// It seems to be missing for mingw
-#ifndef ETIMEDOUT
-#define ETIMEDOUT 116
-#endif
-
-usb_dev_handle *devh = NULL;
-static unsigned int claimed_iface = 0;
-unsigned char return_on_error = 0;
-unsigned char error_occured = 0;
-extern unsigned int current_command;
-
-void SendCommand_(HidCommand *c)
-{
-  int ret;
-
-#if 0
-  printf("Sending %d bytes\n", sizeof(HidCommand));
-#endif
-  current_command = c->cmd;
-  ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(HidCommand), 1000);
-  if (ret<0) {
-    error_occured = 1;
-    if (return_on_error)
-      return;
-
-    fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
-      usb_strerror());
-
-    if (devh) {
-      usb_close(devh);
-      devh = NULL;
-    }
-    while(!OpenProxmark(0)) { sleep(1); }
-    printf(PROXPROMPT);
-    fflush(NULL);
-
-    return;
-  }
-}
-
-bool ReceiveCommandPoll(HidCommand *c)
-{
-  int ret;
-
-  memset(c, 0, sizeof (HidCommand));
-  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(HidCommand), 500);
-  if (ret<0) {
-    if (ret != -ETIMEDOUT) {
-      error_occured = 1;
-      if (return_on_error)
-        return false;
-
-      fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
-        usb_strerror(), ret);
-
-      if (devh) {
-        usb_close(devh);
-        devh = NULL;
-      }
-      while(!OpenProxmark(0)) { sleep(1); }
-      printf(PROXPROMPT);
-      fflush(NULL);
-
-      return false;
-    }
-  } else {
-    if (ret && (ret < sizeof(HidCommand))) {
-      fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
-        ret, (int)sizeof(HidCommand));
-    }
-  }
-
-  return ret > 0;
-}
-
-void ReceiveCommand(HidCommand *c)
-{
-//  printf("%s()\n", __FUNCTION__);
-  int retval = 0;
-  do {
-    retval = ReceiveCommandPoll(c);
-    if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
-  } while(retval<0);
-//  printf("recv %x\n", c->cmd);
-}
-
-usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
-{
-  struct usb_bus *busses, *bus;
-  usb_dev_handle *handle = NULL;
-  struct prox_unit units[50];
-  int iUnit = 0;
-
-  usb_find_busses();
-  usb_find_devices();
-
-  busses = usb_get_busses();
-
-  for (bus = busses; bus; bus = bus->next) {
-    struct usb_device *dev;
-    
-    for (dev = bus->devices; dev; dev = dev->next) {
-      struct usb_device_descriptor *desc = &(dev->descriptor);
-
-      if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
-        handle = usb_open(dev);
-        if (!handle) {
-          if (verbose)
-            fprintf(stderr, "open fabiled: %s!\n", usb_strerror());
-          //return NULL;
-          continue;
-        }
-        *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
-
-        struct prox_unit unit = {handle, {0}};
-        usb_get_string_simple(handle, desc->iSerialNumber, unit.serial_number, sizeof(unit.serial_number));
-        units[iUnit++] = unit;
-
-        //return handle;
-      }
-    }
-  }
-
-  if (iUnit > 0) {
-    int iSelection = 0;
-
-    fprintf(stdout, "\nConnected units:\n");
-
-    for (int i = 0; i < iUnit; i++) {
-      struct usb_device * dev = usb_device(units[i].handle);
-      fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i+1, units[i].serial_number, dev->bus->dirname, dev->filename);
-    }
-    if (iUnit > 1) {
-      while (iSelection < 1 || iSelection > iUnit) {
-        fprintf(stdout, "Which unit do you want to connect to? ");
-        fscanf(stdin, "%d", &iSelection);
-        }
-      }
-    else
-      iSelection = 1;
-    iSelection --;
-
-    for (int i = 0; i < iUnit; i++) {
-      if (iSelection == i) continue;
-      usb_close(units[i].handle);
-      units[i].handle = NULL;
-    }
-
-    return units[iSelection].handle;
-  }
-
-  return NULL;
-}
-
-usb_dev_handle* OpenProxmark(int verbose)
-{
-  int ret;
-  usb_dev_handle *handle = NULL;
-  unsigned int iface;
-
-  handle = findProxmark(verbose, &iface);
-  if (!handle)
-    return NULL;
-
-#ifdef __linux__
-  /* detach kernel driver first */
-  ret = usb_detach_kernel_driver_np(handle, iface);
-  /* don't complain if no driver attached */
-  if (ret<0 && ret != -61 && verbose)
-    fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror());
-#endif
-
-  // Needed for Windows. Optional for Mac OS and Linux
-  ret = usb_set_configuration(handle, 1);
-  if (ret < 0) {
-    if (verbose)
-      fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
-    return NULL;
-  }
-
-  ret = usb_claim_interface(handle, iface);
-  if (ret < 0) {
-    if (verbose)
-      fprintf(stderr, "claim failed: %s!\n", usb_strerror());
-    return NULL;
-  }
-  claimed_iface = iface;
-  devh = handle;
-  return handle;
-}
-
-void CloseProxmark(void)
-{
-  usb_release_interface(devh, claimed_iface);
-  usb_close(devh);
-  devh = NULL;
-}
diff --git a/client/proxusb.h b/client/proxusb.h
deleted file mode 100644 (file)
index cc34fd4..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
-//
-// This code is licensed to you under the terms of the GNU GPL, version 2 or,
-// at your option, any later version. See the LICENSE.txt file for the text of
-// the license.
-//-----------------------------------------------------------------------------
-// USB utilities
-//-----------------------------------------------------------------------------
-
-#ifndef PROXUSB_H__
-#define PROXUSB_H__
-
-#include <stdint.h>
-#include <stdbool.h>
-#include <usb.h>
-#include "usb_cmd.h"
-
-extern unsigned char return_on_error;
-extern unsigned char error_occured;
-
-void SendCommand_(HidCommand *c);
-bool ReceiveCommandPoll(HidCommand *c);
-void ReceiveCommand(HidCommand *c);
-struct usb_dev_handle* FindProxmark(int verbose, unsigned int *iface);
-struct usb_dev_handle* OpenProxmark(int verbose);
-void CloseProxmark(void);
-
-struct prox_unit {
-  usb_dev_handle *handle;
-  char serial_number[256];
-};
-
-#endif
index 9ff05c5005e67e2a840f8081c8d0cf8922d056a5..2befd456d5c4df28718982babc08debba1586395 100644 (file)
@@ -65,7 +65,7 @@ VPATH = . ../common/ ../fpga/
 
 INCLUDES = ../include/proxmark3.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/usb_cmd.h $(APP_INCLUDES)
 
-CFLAGS =  -c $(INCLUDE) -Wall -Werror -pedantic -std=c99 $(APP_CFLAGS)
+CFLAGS =  -c $(INCLUDE) -Wall -Werror -pedantic -std=c99 $(APP_CFLAGS) -Os
 LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-gc-sections -n
 LIBS = -lgcc
 
diff --git a/common/cmd.c b/common/cmd.c
new file mode 100644 (file)
index 0000000..49d9d94
--- /dev/null
@@ -0,0 +1,81 @@
+/*\r
+ * Proxmark send and receive commands\r
+ *\r
+ * Copyright (c) 2012, Roel Verdult\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the copyright holders nor the\r
+ * names of its contributors may be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @file cmd.c\r
+ * @brief\r
+ */\r
+\r
+#include "cmd.h"\r
+#include "string.h"\r
+#include "proxmark3.h"\r
+\r
+//static UsbCommand txcmd;\r
+\r
+bool cmd_receive(UsbCommand* cmd) {\r
\r
+  // Check if there is a usb packet available\r
+  if (!usb_poll()) return false;\r
+  \r
+  // Try to retrieve the available command frame\r
+  size_t rxlen = usb_read((byte_t*)cmd,sizeof(UsbCommand));\r
+\r
+  // Check if the transfer was complete\r
+  if (rxlen != sizeof(UsbCommand)) return false;\r
+  \r
+  // Received command successfully\r
+  return true;\r
+}\r
+\r
+bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r
+  UsbCommand txcmd;\r
+\r
+  for (size_t i=0; i<sizeof(UsbCommand); i++) {\r
+    ((byte_t*)&txcmd)[i] = 0x00;\r
+  }\r
+  \r
+  // Compose the outgoing command frame\r
+  txcmd.cmd = cmd;\r
+  txcmd.arg[0] = arg0;\r
+  txcmd.arg[1] = arg1; \r
+  txcmd.arg[2] = arg2;\r
+\r
+  // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+  if (data && len) {\r
+    len = MIN(len,USB_CMD_DATA_SIZE);\r
+    for (size_t i=0; i<len; i++) {\r
+      txcmd.d.asBytes[i] = ((byte_t*)data)[i];\r
+    }\r
+  }\r
+  \r
+  // Send frame and make sure all bytes are transmitted\r
+  if (usb_write((byte_t*)&txcmd,sizeof(UsbCommand)) != 0) return false;\r
+  \r
+  return true;\r
+}\r
+\r
+\r
diff --git a/common/cmd.h b/common/cmd.h
new file mode 100644 (file)
index 0000000..b330a21
--- /dev/null
@@ -0,0 +1,44 @@
+/*\r
+ * Proxmark send and receive commands\r
+ *\r
+ * Copyright (c) 2010, Roel Verdult\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the copyright holders nor the\r
+ * names of its contributors may be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @file cmd.h\r
+ * @brief\r
+ */\r
+\r
+#ifndef _PROXMARK_CMD_H_\r
+#define _PROXMARK_CMD_H_\r
+\r
+#include <common.h>\r
+#include <usb_cmd.h>\r
+#include "usb_cdc.h"\r
+\r
+bool cmd_receive(UsbCommand* cmd);\r
+bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);\r
+\r
+#endif // _PROXMARK_CMD_H_\r
+\r
diff --git a/common/usb_cdc.c b/common/usb_cdc.c
new file mode 100644 (file)
index 0000000..e2787fb
--- /dev/null
@@ -0,0 +1,567 @@
+/*\r
+ * at91sam7s USB CDC device implementation\r
+ *\r
+ * Copyright (c) 2012, Roel Verdult\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the copyright holders nor the\r
+ * names of its contributors may be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * based on the "Basic USB Example" from ATMEL (doc6123.pdf)\r
+ *\r
+ * @file usb_cdc.c\r
+ * @brief\r
+ */\r
+\r
+#include "usb_cdc.h"\r
+#include "config_gpio.h"\r
+\r
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))\r
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))\r
+#define AT91C_EP_IN_SIZE  0x40\r
+#define AT91C_EP_OUT         1\r
+#define AT91C_EP_OUT_SIZE 0x40\r
+#define AT91C_EP_IN          2\r
+\r
+const char devDescriptor[] = {\r
+       /* Device descriptor */\r
+       0x12,      // bLength\r
+       0x01,      // bDescriptorType\r
+       0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10)\r
+       0x02,      // bDeviceClass:    CDC class code\r
+       0x00,      // bDeviceSubclass: CDC class sub code\r
+       0x00,      // bDeviceProtocol: CDC Device protocol\r
+       0x08,      // bMaxPacketSize0\r
+       0x2d,0x2d, // Vendor ID (--)\r
+       0x4d,0x50, // Product ID (PM), transmitted in reverse\r
+       0x01,0x00, // Device release number (0001)\r
+       0x01,      // iManufacturer    // 0x01\r
+       0x00,      // iProduct\r
+       0x00,      // SerialNumber\r
+       0x01       // bNumConfigs\r
+};\r
+\r
+const char cfgDescriptor[] = {\r
+       /* ============== CONFIGURATION 1 =========== */\r
+       /* Configuration 1 descriptor */\r
+       0x09,   // CbLength\r
+       0x02,   // CbDescriptorType\r
+       0x43,   // CwTotalLength 2 EP + Control\r
+       0x00,\r
+       0x02,   // CbNumInterfaces\r
+       0x01,   // CbConfigurationValue\r
+       0x00,   // CiConfiguration\r
+       0xC0,   // CbmAttributes 0xA0\r
+       0x00,   // CMaxPower\r
+\r
+       /* Communication Class Interface Descriptor Requirement */\r
+       0x09, // bLength\r
+       0x04, // bDescriptorType\r
+       0x00, // bInterfaceNumber\r
+       0x00, // bAlternateSetting\r
+       0x01, // bNumEndpoints\r
+       0x02, // bInterfaceClass\r
+       0x02, // bInterfaceSubclass\r
+       0x00, // bInterfaceProtocol\r
+       0x00, // iInterface\r
+\r
+       /* Header Functional Descriptor */\r
+       0x05, // bFunction Length\r
+       0x24, // bDescriptor type: CS_INTERFACE\r
+       0x00, // bDescriptor subtype: Header Func Desc\r
+       0x10, // bcdCDC:1.1\r
+       0x01,\r
+\r
+       /* ACM Functional Descriptor */\r
+       0x04, // bFunctionLength\r
+       0x24, // bDescriptor Type: CS_INTERFACE\r
+       0x02, // bDescriptor Subtype: ACM Func Desc\r
+       0x00, // bmCapabilities\r
+\r
+       /* Union Functional Descriptor */\r
+       0x05, // bFunctionLength\r
+       0x24, // bDescriptorType: CS_INTERFACE\r
+       0x06, // bDescriptor Subtype: Union Func Desc\r
+       0x00, // bMasterInterface: Communication Class Interface\r
+       0x01, // bSlaveInterface0: Data Class Interface\r
+\r
+       /* Call Management Functional Descriptor */\r
+       0x05, // bFunctionLength\r
+       0x24, // bDescriptor Type: CS_INTERFACE\r
+       0x01, // bDescriptor Subtype: Call Management Func Desc\r
+       0x00, // bmCapabilities: D1 + D0\r
+       0x01, // bDataInterface: Data Class Interface 1\r
+\r
+       /* Endpoint 1 descriptor */\r
+       0x07,   // bLength\r
+       0x05,   // bDescriptorType\r
+       0x83,   // bEndpointAddress, Endpoint 03 - IN\r
+       0x03,   // bmAttributes      INT\r
+       0x08,   // wMaxPacketSize\r
+       0x00,\r
+       0xFF,   // bInterval\r
+\r
+       /* Data Class Interface Descriptor Requirement */\r
+       0x09, // bLength\r
+       0x04, // bDescriptorType\r
+       0x01, // bInterfaceNumber\r
+       0x00, // bAlternateSetting\r
+       0x02, // bNumEndpoints\r
+       0x0A, // bInterfaceClass\r
+       0x00, // bInterfaceSubclass\r
+       0x00, // bInterfaceProtocol\r
+       0x00, // iInterface\r
+\r
+       /* First alternate setting */\r
+       /* Endpoint 1 descriptor */\r
+       0x07,   // bLength\r
+       0x05,   // bDescriptorType\r
+       0x01,   // bEndpointAddress, Endpoint 01 - OUT\r
+       0x02,   // bmAttributes      BULK\r
+       AT91C_EP_OUT_SIZE,   // wMaxPacketSize\r
+       0x00,\r
+       0x00,   // bInterval\r
+\r
+       /* Endpoint 2 descriptor */\r
+       0x07,   // bLength\r
+       0x05,   // bDescriptorType\r
+       0x82,   // bEndpointAddress, Endpoint 02 - IN\r
+       0x02,   // bmAttributes      BULK\r
+       AT91C_EP_IN_SIZE,   // wMaxPacketSize\r
+       0x00,\r
+       0x00    // bInterval\r
+};\r
+\r
+const char strDescriptor[] = {\r
+  26,                          // Length\r
+  0x03,                        // Type is string\r
+  'p', 0x00,\r
+  'r', 0x00,\r
+  'o', 0x00,\r
+  'x', 0x00,\r
+  'm', 0x00,\r
+  'a', 0x00,\r
+  'r', 0x00,\r
+  'k', 0x00,\r
+  '.', 0x00,\r
+  'o', 0x00,\r
+  'r', 0x00,\r
+  'g', 0x00,\r
+};\r
+\r
+\r
+/* USB standard request code */\r
+#define STD_GET_STATUS_ZERO           0x0080\r
+#define STD_GET_STATUS_INTERFACE      0x0081\r
+#define STD_GET_STATUS_ENDPOINT       0x0082\r
+\r
+#define STD_CLEAR_FEATURE_ZERO        0x0100\r
+#define STD_CLEAR_FEATURE_INTERFACE   0x0101\r
+#define STD_CLEAR_FEATURE_ENDPOINT    0x0102\r
+\r
+#define STD_SET_FEATURE_ZERO          0x0300\r
+#define STD_SET_FEATURE_INTERFACE     0x0301\r
+#define STD_SET_FEATURE_ENDPOINT      0x0302\r
+\r
+#define STD_SET_ADDRESS               0x0500\r
+#define STD_GET_DESCRIPTOR            0x0680\r
+#define STD_SET_DESCRIPTOR            0x0700\r
+#define STD_GET_CONFIGURATION         0x0880\r
+#define STD_SET_CONFIGURATION         0x0900\r
+#define STD_GET_INTERFACE             0x0A81\r
+#define STD_SET_INTERFACE             0x0B01\r
+#define STD_SYNCH_FRAME               0x0C82\r
+\r
+/* CDC Class Specific Request Code */\r
+#define GET_LINE_CODING               0x21A1\r
+#define SET_LINE_CODING               0x2021\r
+#define SET_CONTROL_LINE_STATE        0x2221\r
+\r
+typedef struct {\r
+       unsigned int dwDTERRate;\r
+       char bCharFormat;\r
+       char bParityType;\r
+       char bDataBits;\r
+} AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
+\r
+AT91S_CDC_LINE_CODING line = {\r
+       115200, // baudrate\r
+       0,      // 1 Stop Bit\r
+       0,      // None Parity\r
+       8};     // 8 Data bits\r
+\r
+void AT91F_CDC_Enumerate();\r
+\r
+AT91PS_UDP pUdp = AT91C_BASE_UDP;\r
+byte_t btConfiguration = 0;\r
+byte_t btConnection    = 0;\r
+byte_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_disable\r
+//* \brief This function deactivates the USB device\r
+//*----------------------------------------------------------------------------\r
+void usb_disable() {\r
+  // Disconnect the USB device\r
+  AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;\r
+//  SpinDelay(100);\r
+  \r
+  // Clear all lingering interrupts\r
+  if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+    pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
+  }\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_enable\r
+//* \brief This function Activates the USB device\r
+//*----------------------------------------------------------------------------\r
+void usb_enable() {\r
+  // Set the PLL USB Divider\r
+  AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1 ;\r
+  \r
+  // Specific Chip USB Initialisation\r
+  // Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock\r
+  AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;\r
+  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);\r
+  \r
+  // Enable UDP PullUp (USB_DP_PUP) : enable & Clear of the corresponding PIO\r
+  // Set in PIO mode and Configure in Output\r
+  AT91C_BASE_PIOA->PIO_PER = GPIO_USB_PU; // Set in PIO mode\r
+       AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU; // Configure as Output\r
+  \r
+  // Clear for set the Pullup resistor\r
+       AT91C_BASE_PIOA->PIO_CODR = GPIO_USB_PU;\r
+  \r
+  // Disconnect and reconnect USB controller for 100ms\r
+  usb_disable();\r
+  \r
+  // Wait for a short while\r
+  for (volatile size_t i=0; i<0x100000; i++);\r
+//  SpinDelay(100);\r
+\r
+  // Reconnect USB reconnect\r
+  AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;\r
+  AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU;\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_check\r
+//* \brief Test if the device is configured and handle enumeration\r
+//*----------------------------------------------------------------------------\r
+bool usb_check() {\r
+       AT91_REG isr = pUdp->UDP_ISR;\r
+\r
+       if (isr & AT91C_UDP_ENDBUSRES) {\r
+               pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
+               // reset all endpoints\r
+               pUdp->UDP_RSTEP  = (unsigned int)-1;\r
+               pUdp->UDP_RSTEP  = 0;\r
+               // Enable the function\r
+               pUdp->UDP_FADDR = AT91C_UDP_FEN;\r
+               // Configure endpoint 0\r
+               pUdp->UDP_CSR[0] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);\r
+       }\r
+       else if (isr & AT91C_UDP_EPINT0) {\r
+               pUdp->UDP_ICR = AT91C_UDP_EPINT0;\r
+               AT91F_CDC_Enumerate();\r
+       }\r
+       return (btConfiguration) ? true : false;\r
+}\r
+\r
+\r
+bool usb_poll()\r
+{\r
+  if (!usb_check()) return false;\r
+  return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_read\r
+//* \brief Read available data from Endpoint OUT\r
+//*----------------------------------------------------------------------------\r
+uint32_t usb_read(byte_t* data, size_t len) {\r
+  byte_t bank = btReceiveBank;\r
+       uint32_t packetSize, nbBytesRcv = 0;\r
+  uint32_t time_out = 0;\r
+  \r
+       while (len)\r
+  {\r
+               if (!usb_check()) break;\r
+\r
+               if ( pUdp->UDP_CSR[AT91C_EP_OUT] & bank ) {\r
+                       packetSize = MIN(pUdp->UDP_CSR[AT91C_EP_OUT] >> 16, len);\r
+      len -= packetSize;\r
+                       while(packetSize--)\r
+                               data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];\r
+                       pUdp->UDP_CSR[AT91C_EP_OUT] &= ~(bank);\r
+                       if (bank == AT91C_UDP_RX_DATA_BK0)\r
+      {\r
+                               bank = AT91C_UDP_RX_DATA_BK1;\r
+      } else {\r
+                               bank = AT91C_UDP_RX_DATA_BK0;\r
+      }\r
+               }\r
+    if (time_out++ == 0x1fff) break;\r
+       }\r
+\r
+       btReceiveBank = bank;\r
+       return nbBytesRcv;\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_write\r
+//* \brief Send through endpoint 2\r
+//*----------------------------------------------------------------------------\r
+uint32_t usb_write(const byte_t* data, const size_t len) {\r
+  size_t length = len;\r
+       uint32_t cpt = 0;\r
+\r
+  if (!length) return 0;\r
+  if (!usb_check()) return 0;\r
+  \r
+       // Send the first packet\r
+       cpt = MIN(length, AT91C_EP_IN_SIZE-1);\r
+       length -= cpt;\r
+       while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
+       pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;\r
+\r
+       while (length) {\r
+               // Fill the second bank\r
+               cpt = MIN(length, AT91C_EP_IN_SIZE-1);\r
+               length -= cpt;\r
+               while (cpt--) pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
+               // Wait for the the first bank to be sent\r
+               while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
+                       if (!usb_check()) return length;\r
+    }\r
+               pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);\r
+               while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
+               pUdp->UDP_CSR[AT91C_EP_IN] |= AT91C_UDP_TXPKTRDY;\r
+       }\r
+  \r
+       // Wait for the end of transfer\r
+       while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
+               if (!usb_check()) return length;\r
+  }\r
+  \r
+       pUdp->UDP_CSR[AT91C_EP_IN] &= ~(AT91C_UDP_TXCOMP);\r
+       while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
+\r
+       return length;\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    AT91F_USB_SendData\r
+//* \brief Send Data through the control endpoint\r
+//*----------------------------------------------------------------------------\r
+unsigned int csrTab[100];\r
+unsigned char csrIdx = 0;\r
+\r
+static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {\r
+       uint32_t cpt = 0;\r
+       AT91_REG csr;\r
+\r
+       do {\r
+               cpt = MIN(length, 8);\r
+               length -= cpt;\r
+\r
+               while (cpt--)\r
+                       pUdp->UDP_FDR[0] = *pData++;\r
+\r
+               if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
+                       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
+                       while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
+               }\r
+\r
+               pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
+               do {\r
+                       csr = pUdp->UDP_CSR[0];\r
+\r
+                       // Data IN stage has been stopped by a status OUT\r
+                       if (csr & AT91C_UDP_RX_DATA_BK0) {\r
+                               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0);\r
+                               return;\r
+                       }\r
+               } while ( !(csr & AT91C_UDP_TXCOMP) );\r
+\r
+       } while (length);\r
+\r
+       if (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) {\r
+               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
+               while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
+       }\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    AT91F_USB_SendZlp\r
+//* \brief Send zero length packet through the control endpoint\r
+//*----------------------------------------------------------------------------\r
+void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
+       pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
+       while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) );\r
+       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
+       while (pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP);\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    AT91F_USB_SendStall\r
+//* \brief Stall the control endpoint\r
+//*----------------------------------------------------------------------------\r
+void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
+       pUdp->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;\r
+       while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_ISOERROR) );\r
+       pUdp->UDP_CSR[0] &= ~(AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\r
+       while (pUdp->UDP_CSR[0] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));\r
+}\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    AT91F_CDC_Enumerate\r
+//* \brief This function is a callback invoked when a SETUP packet is received\r
+//*----------------------------------------------------------------------------\r
+void AT91F_CDC_Enumerate() {\r
+       byte_t bmRequestType, bRequest;\r
+       uint16_t wValue, wIndex, wLength, wStatus;\r
+\r
+       if ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RXSETUP) )\r
+               return;\r
+\r
+       bmRequestType = pUdp->UDP_FDR[0];\r
+       bRequest      = pUdp->UDP_FDR[0];\r
+       wValue        = (pUdp->UDP_FDR[0] & 0xFF);\r
+       wValue       |= (pUdp->UDP_FDR[0] << 8);\r
+       wIndex        = (pUdp->UDP_FDR[0] & 0xFF);\r
+       wIndex       |= (pUdp->UDP_FDR[0] << 8);\r
+       wLength       = (pUdp->UDP_FDR[0] & 0xFF);\r
+       wLength      |= (pUdp->UDP_FDR[0] << 8);\r
+\r
+       if (bmRequestType & 0x80) {\r
+               pUdp->UDP_CSR[0] |= AT91C_UDP_DIR;\r
+               while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_DIR) );\r
+       }\r
+       pUdp->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP;\r
+       while ( (pUdp->UDP_CSR[0]  & AT91C_UDP_RXSETUP)  );\r
+\r
+       // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1\r
+       switch ((bRequest << 8) | bmRequestType) {\r
+       case STD_GET_DESCRIPTOR:\r
+               if (wValue == 0x100)       // Return Device Descriptor\r
+                       AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength));\r
+               else if (wValue == 0x200)  // Return Configuration Descriptor\r
+                       AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength));\r
+               else if ((wValue & 0x300) == 0x300)  // Return String Descriptor\r
+                       AT91F_USB_SendData(pUdp, strDescriptor, MIN(sizeof(strDescriptor), wLength));\r
+               else\r
+                       AT91F_USB_SendStall(pUdp);\r
+               break;\r
+       case STD_SET_ADDRESS:\r
+               AT91F_USB_SendZlp(pUdp);\r
+               pUdp->UDP_FADDR = (AT91C_UDP_FEN | wValue);\r
+               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_FADDEN : 0;\r
+               break;\r
+       case STD_SET_CONFIGURATION:\r
+               btConfiguration = wValue;\r
+               AT91F_USB_SendZlp(pUdp);\r
+               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_CONFG : AT91C_UDP_FADDEN;\r
+               pUdp->UDP_CSR[1] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT) : 0;\r
+               pUdp->UDP_CSR[2] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN)  : 0;\r
+               pUdp->UDP_CSR[3] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN)   : 0;\r
+               break;\r
+       case STD_GET_CONFIGURATION:\r
+               AT91F_USB_SendData(pUdp, (char *) &(btConfiguration), sizeof(btConfiguration));\r
+               break;\r
+       case STD_GET_STATUS_ZERO:\r
+               wStatus = 0;\r
+               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               break;\r
+       case STD_GET_STATUS_INTERFACE:\r
+               wStatus = 0;\r
+               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               break;\r
+       case STD_GET_STATUS_ENDPOINT:\r
+               wStatus = 0;\r
+               wIndex &= 0x0F;\r
+               if ((pUdp->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= 3)) {\r
+                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
+                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               }\r
+               else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == 0)) {\r
+                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
+                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               }\r
+               else\r
+                       AT91F_USB_SendStall(pUdp);\r
+               break;\r
+       case STD_SET_FEATURE_ZERO:\r
+               AT91F_USB_SendStall(pUdp);\r
+           break;\r
+       case STD_SET_FEATURE_INTERFACE:\r
+               AT91F_USB_SendZlp(pUdp);\r
+               break;\r
+       case STD_SET_FEATURE_ENDPOINT:\r
+               wIndex &= 0x0F;\r
+               if ((wValue == 0) && wIndex && (wIndex <= 3)) {\r
+                       pUdp->UDP_CSR[wIndex] = 0;\r
+                       AT91F_USB_SendZlp(pUdp);\r
+               }\r
+               else\r
+                       AT91F_USB_SendStall(pUdp);\r
+               break;\r
+       case STD_CLEAR_FEATURE_ZERO:\r
+               AT91F_USB_SendStall(pUdp);\r
+           break;\r
+       case STD_CLEAR_FEATURE_INTERFACE:\r
+               AT91F_USB_SendZlp(pUdp);\r
+               break;\r
+       case STD_CLEAR_FEATURE_ENDPOINT:\r
+               wIndex &= 0x0F;\r
+               if ((wValue == 0) && wIndex && (wIndex <= 3)) {\r
+                       if (wIndex == 1)\r
+                               pUdp->UDP_CSR[1] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT);\r
+                       else if (wIndex == 2)\r
+                               pUdp->UDP_CSR[2] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN);\r
+                       else if (wIndex == 3)\r
+                               pUdp->UDP_CSR[3] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_ISO_IN);\r
+                       AT91F_USB_SendZlp(pUdp);\r
+               }\r
+               else\r
+                       AT91F_USB_SendStall(pUdp);\r
+               break;\r
+\r
+       // handle CDC class requests\r
+       case SET_LINE_CODING:\r
+               while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) );\r
+               pUdp->UDP_CSR[0] &= ~(AT91C_UDP_RX_DATA_BK0);\r
+               AT91F_USB_SendZlp(pUdp);\r
+               break;\r
+       case GET_LINE_CODING:\r
+               AT91F_USB_SendData(pUdp, (char *) &line, MIN(sizeof(line), wLength));\r
+               break;\r
+       case SET_CONTROL_LINE_STATE:\r
+               btConnection = wValue;\r
+               AT91F_USB_SendZlp(pUdp);\r
+               break;\r
+       default:\r
+               AT91F_USB_SendStall(pUdp);\r
+           break;\r
+       }\r
+}\r
diff --git a/common/usb_cdc.h b/common/usb_cdc.h
new file mode 100644 (file)
index 0000000..d7b9c2e
--- /dev/null
@@ -0,0 +1,48 @@
+/*\r
+ * at91sam7s USB CDC device implementation\r
+ *\r
+ * Copyright (c) 2012, Roel Verdult\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the copyright holders nor the\r
+ * names of its contributors may be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * based on the "Basic USB Example" from ATMEL (doc6123.pdf)\r
+ *\r
+ * @file usb_cdc.c\r
+ * @brief\r
+ */\r
+\r
+#ifndef _USB_CDC_H_\r
+#define _USB_CDC_H_\r
+\r
+#include <common.h>\r
+\r
+void usb_disable();\r
+void usb_enable();\r
+bool usb_check();\r
+bool usb_poll();\r
+uint32_t usb_read(byte_t* data, size_t len);\r
+uint32_t usb_write(const byte_t* data, const size_t len);\r
+\r
+#endif // _USB_CDC_H_\r
+\r
index 13daa86eb94b35085bb622ce757c5e292255cc61..ba286377fe356ac33686e4696d0badeb8e6710d0 100644 (file)
@@ -18,4 +18,7 @@
 #include <at91sam7s512.h>
 typedef unsigned char byte_t;
 
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
 #endif
index 62c0acd78d07e485b50544fe72b65c5d4f6488f0..349496b7f61c9fabb6f53ece948d33ef3f2b4a90 100644 (file)
@@ -23,15 +23,6 @@ typedef BYTE uint8_t;
 #define PACKED __attribute__((packed))
 #endif
 
-typedef struct {
-  uint32_t     cmd;
-  uint32_t     arg[3];
-  union {
-    uint8_t            asBytes[48];
-    uint32_t   asDwords[12];
-  } d;
-} PACKED HidCommand;
-
 #define USB_CMD_DATA_SIZE 512
 
 typedef struct {
Impressum, Datenschutz