]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Main command parser entry point | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <unistd.h> | |
14 | #include <string.h> | |
91c38cf7 | 15 | #include "sleep.h" |
7fe9b0b7 | 16 | #include "cmdparser.h" |
902cb3c0 | 17 | #include "proxmark3.h" |
7fe9b0b7 | 18 | #include "data.h" |
19 | #include "usb_cmd.h" | |
20 | #include "ui.h" | |
21 | #include "cmdhf.h" | |
22 | #include "cmddata.h" | |
23 | #include "cmdhw.h" | |
24 | #include "cmdlf.h" | |
25 | #include "cmdmain.h" | |
9440213d | 26 | #include "util.h" |
1d59cd8d | 27 | #include "cmdscript.h" |
7fe9b0b7 | 28 | |
29 | unsigned int current_command = CMD_UNKNOWN; | |
fd368d18 | 30 | //unsigned int received_command = CMD_UNKNOWN; |
31 | //UsbCommand current_response; | |
32 | //UsbCommand current_response_user; | |
7fe9b0b7 | 33 | |
34 | static int CmdHelp(const char *Cmd); | |
35 | static int CmdQuit(const char *Cmd); | |
36 | ||
fd368d18 | 37 | //For storing command that are received from the device |
38 | #define CMD_BUFFER_SIZE 50 | |
39 | static UsbCommand cmdBuffer[CMD_BUFFER_SIZE]; | |
40 | //Points to the next empty position to write to | |
41 | static int cmd_head;//Starts as 0 | |
42 | //Points to the position of the last unread command | |
43 | static int cmd_tail;//Starts as 0 | |
44 | ||
7fe9b0b7 | 45 | static command_t CommandTable[] = |
46 | { | |
c37d2e70 | 47 | {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"}, |
37239a7c | 48 | {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"}, |
7fe9b0b7 | 49 | {"exit", CmdQuit, 1, "Exit program"}, |
37239a7c | 50 | {"hf", CmdHF, 1, "{ HF commands... }"}, |
51 | {"hw", CmdHW, 1, "{ Hardware commands... }"}, | |
52 | {"lf", CmdLF, 1, "{ LF commands... }"}, | |
7fe9b0b7 | 53 | {"quit", CmdQuit, 1, "Quit program"}, |
1d59cd8d | 54 | {"script", CmdScript, 1,"Run script"}, |
7fe9b0b7 | 55 | {NULL, NULL, 0, NULL} |
56 | }; | |
57 | ||
58 | int CmdHelp(const char *Cmd) | |
59 | { | |
60 | CmdsHelp(CommandTable); | |
61 | return 0; | |
62 | } | |
63 | ||
64 | int CmdQuit(const char *Cmd) | |
65 | { | |
66 | exit(0); | |
67 | return 0; | |
68 | } | |
fd368d18 | 69 | /** |
70 | * Waits for a certain response type. This method waits for a maximum of | |
71 | * ms_timeout milliseconds for a specified response command. | |
72 | *@brief WaitForResponseTimeout | |
73 | * @param cmd command to wait for | |
74 | * @param response struct to copy received command into. | |
75 | * @param ms_timeout | |
76 | * @return true if command was returned, otherwise false | |
77 | */ | |
902cb3c0 | 78 | bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) { |
fd368d18 | 79 | |
80 | if (response == NULL) { | |
81 | UsbCommand resp; | |
82 | response = &resp; | |
902cb3c0 | 83 | } |
534983d7 | 84 | |
fd368d18 | 85 | // Wait until the command is received |
86 | for(size_t dm_seconds=0; dm_seconds < ms_timeout/10; dm_seconds++) { | |
87 | ||
88 | while(getCommand(response)) | |
89 | { | |
90 | if(response->cmd == cmd){ | |
91 | //We got what we expected | |
92 | return true; | |
93 | } | |
534983d7 | 94 | |
fd368d18 | 95 | } |
96 | msleep(10); // XXX ugh | |
97 | if (dm_seconds == 200) { // Two seconds elapsed | |
98 | PrintAndLog("Waiting for a response from the proxmark..."); | |
99 | PrintAndLog("Don't forget to cancel its operation first by pressing on the button"); | |
100 | } | |
101 | } | |
102 | return false; | |
534983d7 | 103 | } |
104 | ||
902cb3c0 | 105 | bool WaitForResponse(uint32_t cmd, UsbCommand* response) { |
106 | return WaitForResponseTimeout(cmd,response,-1); | |
7fe9b0b7 | 107 | } |
108 | ||
109 | //----------------------------------------------------------------------------- | |
110 | // Entry point into our code: called whenever the user types a command and | |
111 | // then presses Enter, which the full command line that they typed. | |
112 | //----------------------------------------------------------------------------- | |
902cb3c0 | 113 | void CommandReceived(char *Cmd) { |
7fe9b0b7 | 114 | CmdsParse(CommandTable, Cmd); |
115 | } | |
116 | ||
117 | //----------------------------------------------------------------------------- | |
118 | // Entry point into our code: called whenever we received a packet over USB | |
119 | // that we weren't necessarily expecting, for example a debug print. | |
120 | //----------------------------------------------------------------------------- | |
121 | void UsbCommandReceived(UsbCommand *UC) | |
122 | { | |
79a73ab2 | 123 | /* |
9440213d | 124 | // Debug |
79a73ab2 | 125 | printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand)); |
125a98a1 | 126 | printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd); |
127 | printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]); | |
128 | printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]); | |
129 | printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]); | |
79a73ab2 | 130 | printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]); |
131 | */ | |
9440213d | 132 | |
7fe9b0b7 | 133 | // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command); |
9440213d | 134 | // If we recognize a response, return to avoid further processing |
7fe9b0b7 | 135 | switch(UC->cmd) { |
9440213d | 136 | // First check if we are handling a debug message |
7fe9b0b7 | 137 | case CMD_DEBUG_PRINT_STRING: { |
9440213d | 138 | char s[USB_CMD_DATA_SIZE+1]; |
139 | size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE); | |
140 | memcpy(s,UC->d.asBytes,len); | |
141 | s[len] = 0x00; | |
ab8b654e | 142 | PrintAndLog("#db# %s ", s); |
7fe9b0b7 | 143 | return; |
db09cb3a | 144 | } break; |
7fe9b0b7 | 145 | |
db09cb3a | 146 | case CMD_DEBUG_PRINT_INTEGERS: { |
ab8b654e | 147 | PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]); |
7fe9b0b7 | 148 | return; |
db09cb3a | 149 | } break; |
7fe9b0b7 | 150 | |
151 | case CMD_MEASURED_ANTENNA_TUNING: { | |
152 | int peakv, peakf; | |
153 | int vLf125, vLf134, vHf; | |
154 | vLf125 = UC->arg[0] & 0xffff; | |
155 | vLf134 = UC->arg[0] >> 16; | |
156 | vHf = UC->arg[1] & 0xffff;; | |
157 | peakf = UC->arg[2] & 0xffff; | |
158 | peakv = UC->arg[2] >> 16; | |
159 | PrintAndLog(""); | |
7fe9b0b7 | 160 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); |
161 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
162 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
163 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
164 | if (peakv<2000) | |
165 | PrintAndLog("# Your LF antenna is unusable."); | |
166 | else if (peakv<10000) | |
167 | PrintAndLog("# Your LF antenna is marginal."); | |
168 | if (vHf<2000) | |
169 | PrintAndLog("# Your HF antenna is unusable."); | |
170 | else if (vHf<5000) | |
171 | PrintAndLog("# Your HF antenna is marginal."); | |
db09cb3a | 172 | } break; |
173 | ||
902cb3c0 | 174 | case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: { |
175 | // printf("received samples: "); | |
176 | // print_hex(UC->d.asBytes,512); | |
177 | sample_buf_len += UC->arg[1]; | |
178 | // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]); | |
179 | memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]); | |
180 | } break; | |
181 | ||
182 | ||
183 | // case CMD_ACK: { | |
184 | // PrintAndLog("Receive ACK\n"); | |
185 | // } break; | |
186 | ||
db09cb3a | 187 | default: { |
188 | // Maybe it's a response | |
fd368d18 | 189 | /* |
db09cb3a | 190 | switch(current_command) { |
191 | case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: { | |
192 | if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) { | |
193 | PrintAndLog("unrecognized command %08x\n", UC->cmd); | |
194 | break; | |
195 | } | |
902cb3c0 | 196 | // int i; |
197 | PrintAndLog("received samples %d\n",UC->arg[0]); | |
198 | memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48); | |
199 | sample_buf_len += 48; | |
200 | // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i]; | |
fd368d18 | 201 | //received_command = UC->cmd; |
db09cb3a | 202 | } break; |
203 | ||
204 | default: { | |
205 | } break; | |
fd368d18 | 206 | }*/ |
207 | } | |
208 | break; | |
7fe9b0b7 | 209 | } |
fd368d18 | 210 | |
211 | storeCommand(UC); | |
212 | ||
213 | } | |
214 | ||
215 | /** | |
216 | * @brief This method should be called when sending a new command to the pm3. In case any old | |
217 | * responses from previous commands are stored in the buffer, a call to this method should clear them. | |
218 | * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which | |
219 | * operation. Right now we'll just have to live with this. | |
db09cb3a | 220 | */ |
fd368d18 | 221 | void clearCommandBuffer() |
222 | { | |
223 | //This is a very simple operation | |
224 | cmd_tail = cmd_head; | |
225 | } | |
226 | ||
227 | /** | |
228 | * @brief storeCommand stores a USB command in a circular buffer | |
229 | * @param UC | |
230 | */ | |
231 | void storeCommand(UsbCommand *command) | |
232 | { | |
233 | if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail) | |
234 | { | |
235 | //If these two are equal, we're about to overwrite in the | |
236 | // circular buffer. | |
237 | PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!"); | |
238 | } | |
239 | //Store the command at the 'head' location | |
240 | UsbCommand* destination = &cmdBuffer[cmd_head]; | |
241 | memcpy(destination, command, sizeof(UsbCommand)); | |
242 | ||
243 | cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap | |
244 | ||
245 | } | |
246 | /** | |
247 | * @brief getCommand gets a command from an internal circular buffer. | |
248 | * @param response location to write command | |
249 | * @return 1 if response was returned, 0 if nothing has been received | |
250 | */ | |
251 | int getCommand(UsbCommand* response) | |
252 | { | |
253 | //If head == tail, there's nothing to read, or if we just got initialized | |
254 | if(cmd_head == cmd_tail){ | |
255 | return 0; | |
256 | } | |
257 | //Pick out the next unread command | |
258 | UsbCommand* last_unread = &cmdBuffer[cmd_tail]; | |
259 | memcpy(response, last_unread, sizeof(UsbCommand)); | |
260 | //Increment tail - this is a circular buffer, so modulo buffer size | |
261 | cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE; | |
262 | ||
263 | return 1; | |
264 | ||
c37d2e70 | 265 | } |