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