1 |
|
/* |
2 |
|
* disk.cpp - Generic disk driver |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2004 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 |
26 |
|
* Technote FL 24: "Don't Look at ioPosOffset for Devices" |
27 |
|
*/ |
28 |
|
|
29 |
+ |
#include "sysdeps.h" |
30 |
+ |
|
31 |
|
#include <string.h> |
32 |
+ |
#include <vector> |
33 |
+ |
|
34 |
+ |
#ifndef NO_STD_NAMESPACE |
35 |
+ |
using std::vector; |
36 |
+ |
#endif |
37 |
|
|
31 |
– |
#include "sysdeps.h" |
38 |
|
#include "cpu_emulation.h" |
39 |
|
#include "main.h" |
40 |
|
#include "macos_util.h" |
71 |
|
|
72 |
|
|
73 |
|
// Struct for each drive |
74 |
< |
struct DriveInfo { |
75 |
< |
DriveInfo() |
76 |
< |
{ |
77 |
< |
next = NULL; |
78 |
< |
num = 0; |
73 |
< |
fh = NULL; |
74 |
< |
read_only = false; |
75 |
< |
status = 0; |
76 |
< |
} |
74 |
> |
struct disk_drive_info { |
75 |
> |
disk_drive_info() : num(0), fh(NULL), start_byte(0), read_only(false), status(0) {} |
76 |
> |
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 |
|
|
78 |
– |
DriveInfo *next; // Pointer to next DriveInfo (must be first in struct!) |
80 |
|
int num; // Drive number |
81 |
|
void *fh; // File handle |
82 |
+ |
loff_t start_byte; // Start of HFS partition on disk |
83 |
|
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 |
< |
// Linked list of DriveInfos |
90 |
< |
static DriveInfo *first_drive_info; |
89 |
> |
// List of drives handled by this driver |
90 |
> |
typedef vector<disk_drive_info> drive_vec; |
91 |
> |
static drive_vec drives; |
92 |
|
|
93 |
|
// Icon address (Mac address space, set by PatchROM()) |
94 |
|
uint32 DiskIconAddr; |
95 |
|
|
93 |
– |
// Number of ticks between checks for disk insertion |
94 |
– |
const int driver_delay = 120; |
95 |
– |
|
96 |
|
// Flag: Control(accRun) has been called, interrupt routine is now active |
97 |
|
static bool acc_run_called = false; |
98 |
|
|
99 |
|
|
100 |
|
/* |
101 |
< |
* Get pointer to drive info, NULL = invalid drive number |
101 |
> |
* Get pointer to drive info or drives.end() if not found |
102 |
|
*/ |
103 |
|
|
104 |
< |
static DriveInfo *get_drive_info(int num) |
104 |
> |
static drive_vec::iterator get_drive_info(int num) |
105 |
|
{ |
106 |
< |
DriveInfo *info = first_drive_info; |
107 |
< |
while (info != NULL) { |
106 |
> |
drive_vec::iterator info, end = drives.end(); |
107 |
> |
for (info = drives.begin(); info != end; ++info) { |
108 |
|
if (info->num == num) |
109 |
|
return info; |
110 |
– |
info = info->next; |
110 |
|
} |
111 |
< |
return NULL; |
111 |
> |
return info; |
112 |
> |
} |
113 |
> |
|
114 |
> |
|
115 |
> |
/* |
116 |
> |
* 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 |
> |
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 |
> |
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 |
|
|
151 |
|
|
152 |
|
void DiskInit(void) |
153 |
|
{ |
122 |
– |
first_drive_info = NULL; |
123 |
– |
|
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 |
< |
int32 index = 0; |
159 |
> |
int index = 0; |
160 |
|
const char *str; |
161 |
|
while ((str = PrefsFindString("disk", index++)) != NULL) { |
162 |
|
bool read_only = false; |
165 |
|
str++; |
166 |
|
} |
167 |
|
void *fh = Sys_open(str, read_only); |
168 |
< |
if (fh) { |
169 |
< |
D(bug(" adding drive '%s'\n", str)); |
140 |
< |
DriveInfo *info = new DriveInfo; |
141 |
< |
info->fh = fh; |
142 |
< |
info->read_only = SysIsReadOnly(fh); |
143 |
< |
DriveInfo *p = (DriveInfo *)&first_drive_info; |
144 |
< |
while (p->next != NULL) |
145 |
< |
p = p->next; |
146 |
< |
p->next = info; |
147 |
< |
} |
168 |
> |
if (fh) |
169 |
> |
drives.push_back(disk_drive_info(fh, SysIsReadOnly(fh))); |
170 |
|
} |
171 |
|
} |
172 |
|
|
177 |
|
|
178 |
|
void DiskExit(void) |
179 |
|
{ |
180 |
< |
DriveInfo *info = first_drive_info, *next; |
181 |
< |
while (info != NULL) { |
182 |
< |
Sys_close(info->fh); |
183 |
< |
next = info->next; |
162 |
< |
delete info; |
163 |
< |
info = next; |
164 |
< |
} |
180 |
> |
drive_vec::iterator info, end = drives.end(); |
181 |
> |
for (info = drives.begin(); info != end; ++info) |
182 |
> |
info->close_fh(); |
183 |
> |
drives.clear(); |
184 |
|
} |
185 |
|
|
186 |
|
|
190 |
|
|
191 |
|
bool DiskMountVolume(void *fh) |
192 |
|
{ |
193 |
< |
DriveInfo *info; |
194 |
< |
for (info = first_drive_info; info != NULL && info->fh != fh; info = info->next) ; |
195 |
< |
if (info) { |
193 |
> |
drive_vec::iterator info = drives.begin(), end = drives.end(); |
194 |
> |
while (info != end && info->fh != fh) |
195 |
> |
++info; |
196 |
> |
if (info != end) { |
197 |
|
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 |
< |
info->num_blocks = SysGetFileSize(info->fh) / 512; |
201 |
> |
find_hfs_partition(*info); |
202 |
> |
if (info->start_byte == 0) |
203 |
> |
info->num_blocks = SysGetFileSize(info->fh) / 512; |
204 |
|
WriteMacInt16(info->status + dsDriveSize, info->num_blocks & 0xffff); |
205 |
|
WriteMacInt16(info->status + dsDriveS1, info->num_blocks >> 16); |
206 |
|
info->to_be_mounted = true; |
218 |
|
|
219 |
|
static void mount_mountable_volumes(void) |
220 |
|
{ |
221 |
< |
DriveInfo *info = first_drive_info; |
222 |
< |
while (info != NULL) { |
221 |
> |
drive_vec::iterator info, end = drives.end(); |
222 |
> |
for (info = drives.begin(); info != end; ++info) { |
223 |
|
|
224 |
|
// Disk in drive? |
225 |
|
if (!ReadMacInt8(info->status + dsDiskInPlace)) { |
238 |
|
Execute68kTrap(0xa02f, &r); // PostEvent() |
239 |
|
info->to_be_mounted = false; |
240 |
|
} |
219 |
– |
|
220 |
– |
info = info->next; |
241 |
|
} |
242 |
|
} |
243 |
|
|
255 |
|
acc_run_called = false; |
256 |
|
|
257 |
|
// Install drives |
258 |
< |
for (DriveInfo *info = first_drive_info; info; info = info->next) { |
258 |
> |
drive_vec::iterator info, end = drives.end(); |
259 |
> |
for (info = drives.begin(); info != end; ++info) { |
260 |
|
|
261 |
|
info->num = FindFreeDriveNumber(1); |
262 |
|
info->to_be_mounted = false; |
286 |
|
if (disk_in_place) { |
287 |
|
D(bug(" disk inserted\n")); |
288 |
|
WriteMacInt8(info->status + dsWriteProt, info->read_only ? 0x80 : 0); |
289 |
< |
info->num_blocks = SysGetFileSize(info->fh) / 512; |
289 |
> |
find_hfs_partition(*info); |
290 |
> |
if (info->start_byte == 0) |
291 |
> |
info->num_blocks = SysGetFileSize(info->fh) / 512; |
292 |
|
info->to_be_mounted = true; |
293 |
|
} |
294 |
|
D(bug(" %d blocks\n", info->num_blocks)); |
315 |
|
WriteMacInt32(pb + ioActCount, 0); |
316 |
|
|
317 |
|
// Drive valid and disk inserted? |
318 |
< |
DriveInfo *info; |
319 |
< |
if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL) |
318 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
319 |
> |
if (info == drives.end()) |
320 |
|
return nsDrvErr; |
321 |
|
if (!ReadMacInt8(info->status + dsDiskInPlace)) |
322 |
|
return offLinErr; |
334 |
|
if ((ReadMacInt16(pb + ioTrap) & 0xff) == aRdCmd) { |
335 |
|
|
336 |
|
// Read |
337 |
< |
actual = Sys_read(info->fh, buffer, position, length); |
337 |
> |
actual = Sys_read(info->fh, buffer, position + info->start_byte, length); |
338 |
|
if (actual != length) |
339 |
|
return readErr; |
340 |
|
|
343 |
|
// Write |
344 |
|
if (info->read_only) |
345 |
|
return wPrErr; |
346 |
< |
actual = Sys_write(info->fh, buffer, position, length); |
346 |
> |
actual = Sys_write(info->fh, buffer, position + info->start_byte, length); |
347 |
|
if (actual != length) |
348 |
|
return writErr; |
349 |
|
} |
378 |
|
} |
379 |
|
|
380 |
|
// Drive valid? |
381 |
< |
DriveInfo *info; |
382 |
< |
if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL) |
381 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
382 |
> |
if (info == drives.end()) |
383 |
|
return nsDrvErr; |
384 |
|
|
385 |
|
// Drive-specific codes |
443 |
|
|
444 |
|
int16 DiskStatus(uint32 pb, uint32 dce) |
445 |
|
{ |
446 |
< |
DriveInfo *info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
446 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
447 |
|
uint16 code = ReadMacInt16(pb + csCode); |
448 |
|
D(bug("DiskStatus %d\n", code)); |
449 |
|
|
450 |
< |
// General codes |
450 |
> |
// General codes (we can get these even if the drive was invalid) |
451 |
|
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 |
< |
case 'vers': // Version |
456 |
> |
case FOURCC('v','e','r','s'): // Version |
457 |
|
WriteMacInt32(pb + csParam + 4, 0x01008000); |
458 |
|
break; |
459 |
< |
case 'devt': // Device type |
460 |
< |
if (info != NULL) { |
459 |
> |
case FOURCC('d','e','v','t'): // Device type |
460 |
> |
if (info != drives.end()) { |
461 |
|
if (ReadMacInt8(info->status + dsDiskInPlace) == 8) |
462 |
< |
WriteMacInt32(pb + csParam + 4, 'disk'); |
462 |
> |
WriteMacInt32(pb + csParam + 4, FOURCC('d','i','s','k')); |
463 |
|
else |
464 |
< |
WriteMacInt32(pb + csParam + 4, 'rdsk'); |
464 |
> |
WriteMacInt32(pb + csParam + 4, FOURCC('r','d','s','k')); |
465 |
|
} else |
466 |
< |
WriteMacInt32(pb + csParam + 4, 'disk'); |
466 |
> |
WriteMacInt32(pb + csParam + 4, FOURCC('d','i','s','k')); |
467 |
|
break; |
468 |
< |
case 'intf': // Interface type |
469 |
< |
WriteMacInt32(pb + csParam + 4, 'basi'); |
468 |
> |
case FOURCC('i','n','t','f'): // Interface type |
469 |
> |
WriteMacInt32(pb + csParam + 4, EMULATOR_ID_4); |
470 |
|
break; |
471 |
< |
case 'sync': // Only synchronous operation? |
471 |
> |
case FOURCC('s','y','n','c'): // Only synchronous operation? |
472 |
|
WriteMacInt32(pb + csParam + 4, 0x01000000); |
473 |
|
break; |
474 |
< |
case 'boot': // Boot ID |
475 |
< |
if (info != NULL) |
474 |
> |
case FOURCC('b','o','o','t'): // Boot ID |
475 |
> |
if (info != drives.end()) |
476 |
|
WriteMacInt16(pb + csParam + 4, info->num); |
477 |
|
else |
478 |
|
WriteMacInt16(pb + csParam + 4, 0); |
479 |
|
WriteMacInt16(pb + csParam + 6, (uint16)DiskRefNum); |
480 |
|
break; |
481 |
< |
case 'wide': // 64-bit access supported? |
481 |
> |
case FOURCC('w','i','d','e'): // 64-bit access supported? |
482 |
|
WriteMacInt16(pb + csParam + 4, 0x0100); |
483 |
|
break; |
484 |
< |
case 'purg': // Purge flags |
484 |
> |
case FOURCC('p','u','r','g'): // Purge flags |
485 |
|
WriteMacInt32(pb + csParam + 4, 0); |
486 |
|
break; |
487 |
< |
case 'ejec': // Eject flags |
487 |
> |
case FOURCC('e','j','e','c'): // Eject flags |
488 |
|
WriteMacInt32(pb + csParam + 4, 0x00030003); // Don't eject on shutdown/restart |
489 |
|
break; |
490 |
< |
case 'flus': // Flush flags |
490 |
> |
case FOURCC('f','l','u','s'): // Flush flags |
491 |
|
WriteMacInt16(pb + csParam + 4, 0); |
492 |
|
break; |
493 |
< |
case 'vmop': // Virtual memory attributes |
493 |
> |
case FOURCC('v','m','o','p'): // Virtual memory attributes |
494 |
|
WriteMacInt32(pb + csParam + 4, 0); // Drive not available for VM |
495 |
|
break; |
496 |
|
default: |
501 |
|
} |
502 |
|
|
503 |
|
// Drive valid? |
504 |
< |
if (info == NULL) |
504 |
> |
if (info == drives.end()) |
505 |
|
return nsDrvErr; |
506 |
|
|
507 |
|
// Drive-specific codes |
518 |
|
|
519 |
|
|
520 |
|
/* |
521 |
< |
* Driver interrupt routine - check for volumes to be mounted |
521 |
> |
* Driver interrupt routine (1Hz) - check for volumes to be mounted |
522 |
|
*/ |
523 |
|
|
524 |
|
void DiskInterrupt(void) |
525 |
|
{ |
503 |
– |
static int tick_count = 0; |
526 |
|
if (!acc_run_called) |
527 |
|
return; |
528 |
|
|
529 |
< |
tick_count++; |
508 |
< |
if (tick_count > driver_delay) { |
509 |
< |
tick_count = 0; |
510 |
< |
mount_mountable_volumes(); |
511 |
< |
} |
529 |
> |
mount_mountable_volumes(); |
530 |
|
} |