dump2bin 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os, sys
  4. from lib.dump import decode_dump
  5. def main():
  6. # parse the arguments
  7. ap = argparse.ArgumentParser(description="""
  8. Parse and decode a memory dump obtained from the D2/D21/D23 g-code
  9. into readable metadata and binary. The output binary is padded and
  10. extended to fit the original address range.
  11. """)
  12. ap.add_argument('-i', dest='info', action='store_true',
  13. help='display crash info only')
  14. ap.add_argument('dump')
  15. ap.add_argument('output', nargs='?')
  16. args = ap.parse_args()
  17. # decode the dump data
  18. dump = decode_dump(args.dump)
  19. if dump is None:
  20. return os.EX_DATAERR
  21. # output descriptors
  22. if args.info:
  23. o_fd = None
  24. o_md = sys.stdout
  25. elif args.output is None:
  26. o_fd = sys.stdout.buffer
  27. o_md = sys.stderr
  28. else:
  29. o_fd = open(args.output, 'wb')
  30. o_md = sys.stdout
  31. # output binary
  32. if o_fd:
  33. o_fd.write(dump.data)
  34. o_fd.close()
  35. # metadata
  36. print(' dump type: {typ}\n'
  37. 'crash reason: {reason}\n'
  38. ' registers: {regs}\n'
  39. ' PC: {pc}\n'
  40. ' SP: {sp}\n'
  41. ' ranges: {ranges}'.format(
  42. typ=dump.typ,
  43. reason=dump.reason.name if dump.reason is not None else 'N/A',
  44. regs=dump.regs,
  45. pc='{:#x}'.format(dump.pc) if dump.pc is not None else 'N/A',
  46. sp='{:#x}'.format(dump.sp) if dump.sp is not None else 'N/A',
  47. ranges=str(dump.ranges)),
  48. file=o_md)
  49. if __name__ == '__main__':
  50. exit(main())