]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/flasher.c
2e8bd2edb02372c201f09335af8ac336b7cbb115
[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 "sleep.h"
13 //#include "proxusb.h"
14 #include "flash.h"
15 #include "uart.h"
16 #include "usb_cmd.h"
17
18 static serial_port sp;
19 static char* serial_port_name;
20
21 void cmd_debug(UsbCommand* UC) {
22 // Debug
23 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
24 printf(" cmd[len=%zd]: %016llx\n",sizeof(UC->cmd),UC->cmd);
25 printf(" arg0[len=%zd]: %016llx\n",sizeof(UC->arg[0]),UC->arg[0]);
26 printf(" arg1[len=%zd]: %016llx\n",sizeof(UC->arg[1]),UC->arg[1]);
27 printf(" arg2[len=%zd]: %016llx\n",sizeof(UC->arg[2]),UC->arg[2]);
28 printf(" data[len=%zd]: ",sizeof(UC->d.asBytes));
29 for (size_t i=0; i<16; i++) {
30 printf("%02x",UC->d.asBytes[i]);
31 }
32 printf("...\n");
33 }
34
35 void SendCommand(UsbCommand* txcmd) {
36 // printf("send: ");
37 // cmd_debug(txcmd);
38 if (!uart_send(sp,(byte_t*)txcmd,sizeof(UsbCommand))) {
39 printf("Sending bytes to proxmark failed\n");
40 exit(1);
41 }
42 }
43
44 void ReceiveCommand(UsbCommand* rxcmd) {
45 byte_t* prxcmd = (byte_t*)rxcmd;
46 byte_t* prx = prxcmd;
47 size_t rxlen;
48 while (true) {
49 rxlen = sizeof(UsbCommand) - (prx-prxcmd);
50 if (uart_receive(sp,prx,&rxlen)) {
51 // printf("received [%zd] bytes\n",rxlen);
52 prx += rxlen;
53 if ((prx-prxcmd) >= sizeof(UsbCommand)) {
54 // printf("received: ");
55 // cmd_debug(rxcmd);
56 return;
57 }
58 }
59 }
60 }
61
62 void CloseProxmark() {
63 // Clean up the port
64 uart_close(sp);
65 }
66
67 int OpenProxmark(size_t i) {
68 sp = uart_open(serial_port_name);
69 if (sp == INVALID_SERIAL_PORT) {
70 return 0;
71 }
72 return 1;
73 }
74
75 static void usage(char *argv0)
76 {
77 fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
78 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
79 fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
80 }
81
82 #define MAX_FILES 4
83
84 int main(int argc, char **argv)
85 {
86 int can_write_bl = 0;
87 int num_files = 0;
88 int res;
89 flash_file_t files[MAX_FILES];
90
91 memset(files, 0, sizeof(files));
92
93 if (argc < 3) {
94 usage(argv[0]);
95 return -1;
96 }
97
98 for (int i = 2; i < argc; i++) {
99 if (argv[i][0] == '-') {
100 if (!strcmp(argv[i], "-b")) {
101 can_write_bl = 1;
102 } else {
103 usage(argv[0]);
104 return -1;
105 }
106 } else {
107 res = flash_load(&files[num_files], argv[i], can_write_bl);
108 if (res < 0) {
109 fprintf(stderr, "Error while loading %s\n", argv[i]);
110 return -1;
111 }
112 fprintf(stderr, "\n");
113 num_files++;
114 }
115 }
116
117 serial_port_name = argv[1];
118 fprintf(stderr, "Waiting for Proxmark to appear on USB...");
119 while (!OpenProxmark(0)) {
120 fprintf(stderr, ".");
121 }
122 fprintf(stderr, " Found.\n");
123
124 res = flash_start_flashing(can_write_bl);
125 if (res < 0)
126 return -1;
127
128 fprintf(stderr, "\nFlashing...\n");
129
130 for (int i = 0; i < num_files; i++) {
131 res = flash_write(&files[i]);
132 if (res < 0)
133 return -1;
134 flash_free(&files[i]);
135 fprintf(stderr, "\n");
136 }
137
138 fprintf(stderr, "Resetting hardware...\n");
139
140 res = flash_stop_flashing();
141 if (res < 0)
142 return -1;
143
144 CloseProxmark();
145
146 fprintf(stderr, "All done.\n\n");
147 fprintf(stderr, "Have a nice day!\n");
148
149 return 0;
150 }
Impressum, Datenschutz