1 |
/* |
2 |
* $Id: sys_darwin.cpp,v 1.2 2004/01/12 15:29:24 cebix Exp $ |
3 |
* |
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 |
* Basilisk II (C) 1997-2004 Christian Bauer |
11 |
* |
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(kIOMediaEjectableKey), 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, |
94 |
CFSTR(kIOBSDNameKey), |
95 |
kCFAllocatorDefault, 0); |
96 |
*bsdPath = '\0'; |
97 |
if ( bsdPathAsCFString ) |
98 |
{ |
99 |
size_t devPathLength; |
100 |
|
101 |
strcpy(bsdPath, "/dev/"); |
102 |
devPathLength = strlen(bsdPath); |
103 |
|
104 |
if ( CFStringGetCString((const __CFString *)bsdPathAsCFString, |
105 |
bsdPath + devPathLength, |
106 |
MAXPATHLEN - devPathLength, |
107 |
kCFStringEncodingASCII) ) |
108 |
{ |
109 |
// If we try to do raw reads on the file bsdPath (e.g. /dev/disk5), |
110 |
// we get a lot of extra padding in the data. For some reason, |
111 |
// the device we need has a different name (e.g. /dev/disk5s1) |
112 |
//strcat(bsdPath, "s1"); |
113 |
D(bug("CDROM BSD path: %s\n", bsdPath)); |
114 |
} |
115 |
else |
116 |
D(bug("Could not get BSD device path for CD\n")); |
117 |
|
118 |
CFRelease(bsdPathAsCFString); |
119 |
} |
120 |
|
121 |
PrefsAddString("cdrom", bsdPath); |
122 |
} |
123 |
|
124 |
IOObjectRelease(nextCD); |
125 |
IOObjectRelease(allCDs); |
126 |
} |
127 |
|
128 |
|
129 |
#ifdef MAC_OS_X_VERSION_10_2 |
130 |
/* |
131 |
* Read CD-ROM TOC (binary MSF format, 804 bytes max.) |
132 |
*/ |
133 |
|
134 |
bool DarwinCDReadTOC(char *name, uint8 *toc) |
135 |
{ |
136 |
char *c, *devname; |
137 |
int fd; |
138 |
|
139 |
|
140 |
// The open filehandle is something like /dev/disk5s1 |
141 |
// The DKIOCCDREADTOC ioctl needs the original cd file, |
142 |
// so we strip the s1 suffix off it, and open the file just for this ioctl |
143 |
|
144 |
devname = strdup(name); |
145 |
if ( ! devname ) |
146 |
return false; |
147 |
|
148 |
for ( c = devname; *c; ++c ) ; // Go to the end of the name, |
149 |
--c, --c; // point to the 's1' on the end, |
150 |
*c = '\0'; // and truncate the string |
151 |
|
152 |
fd = open(devname, O_RDONLY); |
153 |
if ( ! fd ) |
154 |
{ |
155 |
printf("Failed to open CD device %s for ioctl\n", devname); |
156 |
free(devname); |
157 |
return false; |
158 |
} |
159 |
|
160 |
D(bug("Opened %s for ioctl()\n", devname)); |
161 |
|
162 |
dk_cd_read_toc_t TOCrequest; |
163 |
|
164 |
// Setup the ioctl request structure: |
165 |
|
166 |
memset(&TOCrequest, 0, sizeof(TOCrequest)); |
167 |
TOCrequest.buffer = toc; |
168 |
TOCrequest.bufferLength = 804; |
169 |
TOCrequest.formatAsTime = kCDTrackInfoAddressTypeTrackNumber; |
170 |
|
171 |
if ( ioctl(fd, DKIOCCDREADTOC, &TOCrequest) < 0 ) |
172 |
{ |
173 |
printf("ioctl(DKIOCCDREADTOC) failed: %s\n", strerror(errno)); |
174 |
close(fd); |
175 |
free(devname); |
176 |
return false; |
177 |
} |
178 |
if ( TOCrequest.bufferLength < sizeof(CDTOC) ) |
179 |
{ |
180 |
printf("ioctl(DKIOCCDREADTOC): only read %d bytes (a CDTOC is at least %d)\n", |
181 |
TOCrequest.bufferLength, (int)sizeof(CDTOC)); |
182 |
close(fd); |
183 |
free(devname); |
184 |
return false; |
185 |
} |
186 |
D(bug("ioctl(DKIOCCDREADTOC) read %d bytes\n", TOCrequest.bufferLength)); |
187 |
|
188 |
close(fd); |
189 |
free(devname); |
190 |
return true; |
191 |
} |
192 |
#endif |