]> git.zerfleddert.de Git - proxmark3-svn/blob - client/flasher.c
Comms refactor (prerequisite of libproxmark work) (#371)
[proxmark3-svn] / client / flasher.c
1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
4 // the license.
5 //-----------------------------------------------------------------------------
6 // Flasher frontend tool
7 //-----------------------------------------------------------------------------
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include <pthread.h>
14
15 #include "proxmark3.h"
16 #include "util.h"
17 #include "util_posix.h"
18 #include "flash.h"
19 #include "uart.h"
20 #include "usb_cmd.h"
21 #include "comms.h"
22
23 #ifdef _WIN32
24 # define unlink(x)
25 #else
26 # include <unistd.h>
27 #endif
28
29 void cmd_debug(UsbCommand* UC) {
30 // Debug
31 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
32 printf(" cmd[len=%zd]: %016" PRIx64 "\n",sizeof(UC->cmd),UC->cmd);
33 printf(" arg0[len=%zd]: %016" PRIx64 "\n",sizeof(UC->arg[0]),UC->arg[0]);
34 printf(" arg1[len=%zd]: %016" PRIx64 "\n",sizeof(UC->arg[1]),UC->arg[1]);
35 printf(" arg2[len=%zd]: %016" PRIx64 "\n",sizeof(UC->arg[2]),UC->arg[2]);
36 printf(" data[len=%zd]: ",sizeof(UC->d.asBytes));
37 for (size_t i=0; i<16; i++) {
38 printf("%02x",UC->d.asBytes[i]);
39 }
40 printf("...\n");
41 }
42
43 static void usage(char *argv0)
44 {
45 fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
46 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
47 //Is the example below really true? /Martin
48 fprintf(stderr, "Example:\n\n\t %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
49 fprintf(stderr, "\nExample (Linux):\n\n\t %s /dev/ttyACM0 armsrc/obj/fullimage.elf\n", argv0);
50 fprintf(stderr, "\nNote (Linux): if the flasher gets stuck at 'Waiting for Proxmark to reappear',\n");
51 fprintf(stderr, " you may need to blacklist proxmark for modem-manager. v1.4.14 and later\n");
52 fprintf(stderr, " include this configuration patch already. The change can be found at:\n");
53 fprintf(stderr, " https://cgit.freedesktop.org/ModemManager/ModemManager/commit/?id=6e7ff47\n\n");
54 }
55
56 #define MAX_FILES 4
57
58 int main(int argc, char **argv)
59 {
60 int can_write_bl = 0;
61 int num_files = 0;
62 int res;
63 flash_file_t files[MAX_FILES];
64 receiver_arg conn;
65 pthread_t reader_thread;
66
67 memset(&conn, 0, sizeof(receiver_arg));
68 memset(files, 0, sizeof(files));
69
70 if (argc < 3) {
71 usage(argv[0]);
72 return -1;
73 }
74
75 for (int i = 2; i < argc; i++) {
76 if (argv[i][0] == '-') {
77 if (!strcmp(argv[i], "-b")) {
78 can_write_bl = 1;
79 } else {
80 usage(argv[0]);
81 return -1;
82 }
83 } else {
84 res = flash_load(&files[num_files], argv[i], can_write_bl);
85 if (res < 0) {
86 fprintf(stderr, "Error while loading %s\n", argv[i]);
87 return -1;
88 }
89 fprintf(stderr, "\n");
90 num_files++;
91 }
92 }
93
94 pthread_mutex_init(&conn.recv_lock, NULL);
95
96 char* serial_port_name = argv[1];
97
98 fprintf(stderr,"Waiting for Proxmark to appear on %s", serial_port_name);
99 do {
100 sleep(1);
101 fprintf(stderr, ".");
102 } while (!OpenProxmark(serial_port_name));
103 fprintf(stderr," Found.\n");
104
105 // Lets start up the communications thread
106 conn.run = true;
107 pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
108
109 res = flash_start_flashing(&conn, can_write_bl, serial_port_name);
110 if (res < 0)
111 return -1;
112
113 fprintf(stderr, "\nFlashing...\n");
114
115 for (int i = 0; i < num_files; i++) {
116 res = flash_write(&files[i]);
117 if (res < 0)
118 return -1;
119 flash_free(&files[i]);
120 fprintf(stderr, "\n");
121 }
122
123 fprintf(stderr, "Resetting hardware...\n");
124
125 res = flash_stop_flashing();
126 if (res < 0)
127 return -1;
128
129 // Stop the command thread.
130 conn.run = false;
131 pthread_join(reader_thread, NULL);
132 CloseProxmark(&conn, serial_port_name);
133 pthread_mutex_destroy(&conn.recv_lock);
134
135 fprintf(stderr, "All done.\n\n");
136 fprintf(stderr, "Have a nice day!\n");
137
138 return 0;
139 }
Impressum, Datenschutz