|
|
|
|
@@ -8,6 +8,7 @@
|
|
|
|
|
// Main command parser entry point
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
@@ -42,8 +43,10 @@ static int cmd_head;//Starts as 0
|
|
|
|
|
//Points to the position of the last unread command
|
|
|
|
|
static int cmd_tail;//Starts as 0
|
|
|
|
|
|
|
|
|
|
static command_t CommandTable[] =
|
|
|
|
|
{
|
|
|
|
|
// to lock cmdBuffer operations from different threads
|
|
|
|
|
static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
|
|
static command_t CommandTable[] = {
|
|
|
|
|
{"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
|
|
|
|
|
{"analyse", CmdAnalyse, 1, "{ Analyse bytes... }"},
|
|
|
|
|
{"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
|
|
|
|
|
@@ -57,25 +60,22 @@ static command_t CommandTable[] =
|
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
command_t* getTopLevelCommandTable()
|
|
|
|
|
{
|
|
|
|
|
return CommandTable;
|
|
|
|
|
}
|
|
|
|
|
int CmdHelp(const char *Cmd)
|
|
|
|
|
{
|
|
|
|
|
CmdsHelp(CommandTable);
|
|
|
|
|
return 0;
|
|
|
|
|
command_t* getTopLevelCommandTable() {
|
|
|
|
|
return CommandTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CmdQuit(const char *Cmd)
|
|
|
|
|
{
|
|
|
|
|
return 99;
|
|
|
|
|
int CmdHelp(const char *Cmd) {
|
|
|
|
|
CmdsHelp(CommandTable);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CmdRev(const char *Cmd)
|
|
|
|
|
{
|
|
|
|
|
CmdCrc(Cmd);
|
|
|
|
|
return 0;
|
|
|
|
|
int CmdQuit(const char *Cmd) {
|
|
|
|
|
return 99;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CmdRev(const char *Cmd) {
|
|
|
|
|
CmdCrc(Cmd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @brief This method should be called when sending a new command to the pm3. In case any old
|
|
|
|
|
@@ -83,18 +83,20 @@ int CmdRev(const char *Cmd)
|
|
|
|
|
* 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(&cmdBufferMutex);
|
|
|
|
|
cmd_tail = cmd_head;
|
|
|
|
|
pthread_mutex_unlock(&cmdBufferMutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief storeCommand stores a USB command in a circular buffer
|
|
|
|
|
* @param UC
|
|
|
|
|
*/
|
|
|
|
|
void storeCommand(UsbCommand *command)
|
|
|
|
|
{
|
|
|
|
|
void storeCommand(UsbCommand *command) {
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&cmdBufferMutex);
|
|
|
|
|
if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
|
|
|
|
|
{
|
|
|
|
|
//If these two are equal, we're about to overwrite in the
|
|
|
|
|
@@ -106,18 +108,21 @@ void storeCommand(UsbCommand *command)
|
|
|
|
|
memcpy(destination, command, sizeof(UsbCommand));
|
|
|
|
|
|
|
|
|
|
cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&cmdBufferMutex);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @brief getCommand gets a command from an internal circular buffer.
|
|
|
|
|
* @param response location to write command
|
|
|
|
|
* @return 1 if response was returned, 0 if nothing has been received
|
|
|
|
|
*/
|
|
|
|
|
int getCommand(UsbCommand* response)
|
|
|
|
|
{
|
|
|
|
|
int getCommand(UsbCommand* response) {
|
|
|
|
|
pthread_mutex_lock(&cmdBufferMutex);
|
|
|
|
|
//If head == tail, there's nothing to read, or if we just got initialized
|
|
|
|
|
if(cmd_head == cmd_tail) return 0;
|
|
|
|
|
|
|
|
|
|
if(cmd_head == cmd_tail) {
|
|
|
|
|
pthread_mutex_unlock(&cmdBufferMutex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Pick out the next unread command
|
|
|
|
|
UsbCommand* last_unread = &cmdBuffer[cmd_tail];
|
|
|
|
|
memcpy(response, last_unread, sizeof(UsbCommand));
|
|
|
|
|
@@ -125,6 +130,7 @@ int getCommand(UsbCommand* response)
|
|
|
|
|
//Increment tail - this is a circular buffer, so modulo buffer size
|
|
|
|
|
cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&cmdBufferMutex);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -137,32 +143,42 @@ int getCommand(UsbCommand* response)
|
|
|
|
|
* @param ms_timeout
|
|
|
|
|
* @return true if command was returned, otherwise false
|
|
|
|
|
*/
|
|
|
|
|
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
|
|
|
|
|
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning)) {
|
|
|
|
|
|
|
|
|
|
UsbCommand resp;
|
|
|
|
|
|
|
|
|
|
if (response == NULL)
|
|
|
|
|
response = &resp;
|
|
|
|
|
|
|
|
|
|
uint64_t start_time = msclock();
|
|
|
|
|
|
|
|
|
|
// Wait until the command is received
|
|
|
|
|
for ( size_t dm_seconds = 0; dm_seconds < ms_timeout/10; dm_seconds++ ) {
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
|
|
while( getCommand(response) ) {
|
|
|
|
|
if(response->cmd == cmd)
|
|
|
|
|
while ( getCommand(response) ) {
|
|
|
|
|
if (response->cmd == cmd)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msleep(10); // XXX ugh
|
|
|
|
|
if (dm_seconds == 250) { // 2.5 seconds elapsed
|
|
|
|
|
if (msclock() - start_time > ms_timeout) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msclock() - start_time > 2000 && show_warning) {
|
|
|
|
|
PrintAndLog("Waiting for a response from the proxmark...");
|
|
|
|
|
PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
|
|
|
|
|
return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
|
|
|
|
|
return WaitForResponseTimeout(cmd, response, -1);
|
|
|
|
|
return WaitForResponseTimeoutW(cmd, response, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
@@ -177,18 +193,17 @@ int CommandReceived(char *Cmd) {
|
|
|
|
|
// Entry point into our code: called whenever we received a packet over USB
|
|
|
|
|
// that we weren't necessarily expecting, for example a debug print.
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void UsbCommandReceived(UsbCommand *UC)
|
|
|
|
|
{
|
|
|
|
|
switch(UC->cmd) {
|
|
|
|
|
void UsbCommandReceived(UsbCommand *c) {
|
|
|
|
|
switch(c->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(c->arg[0],USB_CMD_DATA_SIZE);
|
|
|
|
|
memcpy(s, c->d.asBytes, len);
|
|
|
|
|
|
|
|
|
|
// test
|
|
|
|
|
if ( UC->arg[1] == CMD_MEASURE_ANTENNA_TUNING_HF) {
|
|
|
|
|
if ( c->arg[1] == CMD_MEASURE_ANTENNA_TUNING_HF) {
|
|
|
|
|
printf("\r#db# %s", s);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
@@ -199,7 +214,7 @@ void UsbCommandReceived(UsbCommand *UC)
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case CMD_DEBUG_PRINT_INTEGERS: {
|
|
|
|
|
PrintAndLog("#db# %08x, %08x, %08x", UC->arg[0], UC->arg[1], UC->arg[2]);
|
|
|
|
|
PrintAndLog("#db# %08x, %08x, %08x", c->arg[0], c->arg[1], c->arg[2]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K:
|
|
|
|
|
@@ -208,17 +223,16 @@ void UsbCommandReceived(UsbCommand *UC)
|
|
|
|
|
// arg0 = offset in transfer. Startindex of this chunk
|
|
|
|
|
// arg1 = length bytes to transfer
|
|
|
|
|
// arg2 = bigbuff tracelength (?)
|
|
|
|
|
uint32_t offset = UC->arg[0];
|
|
|
|
|
uint32_t len = UC->arg[1];
|
|
|
|
|
//uint32_t tracelen = UC->arg[2];
|
|
|
|
|
memcpy( sample_buf + offset, UC->d.asBytes, len);
|
|
|
|
|
//printf("ICE:: Download from device. chunk %" PRIu32 " | size %" PRIu32 " | tracelen:%" PRIu32 " \n", offset, len, UC->arg[2]);
|
|
|
|
|
uint32_t offset = c->arg[0];
|
|
|
|
|
uint32_t len = c->arg[1];
|
|
|
|
|
//uint32_t tracelen = c->arg[2];
|
|
|
|
|
memcpy( sample_buf + offset, c->d.asBytes, len);
|
|
|
|
|
//printf("ICE:: Download from device. chunk %" PRIu32 " | size %" PRIu32 " | tracelen:%" PRIu32 " \n", offset, len, c->arg[2]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
storeCommand(UC);
|
|
|
|
|
storeCommand(c);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|