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