ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sys_unix.cpp
Revision: 1.31
Committed: 2010-02-21T09:58:00Z (14 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.30: +2 -1 lines
Log Message:
fixed missing INT_MAX

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * sys_unix.cpp - System dependent routines, Unix implementation
3     *
4 cebix 1.31 * Basilisk II (C) 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     #include "sysdeps.h"
22    
23     #include <sys/ioctl.h>
24     #include <sys/stat.h>
25     #include <errno.h>
26    
27 gbeauche 1.23 #ifdef HAVE_AVAILABILITYMACROS_H
28     #include <AvailabilityMacros.h>
29     #endif
30    
31 cebix 1.1 #ifdef __linux__
32 cebix 1.6 #include <sys/mount.h>
33 cebix 1.1 #include <linux/cdrom.h>
34     #include <linux/fd.h>
35     #include <linux/major.h>
36     #include <linux/kdev_t.h>
37 cebix 1.16 #include <dirent.h>
38 cebix 1.31 #include <limits.h>
39 cebix 1.1 #endif
40    
41 cebix 1.3 #if defined(__FreeBSD__) || defined(__NetBSD__)
42 cebix 1.1 #include <sys/cdio.h>
43     #endif
44    
45 gbeauche 1.29 #if defined __APPLE__ && defined __MACH__
46     #include <sys/disk.h>
47     #if (defined AQUA || defined HAVE_FRAMEWORK_COREFOUNDATION)
48     #ifndef __MACOSX__
49     #define __MACOSX__ MAC_OS_X_VERSION_MIN_REQUIRED
50     #endif
51     #endif
52     #endif
53    
54 cebix 1.1 #include "main.h"
55     #include "macos_util.h"
56     #include "prefs.h"
57     #include "user_strings.h"
58     #include "sys.h"
59    
60     #define DEBUG 0
61     #include "debug.h"
62    
63    
64     // File handles are pointers to these structures
65     struct file_handle {
66 cebix 1.28 char *name; // Copy of device/file name
67 cebix 1.1 int fd;
68     bool is_file; // Flag: plain file or /dev/something?
69     bool is_floppy; // Flag: floppy device
70     bool is_cdrom; // Flag: CD-ROM device
71     bool read_only; // Copy of Sys_open() flag
72     loff_t start_byte; // Size of file header (if any)
73     loff_t file_size; // Size of file data (only valid if is_file is true)
74 gbeauche 1.29 bool is_media_present; // Flag: media is inserted and available
75 cebix 1.1
76     #if defined(__linux__)
77     int cdrom_cap; // CD-ROM capability flags (only valid if is_cdrom is true)
78     #elif defined(__FreeBSD__)
79     struct ioc_capability cdrom_cap;
80 nigel 1.19 #elif defined(__APPLE__) && defined(__MACH__)
81     char *ioctl_name; // For CDs on OS X - a device for special ioctls
82     int ioctl_fd;
83 cebix 1.1 #endif
84     };
85    
86 gbeauche 1.29 // Open file handles
87     struct open_file_handle {
88     file_handle *fh;
89     open_file_handle *next;
90     };
91     static open_file_handle *open_file_handles = NULL;
92    
93 cebix 1.1 // File handle of first floppy drive (for SysMountFirstFloppy())
94     static file_handle *first_floppy = NULL;
95    
96 gbeauche 1.29 // Prototypes
97     static void cdrom_close(file_handle *fh);
98     static bool cdrom_open(file_handle *fh, const char *path = NULL);
99    
100 cebix 1.1
101     /*
102     * Initialization
103     */
104    
105     void SysInit(void)
106     {
107 gbeauche 1.29 #if defined __MACOSX__
108     extern void DarwinSysInit(void);
109     DarwinSysInit();
110     #endif
111 cebix 1.1 }
112    
113    
114     /*
115     * Deinitialization
116     */
117    
118     void SysExit(void)
119     {
120 gbeauche 1.29 #if defined __MACOSX__
121     extern void DarwinSysExit(void);
122     DarwinSysExit();
123     #endif
124     }
125    
126    
127     /*
128     * Manage open file handles
129     */
130    
131     static void sys_add_file_handle(file_handle *fh)
132     {
133     open_file_handle *p = new open_file_handle;
134     p->fh = fh;
135     p->next = open_file_handles;
136     open_file_handles = p;
137     }
138    
139     static void sys_remove_file_handle(file_handle *fh)
140     {
141     open_file_handle *p = open_file_handles;
142     open_file_handle *q = NULL;
143    
144     while (p) {
145     if (p->fh == fh) {
146     if (q)
147     q->next = p->next;
148     else
149     open_file_handles = p->next;
150     delete p;
151     break;
152     }
153     q = p;
154     p = p->next;
155     }
156     }
157    
158    
159     /*
160     * Account for media that has just arrived
161     */
162    
163     void SysMediaArrived(const char *path, int type)
164     {
165     // Replace the "cdrom" entry (we are polling, it's unique)
166     if (type == MEDIA_CD && !PrefsFindBool("nocdrom"))
167     PrefsReplaceString("cdrom", path);
168    
169     // Wait for media to be available for reading
170     if (open_file_handles) {
171     const int MAX_WAIT = 5;
172     for (int i = 0; i < MAX_WAIT; i++) {
173     if (access(path, R_OK) == 0)
174     break;
175     switch (errno) {
176     case ENOENT: // Unlikely
177     case EACCES: // MacOS X is mounting the media
178     sleep(1);
179     continue;
180     }
181     printf("WARNING: Cannot access %s (%s)\n", path, strerror(errno));
182     return;
183     }
184     }
185    
186     for (open_file_handle *p = open_file_handles; p != NULL; p = p->next) {
187     file_handle * const fh = p->fh;
188    
189     // Re-open CD-ROM device
190     if (fh->is_cdrom && type == MEDIA_CD) {
191     cdrom_close(fh);
192     if (cdrom_open(fh, path)) {
193     fh->is_media_present = true;
194     MountVolume(fh);
195     }
196     }
197     }
198     }
199    
200    
201     /*
202     * Account for media that has just been removed
203     */
204    
205     void SysMediaRemoved(const char *path, int type)
206     {
207     if ((type & MEDIA_REMOVABLE) != MEDIA_CD)
208     return;
209    
210     for (open_file_handle *p = open_file_handles; p != NULL; p = p->next) {
211     file_handle * const fh = p->fh;
212    
213     // Mark media as not available
214     if (!fh->is_cdrom || !fh->is_media_present)
215     continue;
216     if (fh->name && strcmp(fh->name, path) == 0) {
217     fh->is_media_present = false;
218     break;
219     }
220     #if defined __MACOSX__
221     if (fh->ioctl_name && strcmp(fh->ioctl_name, path) == 0) {
222     fh->is_media_present = false;
223     break;
224     }
225     #endif
226     }
227 cebix 1.1 }
228    
229    
230     /*
231     * Mount first floppy disk
232     */
233    
234     void SysMountFirstFloppy(void)
235     {
236     if (first_floppy)
237     MountVolume(first_floppy);
238     }
239    
240    
241     /*
242     * This gets called when no "floppy" prefs items are found
243     * It scans for available floppy drives and adds appropriate prefs items
244     */
245    
246     void SysAddFloppyPrefs(void)
247     {
248     #if defined(__linux__)
249 cebix 1.27 DIR *fd_dir = opendir("/dev/floppy");
250     if (fd_dir) {
251     struct dirent *floppy_dev;
252     while ((floppy_dev = readdir(fd_dir)) != NULL) {
253     if (strstr(floppy_dev->d_name, "u1440") != NULL) {
254     char fd_dev[20];
255     sprintf(fd_dev, "/dev/floppy/%s", floppy_dev->d_name);
256     PrefsAddString("floppy", fd_dev);
257 cebix 1.16 }
258     }
259 cebix 1.27 closedir(fd_dir);
260     } else {
261     PrefsAddString("floppy", "/dev/fd0");
262     PrefsAddString("floppy", "/dev/fd1");
263 cebix 1.16 }
264 cebix 1.3 #elif defined(__NetBSD__)
265     PrefsAddString("floppy", "/dev/fd0a");
266     PrefsAddString("floppy", "/dev/fd1a");
267 nigel 1.19 #elif defined(__APPLE__) && defined(__MACH__)
268 gbeauche 1.25 #if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION)
269 nigel 1.22 extern void DarwinAddFloppyPrefs(void);
270    
271     DarwinAddFloppyPrefs();
272     #else
273     // Until I can convince the other guys that my Darwin code is useful,
274     // we just add something safe (a non-existant device):
275     PrefsAddString("floppy", "/dev/null");
276     #endif
277 cebix 1.1 #else
278     PrefsAddString("floppy", "/dev/fd0");
279     PrefsAddString("floppy", "/dev/fd1");
280     #endif
281     }
282    
283    
284     /*
285     * This gets called when no "disk" prefs items are found
286     * It scans for available HFS volumes and adds appropriate prefs items
287 nigel 1.19 * On OS X, we could do the same, but on an OS X machine I think it is
288     * very unlikely that any mounted volumes would contain a system which
289     * is old enough to boot a 68k Mac, so we just do nothing here for now.
290 cebix 1.1 */
291    
292     void SysAddDiskPrefs(void)
293     {
294     #ifdef __linux__
295     FILE *f = fopen("/etc/fstab", "r");
296     if (f) {
297     char line[256];
298     while(fgets(line, 255, f)) {
299     // Read line
300     int len = strlen(line);
301 cebix 1.10 if (len == 0 || line[0] == '#')
302 cebix 1.1 continue;
303     line[len-1] = 0;
304    
305     // Parse line
306     char *dev, *mnt_point, *fstype;
307 cebix 1.3 if (sscanf(line, "%as %as %as", &dev, &mnt_point, &fstype) == 3) {
308 cebix 1.1 if (strcmp(fstype, "hfs") == 0)
309     PrefsAddString("disk", dev);
310     }
311 cebix 1.3 free(dev); free(mnt_point); free(fstype);
312 cebix 1.1 }
313     fclose(f);
314     }
315     #endif
316     }
317    
318    
319     /*
320     * This gets called when no "cdrom" prefs items are found
321     * It scans for available CD-ROM drives and adds appropriate prefs items
322     */
323    
324     void SysAddCDROMPrefs(void)
325     {
326     // Don't scan for drives if nocdrom option given
327     if (PrefsFindBool("nocdrom"))
328     return;
329    
330     #if defined(__linux__)
331 gbeauche 1.17 if (access("/dev/.devfsd", F_OK) < 0)
332 cebix 1.16 PrefsAddString("cdrom", "/dev/cdrom");
333     else {
334     DIR *cd_dir = opendir("/dev/cdroms");
335     if (cd_dir) {
336     struct dirent *cdrom_dev;
337     while ((cdrom_dev = readdir(cd_dir)) != NULL) {
338     if (strcmp(cdrom_dev->d_name, ".") != 0 && strcmp(cdrom_dev->d_name, "..") != 0) {
339 gbeauche 1.17 char cd_dev[20];
340     sprintf(cd_dev, "/dev/cdroms/%s", cdrom_dev->d_name);
341 cebix 1.16 PrefsAddString("cdrom", cd_dev);
342     }
343     }
344     closedir(cd_dir);
345     }
346     }
347 gbeauche 1.29 #elif defined __MACOSX__
348     // There is no predefined path for CD-ROMs on MacOS X. Rather, we
349     // define a single fake CD-ROM entry for the emulated MacOS.
350     // XXX this means we handle only CD-ROM drive at a time, wherever
351     // the disk is, the latest one is used.
352     PrefsAddString("cdrom", "/dev/poll/cdrom");
353 cebix 1.18 #elif defined(__FreeBSD__) || defined(__NetBSD__)
354 cebix 1.1 PrefsAddString("cdrom", "/dev/cd0c");
355     #endif
356     }
357    
358    
359     /*
360     * Add default serial prefs (must be added, even if no ports present)
361     */
362    
363     void SysAddSerialPrefs(void)
364     {
365     #if defined(__linux__)
366 gbeauche 1.17 if (access("/dev/.devfsd", F_OK) < 0) {
367 cebix 1.16 PrefsAddString("seriala", "/dev/ttyS0");
368     PrefsAddString("serialb", "/dev/ttyS1");
369     } else {
370     PrefsAddString("seriala", "/dev/tts/0");
371     PrefsAddString("serialb", "/dev/tts/1");
372     }
373 cebix 1.1 #elif defined(__FreeBSD__)
374     PrefsAddString("seriala", "/dev/cuaa0");
375     PrefsAddString("serialb", "/dev/cuaa1");
376 cebix 1.3 #elif defined(__NetBSD__)
377     PrefsAddString("seriala", "/dev/tty00");
378     PrefsAddString("serialb", "/dev/tty01");
379 nigel 1.19 #elif defined(__APPLE__) && defined(__MACH__)
380 gbeauche 1.25 #if defined(AQUA) || defined(HAVE_FRAMEWORK_COREFOUNDATION)
381 nigel 1.22 extern void DarwinAddSerialPrefs(void);
382    
383     DarwinAddSerialPrefs();
384     #else
385     // Until I can convince the other guys that my Darwin code is useful,
386     // we just add something safe (non-existant devices):
387     PrefsAddString("seriala", "/dev/null");
388     PrefsAddString("serialb", "/dev/null");
389     #endif
390 cebix 1.1 #endif
391     }
392    
393    
394     /*
395 gbeauche 1.29 * Open CD-ROM device and initialize internal data
396     */
397    
398     static bool cdrom_open_1(file_handle *fh)
399     {
400     #if defined __MACOSX__
401     // In OS X, the device name is OK for sending ioctls to,
402     // but not for reading raw CDROM data from.
403     // (it seems to have extra data padded in)
404     //
405     // So, we keep the already opened file handle,
406     // and open a slightly different file for CDROM data
407     //
408     fh->ioctl_fd = fh->fd;
409     fh->ioctl_name = fh->name;
410     fh->fd = -1;
411     fh->name = (char *)malloc(strlen(fh->ioctl_name) + 3);
412     if (fh->name) {
413     strcpy(fh->name, fh->ioctl_name);
414     strcat(fh->name, "s1");
415     fh->fd = open(fh->name, O_RDONLY, O_NONBLOCK);
416     }
417     if (fh->ioctl_fd < 0)
418     return false;
419     #endif
420     return true;
421     }
422    
423     bool cdrom_open(file_handle *fh, const char *path)
424     {
425     if (path)
426     fh->name = strdup(path);
427     fh->fd = open(fh->name, O_RDONLY, O_NONBLOCK);
428     fh->start_byte = 0;
429     if (!cdrom_open_1(fh))
430     return false;
431     return fh->fd >= 0;
432     }
433    
434    
435     /*
436     * Close a CD-ROM device
437     */
438    
439     void cdrom_close(file_handle *fh)
440     {
441     if (fh->fd >= 0) {
442     close(fh->fd);
443     fh->fd = -1;
444     }
445     if (fh->name) {
446     free(fh->name);
447     fh->name = NULL;
448     }
449     #if defined __MACOSX__
450     if (fh->ioctl_fd >= 0) {
451     close(fh->ioctl_fd);
452     fh->ioctl_fd = -1;
453     }
454     if (fh->ioctl_name) {
455     free(fh->ioctl_name);
456     fh->ioctl_name = NULL;
457     }
458     #endif
459     }
460    
461    
462     /*
463 cebix 1.1 * Check if device is a mounted HFS volume, get mount name
464     */
465    
466     static bool is_drive_mounted(const char *dev_name, char *mount_name)
467     {
468     #ifdef __linux__
469     FILE *f = fopen("/proc/mounts", "r");
470     if (f) {
471     char line[256];
472     while(fgets(line, 255, f)) {
473     // Read line
474     int len = strlen(line);
475     if (len == 0)
476     continue;
477     line[len-1] = 0;
478    
479     // Parse line
480     if (strncmp(line, dev_name, strlen(dev_name)) == 0) {
481     mount_name[0] = 0;
482 cebix 1.3 char *dummy;
483     sscanf(line, "%as %s", &dummy, mount_name);
484     free(dummy);
485 cebix 1.1 fclose(f);
486     return true;
487     }
488     }
489     fclose(f);
490     }
491     #endif
492     return false;
493     }
494    
495    
496     /*
497     * Open file/device, create new file handle (returns NULL on error)
498     */
499    
500     void *Sys_open(const char *name, bool read_only)
501     {
502     bool is_file = strncmp(name, "/dev/", 5) != 0;
503 cebix 1.3 #if defined(__FreeBSD__)
504 cebix 1.1 // SCSI IDE
505     bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0 || strncmp(name, "/dev/acd", 8) == 0;
506     #else
507     bool is_cdrom = strncmp(name, "/dev/cd", 7) == 0;
508     #endif
509 cebix 1.28 bool is_floppy = strncmp(name, "/dev/fd", 7) == 0;
510 cebix 1.1
511 gbeauche 1.29 bool is_polled_media = strncmp(name, "/dev/poll/", 10) == 0;
512     if (is_floppy) // Floppy open fails if there's no disk inserted
513     is_polled_media = true;
514    
515     #if defined __MACOSX__
516 nigel 1.19 // There is no set filename in /dev which is the cdrom,
517     // so we have to see if it is any of the devices that we found earlier
518     {
519 gbeauche 1.29 int index = 0;
520     const char *str;
521     while ((str = PrefsFindString("cdrom", index++)) != NULL) {
522     if (is_polled_media || strcmp(str, name) == 0) {
523     is_cdrom = true;
524     read_only = true;
525     break;
526     }
527 nigel 1.19 }
528     }
529     #endif
530    
531 cebix 1.1 D(bug("Sys_open(%s, %s)\n", name, read_only ? "read-only" : "read/write"));
532    
533     // Check if write access is allowed, set read-only flag if not
534     if (!read_only && access(name, W_OK))
535     read_only = true;
536    
537     // Print warning message and eventually unmount drive when this is an HFS volume mounted under Linux (double mounting will corrupt the volume)
538     char mount_name[256];
539     if (!is_file && !read_only && is_drive_mounted(name, mount_name)) {
540     char str[512];
541     sprintf(str, GetString(STR_VOLUME_IS_MOUNTED_WARN), mount_name);
542     WarningAlert(str);
543     sprintf(str, "umount %s", mount_name);
544     if (system(str)) {
545     sprintf(str, GetString(STR_CANNOT_UNMOUNT_WARN), mount_name, strerror(errno));
546     WarningAlert(str);
547     return NULL;
548     }
549     }
550    
551     // Open file/device
552 gbeauche 1.29 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__MACOSX__)
553 cebix 1.1 int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0));
554     #else
555     int fd = open(name, read_only ? O_RDONLY : O_RDWR);
556     #endif
557     if (fd < 0 && !read_only) {
558     // Read-write failed, try read-only
559     read_only = true;
560     fd = open(name, O_RDONLY);
561     }
562 gbeauche 1.29 if (fd >= 0 || is_polled_media) {
563 cebix 1.1 file_handle *fh = new file_handle;
564     fh->name = strdup(name);
565     fh->fd = fd;
566     fh->is_file = is_file;
567     fh->read_only = read_only;
568     fh->start_byte = 0;
569 cebix 1.28 fh->is_floppy = is_floppy;
570     fh->is_cdrom = is_cdrom;
571 gbeauche 1.29 fh->is_media_present = false;
572     #if defined __MACOSX__
573     fh->ioctl_fd = -1;
574     fh->ioctl_name = NULL;
575     #endif
576 cebix 1.1 if (fh->is_file) {
577 gbeauche 1.29 fh->is_media_present = true;
578 cebix 1.1 // Detect disk image file layout
579     loff_t size = 0;
580     size = lseek(fd, 0, SEEK_END);
581     uint8 data[256];
582     lseek(fd, 0, SEEK_SET);
583     read(fd, data, 256);
584     FileDiskLayout(size, data, fh->start_byte, fh->file_size);
585     } else {
586     struct stat st;
587     if (fstat(fd, &st) == 0) {
588 gbeauche 1.29 fh->is_media_present = true;
589 cebix 1.1 if (S_ISBLK(st.st_mode)) {
590     fh->is_cdrom = is_cdrom;
591     #if defined(__linux__)
592     fh->is_floppy = (MAJOR(st.st_rdev) == FLOPPY_MAJOR);
593     #ifdef CDROM_GET_CAPABILITY
594     if (is_cdrom) {
595     fh->cdrom_cap = ioctl(fh->fd, CDROM_GET_CAPABILITY);
596     if (fh->cdrom_cap < 0)
597     fh->cdrom_cap = 0;
598     }
599     #else
600     fh->cdrom_cap = 0;
601     #endif
602 cebix 1.4 #elif defined(__FreeBSD__)
603 cebix 1.1 fh->is_floppy = ((st.st_rdev >> 16) == 2);
604     #ifdef CDIOCCAPABILITY
605     if (is_cdrom) {
606     if (ioctl(fh->fd, CDIOCCAPABILITY, &fh->cdrom_cap) < 0)
607     memset(&fh->cdrom_cap, 0, sizeof(fh->cdrom_cap));
608     }
609     #else
610     fh->cdrom_cap = 0;
611     #endif
612 cebix 1.4 #elif defined(__NetBSD__)
613     fh->is_floppy = ((st.st_rdev >> 16) == 2);
614 cebix 1.1 #endif
615     }
616 gbeauche 1.29 #if defined __MACOSX__
617     if (is_cdrom) {
618     fh->is_cdrom = true;
619     fh->is_floppy = false;
620     if (cdrom_open_1(fh))
621     fh->is_media_present = true;
622 nigel 1.19 }
623     #endif
624 cebix 1.1 }
625     }
626     if (fh->is_floppy && first_floppy == NULL)
627     first_floppy = fh;
628 gbeauche 1.29 sys_add_file_handle(fh);
629 cebix 1.1 return fh;
630     } else {
631     printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno));
632     return NULL;
633     }
634     }
635    
636    
637     /*
638     * Close file/device, delete file handle
639     */
640    
641     void Sys_close(void *arg)
642     {
643     file_handle *fh = (file_handle *)arg;
644     if (!fh)
645     return;
646    
647 gbeauche 1.29 sys_remove_file_handle(fh);
648    
649     if (fh->is_cdrom)
650     cdrom_close(fh);
651 cebix 1.28 if (fh->fd >= 0)
652     close(fh->fd);
653 cebix 1.1 if (fh->name)
654     free(fh->name);
655     delete fh;
656     }
657    
658    
659     /*
660     * Read "length" bytes from file/device, starting at "offset", to "buffer",
661     * returns number of bytes read (or 0)
662     */
663    
664     size_t Sys_read(void *arg, void *buffer, loff_t offset, size_t length)
665     {
666     file_handle *fh = (file_handle *)arg;
667     if (!fh)
668     return 0;
669    
670     // Seek to position
671     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
672     return 0;
673    
674     // Read data
675     return read(fh->fd, buffer, length);
676     }
677    
678    
679     /*
680     * Write "length" bytes from "buffer" to file/device, starting at "offset",
681     * returns number of bytes written (or 0)
682     */
683    
684     size_t Sys_write(void *arg, void *buffer, loff_t offset, size_t length)
685     {
686     file_handle *fh = (file_handle *)arg;
687     if (!fh)
688     return 0;
689    
690     // Seek to position
691     if (lseek(fh->fd, offset + fh->start_byte, SEEK_SET) < 0)
692     return 0;
693    
694     // Write data
695     return write(fh->fd, buffer, length);
696     }
697    
698    
699     /*
700     * Return size of file/device (minus header)
701     */
702    
703     loff_t SysGetFileSize(void *arg)
704     {
705     file_handle *fh = (file_handle *)arg;
706     if (!fh)
707     return true;
708    
709     if (fh->is_file)
710     return fh->file_size;
711     else {
712 cebix 1.3 #if defined(__linux__)
713 cebix 1.5 long blocks;
714     if (ioctl(fh->fd, BLKGETSIZE, &blocks) < 0)
715     return 0;
716     D(bug(" BLKGETSIZE returns %d blocks\n", blocks));
717     return (loff_t)blocks * 512;
718 gbeauche 1.29 #elif defined __MACOSX__
719     uint32 block_size;
720     if (ioctl(fh->ioctl_fd, DKIOCGETBLOCKSIZE, &block_size) < 0)
721     return 0;
722     D(bug(" DKIOCGETBLOCKSIZE returns %lu bytes\n", (unsigned long)block_size));
723     uint64 block_count;
724     if (ioctl(fh->ioctl_fd, DKIOCGETBLOCKCOUNT, &block_count) < 0)
725     return 0;
726     D(bug(" DKIOCGETBLOCKCOUNT returns %llu blocks\n", (unsigned long long)block_count));
727     return block_count * block_size;
728 cebix 1.1 #else
729     return lseek(fh->fd, 0, SEEK_END) - fh->start_byte;
730     #endif
731     }
732     }
733    
734    
735     /*
736     * Eject volume (if applicable)
737     */
738    
739     void SysEject(void *arg)
740     {
741     file_handle *fh = (file_handle *)arg;
742     if (!fh)
743     return;
744    
745     #if defined(__linux__)
746     if (fh->is_floppy) {
747 cebix 1.28 if (fh->fd >= 0) {
748     fsync(fh->fd);
749     ioctl(fh->fd, FDFLUSH);
750     ioctl(fh->fd, FDEJECT);
751     close(fh->fd); // Close and reopen so the driver will see the media change
752     }
753 cebix 1.14 fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
754 cebix 1.1 } else if (fh->is_cdrom) {
755     ioctl(fh->fd, CDROMEJECT);
756     close(fh->fd); // Close and reopen so the driver will see the media change
757     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
758     }
759 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
760 cebix 1.1 if (fh->is_floppy) {
761     fsync(fh->fd);
762     } else if (fh->is_cdrom) {
763     ioctl(fh->fd, CDIOCEJECT);
764     close(fh->fd); // Close and reopen so the driver will see the media change
765     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
766     }
767 nigel 1.19 #elif defined(__APPLE__) && defined(__MACH__)
768 gbeauche 1.29 if (fh->is_cdrom && fh->is_media_present) {
769 nigel 1.19 close(fh->fd);
770 gbeauche 1.29 fh->fd = -1;
771     if (ioctl(fh->ioctl_fd, DKIOCEJECT) < 0) {
772     D(bug(" DKIOCEJECT failed on file %s: %s\n",
773     fh->ioctl_name, strerror(errno)));
774 nigel 1.19
775 gbeauche 1.29 // If we are running MacOS X, the device may be in busy
776     // state because the Finder has mounted the disk
777     close(fh->ioctl_fd);
778     fh->ioctl_fd = -1;
779 nigel 1.19
780 gbeauche 1.29 // Try to use "diskutil eject" but it can take up to 5
781     // seconds to complete
782     static const char eject_cmd[] = "/usr/sbin/diskutil eject %s 2>&1 >/dev/null";
783     char *cmd = (char *)alloca(strlen(eject_cmd) + strlen(fh->ioctl_name) + 1);
784     sprintf(cmd, eject_cmd, fh->ioctl_name);
785 nigel 1.19 system(cmd);
786     }
787 gbeauche 1.29 fh->is_media_present = false;
788 nigel 1.19 }
789 cebix 1.1 #endif
790     }
791    
792    
793     /*
794     * Format volume (if applicable)
795     */
796    
797     bool SysFormat(void *arg)
798     {
799     file_handle *fh = (file_handle *)arg;
800     if (!fh)
801     return false;
802    
803     //!!
804     return true;
805     }
806    
807    
808     /*
809     * Check if file/device is read-only (this includes the read-only flag on Sys_open())
810     */
811    
812     bool SysIsReadOnly(void *arg)
813     {
814     file_handle *fh = (file_handle *)arg;
815     if (!fh)
816     return true;
817    
818 cebix 1.3 #if defined(__linux__)
819 cebix 1.1 if (fh->is_floppy) {
820 cebix 1.28 if (fh->fd >= 0) {
821     struct floppy_drive_struct stat;
822     ioctl(fh->fd, FDGETDRVSTAT, &stat);
823     return !(stat.flags & FD_DISK_WRITABLE);
824     } else
825     return true;
826 cebix 1.1 } else
827     #endif
828     return fh->read_only;
829     }
830    
831    
832     /*
833     * Check if the given file handle refers to a fixed or a removable disk
834     */
835    
836     bool SysIsFixedDisk(void *arg)
837     {
838     file_handle *fh = (file_handle *)arg;
839     if (!fh)
840     return true;
841    
842     if (fh->is_file)
843     return true;
844     else if (fh->is_floppy || fh->is_cdrom)
845     return false;
846     else
847     return true;
848     }
849    
850    
851     /*
852     * Check if a disk is inserted in the drive (always true for files)
853     */
854    
855     bool SysIsDiskInserted(void *arg)
856     {
857     file_handle *fh = (file_handle *)arg;
858     if (!fh)
859     return false;
860    
861     if (fh->is_file) {
862     return true;
863    
864     #if defined(__linux__)
865     } else if (fh->is_floppy) {
866     char block[512];
867     lseek(fh->fd, 0, SEEK_SET);
868 cebix 1.14 ssize_t actual = read(fh->fd, block, 512);
869     if (actual < 0) {
870     close(fh->fd); // Close and reopen so the driver will see the media change
871     fh->fd = open(fh->name, fh->read_only ? O_RDONLY : O_RDWR);
872     actual = read(fh->fd, block, 512);
873     }
874     return actual == 512;
875 cebix 1.1 } else if (fh->is_cdrom) {
876 cebix 1.8 #ifdef CDROM_MEDIA_CHANGED
877     if (fh->cdrom_cap & CDC_MEDIA_CHANGED) {
878     // If we don't do this, all attempts to read from a disc fail
879 cebix 1.14 // once the tray has been opened (altough the TOC reads fine).
880 cebix 1.8 // Can somebody explain this to me?
881     if (ioctl(fh->fd, CDROM_MEDIA_CHANGED) == 1) {
882     close(fh->fd);
883     fh->fd = open(fh->name, O_RDONLY | O_NONBLOCK);
884     }
885     }
886     #endif
887 cebix 1.1 #ifdef CDROM_DRIVE_STATUS
888     if (fh->cdrom_cap & CDC_DRIVE_STATUS) {
889     return ioctl(fh->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
890     }
891     #endif
892     cdrom_tochdr header;
893     return ioctl(fh->fd, CDROMREADTOCHDR, &header) == 0;
894 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
895 cebix 1.1 } else if (fh->is_floppy) {
896     return false; //!!
897     } else if (fh->is_cdrom) {
898     struct ioc_toc_header header;
899     return ioctl(fh->fd, CDIOREADTOCHEADER, &header) == 0;
900 gbeauche 1.29 #elif defined __MACOSX__
901     } else if (fh->is_cdrom || fh->is_floppy) {
902     return fh->is_media_present;
903 cebix 1.1 #endif
904    
905     } else
906     return true;
907     }
908    
909    
910     /*
911     * Prevent medium removal (if applicable)
912     */
913    
914     void SysPreventRemoval(void *arg)
915     {
916     file_handle *fh = (file_handle *)arg;
917     if (!fh)
918     return;
919    
920     #if defined(__linux__) && defined(CDROM_LOCKDOOR)
921     if (fh->is_cdrom)
922     ioctl(fh->fd, CDROM_LOCKDOOR, 1);
923     #endif
924     }
925    
926    
927     /*
928     * Allow medium removal (if applicable)
929     */
930    
931     void SysAllowRemoval(void *arg)
932     {
933     file_handle *fh = (file_handle *)arg;
934     if (!fh)
935     return;
936    
937 cebix 1.2 #if defined(__linux__) && defined(CDROM_LOCKDOOR)
938 cebix 1.1 if (fh->is_cdrom)
939     ioctl(fh->fd, CDROM_LOCKDOOR, 0);
940     #endif
941     }
942    
943    
944     /*
945     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
946     */
947    
948     bool SysCDReadTOC(void *arg, uint8 *toc)
949     {
950     file_handle *fh = (file_handle *)arg;
951     if (!fh)
952     return false;
953    
954     if (fh->is_cdrom) {
955     #if defined(__linux__)
956     uint8 *p = toc + 2;
957    
958     // Header
959     cdrom_tochdr header;
960     if (ioctl(fh->fd, CDROMREADTOCHDR, &header) < 0)
961     return false;
962     *p++ = header.cdth_trk0;
963     *p++ = header.cdth_trk1;
964    
965     // Tracks
966     cdrom_tocentry entry;
967     for (int i=header.cdth_trk0; i<=header.cdth_trk1; i++) {
968     entry.cdte_track = i;
969     entry.cdte_format = CDROM_MSF;
970     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
971     return false;
972     *p++ = 0;
973     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
974     *p++ = entry.cdte_track;
975     *p++ = 0;
976     *p++ = 0;
977     *p++ = entry.cdte_addr.msf.minute;
978     *p++ = entry.cdte_addr.msf.second;
979     *p++ = entry.cdte_addr.msf.frame;
980     }
981    
982     // Leadout track
983     entry.cdte_track = CDROM_LEADOUT;
984     entry.cdte_format = CDROM_MSF;
985     if (ioctl(fh->fd, CDROMREADTOCENTRY, &entry) < 0)
986     return false;
987     *p++ = 0;
988     *p++ = (entry.cdte_adr << 4) | entry.cdte_ctrl;
989     *p++ = entry.cdte_track;
990     *p++ = 0;
991     *p++ = 0;
992     *p++ = entry.cdte_addr.msf.minute;
993     *p++ = entry.cdte_addr.msf.second;
994     *p++ = entry.cdte_addr.msf.frame;
995    
996     // TOC size
997     int toc_size = p - toc;
998     *toc++ = toc_size >> 8;
999     *toc++ = toc_size & 0xff;
1000     return true;
1001 gbeauche 1.29 #elif defined __MACOSX__ && defined MAC_OS_X_VERSION_10_2
1002     if (fh->is_media_present) {
1003     extern bool DarwinCDReadTOC(char *name, uint8 *toc);
1004     return DarwinCDReadTOC(fh->name, toc);
1005     }
1006     return false;
1007 cebix 1.4 #elif defined(__FreeBSD__)
1008 cebix 1.1 uint8 *p = toc + 2;
1009    
1010     // Header
1011     struct ioc_toc_header header;
1012     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
1013     return false;
1014     *p++ = header.starting_track;
1015     *p++ = header.ending_track;
1016    
1017     // Tracks
1018     struct ioc_read_toc_single_entry entry;
1019     for (int i=header.starting_track; i<=header.ending_track; i++) {
1020     entry.track = i;
1021     entry.address_format = CD_MSF_FORMAT;
1022     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
1023     return false;
1024     *p++ = 0;
1025     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
1026     *p++ = entry.entry.track;
1027     *p++ = 0;
1028     *p++ = 0;
1029     *p++ = entry.entry.addr.msf.minute;
1030     *p++ = entry.entry.addr.msf.second;
1031     *p++ = entry.entry.addr.msf.frame;
1032     }
1033    
1034     // Leadout track
1035     entry.track = CD_TRACK_INFO;
1036     entry.address_format = CD_MSF_FORMAT;
1037     if (ioctl(fh->fd, CDIOREADTOCENTRY, &entry) < 0)
1038     return false;
1039     *p++ = 0;
1040     *p++ = (entry.entry.addr_type << 4) | entry.entry.control;
1041     *p++ = entry.entry.track;
1042     *p++ = 0;
1043     *p++ = 0;
1044     *p++ = entry.entry.addr.msf.minute;
1045     *p++ = entry.entry.addr.msf.second;
1046     *p++ = entry.entry.addr.msf.frame;
1047 cebix 1.4
1048     // TOC size
1049     int toc_size = p - toc;
1050     *toc++ = toc_size >> 8;
1051     *toc++ = toc_size & 0xff;
1052     return true;
1053     #elif defined(__NetBSD__)
1054     uint8 *p = toc + 2;
1055    
1056     // Header
1057     struct ioc_toc_header header;
1058     if (ioctl(fh->fd, CDIOREADTOCHEADER, &header) < 0)
1059     return false;
1060     *p++ = header.starting_track;
1061     *p++ = header.ending_track;
1062    
1063     // Tracks (this is nice... :-)
1064     struct ioc_read_toc_entry entries;
1065     entries.address_format = CD_MSF_FORMAT;
1066     entries.starting_track = 1;
1067     entries.data_len = 800;
1068     entries.data = (cd_toc_entry *)p;
1069     if (ioctl(fh->fd, CDIOREADTOCENTRIES, &entries) < 0)
1070     return false;
1071 cebix 1.1
1072     // TOC size
1073     int toc_size = p - toc;
1074     *toc++ = toc_size >> 8;
1075     *toc++ = toc_size & 0xff;
1076     return true;
1077 cebix 1.15 #else
1078     return false;
1079 cebix 1.1 #endif
1080     } else
1081     return false;
1082     }
1083    
1084    
1085     /*
1086     * Read CD-ROM position data (Sub-Q Channel, 16 bytes, see SCSI standard)
1087     */
1088    
1089     bool SysCDGetPosition(void *arg, uint8 *pos)
1090     {
1091     file_handle *fh = (file_handle *)arg;
1092     if (!fh)
1093     return false;
1094    
1095     if (fh->is_cdrom) {
1096     #if defined(__linux__)
1097     cdrom_subchnl chan;
1098     chan.cdsc_format = CDROM_MSF;
1099     if (ioctl(fh->fd, CDROMSUBCHNL, &chan) < 0)
1100     return false;
1101     *pos++ = 0;
1102     *pos++ = chan.cdsc_audiostatus;
1103     *pos++ = 0;
1104     *pos++ = 12; // Sub-Q data length
1105     *pos++ = 0;
1106     *pos++ = (chan.cdsc_adr << 4) | chan.cdsc_ctrl;
1107     *pos++ = chan.cdsc_trk;
1108     *pos++ = chan.cdsc_ind;
1109     *pos++ = 0;
1110     *pos++ = chan.cdsc_absaddr.msf.minute;
1111     *pos++ = chan.cdsc_absaddr.msf.second;
1112     *pos++ = chan.cdsc_absaddr.msf.frame;
1113     *pos++ = 0;
1114     *pos++ = chan.cdsc_reladdr.msf.minute;
1115     *pos++ = chan.cdsc_reladdr.msf.second;
1116     *pos++ = chan.cdsc_reladdr.msf.frame;
1117     return true;
1118 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1119 cebix 1.1 struct ioc_read_subchannel chan;
1120     chan.data_format = CD_MSF_FORMAT;
1121     chan.address_format = CD_MSF_FORMAT;
1122     chan.track = CD_CURRENT_POSITION;
1123     if (ioctl(fh->fd, CDIOCREADSUBCHANNEL, &chan) < 0)
1124     return false;
1125     *pos++ = 0;
1126     *pos++ = chan.data->header.audio_status;
1127     *pos++ = 0;
1128     *pos++ = 12; // Sub-Q data length
1129     *pos++ = 0;
1130     *pos++ = (chan.data->what.position.addr_type << 4) | chan.data->what.position.control;
1131     *pos++ = chan.data->what.position.track_number;
1132     *pos++ = chan.data->what.position.index_number;
1133     *pos++ = 0;
1134     *pos++ = chan.data->what.position.absaddr.msf.minute;
1135     *pos++ = chan.data->what.position.absaddr.msf.second;
1136     *pos++ = chan.data->what.position.absaddr.msf.frame;
1137     *pos++ = 0;
1138     *pos++ = chan.data->what.position.reladdr.msf.minute;
1139     *pos++ = chan.data->what.position.reladdr.msf.second;
1140     *pos++ = chan.data->what.position.reladdr.msf.frame;
1141     return true;
1142 cebix 1.15 #else
1143     return false;
1144 cebix 1.1 #endif
1145     } else
1146     return false;
1147     }
1148    
1149    
1150     /*
1151     * Play CD audio
1152     */
1153    
1154     bool SysCDPlay(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, uint8 end_m, uint8 end_s, uint8 end_f)
1155     {
1156     file_handle *fh = (file_handle *)arg;
1157     if (!fh)
1158     return false;
1159    
1160     if (fh->is_cdrom) {
1161     #if defined(__linux__)
1162     cdrom_msf play;
1163     play.cdmsf_min0 = start_m;
1164     play.cdmsf_sec0 = start_s;
1165     play.cdmsf_frame0 = start_f;
1166     play.cdmsf_min1 = end_m;
1167     play.cdmsf_sec1 = end_s;
1168     play.cdmsf_frame1 = end_f;
1169     return ioctl(fh->fd, CDROMPLAYMSF, &play) == 0;
1170 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1171 cebix 1.1 struct ioc_play_msf play;
1172     play.start_m = start_m;
1173     play.start_s = start_s;
1174     play.start_f = start_f;
1175     play.end_m = end_m;
1176     play.end_s = end_s;
1177     play.end_f = end_f;
1178     return ioctl(fh->fd, CDIOCPLAYMSF, &play) == 0;
1179 cebix 1.15 #else
1180     return false;
1181 cebix 1.1 #endif
1182     } else
1183     return false;
1184     }
1185    
1186    
1187     /*
1188     * Pause CD audio
1189     */
1190    
1191     bool SysCDPause(void *arg)
1192     {
1193     file_handle *fh = (file_handle *)arg;
1194     if (!fh)
1195     return false;
1196    
1197     if (fh->is_cdrom) {
1198     #if defined(__linux__)
1199     return ioctl(fh->fd, CDROMPAUSE) == 0;
1200 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1201 cebix 1.1 return ioctl(fh->fd, CDIOCPAUSE) == 0;
1202 cebix 1.15 #else
1203     return false;
1204 cebix 1.1 #endif
1205     } else
1206     return false;
1207     }
1208    
1209    
1210     /*
1211     * Resume paused CD audio
1212     */
1213    
1214     bool SysCDResume(void *arg)
1215     {
1216     file_handle *fh = (file_handle *)arg;
1217     if (!fh)
1218     return false;
1219    
1220     if (fh->is_cdrom) {
1221     #if defined(__linux__)
1222     return ioctl(fh->fd, CDROMRESUME) == 0;
1223 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1224 cebix 1.1 return ioctl(fh->fd, CDIOCRESUME) == 0;
1225 cebix 1.15 #else
1226     return false;
1227 cebix 1.1 #endif
1228     } else
1229     return false;
1230     }
1231    
1232    
1233     /*
1234     * Stop CD audio
1235     */
1236    
1237     bool SysCDStop(void *arg, uint8 lead_out_m, uint8 lead_out_s, uint8 lead_out_f)
1238     {
1239     file_handle *fh = (file_handle *)arg;
1240     if (!fh)
1241     return false;
1242    
1243     if (fh->is_cdrom) {
1244     #if defined(__linux__)
1245     return ioctl(fh->fd, CDROMSTOP) == 0;
1246 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1247 cebix 1.1 return ioctl(fh->fd, CDIOCSTOP) == 0;
1248 cebix 1.15 #else
1249     return false;
1250 cebix 1.1 #endif
1251     } else
1252     return false;
1253     }
1254    
1255    
1256     /*
1257     * Perform CD audio fast-forward/fast-reverse operation starting from specified address
1258     */
1259    
1260     bool SysCDScan(void *arg, uint8 start_m, uint8 start_s, uint8 start_f, bool reverse)
1261     {
1262     file_handle *fh = (file_handle *)arg;
1263     if (!fh)
1264     return false;
1265    
1266     // Not supported under Linux
1267     return false;
1268     }
1269    
1270    
1271     /*
1272     * Set CD audio volume (0..255 each channel)
1273     */
1274    
1275     void SysCDSetVolume(void *arg, uint8 left, uint8 right)
1276     {
1277     file_handle *fh = (file_handle *)arg;
1278     if (!fh)
1279     return;
1280    
1281     if (fh->is_cdrom) {
1282     #if defined(__linux__)
1283     cdrom_volctrl vol;
1284     vol.channel0 = vol.channel2 = left;
1285     vol.channel1 = vol.channel3 = right;
1286     ioctl(fh->fd, CDROMVOLCTRL, &vol);
1287 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1288 cebix 1.1 struct ioc_vol vol;
1289     vol.vol[0] = vol.vol[2] = left;
1290     vol.vol[1] = vol.vol[3] = right;
1291     ioctl(fh->fd, CDIOCSETVOL, &vol);
1292     #endif
1293     }
1294     }
1295    
1296    
1297     /*
1298     * Get CD audio volume (0..255 each channel)
1299     */
1300    
1301     void SysCDGetVolume(void *arg, uint8 &left, uint8 &right)
1302     {
1303     file_handle *fh = (file_handle *)arg;
1304     if (!fh)
1305     return;
1306    
1307     left = right = 0;
1308     if (fh->is_cdrom) {
1309     #if defined(__linux__)
1310     cdrom_volctrl vol;
1311     ioctl(fh->fd, CDROMVOLREAD, &vol);
1312     left = vol.channel0;
1313     right = vol.channel1;
1314 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1315 cebix 1.1 struct ioc_vol vol;
1316     ioctl(fh->fd, CDIOCGETVOL, &vol);
1317     left = vol.vol[0];
1318     right = vol.vol[1];
1319     #endif
1320     }
1321     }