changeset 45:16315ed6401a

init-script logic implemented in fc-xram
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 17 Jun 2013 08:15:30 +0000
parents 5ca0ad4003a0
children eb97cfe5e31a
files loadtools/Makefile loadtools/clmain.c loadtools/initscript.c loadtools/ltmain.c
diffstat 4 files changed, 132 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/loadtools/Makefile	Mon Jun 17 07:32:29 2013 +0000
+++ b/loadtools/Makefile	Mon Jun 17 08:15:30 2013 +0000
@@ -12,8 +12,9 @@
 		ltdump.o ltexit.o ltmain.o ltpassthru.o ltscript.o romload.o \
 		sercomm.o srecreader.o tpinterf.o tpinterf2.o
 
-XRAM_OBJS=	chainload.o clmain.o defpath.o hexdecode.o hwparam.o romload.o \
-		sercomm.o srecreader.o tpinterf.o ttypassthru.o
+XRAM_OBJS=	chainload.o clmain.o defpath.o hexdecode.o hwparam.o \
+		initscript.o romload.o sercomm.o srecreader.o tpinterf.o \
+		ttypassthru.o
 
 all:	${PROGS}
 
--- a/loadtools/clmain.c	Mon Jun 17 07:32:29 2013 +0000
+++ b/loadtools/clmain.c	Mon Jun 17 08:15:30 2013 +0000
@@ -14,6 +14,7 @@
 extern struct srecreader iramimage;
 extern char default_loadagent_image[];
 extern struct srecreader xramimage;
+extern char hw_init_script[];
 
 main(argc, argv)
 	char **argv;
@@ -54,7 +55,12 @@
 	/* loadagent should be running now */
 	if (tpinterf_pass_output(1) < 0)
 		exit(1);
-	/* hw_init_script execution will go here */
+	if (hw_init_script[0]) {
+		printf("Executing init script %s\n", hw_init_script);
+		c = exec_init_script(hw_init_script);
+		if (c)
+			exit(1);
+	}
 	printf("Sending XRAM image to loadagent\n");
 	perform_chain_load();
 	tty_passthru();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/initscript.c	Mon Jun 17 08:15:30 2013 +0000
@@ -0,0 +1,121 @@
+/*
+ * This module has been copied from ltscript.c, ltdispatch.c and ltpassthru.c:
+ * here we implement the init-script functionality for fc-xram.
+ */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+
+extern char default_helpers_dir[];
+
+static
+loadagent_cmd(argc, argv)
+	char **argv;
+{
+	if (tpinterf_make_cmd(argv) < 0) {
+		fprintf(stderr, "error: unable to form target command\n");
+		return(-1);
+	}
+	if (tpinterf_send_cmd() < 0)
+		return(-1);
+	return tpinterf_pass_output(1);
+}
+
+static struct cmdtab {
+	char *cmd;
+	int minargs;
+	int maxargs;
+	int (*func)();
+} cmdtab[] = {
+	{"w8", 2, 2, loadagent_cmd},
+	{"w16", 2, 2, loadagent_cmd},
+	{"w32", 2, 2, loadagent_cmd},
+	{0, 0, 0, 0}
+};
+
+static
+dispatch_cmd(cmd)
+	char *cmd;
+{
+	char *argv[10];
+	char *cp, **ap;
+	struct cmdtab *tp;
+
+	for (cp = cmd; isspace(*cp); cp++)
+		;
+	if (!*cp || *cp == '#')
+		return(0);
+	printf("init-script command: %s\n", cp);
+	argv[0] = cp;
+	while (*cp && !isspace(*cp))
+		cp++;
+	if (*cp)
+		*cp++ = '\0';
+	for (tp = cmdtab; tp->cmd; tp++)
+		if (!strcmp(tp->cmd, argv[0]))
+			break;
+	if (!tp->func) {
+		fprintf(stderr, "error: no such command\n");
+		return(-1);
+	}
+	for (ap = argv + 1; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (!*cp || *cp == '#')
+			break;
+		if (ap - argv - 1 > tp->maxargs) {
+			fprintf(stderr, "error: too many arguments\n");
+			return(-1);
+		}
+		*ap++ = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	if (ap - argv - 1 < tp->minargs) {
+		fprintf(stderr, "error: too few arguments\n");
+		return(-1);
+	}
+	*ap = 0;
+	return tp->func(ap - argv, argv);
+}
+
+exec_init_script(script_name)
+	char *script_name;
+{
+	char pathbuf[MAXPATHLEN], *openfname;
+	FILE *f;
+	char linebuf[512], *cp;
+	int lineno, retval = 0;
+
+	if (index(script_name, '/'))
+		openfname = script_name;
+	else {
+		sprintf(pathbuf, "%s/%s", default_helpers_dir, script_name);
+		openfname = pathbuf;
+	}
+	f = fopen(openfname, "r");
+	if (!f) {
+		perror(openfname);
+		return(-1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
+		cp = index(linebuf, '\n');
+		if (!cp) {
+			fprintf(stderr, "%s line %d: missing newline\n",
+				openfname, lineno);
+			fclose(f);
+			return(-1);
+		}
+		*cp = '\0';
+		retval = dispatch_cmd(linebuf);
+		if (retval)
+			break;
+	}
+	fclose(f);
+	return(retval);
+}
--- a/loadtools/ltmain.c	Mon Jun 17 07:32:29 2013 +0000
+++ b/loadtools/ltmain.c	Mon Jun 17 08:15:30 2013 +0000
@@ -55,7 +55,7 @@
 	if (tpinterf_pass_output(1) < 0)
 		exit(1);
 	putchar('\n');
-	if (hw_init_script) {
+	if (hw_init_script[0]) {
 		printf("Executing init script %s\n", hw_init_script);
 		loadtool_exec_script(hw_init_script);
 	}