From b4a03581c201dfaa305d58d1482e158f19223c63 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 5 Oct 2017 15:55:08 +0200 Subject: [PATCH] CHG: adjustments to the USB reading part. --- include/at91sam7s512.h | 2 +- include/proxmark3.h | 5 ++- include/usb_cmd.h | 1 + uart/uart_win32.c | 69 ++++++++++++++++++++++++++++++------------ 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/include/at91sam7s512.h b/include/at91sam7s512.h index 2cdcbce38..47c79ee41 100644 --- a/include/at91sam7s512.h +++ b/include/at91sam7s512.h @@ -1738,7 +1738,7 @@ typedef struct _AT91S_UDP { #define AT91C_UDP_EPEDS (0x1 << 15) // (UDP) Endpoint Enable Disable #define AT91C_UDP_RXBYTECNT (0x7FF << 16) // (UDP) Number Of Bytes Available in the FIFO // -------- UDP_TXVC : (UDP Offset: 0x74) Transceiver Control Register -------- -#define AT91C_UDP_TXVDIS (0x1 << 8) // (UDP) +#define AT91C_UDP_TXVDIS (0x1 << 8) // (UDP) Transceiver Disable // ***************************************************************************** // REGISTER ADDRESS DEFINITION FOR AT91SAM7S512 diff --git a/include/proxmark3.h b/include/proxmark3.h index 3d3800ccd..75e446d5c 100644 --- a/include/proxmark3.h +++ b/include/proxmark3.h @@ -81,7 +81,10 @@ #define LED_D_INV() INVBIT(GPIO_LED_D) #define RELAY_ON() HIGH(GPIO_RELAY) #define RELAY_OFF() LOW(GPIO_RELAY) -#define BUTTON_PRESS() !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_BUTTON) +#define BUTTON_PRESS() !((AT91C_BASE_PIOA->PIO_PDSR & GPIO_BUTTON) == GPIO_BUTTON) + +//NVDD goes LOW when USB is attached. +#define USB_ATTACHED() !((AT91C_BASE_PIOA->PIO_PDSR & GPIO_NVDD_ON) == GPIO_NVDD_ON) #define VERSION_INFORMATION_MAGIC 0x56334d50 struct version_information { diff --git a/include/usb_cmd.h b/include/usb_cmd.h index d941d4e2c..53cfad5d7 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -202,6 +202,7 @@ typedef struct{ #define CMD_MIFARE_CHKKEYS 0x0623 #define CMD_MIFARE_SETMOD 0x0624 +#define CMD_MIFARE_CHKKEYS_FAST 0x0625 #define CMD_MIFARE_SNIFFER 0x0630 //ultralightC diff --git a/uart/uart_win32.c b/uart/uart_win32.c index d8e41cfa4..abc8a78ca 100644 --- a/uart/uart_win32.c +++ b/uart/uart_win32.c @@ -61,6 +61,9 @@ serial_port uart_open(const char* pcPortName) { char acPortName[255]; serial_port_windows* sp = malloc(sizeof(serial_port_windows)); + if (sp == 0) + return INVALID_SERIAL_PORT; + // Copy the input "com?" to "\\.\COM?" format sprintf(acPortName,"\\\\.\\%s", pcPortName); upcase(acPortName); @@ -86,6 +89,7 @@ serial_port uart_open(const char* pcPortName) { return INVALID_SERIAL_PORT; } // all zero's configure: no timeout for read/write used. + // took settings from libnfc/buses/uart.c sp->ct.ReadIntervalTimeout = 0;//1; sp->ct.ReadTotalTimeoutMultiplier = 0;//1; sp->ct.ReadTotalTimeoutConstant = 30; @@ -107,38 +111,65 @@ serial_port uart_open(const char* pcPortName) { } void uart_close(const serial_port sp) { - if (!sp) return; - if (sp == INVALID_SERIAL_PORT) return; - if (sp == CLAIMED_SERIAL_PORT) return; - if (((serial_port_windows*)sp)->hPort != NULL ) + if (!sp) return; + if (sp == INVALID_SERIAL_PORT) return; + if (sp == CLAIMED_SERIAL_PORT) return; + if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE ) CloseHandle(((serial_port_windows*)sp)->hPort); free(sp); } bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) { - ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL); - return (*pszRxLen != 0); + //ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL); + //return (*pszRxLen != 0); + + DWORD dwBytesToGet = (DWORD)pszMaxRxLen; + DWORD dwBytesReceived = 0; + DWORD dwTotalBytesReceived = 0; + BOOL res; + + do { + res = ReadFile(((serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, dwBytesToGet, &dwBytesReceived, NULL); + + dwTotalBytesReceived += dwBytesReceived; + + if (!res) + return false; + else if (dwBytesReceived == 0) + return false; + + if (((DWORD)pszMaxRxLen) > dwTotalBytesReceived) + dwBytesToGet -= dwBytesReceived; + + } while (((DWORD)pszMaxRxLen) > dwTotalBytesReceived); + + return (dwTotalBytesReceived == (DWORD) pszMaxRxLen); } bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { - DWORD dwTxLen = 0; - return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL); - return (dwTxLen != 0); + DWORD dwTxLen = 0; + if ( !WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL) ) { + return false; + } + return (dwTxLen != 0); } bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { - serial_port_windows* spw; - spw = (serial_port_windows*)sp; - spw->dcb.BaudRate = uiPortSpeed; - return SetCommState(spw->hPort, &spw->dcb); + serial_port_windows* spw; + spw = (serial_port_windows*)sp; + spw->dcb.BaudRate = uiPortSpeed; + bool result = SetCommState(spw->hPort, &spw->dcb); + + PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR); + return result; } uint32_t uart_get_speed(const serial_port sp) { - const serial_port_windows* spw = (serial_port_windows*)sp; - if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) { - return spw->dcb.BaudRate; - } - return 0; + const serial_port_windows* spw = (serial_port_windows*)sp; + if (!GetCommState(spw->hPort, (serial_port) & spw->dcb)) + return spw->dcb.BaudRate; + + return 0; } -#endif +#endif \ No newline at end of file