aboutsummaryrefslogtreecommitdiffstats
path: root/hktool.c
blob: dad7915d02960315cfc8f5e5660e6e3f9265cb64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * 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-2017 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 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 <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

#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: <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-2017 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;

	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;
}