util.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # greaseweazle/tools/util.py
  2. #
  3. # Greaseweazle control script: Utility functions.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. import argparse, os, sys, serial, struct, time
  10. import serial.tools.list_ports
  11. from greaseweazle import version
  12. from greaseweazle import error
  13. from greaseweazle import usb as USB
  14. from greaseweazle.image.scp import SCP
  15. from greaseweazle.image.hfe import HFE
  16. from greaseweazle.image.ipf import IPF
  17. from greaseweazle.image.adf import ADF
  18. class CmdlineHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
  19. def _get_help_string(self, action):
  20. help = action.help
  21. if '%no_default' in help:
  22. return help.replace('%no_default', '')
  23. if ('%(default)' in help
  24. or action.default is None
  25. or action.default is False
  26. or action.default is argparse.SUPPRESS):
  27. return help
  28. return help + ' (default: %(default)s)'
  29. class ArgumentParser(argparse.ArgumentParser):
  30. def __init__(self, formatter_class=CmdlineHelpFormatter, *args, **kwargs):
  31. return super().__init__(formatter_class=formatter_class,
  32. *args, **kwargs)
  33. def drive_letter(letter):
  34. types = {
  35. 'A': (USB.BusType.IBMPC, 0),
  36. 'B': (USB.BusType.IBMPC, 1),
  37. '0': (USB.BusType.Shugart, 0),
  38. '1': (USB.BusType.Shugart, 1),
  39. '2': (USB.BusType.Shugart, 2)
  40. }
  41. if not letter.upper() in types:
  42. raise argparse.ArgumentTypeError("invalid drive letter: '%s'" % letter)
  43. return types[letter.upper()]
  44. def split_opts(seq):
  45. """Splits a name from its list of options."""
  46. parts = seq.split('::')
  47. name, opts = parts[0], dict()
  48. for x in map(lambda x: x.split(':'), parts[1:]):
  49. for y in x:
  50. try:
  51. opt, val = y.split('=')
  52. except ValueError:
  53. opt, val = y, True
  54. if opt:
  55. opts[opt] = val
  56. return name, opts
  57. def get_image_class(name):
  58. image_types = { '.adf': ADF, '.scp': SCP, '.hfe': HFE, '.ipf': IPF }
  59. _, ext = os.path.splitext(name)
  60. error.check(ext.lower() in image_types,
  61. "%s: Unrecognised file suffix '%s'" % (name, ext))
  62. return image_types[ext.lower()]
  63. def with_drive_selected(fn, usb, args, *_args, **_kwargs):
  64. usb.set_bus_type(args.drive[0])
  65. try:
  66. usb.drive_select(args.drive[1])
  67. usb.drive_motor(args.drive[1], _kwargs.pop('motor', True))
  68. fn(usb, args, *_args, **_kwargs)
  69. except KeyboardInterrupt:
  70. print()
  71. usb.reset()
  72. usb.ser.close()
  73. usb.ser.open()
  74. raise
  75. finally:
  76. usb.drive_motor(args.drive[1], False)
  77. usb.drive_deselect()
  78. def valid_ser_id(ser_id):
  79. return ser_id and ser_id.upper().startswith("GW")
  80. def score_port(x, old_port=None):
  81. score = 0
  82. if x.manufacturer == "Keir Fraser" and x.product == "Greaseweazle":
  83. score = 20
  84. elif x.vid == 0x1209 and x.pid == 0x4d69:
  85. # Our very own properly-assigned PID. Guaranteed to be us.
  86. score = 20
  87. elif x.vid == 0x1209 and x.pid == 0x0001:
  88. # Our old shared Test PID. It's not guaranteed to be us.
  89. score = 10
  90. if score > 0 and valid_ser_id(x.serial_number):
  91. # A valid serial id is a good sign unless this is a reopen, and
  92. # the serials don't match!
  93. if not old_port or not valid_ser_id(old_port.serial_number):
  94. score = 20
  95. elif x.serial_number == old_port.serial_number:
  96. score = 30
  97. else:
  98. score = 0
  99. if old_port and old_port.location:
  100. # If this is a reopen, location field must match. A match is not
  101. # sufficient in itself however, as Windows may supply the same
  102. # location for multiple USB ports (this may be an interaction with
  103. # BitDefender). Hence we do not increase the port's score here.
  104. if not x.location or x.location != old_port.location:
  105. score = 0
  106. return score
  107. def find_port(old_port=None):
  108. best_score, best_port = 0, None
  109. for x in serial.tools.list_ports.comports():
  110. score = score_port(x, old_port)
  111. if score > best_score:
  112. best_score, best_port = score, x
  113. if best_port:
  114. return best_port.device
  115. raise serial.SerialException('Cannot find the Greaseweazle device')
  116. def port_info(devname):
  117. for x in serial.tools.list_ports.comports():
  118. if x.device == devname:
  119. return x
  120. return None
  121. def usb_reopen(usb, is_update):
  122. mode = { False: 1, True: 0 }
  123. try:
  124. usb.switch_fw_mode(mode[is_update])
  125. except (serial.SerialException, struct.error):
  126. # Mac and Linux raise SerialException ("... returned no data")
  127. # Win10 pyserial returns a short read which fails struct.unpack
  128. pass
  129. usb.ser.close()
  130. for i in range(10):
  131. time.sleep(0.5)
  132. try:
  133. devicename = find_port(usb.port_info)
  134. new_ser = serial.Serial(devicename)
  135. except serial.SerialException:
  136. # Device not found
  137. pass
  138. else:
  139. new_usb = USB.Unit(new_ser)
  140. new_usb.port_info = port_info(devicename)
  141. return new_usb
  142. raise serial.SerialException('Could not reopen port after mode switch')
  143. def usb_open(devicename, is_update=False, mode_check=True):
  144. if devicename is None:
  145. devicename = find_port()
  146. usb = USB.Unit(serial.Serial(devicename))
  147. usb.port_info = port_info(devicename)
  148. if not mode_check:
  149. return usb
  150. if usb.update_mode and not is_update:
  151. if usb.hw_model == 7 and not usb.update_jumpered:
  152. usb = usb_reopen(usb, is_update)
  153. if not usb.update_mode:
  154. return usb
  155. print("Greaseweazle is in Firmware Update Mode:")
  156. print(" The only available action is \"update\" of main firmware")
  157. if usb.update_jumpered:
  158. print(" Remove the Update Jumper for normal operation")
  159. else:
  160. print(" Main firmware is erased: You *must* perform an update!")
  161. sys.exit(1)
  162. if is_update and not usb.update_mode:
  163. if usb.hw_model == 7:
  164. usb = usb_reopen(usb, is_update)
  165. error.check(usb.update_mode, """\
  166. Greaseweazle F7 did not change to Firmware Update Mode as requested.
  167. If the problem persists, install the Update Jumper (across RX/TX).""")
  168. return usb
  169. print("Greaseweazle is in Normal Mode:")
  170. print(" To \"update\" you must install the Update Jumper")
  171. sys.exit(1)
  172. if not usb.update_mode and usb.update_needed:
  173. print("Firmware is out of date: Require v%u.%u"
  174. % (version.major, version.minor))
  175. if usb.hw_model == 7:
  176. print("Run \"update <update_file>\"")
  177. else:
  178. print("Install the Update Jumper and \"update <update_file>\"")
  179. sys.exit(1)
  180. return usb
  181. # Local variables:
  182. # python-indent: 4
  183. # End: