From 4b423a9dc68d41ba907c202c149c6f0a18a9476b Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 17 Jun 2019 09:02:01 -0400 Subject: [PATCH] tools/blkdump.py: new tool Also, rename "download.py" to "memdump.py". --- tools/blkdump.py | 59 +++++++++++++++++++++++++++++++ tools/{download.py => memdump.py} | 0 2 files changed, 59 insertions(+) create mode 100755 tools/blkdump.py rename tools/{download.py => memdump.py} (100%) diff --git a/tools/blkdump.py b/tools/blkdump.py new file mode 100755 index 0000000..793602f --- /dev/null +++ b/tools/blkdump.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +# Read specified number of bytes in specified blkdev ID and spit it to stdout. +# The proper blkdev has to be selected and placed already. + +import argparse +import os +import sys +import time + +# Some place where it's safe to write 0xff bytes. +MEMPTR = '9000' + +def sendcmd(fd, cmd): + # The serial link echoes back all typed characters and expects us to read + # them. We have to send each char one at a time. + print("Executing {}".format(cmd.decode()), file=sys.stderr) + for c in cmd: + os.write(fd, bytes([c])) + os.read(fd, 1) + os.write(fd, b'\n') + os.read(fd, 2) # sends back \r\n + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('device') + parser.add_argument('bytecount') + args = parser.parse_args() + + try: + bytecount = int(args.bytecount, 16) + except ValueError: + print("bytecount has to be hexadecimal without prefix.") + return 1 + fd = os.open(args.device, os.O_RDWR) + sendcmd(fd, 'mptr {}'.format(MEMPTR).encode()) + os.read(fd, 9) + while bytecount > 0: + toread = min(bytecount, 0x100) + sendcmd(fd, 'load {:x}'.format(toread & 0xff).encode()) + os.read(fd, 5) + sendcmd(fd, 'peek {:x}'.format(toread & 0xff).encode()) + peek = b'' + while len(peek) < toread * 2: + peek += os.read(fd, 1) + time.sleep(0.0001) + os.read(fd, 5) + while peek: + c = peek[:2] + sys.stdout.buffer.write(bytes([int(c, 16)])) + peek = peek[2:] + bytecount -= toread + os.close(fd) + return 0 + +if __name__ == '__main__': + sys.exit(main()) + diff --git a/tools/download.py b/tools/memdump.py similarity index 100% rename from tools/download.py rename to tools/memdump.py