mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-22 14:13:42 -07:00
FIX: Mark uart parameters as pointers, and initialise to NULL.
Internally, the `uart_*` implementations treat their parameter as a pointer. While the C compiler technically doesn't have a way to enforce that you're not modifying memory referred to by a `const` pointer, there's not a good reason to prohibit an implementation of `uart` from doing so, if it makes sense for that platform or environment. PM3 also fairly consistently casts the value to a non-const pointer anyway. This also explicitly sets `sp` to NULL, which should cause an uninitialized use of `sp` to dereference a null pointer (generally crashing with a segfault, as the program tries to reference unmapped memory), rather than reading some nearby program memory (and have undefined behaviour). That will make it very obvious in a debugger, when something has gone wrong. For example: ``` * thread #1: tid = 32727, 0x0000555555556e1b flasher`uart_send(sp=0x0000000000000000, pbtTx="", szTxLen=544) + 123 at uart_posix.c:204, name = 'flasher', stop reason = signal SIGSEGV: invalid address (fault address: 0x0) frame #0: 0x0000555555556e1b flasher`uart_send(sp=0x0000000000000000, pbtTx="", szTxLen=544) + 123 at uart_posix.c:204 ``` This should address a number of compiler warnings on uart files.
This commit is contained in:
parent
e8924be8ba
commit
11901a9118
4 changed files with 31 additions and 31 deletions
|
@ -20,7 +20,7 @@
|
||||||
// Declare globals.
|
// Declare globals.
|
||||||
|
|
||||||
// Serial port that we are communicating with the PM3 on.
|
// Serial port that we are communicating with the PM3 on.
|
||||||
static serial_port sp;
|
static serial_port* sp = NULL;
|
||||||
|
|
||||||
// If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
|
// If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
|
||||||
static bool offline;
|
static bool offline;
|
||||||
|
|
12
uart/uart.h
12
uart/uart.h
|
@ -69,11 +69,11 @@ typedef void* serial_port;
|
||||||
*
|
*
|
||||||
* On errors, this method returns INVALID_SERIAL_PORT or CLAIMED_SERIAL_PORT.
|
* On errors, this method returns INVALID_SERIAL_PORT or CLAIMED_SERIAL_PORT.
|
||||||
*/
|
*/
|
||||||
serial_port uart_open(const char* pcPortName);
|
serial_port* uart_open(const char* pcPortName);
|
||||||
|
|
||||||
/* Closes the given port.
|
/* Closes the given port.
|
||||||
*/
|
*/
|
||||||
void uart_close(const serial_port sp);
|
void uart_close(serial_port* sp);
|
||||||
|
|
||||||
/* Reads from the given serial port for up to 30ms.
|
/* Reads from the given serial port for up to 30ms.
|
||||||
* pbtRx: A pointer to a buffer for the returned data to be written to.
|
* pbtRx: A pointer to a buffer for the returned data to be written to.
|
||||||
|
@ -86,21 +86,21 @@ void uart_close(const serial_port sp);
|
||||||
* partial read may have completed into the buffer by the corresponding
|
* 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);
|
bool uart_receive(serial_port* sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen);
|
||||||
|
|
||||||
/* Sends a buffer to a given serial port.
|
/* Sends a buffer to a given serial port.
|
||||||
* pbtTx: A pointer to a buffer containing the data to send.
|
* pbtTx: A pointer to a buffer containing the data to send.
|
||||||
* szTxLen: The amount of data to be sent.
|
* szTxLen: The amount of data to be sent.
|
||||||
*/
|
*/
|
||||||
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
|
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen);
|
||||||
|
|
||||||
/* Sets the current speed of the serial port, in baud.
|
/* Sets the current speed of the serial port, in baud.
|
||||||
*/
|
*/
|
||||||
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed);
|
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed);
|
||||||
|
|
||||||
/* Gets the current speed of the serial port, in baud.
|
/* Gets the current speed of the serial port, in baud.
|
||||||
*/
|
*/
|
||||||
uint32_t uart_get_speed(const serial_port sp);
|
uint32_t uart_get_speed(serial_port* sp);
|
||||||
|
|
||||||
#endif // _PM3_UART_H_
|
#endif // _PM3_UART_H_
|
||||||
|
|
||||||
|
|
|
@ -59,14 +59,14 @@ const struct timeval timeout = {
|
||||||
.tv_usec = 30000 // 30000 micro seconds
|
.tv_usec = 30000 // 30000 micro seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
serial_port uart_open(const char* pcPortName)
|
serial_port* uart_open(const char* pcPortName)
|
||||||
{
|
{
|
||||||
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
|
serial_port_unix* sp = malloc(sizeof(serial_port_unix));
|
||||||
if (sp == 0) return INVALID_SERIAL_PORT;
|
if (sp == 0) return INVALID_SERIAL_PORT;
|
||||||
|
|
||||||
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
|
||||||
if(sp->fd == -1) {
|
if(sp->fd == -1) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ serial_port uart_open(const char* pcPortName)
|
||||||
|
|
||||||
// Try to retrieve the old (current) terminal info struct
|
// Try to retrieve the old (current) terminal info struct
|
||||||
if(tcgetattr(sp->fd,&sp->tiOld) == -1) {
|
if(tcgetattr(sp->fd,&sp->tiOld) == -1) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,17 +108,17 @@ serial_port uart_open(const char* pcPortName)
|
||||||
|
|
||||||
// Try to set the new terminal info struct
|
// Try to set the new terminal info struct
|
||||||
if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {
|
if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush all lingering data that may exist
|
// Flush all lingering data that may exist
|
||||||
tcflush(sp->fd, TCIOFLUSH);
|
tcflush(sp->fd, TCIOFLUSH);
|
||||||
|
|
||||||
return sp;
|
return (serial_port*)sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_close(const serial_port sp) {
|
void uart_close(serial_port* sp) {
|
||||||
serial_port_unix* spu = (serial_port_unix*)sp;
|
serial_port_unix* spu = (serial_port_unix*)sp;
|
||||||
tcflush(spu->fd,TCIOFLUSH);
|
tcflush(spu->fd,TCIOFLUSH);
|
||||||
tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
|
tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
|
||||||
|
@ -133,7 +133,7 @@ void uart_close(const serial_port sp) {
|
||||||
free(sp);
|
free(sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
|
bool uart_receive(serial_port* sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
|
||||||
int res;
|
int res;
|
||||||
int byteCount;
|
int byteCount;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
|
@ -192,7 +192,7 @@ bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
||||||
int32_t res;
|
int32_t res;
|
||||||
size_t szPos = 0;
|
size_t szPos = 0;
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
|
@ -226,8 +226,8 @@ bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed) {
|
||||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
serial_port_unix* spu = (serial_port_unix*)sp;
|
||||||
speed_t stPortSpeed;
|
speed_t stPortSpeed;
|
||||||
switch (uiPortSpeed) {
|
switch (uiPortSpeed) {
|
||||||
case 0: stPortSpeed = B0; break;
|
case 0: stPortSpeed = B0; break;
|
||||||
|
@ -270,10 +270,10 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
||||||
return (tcsetattr(spu->fd,TCSANOW,&ti) != -1);
|
return (tcsetattr(spu->fd,TCSANOW,&ti) != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t uart_get_speed(const serial_port sp) {
|
uint32_t uart_get_speed(serial_port* sp) {
|
||||||
struct termios ti;
|
struct termios ti;
|
||||||
uint32_t uiPortSpeed;
|
uint32_t uiPortSpeed;
|
||||||
const serial_port_unix* spu = (serial_port_unix*)sp;
|
serial_port_unix* spu = (serial_port_unix*)sp;
|
||||||
if (tcgetattr(spu->fd,&ti) == -1) return 0;
|
if (tcgetattr(spu->fd,&ti) == -1) return 0;
|
||||||
// Set port speed (Input)
|
// Set port speed (Input)
|
||||||
speed_t stPortSpeed = cfgetispeed(&ti);
|
speed_t stPortSpeed = cfgetispeed(&ti);
|
||||||
|
|
|
@ -57,7 +57,7 @@ void upcase(char *p) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serial_port uart_open(const char* pcPortName) {
|
serial_port* uart_open(const char* pcPortName) {
|
||||||
char acPortName[255];
|
char acPortName[255];
|
||||||
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
|
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ serial_port uart_open(const char* pcPortName) {
|
||||||
// Try to open the serial port
|
// Try to open the serial port
|
||||||
sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
|
sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
|
||||||
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
if (sp->hPort == INVALID_HANDLE_VALUE) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,13 +76,13 @@ serial_port uart_open(const char* pcPortName) {
|
||||||
memset(&sp->dcb, 0, sizeof(DCB));
|
memset(&sp->dcb, 0, sizeof(DCB));
|
||||||
sp->dcb.DCBlength = sizeof(DCB);
|
sp->dcb.DCBlength = sizeof(DCB);
|
||||||
if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
|
if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the active serial port
|
// Update the active serial port
|
||||||
if(!SetCommState(sp->hPort,&sp->dcb)) {
|
if(!SetCommState(sp->hPort,&sp->dcb)) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,38 +93,38 @@ serial_port uart_open(const char* pcPortName) {
|
||||||
sp->ct.WriteTotalTimeoutConstant = 30;
|
sp->ct.WriteTotalTimeoutConstant = 30;
|
||||||
|
|
||||||
if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
|
if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
|
||||||
uart_close(sp);
|
uart_close((serial_port*)sp);
|
||||||
return INVALID_SERIAL_PORT;
|
return INVALID_SERIAL_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
|
||||||
|
|
||||||
return sp;
|
return (serial_port*)sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_close(const serial_port sp) {
|
void uart_close(serial_port* sp) {
|
||||||
CloseHandle(((serial_port_windows*)sp)->hPort);
|
CloseHandle(((serial_port_windows*)sp)->hPort);
|
||||||
free(sp);
|
free(sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool uart_receive(const serial_port sp, uint8_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
|
bool uart_receive(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) {
|
bool uart_send(serial_port* sp, const uint8_t* pbtTx, const size_t szTxLen) {
|
||||||
DWORD dwTxLen = 0;
|
DWORD dwTxLen = 0;
|
||||||
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
|
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
|
bool uart_set_speed(serial_port* sp, const uint32_t uiPortSpeed) {
|
||||||
serial_port_windows* spw;
|
serial_port_windows* spw;
|
||||||
spw = (serial_port_windows*)sp;
|
spw = (serial_port_windows*)sp;
|
||||||
spw->dcb.BaudRate = uiPortSpeed;
|
spw->dcb.BaudRate = uiPortSpeed;
|
||||||
return SetCommState(spw->hPort, &spw->dcb);
|
return SetCommState(spw->hPort, &spw->dcb);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t uart_get_speed(const serial_port sp) {
|
uint32_t uart_get_speed(serial_port* sp) {
|
||||||
const serial_port_windows* spw = (serial_port_windows*)sp;
|
serial_port_windows* spw = (serial_port_windows*)sp;
|
||||||
if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
|
if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
|
||||||
return spw->dcb.BaudRate;
|
return spw->dcb.BaudRate;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue