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"
27 #include "cmdscript.h"
29 unsigned int current_command
= CMD_UNKNOWN
;
30 unsigned int received_command
= CMD_UNKNOWN
;
31 UsbCommand current_response
;
32 UsbCommand current_response_user
;
34 static int CmdHelp(const char *Cmd
);
35 static int CmdQuit(const char *Cmd
);
37 static command_t CommandTable
[] =
39 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of the following commands:\n"},
40 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
41 {"exit", CmdQuit
, 1, "Exit program"},
42 {"hf", CmdHF
, 1, "{ HF commands... }"},
43 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
44 {"lf", CmdLF
, 1, "{ LF commands... }"},
45 {"quit", CmdQuit
, 1, "Quit program"},
46 {"script", CmdScript
, 1,"Run script"},
50 int CmdHelp(const char *Cmd
)
52 CmdsHelp(CommandTable
);
56 int CmdQuit(const char *Cmd
)
62 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
64 // Wait until the command is received
65 for(size_t i
=0; received_command
!= cmd
&& i
< ms_timeout
/10; i
++) {
66 msleep(10); // XXX ugh
67 if (i
== 200) { // Two seconds elapsed
68 PrintAndLog("Waiting for a response from the proxmark...");
69 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
73 // Check if timeout occured
74 if(received_command
!= cmd
) return false;
76 // Copy the received response (if supplied)
78 memcpy(response
, ¤t_response
, sizeof(UsbCommand
));
81 // Reset the received command
82 received_command
= CMD_UNKNOWN
;
87 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
88 return WaitForResponseTimeout(cmd
,response
,-1);
91 //-----------------------------------------------------------------------------
92 // Entry point into our code: called whenever the user types a command and
93 // then presses Enter, which the full command line that they typed.
94 //-----------------------------------------------------------------------------
95 void CommandReceived(char *Cmd
) {
96 CmdsParse(CommandTable
, Cmd
);
99 //-----------------------------------------------------------------------------
100 // Entry point into our code: called whenever we received a packet over USB
101 // that we weren't necessarily expecting, for example a debug print.
102 //-----------------------------------------------------------------------------
103 void UsbCommandReceived(UsbCommand
*UC
)
107 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
108 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
109 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
110 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
111 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
112 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
115 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
116 // If we recognize a response, return to avoid further processing
118 // First check if we are handling a debug message
119 case CMD_DEBUG_PRINT_STRING
: {
120 char s
[USB_CMD_DATA_SIZE
+1];
121 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
122 memcpy(s
,UC
->d
.asBytes
,len
);
124 PrintAndLog("#db# %s ", s
);
128 case CMD_DEBUG_PRINT_INTEGERS
: {
129 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
133 case CMD_MEASURED_ANTENNA_TUNING
: {
135 int vLf125
, vLf134
, vHf
;
136 vLf125
= UC
->arg
[0] & 0xffff;
137 vLf134
= UC
->arg
[0] >> 16;
138 vHf
= UC
->arg
[1] & 0xffff;;
139 peakf
= UC
->arg
[2] & 0xffff;
140 peakv
= UC
->arg
[2] >> 16;
142 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
143 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
144 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
145 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
147 PrintAndLog("# Your LF antenna is unusable.");
148 else if (peakv
<10000)
149 PrintAndLog("# Your LF antenna is marginal.");
151 PrintAndLog("# Your HF antenna is unusable.");
153 PrintAndLog("# Your HF antenna is marginal.");
156 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
157 // printf("received samples: ");
158 // print_hex(UC->d.asBytes,512);
159 sample_buf_len
+= UC
->arg
[1];
160 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
161 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
166 // PrintAndLog("Receive ACK\n");
170 // Maybe it's a response
171 switch(current_command
) {
172 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
: {
173 if (UC
->cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
174 PrintAndLog("unrecognized command %08x\n", UC
->cmd
);
178 PrintAndLog("received samples %d\n",UC
->arg
[0]);
179 memcpy(sample_buf
+UC
->arg
[0],UC
->d
.asBytes
,48);
180 sample_buf_len
+= 48;
181 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
182 received_command
= UC
->cmd
;
188 // // Store the last received command
189 // memcpy(¤t_response, UC, sizeof(UsbCommand));
190 // received_command = UC->cmd;
193 // Store the last received command
194 memcpy(¤t_response
, UC
, sizeof(UsbCommand
));
195 received_command
= UC
->cmd
;
197 // Maybe it's a response:
198 switch(current_command) {
199 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
200 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;
202 for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
203 received_command = UC->cmd;
205 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
206 case CMD_DOWNLOADED_SIM_SAMPLES_125K:
207 if (UC->cmd != CMD_ACK) goto unexpected_response;
209 received_command = UC->cmd;
214 if(UC->cmd != CMD_ACK)
215 PrintAndLog("unrecognized command %08x \n", UC->cmd);
217 memcpy(¤t_response, UC, sizeof(UsbCommand));
218 received_command = UC->cmd;