]> git.zerfleddert.de Git - proxmark3-svn/blame - client/flasher.c
Merge pull request #600 from marshmellow42/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)
067bfc8b
MF
22#else
23# include <unistd.h>
13dbdd6b 24#endif
25
28fdb04f 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
3851172d 40void SendCommand(UsbCommand* txcmd) {
41// printf("send: ");
42// cmd_debug(txcmd);
f5ecd97b 43 if (!uart_send(sp,(uint8_t*)txcmd,sizeof(UsbCommand))) {
3851172d 44 printf("Sending bytes to proxmark failed\n");
45 exit(1);
46 }
47}
48
49void ReceiveCommand(UsbCommand* rxcmd) {
f5ecd97b 50 uint8_t* prxcmd = (uint8_t*)rxcmd;
51 uint8_t* prx = prxcmd;
3851172d 52 size_t rxlen;
53 while (true) {
54 if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-prxcmd), &rxlen)) {
55 prx += rxlen;
56 if ((prx-prxcmd) >= sizeof(UsbCommand)) {
57 return;
58 }
59 }
60 }
61}
62
8fe1a992 63static void usage(char *argv0)
a5b1ba20 64{
28fdb04f 65 fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
8fe1a992 66 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
f5ecd97b 67 fprintf(stderr, "\nExample:\n\n\t %s "SERIAL_PORT_H" armsrc/obj/fullimage.elf\n", argv0);
68#ifdef __linux__
69 fprintf(stderr, "\nNote (Linux): if the flasher gets stuck at 'Waiting for Proxmark to reappear',\n");
70 fprintf(stderr, " you may need to blacklist proxmark for modem-manager. v1.4.14 and later\n");
71 fprintf(stderr, " include this configuration patch already. The change can be found at:\n");
72 fprintf(stderr, " https://cgit.freedesktop.org/ModemManager/ModemManager/commit/?id=6e7ff47\n\n");
73#endif
a5b1ba20 74}
6658905f 75
8fe1a992 76#define MAX_FILES 4
77
7fe9b0b7 78int main(int argc, char **argv)
79{
8fe1a992 80 int can_write_bl = 0;
81 int num_files = 0;
82 int res;
83 flash_file_t files[MAX_FILES];
84
85 memset(files, 0, sizeof(files));
86
28fdb04f 87 if (argc < 3) {
8fe1a992 88 usage(argv[0]);
89 return -1;
90 }
91
28fdb04f 92 for (int i = 2; i < argc; i++) {
8fe1a992 93 if (argv[i][0] == '-') {
94 if (!strcmp(argv[i], "-b")) {
95 can_write_bl = 1;
96 } else {
97 usage(argv[0]);
98 return -1;
99 }
100 } else {
101 res = flash_load(&files[num_files], argv[i], can_write_bl);
102 if (res < 0) {
103 fprintf(stderr, "Error while loading %s\n", argv[i]);
104 return -1;
105 }
106 fprintf(stderr, "\n");
107 num_files++;
108 }
109 }
110
f5ecd97b 111 char* serial_port_name = argv[1];
112
113 fprintf(stderr,"Waiting for Proxmark to appear on %s", serial_port_name);
114 do {
115 msleep(1000);
116 fprintf(stderr, ".");
117 } while (!OpenProxmark(0, serial_port_name));
118 fprintf(stderr," Found.\n");
8fe1a992 119
f5ecd97b 120 res = flash_start_flashing(can_write_bl, serial_port_name);
8fe1a992 121 if (res < 0)
122 return -1;
123
124 fprintf(stderr, "\nFlashing...\n");
125
126 for (int i = 0; i < num_files; i++) {
127 res = flash_write(&files[i]);
128 if (res < 0)
129 return -1;
130 flash_free(&files[i]);
131 fprintf(stderr, "\n");
132 }
133
134 fprintf(stderr, "Resetting hardware...\n");
135
136 res = flash_stop_flashing();
137 if (res < 0)
138 return -1;
139
f5ecd97b 140 CloseProxmark(serial_port_name);
8fe1a992 141
142 fprintf(stderr, "All done.\n\n");
143 fprintf(stderr, "Have a nice day!\n");
144
145 return 0;
6658905f 146}
Impressum, Datenschutz