filename | src/gdrom/gdrom.c |
changeset | 1023:264e2fd90be8 |
prev | 837:4eae2ddccf9c |
next | 1074:397d77b6e346 |
author | nkeynes |
date | Mon Jun 08 04:12:21 2009 +0000 (14 years ago) |
permissions | -rw-r--r-- |
last change | General cleanup of the GD-rom subsystem - merge gdrom_image_t and gdrom_disc_t - Abstract MMC devices using a lower-level scsi transport - OSX: only look at the whole disc device, and ignore partitions |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * GD-Rom access functions.
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <glib/gutils.h>
24 #include "gdrom/ide.h"
25 #include "gdrom/gdrom.h"
26 #include "gdrom/gddriver.h"
27 #include "gdrom/packet.h"
28 #include "dream.h"
29 #include "bootstrap.h"
31 extern gdrom_disc_t gdrom_disc;
33 DEFINE_HOOK( gdrom_disc_change_hook, gdrom_disc_change_hook_t )
35 static void gdrom_fire_disc_changed( gdrom_disc_t disc )
36 {
37 CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
38 }
40 char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio",
41 "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
42 uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
45 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
46 {
47 struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
48 dev->name = g_strdup(name);
49 dev->device_name = g_strdup(dev_name);
50 return dev;
51 }
53 void gdrom_device_destroy( gdrom_device_t dev )
54 {
55 if( dev->name != NULL ) {
56 g_free( dev->name );
57 dev->name = NULL;
58 }
59 if( dev->device_name != NULL ) {
60 g_free( dev->device_name );
61 dev->device_name = NULL;
62 }
63 g_free( dev );
64 }
66 void gdrom_mount_disc( gdrom_disc_t disc )
67 {
68 if( disc != gdrom_disc ) {
69 if( gdrom_disc != NULL ) {
70 gdrom_disc->destroy(gdrom_disc,TRUE);
71 }
72 gdrom_disc = disc;
73 gdrom_disc_read_title( disc );
74 gdrom_fire_disc_changed( disc );
75 }
76 }
78 gboolean gdrom_mount_image( const gchar *filename )
79 {
80 gdrom_disc_t disc = gdrom_image_open(filename);
81 if( disc != NULL ) {
82 gdrom_mount_disc( disc );
83 return TRUE;
84 }
85 return FALSE;
86 }
88 void gdrom_unmount_disc( )
89 {
90 if( gdrom_disc != NULL ) {
91 gdrom_disc->destroy(gdrom_disc, TRUE);
92 gdrom_fire_disc_changed(NULL);
93 gdrom_disc = NULL;
94 }
95 }
97 gdrom_disc_t gdrom_get_current_disc()
98 {
99 return gdrom_disc;
100 }
102 const gchar *gdrom_get_current_disc_name()
103 {
104 if( gdrom_disc == NULL ) {
105 return NULL;
106 } else {
107 return gdrom_disc->name;
108 }
109 }
111 const gchar *gdrom_get_current_disc_title()
112 {
113 if( gdrom_disc == NULL || gdrom_disc->title[0] == '\0' ) {
114 return NULL;
115 } else {
116 return gdrom_disc->title;
117 }
118 }
120 int gdrom_disc_get_track_by_lba( gdrom_disc_t disc, uint32_t lba )
121 {
122 int i;
123 for( i=0; i<disc->track_count; i++ ) {
124 if( disc->track[i].lba <= lba &&
125 lba < (disc->track[i].lba + disc->track[i].sector_count) ) {
126 return i+1;
127 }
128 }
129 return -1;
130 }
132 #define CHECK_DISC(disc) do { \
133 if( disc == NULL ) { return PKT_ERR_NODISC; } \
134 disc->check_status(disc); \
135 if( disc->disc_type == IDE_DISC_NONE ) { return PKT_ERR_NODISC; } \
136 } while(0)
138 gdrom_error_t gdrom_disc_check_media( gdrom_disc_t disc )
139 {
140 CHECK_DISC(disc);
141 return PKT_ERR_OK;
142 }
144 gdrom_error_t gdrom_disc_get_toc( gdrom_disc_t disc, unsigned char *buf )
145 {
146 struct gdrom_toc {
147 uint32_t track[99];
148 uint32_t first, last, leadout;
149 };
151 struct gdrom_toc *toc = (struct gdrom_toc *)buf;
152 int i;
154 CHECK_DISC(disc);
156 for( i=0; i<disc->track_count; i++ ) {
157 toc->track[i] = htonl( disc->track[i].lba ) | disc->track[i].flags;
158 }
159 toc->first = 0x0100 | disc->track[0].flags;
160 toc->last = (disc->track_count<<8) | disc->track[i-1].flags;
161 toc->leadout = htonl(disc->track[i-1].lba + disc->track[i-1].sector_count) |
162 disc->track[i-1].flags;
163 for( ;i<99; i++ )
164 toc->track[i] = 0xFFFFFFFF;
165 return PKT_ERR_OK;
166 }
168 gdrom_error_t gdrom_disc_get_session_info( gdrom_disc_t disc, int session, unsigned char *buf )
169 {
170 CHECK_DISC(disc);
172 struct gdrom_track *last_track = &disc->track[disc->track_count-1];
173 unsigned int end_of_disc = last_track->lba + last_track->sector_count;
174 int i;
175 buf[0] = 0x01; /* Disc status? */
176 buf[1] = 0;
178 if( session == 0 ) {
179 buf[2] = last_track->session+1; /* last session */
180 buf[3] = (end_of_disc >> 16) & 0xFF;
181 buf[4] = (end_of_disc >> 8) & 0xFF;
182 buf[5] = end_of_disc & 0xFF;
183 return PKT_ERR_OK;
184 } else {
185 session--;
186 for( i=0; i<disc->track_count; i++ ) {
187 if( disc->track[i].session == session ) {
188 buf[2] = i+1; /* first track of session */
189 buf[3] = (disc->track[i].lba >> 16) & 0xFF;
190 buf[4] = (disc->track[i].lba >> 8) & 0xFF;
191 buf[5] = disc->track[i].lba & 0xFF;
192 return PKT_ERR_OK;
193 }
194 }
195 return PKT_ERR_BADFIELD; /* No such session */
196 }
197 }
199 gdrom_error_t gdrom_disc_get_short_status( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
200 {
201 CHECK_DISC(disc);
203 int track_no = gdrom_disc_get_track_by_lba( disc, lba );
204 if( track_no == -1 ) {
205 track_no = 1;
206 lba = 150;
207 }
208 struct gdrom_track *track = &disc->track[track_no-1];
209 uint32_t offset = lba - track->lba;
210 buf[0] = 0x00;
211 buf[1] = 0x15; /* audio status ? */
212 buf[2] = 0x00;
213 buf[3] = 0x0E;
214 buf[4] = track->flags;
215 buf[5] = track_no;
216 buf[6] = 0x01; /* ?? */
217 buf[7] = (offset >> 16) & 0xFF;
218 buf[8] = (offset >> 8) & 0xFF;
219 buf[9] = offset & 0xFF;
220 buf[10] = 0;
221 buf[11] = (lba >> 16) & 0xFF;
222 buf[12] = (lba >> 8) & 0xFF;
223 buf[13] = lba & 0xFF;
224 return PKT_ERR_OK;
225 }
227 int gdrom_disc_get_drive_status( gdrom_disc_t disc )
228 {
229 if( disc == NULL ) {
230 return IDE_DISC_NONE;
231 }
233 disc->check_status(disc);
234 if( disc->disc_type == IDE_DISC_NONE ) {
235 return IDE_DISC_NONE;
236 } else {
237 return disc->disc_type | IDE_DISC_READY;
238 }
239 }
241 /**
242 * Check the disc for a useable DC bootstrap, and update the disc
243 * with the title accordingly.
244 * @return TRUE if we found a bootstrap, otherwise FALSE.
245 */
246 gboolean gdrom_disc_read_title( gdrom_disc_t disc ) {
247 if( disc->track_count > 0 ) {
248 /* Find the first data track of the last session */
249 int last_session = disc->track[disc->track_count-1].session;
250 int i, boot_track = -1;
251 for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
252 if( disc->track[i].flags & TRACK_DATA ) {
253 boot_track = i;
254 }
255 }
256 if( boot_track != -1 ) {
257 unsigned char boot_sector[MAX_SECTOR_SIZE];
258 uint32_t length = sizeof(boot_sector);
259 if( disc->read_sector( disc, disc->track[boot_track].lba, 0x28,
260 boot_sector, &length ) == PKT_ERR_OK ) {
261 if( memcmp( boot_sector, "SEGA SEGAKATANA SEGA ENTERPRISES", 32) == 0 ) {
262 /* Got magic */
263 memcpy( disc->title, boot_sector+128, 128 );
264 for( i=127; i>=0; i-- ) {
265 if( !isspace(disc->title[i]) )
266 break;
267 }
268 disc->title[i+1] = '\0';
269 }
270 bootstrap_dump(boot_sector, FALSE);
271 return TRUE;
272 }
273 }
274 }
275 return FALSE;
276 }
.