aboutsummaryrefslogtreecommitdiffstats
path: root/hktool.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2016-11-23 21:23:09 +0000
committerTomasz Kramkowski <tk@the-tk.com>2016-11-23 21:23:30 +0000
commit464847c5b25588448e550017a6d91447c076b944 (patch)
treecb48ce0793232ab0046ac1ad18ad50862ed77f5c /hktool.c
downloadhktool-464847c5b25588448e550017a6d91447c076b944.tar.gz
hktool-464847c5b25588448e550017a6d91447c076b944.tar.xz
hktool-464847c5b25588448e550017a6d91447c076b944.zip
init commit
Diffstat (limited to 'hktool.c')
-rw-r--r--hktool.c124
1 files changed, 124 insertions, 0 deletions
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 <tk@the-tk.com>
+ *
+ * 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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#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: <hktool.bugs@the-tk.com>\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 <tk@the-tk.com>\n"
+ "License GPLv3: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\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;
+ }
+}