/* * HalfKay loader - Load binary data to HalfKay protocol based MCUs. * Based on Teensy Loader (cli) (http://www.pjrc.com/teensy/loader_cli.html) * * Copyright (C) 2016 Tomasz Kramkowski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "halfkay.h" #include "eprintf.h" #include "params.h" #ifndef VERSION #define VERSION "unknown version" #endif static const char *usage = "[-hlvr] [-f file] [--] mcu"; static void help(void) { fprintf(stderr, "Options:\n" " -f file Specify binary file to flash to the device.\n" " -h Show this help.\n" " -l Show a list of supported devices.\n" " -v Show version and license information.\n" " -r Reboot the device.\n" "\n" "Please check the hktool(1) man page for more information.\n" "Report bugs to: \n" "hktool home page: https://the-tk.com/projects/hktool\n"); } static void version(void) { fprintf(stderr, "hktool %s\n" "Copyright (C) 2016 Tomasz Kramkowski \n" "License GPLv3: GNU GPL version 3 or later \n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n", VERSION); } int main(int argc, char **argv) { bool doflash = false, doreboot = false; char *flashfile = NULL; int opt; struct flashparams fp; setprogname(argv[0] != NULL ? argv[0] : "hktool"); while (opt = getopt(argc, argv, "f:h-lvr"), opt != -1) { switch (opt) { case 'f': doflash = true; flashfile = optarg; break; case 'h': fprintf(stderr, "Usage: %s %s\n", argv[0], usage); help(); return EXIT_SUCCESS; break; case 'l': listparams(); return EXIT_SUCCESS; break; case 'v': version(); return EXIT_SUCCESS; break; case 'r': doreboot = true; break; case '?': fprintf(stderr, "Usage: %s %s\n", argv[0], usage); return EXIT_FAILURE; break; default: eprintf("getopt returned unexpected value"); } } if (optind >= argc) eprintf("No MCU specified\nUsage: %s %s", argv[0], usage); if (!doflash && !doreboot) eprintf("Nothing to do"); if (argc - optind > 1) eprintf("Invalid number of arguments\nUsage: %s %s", argv[0], usage); if (getparams(&fp, argv[optind]) != 0) return EXIT_FAILURE; if (doflash) if (flash(&fp, flashfile) != 0) return EXIT_FAILURE; if (doreboot) if (reboot(&fp) != 0) return EXIT_FAILURE; }