From 1b22aac9fcc68662cf22129bafc02c8197471426 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Thu, 1 Jul 2021 13:16:02 +0200 Subject: [PATCH] tools: add xfimg2dump --- tools/README.md | 4 ++++ tools/lib/dump.py | 6 ++++-- tools/xfimg2dump | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100755 tools/xfimg2dump diff --git a/tools/README.md b/tools/README.md index 63c1731d..50516dac 100644 --- a/tools/README.md +++ b/tools/README.md @@ -34,6 +34,10 @@ Requires Python3 and the [pyelftools](https://github.com/eliben/pyelftools) modu Parse and decode a memory dump obtained from the D2/D21/D23 g-code into readable metadata and binary. The output binary is padded and extended to fit the original address range. +### ``xfimg2dump`` + +Extract a crash dump from an external flash image and output the same format produced by the D21 g-code. + ### ``update_eeprom`` Given one EEPROM dump, convert the dump to update instructions that can be sent to a printer. diff --git a/tools/lib/dump.py b/tools/lib/dump.py index 5d2ea724..7ce75606 100644 --- a/tools/lib/dump.py +++ b/tools/lib/dump.py @@ -5,8 +5,10 @@ import struct from . import avr -FILL_BYTE = b'\0' # used to fill memory gaps in the dump -DUMP_MAGIC = 0x55525547 # XFLASH dump magic +FILL_BYTE = b'\0' # used to fill memory gaps in the dump +DUMP_MAGIC = 0x55525547 # XFLASH dump magic +DUMP_OFFSET = 0x3d000 # XFLASH dump offset +DUMP_SIZE = 0x2300 # XFLASH dump size class CrashReason(enum.IntEnum): MANUAL = 0 diff --git a/tools/xfimg2dump b/tools/xfimg2dump new file mode 100755 index 00000000..e1fe75ac --- /dev/null +++ b/tools/xfimg2dump @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import argparse +import struct +import os, sys + +from lib.dump import DUMP_MAGIC, DUMP_OFFSET, DUMP_SIZE + + +def error(msg): + print(msg, file=sys.stderr) + +def main(): + # parse the arguments + ap = argparse.ArgumentParser(description=""" + Extract a crash dump from an external flash image and output + the same format produced by the D21 g-code. + """) + ap.add_argument('image') + args = ap.parse_args() + + # read the image + off = DUMP_OFFSET + with open(args.image, 'rb') as fd: + fd.seek(off) + data = fd.read(DUMP_SIZE) + if len(data) != DUMP_SIZE: + error('incorrect image size') + return os.EX_DATAERR + + # check for magic header + magic, = struct.unpack('