| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | #!/usr/bin/env python3import argparseimport sysfrom lib.dump import decode_dumpdef main():    # parse the arguments    ap = argparse.ArgumentParser(description="""        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.    """)    ap.add_argument('-i', dest='info', action='store_true',                    help='display crash info only')    ap.add_argument('dump')    ap.add_argument('output', nargs='?')    args = ap.parse_args()    # decode the dump data    dump = decode_dump(args.dump)    if dump is None:        return 1    # output descriptors    if args.info:        o_fd = None        o_md = sys.stdout    elif args.output is None:        o_fd = sys.stdout.buffer        o_md = sys.stderr    else:        o_fd = open(args.output, 'wb')        o_md = sys.stdout    # output binary    if o_fd:        o_fd.write(dump.data)        o_fd.close()    # metadata    print('   dump type: {typ}\n'          'crash reason: {reason}\n'          '   registers: {regs}\n'          '          PC: {pc}\n'          '          SP: {sp}\n'          '      ranges: {ranges}'.format(              typ=dump.typ,              reason=dump.reason.name if dump.reason is not None else 'N/A',              regs=dump.regs,              pc='{:#x}'.format(dump.pc) if dump.pc is not None else 'N/A',              sp='{:#x}'.format(dump.sp) if dump.sp is not None else 'N/A',              ranges=str(dump.ranges)),          file=o_md)if __name__ == '__main__':    exit(main())
 |