]>
Commit | Line | Data |
---|---|---|
a553f267 | 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 | //----------------------------------------------------------------------------- | |
8fe1a992 | 6 | // Flasher frontend tool |
a553f267 | 7 | //----------------------------------------------------------------------------- |
8 | ||
6658905f | 9 | #include <stdio.h> |
83a9b236 | 10 | #include <stdlib.h> |
8fe1a992 | 11 | #include <string.h> |
43534cba | 12 | #include <inttypes.h> |
afdcb8c1 MF |
13 | #include <pthread.h> |
14 | ||
125a98a1 | 15 | #include "proxmark3.h" |
acf0582d | 16 | #include "util.h" |
ec9c7112 | 17 | #include "util_posix.h" |
6e4d4ee6 | 18 | #include "flash.h" |
28fdb04f | 19 | #include "uart.h" |
20 | #include "usb_cmd.h" | |
afdcb8c1 | 21 | #include "comms.h" |
28fdb04f | 22 | |
13dbdd6b | 23 | #ifdef _WIN32 |
24 | # define unlink(x) | |
067bfc8b MF |
25 | #else |
26 | # include <unistd.h> | |
13dbdd6b | 27 | #endif |
28 | ||
28fdb04f | 29 | void cmd_debug(UsbCommand* UC) { |
30 | // Debug | |
31 | printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand)); | |
43534cba | 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]); | |
28fdb04f | 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 | ||
8fe1a992 | 43 | static void usage(char *argv0) |
a5b1ba20 | 44 | { |
28fdb04f | 45 | fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0); |
8fe1a992 | 46 | fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n"); |
5b59cfb7 | 47 | //Is the example below really true? /Martin |
e12b82d3 | 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); | |
afdcb8c1 MF |
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"); | |
a5b1ba20 | 54 | } |
6658905f | 55 | |
8fe1a992 | 56 | #define MAX_FILES 4 |
57 | ||
7fe9b0b7 | 58 | int main(int argc, char **argv) |
59 | { | |
8fe1a992 | 60 | int can_write_bl = 0; |
61 | int num_files = 0; | |
62 | int res; | |
63 | flash_file_t files[MAX_FILES]; | |
afdcb8c1 MF |
64 | receiver_arg conn; |
65 | pthread_t reader_thread; | |
8fe1a992 | 66 | |
afdcb8c1 | 67 | memset(&conn, 0, sizeof(receiver_arg)); |
8fe1a992 | 68 | memset(files, 0, sizeof(files)); |
69 | ||
28fdb04f | 70 | if (argc < 3) { |
8fe1a992 | 71 | usage(argv[0]); |
72 | return -1; | |
73 | } | |
74 | ||
28fdb04f | 75 | for (int i = 2; i < argc; i++) { |
8fe1a992 | 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 | ||
afdcb8c1 MF |
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); | |
8fe1a992 | 108 | |
afdcb8c1 | 109 | res = flash_start_flashing(&conn, can_write_bl, serial_port_name); |
8fe1a992 | 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 | ||
afdcb8c1 MF |
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); | |
8fe1a992 | 134 | |
135 | fprintf(stderr, "All done.\n\n"); | |
136 | fprintf(stderr, "Have a nice day!\n"); | |
137 | ||
138 | return 0; | |
6658905f | 139 | } |