ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/disk.cpp
Revision: 1.19
Committed: 2008-01-01T09:40:31Z (16 years, 4 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.18: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * disk.cpp - Generic disk driver
3     *
4 gbeauche 1.19 * Basilisk II (C) 1997-2008 Christian Bauer
5 cebix 1.1 *
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 cebix 1.12 #include "sysdeps.h"
30    
31 cebix 1.1 #include <string.h>
32 cebix 1.10 #include <vector>
33    
34     #ifndef NO_STD_NAMESPACE
35     using std::vector;
36     #endif
37 cebix 1.1
38     #include "cpu_emulation.h"
39     #include "main.h"
40     #include "macos_util.h"
41     #include "sys.h"
42     #include "prefs.h"
43     #include "disk.h"
44    
45     #define DEBUG 0
46     #include "debug.h"
47    
48    
49     // .Disk Disk/drive icon
50     const uint8 DiskIcon[258] = {
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x7f, 0xff, 0xff, 0xfe,
56     0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01,
57     0x80, 0x00, 0x00, 0x01, 0x8c, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01,
58     0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59    
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe,
65     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67     0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68    
69     0, 0
70     };
71    
72    
73     // Struct for each drive
74 cebix 1.10 struct disk_drive_info {
75 cebix 1.11 disk_drive_info() : num(0), fh(NULL), start_byte(0), read_only(false), status(0) {}
76 cebix 1.10 disk_drive_info(void *fh_, bool ro) : num(0), fh(fh_), read_only(ro), status(0) {}
77    
78     void close_fh(void) { Sys_close(fh); }
79 cebix 1.1
80     int num; // Drive number
81     void *fh; // File handle
82 cebix 1.11 loff_t start_byte; // Start of HFS partition on disk
83 cebix 1.1 uint32 num_blocks; // Size in 512-byte blocks
84     bool to_be_mounted; // Flag: drive must be mounted in accRun
85     bool read_only; // Flag: force write protection
86     uint32 status; // Mac address of drive status record
87     };
88    
89 cebix 1.10 // List of drives handled by this driver
90     typedef vector<disk_drive_info> drive_vec;
91     static drive_vec drives;
92 cebix 1.1
93     // Icon address (Mac address space, set by PatchROM())
94     uint32 DiskIconAddr;
95    
96 cebix 1.2 // Flag: Control(accRun) has been called, interrupt routine is now active
97     static bool acc_run_called = false;
98    
99 cebix 1.1
100     /*
101 cebix 1.10 * Get pointer to drive info or drives.end() if not found
102 cebix 1.1 */
103    
104 cebix 1.10 static drive_vec::iterator get_drive_info(int num)
105 cebix 1.1 {
106 cebix 1.10 drive_vec::iterator info, end = drives.end();
107     for (info = drives.begin(); info != end; ++info) {
108 cebix 1.1 if (info->num == num)
109     return info;
110     }
111 cebix 1.10 return info;
112 cebix 1.1 }
113    
114    
115     /*
116 cebix 1.11 * Find HFS partition, set info->start_byte and info->num_blocks
117     * (0 = no partition map or HFS partition found, assume flat disk image)
118     */
119    
120     static void find_hfs_partition(disk_drive_info &info)
121     {
122     info.start_byte = 0;
123     info.num_blocks = 0;
124     uint8 *map = new uint8[512];
125    
126     // Search first 64 blocks for HFS partition
127     for (int i=0; i<64; i++) {
128     if (Sys_read(info.fh, map, i * 512, 512) != 512)
129     break;
130    
131     // Not a partition map block? Then look at next block
132     uint16 sig = (map[0] << 8) | map[1];
133     if (sig != 0x504d)
134     continue;
135    
136     // Partition map block found, Apple HFS partition?
137     if (strcmp((char *)(map + 48), "Apple_HFS") == 0) {
138 cebix 1.14 info.start_byte = (loff_t)((map[8] << 24) | (map[9] << 16) | (map[10] << 8) | map[11]) << 9;
139     info.num_blocks = (map[12] << 24) | (map[13] << 16) | (map[14] << 8) | map[15];
140 cebix 1.11 D(bug(" HFS partition found at %d, %d blocks\n", info.start_byte, info.num_blocks));
141     break;
142     }
143     }
144     delete[] map;
145     }
146    
147    
148     /*
149 cebix 1.1 * Initialization
150     */
151    
152     void DiskInit(void)
153     {
154     // No drives specified in prefs? Then add defaults
155     if (PrefsFindString("disk", 0) == NULL)
156     SysAddDiskPrefs();
157    
158     // Add drives specified in preferences
159 cebix 1.10 int index = 0;
160 cebix 1.1 const char *str;
161     while ((str = PrefsFindString("disk", index++)) != NULL) {
162     bool read_only = false;
163     if (str[0] == '*') {
164     read_only = true;
165     str++;
166     }
167     void *fh = Sys_open(str, read_only);
168 cebix 1.10 if (fh)
169     drives.push_back(disk_drive_info(fh, SysIsReadOnly(fh)));
170 cebix 1.1 }
171     }
172    
173    
174     /*
175     * Deinitialization
176     */
177    
178     void DiskExit(void)
179     {
180 cebix 1.10 drive_vec::iterator info, end = drives.end();
181     for (info = drives.begin(); info != end; ++info)
182     info->close_fh();
183     drives.clear();
184 cebix 1.1 }
185    
186    
187     /*
188     * Disk was inserted, flag for mounting
189     */
190    
191     bool DiskMountVolume(void *fh)
192     {
193 cebix 1.10 drive_vec::iterator info = drives.begin(), end = drives.end();
194     while (info != end && info->fh != fh)
195     ++info;
196     if (info != end) {
197 cebix 1.1 if (SysIsDiskInserted(info->fh)) {
198     info->read_only = SysIsReadOnly(info->fh);
199     WriteMacInt8(info->status + dsDiskInPlace, 1); // Inserted removable disk
200     WriteMacInt8(info->status + dsWriteProt, info->read_only ? 0xff : 0);
201 cebix 1.11 find_hfs_partition(*info);
202     if (info->start_byte == 0)
203     info->num_blocks = SysGetFileSize(info->fh) / 512;
204 cebix 1.1 WriteMacInt16(info->status + dsDriveSize, info->num_blocks & 0xffff);
205     WriteMacInt16(info->status + dsDriveS1, info->num_blocks >> 16);
206     info->to_be_mounted = true;
207     }
208     return true;
209     } else
210     return false;
211     }
212    
213    
214     /*
215 cebix 1.2 * Mount volumes for which the to_be_mounted flag is set
216     * (called during interrupt time)
217     */
218    
219     static void mount_mountable_volumes(void)
220     {
221 cebix 1.10 drive_vec::iterator info, end = drives.end();
222     for (info = drives.begin(); info != end; ++info) {
223 cebix 1.2
224     // Disk in drive?
225     if (!ReadMacInt8(info->status + dsDiskInPlace)) {
226    
227     // No, check if disk was inserted
228     if (SysIsDiskInserted(info->fh))
229     DiskMountVolume(info->fh);
230     }
231    
232     // Mount disk if flagged
233     if (info->to_be_mounted) {
234     D(bug(" mounting drive %d\n", info->num));
235     M68kRegisters r;
236     r.d[0] = info->num;
237     r.a[0] = 7; // diskEvent
238     Execute68kTrap(0xa02f, &r); // PostEvent()
239     info->to_be_mounted = false;
240     }
241     }
242     }
243    
244    
245     /*
246 cebix 1.1 * Driver Open() routine
247     */
248    
249     int16 DiskOpen(uint32 pb, uint32 dce)
250     {
251     D(bug("DiskOpen\n"));
252    
253     // Set up DCE
254     WriteMacInt32(dce + dCtlPosition, 0);
255 cebix 1.2 acc_run_called = false;
256 cebix 1.1
257     // Install drives
258 cebix 1.10 drive_vec::iterator info, end = drives.end();
259     for (info = drives.begin(); info != end; ++info) {
260 cebix 1.1
261     info->num = FindFreeDriveNumber(1);
262     info->to_be_mounted = false;
263    
264     if (info->fh) {
265    
266     // Allocate drive status record
267     M68kRegisters r;
268     r.d[0] = SIZEOF_DrvSts;
269     Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
270     if (r.a[0] == 0)
271     continue;
272     info->status = r.a[0];
273     D(bug(" DrvSts at %08lx\n", info->status));
274    
275     // Set up drive status
276     WriteMacInt16(info->status + dsQType, hard20);
277     WriteMacInt8(info->status + dsInstalled, 1);
278     bool disk_in_place = false;
279     if (SysIsFixedDisk(info->fh)) {
280     WriteMacInt8(info->status + dsDiskInPlace, 8); // Fixed disk
281     disk_in_place = true;
282     } else if (SysIsDiskInserted(info->fh)) {
283     WriteMacInt8(info->status + dsDiskInPlace, 1); // Inserted removable disk
284     disk_in_place = true;
285     }
286     if (disk_in_place) {
287     D(bug(" disk inserted\n"));
288     WriteMacInt8(info->status + dsWriteProt, info->read_only ? 0x80 : 0);
289 cebix 1.11 find_hfs_partition(*info);
290     if (info->start_byte == 0)
291     info->num_blocks = SysGetFileSize(info->fh) / 512;
292 cebix 1.1 info->to_be_mounted = true;
293     }
294 cebix 1.3 D(bug(" %d blocks\n", info->num_blocks));
295 cebix 1.1 WriteMacInt16(info->status + dsDriveSize, info->num_blocks & 0xffff);
296     WriteMacInt16(info->status + dsDriveS1, info->num_blocks >> 16);
297    
298     // Add drive to drive queue
299     D(bug(" adding drive %d\n", info->num));
300     r.d[0] = (info->num << 16) | (DiskRefNum & 0xffff);
301     r.a[0] = info->status + dsQLink;
302     Execute68kTrap(0xa04e, &r); // AddDrive()
303     }
304     }
305     return noErr;
306     }
307    
308    
309     /*
310     * Driver Prime() routine
311     */
312    
313     int16 DiskPrime(uint32 pb, uint32 dce)
314     {
315     WriteMacInt32(pb + ioActCount, 0);
316    
317     // Drive valid and disk inserted?
318 cebix 1.10 drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum));
319     if (info == drives.end())
320 cebix 1.1 return nsDrvErr;
321     if (!ReadMacInt8(info->status + dsDiskInPlace))
322     return offLinErr;
323    
324     // Get parameters
325     void *buffer = Mac2HostAddr(ReadMacInt32(pb + ioBuffer));
326     size_t length = ReadMacInt32(pb + ioReqCount);
327     loff_t position = ReadMacInt32(dce + dCtlPosition);
328     if (ReadMacInt16(pb + ioPosMode) & 0x100) // 64 bit positioning
329 gbeauche 1.17 position = ((loff_t)ReadMacInt32(pb + ioWPosOffset) << 32) | ReadMacInt32(pb + ioWPosOffset + 4);
330 cebix 1.1 if ((length & 0x1ff) || (position & 0x1ff))
331     return paramErr;
332    
333     size_t actual = 0;
334     if ((ReadMacInt16(pb + ioTrap) & 0xff) == aRdCmd) {
335    
336     // Read
337 cebix 1.11 actual = Sys_read(info->fh, buffer, position + info->start_byte, length);
338 cebix 1.1 if (actual != length)
339     return readErr;
340    
341     } else {
342    
343     // Write
344     if (info->read_only)
345     return wPrErr;
346 cebix 1.11 actual = Sys_write(info->fh, buffer, position + info->start_byte, length);
347 cebix 1.1 if (actual != length)
348     return writErr;
349     }
350    
351     // Update ParamBlock and DCE
352     WriteMacInt32(pb + ioActCount, actual);
353     WriteMacInt32(dce + dCtlPosition, ReadMacInt32(dce + dCtlPosition) + actual);
354     return noErr;
355     }
356    
357    
358     /*
359     * Driver Control() routine
360     */
361    
362     int16 DiskControl(uint32 pb, uint32 dce)
363     {
364     uint16 code = ReadMacInt16(pb + csCode);
365     D(bug("DiskControl %d\n", code));
366    
367     // General codes
368     switch (code) {
369     case 1: // KillIO
370     return noErr;
371    
372 cebix 1.2 case 65: { // Periodic action (accRun, "insert" disks on startup)
373     mount_mountable_volumes();
374     WriteMacInt16(dce + dCtlFlags, ReadMacInt16(dce + dCtlFlags) & ~0x2000); // Disable periodic action
375     acc_run_called = true;
376 cebix 1.1 return noErr;
377     }
378     }
379    
380     // Drive valid?
381 cebix 1.10 drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum));
382     if (info == drives.end())
383 cebix 1.1 return nsDrvErr;
384    
385     // Drive-specific codes
386     switch (code) {
387     case 5: // Verify disk
388     if (ReadMacInt8(info->status + dsDiskInPlace) > 0)
389     return noErr;
390     else
391     return offLinErr;
392    
393     case 6: // Format disk
394     if (info->read_only)
395     return wPrErr;
396     else if (ReadMacInt8(info->status + dsDiskInPlace) > 0)
397     return noErr;
398     else
399     return offLinErr;
400    
401     case 7: // Eject disk
402     if (ReadMacInt8(info->status + dsDiskInPlace) == 8) {
403     // Fixed disk, re-insert
404     M68kRegisters r;
405     r.d[0] = info->num;
406     r.a[0] = 7; // diskEvent
407     Execute68kTrap(0xa02f, &r); // PostEvent()
408     } else if (ReadMacInt8(info->status + dsDiskInPlace) > 0) {
409     SysEject(info->fh);
410     WriteMacInt8(info->status + dsDiskInPlace, 0);
411     }
412     return noErr;
413    
414     case 21: // Get drive icon
415     case 22: // Get disk icon
416     WriteMacInt32(pb + csParam, DiskIconAddr);
417     return noErr;
418    
419     case 23: // Get drive info
420     if (ReadMacInt8(info->status + dsDiskInPlace) == 8)
421     WriteMacInt32(pb + csParam, 0x0601); // Unspecified fixed SCSI disk
422     else
423     WriteMacInt32(pb + csParam, 0x0201); // Unspecified SCSI disk
424     return noErr;
425    
426     case 24: // Get partition size
427     if (ReadMacInt8(info->status + dsDiskInPlace) > 0) {
428     WriteMacInt32(pb + csParam, info->num_blocks);
429     return noErr;
430     } else
431     return offLinErr;
432    
433     default:
434     printf("WARNING: Unknown DiskControl(%d)\n", code);
435     return controlErr;
436     }
437     }
438    
439    
440     /*
441     * Driver Status() routine
442     */
443    
444     int16 DiskStatus(uint32 pb, uint32 dce)
445     {
446 cebix 1.10 drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum));
447 cebix 1.1 uint16 code = ReadMacInt16(pb + csCode);
448     D(bug("DiskStatus %d\n", code));
449    
450 cebix 1.10 // General codes (we can get these even if the drive was invalid)
451 cebix 1.1 switch (code) {
452     case 43: { // Driver gestalt
453     uint32 sel = ReadMacInt32(pb + csParam);
454     D(bug(" driver gestalt %c%c%c%c\n", sel >> 24, sel >> 16, sel >> 8, sel));
455     switch (sel) {
456 cebix 1.7 case FOURCC('v','e','r','s'): // Version
457 cebix 1.1 WriteMacInt32(pb + csParam + 4, 0x01008000);
458     break;
459 cebix 1.7 case FOURCC('d','e','v','t'): // Device type
460 cebix 1.10 if (info != drives.end()) {
461 cebix 1.1 if (ReadMacInt8(info->status + dsDiskInPlace) == 8)
462 cebix 1.7 WriteMacInt32(pb + csParam + 4, FOURCC('d','i','s','k'));
463 cebix 1.1 else
464 cebix 1.7 WriteMacInt32(pb + csParam + 4, FOURCC('r','d','s','k'));
465 cebix 1.1 } else
466 cebix 1.7 WriteMacInt32(pb + csParam + 4, FOURCC('d','i','s','k'));
467 cebix 1.1 break;
468 cebix 1.7 case FOURCC('i','n','t','f'): // Interface type
469 cebix 1.8 WriteMacInt32(pb + csParam + 4, EMULATOR_ID_4);
470 cebix 1.1 break;
471 cebix 1.7 case FOURCC('s','y','n','c'): // Only synchronous operation?
472 cebix 1.1 WriteMacInt32(pb + csParam + 4, 0x01000000);
473     break;
474 cebix 1.7 case FOURCC('b','o','o','t'): // Boot ID
475 cebix 1.10 if (info != drives.end())
476 cebix 1.1 WriteMacInt16(pb + csParam + 4, info->num);
477     else
478     WriteMacInt16(pb + csParam + 4, 0);
479     WriteMacInt16(pb + csParam + 6, (uint16)DiskRefNum);
480     break;
481 cebix 1.7 case FOURCC('w','i','d','e'): // 64-bit access supported?
482 cebix 1.1 WriteMacInt16(pb + csParam + 4, 0x0100);
483     break;
484 cebix 1.7 case FOURCC('p','u','r','g'): // Purge flags
485 cebix 1.1 WriteMacInt32(pb + csParam + 4, 0);
486     break;
487 cebix 1.7 case FOURCC('e','j','e','c'): // Eject flags
488 cebix 1.1 WriteMacInt32(pb + csParam + 4, 0x00030003); // Don't eject on shutdown/restart
489     break;
490 cebix 1.7 case FOURCC('f','l','u','s'): // Flush flags
491 cebix 1.1 WriteMacInt16(pb + csParam + 4, 0);
492     break;
493 cebix 1.7 case FOURCC('v','m','o','p'): // Virtual memory attributes
494 cebix 1.1 WriteMacInt32(pb + csParam + 4, 0); // Drive not available for VM
495     break;
496     default:
497     return statusErr;
498     }
499     return noErr;
500     }
501     }
502    
503     // Drive valid?
504 cebix 1.10 if (info == drives.end())
505 cebix 1.1 return nsDrvErr;
506    
507     // Drive-specific codes
508     switch (code) {
509     case 8: // Get drive status
510 cebix 1.4 Mac2Mac_memcpy(pb + csParam, info->status, 22);
511 cebix 1.1 return noErr;
512    
513 asvitkine 1.18 case 44: // get startup partition status: http://developer.apple.com/documentation/Hardware/DeviceManagers/ata/ata_ref/ATA.21.html
514     printf("WARNING: DiskStatus(44:'get startup partition status') Not Implemented\n");
515     return statusErr;
516    
517     case 45: // get partition write protect status: http://developer.apple.com/documentation/Hardware/DeviceManagers/ata/ata_ref/ATA.23.html
518     printf("WARNING: DiskStatus(45:'get partition write protect status') Not Implemented\n");
519     return statusErr;
520    
521     case 46: // get partition mount status: http://developer.apple.com/documentation/Hardware/DeviceManagers/ata/ata_ref/ATA.22.html
522     printf("WARNING: DiskStatus(46:'get partition mount status') Not Implemented\n");
523     return statusErr;
524    
525     case 70: // get power mode status: http://developer.apple.com/documentation/Hardware/DeviceManagers/ata/ata_ref/ATA.24.html
526     printf("WARNING: DiskStatus(70:'get power mode status') Not Implemented\n");
527     return statusErr;
528    
529 cebix 1.1 default:
530     printf("WARNING: Unknown DiskStatus(%d)\n", code);
531     return statusErr;
532 cebix 1.2 }
533     }
534    
535    
536     /*
537 cebix 1.6 * Driver interrupt routine (1Hz) - check for volumes to be mounted
538 cebix 1.2 */
539    
540     void DiskInterrupt(void)
541     {
542     if (!acc_run_called)
543     return;
544    
545 cebix 1.6 mount_mountable_volumes();
546 cebix 1.1 }