read.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # greaseweazle/tools/read.py
  2. #
  3. # Greaseweazle control script: Read Disk to Image.
  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. description = "Read a disk to the specified image file."
  10. import sys
  11. import importlib
  12. from greaseweazle.tools import util
  13. from greaseweazle import error
  14. from greaseweazle import usb as USB
  15. from greaseweazle.flux import Flux
  16. def open_image(args, image_class):
  17. error.check(hasattr(image_class, 'to_file'),
  18. "%s: Cannot create %s image files"
  19. % (args.file, image_class.__name__))
  20. image = image_class.to_file(args.scyl, args.nr_sides)
  21. if args.rate is not None:
  22. image.bitrate = args.rate
  23. for opt, val in args.file_opts.items():
  24. error.check(hasattr(image, 'opts') and hasattr(image.opts, opt),
  25. "%s: Invalid file option: %s" % (args.file, opt))
  26. setattr(image.opts, opt, val)
  27. return image
  28. def normalise_rpm(flux, rpm):
  29. """Adjust all revolutions in Flux object to have specified rotation speed.
  30. """
  31. index_list, freq = flux.index_list, flux.sample_freq
  32. norm_to_index = 60/rpm * flux.sample_freq
  33. norm_flux = []
  34. to_index, index_list = index_list[0], index_list[1:]
  35. factor = norm_to_index / to_index
  36. for x in flux.list:
  37. to_index -= x
  38. if to_index >= 0:
  39. norm_flux.append(x*factor)
  40. continue
  41. if not index_list:
  42. break
  43. n_to_index, index_list = index_list[0], index_list[1:]
  44. n_factor = norm_to_index / n_to_index
  45. norm_flux.append((x+to_index)*factor - to_index*n_factor)
  46. to_index, factor = n_to_index, n_factor
  47. return Flux([norm_to_index]*len(flux.index_list), norm_flux, freq)
  48. def read_and_normalise(usb, args, revs):
  49. flux = usb.read_track(revs)
  50. if args.rpm is not None:
  51. flux = normalise_rpm(flux, args.rpm)
  52. return flux
  53. def read_with_retry(usb, args, cyl, side, decoder):
  54. flux = read_and_normalise(usb, args, args.revs)
  55. if decoder is None:
  56. return flux
  57. dat = decoder(cyl, side, flux)
  58. if dat.nr_missing() != 0:
  59. for retry in range(3):
  60. print("T%u.%u: %s - %d sectors missing - Retrying (%d)"
  61. % (cyl, side, dat.summary_string(),
  62. dat.nr_missing(), retry+1))
  63. flux = read_and_normalise(usb, args, max(args.revs, 3))
  64. dat.decode_raw(flux)
  65. if dat.nr_missing() == 0:
  66. break
  67. return dat
  68. def print_summary(args, summary):
  69. print("H. S: Cyls %d-%d -->" % (args.scyl, args.ecyl))
  70. tot_sec = good_sec = 0
  71. for side in range(0, args.nr_sides):
  72. nsec = max(x.nsec for x in summary[side])
  73. for sec in range(nsec):
  74. print("%d.%2d: " % (side, sec), end="")
  75. for cyl in range(args.scyl, args.ecyl+1):
  76. s = summary[side][cyl-args.scyl]
  77. if sec > s.nsec:
  78. print(" ", end="")
  79. else:
  80. tot_sec += 1
  81. if s.has_sec(sec): good_sec += 1
  82. print("." if s.has_sec(sec) else "X", end="")
  83. print()
  84. print("Found %d sectors of %d (%d%%)" %
  85. (good_sec, tot_sec, good_sec*100/tot_sec))
  86. def read_to_image(usb, args, image, decoder=None):
  87. """Reads a floppy disk and dumps it into a new image file.
  88. """
  89. summary = [[],[]]
  90. for cyl in range(args.scyl, args.ecyl+1):
  91. for side in range(0, args.nr_sides):
  92. usb.seek((cyl, cyl*2)[args.double_step], side)
  93. dat = read_with_retry(usb, args, cyl, side, decoder)
  94. print("T%u.%u: %s" % (cyl, side, dat.summary_string()))
  95. summary[side].append(dat)
  96. image.append_track(dat)
  97. if decoder is not None:
  98. print_summary(args, summary)
  99. # Write the image file.
  100. with open(args.file, "wb") as f:
  101. f.write(image.get_image())
  102. def range_str(s, e):
  103. str = "%d" % s
  104. if s != e: str += "-%d" % e
  105. return str
  106. def main(argv):
  107. parser = util.ArgumentParser(usage='%(prog)s [options] file')
  108. parser.add_argument("--device", help="greaseweazle device name")
  109. parser.add_argument("--drive", type=util.drive_letter, default='A',
  110. help="drive to read (A,B,0,1,2)")
  111. parser.add_argument("--format", help="disk format")
  112. parser.add_argument("--revs", type=int,
  113. help="number of revolutions to read per track")
  114. parser.add_argument("--scyl", type=int,
  115. help="first cylinder to read")
  116. parser.add_argument("--ecyl", type=int,
  117. help="last cylinder to read")
  118. parser.add_argument("--single-sided", action="store_true",
  119. help="single-sided read")
  120. parser.add_argument("--double-step", action="store_true",
  121. help="double-step drive heads")
  122. parser.add_argument("--rate", type=int, help="data rate (kbit/s)")
  123. parser.add_argument("--rpm", type=int, help="convert drive speed to RPM")
  124. parser.add_argument("file", help="output filename")
  125. parser.description = description
  126. parser.prog += ' ' + argv[1]
  127. args = parser.parse_args(argv[2:])
  128. args.nr_sides = 1 if args.single_sided else 2
  129. args.file, args.file_opts = util.split_opts(args.file)
  130. try:
  131. usb = util.usb_open(args.device)
  132. image_class = util.get_image_class(args.file)
  133. if not args.format and hasattr(image_class, 'default_format'):
  134. args.format = image_class.default_format
  135. decoder = None
  136. if args.format:
  137. try:
  138. mod = importlib.import_module('greaseweazle.codec.'
  139. + args.format)
  140. decoder = mod.decode_track
  141. except (ModuleNotFoundError, AttributeError) as ex:
  142. raise error.Fatal("Unknown format '%s'" % args.format) from ex
  143. if args.scyl is None: args.scyl = mod.default_cyls[0]
  144. if args.ecyl is None: args.ecyl = mod.default_cyls[1]
  145. if args.revs is None: args.revs = mod.default_revs
  146. if args.scyl is None: args.scyl = 0
  147. if args.ecyl is None: args.ecyl = 81
  148. if args.revs is None: args.revs = 3
  149. print("Reading c=%s s=%s revs=%d" %
  150. (range_str(args.scyl, args.ecyl),
  151. range_str(0, args.nr_sides-1),
  152. args.revs))
  153. image = open_image(args, image_class)
  154. util.with_drive_selected(read_to_image, usb, args, image,
  155. decoder=decoder)
  156. except USB.CmdError as err:
  157. print("Command Failed: %s" % err)
  158. if __name__ == "__main__":
  159. main(sys.argv)
  160. # Local variables:
  161. # python-indent: 4
  162. # End: