| 1 | //----------------------------------------------------------------------------- |
| 2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> |
| 3 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> |
| 4 | // |
| 5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
| 6 | // at your option, any later version. See the LICENSE.txt file for the text of |
| 7 | // the license. |
| 8 | //----------------------------------------------------------------------------- |
| 9 | // Main binary |
| 10 | //----------------------------------------------------------------------------- |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <pthread.h> |
| 16 | #include <unistd.h> |
| 17 | #include <readline/readline.h> |
| 18 | #include <readline/history.h> |
| 19 | //#include "proxusb.h" |
| 20 | #include "proxmark3.h" |
| 21 | #include "proxgui.h" |
| 22 | #include "cmdmain.h" |
| 23 | #include "uart.h" |
| 24 | #include "ui.h" |
| 25 | #include "sleep.h" |
| 26 | #include "cmdparser.h" |
| 27 | #include "cmdmain.h" |
| 28 | |
| 29 | // a global mutex to prevent interlaced printing from different threads |
| 30 | pthread_mutex_t print_lock; |
| 31 | |
| 32 | static serial_port sp; |
| 33 | static UsbCommand txcmd; |
| 34 | volatile static bool txcmd_pending = false; |
| 35 | |
| 36 | void SendCommand(UsbCommand *c) { |
| 37 | #if 0 |
| 38 | printf("Sending %d bytes\n", sizeof(UsbCommand)); |
| 39 | #endif |
| 40 | /* |
| 41 | if (txcmd_pending) { |
| 42 | ERR("Sending command failed, previous command is still pending"); |
| 43 | } |
| 44 | */ |
| 45 | if(offline) |
| 46 | { |
| 47 | PrintAndLog("Sending bytes to proxmark failed - offline"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | while(txcmd_pending); |
| 52 | txcmd = *c; |
| 53 | txcmd_pending = true; |
| 54 | } |
| 55 | |
| 56 | struct receiver_arg { |
| 57 | int run; |
| 58 | }; |
| 59 | |
| 60 | struct main_loop_arg { |
| 61 | int usb_present; |
| 62 | char *script_cmds_file; |
| 63 | }; |
| 64 | |
| 65 | byte_t rx[0x1000000]; |
| 66 | byte_t* prx = rx; |
| 67 | |
| 68 | static void *uart_receiver(void *targ) { |
| 69 | struct receiver_arg *arg = (struct receiver_arg*)targ; |
| 70 | size_t rxlen; |
| 71 | size_t cmd_count; |
| 72 | |
| 73 | while (arg->run) { |
| 74 | rxlen = sizeof(UsbCommand); |
| 75 | if (uart_receive(sp,prx,&rxlen)) { |
| 76 | prx += rxlen; |
| 77 | if (((prx-rx) % sizeof(UsbCommand)) != 0) { |
| 78 | continue; |
| 79 | } |
| 80 | cmd_count = (prx-rx) / sizeof(UsbCommand); |
| 81 | // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count); |
| 82 | for (size_t i=0; i<cmd_count; i++) { |
| 83 | UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand)))); |
| 84 | } |
| 85 | } |
| 86 | prx = rx; |
| 87 | |
| 88 | if(txcmd_pending) { |
| 89 | if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) { |
| 90 | PrintAndLog("Sending bytes to proxmark failed"); |
| 91 | } |
| 92 | txcmd_pending = false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pthread_exit(NULL); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | static void *main_loop(void *targ) { |
| 101 | struct main_loop_arg *arg = (struct main_loop_arg*)targ; |
| 102 | struct receiver_arg rarg; |
| 103 | char *cmd = NULL; |
| 104 | pthread_t reader_thread; |
| 105 | |
| 106 | if (arg->usb_present == 1) { |
| 107 | rarg.run=1; |
| 108 | // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg); |
| 109 | pthread_create(&reader_thread, NULL, &uart_receiver, &rarg); |
| 110 | } |
| 111 | |
| 112 | FILE *script_file = NULL; |
| 113 | char script_cmd_buf[256]; |
| 114 | |
| 115 | if (arg->script_cmds_file) |
| 116 | { |
| 117 | script_file = fopen(arg->script_cmds_file, "r"); |
| 118 | if (script_file) |
| 119 | { |
| 120 | printf("using 'scripting' commands file %s\n", arg->script_cmds_file); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | read_history(".history"); |
| 125 | while(1) |
| 126 | { |
| 127 | // If there is a script file |
| 128 | if (script_file) |
| 129 | { |
| 130 | if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) |
| 131 | { |
| 132 | fclose(script_file); |
| 133 | script_file = NULL; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | char *nl; |
| 138 | nl = strrchr(script_cmd_buf, '\r'); |
| 139 | if (nl) *nl = '\0'; |
| 140 | nl = strrchr(script_cmd_buf, '\n'); |
| 141 | if (nl) *nl = '\0'; |
| 142 | |
| 143 | if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) |
| 144 | { |
| 145 | memset(cmd, 0, strlen(script_cmd_buf)); |
| 146 | strcpy(cmd, script_cmd_buf); |
| 147 | printf("%s\n", cmd); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!script_file) |
| 153 | { |
| 154 | cmd = readline(PROXPROMPT); |
| 155 | } |
| 156 | |
| 157 | if (cmd) { |
| 158 | while(cmd[strlen(cmd) - 1] == ' ') |
| 159 | cmd[strlen(cmd) - 1] = 0x00; |
| 160 | |
| 161 | if (cmd[0] != 0x00) { |
| 162 | if (strncmp(cmd, "quit", 4) == 0) { |
| 163 | exit(0); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | CommandReceived(cmd); |
| 168 | add_history(cmd); |
| 169 | } |
| 170 | free(cmd); |
| 171 | } else { |
| 172 | printf("\n"); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | write_history(".history"); |
| 178 | |
| 179 | if (arg->usb_present == 1) { |
| 180 | rarg.run = 0; |
| 181 | pthread_join(reader_thread, NULL); |
| 182 | } |
| 183 | |
| 184 | if (script_file) |
| 185 | { |
| 186 | fclose(script_file); |
| 187 | script_file = NULL; |
| 188 | } |
| 189 | |
| 190 | ExitGraphics(); |
| 191 | pthread_exit(NULL); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | static void dumpAllHelp(int markdown) |
| 196 | { |
| 197 | printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================"); |
| 198 | printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":""); |
| 199 | printf("Check column \"offline\" for their availability.\n"); |
| 200 | printf("\n"); |
| 201 | command_t *cmds = getTopLevelCommandTable(); |
| 202 | dumpCommandsRecursive(cmds, markdown); |
| 203 | } |
| 204 | |
| 205 | int main(int argc, char* argv[]) { |
| 206 | srand(time(0)); |
| 207 | |
| 208 | if (argc < 2) { |
| 209 | printf("syntax: %s <port>\n\n",argv[0]); |
| 210 | printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]); |
| 211 | printf("help: %s -h\n\n", argv[0]); |
| 212 | printf("\tDump all interactive help at once\n"); |
| 213 | printf("markdown: %s -m\n\n", argv[0]); |
| 214 | printf("\tDump all interactive help at once in markdown syntax\n"); |
| 215 | return 1; |
| 216 | } |
| 217 | if (strcmp(argv[1], "-h") == 0) { |
| 218 | printf("syntax: %s <port>\n\n",argv[0]); |
| 219 | printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]); |
| 220 | dumpAllHelp(0); |
| 221 | return 0; |
| 222 | } |
| 223 | if (strcmp(argv[1], "-m") == 0) { |
| 224 | dumpAllHelp(1); |
| 225 | return 0; |
| 226 | } |
| 227 | // Make sure to initialize |
| 228 | struct main_loop_arg marg = { |
| 229 | .usb_present = 0, |
| 230 | .script_cmds_file = NULL |
| 231 | }; |
| 232 | pthread_t main_loop_t; |
| 233 | |
| 234 | |
| 235 | sp = uart_open(argv[1]); |
| 236 | if (sp == INVALID_SERIAL_PORT) { |
| 237 | printf("ERROR: invalid serial port\n"); |
| 238 | marg.usb_present = 0; |
| 239 | offline = 1; |
| 240 | } else if (sp == CLAIMED_SERIAL_PORT) { |
| 241 | printf("ERROR: serial port is claimed by another process\n"); |
| 242 | marg.usb_present = 0; |
| 243 | offline = 1; |
| 244 | } else { |
| 245 | marg.usb_present = 1; |
| 246 | offline = 0; |
| 247 | } |
| 248 | |
| 249 | // If the user passed the filename of the 'script' to execute, get it |
| 250 | if (argc > 2 && argv[2]) { |
| 251 | if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry. |
| 252 | argv[2][1] == 'l' && |
| 253 | argv[2][2] == 'u' && |
| 254 | argv[2][3] == 's' && |
| 255 | argv[2][4] == 'h') |
| 256 | { |
| 257 | printf("Output will be flushed after every print.\n"); |
| 258 | flushAfterWrite = 1; |
| 259 | } |
| 260 | else |
| 261 | marg.script_cmds_file = argv[2]; |
| 262 | } |
| 263 | |
| 264 | // create a mutex to avoid interlacing print commands from our different threads |
| 265 | pthread_mutex_init(&print_lock, NULL); |
| 266 | |
| 267 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); |
| 268 | InitGraphics(argc, argv); |
| 269 | |
| 270 | MainGraphics(); |
| 271 | |
| 272 | pthread_join(main_loop_t, NULL); |
| 273 | |
| 274 | // Clean up the port |
| 275 | uart_close(sp); |
| 276 | |
| 277 | // clean up mutex |
| 278 | pthread_mutex_destroy(&print_lock); |
| 279 | |
| 280 | return 0; |
| 281 | } |