]>
Commit | Line | Data |
---|---|---|
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 "sleep.h" | |
13 | #include "proxmark3.h" | |
14 | #include "flash.h" | |
15 | #include "uart.h" | |
16 | #include "usb_cmd.h" | |
17 | ||
18 | #ifdef _WIN32 | |
19 | # define unlink(x) | |
20 | #endif | |
21 | ||
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)); | |
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]); | |
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)) { | |
55 | // printf("received [%zd] bytes\n",rxlen); | |
56 | prx += rxlen; | |
57 | if ((prx-prxcmd) >= sizeof(UsbCommand)) { | |
58 | // printf("received: "); | |
59 | // cmd_debug(rxcmd); | |
60 | return; | |
61 | } | |
62 | } | |
63 | } | |
64 | } | |
65 | ||
66 | void CloseProxmark() { | |
67 | // Clean up the port | |
68 | uart_close(sp); | |
69 | // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/* | |
70 | unlink(serial_port_name); | |
71 | } | |
72 | ||
73 | int OpenProxmark(size_t i) { | |
74 | sp = uart_open(serial_port_name); | |
75 | if (sp == INVALID_SERIAL_PORT) { | |
76 | //poll once a second | |
77 | msleep(100); | |
78 | return 0; | |
79 | } | |
80 | return 1; | |
81 | } | |
82 | ||
83 | static void usage(char *argv0) | |
84 | { | |
85 | fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0); | |
86 | fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n"); | |
87 | fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0); | |
88 | } | |
89 | ||
90 | #define MAX_FILES 4 | |
91 | ||
92 | int main(int argc, char **argv) | |
93 | { | |
94 | int can_write_bl = 0; | |
95 | int num_files = 0; | |
96 | int res; | |
97 | flash_file_t files[MAX_FILES]; | |
98 | ||
99 | memset(files, 0, sizeof(files)); | |
100 | ||
101 | if (argc < 3) { | |
102 | usage(argv[0]); | |
103 | return -1; | |
104 | } | |
105 | ||
106 | for (int i = 2; i < argc; i++) { | |
107 | if (argv[i][0] == '-') { | |
108 | if (!strcmp(argv[i], "-b")) { | |
109 | can_write_bl = 1; | |
110 | } else { | |
111 | usage(argv[0]); | |
112 | return -1; | |
113 | } | |
114 | } else { | |
115 | res = flash_load(&files[num_files], argv[i], can_write_bl); | |
116 | if (res < 0) { | |
117 | fprintf(stderr, "Error while loading %s\n", argv[i]); | |
118 | return -1; | |
119 | } | |
120 | fprintf(stderr, "\n"); | |
121 | num_files++; | |
122 | } | |
123 | } | |
124 | ||
125 | serial_port_name = argv[1]; | |
126 | fprintf(stderr, "Waiting for Proxmark to appear on USB..."); | |
127 | while (!OpenProxmark(0)) { | |
128 | fprintf(stderr, "."); | |
129 | } | |
130 | fprintf(stderr, " Found.\n"); | |
131 | ||
132 | res = flash_start_flashing(can_write_bl); | |
133 | if (res < 0) | |
134 | return -1; | |
135 | ||
136 | fprintf(stderr, "\nFlashing...\n"); | |
137 | ||
138 | for (int i = 0; i < num_files; i++) { | |
139 | res = flash_write(&files[i]); | |
140 | if (res < 0) | |
141 | return -1; | |
142 | flash_free(&files[i]); | |
143 | fprintf(stderr, "\n"); | |
144 | } | |
145 | ||
146 | fprintf(stderr, "Resetting hardware...\n"); | |
147 | ||
148 | res = flash_stop_flashing(); | |
149 | if (res < 0) | |
150 | return -1; | |
151 | ||
152 | CloseProxmark(); | |
153 | ||
154 | fprintf(stderr, "All done.\n\n"); | |
155 | fprintf(stderr, "Have a nice day!\n"); | |
156 | ||
157 | return 0; | |
158 | } |