Merge pull request #969 from pwpiwi/gcc10_fixes
[legacy-proxmark3.git] / client / hid-flasher / flasher.c
blob2f0c20072d925389d2365923dc9082597cc19bc0
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 "proxusb.h"
13 #include "flash.h"
15 static void usage(char *argv0)
17 fprintf(stderr, "Usage: %s [-b] image.elf [image.elf...]\n\n", argv0);
18 fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
19 fprintf(stderr, "Example: %s path/to/osimage.elf path/to/fpgaimage.elf\n", argv0);
22 #define MAX_FILES 4
24 int main(int argc, char **argv)
26 int can_write_bl = 0;
27 int num_files = 0;
28 int res;
29 flash_file_t files[MAX_FILES];
31 memset(files, 0, sizeof(files));
33 if (argc < 2) {
34 usage(argv[0]);
35 return -1;
38 for (int i = 1; i < argc; i++) {
39 if (argv[i][0] == '-') {
40 if (!strcmp(argv[i], "-b")) {
41 can_write_bl = 1;
42 } else {
43 usage(argv[0]);
44 return -1;
46 } else {
47 res = flash_load(&files[num_files], argv[i], can_write_bl);
48 if (res < 0) {
49 fprintf(stderr, "Error while loading %s\n", argv[i]);
50 return -1;
52 fprintf(stderr, "\n");
53 num_files++;
57 usb_init();
59 fprintf(stderr, "Waiting for Proxmark to appear on USB...");
60 while (!OpenProxmark(1)) {
61 sleep(1);
62 fprintf(stderr, ".");
64 fprintf(stderr, " Found.\n");
66 res = flash_start_flashing(can_write_bl);
67 if (res < 0)
68 return -1;
70 fprintf(stderr, "\nFlashing...\n");
72 for (int i = 0; i < num_files; i++) {
73 res = flash_write(&files[i]);
74 if (res < 0)
75 return -1;
76 flash_free(&files[i]);
77 fprintf(stderr, "\n");
80 fprintf(stderr, "Resetting hardware...\n");
82 res = flash_stop_flashing();
83 if (res < 0)
84 return -1;
86 CloseProxmark();
88 fprintf(stderr, "All done.\n\n");
89 fprintf(stderr, "Have a nice day!\n");
91 return 0;