aboutsummaryrefslogtreecommitdiffstats
path: root/hktool.c
diff options
context:
space:
mode:
Diffstat (limited to 'hktool.c')
-rw-r--r--hktool.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/hktool.c b/hktool.c
index cee133e..98457b7 100644
--- a/hktool.c
+++ b/hktool.c
@@ -19,10 +19,12 @@
*/
#include <errno.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdbool.h>
+#include <string.h>
#include <unistd.h>
+#include <assert.h>
#include "halfkay.h"
#include "eprintf.h"
@@ -31,6 +33,9 @@
#ifndef VERSION
#define VERSION "unknown version"
#endif
+#ifndef DATADIR
+#define DATADIR "hktool"
+#endif
static const char *usage = "[-hlvr] [-f file] [--] mcu";
@@ -63,12 +68,16 @@ static void version(void)
int main(int argc, char **argv)
{
bool doflash = false, doreboot = false;
- char *flashfile = NULL;
+ char *flashfile = NULL, *mcuname;
int opt;
struct flashparams fp;
setprogname(argv[0] != NULL ? argv[0] : "hktool");
+ datadir = getenv("HKTOOL_DATADIR");
+ if (datadir == NULL || strlen(datadir) == 0)
+ datadir = DATADIR;
+
while (opt = getopt(argc, argv, "f:h-lvr"), opt != -1) {
switch (opt) {
case 'f':
@@ -81,7 +90,7 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
break;
case 'l':
- getparams(NULL, NULL);
+ listparams();
return EXIT_SUCCESS;
break;
case 'v':
@@ -102,6 +111,7 @@ int main(int argc, char **argv)
if (optind >= argc)
eprintf("No MCU specified\nUsage: %s %s", argv[0], usage);
+ mcuname = argv[optind];
if (!doflash && !doreboot)
eprintf("Nothing to do");
@@ -109,8 +119,19 @@ int main(int argc, char **argv)
if (argc - optind > 1)
eprintf("Invalid number of arguments\nUsage: %s %s", argv[0], usage);
- if (getparams(&fp, argv[optind]) == false)
- eprintf("Could not find flash parameters for '%s'", argv[optind]);
+ if (strchr(mcuname, '/') == NULL) {
+ int size;
+ char *path;
+
+ size = snprintf(NULL, 0, "%s/%s", datadir, mcuname);
+ assert(size > 0);
+ path = emalloc((size_t)size + 1);
+ snprintf(path, size, "%s/%s", datadir, mcuname);
+ mcuname = path;
+ }
+ readparams(&fp, mcuname);
+ if (mcuname != argv[optind])
+ free(mcuname);
if (doflash)
if (flash(&fp, flashfile) != 0)