changeset 285:bb28ba9e82c5

fc-fsio: file read primitive and hd command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 28 Feb 2014 06:16:02 +0000
parents 7b4d4e3e610a
children 146e7bf3fa4e
files rvinterf/etmsync/fileio.c rvinterf/etmsync/fscmdtab.c rvinterf/etmsync/fsread.c
diffstat 3 files changed, 85 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/etmsync/fileio.c	Fri Feb 28 05:50:01 2014 +0000
+++ b/rvinterf/etmsync/fileio.c	Fri Feb 28 06:16:02 2014 +0000
@@ -110,3 +110,49 @@
 	}
 	return(0);
 }
+
+do_file_read(pathname, databuf, rdsize, rdret)
+	char *pathname;
+	u_char *databuf;
+	int rdsize, *rdret;
+{
+	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
+	int rc, slen, sz;
+
+	slen = strlen(pathname);
+	if (slen >= TMFFS_STRING_SIZE) {
+		printf("error: pathname arg exceeds string length limit\n");
+		return(ERROR_USAGE);
+	}
+	if (rdsize > MAX_READ_DATA) {
+		fprintf(stderr,
+	"BUG: attempt to read more than possible per TMFFS2 protocol\n");
+		exit(ERROR_BUG);
+	}
+	dp = cmdpkt + 1;
+	*dp++ = ETM_FFS2;
+	*dp++ = TMFFS_FILE_READ;
+	*dp++ = slen + 1;
+	strcpy(dp, pathname);
+	dp += slen + 1;
+	*dp++ = rdsize;
+	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
+	if (rc)
+		return(rc);
+	if (rvi_msg[3]) {
+		printf("read file: FFS error %d\n", rvi_msg[3]);
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len < 6) {
+		*rdret = 0;
+		return(0);
+	}
+	sz = rvi_msg[4];
+	if (rvi_msg_len != sz + 6 || sz > rdsize) {
+		printf("error: read file response has wrong length\n");
+		return(ERROR_TARGET);
+	}
+	bcopy(rvi_msg + 5, databuf, sz);
+	*rdret = sz;
+	return(0);
+}
--- a/rvinterf/etmsync/fscmdtab.c	Fri Feb 28 05:50:01 2014 +0000
+++ b/rvinterf/etmsync/fscmdtab.c	Fri Feb 28 06:16:02 2014 +0000
@@ -7,6 +7,7 @@
 extern int cmd_exec();
 extern int cmd_exit();
 extern int cmd_ffs2ver();
+extern int cmd_hd();
 extern int cmd_ll();
 extern int cmd_ls();
 extern int cmd_stat();
@@ -15,6 +16,7 @@
 	{"exec", 1, 1, cmd_exec},
 	{"exit", 0, 0, cmd_exit},
 	{"ffs2ver", 0, 0, cmd_ffs2ver},
+	{"hd", 1, 1, cmd_hd},
 	{"ll", 1, 1, cmd_ll},
 	{"ls", 1, 1, cmd_ls},
 	{"stat", 1, 1, cmd_stat},
--- a/rvinterf/etmsync/fsread.c	Fri Feb 28 05:50:01 2014 +0000
+++ b/rvinterf/etmsync/fsread.c	Fri Feb 28 06:16:02 2014 +0000
@@ -91,9 +91,45 @@
 	return(0);
 }
 
+void
+hexdump_line(offset, buf, len)
+	u_char *buf;
+{
+	int i, c;
+
+	printf("%02X:  ", offset);
+	for (i = 0; i < 16; i++) {
+		if (i < len)
+			printf("%02X ", buf[i]);
+		else
+			fputs("   ", stdout);
+		if (i == 7 || i == 15)
+			putchar(' ');
+	}
+	for (i = 0; i < len; i++) {
+		c = buf[i];
+		if (c < ' ' || c > '~')
+			c = '.';
+		putchar(c);
+	}
+	putchar('\n');
+}
+
 cmd_hd(argc, argv)
 	char **argv;
 {
-
+	u_char databuf[MAX_READ_DATA];
+	int rc, sz, off, l;
 
+	rc = do_file_read(argv[1], databuf, MAX_READ_DATA, &sz);
+	if (rc)
+		return(rc);
+	printf("%d bytes read\n", sz);
+	for (off = 0; off < sz; off += 16) {
+		l = sz - off;
+		if (l > 16)
+			l = 16;
+		hexdump_line(off, databuf + off, l);
+	}
+	return(0);
 }