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