From 464847c5b25588448e550017a6d91447c076b944 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 23 Nov 2016 21:23:09 +0000 Subject: init commit --- hktool.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 hktool.c (limited to 'hktool.c') diff --git a/hktool.c b/hktool.c new file mode 100644 index 0000000..3a5f3ab --- /dev/null +++ b/hktool.c @@ -0,0 +1,124 @@ +/* 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include +#include +#include +#include +#include + +#include "halfkay.h" +#include "log.h" +#include "params.h" + +#ifndef VERSION +#define VERSION "unknown version" +#endif + +#define 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; + + argv0 = 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 " USAGE "\n", argv[0]); + 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 " USAGE "\n", argv[0]); + return EXIT_FAILURE; + break; + default: + error(0, "getopt returned unexpected value"); + } + } + + if (optind >= argc) + error(0, "No MCU specified\nUsage: %s " USAGE, argv[0]); + + if (!doflash && !doreboot) + error(0, "Nothing to do"); + + if (argc - optind > 1) + error(0, "Invalid number of arguments\nUsage: %s " USAGE, argv[0]); + + if (getparams(&fp, argv[optind]) != 0) + return EXIT_FAILURE; + + if (doflash) { + printf("flashing\n"); + if (flash(&fp, flashfile) != 0) + return EXIT_FAILURE; + } + if (doreboot) { + printf("rebooting\n"); + if (reboot(&fp) != 0) + return EXIT_FAILURE; + } +} -- cgit v1.2.3-54-g00ecf