]> git.zerfleddert.de Git - proxmark3-svn/blame - client/flasher.c
CHG: moved to header file
[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>
4cd41f34 12#include "sleep.h"
125a98a1 13#include "proxmark3.h"
6e4d4ee6 14#include "flash.h"
28fdb04f 15#include "uart.h"
7838f4be 16#include "usb_cmd.h"
28fdb04f 17
13dbdd6b 18#ifdef _WIN32
19# define unlink(x)
20#endif
21
28fdb04f 22static serial_port sp;
23static char* serial_port_name;
24
25void cmd_debug(UsbCommand* UC) {
93048e8b 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
34 for (size_t i=0; i<16; i++)
35 printf("%02x",UC->d.asBytes[i]);
36
37 printf("...\n");
28fdb04f 38}
39
40void SendCommand(UsbCommand* txcmd) {
93048e8b 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 }
28fdb04f 47}
48
49void ReceiveCommand(UsbCommand* rxcmd) {
93048e8b 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)) {
56 prx += rxlen;
57 if ((prx-prxcmd) >= sizeof(UsbCommand)) {
58 return;
59 }
60 }
61 }
28fdb04f 62}
63
64void CloseProxmark() {
93048e8b 65 // Clean up the port
66 uart_close(sp);
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) {
93048e8b 72 sp = uart_open(serial_port_name);
73
74 //poll once a second
75 if (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT)
76 return 0;
77
78 return 1;
28fdb04f 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");
e12b82d3 85 fprintf(stderr, "\nExample (Linux):\n\n\t %s /dev/ttyACM0 armsrc/obj/fullimage.elf\n", argv0);
93048e8b 86 fprintf(stderr, "\nExample (OS) :\n\n\t %s /dev/cu.usbmodem1451 armsrc/obj/fullimage.elf\n", argv0);
5774b2b4 87 fprintf(stderr, "\nExample (WIN) :\n\n\t %s com3 armsrc/obj/fullimage.elf\n", argv0);
e12b82d3 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");
93048e8b 90 fprintf(stderr, " old ref --> http://code.google.com/p/proxmark3/wiki/Linux\n\n");
91 fprintf(stderr, " new refs --> ");
92 fprintf(stderr, " https://github.com/Proxmark/proxmark3/wiki/Gentoo Linux\n\n");
93 fprintf(stderr, " https://github.com/Proxmark/proxmark3/wiki/Ubuntu Linux\n\n");
94 fprintf(stderr, " https://github.com/Proxmark/proxmark3/wiki/OSX\n\n");
a5b1ba20 95}
6658905f 96
8fe1a992 97#define MAX_FILES 4
98
7fe9b0b7 99int main(int argc, char **argv)
100{
8fe1a992 101 int can_write_bl = 0;
102 int num_files = 0;
103 int res;
104 flash_file_t files[MAX_FILES];
105
106 memset(files, 0, sizeof(files));
107
28fdb04f 108 if (argc < 3) {
8fe1a992 109 usage(argv[0]);
110 return -1;
111 }
112
28fdb04f 113 for (int i = 2; i < argc; i++) {
8fe1a992 114 if (argv[i][0] == '-') {
115 if (!strcmp(argv[i], "-b")) {
116 can_write_bl = 1;
117 } else {
118 usage(argv[0]);
119 return -1;
120 }
121 } else {
122 res = flash_load(&files[num_files], argv[i], can_write_bl);
123 if (res < 0) {
124 fprintf(stderr, "Error while loading %s\n", argv[i]);
125 return -1;
126 }
127 fprintf(stderr, "\n");
128 num_files++;
129 }
130 }
131
93048e8b 132 serial_port_name = argv[1];
4a049b78 133
93048e8b 134 fprintf(stderr, "Waiting for Proxmark to appear on %s", serial_port_name);
135 do {
136 sleep(1);
137 fprintf(stderr, ".");
138 } while (!OpenProxmark(0));
139
140 fprintf(stderr," Found.\n");
141
142 res = flash_start_flashing(can_write_bl, serial_port_name);
8fe1a992 143 if (res < 0)
144 return -1;
145
146 fprintf(stderr, "\nFlashing...\n");
147
148 for (int i = 0; i < num_files; i++) {
149 res = flash_write(&files[i]);
150 if (res < 0)
151 return -1;
152 flash_free(&files[i]);
153 fprintf(stderr, "\n");
154 }
155
156 fprintf(stderr, "Resetting hardware...\n");
157
158 res = flash_stop_flashing();
159 if (res < 0)
160 return -1;
161
162 CloseProxmark();
163
164 fprintf(stderr, "All done.\n\n");
165 fprintf(stderr, "Have a nice day!\n");
8fe1a992 166 return 0;
6658905f 167}
Impressum, Datenschutz