Mirror of CollapseOS
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

67 řádky
1.7KB

  1. #!/usr/bin/env python3
  2. # Read specified number of bytes at specified memory address and dump it to
  3. # stdout.
  4. import argparse
  5. import os
  6. import sys
  7. import time
  8. def sendcmd(fd, cmd):
  9. # The serial link echoes back all typed characters and expects us to read
  10. # them. We have to send each char one at a time.
  11. for c in cmd:
  12. os.write(fd, bytes([c]))
  13. os.read(fd, 1)
  14. os.write(fd, b'\n')
  15. os.read(fd, 2) # sends back \r\n
  16. def main():
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument('device')
  19. parser.add_argument('memptr')
  20. parser.add_argument('bytecount')
  21. args = parser.parse_args()
  22. try:
  23. memptr = int('0x' + args.memptr, 0)
  24. except ValueError:
  25. print("memptr are has to be hexadecimal without prefix.")
  26. return 1
  27. try:
  28. bytecount = int('0x' + args.bytecount, 0)
  29. except ValueError:
  30. print("bytecount are has to be hexadecimal without prefix.")
  31. return 1
  32. if memptr >= 0x10000:
  33. print("memptr out of range.")
  34. return 1
  35. if bytecount + memptr >= 0x10000:
  36. print("Bytecount too big.")
  37. return 1
  38. fd = os.open(args.device, os.O_RDWR)
  39. while bytecount > 0:
  40. sendcmd(fd, 'mptr {:04x}'.format(memptr).encode())
  41. os.read(fd, 9)
  42. toread = min(bytecount, 0xff)
  43. sendcmd(fd, 'peek {:x}'.format(toread).encode())
  44. peek = b''
  45. while len(peek) < toread * 2:
  46. peek += os.read(fd, 1)
  47. time.sleep(0.0001)
  48. os.read(fd, 5)
  49. while peek:
  50. c = peek[:2]
  51. sys.stdout.buffer.write(bytes([int(c, 16)]))
  52. peek = peek[2:]
  53. memptr += toread
  54. bytecount -= toread
  55. os.close(fd)
  56. return 0
  57. if __name__ == '__main__':
  58. sys.exit(main())