1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Main command parser entry point
9 //-----------------------------------------------------------------------------
16 #include "cmdparser.h"
17 #include "proxmark3.h"
19 #include "../include/usb_cmd.h"
27 #include "cmdscript.h"
34 unsigned int current_command
= CMD_UNKNOWN
;
35 //unsigned int received_command = CMD_UNKNOWN;
36 //UsbCommand current_response;
37 //UsbCommand current_response_user;
39 static int CmdHelp(const char *Cmd
);
40 static int CmdQuit(const char *Cmd
);
42 //For storing command that are received from the device
43 #define CMD_BUFFER_SIZE 50
44 static UsbCommand cmdBuffer
[CMD_BUFFER_SIZE
];
45 //Points to the next empty position to write to
46 static int cmd_head
;//Starts as 0
47 //Points to the position of the last unread command
48 static int cmd_tail
;//Starts as 0
50 static command_t CommandTable
[] =
52 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of a particular command."},
53 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
54 {"hf", CmdHF
, 1, "{ High Frequency commands... }"},
55 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
56 {"lf", CmdLF
, 1, "{ Low Frequency commands... }"},
57 {"script", CmdScript
, 1,"{ Scripting commands }"},
58 {"quit", CmdQuit
, 1, "Exit program"},
59 {"exit", CmdQuit
, 1, "Exit program"},
63 command_t
* getTopLevelCommandTable()
67 int CmdHelp(const char *Cmd
)
69 CmdsHelp(CommandTable
);
73 int CmdQuit(const char *Cmd
)
79 * @brief This method should be called when sending a new command to the pm3. In case any old
80 * responses from previous commands are stored in the buffer, a call to this method should clear them.
81 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
82 * operation. Right now we'll just have to live with this.
84 void clearCommandBuffer()
86 //This is a very simple operation
91 * @brief storeCommand stores a USB command in a circular buffer
94 void storeCommand(UsbCommand
*command
)
96 if( ( cmd_head
+1) % CMD_BUFFER_SIZE
== cmd_tail
)
98 //If these two are equal, we're about to overwrite in the
100 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
102 //Store the command at the 'head' location
103 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
104 memcpy(destination
, command
, sizeof(UsbCommand
));
106 cmd_head
= (cmd_head
+1) % CMD_BUFFER_SIZE
; //increment head and wrap
110 * @brief getCommand gets a command from an internal circular buffer.
111 * @param response location to write command
112 * @return 1 if response was returned, 0 if nothing has been received
114 int getCommand(UsbCommand
* response
)
116 //If head == tail, there's nothing to read, or if we just got initialized
117 if(cmd_head
== cmd_tail
){
120 //Pick out the next unread command
121 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
122 memcpy(response
, last_unread
, sizeof(UsbCommand
));
123 //Increment tail - this is a circular buffer, so modulo buffer size
124 cmd_tail
= (cmd_tail
+1 ) % CMD_BUFFER_SIZE
;
131 * Waits for a certain response type. This method waits for a maximum of
132 * ms_timeout milliseconds for a specified response command.
133 *@brief WaitForResponseTimeout
134 * @param cmd command to wait for
135 * @param response struct to copy received command into.
137 * @return true if command was returned, otherwise false
139 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
143 if (response
== NULL
) {
148 // Wait until the command is received
149 for(size_t dm_seconds
=0; dm_seconds
< ms_timeout
/10; dm_seconds
++) {
151 while(getCommand(response
))
153 if(response
->cmd
== cmd
){
154 //We got what we expected
159 msleep(10); // XXX ugh
160 if (dm_seconds
== 200) { // Two seconds elapsed
161 PrintAndLog("Waiting for a response from the proxmark...");
162 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
168 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
169 return WaitForResponseTimeout(cmd
,response
,-1);
172 //-----------------------------------------------------------------------------
173 // Entry point into our code: called whenever the user types a command and
174 // then presses Enter, which the full command line that they typed.
175 //-----------------------------------------------------------------------------
176 void CommandReceived(char *Cmd
) {
177 CmdsParse(CommandTable
, Cmd
);
180 //-----------------------------------------------------------------------------
181 // Entry point into our code: called whenever we received a packet over USB
182 // that we weren't necessarily expecting, for example a debug print.
183 //-----------------------------------------------------------------------------
184 void UsbCommandReceived(UsbCommand
*UC
)
188 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
189 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
190 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
191 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
192 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
193 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
196 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
197 // If we recognize a response, return to avoid further processing
199 // First check if we are handling a debug message
200 case CMD_DEBUG_PRINT_STRING
: {
201 char s
[USB_CMD_DATA_SIZE
+1];
202 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
203 memcpy(s
,UC
->d
.asBytes
,len
);
205 PrintAndLog("#db# %s ", s
);
209 case CMD_DEBUG_PRINT_INTEGERS
: {
210 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
214 case CMD_MEASURED_ANTENNA_TUNING
: {
216 int vLf125
, vLf134
, vHf
;
217 vLf125
= UC
->arg
[0] & 0xffff;
218 vLf134
= UC
->arg
[0] >> 16;
219 vHf
= UC
->arg
[1] & 0xffff;;
220 peakf
= UC
->arg
[2] & 0xffff;
221 peakv
= UC
->arg
[2] >> 16;
223 //Reset delta trigger every 3:d time
225 if ( deltaReset
== 4){
226 delta125
[0] = vLf125
;
227 delta134
[0] = vLf134
;
229 } else if ( deltaReset
== 2){
230 delta125
[1] = vLf125
;
231 delta134
[1] = vLf134
;
235 if ( deltaReset
== 0){
240 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
241 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
242 PrintAndLog("# LF optimal: %5.2f V @ %9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
243 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
245 PrintAndLog("# Your LF antenna is unusable.");
246 else if (peakv
<10000)
247 PrintAndLog("# Your LF antenna is marginal.");
249 PrintAndLog("# Your HF antenna is unusable.");
251 PrintAndLog("# Your HF antenna is marginal.");
254 deltaReset
= (deltaReset
== 0) ? 4 : deltaReset
>>1;
257 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
258 // printf("received samples: ");
259 // print_hex(UC->d.asBytes,512);
260 sample_buf_len
+= UC
->arg
[1];
261 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
262 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
267 // PrintAndLog("Receive ACK\n");
271 // Maybe it's a response
273 switch(current_command) {
274 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
275 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
276 PrintAndLog("unrecognized command %08x\n", UC->cmd);
280 PrintAndLog("received samples %d\n",UC->arg[0]);
281 memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
282 sample_buf_len += 48;
283 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
284 //received_command = UC->cmd;