]> git.zerfleddert.de Git - proxmark3-svn/blame - client/flasher.c
trying to tweak uart code, for better flashing
[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"
16#include "usb_cmd.h"
17
18static serial_port sp;
19static char* serial_port_name;
20
21void cmd_debug(UsbCommand* UC) {
22 // Debug
23 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
125a98a1 24 printf(" cmd[len=%zd]: %016"llx"\n",sizeof(UC->cmd),UC->cmd);
25 printf(" arg0[len=%zd]: %016"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
26 printf(" arg1[len=%zd]: %016"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
27 printf(" arg2[len=%zd]: %016"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
28fdb04f 28 printf(" data[len=%zd]: ",sizeof(UC->d.asBytes));
29 for (size_t i=0; i<16; i++) {
30 printf("%02x",UC->d.asBytes[i]);
31 }
32 printf("...\n");
33}
34
35void SendCommand(UsbCommand* txcmd) {
36// printf("send: ");
37// cmd_debug(txcmd);
38 if (!uart_send(sp,(byte_t*)txcmd,sizeof(UsbCommand))) {
39 printf("Sending bytes to proxmark failed\n");
40 exit(1);
41 }
42}
43
44void ReceiveCommand(UsbCommand* rxcmd) {
45 byte_t* prxcmd = (byte_t*)rxcmd;
46 byte_t* prx = prxcmd;
47 size_t rxlen;
48 while (true) {
49 rxlen = sizeof(UsbCommand) - (prx-prxcmd);
50 if (uart_receive(sp,prx,&rxlen)) {
51// printf("received [%zd] bytes\n",rxlen);
52 prx += rxlen;
53 if ((prx-prxcmd) >= sizeof(UsbCommand)) {
54// printf("received: ");
55// cmd_debug(rxcmd);
56 return;
57 }
58 }
59 }
60}
61
62void CloseProxmark() {
63 // Clean up the port
64 uart_close(sp);
65}
66
67int OpenProxmark(size_t i) {
68 sp = uart_open(serial_port_name);
69 if (sp == INVALID_SERIAL_PORT) {
c1e745e4 70 //poll once a second
71 msleep(100);
28fdb04f 72 return 0;
73 }
74 return 1;
75}
6658905f 76
8fe1a992 77static void usage(char *argv0)
a5b1ba20 78{
28fdb04f 79 fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
8fe1a992 80 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
81 fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
a5b1ba20 82}
6658905f 83
8fe1a992 84#define MAX_FILES 4
85
7fe9b0b7 86int main(int argc, char **argv)
87{
8fe1a992 88 int can_write_bl = 0;
89 int num_files = 0;
90 int res;
91 flash_file_t files[MAX_FILES];
92
93 memset(files, 0, sizeof(files));
94
28fdb04f 95 if (argc < 3) {
8fe1a992 96 usage(argv[0]);
97 return -1;
98 }
99
28fdb04f 100 for (int i = 2; i < argc; i++) {
8fe1a992 101 if (argv[i][0] == '-') {
102 if (!strcmp(argv[i], "-b")) {
103 can_write_bl = 1;
104 } else {
105 usage(argv[0]);
106 return -1;
107 }
108 } else {
109 res = flash_load(&files[num_files], argv[i], can_write_bl);
110 if (res < 0) {
111 fprintf(stderr, "Error while loading %s\n", argv[i]);
112 return -1;
113 }
114 fprintf(stderr, "\n");
115 num_files++;
116 }
117 }
118
28fdb04f 119 serial_port_name = argv[1];
8fe1a992 120 fprintf(stderr, "Waiting for Proxmark to appear on USB...");
121 while (!OpenProxmark(0)) {
8fe1a992 122 fprintf(stderr, ".");
123 }
124 fprintf(stderr, " Found.\n");
125
126 res = flash_start_flashing(can_write_bl);
127 if (res < 0)
128 return -1;
129
130 fprintf(stderr, "\nFlashing...\n");
131
132 for (int i = 0; i < num_files; i++) {
133 res = flash_write(&files[i]);
134 if (res < 0)
135 return -1;
136 flash_free(&files[i]);
137 fprintf(stderr, "\n");
138 }
139
140 fprintf(stderr, "Resetting hardware...\n");
141
142 res = flash_stop_flashing();
143 if (res < 0)
144 return -1;
145
146 CloseProxmark();
147
148 fprintf(stderr, "All done.\n\n");
149 fprintf(stderr, "Have a nice day!\n");
150
151 return 0;
6658905f 152}
Impressum, Datenschutz