From 8ff4b18c51accad52331c01502d344b148faa49e Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sun, 8 Dec 2019 22:31:15 -0500 Subject: [PATCH] tools: add memdumpb In C this time. Python/Perl code is barely terser than C for these little tools. Why bother with interpreted? --- tools/.gitignore | 1 + tools/Makefile | 13 +++++++++ tools/memdump.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tools/.gitignore create mode 100644 tools/Makefile create mode 100644 tools/memdump.c diff --git a/tools/.gitignore b/tools/.gitignore new file mode 100644 index 0000000..14d909d --- /dev/null +++ b/tools/.gitignore @@ -0,0 +1 @@ +/memdumpb diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..577e358 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,13 @@ +CFLAGS ?= -Wall + +MEMDUMP_TGT = memdumpb +MEMDUMP_SRC = memdump.c + +all: ${MEMDUMP_TGT} + +${MEMDUMP_TGT}: ${MEMDUMP_SRC} + ${CC} ${CFLAGS} ${MEMDUMP_SRC} -o ${MEMDUMP_TGT} + +.PHONY: clean +clean: + rm -f ${MEMDUMP_TGT} diff --git a/tools/memdump.c b/tools/memdump.c new file mode 100644 index 0000000..7e13652 --- /dev/null +++ b/tools/memdump.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +/* Read specified number of bytes at specified memory address through a BASIC + * remote shell and dump it to stdout. + */ + +void sendcmd(int fd, char *cmd) +{ + char junk[2]; + while (*cmd) { + write(fd, cmd, 1); + read(fd, &junk, 1); + cmd++; + // The other side is sometimes much slower than us and if we don't let + // it breathe, it can choke. + usleep(1000); + } + write(fd, "\n", 1); + read(fd, &junk, 2); // sends back \r\n + usleep(1000); +} + +int main(int argc, char **argv) +{ + if (argc != 4) { + fprintf(stderr, "Usage: ./memdump device memptr bytecount\n"); + return 1; + } + unsigned int memptr = strtol(argv[2], NULL, 16); + unsigned int bytecount = strtol(argv[3], NULL, 16); + fprintf(stderr, "memptr: 0x%04x bytecount: 0x%04x.\n", memptr, bytecount); + if (memptr+bytecount > 0xffff) { + fprintf(stderr, "memptr+bytecount out of range.\n"); + return 1; + } + if (!bytecount) { + // nothing to spit + return 0; + } + + int fd = open(argv[1], O_RDWR|O_NOCTTY); + char s[0x20]; + sprintf(s, "m=0x%04x", memptr); + sendcmd(fd, s); + read(fd, s, 2); // read prompt + + for (int i=0; i