mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-07-16 02:03:00 -07:00
Always enable fast response mode (was enabled for flasher only)
* ensure that CMD_ACK is used exclusively for the very last response of each PM3 operation. All Dbprintf() must be before. * always switch off field before exiting * append null packet for USB transfers % 64 bytes * reformatting and whitespace fixes
This commit is contained in:
parent
b8ed9975e5
commit
929b61c670
17 changed files with 501 additions and 474 deletions
25
uart/uart.h
25
uart/uart.h
|
@ -29,24 +29,21 @@
|
|||
* @file uart.h
|
||||
*/
|
||||
|
||||
#ifndef _PM3_UART_H_
|
||||
#define _PM3_UART_H_
|
||||
#ifndef PM3_UART_H__
|
||||
#define PM3_UART_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Used to substitute for an example serial port path on each platform.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
#define SERIAL_PORT_H "com3"
|
||||
#define SERIAL_PORT_H "com3"
|
||||
#elif __APPLE__
|
||||
#define SERIAL_PORT_H "/dev/tty.usbmodem*"
|
||||
#define SERIAL_PORT_H "/dev/tty.usbmodem*"
|
||||
#else
|
||||
#define SERIAL_PORT_H "/dev/ttyACM0"
|
||||
#define SERIAL_PORT_H "/dev/ttyACM0"
|
||||
#endif
|
||||
|
||||
/* serial_port is declared as a void*, which you should cast to whatever type
|
||||
|
@ -69,11 +66,11 @@ typedef void* serial_port;
|
|||
*
|
||||
* On errors, this method returns INVALID_SERIAL_PORT or CLAIMED_SERIAL_PORT.
|
||||
*/
|
||||
serial_port uart_open(const char* pcPortName);
|
||||
extern serial_port uart_open(const char* pcPortName);
|
||||
|
||||
/* Closes the given port.
|
||||
*/
|
||||
void uart_close(const serial_port sp);
|
||||
extern void uart_close(const serial_port sp);
|
||||
|
||||
/* Reads from the given serial port for up to 30ms.
|
||||
* pbtRx: A pointer to a buffer for the returned data to be written to.
|
||||
|
@ -84,15 +81,15 @@ void uart_close(const serial_port sp);
|
|||
*
|
||||
* Returns FALSE if there was an error reading from the device. Note that a
|
||||
* partial read may have completed into the buffer by the corresponding
|
||||
* implementation, so pszRxLen should be checked to see if any data was written.
|
||||
* implementation, so pszRxLen should be checked to see if any data was written.
|
||||
*/
|
||||
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen);
|
||||
extern bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen);
|
||||
|
||||
/* Sends a buffer to a given serial port.
|
||||
* pbtTx: A pointer to a buffer containing the data to send.
|
||||
* szTxLen: The amount of data to be sent.
|
||||
*/
|
||||
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
|
||||
extern bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
|
||||
|
||||
/* Sets the current speed of the serial port, in baud.
|
||||
*/
|
||||
|
@ -100,7 +97,7 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed);
|
|||
|
||||
/* Gets the current speed of the serial port, in baud.
|
||||
*/
|
||||
uint32_t uart_get_speed(const serial_port sp);
|
||||
extern uint32_t uart_get_speed(const serial_port sp);
|
||||
|
||||
#endif // _PM3_UART_H_
|
||||
|
||||
|
|
|
@ -38,97 +38,109 @@
|
|||
|
||||
#include "uart.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
// The windows serial port implementation
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
typedef struct {
|
||||
HANDLE hPort; // Serial port handle
|
||||
DCB dcb; // Device control settings
|
||||
COMMTIMEOUTS ct; // Serial port time-out configuration
|
||||
HANDLE hPort; // Serial port handle
|
||||
DCB dcb; // Device control settings
|
||||
COMMTIMEOUTS ct; // Serial port time-out configuration
|
||||
} serial_port_windows;
|
||||
|
||||
|
||||
void upcase(char *p) {
|
||||
while(*p != '\0') {
|
||||
if(*p >= 97 && *p <= 122) {
|
||||
*p -= 32;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
while(*p != '\0') {
|
||||
if(*p >= 97 && *p <= 122) {
|
||||
*p -= 32;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
}
|
||||
|
||||
serial_port uart_open(const char* pcPortName) {
|
||||
char acPortName[255];
|
||||
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
|
||||
|
||||
// Copy the input "com?" to "\\.\COM?" format
|
||||
sprintf(acPortName,"\\\\.\\%s",pcPortName);
|
||||
upcase(acPortName);
|
||||
|
||||
// Try to open the serial port
|
||||
sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
|
||||
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Prepare the device control
|
||||
memset(&sp->dcb, 0, sizeof(DCB));
|
||||
sp->dcb.DCBlength = sizeof(DCB);
|
||||
if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Update the active serial port
|
||||
if(!SetCommState(sp->hPort,&sp->dcb)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
sp->ct.ReadIntervalTimeout = 0;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 30;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 0;
|
||||
sp->ct.WriteTotalTimeoutConstant = 30;
|
||||
|
||||
if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
void uart_close(const serial_port sp) {
|
||||
CloseHandle(((serial_port_windows*)sp)->hPort);
|
||||
free(sp);
|
||||
CloseHandle(((serial_port_windows*)sp)->hPort);
|
||||
free(sp);
|
||||
}
|
||||
|
||||
|
||||
serial_port uart_open(const char* pcPortName) {
|
||||
char acPortName[255];
|
||||
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
|
||||
|
||||
// Copy the input "com?" to "\\.\COM?" format
|
||||
sprintf(acPortName,"\\\\.\\%s",pcPortName);
|
||||
upcase(acPortName);
|
||||
|
||||
// Try to open the serial port
|
||||
sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Prepare the device control
|
||||
memset(&sp->dcb, 0, sizeof(DCB));
|
||||
sp->dcb.DCBlength = sizeof(DCB);
|
||||
if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
// Update the active serial port
|
||||
if (!SetCommState(sp->hPort,&sp->dcb)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
sp->ct.ReadIntervalTimeout = 0;
|
||||
sp->ct.ReadTotalTimeoutMultiplier = 0;
|
||||
sp->ct.ReadTotalTimeoutConstant = 30;
|
||||
sp->ct.WriteTotalTimeoutMultiplier = 0;
|
||||
sp->ct.WriteTotalTimeoutConstant = 30;
|
||||
|
||||
if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
|
||||
uart_close(sp);
|
||||
return INVALID_SERIAL_PORT;
|
||||
}
|
||||
|
||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||
|
||||
return sp;
|
||||
}
|
||||
|
||||
|
||||
bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
|
||||
return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
|
||||
return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
|
||||
}
|
||||
|
||||
|
||||
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
||||
DWORD dwTxLen = 0;
|
||||
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
|
||||
DWORD dwTxLen = 0;
|
||||
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
return SetCommState(spw->hPort, &spw->dcb);
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue