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> |
4cd41f34 |
12 | #include "sleep.h" |
125a98a1 |
13 | #include "proxmark3.h" |
6e4d4ee6 |
14 | #include "flash.h" |
28fdb04f |
15 | #include "uart.h" |
16 | #include "usb_cmd.h" |
17 | |
13dbdd6b |
18 | #ifdef _WIN32 |
19 | # define unlink(x) |
20 | #endif |
21 | |
28fdb04f |
22 | static serial_port sp; |
23 | static char* serial_port_name; |
24 | |
25 | void cmd_debug(UsbCommand* UC) { |
26 | // Debug |
27 | printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand)); |
125a98a1 |
28 | printf(" cmd[len=%zd]: %016"llx"\n",sizeof(UC->cmd),UC->cmd); |
29 | printf(" arg0[len=%zd]: %016"llx"\n",sizeof(UC->arg[0]),UC->arg[0]); |
30 | printf(" arg1[len=%zd]: %016"llx"\n",sizeof(UC->arg[1]),UC->arg[1]); |
31 | printf(" arg2[len=%zd]: %016"llx"\n",sizeof(UC->arg[2]),UC->arg[2]); |
28fdb04f |
32 | printf(" data[len=%zd]: ",sizeof(UC->d.asBytes)); |
33 | for (size_t i=0; i<16; i++) { |
34 | printf("%02x",UC->d.asBytes[i]); |
35 | } |
36 | printf("...\n"); |
37 | } |
38 | |
39 | void SendCommand(UsbCommand* txcmd) { |
40 | // printf("send: "); |
41 | // cmd_debug(txcmd); |
42 | if (!uart_send(sp,(byte_t*)txcmd,sizeof(UsbCommand))) { |
43 | printf("Sending bytes to proxmark failed\n"); |
44 | exit(1); |
45 | } |
46 | } |
47 | |
48 | void ReceiveCommand(UsbCommand* rxcmd) { |
49 | byte_t* prxcmd = (byte_t*)rxcmd; |
50 | byte_t* prx = prxcmd; |
51 | size_t rxlen; |
52 | while (true) { |
53 | rxlen = sizeof(UsbCommand) - (prx-prxcmd); |
54 | if (uart_receive(sp,prx,&rxlen)) { |
28fdb04f |
55 | prx += rxlen; |
56 | if ((prx-prxcmd) >= sizeof(UsbCommand)) { |
28fdb04f |
57 | return; |
58 | } |
59 | } |
60 | } |
61 | } |
62 | |
63 | void CloseProxmark() { |
64 | // Clean up the port |
65 | uart_close(sp); |
13dbdd6b |
66 | // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/* |
67 | unlink(serial_port_name); |
28fdb04f |
68 | } |
69 | |
70 | int OpenProxmark(size_t i) { |
71 | sp = uart_open(serial_port_name); |
4890730a |
72 | if (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT) { |
c1e745e4 |
73 | //poll once a second |
28fdb04f |
74 | return 0; |
75 | } |
76 | return 1; |
77 | } |
6658905f |
78 | |
8fe1a992 |
79 | static void usage(char *argv0) |
a5b1ba20 |
80 | { |
28fdb04f |
81 | fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0); |
8fe1a992 |
82 | fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n"); |
5b59cfb7 |
83 | //Is the example below really true? /Martin |
e12b82d3 |
84 | fprintf(stderr, "Example:\n\n\t %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0); |
85 | fprintf(stderr, "\nExample (Linux):\n\n\t %s /dev/ttyACM0 armsrc/obj/fullimage.elf\n", argv0); |
86 | fprintf(stderr, "\nNote (Linux): if the flasher gets stuck in 'Waiting for Proxmark to reappear on <DEVICE>',\n"); |
87 | fprintf(stderr, " you need to blacklist proxmark for modem-manager - see wiki for more details:\n"); |
88 | fprintf(stderr, " http://code.google.com/p/proxmark3/wiki/Linux\n\n"); |
a5b1ba20 |
89 | } |
6658905f |
90 | |
8fe1a992 |
91 | #define MAX_FILES 4 |
92 | |
7fe9b0b7 |
93 | int main(int argc, char **argv) |
94 | { |
8fe1a992 |
95 | int can_write_bl = 0; |
96 | int num_files = 0; |
97 | int res; |
98 | flash_file_t files[MAX_FILES]; |
99 | |
100 | memset(files, 0, sizeof(files)); |
101 | |
28fdb04f |
102 | if (argc < 3) { |
8fe1a992 |
103 | usage(argv[0]); |
104 | return -1; |
105 | } |
106 | |
28fdb04f |
107 | for (int i = 2; i < argc; i++) { |
8fe1a992 |
108 | if (argv[i][0] == '-') { |
109 | if (!strcmp(argv[i], "-b")) { |
110 | can_write_bl = 1; |
111 | } else { |
112 | usage(argv[0]); |
113 | return -1; |
114 | } |
115 | } else { |
116 | res = flash_load(&files[num_files], argv[i], can_write_bl); |
117 | if (res < 0) { |
118 | fprintf(stderr, "Error while loading %s\n", argv[i]); |
119 | return -1; |
120 | } |
121 | fprintf(stderr, "\n"); |
122 | num_files++; |
123 | } |
124 | } |
125 | |
28fdb04f |
126 | serial_port_name = argv[1]; |
4a049b78 |
127 | |
e654346b |
128 | fprintf(stderr,"Waiting for Proxmark to appear on %s",serial_port_name); |
4a049b78 |
129 | do { |
130 | sleep(1); |
131 | fprintf(stderr, "."); |
132 | } while (!OpenProxmark(0)); |
133 | fprintf(stderr," Found.\n"); |
8fe1a992 |
134 | |
e12b82d3 |
135 | res = flash_start_flashing(can_write_bl,serial_port_name); |
8fe1a992 |
136 | if (res < 0) |
137 | return -1; |
138 | |
139 | fprintf(stderr, "\nFlashing...\n"); |
140 | |
141 | for (int i = 0; i < num_files; i++) { |
142 | res = flash_write(&files[i]); |
143 | if (res < 0) |
144 | return -1; |
145 | flash_free(&files[i]); |
146 | fprintf(stderr, "\n"); |
147 | } |
148 | |
149 | fprintf(stderr, "Resetting hardware...\n"); |
150 | |
151 | res = flash_stop_flashing(); |
152 | if (res < 0) |
153 | return -1; |
154 | |
155 | CloseProxmark(); |
156 | |
157 | fprintf(stderr, "All done.\n\n"); |
158 | fprintf(stderr, "Have a nice day!\n"); |
159 | |
160 | return 0; |
6658905f |
161 | } |