Merge pull request #107 from p-l-/fix-hfmf
[legacy-proxmark3.git] / client / hid-flasher / flasher.c
bloba4a0e85e7c73254e360fef2add6a1420aa652f12
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 //-----------------------------------------------------------------------------
6 // Flasher frontend tool
7 //-----------------------------------------------------------------------------
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "sleep.h"
13 #include "proxusb.h"
14 #include "flash.h"
16 static void usage(char *argv0)
18 fprintf(stderr, "Usage: %s [-b] image.elf [image.elf...]\n\n", argv0);
19 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
20 fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
23 #define MAX_FILES 4
25 int main(int argc, char **argv)
27 int can_write_bl = 0;
28 int num_files = 0;
29 int res;
30 flash_file_t files[MAX_FILES];
32 memset(files, 0, sizeof(files));
34 if (argc < 2) {
35 usage(argv[0]);
36 return -1;
39 for (int i = 1; i < argc; i++) {
40 if (argv[i][0] == '-') {
41 if (!strcmp(argv[i], "-b")) {
42 can_write_bl = 1;
43 } else {
44 usage(argv[0]);
45 return -1;
47 } else {
48 res = flash_load(&files[num_files], argv[i], can_write_bl);
49 if (res < 0) {
50 fprintf(stderr, "Error while loading %s\n", argv[i]);
51 return -1;
53 fprintf(stderr, "\n");
54 num_files++;
58 usb_init();
60 fprintf(stderr, "Waiting for Proxmark to appear on USB...");
61 while (!OpenProxmark(1)) {
62 sleep(1);
63 fprintf(stderr, ".");
65 fprintf(stderr, " Found.\n");
67 res = flash_start_flashing(can_write_bl);
68 if (res < 0)
69 return -1;
71 fprintf(stderr, "\nFlashing...\n");
73 for (int i = 0; i < num_files; i++) {
74 res = flash_write(&files[i]);
75 if (res < 0)
76 return -1;
77 flash_free(&files[i]);
78 fprintf(stderr, "\n");
81 fprintf(stderr, "Resetting hardware...\n");
83 res = flash_stop_flashing();
84 if (res < 0)
85 return -1;
87 CloseProxmark();
89 fprintf(stderr, "All done.\n\n");
90 fprintf(stderr, "Have a nice day!\n");
92 return 0;