ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/sys_darwin.cpp
Revision: 1.2
Committed: 2004-01-12T15:29:24Z (20 years, 10 months ago) by cebix
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Happy New Year! :)

File Contents

# User Rev Content
1 nigel 1.1 /*
2 cebix 1.2 * $Id: sys_darwin.cpp,v 1.1 2003/03/21 07:05:28 nigel Exp $
3 nigel 1.1 *
4     * sys_darwin.cpp - Extra Darwin system dependant routines. Called by:
5     *
6     * sys_unix.cpp - System dependent routines, Unix implementation
7     *
8     * Based on Apple's CDROMSample.c and Evan Jones' cd-discid.c patches
9     *
10 cebix 1.2 * Basilisk II (C) 1997-2004 Christian Bauer
11 nigel 1.1 *
12     * This program is free software; you can redistribute it and/or modify
13     * it under the terms of the GNU General Public License as published by
14     * the Free Software Foundation; either version 2 of the License, or
15     * (at your option) any later version.
16     *
17     * This program is distributed in the hope that it will be useful,
18     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     * GNU General Public License for more details.
21     *
22     * You should have received a copy of the GNU General Public License
23     * along with this program; if not, write to the Free Software
24     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25     */
26    
27     #import <errno.h>
28     #import <sys/param.h>
29     #import <IOKit/IOKitLib.h>
30     #import <IOKit/IOBSD.h>
31     #import <IOKit/storage/IOMedia.h>
32     #import <IOKit/storage/IOMediaBSDClient.h>
33     #import <IOKit/storage/IOCDMedia.h>
34     #import <IOKit/storage/IOCDMediaBSDClient.h>
35     #import <CoreFoundation/CoreFoundation.h>
36    
37     #import "sysdeps.h"
38    
39     #import "prefs.h"
40    
41     #define DEBUG 0
42     #import "debug.h"
43    
44    
45    
46     /*
47     * This gets called when no "cdrom" prefs items are found
48     * It scans for available CD-ROM drives and adds appropriate prefs items
49     */
50    
51     void DarwinAddCDROMPrefs(void)
52     {
53     mach_port_t masterPort; // The way to talk to the kernel
54     io_iterator_t allCDs; // List of CD drives on the system
55     CFMutableDictionaryRef classesToMatch;
56     io_object_t nextCD;
57    
58    
59     // Don't scan for drives if nocdrom option given
60     if ( PrefsFindBool("nocdrom") )
61     return;
62    
63    
64     // Let this task talk to the guts of the kernel:
65     if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS )
66     bug("IOMasterPort failed. Won't be able to do anything with CD drives");
67    
68    
69     // CD media are instances of class kIOCDMediaClass
70     classesToMatch = IOServiceMatching(kIOCDMediaClass);
71     if ( classesToMatch )
72     {
73     // Narrow the search a little further. Each IOMedia object
74     // has a property with key kIOMediaEjectable. We limit
75     // the match only to those CDs that are actually ejectable
76     CFDictionarySetValue(classesToMatch,
77     CFSTR(kIOMediaEjectable), kCFBooleanTrue);
78     }
79    
80     if ( IOServiceGetMatchingServices(masterPort,
81     classesToMatch, &allCDs) != KERN_SUCCESS )
82     {
83     D(bug("IOServiceGetMatchingServices failed. No CD media drives found?\n"));
84     return;
85     }
86    
87    
88     // Iterate through each CD drive
89     while ( nextCD = IOIteratorNext(allCDs))
90     {
91     char bsdPath[MAXPATHLEN];
92     CFTypeRef bsdPathAsCFString =
93     IORegistryEntryCreateCFProperty(nextCD, CFSTR(kIOBSDName),
94     kCFAllocatorDefault, 0);
95     *bsdPath = '\0';
96     if ( bsdPathAsCFString )
97     {
98     size_t devPathLength;
99    
100     strcpy(bsdPath, "/dev/");
101     devPathLength = strlen(bsdPath);
102    
103     if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
104     bsdPath + devPathLength,
105     MAXPATHLEN - devPathLength,
106     kCFStringEncodingASCII) )
107     {
108     // If we try to do raw reads on the file bsdPath (e.g. /dev/disk5),
109     // we get a lot of extra padding in the data. For some reason,
110     // the device we need has a different name (e.g. /dev/disk5s1)
111     //strcat(bsdPath, "s1");
112     D(bug("CDROM BSD path: %s\n", bsdPath));
113     }
114     else
115     D(bug("Could not get BSD device path for CD\n"));
116    
117     CFRelease(bsdPathAsCFString);
118     }
119    
120     PrefsAddString("cdrom", bsdPath);
121     }
122    
123     IOObjectRelease(nextCD);
124     IOObjectRelease(allCDs);
125     }
126    
127    
128     #ifdef MAC_OS_X_VERSION_10_2
129     /*
130     * Read CD-ROM TOC (binary MSF format, 804 bytes max.)
131     */
132    
133     bool DarwinCDReadTOC(char *name, uint8 *toc)
134     {
135     char *c, *devname;
136     int fd;
137    
138    
139     // The open filehandle is something like /dev/disk5s1
140     // The DKIOCCDREADTOC ioctl needs the original cd file,
141     // so we strip the s1 suffix off it, and open the file just for this ioctl
142    
143     devname = strdup(name);
144     if ( ! devname )
145     return false;
146    
147     for ( c = devname; *c; ++c ) ; // Go to the end of the name,
148     --c, --c; // point to the 's1' on the end,
149     *c = '\0'; // and truncate the string
150    
151     fd = open(devname, O_RDONLY);
152     if ( ! fd )
153     {
154     printf("Failed to open CD device %s for ioctl\n", devname);
155     free(devname);
156     return false;
157     }
158    
159     D(bug("Opened %s for ioctl()\n", devname));
160    
161     dk_cd_read_toc_t TOCrequest;
162    
163     // Setup the ioctl request structure:
164    
165     memset(&TOCrequest, 0, sizeof(TOCrequest));
166     TOCrequest.buffer = toc;
167     TOCrequest.bufferLength = 804;
168     TOCrequest.formatAsTime = kCDTrackInfoAddressTypeTrackNumber;
169    
170     if ( ioctl(fd, DKIOCCDREADTOC, &TOCrequest) < 0 )
171     {
172     printf("ioctl(DKIOCCDREADTOC) failed: %s\n", strerror(errno));
173     close(fd);
174     free(devname);
175     return false;
176     }
177     if ( TOCrequest.bufferLength < sizeof(CDTOC) )
178     {
179     printf("ioctl(DKIOCCDREADTOC): only read %d bytes (a CDTOC is at least %d)\n",
180     TOCrequest.bufferLength, (int)sizeof(CDTOC));
181     close(fd);
182     free(devname);
183     return false;
184     }
185     D(bug("ioctl(DKIOCCDREADTOC) read %d bytes\n", TOCrequest.bufferLength));
186    
187     close(fd);
188     free(devname);
189     return true;
190     }
191     #endif