void SendVersion(void) {
+ LED_A_ON();
set_hw_capabilities();
char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
uint32_t compressed_data_section_size = common_area.arg1;
cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, hw_capabilities, VersionString, strlen(VersionString) + 1);
+ LED_A_OFF();
}
// measure the USB Speed by sending SpeedTestBufferSize bytes to client and measuring the elapsed time.
uint32_t start_time = end_time = GetTickCount();
uint32_t bytes_transferred = 0;
- LED_B_ON();
- while(end_time < start_time + USB_SPEED_TEST_MIN_TIME) {
+ while (end_time < start_time + USB_SPEED_TEST_MIN_TIME) {
cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, 0, USB_CMD_DATA_SIZE, 0, test_data, USB_CMD_DATA_SIZE);
end_time = GetTickCount();
bytes_transferred += USB_CMD_DATA_SIZE;
}
- LED_B_OFF();
Dbprintf(" Time elapsed: %dms", end_time - start_time);
Dbprintf(" Bytes transferred: %d", bytes_transferred);
* Prints runtime information about the PM3.
**/
void SendStatus(void) {
+ LED_A_ON();
BigBuf_print_status();
Fpga_print_status();
#ifdef WITH_SMARTCARD
Dbprintf(" ToSendMax..........%d", ToSendMax);
Dbprintf(" ToSendBit..........%d", ToSendBit);
- cmd_send(CMD_ACK,1,0,0,0,0);
+ cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
+ LED_A_OFF();
}
#if defined(WITH_ISO14443a_StandAlone) || defined(WITH_LF_StandAlone)
break;
case CMD_FPGA_MAJOR_MODE_OFF: // ## FPGA Control
+ LED_A_ON();
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
SpinDelay(200);
LED_D_OFF(); // LED D indicates field ON or OFF
+ LED_A_OFF();
break;
case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
#include <pthread.h>
#include <inttypes.h>
-#if defined(__linux__) && !defined(NO_UNLINK)
-#include <unistd.h> // for unlink()
-#endif
#include "uart.h"
#include "ui.h"
#include "common.h"
void SendCommand(UsbCommand *c) {
#ifdef COMMS_DEBUG
- printf("Sending %04x cmd\n", c->cmd);
+ printf("Sending %04" PRIx64 " cmd\n", c->cmd);
#endif
if (offline) {
* A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
* operation. Right now we'll just have to live with this.
*/
-void clearCommandBuffer()
-{
+void clearCommandBuffer() {
//This is a very simple operation
pthread_mutex_lock(&rxBufferMutex);
cmd_tail = cmd_head;
* @brief storeCommand stores a USB command in a circular buffer
* @param UC
*/
-static void storeCommand(UsbCommand *command)
-{
+static void storeCommand(UsbCommand *command) {
pthread_mutex_lock(&rxBufferMutex);
- if( (cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
- {
+ if ((cmd_head + 1) % CMD_BUFFER_SIZE == cmd_tail) {
// If these two are equal, we're about to overwrite in the
// circular buffer.
PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
UsbCommand* destination = &rxBuffer[cmd_head];
memcpy(destination, command, sizeof(UsbCommand));
- cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
+ cmd_head = (cmd_head + 1) % CMD_BUFFER_SIZE; //increment head and wrap
pthread_mutex_unlock(&rxBufferMutex);
}
* @param response location to write command
* @return 1 if response was returned, 0 if nothing has been received
*/
-static int getCommand(UsbCommand* response)
-{
+static int getCommand(UsbCommand* response) {
pthread_mutex_lock(&rxBufferMutex);
- //If head == tail, there's nothing to read, or if we just got initialized
- if (cmd_head == cmd_tail){
+ // If head == tail, there's nothing to read
+ if (cmd_head == cmd_tail) {
pthread_mutex_unlock(&rxBufferMutex);
return 0;
}
- //Pick out the next unread command
+ // Pick out the next unread command
UsbCommand* last_unread = &rxBuffer[cmd_tail];
memcpy(response, last_unread, sizeof(UsbCommand));
- //Increment tail - this is a circular buffer, so modulo buffer size
+ // Increment tail - this is a circular buffer, so modulo buffer size
cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
pthread_mutex_unlock(&rxBufferMutex);
// Entry point into our code: called whenever we received a packet over USB.
// Handle debug commands directly, store all other commands in circular buffer.
//----------------------------------------------------------------------------------
-static void UsbCommandReceived(UsbCommand *UC)
-{
- switch(UC->cmd) {
+static void UsbCommandReceived(UsbCommand *UC) {
+ switch (UC->cmd) {
// First check if we are handling a debug message
case CMD_DEBUG_PRINT_STRING: {
char s[USB_CMD_DATA_SIZE+1];
memset(s, 0x00, sizeof(s));
- size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
- memcpy(s,UC->d.asBytes,len);
+ size_t len = MIN(UC->arg[0], USB_CMD_DATA_SIZE);
+ memcpy(s, UC->d.asBytes,len);
PrintAndLog("#db# %s", s);
return;
} break;
while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) {
#ifdef COMMS_DEBUG
if (bytes_read != len - *received_len) {
- printf("uart_receive() returned true but not enough bytes could be received. received: %d, wanted to receive: %d, already received before: %d\n",
+ printf("uart_receive() returned true but not enough bytes could be received. received: %zd, wanted to receive: %zd, already received before: %zd\n",
bytes_read, len - *received_len, *received_len);
}
#endif
if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
prx += rxlen;
if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size
- // PrintAndLog("received new style response %04" PRIx16 ", datalen = %d, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32 "\n",
- // response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
+#ifdef COMMS_DEBUG
+ PrintAndLog("received new style response %04" PRIx16 ", datalen = %zd, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32,
+ response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
+#endif
bytes_to_read = response->datalen;
if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
UsbCommand resp;
}
}
} else { // old style response uses same data structure as commands. Fixed size.
- // PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64 "\n", command->cmd, command->arg[0]);
+#ifdef COMMS_DEBUG
+ PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64, command->cmd, command->arg[0]);
+#endif
bytes_to_read = sizeof(UsbCommand) - bytes_to_read;
if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
UsbCommandReceived(command);
* @param show_warning display message after 2 seconds
* @return true if command was returned, otherwise false
*/
-bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning)
-{
+bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) {
UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
SendCommand(&c);
}
int bytes_completed = 0;
- while(true) {
+ while (true) {
if (getCommand(response)) {
if (response->cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
int copy_bytes = MIN(bytes - bytes_completed, response->arg[1]);
}
-bool GetFromFpgaRAM(uint8_t *dest, int bytes)
-{
+bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
SendCommand(&c);
int bytes_completed = 0;
bool show_warning = true;
- while(true) {
+ while (true) {
if (getCommand(&response)) {
if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
msleep(1000);
printf(".");
fflush(stdout);
- } while(++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
+ } while (++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
printf("\n");
}
uart_close(sp);
}
-#if defined(__linux__) && !defined(NO_UNLINK)
- // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
- //
- // This may be disabled at compile-time with -DNO_UNLINK (used for a JNI-based serial port on Android).
- if (serial_port_name) {
- unlink(serial_port_name);
- }
-#endif
-
// Clean up our state
sp = NULL;
serial_port_name = NULL;
PrintAndLog("You can cancel this operation by pressing the pm3 button");
show_warning = false;
}
+ msleep(1);
}
return false;
}
return 0;
}
+
// Load an ELF file and prepare it for flashing
-int flash_load(flash_file_t *ctx, const char *name, bool can_write_bl)
-{
+int flash_load(flash_file_t *ctx, const char *name, bool can_write_bl) {
FILE *fd = NULL;
Elf32_Ehdr ehdr;
Elf32_Phdr *phdrs = NULL;
return -1;
}
+
// Get the state of the proxmark, backwards compatible
-static int get_proxmark_state(uint32_t *state)
-{
+static int get_proxmark_state(uint32_t *state) {
UsbCommand c = {0};
c.cmd = CMD_DEVICE_INFO;
SendCommand(&c);
return 0;
}
+
// Enter the bootloader to be able to start flashing
-static int enter_bootloader(char *serial_port_name)
-{
+static int enter_bootloader(char *serial_port_name) {
uint32_t state;
if (get_proxmark_state(&state) < 0)
}
if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
- fprintf(stderr,"Entering bootloader...\n");
+ fprintf(stderr, "Entering bootloader...\n");
UsbCommand c;
memset(&c, 0, sizeof (c));
msleep(100);
CloseProxmark();
+ msleep(1000); // wait for OS to detect device disconnect.
+
bool opened = OpenProxmark(serial_port_name, true, 120); // wait for 2 minutes
if (opened) {
fprintf(stderr," Found.\n");
return -1;
}
+
static int wait_for_ack(void)
{
UsbCommand ack;
return 0;
}
+
// Go into flashing mode
int flash_start_flashing(int enable_bl_writes,char *serial_port_name)
{
uint32_t state;
-
if (enter_bootloader(serial_port_name) < 0)
return -1;
/* 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.
+ *
+ * Returns TRUE if all data could be sent within 30ms.
*/
extern bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen);
*/
extern uint32_t uart_get_speed(const serial_port sp);
-#endif // _PM3_UART_H_
+#endif // PM3_UART_H__
#include <sys/socket.h>\r
#include <netinet/tcp.h>\r
#include <netdb.h>\r
+#include <sys/time.h>\r
+#include <errno.h>\r
\r
// Fix missing definition on OS X.\r
// Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7\r
\r
void uart_close(const serial_port sp) {\r
serial_port_unix* spu = (serial_port_unix*)sp;\r
- tcflush(spu->fd,TCIOFLUSH);\r
- tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));\r
+ tcflush(spu->fd, TCIOFLUSH);\r
+ tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));\r
struct flock fl;\r
fl.l_type = F_UNLCK;\r
fl.l_whence = SEEK_SET;\r
}\r
\r
// Try to retrieve the old (current) terminal info struct\r
- if(tcgetattr(sp->fd,&sp->tiOld) == -1) {\r
+ if (tcgetattr(sp->fd,&sp->tiOld) == -1) {\r
uart_close(sp);\r
return INVALID_SERIAL_PORT;\r
}\r
sp->tiNew = sp->tiOld;\r
\r
// Configure the serial port\r
- sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;\r
- sp->tiNew.c_iflag = IGNPAR;\r
- sp->tiNew.c_oflag = 0;\r
- sp->tiNew.c_lflag = 0;\r
+ sp->tiNew.c_cflag &= ~(CSIZE | PARENB);\r
+ sp->tiNew.c_cflag |= (CS8 | CLOCAL | CREAD);\r
+ sp->tiNew.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);\r
+ sp->tiNew.c_iflag |= IGNPAR;\r
+ sp->tiNew.c_oflag &= ~OPOST;\r
+ sp->tiNew.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);\r
\r
- // Block until n bytes are received\r
- sp->tiNew.c_cc[VMIN] = 0;\r
- // Block until a timer expires (n * 100 mSec.)\r
- sp->tiNew.c_cc[VTIME] = 0;\r
\r
// Try to set the new terminal info struct\r
- if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {\r
+ if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {\r
uart_close(sp);\r
return INVALID_SERIAL_PORT;\r
}\r
}\r
\r
\r
-bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {\r
- int byteCount;\r
- fd_set rfds;\r
- struct timeval tv;\r
+bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t szMaxRxLen, size_t* pszRxLen) {\r
\r
- // Reset the output count\r
*pszRxLen = 0;\r
\r
- do {\r
- // Reset file descriptor\r
- FD_ZERO(&rfds);\r
- FD_SET(((serial_port_unix*)sp)->fd,&rfds);\r
- tv = timeout;\r
- int res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);\r
+ if (szMaxRxLen == 0) return true;\r
\r
- // Read error\r
- if (res < 0) {\r
- return false;\r
- }\r
+ struct timeval t_current;\r
+ gettimeofday(&t_current, NULL);\r
+ struct timeval t_end;\r
+ timeradd(&t_current, &timeout, &t_end);\r
\r
- // Read time-out\r
- if (res == 0) {\r
- if (*pszRxLen == 0) {\r
- // Error, we received no data\r
- return false;\r
- } else {\r
- // We received some data, but nothing more is available\r
- return true;\r
- }\r
+ while (true) {\r
+ int res = read(((serial_port_unix*)sp)->fd, pbtRx, szMaxRxLen - *pszRxLen);\r
+ if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) return false;\r
+ if (res > 0) {\r
+ *pszRxLen += res;\r
+ pbtRx += res;\r
}\r
-\r
- // Retrieve the count of the incoming bytes\r
- res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);\r
+ if (*pszRxLen == szMaxRxLen) return true; // we could read all requested bytes in time\r
+ gettimeofday(&t_current, NULL);\r
+ if (timercmp(&t_current, &t_end, >)) return true; // timeout\r
+ // set next select timeout\r
+ struct timeval t_remains;\r
+ timersub(&t_end, &t_current, &t_remains);\r
+ // Set the file descriptor set\r
+ fd_set rfds;\r
+ FD_ZERO(&rfds);\r
+ FD_SET(((serial_port_unix*)sp)->fd, &rfds);\r
+ // wait for more bytes available\r
+ res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &t_remains);\r
if (res < 0) return false;\r
-\r
- // Cap the number of bytes, so we don't overrun the buffer\r
- if (pszMaxRxLen - (*pszRxLen) < byteCount) {\r
- byteCount = pszMaxRxLen - (*pszRxLen);\r
- }\r
-\r
- // There is something available, read the data\r
- res = read(((serial_port_unix*)sp)->fd, pbtRx+(*pszRxLen), byteCount);\r
-\r
- // Stop if the OS has some troubles reading the data\r
- if (res <= 0) return false;\r
-\r
- *pszRxLen += res;\r
-\r
- if (*pszRxLen == pszMaxRxLen) {\r
- // We have all the data we wanted.\r
- return true;\r
- }\r
-\r
- } while (byteCount);\r
-\r
- return true;\r
+ if (res == 0) return true; // timeout\r
+ }\r
+ return true; // should never come here\r
}\r
\r
\r
bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {\r
- size_t szPos = 0;\r
- fd_set rfds;\r
- struct timeval tv;\r
-\r
- while (szPos < szTxLen) {\r
- // Reset file descriptor\r
- FD_ZERO(&rfds);\r
- FD_SET(((serial_port_unix*)sp)->fd,&rfds);\r
- tv = timeout;\r
- int res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);\r
\r
- // Write error\r
- if (res < 0) {\r
- return false;\r
- }\r
+ if (szTxLen == 0) return true;\r
\r
- // Write time-out\r
- if (res == 0) {\r
- return false;\r
- }\r
+ size_t bytes_written = 0;\r
\r
- // Send away the bytes\r
- res = write(((serial_port_unix*)sp)->fd, pbtTx+szPos, szTxLen-szPos);\r
+ struct timeval t_current;\r
+ gettimeofday(&t_current, NULL);\r
+ struct timeval t_end;\r
+ timeradd(&t_current, &timeout, &t_end);\r
\r
- // Stop if the OS has some troubles sending the data\r
- if (res <= 0) return false;\r
-\r
- szPos += res;\r
+ while (true) {\r
+ int res = write(((serial_port_unix*)sp)->fd, pbtTx, szTxLen - bytes_written);\r
+ if (res < 0 && res != EAGAIN && res != EWOULDBLOCK) return false;\r
+ if (res > 0) {\r
+ pbtTx += res;\r
+ bytes_written += res;\r
+ }\r
+ if (bytes_written == szTxLen) return true; // we could write all bytes\r
+ gettimeofday(&t_current, NULL);\r
+ if (timercmp(&t_current, &t_end, >)) return false; // timeout\r
+ // set next select timeout\r
+ struct timeval t_remains;\r
+ timersub(&t_end, &t_current, &t_remains);\r
+ // Set the file descriptor set\r
+ fd_set wfds;\r
+ FD_ZERO(&wfds);\r
+ FD_SET(((serial_port_unix*)sp)->fd, &wfds);\r
+ // wait until more bytes can be written\r
+ res = select(((serial_port_unix*)sp)->fd+1, NULL, &wfds, NULL, &t_remains);\r
+ if (res < 0) return false; // error\r
+ if (res == 0) return false; // timeout\r
}\r
return true;\r
}\r