util.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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, re, platform
  10. import importlib
  11. import serial.tools.list_ports
  12. from greaseweazle import version
  13. from greaseweazle import error
  14. from greaseweazle import usb as USB
  15. class CmdlineHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
  16. def _get_help_string(self, action):
  17. help = action.help
  18. if '%no_default' in help:
  19. return help.replace('%no_default', '')
  20. if ('%(default)' in help
  21. or action.default is None
  22. or action.default is False
  23. or action.default is argparse.SUPPRESS):
  24. return help
  25. return help + ' (default: %(default)s)'
  26. class ArgumentParser(argparse.ArgumentParser):
  27. def __init__(self, formatter_class=CmdlineHelpFormatter, *args, **kwargs):
  28. return super().__init__(formatter_class=formatter_class,
  29. *args, **kwargs)
  30. def drive_letter(letter):
  31. types = {
  32. 'A': (USB.BusType.IBMPC, 0),
  33. 'B': (USB.BusType.IBMPC, 1),
  34. '0': (USB.BusType.Shugart, 0),
  35. '1': (USB.BusType.Shugart, 1),
  36. '2': (USB.BusType.Shugart, 2)
  37. }
  38. if not letter.upper() in types:
  39. raise argparse.ArgumentTypeError("invalid drive letter: '%s'" % letter)
  40. return types[letter.upper()]
  41. def range_str(l):
  42. if len(l) == 0:
  43. return '<none>'
  44. p, str = None, ''
  45. for i in l:
  46. if p is not None and i == p[1]+1:
  47. p = p[0], i
  48. continue
  49. if p is not None:
  50. str += ('%d,' % p[0]) if p[0] == p[1] else ('%d-%d,' % p)
  51. p = (i,i)
  52. if p is not None:
  53. str += ('%d' % p[0]) if p[0] == p[1] else ('%d-%d' % p)
  54. return str
  55. class TrackSet:
  56. class TrackIter:
  57. """Iterate over a TrackSet in physical <cyl,head> order."""
  58. def __init__(self, ts):
  59. l = []
  60. for c in ts.cyls:
  61. for h in ts.heads:
  62. pc = c*ts.step + ts.h_off[h]
  63. l.append((pc, h, c))
  64. l.sort()
  65. self.l = iter(l)
  66. def __next__(self):
  67. self.physical_cyl, self.head, self.cyl = next(self.l)
  68. return self
  69. def __init__(self, trackspec):
  70. self.cyls = list()
  71. self.heads = list()
  72. self.h_off = [0]*2
  73. self.step = 1
  74. self.trackspec = ''
  75. self.update_from_trackspec(trackspec)
  76. def update_from_trackspec(self, trackspec):
  77. """Update a TrackSet based on a trackspec."""
  78. self.trackspec += trackspec
  79. for x in trackspec.split(':'):
  80. k,v = x.split('=')
  81. if k == 'c':
  82. cyls = [False]*100
  83. for crange in v.split(','):
  84. m = re.match('(\d\d?)(-(\d\d?))?$', crange)
  85. if m is None: raise ValueError()
  86. if m.group(3) is None:
  87. s,e = int(m.group(1)), int(m.group(1))
  88. else:
  89. s,e = int(m.group(1)), int(m.group(3))
  90. for c in range(s, e+1):
  91. cyls[c] = True
  92. self.cyls = []
  93. for c in range(len(cyls)):
  94. if cyls[c]: self.cyls.append(c)
  95. elif k == 'h':
  96. heads = [False]*2
  97. for hrange in v.split(','):
  98. m = re.match('([01])(-([01]))?$', hrange)
  99. if m is None: raise ValueError()
  100. if m.group(3) is None:
  101. s,e = int(m.group(1)), int(m.group(1))
  102. else:
  103. s,e = int(m.group(1)), int(m.group(3))
  104. for h in range(s, e+1):
  105. heads[h] = True
  106. self.heads = []
  107. for h in range(len(heads)):
  108. if heads[h]: self.heads.append(h)
  109. elif re.match('h[01].off$', k):
  110. h = int(re.match('h([01]).off$', k).group(1))
  111. m = re.match('([+-][\d])$', v)
  112. if m is None: raise ValueError()
  113. self.h_off[h] = int(m.group(1))
  114. elif k == 'step':
  115. self.step = int(v)
  116. if self.step <= 0: raise ValueError()
  117. else:
  118. raise ValueError()
  119. def __str__(self):
  120. s = 'c=%s' % range_str(self.cyls)
  121. s += ':h=%s' % range_str(self.heads)
  122. for i in range(len(self.h_off)):
  123. x = self.h_off[i]
  124. if x != 0:
  125. s += ':h%d.off=%s%d' % (i, '+' if x >= 0 else '', x)
  126. if self.step != 1: s += ':step=%d' % self.step
  127. return s
  128. def __iter__(self):
  129. return self.TrackIter(self)
  130. def split_opts(seq):
  131. """Splits a name from its list of options."""
  132. parts = seq.split('::')
  133. name, opts = parts[0], dict()
  134. for x in map(lambda x: x.split(':'), parts[1:]):
  135. for y in x:
  136. try:
  137. opt, val = y.split('=')
  138. except ValueError:
  139. opt, val = y, True
  140. if opt:
  141. opts[opt] = val
  142. return name, opts
  143. def get_image_class(name):
  144. image_types = { '.adf': 'ADF',
  145. '.scp': 'SCP',
  146. '.hfe': 'HFE',
  147. '.ima': 'IMG',
  148. '.img': 'IMG',
  149. '.ipf': 'IPF',
  150. '.dsk': 'EDSK',
  151. '.raw': 'KryoFlux' }
  152. if os.path.isdir(name):
  153. typename = 'KryoFlux'
  154. else:
  155. _, ext = os.path.splitext(name)
  156. error.check(ext.lower() in image_types,
  157. """\
  158. %s: Unrecognised file suffix '%s'
  159. Known suffixes: %s"""
  160. % (name, ext, ', '.join(image_types)))
  161. typename = image_types[ext.lower()]
  162. mod = importlib.import_module('greaseweazle.image.' + typename.lower())
  163. return mod.__dict__[typename]
  164. def with_drive_selected(fn, usb, args, *_args, **_kwargs):
  165. usb.set_bus_type(args.drive[0])
  166. try:
  167. usb.drive_select(args.drive[1])
  168. usb.drive_motor(args.drive[1], _kwargs.pop('motor', True))
  169. fn(usb, args, *_args, **_kwargs)
  170. except KeyboardInterrupt:
  171. print()
  172. usb.reset()
  173. raise
  174. finally:
  175. usb.drive_motor(args.drive[1], False)
  176. usb.drive_deselect()
  177. def valid_ser_id(ser_id):
  178. return ser_id and ser_id.upper().startswith("GW")
  179. def score_port(x, old_port=None):
  180. score = 0
  181. if x.manufacturer == "Keir Fraser" and x.product == "Greaseweazle":
  182. score = 20
  183. elif x.vid == 0x1209 and x.pid == 0x4d69:
  184. # Our very own properly-assigned PID. Guaranteed to be us.
  185. score = 20
  186. elif x.vid == 0x1209 and x.pid == 0x0001:
  187. # Our old shared Test PID. It's not guaranteed to be us.
  188. score = 10
  189. if score > 0 and valid_ser_id(x.serial_number):
  190. # A valid serial id is a good sign unless this is a reopen, and
  191. # the serials don't match!
  192. if not old_port or not valid_ser_id(old_port.serial_number):
  193. score = 20
  194. elif x.serial_number == old_port.serial_number:
  195. score = 30
  196. else:
  197. score = 0
  198. if old_port and old_port.location:
  199. # If this is a reopen, location field must match. A match is not
  200. # sufficient in itself however, as Windows may supply the same
  201. # location for multiple USB ports (this may be an interaction with
  202. # BitDefender). Hence we do not increase the port's score here.
  203. if not x.location or x.location != old_port.location:
  204. score = 0
  205. return score
  206. def find_port(old_port=None):
  207. best_score, best_port = 0, None
  208. for x in serial.tools.list_ports.comports():
  209. score = score_port(x, old_port)
  210. if score > best_score:
  211. best_score, best_port = score, x
  212. if best_port:
  213. return best_port.device
  214. raise serial.SerialException('Cannot find the Greaseweazle device')
  215. def port_info(devname):
  216. for x in serial.tools.list_ports.comports():
  217. if x.device == devname:
  218. return x
  219. return None
  220. def usb_reopen(usb, is_update):
  221. mode = { False: 1, True: 0 }
  222. try:
  223. usb.switch_fw_mode(mode[is_update])
  224. except (serial.SerialException, struct.error):
  225. # Mac and Linux raise SerialException ("... returned no data")
  226. # Win10 pyserial returns a short read which fails struct.unpack
  227. pass
  228. usb.ser.close()
  229. for i in range(10):
  230. time.sleep(0.5)
  231. try:
  232. devicename = find_port(usb.port_info)
  233. new_ser = serial.Serial(devicename)
  234. except serial.SerialException:
  235. # Device not found
  236. pass
  237. else:
  238. new_usb = USB.Unit(new_ser)
  239. new_usb.port_info = port_info(devicename)
  240. new_usb.jumperless_update = usb.jumperless_update
  241. return new_usb
  242. raise serial.SerialException('Could not reopen port after mode switch')
  243. def print_update_instructions(usb):
  244. print("To perform an Update:")
  245. if not usb.jumperless_update:
  246. print(" - Disconnect from USB")
  247. print(" - Install the Update Jumper at pins %s"
  248. % ("RXI-TXO" if usb.hw_model != 1 else "DCLK-GND"))
  249. print(" - Reconnect to USB")
  250. print(" - Run \"gw update\" to install firmware v%u.%u" %
  251. (version.major, version.minor))
  252. def usb_open(devicename, is_update=False, mode_check=True):
  253. if devicename is None:
  254. devicename = find_port()
  255. usb = USB.Unit(serial.Serial(devicename))
  256. usb.port_info = port_info(devicename)
  257. is_win7 = (platform.system() == 'Windows' and platform.release() == '7')
  258. usb.jumperless_update = ((usb.hw_model, usb.hw_submodel) != (1, 0)
  259. and not is_win7)
  260. if not mode_check:
  261. return usb
  262. if usb.update_mode and not is_update:
  263. if usb.jumperless_update and not usb.update_jumpered:
  264. usb = usb_reopen(usb, is_update)
  265. if not usb.update_mode:
  266. return usb
  267. print("ERROR: Greaseweazle is in Firmware Update Mode")
  268. print(" - The only available action is \"gw update\"")
  269. if usb.update_jumpered:
  270. print(" - For normal operation disconnect from USB and remove "
  271. "the Update Jumper at pins %s"
  272. % ("RXI-TXO" if usb.hw_model != 1 else "DCLK-GND"))
  273. else:
  274. print(" - Main firmware is erased: You *must* perform an update!")
  275. sys.exit(1)
  276. if is_update and not usb.update_mode:
  277. if usb.jumperless_update:
  278. usb = usb_reopen(usb, is_update)
  279. error.check(usb.update_mode, """\
  280. Greaseweazle F7 did not change to Firmware Update Mode as requested.
  281. If the problem persists, install the Update Jumper at pins RXI-TXO.""")
  282. return usb
  283. print("ERROR: Greaseweazle is not in Firmware Update Mode")
  284. print_update_instructions(usb)
  285. sys.exit(1)
  286. if not usb.update_mode and usb.update_needed:
  287. print("ERROR: Greaseweazle firmware v%u.%u is unsupported"
  288. % (usb.major, usb.minor))
  289. print_update_instructions(usb)
  290. sys.exit(1)
  291. return usb
  292. # Local variables:
  293. # python-indent: 4
  294. # End: