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.

85 lines
2.7KB

  1. #!/usr/bin/python
  2. # Push specified file to specified device and verify that the contents is
  3. # correct by sending a "peek" command afterwards and check the output. Errors
  4. # out if the contents isn't the same. The parameter passed to the "peek"
  5. # command is the length of the uploaded file.
  6. import argparse
  7. import os
  8. import sys
  9. import time
  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()))
  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('memptr')
  23. parser.add_argument('filename')
  24. args = parser.parse_args()
  25. try:
  26. memptr = int('0x' + args.memptr, 0)
  27. except ValueError:
  28. print("memptr are has to be hexadecimal without prefix.")
  29. return 1
  30. if memptr >= 0x10000:
  31. print("memptr out of range.")
  32. return 1
  33. maxsize = 0x10000 - memptr
  34. st = os.stat(args.filename)
  35. if st.st_size > maxsize:
  36. print("File too big. 0x{:04x} bytes max".format(maxsize))
  37. return 1
  38. fd = os.open(args.device, os.O_RDWR)
  39. with open(args.filename, 'rb') as fp:
  40. while True:
  41. fcontents = fp.read(0xff)
  42. if not fcontents:
  43. break
  44. print("Seeking...")
  45. sendcmd(fd, 'mptr {:04x}'.format(memptr).encode())
  46. os.read(fd, 9)
  47. sendcmd(fd, 'poke {:x}'.format(len(fcontents)).encode())
  48. print("Poking...")
  49. for c in fcontents:
  50. os.write(fd, bytes([c]))
  51. # Let's give the machine a bit of time to breathe. We ain't in a
  52. # hurry now, are we?
  53. time.sleep(0.0001)
  54. print("Poked")
  55. os.read(fd, 5)
  56. print("Peeking back...")
  57. sendcmd(fd, 'peek {:x}'.format(len(fcontents)).encode())
  58. peek = b''
  59. while len(peek) < len(fcontents) * 2:
  60. peek += os.read(fd, 1)
  61. time.sleep(0.0001)
  62. os.read(fd, 5)
  63. print("Got {}".format(peek.decode()))
  64. print("Comparing...")
  65. for i, c in enumerate(fcontents):
  66. hexfmt = '{:02X}'.format(c).encode()
  67. if hexfmt != peek[:2]:
  68. print("Mismatch at byte {}! {} != {}".format(i, peek[:2], hexfmt))
  69. return 1
  70. peek = peek[2:]
  71. print("All good!")
  72. memptr += len(fcontents)
  73. print("Done!")
  74. os.close(fd)
  75. return 0
  76. if __name__ == '__main__':
  77. sys.exit(main())