Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6KB

  1. #!/usr/bin/env python3
  2. # Read specified number of bytes in specified blkdev ID and spit it to stdout.
  3. # The proper blkdev has to be selected and placed already.
  4. import argparse
  5. import os
  6. import sys
  7. import time
  8. # Some place where it's safe to write 0xff bytes.
  9. MEMPTR = '9000'
  10. def sendcmd(fd, cmd):
  11. # The serial link echoes back all typed characters and expects us to read
  12. # them. We have to send each char one at a time.
  13. print("Executing {}".format(cmd.decode()), file=sys.stderr)
  14. for c in cmd:
  15. os.write(fd, bytes([c]))
  16. os.read(fd, 1)
  17. os.write(fd, b'\n')
  18. os.read(fd, 2) # sends back \r\n
  19. def main():
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument('device')
  22. parser.add_argument('bytecount')
  23. args = parser.parse_args()
  24. try:
  25. bytecount = int(args.bytecount, 16)
  26. except ValueError:
  27. print("bytecount has to be hexadecimal without prefix.")
  28. return 1
  29. fd = os.open(args.device, os.O_RDWR)
  30. sendcmd(fd, 'mptr {}'.format(MEMPTR).encode())
  31. os.read(fd, 9)
  32. while bytecount > 0:
  33. toread = min(bytecount, 0x100)
  34. sendcmd(fd, 'load {:x}'.format(toread & 0xff).encode())
  35. os.read(fd, 5)
  36. sendcmd(fd, 'peek {:x}'.format(toread & 0xff).encode())
  37. peek = b''
  38. while len(peek) < toread * 2:
  39. peek += os.read(fd, 1)
  40. time.sleep(0.0001)
  41. os.read(fd, 5)
  42. while peek:
  43. c = peek[:2]
  44. sys.stdout.buffer.write(bytes([int(c, 16)]))
  45. peek = peek[2:]
  46. bytecount -= toread
  47. os.close(fd)
  48. return 0
  49. if __name__ == '__main__':
  50. sys.exit(main())