1 |
|
/* |
2 |
|
* sys_unix.cpp - System dependent routines, Unix implementation |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2002 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 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 |
24 |
|
#include <sys/stat.h> |
25 |
|
#include <errno.h> |
26 |
|
|
27 |
+ |
#ifdef HAVE_AVAILABILITYMACROS_H |
28 |
+ |
#include <AvailabilityMacros.h> |
29 |
+ |
#endif |
30 |
+ |
|
31 |
|
#ifdef __linux__ |
32 |
|
#include <sys/mount.h> |
33 |
|
#include <linux/cdrom.h> |
34 |
|
#include <linux/fd.h> |
35 |
|
#include <linux/major.h> |
36 |
|
#include <linux/kdev_t.h> |
33 |
– |
#include <linux/unistd.h> |
37 |
|
#include <dirent.h> |
35 |
– |
|
36 |
– |
#ifdef __NR__llseek |
37 |
– |
_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo, loff_t *, res, unsigned int, wh); |
38 |
– |
#else |
39 |
– |
static int _llseek(unsigned int fd, unsigned long hi, unsigned long lo, loff_t *res, unsigned int wh) |
40 |
– |
{ |
41 |
– |
if (hi) |
42 |
– |
return -1; |
43 |
– |
*res = lseek(fd, lo, wh); |
44 |
– |
if (*res == -1) |
45 |
– |
return -1; |
46 |
– |
return 0; |
47 |
– |
} |
48 |
– |
#endif |
38 |
|
#endif |
39 |
|
|
40 |
|
#if defined(__FreeBSD__) || defined(__NetBSD__) |
66 |
|
int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true) |
67 |
|
#elif defined(__FreeBSD__) |
68 |
|
struct ioc_capability cdrom_cap; |
69 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
70 |
+ |
char *ioctl_name; // For CDs on OS X - a device for special ioctls |
71 |
+ |
int ioctl_fd; |
72 |
|
#endif |
73 |
|
}; |
74 |
|
|
113 |
|
void SysAddFloppyPrefs(void) |
114 |
|
{ |
115 |
|
#if defined(__linux__) |
116 |
< |
if (access("/dev/.devfsd", F_OK) < 0) { |
117 |
< |
PrefsAddString("floppy", "/dev/fd0u1440"); |
118 |
< |
PrefsAddString("floppy", "/dev/fd1u1440"); |
119 |
< |
} else { |
120 |
< |
DIR *fd_dir = opendir("/dev/floppy"); |
121 |
< |
if (fd_dir) { |
122 |
< |
struct dirent *floppy_dev; |
123 |
< |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
132 |
< |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
133 |
< |
char fd_dev[20]; |
134 |
< |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
135 |
< |
PrefsAddString("floppy", fd_dev); |
136 |
< |
} |
116 |
> |
DIR *fd_dir = opendir("/dev/floppy"); |
117 |
> |
if (fd_dir) { |
118 |
> |
struct dirent *floppy_dev; |
119 |
> |
while ((floppy_dev = readdir(fd_dir)) != NULL) { |
120 |
> |
if (strstr(floppy_dev->d_name, "u1440") != NULL) { |
121 |
> |
char fd_dev[20]; |
122 |
> |
sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name); |
123 |
> |
PrefsAddString("floppy", fd_dev); |
124 |
|
} |
138 |
– |
closedir(fd_dir); |
125 |
|
} |
126 |
+ |
closedir(fd_dir); |
127 |
+ |
} else { |
128 |
+ |
PrefsAddString("floppy", "/dev/fd0"); |
129 |
+ |
PrefsAddString("floppy", "/dev/fd1"); |
130 |
|
} |
131 |
|
#elif defined(__NetBSD__) |
132 |
|
PrefsAddString("floppy", "/dev/fd0a"); |
133 |
|
PrefsAddString("floppy", "/dev/fd1a"); |
134 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
135 |
+ |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
136 |
+ |
extern void DarwinAddFloppyPrefs(void); |
137 |
+ |
|
138 |
+ |
DarwinAddFloppyPrefs(); |
139 |
+ |
#else |
140 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
141 |
+ |
// we just add something safe (a non-existant device): |
142 |
+ |
PrefsAddString("floppy", "/dev/null"); |
143 |
+ |
#endif |
144 |
|
#else |
145 |
|
PrefsAddString("floppy", "/dev/fd0"); |
146 |
|
PrefsAddString("floppy", "/dev/fd1"); |
151 |
|
/* |
152 |
|
* This gets called when no "disk" prefs items are found |
153 |
|
* It scans for available HFS volumes and adds appropriate prefs items |
154 |
+ |
* On OS X, we could do the same, but on an OS X machine I think it is |
155 |
+ |
* very unlikely that any mounted volumes would contain a system which |
156 |
+ |
* is old enough to boot a 68k Mac, so we just do nothing here for now. |
157 |
|
*/ |
158 |
|
|
159 |
|
void SysAddDiskPrefs(void) |
211 |
|
closedir(cd_dir); |
212 |
|
} |
213 |
|
} |
214 |
< |
#elif defined(__FreeBSD__) |
214 |
> |
#elif defined(__APPLE__) && defined(__MACH__) |
215 |
> |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
216 |
> |
extern void DarwinAddCDROMPrefs(void); |
217 |
> |
|
218 |
> |
DarwinAddCDROMPrefs(); |
219 |
> |
#else |
220 |
> |
// Until I can convince the other guys that my Darwin code is useful, |
221 |
> |
// we just do nothing (it is safe to have no cdrom device) |
222 |
> |
#endif |
223 |
> |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
224 |
|
PrefsAddString("cdrom", "/dev/cd0c"); |
213 |
– |
#elif defined(__NetBSD__) |
214 |
– |
PrefsAddString("cdrom", "/dev/cd0d"); |
225 |
|
#endif |
226 |
|
} |
227 |
|
|
246 |
|
#elif defined(__NetBSD__) |
247 |
|
PrefsAddString("seriala", "/dev/tty00"); |
248 |
|
PrefsAddString("serialb", "/dev/tty01"); |
249 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
250 |
+ |
#if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION) |
251 |
+ |
extern void DarwinAddSerialPrefs(void); |
252 |
+ |
|
253 |
+ |
DarwinAddSerialPrefs(); |
254 |
+ |
#else |
255 |
+ |
// Until I can convince the other guys that my Darwin code is useful, |
256 |
+ |
// we just add something safe (non-existant devices): |
257 |
+ |
PrefsAddString("seriala", "/dev/null"); |
258 |
+ |
PrefsAddString("serialb", "/dev/null"); |
259 |
+ |
#endif |
260 |
|
#endif |
261 |
|
} |
262 |
|
|
309 |
|
bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0; |
310 |
|
#endif |
311 |
|
|
312 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
313 |
+ |
// |
314 |
+ |
// There is no set filename in /dev which is the cdrom, |
315 |
+ |
// so we have to see if it is any of the devices that we found earlier |
316 |
+ |
// |
317 |
+ |
const char *cdrom; |
318 |
+ |
int tmp = 0; |
319 |
+ |
|
320 |
+ |
while ( (cdrom = PrefsFindString("cdrom", tmp) ) != NULL ) |
321 |
+ |
{ |
322 |
+ |
if ( strcmp(name, cdrom) == 0 ) |
323 |
+ |
{ |
324 |
+ |
is_cdrom = 1; |
325 |
+ |
read_only = 1; |
326 |
+ |
break; |
327 |
+ |
} |
328 |
+ |
++tmp; |
329 |
+ |
} |
330 |
+ |
#endif |
331 |
+ |
|
332 |
|
D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write")); |
333 |
|
|
334 |
|
// Check if write access is allowed, set read-only flag if not |
372 |
|
if (fh->is_file) { |
373 |
|
// Detect disk image file layout |
374 |
|
loff_t size = 0; |
334 |
– |
#if defined(__linux__) |
335 |
– |
_llseek(fh->fd, 0, 0, &size, SEEK_END); |
336 |
– |
#else |
375 |
|
size = lseek(fd, 0, SEEK_END); |
338 |
– |
#endif |
376 |
|
uint8 data[256]; |
377 |
|
lseek(fd, 0, SEEK_SET); |
378 |
|
read(fd, data, 256); |
407 |
|
fh->is_floppy = ((st.st_rdev >> 16) == 2); |
408 |
|
#endif |
409 |
|
} |
410 |
+ |
#if defined(__APPLE__) && defined(__MACH__) |
411 |
+ |
|
412 |
+ |
// In OS X, the device name is OK for sending ioctls to, |
413 |
+ |
// but not for reading raw CDROM data from. |
414 |
+ |
// (it seems to have extra data padded in) |
415 |
+ |
// |
416 |
+ |
// So, we keep the already opened fiole handle, |
417 |
+ |
// and open a slightly different file for CDROM data |
418 |
+ |
// |
419 |
+ |
if ( is_cdrom ) |
420 |
+ |
{ |
421 |
+ |
fh->ioctl_name = fh->name; |
422 |
+ |
fh->ioctl_fd = fh->fd; |
423 |
+ |
|
424 |
+ |
fh->name = (char *) malloc(strlen(name) + 2); |
425 |
+ |
if ( fh->name ) |
426 |
+ |
{ |
427 |
+ |
*fh->name = '\0'; |
428 |
+ |
strcat(fh->name, name); |
429 |
+ |
strcat(fh->name, "s1"); |
430 |
+ |
fh->fd = open(fh->name, (read_only ? O_RDONLY : O_RDWR)); |
431 |
+ |
if ( fh->fd < 0 ) { |
432 |
+ |
printf("WARNING: Cannot open %s (%s)\n", |
433 |
+ |
fh->name, strerror(errno)); |
434 |
+ |
return NULL; |
435 |
+ |
} |
436 |
+ |
} |
437 |
+ |
} |
438 |
+ |
#endif |
439 |
|
} |
440 |
|
} |
441 |
|
if (fh->is_floppy && first_floppy == NULL) |
477 |
|
return 0; |
478 |
|
|
479 |
|
// Seek to position |
414 |
– |
#if defined(__linux__) |
415 |
– |
loff_t pos = offset + fh->start_byte, res; |
416 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
417 |
– |
return 0; |
418 |
– |
#else |
480 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
481 |
|
return 0; |
421 |
– |
#endif |
482 |
|
|
483 |
|
// Read data |
484 |
|
return read(fh->fd, buffer, length); |
497 |
|
return 0; |
498 |
|
|
499 |
|
// Seek to position |
440 |
– |
#if defined(__linux__) |
441 |
– |
loff_t pos = offset + fh->start_byte, res; |
442 |
– |
if (_llseek(fh->fd, pos >> 32, pos, &res, SEEK_SET) < 0) |
443 |
– |
return 0; |
444 |
– |
#else |
500 |
|
if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0) |
501 |
|
return 0; |
447 |
– |
#endif |
502 |
|
|
503 |
|
// Write data |
504 |
|
return write(fh->fd, buffer, length); |
561 |
|
close(fh->fd); // Close and reopen so the driver will see the media change |
562 |
|
fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK); |
563 |
|
} |
564 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) |
565 |
+ |
if ( fh->is_cdrom ) { |
566 |
+ |
|
567 |
+ |
// Stolen from IOKit/storage/IOMediaBSDClient.h |
568 |
+ |
#define DKIOCEJECT _IO('d', 21) |
569 |
+ |
|
570 |
+ |
close(fh->fd); |
571 |
+ |
if ( ioctl(fh->ioctl_fd, DKIOCEJECT) < 0 ) |
572 |
+ |
{ |
573 |
+ |
printf("ioctl(DKIOCEJECT) failed on file %s: %s\n", |
574 |
+ |
fh->ioctl_name, strerror(errno)); |
575 |
+ |
|
576 |
+ |
// If we are running OSX, the device may be is busy |
577 |
+ |
// due to the Finder having the disk mounted and open, |
578 |
+ |
// so we have to use another method. |
579 |
+ |
|
580 |
+ |
// The only problem is that this takes about 5 seconds: |
581 |
+ |
|
582 |
+ |
char *cmd = (char *) malloc(30+sizeof(fh->name)); |
583 |
+ |
|
584 |
+ |
if ( ! cmd ) |
585 |
+ |
return; |
586 |
+ |
close(fh->ioctl_fd); |
587 |
+ |
sprintf(cmd, "diskutil eject %s 2>&1 >/dev/null", fh->name); |
588 |
+ |
system(cmd); |
589 |
+ |
} |
590 |
+ |
} |
591 |
|
#endif |
592 |
|
} |
593 |
|
|
794 |
|
*toc++ = toc_size >> 8; |
795 |
|
*toc++ = toc_size & 0xff; |
796 |
|
return true; |
797 |
+ |
#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_2) && defined(HAVE_FRAMEWORK_COREFOUNDATION) |
798 |
+ |
extern bool DarwinCDReadTOC(char *name, uint8 *toc); |
799 |
+ |
|
800 |
+ |
return DarwinCDReadTOC(fh->name, toc); |
801 |
|
#elif defined(__FreeBSD__) |
802 |
|
uint8 *p = toc + 2; |
803 |
|
|