diff --git a/armsrc/cmd.c b/armsrc/cmd.c index ee2565cd2..61353304c 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -92,12 +92,11 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data, // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE if (data && len) { - for (size_t i = 0; i < len; i++) { - txBufferNG.data[i] = data[i]; - } + memcpy(txBufferNG.data, data, len); } PacketResponseNGPostamble *tx_post = (PacketResponseNGPostamble *)((uint8_t *)&txBufferNG + sizeof(PacketResponseNGPreamble) + len); + // Note: if we send to both FPC & USB, we'll set CRC for both if any of them require CRC if ((g_reply_via_fpc && g_reply_with_crc_on_fpc) || ((g_reply_via_usb) && g_reply_with_crc_on_usb)) { uint8_t first, second; @@ -125,9 +124,14 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data, #endif } // we got two results, let's prioritize the faulty one and USB over FPC. - if (g_reply_via_usb && (resultusb != PM3_SUCCESS)) return resultusb; + if (g_reply_via_usb && (resultusb != PM3_SUCCESS)) { + return resultusb; + } + #ifdef WITH_FPC_USART_HOST - if (g_reply_via_fpc && (resultfpc != PM3_SUCCESS)) return resultfpc; + if (g_reply_via_fpc && (resultfpc != PM3_SUCCESS)) { + return resultfpc; + } #endif return PM3_SUCCESS; } @@ -145,36 +149,42 @@ int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v } uint8_t cmddata[PM3_CMD_DATA_SIZE]; memcpy(cmddata, arg, sizeof(arg)); - if (len && data) + if (len && data) { memcpy(cmddata + sizeof(arg), data, (int)len); + } - int res = reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); - return res; + return reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); } static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) { + PacketCommandNGRaw rx_raw; size_t bytes = read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble)); - if (bytes == 0) + if (bytes == 0) { return PM3_ENODATA; + } - if (bytes != sizeof(PacketCommandNGPreamble)) + if (bytes != sizeof(PacketCommandNGPreamble)) { return PM3_EIO; + } rx->magic = rx_raw.pre.magic; rx->ng = rx_raw.pre.ng; - uint16_t length = rx_raw.pre.length; rx->cmd = rx_raw.pre.cmd; + uint16_t length = rx_raw.pre.length; + if (rx->magic == COMMANDNG_PREAMBLE_MAGIC) { // New style NG command - if (length > PM3_CMD_DATA_SIZE) + if (length > PM3_CMD_DATA_SIZE) { return PM3_EOVFLOW; + } // Get the core and variable length payload bytes = read_ng((uint8_t *)&rx_raw.data, length); - if (bytes != length) + if (bytes != length) { return PM3_EIO; + } if (rx->ng) { memcpy(rx->data.asBytes, rx_raw.data, length); @@ -192,27 +202,33 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da memcpy(rx->data.asBytes, rx_raw.data + sizeof(arg), length - sizeof(arg)); rx->length = length - sizeof(arg); } + // Get the postamble bytes = read_ng((uint8_t *)&rx_raw.foopost, sizeof(PacketCommandNGPostamble)); - if (bytes != sizeof(PacketCommandNGPostamble)) + if (bytes != sizeof(PacketCommandNGPostamble)) { return PM3_EIO; + } // Check CRC, accept MAGIC as placeholder rx->crc = rx_raw.foopost.crc; if (rx->crc != COMMANDNG_POSTAMBLE_MAGIC) { uint8_t first, second; compute_crc(CRC_14443_A, (uint8_t *)&rx_raw, sizeof(PacketCommandNGPreamble) + length, &first, &second); - if ((first << 8) + second != rx->crc) + if ((first << 8) + second != rx->crc) { return PM3_EIO; + } } + g_reply_via_usb = usb; g_reply_via_fpc = fpc; + } else { // Old style command PacketCommandOLD rx_old; memcpy(&rx_old, &rx_raw.pre, sizeof(PacketCommandNGPreamble)); bytes = read_ng(((uint8_t *)&rx_old) + sizeof(PacketCommandNGPreamble), sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)); - if (bytes != sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)) + if (bytes != sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)) { return PM3_EIO; + } g_reply_via_usb = usb; g_reply_via_fpc = fpc; @@ -232,8 +248,9 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da int receive_ng(PacketCommandNG *rx) { // Check if there is a packet available - if (usb_poll_validate_length()) + if (usb_poll_validate_length()) { return receive_ng_internal(rx, usb_read_ng, true, false); + } #ifdef WITH_FPC_USART_HOST // Check if there is a FPC packet available diff --git a/armsrc/usart.c b/armsrc/usart.c index a205acb03..adc6f414f 100644 --- a/armsrc/usart.c +++ b/armsrc/usart.c @@ -89,10 +89,11 @@ static void usart_fill_rxfifo(void) { if (pUS1->US_RNCR == 0) { // One buffer got filled, backup buffer being used - if (us_rxfifo_low > us_rxfifo_high) + if (us_rxfifo_low > us_rxfifo_high) { rxfifo_free = us_rxfifo_low - us_rxfifo_high; - else + } else { rxfifo_free = sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low; + } uint16_t available = USART_BUFFLEN - usart_cur_inbuf_off; @@ -100,8 +101,9 @@ static void usart_fill_rxfifo(void) { for (uint16_t i = 0; i < available; i++) { us_rxfifo[us_rxfifo_high++] = usart_cur_inbuf[usart_cur_inbuf_off + i]; - if (us_rxfifo_high == sizeof(us_rxfifo)) + if (us_rxfifo_high == sizeof(us_rxfifo)) { us_rxfifo_high = 0; + } } // Give next buffer @@ -109,10 +111,11 @@ static void usart_fill_rxfifo(void) { pUS1->US_RNCR = USART_BUFFLEN; // Swap current buff - if (usart_cur_inbuf == us_in_a) + if (usart_cur_inbuf == us_in_a) { usart_cur_inbuf = us_in_b; - else + } else { usart_cur_inbuf = us_in_a; + } usart_cur_inbuf_off = 0; } else { @@ -133,15 +136,17 @@ static void usart_fill_rxfifo(void) { if (pUS1->US_RCR < USART_BUFFLEN - usart_cur_inbuf_off) { // Current buffer partially filled - if (us_rxfifo_low > us_rxfifo_high) + if (us_rxfifo_low > us_rxfifo_high) { rxfifo_free = (us_rxfifo_low - us_rxfifo_high); - else + } else { rxfifo_free = (sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low); + } uint16_t available = (USART_BUFFLEN - pUS1->US_RCR - usart_cur_inbuf_off); - if (available > rxfifo_free) + if (available > rxfifo_free) { available = rxfifo_free; + } for (uint16_t i = 0; i < available; i++) { us_rxfifo[us_rxfifo_high++] = usart_cur_inbuf[usart_cur_inbuf_off + i]; @@ -155,14 +160,19 @@ static void usart_fill_rxfifo(void) { uint16_t usart_rxdata_available(void) { usart_fill_rxfifo(); - if (us_rxfifo_low <= us_rxfifo_high) + if (us_rxfifo_low <= us_rxfifo_high) { return (us_rxfifo_high - us_rxfifo_low); - else + } else { return (sizeof(us_rxfifo) - us_rxfifo_low + us_rxfifo_high); + } } uint32_t usart_read_ng(uint8_t *data, size_t len) { - if (len == 0) return 0; + + if (len == 0) { + return 0; + } + uint32_t bytes_rcv = 0; uint32_t try = 0; // uint32_t highest_observed_try = 0; @@ -187,17 +197,20 @@ uint32_t usart_read_ng(uint8_t *data, size_t len) { // highest_observed_try = MAX(highest_observed_try, try); try = 0; } + len -= packetSize; + while (packetSize--) { if (us_rxfifo_low == sizeof(us_rxfifo)) { us_rxfifo_low = 0; } data[bytes_rcv++] = us_rxfifo[us_rxfifo_low++]; } + if (try++ == maxtry) { // Dbprintf_usb("Dbg USART TIMEOUT"); - break; - } + break; + } } // highest_observed_try = MAX(highest_observed_try, try); // Dbprintf_usb("Dbg USART max observed try %i", highest_observed_try); diff --git a/common_arm/usb_cdc.c b/common_arm/usb_cdc.c index f0050bd90..2447330c8 100644 --- a/common_arm/usb_cdc.c +++ b/common_arm/usb_cdc.c @@ -632,12 +632,15 @@ bool usb_check(void) { } bool usb_poll(void) { - if (!usb_check()) return false; + if (usb_check() == false) { + return false; + } + return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank); } inline uint16_t usb_available_length(void) { - return ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16); + return (((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) & 0x7FF); } /** @@ -649,9 +652,16 @@ inline uint16_t usb_available_length(void) { bug. **/ bool usb_poll_validate_length(void) { - if (!usb_check()) return false; - if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false; - return ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) > 0; + + if (usb_check() == false) { + return false; + } + + if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) { + return false; + } + + return (((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) > 0); } /* @@ -662,21 +672,25 @@ bool usb_poll_validate_length(void) { */ uint32_t usb_read(uint8_t *data, size_t len) { - if (len == 0) return 0; + if (len == 0) { + return 0; + } uint8_t bank = btReceiveBank; - uint32_t packetSize, nbBytesRcv = 0; - uint32_t time_out = 0; + uint16_t packetSize, nbBytesRcv = 0; + uint16_t time_out = 0; while (len) { - if (!usb_check()) + if (usb_check() == false) { break; + } if (pUdp->UDP_CSR[AT91C_EP_OUT] & bank) { - packetSize = ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16); + packetSize = (((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) & 0x7FF); packetSize = MIN(packetSize, len); len -= packetSize; + while (packetSize--) { data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; } @@ -684,14 +698,16 @@ uint32_t usb_read(uint8_t *data, size_t len) { // flip bank UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank) - if (bank == AT91C_UDP_RX_DATA_BK0) + if (bank == AT91C_UDP_RX_DATA_BK0) { bank = AT91C_UDP_RX_DATA_BK1; - else + } else { bank = AT91C_UDP_RX_DATA_BK0; + } } - if (time_out++ == 0x1fff) + if (time_out++ == 0x1FFF) { break; + } } btReceiveBank = bank; @@ -699,68 +715,91 @@ uint32_t usb_read(uint8_t *data, size_t len) { } static uint8_t usb_read_ng_buffer[64] = {0}; -static size_t usb_read_ng_bufoff = 0; -static size_t usb_read_ng_buflen = 0; +static uint8_t usb_read_ng_bufoffset = 0; +static uint8_t usb_read_ng_buflen = 0; uint32_t usb_read_ng(uint8_t *data, size_t len) { - if (len == 0) + if (len == 0) { return 0; + } uint8_t bank = btReceiveBank; - uint32_t packetSize, nbBytesRcv = 0; - uint32_t time_out = 0; + uint16_t packetSize, nbBytesRcv = 0; + uint16_t time_out = 0; // take first from local buffer if (len <= usb_read_ng_buflen) { - for (uint32_t i = 0; i < len; i++) { - data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoff + i]; + // if local buffer has all data + + for (uint8_t i = 0; i < len; i++) { + data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoffset + i]; } usb_read_ng_buflen -= len; - if (usb_read_ng_buflen == 0) - usb_read_ng_bufoff = 0; - else - usb_read_ng_bufoff += len; + + if (usb_read_ng_buflen == 0) { + usb_read_ng_bufoffset = 0; + } else { + usb_read_ng_bufoffset += len; + } return nbBytesRcv; + } else { - for (uint32_t i = 0; i < usb_read_ng_buflen; i++) { - data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoff + i]; + + // take all data from local buffer, then read from usb + + for (uint8_t i = 0; i < usb_read_ng_buflen; i++) { + data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoffset + i]; } + len -= usb_read_ng_buflen; usb_read_ng_buflen = 0; - usb_read_ng_bufoff = 0; + usb_read_ng_bufoffset = 0; } + while (len) { - if (!usb_check()) + + if (usb_check() == false) { break; + } if ((pUdp->UDP_CSR[AT91C_EP_OUT] & bank)) { - uint32_t available = ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16); + uint16_t available = (((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) & 0x7FF); + packetSize = MIN(available, len); available -= packetSize; len -= packetSize; + while (packetSize--) { data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; } + // fill the local buffer with the remaining bytes - for (uint32_t i = 0; i < available; i++) { + for (uint16_t i = 0; i < available; i++) { usb_read_ng_buffer[i] = pUdp->UDP_FDR[AT91C_EP_OUT]; } + + // update number of available bytes in local bytes usb_read_ng_buflen = available; + // flip bank UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank) - if (bank == AT91C_UDP_RX_DATA_BK0) + + if (bank == AT91C_UDP_RX_DATA_BK0) { bank = AT91C_UDP_RX_DATA_BK1; - else + } else { bank = AT91C_UDP_RX_DATA_BK0; + } } - if (time_out++ == 0x1fff) + + if (time_out++ == 0x1FFF) { break; + } } btReceiveBank = bank; @@ -775,16 +814,22 @@ uint32_t usb_read_ng(uint8_t *data, size_t len) { */ int usb_write(const uint8_t *data, const size_t len) { - if (!len) return PM3_EINVARG; - if (!usb_check()) return PM3_EIO; + if (len == 0) { + return PM3_EINVARG; + } + + if (usb_check() == false) { + return PM3_EIO; + } // can we write? - if ((pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) != 0) return PM3_EIO; + if ((pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) != 0) { + return PM3_EIO; + } size_t length = len; uint32_t cpt = 0; - // send first chunk cpt = MIN(length, AT91C_USB_EP_IN_SIZE); length -= cpt; @@ -806,7 +851,9 @@ int usb_write(const uint8_t *data, const size_t len) { // Wait for previous chunk to be sent // (iceman) when is the bankswapping done? while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } } UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP); @@ -818,7 +865,9 @@ int usb_write(const uint8_t *data, const size_t len) { // Wait for the end of transfer while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } } UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP); @@ -852,10 +901,14 @@ int usb_write(const uint8_t *data, const size_t len) { */ int async_usb_write_start(void) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } } isAsyncRequestFinished = false; @@ -927,7 +980,9 @@ inline bool async_usb_write_requestWrite(void) { int async_usb_write_stop(void) { // Wait for the end of transfer while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } } // clear transmission completed flag @@ -939,7 +994,9 @@ int async_usb_write_stop(void) { UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY); while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { - if (!usb_check()) return PM3_EIO; + if (usb_check() == false) { + return PM3_EIO; + } } UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP); @@ -961,12 +1018,13 @@ void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length) { uint32_t cpt = MIN(length, AT91C_USB_EP_CONTROL_SIZE); length -= cpt; - while (cpt--) + while (cpt--) { pudp->UDP_FDR[AT91C_EP_CONTROL] = *pData++; + } if (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) { UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); - while (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP); + while (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {}; } UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY); @@ -985,7 +1043,7 @@ void AT91F_USB_SendData(AT91PS_UDP pudp, const char *pData, uint32_t length) { if (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) { UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP); - while (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP); + while (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {}; } } @@ -1025,8 +1083,9 @@ void AT91F_CDC_Enumerate(void) { uint8_t bmRequestType, bRequest; uint16_t wValue, wIndex, wLength, wStatus; - if (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)) + if (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)) { return; + } bmRequestType = pUdp->UDP_FDR[AT91C_EP_CONTROL]; bRequest = pUdp->UDP_FDR[AT91C_EP_CONTROL]; @@ -1066,13 +1125,13 @@ void AT91F_CDC_Enumerate(void) { switch ((bRequest << 8) | bmRequestType) { case STD_GET_DESCRIPTOR: { - if (wValue == 0x100) // Return Device Descriptor + if (wValue == 0x100) { // Return Device Descriptor AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength)); - else if (wValue == 0x200) // Return Configuration Descriptor + } else if (wValue == 0x200) { // Return Configuration Descriptor AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength)); - else if ((wValue & 0xF00) == 0xF00) // Return BOS Descriptor + } else if ((wValue & 0xF00) == 0xF00) { // Return BOS Descriptor AT91F_USB_SendData(pUdp, bosDescriptor, MIN(sizeof(bosDescriptor), wLength)); - else if ((wValue & 0x300) == 0x300) { // Return String Descriptor + } else if ((wValue & 0x300) == 0x300) { // Return String Descriptor const char *strDescriptor = getStringDescriptor(wValue & 0xff); if (strDescriptor != NULL) { @@ -1164,9 +1223,13 @@ void AT91F_CDC_Enumerate(void) { wIndex &= 0x0F; if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) { - if (wIndex == AT91C_EP_OUT) pUdp->UDP_CSR[AT91C_EP_OUT] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT); - else if (wIndex == AT91C_EP_IN) pUdp->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN); - else if (wIndex == AT91C_EP_NOTIFY) pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN); + if (wIndex == AT91C_EP_OUT) { + pUdp->UDP_CSR[AT91C_EP_OUT] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT); + } else if (wIndex == AT91C_EP_IN) { + pUdp->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN); + } else if (wIndex == AT91C_EP_NOTIFY) { + pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN); + } AT91F_USB_SendZlp(pUdp); } else {