-
Notifications
You must be signed in to change notification settings - Fork 3
/
BootblockFrame.py
383 lines (383 loc) · 16.5 KB
/
BootblockFrame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import os
import glob
import wx
import threading
import struct
from TrackdiskDevice import TrackdiskDevice
from FloppyUtils import FloppyUtils
class BootblockFrame(wx.Frame):
tracksize = 512 * 11
def __init__(self):
self.endcallback = None
self.ser = None
self.amiga = None
self.execlib = None
self.snip = None
self.wantclose = False
self.busy = False
self.installcolor = None
self.drives = 4 #maximum we test for
self.bbpath = "asm/"
self.bbdesc = glob.glob(f"{self.bbpath}/*desc")
self.bbdesc.sort()
self.bbname = [os.path.splitext(os.path.split(bb)[1])[0] for bb in self.bbdesc]
self.bbimg = [os.path.join(dir, os.path.splitext(file)[0]+".dd") for bb in self.bbdesc for dir, file in [os.path.split(bb)]]
super().__init__(None, id=wx.ID_ANY, title=u"amigaXfer Bootblock Tool", pos=wx.DefaultPosition, size=wx.Size(550, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DLIGHT))
bSizer4 = wx.BoxSizer(wx.VERTICAL)
m_bootblockChoices = self.bbname
self.m_bootblock = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_bootblockChoices, 0)
self.m_bootblock.SetSelection(0)
bSizer4.Add(self.m_bootblock, 0, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, 5)
self.m_description = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY | wx.TE_MULTILINE)
bSizer4.Add(self.m_description, 1, wx.ALL | wx.EXPAND, 5)
wSizer8 = wx.WrapSizer(wx.HORIZONTAL, wx.WRAPSIZER_DEFAULT_FLAGS)
bSizer6 = wx.BoxSizer(wx.VERTICAL)
self.m_format = wx.CheckBox(self, wx.ID_ANY, u"Format", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_format.SetForegroundColour(wx.Colour(255, 13, 0))
self.m_format.SetToolTip(u"Formats track 0. Data will be lost.")
bSizer6.Add(self.m_format, 0, 0, 5)
self.m_status = wx.TextCtrl(self, wx.ID_ANY, "Init", wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
self.m_status.SetFont(wx.Font(wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString))
bSizer6.Add(self.m_status, 0, wx.ALIGN_CENTER_HORIZONTAL, 5)
wSizer8.Add(bSizer6, 0, wx.ALIGN_CENTER_VERTICAL, 5)
wSizer8.Add((0, 0), 1, wx.EXPAND, 5)
m_driveChoices = [u"DF0", u"DF1", u"DF2", u"DF3"]
self.m_drive = wx.RadioBox(self, wx.ID_ANY, u"Drive", wx.DefaultPosition, wx.DefaultSize, m_driveChoices, 1, wx.RA_SPECIFY_ROWS)
self.m_drive.SetSelection(0)
wSizer8.Add(self.m_drive, 0, wx.ALIGN_CENTER_VERTICAL, 5)
wSizer8.Add((0, 0), 1, wx.EXPAND, 5)
self.m_exit = wx.Button(self, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0)
wSizer8.Add(self.m_exit, 0, wx.ALIGN_BOTTOM | wx.BOTTOM | wx.RIGHT | wx.LEFT, 5)
self.m_install = wx.Button(self, wx.ID_ANY, u"Install", wx.DefaultPosition, wx.DefaultSize, 0)
wSizer8.Add(self.m_install, 0, wx.ALIGN_BOTTOM | wx.BOTTOM | wx.RIGHT | wx.LEFT, 5)
bSizer4.Add(wSizer8, 0, wx.EXPAND | wx.TOP | wx.LEFT, 5)
self.SetSizer(bSizer4)
self.Enablement(False)
self.Layout()
self.Centre(wx.BOTH)
self.Bind(wx.EVT_CLOSE, self.onCloseSetup)
self.m_format.Bind(wx.EVT_CHECKBOX, self.onFormatCheckBox)
self.m_bootblock.Bind(wx.EVT_CHOICE,self.onBootblockChoice)
self.m_exit.Bind(wx.EVT_BUTTON, self.onExitPressed)
self.m_install.Bind(wx.EVT_BUTTON, self.onInstallPressed)
return
def UpdateStatus(self, status):
self.m_status.ChangeValue(status)
return
def Enablement(self, enable):
self.m_install.Enable(enable)
self.m_exit.Enable(enable)
self.m_format.Enable(enable)
self.m_bootblock.Enable(enable)
self.m_drive.Enable(enable)
if enable:
for i in range(0, 4):
self.m_drive.EnableItem(i, i < self.drives)
return
def onCloseSetup(self, event):
if event.CanVeto():
event.Veto()
return
def onClose(self, event):
if event.CanVeto():
event.Veto()
if self.wantclose:
return
if self.busy:
self.wantclose = True
return
self.UpdateStatus("UserClose")
self.CleanUp()
return
def onExitPressed(self, event):
self.wantclose = True
wx.CallAfter(self.UpdateStatus, "CleanUp")
wx.CallAfter(self.CleanUp)
return
def onFormatCheckBox(self, event):
danger = self.m_format.GetValue()
if danger:
self.installcolor = self.m_install.GetForegroundColour()
self.m_install.SetForegroundColour(wx.Colour(255, 0, 0))
else:
self.m_install.SetForegroundColour(self.installcolor)
return
def onBootblockChoice(self, event):
self.UpdateDescription()
return
def UpdateDescription(self):
choice = self.m_bootblock.GetSelection()
with open(self.bbdesc[choice]) as fh:
text = fh.read()
self.m_description.ChangeValue(text)
return
def onInstallPressed(self, event):
self.Enablement(False)
self.busy = True
self.UpdateStatus("Install")
self.Layout()
bbsel = self.m_bootblock.GetSelection()
bootblock = self.bbimg[bbsel]
drive = self.m_drive.GetSelection()
doformat = self.m_format.GetValue()
print(f'Writing bootblock {self.bbname[bbsel]} to drive DF{drive}. Format: {doformat}.')
blockdump = bytes()
try:
with open(bootblock, "rb") as fh:
blockdump = fh.read()
except:
print(f"Couldn't read bootblock file {bootblock}.")
if blockdump:
threading.Thread(target=self.InstallWorker, args=(drive, blockdump, doformat)).start()
else:
self.UpdateStatus("FileErr.")
wx.CallAfter(self.InstallCleanup)
return
def InstallCleanup(self):
self.busy = False
if self.wantclose:
self.UpdateStatus("UserClose")
self.CleanUp()
else:
self.Enablement(True)
return
def InstallWorker(self, drive, blockdump, doformat):
floppy = TrackdiskDevice(debugger=self.amiga, execlib=self.execlib, ioreqaddr=self.getioreq(drive))
if len(blockdump) > 1024:
print("Passed bootblock is above 1024 bytes. Not supported.")
return
bootblock = bytearray(blockdump)
if len(bootblock) < 1024:
bootblock += bytes(1024 - len(bootblock))
wx.CallAfter(self.UpdateStatus, "Clear")
ioerr = floppy.clear()
if ioerr:
print(f"CLEAR IO ERROR {ioerr:02X}h.")
wx.CallAfter(self.UpdateStatus, f"CrErr: {ioerr:02X}h")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
bufaddr = self.trackbuffer
if not doformat:
wx.CallAfter(self.UpdateStatus, "Read")
ioerr = floppy.read(bufaddr, 1024, 0)
if ioerr==floppy.TDERR_DiskChanged:
print("Couldn't read floppy. Is the floppy in the drive?")
wx.CallAfter(self.UpdateStatus, "NoFloppy?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr:
print(f"Bootblock on target couldn't be read. IO Error {ioerr:02X}h. Is this floppy formatted?")
wx.CallAfter(self.UpdateStatus, "NoFormat?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
wx.CallAfter(self.UpdateStatus, "Recv+CRC")
bbhdr = self.snip.verifiedreadmem(bufaddr, 12)
if bbhdr[0:3] != b"DOS":
print("Floppy's current bootblock lacks DOS signature. Guessing flags/root block is not supported.")
wx.CallAfter(self.UpdateStatus, "NDOS.")
wx.CallAfter(self.InstallCleanup)
return
oldsum = struct.unpack(">I", bootblock[4:8])[0]
bbnosum = bytearray(bootblock)
bbnosum[4:8] = bytes(4)
if oldsum == self.bootblocksum(bbnosum):
print("New bootblock's checksum is correct. Adjusting FS flags and root block, and recalculating checksum.")
bootblock[4:8] == struct.pack(">I", self.bootblocksum(bootblock))
bootblock[3] = bbhdr[3]
bootblock[4:8] = bytes(4)
bootblock[8:12] = bbhdr[8:12]
bootblock[4:8] = struct.pack(">I", self.bootblocksum(bootblock))
else:
print("New bootblock's checksum is wrong. Treat as non-bootable.")
if bootblock[0:3] == b"DOS":
print("New bootblock has DOS signature. Adjusting FS flags and root block, while skipping checksum to keep non-bootable.")
bootblock[3] = bbhdr[3]
bootblock[8:12] = bbhdr[8:12]
wx.CallAfter(self.UpdateStatus, "Send+CRC")
self.snip.verifiedwritemem(bufaddr, bootblock)
if doformat:
wx.CallAfter(self.UpdateStatus, "Format")
ioerr = floppy.tdformat(bufaddr, self.tracksize, 0)
if ioerr==floppy.TDERR_WriteProt:
wx.CallAfter(self.UpdateStatus, "WriteProt?")
print("Couldn't write to floppy. Is the floppy write-protected?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr==floppy.TDERR_DiskChanged:
wx.CallAfter(self.UpdateStatus, "NoFloppy?")
print("Couldn't write to floppy. Is the floppy in the drive?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr:
print(f"Couldn't format track. IO Error {ioerr:02X}h. Is this a bad floppy?")
wx.CallAfter(self.UpdateStatus, f"BadFloppy?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
else:
wx.CallAfter(self.UpdateStatus, "Write")
ioerr = floppy.write(bufaddr, 1024, 0)
if ioerr:
print(f"Write ioerr {ioerr}.")
else:
ioerr = floppy.update()
if ioerr:
print(f"Update ioerr {ioerr}.")
if ioerr==floppy.TDERR_WriteProt:
wx.CallAfter(self.UpdateStatus, "WriteProt?")
print("Couldn't write to floppy. Is the floppy write-protected?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr==floppy.TDERR_DiskChanged:
wx.CallAfter(self.UpdateStatus, "NoFloppy?")
print("TDERR_DiskChanged.")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr:
print(f"WRITE IO ERROR {ioerr:02X}h.")
wx.CallAfter(self.UpdateStatus, f"WrErr: {ioerr:02X}h")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
wx.CallAfter(self.UpdateStatus, "Clear")
ioerr = floppy.clear()
wx.CallAfter(self.UpdateStatus, "Read")
ioerr = floppy.read(bufaddr, 1024, 0)
if ioerr==floppy.TDERR_DiskChanged:
print("Couldn't read floppy. Is the floppy in the drive?")
wx.CallAfter(self.UpdateStatus, "NoFloppy?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
elif ioerr:
print(f"Verify read failed. IO Error {ioerr:02X}h. Is this a bad floppy?")
wx.CallAfter(self.UpdateStatus, "BadFloppy?")
floppy.motoroff()
wx.CallAfter(self.InstallCleanup)
return
if ioerr := floppy.motoroff():
print("Couldn't stop motor. Why?!")
wx.CallAfter(self.UpdateStatus, "CRC")
amigacrc = self.snip.amigakeysum(bufaddr, 1024)
localcrc = self.snip.keysum(bootblock)
if amigacrc != localcrc:
print("Verify failed. CRC does not match.")
wx.CallAfter(self.UpdateStatus, f"CRCErr.")
wx.CallAfter(self.InstallCleanup)
return
wx.CallAfter(self.UpdateStatus, "Done.")
wx.CallAfter(self.InstallCleanup)
print("Bootblock installed.")
return
def bootblocksum(self, data):
cksum = 0
mask = (1 << 32) - 1
for i in struct.unpack('>256I', data):
cksum += i
if cksum > mask:
cksum += 1
cksum &= mask
cksum = (~cksum) & mask
return cksum
def BootblockSetup(self, endcallback, ser, amiga, execlib, snip):
self.Bind(wx.EVT_CLOSE, self.onCloseSetup)
self.m_status.ChangeValue(u'Setup')
self.endcallback = endcallback
self.ser = ser
self.amiga = amiga
self.execlib = execlib
self.snip = snip
self.wantclose = False
self.Enablement(False)
threading.Thread(target=self.BootblockSetupWorker).start()
return
def BootblockSetupWorker(self):
wx.CallAfter(self.UpdateStatus, "Buffer")
self.trackbuffer = self._getbuffer(self.tracksize)
self.drives = self._initdrives(self.drives)
if self.drives == 0:
print("Couldn't open DF0. Aborting.")
wx.CallAfter(self.UpdateStatus, "NoDrives")
wx.CallAfter(self.CleanUp)
return
wx.CallAfter(self.BootblockSetupDone)
return
def BootblockSetupDone(self):
wx.CallAfter(self.UpdateDescription)
wx.CallAfter(self.UpdateStatus, "Ready.")
wx.CallAfter(self.Enablement, True)
self.Unbind(wx.EVT_CLOSE, handler=self.onCloseSetup)
self.Bind(wx.EVT_CLOSE, self.onClose)
return
def _getbuffer(self, size):
if self.execlib.version >= 36:
bufaddr = self.execlib.AllocMem(size, self.execlib.MEMF_PUBLIC | self.execlib.MEMF_CLEAR)
else:
bufaddr = self.execlib.AllocMem(size, self.execlib.MEMF_CHIP | self.execlib.MEMF_PUBLIC | self.execlib.MEMF_CLEAR)
print(f'Allocated {hex(size)} bytes buffer @ {hex(bufaddr)}')
return bufaddr
def getioreq(self, unit):
(ioreq, msgport) = self._ioreq[unit]
return ioreq
def _getioreq(self, devname, unit):
msgport = self.execlib.createmsgport()
if not msgport:
print('Failed to obtain msgport.')
raise BufferError()
ioaddr = self.execlib.createiorequest(msgport)
if not ioaddr:
print('Failed to obtain ioreq.')
raise BufferError()
devname = self.snip.getaddrstr("trackdisk.device")
err = self.execlib.OpenDevice(devname, unit, ioaddr, 0)
if err:
self.execlib.deleteiorequest(ioaddr)
self.execlib.deletemsgport(msgport)
return (None, None)
return (ioaddr, msgport)
def _initdrives(self, maxdrives):
devname = self.snip.getaddrstr("trackdisk.device")
self._ioreq = []
founddrives = 0
for unit in range(0, maxdrives):
wx.CallAfter(self.UpdateStatus, f'Init DF{unit}:')
(ioreq, msgport) = self._getioreq(devname, unit)
if ioreq:
self._ioreq.append((ioreq, msgport))
founddrives += 1
print(f'DF{unit} @ {hex(ioreq)}.')
else:
print(f'DF{unit} Not Present.')
break
return founddrives
def onExitPressed(self, event):
self.wantclose = True
wx.CallAfter(self.UpdateStatus, "CleanUp")
wx.CallAfter(self.CleanUp)
return
def CleanUp(self):
self.Enablement(False)
threading.Thread(target=self.CleanUpWorker).start()
return
def CleanUpWorker(self):
print("CleanUp start.")
for (ioaddr, msgport) in self._ioreq:
if not ioaddr:
continue
print(f"ioreq: {hex(ioaddr)}")
self.execlib.CloseDevice(ioaddr)
self.execlib.deleteiorequest(ioaddr)
self.execlib.deletemsgport(msgport)
self.execlib.FreeMem(self.trackbuffer, self.tracksize)
print("CleanUp done.")
wx.CallAfter(self.endcallback)
return