ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/disk.cpp
Revision: 1.1.1.1 (vendor branch)
Committed: 1999-10-03T14:16:25Z (24 years, 7 months ago) by cebix
Branch: cebix
CVS Tags: release-0_7-2, start
Changes since 1.1: +0 -0 lines
Log Message:
Imported sources

File Contents

# Content
1 /*
2 * disk.cpp - Generic disk driver
3 *
4 * Basilisk II (C) 1997-1999 Christian Bauer
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 * SEE ALSO
23 * Inside Macintosh: Devices, chapter 1 "Device Manager"
24 * Technote DV 05: "Drive Queue Elements"
25 * Technote DV 23: "Driver Education"
26 * Technote FL 24: "Don't Look at ioPosOffset for Devices"
27 */
28
29 #include <string.h>
30
31 #include "sysdeps.h"
32 #include "cpu_emulation.h"
33 #include "main.h"
34 #include "macos_util.h"
35 #include "sys.h"
36 #include "prefs.h"
37 #include "disk.h"
38
39 #define DEBUG 0
40 #include "debug.h"
41
42
43 // .Disk Disk/drive icon
44 const uint8 DiskIcon[258] = {
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe,
50 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01,
51 0x80, 0x00, 0x00, 0x01, 0x8c, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01,
52 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62
63 0, 0
64 };
65
66
67 // Struct for each drive
68 struct DriveInfo {
69 DriveInfo()
70 {
71 next = NULL;
72 num = 0;
73 fh = NULL;
74 read_only = false;
75 status = 0;
76 }
77
78 DriveInfo *next; // Pointer to next DriveInfo (must be first in struct!)
79 int num; // Drive number
80 void *fh; // File handle
81 uint32 num_blocks; // Size in 512-byte blocks
82 bool to_be_mounted; // Flag: drive must be mounted in accRun
83 bool read_only; // Flag: force write protection
84 uint32 status; // Mac address of drive status record
85 };
86
87 // Linked list of DriveInfos
88 static DriveInfo *first_drive_info;
89
90 // Icon address (Mac address space, set by PatchROM())
91 uint32 DiskIconAddr;
92
93
94 /*
95 * Get pointer to drive info, NULL = invalid drive number
96 */
97
98 static DriveInfo *get_drive_info(int num)
99 {
100 DriveInfo *info = first_drive_info;
101 while (info != NULL) {
102 if (info->num == num)
103 return info;
104 info = info->next;
105 }
106 return NULL;
107 }
108
109
110 /*
111 * Initialization
112 */
113
114 void DiskInit(void)
115 {
116 first_drive_info = NULL;
117
118 // No drives specified in prefs? Then add defaults
119 if (PrefsFindString("disk", 0) == NULL)
120 SysAddDiskPrefs();
121
122 // Add drives specified in preferences
123 int32 index = 0;
124 const char *str;
125 while ((str = PrefsFindString("disk", index++)) != NULL) {
126 bool read_only = false;
127 if (str[0] == '*') {
128 read_only = true;
129 str++;
130 }
131 void *fh = Sys_open(str, read_only);
132 if (fh) {
133 D(bug(" adding drive '%s'\n", str));
134 DriveInfo *info = new DriveInfo;
135 info->fh = fh;
136 info->read_only = SysIsReadOnly(fh);
137 DriveInfo *p = (DriveInfo *)&first_drive_info;
138 while (p->next != NULL)
139 p = p->next;
140 p->next = info;
141 }
142 }
143 }
144
145
146 /*
147 * Deinitialization
148 */
149
150 void DiskExit(void)
151 {
152 DriveInfo *info = first_drive_info, *next;
153 while (info != NULL) {
154 Sys_close(info->fh);
155 next = info->next;
156 delete info;
157 info = next;
158 }
159 }
160
161
162 /*
163 * Disk was inserted, flag for mounting
164 */
165
166 bool DiskMountVolume(void *fh)
167 {
168 DriveInfo *info;
169 for (info = first_drive_info; info != NULL && info->fh != fh; info = info->next) ;
170 if (info) {
171 if (SysIsDiskInserted(info->fh)) {
172 info->read_only = SysIsReadOnly(info->fh);
173 WriteMacInt8(info->status + dsDiskInPlace, 1); // Inserted removable disk
174 WriteMacInt8(info->status + dsWriteProt, info->read_only ? 0xff : 0);
175 info->num_blocks = SysGetFileSize(info->fh) / 512;
176 WriteMacInt16(info->status + dsDriveSize, info->num_blocks & 0xffff);
177 WriteMacInt16(info->status + dsDriveS1, info->num_blocks >> 16);
178 info->to_be_mounted = true;
179 }
180 return true;
181 } else
182 return false;
183 }
184
185
186 /*
187 * Driver Open() routine
188 */
189
190 int16 DiskOpen(uint32 pb, uint32 dce)
191 {
192 D(bug("DiskOpen\n"));
193
194 // Set up DCE
195 WriteMacInt32(dce + dCtlPosition, 0);
196
197 // Install drives
198 for (DriveInfo *info = first_drive_info; info; info = info->next) {
199
200 info->num = FindFreeDriveNumber(1);
201 info->to_be_mounted = false;
202
203 if (info->fh) {
204
205 // Allocate drive status record
206 M68kRegisters r;
207 r.d[0] = SIZEOF_DrvSts;
208 Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
209 if (r.a[0] == 0)
210 continue;
211 info->status = r.a[0];
212 D(bug(" DrvSts at %08lx\n", info->status));
213
214 // Set up drive status
215 WriteMacInt16(info->status + dsQType, hard20);
216 WriteMacInt8(info->status + dsInstalled, 1);
217 bool disk_in_place = false;
218 if (SysIsFixedDisk(info->fh)) {
219 WriteMacInt8(info->status + dsDiskInPlace, 8); // Fixed disk
220 disk_in_place = true;
221 } else if (SysIsDiskInserted(info->fh)) {
222 WriteMacInt8(info->status + dsDiskInPlace, 1); // Inserted removable disk
223 disk_in_place = true;
224 }
225 if (disk_in_place) {
226 D(bug(" disk inserted\n"));
227 WriteMacInt8(info->status + dsWriteProt, info->read_only ? 0x80 : 0);
228 info->num_blocks = SysGetFileSize(info->fh) / 512;
229 info->to_be_mounted = true;
230 }
231 D(bug(" %ld blocks\n", info->num_blocks));
232 WriteMacInt16(info->status + dsDriveSize, info->num_blocks & 0xffff);
233 WriteMacInt16(info->status + dsDriveS1, info->num_blocks >> 16);
234
235 // Add drive to drive queue
236 D(bug(" adding drive %d\n", info->num));
237 r.d[0] = (info->num << 16) | (DiskRefNum & 0xffff);
238 r.a[0] = info->status + dsQLink;
239 Execute68kTrap(0xa04e, &r); // AddDrive()
240 }
241 }
242 return noErr;
243 }
244
245
246 /*
247 * Driver Prime() routine
248 */
249
250 int16 DiskPrime(uint32 pb, uint32 dce)
251 {
252 WriteMacInt32(pb + ioActCount, 0);
253
254 // Drive valid and disk inserted?
255 DriveInfo *info;
256 if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL)
257 return nsDrvErr;
258 if (!ReadMacInt8(info->status + dsDiskInPlace))
259 return offLinErr;
260
261 // Get parameters
262 void *buffer = Mac2HostAddr(ReadMacInt32(pb + ioBuffer));
263 size_t length = ReadMacInt32(pb + ioReqCount);
264 loff_t position = ReadMacInt32(dce + dCtlPosition);
265 if (ReadMacInt16(pb + ioPosMode) & 0x100) // 64 bit positioning
266 position = ((loff_t)ReadMacInt32(pb + ioWPosOffset) << 32) || ReadMacInt32(pb + ioWPosOffset + 4);
267 if ((length & 0x1ff) || (position & 0x1ff))
268 return paramErr;
269
270 size_t actual = 0;
271 if ((ReadMacInt16(pb + ioTrap) & 0xff) == aRdCmd) {
272
273 // Read
274 actual = Sys_read(info->fh, buffer, position, length);
275 if (actual != length)
276 return readErr;
277
278 } else {
279
280 // Write
281 if (info->read_only)
282 return wPrErr;
283 actual = Sys_write(info->fh, buffer, position, length);
284 if (actual != length)
285 return writErr;
286 }
287
288 // Update ParamBlock and DCE
289 WriteMacInt32(pb + ioActCount, actual);
290 WriteMacInt32(dce + dCtlPosition, ReadMacInt32(dce + dCtlPosition) + actual);
291 return noErr;
292 }
293
294
295 /*
296 * Driver Control() routine
297 */
298
299 int16 DiskControl(uint32 pb, uint32 dce)
300 {
301 uint16 code = ReadMacInt16(pb + csCode);
302 D(bug("DiskControl %d\n", code));
303
304 // General codes
305 switch (code) {
306 case 1: // KillIO
307 return noErr;
308
309 case 65: { // Periodic action ("insert" disks on startup and check for disk changes)
310 DriveInfo *info = first_drive_info;
311 while (info != NULL) {
312
313 // Disk in drive?
314 if (!ReadMacInt8(info->status + dsDiskInPlace)) {
315
316 // No, check if disk was inserted
317 if (SysIsDiskInserted(info->fh))
318 DiskMountVolume(info->fh);
319 }
320
321 // Mount disk if flagged
322 if (info->to_be_mounted) {
323 D(bug(" mounting drive %d\n", info->num));
324 M68kRegisters r;
325 r.d[0] = info->num;
326 r.a[0] = 7; // diskEvent
327 Execute68kTrap(0xa02f, &r); // PostEvent()
328 info->to_be_mounted = false;
329 }
330
331 info = info->next;
332 }
333 return noErr;
334 }
335 }
336
337 // Drive valid?
338 DriveInfo *info;
339 if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL)
340 return nsDrvErr;
341
342 // Drive-specific codes
343 switch (code) {
344 case 5: // Verify disk
345 if (ReadMacInt8(info->status + dsDiskInPlace) > 0)
346 return noErr;
347 else
348 return offLinErr;
349
350 case 6: // Format disk
351 if (info->read_only)
352 return wPrErr;
353 else if (ReadMacInt8(info->status + dsDiskInPlace) > 0)
354 return noErr;
355 else
356 return offLinErr;
357
358 case 7: // Eject disk
359 if (ReadMacInt8(info->status + dsDiskInPlace) == 8) {
360 // Fixed disk, re-insert
361 M68kRegisters r;
362 r.d[0] = info->num;
363 r.a[0] = 7; // diskEvent
364 Execute68kTrap(0xa02f, &r); // PostEvent()
365 } else if (ReadMacInt8(info->status + dsDiskInPlace) > 0) {
366 SysEject(info->fh);
367 WriteMacInt8(info->status + dsDiskInPlace, 0);
368 }
369 return noErr;
370
371 case 21: // Get drive icon
372 case 22: // Get disk icon
373 WriteMacInt32(pb + csParam, DiskIconAddr);
374 return noErr;
375
376 case 23: // Get drive info
377 if (ReadMacInt8(info->status + dsDiskInPlace) == 8)
378 WriteMacInt32(pb + csParam, 0x0601); // Unspecified fixed SCSI disk
379 else
380 WriteMacInt32(pb + csParam, 0x0201); // Unspecified SCSI disk
381 return noErr;
382
383 case 24: // Get partition size
384 if (ReadMacInt8(info->status + dsDiskInPlace) > 0) {
385 WriteMacInt32(pb + csParam, info->num_blocks);
386 return noErr;
387 } else
388 return offLinErr;
389
390 default:
391 printf("WARNING: Unknown DiskControl(%d)\n", code);
392 return controlErr;
393 }
394 }
395
396
397 /*
398 * Driver Status() routine
399 */
400
401 int16 DiskStatus(uint32 pb, uint32 dce)
402 {
403 DriveInfo *info = get_drive_info(ReadMacInt16(pb + ioVRefNum));
404 uint16 code = ReadMacInt16(pb + csCode);
405 D(bug("DiskStatus %d\n", code));
406
407 // General codes
408 switch (code) {
409 case 43: { // Driver gestalt
410 uint32 sel = ReadMacInt32(pb + csParam);
411 D(bug(" driver gestalt %c%c%c%c\n", sel >> 24, sel >> 16, sel >> 8, sel));
412 switch (sel) {
413 case 'vers': // Version
414 WriteMacInt32(pb + csParam + 4, 0x01008000);
415 break;
416 case 'devt': // Device type
417 if (info != NULL) {
418 if (ReadMacInt8(info->status + dsDiskInPlace) == 8)
419 WriteMacInt32(pb + csParam + 4, 'disk');
420 else
421 WriteMacInt32(pb + csParam + 4, 'rdsk');
422 } else
423 WriteMacInt32(pb + csParam + 4, 'disk');
424 break;
425 case 'intf': // Interface type
426 WriteMacInt32(pb + csParam + 4, 'basi');
427 break;
428 case 'sync': // Only synchronous operation?
429 WriteMacInt32(pb + csParam + 4, 0x01000000);
430 break;
431 case 'boot': // Boot ID
432 if (info != NULL)
433 WriteMacInt16(pb + csParam + 4, info->num);
434 else
435 WriteMacInt16(pb + csParam + 4, 0);
436 WriteMacInt16(pb + csParam + 6, (uint16)DiskRefNum);
437 break;
438 case 'wide': // 64-bit access supported?
439 WriteMacInt16(pb + csParam + 4, 0x0100);
440 break;
441 case 'purg': // Purge flags
442 WriteMacInt32(pb + csParam + 4, 0);
443 break;
444 case 'ejec': // Eject flags
445 WriteMacInt32(pb + csParam + 4, 0x00030003); // Don't eject on shutdown/restart
446 break;
447 case 'flus': // Flush flags
448 WriteMacInt16(pb + csParam + 4, 0);
449 break;
450 case 'vmop': // Virtual memory attributes
451 WriteMacInt32(pb + csParam + 4, 0); // Drive not available for VM
452 break;
453 default:
454 return statusErr;
455 }
456 return noErr;
457 }
458 }
459
460 // Drive valid?
461 if (info == NULL)
462 return nsDrvErr;
463
464 // Drive-specific codes
465 switch (code) {
466 case 8: // Get drive status
467 memcpy(Mac2HostAddr(pb + csParam), Mac2HostAddr(info->status), 22);
468 return noErr;
469
470 default:
471 printf("WARNING: Unknown DiskStatus(%d)\n", code);
472 return statusErr;
473 }
474 }