2010-02-21 08:47:22 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Main command parser entry point
//-----------------------------------------------------------------------------
2018-03-19 01:00:41 +08:00
# include "cmdmain.h"
2013-06-27 05:13:02 +08:00
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
static int CmdHelp ( const char * Cmd ) ;
static int CmdQuit ( const char * Cmd ) ;
2015-06-07 17:35:49 +08:00
static int CmdRev ( const char * Cmd ) ;
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
2013-06-07 17:30:32 +08:00
//For storing command that are received from the device
static UsbCommand cmdBuffer [ CMD_BUFFER_SIZE ] ;
2018-07-01 04:54:30 +08:00
2013-06-07 17:30:32 +08:00
//Points to the next empty position to write to
2018-07-01 04:54:30 +08:00
static int cmd_head = 0 ;
2013-06-07 17:30:32 +08:00
//Points to the position of the last unread command
2018-07-01 04:54:30 +08:00
static int cmd_tail = 0 ;
2013-06-07 17:30:32 +08:00
2017-10-20 22:32:42 +08:00
// to lock cmdBuffer operations from different threads
static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER ;
static command_t CommandTable [ ] = {
2015-05-16 21:36:40 +08:00
{ " help " , CmdHelp , 1 , " This help. Use '<command> help' for details of a particular command. " } ,
2018-07-01 04:54:30 +08:00
{ " analyse " , CmdAnalyse , 1 , " { Analyse utils... } " } ,
2015-05-16 21:36:40 +08:00
{ " data " , CmdData , 1 , " { Plot window / data buffer manipulation... } " } ,
{ " hf " , CmdHF , 1 , " { High Frequency commands... } " } ,
{ " hw " , CmdHW , 1 , " { Hardware commands... } " } ,
{ " lf " , CmdLF , 1 , " { Low Frequency commands... } " } ,
2018-07-01 04:54:30 +08:00
{ " reveng " , CmdRev , 1 , " { Crc calculations from the software reveng 1.51... } " } ,
2018-03-19 01:00:41 +08:00
{ " script " , CmdScript , 1 , " { Scripting commands } " } ,
{ " trace " , CmdTrace , 1 , " { Trace manipulation... } " } ,
2018-05-03 18:15:03 +08:00
# ifdef WITH_FLASH
{ " mem " , CmdFlashMem , 1 , " { RDV40, Flash Memory manipulation... } " } ,
# endif
2018-02-05 23:38:00 +08:00
{ " quit " , CmdQuit , 1 , " " } ,
2015-05-16 21:36:40 +08:00
{ " exit " , CmdQuit , 1 , " Exit program " } ,
{ NULL , NULL , 0 , NULL }
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
} ;
2017-10-20 22:32:42 +08:00
command_t * getTopLevelCommandTable ( ) {
return CommandTable ;
2014-03-27 05:35:25 +08:00
}
2017-10-20 22:32:42 +08:00
int CmdHelp ( const char * Cmd ) {
CmdsHelp ( CommandTable ) ;
return 0 ;
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
2018-07-01 04:54:30 +08:00
int CmdQuit ( const char * Cmd ) {
return 99 ;
}
int CmdRev ( const char * Cmd ) {
CmdCrc ( Cmd ) ;
return 0 ;
}
2018-05-02 14:11:29 +08:00
bool dl_it ( uint8_t * dest , uint32_t bytes , uint32_t start_index , UsbCommand * response , size_t ms_timeout , bool show_warning , uint32_t rec_cmd ) ;
2013-09-20 03:53:09 +08:00
/**
* @ brief This method should be called when sending a new command to the pm3 . In case any old
* responses from previous commands are stored in the buffer , a call to this method should clear them .
* 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 .
*/
2017-10-20 22:32:42 +08:00
void clearCommandBuffer ( ) {
2013-09-20 03:53:09 +08:00
//This is a very simple operation
2017-10-20 22:32:42 +08:00
pthread_mutex_lock ( & cmdBufferMutex ) ;
2013-09-20 03:53:09 +08:00
cmd_tail = cmd_head ;
2017-10-20 22:32:42 +08:00
pthread_mutex_unlock ( & cmdBufferMutex ) ;
2013-09-20 03:53:09 +08:00
}
/**
* @ brief storeCommand stores a USB command in a circular buffer
* @ param UC
*/
2017-10-20 22:32:42 +08:00
void storeCommand ( UsbCommand * command ) {
pthread_mutex_lock ( & cmdBufferMutex ) ;
2018-04-26 18:13:03 +08:00
if ( ( cmd_head + 1 ) % CMD_BUFFER_SIZE = = cmd_tail ) {
2013-09-20 03:53:09 +08:00
//If these two are equal, we're about to overwrite in the
// circular buffer.
2018-02-21 20:05:42 +08:00
PrintAndLogEx ( FAILED , " WARNING: Command buffer about to overwrite command! This needs to be fixed! " ) ;
2018-07-01 04:54:30 +08:00
fflush ( NULL ) ;
2013-09-20 03:53:09 +08:00
}
//Store the command at the 'head' location
UsbCommand * destination = & cmdBuffer [ cmd_head ] ;
memcpy ( destination , command , sizeof ( UsbCommand ) ) ;
2018-04-26 18:13:03 +08:00
//increment head and wrap
cmd_head = ( cmd_head + 1 ) % CMD_BUFFER_SIZE ;
2017-10-20 22:32:42 +08:00
pthread_mutex_unlock ( & cmdBufferMutex ) ;
2013-09-20 03:53:09 +08:00
}
/**
* @ 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
*/
2017-10-20 22:32:42 +08:00
int getCommand ( UsbCommand * response ) {
pthread_mutex_lock ( & cmdBufferMutex ) ;
2013-09-20 03:53:09 +08:00
//If head == tail, there's nothing to read, or if we just got initialized
2018-04-27 16:09:14 +08:00
if ( cmd_head = = cmd_tail ) {
2017-10-20 22:32:42 +08:00
pthread_mutex_unlock ( & cmdBufferMutex ) ;
return 0 ;
}
2013-09-20 03:53:09 +08:00
//Pick out the next unread command
UsbCommand * last_unread = & cmdBuffer [ cmd_tail ] ;
memcpy ( response , last_unread , sizeof ( UsbCommand ) ) ;
2016-02-10 20:22:48 +08:00
2013-09-20 03:53:09 +08:00
//Increment tail - this is a circular buffer, so modulo buffer size
cmd_tail = ( cmd_tail + 1 ) % CMD_BUFFER_SIZE ;
2017-10-20 22:32:42 +08:00
pthread_mutex_unlock ( & cmdBufferMutex ) ;
2013-09-20 03:53:09 +08:00
return 1 ;
}
2013-06-07 17:30:32 +08:00
/**
* Waits for a certain response type . This method waits for a maximum of
* ms_timeout milliseconds for a specified response command .
* @ brief WaitForResponseTimeout
2018-04-19 02:17:00 +08:00
* @ param cmd command to wait for , or CMD_UNKNOWN to take any command .
2013-06-07 17:30:32 +08:00
* @ param response struct to copy received command into .
2018-05-02 14:11:29 +08:00
* @ param ms_timeout display message after 2 seconds
2013-06-07 17:30:32 +08:00
* @ return true if command was returned , otherwise false
*/
2017-10-21 00:00:13 +08:00
bool WaitForResponseTimeoutW ( uint32_t cmd , UsbCommand * response , size_t ms_timeout , bool show_warning ) {
2013-06-07 17:30:32 +08:00
2015-07-31 16:37:24 +08:00
UsbCommand resp ;
2015-01-07 04:20:41 +08:00
if ( response = = NULL )
2015-07-31 16:37:24 +08:00
response = & resp ;
2015-01-07 04:20:41 +08:00
2017-10-20 22:32:42 +08:00
uint64_t start_time = msclock ( ) ;
2015-07-31 16:37:24 +08:00
// Wait until the command is received
2017-10-20 22:32:42 +08:00
while ( true ) {
2013-06-07 17:30:32 +08:00
2017-10-20 22:32:42 +08:00
while ( getCommand ( response ) ) {
2018-04-19 02:17:00 +08:00
if ( cmd = = CMD_UNKNOWN | | response - > cmd = = cmd )
2016-02-10 20:22:48 +08:00
return true ;
2015-07-31 16:37:24 +08:00
}
2016-02-10 20:22:48 +08:00
2017-10-21 00:24:04 +08:00
if ( msclock ( ) - start_time > ms_timeout )
2017-10-20 22:32:42 +08:00
break ;
2017-11-28 15:30:25 +08:00
if ( msclock ( ) - start_time > 3000 & & show_warning ) {
2018-04-19 02:17:00 +08:00
// 3 seconds elapsed (but this doesn't mean the timeout was exceeded)
2018-02-21 20:05:42 +08:00
PrintAndLogEx ( NORMAL , " Waiting for a response from the proxmark... " ) ;
PrintAndLogEx ( NORMAL , " You can cancel this operation by pressing the pm3 button " ) ;
2017-12-24 05:17:30 +08:00
show_warning = false ;
2015-07-31 16:37:24 +08:00
}
2013-06-07 17:30:32 +08:00
}
2015-07-31 16:37:24 +08:00
return false ;
2010-07-13 21:39:30 +08:00
}
2017-10-20 22:32:42 +08:00
bool WaitForResponseTimeout ( uint32_t cmd , UsbCommand * response , size_t ms_timeout ) {
return WaitForResponseTimeoutW ( cmd , response , ms_timeout , true ) ;
}
2012-12-05 07:39:18 +08:00
bool WaitForResponse ( uint32_t cmd , UsbCommand * response ) {
2017-10-21 00:24:04 +08:00
return WaitForResponseTimeoutW ( cmd , response , - 1 , true ) ;
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
//-----------------------------------------------------------------------------
// Entry point into our code: called whenever the user types a command and
// then presses Enter, which the full command line that they typed.
//-----------------------------------------------------------------------------
2015-11-10 04:46:57 +08:00
int CommandReceived ( char * Cmd ) {
return CmdsParse ( CommandTable , Cmd ) ;
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
//-----------------------------------------------------------------------------
// Entry point into our code: called whenever we received a packet over USB
// that we weren't necessarily expecting, for example a debug print.
//-----------------------------------------------------------------------------
2018-04-26 18:13:03 +08:00
void UsbCommandReceived ( UsbCommand * _ch ) {
//UsbCommand *c = malloc(sizeof(UsbCommand));
//memset(cp, 0x00, sizeof(*cp));
pthread_mutex_lock ( & cmdBufferMutex ) ;
UsbCommand * c = _ch ;
pthread_mutex_unlock ( & cmdBufferMutex ) ;
2017-10-20 22:32:42 +08:00
switch ( c - > cmd ) {
2015-01-07 04:20:41 +08:00
// First check if we are handling a debug message
case CMD_DEBUG_PRINT_STRING : {
2018-04-26 18:13:03 +08:00
2016-02-10 20:22:48 +08:00
char s [ USB_CMD_DATA_SIZE + 1 ] ;
2016-02-10 20:07:44 +08:00
memset ( s , 0x00 , sizeof ( s ) ) ;
2018-04-27 17:22:30 +08:00
size_t len = MIN ( c - > arg [ 0 ] , USB_CMD_DATA_SIZE ) ;
2017-10-20 22:32:42 +08:00
memcpy ( s , c - > d . asBytes , len ) ;
2016-03-06 17:35:25 +08:00
2018-04-26 18:13:03 +08:00
// print debug line on same row. escape seq \r
2017-10-20 22:32:42 +08:00
if ( c - > arg [ 1 ] = = CMD_MEASURE_ANTENNA_TUNING_HF ) {
2018-07-01 04:54:30 +08:00
PrintAndLogEx ( NORMAL , " \r #db# %s " , s ) ;
2018-04-26 18:13:03 +08:00
} else {
2018-02-21 20:05:42 +08:00
PrintAndLogEx ( NORMAL , " #db# %s " , s ) ;
2017-01-17 04:06:51 +08:00
}
2018-07-01 04:54:30 +08:00
fflush ( NULL ) ;
2018-04-26 18:13:03 +08:00
break ;
}
2016-03-12 16:03:28 +08:00
case CMD_DEBUG_PRINT_INTEGERS : {
2018-02-21 20:05:42 +08:00
PrintAndLogEx ( NORMAL , " #db# %08x, %08x, %08x " , c - > arg [ 0 ] , c - > arg [ 1 ] , c - > arg [ 2 ] ) ;
2016-02-10 20:22:48 +08:00
break ;
2016-03-12 16:03:28 +08:00
}
2018-05-02 14:11:29 +08:00
// iceman: hw status - down the path on device, runs printusbspeed which starts sending a lot of
// CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K packages which is not dealt with. I wonder if simply ignoring them will
// work. lets try it.
default : {
storeCommand ( c ) ;
break ;
}
}
}
2018-05-03 18:15:03 +08:00
/**
* Data transfer from Proxmark to client . This method times out after
* ms_timeout milliseconds .
* @ brief GetFromDevice
* @ param memtype Type of memory to download from proxmark
* @ param dest Destination address for transfer
* @ param bytes number of bytes to be transferred
* @ param start_index offset into Proxmark3 BigBuf [ ]
* @ param response struct to copy last command ( CMD_ACK ) into
* @ param ms_timeout timeout in milliseconds
* @ param show_warning display message after 2 seconds
* @ return true if command was returned , otherwise false
*/
2018-05-03 01:37:51 +08:00
bool GetFromDevice ( DeviceMemType_t memtype , uint8_t * dest , uint32_t bytes , uint32_t start_index , UsbCommand * response , size_t ms_timeout , bool show_warning ) {
if ( dest = = NULL ) return false ;
if ( bytes = = 0 ) return true ;
UsbCommand resp ;
if ( response = = NULL )
response = & resp ;
// clear
clearCommandBuffer ( ) ;
switch ( memtype ) {
case BIG_BUF : {
UsbCommand c = { CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K , { start_index , bytes , 0 } } ;
SendCommand ( & c ) ;
return dl_it ( dest , bytes , start_index , response , ms_timeout , show_warning , CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K ) ;
}
case BIG_BUF_EML : {
UsbCommand c = { CMD_DOWNLOAD_EML_BIGBUF , { start_index , bytes , 0 } } ;
SendCommand ( & c ) ;
return dl_it ( dest , bytes , start_index , response , ms_timeout , show_warning , CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K ) ;
}
case FLASH_MEM : {
UsbCommand c = { CMD_DOWNLOAND_FLASH_MEM , { start_index , bytes , 0 } } ;
SendCommand ( & c ) ;
return dl_it ( dest , bytes , start_index , response , ms_timeout , show_warning , CMD_DOWNLOADED_FLASHMEM ) ;
}
case SIM_MEM : {
//UsbCommand c = {CMD_DOWNLOAND_SIM_MEM, {start_index, bytes, 0}};
//SendCommand(&c);
//return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM);
return false ;
}
}
return false ;
}
2018-05-02 14:11:29 +08:00
bool dl_it ( uint8_t * dest , uint32_t bytes , uint32_t start_index , UsbCommand * response , size_t ms_timeout , bool show_warning , uint32_t rec_cmd ) {
uint32_t bytes_completed = 0 ;
uint64_t start_time = msclock ( ) ;
while ( true ) {
if ( getCommand ( response ) ) {
2018-04-26 18:13:03 +08:00
2017-01-17 04:06:51 +08:00
// sample_buf is a array pointer, located in data.c
// arg0 = offset in transfer. Startindex of this chunk
// arg1 = length bytes to transfer
2018-05-02 14:11:29 +08:00
// arg2 = bigbuff tracelength (?)
if ( response - > cmd = = rec_cmd ) {
uint32_t offset = response - > arg [ 0 ] ;
uint32_t copy_bytes = MIN ( bytes - bytes_completed , response - > arg [ 1 ] ) ;
//uint32_t tracelen = c->arg[2];
// extended bounds check1. upper limit is USB_CMD_DATA_SIZE
// shouldn't happen
copy_bytes = MIN ( copy_bytes , USB_CMD_DATA_SIZE ) ;
// extended bounds check2.
if ( offset + copy_bytes > bytes ) {
PrintAndLogEx ( FAILED , " ERROR: Out of bounds when downloading from device, offset %u | len %u | total len %u > buf_size %u " , offset , copy_bytes , offset + copy_bytes , bytes ) ;
break ;
}
memcpy ( dest + offset , response - > d . asBytes , copy_bytes ) ;
bytes_completed + = copy_bytes ;
} else if ( response - > cmd = = CMD_ACK ) {
return true ;
2018-04-27 18:36:53 +08:00
}
2016-03-12 16:03:28 +08:00
}
2018-05-02 14:11:29 +08:00
if ( msclock ( ) - start_time > ms_timeout ) {
PrintAndLogEx ( FAILED , " Timed out while trying to download data from device " ) ;
2015-10-05 00:01:33 +08:00
break ;
2015-10-12 01:14:17 +08:00
}
2018-05-02 14:11:29 +08:00
if ( msclock ( ) - start_time > 3000 & & show_warning ) {
// 3 seconds elapsed (but this doesn't mean the timeout was exceeded)
PrintAndLogEx ( NORMAL , " Waiting for a response from the proxmark... " ) ;
PrintAndLogEx ( NORMAL , " You can cancel this operation by pressing the pm3 button " ) ;
show_warning = false ;
}
2015-10-05 00:01:33 +08:00
}
2018-05-02 14:11:29 +08:00
return false ;
}