style, receive_ng_internal, when receiving NG frames we have an extra buffer. Somehow it seems to the be cause of issues with long emrtd dumps

This commit is contained in:
iceman1001 2024-03-03 22:02:38 +01:00
commit a5594d5f3c
3 changed files with 177 additions and 84 deletions

View file

@ -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 // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE
if (data && len) { if (data && len) {
for (size_t i = 0; i < len; i++) { memcpy(txBufferNG.data, data, len);
txBufferNG.data[i] = data[i];
}
} }
PacketResponseNGPostamble *tx_post = (PacketResponseNGPostamble *)((uint8_t *)&txBufferNG + sizeof(PacketResponseNGPreamble) + 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 // 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)) { 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; uint8_t first, second;
@ -125,9 +124,14 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data,
#endif #endif
} }
// we got two results, let's prioritize the faulty one and USB over FPC. // 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 #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 #endif
return PM3_SUCCESS; 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]; uint8_t cmddata[PM3_CMD_DATA_SIZE];
memcpy(cmddata, arg, sizeof(arg)); memcpy(cmddata, arg, sizeof(arg));
if (len && data) if (len && data) {
memcpy(cmddata + sizeof(arg), data, (int)len); memcpy(cmddata + sizeof(arg), data, (int)len);
}
int res = reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); return reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false);
return res;
} }
static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) { static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) {
PacketCommandNGRaw rx_raw; PacketCommandNGRaw rx_raw;
size_t bytes = read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble)); size_t bytes = read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble));
if (bytes == 0) if (bytes == 0) {
return PM3_ENODATA; return PM3_ENODATA;
}
if (bytes != sizeof(PacketCommandNGPreamble)) if (bytes != sizeof(PacketCommandNGPreamble)) {
return PM3_EIO; return PM3_EIO;
}
rx->magic = rx_raw.pre.magic; rx->magic = rx_raw.pre.magic;
rx->ng = rx_raw.pre.ng; rx->ng = rx_raw.pre.ng;
uint16_t length = rx_raw.pre.length;
rx->cmd = rx_raw.pre.cmd; rx->cmd = rx_raw.pre.cmd;
uint16_t length = rx_raw.pre.length;
if (rx->magic == COMMANDNG_PREAMBLE_MAGIC) { // New style NG command 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; return PM3_EOVFLOW;
}
// Get the core and variable length payload // Get the core and variable length payload
bytes = read_ng((uint8_t *)&rx_raw.data, length); bytes = read_ng((uint8_t *)&rx_raw.data, length);
if (bytes != length) if (bytes != length) {
return PM3_EIO; return PM3_EIO;
}
if (rx->ng) { if (rx->ng) {
memcpy(rx->data.asBytes, rx_raw.data, length); 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)); memcpy(rx->data.asBytes, rx_raw.data + sizeof(arg), length - sizeof(arg));
rx->length = length - sizeof(arg); rx->length = length - sizeof(arg);
} }
// Get the postamble // Get the postamble
bytes = read_ng((uint8_t *)&rx_raw.foopost, sizeof(PacketCommandNGPostamble)); bytes = read_ng((uint8_t *)&rx_raw.foopost, sizeof(PacketCommandNGPostamble));
if (bytes != sizeof(PacketCommandNGPostamble)) if (bytes != sizeof(PacketCommandNGPostamble)) {
return PM3_EIO; return PM3_EIO;
}
// Check CRC, accept MAGIC as placeholder // Check CRC, accept MAGIC as placeholder
rx->crc = rx_raw.foopost.crc; rx->crc = rx_raw.foopost.crc;
if (rx->crc != COMMANDNG_POSTAMBLE_MAGIC) { if (rx->crc != COMMANDNG_POSTAMBLE_MAGIC) {
uint8_t first, second; uint8_t first, second;
compute_crc(CRC_14443_A, (uint8_t *)&rx_raw, sizeof(PacketCommandNGPreamble) + length, &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; return PM3_EIO;
} }
}
g_reply_via_usb = usb; g_reply_via_usb = usb;
g_reply_via_fpc = fpc; g_reply_via_fpc = fpc;
} else { // Old style command } else { // Old style command
PacketCommandOLD rx_old; PacketCommandOLD rx_old;
memcpy(&rx_old, &rx_raw.pre, sizeof(PacketCommandNGPreamble)); memcpy(&rx_old, &rx_raw.pre, sizeof(PacketCommandNGPreamble));
bytes = read_ng(((uint8_t *)&rx_old) + sizeof(PacketCommandNGPreamble), sizeof(PacketCommandOLD) - 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; return PM3_EIO;
}
g_reply_via_usb = usb; g_reply_via_usb = usb;
g_reply_via_fpc = fpc; 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) { int receive_ng(PacketCommandNG *rx) {
// Check if there is a packet available // 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); return receive_ng_internal(rx, usb_read_ng, true, false);
}
#ifdef WITH_FPC_USART_HOST #ifdef WITH_FPC_USART_HOST
// Check if there is a FPC packet available // Check if there is a FPC packet available

View file

@ -89,10 +89,11 @@ static void usart_fill_rxfifo(void) {
if (pUS1->US_RNCR == 0) { // One buffer got filled, backup buffer being used 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; rxfifo_free = us_rxfifo_low - us_rxfifo_high;
else } else {
rxfifo_free = sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low; rxfifo_free = sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low;
}
uint16_t available = USART_BUFFLEN - usart_cur_inbuf_off; uint16_t available = USART_BUFFLEN - usart_cur_inbuf_off;
@ -100,19 +101,21 @@ static void usart_fill_rxfifo(void) {
for (uint16_t i = 0; i < available; i++) { for (uint16_t i = 0; i < available; i++) {
us_rxfifo[us_rxfifo_high++] = usart_cur_inbuf[usart_cur_inbuf_off + 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; us_rxfifo_high = 0;
} }
}
// Give next buffer // Give next buffer
pUS1->US_RNPR = (uint32_t)usart_cur_inbuf; pUS1->US_RNPR = (uint32_t)usart_cur_inbuf;
pUS1->US_RNCR = USART_BUFFLEN; pUS1->US_RNCR = USART_BUFFLEN;
// Swap current buff // Swap current buff
if (usart_cur_inbuf == us_in_a) if (usart_cur_inbuf == us_in_a) {
usart_cur_inbuf = us_in_b; usart_cur_inbuf = us_in_b;
else } else {
usart_cur_inbuf = us_in_a; usart_cur_inbuf = us_in_a;
}
usart_cur_inbuf_off = 0; usart_cur_inbuf_off = 0;
} else { } 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 (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); rxfifo_free = (us_rxfifo_low - us_rxfifo_high);
else } else {
rxfifo_free = (sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low); rxfifo_free = (sizeof(us_rxfifo) - us_rxfifo_high + us_rxfifo_low);
}
uint16_t available = (USART_BUFFLEN - pUS1->US_RCR - usart_cur_inbuf_off); uint16_t available = (USART_BUFFLEN - pUS1->US_RCR - usart_cur_inbuf_off);
if (available > rxfifo_free) if (available > rxfifo_free) {
available = rxfifo_free; available = rxfifo_free;
}
for (uint16_t i = 0; i < available; i++) { for (uint16_t i = 0; i < available; i++) {
us_rxfifo[us_rxfifo_high++] = usart_cur_inbuf[usart_cur_inbuf_off + 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) { uint16_t usart_rxdata_available(void) {
usart_fill_rxfifo(); usart_fill_rxfifo();
if (us_rxfifo_low <= us_rxfifo_high) if (us_rxfifo_low <= us_rxfifo_high) {
return (us_rxfifo_high - us_rxfifo_low); return (us_rxfifo_high - us_rxfifo_low);
else } else {
return (sizeof(us_rxfifo) - us_rxfifo_low + us_rxfifo_high); return (sizeof(us_rxfifo) - us_rxfifo_low + us_rxfifo_high);
} }
}
uint32_t usart_read_ng(uint8_t *data, size_t len) { 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 bytes_rcv = 0;
uint32_t try = 0; uint32_t try = 0;
// uint32_t highest_observed_try = 0; // uint32_t highest_observed_try = 0;
@ -187,13 +197,16 @@ uint32_t usart_read_ng(uint8_t *data, size_t len) {
// highest_observed_try = MAX(highest_observed_try, try); // highest_observed_try = MAX(highest_observed_try, try);
try = 0; try = 0;
} }
len -= packetSize; len -= packetSize;
while (packetSize--) { while (packetSize--) {
if (us_rxfifo_low == sizeof(us_rxfifo)) { if (us_rxfifo_low == sizeof(us_rxfifo)) {
us_rxfifo_low = 0; us_rxfifo_low = 0;
} }
data[bytes_rcv++] = us_rxfifo[us_rxfifo_low++]; data[bytes_rcv++] = us_rxfifo[us_rxfifo_low++];
} }
if (try++ == maxtry) { if (try++ == maxtry) {
// Dbprintf_usb("Dbg USART TIMEOUT"); // Dbprintf_usb("Dbg USART TIMEOUT");
break; break;

View file

@ -632,12 +632,15 @@ bool usb_check(void) {
} }
bool usb_poll(void) { bool usb_poll(void) {
if (!usb_check()) return false; if (usb_check() == false) {
return false;
}
return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank); return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);
} }
inline uint16_t usb_available_length(void) { 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. bug.
**/ **/
bool usb_poll_validate_length(void) { bool usb_poll_validate_length(void) {
if (!usb_check()) return false;
if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false; if (usb_check() == false) {
return ((pUdp->UDP_CSR[AT91C_EP_OUT] & AT91C_UDP_RXBYTECNT) >> 16) > 0; 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) { uint32_t usb_read(uint8_t *data, size_t len) {
if (len == 0) return 0; if (len == 0) {
return 0;
}
uint8_t bank = btReceiveBank; uint8_t bank = btReceiveBank;
uint32_t packetSize, nbBytesRcv = 0; uint16_t packetSize, nbBytesRcv = 0;
uint32_t time_out = 0; uint16_t time_out = 0;
while (len) { while (len) {
if (!usb_check()) if (usb_check() == false) {
break; break;
}
if (pUdp->UDP_CSR[AT91C_EP_OUT] & bank) { 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); packetSize = MIN(packetSize, len);
len -= packetSize; len -= packetSize;
while (packetSize--) { while (packetSize--) {
data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];
} }
@ -684,84 +698,109 @@ uint32_t usb_read(uint8_t *data, size_t len) {
// flip bank // flip bank
UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, 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; bank = AT91C_UDP_RX_DATA_BK1;
else } else {
bank = AT91C_UDP_RX_DATA_BK0; bank = AT91C_UDP_RX_DATA_BK0;
} }
}
if (time_out++ == 0x1fff) if (time_out++ == 0x1FFF) {
break; break;
} }
}
btReceiveBank = bank; btReceiveBank = bank;
return nbBytesRcv; return nbBytesRcv;
} }
static uint8_t usb_read_ng_buffer[64] = {0}; static uint8_t usb_read_ng_buffer[64] = {0};
static size_t usb_read_ng_bufoff = 0; static uint8_t usb_read_ng_bufoffset = 0;
static size_t usb_read_ng_buflen = 0; static uint8_t usb_read_ng_buflen = 0;
uint32_t usb_read_ng(uint8_t *data, size_t len) { uint32_t usb_read_ng(uint8_t *data, size_t len) {
if (len == 0) if (len == 0) {
return 0; return 0;
}
uint8_t bank = btReceiveBank; uint8_t bank = btReceiveBank;
uint32_t packetSize, nbBytesRcv = 0; uint16_t packetSize, nbBytesRcv = 0;
uint32_t time_out = 0; uint16_t time_out = 0;
// take first from local buffer // take first from local buffer
if (len <= usb_read_ng_buflen) { if (len <= usb_read_ng_buflen) {
for (uint32_t i = 0; i < len; i++) { // if local buffer has all data
data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoff + i];
for (uint8_t i = 0; i < len; i++) {
data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoffset + i];
} }
usb_read_ng_buflen -= len; usb_read_ng_buflen -= len;
if (usb_read_ng_buflen == 0)
usb_read_ng_bufoff = 0; if (usb_read_ng_buflen == 0) {
else usb_read_ng_bufoffset = 0;
usb_read_ng_bufoff += len; } else {
usb_read_ng_bufoffset += len;
}
return nbBytesRcv; return nbBytesRcv;
} else { } 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
}
len -= usb_read_ng_buflen; for (uint8_t i = 0; i < usb_read_ng_buflen; i++) {
usb_read_ng_buflen = 0; data[nbBytesRcv++] = usb_read_ng_buffer[usb_read_ng_bufoffset + i];
usb_read_ng_bufoff = 0;
} }
len -= usb_read_ng_buflen;
usb_read_ng_buflen = 0;
usb_read_ng_bufoffset = 0;
}
while (len) { while (len) {
if (!usb_check())
if (usb_check() == false) {
break; break;
}
if ((pUdp->UDP_CSR[AT91C_EP_OUT] & bank)) { 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); packetSize = MIN(available, len);
available -= packetSize; available -= packetSize;
len -= packetSize; len -= packetSize;
while (packetSize--) { while (packetSize--) {
data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT]; data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];
} }
// fill the local buffer with the remaining bytes // 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]; usb_read_ng_buffer[i] = pUdp->UDP_FDR[AT91C_EP_OUT];
} }
// update number of available bytes in local bytes
usb_read_ng_buflen = available; usb_read_ng_buflen = available;
// flip bank // flip bank
UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, 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; bank = AT91C_UDP_RX_DATA_BK1;
else } else {
bank = AT91C_UDP_RX_DATA_BK0; bank = AT91C_UDP_RX_DATA_BK0;
} }
if (time_out++ == 0x1fff) }
if (time_out++ == 0x1FFF) {
break; break;
} }
}
btReceiveBank = bank; btReceiveBank = bank;
return nbBytesRcv; return nbBytesRcv;
@ -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) { int usb_write(const uint8_t *data, const size_t len) {
if (!len) return PM3_EINVARG; if (len == 0) {
if (!usb_check()) return PM3_EIO; return PM3_EINVARG;
}
if (usb_check() == false) {
return PM3_EIO;
}
// can we write? // 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; size_t length = len;
uint32_t cpt = 0; uint32_t cpt = 0;
// send first chunk // send first chunk
cpt = MIN(length, AT91C_USB_EP_IN_SIZE); cpt = MIN(length, AT91C_USB_EP_IN_SIZE);
length -= cpt; length -= cpt;
@ -806,7 +851,9 @@ int usb_write(const uint8_t *data, const size_t len) {
// Wait for previous chunk to be sent // Wait for previous chunk to be sent
// (iceman) when is the bankswapping done? // (iceman) when is the bankswapping done?
while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { 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); 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 // Wait for the end of transfer
while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { 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); 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) { 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) { 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; isAsyncRequestFinished = false;
@ -927,7 +980,9 @@ inline bool async_usb_write_requestWrite(void) {
int async_usb_write_stop(void) { int async_usb_write_stop(void) {
// Wait for the end of transfer // Wait for the end of transfer
while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY) { 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 // clear transmission completed flag
@ -939,7 +994,9 @@ int async_usb_write_stop(void) {
UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY); UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);
while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) { 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); 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); uint32_t cpt = MIN(length, AT91C_USB_EP_CONTROL_SIZE);
length -= cpt; length -= cpt;
while (cpt--) while (cpt--) {
pudp->UDP_FDR[AT91C_EP_CONTROL] = *pData++; pudp->UDP_FDR[AT91C_EP_CONTROL] = *pData++;
}
if (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) { if (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {
UDP_CLEAR_EP_FLAGS(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); 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) { if (pudp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {
UDP_CLEAR_EP_FLAGS(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; uint8_t bmRequestType, bRequest;
uint16_t wValue, wIndex, wLength, wStatus; 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; return;
}
bmRequestType = pUdp->UDP_FDR[AT91C_EP_CONTROL]; bmRequestType = pUdp->UDP_FDR[AT91C_EP_CONTROL];
bRequest = 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) { switch ((bRequest << 8) | bmRequestType) {
case STD_GET_DESCRIPTOR: { 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)); 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)); 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)); 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); const char *strDescriptor = getStringDescriptor(wValue & 0xff);
if (strDescriptor != NULL) { if (strDescriptor != NULL) {
@ -1164,9 +1223,13 @@ void AT91F_CDC_Enumerate(void) {
wIndex &= 0x0F; wIndex &= 0x0F;
if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) { 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); if (wIndex == AT91C_EP_OUT) {
else if (wIndex == AT91C_EP_IN) pUdp->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN); pUdp->UDP_CSR[AT91C_EP_OUT] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT);
else if (wIndex == AT91C_EP_NOTIFY) pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN); } 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); AT91F_USB_SendZlp(pUdp);
} else { } else {