xfimg2dump 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. import argparse
  3. import struct
  4. import os, sys
  5. from lib.dump import DUMP_MAGIC, DUMP_OFFSET, DUMP_SIZE
  6. def error(msg):
  7. print(msg, file=sys.stderr)
  8. def main():
  9. # parse the arguments
  10. ap = argparse.ArgumentParser(description="""
  11. Extract a crash dump from an external flash image and output
  12. the same format produced by the D21 g-code.
  13. """)
  14. ap.add_argument('image')
  15. args = ap.parse_args()
  16. # read the image
  17. off = DUMP_OFFSET
  18. with open(args.image, 'rb') as fd:
  19. fd.seek(off)
  20. data = fd.read(DUMP_SIZE)
  21. if len(data) != DUMP_SIZE:
  22. error('incorrect image size')
  23. return os.EX_DATAERR
  24. # check for magic header
  25. magic, = struct.unpack('<L', data[:4])
  26. if magic != DUMP_MAGIC:
  27. error('invalid dump magic or no dump')
  28. return os.EX_DATAERR
  29. # output D21 dump
  30. print('D21 - read crash dump', end='')
  31. for i in range(len(data)):
  32. if i % 16 == 0:
  33. print('\n{:06x} '.format(off + i), end='')
  34. print(' {:02x}'.format(data[i]), end='')
  35. print('\nok')
  36. if __name__ == '__main__':
  37. exit(main())