| 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 | //----------------------------------------------------------------------------- |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include "proxusb.h" |
| 13 | #include "flash.h" |
| 14 | |
| 15 | static void usage(char *argv0) |
| 16 | { |
| 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); |
| 20 | } |
| 21 | |
| 22 | #define MAX_FILES 4 |
| 23 | |
| 24 | int main(int argc, char **argv) |
| 25 | { |
| 26 | int can_write_bl = 0; |
| 27 | int num_files = 0; |
| 28 | int res; |
| 29 | flash_file_t files[MAX_FILES]; |
| 30 | |
| 31 | memset(files, 0, sizeof(files)); |
| 32 | |
| 33 | if (argc < 2) { |
| 34 | usage(argv[0]); |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 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; |
| 45 | } |
| 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; |
| 51 | } |
| 52 | fprintf(stderr, "\n"); |
| 53 | num_files++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | usb_init(); |
| 58 | |
| 59 | fprintf(stderr, "Waiting for Proxmark to appear on USB..."); |
| 60 | while (!OpenProxmark(1)) { |
| 61 | sleep(1); |
| 62 | fprintf(stderr, "."); |
| 63 | } |
| 64 | fprintf(stderr, " Found.\n"); |
| 65 | |
| 66 | res = flash_start_flashing(can_write_bl); |
| 67 | if (res < 0) |
| 68 | return -1; |
| 69 | |
| 70 | fprintf(stderr, "\nFlashing...\n"); |
| 71 | |
| 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"); |
| 78 | } |
| 79 | |
| 80 | fprintf(stderr, "Resetting hardware...\n"); |
| 81 | |
| 82 | res = flash_stop_flashing(); |
| 83 | if (res < 0) |
| 84 | return -1; |
| 85 | |
| 86 | CloseProxmark(); |
| 87 | |
| 88 | fprintf(stderr, "All done.\n\n"); |
| 89 | fprintf(stderr, "Have a nice day!\n"); |
| 90 | |
| 91 | return 0; |
| 92 | } |